blob: bbb71fad861a56a18d24c5c248b976c70d5df51d [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"
43
44struct btrfs_iget_args {
45 u64 ino;
46 struct btrfs_root *root;
47};
48
49static struct inode_operations btrfs_dir_inode_operations;
50static struct inode_operations btrfs_symlink_inode_operations;
51static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040052static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040053static struct inode_operations btrfs_file_inode_operations;
54static struct address_space_operations btrfs_aops;
55static struct address_space_operations btrfs_symlink_aops;
56static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050057static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040058
59static struct kmem_cache *btrfs_inode_cachep;
60struct kmem_cache *btrfs_trans_handle_cachep;
61struct kmem_cache *btrfs_transaction_cachep;
62struct kmem_cache *btrfs_bit_radix_cachep;
63struct kmem_cache *btrfs_path_cachep;
64
65#define S_SHIFT 12
66static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
67 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
68 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
69 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
70 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
71 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
72 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
73 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
74};
75
Chris Mason1832a6d2007-12-21 16:27:21 -050076int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
77 int for_del)
78{
79 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
80 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
81 u64 thresh;
82 int ret = 0;
83
84 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050085 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050086 else
Chris Masonf9ef6602008-01-03 09:22:38 -050087 thresh = total * 85;
88
89 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050090
91 spin_lock(&root->fs_info->delalloc_lock);
92 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
93 ret = -ENOSPC;
94 spin_unlock(&root->fs_info->delalloc_lock);
95 return ret;
96}
97
Chris Masonbe20aa92007-12-17 20:14:01 -050098static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db2b2007-08-27 16:49:44 -040099{
100 struct btrfs_root *root = BTRFS_I(inode)->root;
101 struct btrfs_trans_handle *trans;
Chris Masonb888db2b2007-08-27 16:49:44 -0400102 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400103 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500104 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400105 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500106 u64 orig_start = start;
107 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500108 struct btrfs_key ins;
109 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -0400110
Chris Masonb888db2b2007-08-27 16:49:44 -0400111 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db2b2007-08-27 16:49:44 -0400112 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500113 btrfs_set_trans_block_group(trans, inode);
114
Chris Masondb945352007-10-15 16:15:53 -0400115 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500116 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db2b2007-08-27 16:49:44 -0400117 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400118 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500119 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400120
Chris Mason179e29e2007-11-01 11:28:41 -0400121 if (alloc_hint == EXTENT_MAP_INLINE)
122 goto out;
123
Chris Masonc59f8952007-12-17 20:14:04 -0500124 while(num_bytes > 0) {
125 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
126 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
127 root->root_key.objectid,
128 trans->transid,
129 inode->i_ino, start, 0,
130 alloc_hint, (u64)-1, &ins, 1);
131 if (ret) {
132 WARN_ON(1);
133 goto out;
134 }
135 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
136 start, ins.objectid, ins.offset,
137 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500138 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500139 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400143 }
Chris Masond1310b22008-01-24 16:13:08 -0500144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500146 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500147 btrfs_update_inode(trans, root, inode);
Chris Masonb888db2b2007-08-27 16:49:44 -0400148out:
149 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500150 return ret;
151}
152
153static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
154{
155 u64 extent_start;
156 u64 extent_end;
157 u64 bytenr;
158 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500159 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500160 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500161 struct btrfs_root *root = BTRFS_I(inode)->root;
162 struct extent_buffer *leaf;
163 int found_type;
164 struct btrfs_path *path;
165 struct btrfs_file_extent_item *item;
166 int ret;
167 int err;
168 struct btrfs_key found_key;
169
Chris Masonc31f8832008-01-08 15:46:31 -0500170 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500171 path = btrfs_alloc_path();
172 BUG_ON(!path);
173again:
174 ret = btrfs_lookup_file_extent(NULL, root, path,
175 inode->i_ino, start, 0);
176 if (ret < 0) {
177 btrfs_free_path(path);
178 return ret;
179 }
180
181 cow_end = end;
182 if (ret != 0) {
183 if (path->slots[0] == 0)
184 goto not_found;
185 path->slots[0]--;
186 }
187
188 leaf = path->nodes[0];
189 item = btrfs_item_ptr(leaf, path->slots[0],
190 struct btrfs_file_extent_item);
191
192 /* are we inside the extent that was found? */
193 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
194 found_type = btrfs_key_type(&found_key);
195 if (found_key.objectid != inode->i_ino ||
196 found_type != BTRFS_EXTENT_DATA_KEY) {
197 goto not_found;
198 }
199
200 found_type = btrfs_file_extent_type(leaf, item);
201 extent_start = found_key.offset;
202 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500203 u64 extent_num_bytes;
204
205 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
206 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500207 err = 0;
208
Chris Mason1832a6d2007-12-21 16:27:21 -0500209 if (loops && start != extent_start)
210 goto not_found;
211
Chris Masonbe20aa92007-12-17 20:14:01 -0500212 if (start < extent_start || start >= extent_end)
213 goto not_found;
214
215 cow_end = min(end, extent_end - 1);
216 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
217 if (bytenr == 0)
218 goto not_found;
219
Chris Masonc31f8832008-01-08 15:46:31 -0500220 /*
221 * we may be called by the resizer, make sure we're inside
222 * the limits of the FS
223 */
224 if (bytenr + extent_num_bytes > total_fs_bytes)
225 goto not_found;
226
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
228 goto not_found;
229 }
230
231 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500232 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500233 goto not_found;
234 }
235loop:
236 if (start > end) {
237 btrfs_free_path(path);
238 return 0;
239 }
240 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500242 goto again;
243
244not_found:
245 cow_file_range(inode, start, cow_end);
246 start = cow_end + 1;
247 goto loop;
248}
249
250static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
251{
252 struct btrfs_root *root = BTRFS_I(inode)->root;
253 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500257 ret = run_delalloc_nocow(inode, start, end);
258 else
259 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500260
Chris Masonb888db2b2007-08-27 16:49:44 -0400261 mutex_unlock(&root->fs_info->fs_mutex);
262 return ret;
263}
264
Chris Mason291d6732008-01-29 15:55:23 -0500265int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500266 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500267{
Chris Masonb0c68f82008-01-31 11:05:37 -0500268 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500269 struct btrfs_root *root = BTRFS_I(inode)->root;
270 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500271 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500272 root->fs_info->delalloc_bytes += end - start + 1;
273 spin_unlock(&root->fs_info->delalloc_lock);
274 }
275 return 0;
276}
277
278int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500279 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500280{
Chris Masonb0c68f82008-01-31 11:05:37 -0500281 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500282 struct btrfs_root *root = BTRFS_I(inode)->root;
283 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500284 if (end - start + 1 > root->fs_info->delalloc_bytes) {
285 printk("warning: delalloc account %Lu %Lu\n",
286 end - start + 1, root->fs_info->delalloc_bytes);
287 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500288 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500289 } else {
290 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500291 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 }
Chris Mason291d6732008-01-29 15:55:23 -0500293 spin_unlock(&root->fs_info->delalloc_lock);
294 }
295 return 0;
296}
297
Chris Mason065631f2008-02-20 12:07:25 -0500298int btrfs_submit_bio_hook(int rw, struct bio *bio)
299{
300 // struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
301 struct bio_vec *bvec = bio->bi_io_vec;
302 struct inode *inode = bvec->bv_page->mapping->host;
303 struct btrfs_root *root = BTRFS_I(inode)->root;
304 struct btrfs_trans_handle *trans;
305 int ret = 0;
306
307 if (rw != WRITE)
308 return 0;
309
310 if (btrfs_test_opt(root, NODATASUM) ||
311 btrfs_test_flag(inode, NODATASUM))
312 return 0;
313
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);
321 return ret;
322}
323#if 0
Chris Mason07157aa2007-08-30 08:50:51 -0400324int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
325{
326 struct inode *inode = page->mapping->host;
327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct btrfs_trans_handle *trans;
329 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500330 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400331 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400332 size_t offset = start - page_start;
Yanb98b6762008-01-08 15:54:37 -0500333 if (btrfs_test_opt(root, NODATASUM) ||
334 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500335 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400336 mutex_lock(&root->fs_info->fs_mutex);
337 trans = btrfs_start_transaction(root, 1);
338 btrfs_set_trans_block_group(trans, inode);
339 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400340 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400341 start, kaddr + offset, end - start + 1);
342 kunmap(page);
343 ret = btrfs_end_transaction(trans, root);
344 BUG_ON(ret);
345 mutex_unlock(&root->fs_info->fs_mutex);
346 return ret;
347}
Chris Mason065631f2008-02-20 12:07:25 -0500348#endif
Chris Mason07157aa2007-08-30 08:50:51 -0400349int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
350{
351 int ret = 0;
352 struct inode *inode = page->mapping->host;
353 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500354 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400355 struct btrfs_csum_item *item;
356 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400357 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500358 if (btrfs_test_opt(root, NODATASUM) ||
359 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500360 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400361 mutex_lock(&root->fs_info->fs_mutex);
362 path = btrfs_alloc_path();
363 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
364 if (IS_ERR(item)) {
365 ret = PTR_ERR(item);
366 /* a csum that isn't present is a preallocated region. */
367 if (ret == -ENOENT || ret == -EFBIG)
368 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400369 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500370 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400371 goto out;
372 }
Chris Masonff79f812007-10-15 16:22:25 -0400373 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
374 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500375 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400376out:
377 if (path)
378 btrfs_free_path(path);
379 mutex_unlock(&root->fs_info->fs_mutex);
380 return ret;
381}
382
Chris Mason70dec802008-01-29 09:59:12 -0500383int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
384 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400385{
Chris Mason35ebb932007-10-30 16:56:53 -0400386 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400387 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500388 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400389 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500390 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400391 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400392 struct btrfs_root *root = BTRFS_I(inode)->root;
393 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400394 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500395
Yanb98b6762008-01-08 15:54:37 -0500396 if (btrfs_test_opt(root, NODATASUM) ||
397 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500398 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500399 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500400 private = state->private;
401 ret = 0;
402 } else {
403 ret = get_state_private(io_tree, start, &private);
404 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400405 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400406 kaddr = kmap_atomic(page, KM_IRQ0);
407 if (ret) {
408 goto zeroit;
409 }
Chris Masonff79f812007-10-15 16:22:25 -0400410 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
411 btrfs_csum_final(csum, (char *)&csum);
412 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400413 goto zeroit;
414 }
415 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400416 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400417 return 0;
418
419zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500420 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
421 page->mapping->host->i_ino, (unsigned long long)start, csum,
422 private);
Chris Masondb945352007-10-15 16:15:53 -0400423 memset(kaddr + offset, 1, end - start + 1);
424 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400425 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400426 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400427 return 0;
428}
Chris Masonb888db2b2007-08-27 16:49:44 -0400429
Chris Mason39279cc2007-06-12 06:35:45 -0400430void btrfs_read_locked_inode(struct inode *inode)
431{
432 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400433 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400434 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400435 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400436 struct btrfs_root *root = BTRFS_I(inode)->root;
437 struct btrfs_key location;
438 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400439 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400440 int ret;
441
442 path = btrfs_alloc_path();
443 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400444 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400445 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500446
Chris Mason39279cc2007-06-12 06:35:45 -0400447 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400448 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400449 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400450
Chris Mason5f39d392007-10-15 16:14:19 -0400451 leaf = path->nodes[0];
452 inode_item = btrfs_item_ptr(leaf, path->slots[0],
453 struct btrfs_inode_item);
454
455 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
456 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
457 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
458 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
459 inode->i_size = btrfs_inode_size(leaf, inode_item);
460
461 tspec = btrfs_inode_atime(inode_item);
462 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
463 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
464
465 tspec = btrfs_inode_mtime(inode_item);
466 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
467 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
468
469 tspec = btrfs_inode_ctime(inode_item);
470 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
471 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
472
473 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
474 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400475 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400476 rdev = btrfs_inode_rdev(leaf, inode_item);
477
478 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400479 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
480 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500481 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500482 if (!BTRFS_I(inode)->block_group) {
483 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
484 NULL, 0, 0, 0);
485 }
Chris Mason39279cc2007-06-12 06:35:45 -0400486 btrfs_free_path(path);
487 inode_item = NULL;
488
489 mutex_unlock(&root->fs_info->fs_mutex);
490
491 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400492 case S_IFREG:
493 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500494 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400495 inode->i_fop = &btrfs_file_operations;
496 inode->i_op = &btrfs_file_inode_operations;
497 break;
498 case S_IFDIR:
499 inode->i_fop = &btrfs_dir_file_operations;
500 if (root == root->fs_info->tree_root)
501 inode->i_op = &btrfs_dir_ro_inode_operations;
502 else
503 inode->i_op = &btrfs_dir_inode_operations;
504 break;
505 case S_IFLNK:
506 inode->i_op = &btrfs_symlink_inode_operations;
507 inode->i_mapping->a_ops = &btrfs_symlink_aops;
508 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400509 default:
510 init_special_inode(inode, inode->i_mode, rdev);
511 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400512 }
513 return;
514
515make_bad:
516 btrfs_release_path(root, path);
517 btrfs_free_path(path);
518 mutex_unlock(&root->fs_info->fs_mutex);
519 make_bad_inode(inode);
520}
521
Chris Mason5f39d392007-10-15 16:14:19 -0400522static void fill_inode_item(struct extent_buffer *leaf,
523 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400524 struct inode *inode)
525{
Chris Mason5f39d392007-10-15 16:14:19 -0400526 btrfs_set_inode_uid(leaf, item, inode->i_uid);
527 btrfs_set_inode_gid(leaf, item, inode->i_gid);
528 btrfs_set_inode_size(leaf, item, inode->i_size);
529 btrfs_set_inode_mode(leaf, item, inode->i_mode);
530 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
531
532 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
533 inode->i_atime.tv_sec);
534 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
535 inode->i_atime.tv_nsec);
536
537 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
538 inode->i_mtime.tv_sec);
539 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
540 inode->i_mtime.tv_nsec);
541
542 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
543 inode->i_ctime.tv_sec);
544 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
545 inode->i_ctime.tv_nsec);
546
547 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
548 btrfs_set_inode_generation(leaf, item, inode->i_generation);
549 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500550 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400551 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400552 BTRFS_I(inode)->block_group->key.objectid);
553}
554
Chris Masona52d9a82007-08-27 16:49:44 -0400555int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400556 struct btrfs_root *root,
557 struct inode *inode)
558{
559 struct btrfs_inode_item *inode_item;
560 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400561 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400562 int ret;
563
564 path = btrfs_alloc_path();
565 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400566 ret = btrfs_lookup_inode(trans, root, path,
567 &BTRFS_I(inode)->location, 1);
568 if (ret) {
569 if (ret > 0)
570 ret = -ENOENT;
571 goto failed;
572 }
573
Chris Mason5f39d392007-10-15 16:14:19 -0400574 leaf = path->nodes[0];
575 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400576 struct btrfs_inode_item);
577
Chris Mason5f39d392007-10-15 16:14:19 -0400578 fill_inode_item(leaf, inode_item, inode);
579 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400580 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400581 ret = 0;
582failed:
583 btrfs_release_path(root, path);
584 btrfs_free_path(path);
585 return ret;
586}
587
588
589static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
591 struct inode *dir,
592 struct dentry *dentry)
593{
594 struct btrfs_path *path;
595 const char *name = dentry->d_name.name;
596 int name_len = dentry->d_name.len;
597 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400598 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400599 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400600 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400601
602 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400603 if (!path) {
604 ret = -ENOMEM;
605 goto err;
606 }
607
Chris Mason39279cc2007-06-12 06:35:45 -0400608 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
609 name, name_len, -1);
610 if (IS_ERR(di)) {
611 ret = PTR_ERR(di);
612 goto err;
613 }
614 if (!di) {
615 ret = -ENOENT;
616 goto err;
617 }
Chris Mason5f39d392007-10-15 16:14:19 -0400618 leaf = path->nodes[0];
619 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400620 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400621 if (ret)
622 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400623 btrfs_release_path(root, path);
624
625 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400626 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400627 if (IS_ERR(di)) {
628 ret = PTR_ERR(di);
629 goto err;
630 }
631 if (!di) {
632 ret = -ENOENT;
633 goto err;
634 }
635 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400636
637 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500638 ret = btrfs_del_inode_ref(trans, root, name, name_len,
639 dentry->d_inode->i_ino,
640 dentry->d_parent->d_inode->i_ino);
641 if (ret) {
642 printk("failed to delete reference to %.*s, "
643 "inode %lu parent %lu\n", name_len, name,
644 dentry->d_inode->i_ino,
645 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500646 }
Chris Mason39279cc2007-06-12 06:35:45 -0400647err:
648 btrfs_free_path(path);
649 if (!ret) {
650 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400651 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400652 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500653#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
654 dentry->d_inode->i_nlink--;
655#else
Chris Mason39279cc2007-06-12 06:35:45 -0400656 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500657#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400658 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400659 dir->i_sb->s_dirt = 1;
660 }
661 return ret;
662}
663
664static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
665{
666 struct btrfs_root *root;
667 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500668 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400669 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500670 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400671
672 root = BTRFS_I(dir)->root;
673 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500674
675 ret = btrfs_check_free_space(root, 1, 1);
676 if (ret)
677 goto fail;
678
Chris Mason39279cc2007-06-12 06:35:45 -0400679 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400680
Chris Mason39279cc2007-06-12 06:35:45 -0400681 btrfs_set_trans_block_group(trans, dir);
682 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400683 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400684
Chris Mason2da98f02008-01-16 11:44:43 -0500685 if (inode->i_nlink == 0) {
686 int found;
687 /* if the inode isn't linked anywhere,
688 * we don't need to worry about
689 * data=ordered
690 */
691 found = btrfs_del_ordered_inode(inode);
692 if (found == 1) {
693 atomic_dec(&inode->i_count);
694 }
695 }
696
Chris Mason39279cc2007-06-12 06:35:45 -0400697 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500698fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400699 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400700 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500701 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400702 return ret;
703}
704
705static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
706{
707 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500708 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400709 int ret;
710 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400711 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500712 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400713
Yan134d4512007-10-25 15:49:25 -0400714 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
715 return -ENOTEMPTY;
716
Chris Mason39279cc2007-06-12 06:35:45 -0400717 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500718 ret = btrfs_check_free_space(root, 1, 1);
719 if (ret)
720 goto fail;
721
Chris Mason39279cc2007-06-12 06:35:45 -0400722 trans = btrfs_start_transaction(root, 1);
723 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400724
725 /* now the directory is empty */
726 err = btrfs_unlink_trans(trans, root, dir, dentry);
727 if (!err) {
728 inode->i_size = 0;
729 }
Chris Mason39544012007-12-12 14:38:19 -0500730
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400731 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400732 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500733fail:
Yan134d4512007-10-25 15:49:25 -0400734 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400735 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500736 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500737
Chris Mason39279cc2007-06-12 06:35:45 -0400738 if (ret && !err)
739 err = ret;
740 return err;
741}
742
Chris Mason39279cc2007-06-12 06:35:45 -0400743/*
Chris Mason39279cc2007-06-12 06:35:45 -0400744 * this can truncate away extent items, csum items and directory items.
745 * It starts at a high offset and removes keys until it can't find
746 * any higher than i_size.
747 *
748 * csum items that cross the new i_size are truncated to the new size
749 * as well.
750 */
751static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500753 struct inode *inode,
754 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400755{
756 int ret;
757 struct btrfs_path *path;
758 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400759 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400760 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400761 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400762 struct btrfs_file_extent_item *fi;
763 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400764 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400765 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500766 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500767 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400768 int found_extent;
769 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500770 int pending_del_nr = 0;
771 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400772 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400773
Chris Masona52d9a82007-08-27 16:49:44 -0400774 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400775 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400776 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400778
Chris Mason39279cc2007-06-12 06:35:45 -0400779 /* FIXME, add redo link to tree so we don't leak on crash */
780 key.objectid = inode->i_ino;
781 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400782 key.type = (u8)-1;
783
Chris Mason85e21ba2008-01-29 15:11:36 -0500784 btrfs_init_path(path);
785search_again:
786 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
787 if (ret < 0) {
788 goto error;
789 }
790 if (ret > 0) {
791 BUG_ON(path->slots[0] == 0);
792 path->slots[0]--;
793 }
794
Chris Mason39279cc2007-06-12 06:35:45 -0400795 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400796 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400797 leaf = path->nodes[0];
798 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
799 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400800
Chris Mason5f39d392007-10-15 16:14:19 -0400801 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400802 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400803
Chris Mason85e21ba2008-01-29 15:11:36 -0500804 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400805 break;
806
Chris Mason5f39d392007-10-15 16:14:19 -0400807 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400808 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400809 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400810 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400811 extent_type = btrfs_file_extent_type(leaf, fi);
812 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400813 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400814 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400815 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
816 struct btrfs_item *item = btrfs_item_nr(leaf,
817 path->slots[0]);
818 item_end += btrfs_file_extent_inline_len(leaf,
819 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400820 }
Yan008630c2007-11-07 13:31:09 -0500821 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400822 }
823 if (found_type == BTRFS_CSUM_ITEM_KEY) {
824 ret = btrfs_csum_truncate(trans, root, path,
825 inode->i_size);
826 BUG_ON(ret);
827 }
Yan008630c2007-11-07 13:31:09 -0500828 if (item_end < inode->i_size) {
Chris Masonb888db2b2007-08-27 16:49:44 -0400829 if (found_type == BTRFS_DIR_ITEM_KEY) {
830 found_type = BTRFS_INODE_ITEM_KEY;
831 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
832 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500833 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
834 found_type = BTRFS_XATTR_ITEM_KEY;
835 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
836 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db2b2007-08-27 16:49:44 -0400837 } else if (found_type) {
838 found_type--;
839 } else {
840 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400841 }
Yana61721d2007-09-17 11:08:38 -0400842 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500843 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400844 }
Chris Mason5f39d392007-10-15 16:14:19 -0400845 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400846 del_item = 1;
847 else
848 del_item = 0;
849 found_extent = 0;
850
851 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400852 if (found_type != BTRFS_EXTENT_DATA_KEY)
853 goto delete;
854
855 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400856 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400857 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400858 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400859 u64 orig_num_bytes =
860 btrfs_file_extent_num_bytes(leaf, fi);
861 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400862 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -0500863 extent_num_bytes = extent_num_bytes &
864 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400865 btrfs_set_file_extent_num_bytes(leaf, fi,
866 extent_num_bytes);
867 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -0500868 extent_num_bytes);
869 if (extent_start != 0)
870 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -0400871 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400872 } else {
Chris Masondb945352007-10-15 16:15:53 -0400873 extent_num_bytes =
874 btrfs_file_extent_disk_num_bytes(leaf,
875 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400876 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -0500877 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400878 if (extent_start != 0) {
879 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -0500880 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -0400881 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500882 root_gen = btrfs_header_generation(leaf);
883 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400884 }
Chris Mason90692182008-02-08 13:49:28 -0500885 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
886 if (!del_item) {
887 u32 newsize = inode->i_size - found_key.offset;
888 dec_i_blocks(inode, item_end + 1 -
889 found_key.offset - newsize);
890 newsize =
891 btrfs_file_extent_calc_inline_size(newsize);
892 ret = btrfs_truncate_item(trans, root, path,
893 newsize, 1);
894 BUG_ON(ret);
895 } else {
896 dec_i_blocks(inode, item_end + 1 -
897 found_key.offset);
898 }
Chris Mason39279cc2007-06-12 06:35:45 -0400899 }
Chris Mason179e29e2007-11-01 11:28:41 -0400900delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400901 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500902 if (!pending_del_nr) {
903 /* no pending yet, add ourselves */
904 pending_del_slot = path->slots[0];
905 pending_del_nr = 1;
906 } else if (pending_del_nr &&
907 path->slots[0] + 1 == pending_del_slot) {
908 /* hop on the pending chunk */
909 pending_del_nr++;
910 pending_del_slot = path->slots[0];
911 } else {
912 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
913 }
Chris Mason39279cc2007-06-12 06:35:45 -0400914 } else {
915 break;
916 }
Chris Mason39279cc2007-06-12 06:35:45 -0400917 if (found_extent) {
918 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500919 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500920 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500921 root_gen, inode->i_ino,
922 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400923 BUG_ON(ret);
924 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500925next:
926 if (path->slots[0] == 0) {
927 if (pending_del_nr)
928 goto del_pending;
929 btrfs_release_path(root, path);
930 goto search_again;
931 }
932
933 path->slots[0]--;
934 if (pending_del_nr &&
935 path->slots[0] + 1 != pending_del_slot) {
936 struct btrfs_key debug;
937del_pending:
938 btrfs_item_key_to_cpu(path->nodes[0], &debug,
939 pending_del_slot);
940 ret = btrfs_del_items(trans, root, path,
941 pending_del_slot,
942 pending_del_nr);
943 BUG_ON(ret);
944 pending_del_nr = 0;
945 btrfs_release_path(root, path);
946 goto search_again;
947 }
Chris Mason39279cc2007-06-12 06:35:45 -0400948 }
949 ret = 0;
950error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500951 if (pending_del_nr) {
952 ret = btrfs_del_items(trans, root, path, pending_del_slot,
953 pending_del_nr);
954 }
Chris Mason39279cc2007-06-12 06:35:45 -0400955 btrfs_release_path(root, path);
956 btrfs_free_path(path);
957 inode->i_sb->s_dirt = 1;
958 return ret;
959}
960
Chris Masonb888db2b2007-08-27 16:49:44 -0400961static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400962 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400963{
Chris Mason39279cc2007-06-12 06:35:45 -0400964 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400966 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db2b2007-08-27 16:49:44 -0400967 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500968 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400969
Chris Mason190662b2007-12-18 16:25:45 -0500970 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400971 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400972
Chris Masond1310b22008-01-24 16:13:08 -0500973 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500974 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db2b2007-08-27 16:49:44 -0400975 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500976
Chris Masona52d9a82007-08-27 16:49:44 -0400977 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db2b2007-08-27 16:49:44 -0400978 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400979 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
980 flush_dcache_page(page);
Chris Masonb888db2b2007-08-27 16:49:44 -0400981 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400982 }
Chris Masonb888db2b2007-08-27 16:49:44 -0400983 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500984 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400985
Chris Masona52d9a82007-08-27 16:49:44 -0400986 return ret;
987}
988
989/*
990 * taken from block_truncate_page, but does cow as it zeros out
991 * any bytes left in the last page in the file.
992 */
993static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
994{
995 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400996 struct btrfs_root *root = BTRFS_I(inode)->root;
997 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400998 pgoff_t index = from >> PAGE_CACHE_SHIFT;
999 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1000 struct page *page;
1001 int ret = 0;
1002 u64 page_start;
1003
1004 if ((offset & (blocksize - 1)) == 0)
1005 goto out;
1006
1007 ret = -ENOMEM;
1008 page = grab_cache_page(mapping, index);
1009 if (!page)
1010 goto out;
1011 if (!PageUptodate(page)) {
1012 ret = btrfs_readpage(NULL, page);
1013 lock_page(page);
1014 if (!PageUptodate(page)) {
1015 ret = -EIO;
1016 goto out;
1017 }
1018 }
Chris Mason35ebb932007-10-30 16:56:53 -04001019 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001020
Chris Masonb888db2b2007-08-27 16:49:44 -04001021 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001022
Chris Mason39279cc2007-06-12 06:35:45 -04001023 unlock_page(page);
1024 page_cache_release(page);
1025out:
1026 return ret;
1027}
1028
1029static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1030{
1031 struct inode *inode = dentry->d_inode;
1032 int err;
1033
1034 err = inode_change_ok(inode, attr);
1035 if (err)
1036 return err;
1037
1038 if (S_ISREG(inode->i_mode) &&
1039 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1040 struct btrfs_trans_handle *trans;
1041 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001042 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001043
Chris Mason5f39d392007-10-15 16:14:19 -04001044 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001045 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001046 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001047 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001048 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001049
Chris Mason1b0f7c22008-01-30 14:33:02 -05001050 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001051 goto out;
1052
Chris Mason1832a6d2007-12-21 16:27:21 -05001053 mutex_lock(&root->fs_info->fs_mutex);
1054 err = btrfs_check_free_space(root, 1, 0);
1055 mutex_unlock(&root->fs_info->fs_mutex);
1056 if (err)
1057 goto fail;
1058
Chris Mason39279cc2007-06-12 06:35:45 -04001059 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1060
Chris Mason1b0f7c22008-01-30 14:33:02 -05001061 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001062 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001063
1064 mutex_lock(&root->fs_info->fs_mutex);
1065 trans = btrfs_start_transaction(root, 1);
1066 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001067 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001068 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001069 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001070
Chris Mason179e29e2007-11-01 11:28:41 -04001071 if (alloc_hint != EXTENT_MAP_INLINE) {
1072 err = btrfs_insert_file_extent(trans, root,
1073 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001074 hole_start, 0, 0,
1075 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001076 btrfs_drop_extent_cache(inode, hole_start,
1077 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001078 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001079 }
Chris Mason39279cc2007-06-12 06:35:45 -04001080 btrfs_end_transaction(trans, root);
1081 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001082 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001083 if (err)
1084 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001085 }
1086out:
1087 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001088fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001089 return err;
1090}
Chris Mason61295eb2008-01-14 16:24:38 -05001091
Chris Mason2da98f02008-01-16 11:44:43 -05001092void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001093{
Chris Mason2da98f02008-01-16 11:44:43 -05001094 int ret;
1095
1096 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001097 return;
1098 }
Chris Mason2da98f02008-01-16 11:44:43 -05001099
1100 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1101 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1102 return;
1103
1104 ret = btrfs_del_ordered_inode(inode);
1105 if (ret == 1) {
1106 atomic_dec(&inode->i_count);
1107 }
Chris Mason61295eb2008-01-14 16:24:38 -05001108}
1109
Chris Mason39279cc2007-06-12 06:35:45 -04001110void btrfs_delete_inode(struct inode *inode)
1111{
1112 struct btrfs_trans_handle *trans;
1113 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001114 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001115 int ret;
1116
1117 truncate_inode_pages(&inode->i_data, 0);
1118 if (is_bad_inode(inode)) {
1119 goto no_delete;
1120 }
Chris Mason5f39d392007-10-15 16:14:19 -04001121
Chris Mason39279cc2007-06-12 06:35:45 -04001122 inode->i_size = 0;
1123 mutex_lock(&root->fs_info->fs_mutex);
1124 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001125
Chris Mason39279cc2007-06-12 06:35:45 -04001126 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001127 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001128 if (ret)
1129 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001130
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001131 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001132 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001133
Chris Mason39279cc2007-06-12 06:35:45 -04001134 btrfs_end_transaction(trans, root);
1135 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001136 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001137 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001138 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001139
1140no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001141 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001142 btrfs_end_transaction(trans, root);
1143 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001144 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001145 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001146no_delete:
1147 clear_inode(inode);
1148}
1149
1150/*
1151 * this returns the key found in the dir entry in the location pointer.
1152 * If no dir entries were found, location->objectid is 0.
1153 */
1154static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1155 struct btrfs_key *location)
1156{
1157 const char *name = dentry->d_name.name;
1158 int namelen = dentry->d_name.len;
1159 struct btrfs_dir_item *di;
1160 struct btrfs_path *path;
1161 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001162 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001163
Chris Mason39544012007-12-12 14:38:19 -05001164 if (namelen == 1 && strcmp(name, ".") == 0) {
1165 location->objectid = dir->i_ino;
1166 location->type = BTRFS_INODE_ITEM_KEY;
1167 location->offset = 0;
1168 return 0;
1169 }
Chris Mason39279cc2007-06-12 06:35:45 -04001170 path = btrfs_alloc_path();
1171 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001172
Chris Mason7a720532007-12-13 09:06:59 -05001173 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001174 struct btrfs_key key;
1175 struct extent_buffer *leaf;
1176 u32 nritems;
1177 int slot;
1178
1179 key.objectid = dir->i_ino;
1180 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1181 key.offset = 0;
1182 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1183 BUG_ON(ret == 0);
1184 ret = 0;
1185
1186 leaf = path->nodes[0];
1187 slot = path->slots[0];
1188 nritems = btrfs_header_nritems(leaf);
1189 if (slot >= nritems)
1190 goto out_err;
1191
1192 btrfs_item_key_to_cpu(leaf, &key, slot);
1193 if (key.objectid != dir->i_ino ||
1194 key.type != BTRFS_INODE_REF_KEY) {
1195 goto out_err;
1196 }
1197 location->objectid = key.offset;
1198 location->type = BTRFS_INODE_ITEM_KEY;
1199 location->offset = 0;
1200 goto out;
1201 }
1202
Chris Mason39279cc2007-06-12 06:35:45 -04001203 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1204 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001205 if (IS_ERR(di))
1206 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001207 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001208 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001209 }
Chris Mason5f39d392007-10-15 16:14:19 -04001210 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001211out:
Chris Mason39279cc2007-06-12 06:35:45 -04001212 btrfs_free_path(path);
1213 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001214out_err:
1215 location->objectid = 0;
1216 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001217}
1218
1219/*
1220 * when we hit a tree root in a directory, the btrfs part of the inode
1221 * needs to be changed to reflect the root directory of the tree root. This
1222 * is kind of like crossing a mount point.
1223 */
1224static int fixup_tree_root_location(struct btrfs_root *root,
1225 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001226 struct btrfs_root **sub_root,
1227 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001228{
1229 struct btrfs_path *path;
1230 struct btrfs_root_item *ri;
1231
1232 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1233 return 0;
1234 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1235 return 0;
1236
1237 path = btrfs_alloc_path();
1238 BUG_ON(!path);
1239 mutex_lock(&root->fs_info->fs_mutex);
1240
Josef Bacik58176a92007-08-29 15:47:34 -04001241 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1242 dentry->d_name.name,
1243 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001244 if (IS_ERR(*sub_root))
1245 return PTR_ERR(*sub_root);
1246
1247 ri = &(*sub_root)->root_item;
1248 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001249 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1250 location->offset = 0;
1251
1252 btrfs_free_path(path);
1253 mutex_unlock(&root->fs_info->fs_mutex);
1254 return 0;
1255}
1256
1257static int btrfs_init_locked_inode(struct inode *inode, void *p)
1258{
1259 struct btrfs_iget_args *args = p;
1260 inode->i_ino = args->ino;
1261 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001262 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001263 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1264 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04001265 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001266 return 0;
1267}
1268
1269static int btrfs_find_actor(struct inode *inode, void *opaque)
1270{
1271 struct btrfs_iget_args *args = opaque;
1272 return (args->ino == inode->i_ino &&
1273 args->root == BTRFS_I(inode)->root);
1274}
1275
Chris Masondc17ff82008-01-08 15:46:30 -05001276struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1277 u64 root_objectid)
1278{
1279 struct btrfs_iget_args args;
1280 args.ino = objectid;
1281 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1282
1283 if (!args.root)
1284 return NULL;
1285
1286 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1287}
1288
Chris Mason39279cc2007-06-12 06:35:45 -04001289struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1290 struct btrfs_root *root)
1291{
1292 struct inode *inode;
1293 struct btrfs_iget_args args;
1294 args.ino = objectid;
1295 args.root = root;
1296
1297 inode = iget5_locked(s, objectid, btrfs_find_actor,
1298 btrfs_init_locked_inode,
1299 (void *)&args);
1300 return inode;
1301}
1302
1303static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1304 struct nameidata *nd)
1305{
1306 struct inode * inode;
1307 struct btrfs_inode *bi = BTRFS_I(dir);
1308 struct btrfs_root *root = bi->root;
1309 struct btrfs_root *sub_root = root;
1310 struct btrfs_key location;
1311 int ret;
1312
1313 if (dentry->d_name.len > BTRFS_NAME_LEN)
1314 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001315
Chris Mason39279cc2007-06-12 06:35:45 -04001316 mutex_lock(&root->fs_info->fs_mutex);
1317 ret = btrfs_inode_by_name(dir, dentry, &location);
1318 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001319
Chris Mason39279cc2007-06-12 06:35:45 -04001320 if (ret < 0)
1321 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001322
Chris Mason39279cc2007-06-12 06:35:45 -04001323 inode = NULL;
1324 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001325 ret = fixup_tree_root_location(root, &location, &sub_root,
1326 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001327 if (ret < 0)
1328 return ERR_PTR(ret);
1329 if (ret > 0)
1330 return ERR_PTR(-ENOENT);
1331 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1332 sub_root);
1333 if (!inode)
1334 return ERR_PTR(-EACCES);
1335 if (inode->i_state & I_NEW) {
1336 /* the inode and parent dir are two different roots */
1337 if (sub_root != root) {
1338 igrab(inode);
1339 sub_root->inode = inode;
1340 }
1341 BTRFS_I(inode)->root = sub_root;
1342 memcpy(&BTRFS_I(inode)->location, &location,
1343 sizeof(location));
1344 btrfs_read_locked_inode(inode);
1345 unlock_new_inode(inode);
1346 }
1347 }
1348 return d_splice_alias(inode, dentry);
1349}
1350
Chris Mason39279cc2007-06-12 06:35:45 -04001351static unsigned char btrfs_filetype_table[] = {
1352 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1353};
1354
1355static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1356{
Chris Mason6da6aba2007-12-18 16:15:09 -05001357 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001358 struct btrfs_root *root = BTRFS_I(inode)->root;
1359 struct btrfs_item *item;
1360 struct btrfs_dir_item *di;
1361 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001362 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001363 struct btrfs_path *path;
1364 int ret;
1365 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001366 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001367 int slot;
1368 int advance;
1369 unsigned char d_type;
1370 int over = 0;
1371 u32 di_cur;
1372 u32 di_total;
1373 u32 di_len;
1374 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001375 char tmp_name[32];
1376 char *name_ptr;
1377 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001378
1379 /* FIXME, use a real flag for deciding about the key type */
1380 if (root->fs_info->tree_root == root)
1381 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001382
Chris Mason39544012007-12-12 14:38:19 -05001383 /* special case for "." */
1384 if (filp->f_pos == 0) {
1385 over = filldir(dirent, ".", 1,
1386 1, inode->i_ino,
1387 DT_DIR);
1388 if (over)
1389 return 0;
1390 filp->f_pos = 1;
1391 }
1392
Chris Mason39279cc2007-06-12 06:35:45 -04001393 mutex_lock(&root->fs_info->fs_mutex);
1394 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001395 path = btrfs_alloc_path();
1396 path->reada = 2;
1397
1398 /* special case for .., just use the back ref */
1399 if (filp->f_pos == 1) {
1400 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1401 key.offset = 0;
1402 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1403 BUG_ON(ret == 0);
1404 leaf = path->nodes[0];
1405 slot = path->slots[0];
1406 nritems = btrfs_header_nritems(leaf);
1407 if (slot >= nritems) {
1408 btrfs_release_path(root, path);
1409 goto read_dir_items;
1410 }
1411 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1412 btrfs_release_path(root, path);
1413 if (found_key.objectid != key.objectid ||
1414 found_key.type != BTRFS_INODE_REF_KEY)
1415 goto read_dir_items;
1416 over = filldir(dirent, "..", 2,
1417 2, found_key.offset, DT_DIR);
1418 if (over)
1419 goto nopos;
1420 filp->f_pos = 2;
1421 }
1422
1423read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001424 btrfs_set_key_type(&key, key_type);
1425 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001426
Chris Mason39279cc2007-06-12 06:35:45 -04001427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1428 if (ret < 0)
1429 goto err;
1430 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001431 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001432 leaf = path->nodes[0];
1433 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001434 slot = path->slots[0];
1435 if (advance || slot >= nritems) {
1436 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001437 ret = btrfs_next_leaf(root, path);
1438 if (ret)
1439 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001440 leaf = path->nodes[0];
1441 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001442 slot = path->slots[0];
1443 } else {
1444 slot++;
1445 path->slots[0]++;
1446 }
1447 }
1448 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001449 item = btrfs_item_nr(leaf, slot);
1450 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1451
1452 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001453 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001454 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001455 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001456 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001457 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001458
1459 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001460 advance = 1;
1461 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1462 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001463 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001464 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001465 struct btrfs_key location;
1466
1467 name_len = btrfs_dir_name_len(leaf, di);
1468 if (name_len < 32) {
1469 name_ptr = tmp_name;
1470 } else {
1471 name_ptr = kmalloc(name_len, GFP_NOFS);
1472 BUG_ON(!name_ptr);
1473 }
1474 read_extent_buffer(leaf, name_ptr,
1475 (unsigned long)(di + 1), name_len);
1476
1477 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1478 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001479 over = filldir(dirent, name_ptr, name_len,
1480 found_key.offset,
1481 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001482 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001483
1484 if (name_ptr != tmp_name)
1485 kfree(name_ptr);
1486
Chris Mason39279cc2007-06-12 06:35:45 -04001487 if (over)
1488 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001489 di_len = btrfs_dir_name_len(leaf, di) +
1490 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001491 di_cur += di_len;
1492 di = (struct btrfs_dir_item *)((char *)di + di_len);
1493 }
1494 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001495 if (key_type == BTRFS_DIR_INDEX_KEY)
1496 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1497 else
1498 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001499nopos:
1500 ret = 0;
1501err:
1502 btrfs_release_path(root, path);
1503 btrfs_free_path(path);
1504 mutex_unlock(&root->fs_info->fs_mutex);
1505 return ret;
1506}
1507
1508int btrfs_write_inode(struct inode *inode, int wait)
1509{
1510 struct btrfs_root *root = BTRFS_I(inode)->root;
1511 struct btrfs_trans_handle *trans;
1512 int ret = 0;
1513
1514 if (wait) {
1515 mutex_lock(&root->fs_info->fs_mutex);
1516 trans = btrfs_start_transaction(root, 1);
1517 btrfs_set_trans_block_group(trans, inode);
1518 ret = btrfs_commit_transaction(trans, root);
1519 mutex_unlock(&root->fs_info->fs_mutex);
1520 }
1521 return ret;
1522}
1523
1524/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001525 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001526 * inode changes. But, it is most likely to find the inode in cache.
1527 * FIXME, needs more benchmarking...there are no reasons other than performance
1528 * to keep or drop this code.
1529 */
1530void btrfs_dirty_inode(struct inode *inode)
1531{
1532 struct btrfs_root *root = BTRFS_I(inode)->root;
1533 struct btrfs_trans_handle *trans;
1534
1535 mutex_lock(&root->fs_info->fs_mutex);
1536 trans = btrfs_start_transaction(root, 1);
1537 btrfs_set_trans_block_group(trans, inode);
1538 btrfs_update_inode(trans, root, inode);
1539 btrfs_end_transaction(trans, root);
1540 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001541}
1542
1543static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001545 const char *name, int name_len,
1546 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001547 u64 objectid,
1548 struct btrfs_block_group_cache *group,
1549 int mode)
1550{
1551 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001552 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001553 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001554 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001555 struct btrfs_inode_ref *ref;
1556 struct btrfs_key key[2];
1557 u32 sizes[2];
1558 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001559 int ret;
1560 int owner;
1561
Chris Mason5f39d392007-10-15 16:14:19 -04001562 path = btrfs_alloc_path();
1563 BUG_ON(!path);
1564
Chris Mason39279cc2007-06-12 06:35:45 -04001565 inode = new_inode(root->fs_info->sb);
1566 if (!inode)
1567 return ERR_PTR(-ENOMEM);
1568
Chris Masond1310b22008-01-24 16:13:08 -05001569 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1570 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04001571 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001572 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001573 BTRFS_I(inode)->root = root;
Chris Masonb888db2b2007-08-27 16:49:44 -04001574
Chris Mason39279cc2007-06-12 06:35:45 -04001575 if (mode & S_IFDIR)
1576 owner = 0;
1577 else
1578 owner = 1;
1579 group = btrfs_find_block_group(root, group, 0, 0, owner);
1580 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001581 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001582
1583 key[0].objectid = objectid;
1584 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1585 key[0].offset = 0;
1586
1587 key[1].objectid = objectid;
1588 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1589 key[1].offset = ref_objectid;
1590
1591 sizes[0] = sizeof(struct btrfs_inode_item);
1592 sizes[1] = name_len + sizeof(*ref);
1593
1594 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1595 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001596 goto fail;
1597
Chris Mason9c583092008-01-29 15:15:18 -05001598 if (objectid > root->highest_inode)
1599 root->highest_inode = objectid;
1600
Chris Mason39279cc2007-06-12 06:35:45 -04001601 inode->i_uid = current->fsuid;
1602 inode->i_gid = current->fsgid;
1603 inode->i_mode = mode;
1604 inode->i_ino = objectid;
1605 inode->i_blocks = 0;
1606 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001607 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1608 struct btrfs_inode_item);
1609 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001610
1611 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1612 struct btrfs_inode_ref);
1613 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1614 ptr = (unsigned long)(ref + 1);
1615 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1616
Chris Mason5f39d392007-10-15 16:14:19 -04001617 btrfs_mark_buffer_dirty(path->nodes[0]);
1618 btrfs_free_path(path);
1619
Chris Mason39279cc2007-06-12 06:35:45 -04001620 location = &BTRFS_I(inode)->location;
1621 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001622 location->offset = 0;
1623 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1624
Chris Mason39279cc2007-06-12 06:35:45 -04001625 insert_inode_hash(inode);
1626 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001627fail:
1628 btrfs_free_path(path);
1629 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001630}
1631
1632static inline u8 btrfs_inode_type(struct inode *inode)
1633{
1634 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1635}
1636
1637static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001638 struct dentry *dentry, struct inode *inode,
1639 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001640{
1641 int ret;
1642 struct btrfs_key key;
1643 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001644 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001645
Chris Mason39279cc2007-06-12 06:35:45 -04001646 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001647 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1648 key.offset = 0;
1649
1650 ret = btrfs_insert_dir_item(trans, root,
1651 dentry->d_name.name, dentry->d_name.len,
1652 dentry->d_parent->d_inode->i_ino,
1653 &key, btrfs_inode_type(inode));
1654 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001655 if (add_backref) {
1656 ret = btrfs_insert_inode_ref(trans, root,
1657 dentry->d_name.name,
1658 dentry->d_name.len,
1659 inode->i_ino,
1660 dentry->d_parent->d_inode->i_ino);
1661 }
Chris Mason79c44582007-06-25 10:09:33 -04001662 parent_inode = dentry->d_parent->d_inode;
1663 parent_inode->i_size += dentry->d_name.len * 2;
1664 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001665 ret = btrfs_update_inode(trans, root,
1666 dentry->d_parent->d_inode);
1667 }
1668 return ret;
1669}
1670
1671static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001672 struct dentry *dentry, struct inode *inode,
1673 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001674{
Chris Mason9c583092008-01-29 15:15:18 -05001675 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001676 if (!err) {
1677 d_instantiate(dentry, inode);
1678 return 0;
1679 }
1680 if (err > 0)
1681 err = -EEXIST;
1682 return err;
1683}
1684
Josef Bacik618e21d2007-07-11 10:18:17 -04001685static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1686 int mode, dev_t rdev)
1687{
1688 struct btrfs_trans_handle *trans;
1689 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001690 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001691 int err;
1692 int drop_inode = 0;
1693 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001694 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001695
1696 if (!new_valid_dev(rdev))
1697 return -EINVAL;
1698
1699 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001700 err = btrfs_check_free_space(root, 1, 0);
1701 if (err)
1702 goto fail;
1703
Josef Bacik618e21d2007-07-11 10:18:17 -04001704 trans = btrfs_start_transaction(root, 1);
1705 btrfs_set_trans_block_group(trans, dir);
1706
1707 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1708 if (err) {
1709 err = -ENOSPC;
1710 goto out_unlock;
1711 }
1712
Chris Mason9c583092008-01-29 15:15:18 -05001713 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1714 dentry->d_name.len,
1715 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001716 BTRFS_I(dir)->block_group, mode);
1717 err = PTR_ERR(inode);
1718 if (IS_ERR(inode))
1719 goto out_unlock;
1720
1721 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001722 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001723 if (err)
1724 drop_inode = 1;
1725 else {
1726 inode->i_op = &btrfs_special_inode_operations;
1727 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001728 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001729 }
1730 dir->i_sb->s_dirt = 1;
1731 btrfs_update_inode_block_group(trans, inode);
1732 btrfs_update_inode_block_group(trans, dir);
1733out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001734 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001735 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001736fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001737 mutex_unlock(&root->fs_info->fs_mutex);
1738
1739 if (drop_inode) {
1740 inode_dec_link_count(inode);
1741 iput(inode);
1742 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001743 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001744 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001745 return err;
1746}
1747
Chris Mason39279cc2007-06-12 06:35:45 -04001748static int btrfs_create(struct inode *dir, struct dentry *dentry,
1749 int mode, struct nameidata *nd)
1750{
1751 struct btrfs_trans_handle *trans;
1752 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001753 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001754 int err;
1755 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001756 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001757 u64 objectid;
1758
1759 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001760 err = btrfs_check_free_space(root, 1, 0);
1761 if (err)
1762 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001763 trans = btrfs_start_transaction(root, 1);
1764 btrfs_set_trans_block_group(trans, dir);
1765
1766 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1767 if (err) {
1768 err = -ENOSPC;
1769 goto out_unlock;
1770 }
1771
Chris Mason9c583092008-01-29 15:15:18 -05001772 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1773 dentry->d_name.len,
1774 dentry->d_parent->d_inode->i_ino,
1775 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001776 err = PTR_ERR(inode);
1777 if (IS_ERR(inode))
1778 goto out_unlock;
1779
1780 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001781 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001782 if (err)
1783 drop_inode = 1;
1784 else {
1785 inode->i_mapping->a_ops = &btrfs_aops;
1786 inode->i_fop = &btrfs_file_operations;
1787 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001788 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1789 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001790 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001791 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001792 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001793 }
1794 dir->i_sb->s_dirt = 1;
1795 btrfs_update_inode_block_group(trans, inode);
1796 btrfs_update_inode_block_group(trans, dir);
1797out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001798 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001799 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001800fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001801 mutex_unlock(&root->fs_info->fs_mutex);
1802
1803 if (drop_inode) {
1804 inode_dec_link_count(inode);
1805 iput(inode);
1806 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001807 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001808 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001809 return err;
1810}
1811
1812static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1813 struct dentry *dentry)
1814{
1815 struct btrfs_trans_handle *trans;
1816 struct btrfs_root *root = BTRFS_I(dir)->root;
1817 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001818 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001819 int err;
1820 int drop_inode = 0;
1821
1822 if (inode->i_nlink == 0)
1823 return -ENOENT;
1824
Chris Mason6da6aba2007-12-18 16:15:09 -05001825#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1826 inode->i_nlink++;
1827#else
Chris Mason39279cc2007-06-12 06:35:45 -04001828 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001829#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001830 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001831 err = btrfs_check_free_space(root, 1, 0);
1832 if (err)
1833 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001834 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001835
Chris Mason39279cc2007-06-12 06:35:45 -04001836 btrfs_set_trans_block_group(trans, dir);
1837 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001838 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001839
Chris Mason39279cc2007-06-12 06:35:45 -04001840 if (err)
1841 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001842
Chris Mason39279cc2007-06-12 06:35:45 -04001843 dir->i_sb->s_dirt = 1;
1844 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001845 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001846
Chris Mason54aa1f42007-06-22 14:16:25 -04001847 if (err)
1848 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001849
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001850 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001851 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001852fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001853 mutex_unlock(&root->fs_info->fs_mutex);
1854
1855 if (drop_inode) {
1856 inode_dec_link_count(inode);
1857 iput(inode);
1858 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001859 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001860 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001861 return err;
1862}
1863
Chris Mason39279cc2007-06-12 06:35:45 -04001864static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1865{
1866 struct inode *inode;
1867 struct btrfs_trans_handle *trans;
1868 struct btrfs_root *root = BTRFS_I(dir)->root;
1869 int err = 0;
1870 int drop_on_err = 0;
1871 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001872 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001873
1874 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001875 err = btrfs_check_free_space(root, 1, 0);
1876 if (err)
1877 goto out_unlock;
1878
Chris Mason39279cc2007-06-12 06:35:45 -04001879 trans = btrfs_start_transaction(root, 1);
1880 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001881
Chris Mason39279cc2007-06-12 06:35:45 -04001882 if (IS_ERR(trans)) {
1883 err = PTR_ERR(trans);
1884 goto out_unlock;
1885 }
1886
1887 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1888 if (err) {
1889 err = -ENOSPC;
1890 goto out_unlock;
1891 }
1892
Chris Mason9c583092008-01-29 15:15:18 -05001893 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1894 dentry->d_name.len,
1895 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001896 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1897 if (IS_ERR(inode)) {
1898 err = PTR_ERR(inode);
1899 goto out_fail;
1900 }
Chris Mason5f39d392007-10-15 16:14:19 -04001901
Chris Mason39279cc2007-06-12 06:35:45 -04001902 drop_on_err = 1;
1903 inode->i_op = &btrfs_dir_inode_operations;
1904 inode->i_fop = &btrfs_dir_file_operations;
1905 btrfs_set_trans_block_group(trans, inode);
1906
Chris Mason39544012007-12-12 14:38:19 -05001907 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001908 err = btrfs_update_inode(trans, root, inode);
1909 if (err)
1910 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001911
Chris Mason9c583092008-01-29 15:15:18 -05001912 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001913 if (err)
1914 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001915
Chris Mason39279cc2007-06-12 06:35:45 -04001916 d_instantiate(dentry, inode);
1917 drop_on_err = 0;
1918 dir->i_sb->s_dirt = 1;
1919 btrfs_update_inode_block_group(trans, inode);
1920 btrfs_update_inode_block_group(trans, dir);
1921
1922out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001923 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001924 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001925
Chris Mason39279cc2007-06-12 06:35:45 -04001926out_unlock:
1927 mutex_unlock(&root->fs_info->fs_mutex);
1928 if (drop_on_err)
1929 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001930 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001931 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001932 return err;
1933}
1934
Chris Masona52d9a82007-08-27 16:49:44 -04001935struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001936 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001937 int create)
1938{
1939 int ret;
1940 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001941 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001942 u64 extent_start = 0;
1943 u64 extent_end = 0;
1944 u64 objectid = inode->i_ino;
1945 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001946 struct btrfs_path *path;
1947 struct btrfs_root *root = BTRFS_I(inode)->root;
1948 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001949 struct extent_buffer *leaf;
1950 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001951 struct extent_map *em = NULL;
1952 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001953 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001954 struct btrfs_trans_handle *trans = NULL;
1955
1956 path = btrfs_alloc_path();
1957 BUG_ON(!path);
1958 mutex_lock(&root->fs_info->fs_mutex);
1959
1960again:
Chris Masond1310b22008-01-24 16:13:08 -05001961 spin_lock(&em_tree->lock);
1962 em = lookup_extent_mapping(em_tree, start, len);
1963 spin_unlock(&em_tree->lock);
1964
Chris Masona52d9a82007-08-27 16:49:44 -04001965 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001966 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001967 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1968 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001969 WARN_ON(1);
1970 }
Chris Mason70dec802008-01-29 09:59:12 -05001971 if (em->block_start == EXTENT_MAP_INLINE && page)
1972 free_extent_map(em);
1973 else
1974 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001975 }
Chris Masond1310b22008-01-24 16:13:08 -05001976 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001977 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001978 err = -ENOMEM;
1979 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001980 }
Chris Masond1310b22008-01-24 16:13:08 -05001981
1982 em->start = EXTENT_MAP_HOLE;
1983 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001984 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001985 ret = btrfs_lookup_file_extent(trans, root, path,
1986 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001987 if (ret < 0) {
1988 err = ret;
1989 goto out;
1990 }
1991
1992 if (ret != 0) {
1993 if (path->slots[0] == 0)
1994 goto not_found;
1995 path->slots[0]--;
1996 }
1997
Chris Mason5f39d392007-10-15 16:14:19 -04001998 leaf = path->nodes[0];
1999 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002000 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002001 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002002 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2003 found_type = btrfs_key_type(&found_key);
2004 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002005 found_type != BTRFS_EXTENT_DATA_KEY) {
2006 goto not_found;
2007 }
2008
Chris Mason5f39d392007-10-15 16:14:19 -04002009 found_type = btrfs_file_extent_type(leaf, item);
2010 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002011 if (found_type == BTRFS_FILE_EXTENT_REG) {
2012 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002013 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002014 err = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -04002015 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002016 em->start = start;
2017 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002018 if (start + len <= extent_start)
Chris Masonb888db2b2007-08-27 16:49:44 -04002019 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002020 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002021 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002022 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002023 }
2024 goto not_found_em;
2025 }
Chris Masondb945352007-10-15 16:15:53 -04002026 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2027 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002028 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002029 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002030 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002031 goto insert;
2032 }
Chris Masondb945352007-10-15 16:15:53 -04002033 bytenr += btrfs_file_extent_offset(leaf, item);
2034 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002035 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002036 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002037 goto insert;
2038 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002039 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002040 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002041 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002042 size_t size;
2043 size_t extent_offset;
2044 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2047 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002048 extent_end = (extent_start + size + root->sectorsize - 1) &
2049 ~((u64)root->sectorsize - 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04002050 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002051 em->start = start;
2052 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002053 if (start + len <= extent_start)
Chris Masonb888db2b2007-08-27 16:49:44 -04002054 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002055 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002056 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002057 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002058 }
2059 goto not_found_em;
2060 }
2061 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002062
2063 if (!page) {
2064 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002065 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002066 goto out;
2067 }
2068
Chris Mason70dec802008-01-29 09:59:12 -05002069 page_start = page_offset(page) + pg_offset;
2070 extent_offset = page_start - extent_start;
2071 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002072 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002073 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002074 em->len = (copy_size + root->sectorsize - 1) &
2075 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002076 map = kmap(page);
2077 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002078 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002079 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002080 copy_size);
2081 flush_dcache_page(page);
2082 } else if (create && PageUptodate(page)) {
2083 if (!trans) {
2084 kunmap(page);
2085 free_extent_map(em);
2086 em = NULL;
2087 btrfs_release_path(root, path);
2088 trans = btrfs_start_transaction(root, 1);
2089 goto again;
2090 }
Chris Mason70dec802008-01-29 09:59:12 -05002091 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002092 copy_size);
2093 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002094 }
Chris Masona52d9a82007-08-27 16:49:44 -04002095 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002096 set_extent_uptodate(io_tree, em->start,
2097 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002098 goto insert;
2099 } else {
2100 printk("unkknown found_type %d\n", found_type);
2101 WARN_ON(1);
2102 }
2103not_found:
2104 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002105 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002106not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002107 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002108insert:
2109 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002110 if (em->start > start || extent_map_end(em) <= start) {
2111 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002112 err = -EIO;
2113 goto out;
2114 }
Chris Masond1310b22008-01-24 16:13:08 -05002115
2116 err = 0;
2117 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002118 ret = add_extent_mapping(em_tree, em);
2119 if (ret == -EEXIST) {
2120 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002121 em = lookup_extent_mapping(em_tree, start, len);
2122 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002123 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002124 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002125 }
Chris Masona52d9a82007-08-27 16:49:44 -04002126 }
Chris Masond1310b22008-01-24 16:13:08 -05002127 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002128out:
2129 btrfs_free_path(path);
2130 if (trans) {
2131 ret = btrfs_end_transaction(trans, root);
2132 if (!err)
2133 err = ret;
2134 }
2135 mutex_unlock(&root->fs_info->fs_mutex);
2136 if (err) {
2137 free_extent_map(em);
2138 WARN_ON(1);
2139 return ERR_PTR(err);
2140 }
2141 return em;
2142}
2143
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002144static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002145{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002146 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002147}
2148
Chris Mason9ebefb182007-06-15 13:50:00 -04002149int btrfs_readpage(struct file *file, struct page *page)
2150{
Chris Masond1310b22008-01-24 16:13:08 -05002151 struct extent_io_tree *tree;
2152 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002153 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002154}
Chris Mason1832a6d2007-12-21 16:27:21 -05002155
Chris Mason39279cc2007-06-12 06:35:45 -04002156static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2157{
Chris Masond1310b22008-01-24 16:13:08 -05002158 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04002159
2160
2161 if (current->flags & PF_MEMALLOC) {
2162 redirty_page_for_writepage(wbc, page);
2163 unlock_page(page);
2164 return 0;
2165 }
Chris Masond1310b22008-01-24 16:13:08 -05002166 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002167 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2168}
Chris Mason39279cc2007-06-12 06:35:45 -04002169
Chris Masonb293f02e2007-11-01 19:45:34 -04002170static int btrfs_writepages(struct address_space *mapping,
2171 struct writeback_control *wbc)
2172{
Chris Masond1310b22008-01-24 16:13:08 -05002173 struct extent_io_tree *tree;
2174 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04002175 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2176}
2177
Chris Mason3ab2fb52007-11-08 10:59:22 -05002178static int
2179btrfs_readpages(struct file *file, struct address_space *mapping,
2180 struct list_head *pages, unsigned nr_pages)
2181{
Chris Masond1310b22008-01-24 16:13:08 -05002182 struct extent_io_tree *tree;
2183 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002184 return extent_readpages(tree, mapping, pages, nr_pages,
2185 btrfs_get_extent);
2186}
2187
Chris Mason70dec802008-01-29 09:59:12 -05002188static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002189{
Chris Masond1310b22008-01-24 16:13:08 -05002190 struct extent_io_tree *tree;
2191 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002192 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002193
Chris Masond1310b22008-01-24 16:13:08 -05002194 tree = &BTRFS_I(page->mapping->host)->io_tree;
2195 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002196 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002197 if (ret == 1) {
2198 ClearPagePrivate(page);
2199 set_page_private(page, 0);
2200 page_cache_release(page);
2201 }
2202 return ret;
2203}
Chris Mason39279cc2007-06-12 06:35:45 -04002204
Chris Masona52d9a82007-08-27 16:49:44 -04002205static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2206{
Chris Masond1310b22008-01-24 16:13:08 -05002207 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002208
Chris Masond1310b22008-01-24 16:13:08 -05002209 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002210 extent_invalidatepage(tree, page, offset);
2211 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002212}
2213
Chris Mason9ebefb182007-06-15 13:50:00 -04002214/*
2215 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2216 * called from a page fault handler when a page is first dirtied. Hence we must
2217 * be careful to check for EOF conditions here. We set the page up correctly
2218 * for a written page which means we get ENOSPC checking when writing into
2219 * holes and correct delalloc and unwritten extent mapping on filesystems that
2220 * support these features.
2221 *
2222 * We are not allowed to take the i_mutex here so we have to play games to
2223 * protect against truncate races as the page could now be beyond EOF. Because
2224 * vmtruncate() writes the inode size before removing pages, once we have the
2225 * page lock we can determine safely if the page is beyond EOF. If it is not
2226 * beyond EOF, then the page is guaranteed safe against truncation until we
2227 * unlock the page.
2228 */
2229int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2230{
Chris Mason6da6aba2007-12-18 16:15:09 -05002231 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002232 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002233 unsigned long end;
2234 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002235 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002236 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002237
Chris Mason1832a6d2007-12-21 16:27:21 -05002238 mutex_lock(&root->fs_info->fs_mutex);
2239 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002240 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002241 if (ret)
2242 goto out;
2243
2244 ret = -EINVAL;
2245
Chris Mason9ebefb182007-06-15 13:50:00 -04002246 lock_page(page);
2247 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002248 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002249 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002250
Chris Mason9ebefb182007-06-15 13:50:00 -04002251 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002252 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002253 /* page got truncated out from underneath us */
2254 goto out_unlock;
2255 }
2256
2257 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002258 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002259 end = size & ~PAGE_CACHE_MASK;
2260 else
2261 end = PAGE_CACHE_SIZE;
2262
Chris Masonb888db2b2007-08-27 16:49:44 -04002263 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002264
2265out_unlock:
2266 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002267out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002268 return ret;
2269}
2270
Chris Mason39279cc2007-06-12 06:35:45 -04002271static void btrfs_truncate(struct inode *inode)
2272{
2273 struct btrfs_root *root = BTRFS_I(inode)->root;
2274 int ret;
2275 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002276 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002277
2278 if (!S_ISREG(inode->i_mode))
2279 return;
2280 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2281 return;
2282
2283 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2284
2285 mutex_lock(&root->fs_info->fs_mutex);
2286 trans = btrfs_start_transaction(root, 1);
2287 btrfs_set_trans_block_group(trans, inode);
2288
2289 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002290 ret = btrfs_truncate_in_trans(trans, root, inode,
2291 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002292 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002293 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002294
Chris Mason39279cc2007-06-12 06:35:45 -04002295 ret = btrfs_end_transaction(trans, root);
2296 BUG_ON(ret);
2297 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002298 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002299 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002300}
2301
Chris Mason4313b392008-01-03 09:08:48 -05002302static int noinline create_subvol(struct btrfs_root *root, char *name,
2303 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002304{
2305 struct btrfs_trans_handle *trans;
2306 struct btrfs_key key;
2307 struct btrfs_root_item root_item;
2308 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002309 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002310 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002311 struct inode *inode;
2312 struct inode *dir;
2313 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002314 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002315 u64 objectid;
2316 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002317 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002318
2319 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002320 ret = btrfs_check_free_space(root, 1, 0);
2321 if (ret)
2322 goto fail_commit;
2323
Chris Mason39279cc2007-06-12 06:35:45 -04002324 trans = btrfs_start_transaction(root, 1);
2325 BUG_ON(!trans);
2326
Chris Mason7bb86312007-12-11 09:25:06 -05002327 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2328 0, &objectid);
2329 if (ret)
2330 goto fail;
2331
2332 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2333 objectid, trans->transid, 0, 0,
2334 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002335 if (IS_ERR(leaf))
2336 return PTR_ERR(leaf);
2337
2338 btrfs_set_header_nritems(leaf, 0);
2339 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002340 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002341 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002342 btrfs_set_header_owner(leaf, objectid);
2343
Chris Mason5f39d392007-10-15 16:14:19 -04002344 write_extent_buffer(leaf, root->fs_info->fsid,
2345 (unsigned long)btrfs_header_fsid(leaf),
2346 BTRFS_FSID_SIZE);
2347 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002348
2349 inode_item = &root_item.inode;
2350 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002351 inode_item->generation = cpu_to_le64(1);
2352 inode_item->size = cpu_to_le64(3);
2353 inode_item->nlink = cpu_to_le32(1);
2354 inode_item->nblocks = cpu_to_le64(1);
2355 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002356
Chris Masondb945352007-10-15 16:15:53 -04002357 btrfs_set_root_bytenr(&root_item, leaf->start);
2358 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002359 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002360 btrfs_set_root_used(&root_item, 0);
2361
Chris Mason5eda7b52007-06-22 14:16:25 -04002362 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2363 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002364
2365 free_extent_buffer(leaf);
2366 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002367
Chris Mason39279cc2007-06-12 06:35:45 -04002368 btrfs_set_root_dirid(&root_item, new_dirid);
2369
2370 key.objectid = objectid;
2371 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002372 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2373 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2374 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002375 if (ret)
2376 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002377
2378 /*
2379 * insert the directory item
2380 */
2381 key.offset = (u64)-1;
2382 dir = root->fs_info->sb->s_root->d_inode;
2383 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2384 name, namelen, dir->i_ino, &key,
2385 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002386 if (ret)
2387 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002388
Chris Mason39544012007-12-12 14:38:19 -05002389 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2390 name, namelen, objectid,
2391 root->fs_info->sb->s_root->d_inode->i_ino);
2392 if (ret)
2393 goto fail;
2394
Chris Mason39279cc2007-06-12 06:35:45 -04002395 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002396 if (ret)
2397 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002398
Josef Bacik58176a92007-08-29 15:47:34 -04002399 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002400 BUG_ON(!new_root);
2401
2402 trans = btrfs_start_transaction(new_root, 1);
2403 BUG_ON(!trans);
2404
Chris Mason9c583092008-01-29 15:15:18 -05002405 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2406 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002407 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002408 if (IS_ERR(inode))
2409 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002410 inode->i_op = &btrfs_dir_inode_operations;
2411 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002412 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002413
Chris Mason39544012007-12-12 14:38:19 -05002414 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2415 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002416 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002417 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002418 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002419 if (ret)
2420 goto fail;
2421fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002422 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002423 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002424 if (err && !ret)
2425 ret = err;
2426fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002427 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002428 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002429 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002430 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002431}
2432
2433static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2434{
Chris Mason3063d292008-01-08 15:46:30 -05002435 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002436 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002437 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002438 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002439 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002440
2441 if (!root->ref_cows)
2442 return -EINVAL;
2443
2444 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002445 ret = btrfs_check_free_space(root, 1, 0);
2446 if (ret)
2447 goto fail_unlock;
2448
Chris Mason3063d292008-01-08 15:46:30 -05002449 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2450 if (!pending_snapshot) {
2451 ret = -ENOMEM;
2452 goto fail_unlock;
2453 }
Yanfb4bc1e2008-01-17 11:59:51 -05002454 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002455 if (!pending_snapshot->name) {
2456 ret = -ENOMEM;
2457 kfree(pending_snapshot);
2458 goto fail_unlock;
2459 }
Yanfb4bc1e2008-01-17 11:59:51 -05002460 memcpy(pending_snapshot->name, name, namelen);
2461 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002462 trans = btrfs_start_transaction(root, 1);
2463 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002464 pending_snapshot->root = root;
2465 list_add(&pending_snapshot->list,
2466 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002467 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002468 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002469
Chris Mason1832a6d2007-12-21 16:27:21 -05002470fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002471 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002472 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002473 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002474 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002475}
2476
Chris Masonedbd8d42007-12-21 16:27:24 -05002477unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002478 struct file_ra_state *ra, struct file *file,
2479 pgoff_t offset, pgoff_t last_index)
2480{
2481 pgoff_t req_size;
2482
2483#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2484 req_size = last_index - offset + 1;
2485 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2486 return offset;
2487#else
2488 req_size = min(last_index - offset + 1, (pgoff_t)128);
2489 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2490 return offset + req_size;
2491#endif
2492}
2493
2494int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002495 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002496 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002497 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002498 struct page *page;
2499 unsigned long last_index;
2500 unsigned long ra_index = 0;
2501 u64 page_start;
2502 u64 page_end;
2503 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002504 int ret;
2505
2506 mutex_lock(&root->fs_info->fs_mutex);
2507 ret = btrfs_check_free_space(root, inode->i_size, 0);
2508 mutex_unlock(&root->fs_info->fs_mutex);
2509 if (ret)
2510 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002511
2512 mutex_lock(&inode->i_mutex);
2513 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2514 for (i = 0; i <= last_index; i++) {
2515 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002516 ra_index = btrfs_force_ra(inode->i_mapping,
2517 &file->f_ra,
2518 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002519 }
2520 page = grab_cache_page(inode->i_mapping, i);
2521 if (!page)
2522 goto out_unlock;
2523 if (!PageUptodate(page)) {
2524 btrfs_readpage(NULL, page);
2525 lock_page(page);
2526 if (!PageUptodate(page)) {
2527 unlock_page(page);
2528 page_cache_release(page);
2529 goto out_unlock;
2530 }
2531 }
Chris Mason35ebb932007-10-30 16:56:53 -04002532 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002533 page_end = page_start + PAGE_CACHE_SIZE - 1;
2534
Chris Masond1310b22008-01-24 16:13:08 -05002535 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002536 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002537 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002538
Chris Masond1310b22008-01-24 16:13:08 -05002539 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002540 set_page_dirty(page);
2541 unlock_page(page);
2542 page_cache_release(page);
2543 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2544 }
2545
2546out_unlock:
2547 mutex_unlock(&inode->i_mutex);
2548 return 0;
2549}
2550
Chris Masonedbd8d42007-12-21 16:27:24 -05002551static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2552{
2553 u64 new_size;
2554 u64 old_size;
2555 struct btrfs_ioctl_vol_args *vol_args;
2556 struct btrfs_trans_handle *trans;
2557 char *sizestr;
2558 int ret = 0;
2559 int namelen;
2560 int mod = 0;
2561
2562 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2563
2564 if (!vol_args)
2565 return -ENOMEM;
2566
2567 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2568 ret = -EFAULT;
2569 goto out;
2570 }
2571 namelen = strlen(vol_args->name);
2572 if (namelen > BTRFS_VOL_NAME_MAX) {
2573 ret = -EINVAL;
2574 goto out;
2575 }
2576
2577 sizestr = vol_args->name;
2578 if (!strcmp(sizestr, "max"))
2579 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2580 else {
2581 if (sizestr[0] == '-') {
2582 mod = -1;
2583 sizestr++;
2584 } else if (sizestr[0] == '+') {
2585 mod = 1;
2586 sizestr++;
2587 }
2588 new_size = btrfs_parse_size(sizestr);
2589 if (new_size == 0) {
2590 ret = -EINVAL;
2591 goto out;
2592 }
2593 }
2594
2595 mutex_lock(&root->fs_info->fs_mutex);
2596 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2597
2598 if (mod < 0) {
2599 if (new_size > old_size) {
2600 ret = -EINVAL;
2601 goto out_unlock;
2602 }
2603 new_size = old_size - new_size;
2604 } else if (mod > 0) {
2605 new_size = old_size + new_size;
2606 }
2607
2608 if (new_size < 256 * 1024 * 1024) {
2609 ret = -EINVAL;
2610 goto out_unlock;
2611 }
2612 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2613 ret = -EFBIG;
2614 goto out_unlock;
2615 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002616
2617 do_div(new_size, root->sectorsize);
2618 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002619
2620printk("new size is %Lu\n", new_size);
2621 if (new_size > old_size) {
2622 trans = btrfs_start_transaction(root, 1);
2623 ret = btrfs_grow_extent_tree(trans, root, new_size);
2624 btrfs_commit_transaction(trans, root);
2625 } else {
2626 ret = btrfs_shrink_extent_tree(root, new_size);
2627 }
2628
2629out_unlock:
2630 mutex_unlock(&root->fs_info->fs_mutex);
2631out:
2632 kfree(vol_args);
2633 return ret;
2634}
2635
Chris Mason4313b392008-01-03 09:08:48 -05002636static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2637 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002638{
Chris Mason4aec2b52007-12-18 16:25:45 -05002639 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002640 struct btrfs_dir_item *di;
2641 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002642 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002643 int namelen;
2644 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002645
Chris Mason4aec2b52007-12-18 16:25:45 -05002646 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002647
Chris Mason4aec2b52007-12-18 16:25:45 -05002648 if (!vol_args)
2649 return -ENOMEM;
2650
2651 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2652 ret = -EFAULT;
2653 goto out;
2654 }
2655
2656 namelen = strlen(vol_args->name);
2657 if (namelen > BTRFS_VOL_NAME_MAX) {
2658 ret = -EINVAL;
2659 goto out;
2660 }
2661 if (strchr(vol_args->name, '/')) {
2662 ret = -EINVAL;
2663 goto out;
2664 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002665
2666 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002667 if (!path) {
2668 ret = -ENOMEM;
2669 goto out;
2670 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002671
2672 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2673 mutex_lock(&root->fs_info->fs_mutex);
2674 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2675 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002676 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002677 mutex_unlock(&root->fs_info->fs_mutex);
2678 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002679
2680 if (di && !IS_ERR(di)) {
2681 ret = -EEXIST;
2682 goto out;
2683 }
2684
2685 if (IS_ERR(di)) {
2686 ret = PTR_ERR(di);
2687 goto out;
2688 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002689
2690 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002691 ret = create_subvol(root, vol_args->name, namelen);
2692 else
2693 ret = create_snapshot(root, vol_args->name, namelen);
2694out:
2695 kfree(vol_args);
2696 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002697}
2698
2699static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002700{
Chris Mason6da6aba2007-12-18 16:15:09 -05002701 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002702 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002703
2704 switch (inode->i_mode & S_IFMT) {
2705 case S_IFDIR:
2706 mutex_lock(&root->fs_info->fs_mutex);
2707 btrfs_defrag_root(root, 0);
2708 btrfs_defrag_root(root->fs_info->extent_root, 0);
2709 mutex_unlock(&root->fs_info->fs_mutex);
2710 break;
2711 case S_IFREG:
2712 btrfs_defrag_file(file);
2713 break;
2714 }
2715
2716 return 0;
2717}
2718
2719long btrfs_ioctl(struct file *file, unsigned int
2720 cmd, unsigned long arg)
2721{
Chris Mason6da6aba2007-12-18 16:15:09 -05002722 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002723
2724 switch (cmd) {
2725 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002726 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002727 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002728 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002729 case BTRFS_IOC_RESIZE:
2730 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002731 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002732
2733 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002734}
2735
Chris Mason39279cc2007-06-12 06:35:45 -04002736/*
2737 * Called inside transaction, so use GFP_NOFS
2738 */
2739struct inode *btrfs_alloc_inode(struct super_block *sb)
2740{
2741 struct btrfs_inode *ei;
2742
2743 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2744 if (!ei)
2745 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002746 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002747 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002748 return &ei->vfs_inode;
2749}
2750
2751void btrfs_destroy_inode(struct inode *inode)
2752{
2753 WARN_ON(!list_empty(&inode->i_dentry));
2754 WARN_ON(inode->i_data.nrpages);
2755
Chris Mason8c416c92008-01-14 15:10:26 -05002756 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002757 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2758}
2759
Chris Mason44ec0b72007-10-29 10:55:05 -04002760#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2761static void init_once(struct kmem_cache * cachep, void *foo)
2762#else
Chris Mason39279cc2007-06-12 06:35:45 -04002763static void init_once(void * foo, struct kmem_cache * cachep,
2764 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002765#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002766{
2767 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2768
2769 inode_init_once(&ei->vfs_inode);
2770}
2771
2772void btrfs_destroy_cachep(void)
2773{
2774 if (btrfs_inode_cachep)
2775 kmem_cache_destroy(btrfs_inode_cachep);
2776 if (btrfs_trans_handle_cachep)
2777 kmem_cache_destroy(btrfs_trans_handle_cachep);
2778 if (btrfs_transaction_cachep)
2779 kmem_cache_destroy(btrfs_transaction_cachep);
2780 if (btrfs_bit_radix_cachep)
2781 kmem_cache_destroy(btrfs_bit_radix_cachep);
2782 if (btrfs_path_cachep)
2783 kmem_cache_destroy(btrfs_path_cachep);
2784}
2785
Chris Mason86479a02007-09-10 19:58:16 -04002786struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002787 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002788#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2789 void (*ctor)(struct kmem_cache *, void *)
2790#else
Chris Mason92fee662007-07-25 12:31:35 -04002791 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002792 unsigned long)
2793#endif
2794 )
Chris Mason92fee662007-07-25 12:31:35 -04002795{
2796 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2797 SLAB_MEM_SPREAD | extra_flags), ctor
2798#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2799 ,NULL
2800#endif
2801 );
2802}
2803
Chris Mason39279cc2007-06-12 06:35:45 -04002804int btrfs_init_cachep(void)
2805{
Chris Mason86479a02007-09-10 19:58:16 -04002806 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002807 sizeof(struct btrfs_inode),
2808 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002809 if (!btrfs_inode_cachep)
2810 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002811 btrfs_trans_handle_cachep =
2812 btrfs_cache_create("btrfs_trans_handle_cache",
2813 sizeof(struct btrfs_trans_handle),
2814 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002815 if (!btrfs_trans_handle_cachep)
2816 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002817 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002818 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002819 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002820 if (!btrfs_transaction_cachep)
2821 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002822 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002823 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002824 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002825 if (!btrfs_path_cachep)
2826 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002827 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002828 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002829 if (!btrfs_bit_radix_cachep)
2830 goto fail;
2831 return 0;
2832fail:
2833 btrfs_destroy_cachep();
2834 return -ENOMEM;
2835}
2836
2837static int btrfs_getattr(struct vfsmount *mnt,
2838 struct dentry *dentry, struct kstat *stat)
2839{
2840 struct inode *inode = dentry->d_inode;
2841 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002842 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002843 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002844 return 0;
2845}
2846
2847static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2848 struct inode * new_dir,struct dentry *new_dentry)
2849{
2850 struct btrfs_trans_handle *trans;
2851 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2852 struct inode *new_inode = new_dentry->d_inode;
2853 struct inode *old_inode = old_dentry->d_inode;
2854 struct timespec ctime = CURRENT_TIME;
2855 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002856 int ret;
2857
2858 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2859 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2860 return -ENOTEMPTY;
2861 }
Chris Mason5f39d392007-10-15 16:14:19 -04002862
Chris Mason39279cc2007-06-12 06:35:45 -04002863 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002864 ret = btrfs_check_free_space(root, 1, 0);
2865 if (ret)
2866 goto out_unlock;
2867
Chris Mason39279cc2007-06-12 06:35:45 -04002868 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002869
Chris Mason39279cc2007-06-12 06:35:45 -04002870 btrfs_set_trans_block_group(trans, new_dir);
2871 path = btrfs_alloc_path();
2872 if (!path) {
2873 ret = -ENOMEM;
2874 goto out_fail;
2875 }
2876
2877 old_dentry->d_inode->i_nlink++;
2878 old_dir->i_ctime = old_dir->i_mtime = ctime;
2879 new_dir->i_ctime = new_dir->i_mtime = ctime;
2880 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002881
Chris Mason39279cc2007-06-12 06:35:45 -04002882 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2883 if (ret)
2884 goto out_fail;
2885
2886 if (new_inode) {
2887 new_inode->i_ctime = CURRENT_TIME;
2888 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2889 if (ret)
2890 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002891 }
Chris Mason9c583092008-01-29 15:15:18 -05002892 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002893 if (ret)
2894 goto out_fail;
2895
2896out_fail:
2897 btrfs_free_path(path);
2898 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002899out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002900 mutex_unlock(&root->fs_info->fs_mutex);
2901 return ret;
2902}
2903
2904static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2905 const char *symname)
2906{
2907 struct btrfs_trans_handle *trans;
2908 struct btrfs_root *root = BTRFS_I(dir)->root;
2909 struct btrfs_path *path;
2910 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002911 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002912 int err;
2913 int drop_inode = 0;
2914 u64 objectid;
2915 int name_len;
2916 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002917 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002918 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002919 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002920 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002921
2922 name_len = strlen(symname) + 1;
2923 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2924 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002925
Chris Mason39279cc2007-06-12 06:35:45 -04002926 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002927 err = btrfs_check_free_space(root, 1, 0);
2928 if (err)
2929 goto out_fail;
2930
Chris Mason39279cc2007-06-12 06:35:45 -04002931 trans = btrfs_start_transaction(root, 1);
2932 btrfs_set_trans_block_group(trans, dir);
2933
2934 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2935 if (err) {
2936 err = -ENOSPC;
2937 goto out_unlock;
2938 }
2939
Chris Mason9c583092008-01-29 15:15:18 -05002940 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2941 dentry->d_name.len,
2942 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002943 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2944 err = PTR_ERR(inode);
2945 if (IS_ERR(inode))
2946 goto out_unlock;
2947
2948 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002949 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002950 if (err)
2951 drop_inode = 1;
2952 else {
2953 inode->i_mapping->a_ops = &btrfs_aops;
2954 inode->i_fop = &btrfs_file_operations;
2955 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002956 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2957 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002958 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002959 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002960 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002961 }
2962 dir->i_sb->s_dirt = 1;
2963 btrfs_update_inode_block_group(trans, inode);
2964 btrfs_update_inode_block_group(trans, dir);
2965 if (drop_inode)
2966 goto out_unlock;
2967
2968 path = btrfs_alloc_path();
2969 BUG_ON(!path);
2970 key.objectid = inode->i_ino;
2971 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002972 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2973 datasize = btrfs_file_extent_calc_inline_size(name_len);
2974 err = btrfs_insert_empty_item(trans, root, path, &key,
2975 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002976 if (err) {
2977 drop_inode = 1;
2978 goto out_unlock;
2979 }
Chris Mason5f39d392007-10-15 16:14:19 -04002980 leaf = path->nodes[0];
2981 ei = btrfs_item_ptr(leaf, path->slots[0],
2982 struct btrfs_file_extent_item);
2983 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2984 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002985 BTRFS_FILE_EXTENT_INLINE);
2986 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002987 write_extent_buffer(leaf, symname, ptr, name_len);
2988 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002989 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002990
Chris Mason39279cc2007-06-12 06:35:45 -04002991 inode->i_op = &btrfs_symlink_inode_operations;
2992 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2993 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002994 err = btrfs_update_inode(trans, root, inode);
2995 if (err)
2996 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002997
2998out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002999 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003000 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003001out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003002 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003003 if (drop_inode) {
3004 inode_dec_link_count(inode);
3005 iput(inode);
3006 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003007 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003008 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003009 return err;
3010}
Yanfdebe2b2008-01-14 13:26:08 -05003011static int btrfs_permission(struct inode *inode, int mask,
3012 struct nameidata *nd)
3013{
3014 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3015 return -EACCES;
3016 return generic_permission(inode, mask, NULL);
3017}
Chris Mason39279cc2007-06-12 06:35:45 -04003018
3019static struct inode_operations btrfs_dir_inode_operations = {
3020 .lookup = btrfs_lookup,
3021 .create = btrfs_create,
3022 .unlink = btrfs_unlink,
3023 .link = btrfs_link,
3024 .mkdir = btrfs_mkdir,
3025 .rmdir = btrfs_rmdir,
3026 .rename = btrfs_rename,
3027 .symlink = btrfs_symlink,
3028 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003029 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003030 .setxattr = generic_setxattr,
3031 .getxattr = generic_getxattr,
3032 .listxattr = btrfs_listxattr,
3033 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003034 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003035};
Chris Mason39279cc2007-06-12 06:35:45 -04003036static struct inode_operations btrfs_dir_ro_inode_operations = {
3037 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003038 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003039};
Chris Mason39279cc2007-06-12 06:35:45 -04003040static struct file_operations btrfs_dir_file_operations = {
3041 .llseek = generic_file_llseek,
3042 .read = generic_read_dir,
3043 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003044 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003045#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003046 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003047#endif
3048};
3049
Chris Masond1310b22008-01-24 16:13:08 -05003050static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003051 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003052 // .writepage_io_hook = btrfs_writepage_io_hook,
3053 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003054 .readpage_io_hook = btrfs_readpage_io_hook,
3055 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003056 .set_bit_hook = btrfs_set_bit_hook,
3057 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003058};
3059
Chris Mason39279cc2007-06-12 06:35:45 -04003060static struct address_space_operations btrfs_aops = {
3061 .readpage = btrfs_readpage,
3062 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04003063 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003064 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003065 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003066 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003067 .invalidatepage = btrfs_invalidatepage,
3068 .releasepage = btrfs_releasepage,
3069 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003070};
3071
3072static struct address_space_operations btrfs_symlink_aops = {
3073 .readpage = btrfs_readpage,
3074 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003075 .invalidatepage = btrfs_invalidatepage,
3076 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003077};
3078
3079static struct inode_operations btrfs_file_inode_operations = {
3080 .truncate = btrfs_truncate,
3081 .getattr = btrfs_getattr,
3082 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003083 .setxattr = generic_setxattr,
3084 .getxattr = generic_getxattr,
3085 .listxattr = btrfs_listxattr,
3086 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003087 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003088};
Josef Bacik618e21d2007-07-11 10:18:17 -04003089static struct inode_operations btrfs_special_inode_operations = {
3090 .getattr = btrfs_getattr,
3091 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003092 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003093};
Chris Mason39279cc2007-06-12 06:35:45 -04003094static struct inode_operations btrfs_symlink_inode_operations = {
3095 .readlink = generic_readlink,
3096 .follow_link = page_follow_link_light,
3097 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003098 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003099};