blob: 7af8be076ee53d0632147787b05e2d86e21612e0 [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
Yan Zhengf321e492008-07-30 09:26:11 -0400252 if (btrfs_cross_ref_exists(root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400253 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500264 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto not_found;
266 }
267loop:
268 if (start > end) {
269 btrfs_free_path(path);
270 return 0;
271 }
272 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500273 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto again;
275
276not_found:
Yan Zhengf321e492008-07-30 09:26:11 -0400277 btrfs_release_path(root, path);
Chris Masonbbaf5492008-05-08 16:31:21 -0400278 cow_file_range(inode, start, end);
279 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 goto loop;
281}
282
283static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
284{
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400287
Yanb98b6762008-01-08 15:54:37 -0500288 if (btrfs_test_opt(root, NODATACOW) ||
289 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500290 ret = run_delalloc_nocow(inode, start, end);
291 else
292 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500293
Chris Masonb888db22007-08-27 16:49:44 -0400294 return ret;
295}
296
Chris Mason291d6732008-01-29 15:55:23 -0500297int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500301 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500302 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500304 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500305 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400306 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500307 }
308 return 0;
309}
310
311int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500312 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Chris Masonb0c68f82008-01-31 11:05:37 -0500314 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500315 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400316 unsigned long flags;
317
318 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500319 if (end - start + 1 > root->fs_info->delalloc_bytes) {
320 printk("warning: delalloc account %Lu %Lu\n",
321 end - start + 1, root->fs_info->delalloc_bytes);
322 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500323 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500324 } else {
325 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500326 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500327 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400328 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500329 }
330 return 0;
331}
332
Chris Mason239b14b2008-03-24 15:02:07 -0400333int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
334 size_t size, struct bio *bio)
335{
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
337 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400339 u64 length = 0;
340 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400341 int ret;
342
Chris Masonf2d8d742008-04-21 10:03:05 -0400343 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400344 map_tree = &root->fs_info->mapping_tree;
345 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400346 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400347 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400348
Chris Mason239b14b2008-03-24 15:02:07 -0400349 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400350 return 1;
351 }
352 return 0;
353}
354
Chris Mason44b8bd72008-04-16 11:14:51 -0400355int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400356 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500357{
Chris Mason065631f2008-02-20 12:07:25 -0500358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500359 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400360
Chris Mason3edf7d32008-07-18 06:17:13 -0400361 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400362 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400363
Chris Mason8b712842008-06-11 16:50:36 -0400364 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400365}
366
367int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
368 int mirror_num)
369{
370 struct btrfs_root *root = BTRFS_I(inode)->root;
371 int ret = 0;
372
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
374 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500375
Chris Masone6dcd2d2008-07-17 12:53:50 -0400376 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400377 goto mapit;
378 }
Chris Mason065631f2008-02-20 12:07:25 -0500379
Chris Mason44b8bd72008-04-16 11:14:51 -0400380 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
381 inode, rw, bio, mirror_num,
382 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400383mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400384 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500385}
Chris Mason6885f302008-02-20 16:11:05 -0500386
Chris Masonba1da2f2008-07-17 12:54:15 -0400387static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400388 struct inode *inode, u64 file_offset,
389 struct list_head *list)
390{
391 struct list_head *cur;
392 struct btrfs_ordered_sum *sum;
393
394 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400395 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 sum = list_entry(cur, struct btrfs_ordered_sum, list);
397 mutex_lock(&BTRFS_I(inode)->csum_mutex);
398 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
399 inode, sum);
400 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400401 }
402 return 0;
403}
404
Chris Mason247e7432008-07-17 12:53:51 -0400405struct btrfs_writepage_fixup {
406 struct page *page;
407 struct btrfs_work work;
408};
409
410/* see btrfs_writepage_start_hook for details on why this is required */
411void btrfs_writepage_fixup_worker(struct btrfs_work *work)
412{
413 struct btrfs_writepage_fixup *fixup;
414 struct btrfs_ordered_extent *ordered;
415 struct page *page;
416 struct inode *inode;
417 u64 page_start;
418 u64 page_end;
419
420 fixup = container_of(work, struct btrfs_writepage_fixup, work);
421 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400422again:
Chris Mason247e7432008-07-17 12:53:51 -0400423 lock_page(page);
424 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
425 ClearPageChecked(page);
426 goto out_page;
427 }
428
429 inode = page->mapping->host;
430 page_start = page_offset(page);
431 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
432
433 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400434
435 /* already ordered? We're done */
436 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
437 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400438 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400439 }
440
441 ordered = btrfs_lookup_ordered_extent(inode, page_start);
442 if (ordered) {
443 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
444 page_end, GFP_NOFS);
445 unlock_page(page);
446 btrfs_start_ordered_extent(inode, ordered, 1);
447 goto again;
448 }
Chris Mason247e7432008-07-17 12:53:51 -0400449
450 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
451 GFP_NOFS);
452 ClearPageChecked(page);
453out:
454 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
455out_page:
456 unlock_page(page);
457 page_cache_release(page);
458}
459
460/*
461 * There are a few paths in the higher layers of the kernel that directly
462 * set the page dirty bit without asking the filesystem if it is a
463 * good idea. This causes problems because we want to make sure COW
464 * properly happens and the data=ordered rules are followed.
465 *
466 * In our case any range that doesn't have the EXTENT_ORDERED bit set
467 * hasn't been properly setup for IO. We kick off an async process
468 * to fix it up. The async helper will wait for ordered extents, set
469 * the delalloc bit and make it safe to write the page.
470 */
471int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
472{
473 struct inode *inode = page->mapping->host;
474 struct btrfs_writepage_fixup *fixup;
475 struct btrfs_root *root = BTRFS_I(inode)->root;
476 int ret;
477
478 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
479 EXTENT_ORDERED, 0);
480 if (ret)
481 return 0;
482
483 if (PageChecked(page))
484 return -EAGAIN;
485
486 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
487 if (!fixup)
488 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400489
Chris Mason247e7432008-07-17 12:53:51 -0400490 SetPageChecked(page);
491 page_cache_get(page);
492 fixup->work.func = btrfs_writepage_fixup_worker;
493 fixup->page = page;
494 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
495 return -EAGAIN;
496}
497
Chris Mason211f90e2008-07-18 11:56:15 -0400498static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400500 struct btrfs_root *root = BTRFS_I(inode)->root;
501 struct btrfs_trans_handle *trans;
502 struct btrfs_ordered_extent *ordered_extent;
503 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
505 struct extent_map *em;
Chris Masonf4219502008-07-22 11:18:09 -0400506 struct extent_map *em_orig;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400507 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400508 u64 clear_start;
509 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400510 struct list_head list;
511 struct btrfs_key ins;
Chris Masonf4219502008-07-22 11:18:09 -0400512 struct rb_node *rb;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400513 int ret;
514
515 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400516 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400517 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400518
Chris Masonf9295742008-07-17 12:54:14 -0400519 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400520
521 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
522 BUG_ON(!ordered_extent);
523
524 lock_extent(io_tree, ordered_extent->file_offset,
525 ordered_extent->file_offset + ordered_extent->len - 1,
526 GFP_NOFS);
527
528 INIT_LIST_HEAD(&list);
529
530 ins.objectid = ordered_extent->start;
531 ins.offset = ordered_extent->len;
532 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400533
Chris Masone6dcd2d2008-07-17 12:53:50 -0400534 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
535 trans->transid, inode->i_ino,
536 ordered_extent->file_offset, &ins);
537 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400538
539 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400540
Chris Masonf4219502008-07-22 11:18:09 -0400541 spin_lock(&em_tree->lock);
542 clear_start = ordered_extent->file_offset;
543 clear_end = ordered_extent->file_offset + ordered_extent->len;
544 em = lookup_extent_mapping(em_tree, clear_start,
545 ordered_extent->len);
546 em_orig = em;
547 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
548 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
549 rb = rb_next(&em->rb_node);
550 if (!rb)
551 break;
552 em = rb_entry(rb, struct extent_map, rb_node);
553 }
554 free_extent_map(em_orig);
555 spin_unlock(&em_tree->lock);
556
Chris Masone6dcd2d2008-07-17 12:53:50 -0400557 ret = btrfs_drop_extents(trans, root, inode,
558 ordered_extent->file_offset,
559 ordered_extent->file_offset +
560 ordered_extent->len,
561 ordered_extent->file_offset, &alloc_hint);
562 BUG_ON(ret);
563 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
564 ordered_extent->file_offset,
565 ordered_extent->start,
566 ordered_extent->len,
567 ordered_extent->len, 0);
568 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400569
Chris Masone6dcd2d2008-07-17 12:53:50 -0400570 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
571 ordered_extent->file_offset +
572 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400573 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
574
Chris Masone6dcd2d2008-07-17 12:53:50 -0400575 inode->i_blocks += ordered_extent->len >> 9;
576 unlock_extent(io_tree, ordered_extent->file_offset,
577 ordered_extent->file_offset + ordered_extent->len - 1,
578 GFP_NOFS);
579 add_pending_csums(trans, inode, ordered_extent->file_offset,
580 &ordered_extent->list);
581
Chris Masondbe674a2008-07-17 12:54:05 -0400582 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400583 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400584
Chris Masone6dcd2d2008-07-17 12:53:50 -0400585 /* once for us */
586 btrfs_put_ordered_extent(ordered_extent);
587 /* once for the tree */
588 btrfs_put_ordered_extent(ordered_extent);
589
590 btrfs_update_inode(trans, root, inode);
591 btrfs_end_transaction(trans, root);
592 return 0;
593}
594
Chris Mason211f90e2008-07-18 11:56:15 -0400595int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
596 struct extent_state *state, int uptodate)
597{
598 return btrfs_finish_ordered_io(page->mapping->host, start, end);
599}
600
Chris Mason07157aa2007-08-30 08:50:51 -0400601int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
602{
603 int ret = 0;
604 struct inode *inode = page->mapping->host;
605 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500606 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400607 struct btrfs_csum_item *item;
608 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400609 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400610
Yanb98b6762008-01-08 15:54:37 -0500611 if (btrfs_test_opt(root, NODATASUM) ||
612 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500613 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400614
Chris Mason89642222008-07-24 09:41:53 -0400615 /*
616 * It is possible there is an ordered extent that has
617 * not yet finished for this range in the file. If so,
618 * that extent will have a csum cached, and it will insert
619 * the sum after all the blocks in the extent are fully
620 * on disk. So, look for an ordered extent and use the
621 * sum if found. We have to do this before looking in the
622 * btree because csum items are pre-inserted based on
623 * the file size. btrfs_lookup_csum might find an item
624 * that still hasn't been fully filled.
625 */
626 ret = btrfs_find_ordered_sum(inode, start, &csum);
627 if (ret == 0)
628 goto found;
629
630 ret = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400631 path = btrfs_alloc_path();
632 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
633 if (IS_ERR(item)) {
634 ret = PTR_ERR(item);
635 /* a csum that isn't present is a preallocated region. */
636 if (ret == -ENOENT || ret == -EFBIG)
637 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400638 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400639 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
640 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400641 goto out;
642 }
Chris Masonff79f812007-10-15 16:22:25 -0400643 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
644 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400645found:
Chris Masond1310b22008-01-24 16:13:08 -0500646 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400647out:
648 if (path)
649 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400650 return ret;
651}
652
Chris Mason7e383262008-04-09 16:28:12 -0400653struct io_failure_record {
654 struct page *page;
655 u64 start;
656 u64 len;
657 u64 logical;
658 int last_mirror;
659};
660
Chris Mason1259ab72008-05-12 13:39:03 -0400661int btrfs_io_failed_hook(struct bio *failed_bio,
662 struct page *page, u64 start, u64 end,
663 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400664{
665 struct io_failure_record *failrec = NULL;
666 u64 private;
667 struct extent_map *em;
668 struct inode *inode = page->mapping->host;
669 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400670 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400671 struct bio *bio;
672 int num_copies;
673 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400674 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400675 u64 logical;
676
677 ret = get_state_private(failure_tree, start, &private);
678 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400679 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
680 if (!failrec)
681 return -ENOMEM;
682 failrec->start = start;
683 failrec->len = end - start + 1;
684 failrec->last_mirror = 0;
685
Chris Mason3b951512008-04-17 11:29:12 -0400686 spin_lock(&em_tree->lock);
687 em = lookup_extent_mapping(em_tree, start, failrec->len);
688 if (em->start > start || em->start + em->len < start) {
689 free_extent_map(em);
690 em = NULL;
691 }
692 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400693
694 if (!em || IS_ERR(em)) {
695 kfree(failrec);
696 return -EIO;
697 }
698 logical = start - em->start;
699 logical = em->block_start + logical;
700 failrec->logical = logical;
701 free_extent_map(em);
702 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
703 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400704 set_state_private(failure_tree, start,
705 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400706 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400707 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400708 }
709 num_copies = btrfs_num_copies(
710 &BTRFS_I(inode)->root->fs_info->mapping_tree,
711 failrec->logical, failrec->len);
712 failrec->last_mirror++;
713 if (!state) {
714 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
715 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
716 failrec->start,
717 EXTENT_LOCKED);
718 if (state && state->start != failrec->start)
719 state = NULL;
720 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
721 }
722 if (!state || failrec->last_mirror > num_copies) {
723 set_state_private(failure_tree, failrec->start, 0);
724 clear_extent_bits(failure_tree, failrec->start,
725 failrec->start + failrec->len - 1,
726 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
727 kfree(failrec);
728 return -EIO;
729 }
730 bio = bio_alloc(GFP_NOFS, 1);
731 bio->bi_private = state;
732 bio->bi_end_io = failed_bio->bi_end_io;
733 bio->bi_sector = failrec->logical >> 9;
734 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400735 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400736 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400737 if (failed_bio->bi_rw & (1 << BIO_RW))
738 rw = WRITE;
739 else
740 rw = READ;
741
742 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
743 failrec->last_mirror);
744 return 0;
745}
746
747int btrfs_clean_io_failures(struct inode *inode, u64 start)
748{
749 u64 private;
750 u64 private_failure;
751 struct io_failure_record *failure;
752 int ret;
753
754 private = 0;
755 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
756 (u64)-1, 1, EXTENT_DIRTY)) {
757 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
758 start, &private_failure);
759 if (ret == 0) {
760 failure = (struct io_failure_record *)(unsigned long)
761 private_failure;
762 set_state_private(&BTRFS_I(inode)->io_failure_tree,
763 failure->start, 0);
764 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
765 failure->start,
766 failure->start + failure->len - 1,
767 EXTENT_DIRTY | EXTENT_LOCKED,
768 GFP_NOFS);
769 kfree(failure);
770 }
771 }
Chris Mason7e383262008-04-09 16:28:12 -0400772 return 0;
773}
774
Chris Mason70dec802008-01-29 09:59:12 -0500775int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
776 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400777{
Chris Mason35ebb932007-10-30 16:56:53 -0400778 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400779 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500780 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400781 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500782 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400783 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400784 struct btrfs_root *root = BTRFS_I(inode)->root;
785 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400786 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500787
Yanb98b6762008-01-08 15:54:37 -0500788 if (btrfs_test_opt(root, NODATASUM) ||
789 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500790 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500791 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500792 private = state->private;
793 ret = 0;
794 } else {
795 ret = get_state_private(io_tree, start, &private);
796 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400797 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400798 kaddr = kmap_atomic(page, KM_IRQ0);
799 if (ret) {
800 goto zeroit;
801 }
Chris Masonff79f812007-10-15 16:22:25 -0400802 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
803 btrfs_csum_final(csum, (char *)&csum);
804 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400805 goto zeroit;
806 }
807 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400808 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400809
810 /* if the io failure tree for this inode is non-empty,
811 * check to see if we've recovered from a failed IO
812 */
Chris Mason1259ab72008-05-12 13:39:03 -0400813 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400814 return 0;
815
816zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500817 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
818 page->mapping->host->i_ino, (unsigned long long)start, csum,
819 private);
Chris Masondb945352007-10-15 16:15:53 -0400820 memset(kaddr + offset, 1, end - start + 1);
821 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400822 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400823 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400824 if (private == 0)
825 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400826 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400827}
Chris Masonb888db22007-08-27 16:49:44 -0400828
Josef Bacik7b128762008-07-24 12:17:14 -0400829/*
830 * This creates an orphan entry for the given inode in case something goes
831 * wrong in the middle of an unlink/truncate.
832 */
833int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
834{
835 struct btrfs_root *root = BTRFS_I(inode)->root;
836 int ret = 0;
837
Yanbcc63ab2008-07-30 16:29:20 -0400838 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400839
840 /* already on the orphan list, we're good */
841 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400842 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400843 return 0;
844 }
845
846 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
847
Yanbcc63ab2008-07-30 16:29:20 -0400848 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400849
850 /*
851 * insert an orphan item to track this unlinked/truncated file
852 */
853 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
854
855 return ret;
856}
857
858/*
859 * We have done the truncate/delete so we can go ahead and remove the orphan
860 * item for this particular inode.
861 */
862int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
863{
864 struct btrfs_root *root = BTRFS_I(inode)->root;
865 int ret = 0;
866
Yanbcc63ab2008-07-30 16:29:20 -0400867 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400868
869 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400870 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400871 return 0;
872 }
873
874 list_del_init(&BTRFS_I(inode)->i_orphan);
875 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400876 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400877 return 0;
878 }
879
Yanbcc63ab2008-07-30 16:29:20 -0400880 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400881
882 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
883
884 return ret;
885}
886
887/*
888 * this cleans up any orphans that may be left on the list from the last use
889 * of this root.
890 */
891void btrfs_orphan_cleanup(struct btrfs_root *root)
892{
893 struct btrfs_path *path;
894 struct extent_buffer *leaf;
895 struct btrfs_item *item;
896 struct btrfs_key key, found_key;
897 struct btrfs_trans_handle *trans;
898 struct inode *inode;
899 int ret = 0, nr_unlink = 0, nr_truncate = 0;
900
901 /* don't do orphan cleanup if the fs is readonly. */
902 if (root->inode->i_sb->s_flags & MS_RDONLY)
903 return;
904
905 path = btrfs_alloc_path();
906 if (!path)
907 return;
908 path->reada = -1;
909
910 key.objectid = BTRFS_ORPHAN_OBJECTID;
911 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
912 key.offset = (u64)-1;
913
914 trans = btrfs_start_transaction(root, 1);
915 btrfs_set_trans_block_group(trans, root->inode);
916
917 while (1) {
918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
919 if (ret < 0) {
920 printk(KERN_ERR "Error searching slot for orphan: %d"
921 "\n", ret);
922 break;
923 }
924
925 /*
926 * if ret == 0 means we found what we were searching for, which
927 * is weird, but possible, so only screw with path if we didnt
928 * find the key and see if we have stuff that matches
929 */
930 if (ret > 0) {
931 if (path->slots[0] == 0)
932 break;
933 path->slots[0]--;
934 }
935
936 /* pull out the item */
937 leaf = path->nodes[0];
938 item = btrfs_item_nr(leaf, path->slots[0]);
939 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
940
941 /* make sure the item matches what we want */
942 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
943 break;
944 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
945 break;
946
947 /* release the path since we're done with it */
948 btrfs_release_path(root, path);
949
950 /*
951 * this is where we are basically btrfs_lookup, without the
952 * crossing root thing. we store the inode number in the
953 * offset of the orphan item.
954 */
955 inode = btrfs_iget_locked(root->inode->i_sb,
956 found_key.offset, root);
957 if (!inode)
958 break;
959
960 if (inode->i_state & I_NEW) {
961 BTRFS_I(inode)->root = root;
962
963 /* have to set the location manually */
964 BTRFS_I(inode)->location.objectid = inode->i_ino;
965 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
966 BTRFS_I(inode)->location.offset = 0;
967
968 btrfs_read_locked_inode(inode);
969 unlock_new_inode(inode);
970 }
971
972 /*
973 * add this inode to the orphan list so btrfs_orphan_del does
974 * the proper thing when we hit it
975 */
Yanbcc63ab2008-07-30 16:29:20 -0400976 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400977 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400978 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400979
980 /*
981 * if this is a bad inode, means we actually succeeded in
982 * removing the inode, but not the orphan record, which means
983 * we need to manually delete the orphan since iput will just
984 * do a destroy_inode
985 */
986 if (is_bad_inode(inode)) {
987 btrfs_orphan_del(trans, inode);
988 iput(inode);
989 continue;
990 }
991
992 /* if we have links, this was a truncate, lets do that */
993 if (inode->i_nlink) {
994 nr_truncate++;
995 btrfs_truncate(inode);
996 } else {
997 nr_unlink++;
998 }
999
1000 /* this will do delete_inode and everything for us */
1001 iput(inode);
1002 }
1003
1004 if (nr_unlink)
1005 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1006 if (nr_truncate)
1007 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1008
1009 btrfs_free_path(path);
1010 btrfs_end_transaction(trans, root);
1011}
1012
Chris Mason39279cc2007-06-12 06:35:45 -04001013void btrfs_read_locked_inode(struct inode *inode)
1014{
1015 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001016 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001017 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001018 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001019 struct btrfs_root *root = BTRFS_I(inode)->root;
1020 struct btrfs_key location;
1021 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001022 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001023 int ret;
1024
1025 path = btrfs_alloc_path();
1026 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001027 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001028
Chris Mason39279cc2007-06-12 06:35:45 -04001029 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001030 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001031 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001032
Chris Mason5f39d392007-10-15 16:14:19 -04001033 leaf = path->nodes[0];
1034 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_inode_item);
1036
1037 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1038 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1039 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1040 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001041 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001042
1043 tspec = btrfs_inode_atime(inode_item);
1044 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1045 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1046
1047 tspec = btrfs_inode_mtime(inode_item);
1048 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1049 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1050
1051 tspec = btrfs_inode_ctime(inode_item);
1052 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1053 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1054
1055 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1056 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001057 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001058 rdev = btrfs_inode_rdev(leaf, inode_item);
1059
Josef Bacikaec74772008-07-24 12:12:38 -04001060 BTRFS_I(inode)->index_cnt = (u64)-1;
1061
Chris Mason5f39d392007-10-15 16:14:19 -04001062 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001063 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1064 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001065 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001066 if (!BTRFS_I(inode)->block_group) {
1067 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001068 NULL, 0,
1069 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001070 }
Chris Mason39279cc2007-06-12 06:35:45 -04001071 btrfs_free_path(path);
1072 inode_item = NULL;
1073
Chris Mason39279cc2007-06-12 06:35:45 -04001074 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001075 case S_IFREG:
1076 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001077 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001078 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001079 inode->i_fop = &btrfs_file_operations;
1080 inode->i_op = &btrfs_file_inode_operations;
1081 break;
1082 case S_IFDIR:
1083 inode->i_fop = &btrfs_dir_file_operations;
1084 if (root == root->fs_info->tree_root)
1085 inode->i_op = &btrfs_dir_ro_inode_operations;
1086 else
1087 inode->i_op = &btrfs_dir_inode_operations;
1088 break;
1089 case S_IFLNK:
1090 inode->i_op = &btrfs_symlink_inode_operations;
1091 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001092 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001093 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001094 default:
1095 init_special_inode(inode, inode->i_mode, rdev);
1096 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001097 }
1098 return;
1099
1100make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001101 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001102 make_bad_inode(inode);
1103}
1104
Chris Mason5f39d392007-10-15 16:14:19 -04001105static void fill_inode_item(struct extent_buffer *leaf,
1106 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001107 struct inode *inode)
1108{
Chris Mason5f39d392007-10-15 16:14:19 -04001109 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1110 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001111 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001112 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1113 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1114
1115 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1116 inode->i_atime.tv_sec);
1117 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1118 inode->i_atime.tv_nsec);
1119
1120 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1121 inode->i_mtime.tv_sec);
1122 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1123 inode->i_mtime.tv_nsec);
1124
1125 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1126 inode->i_ctime.tv_sec);
1127 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1128 inode->i_ctime.tv_nsec);
1129
1130 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1131 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1132 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001133 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001134 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001135 BTRFS_I(inode)->block_group->key.objectid);
1136}
1137
Chris Masonba1da2f2008-07-17 12:54:15 -04001138int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001139 struct btrfs_root *root,
1140 struct inode *inode)
1141{
1142 struct btrfs_inode_item *inode_item;
1143 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001144 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001145 int ret;
1146
1147 path = btrfs_alloc_path();
1148 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001149 ret = btrfs_lookup_inode(trans, root, path,
1150 &BTRFS_I(inode)->location, 1);
1151 if (ret) {
1152 if (ret > 0)
1153 ret = -ENOENT;
1154 goto failed;
1155 }
1156
Chris Mason5f39d392007-10-15 16:14:19 -04001157 leaf = path->nodes[0];
1158 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001159 struct btrfs_inode_item);
1160
Chris Mason5f39d392007-10-15 16:14:19 -04001161 fill_inode_item(leaf, inode_item, inode);
1162 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001163 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001164 ret = 0;
1165failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001166 btrfs_free_path(path);
1167 return ret;
1168}
1169
1170
1171static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root,
1173 struct inode *dir,
1174 struct dentry *dentry)
1175{
1176 struct btrfs_path *path;
1177 const char *name = dentry->d_name.name;
1178 int name_len = dentry->d_name.len;
1179 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001180 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001181 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001182 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001183 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001184
1185 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001186 if (!path) {
1187 ret = -ENOMEM;
1188 goto err;
1189 }
1190
Chris Mason39279cc2007-06-12 06:35:45 -04001191 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1192 name, name_len, -1);
1193 if (IS_ERR(di)) {
1194 ret = PTR_ERR(di);
1195 goto err;
1196 }
1197 if (!di) {
1198 ret = -ENOENT;
1199 goto err;
1200 }
Chris Mason5f39d392007-10-15 16:14:19 -04001201 leaf = path->nodes[0];
1202 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001203 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001204 if (ret)
1205 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001206 btrfs_release_path(root, path);
1207
Josef Bacikaec74772008-07-24 12:12:38 -04001208 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1209 dentry->d_inode->i_ino,
1210 dentry->d_parent->d_inode->i_ino, &index);
1211 if (ret) {
1212 printk("failed to delete reference to %.*s, "
1213 "inode %lu parent %lu\n", name_len, name,
1214 dentry->d_inode->i_ino,
1215 dentry->d_parent->d_inode->i_ino);
1216 goto err;
1217 }
1218
Chris Mason39279cc2007-06-12 06:35:45 -04001219 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001220 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001221 if (IS_ERR(di)) {
1222 ret = PTR_ERR(di);
1223 goto err;
1224 }
1225 if (!di) {
1226 ret = -ENOENT;
1227 goto err;
1228 }
1229 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001230 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001231
1232 dentry->d_inode->i_ctime = dir->i_ctime;
1233err:
1234 btrfs_free_path(path);
1235 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001236 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001237 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001238 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001239#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1240 dentry->d_inode->i_nlink--;
1241#else
Chris Mason39279cc2007-06-12 06:35:45 -04001242 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001243#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001244 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001245 dir->i_sb->s_dirt = 1;
1246 }
1247 return ret;
1248}
1249
1250static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1251{
1252 struct btrfs_root *root;
1253 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001254 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001255 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001256 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001257
1258 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001259
1260 ret = btrfs_check_free_space(root, 1, 1);
1261 if (ret)
1262 goto fail;
1263
Chris Mason39279cc2007-06-12 06:35:45 -04001264 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001265
Chris Mason39279cc2007-06-12 06:35:45 -04001266 btrfs_set_trans_block_group(trans, dir);
1267 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001268
1269 if (inode->i_nlink == 0)
1270 ret = btrfs_orphan_add(trans, inode);
1271
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001272 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001273
Chris Mason89ce8a62008-06-25 16:01:31 -04001274 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001275fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001276 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001277 return ret;
1278}
1279
1280static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1281{
1282 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001283 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001284 int ret;
1285 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001286 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001287 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001288
Chris Mason925baed2008-06-25 16:01:30 -04001289 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001290 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001291 }
Yan134d4512007-10-25 15:49:25 -04001292
Chris Mason1832a6d2007-12-21 16:27:21 -05001293 ret = btrfs_check_free_space(root, 1, 1);
1294 if (ret)
1295 goto fail;
1296
Chris Mason39279cc2007-06-12 06:35:45 -04001297 trans = btrfs_start_transaction(root, 1);
1298 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001299
Josef Bacik7b128762008-07-24 12:17:14 -04001300 err = btrfs_orphan_add(trans, inode);
1301 if (err)
1302 goto fail_trans;
1303
Chris Mason39279cc2007-06-12 06:35:45 -04001304 /* now the directory is empty */
1305 err = btrfs_unlink_trans(trans, root, dir, dentry);
1306 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001307 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001308 }
Chris Mason39544012007-12-12 14:38:19 -05001309
Josef Bacik7b128762008-07-24 12:17:14 -04001310fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001311 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001312 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001313fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001314 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001315
Chris Mason39279cc2007-06-12 06:35:45 -04001316 if (ret && !err)
1317 err = ret;
1318 return err;
1319}
1320
Chris Mason39279cc2007-06-12 06:35:45 -04001321/*
Chris Mason39279cc2007-06-12 06:35:45 -04001322 * this can truncate away extent items, csum items and directory items.
1323 * It starts at a high offset and removes keys until it can't find
1324 * any higher than i_size.
1325 *
1326 * csum items that cross the new i_size are truncated to the new size
1327 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001328 *
1329 * min_type is the minimum key type to truncate down to. If set to 0, this
1330 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001331 */
1332static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001334 struct inode *inode,
1335 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001336{
1337 int ret;
1338 struct btrfs_path *path;
1339 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001340 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001341 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001342 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001343 struct btrfs_file_extent_item *fi;
1344 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001345 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001346 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001347 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001348 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001349 int found_extent;
1350 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001351 int pending_del_nr = 0;
1352 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001353 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001354 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001355
Chris Mason3b951512008-04-17 11:29:12 -04001356 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001357 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001358 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001359 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001360
Chris Mason39279cc2007-06-12 06:35:45 -04001361 /* FIXME, add redo link to tree so we don't leak on crash */
1362 key.objectid = inode->i_ino;
1363 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001364 key.type = (u8)-1;
1365
Chris Mason85e21ba2008-01-29 15:11:36 -05001366 btrfs_init_path(path);
1367search_again:
1368 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1369 if (ret < 0) {
1370 goto error;
1371 }
1372 if (ret > 0) {
1373 BUG_ON(path->slots[0] == 0);
1374 path->slots[0]--;
1375 }
1376
Chris Mason39279cc2007-06-12 06:35:45 -04001377 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001378 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001379 leaf = path->nodes[0];
1380 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1381 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001382
Chris Mason5f39d392007-10-15 16:14:19 -04001383 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001384 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001385
Chris Mason85e21ba2008-01-29 15:11:36 -05001386 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001387 break;
1388
Chris Mason5f39d392007-10-15 16:14:19 -04001389 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001390 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001391 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001392 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001393 extent_type = btrfs_file_extent_type(leaf, fi);
1394 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001395 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001396 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001397 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1398 struct btrfs_item *item = btrfs_item_nr(leaf,
1399 path->slots[0]);
1400 item_end += btrfs_file_extent_inline_len(leaf,
1401 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001402 }
Yan008630c2007-11-07 13:31:09 -05001403 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001404 }
1405 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1406 ret = btrfs_csum_truncate(trans, root, path,
1407 inode->i_size);
1408 BUG_ON(ret);
1409 }
Yan008630c2007-11-07 13:31:09 -05001410 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001411 if (found_type == BTRFS_DIR_ITEM_KEY) {
1412 found_type = BTRFS_INODE_ITEM_KEY;
1413 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1414 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001415 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1416 found_type = BTRFS_XATTR_ITEM_KEY;
1417 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1418 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001419 } else if (found_type) {
1420 found_type--;
1421 } else {
1422 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001423 }
Yana61721d2007-09-17 11:08:38 -04001424 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001425 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001426 }
Chris Mason5f39d392007-10-15 16:14:19 -04001427 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001428 del_item = 1;
1429 else
1430 del_item = 0;
1431 found_extent = 0;
1432
1433 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001434 if (found_type != BTRFS_EXTENT_DATA_KEY)
1435 goto delete;
1436
1437 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001438 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001439 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001440 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001441 u64 orig_num_bytes =
1442 btrfs_file_extent_num_bytes(leaf, fi);
1443 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001444 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001445 extent_num_bytes = extent_num_bytes &
1446 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001447 btrfs_set_file_extent_num_bytes(leaf, fi,
1448 extent_num_bytes);
1449 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001450 extent_num_bytes);
1451 if (extent_start != 0)
1452 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001453 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001454 } else {
Chris Masondb945352007-10-15 16:15:53 -04001455 extent_num_bytes =
1456 btrfs_file_extent_disk_num_bytes(leaf,
1457 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001458 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001459 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001460 if (extent_start != 0) {
1461 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001462 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001463 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001464 root_gen = btrfs_header_generation(leaf);
1465 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001466 }
Chris Mason90692182008-02-08 13:49:28 -05001467 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1468 if (!del_item) {
1469 u32 newsize = inode->i_size - found_key.offset;
1470 dec_i_blocks(inode, item_end + 1 -
1471 found_key.offset - newsize);
1472 newsize =
1473 btrfs_file_extent_calc_inline_size(newsize);
1474 ret = btrfs_truncate_item(trans, root, path,
1475 newsize, 1);
1476 BUG_ON(ret);
1477 } else {
1478 dec_i_blocks(inode, item_end + 1 -
1479 found_key.offset);
1480 }
Chris Mason39279cc2007-06-12 06:35:45 -04001481 }
Chris Mason179e29e2007-11-01 11:28:41 -04001482delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001483 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001484 if (!pending_del_nr) {
1485 /* no pending yet, add ourselves */
1486 pending_del_slot = path->slots[0];
1487 pending_del_nr = 1;
1488 } else if (pending_del_nr &&
1489 path->slots[0] + 1 == pending_del_slot) {
1490 /* hop on the pending chunk */
1491 pending_del_nr++;
1492 pending_del_slot = path->slots[0];
1493 } else {
1494 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1495 }
Chris Mason39279cc2007-06-12 06:35:45 -04001496 } else {
1497 break;
1498 }
Chris Mason39279cc2007-06-12 06:35:45 -04001499 if (found_extent) {
1500 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001501 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001502 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001503 root_gen, inode->i_ino,
1504 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001505 BUG_ON(ret);
1506 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001507next:
1508 if (path->slots[0] == 0) {
1509 if (pending_del_nr)
1510 goto del_pending;
1511 btrfs_release_path(root, path);
1512 goto search_again;
1513 }
1514
1515 path->slots[0]--;
1516 if (pending_del_nr &&
1517 path->slots[0] + 1 != pending_del_slot) {
1518 struct btrfs_key debug;
1519del_pending:
1520 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1521 pending_del_slot);
1522 ret = btrfs_del_items(trans, root, path,
1523 pending_del_slot,
1524 pending_del_nr);
1525 BUG_ON(ret);
1526 pending_del_nr = 0;
1527 btrfs_release_path(root, path);
1528 goto search_again;
1529 }
Chris Mason39279cc2007-06-12 06:35:45 -04001530 }
1531 ret = 0;
1532error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001533 if (pending_del_nr) {
1534 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1535 pending_del_nr);
1536 }
Chris Mason39279cc2007-06-12 06:35:45 -04001537 btrfs_free_path(path);
1538 inode->i_sb->s_dirt = 1;
1539 return ret;
1540}
1541
Chris Masona52d9a82007-08-27 16:49:44 -04001542/*
1543 * taken from block_truncate_page, but does cow as it zeros out
1544 * any bytes left in the last page in the file.
1545 */
1546static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1547{
1548 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001549 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001550 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1551 struct btrfs_ordered_extent *ordered;
1552 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001553 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001554 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1555 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1556 struct page *page;
1557 int ret = 0;
1558 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001559 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001560
1561 if ((offset & (blocksize - 1)) == 0)
1562 goto out;
1563
1564 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001565again:
Chris Masona52d9a82007-08-27 16:49:44 -04001566 page = grab_cache_page(mapping, index);
1567 if (!page)
1568 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001569
1570 page_start = page_offset(page);
1571 page_end = page_start + PAGE_CACHE_SIZE - 1;
1572
Chris Masona52d9a82007-08-27 16:49:44 -04001573 if (!PageUptodate(page)) {
1574 ret = btrfs_readpage(NULL, page);
1575 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001576 if (page->mapping != mapping) {
1577 unlock_page(page);
1578 page_cache_release(page);
1579 goto again;
1580 }
Chris Masona52d9a82007-08-27 16:49:44 -04001581 if (!PageUptodate(page)) {
1582 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001583 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001584 }
1585 }
Chris Mason211c17f2008-05-15 09:13:45 -04001586 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001587
1588 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1589 set_page_extent_mapped(page);
1590
1591 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1592 if (ordered) {
1593 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1594 unlock_page(page);
1595 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001596 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001597 btrfs_put_ordered_extent(ordered);
1598 goto again;
1599 }
1600
1601 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1602 page_end, GFP_NOFS);
1603 ret = 0;
1604 if (offset != PAGE_CACHE_SIZE) {
1605 kaddr = kmap(page);
1606 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1607 flush_dcache_page(page);
1608 kunmap(page);
1609 }
Chris Mason247e7432008-07-17 12:53:51 -04001610 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001611 set_page_dirty(page);
1612 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001613
Chris Mason89642222008-07-24 09:41:53 -04001614out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001615 unlock_page(page);
1616 page_cache_release(page);
1617out:
1618 return ret;
1619}
1620
1621static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1622{
1623 struct inode *inode = dentry->d_inode;
1624 int err;
1625
1626 err = inode_change_ok(inode, attr);
1627 if (err)
1628 return err;
1629
1630 if (S_ISREG(inode->i_mode) &&
1631 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1632 struct btrfs_trans_handle *trans;
1633 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001634 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001635
Chris Mason5f39d392007-10-15 16:14:19 -04001636 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001637 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001638 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001639 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001640 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001641
Chris Mason1b0f7c22008-01-30 14:33:02 -05001642 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001643 goto out;
1644
Chris Mason1832a6d2007-12-21 16:27:21 -05001645 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001646 if (err)
1647 goto fail;
1648
Chris Mason39279cc2007-06-12 06:35:45 -04001649 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1650
Chris Mason5f564062008-01-22 16:47:59 -05001651 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001652 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1653 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001654
Chris Mason39279cc2007-06-12 06:35:45 -04001655 trans = btrfs_start_transaction(root, 1);
1656 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001657 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001658 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001659 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001660 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001661
Chris Mason179e29e2007-11-01 11:28:41 -04001662 if (alloc_hint != EXTENT_MAP_INLINE) {
1663 err = btrfs_insert_file_extent(trans, root,
1664 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001665 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001666 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001667 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001668 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001669 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001670 }
Chris Masonee6e6502008-07-17 12:54:40 -04001671 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001672 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001673 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001674 if (err)
1675 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001676 }
1677out:
1678 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001679
1680 if (!err && ((attr->ia_valid & ATTR_MODE)))
1681 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001682fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001683 return err;
1684}
Chris Mason61295eb2008-01-14 16:24:38 -05001685
Chris Mason39279cc2007-06-12 06:35:45 -04001686void btrfs_delete_inode(struct inode *inode)
1687{
1688 struct btrfs_trans_handle *trans;
1689 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001690 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001691 int ret;
1692
1693 truncate_inode_pages(&inode->i_data, 0);
1694 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001695 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001696 goto no_delete;
1697 }
Chris Mason4a096752008-07-21 10:29:44 -04001698 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001699
Chris Masondbe674a2008-07-17 12:54:05 -04001700 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001701 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001702
Chris Mason39279cc2007-06-12 06:35:45 -04001703 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001704 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001705 if (ret) {
1706 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001707 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001708 }
1709
1710 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001711
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001712 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001713 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001714
Chris Mason39279cc2007-06-12 06:35:45 -04001715 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001716 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001717 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001718
1719no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001720 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001721 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001722 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001723no_delete:
1724 clear_inode(inode);
1725}
1726
1727/*
1728 * this returns the key found in the dir entry in the location pointer.
1729 * If no dir entries were found, location->objectid is 0.
1730 */
1731static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1732 struct btrfs_key *location)
1733{
1734 const char *name = dentry->d_name.name;
1735 int namelen = dentry->d_name.len;
1736 struct btrfs_dir_item *di;
1737 struct btrfs_path *path;
1738 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001739 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001740
Chris Mason39544012007-12-12 14:38:19 -05001741 if (namelen == 1 && strcmp(name, ".") == 0) {
1742 location->objectid = dir->i_ino;
1743 location->type = BTRFS_INODE_ITEM_KEY;
1744 location->offset = 0;
1745 return 0;
1746 }
Chris Mason39279cc2007-06-12 06:35:45 -04001747 path = btrfs_alloc_path();
1748 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001749
Chris Mason7a720532007-12-13 09:06:59 -05001750 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001751 struct btrfs_key key;
1752 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001753 int slot;
1754
1755 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001756 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001757 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001758 if (ret < 0 || path->slots[0] == 0)
1759 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001760 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1761 BUG_ON(ret == 0);
1762 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001763 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001764 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001765
1766 btrfs_item_key_to_cpu(leaf, &key, slot);
1767 if (key.objectid != dir->i_ino ||
1768 key.type != BTRFS_INODE_REF_KEY) {
1769 goto out_err;
1770 }
1771 location->objectid = key.offset;
1772 location->type = BTRFS_INODE_ITEM_KEY;
1773 location->offset = 0;
1774 goto out;
1775 }
1776
Chris Mason39279cc2007-06-12 06:35:45 -04001777 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1778 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001779 if (IS_ERR(di))
1780 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001781 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001782 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001783 }
Chris Mason5f39d392007-10-15 16:14:19 -04001784 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001785out:
Chris Mason39279cc2007-06-12 06:35:45 -04001786 btrfs_free_path(path);
1787 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001788out_err:
1789 location->objectid = 0;
1790 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001791}
1792
1793/*
1794 * when we hit a tree root in a directory, the btrfs part of the inode
1795 * needs to be changed to reflect the root directory of the tree root. This
1796 * is kind of like crossing a mount point.
1797 */
1798static int fixup_tree_root_location(struct btrfs_root *root,
1799 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001800 struct btrfs_root **sub_root,
1801 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001802{
Chris Mason39279cc2007-06-12 06:35:45 -04001803 struct btrfs_root_item *ri;
1804
1805 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1806 return 0;
1807 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1808 return 0;
1809
Josef Bacik58176a92007-08-29 15:47:34 -04001810 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1811 dentry->d_name.name,
1812 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001813 if (IS_ERR(*sub_root))
1814 return PTR_ERR(*sub_root);
1815
1816 ri = &(*sub_root)->root_item;
1817 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001818 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1819 location->offset = 0;
1820
Chris Mason39279cc2007-06-12 06:35:45 -04001821 return 0;
1822}
1823
1824static int btrfs_init_locked_inode(struct inode *inode, void *p)
1825{
1826 struct btrfs_iget_args *args = p;
1827 inode->i_ino = args->ino;
1828 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001829 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001830 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001831 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001832 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1833 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001834 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001835 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1836 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001837 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001838 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001839 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001840 return 0;
1841}
1842
1843static int btrfs_find_actor(struct inode *inode, void *opaque)
1844{
1845 struct btrfs_iget_args *args = opaque;
1846 return (args->ino == inode->i_ino &&
1847 args->root == BTRFS_I(inode)->root);
1848}
1849
Chris Masondc17ff82008-01-08 15:46:30 -05001850struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1851 u64 root_objectid)
1852{
1853 struct btrfs_iget_args args;
1854 args.ino = objectid;
1855 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1856
1857 if (!args.root)
1858 return NULL;
1859
1860 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1861}
1862
Chris Mason39279cc2007-06-12 06:35:45 -04001863struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1864 struct btrfs_root *root)
1865{
1866 struct inode *inode;
1867 struct btrfs_iget_args args;
1868 args.ino = objectid;
1869 args.root = root;
1870
1871 inode = iget5_locked(s, objectid, btrfs_find_actor,
1872 btrfs_init_locked_inode,
1873 (void *)&args);
1874 return inode;
1875}
1876
1877static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1878 struct nameidata *nd)
1879{
1880 struct inode * inode;
1881 struct btrfs_inode *bi = BTRFS_I(dir);
1882 struct btrfs_root *root = bi->root;
1883 struct btrfs_root *sub_root = root;
1884 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001885 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001886
1887 if (dentry->d_name.len > BTRFS_NAME_LEN)
1888 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001889
Chris Mason39279cc2007-06-12 06:35:45 -04001890 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001891
Chris Mason39279cc2007-06-12 06:35:45 -04001892 if (ret < 0)
1893 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001894
Chris Mason39279cc2007-06-12 06:35:45 -04001895 inode = NULL;
1896 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001897 ret = fixup_tree_root_location(root, &location, &sub_root,
1898 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001899 if (ret < 0)
1900 return ERR_PTR(ret);
1901 if (ret > 0)
1902 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001903
Chris Mason39279cc2007-06-12 06:35:45 -04001904 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1905 sub_root);
1906 if (!inode)
1907 return ERR_PTR(-EACCES);
1908 if (inode->i_state & I_NEW) {
1909 /* the inode and parent dir are two different roots */
1910 if (sub_root != root) {
1911 igrab(inode);
1912 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001913 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001914 }
1915 BTRFS_I(inode)->root = sub_root;
1916 memcpy(&BTRFS_I(inode)->location, &location,
1917 sizeof(location));
1918 btrfs_read_locked_inode(inode);
1919 unlock_new_inode(inode);
1920 }
1921 }
Josef Bacik7b128762008-07-24 12:17:14 -04001922
1923 if (unlikely(do_orphan))
1924 btrfs_orphan_cleanup(sub_root);
1925
Chris Mason39279cc2007-06-12 06:35:45 -04001926 return d_splice_alias(inode, dentry);
1927}
1928
Chris Mason39279cc2007-06-12 06:35:45 -04001929static unsigned char btrfs_filetype_table[] = {
1930 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1931};
1932
1933static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1934{
Chris Mason6da6aba2007-12-18 16:15:09 -05001935 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001936 struct btrfs_root *root = BTRFS_I(inode)->root;
1937 struct btrfs_item *item;
1938 struct btrfs_dir_item *di;
1939 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001940 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001941 struct btrfs_path *path;
1942 int ret;
1943 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001944 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001945 int slot;
1946 int advance;
1947 unsigned char d_type;
1948 int over = 0;
1949 u32 di_cur;
1950 u32 di_total;
1951 u32 di_len;
1952 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001953 char tmp_name[32];
1954 char *name_ptr;
1955 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001956
1957 /* FIXME, use a real flag for deciding about the key type */
1958 if (root->fs_info->tree_root == root)
1959 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001960
Chris Mason39544012007-12-12 14:38:19 -05001961 /* special case for "." */
1962 if (filp->f_pos == 0) {
1963 over = filldir(dirent, ".", 1,
1964 1, inode->i_ino,
1965 DT_DIR);
1966 if (over)
1967 return 0;
1968 filp->f_pos = 1;
1969 }
1970
Chris Mason39279cc2007-06-12 06:35:45 -04001971 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001972 path = btrfs_alloc_path();
1973 path->reada = 2;
1974
1975 /* special case for .., just use the back ref */
1976 if (filp->f_pos == 1) {
1977 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001978 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001979 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001980 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001981 btrfs_release_path(root, path);
1982 goto read_dir_items;
1983 }
Yan445dceb2008-07-24 12:19:32 -04001984 BUG_ON(ret == 0);
1985 leaf = path->nodes[0];
1986 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001987 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1988 btrfs_release_path(root, path);
1989 if (found_key.objectid != key.objectid ||
1990 found_key.type != BTRFS_INODE_REF_KEY)
1991 goto read_dir_items;
1992 over = filldir(dirent, "..", 2,
1993 2, found_key.offset, DT_DIR);
1994 if (over)
1995 goto nopos;
1996 filp->f_pos = 2;
1997 }
1998
1999read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04002000 btrfs_set_key_type(&key, key_type);
2001 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04002002
Chris Mason39279cc2007-06-12 06:35:45 -04002003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2004 if (ret < 0)
2005 goto err;
2006 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002007 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002008 leaf = path->nodes[0];
2009 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002010 slot = path->slots[0];
2011 if (advance || slot >= nritems) {
2012 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002013 ret = btrfs_next_leaf(root, path);
2014 if (ret)
2015 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002016 leaf = path->nodes[0];
2017 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002018 slot = path->slots[0];
2019 } else {
2020 slot++;
2021 path->slots[0]++;
2022 }
2023 }
2024 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002025 item = btrfs_item_nr(leaf, slot);
2026 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2027
2028 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002029 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002030 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002031 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002032 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002033 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002034
2035 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002036 advance = 1;
2037 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2038 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002039 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04002040 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002041 struct btrfs_key location;
2042
2043 name_len = btrfs_dir_name_len(leaf, di);
2044 if (name_len < 32) {
2045 name_ptr = tmp_name;
2046 } else {
2047 name_ptr = kmalloc(name_len, GFP_NOFS);
2048 BUG_ON(!name_ptr);
2049 }
2050 read_extent_buffer(leaf, name_ptr,
2051 (unsigned long)(di + 1), name_len);
2052
2053 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2054 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002055 over = filldir(dirent, name_ptr, name_len,
2056 found_key.offset,
2057 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002058 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002059
2060 if (name_ptr != tmp_name)
2061 kfree(name_ptr);
2062
Chris Mason39279cc2007-06-12 06:35:45 -04002063 if (over)
2064 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002065 di_len = btrfs_dir_name_len(leaf, di) +
2066 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002067 di_cur += di_len;
2068 di = (struct btrfs_dir_item *)((char *)di + di_len);
2069 }
2070 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002071 if (key_type == BTRFS_DIR_INDEX_KEY)
2072 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2073 else
2074 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002075nopos:
2076 ret = 0;
2077err:
Chris Mason39279cc2007-06-12 06:35:45 -04002078 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002079 return ret;
2080}
2081
2082int btrfs_write_inode(struct inode *inode, int wait)
2083{
2084 struct btrfs_root *root = BTRFS_I(inode)->root;
2085 struct btrfs_trans_handle *trans;
2086 int ret = 0;
2087
2088 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002089 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002090 btrfs_set_trans_block_group(trans, inode);
2091 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002092 }
2093 return ret;
2094}
2095
2096/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002097 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002098 * inode changes. But, it is most likely to find the inode in cache.
2099 * FIXME, needs more benchmarking...there are no reasons other than performance
2100 * to keep or drop this code.
2101 */
2102void btrfs_dirty_inode(struct inode *inode)
2103{
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_trans_handle *trans;
2106
Chris Masonf9295742008-07-17 12:54:14 -04002107 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002108 btrfs_set_trans_block_group(trans, inode);
2109 btrfs_update_inode(trans, root, inode);
2110 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002111}
2112
Josef Bacikaec74772008-07-24 12:12:38 -04002113static int btrfs_set_inode_index_count(struct inode *inode)
2114{
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct btrfs_key key, found_key;
2117 struct btrfs_path *path;
2118 struct extent_buffer *leaf;
2119 int ret;
2120
2121 key.objectid = inode->i_ino;
2122 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2123 key.offset = (u64)-1;
2124
2125 path = btrfs_alloc_path();
2126 if (!path)
2127 return -ENOMEM;
2128
2129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2130 if (ret < 0)
2131 goto out;
2132 /* FIXME: we should be able to handle this */
2133 if (ret == 0)
2134 goto out;
2135 ret = 0;
2136
2137 /*
2138 * MAGIC NUMBER EXPLANATION:
2139 * since we search a directory based on f_pos we have to start at 2
2140 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2141 * else has to start at 2
2142 */
2143 if (path->slots[0] == 0) {
2144 BTRFS_I(inode)->index_cnt = 2;
2145 goto out;
2146 }
2147
2148 path->slots[0]--;
2149
2150 leaf = path->nodes[0];
2151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2152
2153 if (found_key.objectid != inode->i_ino ||
2154 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2155 BTRFS_I(inode)->index_cnt = 2;
2156 goto out;
2157 }
2158
2159 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2160out:
2161 btrfs_free_path(path);
2162 return ret;
2163}
2164
2165static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2166{
2167 int ret = 0;
2168
2169 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2170 ret = btrfs_set_inode_index_count(dir);
2171 if (ret)
2172 return ret;
2173 }
2174
2175 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2176 BTRFS_I(dir)->index_cnt++;
2177
2178 return ret;
2179}
2180
Chris Mason39279cc2007-06-12 06:35:45 -04002181static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002183 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002184 const char *name, int name_len,
2185 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002186 u64 objectid,
2187 struct btrfs_block_group_cache *group,
2188 int mode)
2189{
2190 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002191 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002192 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002193 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002194 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002195 struct btrfs_inode_ref *ref;
2196 struct btrfs_key key[2];
2197 u32 sizes[2];
2198 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002199 int ret;
2200 int owner;
2201
Chris Mason5f39d392007-10-15 16:14:19 -04002202 path = btrfs_alloc_path();
2203 BUG_ON(!path);
2204
Chris Mason39279cc2007-06-12 06:35:45 -04002205 inode = new_inode(root->fs_info->sb);
2206 if (!inode)
2207 return ERR_PTR(-ENOMEM);
2208
Josef Bacikaec74772008-07-24 12:12:38 -04002209 if (dir) {
2210 ret = btrfs_set_inode_index(dir, inode);
2211 if (ret)
2212 return ERR_PTR(ret);
2213 } else {
2214 BTRFS_I(inode)->index = 0;
2215 }
2216 /*
2217 * index_cnt is ignored for everything but a dir,
2218 * btrfs_get_inode_index_count has an explanation for the magic
2219 * number
2220 */
2221 BTRFS_I(inode)->index_cnt = 2;
2222
Chris Masond1310b22008-01-24 16:13:08 -05002223 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2224 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002225 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002226 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2227 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002228 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002229 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002230 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002231 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002232 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002233 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002234
Chris Mason39279cc2007-06-12 06:35:45 -04002235 if (mode & S_IFDIR)
2236 owner = 0;
2237 else
2238 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002239 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002240 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002241 if (!new_inode_group) {
2242 printk("find_block group failed\n");
2243 new_inode_group = group;
2244 }
2245 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002246 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002247
2248 key[0].objectid = objectid;
2249 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2250 key[0].offset = 0;
2251
2252 key[1].objectid = objectid;
2253 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2254 key[1].offset = ref_objectid;
2255
2256 sizes[0] = sizeof(struct btrfs_inode_item);
2257 sizes[1] = name_len + sizeof(*ref);
2258
2259 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2260 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002261 goto fail;
2262
Chris Mason9c583092008-01-29 15:15:18 -05002263 if (objectid > root->highest_inode)
2264 root->highest_inode = objectid;
2265
Chris Mason39279cc2007-06-12 06:35:45 -04002266 inode->i_uid = current->fsuid;
2267 inode->i_gid = current->fsgid;
2268 inode->i_mode = mode;
2269 inode->i_ino = objectid;
2270 inode->i_blocks = 0;
2271 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002272 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2273 struct btrfs_inode_item);
2274 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002275
2276 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2277 struct btrfs_inode_ref);
2278 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002279 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002280 ptr = (unsigned long)(ref + 1);
2281 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2282
Chris Mason5f39d392007-10-15 16:14:19 -04002283 btrfs_mark_buffer_dirty(path->nodes[0]);
2284 btrfs_free_path(path);
2285
Chris Mason39279cc2007-06-12 06:35:45 -04002286 location = &BTRFS_I(inode)->location;
2287 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002288 location->offset = 0;
2289 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2290
Chris Mason39279cc2007-06-12 06:35:45 -04002291 insert_inode_hash(inode);
2292 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002293fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002294 if (dir)
2295 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002296 btrfs_free_path(path);
2297 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002298}
2299
2300static inline u8 btrfs_inode_type(struct inode *inode)
2301{
2302 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2303}
2304
2305static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002306 struct dentry *dentry, struct inode *inode,
2307 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002308{
2309 int ret;
2310 struct btrfs_key key;
2311 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002312 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002313
Chris Mason39279cc2007-06-12 06:35:45 -04002314 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002315 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2316 key.offset = 0;
2317
2318 ret = btrfs_insert_dir_item(trans, root,
2319 dentry->d_name.name, dentry->d_name.len,
2320 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002321 &key, btrfs_inode_type(inode),
2322 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002323 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002324 if (add_backref) {
2325 ret = btrfs_insert_inode_ref(trans, root,
2326 dentry->d_name.name,
2327 dentry->d_name.len,
2328 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002329 parent_inode->i_ino,
2330 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002331 }
Chris Masondbe674a2008-07-17 12:54:05 -04002332 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2333 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002334 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002335 ret = btrfs_update_inode(trans, root,
2336 dentry->d_parent->d_inode);
2337 }
2338 return ret;
2339}
2340
2341static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002342 struct dentry *dentry, struct inode *inode,
2343 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002344{
Chris Mason9c583092008-01-29 15:15:18 -05002345 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002346 if (!err) {
2347 d_instantiate(dentry, inode);
2348 return 0;
2349 }
2350 if (err > 0)
2351 err = -EEXIST;
2352 return err;
2353}
2354
Josef Bacik618e21d2007-07-11 10:18:17 -04002355static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2356 int mode, dev_t rdev)
2357{
2358 struct btrfs_trans_handle *trans;
2359 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002360 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002361 int err;
2362 int drop_inode = 0;
2363 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002364 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002365
2366 if (!new_valid_dev(rdev))
2367 return -EINVAL;
2368
Chris Mason1832a6d2007-12-21 16:27:21 -05002369 err = btrfs_check_free_space(root, 1, 0);
2370 if (err)
2371 goto fail;
2372
Josef Bacik618e21d2007-07-11 10:18:17 -04002373 trans = btrfs_start_transaction(root, 1);
2374 btrfs_set_trans_block_group(trans, dir);
2375
2376 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2377 if (err) {
2378 err = -ENOSPC;
2379 goto out_unlock;
2380 }
2381
Josef Bacikaec74772008-07-24 12:12:38 -04002382 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002383 dentry->d_name.len,
2384 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002385 BTRFS_I(dir)->block_group, mode);
2386 err = PTR_ERR(inode);
2387 if (IS_ERR(inode))
2388 goto out_unlock;
2389
Josef Bacik33268ea2008-07-24 12:16:36 -04002390 err = btrfs_init_acl(inode, dir);
2391 if (err) {
2392 drop_inode = 1;
2393 goto out_unlock;
2394 }
2395
Josef Bacik618e21d2007-07-11 10:18:17 -04002396 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002397 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002398 if (err)
2399 drop_inode = 1;
2400 else {
2401 inode->i_op = &btrfs_special_inode_operations;
2402 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002403 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002404 }
2405 dir->i_sb->s_dirt = 1;
2406 btrfs_update_inode_block_group(trans, inode);
2407 btrfs_update_inode_block_group(trans, dir);
2408out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002409 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002410 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002411fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002412 if (drop_inode) {
2413 inode_dec_link_count(inode);
2414 iput(inode);
2415 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002416 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002417 return err;
2418}
2419
Chris Mason39279cc2007-06-12 06:35:45 -04002420static int btrfs_create(struct inode *dir, struct dentry *dentry,
2421 int mode, struct nameidata *nd)
2422{
2423 struct btrfs_trans_handle *trans;
2424 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002425 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002426 int err;
2427 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002428 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002429 u64 objectid;
2430
Chris Mason1832a6d2007-12-21 16:27:21 -05002431 err = btrfs_check_free_space(root, 1, 0);
2432 if (err)
2433 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002434 trans = btrfs_start_transaction(root, 1);
2435 btrfs_set_trans_block_group(trans, dir);
2436
2437 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2438 if (err) {
2439 err = -ENOSPC;
2440 goto out_unlock;
2441 }
2442
Josef Bacikaec74772008-07-24 12:12:38 -04002443 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002444 dentry->d_name.len,
2445 dentry->d_parent->d_inode->i_ino,
2446 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002447 err = PTR_ERR(inode);
2448 if (IS_ERR(inode))
2449 goto out_unlock;
2450
Josef Bacik33268ea2008-07-24 12:16:36 -04002451 err = btrfs_init_acl(inode, dir);
2452 if (err) {
2453 drop_inode = 1;
2454 goto out_unlock;
2455 }
2456
Chris Mason39279cc2007-06-12 06:35:45 -04002457 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002458 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002459 if (err)
2460 drop_inode = 1;
2461 else {
2462 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002463 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002464 inode->i_fop = &btrfs_file_operations;
2465 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002466 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2467 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002468 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002469 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2470 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002471 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002472 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002473 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002474 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002475 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002476 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002477 }
2478 dir->i_sb->s_dirt = 1;
2479 btrfs_update_inode_block_group(trans, inode);
2480 btrfs_update_inode_block_group(trans, dir);
2481out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002482 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002483 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002484fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002485 if (drop_inode) {
2486 inode_dec_link_count(inode);
2487 iput(inode);
2488 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002489 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002490 return err;
2491}
2492
2493static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2494 struct dentry *dentry)
2495{
2496 struct btrfs_trans_handle *trans;
2497 struct btrfs_root *root = BTRFS_I(dir)->root;
2498 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002499 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002500 int err;
2501 int drop_inode = 0;
2502
2503 if (inode->i_nlink == 0)
2504 return -ENOENT;
2505
Chris Mason6da6aba2007-12-18 16:15:09 -05002506#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2507 inode->i_nlink++;
2508#else
Chris Mason39279cc2007-06-12 06:35:45 -04002509 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002510#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002511 err = btrfs_check_free_space(root, 1, 0);
2512 if (err)
2513 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002514 err = btrfs_set_inode_index(dir, inode);
2515 if (err)
2516 goto fail;
2517
Chris Mason39279cc2007-06-12 06:35:45 -04002518 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002519
Chris Mason39279cc2007-06-12 06:35:45 -04002520 btrfs_set_trans_block_group(trans, dir);
2521 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002522
Chris Mason9c583092008-01-29 15:15:18 -05002523 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002524
Chris Mason39279cc2007-06-12 06:35:45 -04002525 if (err)
2526 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002527
Chris Mason39279cc2007-06-12 06:35:45 -04002528 dir->i_sb->s_dirt = 1;
2529 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002530 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002531
Chris Mason54aa1f42007-06-22 14:16:25 -04002532 if (err)
2533 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002534
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002535 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002536 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002537fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002538 if (drop_inode) {
2539 inode_dec_link_count(inode);
2540 iput(inode);
2541 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002542 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002543 return err;
2544}
2545
Chris Mason39279cc2007-06-12 06:35:45 -04002546static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2547{
Chris Masonb9d86662008-05-02 16:13:49 -04002548 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002549 struct btrfs_trans_handle *trans;
2550 struct btrfs_root *root = BTRFS_I(dir)->root;
2551 int err = 0;
2552 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002553 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002554 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002555
Chris Mason1832a6d2007-12-21 16:27:21 -05002556 err = btrfs_check_free_space(root, 1, 0);
2557 if (err)
2558 goto out_unlock;
2559
Chris Mason39279cc2007-06-12 06:35:45 -04002560 trans = btrfs_start_transaction(root, 1);
2561 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002562
Chris Mason39279cc2007-06-12 06:35:45 -04002563 if (IS_ERR(trans)) {
2564 err = PTR_ERR(trans);
2565 goto out_unlock;
2566 }
2567
2568 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2569 if (err) {
2570 err = -ENOSPC;
2571 goto out_unlock;
2572 }
2573
Josef Bacikaec74772008-07-24 12:12:38 -04002574 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002575 dentry->d_name.len,
2576 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002577 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2578 if (IS_ERR(inode)) {
2579 err = PTR_ERR(inode);
2580 goto out_fail;
2581 }
Chris Mason5f39d392007-10-15 16:14:19 -04002582
Chris Mason39279cc2007-06-12 06:35:45 -04002583 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002584
2585 err = btrfs_init_acl(inode, dir);
2586 if (err)
2587 goto out_fail;
2588
Chris Mason39279cc2007-06-12 06:35:45 -04002589 inode->i_op = &btrfs_dir_inode_operations;
2590 inode->i_fop = &btrfs_dir_file_operations;
2591 btrfs_set_trans_block_group(trans, inode);
2592
Chris Masondbe674a2008-07-17 12:54:05 -04002593 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002594 err = btrfs_update_inode(trans, root, inode);
2595 if (err)
2596 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002597
Chris Mason9c583092008-01-29 15:15:18 -05002598 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002599 if (err)
2600 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002601
Chris Mason39279cc2007-06-12 06:35:45 -04002602 d_instantiate(dentry, inode);
2603 drop_on_err = 0;
2604 dir->i_sb->s_dirt = 1;
2605 btrfs_update_inode_block_group(trans, inode);
2606 btrfs_update_inode_block_group(trans, dir);
2607
2608out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002609 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002610 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002611
Chris Mason39279cc2007-06-12 06:35:45 -04002612out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002613 if (drop_on_err)
2614 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002615 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002616 return err;
2617}
2618
Chris Mason3b951512008-04-17 11:29:12 -04002619static int merge_extent_mapping(struct extent_map_tree *em_tree,
2620 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621 struct extent_map *em,
2622 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002623{
2624 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002625
Chris Masone6dcd2d2008-07-17 12:53:50 -04002626 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2627 start_diff = map_start - em->start;
2628 em->start = map_start;
2629 em->len = map_len;
2630 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2631 em->block_start += start_diff;
2632 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002633}
2634
Chris Masona52d9a82007-08-27 16:49:44 -04002635struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002636 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002637 int create)
2638{
2639 int ret;
2640 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002641 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002642 u64 extent_start = 0;
2643 u64 extent_end = 0;
2644 u64 objectid = inode->i_ino;
2645 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002646 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002647 struct btrfs_root *root = BTRFS_I(inode)->root;
2648 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002649 struct extent_buffer *leaf;
2650 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002651 struct extent_map *em = NULL;
2652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002653 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002654 struct btrfs_trans_handle *trans = NULL;
2655
Chris Masona52d9a82007-08-27 16:49:44 -04002656again:
Chris Masond1310b22008-01-24 16:13:08 -05002657 spin_lock(&em_tree->lock);
2658 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002659 if (em)
2660 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002661 spin_unlock(&em_tree->lock);
2662
Chris Masona52d9a82007-08-27 16:49:44 -04002663 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002664 if (em->start > start || em->start + em->len <= start)
2665 free_extent_map(em);
2666 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002667 free_extent_map(em);
2668 else
2669 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002670 }
Chris Masond1310b22008-01-24 16:13:08 -05002671 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002672 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002673 err = -ENOMEM;
2674 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002675 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002676 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002677 em->start = EXTENT_MAP_HOLE;
2678 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002679
2680 if (!path) {
2681 path = btrfs_alloc_path();
2682 BUG_ON(!path);
2683 }
2684
Chris Mason179e29e2007-11-01 11:28:41 -04002685 ret = btrfs_lookup_file_extent(trans, root, path,
2686 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002687 if (ret < 0) {
2688 err = ret;
2689 goto out;
2690 }
2691
2692 if (ret != 0) {
2693 if (path->slots[0] == 0)
2694 goto not_found;
2695 path->slots[0]--;
2696 }
2697
Chris Mason5f39d392007-10-15 16:14:19 -04002698 leaf = path->nodes[0];
2699 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002700 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002701 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002702 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2703 found_type = btrfs_key_type(&found_key);
2704 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002705 found_type != BTRFS_EXTENT_DATA_KEY) {
2706 goto not_found;
2707 }
2708
Chris Mason5f39d392007-10-15 16:14:19 -04002709 found_type = btrfs_file_extent_type(leaf, item);
2710 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002711 if (found_type == BTRFS_FILE_EXTENT_REG) {
2712 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002713 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002714 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002715 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002716 em->start = start;
2717 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002718 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002719 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002720 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002721 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002722 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002723 }
2724 goto not_found_em;
2725 }
Chris Masondb945352007-10-15 16:15:53 -04002726 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2727 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002728 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002729 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002730 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002731 goto insert;
2732 }
Chris Masondb945352007-10-15 16:15:53 -04002733 bytenr += btrfs_file_extent_offset(leaf, item);
2734 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002735 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002736 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002737 goto insert;
2738 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002739 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002740 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002741 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002742 size_t size;
2743 size_t extent_offset;
2744 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002745
Chris Mason5f39d392007-10-15 16:14:19 -04002746 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2747 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002748 extent_end = (extent_start + size + root->sectorsize - 1) &
2749 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002750 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002751 em->start = start;
2752 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002753 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002754 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002755 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002756 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002757 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002758 }
2759 goto not_found_em;
2760 }
2761 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002762
2763 if (!page) {
2764 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002765 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002766 goto out;
2767 }
2768
Chris Mason70dec802008-01-29 09:59:12 -05002769 page_start = page_offset(page) + pg_offset;
2770 extent_offset = page_start - extent_start;
2771 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002772 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002773 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002774 em->len = (copy_size + root->sectorsize - 1) &
2775 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002776 map = kmap(page);
2777 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002778 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002779 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002780 copy_size);
2781 flush_dcache_page(page);
2782 } else if (create && PageUptodate(page)) {
2783 if (!trans) {
2784 kunmap(page);
2785 free_extent_map(em);
2786 em = NULL;
2787 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002788 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002789 goto again;
2790 }
Chris Mason70dec802008-01-29 09:59:12 -05002791 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002792 copy_size);
2793 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002794 }
Chris Masona52d9a82007-08-27 16:49:44 -04002795 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002796 set_extent_uptodate(io_tree, em->start,
2797 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002798 goto insert;
2799 } else {
2800 printk("unkknown found_type %d\n", found_type);
2801 WARN_ON(1);
2802 }
2803not_found:
2804 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002805 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002806not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002807 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002808insert:
2809 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002810 if (em->start > start || extent_map_end(em) <= start) {
2811 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002812 err = -EIO;
2813 goto out;
2814 }
Chris Masond1310b22008-01-24 16:13:08 -05002815
2816 err = 0;
2817 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002818 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002819 /* it is possible that someone inserted the extent into the tree
2820 * while we had the lock dropped. It is also possible that
2821 * an overlapping map exists in the tree
2822 */
Chris Masona52d9a82007-08-27 16:49:44 -04002823 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002824 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002825
2826 ret = 0;
2827
Chris Mason3b951512008-04-17 11:29:12 -04002828 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002829 if (existing && (existing->start > start ||
2830 existing->start + existing->len <= start)) {
2831 free_extent_map(existing);
2832 existing = NULL;
2833 }
Chris Mason3b951512008-04-17 11:29:12 -04002834 if (!existing) {
2835 existing = lookup_extent_mapping(em_tree, em->start,
2836 em->len);
2837 if (existing) {
2838 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002839 em, start,
2840 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002841 free_extent_map(existing);
2842 if (err) {
2843 free_extent_map(em);
2844 em = NULL;
2845 }
2846 } else {
2847 err = -EIO;
2848 printk("failing to insert %Lu %Lu\n",
2849 start, len);
2850 free_extent_map(em);
2851 em = NULL;
2852 }
2853 } else {
2854 free_extent_map(em);
2855 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002856 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002857 }
Chris Masona52d9a82007-08-27 16:49:44 -04002858 }
Chris Masond1310b22008-01-24 16:13:08 -05002859 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002860out:
Chris Masonf4219502008-07-22 11:18:09 -04002861 if (path)
2862 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002863 if (trans) {
2864 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002865 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002866 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002867 }
Chris Masona52d9a82007-08-27 16:49:44 -04002868 }
Chris Masona52d9a82007-08-27 16:49:44 -04002869 if (err) {
2870 free_extent_map(em);
2871 WARN_ON(1);
2872 return ERR_PTR(err);
2873 }
2874 return em;
2875}
2876
Chris Masone1c4b742008-04-22 13:26:46 -04002877#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002878static int btrfs_get_block(struct inode *inode, sector_t iblock,
2879 struct buffer_head *bh_result, int create)
2880{
2881 struct extent_map *em;
2882 u64 start = (u64)iblock << inode->i_blkbits;
2883 struct btrfs_multi_bio *multi = NULL;
2884 struct btrfs_root *root = BTRFS_I(inode)->root;
2885 u64 len;
2886 u64 logical;
2887 u64 map_length;
2888 int ret = 0;
2889
2890 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2891
2892 if (!em || IS_ERR(em))
2893 goto out;
2894
Chris Masone1c4b742008-04-22 13:26:46 -04002895 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002896 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002897 }
Chris Mason16432982008-04-10 10:23:21 -04002898
2899 if (em->block_start == EXTENT_MAP_INLINE) {
2900 ret = -EINVAL;
2901 goto out;
2902 }
2903
Chris Mason16432982008-04-10 10:23:21 -04002904 len = em->start + em->len - start;
2905 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2906
Chris Masone1c4b742008-04-22 13:26:46 -04002907 if (em->block_start == EXTENT_MAP_HOLE ||
2908 em->block_start == EXTENT_MAP_DELALLOC) {
2909 bh_result->b_size = len;
2910 goto out;
2911 }
2912
Chris Mason16432982008-04-10 10:23:21 -04002913 logical = start - em->start;
2914 logical = em->block_start + logical;
2915
2916 map_length = len;
2917 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2918 logical, &map_length, &multi, 0);
2919 BUG_ON(ret);
2920 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2921 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002922
Chris Mason16432982008-04-10 10:23:21 -04002923 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2924 set_buffer_mapped(bh_result);
2925 kfree(multi);
2926out:
2927 free_extent_map(em);
2928 return ret;
2929}
Chris Masone1c4b742008-04-22 13:26:46 -04002930#endif
Chris Mason16432982008-04-10 10:23:21 -04002931
2932static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2933 const struct iovec *iov, loff_t offset,
2934 unsigned long nr_segs)
2935{
Chris Masone1c4b742008-04-22 13:26:46 -04002936 return -EINVAL;
2937#if 0
Chris Mason16432982008-04-10 10:23:21 -04002938 struct file *file = iocb->ki_filp;
2939 struct inode *inode = file->f_mapping->host;
2940
2941 if (rw == WRITE)
2942 return -EINVAL;
2943
2944 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2945 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002946#endif
Chris Mason16432982008-04-10 10:23:21 -04002947}
2948
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002949static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002950{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002951 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002952}
2953
Chris Mason9ebefb182007-06-15 13:50:00 -04002954int btrfs_readpage(struct file *file, struct page *page)
2955{
Chris Masond1310b22008-01-24 16:13:08 -05002956 struct extent_io_tree *tree;
2957 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002958 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002959}
Chris Mason1832a6d2007-12-21 16:27:21 -05002960
Chris Mason39279cc2007-06-12 06:35:45 -04002961static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2962{
Chris Masond1310b22008-01-24 16:13:08 -05002963 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002964
2965
2966 if (current->flags & PF_MEMALLOC) {
2967 redirty_page_for_writepage(wbc, page);
2968 unlock_page(page);
2969 return 0;
2970 }
Chris Masond1310b22008-01-24 16:13:08 -05002971 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002972 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2973}
Chris Mason39279cc2007-06-12 06:35:45 -04002974
Chris Masonf4219502008-07-22 11:18:09 -04002975int btrfs_writepages(struct address_space *mapping,
2976 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002977{
Chris Masond1310b22008-01-24 16:13:08 -05002978 struct extent_io_tree *tree;
2979 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002980 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2981}
2982
Chris Mason3ab2fb52007-11-08 10:59:22 -05002983static int
2984btrfs_readpages(struct file *file, struct address_space *mapping,
2985 struct list_head *pages, unsigned nr_pages)
2986{
Chris Masond1310b22008-01-24 16:13:08 -05002987 struct extent_io_tree *tree;
2988 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002989 return extent_readpages(tree, mapping, pages, nr_pages,
2990 btrfs_get_extent);
2991}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002992static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002993{
Chris Masond1310b22008-01-24 16:13:08 -05002994 struct extent_io_tree *tree;
2995 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002996 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002997
Chris Masond1310b22008-01-24 16:13:08 -05002998 tree = &BTRFS_I(page->mapping->host)->io_tree;
2999 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003000 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003001 if (ret == 1) {
3002 ClearPagePrivate(page);
3003 set_page_private(page, 0);
3004 page_cache_release(page);
3005 }
3006 return ret;
3007}
Chris Mason39279cc2007-06-12 06:35:45 -04003008
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3010{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003011 return __btrfs_releasepage(page, gfp_flags);
3012}
3013
Chris Masona52d9a82007-08-27 16:49:44 -04003014static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3015{
Chris Masond1310b22008-01-24 16:13:08 -05003016 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003017 struct btrfs_ordered_extent *ordered;
3018 u64 page_start = page_offset(page);
3019 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003020
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003022 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023 if (offset) {
3024 btrfs_releasepage(page, GFP_NOFS);
3025 return;
3026 }
3027
3028 lock_extent(tree, page_start, page_end, GFP_NOFS);
3029 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3030 page_offset(page));
3031 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003032 /*
3033 * IO on this page will never be started, so we need
3034 * to account for any ordered extents now
3035 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036 clear_extent_bit(tree, page_start, page_end,
3037 EXTENT_DIRTY | EXTENT_DELALLOC |
3038 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003039 btrfs_finish_ordered_io(page->mapping->host,
3040 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041 btrfs_put_ordered_extent(ordered);
3042 lock_extent(tree, page_start, page_end, GFP_NOFS);
3043 }
3044 clear_extent_bit(tree, page_start, page_end,
3045 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3046 EXTENT_ORDERED,
3047 1, 1, GFP_NOFS);
3048 __btrfs_releasepage(page, GFP_NOFS);
3049
Chris Mason4a096752008-07-21 10:29:44 -04003050 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003051 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003052 ClearPagePrivate(page);
3053 set_page_private(page, 0);
3054 page_cache_release(page);
3055 }
Chris Mason39279cc2007-06-12 06:35:45 -04003056}
3057
Chris Mason9ebefb182007-06-15 13:50:00 -04003058/*
3059 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3060 * called from a page fault handler when a page is first dirtied. Hence we must
3061 * be careful to check for EOF conditions here. We set the page up correctly
3062 * for a written page which means we get ENOSPC checking when writing into
3063 * holes and correct delalloc and unwritten extent mapping on filesystems that
3064 * support these features.
3065 *
3066 * We are not allowed to take the i_mutex here so we have to play games to
3067 * protect against truncate races as the page could now be beyond EOF. Because
3068 * vmtruncate() writes the inode size before removing pages, once we have the
3069 * page lock we can determine safely if the page is beyond EOF. If it is not
3070 * beyond EOF, then the page is guaranteed safe against truncation until we
3071 * unlock the page.
3072 */
3073int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3074{
Chris Mason6da6aba2007-12-18 16:15:09 -05003075 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003076 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003077 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3078 struct btrfs_ordered_extent *ordered;
3079 char *kaddr;
3080 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003081 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003082 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003083 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003084 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003085
Chris Mason1832a6d2007-12-21 16:27:21 -05003086 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003087 if (ret)
3088 goto out;
3089
3090 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003091again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003092 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003093 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003094 page_start = page_offset(page);
3095 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003096
Chris Mason9ebefb182007-06-15 13:50:00 -04003097 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003098 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003099 /* page got truncated out from underneath us */
3100 goto out_unlock;
3101 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003102 wait_on_page_writeback(page);
3103
3104 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3105 set_page_extent_mapped(page);
3106
Chris Masoneb84ae02008-07-17 13:53:27 -04003107 /*
3108 * we can't set the delalloc bits if there are pending ordered
3109 * extents. Drop our locks and wait for them to finish
3110 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3112 if (ordered) {
3113 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3114 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003115 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003116 btrfs_put_ordered_extent(ordered);
3117 goto again;
3118 }
3119
3120 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3121 page_end, GFP_NOFS);
3122 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003123
3124 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003125 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003126 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003127 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003128 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003129
Chris Masone6dcd2d2008-07-17 12:53:50 -04003130 if (zero_start != PAGE_CACHE_SIZE) {
3131 kaddr = kmap(page);
3132 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3133 flush_dcache_page(page);
3134 kunmap(page);
3135 }
Chris Mason247e7432008-07-17 12:53:51 -04003136 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003137 set_page_dirty(page);
3138 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003139
3140out_unlock:
3141 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003142out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003143 return ret;
3144}
3145
Chris Mason39279cc2007-06-12 06:35:45 -04003146static void btrfs_truncate(struct inode *inode)
3147{
3148 struct btrfs_root *root = BTRFS_I(inode)->root;
3149 int ret;
3150 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003151 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003152 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003153
3154 if (!S_ISREG(inode->i_mode))
3155 return;
3156 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3157 return;
3158
3159 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003160 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003161
Chris Mason39279cc2007-06-12 06:35:45 -04003162 trans = btrfs_start_transaction(root, 1);
3163 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003164 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003165
Josef Bacik7b128762008-07-24 12:17:14 -04003166 ret = btrfs_orphan_add(trans, inode);
3167 if (ret)
3168 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003169 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003170 ret = btrfs_truncate_in_trans(trans, root, inode,
3171 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003172 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003173
Josef Bacik7b128762008-07-24 12:17:14 -04003174 ret = btrfs_orphan_del(trans, inode);
3175 BUG_ON(ret);
3176
3177out:
3178 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003179 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003180 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003181 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003182}
3183
Sven Wegener3b963622008-06-09 21:57:42 -04003184/*
3185 * Invalidate a single dcache entry at the root of the filesystem.
3186 * Needed after creation of snapshot or subvolume.
3187 */
3188void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3189 int namelen)
3190{
3191 struct dentry *alias, *entry;
3192 struct qstr qstr;
3193
3194 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3195 if (alias) {
3196 qstr.name = name;
3197 qstr.len = namelen;
3198 /* change me if btrfs ever gets a d_hash operation */
3199 qstr.hash = full_name_hash(qstr.name, qstr.len);
3200 entry = d_lookup(alias, &qstr);
3201 dput(alias);
3202 if (entry) {
3203 d_invalidate(entry);
3204 dput(entry);
3205 }
3206 }
3207}
3208
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003209int btrfs_create_subvol_root(struct btrfs_root *new_root,
3210 struct btrfs_trans_handle *trans, u64 new_dirid,
3211 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003212{
Chris Mason39279cc2007-06-12 06:35:45 -04003213 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003214
Josef Bacikaec74772008-07-24 12:12:38 -04003215 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003216 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003217 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003218 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003219 inode->i_op = &btrfs_dir_inode_operations;
3220 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003221 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003222
Chris Mason39279cc2007-06-12 06:35:45 -04003223 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003224 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003225
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003226 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003227}
3228
Chris Masonedbd8d42007-12-21 16:27:24 -05003229unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003230 struct file_ra_state *ra, struct file *file,
3231 pgoff_t offset, pgoff_t last_index)
3232{
Chris Mason8e7bf942008-04-28 09:02:36 -04003233 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003234
3235#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003236 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3237 return offset;
3238#else
Chris Mason86479a02007-09-10 19:58:16 -04003239 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3240 return offset + req_size;
3241#endif
3242}
3243
Chris Mason39279cc2007-06-12 06:35:45 -04003244struct inode *btrfs_alloc_inode(struct super_block *sb)
3245{
3246 struct btrfs_inode *ei;
3247
3248 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3249 if (!ei)
3250 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003251 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003252 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003253 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3254 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003255 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003256 return &ei->vfs_inode;
3257}
3258
3259void btrfs_destroy_inode(struct inode *inode)
3260{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003261 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003262 WARN_ON(!list_empty(&inode->i_dentry));
3263 WARN_ON(inode->i_data.nrpages);
3264
Josef Bacik33268ea2008-07-24 12:16:36 -04003265 if (BTRFS_I(inode)->i_acl &&
3266 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3267 posix_acl_release(BTRFS_I(inode)->i_acl);
3268 if (BTRFS_I(inode)->i_default_acl &&
3269 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3270 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3271
Yanbcc63ab2008-07-30 16:29:20 -04003272 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003273 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3274 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3275 " list\n", inode->i_ino);
3276 dump_stack();
3277 }
Yanbcc63ab2008-07-30 16:29:20 -04003278 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003279
Chris Masone6dcd2d2008-07-17 12:53:50 -04003280 while(1) {
3281 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3282 if (!ordered)
3283 break;
3284 else {
3285 printk("found ordered extent %Lu %Lu\n",
3286 ordered->file_offset, ordered->len);
3287 btrfs_remove_ordered_extent(inode, ordered);
3288 btrfs_put_ordered_extent(ordered);
3289 btrfs_put_ordered_extent(ordered);
3290 }
3291 }
Chris Mason8c416c92008-01-14 15:10:26 -05003292 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003293 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3294}
3295
Chris Mason44ec0b72007-10-29 10:55:05 -04003296#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3297static void init_once(struct kmem_cache * cachep, void *foo)
3298#else
Chris Mason39279cc2007-06-12 06:35:45 -04003299static void init_once(void * foo, struct kmem_cache * cachep,
3300 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003301#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003302{
3303 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3304
3305 inode_init_once(&ei->vfs_inode);
3306}
3307
3308void btrfs_destroy_cachep(void)
3309{
3310 if (btrfs_inode_cachep)
3311 kmem_cache_destroy(btrfs_inode_cachep);
3312 if (btrfs_trans_handle_cachep)
3313 kmem_cache_destroy(btrfs_trans_handle_cachep);
3314 if (btrfs_transaction_cachep)
3315 kmem_cache_destroy(btrfs_transaction_cachep);
3316 if (btrfs_bit_radix_cachep)
3317 kmem_cache_destroy(btrfs_bit_radix_cachep);
3318 if (btrfs_path_cachep)
3319 kmem_cache_destroy(btrfs_path_cachep);
3320}
3321
Chris Mason86479a02007-09-10 19:58:16 -04003322struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003323 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003324#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3325 void (*ctor)(struct kmem_cache *, void *)
3326#else
Chris Mason92fee662007-07-25 12:31:35 -04003327 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003328 unsigned long)
3329#endif
3330 )
Chris Mason92fee662007-07-25 12:31:35 -04003331{
3332 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3333 SLAB_MEM_SPREAD | extra_flags), ctor
3334#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3335 ,NULL
3336#endif
3337 );
3338}
3339
Chris Mason39279cc2007-06-12 06:35:45 -04003340int btrfs_init_cachep(void)
3341{
Chris Mason86479a02007-09-10 19:58:16 -04003342 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003343 sizeof(struct btrfs_inode),
3344 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003345 if (!btrfs_inode_cachep)
3346 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003347 btrfs_trans_handle_cachep =
3348 btrfs_cache_create("btrfs_trans_handle_cache",
3349 sizeof(struct btrfs_trans_handle),
3350 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003351 if (!btrfs_trans_handle_cachep)
3352 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003353 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003354 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003355 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003356 if (!btrfs_transaction_cachep)
3357 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003358 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003359 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003360 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003361 if (!btrfs_path_cachep)
3362 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003363 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003364 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003365 if (!btrfs_bit_radix_cachep)
3366 goto fail;
3367 return 0;
3368fail:
3369 btrfs_destroy_cachep();
3370 return -ENOMEM;
3371}
3372
3373static int btrfs_getattr(struct vfsmount *mnt,
3374 struct dentry *dentry, struct kstat *stat)
3375{
3376 struct inode *inode = dentry->d_inode;
3377 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003378 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003379 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003380 return 0;
3381}
3382
3383static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3384 struct inode * new_dir,struct dentry *new_dentry)
3385{
3386 struct btrfs_trans_handle *trans;
3387 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3388 struct inode *new_inode = new_dentry->d_inode;
3389 struct inode *old_inode = old_dentry->d_inode;
3390 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003391 int ret;
3392
3393 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3394 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3395 return -ENOTEMPTY;
3396 }
Chris Mason5f39d392007-10-15 16:14:19 -04003397
Chris Mason1832a6d2007-12-21 16:27:21 -05003398 ret = btrfs_check_free_space(root, 1, 0);
3399 if (ret)
3400 goto out_unlock;
3401
Chris Mason39279cc2007-06-12 06:35:45 -04003402 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003403
Chris Mason39279cc2007-06-12 06:35:45 -04003404 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003405
3406 old_dentry->d_inode->i_nlink++;
3407 old_dir->i_ctime = old_dir->i_mtime = ctime;
3408 new_dir->i_ctime = new_dir->i_mtime = ctime;
3409 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003410
Chris Mason39279cc2007-06-12 06:35:45 -04003411 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3412 if (ret)
3413 goto out_fail;
3414
3415 if (new_inode) {
3416 new_inode->i_ctime = CURRENT_TIME;
3417 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3418 if (ret)
3419 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003420 if (new_inode->i_nlink == 0) {
3421 ret = btrfs_orphan_add(trans, new_inode);
3422 if (ret)
3423 goto out_fail;
3424 }
Chris Mason39279cc2007-06-12 06:35:45 -04003425 }
Josef Bacikaec74772008-07-24 12:12:38 -04003426 ret = btrfs_set_inode_index(new_dir, old_inode);
3427 if (ret)
3428 goto out_fail;
3429
Chris Mason9c583092008-01-29 15:15:18 -05003430 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003431 if (ret)
3432 goto out_fail;
3433
3434out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003435 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003436out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003437 return ret;
3438}
3439
3440static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3441 const char *symname)
3442{
3443 struct btrfs_trans_handle *trans;
3444 struct btrfs_root *root = BTRFS_I(dir)->root;
3445 struct btrfs_path *path;
3446 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003447 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003448 int err;
3449 int drop_inode = 0;
3450 u64 objectid;
3451 int name_len;
3452 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003453 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003454 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003455 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003456 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003457
3458 name_len = strlen(symname) + 1;
3459 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3460 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003461
Chris Mason1832a6d2007-12-21 16:27:21 -05003462 err = btrfs_check_free_space(root, 1, 0);
3463 if (err)
3464 goto out_fail;
3465
Chris Mason39279cc2007-06-12 06:35:45 -04003466 trans = btrfs_start_transaction(root, 1);
3467 btrfs_set_trans_block_group(trans, dir);
3468
3469 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3470 if (err) {
3471 err = -ENOSPC;
3472 goto out_unlock;
3473 }
3474
Josef Bacikaec74772008-07-24 12:12:38 -04003475 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003476 dentry->d_name.len,
3477 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003478 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3479 err = PTR_ERR(inode);
3480 if (IS_ERR(inode))
3481 goto out_unlock;
3482
Josef Bacik33268ea2008-07-24 12:16:36 -04003483 err = btrfs_init_acl(inode, dir);
3484 if (err) {
3485 drop_inode = 1;
3486 goto out_unlock;
3487 }
3488
Chris Mason39279cc2007-06-12 06:35:45 -04003489 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003490 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003491 if (err)
3492 drop_inode = 1;
3493 else {
3494 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003495 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003496 inode->i_fop = &btrfs_file_operations;
3497 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003498 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3499 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003500 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003501 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3502 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003503 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003504 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003505 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003506 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003507 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003508 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003509 }
3510 dir->i_sb->s_dirt = 1;
3511 btrfs_update_inode_block_group(trans, inode);
3512 btrfs_update_inode_block_group(trans, dir);
3513 if (drop_inode)
3514 goto out_unlock;
3515
3516 path = btrfs_alloc_path();
3517 BUG_ON(!path);
3518 key.objectid = inode->i_ino;
3519 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003520 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3521 datasize = btrfs_file_extent_calc_inline_size(name_len);
3522 err = btrfs_insert_empty_item(trans, root, path, &key,
3523 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003524 if (err) {
3525 drop_inode = 1;
3526 goto out_unlock;
3527 }
Chris Mason5f39d392007-10-15 16:14:19 -04003528 leaf = path->nodes[0];
3529 ei = btrfs_item_ptr(leaf, path->slots[0],
3530 struct btrfs_file_extent_item);
3531 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3532 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003533 BTRFS_FILE_EXTENT_INLINE);
3534 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003535 write_extent_buffer(leaf, symname, ptr, name_len);
3536 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003537 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003538
Chris Mason39279cc2007-06-12 06:35:45 -04003539 inode->i_op = &btrfs_symlink_inode_operations;
3540 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003541 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003542 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003543 err = btrfs_update_inode(trans, root, inode);
3544 if (err)
3545 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003546
3547out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003548 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003549 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003550out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003551 if (drop_inode) {
3552 inode_dec_link_count(inode);
3553 iput(inode);
3554 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003555 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003556 return err;
3557}
Chris Mason16432982008-04-10 10:23:21 -04003558
Chris Masone6dcd2d2008-07-17 12:53:50 -04003559static int btrfs_set_page_dirty(struct page *page)
3560{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003561 return __set_page_dirty_nobuffers(page);
3562}
3563
Yanfdebe2b2008-01-14 13:26:08 -05003564static int btrfs_permission(struct inode *inode, int mask,
3565 struct nameidata *nd)
3566{
3567 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3568 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003569 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003570}
Chris Mason39279cc2007-06-12 06:35:45 -04003571
3572static struct inode_operations btrfs_dir_inode_operations = {
3573 .lookup = btrfs_lookup,
3574 .create = btrfs_create,
3575 .unlink = btrfs_unlink,
3576 .link = btrfs_link,
3577 .mkdir = btrfs_mkdir,
3578 .rmdir = btrfs_rmdir,
3579 .rename = btrfs_rename,
3580 .symlink = btrfs_symlink,
3581 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003582 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003583 .setxattr = generic_setxattr,
3584 .getxattr = generic_getxattr,
3585 .listxattr = btrfs_listxattr,
3586 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003587 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003588};
Chris Mason39279cc2007-06-12 06:35:45 -04003589static struct inode_operations btrfs_dir_ro_inode_operations = {
3590 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003591 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003592};
Chris Mason39279cc2007-06-12 06:35:45 -04003593static struct file_operations btrfs_dir_file_operations = {
3594 .llseek = generic_file_llseek,
3595 .read = generic_read_dir,
3596 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003597 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003598#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003599 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003600#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003601 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003602};
3603
Chris Masond1310b22008-01-24 16:13:08 -05003604static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003605 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003606 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003607 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003608 .readpage_io_hook = btrfs_readpage_io_hook,
3609 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003610 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003611 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003612 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003613 .set_bit_hook = btrfs_set_bit_hook,
3614 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003615};
3616
Chris Mason39279cc2007-06-12 06:35:45 -04003617static struct address_space_operations btrfs_aops = {
3618 .readpage = btrfs_readpage,
3619 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003620 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003621 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003622 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003623 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003624 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003625 .invalidatepage = btrfs_invalidatepage,
3626 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003627 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003628};
3629
3630static struct address_space_operations btrfs_symlink_aops = {
3631 .readpage = btrfs_readpage,
3632 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003633 .invalidatepage = btrfs_invalidatepage,
3634 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003635};
3636
3637static struct inode_operations btrfs_file_inode_operations = {
3638 .truncate = btrfs_truncate,
3639 .getattr = btrfs_getattr,
3640 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003641 .setxattr = generic_setxattr,
3642 .getxattr = generic_getxattr,
3643 .listxattr = btrfs_listxattr,
3644 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003645 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003646};
Josef Bacik618e21d2007-07-11 10:18:17 -04003647static struct inode_operations btrfs_special_inode_operations = {
3648 .getattr = btrfs_getattr,
3649 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003650 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003651 .setxattr = generic_setxattr,
3652 .getxattr = generic_getxattr,
3653 .listxattr = btrfs_listxattr,
3654 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003655};
Chris Mason39279cc2007-06-12 06:35:45 -04003656static struct inode_operations btrfs_symlink_inode_operations = {
3657 .readlink = generic_readlink,
3658 .follow_link = page_follow_link_light,
3659 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003660 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003661};