blob: 51fc06dc4a5df65ed4ef7f7f7e838f5862cc89a9 [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 Masonb888db2b2007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db2b2007-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 Masonb888db2b2007-08-27 16:49:44 -0400109
Chris Masonb888db2b2007-08-27 16:49:44 -0400110 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db2b2007-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 Masonb888db2b2007-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);
136 num_bytes -= cur_alloc_size;
137 alloc_hint = ins.objectid + ins.offset;
138 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400139 }
Chris Masondc17ff82008-01-08 15:46:30 -0500140 btrfs_add_ordered_inode(inode);
Chris Masonb888db2b2007-08-27 16:49:44 -0400141out:
142 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500143 return ret;
144}
145
146static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
147{
148 u64 extent_start;
149 u64 extent_end;
150 u64 bytenr;
151 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500152 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500153 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500154 struct btrfs_root *root = BTRFS_I(inode)->root;
155 struct extent_buffer *leaf;
156 int found_type;
157 struct btrfs_path *path;
158 struct btrfs_file_extent_item *item;
159 int ret;
160 int err;
161 struct btrfs_key found_key;
162
Chris Masonc31f8832008-01-08 15:46:31 -0500163 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500164 path = btrfs_alloc_path();
165 BUG_ON(!path);
166again:
167 ret = btrfs_lookup_file_extent(NULL, root, path,
168 inode->i_ino, start, 0);
169 if (ret < 0) {
170 btrfs_free_path(path);
171 return ret;
172 }
173
174 cow_end = end;
175 if (ret != 0) {
176 if (path->slots[0] == 0)
177 goto not_found;
178 path->slots[0]--;
179 }
180
181 leaf = path->nodes[0];
182 item = btrfs_item_ptr(leaf, path->slots[0],
183 struct btrfs_file_extent_item);
184
185 /* are we inside the extent that was found? */
186 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
187 found_type = btrfs_key_type(&found_key);
188 if (found_key.objectid != inode->i_ino ||
189 found_type != BTRFS_EXTENT_DATA_KEY) {
190 goto not_found;
191 }
192
193 found_type = btrfs_file_extent_type(leaf, item);
194 extent_start = found_key.offset;
195 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500196 u64 extent_num_bytes;
197
198 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
199 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500200 err = 0;
201
Chris Mason1832a6d2007-12-21 16:27:21 -0500202 if (loops && start != extent_start)
203 goto not_found;
204
Chris Masonbe20aa92007-12-17 20:14:01 -0500205 if (start < extent_start || start >= extent_end)
206 goto not_found;
207
208 cow_end = min(end, extent_end - 1);
209 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
210 if (bytenr == 0)
211 goto not_found;
212
Chris Masonc31f8832008-01-08 15:46:31 -0500213 /*
214 * we may be called by the resizer, make sure we're inside
215 * the limits of the FS
216 */
217 if (bytenr + extent_num_bytes > total_fs_bytes)
218 goto not_found;
219
Chris Masonbe20aa92007-12-17 20:14:01 -0500220 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
221 goto not_found;
222 }
223
224 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500225 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500226 goto not_found;
227 }
228loop:
229 if (start > end) {
230 btrfs_free_path(path);
231 return 0;
232 }
233 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500234 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500235 goto again;
236
237not_found:
238 cow_file_range(inode, start, cow_end);
239 start = cow_end + 1;
240 goto loop;
241}
242
243static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
244{
245 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500246 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500247 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500248 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500249 if (btrfs_test_opt(root, NODATACOW) ||
250 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500251 ret = run_delalloc_nocow(inode, start, end);
252 else
253 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500254
255 spin_lock(&root->fs_info->delalloc_lock);
256 num_bytes = end + 1 - start;
257 if (root->fs_info->delalloc_bytes < num_bytes) {
258 printk("delalloc accounting error total %llu sub %llu\n",
259 root->fs_info->delalloc_bytes, num_bytes);
260 } else {
261 root->fs_info->delalloc_bytes -= num_bytes;
262 }
263 spin_unlock(&root->fs_info->delalloc_lock);
264
Chris Masonb888db2b2007-08-27 16:49:44 -0400265 mutex_unlock(&root->fs_info->fs_mutex);
266 return ret;
267}
268
Chris Mason07157aa2007-08-30 08:50:51 -0400269int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
270{
271 struct inode *inode = page->mapping->host;
272 struct btrfs_root *root = BTRFS_I(inode)->root;
273 struct btrfs_trans_handle *trans;
274 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500275 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400276 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400277 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500278 if (btrfs_test_opt(root, NODATASUM) ||
279 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500280 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400281 mutex_lock(&root->fs_info->fs_mutex);
282 trans = btrfs_start_transaction(root, 1);
283 btrfs_set_trans_block_group(trans, inode);
284 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400285 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400286 start, kaddr + offset, end - start + 1);
287 kunmap(page);
288 ret = btrfs_end_transaction(trans, root);
289 BUG_ON(ret);
290 mutex_unlock(&root->fs_info->fs_mutex);
291 return ret;
292}
293
294int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
295{
296 int ret = 0;
297 struct inode *inode = page->mapping->host;
298 struct btrfs_root *root = BTRFS_I(inode)->root;
299 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
300 struct btrfs_csum_item *item;
301 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400302 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500303 if (btrfs_test_opt(root, NODATASUM) ||
304 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500305 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400306 mutex_lock(&root->fs_info->fs_mutex);
307 path = btrfs_alloc_path();
308 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
309 if (IS_ERR(item)) {
310 ret = PTR_ERR(item);
311 /* a csum that isn't present is a preallocated region. */
312 if (ret == -ENOENT || ret == -EFBIG)
313 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400314 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400315 goto out;
316 }
Chris Masonff79f812007-10-15 16:22:25 -0400317 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
318 BTRFS_CRC32_SIZE);
319 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400320out:
321 if (path)
322 btrfs_free_path(path);
323 mutex_unlock(&root->fs_info->fs_mutex);
324 return ret;
325}
326
327int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
328{
Chris Mason35ebb932007-10-30 16:56:53 -0400329 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400330 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400331 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
332 char *kaddr;
333 u64 private;
334 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400335 struct btrfs_root *root = BTRFS_I(inode)->root;
336 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400337 unsigned long flags;
Yanb98b6762008-01-08 15:54:37 -0500338 if (btrfs_test_opt(root, NODATASUM) ||
339 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500340 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400341 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400342 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400343 kaddr = kmap_atomic(page, KM_IRQ0);
344 if (ret) {
345 goto zeroit;
346 }
Chris Masonff79f812007-10-15 16:22:25 -0400347 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
348 btrfs_csum_final(csum, (char *)&csum);
349 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400350 goto zeroit;
351 }
352 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400353 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400354 return 0;
355
356zeroit:
357 printk("btrfs csum failed ino %lu off %llu\n",
358 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400359 memset(kaddr + offset, 1, end - start + 1);
360 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400361 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400362 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400363 return 0;
364}
Chris Masonb888db2b2007-08-27 16:49:44 -0400365
Chris Mason39279cc2007-06-12 06:35:45 -0400366void btrfs_read_locked_inode(struct inode *inode)
367{
368 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400369 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400370 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400371 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400372 struct btrfs_root *root = BTRFS_I(inode)->root;
373 struct btrfs_key location;
374 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400375 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400376 int ret;
377
378 path = btrfs_alloc_path();
379 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400380 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400381 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500382
Chris Mason39279cc2007-06-12 06:35:45 -0400383 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400384 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400385 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400386
Chris Mason5f39d392007-10-15 16:14:19 -0400387 leaf = path->nodes[0];
388 inode_item = btrfs_item_ptr(leaf, path->slots[0],
389 struct btrfs_inode_item);
390
391 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
392 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
393 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
394 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
395 inode->i_size = btrfs_inode_size(leaf, inode_item);
396
397 tspec = btrfs_inode_atime(inode_item);
398 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
399 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
400
401 tspec = btrfs_inode_mtime(inode_item);
402 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
403 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
404
405 tspec = btrfs_inode_ctime(inode_item);
406 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
407 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
408
409 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
410 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400411 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400412 rdev = btrfs_inode_rdev(leaf, inode_item);
413
414 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400415 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
416 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500417 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500418 if (!BTRFS_I(inode)->block_group) {
419 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
420 NULL, 0, 0, 0);
421 }
Chris Mason39279cc2007-06-12 06:35:45 -0400422 btrfs_free_path(path);
423 inode_item = NULL;
424
425 mutex_unlock(&root->fs_info->fs_mutex);
426
427 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400428 case S_IFREG:
429 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400430 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400431 inode->i_fop = &btrfs_file_operations;
432 inode->i_op = &btrfs_file_inode_operations;
433 break;
434 case S_IFDIR:
435 inode->i_fop = &btrfs_dir_file_operations;
436 if (root == root->fs_info->tree_root)
437 inode->i_op = &btrfs_dir_ro_inode_operations;
438 else
439 inode->i_op = &btrfs_dir_inode_operations;
440 break;
441 case S_IFLNK:
442 inode->i_op = &btrfs_symlink_inode_operations;
443 inode->i_mapping->a_ops = &btrfs_symlink_aops;
444 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400445 default:
446 init_special_inode(inode, inode->i_mode, rdev);
447 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400448 }
449 return;
450
451make_bad:
452 btrfs_release_path(root, path);
453 btrfs_free_path(path);
454 mutex_unlock(&root->fs_info->fs_mutex);
455 make_bad_inode(inode);
456}
457
Chris Mason5f39d392007-10-15 16:14:19 -0400458static void fill_inode_item(struct extent_buffer *leaf,
459 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400460 struct inode *inode)
461{
Chris Mason5f39d392007-10-15 16:14:19 -0400462 btrfs_set_inode_uid(leaf, item, inode->i_uid);
463 btrfs_set_inode_gid(leaf, item, inode->i_gid);
464 btrfs_set_inode_size(leaf, item, inode->i_size);
465 btrfs_set_inode_mode(leaf, item, inode->i_mode);
466 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
467
468 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
469 inode->i_atime.tv_sec);
470 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
471 inode->i_atime.tv_nsec);
472
473 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
474 inode->i_mtime.tv_sec);
475 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
476 inode->i_mtime.tv_nsec);
477
478 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
479 inode->i_ctime.tv_sec);
480 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
481 inode->i_ctime.tv_nsec);
482
483 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
484 btrfs_set_inode_generation(leaf, item, inode->i_generation);
485 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500486 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400487 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400488 BTRFS_I(inode)->block_group->key.objectid);
489}
490
Chris Masona52d9a82007-08-27 16:49:44 -0400491int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400492 struct btrfs_root *root,
493 struct inode *inode)
494{
495 struct btrfs_inode_item *inode_item;
496 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400497 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400498 int ret;
499
500 path = btrfs_alloc_path();
501 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400502 ret = btrfs_lookup_inode(trans, root, path,
503 &BTRFS_I(inode)->location, 1);
504 if (ret) {
505 if (ret > 0)
506 ret = -ENOENT;
507 goto failed;
508 }
509
Chris Mason5f39d392007-10-15 16:14:19 -0400510 leaf = path->nodes[0];
511 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400512 struct btrfs_inode_item);
513
Chris Mason5f39d392007-10-15 16:14:19 -0400514 fill_inode_item(leaf, inode_item, inode);
515 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400516 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400517 ret = 0;
518failed:
519 btrfs_release_path(root, path);
520 btrfs_free_path(path);
521 return ret;
522}
523
524
525static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
526 struct btrfs_root *root,
527 struct inode *dir,
528 struct dentry *dentry)
529{
530 struct btrfs_path *path;
531 const char *name = dentry->d_name.name;
532 int name_len = dentry->d_name.len;
533 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400534 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400535 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400536 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400537
538 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400539 if (!path) {
540 ret = -ENOMEM;
541 goto err;
542 }
543
Chris Mason39279cc2007-06-12 06:35:45 -0400544 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
545 name, name_len, -1);
546 if (IS_ERR(di)) {
547 ret = PTR_ERR(di);
548 goto err;
549 }
550 if (!di) {
551 ret = -ENOENT;
552 goto err;
553 }
Chris Mason5f39d392007-10-15 16:14:19 -0400554 leaf = path->nodes[0];
555 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400556 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400557 if (ret)
558 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400559 btrfs_release_path(root, path);
560
561 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400562 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400563 if (IS_ERR(di)) {
564 ret = PTR_ERR(di);
565 goto err;
566 }
567 if (!di) {
568 ret = -ENOENT;
569 goto err;
570 }
571 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400572
573 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500574 ret = btrfs_del_inode_ref(trans, root, name, name_len,
575 dentry->d_inode->i_ino,
576 dentry->d_parent->d_inode->i_ino);
577 if (ret) {
578 printk("failed to delete reference to %.*s, "
579 "inode %lu parent %lu\n", name_len, name,
580 dentry->d_inode->i_ino,
581 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500582 }
Chris Mason39279cc2007-06-12 06:35:45 -0400583err:
584 btrfs_free_path(path);
585 if (!ret) {
586 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400587 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400588 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500589#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
590 dentry->d_inode->i_nlink--;
591#else
Chris Mason39279cc2007-06-12 06:35:45 -0400592 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500593#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400594 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400595 dir->i_sb->s_dirt = 1;
596 }
597 return ret;
598}
599
600static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
601{
602 struct btrfs_root *root;
603 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500604 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400605 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500606 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400607
608 root = BTRFS_I(dir)->root;
609 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500610
611 ret = btrfs_check_free_space(root, 1, 1);
612 if (ret)
613 goto fail;
614
Chris Mason39279cc2007-06-12 06:35:45 -0400615 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400616
Chris Mason39279cc2007-06-12 06:35:45 -0400617 btrfs_set_trans_block_group(trans, dir);
618 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400619 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400620
Chris Mason2da98f02008-01-16 11:44:43 -0500621 if (inode->i_nlink == 0) {
622 int found;
623 /* if the inode isn't linked anywhere,
624 * we don't need to worry about
625 * data=ordered
626 */
627 found = btrfs_del_ordered_inode(inode);
628 if (found == 1) {
629 atomic_dec(&inode->i_count);
630 }
631 }
632
Chris Mason39279cc2007-06-12 06:35:45 -0400633 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500634fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400635 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400636 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500637 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400638 return ret;
639}
640
641static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
642{
643 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500644 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400645 int ret;
646 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400647 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500648 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400649
Yan134d4512007-10-25 15:49:25 -0400650 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
651 return -ENOTEMPTY;
652
Chris Mason39279cc2007-06-12 06:35:45 -0400653 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500654 ret = btrfs_check_free_space(root, 1, 1);
655 if (ret)
656 goto fail;
657
Chris Mason39279cc2007-06-12 06:35:45 -0400658 trans = btrfs_start_transaction(root, 1);
659 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400660
661 /* now the directory is empty */
662 err = btrfs_unlink_trans(trans, root, dir, dentry);
663 if (!err) {
664 inode->i_size = 0;
665 }
Chris Mason39544012007-12-12 14:38:19 -0500666
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400667 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400668 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500669fail:
Yan134d4512007-10-25 15:49:25 -0400670 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400671 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500672 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500673
Chris Mason39279cc2007-06-12 06:35:45 -0400674 if (ret && !err)
675 err = ret;
676 return err;
677}
678
679static int btrfs_free_inode(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root,
681 struct inode *inode)
682{
683 struct btrfs_path *path;
684 int ret;
685
686 clear_inode(inode);
687
688 path = btrfs_alloc_path();
689 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400690 ret = btrfs_lookup_inode(trans, root, path,
691 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400692 if (ret > 0)
693 ret = -ENOENT;
694 if (!ret)
695 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400696 btrfs_free_path(path);
697 return ret;
698}
699
700/*
Chris Mason39279cc2007-06-12 06:35:45 -0400701 * this can truncate away extent items, csum items and directory items.
702 * It starts at a high offset and removes keys until it can't find
703 * any higher than i_size.
704 *
705 * csum items that cross the new i_size are truncated to the new size
706 * as well.
707 */
708static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
709 struct btrfs_root *root,
710 struct inode *inode)
711{
712 int ret;
713 struct btrfs_path *path;
714 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400715 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400716 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400717 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400718 struct btrfs_file_extent_item *fi;
719 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400720 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400721 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500722 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500723 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400724 int found_extent;
725 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400726 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400727
Chris Masona52d9a82007-08-27 16:49:44 -0400728 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400729 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400730 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400731 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400732
Chris Mason39279cc2007-06-12 06:35:45 -0400733 /* FIXME, add redo link to tree so we don't leak on crash */
734 key.objectid = inode->i_ino;
735 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400736 key.type = (u8)-1;
737
Chris Mason39279cc2007-06-12 06:35:45 -0400738 while(1) {
739 btrfs_init_path(path);
740 fi = NULL;
741 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
742 if (ret < 0) {
743 goto error;
744 }
745 if (ret > 0) {
746 BUG_ON(path->slots[0] == 0);
747 path->slots[0]--;
748 }
Chris Mason5f39d392007-10-15 16:14:19 -0400749 leaf = path->nodes[0];
750 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
751 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400752
Chris Mason5f39d392007-10-15 16:14:19 -0400753 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400754 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400755
Chris Mason39279cc2007-06-12 06:35:45 -0400756 if (found_type != BTRFS_CSUM_ITEM_KEY &&
757 found_type != BTRFS_DIR_ITEM_KEY &&
758 found_type != BTRFS_DIR_INDEX_KEY &&
759 found_type != BTRFS_EXTENT_DATA_KEY)
760 break;
761
Chris Mason5f39d392007-10-15 16:14:19 -0400762 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400763 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400764 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400765 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400766 extent_type = btrfs_file_extent_type(leaf, fi);
767 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400768 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400769 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400770 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
771 struct btrfs_item *item = btrfs_item_nr(leaf,
772 path->slots[0]);
773 item_end += btrfs_file_extent_inline_len(leaf,
774 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400775 }
Yan008630c2007-11-07 13:31:09 -0500776 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 }
778 if (found_type == BTRFS_CSUM_ITEM_KEY) {
779 ret = btrfs_csum_truncate(trans, root, path,
780 inode->i_size);
781 BUG_ON(ret);
782 }
Yan008630c2007-11-07 13:31:09 -0500783 if (item_end < inode->i_size) {
Chris Masonb888db2b2007-08-27 16:49:44 -0400784 if (found_type == BTRFS_DIR_ITEM_KEY) {
785 found_type = BTRFS_INODE_ITEM_KEY;
786 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
787 found_type = BTRFS_CSUM_ITEM_KEY;
788 } else if (found_type) {
789 found_type--;
790 } else {
791 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400792 }
Yana61721d2007-09-17 11:08:38 -0400793 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400794 btrfs_release_path(root, path);
Chris Masonb888db2b2007-08-27 16:49:44 -0400795 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400796 }
Chris Mason5f39d392007-10-15 16:14:19 -0400797 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400798 del_item = 1;
799 else
800 del_item = 0;
801 found_extent = 0;
802
803 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400804 if (found_type != BTRFS_EXTENT_DATA_KEY)
805 goto delete;
806
807 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400808 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400809 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400810 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400811 u64 orig_num_bytes =
812 btrfs_file_extent_num_bytes(leaf, fi);
813 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400814 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400815 btrfs_set_file_extent_num_bytes(leaf, fi,
816 extent_num_bytes);
817 num_dec = (orig_num_bytes -
818 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400819 if (extent_start != 0) {
820 inode->i_blocks -= num_dec;
821 }
Chris Mason5f39d392007-10-15 16:14:19 -0400822 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400823 } else {
Chris Masondb945352007-10-15 16:15:53 -0400824 extent_num_bytes =
825 btrfs_file_extent_disk_num_bytes(leaf,
826 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400827 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400828 num_dec = btrfs_file_extent_num_bytes(leaf,
829 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400830 if (extent_start != 0) {
831 found_extent = 1;
832 inode->i_blocks -= num_dec;
833 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500834 root_gen = btrfs_header_generation(leaf);
835 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400836 }
Chris Mason179e29e2007-11-01 11:28:41 -0400837 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
838 !del_item) {
839 u32 newsize = inode->i_size - found_key.offset;
840 newsize = btrfs_file_extent_calc_inline_size(newsize);
841 ret = btrfs_truncate_item(trans, root, path,
842 newsize, 1);
843 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400844 }
Chris Mason179e29e2007-11-01 11:28:41 -0400845delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400846 if (del_item) {
847 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400848 if (ret)
849 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400850 } else {
851 break;
852 }
853 btrfs_release_path(root, path);
854 if (found_extent) {
855 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500856 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500857 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500858 root_gen, inode->i_ino,
859 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400860 BUG_ON(ret);
861 }
862 }
863 ret = 0;
864error:
865 btrfs_release_path(root, path);
866 btrfs_free_path(path);
867 inode->i_sb->s_dirt = 1;
868 return ret;
869}
870
Chris Masonb888db2b2007-08-27 16:49:44 -0400871static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400872 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400873{
Chris Mason39279cc2007-06-12 06:35:45 -0400874 char *kaddr;
Chris Masonb888db2b2007-08-27 16:49:44 -0400875 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500876 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400877 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db2b2007-08-27 16:49:44 -0400878 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500879 u64 existing_delalloc;
880 u64 delalloc_start;
881 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400882
Chris Mason190662b2007-12-18 16:25:45 -0500883 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400884 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400885
Chris Masonb888db2b2007-08-27 16:49:44 -0400886 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500887 delalloc_start = page_start;
888 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
889 &delalloc_start, page_end,
890 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db2b2007-08-27 16:49:44 -0400891 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
892 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500893
894 spin_lock(&root->fs_info->delalloc_lock);
895 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
896 spin_unlock(&root->fs_info->delalloc_lock);
897
Chris Masona52d9a82007-08-27 16:49:44 -0400898 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db2b2007-08-27 16:49:44 -0400899 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400900 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
901 flush_dcache_page(page);
Chris Masonb888db2b2007-08-27 16:49:44 -0400902 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400903 }
Chris Masonb888db2b2007-08-27 16:49:44 -0400904 set_page_dirty(page);
905 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400906
Chris Masona52d9a82007-08-27 16:49:44 -0400907 return ret;
908}
909
910/*
911 * taken from block_truncate_page, but does cow as it zeros out
912 * any bytes left in the last page in the file.
913 */
914static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
915{
916 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400917 struct btrfs_root *root = BTRFS_I(inode)->root;
918 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400919 pgoff_t index = from >> PAGE_CACHE_SHIFT;
920 unsigned offset = from & (PAGE_CACHE_SIZE-1);
921 struct page *page;
922 int ret = 0;
923 u64 page_start;
924
925 if ((offset & (blocksize - 1)) == 0)
926 goto out;
927
928 ret = -ENOMEM;
929 page = grab_cache_page(mapping, index);
930 if (!page)
931 goto out;
932 if (!PageUptodate(page)) {
933 ret = btrfs_readpage(NULL, page);
934 lock_page(page);
935 if (!PageUptodate(page)) {
936 ret = -EIO;
937 goto out;
938 }
939 }
Chris Mason35ebb932007-10-30 16:56:53 -0400940 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400941
Chris Masonb888db2b2007-08-27 16:49:44 -0400942 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400943
Chris Mason39279cc2007-06-12 06:35:45 -0400944 unlock_page(page);
945 page_cache_release(page);
946out:
947 return ret;
948}
949
950static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
951{
952 struct inode *inode = dentry->d_inode;
953 int err;
954
955 err = inode_change_ok(inode, attr);
956 if (err)
957 return err;
958
959 if (S_ISREG(inode->i_mode) &&
960 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
961 struct btrfs_trans_handle *trans;
962 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400963 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
964
Chris Mason5f39d392007-10-15 16:14:19 -0400965 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400966 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400967 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400968 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400969 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400970
971 if (attr->ia_size <= pos)
972 goto out;
973
Chris Mason1832a6d2007-12-21 16:27:21 -0500974 mutex_lock(&root->fs_info->fs_mutex);
975 err = btrfs_check_free_space(root, 1, 0);
976 mutex_unlock(&root->fs_info->fs_mutex);
977 if (err)
978 goto fail;
979
Chris Mason39279cc2007-06-12 06:35:45 -0400980 btrfs_truncate_page(inode->i_mapping, inode->i_size);
981
Chris Mason2bf5a722007-08-30 11:54:02 -0400982 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400983 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400984
985 mutex_lock(&root->fs_info->fs_mutex);
986 trans = btrfs_start_transaction(root, 1);
987 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400988 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400989 pos, pos + hole_size, pos,
990 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400991
Chris Mason179e29e2007-11-01 11:28:41 -0400992 if (alloc_hint != EXTENT_MAP_INLINE) {
993 err = btrfs_insert_file_extent(trans, root,
994 inode->i_ino,
995 pos, 0, 0, hole_size);
996 }
Chris Mason39279cc2007-06-12 06:35:45 -0400997 btrfs_end_transaction(trans, root);
998 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400999 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001000 if (err)
1001 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001002 }
1003out:
1004 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001005fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001006 return err;
1007}
Chris Mason61295eb2008-01-14 16:24:38 -05001008
Chris Mason2da98f02008-01-16 11:44:43 -05001009void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001010{
Chris Mason2da98f02008-01-16 11:44:43 -05001011 int ret;
1012
1013 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001014 return;
1015 }
Chris Mason2da98f02008-01-16 11:44:43 -05001016
1017 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1018 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1019 return;
1020
1021 ret = btrfs_del_ordered_inode(inode);
1022 if (ret == 1) {
1023 atomic_dec(&inode->i_count);
1024 }
Chris Mason61295eb2008-01-14 16:24:38 -05001025}
1026
Chris Mason39279cc2007-06-12 06:35:45 -04001027void btrfs_delete_inode(struct inode *inode)
1028{
1029 struct btrfs_trans_handle *trans;
1030 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001031 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001032 int ret;
1033
1034 truncate_inode_pages(&inode->i_data, 0);
1035 if (is_bad_inode(inode)) {
1036 goto no_delete;
1037 }
Chris Mason5f39d392007-10-15 16:14:19 -04001038
Chris Mason39279cc2007-06-12 06:35:45 -04001039 inode->i_size = 0;
1040 mutex_lock(&root->fs_info->fs_mutex);
1041 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001042
Chris Mason39279cc2007-06-12 06:35:45 -04001043 btrfs_set_trans_block_group(trans, inode);
1044 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001045 if (ret)
1046 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001047 ret = btrfs_delete_xattrs(trans, root, inode);
1048 if (ret)
1049 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001050 ret = btrfs_free_inode(trans, root, inode);
1051 if (ret)
1052 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001053 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001054
Chris Mason39279cc2007-06-12 06:35:45 -04001055 btrfs_end_transaction(trans, root);
1056 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001057 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001058 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001059 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001060
1061no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001062 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001063 btrfs_end_transaction(trans, root);
1064 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001065 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001066 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001067no_delete:
1068 clear_inode(inode);
1069}
1070
1071/*
1072 * this returns the key found in the dir entry in the location pointer.
1073 * If no dir entries were found, location->objectid is 0.
1074 */
1075static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1076 struct btrfs_key *location)
1077{
1078 const char *name = dentry->d_name.name;
1079 int namelen = dentry->d_name.len;
1080 struct btrfs_dir_item *di;
1081 struct btrfs_path *path;
1082 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001083 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001084
Chris Mason39544012007-12-12 14:38:19 -05001085 if (namelen == 1 && strcmp(name, ".") == 0) {
1086 location->objectid = dir->i_ino;
1087 location->type = BTRFS_INODE_ITEM_KEY;
1088 location->offset = 0;
1089 return 0;
1090 }
Chris Mason39279cc2007-06-12 06:35:45 -04001091 path = btrfs_alloc_path();
1092 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001093
Chris Mason7a720532007-12-13 09:06:59 -05001094 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001095 struct btrfs_key key;
1096 struct extent_buffer *leaf;
1097 u32 nritems;
1098 int slot;
1099
1100 key.objectid = dir->i_ino;
1101 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1102 key.offset = 0;
1103 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1104 BUG_ON(ret == 0);
1105 ret = 0;
1106
1107 leaf = path->nodes[0];
1108 slot = path->slots[0];
1109 nritems = btrfs_header_nritems(leaf);
1110 if (slot >= nritems)
1111 goto out_err;
1112
1113 btrfs_item_key_to_cpu(leaf, &key, slot);
1114 if (key.objectid != dir->i_ino ||
1115 key.type != BTRFS_INODE_REF_KEY) {
1116 goto out_err;
1117 }
1118 location->objectid = key.offset;
1119 location->type = BTRFS_INODE_ITEM_KEY;
1120 location->offset = 0;
1121 goto out;
1122 }
1123
Chris Mason39279cc2007-06-12 06:35:45 -04001124 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1125 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001126 if (IS_ERR(di))
1127 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001128 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001129 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001130 }
Chris Mason5f39d392007-10-15 16:14:19 -04001131 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001132out:
Chris Mason39279cc2007-06-12 06:35:45 -04001133 btrfs_free_path(path);
1134 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001135out_err:
1136 location->objectid = 0;
1137 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001138}
1139
1140/*
1141 * when we hit a tree root in a directory, the btrfs part of the inode
1142 * needs to be changed to reflect the root directory of the tree root. This
1143 * is kind of like crossing a mount point.
1144 */
1145static int fixup_tree_root_location(struct btrfs_root *root,
1146 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001147 struct btrfs_root **sub_root,
1148 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001149{
1150 struct btrfs_path *path;
1151 struct btrfs_root_item *ri;
1152
1153 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1154 return 0;
1155 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1156 return 0;
1157
1158 path = btrfs_alloc_path();
1159 BUG_ON(!path);
1160 mutex_lock(&root->fs_info->fs_mutex);
1161
Josef Bacik58176a92007-08-29 15:47:34 -04001162 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1163 dentry->d_name.name,
1164 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001165 if (IS_ERR(*sub_root))
1166 return PTR_ERR(*sub_root);
1167
1168 ri = &(*sub_root)->root_item;
1169 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001170 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1171 location->offset = 0;
1172
1173 btrfs_free_path(path);
1174 mutex_unlock(&root->fs_info->fs_mutex);
1175 return 0;
1176}
1177
1178static int btrfs_init_locked_inode(struct inode *inode, void *p)
1179{
1180 struct btrfs_iget_args *args = p;
1181 inode->i_ino = args->ino;
1182 BTRFS_I(inode)->root = args->root;
Chris Masonb888db2b2007-08-27 16:49:44 -04001183 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1184 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001185 return 0;
1186}
1187
1188static int btrfs_find_actor(struct inode *inode, void *opaque)
1189{
1190 struct btrfs_iget_args *args = opaque;
1191 return (args->ino == inode->i_ino &&
1192 args->root == BTRFS_I(inode)->root);
1193}
1194
Chris Masondc17ff82008-01-08 15:46:30 -05001195struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1196 u64 root_objectid)
1197{
1198 struct btrfs_iget_args args;
1199 args.ino = objectid;
1200 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1201
1202 if (!args.root)
1203 return NULL;
1204
1205 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1206}
1207
Chris Mason39279cc2007-06-12 06:35:45 -04001208struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1209 struct btrfs_root *root)
1210{
1211 struct inode *inode;
1212 struct btrfs_iget_args args;
1213 args.ino = objectid;
1214 args.root = root;
1215
1216 inode = iget5_locked(s, objectid, btrfs_find_actor,
1217 btrfs_init_locked_inode,
1218 (void *)&args);
1219 return inode;
1220}
1221
1222static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1223 struct nameidata *nd)
1224{
1225 struct inode * inode;
1226 struct btrfs_inode *bi = BTRFS_I(dir);
1227 struct btrfs_root *root = bi->root;
1228 struct btrfs_root *sub_root = root;
1229 struct btrfs_key location;
1230 int ret;
1231
1232 if (dentry->d_name.len > BTRFS_NAME_LEN)
1233 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001234
Chris Mason39279cc2007-06-12 06:35:45 -04001235 mutex_lock(&root->fs_info->fs_mutex);
1236 ret = btrfs_inode_by_name(dir, dentry, &location);
1237 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001238
Chris Mason39279cc2007-06-12 06:35:45 -04001239 if (ret < 0)
1240 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001241
Chris Mason39279cc2007-06-12 06:35:45 -04001242 inode = NULL;
1243 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001244 ret = fixup_tree_root_location(root, &location, &sub_root,
1245 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001246 if (ret < 0)
1247 return ERR_PTR(ret);
1248 if (ret > 0)
1249 return ERR_PTR(-ENOENT);
1250 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1251 sub_root);
1252 if (!inode)
1253 return ERR_PTR(-EACCES);
1254 if (inode->i_state & I_NEW) {
1255 /* the inode and parent dir are two different roots */
1256 if (sub_root != root) {
1257 igrab(inode);
1258 sub_root->inode = inode;
1259 }
1260 BTRFS_I(inode)->root = sub_root;
1261 memcpy(&BTRFS_I(inode)->location, &location,
1262 sizeof(location));
1263 btrfs_read_locked_inode(inode);
1264 unlock_new_inode(inode);
1265 }
1266 }
1267 return d_splice_alias(inode, dentry);
1268}
1269
Chris Mason39279cc2007-06-12 06:35:45 -04001270static unsigned char btrfs_filetype_table[] = {
1271 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1272};
1273
1274static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1275{
Chris Mason6da6aba2007-12-18 16:15:09 -05001276 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001277 struct btrfs_root *root = BTRFS_I(inode)->root;
1278 struct btrfs_item *item;
1279 struct btrfs_dir_item *di;
1280 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001281 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001282 struct btrfs_path *path;
1283 int ret;
1284 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001285 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001286 int slot;
1287 int advance;
1288 unsigned char d_type;
1289 int over = 0;
1290 u32 di_cur;
1291 u32 di_total;
1292 u32 di_len;
1293 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001294 char tmp_name[32];
1295 char *name_ptr;
1296 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001297
1298 /* FIXME, use a real flag for deciding about the key type */
1299 if (root->fs_info->tree_root == root)
1300 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001301
Chris Mason39544012007-12-12 14:38:19 -05001302 /* special case for "." */
1303 if (filp->f_pos == 0) {
1304 over = filldir(dirent, ".", 1,
1305 1, inode->i_ino,
1306 DT_DIR);
1307 if (over)
1308 return 0;
1309 filp->f_pos = 1;
1310 }
1311
Chris Mason39279cc2007-06-12 06:35:45 -04001312 mutex_lock(&root->fs_info->fs_mutex);
1313 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001314 path = btrfs_alloc_path();
1315 path->reada = 2;
1316
1317 /* special case for .., just use the back ref */
1318 if (filp->f_pos == 1) {
1319 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1320 key.offset = 0;
1321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1322 BUG_ON(ret == 0);
1323 leaf = path->nodes[0];
1324 slot = path->slots[0];
1325 nritems = btrfs_header_nritems(leaf);
1326 if (slot >= nritems) {
1327 btrfs_release_path(root, path);
1328 goto read_dir_items;
1329 }
1330 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1331 btrfs_release_path(root, path);
1332 if (found_key.objectid != key.objectid ||
1333 found_key.type != BTRFS_INODE_REF_KEY)
1334 goto read_dir_items;
1335 over = filldir(dirent, "..", 2,
1336 2, found_key.offset, DT_DIR);
1337 if (over)
1338 goto nopos;
1339 filp->f_pos = 2;
1340 }
1341
1342read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001343 btrfs_set_key_type(&key, key_type);
1344 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001345
Chris Mason39279cc2007-06-12 06:35:45 -04001346 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1347 if (ret < 0)
1348 goto err;
1349 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001350 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001351 leaf = path->nodes[0];
1352 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001353 slot = path->slots[0];
1354 if (advance || slot >= nritems) {
1355 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001356 ret = btrfs_next_leaf(root, path);
1357 if (ret)
1358 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001359 leaf = path->nodes[0];
1360 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001361 slot = path->slots[0];
1362 } else {
1363 slot++;
1364 path->slots[0]++;
1365 }
1366 }
1367 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001368 item = btrfs_item_nr(leaf, slot);
1369 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1370
1371 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001372 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001373 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001374 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001375 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001376 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001377
1378 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001379 advance = 1;
1380 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1381 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001382 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001383 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001384 struct btrfs_key location;
1385
1386 name_len = btrfs_dir_name_len(leaf, di);
1387 if (name_len < 32) {
1388 name_ptr = tmp_name;
1389 } else {
1390 name_ptr = kmalloc(name_len, GFP_NOFS);
1391 BUG_ON(!name_ptr);
1392 }
1393 read_extent_buffer(leaf, name_ptr,
1394 (unsigned long)(di + 1), name_len);
1395
1396 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1397 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001398 over = filldir(dirent, name_ptr, name_len,
1399 found_key.offset,
1400 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001401 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001402
1403 if (name_ptr != tmp_name)
1404 kfree(name_ptr);
1405
Chris Mason39279cc2007-06-12 06:35:45 -04001406 if (over)
1407 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001408 di_len = btrfs_dir_name_len(leaf, di) +
1409 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001410 di_cur += di_len;
1411 di = (struct btrfs_dir_item *)((char *)di + di_len);
1412 }
1413 }
1414 filp->f_pos++;
1415nopos:
1416 ret = 0;
1417err:
1418 btrfs_release_path(root, path);
1419 btrfs_free_path(path);
1420 mutex_unlock(&root->fs_info->fs_mutex);
1421 return ret;
1422}
1423
1424int btrfs_write_inode(struct inode *inode, int wait)
1425{
1426 struct btrfs_root *root = BTRFS_I(inode)->root;
1427 struct btrfs_trans_handle *trans;
1428 int ret = 0;
1429
1430 if (wait) {
1431 mutex_lock(&root->fs_info->fs_mutex);
1432 trans = btrfs_start_transaction(root, 1);
1433 btrfs_set_trans_block_group(trans, inode);
1434 ret = btrfs_commit_transaction(trans, root);
1435 mutex_unlock(&root->fs_info->fs_mutex);
1436 }
1437 return ret;
1438}
1439
1440/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001441 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001442 * inode changes. But, it is most likely to find the inode in cache.
1443 * FIXME, needs more benchmarking...there are no reasons other than performance
1444 * to keep or drop this code.
1445 */
1446void btrfs_dirty_inode(struct inode *inode)
1447{
1448 struct btrfs_root *root = BTRFS_I(inode)->root;
1449 struct btrfs_trans_handle *trans;
1450
1451 mutex_lock(&root->fs_info->fs_mutex);
1452 trans = btrfs_start_transaction(root, 1);
1453 btrfs_set_trans_block_group(trans, inode);
1454 btrfs_update_inode(trans, root, inode);
1455 btrfs_end_transaction(trans, root);
1456 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001457}
1458
1459static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1460 struct btrfs_root *root,
1461 u64 objectid,
1462 struct btrfs_block_group_cache *group,
1463 int mode)
1464{
1465 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001466 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001467 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001468 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001469 int ret;
1470 int owner;
1471
Chris Mason5f39d392007-10-15 16:14:19 -04001472 path = btrfs_alloc_path();
1473 BUG_ON(!path);
1474
Chris Mason39279cc2007-06-12 06:35:45 -04001475 inode = new_inode(root->fs_info->sb);
1476 if (!inode)
1477 return ERR_PTR(-ENOMEM);
1478
Chris Masonb888db2b2007-08-27 16:49:44 -04001479 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1480 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001481 BTRFS_I(inode)->root = root;
Chris Masonb888db2b2007-08-27 16:49:44 -04001482
Chris Mason39279cc2007-06-12 06:35:45 -04001483 if (mode & S_IFDIR)
1484 owner = 0;
1485 else
1486 owner = 1;
1487 group = btrfs_find_block_group(root, group, 0, 0, owner);
1488 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001489 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001490 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1491 if (ret)
1492 goto fail;
1493
Chris Mason39279cc2007-06-12 06:35:45 -04001494 inode->i_uid = current->fsuid;
1495 inode->i_gid = current->fsgid;
1496 inode->i_mode = mode;
1497 inode->i_ino = objectid;
1498 inode->i_blocks = 0;
1499 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001500 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1501 struct btrfs_inode_item);
1502 fill_inode_item(path->nodes[0], inode_item, inode);
1503 btrfs_mark_buffer_dirty(path->nodes[0]);
1504 btrfs_free_path(path);
1505
Chris Mason39279cc2007-06-12 06:35:45 -04001506 location = &BTRFS_I(inode)->location;
1507 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001508 location->offset = 0;
1509 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1510
Chris Mason39279cc2007-06-12 06:35:45 -04001511 insert_inode_hash(inode);
1512 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001513fail:
1514 btrfs_free_path(path);
1515 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001516}
1517
1518static inline u8 btrfs_inode_type(struct inode *inode)
1519{
1520 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1521}
1522
1523static int btrfs_add_link(struct btrfs_trans_handle *trans,
1524 struct dentry *dentry, struct inode *inode)
1525{
1526 int ret;
1527 struct btrfs_key key;
1528 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001529 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001530
Chris Mason39279cc2007-06-12 06:35:45 -04001531 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001532 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1533 key.offset = 0;
1534
1535 ret = btrfs_insert_dir_item(trans, root,
1536 dentry->d_name.name, dentry->d_name.len,
1537 dentry->d_parent->d_inode->i_ino,
1538 &key, btrfs_inode_type(inode));
1539 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001540 ret = btrfs_insert_inode_ref(trans, root,
1541 dentry->d_name.name,
1542 dentry->d_name.len,
1543 inode->i_ino,
1544 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001545 parent_inode = dentry->d_parent->d_inode;
1546 parent_inode->i_size += dentry->d_name.len * 2;
1547 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001548 ret = btrfs_update_inode(trans, root,
1549 dentry->d_parent->d_inode);
1550 }
1551 return ret;
1552}
1553
1554static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1555 struct dentry *dentry, struct inode *inode)
1556{
1557 int err = btrfs_add_link(trans, dentry, inode);
1558 if (!err) {
1559 d_instantiate(dentry, inode);
1560 return 0;
1561 }
1562 if (err > 0)
1563 err = -EEXIST;
1564 return err;
1565}
1566
Josef Bacik618e21d2007-07-11 10:18:17 -04001567static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1568 int mode, dev_t rdev)
1569{
1570 struct btrfs_trans_handle *trans;
1571 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001572 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001573 int err;
1574 int drop_inode = 0;
1575 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001576 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001577
1578 if (!new_valid_dev(rdev))
1579 return -EINVAL;
1580
1581 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001582 err = btrfs_check_free_space(root, 1, 0);
1583 if (err)
1584 goto fail;
1585
Josef Bacik618e21d2007-07-11 10:18:17 -04001586 trans = btrfs_start_transaction(root, 1);
1587 btrfs_set_trans_block_group(trans, dir);
1588
1589 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1590 if (err) {
1591 err = -ENOSPC;
1592 goto out_unlock;
1593 }
1594
1595 inode = btrfs_new_inode(trans, root, objectid,
1596 BTRFS_I(dir)->block_group, mode);
1597 err = PTR_ERR(inode);
1598 if (IS_ERR(inode))
1599 goto out_unlock;
1600
1601 btrfs_set_trans_block_group(trans, inode);
1602 err = btrfs_add_nondir(trans, dentry, inode);
1603 if (err)
1604 drop_inode = 1;
1605 else {
1606 inode->i_op = &btrfs_special_inode_operations;
1607 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001608 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001609 }
1610 dir->i_sb->s_dirt = 1;
1611 btrfs_update_inode_block_group(trans, inode);
1612 btrfs_update_inode_block_group(trans, dir);
1613out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001614 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001615 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001616fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001617 mutex_unlock(&root->fs_info->fs_mutex);
1618
1619 if (drop_inode) {
1620 inode_dec_link_count(inode);
1621 iput(inode);
1622 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001623 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001624 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001625 return err;
1626}
1627
Chris Mason39279cc2007-06-12 06:35:45 -04001628static int btrfs_create(struct inode *dir, struct dentry *dentry,
1629 int mode, struct nameidata *nd)
1630{
1631 struct btrfs_trans_handle *trans;
1632 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001633 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001634 int err;
1635 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001636 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001637 u64 objectid;
1638
1639 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001640 err = btrfs_check_free_space(root, 1, 0);
1641 if (err)
1642 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001643 trans = btrfs_start_transaction(root, 1);
1644 btrfs_set_trans_block_group(trans, dir);
1645
1646 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1647 if (err) {
1648 err = -ENOSPC;
1649 goto out_unlock;
1650 }
1651
1652 inode = btrfs_new_inode(trans, root, objectid,
1653 BTRFS_I(dir)->block_group, mode);
1654 err = PTR_ERR(inode);
1655 if (IS_ERR(inode))
1656 goto out_unlock;
1657
1658 btrfs_set_trans_block_group(trans, inode);
1659 err = btrfs_add_nondir(trans, dentry, inode);
1660 if (err)
1661 drop_inode = 1;
1662 else {
1663 inode->i_mapping->a_ops = &btrfs_aops;
1664 inode->i_fop = &btrfs_file_operations;
1665 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001666 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1667 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001668 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001669 }
1670 dir->i_sb->s_dirt = 1;
1671 btrfs_update_inode_block_group(trans, inode);
1672 btrfs_update_inode_block_group(trans, dir);
1673out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001674 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001675 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001676fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001677 mutex_unlock(&root->fs_info->fs_mutex);
1678
1679 if (drop_inode) {
1680 inode_dec_link_count(inode);
1681 iput(inode);
1682 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001683 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001684 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001685 return err;
1686}
1687
1688static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1689 struct dentry *dentry)
1690{
1691 struct btrfs_trans_handle *trans;
1692 struct btrfs_root *root = BTRFS_I(dir)->root;
1693 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001694 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001695 int err;
1696 int drop_inode = 0;
1697
1698 if (inode->i_nlink == 0)
1699 return -ENOENT;
1700
Chris Mason6da6aba2007-12-18 16:15:09 -05001701#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1702 inode->i_nlink++;
1703#else
Chris Mason39279cc2007-06-12 06:35:45 -04001704 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001705#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001706 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001707 err = btrfs_check_free_space(root, 1, 0);
1708 if (err)
1709 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001710 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001711
Chris Mason39279cc2007-06-12 06:35:45 -04001712 btrfs_set_trans_block_group(trans, dir);
1713 atomic_inc(&inode->i_count);
1714 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001715
Chris Mason39279cc2007-06-12 06:35:45 -04001716 if (err)
1717 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001718
Chris Mason39279cc2007-06-12 06:35:45 -04001719 dir->i_sb->s_dirt = 1;
1720 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001721 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001722
Chris Mason54aa1f42007-06-22 14:16:25 -04001723 if (err)
1724 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001725
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001726 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001727 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001728fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001729 mutex_unlock(&root->fs_info->fs_mutex);
1730
1731 if (drop_inode) {
1732 inode_dec_link_count(inode);
1733 iput(inode);
1734 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001735 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001736 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001737 return err;
1738}
1739
Chris Mason39279cc2007-06-12 06:35:45 -04001740static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1741{
1742 struct inode *inode;
1743 struct btrfs_trans_handle *trans;
1744 struct btrfs_root *root = BTRFS_I(dir)->root;
1745 int err = 0;
1746 int drop_on_err = 0;
1747 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001748 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001749
1750 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001751 err = btrfs_check_free_space(root, 1, 0);
1752 if (err)
1753 goto out_unlock;
1754
Chris Mason39279cc2007-06-12 06:35:45 -04001755 trans = btrfs_start_transaction(root, 1);
1756 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001757
Chris Mason39279cc2007-06-12 06:35:45 -04001758 if (IS_ERR(trans)) {
1759 err = PTR_ERR(trans);
1760 goto out_unlock;
1761 }
1762
1763 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1764 if (err) {
1765 err = -ENOSPC;
1766 goto out_unlock;
1767 }
1768
1769 inode = btrfs_new_inode(trans, root, objectid,
1770 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1771 if (IS_ERR(inode)) {
1772 err = PTR_ERR(inode);
1773 goto out_fail;
1774 }
Chris Mason5f39d392007-10-15 16:14:19 -04001775
Chris Mason39279cc2007-06-12 06:35:45 -04001776 drop_on_err = 1;
1777 inode->i_op = &btrfs_dir_inode_operations;
1778 inode->i_fop = &btrfs_dir_file_operations;
1779 btrfs_set_trans_block_group(trans, inode);
1780
Chris Mason39544012007-12-12 14:38:19 -05001781 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001782 err = btrfs_update_inode(trans, root, inode);
1783 if (err)
1784 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001785
Chris Mason39279cc2007-06-12 06:35:45 -04001786 err = btrfs_add_link(trans, dentry, inode);
1787 if (err)
1788 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001789
Chris Mason39279cc2007-06-12 06:35:45 -04001790 d_instantiate(dentry, inode);
1791 drop_on_err = 0;
1792 dir->i_sb->s_dirt = 1;
1793 btrfs_update_inode_block_group(trans, inode);
1794 btrfs_update_inode_block_group(trans, dir);
1795
1796out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001797 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001798 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001799
Chris Mason39279cc2007-06-12 06:35:45 -04001800out_unlock:
1801 mutex_unlock(&root->fs_info->fs_mutex);
1802 if (drop_on_err)
1803 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001804 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001805 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001806 return err;
1807}
1808
Chris Masona52d9a82007-08-27 16:49:44 -04001809struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1810 size_t page_offset, u64 start, u64 end,
1811 int create)
1812{
1813 int ret;
1814 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001815 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001816 u64 extent_start = 0;
1817 u64 extent_end = 0;
1818 u64 objectid = inode->i_ino;
1819 u32 found_type;
1820 int failed_insert = 0;
1821 struct btrfs_path *path;
1822 struct btrfs_root *root = BTRFS_I(inode)->root;
1823 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001824 struct extent_buffer *leaf;
1825 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001826 struct extent_map *em = NULL;
1827 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1828 struct btrfs_trans_handle *trans = NULL;
1829
1830 path = btrfs_alloc_path();
1831 BUG_ON(!path);
1832 mutex_lock(&root->fs_info->fs_mutex);
1833
1834again:
1835 em = lookup_extent_mapping(em_tree, start, end);
1836 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001837 if (em->start > start) {
1838 printk("get_extent start %Lu em start %Lu\n",
1839 start, em->start);
1840 WARN_ON(1);
1841 }
Chris Masona52d9a82007-08-27 16:49:44 -04001842 goto out;
1843 }
1844 if (!em) {
1845 em = alloc_extent_map(GFP_NOFS);
1846 if (!em) {
1847 err = -ENOMEM;
1848 goto out;
1849 }
Chris Mason5f39d392007-10-15 16:14:19 -04001850 em->start = EXTENT_MAP_HOLE;
1851 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001852 }
1853 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001854 ret = btrfs_lookup_file_extent(trans, root, path,
1855 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001856 if (ret < 0) {
1857 err = ret;
1858 goto out;
1859 }
1860
1861 if (ret != 0) {
1862 if (path->slots[0] == 0)
1863 goto not_found;
1864 path->slots[0]--;
1865 }
1866
Chris Mason5f39d392007-10-15 16:14:19 -04001867 leaf = path->nodes[0];
1868 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001869 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001870 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001871 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1872 found_type = btrfs_key_type(&found_key);
1873 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001874 found_type != BTRFS_EXTENT_DATA_KEY) {
1875 goto not_found;
1876 }
1877
Chris Mason5f39d392007-10-15 16:14:19 -04001878 found_type = btrfs_file_extent_type(leaf, item);
1879 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001880 if (found_type == BTRFS_FILE_EXTENT_REG) {
1881 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001882 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001883 err = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -04001884 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001885 em->start = start;
1886 if (start < extent_start) {
Chris Masonb888db2b2007-08-27 16:49:44 -04001887 if (end < extent_start)
1888 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001889 em->end = extent_end - 1;
1890 } else {
1891 em->end = end;
1892 }
1893 goto not_found_em;
1894 }
Chris Masondb945352007-10-15 16:15:53 -04001895 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1896 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001897 em->start = extent_start;
1898 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001899 em->block_start = EXTENT_MAP_HOLE;
1900 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001901 goto insert;
1902 }
Chris Masondb945352007-10-15 16:15:53 -04001903 bytenr += btrfs_file_extent_offset(leaf, item);
1904 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001905 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001906 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001907 em->start = extent_start;
1908 em->end = extent_end - 1;
1909 goto insert;
1910 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001911 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001912 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001913 size_t size;
1914 size_t extent_offset;
1915 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001916
Chris Mason5f39d392007-10-15 16:14:19 -04001917 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1918 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001919 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001920 ((u64)root->sectorsize - 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001921 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001922 em->start = start;
1923 if (start < extent_start) {
Chris Masonb888db2b2007-08-27 16:49:44 -04001924 if (end < extent_start)
1925 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001926 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001927 } else {
1928 em->end = end;
1929 }
1930 goto not_found_em;
1931 }
1932 em->block_start = EXTENT_MAP_INLINE;
1933 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001934
1935 if (!page) {
1936 em->start = extent_start;
1937 em->end = extent_start + size - 1;
1938 goto out;
1939 }
1940
Chris Mason35ebb932007-10-30 16:56:53 -04001941 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001942 extent_start + page_offset;
1943 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1944 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001945 em->start = extent_start + extent_offset;
1946 em->end = (em->start + copy_size -1) |
1947 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001948 map = kmap(page);
1949 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001950 if (create == 0 && !PageUptodate(page)) {
1951 read_extent_buffer(leaf, map + page_offset, ptr,
1952 copy_size);
1953 flush_dcache_page(page);
1954 } else if (create && PageUptodate(page)) {
1955 if (!trans) {
1956 kunmap(page);
1957 free_extent_map(em);
1958 em = NULL;
1959 btrfs_release_path(root, path);
1960 trans = btrfs_start_transaction(root, 1);
1961 goto again;
1962 }
1963 write_extent_buffer(leaf, map + page_offset, ptr,
1964 copy_size);
1965 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001966 }
Chris Masona52d9a82007-08-27 16:49:44 -04001967 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001968 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001969 goto insert;
1970 } else {
1971 printk("unkknown found_type %d\n", found_type);
1972 WARN_ON(1);
1973 }
1974not_found:
1975 em->start = start;
1976 em->end = end;
1977not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001978 em->block_start = EXTENT_MAP_HOLE;
1979 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001980insert:
1981 btrfs_release_path(root, path);
1982 if (em->start > start || em->end < start) {
Chris Masonb888db2b2007-08-27 16:49:44 -04001983 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001984 err = -EIO;
1985 goto out;
1986 }
1987 ret = add_extent_mapping(em_tree, em);
1988 if (ret == -EEXIST) {
1989 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001990 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001991 if (0 && failed_insert == 1) {
1992 btrfs_drop_extent_cache(inode, start, end);
1993 }
Chris Masona52d9a82007-08-27 16:49:44 -04001994 failed_insert++;
1995 if (failed_insert > 5) {
1996 printk("failing to insert %Lu %Lu\n", start, end);
1997 err = -EIO;
1998 goto out;
1999 }
Chris Masona52d9a82007-08-27 16:49:44 -04002000 goto again;
2001 }
2002 err = 0;
2003out:
2004 btrfs_free_path(path);
2005 if (trans) {
2006 ret = btrfs_end_transaction(trans, root);
2007 if (!err)
2008 err = ret;
2009 }
2010 mutex_unlock(&root->fs_info->fs_mutex);
2011 if (err) {
2012 free_extent_map(em);
2013 WARN_ON(1);
2014 return ERR_PTR(err);
2015 }
2016 return em;
2017}
2018
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002019static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002020{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002021 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002022}
2023
Chris Mason9ebefb182007-06-15 13:50:00 -04002024int btrfs_readpage(struct file *file, struct page *page)
2025{
Chris Masona52d9a82007-08-27 16:49:44 -04002026 struct extent_map_tree *tree;
2027 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2028 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002029}
Chris Mason1832a6d2007-12-21 16:27:21 -05002030
Chris Mason39279cc2007-06-12 06:35:45 -04002031static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2032{
Chris Masona52d9a82007-08-27 16:49:44 -04002033 struct extent_map_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04002034
2035
2036 if (current->flags & PF_MEMALLOC) {
2037 redirty_page_for_writepage(wbc, page);
2038 unlock_page(page);
2039 return 0;
2040 }
Chris Masona52d9a82007-08-27 16:49:44 -04002041 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2042 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2043}
Chris Mason39279cc2007-06-12 06:35:45 -04002044
Chris Masonb293f02e2007-11-01 19:45:34 -04002045static int btrfs_writepages(struct address_space *mapping,
2046 struct writeback_control *wbc)
2047{
2048 struct extent_map_tree *tree;
2049 tree = &BTRFS_I(mapping->host)->extent_tree;
2050 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2051}
2052
Chris Mason3ab2fb52007-11-08 10:59:22 -05002053static int
2054btrfs_readpages(struct file *file, struct address_space *mapping,
2055 struct list_head *pages, unsigned nr_pages)
2056{
2057 struct extent_map_tree *tree;
2058 tree = &BTRFS_I(mapping->host)->extent_tree;
2059 return extent_readpages(tree, mapping, pages, nr_pages,
2060 btrfs_get_extent);
2061}
2062
Chris Masona52d9a82007-08-27 16:49:44 -04002063static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2064{
2065 struct extent_map_tree *tree;
2066 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002067
Chris Masona52d9a82007-08-27 16:49:44 -04002068 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2069 ret = try_release_extent_mapping(tree, page);
2070 if (ret == 1) {
2071 ClearPagePrivate(page);
2072 set_page_private(page, 0);
2073 page_cache_release(page);
2074 }
2075 return ret;
2076}
Chris Mason39279cc2007-06-12 06:35:45 -04002077
Chris Masona52d9a82007-08-27 16:49:44 -04002078static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2079{
2080 struct extent_map_tree *tree;
2081
2082 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2083 extent_invalidatepage(tree, page, offset);
2084 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002085}
2086
Chris Mason9ebefb182007-06-15 13:50:00 -04002087/*
2088 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2089 * called from a page fault handler when a page is first dirtied. Hence we must
2090 * be careful to check for EOF conditions here. We set the page up correctly
2091 * for a written page which means we get ENOSPC checking when writing into
2092 * holes and correct delalloc and unwritten extent mapping on filesystems that
2093 * support these features.
2094 *
2095 * We are not allowed to take the i_mutex here so we have to play games to
2096 * protect against truncate races as the page could now be beyond EOF. Because
2097 * vmtruncate() writes the inode size before removing pages, once we have the
2098 * page lock we can determine safely if the page is beyond EOF. If it is not
2099 * beyond EOF, then the page is guaranteed safe against truncation until we
2100 * unlock the page.
2101 */
2102int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2103{
Chris Mason6da6aba2007-12-18 16:15:09 -05002104 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002105 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002106 unsigned long end;
2107 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002108 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002109 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002110
Chris Mason1832a6d2007-12-21 16:27:21 -05002111 mutex_lock(&root->fs_info->fs_mutex);
2112 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002113 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002114 if (ret)
2115 goto out;
2116
2117 ret = -EINVAL;
2118
Chris Mason9ebefb182007-06-15 13:50:00 -04002119 lock_page(page);
2120 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002121 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002122 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002123
Chris Mason9ebefb182007-06-15 13:50:00 -04002124 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002125 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002126 /* page got truncated out from underneath us */
2127 goto out_unlock;
2128 }
2129
2130 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002131 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002132 end = size & ~PAGE_CACHE_MASK;
2133 else
2134 end = PAGE_CACHE_SIZE;
2135
Chris Masonb888db2b2007-08-27 16:49:44 -04002136 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002137
2138out_unlock:
2139 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002140out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002141 return ret;
2142}
2143
Chris Mason39279cc2007-06-12 06:35:45 -04002144static void btrfs_truncate(struct inode *inode)
2145{
2146 struct btrfs_root *root = BTRFS_I(inode)->root;
2147 int ret;
2148 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002149 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002150
2151 if (!S_ISREG(inode->i_mode))
2152 return;
2153 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2154 return;
2155
2156 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2157
2158 mutex_lock(&root->fs_info->fs_mutex);
2159 trans = btrfs_start_transaction(root, 1);
2160 btrfs_set_trans_block_group(trans, inode);
2161
2162 /* FIXME, add redo link to tree so we don't leak on crash */
2163 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002164 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002165 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002166
Chris Mason39279cc2007-06-12 06:35:45 -04002167 ret = btrfs_end_transaction(trans, root);
2168 BUG_ON(ret);
2169 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002170 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002171 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002172}
2173
Chris Mason4313b392008-01-03 09:08:48 -05002174static int noinline create_subvol(struct btrfs_root *root, char *name,
2175 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002176{
2177 struct btrfs_trans_handle *trans;
2178 struct btrfs_key key;
2179 struct btrfs_root_item root_item;
2180 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002181 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002182 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002183 struct inode *inode;
2184 struct inode *dir;
2185 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002186 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002187 u64 objectid;
2188 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002189 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002190
2191 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002192 ret = btrfs_check_free_space(root, 1, 0);
2193 if (ret)
2194 goto fail_commit;
2195
Chris Mason39279cc2007-06-12 06:35:45 -04002196 trans = btrfs_start_transaction(root, 1);
2197 BUG_ON(!trans);
2198
Chris Mason7bb86312007-12-11 09:25:06 -05002199 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2200 0, &objectid);
2201 if (ret)
2202 goto fail;
2203
2204 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2205 objectid, trans->transid, 0, 0,
2206 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002207 if (IS_ERR(leaf))
2208 return PTR_ERR(leaf);
2209
2210 btrfs_set_header_nritems(leaf, 0);
2211 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002212 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002213 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002214 btrfs_set_header_owner(leaf, objectid);
2215
Chris Mason5f39d392007-10-15 16:14:19 -04002216 write_extent_buffer(leaf, root->fs_info->fsid,
2217 (unsigned long)btrfs_header_fsid(leaf),
2218 BTRFS_FSID_SIZE);
2219 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002220
2221 inode_item = &root_item.inode;
2222 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002223 inode_item->generation = cpu_to_le64(1);
2224 inode_item->size = cpu_to_le64(3);
2225 inode_item->nlink = cpu_to_le32(1);
2226 inode_item->nblocks = cpu_to_le64(1);
2227 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002228
Chris Masondb945352007-10-15 16:15:53 -04002229 btrfs_set_root_bytenr(&root_item, leaf->start);
2230 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002231 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002232 btrfs_set_root_used(&root_item, 0);
2233
Chris Mason5eda7b52007-06-22 14:16:25 -04002234 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2235 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002236
2237 free_extent_buffer(leaf);
2238 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002239
Chris Mason39279cc2007-06-12 06:35:45 -04002240 btrfs_set_root_dirid(&root_item, new_dirid);
2241
2242 key.objectid = objectid;
2243 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002244 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2245 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2246 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002247 if (ret)
2248 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002249
2250 /*
2251 * insert the directory item
2252 */
2253 key.offset = (u64)-1;
2254 dir = root->fs_info->sb->s_root->d_inode;
2255 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2256 name, namelen, dir->i_ino, &key,
2257 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002258 if (ret)
2259 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002260
Chris Mason39544012007-12-12 14:38:19 -05002261 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2262 name, namelen, objectid,
2263 root->fs_info->sb->s_root->d_inode->i_ino);
2264 if (ret)
2265 goto fail;
2266
Chris Mason39279cc2007-06-12 06:35:45 -04002267 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002268 if (ret)
2269 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002270
Josef Bacik58176a92007-08-29 15:47:34 -04002271 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002272 BUG_ON(!new_root);
2273
2274 trans = btrfs_start_transaction(new_root, 1);
2275 BUG_ON(!trans);
2276
2277 inode = btrfs_new_inode(trans, new_root, new_dirid,
2278 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002279 if (IS_ERR(inode))
2280 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002281 inode->i_op = &btrfs_dir_inode_operations;
2282 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002283 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002284
Chris Mason39544012007-12-12 14:38:19 -05002285 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2286 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002287 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002288 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002289 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002290 if (ret)
2291 goto fail;
2292fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002293 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002294 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002295 if (err && !ret)
2296 ret = err;
2297fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002298 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002299 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002300 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002301 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002302}
2303
2304static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2305{
Chris Mason3063d292008-01-08 15:46:30 -05002306 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002307 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002308 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002309 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002310 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002311
2312 if (!root->ref_cows)
2313 return -EINVAL;
2314
2315 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002316 ret = btrfs_check_free_space(root, 1, 0);
2317 if (ret)
2318 goto fail_unlock;
2319
Chris Mason3063d292008-01-08 15:46:30 -05002320 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2321 if (!pending_snapshot) {
2322 ret = -ENOMEM;
2323 goto fail_unlock;
2324 }
Yanfb4bc1e2008-01-17 11:59:51 -05002325 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002326 if (!pending_snapshot->name) {
2327 ret = -ENOMEM;
2328 kfree(pending_snapshot);
2329 goto fail_unlock;
2330 }
Yanfb4bc1e2008-01-17 11:59:51 -05002331 memcpy(pending_snapshot->name, name, namelen);
2332 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002333 trans = btrfs_start_transaction(root, 1);
2334 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002335 pending_snapshot->root = root;
2336 list_add(&pending_snapshot->list,
2337 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002338 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002339 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002340
Chris Mason1832a6d2007-12-21 16:27:21 -05002341fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002342 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002343 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002344 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002345 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002346}
2347
Chris Masonedbd8d42007-12-21 16:27:24 -05002348unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002349 struct file_ra_state *ra, struct file *file,
2350 pgoff_t offset, pgoff_t last_index)
2351{
2352 pgoff_t req_size;
2353
2354#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2355 req_size = last_index - offset + 1;
2356 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2357 return offset;
2358#else
2359 req_size = min(last_index - offset + 1, (pgoff_t)128);
2360 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2361 return offset + req_size;
2362#endif
2363}
2364
2365int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002366 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002367 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002368 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2369 struct page *page;
2370 unsigned long last_index;
2371 unsigned long ra_index = 0;
2372 u64 page_start;
2373 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002374 u64 delalloc_start;
2375 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002376 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002377 int ret;
2378
2379 mutex_lock(&root->fs_info->fs_mutex);
2380 ret = btrfs_check_free_space(root, inode->i_size, 0);
2381 mutex_unlock(&root->fs_info->fs_mutex);
2382 if (ret)
2383 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002384
2385 mutex_lock(&inode->i_mutex);
2386 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2387 for (i = 0; i <= last_index; i++) {
2388 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002389 ra_index = btrfs_force_ra(inode->i_mapping,
2390 &file->f_ra,
2391 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002392 }
2393 page = grab_cache_page(inode->i_mapping, i);
2394 if (!page)
2395 goto out_unlock;
2396 if (!PageUptodate(page)) {
2397 btrfs_readpage(NULL, page);
2398 lock_page(page);
2399 if (!PageUptodate(page)) {
2400 unlock_page(page);
2401 page_cache_release(page);
2402 goto out_unlock;
2403 }
2404 }
Chris Mason35ebb932007-10-30 16:56:53 -04002405 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002406 page_end = page_start + PAGE_CACHE_SIZE - 1;
2407
2408 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002409 delalloc_start = page_start;
2410 existing_delalloc =
2411 count_range_bits(&BTRFS_I(inode)->extent_tree,
2412 &delalloc_start, page_end,
2413 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002414 set_extent_delalloc(em_tree, page_start,
2415 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002416
2417 spin_lock(&root->fs_info->delalloc_lock);
2418 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2419 existing_delalloc;
2420 spin_unlock(&root->fs_info->delalloc_lock);
2421
Chris Mason86479a02007-09-10 19:58:16 -04002422 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2423 set_page_dirty(page);
2424 unlock_page(page);
2425 page_cache_release(page);
2426 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2427 }
2428
2429out_unlock:
2430 mutex_unlock(&inode->i_mutex);
2431 return 0;
2432}
2433
Chris Masonedbd8d42007-12-21 16:27:24 -05002434static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2435{
2436 u64 new_size;
2437 u64 old_size;
2438 struct btrfs_ioctl_vol_args *vol_args;
2439 struct btrfs_trans_handle *trans;
2440 char *sizestr;
2441 int ret = 0;
2442 int namelen;
2443 int mod = 0;
2444
2445 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2446
2447 if (!vol_args)
2448 return -ENOMEM;
2449
2450 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2451 ret = -EFAULT;
2452 goto out;
2453 }
2454 namelen = strlen(vol_args->name);
2455 if (namelen > BTRFS_VOL_NAME_MAX) {
2456 ret = -EINVAL;
2457 goto out;
2458 }
2459
2460 sizestr = vol_args->name;
2461 if (!strcmp(sizestr, "max"))
2462 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2463 else {
2464 if (sizestr[0] == '-') {
2465 mod = -1;
2466 sizestr++;
2467 } else if (sizestr[0] == '+') {
2468 mod = 1;
2469 sizestr++;
2470 }
2471 new_size = btrfs_parse_size(sizestr);
2472 if (new_size == 0) {
2473 ret = -EINVAL;
2474 goto out;
2475 }
2476 }
2477
2478 mutex_lock(&root->fs_info->fs_mutex);
2479 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2480
2481 if (mod < 0) {
2482 if (new_size > old_size) {
2483 ret = -EINVAL;
2484 goto out_unlock;
2485 }
2486 new_size = old_size - new_size;
2487 } else if (mod > 0) {
2488 new_size = old_size + new_size;
2489 }
2490
2491 if (new_size < 256 * 1024 * 1024) {
2492 ret = -EINVAL;
2493 goto out_unlock;
2494 }
2495 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2496 ret = -EFBIG;
2497 goto out_unlock;
2498 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002499
2500 do_div(new_size, root->sectorsize);
2501 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002502
2503printk("new size is %Lu\n", new_size);
2504 if (new_size > old_size) {
2505 trans = btrfs_start_transaction(root, 1);
2506 ret = btrfs_grow_extent_tree(trans, root, new_size);
2507 btrfs_commit_transaction(trans, root);
2508 } else {
2509 ret = btrfs_shrink_extent_tree(root, new_size);
2510 }
2511
2512out_unlock:
2513 mutex_unlock(&root->fs_info->fs_mutex);
2514out:
2515 kfree(vol_args);
2516 return ret;
2517}
2518
Chris Mason4313b392008-01-03 09:08:48 -05002519static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2520 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002521{
Chris Mason4aec2b52007-12-18 16:25:45 -05002522 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002523 struct btrfs_dir_item *di;
2524 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002525 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002526 int namelen;
2527 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002528
Chris Mason4aec2b52007-12-18 16:25:45 -05002529 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002530
Chris Mason4aec2b52007-12-18 16:25:45 -05002531 if (!vol_args)
2532 return -ENOMEM;
2533
2534 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2535 ret = -EFAULT;
2536 goto out;
2537 }
2538
2539 namelen = strlen(vol_args->name);
2540 if (namelen > BTRFS_VOL_NAME_MAX) {
2541 ret = -EINVAL;
2542 goto out;
2543 }
2544 if (strchr(vol_args->name, '/')) {
2545 ret = -EINVAL;
2546 goto out;
2547 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002548
2549 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002550 if (!path) {
2551 ret = -ENOMEM;
2552 goto out;
2553 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002554
2555 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2556 mutex_lock(&root->fs_info->fs_mutex);
2557 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2558 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002559 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002560 mutex_unlock(&root->fs_info->fs_mutex);
2561 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002562
2563 if (di && !IS_ERR(di)) {
2564 ret = -EEXIST;
2565 goto out;
2566 }
2567
2568 if (IS_ERR(di)) {
2569 ret = PTR_ERR(di);
2570 goto out;
2571 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002572
2573 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002574 ret = create_subvol(root, vol_args->name, namelen);
2575 else
2576 ret = create_snapshot(root, vol_args->name, namelen);
2577out:
2578 kfree(vol_args);
2579 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002580}
2581
2582static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002583{
Chris Mason6da6aba2007-12-18 16:15:09 -05002584 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002585 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002586
2587 switch (inode->i_mode & S_IFMT) {
2588 case S_IFDIR:
2589 mutex_lock(&root->fs_info->fs_mutex);
2590 btrfs_defrag_root(root, 0);
2591 btrfs_defrag_root(root->fs_info->extent_root, 0);
2592 mutex_unlock(&root->fs_info->fs_mutex);
2593 break;
2594 case S_IFREG:
2595 btrfs_defrag_file(file);
2596 break;
2597 }
2598
2599 return 0;
2600}
2601
2602long btrfs_ioctl(struct file *file, unsigned int
2603 cmd, unsigned long arg)
2604{
Chris Mason6da6aba2007-12-18 16:15:09 -05002605 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002606
2607 switch (cmd) {
2608 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002609 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002610 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002611 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002612 case BTRFS_IOC_RESIZE:
2613 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002614 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002615
2616 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002617}
2618
Chris Mason39279cc2007-06-12 06:35:45 -04002619/*
2620 * Called inside transaction, so use GFP_NOFS
2621 */
2622struct inode *btrfs_alloc_inode(struct super_block *sb)
2623{
2624 struct btrfs_inode *ei;
2625
2626 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2627 if (!ei)
2628 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002629 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002630 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002631 return &ei->vfs_inode;
2632}
2633
2634void btrfs_destroy_inode(struct inode *inode)
2635{
2636 WARN_ON(!list_empty(&inode->i_dentry));
2637 WARN_ON(inode->i_data.nrpages);
2638
Chris Mason8c416c92008-01-14 15:10:26 -05002639 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002640 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2641}
2642
Chris Mason44ec0b72007-10-29 10:55:05 -04002643#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2644static void init_once(struct kmem_cache * cachep, void *foo)
2645#else
Chris Mason39279cc2007-06-12 06:35:45 -04002646static void init_once(void * foo, struct kmem_cache * cachep,
2647 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002648#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002649{
2650 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2651
2652 inode_init_once(&ei->vfs_inode);
2653}
2654
2655void btrfs_destroy_cachep(void)
2656{
2657 if (btrfs_inode_cachep)
2658 kmem_cache_destroy(btrfs_inode_cachep);
2659 if (btrfs_trans_handle_cachep)
2660 kmem_cache_destroy(btrfs_trans_handle_cachep);
2661 if (btrfs_transaction_cachep)
2662 kmem_cache_destroy(btrfs_transaction_cachep);
2663 if (btrfs_bit_radix_cachep)
2664 kmem_cache_destroy(btrfs_bit_radix_cachep);
2665 if (btrfs_path_cachep)
2666 kmem_cache_destroy(btrfs_path_cachep);
2667}
2668
Chris Mason86479a02007-09-10 19:58:16 -04002669struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002670 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002671#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2672 void (*ctor)(struct kmem_cache *, void *)
2673#else
Chris Mason92fee662007-07-25 12:31:35 -04002674 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002675 unsigned long)
2676#endif
2677 )
Chris Mason92fee662007-07-25 12:31:35 -04002678{
2679 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2680 SLAB_MEM_SPREAD | extra_flags), ctor
2681#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2682 ,NULL
2683#endif
2684 );
2685}
2686
Chris Mason39279cc2007-06-12 06:35:45 -04002687int btrfs_init_cachep(void)
2688{
Chris Mason86479a02007-09-10 19:58:16 -04002689 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002690 sizeof(struct btrfs_inode),
2691 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002692 if (!btrfs_inode_cachep)
2693 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002694 btrfs_trans_handle_cachep =
2695 btrfs_cache_create("btrfs_trans_handle_cache",
2696 sizeof(struct btrfs_trans_handle),
2697 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002698 if (!btrfs_trans_handle_cachep)
2699 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002700 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002701 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002702 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002703 if (!btrfs_transaction_cachep)
2704 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002705 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002706 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002707 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002708 if (!btrfs_path_cachep)
2709 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002710 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002711 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002712 if (!btrfs_bit_radix_cachep)
2713 goto fail;
2714 return 0;
2715fail:
2716 btrfs_destroy_cachep();
2717 return -ENOMEM;
2718}
2719
2720static int btrfs_getattr(struct vfsmount *mnt,
2721 struct dentry *dentry, struct kstat *stat)
2722{
2723 struct inode *inode = dentry->d_inode;
2724 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002725 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002726 return 0;
2727}
2728
2729static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2730 struct inode * new_dir,struct dentry *new_dentry)
2731{
2732 struct btrfs_trans_handle *trans;
2733 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2734 struct inode *new_inode = new_dentry->d_inode;
2735 struct inode *old_inode = old_dentry->d_inode;
2736 struct timespec ctime = CURRENT_TIME;
2737 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002738 int ret;
2739
2740 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2741 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2742 return -ENOTEMPTY;
2743 }
Chris Mason5f39d392007-10-15 16:14:19 -04002744
Chris Mason39279cc2007-06-12 06:35:45 -04002745 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002746 ret = btrfs_check_free_space(root, 1, 0);
2747 if (ret)
2748 goto out_unlock;
2749
Chris Mason39279cc2007-06-12 06:35:45 -04002750 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002751
Chris Mason39279cc2007-06-12 06:35:45 -04002752 btrfs_set_trans_block_group(trans, new_dir);
2753 path = btrfs_alloc_path();
2754 if (!path) {
2755 ret = -ENOMEM;
2756 goto out_fail;
2757 }
2758
2759 old_dentry->d_inode->i_nlink++;
2760 old_dir->i_ctime = old_dir->i_mtime = ctime;
2761 new_dir->i_ctime = new_dir->i_mtime = ctime;
2762 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002763
Chris Mason39279cc2007-06-12 06:35:45 -04002764 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2765 if (ret)
2766 goto out_fail;
2767
2768 if (new_inode) {
2769 new_inode->i_ctime = CURRENT_TIME;
2770 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2771 if (ret)
2772 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002773 }
2774 ret = btrfs_add_link(trans, new_dentry, old_inode);
2775 if (ret)
2776 goto out_fail;
2777
2778out_fail:
2779 btrfs_free_path(path);
2780 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002781out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002782 mutex_unlock(&root->fs_info->fs_mutex);
2783 return ret;
2784}
2785
2786static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2787 const char *symname)
2788{
2789 struct btrfs_trans_handle *trans;
2790 struct btrfs_root *root = BTRFS_I(dir)->root;
2791 struct btrfs_path *path;
2792 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002793 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002794 int err;
2795 int drop_inode = 0;
2796 u64 objectid;
2797 int name_len;
2798 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002799 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002800 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002801 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002802 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002803
2804 name_len = strlen(symname) + 1;
2805 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2806 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002807
Chris Mason39279cc2007-06-12 06:35:45 -04002808 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002809 err = btrfs_check_free_space(root, 1, 0);
2810 if (err)
2811 goto out_fail;
2812
Chris Mason39279cc2007-06-12 06:35:45 -04002813 trans = btrfs_start_transaction(root, 1);
2814 btrfs_set_trans_block_group(trans, dir);
2815
2816 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2817 if (err) {
2818 err = -ENOSPC;
2819 goto out_unlock;
2820 }
2821
2822 inode = btrfs_new_inode(trans, root, objectid,
2823 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2824 err = PTR_ERR(inode);
2825 if (IS_ERR(inode))
2826 goto out_unlock;
2827
2828 btrfs_set_trans_block_group(trans, inode);
2829 err = btrfs_add_nondir(trans, dentry, inode);
2830 if (err)
2831 drop_inode = 1;
2832 else {
2833 inode->i_mapping->a_ops = &btrfs_aops;
2834 inode->i_fop = &btrfs_file_operations;
2835 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002836 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2837 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002838 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002839 }
2840 dir->i_sb->s_dirt = 1;
2841 btrfs_update_inode_block_group(trans, inode);
2842 btrfs_update_inode_block_group(trans, dir);
2843 if (drop_inode)
2844 goto out_unlock;
2845
2846 path = btrfs_alloc_path();
2847 BUG_ON(!path);
2848 key.objectid = inode->i_ino;
2849 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002850 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2851 datasize = btrfs_file_extent_calc_inline_size(name_len);
2852 err = btrfs_insert_empty_item(trans, root, path, &key,
2853 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002854 if (err) {
2855 drop_inode = 1;
2856 goto out_unlock;
2857 }
Chris Mason5f39d392007-10-15 16:14:19 -04002858 leaf = path->nodes[0];
2859 ei = btrfs_item_ptr(leaf, path->slots[0],
2860 struct btrfs_file_extent_item);
2861 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2862 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002863 BTRFS_FILE_EXTENT_INLINE);
2864 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002865 write_extent_buffer(leaf, symname, ptr, name_len);
2866 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002867 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002868
Chris Mason39279cc2007-06-12 06:35:45 -04002869 inode->i_op = &btrfs_symlink_inode_operations;
2870 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2871 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002872 err = btrfs_update_inode(trans, root, inode);
2873 if (err)
2874 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002875
2876out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002877 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002878 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002879out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002880 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002881 if (drop_inode) {
2882 inode_dec_link_count(inode);
2883 iput(inode);
2884 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002885 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002886 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002887 return err;
2888}
Yanfdebe2b2008-01-14 13:26:08 -05002889static int btrfs_permission(struct inode *inode, int mask,
2890 struct nameidata *nd)
2891{
2892 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2893 return -EACCES;
2894 return generic_permission(inode, mask, NULL);
2895}
Chris Mason39279cc2007-06-12 06:35:45 -04002896
2897static struct inode_operations btrfs_dir_inode_operations = {
2898 .lookup = btrfs_lookup,
2899 .create = btrfs_create,
2900 .unlink = btrfs_unlink,
2901 .link = btrfs_link,
2902 .mkdir = btrfs_mkdir,
2903 .rmdir = btrfs_rmdir,
2904 .rename = btrfs_rename,
2905 .symlink = btrfs_symlink,
2906 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002907 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002908 .setxattr = generic_setxattr,
2909 .getxattr = generic_getxattr,
2910 .listxattr = btrfs_listxattr,
2911 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002912 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002913};
Chris Mason39279cc2007-06-12 06:35:45 -04002914static struct inode_operations btrfs_dir_ro_inode_operations = {
2915 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002916 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002917};
Chris Mason39279cc2007-06-12 06:35:45 -04002918static struct file_operations btrfs_dir_file_operations = {
2919 .llseek = generic_file_llseek,
2920 .read = generic_read_dir,
2921 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002922 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002923#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002924 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002925#endif
2926};
2927
Chris Mason07157aa2007-08-30 08:50:51 -04002928static struct extent_map_ops btrfs_extent_map_ops = {
2929 .fill_delalloc = run_delalloc_range,
2930 .writepage_io_hook = btrfs_writepage_io_hook,
2931 .readpage_io_hook = btrfs_readpage_io_hook,
2932 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2933};
2934
Chris Mason39279cc2007-06-12 06:35:45 -04002935static struct address_space_operations btrfs_aops = {
2936 .readpage = btrfs_readpage,
2937 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04002938 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002939 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002940 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002941 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002942 .invalidatepage = btrfs_invalidatepage,
2943 .releasepage = btrfs_releasepage,
2944 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002945};
2946
2947static struct address_space_operations btrfs_symlink_aops = {
2948 .readpage = btrfs_readpage,
2949 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002950 .invalidatepage = btrfs_invalidatepage,
2951 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002952};
2953
2954static struct inode_operations btrfs_file_inode_operations = {
2955 .truncate = btrfs_truncate,
2956 .getattr = btrfs_getattr,
2957 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002958 .setxattr = generic_setxattr,
2959 .getxattr = generic_getxattr,
2960 .listxattr = btrfs_listxattr,
2961 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002962 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002963};
Josef Bacik618e21d2007-07-11 10:18:17 -04002964static struct inode_operations btrfs_special_inode_operations = {
2965 .getattr = btrfs_getattr,
2966 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002967 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002968};
Chris Mason39279cc2007-06-12 06:35:45 -04002969static struct inode_operations btrfs_symlink_inode_operations = {
2970 .readlink = generic_readlink,
2971 .follow_link = page_follow_link_light,
2972 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05002973 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002974};