blob: 67005480e1391b228b791084ccc8de7a790ee2bb [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
Chris Mason9cce6c32008-01-15 08:44:06 -050083 return 0;
84
Chris Mason1832a6d2007-12-21 16:27:21 -050085 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 else
Chris Masonf9ef6602008-01-03 09:22:38 -050088 thresh = total * 85;
89
90 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050091
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
94 ret = -ENOSPC;
95 spin_unlock(&root->fs_info->delalloc_lock);
96 return ret;
97}
98
Chris Masonbe20aa92007-12-17 20:14:01 -050099static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400103 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500105 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400106 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -0500107 struct btrfs_key ins;
108 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400109
Chris Masonb888db22007-08-27 16:49:44 -0400110 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400111 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 btrfs_set_trans_block_group(trans, inode);
113
Chris Masondb945352007-10-15 16:15:53 -0400114 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400116 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400117 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -0400118
Chris Mason179e29e2007-11-01 11:28:41 -0400119 if (alloc_hint == EXTENT_MAP_INLINE)
120 goto out;
121
Chris Masonc59f8952007-12-17 20:14:04 -0500122 while(num_bytes > 0) {
123 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
124 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
125 root->root_key.objectid,
126 trans->transid,
127 inode->i_ino, start, 0,
128 alloc_hint, (u64)-1, &ins, 1);
129 if (ret) {
130 WARN_ON(1);
131 goto out;
132 }
133 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
134 start, ins.objectid, ins.offset,
135 ins.offset);
Chris Mason5f564062008-01-22 16:47:59 -0500136 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500137 num_bytes -= cur_alloc_size;
138 alloc_hint = ins.objectid + ins.offset;
139 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400140 }
Chris Masondc17ff82008-01-08 15:46:30 -0500141 btrfs_add_ordered_inode(inode);
Chris Masonb888db22007-08-27 16:49:44 -0400142out:
143 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500144 return ret;
145}
146
147static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
148{
149 u64 extent_start;
150 u64 extent_end;
151 u64 bytenr;
152 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500153 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500154 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500155 struct btrfs_root *root = BTRFS_I(inode)->root;
156 struct extent_buffer *leaf;
157 int found_type;
158 struct btrfs_path *path;
159 struct btrfs_file_extent_item *item;
160 int ret;
161 int err;
162 struct btrfs_key found_key;
163
Chris Masonc31f8832008-01-08 15:46:31 -0500164 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500165 path = btrfs_alloc_path();
166 BUG_ON(!path);
167again:
168 ret = btrfs_lookup_file_extent(NULL, root, path,
169 inode->i_ino, start, 0);
170 if (ret < 0) {
171 btrfs_free_path(path);
172 return ret;
173 }
174
175 cow_end = end;
176 if (ret != 0) {
177 if (path->slots[0] == 0)
178 goto not_found;
179 path->slots[0]--;
180 }
181
182 leaf = path->nodes[0];
183 item = btrfs_item_ptr(leaf, path->slots[0],
184 struct btrfs_file_extent_item);
185
186 /* are we inside the extent that was found? */
187 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
188 found_type = btrfs_key_type(&found_key);
189 if (found_key.objectid != inode->i_ino ||
190 found_type != BTRFS_EXTENT_DATA_KEY) {
191 goto not_found;
192 }
193
194 found_type = btrfs_file_extent_type(leaf, item);
195 extent_start = found_key.offset;
196 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500197 u64 extent_num_bytes;
198
199 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
200 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201 err = 0;
202
Chris Mason1832a6d2007-12-21 16:27:21 -0500203 if (loops && start != extent_start)
204 goto not_found;
205
Chris Masonbe20aa92007-12-17 20:14:01 -0500206 if (start < extent_start || start >= extent_end)
207 goto not_found;
208
209 cow_end = min(end, extent_end - 1);
210 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
211 if (bytenr == 0)
212 goto not_found;
213
Chris Masonc31f8832008-01-08 15:46:31 -0500214 /*
215 * we may be called by the resizer, make sure we're inside
216 * the limits of the FS
217 */
218 if (bytenr + extent_num_bytes > total_fs_bytes)
219 goto not_found;
220
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
222 goto not_found;
223 }
224
225 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500226 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 goto not_found;
228 }
229loop:
230 if (start > end) {
231 btrfs_free_path(path);
232 return 0;
233 }
234 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500235 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 goto again;
237
238not_found:
239 cow_file_range(inode, start, cow_end);
240 start = cow_end + 1;
241 goto loop;
242}
243
244static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
245{
246 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500247 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500248 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500250 if (btrfs_test_opt(root, NODATACOW) ||
251 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 ret = run_delalloc_nocow(inode, start, end);
253 else
254 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500255
256 spin_lock(&root->fs_info->delalloc_lock);
257 num_bytes = end + 1 - start;
258 if (root->fs_info->delalloc_bytes < num_bytes) {
259 printk("delalloc accounting error total %llu sub %llu\n",
260 root->fs_info->delalloc_bytes, num_bytes);
261 } else {
262 root->fs_info->delalloc_bytes -= num_bytes;
263 }
264 spin_unlock(&root->fs_info->delalloc_lock);
265
Chris Masonb888db22007-08-27 16:49:44 -0400266 mutex_unlock(&root->fs_info->fs_mutex);
267 return ret;
268}
269
Chris Mason07157aa2007-08-30 08:50:51 -0400270int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
271{
272 struct inode *inode = page->mapping->host;
273 struct btrfs_root *root = BTRFS_I(inode)->root;
274 struct btrfs_trans_handle *trans;
275 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500276 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400277 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400278 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500279 if (btrfs_test_opt(root, NODATASUM) ||
280 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500281 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400282 mutex_lock(&root->fs_info->fs_mutex);
283 trans = btrfs_start_transaction(root, 1);
284 btrfs_set_trans_block_group(trans, inode);
285 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400286 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400287 start, kaddr + offset, end - start + 1);
288 kunmap(page);
289 ret = btrfs_end_transaction(trans, root);
290 BUG_ON(ret);
291 mutex_unlock(&root->fs_info->fs_mutex);
292 return ret;
293}
294
295int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
296{
297 int ret = 0;
298 struct inode *inode = page->mapping->host;
299 struct btrfs_root *root = BTRFS_I(inode)->root;
300 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
301 struct btrfs_csum_item *item;
302 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400303 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500304 if (btrfs_test_opt(root, NODATASUM) ||
305 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500306 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400307 mutex_lock(&root->fs_info->fs_mutex);
308 path = btrfs_alloc_path();
309 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
310 if (IS_ERR(item)) {
311 ret = PTR_ERR(item);
312 /* a csum that isn't present is a preallocated region. */
313 if (ret == -ENOENT || ret == -EFBIG)
314 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400315 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400316 goto out;
317 }
Chris Masonff79f812007-10-15 16:22:25 -0400318 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
319 BTRFS_CRC32_SIZE);
320 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400321out:
322 if (path)
323 btrfs_free_path(path);
324 mutex_unlock(&root->fs_info->fs_mutex);
325 return ret;
326}
327
328int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
329{
Chris Mason35ebb932007-10-30 16:56:53 -0400330 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400331 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400332 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
333 char *kaddr;
334 u64 private;
335 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400336 struct btrfs_root *root = BTRFS_I(inode)->root;
337 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400338 unsigned long flags;
Yanb98b6762008-01-08 15:54:37 -0500339 if (btrfs_test_opt(root, NODATASUM) ||
340 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500341 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400342 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400343 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400344 kaddr = kmap_atomic(page, KM_IRQ0);
345 if (ret) {
346 goto zeroit;
347 }
Chris Masonff79f812007-10-15 16:22:25 -0400348 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
349 btrfs_csum_final(csum, (char *)&csum);
350 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400351 goto zeroit;
352 }
353 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400354 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400355 return 0;
356
357zeroit:
358 printk("btrfs csum failed ino %lu off %llu\n",
359 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400360 memset(kaddr + offset, 1, end - start + 1);
361 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400362 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400363 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400364 return 0;
365}
Chris Masonb888db22007-08-27 16:49:44 -0400366
Chris Mason39279cc2007-06-12 06:35:45 -0400367void btrfs_read_locked_inode(struct inode *inode)
368{
369 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400370 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400371 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400372 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400373 struct btrfs_root *root = BTRFS_I(inode)->root;
374 struct btrfs_key location;
375 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400376 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400377 int ret;
378
379 path = btrfs_alloc_path();
380 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400381 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400382 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500383
Chris Mason39279cc2007-06-12 06:35:45 -0400384 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400385 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400386 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400387
Chris Mason5f39d392007-10-15 16:14:19 -0400388 leaf = path->nodes[0];
389 inode_item = btrfs_item_ptr(leaf, path->slots[0],
390 struct btrfs_inode_item);
391
392 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
393 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
394 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
395 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
396 inode->i_size = btrfs_inode_size(leaf, inode_item);
397
398 tspec = btrfs_inode_atime(inode_item);
399 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
400 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
401
402 tspec = btrfs_inode_mtime(inode_item);
403 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
404 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
405
406 tspec = btrfs_inode_ctime(inode_item);
407 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
408 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
409
410 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
411 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400412 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400413 rdev = btrfs_inode_rdev(leaf, inode_item);
414
415 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400416 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
417 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500418 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500419 if (!BTRFS_I(inode)->block_group) {
420 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
421 NULL, 0, 0, 0);
422 }
Chris Mason39279cc2007-06-12 06:35:45 -0400423 btrfs_free_path(path);
424 inode_item = NULL;
425
426 mutex_unlock(&root->fs_info->fs_mutex);
427
428 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400429 case S_IFREG:
430 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400431 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400432 inode->i_fop = &btrfs_file_operations;
433 inode->i_op = &btrfs_file_inode_operations;
434 break;
435 case S_IFDIR:
436 inode->i_fop = &btrfs_dir_file_operations;
437 if (root == root->fs_info->tree_root)
438 inode->i_op = &btrfs_dir_ro_inode_operations;
439 else
440 inode->i_op = &btrfs_dir_inode_operations;
441 break;
442 case S_IFLNK:
443 inode->i_op = &btrfs_symlink_inode_operations;
444 inode->i_mapping->a_ops = &btrfs_symlink_aops;
445 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400446 default:
447 init_special_inode(inode, inode->i_mode, rdev);
448 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400449 }
450 return;
451
452make_bad:
453 btrfs_release_path(root, path);
454 btrfs_free_path(path);
455 mutex_unlock(&root->fs_info->fs_mutex);
456 make_bad_inode(inode);
457}
458
Chris Mason5f39d392007-10-15 16:14:19 -0400459static void fill_inode_item(struct extent_buffer *leaf,
460 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400461 struct inode *inode)
462{
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_set_inode_uid(leaf, item, inode->i_uid);
464 btrfs_set_inode_gid(leaf, item, inode->i_gid);
465 btrfs_set_inode_size(leaf, item, inode->i_size);
466 btrfs_set_inode_mode(leaf, item, inode->i_mode);
467 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
468
469 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
470 inode->i_atime.tv_sec);
471 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
472 inode->i_atime.tv_nsec);
473
474 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
475 inode->i_mtime.tv_sec);
476 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
477 inode->i_mtime.tv_nsec);
478
479 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
480 inode->i_ctime.tv_sec);
481 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
482 inode->i_ctime.tv_nsec);
483
484 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
485 btrfs_set_inode_generation(leaf, item, inode->i_generation);
486 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500487 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400488 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400489 BTRFS_I(inode)->block_group->key.objectid);
490}
491
Chris Masona52d9a82007-08-27 16:49:44 -0400492int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400493 struct btrfs_root *root,
494 struct inode *inode)
495{
496 struct btrfs_inode_item *inode_item;
497 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400498 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400499 int ret;
500
501 path = btrfs_alloc_path();
502 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400503 ret = btrfs_lookup_inode(trans, root, path,
504 &BTRFS_I(inode)->location, 1);
505 if (ret) {
506 if (ret > 0)
507 ret = -ENOENT;
508 goto failed;
509 }
510
Chris Mason5f39d392007-10-15 16:14:19 -0400511 leaf = path->nodes[0];
512 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400513 struct btrfs_inode_item);
514
Chris Mason5f39d392007-10-15 16:14:19 -0400515 fill_inode_item(leaf, inode_item, inode);
516 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400517 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400518 ret = 0;
519failed:
520 btrfs_release_path(root, path);
521 btrfs_free_path(path);
522 return ret;
523}
524
525
526static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
527 struct btrfs_root *root,
528 struct inode *dir,
529 struct dentry *dentry)
530{
531 struct btrfs_path *path;
532 const char *name = dentry->d_name.name;
533 int name_len = dentry->d_name.len;
534 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400535 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400536 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400537 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400538
539 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400540 if (!path) {
541 ret = -ENOMEM;
542 goto err;
543 }
544
Chris Mason39279cc2007-06-12 06:35:45 -0400545 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
546 name, name_len, -1);
547 if (IS_ERR(di)) {
548 ret = PTR_ERR(di);
549 goto err;
550 }
551 if (!di) {
552 ret = -ENOENT;
553 goto err;
554 }
Chris Mason5f39d392007-10-15 16:14:19 -0400555 leaf = path->nodes[0];
556 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400557 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400558 if (ret)
559 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400560 btrfs_release_path(root, path);
561
562 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400563 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400564 if (IS_ERR(di)) {
565 ret = PTR_ERR(di);
566 goto err;
567 }
568 if (!di) {
569 ret = -ENOENT;
570 goto err;
571 }
572 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400573
574 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500575 ret = btrfs_del_inode_ref(trans, root, name, name_len,
576 dentry->d_inode->i_ino,
577 dentry->d_parent->d_inode->i_ino);
578 if (ret) {
579 printk("failed to delete reference to %.*s, "
580 "inode %lu parent %lu\n", name_len, name,
581 dentry->d_inode->i_ino,
582 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500583 }
Chris Mason39279cc2007-06-12 06:35:45 -0400584err:
585 btrfs_free_path(path);
586 if (!ret) {
587 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400588 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400589 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500590#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
591 dentry->d_inode->i_nlink--;
592#else
Chris Mason39279cc2007-06-12 06:35:45 -0400593 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500594#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400595 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400596 dir->i_sb->s_dirt = 1;
597 }
598 return ret;
599}
600
601static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
602{
603 struct btrfs_root *root;
604 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500605 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400606 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500607 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400608
609 root = BTRFS_I(dir)->root;
610 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500611
612 ret = btrfs_check_free_space(root, 1, 1);
613 if (ret)
614 goto fail;
615
Chris Mason39279cc2007-06-12 06:35:45 -0400616 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400617
Chris Mason39279cc2007-06-12 06:35:45 -0400618 btrfs_set_trans_block_group(trans, dir);
619 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400620 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400621
Chris Mason2da98f02008-01-16 11:44:43 -0500622 if (inode->i_nlink == 0) {
623 int found;
624 /* if the inode isn't linked anywhere,
625 * we don't need to worry about
626 * data=ordered
627 */
628 found = btrfs_del_ordered_inode(inode);
629 if (found == 1) {
630 atomic_dec(&inode->i_count);
631 }
632 }
633
Chris Mason39279cc2007-06-12 06:35:45 -0400634 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500635fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400636 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400637 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500638 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400639 return ret;
640}
641
642static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
643{
644 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500645 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400646 int ret;
647 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400648 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500649 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400650
Yan134d4512007-10-25 15:49:25 -0400651 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
652 return -ENOTEMPTY;
653
Chris Mason39279cc2007-06-12 06:35:45 -0400654 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500655 ret = btrfs_check_free_space(root, 1, 1);
656 if (ret)
657 goto fail;
658
Chris Mason39279cc2007-06-12 06:35:45 -0400659 trans = btrfs_start_transaction(root, 1);
660 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400661
662 /* now the directory is empty */
663 err = btrfs_unlink_trans(trans, root, dir, dentry);
664 if (!err) {
665 inode->i_size = 0;
666 }
Chris Mason39544012007-12-12 14:38:19 -0500667
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400668 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400669 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500670fail:
Yan134d4512007-10-25 15:49:25 -0400671 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400672 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500673 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500674
Chris Mason39279cc2007-06-12 06:35:45 -0400675 if (ret && !err)
676 err = ret;
677 return err;
678}
679
680static int btrfs_free_inode(struct btrfs_trans_handle *trans,
681 struct btrfs_root *root,
682 struct inode *inode)
683{
684 struct btrfs_path *path;
685 int ret;
686
687 clear_inode(inode);
688
689 path = btrfs_alloc_path();
690 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400691 ret = btrfs_lookup_inode(trans, root, path,
692 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400693 if (ret > 0)
694 ret = -ENOENT;
695 if (!ret)
696 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400697 btrfs_free_path(path);
698 return ret;
699}
700
701/*
Chris Mason39279cc2007-06-12 06:35:45 -0400702 * this can truncate away extent items, csum items and directory items.
703 * It starts at a high offset and removes keys until it can't find
704 * any higher than i_size.
705 *
706 * csum items that cross the new i_size are truncated to the new size
707 * as well.
708 */
709static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root,
711 struct inode *inode)
712{
713 int ret;
714 struct btrfs_path *path;
715 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400716 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400717 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400718 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400719 struct btrfs_file_extent_item *fi;
720 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400721 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400722 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500723 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500724 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400725 int found_extent;
726 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400727 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400728
Chris Masona52d9a82007-08-27 16:49:44 -0400729 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400730 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400731 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400732 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400733
Chris Mason39279cc2007-06-12 06:35:45 -0400734 /* FIXME, add redo link to tree so we don't leak on crash */
735 key.objectid = inode->i_ino;
736 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400737 key.type = (u8)-1;
738
Chris Mason39279cc2007-06-12 06:35:45 -0400739 while(1) {
740 btrfs_init_path(path);
741 fi = NULL;
742 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
743 if (ret < 0) {
744 goto error;
745 }
746 if (ret > 0) {
747 BUG_ON(path->slots[0] == 0);
748 path->slots[0]--;
749 }
Chris Mason5f39d392007-10-15 16:14:19 -0400750 leaf = path->nodes[0];
751 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
752 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400753
Chris Mason5f39d392007-10-15 16:14:19 -0400754 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400755 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400756
Chris Mason39279cc2007-06-12 06:35:45 -0400757 if (found_type != BTRFS_CSUM_ITEM_KEY &&
758 found_type != BTRFS_DIR_ITEM_KEY &&
759 found_type != BTRFS_DIR_INDEX_KEY &&
760 found_type != BTRFS_EXTENT_DATA_KEY)
761 break;
762
Chris Mason5f39d392007-10-15 16:14:19 -0400763 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400764 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400765 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400766 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400767 extent_type = btrfs_file_extent_type(leaf, fi);
768 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400769 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400770 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400771 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
772 struct btrfs_item *item = btrfs_item_nr(leaf,
773 path->slots[0]);
774 item_end += btrfs_file_extent_inline_len(leaf,
775 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400776 }
Yan008630c2007-11-07 13:31:09 -0500777 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400778 }
779 if (found_type == BTRFS_CSUM_ITEM_KEY) {
780 ret = btrfs_csum_truncate(trans, root, path,
781 inode->i_size);
782 BUG_ON(ret);
783 }
Yan008630c2007-11-07 13:31:09 -0500784 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400785 if (found_type == BTRFS_DIR_ITEM_KEY) {
786 found_type = BTRFS_INODE_ITEM_KEY;
787 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
788 found_type = BTRFS_CSUM_ITEM_KEY;
789 } else if (found_type) {
790 found_type--;
791 } else {
792 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400793 }
Yana61721d2007-09-17 11:08:38 -0400794 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400795 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400796 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400797 }
Chris Mason5f39d392007-10-15 16:14:19 -0400798 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400799 del_item = 1;
800 else
801 del_item = 0;
802 found_extent = 0;
803
804 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400805 if (found_type != BTRFS_EXTENT_DATA_KEY)
806 goto delete;
807
808 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400809 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400810 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400811 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400812 u64 orig_num_bytes =
813 btrfs_file_extent_num_bytes(leaf, fi);
814 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400815 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400816 btrfs_set_file_extent_num_bytes(leaf, fi,
817 extent_num_bytes);
818 num_dec = (orig_num_bytes -
819 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400820 if (extent_start != 0) {
821 inode->i_blocks -= num_dec;
822 }
Chris Mason5f39d392007-10-15 16:14:19 -0400823 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400824 } else {
Chris Masondb945352007-10-15 16:15:53 -0400825 extent_num_bytes =
826 btrfs_file_extent_disk_num_bytes(leaf,
827 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400828 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400829 num_dec = btrfs_file_extent_num_bytes(leaf,
830 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400831 if (extent_start != 0) {
832 found_extent = 1;
833 inode->i_blocks -= num_dec;
834 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500835 root_gen = btrfs_header_generation(leaf);
836 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400837 }
Chris Mason179e29e2007-11-01 11:28:41 -0400838 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
839 !del_item) {
840 u32 newsize = inode->i_size - found_key.offset;
841 newsize = btrfs_file_extent_calc_inline_size(newsize);
842 ret = btrfs_truncate_item(trans, root, path,
843 newsize, 1);
844 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400845 }
Chris Mason179e29e2007-11-01 11:28:41 -0400846delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400847 if (del_item) {
848 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400849 if (ret)
850 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400851 } else {
852 break;
853 }
854 btrfs_release_path(root, path);
855 if (found_extent) {
856 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500857 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500858 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500859 root_gen, inode->i_ino,
860 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400861 BUG_ON(ret);
862 }
863 }
864 ret = 0;
865error:
866 btrfs_release_path(root, path);
867 btrfs_free_path(path);
868 inode->i_sb->s_dirt = 1;
869 return ret;
870}
871
Chris Masonb888db22007-08-27 16:49:44 -0400872static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400873 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400874{
Chris Mason39279cc2007-06-12 06:35:45 -0400875 char *kaddr;
Chris Masonb888db22007-08-27 16:49:44 -0400876 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400878 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400879 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500880 u64 existing_delalloc;
881 u64 delalloc_start;
882 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400883
Chris Mason190662b2007-12-18 16:25:45 -0500884 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400885 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400886
Chris Masonb888db22007-08-27 16:49:44 -0400887 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500888 delalloc_start = page_start;
889 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
890 &delalloc_start, page_end,
891 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db22007-08-27 16:49:44 -0400892 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
893 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500894
895 spin_lock(&root->fs_info->delalloc_lock);
896 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
897 spin_unlock(&root->fs_info->delalloc_lock);
898
Chris Masona52d9a82007-08-27 16:49:44 -0400899 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400900 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400901 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
902 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400903 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400904 }
Chris Masonb888db22007-08-27 16:49:44 -0400905 set_page_dirty(page);
906 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400907
Chris Masona52d9a82007-08-27 16:49:44 -0400908 return ret;
909}
910
911/*
912 * taken from block_truncate_page, but does cow as it zeros out
913 * any bytes left in the last page in the file.
914 */
915static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
916{
917 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400918 struct btrfs_root *root = BTRFS_I(inode)->root;
919 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400920 pgoff_t index = from >> PAGE_CACHE_SHIFT;
921 unsigned offset = from & (PAGE_CACHE_SIZE-1);
922 struct page *page;
923 int ret = 0;
924 u64 page_start;
925
926 if ((offset & (blocksize - 1)) == 0)
927 goto out;
928
929 ret = -ENOMEM;
930 page = grab_cache_page(mapping, index);
931 if (!page)
932 goto out;
933 if (!PageUptodate(page)) {
934 ret = btrfs_readpage(NULL, page);
935 lock_page(page);
936 if (!PageUptodate(page)) {
937 ret = -EIO;
938 goto out;
939 }
940 }
Chris Mason35ebb932007-10-30 16:56:53 -0400941 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400942
Chris Masonb888db22007-08-27 16:49:44 -0400943 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400944
Chris Mason39279cc2007-06-12 06:35:45 -0400945 unlock_page(page);
946 page_cache_release(page);
947out:
948 return ret;
949}
950
951static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
952{
953 struct inode *inode = dentry->d_inode;
954 int err;
955
956 err = inode_change_ok(inode, attr);
957 if (err)
958 return err;
959
960 if (S_ISREG(inode->i_mode) &&
961 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
962 struct btrfs_trans_handle *trans;
963 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400964 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
965
Chris Mason5f39d392007-10-15 16:14:19 -0400966 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400967 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400968 u64 block_end = attr->ia_size | mask;
Chris Mason5f564062008-01-22 16:47:59 -0500969 u64 hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400970 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400971 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400972
973 if (attr->ia_size <= pos)
974 goto out;
975
Chris Mason5f564062008-01-22 16:47:59 -0500976 if (pos != inode->i_size)
977 hole_start = pos + root->sectorsize;
978 else
979 hole_start = pos;
980
Chris Mason1832a6d2007-12-21 16:27:21 -0500981 mutex_lock(&root->fs_info->fs_mutex);
982 err = btrfs_check_free_space(root, 1, 0);
983 mutex_unlock(&root->fs_info->fs_mutex);
984 if (err)
985 goto fail;
986
Chris Mason39279cc2007-06-12 06:35:45 -0400987 btrfs_truncate_page(inode->i_mapping, inode->i_size);
988
Chris Mason2bf5a722007-08-30 11:54:02 -0400989 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -0500990 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400991
992 mutex_lock(&root->fs_info->fs_mutex);
993 trans = btrfs_start_transaction(root, 1);
994 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400995 err = btrfs_drop_extents(trans, root, inode,
Chris Mason5f564062008-01-22 16:47:59 -0500996 pos, block_end, pos,
Chris Mason3326d1b2007-10-15 16:18:25 -0400997 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400998
Chris Mason179e29e2007-11-01 11:28:41 -0400999 if (alloc_hint != EXTENT_MAP_INLINE) {
1000 err = btrfs_insert_file_extent(trans, root,
1001 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001002 hole_start, 0, 0,
1003 hole_size);
1004 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001005 }
Chris Mason39279cc2007-06-12 06:35:45 -04001006 btrfs_end_transaction(trans, root);
1007 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001008 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001009 if (err)
1010 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001011 }
1012out:
1013 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001014fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001015 return err;
1016}
Chris Mason61295eb2008-01-14 16:24:38 -05001017
Chris Mason2da98f02008-01-16 11:44:43 -05001018void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001019{
Chris Mason2da98f02008-01-16 11:44:43 -05001020 int ret;
1021
1022 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001023 return;
1024 }
Chris Mason2da98f02008-01-16 11:44:43 -05001025
1026 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1027 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1028 return;
1029
1030 ret = btrfs_del_ordered_inode(inode);
1031 if (ret == 1) {
1032 atomic_dec(&inode->i_count);
1033 }
Chris Mason61295eb2008-01-14 16:24:38 -05001034}
1035
Chris Mason39279cc2007-06-12 06:35:45 -04001036void btrfs_delete_inode(struct inode *inode)
1037{
1038 struct btrfs_trans_handle *trans;
1039 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001040 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001041 int ret;
1042
1043 truncate_inode_pages(&inode->i_data, 0);
1044 if (is_bad_inode(inode)) {
1045 goto no_delete;
1046 }
Chris Mason5f39d392007-10-15 16:14:19 -04001047
Chris Mason39279cc2007-06-12 06:35:45 -04001048 inode->i_size = 0;
1049 mutex_lock(&root->fs_info->fs_mutex);
1050 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001051
Chris Mason39279cc2007-06-12 06:35:45 -04001052 btrfs_set_trans_block_group(trans, inode);
1053 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001054 if (ret)
1055 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001056 ret = btrfs_delete_xattrs(trans, root, inode);
1057 if (ret)
1058 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001059 ret = btrfs_free_inode(trans, root, inode);
1060 if (ret)
1061 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001062 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001063
Chris Mason39279cc2007-06-12 06:35:45 -04001064 btrfs_end_transaction(trans, root);
1065 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001066 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001067 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001068 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001069
1070no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001071 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001072 btrfs_end_transaction(trans, root);
1073 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001074 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001075 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001076no_delete:
1077 clear_inode(inode);
1078}
1079
1080/*
1081 * this returns the key found in the dir entry in the location pointer.
1082 * If no dir entries were found, location->objectid is 0.
1083 */
1084static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1085 struct btrfs_key *location)
1086{
1087 const char *name = dentry->d_name.name;
1088 int namelen = dentry->d_name.len;
1089 struct btrfs_dir_item *di;
1090 struct btrfs_path *path;
1091 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001092 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001093
Chris Mason39544012007-12-12 14:38:19 -05001094 if (namelen == 1 && strcmp(name, ".") == 0) {
1095 location->objectid = dir->i_ino;
1096 location->type = BTRFS_INODE_ITEM_KEY;
1097 location->offset = 0;
1098 return 0;
1099 }
Chris Mason39279cc2007-06-12 06:35:45 -04001100 path = btrfs_alloc_path();
1101 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001102
Chris Mason7a720532007-12-13 09:06:59 -05001103 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001104 struct btrfs_key key;
1105 struct extent_buffer *leaf;
1106 u32 nritems;
1107 int slot;
1108
1109 key.objectid = dir->i_ino;
1110 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1111 key.offset = 0;
1112 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1113 BUG_ON(ret == 0);
1114 ret = 0;
1115
1116 leaf = path->nodes[0];
1117 slot = path->slots[0];
1118 nritems = btrfs_header_nritems(leaf);
1119 if (slot >= nritems)
1120 goto out_err;
1121
1122 btrfs_item_key_to_cpu(leaf, &key, slot);
1123 if (key.objectid != dir->i_ino ||
1124 key.type != BTRFS_INODE_REF_KEY) {
1125 goto out_err;
1126 }
1127 location->objectid = key.offset;
1128 location->type = BTRFS_INODE_ITEM_KEY;
1129 location->offset = 0;
1130 goto out;
1131 }
1132
Chris Mason39279cc2007-06-12 06:35:45 -04001133 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1134 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001135 if (IS_ERR(di))
1136 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001137 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001138 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001139 }
Chris Mason5f39d392007-10-15 16:14:19 -04001140 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001141out:
Chris Mason39279cc2007-06-12 06:35:45 -04001142 btrfs_free_path(path);
1143 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001144out_err:
1145 location->objectid = 0;
1146 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001147}
1148
1149/*
1150 * when we hit a tree root in a directory, the btrfs part of the inode
1151 * needs to be changed to reflect the root directory of the tree root. This
1152 * is kind of like crossing a mount point.
1153 */
1154static int fixup_tree_root_location(struct btrfs_root *root,
1155 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001156 struct btrfs_root **sub_root,
1157 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001158{
1159 struct btrfs_path *path;
1160 struct btrfs_root_item *ri;
1161
1162 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1163 return 0;
1164 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1165 return 0;
1166
1167 path = btrfs_alloc_path();
1168 BUG_ON(!path);
1169 mutex_lock(&root->fs_info->fs_mutex);
1170
Josef Bacik58176a92007-08-29 15:47:34 -04001171 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1172 dentry->d_name.name,
1173 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001174 if (IS_ERR(*sub_root))
1175 return PTR_ERR(*sub_root);
1176
1177 ri = &(*sub_root)->root_item;
1178 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001179 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1180 location->offset = 0;
1181
1182 btrfs_free_path(path);
1183 mutex_unlock(&root->fs_info->fs_mutex);
1184 return 0;
1185}
1186
1187static int btrfs_init_locked_inode(struct inode *inode, void *p)
1188{
1189 struct btrfs_iget_args *args = p;
1190 inode->i_ino = args->ino;
1191 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001192 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1193 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001194 return 0;
1195}
1196
1197static int btrfs_find_actor(struct inode *inode, void *opaque)
1198{
1199 struct btrfs_iget_args *args = opaque;
1200 return (args->ino == inode->i_ino &&
1201 args->root == BTRFS_I(inode)->root);
1202}
1203
Chris Masondc17ff82008-01-08 15:46:30 -05001204struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1205 u64 root_objectid)
1206{
1207 struct btrfs_iget_args args;
1208 args.ino = objectid;
1209 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1210
1211 if (!args.root)
1212 return NULL;
1213
1214 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1215}
1216
Chris Mason39279cc2007-06-12 06:35:45 -04001217struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1218 struct btrfs_root *root)
1219{
1220 struct inode *inode;
1221 struct btrfs_iget_args args;
1222 args.ino = objectid;
1223 args.root = root;
1224
1225 inode = iget5_locked(s, objectid, btrfs_find_actor,
1226 btrfs_init_locked_inode,
1227 (void *)&args);
1228 return inode;
1229}
1230
1231static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1232 struct nameidata *nd)
1233{
1234 struct inode * inode;
1235 struct btrfs_inode *bi = BTRFS_I(dir);
1236 struct btrfs_root *root = bi->root;
1237 struct btrfs_root *sub_root = root;
1238 struct btrfs_key location;
1239 int ret;
1240
1241 if (dentry->d_name.len > BTRFS_NAME_LEN)
1242 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001243
Chris Mason39279cc2007-06-12 06:35:45 -04001244 mutex_lock(&root->fs_info->fs_mutex);
1245 ret = btrfs_inode_by_name(dir, dentry, &location);
1246 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001247
Chris Mason39279cc2007-06-12 06:35:45 -04001248 if (ret < 0)
1249 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001250
Chris Mason39279cc2007-06-12 06:35:45 -04001251 inode = NULL;
1252 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001253 ret = fixup_tree_root_location(root, &location, &sub_root,
1254 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001255 if (ret < 0)
1256 return ERR_PTR(ret);
1257 if (ret > 0)
1258 return ERR_PTR(-ENOENT);
1259 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1260 sub_root);
1261 if (!inode)
1262 return ERR_PTR(-EACCES);
1263 if (inode->i_state & I_NEW) {
1264 /* the inode and parent dir are two different roots */
1265 if (sub_root != root) {
1266 igrab(inode);
1267 sub_root->inode = inode;
1268 }
1269 BTRFS_I(inode)->root = sub_root;
1270 memcpy(&BTRFS_I(inode)->location, &location,
1271 sizeof(location));
1272 btrfs_read_locked_inode(inode);
1273 unlock_new_inode(inode);
1274 }
1275 }
1276 return d_splice_alias(inode, dentry);
1277}
1278
Chris Mason39279cc2007-06-12 06:35:45 -04001279static unsigned char btrfs_filetype_table[] = {
1280 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1281};
1282
1283static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1284{
Chris Mason6da6aba2007-12-18 16:15:09 -05001285 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001286 struct btrfs_root *root = BTRFS_I(inode)->root;
1287 struct btrfs_item *item;
1288 struct btrfs_dir_item *di;
1289 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001290 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001291 struct btrfs_path *path;
1292 int ret;
1293 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001294 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001295 int slot;
1296 int advance;
1297 unsigned char d_type;
1298 int over = 0;
1299 u32 di_cur;
1300 u32 di_total;
1301 u32 di_len;
1302 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001303 char tmp_name[32];
1304 char *name_ptr;
1305 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001306
1307 /* FIXME, use a real flag for deciding about the key type */
1308 if (root->fs_info->tree_root == root)
1309 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001310
Chris Mason39544012007-12-12 14:38:19 -05001311 /* special case for "." */
1312 if (filp->f_pos == 0) {
1313 over = filldir(dirent, ".", 1,
1314 1, inode->i_ino,
1315 DT_DIR);
1316 if (over)
1317 return 0;
1318 filp->f_pos = 1;
1319 }
1320
Chris Mason39279cc2007-06-12 06:35:45 -04001321 mutex_lock(&root->fs_info->fs_mutex);
1322 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001323 path = btrfs_alloc_path();
1324 path->reada = 2;
1325
1326 /* special case for .., just use the back ref */
1327 if (filp->f_pos == 1) {
1328 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1329 key.offset = 0;
1330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1331 BUG_ON(ret == 0);
1332 leaf = path->nodes[0];
1333 slot = path->slots[0];
1334 nritems = btrfs_header_nritems(leaf);
1335 if (slot >= nritems) {
1336 btrfs_release_path(root, path);
1337 goto read_dir_items;
1338 }
1339 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1340 btrfs_release_path(root, path);
1341 if (found_key.objectid != key.objectid ||
1342 found_key.type != BTRFS_INODE_REF_KEY)
1343 goto read_dir_items;
1344 over = filldir(dirent, "..", 2,
1345 2, found_key.offset, DT_DIR);
1346 if (over)
1347 goto nopos;
1348 filp->f_pos = 2;
1349 }
1350
1351read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001352 btrfs_set_key_type(&key, key_type);
1353 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001354
Chris Mason39279cc2007-06-12 06:35:45 -04001355 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1356 if (ret < 0)
1357 goto err;
1358 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001359 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001360 leaf = path->nodes[0];
1361 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001362 slot = path->slots[0];
1363 if (advance || slot >= nritems) {
1364 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001365 ret = btrfs_next_leaf(root, path);
1366 if (ret)
1367 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001368 leaf = path->nodes[0];
1369 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001370 slot = path->slots[0];
1371 } else {
1372 slot++;
1373 path->slots[0]++;
1374 }
1375 }
1376 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001377 item = btrfs_item_nr(leaf, slot);
1378 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1379
1380 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001381 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001382 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001383 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001384 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001385 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001386
1387 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001388 advance = 1;
1389 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1390 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001391 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001392 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001393 struct btrfs_key location;
1394
1395 name_len = btrfs_dir_name_len(leaf, di);
1396 if (name_len < 32) {
1397 name_ptr = tmp_name;
1398 } else {
1399 name_ptr = kmalloc(name_len, GFP_NOFS);
1400 BUG_ON(!name_ptr);
1401 }
1402 read_extent_buffer(leaf, name_ptr,
1403 (unsigned long)(di + 1), name_len);
1404
1405 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1406 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001407 over = filldir(dirent, name_ptr, name_len,
1408 found_key.offset,
1409 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001410 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001411
1412 if (name_ptr != tmp_name)
1413 kfree(name_ptr);
1414
Chris Mason39279cc2007-06-12 06:35:45 -04001415 if (over)
1416 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001417 di_len = btrfs_dir_name_len(leaf, di) +
1418 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001419 di_cur += di_len;
1420 di = (struct btrfs_dir_item *)((char *)di + di_len);
1421 }
1422 }
1423 filp->f_pos++;
1424nopos:
1425 ret = 0;
1426err:
1427 btrfs_release_path(root, path);
1428 btrfs_free_path(path);
1429 mutex_unlock(&root->fs_info->fs_mutex);
1430 return ret;
1431}
1432
1433int btrfs_write_inode(struct inode *inode, int wait)
1434{
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
1436 struct btrfs_trans_handle *trans;
1437 int ret = 0;
1438
1439 if (wait) {
1440 mutex_lock(&root->fs_info->fs_mutex);
1441 trans = btrfs_start_transaction(root, 1);
1442 btrfs_set_trans_block_group(trans, inode);
1443 ret = btrfs_commit_transaction(trans, root);
1444 mutex_unlock(&root->fs_info->fs_mutex);
1445 }
1446 return ret;
1447}
1448
1449/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001450 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001451 * inode changes. But, it is most likely to find the inode in cache.
1452 * FIXME, needs more benchmarking...there are no reasons other than performance
1453 * to keep or drop this code.
1454 */
1455void btrfs_dirty_inode(struct inode *inode)
1456{
1457 struct btrfs_root *root = BTRFS_I(inode)->root;
1458 struct btrfs_trans_handle *trans;
1459
1460 mutex_lock(&root->fs_info->fs_mutex);
1461 trans = btrfs_start_transaction(root, 1);
1462 btrfs_set_trans_block_group(trans, inode);
1463 btrfs_update_inode(trans, root, inode);
1464 btrfs_end_transaction(trans, root);
1465 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001466}
1467
1468static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1469 struct btrfs_root *root,
1470 u64 objectid,
1471 struct btrfs_block_group_cache *group,
1472 int mode)
1473{
1474 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001475 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001476 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001477 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001478 int ret;
1479 int owner;
1480
Chris Mason5f39d392007-10-15 16:14:19 -04001481 path = btrfs_alloc_path();
1482 BUG_ON(!path);
1483
Chris Mason39279cc2007-06-12 06:35:45 -04001484 inode = new_inode(root->fs_info->sb);
1485 if (!inode)
1486 return ERR_PTR(-ENOMEM);
1487
Chris Masonb888db22007-08-27 16:49:44 -04001488 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1489 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001490 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001491
Chris Mason39279cc2007-06-12 06:35:45 -04001492 if (mode & S_IFDIR)
1493 owner = 0;
1494 else
1495 owner = 1;
1496 group = btrfs_find_block_group(root, group, 0, 0, owner);
1497 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001498 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001499 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1500 if (ret)
1501 goto fail;
1502
Chris Mason39279cc2007-06-12 06:35:45 -04001503 inode->i_uid = current->fsuid;
1504 inode->i_gid = current->fsgid;
1505 inode->i_mode = mode;
1506 inode->i_ino = objectid;
1507 inode->i_blocks = 0;
1508 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001509 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1510 struct btrfs_inode_item);
1511 fill_inode_item(path->nodes[0], inode_item, inode);
1512 btrfs_mark_buffer_dirty(path->nodes[0]);
1513 btrfs_free_path(path);
1514
Chris Mason39279cc2007-06-12 06:35:45 -04001515 location = &BTRFS_I(inode)->location;
1516 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001517 location->offset = 0;
1518 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1519
Chris Mason39279cc2007-06-12 06:35:45 -04001520 insert_inode_hash(inode);
1521 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001522fail:
1523 btrfs_free_path(path);
1524 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001525}
1526
1527static inline u8 btrfs_inode_type(struct inode *inode)
1528{
1529 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1530}
1531
1532static int btrfs_add_link(struct btrfs_trans_handle *trans,
1533 struct dentry *dentry, struct inode *inode)
1534{
1535 int ret;
1536 struct btrfs_key key;
1537 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001538 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001539
Chris Mason39279cc2007-06-12 06:35:45 -04001540 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001541 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1542 key.offset = 0;
1543
1544 ret = btrfs_insert_dir_item(trans, root,
1545 dentry->d_name.name, dentry->d_name.len,
1546 dentry->d_parent->d_inode->i_ino,
1547 &key, btrfs_inode_type(inode));
1548 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001549 ret = btrfs_insert_inode_ref(trans, root,
1550 dentry->d_name.name,
1551 dentry->d_name.len,
1552 inode->i_ino,
1553 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001554 parent_inode = dentry->d_parent->d_inode;
1555 parent_inode->i_size += dentry->d_name.len * 2;
1556 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001557 ret = btrfs_update_inode(trans, root,
1558 dentry->d_parent->d_inode);
1559 }
1560 return ret;
1561}
1562
1563static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1564 struct dentry *dentry, struct inode *inode)
1565{
1566 int err = btrfs_add_link(trans, dentry, inode);
1567 if (!err) {
1568 d_instantiate(dentry, inode);
1569 return 0;
1570 }
1571 if (err > 0)
1572 err = -EEXIST;
1573 return err;
1574}
1575
Josef Bacik618e21d2007-07-11 10:18:17 -04001576static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1577 int mode, dev_t rdev)
1578{
1579 struct btrfs_trans_handle *trans;
1580 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001581 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001582 int err;
1583 int drop_inode = 0;
1584 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001585 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001586
1587 if (!new_valid_dev(rdev))
1588 return -EINVAL;
1589
1590 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001591 err = btrfs_check_free_space(root, 1, 0);
1592 if (err)
1593 goto fail;
1594
Josef Bacik618e21d2007-07-11 10:18:17 -04001595 trans = btrfs_start_transaction(root, 1);
1596 btrfs_set_trans_block_group(trans, dir);
1597
1598 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1599 if (err) {
1600 err = -ENOSPC;
1601 goto out_unlock;
1602 }
1603
1604 inode = btrfs_new_inode(trans, root, objectid,
1605 BTRFS_I(dir)->block_group, mode);
1606 err = PTR_ERR(inode);
1607 if (IS_ERR(inode))
1608 goto out_unlock;
1609
1610 btrfs_set_trans_block_group(trans, inode);
1611 err = btrfs_add_nondir(trans, dentry, inode);
1612 if (err)
1613 drop_inode = 1;
1614 else {
1615 inode->i_op = &btrfs_special_inode_operations;
1616 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001617 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001618 }
1619 dir->i_sb->s_dirt = 1;
1620 btrfs_update_inode_block_group(trans, inode);
1621 btrfs_update_inode_block_group(trans, dir);
1622out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001623 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001624 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001625fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001626 mutex_unlock(&root->fs_info->fs_mutex);
1627
1628 if (drop_inode) {
1629 inode_dec_link_count(inode);
1630 iput(inode);
1631 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001632 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001633 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001634 return err;
1635}
1636
Chris Mason39279cc2007-06-12 06:35:45 -04001637static int btrfs_create(struct inode *dir, struct dentry *dentry,
1638 int mode, struct nameidata *nd)
1639{
1640 struct btrfs_trans_handle *trans;
1641 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001642 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001643 int err;
1644 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001645 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001646 u64 objectid;
1647
1648 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001649 err = btrfs_check_free_space(root, 1, 0);
1650 if (err)
1651 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001652 trans = btrfs_start_transaction(root, 1);
1653 btrfs_set_trans_block_group(trans, dir);
1654
1655 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1656 if (err) {
1657 err = -ENOSPC;
1658 goto out_unlock;
1659 }
1660
1661 inode = btrfs_new_inode(trans, root, objectid,
1662 BTRFS_I(dir)->block_group, mode);
1663 err = PTR_ERR(inode);
1664 if (IS_ERR(inode))
1665 goto out_unlock;
1666
1667 btrfs_set_trans_block_group(trans, inode);
1668 err = btrfs_add_nondir(trans, dentry, inode);
1669 if (err)
1670 drop_inode = 1;
1671 else {
1672 inode->i_mapping->a_ops = &btrfs_aops;
1673 inode->i_fop = &btrfs_file_operations;
1674 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001675 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1676 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001677 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001678 }
1679 dir->i_sb->s_dirt = 1;
1680 btrfs_update_inode_block_group(trans, inode);
1681 btrfs_update_inode_block_group(trans, dir);
1682out_unlock:
Chris Masond3c2fdcf2007-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 Masond3c2fdcf2007-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
1697static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1698 struct dentry *dentry)
1699{
1700 struct btrfs_trans_handle *trans;
1701 struct btrfs_root *root = BTRFS_I(dir)->root;
1702 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001703 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001704 int err;
1705 int drop_inode = 0;
1706
1707 if (inode->i_nlink == 0)
1708 return -ENOENT;
1709
Chris Mason6da6aba2007-12-18 16:15:09 -05001710#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1711 inode->i_nlink++;
1712#else
Chris Mason39279cc2007-06-12 06:35:45 -04001713 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001714#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001715 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001716 err = btrfs_check_free_space(root, 1, 0);
1717 if (err)
1718 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001719 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001720
Chris Mason39279cc2007-06-12 06:35:45 -04001721 btrfs_set_trans_block_group(trans, dir);
1722 atomic_inc(&inode->i_count);
1723 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001724
Chris Mason39279cc2007-06-12 06:35:45 -04001725 if (err)
1726 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001727
Chris Mason39279cc2007-06-12 06:35:45 -04001728 dir->i_sb->s_dirt = 1;
1729 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001730 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001731
Chris Mason54aa1f42007-06-22 14:16:25 -04001732 if (err)
1733 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001734
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001735 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001736 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001737fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001738 mutex_unlock(&root->fs_info->fs_mutex);
1739
1740 if (drop_inode) {
1741 inode_dec_link_count(inode);
1742 iput(inode);
1743 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001744 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001745 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001746 return err;
1747}
1748
Chris Mason39279cc2007-06-12 06:35:45 -04001749static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1750{
1751 struct inode *inode;
1752 struct btrfs_trans_handle *trans;
1753 struct btrfs_root *root = BTRFS_I(dir)->root;
1754 int err = 0;
1755 int drop_on_err = 0;
1756 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001757 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001758
1759 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001760 err = btrfs_check_free_space(root, 1, 0);
1761 if (err)
1762 goto out_unlock;
1763
Chris Mason39279cc2007-06-12 06:35:45 -04001764 trans = btrfs_start_transaction(root, 1);
1765 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001766
Chris Mason39279cc2007-06-12 06:35:45 -04001767 if (IS_ERR(trans)) {
1768 err = PTR_ERR(trans);
1769 goto out_unlock;
1770 }
1771
1772 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1773 if (err) {
1774 err = -ENOSPC;
1775 goto out_unlock;
1776 }
1777
1778 inode = btrfs_new_inode(trans, root, objectid,
1779 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1780 if (IS_ERR(inode)) {
1781 err = PTR_ERR(inode);
1782 goto out_fail;
1783 }
Chris Mason5f39d392007-10-15 16:14:19 -04001784
Chris Mason39279cc2007-06-12 06:35:45 -04001785 drop_on_err = 1;
1786 inode->i_op = &btrfs_dir_inode_operations;
1787 inode->i_fop = &btrfs_dir_file_operations;
1788 btrfs_set_trans_block_group(trans, inode);
1789
Chris Mason39544012007-12-12 14:38:19 -05001790 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001791 err = btrfs_update_inode(trans, root, inode);
1792 if (err)
1793 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001794
Chris Mason39279cc2007-06-12 06:35:45 -04001795 err = btrfs_add_link(trans, dentry, inode);
1796 if (err)
1797 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001798
Chris Mason39279cc2007-06-12 06:35:45 -04001799 d_instantiate(dentry, inode);
1800 drop_on_err = 0;
1801 dir->i_sb->s_dirt = 1;
1802 btrfs_update_inode_block_group(trans, inode);
1803 btrfs_update_inode_block_group(trans, dir);
1804
1805out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001806 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001807 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001808
Chris Mason39279cc2007-06-12 06:35:45 -04001809out_unlock:
1810 mutex_unlock(&root->fs_info->fs_mutex);
1811 if (drop_on_err)
1812 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001813 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001814 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001815 return err;
1816}
1817
Chris Masona52d9a82007-08-27 16:49:44 -04001818struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1819 size_t page_offset, u64 start, u64 end,
1820 int create)
1821{
1822 int ret;
1823 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001824 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001825 u64 extent_start = 0;
1826 u64 extent_end = 0;
1827 u64 objectid = inode->i_ino;
1828 u32 found_type;
1829 int failed_insert = 0;
1830 struct btrfs_path *path;
1831 struct btrfs_root *root = BTRFS_I(inode)->root;
1832 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001833 struct extent_buffer *leaf;
1834 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001835 struct extent_map *em = NULL;
1836 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1837 struct btrfs_trans_handle *trans = NULL;
1838
1839 path = btrfs_alloc_path();
1840 BUG_ON(!path);
1841 mutex_lock(&root->fs_info->fs_mutex);
1842
1843again:
1844 em = lookup_extent_mapping(em_tree, start, end);
1845 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001846 if (em->start > start) {
1847 printk("get_extent start %Lu em start %Lu\n",
1848 start, em->start);
1849 WARN_ON(1);
1850 }
Chris Masona52d9a82007-08-27 16:49:44 -04001851 goto out;
1852 }
1853 if (!em) {
1854 em = alloc_extent_map(GFP_NOFS);
1855 if (!em) {
1856 err = -ENOMEM;
1857 goto out;
1858 }
Chris Mason5f39d392007-10-15 16:14:19 -04001859 em->start = EXTENT_MAP_HOLE;
1860 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001861 }
1862 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001863 ret = btrfs_lookup_file_extent(trans, root, path,
1864 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001865 if (ret < 0) {
1866 err = ret;
1867 goto out;
1868 }
1869
1870 if (ret != 0) {
1871 if (path->slots[0] == 0)
1872 goto not_found;
1873 path->slots[0]--;
1874 }
1875
Chris Mason5f39d392007-10-15 16:14:19 -04001876 leaf = path->nodes[0];
1877 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001878 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001879 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001880 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1881 found_type = btrfs_key_type(&found_key);
1882 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001883 found_type != BTRFS_EXTENT_DATA_KEY) {
1884 goto not_found;
1885 }
1886
Chris Mason5f39d392007-10-15 16:14:19 -04001887 found_type = btrfs_file_extent_type(leaf, item);
1888 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001889 if (found_type == BTRFS_FILE_EXTENT_REG) {
1890 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001891 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001892 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001893 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001894 em->start = start;
1895 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001896 if (end < extent_start)
1897 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001898 em->end = extent_end - 1;
1899 } else {
1900 em->end = end;
1901 }
1902 goto not_found_em;
1903 }
Chris Masondb945352007-10-15 16:15:53 -04001904 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1905 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001906 em->start = extent_start;
1907 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001908 em->block_start = EXTENT_MAP_HOLE;
1909 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001910 goto insert;
1911 }
Chris Masondb945352007-10-15 16:15:53 -04001912 bytenr += btrfs_file_extent_offset(leaf, item);
1913 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001914 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001915 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001916 em->start = extent_start;
1917 em->end = extent_end - 1;
1918 goto insert;
1919 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001920 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001921 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001922 size_t size;
1923 size_t extent_offset;
1924 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001925
Chris Mason5f39d392007-10-15 16:14:19 -04001926 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1927 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001928 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001929 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001930 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001931 em->start = start;
1932 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001933 if (end < extent_start)
1934 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001935 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001936 } else {
1937 em->end = end;
1938 }
1939 goto not_found_em;
1940 }
1941 em->block_start = EXTENT_MAP_INLINE;
1942 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001943
1944 if (!page) {
1945 em->start = extent_start;
1946 em->end = extent_start + size - 1;
1947 goto out;
1948 }
1949
Chris Mason35ebb932007-10-30 16:56:53 -04001950 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001951 extent_start + page_offset;
1952 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1953 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001954 em->start = extent_start + extent_offset;
1955 em->end = (em->start + copy_size -1) |
1956 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001957 map = kmap(page);
1958 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001959 if (create == 0 && !PageUptodate(page)) {
1960 read_extent_buffer(leaf, map + page_offset, ptr,
1961 copy_size);
1962 flush_dcache_page(page);
1963 } else if (create && PageUptodate(page)) {
1964 if (!trans) {
1965 kunmap(page);
1966 free_extent_map(em);
1967 em = NULL;
1968 btrfs_release_path(root, path);
1969 trans = btrfs_start_transaction(root, 1);
1970 goto again;
1971 }
1972 write_extent_buffer(leaf, map + page_offset, ptr,
1973 copy_size);
1974 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001975 }
Chris Masona52d9a82007-08-27 16:49:44 -04001976 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001977 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001978 goto insert;
1979 } else {
1980 printk("unkknown found_type %d\n", found_type);
1981 WARN_ON(1);
1982 }
1983not_found:
1984 em->start = start;
1985 em->end = end;
1986not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001987 em->block_start = EXTENT_MAP_HOLE;
1988 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001989insert:
1990 btrfs_release_path(root, path);
1991 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001992 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001993 err = -EIO;
1994 goto out;
1995 }
1996 ret = add_extent_mapping(em_tree, em);
1997 if (ret == -EEXIST) {
1998 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001999 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05002000 if (0 && failed_insert == 1) {
2001 btrfs_drop_extent_cache(inode, start, end);
2002 }
Chris Masona52d9a82007-08-27 16:49:44 -04002003 failed_insert++;
2004 if (failed_insert > 5) {
2005 printk("failing to insert %Lu %Lu\n", start, end);
2006 err = -EIO;
2007 goto out;
2008 }
Chris Masona52d9a82007-08-27 16:49:44 -04002009 goto again;
2010 }
2011 err = 0;
2012out:
2013 btrfs_free_path(path);
2014 if (trans) {
2015 ret = btrfs_end_transaction(trans, root);
2016 if (!err)
2017 err = ret;
2018 }
2019 mutex_unlock(&root->fs_info->fs_mutex);
2020 if (err) {
2021 free_extent_map(em);
2022 WARN_ON(1);
2023 return ERR_PTR(err);
2024 }
2025 return em;
2026}
2027
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002028static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002029{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002030 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002031}
2032
Chris Mason9ebefb182007-06-15 13:50:00 -04002033int btrfs_readpage(struct file *file, struct page *page)
2034{
Chris Masona52d9a82007-08-27 16:49:44 -04002035 struct extent_map_tree *tree;
2036 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2037 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002038}
Chris Mason1832a6d2007-12-21 16:27:21 -05002039
Chris Mason39279cc2007-06-12 06:35:45 -04002040static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2041{
Chris Masona52d9a82007-08-27 16:49:44 -04002042 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002043
2044
2045 if (current->flags & PF_MEMALLOC) {
2046 redirty_page_for_writepage(wbc, page);
2047 unlock_page(page);
2048 return 0;
2049 }
Chris Masona52d9a82007-08-27 16:49:44 -04002050 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2051 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2052}
Chris Mason39279cc2007-06-12 06:35:45 -04002053
Chris Masonb293f022007-11-01 19:45:34 -04002054static int btrfs_writepages(struct address_space *mapping,
2055 struct writeback_control *wbc)
2056{
2057 struct extent_map_tree *tree;
2058 tree = &BTRFS_I(mapping->host)->extent_tree;
2059 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2060}
2061
Chris Mason3ab2fb52007-11-08 10:59:22 -05002062static int
2063btrfs_readpages(struct file *file, struct address_space *mapping,
2064 struct list_head *pages, unsigned nr_pages)
2065{
2066 struct extent_map_tree *tree;
2067 tree = &BTRFS_I(mapping->host)->extent_tree;
2068 return extent_readpages(tree, mapping, pages, nr_pages,
2069 btrfs_get_extent);
2070}
2071
Chris Masona52d9a82007-08-27 16:49:44 -04002072static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2073{
2074 struct extent_map_tree *tree;
2075 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002076
Chris Masona52d9a82007-08-27 16:49:44 -04002077 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2078 ret = try_release_extent_mapping(tree, page);
2079 if (ret == 1) {
2080 ClearPagePrivate(page);
2081 set_page_private(page, 0);
2082 page_cache_release(page);
2083 }
2084 return ret;
2085}
Chris Mason39279cc2007-06-12 06:35:45 -04002086
Chris Masona52d9a82007-08-27 16:49:44 -04002087static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2088{
2089 struct extent_map_tree *tree;
2090
2091 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2092 extent_invalidatepage(tree, page, offset);
2093 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002094}
2095
Chris Mason9ebefb182007-06-15 13:50:00 -04002096/*
2097 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2098 * called from a page fault handler when a page is first dirtied. Hence we must
2099 * be careful to check for EOF conditions here. We set the page up correctly
2100 * for a written page which means we get ENOSPC checking when writing into
2101 * holes and correct delalloc and unwritten extent mapping on filesystems that
2102 * support these features.
2103 *
2104 * We are not allowed to take the i_mutex here so we have to play games to
2105 * protect against truncate races as the page could now be beyond EOF. Because
2106 * vmtruncate() writes the inode size before removing pages, once we have the
2107 * page lock we can determine safely if the page is beyond EOF. If it is not
2108 * beyond EOF, then the page is guaranteed safe against truncation until we
2109 * unlock the page.
2110 */
2111int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2112{
Chris Mason6da6aba2007-12-18 16:15:09 -05002113 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002114 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002115 unsigned long end;
2116 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002117 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002118 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002119
Chris Mason1832a6d2007-12-21 16:27:21 -05002120 mutex_lock(&root->fs_info->fs_mutex);
2121 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002122 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002123 if (ret)
2124 goto out;
2125
2126 ret = -EINVAL;
2127
Chris Mason9ebefb182007-06-15 13:50:00 -04002128 lock_page(page);
2129 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002130 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002131 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002132
Chris Mason9ebefb182007-06-15 13:50:00 -04002133 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002134 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002135 /* page got truncated out from underneath us */
2136 goto out_unlock;
2137 }
2138
2139 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002140 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002141 end = size & ~PAGE_CACHE_MASK;
2142 else
2143 end = PAGE_CACHE_SIZE;
2144
Chris Masonb888db22007-08-27 16:49:44 -04002145 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002146
2147out_unlock:
2148 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002149out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002150 return ret;
2151}
2152
Chris Mason39279cc2007-06-12 06:35:45 -04002153static void btrfs_truncate(struct inode *inode)
2154{
2155 struct btrfs_root *root = BTRFS_I(inode)->root;
2156 int ret;
2157 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002158 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002159
2160 if (!S_ISREG(inode->i_mode))
2161 return;
2162 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2163 return;
2164
2165 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2166
2167 mutex_lock(&root->fs_info->fs_mutex);
2168 trans = btrfs_start_transaction(root, 1);
2169 btrfs_set_trans_block_group(trans, inode);
2170
2171 /* FIXME, add redo link to tree so we don't leak on crash */
2172 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002173 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002174 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002175
Chris Mason39279cc2007-06-12 06:35:45 -04002176 ret = btrfs_end_transaction(trans, root);
2177 BUG_ON(ret);
2178 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002179 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002180 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002181}
2182
Chris Mason4313b392008-01-03 09:08:48 -05002183static int noinline create_subvol(struct btrfs_root *root, char *name,
2184 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002185{
2186 struct btrfs_trans_handle *trans;
2187 struct btrfs_key key;
2188 struct btrfs_root_item root_item;
2189 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002190 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002191 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002192 struct inode *inode;
2193 struct inode *dir;
2194 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002195 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002196 u64 objectid;
2197 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002198 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002199
2200 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002201 ret = btrfs_check_free_space(root, 1, 0);
2202 if (ret)
2203 goto fail_commit;
2204
Chris Mason39279cc2007-06-12 06:35:45 -04002205 trans = btrfs_start_transaction(root, 1);
2206 BUG_ON(!trans);
2207
Chris Mason7bb86312007-12-11 09:25:06 -05002208 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2209 0, &objectid);
2210 if (ret)
2211 goto fail;
2212
2213 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2214 objectid, trans->transid, 0, 0,
2215 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002216 if (IS_ERR(leaf))
2217 return PTR_ERR(leaf);
2218
2219 btrfs_set_header_nritems(leaf, 0);
2220 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002221 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002222 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002223 btrfs_set_header_owner(leaf, objectid);
2224
Chris Mason5f39d392007-10-15 16:14:19 -04002225 write_extent_buffer(leaf, root->fs_info->fsid,
2226 (unsigned long)btrfs_header_fsid(leaf),
2227 BTRFS_FSID_SIZE);
2228 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002229
2230 inode_item = &root_item.inode;
2231 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002232 inode_item->generation = cpu_to_le64(1);
2233 inode_item->size = cpu_to_le64(3);
2234 inode_item->nlink = cpu_to_le32(1);
2235 inode_item->nblocks = cpu_to_le64(1);
2236 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002237
Chris Masondb945352007-10-15 16:15:53 -04002238 btrfs_set_root_bytenr(&root_item, leaf->start);
2239 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002240 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002241 btrfs_set_root_used(&root_item, 0);
2242
Chris Mason5eda7b52007-06-22 14:16:25 -04002243 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2244 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002245
2246 free_extent_buffer(leaf);
2247 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002248
Chris Mason39279cc2007-06-12 06:35:45 -04002249 btrfs_set_root_dirid(&root_item, new_dirid);
2250
2251 key.objectid = objectid;
2252 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002253 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2254 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2255 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002256 if (ret)
2257 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002258
2259 /*
2260 * insert the directory item
2261 */
2262 key.offset = (u64)-1;
2263 dir = root->fs_info->sb->s_root->d_inode;
2264 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2265 name, namelen, dir->i_ino, &key,
2266 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002267 if (ret)
2268 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002269
Chris Mason39544012007-12-12 14:38:19 -05002270 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2271 name, namelen, objectid,
2272 root->fs_info->sb->s_root->d_inode->i_ino);
2273 if (ret)
2274 goto fail;
2275
Chris Mason39279cc2007-06-12 06:35:45 -04002276 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002277 if (ret)
2278 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002279
Josef Bacik58176a92007-08-29 15:47:34 -04002280 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002281 BUG_ON(!new_root);
2282
2283 trans = btrfs_start_transaction(new_root, 1);
2284 BUG_ON(!trans);
2285
2286 inode = btrfs_new_inode(trans, new_root, new_dirid,
2287 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002288 if (IS_ERR(inode))
2289 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002290 inode->i_op = &btrfs_dir_inode_operations;
2291 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002292 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002293
Chris Mason39544012007-12-12 14:38:19 -05002294 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2295 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002296 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002297 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002298 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002299 if (ret)
2300 goto fail;
2301fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002302 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002303 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002304 if (err && !ret)
2305 ret = err;
2306fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002307 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002308 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002309 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002310 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002311}
2312
2313static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2314{
Chris Mason3063d292008-01-08 15:46:30 -05002315 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002316 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002317 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002318 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002319 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002320
2321 if (!root->ref_cows)
2322 return -EINVAL;
2323
2324 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002325 ret = btrfs_check_free_space(root, 1, 0);
2326 if (ret)
2327 goto fail_unlock;
2328
Chris Mason3063d292008-01-08 15:46:30 -05002329 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2330 if (!pending_snapshot) {
2331 ret = -ENOMEM;
2332 goto fail_unlock;
2333 }
Yanfb4bc1e2008-01-17 11:59:51 -05002334 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002335 if (!pending_snapshot->name) {
2336 ret = -ENOMEM;
2337 kfree(pending_snapshot);
2338 goto fail_unlock;
2339 }
Yanfb4bc1e2008-01-17 11:59:51 -05002340 memcpy(pending_snapshot->name, name, namelen);
2341 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002342 trans = btrfs_start_transaction(root, 1);
2343 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002344 pending_snapshot->root = root;
2345 list_add(&pending_snapshot->list,
2346 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002347 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002348 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002349
Chris Mason1832a6d2007-12-21 16:27:21 -05002350fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002351 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002352 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002353 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002354 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002355}
2356
Chris Masonedbd8d42007-12-21 16:27:24 -05002357unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002358 struct file_ra_state *ra, struct file *file,
2359 pgoff_t offset, pgoff_t last_index)
2360{
2361 pgoff_t req_size;
2362
2363#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2364 req_size = last_index - offset + 1;
2365 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2366 return offset;
2367#else
2368 req_size = min(last_index - offset + 1, (pgoff_t)128);
2369 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2370 return offset + req_size;
2371#endif
2372}
2373
2374int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002375 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002376 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002377 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2378 struct page *page;
2379 unsigned long last_index;
2380 unsigned long ra_index = 0;
2381 u64 page_start;
2382 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002383 u64 delalloc_start;
2384 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002385 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002386 int ret;
2387
2388 mutex_lock(&root->fs_info->fs_mutex);
2389 ret = btrfs_check_free_space(root, inode->i_size, 0);
2390 mutex_unlock(&root->fs_info->fs_mutex);
2391 if (ret)
2392 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002393
2394 mutex_lock(&inode->i_mutex);
2395 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2396 for (i = 0; i <= last_index; i++) {
2397 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002398 ra_index = btrfs_force_ra(inode->i_mapping,
2399 &file->f_ra,
2400 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002401 }
2402 page = grab_cache_page(inode->i_mapping, i);
2403 if (!page)
2404 goto out_unlock;
2405 if (!PageUptodate(page)) {
2406 btrfs_readpage(NULL, page);
2407 lock_page(page);
2408 if (!PageUptodate(page)) {
2409 unlock_page(page);
2410 page_cache_release(page);
2411 goto out_unlock;
2412 }
2413 }
Chris Mason35ebb932007-10-30 16:56:53 -04002414 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002415 page_end = page_start + PAGE_CACHE_SIZE - 1;
2416
2417 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002418 delalloc_start = page_start;
2419 existing_delalloc =
2420 count_range_bits(&BTRFS_I(inode)->extent_tree,
2421 &delalloc_start, page_end,
2422 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002423 set_extent_delalloc(em_tree, page_start,
2424 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002425
2426 spin_lock(&root->fs_info->delalloc_lock);
2427 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2428 existing_delalloc;
2429 spin_unlock(&root->fs_info->delalloc_lock);
2430
Chris Mason86479a02007-09-10 19:58:16 -04002431 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2432 set_page_dirty(page);
2433 unlock_page(page);
2434 page_cache_release(page);
2435 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2436 }
2437
2438out_unlock:
2439 mutex_unlock(&inode->i_mutex);
2440 return 0;
2441}
2442
Chris Masonedbd8d42007-12-21 16:27:24 -05002443static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2444{
2445 u64 new_size;
2446 u64 old_size;
2447 struct btrfs_ioctl_vol_args *vol_args;
2448 struct btrfs_trans_handle *trans;
2449 char *sizestr;
2450 int ret = 0;
2451 int namelen;
2452 int mod = 0;
2453
2454 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2455
2456 if (!vol_args)
2457 return -ENOMEM;
2458
2459 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2460 ret = -EFAULT;
2461 goto out;
2462 }
2463 namelen = strlen(vol_args->name);
2464 if (namelen > BTRFS_VOL_NAME_MAX) {
2465 ret = -EINVAL;
2466 goto out;
2467 }
2468
2469 sizestr = vol_args->name;
2470 if (!strcmp(sizestr, "max"))
2471 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2472 else {
2473 if (sizestr[0] == '-') {
2474 mod = -1;
2475 sizestr++;
2476 } else if (sizestr[0] == '+') {
2477 mod = 1;
2478 sizestr++;
2479 }
2480 new_size = btrfs_parse_size(sizestr);
2481 if (new_size == 0) {
2482 ret = -EINVAL;
2483 goto out;
2484 }
2485 }
2486
2487 mutex_lock(&root->fs_info->fs_mutex);
2488 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2489
2490 if (mod < 0) {
2491 if (new_size > old_size) {
2492 ret = -EINVAL;
2493 goto out_unlock;
2494 }
2495 new_size = old_size - new_size;
2496 } else if (mod > 0) {
2497 new_size = old_size + new_size;
2498 }
2499
2500 if (new_size < 256 * 1024 * 1024) {
2501 ret = -EINVAL;
2502 goto out_unlock;
2503 }
2504 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2505 ret = -EFBIG;
2506 goto out_unlock;
2507 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002508
2509 do_div(new_size, root->sectorsize);
2510 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002511
2512printk("new size is %Lu\n", new_size);
2513 if (new_size > old_size) {
2514 trans = btrfs_start_transaction(root, 1);
2515 ret = btrfs_grow_extent_tree(trans, root, new_size);
2516 btrfs_commit_transaction(trans, root);
2517 } else {
2518 ret = btrfs_shrink_extent_tree(root, new_size);
2519 }
2520
2521out_unlock:
2522 mutex_unlock(&root->fs_info->fs_mutex);
2523out:
2524 kfree(vol_args);
2525 return ret;
2526}
2527
Chris Mason4313b392008-01-03 09:08:48 -05002528static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2529 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002530{
Chris Mason4aec2b52007-12-18 16:25:45 -05002531 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002532 struct btrfs_dir_item *di;
2533 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002534 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002535 int namelen;
2536 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002537
Chris Mason4aec2b52007-12-18 16:25:45 -05002538 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002539
Chris Mason4aec2b52007-12-18 16:25:45 -05002540 if (!vol_args)
2541 return -ENOMEM;
2542
2543 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2544 ret = -EFAULT;
2545 goto out;
2546 }
2547
2548 namelen = strlen(vol_args->name);
2549 if (namelen > BTRFS_VOL_NAME_MAX) {
2550 ret = -EINVAL;
2551 goto out;
2552 }
2553 if (strchr(vol_args->name, '/')) {
2554 ret = -EINVAL;
2555 goto out;
2556 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002557
2558 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002559 if (!path) {
2560 ret = -ENOMEM;
2561 goto out;
2562 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002563
2564 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2565 mutex_lock(&root->fs_info->fs_mutex);
2566 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2567 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002568 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002569 mutex_unlock(&root->fs_info->fs_mutex);
2570 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002571
2572 if (di && !IS_ERR(di)) {
2573 ret = -EEXIST;
2574 goto out;
2575 }
2576
2577 if (IS_ERR(di)) {
2578 ret = PTR_ERR(di);
2579 goto out;
2580 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002581
2582 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002583 ret = create_subvol(root, vol_args->name, namelen);
2584 else
2585 ret = create_snapshot(root, vol_args->name, namelen);
2586out:
2587 kfree(vol_args);
2588 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002589}
2590
2591static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002592{
Chris Mason6da6aba2007-12-18 16:15:09 -05002593 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002594 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002595
2596 switch (inode->i_mode & S_IFMT) {
2597 case S_IFDIR:
2598 mutex_lock(&root->fs_info->fs_mutex);
2599 btrfs_defrag_root(root, 0);
2600 btrfs_defrag_root(root->fs_info->extent_root, 0);
2601 mutex_unlock(&root->fs_info->fs_mutex);
2602 break;
2603 case S_IFREG:
2604 btrfs_defrag_file(file);
2605 break;
2606 }
2607
2608 return 0;
2609}
2610
2611long btrfs_ioctl(struct file *file, unsigned int
2612 cmd, unsigned long arg)
2613{
Chris Mason6da6aba2007-12-18 16:15:09 -05002614 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002615
2616 switch (cmd) {
2617 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002618 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002619 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002620 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002621 case BTRFS_IOC_RESIZE:
2622 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002623 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002624
2625 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002626}
2627
Chris Mason39279cc2007-06-12 06:35:45 -04002628/*
2629 * Called inside transaction, so use GFP_NOFS
2630 */
2631struct inode *btrfs_alloc_inode(struct super_block *sb)
2632{
2633 struct btrfs_inode *ei;
2634
2635 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2636 if (!ei)
2637 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002638 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002639 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002640 return &ei->vfs_inode;
2641}
2642
2643void btrfs_destroy_inode(struct inode *inode)
2644{
2645 WARN_ON(!list_empty(&inode->i_dentry));
2646 WARN_ON(inode->i_data.nrpages);
2647
Chris Mason8c416c92008-01-14 15:10:26 -05002648 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002649 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2650}
2651
Chris Mason44ec0b72007-10-29 10:55:05 -04002652#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2653static void init_once(struct kmem_cache * cachep, void *foo)
2654#else
Chris Mason39279cc2007-06-12 06:35:45 -04002655static void init_once(void * foo, struct kmem_cache * cachep,
2656 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002657#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002658{
2659 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2660
2661 inode_init_once(&ei->vfs_inode);
2662}
2663
2664void btrfs_destroy_cachep(void)
2665{
2666 if (btrfs_inode_cachep)
2667 kmem_cache_destroy(btrfs_inode_cachep);
2668 if (btrfs_trans_handle_cachep)
2669 kmem_cache_destroy(btrfs_trans_handle_cachep);
2670 if (btrfs_transaction_cachep)
2671 kmem_cache_destroy(btrfs_transaction_cachep);
2672 if (btrfs_bit_radix_cachep)
2673 kmem_cache_destroy(btrfs_bit_radix_cachep);
2674 if (btrfs_path_cachep)
2675 kmem_cache_destroy(btrfs_path_cachep);
2676}
2677
Chris Mason86479a02007-09-10 19:58:16 -04002678struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002679 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002680#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2681 void (*ctor)(struct kmem_cache *, void *)
2682#else
Chris Mason92fee662007-07-25 12:31:35 -04002683 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002684 unsigned long)
2685#endif
2686 )
Chris Mason92fee662007-07-25 12:31:35 -04002687{
2688 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2689 SLAB_MEM_SPREAD | extra_flags), ctor
2690#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2691 ,NULL
2692#endif
2693 );
2694}
2695
Chris Mason39279cc2007-06-12 06:35:45 -04002696int btrfs_init_cachep(void)
2697{
Chris Mason86479a02007-09-10 19:58:16 -04002698 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002699 sizeof(struct btrfs_inode),
2700 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002701 if (!btrfs_inode_cachep)
2702 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002703 btrfs_trans_handle_cachep =
2704 btrfs_cache_create("btrfs_trans_handle_cache",
2705 sizeof(struct btrfs_trans_handle),
2706 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002707 if (!btrfs_trans_handle_cachep)
2708 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002709 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002710 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002711 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002712 if (!btrfs_transaction_cachep)
2713 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002714 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002715 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002716 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002717 if (!btrfs_path_cachep)
2718 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002719 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002720 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002721 if (!btrfs_bit_radix_cachep)
2722 goto fail;
2723 return 0;
2724fail:
2725 btrfs_destroy_cachep();
2726 return -ENOMEM;
2727}
2728
2729static int btrfs_getattr(struct vfsmount *mnt,
2730 struct dentry *dentry, struct kstat *stat)
2731{
2732 struct inode *inode = dentry->d_inode;
2733 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002734 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002735 return 0;
2736}
2737
2738static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2739 struct inode * new_dir,struct dentry *new_dentry)
2740{
2741 struct btrfs_trans_handle *trans;
2742 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2743 struct inode *new_inode = new_dentry->d_inode;
2744 struct inode *old_inode = old_dentry->d_inode;
2745 struct timespec ctime = CURRENT_TIME;
2746 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002747 int ret;
2748
2749 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2750 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2751 return -ENOTEMPTY;
2752 }
Chris Mason5f39d392007-10-15 16:14:19 -04002753
Chris Mason39279cc2007-06-12 06:35:45 -04002754 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002755 ret = btrfs_check_free_space(root, 1, 0);
2756 if (ret)
2757 goto out_unlock;
2758
Chris Mason39279cc2007-06-12 06:35:45 -04002759 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002760
Chris Mason39279cc2007-06-12 06:35:45 -04002761 btrfs_set_trans_block_group(trans, new_dir);
2762 path = btrfs_alloc_path();
2763 if (!path) {
2764 ret = -ENOMEM;
2765 goto out_fail;
2766 }
2767
2768 old_dentry->d_inode->i_nlink++;
2769 old_dir->i_ctime = old_dir->i_mtime = ctime;
2770 new_dir->i_ctime = new_dir->i_mtime = ctime;
2771 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002772
Chris Mason39279cc2007-06-12 06:35:45 -04002773 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2774 if (ret)
2775 goto out_fail;
2776
2777 if (new_inode) {
2778 new_inode->i_ctime = CURRENT_TIME;
2779 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2780 if (ret)
2781 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002782 }
2783 ret = btrfs_add_link(trans, new_dentry, old_inode);
2784 if (ret)
2785 goto out_fail;
2786
2787out_fail:
2788 btrfs_free_path(path);
2789 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002790out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002791 mutex_unlock(&root->fs_info->fs_mutex);
2792 return ret;
2793}
2794
2795static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2796 const char *symname)
2797{
2798 struct btrfs_trans_handle *trans;
2799 struct btrfs_root *root = BTRFS_I(dir)->root;
2800 struct btrfs_path *path;
2801 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002802 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002803 int err;
2804 int drop_inode = 0;
2805 u64 objectid;
2806 int name_len;
2807 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002808 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002809 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002810 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002811 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002812
2813 name_len = strlen(symname) + 1;
2814 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2815 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002816
Chris Mason39279cc2007-06-12 06:35:45 -04002817 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002818 err = btrfs_check_free_space(root, 1, 0);
2819 if (err)
2820 goto out_fail;
2821
Chris Mason39279cc2007-06-12 06:35:45 -04002822 trans = btrfs_start_transaction(root, 1);
2823 btrfs_set_trans_block_group(trans, dir);
2824
2825 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2826 if (err) {
2827 err = -ENOSPC;
2828 goto out_unlock;
2829 }
2830
2831 inode = btrfs_new_inode(trans, root, objectid,
2832 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2833 err = PTR_ERR(inode);
2834 if (IS_ERR(inode))
2835 goto out_unlock;
2836
2837 btrfs_set_trans_block_group(trans, inode);
2838 err = btrfs_add_nondir(trans, dentry, inode);
2839 if (err)
2840 drop_inode = 1;
2841 else {
2842 inode->i_mapping->a_ops = &btrfs_aops;
2843 inode->i_fop = &btrfs_file_operations;
2844 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002845 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2846 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002847 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002848 }
2849 dir->i_sb->s_dirt = 1;
2850 btrfs_update_inode_block_group(trans, inode);
2851 btrfs_update_inode_block_group(trans, dir);
2852 if (drop_inode)
2853 goto out_unlock;
2854
2855 path = btrfs_alloc_path();
2856 BUG_ON(!path);
2857 key.objectid = inode->i_ino;
2858 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002859 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2860 datasize = btrfs_file_extent_calc_inline_size(name_len);
2861 err = btrfs_insert_empty_item(trans, root, path, &key,
2862 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002863 if (err) {
2864 drop_inode = 1;
2865 goto out_unlock;
2866 }
Chris Mason5f39d392007-10-15 16:14:19 -04002867 leaf = path->nodes[0];
2868 ei = btrfs_item_ptr(leaf, path->slots[0],
2869 struct btrfs_file_extent_item);
2870 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2871 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002872 BTRFS_FILE_EXTENT_INLINE);
2873 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002874 write_extent_buffer(leaf, symname, ptr, name_len);
2875 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002876 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002877
Chris Mason39279cc2007-06-12 06:35:45 -04002878 inode->i_op = &btrfs_symlink_inode_operations;
2879 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2880 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002881 err = btrfs_update_inode(trans, root, inode);
2882 if (err)
2883 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002884
2885out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002886 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002887 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002888out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002889 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002890 if (drop_inode) {
2891 inode_dec_link_count(inode);
2892 iput(inode);
2893 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002894 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002895 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002896 return err;
2897}
Yanfdebe2b2008-01-14 13:26:08 -05002898static int btrfs_permission(struct inode *inode, int mask,
2899 struct nameidata *nd)
2900{
2901 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2902 return -EACCES;
2903 return generic_permission(inode, mask, NULL);
2904}
Chris Mason39279cc2007-06-12 06:35:45 -04002905
2906static struct inode_operations btrfs_dir_inode_operations = {
2907 .lookup = btrfs_lookup,
2908 .create = btrfs_create,
2909 .unlink = btrfs_unlink,
2910 .link = btrfs_link,
2911 .mkdir = btrfs_mkdir,
2912 .rmdir = btrfs_rmdir,
2913 .rename = btrfs_rename,
2914 .symlink = btrfs_symlink,
2915 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002916 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002917 .setxattr = generic_setxattr,
2918 .getxattr = generic_getxattr,
2919 .listxattr = btrfs_listxattr,
2920 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002921 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002922};
Chris Mason39279cc2007-06-12 06:35:45 -04002923static struct inode_operations btrfs_dir_ro_inode_operations = {
2924 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002925 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002926};
Chris Mason39279cc2007-06-12 06:35:45 -04002927static struct file_operations btrfs_dir_file_operations = {
2928 .llseek = generic_file_llseek,
2929 .read = generic_read_dir,
2930 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002931 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002932#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002933 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002934#endif
2935};
2936
Chris Mason07157aa2007-08-30 08:50:51 -04002937static struct extent_map_ops btrfs_extent_map_ops = {
2938 .fill_delalloc = run_delalloc_range,
2939 .writepage_io_hook = btrfs_writepage_io_hook,
2940 .readpage_io_hook = btrfs_readpage_io_hook,
2941 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2942};
2943
Chris Mason39279cc2007-06-12 06:35:45 -04002944static struct address_space_operations btrfs_aops = {
2945 .readpage = btrfs_readpage,
2946 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002947 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002948 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002949 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002950 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002951 .invalidatepage = btrfs_invalidatepage,
2952 .releasepage = btrfs_releasepage,
2953 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002954};
2955
2956static struct address_space_operations btrfs_symlink_aops = {
2957 .readpage = btrfs_readpage,
2958 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002959 .invalidatepage = btrfs_invalidatepage,
2960 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002961};
2962
2963static struct inode_operations btrfs_file_inode_operations = {
2964 .truncate = btrfs_truncate,
2965 .getattr = btrfs_getattr,
2966 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002967 .setxattr = generic_setxattr,
2968 .getxattr = generic_getxattr,
2969 .listxattr = btrfs_listxattr,
2970 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002971 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002972};
Josef Bacik618e21d2007-07-11 10:18:17 -04002973static struct inode_operations btrfs_special_inode_operations = {
2974 .getattr = btrfs_getattr,
2975 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002976 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002977};
Chris Mason39279cc2007-06-12 06:35:45 -04002978static struct inode_operations btrfs_symlink_inode_operations = {
2979 .readlink = generic_readlink,
2980 .follow_link = page_follow_link_light,
2981 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05002982 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002983};