blob: 50ee4befac8ec5e3a1f206dcb0bf4a2fbe389cef [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>
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040046#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047
48struct btrfs_iget_args {
49 u64 ino;
50 struct btrfs_root *root;
51};
52
53static struct inode_operations btrfs_dir_inode_operations;
54static struct inode_operations btrfs_symlink_inode_operations;
55static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040056static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040057static struct inode_operations btrfs_file_inode_operations;
58static struct address_space_operations btrfs_aops;
59static struct address_space_operations btrfs_symlink_aops;
60static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050061static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040062
63static struct kmem_cache *btrfs_inode_cachep;
64struct kmem_cache *btrfs_trans_handle_cachep;
65struct kmem_cache *btrfs_transaction_cachep;
66struct kmem_cache *btrfs_bit_radix_cachep;
67struct kmem_cache *btrfs_path_cachep;
68
69#define S_SHIFT 12
70static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
71 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
72 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
73 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
74 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
75 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
76 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
77 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
78};
79
Chris Mason1832a6d2007-12-21 16:27:21 -050080int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
81 int for_del)
82{
Chris Masona2135012008-06-25 16:01:30 -040083 u64 total;
84 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040086 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 int ret = 0;
88
Chris Masona2135012008-06-25 16:01:30 -040089 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
90 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
91 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050092 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050093 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050094 else
Chris Masonf9ef6602008-01-03 09:22:38 -050095 thresh = total * 85;
96
97 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050098
Chris Mason1832a6d2007-12-21 16:27:21 -050099 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
100 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400101 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 return ret;
103}
104
Chris Masonbe20aa92007-12-17 20:14:01 -0500105static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400106{
107 struct btrfs_root *root = BTRFS_I(inode)->root;
108 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400109 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400110 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500111 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400112 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500113 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400115 struct extent_map *em;
116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
117 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400118
Chris Masonf9295742008-07-17 12:54:14 -0400119 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400120 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500121 btrfs_set_trans_block_group(trans, inode);
122
Chris Masondb945352007-10-15 16:15:53 -0400123 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500125 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400126
Chris Mason179e29e2007-11-01 11:28:41 -0400127 if (alloc_hint == EXTENT_MAP_INLINE)
128 goto out;
129
Chris Mason3b951512008-04-17 11:29:12 -0400130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400131 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400132 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400133 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400134
Chris Masonc59f8952007-12-17 20:14:04 -0500135 while(num_bytes > 0) {
136 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400137 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
138 root->sectorsize, 0, 0,
139 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500140 if (ret) {
141 WARN_ON(1);
142 goto out;
143 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400144 em = alloc_extent_map(GFP_NOFS);
145 em->start = start;
146 em->len = ins.offset;
147 em->block_start = ins.objectid;
148 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400149 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400150 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400151 while(1) {
152 spin_lock(&em_tree->lock);
153 ret = add_extent_mapping(em_tree, em);
154 spin_unlock(&em_tree->lock);
155 if (ret != -EEXIST) {
156 free_extent_map(em);
157 break;
158 }
159 btrfs_drop_extent_cache(inode, start,
160 start + ins.offset - 1);
161 }
Chris Masone5a22172008-07-18 20:42:20 -0400162 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400163
Chris Mason98d20f62008-04-14 09:46:10 -0400164 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400165 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
166 ins.offset);
167 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400168 if (num_bytes < cur_alloc_size) {
169 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
170 cur_alloc_size);
171 break;
172 }
Chris Masonc59f8952007-12-17 20:14:04 -0500173 num_bytes -= cur_alloc_size;
174 alloc_hint = ins.objectid + ins.offset;
175 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400176 }
Chris Masonb888db22007-08-27 16:49:44 -0400177out:
178 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500179 return ret;
180}
181
182static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
183{
184 u64 extent_start;
185 u64 extent_end;
186 u64 bytenr;
187 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500188 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500189 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500190 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400191 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 struct extent_buffer *leaf;
193 int found_type;
194 struct btrfs_path *path;
195 struct btrfs_file_extent_item *item;
196 int ret;
197 int err;
198 struct btrfs_key found_key;
199
Chris Masonc31f8832008-01-08 15:46:31 -0500200 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500201 path = btrfs_alloc_path();
202 BUG_ON(!path);
203again:
204 ret = btrfs_lookup_file_extent(NULL, root, path,
205 inode->i_ino, start, 0);
206 if (ret < 0) {
207 btrfs_free_path(path);
208 return ret;
209 }
210
211 cow_end = end;
212 if (ret != 0) {
213 if (path->slots[0] == 0)
214 goto not_found;
215 path->slots[0]--;
216 }
217
218 leaf = path->nodes[0];
219 item = btrfs_item_ptr(leaf, path->slots[0],
220 struct btrfs_file_extent_item);
221
222 /* are we inside the extent that was found? */
223 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
224 found_type = btrfs_key_type(&found_key);
225 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400226 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500228
229 found_type = btrfs_file_extent_type(leaf, item);
230 extent_start = found_key.offset;
231 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500232 u64 extent_num_bytes;
233
234 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
235 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 err = 0;
237
Chris Mason1832a6d2007-12-21 16:27:21 -0500238 if (loops && start != extent_start)
239 goto not_found;
240
Chris Masonbe20aa92007-12-17 20:14:01 -0500241 if (start < extent_start || start >= extent_end)
242 goto not_found;
243
244 cow_end = min(end, extent_end - 1);
245 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
246 if (bytenr == 0)
247 goto not_found;
248
Chris Masona68d5932008-05-08 14:11:56 -0400249 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
250 bytenr) != 1) {
251 goto not_found;
252 }
253
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:
Chris Masonbbaf5492008-05-08 16:31:21 -0400277 cow_file_range(inode, start, end);
278 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500279 goto loop;
280}
281
282static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
283{
284 struct btrfs_root *root = BTRFS_I(inode)->root;
285 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400286
Yanb98b6762008-01-08 15:54:37 -0500287 if (btrfs_test_opt(root, NODATACOW) ||
288 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500289 ret = run_delalloc_nocow(inode, start, end);
290 else
291 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500292
Chris Masonb888db22007-08-27 16:49:44 -0400293 return ret;
294}
295
Chris Mason291d6732008-01-29 15:55:23 -0500296int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500297 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500298{
Chris Masonbcbfce82008-04-22 13:26:47 -0400299 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500300 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500301 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400302 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500303 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500304 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400305 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500306 }
307 return 0;
308}
309
310int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500311 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500312{
Chris Masonb0c68f82008-01-31 11:05:37 -0500313 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500314 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400315 unsigned long flags;
316
317 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 if (end - start + 1 > root->fs_info->delalloc_bytes) {
319 printk("warning: delalloc account %Lu %Lu\n",
320 end - start + 1, root->fs_info->delalloc_bytes);
321 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500322 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500323 } else {
324 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500325 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500326 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400327 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500328 }
329 return 0;
330}
331
Chris Mason239b14b2008-03-24 15:02:07 -0400332int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
333 size_t size, struct bio *bio)
334{
335 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
336 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400337 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 u64 length = 0;
339 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400340 int ret;
341
Chris Masonf2d8d742008-04-21 10:03:05 -0400342 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400343 map_tree = &root->fs_info->mapping_tree;
344 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400345 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400346 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400347
Chris Mason239b14b2008-03-24 15:02:07 -0400348 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400349 return 1;
350 }
351 return 0;
352}
353
Chris Mason44b8bd72008-04-16 11:14:51 -0400354int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400355 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500356{
Chris Mason065631f2008-02-20 12:07:25 -0500357 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500358 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400359
Chris Mason3edf7d32008-07-18 06:17:13 -0400360 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400361 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400362
Chris Mason8b712842008-06-11 16:50:36 -0400363 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400364}
365
366int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
367 int mirror_num)
368{
369 struct btrfs_root *root = BTRFS_I(inode)->root;
370 int ret = 0;
371
Chris Masone6dcd2d2008-07-17 12:53:50 -0400372 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
373 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500374
Chris Masone6dcd2d2008-07-17 12:53:50 -0400375 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400376 goto mapit;
377 }
Chris Mason065631f2008-02-20 12:07:25 -0500378
Chris Mason44b8bd72008-04-16 11:14:51 -0400379 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
380 inode, rw, bio, mirror_num,
381 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400382mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400383 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500384}
Chris Mason6885f302008-02-20 16:11:05 -0500385
Chris Masonba1da2f2008-07-17 12:54:15 -0400386static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400387 struct inode *inode, u64 file_offset,
388 struct list_head *list)
389{
390 struct list_head *cur;
391 struct btrfs_ordered_sum *sum;
392
393 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400394 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400395 sum = list_entry(cur, struct btrfs_ordered_sum, list);
396 mutex_lock(&BTRFS_I(inode)->csum_mutex);
397 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
398 inode, sum);
399 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400400 }
401 return 0;
402}
403
Chris Mason247e7432008-07-17 12:53:51 -0400404struct btrfs_writepage_fixup {
405 struct page *page;
406 struct btrfs_work work;
407};
408
409/* see btrfs_writepage_start_hook for details on why this is required */
410void btrfs_writepage_fixup_worker(struct btrfs_work *work)
411{
412 struct btrfs_writepage_fixup *fixup;
413 struct btrfs_ordered_extent *ordered;
414 struct page *page;
415 struct inode *inode;
416 u64 page_start;
417 u64 page_end;
418
419 fixup = container_of(work, struct btrfs_writepage_fixup, work);
420 page = fixup->page;
421
422 lock_page(page);
423 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
424 ClearPageChecked(page);
425 goto out_page;
426 }
427
428 inode = page->mapping->host;
429 page_start = page_offset(page);
430 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
431
432 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
433 ordered = btrfs_lookup_ordered_extent(inode, page_start);
434 if (ordered)
435 goto out;
436
437 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
438 GFP_NOFS);
439 ClearPageChecked(page);
440out:
441 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
442out_page:
443 unlock_page(page);
444 page_cache_release(page);
445}
446
447/*
448 * There are a few paths in the higher layers of the kernel that directly
449 * set the page dirty bit without asking the filesystem if it is a
450 * good idea. This causes problems because we want to make sure COW
451 * properly happens and the data=ordered rules are followed.
452 *
453 * In our case any range that doesn't have the EXTENT_ORDERED bit set
454 * hasn't been properly setup for IO. We kick off an async process
455 * to fix it up. The async helper will wait for ordered extents, set
456 * the delalloc bit and make it safe to write the page.
457 */
458int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
459{
460 struct inode *inode = page->mapping->host;
461 struct btrfs_writepage_fixup *fixup;
462 struct btrfs_root *root = BTRFS_I(inode)->root;
463 int ret;
464
465 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
466 EXTENT_ORDERED, 0);
467 if (ret)
468 return 0;
469
470 if (PageChecked(page))
471 return -EAGAIN;
472
473 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
474 if (!fixup)
475 return -EAGAIN;
476printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
477 SetPageChecked(page);
478 page_cache_get(page);
479 fixup->work.func = btrfs_writepage_fixup_worker;
480 fixup->page = page;
481 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
482 return -EAGAIN;
483}
484
Chris Mason211f90e2008-07-18 11:56:15 -0400485static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400486{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400487 struct btrfs_root *root = BTRFS_I(inode)->root;
488 struct btrfs_trans_handle *trans;
489 struct btrfs_ordered_extent *ordered_extent;
490 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400491 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
492 struct extent_map *em;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400493 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400494 u64 clear_start;
495 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400496 struct list_head list;
497 struct btrfs_key ins;
498 int ret;
499
500 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400501 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400502 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400503
Chris Masonf9295742008-07-17 12:54:14 -0400504 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400505
506 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
507 BUG_ON(!ordered_extent);
508
509 lock_extent(io_tree, ordered_extent->file_offset,
510 ordered_extent->file_offset + ordered_extent->len - 1,
511 GFP_NOFS);
512
513 INIT_LIST_HEAD(&list);
514
515 ins.objectid = ordered_extent->start;
516 ins.offset = ordered_extent->len;
517 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400518
Chris Masone6dcd2d2008-07-17 12:53:50 -0400519 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
520 trans->transid, inode->i_ino,
521 ordered_extent->file_offset, &ins);
522 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400523
524 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400525
Chris Masone6dcd2d2008-07-17 12:53:50 -0400526 ret = btrfs_drop_extents(trans, root, inode,
527 ordered_extent->file_offset,
528 ordered_extent->file_offset +
529 ordered_extent->len,
530 ordered_extent->file_offset, &alloc_hint);
531 BUG_ON(ret);
532 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
533 ordered_extent->file_offset,
534 ordered_extent->start,
535 ordered_extent->len,
536 ordered_extent->len, 0);
537 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400538
Chris Mason7f3c74f2008-07-18 12:01:11 -0400539 spin_lock(&em_tree->lock);
Chris Masone5a22172008-07-18 20:42:20 -0400540 clear_start = ordered_extent->file_offset;
541 clear_end = ordered_extent->file_offset + ordered_extent->len;
542 while(clear_start < clear_end) {
543 em = lookup_extent_mapping(em_tree, clear_start,
544 clear_end - clear_start);
545 if (em) {
546 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
547 clear_start = em->start + em->len;
548 free_extent_map(em);
549 } else {
550 break;
551 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400552 }
553 spin_unlock(&em_tree->lock);
554
Chris Masone6dcd2d2008-07-17 12:53:50 -0400555 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
556 ordered_extent->file_offset +
557 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400558 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
559
Chris Masone6dcd2d2008-07-17 12:53:50 -0400560 inode->i_blocks += ordered_extent->len >> 9;
561 unlock_extent(io_tree, ordered_extent->file_offset,
562 ordered_extent->file_offset + ordered_extent->len - 1,
563 GFP_NOFS);
564 add_pending_csums(trans, inode, ordered_extent->file_offset,
565 &ordered_extent->list);
566
Chris Masondbe674a2008-07-17 12:54:05 -0400567 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400568 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400569
Chris Masone6dcd2d2008-07-17 12:53:50 -0400570 /* once for us */
571 btrfs_put_ordered_extent(ordered_extent);
572 /* once for the tree */
573 btrfs_put_ordered_extent(ordered_extent);
574
575 btrfs_update_inode(trans, root, inode);
576 btrfs_end_transaction(trans, root);
577 return 0;
578}
579
Chris Mason211f90e2008-07-18 11:56:15 -0400580int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
581 struct extent_state *state, int uptodate)
582{
583 return btrfs_finish_ordered_io(page->mapping->host, start, end);
584}
585
Chris Mason07157aa2007-08-30 08:50:51 -0400586int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
587{
588 int ret = 0;
589 struct inode *inode = page->mapping->host;
590 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500591 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400592 struct btrfs_csum_item *item;
593 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400594 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400595
Yanb98b6762008-01-08 15:54:37 -0500596 if (btrfs_test_opt(root, NODATASUM) ||
597 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500598 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400599
Chris Mason07157aa2007-08-30 08:50:51 -0400600 path = btrfs_alloc_path();
601 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
602 if (IS_ERR(item)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400603 /*
604 * It is possible there is an ordered extent that has
605 * not yet finished for this range in the file. If so,
606 * that extent will have a csum cached, and it will insert
607 * the sum after all the blocks in the extent are fully
608 * on disk. So, look for an ordered extent and use the
609 * sum if found.
610 */
611 ret = btrfs_find_ordered_sum(inode, start, &csum);
612 if (ret == 0)
613 goto found;
614
Chris Mason07157aa2007-08-30 08:50:51 -0400615 ret = PTR_ERR(item);
616 /* a csum that isn't present is a preallocated region. */
617 if (ret == -ENOENT || ret == -EFBIG)
618 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400619 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400620 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
621 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400622 goto out;
623 }
Chris Masonff79f812007-10-15 16:22:25 -0400624 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
625 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400626found:
Chris Masond1310b22008-01-24 16:13:08 -0500627 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400628out:
629 if (path)
630 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400631 return ret;
632}
633
Chris Mason7e383262008-04-09 16:28:12 -0400634struct io_failure_record {
635 struct page *page;
636 u64 start;
637 u64 len;
638 u64 logical;
639 int last_mirror;
640};
641
Chris Mason1259ab72008-05-12 13:39:03 -0400642int btrfs_io_failed_hook(struct bio *failed_bio,
643 struct page *page, u64 start, u64 end,
644 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400645{
646 struct io_failure_record *failrec = NULL;
647 u64 private;
648 struct extent_map *em;
649 struct inode *inode = page->mapping->host;
650 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400651 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400652 struct bio *bio;
653 int num_copies;
654 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400655 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400656 u64 logical;
657
658 ret = get_state_private(failure_tree, start, &private);
659 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400660 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
661 if (!failrec)
662 return -ENOMEM;
663 failrec->start = start;
664 failrec->len = end - start + 1;
665 failrec->last_mirror = 0;
666
Chris Mason3b951512008-04-17 11:29:12 -0400667 spin_lock(&em_tree->lock);
668 em = lookup_extent_mapping(em_tree, start, failrec->len);
669 if (em->start > start || em->start + em->len < start) {
670 free_extent_map(em);
671 em = NULL;
672 }
673 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400674
675 if (!em || IS_ERR(em)) {
676 kfree(failrec);
677 return -EIO;
678 }
679 logical = start - em->start;
680 logical = em->block_start + logical;
681 failrec->logical = logical;
682 free_extent_map(em);
683 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
684 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400685 set_state_private(failure_tree, start,
686 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400687 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400688 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400689 }
690 num_copies = btrfs_num_copies(
691 &BTRFS_I(inode)->root->fs_info->mapping_tree,
692 failrec->logical, failrec->len);
693 failrec->last_mirror++;
694 if (!state) {
695 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
696 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
697 failrec->start,
698 EXTENT_LOCKED);
699 if (state && state->start != failrec->start)
700 state = NULL;
701 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
702 }
703 if (!state || failrec->last_mirror > num_copies) {
704 set_state_private(failure_tree, failrec->start, 0);
705 clear_extent_bits(failure_tree, failrec->start,
706 failrec->start + failrec->len - 1,
707 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
708 kfree(failrec);
709 return -EIO;
710 }
711 bio = bio_alloc(GFP_NOFS, 1);
712 bio->bi_private = state;
713 bio->bi_end_io = failed_bio->bi_end_io;
714 bio->bi_sector = failrec->logical >> 9;
715 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400716 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400717 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400718 if (failed_bio->bi_rw & (1 << BIO_RW))
719 rw = WRITE;
720 else
721 rw = READ;
722
723 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
724 failrec->last_mirror);
725 return 0;
726}
727
728int btrfs_clean_io_failures(struct inode *inode, u64 start)
729{
730 u64 private;
731 u64 private_failure;
732 struct io_failure_record *failure;
733 int ret;
734
735 private = 0;
736 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
737 (u64)-1, 1, EXTENT_DIRTY)) {
738 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
739 start, &private_failure);
740 if (ret == 0) {
741 failure = (struct io_failure_record *)(unsigned long)
742 private_failure;
743 set_state_private(&BTRFS_I(inode)->io_failure_tree,
744 failure->start, 0);
745 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
746 failure->start,
747 failure->start + failure->len - 1,
748 EXTENT_DIRTY | EXTENT_LOCKED,
749 GFP_NOFS);
750 kfree(failure);
751 }
752 }
Chris Mason7e383262008-04-09 16:28:12 -0400753 return 0;
754}
755
Chris Mason70dec802008-01-29 09:59:12 -0500756int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
757 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400758{
Chris Mason35ebb932007-10-30 16:56:53 -0400759 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400760 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500761 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400762 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500763 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400764 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400765 struct btrfs_root *root = BTRFS_I(inode)->root;
766 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400767 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500768
Yanb98b6762008-01-08 15:54:37 -0500769 if (btrfs_test_opt(root, NODATASUM) ||
770 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500771 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500772 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500773 private = state->private;
774 ret = 0;
775 } else {
776 ret = get_state_private(io_tree, start, &private);
777 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400778 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400779 kaddr = kmap_atomic(page, KM_IRQ0);
780 if (ret) {
781 goto zeroit;
782 }
Chris Masonff79f812007-10-15 16:22:25 -0400783 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
784 btrfs_csum_final(csum, (char *)&csum);
785 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400786 goto zeroit;
787 }
788 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400789 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400790
791 /* if the io failure tree for this inode is non-empty,
792 * check to see if we've recovered from a failed IO
793 */
Chris Mason1259ab72008-05-12 13:39:03 -0400794 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400795 return 0;
796
797zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500798 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
799 page->mapping->host->i_ino, (unsigned long long)start, csum,
800 private);
Chris Masondb945352007-10-15 16:15:53 -0400801 memset(kaddr + offset, 1, end - start + 1);
802 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400803 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400804 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400805 if (private == 0)
806 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400807 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400808}
Chris Masonb888db22007-08-27 16:49:44 -0400809
Chris Mason39279cc2007-06-12 06:35:45 -0400810void btrfs_read_locked_inode(struct inode *inode)
811{
812 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400813 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400814 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400815 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400816 struct btrfs_root *root = BTRFS_I(inode)->root;
817 struct btrfs_key location;
818 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400819 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400820 int ret;
821
822 path = btrfs_alloc_path();
823 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400824 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500825
Chris Mason39279cc2007-06-12 06:35:45 -0400826 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400827 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400828 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400829
Chris Mason5f39d392007-10-15 16:14:19 -0400830 leaf = path->nodes[0];
831 inode_item = btrfs_item_ptr(leaf, path->slots[0],
832 struct btrfs_inode_item);
833
834 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
835 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
836 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
837 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400838 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400839
840 tspec = btrfs_inode_atime(inode_item);
841 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
842 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
843
844 tspec = btrfs_inode_mtime(inode_item);
845 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
846 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
847
848 tspec = btrfs_inode_ctime(inode_item);
849 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
850 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
851
852 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
853 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400854 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400855 rdev = btrfs_inode_rdev(leaf, inode_item);
856
857 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400858 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
859 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500860 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500861 if (!BTRFS_I(inode)->block_group) {
862 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400863 NULL, 0,
864 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500865 }
Chris Mason39279cc2007-06-12 06:35:45 -0400866 btrfs_free_path(path);
867 inode_item = NULL;
868
Chris Mason39279cc2007-06-12 06:35:45 -0400869 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400870 case S_IFREG:
871 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400872 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500873 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400874 inode->i_fop = &btrfs_file_operations;
875 inode->i_op = &btrfs_file_inode_operations;
876 break;
877 case S_IFDIR:
878 inode->i_fop = &btrfs_dir_file_operations;
879 if (root == root->fs_info->tree_root)
880 inode->i_op = &btrfs_dir_ro_inode_operations;
881 else
882 inode->i_op = &btrfs_dir_inode_operations;
883 break;
884 case S_IFLNK:
885 inode->i_op = &btrfs_symlink_inode_operations;
886 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400887 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400888 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400889 default:
890 init_special_inode(inode, inode->i_mode, rdev);
891 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400892 }
893 return;
894
895make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400896 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400897 make_bad_inode(inode);
898}
899
Chris Mason5f39d392007-10-15 16:14:19 -0400900static void fill_inode_item(struct extent_buffer *leaf,
901 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400902 struct inode *inode)
903{
Chris Mason5f39d392007-10-15 16:14:19 -0400904 btrfs_set_inode_uid(leaf, item, inode->i_uid);
905 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -0400906 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -0400907 btrfs_set_inode_mode(leaf, item, inode->i_mode);
908 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
909
910 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
911 inode->i_atime.tv_sec);
912 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
913 inode->i_atime.tv_nsec);
914
915 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
916 inode->i_mtime.tv_sec);
917 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
918 inode->i_mtime.tv_nsec);
919
920 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
921 inode->i_ctime.tv_sec);
922 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
923 inode->i_ctime.tv_nsec);
924
925 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
926 btrfs_set_inode_generation(leaf, item, inode->i_generation);
927 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500928 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400929 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400930 BTRFS_I(inode)->block_group->key.objectid);
931}
932
Chris Masonba1da2f2008-07-17 12:54:15 -0400933int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400934 struct btrfs_root *root,
935 struct inode *inode)
936{
937 struct btrfs_inode_item *inode_item;
938 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400939 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400940 int ret;
941
942 path = btrfs_alloc_path();
943 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400944 ret = btrfs_lookup_inode(trans, root, path,
945 &BTRFS_I(inode)->location, 1);
946 if (ret) {
947 if (ret > 0)
948 ret = -ENOENT;
949 goto failed;
950 }
951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 leaf = path->nodes[0];
953 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400954 struct btrfs_inode_item);
955
Chris Mason5f39d392007-10-15 16:14:19 -0400956 fill_inode_item(leaf, inode_item, inode);
957 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400958 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400959 ret = 0;
960failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400961 btrfs_free_path(path);
962 return ret;
963}
964
965
966static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
967 struct btrfs_root *root,
968 struct inode *dir,
969 struct dentry *dentry)
970{
971 struct btrfs_path *path;
972 const char *name = dentry->d_name.name;
973 int name_len = dentry->d_name.len;
974 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400975 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400976 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400977 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400978
979 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400980 if (!path) {
981 ret = -ENOMEM;
982 goto err;
983 }
984
Chris Mason39279cc2007-06-12 06:35:45 -0400985 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
986 name, name_len, -1);
987 if (IS_ERR(di)) {
988 ret = PTR_ERR(di);
989 goto err;
990 }
991 if (!di) {
992 ret = -ENOENT;
993 goto err;
994 }
Chris Mason5f39d392007-10-15 16:14:19 -0400995 leaf = path->nodes[0];
996 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400997 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400998 if (ret)
999 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001000 btrfs_release_path(root, path);
1001
1002 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04001003 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001004 if (IS_ERR(di)) {
1005 ret = PTR_ERR(di);
1006 goto err;
1007 }
1008 if (!di) {
1009 ret = -ENOENT;
1010 goto err;
1011 }
1012 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001013 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001014
1015 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -05001016 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1017 dentry->d_inode->i_ino,
1018 dentry->d_parent->d_inode->i_ino);
1019 if (ret) {
1020 printk("failed to delete reference to %.*s, "
1021 "inode %lu parent %lu\n", name_len, name,
1022 dentry->d_inode->i_ino,
1023 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -05001024 }
Chris Mason39279cc2007-06-12 06:35:45 -04001025err:
1026 btrfs_free_path(path);
1027 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001028 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001029 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001030 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001031#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1032 dentry->d_inode->i_nlink--;
1033#else
Chris Mason39279cc2007-06-12 06:35:45 -04001034 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001035#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001036 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001037 dir->i_sb->s_dirt = 1;
1038 }
1039 return ret;
1040}
1041
1042static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1043{
1044 struct btrfs_root *root;
1045 struct btrfs_trans_handle *trans;
1046 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001047 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001048
1049 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001050
1051 ret = btrfs_check_free_space(root, 1, 1);
1052 if (ret)
1053 goto fail;
1054
Chris Mason39279cc2007-06-12 06:35:45 -04001055 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001056
Chris Mason39279cc2007-06-12 06:35:45 -04001057 btrfs_set_trans_block_group(trans, dir);
1058 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001059 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001060
Chris Mason89ce8a62008-06-25 16:01:31 -04001061 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001062fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001063 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001064 return ret;
1065}
1066
1067static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1068{
1069 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001070 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001071 int ret;
1072 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001073 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001074 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001075
Chris Mason925baed2008-06-25 16:01:30 -04001076 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001077 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001078 }
Yan134d4512007-10-25 15:49:25 -04001079
Chris Mason1832a6d2007-12-21 16:27:21 -05001080 ret = btrfs_check_free_space(root, 1, 1);
1081 if (ret)
1082 goto fail;
1083
Chris Mason39279cc2007-06-12 06:35:45 -04001084 trans = btrfs_start_transaction(root, 1);
1085 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001086
1087 /* now the directory is empty */
1088 err = btrfs_unlink_trans(trans, root, dir, dentry);
1089 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001090 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001091 }
Chris Mason39544012007-12-12 14:38:19 -05001092
Chris Masond3c2fdc2007-09-17 10:58:06 -04001093 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001094 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001095fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001096 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001097
Chris Mason39279cc2007-06-12 06:35:45 -04001098 if (ret && !err)
1099 err = ret;
1100 return err;
1101}
1102
Chris Mason39279cc2007-06-12 06:35:45 -04001103/*
Chris Mason39279cc2007-06-12 06:35:45 -04001104 * this can truncate away extent items, csum items and directory items.
1105 * It starts at a high offset and removes keys until it can't find
1106 * any higher than i_size.
1107 *
1108 * csum items that cross the new i_size are truncated to the new size
1109 * as well.
1110 */
1111static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1112 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001113 struct inode *inode,
1114 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001115{
1116 int ret;
1117 struct btrfs_path *path;
1118 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001119 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001120 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001121 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001122 struct btrfs_file_extent_item *fi;
1123 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001124 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001125 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001126 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001127 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001128 int found_extent;
1129 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001130 int pending_del_nr = 0;
1131 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001132 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001133 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001134
Chris Mason3b951512008-04-17 11:29:12 -04001135 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001136 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001137 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001138 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001139
Chris Mason39279cc2007-06-12 06:35:45 -04001140 /* FIXME, add redo link to tree so we don't leak on crash */
1141 key.objectid = inode->i_ino;
1142 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001143 key.type = (u8)-1;
1144
Chris Mason85e21ba2008-01-29 15:11:36 -05001145 btrfs_init_path(path);
1146search_again:
1147 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1148 if (ret < 0) {
1149 goto error;
1150 }
1151 if (ret > 0) {
1152 BUG_ON(path->slots[0] == 0);
1153 path->slots[0]--;
1154 }
1155
Chris Mason39279cc2007-06-12 06:35:45 -04001156 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001157 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001158 leaf = path->nodes[0];
1159 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1160 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001161
Chris Mason5f39d392007-10-15 16:14:19 -04001162 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001163 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001164
Chris Mason85e21ba2008-01-29 15:11:36 -05001165 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001166 break;
1167
Chris Mason5f39d392007-10-15 16:14:19 -04001168 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001169 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001170 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001171 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001172 extent_type = btrfs_file_extent_type(leaf, fi);
1173 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001174 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001175 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001176 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1177 struct btrfs_item *item = btrfs_item_nr(leaf,
1178 path->slots[0]);
1179 item_end += btrfs_file_extent_inline_len(leaf,
1180 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001181 }
Yan008630c2007-11-07 13:31:09 -05001182 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001183 }
1184 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1185 ret = btrfs_csum_truncate(trans, root, path,
1186 inode->i_size);
1187 BUG_ON(ret);
1188 }
Yan008630c2007-11-07 13:31:09 -05001189 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001190 if (found_type == BTRFS_DIR_ITEM_KEY) {
1191 found_type = BTRFS_INODE_ITEM_KEY;
1192 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1193 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001194 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1195 found_type = BTRFS_XATTR_ITEM_KEY;
1196 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1197 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001198 } else if (found_type) {
1199 found_type--;
1200 } else {
1201 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001202 }
Yana61721d2007-09-17 11:08:38 -04001203 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001204 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001205 }
Chris Mason5f39d392007-10-15 16:14:19 -04001206 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001207 del_item = 1;
1208 else
1209 del_item = 0;
1210 found_extent = 0;
1211
1212 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001213 if (found_type != BTRFS_EXTENT_DATA_KEY)
1214 goto delete;
1215
1216 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001217 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001218 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001219 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001220 u64 orig_num_bytes =
1221 btrfs_file_extent_num_bytes(leaf, fi);
1222 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001223 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001224 extent_num_bytes = extent_num_bytes &
1225 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001226 btrfs_set_file_extent_num_bytes(leaf, fi,
1227 extent_num_bytes);
1228 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001229 extent_num_bytes);
1230 if (extent_start != 0)
1231 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001232 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001233 } else {
Chris Masondb945352007-10-15 16:15:53 -04001234 extent_num_bytes =
1235 btrfs_file_extent_disk_num_bytes(leaf,
1236 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001237 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001238 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001239 if (extent_start != 0) {
1240 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001241 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001242 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001243 root_gen = btrfs_header_generation(leaf);
1244 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001245 }
Chris Mason90692182008-02-08 13:49:28 -05001246 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1247 if (!del_item) {
1248 u32 newsize = inode->i_size - found_key.offset;
1249 dec_i_blocks(inode, item_end + 1 -
1250 found_key.offset - newsize);
1251 newsize =
1252 btrfs_file_extent_calc_inline_size(newsize);
1253 ret = btrfs_truncate_item(trans, root, path,
1254 newsize, 1);
1255 BUG_ON(ret);
1256 } else {
1257 dec_i_blocks(inode, item_end + 1 -
1258 found_key.offset);
1259 }
Chris Mason39279cc2007-06-12 06:35:45 -04001260 }
Chris Mason179e29e2007-11-01 11:28:41 -04001261delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001262 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001263 if (!pending_del_nr) {
1264 /* no pending yet, add ourselves */
1265 pending_del_slot = path->slots[0];
1266 pending_del_nr = 1;
1267 } else if (pending_del_nr &&
1268 path->slots[0] + 1 == pending_del_slot) {
1269 /* hop on the pending chunk */
1270 pending_del_nr++;
1271 pending_del_slot = path->slots[0];
1272 } else {
1273 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1274 }
Chris Mason39279cc2007-06-12 06:35:45 -04001275 } else {
1276 break;
1277 }
Chris Mason39279cc2007-06-12 06:35:45 -04001278 if (found_extent) {
1279 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001280 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001281 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001282 root_gen, inode->i_ino,
1283 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001284 BUG_ON(ret);
1285 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001286next:
1287 if (path->slots[0] == 0) {
1288 if (pending_del_nr)
1289 goto del_pending;
1290 btrfs_release_path(root, path);
1291 goto search_again;
1292 }
1293
1294 path->slots[0]--;
1295 if (pending_del_nr &&
1296 path->slots[0] + 1 != pending_del_slot) {
1297 struct btrfs_key debug;
1298del_pending:
1299 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1300 pending_del_slot);
1301 ret = btrfs_del_items(trans, root, path,
1302 pending_del_slot,
1303 pending_del_nr);
1304 BUG_ON(ret);
1305 pending_del_nr = 0;
1306 btrfs_release_path(root, path);
1307 goto search_again;
1308 }
Chris Mason39279cc2007-06-12 06:35:45 -04001309 }
1310 ret = 0;
1311error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001312 if (pending_del_nr) {
1313 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1314 pending_del_nr);
1315 }
Chris Mason39279cc2007-06-12 06:35:45 -04001316 btrfs_free_path(path);
1317 inode->i_sb->s_dirt = 1;
1318 return ret;
1319}
1320
Chris Masona52d9a82007-08-27 16:49:44 -04001321/*
1322 * taken from block_truncate_page, but does cow as it zeros out
1323 * any bytes left in the last page in the file.
1324 */
1325static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1326{
1327 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001328 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001329 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1330 struct btrfs_ordered_extent *ordered;
1331 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001332 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001333 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1334 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1335 struct page *page;
1336 int ret = 0;
1337 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001338 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001339
1340 if ((offset & (blocksize - 1)) == 0)
1341 goto out;
1342
1343 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001344again:
Chris Masona52d9a82007-08-27 16:49:44 -04001345 page = grab_cache_page(mapping, index);
1346 if (!page)
1347 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001348
1349 page_start = page_offset(page);
1350 page_end = page_start + PAGE_CACHE_SIZE - 1;
1351
Chris Masona52d9a82007-08-27 16:49:44 -04001352 if (!PageUptodate(page)) {
1353 ret = btrfs_readpage(NULL, page);
1354 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001355 if (page->mapping != mapping) {
1356 unlock_page(page);
1357 page_cache_release(page);
1358 goto again;
1359 }
Chris Masona52d9a82007-08-27 16:49:44 -04001360 if (!PageUptodate(page)) {
1361 ret = -EIO;
1362 goto out;
1363 }
1364 }
Chris Mason211c17f2008-05-15 09:13:45 -04001365 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001366
1367 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1368 set_page_extent_mapped(page);
1369
1370 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1371 if (ordered) {
1372 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1373 unlock_page(page);
1374 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001375 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001376 btrfs_put_ordered_extent(ordered);
1377 goto again;
1378 }
1379
1380 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1381 page_end, GFP_NOFS);
1382 ret = 0;
1383 if (offset != PAGE_CACHE_SIZE) {
1384 kaddr = kmap(page);
1385 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1386 flush_dcache_page(page);
1387 kunmap(page);
1388 }
Chris Mason247e7432008-07-17 12:53:51 -04001389 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001390 set_page_dirty(page);
1391 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001392
Chris Mason39279cc2007-06-12 06:35:45 -04001393 unlock_page(page);
1394 page_cache_release(page);
1395out:
1396 return ret;
1397}
1398
1399static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1400{
1401 struct inode *inode = dentry->d_inode;
1402 int err;
1403
1404 err = inode_change_ok(inode, attr);
1405 if (err)
1406 return err;
1407
1408 if (S_ISREG(inode->i_mode) &&
1409 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1410 struct btrfs_trans_handle *trans;
1411 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001412 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001413
Chris Mason5f39d392007-10-15 16:14:19 -04001414 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001415 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001416 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001417 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001418 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001419
Chris Mason1b0f7c22008-01-30 14:33:02 -05001420 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001421 goto out;
1422
Chris Mason1832a6d2007-12-21 16:27:21 -05001423 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001424 if (err)
1425 goto fail;
1426
Chris Mason39279cc2007-06-12 06:35:45 -04001427 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1428
Chris Mason5f564062008-01-22 16:47:59 -05001429 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001430 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1431 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001432
Chris Mason39279cc2007-06-12 06:35:45 -04001433 trans = btrfs_start_transaction(root, 1);
1434 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001435 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001436 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001437 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001438 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001439
Chris Mason179e29e2007-11-01 11:28:41 -04001440 if (alloc_hint != EXTENT_MAP_INLINE) {
1441 err = btrfs_insert_file_extent(trans, root,
1442 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001443 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001444 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001445 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001446 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001447 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001448 }
Chris Masonee6e6502008-07-17 12:54:40 -04001449 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001450 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001451 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001452 if (err)
1453 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001454 }
1455out:
1456 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001457fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001458 return err;
1459}
Chris Mason61295eb2008-01-14 16:24:38 -05001460
Chris Mason39279cc2007-06-12 06:35:45 -04001461void btrfs_delete_inode(struct inode *inode)
1462{
1463 struct btrfs_trans_handle *trans;
1464 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001465 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001466 int ret;
1467
Chris Masone6dcd2d2008-07-17 12:53:50 -04001468 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001469 truncate_inode_pages(&inode->i_data, 0);
1470 if (is_bad_inode(inode)) {
1471 goto no_delete;
1472 }
Chris Mason5f39d392007-10-15 16:14:19 -04001473
Chris Masondbe674a2008-07-17 12:54:05 -04001474 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001475 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001476
Chris Mason39279cc2007-06-12 06:35:45 -04001477 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001478 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001479 if (ret)
1480 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001481
Chris Masond3c2fdc2007-09-17 10:58:06 -04001482 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001483 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001484
Chris Mason39279cc2007-06-12 06:35:45 -04001485 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001486 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001487 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001488
1489no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001490 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001491 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001492 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001493no_delete:
1494 clear_inode(inode);
1495}
1496
1497/*
1498 * this returns the key found in the dir entry in the location pointer.
1499 * If no dir entries were found, location->objectid is 0.
1500 */
1501static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1502 struct btrfs_key *location)
1503{
1504 const char *name = dentry->d_name.name;
1505 int namelen = dentry->d_name.len;
1506 struct btrfs_dir_item *di;
1507 struct btrfs_path *path;
1508 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001509 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001510
Chris Mason39544012007-12-12 14:38:19 -05001511 if (namelen == 1 && strcmp(name, ".") == 0) {
1512 location->objectid = dir->i_ino;
1513 location->type = BTRFS_INODE_ITEM_KEY;
1514 location->offset = 0;
1515 return 0;
1516 }
Chris Mason39279cc2007-06-12 06:35:45 -04001517 path = btrfs_alloc_path();
1518 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001519
Chris Mason7a720532007-12-13 09:06:59 -05001520 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001521 struct btrfs_key key;
1522 struct extent_buffer *leaf;
1523 u32 nritems;
1524 int slot;
1525
1526 key.objectid = dir->i_ino;
1527 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1528 key.offset = 0;
1529 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1530 BUG_ON(ret == 0);
1531 ret = 0;
1532
1533 leaf = path->nodes[0];
1534 slot = path->slots[0];
1535 nritems = btrfs_header_nritems(leaf);
1536 if (slot >= nritems)
1537 goto out_err;
1538
1539 btrfs_item_key_to_cpu(leaf, &key, slot);
1540 if (key.objectid != dir->i_ino ||
1541 key.type != BTRFS_INODE_REF_KEY) {
1542 goto out_err;
1543 }
1544 location->objectid = key.offset;
1545 location->type = BTRFS_INODE_ITEM_KEY;
1546 location->offset = 0;
1547 goto out;
1548 }
1549
Chris Mason39279cc2007-06-12 06:35:45 -04001550 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1551 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001552 if (IS_ERR(di))
1553 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001554 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001555 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001556 }
Chris Mason5f39d392007-10-15 16:14:19 -04001557 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001558out:
Chris Mason39279cc2007-06-12 06:35:45 -04001559 btrfs_free_path(path);
1560 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001561out_err:
1562 location->objectid = 0;
1563 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001564}
1565
1566/*
1567 * when we hit a tree root in a directory, the btrfs part of the inode
1568 * needs to be changed to reflect the root directory of the tree root. This
1569 * is kind of like crossing a mount point.
1570 */
1571static int fixup_tree_root_location(struct btrfs_root *root,
1572 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001573 struct btrfs_root **sub_root,
1574 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001575{
1576 struct btrfs_path *path;
1577 struct btrfs_root_item *ri;
1578
1579 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1580 return 0;
1581 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1582 return 0;
1583
1584 path = btrfs_alloc_path();
1585 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001586
Josef Bacik58176a92007-08-29 15:47:34 -04001587 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1588 dentry->d_name.name,
1589 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001590 if (IS_ERR(*sub_root))
1591 return PTR_ERR(*sub_root);
1592
1593 ri = &(*sub_root)->root_item;
1594 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001595 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1596 location->offset = 0;
1597
1598 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001599 return 0;
1600}
1601
1602static int btrfs_init_locked_inode(struct inode *inode, void *p)
1603{
1604 struct btrfs_iget_args *args = p;
1605 inode->i_ino = args->ino;
1606 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001607 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001608 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001609 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1610 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001611 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001612 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1613 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001614 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001615 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001616 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001617 return 0;
1618}
1619
1620static int btrfs_find_actor(struct inode *inode, void *opaque)
1621{
1622 struct btrfs_iget_args *args = opaque;
1623 return (args->ino == inode->i_ino &&
1624 args->root == BTRFS_I(inode)->root);
1625}
1626
Chris Masondc17ff82008-01-08 15:46:30 -05001627struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1628 u64 root_objectid)
1629{
1630 struct btrfs_iget_args args;
1631 args.ino = objectid;
1632 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1633
1634 if (!args.root)
1635 return NULL;
1636
1637 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1638}
1639
Chris Mason39279cc2007-06-12 06:35:45 -04001640struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1641 struct btrfs_root *root)
1642{
1643 struct inode *inode;
1644 struct btrfs_iget_args args;
1645 args.ino = objectid;
1646 args.root = root;
1647
1648 inode = iget5_locked(s, objectid, btrfs_find_actor,
1649 btrfs_init_locked_inode,
1650 (void *)&args);
1651 return inode;
1652}
1653
1654static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1655 struct nameidata *nd)
1656{
1657 struct inode * inode;
1658 struct btrfs_inode *bi = BTRFS_I(dir);
1659 struct btrfs_root *root = bi->root;
1660 struct btrfs_root *sub_root = root;
1661 struct btrfs_key location;
1662 int ret;
1663
1664 if (dentry->d_name.len > BTRFS_NAME_LEN)
1665 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001666
Chris Mason39279cc2007-06-12 06:35:45 -04001667 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001668
Chris Mason39279cc2007-06-12 06:35:45 -04001669 if (ret < 0)
1670 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001671
Chris Mason39279cc2007-06-12 06:35:45 -04001672 inode = NULL;
1673 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001674 ret = fixup_tree_root_location(root, &location, &sub_root,
1675 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001676 if (ret < 0)
1677 return ERR_PTR(ret);
1678 if (ret > 0)
1679 return ERR_PTR(-ENOENT);
1680 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1681 sub_root);
1682 if (!inode)
1683 return ERR_PTR(-EACCES);
1684 if (inode->i_state & I_NEW) {
1685 /* the inode and parent dir are two different roots */
1686 if (sub_root != root) {
1687 igrab(inode);
1688 sub_root->inode = inode;
1689 }
1690 BTRFS_I(inode)->root = sub_root;
1691 memcpy(&BTRFS_I(inode)->location, &location,
1692 sizeof(location));
1693 btrfs_read_locked_inode(inode);
1694 unlock_new_inode(inode);
1695 }
1696 }
1697 return d_splice_alias(inode, dentry);
1698}
1699
Chris Mason39279cc2007-06-12 06:35:45 -04001700static unsigned char btrfs_filetype_table[] = {
1701 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1702};
1703
1704static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1705{
Chris Mason6da6aba2007-12-18 16:15:09 -05001706 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 struct btrfs_item *item;
1709 struct btrfs_dir_item *di;
1710 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001711 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001712 struct btrfs_path *path;
1713 int ret;
1714 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001715 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001716 int slot;
1717 int advance;
1718 unsigned char d_type;
1719 int over = 0;
1720 u32 di_cur;
1721 u32 di_total;
1722 u32 di_len;
1723 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001724 char tmp_name[32];
1725 char *name_ptr;
1726 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001727
1728 /* FIXME, use a real flag for deciding about the key type */
1729 if (root->fs_info->tree_root == root)
1730 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001731
Chris Mason39544012007-12-12 14:38:19 -05001732 /* special case for "." */
1733 if (filp->f_pos == 0) {
1734 over = filldir(dirent, ".", 1,
1735 1, inode->i_ino,
1736 DT_DIR);
1737 if (over)
1738 return 0;
1739 filp->f_pos = 1;
1740 }
1741
Chris Mason39279cc2007-06-12 06:35:45 -04001742 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001743 path = btrfs_alloc_path();
1744 path->reada = 2;
1745
1746 /* special case for .., just use the back ref */
1747 if (filp->f_pos == 1) {
1748 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1749 key.offset = 0;
1750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1751 BUG_ON(ret == 0);
1752 leaf = path->nodes[0];
1753 slot = path->slots[0];
1754 nritems = btrfs_header_nritems(leaf);
1755 if (slot >= nritems) {
1756 btrfs_release_path(root, path);
1757 goto read_dir_items;
1758 }
1759 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1760 btrfs_release_path(root, path);
1761 if (found_key.objectid != key.objectid ||
1762 found_key.type != BTRFS_INODE_REF_KEY)
1763 goto read_dir_items;
1764 over = filldir(dirent, "..", 2,
1765 2, found_key.offset, DT_DIR);
1766 if (over)
1767 goto nopos;
1768 filp->f_pos = 2;
1769 }
1770
1771read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001772 btrfs_set_key_type(&key, key_type);
1773 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001774
Chris Mason39279cc2007-06-12 06:35:45 -04001775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1776 if (ret < 0)
1777 goto err;
1778 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001779 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001780 leaf = path->nodes[0];
1781 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001782 slot = path->slots[0];
1783 if (advance || slot >= nritems) {
1784 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001785 ret = btrfs_next_leaf(root, path);
1786 if (ret)
1787 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001788 leaf = path->nodes[0];
1789 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001790 slot = path->slots[0];
1791 } else {
1792 slot++;
1793 path->slots[0]++;
1794 }
1795 }
1796 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001797 item = btrfs_item_nr(leaf, slot);
1798 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1799
1800 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001801 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001802 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001803 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001804 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001805 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001806
1807 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001808 advance = 1;
1809 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1810 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001811 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001812 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001813 struct btrfs_key location;
1814
1815 name_len = btrfs_dir_name_len(leaf, di);
1816 if (name_len < 32) {
1817 name_ptr = tmp_name;
1818 } else {
1819 name_ptr = kmalloc(name_len, GFP_NOFS);
1820 BUG_ON(!name_ptr);
1821 }
1822 read_extent_buffer(leaf, name_ptr,
1823 (unsigned long)(di + 1), name_len);
1824
1825 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1826 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001827 over = filldir(dirent, name_ptr, name_len,
1828 found_key.offset,
1829 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001830 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001831
1832 if (name_ptr != tmp_name)
1833 kfree(name_ptr);
1834
Chris Mason39279cc2007-06-12 06:35:45 -04001835 if (over)
1836 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001837 di_len = btrfs_dir_name_len(leaf, di) +
1838 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001839 di_cur += di_len;
1840 di = (struct btrfs_dir_item *)((char *)di + di_len);
1841 }
1842 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001843 if (key_type == BTRFS_DIR_INDEX_KEY)
1844 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1845 else
1846 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001847nopos:
1848 ret = 0;
1849err:
Chris Mason39279cc2007-06-12 06:35:45 -04001850 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001851 return ret;
1852}
1853
1854int btrfs_write_inode(struct inode *inode, int wait)
1855{
1856 struct btrfs_root *root = BTRFS_I(inode)->root;
1857 struct btrfs_trans_handle *trans;
1858 int ret = 0;
1859
1860 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04001861 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001862 btrfs_set_trans_block_group(trans, inode);
1863 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001864 }
1865 return ret;
1866}
1867
1868/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001869 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001870 * inode changes. But, it is most likely to find the inode in cache.
1871 * FIXME, needs more benchmarking...there are no reasons other than performance
1872 * to keep or drop this code.
1873 */
1874void btrfs_dirty_inode(struct inode *inode)
1875{
1876 struct btrfs_root *root = BTRFS_I(inode)->root;
1877 struct btrfs_trans_handle *trans;
1878
Chris Masonf9295742008-07-17 12:54:14 -04001879 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001880 btrfs_set_trans_block_group(trans, inode);
1881 btrfs_update_inode(trans, root, inode);
1882 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001883}
1884
1885static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001887 const char *name, int name_len,
1888 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001889 u64 objectid,
1890 struct btrfs_block_group_cache *group,
1891 int mode)
1892{
1893 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001894 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001895 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001896 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001897 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001898 struct btrfs_inode_ref *ref;
1899 struct btrfs_key key[2];
1900 u32 sizes[2];
1901 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001902 int ret;
1903 int owner;
1904
Chris Mason5f39d392007-10-15 16:14:19 -04001905 path = btrfs_alloc_path();
1906 BUG_ON(!path);
1907
Chris Mason39279cc2007-06-12 06:35:45 -04001908 inode = new_inode(root->fs_info->sb);
1909 if (!inode)
1910 return ERR_PTR(-ENOMEM);
1911
Chris Masond1310b22008-01-24 16:13:08 -05001912 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1913 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001914 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001915 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1916 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001917 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001918 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001919 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05001920 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001921 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001922 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001923
Chris Mason39279cc2007-06-12 06:35:45 -04001924 if (mode & S_IFDIR)
1925 owner = 0;
1926 else
1927 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001928 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001929 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001930 if (!new_inode_group) {
1931 printk("find_block group failed\n");
1932 new_inode_group = group;
1933 }
1934 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001935 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001936
1937 key[0].objectid = objectid;
1938 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1939 key[0].offset = 0;
1940
1941 key[1].objectid = objectid;
1942 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1943 key[1].offset = ref_objectid;
1944
1945 sizes[0] = sizeof(struct btrfs_inode_item);
1946 sizes[1] = name_len + sizeof(*ref);
1947
1948 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1949 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001950 goto fail;
1951
Chris Mason9c583092008-01-29 15:15:18 -05001952 if (objectid > root->highest_inode)
1953 root->highest_inode = objectid;
1954
Chris Mason39279cc2007-06-12 06:35:45 -04001955 inode->i_uid = current->fsuid;
1956 inode->i_gid = current->fsgid;
1957 inode->i_mode = mode;
1958 inode->i_ino = objectid;
1959 inode->i_blocks = 0;
1960 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001961 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1962 struct btrfs_inode_item);
1963 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001964
1965 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1966 struct btrfs_inode_ref);
1967 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1968 ptr = (unsigned long)(ref + 1);
1969 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 btrfs_mark_buffer_dirty(path->nodes[0]);
1972 btrfs_free_path(path);
1973
Chris Mason39279cc2007-06-12 06:35:45 -04001974 location = &BTRFS_I(inode)->location;
1975 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001976 location->offset = 0;
1977 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1978
Chris Mason39279cc2007-06-12 06:35:45 -04001979 insert_inode_hash(inode);
1980 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001981fail:
1982 btrfs_free_path(path);
1983 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001984}
1985
1986static inline u8 btrfs_inode_type(struct inode *inode)
1987{
1988 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1989}
1990
1991static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001992 struct dentry *dentry, struct inode *inode,
1993 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001994{
1995 int ret;
1996 struct btrfs_key key;
1997 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001998 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001999
Chris Mason39279cc2007-06-12 06:35:45 -04002000 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002001 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2002 key.offset = 0;
2003
2004 ret = btrfs_insert_dir_item(trans, root,
2005 dentry->d_name.name, dentry->d_name.len,
2006 dentry->d_parent->d_inode->i_ino,
2007 &key, btrfs_inode_type(inode));
2008 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002009 if (add_backref) {
2010 ret = btrfs_insert_inode_ref(trans, root,
2011 dentry->d_name.name,
2012 dentry->d_name.len,
2013 inode->i_ino,
2014 dentry->d_parent->d_inode->i_ino);
2015 }
Chris Mason79c44582007-06-25 10:09:33 -04002016 parent_inode = dentry->d_parent->d_inode;
Chris Masondbe674a2008-07-17 12:54:05 -04002017 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2018 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002019 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002020 ret = btrfs_update_inode(trans, root,
2021 dentry->d_parent->d_inode);
2022 }
2023 return ret;
2024}
2025
2026static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002027 struct dentry *dentry, struct inode *inode,
2028 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002029{
Chris Mason9c583092008-01-29 15:15:18 -05002030 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002031 if (!err) {
2032 d_instantiate(dentry, inode);
2033 return 0;
2034 }
2035 if (err > 0)
2036 err = -EEXIST;
2037 return err;
2038}
2039
Josef Bacik618e21d2007-07-11 10:18:17 -04002040static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2041 int mode, dev_t rdev)
2042{
2043 struct btrfs_trans_handle *trans;
2044 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002045 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002046 int err;
2047 int drop_inode = 0;
2048 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002049 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002050
2051 if (!new_valid_dev(rdev))
2052 return -EINVAL;
2053
Chris Mason1832a6d2007-12-21 16:27:21 -05002054 err = btrfs_check_free_space(root, 1, 0);
2055 if (err)
2056 goto fail;
2057
Josef Bacik618e21d2007-07-11 10:18:17 -04002058 trans = btrfs_start_transaction(root, 1);
2059 btrfs_set_trans_block_group(trans, dir);
2060
2061 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2062 if (err) {
2063 err = -ENOSPC;
2064 goto out_unlock;
2065 }
2066
Chris Mason9c583092008-01-29 15:15:18 -05002067 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2068 dentry->d_name.len,
2069 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002070 BTRFS_I(dir)->block_group, mode);
2071 err = PTR_ERR(inode);
2072 if (IS_ERR(inode))
2073 goto out_unlock;
2074
2075 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002076 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002077 if (err)
2078 drop_inode = 1;
2079 else {
2080 inode->i_op = &btrfs_special_inode_operations;
2081 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002082 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002083 }
2084 dir->i_sb->s_dirt = 1;
2085 btrfs_update_inode_block_group(trans, inode);
2086 btrfs_update_inode_block_group(trans, dir);
2087out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002088 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002089 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002090fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002091 if (drop_inode) {
2092 inode_dec_link_count(inode);
2093 iput(inode);
2094 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002095 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002096 return err;
2097}
2098
Chris Mason39279cc2007-06-12 06:35:45 -04002099static int btrfs_create(struct inode *dir, struct dentry *dentry,
2100 int mode, struct nameidata *nd)
2101{
2102 struct btrfs_trans_handle *trans;
2103 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002104 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002105 int err;
2106 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002107 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002108 u64 objectid;
2109
Chris Mason1832a6d2007-12-21 16:27:21 -05002110 err = btrfs_check_free_space(root, 1, 0);
2111 if (err)
2112 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002113 trans = btrfs_start_transaction(root, 1);
2114 btrfs_set_trans_block_group(trans, dir);
2115
2116 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2117 if (err) {
2118 err = -ENOSPC;
2119 goto out_unlock;
2120 }
2121
Chris Mason9c583092008-01-29 15:15:18 -05002122 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2123 dentry->d_name.len,
2124 dentry->d_parent->d_inode->i_ino,
2125 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002126 err = PTR_ERR(inode);
2127 if (IS_ERR(inode))
2128 goto out_unlock;
2129
2130 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002131 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002132 if (err)
2133 drop_inode = 1;
2134 else {
2135 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002136 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002137 inode->i_fop = &btrfs_file_operations;
2138 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002139 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2140 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002141 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002142 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2143 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002144 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002145 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002146 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002147 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002148 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002149 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002150 }
2151 dir->i_sb->s_dirt = 1;
2152 btrfs_update_inode_block_group(trans, inode);
2153 btrfs_update_inode_block_group(trans, dir);
2154out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002155 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002156 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002157fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002158 if (drop_inode) {
2159 inode_dec_link_count(inode);
2160 iput(inode);
2161 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002162 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002163 return err;
2164}
2165
2166static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2167 struct dentry *dentry)
2168{
2169 struct btrfs_trans_handle *trans;
2170 struct btrfs_root *root = BTRFS_I(dir)->root;
2171 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002172 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002173 int err;
2174 int drop_inode = 0;
2175
2176 if (inode->i_nlink == 0)
2177 return -ENOENT;
2178
Chris Mason6da6aba2007-12-18 16:15:09 -05002179#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2180 inode->i_nlink++;
2181#else
Chris Mason39279cc2007-06-12 06:35:45 -04002182 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002183#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002184 err = btrfs_check_free_space(root, 1, 0);
2185 if (err)
2186 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002187 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002188
Chris Mason39279cc2007-06-12 06:35:45 -04002189 btrfs_set_trans_block_group(trans, dir);
2190 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002191 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002192
Chris Mason39279cc2007-06-12 06:35:45 -04002193 if (err)
2194 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002195
Chris Mason39279cc2007-06-12 06:35:45 -04002196 dir->i_sb->s_dirt = 1;
2197 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002198 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002199
Chris Mason54aa1f42007-06-22 14:16:25 -04002200 if (err)
2201 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002202
Chris Masond3c2fdc2007-09-17 10:58:06 -04002203 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002204 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002205fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002206 if (drop_inode) {
2207 inode_dec_link_count(inode);
2208 iput(inode);
2209 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002210 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002211 return err;
2212}
2213
Chris Mason39279cc2007-06-12 06:35:45 -04002214static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2215{
Chris Masonb9d86662008-05-02 16:13:49 -04002216 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002217 struct btrfs_trans_handle *trans;
2218 struct btrfs_root *root = BTRFS_I(dir)->root;
2219 int err = 0;
2220 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002221 u64 objectid = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002222 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002223
Chris Mason1832a6d2007-12-21 16:27:21 -05002224 err = btrfs_check_free_space(root, 1, 0);
2225 if (err)
2226 goto out_unlock;
2227
Chris Mason39279cc2007-06-12 06:35:45 -04002228 trans = btrfs_start_transaction(root, 1);
2229 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002230
Chris Mason39279cc2007-06-12 06:35:45 -04002231 if (IS_ERR(trans)) {
2232 err = PTR_ERR(trans);
2233 goto out_unlock;
2234 }
2235
2236 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2237 if (err) {
2238 err = -ENOSPC;
2239 goto out_unlock;
2240 }
2241
Chris Mason9c583092008-01-29 15:15:18 -05002242 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2243 dentry->d_name.len,
2244 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002245 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2246 if (IS_ERR(inode)) {
2247 err = PTR_ERR(inode);
2248 goto out_fail;
2249 }
Chris Mason5f39d392007-10-15 16:14:19 -04002250
Chris Mason39279cc2007-06-12 06:35:45 -04002251 drop_on_err = 1;
2252 inode->i_op = &btrfs_dir_inode_operations;
2253 inode->i_fop = &btrfs_dir_file_operations;
2254 btrfs_set_trans_block_group(trans, inode);
2255
Chris Masondbe674a2008-07-17 12:54:05 -04002256 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002257 err = btrfs_update_inode(trans, root, inode);
2258 if (err)
2259 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002260
Chris Mason9c583092008-01-29 15:15:18 -05002261 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002262 if (err)
2263 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002264
Chris Mason39279cc2007-06-12 06:35:45 -04002265 d_instantiate(dentry, inode);
2266 drop_on_err = 0;
2267 dir->i_sb->s_dirt = 1;
2268 btrfs_update_inode_block_group(trans, inode);
2269 btrfs_update_inode_block_group(trans, dir);
2270
2271out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002272 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002273 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002274
Chris Mason39279cc2007-06-12 06:35:45 -04002275out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002276 if (drop_on_err)
2277 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002278 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002279 return err;
2280}
2281
Chris Mason3b951512008-04-17 11:29:12 -04002282static int merge_extent_mapping(struct extent_map_tree *em_tree,
2283 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002284 struct extent_map *em,
2285 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002286{
2287 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002288
Chris Masone6dcd2d2008-07-17 12:53:50 -04002289 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2290 start_diff = map_start - em->start;
2291 em->start = map_start;
2292 em->len = map_len;
2293 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2294 em->block_start += start_diff;
2295 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002296}
2297
Chris Masona52d9a82007-08-27 16:49:44 -04002298struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002299 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002300 int create)
2301{
2302 int ret;
2303 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002304 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002305 u64 extent_start = 0;
2306 u64 extent_end = 0;
2307 u64 objectid = inode->i_ino;
2308 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002309 struct btrfs_path *path;
2310 struct btrfs_root *root = BTRFS_I(inode)->root;
2311 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002312 struct extent_buffer *leaf;
2313 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002314 struct extent_map *em = NULL;
2315 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002316 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002317 struct btrfs_trans_handle *trans = NULL;
2318
2319 path = btrfs_alloc_path();
2320 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002321
2322again:
Chris Masond1310b22008-01-24 16:13:08 -05002323 spin_lock(&em_tree->lock);
2324 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002325 if (em)
2326 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002327 spin_unlock(&em_tree->lock);
2328
Chris Masona52d9a82007-08-27 16:49:44 -04002329 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002330 if (em->start > start || em->start + em->len <= start)
2331 free_extent_map(em);
2332 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002333 free_extent_map(em);
2334 else
2335 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002336 }
Chris Masond1310b22008-01-24 16:13:08 -05002337 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002338 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002339 err = -ENOMEM;
2340 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002341 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002342 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002343 em->start = EXTENT_MAP_HOLE;
2344 em->len = (u64)-1;
Chris Mason179e29e2007-11-01 11:28:41 -04002345 ret = btrfs_lookup_file_extent(trans, root, path,
2346 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002347 if (ret < 0) {
2348 err = ret;
2349 goto out;
2350 }
2351
2352 if (ret != 0) {
2353 if (path->slots[0] == 0)
2354 goto not_found;
2355 path->slots[0]--;
2356 }
2357
Chris Mason5f39d392007-10-15 16:14:19 -04002358 leaf = path->nodes[0];
2359 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002360 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002361 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2363 found_type = btrfs_key_type(&found_key);
2364 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002365 found_type != BTRFS_EXTENT_DATA_KEY) {
2366 goto not_found;
2367 }
2368
Chris Mason5f39d392007-10-15 16:14:19 -04002369 found_type = btrfs_file_extent_type(leaf, item);
2370 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002371 if (found_type == BTRFS_FILE_EXTENT_REG) {
2372 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002373 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002374 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002375 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002376 em->start = start;
2377 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002378 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002379 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002380 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002381 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002382 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002383 }
2384 goto not_found_em;
2385 }
Chris Masondb945352007-10-15 16:15:53 -04002386 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2387 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002388 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002389 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002390 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002391 goto insert;
2392 }
Chris Masondb945352007-10-15 16:15:53 -04002393 bytenr += btrfs_file_extent_offset(leaf, item);
2394 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002395 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002396 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002397 goto insert;
2398 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002399 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002400 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002401 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002402 size_t size;
2403 size_t extent_offset;
2404 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002405
Chris Mason5f39d392007-10-15 16:14:19 -04002406 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2407 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002408 extent_end = (extent_start + size + root->sectorsize - 1) &
2409 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002410 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002411 em->start = start;
2412 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002413 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002414 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002415 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002416 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002417 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002418 }
2419 goto not_found_em;
2420 }
2421 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002422
2423 if (!page) {
2424 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002425 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002426 goto out;
2427 }
2428
Chris Mason70dec802008-01-29 09:59:12 -05002429 page_start = page_offset(page) + pg_offset;
2430 extent_offset = page_start - extent_start;
2431 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002432 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002433 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002434 em->len = (copy_size + root->sectorsize - 1) &
2435 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002436 map = kmap(page);
2437 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002438 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002439 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002440 copy_size);
2441 flush_dcache_page(page);
2442 } else if (create && PageUptodate(page)) {
2443 if (!trans) {
2444 kunmap(page);
2445 free_extent_map(em);
2446 em = NULL;
2447 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002448 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002449 goto again;
2450 }
Chris Mason70dec802008-01-29 09:59:12 -05002451 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002452 copy_size);
2453 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002454 }
Chris Masona52d9a82007-08-27 16:49:44 -04002455 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002456 set_extent_uptodate(io_tree, em->start,
2457 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002458 goto insert;
2459 } else {
2460 printk("unkknown found_type %d\n", found_type);
2461 WARN_ON(1);
2462 }
2463not_found:
2464 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002465 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002466not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002467 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002468insert:
2469 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002470 if (em->start > start || extent_map_end(em) <= start) {
2471 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002472 err = -EIO;
2473 goto out;
2474 }
Chris Masond1310b22008-01-24 16:13:08 -05002475
2476 err = 0;
2477 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002478 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002479 /* it is possible that someone inserted the extent into the tree
2480 * while we had the lock dropped. It is also possible that
2481 * an overlapping map exists in the tree
2482 */
Chris Masona52d9a82007-08-27 16:49:44 -04002483 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002484 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002485
2486 ret = 0;
2487
Chris Mason3b951512008-04-17 11:29:12 -04002488 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002489 if (existing && (existing->start > start ||
2490 existing->start + existing->len <= start)) {
2491 free_extent_map(existing);
2492 existing = NULL;
2493 }
Chris Mason3b951512008-04-17 11:29:12 -04002494 if (!existing) {
2495 existing = lookup_extent_mapping(em_tree, em->start,
2496 em->len);
2497 if (existing) {
2498 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002499 em, start,
2500 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002501 free_extent_map(existing);
2502 if (err) {
2503 free_extent_map(em);
2504 em = NULL;
2505 }
2506 } else {
2507 err = -EIO;
2508 printk("failing to insert %Lu %Lu\n",
2509 start, len);
2510 free_extent_map(em);
2511 em = NULL;
2512 }
2513 } else {
2514 free_extent_map(em);
2515 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002516 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002517 }
Chris Masona52d9a82007-08-27 16:49:44 -04002518 }
Chris Masond1310b22008-01-24 16:13:08 -05002519 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002520out:
2521 btrfs_free_path(path);
2522 if (trans) {
2523 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002524 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002525 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002526 }
Chris Masona52d9a82007-08-27 16:49:44 -04002527 }
Chris Masona52d9a82007-08-27 16:49:44 -04002528 if (err) {
2529 free_extent_map(em);
2530 WARN_ON(1);
2531 return ERR_PTR(err);
2532 }
2533 return em;
2534}
2535
Chris Masone1c4b742008-04-22 13:26:46 -04002536#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002537static int btrfs_get_block(struct inode *inode, sector_t iblock,
2538 struct buffer_head *bh_result, int create)
2539{
2540 struct extent_map *em;
2541 u64 start = (u64)iblock << inode->i_blkbits;
2542 struct btrfs_multi_bio *multi = NULL;
2543 struct btrfs_root *root = BTRFS_I(inode)->root;
2544 u64 len;
2545 u64 logical;
2546 u64 map_length;
2547 int ret = 0;
2548
2549 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2550
2551 if (!em || IS_ERR(em))
2552 goto out;
2553
Chris Masone1c4b742008-04-22 13:26:46 -04002554 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002555 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002556 }
Chris Mason16432982008-04-10 10:23:21 -04002557
2558 if (em->block_start == EXTENT_MAP_INLINE) {
2559 ret = -EINVAL;
2560 goto out;
2561 }
2562
Chris Mason16432982008-04-10 10:23:21 -04002563 len = em->start + em->len - start;
2564 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2565
Chris Masone1c4b742008-04-22 13:26:46 -04002566 if (em->block_start == EXTENT_MAP_HOLE ||
2567 em->block_start == EXTENT_MAP_DELALLOC) {
2568 bh_result->b_size = len;
2569 goto out;
2570 }
2571
Chris Mason16432982008-04-10 10:23:21 -04002572 logical = start - em->start;
2573 logical = em->block_start + logical;
2574
2575 map_length = len;
2576 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2577 logical, &map_length, &multi, 0);
2578 BUG_ON(ret);
2579 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2580 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002581
Chris Mason16432982008-04-10 10:23:21 -04002582 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2583 set_buffer_mapped(bh_result);
2584 kfree(multi);
2585out:
2586 free_extent_map(em);
2587 return ret;
2588}
Chris Masone1c4b742008-04-22 13:26:46 -04002589#endif
Chris Mason16432982008-04-10 10:23:21 -04002590
2591static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2592 const struct iovec *iov, loff_t offset,
2593 unsigned long nr_segs)
2594{
Chris Masone1c4b742008-04-22 13:26:46 -04002595 return -EINVAL;
2596#if 0
Chris Mason16432982008-04-10 10:23:21 -04002597 struct file *file = iocb->ki_filp;
2598 struct inode *inode = file->f_mapping->host;
2599
2600 if (rw == WRITE)
2601 return -EINVAL;
2602
2603 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2604 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002605#endif
Chris Mason16432982008-04-10 10:23:21 -04002606}
2607
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002608static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002609{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002610 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002611}
2612
Chris Mason9ebefb182007-06-15 13:50:00 -04002613int btrfs_readpage(struct file *file, struct page *page)
2614{
Chris Masond1310b22008-01-24 16:13:08 -05002615 struct extent_io_tree *tree;
2616 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002617 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002618}
Chris Mason1832a6d2007-12-21 16:27:21 -05002619
Chris Mason39279cc2007-06-12 06:35:45 -04002620static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2621{
Chris Masond1310b22008-01-24 16:13:08 -05002622 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002623
2624
2625 if (current->flags & PF_MEMALLOC) {
2626 redirty_page_for_writepage(wbc, page);
2627 unlock_page(page);
2628 return 0;
2629 }
Chris Masond1310b22008-01-24 16:13:08 -05002630 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002631 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2632}
Chris Mason39279cc2007-06-12 06:35:45 -04002633
Chris Masonb293f022007-11-01 19:45:34 -04002634static int btrfs_writepages(struct address_space *mapping,
2635 struct writeback_control *wbc)
2636{
Chris Masond1310b22008-01-24 16:13:08 -05002637 struct extent_io_tree *tree;
2638 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002639 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2640}
2641
Chris Mason3ab2fb52007-11-08 10:59:22 -05002642static int
2643btrfs_readpages(struct file *file, struct address_space *mapping,
2644 struct list_head *pages, unsigned nr_pages)
2645{
Chris Masond1310b22008-01-24 16:13:08 -05002646 struct extent_io_tree *tree;
2647 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002648 return extent_readpages(tree, mapping, pages, nr_pages,
2649 btrfs_get_extent);
2650}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002651static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002652{
Chris Masond1310b22008-01-24 16:13:08 -05002653 struct extent_io_tree *tree;
2654 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002655 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002656
Chris Masond1310b22008-01-24 16:13:08 -05002657 tree = &BTRFS_I(page->mapping->host)->io_tree;
2658 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002659 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002660 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002661 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002662 ClearPagePrivate(page);
2663 set_page_private(page, 0);
2664 page_cache_release(page);
2665 }
2666 return ret;
2667}
Chris Mason39279cc2007-06-12 06:35:45 -04002668
Chris Masone6dcd2d2008-07-17 12:53:50 -04002669static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2670{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002671 return __btrfs_releasepage(page, gfp_flags);
2672}
2673
Chris Masona52d9a82007-08-27 16:49:44 -04002674static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2675{
Chris Masond1310b22008-01-24 16:13:08 -05002676 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002677 struct btrfs_ordered_extent *ordered;
2678 u64 page_start = page_offset(page);
2679 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002680
Chris Masone6dcd2d2008-07-17 12:53:50 -04002681 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002682 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002683 if (offset) {
2684 btrfs_releasepage(page, GFP_NOFS);
2685 return;
2686 }
2687
2688 lock_extent(tree, page_start, page_end, GFP_NOFS);
2689 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2690 page_offset(page));
2691 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002692 /*
2693 * IO on this page will never be started, so we need
2694 * to account for any ordered extents now
2695 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002696 clear_extent_bit(tree, page_start, page_end,
2697 EXTENT_DIRTY | EXTENT_DELALLOC |
2698 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002699 btrfs_finish_ordered_io(page->mapping->host,
2700 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002701 btrfs_put_ordered_extent(ordered);
2702 lock_extent(tree, page_start, page_end, GFP_NOFS);
2703 }
2704 clear_extent_bit(tree, page_start, page_end,
2705 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2706 EXTENT_ORDERED,
2707 1, 1, GFP_NOFS);
2708 __btrfs_releasepage(page, GFP_NOFS);
2709
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002710 if (PagePrivate(page)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002711 invalidate_extent_lru(tree, page_offset(page),
2712 PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002713 ClearPagePrivate(page);
2714 set_page_private(page, 0);
2715 page_cache_release(page);
2716 }
Chris Mason39279cc2007-06-12 06:35:45 -04002717}
2718
Chris Mason9ebefb182007-06-15 13:50:00 -04002719/*
2720 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2721 * called from a page fault handler when a page is first dirtied. Hence we must
2722 * be careful to check for EOF conditions here. We set the page up correctly
2723 * for a written page which means we get ENOSPC checking when writing into
2724 * holes and correct delalloc and unwritten extent mapping on filesystems that
2725 * support these features.
2726 *
2727 * We are not allowed to take the i_mutex here so we have to play games to
2728 * protect against truncate races as the page could now be beyond EOF. Because
2729 * vmtruncate() writes the inode size before removing pages, once we have the
2730 * page lock we can determine safely if the page is beyond EOF. If it is not
2731 * beyond EOF, then the page is guaranteed safe against truncation until we
2732 * unlock the page.
2733 */
2734int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2735{
Chris Mason6da6aba2007-12-18 16:15:09 -05002736 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002737 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002738 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2739 struct btrfs_ordered_extent *ordered;
2740 char *kaddr;
2741 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002742 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002743 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002744 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002745 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002746
Chris Mason1832a6d2007-12-21 16:27:21 -05002747 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002748 if (ret)
2749 goto out;
2750
2751 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002752again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002753 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002754 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755 page_start = page_offset(page);
2756 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002757
Chris Mason9ebefb182007-06-15 13:50:00 -04002758 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002759 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002760 /* page got truncated out from underneath us */
2761 goto out_unlock;
2762 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002763 wait_on_page_writeback(page);
2764
2765 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2766 set_page_extent_mapped(page);
2767
Chris Masoneb84ae02008-07-17 13:53:27 -04002768 /*
2769 * we can't set the delalloc bits if there are pending ordered
2770 * extents. Drop our locks and wait for them to finish
2771 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2773 if (ordered) {
2774 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2775 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002776 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002777 btrfs_put_ordered_extent(ordered);
2778 goto again;
2779 }
2780
2781 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2782 page_end, GFP_NOFS);
2783 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002784
2785 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002786 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002787 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002788 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002789 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002790
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791 if (zero_start != PAGE_CACHE_SIZE) {
2792 kaddr = kmap(page);
2793 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2794 flush_dcache_page(page);
2795 kunmap(page);
2796 }
Chris Mason247e7432008-07-17 12:53:51 -04002797 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002798 set_page_dirty(page);
2799 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002800
2801out_unlock:
2802 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002803out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002804 return ret;
2805}
2806
Chris Mason39279cc2007-06-12 06:35:45 -04002807static void btrfs_truncate(struct inode *inode)
2808{
2809 struct btrfs_root *root = BTRFS_I(inode)->root;
2810 int ret;
2811 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002812 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04002813 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002814
2815 if (!S_ISREG(inode->i_mode))
2816 return;
2817 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2818 return;
2819
2820 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2821
Chris Mason39279cc2007-06-12 06:35:45 -04002822 trans = btrfs_start_transaction(root, 1);
2823 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04002824 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
2825 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002826
2827 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002828 ret = btrfs_truncate_in_trans(trans, root, inode,
2829 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002830 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002831 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002832
Chris Mason89ce8a62008-06-25 16:01:31 -04002833 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002834 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002835 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002836}
2837
Sven Wegener3b963622008-06-09 21:57:42 -04002838/*
2839 * Invalidate a single dcache entry at the root of the filesystem.
2840 * Needed after creation of snapshot or subvolume.
2841 */
2842void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2843 int namelen)
2844{
2845 struct dentry *alias, *entry;
2846 struct qstr qstr;
2847
2848 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2849 if (alias) {
2850 qstr.name = name;
2851 qstr.len = namelen;
2852 /* change me if btrfs ever gets a d_hash operation */
2853 qstr.hash = full_name_hash(qstr.name, qstr.len);
2854 entry = d_lookup(alias, &qstr);
2855 dput(alias);
2856 if (entry) {
2857 d_invalidate(entry);
2858 dput(entry);
2859 }
2860 }
2861}
2862
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002863int btrfs_create_subvol_root(struct btrfs_root *new_root,
2864 struct btrfs_trans_handle *trans, u64 new_dirid,
2865 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002866{
Chris Mason39279cc2007-06-12 06:35:45 -04002867 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002868 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002869
Chris Mason9c583092008-01-29 15:15:18 -05002870 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002871 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002872 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002873 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002874 inode->i_op = &btrfs_dir_inode_operations;
2875 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002876 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002877
Chris Mason39544012007-12-12 14:38:19 -05002878 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2879 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002880 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04002881 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04002882
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002883 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002884}
2885
Chris Masonedbd8d42007-12-21 16:27:24 -05002886unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002887 struct file_ra_state *ra, struct file *file,
2888 pgoff_t offset, pgoff_t last_index)
2889{
Chris Mason8e7bf942008-04-28 09:02:36 -04002890 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002891
2892#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002893 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2894 return offset;
2895#else
Chris Mason86479a02007-09-10 19:58:16 -04002896 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2897 return offset + req_size;
2898#endif
2899}
2900
Chris Mason39279cc2007-06-12 06:35:45 -04002901struct inode *btrfs_alloc_inode(struct super_block *sb)
2902{
2903 struct btrfs_inode *ei;
2904
2905 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2906 if (!ei)
2907 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002908 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002909 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002910 return &ei->vfs_inode;
2911}
2912
2913void btrfs_destroy_inode(struct inode *inode)
2914{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002915 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04002916 WARN_ON(!list_empty(&inode->i_dentry));
2917 WARN_ON(inode->i_data.nrpages);
2918
Chris Masone6dcd2d2008-07-17 12:53:50 -04002919 while(1) {
2920 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2921 if (!ordered)
2922 break;
2923 else {
2924 printk("found ordered extent %Lu %Lu\n",
2925 ordered->file_offset, ordered->len);
2926 btrfs_remove_ordered_extent(inode, ordered);
2927 btrfs_put_ordered_extent(ordered);
2928 btrfs_put_ordered_extent(ordered);
2929 }
2930 }
Chris Mason8c416c92008-01-14 15:10:26 -05002931 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002932 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2933}
2934
Chris Mason44ec0b72007-10-29 10:55:05 -04002935#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2936static void init_once(struct kmem_cache * cachep, void *foo)
2937#else
Chris Mason39279cc2007-06-12 06:35:45 -04002938static void init_once(void * foo, struct kmem_cache * cachep,
2939 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002940#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002941{
2942 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2943
2944 inode_init_once(&ei->vfs_inode);
2945}
2946
2947void btrfs_destroy_cachep(void)
2948{
2949 if (btrfs_inode_cachep)
2950 kmem_cache_destroy(btrfs_inode_cachep);
2951 if (btrfs_trans_handle_cachep)
2952 kmem_cache_destroy(btrfs_trans_handle_cachep);
2953 if (btrfs_transaction_cachep)
2954 kmem_cache_destroy(btrfs_transaction_cachep);
2955 if (btrfs_bit_radix_cachep)
2956 kmem_cache_destroy(btrfs_bit_radix_cachep);
2957 if (btrfs_path_cachep)
2958 kmem_cache_destroy(btrfs_path_cachep);
2959}
2960
Chris Mason86479a02007-09-10 19:58:16 -04002961struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002962 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002963#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2964 void (*ctor)(struct kmem_cache *, void *)
2965#else
Chris Mason92fee662007-07-25 12:31:35 -04002966 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002967 unsigned long)
2968#endif
2969 )
Chris Mason92fee662007-07-25 12:31:35 -04002970{
2971 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2972 SLAB_MEM_SPREAD | extra_flags), ctor
2973#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2974 ,NULL
2975#endif
2976 );
2977}
2978
Chris Mason39279cc2007-06-12 06:35:45 -04002979int btrfs_init_cachep(void)
2980{
Chris Mason86479a02007-09-10 19:58:16 -04002981 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002982 sizeof(struct btrfs_inode),
2983 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002984 if (!btrfs_inode_cachep)
2985 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002986 btrfs_trans_handle_cachep =
2987 btrfs_cache_create("btrfs_trans_handle_cache",
2988 sizeof(struct btrfs_trans_handle),
2989 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002990 if (!btrfs_trans_handle_cachep)
2991 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002992 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002993 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002994 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002995 if (!btrfs_transaction_cachep)
2996 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002997 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002998 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002999 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003000 if (!btrfs_path_cachep)
3001 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003002 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003003 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003004 if (!btrfs_bit_radix_cachep)
3005 goto fail;
3006 return 0;
3007fail:
3008 btrfs_destroy_cachep();
3009 return -ENOMEM;
3010}
3011
3012static int btrfs_getattr(struct vfsmount *mnt,
3013 struct dentry *dentry, struct kstat *stat)
3014{
3015 struct inode *inode = dentry->d_inode;
3016 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003017 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003018 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003019 return 0;
3020}
3021
3022static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3023 struct inode * new_dir,struct dentry *new_dentry)
3024{
3025 struct btrfs_trans_handle *trans;
3026 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3027 struct inode *new_inode = new_dentry->d_inode;
3028 struct inode *old_inode = old_dentry->d_inode;
3029 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003030 int ret;
3031
3032 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3033 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3034 return -ENOTEMPTY;
3035 }
Chris Mason5f39d392007-10-15 16:14:19 -04003036
Chris Mason1832a6d2007-12-21 16:27:21 -05003037 ret = btrfs_check_free_space(root, 1, 0);
3038 if (ret)
3039 goto out_unlock;
3040
Chris Mason39279cc2007-06-12 06:35:45 -04003041 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003042
Chris Mason39279cc2007-06-12 06:35:45 -04003043 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003044
3045 old_dentry->d_inode->i_nlink++;
3046 old_dir->i_ctime = old_dir->i_mtime = ctime;
3047 new_dir->i_ctime = new_dir->i_mtime = ctime;
3048 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003049
Chris Mason39279cc2007-06-12 06:35:45 -04003050 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3051 if (ret)
3052 goto out_fail;
3053
3054 if (new_inode) {
3055 new_inode->i_ctime = CURRENT_TIME;
3056 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3057 if (ret)
3058 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003059 }
Chris Mason9c583092008-01-29 15:15:18 -05003060 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003061 if (ret)
3062 goto out_fail;
3063
3064out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003065 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003066out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003067 return ret;
3068}
3069
3070static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3071 const char *symname)
3072{
3073 struct btrfs_trans_handle *trans;
3074 struct btrfs_root *root = BTRFS_I(dir)->root;
3075 struct btrfs_path *path;
3076 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003077 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003078 int err;
3079 int drop_inode = 0;
3080 u64 objectid;
3081 int name_len;
3082 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003083 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003084 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003085 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003086 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003087
3088 name_len = strlen(symname) + 1;
3089 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3090 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003091
Chris Mason1832a6d2007-12-21 16:27:21 -05003092 err = btrfs_check_free_space(root, 1, 0);
3093 if (err)
3094 goto out_fail;
3095
Chris Mason39279cc2007-06-12 06:35:45 -04003096 trans = btrfs_start_transaction(root, 1);
3097 btrfs_set_trans_block_group(trans, dir);
3098
3099 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3100 if (err) {
3101 err = -ENOSPC;
3102 goto out_unlock;
3103 }
3104
Chris Mason9c583092008-01-29 15:15:18 -05003105 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3106 dentry->d_name.len,
3107 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003108 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3109 err = PTR_ERR(inode);
3110 if (IS_ERR(inode))
3111 goto out_unlock;
3112
3113 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003114 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003115 if (err)
3116 drop_inode = 1;
3117 else {
3118 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003119 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003120 inode->i_fop = &btrfs_file_operations;
3121 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003122 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3123 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003124 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003125 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3126 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003127 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003128 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003129 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003130 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003131 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003132 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003133 }
3134 dir->i_sb->s_dirt = 1;
3135 btrfs_update_inode_block_group(trans, inode);
3136 btrfs_update_inode_block_group(trans, dir);
3137 if (drop_inode)
3138 goto out_unlock;
3139
3140 path = btrfs_alloc_path();
3141 BUG_ON(!path);
3142 key.objectid = inode->i_ino;
3143 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3145 datasize = btrfs_file_extent_calc_inline_size(name_len);
3146 err = btrfs_insert_empty_item(trans, root, path, &key,
3147 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003148 if (err) {
3149 drop_inode = 1;
3150 goto out_unlock;
3151 }
Chris Mason5f39d392007-10-15 16:14:19 -04003152 leaf = path->nodes[0];
3153 ei = btrfs_item_ptr(leaf, path->slots[0],
3154 struct btrfs_file_extent_item);
3155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3156 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003157 BTRFS_FILE_EXTENT_INLINE);
3158 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003159 write_extent_buffer(leaf, symname, ptr, name_len);
3160 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003161 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003162
Chris Mason39279cc2007-06-12 06:35:45 -04003163 inode->i_op = &btrfs_symlink_inode_operations;
3164 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003165 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003166 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003167 err = btrfs_update_inode(trans, root, inode);
3168 if (err)
3169 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003170
3171out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003172 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003173 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003174out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003175 if (drop_inode) {
3176 inode_dec_link_count(inode);
3177 iput(inode);
3178 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003179 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003180 return err;
3181}
Chris Mason16432982008-04-10 10:23:21 -04003182
Chris Masone6dcd2d2008-07-17 12:53:50 -04003183static int btrfs_set_page_dirty(struct page *page)
3184{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003185 return __set_page_dirty_nobuffers(page);
3186}
3187
Yanfdebe2b2008-01-14 13:26:08 -05003188static int btrfs_permission(struct inode *inode, int mask,
3189 struct nameidata *nd)
3190{
3191 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3192 return -EACCES;
3193 return generic_permission(inode, mask, NULL);
3194}
Chris Mason39279cc2007-06-12 06:35:45 -04003195
3196static struct inode_operations btrfs_dir_inode_operations = {
3197 .lookup = btrfs_lookup,
3198 .create = btrfs_create,
3199 .unlink = btrfs_unlink,
3200 .link = btrfs_link,
3201 .mkdir = btrfs_mkdir,
3202 .rmdir = btrfs_rmdir,
3203 .rename = btrfs_rename,
3204 .symlink = btrfs_symlink,
3205 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003206 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003207 .setxattr = generic_setxattr,
3208 .getxattr = generic_getxattr,
3209 .listxattr = btrfs_listxattr,
3210 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003211 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003212};
Chris Mason39279cc2007-06-12 06:35:45 -04003213static struct inode_operations btrfs_dir_ro_inode_operations = {
3214 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003215 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003216};
Chris Mason39279cc2007-06-12 06:35:45 -04003217static struct file_operations btrfs_dir_file_operations = {
3218 .llseek = generic_file_llseek,
3219 .read = generic_read_dir,
3220 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003221 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003222#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003223 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003224#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003225 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003226};
3227
Chris Masond1310b22008-01-24 16:13:08 -05003228static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003229 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003230 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003231 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003232 .readpage_io_hook = btrfs_readpage_io_hook,
3233 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003234 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003235 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003236 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003237 .set_bit_hook = btrfs_set_bit_hook,
3238 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003239};
3240
Chris Mason39279cc2007-06-12 06:35:45 -04003241static struct address_space_operations btrfs_aops = {
3242 .readpage = btrfs_readpage,
3243 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003244 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003245 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003246 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003247 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003248 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003249 .invalidatepage = btrfs_invalidatepage,
3250 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003251 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003252};
3253
3254static struct address_space_operations btrfs_symlink_aops = {
3255 .readpage = btrfs_readpage,
3256 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003257 .invalidatepage = btrfs_invalidatepage,
3258 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003259};
3260
3261static struct inode_operations btrfs_file_inode_operations = {
3262 .truncate = btrfs_truncate,
3263 .getattr = btrfs_getattr,
3264 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003265 .setxattr = generic_setxattr,
3266 .getxattr = generic_getxattr,
3267 .listxattr = btrfs_listxattr,
3268 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003269 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003270};
Josef Bacik618e21d2007-07-11 10:18:17 -04003271static struct inode_operations btrfs_special_inode_operations = {
3272 .getattr = btrfs_getattr,
3273 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003274 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003275};
Chris Mason39279cc2007-06-12 06:35:45 -04003276static struct inode_operations btrfs_symlink_inode_operations = {
3277 .readlink = generic_readlink,
3278 .follow_link = page_follow_link_light,
3279 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003280 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003281};