blob: 5003a86510a1f90f81e4f6cbb01be8ee5841d172 [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)
84 thresh = (total * 90) / 100;
85 else
86 thresh = (total * 85) / 100;
87
88 spin_lock(&root->fs_info->delalloc_lock);
89 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
90 ret = -ENOSPC;
91 spin_unlock(&root->fs_info->delalloc_lock);
92 return ret;
93}
94
Chris Masonbe20aa92007-12-17 20:14:01 -050095static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040096{
97 struct btrfs_root *root = BTRFS_I(inode)->root;
98 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -040099 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400100 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500101 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400102 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -0500103 struct btrfs_key ins;
104 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400105
Chris Masonb888db22007-08-27 16:49:44 -0400106 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400107 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500108 btrfs_set_trans_block_group(trans, inode);
109
Chris Masondb945352007-10-15 16:15:53 -0400110 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500111 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400112 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400113 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -0400114
Chris Mason179e29e2007-11-01 11:28:41 -0400115 if (alloc_hint == EXTENT_MAP_INLINE)
116 goto out;
117
Chris Masonc59f8952007-12-17 20:14:04 -0500118 while(num_bytes > 0) {
119 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
120 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
121 root->root_key.objectid,
122 trans->transid,
123 inode->i_ino, start, 0,
124 alloc_hint, (u64)-1, &ins, 1);
125 if (ret) {
126 WARN_ON(1);
127 goto out;
128 }
129 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
130 start, ins.objectid, ins.offset,
131 ins.offset);
132 num_bytes -= cur_alloc_size;
133 alloc_hint = ins.objectid + ins.offset;
134 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400135 }
Chris Masonb888db22007-08-27 16:49:44 -0400136out:
137 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500138 return ret;
139}
140
141static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
142{
143 u64 extent_start;
144 u64 extent_end;
145 u64 bytenr;
146 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500147 u64 loops = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500148 struct btrfs_root *root = BTRFS_I(inode)->root;
149 struct extent_buffer *leaf;
150 int found_type;
151 struct btrfs_path *path;
152 struct btrfs_file_extent_item *item;
153 int ret;
154 int err;
155 struct btrfs_key found_key;
156
157 path = btrfs_alloc_path();
158 BUG_ON(!path);
159again:
160 ret = btrfs_lookup_file_extent(NULL, root, path,
161 inode->i_ino, start, 0);
162 if (ret < 0) {
163 btrfs_free_path(path);
164 return ret;
165 }
166
167 cow_end = end;
168 if (ret != 0) {
169 if (path->slots[0] == 0)
170 goto not_found;
171 path->slots[0]--;
172 }
173
174 leaf = path->nodes[0];
175 item = btrfs_item_ptr(leaf, path->slots[0],
176 struct btrfs_file_extent_item);
177
178 /* are we inside the extent that was found? */
179 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
180 found_type = btrfs_key_type(&found_key);
181 if (found_key.objectid != inode->i_ino ||
182 found_type != BTRFS_EXTENT_DATA_KEY) {
183 goto not_found;
184 }
185
186 found_type = btrfs_file_extent_type(leaf, item);
187 extent_start = found_key.offset;
188 if (found_type == BTRFS_FILE_EXTENT_REG) {
189 extent_end = extent_start +
190 btrfs_file_extent_num_bytes(leaf, item);
191 err = 0;
192
Chris Mason1832a6d2007-12-21 16:27:21 -0500193 if (loops && start != extent_start)
194 goto not_found;
195
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 if (start < extent_start || start >= extent_end)
197 goto not_found;
198
199 cow_end = min(end, extent_end - 1);
200 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
201 if (bytenr == 0)
202 goto not_found;
203
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
205 goto not_found;
206 }
207
208 start = extent_end;
209 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
210 goto not_found;
211 }
212loop:
213 if (start > end) {
214 btrfs_free_path(path);
215 return 0;
216 }
217 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500218 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500219 goto again;
220
221not_found:
222 cow_file_range(inode, start, cow_end);
223 start = cow_end + 1;
224 goto loop;
225}
226
227static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
228{
229 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500230 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 int ret;
232
233 mutex_lock(&root->fs_info->fs_mutex);
234 if (btrfs_test_opt(root, NODATACOW))
235 ret = run_delalloc_nocow(inode, start, end);
236 else
237 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500238
239 spin_lock(&root->fs_info->delalloc_lock);
240 num_bytes = end + 1 - start;
241 if (root->fs_info->delalloc_bytes < num_bytes) {
242 printk("delalloc accounting error total %llu sub %llu\n",
243 root->fs_info->delalloc_bytes, num_bytes);
244 } else {
245 root->fs_info->delalloc_bytes -= num_bytes;
246 }
247 spin_unlock(&root->fs_info->delalloc_lock);
248
Chris Masonb888db22007-08-27 16:49:44 -0400249 mutex_unlock(&root->fs_info->fs_mutex);
250 return ret;
251}
252
Chris Mason07157aa2007-08-30 08:50:51 -0400253int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
254{
255 struct inode *inode = page->mapping->host;
256 struct btrfs_root *root = BTRFS_I(inode)->root;
257 struct btrfs_trans_handle *trans;
258 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500259 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400260 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400261 size_t offset = start - page_start;
262
Chris Masonb6cda9b2007-12-14 15:30:32 -0500263 if (btrfs_test_opt(root, NODATASUM))
264 return 0;
265
Chris Mason07157aa2007-08-30 08:50:51 -0400266 mutex_lock(&root->fs_info->fs_mutex);
267 trans = btrfs_start_transaction(root, 1);
268 btrfs_set_trans_block_group(trans, inode);
269 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400270 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400271 start, kaddr + offset, end - start + 1);
272 kunmap(page);
273 ret = btrfs_end_transaction(trans, root);
274 BUG_ON(ret);
275 mutex_unlock(&root->fs_info->fs_mutex);
276 return ret;
277}
278
279int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
280{
281 int ret = 0;
282 struct inode *inode = page->mapping->host;
283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
285 struct btrfs_csum_item *item;
286 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400287 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400288
Chris Masonb6cda9b2007-12-14 15:30:32 -0500289 if (btrfs_test_opt(root, NODATASUM))
290 return 0;
291
Chris Mason07157aa2007-08-30 08:50:51 -0400292 mutex_lock(&root->fs_info->fs_mutex);
293 path = btrfs_alloc_path();
294 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
295 if (IS_ERR(item)) {
296 ret = PTR_ERR(item);
297 /* a csum that isn't present is a preallocated region. */
298 if (ret == -ENOENT || ret == -EFBIG)
299 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400300 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400301 goto out;
302 }
Chris Masonff79f812007-10-15 16:22:25 -0400303 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
304 BTRFS_CRC32_SIZE);
305 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400306out:
307 if (path)
308 btrfs_free_path(path);
309 mutex_unlock(&root->fs_info->fs_mutex);
310 return ret;
311}
312
313int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
314{
Chris Mason35ebb932007-10-30 16:56:53 -0400315 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400316 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400317 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
318 char *kaddr;
319 u64 private;
320 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400321 struct btrfs_root *root = BTRFS_I(inode)->root;
322 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400323 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400324
Chris Masonb6cda9b2007-12-14 15:30:32 -0500325 if (btrfs_test_opt(root, NODATASUM))
326 return 0;
327
Chris Mason07157aa2007-08-30 08:50:51 -0400328 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400329 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400330 kaddr = kmap_atomic(page, KM_IRQ0);
331 if (ret) {
332 goto zeroit;
333 }
Chris Masonff79f812007-10-15 16:22:25 -0400334 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
335 btrfs_csum_final(csum, (char *)&csum);
336 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400337 goto zeroit;
338 }
339 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400340 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400341 return 0;
342
343zeroit:
344 printk("btrfs csum failed ino %lu off %llu\n",
345 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400346 memset(kaddr + offset, 1, end - start + 1);
347 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400348 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400349 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400350 return 0;
351}
Chris Masonb888db22007-08-27 16:49:44 -0400352
Chris Mason39279cc2007-06-12 06:35:45 -0400353void btrfs_read_locked_inode(struct inode *inode)
354{
355 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400356 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400357 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400358 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400359 struct btrfs_root *root = BTRFS_I(inode)->root;
360 struct btrfs_key location;
361 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400362 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400363 int ret;
364
365 path = btrfs_alloc_path();
366 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400367 mutex_lock(&root->fs_info->fs_mutex);
368
369 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
370 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400371 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400372 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400373
Chris Mason5f39d392007-10-15 16:14:19 -0400374 leaf = path->nodes[0];
375 inode_item = btrfs_item_ptr(leaf, path->slots[0],
376 struct btrfs_inode_item);
377
378 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
379 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
380 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
381 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
382 inode->i_size = btrfs_inode_size(leaf, inode_item);
383
384 tspec = btrfs_inode_atime(inode_item);
385 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
386 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
387
388 tspec = btrfs_inode_mtime(inode_item);
389 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
390 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
391
392 tspec = btrfs_inode_ctime(inode_item);
393 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
394 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
395
396 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
397 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400398 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400399 rdev = btrfs_inode_rdev(leaf, inode_item);
400
401 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400402 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
403 alloc_group_block);
404
Chris Masone52ec0e2007-12-21 16:36:24 -0500405 if (!BTRFS_I(inode)->block_group) {
406 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
407 NULL, 0, 0, 0);
408 }
Chris Mason39279cc2007-06-12 06:35:45 -0400409 btrfs_free_path(path);
410 inode_item = NULL;
411
412 mutex_unlock(&root->fs_info->fs_mutex);
413
414 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400415 case S_IFREG:
416 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400417 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400418 inode->i_fop = &btrfs_file_operations;
419 inode->i_op = &btrfs_file_inode_operations;
420 break;
421 case S_IFDIR:
422 inode->i_fop = &btrfs_dir_file_operations;
423 if (root == root->fs_info->tree_root)
424 inode->i_op = &btrfs_dir_ro_inode_operations;
425 else
426 inode->i_op = &btrfs_dir_inode_operations;
427 break;
428 case S_IFLNK:
429 inode->i_op = &btrfs_symlink_inode_operations;
430 inode->i_mapping->a_ops = &btrfs_symlink_aops;
431 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400432 default:
433 init_special_inode(inode, inode->i_mode, rdev);
434 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400435 }
436 return;
437
438make_bad:
439 btrfs_release_path(root, path);
440 btrfs_free_path(path);
441 mutex_unlock(&root->fs_info->fs_mutex);
442 make_bad_inode(inode);
443}
444
Chris Mason5f39d392007-10-15 16:14:19 -0400445static void fill_inode_item(struct extent_buffer *leaf,
446 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400447 struct inode *inode)
448{
Chris Mason5f39d392007-10-15 16:14:19 -0400449 btrfs_set_inode_uid(leaf, item, inode->i_uid);
450 btrfs_set_inode_gid(leaf, item, inode->i_gid);
451 btrfs_set_inode_size(leaf, item, inode->i_size);
452 btrfs_set_inode_mode(leaf, item, inode->i_mode);
453 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
454
455 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
456 inode->i_atime.tv_sec);
457 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
458 inode->i_atime.tv_nsec);
459
460 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
461 inode->i_mtime.tv_sec);
462 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
463 inode->i_mtime.tv_nsec);
464
465 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
466 inode->i_ctime.tv_sec);
467 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
468 inode->i_ctime.tv_nsec);
469
470 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
471 btrfs_set_inode_generation(leaf, item, inode->i_generation);
472 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
473 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400474 BTRFS_I(inode)->block_group->key.objectid);
475}
476
Chris Masona52d9a82007-08-27 16:49:44 -0400477int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400478 struct btrfs_root *root,
479 struct inode *inode)
480{
481 struct btrfs_inode_item *inode_item;
482 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400483 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400484 int ret;
485
486 path = btrfs_alloc_path();
487 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400488 ret = btrfs_lookup_inode(trans, root, path,
489 &BTRFS_I(inode)->location, 1);
490 if (ret) {
491 if (ret > 0)
492 ret = -ENOENT;
493 goto failed;
494 }
495
Chris Mason5f39d392007-10-15 16:14:19 -0400496 leaf = path->nodes[0];
497 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400498 struct btrfs_inode_item);
499
Chris Mason5f39d392007-10-15 16:14:19 -0400500 fill_inode_item(leaf, inode_item, inode);
501 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400502 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400503 ret = 0;
504failed:
505 btrfs_release_path(root, path);
506 btrfs_free_path(path);
507 return ret;
508}
509
510
511static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
512 struct btrfs_root *root,
513 struct inode *dir,
514 struct dentry *dentry)
515{
516 struct btrfs_path *path;
517 const char *name = dentry->d_name.name;
518 int name_len = dentry->d_name.len;
519 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400520 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400521 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400522 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400523
524 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400525 if (!path) {
526 ret = -ENOMEM;
527 goto err;
528 }
529
Chris Mason39279cc2007-06-12 06:35:45 -0400530 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
531 name, name_len, -1);
532 if (IS_ERR(di)) {
533 ret = PTR_ERR(di);
534 goto err;
535 }
536 if (!di) {
537 ret = -ENOENT;
538 goto err;
539 }
Chris Mason5f39d392007-10-15 16:14:19 -0400540 leaf = path->nodes[0];
541 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400542 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400543 if (ret)
544 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400545 btrfs_release_path(root, path);
546
547 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400548 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400549 if (IS_ERR(di)) {
550 ret = PTR_ERR(di);
551 goto err;
552 }
553 if (!di) {
554 ret = -ENOENT;
555 goto err;
556 }
557 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400558
559 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500560 ret = btrfs_del_inode_ref(trans, root, name, name_len,
561 dentry->d_inode->i_ino,
562 dentry->d_parent->d_inode->i_ino);
563 if (ret) {
564 printk("failed to delete reference to %.*s, "
565 "inode %lu parent %lu\n", name_len, name,
566 dentry->d_inode->i_ino,
567 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500568 }
Chris Mason39279cc2007-06-12 06:35:45 -0400569err:
570 btrfs_free_path(path);
571 if (!ret) {
572 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400573 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400574 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500575#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
576 dentry->d_inode->i_nlink--;
577#else
Chris Mason39279cc2007-06-12 06:35:45 -0400578 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500579#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400580 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400581 dir->i_sb->s_dirt = 1;
582 }
583 return ret;
584}
585
586static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
587{
588 struct btrfs_root *root;
589 struct btrfs_trans_handle *trans;
590 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500591 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400592
593 root = BTRFS_I(dir)->root;
594 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500595
596 ret = btrfs_check_free_space(root, 1, 1);
597 if (ret)
598 goto fail;
599
Chris Mason39279cc2007-06-12 06:35:45 -0400600 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400601
Chris Mason39279cc2007-06-12 06:35:45 -0400602 btrfs_set_trans_block_group(trans, dir);
603 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400604 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400605
Chris Mason39279cc2007-06-12 06:35:45 -0400606 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500607fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400608 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400609 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400610 return ret;
611}
612
613static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
614{
615 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500616 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400617 int ret;
618 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400619 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500620 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400621
Yan134d4512007-10-25 15:49:25 -0400622 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
623 return -ENOTEMPTY;
624
Chris Mason39279cc2007-06-12 06:35:45 -0400625 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500626 ret = btrfs_check_free_space(root, 1, 1);
627 if (ret)
628 goto fail;
629
Chris Mason39279cc2007-06-12 06:35:45 -0400630 trans = btrfs_start_transaction(root, 1);
631 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400632
633 /* now the directory is empty */
634 err = btrfs_unlink_trans(trans, root, dir, dentry);
635 if (!err) {
636 inode->i_size = 0;
637 }
Chris Mason39544012007-12-12 14:38:19 -0500638
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400639 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400640 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500641fail:
Yan134d4512007-10-25 15:49:25 -0400642 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400643 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500644
Chris Mason39279cc2007-06-12 06:35:45 -0400645 if (ret && !err)
646 err = ret;
647 return err;
648}
649
650static int btrfs_free_inode(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root,
652 struct inode *inode)
653{
654 struct btrfs_path *path;
655 int ret;
656
657 clear_inode(inode);
658
659 path = btrfs_alloc_path();
660 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400661 ret = btrfs_lookup_inode(trans, root, path,
662 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400663 if (ret > 0)
664 ret = -ENOENT;
665 if (!ret)
666 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400667 btrfs_free_path(path);
668 return ret;
669}
670
671/*
Chris Mason39279cc2007-06-12 06:35:45 -0400672 * this can truncate away extent items, csum items and directory items.
673 * It starts at a high offset and removes keys until it can't find
674 * any higher than i_size.
675 *
676 * csum items that cross the new i_size are truncated to the new size
677 * as well.
678 */
679static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root,
681 struct inode *inode)
682{
683 int ret;
684 struct btrfs_path *path;
685 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400686 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400687 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400688 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400689 struct btrfs_file_extent_item *fi;
690 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400691 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400692 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500693 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500694 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400695 int found_extent;
696 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400697 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400698
Chris Masona52d9a82007-08-27 16:49:44 -0400699 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400700 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400701 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400702 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400703
Chris Mason39279cc2007-06-12 06:35:45 -0400704 /* FIXME, add redo link to tree so we don't leak on crash */
705 key.objectid = inode->i_ino;
706 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400707 key.type = (u8)-1;
708
Chris Mason39279cc2007-06-12 06:35:45 -0400709 while(1) {
710 btrfs_init_path(path);
711 fi = NULL;
712 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
713 if (ret < 0) {
714 goto error;
715 }
716 if (ret > 0) {
717 BUG_ON(path->slots[0] == 0);
718 path->slots[0]--;
719 }
Chris Mason5f39d392007-10-15 16:14:19 -0400720 leaf = path->nodes[0];
721 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
722 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400723
Chris Mason5f39d392007-10-15 16:14:19 -0400724 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400725 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400726
Chris Mason39279cc2007-06-12 06:35:45 -0400727 if (found_type != BTRFS_CSUM_ITEM_KEY &&
728 found_type != BTRFS_DIR_ITEM_KEY &&
729 found_type != BTRFS_DIR_INDEX_KEY &&
730 found_type != BTRFS_EXTENT_DATA_KEY)
731 break;
732
Chris Mason5f39d392007-10-15 16:14:19 -0400733 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400734 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400735 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400736 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400737 extent_type = btrfs_file_extent_type(leaf, fi);
738 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400739 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400740 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400741 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
742 struct btrfs_item *item = btrfs_item_nr(leaf,
743 path->slots[0]);
744 item_end += btrfs_file_extent_inline_len(leaf,
745 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400746 }
Yan008630c2007-11-07 13:31:09 -0500747 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400748 }
749 if (found_type == BTRFS_CSUM_ITEM_KEY) {
750 ret = btrfs_csum_truncate(trans, root, path,
751 inode->i_size);
752 BUG_ON(ret);
753 }
Yan008630c2007-11-07 13:31:09 -0500754 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400755 if (found_type == BTRFS_DIR_ITEM_KEY) {
756 found_type = BTRFS_INODE_ITEM_KEY;
757 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
758 found_type = BTRFS_CSUM_ITEM_KEY;
759 } else if (found_type) {
760 found_type--;
761 } else {
762 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400763 }
Yana61721d2007-09-17 11:08:38 -0400764 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400765 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400766 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400767 }
Chris Mason5f39d392007-10-15 16:14:19 -0400768 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400769 del_item = 1;
770 else
771 del_item = 0;
772 found_extent = 0;
773
774 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400775 if (found_type != BTRFS_EXTENT_DATA_KEY)
776 goto delete;
777
778 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400779 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400780 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400781 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400782 u64 orig_num_bytes =
783 btrfs_file_extent_num_bytes(leaf, fi);
784 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400785 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400786 btrfs_set_file_extent_num_bytes(leaf, fi,
787 extent_num_bytes);
788 num_dec = (orig_num_bytes -
789 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400790 if (extent_start != 0) {
791 inode->i_blocks -= num_dec;
792 }
Chris Mason5f39d392007-10-15 16:14:19 -0400793 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400794 } else {
Chris Masondb945352007-10-15 16:15:53 -0400795 extent_num_bytes =
796 btrfs_file_extent_disk_num_bytes(leaf,
797 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400798 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400799 num_dec = btrfs_file_extent_num_bytes(leaf,
800 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400801 if (extent_start != 0) {
802 found_extent = 1;
803 inode->i_blocks -= num_dec;
804 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500805 root_gen = btrfs_header_generation(leaf);
806 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400807 }
Chris Mason179e29e2007-11-01 11:28:41 -0400808 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
809 !del_item) {
810 u32 newsize = inode->i_size - found_key.offset;
811 newsize = btrfs_file_extent_calc_inline_size(newsize);
812 ret = btrfs_truncate_item(trans, root, path,
813 newsize, 1);
814 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400815 }
Chris Mason179e29e2007-11-01 11:28:41 -0400816delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400817 if (del_item) {
818 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400819 if (ret)
820 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400821 } else {
822 break;
823 }
824 btrfs_release_path(root, path);
825 if (found_extent) {
826 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500827 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500828 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500829 root_gen, inode->i_ino,
830 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400831 BUG_ON(ret);
832 }
833 }
834 ret = 0;
835error:
836 btrfs_release_path(root, path);
837 btrfs_free_path(path);
838 inode->i_sb->s_dirt = 1;
839 return ret;
840}
841
Chris Masonb888db22007-08-27 16:49:44 -0400842static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400843 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400844{
Chris Mason39279cc2007-06-12 06:35:45 -0400845 char *kaddr;
Chris Masonb888db22007-08-27 16:49:44 -0400846 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500847 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400848 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400849 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500850 u64 existing_delalloc;
851 u64 delalloc_start;
852 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400853
Chris Mason190662b2007-12-18 16:25:45 -0500854 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400855 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400856
Chris Masonb888db22007-08-27 16:49:44 -0400857 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500858 delalloc_start = page_start;
859 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
860 &delalloc_start, page_end,
861 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db22007-08-27 16:49:44 -0400862 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
863 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500864
865 spin_lock(&root->fs_info->delalloc_lock);
866 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
867 spin_unlock(&root->fs_info->delalloc_lock);
868
Chris Masona52d9a82007-08-27 16:49:44 -0400869 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400870 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400871 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
872 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400873 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400874 }
Chris Masonb888db22007-08-27 16:49:44 -0400875 set_page_dirty(page);
876 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400877
Chris Masona52d9a82007-08-27 16:49:44 -0400878 return ret;
879}
880
881/*
882 * taken from block_truncate_page, but does cow as it zeros out
883 * any bytes left in the last page in the file.
884 */
885static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
886{
887 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400888 struct btrfs_root *root = BTRFS_I(inode)->root;
889 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400890 pgoff_t index = from >> PAGE_CACHE_SHIFT;
891 unsigned offset = from & (PAGE_CACHE_SIZE-1);
892 struct page *page;
893 int ret = 0;
894 u64 page_start;
895
896 if ((offset & (blocksize - 1)) == 0)
897 goto out;
898
Chris Masondb945352007-10-15 16:15:53 -0400899 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400900 ret = -ENOMEM;
901 page = grab_cache_page(mapping, index);
902 if (!page)
903 goto out;
904 if (!PageUptodate(page)) {
905 ret = btrfs_readpage(NULL, page);
906 lock_page(page);
907 if (!PageUptodate(page)) {
908 ret = -EIO;
909 goto out;
910 }
911 }
Chris Mason35ebb932007-10-30 16:56:53 -0400912 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400913
Chris Masonb888db22007-08-27 16:49:44 -0400914 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400915
Chris Mason39279cc2007-06-12 06:35:45 -0400916 unlock_page(page);
917 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400918 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400919out:
920 return ret;
921}
922
923static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
924{
925 struct inode *inode = dentry->d_inode;
926 int err;
927
928 err = inode_change_ok(inode, attr);
929 if (err)
930 return err;
931
932 if (S_ISREG(inode->i_mode) &&
933 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
934 struct btrfs_trans_handle *trans;
935 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400936 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
937
Chris Mason5f39d392007-10-15 16:14:19 -0400938 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400939 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400940 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400941 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400942 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400943
944 if (attr->ia_size <= pos)
945 goto out;
946
Chris Mason1832a6d2007-12-21 16:27:21 -0500947 mutex_lock(&root->fs_info->fs_mutex);
948 err = btrfs_check_free_space(root, 1, 0);
949 mutex_unlock(&root->fs_info->fs_mutex);
950 if (err)
951 goto fail;
952
Chris Mason39279cc2007-06-12 06:35:45 -0400953 btrfs_truncate_page(inode->i_mapping, inode->i_size);
954
Chris Mason2bf5a722007-08-30 11:54:02 -0400955 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400956 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400957
958 mutex_lock(&root->fs_info->fs_mutex);
959 trans = btrfs_start_transaction(root, 1);
960 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400961 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400962 pos, pos + hole_size, pos,
963 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400964
Chris Mason179e29e2007-11-01 11:28:41 -0400965 if (alloc_hint != EXTENT_MAP_INLINE) {
966 err = btrfs_insert_file_extent(trans, root,
967 inode->i_ino,
968 pos, 0, 0, hole_size);
969 }
Chris Mason39279cc2007-06-12 06:35:45 -0400970 btrfs_end_transaction(trans, root);
971 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400972 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400973 if (err)
974 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400975 }
976out:
977 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -0500978fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400979 return err;
980}
981void btrfs_delete_inode(struct inode *inode)
982{
983 struct btrfs_trans_handle *trans;
984 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400985 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400986 int ret;
987
988 truncate_inode_pages(&inode->i_data, 0);
989 if (is_bad_inode(inode)) {
990 goto no_delete;
991 }
Chris Mason5f39d392007-10-15 16:14:19 -0400992
Chris Mason39279cc2007-06-12 06:35:45 -0400993 inode->i_size = 0;
994 mutex_lock(&root->fs_info->fs_mutex);
995 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400996
Chris Mason39279cc2007-06-12 06:35:45 -0400997 btrfs_set_trans_block_group(trans, inode);
998 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400999 if (ret)
1000 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001001 ret = btrfs_delete_xattrs(trans, root, inode);
1002 if (ret)
1003 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001004 ret = btrfs_free_inode(trans, root, inode);
1005 if (ret)
1006 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001007 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001008
Chris Mason39279cc2007-06-12 06:35:45 -04001009 btrfs_end_transaction(trans, root);
1010 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001011 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001012 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001013
1014no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001015 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001016 btrfs_end_transaction(trans, root);
1017 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001018 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001019no_delete:
1020 clear_inode(inode);
1021}
1022
1023/*
1024 * this returns the key found in the dir entry in the location pointer.
1025 * If no dir entries were found, location->objectid is 0.
1026 */
1027static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1028 struct btrfs_key *location)
1029{
1030 const char *name = dentry->d_name.name;
1031 int namelen = dentry->d_name.len;
1032 struct btrfs_dir_item *di;
1033 struct btrfs_path *path;
1034 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001035 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001036
Chris Mason39544012007-12-12 14:38:19 -05001037 if (namelen == 1 && strcmp(name, ".") == 0) {
1038 location->objectid = dir->i_ino;
1039 location->type = BTRFS_INODE_ITEM_KEY;
1040 location->offset = 0;
1041 return 0;
1042 }
Chris Mason39279cc2007-06-12 06:35:45 -04001043 path = btrfs_alloc_path();
1044 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001045
Chris Mason7a720532007-12-13 09:06:59 -05001046 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001047 struct btrfs_key key;
1048 struct extent_buffer *leaf;
1049 u32 nritems;
1050 int slot;
1051
1052 key.objectid = dir->i_ino;
1053 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1054 key.offset = 0;
1055 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1056 BUG_ON(ret == 0);
1057 ret = 0;
1058
1059 leaf = path->nodes[0];
1060 slot = path->slots[0];
1061 nritems = btrfs_header_nritems(leaf);
1062 if (slot >= nritems)
1063 goto out_err;
1064
1065 btrfs_item_key_to_cpu(leaf, &key, slot);
1066 if (key.objectid != dir->i_ino ||
1067 key.type != BTRFS_INODE_REF_KEY) {
1068 goto out_err;
1069 }
1070 location->objectid = key.offset;
1071 location->type = BTRFS_INODE_ITEM_KEY;
1072 location->offset = 0;
1073 goto out;
1074 }
1075
Chris Mason39279cc2007-06-12 06:35:45 -04001076 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1077 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001078 if (IS_ERR(di))
1079 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001080 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001081 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001082 }
Chris Mason5f39d392007-10-15 16:14:19 -04001083 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001084out:
Chris Mason39279cc2007-06-12 06:35:45 -04001085 btrfs_free_path(path);
1086 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001087out_err:
1088 location->objectid = 0;
1089 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001090}
1091
1092/*
1093 * when we hit a tree root in a directory, the btrfs part of the inode
1094 * needs to be changed to reflect the root directory of the tree root. This
1095 * is kind of like crossing a mount point.
1096 */
1097static int fixup_tree_root_location(struct btrfs_root *root,
1098 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001099 struct btrfs_root **sub_root,
1100 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001101{
1102 struct btrfs_path *path;
1103 struct btrfs_root_item *ri;
1104
1105 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1106 return 0;
1107 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1108 return 0;
1109
1110 path = btrfs_alloc_path();
1111 BUG_ON(!path);
1112 mutex_lock(&root->fs_info->fs_mutex);
1113
Josef Bacik58176a92007-08-29 15:47:34 -04001114 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1115 dentry->d_name.name,
1116 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001117 if (IS_ERR(*sub_root))
1118 return PTR_ERR(*sub_root);
1119
1120 ri = &(*sub_root)->root_item;
1121 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001122 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1123 location->offset = 0;
1124
1125 btrfs_free_path(path);
1126 mutex_unlock(&root->fs_info->fs_mutex);
1127 return 0;
1128}
1129
1130static int btrfs_init_locked_inode(struct inode *inode, void *p)
1131{
1132 struct btrfs_iget_args *args = p;
1133 inode->i_ino = args->ino;
1134 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001135 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1136 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001137 return 0;
1138}
1139
1140static int btrfs_find_actor(struct inode *inode, void *opaque)
1141{
1142 struct btrfs_iget_args *args = opaque;
1143 return (args->ino == inode->i_ino &&
1144 args->root == BTRFS_I(inode)->root);
1145}
1146
1147struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1148 struct btrfs_root *root)
1149{
1150 struct inode *inode;
1151 struct btrfs_iget_args args;
1152 args.ino = objectid;
1153 args.root = root;
1154
1155 inode = iget5_locked(s, objectid, btrfs_find_actor,
1156 btrfs_init_locked_inode,
1157 (void *)&args);
1158 return inode;
1159}
1160
1161static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1162 struct nameidata *nd)
1163{
1164 struct inode * inode;
1165 struct btrfs_inode *bi = BTRFS_I(dir);
1166 struct btrfs_root *root = bi->root;
1167 struct btrfs_root *sub_root = root;
1168 struct btrfs_key location;
1169 int ret;
1170
1171 if (dentry->d_name.len > BTRFS_NAME_LEN)
1172 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001173
Chris Mason39279cc2007-06-12 06:35:45 -04001174 mutex_lock(&root->fs_info->fs_mutex);
1175 ret = btrfs_inode_by_name(dir, dentry, &location);
1176 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001177
Chris Mason39279cc2007-06-12 06:35:45 -04001178 if (ret < 0)
1179 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001180
Chris Mason39279cc2007-06-12 06:35:45 -04001181 inode = NULL;
1182 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001183 ret = fixup_tree_root_location(root, &location, &sub_root,
1184 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001185 if (ret < 0)
1186 return ERR_PTR(ret);
1187 if (ret > 0)
1188 return ERR_PTR(-ENOENT);
1189 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1190 sub_root);
1191 if (!inode)
1192 return ERR_PTR(-EACCES);
1193 if (inode->i_state & I_NEW) {
1194 /* the inode and parent dir are two different roots */
1195 if (sub_root != root) {
1196 igrab(inode);
1197 sub_root->inode = inode;
1198 }
1199 BTRFS_I(inode)->root = sub_root;
1200 memcpy(&BTRFS_I(inode)->location, &location,
1201 sizeof(location));
1202 btrfs_read_locked_inode(inode);
1203 unlock_new_inode(inode);
1204 }
1205 }
1206 return d_splice_alias(inode, dentry);
1207}
1208
Chris Mason39279cc2007-06-12 06:35:45 -04001209static unsigned char btrfs_filetype_table[] = {
1210 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1211};
1212
1213static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1214{
Chris Mason6da6aba2007-12-18 16:15:09 -05001215 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001216 struct btrfs_root *root = BTRFS_I(inode)->root;
1217 struct btrfs_item *item;
1218 struct btrfs_dir_item *di;
1219 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001220 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001221 struct btrfs_path *path;
1222 int ret;
1223 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001224 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001225 int slot;
1226 int advance;
1227 unsigned char d_type;
1228 int over = 0;
1229 u32 di_cur;
1230 u32 di_total;
1231 u32 di_len;
1232 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001233 char tmp_name[32];
1234 char *name_ptr;
1235 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001236
1237 /* FIXME, use a real flag for deciding about the key type */
1238 if (root->fs_info->tree_root == root)
1239 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001240
Chris Mason39544012007-12-12 14:38:19 -05001241 /* special case for "." */
1242 if (filp->f_pos == 0) {
1243 over = filldir(dirent, ".", 1,
1244 1, inode->i_ino,
1245 DT_DIR);
1246 if (over)
1247 return 0;
1248 filp->f_pos = 1;
1249 }
1250
Chris Mason39279cc2007-06-12 06:35:45 -04001251 mutex_lock(&root->fs_info->fs_mutex);
1252 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001253 path = btrfs_alloc_path();
1254 path->reada = 2;
1255
1256 /* special case for .., just use the back ref */
1257 if (filp->f_pos == 1) {
1258 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1259 key.offset = 0;
1260 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1261 BUG_ON(ret == 0);
1262 leaf = path->nodes[0];
1263 slot = path->slots[0];
1264 nritems = btrfs_header_nritems(leaf);
1265 if (slot >= nritems) {
1266 btrfs_release_path(root, path);
1267 goto read_dir_items;
1268 }
1269 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1270 btrfs_release_path(root, path);
1271 if (found_key.objectid != key.objectid ||
1272 found_key.type != BTRFS_INODE_REF_KEY)
1273 goto read_dir_items;
1274 over = filldir(dirent, "..", 2,
1275 2, found_key.offset, DT_DIR);
1276 if (over)
1277 goto nopos;
1278 filp->f_pos = 2;
1279 }
1280
1281read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001282 btrfs_set_key_type(&key, key_type);
1283 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001284
Chris Mason39279cc2007-06-12 06:35:45 -04001285 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1286 if (ret < 0)
1287 goto err;
1288 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001289 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001290 leaf = path->nodes[0];
1291 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001292 slot = path->slots[0];
1293 if (advance || slot >= nritems) {
1294 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001295 ret = btrfs_next_leaf(root, path);
1296 if (ret)
1297 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001298 leaf = path->nodes[0];
1299 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001300 slot = path->slots[0];
1301 } else {
1302 slot++;
1303 path->slots[0]++;
1304 }
1305 }
1306 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001307 item = btrfs_item_nr(leaf, slot);
1308 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1309
1310 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001311 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001312 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001313 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001314 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001315 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001316
1317 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001318 advance = 1;
1319 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1320 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001321 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001322 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001323 struct btrfs_key location;
1324
1325 name_len = btrfs_dir_name_len(leaf, di);
1326 if (name_len < 32) {
1327 name_ptr = tmp_name;
1328 } else {
1329 name_ptr = kmalloc(name_len, GFP_NOFS);
1330 BUG_ON(!name_ptr);
1331 }
1332 read_extent_buffer(leaf, name_ptr,
1333 (unsigned long)(di + 1), name_len);
1334
1335 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1336 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1337
1338 over = filldir(dirent, name_ptr, name_len,
1339 found_key.offset,
1340 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001341 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001342
1343 if (name_ptr != tmp_name)
1344 kfree(name_ptr);
1345
Chris Mason39279cc2007-06-12 06:35:45 -04001346 if (over)
1347 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001348 di_len = btrfs_dir_name_len(leaf, di) +
1349 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001350 di_cur += di_len;
1351 di = (struct btrfs_dir_item *)((char *)di + di_len);
1352 }
1353 }
1354 filp->f_pos++;
1355nopos:
1356 ret = 0;
1357err:
1358 btrfs_release_path(root, path);
1359 btrfs_free_path(path);
1360 mutex_unlock(&root->fs_info->fs_mutex);
1361 return ret;
1362}
1363
1364int btrfs_write_inode(struct inode *inode, int wait)
1365{
1366 struct btrfs_root *root = BTRFS_I(inode)->root;
1367 struct btrfs_trans_handle *trans;
1368 int ret = 0;
1369
1370 if (wait) {
1371 mutex_lock(&root->fs_info->fs_mutex);
1372 trans = btrfs_start_transaction(root, 1);
1373 btrfs_set_trans_block_group(trans, inode);
1374 ret = btrfs_commit_transaction(trans, root);
1375 mutex_unlock(&root->fs_info->fs_mutex);
1376 }
1377 return ret;
1378}
1379
1380/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001381 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001382 * inode changes. But, it is most likely to find the inode in cache.
1383 * FIXME, needs more benchmarking...there are no reasons other than performance
1384 * to keep or drop this code.
1385 */
1386void btrfs_dirty_inode(struct inode *inode)
1387{
1388 struct btrfs_root *root = BTRFS_I(inode)->root;
1389 struct btrfs_trans_handle *trans;
1390
1391 mutex_lock(&root->fs_info->fs_mutex);
1392 trans = btrfs_start_transaction(root, 1);
1393 btrfs_set_trans_block_group(trans, inode);
1394 btrfs_update_inode(trans, root, inode);
1395 btrfs_end_transaction(trans, root);
1396 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001397}
1398
1399static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1400 struct btrfs_root *root,
1401 u64 objectid,
1402 struct btrfs_block_group_cache *group,
1403 int mode)
1404{
1405 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001406 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001407 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001408 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001409 int ret;
1410 int owner;
1411
Chris Mason5f39d392007-10-15 16:14:19 -04001412 path = btrfs_alloc_path();
1413 BUG_ON(!path);
1414
Chris Mason39279cc2007-06-12 06:35:45 -04001415 inode = new_inode(root->fs_info->sb);
1416 if (!inode)
1417 return ERR_PTR(-ENOMEM);
1418
Chris Masonb888db22007-08-27 16:49:44 -04001419 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1420 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001421 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001422
Chris Mason39279cc2007-06-12 06:35:45 -04001423 if (mode & S_IFDIR)
1424 owner = 0;
1425 else
1426 owner = 1;
1427 group = btrfs_find_block_group(root, group, 0, 0, owner);
1428 BTRFS_I(inode)->block_group = group;
1429
Chris Mason5f39d392007-10-15 16:14:19 -04001430 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1431 if (ret)
1432 goto fail;
1433
Chris Mason39279cc2007-06-12 06:35:45 -04001434 inode->i_uid = current->fsuid;
1435 inode->i_gid = current->fsgid;
1436 inode->i_mode = mode;
1437 inode->i_ino = objectid;
1438 inode->i_blocks = 0;
1439 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001440 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1441 struct btrfs_inode_item);
1442 fill_inode_item(path->nodes[0], inode_item, inode);
1443 btrfs_mark_buffer_dirty(path->nodes[0]);
1444 btrfs_free_path(path);
1445
Chris Mason39279cc2007-06-12 06:35:45 -04001446 location = &BTRFS_I(inode)->location;
1447 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001448 location->offset = 0;
1449 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1450
Chris Mason39279cc2007-06-12 06:35:45 -04001451 insert_inode_hash(inode);
1452 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001453fail:
1454 btrfs_free_path(path);
1455 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001456}
1457
1458static inline u8 btrfs_inode_type(struct inode *inode)
1459{
1460 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1461}
1462
1463static int btrfs_add_link(struct btrfs_trans_handle *trans,
1464 struct dentry *dentry, struct inode *inode)
1465{
1466 int ret;
1467 struct btrfs_key key;
1468 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001469 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001470
Chris Mason39279cc2007-06-12 06:35:45 -04001471 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001472 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1473 key.offset = 0;
1474
1475 ret = btrfs_insert_dir_item(trans, root,
1476 dentry->d_name.name, dentry->d_name.len,
1477 dentry->d_parent->d_inode->i_ino,
1478 &key, btrfs_inode_type(inode));
1479 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001480 ret = btrfs_insert_inode_ref(trans, root,
1481 dentry->d_name.name,
1482 dentry->d_name.len,
1483 inode->i_ino,
1484 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001485 parent_inode = dentry->d_parent->d_inode;
1486 parent_inode->i_size += dentry->d_name.len * 2;
1487 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001488 ret = btrfs_update_inode(trans, root,
1489 dentry->d_parent->d_inode);
1490 }
1491 return ret;
1492}
1493
1494static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1495 struct dentry *dentry, struct inode *inode)
1496{
1497 int err = btrfs_add_link(trans, dentry, inode);
1498 if (!err) {
1499 d_instantiate(dentry, inode);
1500 return 0;
1501 }
1502 if (err > 0)
1503 err = -EEXIST;
1504 return err;
1505}
1506
Josef Bacik618e21d2007-07-11 10:18:17 -04001507static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1508 int mode, dev_t rdev)
1509{
1510 struct btrfs_trans_handle *trans;
1511 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001512 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001513 int err;
1514 int drop_inode = 0;
1515 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001516 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001517
1518 if (!new_valid_dev(rdev))
1519 return -EINVAL;
1520
1521 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001522 err = btrfs_check_free_space(root, 1, 0);
1523 if (err)
1524 goto fail;
1525
Josef Bacik618e21d2007-07-11 10:18:17 -04001526 trans = btrfs_start_transaction(root, 1);
1527 btrfs_set_trans_block_group(trans, dir);
1528
1529 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1530 if (err) {
1531 err = -ENOSPC;
1532 goto out_unlock;
1533 }
1534
1535 inode = btrfs_new_inode(trans, root, objectid,
1536 BTRFS_I(dir)->block_group, mode);
1537 err = PTR_ERR(inode);
1538 if (IS_ERR(inode))
1539 goto out_unlock;
1540
1541 btrfs_set_trans_block_group(trans, inode);
1542 err = btrfs_add_nondir(trans, dentry, inode);
1543 if (err)
1544 drop_inode = 1;
1545 else {
1546 inode->i_op = &btrfs_special_inode_operations;
1547 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001548 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001549 }
1550 dir->i_sb->s_dirt = 1;
1551 btrfs_update_inode_block_group(trans, inode);
1552 btrfs_update_inode_block_group(trans, dir);
1553out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001554 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001555 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001556fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001557 mutex_unlock(&root->fs_info->fs_mutex);
1558
1559 if (drop_inode) {
1560 inode_dec_link_count(inode);
1561 iput(inode);
1562 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001563 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001564 return err;
1565}
1566
Chris Mason39279cc2007-06-12 06:35:45 -04001567static int btrfs_create(struct inode *dir, struct dentry *dentry,
1568 int mode, struct nameidata *nd)
1569{
1570 struct btrfs_trans_handle *trans;
1571 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001572 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001573 int err;
1574 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001575 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001576 u64 objectid;
1577
1578 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001579 err = btrfs_check_free_space(root, 1, 0);
1580 if (err)
1581 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001582 trans = btrfs_start_transaction(root, 1);
1583 btrfs_set_trans_block_group(trans, dir);
1584
1585 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1586 if (err) {
1587 err = -ENOSPC;
1588 goto out_unlock;
1589 }
1590
1591 inode = btrfs_new_inode(trans, root, objectid,
1592 BTRFS_I(dir)->block_group, mode);
1593 err = PTR_ERR(inode);
1594 if (IS_ERR(inode))
1595 goto out_unlock;
1596
1597 btrfs_set_trans_block_group(trans, inode);
1598 err = btrfs_add_nondir(trans, dentry, inode);
1599 if (err)
1600 drop_inode = 1;
1601 else {
1602 inode->i_mapping->a_ops = &btrfs_aops;
1603 inode->i_fop = &btrfs_file_operations;
1604 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001605 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1606 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001607 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001608 }
1609 dir->i_sb->s_dirt = 1;
1610 btrfs_update_inode_block_group(trans, inode);
1611 btrfs_update_inode_block_group(trans, dir);
1612out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001613 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001614 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001615fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001616 mutex_unlock(&root->fs_info->fs_mutex);
1617
1618 if (drop_inode) {
1619 inode_dec_link_count(inode);
1620 iput(inode);
1621 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001622 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001623 return err;
1624}
1625
1626static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1627 struct dentry *dentry)
1628{
1629 struct btrfs_trans_handle *trans;
1630 struct btrfs_root *root = BTRFS_I(dir)->root;
1631 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001632 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001633 int err;
1634 int drop_inode = 0;
1635
1636 if (inode->i_nlink == 0)
1637 return -ENOENT;
1638
Chris Mason6da6aba2007-12-18 16:15:09 -05001639#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1640 inode->i_nlink++;
1641#else
Chris Mason39279cc2007-06-12 06:35:45 -04001642 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001643#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001644 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001645 err = btrfs_check_free_space(root, 1, 0);
1646 if (err)
1647 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001648 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001649
Chris Mason39279cc2007-06-12 06:35:45 -04001650 btrfs_set_trans_block_group(trans, dir);
1651 atomic_inc(&inode->i_count);
1652 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001653
Chris Mason39279cc2007-06-12 06:35:45 -04001654 if (err)
1655 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001656
Chris Mason39279cc2007-06-12 06:35:45 -04001657 dir->i_sb->s_dirt = 1;
1658 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001659 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001660
Chris Mason54aa1f42007-06-22 14:16:25 -04001661 if (err)
1662 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001663
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001664 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001665 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001666fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001667 mutex_unlock(&root->fs_info->fs_mutex);
1668
1669 if (drop_inode) {
1670 inode_dec_link_count(inode);
1671 iput(inode);
1672 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001673 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001674 return err;
1675}
1676
Chris Mason39279cc2007-06-12 06:35:45 -04001677static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1678{
1679 struct inode *inode;
1680 struct btrfs_trans_handle *trans;
1681 struct btrfs_root *root = BTRFS_I(dir)->root;
1682 int err = 0;
1683 int drop_on_err = 0;
1684 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001685 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001686
1687 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001688 err = btrfs_check_free_space(root, 1, 0);
1689 if (err)
1690 goto out_unlock;
1691
Chris Mason39279cc2007-06-12 06:35:45 -04001692 trans = btrfs_start_transaction(root, 1);
1693 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001694
Chris Mason39279cc2007-06-12 06:35:45 -04001695 if (IS_ERR(trans)) {
1696 err = PTR_ERR(trans);
1697 goto out_unlock;
1698 }
1699
1700 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1701 if (err) {
1702 err = -ENOSPC;
1703 goto out_unlock;
1704 }
1705
1706 inode = btrfs_new_inode(trans, root, objectid,
1707 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1708 if (IS_ERR(inode)) {
1709 err = PTR_ERR(inode);
1710 goto out_fail;
1711 }
Chris Mason5f39d392007-10-15 16:14:19 -04001712
Chris Mason39279cc2007-06-12 06:35:45 -04001713 drop_on_err = 1;
1714 inode->i_op = &btrfs_dir_inode_operations;
1715 inode->i_fop = &btrfs_dir_file_operations;
1716 btrfs_set_trans_block_group(trans, inode);
1717
Chris Mason39544012007-12-12 14:38:19 -05001718 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001719 err = btrfs_update_inode(trans, root, inode);
1720 if (err)
1721 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001722
Chris Mason39279cc2007-06-12 06:35:45 -04001723 err = btrfs_add_link(trans, dentry, inode);
1724 if (err)
1725 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001726
Chris Mason39279cc2007-06-12 06:35:45 -04001727 d_instantiate(dentry, inode);
1728 drop_on_err = 0;
1729 dir->i_sb->s_dirt = 1;
1730 btrfs_update_inode_block_group(trans, inode);
1731 btrfs_update_inode_block_group(trans, dir);
1732
1733out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001734 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001735 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001736
Chris Mason39279cc2007-06-12 06:35:45 -04001737out_unlock:
1738 mutex_unlock(&root->fs_info->fs_mutex);
1739 if (drop_on_err)
1740 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001741 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001742 return err;
1743}
1744
Chris Masona52d9a82007-08-27 16:49:44 -04001745struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1746 size_t page_offset, u64 start, u64 end,
1747 int create)
1748{
1749 int ret;
1750 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001751 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001752 u64 extent_start = 0;
1753 u64 extent_end = 0;
1754 u64 objectid = inode->i_ino;
1755 u32 found_type;
1756 int failed_insert = 0;
1757 struct btrfs_path *path;
1758 struct btrfs_root *root = BTRFS_I(inode)->root;
1759 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001760 struct extent_buffer *leaf;
1761 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001762 struct extent_map *em = NULL;
1763 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1764 struct btrfs_trans_handle *trans = NULL;
1765
1766 path = btrfs_alloc_path();
1767 BUG_ON(!path);
1768 mutex_lock(&root->fs_info->fs_mutex);
1769
1770again:
1771 em = lookup_extent_mapping(em_tree, start, end);
1772 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001773 if (em->start > start) {
1774 printk("get_extent start %Lu em start %Lu\n",
1775 start, em->start);
1776 WARN_ON(1);
1777 }
Chris Masona52d9a82007-08-27 16:49:44 -04001778 goto out;
1779 }
1780 if (!em) {
1781 em = alloc_extent_map(GFP_NOFS);
1782 if (!em) {
1783 err = -ENOMEM;
1784 goto out;
1785 }
Chris Mason5f39d392007-10-15 16:14:19 -04001786 em->start = EXTENT_MAP_HOLE;
1787 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001788 }
1789 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001790 ret = btrfs_lookup_file_extent(trans, root, path,
1791 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001792 if (ret < 0) {
1793 err = ret;
1794 goto out;
1795 }
1796
1797 if (ret != 0) {
1798 if (path->slots[0] == 0)
1799 goto not_found;
1800 path->slots[0]--;
1801 }
1802
Chris Mason5f39d392007-10-15 16:14:19 -04001803 leaf = path->nodes[0];
1804 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001805 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001806 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001807 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1808 found_type = btrfs_key_type(&found_key);
1809 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001810 found_type != BTRFS_EXTENT_DATA_KEY) {
1811 goto not_found;
1812 }
1813
Chris Mason5f39d392007-10-15 16:14:19 -04001814 found_type = btrfs_file_extent_type(leaf, item);
1815 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001816 if (found_type == BTRFS_FILE_EXTENT_REG) {
1817 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001818 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001819 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001820 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001821 em->start = start;
1822 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001823 if (end < extent_start)
1824 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001825 em->end = extent_end - 1;
1826 } else {
1827 em->end = end;
1828 }
1829 goto not_found_em;
1830 }
Chris Masondb945352007-10-15 16:15:53 -04001831 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1832 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001833 em->start = extent_start;
1834 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001835 em->block_start = EXTENT_MAP_HOLE;
1836 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001837 goto insert;
1838 }
Chris Masondb945352007-10-15 16:15:53 -04001839 bytenr += btrfs_file_extent_offset(leaf, item);
1840 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001841 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001842 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001843 em->start = extent_start;
1844 em->end = extent_end - 1;
1845 goto insert;
1846 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001847 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001848 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001849 size_t size;
1850 size_t extent_offset;
1851 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001852
Chris Mason5f39d392007-10-15 16:14:19 -04001853 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1854 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001855 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001856 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001857 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001858 em->start = start;
1859 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001860 if (end < extent_start)
1861 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001862 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001863 } else {
1864 em->end = end;
1865 }
1866 goto not_found_em;
1867 }
1868 em->block_start = EXTENT_MAP_INLINE;
1869 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001870
1871 if (!page) {
1872 em->start = extent_start;
1873 em->end = extent_start + size - 1;
1874 goto out;
1875 }
1876
Chris Mason35ebb932007-10-30 16:56:53 -04001877 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001878 extent_start + page_offset;
1879 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1880 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001881 em->start = extent_start + extent_offset;
1882 em->end = (em->start + copy_size -1) |
1883 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001884 map = kmap(page);
1885 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001886 if (create == 0 && !PageUptodate(page)) {
1887 read_extent_buffer(leaf, map + page_offset, ptr,
1888 copy_size);
1889 flush_dcache_page(page);
1890 } else if (create && PageUptodate(page)) {
1891 if (!trans) {
1892 kunmap(page);
1893 free_extent_map(em);
1894 em = NULL;
1895 btrfs_release_path(root, path);
1896 trans = btrfs_start_transaction(root, 1);
1897 goto again;
1898 }
1899 write_extent_buffer(leaf, map + page_offset, ptr,
1900 copy_size);
1901 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001902 }
Chris Masona52d9a82007-08-27 16:49:44 -04001903 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001904 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001905 goto insert;
1906 } else {
1907 printk("unkknown found_type %d\n", found_type);
1908 WARN_ON(1);
1909 }
1910not_found:
1911 em->start = start;
1912 em->end = end;
1913not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001914 em->block_start = EXTENT_MAP_HOLE;
1915 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001916insert:
1917 btrfs_release_path(root, path);
1918 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001919 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001920 err = -EIO;
1921 goto out;
1922 }
1923 ret = add_extent_mapping(em_tree, em);
1924 if (ret == -EEXIST) {
1925 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001926 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001927 if (0 && failed_insert == 1) {
1928 btrfs_drop_extent_cache(inode, start, end);
1929 }
Chris Masona52d9a82007-08-27 16:49:44 -04001930 failed_insert++;
1931 if (failed_insert > 5) {
1932 printk("failing to insert %Lu %Lu\n", start, end);
1933 err = -EIO;
1934 goto out;
1935 }
Chris Masona52d9a82007-08-27 16:49:44 -04001936 goto again;
1937 }
1938 err = 0;
1939out:
1940 btrfs_free_path(path);
1941 if (trans) {
1942 ret = btrfs_end_transaction(trans, root);
1943 if (!err)
1944 err = ret;
1945 }
1946 mutex_unlock(&root->fs_info->fs_mutex);
1947 if (err) {
1948 free_extent_map(em);
1949 WARN_ON(1);
1950 return ERR_PTR(err);
1951 }
1952 return em;
1953}
1954
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001955static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001956{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001957 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001958}
1959
Chris Mason9ebefb182007-06-15 13:50:00 -04001960int btrfs_readpage(struct file *file, struct page *page)
1961{
Chris Masona52d9a82007-08-27 16:49:44 -04001962 struct extent_map_tree *tree;
1963 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1964 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001965}
Chris Mason1832a6d2007-12-21 16:27:21 -05001966
Chris Mason39279cc2007-06-12 06:35:45 -04001967static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1968{
Chris Masona52d9a82007-08-27 16:49:44 -04001969 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001970
1971
1972 if (current->flags & PF_MEMALLOC) {
1973 redirty_page_for_writepage(wbc, page);
1974 unlock_page(page);
1975 return 0;
1976 }
Chris Masona52d9a82007-08-27 16:49:44 -04001977 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1978 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1979}
Chris Mason39279cc2007-06-12 06:35:45 -04001980
Chris Masonb293f022007-11-01 19:45:34 -04001981static int btrfs_writepages(struct address_space *mapping,
1982 struct writeback_control *wbc)
1983{
1984 struct extent_map_tree *tree;
1985 tree = &BTRFS_I(mapping->host)->extent_tree;
1986 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1987}
1988
Chris Mason3ab2fb52007-11-08 10:59:22 -05001989static int
1990btrfs_readpages(struct file *file, struct address_space *mapping,
1991 struct list_head *pages, unsigned nr_pages)
1992{
1993 struct extent_map_tree *tree;
1994 tree = &BTRFS_I(mapping->host)->extent_tree;
1995 return extent_readpages(tree, mapping, pages, nr_pages,
1996 btrfs_get_extent);
1997}
1998
Chris Masona52d9a82007-08-27 16:49:44 -04001999static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2000{
2001 struct extent_map_tree *tree;
2002 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002003
Chris Masona52d9a82007-08-27 16:49:44 -04002004 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2005 ret = try_release_extent_mapping(tree, page);
2006 if (ret == 1) {
2007 ClearPagePrivate(page);
2008 set_page_private(page, 0);
2009 page_cache_release(page);
2010 }
2011 return ret;
2012}
Chris Mason39279cc2007-06-12 06:35:45 -04002013
Chris Masona52d9a82007-08-27 16:49:44 -04002014static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2015{
2016 struct extent_map_tree *tree;
2017
2018 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2019 extent_invalidatepage(tree, page, offset);
2020 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002021}
2022
Chris Mason9ebefb182007-06-15 13:50:00 -04002023/*
2024 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2025 * called from a page fault handler when a page is first dirtied. Hence we must
2026 * be careful to check for EOF conditions here. We set the page up correctly
2027 * for a written page which means we get ENOSPC checking when writing into
2028 * holes and correct delalloc and unwritten extent mapping on filesystems that
2029 * support these features.
2030 *
2031 * We are not allowed to take the i_mutex here so we have to play games to
2032 * protect against truncate races as the page could now be beyond EOF. Because
2033 * vmtruncate() writes the inode size before removing pages, once we have the
2034 * page lock we can determine safely if the page is beyond EOF. If it is not
2035 * beyond EOF, then the page is guaranteed safe against truncation until we
2036 * unlock the page.
2037 */
2038int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2039{
Chris Mason6da6aba2007-12-18 16:15:09 -05002040 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002041 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002042 unsigned long end;
2043 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002044 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002045 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002046
Chris Mason1832a6d2007-12-21 16:27:21 -05002047 mutex_lock(&root->fs_info->fs_mutex);
2048 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002049 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002050 if (ret)
2051 goto out;
2052
2053 ret = -EINVAL;
2054
Chris Mason011410b2007-09-10 19:58:36 -04002055 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04002056 lock_page(page);
2057 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002058 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002059 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002060
Chris Mason9ebefb182007-06-15 13:50:00 -04002061 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002062 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002063 /* page got truncated out from underneath us */
2064 goto out_unlock;
2065 }
2066
2067 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002068 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002069 end = size & ~PAGE_CACHE_MASK;
2070 else
2071 end = PAGE_CACHE_SIZE;
2072
Chris Masonb888db22007-08-27 16:49:44 -04002073 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002074
2075out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04002076 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04002077 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002078out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002079 return ret;
2080}
2081
Chris Mason39279cc2007-06-12 06:35:45 -04002082static void btrfs_truncate(struct inode *inode)
2083{
2084 struct btrfs_root *root = BTRFS_I(inode)->root;
2085 int ret;
2086 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002087 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002088
2089 if (!S_ISREG(inode->i_mode))
2090 return;
2091 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2092 return;
2093
2094 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2095
2096 mutex_lock(&root->fs_info->fs_mutex);
2097 trans = btrfs_start_transaction(root, 1);
2098 btrfs_set_trans_block_group(trans, inode);
2099
2100 /* FIXME, add redo link to tree so we don't leak on crash */
2101 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002102 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002103 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002104
Chris Mason39279cc2007-06-12 06:35:45 -04002105 ret = btrfs_end_transaction(trans, root);
2106 BUG_ON(ret);
2107 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002108 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002109}
2110
Chris Mason39279cc2007-06-12 06:35:45 -04002111static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2112{
2113 struct btrfs_trans_handle *trans;
2114 struct btrfs_key key;
2115 struct btrfs_root_item root_item;
2116 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002117 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002118 struct btrfs_root *new_root;
2119 struct inode *inode;
2120 struct inode *dir;
2121 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002122 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002123 u64 objectid;
2124 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002125 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002126
2127 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002128 ret = btrfs_check_free_space(root, 1, 0);
2129 if (ret)
2130 goto fail_commit;
2131
Chris Mason39279cc2007-06-12 06:35:45 -04002132 trans = btrfs_start_transaction(root, 1);
2133 BUG_ON(!trans);
2134
Chris Mason7bb86312007-12-11 09:25:06 -05002135 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2136 0, &objectid);
2137 if (ret)
2138 goto fail;
2139
2140 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2141 objectid, trans->transid, 0, 0,
2142 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 if (IS_ERR(leaf))
2144 return PTR_ERR(leaf);
2145
2146 btrfs_set_header_nritems(leaf, 0);
2147 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002148 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002150 btrfs_set_header_owner(leaf, objectid);
2151
Chris Mason5f39d392007-10-15 16:14:19 -04002152 write_extent_buffer(leaf, root->fs_info->fsid,
2153 (unsigned long)btrfs_header_fsid(leaf),
2154 BTRFS_FSID_SIZE);
2155 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002156
2157 inode_item = &root_item.inode;
2158 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002159 inode_item->generation = cpu_to_le64(1);
2160 inode_item->size = cpu_to_le64(3);
2161 inode_item->nlink = cpu_to_le32(1);
2162 inode_item->nblocks = cpu_to_le64(1);
2163 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002164
Chris Masondb945352007-10-15 16:15:53 -04002165 btrfs_set_root_bytenr(&root_item, leaf->start);
2166 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002167 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 btrfs_set_root_used(&root_item, 0);
2169
Chris Mason5eda7b52007-06-22 14:16:25 -04002170 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2171 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002172
2173 free_extent_buffer(leaf);
2174 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002175
Chris Mason39279cc2007-06-12 06:35:45 -04002176 btrfs_set_root_dirid(&root_item, new_dirid);
2177
2178 key.objectid = objectid;
2179 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002180 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2181 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2182 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002183 if (ret)
2184 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002185
2186 /*
2187 * insert the directory item
2188 */
2189 key.offset = (u64)-1;
2190 dir = root->fs_info->sb->s_root->d_inode;
2191 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2192 name, namelen, dir->i_ino, &key,
2193 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002194 if (ret)
2195 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002196
Chris Mason39544012007-12-12 14:38:19 -05002197 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2198 name, namelen, objectid,
2199 root->fs_info->sb->s_root->d_inode->i_ino);
2200 if (ret)
2201 goto fail;
2202
Chris Mason39279cc2007-06-12 06:35:45 -04002203 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002204 if (ret)
2205 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002206
Josef Bacik58176a92007-08-29 15:47:34 -04002207 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002208 BUG_ON(!new_root);
2209
2210 trans = btrfs_start_transaction(new_root, 1);
2211 BUG_ON(!trans);
2212
2213 inode = btrfs_new_inode(trans, new_root, new_dirid,
2214 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002215 if (IS_ERR(inode))
2216 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002217 inode->i_op = &btrfs_dir_inode_operations;
2218 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002219 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002220
Chris Mason39544012007-12-12 14:38:19 -05002221 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2222 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002223 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002224 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002225 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002226 if (ret)
2227 goto fail;
2228fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002229 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002230 err = btrfs_commit_transaction(trans, root);
2231 if (err && !ret)
2232 ret = err;
2233fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002234 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002235 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002236 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002237}
2238
2239static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2240{
2241 struct btrfs_trans_handle *trans;
2242 struct btrfs_key key;
2243 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002244 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002245 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002246 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002247 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002248 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002249
2250 if (!root->ref_cows)
2251 return -EINVAL;
2252
Chris Mason011410b2007-09-10 19:58:36 -04002253 down_write(&root->snap_sem);
2254 freeze_bdev(root->fs_info->sb->s_bdev);
2255 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2256
Chris Mason39279cc2007-06-12 06:35:45 -04002257 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002258 ret = btrfs_check_free_space(root, 1, 0);
2259 if (ret)
2260 goto fail_unlock;
2261
Chris Mason39279cc2007-06-12 06:35:45 -04002262 trans = btrfs_start_transaction(root, 1);
2263 BUG_ON(!trans);
2264
2265 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002266 if (ret)
2267 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002268
2269 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2270 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002271 if (ret)
2272 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002273
2274 memcpy(&new_root_item, &root->root_item,
2275 sizeof(new_root_item));
2276
2277 key.objectid = objectid;
2278 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002279 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Masonbe20aa92007-12-17 20:14:01 -05002280
Yan96919752007-12-04 13:20:20 -05002281 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002282 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002283 free_extent_buffer(tmp);
Chris Masonbe20aa92007-12-17 20:14:01 -05002284
2285 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2286
2287 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2288 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
Chris Mason39279cc2007-06-12 06:35:45 -04002289 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2290 &new_root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05002291 free_extent_buffer(tmp);
Chris Mason54aa1f42007-06-22 14:16:25 -04002292 if (ret)
2293 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002294
2295 /*
2296 * insert the directory item
2297 */
2298 key.offset = (u64)-1;
2299 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2300 name, namelen,
2301 root->fs_info->sb->s_root->d_inode->i_ino,
2302 &key, BTRFS_FT_DIR);
2303
Chris Mason54aa1f42007-06-22 14:16:25 -04002304 if (ret)
2305 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002306
Chris Mason39544012007-12-12 14:38:19 -05002307 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2308 name, namelen, objectid,
2309 root->fs_info->sb->s_root->d_inode->i_ino);
2310
2311 if (ret)
2312 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002313fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002314 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002315 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002316
Chris Mason54aa1f42007-06-22 14:16:25 -04002317 if (err && !ret)
2318 ret = err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002319fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002320 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002321 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002322 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002323 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002324}
2325
Chris Masonedbd8d42007-12-21 16:27:24 -05002326unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002327 struct file_ra_state *ra, struct file *file,
2328 pgoff_t offset, pgoff_t last_index)
2329{
2330 pgoff_t req_size;
2331
2332#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2333 req_size = last_index - offset + 1;
2334 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2335 return offset;
2336#else
2337 req_size = min(last_index - offset + 1, (pgoff_t)128);
2338 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2339 return offset + req_size;
2340#endif
2341}
2342
2343int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002344 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002345 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002346 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2347 struct page *page;
2348 unsigned long last_index;
2349 unsigned long ra_index = 0;
2350 u64 page_start;
2351 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002352 u64 delalloc_start;
2353 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002354 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002355 int ret;
2356
2357 mutex_lock(&root->fs_info->fs_mutex);
2358 ret = btrfs_check_free_space(root, inode->i_size, 0);
2359 mutex_unlock(&root->fs_info->fs_mutex);
2360 if (ret)
2361 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002362
2363 mutex_lock(&inode->i_mutex);
2364 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2365 for (i = 0; i <= last_index; i++) {
2366 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002367 ra_index = btrfs_force_ra(inode->i_mapping,
2368 &file->f_ra,
2369 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002370 }
2371 page = grab_cache_page(inode->i_mapping, i);
2372 if (!page)
2373 goto out_unlock;
2374 if (!PageUptodate(page)) {
2375 btrfs_readpage(NULL, page);
2376 lock_page(page);
2377 if (!PageUptodate(page)) {
2378 unlock_page(page);
2379 page_cache_release(page);
2380 goto out_unlock;
2381 }
2382 }
Chris Mason35ebb932007-10-30 16:56:53 -04002383 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002384 page_end = page_start + PAGE_CACHE_SIZE - 1;
2385
2386 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002387 delalloc_start = page_start;
2388 existing_delalloc =
2389 count_range_bits(&BTRFS_I(inode)->extent_tree,
2390 &delalloc_start, page_end,
2391 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002392 set_extent_delalloc(em_tree, page_start,
2393 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002394
2395 spin_lock(&root->fs_info->delalloc_lock);
2396 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2397 existing_delalloc;
2398 spin_unlock(&root->fs_info->delalloc_lock);
2399
Chris Mason86479a02007-09-10 19:58:16 -04002400 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2401 set_page_dirty(page);
2402 unlock_page(page);
2403 page_cache_release(page);
2404 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2405 }
2406
2407out_unlock:
2408 mutex_unlock(&inode->i_mutex);
2409 return 0;
2410}
2411
Chris Masonedbd8d42007-12-21 16:27:24 -05002412static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2413{
2414 u64 new_size;
2415 u64 old_size;
2416 struct btrfs_ioctl_vol_args *vol_args;
2417 struct btrfs_trans_handle *trans;
2418 char *sizestr;
2419 int ret = 0;
2420 int namelen;
2421 int mod = 0;
2422
2423 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2424
2425 if (!vol_args)
2426 return -ENOMEM;
2427
2428 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2429 ret = -EFAULT;
2430 goto out;
2431 }
2432 namelen = strlen(vol_args->name);
2433 if (namelen > BTRFS_VOL_NAME_MAX) {
2434 ret = -EINVAL;
2435 goto out;
2436 }
2437
2438 sizestr = vol_args->name;
2439 if (!strcmp(sizestr, "max"))
2440 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2441 else {
2442 if (sizestr[0] == '-') {
2443 mod = -1;
2444 sizestr++;
2445 } else if (sizestr[0] == '+') {
2446 mod = 1;
2447 sizestr++;
2448 }
2449 new_size = btrfs_parse_size(sizestr);
2450 if (new_size == 0) {
2451 ret = -EINVAL;
2452 goto out;
2453 }
2454 }
2455
2456 mutex_lock(&root->fs_info->fs_mutex);
2457 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2458
2459 if (mod < 0) {
2460 if (new_size > old_size) {
2461 ret = -EINVAL;
2462 goto out_unlock;
2463 }
2464 new_size = old_size - new_size;
2465 } else if (mod > 0) {
2466 new_size = old_size + new_size;
2467 }
2468
2469 if (new_size < 256 * 1024 * 1024) {
2470 ret = -EINVAL;
2471 goto out_unlock;
2472 }
2473 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2474 ret = -EFBIG;
2475 goto out_unlock;
2476 }
2477 new_size = (new_size / root->sectorsize) * root->sectorsize;
2478
2479printk("new size is %Lu\n", new_size);
2480 if (new_size > old_size) {
2481 trans = btrfs_start_transaction(root, 1);
2482 ret = btrfs_grow_extent_tree(trans, root, new_size);
2483 btrfs_commit_transaction(trans, root);
2484 } else {
2485 ret = btrfs_shrink_extent_tree(root, new_size);
2486 }
2487
2488out_unlock:
2489 mutex_unlock(&root->fs_info->fs_mutex);
2490out:
2491 kfree(vol_args);
2492 return ret;
2493}
2494
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002495static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2496{
Chris Mason4aec2b52007-12-18 16:25:45 -05002497 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002498 struct btrfs_dir_item *di;
2499 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002500 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002501 int namelen;
2502 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002503
Chris Mason4aec2b52007-12-18 16:25:45 -05002504 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002505
Chris Mason4aec2b52007-12-18 16:25:45 -05002506 if (!vol_args)
2507 return -ENOMEM;
2508
2509 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2510 ret = -EFAULT;
2511 goto out;
2512 }
2513
2514 namelen = strlen(vol_args->name);
2515 if (namelen > BTRFS_VOL_NAME_MAX) {
2516 ret = -EINVAL;
2517 goto out;
2518 }
2519 if (strchr(vol_args->name, '/')) {
2520 ret = -EINVAL;
2521 goto out;
2522 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002523
2524 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002525 if (!path) {
2526 ret = -ENOMEM;
2527 goto out;
2528 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002529
2530 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2531 mutex_lock(&root->fs_info->fs_mutex);
2532 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2533 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002534 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002535 mutex_unlock(&root->fs_info->fs_mutex);
2536 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002537
2538 if (di && !IS_ERR(di)) {
2539 ret = -EEXIST;
2540 goto out;
2541 }
2542
2543 if (IS_ERR(di)) {
2544 ret = PTR_ERR(di);
2545 goto out;
2546 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002547
2548 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002549 ret = create_subvol(root, vol_args->name, namelen);
2550 else
2551 ret = create_snapshot(root, vol_args->name, namelen);
2552out:
2553 kfree(vol_args);
2554 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002555}
2556
2557static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002558{
Chris Mason6da6aba2007-12-18 16:15:09 -05002559 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002560 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002561
2562 switch (inode->i_mode & S_IFMT) {
2563 case S_IFDIR:
2564 mutex_lock(&root->fs_info->fs_mutex);
2565 btrfs_defrag_root(root, 0);
2566 btrfs_defrag_root(root->fs_info->extent_root, 0);
2567 mutex_unlock(&root->fs_info->fs_mutex);
2568 break;
2569 case S_IFREG:
2570 btrfs_defrag_file(file);
2571 break;
2572 }
2573
2574 return 0;
2575}
2576
2577long btrfs_ioctl(struct file *file, unsigned int
2578 cmd, unsigned long arg)
2579{
Chris Mason6da6aba2007-12-18 16:15:09 -05002580 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002581
2582 switch (cmd) {
2583 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002584 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002585 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002586 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002587 case BTRFS_IOC_RESIZE:
2588 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002589 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002590
2591 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002592}
2593
Chris Mason39279cc2007-06-12 06:35:45 -04002594/*
2595 * Called inside transaction, so use GFP_NOFS
2596 */
2597struct inode *btrfs_alloc_inode(struct super_block *sb)
2598{
2599 struct btrfs_inode *ei;
2600
2601 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2602 if (!ei)
2603 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002604 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002605 return &ei->vfs_inode;
2606}
2607
2608void btrfs_destroy_inode(struct inode *inode)
2609{
2610 WARN_ON(!list_empty(&inode->i_dentry));
2611 WARN_ON(inode->i_data.nrpages);
2612
2613 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2614}
2615
Chris Mason44ec0b72007-10-29 10:55:05 -04002616#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2617static void init_once(struct kmem_cache * cachep, void *foo)
2618#else
Chris Mason39279cc2007-06-12 06:35:45 -04002619static void init_once(void * foo, struct kmem_cache * cachep,
2620 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002621#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002622{
2623 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2624
2625 inode_init_once(&ei->vfs_inode);
2626}
2627
2628void btrfs_destroy_cachep(void)
2629{
2630 if (btrfs_inode_cachep)
2631 kmem_cache_destroy(btrfs_inode_cachep);
2632 if (btrfs_trans_handle_cachep)
2633 kmem_cache_destroy(btrfs_trans_handle_cachep);
2634 if (btrfs_transaction_cachep)
2635 kmem_cache_destroy(btrfs_transaction_cachep);
2636 if (btrfs_bit_radix_cachep)
2637 kmem_cache_destroy(btrfs_bit_radix_cachep);
2638 if (btrfs_path_cachep)
2639 kmem_cache_destroy(btrfs_path_cachep);
2640}
2641
Chris Mason86479a02007-09-10 19:58:16 -04002642struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002643 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002644#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2645 void (*ctor)(struct kmem_cache *, void *)
2646#else
Chris Mason92fee662007-07-25 12:31:35 -04002647 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002648 unsigned long)
2649#endif
2650 )
Chris Mason92fee662007-07-25 12:31:35 -04002651{
2652 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2653 SLAB_MEM_SPREAD | extra_flags), ctor
2654#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2655 ,NULL
2656#endif
2657 );
2658}
2659
Chris Mason39279cc2007-06-12 06:35:45 -04002660int btrfs_init_cachep(void)
2661{
Chris Mason86479a02007-09-10 19:58:16 -04002662 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002663 sizeof(struct btrfs_inode),
2664 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002665 if (!btrfs_inode_cachep)
2666 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002667 btrfs_trans_handle_cachep =
2668 btrfs_cache_create("btrfs_trans_handle_cache",
2669 sizeof(struct btrfs_trans_handle),
2670 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002671 if (!btrfs_trans_handle_cachep)
2672 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002673 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002674 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002675 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002676 if (!btrfs_transaction_cachep)
2677 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002678 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002679 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002680 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002681 if (!btrfs_path_cachep)
2682 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002683 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002684 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002685 if (!btrfs_bit_radix_cachep)
2686 goto fail;
2687 return 0;
2688fail:
2689 btrfs_destroy_cachep();
2690 return -ENOMEM;
2691}
2692
2693static int btrfs_getattr(struct vfsmount *mnt,
2694 struct dentry *dentry, struct kstat *stat)
2695{
2696 struct inode *inode = dentry->d_inode;
2697 generic_fillattr(inode, stat);
2698 stat->blksize = 256 * 1024;
2699 return 0;
2700}
2701
2702static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2703 struct inode * new_dir,struct dentry *new_dentry)
2704{
2705 struct btrfs_trans_handle *trans;
2706 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2707 struct inode *new_inode = new_dentry->d_inode;
2708 struct inode *old_inode = old_dentry->d_inode;
2709 struct timespec ctime = CURRENT_TIME;
2710 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002711 int ret;
2712
2713 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2714 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2715 return -ENOTEMPTY;
2716 }
Chris Mason5f39d392007-10-15 16:14:19 -04002717
Chris Mason39279cc2007-06-12 06:35:45 -04002718 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002719 ret = btrfs_check_free_space(root, 1, 0);
2720 if (ret)
2721 goto out_unlock;
2722
Chris Mason39279cc2007-06-12 06:35:45 -04002723 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002724
Chris Mason39279cc2007-06-12 06:35:45 -04002725 btrfs_set_trans_block_group(trans, new_dir);
2726 path = btrfs_alloc_path();
2727 if (!path) {
2728 ret = -ENOMEM;
2729 goto out_fail;
2730 }
2731
2732 old_dentry->d_inode->i_nlink++;
2733 old_dir->i_ctime = old_dir->i_mtime = ctime;
2734 new_dir->i_ctime = new_dir->i_mtime = ctime;
2735 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002736
Chris Mason39279cc2007-06-12 06:35:45 -04002737 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2738 if (ret)
2739 goto out_fail;
2740
2741 if (new_inode) {
2742 new_inode->i_ctime = CURRENT_TIME;
2743 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2744 if (ret)
2745 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002746 }
2747 ret = btrfs_add_link(trans, new_dentry, old_inode);
2748 if (ret)
2749 goto out_fail;
2750
2751out_fail:
2752 btrfs_free_path(path);
2753 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002754out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002755 mutex_unlock(&root->fs_info->fs_mutex);
2756 return ret;
2757}
2758
2759static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2760 const char *symname)
2761{
2762 struct btrfs_trans_handle *trans;
2763 struct btrfs_root *root = BTRFS_I(dir)->root;
2764 struct btrfs_path *path;
2765 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002766 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002767 int err;
2768 int drop_inode = 0;
2769 u64 objectid;
2770 int name_len;
2771 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002772 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002773 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002774 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002775 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002776
2777 name_len = strlen(symname) + 1;
2778 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2779 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002780
Chris Mason39279cc2007-06-12 06:35:45 -04002781 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002782 err = btrfs_check_free_space(root, 1, 0);
2783 if (err)
2784 goto out_fail;
2785
Chris Mason39279cc2007-06-12 06:35:45 -04002786 trans = btrfs_start_transaction(root, 1);
2787 btrfs_set_trans_block_group(trans, dir);
2788
2789 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2790 if (err) {
2791 err = -ENOSPC;
2792 goto out_unlock;
2793 }
2794
2795 inode = btrfs_new_inode(trans, root, objectid,
2796 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2797 err = PTR_ERR(inode);
2798 if (IS_ERR(inode))
2799 goto out_unlock;
2800
2801 btrfs_set_trans_block_group(trans, inode);
2802 err = btrfs_add_nondir(trans, dentry, inode);
2803 if (err)
2804 drop_inode = 1;
2805 else {
2806 inode->i_mapping->a_ops = &btrfs_aops;
2807 inode->i_fop = &btrfs_file_operations;
2808 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002809 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2810 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002811 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002812 }
2813 dir->i_sb->s_dirt = 1;
2814 btrfs_update_inode_block_group(trans, inode);
2815 btrfs_update_inode_block_group(trans, dir);
2816 if (drop_inode)
2817 goto out_unlock;
2818
2819 path = btrfs_alloc_path();
2820 BUG_ON(!path);
2821 key.objectid = inode->i_ino;
2822 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002823 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2824 datasize = btrfs_file_extent_calc_inline_size(name_len);
2825 err = btrfs_insert_empty_item(trans, root, path, &key,
2826 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002827 if (err) {
2828 drop_inode = 1;
2829 goto out_unlock;
2830 }
Chris Mason5f39d392007-10-15 16:14:19 -04002831 leaf = path->nodes[0];
2832 ei = btrfs_item_ptr(leaf, path->slots[0],
2833 struct btrfs_file_extent_item);
2834 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2835 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002836 BTRFS_FILE_EXTENT_INLINE);
2837 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002838 write_extent_buffer(leaf, symname, ptr, name_len);
2839 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002840 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002841
Chris Mason39279cc2007-06-12 06:35:45 -04002842 inode->i_op = &btrfs_symlink_inode_operations;
2843 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2844 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002845 err = btrfs_update_inode(trans, root, inode);
2846 if (err)
2847 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002848
2849out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002850 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002851 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002852out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002853 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002854 if (drop_inode) {
2855 inode_dec_link_count(inode);
2856 iput(inode);
2857 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002858 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002859 return err;
2860}
2861
2862static struct inode_operations btrfs_dir_inode_operations = {
2863 .lookup = btrfs_lookup,
2864 .create = btrfs_create,
2865 .unlink = btrfs_unlink,
2866 .link = btrfs_link,
2867 .mkdir = btrfs_mkdir,
2868 .rmdir = btrfs_rmdir,
2869 .rename = btrfs_rename,
2870 .symlink = btrfs_symlink,
2871 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002872 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002873 .setxattr = generic_setxattr,
2874 .getxattr = generic_getxattr,
2875 .listxattr = btrfs_listxattr,
2876 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002877};
2878
2879static struct inode_operations btrfs_dir_ro_inode_operations = {
2880 .lookup = btrfs_lookup,
2881};
2882
2883static struct file_operations btrfs_dir_file_operations = {
2884 .llseek = generic_file_llseek,
2885 .read = generic_read_dir,
2886 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002887 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002888#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002889 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002890#endif
2891};
2892
Chris Mason07157aa2007-08-30 08:50:51 -04002893static struct extent_map_ops btrfs_extent_map_ops = {
2894 .fill_delalloc = run_delalloc_range,
2895 .writepage_io_hook = btrfs_writepage_io_hook,
2896 .readpage_io_hook = btrfs_readpage_io_hook,
2897 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2898};
2899
Chris Mason39279cc2007-06-12 06:35:45 -04002900static struct address_space_operations btrfs_aops = {
2901 .readpage = btrfs_readpage,
2902 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002903 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002904 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002905 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002906 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002907 .invalidatepage = btrfs_invalidatepage,
2908 .releasepage = btrfs_releasepage,
2909 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002910};
2911
2912static struct address_space_operations btrfs_symlink_aops = {
2913 .readpage = btrfs_readpage,
2914 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002915 .invalidatepage = btrfs_invalidatepage,
2916 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002917};
2918
2919static struct inode_operations btrfs_file_inode_operations = {
2920 .truncate = btrfs_truncate,
2921 .getattr = btrfs_getattr,
2922 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002923 .setxattr = generic_setxattr,
2924 .getxattr = generic_getxattr,
2925 .listxattr = btrfs_listxattr,
2926 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002927};
2928
Josef Bacik618e21d2007-07-11 10:18:17 -04002929static struct inode_operations btrfs_special_inode_operations = {
2930 .getattr = btrfs_getattr,
2931 .setattr = btrfs_setattr,
2932};
2933
Chris Mason39279cc2007-06-12 06:35:45 -04002934static struct inode_operations btrfs_symlink_inode_operations = {
2935 .readlink = generic_readlink,
2936 .follow_link = page_follow_link_light,
2937 .put_link = page_put_link,
2938};