blob: 008e3445748ef95b3d7d42da3d7ce815f005553c [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 Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_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 Masonbe20aa92007-12-17 20:14:01 -0500105 struct btrfs_key ins;
106 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400107
Chris Masonb888db22007-08-27 16:49:44 -0400108 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400109 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500110 btrfs_set_trans_block_group(trans, inode);
111
Chris Masondb945352007-10-15 16:15:53 -0400112 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500113 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400114 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400115 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -0400116
Chris Mason179e29e2007-11-01 11:28:41 -0400117 if (alloc_hint == EXTENT_MAP_INLINE)
118 goto out;
119
Chris Masonc59f8952007-12-17 20:14:04 -0500120 while(num_bytes > 0) {
121 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
122 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
123 root->root_key.objectid,
124 trans->transid,
125 inode->i_ino, start, 0,
126 alloc_hint, (u64)-1, &ins, 1);
127 if (ret) {
128 WARN_ON(1);
129 goto out;
130 }
131 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
132 start, ins.objectid, ins.offset,
133 ins.offset);
134 num_bytes -= cur_alloc_size;
135 alloc_hint = ins.objectid + ins.offset;
136 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400137 }
Chris Masondc17ff82008-01-08 15:46:30 -0500138 btrfs_add_ordered_inode(inode);
Chris Masonb888db22007-08-27 16:49:44 -0400139out:
140 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500141 return ret;
142}
143
144static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
145{
146 u64 extent_start;
147 u64 extent_end;
148 u64 bytenr;
149 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500150 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500151 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500152 struct btrfs_root *root = BTRFS_I(inode)->root;
153 struct extent_buffer *leaf;
154 int found_type;
155 struct btrfs_path *path;
156 struct btrfs_file_extent_item *item;
157 int ret;
158 int err;
159 struct btrfs_key found_key;
160
Chris Masonc31f8832008-01-08 15:46:31 -0500161 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 path = btrfs_alloc_path();
163 BUG_ON(!path);
164again:
165 ret = btrfs_lookup_file_extent(NULL, root, path,
166 inode->i_ino, start, 0);
167 if (ret < 0) {
168 btrfs_free_path(path);
169 return ret;
170 }
171
172 cow_end = end;
173 if (ret != 0) {
174 if (path->slots[0] == 0)
175 goto not_found;
176 path->slots[0]--;
177 }
178
179 leaf = path->nodes[0];
180 item = btrfs_item_ptr(leaf, path->slots[0],
181 struct btrfs_file_extent_item);
182
183 /* are we inside the extent that was found? */
184 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
185 found_type = btrfs_key_type(&found_key);
186 if (found_key.objectid != inode->i_ino ||
187 found_type != BTRFS_EXTENT_DATA_KEY) {
188 goto not_found;
189 }
190
191 found_type = btrfs_file_extent_type(leaf, item);
192 extent_start = found_key.offset;
193 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500194 u64 extent_num_bytes;
195
196 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
197 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500198 err = 0;
199
Chris Mason1832a6d2007-12-21 16:27:21 -0500200 if (loops && start != extent_start)
201 goto not_found;
202
Chris Masonbe20aa92007-12-17 20:14:01 -0500203 if (start < extent_start || start >= extent_end)
204 goto not_found;
205
206 cow_end = min(end, extent_end - 1);
207 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
208 if (bytenr == 0)
209 goto not_found;
210
Chris Masonc31f8832008-01-08 15:46:31 -0500211 /*
212 * we may be called by the resizer, make sure we're inside
213 * the limits of the FS
214 */
215 if (bytenr + extent_num_bytes > total_fs_bytes)
216 goto not_found;
217
Chris Masonbe20aa92007-12-17 20:14:01 -0500218 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
219 goto not_found;
220 }
221
222 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500223 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500224 goto not_found;
225 }
226loop:
227 if (start > end) {
228 btrfs_free_path(path);
229 return 0;
230 }
231 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500232 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500233 goto again;
234
235not_found:
236 cow_file_range(inode, start, cow_end);
237 start = cow_end + 1;
238 goto loop;
239}
240
241static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
242{
243 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500244 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500247 if (btrfs_test_opt(root, NODATACOW) ||
248 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500249 ret = run_delalloc_nocow(inode, start, end);
250 else
251 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500252
253 spin_lock(&root->fs_info->delalloc_lock);
254 num_bytes = end + 1 - start;
255 if (root->fs_info->delalloc_bytes < num_bytes) {
256 printk("delalloc accounting error total %llu sub %llu\n",
257 root->fs_info->delalloc_bytes, num_bytes);
258 } else {
259 root->fs_info->delalloc_bytes -= num_bytes;
260 }
261 spin_unlock(&root->fs_info->delalloc_lock);
262
Chris Masonb888db22007-08-27 16:49:44 -0400263 mutex_unlock(&root->fs_info->fs_mutex);
264 return ret;
265}
266
Chris Mason07157aa2007-08-30 08:50:51 -0400267int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
268{
269 struct inode *inode = page->mapping->host;
270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 struct btrfs_trans_handle *trans;
272 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500273 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400274 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400275 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500276 if (btrfs_test_opt(root, NODATASUM) ||
277 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500278 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400279 mutex_lock(&root->fs_info->fs_mutex);
280 trans = btrfs_start_transaction(root, 1);
281 btrfs_set_trans_block_group(trans, inode);
282 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400283 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400284 start, kaddr + offset, end - start + 1);
285 kunmap(page);
286 ret = btrfs_end_transaction(trans, root);
287 BUG_ON(ret);
288 mutex_unlock(&root->fs_info->fs_mutex);
289 return ret;
290}
291
292int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
293{
294 int ret = 0;
295 struct inode *inode = page->mapping->host;
296 struct btrfs_root *root = BTRFS_I(inode)->root;
297 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
298 struct btrfs_csum_item *item;
299 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400300 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500301 if (btrfs_test_opt(root, NODATASUM) ||
302 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500303 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400304 mutex_lock(&root->fs_info->fs_mutex);
305 path = btrfs_alloc_path();
306 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
307 if (IS_ERR(item)) {
308 ret = PTR_ERR(item);
309 /* a csum that isn't present is a preallocated region. */
310 if (ret == -ENOENT || ret == -EFBIG)
311 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400312 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400313 goto out;
314 }
Chris Masonff79f812007-10-15 16:22:25 -0400315 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
316 BTRFS_CRC32_SIZE);
317 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400318out:
319 if (path)
320 btrfs_free_path(path);
321 mutex_unlock(&root->fs_info->fs_mutex);
322 return ret;
323}
324
325int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
326{
Chris Mason35ebb932007-10-30 16:56:53 -0400327 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400328 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400329 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
330 char *kaddr;
331 u64 private;
332 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400333 struct btrfs_root *root = BTRFS_I(inode)->root;
334 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400335 unsigned long flags;
Yanb98b6762008-01-08 15:54:37 -0500336 if (btrfs_test_opt(root, NODATASUM) ||
337 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500338 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400339 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400340 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400341 kaddr = kmap_atomic(page, KM_IRQ0);
342 if (ret) {
343 goto zeroit;
344 }
Chris Masonff79f812007-10-15 16:22:25 -0400345 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
346 btrfs_csum_final(csum, (char *)&csum);
347 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400348 goto zeroit;
349 }
350 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400351 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400352 return 0;
353
354zeroit:
355 printk("btrfs csum failed ino %lu off %llu\n",
356 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400357 memset(kaddr + offset, 1, end - start + 1);
358 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400359 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400360 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400361 return 0;
362}
Chris Masonb888db22007-08-27 16:49:44 -0400363
Chris Mason39279cc2007-06-12 06:35:45 -0400364void btrfs_read_locked_inode(struct inode *inode)
365{
366 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400367 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400368 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400369 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400370 struct btrfs_root *root = BTRFS_I(inode)->root;
371 struct btrfs_key location;
372 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400373 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400374 int ret;
375
376 path = btrfs_alloc_path();
377 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400378 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400379 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500380
Chris Mason39279cc2007-06-12 06:35:45 -0400381 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400382 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400383 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400384
Chris Mason5f39d392007-10-15 16:14:19 -0400385 leaf = path->nodes[0];
386 inode_item = btrfs_item_ptr(leaf, path->slots[0],
387 struct btrfs_inode_item);
388
389 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
390 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
391 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
392 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
393 inode->i_size = btrfs_inode_size(leaf, inode_item);
394
395 tspec = btrfs_inode_atime(inode_item);
396 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
397 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
398
399 tspec = btrfs_inode_mtime(inode_item);
400 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
401 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
402
403 tspec = btrfs_inode_ctime(inode_item);
404 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
405 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
406
407 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
408 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400409 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400410 rdev = btrfs_inode_rdev(leaf, inode_item);
411
412 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400413 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
414 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500415 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500416 if (!BTRFS_I(inode)->block_group) {
417 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
418 NULL, 0, 0, 0);
419 }
Chris Mason39279cc2007-06-12 06:35:45 -0400420 btrfs_free_path(path);
421 inode_item = NULL;
422
423 mutex_unlock(&root->fs_info->fs_mutex);
424
425 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400426 case S_IFREG:
427 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400428 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400429 inode->i_fop = &btrfs_file_operations;
430 inode->i_op = &btrfs_file_inode_operations;
431 break;
432 case S_IFDIR:
433 inode->i_fop = &btrfs_dir_file_operations;
434 if (root == root->fs_info->tree_root)
435 inode->i_op = &btrfs_dir_ro_inode_operations;
436 else
437 inode->i_op = &btrfs_dir_inode_operations;
438 break;
439 case S_IFLNK:
440 inode->i_op = &btrfs_symlink_inode_operations;
441 inode->i_mapping->a_ops = &btrfs_symlink_aops;
442 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400443 default:
444 init_special_inode(inode, inode->i_mode, rdev);
445 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400446 }
447 return;
448
449make_bad:
450 btrfs_release_path(root, path);
451 btrfs_free_path(path);
452 mutex_unlock(&root->fs_info->fs_mutex);
453 make_bad_inode(inode);
454}
455
Chris Mason5f39d392007-10-15 16:14:19 -0400456static void fill_inode_item(struct extent_buffer *leaf,
457 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400458 struct inode *inode)
459{
Chris Mason5f39d392007-10-15 16:14:19 -0400460 btrfs_set_inode_uid(leaf, item, inode->i_uid);
461 btrfs_set_inode_gid(leaf, item, inode->i_gid);
462 btrfs_set_inode_size(leaf, item, inode->i_size);
463 btrfs_set_inode_mode(leaf, item, inode->i_mode);
464 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
465
466 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
467 inode->i_atime.tv_sec);
468 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
469 inode->i_atime.tv_nsec);
470
471 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
472 inode->i_mtime.tv_sec);
473 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
474 inode->i_mtime.tv_nsec);
475
476 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
477 inode->i_ctime.tv_sec);
478 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
479 inode->i_ctime.tv_nsec);
480
481 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
482 btrfs_set_inode_generation(leaf, item, inode->i_generation);
483 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500484 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400485 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400486 BTRFS_I(inode)->block_group->key.objectid);
487}
488
Chris Masona52d9a82007-08-27 16:49:44 -0400489int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400490 struct btrfs_root *root,
491 struct inode *inode)
492{
493 struct btrfs_inode_item *inode_item;
494 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400495 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400496 int ret;
497
498 path = btrfs_alloc_path();
499 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400500 ret = btrfs_lookup_inode(trans, root, path,
501 &BTRFS_I(inode)->location, 1);
502 if (ret) {
503 if (ret > 0)
504 ret = -ENOENT;
505 goto failed;
506 }
507
Chris Mason5f39d392007-10-15 16:14:19 -0400508 leaf = path->nodes[0];
509 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400510 struct btrfs_inode_item);
511
Chris Mason5f39d392007-10-15 16:14:19 -0400512 fill_inode_item(leaf, inode_item, inode);
513 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400514 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400515 ret = 0;
516failed:
517 btrfs_release_path(root, path);
518 btrfs_free_path(path);
519 return ret;
520}
521
522
523static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root,
525 struct inode *dir,
526 struct dentry *dentry)
527{
528 struct btrfs_path *path;
529 const char *name = dentry->d_name.name;
530 int name_len = dentry->d_name.len;
531 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400532 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400533 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400534 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400535
536 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400537 if (!path) {
538 ret = -ENOMEM;
539 goto err;
540 }
541
Chris Mason39279cc2007-06-12 06:35:45 -0400542 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
543 name, name_len, -1);
544 if (IS_ERR(di)) {
545 ret = PTR_ERR(di);
546 goto err;
547 }
548 if (!di) {
549 ret = -ENOENT;
550 goto err;
551 }
Chris Mason5f39d392007-10-15 16:14:19 -0400552 leaf = path->nodes[0];
553 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400554 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400555 if (ret)
556 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400557 btrfs_release_path(root, path);
558
559 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400560 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400561 if (IS_ERR(di)) {
562 ret = PTR_ERR(di);
563 goto err;
564 }
565 if (!di) {
566 ret = -ENOENT;
567 goto err;
568 }
569 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400570
571 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500572 ret = btrfs_del_inode_ref(trans, root, name, name_len,
573 dentry->d_inode->i_ino,
574 dentry->d_parent->d_inode->i_ino);
575 if (ret) {
576 printk("failed to delete reference to %.*s, "
577 "inode %lu parent %lu\n", name_len, name,
578 dentry->d_inode->i_ino,
579 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500580 }
Chris Mason39279cc2007-06-12 06:35:45 -0400581err:
582 btrfs_free_path(path);
583 if (!ret) {
584 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400585 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400586 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500587#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
588 dentry->d_inode->i_nlink--;
589#else
Chris Mason39279cc2007-06-12 06:35:45 -0400590 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500591#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400592 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400593 dir->i_sb->s_dirt = 1;
594 }
595 return ret;
596}
597
598static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
599{
600 struct btrfs_root *root;
601 struct btrfs_trans_handle *trans;
602 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500603 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400604
605 root = BTRFS_I(dir)->root;
606 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500607
608 ret = btrfs_check_free_space(root, 1, 1);
609 if (ret)
610 goto fail;
611
Chris Mason39279cc2007-06-12 06:35:45 -0400612 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400613
Chris Mason39279cc2007-06-12 06:35:45 -0400614 btrfs_set_trans_block_group(trans, dir);
615 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400616 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400617
Chris Mason39279cc2007-06-12 06:35:45 -0400618 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500619fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400620 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400621 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500622 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400623 return ret;
624}
625
626static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
627{
628 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500629 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400630 int ret;
631 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400632 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500633 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400634
Yan134d4512007-10-25 15:49:25 -0400635 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
636 return -ENOTEMPTY;
637
Chris Mason39279cc2007-06-12 06:35:45 -0400638 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500639 ret = btrfs_check_free_space(root, 1, 1);
640 if (ret)
641 goto fail;
642
Chris Mason39279cc2007-06-12 06:35:45 -0400643 trans = btrfs_start_transaction(root, 1);
644 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400645
646 /* now the directory is empty */
647 err = btrfs_unlink_trans(trans, root, dir, dentry);
648 if (!err) {
649 inode->i_size = 0;
650 }
Chris Mason39544012007-12-12 14:38:19 -0500651
Chris Masond3c2fdc2007-09-17 10:58:06 -0400652 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400653 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500654fail:
Yan134d4512007-10-25 15:49:25 -0400655 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400656 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500657 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500658
Chris Mason39279cc2007-06-12 06:35:45 -0400659 if (ret && !err)
660 err = ret;
661 return err;
662}
663
664static int btrfs_free_inode(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root,
666 struct inode *inode)
667{
668 struct btrfs_path *path;
669 int ret;
670
671 clear_inode(inode);
672
673 path = btrfs_alloc_path();
674 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400675 ret = btrfs_lookup_inode(trans, root, path,
676 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400677 if (ret > 0)
678 ret = -ENOENT;
679 if (!ret)
680 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400681 btrfs_free_path(path);
682 return ret;
683}
684
685/*
Chris Mason39279cc2007-06-12 06:35:45 -0400686 * this can truncate away extent items, csum items and directory items.
687 * It starts at a high offset and removes keys until it can't find
688 * any higher than i_size.
689 *
690 * csum items that cross the new i_size are truncated to the new size
691 * as well.
692 */
693static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
694 struct btrfs_root *root,
695 struct inode *inode)
696{
697 int ret;
698 struct btrfs_path *path;
699 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400700 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400701 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400702 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400703 struct btrfs_file_extent_item *fi;
704 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400705 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400706 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500707 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500708 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400709 int found_extent;
710 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400711 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400712
Chris Masona52d9a82007-08-27 16:49:44 -0400713 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400714 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400715 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400716 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400717
Chris Mason39279cc2007-06-12 06:35:45 -0400718 /* FIXME, add redo link to tree so we don't leak on crash */
719 key.objectid = inode->i_ino;
720 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400721 key.type = (u8)-1;
722
Chris Mason39279cc2007-06-12 06:35:45 -0400723 while(1) {
724 btrfs_init_path(path);
725 fi = NULL;
726 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
727 if (ret < 0) {
728 goto error;
729 }
730 if (ret > 0) {
731 BUG_ON(path->slots[0] == 0);
732 path->slots[0]--;
733 }
Chris Mason5f39d392007-10-15 16:14:19 -0400734 leaf = path->nodes[0];
735 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
736 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400737
Chris Mason5f39d392007-10-15 16:14:19 -0400738 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400739 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400740
Chris Mason39279cc2007-06-12 06:35:45 -0400741 if (found_type != BTRFS_CSUM_ITEM_KEY &&
742 found_type != BTRFS_DIR_ITEM_KEY &&
743 found_type != BTRFS_DIR_INDEX_KEY &&
744 found_type != BTRFS_EXTENT_DATA_KEY)
745 break;
746
Chris Mason5f39d392007-10-15 16:14:19 -0400747 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400748 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400749 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400750 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400751 extent_type = btrfs_file_extent_type(leaf, fi);
752 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400753 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400754 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400755 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
756 struct btrfs_item *item = btrfs_item_nr(leaf,
757 path->slots[0]);
758 item_end += btrfs_file_extent_inline_len(leaf,
759 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400760 }
Yan008630c2007-11-07 13:31:09 -0500761 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400762 }
763 if (found_type == BTRFS_CSUM_ITEM_KEY) {
764 ret = btrfs_csum_truncate(trans, root, path,
765 inode->i_size);
766 BUG_ON(ret);
767 }
Yan008630c2007-11-07 13:31:09 -0500768 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400769 if (found_type == BTRFS_DIR_ITEM_KEY) {
770 found_type = BTRFS_INODE_ITEM_KEY;
771 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
772 found_type = BTRFS_CSUM_ITEM_KEY;
773 } else if (found_type) {
774 found_type--;
775 } else {
776 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 }
Yana61721d2007-09-17 11:08:38 -0400778 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400779 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400780 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400781 }
Chris Mason5f39d392007-10-15 16:14:19 -0400782 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400783 del_item = 1;
784 else
785 del_item = 0;
786 found_extent = 0;
787
788 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400789 if (found_type != BTRFS_EXTENT_DATA_KEY)
790 goto delete;
791
792 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400793 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400794 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400795 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400796 u64 orig_num_bytes =
797 btrfs_file_extent_num_bytes(leaf, fi);
798 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400799 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400800 btrfs_set_file_extent_num_bytes(leaf, fi,
801 extent_num_bytes);
802 num_dec = (orig_num_bytes -
803 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400804 if (extent_start != 0) {
805 inode->i_blocks -= num_dec;
806 }
Chris Mason5f39d392007-10-15 16:14:19 -0400807 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400808 } else {
Chris Masondb945352007-10-15 16:15:53 -0400809 extent_num_bytes =
810 btrfs_file_extent_disk_num_bytes(leaf,
811 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400812 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400813 num_dec = btrfs_file_extent_num_bytes(leaf,
814 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400815 if (extent_start != 0) {
816 found_extent = 1;
817 inode->i_blocks -= num_dec;
818 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500819 root_gen = btrfs_header_generation(leaf);
820 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400821 }
Chris Mason179e29e2007-11-01 11:28:41 -0400822 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
823 !del_item) {
824 u32 newsize = inode->i_size - found_key.offset;
825 newsize = btrfs_file_extent_calc_inline_size(newsize);
826 ret = btrfs_truncate_item(trans, root, path,
827 newsize, 1);
828 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400829 }
Chris Mason179e29e2007-11-01 11:28:41 -0400830delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400831 if (del_item) {
832 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400833 if (ret)
834 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400835 } else {
836 break;
837 }
838 btrfs_release_path(root, path);
839 if (found_extent) {
840 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500841 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500842 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500843 root_gen, inode->i_ino,
844 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400845 BUG_ON(ret);
846 }
847 }
848 ret = 0;
849error:
850 btrfs_release_path(root, path);
851 btrfs_free_path(path);
852 inode->i_sb->s_dirt = 1;
853 return ret;
854}
855
Chris Masonb888db22007-08-27 16:49:44 -0400856static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400857 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400858{
Chris Mason39279cc2007-06-12 06:35:45 -0400859 char *kaddr;
Chris Masonb888db22007-08-27 16:49:44 -0400860 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500861 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400862 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400863 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500864 u64 existing_delalloc;
865 u64 delalloc_start;
866 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400867
Chris Mason190662b2007-12-18 16:25:45 -0500868 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400869 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400870
Chris Masonb888db22007-08-27 16:49:44 -0400871 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500872 delalloc_start = page_start;
873 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
874 &delalloc_start, page_end,
875 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db22007-08-27 16:49:44 -0400876 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
877 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500878
879 spin_lock(&root->fs_info->delalloc_lock);
880 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
881 spin_unlock(&root->fs_info->delalloc_lock);
882
Chris Masona52d9a82007-08-27 16:49:44 -0400883 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400884 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400885 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
886 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400887 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400888 }
Chris Masonb888db22007-08-27 16:49:44 -0400889 set_page_dirty(page);
890 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400891
Chris Masona52d9a82007-08-27 16:49:44 -0400892 return ret;
893}
894
895/*
896 * taken from block_truncate_page, but does cow as it zeros out
897 * any bytes left in the last page in the file.
898 */
899static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
900{
901 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400902 struct btrfs_root *root = BTRFS_I(inode)->root;
903 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400904 pgoff_t index = from >> PAGE_CACHE_SHIFT;
905 unsigned offset = from & (PAGE_CACHE_SIZE-1);
906 struct page *page;
907 int ret = 0;
908 u64 page_start;
909
910 if ((offset & (blocksize - 1)) == 0)
911 goto out;
912
913 ret = -ENOMEM;
914 page = grab_cache_page(mapping, index);
915 if (!page)
916 goto out;
917 if (!PageUptodate(page)) {
918 ret = btrfs_readpage(NULL, page);
919 lock_page(page);
920 if (!PageUptodate(page)) {
921 ret = -EIO;
922 goto out;
923 }
924 }
Chris Mason35ebb932007-10-30 16:56:53 -0400925 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400926
Chris Masonb888db22007-08-27 16:49:44 -0400927 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400928
Chris Mason39279cc2007-06-12 06:35:45 -0400929 unlock_page(page);
930 page_cache_release(page);
931out:
932 return ret;
933}
934
935static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
936{
937 struct inode *inode = dentry->d_inode;
938 int err;
939
940 err = inode_change_ok(inode, attr);
941 if (err)
942 return err;
943
944 if (S_ISREG(inode->i_mode) &&
945 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
946 struct btrfs_trans_handle *trans;
947 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400948 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
949
Chris Mason5f39d392007-10-15 16:14:19 -0400950 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400951 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400952 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400953 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400954 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400955
956 if (attr->ia_size <= pos)
957 goto out;
958
Chris Mason1832a6d2007-12-21 16:27:21 -0500959 mutex_lock(&root->fs_info->fs_mutex);
960 err = btrfs_check_free_space(root, 1, 0);
961 mutex_unlock(&root->fs_info->fs_mutex);
962 if (err)
963 goto fail;
964
Chris Mason39279cc2007-06-12 06:35:45 -0400965 btrfs_truncate_page(inode->i_mapping, inode->i_size);
966
Chris Mason2bf5a722007-08-30 11:54:02 -0400967 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400968 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400969
970 mutex_lock(&root->fs_info->fs_mutex);
971 trans = btrfs_start_transaction(root, 1);
972 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400973 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400974 pos, pos + hole_size, pos,
975 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400976
Chris Mason179e29e2007-11-01 11:28:41 -0400977 if (alloc_hint != EXTENT_MAP_INLINE) {
978 err = btrfs_insert_file_extent(trans, root,
979 inode->i_ino,
980 pos, 0, 0, hole_size);
981 }
Chris Mason39279cc2007-06-12 06:35:45 -0400982 btrfs_end_transaction(trans, root);
983 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400984 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 if (err)
986 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400987 }
988out:
989 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -0500990fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400991 return err;
992}
Chris Mason61295eb2008-01-14 16:24:38 -0500993
994void btrfs_drop_inode(struct inode *inode)
995{
Chris Masoncee36a02008-01-15 08:40:48 -0500996 if (!BTRFS_I(inode)->ordered_trans || inode->i_nlink) {
Chris Mason61295eb2008-01-14 16:24:38 -0500997 generic_drop_inode(inode);
998 return;
999 }
Chris Masoncee36a02008-01-15 08:40:48 -05001000 /* FIXME, make sure this delete actually ends up in the transaction */
1001 btrfs_del_ordered_inode(inode);
Chris Mason61295eb2008-01-14 16:24:38 -05001002 generic_drop_inode(inode);
Chris Mason61295eb2008-01-14 16:24:38 -05001003}
1004
Chris Mason39279cc2007-06-12 06:35:45 -04001005void btrfs_delete_inode(struct inode *inode)
1006{
1007 struct btrfs_trans_handle *trans;
1008 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001009 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001010 int ret;
1011
1012 truncate_inode_pages(&inode->i_data, 0);
1013 if (is_bad_inode(inode)) {
1014 goto no_delete;
1015 }
Chris Mason5f39d392007-10-15 16:14:19 -04001016
Chris Mason39279cc2007-06-12 06:35:45 -04001017 inode->i_size = 0;
1018 mutex_lock(&root->fs_info->fs_mutex);
1019 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001020
Chris Mason39279cc2007-06-12 06:35:45 -04001021 btrfs_set_trans_block_group(trans, inode);
1022 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001023 if (ret)
1024 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001025 ret = btrfs_delete_xattrs(trans, root, inode);
1026 if (ret)
1027 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001028 ret = btrfs_free_inode(trans, root, inode);
1029 if (ret)
1030 goto no_delete_lock;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001031 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001032
Chris Mason39279cc2007-06-12 06:35:45 -04001033 btrfs_end_transaction(trans, root);
1034 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001035 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001036 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001037 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001038
1039no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001040 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001041 btrfs_end_transaction(trans, root);
1042 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001043 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001044 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001045no_delete:
1046 clear_inode(inode);
1047}
1048
1049/*
1050 * this returns the key found in the dir entry in the location pointer.
1051 * If no dir entries were found, location->objectid is 0.
1052 */
1053static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1054 struct btrfs_key *location)
1055{
1056 const char *name = dentry->d_name.name;
1057 int namelen = dentry->d_name.len;
1058 struct btrfs_dir_item *di;
1059 struct btrfs_path *path;
1060 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001061 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001062
Chris Mason39544012007-12-12 14:38:19 -05001063 if (namelen == 1 && strcmp(name, ".") == 0) {
1064 location->objectid = dir->i_ino;
1065 location->type = BTRFS_INODE_ITEM_KEY;
1066 location->offset = 0;
1067 return 0;
1068 }
Chris Mason39279cc2007-06-12 06:35:45 -04001069 path = btrfs_alloc_path();
1070 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001071
Chris Mason7a720532007-12-13 09:06:59 -05001072 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001073 struct btrfs_key key;
1074 struct extent_buffer *leaf;
1075 u32 nritems;
1076 int slot;
1077
1078 key.objectid = dir->i_ino;
1079 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1080 key.offset = 0;
1081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1082 BUG_ON(ret == 0);
1083 ret = 0;
1084
1085 leaf = path->nodes[0];
1086 slot = path->slots[0];
1087 nritems = btrfs_header_nritems(leaf);
1088 if (slot >= nritems)
1089 goto out_err;
1090
1091 btrfs_item_key_to_cpu(leaf, &key, slot);
1092 if (key.objectid != dir->i_ino ||
1093 key.type != BTRFS_INODE_REF_KEY) {
1094 goto out_err;
1095 }
1096 location->objectid = key.offset;
1097 location->type = BTRFS_INODE_ITEM_KEY;
1098 location->offset = 0;
1099 goto out;
1100 }
1101
Chris Mason39279cc2007-06-12 06:35:45 -04001102 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1103 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001104 if (IS_ERR(di))
1105 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001106 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001107 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001108 }
Chris Mason5f39d392007-10-15 16:14:19 -04001109 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001110out:
Chris Mason39279cc2007-06-12 06:35:45 -04001111 btrfs_free_path(path);
1112 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001113out_err:
1114 location->objectid = 0;
1115 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001116}
1117
1118/*
1119 * when we hit a tree root in a directory, the btrfs part of the inode
1120 * needs to be changed to reflect the root directory of the tree root. This
1121 * is kind of like crossing a mount point.
1122 */
1123static int fixup_tree_root_location(struct btrfs_root *root,
1124 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001125 struct btrfs_root **sub_root,
1126 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001127{
1128 struct btrfs_path *path;
1129 struct btrfs_root_item *ri;
1130
1131 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1132 return 0;
1133 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1134 return 0;
1135
1136 path = btrfs_alloc_path();
1137 BUG_ON(!path);
1138 mutex_lock(&root->fs_info->fs_mutex);
1139
Josef Bacik58176a92007-08-29 15:47:34 -04001140 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1141 dentry->d_name.name,
1142 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001143 if (IS_ERR(*sub_root))
1144 return PTR_ERR(*sub_root);
1145
1146 ri = &(*sub_root)->root_item;
1147 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001148 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1149 location->offset = 0;
1150
1151 btrfs_free_path(path);
1152 mutex_unlock(&root->fs_info->fs_mutex);
1153 return 0;
1154}
1155
1156static int btrfs_init_locked_inode(struct inode *inode, void *p)
1157{
1158 struct btrfs_iget_args *args = p;
1159 inode->i_ino = args->ino;
1160 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001161 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1162 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001163 return 0;
1164}
1165
1166static int btrfs_find_actor(struct inode *inode, void *opaque)
1167{
1168 struct btrfs_iget_args *args = opaque;
1169 return (args->ino == inode->i_ino &&
1170 args->root == BTRFS_I(inode)->root);
1171}
1172
Chris Masondc17ff82008-01-08 15:46:30 -05001173struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1174 u64 root_objectid)
1175{
1176 struct btrfs_iget_args args;
1177 args.ino = objectid;
1178 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1179
1180 if (!args.root)
1181 return NULL;
1182
1183 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1184}
1185
Chris Mason39279cc2007-06-12 06:35:45 -04001186struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1187 struct btrfs_root *root)
1188{
1189 struct inode *inode;
1190 struct btrfs_iget_args args;
1191 args.ino = objectid;
1192 args.root = root;
1193
1194 inode = iget5_locked(s, objectid, btrfs_find_actor,
1195 btrfs_init_locked_inode,
1196 (void *)&args);
1197 return inode;
1198}
1199
1200static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1201 struct nameidata *nd)
1202{
1203 struct inode * inode;
1204 struct btrfs_inode *bi = BTRFS_I(dir);
1205 struct btrfs_root *root = bi->root;
1206 struct btrfs_root *sub_root = root;
1207 struct btrfs_key location;
1208 int ret;
1209
1210 if (dentry->d_name.len > BTRFS_NAME_LEN)
1211 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001212
Chris Mason39279cc2007-06-12 06:35:45 -04001213 mutex_lock(&root->fs_info->fs_mutex);
1214 ret = btrfs_inode_by_name(dir, dentry, &location);
1215 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001216
Chris Mason39279cc2007-06-12 06:35:45 -04001217 if (ret < 0)
1218 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001219
Chris Mason39279cc2007-06-12 06:35:45 -04001220 inode = NULL;
1221 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001222 ret = fixup_tree_root_location(root, &location, &sub_root,
1223 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001224 if (ret < 0)
1225 return ERR_PTR(ret);
1226 if (ret > 0)
1227 return ERR_PTR(-ENOENT);
1228 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1229 sub_root);
1230 if (!inode)
1231 return ERR_PTR(-EACCES);
1232 if (inode->i_state & I_NEW) {
1233 /* the inode and parent dir are two different roots */
1234 if (sub_root != root) {
1235 igrab(inode);
1236 sub_root->inode = inode;
1237 }
1238 BTRFS_I(inode)->root = sub_root;
1239 memcpy(&BTRFS_I(inode)->location, &location,
1240 sizeof(location));
1241 btrfs_read_locked_inode(inode);
1242 unlock_new_inode(inode);
1243 }
1244 }
1245 return d_splice_alias(inode, dentry);
1246}
1247
Chris Mason39279cc2007-06-12 06:35:45 -04001248static unsigned char btrfs_filetype_table[] = {
1249 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1250};
1251
1252static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1253{
Chris Mason6da6aba2007-12-18 16:15:09 -05001254 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001255 struct btrfs_root *root = BTRFS_I(inode)->root;
1256 struct btrfs_item *item;
1257 struct btrfs_dir_item *di;
1258 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001259 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001260 struct btrfs_path *path;
1261 int ret;
1262 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001263 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001264 int slot;
1265 int advance;
1266 unsigned char d_type;
1267 int over = 0;
1268 u32 di_cur;
1269 u32 di_total;
1270 u32 di_len;
1271 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001272 char tmp_name[32];
1273 char *name_ptr;
1274 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001275
1276 /* FIXME, use a real flag for deciding about the key type */
1277 if (root->fs_info->tree_root == root)
1278 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001279
Chris Mason39544012007-12-12 14:38:19 -05001280 /* special case for "." */
1281 if (filp->f_pos == 0) {
1282 over = filldir(dirent, ".", 1,
1283 1, inode->i_ino,
1284 DT_DIR);
1285 if (over)
1286 return 0;
1287 filp->f_pos = 1;
1288 }
1289
Chris Mason39279cc2007-06-12 06:35:45 -04001290 mutex_lock(&root->fs_info->fs_mutex);
1291 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001292 path = btrfs_alloc_path();
1293 path->reada = 2;
1294
1295 /* special case for .., just use the back ref */
1296 if (filp->f_pos == 1) {
1297 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1298 key.offset = 0;
1299 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1300 BUG_ON(ret == 0);
1301 leaf = path->nodes[0];
1302 slot = path->slots[0];
1303 nritems = btrfs_header_nritems(leaf);
1304 if (slot >= nritems) {
1305 btrfs_release_path(root, path);
1306 goto read_dir_items;
1307 }
1308 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1309 btrfs_release_path(root, path);
1310 if (found_key.objectid != key.objectid ||
1311 found_key.type != BTRFS_INODE_REF_KEY)
1312 goto read_dir_items;
1313 over = filldir(dirent, "..", 2,
1314 2, found_key.offset, DT_DIR);
1315 if (over)
1316 goto nopos;
1317 filp->f_pos = 2;
1318 }
1319
1320read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001321 btrfs_set_key_type(&key, key_type);
1322 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001323
Chris Mason39279cc2007-06-12 06:35:45 -04001324 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1325 if (ret < 0)
1326 goto err;
1327 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001328 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001329 leaf = path->nodes[0];
1330 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001331 slot = path->slots[0];
1332 if (advance || slot >= nritems) {
1333 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001334 ret = btrfs_next_leaf(root, path);
1335 if (ret)
1336 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001337 leaf = path->nodes[0];
1338 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001339 slot = path->slots[0];
1340 } else {
1341 slot++;
1342 path->slots[0]++;
1343 }
1344 }
1345 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001346 item = btrfs_item_nr(leaf, slot);
1347 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1348
1349 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001350 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001351 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001352 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001353 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001354 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001355
1356 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001357 advance = 1;
1358 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1359 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001360 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001361 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001362 struct btrfs_key location;
1363
1364 name_len = btrfs_dir_name_len(leaf, di);
1365 if (name_len < 32) {
1366 name_ptr = tmp_name;
1367 } else {
1368 name_ptr = kmalloc(name_len, GFP_NOFS);
1369 BUG_ON(!name_ptr);
1370 }
1371 read_extent_buffer(leaf, name_ptr,
1372 (unsigned long)(di + 1), name_len);
1373
1374 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1375 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001376 over = filldir(dirent, name_ptr, name_len,
1377 found_key.offset,
1378 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001379 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001380
1381 if (name_ptr != tmp_name)
1382 kfree(name_ptr);
1383
Chris Mason39279cc2007-06-12 06:35:45 -04001384 if (over)
1385 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001386 di_len = btrfs_dir_name_len(leaf, di) +
1387 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001388 di_cur += di_len;
1389 di = (struct btrfs_dir_item *)((char *)di + di_len);
1390 }
1391 }
1392 filp->f_pos++;
1393nopos:
1394 ret = 0;
1395err:
1396 btrfs_release_path(root, path);
1397 btrfs_free_path(path);
1398 mutex_unlock(&root->fs_info->fs_mutex);
1399 return ret;
1400}
1401
1402int btrfs_write_inode(struct inode *inode, int wait)
1403{
1404 struct btrfs_root *root = BTRFS_I(inode)->root;
1405 struct btrfs_trans_handle *trans;
1406 int ret = 0;
1407
1408 if (wait) {
1409 mutex_lock(&root->fs_info->fs_mutex);
1410 trans = btrfs_start_transaction(root, 1);
1411 btrfs_set_trans_block_group(trans, inode);
1412 ret = btrfs_commit_transaction(trans, root);
1413 mutex_unlock(&root->fs_info->fs_mutex);
1414 }
1415 return ret;
1416}
1417
1418/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001419 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001420 * inode changes. But, it is most likely to find the inode in cache.
1421 * FIXME, needs more benchmarking...there are no reasons other than performance
1422 * to keep or drop this code.
1423 */
1424void btrfs_dirty_inode(struct inode *inode)
1425{
1426 struct btrfs_root *root = BTRFS_I(inode)->root;
1427 struct btrfs_trans_handle *trans;
1428
1429 mutex_lock(&root->fs_info->fs_mutex);
1430 trans = btrfs_start_transaction(root, 1);
1431 btrfs_set_trans_block_group(trans, inode);
1432 btrfs_update_inode(trans, root, inode);
1433 btrfs_end_transaction(trans, root);
1434 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001435}
1436
1437static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1438 struct btrfs_root *root,
1439 u64 objectid,
1440 struct btrfs_block_group_cache *group,
1441 int mode)
1442{
1443 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001444 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001445 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001446 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001447 int ret;
1448 int owner;
1449
Chris Mason5f39d392007-10-15 16:14:19 -04001450 path = btrfs_alloc_path();
1451 BUG_ON(!path);
1452
Chris Mason39279cc2007-06-12 06:35:45 -04001453 inode = new_inode(root->fs_info->sb);
1454 if (!inode)
1455 return ERR_PTR(-ENOMEM);
1456
Chris Masonb888db22007-08-27 16:49:44 -04001457 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1458 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001459 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001460
Chris Mason39279cc2007-06-12 06:35:45 -04001461 if (mode & S_IFDIR)
1462 owner = 0;
1463 else
1464 owner = 1;
1465 group = btrfs_find_block_group(root, group, 0, 0, owner);
1466 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001467 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001468 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1469 if (ret)
1470 goto fail;
1471
Chris Mason39279cc2007-06-12 06:35:45 -04001472 inode->i_uid = current->fsuid;
1473 inode->i_gid = current->fsgid;
1474 inode->i_mode = mode;
1475 inode->i_ino = objectid;
1476 inode->i_blocks = 0;
1477 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001478 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1479 struct btrfs_inode_item);
1480 fill_inode_item(path->nodes[0], inode_item, inode);
1481 btrfs_mark_buffer_dirty(path->nodes[0]);
1482 btrfs_free_path(path);
1483
Chris Mason39279cc2007-06-12 06:35:45 -04001484 location = &BTRFS_I(inode)->location;
1485 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001486 location->offset = 0;
1487 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1488
Chris Mason39279cc2007-06-12 06:35:45 -04001489 insert_inode_hash(inode);
1490 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001491fail:
1492 btrfs_free_path(path);
1493 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001494}
1495
1496static inline u8 btrfs_inode_type(struct inode *inode)
1497{
1498 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1499}
1500
1501static int btrfs_add_link(struct btrfs_trans_handle *trans,
1502 struct dentry *dentry, struct inode *inode)
1503{
1504 int ret;
1505 struct btrfs_key key;
1506 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001507 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001508
Chris Mason39279cc2007-06-12 06:35:45 -04001509 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001510 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1511 key.offset = 0;
1512
1513 ret = btrfs_insert_dir_item(trans, root,
1514 dentry->d_name.name, dentry->d_name.len,
1515 dentry->d_parent->d_inode->i_ino,
1516 &key, btrfs_inode_type(inode));
1517 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001518 ret = btrfs_insert_inode_ref(trans, root,
1519 dentry->d_name.name,
1520 dentry->d_name.len,
1521 inode->i_ino,
1522 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001523 parent_inode = dentry->d_parent->d_inode;
1524 parent_inode->i_size += dentry->d_name.len * 2;
1525 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001526 ret = btrfs_update_inode(trans, root,
1527 dentry->d_parent->d_inode);
1528 }
1529 return ret;
1530}
1531
1532static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1533 struct dentry *dentry, struct inode *inode)
1534{
1535 int err = btrfs_add_link(trans, dentry, inode);
1536 if (!err) {
1537 d_instantiate(dentry, inode);
1538 return 0;
1539 }
1540 if (err > 0)
1541 err = -EEXIST;
1542 return err;
1543}
1544
Josef Bacik618e21d2007-07-11 10:18:17 -04001545static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1546 int mode, dev_t rdev)
1547{
1548 struct btrfs_trans_handle *trans;
1549 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001550 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001551 int err;
1552 int drop_inode = 0;
1553 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001554 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001555
1556 if (!new_valid_dev(rdev))
1557 return -EINVAL;
1558
1559 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001560 err = btrfs_check_free_space(root, 1, 0);
1561 if (err)
1562 goto fail;
1563
Josef Bacik618e21d2007-07-11 10:18:17 -04001564 trans = btrfs_start_transaction(root, 1);
1565 btrfs_set_trans_block_group(trans, dir);
1566
1567 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1568 if (err) {
1569 err = -ENOSPC;
1570 goto out_unlock;
1571 }
1572
1573 inode = btrfs_new_inode(trans, root, objectid,
1574 BTRFS_I(dir)->block_group, mode);
1575 err = PTR_ERR(inode);
1576 if (IS_ERR(inode))
1577 goto out_unlock;
1578
1579 btrfs_set_trans_block_group(trans, inode);
1580 err = btrfs_add_nondir(trans, dentry, inode);
1581 if (err)
1582 drop_inode = 1;
1583 else {
1584 inode->i_op = &btrfs_special_inode_operations;
1585 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001586 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001587 }
1588 dir->i_sb->s_dirt = 1;
1589 btrfs_update_inode_block_group(trans, inode);
1590 btrfs_update_inode_block_group(trans, dir);
1591out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001592 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001593 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001594fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001595 mutex_unlock(&root->fs_info->fs_mutex);
1596
1597 if (drop_inode) {
1598 inode_dec_link_count(inode);
1599 iput(inode);
1600 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001601 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001602 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001603 return err;
1604}
1605
Chris Mason39279cc2007-06-12 06:35:45 -04001606static int btrfs_create(struct inode *dir, struct dentry *dentry,
1607 int mode, struct nameidata *nd)
1608{
1609 struct btrfs_trans_handle *trans;
1610 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001611 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001612 int err;
1613 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001614 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001615 u64 objectid;
1616
1617 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001618 err = btrfs_check_free_space(root, 1, 0);
1619 if (err)
1620 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001621 trans = btrfs_start_transaction(root, 1);
1622 btrfs_set_trans_block_group(trans, dir);
1623
1624 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1625 if (err) {
1626 err = -ENOSPC;
1627 goto out_unlock;
1628 }
1629
1630 inode = btrfs_new_inode(trans, root, objectid,
1631 BTRFS_I(dir)->block_group, mode);
1632 err = PTR_ERR(inode);
1633 if (IS_ERR(inode))
1634 goto out_unlock;
1635
1636 btrfs_set_trans_block_group(trans, inode);
1637 err = btrfs_add_nondir(trans, dentry, inode);
1638 if (err)
1639 drop_inode = 1;
1640 else {
1641 inode->i_mapping->a_ops = &btrfs_aops;
1642 inode->i_fop = &btrfs_file_operations;
1643 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001644 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1645 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001646 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001647 }
1648 dir->i_sb->s_dirt = 1;
1649 btrfs_update_inode_block_group(trans, inode);
1650 btrfs_update_inode_block_group(trans, dir);
1651out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001652 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001653 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001654fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001655 mutex_unlock(&root->fs_info->fs_mutex);
1656
1657 if (drop_inode) {
1658 inode_dec_link_count(inode);
1659 iput(inode);
1660 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001661 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001662 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001663 return err;
1664}
1665
1666static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1667 struct dentry *dentry)
1668{
1669 struct btrfs_trans_handle *trans;
1670 struct btrfs_root *root = BTRFS_I(dir)->root;
1671 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001672 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001673 int err;
1674 int drop_inode = 0;
1675
1676 if (inode->i_nlink == 0)
1677 return -ENOENT;
1678
Chris Mason6da6aba2007-12-18 16:15:09 -05001679#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1680 inode->i_nlink++;
1681#else
Chris Mason39279cc2007-06-12 06:35:45 -04001682 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001683#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001684 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001685 err = btrfs_check_free_space(root, 1, 0);
1686 if (err)
1687 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001688 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001689
Chris Mason39279cc2007-06-12 06:35:45 -04001690 btrfs_set_trans_block_group(trans, dir);
1691 atomic_inc(&inode->i_count);
1692 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001693
Chris Mason39279cc2007-06-12 06:35:45 -04001694 if (err)
1695 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001696
Chris Mason39279cc2007-06-12 06:35:45 -04001697 dir->i_sb->s_dirt = 1;
1698 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001699 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001700
Chris Mason54aa1f42007-06-22 14:16:25 -04001701 if (err)
1702 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001703
Chris Masond3c2fdc2007-09-17 10:58:06 -04001704 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001705 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001706fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001707 mutex_unlock(&root->fs_info->fs_mutex);
1708
1709 if (drop_inode) {
1710 inode_dec_link_count(inode);
1711 iput(inode);
1712 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001713 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001714 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001715 return err;
1716}
1717
Chris Mason39279cc2007-06-12 06:35:45 -04001718static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1719{
1720 struct inode *inode;
1721 struct btrfs_trans_handle *trans;
1722 struct btrfs_root *root = BTRFS_I(dir)->root;
1723 int err = 0;
1724 int drop_on_err = 0;
1725 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001726 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001727
1728 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001729 err = btrfs_check_free_space(root, 1, 0);
1730 if (err)
1731 goto out_unlock;
1732
Chris Mason39279cc2007-06-12 06:35:45 -04001733 trans = btrfs_start_transaction(root, 1);
1734 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001735
Chris Mason39279cc2007-06-12 06:35:45 -04001736 if (IS_ERR(trans)) {
1737 err = PTR_ERR(trans);
1738 goto out_unlock;
1739 }
1740
1741 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1742 if (err) {
1743 err = -ENOSPC;
1744 goto out_unlock;
1745 }
1746
1747 inode = btrfs_new_inode(trans, root, objectid,
1748 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1749 if (IS_ERR(inode)) {
1750 err = PTR_ERR(inode);
1751 goto out_fail;
1752 }
Chris Mason5f39d392007-10-15 16:14:19 -04001753
Chris Mason39279cc2007-06-12 06:35:45 -04001754 drop_on_err = 1;
1755 inode->i_op = &btrfs_dir_inode_operations;
1756 inode->i_fop = &btrfs_dir_file_operations;
1757 btrfs_set_trans_block_group(trans, inode);
1758
Chris Mason39544012007-12-12 14:38:19 -05001759 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001760 err = btrfs_update_inode(trans, root, inode);
1761 if (err)
1762 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001763
Chris Mason39279cc2007-06-12 06:35:45 -04001764 err = btrfs_add_link(trans, dentry, inode);
1765 if (err)
1766 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001767
Chris Mason39279cc2007-06-12 06:35:45 -04001768 d_instantiate(dentry, inode);
1769 drop_on_err = 0;
1770 dir->i_sb->s_dirt = 1;
1771 btrfs_update_inode_block_group(trans, inode);
1772 btrfs_update_inode_block_group(trans, dir);
1773
1774out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001775 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001776 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001777
Chris Mason39279cc2007-06-12 06:35:45 -04001778out_unlock:
1779 mutex_unlock(&root->fs_info->fs_mutex);
1780 if (drop_on_err)
1781 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001782 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001783 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001784 return err;
1785}
1786
Chris Masona52d9a82007-08-27 16:49:44 -04001787struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1788 size_t page_offset, u64 start, u64 end,
1789 int create)
1790{
1791 int ret;
1792 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001793 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001794 u64 extent_start = 0;
1795 u64 extent_end = 0;
1796 u64 objectid = inode->i_ino;
1797 u32 found_type;
1798 int failed_insert = 0;
1799 struct btrfs_path *path;
1800 struct btrfs_root *root = BTRFS_I(inode)->root;
1801 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001802 struct extent_buffer *leaf;
1803 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001804 struct extent_map *em = NULL;
1805 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1806 struct btrfs_trans_handle *trans = NULL;
1807
1808 path = btrfs_alloc_path();
1809 BUG_ON(!path);
1810 mutex_lock(&root->fs_info->fs_mutex);
1811
1812again:
1813 em = lookup_extent_mapping(em_tree, start, end);
1814 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001815 if (em->start > start) {
1816 printk("get_extent start %Lu em start %Lu\n",
1817 start, em->start);
1818 WARN_ON(1);
1819 }
Chris Masona52d9a82007-08-27 16:49:44 -04001820 goto out;
1821 }
1822 if (!em) {
1823 em = alloc_extent_map(GFP_NOFS);
1824 if (!em) {
1825 err = -ENOMEM;
1826 goto out;
1827 }
Chris Mason5f39d392007-10-15 16:14:19 -04001828 em->start = EXTENT_MAP_HOLE;
1829 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001830 }
1831 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001832 ret = btrfs_lookup_file_extent(trans, root, path,
1833 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001834 if (ret < 0) {
1835 err = ret;
1836 goto out;
1837 }
1838
1839 if (ret != 0) {
1840 if (path->slots[0] == 0)
1841 goto not_found;
1842 path->slots[0]--;
1843 }
1844
Chris Mason5f39d392007-10-15 16:14:19 -04001845 leaf = path->nodes[0];
1846 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001847 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001848 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1850 found_type = btrfs_key_type(&found_key);
1851 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001852 found_type != BTRFS_EXTENT_DATA_KEY) {
1853 goto not_found;
1854 }
1855
Chris Mason5f39d392007-10-15 16:14:19 -04001856 found_type = btrfs_file_extent_type(leaf, item);
1857 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001858 if (found_type == BTRFS_FILE_EXTENT_REG) {
1859 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001860 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001861 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001862 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001863 em->start = start;
1864 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001865 if (end < extent_start)
1866 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001867 em->end = extent_end - 1;
1868 } else {
1869 em->end = end;
1870 }
1871 goto not_found_em;
1872 }
Chris Masondb945352007-10-15 16:15:53 -04001873 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1874 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001875 em->start = extent_start;
1876 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001877 em->block_start = EXTENT_MAP_HOLE;
1878 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001879 goto insert;
1880 }
Chris Masondb945352007-10-15 16:15:53 -04001881 bytenr += btrfs_file_extent_offset(leaf, item);
1882 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001883 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001884 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001885 em->start = extent_start;
1886 em->end = extent_end - 1;
1887 goto insert;
1888 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001889 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001890 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001891 size_t size;
1892 size_t extent_offset;
1893 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001894
Chris Mason5f39d392007-10-15 16:14:19 -04001895 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1896 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001897 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001898 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001899 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001900 em->start = start;
1901 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001902 if (end < extent_start)
1903 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001904 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001905 } else {
1906 em->end = end;
1907 }
1908 goto not_found_em;
1909 }
1910 em->block_start = EXTENT_MAP_INLINE;
1911 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001912
1913 if (!page) {
1914 em->start = extent_start;
1915 em->end = extent_start + size - 1;
1916 goto out;
1917 }
1918
Chris Mason35ebb932007-10-30 16:56:53 -04001919 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001920 extent_start + page_offset;
1921 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1922 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001923 em->start = extent_start + extent_offset;
1924 em->end = (em->start + copy_size -1) |
1925 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001926 map = kmap(page);
1927 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001928 if (create == 0 && !PageUptodate(page)) {
1929 read_extent_buffer(leaf, map + page_offset, ptr,
1930 copy_size);
1931 flush_dcache_page(page);
1932 } else if (create && PageUptodate(page)) {
1933 if (!trans) {
1934 kunmap(page);
1935 free_extent_map(em);
1936 em = NULL;
1937 btrfs_release_path(root, path);
1938 trans = btrfs_start_transaction(root, 1);
1939 goto again;
1940 }
1941 write_extent_buffer(leaf, map + page_offset, ptr,
1942 copy_size);
1943 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001944 }
Chris Masona52d9a82007-08-27 16:49:44 -04001945 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001946 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001947 goto insert;
1948 } else {
1949 printk("unkknown found_type %d\n", found_type);
1950 WARN_ON(1);
1951 }
1952not_found:
1953 em->start = start;
1954 em->end = end;
1955not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001956 em->block_start = EXTENT_MAP_HOLE;
1957 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001958insert:
1959 btrfs_release_path(root, path);
1960 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001961 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001962 err = -EIO;
1963 goto out;
1964 }
1965 ret = add_extent_mapping(em_tree, em);
1966 if (ret == -EEXIST) {
1967 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001968 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001969 if (0 && failed_insert == 1) {
1970 btrfs_drop_extent_cache(inode, start, end);
1971 }
Chris Masona52d9a82007-08-27 16:49:44 -04001972 failed_insert++;
1973 if (failed_insert > 5) {
1974 printk("failing to insert %Lu %Lu\n", start, end);
1975 err = -EIO;
1976 goto out;
1977 }
Chris Masona52d9a82007-08-27 16:49:44 -04001978 goto again;
1979 }
1980 err = 0;
1981out:
1982 btrfs_free_path(path);
1983 if (trans) {
1984 ret = btrfs_end_transaction(trans, root);
1985 if (!err)
1986 err = ret;
1987 }
1988 mutex_unlock(&root->fs_info->fs_mutex);
1989 if (err) {
1990 free_extent_map(em);
1991 WARN_ON(1);
1992 return ERR_PTR(err);
1993 }
1994 return em;
1995}
1996
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001997static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001998{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001999 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002000}
2001
Chris Mason9ebefb182007-06-15 13:50:00 -04002002int btrfs_readpage(struct file *file, struct page *page)
2003{
Chris Masona52d9a82007-08-27 16:49:44 -04002004 struct extent_map_tree *tree;
2005 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2006 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002007}
Chris Mason1832a6d2007-12-21 16:27:21 -05002008
Chris Mason39279cc2007-06-12 06:35:45 -04002009static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2010{
Chris Masona52d9a82007-08-27 16:49:44 -04002011 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002012
2013
2014 if (current->flags & PF_MEMALLOC) {
2015 redirty_page_for_writepage(wbc, page);
2016 unlock_page(page);
2017 return 0;
2018 }
Chris Masona52d9a82007-08-27 16:49:44 -04002019 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2020 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2021}
Chris Mason39279cc2007-06-12 06:35:45 -04002022
Chris Masonb293f022007-11-01 19:45:34 -04002023static int btrfs_writepages(struct address_space *mapping,
2024 struct writeback_control *wbc)
2025{
2026 struct extent_map_tree *tree;
2027 tree = &BTRFS_I(mapping->host)->extent_tree;
2028 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2029}
2030
Chris Mason3ab2fb52007-11-08 10:59:22 -05002031static int
2032btrfs_readpages(struct file *file, struct address_space *mapping,
2033 struct list_head *pages, unsigned nr_pages)
2034{
2035 struct extent_map_tree *tree;
2036 tree = &BTRFS_I(mapping->host)->extent_tree;
2037 return extent_readpages(tree, mapping, pages, nr_pages,
2038 btrfs_get_extent);
2039}
2040
Chris Masona52d9a82007-08-27 16:49:44 -04002041static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2042{
2043 struct extent_map_tree *tree;
2044 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002045
Chris Masona52d9a82007-08-27 16:49:44 -04002046 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2047 ret = try_release_extent_mapping(tree, page);
2048 if (ret == 1) {
2049 ClearPagePrivate(page);
2050 set_page_private(page, 0);
2051 page_cache_release(page);
2052 }
2053 return ret;
2054}
Chris Mason39279cc2007-06-12 06:35:45 -04002055
Chris Masona52d9a82007-08-27 16:49:44 -04002056static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2057{
2058 struct extent_map_tree *tree;
2059
2060 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2061 extent_invalidatepage(tree, page, offset);
2062 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002063}
2064
Chris Mason9ebefb182007-06-15 13:50:00 -04002065/*
2066 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2067 * called from a page fault handler when a page is first dirtied. Hence we must
2068 * be careful to check for EOF conditions here. We set the page up correctly
2069 * for a written page which means we get ENOSPC checking when writing into
2070 * holes and correct delalloc and unwritten extent mapping on filesystems that
2071 * support these features.
2072 *
2073 * We are not allowed to take the i_mutex here so we have to play games to
2074 * protect against truncate races as the page could now be beyond EOF. Because
2075 * vmtruncate() writes the inode size before removing pages, once we have the
2076 * page lock we can determine safely if the page is beyond EOF. If it is not
2077 * beyond EOF, then the page is guaranteed safe against truncation until we
2078 * unlock the page.
2079 */
2080int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2081{
Chris Mason6da6aba2007-12-18 16:15:09 -05002082 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002083 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002084 unsigned long end;
2085 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002086 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002087 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002088
Chris Mason1832a6d2007-12-21 16:27:21 -05002089 mutex_lock(&root->fs_info->fs_mutex);
2090 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002091 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002092 if (ret)
2093 goto out;
2094
2095 ret = -EINVAL;
2096
Chris Mason9ebefb182007-06-15 13:50:00 -04002097 lock_page(page);
2098 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002099 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002100 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002101
Chris Mason9ebefb182007-06-15 13:50:00 -04002102 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002103 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002104 /* page got truncated out from underneath us */
2105 goto out_unlock;
2106 }
2107
2108 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002109 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002110 end = size & ~PAGE_CACHE_MASK;
2111 else
2112 end = PAGE_CACHE_SIZE;
2113
Chris Masonb888db22007-08-27 16:49:44 -04002114 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002115
2116out_unlock:
2117 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002118out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002119 return ret;
2120}
2121
Chris Mason39279cc2007-06-12 06:35:45 -04002122static void btrfs_truncate(struct inode *inode)
2123{
2124 struct btrfs_root *root = BTRFS_I(inode)->root;
2125 int ret;
2126 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002127 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002128
2129 if (!S_ISREG(inode->i_mode))
2130 return;
2131 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2132 return;
2133
2134 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2135
2136 mutex_lock(&root->fs_info->fs_mutex);
2137 trans = btrfs_start_transaction(root, 1);
2138 btrfs_set_trans_block_group(trans, inode);
2139
2140 /* FIXME, add redo link to tree so we don't leak on crash */
2141 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002142 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002143 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002144
Chris Mason39279cc2007-06-12 06:35:45 -04002145 ret = btrfs_end_transaction(trans, root);
2146 BUG_ON(ret);
2147 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002148 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002149 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002150}
2151
Chris Mason4313b392008-01-03 09:08:48 -05002152static int noinline create_subvol(struct btrfs_root *root, char *name,
2153 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002154{
2155 struct btrfs_trans_handle *trans;
2156 struct btrfs_key key;
2157 struct btrfs_root_item root_item;
2158 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002159 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002160 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002161 struct inode *inode;
2162 struct inode *dir;
2163 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002164 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002165 u64 objectid;
2166 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002167 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002168
2169 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002170 ret = btrfs_check_free_space(root, 1, 0);
2171 if (ret)
2172 goto fail_commit;
2173
Chris Mason39279cc2007-06-12 06:35:45 -04002174 trans = btrfs_start_transaction(root, 1);
2175 BUG_ON(!trans);
2176
Chris Mason7bb86312007-12-11 09:25:06 -05002177 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2178 0, &objectid);
2179 if (ret)
2180 goto fail;
2181
2182 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2183 objectid, trans->transid, 0, 0,
2184 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 if (IS_ERR(leaf))
2186 return PTR_ERR(leaf);
2187
2188 btrfs_set_header_nritems(leaf, 0);
2189 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002190 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002191 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002192 btrfs_set_header_owner(leaf, objectid);
2193
Chris Mason5f39d392007-10-15 16:14:19 -04002194 write_extent_buffer(leaf, root->fs_info->fsid,
2195 (unsigned long)btrfs_header_fsid(leaf),
2196 BTRFS_FSID_SIZE);
2197 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002198
2199 inode_item = &root_item.inode;
2200 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002201 inode_item->generation = cpu_to_le64(1);
2202 inode_item->size = cpu_to_le64(3);
2203 inode_item->nlink = cpu_to_le32(1);
2204 inode_item->nblocks = cpu_to_le64(1);
2205 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002206
Chris Masondb945352007-10-15 16:15:53 -04002207 btrfs_set_root_bytenr(&root_item, leaf->start);
2208 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002209 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002210 btrfs_set_root_used(&root_item, 0);
2211
Chris Mason5eda7b52007-06-22 14:16:25 -04002212 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2213 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002214
2215 free_extent_buffer(leaf);
2216 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002217
Chris Mason39279cc2007-06-12 06:35:45 -04002218 btrfs_set_root_dirid(&root_item, new_dirid);
2219
2220 key.objectid = objectid;
2221 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002222 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2223 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2224 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002225 if (ret)
2226 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002227
2228 /*
2229 * insert the directory item
2230 */
2231 key.offset = (u64)-1;
2232 dir = root->fs_info->sb->s_root->d_inode;
2233 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2234 name, namelen, dir->i_ino, &key,
2235 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002236 if (ret)
2237 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002238
Chris Mason39544012007-12-12 14:38:19 -05002239 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2240 name, namelen, objectid,
2241 root->fs_info->sb->s_root->d_inode->i_ino);
2242 if (ret)
2243 goto fail;
2244
Chris Mason39279cc2007-06-12 06:35:45 -04002245 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002246 if (ret)
2247 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002248
Josef Bacik58176a92007-08-29 15:47:34 -04002249 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002250 BUG_ON(!new_root);
2251
2252 trans = btrfs_start_transaction(new_root, 1);
2253 BUG_ON(!trans);
2254
2255 inode = btrfs_new_inode(trans, new_root, new_dirid,
2256 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002257 if (IS_ERR(inode))
2258 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002259 inode->i_op = &btrfs_dir_inode_operations;
2260 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002261 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002262
Chris Mason39544012007-12-12 14:38:19 -05002263 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2264 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002265 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002266 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002267 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002268 if (ret)
2269 goto fail;
2270fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002271 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002272 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002273 if (err && !ret)
2274 ret = err;
2275fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002276 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002277 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002278 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002279 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002280}
2281
2282static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2283{
Chris Mason3063d292008-01-08 15:46:30 -05002284 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002285 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002286 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002287 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002288 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002289
2290 if (!root->ref_cows)
2291 return -EINVAL;
2292
2293 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002294 ret = btrfs_check_free_space(root, 1, 0);
2295 if (ret)
2296 goto fail_unlock;
2297
Chris Mason3063d292008-01-08 15:46:30 -05002298 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2299 if (!pending_snapshot) {
2300 ret = -ENOMEM;
2301 goto fail_unlock;
2302 }
2303 pending_snapshot->name = kstrndup(name, namelen, GFP_NOFS);
2304 if (!pending_snapshot->name) {
2305 ret = -ENOMEM;
2306 kfree(pending_snapshot);
2307 goto fail_unlock;
2308 }
Chris Mason39279cc2007-06-12 06:35:45 -04002309 trans = btrfs_start_transaction(root, 1);
2310 BUG_ON(!trans);
Chris Masondc17ff82008-01-08 15:46:30 -05002311
Chris Mason3063d292008-01-08 15:46:30 -05002312 pending_snapshot->root = root;
2313 list_add(&pending_snapshot->list,
2314 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002315 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002316 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002317
Chris Mason1832a6d2007-12-21 16:27:21 -05002318fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002319 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002320 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002321 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002322 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002323}
2324
Chris Masonedbd8d42007-12-21 16:27:24 -05002325unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002326 struct file_ra_state *ra, struct file *file,
2327 pgoff_t offset, pgoff_t last_index)
2328{
2329 pgoff_t req_size;
2330
2331#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2332 req_size = last_index - offset + 1;
2333 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2334 return offset;
2335#else
2336 req_size = min(last_index - offset + 1, (pgoff_t)128);
2337 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2338 return offset + req_size;
2339#endif
2340}
2341
2342int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002343 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002344 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002345 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2346 struct page *page;
2347 unsigned long last_index;
2348 unsigned long ra_index = 0;
2349 u64 page_start;
2350 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002351 u64 delalloc_start;
2352 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002353 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002354 int ret;
2355
2356 mutex_lock(&root->fs_info->fs_mutex);
2357 ret = btrfs_check_free_space(root, inode->i_size, 0);
2358 mutex_unlock(&root->fs_info->fs_mutex);
2359 if (ret)
2360 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002361
2362 mutex_lock(&inode->i_mutex);
2363 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2364 for (i = 0; i <= last_index; i++) {
2365 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002366 ra_index = btrfs_force_ra(inode->i_mapping,
2367 &file->f_ra,
2368 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002369 }
2370 page = grab_cache_page(inode->i_mapping, i);
2371 if (!page)
2372 goto out_unlock;
2373 if (!PageUptodate(page)) {
2374 btrfs_readpage(NULL, page);
2375 lock_page(page);
2376 if (!PageUptodate(page)) {
2377 unlock_page(page);
2378 page_cache_release(page);
2379 goto out_unlock;
2380 }
2381 }
Chris Mason35ebb932007-10-30 16:56:53 -04002382 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002383 page_end = page_start + PAGE_CACHE_SIZE - 1;
2384
2385 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002386 delalloc_start = page_start;
2387 existing_delalloc =
2388 count_range_bits(&BTRFS_I(inode)->extent_tree,
2389 &delalloc_start, page_end,
2390 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002391 set_extent_delalloc(em_tree, page_start,
2392 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002393
2394 spin_lock(&root->fs_info->delalloc_lock);
2395 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2396 existing_delalloc;
2397 spin_unlock(&root->fs_info->delalloc_lock);
2398
Chris Mason86479a02007-09-10 19:58:16 -04002399 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2400 set_page_dirty(page);
2401 unlock_page(page);
2402 page_cache_release(page);
2403 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2404 }
2405
2406out_unlock:
2407 mutex_unlock(&inode->i_mutex);
2408 return 0;
2409}
2410
Chris Masonedbd8d42007-12-21 16:27:24 -05002411static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2412{
2413 u64 new_size;
2414 u64 old_size;
2415 struct btrfs_ioctl_vol_args *vol_args;
2416 struct btrfs_trans_handle *trans;
2417 char *sizestr;
2418 int ret = 0;
2419 int namelen;
2420 int mod = 0;
2421
2422 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2423
2424 if (!vol_args)
2425 return -ENOMEM;
2426
2427 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2428 ret = -EFAULT;
2429 goto out;
2430 }
2431 namelen = strlen(vol_args->name);
2432 if (namelen > BTRFS_VOL_NAME_MAX) {
2433 ret = -EINVAL;
2434 goto out;
2435 }
2436
2437 sizestr = vol_args->name;
2438 if (!strcmp(sizestr, "max"))
2439 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2440 else {
2441 if (sizestr[0] == '-') {
2442 mod = -1;
2443 sizestr++;
2444 } else if (sizestr[0] == '+') {
2445 mod = 1;
2446 sizestr++;
2447 }
2448 new_size = btrfs_parse_size(sizestr);
2449 if (new_size == 0) {
2450 ret = -EINVAL;
2451 goto out;
2452 }
2453 }
2454
2455 mutex_lock(&root->fs_info->fs_mutex);
2456 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2457
2458 if (mod < 0) {
2459 if (new_size > old_size) {
2460 ret = -EINVAL;
2461 goto out_unlock;
2462 }
2463 new_size = old_size - new_size;
2464 } else if (mod > 0) {
2465 new_size = old_size + new_size;
2466 }
2467
2468 if (new_size < 256 * 1024 * 1024) {
2469 ret = -EINVAL;
2470 goto out_unlock;
2471 }
2472 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2473 ret = -EFBIG;
2474 goto out_unlock;
2475 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002476
2477 do_div(new_size, root->sectorsize);
2478 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002479
2480printk("new size is %Lu\n", new_size);
2481 if (new_size > old_size) {
2482 trans = btrfs_start_transaction(root, 1);
2483 ret = btrfs_grow_extent_tree(trans, root, new_size);
2484 btrfs_commit_transaction(trans, root);
2485 } else {
2486 ret = btrfs_shrink_extent_tree(root, new_size);
2487 }
2488
2489out_unlock:
2490 mutex_unlock(&root->fs_info->fs_mutex);
2491out:
2492 kfree(vol_args);
2493 return ret;
2494}
2495
Chris Mason4313b392008-01-03 09:08:48 -05002496static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2497 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002498{
Chris Mason4aec2b52007-12-18 16:25:45 -05002499 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002500 struct btrfs_dir_item *di;
2501 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002502 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002503 int namelen;
2504 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002505
Chris Mason4aec2b52007-12-18 16:25:45 -05002506 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002507
Chris Mason4aec2b52007-12-18 16:25:45 -05002508 if (!vol_args)
2509 return -ENOMEM;
2510
2511 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2512 ret = -EFAULT;
2513 goto out;
2514 }
2515
2516 namelen = strlen(vol_args->name);
2517 if (namelen > BTRFS_VOL_NAME_MAX) {
2518 ret = -EINVAL;
2519 goto out;
2520 }
2521 if (strchr(vol_args->name, '/')) {
2522 ret = -EINVAL;
2523 goto out;
2524 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002525
2526 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002527 if (!path) {
2528 ret = -ENOMEM;
2529 goto out;
2530 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002531
2532 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2533 mutex_lock(&root->fs_info->fs_mutex);
2534 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2535 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002536 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002537 mutex_unlock(&root->fs_info->fs_mutex);
2538 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002539
2540 if (di && !IS_ERR(di)) {
2541 ret = -EEXIST;
2542 goto out;
2543 }
2544
2545 if (IS_ERR(di)) {
2546 ret = PTR_ERR(di);
2547 goto out;
2548 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002549
2550 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002551 ret = create_subvol(root, vol_args->name, namelen);
2552 else
2553 ret = create_snapshot(root, vol_args->name, namelen);
2554out:
2555 kfree(vol_args);
2556 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002557}
2558
2559static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002560{
Chris Mason6da6aba2007-12-18 16:15:09 -05002561 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002562 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002563
2564 switch (inode->i_mode & S_IFMT) {
2565 case S_IFDIR:
2566 mutex_lock(&root->fs_info->fs_mutex);
2567 btrfs_defrag_root(root, 0);
2568 btrfs_defrag_root(root->fs_info->extent_root, 0);
2569 mutex_unlock(&root->fs_info->fs_mutex);
2570 break;
2571 case S_IFREG:
2572 btrfs_defrag_file(file);
2573 break;
2574 }
2575
2576 return 0;
2577}
2578
2579long btrfs_ioctl(struct file *file, unsigned int
2580 cmd, unsigned long arg)
2581{
Chris Mason6da6aba2007-12-18 16:15:09 -05002582 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002583
2584 switch (cmd) {
2585 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002586 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002587 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002588 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002589 case BTRFS_IOC_RESIZE:
2590 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002591 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002592
2593 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002594}
2595
Chris Mason39279cc2007-06-12 06:35:45 -04002596/*
2597 * Called inside transaction, so use GFP_NOFS
2598 */
2599struct inode *btrfs_alloc_inode(struct super_block *sb)
2600{
2601 struct btrfs_inode *ei;
2602
2603 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2604 if (!ei)
2605 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002606 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002607 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002608 return &ei->vfs_inode;
2609}
2610
2611void btrfs_destroy_inode(struct inode *inode)
2612{
2613 WARN_ON(!list_empty(&inode->i_dentry));
2614 WARN_ON(inode->i_data.nrpages);
2615
Chris Mason8c416c92008-01-14 15:10:26 -05002616 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002617 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2618}
2619
Chris Mason44ec0b72007-10-29 10:55:05 -04002620#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2621static void init_once(struct kmem_cache * cachep, void *foo)
2622#else
Chris Mason39279cc2007-06-12 06:35:45 -04002623static void init_once(void * foo, struct kmem_cache * cachep,
2624 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002625#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002626{
2627 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2628
2629 inode_init_once(&ei->vfs_inode);
2630}
2631
2632void btrfs_destroy_cachep(void)
2633{
2634 if (btrfs_inode_cachep)
2635 kmem_cache_destroy(btrfs_inode_cachep);
2636 if (btrfs_trans_handle_cachep)
2637 kmem_cache_destroy(btrfs_trans_handle_cachep);
2638 if (btrfs_transaction_cachep)
2639 kmem_cache_destroy(btrfs_transaction_cachep);
2640 if (btrfs_bit_radix_cachep)
2641 kmem_cache_destroy(btrfs_bit_radix_cachep);
2642 if (btrfs_path_cachep)
2643 kmem_cache_destroy(btrfs_path_cachep);
2644}
2645
Chris Mason86479a02007-09-10 19:58:16 -04002646struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002647 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002648#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2649 void (*ctor)(struct kmem_cache *, void *)
2650#else
Chris Mason92fee662007-07-25 12:31:35 -04002651 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002652 unsigned long)
2653#endif
2654 )
Chris Mason92fee662007-07-25 12:31:35 -04002655{
2656 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2657 SLAB_MEM_SPREAD | extra_flags), ctor
2658#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2659 ,NULL
2660#endif
2661 );
2662}
2663
Chris Mason39279cc2007-06-12 06:35:45 -04002664int btrfs_init_cachep(void)
2665{
Chris Mason86479a02007-09-10 19:58:16 -04002666 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002667 sizeof(struct btrfs_inode),
2668 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002669 if (!btrfs_inode_cachep)
2670 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002671 btrfs_trans_handle_cachep =
2672 btrfs_cache_create("btrfs_trans_handle_cache",
2673 sizeof(struct btrfs_trans_handle),
2674 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002675 if (!btrfs_trans_handle_cachep)
2676 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002677 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002678 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002679 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002680 if (!btrfs_transaction_cachep)
2681 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002682 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002683 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002684 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002685 if (!btrfs_path_cachep)
2686 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002687 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002688 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002689 if (!btrfs_bit_radix_cachep)
2690 goto fail;
2691 return 0;
2692fail:
2693 btrfs_destroy_cachep();
2694 return -ENOMEM;
2695}
2696
2697static int btrfs_getattr(struct vfsmount *mnt,
2698 struct dentry *dentry, struct kstat *stat)
2699{
2700 struct inode *inode = dentry->d_inode;
2701 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002702 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002703 return 0;
2704}
2705
2706static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2707 struct inode * new_dir,struct dentry *new_dentry)
2708{
2709 struct btrfs_trans_handle *trans;
2710 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2711 struct inode *new_inode = new_dentry->d_inode;
2712 struct inode *old_inode = old_dentry->d_inode;
2713 struct timespec ctime = CURRENT_TIME;
2714 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002715 int ret;
2716
2717 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2718 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2719 return -ENOTEMPTY;
2720 }
Chris Mason5f39d392007-10-15 16:14:19 -04002721
Chris Mason39279cc2007-06-12 06:35:45 -04002722 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002723 ret = btrfs_check_free_space(root, 1, 0);
2724 if (ret)
2725 goto out_unlock;
2726
Chris Mason39279cc2007-06-12 06:35:45 -04002727 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002728
Chris Mason39279cc2007-06-12 06:35:45 -04002729 btrfs_set_trans_block_group(trans, new_dir);
2730 path = btrfs_alloc_path();
2731 if (!path) {
2732 ret = -ENOMEM;
2733 goto out_fail;
2734 }
2735
2736 old_dentry->d_inode->i_nlink++;
2737 old_dir->i_ctime = old_dir->i_mtime = ctime;
2738 new_dir->i_ctime = new_dir->i_mtime = ctime;
2739 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002740
Chris Mason39279cc2007-06-12 06:35:45 -04002741 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2742 if (ret)
2743 goto out_fail;
2744
2745 if (new_inode) {
2746 new_inode->i_ctime = CURRENT_TIME;
2747 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2748 if (ret)
2749 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002750 }
2751 ret = btrfs_add_link(trans, new_dentry, old_inode);
2752 if (ret)
2753 goto out_fail;
2754
2755out_fail:
2756 btrfs_free_path(path);
2757 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002758out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002759 mutex_unlock(&root->fs_info->fs_mutex);
2760 return ret;
2761}
2762
2763static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2764 const char *symname)
2765{
2766 struct btrfs_trans_handle *trans;
2767 struct btrfs_root *root = BTRFS_I(dir)->root;
2768 struct btrfs_path *path;
2769 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002770 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002771 int err;
2772 int drop_inode = 0;
2773 u64 objectid;
2774 int name_len;
2775 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002776 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002777 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002778 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002779 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002780
2781 name_len = strlen(symname) + 1;
2782 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2783 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002784
Chris Mason39279cc2007-06-12 06:35:45 -04002785 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002786 err = btrfs_check_free_space(root, 1, 0);
2787 if (err)
2788 goto out_fail;
2789
Chris Mason39279cc2007-06-12 06:35:45 -04002790 trans = btrfs_start_transaction(root, 1);
2791 btrfs_set_trans_block_group(trans, dir);
2792
2793 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2794 if (err) {
2795 err = -ENOSPC;
2796 goto out_unlock;
2797 }
2798
2799 inode = btrfs_new_inode(trans, root, objectid,
2800 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2801 err = PTR_ERR(inode);
2802 if (IS_ERR(inode))
2803 goto out_unlock;
2804
2805 btrfs_set_trans_block_group(trans, inode);
2806 err = btrfs_add_nondir(trans, dentry, inode);
2807 if (err)
2808 drop_inode = 1;
2809 else {
2810 inode->i_mapping->a_ops = &btrfs_aops;
2811 inode->i_fop = &btrfs_file_operations;
2812 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002813 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2814 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002815 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002816 }
2817 dir->i_sb->s_dirt = 1;
2818 btrfs_update_inode_block_group(trans, inode);
2819 btrfs_update_inode_block_group(trans, dir);
2820 if (drop_inode)
2821 goto out_unlock;
2822
2823 path = btrfs_alloc_path();
2824 BUG_ON(!path);
2825 key.objectid = inode->i_ino;
2826 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002827 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2828 datasize = btrfs_file_extent_calc_inline_size(name_len);
2829 err = btrfs_insert_empty_item(trans, root, path, &key,
2830 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002831 if (err) {
2832 drop_inode = 1;
2833 goto out_unlock;
2834 }
Chris Mason5f39d392007-10-15 16:14:19 -04002835 leaf = path->nodes[0];
2836 ei = btrfs_item_ptr(leaf, path->slots[0],
2837 struct btrfs_file_extent_item);
2838 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2839 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002840 BTRFS_FILE_EXTENT_INLINE);
2841 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002842 write_extent_buffer(leaf, symname, ptr, name_len);
2843 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002844 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002845
Chris Mason39279cc2007-06-12 06:35:45 -04002846 inode->i_op = &btrfs_symlink_inode_operations;
2847 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2848 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002849 err = btrfs_update_inode(trans, root, inode);
2850 if (err)
2851 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002852
2853out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002854 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002855 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002856out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002857 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002858 if (drop_inode) {
2859 inode_dec_link_count(inode);
2860 iput(inode);
2861 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002862 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002863 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002864 return err;
2865}
Yanfdebe2b2008-01-14 13:26:08 -05002866static int btrfs_permission(struct inode *inode, int mask,
2867 struct nameidata *nd)
2868{
2869 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2870 return -EACCES;
2871 return generic_permission(inode, mask, NULL);
2872}
Chris Mason39279cc2007-06-12 06:35:45 -04002873
2874static struct inode_operations btrfs_dir_inode_operations = {
2875 .lookup = btrfs_lookup,
2876 .create = btrfs_create,
2877 .unlink = btrfs_unlink,
2878 .link = btrfs_link,
2879 .mkdir = btrfs_mkdir,
2880 .rmdir = btrfs_rmdir,
2881 .rename = btrfs_rename,
2882 .symlink = btrfs_symlink,
2883 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002884 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002885 .setxattr = generic_setxattr,
2886 .getxattr = generic_getxattr,
2887 .listxattr = btrfs_listxattr,
2888 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002889 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002890};
Chris Mason39279cc2007-06-12 06:35:45 -04002891static struct inode_operations btrfs_dir_ro_inode_operations = {
2892 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002893 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002894};
Chris Mason39279cc2007-06-12 06:35:45 -04002895static struct file_operations btrfs_dir_file_operations = {
2896 .llseek = generic_file_llseek,
2897 .read = generic_read_dir,
2898 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002899 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002900#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002901 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002902#endif
2903};
2904
Chris Mason07157aa2007-08-30 08:50:51 -04002905static struct extent_map_ops btrfs_extent_map_ops = {
2906 .fill_delalloc = run_delalloc_range,
2907 .writepage_io_hook = btrfs_writepage_io_hook,
2908 .readpage_io_hook = btrfs_readpage_io_hook,
2909 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2910};
2911
Chris Mason39279cc2007-06-12 06:35:45 -04002912static struct address_space_operations btrfs_aops = {
2913 .readpage = btrfs_readpage,
2914 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002915 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002916 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002917 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002918 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002919 .invalidatepage = btrfs_invalidatepage,
2920 .releasepage = btrfs_releasepage,
2921 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002922};
2923
2924static struct address_space_operations btrfs_symlink_aops = {
2925 .readpage = btrfs_readpage,
2926 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002927 .invalidatepage = btrfs_invalidatepage,
2928 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002929};
2930
2931static struct inode_operations btrfs_file_inode_operations = {
2932 .truncate = btrfs_truncate,
2933 .getattr = btrfs_getattr,
2934 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002935 .setxattr = generic_setxattr,
2936 .getxattr = generic_getxattr,
2937 .listxattr = btrfs_listxattr,
2938 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002939 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002940};
Josef Bacik618e21d2007-07-11 10:18:17 -04002941static struct inode_operations btrfs_special_inode_operations = {
2942 .getattr = btrfs_getattr,
2943 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002944 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002945};
Chris Mason39279cc2007-06-12 06:35:45 -04002946static struct inode_operations btrfs_symlink_inode_operations = {
2947 .readlink = generic_readlink,
2948 .follow_link = page_follow_link_light,
2949 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05002950 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002951};