blob: f83f88ca8ac749dc569d0641574382ad4d63b5f8 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Mason1832a6d2007-12-21 16:27:21 -050075int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
76 int for_del)
77{
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
80 u64 thresh;
81 int ret = 0;
82
Chris Mason9cce6c32008-01-15 08:44:06 -050083 return 0;
84
Chris Mason1832a6d2007-12-21 16:27:21 -050085 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 else
Chris Masonf9ef6602008-01-03 09:22:38 -050088 thresh = total * 85;
89
90 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050091
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
94 ret = -ENOSPC;
95 spin_unlock(&root->fs_info->delalloc_lock);
96 return ret;
97}
98
Chris Masonbe20aa92007-12-17 20:14:01 -050099static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400103 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500105 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400106 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -0500107 struct btrfs_key ins;
108 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400109
Chris Masonb888db22007-08-27 16:49:44 -0400110 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400111 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 btrfs_set_trans_block_group(trans, inode);
113
Chris Masondb945352007-10-15 16:15:53 -0400114 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400116 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400117 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -0400118
Chris Mason179e29e2007-11-01 11:28:41 -0400119 if (alloc_hint == EXTENT_MAP_INLINE)
120 goto out;
121
Chris Masonc59f8952007-12-17 20:14:04 -0500122 while(num_bytes > 0) {
123 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
124 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
125 root->root_key.objectid,
126 trans->transid,
127 inode->i_ino, start, 0,
128 alloc_hint, (u64)-1, &ins, 1);
129 if (ret) {
130 WARN_ON(1);
131 goto out;
132 }
133 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
134 start, ins.objectid, ins.offset,
135 ins.offset);
136 num_bytes -= cur_alloc_size;
137 alloc_hint = ins.objectid + ins.offset;
138 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400139 }
Chris Masondc17ff82008-01-08 15:46:30 -0500140 btrfs_add_ordered_inode(inode);
Chris Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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;
604 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500605 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400606
607 root = BTRFS_I(dir)->root;
608 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500609
610 ret = btrfs_check_free_space(root, 1, 1);
611 if (ret)
612 goto fail;
613
Chris Mason39279cc2007-06-12 06:35:45 -0400614 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400615
Chris Mason39279cc2007-06-12 06:35:45 -0400616 btrfs_set_trans_block_group(trans, dir);
617 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400618 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400619
Chris Mason39279cc2007-06-12 06:35:45 -0400620 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500621fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400622 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400623 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500624 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400625 return ret;
626}
627
628static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
629{
630 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500631 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400632 int ret;
633 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400634 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500635 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400636
Yan134d4512007-10-25 15:49:25 -0400637 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
638 return -ENOTEMPTY;
639
Chris Mason39279cc2007-06-12 06:35:45 -0400640 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500641 ret = btrfs_check_free_space(root, 1, 1);
642 if (ret)
643 goto fail;
644
Chris Mason39279cc2007-06-12 06:35:45 -0400645 trans = btrfs_start_transaction(root, 1);
646 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400647
648 /* now the directory is empty */
649 err = btrfs_unlink_trans(trans, root, dir, dentry);
650 if (!err) {
651 inode->i_size = 0;
652 }
Chris Mason39544012007-12-12 14:38:19 -0500653
Chris Masond3c2fdc2007-09-17 10:58:06 -0400654 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400655 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500656fail:
Yan134d4512007-10-25 15:49:25 -0400657 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400658 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500659 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500660
Chris Mason39279cc2007-06-12 06:35:45 -0400661 if (ret && !err)
662 err = ret;
663 return err;
664}
665
666static int btrfs_free_inode(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root,
668 struct inode *inode)
669{
670 struct btrfs_path *path;
671 int ret;
672
673 clear_inode(inode);
674
675 path = btrfs_alloc_path();
676 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400677 ret = btrfs_lookup_inode(trans, root, path,
678 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400679 if (ret > 0)
680 ret = -ENOENT;
681 if (!ret)
682 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400683 btrfs_free_path(path);
684 return ret;
685}
686
687/*
Chris Mason39279cc2007-06-12 06:35:45 -0400688 * this can truncate away extent items, csum items and directory items.
689 * It starts at a high offset and removes keys until it can't find
690 * any higher than i_size.
691 *
692 * csum items that cross the new i_size are truncated to the new size
693 * as well.
694 */
695static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
696 struct btrfs_root *root,
697 struct inode *inode)
698{
699 int ret;
700 struct btrfs_path *path;
701 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400702 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400703 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400704 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400705 struct btrfs_file_extent_item *fi;
706 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400707 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400708 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500709 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500710 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400711 int found_extent;
712 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400713 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400714
Chris Masona52d9a82007-08-27 16:49:44 -0400715 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400716 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400717 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400718 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400719
Chris Mason39279cc2007-06-12 06:35:45 -0400720 /* FIXME, add redo link to tree so we don't leak on crash */
721 key.objectid = inode->i_ino;
722 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400723 key.type = (u8)-1;
724
Chris Mason39279cc2007-06-12 06:35:45 -0400725 while(1) {
726 btrfs_init_path(path);
727 fi = NULL;
728 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
729 if (ret < 0) {
730 goto error;
731 }
732 if (ret > 0) {
733 BUG_ON(path->slots[0] == 0);
734 path->slots[0]--;
735 }
Chris Mason5f39d392007-10-15 16:14:19 -0400736 leaf = path->nodes[0];
737 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
738 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400739
Chris Mason5f39d392007-10-15 16:14:19 -0400740 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400741 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400742
Chris Mason39279cc2007-06-12 06:35:45 -0400743 if (found_type != BTRFS_CSUM_ITEM_KEY &&
744 found_type != BTRFS_DIR_ITEM_KEY &&
745 found_type != BTRFS_DIR_INDEX_KEY &&
746 found_type != BTRFS_EXTENT_DATA_KEY)
747 break;
748
Chris Mason5f39d392007-10-15 16:14:19 -0400749 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400750 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400751 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400752 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400753 extent_type = btrfs_file_extent_type(leaf, fi);
754 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400755 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400756 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400757 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
758 struct btrfs_item *item = btrfs_item_nr(leaf,
759 path->slots[0]);
760 item_end += btrfs_file_extent_inline_len(leaf,
761 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400762 }
Yan008630c2007-11-07 13:31:09 -0500763 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400764 }
765 if (found_type == BTRFS_CSUM_ITEM_KEY) {
766 ret = btrfs_csum_truncate(trans, root, path,
767 inode->i_size);
768 BUG_ON(ret);
769 }
Yan008630c2007-11-07 13:31:09 -0500770 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400771 if (found_type == BTRFS_DIR_ITEM_KEY) {
772 found_type = BTRFS_INODE_ITEM_KEY;
773 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
774 found_type = BTRFS_CSUM_ITEM_KEY;
775 } else if (found_type) {
776 found_type--;
777 } else {
778 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400779 }
Yana61721d2007-09-17 11:08:38 -0400780 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400781 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400782 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400783 }
Chris Mason5f39d392007-10-15 16:14:19 -0400784 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400785 del_item = 1;
786 else
787 del_item = 0;
788 found_extent = 0;
789
790 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400791 if (found_type != BTRFS_EXTENT_DATA_KEY)
792 goto delete;
793
794 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400795 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400796 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400797 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400798 u64 orig_num_bytes =
799 btrfs_file_extent_num_bytes(leaf, fi);
800 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400801 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400802 btrfs_set_file_extent_num_bytes(leaf, fi,
803 extent_num_bytes);
804 num_dec = (orig_num_bytes -
805 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400806 if (extent_start != 0) {
807 inode->i_blocks -= num_dec;
808 }
Chris Mason5f39d392007-10-15 16:14:19 -0400809 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400810 } else {
Chris Masondb945352007-10-15 16:15:53 -0400811 extent_num_bytes =
812 btrfs_file_extent_disk_num_bytes(leaf,
813 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400814 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400815 num_dec = btrfs_file_extent_num_bytes(leaf,
816 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400817 if (extent_start != 0) {
818 found_extent = 1;
819 inode->i_blocks -= num_dec;
820 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500821 root_gen = btrfs_header_generation(leaf);
822 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400823 }
Chris Mason179e29e2007-11-01 11:28:41 -0400824 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
825 !del_item) {
826 u32 newsize = inode->i_size - found_key.offset;
827 newsize = btrfs_file_extent_calc_inline_size(newsize);
828 ret = btrfs_truncate_item(trans, root, path,
829 newsize, 1);
830 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400831 }
Chris Mason179e29e2007-11-01 11:28:41 -0400832delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400833 if (del_item) {
834 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400835 if (ret)
836 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400837 } else {
838 break;
839 }
840 btrfs_release_path(root, path);
841 if (found_extent) {
842 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500843 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500844 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500845 root_gen, inode->i_ino,
846 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400847 BUG_ON(ret);
848 }
849 }
850 ret = 0;
851error:
852 btrfs_release_path(root, path);
853 btrfs_free_path(path);
854 inode->i_sb->s_dirt = 1;
855 return ret;
856}
857
Chris Masonb888db22007-08-27 16:49:44 -0400858static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400859 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400860{
Chris Mason39279cc2007-06-12 06:35:45 -0400861 char *kaddr;
Chris Masonb888db22007-08-27 16:49:44 -0400862 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500863 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400864 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400865 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500866 u64 existing_delalloc;
867 u64 delalloc_start;
868 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400869
Chris Mason190662b2007-12-18 16:25:45 -0500870 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400871 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400872
Chris Masonb888db22007-08-27 16:49:44 -0400873 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500874 delalloc_start = page_start;
875 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
876 &delalloc_start, page_end,
877 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonb888db22007-08-27 16:49:44 -0400878 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
879 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500880
881 spin_lock(&root->fs_info->delalloc_lock);
882 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
883 spin_unlock(&root->fs_info->delalloc_lock);
884
Chris Masona52d9a82007-08-27 16:49:44 -0400885 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400886 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400887 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
888 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400889 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400890 }
Chris Masonb888db22007-08-27 16:49:44 -0400891 set_page_dirty(page);
892 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400893
Chris Masona52d9a82007-08-27 16:49:44 -0400894 return ret;
895}
896
897/*
898 * taken from block_truncate_page, but does cow as it zeros out
899 * any bytes left in the last page in the file.
900 */
901static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
902{
903 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400904 struct btrfs_root *root = BTRFS_I(inode)->root;
905 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400906 pgoff_t index = from >> PAGE_CACHE_SHIFT;
907 unsigned offset = from & (PAGE_CACHE_SIZE-1);
908 struct page *page;
909 int ret = 0;
910 u64 page_start;
911
912 if ((offset & (blocksize - 1)) == 0)
913 goto out;
914
915 ret = -ENOMEM;
916 page = grab_cache_page(mapping, index);
917 if (!page)
918 goto out;
919 if (!PageUptodate(page)) {
920 ret = btrfs_readpage(NULL, page);
921 lock_page(page);
922 if (!PageUptodate(page)) {
923 ret = -EIO;
924 goto out;
925 }
926 }
Chris Mason35ebb932007-10-30 16:56:53 -0400927 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400928
Chris Masonb888db22007-08-27 16:49:44 -0400929 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400930
Chris Mason39279cc2007-06-12 06:35:45 -0400931 unlock_page(page);
932 page_cache_release(page);
933out:
934 return ret;
935}
936
937static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
938{
939 struct inode *inode = dentry->d_inode;
940 int err;
941
942 err = inode_change_ok(inode, attr);
943 if (err)
944 return err;
945
946 if (S_ISREG(inode->i_mode) &&
947 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
948 struct btrfs_trans_handle *trans;
949 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400950 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400953 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400954 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400955 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400956 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400957
958 if (attr->ia_size <= pos)
959 goto out;
960
Chris Mason1832a6d2007-12-21 16:27:21 -0500961 mutex_lock(&root->fs_info->fs_mutex);
962 err = btrfs_check_free_space(root, 1, 0);
963 mutex_unlock(&root->fs_info->fs_mutex);
964 if (err)
965 goto fail;
966
Chris Mason39279cc2007-06-12 06:35:45 -0400967 btrfs_truncate_page(inode->i_mapping, inode->i_size);
968
Chris Mason2bf5a722007-08-30 11:54:02 -0400969 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400970 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400971
972 mutex_lock(&root->fs_info->fs_mutex);
973 trans = btrfs_start_transaction(root, 1);
974 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400975 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400976 pos, pos + hole_size, pos,
977 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400978
Chris Mason179e29e2007-11-01 11:28:41 -0400979 if (alloc_hint != EXTENT_MAP_INLINE) {
980 err = btrfs_insert_file_extent(trans, root,
981 inode->i_ino,
982 pos, 0, 0, hole_size);
983 }
Chris Mason39279cc2007-06-12 06:35:45 -0400984 btrfs_end_transaction(trans, root);
985 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400986 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 if (err)
988 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400989 }
990out:
991 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -0500992fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400993 return err;
994}
Chris Mason61295eb2008-01-14 16:24:38 -0500995
996void btrfs_drop_inode(struct inode *inode)
997{
Chris Masoncee36a02008-01-15 08:40:48 -0500998 if (!BTRFS_I(inode)->ordered_trans || inode->i_nlink) {
Chris Mason61295eb2008-01-14 16:24:38 -0500999 generic_drop_inode(inode);
1000 return;
1001 }
Chris Masoncee36a02008-01-15 08:40:48 -05001002 /* FIXME, make sure this delete actually ends up in the transaction */
1003 btrfs_del_ordered_inode(inode);
Chris Mason61295eb2008-01-14 16:24:38 -05001004 generic_drop_inode(inode);
Chris Mason61295eb2008-01-14 16:24:38 -05001005}
1006
Chris Mason39279cc2007-06-12 06:35:45 -04001007void btrfs_delete_inode(struct inode *inode)
1008{
1009 struct btrfs_trans_handle *trans;
1010 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001011 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001012 int ret;
1013
1014 truncate_inode_pages(&inode->i_data, 0);
1015 if (is_bad_inode(inode)) {
1016 goto no_delete;
1017 }
Chris Mason5f39d392007-10-15 16:14:19 -04001018
Chris Mason39279cc2007-06-12 06:35:45 -04001019 inode->i_size = 0;
1020 mutex_lock(&root->fs_info->fs_mutex);
1021 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001022
Chris Mason39279cc2007-06-12 06:35:45 -04001023 btrfs_set_trans_block_group(trans, inode);
1024 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001025 if (ret)
1026 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001027 ret = btrfs_delete_xattrs(trans, root, inode);
1028 if (ret)
1029 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001030 ret = btrfs_free_inode(trans, root, inode);
1031 if (ret)
1032 goto no_delete_lock;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001033 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001034
Chris Mason39279cc2007-06-12 06:35:45 -04001035 btrfs_end_transaction(trans, root);
1036 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001037 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001038 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001039 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001040
1041no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001042 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001043 btrfs_end_transaction(trans, root);
1044 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001045 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001046 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001047no_delete:
1048 clear_inode(inode);
1049}
1050
1051/*
1052 * this returns the key found in the dir entry in the location pointer.
1053 * If no dir entries were found, location->objectid is 0.
1054 */
1055static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1056 struct btrfs_key *location)
1057{
1058 const char *name = dentry->d_name.name;
1059 int namelen = dentry->d_name.len;
1060 struct btrfs_dir_item *di;
1061 struct btrfs_path *path;
1062 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001063 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001064
Chris Mason39544012007-12-12 14:38:19 -05001065 if (namelen == 1 && strcmp(name, ".") == 0) {
1066 location->objectid = dir->i_ino;
1067 location->type = BTRFS_INODE_ITEM_KEY;
1068 location->offset = 0;
1069 return 0;
1070 }
Chris Mason39279cc2007-06-12 06:35:45 -04001071 path = btrfs_alloc_path();
1072 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001073
Chris Mason7a720532007-12-13 09:06:59 -05001074 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001075 struct btrfs_key key;
1076 struct extent_buffer *leaf;
1077 u32 nritems;
1078 int slot;
1079
1080 key.objectid = dir->i_ino;
1081 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1082 key.offset = 0;
1083 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1084 BUG_ON(ret == 0);
1085 ret = 0;
1086
1087 leaf = path->nodes[0];
1088 slot = path->slots[0];
1089 nritems = btrfs_header_nritems(leaf);
1090 if (slot >= nritems)
1091 goto out_err;
1092
1093 btrfs_item_key_to_cpu(leaf, &key, slot);
1094 if (key.objectid != dir->i_ino ||
1095 key.type != BTRFS_INODE_REF_KEY) {
1096 goto out_err;
1097 }
1098 location->objectid = key.offset;
1099 location->type = BTRFS_INODE_ITEM_KEY;
1100 location->offset = 0;
1101 goto out;
1102 }
1103
Chris Mason39279cc2007-06-12 06:35:45 -04001104 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1105 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001106 if (IS_ERR(di))
1107 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001108 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001109 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001110 }
Chris Mason5f39d392007-10-15 16:14:19 -04001111 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001112out:
Chris Mason39279cc2007-06-12 06:35:45 -04001113 btrfs_free_path(path);
1114 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001115out_err:
1116 location->objectid = 0;
1117 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001118}
1119
1120/*
1121 * when we hit a tree root in a directory, the btrfs part of the inode
1122 * needs to be changed to reflect the root directory of the tree root. This
1123 * is kind of like crossing a mount point.
1124 */
1125static int fixup_tree_root_location(struct btrfs_root *root,
1126 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001127 struct btrfs_root **sub_root,
1128 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001129{
1130 struct btrfs_path *path;
1131 struct btrfs_root_item *ri;
1132
1133 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1134 return 0;
1135 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1136 return 0;
1137
1138 path = btrfs_alloc_path();
1139 BUG_ON(!path);
1140 mutex_lock(&root->fs_info->fs_mutex);
1141
Josef Bacik58176a92007-08-29 15:47:34 -04001142 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1143 dentry->d_name.name,
1144 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001145 if (IS_ERR(*sub_root))
1146 return PTR_ERR(*sub_root);
1147
1148 ri = &(*sub_root)->root_item;
1149 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001150 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1151 location->offset = 0;
1152
1153 btrfs_free_path(path);
1154 mutex_unlock(&root->fs_info->fs_mutex);
1155 return 0;
1156}
1157
1158static int btrfs_init_locked_inode(struct inode *inode, void *p)
1159{
1160 struct btrfs_iget_args *args = p;
1161 inode->i_ino = args->ino;
1162 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001163 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1164 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001165 return 0;
1166}
1167
1168static int btrfs_find_actor(struct inode *inode, void *opaque)
1169{
1170 struct btrfs_iget_args *args = opaque;
1171 return (args->ino == inode->i_ino &&
1172 args->root == BTRFS_I(inode)->root);
1173}
1174
Chris Masondc17ff82008-01-08 15:46:30 -05001175struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1176 u64 root_objectid)
1177{
1178 struct btrfs_iget_args args;
1179 args.ino = objectid;
1180 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1181
1182 if (!args.root)
1183 return NULL;
1184
1185 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1186}
1187
Chris Mason39279cc2007-06-12 06:35:45 -04001188struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1189 struct btrfs_root *root)
1190{
1191 struct inode *inode;
1192 struct btrfs_iget_args args;
1193 args.ino = objectid;
1194 args.root = root;
1195
1196 inode = iget5_locked(s, objectid, btrfs_find_actor,
1197 btrfs_init_locked_inode,
1198 (void *)&args);
1199 return inode;
1200}
1201
1202static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1203 struct nameidata *nd)
1204{
1205 struct inode * inode;
1206 struct btrfs_inode *bi = BTRFS_I(dir);
1207 struct btrfs_root *root = bi->root;
1208 struct btrfs_root *sub_root = root;
1209 struct btrfs_key location;
1210 int ret;
1211
1212 if (dentry->d_name.len > BTRFS_NAME_LEN)
1213 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001214
Chris Mason39279cc2007-06-12 06:35:45 -04001215 mutex_lock(&root->fs_info->fs_mutex);
1216 ret = btrfs_inode_by_name(dir, dentry, &location);
1217 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001218
Chris Mason39279cc2007-06-12 06:35:45 -04001219 if (ret < 0)
1220 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001221
Chris Mason39279cc2007-06-12 06:35:45 -04001222 inode = NULL;
1223 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001224 ret = fixup_tree_root_location(root, &location, &sub_root,
1225 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001226 if (ret < 0)
1227 return ERR_PTR(ret);
1228 if (ret > 0)
1229 return ERR_PTR(-ENOENT);
1230 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1231 sub_root);
1232 if (!inode)
1233 return ERR_PTR(-EACCES);
1234 if (inode->i_state & I_NEW) {
1235 /* the inode and parent dir are two different roots */
1236 if (sub_root != root) {
1237 igrab(inode);
1238 sub_root->inode = inode;
1239 }
1240 BTRFS_I(inode)->root = sub_root;
1241 memcpy(&BTRFS_I(inode)->location, &location,
1242 sizeof(location));
1243 btrfs_read_locked_inode(inode);
1244 unlock_new_inode(inode);
1245 }
1246 }
1247 return d_splice_alias(inode, dentry);
1248}
1249
Chris Mason39279cc2007-06-12 06:35:45 -04001250static unsigned char btrfs_filetype_table[] = {
1251 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1252};
1253
1254static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1255{
Chris Mason6da6aba2007-12-18 16:15:09 -05001256 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001257 struct btrfs_root *root = BTRFS_I(inode)->root;
1258 struct btrfs_item *item;
1259 struct btrfs_dir_item *di;
1260 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001261 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001262 struct btrfs_path *path;
1263 int ret;
1264 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001265 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001266 int slot;
1267 int advance;
1268 unsigned char d_type;
1269 int over = 0;
1270 u32 di_cur;
1271 u32 di_total;
1272 u32 di_len;
1273 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001274 char tmp_name[32];
1275 char *name_ptr;
1276 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001277
1278 /* FIXME, use a real flag for deciding about the key type */
1279 if (root->fs_info->tree_root == root)
1280 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001281
Chris Mason39544012007-12-12 14:38:19 -05001282 /* special case for "." */
1283 if (filp->f_pos == 0) {
1284 over = filldir(dirent, ".", 1,
1285 1, inode->i_ino,
1286 DT_DIR);
1287 if (over)
1288 return 0;
1289 filp->f_pos = 1;
1290 }
1291
Chris Mason39279cc2007-06-12 06:35:45 -04001292 mutex_lock(&root->fs_info->fs_mutex);
1293 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001294 path = btrfs_alloc_path();
1295 path->reada = 2;
1296
1297 /* special case for .., just use the back ref */
1298 if (filp->f_pos == 1) {
1299 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1300 key.offset = 0;
1301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1302 BUG_ON(ret == 0);
1303 leaf = path->nodes[0];
1304 slot = path->slots[0];
1305 nritems = btrfs_header_nritems(leaf);
1306 if (slot >= nritems) {
1307 btrfs_release_path(root, path);
1308 goto read_dir_items;
1309 }
1310 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1311 btrfs_release_path(root, path);
1312 if (found_key.objectid != key.objectid ||
1313 found_key.type != BTRFS_INODE_REF_KEY)
1314 goto read_dir_items;
1315 over = filldir(dirent, "..", 2,
1316 2, found_key.offset, DT_DIR);
1317 if (over)
1318 goto nopos;
1319 filp->f_pos = 2;
1320 }
1321
1322read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001323 btrfs_set_key_type(&key, key_type);
1324 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001325
Chris Mason39279cc2007-06-12 06:35:45 -04001326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1327 if (ret < 0)
1328 goto err;
1329 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001330 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001331 leaf = path->nodes[0];
1332 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001333 slot = path->slots[0];
1334 if (advance || slot >= nritems) {
1335 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001336 ret = btrfs_next_leaf(root, path);
1337 if (ret)
1338 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001339 leaf = path->nodes[0];
1340 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001341 slot = path->slots[0];
1342 } else {
1343 slot++;
1344 path->slots[0]++;
1345 }
1346 }
1347 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001348 item = btrfs_item_nr(leaf, slot);
1349 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1350
1351 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001352 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001353 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001354 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001355 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001356 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001357
1358 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001359 advance = 1;
1360 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1361 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001362 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001363 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001364 struct btrfs_key location;
1365
1366 name_len = btrfs_dir_name_len(leaf, di);
1367 if (name_len < 32) {
1368 name_ptr = tmp_name;
1369 } else {
1370 name_ptr = kmalloc(name_len, GFP_NOFS);
1371 BUG_ON(!name_ptr);
1372 }
1373 read_extent_buffer(leaf, name_ptr,
1374 (unsigned long)(di + 1), name_len);
1375
1376 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1377 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001378 over = filldir(dirent, name_ptr, name_len,
1379 found_key.offset,
1380 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001381 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001382
1383 if (name_ptr != tmp_name)
1384 kfree(name_ptr);
1385
Chris Mason39279cc2007-06-12 06:35:45 -04001386 if (over)
1387 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001388 di_len = btrfs_dir_name_len(leaf, di) +
1389 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001390 di_cur += di_len;
1391 di = (struct btrfs_dir_item *)((char *)di + di_len);
1392 }
1393 }
1394 filp->f_pos++;
1395nopos:
1396 ret = 0;
1397err:
1398 btrfs_release_path(root, path);
1399 btrfs_free_path(path);
1400 mutex_unlock(&root->fs_info->fs_mutex);
1401 return ret;
1402}
1403
1404int btrfs_write_inode(struct inode *inode, int wait)
1405{
1406 struct btrfs_root *root = BTRFS_I(inode)->root;
1407 struct btrfs_trans_handle *trans;
1408 int ret = 0;
1409
1410 if (wait) {
1411 mutex_lock(&root->fs_info->fs_mutex);
1412 trans = btrfs_start_transaction(root, 1);
1413 btrfs_set_trans_block_group(trans, inode);
1414 ret = btrfs_commit_transaction(trans, root);
1415 mutex_unlock(&root->fs_info->fs_mutex);
1416 }
1417 return ret;
1418}
1419
1420/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001421 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001422 * inode changes. But, it is most likely to find the inode in cache.
1423 * FIXME, needs more benchmarking...there are no reasons other than performance
1424 * to keep or drop this code.
1425 */
1426void btrfs_dirty_inode(struct inode *inode)
1427{
1428 struct btrfs_root *root = BTRFS_I(inode)->root;
1429 struct btrfs_trans_handle *trans;
1430
1431 mutex_lock(&root->fs_info->fs_mutex);
1432 trans = btrfs_start_transaction(root, 1);
1433 btrfs_set_trans_block_group(trans, inode);
1434 btrfs_update_inode(trans, root, inode);
1435 btrfs_end_transaction(trans, root);
1436 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001437}
1438
1439static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1441 u64 objectid,
1442 struct btrfs_block_group_cache *group,
1443 int mode)
1444{
1445 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001446 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001447 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001448 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001449 int ret;
1450 int owner;
1451
Chris Mason5f39d392007-10-15 16:14:19 -04001452 path = btrfs_alloc_path();
1453 BUG_ON(!path);
1454
Chris Mason39279cc2007-06-12 06:35:45 -04001455 inode = new_inode(root->fs_info->sb);
1456 if (!inode)
1457 return ERR_PTR(-ENOMEM);
1458
Chris Masonb888db22007-08-27 16:49:44 -04001459 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1460 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001461 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001462
Chris Mason39279cc2007-06-12 06:35:45 -04001463 if (mode & S_IFDIR)
1464 owner = 0;
1465 else
1466 owner = 1;
1467 group = btrfs_find_block_group(root, group, 0, 0, owner);
1468 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001469 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001470 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1471 if (ret)
1472 goto fail;
1473
Chris Mason39279cc2007-06-12 06:35:45 -04001474 inode->i_uid = current->fsuid;
1475 inode->i_gid = current->fsgid;
1476 inode->i_mode = mode;
1477 inode->i_ino = objectid;
1478 inode->i_blocks = 0;
1479 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001480 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1481 struct btrfs_inode_item);
1482 fill_inode_item(path->nodes[0], inode_item, inode);
1483 btrfs_mark_buffer_dirty(path->nodes[0]);
1484 btrfs_free_path(path);
1485
Chris Mason39279cc2007-06-12 06:35:45 -04001486 location = &BTRFS_I(inode)->location;
1487 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001488 location->offset = 0;
1489 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1490
Chris Mason39279cc2007-06-12 06:35:45 -04001491 insert_inode_hash(inode);
1492 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001493fail:
1494 btrfs_free_path(path);
1495 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001496}
1497
1498static inline u8 btrfs_inode_type(struct inode *inode)
1499{
1500 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1501}
1502
1503static int btrfs_add_link(struct btrfs_trans_handle *trans,
1504 struct dentry *dentry, struct inode *inode)
1505{
1506 int ret;
1507 struct btrfs_key key;
1508 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001509 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001510
Chris Mason39279cc2007-06-12 06:35:45 -04001511 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001512 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1513 key.offset = 0;
1514
1515 ret = btrfs_insert_dir_item(trans, root,
1516 dentry->d_name.name, dentry->d_name.len,
1517 dentry->d_parent->d_inode->i_ino,
1518 &key, btrfs_inode_type(inode));
1519 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001520 ret = btrfs_insert_inode_ref(trans, root,
1521 dentry->d_name.name,
1522 dentry->d_name.len,
1523 inode->i_ino,
1524 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001525 parent_inode = dentry->d_parent->d_inode;
1526 parent_inode->i_size += dentry->d_name.len * 2;
1527 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001528 ret = btrfs_update_inode(trans, root,
1529 dentry->d_parent->d_inode);
1530 }
1531 return ret;
1532}
1533
1534static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1535 struct dentry *dentry, struct inode *inode)
1536{
1537 int err = btrfs_add_link(trans, dentry, inode);
1538 if (!err) {
1539 d_instantiate(dentry, inode);
1540 return 0;
1541 }
1542 if (err > 0)
1543 err = -EEXIST;
1544 return err;
1545}
1546
Josef Bacik618e21d2007-07-11 10:18:17 -04001547static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1548 int mode, dev_t rdev)
1549{
1550 struct btrfs_trans_handle *trans;
1551 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001552 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001553 int err;
1554 int drop_inode = 0;
1555 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001556 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001557
1558 if (!new_valid_dev(rdev))
1559 return -EINVAL;
1560
1561 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001562 err = btrfs_check_free_space(root, 1, 0);
1563 if (err)
1564 goto fail;
1565
Josef Bacik618e21d2007-07-11 10:18:17 -04001566 trans = btrfs_start_transaction(root, 1);
1567 btrfs_set_trans_block_group(trans, dir);
1568
1569 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1570 if (err) {
1571 err = -ENOSPC;
1572 goto out_unlock;
1573 }
1574
1575 inode = btrfs_new_inode(trans, root, objectid,
1576 BTRFS_I(dir)->block_group, mode);
1577 err = PTR_ERR(inode);
1578 if (IS_ERR(inode))
1579 goto out_unlock;
1580
1581 btrfs_set_trans_block_group(trans, inode);
1582 err = btrfs_add_nondir(trans, dentry, inode);
1583 if (err)
1584 drop_inode = 1;
1585 else {
1586 inode->i_op = &btrfs_special_inode_operations;
1587 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001588 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001589 }
1590 dir->i_sb->s_dirt = 1;
1591 btrfs_update_inode_block_group(trans, inode);
1592 btrfs_update_inode_block_group(trans, dir);
1593out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001594 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001595 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001596fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001597 mutex_unlock(&root->fs_info->fs_mutex);
1598
1599 if (drop_inode) {
1600 inode_dec_link_count(inode);
1601 iput(inode);
1602 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001603 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001604 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001605 return err;
1606}
1607
Chris Mason39279cc2007-06-12 06:35:45 -04001608static int btrfs_create(struct inode *dir, struct dentry *dentry,
1609 int mode, struct nameidata *nd)
1610{
1611 struct btrfs_trans_handle *trans;
1612 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001613 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001614 int err;
1615 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001616 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001617 u64 objectid;
1618
1619 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001620 err = btrfs_check_free_space(root, 1, 0);
1621 if (err)
1622 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001623 trans = btrfs_start_transaction(root, 1);
1624 btrfs_set_trans_block_group(trans, dir);
1625
1626 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1627 if (err) {
1628 err = -ENOSPC;
1629 goto out_unlock;
1630 }
1631
1632 inode = btrfs_new_inode(trans, root, objectid,
1633 BTRFS_I(dir)->block_group, mode);
1634 err = PTR_ERR(inode);
1635 if (IS_ERR(inode))
1636 goto out_unlock;
1637
1638 btrfs_set_trans_block_group(trans, inode);
1639 err = btrfs_add_nondir(trans, dentry, inode);
1640 if (err)
1641 drop_inode = 1;
1642 else {
1643 inode->i_mapping->a_ops = &btrfs_aops;
1644 inode->i_fop = &btrfs_file_operations;
1645 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001646 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1647 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001648 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001649 }
1650 dir->i_sb->s_dirt = 1;
1651 btrfs_update_inode_block_group(trans, inode);
1652 btrfs_update_inode_block_group(trans, dir);
1653out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001654 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001655 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001656fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001657 mutex_unlock(&root->fs_info->fs_mutex);
1658
1659 if (drop_inode) {
1660 inode_dec_link_count(inode);
1661 iput(inode);
1662 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001663 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001664 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001665 return err;
1666}
1667
1668static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1669 struct dentry *dentry)
1670{
1671 struct btrfs_trans_handle *trans;
1672 struct btrfs_root *root = BTRFS_I(dir)->root;
1673 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001674 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001675 int err;
1676 int drop_inode = 0;
1677
1678 if (inode->i_nlink == 0)
1679 return -ENOENT;
1680
Chris Mason6da6aba2007-12-18 16:15:09 -05001681#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1682 inode->i_nlink++;
1683#else
Chris Mason39279cc2007-06-12 06:35:45 -04001684 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001685#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001686 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001687 err = btrfs_check_free_space(root, 1, 0);
1688 if (err)
1689 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001690 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001691
Chris Mason39279cc2007-06-12 06:35:45 -04001692 btrfs_set_trans_block_group(trans, dir);
1693 atomic_inc(&inode->i_count);
1694 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001695
Chris Mason39279cc2007-06-12 06:35:45 -04001696 if (err)
1697 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001698
Chris Mason39279cc2007-06-12 06:35:45 -04001699 dir->i_sb->s_dirt = 1;
1700 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001701 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001702
Chris Mason54aa1f42007-06-22 14:16:25 -04001703 if (err)
1704 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001705
Chris Masond3c2fdc2007-09-17 10:58:06 -04001706 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001707 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001708fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001709 mutex_unlock(&root->fs_info->fs_mutex);
1710
1711 if (drop_inode) {
1712 inode_dec_link_count(inode);
1713 iput(inode);
1714 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001715 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001716 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001717 return err;
1718}
1719
Chris Mason39279cc2007-06-12 06:35:45 -04001720static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1721{
1722 struct inode *inode;
1723 struct btrfs_trans_handle *trans;
1724 struct btrfs_root *root = BTRFS_I(dir)->root;
1725 int err = 0;
1726 int drop_on_err = 0;
1727 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001728 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001729
1730 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001731 err = btrfs_check_free_space(root, 1, 0);
1732 if (err)
1733 goto out_unlock;
1734
Chris Mason39279cc2007-06-12 06:35:45 -04001735 trans = btrfs_start_transaction(root, 1);
1736 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001737
Chris Mason39279cc2007-06-12 06:35:45 -04001738 if (IS_ERR(trans)) {
1739 err = PTR_ERR(trans);
1740 goto out_unlock;
1741 }
1742
1743 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1744 if (err) {
1745 err = -ENOSPC;
1746 goto out_unlock;
1747 }
1748
1749 inode = btrfs_new_inode(trans, root, objectid,
1750 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1751 if (IS_ERR(inode)) {
1752 err = PTR_ERR(inode);
1753 goto out_fail;
1754 }
Chris Mason5f39d392007-10-15 16:14:19 -04001755
Chris Mason39279cc2007-06-12 06:35:45 -04001756 drop_on_err = 1;
1757 inode->i_op = &btrfs_dir_inode_operations;
1758 inode->i_fop = &btrfs_dir_file_operations;
1759 btrfs_set_trans_block_group(trans, inode);
1760
Chris Mason39544012007-12-12 14:38:19 -05001761 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001762 err = btrfs_update_inode(trans, root, inode);
1763 if (err)
1764 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001765
Chris Mason39279cc2007-06-12 06:35:45 -04001766 err = btrfs_add_link(trans, dentry, inode);
1767 if (err)
1768 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001769
Chris Mason39279cc2007-06-12 06:35:45 -04001770 d_instantiate(dentry, inode);
1771 drop_on_err = 0;
1772 dir->i_sb->s_dirt = 1;
1773 btrfs_update_inode_block_group(trans, inode);
1774 btrfs_update_inode_block_group(trans, dir);
1775
1776out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001777 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001778 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001779
Chris Mason39279cc2007-06-12 06:35:45 -04001780out_unlock:
1781 mutex_unlock(&root->fs_info->fs_mutex);
1782 if (drop_on_err)
1783 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001784 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001785 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001786 return err;
1787}
1788
Chris Masona52d9a82007-08-27 16:49:44 -04001789struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1790 size_t page_offset, u64 start, u64 end,
1791 int create)
1792{
1793 int ret;
1794 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001795 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001796 u64 extent_start = 0;
1797 u64 extent_end = 0;
1798 u64 objectid = inode->i_ino;
1799 u32 found_type;
1800 int failed_insert = 0;
1801 struct btrfs_path *path;
1802 struct btrfs_root *root = BTRFS_I(inode)->root;
1803 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001804 struct extent_buffer *leaf;
1805 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001806 struct extent_map *em = NULL;
1807 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1808 struct btrfs_trans_handle *trans = NULL;
1809
1810 path = btrfs_alloc_path();
1811 BUG_ON(!path);
1812 mutex_lock(&root->fs_info->fs_mutex);
1813
1814again:
1815 em = lookup_extent_mapping(em_tree, start, end);
1816 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001817 if (em->start > start) {
1818 printk("get_extent start %Lu em start %Lu\n",
1819 start, em->start);
1820 WARN_ON(1);
1821 }
Chris Masona52d9a82007-08-27 16:49:44 -04001822 goto out;
1823 }
1824 if (!em) {
1825 em = alloc_extent_map(GFP_NOFS);
1826 if (!em) {
1827 err = -ENOMEM;
1828 goto out;
1829 }
Chris Mason5f39d392007-10-15 16:14:19 -04001830 em->start = EXTENT_MAP_HOLE;
1831 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001832 }
1833 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001834 ret = btrfs_lookup_file_extent(trans, root, path,
1835 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001836 if (ret < 0) {
1837 err = ret;
1838 goto out;
1839 }
1840
1841 if (ret != 0) {
1842 if (path->slots[0] == 0)
1843 goto not_found;
1844 path->slots[0]--;
1845 }
1846
Chris Mason5f39d392007-10-15 16:14:19 -04001847 leaf = path->nodes[0];
1848 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001849 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001850 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001851 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1852 found_type = btrfs_key_type(&found_key);
1853 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001854 found_type != BTRFS_EXTENT_DATA_KEY) {
1855 goto not_found;
1856 }
1857
Chris Mason5f39d392007-10-15 16:14:19 -04001858 found_type = btrfs_file_extent_type(leaf, item);
1859 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001860 if (found_type == BTRFS_FILE_EXTENT_REG) {
1861 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001862 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001863 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001864 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001865 em->start = start;
1866 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001867 if (end < extent_start)
1868 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001869 em->end = extent_end - 1;
1870 } else {
1871 em->end = end;
1872 }
1873 goto not_found_em;
1874 }
Chris Masondb945352007-10-15 16:15:53 -04001875 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1876 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001877 em->start = extent_start;
1878 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001879 em->block_start = EXTENT_MAP_HOLE;
1880 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001881 goto insert;
1882 }
Chris Masondb945352007-10-15 16:15:53 -04001883 bytenr += btrfs_file_extent_offset(leaf, item);
1884 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001885 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001886 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001887 em->start = extent_start;
1888 em->end = extent_end - 1;
1889 goto insert;
1890 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001891 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001892 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001893 size_t size;
1894 size_t extent_offset;
1895 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001896
Chris Mason5f39d392007-10-15 16:14:19 -04001897 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1898 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001899 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001900 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001901 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001902 em->start = start;
1903 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001904 if (end < extent_start)
1905 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001906 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001907 } else {
1908 em->end = end;
1909 }
1910 goto not_found_em;
1911 }
1912 em->block_start = EXTENT_MAP_INLINE;
1913 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001914
1915 if (!page) {
1916 em->start = extent_start;
1917 em->end = extent_start + size - 1;
1918 goto out;
1919 }
1920
Chris Mason35ebb932007-10-30 16:56:53 -04001921 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001922 extent_start + page_offset;
1923 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1924 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001925 em->start = extent_start + extent_offset;
1926 em->end = (em->start + copy_size -1) |
1927 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001928 map = kmap(page);
1929 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001930 if (create == 0 && !PageUptodate(page)) {
1931 read_extent_buffer(leaf, map + page_offset, ptr,
1932 copy_size);
1933 flush_dcache_page(page);
1934 } else if (create && PageUptodate(page)) {
1935 if (!trans) {
1936 kunmap(page);
1937 free_extent_map(em);
1938 em = NULL;
1939 btrfs_release_path(root, path);
1940 trans = btrfs_start_transaction(root, 1);
1941 goto again;
1942 }
1943 write_extent_buffer(leaf, map + page_offset, ptr,
1944 copy_size);
1945 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001946 }
Chris Masona52d9a82007-08-27 16:49:44 -04001947 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001948 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001949 goto insert;
1950 } else {
1951 printk("unkknown found_type %d\n", found_type);
1952 WARN_ON(1);
1953 }
1954not_found:
1955 em->start = start;
1956 em->end = end;
1957not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001958 em->block_start = EXTENT_MAP_HOLE;
1959 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001960insert:
1961 btrfs_release_path(root, path);
1962 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001963 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001964 err = -EIO;
1965 goto out;
1966 }
1967 ret = add_extent_mapping(em_tree, em);
1968 if (ret == -EEXIST) {
1969 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001970 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001971 if (0 && failed_insert == 1) {
1972 btrfs_drop_extent_cache(inode, start, end);
1973 }
Chris Masona52d9a82007-08-27 16:49:44 -04001974 failed_insert++;
1975 if (failed_insert > 5) {
1976 printk("failing to insert %Lu %Lu\n", start, end);
1977 err = -EIO;
1978 goto out;
1979 }
Chris Masona52d9a82007-08-27 16:49:44 -04001980 goto again;
1981 }
1982 err = 0;
1983out:
1984 btrfs_free_path(path);
1985 if (trans) {
1986 ret = btrfs_end_transaction(trans, root);
1987 if (!err)
1988 err = ret;
1989 }
1990 mutex_unlock(&root->fs_info->fs_mutex);
1991 if (err) {
1992 free_extent_map(em);
1993 WARN_ON(1);
1994 return ERR_PTR(err);
1995 }
1996 return em;
1997}
1998
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001999static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002000{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002001 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002002}
2003
Chris Mason9ebefb182007-06-15 13:50:00 -04002004int btrfs_readpage(struct file *file, struct page *page)
2005{
Chris Masona52d9a82007-08-27 16:49:44 -04002006 struct extent_map_tree *tree;
2007 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2008 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002009}
Chris Mason1832a6d2007-12-21 16:27:21 -05002010
Chris Mason39279cc2007-06-12 06:35:45 -04002011static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2012{
Chris Masona52d9a82007-08-27 16:49:44 -04002013 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002014
2015
2016 if (current->flags & PF_MEMALLOC) {
2017 redirty_page_for_writepage(wbc, page);
2018 unlock_page(page);
2019 return 0;
2020 }
Chris Masona52d9a82007-08-27 16:49:44 -04002021 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2022 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2023}
Chris Mason39279cc2007-06-12 06:35:45 -04002024
Chris Masonb293f022007-11-01 19:45:34 -04002025static int btrfs_writepages(struct address_space *mapping,
2026 struct writeback_control *wbc)
2027{
2028 struct extent_map_tree *tree;
2029 tree = &BTRFS_I(mapping->host)->extent_tree;
2030 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2031}
2032
Chris Mason3ab2fb52007-11-08 10:59:22 -05002033static int
2034btrfs_readpages(struct file *file, struct address_space *mapping,
2035 struct list_head *pages, unsigned nr_pages)
2036{
2037 struct extent_map_tree *tree;
2038 tree = &BTRFS_I(mapping->host)->extent_tree;
2039 return extent_readpages(tree, mapping, pages, nr_pages,
2040 btrfs_get_extent);
2041}
2042
Chris Masona52d9a82007-08-27 16:49:44 -04002043static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2044{
2045 struct extent_map_tree *tree;
2046 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002047
Chris Masona52d9a82007-08-27 16:49:44 -04002048 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2049 ret = try_release_extent_mapping(tree, page);
2050 if (ret == 1) {
2051 ClearPagePrivate(page);
2052 set_page_private(page, 0);
2053 page_cache_release(page);
2054 }
2055 return ret;
2056}
Chris Mason39279cc2007-06-12 06:35:45 -04002057
Chris Masona52d9a82007-08-27 16:49:44 -04002058static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2059{
2060 struct extent_map_tree *tree;
2061
2062 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2063 extent_invalidatepage(tree, page, offset);
2064 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002065}
2066
Chris Mason9ebefb182007-06-15 13:50:00 -04002067/*
2068 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2069 * called from a page fault handler when a page is first dirtied. Hence we must
2070 * be careful to check for EOF conditions here. We set the page up correctly
2071 * for a written page which means we get ENOSPC checking when writing into
2072 * holes and correct delalloc and unwritten extent mapping on filesystems that
2073 * support these features.
2074 *
2075 * We are not allowed to take the i_mutex here so we have to play games to
2076 * protect against truncate races as the page could now be beyond EOF. Because
2077 * vmtruncate() writes the inode size before removing pages, once we have the
2078 * page lock we can determine safely if the page is beyond EOF. If it is not
2079 * beyond EOF, then the page is guaranteed safe against truncation until we
2080 * unlock the page.
2081 */
2082int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2083{
Chris Mason6da6aba2007-12-18 16:15:09 -05002084 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002085 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002086 unsigned long end;
2087 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002088 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002089 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002090
Chris Mason1832a6d2007-12-21 16:27:21 -05002091 mutex_lock(&root->fs_info->fs_mutex);
2092 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002093 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002094 if (ret)
2095 goto out;
2096
2097 ret = -EINVAL;
2098
Chris Mason9ebefb182007-06-15 13:50:00 -04002099 lock_page(page);
2100 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002101 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002102 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002103
Chris Mason9ebefb182007-06-15 13:50:00 -04002104 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002105 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002106 /* page got truncated out from underneath us */
2107 goto out_unlock;
2108 }
2109
2110 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002111 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002112 end = size & ~PAGE_CACHE_MASK;
2113 else
2114 end = PAGE_CACHE_SIZE;
2115
Chris Masonb888db22007-08-27 16:49:44 -04002116 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002117
2118out_unlock:
2119 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002120out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002121 return ret;
2122}
2123
Chris Mason39279cc2007-06-12 06:35:45 -04002124static void btrfs_truncate(struct inode *inode)
2125{
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2127 int ret;
2128 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002129 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002130
2131 if (!S_ISREG(inode->i_mode))
2132 return;
2133 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2134 return;
2135
2136 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2137
2138 mutex_lock(&root->fs_info->fs_mutex);
2139 trans = btrfs_start_transaction(root, 1);
2140 btrfs_set_trans_block_group(trans, inode);
2141
2142 /* FIXME, add redo link to tree so we don't leak on crash */
2143 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002144 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002145 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002146
Chris Mason39279cc2007-06-12 06:35:45 -04002147 ret = btrfs_end_transaction(trans, root);
2148 BUG_ON(ret);
2149 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002150 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002151 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002152}
2153
Chris Mason4313b392008-01-03 09:08:48 -05002154static int noinline create_subvol(struct btrfs_root *root, char *name,
2155 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002156{
2157 struct btrfs_trans_handle *trans;
2158 struct btrfs_key key;
2159 struct btrfs_root_item root_item;
2160 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002161 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002162 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002163 struct inode *inode;
2164 struct inode *dir;
2165 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002166 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002167 u64 objectid;
2168 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002169 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002170
2171 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002172 ret = btrfs_check_free_space(root, 1, 0);
2173 if (ret)
2174 goto fail_commit;
2175
Chris Mason39279cc2007-06-12 06:35:45 -04002176 trans = btrfs_start_transaction(root, 1);
2177 BUG_ON(!trans);
2178
Chris Mason7bb86312007-12-11 09:25:06 -05002179 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2180 0, &objectid);
2181 if (ret)
2182 goto fail;
2183
2184 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2185 objectid, trans->transid, 0, 0,
2186 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002187 if (IS_ERR(leaf))
2188 return PTR_ERR(leaf);
2189
2190 btrfs_set_header_nritems(leaf, 0);
2191 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002192 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002193 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002194 btrfs_set_header_owner(leaf, objectid);
2195
Chris Mason5f39d392007-10-15 16:14:19 -04002196 write_extent_buffer(leaf, root->fs_info->fsid,
2197 (unsigned long)btrfs_header_fsid(leaf),
2198 BTRFS_FSID_SIZE);
2199 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002200
2201 inode_item = &root_item.inode;
2202 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002203 inode_item->generation = cpu_to_le64(1);
2204 inode_item->size = cpu_to_le64(3);
2205 inode_item->nlink = cpu_to_le32(1);
2206 inode_item->nblocks = cpu_to_le64(1);
2207 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002208
Chris Masondb945352007-10-15 16:15:53 -04002209 btrfs_set_root_bytenr(&root_item, leaf->start);
2210 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002211 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002212 btrfs_set_root_used(&root_item, 0);
2213
Chris Mason5eda7b52007-06-22 14:16:25 -04002214 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2215 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002216
2217 free_extent_buffer(leaf);
2218 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002219
Chris Mason39279cc2007-06-12 06:35:45 -04002220 btrfs_set_root_dirid(&root_item, new_dirid);
2221
2222 key.objectid = objectid;
2223 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002224 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2225 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2226 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002227 if (ret)
2228 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002229
2230 /*
2231 * insert the directory item
2232 */
2233 key.offset = (u64)-1;
2234 dir = root->fs_info->sb->s_root->d_inode;
2235 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2236 name, namelen, dir->i_ino, &key,
2237 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002238 if (ret)
2239 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002240
Chris Mason39544012007-12-12 14:38:19 -05002241 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2242 name, namelen, objectid,
2243 root->fs_info->sb->s_root->d_inode->i_ino);
2244 if (ret)
2245 goto fail;
2246
Chris Mason39279cc2007-06-12 06:35:45 -04002247 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002248 if (ret)
2249 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002250
Josef Bacik58176a92007-08-29 15:47:34 -04002251 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002252 BUG_ON(!new_root);
2253
2254 trans = btrfs_start_transaction(new_root, 1);
2255 BUG_ON(!trans);
2256
2257 inode = btrfs_new_inode(trans, new_root, new_dirid,
2258 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002259 if (IS_ERR(inode))
2260 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002261 inode->i_op = &btrfs_dir_inode_operations;
2262 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002263 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002264
Chris Mason39544012007-12-12 14:38:19 -05002265 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2266 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002267 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002268 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002269 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002270 if (ret)
2271 goto fail;
2272fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002273 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002274 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002275 if (err && !ret)
2276 ret = err;
2277fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002278 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002279 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002280 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002281 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002282}
2283
2284static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2285{
Chris Mason3063d292008-01-08 15:46:30 -05002286 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002287 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002288 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002289 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002290 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002291
2292 if (!root->ref_cows)
2293 return -EINVAL;
2294
2295 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002296 ret = btrfs_check_free_space(root, 1, 0);
2297 if (ret)
2298 goto fail_unlock;
2299
Chris Mason3063d292008-01-08 15:46:30 -05002300 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2301 if (!pending_snapshot) {
2302 ret = -ENOMEM;
2303 goto fail_unlock;
2304 }
2305 pending_snapshot->name = kstrndup(name, namelen, GFP_NOFS);
2306 if (!pending_snapshot->name) {
2307 ret = -ENOMEM;
2308 kfree(pending_snapshot);
2309 goto fail_unlock;
2310 }
Chris Mason39279cc2007-06-12 06:35:45 -04002311 trans = btrfs_start_transaction(root, 1);
2312 BUG_ON(!trans);
Chris Masondc17ff82008-01-08 15:46:30 -05002313
Chris Mason3063d292008-01-08 15:46:30 -05002314 pending_snapshot->root = root;
2315 list_add(&pending_snapshot->list,
2316 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002317 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002318 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002319
Chris Mason1832a6d2007-12-21 16:27:21 -05002320fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002321 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002322 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002323 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002324 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002325}
2326
Chris Masonedbd8d42007-12-21 16:27:24 -05002327unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002328 struct file_ra_state *ra, struct file *file,
2329 pgoff_t offset, pgoff_t last_index)
2330{
2331 pgoff_t req_size;
2332
2333#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2334 req_size = last_index - offset + 1;
2335 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2336 return offset;
2337#else
2338 req_size = min(last_index - offset + 1, (pgoff_t)128);
2339 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2340 return offset + req_size;
2341#endif
2342}
2343
2344int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002345 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002346 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason86479a02007-09-10 19:58:16 -04002347 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2348 struct page *page;
2349 unsigned long last_index;
2350 unsigned long ra_index = 0;
2351 u64 page_start;
2352 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002353 u64 delalloc_start;
2354 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002355 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002356 int ret;
2357
2358 mutex_lock(&root->fs_info->fs_mutex);
2359 ret = btrfs_check_free_space(root, inode->i_size, 0);
2360 mutex_unlock(&root->fs_info->fs_mutex);
2361 if (ret)
2362 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002363
2364 mutex_lock(&inode->i_mutex);
2365 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2366 for (i = 0; i <= last_index; i++) {
2367 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002368 ra_index = btrfs_force_ra(inode->i_mapping,
2369 &file->f_ra,
2370 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002371 }
2372 page = grab_cache_page(inode->i_mapping, i);
2373 if (!page)
2374 goto out_unlock;
2375 if (!PageUptodate(page)) {
2376 btrfs_readpage(NULL, page);
2377 lock_page(page);
2378 if (!PageUptodate(page)) {
2379 unlock_page(page);
2380 page_cache_release(page);
2381 goto out_unlock;
2382 }
2383 }
Chris Mason35ebb932007-10-30 16:56:53 -04002384 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002385 page_end = page_start + PAGE_CACHE_SIZE - 1;
2386
2387 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002388 delalloc_start = page_start;
2389 existing_delalloc =
2390 count_range_bits(&BTRFS_I(inode)->extent_tree,
2391 &delalloc_start, page_end,
2392 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Mason86479a02007-09-10 19:58:16 -04002393 set_extent_delalloc(em_tree, page_start,
2394 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002395
2396 spin_lock(&root->fs_info->delalloc_lock);
2397 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2398 existing_delalloc;
2399 spin_unlock(&root->fs_info->delalloc_lock);
2400
Chris Mason86479a02007-09-10 19:58:16 -04002401 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2402 set_page_dirty(page);
2403 unlock_page(page);
2404 page_cache_release(page);
2405 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2406 }
2407
2408out_unlock:
2409 mutex_unlock(&inode->i_mutex);
2410 return 0;
2411}
2412
Chris Masonedbd8d42007-12-21 16:27:24 -05002413static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2414{
2415 u64 new_size;
2416 u64 old_size;
2417 struct btrfs_ioctl_vol_args *vol_args;
2418 struct btrfs_trans_handle *trans;
2419 char *sizestr;
2420 int ret = 0;
2421 int namelen;
2422 int mod = 0;
2423
2424 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2425
2426 if (!vol_args)
2427 return -ENOMEM;
2428
2429 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2430 ret = -EFAULT;
2431 goto out;
2432 }
2433 namelen = strlen(vol_args->name);
2434 if (namelen > BTRFS_VOL_NAME_MAX) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
2438
2439 sizestr = vol_args->name;
2440 if (!strcmp(sizestr, "max"))
2441 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2442 else {
2443 if (sizestr[0] == '-') {
2444 mod = -1;
2445 sizestr++;
2446 } else if (sizestr[0] == '+') {
2447 mod = 1;
2448 sizestr++;
2449 }
2450 new_size = btrfs_parse_size(sizestr);
2451 if (new_size == 0) {
2452 ret = -EINVAL;
2453 goto out;
2454 }
2455 }
2456
2457 mutex_lock(&root->fs_info->fs_mutex);
2458 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2459
2460 if (mod < 0) {
2461 if (new_size > old_size) {
2462 ret = -EINVAL;
2463 goto out_unlock;
2464 }
2465 new_size = old_size - new_size;
2466 } else if (mod > 0) {
2467 new_size = old_size + new_size;
2468 }
2469
2470 if (new_size < 256 * 1024 * 1024) {
2471 ret = -EINVAL;
2472 goto out_unlock;
2473 }
2474 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2475 ret = -EFBIG;
2476 goto out_unlock;
2477 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002478
2479 do_div(new_size, root->sectorsize);
2480 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002481
2482printk("new size is %Lu\n", new_size);
2483 if (new_size > old_size) {
2484 trans = btrfs_start_transaction(root, 1);
2485 ret = btrfs_grow_extent_tree(trans, root, new_size);
2486 btrfs_commit_transaction(trans, root);
2487 } else {
2488 ret = btrfs_shrink_extent_tree(root, new_size);
2489 }
2490
2491out_unlock:
2492 mutex_unlock(&root->fs_info->fs_mutex);
2493out:
2494 kfree(vol_args);
2495 return ret;
2496}
2497
Chris Mason4313b392008-01-03 09:08:48 -05002498static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2499 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002500{
Chris Mason4aec2b52007-12-18 16:25:45 -05002501 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002502 struct btrfs_dir_item *di;
2503 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002504 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002505 int namelen;
2506 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002507
Chris Mason4aec2b52007-12-18 16:25:45 -05002508 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Chris Mason4aec2b52007-12-18 16:25:45 -05002510 if (!vol_args)
2511 return -ENOMEM;
2512
2513 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2514 ret = -EFAULT;
2515 goto out;
2516 }
2517
2518 namelen = strlen(vol_args->name);
2519 if (namelen > BTRFS_VOL_NAME_MAX) {
2520 ret = -EINVAL;
2521 goto out;
2522 }
2523 if (strchr(vol_args->name, '/')) {
2524 ret = -EINVAL;
2525 goto out;
2526 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002527
2528 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002529 if (!path) {
2530 ret = -ENOMEM;
2531 goto out;
2532 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002533
2534 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2535 mutex_lock(&root->fs_info->fs_mutex);
2536 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2537 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002538 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002539 mutex_unlock(&root->fs_info->fs_mutex);
2540 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002541
2542 if (di && !IS_ERR(di)) {
2543 ret = -EEXIST;
2544 goto out;
2545 }
2546
2547 if (IS_ERR(di)) {
2548 ret = PTR_ERR(di);
2549 goto out;
2550 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002551
2552 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002553 ret = create_subvol(root, vol_args->name, namelen);
2554 else
2555 ret = create_snapshot(root, vol_args->name, namelen);
2556out:
2557 kfree(vol_args);
2558 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002559}
2560
2561static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002562{
Chris Mason6da6aba2007-12-18 16:15:09 -05002563 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002564 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002565
2566 switch (inode->i_mode & S_IFMT) {
2567 case S_IFDIR:
2568 mutex_lock(&root->fs_info->fs_mutex);
2569 btrfs_defrag_root(root, 0);
2570 btrfs_defrag_root(root->fs_info->extent_root, 0);
2571 mutex_unlock(&root->fs_info->fs_mutex);
2572 break;
2573 case S_IFREG:
2574 btrfs_defrag_file(file);
2575 break;
2576 }
2577
2578 return 0;
2579}
2580
2581long btrfs_ioctl(struct file *file, unsigned int
2582 cmd, unsigned long arg)
2583{
Chris Mason6da6aba2007-12-18 16:15:09 -05002584 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002585
2586 switch (cmd) {
2587 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002588 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002589 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002590 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002591 case BTRFS_IOC_RESIZE:
2592 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002593 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002594
2595 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002596}
2597
Chris Mason39279cc2007-06-12 06:35:45 -04002598/*
2599 * Called inside transaction, so use GFP_NOFS
2600 */
2601struct inode *btrfs_alloc_inode(struct super_block *sb)
2602{
2603 struct btrfs_inode *ei;
2604
2605 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2606 if (!ei)
2607 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002608 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002609 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002610 return &ei->vfs_inode;
2611}
2612
2613void btrfs_destroy_inode(struct inode *inode)
2614{
2615 WARN_ON(!list_empty(&inode->i_dentry));
2616 WARN_ON(inode->i_data.nrpages);
2617
Chris Mason8c416c92008-01-14 15:10:26 -05002618 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002619 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2620}
2621
Chris Mason44ec0b72007-10-29 10:55:05 -04002622#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2623static void init_once(struct kmem_cache * cachep, void *foo)
2624#else
Chris Mason39279cc2007-06-12 06:35:45 -04002625static void init_once(void * foo, struct kmem_cache * cachep,
2626 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002627#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002628{
2629 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2630
2631 inode_init_once(&ei->vfs_inode);
2632}
2633
2634void btrfs_destroy_cachep(void)
2635{
2636 if (btrfs_inode_cachep)
2637 kmem_cache_destroy(btrfs_inode_cachep);
2638 if (btrfs_trans_handle_cachep)
2639 kmem_cache_destroy(btrfs_trans_handle_cachep);
2640 if (btrfs_transaction_cachep)
2641 kmem_cache_destroy(btrfs_transaction_cachep);
2642 if (btrfs_bit_radix_cachep)
2643 kmem_cache_destroy(btrfs_bit_radix_cachep);
2644 if (btrfs_path_cachep)
2645 kmem_cache_destroy(btrfs_path_cachep);
2646}
2647
Chris Mason86479a02007-09-10 19:58:16 -04002648struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002649 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002650#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2651 void (*ctor)(struct kmem_cache *, void *)
2652#else
Chris Mason92fee662007-07-25 12:31:35 -04002653 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002654 unsigned long)
2655#endif
2656 )
Chris Mason92fee662007-07-25 12:31:35 -04002657{
2658 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2659 SLAB_MEM_SPREAD | extra_flags), ctor
2660#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2661 ,NULL
2662#endif
2663 );
2664}
2665
Chris Mason39279cc2007-06-12 06:35:45 -04002666int btrfs_init_cachep(void)
2667{
Chris Mason86479a02007-09-10 19:58:16 -04002668 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002669 sizeof(struct btrfs_inode),
2670 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002671 if (!btrfs_inode_cachep)
2672 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002673 btrfs_trans_handle_cachep =
2674 btrfs_cache_create("btrfs_trans_handle_cache",
2675 sizeof(struct btrfs_trans_handle),
2676 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002677 if (!btrfs_trans_handle_cachep)
2678 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002679 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002680 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002681 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002682 if (!btrfs_transaction_cachep)
2683 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002684 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002685 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002686 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002687 if (!btrfs_path_cachep)
2688 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002689 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002690 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002691 if (!btrfs_bit_radix_cachep)
2692 goto fail;
2693 return 0;
2694fail:
2695 btrfs_destroy_cachep();
2696 return -ENOMEM;
2697}
2698
2699static int btrfs_getattr(struct vfsmount *mnt,
2700 struct dentry *dentry, struct kstat *stat)
2701{
2702 struct inode *inode = dentry->d_inode;
2703 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002704 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002705 return 0;
2706}
2707
2708static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2709 struct inode * new_dir,struct dentry *new_dentry)
2710{
2711 struct btrfs_trans_handle *trans;
2712 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2713 struct inode *new_inode = new_dentry->d_inode;
2714 struct inode *old_inode = old_dentry->d_inode;
2715 struct timespec ctime = CURRENT_TIME;
2716 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002717 int ret;
2718
2719 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2720 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2721 return -ENOTEMPTY;
2722 }
Chris Mason5f39d392007-10-15 16:14:19 -04002723
Chris Mason39279cc2007-06-12 06:35:45 -04002724 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002725 ret = btrfs_check_free_space(root, 1, 0);
2726 if (ret)
2727 goto out_unlock;
2728
Chris Mason39279cc2007-06-12 06:35:45 -04002729 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002730
Chris Mason39279cc2007-06-12 06:35:45 -04002731 btrfs_set_trans_block_group(trans, new_dir);
2732 path = btrfs_alloc_path();
2733 if (!path) {
2734 ret = -ENOMEM;
2735 goto out_fail;
2736 }
2737
2738 old_dentry->d_inode->i_nlink++;
2739 old_dir->i_ctime = old_dir->i_mtime = ctime;
2740 new_dir->i_ctime = new_dir->i_mtime = ctime;
2741 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002742
Chris Mason39279cc2007-06-12 06:35:45 -04002743 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2744 if (ret)
2745 goto out_fail;
2746
2747 if (new_inode) {
2748 new_inode->i_ctime = CURRENT_TIME;
2749 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2750 if (ret)
2751 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002752 }
2753 ret = btrfs_add_link(trans, new_dentry, old_inode);
2754 if (ret)
2755 goto out_fail;
2756
2757out_fail:
2758 btrfs_free_path(path);
2759 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002760out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002761 mutex_unlock(&root->fs_info->fs_mutex);
2762 return ret;
2763}
2764
2765static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2766 const char *symname)
2767{
2768 struct btrfs_trans_handle *trans;
2769 struct btrfs_root *root = BTRFS_I(dir)->root;
2770 struct btrfs_path *path;
2771 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002772 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002773 int err;
2774 int drop_inode = 0;
2775 u64 objectid;
2776 int name_len;
2777 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002778 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002779 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002780 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002781 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002782
2783 name_len = strlen(symname) + 1;
2784 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2785 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002786
Chris Mason39279cc2007-06-12 06:35:45 -04002787 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002788 err = btrfs_check_free_space(root, 1, 0);
2789 if (err)
2790 goto out_fail;
2791
Chris Mason39279cc2007-06-12 06:35:45 -04002792 trans = btrfs_start_transaction(root, 1);
2793 btrfs_set_trans_block_group(trans, dir);
2794
2795 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2796 if (err) {
2797 err = -ENOSPC;
2798 goto out_unlock;
2799 }
2800
2801 inode = btrfs_new_inode(trans, root, objectid,
2802 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2803 err = PTR_ERR(inode);
2804 if (IS_ERR(inode))
2805 goto out_unlock;
2806
2807 btrfs_set_trans_block_group(trans, inode);
2808 err = btrfs_add_nondir(trans, dentry, inode);
2809 if (err)
2810 drop_inode = 1;
2811 else {
2812 inode->i_mapping->a_ops = &btrfs_aops;
2813 inode->i_fop = &btrfs_file_operations;
2814 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002815 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2816 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002817 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002818 }
2819 dir->i_sb->s_dirt = 1;
2820 btrfs_update_inode_block_group(trans, inode);
2821 btrfs_update_inode_block_group(trans, dir);
2822 if (drop_inode)
2823 goto out_unlock;
2824
2825 path = btrfs_alloc_path();
2826 BUG_ON(!path);
2827 key.objectid = inode->i_ino;
2828 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002829 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2830 datasize = btrfs_file_extent_calc_inline_size(name_len);
2831 err = btrfs_insert_empty_item(trans, root, path, &key,
2832 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002833 if (err) {
2834 drop_inode = 1;
2835 goto out_unlock;
2836 }
Chris Mason5f39d392007-10-15 16:14:19 -04002837 leaf = path->nodes[0];
2838 ei = btrfs_item_ptr(leaf, path->slots[0],
2839 struct btrfs_file_extent_item);
2840 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2841 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002842 BTRFS_FILE_EXTENT_INLINE);
2843 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002844 write_extent_buffer(leaf, symname, ptr, name_len);
2845 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002846 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002847
Chris Mason39279cc2007-06-12 06:35:45 -04002848 inode->i_op = &btrfs_symlink_inode_operations;
2849 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2850 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002851 err = btrfs_update_inode(trans, root, inode);
2852 if (err)
2853 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002854
2855out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002856 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002857 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002858out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002859 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002860 if (drop_inode) {
2861 inode_dec_link_count(inode);
2862 iput(inode);
2863 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002864 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002865 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002866 return err;
2867}
Yanfdebe2b2008-01-14 13:26:08 -05002868static int btrfs_permission(struct inode *inode, int mask,
2869 struct nameidata *nd)
2870{
2871 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2872 return -EACCES;
2873 return generic_permission(inode, mask, NULL);
2874}
Chris Mason39279cc2007-06-12 06:35:45 -04002875
2876static struct inode_operations btrfs_dir_inode_operations = {
2877 .lookup = btrfs_lookup,
2878 .create = btrfs_create,
2879 .unlink = btrfs_unlink,
2880 .link = btrfs_link,
2881 .mkdir = btrfs_mkdir,
2882 .rmdir = btrfs_rmdir,
2883 .rename = btrfs_rename,
2884 .symlink = btrfs_symlink,
2885 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002886 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002887 .setxattr = generic_setxattr,
2888 .getxattr = generic_getxattr,
2889 .listxattr = btrfs_listxattr,
2890 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002891 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002892};
Chris Mason39279cc2007-06-12 06:35:45 -04002893static struct inode_operations btrfs_dir_ro_inode_operations = {
2894 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002895 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002896};
Chris Mason39279cc2007-06-12 06:35:45 -04002897static struct file_operations btrfs_dir_file_operations = {
2898 .llseek = generic_file_llseek,
2899 .read = generic_read_dir,
2900 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002901 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002902#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002903 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002904#endif
2905};
2906
Chris Mason07157aa2007-08-30 08:50:51 -04002907static struct extent_map_ops btrfs_extent_map_ops = {
2908 .fill_delalloc = run_delalloc_range,
2909 .writepage_io_hook = btrfs_writepage_io_hook,
2910 .readpage_io_hook = btrfs_readpage_io_hook,
2911 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2912};
2913
Chris Mason39279cc2007-06-12 06:35:45 -04002914static struct address_space_operations btrfs_aops = {
2915 .readpage = btrfs_readpage,
2916 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002917 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002918 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002919 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002920 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002921 .invalidatepage = btrfs_invalidatepage,
2922 .releasepage = btrfs_releasepage,
2923 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002924};
2925
2926static struct address_space_operations btrfs_symlink_aops = {
2927 .readpage = btrfs_readpage,
2928 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002929 .invalidatepage = btrfs_invalidatepage,
2930 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002931};
2932
2933static struct inode_operations btrfs_file_inode_operations = {
2934 .truncate = btrfs_truncate,
2935 .getattr = btrfs_getattr,
2936 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002937 .setxattr = generic_setxattr,
2938 .getxattr = generic_getxattr,
2939 .listxattr = btrfs_listxattr,
2940 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002941 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002942};
Josef Bacik618e21d2007-07-11 10:18:17 -04002943static struct inode_operations btrfs_special_inode_operations = {
2944 .getattr = btrfs_getattr,
2945 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002946 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002947};
Chris Mason39279cc2007-06-12 06:35:45 -04002948static struct inode_operations btrfs_symlink_inode_operations = {
2949 .readlink = generic_readlink,
2950 .follow_link = page_follow_link_light,
2951 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05002952 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002953};