blob: bcf3b35fb65e0e42eb330f52c8f3b9868826428c [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,
263 unsigned long bits)
264{
265 if ((bits & EXTENT_DELALLOC)) {
266 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,
275 unsigned long bits)
276{
277 if ((bits & EXTENT_DELALLOC)) {
278 struct btrfs_root *root = BTRFS_I(inode)->root;
279 spin_lock(&root->fs_info->delalloc_lock);
280 root->fs_info->delalloc_bytes -= end - start + 1;
281 spin_unlock(&root->fs_info->delalloc_lock);
282 }
283 return 0;
284}
285
Chris Mason07157aa2007-08-30 08:50:51 -0400286int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
287{
288 struct inode *inode = page->mapping->host;
289 struct btrfs_root *root = BTRFS_I(inode)->root;
290 struct btrfs_trans_handle *trans;
291 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500292 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400293 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400294 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500295 if (btrfs_test_opt(root, NODATASUM) ||
296 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500297 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400298 mutex_lock(&root->fs_info->fs_mutex);
299 trans = btrfs_start_transaction(root, 1);
300 btrfs_set_trans_block_group(trans, inode);
301 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400302 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400303 start, kaddr + offset, end - start + 1);
304 kunmap(page);
305 ret = btrfs_end_transaction(trans, root);
306 BUG_ON(ret);
307 mutex_unlock(&root->fs_info->fs_mutex);
308 return ret;
309}
310
311int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
312{
313 int ret = 0;
314 struct inode *inode = page->mapping->host;
315 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500316 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400317 struct btrfs_csum_item *item;
318 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400319 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500320 if (btrfs_test_opt(root, NODATASUM) ||
321 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500322 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400323 mutex_lock(&root->fs_info->fs_mutex);
324 path = btrfs_alloc_path();
325 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
326 if (IS_ERR(item)) {
327 ret = PTR_ERR(item);
328 /* a csum that isn't present is a preallocated region. */
329 if (ret == -ENOENT || ret == -EFBIG)
330 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400331 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500332 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400333 goto out;
334 }
Chris Masonff79f812007-10-15 16:22:25 -0400335 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
336 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500337 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400338out:
339 if (path)
340 btrfs_free_path(path);
341 mutex_unlock(&root->fs_info->fs_mutex);
342 return ret;
343}
344
Chris Mason70dec802008-01-29 09:59:12 -0500345int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
346 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400347{
Chris Mason35ebb932007-10-30 16:56:53 -0400348 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400349 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500350 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400351 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500352 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400353 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400354 struct btrfs_root *root = BTRFS_I(inode)->root;
355 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400356 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500357
Yanb98b6762008-01-08 15:54:37 -0500358 if (btrfs_test_opt(root, NODATASUM) ||
359 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500360 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500361
Chris Mason70dec802008-01-29 09:59:12 -0500362 if (state->start == start) {
363 private = state->private;
364 ret = 0;
365 } else {
366 ret = get_state_private(io_tree, start, &private);
367 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400368 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400369 kaddr = kmap_atomic(page, KM_IRQ0);
370 if (ret) {
371 goto zeroit;
372 }
Chris Masonff79f812007-10-15 16:22:25 -0400373 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
374 btrfs_csum_final(csum, (char *)&csum);
375 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400376 goto zeroit;
377 }
378 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400379 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400380 return 0;
381
382zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500383 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
384 page->mapping->host->i_ino, (unsigned long long)start, csum,
385 private);
Chris Masondb945352007-10-15 16:15:53 -0400386 memset(kaddr + offset, 1, end - start + 1);
387 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400388 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400389 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400390 return 0;
391}
Chris Masonb888db22007-08-27 16:49:44 -0400392
Chris Mason39279cc2007-06-12 06:35:45 -0400393void btrfs_read_locked_inode(struct inode *inode)
394{
395 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400396 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400397 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400398 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400399 struct btrfs_root *root = BTRFS_I(inode)->root;
400 struct btrfs_key location;
401 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400402 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400403 int ret;
404
405 path = btrfs_alloc_path();
406 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400407 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400408 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500409
Chris Mason39279cc2007-06-12 06:35:45 -0400410 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400411 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400412 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400413
Chris Mason5f39d392007-10-15 16:14:19 -0400414 leaf = path->nodes[0];
415 inode_item = btrfs_item_ptr(leaf, path->slots[0],
416 struct btrfs_inode_item);
417
418 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
419 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
420 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
421 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
422 inode->i_size = btrfs_inode_size(leaf, inode_item);
423
424 tspec = btrfs_inode_atime(inode_item);
425 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
426 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
427
428 tspec = btrfs_inode_mtime(inode_item);
429 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
430 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
431
432 tspec = btrfs_inode_ctime(inode_item);
433 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
434 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
435
436 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
437 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400438 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400439 rdev = btrfs_inode_rdev(leaf, inode_item);
440
441 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400442 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
443 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500444 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500445 if (!BTRFS_I(inode)->block_group) {
446 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
447 NULL, 0, 0, 0);
448 }
Chris Mason39279cc2007-06-12 06:35:45 -0400449 btrfs_free_path(path);
450 inode_item = NULL;
451
452 mutex_unlock(&root->fs_info->fs_mutex);
453
454 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400455 case S_IFREG:
456 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500457 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400458 inode->i_fop = &btrfs_file_operations;
459 inode->i_op = &btrfs_file_inode_operations;
460 break;
461 case S_IFDIR:
462 inode->i_fop = &btrfs_dir_file_operations;
463 if (root == root->fs_info->tree_root)
464 inode->i_op = &btrfs_dir_ro_inode_operations;
465 else
466 inode->i_op = &btrfs_dir_inode_operations;
467 break;
468 case S_IFLNK:
469 inode->i_op = &btrfs_symlink_inode_operations;
470 inode->i_mapping->a_ops = &btrfs_symlink_aops;
471 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400472 default:
473 init_special_inode(inode, inode->i_mode, rdev);
474 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400475 }
476 return;
477
478make_bad:
479 btrfs_release_path(root, path);
480 btrfs_free_path(path);
481 mutex_unlock(&root->fs_info->fs_mutex);
482 make_bad_inode(inode);
483}
484
Chris Mason5f39d392007-10-15 16:14:19 -0400485static void fill_inode_item(struct extent_buffer *leaf,
486 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400487 struct inode *inode)
488{
Chris Mason5f39d392007-10-15 16:14:19 -0400489 btrfs_set_inode_uid(leaf, item, inode->i_uid);
490 btrfs_set_inode_gid(leaf, item, inode->i_gid);
491 btrfs_set_inode_size(leaf, item, inode->i_size);
492 btrfs_set_inode_mode(leaf, item, inode->i_mode);
493 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
494
495 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
496 inode->i_atime.tv_sec);
497 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
498 inode->i_atime.tv_nsec);
499
500 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
501 inode->i_mtime.tv_sec);
502 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
503 inode->i_mtime.tv_nsec);
504
505 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
506 inode->i_ctime.tv_sec);
507 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
508 inode->i_ctime.tv_nsec);
509
510 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
511 btrfs_set_inode_generation(leaf, item, inode->i_generation);
512 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500513 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400514 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400515 BTRFS_I(inode)->block_group->key.objectid);
516}
517
Chris Masona52d9a82007-08-27 16:49:44 -0400518int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400519 struct btrfs_root *root,
520 struct inode *inode)
521{
522 struct btrfs_inode_item *inode_item;
523 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400524 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400525 int ret;
526
527 path = btrfs_alloc_path();
528 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400529 ret = btrfs_lookup_inode(trans, root, path,
530 &BTRFS_I(inode)->location, 1);
531 if (ret) {
532 if (ret > 0)
533 ret = -ENOENT;
534 goto failed;
535 }
536
Chris Mason5f39d392007-10-15 16:14:19 -0400537 leaf = path->nodes[0];
538 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400539 struct btrfs_inode_item);
540
Chris Mason5f39d392007-10-15 16:14:19 -0400541 fill_inode_item(leaf, inode_item, inode);
542 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400543 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400544 ret = 0;
545failed:
546 btrfs_release_path(root, path);
547 btrfs_free_path(path);
548 return ret;
549}
550
551
552static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
553 struct btrfs_root *root,
554 struct inode *dir,
555 struct dentry *dentry)
556{
557 struct btrfs_path *path;
558 const char *name = dentry->d_name.name;
559 int name_len = dentry->d_name.len;
560 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400561 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400562 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400563 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400564
565 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400566 if (!path) {
567 ret = -ENOMEM;
568 goto err;
569 }
570
Chris Mason39279cc2007-06-12 06:35:45 -0400571 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
572 name, name_len, -1);
573 if (IS_ERR(di)) {
574 ret = PTR_ERR(di);
575 goto err;
576 }
577 if (!di) {
578 ret = -ENOENT;
579 goto err;
580 }
Chris Mason5f39d392007-10-15 16:14:19 -0400581 leaf = path->nodes[0];
582 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400583 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400584 if (ret)
585 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400586 btrfs_release_path(root, path);
587
588 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400589 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400590 if (IS_ERR(di)) {
591 ret = PTR_ERR(di);
592 goto err;
593 }
594 if (!di) {
595 ret = -ENOENT;
596 goto err;
597 }
598 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400599
600 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500601 ret = btrfs_del_inode_ref(trans, root, name, name_len,
602 dentry->d_inode->i_ino,
603 dentry->d_parent->d_inode->i_ino);
604 if (ret) {
605 printk("failed to delete reference to %.*s, "
606 "inode %lu parent %lu\n", name_len, name,
607 dentry->d_inode->i_ino,
608 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500609 }
Chris Mason39279cc2007-06-12 06:35:45 -0400610err:
611 btrfs_free_path(path);
612 if (!ret) {
613 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400614 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400615 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500616#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
617 dentry->d_inode->i_nlink--;
618#else
Chris Mason39279cc2007-06-12 06:35:45 -0400619 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500620#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400621 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400622 dir->i_sb->s_dirt = 1;
623 }
624 return ret;
625}
626
627static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
628{
629 struct btrfs_root *root;
630 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500631 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400632 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500633 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400634
635 root = BTRFS_I(dir)->root;
636 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500637
638 ret = btrfs_check_free_space(root, 1, 1);
639 if (ret)
640 goto fail;
641
Chris Mason39279cc2007-06-12 06:35:45 -0400642 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400643
Chris Mason39279cc2007-06-12 06:35:45 -0400644 btrfs_set_trans_block_group(trans, dir);
645 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400646 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400647
Chris Mason2da98f02008-01-16 11:44:43 -0500648 if (inode->i_nlink == 0) {
649 int found;
650 /* if the inode isn't linked anywhere,
651 * we don't need to worry about
652 * data=ordered
653 */
654 found = btrfs_del_ordered_inode(inode);
655 if (found == 1) {
656 atomic_dec(&inode->i_count);
657 }
658 }
659
Chris Mason39279cc2007-06-12 06:35:45 -0400660 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500661fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400662 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400663 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500664 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400665 return ret;
666}
667
668static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
669{
670 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500671 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400672 int ret;
673 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400674 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500675 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400676
Yan134d4512007-10-25 15:49:25 -0400677 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
678 return -ENOTEMPTY;
679
Chris Mason39279cc2007-06-12 06:35:45 -0400680 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500681 ret = btrfs_check_free_space(root, 1, 1);
682 if (ret)
683 goto fail;
684
Chris Mason39279cc2007-06-12 06:35:45 -0400685 trans = btrfs_start_transaction(root, 1);
686 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400687
688 /* now the directory is empty */
689 err = btrfs_unlink_trans(trans, root, dir, dentry);
690 if (!err) {
691 inode->i_size = 0;
692 }
Chris Mason39544012007-12-12 14:38:19 -0500693
Chris Masond3c2fdc2007-09-17 10:58:06 -0400694 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400695 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500696fail:
Yan134d4512007-10-25 15:49:25 -0400697 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400698 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500699 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500700
Chris Mason39279cc2007-06-12 06:35:45 -0400701 if (ret && !err)
702 err = ret;
703 return err;
704}
705
Chris Mason39279cc2007-06-12 06:35:45 -0400706/*
Chris Mason39279cc2007-06-12 06:35:45 -0400707 * this can truncate away extent items, csum items and directory items.
708 * It starts at a high offset and removes keys until it can't find
709 * any higher than i_size.
710 *
711 * csum items that cross the new i_size are truncated to the new size
712 * as well.
713 */
714static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
715 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500716 struct inode *inode,
717 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400718{
719 int ret;
720 struct btrfs_path *path;
721 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400722 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400723 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400724 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400725 struct btrfs_file_extent_item *fi;
726 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400727 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400728 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500729 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500730 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400731 int found_extent;
732 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500733 int pending_del_nr = 0;
734 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400735 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400736
Chris Masona52d9a82007-08-27 16:49:44 -0400737 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400738 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400739 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400740 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400741
Chris Mason39279cc2007-06-12 06:35:45 -0400742 /* FIXME, add redo link to tree so we don't leak on crash */
743 key.objectid = inode->i_ino;
744 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400745 key.type = (u8)-1;
746
Chris Mason85e21ba2008-01-29 15:11:36 -0500747 btrfs_init_path(path);
748search_again:
749 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
750 if (ret < 0) {
751 goto error;
752 }
753 if (ret > 0) {
754 BUG_ON(path->slots[0] == 0);
755 path->slots[0]--;
756 }
757
Chris Mason39279cc2007-06-12 06:35:45 -0400758 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400759 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400760 leaf = path->nodes[0];
761 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
762 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400763
Chris Mason5f39d392007-10-15 16:14:19 -0400764 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400765 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400766
Chris Mason85e21ba2008-01-29 15:11:36 -0500767 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400768 break;
769
Chris Mason5f39d392007-10-15 16:14:19 -0400770 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400772 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400773 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400774 extent_type = btrfs_file_extent_type(leaf, fi);
775 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400776 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400777 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400778 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
779 struct btrfs_item *item = btrfs_item_nr(leaf,
780 path->slots[0]);
781 item_end += btrfs_file_extent_inline_len(leaf,
782 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400783 }
Yan008630c2007-11-07 13:31:09 -0500784 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400785 }
786 if (found_type == BTRFS_CSUM_ITEM_KEY) {
787 ret = btrfs_csum_truncate(trans, root, path,
788 inode->i_size);
789 BUG_ON(ret);
790 }
Yan008630c2007-11-07 13:31:09 -0500791 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400792 if (found_type == BTRFS_DIR_ITEM_KEY) {
793 found_type = BTRFS_INODE_ITEM_KEY;
794 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
795 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500796 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
797 found_type = BTRFS_XATTR_ITEM_KEY;
798 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
799 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400800 } else if (found_type) {
801 found_type--;
802 } else {
803 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400804 }
Yana61721d2007-09-17 11:08:38 -0400805 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500806 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400807 }
Chris Mason5f39d392007-10-15 16:14:19 -0400808 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400809 del_item = 1;
810 else
811 del_item = 0;
812 found_extent = 0;
813
814 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400815 if (found_type != BTRFS_EXTENT_DATA_KEY)
816 goto delete;
817
818 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400819 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400820 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400821 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400822 u64 orig_num_bytes =
823 btrfs_file_extent_num_bytes(leaf, fi);
824 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400825 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -0500826 extent_num_bytes = extent_num_bytes &
827 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400828 btrfs_set_file_extent_num_bytes(leaf, fi,
829 extent_num_bytes);
830 num_dec = (orig_num_bytes -
831 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400832 if (extent_start != 0) {
833 inode->i_blocks -= num_dec;
834 }
Chris Mason5f39d392007-10-15 16:14:19 -0400835 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400836 } else {
Chris Masondb945352007-10-15 16:15:53 -0400837 extent_num_bytes =
838 btrfs_file_extent_disk_num_bytes(leaf,
839 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400840 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400841 num_dec = btrfs_file_extent_num_bytes(leaf,
842 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400843 if (extent_start != 0) {
844 found_extent = 1;
845 inode->i_blocks -= num_dec;
846 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500847 root_gen = btrfs_header_generation(leaf);
848 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400849 }
Chris Mason179e29e2007-11-01 11:28:41 -0400850 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
851 !del_item) {
852 u32 newsize = inode->i_size - found_key.offset;
853 newsize = btrfs_file_extent_calc_inline_size(newsize);
854 ret = btrfs_truncate_item(trans, root, path,
855 newsize, 1);
856 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400857 }
Chris Mason179e29e2007-11-01 11:28:41 -0400858delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400859 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500860 if (!pending_del_nr) {
861 /* no pending yet, add ourselves */
862 pending_del_slot = path->slots[0];
863 pending_del_nr = 1;
864 } else if (pending_del_nr &&
865 path->slots[0] + 1 == pending_del_slot) {
866 /* hop on the pending chunk */
867 pending_del_nr++;
868 pending_del_slot = path->slots[0];
869 } else {
870 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
871 }
Chris Mason39279cc2007-06-12 06:35:45 -0400872 } else {
873 break;
874 }
Chris Mason39279cc2007-06-12 06:35:45 -0400875 if (found_extent) {
876 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500877 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500878 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500879 root_gen, inode->i_ino,
880 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400881 BUG_ON(ret);
882 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500883next:
884 if (path->slots[0] == 0) {
885 if (pending_del_nr)
886 goto del_pending;
887 btrfs_release_path(root, path);
888 goto search_again;
889 }
890
891 path->slots[0]--;
892 if (pending_del_nr &&
893 path->slots[0] + 1 != pending_del_slot) {
894 struct btrfs_key debug;
895del_pending:
896 btrfs_item_key_to_cpu(path->nodes[0], &debug,
897 pending_del_slot);
898 ret = btrfs_del_items(trans, root, path,
899 pending_del_slot,
900 pending_del_nr);
901 BUG_ON(ret);
902 pending_del_nr = 0;
903 btrfs_release_path(root, path);
904 goto search_again;
905 }
Chris Mason39279cc2007-06-12 06:35:45 -0400906 }
907 ret = 0;
908error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500909 if (pending_del_nr) {
910 ret = btrfs_del_items(trans, root, path, pending_del_slot,
911 pending_del_nr);
912 }
Chris Mason39279cc2007-06-12 06:35:45 -0400913 btrfs_release_path(root, path);
914 btrfs_free_path(path);
915 inode->i_sb->s_dirt = 1;
916 return ret;
917}
918
Chris Masonb888db22007-08-27 16:49:44 -0400919static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400920 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400921{
Chris Mason39279cc2007-06-12 06:35:45 -0400922 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400924 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400925 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500926 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400927
Chris Mason190662b2007-12-18 16:25:45 -0500928 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400929 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400930
Chris Masond1310b22008-01-24 16:13:08 -0500931 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500932 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400933 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500934
Chris Masona52d9a82007-08-27 16:49:44 -0400935 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400936 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400937 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
938 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400939 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400940 }
Chris Masonb888db22007-08-27 16:49:44 -0400941 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500942 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400943
Chris Masona52d9a82007-08-27 16:49:44 -0400944 return ret;
945}
946
947/*
948 * taken from block_truncate_page, but does cow as it zeros out
949 * any bytes left in the last page in the file.
950 */
951static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
952{
953 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400954 struct btrfs_root *root = BTRFS_I(inode)->root;
955 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400956 pgoff_t index = from >> PAGE_CACHE_SHIFT;
957 unsigned offset = from & (PAGE_CACHE_SIZE-1);
958 struct page *page;
959 int ret = 0;
960 u64 page_start;
961
962 if ((offset & (blocksize - 1)) == 0)
963 goto out;
964
965 ret = -ENOMEM;
966 page = grab_cache_page(mapping, index);
967 if (!page)
968 goto out;
969 if (!PageUptodate(page)) {
970 ret = btrfs_readpage(NULL, page);
971 lock_page(page);
972 if (!PageUptodate(page)) {
973 ret = -EIO;
974 goto out;
975 }
976 }
Chris Mason35ebb932007-10-30 16:56:53 -0400977 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400978
Chris Masonb888db22007-08-27 16:49:44 -0400979 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400980
Chris Mason39279cc2007-06-12 06:35:45 -0400981 unlock_page(page);
982 page_cache_release(page);
983out:
984 return ret;
985}
986
987static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
988{
989 struct inode *inode = dentry->d_inode;
990 int err;
991
992 err = inode_change_ok(inode, attr);
993 if (err)
994 return err;
995
996 if (S_ISREG(inode->i_mode) &&
997 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
998 struct btrfs_trans_handle *trans;
999 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001000 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001001
Chris Mason5f39d392007-10-15 16:14:19 -04001002 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001003 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001004 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001005 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001006 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001007
Chris Mason1b0f7c22008-01-30 14:33:02 -05001008 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001009 goto out;
1010
Chris Mason1832a6d2007-12-21 16:27:21 -05001011 mutex_lock(&root->fs_info->fs_mutex);
1012 err = btrfs_check_free_space(root, 1, 0);
1013 mutex_unlock(&root->fs_info->fs_mutex);
1014 if (err)
1015 goto fail;
1016
Chris Mason39279cc2007-06-12 06:35:45 -04001017 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1018
Chris Mason1b0f7c22008-01-30 14:33:02 -05001019 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001020 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001021
1022 mutex_lock(&root->fs_info->fs_mutex);
1023 trans = btrfs_start_transaction(root, 1);
1024 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001025 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001026 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001027 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001028
Chris Mason179e29e2007-11-01 11:28:41 -04001029 if (alloc_hint != EXTENT_MAP_INLINE) {
1030 err = btrfs_insert_file_extent(trans, root,
1031 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001032 hole_start, 0, 0,
1033 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001034 btrfs_drop_extent_cache(inode, hole_start,
1035 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001036 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001037 }
Chris Mason39279cc2007-06-12 06:35:45 -04001038 btrfs_end_transaction(trans, root);
1039 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001040 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001041 if (err)
1042 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001043 }
1044out:
1045 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001046fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001047 return err;
1048}
Chris Mason61295eb2008-01-14 16:24:38 -05001049
Chris Mason2da98f02008-01-16 11:44:43 -05001050void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001051{
Chris Mason2da98f02008-01-16 11:44:43 -05001052 int ret;
1053
1054 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001055 return;
1056 }
Chris Mason2da98f02008-01-16 11:44:43 -05001057
1058 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1059 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1060 return;
1061
1062 ret = btrfs_del_ordered_inode(inode);
1063 if (ret == 1) {
1064 atomic_dec(&inode->i_count);
1065 }
Chris Mason61295eb2008-01-14 16:24:38 -05001066}
1067
Chris Mason39279cc2007-06-12 06:35:45 -04001068void btrfs_delete_inode(struct inode *inode)
1069{
1070 struct btrfs_trans_handle *trans;
1071 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001072 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001073 int ret;
1074
1075 truncate_inode_pages(&inode->i_data, 0);
1076 if (is_bad_inode(inode)) {
1077 goto no_delete;
1078 }
Chris Mason5f39d392007-10-15 16:14:19 -04001079
Chris Mason39279cc2007-06-12 06:35:45 -04001080 inode->i_size = 0;
1081 mutex_lock(&root->fs_info->fs_mutex);
1082 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001083
Chris Mason39279cc2007-06-12 06:35:45 -04001084 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001085 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001086 if (ret)
1087 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001088
Chris Masond3c2fdc2007-09-17 10:58:06 -04001089 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001090 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001091
Chris Mason39279cc2007-06-12 06:35:45 -04001092 btrfs_end_transaction(trans, root);
1093 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001094 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001095 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001096 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001097
1098no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001099 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001100 btrfs_end_transaction(trans, root);
1101 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001102 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001103 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001104no_delete:
1105 clear_inode(inode);
1106}
1107
1108/*
1109 * this returns the key found in the dir entry in the location pointer.
1110 * If no dir entries were found, location->objectid is 0.
1111 */
1112static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1113 struct btrfs_key *location)
1114{
1115 const char *name = dentry->d_name.name;
1116 int namelen = dentry->d_name.len;
1117 struct btrfs_dir_item *di;
1118 struct btrfs_path *path;
1119 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001120 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001121
Chris Mason39544012007-12-12 14:38:19 -05001122 if (namelen == 1 && strcmp(name, ".") == 0) {
1123 location->objectid = dir->i_ino;
1124 location->type = BTRFS_INODE_ITEM_KEY;
1125 location->offset = 0;
1126 return 0;
1127 }
Chris Mason39279cc2007-06-12 06:35:45 -04001128 path = btrfs_alloc_path();
1129 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001130
Chris Mason7a720532007-12-13 09:06:59 -05001131 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001132 struct btrfs_key key;
1133 struct extent_buffer *leaf;
1134 u32 nritems;
1135 int slot;
1136
1137 key.objectid = dir->i_ino;
1138 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1139 key.offset = 0;
1140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1141 BUG_ON(ret == 0);
1142 ret = 0;
1143
1144 leaf = path->nodes[0];
1145 slot = path->slots[0];
1146 nritems = btrfs_header_nritems(leaf);
1147 if (slot >= nritems)
1148 goto out_err;
1149
1150 btrfs_item_key_to_cpu(leaf, &key, slot);
1151 if (key.objectid != dir->i_ino ||
1152 key.type != BTRFS_INODE_REF_KEY) {
1153 goto out_err;
1154 }
1155 location->objectid = key.offset;
1156 location->type = BTRFS_INODE_ITEM_KEY;
1157 location->offset = 0;
1158 goto out;
1159 }
1160
Chris Mason39279cc2007-06-12 06:35:45 -04001161 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1162 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001163 if (IS_ERR(di))
1164 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001165 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001166 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001167 }
Chris Mason5f39d392007-10-15 16:14:19 -04001168 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001169out:
Chris Mason39279cc2007-06-12 06:35:45 -04001170 btrfs_free_path(path);
1171 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001172out_err:
1173 location->objectid = 0;
1174 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001175}
1176
1177/*
1178 * when we hit a tree root in a directory, the btrfs part of the inode
1179 * needs to be changed to reflect the root directory of the tree root. This
1180 * is kind of like crossing a mount point.
1181 */
1182static int fixup_tree_root_location(struct btrfs_root *root,
1183 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001184 struct btrfs_root **sub_root,
1185 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001186{
1187 struct btrfs_path *path;
1188 struct btrfs_root_item *ri;
1189
1190 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1191 return 0;
1192 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1193 return 0;
1194
1195 path = btrfs_alloc_path();
1196 BUG_ON(!path);
1197 mutex_lock(&root->fs_info->fs_mutex);
1198
Josef Bacik58176a92007-08-29 15:47:34 -04001199 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1200 dentry->d_name.name,
1201 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001202 if (IS_ERR(*sub_root))
1203 return PTR_ERR(*sub_root);
1204
1205 ri = &(*sub_root)->root_item;
1206 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001207 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1208 location->offset = 0;
1209
1210 btrfs_free_path(path);
1211 mutex_unlock(&root->fs_info->fs_mutex);
1212 return 0;
1213}
1214
1215static int btrfs_init_locked_inode(struct inode *inode, void *p)
1216{
1217 struct btrfs_iget_args *args = p;
1218 inode->i_ino = args->ino;
1219 BTRFS_I(inode)->root = args->root;
Chris Masond1310b22008-01-24 16:13:08 -05001220 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1221 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001222 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001223 return 0;
1224}
1225
1226static int btrfs_find_actor(struct inode *inode, void *opaque)
1227{
1228 struct btrfs_iget_args *args = opaque;
1229 return (args->ino == inode->i_ino &&
1230 args->root == BTRFS_I(inode)->root);
1231}
1232
Chris Masondc17ff82008-01-08 15:46:30 -05001233struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1234 u64 root_objectid)
1235{
1236 struct btrfs_iget_args args;
1237 args.ino = objectid;
1238 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1239
1240 if (!args.root)
1241 return NULL;
1242
1243 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1244}
1245
Chris Mason39279cc2007-06-12 06:35:45 -04001246struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1247 struct btrfs_root *root)
1248{
1249 struct inode *inode;
1250 struct btrfs_iget_args args;
1251 args.ino = objectid;
1252 args.root = root;
1253
1254 inode = iget5_locked(s, objectid, btrfs_find_actor,
1255 btrfs_init_locked_inode,
1256 (void *)&args);
1257 return inode;
1258}
1259
1260static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1261 struct nameidata *nd)
1262{
1263 struct inode * inode;
1264 struct btrfs_inode *bi = BTRFS_I(dir);
1265 struct btrfs_root *root = bi->root;
1266 struct btrfs_root *sub_root = root;
1267 struct btrfs_key location;
1268 int ret;
1269
1270 if (dentry->d_name.len > BTRFS_NAME_LEN)
1271 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001272
Chris Mason39279cc2007-06-12 06:35:45 -04001273 mutex_lock(&root->fs_info->fs_mutex);
1274 ret = btrfs_inode_by_name(dir, dentry, &location);
1275 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001276
Chris Mason39279cc2007-06-12 06:35:45 -04001277 if (ret < 0)
1278 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001279
Chris Mason39279cc2007-06-12 06:35:45 -04001280 inode = NULL;
1281 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001282 ret = fixup_tree_root_location(root, &location, &sub_root,
1283 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001284 if (ret < 0)
1285 return ERR_PTR(ret);
1286 if (ret > 0)
1287 return ERR_PTR(-ENOENT);
1288 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1289 sub_root);
1290 if (!inode)
1291 return ERR_PTR(-EACCES);
1292 if (inode->i_state & I_NEW) {
1293 /* the inode and parent dir are two different roots */
1294 if (sub_root != root) {
1295 igrab(inode);
1296 sub_root->inode = inode;
1297 }
1298 BTRFS_I(inode)->root = sub_root;
1299 memcpy(&BTRFS_I(inode)->location, &location,
1300 sizeof(location));
1301 btrfs_read_locked_inode(inode);
1302 unlock_new_inode(inode);
1303 }
1304 }
1305 return d_splice_alias(inode, dentry);
1306}
1307
Chris Mason39279cc2007-06-12 06:35:45 -04001308static unsigned char btrfs_filetype_table[] = {
1309 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1310};
1311
1312static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1313{
Chris Mason6da6aba2007-12-18 16:15:09 -05001314 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001315 struct btrfs_root *root = BTRFS_I(inode)->root;
1316 struct btrfs_item *item;
1317 struct btrfs_dir_item *di;
1318 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001319 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001320 struct btrfs_path *path;
1321 int ret;
1322 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001323 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001324 int slot;
1325 int advance;
1326 unsigned char d_type;
1327 int over = 0;
1328 u32 di_cur;
1329 u32 di_total;
1330 u32 di_len;
1331 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001332 char tmp_name[32];
1333 char *name_ptr;
1334 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001335
1336 /* FIXME, use a real flag for deciding about the key type */
1337 if (root->fs_info->tree_root == root)
1338 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001339
Chris Mason39544012007-12-12 14:38:19 -05001340 /* special case for "." */
1341 if (filp->f_pos == 0) {
1342 over = filldir(dirent, ".", 1,
1343 1, inode->i_ino,
1344 DT_DIR);
1345 if (over)
1346 return 0;
1347 filp->f_pos = 1;
1348 }
1349
Chris Mason39279cc2007-06-12 06:35:45 -04001350 mutex_lock(&root->fs_info->fs_mutex);
1351 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001352 path = btrfs_alloc_path();
1353 path->reada = 2;
1354
1355 /* special case for .., just use the back ref */
1356 if (filp->f_pos == 1) {
1357 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1358 key.offset = 0;
1359 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1360 BUG_ON(ret == 0);
1361 leaf = path->nodes[0];
1362 slot = path->slots[0];
1363 nritems = btrfs_header_nritems(leaf);
1364 if (slot >= nritems) {
1365 btrfs_release_path(root, path);
1366 goto read_dir_items;
1367 }
1368 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1369 btrfs_release_path(root, path);
1370 if (found_key.objectid != key.objectid ||
1371 found_key.type != BTRFS_INODE_REF_KEY)
1372 goto read_dir_items;
1373 over = filldir(dirent, "..", 2,
1374 2, found_key.offset, DT_DIR);
1375 if (over)
1376 goto nopos;
1377 filp->f_pos = 2;
1378 }
1379
1380read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001381 btrfs_set_key_type(&key, key_type);
1382 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001383
Chris Mason39279cc2007-06-12 06:35:45 -04001384 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1385 if (ret < 0)
1386 goto err;
1387 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001388 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001389 leaf = path->nodes[0];
1390 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001391 slot = path->slots[0];
1392 if (advance || slot >= nritems) {
1393 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001394 ret = btrfs_next_leaf(root, path);
1395 if (ret)
1396 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001397 leaf = path->nodes[0];
1398 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001399 slot = path->slots[0];
1400 } else {
1401 slot++;
1402 path->slots[0]++;
1403 }
1404 }
1405 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001406 item = btrfs_item_nr(leaf, slot);
1407 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1408
1409 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001410 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001411 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001412 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001413 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001414 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001415
1416 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001417 advance = 1;
1418 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1419 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001420 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001421 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001422 struct btrfs_key location;
1423
1424 name_len = btrfs_dir_name_len(leaf, di);
1425 if (name_len < 32) {
1426 name_ptr = tmp_name;
1427 } else {
1428 name_ptr = kmalloc(name_len, GFP_NOFS);
1429 BUG_ON(!name_ptr);
1430 }
1431 read_extent_buffer(leaf, name_ptr,
1432 (unsigned long)(di + 1), name_len);
1433
1434 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1435 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001436 over = filldir(dirent, name_ptr, name_len,
1437 found_key.offset,
1438 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001439 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001440
1441 if (name_ptr != tmp_name)
1442 kfree(name_ptr);
1443
Chris Mason39279cc2007-06-12 06:35:45 -04001444 if (over)
1445 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001446 di_len = btrfs_dir_name_len(leaf, di) +
1447 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001448 di_cur += di_len;
1449 di = (struct btrfs_dir_item *)((char *)di + di_len);
1450 }
1451 }
Chris Masonc2a8b6e2008-01-29 09:10:26 -05001452 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
Chris Mason39279cc2007-06-12 06:35:45 -04001453nopos:
1454 ret = 0;
1455err:
1456 btrfs_release_path(root, path);
1457 btrfs_free_path(path);
1458 mutex_unlock(&root->fs_info->fs_mutex);
1459 return ret;
1460}
1461
1462int btrfs_write_inode(struct inode *inode, int wait)
1463{
1464 struct btrfs_root *root = BTRFS_I(inode)->root;
1465 struct btrfs_trans_handle *trans;
1466 int ret = 0;
1467
1468 if (wait) {
1469 mutex_lock(&root->fs_info->fs_mutex);
1470 trans = btrfs_start_transaction(root, 1);
1471 btrfs_set_trans_block_group(trans, inode);
1472 ret = btrfs_commit_transaction(trans, root);
1473 mutex_unlock(&root->fs_info->fs_mutex);
1474 }
1475 return ret;
1476}
1477
1478/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001479 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001480 * inode changes. But, it is most likely to find the inode in cache.
1481 * FIXME, needs more benchmarking...there are no reasons other than performance
1482 * to keep or drop this code.
1483 */
1484void btrfs_dirty_inode(struct inode *inode)
1485{
1486 struct btrfs_root *root = BTRFS_I(inode)->root;
1487 struct btrfs_trans_handle *trans;
1488
1489 mutex_lock(&root->fs_info->fs_mutex);
1490 trans = btrfs_start_transaction(root, 1);
1491 btrfs_set_trans_block_group(trans, inode);
1492 btrfs_update_inode(trans, root, inode);
1493 btrfs_end_transaction(trans, root);
1494 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001495}
1496
1497static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1498 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001499 const char *name, int name_len,
1500 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001501 u64 objectid,
1502 struct btrfs_block_group_cache *group,
1503 int mode)
1504{
1505 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001506 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001507 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001508 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001509 struct btrfs_inode_ref *ref;
1510 struct btrfs_key key[2];
1511 u32 sizes[2];
1512 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001513 int ret;
1514 int owner;
1515
Chris Mason5f39d392007-10-15 16:14:19 -04001516 path = btrfs_alloc_path();
1517 BUG_ON(!path);
1518
Chris Mason39279cc2007-06-12 06:35:45 -04001519 inode = new_inode(root->fs_info->sb);
1520 if (!inode)
1521 return ERR_PTR(-ENOMEM);
1522
Chris Masond1310b22008-01-24 16:13:08 -05001523 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1524 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001525 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001526 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001527
Chris Mason39279cc2007-06-12 06:35:45 -04001528 if (mode & S_IFDIR)
1529 owner = 0;
1530 else
1531 owner = 1;
1532 group = btrfs_find_block_group(root, group, 0, 0, owner);
1533 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001534 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001535
1536 key[0].objectid = objectid;
1537 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1538 key[0].offset = 0;
1539
1540 key[1].objectid = objectid;
1541 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1542 key[1].offset = ref_objectid;
1543
1544 sizes[0] = sizeof(struct btrfs_inode_item);
1545 sizes[1] = name_len + sizeof(*ref);
1546
1547 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1548 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001549 goto fail;
1550
Chris Mason9c583092008-01-29 15:15:18 -05001551 if (objectid > root->highest_inode)
1552 root->highest_inode = objectid;
1553
Chris Mason39279cc2007-06-12 06:35:45 -04001554 inode->i_uid = current->fsuid;
1555 inode->i_gid = current->fsgid;
1556 inode->i_mode = mode;
1557 inode->i_ino = objectid;
1558 inode->i_blocks = 0;
1559 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001560 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1561 struct btrfs_inode_item);
1562 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001563
1564 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1565 struct btrfs_inode_ref);
1566 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1567 ptr = (unsigned long)(ref + 1);
1568 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1569
Chris Mason5f39d392007-10-15 16:14:19 -04001570 btrfs_mark_buffer_dirty(path->nodes[0]);
1571 btrfs_free_path(path);
1572
Chris Mason39279cc2007-06-12 06:35:45 -04001573 location = &BTRFS_I(inode)->location;
1574 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001575 location->offset = 0;
1576 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1577
Chris Mason39279cc2007-06-12 06:35:45 -04001578 insert_inode_hash(inode);
1579 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001580fail:
1581 btrfs_free_path(path);
1582 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001583}
1584
1585static inline u8 btrfs_inode_type(struct inode *inode)
1586{
1587 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1588}
1589
1590static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001591 struct dentry *dentry, struct inode *inode,
1592 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001593{
1594 int ret;
1595 struct btrfs_key key;
1596 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001597 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001598
Chris Mason39279cc2007-06-12 06:35:45 -04001599 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001600 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1601 key.offset = 0;
1602
1603 ret = btrfs_insert_dir_item(trans, root,
1604 dentry->d_name.name, dentry->d_name.len,
1605 dentry->d_parent->d_inode->i_ino,
1606 &key, btrfs_inode_type(inode));
1607 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001608 if (add_backref) {
1609 ret = btrfs_insert_inode_ref(trans, root,
1610 dentry->d_name.name,
1611 dentry->d_name.len,
1612 inode->i_ino,
1613 dentry->d_parent->d_inode->i_ino);
1614 }
Chris Mason79c44582007-06-25 10:09:33 -04001615 parent_inode = dentry->d_parent->d_inode;
1616 parent_inode->i_size += dentry->d_name.len * 2;
1617 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001618 ret = btrfs_update_inode(trans, root,
1619 dentry->d_parent->d_inode);
1620 }
1621 return ret;
1622}
1623
1624static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001625 struct dentry *dentry, struct inode *inode,
1626 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001627{
Chris Mason9c583092008-01-29 15:15:18 -05001628 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001629 if (!err) {
1630 d_instantiate(dentry, inode);
1631 return 0;
1632 }
1633 if (err > 0)
1634 err = -EEXIST;
1635 return err;
1636}
1637
Josef Bacik618e21d2007-07-11 10:18:17 -04001638static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1639 int mode, dev_t rdev)
1640{
1641 struct btrfs_trans_handle *trans;
1642 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001643 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001644 int err;
1645 int drop_inode = 0;
1646 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001647 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001648
1649 if (!new_valid_dev(rdev))
1650 return -EINVAL;
1651
1652 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001653 err = btrfs_check_free_space(root, 1, 0);
1654 if (err)
1655 goto fail;
1656
Josef Bacik618e21d2007-07-11 10:18:17 -04001657 trans = btrfs_start_transaction(root, 1);
1658 btrfs_set_trans_block_group(trans, dir);
1659
1660 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1661 if (err) {
1662 err = -ENOSPC;
1663 goto out_unlock;
1664 }
1665
Chris Mason9c583092008-01-29 15:15:18 -05001666 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1667 dentry->d_name.len,
1668 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001669 BTRFS_I(dir)->block_group, mode);
1670 err = PTR_ERR(inode);
1671 if (IS_ERR(inode))
1672 goto out_unlock;
1673
1674 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001675 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001676 if (err)
1677 drop_inode = 1;
1678 else {
1679 inode->i_op = &btrfs_special_inode_operations;
1680 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001681 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001682 }
1683 dir->i_sb->s_dirt = 1;
1684 btrfs_update_inode_block_group(trans, inode);
1685 btrfs_update_inode_block_group(trans, dir);
1686out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001687 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001688 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001689fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001690 mutex_unlock(&root->fs_info->fs_mutex);
1691
1692 if (drop_inode) {
1693 inode_dec_link_count(inode);
1694 iput(inode);
1695 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001696 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001697 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001698 return err;
1699}
1700
Chris Mason39279cc2007-06-12 06:35:45 -04001701static int btrfs_create(struct inode *dir, struct dentry *dentry,
1702 int mode, struct nameidata *nd)
1703{
1704 struct btrfs_trans_handle *trans;
1705 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001706 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001707 int err;
1708 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001709 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001710 u64 objectid;
1711
1712 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001713 err = btrfs_check_free_space(root, 1, 0);
1714 if (err)
1715 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001716 trans = btrfs_start_transaction(root, 1);
1717 btrfs_set_trans_block_group(trans, dir);
1718
1719 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1720 if (err) {
1721 err = -ENOSPC;
1722 goto out_unlock;
1723 }
1724
Chris Mason9c583092008-01-29 15:15:18 -05001725 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1726 dentry->d_name.len,
1727 dentry->d_parent->d_inode->i_ino,
1728 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001729 err = PTR_ERR(inode);
1730 if (IS_ERR(inode))
1731 goto out_unlock;
1732
1733 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001734 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001735 if (err)
1736 drop_inode = 1;
1737 else {
1738 inode->i_mapping->a_ops = &btrfs_aops;
1739 inode->i_fop = &btrfs_file_operations;
1740 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001741 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1742 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001743 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001744 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001745 }
1746 dir->i_sb->s_dirt = 1;
1747 btrfs_update_inode_block_group(trans, inode);
1748 btrfs_update_inode_block_group(trans, dir);
1749out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001750 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001751 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001752fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001753 mutex_unlock(&root->fs_info->fs_mutex);
1754
1755 if (drop_inode) {
1756 inode_dec_link_count(inode);
1757 iput(inode);
1758 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001759 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001760 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001761 return err;
1762}
1763
1764static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1765 struct dentry *dentry)
1766{
1767 struct btrfs_trans_handle *trans;
1768 struct btrfs_root *root = BTRFS_I(dir)->root;
1769 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001770 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001771 int err;
1772 int drop_inode = 0;
1773
1774 if (inode->i_nlink == 0)
1775 return -ENOENT;
1776
Chris Mason6da6aba2007-12-18 16:15:09 -05001777#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1778 inode->i_nlink++;
1779#else
Chris Mason39279cc2007-06-12 06:35:45 -04001780 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001781#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001782 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001783 err = btrfs_check_free_space(root, 1, 0);
1784 if (err)
1785 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001786 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001787
Chris Mason39279cc2007-06-12 06:35:45 -04001788 btrfs_set_trans_block_group(trans, dir);
1789 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001790 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001791
Chris Mason39279cc2007-06-12 06:35:45 -04001792 if (err)
1793 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001794
Chris Mason39279cc2007-06-12 06:35:45 -04001795 dir->i_sb->s_dirt = 1;
1796 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001797 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001798
Chris Mason54aa1f42007-06-22 14:16:25 -04001799 if (err)
1800 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001801
Chris Masond3c2fdc2007-09-17 10:58:06 -04001802 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001803 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001804fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001805 mutex_unlock(&root->fs_info->fs_mutex);
1806
1807 if (drop_inode) {
1808 inode_dec_link_count(inode);
1809 iput(inode);
1810 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001811 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001812 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001813 return err;
1814}
1815
Chris Mason39279cc2007-06-12 06:35:45 -04001816static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1817{
1818 struct inode *inode;
1819 struct btrfs_trans_handle *trans;
1820 struct btrfs_root *root = BTRFS_I(dir)->root;
1821 int err = 0;
1822 int drop_on_err = 0;
1823 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001824 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001825
1826 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001827 err = btrfs_check_free_space(root, 1, 0);
1828 if (err)
1829 goto out_unlock;
1830
Chris Mason39279cc2007-06-12 06:35:45 -04001831 trans = btrfs_start_transaction(root, 1);
1832 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001833
Chris Mason39279cc2007-06-12 06:35:45 -04001834 if (IS_ERR(trans)) {
1835 err = PTR_ERR(trans);
1836 goto out_unlock;
1837 }
1838
1839 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1840 if (err) {
1841 err = -ENOSPC;
1842 goto out_unlock;
1843 }
1844
Chris Mason9c583092008-01-29 15:15:18 -05001845 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1846 dentry->d_name.len,
1847 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001848 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1849 if (IS_ERR(inode)) {
1850 err = PTR_ERR(inode);
1851 goto out_fail;
1852 }
Chris Mason5f39d392007-10-15 16:14:19 -04001853
Chris Mason39279cc2007-06-12 06:35:45 -04001854 drop_on_err = 1;
1855 inode->i_op = &btrfs_dir_inode_operations;
1856 inode->i_fop = &btrfs_dir_file_operations;
1857 btrfs_set_trans_block_group(trans, inode);
1858
Chris Mason39544012007-12-12 14:38:19 -05001859 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001860 err = btrfs_update_inode(trans, root, inode);
1861 if (err)
1862 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001863
Chris Mason9c583092008-01-29 15:15:18 -05001864 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001865 if (err)
1866 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001867
Chris Mason39279cc2007-06-12 06:35:45 -04001868 d_instantiate(dentry, inode);
1869 drop_on_err = 0;
1870 dir->i_sb->s_dirt = 1;
1871 btrfs_update_inode_block_group(trans, inode);
1872 btrfs_update_inode_block_group(trans, dir);
1873
1874out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001875 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001876 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001877
Chris Mason39279cc2007-06-12 06:35:45 -04001878out_unlock:
1879 mutex_unlock(&root->fs_info->fs_mutex);
1880 if (drop_on_err)
1881 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001882 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001883 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001884 return err;
1885}
1886
Chris Masona52d9a82007-08-27 16:49:44 -04001887struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001888 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001889 int create)
1890{
1891 int ret;
1892 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001893 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001894 u64 extent_start = 0;
1895 u64 extent_end = 0;
1896 u64 objectid = inode->i_ino;
1897 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001898 struct btrfs_path *path;
1899 struct btrfs_root *root = BTRFS_I(inode)->root;
1900 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001901 struct extent_buffer *leaf;
1902 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001903 struct extent_map *em = NULL;
1904 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001905 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001906 struct btrfs_trans_handle *trans = NULL;
1907
1908 path = btrfs_alloc_path();
1909 BUG_ON(!path);
1910 mutex_lock(&root->fs_info->fs_mutex);
1911
1912again:
Chris Masond1310b22008-01-24 16:13:08 -05001913 spin_lock(&em_tree->lock);
1914 em = lookup_extent_mapping(em_tree, start, len);
1915 spin_unlock(&em_tree->lock);
1916
Chris Masona52d9a82007-08-27 16:49:44 -04001917 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001918 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001919 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1920 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001921 WARN_ON(1);
1922 }
Chris Mason70dec802008-01-29 09:59:12 -05001923 if (em->block_start == EXTENT_MAP_INLINE && page)
1924 free_extent_map(em);
1925 else
1926 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001927 }
Chris Masond1310b22008-01-24 16:13:08 -05001928 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001929 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001930 err = -ENOMEM;
1931 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001932 }
Chris Masond1310b22008-01-24 16:13:08 -05001933
1934 em->start = EXTENT_MAP_HOLE;
1935 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001936 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001937 ret = btrfs_lookup_file_extent(trans, root, path,
1938 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001939 if (ret < 0) {
1940 err = ret;
1941 goto out;
1942 }
1943
1944 if (ret != 0) {
1945 if (path->slots[0] == 0)
1946 goto not_found;
1947 path->slots[0]--;
1948 }
1949
Chris Mason5f39d392007-10-15 16:14:19 -04001950 leaf = path->nodes[0];
1951 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001952 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001953 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1955 found_type = btrfs_key_type(&found_key);
1956 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001957 found_type != BTRFS_EXTENT_DATA_KEY) {
1958 goto not_found;
1959 }
1960
Chris Mason5f39d392007-10-15 16:14:19 -04001961 found_type = btrfs_file_extent_type(leaf, item);
1962 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001963 if (found_type == BTRFS_FILE_EXTENT_REG) {
1964 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001965 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001966 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001967 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001968 em->start = start;
1969 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001970 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001971 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001972 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001973 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001974 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001975 }
1976 goto not_found_em;
1977 }
Chris Masondb945352007-10-15 16:15:53 -04001978 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1979 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001980 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001981 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001982 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001983 goto insert;
1984 }
Chris Masondb945352007-10-15 16:15:53 -04001985 bytenr += btrfs_file_extent_offset(leaf, item);
1986 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001987 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001988 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001989 goto insert;
1990 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05001991 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001992 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001993 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001994 size_t size;
1995 size_t extent_offset;
1996 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001997
Chris Mason5f39d392007-10-15 16:14:19 -04001998 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1999 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002000 extent_end = (extent_start + size + root->sectorsize - 1) &
2001 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002002 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002003 em->start = start;
2004 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002005 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002006 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002007 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002008 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002009 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002010 }
2011 goto not_found_em;
2012 }
2013 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002014
2015 if (!page) {
2016 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002017 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002018 goto out;
2019 }
2020
Chris Mason70dec802008-01-29 09:59:12 -05002021 page_start = page_offset(page) + pg_offset;
2022 extent_offset = page_start - extent_start;
2023 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002024 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002025 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002026 em->len = (copy_size + root->sectorsize - 1) &
2027 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002028 map = kmap(page);
2029 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002030 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002031 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002032 copy_size);
2033 flush_dcache_page(page);
2034 } else if (create && PageUptodate(page)) {
2035 if (!trans) {
2036 kunmap(page);
2037 free_extent_map(em);
2038 em = NULL;
2039 btrfs_release_path(root, path);
2040 trans = btrfs_start_transaction(root, 1);
2041 goto again;
2042 }
Chris Mason70dec802008-01-29 09:59:12 -05002043 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002044 copy_size);
2045 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002046 }
Chris Masona52d9a82007-08-27 16:49:44 -04002047 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002048 set_extent_uptodate(io_tree, em->start,
2049 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002050 goto insert;
2051 } else {
2052 printk("unkknown found_type %d\n", found_type);
2053 WARN_ON(1);
2054 }
2055not_found:
2056 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002057 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002058not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002059 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002060insert:
2061 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002062 if (em->start > start || extent_map_end(em) <= start) {
2063 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002064 err = -EIO;
2065 goto out;
2066 }
Chris Masond1310b22008-01-24 16:13:08 -05002067
2068 err = 0;
2069 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002070 ret = add_extent_mapping(em_tree, em);
2071 if (ret == -EEXIST) {
2072 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002073 em = lookup_extent_mapping(em_tree, start, len);
2074 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002075 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002076 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002077 }
Chris Masona52d9a82007-08-27 16:49:44 -04002078 }
Chris Masond1310b22008-01-24 16:13:08 -05002079 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002080out:
2081 btrfs_free_path(path);
2082 if (trans) {
2083 ret = btrfs_end_transaction(trans, root);
2084 if (!err)
2085 err = ret;
2086 }
2087 mutex_unlock(&root->fs_info->fs_mutex);
2088 if (err) {
2089 free_extent_map(em);
2090 WARN_ON(1);
2091 return ERR_PTR(err);
2092 }
2093 return em;
2094}
2095
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002096static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002097{
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002098 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002099}
2100
Chris Mason9ebefb182007-06-15 13:50:00 -04002101int btrfs_readpage(struct file *file, struct page *page)
2102{
Chris Masond1310b22008-01-24 16:13:08 -05002103 struct extent_io_tree *tree;
2104 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002105 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002106}
Chris Mason1832a6d2007-12-21 16:27:21 -05002107
Chris Mason39279cc2007-06-12 06:35:45 -04002108static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2109{
Chris Masond1310b22008-01-24 16:13:08 -05002110 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002111
2112
2113 if (current->flags & PF_MEMALLOC) {
2114 redirty_page_for_writepage(wbc, page);
2115 unlock_page(page);
2116 return 0;
2117 }
Chris Masond1310b22008-01-24 16:13:08 -05002118 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002119 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2120}
Chris Mason39279cc2007-06-12 06:35:45 -04002121
Chris Masonb293f022007-11-01 19:45:34 -04002122static int btrfs_writepages(struct address_space *mapping,
2123 struct writeback_control *wbc)
2124{
Chris Masond1310b22008-01-24 16:13:08 -05002125 struct extent_io_tree *tree;
2126 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002127 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2128}
2129
Chris Mason3ab2fb52007-11-08 10:59:22 -05002130static int
2131btrfs_readpages(struct file *file, struct address_space *mapping,
2132 struct list_head *pages, unsigned nr_pages)
2133{
Chris Masond1310b22008-01-24 16:13:08 -05002134 struct extent_io_tree *tree;
2135 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002136 return extent_readpages(tree, mapping, pages, nr_pages,
2137 btrfs_get_extent);
2138}
2139
Chris Mason70dec802008-01-29 09:59:12 -05002140static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002141{
Chris Masond1310b22008-01-24 16:13:08 -05002142 struct extent_io_tree *tree;
2143 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002144 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002145
Chris Masond1310b22008-01-24 16:13:08 -05002146 tree = &BTRFS_I(page->mapping->host)->io_tree;
2147 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002148 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002149 if (ret == 1) {
2150 ClearPagePrivate(page);
2151 set_page_private(page, 0);
2152 page_cache_release(page);
2153 }
2154 return ret;
2155}
Chris Mason39279cc2007-06-12 06:35:45 -04002156
Chris Masona52d9a82007-08-27 16:49:44 -04002157static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2158{
Chris Masond1310b22008-01-24 16:13:08 -05002159 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002160
Chris Masond1310b22008-01-24 16:13:08 -05002161 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002162 extent_invalidatepage(tree, page, offset);
2163 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002164}
2165
Chris Mason9ebefb182007-06-15 13:50:00 -04002166/*
2167 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2168 * called from a page fault handler when a page is first dirtied. Hence we must
2169 * be careful to check for EOF conditions here. We set the page up correctly
2170 * for a written page which means we get ENOSPC checking when writing into
2171 * holes and correct delalloc and unwritten extent mapping on filesystems that
2172 * support these features.
2173 *
2174 * We are not allowed to take the i_mutex here so we have to play games to
2175 * protect against truncate races as the page could now be beyond EOF. Because
2176 * vmtruncate() writes the inode size before removing pages, once we have the
2177 * page lock we can determine safely if the page is beyond EOF. If it is not
2178 * beyond EOF, then the page is guaranteed safe against truncation until we
2179 * unlock the page.
2180 */
2181int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2182{
Chris Mason6da6aba2007-12-18 16:15:09 -05002183 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002184 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002185 unsigned long end;
2186 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002187 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002188 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002189
Chris Mason1832a6d2007-12-21 16:27:21 -05002190 mutex_lock(&root->fs_info->fs_mutex);
2191 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002192 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002193 if (ret)
2194 goto out;
2195
2196 ret = -EINVAL;
2197
Chris Mason9ebefb182007-06-15 13:50:00 -04002198 lock_page(page);
2199 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002200 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002201 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002202
Chris Mason9ebefb182007-06-15 13:50:00 -04002203 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002204 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002205 /* page got truncated out from underneath us */
2206 goto out_unlock;
2207 }
2208
2209 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002210 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002211 end = size & ~PAGE_CACHE_MASK;
2212 else
2213 end = PAGE_CACHE_SIZE;
2214
Chris Masonb888db22007-08-27 16:49:44 -04002215 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002216
2217out_unlock:
2218 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002219out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002220 return ret;
2221}
2222
Chris Mason39279cc2007-06-12 06:35:45 -04002223static void btrfs_truncate(struct inode *inode)
2224{
2225 struct btrfs_root *root = BTRFS_I(inode)->root;
2226 int ret;
2227 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002228 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002229
2230 if (!S_ISREG(inode->i_mode))
2231 return;
2232 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2233 return;
2234
2235 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2236
2237 mutex_lock(&root->fs_info->fs_mutex);
2238 trans = btrfs_start_transaction(root, 1);
2239 btrfs_set_trans_block_group(trans, inode);
2240
2241 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002242 ret = btrfs_truncate_in_trans(trans, root, inode,
2243 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002244 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002245 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002246
Chris Mason39279cc2007-06-12 06:35:45 -04002247 ret = btrfs_end_transaction(trans, root);
2248 BUG_ON(ret);
2249 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002250 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002251 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002252}
2253
Chris Mason4313b392008-01-03 09:08:48 -05002254static int noinline create_subvol(struct btrfs_root *root, char *name,
2255 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002256{
2257 struct btrfs_trans_handle *trans;
2258 struct btrfs_key key;
2259 struct btrfs_root_item root_item;
2260 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002261 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002262 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002263 struct inode *inode;
2264 struct inode *dir;
2265 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002266 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002267 u64 objectid;
2268 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002269 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002270
2271 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002272 ret = btrfs_check_free_space(root, 1, 0);
2273 if (ret)
2274 goto fail_commit;
2275
Chris Mason39279cc2007-06-12 06:35:45 -04002276 trans = btrfs_start_transaction(root, 1);
2277 BUG_ON(!trans);
2278
Chris Mason7bb86312007-12-11 09:25:06 -05002279 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2280 0, &objectid);
2281 if (ret)
2282 goto fail;
2283
2284 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2285 objectid, trans->transid, 0, 0,
2286 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002287 if (IS_ERR(leaf))
2288 return PTR_ERR(leaf);
2289
2290 btrfs_set_header_nritems(leaf, 0);
2291 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002292 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002293 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002294 btrfs_set_header_owner(leaf, objectid);
2295
Chris Mason5f39d392007-10-15 16:14:19 -04002296 write_extent_buffer(leaf, root->fs_info->fsid,
2297 (unsigned long)btrfs_header_fsid(leaf),
2298 BTRFS_FSID_SIZE);
2299 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002300
2301 inode_item = &root_item.inode;
2302 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002303 inode_item->generation = cpu_to_le64(1);
2304 inode_item->size = cpu_to_le64(3);
2305 inode_item->nlink = cpu_to_le32(1);
2306 inode_item->nblocks = cpu_to_le64(1);
2307 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002308
Chris Masondb945352007-10-15 16:15:53 -04002309 btrfs_set_root_bytenr(&root_item, leaf->start);
2310 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002311 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002312 btrfs_set_root_used(&root_item, 0);
2313
Chris Mason5eda7b52007-06-22 14:16:25 -04002314 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2315 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002316
2317 free_extent_buffer(leaf);
2318 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002319
Chris Mason39279cc2007-06-12 06:35:45 -04002320 btrfs_set_root_dirid(&root_item, new_dirid);
2321
2322 key.objectid = objectid;
2323 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002324 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2325 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2326 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002327 if (ret)
2328 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002329
2330 /*
2331 * insert the directory item
2332 */
2333 key.offset = (u64)-1;
2334 dir = root->fs_info->sb->s_root->d_inode;
2335 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2336 name, namelen, dir->i_ino, &key,
2337 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002338 if (ret)
2339 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002340
Chris Mason39544012007-12-12 14:38:19 -05002341 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2342 name, namelen, objectid,
2343 root->fs_info->sb->s_root->d_inode->i_ino);
2344 if (ret)
2345 goto fail;
2346
Chris Mason39279cc2007-06-12 06:35:45 -04002347 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002348 if (ret)
2349 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002350
Josef Bacik58176a92007-08-29 15:47:34 -04002351 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002352 BUG_ON(!new_root);
2353
2354 trans = btrfs_start_transaction(new_root, 1);
2355 BUG_ON(!trans);
2356
Chris Mason9c583092008-01-29 15:15:18 -05002357 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2358 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002359 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002360 if (IS_ERR(inode))
2361 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002362 inode->i_op = &btrfs_dir_inode_operations;
2363 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002364 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002365
Chris Mason39544012007-12-12 14:38:19 -05002366 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2367 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002368 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002369 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002370 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002371 if (ret)
2372 goto fail;
2373fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002374 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002375 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002376 if (err && !ret)
2377 ret = err;
2378fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002379 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002380 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002381 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002382 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002383}
2384
2385static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2386{
Chris Mason3063d292008-01-08 15:46:30 -05002387 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002388 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002389 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002390 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002391 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002392
2393 if (!root->ref_cows)
2394 return -EINVAL;
2395
2396 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002397 ret = btrfs_check_free_space(root, 1, 0);
2398 if (ret)
2399 goto fail_unlock;
2400
Chris Mason3063d292008-01-08 15:46:30 -05002401 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2402 if (!pending_snapshot) {
2403 ret = -ENOMEM;
2404 goto fail_unlock;
2405 }
Yanfb4bc1e2008-01-17 11:59:51 -05002406 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002407 if (!pending_snapshot->name) {
2408 ret = -ENOMEM;
2409 kfree(pending_snapshot);
2410 goto fail_unlock;
2411 }
Yanfb4bc1e2008-01-17 11:59:51 -05002412 memcpy(pending_snapshot->name, name, namelen);
2413 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002414 trans = btrfs_start_transaction(root, 1);
2415 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002416 pending_snapshot->root = root;
2417 list_add(&pending_snapshot->list,
2418 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002419 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002420 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002421
Chris Mason1832a6d2007-12-21 16:27:21 -05002422fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002423 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002424 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002425 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002426 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002427}
2428
Chris Masonedbd8d42007-12-21 16:27:24 -05002429unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002430 struct file_ra_state *ra, struct file *file,
2431 pgoff_t offset, pgoff_t last_index)
2432{
2433 pgoff_t req_size;
2434
2435#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2436 req_size = last_index - offset + 1;
2437 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2438 return offset;
2439#else
2440 req_size = min(last_index - offset + 1, (pgoff_t)128);
2441 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2442 return offset + req_size;
2443#endif
2444}
2445
2446int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002447 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002449 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002450 struct page *page;
2451 unsigned long last_index;
2452 unsigned long ra_index = 0;
2453 u64 page_start;
2454 u64 page_end;
2455 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002456 int ret;
2457
2458 mutex_lock(&root->fs_info->fs_mutex);
2459 ret = btrfs_check_free_space(root, inode->i_size, 0);
2460 mutex_unlock(&root->fs_info->fs_mutex);
2461 if (ret)
2462 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002463
2464 mutex_lock(&inode->i_mutex);
2465 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2466 for (i = 0; i <= last_index; i++) {
2467 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002468 ra_index = btrfs_force_ra(inode->i_mapping,
2469 &file->f_ra,
2470 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002471 }
2472 page = grab_cache_page(inode->i_mapping, i);
2473 if (!page)
2474 goto out_unlock;
2475 if (!PageUptodate(page)) {
2476 btrfs_readpage(NULL, page);
2477 lock_page(page);
2478 if (!PageUptodate(page)) {
2479 unlock_page(page);
2480 page_cache_release(page);
2481 goto out_unlock;
2482 }
2483 }
Chris Mason35ebb932007-10-30 16:56:53 -04002484 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002485 page_end = page_start + PAGE_CACHE_SIZE - 1;
2486
Chris Masond1310b22008-01-24 16:13:08 -05002487 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002488 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002489 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002490
Chris Masond1310b22008-01-24 16:13:08 -05002491 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002492 set_page_dirty(page);
2493 unlock_page(page);
2494 page_cache_release(page);
2495 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2496 }
2497
2498out_unlock:
2499 mutex_unlock(&inode->i_mutex);
2500 return 0;
2501}
2502
Chris Masonedbd8d42007-12-21 16:27:24 -05002503static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2504{
2505 u64 new_size;
2506 u64 old_size;
2507 struct btrfs_ioctl_vol_args *vol_args;
2508 struct btrfs_trans_handle *trans;
2509 char *sizestr;
2510 int ret = 0;
2511 int namelen;
2512 int mod = 0;
2513
2514 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2515
2516 if (!vol_args)
2517 return -ENOMEM;
2518
2519 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2520 ret = -EFAULT;
2521 goto out;
2522 }
2523 namelen = strlen(vol_args->name);
2524 if (namelen > BTRFS_VOL_NAME_MAX) {
2525 ret = -EINVAL;
2526 goto out;
2527 }
2528
2529 sizestr = vol_args->name;
2530 if (!strcmp(sizestr, "max"))
2531 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2532 else {
2533 if (sizestr[0] == '-') {
2534 mod = -1;
2535 sizestr++;
2536 } else if (sizestr[0] == '+') {
2537 mod = 1;
2538 sizestr++;
2539 }
2540 new_size = btrfs_parse_size(sizestr);
2541 if (new_size == 0) {
2542 ret = -EINVAL;
2543 goto out;
2544 }
2545 }
2546
2547 mutex_lock(&root->fs_info->fs_mutex);
2548 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2549
2550 if (mod < 0) {
2551 if (new_size > old_size) {
2552 ret = -EINVAL;
2553 goto out_unlock;
2554 }
2555 new_size = old_size - new_size;
2556 } else if (mod > 0) {
2557 new_size = old_size + new_size;
2558 }
2559
2560 if (new_size < 256 * 1024 * 1024) {
2561 ret = -EINVAL;
2562 goto out_unlock;
2563 }
2564 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2565 ret = -EFBIG;
2566 goto out_unlock;
2567 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002568
2569 do_div(new_size, root->sectorsize);
2570 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002571
2572printk("new size is %Lu\n", new_size);
2573 if (new_size > old_size) {
2574 trans = btrfs_start_transaction(root, 1);
2575 ret = btrfs_grow_extent_tree(trans, root, new_size);
2576 btrfs_commit_transaction(trans, root);
2577 } else {
2578 ret = btrfs_shrink_extent_tree(root, new_size);
2579 }
2580
2581out_unlock:
2582 mutex_unlock(&root->fs_info->fs_mutex);
2583out:
2584 kfree(vol_args);
2585 return ret;
2586}
2587
Chris Mason4313b392008-01-03 09:08:48 -05002588static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2589 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002590{
Chris Mason4aec2b52007-12-18 16:25:45 -05002591 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002592 struct btrfs_dir_item *di;
2593 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002594 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002595 int namelen;
2596 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002597
Chris Mason4aec2b52007-12-18 16:25:45 -05002598 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002599
Chris Mason4aec2b52007-12-18 16:25:45 -05002600 if (!vol_args)
2601 return -ENOMEM;
2602
2603 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2604 ret = -EFAULT;
2605 goto out;
2606 }
2607
2608 namelen = strlen(vol_args->name);
2609 if (namelen > BTRFS_VOL_NAME_MAX) {
2610 ret = -EINVAL;
2611 goto out;
2612 }
2613 if (strchr(vol_args->name, '/')) {
2614 ret = -EINVAL;
2615 goto out;
2616 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002617
2618 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002619 if (!path) {
2620 ret = -ENOMEM;
2621 goto out;
2622 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002623
2624 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2625 mutex_lock(&root->fs_info->fs_mutex);
2626 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2627 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002628 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002629 mutex_unlock(&root->fs_info->fs_mutex);
2630 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002631
2632 if (di && !IS_ERR(di)) {
2633 ret = -EEXIST;
2634 goto out;
2635 }
2636
2637 if (IS_ERR(di)) {
2638 ret = PTR_ERR(di);
2639 goto out;
2640 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002641
2642 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002643 ret = create_subvol(root, vol_args->name, namelen);
2644 else
2645 ret = create_snapshot(root, vol_args->name, namelen);
2646out:
2647 kfree(vol_args);
2648 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002649}
2650
2651static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002652{
Chris Mason6da6aba2007-12-18 16:15:09 -05002653 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002654 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002655
2656 switch (inode->i_mode & S_IFMT) {
2657 case S_IFDIR:
2658 mutex_lock(&root->fs_info->fs_mutex);
2659 btrfs_defrag_root(root, 0);
2660 btrfs_defrag_root(root->fs_info->extent_root, 0);
2661 mutex_unlock(&root->fs_info->fs_mutex);
2662 break;
2663 case S_IFREG:
2664 btrfs_defrag_file(file);
2665 break;
2666 }
2667
2668 return 0;
2669}
2670
2671long btrfs_ioctl(struct file *file, unsigned int
2672 cmd, unsigned long arg)
2673{
Chris Mason6da6aba2007-12-18 16:15:09 -05002674 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002675
2676 switch (cmd) {
2677 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002678 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002679 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002680 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002681 case BTRFS_IOC_RESIZE:
2682 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002683 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002684
2685 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002686}
2687
Chris Mason39279cc2007-06-12 06:35:45 -04002688/*
2689 * Called inside transaction, so use GFP_NOFS
2690 */
2691struct inode *btrfs_alloc_inode(struct super_block *sb)
2692{
2693 struct btrfs_inode *ei;
2694
2695 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2696 if (!ei)
2697 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002698 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002699 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002700 return &ei->vfs_inode;
2701}
2702
2703void btrfs_destroy_inode(struct inode *inode)
2704{
2705 WARN_ON(!list_empty(&inode->i_dentry));
2706 WARN_ON(inode->i_data.nrpages);
2707
Chris Mason8c416c92008-01-14 15:10:26 -05002708 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002709 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2710}
2711
Chris Mason44ec0b72007-10-29 10:55:05 -04002712#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2713static void init_once(struct kmem_cache * cachep, void *foo)
2714#else
Chris Mason39279cc2007-06-12 06:35:45 -04002715static void init_once(void * foo, struct kmem_cache * cachep,
2716 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002717#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002718{
2719 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2720
2721 inode_init_once(&ei->vfs_inode);
2722}
2723
2724void btrfs_destroy_cachep(void)
2725{
2726 if (btrfs_inode_cachep)
2727 kmem_cache_destroy(btrfs_inode_cachep);
2728 if (btrfs_trans_handle_cachep)
2729 kmem_cache_destroy(btrfs_trans_handle_cachep);
2730 if (btrfs_transaction_cachep)
2731 kmem_cache_destroy(btrfs_transaction_cachep);
2732 if (btrfs_bit_radix_cachep)
2733 kmem_cache_destroy(btrfs_bit_radix_cachep);
2734 if (btrfs_path_cachep)
2735 kmem_cache_destroy(btrfs_path_cachep);
2736}
2737
Chris Mason86479a02007-09-10 19:58:16 -04002738struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002739 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002740#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2741 void (*ctor)(struct kmem_cache *, void *)
2742#else
Chris Mason92fee662007-07-25 12:31:35 -04002743 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002744 unsigned long)
2745#endif
2746 )
Chris Mason92fee662007-07-25 12:31:35 -04002747{
2748 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2749 SLAB_MEM_SPREAD | extra_flags), ctor
2750#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2751 ,NULL
2752#endif
2753 );
2754}
2755
Chris Mason39279cc2007-06-12 06:35:45 -04002756int btrfs_init_cachep(void)
2757{
Chris Mason86479a02007-09-10 19:58:16 -04002758 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002759 sizeof(struct btrfs_inode),
2760 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002761 if (!btrfs_inode_cachep)
2762 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002763 btrfs_trans_handle_cachep =
2764 btrfs_cache_create("btrfs_trans_handle_cache",
2765 sizeof(struct btrfs_trans_handle),
2766 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002767 if (!btrfs_trans_handle_cachep)
2768 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002769 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002770 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002771 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002772 if (!btrfs_transaction_cachep)
2773 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002774 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002775 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002776 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002777 if (!btrfs_path_cachep)
2778 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002779 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002780 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002781 if (!btrfs_bit_radix_cachep)
2782 goto fail;
2783 return 0;
2784fail:
2785 btrfs_destroy_cachep();
2786 return -ENOMEM;
2787}
2788
2789static int btrfs_getattr(struct vfsmount *mnt,
2790 struct dentry *dentry, struct kstat *stat)
2791{
2792 struct inode *inode = dentry->d_inode;
2793 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002794 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002795 return 0;
2796}
2797
2798static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2799 struct inode * new_dir,struct dentry *new_dentry)
2800{
2801 struct btrfs_trans_handle *trans;
2802 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2803 struct inode *new_inode = new_dentry->d_inode;
2804 struct inode *old_inode = old_dentry->d_inode;
2805 struct timespec ctime = CURRENT_TIME;
2806 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002807 int ret;
2808
2809 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2810 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2811 return -ENOTEMPTY;
2812 }
Chris Mason5f39d392007-10-15 16:14:19 -04002813
Chris Mason39279cc2007-06-12 06:35:45 -04002814 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002815 ret = btrfs_check_free_space(root, 1, 0);
2816 if (ret)
2817 goto out_unlock;
2818
Chris Mason39279cc2007-06-12 06:35:45 -04002819 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002820
Chris Mason39279cc2007-06-12 06:35:45 -04002821 btrfs_set_trans_block_group(trans, new_dir);
2822 path = btrfs_alloc_path();
2823 if (!path) {
2824 ret = -ENOMEM;
2825 goto out_fail;
2826 }
2827
2828 old_dentry->d_inode->i_nlink++;
2829 old_dir->i_ctime = old_dir->i_mtime = ctime;
2830 new_dir->i_ctime = new_dir->i_mtime = ctime;
2831 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002832
Chris Mason39279cc2007-06-12 06:35:45 -04002833 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2834 if (ret)
2835 goto out_fail;
2836
2837 if (new_inode) {
2838 new_inode->i_ctime = CURRENT_TIME;
2839 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2840 if (ret)
2841 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002842 }
Chris Mason9c583092008-01-29 15:15:18 -05002843 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002844 if (ret)
2845 goto out_fail;
2846
2847out_fail:
2848 btrfs_free_path(path);
2849 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002850out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002851 mutex_unlock(&root->fs_info->fs_mutex);
2852 return ret;
2853}
2854
2855static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2856 const char *symname)
2857{
2858 struct btrfs_trans_handle *trans;
2859 struct btrfs_root *root = BTRFS_I(dir)->root;
2860 struct btrfs_path *path;
2861 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002862 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002863 int err;
2864 int drop_inode = 0;
2865 u64 objectid;
2866 int name_len;
2867 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002868 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002869 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002870 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002871 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002872
2873 name_len = strlen(symname) + 1;
2874 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2875 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002876
Chris Mason39279cc2007-06-12 06:35:45 -04002877 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002878 err = btrfs_check_free_space(root, 1, 0);
2879 if (err)
2880 goto out_fail;
2881
Chris Mason39279cc2007-06-12 06:35:45 -04002882 trans = btrfs_start_transaction(root, 1);
2883 btrfs_set_trans_block_group(trans, dir);
2884
2885 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2886 if (err) {
2887 err = -ENOSPC;
2888 goto out_unlock;
2889 }
2890
Chris Mason9c583092008-01-29 15:15:18 -05002891 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2892 dentry->d_name.len,
2893 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002894 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2895 err = PTR_ERR(inode);
2896 if (IS_ERR(inode))
2897 goto out_unlock;
2898
2899 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002900 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002901 if (err)
2902 drop_inode = 1;
2903 else {
2904 inode->i_mapping->a_ops = &btrfs_aops;
2905 inode->i_fop = &btrfs_file_operations;
2906 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002907 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2908 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002909 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002910 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002911 }
2912 dir->i_sb->s_dirt = 1;
2913 btrfs_update_inode_block_group(trans, inode);
2914 btrfs_update_inode_block_group(trans, dir);
2915 if (drop_inode)
2916 goto out_unlock;
2917
2918 path = btrfs_alloc_path();
2919 BUG_ON(!path);
2920 key.objectid = inode->i_ino;
2921 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002922 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2923 datasize = btrfs_file_extent_calc_inline_size(name_len);
2924 err = btrfs_insert_empty_item(trans, root, path, &key,
2925 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002926 if (err) {
2927 drop_inode = 1;
2928 goto out_unlock;
2929 }
Chris Mason5f39d392007-10-15 16:14:19 -04002930 leaf = path->nodes[0];
2931 ei = btrfs_item_ptr(leaf, path->slots[0],
2932 struct btrfs_file_extent_item);
2933 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2934 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002935 BTRFS_FILE_EXTENT_INLINE);
2936 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002937 write_extent_buffer(leaf, symname, ptr, name_len);
2938 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002939 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002940
Chris Mason39279cc2007-06-12 06:35:45 -04002941 inode->i_op = &btrfs_symlink_inode_operations;
2942 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2943 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002944 err = btrfs_update_inode(trans, root, inode);
2945 if (err)
2946 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002947
2948out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002949 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002950 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002951out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002952 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002953 if (drop_inode) {
2954 inode_dec_link_count(inode);
2955 iput(inode);
2956 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002957 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002958 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002959 return err;
2960}
Yanfdebe2b2008-01-14 13:26:08 -05002961static int btrfs_permission(struct inode *inode, int mask,
2962 struct nameidata *nd)
2963{
2964 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2965 return -EACCES;
2966 return generic_permission(inode, mask, NULL);
2967}
Chris Mason39279cc2007-06-12 06:35:45 -04002968
2969static struct inode_operations btrfs_dir_inode_operations = {
2970 .lookup = btrfs_lookup,
2971 .create = btrfs_create,
2972 .unlink = btrfs_unlink,
2973 .link = btrfs_link,
2974 .mkdir = btrfs_mkdir,
2975 .rmdir = btrfs_rmdir,
2976 .rename = btrfs_rename,
2977 .symlink = btrfs_symlink,
2978 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002979 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002980 .setxattr = generic_setxattr,
2981 .getxattr = generic_getxattr,
2982 .listxattr = btrfs_listxattr,
2983 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002984 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002985};
Chris Mason39279cc2007-06-12 06:35:45 -04002986static struct inode_operations btrfs_dir_ro_inode_operations = {
2987 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002988 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002989};
Chris Mason39279cc2007-06-12 06:35:45 -04002990static struct file_operations btrfs_dir_file_operations = {
2991 .llseek = generic_file_llseek,
2992 .read = generic_read_dir,
2993 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002994 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002995#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002996 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002997#endif
2998};
2999
Chris Masond1310b22008-01-24 16:13:08 -05003000static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003001 .fill_delalloc = run_delalloc_range,
3002 .writepage_io_hook = btrfs_writepage_io_hook,
3003 .readpage_io_hook = btrfs_readpage_io_hook,
3004 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3005};
3006
Chris Mason39279cc2007-06-12 06:35:45 -04003007static struct address_space_operations btrfs_aops = {
3008 .readpage = btrfs_readpage,
3009 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003010 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003011 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003012 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003013 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003014 .invalidatepage = btrfs_invalidatepage,
3015 .releasepage = btrfs_releasepage,
3016 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003017};
3018
3019static struct address_space_operations btrfs_symlink_aops = {
3020 .readpage = btrfs_readpage,
3021 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003022 .invalidatepage = btrfs_invalidatepage,
3023 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003024};
3025
3026static struct inode_operations btrfs_file_inode_operations = {
3027 .truncate = btrfs_truncate,
3028 .getattr = btrfs_getattr,
3029 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003030 .setxattr = generic_setxattr,
3031 .getxattr = generic_getxattr,
3032 .listxattr = btrfs_listxattr,
3033 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003034 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003035};
Josef Bacik618e21d2007-07-11 10:18:17 -04003036static struct inode_operations btrfs_special_inode_operations = {
3037 .getattr = btrfs_getattr,
3038 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003039 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003040};
Chris Mason39279cc2007-06-12 06:35:45 -04003041static struct inode_operations btrfs_symlink_inode_operations = {
3042 .readlink = generic_readlink,
3043 .follow_link = page_follow_link_light,
3044 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003045 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003046};