blob: 484551274100f6828fcd910d860b0c2af7cb9f8d [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 Masonaadfeb62008-01-29 09:10:27 -0500321 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400322 goto out;
323 }
Chris Masonff79f812007-10-15 16:22:25 -0400324 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
325 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500326 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400327out:
328 if (path)
329 btrfs_free_path(path);
330 mutex_unlock(&root->fs_info->fs_mutex);
331 return ret;
332}
333
334int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
335{
Chris Mason35ebb932007-10-30 16:56:53 -0400336 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400337 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500338 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400339 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500340 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400341 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400344 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500345
Yanb98b6762008-01-08 15:54:37 -0500346 if (btrfs_test_opt(root, NODATASUM) ||
347 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500348 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500349
350 ret = get_state_private(io_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400351 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400352 kaddr = kmap_atomic(page, KM_IRQ0);
353 if (ret) {
354 goto zeroit;
355 }
Chris Masonff79f812007-10-15 16:22:25 -0400356 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
357 btrfs_csum_final(csum, (char *)&csum);
358 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400359 goto zeroit;
360 }
361 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400362 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400363 return 0;
364
365zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500366 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
367 page->mapping->host->i_ino, (unsigned long long)start, csum,
368 private);
Chris Masondb945352007-10-15 16:15:53 -0400369 memset(kaddr + offset, 1, end - start + 1);
370 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400371 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400372 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400373 return 0;
374}
Chris Masonb888db22007-08-27 16:49:44 -0400375
Chris Mason39279cc2007-06-12 06:35:45 -0400376void btrfs_read_locked_inode(struct inode *inode)
377{
378 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400379 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400380 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400381 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400382 struct btrfs_root *root = BTRFS_I(inode)->root;
383 struct btrfs_key location;
384 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400385 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400386 int ret;
387
388 path = btrfs_alloc_path();
389 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400390 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400391 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500392
Chris Mason39279cc2007-06-12 06:35:45 -0400393 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400394 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400395 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400396
Chris Mason5f39d392007-10-15 16:14:19 -0400397 leaf = path->nodes[0];
398 inode_item = btrfs_item_ptr(leaf, path->slots[0],
399 struct btrfs_inode_item);
400
401 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
402 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
403 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
404 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
405 inode->i_size = btrfs_inode_size(leaf, inode_item);
406
407 tspec = btrfs_inode_atime(inode_item);
408 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
409 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
410
411 tspec = btrfs_inode_mtime(inode_item);
412 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
413 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
414
415 tspec = btrfs_inode_ctime(inode_item);
416 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
417 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
418
419 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
420 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400421 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400422 rdev = btrfs_inode_rdev(leaf, inode_item);
423
424 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400425 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
426 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500427 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500428 if (!BTRFS_I(inode)->block_group) {
429 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
430 NULL, 0, 0, 0);
431 }
Chris Mason39279cc2007-06-12 06:35:45 -0400432 btrfs_free_path(path);
433 inode_item = NULL;
434
435 mutex_unlock(&root->fs_info->fs_mutex);
436
437 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400438 case S_IFREG:
439 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500440 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400441 inode->i_fop = &btrfs_file_operations;
442 inode->i_op = &btrfs_file_inode_operations;
443 break;
444 case S_IFDIR:
445 inode->i_fop = &btrfs_dir_file_operations;
446 if (root == root->fs_info->tree_root)
447 inode->i_op = &btrfs_dir_ro_inode_operations;
448 else
449 inode->i_op = &btrfs_dir_inode_operations;
450 break;
451 case S_IFLNK:
452 inode->i_op = &btrfs_symlink_inode_operations;
453 inode->i_mapping->a_ops = &btrfs_symlink_aops;
454 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400455 default:
456 init_special_inode(inode, inode->i_mode, rdev);
457 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400458 }
459 return;
460
461make_bad:
462 btrfs_release_path(root, path);
463 btrfs_free_path(path);
464 mutex_unlock(&root->fs_info->fs_mutex);
465 make_bad_inode(inode);
466}
467
Chris Mason5f39d392007-10-15 16:14:19 -0400468static void fill_inode_item(struct extent_buffer *leaf,
469 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400470 struct inode *inode)
471{
Chris Mason5f39d392007-10-15 16:14:19 -0400472 btrfs_set_inode_uid(leaf, item, inode->i_uid);
473 btrfs_set_inode_gid(leaf, item, inode->i_gid);
474 btrfs_set_inode_size(leaf, item, inode->i_size);
475 btrfs_set_inode_mode(leaf, item, inode->i_mode);
476 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
477
478 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
479 inode->i_atime.tv_sec);
480 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
481 inode->i_atime.tv_nsec);
482
483 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
484 inode->i_mtime.tv_sec);
485 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
486 inode->i_mtime.tv_nsec);
487
488 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
489 inode->i_ctime.tv_sec);
490 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
491 inode->i_ctime.tv_nsec);
492
493 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
494 btrfs_set_inode_generation(leaf, item, inode->i_generation);
495 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500496 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400497 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400498 BTRFS_I(inode)->block_group->key.objectid);
499}
500
Chris Masona52d9a82007-08-27 16:49:44 -0400501int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400502 struct btrfs_root *root,
503 struct inode *inode)
504{
505 struct btrfs_inode_item *inode_item;
506 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400507 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400508 int ret;
509
510 path = btrfs_alloc_path();
511 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400512 ret = btrfs_lookup_inode(trans, root, path,
513 &BTRFS_I(inode)->location, 1);
514 if (ret) {
515 if (ret > 0)
516 ret = -ENOENT;
517 goto failed;
518 }
519
Chris Mason5f39d392007-10-15 16:14:19 -0400520 leaf = path->nodes[0];
521 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400522 struct btrfs_inode_item);
523
Chris Mason5f39d392007-10-15 16:14:19 -0400524 fill_inode_item(leaf, inode_item, inode);
525 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400526 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400527 ret = 0;
528failed:
529 btrfs_release_path(root, path);
530 btrfs_free_path(path);
531 return ret;
532}
533
534
535static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
536 struct btrfs_root *root,
537 struct inode *dir,
538 struct dentry *dentry)
539{
540 struct btrfs_path *path;
541 const char *name = dentry->d_name.name;
542 int name_len = dentry->d_name.len;
543 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400544 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400545 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400546 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400547
548 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400549 if (!path) {
550 ret = -ENOMEM;
551 goto err;
552 }
553
Chris Mason39279cc2007-06-12 06:35:45 -0400554 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
555 name, name_len, -1);
556 if (IS_ERR(di)) {
557 ret = PTR_ERR(di);
558 goto err;
559 }
560 if (!di) {
561 ret = -ENOENT;
562 goto err;
563 }
Chris Mason5f39d392007-10-15 16:14:19 -0400564 leaf = path->nodes[0];
565 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400566 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400567 if (ret)
568 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400569 btrfs_release_path(root, path);
570
571 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400572 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400573 if (IS_ERR(di)) {
574 ret = PTR_ERR(di);
575 goto err;
576 }
577 if (!di) {
578 ret = -ENOENT;
579 goto err;
580 }
581 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400582
583 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500584 ret = btrfs_del_inode_ref(trans, root, name, name_len,
585 dentry->d_inode->i_ino,
586 dentry->d_parent->d_inode->i_ino);
587 if (ret) {
588 printk("failed to delete reference to %.*s, "
589 "inode %lu parent %lu\n", name_len, name,
590 dentry->d_inode->i_ino,
591 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500592 }
Chris Mason39279cc2007-06-12 06:35:45 -0400593err:
594 btrfs_free_path(path);
595 if (!ret) {
596 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400597 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400598 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500599#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
600 dentry->d_inode->i_nlink--;
601#else
Chris Mason39279cc2007-06-12 06:35:45 -0400602 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500603#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400604 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400605 dir->i_sb->s_dirt = 1;
606 }
607 return ret;
608}
609
610static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
611{
612 struct btrfs_root *root;
613 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500614 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400615 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500616 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400617
618 root = BTRFS_I(dir)->root;
619 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500620
621 ret = btrfs_check_free_space(root, 1, 1);
622 if (ret)
623 goto fail;
624
Chris Mason39279cc2007-06-12 06:35:45 -0400625 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400626
Chris Mason39279cc2007-06-12 06:35:45 -0400627 btrfs_set_trans_block_group(trans, dir);
628 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400629 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400630
Chris Mason2da98f02008-01-16 11:44:43 -0500631 if (inode->i_nlink == 0) {
632 int found;
633 /* if the inode isn't linked anywhere,
634 * we don't need to worry about
635 * data=ordered
636 */
637 found = btrfs_del_ordered_inode(inode);
638 if (found == 1) {
639 atomic_dec(&inode->i_count);
640 }
641 }
642
Chris Mason39279cc2007-06-12 06:35:45 -0400643 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500644fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400645 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400646 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500647 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400648 return ret;
649}
650
651static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
652{
653 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500654 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400655 int ret;
656 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400657 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500658 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400659
Yan134d4512007-10-25 15:49:25 -0400660 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
661 return -ENOTEMPTY;
662
Chris Mason39279cc2007-06-12 06:35:45 -0400663 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500664 ret = btrfs_check_free_space(root, 1, 1);
665 if (ret)
666 goto fail;
667
Chris Mason39279cc2007-06-12 06:35:45 -0400668 trans = btrfs_start_transaction(root, 1);
669 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400670
671 /* now the directory is empty */
672 err = btrfs_unlink_trans(trans, root, dir, dentry);
673 if (!err) {
674 inode->i_size = 0;
675 }
Chris Mason39544012007-12-12 14:38:19 -0500676
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400677 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400678 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500679fail:
Yan134d4512007-10-25 15:49:25 -0400680 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400681 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500682 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500683
Chris Mason39279cc2007-06-12 06:35:45 -0400684 if (ret && !err)
685 err = ret;
686 return err;
687}
688
689static int btrfs_free_inode(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root,
691 struct inode *inode)
692{
693 struct btrfs_path *path;
694 int ret;
695
696 clear_inode(inode);
697
698 path = btrfs_alloc_path();
699 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400700 ret = btrfs_lookup_inode(trans, root, path,
701 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400702 if (ret > 0)
703 ret = -ENOENT;
704 if (!ret)
705 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400706 btrfs_free_path(path);
707 return ret;
708}
709
710/*
Chris Mason39279cc2007-06-12 06:35:45 -0400711 * this can truncate away extent items, csum items and directory items.
712 * It starts at a high offset and removes keys until it can't find
713 * any higher than i_size.
714 *
715 * csum items that cross the new i_size are truncated to the new size
716 * as well.
717 */
718static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
719 struct btrfs_root *root,
720 struct inode *inode)
721{
722 int ret;
723 struct btrfs_path *path;
724 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400725 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400726 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400727 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400728 struct btrfs_file_extent_item *fi;
729 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400730 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400731 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500732 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500733 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400734 int found_extent;
735 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400736 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400737
Chris Masona52d9a82007-08-27 16:49:44 -0400738 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400739 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400740 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400741 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400742
Chris Mason39279cc2007-06-12 06:35:45 -0400743 /* FIXME, add redo link to tree so we don't leak on crash */
744 key.objectid = inode->i_ino;
745 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400746 key.type = (u8)-1;
747
Chris Mason39279cc2007-06-12 06:35:45 -0400748 while(1) {
749 btrfs_init_path(path);
750 fi = NULL;
751 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
752 if (ret < 0) {
753 goto error;
754 }
755 if (ret > 0) {
756 BUG_ON(path->slots[0] == 0);
757 path->slots[0]--;
758 }
Chris Mason5f39d392007-10-15 16:14:19 -0400759 leaf = path->nodes[0];
760 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
761 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400762
Chris Mason5f39d392007-10-15 16:14:19 -0400763 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400764 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400765
Chris Mason39279cc2007-06-12 06:35:45 -0400766 if (found_type != BTRFS_CSUM_ITEM_KEY &&
767 found_type != BTRFS_DIR_ITEM_KEY &&
768 found_type != BTRFS_DIR_INDEX_KEY &&
769 found_type != BTRFS_EXTENT_DATA_KEY)
770 break;
771
Chris Mason5f39d392007-10-15 16:14:19 -0400772 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400773 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400774 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400775 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400776 extent_type = btrfs_file_extent_type(leaf, fi);
777 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400778 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400779 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400780 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
781 struct btrfs_item *item = btrfs_item_nr(leaf,
782 path->slots[0]);
783 item_end += btrfs_file_extent_inline_len(leaf,
784 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400785 }
Yan008630c2007-11-07 13:31:09 -0500786 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400787 }
788 if (found_type == BTRFS_CSUM_ITEM_KEY) {
789 ret = btrfs_csum_truncate(trans, root, path,
790 inode->i_size);
791 BUG_ON(ret);
792 }
Yan008630c2007-11-07 13:31:09 -0500793 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400794 if (found_type == BTRFS_DIR_ITEM_KEY) {
795 found_type = BTRFS_INODE_ITEM_KEY;
796 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
797 found_type = BTRFS_CSUM_ITEM_KEY;
798 } else if (found_type) {
799 found_type--;
800 } else {
801 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400802 }
Yana61721d2007-09-17 11:08:38 -0400803 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400804 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400805 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400806 }
Chris Mason5f39d392007-10-15 16:14:19 -0400807 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400808 del_item = 1;
809 else
810 del_item = 0;
811 found_extent = 0;
812
813 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400814 if (found_type != BTRFS_EXTENT_DATA_KEY)
815 goto delete;
816
817 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400818 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400819 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400820 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400821 u64 orig_num_bytes =
822 btrfs_file_extent_num_bytes(leaf, fi);
823 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400824 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400825 btrfs_set_file_extent_num_bytes(leaf, fi,
826 extent_num_bytes);
827 num_dec = (orig_num_bytes -
828 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400829 if (extent_start != 0) {
830 inode->i_blocks -= num_dec;
831 }
Chris Mason5f39d392007-10-15 16:14:19 -0400832 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400833 } else {
Chris Masondb945352007-10-15 16:15:53 -0400834 extent_num_bytes =
835 btrfs_file_extent_disk_num_bytes(leaf,
836 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400837 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400838 num_dec = btrfs_file_extent_num_bytes(leaf,
839 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400840 if (extent_start != 0) {
841 found_extent = 1;
842 inode->i_blocks -= num_dec;
843 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500844 root_gen = btrfs_header_generation(leaf);
845 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400846 }
Chris Mason179e29e2007-11-01 11:28:41 -0400847 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
848 !del_item) {
849 u32 newsize = inode->i_size - found_key.offset;
850 newsize = btrfs_file_extent_calc_inline_size(newsize);
851 ret = btrfs_truncate_item(trans, root, path,
852 newsize, 1);
853 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400854 }
Chris Mason179e29e2007-11-01 11:28:41 -0400855delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400856 if (del_item) {
857 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400858 if (ret)
859 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400860 } else {
861 break;
862 }
863 btrfs_release_path(root, path);
864 if (found_extent) {
865 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500866 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500867 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500868 root_gen, inode->i_ino,
869 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400870 BUG_ON(ret);
871 }
872 }
873 ret = 0;
874error:
875 btrfs_release_path(root, path);
876 btrfs_free_path(path);
877 inode->i_sb->s_dirt = 1;
878 return ret;
879}
880
Chris Masonb888db22007-08-27 16:49:44 -0400881static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400882 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400883{
Chris Mason39279cc2007-06-12 06:35:45 -0400884 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500886 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400887 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400888 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500889 u64 existing_delalloc;
890 u64 delalloc_start;
891 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400892
Chris Mason190662b2007-12-18 16:25:45 -0500893 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400894 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400895
Chris Masond1310b22008-01-24 16:13:08 -0500896 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500897 delalloc_start = page_start;
Chris Masond1310b22008-01-24 16:13:08 -0500898 existing_delalloc = count_range_bits(&BTRFS_I(inode)->io_tree,
Chris Mason1832a6d2007-12-21 16:27:21 -0500899 &delalloc_start, page_end,
900 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masond1310b22008-01-24 16:13:08 -0500901 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400902 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500903
904 spin_lock(&root->fs_info->delalloc_lock);
905 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
906 spin_unlock(&root->fs_info->delalloc_lock);
907
Chris Masona52d9a82007-08-27 16:49:44 -0400908 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400909 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400910 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
911 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400912 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400913 }
Chris Masonb888db22007-08-27 16:49:44 -0400914 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500915 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400916
Chris Masona52d9a82007-08-27 16:49:44 -0400917 return ret;
918}
919
920/*
921 * taken from block_truncate_page, but does cow as it zeros out
922 * any bytes left in the last page in the file.
923 */
924static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
925{
926 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400927 struct btrfs_root *root = BTRFS_I(inode)->root;
928 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400929 pgoff_t index = from >> PAGE_CACHE_SHIFT;
930 unsigned offset = from & (PAGE_CACHE_SIZE-1);
931 struct page *page;
932 int ret = 0;
933 u64 page_start;
934
935 if ((offset & (blocksize - 1)) == 0)
936 goto out;
937
938 ret = -ENOMEM;
939 page = grab_cache_page(mapping, index);
940 if (!page)
941 goto out;
942 if (!PageUptodate(page)) {
943 ret = btrfs_readpage(NULL, page);
944 lock_page(page);
945 if (!PageUptodate(page)) {
946 ret = -EIO;
947 goto out;
948 }
949 }
Chris Mason35ebb932007-10-30 16:56:53 -0400950 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400951
Chris Masonb888db22007-08-27 16:49:44 -0400952 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400953
Chris Mason39279cc2007-06-12 06:35:45 -0400954 unlock_page(page);
955 page_cache_release(page);
956out:
957 return ret;
958}
959
960static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
961{
962 struct inode *inode = dentry->d_inode;
963 int err;
964
965 err = inode_change_ok(inode, attr);
966 if (err)
967 return err;
968
969 if (S_ISREG(inode->i_mode) &&
970 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
971 struct btrfs_trans_handle *trans;
972 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500973 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -0400974
Chris Mason5f39d392007-10-15 16:14:19 -0400975 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400976 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400977 u64 block_end = attr->ia_size | mask;
Chris Mason5f564062008-01-22 16:47:59 -0500978 u64 hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400979 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400980 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400981
982 if (attr->ia_size <= pos)
983 goto out;
984
Chris Mason5f564062008-01-22 16:47:59 -0500985 if (pos != inode->i_size)
986 hole_start = pos + root->sectorsize;
987 else
988 hole_start = pos;
989
Chris Mason1832a6d2007-12-21 16:27:21 -0500990 mutex_lock(&root->fs_info->fs_mutex);
991 err = btrfs_check_free_space(root, 1, 0);
992 mutex_unlock(&root->fs_info->fs_mutex);
993 if (err)
994 goto fail;
995
Chris Mason39279cc2007-06-12 06:35:45 -0400996 btrfs_truncate_page(inode->i_mapping, inode->i_size);
997
Chris Masond1310b22008-01-24 16:13:08 -0500998 lock_extent(io_tree, pos, block_end, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -0500999 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001000
1001 mutex_lock(&root->fs_info->fs_mutex);
1002 trans = btrfs_start_transaction(root, 1);
1003 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001004 err = btrfs_drop_extents(trans, root, inode,
Chris Mason5f564062008-01-22 16:47:59 -05001005 pos, block_end, pos,
Chris Mason3326d1b2007-10-15 16:18:25 -04001006 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001007
Chris Mason179e29e2007-11-01 11:28:41 -04001008 if (alloc_hint != EXTENT_MAP_INLINE) {
1009 err = btrfs_insert_file_extent(trans, root,
1010 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001011 hole_start, 0, 0,
1012 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001013 btrfs_drop_extent_cache(inode, hole_start,
1014 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001015 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001016 }
Chris Mason39279cc2007-06-12 06:35:45 -04001017 btrfs_end_transaction(trans, root);
1018 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond1310b22008-01-24 16:13:08 -05001019 unlock_extent(io_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001020 if (err)
1021 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001022 }
1023out:
1024 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001025fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001026 return err;
1027}
Chris Mason61295eb2008-01-14 16:24:38 -05001028
Chris Mason2da98f02008-01-16 11:44:43 -05001029void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001030{
Chris Mason2da98f02008-01-16 11:44:43 -05001031 int ret;
1032
1033 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001034 return;
1035 }
Chris Mason2da98f02008-01-16 11:44:43 -05001036
1037 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1038 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1039 return;
1040
1041 ret = btrfs_del_ordered_inode(inode);
1042 if (ret == 1) {
1043 atomic_dec(&inode->i_count);
1044 }
Chris Mason61295eb2008-01-14 16:24:38 -05001045}
1046
Chris Mason39279cc2007-06-12 06:35:45 -04001047void btrfs_delete_inode(struct inode *inode)
1048{
1049 struct btrfs_trans_handle *trans;
1050 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001051 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001052 int ret;
1053
1054 truncate_inode_pages(&inode->i_data, 0);
1055 if (is_bad_inode(inode)) {
1056 goto no_delete;
1057 }
Chris Mason5f39d392007-10-15 16:14:19 -04001058
Chris Mason39279cc2007-06-12 06:35:45 -04001059 inode->i_size = 0;
1060 mutex_lock(&root->fs_info->fs_mutex);
1061 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001062
Chris Mason39279cc2007-06-12 06:35:45 -04001063 btrfs_set_trans_block_group(trans, inode);
1064 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001065 if (ret)
1066 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001067 ret = btrfs_delete_xattrs(trans, root, inode);
1068 if (ret)
1069 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001070 ret = btrfs_free_inode(trans, root, inode);
1071 if (ret)
1072 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001073 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001074
Chris Mason39279cc2007-06-12 06:35:45 -04001075 btrfs_end_transaction(trans, root);
1076 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001077 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001078 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001079 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001080
1081no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001082 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001083 btrfs_end_transaction(trans, root);
1084 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001085 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001086 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001087no_delete:
1088 clear_inode(inode);
1089}
1090
1091/*
1092 * this returns the key found in the dir entry in the location pointer.
1093 * If no dir entries were found, location->objectid is 0.
1094 */
1095static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1096 struct btrfs_key *location)
1097{
1098 const char *name = dentry->d_name.name;
1099 int namelen = dentry->d_name.len;
1100 struct btrfs_dir_item *di;
1101 struct btrfs_path *path;
1102 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001103 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001104
Chris Mason39544012007-12-12 14:38:19 -05001105 if (namelen == 1 && strcmp(name, ".") == 0) {
1106 location->objectid = dir->i_ino;
1107 location->type = BTRFS_INODE_ITEM_KEY;
1108 location->offset = 0;
1109 return 0;
1110 }
Chris Mason39279cc2007-06-12 06:35:45 -04001111 path = btrfs_alloc_path();
1112 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001113
Chris Mason7a720532007-12-13 09:06:59 -05001114 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001115 struct btrfs_key key;
1116 struct extent_buffer *leaf;
1117 u32 nritems;
1118 int slot;
1119
1120 key.objectid = dir->i_ino;
1121 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1122 key.offset = 0;
1123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1124 BUG_ON(ret == 0);
1125 ret = 0;
1126
1127 leaf = path->nodes[0];
1128 slot = path->slots[0];
1129 nritems = btrfs_header_nritems(leaf);
1130 if (slot >= nritems)
1131 goto out_err;
1132
1133 btrfs_item_key_to_cpu(leaf, &key, slot);
1134 if (key.objectid != dir->i_ino ||
1135 key.type != BTRFS_INODE_REF_KEY) {
1136 goto out_err;
1137 }
1138 location->objectid = key.offset;
1139 location->type = BTRFS_INODE_ITEM_KEY;
1140 location->offset = 0;
1141 goto out;
1142 }
1143
Chris Mason39279cc2007-06-12 06:35:45 -04001144 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1145 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001146 if (IS_ERR(di))
1147 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001148 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001149 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001150 }
Chris Mason5f39d392007-10-15 16:14:19 -04001151 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001152out:
Chris Mason39279cc2007-06-12 06:35:45 -04001153 btrfs_free_path(path);
1154 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001155out_err:
1156 location->objectid = 0;
1157 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001158}
1159
1160/*
1161 * when we hit a tree root in a directory, the btrfs part of the inode
1162 * needs to be changed to reflect the root directory of the tree root. This
1163 * is kind of like crossing a mount point.
1164 */
1165static int fixup_tree_root_location(struct btrfs_root *root,
1166 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001167 struct btrfs_root **sub_root,
1168 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001169{
1170 struct btrfs_path *path;
1171 struct btrfs_root_item *ri;
1172
1173 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1174 return 0;
1175 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1176 return 0;
1177
1178 path = btrfs_alloc_path();
1179 BUG_ON(!path);
1180 mutex_lock(&root->fs_info->fs_mutex);
1181
Josef Bacik58176a92007-08-29 15:47:34 -04001182 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1183 dentry->d_name.name,
1184 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001185 if (IS_ERR(*sub_root))
1186 return PTR_ERR(*sub_root);
1187
1188 ri = &(*sub_root)->root_item;
1189 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001190 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1191 location->offset = 0;
1192
1193 btrfs_free_path(path);
1194 mutex_unlock(&root->fs_info->fs_mutex);
1195 return 0;
1196}
1197
1198static int btrfs_init_locked_inode(struct inode *inode, void *p)
1199{
1200 struct btrfs_iget_args *args = p;
1201 inode->i_ino = args->ino;
1202 BTRFS_I(inode)->root = args->root;
Chris Masond1310b22008-01-24 16:13:08 -05001203 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1204 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001205 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001206 return 0;
1207}
1208
1209static int btrfs_find_actor(struct inode *inode, void *opaque)
1210{
1211 struct btrfs_iget_args *args = opaque;
1212 return (args->ino == inode->i_ino &&
1213 args->root == BTRFS_I(inode)->root);
1214}
1215
Chris Masondc17ff82008-01-08 15:46:30 -05001216struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1217 u64 root_objectid)
1218{
1219 struct btrfs_iget_args args;
1220 args.ino = objectid;
1221 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1222
1223 if (!args.root)
1224 return NULL;
1225
1226 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1227}
1228
Chris Mason39279cc2007-06-12 06:35:45 -04001229struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1230 struct btrfs_root *root)
1231{
1232 struct inode *inode;
1233 struct btrfs_iget_args args;
1234 args.ino = objectid;
1235 args.root = root;
1236
1237 inode = iget5_locked(s, objectid, btrfs_find_actor,
1238 btrfs_init_locked_inode,
1239 (void *)&args);
1240 return inode;
1241}
1242
1243static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1244 struct nameidata *nd)
1245{
1246 struct inode * inode;
1247 struct btrfs_inode *bi = BTRFS_I(dir);
1248 struct btrfs_root *root = bi->root;
1249 struct btrfs_root *sub_root = root;
1250 struct btrfs_key location;
1251 int ret;
1252
1253 if (dentry->d_name.len > BTRFS_NAME_LEN)
1254 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001255
Chris Mason39279cc2007-06-12 06:35:45 -04001256 mutex_lock(&root->fs_info->fs_mutex);
1257 ret = btrfs_inode_by_name(dir, dentry, &location);
1258 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001259
Chris Mason39279cc2007-06-12 06:35:45 -04001260 if (ret < 0)
1261 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001262
Chris Mason39279cc2007-06-12 06:35:45 -04001263 inode = NULL;
1264 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001265 ret = fixup_tree_root_location(root, &location, &sub_root,
1266 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001267 if (ret < 0)
1268 return ERR_PTR(ret);
1269 if (ret > 0)
1270 return ERR_PTR(-ENOENT);
1271 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1272 sub_root);
1273 if (!inode)
1274 return ERR_PTR(-EACCES);
1275 if (inode->i_state & I_NEW) {
1276 /* the inode and parent dir are two different roots */
1277 if (sub_root != root) {
1278 igrab(inode);
1279 sub_root->inode = inode;
1280 }
1281 BTRFS_I(inode)->root = sub_root;
1282 memcpy(&BTRFS_I(inode)->location, &location,
1283 sizeof(location));
1284 btrfs_read_locked_inode(inode);
1285 unlock_new_inode(inode);
1286 }
1287 }
1288 return d_splice_alias(inode, dentry);
1289}
1290
Chris Mason39279cc2007-06-12 06:35:45 -04001291static unsigned char btrfs_filetype_table[] = {
1292 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1293};
1294
1295static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1296{
Chris Mason6da6aba2007-12-18 16:15:09 -05001297 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001298 struct btrfs_root *root = BTRFS_I(inode)->root;
1299 struct btrfs_item *item;
1300 struct btrfs_dir_item *di;
1301 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001302 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001303 struct btrfs_path *path;
1304 int ret;
1305 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001306 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001307 int slot;
1308 int advance;
1309 unsigned char d_type;
1310 int over = 0;
1311 u32 di_cur;
1312 u32 di_total;
1313 u32 di_len;
1314 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001315 char tmp_name[32];
1316 char *name_ptr;
1317 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001318
1319 /* FIXME, use a real flag for deciding about the key type */
1320 if (root->fs_info->tree_root == root)
1321 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001322
Chris Mason39544012007-12-12 14:38:19 -05001323 /* special case for "." */
1324 if (filp->f_pos == 0) {
1325 over = filldir(dirent, ".", 1,
1326 1, inode->i_ino,
1327 DT_DIR);
1328 if (over)
1329 return 0;
1330 filp->f_pos = 1;
1331 }
1332
Chris Mason39279cc2007-06-12 06:35:45 -04001333 mutex_lock(&root->fs_info->fs_mutex);
1334 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001335 path = btrfs_alloc_path();
1336 path->reada = 2;
1337
1338 /* special case for .., just use the back ref */
1339 if (filp->f_pos == 1) {
1340 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1341 key.offset = 0;
1342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1343 BUG_ON(ret == 0);
1344 leaf = path->nodes[0];
1345 slot = path->slots[0];
1346 nritems = btrfs_header_nritems(leaf);
1347 if (slot >= nritems) {
1348 btrfs_release_path(root, path);
1349 goto read_dir_items;
1350 }
1351 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1352 btrfs_release_path(root, path);
1353 if (found_key.objectid != key.objectid ||
1354 found_key.type != BTRFS_INODE_REF_KEY)
1355 goto read_dir_items;
1356 over = filldir(dirent, "..", 2,
1357 2, found_key.offset, DT_DIR);
1358 if (over)
1359 goto nopos;
1360 filp->f_pos = 2;
1361 }
1362
1363read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001364 btrfs_set_key_type(&key, key_type);
1365 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001366
Chris Mason39279cc2007-06-12 06:35:45 -04001367 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1368 if (ret < 0)
1369 goto err;
1370 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001371 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001372 leaf = path->nodes[0];
1373 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001374 slot = path->slots[0];
1375 if (advance || slot >= nritems) {
1376 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001377 ret = btrfs_next_leaf(root, path);
1378 if (ret)
1379 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001380 leaf = path->nodes[0];
1381 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001382 slot = path->slots[0];
1383 } else {
1384 slot++;
1385 path->slots[0]++;
1386 }
1387 }
1388 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001389 item = btrfs_item_nr(leaf, slot);
1390 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1391
1392 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001393 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001394 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001395 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001396 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001397 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001398
1399 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001400 advance = 1;
1401 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1402 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001403 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001404 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001405 struct btrfs_key location;
1406
1407 name_len = btrfs_dir_name_len(leaf, di);
1408 if (name_len < 32) {
1409 name_ptr = tmp_name;
1410 } else {
1411 name_ptr = kmalloc(name_len, GFP_NOFS);
1412 BUG_ON(!name_ptr);
1413 }
1414 read_extent_buffer(leaf, name_ptr,
1415 (unsigned long)(di + 1), name_len);
1416
1417 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1418 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001419 over = filldir(dirent, name_ptr, name_len,
1420 found_key.offset,
1421 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001422 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001423
1424 if (name_ptr != tmp_name)
1425 kfree(name_ptr);
1426
Chris Mason39279cc2007-06-12 06:35:45 -04001427 if (over)
1428 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001429 di_len = btrfs_dir_name_len(leaf, di) +
1430 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001431 di_cur += di_len;
1432 di = (struct btrfs_dir_item *)((char *)di + di_len);
1433 }
1434 }
Chris Masonc2a8b6e2008-01-29 09:10:26 -05001435 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
Chris Mason39279cc2007-06-12 06:35:45 -04001436nopos:
1437 ret = 0;
1438err:
1439 btrfs_release_path(root, path);
1440 btrfs_free_path(path);
1441 mutex_unlock(&root->fs_info->fs_mutex);
1442 return ret;
1443}
1444
1445int btrfs_write_inode(struct inode *inode, int wait)
1446{
1447 struct btrfs_root *root = BTRFS_I(inode)->root;
1448 struct btrfs_trans_handle *trans;
1449 int ret = 0;
1450
1451 if (wait) {
1452 mutex_lock(&root->fs_info->fs_mutex);
1453 trans = btrfs_start_transaction(root, 1);
1454 btrfs_set_trans_block_group(trans, inode);
1455 ret = btrfs_commit_transaction(trans, root);
1456 mutex_unlock(&root->fs_info->fs_mutex);
1457 }
1458 return ret;
1459}
1460
1461/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001462 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001463 * inode changes. But, it is most likely to find the inode in cache.
1464 * FIXME, needs more benchmarking...there are no reasons other than performance
1465 * to keep or drop this code.
1466 */
1467void btrfs_dirty_inode(struct inode *inode)
1468{
1469 struct btrfs_root *root = BTRFS_I(inode)->root;
1470 struct btrfs_trans_handle *trans;
1471
1472 mutex_lock(&root->fs_info->fs_mutex);
1473 trans = btrfs_start_transaction(root, 1);
1474 btrfs_set_trans_block_group(trans, inode);
1475 btrfs_update_inode(trans, root, inode);
1476 btrfs_end_transaction(trans, root);
1477 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001478}
1479
1480static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1481 struct btrfs_root *root,
1482 u64 objectid,
1483 struct btrfs_block_group_cache *group,
1484 int mode)
1485{
1486 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001487 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001488 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001489 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001490 int ret;
1491 int owner;
1492
Chris Mason5f39d392007-10-15 16:14:19 -04001493 path = btrfs_alloc_path();
1494 BUG_ON(!path);
1495
Chris Mason39279cc2007-06-12 06:35:45 -04001496 inode = new_inode(root->fs_info->sb);
1497 if (!inode)
1498 return ERR_PTR(-ENOMEM);
1499
Chris Masond1310b22008-01-24 16:13:08 -05001500 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1501 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001502 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001503 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001504
Chris Mason39279cc2007-06-12 06:35:45 -04001505 if (mode & S_IFDIR)
1506 owner = 0;
1507 else
1508 owner = 1;
1509 group = btrfs_find_block_group(root, group, 0, 0, owner);
1510 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001511 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001512 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1513 if (ret)
1514 goto fail;
1515
Chris Mason39279cc2007-06-12 06:35:45 -04001516 inode->i_uid = current->fsuid;
1517 inode->i_gid = current->fsgid;
1518 inode->i_mode = mode;
1519 inode->i_ino = objectid;
1520 inode->i_blocks = 0;
1521 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001522 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1523 struct btrfs_inode_item);
1524 fill_inode_item(path->nodes[0], inode_item, inode);
1525 btrfs_mark_buffer_dirty(path->nodes[0]);
1526 btrfs_free_path(path);
1527
Chris Mason39279cc2007-06-12 06:35:45 -04001528 location = &BTRFS_I(inode)->location;
1529 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001530 location->offset = 0;
1531 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1532
Chris Mason39279cc2007-06-12 06:35:45 -04001533 insert_inode_hash(inode);
1534 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001535fail:
1536 btrfs_free_path(path);
1537 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001538}
1539
1540static inline u8 btrfs_inode_type(struct inode *inode)
1541{
1542 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1543}
1544
1545static int btrfs_add_link(struct btrfs_trans_handle *trans,
1546 struct dentry *dentry, struct inode *inode)
1547{
1548 int ret;
1549 struct btrfs_key key;
1550 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001551 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001552
Chris Mason39279cc2007-06-12 06:35:45 -04001553 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001554 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1555 key.offset = 0;
1556
1557 ret = btrfs_insert_dir_item(trans, root,
1558 dentry->d_name.name, dentry->d_name.len,
1559 dentry->d_parent->d_inode->i_ino,
1560 &key, btrfs_inode_type(inode));
1561 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001562 ret = btrfs_insert_inode_ref(trans, root,
1563 dentry->d_name.name,
1564 dentry->d_name.len,
1565 inode->i_ino,
1566 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001567 parent_inode = dentry->d_parent->d_inode;
1568 parent_inode->i_size += dentry->d_name.len * 2;
1569 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001570 ret = btrfs_update_inode(trans, root,
1571 dentry->d_parent->d_inode);
1572 }
1573 return ret;
1574}
1575
1576static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1577 struct dentry *dentry, struct inode *inode)
1578{
1579 int err = btrfs_add_link(trans, dentry, inode);
1580 if (!err) {
1581 d_instantiate(dentry, inode);
1582 return 0;
1583 }
1584 if (err > 0)
1585 err = -EEXIST;
1586 return err;
1587}
1588
Josef Bacik618e21d2007-07-11 10:18:17 -04001589static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1590 int mode, dev_t rdev)
1591{
1592 struct btrfs_trans_handle *trans;
1593 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001594 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001595 int err;
1596 int drop_inode = 0;
1597 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001598 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001599
1600 if (!new_valid_dev(rdev))
1601 return -EINVAL;
1602
1603 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001604 err = btrfs_check_free_space(root, 1, 0);
1605 if (err)
1606 goto fail;
1607
Josef Bacik618e21d2007-07-11 10:18:17 -04001608 trans = btrfs_start_transaction(root, 1);
1609 btrfs_set_trans_block_group(trans, dir);
1610
1611 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1612 if (err) {
1613 err = -ENOSPC;
1614 goto out_unlock;
1615 }
1616
1617 inode = btrfs_new_inode(trans, root, objectid,
1618 BTRFS_I(dir)->block_group, mode);
1619 err = PTR_ERR(inode);
1620 if (IS_ERR(inode))
1621 goto out_unlock;
1622
1623 btrfs_set_trans_block_group(trans, inode);
1624 err = btrfs_add_nondir(trans, dentry, inode);
1625 if (err)
1626 drop_inode = 1;
1627 else {
1628 inode->i_op = &btrfs_special_inode_operations;
1629 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001630 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001631 }
1632 dir->i_sb->s_dirt = 1;
1633 btrfs_update_inode_block_group(trans, inode);
1634 btrfs_update_inode_block_group(trans, dir);
1635out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001636 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001637 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001638fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001639 mutex_unlock(&root->fs_info->fs_mutex);
1640
1641 if (drop_inode) {
1642 inode_dec_link_count(inode);
1643 iput(inode);
1644 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001645 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001646 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001647 return err;
1648}
1649
Chris Mason39279cc2007-06-12 06:35:45 -04001650static int btrfs_create(struct inode *dir, struct dentry *dentry,
1651 int mode, struct nameidata *nd)
1652{
1653 struct btrfs_trans_handle *trans;
1654 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001655 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001656 int err;
1657 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001658 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001659 u64 objectid;
1660
1661 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001662 err = btrfs_check_free_space(root, 1, 0);
1663 if (err)
1664 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001665 trans = btrfs_start_transaction(root, 1);
1666 btrfs_set_trans_block_group(trans, dir);
1667
1668 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1669 if (err) {
1670 err = -ENOSPC;
1671 goto out_unlock;
1672 }
1673
1674 inode = btrfs_new_inode(trans, root, objectid,
1675 BTRFS_I(dir)->block_group, mode);
1676 err = PTR_ERR(inode);
1677 if (IS_ERR(inode))
1678 goto out_unlock;
1679
1680 btrfs_set_trans_block_group(trans, inode);
1681 err = btrfs_add_nondir(trans, dentry, inode);
1682 if (err)
1683 drop_inode = 1;
1684 else {
1685 inode->i_mapping->a_ops = &btrfs_aops;
1686 inode->i_fop = &btrfs_file_operations;
1687 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001688 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1689 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001690 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001691 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001692 }
1693 dir->i_sb->s_dirt = 1;
1694 btrfs_update_inode_block_group(trans, inode);
1695 btrfs_update_inode_block_group(trans, dir);
1696out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001697 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001698 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001699fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001700 mutex_unlock(&root->fs_info->fs_mutex);
1701
1702 if (drop_inode) {
1703 inode_dec_link_count(inode);
1704 iput(inode);
1705 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001706 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001707 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001708 return err;
1709}
1710
1711static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1712 struct dentry *dentry)
1713{
1714 struct btrfs_trans_handle *trans;
1715 struct btrfs_root *root = BTRFS_I(dir)->root;
1716 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001717 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001718 int err;
1719 int drop_inode = 0;
1720
1721 if (inode->i_nlink == 0)
1722 return -ENOENT;
1723
Chris Mason6da6aba2007-12-18 16:15:09 -05001724#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1725 inode->i_nlink++;
1726#else
Chris Mason39279cc2007-06-12 06:35:45 -04001727 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001728#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001729 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001730 err = btrfs_check_free_space(root, 1, 0);
1731 if (err)
1732 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001733 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001734
Chris Mason39279cc2007-06-12 06:35:45 -04001735 btrfs_set_trans_block_group(trans, dir);
1736 atomic_inc(&inode->i_count);
1737 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001738
Chris Mason39279cc2007-06-12 06:35:45 -04001739 if (err)
1740 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001741
Chris Mason39279cc2007-06-12 06:35:45 -04001742 dir->i_sb->s_dirt = 1;
1743 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001744 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001745
Chris Mason54aa1f42007-06-22 14:16:25 -04001746 if (err)
1747 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001748
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001749 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001750 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001751fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001752 mutex_unlock(&root->fs_info->fs_mutex);
1753
1754 if (drop_inode) {
1755 inode_dec_link_count(inode);
1756 iput(inode);
1757 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001758 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001759 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001760 return err;
1761}
1762
Chris Mason39279cc2007-06-12 06:35:45 -04001763static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1764{
1765 struct inode *inode;
1766 struct btrfs_trans_handle *trans;
1767 struct btrfs_root *root = BTRFS_I(dir)->root;
1768 int err = 0;
1769 int drop_on_err = 0;
1770 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001771 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001772
1773 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001774 err = btrfs_check_free_space(root, 1, 0);
1775 if (err)
1776 goto out_unlock;
1777
Chris Mason39279cc2007-06-12 06:35:45 -04001778 trans = btrfs_start_transaction(root, 1);
1779 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001780
Chris Mason39279cc2007-06-12 06:35:45 -04001781 if (IS_ERR(trans)) {
1782 err = PTR_ERR(trans);
1783 goto out_unlock;
1784 }
1785
1786 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1787 if (err) {
1788 err = -ENOSPC;
1789 goto out_unlock;
1790 }
1791
1792 inode = btrfs_new_inode(trans, root, objectid,
1793 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1794 if (IS_ERR(inode)) {
1795 err = PTR_ERR(inode);
1796 goto out_fail;
1797 }
Chris Mason5f39d392007-10-15 16:14:19 -04001798
Chris Mason39279cc2007-06-12 06:35:45 -04001799 drop_on_err = 1;
1800 inode->i_op = &btrfs_dir_inode_operations;
1801 inode->i_fop = &btrfs_dir_file_operations;
1802 btrfs_set_trans_block_group(trans, inode);
1803
Chris Mason39544012007-12-12 14:38:19 -05001804 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001805 err = btrfs_update_inode(trans, root, inode);
1806 if (err)
1807 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001808
Chris Mason39279cc2007-06-12 06:35:45 -04001809 err = btrfs_add_link(trans, dentry, inode);
1810 if (err)
1811 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001812
Chris Mason39279cc2007-06-12 06:35:45 -04001813 d_instantiate(dentry, inode);
1814 drop_on_err = 0;
1815 dir->i_sb->s_dirt = 1;
1816 btrfs_update_inode_block_group(trans, inode);
1817 btrfs_update_inode_block_group(trans, dir);
1818
1819out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001820 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001821 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001822
Chris Mason39279cc2007-06-12 06:35:45 -04001823out_unlock:
1824 mutex_unlock(&root->fs_info->fs_mutex);
1825 if (drop_on_err)
1826 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001827 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001828 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001829 return err;
1830}
1831
Chris Masona52d9a82007-08-27 16:49:44 -04001832struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Masond1310b22008-01-24 16:13:08 -05001833 size_t page_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001834 int create)
1835{
1836 int ret;
1837 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001838 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001839 u64 extent_start = 0;
1840 u64 extent_end = 0;
1841 u64 objectid = inode->i_ino;
1842 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001843 struct btrfs_path *path;
1844 struct btrfs_root *root = BTRFS_I(inode)->root;
1845 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001846 struct extent_buffer *leaf;
1847 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001848 struct extent_map *em = NULL;
1849 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001850 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001851 struct btrfs_trans_handle *trans = NULL;
1852
1853 path = btrfs_alloc_path();
1854 BUG_ON(!path);
1855 mutex_lock(&root->fs_info->fs_mutex);
1856
1857again:
Chris Masond1310b22008-01-24 16:13:08 -05001858 spin_lock(&em_tree->lock);
1859 em = lookup_extent_mapping(em_tree, start, len);
1860 spin_unlock(&em_tree->lock);
1861
Chris Masona52d9a82007-08-27 16:49:44 -04001862 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001863 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001864 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1865 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001866 WARN_ON(1);
1867 }
Chris Masona52d9a82007-08-27 16:49:44 -04001868 goto out;
1869 }
Chris Masond1310b22008-01-24 16:13:08 -05001870 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001871 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001872 err = -ENOMEM;
1873 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001874 }
Chris Masond1310b22008-01-24 16:13:08 -05001875
1876 em->start = EXTENT_MAP_HOLE;
1877 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001878 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001879 ret = btrfs_lookup_file_extent(trans, root, path,
1880 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001881 if (ret < 0) {
1882 err = ret;
1883 goto out;
1884 }
1885
1886 if (ret != 0) {
1887 if (path->slots[0] == 0)
1888 goto not_found;
1889 path->slots[0]--;
1890 }
1891
Chris Mason5f39d392007-10-15 16:14:19 -04001892 leaf = path->nodes[0];
1893 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001894 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001895 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001896 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1897 found_type = btrfs_key_type(&found_key);
1898 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001899 found_type != BTRFS_EXTENT_DATA_KEY) {
1900 goto not_found;
1901 }
1902
Chris Mason5f39d392007-10-15 16:14:19 -04001903 found_type = btrfs_file_extent_type(leaf, item);
1904 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001905 if (found_type == BTRFS_FILE_EXTENT_REG) {
1906 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001907 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001908 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001909 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001910 em->start = start;
1911 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001912 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001913 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001914 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001915 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001916 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001917 }
1918 goto not_found_em;
1919 }
Chris Masondb945352007-10-15 16:15:53 -04001920 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1921 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001922 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001923 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001924 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001925 goto insert;
1926 }
Chris Masondb945352007-10-15 16:15:53 -04001927 bytenr += btrfs_file_extent_offset(leaf, item);
1928 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001929 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001930 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001931 goto insert;
1932 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001933 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001934 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001935 size_t size;
1936 size_t extent_offset;
1937 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1940 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05001941 extent_end = (extent_start + size + root->sectorsize - 1) &
1942 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001943 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001944 em->start = start;
1945 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001946 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001947 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001948 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001949 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001950 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001951 }
1952 goto not_found_em;
1953 }
1954 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001955
1956 if (!page) {
1957 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001958 em->len = size;
Yan689f9342007-10-29 11:41:07 -04001959 goto out;
1960 }
1961
Chris Mason35ebb932007-10-30 16:56:53 -04001962 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001963 extent_start + page_offset;
1964 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1965 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001966 em->start = extent_start + extent_offset;
Chris Masond1310b22008-01-24 16:13:08 -05001967 em->len = copy_size;
Yan689f9342007-10-29 11:41:07 -04001968 map = kmap(page);
1969 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001970 if (create == 0 && !PageUptodate(page)) {
1971 read_extent_buffer(leaf, map + page_offset, ptr,
1972 copy_size);
1973 flush_dcache_page(page);
1974 } else if (create && PageUptodate(page)) {
1975 if (!trans) {
1976 kunmap(page);
1977 free_extent_map(em);
1978 em = NULL;
1979 btrfs_release_path(root, path);
1980 trans = btrfs_start_transaction(root, 1);
1981 goto again;
1982 }
1983 write_extent_buffer(leaf, map + page_offset, ptr,
1984 copy_size);
1985 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001986 }
Chris Masona52d9a82007-08-27 16:49:44 -04001987 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05001988 set_extent_uptodate(io_tree, em->start,
1989 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001990 goto insert;
1991 } else {
1992 printk("unkknown found_type %d\n", found_type);
1993 WARN_ON(1);
1994 }
1995not_found:
1996 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05001997 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001998not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001999 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002000insert:
2001 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002002 if (em->start > start || extent_map_end(em) <= start) {
2003 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002004 err = -EIO;
2005 goto out;
2006 }
Chris Masond1310b22008-01-24 16:13:08 -05002007
2008 err = 0;
2009 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002010 ret = add_extent_mapping(em_tree, em);
2011 if (ret == -EEXIST) {
2012 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002013 em = lookup_extent_mapping(em_tree, start, len);
2014 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002015 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002016 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002017 }
Chris Masona52d9a82007-08-27 16:49:44 -04002018 }
Chris Masond1310b22008-01-24 16:13:08 -05002019 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002020out:
2021 btrfs_free_path(path);
2022 if (trans) {
2023 ret = btrfs_end_transaction(trans, root);
2024 if (!err)
2025 err = ret;
2026 }
2027 mutex_unlock(&root->fs_info->fs_mutex);
2028 if (err) {
2029 free_extent_map(em);
2030 WARN_ON(1);
2031 return ERR_PTR(err);
2032 }
2033 return em;
2034}
2035
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002036static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002037{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002038 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002039}
2040
Chris Mason9ebefb182007-06-15 13:50:00 -04002041int btrfs_readpage(struct file *file, struct page *page)
2042{
Chris Masond1310b22008-01-24 16:13:08 -05002043 struct extent_io_tree *tree;
2044 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002045 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002046}
Chris Mason1832a6d2007-12-21 16:27:21 -05002047
Chris Mason39279cc2007-06-12 06:35:45 -04002048static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2049{
Chris Masond1310b22008-01-24 16:13:08 -05002050 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002051
2052
2053 if (current->flags & PF_MEMALLOC) {
2054 redirty_page_for_writepage(wbc, page);
2055 unlock_page(page);
2056 return 0;
2057 }
Chris Masond1310b22008-01-24 16:13:08 -05002058 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002059 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2060}
Chris Mason39279cc2007-06-12 06:35:45 -04002061
Chris Masonb293f02e2007-11-01 19:45:34 -04002062static int btrfs_writepages(struct address_space *mapping,
2063 struct writeback_control *wbc)
2064{
Chris Masond1310b22008-01-24 16:13:08 -05002065 struct extent_io_tree *tree;
2066 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04002067 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2068}
2069
Chris Mason3ab2fb52007-11-08 10:59:22 -05002070static int
2071btrfs_readpages(struct file *file, struct address_space *mapping,
2072 struct list_head *pages, unsigned nr_pages)
2073{
Chris Masond1310b22008-01-24 16:13:08 -05002074 struct extent_io_tree *tree;
2075 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002076 return extent_readpages(tree, mapping, pages, nr_pages,
2077 btrfs_get_extent);
2078}
2079
Chris Masona52d9a82007-08-27 16:49:44 -04002080static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2081{
Chris Masond1310b22008-01-24 16:13:08 -05002082 struct extent_io_tree *tree;
2083 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002084 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002085
Chris Masond1310b22008-01-24 16:13:08 -05002086 tree = &BTRFS_I(page->mapping->host)->io_tree;
2087 map = &BTRFS_I(page->mapping->host)->extent_tree;
2088 ret = try_release_extent_mapping(map, tree, page);
Chris Masona52d9a82007-08-27 16:49:44 -04002089 if (ret == 1) {
2090 ClearPagePrivate(page);
2091 set_page_private(page, 0);
2092 page_cache_release(page);
2093 }
2094 return ret;
2095}
Chris Mason39279cc2007-06-12 06:35:45 -04002096
Chris Masona52d9a82007-08-27 16:49:44 -04002097static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2098{
Chris Masond1310b22008-01-24 16:13:08 -05002099 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002100
Chris Masond1310b22008-01-24 16:13:08 -05002101 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002102 extent_invalidatepage(tree, page, offset);
2103 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002104}
2105
Chris Mason9ebefb182007-06-15 13:50:00 -04002106/*
2107 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2108 * called from a page fault handler when a page is first dirtied. Hence we must
2109 * be careful to check for EOF conditions here. We set the page up correctly
2110 * for a written page which means we get ENOSPC checking when writing into
2111 * holes and correct delalloc and unwritten extent mapping on filesystems that
2112 * support these features.
2113 *
2114 * We are not allowed to take the i_mutex here so we have to play games to
2115 * protect against truncate races as the page could now be beyond EOF. Because
2116 * vmtruncate() writes the inode size before removing pages, once we have the
2117 * page lock we can determine safely if the page is beyond EOF. If it is not
2118 * beyond EOF, then the page is guaranteed safe against truncation until we
2119 * unlock the page.
2120 */
2121int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2122{
Chris Mason6da6aba2007-12-18 16:15:09 -05002123 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002124 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002125 unsigned long end;
2126 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002127 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002128 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002129
Chris Mason1832a6d2007-12-21 16:27:21 -05002130 mutex_lock(&root->fs_info->fs_mutex);
2131 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002132 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002133 if (ret)
2134 goto out;
2135
2136 ret = -EINVAL;
2137
Chris Mason9ebefb182007-06-15 13:50:00 -04002138 lock_page(page);
2139 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002140 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002141 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002142
Chris Mason9ebefb182007-06-15 13:50:00 -04002143 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002144 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002145 /* page got truncated out from underneath us */
2146 goto out_unlock;
2147 }
2148
2149 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002150 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002151 end = size & ~PAGE_CACHE_MASK;
2152 else
2153 end = PAGE_CACHE_SIZE;
2154
Chris Masonb888db22007-08-27 16:49:44 -04002155 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002156
2157out_unlock:
2158 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002159out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002160 return ret;
2161}
2162
Chris Mason39279cc2007-06-12 06:35:45 -04002163static void btrfs_truncate(struct inode *inode)
2164{
2165 struct btrfs_root *root = BTRFS_I(inode)->root;
2166 int ret;
2167 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002168 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002169
2170 if (!S_ISREG(inode->i_mode))
2171 return;
2172 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2173 return;
2174
2175 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2176
2177 mutex_lock(&root->fs_info->fs_mutex);
2178 trans = btrfs_start_transaction(root, 1);
2179 btrfs_set_trans_block_group(trans, inode);
2180
2181 /* FIXME, add redo link to tree so we don't leak on crash */
2182 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002183 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002184 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002185
Chris Mason39279cc2007-06-12 06:35:45 -04002186 ret = btrfs_end_transaction(trans, root);
2187 BUG_ON(ret);
2188 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002189 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002190 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002191}
2192
Chris Mason4313b392008-01-03 09:08:48 -05002193static int noinline create_subvol(struct btrfs_root *root, char *name,
2194 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002195{
2196 struct btrfs_trans_handle *trans;
2197 struct btrfs_key key;
2198 struct btrfs_root_item root_item;
2199 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002200 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002201 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002202 struct inode *inode;
2203 struct inode *dir;
2204 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002205 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002206 u64 objectid;
2207 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002208 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002209
2210 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002211 ret = btrfs_check_free_space(root, 1, 0);
2212 if (ret)
2213 goto fail_commit;
2214
Chris Mason39279cc2007-06-12 06:35:45 -04002215 trans = btrfs_start_transaction(root, 1);
2216 BUG_ON(!trans);
2217
Chris Mason7bb86312007-12-11 09:25:06 -05002218 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2219 0, &objectid);
2220 if (ret)
2221 goto fail;
2222
2223 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2224 objectid, trans->transid, 0, 0,
2225 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002226 if (IS_ERR(leaf))
2227 return PTR_ERR(leaf);
2228
2229 btrfs_set_header_nritems(leaf, 0);
2230 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002231 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002232 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002233 btrfs_set_header_owner(leaf, objectid);
2234
Chris Mason5f39d392007-10-15 16:14:19 -04002235 write_extent_buffer(leaf, root->fs_info->fsid,
2236 (unsigned long)btrfs_header_fsid(leaf),
2237 BTRFS_FSID_SIZE);
2238 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002239
2240 inode_item = &root_item.inode;
2241 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002242 inode_item->generation = cpu_to_le64(1);
2243 inode_item->size = cpu_to_le64(3);
2244 inode_item->nlink = cpu_to_le32(1);
2245 inode_item->nblocks = cpu_to_le64(1);
2246 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002247
Chris Masondb945352007-10-15 16:15:53 -04002248 btrfs_set_root_bytenr(&root_item, leaf->start);
2249 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002250 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002251 btrfs_set_root_used(&root_item, 0);
2252
Chris Mason5eda7b52007-06-22 14:16:25 -04002253 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2254 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002255
2256 free_extent_buffer(leaf);
2257 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002258
Chris Mason39279cc2007-06-12 06:35:45 -04002259 btrfs_set_root_dirid(&root_item, new_dirid);
2260
2261 key.objectid = objectid;
2262 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002263 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2264 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2265 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002266 if (ret)
2267 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002268
2269 /*
2270 * insert the directory item
2271 */
2272 key.offset = (u64)-1;
2273 dir = root->fs_info->sb->s_root->d_inode;
2274 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2275 name, namelen, dir->i_ino, &key,
2276 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002277 if (ret)
2278 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002279
Chris Mason39544012007-12-12 14:38:19 -05002280 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2281 name, namelen, objectid,
2282 root->fs_info->sb->s_root->d_inode->i_ino);
2283 if (ret)
2284 goto fail;
2285
Chris Mason39279cc2007-06-12 06:35:45 -04002286 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002287 if (ret)
2288 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002289
Josef Bacik58176a92007-08-29 15:47:34 -04002290 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002291 BUG_ON(!new_root);
2292
2293 trans = btrfs_start_transaction(new_root, 1);
2294 BUG_ON(!trans);
2295
2296 inode = btrfs_new_inode(trans, new_root, new_dirid,
2297 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002298 if (IS_ERR(inode))
2299 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002300 inode->i_op = &btrfs_dir_inode_operations;
2301 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002302 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002303
Chris Mason39544012007-12-12 14:38:19 -05002304 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2305 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002306 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002307 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002308 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002309 if (ret)
2310 goto fail;
2311fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002312 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002313 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002314 if (err && !ret)
2315 ret = err;
2316fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002317 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002318 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002319 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002320 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002321}
2322
2323static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2324{
Chris Mason3063d292008-01-08 15:46:30 -05002325 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002326 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002327 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002328 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002329 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002330
2331 if (!root->ref_cows)
2332 return -EINVAL;
2333
2334 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002335 ret = btrfs_check_free_space(root, 1, 0);
2336 if (ret)
2337 goto fail_unlock;
2338
Chris Mason3063d292008-01-08 15:46:30 -05002339 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2340 if (!pending_snapshot) {
2341 ret = -ENOMEM;
2342 goto fail_unlock;
2343 }
Yanfb4bc1e2008-01-17 11:59:51 -05002344 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002345 if (!pending_snapshot->name) {
2346 ret = -ENOMEM;
2347 kfree(pending_snapshot);
2348 goto fail_unlock;
2349 }
Yanfb4bc1e2008-01-17 11:59:51 -05002350 memcpy(pending_snapshot->name, name, namelen);
2351 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002352 trans = btrfs_start_transaction(root, 1);
2353 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002354 pending_snapshot->root = root;
2355 list_add(&pending_snapshot->list,
2356 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002357 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002358 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002359
Chris Mason1832a6d2007-12-21 16:27:21 -05002360fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002361 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002362 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002363 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002364 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002365}
2366
Chris Masonedbd8d42007-12-21 16:27:24 -05002367unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002368 struct file_ra_state *ra, struct file *file,
2369 pgoff_t offset, pgoff_t last_index)
2370{
2371 pgoff_t req_size;
2372
2373#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2374 req_size = last_index - offset + 1;
2375 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2376 return offset;
2377#else
2378 req_size = min(last_index - offset + 1, (pgoff_t)128);
2379 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2380 return offset + req_size;
2381#endif
2382}
2383
2384int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002385 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002386 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002387 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002388 struct page *page;
2389 unsigned long last_index;
2390 unsigned long ra_index = 0;
2391 u64 page_start;
2392 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002393 u64 delalloc_start;
2394 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002395 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002396 int ret;
2397
2398 mutex_lock(&root->fs_info->fs_mutex);
2399 ret = btrfs_check_free_space(root, inode->i_size, 0);
2400 mutex_unlock(&root->fs_info->fs_mutex);
2401 if (ret)
2402 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002403
2404 mutex_lock(&inode->i_mutex);
2405 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2406 for (i = 0; i <= last_index; i++) {
2407 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002408 ra_index = btrfs_force_ra(inode->i_mapping,
2409 &file->f_ra,
2410 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002411 }
2412 page = grab_cache_page(inode->i_mapping, i);
2413 if (!page)
2414 goto out_unlock;
2415 if (!PageUptodate(page)) {
2416 btrfs_readpage(NULL, page);
2417 lock_page(page);
2418 if (!PageUptodate(page)) {
2419 unlock_page(page);
2420 page_cache_release(page);
2421 goto out_unlock;
2422 }
2423 }
Chris Mason35ebb932007-10-30 16:56:53 -04002424 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002425 page_end = page_start + PAGE_CACHE_SIZE - 1;
2426
Chris Masond1310b22008-01-24 16:13:08 -05002427 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002428 delalloc_start = page_start;
2429 existing_delalloc =
Chris Masond1310b22008-01-24 16:13:08 -05002430 count_range_bits(&BTRFS_I(inode)->io_tree,
Chris Masonedbd8d42007-12-21 16:27:24 -05002431 &delalloc_start, page_end,
2432 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masond1310b22008-01-24 16:13:08 -05002433 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002434 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002435
2436 spin_lock(&root->fs_info->delalloc_lock);
2437 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2438 existing_delalloc;
2439 spin_unlock(&root->fs_info->delalloc_lock);
2440
Chris Masond1310b22008-01-24 16:13:08 -05002441 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002442 set_page_dirty(page);
2443 unlock_page(page);
2444 page_cache_release(page);
2445 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2446 }
2447
2448out_unlock:
2449 mutex_unlock(&inode->i_mutex);
2450 return 0;
2451}
2452
Chris Masonedbd8d42007-12-21 16:27:24 -05002453static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2454{
2455 u64 new_size;
2456 u64 old_size;
2457 struct btrfs_ioctl_vol_args *vol_args;
2458 struct btrfs_trans_handle *trans;
2459 char *sizestr;
2460 int ret = 0;
2461 int namelen;
2462 int mod = 0;
2463
2464 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2465
2466 if (!vol_args)
2467 return -ENOMEM;
2468
2469 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2470 ret = -EFAULT;
2471 goto out;
2472 }
2473 namelen = strlen(vol_args->name);
2474 if (namelen > BTRFS_VOL_NAME_MAX) {
2475 ret = -EINVAL;
2476 goto out;
2477 }
2478
2479 sizestr = vol_args->name;
2480 if (!strcmp(sizestr, "max"))
2481 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2482 else {
2483 if (sizestr[0] == '-') {
2484 mod = -1;
2485 sizestr++;
2486 } else if (sizestr[0] == '+') {
2487 mod = 1;
2488 sizestr++;
2489 }
2490 new_size = btrfs_parse_size(sizestr);
2491 if (new_size == 0) {
2492 ret = -EINVAL;
2493 goto out;
2494 }
2495 }
2496
2497 mutex_lock(&root->fs_info->fs_mutex);
2498 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2499
2500 if (mod < 0) {
2501 if (new_size > old_size) {
2502 ret = -EINVAL;
2503 goto out_unlock;
2504 }
2505 new_size = old_size - new_size;
2506 } else if (mod > 0) {
2507 new_size = old_size + new_size;
2508 }
2509
2510 if (new_size < 256 * 1024 * 1024) {
2511 ret = -EINVAL;
2512 goto out_unlock;
2513 }
2514 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2515 ret = -EFBIG;
2516 goto out_unlock;
2517 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002518
2519 do_div(new_size, root->sectorsize);
2520 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002521
2522printk("new size is %Lu\n", new_size);
2523 if (new_size > old_size) {
2524 trans = btrfs_start_transaction(root, 1);
2525 ret = btrfs_grow_extent_tree(trans, root, new_size);
2526 btrfs_commit_transaction(trans, root);
2527 } else {
2528 ret = btrfs_shrink_extent_tree(root, new_size);
2529 }
2530
2531out_unlock:
2532 mutex_unlock(&root->fs_info->fs_mutex);
2533out:
2534 kfree(vol_args);
2535 return ret;
2536}
2537
Chris Mason4313b392008-01-03 09:08:48 -05002538static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2539 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002540{
Chris Mason4aec2b52007-12-18 16:25:45 -05002541 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002542 struct btrfs_dir_item *di;
2543 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002544 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002545 int namelen;
2546 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002547
Chris Mason4aec2b52007-12-18 16:25:45 -05002548 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002549
Chris Mason4aec2b52007-12-18 16:25:45 -05002550 if (!vol_args)
2551 return -ENOMEM;
2552
2553 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2554 ret = -EFAULT;
2555 goto out;
2556 }
2557
2558 namelen = strlen(vol_args->name);
2559 if (namelen > BTRFS_VOL_NAME_MAX) {
2560 ret = -EINVAL;
2561 goto out;
2562 }
2563 if (strchr(vol_args->name, '/')) {
2564 ret = -EINVAL;
2565 goto out;
2566 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002567
2568 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002569 if (!path) {
2570 ret = -ENOMEM;
2571 goto out;
2572 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002573
2574 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2575 mutex_lock(&root->fs_info->fs_mutex);
2576 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2577 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002578 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002579 mutex_unlock(&root->fs_info->fs_mutex);
2580 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002581
2582 if (di && !IS_ERR(di)) {
2583 ret = -EEXIST;
2584 goto out;
2585 }
2586
2587 if (IS_ERR(di)) {
2588 ret = PTR_ERR(di);
2589 goto out;
2590 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002591
2592 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002593 ret = create_subvol(root, vol_args->name, namelen);
2594 else
2595 ret = create_snapshot(root, vol_args->name, namelen);
2596out:
2597 kfree(vol_args);
2598 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002599}
2600
2601static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002602{
Chris Mason6da6aba2007-12-18 16:15:09 -05002603 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002604 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002605
2606 switch (inode->i_mode & S_IFMT) {
2607 case S_IFDIR:
2608 mutex_lock(&root->fs_info->fs_mutex);
2609 btrfs_defrag_root(root, 0);
2610 btrfs_defrag_root(root->fs_info->extent_root, 0);
2611 mutex_unlock(&root->fs_info->fs_mutex);
2612 break;
2613 case S_IFREG:
2614 btrfs_defrag_file(file);
2615 break;
2616 }
2617
2618 return 0;
2619}
2620
2621long btrfs_ioctl(struct file *file, unsigned int
2622 cmd, unsigned long arg)
2623{
Chris Mason6da6aba2007-12-18 16:15:09 -05002624 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002625
2626 switch (cmd) {
2627 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002628 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002629 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002630 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002631 case BTRFS_IOC_RESIZE:
2632 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002633 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002634
2635 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002636}
2637
Chris Mason39279cc2007-06-12 06:35:45 -04002638/*
2639 * Called inside transaction, so use GFP_NOFS
2640 */
2641struct inode *btrfs_alloc_inode(struct super_block *sb)
2642{
2643 struct btrfs_inode *ei;
2644
2645 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2646 if (!ei)
2647 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002648 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002649 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002650 return &ei->vfs_inode;
2651}
2652
2653void btrfs_destroy_inode(struct inode *inode)
2654{
2655 WARN_ON(!list_empty(&inode->i_dentry));
2656 WARN_ON(inode->i_data.nrpages);
2657
Chris Mason8c416c92008-01-14 15:10:26 -05002658 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002659 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2660}
2661
Chris Mason44ec0b72007-10-29 10:55:05 -04002662#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2663static void init_once(struct kmem_cache * cachep, void *foo)
2664#else
Chris Mason39279cc2007-06-12 06:35:45 -04002665static void init_once(void * foo, struct kmem_cache * cachep,
2666 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002667#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002668{
2669 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2670
2671 inode_init_once(&ei->vfs_inode);
2672}
2673
2674void btrfs_destroy_cachep(void)
2675{
2676 if (btrfs_inode_cachep)
2677 kmem_cache_destroy(btrfs_inode_cachep);
2678 if (btrfs_trans_handle_cachep)
2679 kmem_cache_destroy(btrfs_trans_handle_cachep);
2680 if (btrfs_transaction_cachep)
2681 kmem_cache_destroy(btrfs_transaction_cachep);
2682 if (btrfs_bit_radix_cachep)
2683 kmem_cache_destroy(btrfs_bit_radix_cachep);
2684 if (btrfs_path_cachep)
2685 kmem_cache_destroy(btrfs_path_cachep);
2686}
2687
Chris Mason86479a02007-09-10 19:58:16 -04002688struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002689 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002690#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2691 void (*ctor)(struct kmem_cache *, void *)
2692#else
Chris Mason92fee662007-07-25 12:31:35 -04002693 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002694 unsigned long)
2695#endif
2696 )
Chris Mason92fee662007-07-25 12:31:35 -04002697{
2698 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2699 SLAB_MEM_SPREAD | extra_flags), ctor
2700#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2701 ,NULL
2702#endif
2703 );
2704}
2705
Chris Mason39279cc2007-06-12 06:35:45 -04002706int btrfs_init_cachep(void)
2707{
Chris Mason86479a02007-09-10 19:58:16 -04002708 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002709 sizeof(struct btrfs_inode),
2710 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002711 if (!btrfs_inode_cachep)
2712 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002713 btrfs_trans_handle_cachep =
2714 btrfs_cache_create("btrfs_trans_handle_cache",
2715 sizeof(struct btrfs_trans_handle),
2716 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002717 if (!btrfs_trans_handle_cachep)
2718 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002719 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002720 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002721 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002722 if (!btrfs_transaction_cachep)
2723 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002724 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002725 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002726 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002727 if (!btrfs_path_cachep)
2728 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002729 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002730 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002731 if (!btrfs_bit_radix_cachep)
2732 goto fail;
2733 return 0;
2734fail:
2735 btrfs_destroy_cachep();
2736 return -ENOMEM;
2737}
2738
2739static int btrfs_getattr(struct vfsmount *mnt,
2740 struct dentry *dentry, struct kstat *stat)
2741{
2742 struct inode *inode = dentry->d_inode;
2743 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002744 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002745 return 0;
2746}
2747
2748static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2749 struct inode * new_dir,struct dentry *new_dentry)
2750{
2751 struct btrfs_trans_handle *trans;
2752 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2753 struct inode *new_inode = new_dentry->d_inode;
2754 struct inode *old_inode = old_dentry->d_inode;
2755 struct timespec ctime = CURRENT_TIME;
2756 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002757 int ret;
2758
2759 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2760 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2761 return -ENOTEMPTY;
2762 }
Chris Mason5f39d392007-10-15 16:14:19 -04002763
Chris Mason39279cc2007-06-12 06:35:45 -04002764 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002765 ret = btrfs_check_free_space(root, 1, 0);
2766 if (ret)
2767 goto out_unlock;
2768
Chris Mason39279cc2007-06-12 06:35:45 -04002769 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002770
Chris Mason39279cc2007-06-12 06:35:45 -04002771 btrfs_set_trans_block_group(trans, new_dir);
2772 path = btrfs_alloc_path();
2773 if (!path) {
2774 ret = -ENOMEM;
2775 goto out_fail;
2776 }
2777
2778 old_dentry->d_inode->i_nlink++;
2779 old_dir->i_ctime = old_dir->i_mtime = ctime;
2780 new_dir->i_ctime = new_dir->i_mtime = ctime;
2781 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002782
Chris Mason39279cc2007-06-12 06:35:45 -04002783 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2784 if (ret)
2785 goto out_fail;
2786
2787 if (new_inode) {
2788 new_inode->i_ctime = CURRENT_TIME;
2789 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2790 if (ret)
2791 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002792 }
2793 ret = btrfs_add_link(trans, new_dentry, old_inode);
2794 if (ret)
2795 goto out_fail;
2796
2797out_fail:
2798 btrfs_free_path(path);
2799 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002800out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002801 mutex_unlock(&root->fs_info->fs_mutex);
2802 return ret;
2803}
2804
2805static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2806 const char *symname)
2807{
2808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(dir)->root;
2810 struct btrfs_path *path;
2811 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002812 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002813 int err;
2814 int drop_inode = 0;
2815 u64 objectid;
2816 int name_len;
2817 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002818 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002819 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002820 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002821 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002822
2823 name_len = strlen(symname) + 1;
2824 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2825 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002826
Chris Mason39279cc2007-06-12 06:35:45 -04002827 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002828 err = btrfs_check_free_space(root, 1, 0);
2829 if (err)
2830 goto out_fail;
2831
Chris Mason39279cc2007-06-12 06:35:45 -04002832 trans = btrfs_start_transaction(root, 1);
2833 btrfs_set_trans_block_group(trans, dir);
2834
2835 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2836 if (err) {
2837 err = -ENOSPC;
2838 goto out_unlock;
2839 }
2840
2841 inode = btrfs_new_inode(trans, root, objectid,
2842 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2843 err = PTR_ERR(inode);
2844 if (IS_ERR(inode))
2845 goto out_unlock;
2846
2847 btrfs_set_trans_block_group(trans, inode);
2848 err = btrfs_add_nondir(trans, dentry, inode);
2849 if (err)
2850 drop_inode = 1;
2851 else {
2852 inode->i_mapping->a_ops = &btrfs_aops;
2853 inode->i_fop = &btrfs_file_operations;
2854 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002855 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2856 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002857 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002858 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002859 }
2860 dir->i_sb->s_dirt = 1;
2861 btrfs_update_inode_block_group(trans, inode);
2862 btrfs_update_inode_block_group(trans, dir);
2863 if (drop_inode)
2864 goto out_unlock;
2865
2866 path = btrfs_alloc_path();
2867 BUG_ON(!path);
2868 key.objectid = inode->i_ino;
2869 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2871 datasize = btrfs_file_extent_calc_inline_size(name_len);
2872 err = btrfs_insert_empty_item(trans, root, path, &key,
2873 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002874 if (err) {
2875 drop_inode = 1;
2876 goto out_unlock;
2877 }
Chris Mason5f39d392007-10-15 16:14:19 -04002878 leaf = path->nodes[0];
2879 ei = btrfs_item_ptr(leaf, path->slots[0],
2880 struct btrfs_file_extent_item);
2881 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2882 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002883 BTRFS_FILE_EXTENT_INLINE);
2884 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002885 write_extent_buffer(leaf, symname, ptr, name_len);
2886 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002887 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002888
Chris Mason39279cc2007-06-12 06:35:45 -04002889 inode->i_op = &btrfs_symlink_inode_operations;
2890 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2891 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002892 err = btrfs_update_inode(trans, root, inode);
2893 if (err)
2894 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002895
2896out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002897 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002898 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002899out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002900 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002901 if (drop_inode) {
2902 inode_dec_link_count(inode);
2903 iput(inode);
2904 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002905 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002906 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002907 return err;
2908}
Yanfdebe2b2008-01-14 13:26:08 -05002909static int btrfs_permission(struct inode *inode, int mask,
2910 struct nameidata *nd)
2911{
2912 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2913 return -EACCES;
2914 return generic_permission(inode, mask, NULL);
2915}
Chris Mason39279cc2007-06-12 06:35:45 -04002916
2917static struct inode_operations btrfs_dir_inode_operations = {
2918 .lookup = btrfs_lookup,
2919 .create = btrfs_create,
2920 .unlink = btrfs_unlink,
2921 .link = btrfs_link,
2922 .mkdir = btrfs_mkdir,
2923 .rmdir = btrfs_rmdir,
2924 .rename = btrfs_rename,
2925 .symlink = btrfs_symlink,
2926 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002927 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002928 .setxattr = generic_setxattr,
2929 .getxattr = generic_getxattr,
2930 .listxattr = btrfs_listxattr,
2931 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002932 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002933};
Chris Mason39279cc2007-06-12 06:35:45 -04002934static struct inode_operations btrfs_dir_ro_inode_operations = {
2935 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002936 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002937};
Chris Mason39279cc2007-06-12 06:35:45 -04002938static struct file_operations btrfs_dir_file_operations = {
2939 .llseek = generic_file_llseek,
2940 .read = generic_read_dir,
2941 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002942 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002943#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002944 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002945#endif
2946};
2947
Chris Masond1310b22008-01-24 16:13:08 -05002948static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04002949 .fill_delalloc = run_delalloc_range,
2950 .writepage_io_hook = btrfs_writepage_io_hook,
2951 .readpage_io_hook = btrfs_readpage_io_hook,
2952 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2953};
2954
Chris Mason39279cc2007-06-12 06:35:45 -04002955static struct address_space_operations btrfs_aops = {
2956 .readpage = btrfs_readpage,
2957 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04002958 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002959 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002960 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002961 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002962 .invalidatepage = btrfs_invalidatepage,
2963 .releasepage = btrfs_releasepage,
2964 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002965};
2966
2967static struct address_space_operations btrfs_symlink_aops = {
2968 .readpage = btrfs_readpage,
2969 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002970 .invalidatepage = btrfs_invalidatepage,
2971 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002972};
2973
2974static struct inode_operations btrfs_file_inode_operations = {
2975 .truncate = btrfs_truncate,
2976 .getattr = btrfs_getattr,
2977 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002978 .setxattr = generic_setxattr,
2979 .getxattr = generic_getxattr,
2980 .listxattr = btrfs_listxattr,
2981 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002982 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002983};
Josef Bacik618e21d2007-07-11 10:18:17 -04002984static struct inode_operations btrfs_special_inode_operations = {
2985 .getattr = btrfs_getattr,
2986 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002987 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002988};
Chris Mason39279cc2007-06-12 06:35:45 -04002989static struct inode_operations btrfs_symlink_inode_operations = {
2990 .readlink = generic_readlink,
2991 .follow_link = page_follow_link_light,
2992 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05002993 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002994};