blob: 109576b57f69d0ee56fe0ba23d852005d3868aaa [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 Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include <linux/buffer_head.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include <linux/highmem.h>
24#include <linux/time.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/smp_lock.h>
28#include <linux/backing-dev.h>
29#include <linux/mpage.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
32#include <linux/statfs.h>
33#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040035#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
41#include "ioctl.h"
42#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044
45struct btrfs_iget_args {
46 u64 ino;
47 struct btrfs_root *root;
48};
49
50static struct inode_operations btrfs_dir_inode_operations;
51static struct inode_operations btrfs_symlink_inode_operations;
52static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040053static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040054static struct inode_operations btrfs_file_inode_operations;
55static struct address_space_operations btrfs_aops;
56static struct address_space_operations btrfs_symlink_aops;
57static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050058static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040059
60static struct kmem_cache *btrfs_inode_cachep;
61struct kmem_cache *btrfs_trans_handle_cachep;
62struct kmem_cache *btrfs_transaction_cachep;
63struct kmem_cache *btrfs_bit_radix_cachep;
64struct kmem_cache *btrfs_path_cachep;
65
66#define S_SHIFT 12
67static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
68 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
69 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
70 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
71 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
72 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
73 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
74 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
75};
76
Chris Mason1832a6d2007-12-21 16:27:21 -050077int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
78 int for_del)
79{
80 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
81 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
82 u64 thresh;
83 int ret = 0;
84
85 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 Mason90692182008-02-08 13:49:28 -0500139 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500140 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500141 num_bytes -= cur_alloc_size;
142 alloc_hint = ins.objectid + ins.offset;
143 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400144 }
Chris Masond1310b22008-01-24 16:13:08 -0500145 btrfs_drop_extent_cache(inode, orig_start,
146 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500147 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500148 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400149out:
150 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500151 return ret;
152}
153
154static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
155{
156 u64 extent_start;
157 u64 extent_end;
158 u64 bytenr;
159 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500160 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500161 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 struct btrfs_root *root = BTRFS_I(inode)->root;
163 struct extent_buffer *leaf;
164 int found_type;
165 struct btrfs_path *path;
166 struct btrfs_file_extent_item *item;
167 int ret;
168 int err;
169 struct btrfs_key found_key;
170
Chris Masonc31f8832008-01-08 15:46:31 -0500171 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500172 path = btrfs_alloc_path();
173 BUG_ON(!path);
174again:
175 ret = btrfs_lookup_file_extent(NULL, root, path,
176 inode->i_ino, start, 0);
177 if (ret < 0) {
178 btrfs_free_path(path);
179 return ret;
180 }
181
182 cow_end = end;
183 if (ret != 0) {
184 if (path->slots[0] == 0)
185 goto not_found;
186 path->slots[0]--;
187 }
188
189 leaf = path->nodes[0];
190 item = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_file_extent_item);
192
193 /* are we inside the extent that was found? */
194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
195 found_type = btrfs_key_type(&found_key);
196 if (found_key.objectid != inode->i_ino ||
197 found_type != BTRFS_EXTENT_DATA_KEY) {
198 goto not_found;
199 }
200
201 found_type = btrfs_file_extent_type(leaf, item);
202 extent_start = found_key.offset;
203 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500204 u64 extent_num_bytes;
205
206 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
207 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208 err = 0;
209
Chris Mason1832a6d2007-12-21 16:27:21 -0500210 if (loops && start != extent_start)
211 goto not_found;
212
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 if (start < extent_start || start >= extent_end)
214 goto not_found;
215
216 cow_end = min(end, extent_end - 1);
217 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
218 if (bytenr == 0)
219 goto not_found;
220
Chris Masonc31f8832008-01-08 15:46:31 -0500221 /*
222 * we may be called by the resizer, make sure we're inside
223 * the limits of the FS
224 */
225 if (bytenr + extent_num_bytes > total_fs_bytes)
226 goto not_found;
227
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
229 goto not_found;
230 }
231
232 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500233 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 goto not_found;
235 }
236loop:
237 if (start > end) {
238 btrfs_free_path(path);
239 return 0;
240 }
241 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500242 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 goto again;
244
245not_found:
246 cow_file_range(inode, start, cow_end);
247 start = cow_end + 1;
248 goto loop;
249}
250
251static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
252{
253 struct btrfs_root *root = BTRFS_I(inode)->root;
254 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500256 if (btrfs_test_opt(root, NODATACOW) ||
257 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 ret = run_delalloc_nocow(inode, start, end);
259 else
260 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500261
Chris Masonb888db22007-08-27 16:49:44 -0400262 mutex_unlock(&root->fs_info->fs_mutex);
263 return ret;
264}
265
Chris Mason291d6732008-01-29 15:55:23 -0500266int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500267 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500268{
Chris Masonb0c68f82008-01-31 11:05:37 -0500269 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500272 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500273 root->fs_info->delalloc_bytes += end - start + 1;
274 spin_unlock(&root->fs_info->delalloc_lock);
275 }
276 return 0;
277}
278
279int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500280 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500281{
Chris Masonb0c68f82008-01-31 11:05:37 -0500282 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500285 if (end - start + 1 > root->fs_info->delalloc_bytes) {
286 printk("warning: delalloc account %Lu %Lu\n",
287 end - start + 1, root->fs_info->delalloc_bytes);
288 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500289 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500290 } else {
291 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500292 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500293 }
Chris Mason291d6732008-01-29 15:55:23 -0500294 spin_unlock(&root->fs_info->delalloc_lock);
295 }
296 return 0;
297}
298
Chris Mason0b86a832008-03-24 15:01:56 -0400299int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
Chris Mason065631f2008-02-20 12:07:25 -0500300{
Chris Mason065631f2008-02-20 12:07:25 -0500301 struct btrfs_root *root = BTRFS_I(inode)->root;
302 struct btrfs_trans_handle *trans;
303 int ret = 0;
304
Chris Mason0b86a832008-03-24 15:01:56 -0400305 if (rw != WRITE) {
306 goto mapit;
307 }
Chris Mason065631f2008-02-20 12:07:25 -0500308
309 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400310 btrfs_test_flag(inode, NODATASUM)) {
311 goto mapit;
312 }
Chris Mason065631f2008-02-20 12:07:25 -0500313
314 mutex_lock(&root->fs_info->fs_mutex);
315 trans = btrfs_start_transaction(root, 1);
316 btrfs_set_trans_block_group(trans, inode);
317 btrfs_csum_file_blocks(trans, root, inode, bio);
318 ret = btrfs_end_transaction(trans, root);
319 BUG_ON(ret);
320 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -0400321mapit:
322 return btrfs_map_bio(root, rw, bio);
Chris Mason065631f2008-02-20 12:07:25 -0500323}
Chris Mason6885f302008-02-20 16:11:05 -0500324
Chris Mason07157aa2007-08-30 08:50:51 -0400325int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
326{
327 int ret = 0;
328 struct inode *inode = page->mapping->host;
329 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500330 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400331 struct btrfs_csum_item *item;
332 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400333 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500334 if (btrfs_test_opt(root, NODATASUM) ||
335 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500336 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400337 mutex_lock(&root->fs_info->fs_mutex);
338 path = btrfs_alloc_path();
339 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
340 if (IS_ERR(item)) {
341 ret = PTR_ERR(item);
342 /* a csum that isn't present is a preallocated region. */
343 if (ret == -ENOENT || ret == -EFBIG)
344 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400345 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500346 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400347 goto out;
348 }
Chris Masonff79f812007-10-15 16:22:25 -0400349 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
350 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500351 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400352out:
353 if (path)
354 btrfs_free_path(path);
355 mutex_unlock(&root->fs_info->fs_mutex);
356 return ret;
357}
358
Chris Mason70dec802008-01-29 09:59:12 -0500359int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
360 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400361{
Chris Mason35ebb932007-10-30 16:56:53 -0400362 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400363 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500364 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400365 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500366 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400367 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400368 struct btrfs_root *root = BTRFS_I(inode)->root;
369 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400370 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500371
Yanb98b6762008-01-08 15:54:37 -0500372 if (btrfs_test_opt(root, NODATASUM) ||
373 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500374 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500375 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500376 private = state->private;
377 ret = 0;
378 } else {
379 ret = get_state_private(io_tree, start, &private);
380 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400381 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400382 kaddr = kmap_atomic(page, KM_IRQ0);
383 if (ret) {
384 goto zeroit;
385 }
Chris Masonff79f812007-10-15 16:22:25 -0400386 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
387 btrfs_csum_final(csum, (char *)&csum);
388 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400389 goto zeroit;
390 }
391 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400392 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400393 return 0;
394
395zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500396 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
397 page->mapping->host->i_ino, (unsigned long long)start, csum,
398 private);
Chris Masondb945352007-10-15 16:15:53 -0400399 memset(kaddr + offset, 1, end - start + 1);
400 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400401 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400402 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400403 return 0;
404}
Chris Masonb888db22007-08-27 16:49:44 -0400405
Chris Mason39279cc2007-06-12 06:35:45 -0400406void btrfs_read_locked_inode(struct inode *inode)
407{
408 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400409 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400410 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400411 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400412 struct btrfs_root *root = BTRFS_I(inode)->root;
413 struct btrfs_key location;
414 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400415 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400416 int ret;
417
418 path = btrfs_alloc_path();
419 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400420 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400421 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500422
Chris Mason39279cc2007-06-12 06:35:45 -0400423 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400424 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400425 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400426
Chris Mason5f39d392007-10-15 16:14:19 -0400427 leaf = path->nodes[0];
428 inode_item = btrfs_item_ptr(leaf, path->slots[0],
429 struct btrfs_inode_item);
430
431 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
432 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
433 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
434 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
435 inode->i_size = btrfs_inode_size(leaf, inode_item);
436
437 tspec = btrfs_inode_atime(inode_item);
438 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
439 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
440
441 tspec = btrfs_inode_mtime(inode_item);
442 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
443 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
444
445 tspec = btrfs_inode_ctime(inode_item);
446 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
447 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
448
449 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
450 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400451 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400452 rdev = btrfs_inode_rdev(leaf, inode_item);
453
454 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400455 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
456 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500457 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500458 if (!BTRFS_I(inode)->block_group) {
459 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400460 NULL, 0,
461 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500462 }
Chris Mason39279cc2007-06-12 06:35:45 -0400463 btrfs_free_path(path);
464 inode_item = NULL;
465
466 mutex_unlock(&root->fs_info->fs_mutex);
467
468 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400469 case S_IFREG:
470 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500471 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400472 inode->i_fop = &btrfs_file_operations;
473 inode->i_op = &btrfs_file_inode_operations;
474 break;
475 case S_IFDIR:
476 inode->i_fop = &btrfs_dir_file_operations;
477 if (root == root->fs_info->tree_root)
478 inode->i_op = &btrfs_dir_ro_inode_operations;
479 else
480 inode->i_op = &btrfs_dir_inode_operations;
481 break;
482 case S_IFLNK:
483 inode->i_op = &btrfs_symlink_inode_operations;
484 inode->i_mapping->a_ops = &btrfs_symlink_aops;
485 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400486 default:
487 init_special_inode(inode, inode->i_mode, rdev);
488 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400489 }
490 return;
491
492make_bad:
493 btrfs_release_path(root, path);
494 btrfs_free_path(path);
495 mutex_unlock(&root->fs_info->fs_mutex);
496 make_bad_inode(inode);
497}
498
Chris Mason5f39d392007-10-15 16:14:19 -0400499static void fill_inode_item(struct extent_buffer *leaf,
500 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400501 struct inode *inode)
502{
Chris Mason5f39d392007-10-15 16:14:19 -0400503 btrfs_set_inode_uid(leaf, item, inode->i_uid);
504 btrfs_set_inode_gid(leaf, item, inode->i_gid);
505 btrfs_set_inode_size(leaf, item, inode->i_size);
506 btrfs_set_inode_mode(leaf, item, inode->i_mode);
507 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
508
509 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
510 inode->i_atime.tv_sec);
511 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
512 inode->i_atime.tv_nsec);
513
514 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
515 inode->i_mtime.tv_sec);
516 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
517 inode->i_mtime.tv_nsec);
518
519 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
520 inode->i_ctime.tv_sec);
521 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
522 inode->i_ctime.tv_nsec);
523
524 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
525 btrfs_set_inode_generation(leaf, item, inode->i_generation);
526 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500527 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400528 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400529 BTRFS_I(inode)->block_group->key.objectid);
530}
531
Chris Masona52d9a82007-08-27 16:49:44 -0400532int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400533 struct btrfs_root *root,
534 struct inode *inode)
535{
536 struct btrfs_inode_item *inode_item;
537 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400538 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400539 int ret;
540
541 path = btrfs_alloc_path();
542 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400543 ret = btrfs_lookup_inode(trans, root, path,
544 &BTRFS_I(inode)->location, 1);
545 if (ret) {
546 if (ret > 0)
547 ret = -ENOENT;
548 goto failed;
549 }
550
Chris Mason5f39d392007-10-15 16:14:19 -0400551 leaf = path->nodes[0];
552 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400553 struct btrfs_inode_item);
554
Chris Mason5f39d392007-10-15 16:14:19 -0400555 fill_inode_item(leaf, inode_item, inode);
556 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400557 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400558 ret = 0;
559failed:
560 btrfs_release_path(root, path);
561 btrfs_free_path(path);
562 return ret;
563}
564
565
566static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root,
568 struct inode *dir,
569 struct dentry *dentry)
570{
571 struct btrfs_path *path;
572 const char *name = dentry->d_name.name;
573 int name_len = dentry->d_name.len;
574 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400575 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400576 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400577 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400578
579 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400580 if (!path) {
581 ret = -ENOMEM;
582 goto err;
583 }
584
Chris Mason39279cc2007-06-12 06:35:45 -0400585 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
586 name, name_len, -1);
587 if (IS_ERR(di)) {
588 ret = PTR_ERR(di);
589 goto err;
590 }
591 if (!di) {
592 ret = -ENOENT;
593 goto err;
594 }
Chris Mason5f39d392007-10-15 16:14:19 -0400595 leaf = path->nodes[0];
596 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400597 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400598 if (ret)
599 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400600 btrfs_release_path(root, path);
601
602 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400603 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400604 if (IS_ERR(di)) {
605 ret = PTR_ERR(di);
606 goto err;
607 }
608 if (!di) {
609 ret = -ENOENT;
610 goto err;
611 }
612 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400613
614 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500615 ret = btrfs_del_inode_ref(trans, root, name, name_len,
616 dentry->d_inode->i_ino,
617 dentry->d_parent->d_inode->i_ino);
618 if (ret) {
619 printk("failed to delete reference to %.*s, "
620 "inode %lu parent %lu\n", name_len, name,
621 dentry->d_inode->i_ino,
622 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500623 }
Chris Mason39279cc2007-06-12 06:35:45 -0400624err:
625 btrfs_free_path(path);
626 if (!ret) {
627 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400628 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400629 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500630#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
631 dentry->d_inode->i_nlink--;
632#else
Chris Mason39279cc2007-06-12 06:35:45 -0400633 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500634#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400635 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400636 dir->i_sb->s_dirt = 1;
637 }
638 return ret;
639}
640
641static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
642{
643 struct btrfs_root *root;
644 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500645 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400646 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500647 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400648
649 root = BTRFS_I(dir)->root;
650 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500651
652 ret = btrfs_check_free_space(root, 1, 1);
653 if (ret)
654 goto fail;
655
Chris Mason39279cc2007-06-12 06:35:45 -0400656 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400657
Chris Mason39279cc2007-06-12 06:35:45 -0400658 btrfs_set_trans_block_group(trans, dir);
659 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400660 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400661
Chris Mason2da98f02008-01-16 11:44:43 -0500662 if (inode->i_nlink == 0) {
663 int found;
664 /* if the inode isn't linked anywhere,
665 * we don't need to worry about
666 * data=ordered
667 */
668 found = btrfs_del_ordered_inode(inode);
669 if (found == 1) {
670 atomic_dec(&inode->i_count);
671 }
672 }
673
Chris Mason39279cc2007-06-12 06:35:45 -0400674 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500675fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400676 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400677 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500678 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400679 return ret;
680}
681
682static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
683{
684 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500685 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400686 int ret;
687 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400688 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500689 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400690
Yan134d4512007-10-25 15:49:25 -0400691 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
692 return -ENOTEMPTY;
693
Chris Mason39279cc2007-06-12 06:35:45 -0400694 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500695 ret = btrfs_check_free_space(root, 1, 1);
696 if (ret)
697 goto fail;
698
Chris Mason39279cc2007-06-12 06:35:45 -0400699 trans = btrfs_start_transaction(root, 1);
700 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400701
702 /* now the directory is empty */
703 err = btrfs_unlink_trans(trans, root, dir, dentry);
704 if (!err) {
705 inode->i_size = 0;
706 }
Chris Mason39544012007-12-12 14:38:19 -0500707
Chris Masond3c2fdc2007-09-17 10:58:06 -0400708 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400709 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500710fail:
Yan134d4512007-10-25 15:49:25 -0400711 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400712 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500713 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500714
Chris Mason39279cc2007-06-12 06:35:45 -0400715 if (ret && !err)
716 err = ret;
717 return err;
718}
719
Chris Mason39279cc2007-06-12 06:35:45 -0400720/*
Chris Mason39279cc2007-06-12 06:35:45 -0400721 * this can truncate away extent items, csum items and directory items.
722 * It starts at a high offset and removes keys until it can't find
723 * any higher than i_size.
724 *
725 * csum items that cross the new i_size are truncated to the new size
726 * as well.
727 */
728static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
729 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500730 struct inode *inode,
731 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400732{
733 int ret;
734 struct btrfs_path *path;
735 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400736 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400737 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400738 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400739 struct btrfs_file_extent_item *fi;
740 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400741 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400742 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500743 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500744 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400745 int found_extent;
746 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500747 int pending_del_nr = 0;
748 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400749 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400750
Chris Masona52d9a82007-08-27 16:49:44 -0400751 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400752 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400753 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400754 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400755
Chris Mason39279cc2007-06-12 06:35:45 -0400756 /* FIXME, add redo link to tree so we don't leak on crash */
757 key.objectid = inode->i_ino;
758 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400759 key.type = (u8)-1;
760
Chris Mason85e21ba2008-01-29 15:11:36 -0500761 btrfs_init_path(path);
762search_again:
763 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
764 if (ret < 0) {
765 goto error;
766 }
767 if (ret > 0) {
768 BUG_ON(path->slots[0] == 0);
769 path->slots[0]--;
770 }
771
Chris Mason39279cc2007-06-12 06:35:45 -0400772 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400773 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 leaf = path->nodes[0];
775 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
776 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400777
Chris Mason5f39d392007-10-15 16:14:19 -0400778 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400779 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400780
Chris Mason85e21ba2008-01-29 15:11:36 -0500781 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400782 break;
783
Chris Mason5f39d392007-10-15 16:14:19 -0400784 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400785 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400786 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400787 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400788 extent_type = btrfs_file_extent_type(leaf, fi);
789 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400790 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400791 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400792 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
793 struct btrfs_item *item = btrfs_item_nr(leaf,
794 path->slots[0]);
795 item_end += btrfs_file_extent_inline_len(leaf,
796 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400797 }
Yan008630c2007-11-07 13:31:09 -0500798 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400799 }
800 if (found_type == BTRFS_CSUM_ITEM_KEY) {
801 ret = btrfs_csum_truncate(trans, root, path,
802 inode->i_size);
803 BUG_ON(ret);
804 }
Yan008630c2007-11-07 13:31:09 -0500805 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400806 if (found_type == BTRFS_DIR_ITEM_KEY) {
807 found_type = BTRFS_INODE_ITEM_KEY;
808 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
809 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500810 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
811 found_type = BTRFS_XATTR_ITEM_KEY;
812 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
813 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400814 } else if (found_type) {
815 found_type--;
816 } else {
817 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400818 }
Yana61721d2007-09-17 11:08:38 -0400819 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500820 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400821 }
Chris Mason5f39d392007-10-15 16:14:19 -0400822 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400823 del_item = 1;
824 else
825 del_item = 0;
826 found_extent = 0;
827
828 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400829 if (found_type != BTRFS_EXTENT_DATA_KEY)
830 goto delete;
831
832 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400833 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400834 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400835 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400836 u64 orig_num_bytes =
837 btrfs_file_extent_num_bytes(leaf, fi);
838 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400839 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -0500840 extent_num_bytes = extent_num_bytes &
841 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400842 btrfs_set_file_extent_num_bytes(leaf, fi,
843 extent_num_bytes);
844 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -0500845 extent_num_bytes);
846 if (extent_start != 0)
847 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -0400848 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400849 } else {
Chris Masondb945352007-10-15 16:15:53 -0400850 extent_num_bytes =
851 btrfs_file_extent_disk_num_bytes(leaf,
852 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400853 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -0500854 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400855 if (extent_start != 0) {
856 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -0500857 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -0400858 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500859 root_gen = btrfs_header_generation(leaf);
860 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400861 }
Chris Mason90692182008-02-08 13:49:28 -0500862 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
863 if (!del_item) {
864 u32 newsize = inode->i_size - found_key.offset;
865 dec_i_blocks(inode, item_end + 1 -
866 found_key.offset - newsize);
867 newsize =
868 btrfs_file_extent_calc_inline_size(newsize);
869 ret = btrfs_truncate_item(trans, root, path,
870 newsize, 1);
871 BUG_ON(ret);
872 } else {
873 dec_i_blocks(inode, item_end + 1 -
874 found_key.offset);
875 }
Chris Mason39279cc2007-06-12 06:35:45 -0400876 }
Chris Mason179e29e2007-11-01 11:28:41 -0400877delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400878 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500879 if (!pending_del_nr) {
880 /* no pending yet, add ourselves */
881 pending_del_slot = path->slots[0];
882 pending_del_nr = 1;
883 } else if (pending_del_nr &&
884 path->slots[0] + 1 == pending_del_slot) {
885 /* hop on the pending chunk */
886 pending_del_nr++;
887 pending_del_slot = path->slots[0];
888 } else {
889 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
890 }
Chris Mason39279cc2007-06-12 06:35:45 -0400891 } else {
892 break;
893 }
Chris Mason39279cc2007-06-12 06:35:45 -0400894 if (found_extent) {
895 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500896 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500897 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500898 root_gen, inode->i_ino,
899 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400900 BUG_ON(ret);
901 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500902next:
903 if (path->slots[0] == 0) {
904 if (pending_del_nr)
905 goto del_pending;
906 btrfs_release_path(root, path);
907 goto search_again;
908 }
909
910 path->slots[0]--;
911 if (pending_del_nr &&
912 path->slots[0] + 1 != pending_del_slot) {
913 struct btrfs_key debug;
914del_pending:
915 btrfs_item_key_to_cpu(path->nodes[0], &debug,
916 pending_del_slot);
917 ret = btrfs_del_items(trans, root, path,
918 pending_del_slot,
919 pending_del_nr);
920 BUG_ON(ret);
921 pending_del_nr = 0;
922 btrfs_release_path(root, path);
923 goto search_again;
924 }
Chris Mason39279cc2007-06-12 06:35:45 -0400925 }
926 ret = 0;
927error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500928 if (pending_del_nr) {
929 ret = btrfs_del_items(trans, root, path, pending_del_slot,
930 pending_del_nr);
931 }
Chris Mason39279cc2007-06-12 06:35:45 -0400932 btrfs_release_path(root, path);
933 btrfs_free_path(path);
934 inode->i_sb->s_dirt = 1;
935 return ret;
936}
937
Chris Masonb888db22007-08-27 16:49:44 -0400938static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400939 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400940{
Chris Mason39279cc2007-06-12 06:35:45 -0400941 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500942 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400943 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400944 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500945 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400946
Chris Mason190662b2007-12-18 16:25:45 -0500947 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400948 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400949
Chris Masond1310b22008-01-24 16:13:08 -0500950 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500951 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400952 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500953
Chris Masona52d9a82007-08-27 16:49:44 -0400954 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400955 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400956 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
957 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400958 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400959 }
Chris Masonb888db22007-08-27 16:49:44 -0400960 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500961 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400962
Chris Masona52d9a82007-08-27 16:49:44 -0400963 return ret;
964}
965
966/*
967 * taken from block_truncate_page, but does cow as it zeros out
968 * any bytes left in the last page in the file.
969 */
970static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
971{
972 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400973 struct btrfs_root *root = BTRFS_I(inode)->root;
974 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400975 pgoff_t index = from >> PAGE_CACHE_SHIFT;
976 unsigned offset = from & (PAGE_CACHE_SIZE-1);
977 struct page *page;
978 int ret = 0;
979 u64 page_start;
980
981 if ((offset & (blocksize - 1)) == 0)
982 goto out;
983
984 ret = -ENOMEM;
985 page = grab_cache_page(mapping, index);
986 if (!page)
987 goto out;
988 if (!PageUptodate(page)) {
989 ret = btrfs_readpage(NULL, page);
990 lock_page(page);
991 if (!PageUptodate(page)) {
992 ret = -EIO;
993 goto out;
994 }
995 }
Chris Mason35ebb932007-10-30 16:56:53 -0400996 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400997
Chris Masonb888db22007-08-27 16:49:44 -0400998 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400999
Chris Mason39279cc2007-06-12 06:35:45 -04001000 unlock_page(page);
1001 page_cache_release(page);
1002out:
1003 return ret;
1004}
1005
1006static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1007{
1008 struct inode *inode = dentry->d_inode;
1009 int err;
1010
1011 err = inode_change_ok(inode, attr);
1012 if (err)
1013 return err;
1014
1015 if (S_ISREG(inode->i_mode) &&
1016 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1017 struct btrfs_trans_handle *trans;
1018 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001019 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001022 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001023 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001024 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001025 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001026
Chris Mason1b0f7c22008-01-30 14:33:02 -05001027 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001028 goto out;
1029
Chris Mason1832a6d2007-12-21 16:27:21 -05001030 mutex_lock(&root->fs_info->fs_mutex);
1031 err = btrfs_check_free_space(root, 1, 0);
1032 mutex_unlock(&root->fs_info->fs_mutex);
1033 if (err)
1034 goto fail;
1035
Chris Mason39279cc2007-06-12 06:35:45 -04001036 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1037
Chris Mason1b0f7c22008-01-30 14:33:02 -05001038 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001039 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001040
1041 mutex_lock(&root->fs_info->fs_mutex);
1042 trans = btrfs_start_transaction(root, 1);
1043 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001044 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001045 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001046 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001047
Chris Mason179e29e2007-11-01 11:28:41 -04001048 if (alloc_hint != EXTENT_MAP_INLINE) {
1049 err = btrfs_insert_file_extent(trans, root,
1050 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001051 hole_start, 0, 0,
1052 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001053 btrfs_drop_extent_cache(inode, hole_start,
1054 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001055 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001056 }
Chris Mason39279cc2007-06-12 06:35:45 -04001057 btrfs_end_transaction(trans, root);
1058 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001059 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001060 if (err)
1061 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001062 }
1063out:
1064 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001065fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001066 return err;
1067}
Chris Mason61295eb2008-01-14 16:24:38 -05001068
Chris Mason2da98f02008-01-16 11:44:43 -05001069void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001070{
Chris Mason2da98f02008-01-16 11:44:43 -05001071 int ret;
1072
1073 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001074 return;
1075 }
Chris Mason2da98f02008-01-16 11:44:43 -05001076
1077 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1078 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1079 return;
1080
1081 ret = btrfs_del_ordered_inode(inode);
1082 if (ret == 1) {
1083 atomic_dec(&inode->i_count);
1084 }
Chris Mason61295eb2008-01-14 16:24:38 -05001085}
1086
Chris Mason39279cc2007-06-12 06:35:45 -04001087void btrfs_delete_inode(struct inode *inode)
1088{
1089 struct btrfs_trans_handle *trans;
1090 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001091 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001092 int ret;
1093
1094 truncate_inode_pages(&inode->i_data, 0);
1095 if (is_bad_inode(inode)) {
1096 goto no_delete;
1097 }
Chris Mason5f39d392007-10-15 16:14:19 -04001098
Chris Mason39279cc2007-06-12 06:35:45 -04001099 inode->i_size = 0;
1100 mutex_lock(&root->fs_info->fs_mutex);
1101 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001102
Chris Mason39279cc2007-06-12 06:35:45 -04001103 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001104 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001105 if (ret)
1106 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001107
Chris Masond3c2fdc2007-09-17 10:58:06 -04001108 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001109 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001110
Chris Mason39279cc2007-06-12 06:35:45 -04001111 btrfs_end_transaction(trans, root);
1112 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001113 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001114 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001115 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001116
1117no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001118 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001119 btrfs_end_transaction(trans, root);
1120 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001121 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001122 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001123no_delete:
1124 clear_inode(inode);
1125}
1126
1127/*
1128 * this returns the key found in the dir entry in the location pointer.
1129 * If no dir entries were found, location->objectid is 0.
1130 */
1131static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1132 struct btrfs_key *location)
1133{
1134 const char *name = dentry->d_name.name;
1135 int namelen = dentry->d_name.len;
1136 struct btrfs_dir_item *di;
1137 struct btrfs_path *path;
1138 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001139 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001140
Chris Mason39544012007-12-12 14:38:19 -05001141 if (namelen == 1 && strcmp(name, ".") == 0) {
1142 location->objectid = dir->i_ino;
1143 location->type = BTRFS_INODE_ITEM_KEY;
1144 location->offset = 0;
1145 return 0;
1146 }
Chris Mason39279cc2007-06-12 06:35:45 -04001147 path = btrfs_alloc_path();
1148 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001149
Chris Mason7a720532007-12-13 09:06:59 -05001150 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001151 struct btrfs_key key;
1152 struct extent_buffer *leaf;
1153 u32 nritems;
1154 int slot;
1155
1156 key.objectid = dir->i_ino;
1157 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1158 key.offset = 0;
1159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1160 BUG_ON(ret == 0);
1161 ret = 0;
1162
1163 leaf = path->nodes[0];
1164 slot = path->slots[0];
1165 nritems = btrfs_header_nritems(leaf);
1166 if (slot >= nritems)
1167 goto out_err;
1168
1169 btrfs_item_key_to_cpu(leaf, &key, slot);
1170 if (key.objectid != dir->i_ino ||
1171 key.type != BTRFS_INODE_REF_KEY) {
1172 goto out_err;
1173 }
1174 location->objectid = key.offset;
1175 location->type = BTRFS_INODE_ITEM_KEY;
1176 location->offset = 0;
1177 goto out;
1178 }
1179
Chris Mason39279cc2007-06-12 06:35:45 -04001180 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1181 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001182 if (IS_ERR(di))
1183 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001184 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001185 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001186 }
Chris Mason5f39d392007-10-15 16:14:19 -04001187 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001188out:
Chris Mason39279cc2007-06-12 06:35:45 -04001189 btrfs_free_path(path);
1190 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001191out_err:
1192 location->objectid = 0;
1193 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001194}
1195
1196/*
1197 * when we hit a tree root in a directory, the btrfs part of the inode
1198 * needs to be changed to reflect the root directory of the tree root. This
1199 * is kind of like crossing a mount point.
1200 */
1201static int fixup_tree_root_location(struct btrfs_root *root,
1202 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001203 struct btrfs_root **sub_root,
1204 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001205{
1206 struct btrfs_path *path;
1207 struct btrfs_root_item *ri;
1208
1209 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1210 return 0;
1211 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1212 return 0;
1213
1214 path = btrfs_alloc_path();
1215 BUG_ON(!path);
1216 mutex_lock(&root->fs_info->fs_mutex);
1217
Josef Bacik58176a92007-08-29 15:47:34 -04001218 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1219 dentry->d_name.name,
1220 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001221 if (IS_ERR(*sub_root))
1222 return PTR_ERR(*sub_root);
1223
1224 ri = &(*sub_root)->root_item;
1225 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001226 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1227 location->offset = 0;
1228
1229 btrfs_free_path(path);
1230 mutex_unlock(&root->fs_info->fs_mutex);
1231 return 0;
1232}
1233
1234static int btrfs_init_locked_inode(struct inode *inode, void *p)
1235{
1236 struct btrfs_iget_args *args = p;
1237 inode->i_ino = args->ino;
1238 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001239 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001240 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1241 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001242 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001243 return 0;
1244}
1245
1246static int btrfs_find_actor(struct inode *inode, void *opaque)
1247{
1248 struct btrfs_iget_args *args = opaque;
1249 return (args->ino == inode->i_ino &&
1250 args->root == BTRFS_I(inode)->root);
1251}
1252
Chris Masondc17ff82008-01-08 15:46:30 -05001253struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1254 u64 root_objectid)
1255{
1256 struct btrfs_iget_args args;
1257 args.ino = objectid;
1258 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1259
1260 if (!args.root)
1261 return NULL;
1262
1263 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1264}
1265
Chris Mason39279cc2007-06-12 06:35:45 -04001266struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1267 struct btrfs_root *root)
1268{
1269 struct inode *inode;
1270 struct btrfs_iget_args args;
1271 args.ino = objectid;
1272 args.root = root;
1273
1274 inode = iget5_locked(s, objectid, btrfs_find_actor,
1275 btrfs_init_locked_inode,
1276 (void *)&args);
1277 return inode;
1278}
1279
1280static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1281 struct nameidata *nd)
1282{
1283 struct inode * inode;
1284 struct btrfs_inode *bi = BTRFS_I(dir);
1285 struct btrfs_root *root = bi->root;
1286 struct btrfs_root *sub_root = root;
1287 struct btrfs_key location;
1288 int ret;
1289
1290 if (dentry->d_name.len > BTRFS_NAME_LEN)
1291 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001292
Chris Mason39279cc2007-06-12 06:35:45 -04001293 mutex_lock(&root->fs_info->fs_mutex);
1294 ret = btrfs_inode_by_name(dir, dentry, &location);
1295 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001296
Chris Mason39279cc2007-06-12 06:35:45 -04001297 if (ret < 0)
1298 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001299
Chris Mason39279cc2007-06-12 06:35:45 -04001300 inode = NULL;
1301 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001302 ret = fixup_tree_root_location(root, &location, &sub_root,
1303 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001304 if (ret < 0)
1305 return ERR_PTR(ret);
1306 if (ret > 0)
1307 return ERR_PTR(-ENOENT);
1308 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1309 sub_root);
1310 if (!inode)
1311 return ERR_PTR(-EACCES);
1312 if (inode->i_state & I_NEW) {
1313 /* the inode and parent dir are two different roots */
1314 if (sub_root != root) {
1315 igrab(inode);
1316 sub_root->inode = inode;
1317 }
1318 BTRFS_I(inode)->root = sub_root;
1319 memcpy(&BTRFS_I(inode)->location, &location,
1320 sizeof(location));
1321 btrfs_read_locked_inode(inode);
1322 unlock_new_inode(inode);
1323 }
1324 }
1325 return d_splice_alias(inode, dentry);
1326}
1327
Chris Mason39279cc2007-06-12 06:35:45 -04001328static unsigned char btrfs_filetype_table[] = {
1329 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1330};
1331
1332static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1333{
Chris Mason6da6aba2007-12-18 16:15:09 -05001334 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001335 struct btrfs_root *root = BTRFS_I(inode)->root;
1336 struct btrfs_item *item;
1337 struct btrfs_dir_item *di;
1338 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001339 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001340 struct btrfs_path *path;
1341 int ret;
1342 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001343 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001344 int slot;
1345 int advance;
1346 unsigned char d_type;
1347 int over = 0;
1348 u32 di_cur;
1349 u32 di_total;
1350 u32 di_len;
1351 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001352 char tmp_name[32];
1353 char *name_ptr;
1354 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001355
1356 /* FIXME, use a real flag for deciding about the key type */
1357 if (root->fs_info->tree_root == root)
1358 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001359
Chris Mason39544012007-12-12 14:38:19 -05001360 /* special case for "." */
1361 if (filp->f_pos == 0) {
1362 over = filldir(dirent, ".", 1,
1363 1, inode->i_ino,
1364 DT_DIR);
1365 if (over)
1366 return 0;
1367 filp->f_pos = 1;
1368 }
1369
Chris Mason39279cc2007-06-12 06:35:45 -04001370 mutex_lock(&root->fs_info->fs_mutex);
1371 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001372 path = btrfs_alloc_path();
1373 path->reada = 2;
1374
1375 /* special case for .., just use the back ref */
1376 if (filp->f_pos == 1) {
1377 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1378 key.offset = 0;
1379 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1380 BUG_ON(ret == 0);
1381 leaf = path->nodes[0];
1382 slot = path->slots[0];
1383 nritems = btrfs_header_nritems(leaf);
1384 if (slot >= nritems) {
1385 btrfs_release_path(root, path);
1386 goto read_dir_items;
1387 }
1388 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1389 btrfs_release_path(root, path);
1390 if (found_key.objectid != key.objectid ||
1391 found_key.type != BTRFS_INODE_REF_KEY)
1392 goto read_dir_items;
1393 over = filldir(dirent, "..", 2,
1394 2, found_key.offset, DT_DIR);
1395 if (over)
1396 goto nopos;
1397 filp->f_pos = 2;
1398 }
1399
1400read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001401 btrfs_set_key_type(&key, key_type);
1402 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001403
Chris Mason39279cc2007-06-12 06:35:45 -04001404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1405 if (ret < 0)
1406 goto err;
1407 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001408 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001409 leaf = path->nodes[0];
1410 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001411 slot = path->slots[0];
1412 if (advance || slot >= nritems) {
1413 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001414 ret = btrfs_next_leaf(root, path);
1415 if (ret)
1416 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001417 leaf = path->nodes[0];
1418 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001419 slot = path->slots[0];
1420 } else {
1421 slot++;
1422 path->slots[0]++;
1423 }
1424 }
1425 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001426 item = btrfs_item_nr(leaf, slot);
1427 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1428
1429 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001430 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001431 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001432 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001433 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001434 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001435
1436 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001437 advance = 1;
1438 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1439 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001440 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001441 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001442 struct btrfs_key location;
1443
1444 name_len = btrfs_dir_name_len(leaf, di);
1445 if (name_len < 32) {
1446 name_ptr = tmp_name;
1447 } else {
1448 name_ptr = kmalloc(name_len, GFP_NOFS);
1449 BUG_ON(!name_ptr);
1450 }
1451 read_extent_buffer(leaf, name_ptr,
1452 (unsigned long)(di + 1), name_len);
1453
1454 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1455 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001456 over = filldir(dirent, name_ptr, name_len,
1457 found_key.offset,
1458 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001459 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001460
1461 if (name_ptr != tmp_name)
1462 kfree(name_ptr);
1463
Chris Mason39279cc2007-06-12 06:35:45 -04001464 if (over)
1465 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001466 di_len = btrfs_dir_name_len(leaf, di) +
1467 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001468 di_cur += di_len;
1469 di = (struct btrfs_dir_item *)((char *)di + di_len);
1470 }
1471 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001472 if (key_type == BTRFS_DIR_INDEX_KEY)
1473 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1474 else
1475 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001476nopos:
1477 ret = 0;
1478err:
1479 btrfs_release_path(root, path);
1480 btrfs_free_path(path);
1481 mutex_unlock(&root->fs_info->fs_mutex);
1482 return ret;
1483}
1484
1485int btrfs_write_inode(struct inode *inode, int wait)
1486{
1487 struct btrfs_root *root = BTRFS_I(inode)->root;
1488 struct btrfs_trans_handle *trans;
1489 int ret = 0;
1490
1491 if (wait) {
1492 mutex_lock(&root->fs_info->fs_mutex);
1493 trans = btrfs_start_transaction(root, 1);
1494 btrfs_set_trans_block_group(trans, inode);
1495 ret = btrfs_commit_transaction(trans, root);
1496 mutex_unlock(&root->fs_info->fs_mutex);
1497 }
1498 return ret;
1499}
1500
1501/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001502 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001503 * inode changes. But, it is most likely to find the inode in cache.
1504 * FIXME, needs more benchmarking...there are no reasons other than performance
1505 * to keep or drop this code.
1506 */
1507void btrfs_dirty_inode(struct inode *inode)
1508{
1509 struct btrfs_root *root = BTRFS_I(inode)->root;
1510 struct btrfs_trans_handle *trans;
1511
1512 mutex_lock(&root->fs_info->fs_mutex);
1513 trans = btrfs_start_transaction(root, 1);
1514 btrfs_set_trans_block_group(trans, inode);
1515 btrfs_update_inode(trans, root, inode);
1516 btrfs_end_transaction(trans, root);
1517 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001518}
1519
1520static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1521 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001522 const char *name, int name_len,
1523 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001524 u64 objectid,
1525 struct btrfs_block_group_cache *group,
1526 int mode)
1527{
1528 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001529 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001530 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001531 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001532 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001533 struct btrfs_inode_ref *ref;
1534 struct btrfs_key key[2];
1535 u32 sizes[2];
1536 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001537 int ret;
1538 int owner;
1539
Chris Mason5f39d392007-10-15 16:14:19 -04001540 path = btrfs_alloc_path();
1541 BUG_ON(!path);
1542
Chris Mason39279cc2007-06-12 06:35:45 -04001543 inode = new_inode(root->fs_info->sb);
1544 if (!inode)
1545 return ERR_PTR(-ENOMEM);
1546
Chris Masond1310b22008-01-24 16:13:08 -05001547 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1548 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001549 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001550 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001551 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001552
Chris Mason39279cc2007-06-12 06:35:45 -04001553 if (mode & S_IFDIR)
1554 owner = 0;
1555 else
1556 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001557 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001558 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001559 if (!new_inode_group) {
1560 printk("find_block group failed\n");
1561 new_inode_group = group;
1562 }
1563 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001564 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001565
1566 key[0].objectid = objectid;
1567 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1568 key[0].offset = 0;
1569
1570 key[1].objectid = objectid;
1571 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1572 key[1].offset = ref_objectid;
1573
1574 sizes[0] = sizeof(struct btrfs_inode_item);
1575 sizes[1] = name_len + sizeof(*ref);
1576
1577 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1578 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001579 goto fail;
1580
Chris Mason9c583092008-01-29 15:15:18 -05001581 if (objectid > root->highest_inode)
1582 root->highest_inode = objectid;
1583
Chris Mason39279cc2007-06-12 06:35:45 -04001584 inode->i_uid = current->fsuid;
1585 inode->i_gid = current->fsgid;
1586 inode->i_mode = mode;
1587 inode->i_ino = objectid;
1588 inode->i_blocks = 0;
1589 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001590 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1591 struct btrfs_inode_item);
1592 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001593
1594 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1595 struct btrfs_inode_ref);
1596 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1597 ptr = (unsigned long)(ref + 1);
1598 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1599
Chris Mason5f39d392007-10-15 16:14:19 -04001600 btrfs_mark_buffer_dirty(path->nodes[0]);
1601 btrfs_free_path(path);
1602
Chris Mason39279cc2007-06-12 06:35:45 -04001603 location = &BTRFS_I(inode)->location;
1604 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001605 location->offset = 0;
1606 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1607
Chris Mason39279cc2007-06-12 06:35:45 -04001608 insert_inode_hash(inode);
1609 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001610fail:
1611 btrfs_free_path(path);
1612 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001613}
1614
1615static inline u8 btrfs_inode_type(struct inode *inode)
1616{
1617 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1618}
1619
1620static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001621 struct dentry *dentry, struct inode *inode,
1622 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001623{
1624 int ret;
1625 struct btrfs_key key;
1626 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001627 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001628
Chris Mason39279cc2007-06-12 06:35:45 -04001629 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001630 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1631 key.offset = 0;
1632
1633 ret = btrfs_insert_dir_item(trans, root,
1634 dentry->d_name.name, dentry->d_name.len,
1635 dentry->d_parent->d_inode->i_ino,
1636 &key, btrfs_inode_type(inode));
1637 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001638 if (add_backref) {
1639 ret = btrfs_insert_inode_ref(trans, root,
1640 dentry->d_name.name,
1641 dentry->d_name.len,
1642 inode->i_ino,
1643 dentry->d_parent->d_inode->i_ino);
1644 }
Chris Mason79c44582007-06-25 10:09:33 -04001645 parent_inode = dentry->d_parent->d_inode;
1646 parent_inode->i_size += dentry->d_name.len * 2;
1647 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001648 ret = btrfs_update_inode(trans, root,
1649 dentry->d_parent->d_inode);
1650 }
1651 return ret;
1652}
1653
1654static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001655 struct dentry *dentry, struct inode *inode,
1656 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001657{
Chris Mason9c583092008-01-29 15:15:18 -05001658 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001659 if (!err) {
1660 d_instantiate(dentry, inode);
1661 return 0;
1662 }
1663 if (err > 0)
1664 err = -EEXIST;
1665 return err;
1666}
1667
Josef Bacik618e21d2007-07-11 10:18:17 -04001668static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1669 int mode, dev_t rdev)
1670{
1671 struct btrfs_trans_handle *trans;
1672 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001673 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001674 int err;
1675 int drop_inode = 0;
1676 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001677 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001678
1679 if (!new_valid_dev(rdev))
1680 return -EINVAL;
1681
1682 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001683 err = btrfs_check_free_space(root, 1, 0);
1684 if (err)
1685 goto fail;
1686
Josef Bacik618e21d2007-07-11 10:18:17 -04001687 trans = btrfs_start_transaction(root, 1);
1688 btrfs_set_trans_block_group(trans, dir);
1689
1690 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1691 if (err) {
1692 err = -ENOSPC;
1693 goto out_unlock;
1694 }
1695
Chris Mason9c583092008-01-29 15:15:18 -05001696 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1697 dentry->d_name.len,
1698 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001699 BTRFS_I(dir)->block_group, mode);
1700 err = PTR_ERR(inode);
1701 if (IS_ERR(inode))
1702 goto out_unlock;
1703
1704 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001705 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001706 if (err)
1707 drop_inode = 1;
1708 else {
1709 inode->i_op = &btrfs_special_inode_operations;
1710 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001711 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001712 }
1713 dir->i_sb->s_dirt = 1;
1714 btrfs_update_inode_block_group(trans, inode);
1715 btrfs_update_inode_block_group(trans, dir);
1716out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001717 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001718 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001719fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001720 mutex_unlock(&root->fs_info->fs_mutex);
1721
1722 if (drop_inode) {
1723 inode_dec_link_count(inode);
1724 iput(inode);
1725 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001726 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001727 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001728 return err;
1729}
1730
Chris Mason39279cc2007-06-12 06:35:45 -04001731static int btrfs_create(struct inode *dir, struct dentry *dentry,
1732 int mode, struct nameidata *nd)
1733{
1734 struct btrfs_trans_handle *trans;
1735 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001736 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001737 int err;
1738 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001739 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001740 u64 objectid;
1741
1742 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001743 err = btrfs_check_free_space(root, 1, 0);
1744 if (err)
1745 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001746 trans = btrfs_start_transaction(root, 1);
1747 btrfs_set_trans_block_group(trans, dir);
1748
1749 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1750 if (err) {
1751 err = -ENOSPC;
1752 goto out_unlock;
1753 }
1754
Chris Mason9c583092008-01-29 15:15:18 -05001755 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1756 dentry->d_name.len,
1757 dentry->d_parent->d_inode->i_ino,
1758 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001759 err = PTR_ERR(inode);
1760 if (IS_ERR(inode))
1761 goto out_unlock;
1762
1763 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001764 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001765 if (err)
1766 drop_inode = 1;
1767 else {
1768 inode->i_mapping->a_ops = &btrfs_aops;
1769 inode->i_fop = &btrfs_file_operations;
1770 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001771 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1772 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001773 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001774 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001775 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001776 }
1777 dir->i_sb->s_dirt = 1;
1778 btrfs_update_inode_block_group(trans, inode);
1779 btrfs_update_inode_block_group(trans, dir);
1780out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001781 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001782 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001783fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001784 mutex_unlock(&root->fs_info->fs_mutex);
1785
1786 if (drop_inode) {
1787 inode_dec_link_count(inode);
1788 iput(inode);
1789 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001790 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001791 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001792 return err;
1793}
1794
1795static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1796 struct dentry *dentry)
1797{
1798 struct btrfs_trans_handle *trans;
1799 struct btrfs_root *root = BTRFS_I(dir)->root;
1800 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001801 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001802 int err;
1803 int drop_inode = 0;
1804
1805 if (inode->i_nlink == 0)
1806 return -ENOENT;
1807
Chris Mason6da6aba2007-12-18 16:15:09 -05001808#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1809 inode->i_nlink++;
1810#else
Chris Mason39279cc2007-06-12 06:35:45 -04001811 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001812#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001813 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001814 err = btrfs_check_free_space(root, 1, 0);
1815 if (err)
1816 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001817 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001818
Chris Mason39279cc2007-06-12 06:35:45 -04001819 btrfs_set_trans_block_group(trans, dir);
1820 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001821 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001822
Chris Mason39279cc2007-06-12 06:35:45 -04001823 if (err)
1824 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001825
Chris Mason39279cc2007-06-12 06:35:45 -04001826 dir->i_sb->s_dirt = 1;
1827 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001828 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001829
Chris Mason54aa1f42007-06-22 14:16:25 -04001830 if (err)
1831 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001832
Chris Masond3c2fdc2007-09-17 10:58:06 -04001833 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001834 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001835fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001836 mutex_unlock(&root->fs_info->fs_mutex);
1837
1838 if (drop_inode) {
1839 inode_dec_link_count(inode);
1840 iput(inode);
1841 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001842 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001843 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001844 return err;
1845}
1846
Chris Mason39279cc2007-06-12 06:35:45 -04001847static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1848{
1849 struct inode *inode;
1850 struct btrfs_trans_handle *trans;
1851 struct btrfs_root *root = BTRFS_I(dir)->root;
1852 int err = 0;
1853 int drop_on_err = 0;
1854 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001855 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001856
1857 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001858 err = btrfs_check_free_space(root, 1, 0);
1859 if (err)
1860 goto out_unlock;
1861
Chris Mason39279cc2007-06-12 06:35:45 -04001862 trans = btrfs_start_transaction(root, 1);
1863 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001864
Chris Mason39279cc2007-06-12 06:35:45 -04001865 if (IS_ERR(trans)) {
1866 err = PTR_ERR(trans);
1867 goto out_unlock;
1868 }
1869
1870 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1871 if (err) {
1872 err = -ENOSPC;
1873 goto out_unlock;
1874 }
1875
Chris Mason9c583092008-01-29 15:15:18 -05001876 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1877 dentry->d_name.len,
1878 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001879 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1880 if (IS_ERR(inode)) {
1881 err = PTR_ERR(inode);
1882 goto out_fail;
1883 }
Chris Mason5f39d392007-10-15 16:14:19 -04001884
Chris Mason39279cc2007-06-12 06:35:45 -04001885 drop_on_err = 1;
1886 inode->i_op = &btrfs_dir_inode_operations;
1887 inode->i_fop = &btrfs_dir_file_operations;
1888 btrfs_set_trans_block_group(trans, inode);
1889
Chris Mason39544012007-12-12 14:38:19 -05001890 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001891 err = btrfs_update_inode(trans, root, inode);
1892 if (err)
1893 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001894
Chris Mason9c583092008-01-29 15:15:18 -05001895 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001896 if (err)
1897 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001898
Chris Mason39279cc2007-06-12 06:35:45 -04001899 d_instantiate(dentry, inode);
1900 drop_on_err = 0;
1901 dir->i_sb->s_dirt = 1;
1902 btrfs_update_inode_block_group(trans, inode);
1903 btrfs_update_inode_block_group(trans, dir);
1904
1905out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001906 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001907 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001908
Chris Mason39279cc2007-06-12 06:35:45 -04001909out_unlock:
1910 mutex_unlock(&root->fs_info->fs_mutex);
1911 if (drop_on_err)
1912 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001913 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001914 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001915 return err;
1916}
1917
Chris Masona52d9a82007-08-27 16:49:44 -04001918struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001919 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001920 int create)
1921{
1922 int ret;
1923 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001924 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001925 u64 extent_start = 0;
1926 u64 extent_end = 0;
1927 u64 objectid = inode->i_ino;
1928 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001929 struct btrfs_path *path;
1930 struct btrfs_root *root = BTRFS_I(inode)->root;
1931 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001932 struct extent_buffer *leaf;
1933 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001934 struct extent_map *em = NULL;
1935 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001936 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001937 struct btrfs_trans_handle *trans = NULL;
1938
1939 path = btrfs_alloc_path();
1940 BUG_ON(!path);
1941 mutex_lock(&root->fs_info->fs_mutex);
1942
1943again:
Chris Masond1310b22008-01-24 16:13:08 -05001944 spin_lock(&em_tree->lock);
1945 em = lookup_extent_mapping(em_tree, start, len);
1946 spin_unlock(&em_tree->lock);
1947
Chris Masona52d9a82007-08-27 16:49:44 -04001948 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001949 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001950 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1951 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001952 WARN_ON(1);
1953 }
Chris Mason70dec802008-01-29 09:59:12 -05001954 if (em->block_start == EXTENT_MAP_INLINE && page)
1955 free_extent_map(em);
1956 else
1957 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001958 }
Chris Masond1310b22008-01-24 16:13:08 -05001959 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001960 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001961 err = -ENOMEM;
1962 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001963 }
Chris Masond1310b22008-01-24 16:13:08 -05001964
1965 em->start = EXTENT_MAP_HOLE;
1966 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001967 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001968 ret = btrfs_lookup_file_extent(trans, root, path,
1969 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001970 if (ret < 0) {
1971 err = ret;
1972 goto out;
1973 }
1974
1975 if (ret != 0) {
1976 if (path->slots[0] == 0)
1977 goto not_found;
1978 path->slots[0]--;
1979 }
1980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 leaf = path->nodes[0];
1982 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001983 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001984 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001985 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1986 found_type = btrfs_key_type(&found_key);
1987 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001988 found_type != BTRFS_EXTENT_DATA_KEY) {
1989 goto not_found;
1990 }
1991
Chris Mason5f39d392007-10-15 16:14:19 -04001992 found_type = btrfs_file_extent_type(leaf, item);
1993 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001994 if (found_type == BTRFS_FILE_EXTENT_REG) {
1995 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001996 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001997 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001998 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001999 em->start = start;
2000 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002001 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002002 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002003 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002004 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002005 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002006 }
2007 goto not_found_em;
2008 }
Chris Masondb945352007-10-15 16:15:53 -04002009 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2010 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002011 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002012 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002013 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002014 goto insert;
2015 }
Chris Masondb945352007-10-15 16:15:53 -04002016 bytenr += btrfs_file_extent_offset(leaf, item);
2017 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002018 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002019 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002020 goto insert;
2021 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002022 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002023 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002024 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002025 size_t size;
2026 size_t extent_offset;
2027 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002028
Chris Mason5f39d392007-10-15 16:14:19 -04002029 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2030 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002031 extent_end = (extent_start + size + root->sectorsize - 1) &
2032 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002033 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002034 em->start = start;
2035 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002036 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002037 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002038 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002039 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002040 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002041 }
2042 goto not_found_em;
2043 }
2044 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002045
2046 if (!page) {
2047 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002048 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002049 goto out;
2050 }
2051
Chris Mason70dec802008-01-29 09:59:12 -05002052 page_start = page_offset(page) + pg_offset;
2053 extent_offset = page_start - extent_start;
2054 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002055 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002056 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002057 em->len = (copy_size + root->sectorsize - 1) &
2058 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002059 map = kmap(page);
2060 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002061 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002062 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002063 copy_size);
2064 flush_dcache_page(page);
2065 } else if (create && PageUptodate(page)) {
2066 if (!trans) {
2067 kunmap(page);
2068 free_extent_map(em);
2069 em = NULL;
2070 btrfs_release_path(root, path);
2071 trans = btrfs_start_transaction(root, 1);
2072 goto again;
2073 }
Chris Mason70dec802008-01-29 09:59:12 -05002074 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002075 copy_size);
2076 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002077 }
Chris Masona52d9a82007-08-27 16:49:44 -04002078 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002079 set_extent_uptodate(io_tree, em->start,
2080 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002081 goto insert;
2082 } else {
2083 printk("unkknown found_type %d\n", found_type);
2084 WARN_ON(1);
2085 }
2086not_found:
2087 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002088 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002089not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002090 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002091insert:
2092 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002093 if (em->start > start || extent_map_end(em) <= start) {
2094 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002095 err = -EIO;
2096 goto out;
2097 }
Chris Masond1310b22008-01-24 16:13:08 -05002098
2099 err = 0;
2100 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002101 ret = add_extent_mapping(em_tree, em);
2102 if (ret == -EEXIST) {
2103 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002104 em = lookup_extent_mapping(em_tree, start, len);
2105 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002106 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002107 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002108 }
Chris Masona52d9a82007-08-27 16:49:44 -04002109 }
Chris Masond1310b22008-01-24 16:13:08 -05002110 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002111out:
2112 btrfs_free_path(path);
2113 if (trans) {
2114 ret = btrfs_end_transaction(trans, root);
2115 if (!err)
2116 err = ret;
2117 }
2118 mutex_unlock(&root->fs_info->fs_mutex);
2119 if (err) {
2120 free_extent_map(em);
2121 WARN_ON(1);
2122 return ERR_PTR(err);
2123 }
2124 return em;
2125}
2126
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002127static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002128{
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002129 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002130}
2131
Chris Mason9ebefb182007-06-15 13:50:00 -04002132int btrfs_readpage(struct file *file, struct page *page)
2133{
Chris Masond1310b22008-01-24 16:13:08 -05002134 struct extent_io_tree *tree;
2135 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002136 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002137}
Chris Mason1832a6d2007-12-21 16:27:21 -05002138
Chris Mason39279cc2007-06-12 06:35:45 -04002139static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2140{
Chris Masond1310b22008-01-24 16:13:08 -05002141 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002142
2143
2144 if (current->flags & PF_MEMALLOC) {
2145 redirty_page_for_writepage(wbc, page);
2146 unlock_page(page);
2147 return 0;
2148 }
Chris Masond1310b22008-01-24 16:13:08 -05002149 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002150 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2151}
Chris Mason39279cc2007-06-12 06:35:45 -04002152
Chris Masonb293f022007-11-01 19:45:34 -04002153static int btrfs_writepages(struct address_space *mapping,
2154 struct writeback_control *wbc)
2155{
Chris Masond1310b22008-01-24 16:13:08 -05002156 struct extent_io_tree *tree;
2157 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002158 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2159}
2160
Chris Mason3ab2fb52007-11-08 10:59:22 -05002161static int
2162btrfs_readpages(struct file *file, struct address_space *mapping,
2163 struct list_head *pages, unsigned nr_pages)
2164{
Chris Masond1310b22008-01-24 16:13:08 -05002165 struct extent_io_tree *tree;
2166 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002167 return extent_readpages(tree, mapping, pages, nr_pages,
2168 btrfs_get_extent);
2169}
2170
Chris Mason70dec802008-01-29 09:59:12 -05002171static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002172{
Chris Masond1310b22008-01-24 16:13:08 -05002173 struct extent_io_tree *tree;
2174 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002175 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002176
Chris Masond1310b22008-01-24 16:13:08 -05002177 tree = &BTRFS_I(page->mapping->host)->io_tree;
2178 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002179 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002180 if (ret == 1) {
2181 ClearPagePrivate(page);
2182 set_page_private(page, 0);
2183 page_cache_release(page);
2184 }
2185 return ret;
2186}
Chris Mason39279cc2007-06-12 06:35:45 -04002187
Chris Masona52d9a82007-08-27 16:49:44 -04002188static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2189{
Chris Masond1310b22008-01-24 16:13:08 -05002190 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002191
Chris Masond1310b22008-01-24 16:13:08 -05002192 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002193 extent_invalidatepage(tree, page, offset);
2194 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002195}
2196
Chris Mason9ebefb182007-06-15 13:50:00 -04002197/*
2198 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2199 * called from a page fault handler when a page is first dirtied. Hence we must
2200 * be careful to check for EOF conditions here. We set the page up correctly
2201 * for a written page which means we get ENOSPC checking when writing into
2202 * holes and correct delalloc and unwritten extent mapping on filesystems that
2203 * support these features.
2204 *
2205 * We are not allowed to take the i_mutex here so we have to play games to
2206 * protect against truncate races as the page could now be beyond EOF. Because
2207 * vmtruncate() writes the inode size before removing pages, once we have the
2208 * page lock we can determine safely if the page is beyond EOF. If it is not
2209 * beyond EOF, then the page is guaranteed safe against truncation until we
2210 * unlock the page.
2211 */
2212int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2213{
Chris Mason6da6aba2007-12-18 16:15:09 -05002214 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002215 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002216 unsigned long end;
2217 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002218 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002219 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002220
Chris Mason1832a6d2007-12-21 16:27:21 -05002221 mutex_lock(&root->fs_info->fs_mutex);
2222 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002223 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002224 if (ret)
2225 goto out;
2226
2227 ret = -EINVAL;
2228
Chris Mason9ebefb182007-06-15 13:50:00 -04002229 lock_page(page);
2230 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002231 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002232 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002233
Chris Mason9ebefb182007-06-15 13:50:00 -04002234 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002235 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002236 /* page got truncated out from underneath us */
2237 goto out_unlock;
2238 }
2239
2240 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002241 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002242 end = size & ~PAGE_CACHE_MASK;
2243 else
2244 end = PAGE_CACHE_SIZE;
2245
Chris Masonb888db22007-08-27 16:49:44 -04002246 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002247
2248out_unlock:
2249 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002250out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002251 return ret;
2252}
2253
Chris Mason39279cc2007-06-12 06:35:45 -04002254static void btrfs_truncate(struct inode *inode)
2255{
2256 struct btrfs_root *root = BTRFS_I(inode)->root;
2257 int ret;
2258 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002259 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002260
2261 if (!S_ISREG(inode->i_mode))
2262 return;
2263 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2264 return;
2265
2266 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2267
2268 mutex_lock(&root->fs_info->fs_mutex);
2269 trans = btrfs_start_transaction(root, 1);
2270 btrfs_set_trans_block_group(trans, inode);
2271
2272 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002273 ret = btrfs_truncate_in_trans(trans, root, inode,
2274 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002275 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002276 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002277
Chris Mason39279cc2007-06-12 06:35:45 -04002278 ret = btrfs_end_transaction(trans, root);
2279 BUG_ON(ret);
2280 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002281 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002282 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002283}
2284
Chris Mason4313b392008-01-03 09:08:48 -05002285static int noinline create_subvol(struct btrfs_root *root, char *name,
2286 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002287{
2288 struct btrfs_trans_handle *trans;
2289 struct btrfs_key key;
2290 struct btrfs_root_item root_item;
2291 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002292 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002293 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002294 struct inode *inode;
2295 struct inode *dir;
2296 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002297 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002298 u64 objectid;
2299 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002300 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002301
2302 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002303 ret = btrfs_check_free_space(root, 1, 0);
2304 if (ret)
2305 goto fail_commit;
2306
Chris Mason39279cc2007-06-12 06:35:45 -04002307 trans = btrfs_start_transaction(root, 1);
2308 BUG_ON(!trans);
2309
Chris Mason7bb86312007-12-11 09:25:06 -05002310 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2311 0, &objectid);
2312 if (ret)
2313 goto fail;
2314
2315 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2316 objectid, trans->transid, 0, 0,
2317 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002318 if (IS_ERR(leaf))
2319 return PTR_ERR(leaf);
2320
2321 btrfs_set_header_nritems(leaf, 0);
2322 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002323 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002324 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002325 btrfs_set_header_owner(leaf, objectid);
2326
Chris Mason5f39d392007-10-15 16:14:19 -04002327 write_extent_buffer(leaf, root->fs_info->fsid,
2328 (unsigned long)btrfs_header_fsid(leaf),
2329 BTRFS_FSID_SIZE);
2330 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002331
2332 inode_item = &root_item.inode;
2333 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002334 inode_item->generation = cpu_to_le64(1);
2335 inode_item->size = cpu_to_le64(3);
2336 inode_item->nlink = cpu_to_le32(1);
2337 inode_item->nblocks = cpu_to_le64(1);
2338 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002339
Chris Masondb945352007-10-15 16:15:53 -04002340 btrfs_set_root_bytenr(&root_item, leaf->start);
2341 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002342 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002343 btrfs_set_root_used(&root_item, 0);
2344
Chris Mason5eda7b52007-06-22 14:16:25 -04002345 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2346 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002347
2348 free_extent_buffer(leaf);
2349 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002350
Chris Mason39279cc2007-06-12 06:35:45 -04002351 btrfs_set_root_dirid(&root_item, new_dirid);
2352
2353 key.objectid = objectid;
2354 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002355 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2356 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2357 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002358 if (ret)
2359 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002360
2361 /*
2362 * insert the directory item
2363 */
2364 key.offset = (u64)-1;
2365 dir = root->fs_info->sb->s_root->d_inode;
2366 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2367 name, namelen, dir->i_ino, &key,
2368 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002369 if (ret)
2370 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002371
Chris Mason39544012007-12-12 14:38:19 -05002372 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2373 name, namelen, objectid,
2374 root->fs_info->sb->s_root->d_inode->i_ino);
2375 if (ret)
2376 goto fail;
2377
Chris Mason39279cc2007-06-12 06:35:45 -04002378 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002379 if (ret)
2380 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002381
Josef Bacik58176a92007-08-29 15:47:34 -04002382 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002383 BUG_ON(!new_root);
2384
2385 trans = btrfs_start_transaction(new_root, 1);
2386 BUG_ON(!trans);
2387
Chris Mason9c583092008-01-29 15:15:18 -05002388 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2389 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002390 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002391 if (IS_ERR(inode))
2392 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002393 inode->i_op = &btrfs_dir_inode_operations;
2394 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002395 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002396
Chris Mason39544012007-12-12 14:38:19 -05002397 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2398 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002399 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002400 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002401 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002402 if (ret)
2403 goto fail;
2404fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002405 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002406 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002407 if (err && !ret)
2408 ret = err;
2409fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002410 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002411 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002412 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002413 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002414}
2415
2416static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2417{
Chris Mason3063d292008-01-08 15:46:30 -05002418 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002419 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002420 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002421 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002422 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002423
2424 if (!root->ref_cows)
2425 return -EINVAL;
2426
2427 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002428 ret = btrfs_check_free_space(root, 1, 0);
2429 if (ret)
2430 goto fail_unlock;
2431
Chris Mason3063d292008-01-08 15:46:30 -05002432 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2433 if (!pending_snapshot) {
2434 ret = -ENOMEM;
2435 goto fail_unlock;
2436 }
Yanfb4bc1e2008-01-17 11:59:51 -05002437 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002438 if (!pending_snapshot->name) {
2439 ret = -ENOMEM;
2440 kfree(pending_snapshot);
2441 goto fail_unlock;
2442 }
Yanfb4bc1e2008-01-17 11:59:51 -05002443 memcpy(pending_snapshot->name, name, namelen);
2444 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002445 trans = btrfs_start_transaction(root, 1);
2446 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002447 pending_snapshot->root = root;
2448 list_add(&pending_snapshot->list,
2449 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002450 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002451 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002452
Chris Mason1832a6d2007-12-21 16:27:21 -05002453fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002454 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002455 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002456 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002457 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002458}
2459
Chris Masonedbd8d42007-12-21 16:27:24 -05002460unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002461 struct file_ra_state *ra, struct file *file,
2462 pgoff_t offset, pgoff_t last_index)
2463{
2464 pgoff_t req_size;
2465
2466#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2467 req_size = last_index - offset + 1;
2468 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2469 return offset;
2470#else
2471 req_size = min(last_index - offset + 1, (pgoff_t)128);
2472 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2473 return offset + req_size;
2474#endif
2475}
2476
2477int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002478 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002480 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002481 struct page *page;
2482 unsigned long last_index;
2483 unsigned long ra_index = 0;
2484 u64 page_start;
2485 u64 page_end;
2486 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002487 int ret;
2488
2489 mutex_lock(&root->fs_info->fs_mutex);
2490 ret = btrfs_check_free_space(root, inode->i_size, 0);
2491 mutex_unlock(&root->fs_info->fs_mutex);
2492 if (ret)
2493 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002494
2495 mutex_lock(&inode->i_mutex);
2496 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2497 for (i = 0; i <= last_index; i++) {
2498 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002499 ra_index = btrfs_force_ra(inode->i_mapping,
2500 &file->f_ra,
2501 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002502 }
2503 page = grab_cache_page(inode->i_mapping, i);
2504 if (!page)
2505 goto out_unlock;
2506 if (!PageUptodate(page)) {
2507 btrfs_readpage(NULL, page);
2508 lock_page(page);
2509 if (!PageUptodate(page)) {
2510 unlock_page(page);
2511 page_cache_release(page);
2512 goto out_unlock;
2513 }
2514 }
Chris Mason35ebb932007-10-30 16:56:53 -04002515 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002516 page_end = page_start + PAGE_CACHE_SIZE - 1;
2517
Chris Masond1310b22008-01-24 16:13:08 -05002518 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002519 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002520 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002521
Chris Masond1310b22008-01-24 16:13:08 -05002522 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002523 set_page_dirty(page);
2524 unlock_page(page);
2525 page_cache_release(page);
2526 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2527 }
2528
2529out_unlock:
2530 mutex_unlock(&inode->i_mutex);
2531 return 0;
2532}
2533
Chris Masonedbd8d42007-12-21 16:27:24 -05002534static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2535{
2536 u64 new_size;
2537 u64 old_size;
2538 struct btrfs_ioctl_vol_args *vol_args;
2539 struct btrfs_trans_handle *trans;
2540 char *sizestr;
2541 int ret = 0;
2542 int namelen;
2543 int mod = 0;
2544
2545 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2546
2547 if (!vol_args)
2548 return -ENOMEM;
2549
2550 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2551 ret = -EFAULT;
2552 goto out;
2553 }
2554 namelen = strlen(vol_args->name);
2555 if (namelen > BTRFS_VOL_NAME_MAX) {
2556 ret = -EINVAL;
2557 goto out;
2558 }
2559
2560 sizestr = vol_args->name;
2561 if (!strcmp(sizestr, "max"))
2562 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2563 else {
2564 if (sizestr[0] == '-') {
2565 mod = -1;
2566 sizestr++;
2567 } else if (sizestr[0] == '+') {
2568 mod = 1;
2569 sizestr++;
2570 }
2571 new_size = btrfs_parse_size(sizestr);
2572 if (new_size == 0) {
2573 ret = -EINVAL;
2574 goto out;
2575 }
2576 }
2577
2578 mutex_lock(&root->fs_info->fs_mutex);
2579 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2580
2581 if (mod < 0) {
2582 if (new_size > old_size) {
2583 ret = -EINVAL;
2584 goto out_unlock;
2585 }
2586 new_size = old_size - new_size;
2587 } else if (mod > 0) {
2588 new_size = old_size + new_size;
2589 }
2590
2591 if (new_size < 256 * 1024 * 1024) {
2592 ret = -EINVAL;
2593 goto out_unlock;
2594 }
2595 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2596 ret = -EFBIG;
2597 goto out_unlock;
2598 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002599
2600 do_div(new_size, root->sectorsize);
2601 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002602
2603printk("new size is %Lu\n", new_size);
2604 if (new_size > old_size) {
2605 trans = btrfs_start_transaction(root, 1);
2606 ret = btrfs_grow_extent_tree(trans, root, new_size);
2607 btrfs_commit_transaction(trans, root);
2608 } else {
2609 ret = btrfs_shrink_extent_tree(root, new_size);
2610 }
2611
2612out_unlock:
2613 mutex_unlock(&root->fs_info->fs_mutex);
2614out:
2615 kfree(vol_args);
2616 return ret;
2617}
2618
Chris Mason4313b392008-01-03 09:08:48 -05002619static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2620 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002621{
Chris Mason4aec2b52007-12-18 16:25:45 -05002622 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002623 struct btrfs_dir_item *di;
2624 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002625 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002626 int namelen;
2627 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002628
Chris Mason4aec2b52007-12-18 16:25:45 -05002629 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002630
Chris Mason4aec2b52007-12-18 16:25:45 -05002631 if (!vol_args)
2632 return -ENOMEM;
2633
2634 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2635 ret = -EFAULT;
2636 goto out;
2637 }
2638
2639 namelen = strlen(vol_args->name);
2640 if (namelen > BTRFS_VOL_NAME_MAX) {
2641 ret = -EINVAL;
2642 goto out;
2643 }
2644 if (strchr(vol_args->name, '/')) {
2645 ret = -EINVAL;
2646 goto out;
2647 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002648
2649 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002650 if (!path) {
2651 ret = -ENOMEM;
2652 goto out;
2653 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002654
2655 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2656 mutex_lock(&root->fs_info->fs_mutex);
2657 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2658 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002659 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002660 mutex_unlock(&root->fs_info->fs_mutex);
2661 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002662
2663 if (di && !IS_ERR(di)) {
2664 ret = -EEXIST;
2665 goto out;
2666 }
2667
2668 if (IS_ERR(di)) {
2669 ret = PTR_ERR(di);
2670 goto out;
2671 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002672
2673 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002674 ret = create_subvol(root, vol_args->name, namelen);
2675 else
2676 ret = create_snapshot(root, vol_args->name, namelen);
2677out:
2678 kfree(vol_args);
2679 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002680}
2681
2682static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002683{
Chris Mason6da6aba2007-12-18 16:15:09 -05002684 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002685 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002686
2687 switch (inode->i_mode & S_IFMT) {
2688 case S_IFDIR:
2689 mutex_lock(&root->fs_info->fs_mutex);
2690 btrfs_defrag_root(root, 0);
2691 btrfs_defrag_root(root->fs_info->extent_root, 0);
2692 mutex_unlock(&root->fs_info->fs_mutex);
2693 break;
2694 case S_IFREG:
2695 btrfs_defrag_file(file);
2696 break;
2697 }
2698
2699 return 0;
2700}
2701
2702long btrfs_ioctl(struct file *file, unsigned int
2703 cmd, unsigned long arg)
2704{
Chris Mason6da6aba2007-12-18 16:15:09 -05002705 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002706
2707 switch (cmd) {
2708 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002709 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002710 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002711 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002712 case BTRFS_IOC_RESIZE:
2713 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002714 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002715
2716 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002717}
2718
Chris Mason39279cc2007-06-12 06:35:45 -04002719/*
2720 * Called inside transaction, so use GFP_NOFS
2721 */
2722struct inode *btrfs_alloc_inode(struct super_block *sb)
2723{
2724 struct btrfs_inode *ei;
2725
2726 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2727 if (!ei)
2728 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002729 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002730 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002731 return &ei->vfs_inode;
2732}
2733
2734void btrfs_destroy_inode(struct inode *inode)
2735{
2736 WARN_ON(!list_empty(&inode->i_dentry));
2737 WARN_ON(inode->i_data.nrpages);
2738
Chris Mason8c416c92008-01-14 15:10:26 -05002739 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002740 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2741}
2742
Chris Mason44ec0b72007-10-29 10:55:05 -04002743#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2744static void init_once(struct kmem_cache * cachep, void *foo)
2745#else
Chris Mason39279cc2007-06-12 06:35:45 -04002746static void init_once(void * foo, struct kmem_cache * cachep,
2747 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002748#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002749{
2750 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2751
2752 inode_init_once(&ei->vfs_inode);
2753}
2754
2755void btrfs_destroy_cachep(void)
2756{
2757 if (btrfs_inode_cachep)
2758 kmem_cache_destroy(btrfs_inode_cachep);
2759 if (btrfs_trans_handle_cachep)
2760 kmem_cache_destroy(btrfs_trans_handle_cachep);
2761 if (btrfs_transaction_cachep)
2762 kmem_cache_destroy(btrfs_transaction_cachep);
2763 if (btrfs_bit_radix_cachep)
2764 kmem_cache_destroy(btrfs_bit_radix_cachep);
2765 if (btrfs_path_cachep)
2766 kmem_cache_destroy(btrfs_path_cachep);
2767}
2768
Chris Mason86479a02007-09-10 19:58:16 -04002769struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002770 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002771#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2772 void (*ctor)(struct kmem_cache *, void *)
2773#else
Chris Mason92fee662007-07-25 12:31:35 -04002774 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002775 unsigned long)
2776#endif
2777 )
Chris Mason92fee662007-07-25 12:31:35 -04002778{
2779 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2780 SLAB_MEM_SPREAD | extra_flags), ctor
2781#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2782 ,NULL
2783#endif
2784 );
2785}
2786
Chris Mason39279cc2007-06-12 06:35:45 -04002787int btrfs_init_cachep(void)
2788{
Chris Mason86479a02007-09-10 19:58:16 -04002789 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002790 sizeof(struct btrfs_inode),
2791 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002792 if (!btrfs_inode_cachep)
2793 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002794 btrfs_trans_handle_cachep =
2795 btrfs_cache_create("btrfs_trans_handle_cache",
2796 sizeof(struct btrfs_trans_handle),
2797 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002798 if (!btrfs_trans_handle_cachep)
2799 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002800 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002801 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002802 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002803 if (!btrfs_transaction_cachep)
2804 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002805 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002806 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002807 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002808 if (!btrfs_path_cachep)
2809 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002810 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002811 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002812 if (!btrfs_bit_radix_cachep)
2813 goto fail;
2814 return 0;
2815fail:
2816 btrfs_destroy_cachep();
2817 return -ENOMEM;
2818}
2819
2820static int btrfs_getattr(struct vfsmount *mnt,
2821 struct dentry *dentry, struct kstat *stat)
2822{
2823 struct inode *inode = dentry->d_inode;
2824 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002825 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002826 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002827 return 0;
2828}
2829
2830static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2831 struct inode * new_dir,struct dentry *new_dentry)
2832{
2833 struct btrfs_trans_handle *trans;
2834 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2835 struct inode *new_inode = new_dentry->d_inode;
2836 struct inode *old_inode = old_dentry->d_inode;
2837 struct timespec ctime = CURRENT_TIME;
2838 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002839 int ret;
2840
2841 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2842 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2843 return -ENOTEMPTY;
2844 }
Chris Mason5f39d392007-10-15 16:14:19 -04002845
Chris Mason39279cc2007-06-12 06:35:45 -04002846 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002847 ret = btrfs_check_free_space(root, 1, 0);
2848 if (ret)
2849 goto out_unlock;
2850
Chris Mason39279cc2007-06-12 06:35:45 -04002851 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002852
Chris Mason39279cc2007-06-12 06:35:45 -04002853 btrfs_set_trans_block_group(trans, new_dir);
2854 path = btrfs_alloc_path();
2855 if (!path) {
2856 ret = -ENOMEM;
2857 goto out_fail;
2858 }
2859
2860 old_dentry->d_inode->i_nlink++;
2861 old_dir->i_ctime = old_dir->i_mtime = ctime;
2862 new_dir->i_ctime = new_dir->i_mtime = ctime;
2863 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002864
Chris Mason39279cc2007-06-12 06:35:45 -04002865 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2866 if (ret)
2867 goto out_fail;
2868
2869 if (new_inode) {
2870 new_inode->i_ctime = CURRENT_TIME;
2871 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2872 if (ret)
2873 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002874 }
Chris Mason9c583092008-01-29 15:15:18 -05002875 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002876 if (ret)
2877 goto out_fail;
2878
2879out_fail:
2880 btrfs_free_path(path);
2881 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002882out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002883 mutex_unlock(&root->fs_info->fs_mutex);
2884 return ret;
2885}
2886
2887static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2888 const char *symname)
2889{
2890 struct btrfs_trans_handle *trans;
2891 struct btrfs_root *root = BTRFS_I(dir)->root;
2892 struct btrfs_path *path;
2893 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002894 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002895 int err;
2896 int drop_inode = 0;
2897 u64 objectid;
2898 int name_len;
2899 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002900 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002901 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002902 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002903 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002904
2905 name_len = strlen(symname) + 1;
2906 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2907 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002908
Chris Mason39279cc2007-06-12 06:35:45 -04002909 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002910 err = btrfs_check_free_space(root, 1, 0);
2911 if (err)
2912 goto out_fail;
2913
Chris Mason39279cc2007-06-12 06:35:45 -04002914 trans = btrfs_start_transaction(root, 1);
2915 btrfs_set_trans_block_group(trans, dir);
2916
2917 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2918 if (err) {
2919 err = -ENOSPC;
2920 goto out_unlock;
2921 }
2922
Chris Mason9c583092008-01-29 15:15:18 -05002923 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2924 dentry->d_name.len,
2925 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002926 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2927 err = PTR_ERR(inode);
2928 if (IS_ERR(inode))
2929 goto out_unlock;
2930
2931 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002932 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002933 if (err)
2934 drop_inode = 1;
2935 else {
2936 inode->i_mapping->a_ops = &btrfs_aops;
2937 inode->i_fop = &btrfs_file_operations;
2938 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002939 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2940 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002941 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002942 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002943 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002944 }
2945 dir->i_sb->s_dirt = 1;
2946 btrfs_update_inode_block_group(trans, inode);
2947 btrfs_update_inode_block_group(trans, dir);
2948 if (drop_inode)
2949 goto out_unlock;
2950
2951 path = btrfs_alloc_path();
2952 BUG_ON(!path);
2953 key.objectid = inode->i_ino;
2954 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002955 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2956 datasize = btrfs_file_extent_calc_inline_size(name_len);
2957 err = btrfs_insert_empty_item(trans, root, path, &key,
2958 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002959 if (err) {
2960 drop_inode = 1;
2961 goto out_unlock;
2962 }
Chris Mason5f39d392007-10-15 16:14:19 -04002963 leaf = path->nodes[0];
2964 ei = btrfs_item_ptr(leaf, path->slots[0],
2965 struct btrfs_file_extent_item);
2966 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2967 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002968 BTRFS_FILE_EXTENT_INLINE);
2969 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002970 write_extent_buffer(leaf, symname, ptr, name_len);
2971 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002972 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002973
Chris Mason39279cc2007-06-12 06:35:45 -04002974 inode->i_op = &btrfs_symlink_inode_operations;
2975 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2976 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002977 err = btrfs_update_inode(trans, root, inode);
2978 if (err)
2979 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002980
2981out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002982 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002983 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002984out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002985 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002986 if (drop_inode) {
2987 inode_dec_link_count(inode);
2988 iput(inode);
2989 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002990 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002991 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002992 return err;
2993}
Yanfdebe2b2008-01-14 13:26:08 -05002994static int btrfs_permission(struct inode *inode, int mask,
2995 struct nameidata *nd)
2996{
2997 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2998 return -EACCES;
2999 return generic_permission(inode, mask, NULL);
3000}
Chris Mason39279cc2007-06-12 06:35:45 -04003001
3002static struct inode_operations btrfs_dir_inode_operations = {
3003 .lookup = btrfs_lookup,
3004 .create = btrfs_create,
3005 .unlink = btrfs_unlink,
3006 .link = btrfs_link,
3007 .mkdir = btrfs_mkdir,
3008 .rmdir = btrfs_rmdir,
3009 .rename = btrfs_rename,
3010 .symlink = btrfs_symlink,
3011 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003012 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003013 .setxattr = generic_setxattr,
3014 .getxattr = generic_getxattr,
3015 .listxattr = btrfs_listxattr,
3016 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003017 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003018};
Chris Mason39279cc2007-06-12 06:35:45 -04003019static struct inode_operations btrfs_dir_ro_inode_operations = {
3020 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003021 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003022};
Chris Mason39279cc2007-06-12 06:35:45 -04003023static struct file_operations btrfs_dir_file_operations = {
3024 .llseek = generic_file_llseek,
3025 .read = generic_read_dir,
3026 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003027 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003028#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003029 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003030#endif
3031};
3032
Chris Masond1310b22008-01-24 16:13:08 -05003033static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003034 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003035 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003036 .readpage_io_hook = btrfs_readpage_io_hook,
3037 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003038 .set_bit_hook = btrfs_set_bit_hook,
3039 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003040};
3041
Chris Mason39279cc2007-06-12 06:35:45 -04003042static struct address_space_operations btrfs_aops = {
3043 .readpage = btrfs_readpage,
3044 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003045 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003046 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003047 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003048 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003049 .invalidatepage = btrfs_invalidatepage,
3050 .releasepage = btrfs_releasepage,
3051 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003052};
3053
3054static struct address_space_operations btrfs_symlink_aops = {
3055 .readpage = btrfs_readpage,
3056 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003057 .invalidatepage = btrfs_invalidatepage,
3058 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003059};
3060
3061static struct inode_operations btrfs_file_inode_operations = {
3062 .truncate = btrfs_truncate,
3063 .getattr = btrfs_getattr,
3064 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003065 .setxattr = generic_setxattr,
3066 .getxattr = generic_getxattr,
3067 .listxattr = btrfs_listxattr,
3068 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003069 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003070};
Josef Bacik618e21d2007-07-11 10:18:17 -04003071static struct inode_operations btrfs_special_inode_operations = {
3072 .getattr = btrfs_getattr,
3073 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003074 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003075};
Chris Mason39279cc2007-06-12 06:35:45 -04003076static struct inode_operations btrfs_symlink_inode_operations = {
3077 .readlink = generic_readlink,
3078 .follow_link = page_follow_link_light,
3079 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003080 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003081};