blob: 2cb2dd32407e6390b00c6724876f16b78af1aa63 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Mason1832a6d2007-12-21 16:27:21 -050075int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
76 int for_del)
77{
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
80 u64 thresh;
81 int ret = 0;
82
83 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050084 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 else
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 85;
87
88 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050089
90 spin_lock(&root->fs_info->delalloc_lock);
91 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
92 ret = -ENOSPC;
93 spin_unlock(&root->fs_info->delalloc_lock);
94 return ret;
95}
96
Chris Masonbe20aa92007-12-17 20:14:01 -050097static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040098{
99 struct btrfs_root *root = BTRFS_I(inode)->root;
100 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400101 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400102 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500103 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -0500105 struct btrfs_key ins;
106 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400107
Chris Masonb888db22007-08-27 16:49:44 -0400108 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400109 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500110 btrfs_set_trans_block_group(trans, inode);
111
Chris Masondb945352007-10-15 16:15:53 -0400112 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500113 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400114 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400115 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -0400116
Chris Mason179e29e2007-11-01 11:28:41 -0400117 if (alloc_hint == EXTENT_MAP_INLINE)
118 goto out;
119
Chris Masonc59f8952007-12-17 20:14:04 -0500120 while(num_bytes > 0) {
121 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
122 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
123 root->root_key.objectid,
124 trans->transid,
125 inode->i_ino, start, 0,
126 alloc_hint, (u64)-1, &ins, 1);
127 if (ret) {
128 WARN_ON(1);
129 goto out;
130 }
131 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
132 start, ins.objectid, ins.offset,
133 ins.offset);
134 num_bytes -= cur_alloc_size;
135 alloc_hint = ins.objectid + ins.offset;
136 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400137 }
Chris Masondc17ff82008-01-08 15:46:30 -0500138 btrfs_add_ordered_inode(inode);
Chris Masonb888db22007-08-27 16:49:44 -0400139out:
140 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500141 return ret;
142}
143
144static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
145{
146 u64 extent_start;
147 u64 extent_end;
148 u64 bytenr;
149 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500150 u64 loops = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500151 struct btrfs_root *root = BTRFS_I(inode)->root;
152 struct extent_buffer *leaf;
153 int found_type;
154 struct btrfs_path *path;
155 struct btrfs_file_extent_item *item;
156 int ret;
157 int err;
158 struct btrfs_key found_key;
159
160 path = btrfs_alloc_path();
161 BUG_ON(!path);
162again:
163 ret = btrfs_lookup_file_extent(NULL, root, path,
164 inode->i_ino, start, 0);
165 if (ret < 0) {
166 btrfs_free_path(path);
167 return ret;
168 }
169
170 cow_end = end;
171 if (ret != 0) {
172 if (path->slots[0] == 0)
173 goto not_found;
174 path->slots[0]--;
175 }
176
177 leaf = path->nodes[0];
178 item = btrfs_item_ptr(leaf, path->slots[0],
179 struct btrfs_file_extent_item);
180
181 /* are we inside the extent that was found? */
182 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
183 found_type = btrfs_key_type(&found_key);
184 if (found_key.objectid != inode->i_ino ||
185 found_type != BTRFS_EXTENT_DATA_KEY) {
186 goto not_found;
187 }
188
189 found_type = btrfs_file_extent_type(leaf, item);
190 extent_start = found_key.offset;
191 if (found_type == BTRFS_FILE_EXTENT_REG) {
192 extent_end = extent_start +
193 btrfs_file_extent_num_bytes(leaf, item);
194 err = 0;
195
Chris Mason1832a6d2007-12-21 16:27:21 -0500196 if (loops && start != extent_start)
197 goto not_found;
198
Chris Masonbe20aa92007-12-17 20:14:01 -0500199 if (start < extent_start || start >= extent_end)
200 goto not_found;
201
202 cow_end = min(end, extent_end - 1);
203 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
204 if (bytenr == 0)
205 goto not_found;
206
Chris Masonbe20aa92007-12-17 20:14:01 -0500207 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
208 goto not_found;
209 }
210
211 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500212 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 goto not_found;
214 }
215loop:
216 if (start > end) {
217 btrfs_free_path(path);
218 return 0;
219 }
220 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500221 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 goto again;
223
224not_found:
225 cow_file_range(inode, start, cow_end);
226 start = cow_end + 1;
227 goto loop;
228}
229
230static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
231{
232 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500233 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 int ret;
235
236 mutex_lock(&root->fs_info->fs_mutex);
237 if (btrfs_test_opt(root, NODATACOW))
238 ret = run_delalloc_nocow(inode, start, end);
239 else
240 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500241
242 spin_lock(&root->fs_info->delalloc_lock);
243 num_bytes = end + 1 - start;
244 if (root->fs_info->delalloc_bytes < num_bytes) {
245 printk("delalloc accounting error total %llu sub %llu\n",
246 root->fs_info->delalloc_bytes, num_bytes);
247 } else {
248 root->fs_info->delalloc_bytes -= num_bytes;
249 }
250 spin_unlock(&root->fs_info->delalloc_lock);
251
Chris Masonb888db22007-08-27 16:49:44 -0400252 mutex_unlock(&root->fs_info->fs_mutex);
253 return ret;
254}
255
Chris Mason07157aa2007-08-30 08:50:51 -0400256int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
257{
258 struct inode *inode = page->mapping->host;
259 struct btrfs_root *root = BTRFS_I(inode)->root;
260 struct btrfs_trans_handle *trans;
261 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500262 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400263 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400264 size_t offset = start - page_start;
265
Chris Masonb6cda9b2007-12-14 15:30:32 -0500266 if (btrfs_test_opt(root, NODATASUM))
267 return 0;
268
Chris Mason07157aa2007-08-30 08:50:51 -0400269 mutex_lock(&root->fs_info->fs_mutex);
270 trans = btrfs_start_transaction(root, 1);
271 btrfs_set_trans_block_group(trans, inode);
272 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400273 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400274 start, kaddr + offset, end - start + 1);
275 kunmap(page);
276 ret = btrfs_end_transaction(trans, root);
277 BUG_ON(ret);
278 mutex_unlock(&root->fs_info->fs_mutex);
279 return ret;
280}
281
282int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
283{
284 int ret = 0;
285 struct inode *inode = page->mapping->host;
286 struct btrfs_root *root = BTRFS_I(inode)->root;
287 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
288 struct btrfs_csum_item *item;
289 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400290 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400291
Chris Masonb6cda9b2007-12-14 15:30:32 -0500292 if (btrfs_test_opt(root, NODATASUM))
293 return 0;
294
Chris Mason07157aa2007-08-30 08:50:51 -0400295 mutex_lock(&root->fs_info->fs_mutex);
296 path = btrfs_alloc_path();
297 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
298 if (IS_ERR(item)) {
299 ret = PTR_ERR(item);
300 /* a csum that isn't present is a preallocated region. */
301 if (ret == -ENOENT || ret == -EFBIG)
302 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400303 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400304 goto out;
305 }
Chris Masonff79f812007-10-15 16:22:25 -0400306 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
307 BTRFS_CRC32_SIZE);
308 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400309out:
310 if (path)
311 btrfs_free_path(path);
312 mutex_unlock(&root->fs_info->fs_mutex);
313 return ret;
314}
315
316int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
317{
Chris Mason35ebb932007-10-30 16:56:53 -0400318 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400319 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400320 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
321 char *kaddr;
322 u64 private;
323 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400324 struct btrfs_root *root = BTRFS_I(inode)->root;
325 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400326 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400327
Chris Masonb6cda9b2007-12-14 15:30:32 -0500328 if (btrfs_test_opt(root, NODATASUM))
329 return 0;
330
Chris Mason07157aa2007-08-30 08:50:51 -0400331 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400332 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400333 kaddr = kmap_atomic(page, KM_IRQ0);
334 if (ret) {
335 goto zeroit;
336 }
Chris Masonff79f812007-10-15 16:22:25 -0400337 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
338 btrfs_csum_final(csum, (char *)&csum);
339 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400340 goto zeroit;
341 }
342 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400343 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400344 return 0;
345
346zeroit:
347 printk("btrfs csum failed ino %lu off %llu\n",
348 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400349 memset(kaddr + offset, 1, end - start + 1);
350 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400351 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400352 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400353 return 0;
354}
Chris Masonb888db22007-08-27 16:49:44 -0400355
Chris Mason39279cc2007-06-12 06:35:45 -0400356void btrfs_read_locked_inode(struct inode *inode)
357{
358 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400359 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400360 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400361 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400362 struct btrfs_root *root = BTRFS_I(inode)->root;
363 struct btrfs_key location;
364 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400365 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400366 int ret;
367
368 path = btrfs_alloc_path();
369 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400370 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400371 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500372
Chris Mason39279cc2007-06-12 06:35:45 -0400373 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400374 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400375 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400376
Chris Mason5f39d392007-10-15 16:14:19 -0400377 leaf = path->nodes[0];
378 inode_item = btrfs_item_ptr(leaf, path->slots[0],
379 struct btrfs_inode_item);
380
381 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
382 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
383 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
384 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
385 inode->i_size = btrfs_inode_size(leaf, inode_item);
386
387 tspec = btrfs_inode_atime(inode_item);
388 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
389 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
390
391 tspec = btrfs_inode_mtime(inode_item);
392 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
393 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
394
395 tspec = btrfs_inode_ctime(inode_item);
396 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
397 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
398
399 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
400 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400401 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400402 rdev = btrfs_inode_rdev(leaf, inode_item);
403
404 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400405 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
406 alloc_group_block);
407
Chris Masone52ec0e2007-12-21 16:36:24 -0500408 if (!BTRFS_I(inode)->block_group) {
409 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
410 NULL, 0, 0, 0);
411 }
Chris Mason39279cc2007-06-12 06:35:45 -0400412 btrfs_free_path(path);
413 inode_item = NULL;
414
415 mutex_unlock(&root->fs_info->fs_mutex);
416
417 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400418 case S_IFREG:
419 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400420 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400421 inode->i_fop = &btrfs_file_operations;
422 inode->i_op = &btrfs_file_inode_operations;
423 break;
424 case S_IFDIR:
425 inode->i_fop = &btrfs_dir_file_operations;
426 if (root == root->fs_info->tree_root)
427 inode->i_op = &btrfs_dir_ro_inode_operations;
428 else
429 inode->i_op = &btrfs_dir_inode_operations;
430 break;
431 case S_IFLNK:
432 inode->i_op = &btrfs_symlink_inode_operations;
433 inode->i_mapping->a_ops = &btrfs_symlink_aops;
434 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400435 default:
436 init_special_inode(inode, inode->i_mode, rdev);
437 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400438 }
439 return;
440
441make_bad:
442 btrfs_release_path(root, path);
443 btrfs_free_path(path);
444 mutex_unlock(&root->fs_info->fs_mutex);
445 make_bad_inode(inode);
446}
447
Chris Mason5f39d392007-10-15 16:14:19 -0400448static void fill_inode_item(struct extent_buffer *leaf,
449 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400450 struct inode *inode)
451{
Chris Mason5f39d392007-10-15 16:14:19 -0400452 btrfs_set_inode_uid(leaf, item, inode->i_uid);
453 btrfs_set_inode_gid(leaf, item, inode->i_gid);
454 btrfs_set_inode_size(leaf, item, inode->i_size);
455 btrfs_set_inode_mode(leaf, item, inode->i_mode);
456 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
457
458 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
459 inode->i_atime.tv_sec);
460 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
461 inode->i_atime.tv_nsec);
462
463 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
464 inode->i_mtime.tv_sec);
465 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
466 inode->i_mtime.tv_nsec);
467
468 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
469 inode->i_ctime.tv_sec);
470 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
471 inode->i_ctime.tv_nsec);
472
473 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
474 btrfs_set_inode_generation(leaf, item, inode->i_generation);
475 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
476 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400477 BTRFS_I(inode)->block_group->key.objectid);
478}
479
Chris Masona52d9a82007-08-27 16:49:44 -0400480int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400481 struct btrfs_root *root,
482 struct inode *inode)
483{
484 struct btrfs_inode_item *inode_item;
485 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400486 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400487 int ret;
488
489 path = btrfs_alloc_path();
490 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400491 ret = btrfs_lookup_inode(trans, root, path,
492 &BTRFS_I(inode)->location, 1);
493 if (ret) {
494 if (ret > 0)
495 ret = -ENOENT;
496 goto failed;
497 }
498
Chris Mason5f39d392007-10-15 16:14:19 -0400499 leaf = path->nodes[0];
500 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400501 struct btrfs_inode_item);
502
Chris Mason5f39d392007-10-15 16:14:19 -0400503 fill_inode_item(leaf, inode_item, inode);
504 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400505 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400506 ret = 0;
507failed:
508 btrfs_release_path(root, path);
509 btrfs_free_path(path);
510 return ret;
511}
512
513
514static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
515 struct btrfs_root *root,
516 struct inode *dir,
517 struct dentry *dentry)
518{
519 struct btrfs_path *path;
520 const char *name = dentry->d_name.name;
521 int name_len = dentry->d_name.len;
522 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400523 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400524 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400525 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400526
527 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400528 if (!path) {
529 ret = -ENOMEM;
530 goto err;
531 }
532
Chris Mason39279cc2007-06-12 06:35:45 -0400533 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
534 name, name_len, -1);
535 if (IS_ERR(di)) {
536 ret = PTR_ERR(di);
537 goto err;
538 }
539 if (!di) {
540 ret = -ENOENT;
541 goto err;
542 }
Chris Mason5f39d392007-10-15 16:14:19 -0400543 leaf = path->nodes[0];
544 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400545 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400546 if (ret)
547 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400548 btrfs_release_path(root, path);
549
550 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400551 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400552 if (IS_ERR(di)) {
553 ret = PTR_ERR(di);
554 goto err;
555 }
556 if (!di) {
557 ret = -ENOENT;
558 goto err;
559 }
560 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400561
562 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500563 ret = btrfs_del_inode_ref(trans, root, name, name_len,
564 dentry->d_inode->i_ino,
565 dentry->d_parent->d_inode->i_ino);
566 if (ret) {
567 printk("failed to delete reference to %.*s, "
568 "inode %lu parent %lu\n", name_len, name,
569 dentry->d_inode->i_ino,
570 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500571 }
Chris Mason39279cc2007-06-12 06:35:45 -0400572err:
573 btrfs_free_path(path);
574 if (!ret) {
575 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400576 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400577 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500578#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
579 dentry->d_inode->i_nlink--;
580#else
Chris Mason39279cc2007-06-12 06:35:45 -0400581 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500582#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400583 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400584 dir->i_sb->s_dirt = 1;
585 }
586 return ret;
587}
588
589static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
590{
591 struct btrfs_root *root;
592 struct btrfs_trans_handle *trans;
593 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500594 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400595
596 root = BTRFS_I(dir)->root;
597 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500598
599 ret = btrfs_check_free_space(root, 1, 1);
600 if (ret)
601 goto fail;
602
Chris Mason39279cc2007-06-12 06:35:45 -0400603 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400604
Chris Mason39279cc2007-06-12 06:35:45 -0400605 btrfs_set_trans_block_group(trans, dir);
606 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400607 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400608
Chris Mason39279cc2007-06-12 06:35:45 -0400609 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500610fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400611 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400612 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500613 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400614 return ret;
615}
616
617static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
618{
619 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500620 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400621 int ret;
622 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400623 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500624 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400625
Yan134d4512007-10-25 15:49:25 -0400626 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
627 return -ENOTEMPTY;
628
Chris Mason39279cc2007-06-12 06:35:45 -0400629 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500630 ret = btrfs_check_free_space(root, 1, 1);
631 if (ret)
632 goto fail;
633
Chris Mason39279cc2007-06-12 06:35:45 -0400634 trans = btrfs_start_transaction(root, 1);
635 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400636
637 /* now the directory is empty */
638 err = btrfs_unlink_trans(trans, root, dir, dentry);
639 if (!err) {
640 inode->i_size = 0;
641 }
Chris Mason39544012007-12-12 14:38:19 -0500642
Chris Masond3c2fdc2007-09-17 10:58:06 -0400643 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400644 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500645fail:
Yan134d4512007-10-25 15:49:25 -0400646 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400647 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500648 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500649
Chris Mason39279cc2007-06-12 06:35:45 -0400650 if (ret && !err)
651 err = ret;
652 return err;
653}
654
655static int btrfs_free_inode(struct btrfs_trans_handle *trans,
656 struct btrfs_root *root,
657 struct inode *inode)
658{
659 struct btrfs_path *path;
660 int ret;
661
662 clear_inode(inode);
663
664 path = btrfs_alloc_path();
665 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400666 ret = btrfs_lookup_inode(trans, root, path,
667 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400668 if (ret > 0)
669 ret = -ENOENT;
670 if (!ret)
671 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400672 btrfs_free_path(path);
673 return ret;
674}
675
676/*
Chris Mason39279cc2007-06-12 06:35:45 -0400677 * this can truncate away extent items, csum items and directory items.
678 * It starts at a high offset and removes keys until it can't find
679 * any higher than i_size.
680 *
681 * csum items that cross the new i_size are truncated to the new size
682 * as well.
683 */
684static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
685 struct btrfs_root *root,
686 struct inode *inode)
687{
688 int ret;
689 struct btrfs_path *path;
690 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400691 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400692 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400693 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400694 struct btrfs_file_extent_item *fi;
695 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400696 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400697 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500698 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500699 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400700 int found_extent;
701 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400702 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400703
Chris Masona52d9a82007-08-27 16:49:44 -0400704 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400705 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400706 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400707 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400708
Chris Mason39279cc2007-06-12 06:35:45 -0400709 /* FIXME, add redo link to tree so we don't leak on crash */
710 key.objectid = inode->i_ino;
711 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400712 key.type = (u8)-1;
713
Chris Mason39279cc2007-06-12 06:35:45 -0400714 while(1) {
715 btrfs_init_path(path);
716 fi = NULL;
717 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
718 if (ret < 0) {
719 goto error;
720 }
721 if (ret > 0) {
722 BUG_ON(path->slots[0] == 0);
723 path->slots[0]--;
724 }
Chris Mason5f39d392007-10-15 16:14:19 -0400725 leaf = path->nodes[0];
726 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
727 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400728
Chris Mason5f39d392007-10-15 16:14:19 -0400729 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400730 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400731
Chris Mason39279cc2007-06-12 06:35:45 -0400732 if (found_type != BTRFS_CSUM_ITEM_KEY &&
733 found_type != BTRFS_DIR_ITEM_KEY &&
734 found_type != BTRFS_DIR_INDEX_KEY &&
735 found_type != BTRFS_EXTENT_DATA_KEY)
736 break;
737
Chris Mason5f39d392007-10-15 16:14:19 -0400738 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400739 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400740 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400741 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400742 extent_type = btrfs_file_extent_type(leaf, fi);
743 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400744 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400745 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400746 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
747 struct btrfs_item *item = btrfs_item_nr(leaf,
748 path->slots[0]);
749 item_end += btrfs_file_extent_inline_len(leaf,
750 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400751 }
Yan008630c2007-11-07 13:31:09 -0500752 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400753 }
754 if (found_type == BTRFS_CSUM_ITEM_KEY) {
755 ret = btrfs_csum_truncate(trans, root, path,
756 inode->i_size);
757 BUG_ON(ret);
758 }
Yan008630c2007-11-07 13:31:09 -0500759 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400760 if (found_type == BTRFS_DIR_ITEM_KEY) {
761 found_type = BTRFS_INODE_ITEM_KEY;
762 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
763 found_type = BTRFS_CSUM_ITEM_KEY;
764 } else if (found_type) {
765 found_type--;
766 } else {
767 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400768 }
Yana61721d2007-09-17 11:08:38 -0400769 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400770 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400771 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400772 }
Chris Mason5f39d392007-10-15 16:14:19 -0400773 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400774 del_item = 1;
775 else
776 del_item = 0;
777 found_extent = 0;
778
779 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400780 if (found_type != BTRFS_EXTENT_DATA_KEY)
781 goto delete;
782
783 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400784 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400785 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400786 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400787 u64 orig_num_bytes =
788 btrfs_file_extent_num_bytes(leaf, fi);
789 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400790 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400791 btrfs_set_file_extent_num_bytes(leaf, fi,
792 extent_num_bytes);
793 num_dec = (orig_num_bytes -
794 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400795 if (extent_start != 0) {
796 inode->i_blocks -= num_dec;
797 }
Chris Mason5f39d392007-10-15 16:14:19 -0400798 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400799 } else {
Chris Masondb945352007-10-15 16:15:53 -0400800 extent_num_bytes =
801 btrfs_file_extent_disk_num_bytes(leaf,
802 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400803 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400804 num_dec = btrfs_file_extent_num_bytes(leaf,
805 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400806 if (extent_start != 0) {
807 found_extent = 1;
808 inode->i_blocks -= num_dec;
809 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500810 root_gen = btrfs_header_generation(leaf);
811 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400812 }
Chris Mason179e29e2007-11-01 11:28:41 -0400813 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
814 !del_item) {
815 u32 newsize = inode->i_size - found_key.offset;
816 newsize = btrfs_file_extent_calc_inline_size(newsize);
817 ret = btrfs_truncate_item(trans, root, path,
818 newsize, 1);
819 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400820 }
Chris Mason179e29e2007-11-01 11:28:41 -0400821delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400822 if (del_item) {
823 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400824 if (ret)
825 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400826 } else {
827 break;
828 }
829 btrfs_release_path(root, path);
830 if (found_extent) {
831 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500832 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500833 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500834 root_gen, inode->i_ino,
835 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400836 BUG_ON(ret);
837 }
838 }
839 ret = 0;
840error:
841 btrfs_release_path(root, path);
842 btrfs_free_path(path);
843 inode->i_sb->s_dirt = 1;
844 return ret;
845}
846
Chris Masonb888db22007-08-27 16:49:44 -0400847static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400848 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400849{
Chris Mason39279cc2007-06-12 06:35:45 -0400850 char *kaddr;
Chris Masonb888db22007-08-27 16:49:44 -0400851 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500852 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400853 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400854 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500855 u64 existing_delalloc;
856 u64 delalloc_start;
857 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400858
Chris Mason190662b2007-12-18 16:25:45 -0500859 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400860 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400861
Chris Masonb888db22007-08-27 16:49:44 -0400862 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500863 delalloc_start = page_start;
864 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
865 &delalloc_start, page_end,
866 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db22007-08-27 16:49:44 -0400867 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
868 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500869
870 spin_lock(&root->fs_info->delalloc_lock);
871 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
872 spin_unlock(&root->fs_info->delalloc_lock);
873
Chris Masona52d9a82007-08-27 16:49:44 -0400874 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400875 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400876 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
877 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400878 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400879 }
Chris Masonb888db22007-08-27 16:49:44 -0400880 set_page_dirty(page);
881 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400882
Chris Masona52d9a82007-08-27 16:49:44 -0400883 return ret;
884}
885
886/*
887 * taken from block_truncate_page, but does cow as it zeros out
888 * any bytes left in the last page in the file.
889 */
890static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
891{
892 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400893 struct btrfs_root *root = BTRFS_I(inode)->root;
894 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400895 pgoff_t index = from >> PAGE_CACHE_SHIFT;
896 unsigned offset = from & (PAGE_CACHE_SIZE-1);
897 struct page *page;
898 int ret = 0;
899 u64 page_start;
900
901 if ((offset & (blocksize - 1)) == 0)
902 goto out;
903
904 ret = -ENOMEM;
905 page = grab_cache_page(mapping, index);
906 if (!page)
907 goto out;
908 if (!PageUptodate(page)) {
909 ret = btrfs_readpage(NULL, page);
910 lock_page(page);
911 if (!PageUptodate(page)) {
912 ret = -EIO;
913 goto out;
914 }
915 }
Chris Mason35ebb932007-10-30 16:56:53 -0400916 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400917
Chris Masonb888db22007-08-27 16:49:44 -0400918 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400919
Chris Mason39279cc2007-06-12 06:35:45 -0400920 unlock_page(page);
921 page_cache_release(page);
922out:
923 return ret;
924}
925
926static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
927{
928 struct inode *inode = dentry->d_inode;
929 int err;
930
931 err = inode_change_ok(inode, attr);
932 if (err)
933 return err;
934
935 if (S_ISREG(inode->i_mode) &&
936 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
937 struct btrfs_trans_handle *trans;
938 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400939 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
940
Chris Mason5f39d392007-10-15 16:14:19 -0400941 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400942 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400943 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400944 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400945 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400946
947 if (attr->ia_size <= pos)
948 goto out;
949
Chris Mason1832a6d2007-12-21 16:27:21 -0500950 mutex_lock(&root->fs_info->fs_mutex);
951 err = btrfs_check_free_space(root, 1, 0);
952 mutex_unlock(&root->fs_info->fs_mutex);
953 if (err)
954 goto fail;
955
Chris Mason39279cc2007-06-12 06:35:45 -0400956 btrfs_truncate_page(inode->i_mapping, inode->i_size);
957
Chris Mason2bf5a722007-08-30 11:54:02 -0400958 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400959 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400960
961 mutex_lock(&root->fs_info->fs_mutex);
962 trans = btrfs_start_transaction(root, 1);
963 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400964 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400965 pos, pos + hole_size, pos,
966 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400967
Chris Mason179e29e2007-11-01 11:28:41 -0400968 if (alloc_hint != EXTENT_MAP_INLINE) {
969 err = btrfs_insert_file_extent(trans, root,
970 inode->i_ino,
971 pos, 0, 0, hole_size);
972 }
Chris Mason39279cc2007-06-12 06:35:45 -0400973 btrfs_end_transaction(trans, root);
974 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400975 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400976 if (err)
977 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400978 }
979out:
980 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -0500981fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400982 return err;
983}
984void btrfs_delete_inode(struct inode *inode)
985{
986 struct btrfs_trans_handle *trans;
987 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -0400988 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400989 int ret;
990
991 truncate_inode_pages(&inode->i_data, 0);
992 if (is_bad_inode(inode)) {
993 goto no_delete;
994 }
Chris Mason5f39d392007-10-15 16:14:19 -0400995
Chris Mason39279cc2007-06-12 06:35:45 -0400996 inode->i_size = 0;
997 mutex_lock(&root->fs_info->fs_mutex);
998 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400999
Chris Mason39279cc2007-06-12 06:35:45 -04001000 btrfs_set_trans_block_group(trans, inode);
1001 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001002 if (ret)
1003 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001004 ret = btrfs_delete_xattrs(trans, root, inode);
1005 if (ret)
1006 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 ret = btrfs_free_inode(trans, root, inode);
1008 if (ret)
1009 goto no_delete_lock;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001010 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001011
Chris Mason39279cc2007-06-12 06:35:45 -04001012 btrfs_end_transaction(trans, root);
1013 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001014 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001015 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001016 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001017
1018no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001019 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001020 btrfs_end_transaction(trans, root);
1021 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001022 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001023 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001024no_delete:
1025 clear_inode(inode);
1026}
1027
1028/*
1029 * this returns the key found in the dir entry in the location pointer.
1030 * If no dir entries were found, location->objectid is 0.
1031 */
1032static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1033 struct btrfs_key *location)
1034{
1035 const char *name = dentry->d_name.name;
1036 int namelen = dentry->d_name.len;
1037 struct btrfs_dir_item *di;
1038 struct btrfs_path *path;
1039 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001040 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001041
Chris Mason39544012007-12-12 14:38:19 -05001042 if (namelen == 1 && strcmp(name, ".") == 0) {
1043 location->objectid = dir->i_ino;
1044 location->type = BTRFS_INODE_ITEM_KEY;
1045 location->offset = 0;
1046 return 0;
1047 }
Chris Mason39279cc2007-06-12 06:35:45 -04001048 path = btrfs_alloc_path();
1049 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001050
Chris Mason7a720532007-12-13 09:06:59 -05001051 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001052 struct btrfs_key key;
1053 struct extent_buffer *leaf;
1054 u32 nritems;
1055 int slot;
1056
1057 key.objectid = dir->i_ino;
1058 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1059 key.offset = 0;
1060 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1061 BUG_ON(ret == 0);
1062 ret = 0;
1063
1064 leaf = path->nodes[0];
1065 slot = path->slots[0];
1066 nritems = btrfs_header_nritems(leaf);
1067 if (slot >= nritems)
1068 goto out_err;
1069
1070 btrfs_item_key_to_cpu(leaf, &key, slot);
1071 if (key.objectid != dir->i_ino ||
1072 key.type != BTRFS_INODE_REF_KEY) {
1073 goto out_err;
1074 }
1075 location->objectid = key.offset;
1076 location->type = BTRFS_INODE_ITEM_KEY;
1077 location->offset = 0;
1078 goto out;
1079 }
1080
Chris Mason39279cc2007-06-12 06:35:45 -04001081 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1082 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001083 if (IS_ERR(di))
1084 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001085 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001086 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001087 }
Chris Mason5f39d392007-10-15 16:14:19 -04001088 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001089out:
Chris Mason39279cc2007-06-12 06:35:45 -04001090 btrfs_free_path(path);
1091 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001092out_err:
1093 location->objectid = 0;
1094 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001095}
1096
1097/*
1098 * when we hit a tree root in a directory, the btrfs part of the inode
1099 * needs to be changed to reflect the root directory of the tree root. This
1100 * is kind of like crossing a mount point.
1101 */
1102static int fixup_tree_root_location(struct btrfs_root *root,
1103 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001104 struct btrfs_root **sub_root,
1105 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001106{
1107 struct btrfs_path *path;
1108 struct btrfs_root_item *ri;
1109
1110 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1111 return 0;
1112 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1113 return 0;
1114
1115 path = btrfs_alloc_path();
1116 BUG_ON(!path);
1117 mutex_lock(&root->fs_info->fs_mutex);
1118
Josef Bacik58176a92007-08-29 15:47:34 -04001119 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1120 dentry->d_name.name,
1121 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001122 if (IS_ERR(*sub_root))
1123 return PTR_ERR(*sub_root);
1124
1125 ri = &(*sub_root)->root_item;
1126 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001127 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1128 location->offset = 0;
1129
1130 btrfs_free_path(path);
1131 mutex_unlock(&root->fs_info->fs_mutex);
1132 return 0;
1133}
1134
1135static int btrfs_init_locked_inode(struct inode *inode, void *p)
1136{
1137 struct btrfs_iget_args *args = p;
1138 inode->i_ino = args->ino;
1139 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001140 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1141 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001142 return 0;
1143}
1144
1145static int btrfs_find_actor(struct inode *inode, void *opaque)
1146{
1147 struct btrfs_iget_args *args = opaque;
1148 return (args->ino == inode->i_ino &&
1149 args->root == BTRFS_I(inode)->root);
1150}
1151
Chris Masondc17ff82008-01-08 15:46:30 -05001152struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1153 u64 root_objectid)
1154{
1155 struct btrfs_iget_args args;
1156 args.ino = objectid;
1157 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1158
1159 if (!args.root)
1160 return NULL;
1161
1162 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1163}
1164
Chris Mason39279cc2007-06-12 06:35:45 -04001165struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1166 struct btrfs_root *root)
1167{
1168 struct inode *inode;
1169 struct btrfs_iget_args args;
1170 args.ino = objectid;
1171 args.root = root;
1172
1173 inode = iget5_locked(s, objectid, btrfs_find_actor,
1174 btrfs_init_locked_inode,
1175 (void *)&args);
1176 return inode;
1177}
1178
1179static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1180 struct nameidata *nd)
1181{
1182 struct inode * inode;
1183 struct btrfs_inode *bi = BTRFS_I(dir);
1184 struct btrfs_root *root = bi->root;
1185 struct btrfs_root *sub_root = root;
1186 struct btrfs_key location;
1187 int ret;
1188
1189 if (dentry->d_name.len > BTRFS_NAME_LEN)
1190 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001191
Chris Mason39279cc2007-06-12 06:35:45 -04001192 mutex_lock(&root->fs_info->fs_mutex);
1193 ret = btrfs_inode_by_name(dir, dentry, &location);
1194 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001195
Chris Mason39279cc2007-06-12 06:35:45 -04001196 if (ret < 0)
1197 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001198
Chris Mason39279cc2007-06-12 06:35:45 -04001199 inode = NULL;
1200 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001201 ret = fixup_tree_root_location(root, &location, &sub_root,
1202 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001203 if (ret < 0)
1204 return ERR_PTR(ret);
1205 if (ret > 0)
1206 return ERR_PTR(-ENOENT);
1207 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1208 sub_root);
1209 if (!inode)
1210 return ERR_PTR(-EACCES);
1211 if (inode->i_state & I_NEW) {
1212 /* the inode and parent dir are two different roots */
1213 if (sub_root != root) {
1214 igrab(inode);
1215 sub_root->inode = inode;
1216 }
1217 BTRFS_I(inode)->root = sub_root;
1218 memcpy(&BTRFS_I(inode)->location, &location,
1219 sizeof(location));
1220 btrfs_read_locked_inode(inode);
1221 unlock_new_inode(inode);
1222 }
1223 }
1224 return d_splice_alias(inode, dentry);
1225}
1226
Chris Mason39279cc2007-06-12 06:35:45 -04001227static unsigned char btrfs_filetype_table[] = {
1228 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1229};
1230
1231static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1232{
Chris Mason6da6aba2007-12-18 16:15:09 -05001233 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001234 struct btrfs_root *root = BTRFS_I(inode)->root;
1235 struct btrfs_item *item;
1236 struct btrfs_dir_item *di;
1237 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001238 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001239 struct btrfs_path *path;
1240 int ret;
1241 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001242 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001243 int slot;
1244 int advance;
1245 unsigned char d_type;
1246 int over = 0;
1247 u32 di_cur;
1248 u32 di_total;
1249 u32 di_len;
1250 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001251 char tmp_name[32];
1252 char *name_ptr;
1253 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001254
1255 /* FIXME, use a real flag for deciding about the key type */
1256 if (root->fs_info->tree_root == root)
1257 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001258
Chris Mason39544012007-12-12 14:38:19 -05001259 /* special case for "." */
1260 if (filp->f_pos == 0) {
1261 over = filldir(dirent, ".", 1,
1262 1, inode->i_ino,
1263 DT_DIR);
1264 if (over)
1265 return 0;
1266 filp->f_pos = 1;
1267 }
1268
Chris Mason39279cc2007-06-12 06:35:45 -04001269 mutex_lock(&root->fs_info->fs_mutex);
1270 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001271 path = btrfs_alloc_path();
1272 path->reada = 2;
1273
1274 /* special case for .., just use the back ref */
1275 if (filp->f_pos == 1) {
1276 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1277 key.offset = 0;
1278 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1279 BUG_ON(ret == 0);
1280 leaf = path->nodes[0];
1281 slot = path->slots[0];
1282 nritems = btrfs_header_nritems(leaf);
1283 if (slot >= nritems) {
1284 btrfs_release_path(root, path);
1285 goto read_dir_items;
1286 }
1287 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1288 btrfs_release_path(root, path);
1289 if (found_key.objectid != key.objectid ||
1290 found_key.type != BTRFS_INODE_REF_KEY)
1291 goto read_dir_items;
1292 over = filldir(dirent, "..", 2,
1293 2, found_key.offset, DT_DIR);
1294 if (over)
1295 goto nopos;
1296 filp->f_pos = 2;
1297 }
1298
1299read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001300 btrfs_set_key_type(&key, key_type);
1301 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001302
Chris Mason39279cc2007-06-12 06:35:45 -04001303 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1304 if (ret < 0)
1305 goto err;
1306 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001307 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001308 leaf = path->nodes[0];
1309 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001310 slot = path->slots[0];
1311 if (advance || slot >= nritems) {
1312 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001313 ret = btrfs_next_leaf(root, path);
1314 if (ret)
1315 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001316 leaf = path->nodes[0];
1317 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001318 slot = path->slots[0];
1319 } else {
1320 slot++;
1321 path->slots[0]++;
1322 }
1323 }
1324 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001325 item = btrfs_item_nr(leaf, slot);
1326 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1327
1328 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001329 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001330 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001331 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001332 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001333 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001334
1335 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001336 advance = 1;
1337 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1338 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001339 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001340 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001341 struct btrfs_key location;
1342
1343 name_len = btrfs_dir_name_len(leaf, di);
1344 if (name_len < 32) {
1345 name_ptr = tmp_name;
1346 } else {
1347 name_ptr = kmalloc(name_len, GFP_NOFS);
1348 BUG_ON(!name_ptr);
1349 }
1350 read_extent_buffer(leaf, name_ptr,
1351 (unsigned long)(di + 1), name_len);
1352
1353 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1354 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001355 over = filldir(dirent, name_ptr, name_len,
1356 found_key.offset,
1357 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001358 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001359
1360 if (name_ptr != tmp_name)
1361 kfree(name_ptr);
1362
Chris Mason39279cc2007-06-12 06:35:45 -04001363 if (over)
1364 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001365 di_len = btrfs_dir_name_len(leaf, di) +
1366 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001367 di_cur += di_len;
1368 di = (struct btrfs_dir_item *)((char *)di + di_len);
1369 }
1370 }
1371 filp->f_pos++;
1372nopos:
1373 ret = 0;
1374err:
1375 btrfs_release_path(root, path);
1376 btrfs_free_path(path);
1377 mutex_unlock(&root->fs_info->fs_mutex);
1378 return ret;
1379}
1380
1381int btrfs_write_inode(struct inode *inode, int wait)
1382{
1383 struct btrfs_root *root = BTRFS_I(inode)->root;
1384 struct btrfs_trans_handle *trans;
1385 int ret = 0;
1386
1387 if (wait) {
1388 mutex_lock(&root->fs_info->fs_mutex);
1389 trans = btrfs_start_transaction(root, 1);
1390 btrfs_set_trans_block_group(trans, inode);
1391 ret = btrfs_commit_transaction(trans, root);
1392 mutex_unlock(&root->fs_info->fs_mutex);
1393 }
1394 return ret;
1395}
1396
1397/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001398 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001399 * inode changes. But, it is most likely to find the inode in cache.
1400 * FIXME, needs more benchmarking...there are no reasons other than performance
1401 * to keep or drop this code.
1402 */
1403void btrfs_dirty_inode(struct inode *inode)
1404{
1405 struct btrfs_root *root = BTRFS_I(inode)->root;
1406 struct btrfs_trans_handle *trans;
1407
1408 mutex_lock(&root->fs_info->fs_mutex);
1409 trans = btrfs_start_transaction(root, 1);
1410 btrfs_set_trans_block_group(trans, inode);
1411 btrfs_update_inode(trans, root, inode);
1412 btrfs_end_transaction(trans, root);
1413 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001414}
1415
1416static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1417 struct btrfs_root *root,
1418 u64 objectid,
1419 struct btrfs_block_group_cache *group,
1420 int mode)
1421{
1422 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001423 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001424 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001425 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001426 int ret;
1427 int owner;
1428
Chris Mason5f39d392007-10-15 16:14:19 -04001429 path = btrfs_alloc_path();
1430 BUG_ON(!path);
1431
Chris Mason39279cc2007-06-12 06:35:45 -04001432 inode = new_inode(root->fs_info->sb);
1433 if (!inode)
1434 return ERR_PTR(-ENOMEM);
1435
Chris Masonb888db22007-08-27 16:49:44 -04001436 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1437 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001438 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001439
Chris Mason39279cc2007-06-12 06:35:45 -04001440 if (mode & S_IFDIR)
1441 owner = 0;
1442 else
1443 owner = 1;
1444 group = btrfs_find_block_group(root, group, 0, 0, owner);
1445 BTRFS_I(inode)->block_group = group;
1446
Chris Mason5f39d392007-10-15 16:14:19 -04001447 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1448 if (ret)
1449 goto fail;
1450
Chris Mason39279cc2007-06-12 06:35:45 -04001451 inode->i_uid = current->fsuid;
1452 inode->i_gid = current->fsgid;
1453 inode->i_mode = mode;
1454 inode->i_ino = objectid;
1455 inode->i_blocks = 0;
1456 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001457 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1458 struct btrfs_inode_item);
1459 fill_inode_item(path->nodes[0], inode_item, inode);
1460 btrfs_mark_buffer_dirty(path->nodes[0]);
1461 btrfs_free_path(path);
1462
Chris Mason39279cc2007-06-12 06:35:45 -04001463 location = &BTRFS_I(inode)->location;
1464 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001465 location->offset = 0;
1466 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1467
Chris Mason39279cc2007-06-12 06:35:45 -04001468 insert_inode_hash(inode);
1469 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001470fail:
1471 btrfs_free_path(path);
1472 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001473}
1474
1475static inline u8 btrfs_inode_type(struct inode *inode)
1476{
1477 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1478}
1479
1480static int btrfs_add_link(struct btrfs_trans_handle *trans,
1481 struct dentry *dentry, struct inode *inode)
1482{
1483 int ret;
1484 struct btrfs_key key;
1485 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001486 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001487
Chris Mason39279cc2007-06-12 06:35:45 -04001488 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001489 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1490 key.offset = 0;
1491
1492 ret = btrfs_insert_dir_item(trans, root,
1493 dentry->d_name.name, dentry->d_name.len,
1494 dentry->d_parent->d_inode->i_ino,
1495 &key, btrfs_inode_type(inode));
1496 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001497 ret = btrfs_insert_inode_ref(trans, root,
1498 dentry->d_name.name,
1499 dentry->d_name.len,
1500 inode->i_ino,
1501 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001502 parent_inode = dentry->d_parent->d_inode;
1503 parent_inode->i_size += dentry->d_name.len * 2;
1504 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001505 ret = btrfs_update_inode(trans, root,
1506 dentry->d_parent->d_inode);
1507 }
1508 return ret;
1509}
1510
1511static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1512 struct dentry *dentry, struct inode *inode)
1513{
1514 int err = btrfs_add_link(trans, dentry, inode);
1515 if (!err) {
1516 d_instantiate(dentry, inode);
1517 return 0;
1518 }
1519 if (err > 0)
1520 err = -EEXIST;
1521 return err;
1522}
1523
Josef Bacik618e21d2007-07-11 10:18:17 -04001524static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1525 int mode, dev_t rdev)
1526{
1527 struct btrfs_trans_handle *trans;
1528 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001529 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001530 int err;
1531 int drop_inode = 0;
1532 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001533 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001534
1535 if (!new_valid_dev(rdev))
1536 return -EINVAL;
1537
1538 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001539 err = btrfs_check_free_space(root, 1, 0);
1540 if (err)
1541 goto fail;
1542
Josef Bacik618e21d2007-07-11 10:18:17 -04001543 trans = btrfs_start_transaction(root, 1);
1544 btrfs_set_trans_block_group(trans, dir);
1545
1546 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1547 if (err) {
1548 err = -ENOSPC;
1549 goto out_unlock;
1550 }
1551
1552 inode = btrfs_new_inode(trans, root, objectid,
1553 BTRFS_I(dir)->block_group, mode);
1554 err = PTR_ERR(inode);
1555 if (IS_ERR(inode))
1556 goto out_unlock;
1557
1558 btrfs_set_trans_block_group(trans, inode);
1559 err = btrfs_add_nondir(trans, dentry, inode);
1560 if (err)
1561 drop_inode = 1;
1562 else {
1563 inode->i_op = &btrfs_special_inode_operations;
1564 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001565 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001566 }
1567 dir->i_sb->s_dirt = 1;
1568 btrfs_update_inode_block_group(trans, inode);
1569 btrfs_update_inode_block_group(trans, dir);
1570out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001571 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001572 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001573fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001574 mutex_unlock(&root->fs_info->fs_mutex);
1575
1576 if (drop_inode) {
1577 inode_dec_link_count(inode);
1578 iput(inode);
1579 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001580 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001581 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001582 return err;
1583}
1584
Chris Mason39279cc2007-06-12 06:35:45 -04001585static int btrfs_create(struct inode *dir, struct dentry *dentry,
1586 int mode, struct nameidata *nd)
1587{
1588 struct btrfs_trans_handle *trans;
1589 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001590 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001591 int err;
1592 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001593 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001594 u64 objectid;
1595
1596 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001597 err = btrfs_check_free_space(root, 1, 0);
1598 if (err)
1599 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001600 trans = btrfs_start_transaction(root, 1);
1601 btrfs_set_trans_block_group(trans, dir);
1602
1603 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1604 if (err) {
1605 err = -ENOSPC;
1606 goto out_unlock;
1607 }
1608
1609 inode = btrfs_new_inode(trans, root, objectid,
1610 BTRFS_I(dir)->block_group, mode);
1611 err = PTR_ERR(inode);
1612 if (IS_ERR(inode))
1613 goto out_unlock;
1614
1615 btrfs_set_trans_block_group(trans, inode);
1616 err = btrfs_add_nondir(trans, dentry, inode);
1617 if (err)
1618 drop_inode = 1;
1619 else {
1620 inode->i_mapping->a_ops = &btrfs_aops;
1621 inode->i_fop = &btrfs_file_operations;
1622 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001623 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1624 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001625 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001626 }
1627 dir->i_sb->s_dirt = 1;
1628 btrfs_update_inode_block_group(trans, inode);
1629 btrfs_update_inode_block_group(trans, dir);
1630out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001631 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001632 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001633fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001634 mutex_unlock(&root->fs_info->fs_mutex);
1635
1636 if (drop_inode) {
1637 inode_dec_link_count(inode);
1638 iput(inode);
1639 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001640 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001641 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001642 return err;
1643}
1644
1645static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1646 struct dentry *dentry)
1647{
1648 struct btrfs_trans_handle *trans;
1649 struct btrfs_root *root = BTRFS_I(dir)->root;
1650 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001651 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001652 int err;
1653 int drop_inode = 0;
1654
1655 if (inode->i_nlink == 0)
1656 return -ENOENT;
1657
Chris Mason6da6aba2007-12-18 16:15:09 -05001658#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1659 inode->i_nlink++;
1660#else
Chris Mason39279cc2007-06-12 06:35:45 -04001661 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001662#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001663 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001664 err = btrfs_check_free_space(root, 1, 0);
1665 if (err)
1666 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001667 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001668
Chris Mason39279cc2007-06-12 06:35:45 -04001669 btrfs_set_trans_block_group(trans, dir);
1670 atomic_inc(&inode->i_count);
1671 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001672
Chris Mason39279cc2007-06-12 06:35:45 -04001673 if (err)
1674 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001675
Chris Mason39279cc2007-06-12 06:35:45 -04001676 dir->i_sb->s_dirt = 1;
1677 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001678 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001679
Chris Mason54aa1f42007-06-22 14:16:25 -04001680 if (err)
1681 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001682
Chris Masond3c2fdc2007-09-17 10:58:06 -04001683 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001684 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001685fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001686 mutex_unlock(&root->fs_info->fs_mutex);
1687
1688 if (drop_inode) {
1689 inode_dec_link_count(inode);
1690 iput(inode);
1691 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001692 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001693 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001694 return err;
1695}
1696
Chris Mason39279cc2007-06-12 06:35:45 -04001697static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1698{
1699 struct inode *inode;
1700 struct btrfs_trans_handle *trans;
1701 struct btrfs_root *root = BTRFS_I(dir)->root;
1702 int err = 0;
1703 int drop_on_err = 0;
1704 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001705 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001706
1707 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001708 err = btrfs_check_free_space(root, 1, 0);
1709 if (err)
1710 goto out_unlock;
1711
Chris Mason39279cc2007-06-12 06:35:45 -04001712 trans = btrfs_start_transaction(root, 1);
1713 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001714
Chris Mason39279cc2007-06-12 06:35:45 -04001715 if (IS_ERR(trans)) {
1716 err = PTR_ERR(trans);
1717 goto out_unlock;
1718 }
1719
1720 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1721 if (err) {
1722 err = -ENOSPC;
1723 goto out_unlock;
1724 }
1725
1726 inode = btrfs_new_inode(trans, root, objectid,
1727 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1728 if (IS_ERR(inode)) {
1729 err = PTR_ERR(inode);
1730 goto out_fail;
1731 }
Chris Mason5f39d392007-10-15 16:14:19 -04001732
Chris Mason39279cc2007-06-12 06:35:45 -04001733 drop_on_err = 1;
1734 inode->i_op = &btrfs_dir_inode_operations;
1735 inode->i_fop = &btrfs_dir_file_operations;
1736 btrfs_set_trans_block_group(trans, inode);
1737
Chris Mason39544012007-12-12 14:38:19 -05001738 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001739 err = btrfs_update_inode(trans, root, inode);
1740 if (err)
1741 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001742
Chris Mason39279cc2007-06-12 06:35:45 -04001743 err = btrfs_add_link(trans, dentry, inode);
1744 if (err)
1745 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001746
Chris Mason39279cc2007-06-12 06:35:45 -04001747 d_instantiate(dentry, inode);
1748 drop_on_err = 0;
1749 dir->i_sb->s_dirt = 1;
1750 btrfs_update_inode_block_group(trans, inode);
1751 btrfs_update_inode_block_group(trans, dir);
1752
1753out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001754 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001755 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001756
Chris Mason39279cc2007-06-12 06:35:45 -04001757out_unlock:
1758 mutex_unlock(&root->fs_info->fs_mutex);
1759 if (drop_on_err)
1760 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001761 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001762 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001763 return err;
1764}
1765
Chris Masona52d9a82007-08-27 16:49:44 -04001766struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1767 size_t page_offset, u64 start, u64 end,
1768 int create)
1769{
1770 int ret;
1771 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001772 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001773 u64 extent_start = 0;
1774 u64 extent_end = 0;
1775 u64 objectid = inode->i_ino;
1776 u32 found_type;
1777 int failed_insert = 0;
1778 struct btrfs_path *path;
1779 struct btrfs_root *root = BTRFS_I(inode)->root;
1780 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001781 struct extent_buffer *leaf;
1782 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001783 struct extent_map *em = NULL;
1784 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1785 struct btrfs_trans_handle *trans = NULL;
1786
1787 path = btrfs_alloc_path();
1788 BUG_ON(!path);
1789 mutex_lock(&root->fs_info->fs_mutex);
1790
1791again:
1792 em = lookup_extent_mapping(em_tree, start, end);
1793 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001794 if (em->start > start) {
1795 printk("get_extent start %Lu em start %Lu\n",
1796 start, em->start);
1797 WARN_ON(1);
1798 }
Chris Masona52d9a82007-08-27 16:49:44 -04001799 goto out;
1800 }
1801 if (!em) {
1802 em = alloc_extent_map(GFP_NOFS);
1803 if (!em) {
1804 err = -ENOMEM;
1805 goto out;
1806 }
Chris Mason5f39d392007-10-15 16:14:19 -04001807 em->start = EXTENT_MAP_HOLE;
1808 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001809 }
1810 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001811 ret = btrfs_lookup_file_extent(trans, root, path,
1812 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001813 if (ret < 0) {
1814 err = ret;
1815 goto out;
1816 }
1817
1818 if (ret != 0) {
1819 if (path->slots[0] == 0)
1820 goto not_found;
1821 path->slots[0]--;
1822 }
1823
Chris Mason5f39d392007-10-15 16:14:19 -04001824 leaf = path->nodes[0];
1825 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001826 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001827 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001828 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1829 found_type = btrfs_key_type(&found_key);
1830 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001831 found_type != BTRFS_EXTENT_DATA_KEY) {
1832 goto not_found;
1833 }
1834
Chris Mason5f39d392007-10-15 16:14:19 -04001835 found_type = btrfs_file_extent_type(leaf, item);
1836 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001837 if (found_type == BTRFS_FILE_EXTENT_REG) {
1838 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001839 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001840 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001841 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001842 em->start = start;
1843 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001844 if (end < extent_start)
1845 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001846 em->end = extent_end - 1;
1847 } else {
1848 em->end = end;
1849 }
1850 goto not_found_em;
1851 }
Chris Masondb945352007-10-15 16:15:53 -04001852 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1853 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001854 em->start = extent_start;
1855 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001856 em->block_start = EXTENT_MAP_HOLE;
1857 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001858 goto insert;
1859 }
Chris Masondb945352007-10-15 16:15:53 -04001860 bytenr += btrfs_file_extent_offset(leaf, item);
1861 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001862 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001863 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001864 em->start = extent_start;
1865 em->end = extent_end - 1;
1866 goto insert;
1867 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001868 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001869 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001870 size_t size;
1871 size_t extent_offset;
1872 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001873
Chris Mason5f39d392007-10-15 16:14:19 -04001874 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1875 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001876 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001877 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001878 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001879 em->start = start;
1880 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001881 if (end < extent_start)
1882 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001883 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001884 } else {
1885 em->end = end;
1886 }
1887 goto not_found_em;
1888 }
1889 em->block_start = EXTENT_MAP_INLINE;
1890 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001891
1892 if (!page) {
1893 em->start = extent_start;
1894 em->end = extent_start + size - 1;
1895 goto out;
1896 }
1897
Chris Mason35ebb932007-10-30 16:56:53 -04001898 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001899 extent_start + page_offset;
1900 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1901 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001902 em->start = extent_start + extent_offset;
1903 em->end = (em->start + copy_size -1) |
1904 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001905 map = kmap(page);
1906 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001907 if (create == 0 && !PageUptodate(page)) {
1908 read_extent_buffer(leaf, map + page_offset, ptr,
1909 copy_size);
1910 flush_dcache_page(page);
1911 } else if (create && PageUptodate(page)) {
1912 if (!trans) {
1913 kunmap(page);
1914 free_extent_map(em);
1915 em = NULL;
1916 btrfs_release_path(root, path);
1917 trans = btrfs_start_transaction(root, 1);
1918 goto again;
1919 }
1920 write_extent_buffer(leaf, map + page_offset, ptr,
1921 copy_size);
1922 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001923 }
Chris Masona52d9a82007-08-27 16:49:44 -04001924 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001925 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001926 goto insert;
1927 } else {
1928 printk("unkknown found_type %d\n", found_type);
1929 WARN_ON(1);
1930 }
1931not_found:
1932 em->start = start;
1933 em->end = end;
1934not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001935 em->block_start = EXTENT_MAP_HOLE;
1936 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001937insert:
1938 btrfs_release_path(root, path);
1939 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001940 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001941 err = -EIO;
1942 goto out;
1943 }
1944 ret = add_extent_mapping(em_tree, em);
1945 if (ret == -EEXIST) {
1946 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001947 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001948 if (0 && failed_insert == 1) {
1949 btrfs_drop_extent_cache(inode, start, end);
1950 }
Chris Masona52d9a82007-08-27 16:49:44 -04001951 failed_insert++;
1952 if (failed_insert > 5) {
1953 printk("failing to insert %Lu %Lu\n", start, end);
1954 err = -EIO;
1955 goto out;
1956 }
Chris Masona52d9a82007-08-27 16:49:44 -04001957 goto again;
1958 }
1959 err = 0;
1960out:
1961 btrfs_free_path(path);
1962 if (trans) {
1963 ret = btrfs_end_transaction(trans, root);
1964 if (!err)
1965 err = ret;
1966 }
1967 mutex_unlock(&root->fs_info->fs_mutex);
1968 if (err) {
1969 free_extent_map(em);
1970 WARN_ON(1);
1971 return ERR_PTR(err);
1972 }
1973 return em;
1974}
1975
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001976static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001977{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001978 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001979}
1980
Chris Mason9ebefb182007-06-15 13:50:00 -04001981int btrfs_readpage(struct file *file, struct page *page)
1982{
Chris Masona52d9a82007-08-27 16:49:44 -04001983 struct extent_map_tree *tree;
1984 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1985 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001986}
Chris Mason1832a6d2007-12-21 16:27:21 -05001987
Chris Mason39279cc2007-06-12 06:35:45 -04001988static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1989{
Chris Masona52d9a82007-08-27 16:49:44 -04001990 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001991
1992
1993 if (current->flags & PF_MEMALLOC) {
1994 redirty_page_for_writepage(wbc, page);
1995 unlock_page(page);
1996 return 0;
1997 }
Chris Masona52d9a82007-08-27 16:49:44 -04001998 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1999 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2000}
Chris Mason39279cc2007-06-12 06:35:45 -04002001
Chris Masonb293f022007-11-01 19:45:34 -04002002static int btrfs_writepages(struct address_space *mapping,
2003 struct writeback_control *wbc)
2004{
2005 struct extent_map_tree *tree;
2006 tree = &BTRFS_I(mapping->host)->extent_tree;
2007 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2008}
2009
Chris Mason3ab2fb52007-11-08 10:59:22 -05002010static int
2011btrfs_readpages(struct file *file, struct address_space *mapping,
2012 struct list_head *pages, unsigned nr_pages)
2013{
2014 struct extent_map_tree *tree;
2015 tree = &BTRFS_I(mapping->host)->extent_tree;
2016 return extent_readpages(tree, mapping, pages, nr_pages,
2017 btrfs_get_extent);
2018}
2019
Chris Masona52d9a82007-08-27 16:49:44 -04002020static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2021{
2022 struct extent_map_tree *tree;
2023 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002024
Chris Masona52d9a82007-08-27 16:49:44 -04002025 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2026 ret = try_release_extent_mapping(tree, page);
2027 if (ret == 1) {
2028 ClearPagePrivate(page);
2029 set_page_private(page, 0);
2030 page_cache_release(page);
2031 }
2032 return ret;
2033}
Chris Mason39279cc2007-06-12 06:35:45 -04002034
Chris Masona52d9a82007-08-27 16:49:44 -04002035static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2036{
2037 struct extent_map_tree *tree;
2038
2039 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2040 extent_invalidatepage(tree, page, offset);
2041 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002042}
2043
Chris Mason9ebefb182007-06-15 13:50:00 -04002044/*
2045 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2046 * called from a page fault handler when a page is first dirtied. Hence we must
2047 * be careful to check for EOF conditions here. We set the page up correctly
2048 * for a written page which means we get ENOSPC checking when writing into
2049 * holes and correct delalloc and unwritten extent mapping on filesystems that
2050 * support these features.
2051 *
2052 * We are not allowed to take the i_mutex here so we have to play games to
2053 * protect against truncate races as the page could now be beyond EOF. Because
2054 * vmtruncate() writes the inode size before removing pages, once we have the
2055 * page lock we can determine safely if the page is beyond EOF. If it is not
2056 * beyond EOF, then the page is guaranteed safe against truncation until we
2057 * unlock the page.
2058 */
2059int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2060{
Chris Mason6da6aba2007-12-18 16:15:09 -05002061 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002062 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002063 unsigned long end;
2064 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002065 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002066 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002067
Chris Mason1832a6d2007-12-21 16:27:21 -05002068 mutex_lock(&root->fs_info->fs_mutex);
2069 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002070 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002071 if (ret)
2072 goto out;
2073
2074 ret = -EINVAL;
2075
Chris Mason9ebefb182007-06-15 13:50:00 -04002076 lock_page(page);
2077 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002078 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002079 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002080
Chris Mason9ebefb182007-06-15 13:50:00 -04002081 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002082 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002083 /* page got truncated out from underneath us */
2084 goto out_unlock;
2085 }
2086
2087 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002088 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002089 end = size & ~PAGE_CACHE_MASK;
2090 else
2091 end = PAGE_CACHE_SIZE;
2092
Chris Masonb888db22007-08-27 16:49:44 -04002093 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002094
2095out_unlock:
2096 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002097out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002098 return ret;
2099}
2100
Chris Mason39279cc2007-06-12 06:35:45 -04002101static void btrfs_truncate(struct inode *inode)
2102{
2103 struct btrfs_root *root = BTRFS_I(inode)->root;
2104 int ret;
2105 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002106 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002107
2108 if (!S_ISREG(inode->i_mode))
2109 return;
2110 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2111 return;
2112
2113 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2114
2115 mutex_lock(&root->fs_info->fs_mutex);
2116 trans = btrfs_start_transaction(root, 1);
2117 btrfs_set_trans_block_group(trans, inode);
2118
2119 /* FIXME, add redo link to tree so we don't leak on crash */
2120 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002121 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002122 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002123
Chris Mason39279cc2007-06-12 06:35:45 -04002124 ret = btrfs_end_transaction(trans, root);
2125 BUG_ON(ret);
2126 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002127 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002128 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002129}
2130
Chris Mason4313b392008-01-03 09:08:48 -05002131static int noinline create_subvol(struct btrfs_root *root, char *name,
2132 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002133{
2134 struct btrfs_trans_handle *trans;
2135 struct btrfs_key key;
2136 struct btrfs_root_item root_item;
2137 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002138 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002139 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002140 struct inode *inode;
2141 struct inode *dir;
2142 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002143 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002144 u64 objectid;
2145 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002146 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002147
2148 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002149 ret = btrfs_check_free_space(root, 1, 0);
2150 if (ret)
2151 goto fail_commit;
2152
Chris Mason39279cc2007-06-12 06:35:45 -04002153 trans = btrfs_start_transaction(root, 1);
2154 BUG_ON(!trans);
2155
Chris Mason7bb86312007-12-11 09:25:06 -05002156 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2157 0, &objectid);
2158 if (ret)
2159 goto fail;
2160
2161 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2162 objectid, trans->transid, 0, 0,
2163 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002164 if (IS_ERR(leaf))
2165 return PTR_ERR(leaf);
2166
2167 btrfs_set_header_nritems(leaf, 0);
2168 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002169 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002170 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002171 btrfs_set_header_owner(leaf, objectid);
2172
Chris Mason5f39d392007-10-15 16:14:19 -04002173 write_extent_buffer(leaf, root->fs_info->fsid,
2174 (unsigned long)btrfs_header_fsid(leaf),
2175 BTRFS_FSID_SIZE);
2176 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002177
2178 inode_item = &root_item.inode;
2179 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002180 inode_item->generation = cpu_to_le64(1);
2181 inode_item->size = cpu_to_le64(3);
2182 inode_item->nlink = cpu_to_le32(1);
2183 inode_item->nblocks = cpu_to_le64(1);
2184 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002185
Chris Masondb945352007-10-15 16:15:53 -04002186 btrfs_set_root_bytenr(&root_item, leaf->start);
2187 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002188 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 btrfs_set_root_used(&root_item, 0);
2190
Chris Mason5eda7b52007-06-22 14:16:25 -04002191 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2192 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002193
2194 free_extent_buffer(leaf);
2195 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002196
Chris Mason39279cc2007-06-12 06:35:45 -04002197 btrfs_set_root_dirid(&root_item, new_dirid);
2198
2199 key.objectid = objectid;
2200 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002201 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2202 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2203 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002204 if (ret)
2205 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002206
2207 /*
2208 * insert the directory item
2209 */
2210 key.offset = (u64)-1;
2211 dir = root->fs_info->sb->s_root->d_inode;
2212 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2213 name, namelen, dir->i_ino, &key,
2214 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002215 if (ret)
2216 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002217
Chris Mason39544012007-12-12 14:38:19 -05002218 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2219 name, namelen, objectid,
2220 root->fs_info->sb->s_root->d_inode->i_ino);
2221 if (ret)
2222 goto fail;
2223
Chris Mason39279cc2007-06-12 06:35:45 -04002224 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002225 if (ret)
2226 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002227
Josef Bacik58176a92007-08-29 15:47:34 -04002228 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002229 BUG_ON(!new_root);
2230
2231 trans = btrfs_start_transaction(new_root, 1);
2232 BUG_ON(!trans);
2233
2234 inode = btrfs_new_inode(trans, new_root, new_dirid,
2235 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002236 if (IS_ERR(inode))
2237 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002238 inode->i_op = &btrfs_dir_inode_operations;
2239 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002240 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002241
Chris Mason39544012007-12-12 14:38:19 -05002242 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2243 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002244 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002245 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002246 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002247 if (ret)
2248 goto fail;
2249fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002250 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002251 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002252 if (err && !ret)
2253 ret = err;
2254fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002255 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002256 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002257 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002258 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002259}
2260
2261static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2262{
Chris Mason3063d292008-01-08 15:46:30 -05002263 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002264 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002265 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002266 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002267 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002268
2269 if (!root->ref_cows)
2270 return -EINVAL;
2271
2272 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002273 ret = btrfs_check_free_space(root, 1, 0);
2274 if (ret)
2275 goto fail_unlock;
2276
Chris Mason3063d292008-01-08 15:46:30 -05002277 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2278 if (!pending_snapshot) {
2279 ret = -ENOMEM;
2280 goto fail_unlock;
2281 }
2282 pending_snapshot->name = kstrndup(name, namelen, GFP_NOFS);
2283 if (!pending_snapshot->name) {
2284 ret = -ENOMEM;
2285 kfree(pending_snapshot);
2286 goto fail_unlock;
2287 }
Chris Mason39279cc2007-06-12 06:35:45 -04002288 trans = btrfs_start_transaction(root, 1);
2289 BUG_ON(!trans);
Chris Masondc17ff82008-01-08 15:46:30 -05002290
Chris Mason3063d292008-01-08 15:46:30 -05002291 pending_snapshot->root = root;
2292 list_add(&pending_snapshot->list,
2293 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002294 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002295 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002296
Chris Mason1832a6d2007-12-21 16:27:21 -05002297fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002298 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002299 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002300 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002301 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002302}
2303
Chris Masonedbd8d42007-12-21 16:27:24 -05002304unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002305 struct file_ra_state *ra, struct file *file,
2306 pgoff_t offset, pgoff_t last_index)
2307{
2308 pgoff_t req_size;
2309
2310#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2311 req_size = last_index - offset + 1;
2312 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2313 return offset;
2314#else
2315 req_size = min(last_index - offset + 1, (pgoff_t)128);
2316 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2317 return offset + req_size;
2318#endif
2319}
2320
2321int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002322 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002323 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002324 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2325 struct page *page;
2326 unsigned long last_index;
2327 unsigned long ra_index = 0;
2328 u64 page_start;
2329 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002330 u64 delalloc_start;
2331 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002332 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002333 int ret;
2334
2335 mutex_lock(&root->fs_info->fs_mutex);
2336 ret = btrfs_check_free_space(root, inode->i_size, 0);
2337 mutex_unlock(&root->fs_info->fs_mutex);
2338 if (ret)
2339 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002340
2341 mutex_lock(&inode->i_mutex);
2342 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2343 for (i = 0; i <= last_index; i++) {
2344 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002345 ra_index = btrfs_force_ra(inode->i_mapping,
2346 &file->f_ra,
2347 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002348 }
2349 page = grab_cache_page(inode->i_mapping, i);
2350 if (!page)
2351 goto out_unlock;
2352 if (!PageUptodate(page)) {
2353 btrfs_readpage(NULL, page);
2354 lock_page(page);
2355 if (!PageUptodate(page)) {
2356 unlock_page(page);
2357 page_cache_release(page);
2358 goto out_unlock;
2359 }
2360 }
Chris Mason35ebb932007-10-30 16:56:53 -04002361 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002362 page_end = page_start + PAGE_CACHE_SIZE - 1;
2363
2364 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002365 delalloc_start = page_start;
2366 existing_delalloc =
2367 count_range_bits(&BTRFS_I(inode)->extent_tree,
2368 &delalloc_start, page_end,
2369 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002370 set_extent_delalloc(em_tree, page_start,
2371 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002372
2373 spin_lock(&root->fs_info->delalloc_lock);
2374 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2375 existing_delalloc;
2376 spin_unlock(&root->fs_info->delalloc_lock);
2377
Chris Mason86479a02007-09-10 19:58:16 -04002378 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2379 set_page_dirty(page);
2380 unlock_page(page);
2381 page_cache_release(page);
2382 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2383 }
2384
2385out_unlock:
2386 mutex_unlock(&inode->i_mutex);
2387 return 0;
2388}
2389
Chris Masonedbd8d42007-12-21 16:27:24 -05002390static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2391{
2392 u64 new_size;
2393 u64 old_size;
2394 struct btrfs_ioctl_vol_args *vol_args;
2395 struct btrfs_trans_handle *trans;
2396 char *sizestr;
2397 int ret = 0;
2398 int namelen;
2399 int mod = 0;
2400
2401 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2402
2403 if (!vol_args)
2404 return -ENOMEM;
2405
2406 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2407 ret = -EFAULT;
2408 goto out;
2409 }
2410 namelen = strlen(vol_args->name);
2411 if (namelen > BTRFS_VOL_NAME_MAX) {
2412 ret = -EINVAL;
2413 goto out;
2414 }
2415
2416 sizestr = vol_args->name;
2417 if (!strcmp(sizestr, "max"))
2418 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2419 else {
2420 if (sizestr[0] == '-') {
2421 mod = -1;
2422 sizestr++;
2423 } else if (sizestr[0] == '+') {
2424 mod = 1;
2425 sizestr++;
2426 }
2427 new_size = btrfs_parse_size(sizestr);
2428 if (new_size == 0) {
2429 ret = -EINVAL;
2430 goto out;
2431 }
2432 }
2433
2434 mutex_lock(&root->fs_info->fs_mutex);
2435 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2436
2437 if (mod < 0) {
2438 if (new_size > old_size) {
2439 ret = -EINVAL;
2440 goto out_unlock;
2441 }
2442 new_size = old_size - new_size;
2443 } else if (mod > 0) {
2444 new_size = old_size + new_size;
2445 }
2446
2447 if (new_size < 256 * 1024 * 1024) {
2448 ret = -EINVAL;
2449 goto out_unlock;
2450 }
2451 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2452 ret = -EFBIG;
2453 goto out_unlock;
2454 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002455
2456 do_div(new_size, root->sectorsize);
2457 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002458
2459printk("new size is %Lu\n", new_size);
2460 if (new_size > old_size) {
2461 trans = btrfs_start_transaction(root, 1);
2462 ret = btrfs_grow_extent_tree(trans, root, new_size);
2463 btrfs_commit_transaction(trans, root);
2464 } else {
2465 ret = btrfs_shrink_extent_tree(root, new_size);
2466 }
2467
2468out_unlock:
2469 mutex_unlock(&root->fs_info->fs_mutex);
2470out:
2471 kfree(vol_args);
2472 return ret;
2473}
2474
Chris Mason4313b392008-01-03 09:08:48 -05002475static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2476 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002477{
Chris Mason4aec2b52007-12-18 16:25:45 -05002478 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002479 struct btrfs_dir_item *di;
2480 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002481 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002482 int namelen;
2483 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002484
Chris Mason4aec2b52007-12-18 16:25:45 -05002485 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002486
Chris Mason4aec2b52007-12-18 16:25:45 -05002487 if (!vol_args)
2488 return -ENOMEM;
2489
2490 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2491 ret = -EFAULT;
2492 goto out;
2493 }
2494
2495 namelen = strlen(vol_args->name);
2496 if (namelen > BTRFS_VOL_NAME_MAX) {
2497 ret = -EINVAL;
2498 goto out;
2499 }
2500 if (strchr(vol_args->name, '/')) {
2501 ret = -EINVAL;
2502 goto out;
2503 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002504
2505 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002506 if (!path) {
2507 ret = -ENOMEM;
2508 goto out;
2509 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002510
2511 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2512 mutex_lock(&root->fs_info->fs_mutex);
2513 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2514 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002515 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002516 mutex_unlock(&root->fs_info->fs_mutex);
2517 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002518
2519 if (di && !IS_ERR(di)) {
2520 ret = -EEXIST;
2521 goto out;
2522 }
2523
2524 if (IS_ERR(di)) {
2525 ret = PTR_ERR(di);
2526 goto out;
2527 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002528
2529 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002530 ret = create_subvol(root, vol_args->name, namelen);
2531 else
2532 ret = create_snapshot(root, vol_args->name, namelen);
2533out:
2534 kfree(vol_args);
2535 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002536}
2537
2538static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002539{
Chris Mason6da6aba2007-12-18 16:15:09 -05002540 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002541 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002542
2543 switch (inode->i_mode & S_IFMT) {
2544 case S_IFDIR:
2545 mutex_lock(&root->fs_info->fs_mutex);
2546 btrfs_defrag_root(root, 0);
2547 btrfs_defrag_root(root->fs_info->extent_root, 0);
2548 mutex_unlock(&root->fs_info->fs_mutex);
2549 break;
2550 case S_IFREG:
2551 btrfs_defrag_file(file);
2552 break;
2553 }
2554
2555 return 0;
2556}
2557
2558long btrfs_ioctl(struct file *file, unsigned int
2559 cmd, unsigned long arg)
2560{
Chris Mason6da6aba2007-12-18 16:15:09 -05002561 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002562
2563 switch (cmd) {
2564 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002565 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002566 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002567 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002568 case BTRFS_IOC_RESIZE:
2569 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002570 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002571
2572 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002573}
2574
Chris Mason39279cc2007-06-12 06:35:45 -04002575/*
2576 * Called inside transaction, so use GFP_NOFS
2577 */
2578struct inode *btrfs_alloc_inode(struct super_block *sb)
2579{
2580 struct btrfs_inode *ei;
2581
2582 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2583 if (!ei)
2584 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002585 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002586 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002587 return &ei->vfs_inode;
2588}
2589
2590void btrfs_destroy_inode(struct inode *inode)
2591{
2592 WARN_ON(!list_empty(&inode->i_dentry));
2593 WARN_ON(inode->i_data.nrpages);
2594
2595 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2596}
2597
Chris Mason44ec0b72007-10-29 10:55:05 -04002598#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2599static void init_once(struct kmem_cache * cachep, void *foo)
2600#else
Chris Mason39279cc2007-06-12 06:35:45 -04002601static void init_once(void * foo, struct kmem_cache * cachep,
2602 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002603#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002604{
2605 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2606
2607 inode_init_once(&ei->vfs_inode);
2608}
2609
2610void btrfs_destroy_cachep(void)
2611{
2612 if (btrfs_inode_cachep)
2613 kmem_cache_destroy(btrfs_inode_cachep);
2614 if (btrfs_trans_handle_cachep)
2615 kmem_cache_destroy(btrfs_trans_handle_cachep);
2616 if (btrfs_transaction_cachep)
2617 kmem_cache_destroy(btrfs_transaction_cachep);
2618 if (btrfs_bit_radix_cachep)
2619 kmem_cache_destroy(btrfs_bit_radix_cachep);
2620 if (btrfs_path_cachep)
2621 kmem_cache_destroy(btrfs_path_cachep);
2622}
2623
Chris Mason86479a02007-09-10 19:58:16 -04002624struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002625 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002626#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2627 void (*ctor)(struct kmem_cache *, void *)
2628#else
Chris Mason92fee662007-07-25 12:31:35 -04002629 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002630 unsigned long)
2631#endif
2632 )
Chris Mason92fee662007-07-25 12:31:35 -04002633{
2634 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2635 SLAB_MEM_SPREAD | extra_flags), ctor
2636#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2637 ,NULL
2638#endif
2639 );
2640}
2641
Chris Mason39279cc2007-06-12 06:35:45 -04002642int btrfs_init_cachep(void)
2643{
Chris Mason86479a02007-09-10 19:58:16 -04002644 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002645 sizeof(struct btrfs_inode),
2646 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002647 if (!btrfs_inode_cachep)
2648 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002649 btrfs_trans_handle_cachep =
2650 btrfs_cache_create("btrfs_trans_handle_cache",
2651 sizeof(struct btrfs_trans_handle),
2652 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002653 if (!btrfs_trans_handle_cachep)
2654 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002655 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002656 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002657 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002658 if (!btrfs_transaction_cachep)
2659 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002660 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002661 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002662 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002663 if (!btrfs_path_cachep)
2664 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002665 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002666 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002667 if (!btrfs_bit_radix_cachep)
2668 goto fail;
2669 return 0;
2670fail:
2671 btrfs_destroy_cachep();
2672 return -ENOMEM;
2673}
2674
2675static int btrfs_getattr(struct vfsmount *mnt,
2676 struct dentry *dentry, struct kstat *stat)
2677{
2678 struct inode *inode = dentry->d_inode;
2679 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002680 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002681 return 0;
2682}
2683
2684static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2685 struct inode * new_dir,struct dentry *new_dentry)
2686{
2687 struct btrfs_trans_handle *trans;
2688 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2689 struct inode *new_inode = new_dentry->d_inode;
2690 struct inode *old_inode = old_dentry->d_inode;
2691 struct timespec ctime = CURRENT_TIME;
2692 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002693 int ret;
2694
2695 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2696 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2697 return -ENOTEMPTY;
2698 }
Chris Mason5f39d392007-10-15 16:14:19 -04002699
Chris Mason39279cc2007-06-12 06:35:45 -04002700 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002701 ret = btrfs_check_free_space(root, 1, 0);
2702 if (ret)
2703 goto out_unlock;
2704
Chris Mason39279cc2007-06-12 06:35:45 -04002705 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002706
Chris Mason39279cc2007-06-12 06:35:45 -04002707 btrfs_set_trans_block_group(trans, new_dir);
2708 path = btrfs_alloc_path();
2709 if (!path) {
2710 ret = -ENOMEM;
2711 goto out_fail;
2712 }
2713
2714 old_dentry->d_inode->i_nlink++;
2715 old_dir->i_ctime = old_dir->i_mtime = ctime;
2716 new_dir->i_ctime = new_dir->i_mtime = ctime;
2717 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002718
Chris Mason39279cc2007-06-12 06:35:45 -04002719 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2720 if (ret)
2721 goto out_fail;
2722
2723 if (new_inode) {
2724 new_inode->i_ctime = CURRENT_TIME;
2725 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2726 if (ret)
2727 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002728 }
2729 ret = btrfs_add_link(trans, new_dentry, old_inode);
2730 if (ret)
2731 goto out_fail;
2732
2733out_fail:
2734 btrfs_free_path(path);
2735 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002736out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002737 mutex_unlock(&root->fs_info->fs_mutex);
2738 return ret;
2739}
2740
2741static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2742 const char *symname)
2743{
2744 struct btrfs_trans_handle *trans;
2745 struct btrfs_root *root = BTRFS_I(dir)->root;
2746 struct btrfs_path *path;
2747 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002748 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002749 int err;
2750 int drop_inode = 0;
2751 u64 objectid;
2752 int name_len;
2753 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002754 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002755 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002756 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002757 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002758
2759 name_len = strlen(symname) + 1;
2760 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2761 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002762
Chris Mason39279cc2007-06-12 06:35:45 -04002763 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002764 err = btrfs_check_free_space(root, 1, 0);
2765 if (err)
2766 goto out_fail;
2767
Chris Mason39279cc2007-06-12 06:35:45 -04002768 trans = btrfs_start_transaction(root, 1);
2769 btrfs_set_trans_block_group(trans, dir);
2770
2771 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2772 if (err) {
2773 err = -ENOSPC;
2774 goto out_unlock;
2775 }
2776
2777 inode = btrfs_new_inode(trans, root, objectid,
2778 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2779 err = PTR_ERR(inode);
2780 if (IS_ERR(inode))
2781 goto out_unlock;
2782
2783 btrfs_set_trans_block_group(trans, inode);
2784 err = btrfs_add_nondir(trans, dentry, inode);
2785 if (err)
2786 drop_inode = 1;
2787 else {
2788 inode->i_mapping->a_ops = &btrfs_aops;
2789 inode->i_fop = &btrfs_file_operations;
2790 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002791 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2792 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002793 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002794 }
2795 dir->i_sb->s_dirt = 1;
2796 btrfs_update_inode_block_group(trans, inode);
2797 btrfs_update_inode_block_group(trans, dir);
2798 if (drop_inode)
2799 goto out_unlock;
2800
2801 path = btrfs_alloc_path();
2802 BUG_ON(!path);
2803 key.objectid = inode->i_ino;
2804 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002805 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2806 datasize = btrfs_file_extent_calc_inline_size(name_len);
2807 err = btrfs_insert_empty_item(trans, root, path, &key,
2808 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002809 if (err) {
2810 drop_inode = 1;
2811 goto out_unlock;
2812 }
Chris Mason5f39d392007-10-15 16:14:19 -04002813 leaf = path->nodes[0];
2814 ei = btrfs_item_ptr(leaf, path->slots[0],
2815 struct btrfs_file_extent_item);
2816 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2817 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002818 BTRFS_FILE_EXTENT_INLINE);
2819 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002820 write_extent_buffer(leaf, symname, ptr, name_len);
2821 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002822 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002823
Chris Mason39279cc2007-06-12 06:35:45 -04002824 inode->i_op = &btrfs_symlink_inode_operations;
2825 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2826 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002827 err = btrfs_update_inode(trans, root, inode);
2828 if (err)
2829 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002830
2831out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002832 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002833 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002834out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002835 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002836 if (drop_inode) {
2837 inode_dec_link_count(inode);
2838 iput(inode);
2839 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002840 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002841 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002842 return err;
2843}
2844
2845static struct inode_operations btrfs_dir_inode_operations = {
2846 .lookup = btrfs_lookup,
2847 .create = btrfs_create,
2848 .unlink = btrfs_unlink,
2849 .link = btrfs_link,
2850 .mkdir = btrfs_mkdir,
2851 .rmdir = btrfs_rmdir,
2852 .rename = btrfs_rename,
2853 .symlink = btrfs_symlink,
2854 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002855 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002856 .setxattr = generic_setxattr,
2857 .getxattr = generic_getxattr,
2858 .listxattr = btrfs_listxattr,
2859 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002860};
2861
2862static struct inode_operations btrfs_dir_ro_inode_operations = {
2863 .lookup = btrfs_lookup,
2864};
2865
2866static struct file_operations btrfs_dir_file_operations = {
2867 .llseek = generic_file_llseek,
2868 .read = generic_read_dir,
2869 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002870 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002871#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002872 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002873#endif
2874};
2875
Chris Mason07157aa2007-08-30 08:50:51 -04002876static struct extent_map_ops btrfs_extent_map_ops = {
2877 .fill_delalloc = run_delalloc_range,
2878 .writepage_io_hook = btrfs_writepage_io_hook,
2879 .readpage_io_hook = btrfs_readpage_io_hook,
2880 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2881};
2882
Chris Mason39279cc2007-06-12 06:35:45 -04002883static struct address_space_operations btrfs_aops = {
2884 .readpage = btrfs_readpage,
2885 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002886 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002887 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002888 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002889 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002890 .invalidatepage = btrfs_invalidatepage,
2891 .releasepage = btrfs_releasepage,
2892 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002893};
2894
2895static struct address_space_operations btrfs_symlink_aops = {
2896 .readpage = btrfs_readpage,
2897 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002898 .invalidatepage = btrfs_invalidatepage,
2899 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002900};
2901
2902static struct inode_operations btrfs_file_inode_operations = {
2903 .truncate = btrfs_truncate,
2904 .getattr = btrfs_getattr,
2905 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002906 .setxattr = generic_setxattr,
2907 .getxattr = generic_getxattr,
2908 .listxattr = btrfs_listxattr,
2909 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002910};
2911
Josef Bacik618e21d2007-07-11 10:18:17 -04002912static struct inode_operations btrfs_special_inode_operations = {
2913 .getattr = btrfs_getattr,
2914 .setattr = btrfs_setattr,
2915};
2916
Chris Mason39279cc2007-06-12 06:35:45 -04002917static struct inode_operations btrfs_symlink_inode_operations = {
2918 .readlink = generic_readlink,
2919 .follow_link = page_follow_link_light,
2920 .put_link = page_put_link,
2921};