blob: 6d0cd9a7a615329bef40230753090dd5a64eb9e4 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Mason1832a6d2007-12-21 16:27:21 -050075int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
76 int for_del)
77{
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
80 u64 thresh;
81 int ret = 0;
82
83 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050084 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 else
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 85;
87
88 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050089
90 spin_lock(&root->fs_info->delalloc_lock);
91 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
92 ret = -ENOSPC;
93 spin_unlock(&root->fs_info->delalloc_lock);
94 return ret;
95}
96
Chris Masonbe20aa92007-12-17 20:14:01 -050097static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040098{
99 struct btrfs_root *root = BTRFS_I(inode)->root;
100 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400101 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400102 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500103 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -0500105 struct btrfs_key ins;
106 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400107
Chris Masonb888db22007-08-27 16:49:44 -0400108 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400109 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500110 btrfs_set_trans_block_group(trans, inode);
111
Chris Masondb945352007-10-15 16:15:53 -0400112 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500113 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400114 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400115 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -0400116
Chris Mason179e29e2007-11-01 11:28:41 -0400117 if (alloc_hint == EXTENT_MAP_INLINE)
118 goto out;
119
Chris Masonc59f8952007-12-17 20:14:04 -0500120 while(num_bytes > 0) {
121 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
122 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
123 root->root_key.objectid,
124 trans->transid,
125 inode->i_ino, start, 0,
126 alloc_hint, (u64)-1, &ins, 1);
127 if (ret) {
128 WARN_ON(1);
129 goto out;
130 }
131 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
132 start, ins.objectid, ins.offset,
133 ins.offset);
134 num_bytes -= cur_alloc_size;
135 alloc_hint = ins.objectid + ins.offset;
136 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400137 }
Chris Masonb888db22007-08-27 16:49:44 -0400138out:
139 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500140 return ret;
141}
142
143static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
144{
145 u64 extent_start;
146 u64 extent_end;
147 u64 bytenr;
148 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500149 u64 loops = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500150 struct btrfs_root *root = BTRFS_I(inode)->root;
151 struct extent_buffer *leaf;
152 int found_type;
153 struct btrfs_path *path;
154 struct btrfs_file_extent_item *item;
155 int ret;
156 int err;
157 struct btrfs_key found_key;
158
159 path = btrfs_alloc_path();
160 BUG_ON(!path);
161again:
162 ret = btrfs_lookup_file_extent(NULL, root, path,
163 inode->i_ino, start, 0);
164 if (ret < 0) {
165 btrfs_free_path(path);
166 return ret;
167 }
168
169 cow_end = end;
170 if (ret != 0) {
171 if (path->slots[0] == 0)
172 goto not_found;
173 path->slots[0]--;
174 }
175
176 leaf = path->nodes[0];
177 item = btrfs_item_ptr(leaf, path->slots[0],
178 struct btrfs_file_extent_item);
179
180 /* are we inside the extent that was found? */
181 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
182 found_type = btrfs_key_type(&found_key);
183 if (found_key.objectid != inode->i_ino ||
184 found_type != BTRFS_EXTENT_DATA_KEY) {
185 goto not_found;
186 }
187
188 found_type = btrfs_file_extent_type(leaf, item);
189 extent_start = found_key.offset;
190 if (found_type == BTRFS_FILE_EXTENT_REG) {
191 extent_end = extent_start +
192 btrfs_file_extent_num_bytes(leaf, item);
193 err = 0;
194
Chris Mason1832a6d2007-12-21 16:27:21 -0500195 if (loops && start != extent_start)
196 goto not_found;
197
Chris Masonbe20aa92007-12-17 20:14:01 -0500198 if (start < extent_start || start >= extent_end)
199 goto not_found;
200
201 cow_end = min(end, extent_end - 1);
202 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
203 if (bytenr == 0)
204 goto not_found;
205
Chris Masonbe20aa92007-12-17 20:14:01 -0500206 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
207 goto not_found;
208 }
209
210 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500211 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500212 goto not_found;
213 }
214loop:
215 if (start > end) {
216 btrfs_free_path(path);
217 return 0;
218 }
219 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500220 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 goto again;
222
223not_found:
224 cow_file_range(inode, start, cow_end);
225 start = cow_end + 1;
226 goto loop;
227}
228
229static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
230{
231 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500232 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500233 int ret;
234
235 mutex_lock(&root->fs_info->fs_mutex);
236 if (btrfs_test_opt(root, NODATACOW))
237 ret = run_delalloc_nocow(inode, start, end);
238 else
239 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500240
241 spin_lock(&root->fs_info->delalloc_lock);
242 num_bytes = end + 1 - start;
243 if (root->fs_info->delalloc_bytes < num_bytes) {
244 printk("delalloc accounting error total %llu sub %llu\n",
245 root->fs_info->delalloc_bytes, num_bytes);
246 } else {
247 root->fs_info->delalloc_bytes -= num_bytes;
248 }
249 spin_unlock(&root->fs_info->delalloc_lock);
250
Chris Masonb888db22007-08-27 16:49:44 -0400251 mutex_unlock(&root->fs_info->fs_mutex);
252 return ret;
253}
254
Chris Mason07157aa2007-08-30 08:50:51 -0400255int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
256{
257 struct inode *inode = page->mapping->host;
258 struct btrfs_root *root = BTRFS_I(inode)->root;
259 struct btrfs_trans_handle *trans;
260 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500261 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400262 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400263 size_t offset = start - page_start;
264
Chris Masonb6cda9b2007-12-14 15:30:32 -0500265 if (btrfs_test_opt(root, NODATASUM))
266 return 0;
267
Chris Mason07157aa2007-08-30 08:50:51 -0400268 mutex_lock(&root->fs_info->fs_mutex);
269 trans = btrfs_start_transaction(root, 1);
270 btrfs_set_trans_block_group(trans, inode);
271 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400272 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400273 start, kaddr + offset, end - start + 1);
274 kunmap(page);
275 ret = btrfs_end_transaction(trans, root);
276 BUG_ON(ret);
277 mutex_unlock(&root->fs_info->fs_mutex);
278 return ret;
279}
280
281int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
282{
283 int ret = 0;
284 struct inode *inode = page->mapping->host;
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
287 struct btrfs_csum_item *item;
288 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400289 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400290
Chris Masonb6cda9b2007-12-14 15:30:32 -0500291 if (btrfs_test_opt(root, NODATASUM))
292 return 0;
293
Chris Mason07157aa2007-08-30 08:50:51 -0400294 mutex_lock(&root->fs_info->fs_mutex);
295 path = btrfs_alloc_path();
296 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
297 if (IS_ERR(item)) {
298 ret = PTR_ERR(item);
299 /* a csum that isn't present is a preallocated region. */
300 if (ret == -ENOENT || ret == -EFBIG)
301 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400302 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400303 goto out;
304 }
Chris Masonff79f812007-10-15 16:22:25 -0400305 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
306 BTRFS_CRC32_SIZE);
307 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400308out:
309 if (path)
310 btrfs_free_path(path);
311 mutex_unlock(&root->fs_info->fs_mutex);
312 return ret;
313}
314
315int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
316{
Chris Mason35ebb932007-10-30 16:56:53 -0400317 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400318 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400319 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
320 char *kaddr;
321 u64 private;
322 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400323 struct btrfs_root *root = BTRFS_I(inode)->root;
324 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400325 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400326
Chris Masonb6cda9b2007-12-14 15:30:32 -0500327 if (btrfs_test_opt(root, NODATASUM))
328 return 0;
329
Chris Mason07157aa2007-08-30 08:50:51 -0400330 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400331 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400332 kaddr = kmap_atomic(page, KM_IRQ0);
333 if (ret) {
334 goto zeroit;
335 }
Chris Masonff79f812007-10-15 16:22:25 -0400336 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
337 btrfs_csum_final(csum, (char *)&csum);
338 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400339 goto zeroit;
340 }
341 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400342 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400343 return 0;
344
345zeroit:
346 printk("btrfs csum failed ino %lu off %llu\n",
347 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400348 memset(kaddr + offset, 1, end - start + 1);
349 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400350 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400351 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400352 return 0;
353}
Chris Masonb888db22007-08-27 16:49:44 -0400354
Chris Mason39279cc2007-06-12 06:35:45 -0400355void btrfs_read_locked_inode(struct inode *inode)
356{
357 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400358 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400359 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400360 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400361 struct btrfs_root *root = BTRFS_I(inode)->root;
362 struct btrfs_key location;
363 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400364 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400365 int ret;
366
367 path = btrfs_alloc_path();
368 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400369 mutex_lock(&root->fs_info->fs_mutex);
370
371 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
372 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400373 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400374 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400375
Chris Mason5f39d392007-10-15 16:14:19 -0400376 leaf = path->nodes[0];
377 inode_item = btrfs_item_ptr(leaf, path->slots[0],
378 struct btrfs_inode_item);
379
380 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
381 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
382 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
383 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
384 inode->i_size = btrfs_inode_size(leaf, inode_item);
385
386 tspec = btrfs_inode_atime(inode_item);
387 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
388 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
389
390 tspec = btrfs_inode_mtime(inode_item);
391 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
392 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
393
394 tspec = btrfs_inode_ctime(inode_item);
395 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
396 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
397
398 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
399 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400400 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400401 rdev = btrfs_inode_rdev(leaf, inode_item);
402
403 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400404 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
405 alloc_group_block);
406
Chris Masone52ec0e2007-12-21 16:36:24 -0500407 if (!BTRFS_I(inode)->block_group) {
408 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
409 NULL, 0, 0, 0);
410 }
Chris Mason39279cc2007-06-12 06:35:45 -0400411 btrfs_free_path(path);
412 inode_item = NULL;
413
414 mutex_unlock(&root->fs_info->fs_mutex);
415
416 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400417 case S_IFREG:
418 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400419 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400420 inode->i_fop = &btrfs_file_operations;
421 inode->i_op = &btrfs_file_inode_operations;
422 break;
423 case S_IFDIR:
424 inode->i_fop = &btrfs_dir_file_operations;
425 if (root == root->fs_info->tree_root)
426 inode->i_op = &btrfs_dir_ro_inode_operations;
427 else
428 inode->i_op = &btrfs_dir_inode_operations;
429 break;
430 case S_IFLNK:
431 inode->i_op = &btrfs_symlink_inode_operations;
432 inode->i_mapping->a_ops = &btrfs_symlink_aops;
433 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400434 default:
435 init_special_inode(inode, inode->i_mode, rdev);
436 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400437 }
438 return;
439
440make_bad:
441 btrfs_release_path(root, path);
442 btrfs_free_path(path);
443 mutex_unlock(&root->fs_info->fs_mutex);
444 make_bad_inode(inode);
445}
446
Chris Mason5f39d392007-10-15 16:14:19 -0400447static void fill_inode_item(struct extent_buffer *leaf,
448 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400449 struct inode *inode)
450{
Chris Mason5f39d392007-10-15 16:14:19 -0400451 btrfs_set_inode_uid(leaf, item, inode->i_uid);
452 btrfs_set_inode_gid(leaf, item, inode->i_gid);
453 btrfs_set_inode_size(leaf, item, inode->i_size);
454 btrfs_set_inode_mode(leaf, item, inode->i_mode);
455 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
456
457 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
458 inode->i_atime.tv_sec);
459 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
460 inode->i_atime.tv_nsec);
461
462 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
463 inode->i_mtime.tv_sec);
464 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
465 inode->i_mtime.tv_nsec);
466
467 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
468 inode->i_ctime.tv_sec);
469 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
470 inode->i_ctime.tv_nsec);
471
472 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
473 btrfs_set_inode_generation(leaf, item, inode->i_generation);
474 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
475 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400476 BTRFS_I(inode)->block_group->key.objectid);
477}
478
Chris Masona52d9a82007-08-27 16:49:44 -0400479int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400480 struct btrfs_root *root,
481 struct inode *inode)
482{
483 struct btrfs_inode_item *inode_item;
484 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400485 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400486 int ret;
487
488 path = btrfs_alloc_path();
489 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400490 ret = btrfs_lookup_inode(trans, root, path,
491 &BTRFS_I(inode)->location, 1);
492 if (ret) {
493 if (ret > 0)
494 ret = -ENOENT;
495 goto failed;
496 }
497
Chris Mason5f39d392007-10-15 16:14:19 -0400498 leaf = path->nodes[0];
499 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400500 struct btrfs_inode_item);
501
Chris Mason5f39d392007-10-15 16:14:19 -0400502 fill_inode_item(leaf, inode_item, inode);
503 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400504 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400505 ret = 0;
506failed:
507 btrfs_release_path(root, path);
508 btrfs_free_path(path);
509 return ret;
510}
511
512
513static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
514 struct btrfs_root *root,
515 struct inode *dir,
516 struct dentry *dentry)
517{
518 struct btrfs_path *path;
519 const char *name = dentry->d_name.name;
520 int name_len = dentry->d_name.len;
521 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400522 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400523 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400524 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400525
526 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400527 if (!path) {
528 ret = -ENOMEM;
529 goto err;
530 }
531
Chris Mason39279cc2007-06-12 06:35:45 -0400532 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
533 name, name_len, -1);
534 if (IS_ERR(di)) {
535 ret = PTR_ERR(di);
536 goto err;
537 }
538 if (!di) {
539 ret = -ENOENT;
540 goto err;
541 }
Chris Mason5f39d392007-10-15 16:14:19 -0400542 leaf = path->nodes[0];
543 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400544 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400545 if (ret)
546 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400547 btrfs_release_path(root, path);
548
549 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400550 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400551 if (IS_ERR(di)) {
552 ret = PTR_ERR(di);
553 goto err;
554 }
555 if (!di) {
556 ret = -ENOENT;
557 goto err;
558 }
559 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400560
561 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500562 ret = btrfs_del_inode_ref(trans, root, name, name_len,
563 dentry->d_inode->i_ino,
564 dentry->d_parent->d_inode->i_ino);
565 if (ret) {
566 printk("failed to delete reference to %.*s, "
567 "inode %lu parent %lu\n", name_len, name,
568 dentry->d_inode->i_ino,
569 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500570 }
Chris Mason39279cc2007-06-12 06:35:45 -0400571err:
572 btrfs_free_path(path);
573 if (!ret) {
574 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400575 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400576 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500577#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
578 dentry->d_inode->i_nlink--;
579#else
Chris Mason39279cc2007-06-12 06:35:45 -0400580 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500581#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400582 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400583 dir->i_sb->s_dirt = 1;
584 }
585 return ret;
586}
587
588static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
589{
590 struct btrfs_root *root;
591 struct btrfs_trans_handle *trans;
592 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500593 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400594
595 root = BTRFS_I(dir)->root;
596 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500597
598 ret = btrfs_check_free_space(root, 1, 1);
599 if (ret)
600 goto fail;
601
Chris Mason39279cc2007-06-12 06:35:45 -0400602 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400603
Chris Mason39279cc2007-06-12 06:35:45 -0400604 btrfs_set_trans_block_group(trans, dir);
605 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400606 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400607
Chris Mason39279cc2007-06-12 06:35:45 -0400608 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500609fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400610 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400611 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400612 return ret;
613}
614
615static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
616{
617 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500618 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400619 int ret;
620 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400621 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500622 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400623
Yan134d4512007-10-25 15:49:25 -0400624 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
625 return -ENOTEMPTY;
626
Chris Mason39279cc2007-06-12 06:35:45 -0400627 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500628 ret = btrfs_check_free_space(root, 1, 1);
629 if (ret)
630 goto fail;
631
Chris Mason39279cc2007-06-12 06:35:45 -0400632 trans = btrfs_start_transaction(root, 1);
633 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400634
635 /* now the directory is empty */
636 err = btrfs_unlink_trans(trans, root, dir, dentry);
637 if (!err) {
638 inode->i_size = 0;
639 }
Chris Mason39544012007-12-12 14:38:19 -0500640
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400641 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400642 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500643fail:
Yan134d4512007-10-25 15:49:25 -0400644 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400645 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500646
Chris Mason39279cc2007-06-12 06:35:45 -0400647 if (ret && !err)
648 err = ret;
649 return err;
650}
651
652static int btrfs_free_inode(struct btrfs_trans_handle *trans,
653 struct btrfs_root *root,
654 struct inode *inode)
655{
656 struct btrfs_path *path;
657 int ret;
658
659 clear_inode(inode);
660
661 path = btrfs_alloc_path();
662 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400663 ret = btrfs_lookup_inode(trans, root, path,
664 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400665 if (ret > 0)
666 ret = -ENOENT;
667 if (!ret)
668 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400669 btrfs_free_path(path);
670 return ret;
671}
672
673/*
Chris Mason39279cc2007-06-12 06:35:45 -0400674 * this can truncate away extent items, csum items and directory items.
675 * It starts at a high offset and removes keys until it can't find
676 * any higher than i_size.
677 *
678 * csum items that cross the new i_size are truncated to the new size
679 * as well.
680 */
681static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
682 struct btrfs_root *root,
683 struct inode *inode)
684{
685 int ret;
686 struct btrfs_path *path;
687 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400688 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400689 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400690 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400691 struct btrfs_file_extent_item *fi;
692 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400693 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400694 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500695 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500696 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400697 int found_extent;
698 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400699 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400700
Chris Masona52d9a82007-08-27 16:49:44 -0400701 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400702 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400703 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400704 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400705
Chris Mason39279cc2007-06-12 06:35:45 -0400706 /* FIXME, add redo link to tree so we don't leak on crash */
707 key.objectid = inode->i_ino;
708 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400709 key.type = (u8)-1;
710
Chris Mason39279cc2007-06-12 06:35:45 -0400711 while(1) {
712 btrfs_init_path(path);
713 fi = NULL;
714 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
715 if (ret < 0) {
716 goto error;
717 }
718 if (ret > 0) {
719 BUG_ON(path->slots[0] == 0);
720 path->slots[0]--;
721 }
Chris Mason5f39d392007-10-15 16:14:19 -0400722 leaf = path->nodes[0];
723 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
724 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400725
Chris Mason5f39d392007-10-15 16:14:19 -0400726 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400727 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400728
Chris Mason39279cc2007-06-12 06:35:45 -0400729 if (found_type != BTRFS_CSUM_ITEM_KEY &&
730 found_type != BTRFS_DIR_ITEM_KEY &&
731 found_type != BTRFS_DIR_INDEX_KEY &&
732 found_type != BTRFS_EXTENT_DATA_KEY)
733 break;
734
Chris Mason5f39d392007-10-15 16:14:19 -0400735 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400736 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400737 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400738 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400739 extent_type = btrfs_file_extent_type(leaf, fi);
740 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400741 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400742 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400743 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
744 struct btrfs_item *item = btrfs_item_nr(leaf,
745 path->slots[0]);
746 item_end += btrfs_file_extent_inline_len(leaf,
747 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400748 }
Yan008630c2007-11-07 13:31:09 -0500749 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400750 }
751 if (found_type == BTRFS_CSUM_ITEM_KEY) {
752 ret = btrfs_csum_truncate(trans, root, path,
753 inode->i_size);
754 BUG_ON(ret);
755 }
Yan008630c2007-11-07 13:31:09 -0500756 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400757 if (found_type == BTRFS_DIR_ITEM_KEY) {
758 found_type = BTRFS_INODE_ITEM_KEY;
759 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
760 found_type = BTRFS_CSUM_ITEM_KEY;
761 } else if (found_type) {
762 found_type--;
763 } else {
764 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400765 }
Yana61721d2007-09-17 11:08:38 -0400766 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400767 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400768 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400769 }
Chris Mason5f39d392007-10-15 16:14:19 -0400770 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400771 del_item = 1;
772 else
773 del_item = 0;
774 found_extent = 0;
775
776 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400777 if (found_type != BTRFS_EXTENT_DATA_KEY)
778 goto delete;
779
780 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400781 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400782 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400783 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400784 u64 orig_num_bytes =
785 btrfs_file_extent_num_bytes(leaf, fi);
786 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400787 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400788 btrfs_set_file_extent_num_bytes(leaf, fi,
789 extent_num_bytes);
790 num_dec = (orig_num_bytes -
791 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400792 if (extent_start != 0) {
793 inode->i_blocks -= num_dec;
794 }
Chris Mason5f39d392007-10-15 16:14:19 -0400795 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400796 } else {
Chris Masondb945352007-10-15 16:15:53 -0400797 extent_num_bytes =
798 btrfs_file_extent_disk_num_bytes(leaf,
799 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400800 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400801 num_dec = btrfs_file_extent_num_bytes(leaf,
802 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400803 if (extent_start != 0) {
804 found_extent = 1;
805 inode->i_blocks -= num_dec;
806 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500807 root_gen = btrfs_header_generation(leaf);
808 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400809 }
Chris Mason179e29e2007-11-01 11:28:41 -0400810 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
811 !del_item) {
812 u32 newsize = inode->i_size - found_key.offset;
813 newsize = btrfs_file_extent_calc_inline_size(newsize);
814 ret = btrfs_truncate_item(trans, root, path,
815 newsize, 1);
816 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400817 }
Chris Mason179e29e2007-11-01 11:28:41 -0400818delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400819 if (del_item) {
820 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400821 if (ret)
822 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400823 } else {
824 break;
825 }
826 btrfs_release_path(root, path);
827 if (found_extent) {
828 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500829 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500830 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500831 root_gen, inode->i_ino,
832 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400833 BUG_ON(ret);
834 }
835 }
836 ret = 0;
837error:
838 btrfs_release_path(root, path);
839 btrfs_free_path(path);
840 inode->i_sb->s_dirt = 1;
841 return ret;
842}
843
Chris Masonb888db22007-08-27 16:49:44 -0400844static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400845 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400846{
Chris Mason39279cc2007-06-12 06:35:45 -0400847 char *kaddr;
Chris Masonb888db22007-08-27 16:49:44 -0400848 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500849 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400850 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400851 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500852 u64 existing_delalloc;
853 u64 delalloc_start;
854 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400855
Chris Mason190662b2007-12-18 16:25:45 -0500856 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400857 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400858
Chris Masonb888db22007-08-27 16:49:44 -0400859 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500860 delalloc_start = page_start;
861 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
862 &delalloc_start, page_end,
863 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db22007-08-27 16:49:44 -0400864 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
865 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500866
867 spin_lock(&root->fs_info->delalloc_lock);
868 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
869 spin_unlock(&root->fs_info->delalloc_lock);
870
Chris Masona52d9a82007-08-27 16:49:44 -0400871 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400872 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400873 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
874 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400875 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400876 }
Chris Masonb888db22007-08-27 16:49:44 -0400877 set_page_dirty(page);
878 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400879
Chris Masona52d9a82007-08-27 16:49:44 -0400880 return ret;
881}
882
883/*
884 * taken from block_truncate_page, but does cow as it zeros out
885 * any bytes left in the last page in the file.
886 */
887static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
888{
889 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400890 struct btrfs_root *root = BTRFS_I(inode)->root;
891 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400892 pgoff_t index = from >> PAGE_CACHE_SHIFT;
893 unsigned offset = from & (PAGE_CACHE_SIZE-1);
894 struct page *page;
895 int ret = 0;
896 u64 page_start;
897
898 if ((offset & (blocksize - 1)) == 0)
899 goto out;
900
Chris Masondb945352007-10-15 16:15:53 -0400901 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400902 ret = -ENOMEM;
903 page = grab_cache_page(mapping, index);
904 if (!page)
905 goto out;
906 if (!PageUptodate(page)) {
907 ret = btrfs_readpage(NULL, page);
908 lock_page(page);
909 if (!PageUptodate(page)) {
910 ret = -EIO;
911 goto out;
912 }
913 }
Chris Mason35ebb932007-10-30 16:56:53 -0400914 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400915
Chris Masonb888db22007-08-27 16:49:44 -0400916 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400917
Chris Mason39279cc2007-06-12 06:35:45 -0400918 unlock_page(page);
919 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400920 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400921out:
922 return ret;
923}
924
925static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
926{
927 struct inode *inode = dentry->d_inode;
928 int err;
929
930 err = inode_change_ok(inode, attr);
931 if (err)
932 return err;
933
934 if (S_ISREG(inode->i_mode) &&
935 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
936 struct btrfs_trans_handle *trans;
937 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400938 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
939
Chris Mason5f39d392007-10-15 16:14:19 -0400940 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400941 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400942 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400943 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400944 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400945
946 if (attr->ia_size <= pos)
947 goto out;
948
Chris Mason1832a6d2007-12-21 16:27:21 -0500949 mutex_lock(&root->fs_info->fs_mutex);
950 err = btrfs_check_free_space(root, 1, 0);
951 mutex_unlock(&root->fs_info->fs_mutex);
952 if (err)
953 goto fail;
954
Chris Mason39279cc2007-06-12 06:35:45 -0400955 btrfs_truncate_page(inode->i_mapping, inode->i_size);
956
Chris Mason2bf5a722007-08-30 11:54:02 -0400957 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400958 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400959
960 mutex_lock(&root->fs_info->fs_mutex);
961 trans = btrfs_start_transaction(root, 1);
962 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400963 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400964 pos, pos + hole_size, pos,
965 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400966
Chris Mason179e29e2007-11-01 11:28:41 -0400967 if (alloc_hint != EXTENT_MAP_INLINE) {
968 err = btrfs_insert_file_extent(trans, root,
969 inode->i_ino,
970 pos, 0, 0, hole_size);
971 }
Chris Mason39279cc2007-06-12 06:35:45 -0400972 btrfs_end_transaction(trans, root);
973 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400974 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400975 if (err)
976 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400977 }
978out:
979 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -0500980fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400981 return err;
982}
983void btrfs_delete_inode(struct inode *inode)
984{
985 struct btrfs_trans_handle *trans;
986 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400987 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400988 int ret;
989
990 truncate_inode_pages(&inode->i_data, 0);
991 if (is_bad_inode(inode)) {
992 goto no_delete;
993 }
Chris Mason5f39d392007-10-15 16:14:19 -0400994
Chris Mason39279cc2007-06-12 06:35:45 -0400995 inode->i_size = 0;
996 mutex_lock(&root->fs_info->fs_mutex);
997 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400998
Chris Mason39279cc2007-06-12 06:35:45 -0400999 btrfs_set_trans_block_group(trans, inode);
1000 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001001 if (ret)
1002 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001003 ret = btrfs_delete_xattrs(trans, root, inode);
1004 if (ret)
1005 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001006 ret = btrfs_free_inode(trans, root, inode);
1007 if (ret)
1008 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001009 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001010
Chris Mason39279cc2007-06-12 06:35:45 -04001011 btrfs_end_transaction(trans, root);
1012 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001013 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001014 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001015
1016no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001017 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001018 btrfs_end_transaction(trans, root);
1019 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001020 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001021no_delete:
1022 clear_inode(inode);
1023}
1024
1025/*
1026 * this returns the key found in the dir entry in the location pointer.
1027 * If no dir entries were found, location->objectid is 0.
1028 */
1029static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1030 struct btrfs_key *location)
1031{
1032 const char *name = dentry->d_name.name;
1033 int namelen = dentry->d_name.len;
1034 struct btrfs_dir_item *di;
1035 struct btrfs_path *path;
1036 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001037 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001038
Chris Mason39544012007-12-12 14:38:19 -05001039 if (namelen == 1 && strcmp(name, ".") == 0) {
1040 location->objectid = dir->i_ino;
1041 location->type = BTRFS_INODE_ITEM_KEY;
1042 location->offset = 0;
1043 return 0;
1044 }
Chris Mason39279cc2007-06-12 06:35:45 -04001045 path = btrfs_alloc_path();
1046 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001047
Chris Mason7a720532007-12-13 09:06:59 -05001048 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001049 struct btrfs_key key;
1050 struct extent_buffer *leaf;
1051 u32 nritems;
1052 int slot;
1053
1054 key.objectid = dir->i_ino;
1055 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1056 key.offset = 0;
1057 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1058 BUG_ON(ret == 0);
1059 ret = 0;
1060
1061 leaf = path->nodes[0];
1062 slot = path->slots[0];
1063 nritems = btrfs_header_nritems(leaf);
1064 if (slot >= nritems)
1065 goto out_err;
1066
1067 btrfs_item_key_to_cpu(leaf, &key, slot);
1068 if (key.objectid != dir->i_ino ||
1069 key.type != BTRFS_INODE_REF_KEY) {
1070 goto out_err;
1071 }
1072 location->objectid = key.offset;
1073 location->type = BTRFS_INODE_ITEM_KEY;
1074 location->offset = 0;
1075 goto out;
1076 }
1077
Chris Mason39279cc2007-06-12 06:35:45 -04001078 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1079 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001080 if (IS_ERR(di))
1081 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001082 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001083 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001084 }
Chris Mason5f39d392007-10-15 16:14:19 -04001085 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001086out:
Chris Mason39279cc2007-06-12 06:35:45 -04001087 btrfs_free_path(path);
1088 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001089out_err:
1090 location->objectid = 0;
1091 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001092}
1093
1094/*
1095 * when we hit a tree root in a directory, the btrfs part of the inode
1096 * needs to be changed to reflect the root directory of the tree root. This
1097 * is kind of like crossing a mount point.
1098 */
1099static int fixup_tree_root_location(struct btrfs_root *root,
1100 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001101 struct btrfs_root **sub_root,
1102 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001103{
1104 struct btrfs_path *path;
1105 struct btrfs_root_item *ri;
1106
1107 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1108 return 0;
1109 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1110 return 0;
1111
1112 path = btrfs_alloc_path();
1113 BUG_ON(!path);
1114 mutex_lock(&root->fs_info->fs_mutex);
1115
Josef Bacik58176a92007-08-29 15:47:34 -04001116 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1117 dentry->d_name.name,
1118 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001119 if (IS_ERR(*sub_root))
1120 return PTR_ERR(*sub_root);
1121
1122 ri = &(*sub_root)->root_item;
1123 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001124 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1125 location->offset = 0;
1126
1127 btrfs_free_path(path);
1128 mutex_unlock(&root->fs_info->fs_mutex);
1129 return 0;
1130}
1131
1132static int btrfs_init_locked_inode(struct inode *inode, void *p)
1133{
1134 struct btrfs_iget_args *args = p;
1135 inode->i_ino = args->ino;
1136 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001137 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1138 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001139 return 0;
1140}
1141
1142static int btrfs_find_actor(struct inode *inode, void *opaque)
1143{
1144 struct btrfs_iget_args *args = opaque;
1145 return (args->ino == inode->i_ino &&
1146 args->root == BTRFS_I(inode)->root);
1147}
1148
1149struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1150 struct btrfs_root *root)
1151{
1152 struct inode *inode;
1153 struct btrfs_iget_args args;
1154 args.ino = objectid;
1155 args.root = root;
1156
1157 inode = iget5_locked(s, objectid, btrfs_find_actor,
1158 btrfs_init_locked_inode,
1159 (void *)&args);
1160 return inode;
1161}
1162
1163static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1164 struct nameidata *nd)
1165{
1166 struct inode * inode;
1167 struct btrfs_inode *bi = BTRFS_I(dir);
1168 struct btrfs_root *root = bi->root;
1169 struct btrfs_root *sub_root = root;
1170 struct btrfs_key location;
1171 int ret;
1172
1173 if (dentry->d_name.len > BTRFS_NAME_LEN)
1174 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001175
Chris Mason39279cc2007-06-12 06:35:45 -04001176 mutex_lock(&root->fs_info->fs_mutex);
1177 ret = btrfs_inode_by_name(dir, dentry, &location);
1178 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001179
Chris Mason39279cc2007-06-12 06:35:45 -04001180 if (ret < 0)
1181 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001182
Chris Mason39279cc2007-06-12 06:35:45 -04001183 inode = NULL;
1184 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001185 ret = fixup_tree_root_location(root, &location, &sub_root,
1186 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001187 if (ret < 0)
1188 return ERR_PTR(ret);
1189 if (ret > 0)
1190 return ERR_PTR(-ENOENT);
1191 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1192 sub_root);
1193 if (!inode)
1194 return ERR_PTR(-EACCES);
1195 if (inode->i_state & I_NEW) {
1196 /* the inode and parent dir are two different roots */
1197 if (sub_root != root) {
1198 igrab(inode);
1199 sub_root->inode = inode;
1200 }
1201 BTRFS_I(inode)->root = sub_root;
1202 memcpy(&BTRFS_I(inode)->location, &location,
1203 sizeof(location));
1204 btrfs_read_locked_inode(inode);
1205 unlock_new_inode(inode);
1206 }
1207 }
1208 return d_splice_alias(inode, dentry);
1209}
1210
Chris Mason39279cc2007-06-12 06:35:45 -04001211static unsigned char btrfs_filetype_table[] = {
1212 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1213};
1214
1215static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1216{
Chris Mason6da6aba2007-12-18 16:15:09 -05001217 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001218 struct btrfs_root *root = BTRFS_I(inode)->root;
1219 struct btrfs_item *item;
1220 struct btrfs_dir_item *di;
1221 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001222 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001223 struct btrfs_path *path;
1224 int ret;
1225 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001226 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001227 int slot;
1228 int advance;
1229 unsigned char d_type;
1230 int over = 0;
1231 u32 di_cur;
1232 u32 di_total;
1233 u32 di_len;
1234 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001235 char tmp_name[32];
1236 char *name_ptr;
1237 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001238
1239 /* FIXME, use a real flag for deciding about the key type */
1240 if (root->fs_info->tree_root == root)
1241 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001242
Chris Mason39544012007-12-12 14:38:19 -05001243 /* special case for "." */
1244 if (filp->f_pos == 0) {
1245 over = filldir(dirent, ".", 1,
1246 1, inode->i_ino,
1247 DT_DIR);
1248 if (over)
1249 return 0;
1250 filp->f_pos = 1;
1251 }
1252
Chris Mason39279cc2007-06-12 06:35:45 -04001253 mutex_lock(&root->fs_info->fs_mutex);
1254 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001255 path = btrfs_alloc_path();
1256 path->reada = 2;
1257
1258 /* special case for .., just use the back ref */
1259 if (filp->f_pos == 1) {
1260 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1261 key.offset = 0;
1262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1263 BUG_ON(ret == 0);
1264 leaf = path->nodes[0];
1265 slot = path->slots[0];
1266 nritems = btrfs_header_nritems(leaf);
1267 if (slot >= nritems) {
1268 btrfs_release_path(root, path);
1269 goto read_dir_items;
1270 }
1271 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1272 btrfs_release_path(root, path);
1273 if (found_key.objectid != key.objectid ||
1274 found_key.type != BTRFS_INODE_REF_KEY)
1275 goto read_dir_items;
1276 over = filldir(dirent, "..", 2,
1277 2, found_key.offset, DT_DIR);
1278 if (over)
1279 goto nopos;
1280 filp->f_pos = 2;
1281 }
1282
1283read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001284 btrfs_set_key_type(&key, key_type);
1285 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001286
Chris Mason39279cc2007-06-12 06:35:45 -04001287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1288 if (ret < 0)
1289 goto err;
1290 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001291 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001292 leaf = path->nodes[0];
1293 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001294 slot = path->slots[0];
1295 if (advance || slot >= nritems) {
1296 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001297 ret = btrfs_next_leaf(root, path);
1298 if (ret)
1299 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001300 leaf = path->nodes[0];
1301 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001302 slot = path->slots[0];
1303 } else {
1304 slot++;
1305 path->slots[0]++;
1306 }
1307 }
1308 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001309 item = btrfs_item_nr(leaf, slot);
1310 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1311
1312 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001313 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001314 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001315 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001316 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001317 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001318
1319 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001320 advance = 1;
1321 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1322 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001323 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001324 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001325 struct btrfs_key location;
1326
1327 name_len = btrfs_dir_name_len(leaf, di);
1328 if (name_len < 32) {
1329 name_ptr = tmp_name;
1330 } else {
1331 name_ptr = kmalloc(name_len, GFP_NOFS);
1332 BUG_ON(!name_ptr);
1333 }
1334 read_extent_buffer(leaf, name_ptr,
1335 (unsigned long)(di + 1), name_len);
1336
1337 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1338 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1339
1340 over = filldir(dirent, name_ptr, name_len,
1341 found_key.offset,
1342 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001343 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001344
1345 if (name_ptr != tmp_name)
1346 kfree(name_ptr);
1347
Chris Mason39279cc2007-06-12 06:35:45 -04001348 if (over)
1349 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001350 di_len = btrfs_dir_name_len(leaf, di) +
1351 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001352 di_cur += di_len;
1353 di = (struct btrfs_dir_item *)((char *)di + di_len);
1354 }
1355 }
1356 filp->f_pos++;
1357nopos:
1358 ret = 0;
1359err:
1360 btrfs_release_path(root, path);
1361 btrfs_free_path(path);
1362 mutex_unlock(&root->fs_info->fs_mutex);
1363 return ret;
1364}
1365
1366int btrfs_write_inode(struct inode *inode, int wait)
1367{
1368 struct btrfs_root *root = BTRFS_I(inode)->root;
1369 struct btrfs_trans_handle *trans;
1370 int ret = 0;
1371
1372 if (wait) {
1373 mutex_lock(&root->fs_info->fs_mutex);
1374 trans = btrfs_start_transaction(root, 1);
1375 btrfs_set_trans_block_group(trans, inode);
1376 ret = btrfs_commit_transaction(trans, root);
1377 mutex_unlock(&root->fs_info->fs_mutex);
1378 }
1379 return ret;
1380}
1381
1382/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001383 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001384 * inode changes. But, it is most likely to find the inode in cache.
1385 * FIXME, needs more benchmarking...there are no reasons other than performance
1386 * to keep or drop this code.
1387 */
1388void btrfs_dirty_inode(struct inode *inode)
1389{
1390 struct btrfs_root *root = BTRFS_I(inode)->root;
1391 struct btrfs_trans_handle *trans;
1392
1393 mutex_lock(&root->fs_info->fs_mutex);
1394 trans = btrfs_start_transaction(root, 1);
1395 btrfs_set_trans_block_group(trans, inode);
1396 btrfs_update_inode(trans, root, inode);
1397 btrfs_end_transaction(trans, root);
1398 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001399}
1400
1401static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1403 u64 objectid,
1404 struct btrfs_block_group_cache *group,
1405 int mode)
1406{
1407 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001408 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001409 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001410 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001411 int ret;
1412 int owner;
1413
Chris Mason5f39d392007-10-15 16:14:19 -04001414 path = btrfs_alloc_path();
1415 BUG_ON(!path);
1416
Chris Mason39279cc2007-06-12 06:35:45 -04001417 inode = new_inode(root->fs_info->sb);
1418 if (!inode)
1419 return ERR_PTR(-ENOMEM);
1420
Chris Masonb888db22007-08-27 16:49:44 -04001421 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1422 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001423 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001424
Chris Mason39279cc2007-06-12 06:35:45 -04001425 if (mode & S_IFDIR)
1426 owner = 0;
1427 else
1428 owner = 1;
1429 group = btrfs_find_block_group(root, group, 0, 0, owner);
1430 BTRFS_I(inode)->block_group = group;
1431
Chris Mason5f39d392007-10-15 16:14:19 -04001432 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1433 if (ret)
1434 goto fail;
1435
Chris Mason39279cc2007-06-12 06:35:45 -04001436 inode->i_uid = current->fsuid;
1437 inode->i_gid = current->fsgid;
1438 inode->i_mode = mode;
1439 inode->i_ino = objectid;
1440 inode->i_blocks = 0;
1441 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001442 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1443 struct btrfs_inode_item);
1444 fill_inode_item(path->nodes[0], inode_item, inode);
1445 btrfs_mark_buffer_dirty(path->nodes[0]);
1446 btrfs_free_path(path);
1447
Chris Mason39279cc2007-06-12 06:35:45 -04001448 location = &BTRFS_I(inode)->location;
1449 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001450 location->offset = 0;
1451 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1452
Chris Mason39279cc2007-06-12 06:35:45 -04001453 insert_inode_hash(inode);
1454 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001455fail:
1456 btrfs_free_path(path);
1457 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001458}
1459
1460static inline u8 btrfs_inode_type(struct inode *inode)
1461{
1462 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1463}
1464
1465static int btrfs_add_link(struct btrfs_trans_handle *trans,
1466 struct dentry *dentry, struct inode *inode)
1467{
1468 int ret;
1469 struct btrfs_key key;
1470 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001471 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001472
Chris Mason39279cc2007-06-12 06:35:45 -04001473 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001474 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1475 key.offset = 0;
1476
1477 ret = btrfs_insert_dir_item(trans, root,
1478 dentry->d_name.name, dentry->d_name.len,
1479 dentry->d_parent->d_inode->i_ino,
1480 &key, btrfs_inode_type(inode));
1481 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001482 ret = btrfs_insert_inode_ref(trans, root,
1483 dentry->d_name.name,
1484 dentry->d_name.len,
1485 inode->i_ino,
1486 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001487 parent_inode = dentry->d_parent->d_inode;
1488 parent_inode->i_size += dentry->d_name.len * 2;
1489 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001490 ret = btrfs_update_inode(trans, root,
1491 dentry->d_parent->d_inode);
1492 }
1493 return ret;
1494}
1495
1496static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1497 struct dentry *dentry, struct inode *inode)
1498{
1499 int err = btrfs_add_link(trans, dentry, inode);
1500 if (!err) {
1501 d_instantiate(dentry, inode);
1502 return 0;
1503 }
1504 if (err > 0)
1505 err = -EEXIST;
1506 return err;
1507}
1508
Josef Bacik618e21d2007-07-11 10:18:17 -04001509static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1510 int mode, dev_t rdev)
1511{
1512 struct btrfs_trans_handle *trans;
1513 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001514 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001515 int err;
1516 int drop_inode = 0;
1517 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001518 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001519
1520 if (!new_valid_dev(rdev))
1521 return -EINVAL;
1522
1523 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001524 err = btrfs_check_free_space(root, 1, 0);
1525 if (err)
1526 goto fail;
1527
Josef Bacik618e21d2007-07-11 10:18:17 -04001528 trans = btrfs_start_transaction(root, 1);
1529 btrfs_set_trans_block_group(trans, dir);
1530
1531 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1532 if (err) {
1533 err = -ENOSPC;
1534 goto out_unlock;
1535 }
1536
1537 inode = btrfs_new_inode(trans, root, objectid,
1538 BTRFS_I(dir)->block_group, mode);
1539 err = PTR_ERR(inode);
1540 if (IS_ERR(inode))
1541 goto out_unlock;
1542
1543 btrfs_set_trans_block_group(trans, inode);
1544 err = btrfs_add_nondir(trans, dentry, inode);
1545 if (err)
1546 drop_inode = 1;
1547 else {
1548 inode->i_op = &btrfs_special_inode_operations;
1549 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001550 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001551 }
1552 dir->i_sb->s_dirt = 1;
1553 btrfs_update_inode_block_group(trans, inode);
1554 btrfs_update_inode_block_group(trans, dir);
1555out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001556 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001557 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001558fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001559 mutex_unlock(&root->fs_info->fs_mutex);
1560
1561 if (drop_inode) {
1562 inode_dec_link_count(inode);
1563 iput(inode);
1564 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001565 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001566 return err;
1567}
1568
Chris Mason39279cc2007-06-12 06:35:45 -04001569static int btrfs_create(struct inode *dir, struct dentry *dentry,
1570 int mode, struct nameidata *nd)
1571{
1572 struct btrfs_trans_handle *trans;
1573 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001574 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001575 int err;
1576 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001577 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001578 u64 objectid;
1579
1580 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001581 err = btrfs_check_free_space(root, 1, 0);
1582 if (err)
1583 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001584 trans = btrfs_start_transaction(root, 1);
1585 btrfs_set_trans_block_group(trans, dir);
1586
1587 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1588 if (err) {
1589 err = -ENOSPC;
1590 goto out_unlock;
1591 }
1592
1593 inode = btrfs_new_inode(trans, root, objectid,
1594 BTRFS_I(dir)->block_group, mode);
1595 err = PTR_ERR(inode);
1596 if (IS_ERR(inode))
1597 goto out_unlock;
1598
1599 btrfs_set_trans_block_group(trans, inode);
1600 err = btrfs_add_nondir(trans, dentry, inode);
1601 if (err)
1602 drop_inode = 1;
1603 else {
1604 inode->i_mapping->a_ops = &btrfs_aops;
1605 inode->i_fop = &btrfs_file_operations;
1606 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001607 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1608 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001609 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001610 }
1611 dir->i_sb->s_dirt = 1;
1612 btrfs_update_inode_block_group(trans, inode);
1613 btrfs_update_inode_block_group(trans, dir);
1614out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001615 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001616 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001617fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001618 mutex_unlock(&root->fs_info->fs_mutex);
1619
1620 if (drop_inode) {
1621 inode_dec_link_count(inode);
1622 iput(inode);
1623 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001624 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001625 return err;
1626}
1627
1628static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1629 struct dentry *dentry)
1630{
1631 struct btrfs_trans_handle *trans;
1632 struct btrfs_root *root = BTRFS_I(dir)->root;
1633 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001634 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001635 int err;
1636 int drop_inode = 0;
1637
1638 if (inode->i_nlink == 0)
1639 return -ENOENT;
1640
Chris Mason6da6aba2007-12-18 16:15:09 -05001641#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1642 inode->i_nlink++;
1643#else
Chris Mason39279cc2007-06-12 06:35:45 -04001644 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001645#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001646 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001647 err = btrfs_check_free_space(root, 1, 0);
1648 if (err)
1649 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001650 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001651
Chris Mason39279cc2007-06-12 06:35:45 -04001652 btrfs_set_trans_block_group(trans, dir);
1653 atomic_inc(&inode->i_count);
1654 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001655
Chris Mason39279cc2007-06-12 06:35:45 -04001656 if (err)
1657 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001658
Chris Mason39279cc2007-06-12 06:35:45 -04001659 dir->i_sb->s_dirt = 1;
1660 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001661 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001662
Chris Mason54aa1f42007-06-22 14:16:25 -04001663 if (err)
1664 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001665
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001666 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001667 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001668fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001669 mutex_unlock(&root->fs_info->fs_mutex);
1670
1671 if (drop_inode) {
1672 inode_dec_link_count(inode);
1673 iput(inode);
1674 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001675 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001676 return err;
1677}
1678
Chris Mason39279cc2007-06-12 06:35:45 -04001679static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1680{
1681 struct inode *inode;
1682 struct btrfs_trans_handle *trans;
1683 struct btrfs_root *root = BTRFS_I(dir)->root;
1684 int err = 0;
1685 int drop_on_err = 0;
1686 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001687 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001688
1689 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001690 err = btrfs_check_free_space(root, 1, 0);
1691 if (err)
1692 goto out_unlock;
1693
Chris Mason39279cc2007-06-12 06:35:45 -04001694 trans = btrfs_start_transaction(root, 1);
1695 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001696
Chris Mason39279cc2007-06-12 06:35:45 -04001697 if (IS_ERR(trans)) {
1698 err = PTR_ERR(trans);
1699 goto out_unlock;
1700 }
1701
1702 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1703 if (err) {
1704 err = -ENOSPC;
1705 goto out_unlock;
1706 }
1707
1708 inode = btrfs_new_inode(trans, root, objectid,
1709 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1710 if (IS_ERR(inode)) {
1711 err = PTR_ERR(inode);
1712 goto out_fail;
1713 }
Chris Mason5f39d392007-10-15 16:14:19 -04001714
Chris Mason39279cc2007-06-12 06:35:45 -04001715 drop_on_err = 1;
1716 inode->i_op = &btrfs_dir_inode_operations;
1717 inode->i_fop = &btrfs_dir_file_operations;
1718 btrfs_set_trans_block_group(trans, inode);
1719
Chris Mason39544012007-12-12 14:38:19 -05001720 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001721 err = btrfs_update_inode(trans, root, inode);
1722 if (err)
1723 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001724
Chris Mason39279cc2007-06-12 06:35:45 -04001725 err = btrfs_add_link(trans, dentry, inode);
1726 if (err)
1727 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001728
Chris Mason39279cc2007-06-12 06:35:45 -04001729 d_instantiate(dentry, inode);
1730 drop_on_err = 0;
1731 dir->i_sb->s_dirt = 1;
1732 btrfs_update_inode_block_group(trans, inode);
1733 btrfs_update_inode_block_group(trans, dir);
1734
1735out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001736 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001737 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001738
Chris Mason39279cc2007-06-12 06:35:45 -04001739out_unlock:
1740 mutex_unlock(&root->fs_info->fs_mutex);
1741 if (drop_on_err)
1742 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001743 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001744 return err;
1745}
1746
Chris Masona52d9a82007-08-27 16:49:44 -04001747struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1748 size_t page_offset, u64 start, u64 end,
1749 int create)
1750{
1751 int ret;
1752 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001753 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001754 u64 extent_start = 0;
1755 u64 extent_end = 0;
1756 u64 objectid = inode->i_ino;
1757 u32 found_type;
1758 int failed_insert = 0;
1759 struct btrfs_path *path;
1760 struct btrfs_root *root = BTRFS_I(inode)->root;
1761 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001762 struct extent_buffer *leaf;
1763 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001764 struct extent_map *em = NULL;
1765 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1766 struct btrfs_trans_handle *trans = NULL;
1767
1768 path = btrfs_alloc_path();
1769 BUG_ON(!path);
1770 mutex_lock(&root->fs_info->fs_mutex);
1771
1772again:
1773 em = lookup_extent_mapping(em_tree, start, end);
1774 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001775 if (em->start > start) {
1776 printk("get_extent start %Lu em start %Lu\n",
1777 start, em->start);
1778 WARN_ON(1);
1779 }
Chris Masona52d9a82007-08-27 16:49:44 -04001780 goto out;
1781 }
1782 if (!em) {
1783 em = alloc_extent_map(GFP_NOFS);
1784 if (!em) {
1785 err = -ENOMEM;
1786 goto out;
1787 }
Chris Mason5f39d392007-10-15 16:14:19 -04001788 em->start = EXTENT_MAP_HOLE;
1789 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001790 }
1791 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001792 ret = btrfs_lookup_file_extent(trans, root, path,
1793 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001794 if (ret < 0) {
1795 err = ret;
1796 goto out;
1797 }
1798
1799 if (ret != 0) {
1800 if (path->slots[0] == 0)
1801 goto not_found;
1802 path->slots[0]--;
1803 }
1804
Chris Mason5f39d392007-10-15 16:14:19 -04001805 leaf = path->nodes[0];
1806 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001807 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001808 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001809 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1810 found_type = btrfs_key_type(&found_key);
1811 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001812 found_type != BTRFS_EXTENT_DATA_KEY) {
1813 goto not_found;
1814 }
1815
Chris Mason5f39d392007-10-15 16:14:19 -04001816 found_type = btrfs_file_extent_type(leaf, item);
1817 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001818 if (found_type == BTRFS_FILE_EXTENT_REG) {
1819 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001820 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001821 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001822 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001823 em->start = start;
1824 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001825 if (end < extent_start)
1826 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001827 em->end = extent_end - 1;
1828 } else {
1829 em->end = end;
1830 }
1831 goto not_found_em;
1832 }
Chris Masondb945352007-10-15 16:15:53 -04001833 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1834 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001835 em->start = extent_start;
1836 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001837 em->block_start = EXTENT_MAP_HOLE;
1838 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001839 goto insert;
1840 }
Chris Masondb945352007-10-15 16:15:53 -04001841 bytenr += btrfs_file_extent_offset(leaf, item);
1842 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001843 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001844 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001845 em->start = extent_start;
1846 em->end = extent_end - 1;
1847 goto insert;
1848 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001849 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001850 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001851 size_t size;
1852 size_t extent_offset;
1853 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001854
Chris Mason5f39d392007-10-15 16:14:19 -04001855 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1856 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001857 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001858 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001859 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001860 em->start = start;
1861 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001862 if (end < extent_start)
1863 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001864 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001865 } else {
1866 em->end = end;
1867 }
1868 goto not_found_em;
1869 }
1870 em->block_start = EXTENT_MAP_INLINE;
1871 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001872
1873 if (!page) {
1874 em->start = extent_start;
1875 em->end = extent_start + size - 1;
1876 goto out;
1877 }
1878
Chris Mason35ebb932007-10-30 16:56:53 -04001879 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001880 extent_start + page_offset;
1881 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1882 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001883 em->start = extent_start + extent_offset;
1884 em->end = (em->start + copy_size -1) |
1885 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001886 map = kmap(page);
1887 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001888 if (create == 0 && !PageUptodate(page)) {
1889 read_extent_buffer(leaf, map + page_offset, ptr,
1890 copy_size);
1891 flush_dcache_page(page);
1892 } else if (create && PageUptodate(page)) {
1893 if (!trans) {
1894 kunmap(page);
1895 free_extent_map(em);
1896 em = NULL;
1897 btrfs_release_path(root, path);
1898 trans = btrfs_start_transaction(root, 1);
1899 goto again;
1900 }
1901 write_extent_buffer(leaf, map + page_offset, ptr,
1902 copy_size);
1903 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001904 }
Chris Masona52d9a82007-08-27 16:49:44 -04001905 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001906 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001907 goto insert;
1908 } else {
1909 printk("unkknown found_type %d\n", found_type);
1910 WARN_ON(1);
1911 }
1912not_found:
1913 em->start = start;
1914 em->end = end;
1915not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001916 em->block_start = EXTENT_MAP_HOLE;
1917 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001918insert:
1919 btrfs_release_path(root, path);
1920 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001921 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001922 err = -EIO;
1923 goto out;
1924 }
1925 ret = add_extent_mapping(em_tree, em);
1926 if (ret == -EEXIST) {
1927 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001928 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001929 if (0 && failed_insert == 1) {
1930 btrfs_drop_extent_cache(inode, start, end);
1931 }
Chris Masona52d9a82007-08-27 16:49:44 -04001932 failed_insert++;
1933 if (failed_insert > 5) {
1934 printk("failing to insert %Lu %Lu\n", start, end);
1935 err = -EIO;
1936 goto out;
1937 }
Chris Masona52d9a82007-08-27 16:49:44 -04001938 goto again;
1939 }
1940 err = 0;
1941out:
1942 btrfs_free_path(path);
1943 if (trans) {
1944 ret = btrfs_end_transaction(trans, root);
1945 if (!err)
1946 err = ret;
1947 }
1948 mutex_unlock(&root->fs_info->fs_mutex);
1949 if (err) {
1950 free_extent_map(em);
1951 WARN_ON(1);
1952 return ERR_PTR(err);
1953 }
1954 return em;
1955}
1956
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001957static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001958{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001959 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001960}
1961
Chris Mason9ebefb182007-06-15 13:50:00 -04001962int btrfs_readpage(struct file *file, struct page *page)
1963{
Chris Masona52d9a82007-08-27 16:49:44 -04001964 struct extent_map_tree *tree;
1965 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1966 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001967}
Chris Mason1832a6d2007-12-21 16:27:21 -05001968
Chris Mason39279cc2007-06-12 06:35:45 -04001969static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1970{
Chris Masona52d9a82007-08-27 16:49:44 -04001971 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001972
1973
1974 if (current->flags & PF_MEMALLOC) {
1975 redirty_page_for_writepage(wbc, page);
1976 unlock_page(page);
1977 return 0;
1978 }
Chris Masona52d9a82007-08-27 16:49:44 -04001979 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1980 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1981}
Chris Mason39279cc2007-06-12 06:35:45 -04001982
Chris Masonb293f022007-11-01 19:45:34 -04001983static int btrfs_writepages(struct address_space *mapping,
1984 struct writeback_control *wbc)
1985{
1986 struct extent_map_tree *tree;
1987 tree = &BTRFS_I(mapping->host)->extent_tree;
1988 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1989}
1990
Chris Mason3ab2fb52007-11-08 10:59:22 -05001991static int
1992btrfs_readpages(struct file *file, struct address_space *mapping,
1993 struct list_head *pages, unsigned nr_pages)
1994{
1995 struct extent_map_tree *tree;
1996 tree = &BTRFS_I(mapping->host)->extent_tree;
1997 return extent_readpages(tree, mapping, pages, nr_pages,
1998 btrfs_get_extent);
1999}
2000
Chris Masona52d9a82007-08-27 16:49:44 -04002001static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2002{
2003 struct extent_map_tree *tree;
2004 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002005
Chris Masona52d9a82007-08-27 16:49:44 -04002006 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2007 ret = try_release_extent_mapping(tree, page);
2008 if (ret == 1) {
2009 ClearPagePrivate(page);
2010 set_page_private(page, 0);
2011 page_cache_release(page);
2012 }
2013 return ret;
2014}
Chris Mason39279cc2007-06-12 06:35:45 -04002015
Chris Masona52d9a82007-08-27 16:49:44 -04002016static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2017{
2018 struct extent_map_tree *tree;
2019
2020 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2021 extent_invalidatepage(tree, page, offset);
2022 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002023}
2024
Chris Mason9ebefb182007-06-15 13:50:00 -04002025/*
2026 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2027 * called from a page fault handler when a page is first dirtied. Hence we must
2028 * be careful to check for EOF conditions here. We set the page up correctly
2029 * for a written page which means we get ENOSPC checking when writing into
2030 * holes and correct delalloc and unwritten extent mapping on filesystems that
2031 * support these features.
2032 *
2033 * We are not allowed to take the i_mutex here so we have to play games to
2034 * protect against truncate races as the page could now be beyond EOF. Because
2035 * vmtruncate() writes the inode size before removing pages, once we have the
2036 * page lock we can determine safely if the page is beyond EOF. If it is not
2037 * beyond EOF, then the page is guaranteed safe against truncation until we
2038 * unlock the page.
2039 */
2040int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2041{
Chris Mason6da6aba2007-12-18 16:15:09 -05002042 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002043 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002044 unsigned long end;
2045 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002046 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002047 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002048
Chris Mason1832a6d2007-12-21 16:27:21 -05002049 mutex_lock(&root->fs_info->fs_mutex);
2050 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002051 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002052 if (ret)
2053 goto out;
2054
2055 ret = -EINVAL;
2056
Chris Mason011410b2007-09-10 19:58:36 -04002057 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04002058 lock_page(page);
2059 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002060 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002061 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002062
Chris Mason9ebefb182007-06-15 13:50:00 -04002063 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002064 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002065 /* page got truncated out from underneath us */
2066 goto out_unlock;
2067 }
2068
2069 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002070 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002071 end = size & ~PAGE_CACHE_MASK;
2072 else
2073 end = PAGE_CACHE_SIZE;
2074
Chris Masonb888db22007-08-27 16:49:44 -04002075 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002076
2077out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04002078 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04002079 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002080out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002081 return ret;
2082}
2083
Chris Mason39279cc2007-06-12 06:35:45 -04002084static void btrfs_truncate(struct inode *inode)
2085{
2086 struct btrfs_root *root = BTRFS_I(inode)->root;
2087 int ret;
2088 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002089 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002090
2091 if (!S_ISREG(inode->i_mode))
2092 return;
2093 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2094 return;
2095
2096 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2097
2098 mutex_lock(&root->fs_info->fs_mutex);
2099 trans = btrfs_start_transaction(root, 1);
2100 btrfs_set_trans_block_group(trans, inode);
2101
2102 /* FIXME, add redo link to tree so we don't leak on crash */
2103 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002104 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002105 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002106
Chris Mason39279cc2007-06-12 06:35:45 -04002107 ret = btrfs_end_transaction(trans, root);
2108 BUG_ON(ret);
2109 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002110 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002111}
2112
Chris Mason4313b392008-01-03 09:08:48 -05002113static int noinline create_subvol(struct btrfs_root *root, char *name,
2114 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002115{
2116 struct btrfs_trans_handle *trans;
2117 struct btrfs_key key;
2118 struct btrfs_root_item root_item;
2119 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002120 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002121 struct btrfs_root *new_root;
2122 struct inode *inode;
2123 struct inode *dir;
2124 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002125 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002126 u64 objectid;
2127 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002128 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002129
2130 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002131 ret = btrfs_check_free_space(root, 1, 0);
2132 if (ret)
2133 goto fail_commit;
2134
Chris Mason39279cc2007-06-12 06:35:45 -04002135 trans = btrfs_start_transaction(root, 1);
2136 BUG_ON(!trans);
2137
Chris Mason7bb86312007-12-11 09:25:06 -05002138 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2139 0, &objectid);
2140 if (ret)
2141 goto fail;
2142
2143 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2144 objectid, trans->transid, 0, 0,
2145 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002146 if (IS_ERR(leaf))
2147 return PTR_ERR(leaf);
2148
2149 btrfs_set_header_nritems(leaf, 0);
2150 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002151 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002152 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002153 btrfs_set_header_owner(leaf, objectid);
2154
Chris Mason5f39d392007-10-15 16:14:19 -04002155 write_extent_buffer(leaf, root->fs_info->fsid,
2156 (unsigned long)btrfs_header_fsid(leaf),
2157 BTRFS_FSID_SIZE);
2158 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002159
2160 inode_item = &root_item.inode;
2161 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002162 inode_item->generation = cpu_to_le64(1);
2163 inode_item->size = cpu_to_le64(3);
2164 inode_item->nlink = cpu_to_le32(1);
2165 inode_item->nblocks = cpu_to_le64(1);
2166 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002167
Chris Masondb945352007-10-15 16:15:53 -04002168 btrfs_set_root_bytenr(&root_item, leaf->start);
2169 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002170 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002171 btrfs_set_root_used(&root_item, 0);
2172
Chris Mason5eda7b52007-06-22 14:16:25 -04002173 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2174 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002175
2176 free_extent_buffer(leaf);
2177 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002178
Chris Mason39279cc2007-06-12 06:35:45 -04002179 btrfs_set_root_dirid(&root_item, new_dirid);
2180
2181 key.objectid = objectid;
2182 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002183 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2184 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2185 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002186 if (ret)
2187 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002188
2189 /*
2190 * insert the directory item
2191 */
2192 key.offset = (u64)-1;
2193 dir = root->fs_info->sb->s_root->d_inode;
2194 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2195 name, namelen, dir->i_ino, &key,
2196 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002197 if (ret)
2198 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002199
Chris Mason39544012007-12-12 14:38:19 -05002200 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2201 name, namelen, objectid,
2202 root->fs_info->sb->s_root->d_inode->i_ino);
2203 if (ret)
2204 goto fail;
2205
Chris Mason39279cc2007-06-12 06:35:45 -04002206 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002207 if (ret)
2208 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002209
Josef Bacik58176a92007-08-29 15:47:34 -04002210 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002211 BUG_ON(!new_root);
2212
2213 trans = btrfs_start_transaction(new_root, 1);
2214 BUG_ON(!trans);
2215
2216 inode = btrfs_new_inode(trans, new_root, new_dirid,
2217 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002218 if (IS_ERR(inode))
2219 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002220 inode->i_op = &btrfs_dir_inode_operations;
2221 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002222 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002223
Chris Mason39544012007-12-12 14:38:19 -05002224 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2225 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002226 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002227 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002228 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002229 if (ret)
2230 goto fail;
2231fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002232 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002233 err = btrfs_commit_transaction(trans, root);
2234 if (err && !ret)
2235 ret = err;
2236fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002237 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002238 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002239 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002240}
2241
2242static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2243{
2244 struct btrfs_trans_handle *trans;
2245 struct btrfs_key key;
2246 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002247 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002248 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002249 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002250 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002251 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002252
2253 if (!root->ref_cows)
2254 return -EINVAL;
2255
Chris Mason011410b2007-09-10 19:58:36 -04002256 down_write(&root->snap_sem);
2257 freeze_bdev(root->fs_info->sb->s_bdev);
2258 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2259
Chris Mason39279cc2007-06-12 06:35:45 -04002260 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002261 ret = btrfs_check_free_space(root, 1, 0);
2262 if (ret)
2263 goto fail_unlock;
2264
Chris Mason39279cc2007-06-12 06:35:45 -04002265 trans = btrfs_start_transaction(root, 1);
2266 BUG_ON(!trans);
2267
2268 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002269 if (ret)
2270 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002271
2272 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2273 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002274 if (ret)
2275 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002276
2277 memcpy(&new_root_item, &root->root_item,
2278 sizeof(new_root_item));
2279
2280 key.objectid = objectid;
2281 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002282 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Masonbe20aa92007-12-17 20:14:01 -05002283
Yan96919752007-12-04 13:20:20 -05002284 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002285 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002286 free_extent_buffer(tmp);
Chris Masonbe20aa92007-12-17 20:14:01 -05002287
2288 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2289
2290 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2291 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
Chris Mason39279cc2007-06-12 06:35:45 -04002292 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2293 &new_root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05002294 free_extent_buffer(tmp);
Chris Mason54aa1f42007-06-22 14:16:25 -04002295 if (ret)
2296 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002297
2298 /*
2299 * insert the directory item
2300 */
2301 key.offset = (u64)-1;
2302 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2303 name, namelen,
2304 root->fs_info->sb->s_root->d_inode->i_ino,
2305 &key, BTRFS_FT_DIR);
2306
Chris Mason54aa1f42007-06-22 14:16:25 -04002307 if (ret)
2308 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002309
Chris Mason39544012007-12-12 14:38:19 -05002310 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2311 name, namelen, objectid,
2312 root->fs_info->sb->s_root->d_inode->i_ino);
2313
2314 if (ret)
2315 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002316fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002317 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002318 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002319
Chris Mason54aa1f42007-06-22 14:16:25 -04002320 if (err && !ret)
2321 ret = err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002322fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002323 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002324 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002325 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002326 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002327}
2328
Chris Masonedbd8d42007-12-21 16:27:24 -05002329unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002330 struct file_ra_state *ra, struct file *file,
2331 pgoff_t offset, pgoff_t last_index)
2332{
2333 pgoff_t req_size;
2334
2335#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2336 req_size = last_index - offset + 1;
2337 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2338 return offset;
2339#else
2340 req_size = min(last_index - offset + 1, (pgoff_t)128);
2341 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2342 return offset + req_size;
2343#endif
2344}
2345
2346int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002347 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002348 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002349 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2350 struct page *page;
2351 unsigned long last_index;
2352 unsigned long ra_index = 0;
2353 u64 page_start;
2354 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002355 u64 delalloc_start;
2356 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002357 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002358 int ret;
2359
2360 mutex_lock(&root->fs_info->fs_mutex);
2361 ret = btrfs_check_free_space(root, inode->i_size, 0);
2362 mutex_unlock(&root->fs_info->fs_mutex);
2363 if (ret)
2364 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002365
2366 mutex_lock(&inode->i_mutex);
2367 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2368 for (i = 0; i <= last_index; i++) {
2369 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002370 ra_index = btrfs_force_ra(inode->i_mapping,
2371 &file->f_ra,
2372 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002373 }
2374 page = grab_cache_page(inode->i_mapping, i);
2375 if (!page)
2376 goto out_unlock;
2377 if (!PageUptodate(page)) {
2378 btrfs_readpage(NULL, page);
2379 lock_page(page);
2380 if (!PageUptodate(page)) {
2381 unlock_page(page);
2382 page_cache_release(page);
2383 goto out_unlock;
2384 }
2385 }
Chris Mason35ebb932007-10-30 16:56:53 -04002386 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002387 page_end = page_start + PAGE_CACHE_SIZE - 1;
2388
2389 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002390 delalloc_start = page_start;
2391 existing_delalloc =
2392 count_range_bits(&BTRFS_I(inode)->extent_tree,
2393 &delalloc_start, page_end,
2394 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002395 set_extent_delalloc(em_tree, page_start,
2396 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002397
2398 spin_lock(&root->fs_info->delalloc_lock);
2399 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2400 existing_delalloc;
2401 spin_unlock(&root->fs_info->delalloc_lock);
2402
Chris Mason86479a02007-09-10 19:58:16 -04002403 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2404 set_page_dirty(page);
2405 unlock_page(page);
2406 page_cache_release(page);
2407 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2408 }
2409
2410out_unlock:
2411 mutex_unlock(&inode->i_mutex);
2412 return 0;
2413}
2414
Chris Masonedbd8d42007-12-21 16:27:24 -05002415static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2416{
2417 u64 new_size;
2418 u64 old_size;
2419 struct btrfs_ioctl_vol_args *vol_args;
2420 struct btrfs_trans_handle *trans;
2421 char *sizestr;
2422 int ret = 0;
2423 int namelen;
2424 int mod = 0;
2425
2426 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2427
2428 if (!vol_args)
2429 return -ENOMEM;
2430
2431 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2432 ret = -EFAULT;
2433 goto out;
2434 }
2435 namelen = strlen(vol_args->name);
2436 if (namelen > BTRFS_VOL_NAME_MAX) {
2437 ret = -EINVAL;
2438 goto out;
2439 }
2440
2441 sizestr = vol_args->name;
2442 if (!strcmp(sizestr, "max"))
2443 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2444 else {
2445 if (sizestr[0] == '-') {
2446 mod = -1;
2447 sizestr++;
2448 } else if (sizestr[0] == '+') {
2449 mod = 1;
2450 sizestr++;
2451 }
2452 new_size = btrfs_parse_size(sizestr);
2453 if (new_size == 0) {
2454 ret = -EINVAL;
2455 goto out;
2456 }
2457 }
2458
2459 mutex_lock(&root->fs_info->fs_mutex);
2460 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2461
2462 if (mod < 0) {
2463 if (new_size > old_size) {
2464 ret = -EINVAL;
2465 goto out_unlock;
2466 }
2467 new_size = old_size - new_size;
2468 } else if (mod > 0) {
2469 new_size = old_size + new_size;
2470 }
2471
2472 if (new_size < 256 * 1024 * 1024) {
2473 ret = -EINVAL;
2474 goto out_unlock;
2475 }
2476 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2477 ret = -EFBIG;
2478 goto out_unlock;
2479 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002480
2481 do_div(new_size, root->sectorsize);
2482 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002483
2484printk("new size is %Lu\n", new_size);
2485 if (new_size > old_size) {
2486 trans = btrfs_start_transaction(root, 1);
2487 ret = btrfs_grow_extent_tree(trans, root, new_size);
2488 btrfs_commit_transaction(trans, root);
2489 } else {
2490 ret = btrfs_shrink_extent_tree(root, new_size);
2491 }
2492
2493out_unlock:
2494 mutex_unlock(&root->fs_info->fs_mutex);
2495out:
2496 kfree(vol_args);
2497 return ret;
2498}
2499
Chris Mason4313b392008-01-03 09:08:48 -05002500static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2501 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002502{
Chris Mason4aec2b52007-12-18 16:25:45 -05002503 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002504 struct btrfs_dir_item *di;
2505 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002506 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002507 int namelen;
2508 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002509
Chris Mason4aec2b52007-12-18 16:25:45 -05002510 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002511
Chris Mason4aec2b52007-12-18 16:25:45 -05002512 if (!vol_args)
2513 return -ENOMEM;
2514
2515 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2516 ret = -EFAULT;
2517 goto out;
2518 }
2519
2520 namelen = strlen(vol_args->name);
2521 if (namelen > BTRFS_VOL_NAME_MAX) {
2522 ret = -EINVAL;
2523 goto out;
2524 }
2525 if (strchr(vol_args->name, '/')) {
2526 ret = -EINVAL;
2527 goto out;
2528 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002529
2530 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002531 if (!path) {
2532 ret = -ENOMEM;
2533 goto out;
2534 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002535
2536 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2537 mutex_lock(&root->fs_info->fs_mutex);
2538 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2539 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002540 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002541 mutex_unlock(&root->fs_info->fs_mutex);
2542 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002543
2544 if (di && !IS_ERR(di)) {
2545 ret = -EEXIST;
2546 goto out;
2547 }
2548
2549 if (IS_ERR(di)) {
2550 ret = PTR_ERR(di);
2551 goto out;
2552 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002553
2554 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002555 ret = create_subvol(root, vol_args->name, namelen);
2556 else
2557 ret = create_snapshot(root, vol_args->name, namelen);
2558out:
2559 kfree(vol_args);
2560 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002561}
2562
2563static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002564{
Chris Mason6da6aba2007-12-18 16:15:09 -05002565 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002566 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002567
2568 switch (inode->i_mode & S_IFMT) {
2569 case S_IFDIR:
2570 mutex_lock(&root->fs_info->fs_mutex);
2571 btrfs_defrag_root(root, 0);
2572 btrfs_defrag_root(root->fs_info->extent_root, 0);
2573 mutex_unlock(&root->fs_info->fs_mutex);
2574 break;
2575 case S_IFREG:
2576 btrfs_defrag_file(file);
2577 break;
2578 }
2579
2580 return 0;
2581}
2582
2583long btrfs_ioctl(struct file *file, unsigned int
2584 cmd, unsigned long arg)
2585{
Chris Mason6da6aba2007-12-18 16:15:09 -05002586 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002587
2588 switch (cmd) {
2589 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002590 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002591 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002592 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002593 case BTRFS_IOC_RESIZE:
2594 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002595 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002596
2597 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002598}
2599
Chris Mason39279cc2007-06-12 06:35:45 -04002600/*
2601 * Called inside transaction, so use GFP_NOFS
2602 */
2603struct inode *btrfs_alloc_inode(struct super_block *sb)
2604{
2605 struct btrfs_inode *ei;
2606
2607 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2608 if (!ei)
2609 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002610 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002611 return &ei->vfs_inode;
2612}
2613
2614void btrfs_destroy_inode(struct inode *inode)
2615{
2616 WARN_ON(!list_empty(&inode->i_dentry));
2617 WARN_ON(inode->i_data.nrpages);
2618
2619 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2620}
2621
Chris Mason44ec0b72007-10-29 10:55:05 -04002622#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2623static void init_once(struct kmem_cache * cachep, void *foo)
2624#else
Chris Mason39279cc2007-06-12 06:35:45 -04002625static void init_once(void * foo, struct kmem_cache * cachep,
2626 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002627#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002628{
2629 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2630
2631 inode_init_once(&ei->vfs_inode);
2632}
2633
2634void btrfs_destroy_cachep(void)
2635{
2636 if (btrfs_inode_cachep)
2637 kmem_cache_destroy(btrfs_inode_cachep);
2638 if (btrfs_trans_handle_cachep)
2639 kmem_cache_destroy(btrfs_trans_handle_cachep);
2640 if (btrfs_transaction_cachep)
2641 kmem_cache_destroy(btrfs_transaction_cachep);
2642 if (btrfs_bit_radix_cachep)
2643 kmem_cache_destroy(btrfs_bit_radix_cachep);
2644 if (btrfs_path_cachep)
2645 kmem_cache_destroy(btrfs_path_cachep);
2646}
2647
Chris Mason86479a02007-09-10 19:58:16 -04002648struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002649 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002650#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2651 void (*ctor)(struct kmem_cache *, void *)
2652#else
Chris Mason92fee662007-07-25 12:31:35 -04002653 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002654 unsigned long)
2655#endif
2656 )
Chris Mason92fee662007-07-25 12:31:35 -04002657{
2658 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2659 SLAB_MEM_SPREAD | extra_flags), ctor
2660#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2661 ,NULL
2662#endif
2663 );
2664}
2665
Chris Mason39279cc2007-06-12 06:35:45 -04002666int btrfs_init_cachep(void)
2667{
Chris Mason86479a02007-09-10 19:58:16 -04002668 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002669 sizeof(struct btrfs_inode),
2670 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002671 if (!btrfs_inode_cachep)
2672 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002673 btrfs_trans_handle_cachep =
2674 btrfs_cache_create("btrfs_trans_handle_cache",
2675 sizeof(struct btrfs_trans_handle),
2676 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002677 if (!btrfs_trans_handle_cachep)
2678 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002679 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002680 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002681 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002682 if (!btrfs_transaction_cachep)
2683 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002684 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002685 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002686 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002687 if (!btrfs_path_cachep)
2688 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002689 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002690 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002691 if (!btrfs_bit_radix_cachep)
2692 goto fail;
2693 return 0;
2694fail:
2695 btrfs_destroy_cachep();
2696 return -ENOMEM;
2697}
2698
2699static int btrfs_getattr(struct vfsmount *mnt,
2700 struct dentry *dentry, struct kstat *stat)
2701{
2702 struct inode *inode = dentry->d_inode;
2703 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002704 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002705 return 0;
2706}
2707
2708static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2709 struct inode * new_dir,struct dentry *new_dentry)
2710{
2711 struct btrfs_trans_handle *trans;
2712 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2713 struct inode *new_inode = new_dentry->d_inode;
2714 struct inode *old_inode = old_dentry->d_inode;
2715 struct timespec ctime = CURRENT_TIME;
2716 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002717 int ret;
2718
2719 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2720 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2721 return -ENOTEMPTY;
2722 }
Chris Mason5f39d392007-10-15 16:14:19 -04002723
Chris Mason39279cc2007-06-12 06:35:45 -04002724 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002725 ret = btrfs_check_free_space(root, 1, 0);
2726 if (ret)
2727 goto out_unlock;
2728
Chris Mason39279cc2007-06-12 06:35:45 -04002729 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002730
Chris Mason39279cc2007-06-12 06:35:45 -04002731 btrfs_set_trans_block_group(trans, new_dir);
2732 path = btrfs_alloc_path();
2733 if (!path) {
2734 ret = -ENOMEM;
2735 goto out_fail;
2736 }
2737
2738 old_dentry->d_inode->i_nlink++;
2739 old_dir->i_ctime = old_dir->i_mtime = ctime;
2740 new_dir->i_ctime = new_dir->i_mtime = ctime;
2741 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002742
Chris Mason39279cc2007-06-12 06:35:45 -04002743 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2744 if (ret)
2745 goto out_fail;
2746
2747 if (new_inode) {
2748 new_inode->i_ctime = CURRENT_TIME;
2749 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2750 if (ret)
2751 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002752 }
2753 ret = btrfs_add_link(trans, new_dentry, old_inode);
2754 if (ret)
2755 goto out_fail;
2756
2757out_fail:
2758 btrfs_free_path(path);
2759 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002760out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002761 mutex_unlock(&root->fs_info->fs_mutex);
2762 return ret;
2763}
2764
2765static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2766 const char *symname)
2767{
2768 struct btrfs_trans_handle *trans;
2769 struct btrfs_root *root = BTRFS_I(dir)->root;
2770 struct btrfs_path *path;
2771 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002772 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002773 int err;
2774 int drop_inode = 0;
2775 u64 objectid;
2776 int name_len;
2777 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002778 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002779 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002780 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002781 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002782
2783 name_len = strlen(symname) + 1;
2784 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2785 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002786
Chris Mason39279cc2007-06-12 06:35:45 -04002787 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002788 err = btrfs_check_free_space(root, 1, 0);
2789 if (err)
2790 goto out_fail;
2791
Chris Mason39279cc2007-06-12 06:35:45 -04002792 trans = btrfs_start_transaction(root, 1);
2793 btrfs_set_trans_block_group(trans, dir);
2794
2795 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2796 if (err) {
2797 err = -ENOSPC;
2798 goto out_unlock;
2799 }
2800
2801 inode = btrfs_new_inode(trans, root, objectid,
2802 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2803 err = PTR_ERR(inode);
2804 if (IS_ERR(inode))
2805 goto out_unlock;
2806
2807 btrfs_set_trans_block_group(trans, inode);
2808 err = btrfs_add_nondir(trans, dentry, inode);
2809 if (err)
2810 drop_inode = 1;
2811 else {
2812 inode->i_mapping->a_ops = &btrfs_aops;
2813 inode->i_fop = &btrfs_file_operations;
2814 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002815 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2816 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002817 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002818 }
2819 dir->i_sb->s_dirt = 1;
2820 btrfs_update_inode_block_group(trans, inode);
2821 btrfs_update_inode_block_group(trans, dir);
2822 if (drop_inode)
2823 goto out_unlock;
2824
2825 path = btrfs_alloc_path();
2826 BUG_ON(!path);
2827 key.objectid = inode->i_ino;
2828 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002829 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2830 datasize = btrfs_file_extent_calc_inline_size(name_len);
2831 err = btrfs_insert_empty_item(trans, root, path, &key,
2832 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002833 if (err) {
2834 drop_inode = 1;
2835 goto out_unlock;
2836 }
Chris Mason5f39d392007-10-15 16:14:19 -04002837 leaf = path->nodes[0];
2838 ei = btrfs_item_ptr(leaf, path->slots[0],
2839 struct btrfs_file_extent_item);
2840 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2841 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002842 BTRFS_FILE_EXTENT_INLINE);
2843 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002844 write_extent_buffer(leaf, symname, ptr, name_len);
2845 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002846 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002847
Chris Mason39279cc2007-06-12 06:35:45 -04002848 inode->i_op = &btrfs_symlink_inode_operations;
2849 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2850 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002851 err = btrfs_update_inode(trans, root, inode);
2852 if (err)
2853 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002854
2855out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002856 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002857 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002858out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002859 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002860 if (drop_inode) {
2861 inode_dec_link_count(inode);
2862 iput(inode);
2863 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002864 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002865 return err;
2866}
2867
2868static struct inode_operations btrfs_dir_inode_operations = {
2869 .lookup = btrfs_lookup,
2870 .create = btrfs_create,
2871 .unlink = btrfs_unlink,
2872 .link = btrfs_link,
2873 .mkdir = btrfs_mkdir,
2874 .rmdir = btrfs_rmdir,
2875 .rename = btrfs_rename,
2876 .symlink = btrfs_symlink,
2877 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002878 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002879 .setxattr = generic_setxattr,
2880 .getxattr = generic_getxattr,
2881 .listxattr = btrfs_listxattr,
2882 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002883};
2884
2885static struct inode_operations btrfs_dir_ro_inode_operations = {
2886 .lookup = btrfs_lookup,
2887};
2888
2889static struct file_operations btrfs_dir_file_operations = {
2890 .llseek = generic_file_llseek,
2891 .read = generic_read_dir,
2892 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002893 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002894#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002895 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002896#endif
2897};
2898
Chris Mason07157aa2007-08-30 08:50:51 -04002899static struct extent_map_ops btrfs_extent_map_ops = {
2900 .fill_delalloc = run_delalloc_range,
2901 .writepage_io_hook = btrfs_writepage_io_hook,
2902 .readpage_io_hook = btrfs_readpage_io_hook,
2903 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2904};
2905
Chris Mason39279cc2007-06-12 06:35:45 -04002906static struct address_space_operations btrfs_aops = {
2907 .readpage = btrfs_readpage,
2908 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002909 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002910 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002911 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002912 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002913 .invalidatepage = btrfs_invalidatepage,
2914 .releasepage = btrfs_releasepage,
2915 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002916};
2917
2918static struct address_space_operations btrfs_symlink_aops = {
2919 .readpage = btrfs_readpage,
2920 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002921 .invalidatepage = btrfs_invalidatepage,
2922 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002923};
2924
2925static struct inode_operations btrfs_file_inode_operations = {
2926 .truncate = btrfs_truncate,
2927 .getattr = btrfs_getattr,
2928 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002929 .setxattr = generic_setxattr,
2930 .getxattr = generic_getxattr,
2931 .listxattr = btrfs_listxattr,
2932 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002933};
2934
Josef Bacik618e21d2007-07-11 10:18:17 -04002935static struct inode_operations btrfs_special_inode_operations = {
2936 .getattr = btrfs_getattr,
2937 .setattr = btrfs_setattr,
2938};
2939
Chris Mason39279cc2007-06-12 06:35:45 -04002940static struct inode_operations btrfs_symlink_inode_operations = {
2941 .readlink = generic_readlink,
2942 .follow_link = page_follow_link_light,
2943 .put_link = page_put_link,
2944};