blob: 026bc9c4c90d37a0b6a766cf48e56102c432e8ea [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 Masond1310b22008-01-24 16:13:08 -050056static struct extent_io_ops btrfs_extent_io_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 Masond1310b22008-01-24 16:13:08 -0500107 u64 orig_start = start;
108 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500109 struct btrfs_key ins;
110 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400111
Chris Masonb888db22007-08-27 16:49:44 -0400112 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400113 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 btrfs_set_trans_block_group(trans, inode);
115
Chris Masondb945352007-10-15 16:15:53 -0400116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400118 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400119 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500120 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400121
Chris Mason179e29e2007-11-01 11:28:41 -0400122 if (alloc_hint == EXTENT_MAP_INLINE)
123 goto out;
124
Chris Masonc59f8952007-12-17 20:14:04 -0500125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
129 trans->transid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
132 if (ret) {
133 WARN_ON(1);
134 goto out;
135 }
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
138 ins.offset);
Chris Mason5f564062008-01-22 16:47:59 -0500139 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400143 }
Chris Masond1310b22008-01-24 16:13:08 -0500144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500146 btrfs_add_ordered_inode(inode);
Chris Masonb888db22007-08-27 16:49:44 -0400147out:
148 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500149 return ret;
150}
151
152static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
153{
154 u64 extent_start;
155 u64 extent_end;
156 u64 bytenr;
157 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500158 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500159 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500160 struct btrfs_root *root = BTRFS_I(inode)->root;
161 struct extent_buffer *leaf;
162 int found_type;
163 struct btrfs_path *path;
164 struct btrfs_file_extent_item *item;
165 int ret;
166 int err;
167 struct btrfs_key found_key;
168
Chris Masonc31f8832008-01-08 15:46:31 -0500169 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500170 path = btrfs_alloc_path();
171 BUG_ON(!path);
172again:
173 ret = btrfs_lookup_file_extent(NULL, root, path,
174 inode->i_ino, start, 0);
175 if (ret < 0) {
176 btrfs_free_path(path);
177 return ret;
178 }
179
180 cow_end = end;
181 if (ret != 0) {
182 if (path->slots[0] == 0)
183 goto not_found;
184 path->slots[0]--;
185 }
186
187 leaf = path->nodes[0];
188 item = btrfs_item_ptr(leaf, path->slots[0],
189 struct btrfs_file_extent_item);
190
191 /* are we inside the extent that was found? */
192 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
193 found_type = btrfs_key_type(&found_key);
194 if (found_key.objectid != inode->i_ino ||
195 found_type != BTRFS_EXTENT_DATA_KEY) {
196 goto not_found;
197 }
198
199 found_type = btrfs_file_extent_type(leaf, item);
200 extent_start = found_key.offset;
201 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500202 u64 extent_num_bytes;
203
204 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
205 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500206 err = 0;
207
Chris Mason1832a6d2007-12-21 16:27:21 -0500208 if (loops && start != extent_start)
209 goto not_found;
210
Chris Masonbe20aa92007-12-17 20:14:01 -0500211 if (start < extent_start || start >= extent_end)
212 goto not_found;
213
214 cow_end = min(end, extent_end - 1);
215 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
216 if (bytenr == 0)
217 goto not_found;
218
Chris Masonc31f8832008-01-08 15:46:31 -0500219 /*
220 * we may be called by the resizer, make sure we're inside
221 * the limits of the FS
222 */
223 if (bytenr + extent_num_bytes > total_fs_bytes)
224 goto not_found;
225
Chris Masonbe20aa92007-12-17 20:14:01 -0500226 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
227 goto not_found;
228 }
229
230 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500231 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500232 goto not_found;
233 }
234loop:
235 if (start > end) {
236 btrfs_free_path(path);
237 return 0;
238 }
239 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500240 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500241 goto again;
242
243not_found:
244 cow_file_range(inode, start, cow_end);
245 start = cow_end + 1;
246 goto loop;
247}
248
249static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
250{
251 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500252 u64 num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500253 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500257 ret = run_delalloc_nocow(inode, start, end);
258 else
259 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500260
261 spin_lock(&root->fs_info->delalloc_lock);
262 num_bytes = end + 1 - start;
263 if (root->fs_info->delalloc_bytes < num_bytes) {
264 printk("delalloc accounting error total %llu sub %llu\n",
265 root->fs_info->delalloc_bytes, num_bytes);
266 } else {
267 root->fs_info->delalloc_bytes -= num_bytes;
268 }
269 spin_unlock(&root->fs_info->delalloc_lock);
270
Chris Masonb888db22007-08-27 16:49:44 -0400271 mutex_unlock(&root->fs_info->fs_mutex);
272 return ret;
273}
274
Chris Mason07157aa2007-08-30 08:50:51 -0400275int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
276{
277 struct inode *inode = page->mapping->host;
278 struct btrfs_root *root = BTRFS_I(inode)->root;
279 struct btrfs_trans_handle *trans;
280 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500281 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400282 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400283 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500284 if (btrfs_test_opt(root, NODATASUM) ||
285 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500286 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400287 mutex_lock(&root->fs_info->fs_mutex);
288 trans = btrfs_start_transaction(root, 1);
289 btrfs_set_trans_block_group(trans, inode);
290 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400291 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400292 start, kaddr + offset, end - start + 1);
293 kunmap(page);
294 ret = btrfs_end_transaction(trans, root);
295 BUG_ON(ret);
296 mutex_unlock(&root->fs_info->fs_mutex);
297 return ret;
298}
299
300int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
301{
302 int ret = 0;
303 struct inode *inode = page->mapping->host;
304 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500305 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400306 struct btrfs_csum_item *item;
307 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400308 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500309 if (btrfs_test_opt(root, NODATASUM) ||
310 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500311 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400312 mutex_lock(&root->fs_info->fs_mutex);
313 path = btrfs_alloc_path();
314 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
315 if (IS_ERR(item)) {
316 ret = PTR_ERR(item);
317 /* a csum that isn't present is a preallocated region. */
318 if (ret == -ENOENT || ret == -EFBIG)
319 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400320 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400321 goto out;
322 }
Chris Masonff79f812007-10-15 16:22:25 -0400323 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
324 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500325 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400326out:
327 if (path)
328 btrfs_free_path(path);
329 mutex_unlock(&root->fs_info->fs_mutex);
330 return ret;
331}
332
333int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
334{
Chris Mason35ebb932007-10-30 16:56:53 -0400335 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400336 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500337 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400338 char *kaddr;
339 u64 private;
340 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400341 struct btrfs_root *root = BTRFS_I(inode)->root;
342 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400343 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500344
Yanb98b6762008-01-08 15:54:37 -0500345 if (btrfs_test_opt(root, NODATASUM) ||
346 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500347 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500348
349 ret = get_state_private(io_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400350 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400351 kaddr = kmap_atomic(page, KM_IRQ0);
352 if (ret) {
353 goto zeroit;
354 }
Chris Masonff79f812007-10-15 16:22:25 -0400355 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
356 btrfs_csum_final(csum, (char *)&csum);
357 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400358 goto zeroit;
359 }
360 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400361 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400362 return 0;
363
364zeroit:
365 printk("btrfs csum failed ino %lu off %llu\n",
366 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400367 memset(kaddr + offset, 1, end - start + 1);
368 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400369 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400370 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400371 return 0;
372}
Chris Masonb888db22007-08-27 16:49:44 -0400373
Chris Mason39279cc2007-06-12 06:35:45 -0400374void btrfs_read_locked_inode(struct inode *inode)
375{
376 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400377 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400378 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400379 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400380 struct btrfs_root *root = BTRFS_I(inode)->root;
381 struct btrfs_key location;
382 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400383 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400384 int ret;
385
386 path = btrfs_alloc_path();
387 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400388 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400389 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500390
Chris Mason39279cc2007-06-12 06:35:45 -0400391 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400392 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400393 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400394
Chris Mason5f39d392007-10-15 16:14:19 -0400395 leaf = path->nodes[0];
396 inode_item = btrfs_item_ptr(leaf, path->slots[0],
397 struct btrfs_inode_item);
398
399 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
400 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
401 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
402 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
403 inode->i_size = btrfs_inode_size(leaf, inode_item);
404
405 tspec = btrfs_inode_atime(inode_item);
406 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
407 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
408
409 tspec = btrfs_inode_mtime(inode_item);
410 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
411 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
412
413 tspec = btrfs_inode_ctime(inode_item);
414 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
415 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
416
417 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
418 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400419 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400420 rdev = btrfs_inode_rdev(leaf, inode_item);
421
422 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400423 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
424 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500425 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500426 if (!BTRFS_I(inode)->block_group) {
427 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
428 NULL, 0, 0, 0);
429 }
Chris Mason39279cc2007-06-12 06:35:45 -0400430 btrfs_free_path(path);
431 inode_item = NULL;
432
433 mutex_unlock(&root->fs_info->fs_mutex);
434
435 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400436 case S_IFREG:
437 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500438 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400439 inode->i_fop = &btrfs_file_operations;
440 inode->i_op = &btrfs_file_inode_operations;
441 break;
442 case S_IFDIR:
443 inode->i_fop = &btrfs_dir_file_operations;
444 if (root == root->fs_info->tree_root)
445 inode->i_op = &btrfs_dir_ro_inode_operations;
446 else
447 inode->i_op = &btrfs_dir_inode_operations;
448 break;
449 case S_IFLNK:
450 inode->i_op = &btrfs_symlink_inode_operations;
451 inode->i_mapping->a_ops = &btrfs_symlink_aops;
452 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400453 default:
454 init_special_inode(inode, inode->i_mode, rdev);
455 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400456 }
457 return;
458
459make_bad:
460 btrfs_release_path(root, path);
461 btrfs_free_path(path);
462 mutex_unlock(&root->fs_info->fs_mutex);
463 make_bad_inode(inode);
464}
465
Chris Mason5f39d392007-10-15 16:14:19 -0400466static void fill_inode_item(struct extent_buffer *leaf,
467 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400468 struct inode *inode)
469{
Chris Mason5f39d392007-10-15 16:14:19 -0400470 btrfs_set_inode_uid(leaf, item, inode->i_uid);
471 btrfs_set_inode_gid(leaf, item, inode->i_gid);
472 btrfs_set_inode_size(leaf, item, inode->i_size);
473 btrfs_set_inode_mode(leaf, item, inode->i_mode);
474 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
475
476 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
477 inode->i_atime.tv_sec);
478 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
479 inode->i_atime.tv_nsec);
480
481 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
482 inode->i_mtime.tv_sec);
483 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
484 inode->i_mtime.tv_nsec);
485
486 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
487 inode->i_ctime.tv_sec);
488 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
489 inode->i_ctime.tv_nsec);
490
491 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
492 btrfs_set_inode_generation(leaf, item, inode->i_generation);
493 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500494 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400495 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400496 BTRFS_I(inode)->block_group->key.objectid);
497}
498
Chris Masona52d9a82007-08-27 16:49:44 -0400499int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400500 struct btrfs_root *root,
501 struct inode *inode)
502{
503 struct btrfs_inode_item *inode_item;
504 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400505 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400506 int ret;
507
508 path = btrfs_alloc_path();
509 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400510 ret = btrfs_lookup_inode(trans, root, path,
511 &BTRFS_I(inode)->location, 1);
512 if (ret) {
513 if (ret > 0)
514 ret = -ENOENT;
515 goto failed;
516 }
517
Chris Mason5f39d392007-10-15 16:14:19 -0400518 leaf = path->nodes[0];
519 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400520 struct btrfs_inode_item);
521
Chris Mason5f39d392007-10-15 16:14:19 -0400522 fill_inode_item(leaf, inode_item, inode);
523 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400524 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400525 ret = 0;
526failed:
527 btrfs_release_path(root, path);
528 btrfs_free_path(path);
529 return ret;
530}
531
532
533static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
534 struct btrfs_root *root,
535 struct inode *dir,
536 struct dentry *dentry)
537{
538 struct btrfs_path *path;
539 const char *name = dentry->d_name.name;
540 int name_len = dentry->d_name.len;
541 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400542 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400543 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400544 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400545
546 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400547 if (!path) {
548 ret = -ENOMEM;
549 goto err;
550 }
551
Chris Mason39279cc2007-06-12 06:35:45 -0400552 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
553 name, name_len, -1);
554 if (IS_ERR(di)) {
555 ret = PTR_ERR(di);
556 goto err;
557 }
558 if (!di) {
559 ret = -ENOENT;
560 goto err;
561 }
Chris Mason5f39d392007-10-15 16:14:19 -0400562 leaf = path->nodes[0];
563 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400564 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400565 if (ret)
566 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400567 btrfs_release_path(root, path);
568
569 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400570 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400571 if (IS_ERR(di)) {
572 ret = PTR_ERR(di);
573 goto err;
574 }
575 if (!di) {
576 ret = -ENOENT;
577 goto err;
578 }
579 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400580
581 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500582 ret = btrfs_del_inode_ref(trans, root, name, name_len,
583 dentry->d_inode->i_ino,
584 dentry->d_parent->d_inode->i_ino);
585 if (ret) {
586 printk("failed to delete reference to %.*s, "
587 "inode %lu parent %lu\n", name_len, name,
588 dentry->d_inode->i_ino,
589 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500590 }
Chris Mason39279cc2007-06-12 06:35:45 -0400591err:
592 btrfs_free_path(path);
593 if (!ret) {
594 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400595 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400596 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500597#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
598 dentry->d_inode->i_nlink--;
599#else
Chris Mason39279cc2007-06-12 06:35:45 -0400600 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500601#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400602 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400603 dir->i_sb->s_dirt = 1;
604 }
605 return ret;
606}
607
608static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
609{
610 struct btrfs_root *root;
611 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500612 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400613 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500614 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400615
616 root = BTRFS_I(dir)->root;
617 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500618
619 ret = btrfs_check_free_space(root, 1, 1);
620 if (ret)
621 goto fail;
622
Chris Mason39279cc2007-06-12 06:35:45 -0400623 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400624
Chris Mason39279cc2007-06-12 06:35:45 -0400625 btrfs_set_trans_block_group(trans, dir);
626 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400627 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400628
Chris Mason2da98f02008-01-16 11:44:43 -0500629 if (inode->i_nlink == 0) {
630 int found;
631 /* if the inode isn't linked anywhere,
632 * we don't need to worry about
633 * data=ordered
634 */
635 found = btrfs_del_ordered_inode(inode);
636 if (found == 1) {
637 atomic_dec(&inode->i_count);
638 }
639 }
640
Chris Mason39279cc2007-06-12 06:35:45 -0400641 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500642fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400643 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400644 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500645 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400646 return ret;
647}
648
649static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
650{
651 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500652 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400653 int ret;
654 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400655 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500656 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400657
Yan134d4512007-10-25 15:49:25 -0400658 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
659 return -ENOTEMPTY;
660
Chris Mason39279cc2007-06-12 06:35:45 -0400661 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500662 ret = btrfs_check_free_space(root, 1, 1);
663 if (ret)
664 goto fail;
665
Chris Mason39279cc2007-06-12 06:35:45 -0400666 trans = btrfs_start_transaction(root, 1);
667 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400668
669 /* now the directory is empty */
670 err = btrfs_unlink_trans(trans, root, dir, dentry);
671 if (!err) {
672 inode->i_size = 0;
673 }
Chris Mason39544012007-12-12 14:38:19 -0500674
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400675 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400676 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500677fail:
Yan134d4512007-10-25 15:49:25 -0400678 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400679 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500680 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500681
Chris Mason39279cc2007-06-12 06:35:45 -0400682 if (ret && !err)
683 err = ret;
684 return err;
685}
686
687static int btrfs_free_inode(struct btrfs_trans_handle *trans,
688 struct btrfs_root *root,
689 struct inode *inode)
690{
691 struct btrfs_path *path;
692 int ret;
693
694 clear_inode(inode);
695
696 path = btrfs_alloc_path();
697 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400698 ret = btrfs_lookup_inode(trans, root, path,
699 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400700 if (ret > 0)
701 ret = -ENOENT;
702 if (!ret)
703 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400704 btrfs_free_path(path);
705 return ret;
706}
707
708/*
Chris Mason39279cc2007-06-12 06:35:45 -0400709 * this can truncate away extent items, csum items and directory items.
710 * It starts at a high offset and removes keys until it can't find
711 * any higher than i_size.
712 *
713 * csum items that cross the new i_size are truncated to the new size
714 * as well.
715 */
716static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
717 struct btrfs_root *root,
718 struct inode *inode)
719{
720 int ret;
721 struct btrfs_path *path;
722 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400723 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400724 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400725 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400726 struct btrfs_file_extent_item *fi;
727 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400728 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400729 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500730 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500731 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400732 int found_extent;
733 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400734 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400735
Chris Masona52d9a82007-08-27 16:49:44 -0400736 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400737 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400738 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400739 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400740
Chris Mason39279cc2007-06-12 06:35:45 -0400741 /* FIXME, add redo link to tree so we don't leak on crash */
742 key.objectid = inode->i_ino;
743 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400744 key.type = (u8)-1;
745
Chris Mason39279cc2007-06-12 06:35:45 -0400746 while(1) {
747 btrfs_init_path(path);
748 fi = NULL;
749 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
750 if (ret < 0) {
751 goto error;
752 }
753 if (ret > 0) {
754 BUG_ON(path->slots[0] == 0);
755 path->slots[0]--;
756 }
Chris Mason5f39d392007-10-15 16:14:19 -0400757 leaf = path->nodes[0];
758 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
759 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400760
Chris Mason5f39d392007-10-15 16:14:19 -0400761 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400762 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400763
Chris Mason39279cc2007-06-12 06:35:45 -0400764 if (found_type != BTRFS_CSUM_ITEM_KEY &&
765 found_type != BTRFS_DIR_ITEM_KEY &&
766 found_type != BTRFS_DIR_INDEX_KEY &&
767 found_type != BTRFS_EXTENT_DATA_KEY)
768 break;
769
Chris Mason5f39d392007-10-15 16:14:19 -0400770 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400772 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400773 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400774 extent_type = btrfs_file_extent_type(leaf, fi);
775 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400776 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400777 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400778 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
779 struct btrfs_item *item = btrfs_item_nr(leaf,
780 path->slots[0]);
781 item_end += btrfs_file_extent_inline_len(leaf,
782 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400783 }
Yan008630c2007-11-07 13:31:09 -0500784 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400785 }
786 if (found_type == BTRFS_CSUM_ITEM_KEY) {
787 ret = btrfs_csum_truncate(trans, root, path,
788 inode->i_size);
789 BUG_ON(ret);
790 }
Yan008630c2007-11-07 13:31:09 -0500791 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400792 if (found_type == BTRFS_DIR_ITEM_KEY) {
793 found_type = BTRFS_INODE_ITEM_KEY;
794 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
795 found_type = BTRFS_CSUM_ITEM_KEY;
796 } else if (found_type) {
797 found_type--;
798 } else {
799 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400800 }
Yana61721d2007-09-17 11:08:38 -0400801 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400802 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400803 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400804 }
Chris Mason5f39d392007-10-15 16:14:19 -0400805 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400806 del_item = 1;
807 else
808 del_item = 0;
809 found_extent = 0;
810
811 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400812 if (found_type != BTRFS_EXTENT_DATA_KEY)
813 goto delete;
814
815 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400816 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400817 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400818 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400819 u64 orig_num_bytes =
820 btrfs_file_extent_num_bytes(leaf, fi);
821 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400822 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400823 btrfs_set_file_extent_num_bytes(leaf, fi,
824 extent_num_bytes);
825 num_dec = (orig_num_bytes -
826 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400827 if (extent_start != 0) {
828 inode->i_blocks -= num_dec;
829 }
Chris Mason5f39d392007-10-15 16:14:19 -0400830 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400831 } else {
Chris Masondb945352007-10-15 16:15:53 -0400832 extent_num_bytes =
833 btrfs_file_extent_disk_num_bytes(leaf,
834 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400835 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400836 num_dec = btrfs_file_extent_num_bytes(leaf,
837 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400838 if (extent_start != 0) {
839 found_extent = 1;
840 inode->i_blocks -= num_dec;
841 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500842 root_gen = btrfs_header_generation(leaf);
843 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400844 }
Chris Mason179e29e2007-11-01 11:28:41 -0400845 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
846 !del_item) {
847 u32 newsize = inode->i_size - found_key.offset;
848 newsize = btrfs_file_extent_calc_inline_size(newsize);
849 ret = btrfs_truncate_item(trans, root, path,
850 newsize, 1);
851 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400852 }
Chris Mason179e29e2007-11-01 11:28:41 -0400853delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400854 if (del_item) {
855 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 if (ret)
857 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400858 } else {
859 break;
860 }
861 btrfs_release_path(root, path);
862 if (found_extent) {
863 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500864 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500866 root_gen, inode->i_ino,
867 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400868 BUG_ON(ret);
869 }
870 }
871 ret = 0;
872error:
873 btrfs_release_path(root, path);
874 btrfs_free_path(path);
875 inode->i_sb->s_dirt = 1;
876 return ret;
877}
878
Chris Masonb888db22007-08-27 16:49:44 -0400879static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400880 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400881{
Chris Mason39279cc2007-06-12 06:35:45 -0400882 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500883 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500884 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400885 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400886 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500887 u64 existing_delalloc;
888 u64 delalloc_start;
889 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400890
Chris Mason190662b2007-12-18 16:25:45 -0500891 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400892 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400893
Chris Masond1310b22008-01-24 16:13:08 -0500894 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500895 delalloc_start = page_start;
Chris Masond1310b22008-01-24 16:13:08 -0500896 existing_delalloc = count_range_bits(&BTRFS_I(inode)->io_tree,
Chris Mason1832a6d2007-12-21 16:27:21 -0500897 &delalloc_start, page_end,
898 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masond1310b22008-01-24 16:13:08 -0500899 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400900 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500901
902 spin_lock(&root->fs_info->delalloc_lock);
903 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
904 spin_unlock(&root->fs_info->delalloc_lock);
905
Chris Masona52d9a82007-08-27 16:49:44 -0400906 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400907 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400908 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
909 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400910 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400911 }
Chris Masonb888db22007-08-27 16:49:44 -0400912 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500913 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400914
Chris Masona52d9a82007-08-27 16:49:44 -0400915 return ret;
916}
917
918/*
919 * taken from block_truncate_page, but does cow as it zeros out
920 * any bytes left in the last page in the file.
921 */
922static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
923{
924 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400925 struct btrfs_root *root = BTRFS_I(inode)->root;
926 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400927 pgoff_t index = from >> PAGE_CACHE_SHIFT;
928 unsigned offset = from & (PAGE_CACHE_SIZE-1);
929 struct page *page;
930 int ret = 0;
931 u64 page_start;
932
933 if ((offset & (blocksize - 1)) == 0)
934 goto out;
935
936 ret = -ENOMEM;
937 page = grab_cache_page(mapping, index);
938 if (!page)
939 goto out;
940 if (!PageUptodate(page)) {
941 ret = btrfs_readpage(NULL, page);
942 lock_page(page);
943 if (!PageUptodate(page)) {
944 ret = -EIO;
945 goto out;
946 }
947 }
Chris Mason35ebb932007-10-30 16:56:53 -0400948 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400949
Chris Masonb888db22007-08-27 16:49:44 -0400950 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400951
Chris Mason39279cc2007-06-12 06:35:45 -0400952 unlock_page(page);
953 page_cache_release(page);
954out:
955 return ret;
956}
957
958static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
959{
960 struct inode *inode = dentry->d_inode;
961 int err;
962
963 err = inode_change_ok(inode, attr);
964 if (err)
965 return err;
966
967 if (S_ISREG(inode->i_mode) &&
968 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
969 struct btrfs_trans_handle *trans;
970 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -0400972
Chris Mason5f39d392007-10-15 16:14:19 -0400973 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400974 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400975 u64 block_end = attr->ia_size | mask;
Chris Mason5f564062008-01-22 16:47:59 -0500976 u64 hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400977 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400978 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400979
980 if (attr->ia_size <= pos)
981 goto out;
982
Chris Mason5f564062008-01-22 16:47:59 -0500983 if (pos != inode->i_size)
984 hole_start = pos + root->sectorsize;
985 else
986 hole_start = pos;
987
Chris Mason1832a6d2007-12-21 16:27:21 -0500988 mutex_lock(&root->fs_info->fs_mutex);
989 err = btrfs_check_free_space(root, 1, 0);
990 mutex_unlock(&root->fs_info->fs_mutex);
991 if (err)
992 goto fail;
993
Chris Mason39279cc2007-06-12 06:35:45 -0400994 btrfs_truncate_page(inode->i_mapping, inode->i_size);
995
Chris Masond1310b22008-01-24 16:13:08 -0500996 lock_extent(io_tree, pos, block_end, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -0500997 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400998
999 mutex_lock(&root->fs_info->fs_mutex);
1000 trans = btrfs_start_transaction(root, 1);
1001 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001002 err = btrfs_drop_extents(trans, root, inode,
Chris Mason5f564062008-01-22 16:47:59 -05001003 pos, block_end, pos,
Chris Mason3326d1b2007-10-15 16:18:25 -04001004 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001005
Chris Mason179e29e2007-11-01 11:28:41 -04001006 if (alloc_hint != EXTENT_MAP_INLINE) {
1007 err = btrfs_insert_file_extent(trans, root,
1008 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001009 hole_start, 0, 0,
1010 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001011 btrfs_drop_extent_cache(inode, hole_start,
1012 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001013 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001014 }
Chris Mason39279cc2007-06-12 06:35:45 -04001015 btrfs_end_transaction(trans, root);
1016 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond1310b22008-01-24 16:13:08 -05001017 unlock_extent(io_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001018 if (err)
1019 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001020 }
1021out:
1022 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001023fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001024 return err;
1025}
Chris Mason61295eb2008-01-14 16:24:38 -05001026
Chris Mason2da98f02008-01-16 11:44:43 -05001027void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001028{
Chris Mason2da98f02008-01-16 11:44:43 -05001029 int ret;
1030
1031 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001032 return;
1033 }
Chris Mason2da98f02008-01-16 11:44:43 -05001034
1035 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1036 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1037 return;
1038
1039 ret = btrfs_del_ordered_inode(inode);
1040 if (ret == 1) {
1041 atomic_dec(&inode->i_count);
1042 }
Chris Mason61295eb2008-01-14 16:24:38 -05001043}
1044
Chris Mason39279cc2007-06-12 06:35:45 -04001045void btrfs_delete_inode(struct inode *inode)
1046{
1047 struct btrfs_trans_handle *trans;
1048 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001049 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001050 int ret;
1051
1052 truncate_inode_pages(&inode->i_data, 0);
1053 if (is_bad_inode(inode)) {
1054 goto no_delete;
1055 }
Chris Mason5f39d392007-10-15 16:14:19 -04001056
Chris Mason39279cc2007-06-12 06:35:45 -04001057 inode->i_size = 0;
1058 mutex_lock(&root->fs_info->fs_mutex);
1059 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001060
Chris Mason39279cc2007-06-12 06:35:45 -04001061 btrfs_set_trans_block_group(trans, inode);
1062 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001063 if (ret)
1064 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001065 ret = btrfs_delete_xattrs(trans, root, inode);
1066 if (ret)
1067 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001068 ret = btrfs_free_inode(trans, root, inode);
1069 if (ret)
1070 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001071 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001072
Chris Mason39279cc2007-06-12 06:35:45 -04001073 btrfs_end_transaction(trans, root);
1074 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001075 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001076 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001077 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001078
1079no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001080 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001081 btrfs_end_transaction(trans, root);
1082 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001083 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001084 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001085no_delete:
1086 clear_inode(inode);
1087}
1088
1089/*
1090 * this returns the key found in the dir entry in the location pointer.
1091 * If no dir entries were found, location->objectid is 0.
1092 */
1093static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1094 struct btrfs_key *location)
1095{
1096 const char *name = dentry->d_name.name;
1097 int namelen = dentry->d_name.len;
1098 struct btrfs_dir_item *di;
1099 struct btrfs_path *path;
1100 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001101 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001102
Chris Mason39544012007-12-12 14:38:19 -05001103 if (namelen == 1 && strcmp(name, ".") == 0) {
1104 location->objectid = dir->i_ino;
1105 location->type = BTRFS_INODE_ITEM_KEY;
1106 location->offset = 0;
1107 return 0;
1108 }
Chris Mason39279cc2007-06-12 06:35:45 -04001109 path = btrfs_alloc_path();
1110 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001111
Chris Mason7a720532007-12-13 09:06:59 -05001112 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001113 struct btrfs_key key;
1114 struct extent_buffer *leaf;
1115 u32 nritems;
1116 int slot;
1117
1118 key.objectid = dir->i_ino;
1119 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1120 key.offset = 0;
1121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1122 BUG_ON(ret == 0);
1123 ret = 0;
1124
1125 leaf = path->nodes[0];
1126 slot = path->slots[0];
1127 nritems = btrfs_header_nritems(leaf);
1128 if (slot >= nritems)
1129 goto out_err;
1130
1131 btrfs_item_key_to_cpu(leaf, &key, slot);
1132 if (key.objectid != dir->i_ino ||
1133 key.type != BTRFS_INODE_REF_KEY) {
1134 goto out_err;
1135 }
1136 location->objectid = key.offset;
1137 location->type = BTRFS_INODE_ITEM_KEY;
1138 location->offset = 0;
1139 goto out;
1140 }
1141
Chris Mason39279cc2007-06-12 06:35:45 -04001142 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1143 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001144 if (IS_ERR(di))
1145 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001146 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001147 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001148 }
Chris Mason5f39d392007-10-15 16:14:19 -04001149 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001150out:
Chris Mason39279cc2007-06-12 06:35:45 -04001151 btrfs_free_path(path);
1152 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001153out_err:
1154 location->objectid = 0;
1155 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001156}
1157
1158/*
1159 * when we hit a tree root in a directory, the btrfs part of the inode
1160 * needs to be changed to reflect the root directory of the tree root. This
1161 * is kind of like crossing a mount point.
1162 */
1163static int fixup_tree_root_location(struct btrfs_root *root,
1164 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001165 struct btrfs_root **sub_root,
1166 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001167{
1168 struct btrfs_path *path;
1169 struct btrfs_root_item *ri;
1170
1171 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1172 return 0;
1173 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1174 return 0;
1175
1176 path = btrfs_alloc_path();
1177 BUG_ON(!path);
1178 mutex_lock(&root->fs_info->fs_mutex);
1179
Josef Bacik58176a92007-08-29 15:47:34 -04001180 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1181 dentry->d_name.name,
1182 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001183 if (IS_ERR(*sub_root))
1184 return PTR_ERR(*sub_root);
1185
1186 ri = &(*sub_root)->root_item;
1187 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001188 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1189 location->offset = 0;
1190
1191 btrfs_free_path(path);
1192 mutex_unlock(&root->fs_info->fs_mutex);
1193 return 0;
1194}
1195
1196static int btrfs_init_locked_inode(struct inode *inode, void *p)
1197{
1198 struct btrfs_iget_args *args = p;
1199 inode->i_ino = args->ino;
1200 BTRFS_I(inode)->root = args->root;
Chris Masond1310b22008-01-24 16:13:08 -05001201 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1202 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001203 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001204 return 0;
1205}
1206
1207static int btrfs_find_actor(struct inode *inode, void *opaque)
1208{
1209 struct btrfs_iget_args *args = opaque;
1210 return (args->ino == inode->i_ino &&
1211 args->root == BTRFS_I(inode)->root);
1212}
1213
Chris Masondc17ff82008-01-08 15:46:30 -05001214struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1215 u64 root_objectid)
1216{
1217 struct btrfs_iget_args args;
1218 args.ino = objectid;
1219 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1220
1221 if (!args.root)
1222 return NULL;
1223
1224 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1225}
1226
Chris Mason39279cc2007-06-12 06:35:45 -04001227struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1228 struct btrfs_root *root)
1229{
1230 struct inode *inode;
1231 struct btrfs_iget_args args;
1232 args.ino = objectid;
1233 args.root = root;
1234
1235 inode = iget5_locked(s, objectid, btrfs_find_actor,
1236 btrfs_init_locked_inode,
1237 (void *)&args);
1238 return inode;
1239}
1240
1241static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1242 struct nameidata *nd)
1243{
1244 struct inode * inode;
1245 struct btrfs_inode *bi = BTRFS_I(dir);
1246 struct btrfs_root *root = bi->root;
1247 struct btrfs_root *sub_root = root;
1248 struct btrfs_key location;
1249 int ret;
1250
1251 if (dentry->d_name.len > BTRFS_NAME_LEN)
1252 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001253
Chris Mason39279cc2007-06-12 06:35:45 -04001254 mutex_lock(&root->fs_info->fs_mutex);
1255 ret = btrfs_inode_by_name(dir, dentry, &location);
1256 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001257
Chris Mason39279cc2007-06-12 06:35:45 -04001258 if (ret < 0)
1259 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001260
Chris Mason39279cc2007-06-12 06:35:45 -04001261 inode = NULL;
1262 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001263 ret = fixup_tree_root_location(root, &location, &sub_root,
1264 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001265 if (ret < 0)
1266 return ERR_PTR(ret);
1267 if (ret > 0)
1268 return ERR_PTR(-ENOENT);
1269 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1270 sub_root);
1271 if (!inode)
1272 return ERR_PTR(-EACCES);
1273 if (inode->i_state & I_NEW) {
1274 /* the inode and parent dir are two different roots */
1275 if (sub_root != root) {
1276 igrab(inode);
1277 sub_root->inode = inode;
1278 }
1279 BTRFS_I(inode)->root = sub_root;
1280 memcpy(&BTRFS_I(inode)->location, &location,
1281 sizeof(location));
1282 btrfs_read_locked_inode(inode);
1283 unlock_new_inode(inode);
1284 }
1285 }
1286 return d_splice_alias(inode, dentry);
1287}
1288
Chris Mason39279cc2007-06-12 06:35:45 -04001289static unsigned char btrfs_filetype_table[] = {
1290 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1291};
1292
1293static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1294{
Chris Mason6da6aba2007-12-18 16:15:09 -05001295 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001296 struct btrfs_root *root = BTRFS_I(inode)->root;
1297 struct btrfs_item *item;
1298 struct btrfs_dir_item *di;
1299 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001300 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001301 struct btrfs_path *path;
1302 int ret;
1303 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001304 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001305 int slot;
1306 int advance;
1307 unsigned char d_type;
1308 int over = 0;
1309 u32 di_cur;
1310 u32 di_total;
1311 u32 di_len;
1312 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001313 char tmp_name[32];
1314 char *name_ptr;
1315 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001316
1317 /* FIXME, use a real flag for deciding about the key type */
1318 if (root->fs_info->tree_root == root)
1319 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001320
Chris Mason39544012007-12-12 14:38:19 -05001321 /* special case for "." */
1322 if (filp->f_pos == 0) {
1323 over = filldir(dirent, ".", 1,
1324 1, inode->i_ino,
1325 DT_DIR);
1326 if (over)
1327 return 0;
1328 filp->f_pos = 1;
1329 }
1330
Chris Mason39279cc2007-06-12 06:35:45 -04001331 mutex_lock(&root->fs_info->fs_mutex);
1332 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001333 path = btrfs_alloc_path();
1334 path->reada = 2;
1335
1336 /* special case for .., just use the back ref */
1337 if (filp->f_pos == 1) {
1338 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1339 key.offset = 0;
1340 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1341 BUG_ON(ret == 0);
1342 leaf = path->nodes[0];
1343 slot = path->slots[0];
1344 nritems = btrfs_header_nritems(leaf);
1345 if (slot >= nritems) {
1346 btrfs_release_path(root, path);
1347 goto read_dir_items;
1348 }
1349 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1350 btrfs_release_path(root, path);
1351 if (found_key.objectid != key.objectid ||
1352 found_key.type != BTRFS_INODE_REF_KEY)
1353 goto read_dir_items;
1354 over = filldir(dirent, "..", 2,
1355 2, found_key.offset, DT_DIR);
1356 if (over)
1357 goto nopos;
1358 filp->f_pos = 2;
1359 }
1360
1361read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001362 btrfs_set_key_type(&key, key_type);
1363 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001364
Chris Mason39279cc2007-06-12 06:35:45 -04001365 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1366 if (ret < 0)
1367 goto err;
1368 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001369 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001370 leaf = path->nodes[0];
1371 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001372 slot = path->slots[0];
1373 if (advance || slot >= nritems) {
1374 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001375 ret = btrfs_next_leaf(root, path);
1376 if (ret)
1377 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001378 leaf = path->nodes[0];
1379 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001380 slot = path->slots[0];
1381 } else {
1382 slot++;
1383 path->slots[0]++;
1384 }
1385 }
1386 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001387 item = btrfs_item_nr(leaf, slot);
1388 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1389
1390 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001391 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001392 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001393 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001394 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001395 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001396
1397 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001398 advance = 1;
1399 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1400 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001401 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001402 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001403 struct btrfs_key location;
1404
1405 name_len = btrfs_dir_name_len(leaf, di);
1406 if (name_len < 32) {
1407 name_ptr = tmp_name;
1408 } else {
1409 name_ptr = kmalloc(name_len, GFP_NOFS);
1410 BUG_ON(!name_ptr);
1411 }
1412 read_extent_buffer(leaf, name_ptr,
1413 (unsigned long)(di + 1), name_len);
1414
1415 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1416 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001417 over = filldir(dirent, name_ptr, name_len,
1418 found_key.offset,
1419 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001420 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001421
1422 if (name_ptr != tmp_name)
1423 kfree(name_ptr);
1424
Chris Mason39279cc2007-06-12 06:35:45 -04001425 if (over)
1426 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001427 di_len = btrfs_dir_name_len(leaf, di) +
1428 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001429 di_cur += di_len;
1430 di = (struct btrfs_dir_item *)((char *)di + di_len);
1431 }
1432 }
Chris Masonc2a8b6e2008-01-29 09:10:26 -05001433 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
Chris Mason39279cc2007-06-12 06:35:45 -04001434nopos:
1435 ret = 0;
1436err:
1437 btrfs_release_path(root, path);
1438 btrfs_free_path(path);
1439 mutex_unlock(&root->fs_info->fs_mutex);
1440 return ret;
1441}
1442
1443int btrfs_write_inode(struct inode *inode, int wait)
1444{
1445 struct btrfs_root *root = BTRFS_I(inode)->root;
1446 struct btrfs_trans_handle *trans;
1447 int ret = 0;
1448
1449 if (wait) {
1450 mutex_lock(&root->fs_info->fs_mutex);
1451 trans = btrfs_start_transaction(root, 1);
1452 btrfs_set_trans_block_group(trans, inode);
1453 ret = btrfs_commit_transaction(trans, root);
1454 mutex_unlock(&root->fs_info->fs_mutex);
1455 }
1456 return ret;
1457}
1458
1459/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001460 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001461 * inode changes. But, it is most likely to find the inode in cache.
1462 * FIXME, needs more benchmarking...there are no reasons other than performance
1463 * to keep or drop this code.
1464 */
1465void btrfs_dirty_inode(struct inode *inode)
1466{
1467 struct btrfs_root *root = BTRFS_I(inode)->root;
1468 struct btrfs_trans_handle *trans;
1469
1470 mutex_lock(&root->fs_info->fs_mutex);
1471 trans = btrfs_start_transaction(root, 1);
1472 btrfs_set_trans_block_group(trans, inode);
1473 btrfs_update_inode(trans, root, inode);
1474 btrfs_end_transaction(trans, root);
1475 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001476}
1477
1478static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1479 struct btrfs_root *root,
1480 u64 objectid,
1481 struct btrfs_block_group_cache *group,
1482 int mode)
1483{
1484 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001485 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001486 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001487 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001488 int ret;
1489 int owner;
1490
Chris Mason5f39d392007-10-15 16:14:19 -04001491 path = btrfs_alloc_path();
1492 BUG_ON(!path);
1493
Chris Mason39279cc2007-06-12 06:35:45 -04001494 inode = new_inode(root->fs_info->sb);
1495 if (!inode)
1496 return ERR_PTR(-ENOMEM);
1497
Chris Masond1310b22008-01-24 16:13:08 -05001498 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1499 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001500 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001501 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001502
Chris Mason39279cc2007-06-12 06:35:45 -04001503 if (mode & S_IFDIR)
1504 owner = 0;
1505 else
1506 owner = 1;
1507 group = btrfs_find_block_group(root, group, 0, 0, owner);
1508 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001509 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001510 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1511 if (ret)
1512 goto fail;
1513
Chris Mason39279cc2007-06-12 06:35:45 -04001514 inode->i_uid = current->fsuid;
1515 inode->i_gid = current->fsgid;
1516 inode->i_mode = mode;
1517 inode->i_ino = objectid;
1518 inode->i_blocks = 0;
1519 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001520 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1521 struct btrfs_inode_item);
1522 fill_inode_item(path->nodes[0], inode_item, inode);
1523 btrfs_mark_buffer_dirty(path->nodes[0]);
1524 btrfs_free_path(path);
1525
Chris Mason39279cc2007-06-12 06:35:45 -04001526 location = &BTRFS_I(inode)->location;
1527 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001528 location->offset = 0;
1529 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1530
Chris Mason39279cc2007-06-12 06:35:45 -04001531 insert_inode_hash(inode);
1532 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001533fail:
1534 btrfs_free_path(path);
1535 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001536}
1537
1538static inline u8 btrfs_inode_type(struct inode *inode)
1539{
1540 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1541}
1542
1543static int btrfs_add_link(struct btrfs_trans_handle *trans,
1544 struct dentry *dentry, struct inode *inode)
1545{
1546 int ret;
1547 struct btrfs_key key;
1548 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001549 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001550
Chris Mason39279cc2007-06-12 06:35:45 -04001551 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001552 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1553 key.offset = 0;
1554
1555 ret = btrfs_insert_dir_item(trans, root,
1556 dentry->d_name.name, dentry->d_name.len,
1557 dentry->d_parent->d_inode->i_ino,
1558 &key, btrfs_inode_type(inode));
1559 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001560 ret = btrfs_insert_inode_ref(trans, root,
1561 dentry->d_name.name,
1562 dentry->d_name.len,
1563 inode->i_ino,
1564 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001565 parent_inode = dentry->d_parent->d_inode;
1566 parent_inode->i_size += dentry->d_name.len * 2;
1567 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001568 ret = btrfs_update_inode(trans, root,
1569 dentry->d_parent->d_inode);
1570 }
1571 return ret;
1572}
1573
1574static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1575 struct dentry *dentry, struct inode *inode)
1576{
1577 int err = btrfs_add_link(trans, dentry, inode);
1578 if (!err) {
1579 d_instantiate(dentry, inode);
1580 return 0;
1581 }
1582 if (err > 0)
1583 err = -EEXIST;
1584 return err;
1585}
1586
Josef Bacik618e21d2007-07-11 10:18:17 -04001587static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1588 int mode, dev_t rdev)
1589{
1590 struct btrfs_trans_handle *trans;
1591 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001592 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001593 int err;
1594 int drop_inode = 0;
1595 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001596 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001597
1598 if (!new_valid_dev(rdev))
1599 return -EINVAL;
1600
1601 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001602 err = btrfs_check_free_space(root, 1, 0);
1603 if (err)
1604 goto fail;
1605
Josef Bacik618e21d2007-07-11 10:18:17 -04001606 trans = btrfs_start_transaction(root, 1);
1607 btrfs_set_trans_block_group(trans, dir);
1608
1609 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1610 if (err) {
1611 err = -ENOSPC;
1612 goto out_unlock;
1613 }
1614
1615 inode = btrfs_new_inode(trans, root, objectid,
1616 BTRFS_I(dir)->block_group, mode);
1617 err = PTR_ERR(inode);
1618 if (IS_ERR(inode))
1619 goto out_unlock;
1620
1621 btrfs_set_trans_block_group(trans, inode);
1622 err = btrfs_add_nondir(trans, dentry, inode);
1623 if (err)
1624 drop_inode = 1;
1625 else {
1626 inode->i_op = &btrfs_special_inode_operations;
1627 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001628 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001629 }
1630 dir->i_sb->s_dirt = 1;
1631 btrfs_update_inode_block_group(trans, inode);
1632 btrfs_update_inode_block_group(trans, dir);
1633out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001634 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001635 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001636fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001637 mutex_unlock(&root->fs_info->fs_mutex);
1638
1639 if (drop_inode) {
1640 inode_dec_link_count(inode);
1641 iput(inode);
1642 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001643 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001644 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001645 return err;
1646}
1647
Chris Mason39279cc2007-06-12 06:35:45 -04001648static int btrfs_create(struct inode *dir, struct dentry *dentry,
1649 int mode, struct nameidata *nd)
1650{
1651 struct btrfs_trans_handle *trans;
1652 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001653 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001654 int err;
1655 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001656 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001657 u64 objectid;
1658
1659 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001660 err = btrfs_check_free_space(root, 1, 0);
1661 if (err)
1662 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001663 trans = btrfs_start_transaction(root, 1);
1664 btrfs_set_trans_block_group(trans, dir);
1665
1666 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1667 if (err) {
1668 err = -ENOSPC;
1669 goto out_unlock;
1670 }
1671
1672 inode = btrfs_new_inode(trans, root, objectid,
1673 BTRFS_I(dir)->block_group, mode);
1674 err = PTR_ERR(inode);
1675 if (IS_ERR(inode))
1676 goto out_unlock;
1677
1678 btrfs_set_trans_block_group(trans, inode);
1679 err = btrfs_add_nondir(trans, dentry, inode);
1680 if (err)
1681 drop_inode = 1;
1682 else {
1683 inode->i_mapping->a_ops = &btrfs_aops;
1684 inode->i_fop = &btrfs_file_operations;
1685 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001686 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1687 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001688 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001689 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001690 }
1691 dir->i_sb->s_dirt = 1;
1692 btrfs_update_inode_block_group(trans, inode);
1693 btrfs_update_inode_block_group(trans, dir);
1694out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001695 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001696 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001697fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001698 mutex_unlock(&root->fs_info->fs_mutex);
1699
1700 if (drop_inode) {
1701 inode_dec_link_count(inode);
1702 iput(inode);
1703 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001704 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001705 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001706 return err;
1707}
1708
1709static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1710 struct dentry *dentry)
1711{
1712 struct btrfs_trans_handle *trans;
1713 struct btrfs_root *root = BTRFS_I(dir)->root;
1714 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001715 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001716 int err;
1717 int drop_inode = 0;
1718
1719 if (inode->i_nlink == 0)
1720 return -ENOENT;
1721
Chris Mason6da6aba2007-12-18 16:15:09 -05001722#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1723 inode->i_nlink++;
1724#else
Chris Mason39279cc2007-06-12 06:35:45 -04001725 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001726#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001727 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001728 err = btrfs_check_free_space(root, 1, 0);
1729 if (err)
1730 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001731 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001732
Chris Mason39279cc2007-06-12 06:35:45 -04001733 btrfs_set_trans_block_group(trans, dir);
1734 atomic_inc(&inode->i_count);
1735 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001736
Chris Mason39279cc2007-06-12 06:35:45 -04001737 if (err)
1738 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001739
Chris Mason39279cc2007-06-12 06:35:45 -04001740 dir->i_sb->s_dirt = 1;
1741 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001742 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001743
Chris Mason54aa1f42007-06-22 14:16:25 -04001744 if (err)
1745 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001746
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001747 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001748 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001749fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001750 mutex_unlock(&root->fs_info->fs_mutex);
1751
1752 if (drop_inode) {
1753 inode_dec_link_count(inode);
1754 iput(inode);
1755 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001756 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001757 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001758 return err;
1759}
1760
Chris Mason39279cc2007-06-12 06:35:45 -04001761static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1762{
1763 struct inode *inode;
1764 struct btrfs_trans_handle *trans;
1765 struct btrfs_root *root = BTRFS_I(dir)->root;
1766 int err = 0;
1767 int drop_on_err = 0;
1768 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001769 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001770
1771 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001772 err = btrfs_check_free_space(root, 1, 0);
1773 if (err)
1774 goto out_unlock;
1775
Chris Mason39279cc2007-06-12 06:35:45 -04001776 trans = btrfs_start_transaction(root, 1);
1777 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001778
Chris Mason39279cc2007-06-12 06:35:45 -04001779 if (IS_ERR(trans)) {
1780 err = PTR_ERR(trans);
1781 goto out_unlock;
1782 }
1783
1784 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1785 if (err) {
1786 err = -ENOSPC;
1787 goto out_unlock;
1788 }
1789
1790 inode = btrfs_new_inode(trans, root, objectid,
1791 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1792 if (IS_ERR(inode)) {
1793 err = PTR_ERR(inode);
1794 goto out_fail;
1795 }
Chris Mason5f39d392007-10-15 16:14:19 -04001796
Chris Mason39279cc2007-06-12 06:35:45 -04001797 drop_on_err = 1;
1798 inode->i_op = &btrfs_dir_inode_operations;
1799 inode->i_fop = &btrfs_dir_file_operations;
1800 btrfs_set_trans_block_group(trans, inode);
1801
Chris Mason39544012007-12-12 14:38:19 -05001802 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001803 err = btrfs_update_inode(trans, root, inode);
1804 if (err)
1805 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001806
Chris Mason39279cc2007-06-12 06:35:45 -04001807 err = btrfs_add_link(trans, dentry, inode);
1808 if (err)
1809 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001810
Chris Mason39279cc2007-06-12 06:35:45 -04001811 d_instantiate(dentry, inode);
1812 drop_on_err = 0;
1813 dir->i_sb->s_dirt = 1;
1814 btrfs_update_inode_block_group(trans, inode);
1815 btrfs_update_inode_block_group(trans, dir);
1816
1817out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001818 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001819 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001820
Chris Mason39279cc2007-06-12 06:35:45 -04001821out_unlock:
1822 mutex_unlock(&root->fs_info->fs_mutex);
1823 if (drop_on_err)
1824 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001825 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001826 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001827 return err;
1828}
1829
Chris Masona52d9a82007-08-27 16:49:44 -04001830struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -05001831 size_t page_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001832 int create)
1833{
1834 int ret;
1835 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001836 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001837 u64 extent_start = 0;
1838 u64 extent_end = 0;
1839 u64 objectid = inode->i_ino;
1840 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001841 struct btrfs_path *path;
1842 struct btrfs_root *root = BTRFS_I(inode)->root;
1843 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001844 struct extent_buffer *leaf;
1845 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001846 struct extent_map *em = NULL;
1847 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001848 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001849 struct btrfs_trans_handle *trans = NULL;
1850
1851 path = btrfs_alloc_path();
1852 BUG_ON(!path);
1853 mutex_lock(&root->fs_info->fs_mutex);
1854
1855again:
Chris Masond1310b22008-01-24 16:13:08 -05001856 spin_lock(&em_tree->lock);
1857 em = lookup_extent_mapping(em_tree, start, len);
1858 spin_unlock(&em_tree->lock);
1859
Chris Masona52d9a82007-08-27 16:49:44 -04001860 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001861 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001862 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1863 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001864 WARN_ON(1);
1865 }
Chris Masona52d9a82007-08-27 16:49:44 -04001866 goto out;
1867 }
Chris Masond1310b22008-01-24 16:13:08 -05001868 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001869 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001870 err = -ENOMEM;
1871 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001872 }
Chris Masond1310b22008-01-24 16:13:08 -05001873
1874 em->start = EXTENT_MAP_HOLE;
1875 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001876 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001877 ret = btrfs_lookup_file_extent(trans, root, path,
1878 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001879 if (ret < 0) {
1880 err = ret;
1881 goto out;
1882 }
1883
1884 if (ret != 0) {
1885 if (path->slots[0] == 0)
1886 goto not_found;
1887 path->slots[0]--;
1888 }
1889
Chris Mason5f39d392007-10-15 16:14:19 -04001890 leaf = path->nodes[0];
1891 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001892 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001893 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001894 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1895 found_type = btrfs_key_type(&found_key);
1896 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001897 found_type != BTRFS_EXTENT_DATA_KEY) {
1898 goto not_found;
1899 }
1900
Chris Mason5f39d392007-10-15 16:14:19 -04001901 found_type = btrfs_file_extent_type(leaf, item);
1902 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001903 if (found_type == BTRFS_FILE_EXTENT_REG) {
1904 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001905 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001906 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001907 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001908 em->start = start;
1909 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001910 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001911 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001912 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001913 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001914 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001915 }
1916 goto not_found_em;
1917 }
Chris Masondb945352007-10-15 16:15:53 -04001918 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1919 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001920 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001921 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001922 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001923 goto insert;
1924 }
Chris Masondb945352007-10-15 16:15:53 -04001925 bytenr += btrfs_file_extent_offset(leaf, item);
1926 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001927 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001928 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001929 goto insert;
1930 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001931 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001932 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001933 size_t size;
1934 size_t extent_offset;
1935 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001936
Chris Mason5f39d392007-10-15 16:14:19 -04001937 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1938 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05001939 extent_end = (extent_start + size + root->sectorsize - 1) &
1940 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001941 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001942 em->start = start;
1943 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001944 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001945 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001946 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001947 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001948 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001949 }
1950 goto not_found_em;
1951 }
1952 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001953
1954 if (!page) {
1955 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001956 em->len = size;
Yan689f9342007-10-29 11:41:07 -04001957 goto out;
1958 }
1959
Chris Mason35ebb932007-10-30 16:56:53 -04001960 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001961 extent_start + page_offset;
1962 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1963 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001964 em->start = extent_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05001965 em->len = copy_size;
Yan689f9342007-10-29 11:41:07 -04001966 map = kmap(page);
1967 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001968 if (create == 0 && !PageUptodate(page)) {
1969 read_extent_buffer(leaf, map + page_offset, ptr,
1970 copy_size);
1971 flush_dcache_page(page);
1972 } else if (create && PageUptodate(page)) {
1973 if (!trans) {
1974 kunmap(page);
1975 free_extent_map(em);
1976 em = NULL;
1977 btrfs_release_path(root, path);
1978 trans = btrfs_start_transaction(root, 1);
1979 goto again;
1980 }
1981 write_extent_buffer(leaf, map + page_offset, ptr,
1982 copy_size);
1983 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001984 }
Chris Masona52d9a82007-08-27 16:49:44 -04001985 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05001986 set_extent_uptodate(io_tree, em->start,
1987 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001988 goto insert;
1989 } else {
1990 printk("unkknown found_type %d\n", found_type);
1991 WARN_ON(1);
1992 }
1993not_found:
1994 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05001995 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001996not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001997 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001998insert:
1999 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002000 if (em->start > start || extent_map_end(em) <= start) {
2001 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002002 err = -EIO;
2003 goto out;
2004 }
Chris Masond1310b22008-01-24 16:13:08 -05002005
2006 err = 0;
2007 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002008 ret = add_extent_mapping(em_tree, em);
2009 if (ret == -EEXIST) {
2010 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002011 em = lookup_extent_mapping(em_tree, start, len);
2012 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002013 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002014 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002015 }
Chris Masona52d9a82007-08-27 16:49:44 -04002016 }
Chris Masond1310b22008-01-24 16:13:08 -05002017 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002018out:
2019 btrfs_free_path(path);
2020 if (trans) {
2021 ret = btrfs_end_transaction(trans, root);
2022 if (!err)
2023 err = ret;
2024 }
2025 mutex_unlock(&root->fs_info->fs_mutex);
2026 if (err) {
2027 free_extent_map(em);
2028 WARN_ON(1);
2029 return ERR_PTR(err);
2030 }
2031 return em;
2032}
2033
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002034static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002035{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002036 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002037}
2038
Chris Mason9ebefb182007-06-15 13:50:00 -04002039int btrfs_readpage(struct file *file, struct page *page)
2040{
Chris Masond1310b22008-01-24 16:13:08 -05002041 struct extent_io_tree *tree;
2042 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002043 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002044}
Chris Mason1832a6d2007-12-21 16:27:21 -05002045
Chris Mason39279cc2007-06-12 06:35:45 -04002046static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2047{
Chris Masond1310b22008-01-24 16:13:08 -05002048 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002049
2050
2051 if (current->flags & PF_MEMALLOC) {
2052 redirty_page_for_writepage(wbc, page);
2053 unlock_page(page);
2054 return 0;
2055 }
Chris Masond1310b22008-01-24 16:13:08 -05002056 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002057 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2058}
Chris Mason39279cc2007-06-12 06:35:45 -04002059
Chris Masonb293f022007-11-01 19:45:34 -04002060static int btrfs_writepages(struct address_space *mapping,
2061 struct writeback_control *wbc)
2062{
Chris Masond1310b22008-01-24 16:13:08 -05002063 struct extent_io_tree *tree;
2064 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002065 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2066}
2067
Chris Mason3ab2fb52007-11-08 10:59:22 -05002068static int
2069btrfs_readpages(struct file *file, struct address_space *mapping,
2070 struct list_head *pages, unsigned nr_pages)
2071{
Chris Masond1310b22008-01-24 16:13:08 -05002072 struct extent_io_tree *tree;
2073 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002074 return extent_readpages(tree, mapping, pages, nr_pages,
2075 btrfs_get_extent);
2076}
2077
Chris Masona52d9a82007-08-27 16:49:44 -04002078static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2079{
Chris Masond1310b22008-01-24 16:13:08 -05002080 struct extent_io_tree *tree;
2081 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002082 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002083
Chris Masond1310b22008-01-24 16:13:08 -05002084 tree = &BTRFS_I(page->mapping->host)->io_tree;
2085 map = &BTRFS_I(page->mapping->host)->extent_tree;
2086 ret = try_release_extent_mapping(map, tree, page);
Chris Masona52d9a82007-08-27 16:49:44 -04002087 if (ret == 1) {
2088 ClearPagePrivate(page);
2089 set_page_private(page, 0);
2090 page_cache_release(page);
2091 }
2092 return ret;
2093}
Chris Mason39279cc2007-06-12 06:35:45 -04002094
Chris Masona52d9a82007-08-27 16:49:44 -04002095static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2096{
Chris Masond1310b22008-01-24 16:13:08 -05002097 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002098
Chris Masond1310b22008-01-24 16:13:08 -05002099 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002100 extent_invalidatepage(tree, page, offset);
2101 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002102}
2103
Chris Mason9ebefb182007-06-15 13:50:00 -04002104/*
2105 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2106 * called from a page fault handler when a page is first dirtied. Hence we must
2107 * be careful to check for EOF conditions here. We set the page up correctly
2108 * for a written page which means we get ENOSPC checking when writing into
2109 * holes and correct delalloc and unwritten extent mapping on filesystems that
2110 * support these features.
2111 *
2112 * We are not allowed to take the i_mutex here so we have to play games to
2113 * protect against truncate races as the page could now be beyond EOF. Because
2114 * vmtruncate() writes the inode size before removing pages, once we have the
2115 * page lock we can determine safely if the page is beyond EOF. If it is not
2116 * beyond EOF, then the page is guaranteed safe against truncation until we
2117 * unlock the page.
2118 */
2119int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2120{
Chris Mason6da6aba2007-12-18 16:15:09 -05002121 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002122 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002123 unsigned long end;
2124 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002125 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002126 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002127
Chris Mason1832a6d2007-12-21 16:27:21 -05002128 mutex_lock(&root->fs_info->fs_mutex);
2129 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002130 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002131 if (ret)
2132 goto out;
2133
2134 ret = -EINVAL;
2135
Chris Mason9ebefb182007-06-15 13:50:00 -04002136 lock_page(page);
2137 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002138 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002139 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002140
Chris Mason9ebefb182007-06-15 13:50:00 -04002141 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002142 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002143 /* page got truncated out from underneath us */
2144 goto out_unlock;
2145 }
2146
2147 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002148 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002149 end = size & ~PAGE_CACHE_MASK;
2150 else
2151 end = PAGE_CACHE_SIZE;
2152
Chris Masonb888db22007-08-27 16:49:44 -04002153 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002154
2155out_unlock:
2156 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002157out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002158 return ret;
2159}
2160
Chris Mason39279cc2007-06-12 06:35:45 -04002161static void btrfs_truncate(struct inode *inode)
2162{
2163 struct btrfs_root *root = BTRFS_I(inode)->root;
2164 int ret;
2165 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002166 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002167
2168 if (!S_ISREG(inode->i_mode))
2169 return;
2170 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2171 return;
2172
2173 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2174
2175 mutex_lock(&root->fs_info->fs_mutex);
2176 trans = btrfs_start_transaction(root, 1);
2177 btrfs_set_trans_block_group(trans, inode);
2178
2179 /* FIXME, add redo link to tree so we don't leak on crash */
2180 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002181 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002182 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002183
Chris Mason39279cc2007-06-12 06:35:45 -04002184 ret = btrfs_end_transaction(trans, root);
2185 BUG_ON(ret);
2186 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002187 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002188 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002189}
2190
Chris Mason4313b392008-01-03 09:08:48 -05002191static int noinline create_subvol(struct btrfs_root *root, char *name,
2192 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002193{
2194 struct btrfs_trans_handle *trans;
2195 struct btrfs_key key;
2196 struct btrfs_root_item root_item;
2197 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002198 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002199 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002200 struct inode *inode;
2201 struct inode *dir;
2202 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002203 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002204 u64 objectid;
2205 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002206 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002207
2208 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002209 ret = btrfs_check_free_space(root, 1, 0);
2210 if (ret)
2211 goto fail_commit;
2212
Chris Mason39279cc2007-06-12 06:35:45 -04002213 trans = btrfs_start_transaction(root, 1);
2214 BUG_ON(!trans);
2215
Chris Mason7bb86312007-12-11 09:25:06 -05002216 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2217 0, &objectid);
2218 if (ret)
2219 goto fail;
2220
2221 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2222 objectid, trans->transid, 0, 0,
2223 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002224 if (IS_ERR(leaf))
2225 return PTR_ERR(leaf);
2226
2227 btrfs_set_header_nritems(leaf, 0);
2228 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002229 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002230 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002231 btrfs_set_header_owner(leaf, objectid);
2232
Chris Mason5f39d392007-10-15 16:14:19 -04002233 write_extent_buffer(leaf, root->fs_info->fsid,
2234 (unsigned long)btrfs_header_fsid(leaf),
2235 BTRFS_FSID_SIZE);
2236 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002237
2238 inode_item = &root_item.inode;
2239 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002240 inode_item->generation = cpu_to_le64(1);
2241 inode_item->size = cpu_to_le64(3);
2242 inode_item->nlink = cpu_to_le32(1);
2243 inode_item->nblocks = cpu_to_le64(1);
2244 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002245
Chris Masondb945352007-10-15 16:15:53 -04002246 btrfs_set_root_bytenr(&root_item, leaf->start);
2247 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002248 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002249 btrfs_set_root_used(&root_item, 0);
2250
Chris Mason5eda7b52007-06-22 14:16:25 -04002251 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2252 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002253
2254 free_extent_buffer(leaf);
2255 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002256
Chris Mason39279cc2007-06-12 06:35:45 -04002257 btrfs_set_root_dirid(&root_item, new_dirid);
2258
2259 key.objectid = objectid;
2260 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002261 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2262 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2263 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002264 if (ret)
2265 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002266
2267 /*
2268 * insert the directory item
2269 */
2270 key.offset = (u64)-1;
2271 dir = root->fs_info->sb->s_root->d_inode;
2272 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2273 name, namelen, dir->i_ino, &key,
2274 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002275 if (ret)
2276 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002277
Chris Mason39544012007-12-12 14:38:19 -05002278 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2279 name, namelen, objectid,
2280 root->fs_info->sb->s_root->d_inode->i_ino);
2281 if (ret)
2282 goto fail;
2283
Chris Mason39279cc2007-06-12 06:35:45 -04002284 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002285 if (ret)
2286 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002287
Josef Bacik58176a92007-08-29 15:47:34 -04002288 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002289 BUG_ON(!new_root);
2290
2291 trans = btrfs_start_transaction(new_root, 1);
2292 BUG_ON(!trans);
2293
2294 inode = btrfs_new_inode(trans, new_root, new_dirid,
2295 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002296 if (IS_ERR(inode))
2297 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002298 inode->i_op = &btrfs_dir_inode_operations;
2299 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002300 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002301
Chris Mason39544012007-12-12 14:38:19 -05002302 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2303 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002304 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002305 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002306 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002307 if (ret)
2308 goto fail;
2309fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002310 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002311 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002312 if (err && !ret)
2313 ret = err;
2314fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002315 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002316 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002317 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002318 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002319}
2320
2321static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2322{
Chris Mason3063d292008-01-08 15:46:30 -05002323 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002324 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002325 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002326 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002327 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002328
2329 if (!root->ref_cows)
2330 return -EINVAL;
2331
2332 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002333 ret = btrfs_check_free_space(root, 1, 0);
2334 if (ret)
2335 goto fail_unlock;
2336
Chris Mason3063d292008-01-08 15:46:30 -05002337 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2338 if (!pending_snapshot) {
2339 ret = -ENOMEM;
2340 goto fail_unlock;
2341 }
Yanfb4bc1e2008-01-17 11:59:51 -05002342 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002343 if (!pending_snapshot->name) {
2344 ret = -ENOMEM;
2345 kfree(pending_snapshot);
2346 goto fail_unlock;
2347 }
Yanfb4bc1e2008-01-17 11:59:51 -05002348 memcpy(pending_snapshot->name, name, namelen);
2349 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002350 trans = btrfs_start_transaction(root, 1);
2351 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002352 pending_snapshot->root = root;
2353 list_add(&pending_snapshot->list,
2354 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002355 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002356 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002357
Chris Mason1832a6d2007-12-21 16:27:21 -05002358fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002359 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002360 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002361 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002362 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002363}
2364
Chris Masonedbd8d42007-12-21 16:27:24 -05002365unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002366 struct file_ra_state *ra, struct file *file,
2367 pgoff_t offset, pgoff_t last_index)
2368{
2369 pgoff_t req_size;
2370
2371#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2372 req_size = last_index - offset + 1;
2373 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2374 return offset;
2375#else
2376 req_size = min(last_index - offset + 1, (pgoff_t)128);
2377 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2378 return offset + req_size;
2379#endif
2380}
2381
2382int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002383 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002384 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002385 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002386 struct page *page;
2387 unsigned long last_index;
2388 unsigned long ra_index = 0;
2389 u64 page_start;
2390 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002391 u64 delalloc_start;
2392 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002393 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002394 int ret;
2395
2396 mutex_lock(&root->fs_info->fs_mutex);
2397 ret = btrfs_check_free_space(root, inode->i_size, 0);
2398 mutex_unlock(&root->fs_info->fs_mutex);
2399 if (ret)
2400 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002401
2402 mutex_lock(&inode->i_mutex);
2403 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2404 for (i = 0; i <= last_index; i++) {
2405 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002406 ra_index = btrfs_force_ra(inode->i_mapping,
2407 &file->f_ra,
2408 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002409 }
2410 page = grab_cache_page(inode->i_mapping, i);
2411 if (!page)
2412 goto out_unlock;
2413 if (!PageUptodate(page)) {
2414 btrfs_readpage(NULL, page);
2415 lock_page(page);
2416 if (!PageUptodate(page)) {
2417 unlock_page(page);
2418 page_cache_release(page);
2419 goto out_unlock;
2420 }
2421 }
Chris Mason35ebb932007-10-30 16:56:53 -04002422 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002423 page_end = page_start + PAGE_CACHE_SIZE - 1;
2424
Chris Masond1310b22008-01-24 16:13:08 -05002425 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002426 delalloc_start = page_start;
2427 existing_delalloc =
Chris Masond1310b22008-01-24 16:13:08 -05002428 count_range_bits(&BTRFS_I(inode)->io_tree,
Chris Masonedbd8d42007-12-21 16:27:24 -05002429 &delalloc_start, page_end,
2430 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masond1310b22008-01-24 16:13:08 -05002431 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002432 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002433
2434 spin_lock(&root->fs_info->delalloc_lock);
2435 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2436 existing_delalloc;
2437 spin_unlock(&root->fs_info->delalloc_lock);
2438
Chris Masond1310b22008-01-24 16:13:08 -05002439 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002440 set_page_dirty(page);
2441 unlock_page(page);
2442 page_cache_release(page);
2443 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2444 }
2445
2446out_unlock:
2447 mutex_unlock(&inode->i_mutex);
2448 return 0;
2449}
2450
Chris Masonedbd8d42007-12-21 16:27:24 -05002451static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2452{
2453 u64 new_size;
2454 u64 old_size;
2455 struct btrfs_ioctl_vol_args *vol_args;
2456 struct btrfs_trans_handle *trans;
2457 char *sizestr;
2458 int ret = 0;
2459 int namelen;
2460 int mod = 0;
2461
2462 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2463
2464 if (!vol_args)
2465 return -ENOMEM;
2466
2467 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2468 ret = -EFAULT;
2469 goto out;
2470 }
2471 namelen = strlen(vol_args->name);
2472 if (namelen > BTRFS_VOL_NAME_MAX) {
2473 ret = -EINVAL;
2474 goto out;
2475 }
2476
2477 sizestr = vol_args->name;
2478 if (!strcmp(sizestr, "max"))
2479 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2480 else {
2481 if (sizestr[0] == '-') {
2482 mod = -1;
2483 sizestr++;
2484 } else if (sizestr[0] == '+') {
2485 mod = 1;
2486 sizestr++;
2487 }
2488 new_size = btrfs_parse_size(sizestr);
2489 if (new_size == 0) {
2490 ret = -EINVAL;
2491 goto out;
2492 }
2493 }
2494
2495 mutex_lock(&root->fs_info->fs_mutex);
2496 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2497
2498 if (mod < 0) {
2499 if (new_size > old_size) {
2500 ret = -EINVAL;
2501 goto out_unlock;
2502 }
2503 new_size = old_size - new_size;
2504 } else if (mod > 0) {
2505 new_size = old_size + new_size;
2506 }
2507
2508 if (new_size < 256 * 1024 * 1024) {
2509 ret = -EINVAL;
2510 goto out_unlock;
2511 }
2512 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2513 ret = -EFBIG;
2514 goto out_unlock;
2515 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002516
2517 do_div(new_size, root->sectorsize);
2518 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002519
2520printk("new size is %Lu\n", new_size);
2521 if (new_size > old_size) {
2522 trans = btrfs_start_transaction(root, 1);
2523 ret = btrfs_grow_extent_tree(trans, root, new_size);
2524 btrfs_commit_transaction(trans, root);
2525 } else {
2526 ret = btrfs_shrink_extent_tree(root, new_size);
2527 }
2528
2529out_unlock:
2530 mutex_unlock(&root->fs_info->fs_mutex);
2531out:
2532 kfree(vol_args);
2533 return ret;
2534}
2535
Chris Mason4313b392008-01-03 09:08:48 -05002536static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2537 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002538{
Chris Mason4aec2b52007-12-18 16:25:45 -05002539 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002540 struct btrfs_dir_item *di;
2541 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002542 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002543 int namelen;
2544 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002545
Chris Mason4aec2b52007-12-18 16:25:45 -05002546 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002547
Chris Mason4aec2b52007-12-18 16:25:45 -05002548 if (!vol_args)
2549 return -ENOMEM;
2550
2551 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2552 ret = -EFAULT;
2553 goto out;
2554 }
2555
2556 namelen = strlen(vol_args->name);
2557 if (namelen > BTRFS_VOL_NAME_MAX) {
2558 ret = -EINVAL;
2559 goto out;
2560 }
2561 if (strchr(vol_args->name, '/')) {
2562 ret = -EINVAL;
2563 goto out;
2564 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002565
2566 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002567 if (!path) {
2568 ret = -ENOMEM;
2569 goto out;
2570 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002571
2572 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2573 mutex_lock(&root->fs_info->fs_mutex);
2574 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2575 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002576 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002577 mutex_unlock(&root->fs_info->fs_mutex);
2578 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002579
2580 if (di && !IS_ERR(di)) {
2581 ret = -EEXIST;
2582 goto out;
2583 }
2584
2585 if (IS_ERR(di)) {
2586 ret = PTR_ERR(di);
2587 goto out;
2588 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002589
2590 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002591 ret = create_subvol(root, vol_args->name, namelen);
2592 else
2593 ret = create_snapshot(root, vol_args->name, namelen);
2594out:
2595 kfree(vol_args);
2596 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002597}
2598
2599static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002600{
Chris Mason6da6aba2007-12-18 16:15:09 -05002601 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002602 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002603
2604 switch (inode->i_mode & S_IFMT) {
2605 case S_IFDIR:
2606 mutex_lock(&root->fs_info->fs_mutex);
2607 btrfs_defrag_root(root, 0);
2608 btrfs_defrag_root(root->fs_info->extent_root, 0);
2609 mutex_unlock(&root->fs_info->fs_mutex);
2610 break;
2611 case S_IFREG:
2612 btrfs_defrag_file(file);
2613 break;
2614 }
2615
2616 return 0;
2617}
2618
2619long btrfs_ioctl(struct file *file, unsigned int
2620 cmd, unsigned long arg)
2621{
Chris Mason6da6aba2007-12-18 16:15:09 -05002622 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002623
2624 switch (cmd) {
2625 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002626 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002627 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002628 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002629 case BTRFS_IOC_RESIZE:
2630 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002631 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002632
2633 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002634}
2635
Chris Mason39279cc2007-06-12 06:35:45 -04002636/*
2637 * Called inside transaction, so use GFP_NOFS
2638 */
2639struct inode *btrfs_alloc_inode(struct super_block *sb)
2640{
2641 struct btrfs_inode *ei;
2642
2643 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2644 if (!ei)
2645 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002646 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002647 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002648 return &ei->vfs_inode;
2649}
2650
2651void btrfs_destroy_inode(struct inode *inode)
2652{
2653 WARN_ON(!list_empty(&inode->i_dentry));
2654 WARN_ON(inode->i_data.nrpages);
2655
Chris Mason8c416c92008-01-14 15:10:26 -05002656 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002657 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2658}
2659
Chris Mason44ec0b72007-10-29 10:55:05 -04002660#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2661static void init_once(struct kmem_cache * cachep, void *foo)
2662#else
Chris Mason39279cc2007-06-12 06:35:45 -04002663static void init_once(void * foo, struct kmem_cache * cachep,
2664 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002665#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002666{
2667 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2668
2669 inode_init_once(&ei->vfs_inode);
2670}
2671
2672void btrfs_destroy_cachep(void)
2673{
2674 if (btrfs_inode_cachep)
2675 kmem_cache_destroy(btrfs_inode_cachep);
2676 if (btrfs_trans_handle_cachep)
2677 kmem_cache_destroy(btrfs_trans_handle_cachep);
2678 if (btrfs_transaction_cachep)
2679 kmem_cache_destroy(btrfs_transaction_cachep);
2680 if (btrfs_bit_radix_cachep)
2681 kmem_cache_destroy(btrfs_bit_radix_cachep);
2682 if (btrfs_path_cachep)
2683 kmem_cache_destroy(btrfs_path_cachep);
2684}
2685
Chris Mason86479a02007-09-10 19:58:16 -04002686struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002687 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002688#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2689 void (*ctor)(struct kmem_cache *, void *)
2690#else
Chris Mason92fee662007-07-25 12:31:35 -04002691 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002692 unsigned long)
2693#endif
2694 )
Chris Mason92fee662007-07-25 12:31:35 -04002695{
2696 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2697 SLAB_MEM_SPREAD | extra_flags), ctor
2698#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2699 ,NULL
2700#endif
2701 );
2702}
2703
Chris Mason39279cc2007-06-12 06:35:45 -04002704int btrfs_init_cachep(void)
2705{
Chris Mason86479a02007-09-10 19:58:16 -04002706 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002707 sizeof(struct btrfs_inode),
2708 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002709 if (!btrfs_inode_cachep)
2710 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002711 btrfs_trans_handle_cachep =
2712 btrfs_cache_create("btrfs_trans_handle_cache",
2713 sizeof(struct btrfs_trans_handle),
2714 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002715 if (!btrfs_trans_handle_cachep)
2716 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002717 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002718 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002719 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002720 if (!btrfs_transaction_cachep)
2721 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002722 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002723 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002724 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002725 if (!btrfs_path_cachep)
2726 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002727 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002728 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002729 if (!btrfs_bit_radix_cachep)
2730 goto fail;
2731 return 0;
2732fail:
2733 btrfs_destroy_cachep();
2734 return -ENOMEM;
2735}
2736
2737static int btrfs_getattr(struct vfsmount *mnt,
2738 struct dentry *dentry, struct kstat *stat)
2739{
2740 struct inode *inode = dentry->d_inode;
2741 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002742 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002743 return 0;
2744}
2745
2746static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2747 struct inode * new_dir,struct dentry *new_dentry)
2748{
2749 struct btrfs_trans_handle *trans;
2750 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2751 struct inode *new_inode = new_dentry->d_inode;
2752 struct inode *old_inode = old_dentry->d_inode;
2753 struct timespec ctime = CURRENT_TIME;
2754 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002755 int ret;
2756
2757 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2758 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2759 return -ENOTEMPTY;
2760 }
Chris Mason5f39d392007-10-15 16:14:19 -04002761
Chris Mason39279cc2007-06-12 06:35:45 -04002762 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002763 ret = btrfs_check_free_space(root, 1, 0);
2764 if (ret)
2765 goto out_unlock;
2766
Chris Mason39279cc2007-06-12 06:35:45 -04002767 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002768
Chris Mason39279cc2007-06-12 06:35:45 -04002769 btrfs_set_trans_block_group(trans, new_dir);
2770 path = btrfs_alloc_path();
2771 if (!path) {
2772 ret = -ENOMEM;
2773 goto out_fail;
2774 }
2775
2776 old_dentry->d_inode->i_nlink++;
2777 old_dir->i_ctime = old_dir->i_mtime = ctime;
2778 new_dir->i_ctime = new_dir->i_mtime = ctime;
2779 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002780
Chris Mason39279cc2007-06-12 06:35:45 -04002781 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2782 if (ret)
2783 goto out_fail;
2784
2785 if (new_inode) {
2786 new_inode->i_ctime = CURRENT_TIME;
2787 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2788 if (ret)
2789 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002790 }
2791 ret = btrfs_add_link(trans, new_dentry, old_inode);
2792 if (ret)
2793 goto out_fail;
2794
2795out_fail:
2796 btrfs_free_path(path);
2797 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002798out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002799 mutex_unlock(&root->fs_info->fs_mutex);
2800 return ret;
2801}
2802
2803static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2804 const char *symname)
2805{
2806 struct btrfs_trans_handle *trans;
2807 struct btrfs_root *root = BTRFS_I(dir)->root;
2808 struct btrfs_path *path;
2809 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002810 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002811 int err;
2812 int drop_inode = 0;
2813 u64 objectid;
2814 int name_len;
2815 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002816 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002817 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002818 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002819 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002820
2821 name_len = strlen(symname) + 1;
2822 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2823 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002824
Chris Mason39279cc2007-06-12 06:35:45 -04002825 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002826 err = btrfs_check_free_space(root, 1, 0);
2827 if (err)
2828 goto out_fail;
2829
Chris Mason39279cc2007-06-12 06:35:45 -04002830 trans = btrfs_start_transaction(root, 1);
2831 btrfs_set_trans_block_group(trans, dir);
2832
2833 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2834 if (err) {
2835 err = -ENOSPC;
2836 goto out_unlock;
2837 }
2838
2839 inode = btrfs_new_inode(trans, root, objectid,
2840 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2841 err = PTR_ERR(inode);
2842 if (IS_ERR(inode))
2843 goto out_unlock;
2844
2845 btrfs_set_trans_block_group(trans, inode);
2846 err = btrfs_add_nondir(trans, dentry, inode);
2847 if (err)
2848 drop_inode = 1;
2849 else {
2850 inode->i_mapping->a_ops = &btrfs_aops;
2851 inode->i_fop = &btrfs_file_operations;
2852 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002853 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2854 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002855 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002856 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002857 }
2858 dir->i_sb->s_dirt = 1;
2859 btrfs_update_inode_block_group(trans, inode);
2860 btrfs_update_inode_block_group(trans, dir);
2861 if (drop_inode)
2862 goto out_unlock;
2863
2864 path = btrfs_alloc_path();
2865 BUG_ON(!path);
2866 key.objectid = inode->i_ino;
2867 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002868 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2869 datasize = btrfs_file_extent_calc_inline_size(name_len);
2870 err = btrfs_insert_empty_item(trans, root, path, &key,
2871 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002872 if (err) {
2873 drop_inode = 1;
2874 goto out_unlock;
2875 }
Chris Mason5f39d392007-10-15 16:14:19 -04002876 leaf = path->nodes[0];
2877 ei = btrfs_item_ptr(leaf, path->slots[0],
2878 struct btrfs_file_extent_item);
2879 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2880 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002881 BTRFS_FILE_EXTENT_INLINE);
2882 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002883 write_extent_buffer(leaf, symname, ptr, name_len);
2884 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002885 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002886
Chris Mason39279cc2007-06-12 06:35:45 -04002887 inode->i_op = &btrfs_symlink_inode_operations;
2888 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2889 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002890 err = btrfs_update_inode(trans, root, inode);
2891 if (err)
2892 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002893
2894out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002895 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002896 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002897out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002898 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002899 if (drop_inode) {
2900 inode_dec_link_count(inode);
2901 iput(inode);
2902 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002903 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002904 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002905 return err;
2906}
Yanfdebe2b2008-01-14 13:26:08 -05002907static int btrfs_permission(struct inode *inode, int mask,
2908 struct nameidata *nd)
2909{
2910 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2911 return -EACCES;
2912 return generic_permission(inode, mask, NULL);
2913}
Chris Mason39279cc2007-06-12 06:35:45 -04002914
2915static struct inode_operations btrfs_dir_inode_operations = {
2916 .lookup = btrfs_lookup,
2917 .create = btrfs_create,
2918 .unlink = btrfs_unlink,
2919 .link = btrfs_link,
2920 .mkdir = btrfs_mkdir,
2921 .rmdir = btrfs_rmdir,
2922 .rename = btrfs_rename,
2923 .symlink = btrfs_symlink,
2924 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002925 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002926 .setxattr = generic_setxattr,
2927 .getxattr = generic_getxattr,
2928 .listxattr = btrfs_listxattr,
2929 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002930 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002931};
Chris Mason39279cc2007-06-12 06:35:45 -04002932static struct inode_operations btrfs_dir_ro_inode_operations = {
2933 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002934 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002935};
Chris Mason39279cc2007-06-12 06:35:45 -04002936static struct file_operations btrfs_dir_file_operations = {
2937 .llseek = generic_file_llseek,
2938 .read = generic_read_dir,
2939 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002940 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002941#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002942 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002943#endif
2944};
2945
Chris Masond1310b22008-01-24 16:13:08 -05002946static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04002947 .fill_delalloc = run_delalloc_range,
2948 .writepage_io_hook = btrfs_writepage_io_hook,
2949 .readpage_io_hook = btrfs_readpage_io_hook,
2950 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2951};
2952
Chris Mason39279cc2007-06-12 06:35:45 -04002953static struct address_space_operations btrfs_aops = {
2954 .readpage = btrfs_readpage,
2955 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002956 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002957 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002958 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002959 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002960 .invalidatepage = btrfs_invalidatepage,
2961 .releasepage = btrfs_releasepage,
2962 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002963};
2964
2965static struct address_space_operations btrfs_symlink_aops = {
2966 .readpage = btrfs_readpage,
2967 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002968 .invalidatepage = btrfs_invalidatepage,
2969 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002970};
2971
2972static struct inode_operations btrfs_file_inode_operations = {
2973 .truncate = btrfs_truncate,
2974 .getattr = btrfs_getattr,
2975 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002976 .setxattr = generic_setxattr,
2977 .getxattr = generic_getxattr,
2978 .listxattr = btrfs_listxattr,
2979 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002980 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002981};
Josef Bacik618e21d2007-07-11 10:18:17 -04002982static struct inode_operations btrfs_special_inode_operations = {
2983 .getattr = btrfs_getattr,
2984 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002985 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002986};
Chris Mason39279cc2007-06-12 06:35:45 -04002987static struct inode_operations btrfs_symlink_inode_operations = {
2988 .readlink = generic_readlink,
2989 .follow_link = page_follow_link_light,
2990 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05002991 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002992};