blob: 44fc94fee2c4ca3e2b5738ddef355c460c796e19 [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 Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050056static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Mason1832a6d2007-12-21 16:27:21 -050075int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
76 int for_del)
77{
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
80 u64 thresh;
81 int ret = 0;
82
83 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050084 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 else
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 85;
87
88 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050089
90 spin_lock(&root->fs_info->delalloc_lock);
91 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
92 ret = -ENOSPC;
93 spin_unlock(&root->fs_info->delalloc_lock);
94 return ret;
95}
96
Chris Masonbe20aa92007-12-17 20:14:01 -050097static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040098{
99 struct btrfs_root *root = BTRFS_I(inode)->root;
100 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400101 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400102 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500103 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500105 u64 orig_start = start;
106 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500107 struct btrfs_key ins;
108 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400109
Chris Masonb888db22007-08-27 16:49:44 -0400110 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400111 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 btrfs_set_trans_block_group(trans, inode);
113
Chris Masondb945352007-10-15 16:15:53 -0400114 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400116 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400117 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500118 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400119
Chris Mason179e29e2007-11-01 11:28:41 -0400120 if (alloc_hint == EXTENT_MAP_INLINE)
121 goto out;
122
Chris Masonc59f8952007-12-17 20:14:04 -0500123 while(num_bytes > 0) {
124 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
125 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
126 root->root_key.objectid,
127 trans->transid,
128 inode->i_ino, start, 0,
129 alloc_hint, (u64)-1, &ins, 1);
130 if (ret) {
131 WARN_ON(1);
132 goto out;
133 }
134 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
135 start, ins.objectid, ins.offset,
136 ins.offset);
Chris Mason5f564062008-01-22 16:47:59 -0500137 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500138 num_bytes -= cur_alloc_size;
139 alloc_hint = ins.objectid + ins.offset;
140 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400141 }
Chris Masond1310b22008-01-24 16:13:08 -0500142 btrfs_drop_extent_cache(inode, orig_start,
143 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500144 btrfs_add_ordered_inode(inode);
Chris Masonb888db22007-08-27 16:49:44 -0400145out:
146 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500147 return ret;
148}
149
150static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
151{
152 u64 extent_start;
153 u64 extent_end;
154 u64 bytenr;
155 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500156 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500157 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500158 struct btrfs_root *root = BTRFS_I(inode)->root;
159 struct extent_buffer *leaf;
160 int found_type;
161 struct btrfs_path *path;
162 struct btrfs_file_extent_item *item;
163 int ret;
164 int err;
165 struct btrfs_key found_key;
166
Chris Masonc31f8832008-01-08 15:46:31 -0500167 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500168 path = btrfs_alloc_path();
169 BUG_ON(!path);
170again:
171 ret = btrfs_lookup_file_extent(NULL, root, path,
172 inode->i_ino, start, 0);
173 if (ret < 0) {
174 btrfs_free_path(path);
175 return ret;
176 }
177
178 cow_end = end;
179 if (ret != 0) {
180 if (path->slots[0] == 0)
181 goto not_found;
182 path->slots[0]--;
183 }
184
185 leaf = path->nodes[0];
186 item = btrfs_item_ptr(leaf, path->slots[0],
187 struct btrfs_file_extent_item);
188
189 /* are we inside the extent that was found? */
190 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
191 found_type = btrfs_key_type(&found_key);
192 if (found_key.objectid != inode->i_ino ||
193 found_type != BTRFS_EXTENT_DATA_KEY) {
194 goto not_found;
195 }
196
197 found_type = btrfs_file_extent_type(leaf, item);
198 extent_start = found_key.offset;
199 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500200 u64 extent_num_bytes;
201
202 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
203 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 err = 0;
205
Chris Mason1832a6d2007-12-21 16:27:21 -0500206 if (loops && start != extent_start)
207 goto not_found;
208
Chris Masonbe20aa92007-12-17 20:14:01 -0500209 if (start < extent_start || start >= extent_end)
210 goto not_found;
211
212 cow_end = min(end, extent_end - 1);
213 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
214 if (bytenr == 0)
215 goto not_found;
216
Chris Masonc31f8832008-01-08 15:46:31 -0500217 /*
218 * we may be called by the resizer, make sure we're inside
219 * the limits of the FS
220 */
221 if (bytenr + extent_num_bytes > total_fs_bytes)
222 goto not_found;
223
Chris Masonbe20aa92007-12-17 20:14:01 -0500224 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
225 goto not_found;
226 }
227
228 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500229 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 goto not_found;
231 }
232loop:
233 if (start > end) {
234 btrfs_free_path(path);
235 return 0;
236 }
237 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500238 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 goto again;
240
241not_found:
242 cow_file_range(inode, start, cow_end);
243 start = cow_end + 1;
244 goto loop;
245}
246
247static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
248{
249 struct btrfs_root *root = BTRFS_I(inode)->root;
250 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500251 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500252 if (btrfs_test_opt(root, NODATACOW) ||
253 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 ret = run_delalloc_nocow(inode, start, end);
255 else
256 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500257
Chris Masonb888db22007-08-27 16:49:44 -0400258 mutex_unlock(&root->fs_info->fs_mutex);
259 return ret;
260}
261
Chris Mason291d6732008-01-29 15:55:23 -0500262int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500263 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500264{
Chris Masonb0c68f82008-01-31 11:05:37 -0500265 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500266 struct btrfs_root *root = BTRFS_I(inode)->root;
267 spin_lock(&root->fs_info->delalloc_lock);
268 root->fs_info->delalloc_bytes += end - start + 1;
269 spin_unlock(&root->fs_info->delalloc_lock);
270 }
271 return 0;
272}
273
274int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500275 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500276{
Chris Masonb0c68f82008-01-31 11:05:37 -0500277 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500278 struct btrfs_root *root = BTRFS_I(inode)->root;
279 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500280 if (end - start + 1 > root->fs_info->delalloc_bytes) {
281 printk("warning: delalloc account %Lu %Lu\n",
282 end - start + 1, root->fs_info->delalloc_bytes);
283 root->fs_info->delalloc_bytes = 0;
284 } else {
285 root->fs_info->delalloc_bytes -= end - start + 1;
286 }
Chris Mason291d6732008-01-29 15:55:23 -0500287 spin_unlock(&root->fs_info->delalloc_lock);
288 }
289 return 0;
290}
291
Chris Mason07157aa2007-08-30 08:50:51 -0400292int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
293{
294 struct inode *inode = page->mapping->host;
295 struct btrfs_root *root = BTRFS_I(inode)->root;
296 struct btrfs_trans_handle *trans;
297 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500298 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400299 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400300 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500301 if (btrfs_test_opt(root, NODATASUM) ||
302 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500303 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400304 mutex_lock(&root->fs_info->fs_mutex);
305 trans = btrfs_start_transaction(root, 1);
306 btrfs_set_trans_block_group(trans, inode);
307 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400308 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400309 start, kaddr + offset, end - start + 1);
310 kunmap(page);
311 ret = btrfs_end_transaction(trans, root);
312 BUG_ON(ret);
313 mutex_unlock(&root->fs_info->fs_mutex);
314 return ret;
315}
316
317int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
318{
319 int ret = 0;
320 struct inode *inode = page->mapping->host;
321 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500322 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400323 struct btrfs_csum_item *item;
324 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400325 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500326 if (btrfs_test_opt(root, NODATASUM) ||
327 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500328 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400329 mutex_lock(&root->fs_info->fs_mutex);
330 path = btrfs_alloc_path();
331 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
332 if (IS_ERR(item)) {
333 ret = PTR_ERR(item);
334 /* a csum that isn't present is a preallocated region. */
335 if (ret == -ENOENT || ret == -EFBIG)
336 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400337 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500338 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400339 goto out;
340 }
Chris Masonff79f812007-10-15 16:22:25 -0400341 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
342 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500343 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400344out:
345 if (path)
346 btrfs_free_path(path);
347 mutex_unlock(&root->fs_info->fs_mutex);
348 return ret;
349}
350
Chris Mason70dec802008-01-29 09:59:12 -0500351int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
352 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400353{
Chris Mason35ebb932007-10-30 16:56:53 -0400354 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400355 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400357 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500358 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400359 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400360 struct btrfs_root *root = BTRFS_I(inode)->root;
361 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -0400362 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500363
Yanb98b6762008-01-08 15:54:37 -0500364 if (btrfs_test_opt(root, NODATASUM) ||
365 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500366 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500367 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500368 private = state->private;
369 ret = 0;
370 } else {
371 ret = get_state_private(io_tree, start, &private);
372 }
Jens Axboebbf0d0062007-10-19 09:23:07 -0400373 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400374 kaddr = kmap_atomic(page, KM_IRQ0);
375 if (ret) {
376 goto zeroit;
377 }
Chris Masonff79f812007-10-15 16:22:25 -0400378 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
379 btrfs_csum_final(csum, (char *)&csum);
380 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400381 goto zeroit;
382 }
383 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400384 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400385 return 0;
386
387zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500388 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
389 page->mapping->host->i_ino, (unsigned long long)start, csum,
390 private);
Chris Masondb945352007-10-15 16:15:53 -0400391 memset(kaddr + offset, 1, end - start + 1);
392 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400393 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400394 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400395 return 0;
396}
Chris Masonb888db22007-08-27 16:49:44 -0400397
Chris Mason39279cc2007-06-12 06:35:45 -0400398void btrfs_read_locked_inode(struct inode *inode)
399{
400 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400401 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400402 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400403 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400404 struct btrfs_root *root = BTRFS_I(inode)->root;
405 struct btrfs_key location;
406 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400407 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400408 int ret;
409
410 path = btrfs_alloc_path();
411 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400412 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400413 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500414
Chris Mason39279cc2007-06-12 06:35:45 -0400415 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400416 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400417 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400418
Chris Mason5f39d392007-10-15 16:14:19 -0400419 leaf = path->nodes[0];
420 inode_item = btrfs_item_ptr(leaf, path->slots[0],
421 struct btrfs_inode_item);
422
423 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
424 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
425 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
426 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
427 inode->i_size = btrfs_inode_size(leaf, inode_item);
428
429 tspec = btrfs_inode_atime(inode_item);
430 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
431 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
432
433 tspec = btrfs_inode_mtime(inode_item);
434 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
435 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
436
437 tspec = btrfs_inode_ctime(inode_item);
438 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
439 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
440
441 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
442 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400443 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400444 rdev = btrfs_inode_rdev(leaf, inode_item);
445
446 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400447 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
448 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500449 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500450 if (!BTRFS_I(inode)->block_group) {
451 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
452 NULL, 0, 0, 0);
453 }
Chris Mason39279cc2007-06-12 06:35:45 -0400454 btrfs_free_path(path);
455 inode_item = NULL;
456
457 mutex_unlock(&root->fs_info->fs_mutex);
458
459 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400460 case S_IFREG:
461 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500462 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400463 inode->i_fop = &btrfs_file_operations;
464 inode->i_op = &btrfs_file_inode_operations;
465 break;
466 case S_IFDIR:
467 inode->i_fop = &btrfs_dir_file_operations;
468 if (root == root->fs_info->tree_root)
469 inode->i_op = &btrfs_dir_ro_inode_operations;
470 else
471 inode->i_op = &btrfs_dir_inode_operations;
472 break;
473 case S_IFLNK:
474 inode->i_op = &btrfs_symlink_inode_operations;
475 inode->i_mapping->a_ops = &btrfs_symlink_aops;
476 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400477 default:
478 init_special_inode(inode, inode->i_mode, rdev);
479 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400480 }
481 return;
482
483make_bad:
484 btrfs_release_path(root, path);
485 btrfs_free_path(path);
486 mutex_unlock(&root->fs_info->fs_mutex);
487 make_bad_inode(inode);
488}
489
Chris Mason5f39d392007-10-15 16:14:19 -0400490static void fill_inode_item(struct extent_buffer *leaf,
491 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400492 struct inode *inode)
493{
Chris Mason5f39d392007-10-15 16:14:19 -0400494 btrfs_set_inode_uid(leaf, item, inode->i_uid);
495 btrfs_set_inode_gid(leaf, item, inode->i_gid);
496 btrfs_set_inode_size(leaf, item, inode->i_size);
497 btrfs_set_inode_mode(leaf, item, inode->i_mode);
498 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
499
500 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
501 inode->i_atime.tv_sec);
502 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
503 inode->i_atime.tv_nsec);
504
505 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
506 inode->i_mtime.tv_sec);
507 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
508 inode->i_mtime.tv_nsec);
509
510 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
511 inode->i_ctime.tv_sec);
512 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
513 inode->i_ctime.tv_nsec);
514
515 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
516 btrfs_set_inode_generation(leaf, item, inode->i_generation);
517 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500518 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400519 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400520 BTRFS_I(inode)->block_group->key.objectid);
521}
522
Chris Masona52d9a82007-08-27 16:49:44 -0400523int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400524 struct btrfs_root *root,
525 struct inode *inode)
526{
527 struct btrfs_inode_item *inode_item;
528 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400529 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400530 int ret;
531
532 path = btrfs_alloc_path();
533 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400534 ret = btrfs_lookup_inode(trans, root, path,
535 &BTRFS_I(inode)->location, 1);
536 if (ret) {
537 if (ret > 0)
538 ret = -ENOENT;
539 goto failed;
540 }
541
Chris Mason5f39d392007-10-15 16:14:19 -0400542 leaf = path->nodes[0];
543 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400544 struct btrfs_inode_item);
545
Chris Mason5f39d392007-10-15 16:14:19 -0400546 fill_inode_item(leaf, inode_item, inode);
547 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400548 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400549 ret = 0;
550failed:
551 btrfs_release_path(root, path);
552 btrfs_free_path(path);
553 return ret;
554}
555
556
557static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
558 struct btrfs_root *root,
559 struct inode *dir,
560 struct dentry *dentry)
561{
562 struct btrfs_path *path;
563 const char *name = dentry->d_name.name;
564 int name_len = dentry->d_name.len;
565 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400566 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400567 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400568 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400569
570 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400571 if (!path) {
572 ret = -ENOMEM;
573 goto err;
574 }
575
Chris Mason39279cc2007-06-12 06:35:45 -0400576 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
577 name, name_len, -1);
578 if (IS_ERR(di)) {
579 ret = PTR_ERR(di);
580 goto err;
581 }
582 if (!di) {
583 ret = -ENOENT;
584 goto err;
585 }
Chris Mason5f39d392007-10-15 16:14:19 -0400586 leaf = path->nodes[0];
587 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400588 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400589 if (ret)
590 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400591 btrfs_release_path(root, path);
592
593 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400594 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400595 if (IS_ERR(di)) {
596 ret = PTR_ERR(di);
597 goto err;
598 }
599 if (!di) {
600 ret = -ENOENT;
601 goto err;
602 }
603 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400604
605 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500606 ret = btrfs_del_inode_ref(trans, root, name, name_len,
607 dentry->d_inode->i_ino,
608 dentry->d_parent->d_inode->i_ino);
609 if (ret) {
610 printk("failed to delete reference to %.*s, "
611 "inode %lu parent %lu\n", name_len, name,
612 dentry->d_inode->i_ino,
613 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500614 }
Chris Mason39279cc2007-06-12 06:35:45 -0400615err:
616 btrfs_free_path(path);
617 if (!ret) {
618 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400619 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400620 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500621#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
622 dentry->d_inode->i_nlink--;
623#else
Chris Mason39279cc2007-06-12 06:35:45 -0400624 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500625#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400626 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400627 dir->i_sb->s_dirt = 1;
628 }
629 return ret;
630}
631
632static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
633{
634 struct btrfs_root *root;
635 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500636 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400637 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500638 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400639
640 root = BTRFS_I(dir)->root;
641 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500642
643 ret = btrfs_check_free_space(root, 1, 1);
644 if (ret)
645 goto fail;
646
Chris Mason39279cc2007-06-12 06:35:45 -0400647 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400648
Chris Mason39279cc2007-06-12 06:35:45 -0400649 btrfs_set_trans_block_group(trans, dir);
650 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400651 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400652
Chris Mason2da98f02008-01-16 11:44:43 -0500653 if (inode->i_nlink == 0) {
654 int found;
655 /* if the inode isn't linked anywhere,
656 * we don't need to worry about
657 * data=ordered
658 */
659 found = btrfs_del_ordered_inode(inode);
660 if (found == 1) {
661 atomic_dec(&inode->i_count);
662 }
663 }
664
Chris Mason39279cc2007-06-12 06:35:45 -0400665 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500666fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400667 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400668 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500669 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400670 return ret;
671}
672
673static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
674{
675 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500676 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400677 int ret;
678 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400679 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500680 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400681
Yan134d4512007-10-25 15:49:25 -0400682 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
683 return -ENOTEMPTY;
684
Chris Mason39279cc2007-06-12 06:35:45 -0400685 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500686 ret = btrfs_check_free_space(root, 1, 1);
687 if (ret)
688 goto fail;
689
Chris Mason39279cc2007-06-12 06:35:45 -0400690 trans = btrfs_start_transaction(root, 1);
691 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400692
693 /* now the directory is empty */
694 err = btrfs_unlink_trans(trans, root, dir, dentry);
695 if (!err) {
696 inode->i_size = 0;
697 }
Chris Mason39544012007-12-12 14:38:19 -0500698
Chris Masond3c2fdc2007-09-17 10:58:06 -0400699 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400700 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500701fail:
Yan134d4512007-10-25 15:49:25 -0400702 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400703 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500704 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500705
Chris Mason39279cc2007-06-12 06:35:45 -0400706 if (ret && !err)
707 err = ret;
708 return err;
709}
710
Chris Mason39279cc2007-06-12 06:35:45 -0400711/*
Chris Mason39279cc2007-06-12 06:35:45 -0400712 * this can truncate away extent items, csum items and directory items.
713 * It starts at a high offset and removes keys until it can't find
714 * any higher than i_size.
715 *
716 * csum items that cross the new i_size are truncated to the new size
717 * as well.
718 */
719static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
720 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500721 struct inode *inode,
722 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400723{
724 int ret;
725 struct btrfs_path *path;
726 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400727 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400728 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400729 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400730 struct btrfs_file_extent_item *fi;
731 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400732 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400733 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500734 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500735 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400736 int found_extent;
737 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500738 int pending_del_nr = 0;
739 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400740 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400741
Chris Masona52d9a82007-08-27 16:49:44 -0400742 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400743 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400744 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400745 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400746
Chris Mason39279cc2007-06-12 06:35:45 -0400747 /* FIXME, add redo link to tree so we don't leak on crash */
748 key.objectid = inode->i_ino;
749 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400750 key.type = (u8)-1;
751
Chris Mason85e21ba2008-01-29 15:11:36 -0500752 btrfs_init_path(path);
753search_again:
754 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
755 if (ret < 0) {
756 goto error;
757 }
758 if (ret > 0) {
759 BUG_ON(path->slots[0] == 0);
760 path->slots[0]--;
761 }
762
Chris Mason39279cc2007-06-12 06:35:45 -0400763 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400764 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400765 leaf = path->nodes[0];
766 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
767 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400768
Chris Mason5f39d392007-10-15 16:14:19 -0400769 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400770 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400771
Chris Mason85e21ba2008-01-29 15:11:36 -0500772 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400773 break;
774
Chris Mason5f39d392007-10-15 16:14:19 -0400775 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400776 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400777 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400778 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400779 extent_type = btrfs_file_extent_type(leaf, fi);
780 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400781 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400782 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400783 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
784 struct btrfs_item *item = btrfs_item_nr(leaf,
785 path->slots[0]);
786 item_end += btrfs_file_extent_inline_len(leaf,
787 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400788 }
Yan008630c2007-11-07 13:31:09 -0500789 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400790 }
791 if (found_type == BTRFS_CSUM_ITEM_KEY) {
792 ret = btrfs_csum_truncate(trans, root, path,
793 inode->i_size);
794 BUG_ON(ret);
795 }
Yan008630c2007-11-07 13:31:09 -0500796 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400797 if (found_type == BTRFS_DIR_ITEM_KEY) {
798 found_type = BTRFS_INODE_ITEM_KEY;
799 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
800 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500801 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
802 found_type = BTRFS_XATTR_ITEM_KEY;
803 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
804 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400805 } else if (found_type) {
806 found_type--;
807 } else {
808 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400809 }
Yana61721d2007-09-17 11:08:38 -0400810 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500811 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400812 }
Chris Mason5f39d392007-10-15 16:14:19 -0400813 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400814 del_item = 1;
815 else
816 del_item = 0;
817 found_extent = 0;
818
819 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400820 if (found_type != BTRFS_EXTENT_DATA_KEY)
821 goto delete;
822
823 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400824 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400825 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400826 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400827 u64 orig_num_bytes =
828 btrfs_file_extent_num_bytes(leaf, fi);
829 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400830 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -0500831 extent_num_bytes = extent_num_bytes &
832 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400833 btrfs_set_file_extent_num_bytes(leaf, fi,
834 extent_num_bytes);
835 num_dec = (orig_num_bytes -
836 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400837 if (extent_start != 0) {
838 inode->i_blocks -= num_dec;
839 }
Chris Mason5f39d392007-10-15 16:14:19 -0400840 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400841 } else {
Chris Masondb945352007-10-15 16:15:53 -0400842 extent_num_bytes =
843 btrfs_file_extent_disk_num_bytes(leaf,
844 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400845 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400846 num_dec = btrfs_file_extent_num_bytes(leaf,
847 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400848 if (extent_start != 0) {
849 found_extent = 1;
850 inode->i_blocks -= num_dec;
851 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500852 root_gen = btrfs_header_generation(leaf);
853 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400854 }
Chris Mason179e29e2007-11-01 11:28:41 -0400855 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
856 !del_item) {
857 u32 newsize = inode->i_size - found_key.offset;
858 newsize = btrfs_file_extent_calc_inline_size(newsize);
859 ret = btrfs_truncate_item(trans, root, path,
860 newsize, 1);
861 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400862 }
Chris Mason179e29e2007-11-01 11:28:41 -0400863delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400864 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500865 if (!pending_del_nr) {
866 /* no pending yet, add ourselves */
867 pending_del_slot = path->slots[0];
868 pending_del_nr = 1;
869 } else if (pending_del_nr &&
870 path->slots[0] + 1 == pending_del_slot) {
871 /* hop on the pending chunk */
872 pending_del_nr++;
873 pending_del_slot = path->slots[0];
874 } else {
875 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
876 }
Chris Mason39279cc2007-06-12 06:35:45 -0400877 } else {
878 break;
879 }
Chris Mason39279cc2007-06-12 06:35:45 -0400880 if (found_extent) {
881 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500882 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500883 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500884 root_gen, inode->i_ino,
885 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400886 BUG_ON(ret);
887 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500888next:
889 if (path->slots[0] == 0) {
890 if (pending_del_nr)
891 goto del_pending;
892 btrfs_release_path(root, path);
893 goto search_again;
894 }
895
896 path->slots[0]--;
897 if (pending_del_nr &&
898 path->slots[0] + 1 != pending_del_slot) {
899 struct btrfs_key debug;
900del_pending:
901 btrfs_item_key_to_cpu(path->nodes[0], &debug,
902 pending_del_slot);
903 ret = btrfs_del_items(trans, root, path,
904 pending_del_slot,
905 pending_del_nr);
906 BUG_ON(ret);
907 pending_del_nr = 0;
908 btrfs_release_path(root, path);
909 goto search_again;
910 }
Chris Mason39279cc2007-06-12 06:35:45 -0400911 }
912 ret = 0;
913error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500914 if (pending_del_nr) {
915 ret = btrfs_del_items(trans, root, path, pending_del_slot,
916 pending_del_nr);
917 }
Chris Mason39279cc2007-06-12 06:35:45 -0400918 btrfs_release_path(root, path);
919 btrfs_free_path(path);
920 inode->i_sb->s_dirt = 1;
921 return ret;
922}
923
Chris Masonb888db22007-08-27 16:49:44 -0400924static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400925 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400926{
Chris Mason39279cc2007-06-12 06:35:45 -0400927 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400929 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400930 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500931 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400932
Chris Mason190662b2007-12-18 16:25:45 -0500933 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400934 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400935
Chris Masond1310b22008-01-24 16:13:08 -0500936 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500937 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400938 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500939
Chris Masona52d9a82007-08-27 16:49:44 -0400940 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400941 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400942 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
943 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400944 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400945 }
Chris Masonb888db22007-08-27 16:49:44 -0400946 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500947 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400948
Chris Masona52d9a82007-08-27 16:49:44 -0400949 return ret;
950}
951
952/*
953 * taken from block_truncate_page, but does cow as it zeros out
954 * any bytes left in the last page in the file.
955 */
956static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
957{
958 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400959 struct btrfs_root *root = BTRFS_I(inode)->root;
960 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400961 pgoff_t index = from >> PAGE_CACHE_SHIFT;
962 unsigned offset = from & (PAGE_CACHE_SIZE-1);
963 struct page *page;
964 int ret = 0;
965 u64 page_start;
966
967 if ((offset & (blocksize - 1)) == 0)
968 goto out;
969
970 ret = -ENOMEM;
971 page = grab_cache_page(mapping, index);
972 if (!page)
973 goto out;
974 if (!PageUptodate(page)) {
975 ret = btrfs_readpage(NULL, page);
976 lock_page(page);
977 if (!PageUptodate(page)) {
978 ret = -EIO;
979 goto out;
980 }
981 }
Chris Mason35ebb932007-10-30 16:56:53 -0400982 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400983
Chris Masonb888db22007-08-27 16:49:44 -0400984 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400985
Chris Mason39279cc2007-06-12 06:35:45 -0400986 unlock_page(page);
987 page_cache_release(page);
988out:
989 return ret;
990}
991
992static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
993{
994 struct inode *inode = dentry->d_inode;
995 int err;
996
997 err = inode_change_ok(inode, attr);
998 if (err)
999 return err;
1000
1001 if (S_ISREG(inode->i_mode) &&
1002 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1003 struct btrfs_trans_handle *trans;
1004 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001005 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001006
Chris Mason5f39d392007-10-15 16:14:19 -04001007 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001008 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001009 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001010 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001011 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001012
Chris Mason1b0f7c22008-01-30 14:33:02 -05001013 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001014 goto out;
1015
Chris Mason1832a6d2007-12-21 16:27:21 -05001016 mutex_lock(&root->fs_info->fs_mutex);
1017 err = btrfs_check_free_space(root, 1, 0);
1018 mutex_unlock(&root->fs_info->fs_mutex);
1019 if (err)
1020 goto fail;
1021
Chris Mason39279cc2007-06-12 06:35:45 -04001022 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1023
Chris Mason1b0f7c22008-01-30 14:33:02 -05001024 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001025 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001026
1027 mutex_lock(&root->fs_info->fs_mutex);
1028 trans = btrfs_start_transaction(root, 1);
1029 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001030 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001031 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001032 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001033
Chris Mason179e29e2007-11-01 11:28:41 -04001034 if (alloc_hint != EXTENT_MAP_INLINE) {
1035 err = btrfs_insert_file_extent(trans, root,
1036 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001037 hole_start, 0, 0,
1038 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001039 btrfs_drop_extent_cache(inode, hole_start,
1040 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001041 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001042 }
Chris Mason39279cc2007-06-12 06:35:45 -04001043 btrfs_end_transaction(trans, root);
1044 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001045 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001046 if (err)
1047 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001048 }
1049out:
1050 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001051fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001052 return err;
1053}
Chris Mason61295eb2008-01-14 16:24:38 -05001054
Chris Mason2da98f02008-01-16 11:44:43 -05001055void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001056{
Chris Mason2da98f02008-01-16 11:44:43 -05001057 int ret;
1058
1059 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001060 return;
1061 }
Chris Mason2da98f02008-01-16 11:44:43 -05001062
1063 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1064 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1065 return;
1066
1067 ret = btrfs_del_ordered_inode(inode);
1068 if (ret == 1) {
1069 atomic_dec(&inode->i_count);
1070 }
Chris Mason61295eb2008-01-14 16:24:38 -05001071}
1072
Chris Mason39279cc2007-06-12 06:35:45 -04001073void btrfs_delete_inode(struct inode *inode)
1074{
1075 struct btrfs_trans_handle *trans;
1076 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001077 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001078 int ret;
1079
1080 truncate_inode_pages(&inode->i_data, 0);
1081 if (is_bad_inode(inode)) {
1082 goto no_delete;
1083 }
Chris Mason5f39d392007-10-15 16:14:19 -04001084
Chris Mason39279cc2007-06-12 06:35:45 -04001085 inode->i_size = 0;
1086 mutex_lock(&root->fs_info->fs_mutex);
1087 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001088
Chris Mason39279cc2007-06-12 06:35:45 -04001089 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001090 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001091 if (ret)
1092 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001093
Chris Masond3c2fdc2007-09-17 10:58:06 -04001094 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001095 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001096
Chris Mason39279cc2007-06-12 06:35:45 -04001097 btrfs_end_transaction(trans, root);
1098 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001099 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001100 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001101 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001102
1103no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001104 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001105 btrfs_end_transaction(trans, root);
1106 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001107 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001108 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001109no_delete:
1110 clear_inode(inode);
1111}
1112
1113/*
1114 * this returns the key found in the dir entry in the location pointer.
1115 * If no dir entries were found, location->objectid is 0.
1116 */
1117static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1118 struct btrfs_key *location)
1119{
1120 const char *name = dentry->d_name.name;
1121 int namelen = dentry->d_name.len;
1122 struct btrfs_dir_item *di;
1123 struct btrfs_path *path;
1124 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001125 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001126
Chris Mason39544012007-12-12 14:38:19 -05001127 if (namelen == 1 && strcmp(name, ".") == 0) {
1128 location->objectid = dir->i_ino;
1129 location->type = BTRFS_INODE_ITEM_KEY;
1130 location->offset = 0;
1131 return 0;
1132 }
Chris Mason39279cc2007-06-12 06:35:45 -04001133 path = btrfs_alloc_path();
1134 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001135
Chris Mason7a720532007-12-13 09:06:59 -05001136 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001137 struct btrfs_key key;
1138 struct extent_buffer *leaf;
1139 u32 nritems;
1140 int slot;
1141
1142 key.objectid = dir->i_ino;
1143 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1144 key.offset = 0;
1145 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1146 BUG_ON(ret == 0);
1147 ret = 0;
1148
1149 leaf = path->nodes[0];
1150 slot = path->slots[0];
1151 nritems = btrfs_header_nritems(leaf);
1152 if (slot >= nritems)
1153 goto out_err;
1154
1155 btrfs_item_key_to_cpu(leaf, &key, slot);
1156 if (key.objectid != dir->i_ino ||
1157 key.type != BTRFS_INODE_REF_KEY) {
1158 goto out_err;
1159 }
1160 location->objectid = key.offset;
1161 location->type = BTRFS_INODE_ITEM_KEY;
1162 location->offset = 0;
1163 goto out;
1164 }
1165
Chris Mason39279cc2007-06-12 06:35:45 -04001166 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1167 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001168 if (IS_ERR(di))
1169 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001170 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001171 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001172 }
Chris Mason5f39d392007-10-15 16:14:19 -04001173 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001174out:
Chris Mason39279cc2007-06-12 06:35:45 -04001175 btrfs_free_path(path);
1176 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001177out_err:
1178 location->objectid = 0;
1179 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001180}
1181
1182/*
1183 * when we hit a tree root in a directory, the btrfs part of the inode
1184 * needs to be changed to reflect the root directory of the tree root. This
1185 * is kind of like crossing a mount point.
1186 */
1187static int fixup_tree_root_location(struct btrfs_root *root,
1188 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001189 struct btrfs_root **sub_root,
1190 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001191{
1192 struct btrfs_path *path;
1193 struct btrfs_root_item *ri;
1194
1195 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1196 return 0;
1197 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1198 return 0;
1199
1200 path = btrfs_alloc_path();
1201 BUG_ON(!path);
1202 mutex_lock(&root->fs_info->fs_mutex);
1203
Josef Bacik58176a92007-08-29 15:47:34 -04001204 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1205 dentry->d_name.name,
1206 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001207 if (IS_ERR(*sub_root))
1208 return PTR_ERR(*sub_root);
1209
1210 ri = &(*sub_root)->root_item;
1211 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001212 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1213 location->offset = 0;
1214
1215 btrfs_free_path(path);
1216 mutex_unlock(&root->fs_info->fs_mutex);
1217 return 0;
1218}
1219
1220static int btrfs_init_locked_inode(struct inode *inode, void *p)
1221{
1222 struct btrfs_iget_args *args = p;
1223 inode->i_ino = args->ino;
1224 BTRFS_I(inode)->root = args->root;
Chris Masond1310b22008-01-24 16:13:08 -05001225 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1226 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001227 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001228 return 0;
1229}
1230
1231static int btrfs_find_actor(struct inode *inode, void *opaque)
1232{
1233 struct btrfs_iget_args *args = opaque;
1234 return (args->ino == inode->i_ino &&
1235 args->root == BTRFS_I(inode)->root);
1236}
1237
Chris Masondc17ff82008-01-08 15:46:30 -05001238struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1239 u64 root_objectid)
1240{
1241 struct btrfs_iget_args args;
1242 args.ino = objectid;
1243 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1244
1245 if (!args.root)
1246 return NULL;
1247
1248 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1249}
1250
Chris Mason39279cc2007-06-12 06:35:45 -04001251struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1252 struct btrfs_root *root)
1253{
1254 struct inode *inode;
1255 struct btrfs_iget_args args;
1256 args.ino = objectid;
1257 args.root = root;
1258
1259 inode = iget5_locked(s, objectid, btrfs_find_actor,
1260 btrfs_init_locked_inode,
1261 (void *)&args);
1262 return inode;
1263}
1264
1265static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1266 struct nameidata *nd)
1267{
1268 struct inode * inode;
1269 struct btrfs_inode *bi = BTRFS_I(dir);
1270 struct btrfs_root *root = bi->root;
1271 struct btrfs_root *sub_root = root;
1272 struct btrfs_key location;
1273 int ret;
1274
1275 if (dentry->d_name.len > BTRFS_NAME_LEN)
1276 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001277
Chris Mason39279cc2007-06-12 06:35:45 -04001278 mutex_lock(&root->fs_info->fs_mutex);
1279 ret = btrfs_inode_by_name(dir, dentry, &location);
1280 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001281
Chris Mason39279cc2007-06-12 06:35:45 -04001282 if (ret < 0)
1283 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001284
Chris Mason39279cc2007-06-12 06:35:45 -04001285 inode = NULL;
1286 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001287 ret = fixup_tree_root_location(root, &location, &sub_root,
1288 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001289 if (ret < 0)
1290 return ERR_PTR(ret);
1291 if (ret > 0)
1292 return ERR_PTR(-ENOENT);
1293 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1294 sub_root);
1295 if (!inode)
1296 return ERR_PTR(-EACCES);
1297 if (inode->i_state & I_NEW) {
1298 /* the inode and parent dir are two different roots */
1299 if (sub_root != root) {
1300 igrab(inode);
1301 sub_root->inode = inode;
1302 }
1303 BTRFS_I(inode)->root = sub_root;
1304 memcpy(&BTRFS_I(inode)->location, &location,
1305 sizeof(location));
1306 btrfs_read_locked_inode(inode);
1307 unlock_new_inode(inode);
1308 }
1309 }
1310 return d_splice_alias(inode, dentry);
1311}
1312
Chris Mason39279cc2007-06-12 06:35:45 -04001313static unsigned char btrfs_filetype_table[] = {
1314 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1315};
1316
1317static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1318{
Chris Mason6da6aba2007-12-18 16:15:09 -05001319 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001320 struct btrfs_root *root = BTRFS_I(inode)->root;
1321 struct btrfs_item *item;
1322 struct btrfs_dir_item *di;
1323 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001324 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001325 struct btrfs_path *path;
1326 int ret;
1327 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001328 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001329 int slot;
1330 int advance;
1331 unsigned char d_type;
1332 int over = 0;
1333 u32 di_cur;
1334 u32 di_total;
1335 u32 di_len;
1336 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001337 char tmp_name[32];
1338 char *name_ptr;
1339 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001340
1341 /* FIXME, use a real flag for deciding about the key type */
1342 if (root->fs_info->tree_root == root)
1343 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001344
Chris Mason39544012007-12-12 14:38:19 -05001345 /* special case for "." */
1346 if (filp->f_pos == 0) {
1347 over = filldir(dirent, ".", 1,
1348 1, inode->i_ino,
1349 DT_DIR);
1350 if (over)
1351 return 0;
1352 filp->f_pos = 1;
1353 }
1354
Chris Mason39279cc2007-06-12 06:35:45 -04001355 mutex_lock(&root->fs_info->fs_mutex);
1356 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001357 path = btrfs_alloc_path();
1358 path->reada = 2;
1359
1360 /* special case for .., just use the back ref */
1361 if (filp->f_pos == 1) {
1362 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1363 key.offset = 0;
1364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1365 BUG_ON(ret == 0);
1366 leaf = path->nodes[0];
1367 slot = path->slots[0];
1368 nritems = btrfs_header_nritems(leaf);
1369 if (slot >= nritems) {
1370 btrfs_release_path(root, path);
1371 goto read_dir_items;
1372 }
1373 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1374 btrfs_release_path(root, path);
1375 if (found_key.objectid != key.objectid ||
1376 found_key.type != BTRFS_INODE_REF_KEY)
1377 goto read_dir_items;
1378 over = filldir(dirent, "..", 2,
1379 2, found_key.offset, DT_DIR);
1380 if (over)
1381 goto nopos;
1382 filp->f_pos = 2;
1383 }
1384
1385read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001386 btrfs_set_key_type(&key, key_type);
1387 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001388
Chris Mason39279cc2007-06-12 06:35:45 -04001389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1390 if (ret < 0)
1391 goto err;
1392 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001393 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001394 leaf = path->nodes[0];
1395 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001396 slot = path->slots[0];
1397 if (advance || slot >= nritems) {
1398 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001399 ret = btrfs_next_leaf(root, path);
1400 if (ret)
1401 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001402 leaf = path->nodes[0];
1403 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001404 slot = path->slots[0];
1405 } else {
1406 slot++;
1407 path->slots[0]++;
1408 }
1409 }
1410 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001411 item = btrfs_item_nr(leaf, slot);
1412 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1413
1414 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001415 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001416 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001417 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001418 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001419 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001420
1421 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001422 advance = 1;
1423 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1424 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001425 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001426 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001427 struct btrfs_key location;
1428
1429 name_len = btrfs_dir_name_len(leaf, di);
1430 if (name_len < 32) {
1431 name_ptr = tmp_name;
1432 } else {
1433 name_ptr = kmalloc(name_len, GFP_NOFS);
1434 BUG_ON(!name_ptr);
1435 }
1436 read_extent_buffer(leaf, name_ptr,
1437 (unsigned long)(di + 1), name_len);
1438
1439 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1440 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001441 over = filldir(dirent, name_ptr, name_len,
1442 found_key.offset,
1443 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001444 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001445
1446 if (name_ptr != tmp_name)
1447 kfree(name_ptr);
1448
Chris Mason39279cc2007-06-12 06:35:45 -04001449 if (over)
1450 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001451 di_len = btrfs_dir_name_len(leaf, di) +
1452 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001453 di_cur += di_len;
1454 di = (struct btrfs_dir_item *)((char *)di + di_len);
1455 }
1456 }
Chris Masonc2a8b6e2008-01-29 09:10:26 -05001457 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
Chris Mason39279cc2007-06-12 06:35:45 -04001458nopos:
1459 ret = 0;
1460err:
1461 btrfs_release_path(root, path);
1462 btrfs_free_path(path);
1463 mutex_unlock(&root->fs_info->fs_mutex);
1464 return ret;
1465}
1466
1467int btrfs_write_inode(struct inode *inode, int wait)
1468{
1469 struct btrfs_root *root = BTRFS_I(inode)->root;
1470 struct btrfs_trans_handle *trans;
1471 int ret = 0;
1472
1473 if (wait) {
1474 mutex_lock(&root->fs_info->fs_mutex);
1475 trans = btrfs_start_transaction(root, 1);
1476 btrfs_set_trans_block_group(trans, inode);
1477 ret = btrfs_commit_transaction(trans, root);
1478 mutex_unlock(&root->fs_info->fs_mutex);
1479 }
1480 return ret;
1481}
1482
1483/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001484 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001485 * inode changes. But, it is most likely to find the inode in cache.
1486 * FIXME, needs more benchmarking...there are no reasons other than performance
1487 * to keep or drop this code.
1488 */
1489void btrfs_dirty_inode(struct inode *inode)
1490{
1491 struct btrfs_root *root = BTRFS_I(inode)->root;
1492 struct btrfs_trans_handle *trans;
1493
1494 mutex_lock(&root->fs_info->fs_mutex);
1495 trans = btrfs_start_transaction(root, 1);
1496 btrfs_set_trans_block_group(trans, inode);
1497 btrfs_update_inode(trans, root, inode);
1498 btrfs_end_transaction(trans, root);
1499 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001500}
1501
1502static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1503 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001504 const char *name, int name_len,
1505 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001506 u64 objectid,
1507 struct btrfs_block_group_cache *group,
1508 int mode)
1509{
1510 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001511 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001512 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001513 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001514 struct btrfs_inode_ref *ref;
1515 struct btrfs_key key[2];
1516 u32 sizes[2];
1517 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001518 int ret;
1519 int owner;
1520
Chris Mason5f39d392007-10-15 16:14:19 -04001521 path = btrfs_alloc_path();
1522 BUG_ON(!path);
1523
Chris Mason39279cc2007-06-12 06:35:45 -04001524 inode = new_inode(root->fs_info->sb);
1525 if (!inode)
1526 return ERR_PTR(-ENOMEM);
1527
Chris Masond1310b22008-01-24 16:13:08 -05001528 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1529 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001530 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001531 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001532
Chris Mason39279cc2007-06-12 06:35:45 -04001533 if (mode & S_IFDIR)
1534 owner = 0;
1535 else
1536 owner = 1;
1537 group = btrfs_find_block_group(root, group, 0, 0, owner);
1538 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001539 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001540
1541 key[0].objectid = objectid;
1542 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1543 key[0].offset = 0;
1544
1545 key[1].objectid = objectid;
1546 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1547 key[1].offset = ref_objectid;
1548
1549 sizes[0] = sizeof(struct btrfs_inode_item);
1550 sizes[1] = name_len + sizeof(*ref);
1551
1552 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1553 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001554 goto fail;
1555
Chris Mason9c583092008-01-29 15:15:18 -05001556 if (objectid > root->highest_inode)
1557 root->highest_inode = objectid;
1558
Chris Mason39279cc2007-06-12 06:35:45 -04001559 inode->i_uid = current->fsuid;
1560 inode->i_gid = current->fsgid;
1561 inode->i_mode = mode;
1562 inode->i_ino = objectid;
1563 inode->i_blocks = 0;
1564 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001565 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1566 struct btrfs_inode_item);
1567 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001568
1569 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1570 struct btrfs_inode_ref);
1571 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1572 ptr = (unsigned long)(ref + 1);
1573 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1574
Chris Mason5f39d392007-10-15 16:14:19 -04001575 btrfs_mark_buffer_dirty(path->nodes[0]);
1576 btrfs_free_path(path);
1577
Chris Mason39279cc2007-06-12 06:35:45 -04001578 location = &BTRFS_I(inode)->location;
1579 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001580 location->offset = 0;
1581 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1582
Chris Mason39279cc2007-06-12 06:35:45 -04001583 insert_inode_hash(inode);
1584 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001585fail:
1586 btrfs_free_path(path);
1587 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001588}
1589
1590static inline u8 btrfs_inode_type(struct inode *inode)
1591{
1592 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1593}
1594
1595static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001596 struct dentry *dentry, struct inode *inode,
1597 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001598{
1599 int ret;
1600 struct btrfs_key key;
1601 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001602 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001603
Chris Mason39279cc2007-06-12 06:35:45 -04001604 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001605 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1606 key.offset = 0;
1607
1608 ret = btrfs_insert_dir_item(trans, root,
1609 dentry->d_name.name, dentry->d_name.len,
1610 dentry->d_parent->d_inode->i_ino,
1611 &key, btrfs_inode_type(inode));
1612 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001613 if (add_backref) {
1614 ret = btrfs_insert_inode_ref(trans, root,
1615 dentry->d_name.name,
1616 dentry->d_name.len,
1617 inode->i_ino,
1618 dentry->d_parent->d_inode->i_ino);
1619 }
Chris Mason79c44582007-06-25 10:09:33 -04001620 parent_inode = dentry->d_parent->d_inode;
1621 parent_inode->i_size += dentry->d_name.len * 2;
1622 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001623 ret = btrfs_update_inode(trans, root,
1624 dentry->d_parent->d_inode);
1625 }
1626 return ret;
1627}
1628
1629static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001630 struct dentry *dentry, struct inode *inode,
1631 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001632{
Chris Mason9c583092008-01-29 15:15:18 -05001633 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001634 if (!err) {
1635 d_instantiate(dentry, inode);
1636 return 0;
1637 }
1638 if (err > 0)
1639 err = -EEXIST;
1640 return err;
1641}
1642
Josef Bacik618e21d2007-07-11 10:18:17 -04001643static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1644 int mode, dev_t rdev)
1645{
1646 struct btrfs_trans_handle *trans;
1647 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001648 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001649 int err;
1650 int drop_inode = 0;
1651 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001652 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001653
1654 if (!new_valid_dev(rdev))
1655 return -EINVAL;
1656
1657 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001658 err = btrfs_check_free_space(root, 1, 0);
1659 if (err)
1660 goto fail;
1661
Josef Bacik618e21d2007-07-11 10:18:17 -04001662 trans = btrfs_start_transaction(root, 1);
1663 btrfs_set_trans_block_group(trans, dir);
1664
1665 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1666 if (err) {
1667 err = -ENOSPC;
1668 goto out_unlock;
1669 }
1670
Chris Mason9c583092008-01-29 15:15:18 -05001671 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1672 dentry->d_name.len,
1673 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001674 BTRFS_I(dir)->block_group, mode);
1675 err = PTR_ERR(inode);
1676 if (IS_ERR(inode))
1677 goto out_unlock;
1678
1679 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001680 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001681 if (err)
1682 drop_inode = 1;
1683 else {
1684 inode->i_op = &btrfs_special_inode_operations;
1685 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001686 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001687 }
1688 dir->i_sb->s_dirt = 1;
1689 btrfs_update_inode_block_group(trans, inode);
1690 btrfs_update_inode_block_group(trans, dir);
1691out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001692 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001693 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001694fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001695 mutex_unlock(&root->fs_info->fs_mutex);
1696
1697 if (drop_inode) {
1698 inode_dec_link_count(inode);
1699 iput(inode);
1700 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001701 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001702 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001703 return err;
1704}
1705
Chris Mason39279cc2007-06-12 06:35:45 -04001706static int btrfs_create(struct inode *dir, struct dentry *dentry,
1707 int mode, struct nameidata *nd)
1708{
1709 struct btrfs_trans_handle *trans;
1710 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001711 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001712 int err;
1713 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001714 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001715 u64 objectid;
1716
1717 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001718 err = btrfs_check_free_space(root, 1, 0);
1719 if (err)
1720 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001721 trans = btrfs_start_transaction(root, 1);
1722 btrfs_set_trans_block_group(trans, dir);
1723
1724 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1725 if (err) {
1726 err = -ENOSPC;
1727 goto out_unlock;
1728 }
1729
Chris Mason9c583092008-01-29 15:15:18 -05001730 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1731 dentry->d_name.len,
1732 dentry->d_parent->d_inode->i_ino,
1733 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001734 err = PTR_ERR(inode);
1735 if (IS_ERR(inode))
1736 goto out_unlock;
1737
1738 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001739 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001740 if (err)
1741 drop_inode = 1;
1742 else {
1743 inode->i_mapping->a_ops = &btrfs_aops;
1744 inode->i_fop = &btrfs_file_operations;
1745 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001746 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1747 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001748 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001749 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001750 }
1751 dir->i_sb->s_dirt = 1;
1752 btrfs_update_inode_block_group(trans, inode);
1753 btrfs_update_inode_block_group(trans, dir);
1754out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001755 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001756 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001757fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001758 mutex_unlock(&root->fs_info->fs_mutex);
1759
1760 if (drop_inode) {
1761 inode_dec_link_count(inode);
1762 iput(inode);
1763 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001764 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001765 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001766 return err;
1767}
1768
1769static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1770 struct dentry *dentry)
1771{
1772 struct btrfs_trans_handle *trans;
1773 struct btrfs_root *root = BTRFS_I(dir)->root;
1774 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001775 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001776 int err;
1777 int drop_inode = 0;
1778
1779 if (inode->i_nlink == 0)
1780 return -ENOENT;
1781
Chris Mason6da6aba2007-12-18 16:15:09 -05001782#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1783 inode->i_nlink++;
1784#else
Chris Mason39279cc2007-06-12 06:35:45 -04001785 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001786#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001787 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001788 err = btrfs_check_free_space(root, 1, 0);
1789 if (err)
1790 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001791 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001792
Chris Mason39279cc2007-06-12 06:35:45 -04001793 btrfs_set_trans_block_group(trans, dir);
1794 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001795 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001796
Chris Mason39279cc2007-06-12 06:35:45 -04001797 if (err)
1798 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001799
Chris Mason39279cc2007-06-12 06:35:45 -04001800 dir->i_sb->s_dirt = 1;
1801 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001802 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001803
Chris Mason54aa1f42007-06-22 14:16:25 -04001804 if (err)
1805 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001806
Chris Masond3c2fdc2007-09-17 10:58:06 -04001807 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001808 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001809fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001810 mutex_unlock(&root->fs_info->fs_mutex);
1811
1812 if (drop_inode) {
1813 inode_dec_link_count(inode);
1814 iput(inode);
1815 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001816 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001817 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001818 return err;
1819}
1820
Chris Mason39279cc2007-06-12 06:35:45 -04001821static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1822{
1823 struct inode *inode;
1824 struct btrfs_trans_handle *trans;
1825 struct btrfs_root *root = BTRFS_I(dir)->root;
1826 int err = 0;
1827 int drop_on_err = 0;
1828 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001829 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001830
1831 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001832 err = btrfs_check_free_space(root, 1, 0);
1833 if (err)
1834 goto out_unlock;
1835
Chris Mason39279cc2007-06-12 06:35:45 -04001836 trans = btrfs_start_transaction(root, 1);
1837 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001838
Chris Mason39279cc2007-06-12 06:35:45 -04001839 if (IS_ERR(trans)) {
1840 err = PTR_ERR(trans);
1841 goto out_unlock;
1842 }
1843
1844 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1845 if (err) {
1846 err = -ENOSPC;
1847 goto out_unlock;
1848 }
1849
Chris Mason9c583092008-01-29 15:15:18 -05001850 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1851 dentry->d_name.len,
1852 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001853 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1854 if (IS_ERR(inode)) {
1855 err = PTR_ERR(inode);
1856 goto out_fail;
1857 }
Chris Mason5f39d392007-10-15 16:14:19 -04001858
Chris Mason39279cc2007-06-12 06:35:45 -04001859 drop_on_err = 1;
1860 inode->i_op = &btrfs_dir_inode_operations;
1861 inode->i_fop = &btrfs_dir_file_operations;
1862 btrfs_set_trans_block_group(trans, inode);
1863
Chris Mason39544012007-12-12 14:38:19 -05001864 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001865 err = btrfs_update_inode(trans, root, inode);
1866 if (err)
1867 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001868
Chris Mason9c583092008-01-29 15:15:18 -05001869 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001870 if (err)
1871 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001872
Chris Mason39279cc2007-06-12 06:35:45 -04001873 d_instantiate(dentry, inode);
1874 drop_on_err = 0;
1875 dir->i_sb->s_dirt = 1;
1876 btrfs_update_inode_block_group(trans, inode);
1877 btrfs_update_inode_block_group(trans, dir);
1878
1879out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001880 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001881 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001882
Chris Mason39279cc2007-06-12 06:35:45 -04001883out_unlock:
1884 mutex_unlock(&root->fs_info->fs_mutex);
1885 if (drop_on_err)
1886 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001887 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001888 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001889 return err;
1890}
1891
Chris Masona52d9a82007-08-27 16:49:44 -04001892struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001893 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001894 int create)
1895{
1896 int ret;
1897 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001898 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001899 u64 extent_start = 0;
1900 u64 extent_end = 0;
1901 u64 objectid = inode->i_ino;
1902 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001903 struct btrfs_path *path;
1904 struct btrfs_root *root = BTRFS_I(inode)->root;
1905 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001906 struct extent_buffer *leaf;
1907 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001908 struct extent_map *em = NULL;
1909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001910 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001911 struct btrfs_trans_handle *trans = NULL;
1912
1913 path = btrfs_alloc_path();
1914 BUG_ON(!path);
1915 mutex_lock(&root->fs_info->fs_mutex);
1916
1917again:
Chris Masond1310b22008-01-24 16:13:08 -05001918 spin_lock(&em_tree->lock);
1919 em = lookup_extent_mapping(em_tree, start, len);
1920 spin_unlock(&em_tree->lock);
1921
Chris Masona52d9a82007-08-27 16:49:44 -04001922 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001923 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001924 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1925 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001926 WARN_ON(1);
1927 }
Chris Mason70dec802008-01-29 09:59:12 -05001928 if (em->block_start == EXTENT_MAP_INLINE && page)
1929 free_extent_map(em);
1930 else
1931 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001932 }
Chris Masond1310b22008-01-24 16:13:08 -05001933 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001934 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001935 err = -ENOMEM;
1936 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001937 }
Chris Masond1310b22008-01-24 16:13:08 -05001938
1939 em->start = EXTENT_MAP_HOLE;
1940 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001941 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001942 ret = btrfs_lookup_file_extent(trans, root, path,
1943 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001944 if (ret < 0) {
1945 err = ret;
1946 goto out;
1947 }
1948
1949 if (ret != 0) {
1950 if (path->slots[0] == 0)
1951 goto not_found;
1952 path->slots[0]--;
1953 }
1954
Chris Mason5f39d392007-10-15 16:14:19 -04001955 leaf = path->nodes[0];
1956 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001957 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001958 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001959 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1960 found_type = btrfs_key_type(&found_key);
1961 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001962 found_type != BTRFS_EXTENT_DATA_KEY) {
1963 goto not_found;
1964 }
1965
Chris Mason5f39d392007-10-15 16:14:19 -04001966 found_type = btrfs_file_extent_type(leaf, item);
1967 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001968 if (found_type == BTRFS_FILE_EXTENT_REG) {
1969 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001970 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001971 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001972 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001973 em->start = start;
1974 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001975 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001976 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001977 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001978 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001979 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001980 }
1981 goto not_found_em;
1982 }
Chris Masondb945352007-10-15 16:15:53 -04001983 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1984 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001985 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001986 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001987 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001988 goto insert;
1989 }
Chris Masondb945352007-10-15 16:15:53 -04001990 bytenr += btrfs_file_extent_offset(leaf, item);
1991 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001992 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001993 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001994 goto insert;
1995 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05001996 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001997 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001998 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001999 size_t size;
2000 size_t extent_offset;
2001 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002002
Chris Mason5f39d392007-10-15 16:14:19 -04002003 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2004 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002005 extent_end = (extent_start + size + root->sectorsize - 1) &
2006 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002007 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002008 em->start = start;
2009 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002010 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002011 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002012 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002013 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002014 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002015 }
2016 goto not_found_em;
2017 }
2018 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002019
2020 if (!page) {
2021 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002022 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002023 goto out;
2024 }
2025
Chris Mason70dec802008-01-29 09:59:12 -05002026 page_start = page_offset(page) + pg_offset;
2027 extent_offset = page_start - extent_start;
2028 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002029 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002030 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002031 em->len = (copy_size + root->sectorsize - 1) &
2032 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002033 map = kmap(page);
2034 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002035 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002036 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002037 copy_size);
2038 flush_dcache_page(page);
2039 } else if (create && PageUptodate(page)) {
2040 if (!trans) {
2041 kunmap(page);
2042 free_extent_map(em);
2043 em = NULL;
2044 btrfs_release_path(root, path);
2045 trans = btrfs_start_transaction(root, 1);
2046 goto again;
2047 }
Chris Mason70dec802008-01-29 09:59:12 -05002048 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002049 copy_size);
2050 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002051 }
Chris Masona52d9a82007-08-27 16:49:44 -04002052 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002053 set_extent_uptodate(io_tree, em->start,
2054 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002055 goto insert;
2056 } else {
2057 printk("unkknown found_type %d\n", found_type);
2058 WARN_ON(1);
2059 }
2060not_found:
2061 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002062 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002063not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002064 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002065insert:
2066 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002067 if (em->start > start || extent_map_end(em) <= start) {
2068 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002069 err = -EIO;
2070 goto out;
2071 }
Chris Masond1310b22008-01-24 16:13:08 -05002072
2073 err = 0;
2074 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002075 ret = add_extent_mapping(em_tree, em);
2076 if (ret == -EEXIST) {
2077 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002078 em = lookup_extent_mapping(em_tree, start, len);
2079 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002080 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002081 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002082 }
Chris Masona52d9a82007-08-27 16:49:44 -04002083 }
Chris Masond1310b22008-01-24 16:13:08 -05002084 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002085out:
2086 btrfs_free_path(path);
2087 if (trans) {
2088 ret = btrfs_end_transaction(trans, root);
2089 if (!err)
2090 err = ret;
2091 }
2092 mutex_unlock(&root->fs_info->fs_mutex);
2093 if (err) {
2094 free_extent_map(em);
2095 WARN_ON(1);
2096 return ERR_PTR(err);
2097 }
2098 return em;
2099}
2100
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002101static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002102{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002103 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002104}
2105
Chris Mason9ebefb182007-06-15 13:50:00 -04002106int btrfs_readpage(struct file *file, struct page *page)
2107{
Chris Masond1310b22008-01-24 16:13:08 -05002108 struct extent_io_tree *tree;
2109 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002110 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002111}
Chris Mason1832a6d2007-12-21 16:27:21 -05002112
Chris Mason39279cc2007-06-12 06:35:45 -04002113static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2114{
Chris Masond1310b22008-01-24 16:13:08 -05002115 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002116
2117
2118 if (current->flags & PF_MEMALLOC) {
2119 redirty_page_for_writepage(wbc, page);
2120 unlock_page(page);
2121 return 0;
2122 }
Chris Masond1310b22008-01-24 16:13:08 -05002123 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002124 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2125}
Chris Mason39279cc2007-06-12 06:35:45 -04002126
Chris Masonb293f022007-11-01 19:45:34 -04002127static int btrfs_writepages(struct address_space *mapping,
2128 struct writeback_control *wbc)
2129{
Chris Masond1310b22008-01-24 16:13:08 -05002130 struct extent_io_tree *tree;
2131 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002132 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2133}
2134
Chris Mason3ab2fb52007-11-08 10:59:22 -05002135static int
2136btrfs_readpages(struct file *file, struct address_space *mapping,
2137 struct list_head *pages, unsigned nr_pages)
2138{
Chris Masond1310b22008-01-24 16:13:08 -05002139 struct extent_io_tree *tree;
2140 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002141 return extent_readpages(tree, mapping, pages, nr_pages,
2142 btrfs_get_extent);
2143}
2144
Chris Mason70dec802008-01-29 09:59:12 -05002145static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002146{
Chris Masond1310b22008-01-24 16:13:08 -05002147 struct extent_io_tree *tree;
2148 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002149 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002150
Chris Masond1310b22008-01-24 16:13:08 -05002151 tree = &BTRFS_I(page->mapping->host)->io_tree;
2152 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002153 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002154 if (ret == 1) {
2155 ClearPagePrivate(page);
2156 set_page_private(page, 0);
2157 page_cache_release(page);
2158 }
2159 return ret;
2160}
Chris Mason39279cc2007-06-12 06:35:45 -04002161
Chris Masona52d9a82007-08-27 16:49:44 -04002162static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2163{
Chris Masond1310b22008-01-24 16:13:08 -05002164 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002165
Chris Masond1310b22008-01-24 16:13:08 -05002166 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002167 extent_invalidatepage(tree, page, offset);
2168 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002169}
2170
Chris Mason9ebefb182007-06-15 13:50:00 -04002171/*
2172 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2173 * called from a page fault handler when a page is first dirtied. Hence we must
2174 * be careful to check for EOF conditions here. We set the page up correctly
2175 * for a written page which means we get ENOSPC checking when writing into
2176 * holes and correct delalloc and unwritten extent mapping on filesystems that
2177 * support these features.
2178 *
2179 * We are not allowed to take the i_mutex here so we have to play games to
2180 * protect against truncate races as the page could now be beyond EOF. Because
2181 * vmtruncate() writes the inode size before removing pages, once we have the
2182 * page lock we can determine safely if the page is beyond EOF. If it is not
2183 * beyond EOF, then the page is guaranteed safe against truncation until we
2184 * unlock the page.
2185 */
2186int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2187{
Chris Mason6da6aba2007-12-18 16:15:09 -05002188 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002189 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002190 unsigned long end;
2191 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002192 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002193 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002194
Chris Mason1832a6d2007-12-21 16:27:21 -05002195 mutex_lock(&root->fs_info->fs_mutex);
2196 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002197 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002198 if (ret)
2199 goto out;
2200
2201 ret = -EINVAL;
2202
Chris Mason9ebefb182007-06-15 13:50:00 -04002203 lock_page(page);
2204 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002205 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002206 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002207
Chris Mason9ebefb182007-06-15 13:50:00 -04002208 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002209 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002210 /* page got truncated out from underneath us */
2211 goto out_unlock;
2212 }
2213
2214 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002215 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002216 end = size & ~PAGE_CACHE_MASK;
2217 else
2218 end = PAGE_CACHE_SIZE;
2219
Chris Masonb888db22007-08-27 16:49:44 -04002220 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002221
2222out_unlock:
2223 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002224out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002225 return ret;
2226}
2227
Chris Mason39279cc2007-06-12 06:35:45 -04002228static void btrfs_truncate(struct inode *inode)
2229{
2230 struct btrfs_root *root = BTRFS_I(inode)->root;
2231 int ret;
2232 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002233 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002234
2235 if (!S_ISREG(inode->i_mode))
2236 return;
2237 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2238 return;
2239
2240 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2241
2242 mutex_lock(&root->fs_info->fs_mutex);
2243 trans = btrfs_start_transaction(root, 1);
2244 btrfs_set_trans_block_group(trans, inode);
2245
2246 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002247 ret = btrfs_truncate_in_trans(trans, root, inode,
2248 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002249 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002250 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002251
Chris Mason39279cc2007-06-12 06:35:45 -04002252 ret = btrfs_end_transaction(trans, root);
2253 BUG_ON(ret);
2254 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002255 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002256 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002257}
2258
Chris Mason4313b392008-01-03 09:08:48 -05002259static int noinline create_subvol(struct btrfs_root *root, char *name,
2260 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002261{
2262 struct btrfs_trans_handle *trans;
2263 struct btrfs_key key;
2264 struct btrfs_root_item root_item;
2265 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002266 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002267 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002268 struct inode *inode;
2269 struct inode *dir;
2270 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002271 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002272 u64 objectid;
2273 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002274 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002275
2276 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002277 ret = btrfs_check_free_space(root, 1, 0);
2278 if (ret)
2279 goto fail_commit;
2280
Chris Mason39279cc2007-06-12 06:35:45 -04002281 trans = btrfs_start_transaction(root, 1);
2282 BUG_ON(!trans);
2283
Chris Mason7bb86312007-12-11 09:25:06 -05002284 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2285 0, &objectid);
2286 if (ret)
2287 goto fail;
2288
2289 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2290 objectid, trans->transid, 0, 0,
2291 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002292 if (IS_ERR(leaf))
2293 return PTR_ERR(leaf);
2294
2295 btrfs_set_header_nritems(leaf, 0);
2296 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002297 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002298 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002299 btrfs_set_header_owner(leaf, objectid);
2300
Chris Mason5f39d392007-10-15 16:14:19 -04002301 write_extent_buffer(leaf, root->fs_info->fsid,
2302 (unsigned long)btrfs_header_fsid(leaf),
2303 BTRFS_FSID_SIZE);
2304 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002305
2306 inode_item = &root_item.inode;
2307 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002308 inode_item->generation = cpu_to_le64(1);
2309 inode_item->size = cpu_to_le64(3);
2310 inode_item->nlink = cpu_to_le32(1);
2311 inode_item->nblocks = cpu_to_le64(1);
2312 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002313
Chris Masondb945352007-10-15 16:15:53 -04002314 btrfs_set_root_bytenr(&root_item, leaf->start);
2315 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002316 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002317 btrfs_set_root_used(&root_item, 0);
2318
Chris Mason5eda7b52007-06-22 14:16:25 -04002319 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2320 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002321
2322 free_extent_buffer(leaf);
2323 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002324
Chris Mason39279cc2007-06-12 06:35:45 -04002325 btrfs_set_root_dirid(&root_item, new_dirid);
2326
2327 key.objectid = objectid;
2328 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002329 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2330 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2331 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002332 if (ret)
2333 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002334
2335 /*
2336 * insert the directory item
2337 */
2338 key.offset = (u64)-1;
2339 dir = root->fs_info->sb->s_root->d_inode;
2340 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2341 name, namelen, dir->i_ino, &key,
2342 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002343 if (ret)
2344 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002345
Chris Mason39544012007-12-12 14:38:19 -05002346 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2347 name, namelen, objectid,
2348 root->fs_info->sb->s_root->d_inode->i_ino);
2349 if (ret)
2350 goto fail;
2351
Chris Mason39279cc2007-06-12 06:35:45 -04002352 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002353 if (ret)
2354 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002355
Josef Bacik58176a92007-08-29 15:47:34 -04002356 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002357 BUG_ON(!new_root);
2358
2359 trans = btrfs_start_transaction(new_root, 1);
2360 BUG_ON(!trans);
2361
Chris Mason9c583092008-01-29 15:15:18 -05002362 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2363 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002364 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002365 if (IS_ERR(inode))
2366 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002367 inode->i_op = &btrfs_dir_inode_operations;
2368 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002369 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002370
Chris Mason39544012007-12-12 14:38:19 -05002371 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2372 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002373 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002374 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002375 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002376 if (ret)
2377 goto fail;
2378fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002379 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002380 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002381 if (err && !ret)
2382 ret = err;
2383fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002384 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002385 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002386 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002387 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002388}
2389
2390static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2391{
Chris Mason3063d292008-01-08 15:46:30 -05002392 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002393 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002394 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002395 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002396 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002397
2398 if (!root->ref_cows)
2399 return -EINVAL;
2400
2401 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002402 ret = btrfs_check_free_space(root, 1, 0);
2403 if (ret)
2404 goto fail_unlock;
2405
Chris Mason3063d292008-01-08 15:46:30 -05002406 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2407 if (!pending_snapshot) {
2408 ret = -ENOMEM;
2409 goto fail_unlock;
2410 }
Yanfb4bc1e2008-01-17 11:59:51 -05002411 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002412 if (!pending_snapshot->name) {
2413 ret = -ENOMEM;
2414 kfree(pending_snapshot);
2415 goto fail_unlock;
2416 }
Yanfb4bc1e2008-01-17 11:59:51 -05002417 memcpy(pending_snapshot->name, name, namelen);
2418 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002419 trans = btrfs_start_transaction(root, 1);
2420 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002421 pending_snapshot->root = root;
2422 list_add(&pending_snapshot->list,
2423 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002424 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002425 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002426
Chris Mason1832a6d2007-12-21 16:27:21 -05002427fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002428 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002429 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002430 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002431 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002432}
2433
Chris Masonedbd8d42007-12-21 16:27:24 -05002434unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002435 struct file_ra_state *ra, struct file *file,
2436 pgoff_t offset, pgoff_t last_index)
2437{
2438 pgoff_t req_size;
2439
2440#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2441 req_size = last_index - offset + 1;
2442 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2443 return offset;
2444#else
2445 req_size = min(last_index - offset + 1, (pgoff_t)128);
2446 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2447 return offset + req_size;
2448#endif
2449}
2450
2451int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002452 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002453 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002454 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002455 struct page *page;
2456 unsigned long last_index;
2457 unsigned long ra_index = 0;
2458 u64 page_start;
2459 u64 page_end;
2460 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002461 int ret;
2462
2463 mutex_lock(&root->fs_info->fs_mutex);
2464 ret = btrfs_check_free_space(root, inode->i_size, 0);
2465 mutex_unlock(&root->fs_info->fs_mutex);
2466 if (ret)
2467 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002468
2469 mutex_lock(&inode->i_mutex);
2470 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2471 for (i = 0; i <= last_index; i++) {
2472 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002473 ra_index = btrfs_force_ra(inode->i_mapping,
2474 &file->f_ra,
2475 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002476 }
2477 page = grab_cache_page(inode->i_mapping, i);
2478 if (!page)
2479 goto out_unlock;
2480 if (!PageUptodate(page)) {
2481 btrfs_readpage(NULL, page);
2482 lock_page(page);
2483 if (!PageUptodate(page)) {
2484 unlock_page(page);
2485 page_cache_release(page);
2486 goto out_unlock;
2487 }
2488 }
Chris Mason35ebb932007-10-30 16:56:53 -04002489 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002490 page_end = page_start + PAGE_CACHE_SIZE - 1;
2491
Chris Masond1310b22008-01-24 16:13:08 -05002492 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002493 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002494 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002495
Chris Masond1310b22008-01-24 16:13:08 -05002496 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002497 set_page_dirty(page);
2498 unlock_page(page);
2499 page_cache_release(page);
2500 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2501 }
2502
2503out_unlock:
2504 mutex_unlock(&inode->i_mutex);
2505 return 0;
2506}
2507
Chris Masonedbd8d42007-12-21 16:27:24 -05002508static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2509{
2510 u64 new_size;
2511 u64 old_size;
2512 struct btrfs_ioctl_vol_args *vol_args;
2513 struct btrfs_trans_handle *trans;
2514 char *sizestr;
2515 int ret = 0;
2516 int namelen;
2517 int mod = 0;
2518
2519 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2520
2521 if (!vol_args)
2522 return -ENOMEM;
2523
2524 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2525 ret = -EFAULT;
2526 goto out;
2527 }
2528 namelen = strlen(vol_args->name);
2529 if (namelen > BTRFS_VOL_NAME_MAX) {
2530 ret = -EINVAL;
2531 goto out;
2532 }
2533
2534 sizestr = vol_args->name;
2535 if (!strcmp(sizestr, "max"))
2536 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2537 else {
2538 if (sizestr[0] == '-') {
2539 mod = -1;
2540 sizestr++;
2541 } else if (sizestr[0] == '+') {
2542 mod = 1;
2543 sizestr++;
2544 }
2545 new_size = btrfs_parse_size(sizestr);
2546 if (new_size == 0) {
2547 ret = -EINVAL;
2548 goto out;
2549 }
2550 }
2551
2552 mutex_lock(&root->fs_info->fs_mutex);
2553 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2554
2555 if (mod < 0) {
2556 if (new_size > old_size) {
2557 ret = -EINVAL;
2558 goto out_unlock;
2559 }
2560 new_size = old_size - new_size;
2561 } else if (mod > 0) {
2562 new_size = old_size + new_size;
2563 }
2564
2565 if (new_size < 256 * 1024 * 1024) {
2566 ret = -EINVAL;
2567 goto out_unlock;
2568 }
2569 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2570 ret = -EFBIG;
2571 goto out_unlock;
2572 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002573
2574 do_div(new_size, root->sectorsize);
2575 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002576
2577printk("new size is %Lu\n", new_size);
2578 if (new_size > old_size) {
2579 trans = btrfs_start_transaction(root, 1);
2580 ret = btrfs_grow_extent_tree(trans, root, new_size);
2581 btrfs_commit_transaction(trans, root);
2582 } else {
2583 ret = btrfs_shrink_extent_tree(root, new_size);
2584 }
2585
2586out_unlock:
2587 mutex_unlock(&root->fs_info->fs_mutex);
2588out:
2589 kfree(vol_args);
2590 return ret;
2591}
2592
Chris Mason4313b392008-01-03 09:08:48 -05002593static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2594 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002595{
Chris Mason4aec2b52007-12-18 16:25:45 -05002596 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002597 struct btrfs_dir_item *di;
2598 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002599 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002600 int namelen;
2601 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002602
Chris Mason4aec2b52007-12-18 16:25:45 -05002603 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002604
Chris Mason4aec2b52007-12-18 16:25:45 -05002605 if (!vol_args)
2606 return -ENOMEM;
2607
2608 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2609 ret = -EFAULT;
2610 goto out;
2611 }
2612
2613 namelen = strlen(vol_args->name);
2614 if (namelen > BTRFS_VOL_NAME_MAX) {
2615 ret = -EINVAL;
2616 goto out;
2617 }
2618 if (strchr(vol_args->name, '/')) {
2619 ret = -EINVAL;
2620 goto out;
2621 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002622
2623 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002624 if (!path) {
2625 ret = -ENOMEM;
2626 goto out;
2627 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002628
2629 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2630 mutex_lock(&root->fs_info->fs_mutex);
2631 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2632 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002633 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002634 mutex_unlock(&root->fs_info->fs_mutex);
2635 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002636
2637 if (di && !IS_ERR(di)) {
2638 ret = -EEXIST;
2639 goto out;
2640 }
2641
2642 if (IS_ERR(di)) {
2643 ret = PTR_ERR(di);
2644 goto out;
2645 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002646
2647 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002648 ret = create_subvol(root, vol_args->name, namelen);
2649 else
2650 ret = create_snapshot(root, vol_args->name, namelen);
2651out:
2652 kfree(vol_args);
2653 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002654}
2655
2656static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002657{
Chris Mason6da6aba2007-12-18 16:15:09 -05002658 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002659 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002660
2661 switch (inode->i_mode & S_IFMT) {
2662 case S_IFDIR:
2663 mutex_lock(&root->fs_info->fs_mutex);
2664 btrfs_defrag_root(root, 0);
2665 btrfs_defrag_root(root->fs_info->extent_root, 0);
2666 mutex_unlock(&root->fs_info->fs_mutex);
2667 break;
2668 case S_IFREG:
2669 btrfs_defrag_file(file);
2670 break;
2671 }
2672
2673 return 0;
2674}
2675
2676long btrfs_ioctl(struct file *file, unsigned int
2677 cmd, unsigned long arg)
2678{
Chris Mason6da6aba2007-12-18 16:15:09 -05002679 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002680
2681 switch (cmd) {
2682 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002683 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002684 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002685 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002686 case BTRFS_IOC_RESIZE:
2687 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002688 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002689
2690 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002691}
2692
Chris Mason39279cc2007-06-12 06:35:45 -04002693/*
2694 * Called inside transaction, so use GFP_NOFS
2695 */
2696struct inode *btrfs_alloc_inode(struct super_block *sb)
2697{
2698 struct btrfs_inode *ei;
2699
2700 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2701 if (!ei)
2702 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002703 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002704 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002705 return &ei->vfs_inode;
2706}
2707
2708void btrfs_destroy_inode(struct inode *inode)
2709{
2710 WARN_ON(!list_empty(&inode->i_dentry));
2711 WARN_ON(inode->i_data.nrpages);
2712
Chris Mason8c416c92008-01-14 15:10:26 -05002713 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002714 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2715}
2716
Chris Mason44ec0b72007-10-29 10:55:05 -04002717#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2718static void init_once(struct kmem_cache * cachep, void *foo)
2719#else
Chris Mason39279cc2007-06-12 06:35:45 -04002720static void init_once(void * foo, struct kmem_cache * cachep,
2721 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002722#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002723{
2724 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2725
2726 inode_init_once(&ei->vfs_inode);
2727}
2728
2729void btrfs_destroy_cachep(void)
2730{
2731 if (btrfs_inode_cachep)
2732 kmem_cache_destroy(btrfs_inode_cachep);
2733 if (btrfs_trans_handle_cachep)
2734 kmem_cache_destroy(btrfs_trans_handle_cachep);
2735 if (btrfs_transaction_cachep)
2736 kmem_cache_destroy(btrfs_transaction_cachep);
2737 if (btrfs_bit_radix_cachep)
2738 kmem_cache_destroy(btrfs_bit_radix_cachep);
2739 if (btrfs_path_cachep)
2740 kmem_cache_destroy(btrfs_path_cachep);
2741}
2742
Chris Mason86479a02007-09-10 19:58:16 -04002743struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002744 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002745#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2746 void (*ctor)(struct kmem_cache *, void *)
2747#else
Chris Mason92fee662007-07-25 12:31:35 -04002748 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002749 unsigned long)
2750#endif
2751 )
Chris Mason92fee662007-07-25 12:31:35 -04002752{
2753 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2754 SLAB_MEM_SPREAD | extra_flags), ctor
2755#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2756 ,NULL
2757#endif
2758 );
2759}
2760
Chris Mason39279cc2007-06-12 06:35:45 -04002761int btrfs_init_cachep(void)
2762{
Chris Mason86479a02007-09-10 19:58:16 -04002763 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002764 sizeof(struct btrfs_inode),
2765 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002766 if (!btrfs_inode_cachep)
2767 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002768 btrfs_trans_handle_cachep =
2769 btrfs_cache_create("btrfs_trans_handle_cache",
2770 sizeof(struct btrfs_trans_handle),
2771 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002772 if (!btrfs_trans_handle_cachep)
2773 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002774 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002775 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002776 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002777 if (!btrfs_transaction_cachep)
2778 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002779 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002780 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002781 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002782 if (!btrfs_path_cachep)
2783 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002784 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002785 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002786 if (!btrfs_bit_radix_cachep)
2787 goto fail;
2788 return 0;
2789fail:
2790 btrfs_destroy_cachep();
2791 return -ENOMEM;
2792}
2793
2794static int btrfs_getattr(struct vfsmount *mnt,
2795 struct dentry *dentry, struct kstat *stat)
2796{
2797 struct inode *inode = dentry->d_inode;
2798 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002799 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002800 return 0;
2801}
2802
2803static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2804 struct inode * new_dir,struct dentry *new_dentry)
2805{
2806 struct btrfs_trans_handle *trans;
2807 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2808 struct inode *new_inode = new_dentry->d_inode;
2809 struct inode *old_inode = old_dentry->d_inode;
2810 struct timespec ctime = CURRENT_TIME;
2811 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002812 int ret;
2813
2814 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2815 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2816 return -ENOTEMPTY;
2817 }
Chris Mason5f39d392007-10-15 16:14:19 -04002818
Chris Mason39279cc2007-06-12 06:35:45 -04002819 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002820 ret = btrfs_check_free_space(root, 1, 0);
2821 if (ret)
2822 goto out_unlock;
2823
Chris Mason39279cc2007-06-12 06:35:45 -04002824 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002825
Chris Mason39279cc2007-06-12 06:35:45 -04002826 btrfs_set_trans_block_group(trans, new_dir);
2827 path = btrfs_alloc_path();
2828 if (!path) {
2829 ret = -ENOMEM;
2830 goto out_fail;
2831 }
2832
2833 old_dentry->d_inode->i_nlink++;
2834 old_dir->i_ctime = old_dir->i_mtime = ctime;
2835 new_dir->i_ctime = new_dir->i_mtime = ctime;
2836 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002837
Chris Mason39279cc2007-06-12 06:35:45 -04002838 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2839 if (ret)
2840 goto out_fail;
2841
2842 if (new_inode) {
2843 new_inode->i_ctime = CURRENT_TIME;
2844 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2845 if (ret)
2846 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002847 }
Chris Mason9c583092008-01-29 15:15:18 -05002848 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002849 if (ret)
2850 goto out_fail;
2851
2852out_fail:
2853 btrfs_free_path(path);
2854 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002855out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002856 mutex_unlock(&root->fs_info->fs_mutex);
2857 return ret;
2858}
2859
2860static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2861 const char *symname)
2862{
2863 struct btrfs_trans_handle *trans;
2864 struct btrfs_root *root = BTRFS_I(dir)->root;
2865 struct btrfs_path *path;
2866 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002867 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002868 int err;
2869 int drop_inode = 0;
2870 u64 objectid;
2871 int name_len;
2872 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002873 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002874 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002875 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002876 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002877
2878 name_len = strlen(symname) + 1;
2879 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2880 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002881
Chris Mason39279cc2007-06-12 06:35:45 -04002882 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002883 err = btrfs_check_free_space(root, 1, 0);
2884 if (err)
2885 goto out_fail;
2886
Chris Mason39279cc2007-06-12 06:35:45 -04002887 trans = btrfs_start_transaction(root, 1);
2888 btrfs_set_trans_block_group(trans, dir);
2889
2890 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2891 if (err) {
2892 err = -ENOSPC;
2893 goto out_unlock;
2894 }
2895
Chris Mason9c583092008-01-29 15:15:18 -05002896 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2897 dentry->d_name.len,
2898 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002899 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2900 err = PTR_ERR(inode);
2901 if (IS_ERR(inode))
2902 goto out_unlock;
2903
2904 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002905 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002906 if (err)
2907 drop_inode = 1;
2908 else {
2909 inode->i_mapping->a_ops = &btrfs_aops;
2910 inode->i_fop = &btrfs_file_operations;
2911 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002912 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2913 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002914 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002915 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002916 }
2917 dir->i_sb->s_dirt = 1;
2918 btrfs_update_inode_block_group(trans, inode);
2919 btrfs_update_inode_block_group(trans, dir);
2920 if (drop_inode)
2921 goto out_unlock;
2922
2923 path = btrfs_alloc_path();
2924 BUG_ON(!path);
2925 key.objectid = inode->i_ino;
2926 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002927 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2928 datasize = btrfs_file_extent_calc_inline_size(name_len);
2929 err = btrfs_insert_empty_item(trans, root, path, &key,
2930 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002931 if (err) {
2932 drop_inode = 1;
2933 goto out_unlock;
2934 }
Chris Mason5f39d392007-10-15 16:14:19 -04002935 leaf = path->nodes[0];
2936 ei = btrfs_item_ptr(leaf, path->slots[0],
2937 struct btrfs_file_extent_item);
2938 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2939 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002940 BTRFS_FILE_EXTENT_INLINE);
2941 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002942 write_extent_buffer(leaf, symname, ptr, name_len);
2943 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002944 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002945
Chris Mason39279cc2007-06-12 06:35:45 -04002946 inode->i_op = &btrfs_symlink_inode_operations;
2947 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2948 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002949 err = btrfs_update_inode(trans, root, inode);
2950 if (err)
2951 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002952
2953out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002954 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002955 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002956out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002957 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002958 if (drop_inode) {
2959 inode_dec_link_count(inode);
2960 iput(inode);
2961 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002962 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002963 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002964 return err;
2965}
Yanfdebe2b2008-01-14 13:26:08 -05002966static int btrfs_permission(struct inode *inode, int mask,
2967 struct nameidata *nd)
2968{
2969 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2970 return -EACCES;
2971 return generic_permission(inode, mask, NULL);
2972}
Chris Mason39279cc2007-06-12 06:35:45 -04002973
2974static struct inode_operations btrfs_dir_inode_operations = {
2975 .lookup = btrfs_lookup,
2976 .create = btrfs_create,
2977 .unlink = btrfs_unlink,
2978 .link = btrfs_link,
2979 .mkdir = btrfs_mkdir,
2980 .rmdir = btrfs_rmdir,
2981 .rename = btrfs_rename,
2982 .symlink = btrfs_symlink,
2983 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002984 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002985 .setxattr = generic_setxattr,
2986 .getxattr = generic_getxattr,
2987 .listxattr = btrfs_listxattr,
2988 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002989 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002990};
Chris Mason39279cc2007-06-12 06:35:45 -04002991static struct inode_operations btrfs_dir_ro_inode_operations = {
2992 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002993 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002994};
Chris Mason39279cc2007-06-12 06:35:45 -04002995static struct file_operations btrfs_dir_file_operations = {
2996 .llseek = generic_file_llseek,
2997 .read = generic_read_dir,
2998 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002999 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003000#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003001 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003002#endif
3003};
3004
Chris Masond1310b22008-01-24 16:13:08 -05003005static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003006 .fill_delalloc = run_delalloc_range,
3007 .writepage_io_hook = btrfs_writepage_io_hook,
3008 .readpage_io_hook = btrfs_readpage_io_hook,
3009 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003010 .set_bit_hook = btrfs_set_bit_hook,
3011 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003012};
3013
Chris Mason39279cc2007-06-12 06:35:45 -04003014static struct address_space_operations btrfs_aops = {
3015 .readpage = btrfs_readpage,
3016 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003017 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003018 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003019 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003020 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003021 .invalidatepage = btrfs_invalidatepage,
3022 .releasepage = btrfs_releasepage,
3023 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003024};
3025
3026static struct address_space_operations btrfs_symlink_aops = {
3027 .readpage = btrfs_readpage,
3028 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003029 .invalidatepage = btrfs_invalidatepage,
3030 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003031};
3032
3033static struct inode_operations btrfs_file_inode_operations = {
3034 .truncate = btrfs_truncate,
3035 .getattr = btrfs_getattr,
3036 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003037 .setxattr = generic_setxattr,
3038 .getxattr = generic_getxattr,
3039 .listxattr = btrfs_listxattr,
3040 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003041 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003042};
Josef Bacik618e21d2007-07-11 10:18:17 -04003043static struct inode_operations btrfs_special_inode_operations = {
3044 .getattr = btrfs_getattr,
3045 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003046 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003047};
Chris Mason39279cc2007-06-12 06:35:45 -04003048static struct inode_operations btrfs_symlink_inode_operations = {
3049 .readlink = generic_readlink,
3050 .follow_link = page_follow_link_light,
3051 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003052 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003053};