blob: bac8722e14e1938e0c3640a0718773fde913976b [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
Chris Mason70dec802008-01-29 09:59:12 -0500334int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
335 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400336{
Chris Mason35ebb932007-10-30 16:56:53 -0400337 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400338 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500339 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400340 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500341 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400342 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400343 struct btrfs_root *root = BTRFS_I(inode)->root;
344 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400345 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500346
Yanb98b6762008-01-08 15:54:37 -0500347 if (btrfs_test_opt(root, NODATASUM) ||
348 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500349 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500350
Chris Mason70dec802008-01-29 09:59:12 -0500351 if (state->start == start) {
352 private = state->private;
353 ret = 0;
354 } else {
355 ret = get_state_private(io_tree, start, &private);
356 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400357 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400358 kaddr = kmap_atomic(page, KM_IRQ0);
359 if (ret) {
360 goto zeroit;
361 }
Chris Masonff79f812007-10-15 16:22:25 -0400362 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
363 btrfs_csum_final(csum, (char *)&csum);
364 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400365 goto zeroit;
366 }
367 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400368 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400369 return 0;
370
371zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500372 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
373 page->mapping->host->i_ino, (unsigned long long)start, csum,
374 private);
Chris Masondb945352007-10-15 16:15:53 -0400375 memset(kaddr + offset, 1, end - start + 1);
376 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400377 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400378 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400379 return 0;
380}
Chris Masonb888db22007-08-27 16:49:44 -0400381
Chris Mason39279cc2007-06-12 06:35:45 -0400382void btrfs_read_locked_inode(struct inode *inode)
383{
384 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400385 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400386 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400387 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400388 struct btrfs_root *root = BTRFS_I(inode)->root;
389 struct btrfs_key location;
390 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400391 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400392 int ret;
393
394 path = btrfs_alloc_path();
395 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400396 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400397 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500398
Chris Mason39279cc2007-06-12 06:35:45 -0400399 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400400 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400401 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400402
Chris Mason5f39d392007-10-15 16:14:19 -0400403 leaf = path->nodes[0];
404 inode_item = btrfs_item_ptr(leaf, path->slots[0],
405 struct btrfs_inode_item);
406
407 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
408 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
409 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
410 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
411 inode->i_size = btrfs_inode_size(leaf, inode_item);
412
413 tspec = btrfs_inode_atime(inode_item);
414 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
415 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
416
417 tspec = btrfs_inode_mtime(inode_item);
418 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
419 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
420
421 tspec = btrfs_inode_ctime(inode_item);
422 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
423 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
424
425 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
426 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400427 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400428 rdev = btrfs_inode_rdev(leaf, inode_item);
429
430 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400431 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
432 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500433 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500434 if (!BTRFS_I(inode)->block_group) {
435 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
436 NULL, 0, 0, 0);
437 }
Chris Mason39279cc2007-06-12 06:35:45 -0400438 btrfs_free_path(path);
439 inode_item = NULL;
440
441 mutex_unlock(&root->fs_info->fs_mutex);
442
443 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400444 case S_IFREG:
445 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500446 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400447 inode->i_fop = &btrfs_file_operations;
448 inode->i_op = &btrfs_file_inode_operations;
449 break;
450 case S_IFDIR:
451 inode->i_fop = &btrfs_dir_file_operations;
452 if (root == root->fs_info->tree_root)
453 inode->i_op = &btrfs_dir_ro_inode_operations;
454 else
455 inode->i_op = &btrfs_dir_inode_operations;
456 break;
457 case S_IFLNK:
458 inode->i_op = &btrfs_symlink_inode_operations;
459 inode->i_mapping->a_ops = &btrfs_symlink_aops;
460 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400461 default:
462 init_special_inode(inode, inode->i_mode, rdev);
463 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400464 }
465 return;
466
467make_bad:
468 btrfs_release_path(root, path);
469 btrfs_free_path(path);
470 mutex_unlock(&root->fs_info->fs_mutex);
471 make_bad_inode(inode);
472}
473
Chris Mason5f39d392007-10-15 16:14:19 -0400474static void fill_inode_item(struct extent_buffer *leaf,
475 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400476 struct inode *inode)
477{
Chris Mason5f39d392007-10-15 16:14:19 -0400478 btrfs_set_inode_uid(leaf, item, inode->i_uid);
479 btrfs_set_inode_gid(leaf, item, inode->i_gid);
480 btrfs_set_inode_size(leaf, item, inode->i_size);
481 btrfs_set_inode_mode(leaf, item, inode->i_mode);
482 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
483
484 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
485 inode->i_atime.tv_sec);
486 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
487 inode->i_atime.tv_nsec);
488
489 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
490 inode->i_mtime.tv_sec);
491 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
492 inode->i_mtime.tv_nsec);
493
494 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
495 inode->i_ctime.tv_sec);
496 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
497 inode->i_ctime.tv_nsec);
498
499 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
500 btrfs_set_inode_generation(leaf, item, inode->i_generation);
501 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500502 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400503 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400504 BTRFS_I(inode)->block_group->key.objectid);
505}
506
Chris Masona52d9a82007-08-27 16:49:44 -0400507int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400508 struct btrfs_root *root,
509 struct inode *inode)
510{
511 struct btrfs_inode_item *inode_item;
512 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400513 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400514 int ret;
515
516 path = btrfs_alloc_path();
517 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400518 ret = btrfs_lookup_inode(trans, root, path,
519 &BTRFS_I(inode)->location, 1);
520 if (ret) {
521 if (ret > 0)
522 ret = -ENOENT;
523 goto failed;
524 }
525
Chris Mason5f39d392007-10-15 16:14:19 -0400526 leaf = path->nodes[0];
527 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400528 struct btrfs_inode_item);
529
Chris Mason5f39d392007-10-15 16:14:19 -0400530 fill_inode_item(leaf, inode_item, inode);
531 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400532 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400533 ret = 0;
534failed:
535 btrfs_release_path(root, path);
536 btrfs_free_path(path);
537 return ret;
538}
539
540
541static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
542 struct btrfs_root *root,
543 struct inode *dir,
544 struct dentry *dentry)
545{
546 struct btrfs_path *path;
547 const char *name = dentry->d_name.name;
548 int name_len = dentry->d_name.len;
549 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400550 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400551 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400552 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400553
554 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400555 if (!path) {
556 ret = -ENOMEM;
557 goto err;
558 }
559
Chris Mason39279cc2007-06-12 06:35:45 -0400560 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
561 name, name_len, -1);
562 if (IS_ERR(di)) {
563 ret = PTR_ERR(di);
564 goto err;
565 }
566 if (!di) {
567 ret = -ENOENT;
568 goto err;
569 }
Chris Mason5f39d392007-10-15 16:14:19 -0400570 leaf = path->nodes[0];
571 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400572 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400573 if (ret)
574 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400575 btrfs_release_path(root, path);
576
577 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400578 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400579 if (IS_ERR(di)) {
580 ret = PTR_ERR(di);
581 goto err;
582 }
583 if (!di) {
584 ret = -ENOENT;
585 goto err;
586 }
587 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400588
589 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500590 ret = btrfs_del_inode_ref(trans, root, name, name_len,
591 dentry->d_inode->i_ino,
592 dentry->d_parent->d_inode->i_ino);
593 if (ret) {
594 printk("failed to delete reference to %.*s, "
595 "inode %lu parent %lu\n", name_len, name,
596 dentry->d_inode->i_ino,
597 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500598 }
Chris Mason39279cc2007-06-12 06:35:45 -0400599err:
600 btrfs_free_path(path);
601 if (!ret) {
602 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400603 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400604 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500605#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
606 dentry->d_inode->i_nlink--;
607#else
Chris Mason39279cc2007-06-12 06:35:45 -0400608 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500609#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400610 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400611 dir->i_sb->s_dirt = 1;
612 }
613 return ret;
614}
615
616static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
617{
618 struct btrfs_root *root;
619 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500620 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400621 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500622 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400623
624 root = BTRFS_I(dir)->root;
625 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500626
627 ret = btrfs_check_free_space(root, 1, 1);
628 if (ret)
629 goto fail;
630
Chris Mason39279cc2007-06-12 06:35:45 -0400631 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400632
Chris Mason39279cc2007-06-12 06:35:45 -0400633 btrfs_set_trans_block_group(trans, dir);
634 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400635 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400636
Chris Mason2da98f02008-01-16 11:44:43 -0500637 if (inode->i_nlink == 0) {
638 int found;
639 /* if the inode isn't linked anywhere,
640 * we don't need to worry about
641 * data=ordered
642 */
643 found = btrfs_del_ordered_inode(inode);
644 if (found == 1) {
645 atomic_dec(&inode->i_count);
646 }
647 }
648
Chris Mason39279cc2007-06-12 06:35:45 -0400649 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500650fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400651 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400652 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500653 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400654 return ret;
655}
656
657static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
658{
659 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500660 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400661 int ret;
662 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400663 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500664 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400665
Yan134d4512007-10-25 15:49:25 -0400666 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
667 return -ENOTEMPTY;
668
Chris Mason39279cc2007-06-12 06:35:45 -0400669 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500670 ret = btrfs_check_free_space(root, 1, 1);
671 if (ret)
672 goto fail;
673
Chris Mason39279cc2007-06-12 06:35:45 -0400674 trans = btrfs_start_transaction(root, 1);
675 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400676
677 /* now the directory is empty */
678 err = btrfs_unlink_trans(trans, root, dir, dentry);
679 if (!err) {
680 inode->i_size = 0;
681 }
Chris Mason39544012007-12-12 14:38:19 -0500682
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400683 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400684 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500685fail:
Yan134d4512007-10-25 15:49:25 -0400686 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400687 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500688 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500689
Chris Mason39279cc2007-06-12 06:35:45 -0400690 if (ret && !err)
691 err = ret;
692 return err;
693}
694
695static int btrfs_free_inode(struct btrfs_trans_handle *trans,
696 struct btrfs_root *root,
697 struct inode *inode)
698{
699 struct btrfs_path *path;
700 int ret;
701
702 clear_inode(inode);
703
704 path = btrfs_alloc_path();
705 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400706 ret = btrfs_lookup_inode(trans, root, path,
707 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400708 if (ret > 0)
709 ret = -ENOENT;
710 if (!ret)
711 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400712 btrfs_free_path(path);
713 return ret;
714}
715
716/*
Chris Mason39279cc2007-06-12 06:35:45 -0400717 * this can truncate away extent items, csum items and directory items.
718 * It starts at a high offset and removes keys until it can't find
719 * any higher than i_size.
720 *
721 * csum items that cross the new i_size are truncated to the new size
722 * as well.
723 */
724static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
725 struct btrfs_root *root,
726 struct inode *inode)
727{
728 int ret;
729 struct btrfs_path *path;
730 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400731 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400732 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400733 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400734 struct btrfs_file_extent_item *fi;
735 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400736 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400737 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500738 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500739 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400740 int found_extent;
741 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400742 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400743
Chris Masona52d9a82007-08-27 16:49:44 -0400744 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400745 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400746 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400747 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400748
Chris Mason39279cc2007-06-12 06:35:45 -0400749 /* FIXME, add redo link to tree so we don't leak on crash */
750 key.objectid = inode->i_ino;
751 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400752 key.type = (u8)-1;
753
Chris Mason39279cc2007-06-12 06:35:45 -0400754 while(1) {
755 btrfs_init_path(path);
756 fi = NULL;
757 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
758 if (ret < 0) {
759 goto error;
760 }
761 if (ret > 0) {
762 BUG_ON(path->slots[0] == 0);
763 path->slots[0]--;
764 }
Chris Mason5f39d392007-10-15 16:14:19 -0400765 leaf = path->nodes[0];
766 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
767 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400768
Chris Mason5f39d392007-10-15 16:14:19 -0400769 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400770 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400771
Chris Mason39279cc2007-06-12 06:35:45 -0400772 if (found_type != BTRFS_CSUM_ITEM_KEY &&
773 found_type != BTRFS_DIR_ITEM_KEY &&
774 found_type != BTRFS_DIR_INDEX_KEY &&
775 found_type != BTRFS_EXTENT_DATA_KEY)
776 break;
777
Chris Mason5f39d392007-10-15 16:14:19 -0400778 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400779 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400780 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400781 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400782 extent_type = btrfs_file_extent_type(leaf, fi);
783 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400784 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400785 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400786 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
787 struct btrfs_item *item = btrfs_item_nr(leaf,
788 path->slots[0]);
789 item_end += btrfs_file_extent_inline_len(leaf,
790 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400791 }
Yan008630c2007-11-07 13:31:09 -0500792 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400793 }
794 if (found_type == BTRFS_CSUM_ITEM_KEY) {
795 ret = btrfs_csum_truncate(trans, root, path,
796 inode->i_size);
797 BUG_ON(ret);
798 }
Yan008630c2007-11-07 13:31:09 -0500799 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400800 if (found_type == BTRFS_DIR_ITEM_KEY) {
801 found_type = BTRFS_INODE_ITEM_KEY;
802 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
803 found_type = BTRFS_CSUM_ITEM_KEY;
804 } else if (found_type) {
805 found_type--;
806 } else {
807 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400808 }
Yana61721d2007-09-17 11:08:38 -0400809 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400810 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400811 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400812 }
Chris Mason5f39d392007-10-15 16:14:19 -0400813 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400814 del_item = 1;
815 else
816 del_item = 0;
817 found_extent = 0;
818
819 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400820 if (found_type != BTRFS_EXTENT_DATA_KEY)
821 goto delete;
822
823 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400824 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400825 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400826 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400827 u64 orig_num_bytes =
828 btrfs_file_extent_num_bytes(leaf, fi);
829 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400830 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400831 btrfs_set_file_extent_num_bytes(leaf, fi,
832 extent_num_bytes);
833 num_dec = (orig_num_bytes -
834 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400835 if (extent_start != 0) {
836 inode->i_blocks -= num_dec;
837 }
Chris Mason5f39d392007-10-15 16:14:19 -0400838 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400839 } else {
Chris Masondb945352007-10-15 16:15:53 -0400840 extent_num_bytes =
841 btrfs_file_extent_disk_num_bytes(leaf,
842 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400843 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400844 num_dec = btrfs_file_extent_num_bytes(leaf,
845 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400846 if (extent_start != 0) {
847 found_extent = 1;
848 inode->i_blocks -= num_dec;
849 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500850 root_gen = btrfs_header_generation(leaf);
851 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400852 }
Chris Mason179e29e2007-11-01 11:28:41 -0400853 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
854 !del_item) {
855 u32 newsize = inode->i_size - found_key.offset;
856 newsize = btrfs_file_extent_calc_inline_size(newsize);
857 ret = btrfs_truncate_item(trans, root, path,
858 newsize, 1);
859 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400860 }
Chris Mason179e29e2007-11-01 11:28:41 -0400861delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400862 if (del_item) {
863 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400864 if (ret)
865 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400866 } else {
867 break;
868 }
869 btrfs_release_path(root, path);
870 if (found_extent) {
871 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500872 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500873 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500874 root_gen, inode->i_ino,
875 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400876 BUG_ON(ret);
877 }
878 }
879 ret = 0;
880error:
881 btrfs_release_path(root, path);
882 btrfs_free_path(path);
883 inode->i_sb->s_dirt = 1;
884 return ret;
885}
886
Chris Masonb888db22007-08-27 16:49:44 -0400887static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400888 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400889{
Chris Mason39279cc2007-06-12 06:35:45 -0400890 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500891 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason1832a6d2007-12-21 16:27:21 -0500892 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason35ebb932007-10-30 16:56:53 -0400893 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400894 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500895 u64 existing_delalloc;
896 u64 delalloc_start;
897 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400898
Chris Mason190662b2007-12-18 16:25:45 -0500899 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400900 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400901
Chris Masond1310b22008-01-24 16:13:08 -0500902 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500903 delalloc_start = page_start;
Chris Masond1310b22008-01-24 16:13:08 -0500904 existing_delalloc = count_range_bits(&BTRFS_I(inode)->io_tree,
Chris Mason1832a6d2007-12-21 16:27:21 -0500905 &delalloc_start, page_end,
906 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masond1310b22008-01-24 16:13:08 -0500907 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400908 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500909
910 spin_lock(&root->fs_info->delalloc_lock);
911 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
912 spin_unlock(&root->fs_info->delalloc_lock);
913
Chris Masona52d9a82007-08-27 16:49:44 -0400914 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400915 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400916 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
917 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400918 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400919 }
Chris Masonb888db22007-08-27 16:49:44 -0400920 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500921 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400922
Chris Masona52d9a82007-08-27 16:49:44 -0400923 return ret;
924}
925
926/*
927 * taken from block_truncate_page, but does cow as it zeros out
928 * any bytes left in the last page in the file.
929 */
930static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
931{
932 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400933 struct btrfs_root *root = BTRFS_I(inode)->root;
934 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400935 pgoff_t index = from >> PAGE_CACHE_SHIFT;
936 unsigned offset = from & (PAGE_CACHE_SIZE-1);
937 struct page *page;
938 int ret = 0;
939 u64 page_start;
940
941 if ((offset & (blocksize - 1)) == 0)
942 goto out;
943
944 ret = -ENOMEM;
945 page = grab_cache_page(mapping, index);
946 if (!page)
947 goto out;
948 if (!PageUptodate(page)) {
949 ret = btrfs_readpage(NULL, page);
950 lock_page(page);
951 if (!PageUptodate(page)) {
952 ret = -EIO;
953 goto out;
954 }
955 }
Chris Mason35ebb932007-10-30 16:56:53 -0400956 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400957
Chris Masonb888db22007-08-27 16:49:44 -0400958 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400959
Chris Mason39279cc2007-06-12 06:35:45 -0400960 unlock_page(page);
961 page_cache_release(page);
962out:
963 return ret;
964}
965
966static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
967{
968 struct inode *inode = dentry->d_inode;
969 int err;
970
971 err = inode_change_ok(inode, attr);
972 if (err)
973 return err;
974
975 if (S_ISREG(inode->i_mode) &&
976 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
977 struct btrfs_trans_handle *trans;
978 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500979 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -0400980
Chris Mason5f39d392007-10-15 16:14:19 -0400981 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400982 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400983 u64 block_end = attr->ia_size | mask;
Chris Mason5f564062008-01-22 16:47:59 -0500984 u64 hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400985 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400986 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400987
988 if (attr->ia_size <= pos)
989 goto out;
990
Chris Mason5f564062008-01-22 16:47:59 -0500991 if (pos != inode->i_size)
992 hole_start = pos + root->sectorsize;
993 else
994 hole_start = pos;
995
Chris Mason1832a6d2007-12-21 16:27:21 -0500996 mutex_lock(&root->fs_info->fs_mutex);
997 err = btrfs_check_free_space(root, 1, 0);
998 mutex_unlock(&root->fs_info->fs_mutex);
999 if (err)
1000 goto fail;
1001
Chris Mason39279cc2007-06-12 06:35:45 -04001002 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1003
Chris Masond1310b22008-01-24 16:13:08 -05001004 lock_extent(io_tree, pos, block_end, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001005 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001006
1007 mutex_lock(&root->fs_info->fs_mutex);
1008 trans = btrfs_start_transaction(root, 1);
1009 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001010 err = btrfs_drop_extents(trans, root, inode,
Chris Mason5f564062008-01-22 16:47:59 -05001011 pos, block_end, pos,
Chris Mason3326d1b2007-10-15 16:18:25 -04001012 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001013
Chris Mason179e29e2007-11-01 11:28:41 -04001014 if (alloc_hint != EXTENT_MAP_INLINE) {
1015 err = btrfs_insert_file_extent(trans, root,
1016 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001017 hole_start, 0, 0,
1018 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001019 btrfs_drop_extent_cache(inode, hole_start,
1020 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001021 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001022 }
Chris Mason39279cc2007-06-12 06:35:45 -04001023 btrfs_end_transaction(trans, root);
1024 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond1310b22008-01-24 16:13:08 -05001025 unlock_extent(io_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001026 if (err)
1027 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001028 }
1029out:
1030 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001031fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001032 return err;
1033}
Chris Mason61295eb2008-01-14 16:24:38 -05001034
Chris Mason2da98f02008-01-16 11:44:43 -05001035void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001036{
Chris Mason2da98f02008-01-16 11:44:43 -05001037 int ret;
1038
1039 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001040 return;
1041 }
Chris Mason2da98f02008-01-16 11:44:43 -05001042
1043 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1044 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1045 return;
1046
1047 ret = btrfs_del_ordered_inode(inode);
1048 if (ret == 1) {
1049 atomic_dec(&inode->i_count);
1050 }
Chris Mason61295eb2008-01-14 16:24:38 -05001051}
1052
Chris Mason39279cc2007-06-12 06:35:45 -04001053void btrfs_delete_inode(struct inode *inode)
1054{
1055 struct btrfs_trans_handle *trans;
1056 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001057 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001058 int ret;
1059
1060 truncate_inode_pages(&inode->i_data, 0);
1061 if (is_bad_inode(inode)) {
1062 goto no_delete;
1063 }
Chris Mason5f39d392007-10-15 16:14:19 -04001064
Chris Mason39279cc2007-06-12 06:35:45 -04001065 inode->i_size = 0;
1066 mutex_lock(&root->fs_info->fs_mutex);
1067 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001068
Chris Mason39279cc2007-06-12 06:35:45 -04001069 btrfs_set_trans_block_group(trans, inode);
1070 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001071 if (ret)
1072 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -05001073 ret = btrfs_delete_xattrs(trans, root, inode);
1074 if (ret)
1075 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -04001076 ret = btrfs_free_inode(trans, root, inode);
1077 if (ret)
1078 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001079 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001080
Chris Mason39279cc2007-06-12 06:35:45 -04001081 btrfs_end_transaction(trans, root);
1082 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001083 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001084 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001085 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001086
1087no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001088 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001089 btrfs_end_transaction(trans, root);
1090 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001091 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001092 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001093no_delete:
1094 clear_inode(inode);
1095}
1096
1097/*
1098 * this returns the key found in the dir entry in the location pointer.
1099 * If no dir entries were found, location->objectid is 0.
1100 */
1101static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1102 struct btrfs_key *location)
1103{
1104 const char *name = dentry->d_name.name;
1105 int namelen = dentry->d_name.len;
1106 struct btrfs_dir_item *di;
1107 struct btrfs_path *path;
1108 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001109 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001110
Chris Mason39544012007-12-12 14:38:19 -05001111 if (namelen == 1 && strcmp(name, ".") == 0) {
1112 location->objectid = dir->i_ino;
1113 location->type = BTRFS_INODE_ITEM_KEY;
1114 location->offset = 0;
1115 return 0;
1116 }
Chris Mason39279cc2007-06-12 06:35:45 -04001117 path = btrfs_alloc_path();
1118 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001119
Chris Mason7a720532007-12-13 09:06:59 -05001120 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001121 struct btrfs_key key;
1122 struct extent_buffer *leaf;
1123 u32 nritems;
1124 int slot;
1125
1126 key.objectid = dir->i_ino;
1127 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1128 key.offset = 0;
1129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1130 BUG_ON(ret == 0);
1131 ret = 0;
1132
1133 leaf = path->nodes[0];
1134 slot = path->slots[0];
1135 nritems = btrfs_header_nritems(leaf);
1136 if (slot >= nritems)
1137 goto out_err;
1138
1139 btrfs_item_key_to_cpu(leaf, &key, slot);
1140 if (key.objectid != dir->i_ino ||
1141 key.type != BTRFS_INODE_REF_KEY) {
1142 goto out_err;
1143 }
1144 location->objectid = key.offset;
1145 location->type = BTRFS_INODE_ITEM_KEY;
1146 location->offset = 0;
1147 goto out;
1148 }
1149
Chris Mason39279cc2007-06-12 06:35:45 -04001150 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1151 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001152 if (IS_ERR(di))
1153 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001154 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001155 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001156 }
Chris Mason5f39d392007-10-15 16:14:19 -04001157 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001158out:
Chris Mason39279cc2007-06-12 06:35:45 -04001159 btrfs_free_path(path);
1160 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001161out_err:
1162 location->objectid = 0;
1163 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001164}
1165
1166/*
1167 * when we hit a tree root in a directory, the btrfs part of the inode
1168 * needs to be changed to reflect the root directory of the tree root. This
1169 * is kind of like crossing a mount point.
1170 */
1171static int fixup_tree_root_location(struct btrfs_root *root,
1172 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001173 struct btrfs_root **sub_root,
1174 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001175{
1176 struct btrfs_path *path;
1177 struct btrfs_root_item *ri;
1178
1179 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1180 return 0;
1181 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1182 return 0;
1183
1184 path = btrfs_alloc_path();
1185 BUG_ON(!path);
1186 mutex_lock(&root->fs_info->fs_mutex);
1187
Josef Bacik58176a92007-08-29 15:47:34 -04001188 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1189 dentry->d_name.name,
1190 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001191 if (IS_ERR(*sub_root))
1192 return PTR_ERR(*sub_root);
1193
1194 ri = &(*sub_root)->root_item;
1195 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001196 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1197 location->offset = 0;
1198
1199 btrfs_free_path(path);
1200 mutex_unlock(&root->fs_info->fs_mutex);
1201 return 0;
1202}
1203
1204static int btrfs_init_locked_inode(struct inode *inode, void *p)
1205{
1206 struct btrfs_iget_args *args = p;
1207 inode->i_ino = args->ino;
1208 BTRFS_I(inode)->root = args->root;
Chris Masond1310b22008-01-24 16:13:08 -05001209 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1210 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001211 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001212 return 0;
1213}
1214
1215static int btrfs_find_actor(struct inode *inode, void *opaque)
1216{
1217 struct btrfs_iget_args *args = opaque;
1218 return (args->ino == inode->i_ino &&
1219 args->root == BTRFS_I(inode)->root);
1220}
1221
Chris Masondc17ff82008-01-08 15:46:30 -05001222struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1223 u64 root_objectid)
1224{
1225 struct btrfs_iget_args args;
1226 args.ino = objectid;
1227 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1228
1229 if (!args.root)
1230 return NULL;
1231
1232 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1233}
1234
Chris Mason39279cc2007-06-12 06:35:45 -04001235struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1236 struct btrfs_root *root)
1237{
1238 struct inode *inode;
1239 struct btrfs_iget_args args;
1240 args.ino = objectid;
1241 args.root = root;
1242
1243 inode = iget5_locked(s, objectid, btrfs_find_actor,
1244 btrfs_init_locked_inode,
1245 (void *)&args);
1246 return inode;
1247}
1248
1249static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1250 struct nameidata *nd)
1251{
1252 struct inode * inode;
1253 struct btrfs_inode *bi = BTRFS_I(dir);
1254 struct btrfs_root *root = bi->root;
1255 struct btrfs_root *sub_root = root;
1256 struct btrfs_key location;
1257 int ret;
1258
1259 if (dentry->d_name.len > BTRFS_NAME_LEN)
1260 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001261
Chris Mason39279cc2007-06-12 06:35:45 -04001262 mutex_lock(&root->fs_info->fs_mutex);
1263 ret = btrfs_inode_by_name(dir, dentry, &location);
1264 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001265
Chris Mason39279cc2007-06-12 06:35:45 -04001266 if (ret < 0)
1267 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001268
Chris Mason39279cc2007-06-12 06:35:45 -04001269 inode = NULL;
1270 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001271 ret = fixup_tree_root_location(root, &location, &sub_root,
1272 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001273 if (ret < 0)
1274 return ERR_PTR(ret);
1275 if (ret > 0)
1276 return ERR_PTR(-ENOENT);
1277 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1278 sub_root);
1279 if (!inode)
1280 return ERR_PTR(-EACCES);
1281 if (inode->i_state & I_NEW) {
1282 /* the inode and parent dir are two different roots */
1283 if (sub_root != root) {
1284 igrab(inode);
1285 sub_root->inode = inode;
1286 }
1287 BTRFS_I(inode)->root = sub_root;
1288 memcpy(&BTRFS_I(inode)->location, &location,
1289 sizeof(location));
1290 btrfs_read_locked_inode(inode);
1291 unlock_new_inode(inode);
1292 }
1293 }
1294 return d_splice_alias(inode, dentry);
1295}
1296
Chris Mason39279cc2007-06-12 06:35:45 -04001297static unsigned char btrfs_filetype_table[] = {
1298 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1299};
1300
1301static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1302{
Chris Mason6da6aba2007-12-18 16:15:09 -05001303 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001304 struct btrfs_root *root = BTRFS_I(inode)->root;
1305 struct btrfs_item *item;
1306 struct btrfs_dir_item *di;
1307 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001308 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001309 struct btrfs_path *path;
1310 int ret;
1311 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001312 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001313 int slot;
1314 int advance;
1315 unsigned char d_type;
1316 int over = 0;
1317 u32 di_cur;
1318 u32 di_total;
1319 u32 di_len;
1320 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001321 char tmp_name[32];
1322 char *name_ptr;
1323 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001324
1325 /* FIXME, use a real flag for deciding about the key type */
1326 if (root->fs_info->tree_root == root)
1327 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001328
Chris Mason39544012007-12-12 14:38:19 -05001329 /* special case for "." */
1330 if (filp->f_pos == 0) {
1331 over = filldir(dirent, ".", 1,
1332 1, inode->i_ino,
1333 DT_DIR);
1334 if (over)
1335 return 0;
1336 filp->f_pos = 1;
1337 }
1338
Chris Mason39279cc2007-06-12 06:35:45 -04001339 mutex_lock(&root->fs_info->fs_mutex);
1340 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001341 path = btrfs_alloc_path();
1342 path->reada = 2;
1343
1344 /* special case for .., just use the back ref */
1345 if (filp->f_pos == 1) {
1346 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1347 key.offset = 0;
1348 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1349 BUG_ON(ret == 0);
1350 leaf = path->nodes[0];
1351 slot = path->slots[0];
1352 nritems = btrfs_header_nritems(leaf);
1353 if (slot >= nritems) {
1354 btrfs_release_path(root, path);
1355 goto read_dir_items;
1356 }
1357 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1358 btrfs_release_path(root, path);
1359 if (found_key.objectid != key.objectid ||
1360 found_key.type != BTRFS_INODE_REF_KEY)
1361 goto read_dir_items;
1362 over = filldir(dirent, "..", 2,
1363 2, found_key.offset, DT_DIR);
1364 if (over)
1365 goto nopos;
1366 filp->f_pos = 2;
1367 }
1368
1369read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001370 btrfs_set_key_type(&key, key_type);
1371 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001372
Chris Mason39279cc2007-06-12 06:35:45 -04001373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1374 if (ret < 0)
1375 goto err;
1376 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001377 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001378 leaf = path->nodes[0];
1379 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001380 slot = path->slots[0];
1381 if (advance || slot >= nritems) {
1382 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001383 ret = btrfs_next_leaf(root, path);
1384 if (ret)
1385 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001386 leaf = path->nodes[0];
1387 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001388 slot = path->slots[0];
1389 } else {
1390 slot++;
1391 path->slots[0]++;
1392 }
1393 }
1394 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001395 item = btrfs_item_nr(leaf, slot);
1396 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1397
1398 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001399 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001400 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001401 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001402 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001403 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001404
1405 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001406 advance = 1;
1407 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1408 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001409 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001410 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001411 struct btrfs_key location;
1412
1413 name_len = btrfs_dir_name_len(leaf, di);
1414 if (name_len < 32) {
1415 name_ptr = tmp_name;
1416 } else {
1417 name_ptr = kmalloc(name_len, GFP_NOFS);
1418 BUG_ON(!name_ptr);
1419 }
1420 read_extent_buffer(leaf, name_ptr,
1421 (unsigned long)(di + 1), name_len);
1422
1423 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1424 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001425 over = filldir(dirent, name_ptr, name_len,
1426 found_key.offset,
1427 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001428 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001429
1430 if (name_ptr != tmp_name)
1431 kfree(name_ptr);
1432
Chris Mason39279cc2007-06-12 06:35:45 -04001433 if (over)
1434 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001435 di_len = btrfs_dir_name_len(leaf, di) +
1436 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001437 di_cur += di_len;
1438 di = (struct btrfs_dir_item *)((char *)di + di_len);
1439 }
1440 }
Chris Masonc2a8b6e2008-01-29 09:10:26 -05001441 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
Chris Mason39279cc2007-06-12 06:35:45 -04001442nopos:
1443 ret = 0;
1444err:
1445 btrfs_release_path(root, path);
1446 btrfs_free_path(path);
1447 mutex_unlock(&root->fs_info->fs_mutex);
1448 return ret;
1449}
1450
1451int btrfs_write_inode(struct inode *inode, int wait)
1452{
1453 struct btrfs_root *root = BTRFS_I(inode)->root;
1454 struct btrfs_trans_handle *trans;
1455 int ret = 0;
1456
1457 if (wait) {
1458 mutex_lock(&root->fs_info->fs_mutex);
1459 trans = btrfs_start_transaction(root, 1);
1460 btrfs_set_trans_block_group(trans, inode);
1461 ret = btrfs_commit_transaction(trans, root);
1462 mutex_unlock(&root->fs_info->fs_mutex);
1463 }
1464 return ret;
1465}
1466
1467/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001468 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001469 * inode changes. But, it is most likely to find the inode in cache.
1470 * FIXME, needs more benchmarking...there are no reasons other than performance
1471 * to keep or drop this code.
1472 */
1473void btrfs_dirty_inode(struct inode *inode)
1474{
1475 struct btrfs_root *root = BTRFS_I(inode)->root;
1476 struct btrfs_trans_handle *trans;
1477
1478 mutex_lock(&root->fs_info->fs_mutex);
1479 trans = btrfs_start_transaction(root, 1);
1480 btrfs_set_trans_block_group(trans, inode);
1481 btrfs_update_inode(trans, root, inode);
1482 btrfs_end_transaction(trans, root);
1483 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001484}
1485
1486static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1487 struct btrfs_root *root,
1488 u64 objectid,
1489 struct btrfs_block_group_cache *group,
1490 int mode)
1491{
1492 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001493 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001494 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001495 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001496 int ret;
1497 int owner;
1498
Chris Mason5f39d392007-10-15 16:14:19 -04001499 path = btrfs_alloc_path();
1500 BUG_ON(!path);
1501
Chris Mason39279cc2007-06-12 06:35:45 -04001502 inode = new_inode(root->fs_info->sb);
1503 if (!inode)
1504 return ERR_PTR(-ENOMEM);
1505
Chris Masond1310b22008-01-24 16:13:08 -05001506 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1507 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001508 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001509 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001510
Chris Mason39279cc2007-06-12 06:35:45 -04001511 if (mode & S_IFDIR)
1512 owner = 0;
1513 else
1514 owner = 1;
1515 group = btrfs_find_block_group(root, group, 0, 0, owner);
1516 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001517 BTRFS_I(inode)->flags = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001518 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1519 if (ret)
1520 goto fail;
1521
Chris Mason39279cc2007-06-12 06:35:45 -04001522 inode->i_uid = current->fsuid;
1523 inode->i_gid = current->fsgid;
1524 inode->i_mode = mode;
1525 inode->i_ino = objectid;
1526 inode->i_blocks = 0;
1527 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001528 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1529 struct btrfs_inode_item);
1530 fill_inode_item(path->nodes[0], inode_item, inode);
1531 btrfs_mark_buffer_dirty(path->nodes[0]);
1532 btrfs_free_path(path);
1533
Chris Mason39279cc2007-06-12 06:35:45 -04001534 location = &BTRFS_I(inode)->location;
1535 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001536 location->offset = 0;
1537 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1538
Chris Mason39279cc2007-06-12 06:35:45 -04001539 insert_inode_hash(inode);
1540 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001541fail:
1542 btrfs_free_path(path);
1543 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001544}
1545
1546static inline u8 btrfs_inode_type(struct inode *inode)
1547{
1548 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1549}
1550
1551static int btrfs_add_link(struct btrfs_trans_handle *trans,
1552 struct dentry *dentry, struct inode *inode)
1553{
1554 int ret;
1555 struct btrfs_key key;
1556 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001557 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001558
Chris Mason39279cc2007-06-12 06:35:45 -04001559 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001560 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1561 key.offset = 0;
1562
1563 ret = btrfs_insert_dir_item(trans, root,
1564 dentry->d_name.name, dentry->d_name.len,
1565 dentry->d_parent->d_inode->i_ino,
1566 &key, btrfs_inode_type(inode));
1567 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001568 ret = btrfs_insert_inode_ref(trans, root,
1569 dentry->d_name.name,
1570 dentry->d_name.len,
1571 inode->i_ino,
1572 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001573 parent_inode = dentry->d_parent->d_inode;
1574 parent_inode->i_size += dentry->d_name.len * 2;
1575 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001576 ret = btrfs_update_inode(trans, root,
1577 dentry->d_parent->d_inode);
1578 }
1579 return ret;
1580}
1581
1582static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1583 struct dentry *dentry, struct inode *inode)
1584{
1585 int err = btrfs_add_link(trans, dentry, inode);
1586 if (!err) {
1587 d_instantiate(dentry, inode);
1588 return 0;
1589 }
1590 if (err > 0)
1591 err = -EEXIST;
1592 return err;
1593}
1594
Josef Bacik618e21d2007-07-11 10:18:17 -04001595static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1596 int mode, dev_t rdev)
1597{
1598 struct btrfs_trans_handle *trans;
1599 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001600 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001601 int err;
1602 int drop_inode = 0;
1603 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001604 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001605
1606 if (!new_valid_dev(rdev))
1607 return -EINVAL;
1608
1609 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001610 err = btrfs_check_free_space(root, 1, 0);
1611 if (err)
1612 goto fail;
1613
Josef Bacik618e21d2007-07-11 10:18:17 -04001614 trans = btrfs_start_transaction(root, 1);
1615 btrfs_set_trans_block_group(trans, dir);
1616
1617 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1618 if (err) {
1619 err = -ENOSPC;
1620 goto out_unlock;
1621 }
1622
1623 inode = btrfs_new_inode(trans, root, objectid,
1624 BTRFS_I(dir)->block_group, mode);
1625 err = PTR_ERR(inode);
1626 if (IS_ERR(inode))
1627 goto out_unlock;
1628
1629 btrfs_set_trans_block_group(trans, inode);
1630 err = btrfs_add_nondir(trans, dentry, inode);
1631 if (err)
1632 drop_inode = 1;
1633 else {
1634 inode->i_op = &btrfs_special_inode_operations;
1635 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001636 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001637 }
1638 dir->i_sb->s_dirt = 1;
1639 btrfs_update_inode_block_group(trans, inode);
1640 btrfs_update_inode_block_group(trans, dir);
1641out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001642 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001643 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001644fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001645 mutex_unlock(&root->fs_info->fs_mutex);
1646
1647 if (drop_inode) {
1648 inode_dec_link_count(inode);
1649 iput(inode);
1650 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001651 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001652 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001653 return err;
1654}
1655
Chris Mason39279cc2007-06-12 06:35:45 -04001656static int btrfs_create(struct inode *dir, struct dentry *dentry,
1657 int mode, struct nameidata *nd)
1658{
1659 struct btrfs_trans_handle *trans;
1660 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001661 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001662 int err;
1663 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001664 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001665 u64 objectid;
1666
1667 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001668 err = btrfs_check_free_space(root, 1, 0);
1669 if (err)
1670 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001671 trans = btrfs_start_transaction(root, 1);
1672 btrfs_set_trans_block_group(trans, dir);
1673
1674 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1675 if (err) {
1676 err = -ENOSPC;
1677 goto out_unlock;
1678 }
1679
1680 inode = btrfs_new_inode(trans, root, objectid,
1681 BTRFS_I(dir)->block_group, mode);
1682 err = PTR_ERR(inode);
1683 if (IS_ERR(inode))
1684 goto out_unlock;
1685
1686 btrfs_set_trans_block_group(trans, inode);
1687 err = btrfs_add_nondir(trans, dentry, inode);
1688 if (err)
1689 drop_inode = 1;
1690 else {
1691 inode->i_mapping->a_ops = &btrfs_aops;
1692 inode->i_fop = &btrfs_file_operations;
1693 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001694 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1695 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001696 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001698 }
1699 dir->i_sb->s_dirt = 1;
1700 btrfs_update_inode_block_group(trans, inode);
1701 btrfs_update_inode_block_group(trans, dir);
1702out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001703 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001704 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001705fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001706 mutex_unlock(&root->fs_info->fs_mutex);
1707
1708 if (drop_inode) {
1709 inode_dec_link_count(inode);
1710 iput(inode);
1711 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001712 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001713 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001714 return err;
1715}
1716
1717static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1718 struct dentry *dentry)
1719{
1720 struct btrfs_trans_handle *trans;
1721 struct btrfs_root *root = BTRFS_I(dir)->root;
1722 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001723 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001724 int err;
1725 int drop_inode = 0;
1726
1727 if (inode->i_nlink == 0)
1728 return -ENOENT;
1729
Chris Mason6da6aba2007-12-18 16:15:09 -05001730#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1731 inode->i_nlink++;
1732#else
Chris Mason39279cc2007-06-12 06:35:45 -04001733 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001734#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001735 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001736 err = btrfs_check_free_space(root, 1, 0);
1737 if (err)
1738 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001739 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001740
Chris Mason39279cc2007-06-12 06:35:45 -04001741 btrfs_set_trans_block_group(trans, dir);
1742 atomic_inc(&inode->i_count);
1743 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001744
Chris Mason39279cc2007-06-12 06:35:45 -04001745 if (err)
1746 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001747
Chris Mason39279cc2007-06-12 06:35:45 -04001748 dir->i_sb->s_dirt = 1;
1749 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001750 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001751
Chris Mason54aa1f42007-06-22 14:16:25 -04001752 if (err)
1753 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001754
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001755 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001756 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001757fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001758 mutex_unlock(&root->fs_info->fs_mutex);
1759
1760 if (drop_inode) {
1761 inode_dec_link_count(inode);
1762 iput(inode);
1763 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001764 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001765 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001766 return err;
1767}
1768
Chris Mason39279cc2007-06-12 06:35:45 -04001769static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1770{
1771 struct inode *inode;
1772 struct btrfs_trans_handle *trans;
1773 struct btrfs_root *root = BTRFS_I(dir)->root;
1774 int err = 0;
1775 int drop_on_err = 0;
1776 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001777 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001778
1779 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001780 err = btrfs_check_free_space(root, 1, 0);
1781 if (err)
1782 goto out_unlock;
1783
Chris Mason39279cc2007-06-12 06:35:45 -04001784 trans = btrfs_start_transaction(root, 1);
1785 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001786
Chris Mason39279cc2007-06-12 06:35:45 -04001787 if (IS_ERR(trans)) {
1788 err = PTR_ERR(trans);
1789 goto out_unlock;
1790 }
1791
1792 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1793 if (err) {
1794 err = -ENOSPC;
1795 goto out_unlock;
1796 }
1797
1798 inode = btrfs_new_inode(trans, root, objectid,
1799 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1800 if (IS_ERR(inode)) {
1801 err = PTR_ERR(inode);
1802 goto out_fail;
1803 }
Chris Mason5f39d392007-10-15 16:14:19 -04001804
Chris Mason39279cc2007-06-12 06:35:45 -04001805 drop_on_err = 1;
1806 inode->i_op = &btrfs_dir_inode_operations;
1807 inode->i_fop = &btrfs_dir_file_operations;
1808 btrfs_set_trans_block_group(trans, inode);
1809
Chris Mason39544012007-12-12 14:38:19 -05001810 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001811 err = btrfs_update_inode(trans, root, inode);
1812 if (err)
1813 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001814
Chris Mason39279cc2007-06-12 06:35:45 -04001815 err = btrfs_add_link(trans, dentry, inode);
1816 if (err)
1817 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001818
Chris Mason39279cc2007-06-12 06:35:45 -04001819 d_instantiate(dentry, inode);
1820 drop_on_err = 0;
1821 dir->i_sb->s_dirt = 1;
1822 btrfs_update_inode_block_group(trans, inode);
1823 btrfs_update_inode_block_group(trans, dir);
1824
1825out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001826 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001827 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001828
Chris Mason39279cc2007-06-12 06:35:45 -04001829out_unlock:
1830 mutex_unlock(&root->fs_info->fs_mutex);
1831 if (drop_on_err)
1832 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001833 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001834 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001835 return err;
1836}
1837
Chris Masona52d9a82007-08-27 16:49:44 -04001838struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001839 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001840 int create)
1841{
1842 int ret;
1843 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001844 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001845 u64 extent_start = 0;
1846 u64 extent_end = 0;
1847 u64 objectid = inode->i_ino;
1848 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001849 struct btrfs_path *path;
1850 struct btrfs_root *root = BTRFS_I(inode)->root;
1851 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001852 struct extent_buffer *leaf;
1853 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001854 struct extent_map *em = NULL;
1855 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001857 struct btrfs_trans_handle *trans = NULL;
1858
1859 path = btrfs_alloc_path();
1860 BUG_ON(!path);
1861 mutex_lock(&root->fs_info->fs_mutex);
1862
1863again:
Chris Masond1310b22008-01-24 16:13:08 -05001864 spin_lock(&em_tree->lock);
1865 em = lookup_extent_mapping(em_tree, start, len);
1866 spin_unlock(&em_tree->lock);
1867
Chris Masona52d9a82007-08-27 16:49:44 -04001868 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001869 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001870 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1871 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001872 WARN_ON(1);
1873 }
Chris Mason70dec802008-01-29 09:59:12 -05001874 if (em->block_start == EXTENT_MAP_INLINE && page)
1875 free_extent_map(em);
1876 else
1877 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001878 }
Chris Masond1310b22008-01-24 16:13:08 -05001879 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001880 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001881 err = -ENOMEM;
1882 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001883 }
Chris Masond1310b22008-01-24 16:13:08 -05001884
1885 em->start = EXTENT_MAP_HOLE;
1886 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001887 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001888 ret = btrfs_lookup_file_extent(trans, root, path,
1889 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001890 if (ret < 0) {
1891 err = ret;
1892 goto out;
1893 }
1894
1895 if (ret != 0) {
1896 if (path->slots[0] == 0)
1897 goto not_found;
1898 path->slots[0]--;
1899 }
1900
Chris Mason5f39d392007-10-15 16:14:19 -04001901 leaf = path->nodes[0];
1902 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001903 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001904 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001905 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1906 found_type = btrfs_key_type(&found_key);
1907 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001908 found_type != BTRFS_EXTENT_DATA_KEY) {
1909 goto not_found;
1910 }
1911
Chris Mason5f39d392007-10-15 16:14:19 -04001912 found_type = btrfs_file_extent_type(leaf, item);
1913 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001914 if (found_type == BTRFS_FILE_EXTENT_REG) {
1915 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001916 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001917 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001918 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001919 em->start = start;
1920 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001921 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001922 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001923 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001924 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001925 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001926 }
1927 goto not_found_em;
1928 }
Chris Masondb945352007-10-15 16:15:53 -04001929 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1930 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001931 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001932 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001933 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001934 goto insert;
1935 }
Chris Masondb945352007-10-15 16:15:53 -04001936 bytenr += btrfs_file_extent_offset(leaf, item);
1937 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001938 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001939 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001940 goto insert;
1941 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05001942 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04001943 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001944 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001945 size_t size;
1946 size_t extent_offset;
1947 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001948
Chris Mason5f39d392007-10-15 16:14:19 -04001949 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1950 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05001951 extent_end = (extent_start + size + root->sectorsize - 1) &
1952 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001953 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001954 em->start = start;
1955 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001956 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001957 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001958 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001959 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001960 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001961 }
1962 goto not_found_em;
1963 }
1964 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001965
1966 if (!page) {
1967 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05001968 em->len = size;
Yan689f9342007-10-29 11:41:07 -04001969 goto out;
1970 }
1971
Chris Mason70dec802008-01-29 09:59:12 -05001972 page_start = page_offset(page) + pg_offset;
1973 extent_offset = page_start - extent_start;
1974 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04001975 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001976 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05001977 em->len = (copy_size + root->sectorsize - 1) &
1978 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04001979 map = kmap(page);
1980 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001981 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05001982 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04001983 copy_size);
1984 flush_dcache_page(page);
1985 } else if (create && PageUptodate(page)) {
1986 if (!trans) {
1987 kunmap(page);
1988 free_extent_map(em);
1989 em = NULL;
1990 btrfs_release_path(root, path);
1991 trans = btrfs_start_transaction(root, 1);
1992 goto again;
1993 }
Chris Mason70dec802008-01-29 09:59:12 -05001994 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04001995 copy_size);
1996 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001997 }
Chris Masona52d9a82007-08-27 16:49:44 -04001998 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05001999 set_extent_uptodate(io_tree, em->start,
2000 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002001 goto insert;
2002 } else {
2003 printk("unkknown found_type %d\n", found_type);
2004 WARN_ON(1);
2005 }
2006not_found:
2007 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002008 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002009not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002010 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002011insert:
2012 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002013 if (em->start > start || extent_map_end(em) <= start) {
2014 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002015 err = -EIO;
2016 goto out;
2017 }
Chris Masond1310b22008-01-24 16:13:08 -05002018
2019 err = 0;
2020 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002021 ret = add_extent_mapping(em_tree, em);
2022 if (ret == -EEXIST) {
2023 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002024 em = lookup_extent_mapping(em_tree, start, len);
2025 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002026 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002027 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002028 }
Chris Masona52d9a82007-08-27 16:49:44 -04002029 }
Chris Masond1310b22008-01-24 16:13:08 -05002030 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002031out:
2032 btrfs_free_path(path);
2033 if (trans) {
2034 ret = btrfs_end_transaction(trans, root);
2035 if (!err)
2036 err = ret;
2037 }
2038 mutex_unlock(&root->fs_info->fs_mutex);
2039 if (err) {
2040 free_extent_map(em);
2041 WARN_ON(1);
2042 return ERR_PTR(err);
2043 }
2044 return em;
2045}
2046
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002047static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002048{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002049 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002050}
2051
Chris Mason9ebefb182007-06-15 13:50:00 -04002052int btrfs_readpage(struct file *file, struct page *page)
2053{
Chris Masond1310b22008-01-24 16:13:08 -05002054 struct extent_io_tree *tree;
2055 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002056 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002057}
Chris Mason1832a6d2007-12-21 16:27:21 -05002058
Chris Mason39279cc2007-06-12 06:35:45 -04002059static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2060{
Chris Masond1310b22008-01-24 16:13:08 -05002061 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002062
2063
2064 if (current->flags & PF_MEMALLOC) {
2065 redirty_page_for_writepage(wbc, page);
2066 unlock_page(page);
2067 return 0;
2068 }
Chris Masond1310b22008-01-24 16:13:08 -05002069 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002070 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2071}
Chris Mason39279cc2007-06-12 06:35:45 -04002072
Chris Masonb293f02e2007-11-01 19:45:34 -04002073static int btrfs_writepages(struct address_space *mapping,
2074 struct writeback_control *wbc)
2075{
Chris Masond1310b22008-01-24 16:13:08 -05002076 struct extent_io_tree *tree;
2077 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04002078 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2079}
2080
Chris Mason3ab2fb52007-11-08 10:59:22 -05002081static int
2082btrfs_readpages(struct file *file, struct address_space *mapping,
2083 struct list_head *pages, unsigned nr_pages)
2084{
Chris Masond1310b22008-01-24 16:13:08 -05002085 struct extent_io_tree *tree;
2086 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002087 return extent_readpages(tree, mapping, pages, nr_pages,
2088 btrfs_get_extent);
2089}
2090
Chris Mason70dec802008-01-29 09:59:12 -05002091static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002092{
Chris Masond1310b22008-01-24 16:13:08 -05002093 struct extent_io_tree *tree;
2094 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002095 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002096
Chris Masond1310b22008-01-24 16:13:08 -05002097 tree = &BTRFS_I(page->mapping->host)->io_tree;
2098 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002099 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002100 if (ret == 1) {
2101 ClearPagePrivate(page);
2102 set_page_private(page, 0);
2103 page_cache_release(page);
2104 }
2105 return ret;
2106}
Chris Mason39279cc2007-06-12 06:35:45 -04002107
Chris Masona52d9a82007-08-27 16:49:44 -04002108static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2109{
Chris Masond1310b22008-01-24 16:13:08 -05002110 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002111
Chris Masond1310b22008-01-24 16:13:08 -05002112 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002113 extent_invalidatepage(tree, page, offset);
2114 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002115}
2116
Chris Mason9ebefb182007-06-15 13:50:00 -04002117/*
2118 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2119 * called from a page fault handler when a page is first dirtied. Hence we must
2120 * be careful to check for EOF conditions here. We set the page up correctly
2121 * for a written page which means we get ENOSPC checking when writing into
2122 * holes and correct delalloc and unwritten extent mapping on filesystems that
2123 * support these features.
2124 *
2125 * We are not allowed to take the i_mutex here so we have to play games to
2126 * protect against truncate races as the page could now be beyond EOF. Because
2127 * vmtruncate() writes the inode size before removing pages, once we have the
2128 * page lock we can determine safely if the page is beyond EOF. If it is not
2129 * beyond EOF, then the page is guaranteed safe against truncation until we
2130 * unlock the page.
2131 */
2132int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2133{
Chris Mason6da6aba2007-12-18 16:15:09 -05002134 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002135 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002136 unsigned long end;
2137 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002138 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002139 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002140
Chris Mason1832a6d2007-12-21 16:27:21 -05002141 mutex_lock(&root->fs_info->fs_mutex);
2142 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002143 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002144 if (ret)
2145 goto out;
2146
2147 ret = -EINVAL;
2148
Chris Mason9ebefb182007-06-15 13:50:00 -04002149 lock_page(page);
2150 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002151 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002152 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002153
Chris Mason9ebefb182007-06-15 13:50:00 -04002154 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002155 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002156 /* page got truncated out from underneath us */
2157 goto out_unlock;
2158 }
2159
2160 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002161 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002162 end = size & ~PAGE_CACHE_MASK;
2163 else
2164 end = PAGE_CACHE_SIZE;
2165
Chris Masonb888db22007-08-27 16:49:44 -04002166 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002167
2168out_unlock:
2169 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002170out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002171 return ret;
2172}
2173
Chris Mason39279cc2007-06-12 06:35:45 -04002174static void btrfs_truncate(struct inode *inode)
2175{
2176 struct btrfs_root *root = BTRFS_I(inode)->root;
2177 int ret;
2178 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002179 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002180
2181 if (!S_ISREG(inode->i_mode))
2182 return;
2183 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2184 return;
2185
2186 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2187
2188 mutex_lock(&root->fs_info->fs_mutex);
2189 trans = btrfs_start_transaction(root, 1);
2190 btrfs_set_trans_block_group(trans, inode);
2191
2192 /* FIXME, add redo link to tree so we don't leak on crash */
2193 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002194 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002195 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002196
Chris Mason39279cc2007-06-12 06:35:45 -04002197 ret = btrfs_end_transaction(trans, root);
2198 BUG_ON(ret);
2199 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002200 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002201 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002202}
2203
Chris Mason4313b392008-01-03 09:08:48 -05002204static int noinline create_subvol(struct btrfs_root *root, char *name,
2205 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002206{
2207 struct btrfs_trans_handle *trans;
2208 struct btrfs_key key;
2209 struct btrfs_root_item root_item;
2210 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002211 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002212 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002213 struct inode *inode;
2214 struct inode *dir;
2215 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002216 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002217 u64 objectid;
2218 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002219 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002220
2221 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002222 ret = btrfs_check_free_space(root, 1, 0);
2223 if (ret)
2224 goto fail_commit;
2225
Chris Mason39279cc2007-06-12 06:35:45 -04002226 trans = btrfs_start_transaction(root, 1);
2227 BUG_ON(!trans);
2228
Chris Mason7bb86312007-12-11 09:25:06 -05002229 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2230 0, &objectid);
2231 if (ret)
2232 goto fail;
2233
2234 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2235 objectid, trans->transid, 0, 0,
2236 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 if (IS_ERR(leaf))
2238 return PTR_ERR(leaf);
2239
2240 btrfs_set_header_nritems(leaf, 0);
2241 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002242 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002243 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002244 btrfs_set_header_owner(leaf, objectid);
2245
Chris Mason5f39d392007-10-15 16:14:19 -04002246 write_extent_buffer(leaf, root->fs_info->fsid,
2247 (unsigned long)btrfs_header_fsid(leaf),
2248 BTRFS_FSID_SIZE);
2249 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002250
2251 inode_item = &root_item.inode;
2252 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002253 inode_item->generation = cpu_to_le64(1);
2254 inode_item->size = cpu_to_le64(3);
2255 inode_item->nlink = cpu_to_le32(1);
2256 inode_item->nblocks = cpu_to_le64(1);
2257 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002258
Chris Masondb945352007-10-15 16:15:53 -04002259 btrfs_set_root_bytenr(&root_item, leaf->start);
2260 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002261 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002262 btrfs_set_root_used(&root_item, 0);
2263
Chris Mason5eda7b52007-06-22 14:16:25 -04002264 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2265 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002266
2267 free_extent_buffer(leaf);
2268 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002269
Chris Mason39279cc2007-06-12 06:35:45 -04002270 btrfs_set_root_dirid(&root_item, new_dirid);
2271
2272 key.objectid = objectid;
2273 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002274 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2275 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2276 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002277 if (ret)
2278 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002279
2280 /*
2281 * insert the directory item
2282 */
2283 key.offset = (u64)-1;
2284 dir = root->fs_info->sb->s_root->d_inode;
2285 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2286 name, namelen, dir->i_ino, &key,
2287 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002288 if (ret)
2289 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002290
Chris Mason39544012007-12-12 14:38:19 -05002291 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2292 name, namelen, objectid,
2293 root->fs_info->sb->s_root->d_inode->i_ino);
2294 if (ret)
2295 goto fail;
2296
Chris Mason39279cc2007-06-12 06:35:45 -04002297 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002298 if (ret)
2299 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002300
Josef Bacik58176a92007-08-29 15:47:34 -04002301 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002302 BUG_ON(!new_root);
2303
2304 trans = btrfs_start_transaction(new_root, 1);
2305 BUG_ON(!trans);
2306
2307 inode = btrfs_new_inode(trans, new_root, new_dirid,
2308 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002309 if (IS_ERR(inode))
2310 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002311 inode->i_op = &btrfs_dir_inode_operations;
2312 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002313 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002314
Chris Mason39544012007-12-12 14:38:19 -05002315 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2316 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002317 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002318 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002319 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002320 if (ret)
2321 goto fail;
2322fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002323 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002324 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002325 if (err && !ret)
2326 ret = err;
2327fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002328 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002329 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002330 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002331 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002332}
2333
2334static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2335{
Chris Mason3063d292008-01-08 15:46:30 -05002336 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002337 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002338 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002339 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002340 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002341
2342 if (!root->ref_cows)
2343 return -EINVAL;
2344
2345 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002346 ret = btrfs_check_free_space(root, 1, 0);
2347 if (ret)
2348 goto fail_unlock;
2349
Chris Mason3063d292008-01-08 15:46:30 -05002350 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2351 if (!pending_snapshot) {
2352 ret = -ENOMEM;
2353 goto fail_unlock;
2354 }
Yanfb4bc1e2008-01-17 11:59:51 -05002355 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002356 if (!pending_snapshot->name) {
2357 ret = -ENOMEM;
2358 kfree(pending_snapshot);
2359 goto fail_unlock;
2360 }
Yanfb4bc1e2008-01-17 11:59:51 -05002361 memcpy(pending_snapshot->name, name, namelen);
2362 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002363 trans = btrfs_start_transaction(root, 1);
2364 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002365 pending_snapshot->root = root;
2366 list_add(&pending_snapshot->list,
2367 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002368 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002369 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Mason1832a6d2007-12-21 16:27:21 -05002371fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002372 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002373 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002374 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002375 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002376}
2377
Chris Masonedbd8d42007-12-21 16:27:24 -05002378unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002379 struct file_ra_state *ra, struct file *file,
2380 pgoff_t offset, pgoff_t last_index)
2381{
2382 pgoff_t req_size;
2383
2384#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2385 req_size = last_index - offset + 1;
2386 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2387 return offset;
2388#else
2389 req_size = min(last_index - offset + 1, (pgoff_t)128);
2390 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2391 return offset + req_size;
2392#endif
2393}
2394
2395int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002396 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002397 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002398 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002399 struct page *page;
2400 unsigned long last_index;
2401 unsigned long ra_index = 0;
2402 u64 page_start;
2403 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002404 u64 delalloc_start;
2405 u64 existing_delalloc;
Chris Mason86479a02007-09-10 19:58:16 -04002406 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002407 int ret;
2408
2409 mutex_lock(&root->fs_info->fs_mutex);
2410 ret = btrfs_check_free_space(root, inode->i_size, 0);
2411 mutex_unlock(&root->fs_info->fs_mutex);
2412 if (ret)
2413 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002414
2415 mutex_lock(&inode->i_mutex);
2416 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2417 for (i = 0; i <= last_index; i++) {
2418 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002419 ra_index = btrfs_force_ra(inode->i_mapping,
2420 &file->f_ra,
2421 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002422 }
2423 page = grab_cache_page(inode->i_mapping, i);
2424 if (!page)
2425 goto out_unlock;
2426 if (!PageUptodate(page)) {
2427 btrfs_readpage(NULL, page);
2428 lock_page(page);
2429 if (!PageUptodate(page)) {
2430 unlock_page(page);
2431 page_cache_release(page);
2432 goto out_unlock;
2433 }
2434 }
Chris Mason35ebb932007-10-30 16:56:53 -04002435 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002436 page_end = page_start + PAGE_CACHE_SIZE - 1;
2437
Chris Masond1310b22008-01-24 16:13:08 -05002438 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002439 delalloc_start = page_start;
2440 existing_delalloc =
Chris Masond1310b22008-01-24 16:13:08 -05002441 count_range_bits(&BTRFS_I(inode)->io_tree,
Chris Masonedbd8d42007-12-21 16:27:24 -05002442 &delalloc_start, page_end,
2443 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masond1310b22008-01-24 16:13:08 -05002444 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002445 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002446
2447 spin_lock(&root->fs_info->delalloc_lock);
2448 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2449 existing_delalloc;
2450 spin_unlock(&root->fs_info->delalloc_lock);
2451
Chris Masond1310b22008-01-24 16:13:08 -05002452 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002453 set_page_dirty(page);
2454 unlock_page(page);
2455 page_cache_release(page);
2456 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2457 }
2458
2459out_unlock:
2460 mutex_unlock(&inode->i_mutex);
2461 return 0;
2462}
2463
Chris Masonedbd8d42007-12-21 16:27:24 -05002464static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2465{
2466 u64 new_size;
2467 u64 old_size;
2468 struct btrfs_ioctl_vol_args *vol_args;
2469 struct btrfs_trans_handle *trans;
2470 char *sizestr;
2471 int ret = 0;
2472 int namelen;
2473 int mod = 0;
2474
2475 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2476
2477 if (!vol_args)
2478 return -ENOMEM;
2479
2480 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2481 ret = -EFAULT;
2482 goto out;
2483 }
2484 namelen = strlen(vol_args->name);
2485 if (namelen > BTRFS_VOL_NAME_MAX) {
2486 ret = -EINVAL;
2487 goto out;
2488 }
2489
2490 sizestr = vol_args->name;
2491 if (!strcmp(sizestr, "max"))
2492 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2493 else {
2494 if (sizestr[0] == '-') {
2495 mod = -1;
2496 sizestr++;
2497 } else if (sizestr[0] == '+') {
2498 mod = 1;
2499 sizestr++;
2500 }
2501 new_size = btrfs_parse_size(sizestr);
2502 if (new_size == 0) {
2503 ret = -EINVAL;
2504 goto out;
2505 }
2506 }
2507
2508 mutex_lock(&root->fs_info->fs_mutex);
2509 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2510
2511 if (mod < 0) {
2512 if (new_size > old_size) {
2513 ret = -EINVAL;
2514 goto out_unlock;
2515 }
2516 new_size = old_size - new_size;
2517 } else if (mod > 0) {
2518 new_size = old_size + new_size;
2519 }
2520
2521 if (new_size < 256 * 1024 * 1024) {
2522 ret = -EINVAL;
2523 goto out_unlock;
2524 }
2525 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2526 ret = -EFBIG;
2527 goto out_unlock;
2528 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002529
2530 do_div(new_size, root->sectorsize);
2531 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002532
2533printk("new size is %Lu\n", new_size);
2534 if (new_size > old_size) {
2535 trans = btrfs_start_transaction(root, 1);
2536 ret = btrfs_grow_extent_tree(trans, root, new_size);
2537 btrfs_commit_transaction(trans, root);
2538 } else {
2539 ret = btrfs_shrink_extent_tree(root, new_size);
2540 }
2541
2542out_unlock:
2543 mutex_unlock(&root->fs_info->fs_mutex);
2544out:
2545 kfree(vol_args);
2546 return ret;
2547}
2548
Chris Mason4313b392008-01-03 09:08:48 -05002549static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2550 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002551{
Chris Mason4aec2b52007-12-18 16:25:45 -05002552 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002553 struct btrfs_dir_item *di;
2554 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002555 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002556 int namelen;
2557 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002558
Chris Mason4aec2b52007-12-18 16:25:45 -05002559 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002560
Chris Mason4aec2b52007-12-18 16:25:45 -05002561 if (!vol_args)
2562 return -ENOMEM;
2563
2564 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2565 ret = -EFAULT;
2566 goto out;
2567 }
2568
2569 namelen = strlen(vol_args->name);
2570 if (namelen > BTRFS_VOL_NAME_MAX) {
2571 ret = -EINVAL;
2572 goto out;
2573 }
2574 if (strchr(vol_args->name, '/')) {
2575 ret = -EINVAL;
2576 goto out;
2577 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002578
2579 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002580 if (!path) {
2581 ret = -ENOMEM;
2582 goto out;
2583 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002584
2585 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2586 mutex_lock(&root->fs_info->fs_mutex);
2587 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2588 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002589 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002590 mutex_unlock(&root->fs_info->fs_mutex);
2591 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002592
2593 if (di && !IS_ERR(di)) {
2594 ret = -EEXIST;
2595 goto out;
2596 }
2597
2598 if (IS_ERR(di)) {
2599 ret = PTR_ERR(di);
2600 goto out;
2601 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002602
2603 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002604 ret = create_subvol(root, vol_args->name, namelen);
2605 else
2606 ret = create_snapshot(root, vol_args->name, namelen);
2607out:
2608 kfree(vol_args);
2609 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002610}
2611
2612static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002613{
Chris Mason6da6aba2007-12-18 16:15:09 -05002614 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002615 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002616
2617 switch (inode->i_mode & S_IFMT) {
2618 case S_IFDIR:
2619 mutex_lock(&root->fs_info->fs_mutex);
2620 btrfs_defrag_root(root, 0);
2621 btrfs_defrag_root(root->fs_info->extent_root, 0);
2622 mutex_unlock(&root->fs_info->fs_mutex);
2623 break;
2624 case S_IFREG:
2625 btrfs_defrag_file(file);
2626 break;
2627 }
2628
2629 return 0;
2630}
2631
2632long btrfs_ioctl(struct file *file, unsigned int
2633 cmd, unsigned long arg)
2634{
Chris Mason6da6aba2007-12-18 16:15:09 -05002635 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002636
2637 switch (cmd) {
2638 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002639 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002640 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002641 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002642 case BTRFS_IOC_RESIZE:
2643 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002644 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002645
2646 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002647}
2648
Chris Mason39279cc2007-06-12 06:35:45 -04002649/*
2650 * Called inside transaction, so use GFP_NOFS
2651 */
2652struct inode *btrfs_alloc_inode(struct super_block *sb)
2653{
2654 struct btrfs_inode *ei;
2655
2656 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2657 if (!ei)
2658 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002659 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002660 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002661 return &ei->vfs_inode;
2662}
2663
2664void btrfs_destroy_inode(struct inode *inode)
2665{
2666 WARN_ON(!list_empty(&inode->i_dentry));
2667 WARN_ON(inode->i_data.nrpages);
2668
Chris Mason8c416c92008-01-14 15:10:26 -05002669 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002670 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2671}
2672
Chris Mason44ec0b72007-10-29 10:55:05 -04002673#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2674static void init_once(struct kmem_cache * cachep, void *foo)
2675#else
Chris Mason39279cc2007-06-12 06:35:45 -04002676static void init_once(void * foo, struct kmem_cache * cachep,
2677 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002678#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002679{
2680 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2681
2682 inode_init_once(&ei->vfs_inode);
2683}
2684
2685void btrfs_destroy_cachep(void)
2686{
2687 if (btrfs_inode_cachep)
2688 kmem_cache_destroy(btrfs_inode_cachep);
2689 if (btrfs_trans_handle_cachep)
2690 kmem_cache_destroy(btrfs_trans_handle_cachep);
2691 if (btrfs_transaction_cachep)
2692 kmem_cache_destroy(btrfs_transaction_cachep);
2693 if (btrfs_bit_radix_cachep)
2694 kmem_cache_destroy(btrfs_bit_radix_cachep);
2695 if (btrfs_path_cachep)
2696 kmem_cache_destroy(btrfs_path_cachep);
2697}
2698
Chris Mason86479a02007-09-10 19:58:16 -04002699struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002700 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002701#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2702 void (*ctor)(struct kmem_cache *, void *)
2703#else
Chris Mason92fee662007-07-25 12:31:35 -04002704 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002705 unsigned long)
2706#endif
2707 )
Chris Mason92fee662007-07-25 12:31:35 -04002708{
2709 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2710 SLAB_MEM_SPREAD | extra_flags), ctor
2711#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2712 ,NULL
2713#endif
2714 );
2715}
2716
Chris Mason39279cc2007-06-12 06:35:45 -04002717int btrfs_init_cachep(void)
2718{
Chris Mason86479a02007-09-10 19:58:16 -04002719 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002720 sizeof(struct btrfs_inode),
2721 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002722 if (!btrfs_inode_cachep)
2723 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002724 btrfs_trans_handle_cachep =
2725 btrfs_cache_create("btrfs_trans_handle_cache",
2726 sizeof(struct btrfs_trans_handle),
2727 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002728 if (!btrfs_trans_handle_cachep)
2729 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002730 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002731 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002732 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002733 if (!btrfs_transaction_cachep)
2734 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002735 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002736 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002737 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002738 if (!btrfs_path_cachep)
2739 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002740 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002741 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002742 if (!btrfs_bit_radix_cachep)
2743 goto fail;
2744 return 0;
2745fail:
2746 btrfs_destroy_cachep();
2747 return -ENOMEM;
2748}
2749
2750static int btrfs_getattr(struct vfsmount *mnt,
2751 struct dentry *dentry, struct kstat *stat)
2752{
2753 struct inode *inode = dentry->d_inode;
2754 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002755 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason39279cc2007-06-12 06:35:45 -04002756 return 0;
2757}
2758
2759static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2760 struct inode * new_dir,struct dentry *new_dentry)
2761{
2762 struct btrfs_trans_handle *trans;
2763 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2764 struct inode *new_inode = new_dentry->d_inode;
2765 struct inode *old_inode = old_dentry->d_inode;
2766 struct timespec ctime = CURRENT_TIME;
2767 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002768 int ret;
2769
2770 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2771 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2772 return -ENOTEMPTY;
2773 }
Chris Mason5f39d392007-10-15 16:14:19 -04002774
Chris Mason39279cc2007-06-12 06:35:45 -04002775 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002776 ret = btrfs_check_free_space(root, 1, 0);
2777 if (ret)
2778 goto out_unlock;
2779
Chris Mason39279cc2007-06-12 06:35:45 -04002780 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002781
Chris Mason39279cc2007-06-12 06:35:45 -04002782 btrfs_set_trans_block_group(trans, new_dir);
2783 path = btrfs_alloc_path();
2784 if (!path) {
2785 ret = -ENOMEM;
2786 goto out_fail;
2787 }
2788
2789 old_dentry->d_inode->i_nlink++;
2790 old_dir->i_ctime = old_dir->i_mtime = ctime;
2791 new_dir->i_ctime = new_dir->i_mtime = ctime;
2792 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002793
Chris Mason39279cc2007-06-12 06:35:45 -04002794 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2795 if (ret)
2796 goto out_fail;
2797
2798 if (new_inode) {
2799 new_inode->i_ctime = CURRENT_TIME;
2800 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2801 if (ret)
2802 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002803 }
2804 ret = btrfs_add_link(trans, new_dentry, old_inode);
2805 if (ret)
2806 goto out_fail;
2807
2808out_fail:
2809 btrfs_free_path(path);
2810 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002811out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002812 mutex_unlock(&root->fs_info->fs_mutex);
2813 return ret;
2814}
2815
2816static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2817 const char *symname)
2818{
2819 struct btrfs_trans_handle *trans;
2820 struct btrfs_root *root = BTRFS_I(dir)->root;
2821 struct btrfs_path *path;
2822 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002823 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002824 int err;
2825 int drop_inode = 0;
2826 u64 objectid;
2827 int name_len;
2828 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002829 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002830 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002831 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002832 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002833
2834 name_len = strlen(symname) + 1;
2835 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2836 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002837
Chris Mason39279cc2007-06-12 06:35:45 -04002838 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002839 err = btrfs_check_free_space(root, 1, 0);
2840 if (err)
2841 goto out_fail;
2842
Chris Mason39279cc2007-06-12 06:35:45 -04002843 trans = btrfs_start_transaction(root, 1);
2844 btrfs_set_trans_block_group(trans, dir);
2845
2846 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2847 if (err) {
2848 err = -ENOSPC;
2849 goto out_unlock;
2850 }
2851
2852 inode = btrfs_new_inode(trans, root, objectid,
2853 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2854 err = PTR_ERR(inode);
2855 if (IS_ERR(inode))
2856 goto out_unlock;
2857
2858 btrfs_set_trans_block_group(trans, inode);
2859 err = btrfs_add_nondir(trans, dentry, inode);
2860 if (err)
2861 drop_inode = 1;
2862 else {
2863 inode->i_mapping->a_ops = &btrfs_aops;
2864 inode->i_fop = &btrfs_file_operations;
2865 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002866 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2867 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002868 inode->i_mapping, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002869 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 }
2871 dir->i_sb->s_dirt = 1;
2872 btrfs_update_inode_block_group(trans, inode);
2873 btrfs_update_inode_block_group(trans, dir);
2874 if (drop_inode)
2875 goto out_unlock;
2876
2877 path = btrfs_alloc_path();
2878 BUG_ON(!path);
2879 key.objectid = inode->i_ino;
2880 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002881 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2882 datasize = btrfs_file_extent_calc_inline_size(name_len);
2883 err = btrfs_insert_empty_item(trans, root, path, &key,
2884 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002885 if (err) {
2886 drop_inode = 1;
2887 goto out_unlock;
2888 }
Chris Mason5f39d392007-10-15 16:14:19 -04002889 leaf = path->nodes[0];
2890 ei = btrfs_item_ptr(leaf, path->slots[0],
2891 struct btrfs_file_extent_item);
2892 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2893 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002894 BTRFS_FILE_EXTENT_INLINE);
2895 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002896 write_extent_buffer(leaf, symname, ptr, name_len);
2897 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002898 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002899
Chris Mason39279cc2007-06-12 06:35:45 -04002900 inode->i_op = &btrfs_symlink_inode_operations;
2901 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2902 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002903 err = btrfs_update_inode(trans, root, inode);
2904 if (err)
2905 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002906
2907out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002908 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002909 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002910out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002911 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002912 if (drop_inode) {
2913 inode_dec_link_count(inode);
2914 iput(inode);
2915 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002916 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002917 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002918 return err;
2919}
Yanfdebe2b2008-01-14 13:26:08 -05002920static int btrfs_permission(struct inode *inode, int mask,
2921 struct nameidata *nd)
2922{
2923 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2924 return -EACCES;
2925 return generic_permission(inode, mask, NULL);
2926}
Chris Mason39279cc2007-06-12 06:35:45 -04002927
2928static struct inode_operations btrfs_dir_inode_operations = {
2929 .lookup = btrfs_lookup,
2930 .create = btrfs_create,
2931 .unlink = btrfs_unlink,
2932 .link = btrfs_link,
2933 .mkdir = btrfs_mkdir,
2934 .rmdir = btrfs_rmdir,
2935 .rename = btrfs_rename,
2936 .symlink = btrfs_symlink,
2937 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002938 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002939 .setxattr = generic_setxattr,
2940 .getxattr = generic_getxattr,
2941 .listxattr = btrfs_listxattr,
2942 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002943 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002944};
Chris Mason39279cc2007-06-12 06:35:45 -04002945static struct inode_operations btrfs_dir_ro_inode_operations = {
2946 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002947 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002948};
Chris Mason39279cc2007-06-12 06:35:45 -04002949static struct file_operations btrfs_dir_file_operations = {
2950 .llseek = generic_file_llseek,
2951 .read = generic_read_dir,
2952 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002953 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002954#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002955 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002956#endif
2957};
2958
Chris Masond1310b22008-01-24 16:13:08 -05002959static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04002960 .fill_delalloc = run_delalloc_range,
2961 .writepage_io_hook = btrfs_writepage_io_hook,
2962 .readpage_io_hook = btrfs_readpage_io_hook,
2963 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2964};
2965
Chris Mason39279cc2007-06-12 06:35:45 -04002966static struct address_space_operations btrfs_aops = {
2967 .readpage = btrfs_readpage,
2968 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04002969 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002970 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002971 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002972 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002973 .invalidatepage = btrfs_invalidatepage,
2974 .releasepage = btrfs_releasepage,
2975 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002976};
2977
2978static struct address_space_operations btrfs_symlink_aops = {
2979 .readpage = btrfs_readpage,
2980 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002981 .invalidatepage = btrfs_invalidatepage,
2982 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002983};
2984
2985static struct inode_operations btrfs_file_inode_operations = {
2986 .truncate = btrfs_truncate,
2987 .getattr = btrfs_getattr,
2988 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002989 .setxattr = generic_setxattr,
2990 .getxattr = generic_getxattr,
2991 .listxattr = btrfs_listxattr,
2992 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002993 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002994};
Josef Bacik618e21d2007-07-11 10:18:17 -04002995static struct inode_operations btrfs_special_inode_operations = {
2996 .getattr = btrfs_getattr,
2997 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05002998 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04002999};
Chris Mason39279cc2007-06-12 06:35:45 -04003000static struct inode_operations btrfs_symlink_inode_operations = {
3001 .readlink = generic_readlink,
3002 .follow_link = page_follow_link_light,
3003 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003004 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003005};