blob: 8bc981c4f5efd0b5cca955756494559d81f2636b [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 Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Josef Bacik7b128762008-07-24 12:17:14 -040081static void btrfs_truncate(struct inode *inode);
82
Chris Mason1832a6d2007-12-21 16:27:21 -050083int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
84 int for_del)
85{
Chris Masona2135012008-06-25 16:01:30 -040086 u64 total;
87 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040089 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 int ret = 0;
91
Chris Masona2135012008-06-25 16:01:30 -040092 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 else
Chris Masonf9ef6602008-01-03 09:22:38 -050098 thresh = total * 85;
99
100 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
103 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 return ret;
106}
107
Chris Masonbe20aa92007-12-17 20:14:01 -0500108static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400109{
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400112 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500114 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400115 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500116 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
120 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400121
Chris Masonf9295742008-07-17 12:54:14 -0400122 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400123 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 btrfs_set_trans_block_group(trans, inode);
125
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500127 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500128 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400129
Chris Mason179e29e2007-11-01 11:28:41 -0400130 if (alloc_hint == EXTENT_MAP_INLINE)
131 goto out;
132
Chris Mason3b951512008-04-17 11:29:12 -0400133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400137
Chris Masonc59f8952007-12-17 20:14:04 -0500138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
142 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 if (ret) {
144 WARN_ON(1);
145 goto out;
146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147 em = alloc_extent_map(GFP_NOFS);
148 em->start = start;
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 while(1) {
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
159 free_extent_map(em);
160 break;
161 }
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
164 }
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166
Chris Mason98d20f62008-04-14 09:46:10 -0400167 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
169 ins.offset);
170 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 cur_alloc_size);
174 break;
175 }
Chris Masonc59f8952007-12-17 20:14:04 -0500176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400179 }
Chris Masonb888db22007-08-27 16:49:44 -0400180out:
181 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 return ret;
183}
184
185static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
186{
187 u64 extent_start;
188 u64 extent_end;
189 u64 bytenr;
190 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500191 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500192 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400194 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 struct extent_buffer *leaf;
196 int found_type;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
199 int ret;
200 int err;
201 struct btrfs_key found_key;
202
Chris Masonc31f8832008-01-08 15:46:31 -0500203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 path = btrfs_alloc_path();
205 BUG_ON(!path);
206again:
207 ret = btrfs_lookup_file_extent(NULL, root, path,
208 inode->i_ino, start, 0);
209 if (ret < 0) {
210 btrfs_free_path(path);
211 return ret;
212 }
213
214 cow_end = end;
215 if (ret != 0) {
216 if (path->slots[0] == 0)
217 goto not_found;
218 path->slots[0]--;
219 }
220
221 leaf = path->nodes[0];
222 item = btrfs_item_ptr(leaf, path->slots[0],
223 struct btrfs_file_extent_item);
224
225 /* are we inside the extent that was found? */
226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
227 found_type = btrfs_key_type(&found_key);
228 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400229 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231
232 found_type = btrfs_file_extent_type(leaf, item);
233 extent_start = found_key.offset;
234 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500235 u64 extent_num_bytes;
236
237 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
238 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 err = 0;
240
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 if (loops && start != extent_start)
242 goto not_found;
243
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 if (start < extent_start || start >= extent_end)
245 goto not_found;
246
247 cow_end = min(end, extent_end - 1);
248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (bytenr == 0)
250 goto not_found;
251
Chris Masona68d5932008-05-08 14:11:56 -0400252 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
253 bytenr) != 1) {
254 goto not_found;
255 }
256
Chris Masonc31f8832008-01-08 15:46:31 -0500257 /*
258 * we may be called by the resizer, make sure we're inside
259 * the limits of the FS
260 */
Chris Masona68d5932008-05-08 14:11:56 -0400261 block_group = btrfs_lookup_block_group(root->fs_info,
262 bytenr);
263 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500264 goto not_found;
265
Chris Masonbe20aa92007-12-17 20:14:01 -0500266 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500267 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500268 goto not_found;
269 }
270loop:
271 if (start > end) {
272 btrfs_free_path(path);
273 return 0;
274 }
275 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500276 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500277 goto again;
278
279not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400280 cow_file_range(inode, start, end);
281 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500282 goto loop;
283}
284
285static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
286{
287 struct btrfs_root *root = BTRFS_I(inode)->root;
288 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400289
Yanb98b6762008-01-08 15:54:37 -0500290 if (btrfs_test_opt(root, NODATACOW) ||
291 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500292 ret = run_delalloc_nocow(inode, start, end);
293 else
294 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500295
Chris Masonb888db22007-08-27 16:49:44 -0400296 return ret;
297}
298
Chris Mason291d6732008-01-29 15:55:23 -0500299int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500300 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500301{
Chris Masonbcbfce82008-04-22 13:26:47 -0400302 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500303 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500304 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400305 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500306 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500307 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400308 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500309 }
310 return 0;
311}
312
313int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500314 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500315{
Chris Masonb0c68f82008-01-31 11:05:37 -0500316 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500317 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400318 unsigned long flags;
319
320 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500321 if (end - start + 1 > root->fs_info->delalloc_bytes) {
322 printk("warning: delalloc account %Lu %Lu\n",
323 end - start + 1, root->fs_info->delalloc_bytes);
324 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500325 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500326 } else {
327 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500328 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500329 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400330 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500331 }
332 return 0;
333}
334
Chris Mason239b14b2008-03-24 15:02:07 -0400335int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
336 size_t size, struct bio *bio)
337{
338 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
339 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400340 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400341 u64 length = 0;
342 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400343 int ret;
344
Chris Masonf2d8d742008-04-21 10:03:05 -0400345 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400346 map_tree = &root->fs_info->mapping_tree;
347 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400348 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400349 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400350
Chris Mason239b14b2008-03-24 15:02:07 -0400351 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400352 return 1;
353 }
354 return 0;
355}
356
Chris Mason44b8bd72008-04-16 11:14:51 -0400357int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400358 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500359{
Chris Mason065631f2008-02-20 12:07:25 -0500360 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500361 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400362
Chris Mason3edf7d32008-07-18 06:17:13 -0400363 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400364 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400365
Chris Mason8b712842008-06-11 16:50:36 -0400366 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400367}
368
369int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
370 int mirror_num)
371{
372 struct btrfs_root *root = BTRFS_I(inode)->root;
373 int ret = 0;
374
Chris Masone6dcd2d2008-07-17 12:53:50 -0400375 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
376 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500377
Chris Masone6dcd2d2008-07-17 12:53:50 -0400378 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400379 goto mapit;
380 }
Chris Mason065631f2008-02-20 12:07:25 -0500381
Chris Mason44b8bd72008-04-16 11:14:51 -0400382 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
383 inode, rw, bio, mirror_num,
384 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400385mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400386 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500387}
Chris Mason6885f302008-02-20 16:11:05 -0500388
Chris Masonba1da2f2008-07-17 12:54:15 -0400389static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400390 struct inode *inode, u64 file_offset,
391 struct list_head *list)
392{
393 struct list_head *cur;
394 struct btrfs_ordered_sum *sum;
395
396 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400397 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400398 sum = list_entry(cur, struct btrfs_ordered_sum, list);
399 mutex_lock(&BTRFS_I(inode)->csum_mutex);
400 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
401 inode, sum);
402 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400403 }
404 return 0;
405}
406
Chris Mason247e7432008-07-17 12:53:51 -0400407struct btrfs_writepage_fixup {
408 struct page *page;
409 struct btrfs_work work;
410};
411
412/* see btrfs_writepage_start_hook for details on why this is required */
413void btrfs_writepage_fixup_worker(struct btrfs_work *work)
414{
415 struct btrfs_writepage_fixup *fixup;
416 struct btrfs_ordered_extent *ordered;
417 struct page *page;
418 struct inode *inode;
419 u64 page_start;
420 u64 page_end;
421
422 fixup = container_of(work, struct btrfs_writepage_fixup, work);
423 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400424again:
Chris Mason247e7432008-07-17 12:53:51 -0400425 lock_page(page);
426 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
427 ClearPageChecked(page);
428 goto out_page;
429 }
430
431 inode = page->mapping->host;
432 page_start = page_offset(page);
433 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
434
435 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400436
437 /* already ordered? We're done */
438 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
439 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400440 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400441 }
442
443 ordered = btrfs_lookup_ordered_extent(inode, page_start);
444 if (ordered) {
445 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
446 page_end, GFP_NOFS);
447 unlock_page(page);
448 btrfs_start_ordered_extent(inode, ordered, 1);
449 goto again;
450 }
Chris Mason247e7432008-07-17 12:53:51 -0400451
452 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
453 GFP_NOFS);
454 ClearPageChecked(page);
455out:
456 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
457out_page:
458 unlock_page(page);
459 page_cache_release(page);
460}
461
462/*
463 * There are a few paths in the higher layers of the kernel that directly
464 * set the page dirty bit without asking the filesystem if it is a
465 * good idea. This causes problems because we want to make sure COW
466 * properly happens and the data=ordered rules are followed.
467 *
468 * In our case any range that doesn't have the EXTENT_ORDERED bit set
469 * hasn't been properly setup for IO. We kick off an async process
470 * to fix it up. The async helper will wait for ordered extents, set
471 * the delalloc bit and make it safe to write the page.
472 */
473int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
474{
475 struct inode *inode = page->mapping->host;
476 struct btrfs_writepage_fixup *fixup;
477 struct btrfs_root *root = BTRFS_I(inode)->root;
478 int ret;
479
480 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
481 EXTENT_ORDERED, 0);
482 if (ret)
483 return 0;
484
485 if (PageChecked(page))
486 return -EAGAIN;
487
488 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
489 if (!fixup)
490 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400491
Chris Mason247e7432008-07-17 12:53:51 -0400492 SetPageChecked(page);
493 page_cache_get(page);
494 fixup->work.func = btrfs_writepage_fixup_worker;
495 fixup->page = page;
496 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
497 return -EAGAIN;
498}
499
Chris Mason211f90e2008-07-18 11:56:15 -0400500static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400501{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400502 struct btrfs_root *root = BTRFS_I(inode)->root;
503 struct btrfs_trans_handle *trans;
504 struct btrfs_ordered_extent *ordered_extent;
505 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400506 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
507 struct extent_map *em;
Chris Masonf4219502008-07-22 11:18:09 -0400508 struct extent_map *em_orig;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400509 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400510 u64 clear_start;
511 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400512 struct list_head list;
513 struct btrfs_key ins;
Chris Masonf4219502008-07-22 11:18:09 -0400514 struct rb_node *rb;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400515 int ret;
516
517 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400518 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400519 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400520
Chris Masonf9295742008-07-17 12:54:14 -0400521 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400522
523 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
524 BUG_ON(!ordered_extent);
525
526 lock_extent(io_tree, ordered_extent->file_offset,
527 ordered_extent->file_offset + ordered_extent->len - 1,
528 GFP_NOFS);
529
530 INIT_LIST_HEAD(&list);
531
532 ins.objectid = ordered_extent->start;
533 ins.offset = ordered_extent->len;
534 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400535
Chris Masone6dcd2d2008-07-17 12:53:50 -0400536 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
537 trans->transid, inode->i_ino,
538 ordered_extent->file_offset, &ins);
539 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400540
541 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400542
Chris Masonf4219502008-07-22 11:18:09 -0400543 spin_lock(&em_tree->lock);
544 clear_start = ordered_extent->file_offset;
545 clear_end = ordered_extent->file_offset + ordered_extent->len;
546 em = lookup_extent_mapping(em_tree, clear_start,
547 ordered_extent->len);
548 em_orig = em;
549 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
550 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
551 rb = rb_next(&em->rb_node);
552 if (!rb)
553 break;
554 em = rb_entry(rb, struct extent_map, rb_node);
555 }
556 free_extent_map(em_orig);
557 spin_unlock(&em_tree->lock);
558
Chris Masone6dcd2d2008-07-17 12:53:50 -0400559 ret = btrfs_drop_extents(trans, root, inode,
560 ordered_extent->file_offset,
561 ordered_extent->file_offset +
562 ordered_extent->len,
563 ordered_extent->file_offset, &alloc_hint);
564 BUG_ON(ret);
565 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
566 ordered_extent->file_offset,
567 ordered_extent->start,
568 ordered_extent->len,
569 ordered_extent->len, 0);
570 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400571
Chris Masone6dcd2d2008-07-17 12:53:50 -0400572 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
573 ordered_extent->file_offset +
574 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400575 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
576
Chris Masone6dcd2d2008-07-17 12:53:50 -0400577 inode->i_blocks += ordered_extent->len >> 9;
578 unlock_extent(io_tree, ordered_extent->file_offset,
579 ordered_extent->file_offset + ordered_extent->len - 1,
580 GFP_NOFS);
581 add_pending_csums(trans, inode, ordered_extent->file_offset,
582 &ordered_extent->list);
583
Chris Masondbe674a2008-07-17 12:54:05 -0400584 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400585 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400586
Chris Masone6dcd2d2008-07-17 12:53:50 -0400587 /* once for us */
588 btrfs_put_ordered_extent(ordered_extent);
589 /* once for the tree */
590 btrfs_put_ordered_extent(ordered_extent);
591
592 btrfs_update_inode(trans, root, inode);
593 btrfs_end_transaction(trans, root);
594 return 0;
595}
596
Chris Mason211f90e2008-07-18 11:56:15 -0400597int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
598 struct extent_state *state, int uptodate)
599{
600 return btrfs_finish_ordered_io(page->mapping->host, start, end);
601}
602
Chris Mason07157aa2007-08-30 08:50:51 -0400603int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
604{
605 int ret = 0;
606 struct inode *inode = page->mapping->host;
607 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500608 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400609 struct btrfs_csum_item *item;
610 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400611 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400612
Yanb98b6762008-01-08 15:54:37 -0500613 if (btrfs_test_opt(root, NODATASUM) ||
614 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500615 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400616
Chris Mason89642222008-07-24 09:41:53 -0400617 /*
618 * It is possible there is an ordered extent that has
619 * not yet finished for this range in the file. If so,
620 * that extent will have a csum cached, and it will insert
621 * the sum after all the blocks in the extent are fully
622 * on disk. So, look for an ordered extent and use the
623 * sum if found. We have to do this before looking in the
624 * btree because csum items are pre-inserted based on
625 * the file size. btrfs_lookup_csum might find an item
626 * that still hasn't been fully filled.
627 */
628 ret = btrfs_find_ordered_sum(inode, start, &csum);
629 if (ret == 0)
630 goto found;
631
632 ret = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400633 path = btrfs_alloc_path();
634 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
635 if (IS_ERR(item)) {
636 ret = PTR_ERR(item);
637 /* a csum that isn't present is a preallocated region. */
638 if (ret == -ENOENT || ret == -EFBIG)
639 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400640 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400641 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
642 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400643 goto out;
644 }
Chris Masonff79f812007-10-15 16:22:25 -0400645 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
646 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400647found:
Chris Masond1310b22008-01-24 16:13:08 -0500648 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400649out:
650 if (path)
651 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400652 return ret;
653}
654
Chris Mason7e383262008-04-09 16:28:12 -0400655struct io_failure_record {
656 struct page *page;
657 u64 start;
658 u64 len;
659 u64 logical;
660 int last_mirror;
661};
662
Chris Mason1259ab72008-05-12 13:39:03 -0400663int btrfs_io_failed_hook(struct bio *failed_bio,
664 struct page *page, u64 start, u64 end,
665 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400666{
667 struct io_failure_record *failrec = NULL;
668 u64 private;
669 struct extent_map *em;
670 struct inode *inode = page->mapping->host;
671 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400672 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400673 struct bio *bio;
674 int num_copies;
675 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400676 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400677 u64 logical;
678
679 ret = get_state_private(failure_tree, start, &private);
680 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400681 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
682 if (!failrec)
683 return -ENOMEM;
684 failrec->start = start;
685 failrec->len = end - start + 1;
686 failrec->last_mirror = 0;
687
Chris Mason3b951512008-04-17 11:29:12 -0400688 spin_lock(&em_tree->lock);
689 em = lookup_extent_mapping(em_tree, start, failrec->len);
690 if (em->start > start || em->start + em->len < start) {
691 free_extent_map(em);
692 em = NULL;
693 }
694 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400695
696 if (!em || IS_ERR(em)) {
697 kfree(failrec);
698 return -EIO;
699 }
700 logical = start - em->start;
701 logical = em->block_start + logical;
702 failrec->logical = logical;
703 free_extent_map(em);
704 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
705 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400706 set_state_private(failure_tree, start,
707 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400708 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400709 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400710 }
711 num_copies = btrfs_num_copies(
712 &BTRFS_I(inode)->root->fs_info->mapping_tree,
713 failrec->logical, failrec->len);
714 failrec->last_mirror++;
715 if (!state) {
716 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
717 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
718 failrec->start,
719 EXTENT_LOCKED);
720 if (state && state->start != failrec->start)
721 state = NULL;
722 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
723 }
724 if (!state || failrec->last_mirror > num_copies) {
725 set_state_private(failure_tree, failrec->start, 0);
726 clear_extent_bits(failure_tree, failrec->start,
727 failrec->start + failrec->len - 1,
728 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
729 kfree(failrec);
730 return -EIO;
731 }
732 bio = bio_alloc(GFP_NOFS, 1);
733 bio->bi_private = state;
734 bio->bi_end_io = failed_bio->bi_end_io;
735 bio->bi_sector = failrec->logical >> 9;
736 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400737 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400738 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400739 if (failed_bio->bi_rw & (1 << BIO_RW))
740 rw = WRITE;
741 else
742 rw = READ;
743
744 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
745 failrec->last_mirror);
746 return 0;
747}
748
749int btrfs_clean_io_failures(struct inode *inode, u64 start)
750{
751 u64 private;
752 u64 private_failure;
753 struct io_failure_record *failure;
754 int ret;
755
756 private = 0;
757 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
758 (u64)-1, 1, EXTENT_DIRTY)) {
759 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
760 start, &private_failure);
761 if (ret == 0) {
762 failure = (struct io_failure_record *)(unsigned long)
763 private_failure;
764 set_state_private(&BTRFS_I(inode)->io_failure_tree,
765 failure->start, 0);
766 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
767 failure->start,
768 failure->start + failure->len - 1,
769 EXTENT_DIRTY | EXTENT_LOCKED,
770 GFP_NOFS);
771 kfree(failure);
772 }
773 }
Chris Mason7e383262008-04-09 16:28:12 -0400774 return 0;
775}
776
Chris Mason70dec802008-01-29 09:59:12 -0500777int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
778 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400779{
Chris Mason35ebb932007-10-30 16:56:53 -0400780 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400781 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500782 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400783 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500784 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400785 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400786 struct btrfs_root *root = BTRFS_I(inode)->root;
787 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400788 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500789
Yanb98b6762008-01-08 15:54:37 -0500790 if (btrfs_test_opt(root, NODATASUM) ||
791 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500792 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500793 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500794 private = state->private;
795 ret = 0;
796 } else {
797 ret = get_state_private(io_tree, start, &private);
798 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400799 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400800 kaddr = kmap_atomic(page, KM_IRQ0);
801 if (ret) {
802 goto zeroit;
803 }
Chris Masonff79f812007-10-15 16:22:25 -0400804 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
805 btrfs_csum_final(csum, (char *)&csum);
806 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400807 goto zeroit;
808 }
809 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400810 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400811
812 /* if the io failure tree for this inode is non-empty,
813 * check to see if we've recovered from a failed IO
814 */
Chris Mason1259ab72008-05-12 13:39:03 -0400815 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400816 return 0;
817
818zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500819 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
820 page->mapping->host->i_ino, (unsigned long long)start, csum,
821 private);
Chris Masondb945352007-10-15 16:15:53 -0400822 memset(kaddr + offset, 1, end - start + 1);
823 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400824 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400825 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400826 if (private == 0)
827 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400828 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400829}
Chris Masonb888db22007-08-27 16:49:44 -0400830
Josef Bacik7b128762008-07-24 12:17:14 -0400831/*
832 * This creates an orphan entry for the given inode in case something goes
833 * wrong in the middle of an unlink/truncate.
834 */
835int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
836{
837 struct btrfs_root *root = BTRFS_I(inode)->root;
838 int ret = 0;
839
840 spin_lock(&root->orphan_lock);
841
842 /* already on the orphan list, we're good */
843 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
844 spin_unlock(&root->orphan_lock);
845 return 0;
846 }
847
848 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
849
850 spin_unlock(&root->orphan_lock);
851
852 /*
853 * insert an orphan item to track this unlinked/truncated file
854 */
855 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
856
857 return ret;
858}
859
860/*
861 * We have done the truncate/delete so we can go ahead and remove the orphan
862 * item for this particular inode.
863 */
864int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
865{
866 struct btrfs_root *root = BTRFS_I(inode)->root;
867 int ret = 0;
868
869 spin_lock(&root->orphan_lock);
870
871 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
872 spin_unlock(&root->orphan_lock);
873 return 0;
874 }
875
876 list_del_init(&BTRFS_I(inode)->i_orphan);
877 if (!trans) {
878 spin_unlock(&root->orphan_lock);
879 return 0;
880 }
881
882 spin_unlock(&root->orphan_lock);
883
884 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
885
886 return ret;
887}
888
889/*
890 * this cleans up any orphans that may be left on the list from the last use
891 * of this root.
892 */
893void btrfs_orphan_cleanup(struct btrfs_root *root)
894{
895 struct btrfs_path *path;
896 struct extent_buffer *leaf;
897 struct btrfs_item *item;
898 struct btrfs_key key, found_key;
899 struct btrfs_trans_handle *trans;
900 struct inode *inode;
901 int ret = 0, nr_unlink = 0, nr_truncate = 0;
902
903 /* don't do orphan cleanup if the fs is readonly. */
904 if (root->inode->i_sb->s_flags & MS_RDONLY)
905 return;
906
907 path = btrfs_alloc_path();
908 if (!path)
909 return;
910 path->reada = -1;
911
912 key.objectid = BTRFS_ORPHAN_OBJECTID;
913 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
914 key.offset = (u64)-1;
915
916 trans = btrfs_start_transaction(root, 1);
917 btrfs_set_trans_block_group(trans, root->inode);
918
919 while (1) {
920 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
921 if (ret < 0) {
922 printk(KERN_ERR "Error searching slot for orphan: %d"
923 "\n", ret);
924 break;
925 }
926
927 /*
928 * if ret == 0 means we found what we were searching for, which
929 * is weird, but possible, so only screw with path if we didnt
930 * find the key and see if we have stuff that matches
931 */
932 if (ret > 0) {
933 if (path->slots[0] == 0)
934 break;
935 path->slots[0]--;
936 }
937
938 /* pull out the item */
939 leaf = path->nodes[0];
940 item = btrfs_item_nr(leaf, path->slots[0]);
941 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
942
943 /* make sure the item matches what we want */
944 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
945 break;
946 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
947 break;
948
949 /* release the path since we're done with it */
950 btrfs_release_path(root, path);
951
952 /*
953 * this is where we are basically btrfs_lookup, without the
954 * crossing root thing. we store the inode number in the
955 * offset of the orphan item.
956 */
957 inode = btrfs_iget_locked(root->inode->i_sb,
958 found_key.offset, root);
959 if (!inode)
960 break;
961
962 if (inode->i_state & I_NEW) {
963 BTRFS_I(inode)->root = root;
964
965 /* have to set the location manually */
966 BTRFS_I(inode)->location.objectid = inode->i_ino;
967 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
968 BTRFS_I(inode)->location.offset = 0;
969
970 btrfs_read_locked_inode(inode);
971 unlock_new_inode(inode);
972 }
973
974 /*
975 * add this inode to the orphan list so btrfs_orphan_del does
976 * the proper thing when we hit it
977 */
978 spin_lock(&root->orphan_lock);
979 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
980 spin_unlock(&root->orphan_lock);
981
982 /*
983 * if this is a bad inode, means we actually succeeded in
984 * removing the inode, but not the orphan record, which means
985 * we need to manually delete the orphan since iput will just
986 * do a destroy_inode
987 */
988 if (is_bad_inode(inode)) {
989 btrfs_orphan_del(trans, inode);
990 iput(inode);
991 continue;
992 }
993
994 /* if we have links, this was a truncate, lets do that */
995 if (inode->i_nlink) {
996 nr_truncate++;
997 btrfs_truncate(inode);
998 } else {
999 nr_unlink++;
1000 }
1001
1002 /* this will do delete_inode and everything for us */
1003 iput(inode);
1004 }
1005
1006 if (nr_unlink)
1007 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1008 if (nr_truncate)
1009 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1010
1011 btrfs_free_path(path);
1012 btrfs_end_transaction(trans, root);
1013}
1014
Chris Mason39279cc2007-06-12 06:35:45 -04001015void btrfs_read_locked_inode(struct inode *inode)
1016{
1017 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001018 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001019 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001020 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001021 struct btrfs_root *root = BTRFS_I(inode)->root;
1022 struct btrfs_key location;
1023 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001024 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001025 int ret;
1026
1027 path = btrfs_alloc_path();
1028 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001029 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001030
Chris Mason39279cc2007-06-12 06:35:45 -04001031 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001033 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001034
Chris Mason5f39d392007-10-15 16:14:19 -04001035 leaf = path->nodes[0];
1036 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1037 struct btrfs_inode_item);
1038
1039 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1040 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1041 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1042 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001043 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001044
1045 tspec = btrfs_inode_atime(inode_item);
1046 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1047 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1048
1049 tspec = btrfs_inode_mtime(inode_item);
1050 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1051 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1052
1053 tspec = btrfs_inode_ctime(inode_item);
1054 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1055 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1056
1057 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1058 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001059 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001060 rdev = btrfs_inode_rdev(leaf, inode_item);
1061
Josef Bacikaec74772008-07-24 12:12:38 -04001062 BTRFS_I(inode)->index_cnt = (u64)-1;
1063
Chris Mason5f39d392007-10-15 16:14:19 -04001064 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001065 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1066 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001067 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001068 if (!BTRFS_I(inode)->block_group) {
1069 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001070 NULL, 0,
1071 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001072 }
Chris Mason39279cc2007-06-12 06:35:45 -04001073 btrfs_free_path(path);
1074 inode_item = NULL;
1075
Chris Mason39279cc2007-06-12 06:35:45 -04001076 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001077 case S_IFREG:
1078 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001079 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001080 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001081 inode->i_fop = &btrfs_file_operations;
1082 inode->i_op = &btrfs_file_inode_operations;
1083 break;
1084 case S_IFDIR:
1085 inode->i_fop = &btrfs_dir_file_operations;
1086 if (root == root->fs_info->tree_root)
1087 inode->i_op = &btrfs_dir_ro_inode_operations;
1088 else
1089 inode->i_op = &btrfs_dir_inode_operations;
1090 break;
1091 case S_IFLNK:
1092 inode->i_op = &btrfs_symlink_inode_operations;
1093 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001094 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001095 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001096 default:
1097 init_special_inode(inode, inode->i_mode, rdev);
1098 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001099 }
1100 return;
1101
1102make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001103 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001104 make_bad_inode(inode);
1105}
1106
Chris Mason5f39d392007-10-15 16:14:19 -04001107static void fill_inode_item(struct extent_buffer *leaf,
1108 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001109 struct inode *inode)
1110{
Chris Mason5f39d392007-10-15 16:14:19 -04001111 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1112 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001113 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001114 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1115 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1116
1117 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1118 inode->i_atime.tv_sec);
1119 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1120 inode->i_atime.tv_nsec);
1121
1122 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1123 inode->i_mtime.tv_sec);
1124 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1125 inode->i_mtime.tv_nsec);
1126
1127 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1128 inode->i_ctime.tv_sec);
1129 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1130 inode->i_ctime.tv_nsec);
1131
1132 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1133 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1134 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001135 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001136 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001137 BTRFS_I(inode)->block_group->key.objectid);
1138}
1139
Chris Masonba1da2f2008-07-17 12:54:15 -04001140int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001141 struct btrfs_root *root,
1142 struct inode *inode)
1143{
1144 struct btrfs_inode_item *inode_item;
1145 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001146 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001147 int ret;
1148
1149 path = btrfs_alloc_path();
1150 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001151 ret = btrfs_lookup_inode(trans, root, path,
1152 &BTRFS_I(inode)->location, 1);
1153 if (ret) {
1154 if (ret > 0)
1155 ret = -ENOENT;
1156 goto failed;
1157 }
1158
Chris Mason5f39d392007-10-15 16:14:19 -04001159 leaf = path->nodes[0];
1160 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001161 struct btrfs_inode_item);
1162
Chris Mason5f39d392007-10-15 16:14:19 -04001163 fill_inode_item(leaf, inode_item, inode);
1164 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001165 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001166 ret = 0;
1167failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001168 btrfs_free_path(path);
1169 return ret;
1170}
1171
1172
1173static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1174 struct btrfs_root *root,
1175 struct inode *dir,
1176 struct dentry *dentry)
1177{
1178 struct btrfs_path *path;
1179 const char *name = dentry->d_name.name;
1180 int name_len = dentry->d_name.len;
1181 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001182 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001183 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001184 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001185 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001186
1187 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001188 if (!path) {
1189 ret = -ENOMEM;
1190 goto err;
1191 }
1192
Chris Mason39279cc2007-06-12 06:35:45 -04001193 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1194 name, name_len, -1);
1195 if (IS_ERR(di)) {
1196 ret = PTR_ERR(di);
1197 goto err;
1198 }
1199 if (!di) {
1200 ret = -ENOENT;
1201 goto err;
1202 }
Chris Mason5f39d392007-10-15 16:14:19 -04001203 leaf = path->nodes[0];
1204 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001205 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001206 if (ret)
1207 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001208 btrfs_release_path(root, path);
1209
Josef Bacikaec74772008-07-24 12:12:38 -04001210 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1211 dentry->d_inode->i_ino,
1212 dentry->d_parent->d_inode->i_ino, &index);
1213 if (ret) {
1214 printk("failed to delete reference to %.*s, "
1215 "inode %lu parent %lu\n", name_len, name,
1216 dentry->d_inode->i_ino,
1217 dentry->d_parent->d_inode->i_ino);
1218 goto err;
1219 }
1220
Chris Mason39279cc2007-06-12 06:35:45 -04001221 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001222 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001223 if (IS_ERR(di)) {
1224 ret = PTR_ERR(di);
1225 goto err;
1226 }
1227 if (!di) {
1228 ret = -ENOENT;
1229 goto err;
1230 }
1231 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001232 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001233
1234 dentry->d_inode->i_ctime = dir->i_ctime;
1235err:
1236 btrfs_free_path(path);
1237 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001238 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001239 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001240 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001241#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1242 dentry->d_inode->i_nlink--;
1243#else
Chris Mason39279cc2007-06-12 06:35:45 -04001244 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001245#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001246 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001247 dir->i_sb->s_dirt = 1;
1248 }
1249 return ret;
1250}
1251
1252static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1253{
1254 struct btrfs_root *root;
1255 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001256 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001257 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001258 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001259
1260 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001261
1262 ret = btrfs_check_free_space(root, 1, 1);
1263 if (ret)
1264 goto fail;
1265
Chris Mason39279cc2007-06-12 06:35:45 -04001266 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001267
Chris Mason39279cc2007-06-12 06:35:45 -04001268 btrfs_set_trans_block_group(trans, dir);
1269 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001270
1271 if (inode->i_nlink == 0)
1272 ret = btrfs_orphan_add(trans, inode);
1273
Chris Masond3c2fdc2007-09-17 10:58:06 -04001274 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001275
Chris Mason89ce8a62008-06-25 16:01:31 -04001276 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001277fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001278 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001279 return ret;
1280}
1281
1282static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1283{
1284 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001285 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001286 int ret;
1287 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001288 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001289 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001290
Chris Mason925baed2008-06-25 16:01:30 -04001291 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001292 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001293 }
Yan134d4512007-10-25 15:49:25 -04001294
Chris Mason1832a6d2007-12-21 16:27:21 -05001295 ret = btrfs_check_free_space(root, 1, 1);
1296 if (ret)
1297 goto fail;
1298
Chris Mason39279cc2007-06-12 06:35:45 -04001299 trans = btrfs_start_transaction(root, 1);
1300 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001301
Josef Bacik7b128762008-07-24 12:17:14 -04001302 err = btrfs_orphan_add(trans, inode);
1303 if (err)
1304 goto fail_trans;
1305
Chris Mason39279cc2007-06-12 06:35:45 -04001306 /* now the directory is empty */
1307 err = btrfs_unlink_trans(trans, root, dir, dentry);
1308 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001309 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001310 }
Chris Mason39544012007-12-12 14:38:19 -05001311
Josef Bacik7b128762008-07-24 12:17:14 -04001312fail_trans:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001313 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001314 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001315fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001316 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001317
Chris Mason39279cc2007-06-12 06:35:45 -04001318 if (ret && !err)
1319 err = ret;
1320 return err;
1321}
1322
Chris Mason39279cc2007-06-12 06:35:45 -04001323/*
Chris Mason39279cc2007-06-12 06:35:45 -04001324 * this can truncate away extent items, csum items and directory items.
1325 * It starts at a high offset and removes keys until it can't find
1326 * any higher than i_size.
1327 *
1328 * csum items that cross the new i_size are truncated to the new size
1329 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001330 *
1331 * min_type is the minimum key type to truncate down to. If set to 0, this
1332 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001333 */
1334static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1335 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001336 struct inode *inode,
1337 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001338{
1339 int ret;
1340 struct btrfs_path *path;
1341 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001342 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001343 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001344 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001345 struct btrfs_file_extent_item *fi;
1346 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001347 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001348 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001349 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001350 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001351 int found_extent;
1352 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001353 int pending_del_nr = 0;
1354 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001355 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001356 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001357
Chris Mason3b951512008-04-17 11:29:12 -04001358 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001359 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001360 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001361 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001362
Chris Mason39279cc2007-06-12 06:35:45 -04001363 /* FIXME, add redo link to tree so we don't leak on crash */
1364 key.objectid = inode->i_ino;
1365 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001366 key.type = (u8)-1;
1367
Chris Mason85e21ba2008-01-29 15:11:36 -05001368 btrfs_init_path(path);
1369search_again:
1370 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1371 if (ret < 0) {
1372 goto error;
1373 }
1374 if (ret > 0) {
1375 BUG_ON(path->slots[0] == 0);
1376 path->slots[0]--;
1377 }
1378
Chris Mason39279cc2007-06-12 06:35:45 -04001379 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001380 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001381 leaf = path->nodes[0];
1382 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1383 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001384
Chris Mason5f39d392007-10-15 16:14:19 -04001385 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001386 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001387
Chris Mason85e21ba2008-01-29 15:11:36 -05001388 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001389 break;
1390
Chris Mason5f39d392007-10-15 16:14:19 -04001391 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001392 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001393 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001394 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001395 extent_type = btrfs_file_extent_type(leaf, fi);
1396 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001397 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001398 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001399 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1400 struct btrfs_item *item = btrfs_item_nr(leaf,
1401 path->slots[0]);
1402 item_end += btrfs_file_extent_inline_len(leaf,
1403 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001404 }
Yan008630c2007-11-07 13:31:09 -05001405 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001406 }
1407 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1408 ret = btrfs_csum_truncate(trans, root, path,
1409 inode->i_size);
1410 BUG_ON(ret);
1411 }
Yan008630c2007-11-07 13:31:09 -05001412 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001413 if (found_type == BTRFS_DIR_ITEM_KEY) {
1414 found_type = BTRFS_INODE_ITEM_KEY;
1415 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1416 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001417 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1418 found_type = BTRFS_XATTR_ITEM_KEY;
1419 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1420 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001421 } else if (found_type) {
1422 found_type--;
1423 } else {
1424 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001425 }
Yana61721d2007-09-17 11:08:38 -04001426 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001427 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001428 }
Chris Mason5f39d392007-10-15 16:14:19 -04001429 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001430 del_item = 1;
1431 else
1432 del_item = 0;
1433 found_extent = 0;
1434
1435 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001436 if (found_type != BTRFS_EXTENT_DATA_KEY)
1437 goto delete;
1438
1439 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001440 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001441 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001442 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001443 u64 orig_num_bytes =
1444 btrfs_file_extent_num_bytes(leaf, fi);
1445 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001446 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001447 extent_num_bytes = extent_num_bytes &
1448 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001449 btrfs_set_file_extent_num_bytes(leaf, fi,
1450 extent_num_bytes);
1451 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001452 extent_num_bytes);
1453 if (extent_start != 0)
1454 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001455 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001456 } else {
Chris Masondb945352007-10-15 16:15:53 -04001457 extent_num_bytes =
1458 btrfs_file_extent_disk_num_bytes(leaf,
1459 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001460 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001461 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001462 if (extent_start != 0) {
1463 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001464 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001465 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001466 root_gen = btrfs_header_generation(leaf);
1467 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001468 }
Chris Mason90692182008-02-08 13:49:28 -05001469 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1470 if (!del_item) {
1471 u32 newsize = inode->i_size - found_key.offset;
1472 dec_i_blocks(inode, item_end + 1 -
1473 found_key.offset - newsize);
1474 newsize =
1475 btrfs_file_extent_calc_inline_size(newsize);
1476 ret = btrfs_truncate_item(trans, root, path,
1477 newsize, 1);
1478 BUG_ON(ret);
1479 } else {
1480 dec_i_blocks(inode, item_end + 1 -
1481 found_key.offset);
1482 }
Chris Mason39279cc2007-06-12 06:35:45 -04001483 }
Chris Mason179e29e2007-11-01 11:28:41 -04001484delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001485 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001486 if (!pending_del_nr) {
1487 /* no pending yet, add ourselves */
1488 pending_del_slot = path->slots[0];
1489 pending_del_nr = 1;
1490 } else if (pending_del_nr &&
1491 path->slots[0] + 1 == pending_del_slot) {
1492 /* hop on the pending chunk */
1493 pending_del_nr++;
1494 pending_del_slot = path->slots[0];
1495 } else {
1496 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1497 }
Chris Mason39279cc2007-06-12 06:35:45 -04001498 } else {
1499 break;
1500 }
Chris Mason39279cc2007-06-12 06:35:45 -04001501 if (found_extent) {
1502 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001503 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001504 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001505 root_gen, inode->i_ino,
1506 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001507 BUG_ON(ret);
1508 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001509next:
1510 if (path->slots[0] == 0) {
1511 if (pending_del_nr)
1512 goto del_pending;
1513 btrfs_release_path(root, path);
1514 goto search_again;
1515 }
1516
1517 path->slots[0]--;
1518 if (pending_del_nr &&
1519 path->slots[0] + 1 != pending_del_slot) {
1520 struct btrfs_key debug;
1521del_pending:
1522 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1523 pending_del_slot);
1524 ret = btrfs_del_items(trans, root, path,
1525 pending_del_slot,
1526 pending_del_nr);
1527 BUG_ON(ret);
1528 pending_del_nr = 0;
1529 btrfs_release_path(root, path);
1530 goto search_again;
1531 }
Chris Mason39279cc2007-06-12 06:35:45 -04001532 }
1533 ret = 0;
1534error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001535 if (pending_del_nr) {
1536 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1537 pending_del_nr);
1538 }
Chris Mason39279cc2007-06-12 06:35:45 -04001539 btrfs_free_path(path);
1540 inode->i_sb->s_dirt = 1;
1541 return ret;
1542}
1543
Chris Masona52d9a82007-08-27 16:49:44 -04001544/*
1545 * taken from block_truncate_page, but does cow as it zeros out
1546 * any bytes left in the last page in the file.
1547 */
1548static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1549{
1550 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001551 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1553 struct btrfs_ordered_extent *ordered;
1554 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001555 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001556 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1557 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1558 struct page *page;
1559 int ret = 0;
1560 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001561 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001562
1563 if ((offset & (blocksize - 1)) == 0)
1564 goto out;
1565
1566 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001567again:
Chris Masona52d9a82007-08-27 16:49:44 -04001568 page = grab_cache_page(mapping, index);
1569 if (!page)
1570 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001571
1572 page_start = page_offset(page);
1573 page_end = page_start + PAGE_CACHE_SIZE - 1;
1574
Chris Masona52d9a82007-08-27 16:49:44 -04001575 if (!PageUptodate(page)) {
1576 ret = btrfs_readpage(NULL, page);
1577 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001578 if (page->mapping != mapping) {
1579 unlock_page(page);
1580 page_cache_release(page);
1581 goto again;
1582 }
Chris Masona52d9a82007-08-27 16:49:44 -04001583 if (!PageUptodate(page)) {
1584 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001585 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001586 }
1587 }
Chris Mason211c17f2008-05-15 09:13:45 -04001588 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001589
1590 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1591 set_page_extent_mapped(page);
1592
1593 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1594 if (ordered) {
1595 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1596 unlock_page(page);
1597 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001598 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001599 btrfs_put_ordered_extent(ordered);
1600 goto again;
1601 }
1602
1603 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1604 page_end, GFP_NOFS);
1605 ret = 0;
1606 if (offset != PAGE_CACHE_SIZE) {
1607 kaddr = kmap(page);
1608 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1609 flush_dcache_page(page);
1610 kunmap(page);
1611 }
Chris Mason247e7432008-07-17 12:53:51 -04001612 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001613 set_page_dirty(page);
1614 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001615
Chris Mason89642222008-07-24 09:41:53 -04001616out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001617 unlock_page(page);
1618 page_cache_release(page);
1619out:
1620 return ret;
1621}
1622
1623static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1624{
1625 struct inode *inode = dentry->d_inode;
1626 int err;
1627
1628 err = inode_change_ok(inode, attr);
1629 if (err)
1630 return err;
1631
1632 if (S_ISREG(inode->i_mode) &&
1633 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1634 struct btrfs_trans_handle *trans;
1635 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001636 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001637
Chris Mason5f39d392007-10-15 16:14:19 -04001638 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001639 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001640 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001641 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001642 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001643
Chris Mason1b0f7c22008-01-30 14:33:02 -05001644 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001645 goto out;
1646
Chris Mason1832a6d2007-12-21 16:27:21 -05001647 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001648 if (err)
1649 goto fail;
1650
Chris Mason39279cc2007-06-12 06:35:45 -04001651 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1652
Chris Mason5f564062008-01-22 16:47:59 -05001653 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001654 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1655 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001656
Chris Mason39279cc2007-06-12 06:35:45 -04001657 trans = btrfs_start_transaction(root, 1);
1658 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001659 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001660 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001661 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001662 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001663
Chris Mason179e29e2007-11-01 11:28:41 -04001664 if (alloc_hint != EXTENT_MAP_INLINE) {
1665 err = btrfs_insert_file_extent(trans, root,
1666 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001667 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001668 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001669 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001670 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001671 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001672 }
Chris Masonee6e6502008-07-17 12:54:40 -04001673 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001674 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001675 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001676 if (err)
1677 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001678 }
1679out:
1680 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001681
1682 if (!err && ((attr->ia_valid & ATTR_MODE)))
1683 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001684fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001685 return err;
1686}
Chris Mason61295eb2008-01-14 16:24:38 -05001687
Chris Mason39279cc2007-06-12 06:35:45 -04001688void btrfs_delete_inode(struct inode *inode)
1689{
1690 struct btrfs_trans_handle *trans;
1691 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001692 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001693 int ret;
1694
1695 truncate_inode_pages(&inode->i_data, 0);
1696 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001697 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001698 goto no_delete;
1699 }
Chris Mason4a096752008-07-21 10:29:44 -04001700 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001701
Chris Masondbe674a2008-07-17 12:54:05 -04001702 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001703 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001704
Chris Mason39279cc2007-06-12 06:35:45 -04001705 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001706 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001707 if (ret) {
1708 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001709 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001710 }
1711
1712 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001713
Chris Masond3c2fdc2007-09-17 10:58:06 -04001714 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001715 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001716
Chris Mason39279cc2007-06-12 06:35:45 -04001717 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001718 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001719 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001720
1721no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001722 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001723 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001724 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001725no_delete:
1726 clear_inode(inode);
1727}
1728
1729/*
1730 * this returns the key found in the dir entry in the location pointer.
1731 * If no dir entries were found, location->objectid is 0.
1732 */
1733static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1734 struct btrfs_key *location)
1735{
1736 const char *name = dentry->d_name.name;
1737 int namelen = dentry->d_name.len;
1738 struct btrfs_dir_item *di;
1739 struct btrfs_path *path;
1740 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001741 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001742
Chris Mason39544012007-12-12 14:38:19 -05001743 if (namelen == 1 && strcmp(name, ".") == 0) {
1744 location->objectid = dir->i_ino;
1745 location->type = BTRFS_INODE_ITEM_KEY;
1746 location->offset = 0;
1747 return 0;
1748 }
Chris Mason39279cc2007-06-12 06:35:45 -04001749 path = btrfs_alloc_path();
1750 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001751
Chris Mason7a720532007-12-13 09:06:59 -05001752 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001753 struct btrfs_key key;
1754 struct extent_buffer *leaf;
1755 u32 nritems;
1756 int slot;
1757
1758 key.objectid = dir->i_ino;
1759 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1760 key.offset = 0;
1761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1762 BUG_ON(ret == 0);
1763 ret = 0;
1764
1765 leaf = path->nodes[0];
1766 slot = path->slots[0];
1767 nritems = btrfs_header_nritems(leaf);
1768 if (slot >= nritems)
1769 goto out_err;
1770
1771 btrfs_item_key_to_cpu(leaf, &key, slot);
1772 if (key.objectid != dir->i_ino ||
1773 key.type != BTRFS_INODE_REF_KEY) {
1774 goto out_err;
1775 }
1776 location->objectid = key.offset;
1777 location->type = BTRFS_INODE_ITEM_KEY;
1778 location->offset = 0;
1779 goto out;
1780 }
1781
Chris Mason39279cc2007-06-12 06:35:45 -04001782 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1783 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001784 if (IS_ERR(di))
1785 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001786 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001787 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001788 }
Chris Mason5f39d392007-10-15 16:14:19 -04001789 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001790out:
Chris Mason39279cc2007-06-12 06:35:45 -04001791 btrfs_free_path(path);
1792 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001793out_err:
1794 location->objectid = 0;
1795 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001796}
1797
1798/*
1799 * when we hit a tree root in a directory, the btrfs part of the inode
1800 * needs to be changed to reflect the root directory of the tree root. This
1801 * is kind of like crossing a mount point.
1802 */
1803static int fixup_tree_root_location(struct btrfs_root *root,
1804 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001805 struct btrfs_root **sub_root,
1806 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001807{
Chris Mason39279cc2007-06-12 06:35:45 -04001808 struct btrfs_root_item *ri;
1809
1810 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1811 return 0;
1812 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1813 return 0;
1814
Josef Bacik58176a92007-08-29 15:47:34 -04001815 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1816 dentry->d_name.name,
1817 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001818 if (IS_ERR(*sub_root))
1819 return PTR_ERR(*sub_root);
1820
1821 ri = &(*sub_root)->root_item;
1822 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001823 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1824 location->offset = 0;
1825
Chris Mason39279cc2007-06-12 06:35:45 -04001826 return 0;
1827}
1828
1829static int btrfs_init_locked_inode(struct inode *inode, void *p)
1830{
1831 struct btrfs_iget_args *args = p;
1832 inode->i_ino = args->ino;
1833 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001834 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001835 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001836 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001837 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1838 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001839 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001840 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1841 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001842 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001843 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001844 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001845 return 0;
1846}
1847
1848static int btrfs_find_actor(struct inode *inode, void *opaque)
1849{
1850 struct btrfs_iget_args *args = opaque;
1851 return (args->ino == inode->i_ino &&
1852 args->root == BTRFS_I(inode)->root);
1853}
1854
Chris Masondc17ff82008-01-08 15:46:30 -05001855struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1856 u64 root_objectid)
1857{
1858 struct btrfs_iget_args args;
1859 args.ino = objectid;
1860 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1861
1862 if (!args.root)
1863 return NULL;
1864
1865 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1866}
1867
Chris Mason39279cc2007-06-12 06:35:45 -04001868struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1869 struct btrfs_root *root)
1870{
1871 struct inode *inode;
1872 struct btrfs_iget_args args;
1873 args.ino = objectid;
1874 args.root = root;
1875
1876 inode = iget5_locked(s, objectid, btrfs_find_actor,
1877 btrfs_init_locked_inode,
1878 (void *)&args);
1879 return inode;
1880}
1881
1882static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1883 struct nameidata *nd)
1884{
1885 struct inode * inode;
1886 struct btrfs_inode *bi = BTRFS_I(dir);
1887 struct btrfs_root *root = bi->root;
1888 struct btrfs_root *sub_root = root;
1889 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001890 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001891
1892 if (dentry->d_name.len > BTRFS_NAME_LEN)
1893 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001894
Chris Mason39279cc2007-06-12 06:35:45 -04001895 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001896
Chris Mason39279cc2007-06-12 06:35:45 -04001897 if (ret < 0)
1898 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001899
Chris Mason39279cc2007-06-12 06:35:45 -04001900 inode = NULL;
1901 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001902 ret = fixup_tree_root_location(root, &location, &sub_root,
1903 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001904 if (ret < 0)
1905 return ERR_PTR(ret);
1906 if (ret > 0)
1907 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001908
Chris Mason39279cc2007-06-12 06:35:45 -04001909 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1910 sub_root);
1911 if (!inode)
1912 return ERR_PTR(-EACCES);
1913 if (inode->i_state & I_NEW) {
1914 /* the inode and parent dir are two different roots */
1915 if (sub_root != root) {
1916 igrab(inode);
1917 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001918 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001919 }
1920 BTRFS_I(inode)->root = sub_root;
1921 memcpy(&BTRFS_I(inode)->location, &location,
1922 sizeof(location));
1923 btrfs_read_locked_inode(inode);
1924 unlock_new_inode(inode);
1925 }
1926 }
Josef Bacik7b128762008-07-24 12:17:14 -04001927
1928 if (unlikely(do_orphan))
1929 btrfs_orphan_cleanup(sub_root);
1930
Chris Mason39279cc2007-06-12 06:35:45 -04001931 return d_splice_alias(inode, dentry);
1932}
1933
Chris Mason39279cc2007-06-12 06:35:45 -04001934static unsigned char btrfs_filetype_table[] = {
1935 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1936};
1937
1938static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1939{
Chris Mason6da6aba2007-12-18 16:15:09 -05001940 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001941 struct btrfs_root *root = BTRFS_I(inode)->root;
1942 struct btrfs_item *item;
1943 struct btrfs_dir_item *di;
1944 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001945 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001946 struct btrfs_path *path;
1947 int ret;
1948 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001949 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001950 int slot;
1951 int advance;
1952 unsigned char d_type;
1953 int over = 0;
1954 u32 di_cur;
1955 u32 di_total;
1956 u32 di_len;
1957 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001958 char tmp_name[32];
1959 char *name_ptr;
1960 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001961
1962 /* FIXME, use a real flag for deciding about the key type */
1963 if (root->fs_info->tree_root == root)
1964 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001965
Chris Mason39544012007-12-12 14:38:19 -05001966 /* special case for "." */
1967 if (filp->f_pos == 0) {
1968 over = filldir(dirent, ".", 1,
1969 1, inode->i_ino,
1970 DT_DIR);
1971 if (over)
1972 return 0;
1973 filp->f_pos = 1;
1974 }
1975
Chris Mason39279cc2007-06-12 06:35:45 -04001976 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001977 path = btrfs_alloc_path();
1978 path->reada = 2;
1979
1980 /* special case for .., just use the back ref */
1981 if (filp->f_pos == 1) {
1982 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1983 key.offset = 0;
1984 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1985 BUG_ON(ret == 0);
1986 leaf = path->nodes[0];
1987 slot = path->slots[0];
1988 nritems = btrfs_header_nritems(leaf);
1989 if (slot >= nritems) {
1990 btrfs_release_path(root, path);
1991 goto read_dir_items;
1992 }
1993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1994 btrfs_release_path(root, path);
1995 if (found_key.objectid != key.objectid ||
1996 found_key.type != BTRFS_INODE_REF_KEY)
1997 goto read_dir_items;
1998 over = filldir(dirent, "..", 2,
1999 2, found_key.offset, DT_DIR);
2000 if (over)
2001 goto nopos;
2002 filp->f_pos = 2;
2003 }
2004
2005read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04002006 btrfs_set_key_type(&key, key_type);
2007 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04002008
Chris Mason39279cc2007-06-12 06:35:45 -04002009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2010 if (ret < 0)
2011 goto err;
2012 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002013 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002014 leaf = path->nodes[0];
2015 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002016 slot = path->slots[0];
2017 if (advance || slot >= nritems) {
2018 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002019 ret = btrfs_next_leaf(root, path);
2020 if (ret)
2021 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002022 leaf = path->nodes[0];
2023 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002024 slot = path->slots[0];
2025 } else {
2026 slot++;
2027 path->slots[0]++;
2028 }
2029 }
2030 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002031 item = btrfs_item_nr(leaf, slot);
2032 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2033
2034 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002035 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002036 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002037 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002039 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002040
2041 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002042 advance = 1;
2043 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2044 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002045 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04002046 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002047 struct btrfs_key location;
2048
2049 name_len = btrfs_dir_name_len(leaf, di);
2050 if (name_len < 32) {
2051 name_ptr = tmp_name;
2052 } else {
2053 name_ptr = kmalloc(name_len, GFP_NOFS);
2054 BUG_ON(!name_ptr);
2055 }
2056 read_extent_buffer(leaf, name_ptr,
2057 (unsigned long)(di + 1), name_len);
2058
2059 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2060 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 over = filldir(dirent, name_ptr, name_len,
2062 found_key.offset,
2063 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002064 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002065
2066 if (name_ptr != tmp_name)
2067 kfree(name_ptr);
2068
Chris Mason39279cc2007-06-12 06:35:45 -04002069 if (over)
2070 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002071 di_len = btrfs_dir_name_len(leaf, di) +
2072 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002073 di_cur += di_len;
2074 di = (struct btrfs_dir_item *)((char *)di + di_len);
2075 }
2076 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002077 if (key_type == BTRFS_DIR_INDEX_KEY)
2078 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2079 else
2080 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002081nopos:
2082 ret = 0;
2083err:
Chris Mason39279cc2007-06-12 06:35:45 -04002084 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002085 return ret;
2086}
2087
2088int btrfs_write_inode(struct inode *inode, int wait)
2089{
2090 struct btrfs_root *root = BTRFS_I(inode)->root;
2091 struct btrfs_trans_handle *trans;
2092 int ret = 0;
2093
2094 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002095 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002096 btrfs_set_trans_block_group(trans, inode);
2097 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002098 }
2099 return ret;
2100}
2101
2102/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002103 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002104 * inode changes. But, it is most likely to find the inode in cache.
2105 * FIXME, needs more benchmarking...there are no reasons other than performance
2106 * to keep or drop this code.
2107 */
2108void btrfs_dirty_inode(struct inode *inode)
2109{
2110 struct btrfs_root *root = BTRFS_I(inode)->root;
2111 struct btrfs_trans_handle *trans;
2112
Chris Masonf9295742008-07-17 12:54:14 -04002113 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002114 btrfs_set_trans_block_group(trans, inode);
2115 btrfs_update_inode(trans, root, inode);
2116 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002117}
2118
Josef Bacikaec74772008-07-24 12:12:38 -04002119static int btrfs_set_inode_index_count(struct inode *inode)
2120{
2121 struct btrfs_root *root = BTRFS_I(inode)->root;
2122 struct btrfs_key key, found_key;
2123 struct btrfs_path *path;
2124 struct extent_buffer *leaf;
2125 int ret;
2126
2127 key.objectid = inode->i_ino;
2128 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2129 key.offset = (u64)-1;
2130
2131 path = btrfs_alloc_path();
2132 if (!path)
2133 return -ENOMEM;
2134
2135 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2136 if (ret < 0)
2137 goto out;
2138 /* FIXME: we should be able to handle this */
2139 if (ret == 0)
2140 goto out;
2141 ret = 0;
2142
2143 /*
2144 * MAGIC NUMBER EXPLANATION:
2145 * since we search a directory based on f_pos we have to start at 2
2146 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2147 * else has to start at 2
2148 */
2149 if (path->slots[0] == 0) {
2150 BTRFS_I(inode)->index_cnt = 2;
2151 goto out;
2152 }
2153
2154 path->slots[0]--;
2155
2156 leaf = path->nodes[0];
2157 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2158
2159 if (found_key.objectid != inode->i_ino ||
2160 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2161 BTRFS_I(inode)->index_cnt = 2;
2162 goto out;
2163 }
2164
2165 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2166out:
2167 btrfs_free_path(path);
2168 return ret;
2169}
2170
2171static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2172{
2173 int ret = 0;
2174
2175 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2176 ret = btrfs_set_inode_index_count(dir);
2177 if (ret)
2178 return ret;
2179 }
2180
2181 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2182 BTRFS_I(dir)->index_cnt++;
2183
2184 return ret;
2185}
2186
Chris Mason39279cc2007-06-12 06:35:45 -04002187static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2188 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002189 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002190 const char *name, int name_len,
2191 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002192 u64 objectid,
2193 struct btrfs_block_group_cache *group,
2194 int mode)
2195{
2196 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002197 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002198 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002199 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002200 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002201 struct btrfs_inode_ref *ref;
2202 struct btrfs_key key[2];
2203 u32 sizes[2];
2204 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002205 int ret;
2206 int owner;
2207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 path = btrfs_alloc_path();
2209 BUG_ON(!path);
2210
Chris Mason39279cc2007-06-12 06:35:45 -04002211 inode = new_inode(root->fs_info->sb);
2212 if (!inode)
2213 return ERR_PTR(-ENOMEM);
2214
Josef Bacikaec74772008-07-24 12:12:38 -04002215 if (dir) {
2216 ret = btrfs_set_inode_index(dir, inode);
2217 if (ret)
2218 return ERR_PTR(ret);
2219 } else {
2220 BTRFS_I(inode)->index = 0;
2221 }
2222 /*
2223 * index_cnt is ignored for everything but a dir,
2224 * btrfs_get_inode_index_count has an explanation for the magic
2225 * number
2226 */
2227 BTRFS_I(inode)->index_cnt = 2;
2228
Chris Masond1310b22008-01-24 16:13:08 -05002229 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2230 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002231 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002232 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2233 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002234 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002235 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002236 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002237 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002238 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002239 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002240
Chris Mason39279cc2007-06-12 06:35:45 -04002241 if (mode & S_IFDIR)
2242 owner = 0;
2243 else
2244 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002245 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002246 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002247 if (!new_inode_group) {
2248 printk("find_block group failed\n");
2249 new_inode_group = group;
2250 }
2251 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002252 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002253
2254 key[0].objectid = objectid;
2255 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2256 key[0].offset = 0;
2257
2258 key[1].objectid = objectid;
2259 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2260 key[1].offset = ref_objectid;
2261
2262 sizes[0] = sizeof(struct btrfs_inode_item);
2263 sizes[1] = name_len + sizeof(*ref);
2264
2265 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2266 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002267 goto fail;
2268
Chris Mason9c583092008-01-29 15:15:18 -05002269 if (objectid > root->highest_inode)
2270 root->highest_inode = objectid;
2271
Chris Mason39279cc2007-06-12 06:35:45 -04002272 inode->i_uid = current->fsuid;
2273 inode->i_gid = current->fsgid;
2274 inode->i_mode = mode;
2275 inode->i_ino = objectid;
2276 inode->i_blocks = 0;
2277 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002278 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2279 struct btrfs_inode_item);
2280 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002281
2282 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2283 struct btrfs_inode_ref);
2284 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002285 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002286 ptr = (unsigned long)(ref + 1);
2287 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2288
Chris Mason5f39d392007-10-15 16:14:19 -04002289 btrfs_mark_buffer_dirty(path->nodes[0]);
2290 btrfs_free_path(path);
2291
Chris Mason39279cc2007-06-12 06:35:45 -04002292 location = &BTRFS_I(inode)->location;
2293 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002294 location->offset = 0;
2295 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2296
Chris Mason39279cc2007-06-12 06:35:45 -04002297 insert_inode_hash(inode);
2298 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002299fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002300 if (dir)
2301 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002302 btrfs_free_path(path);
2303 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002304}
2305
2306static inline u8 btrfs_inode_type(struct inode *inode)
2307{
2308 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2309}
2310
2311static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002312 struct dentry *dentry, struct inode *inode,
2313 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002314{
2315 int ret;
2316 struct btrfs_key key;
2317 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002318 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002319
Chris Mason39279cc2007-06-12 06:35:45 -04002320 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002321 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2322 key.offset = 0;
2323
2324 ret = btrfs_insert_dir_item(trans, root,
2325 dentry->d_name.name, dentry->d_name.len,
2326 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002327 &key, btrfs_inode_type(inode),
2328 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002329 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002330 if (add_backref) {
2331 ret = btrfs_insert_inode_ref(trans, root,
2332 dentry->d_name.name,
2333 dentry->d_name.len,
2334 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002335 parent_inode->i_ino,
2336 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002337 }
Chris Masondbe674a2008-07-17 12:54:05 -04002338 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2339 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002340 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002341 ret = btrfs_update_inode(trans, root,
2342 dentry->d_parent->d_inode);
2343 }
2344 return ret;
2345}
2346
2347static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002348 struct dentry *dentry, struct inode *inode,
2349 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002350{
Chris Mason9c583092008-01-29 15:15:18 -05002351 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002352 if (!err) {
2353 d_instantiate(dentry, inode);
2354 return 0;
2355 }
2356 if (err > 0)
2357 err = -EEXIST;
2358 return err;
2359}
2360
Josef Bacik618e21d2007-07-11 10:18:17 -04002361static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2362 int mode, dev_t rdev)
2363{
2364 struct btrfs_trans_handle *trans;
2365 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002366 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002367 int err;
2368 int drop_inode = 0;
2369 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002370 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002371
2372 if (!new_valid_dev(rdev))
2373 return -EINVAL;
2374
Chris Mason1832a6d2007-12-21 16:27:21 -05002375 err = btrfs_check_free_space(root, 1, 0);
2376 if (err)
2377 goto fail;
2378
Josef Bacik618e21d2007-07-11 10:18:17 -04002379 trans = btrfs_start_transaction(root, 1);
2380 btrfs_set_trans_block_group(trans, dir);
2381
2382 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2383 if (err) {
2384 err = -ENOSPC;
2385 goto out_unlock;
2386 }
2387
Josef Bacikaec74772008-07-24 12:12:38 -04002388 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002389 dentry->d_name.len,
2390 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002391 BTRFS_I(dir)->block_group, mode);
2392 err = PTR_ERR(inode);
2393 if (IS_ERR(inode))
2394 goto out_unlock;
2395
Josef Bacik33268ea2008-07-24 12:16:36 -04002396 err = btrfs_init_acl(inode, dir);
2397 if (err) {
2398 drop_inode = 1;
2399 goto out_unlock;
2400 }
2401
Josef Bacik618e21d2007-07-11 10:18:17 -04002402 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002403 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002404 if (err)
2405 drop_inode = 1;
2406 else {
2407 inode->i_op = &btrfs_special_inode_operations;
2408 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002409 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002410 }
2411 dir->i_sb->s_dirt = 1;
2412 btrfs_update_inode_block_group(trans, inode);
2413 btrfs_update_inode_block_group(trans, dir);
2414out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002415 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002416 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002417fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002418 if (drop_inode) {
2419 inode_dec_link_count(inode);
2420 iput(inode);
2421 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002422 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002423 return err;
2424}
2425
Chris Mason39279cc2007-06-12 06:35:45 -04002426static int btrfs_create(struct inode *dir, struct dentry *dentry,
2427 int mode, struct nameidata *nd)
2428{
2429 struct btrfs_trans_handle *trans;
2430 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002431 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002432 int err;
2433 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002434 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002435 u64 objectid;
2436
Chris Mason1832a6d2007-12-21 16:27:21 -05002437 err = btrfs_check_free_space(root, 1, 0);
2438 if (err)
2439 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002440 trans = btrfs_start_transaction(root, 1);
2441 btrfs_set_trans_block_group(trans, dir);
2442
2443 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2444 if (err) {
2445 err = -ENOSPC;
2446 goto out_unlock;
2447 }
2448
Josef Bacikaec74772008-07-24 12:12:38 -04002449 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002450 dentry->d_name.len,
2451 dentry->d_parent->d_inode->i_ino,
2452 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002453 err = PTR_ERR(inode);
2454 if (IS_ERR(inode))
2455 goto out_unlock;
2456
Josef Bacik33268ea2008-07-24 12:16:36 -04002457 err = btrfs_init_acl(inode, dir);
2458 if (err) {
2459 drop_inode = 1;
2460 goto out_unlock;
2461 }
2462
Chris Mason39279cc2007-06-12 06:35:45 -04002463 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002464 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002465 if (err)
2466 drop_inode = 1;
2467 else {
2468 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002469 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002470 inode->i_fop = &btrfs_file_operations;
2471 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002472 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2473 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002474 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002475 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2476 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002477 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002478 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002479 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002480 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002481 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002482 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002483 }
2484 dir->i_sb->s_dirt = 1;
2485 btrfs_update_inode_block_group(trans, inode);
2486 btrfs_update_inode_block_group(trans, dir);
2487out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002488 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002489 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002490fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002491 if (drop_inode) {
2492 inode_dec_link_count(inode);
2493 iput(inode);
2494 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002495 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002496 return err;
2497}
2498
2499static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2500 struct dentry *dentry)
2501{
2502 struct btrfs_trans_handle *trans;
2503 struct btrfs_root *root = BTRFS_I(dir)->root;
2504 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002505 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002506 int err;
2507 int drop_inode = 0;
2508
2509 if (inode->i_nlink == 0)
2510 return -ENOENT;
2511
Chris Mason6da6aba2007-12-18 16:15:09 -05002512#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2513 inode->i_nlink++;
2514#else
Chris Mason39279cc2007-06-12 06:35:45 -04002515 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002516#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002517 err = btrfs_check_free_space(root, 1, 0);
2518 if (err)
2519 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002520 err = btrfs_set_inode_index(dir, inode);
2521 if (err)
2522 goto fail;
2523
Chris Mason39279cc2007-06-12 06:35:45 -04002524 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002525
Chris Mason39279cc2007-06-12 06:35:45 -04002526 btrfs_set_trans_block_group(trans, dir);
2527 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002528
Chris Mason9c583092008-01-29 15:15:18 -05002529 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002530
Chris Mason39279cc2007-06-12 06:35:45 -04002531 if (err)
2532 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002533
Chris Mason39279cc2007-06-12 06:35:45 -04002534 dir->i_sb->s_dirt = 1;
2535 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002536 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002537
Chris Mason54aa1f42007-06-22 14:16:25 -04002538 if (err)
2539 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002540
Chris Masond3c2fdc2007-09-17 10:58:06 -04002541 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002542 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002543fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002544 if (drop_inode) {
2545 inode_dec_link_count(inode);
2546 iput(inode);
2547 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002548 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002549 return err;
2550}
2551
Chris Mason39279cc2007-06-12 06:35:45 -04002552static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2553{
Chris Masonb9d86662008-05-02 16:13:49 -04002554 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002555 struct btrfs_trans_handle *trans;
2556 struct btrfs_root *root = BTRFS_I(dir)->root;
2557 int err = 0;
2558 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002559 u64 objectid = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002560 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002561
Chris Mason1832a6d2007-12-21 16:27:21 -05002562 err = btrfs_check_free_space(root, 1, 0);
2563 if (err)
2564 goto out_unlock;
2565
Chris Mason39279cc2007-06-12 06:35:45 -04002566 trans = btrfs_start_transaction(root, 1);
2567 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002568
Chris Mason39279cc2007-06-12 06:35:45 -04002569 if (IS_ERR(trans)) {
2570 err = PTR_ERR(trans);
2571 goto out_unlock;
2572 }
2573
2574 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2575 if (err) {
2576 err = -ENOSPC;
2577 goto out_unlock;
2578 }
2579
Josef Bacikaec74772008-07-24 12:12:38 -04002580 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002581 dentry->d_name.len,
2582 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002583 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2584 if (IS_ERR(inode)) {
2585 err = PTR_ERR(inode);
2586 goto out_fail;
2587 }
Chris Mason5f39d392007-10-15 16:14:19 -04002588
Chris Mason39279cc2007-06-12 06:35:45 -04002589 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002590
2591 err = btrfs_init_acl(inode, dir);
2592 if (err)
2593 goto out_fail;
2594
Chris Mason39279cc2007-06-12 06:35:45 -04002595 inode->i_op = &btrfs_dir_inode_operations;
2596 inode->i_fop = &btrfs_dir_file_operations;
2597 btrfs_set_trans_block_group(trans, inode);
2598
Chris Masondbe674a2008-07-17 12:54:05 -04002599 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002600 err = btrfs_update_inode(trans, root, inode);
2601 if (err)
2602 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002603
Chris Mason9c583092008-01-29 15:15:18 -05002604 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002605 if (err)
2606 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002607
Chris Mason39279cc2007-06-12 06:35:45 -04002608 d_instantiate(dentry, inode);
2609 drop_on_err = 0;
2610 dir->i_sb->s_dirt = 1;
2611 btrfs_update_inode_block_group(trans, inode);
2612 btrfs_update_inode_block_group(trans, dir);
2613
2614out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002615 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002616 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002617
Chris Mason39279cc2007-06-12 06:35:45 -04002618out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002619 if (drop_on_err)
2620 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002621 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002622 return err;
2623}
2624
Chris Mason3b951512008-04-17 11:29:12 -04002625static int merge_extent_mapping(struct extent_map_tree *em_tree,
2626 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002627 struct extent_map *em,
2628 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002629{
2630 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002631
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2633 start_diff = map_start - em->start;
2634 em->start = map_start;
2635 em->len = map_len;
2636 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2637 em->block_start += start_diff;
2638 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002639}
2640
Chris Masona52d9a82007-08-27 16:49:44 -04002641struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002642 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002643 int create)
2644{
2645 int ret;
2646 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002647 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002648 u64 extent_start = 0;
2649 u64 extent_end = 0;
2650 u64 objectid = inode->i_ino;
2651 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002652 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002653 struct btrfs_root *root = BTRFS_I(inode)->root;
2654 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002655 struct extent_buffer *leaf;
2656 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002657 struct extent_map *em = NULL;
2658 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002659 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002660 struct btrfs_trans_handle *trans = NULL;
2661
Chris Masona52d9a82007-08-27 16:49:44 -04002662again:
Chris Masond1310b22008-01-24 16:13:08 -05002663 spin_lock(&em_tree->lock);
2664 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002665 if (em)
2666 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002667 spin_unlock(&em_tree->lock);
2668
Chris Masona52d9a82007-08-27 16:49:44 -04002669 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002670 if (em->start > start || em->start + em->len <= start)
2671 free_extent_map(em);
2672 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002673 free_extent_map(em);
2674 else
2675 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002676 }
Chris Masond1310b22008-01-24 16:13:08 -05002677 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002678 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002679 err = -ENOMEM;
2680 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002681 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002682 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002683 em->start = EXTENT_MAP_HOLE;
2684 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002685
2686 if (!path) {
2687 path = btrfs_alloc_path();
2688 BUG_ON(!path);
2689 }
2690
Chris Mason179e29e2007-11-01 11:28:41 -04002691 ret = btrfs_lookup_file_extent(trans, root, path,
2692 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002693 if (ret < 0) {
2694 err = ret;
2695 goto out;
2696 }
2697
2698 if (ret != 0) {
2699 if (path->slots[0] == 0)
2700 goto not_found;
2701 path->slots[0]--;
2702 }
2703
Chris Mason5f39d392007-10-15 16:14:19 -04002704 leaf = path->nodes[0];
2705 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002706 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002707 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2709 found_type = btrfs_key_type(&found_key);
2710 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002711 found_type != BTRFS_EXTENT_DATA_KEY) {
2712 goto not_found;
2713 }
2714
Chris Mason5f39d392007-10-15 16:14:19 -04002715 found_type = btrfs_file_extent_type(leaf, item);
2716 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002717 if (found_type == BTRFS_FILE_EXTENT_REG) {
2718 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002719 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002720 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002721 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002722 em->start = start;
2723 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002724 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002725 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002726 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002727 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002728 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002729 }
2730 goto not_found_em;
2731 }
Chris Masondb945352007-10-15 16:15:53 -04002732 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2733 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002734 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002735 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002736 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002737 goto insert;
2738 }
Chris Masondb945352007-10-15 16:15:53 -04002739 bytenr += btrfs_file_extent_offset(leaf, item);
2740 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002741 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002742 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002743 goto insert;
2744 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002745 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002746 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002747 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002748 size_t size;
2749 size_t extent_offset;
2750 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002751
Chris Mason5f39d392007-10-15 16:14:19 -04002752 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2753 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002754 extent_end = (extent_start + size + root->sectorsize - 1) &
2755 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002756 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002757 em->start = start;
2758 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002759 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002760 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002761 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002762 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002763 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002764 }
2765 goto not_found_em;
2766 }
2767 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002768
2769 if (!page) {
2770 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002771 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002772 goto out;
2773 }
2774
Chris Mason70dec802008-01-29 09:59:12 -05002775 page_start = page_offset(page) + pg_offset;
2776 extent_offset = page_start - extent_start;
2777 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002778 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002779 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002780 em->len = (copy_size + root->sectorsize - 1) &
2781 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002782 map = kmap(page);
2783 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002784 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002785 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002786 copy_size);
2787 flush_dcache_page(page);
2788 } else if (create && PageUptodate(page)) {
2789 if (!trans) {
2790 kunmap(page);
2791 free_extent_map(em);
2792 em = NULL;
2793 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002794 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002795 goto again;
2796 }
Chris Mason70dec802008-01-29 09:59:12 -05002797 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002798 copy_size);
2799 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002800 }
Chris Masona52d9a82007-08-27 16:49:44 -04002801 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002802 set_extent_uptodate(io_tree, em->start,
2803 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002804 goto insert;
2805 } else {
2806 printk("unkknown found_type %d\n", found_type);
2807 WARN_ON(1);
2808 }
2809not_found:
2810 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002811 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002812not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002813 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002814insert:
2815 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002816 if (em->start > start || extent_map_end(em) <= start) {
2817 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002818 err = -EIO;
2819 goto out;
2820 }
Chris Masond1310b22008-01-24 16:13:08 -05002821
2822 err = 0;
2823 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002824 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002825 /* it is possible that someone inserted the extent into the tree
2826 * while we had the lock dropped. It is also possible that
2827 * an overlapping map exists in the tree
2828 */
Chris Masona52d9a82007-08-27 16:49:44 -04002829 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002830 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002831
2832 ret = 0;
2833
Chris Mason3b951512008-04-17 11:29:12 -04002834 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002835 if (existing && (existing->start > start ||
2836 existing->start + existing->len <= start)) {
2837 free_extent_map(existing);
2838 existing = NULL;
2839 }
Chris Mason3b951512008-04-17 11:29:12 -04002840 if (!existing) {
2841 existing = lookup_extent_mapping(em_tree, em->start,
2842 em->len);
2843 if (existing) {
2844 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002845 em, start,
2846 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002847 free_extent_map(existing);
2848 if (err) {
2849 free_extent_map(em);
2850 em = NULL;
2851 }
2852 } else {
2853 err = -EIO;
2854 printk("failing to insert %Lu %Lu\n",
2855 start, len);
2856 free_extent_map(em);
2857 em = NULL;
2858 }
2859 } else {
2860 free_extent_map(em);
2861 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002862 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002863 }
Chris Masona52d9a82007-08-27 16:49:44 -04002864 }
Chris Masond1310b22008-01-24 16:13:08 -05002865 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002866out:
Chris Masonf4219502008-07-22 11:18:09 -04002867 if (path)
2868 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002869 if (trans) {
2870 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002871 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002872 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002873 }
Chris Masona52d9a82007-08-27 16:49:44 -04002874 }
Chris Masona52d9a82007-08-27 16:49:44 -04002875 if (err) {
2876 free_extent_map(em);
2877 WARN_ON(1);
2878 return ERR_PTR(err);
2879 }
2880 return em;
2881}
2882
Chris Masone1c4b742008-04-22 13:26:46 -04002883#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002884static int btrfs_get_block(struct inode *inode, sector_t iblock,
2885 struct buffer_head *bh_result, int create)
2886{
2887 struct extent_map *em;
2888 u64 start = (u64)iblock << inode->i_blkbits;
2889 struct btrfs_multi_bio *multi = NULL;
2890 struct btrfs_root *root = BTRFS_I(inode)->root;
2891 u64 len;
2892 u64 logical;
2893 u64 map_length;
2894 int ret = 0;
2895
2896 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2897
2898 if (!em || IS_ERR(em))
2899 goto out;
2900
Chris Masone1c4b742008-04-22 13:26:46 -04002901 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002902 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002903 }
Chris Mason16432982008-04-10 10:23:21 -04002904
2905 if (em->block_start == EXTENT_MAP_INLINE) {
2906 ret = -EINVAL;
2907 goto out;
2908 }
2909
Chris Mason16432982008-04-10 10:23:21 -04002910 len = em->start + em->len - start;
2911 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2912
Chris Masone1c4b742008-04-22 13:26:46 -04002913 if (em->block_start == EXTENT_MAP_HOLE ||
2914 em->block_start == EXTENT_MAP_DELALLOC) {
2915 bh_result->b_size = len;
2916 goto out;
2917 }
2918
Chris Mason16432982008-04-10 10:23:21 -04002919 logical = start - em->start;
2920 logical = em->block_start + logical;
2921
2922 map_length = len;
2923 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2924 logical, &map_length, &multi, 0);
2925 BUG_ON(ret);
2926 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2927 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002928
Chris Mason16432982008-04-10 10:23:21 -04002929 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2930 set_buffer_mapped(bh_result);
2931 kfree(multi);
2932out:
2933 free_extent_map(em);
2934 return ret;
2935}
Chris Masone1c4b742008-04-22 13:26:46 -04002936#endif
Chris Mason16432982008-04-10 10:23:21 -04002937
2938static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2939 const struct iovec *iov, loff_t offset,
2940 unsigned long nr_segs)
2941{
Chris Masone1c4b742008-04-22 13:26:46 -04002942 return -EINVAL;
2943#if 0
Chris Mason16432982008-04-10 10:23:21 -04002944 struct file *file = iocb->ki_filp;
2945 struct inode *inode = file->f_mapping->host;
2946
2947 if (rw == WRITE)
2948 return -EINVAL;
2949
2950 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2951 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002952#endif
Chris Mason16432982008-04-10 10:23:21 -04002953}
2954
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002955static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002956{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002957 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002958}
2959
Chris Mason9ebefb182007-06-15 13:50:00 -04002960int btrfs_readpage(struct file *file, struct page *page)
2961{
Chris Masond1310b22008-01-24 16:13:08 -05002962 struct extent_io_tree *tree;
2963 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002964 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002965}
Chris Mason1832a6d2007-12-21 16:27:21 -05002966
Chris Mason39279cc2007-06-12 06:35:45 -04002967static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2968{
Chris Masond1310b22008-01-24 16:13:08 -05002969 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002970
2971
2972 if (current->flags & PF_MEMALLOC) {
2973 redirty_page_for_writepage(wbc, page);
2974 unlock_page(page);
2975 return 0;
2976 }
Chris Masond1310b22008-01-24 16:13:08 -05002977 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002978 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2979}
Chris Mason39279cc2007-06-12 06:35:45 -04002980
Chris Masonf4219502008-07-22 11:18:09 -04002981int btrfs_writepages(struct address_space *mapping,
2982 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002983{
Chris Masond1310b22008-01-24 16:13:08 -05002984 struct extent_io_tree *tree;
2985 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002986 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2987}
2988
Chris Mason3ab2fb52007-11-08 10:59:22 -05002989static int
2990btrfs_readpages(struct file *file, struct address_space *mapping,
2991 struct list_head *pages, unsigned nr_pages)
2992{
Chris Masond1310b22008-01-24 16:13:08 -05002993 struct extent_io_tree *tree;
2994 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002995 return extent_readpages(tree, mapping, pages, nr_pages,
2996 btrfs_get_extent);
2997}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002998static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002999{
Chris Masond1310b22008-01-24 16:13:08 -05003000 struct extent_io_tree *tree;
3001 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003002 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003003
Chris Masond1310b22008-01-24 16:13:08 -05003004 tree = &BTRFS_I(page->mapping->host)->io_tree;
3005 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003006 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003007 if (ret == 1) {
3008 ClearPagePrivate(page);
3009 set_page_private(page, 0);
3010 page_cache_release(page);
3011 }
3012 return ret;
3013}
Chris Mason39279cc2007-06-12 06:35:45 -04003014
Chris Masone6dcd2d2008-07-17 12:53:50 -04003015static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3016{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003017 return __btrfs_releasepage(page, gfp_flags);
3018}
3019
Chris Masona52d9a82007-08-27 16:49:44 -04003020static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3021{
Chris Masond1310b22008-01-24 16:13:08 -05003022 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023 struct btrfs_ordered_extent *ordered;
3024 u64 page_start = page_offset(page);
3025 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003026
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003028 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003029 if (offset) {
3030 btrfs_releasepage(page, GFP_NOFS);
3031 return;
3032 }
3033
3034 lock_extent(tree, page_start, page_end, GFP_NOFS);
3035 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3036 page_offset(page));
3037 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003038 /*
3039 * IO on this page will never be started, so we need
3040 * to account for any ordered extents now
3041 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003042 clear_extent_bit(tree, page_start, page_end,
3043 EXTENT_DIRTY | EXTENT_DELALLOC |
3044 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003045 btrfs_finish_ordered_io(page->mapping->host,
3046 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047 btrfs_put_ordered_extent(ordered);
3048 lock_extent(tree, page_start, page_end, GFP_NOFS);
3049 }
3050 clear_extent_bit(tree, page_start, page_end,
3051 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3052 EXTENT_ORDERED,
3053 1, 1, GFP_NOFS);
3054 __btrfs_releasepage(page, GFP_NOFS);
3055
Chris Mason4a096752008-07-21 10:29:44 -04003056 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003057 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003058 ClearPagePrivate(page);
3059 set_page_private(page, 0);
3060 page_cache_release(page);
3061 }
Chris Mason39279cc2007-06-12 06:35:45 -04003062}
3063
Chris Mason9ebefb182007-06-15 13:50:00 -04003064/*
3065 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3066 * called from a page fault handler when a page is first dirtied. Hence we must
3067 * be careful to check for EOF conditions here. We set the page up correctly
3068 * for a written page which means we get ENOSPC checking when writing into
3069 * holes and correct delalloc and unwritten extent mapping on filesystems that
3070 * support these features.
3071 *
3072 * We are not allowed to take the i_mutex here so we have to play games to
3073 * protect against truncate races as the page could now be beyond EOF. Because
3074 * vmtruncate() writes the inode size before removing pages, once we have the
3075 * page lock we can determine safely if the page is beyond EOF. If it is not
3076 * beyond EOF, then the page is guaranteed safe against truncation until we
3077 * unlock the page.
3078 */
3079int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3080{
Chris Mason6da6aba2007-12-18 16:15:09 -05003081 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003082 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003083 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3084 struct btrfs_ordered_extent *ordered;
3085 char *kaddr;
3086 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003087 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003088 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003089 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003090 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003091
Chris Mason1832a6d2007-12-21 16:27:21 -05003092 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003093 if (ret)
3094 goto out;
3095
3096 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003097again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003098 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003099 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003100 page_start = page_offset(page);
3101 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003102
Chris Mason9ebefb182007-06-15 13:50:00 -04003103 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003105 /* page got truncated out from underneath us */
3106 goto out_unlock;
3107 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003108 wait_on_page_writeback(page);
3109
3110 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3111 set_page_extent_mapped(page);
3112
Chris Masoneb84ae02008-07-17 13:53:27 -04003113 /*
3114 * we can't set the delalloc bits if there are pending ordered
3115 * extents. Drop our locks and wait for them to finish
3116 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003117 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3118 if (ordered) {
3119 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3120 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003121 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122 btrfs_put_ordered_extent(ordered);
3123 goto again;
3124 }
3125
3126 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3127 page_end, GFP_NOFS);
3128 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003129
3130 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003131 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003132 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003133 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003134 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003135
Chris Masone6dcd2d2008-07-17 12:53:50 -04003136 if (zero_start != PAGE_CACHE_SIZE) {
3137 kaddr = kmap(page);
3138 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3139 flush_dcache_page(page);
3140 kunmap(page);
3141 }
Chris Mason247e7432008-07-17 12:53:51 -04003142 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003143 set_page_dirty(page);
3144 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003145
3146out_unlock:
3147 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003148out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003149 return ret;
3150}
3151
Chris Mason39279cc2007-06-12 06:35:45 -04003152static void btrfs_truncate(struct inode *inode)
3153{
3154 struct btrfs_root *root = BTRFS_I(inode)->root;
3155 int ret;
3156 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003157 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003158 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003159
3160 if (!S_ISREG(inode->i_mode))
3161 return;
3162 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3163 return;
3164
3165 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003166 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003167
Chris Mason39279cc2007-06-12 06:35:45 -04003168 trans = btrfs_start_transaction(root, 1);
3169 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003170 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003171
Josef Bacik7b128762008-07-24 12:17:14 -04003172 ret = btrfs_orphan_add(trans, inode);
3173 if (ret)
3174 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003175 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003176 ret = btrfs_truncate_in_trans(trans, root, inode,
3177 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003178 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003179
Josef Bacik7b128762008-07-24 12:17:14 -04003180 ret = btrfs_orphan_del(trans, inode);
3181 BUG_ON(ret);
3182
3183out:
3184 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003185 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003186 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003187 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003188}
3189
Sven Wegener3b963622008-06-09 21:57:42 -04003190/*
3191 * Invalidate a single dcache entry at the root of the filesystem.
3192 * Needed after creation of snapshot or subvolume.
3193 */
3194void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3195 int namelen)
3196{
3197 struct dentry *alias, *entry;
3198 struct qstr qstr;
3199
3200 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3201 if (alias) {
3202 qstr.name = name;
3203 qstr.len = namelen;
3204 /* change me if btrfs ever gets a d_hash operation */
3205 qstr.hash = full_name_hash(qstr.name, qstr.len);
3206 entry = d_lookup(alias, &qstr);
3207 dput(alias);
3208 if (entry) {
3209 d_invalidate(entry);
3210 dput(entry);
3211 }
3212 }
3213}
3214
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003215int btrfs_create_subvol_root(struct btrfs_root *new_root,
3216 struct btrfs_trans_handle *trans, u64 new_dirid,
3217 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003218{
Chris Mason39279cc2007-06-12 06:35:45 -04003219 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003220
Josef Bacikaec74772008-07-24 12:12:38 -04003221 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003222 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003223 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003224 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003225 inode->i_op = &btrfs_dir_inode_operations;
3226 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003227 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003228
Chris Mason39279cc2007-06-12 06:35:45 -04003229 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003230 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003231
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003232 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003233}
3234
Chris Masonedbd8d42007-12-21 16:27:24 -05003235unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003236 struct file_ra_state *ra, struct file *file,
3237 pgoff_t offset, pgoff_t last_index)
3238{
Chris Mason8e7bf942008-04-28 09:02:36 -04003239 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003240
3241#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003242 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3243 return offset;
3244#else
Chris Mason86479a02007-09-10 19:58:16 -04003245 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3246 return offset + req_size;
3247#endif
3248}
3249
Chris Mason39279cc2007-06-12 06:35:45 -04003250struct inode *btrfs_alloc_inode(struct super_block *sb)
3251{
3252 struct btrfs_inode *ei;
3253
3254 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3255 if (!ei)
3256 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003257 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003258 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003259 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3260 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003261 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003262 return &ei->vfs_inode;
3263}
3264
3265void btrfs_destroy_inode(struct inode *inode)
3266{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003267 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003268 WARN_ON(!list_empty(&inode->i_dentry));
3269 WARN_ON(inode->i_data.nrpages);
3270
Josef Bacik33268ea2008-07-24 12:16:36 -04003271 if (BTRFS_I(inode)->i_acl &&
3272 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3273 posix_acl_release(BTRFS_I(inode)->i_acl);
3274 if (BTRFS_I(inode)->i_default_acl &&
3275 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3276 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3277
Josef Bacik7b128762008-07-24 12:17:14 -04003278 spin_lock(&BTRFS_I(inode)->root->orphan_lock);
3279 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3280 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3281 " list\n", inode->i_ino);
3282 dump_stack();
3283 }
3284 spin_unlock(&BTRFS_I(inode)->root->orphan_lock);
3285
Chris Masone6dcd2d2008-07-17 12:53:50 -04003286 while(1) {
3287 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3288 if (!ordered)
3289 break;
3290 else {
3291 printk("found ordered extent %Lu %Lu\n",
3292 ordered->file_offset, ordered->len);
3293 btrfs_remove_ordered_extent(inode, ordered);
3294 btrfs_put_ordered_extent(ordered);
3295 btrfs_put_ordered_extent(ordered);
3296 }
3297 }
Chris Mason8c416c92008-01-14 15:10:26 -05003298 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003299 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3300}
3301
Chris Mason44ec0b72007-10-29 10:55:05 -04003302#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3303static void init_once(struct kmem_cache * cachep, void *foo)
3304#else
Chris Mason39279cc2007-06-12 06:35:45 -04003305static void init_once(void * foo, struct kmem_cache * cachep,
3306 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003307#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003308{
3309 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3310
3311 inode_init_once(&ei->vfs_inode);
3312}
3313
3314void btrfs_destroy_cachep(void)
3315{
3316 if (btrfs_inode_cachep)
3317 kmem_cache_destroy(btrfs_inode_cachep);
3318 if (btrfs_trans_handle_cachep)
3319 kmem_cache_destroy(btrfs_trans_handle_cachep);
3320 if (btrfs_transaction_cachep)
3321 kmem_cache_destroy(btrfs_transaction_cachep);
3322 if (btrfs_bit_radix_cachep)
3323 kmem_cache_destroy(btrfs_bit_radix_cachep);
3324 if (btrfs_path_cachep)
3325 kmem_cache_destroy(btrfs_path_cachep);
3326}
3327
Chris Mason86479a02007-09-10 19:58:16 -04003328struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003329 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003330#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3331 void (*ctor)(struct kmem_cache *, void *)
3332#else
Chris Mason92fee662007-07-25 12:31:35 -04003333 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003334 unsigned long)
3335#endif
3336 )
Chris Mason92fee662007-07-25 12:31:35 -04003337{
3338 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3339 SLAB_MEM_SPREAD | extra_flags), ctor
3340#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3341 ,NULL
3342#endif
3343 );
3344}
3345
Chris Mason39279cc2007-06-12 06:35:45 -04003346int btrfs_init_cachep(void)
3347{
Chris Mason86479a02007-09-10 19:58:16 -04003348 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003349 sizeof(struct btrfs_inode),
3350 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003351 if (!btrfs_inode_cachep)
3352 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003353 btrfs_trans_handle_cachep =
3354 btrfs_cache_create("btrfs_trans_handle_cache",
3355 sizeof(struct btrfs_trans_handle),
3356 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003357 if (!btrfs_trans_handle_cachep)
3358 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003359 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003360 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003361 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003362 if (!btrfs_transaction_cachep)
3363 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003364 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003365 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003366 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003367 if (!btrfs_path_cachep)
3368 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003369 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003370 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003371 if (!btrfs_bit_radix_cachep)
3372 goto fail;
3373 return 0;
3374fail:
3375 btrfs_destroy_cachep();
3376 return -ENOMEM;
3377}
3378
3379static int btrfs_getattr(struct vfsmount *mnt,
3380 struct dentry *dentry, struct kstat *stat)
3381{
3382 struct inode *inode = dentry->d_inode;
3383 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003384 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003385 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003386 return 0;
3387}
3388
3389static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3390 struct inode * new_dir,struct dentry *new_dentry)
3391{
3392 struct btrfs_trans_handle *trans;
3393 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3394 struct inode *new_inode = new_dentry->d_inode;
3395 struct inode *old_inode = old_dentry->d_inode;
3396 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003397 int ret;
3398
3399 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3400 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3401 return -ENOTEMPTY;
3402 }
Chris Mason5f39d392007-10-15 16:14:19 -04003403
Chris Mason1832a6d2007-12-21 16:27:21 -05003404 ret = btrfs_check_free_space(root, 1, 0);
3405 if (ret)
3406 goto out_unlock;
3407
Chris Mason39279cc2007-06-12 06:35:45 -04003408 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003409
Chris Mason39279cc2007-06-12 06:35:45 -04003410 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003411
3412 old_dentry->d_inode->i_nlink++;
3413 old_dir->i_ctime = old_dir->i_mtime = ctime;
3414 new_dir->i_ctime = new_dir->i_mtime = ctime;
3415 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003416
Chris Mason39279cc2007-06-12 06:35:45 -04003417 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3418 if (ret)
3419 goto out_fail;
3420
3421 if (new_inode) {
3422 new_inode->i_ctime = CURRENT_TIME;
3423 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3424 if (ret)
3425 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003426 if (new_inode->i_nlink == 0) {
3427 ret = btrfs_orphan_add(trans, new_inode);
3428 if (ret)
3429 goto out_fail;
3430 }
Chris Mason39279cc2007-06-12 06:35:45 -04003431 }
Josef Bacikaec74772008-07-24 12:12:38 -04003432 ret = btrfs_set_inode_index(new_dir, old_inode);
3433 if (ret)
3434 goto out_fail;
3435
Chris Mason9c583092008-01-29 15:15:18 -05003436 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003437 if (ret)
3438 goto out_fail;
3439
3440out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003441 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003442out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003443 return ret;
3444}
3445
3446static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3447 const char *symname)
3448{
3449 struct btrfs_trans_handle *trans;
3450 struct btrfs_root *root = BTRFS_I(dir)->root;
3451 struct btrfs_path *path;
3452 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003453 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003454 int err;
3455 int drop_inode = 0;
3456 u64 objectid;
3457 int name_len;
3458 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003459 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003460 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003461 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003462 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003463
3464 name_len = strlen(symname) + 1;
3465 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3466 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003467
Chris Mason1832a6d2007-12-21 16:27:21 -05003468 err = btrfs_check_free_space(root, 1, 0);
3469 if (err)
3470 goto out_fail;
3471
Chris Mason39279cc2007-06-12 06:35:45 -04003472 trans = btrfs_start_transaction(root, 1);
3473 btrfs_set_trans_block_group(trans, dir);
3474
3475 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3476 if (err) {
3477 err = -ENOSPC;
3478 goto out_unlock;
3479 }
3480
Josef Bacikaec74772008-07-24 12:12:38 -04003481 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003482 dentry->d_name.len,
3483 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003484 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3485 err = PTR_ERR(inode);
3486 if (IS_ERR(inode))
3487 goto out_unlock;
3488
Josef Bacik33268ea2008-07-24 12:16:36 -04003489 err = btrfs_init_acl(inode, dir);
3490 if (err) {
3491 drop_inode = 1;
3492 goto out_unlock;
3493 }
3494
Chris Mason39279cc2007-06-12 06:35:45 -04003495 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003496 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 if (err)
3498 drop_inode = 1;
3499 else {
3500 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003501 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003502 inode->i_fop = &btrfs_file_operations;
3503 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003504 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3505 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003506 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003507 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3508 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003509 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003510 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003511 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003512 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003513 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003514 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003515 }
3516 dir->i_sb->s_dirt = 1;
3517 btrfs_update_inode_block_group(trans, inode);
3518 btrfs_update_inode_block_group(trans, dir);
3519 if (drop_inode)
3520 goto out_unlock;
3521
3522 path = btrfs_alloc_path();
3523 BUG_ON(!path);
3524 key.objectid = inode->i_ino;
3525 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003526 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3527 datasize = btrfs_file_extent_calc_inline_size(name_len);
3528 err = btrfs_insert_empty_item(trans, root, path, &key,
3529 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003530 if (err) {
3531 drop_inode = 1;
3532 goto out_unlock;
3533 }
Chris Mason5f39d392007-10-15 16:14:19 -04003534 leaf = path->nodes[0];
3535 ei = btrfs_item_ptr(leaf, path->slots[0],
3536 struct btrfs_file_extent_item);
3537 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3538 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003539 BTRFS_FILE_EXTENT_INLINE);
3540 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003541 write_extent_buffer(leaf, symname, ptr, name_len);
3542 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003544
Chris Mason39279cc2007-06-12 06:35:45 -04003545 inode->i_op = &btrfs_symlink_inode_operations;
3546 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003547 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003548 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003549 err = btrfs_update_inode(trans, root, inode);
3550 if (err)
3551 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003552
3553out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003554 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003555 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003556out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003557 if (drop_inode) {
3558 inode_dec_link_count(inode);
3559 iput(inode);
3560 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003561 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003562 return err;
3563}
Chris Mason16432982008-04-10 10:23:21 -04003564
Chris Masone6dcd2d2008-07-17 12:53:50 -04003565static int btrfs_set_page_dirty(struct page *page)
3566{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003567 return __set_page_dirty_nobuffers(page);
3568}
3569
Yanfdebe2b2008-01-14 13:26:08 -05003570static int btrfs_permission(struct inode *inode, int mask,
3571 struct nameidata *nd)
3572{
3573 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3574 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003575 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003576}
Chris Mason39279cc2007-06-12 06:35:45 -04003577
3578static struct inode_operations btrfs_dir_inode_operations = {
3579 .lookup = btrfs_lookup,
3580 .create = btrfs_create,
3581 .unlink = btrfs_unlink,
3582 .link = btrfs_link,
3583 .mkdir = btrfs_mkdir,
3584 .rmdir = btrfs_rmdir,
3585 .rename = btrfs_rename,
3586 .symlink = btrfs_symlink,
3587 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003588 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003589 .setxattr = generic_setxattr,
3590 .getxattr = generic_getxattr,
3591 .listxattr = btrfs_listxattr,
3592 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003593 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003594};
Chris Mason39279cc2007-06-12 06:35:45 -04003595static struct inode_operations btrfs_dir_ro_inode_operations = {
3596 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003597 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003598};
Chris Mason39279cc2007-06-12 06:35:45 -04003599static struct file_operations btrfs_dir_file_operations = {
3600 .llseek = generic_file_llseek,
3601 .read = generic_read_dir,
3602 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003603 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003604#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003605 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003606#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003607 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003608};
3609
Chris Masond1310b22008-01-24 16:13:08 -05003610static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003611 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003612 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003613 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003614 .readpage_io_hook = btrfs_readpage_io_hook,
3615 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003616 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003617 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003618 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003619 .set_bit_hook = btrfs_set_bit_hook,
3620 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003621};
3622
Chris Mason39279cc2007-06-12 06:35:45 -04003623static struct address_space_operations btrfs_aops = {
3624 .readpage = btrfs_readpage,
3625 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003626 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003627 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003628 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003629 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003630 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003631 .invalidatepage = btrfs_invalidatepage,
3632 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003633 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003634};
3635
3636static struct address_space_operations btrfs_symlink_aops = {
3637 .readpage = btrfs_readpage,
3638 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003639 .invalidatepage = btrfs_invalidatepage,
3640 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003641};
3642
3643static struct inode_operations btrfs_file_inode_operations = {
3644 .truncate = btrfs_truncate,
3645 .getattr = btrfs_getattr,
3646 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003647 .setxattr = generic_setxattr,
3648 .getxattr = generic_getxattr,
3649 .listxattr = btrfs_listxattr,
3650 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003651 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003652};
Josef Bacik618e21d2007-07-11 10:18:17 -04003653static struct inode_operations btrfs_special_inode_operations = {
3654 .getattr = btrfs_getattr,
3655 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003656 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003657 .setxattr = generic_setxattr,
3658 .getxattr = generic_getxattr,
3659 .listxattr = btrfs_listxattr,
3660 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003661};
Chris Mason39279cc2007-06-12 06:35:45 -04003662static struct inode_operations btrfs_symlink_inode_operations = {
3663 .readlink = generic_readlink,
3664 .follow_link = page_follow_link_light,
3665 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003666 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003667};