blob: 2d8853543a714538df495c655b0c45c51b618216 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Chris Mason1832a6d2007-12-21 16:27:21 -050081int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
82 int for_del)
83{
Chris Masona2135012008-06-25 16:01:30 -040084 u64 total;
85 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050086 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040087 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 int ret = 0;
89
Chris Masona2135012008-06-25 16:01:30 -040090 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
91 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
92 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050093 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050094 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050095 else
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 85;
97
98 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050099
Chris Mason1832a6d2007-12-21 16:27:21 -0500100 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
101 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400102 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500103 return ret;
104}
105
Chris Masonbe20aa92007-12-17 20:14:01 -0500106static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400107{
108 struct btrfs_root *root = BTRFS_I(inode)->root;
109 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400110 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400111 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500112 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500114 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400116 struct extent_map *em;
117 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
118 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400119
Chris Masonf9295742008-07-17 12:54:14 -0400120 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400121 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500122 btrfs_set_trans_block_group(trans, inode);
123
Chris Masondb945352007-10-15 16:15:53 -0400124 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500125 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500126 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400127
Chris Mason179e29e2007-11-01 11:28:41 -0400128 if (alloc_hint == EXTENT_MAP_INLINE)
129 goto out;
130
Chris Mason3b951512008-04-17 11:29:12 -0400131 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400132 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400133 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400135
Chris Masonc59f8952007-12-17 20:14:04 -0500136 while(num_bytes > 0) {
137 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400138 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
139 root->sectorsize, 0, 0,
140 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500141 if (ret) {
142 WARN_ON(1);
143 goto out;
144 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400145 em = alloc_extent_map(GFP_NOFS);
146 em->start = start;
147 em->len = ins.offset;
148 em->block_start = ins.objectid;
149 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400150 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400151 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400152 while(1) {
153 spin_lock(&em_tree->lock);
154 ret = add_extent_mapping(em_tree, em);
155 spin_unlock(&em_tree->lock);
156 if (ret != -EEXIST) {
157 free_extent_map(em);
158 break;
159 }
160 btrfs_drop_extent_cache(inode, start,
161 start + ins.offset - 1);
162 }
Chris Masone5a22172008-07-18 20:42:20 -0400163 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400164
Chris Mason98d20f62008-04-14 09:46:10 -0400165 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
167 ins.offset);
168 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400169 if (num_bytes < cur_alloc_size) {
170 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
171 cur_alloc_size);
172 break;
173 }
Chris Masonc59f8952007-12-17 20:14:04 -0500174 num_bytes -= cur_alloc_size;
175 alloc_hint = ins.objectid + ins.offset;
176 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400177 }
Chris Masonb888db22007-08-27 16:49:44 -0400178out:
179 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500180 return ret;
181}
182
183static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
184{
185 u64 extent_start;
186 u64 extent_end;
187 u64 bytenr;
188 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500189 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500190 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500191 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400192 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 struct extent_buffer *leaf;
194 int found_type;
195 struct btrfs_path *path;
196 struct btrfs_file_extent_item *item;
197 int ret;
198 int err;
199 struct btrfs_key found_key;
200
Chris Masonc31f8832008-01-08 15:46:31 -0500201 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500202 path = btrfs_alloc_path();
203 BUG_ON(!path);
204again:
205 ret = btrfs_lookup_file_extent(NULL, root, path,
206 inode->i_ino, start, 0);
207 if (ret < 0) {
208 btrfs_free_path(path);
209 return ret;
210 }
211
212 cow_end = end;
213 if (ret != 0) {
214 if (path->slots[0] == 0)
215 goto not_found;
216 path->slots[0]--;
217 }
218
219 leaf = path->nodes[0];
220 item = btrfs_item_ptr(leaf, path->slots[0],
221 struct btrfs_file_extent_item);
222
223 /* are we inside the extent that was found? */
224 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
225 found_type = btrfs_key_type(&found_key);
226 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400227 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500229
230 found_type = btrfs_file_extent_type(leaf, item);
231 extent_start = found_key.offset;
232 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500233 u64 extent_num_bytes;
234
235 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
236 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500237 err = 0;
238
Chris Mason1832a6d2007-12-21 16:27:21 -0500239 if (loops && start != extent_start)
240 goto not_found;
241
Chris Masonbe20aa92007-12-17 20:14:01 -0500242 if (start < extent_start || start >= extent_end)
243 goto not_found;
244
245 cow_end = min(end, extent_end - 1);
246 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
247 if (bytenr == 0)
248 goto not_found;
249
Chris Masona68d5932008-05-08 14:11:56 -0400250 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
251 bytenr) != 1) {
252 goto not_found;
253 }
254
Chris Masonc31f8832008-01-08 15:46:31 -0500255 /*
256 * we may be called by the resizer, make sure we're inside
257 * the limits of the FS
258 */
Chris Masona68d5932008-05-08 14:11:56 -0400259 block_group = btrfs_lookup_block_group(root->fs_info,
260 bytenr);
261 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500262 goto not_found;
263
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500265 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500266 goto not_found;
267 }
268loop:
269 if (start > end) {
270 btrfs_free_path(path);
271 return 0;
272 }
273 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500274 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500275 goto again;
276
277not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400278 cow_file_range(inode, start, end);
279 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 goto loop;
281}
282
283static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
284{
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400287
Yanb98b6762008-01-08 15:54:37 -0500288 if (btrfs_test_opt(root, NODATACOW) ||
289 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500290 ret = run_delalloc_nocow(inode, start, end);
291 else
292 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500293
Chris Masonb888db22007-08-27 16:49:44 -0400294 return ret;
295}
296
Chris Mason291d6732008-01-29 15:55:23 -0500297int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500301 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500302 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500304 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500305 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400306 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500307 }
308 return 0;
309}
310
311int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500312 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Chris Masonb0c68f82008-01-31 11:05:37 -0500314 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500315 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400316 unsigned long flags;
317
318 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500319 if (end - start + 1 > root->fs_info->delalloc_bytes) {
320 printk("warning: delalloc account %Lu %Lu\n",
321 end - start + 1, root->fs_info->delalloc_bytes);
322 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500323 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500324 } else {
325 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500326 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500327 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400328 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500329 }
330 return 0;
331}
332
Chris Mason239b14b2008-03-24 15:02:07 -0400333int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
334 size_t size, struct bio *bio)
335{
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
337 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400339 u64 length = 0;
340 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400341 int ret;
342
Chris Masonf2d8d742008-04-21 10:03:05 -0400343 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400344 map_tree = &root->fs_info->mapping_tree;
345 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400346 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400347 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400348
Chris Mason239b14b2008-03-24 15:02:07 -0400349 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400350 return 1;
351 }
352 return 0;
353}
354
Chris Mason44b8bd72008-04-16 11:14:51 -0400355int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400356 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500357{
Chris Mason065631f2008-02-20 12:07:25 -0500358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500359 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400360
Chris Mason3edf7d32008-07-18 06:17:13 -0400361 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400362 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400363
Chris Mason8b712842008-06-11 16:50:36 -0400364 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400365}
366
367int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
368 int mirror_num)
369{
370 struct btrfs_root *root = BTRFS_I(inode)->root;
371 int ret = 0;
372
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
374 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500375
Chris Masone6dcd2d2008-07-17 12:53:50 -0400376 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400377 goto mapit;
378 }
Chris Mason065631f2008-02-20 12:07:25 -0500379
Chris Mason44b8bd72008-04-16 11:14:51 -0400380 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
381 inode, rw, bio, mirror_num,
382 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400383mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400384 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500385}
Chris Mason6885f302008-02-20 16:11:05 -0500386
Chris Masonba1da2f2008-07-17 12:54:15 -0400387static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400388 struct inode *inode, u64 file_offset,
389 struct list_head *list)
390{
391 struct list_head *cur;
392 struct btrfs_ordered_sum *sum;
393
394 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400395 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 sum = list_entry(cur, struct btrfs_ordered_sum, list);
397 mutex_lock(&BTRFS_I(inode)->csum_mutex);
398 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
399 inode, sum);
400 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400401 }
402 return 0;
403}
404
Chris Mason247e7432008-07-17 12:53:51 -0400405struct btrfs_writepage_fixup {
406 struct page *page;
407 struct btrfs_work work;
408};
409
410/* see btrfs_writepage_start_hook for details on why this is required */
411void btrfs_writepage_fixup_worker(struct btrfs_work *work)
412{
413 struct btrfs_writepage_fixup *fixup;
414 struct btrfs_ordered_extent *ordered;
415 struct page *page;
416 struct inode *inode;
417 u64 page_start;
418 u64 page_end;
419
420 fixup = container_of(work, struct btrfs_writepage_fixup, work);
421 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400422again:
Chris Mason247e7432008-07-17 12:53:51 -0400423 lock_page(page);
424 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
425 ClearPageChecked(page);
426 goto out_page;
427 }
428
429 inode = page->mapping->host;
430 page_start = page_offset(page);
431 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
432
433 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400434
435 /* already ordered? We're done */
436 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
437 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400438 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400439 }
440
441 ordered = btrfs_lookup_ordered_extent(inode, page_start);
442 if (ordered) {
443 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
444 page_end, GFP_NOFS);
445 unlock_page(page);
446 btrfs_start_ordered_extent(inode, ordered, 1);
447 goto again;
448 }
Chris Mason247e7432008-07-17 12:53:51 -0400449
450 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
451 GFP_NOFS);
452 ClearPageChecked(page);
453out:
454 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
455out_page:
456 unlock_page(page);
457 page_cache_release(page);
458}
459
460/*
461 * There are a few paths in the higher layers of the kernel that directly
462 * set the page dirty bit without asking the filesystem if it is a
463 * good idea. This causes problems because we want to make sure COW
464 * properly happens and the data=ordered rules are followed.
465 *
466 * In our case any range that doesn't have the EXTENT_ORDERED bit set
467 * hasn't been properly setup for IO. We kick off an async process
468 * to fix it up. The async helper will wait for ordered extents, set
469 * the delalloc bit and make it safe to write the page.
470 */
471int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
472{
473 struct inode *inode = page->mapping->host;
474 struct btrfs_writepage_fixup *fixup;
475 struct btrfs_root *root = BTRFS_I(inode)->root;
476 int ret;
477
478 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
479 EXTENT_ORDERED, 0);
480 if (ret)
481 return 0;
482
483 if (PageChecked(page))
484 return -EAGAIN;
485
486 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
487 if (!fixup)
488 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400489
Chris Mason247e7432008-07-17 12:53:51 -0400490 SetPageChecked(page);
491 page_cache_get(page);
492 fixup->work.func = btrfs_writepage_fixup_worker;
493 fixup->page = page;
494 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
495 return -EAGAIN;
496}
497
Chris Mason211f90e2008-07-18 11:56:15 -0400498static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400500 struct btrfs_root *root = BTRFS_I(inode)->root;
501 struct btrfs_trans_handle *trans;
502 struct btrfs_ordered_extent *ordered_extent;
503 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
505 struct extent_map *em;
Chris Masonf4219502008-07-22 11:18:09 -0400506 struct extent_map *em_orig;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400507 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400508 u64 clear_start;
509 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400510 struct list_head list;
511 struct btrfs_key ins;
Chris Masonf4219502008-07-22 11:18:09 -0400512 struct rb_node *rb;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400513 int ret;
514
515 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400516 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400517 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400518
Chris Masonf9295742008-07-17 12:54:14 -0400519 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400520
521 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
522 BUG_ON(!ordered_extent);
523
524 lock_extent(io_tree, ordered_extent->file_offset,
525 ordered_extent->file_offset + ordered_extent->len - 1,
526 GFP_NOFS);
527
528 INIT_LIST_HEAD(&list);
529
530 ins.objectid = ordered_extent->start;
531 ins.offset = ordered_extent->len;
532 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400533
Chris Masone6dcd2d2008-07-17 12:53:50 -0400534 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
535 trans->transid, inode->i_ino,
536 ordered_extent->file_offset, &ins);
537 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400538
539 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400540
Chris Masonf4219502008-07-22 11:18:09 -0400541 spin_lock(&em_tree->lock);
542 clear_start = ordered_extent->file_offset;
543 clear_end = ordered_extent->file_offset + ordered_extent->len;
544 em = lookup_extent_mapping(em_tree, clear_start,
545 ordered_extent->len);
546 em_orig = em;
547 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
548 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
549 rb = rb_next(&em->rb_node);
550 if (!rb)
551 break;
552 em = rb_entry(rb, struct extent_map, rb_node);
553 }
554 free_extent_map(em_orig);
555 spin_unlock(&em_tree->lock);
556
Chris Masone6dcd2d2008-07-17 12:53:50 -0400557 ret = btrfs_drop_extents(trans, root, inode,
558 ordered_extent->file_offset,
559 ordered_extent->file_offset +
560 ordered_extent->len,
561 ordered_extent->file_offset, &alloc_hint);
562 BUG_ON(ret);
563 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
564 ordered_extent->file_offset,
565 ordered_extent->start,
566 ordered_extent->len,
567 ordered_extent->len, 0);
568 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400569
Chris Masone6dcd2d2008-07-17 12:53:50 -0400570 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
571 ordered_extent->file_offset +
572 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400573 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
574
Chris Masone6dcd2d2008-07-17 12:53:50 -0400575 inode->i_blocks += ordered_extent->len >> 9;
576 unlock_extent(io_tree, ordered_extent->file_offset,
577 ordered_extent->file_offset + ordered_extent->len - 1,
578 GFP_NOFS);
579 add_pending_csums(trans, inode, ordered_extent->file_offset,
580 &ordered_extent->list);
581
Chris Masondbe674a2008-07-17 12:54:05 -0400582 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400583 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400584
Chris Masone6dcd2d2008-07-17 12:53:50 -0400585 /* once for us */
586 btrfs_put_ordered_extent(ordered_extent);
587 /* once for the tree */
588 btrfs_put_ordered_extent(ordered_extent);
589
590 btrfs_update_inode(trans, root, inode);
591 btrfs_end_transaction(trans, root);
592 return 0;
593}
594
Chris Mason211f90e2008-07-18 11:56:15 -0400595int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
596 struct extent_state *state, int uptodate)
597{
598 return btrfs_finish_ordered_io(page->mapping->host, start, end);
599}
600
Chris Mason07157aa2007-08-30 08:50:51 -0400601int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
602{
603 int ret = 0;
604 struct inode *inode = page->mapping->host;
605 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500606 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400607 struct btrfs_csum_item *item;
608 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400609 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400610
Yanb98b6762008-01-08 15:54:37 -0500611 if (btrfs_test_opt(root, NODATASUM) ||
612 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500613 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400614
Chris Mason89642222008-07-24 09:41:53 -0400615 /*
616 * It is possible there is an ordered extent that has
617 * not yet finished for this range in the file. If so,
618 * that extent will have a csum cached, and it will insert
619 * the sum after all the blocks in the extent are fully
620 * on disk. So, look for an ordered extent and use the
621 * sum if found. We have to do this before looking in the
622 * btree because csum items are pre-inserted based on
623 * the file size. btrfs_lookup_csum might find an item
624 * that still hasn't been fully filled.
625 */
626 ret = btrfs_find_ordered_sum(inode, start, &csum);
627 if (ret == 0)
628 goto found;
629
630 ret = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400631 path = btrfs_alloc_path();
632 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
633 if (IS_ERR(item)) {
634 ret = PTR_ERR(item);
635 /* a csum that isn't present is a preallocated region. */
636 if (ret == -ENOENT || ret == -EFBIG)
637 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400638 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400639 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
640 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400641 goto out;
642 }
Chris Masonff79f812007-10-15 16:22:25 -0400643 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
644 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400645found:
Chris Masond1310b22008-01-24 16:13:08 -0500646 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400647out:
648 if (path)
649 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400650 return ret;
651}
652
Chris Mason7e383262008-04-09 16:28:12 -0400653struct io_failure_record {
654 struct page *page;
655 u64 start;
656 u64 len;
657 u64 logical;
658 int last_mirror;
659};
660
Chris Mason1259ab72008-05-12 13:39:03 -0400661int btrfs_io_failed_hook(struct bio *failed_bio,
662 struct page *page, u64 start, u64 end,
663 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400664{
665 struct io_failure_record *failrec = NULL;
666 u64 private;
667 struct extent_map *em;
668 struct inode *inode = page->mapping->host;
669 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400670 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400671 struct bio *bio;
672 int num_copies;
673 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400674 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400675 u64 logical;
676
677 ret = get_state_private(failure_tree, start, &private);
678 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400679 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
680 if (!failrec)
681 return -ENOMEM;
682 failrec->start = start;
683 failrec->len = end - start + 1;
684 failrec->last_mirror = 0;
685
Chris Mason3b951512008-04-17 11:29:12 -0400686 spin_lock(&em_tree->lock);
687 em = lookup_extent_mapping(em_tree, start, failrec->len);
688 if (em->start > start || em->start + em->len < start) {
689 free_extent_map(em);
690 em = NULL;
691 }
692 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400693
694 if (!em || IS_ERR(em)) {
695 kfree(failrec);
696 return -EIO;
697 }
698 logical = start - em->start;
699 logical = em->block_start + logical;
700 failrec->logical = logical;
701 free_extent_map(em);
702 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
703 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400704 set_state_private(failure_tree, start,
705 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400706 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400707 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400708 }
709 num_copies = btrfs_num_copies(
710 &BTRFS_I(inode)->root->fs_info->mapping_tree,
711 failrec->logical, failrec->len);
712 failrec->last_mirror++;
713 if (!state) {
714 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
715 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
716 failrec->start,
717 EXTENT_LOCKED);
718 if (state && state->start != failrec->start)
719 state = NULL;
720 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
721 }
722 if (!state || failrec->last_mirror > num_copies) {
723 set_state_private(failure_tree, failrec->start, 0);
724 clear_extent_bits(failure_tree, failrec->start,
725 failrec->start + failrec->len - 1,
726 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
727 kfree(failrec);
728 return -EIO;
729 }
730 bio = bio_alloc(GFP_NOFS, 1);
731 bio->bi_private = state;
732 bio->bi_end_io = failed_bio->bi_end_io;
733 bio->bi_sector = failrec->logical >> 9;
734 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400735 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400736 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400737 if (failed_bio->bi_rw & (1 << BIO_RW))
738 rw = WRITE;
739 else
740 rw = READ;
741
742 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
743 failrec->last_mirror);
744 return 0;
745}
746
747int btrfs_clean_io_failures(struct inode *inode, u64 start)
748{
749 u64 private;
750 u64 private_failure;
751 struct io_failure_record *failure;
752 int ret;
753
754 private = 0;
755 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
756 (u64)-1, 1, EXTENT_DIRTY)) {
757 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
758 start, &private_failure);
759 if (ret == 0) {
760 failure = (struct io_failure_record *)(unsigned long)
761 private_failure;
762 set_state_private(&BTRFS_I(inode)->io_failure_tree,
763 failure->start, 0);
764 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
765 failure->start,
766 failure->start + failure->len - 1,
767 EXTENT_DIRTY | EXTENT_LOCKED,
768 GFP_NOFS);
769 kfree(failure);
770 }
771 }
Chris Mason7e383262008-04-09 16:28:12 -0400772 return 0;
773}
774
Chris Mason70dec802008-01-29 09:59:12 -0500775int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
776 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400777{
Chris Mason35ebb932007-10-30 16:56:53 -0400778 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400779 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500780 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400781 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500782 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400783 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400784 struct btrfs_root *root = BTRFS_I(inode)->root;
785 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400786 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500787
Yanb98b6762008-01-08 15:54:37 -0500788 if (btrfs_test_opt(root, NODATASUM) ||
789 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500790 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500791 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500792 private = state->private;
793 ret = 0;
794 } else {
795 ret = get_state_private(io_tree, start, &private);
796 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400797 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400798 kaddr = kmap_atomic(page, KM_IRQ0);
799 if (ret) {
800 goto zeroit;
801 }
Chris Masonff79f812007-10-15 16:22:25 -0400802 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
803 btrfs_csum_final(csum, (char *)&csum);
804 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400805 goto zeroit;
806 }
807 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400808 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400809
810 /* if the io failure tree for this inode is non-empty,
811 * check to see if we've recovered from a failed IO
812 */
Chris Mason1259ab72008-05-12 13:39:03 -0400813 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400814 return 0;
815
816zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500817 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
818 page->mapping->host->i_ino, (unsigned long long)start, csum,
819 private);
Chris Masondb945352007-10-15 16:15:53 -0400820 memset(kaddr + offset, 1, end - start + 1);
821 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400822 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400823 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400824 if (private == 0)
825 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400826 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400827}
Chris Masonb888db22007-08-27 16:49:44 -0400828
Chris Mason39279cc2007-06-12 06:35:45 -0400829void btrfs_read_locked_inode(struct inode *inode)
830{
831 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400832 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400833 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400834 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400835 struct btrfs_root *root = BTRFS_I(inode)->root;
836 struct btrfs_key location;
837 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400838 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400839 int ret;
840
841 path = btrfs_alloc_path();
842 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400843 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500844
Chris Mason39279cc2007-06-12 06:35:45 -0400845 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400846 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400847 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400848
Chris Mason5f39d392007-10-15 16:14:19 -0400849 leaf = path->nodes[0];
850 inode_item = btrfs_item_ptr(leaf, path->slots[0],
851 struct btrfs_inode_item);
852
853 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
854 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
855 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
856 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400857 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400858
859 tspec = btrfs_inode_atime(inode_item);
860 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
861 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
862
863 tspec = btrfs_inode_mtime(inode_item);
864 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
865 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
866
867 tspec = btrfs_inode_ctime(inode_item);
868 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
869 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
870
871 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
872 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400873 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400874 rdev = btrfs_inode_rdev(leaf, inode_item);
875
Josef Bacikaec74772008-07-24 12:12:38 -0400876 BTRFS_I(inode)->index_cnt = (u64)-1;
877
Chris Mason5f39d392007-10-15 16:14:19 -0400878 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400879 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
880 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500881 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500882 if (!BTRFS_I(inode)->block_group) {
883 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400884 NULL, 0,
885 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500886 }
Chris Mason39279cc2007-06-12 06:35:45 -0400887 btrfs_free_path(path);
888 inode_item = NULL;
889
Chris Mason39279cc2007-06-12 06:35:45 -0400890 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400891 case S_IFREG:
892 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400893 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500894 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400895 inode->i_fop = &btrfs_file_operations;
896 inode->i_op = &btrfs_file_inode_operations;
897 break;
898 case S_IFDIR:
899 inode->i_fop = &btrfs_dir_file_operations;
900 if (root == root->fs_info->tree_root)
901 inode->i_op = &btrfs_dir_ro_inode_operations;
902 else
903 inode->i_op = &btrfs_dir_inode_operations;
904 break;
905 case S_IFLNK:
906 inode->i_op = &btrfs_symlink_inode_operations;
907 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400908 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400909 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400910 default:
911 init_special_inode(inode, inode->i_mode, rdev);
912 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400913 }
914 return;
915
916make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400917 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400918 make_bad_inode(inode);
919}
920
Chris Mason5f39d392007-10-15 16:14:19 -0400921static void fill_inode_item(struct extent_buffer *leaf,
922 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400923 struct inode *inode)
924{
Chris Mason5f39d392007-10-15 16:14:19 -0400925 btrfs_set_inode_uid(leaf, item, inode->i_uid);
926 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -0400927 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -0400928 btrfs_set_inode_mode(leaf, item, inode->i_mode);
929 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
930
931 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
932 inode->i_atime.tv_sec);
933 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
934 inode->i_atime.tv_nsec);
935
936 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
937 inode->i_mtime.tv_sec);
938 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
939 inode->i_mtime.tv_nsec);
940
941 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
942 inode->i_ctime.tv_sec);
943 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
944 inode->i_ctime.tv_nsec);
945
946 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
947 btrfs_set_inode_generation(leaf, item, inode->i_generation);
948 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500949 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400950 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400951 BTRFS_I(inode)->block_group->key.objectid);
952}
953
Chris Masonba1da2f2008-07-17 12:54:15 -0400954int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400955 struct btrfs_root *root,
956 struct inode *inode)
957{
958 struct btrfs_inode_item *inode_item;
959 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400960 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400961 int ret;
962
963 path = btrfs_alloc_path();
964 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400965 ret = btrfs_lookup_inode(trans, root, path,
966 &BTRFS_I(inode)->location, 1);
967 if (ret) {
968 if (ret > 0)
969 ret = -ENOENT;
970 goto failed;
971 }
972
Chris Mason5f39d392007-10-15 16:14:19 -0400973 leaf = path->nodes[0];
974 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400975 struct btrfs_inode_item);
976
Chris Mason5f39d392007-10-15 16:14:19 -0400977 fill_inode_item(leaf, inode_item, inode);
978 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400979 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400980 ret = 0;
981failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400982 btrfs_free_path(path);
983 return ret;
984}
985
986
987static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
989 struct inode *dir,
990 struct dentry *dentry)
991{
992 struct btrfs_path *path;
993 const char *name = dentry->d_name.name;
994 int name_len = dentry->d_name.len;
995 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400996 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400997 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400998 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -0400999 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001000
1001 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001002 if (!path) {
1003 ret = -ENOMEM;
1004 goto err;
1005 }
1006
Chris Mason39279cc2007-06-12 06:35:45 -04001007 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1008 name, name_len, -1);
1009 if (IS_ERR(di)) {
1010 ret = PTR_ERR(di);
1011 goto err;
1012 }
1013 if (!di) {
1014 ret = -ENOENT;
1015 goto err;
1016 }
Chris Mason5f39d392007-10-15 16:14:19 -04001017 leaf = path->nodes[0];
1018 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001019 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001020 if (ret)
1021 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001022 btrfs_release_path(root, path);
1023
Josef Bacikaec74772008-07-24 12:12:38 -04001024 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1025 dentry->d_inode->i_ino,
1026 dentry->d_parent->d_inode->i_ino, &index);
1027 if (ret) {
1028 printk("failed to delete reference to %.*s, "
1029 "inode %lu parent %lu\n", name_len, name,
1030 dentry->d_inode->i_ino,
1031 dentry->d_parent->d_inode->i_ino);
1032 goto err;
1033 }
1034
Chris Mason39279cc2007-06-12 06:35:45 -04001035 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001036 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001037 if (IS_ERR(di)) {
1038 ret = PTR_ERR(di);
1039 goto err;
1040 }
1041 if (!di) {
1042 ret = -ENOENT;
1043 goto err;
1044 }
1045 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001046 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001047
1048 dentry->d_inode->i_ctime = dir->i_ctime;
1049err:
1050 btrfs_free_path(path);
1051 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001052 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001053 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001054 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001055#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1056 dentry->d_inode->i_nlink--;
1057#else
Chris Mason39279cc2007-06-12 06:35:45 -04001058 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001059#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001060 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001061 dir->i_sb->s_dirt = 1;
1062 }
1063 return ret;
1064}
1065
1066static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1067{
1068 struct btrfs_root *root;
1069 struct btrfs_trans_handle *trans;
1070 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001071 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001072
1073 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001074
1075 ret = btrfs_check_free_space(root, 1, 1);
1076 if (ret)
1077 goto fail;
1078
Chris Mason39279cc2007-06-12 06:35:45 -04001079 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001080
Chris Mason39279cc2007-06-12 06:35:45 -04001081 btrfs_set_trans_block_group(trans, dir);
1082 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001083 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001084
Chris Mason89ce8a62008-06-25 16:01:31 -04001085 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001086fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001087 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001088 return ret;
1089}
1090
1091static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1092{
1093 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001094 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001095 int ret;
1096 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001097 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001098 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001099
Chris Mason925baed2008-06-25 16:01:30 -04001100 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001101 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001102 }
Yan134d4512007-10-25 15:49:25 -04001103
Chris Mason1832a6d2007-12-21 16:27:21 -05001104 ret = btrfs_check_free_space(root, 1, 1);
1105 if (ret)
1106 goto fail;
1107
Chris Mason39279cc2007-06-12 06:35:45 -04001108 trans = btrfs_start_transaction(root, 1);
1109 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001110
1111 /* now the directory is empty */
1112 err = btrfs_unlink_trans(trans, root, dir, dentry);
1113 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001114 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001115 }
Chris Mason39544012007-12-12 14:38:19 -05001116
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001117 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001118 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001119fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001120 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001121
Chris Mason39279cc2007-06-12 06:35:45 -04001122 if (ret && !err)
1123 err = ret;
1124 return err;
1125}
1126
Chris Mason39279cc2007-06-12 06:35:45 -04001127/*
Chris Mason39279cc2007-06-12 06:35:45 -04001128 * this can truncate away extent items, csum items and directory items.
1129 * It starts at a high offset and removes keys until it can't find
1130 * any higher than i_size.
1131 *
1132 * csum items that cross the new i_size are truncated to the new size
1133 * as well.
1134 */
1135static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1136 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001137 struct inode *inode,
1138 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001139{
1140 int ret;
1141 struct btrfs_path *path;
1142 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001143 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001144 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001145 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001146 struct btrfs_file_extent_item *fi;
1147 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001148 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001149 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001150 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001151 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001152 int found_extent;
1153 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001154 int pending_del_nr = 0;
1155 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001156 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001157 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001158
Chris Mason3b951512008-04-17 11:29:12 -04001159 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001160 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001161 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001162 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001163
Chris Mason39279cc2007-06-12 06:35:45 -04001164 /* FIXME, add redo link to tree so we don't leak on crash */
1165 key.objectid = inode->i_ino;
1166 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001167 key.type = (u8)-1;
1168
Chris Mason85e21ba2008-01-29 15:11:36 -05001169 btrfs_init_path(path);
1170search_again:
1171 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1172 if (ret < 0) {
1173 goto error;
1174 }
1175 if (ret > 0) {
1176 BUG_ON(path->slots[0] == 0);
1177 path->slots[0]--;
1178 }
1179
Chris Mason39279cc2007-06-12 06:35:45 -04001180 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001181 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001182 leaf = path->nodes[0];
1183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1184 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001185
Chris Mason5f39d392007-10-15 16:14:19 -04001186 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001187 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001188
Chris Mason85e21ba2008-01-29 15:11:36 -05001189 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001190 break;
1191
Chris Mason5f39d392007-10-15 16:14:19 -04001192 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001193 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001194 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001195 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001196 extent_type = btrfs_file_extent_type(leaf, fi);
1197 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001198 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001199 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001200 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1201 struct btrfs_item *item = btrfs_item_nr(leaf,
1202 path->slots[0]);
1203 item_end += btrfs_file_extent_inline_len(leaf,
1204 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001205 }
Yan008630c2007-11-07 13:31:09 -05001206 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001207 }
1208 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1209 ret = btrfs_csum_truncate(trans, root, path,
1210 inode->i_size);
1211 BUG_ON(ret);
1212 }
Yan008630c2007-11-07 13:31:09 -05001213 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001214 if (found_type == BTRFS_DIR_ITEM_KEY) {
1215 found_type = BTRFS_INODE_ITEM_KEY;
1216 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1217 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001218 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1219 found_type = BTRFS_XATTR_ITEM_KEY;
1220 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1221 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001222 } else if (found_type) {
1223 found_type--;
1224 } else {
1225 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001226 }
Yana61721d2007-09-17 11:08:38 -04001227 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001228 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001229 }
Chris Mason5f39d392007-10-15 16:14:19 -04001230 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001231 del_item = 1;
1232 else
1233 del_item = 0;
1234 found_extent = 0;
1235
1236 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001237 if (found_type != BTRFS_EXTENT_DATA_KEY)
1238 goto delete;
1239
1240 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001241 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001242 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001243 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001244 u64 orig_num_bytes =
1245 btrfs_file_extent_num_bytes(leaf, fi);
1246 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001247 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001248 extent_num_bytes = extent_num_bytes &
1249 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001250 btrfs_set_file_extent_num_bytes(leaf, fi,
1251 extent_num_bytes);
1252 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001253 extent_num_bytes);
1254 if (extent_start != 0)
1255 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001256 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001257 } else {
Chris Masondb945352007-10-15 16:15:53 -04001258 extent_num_bytes =
1259 btrfs_file_extent_disk_num_bytes(leaf,
1260 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001261 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001262 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001263 if (extent_start != 0) {
1264 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001265 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001266 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001267 root_gen = btrfs_header_generation(leaf);
1268 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001269 }
Chris Mason90692182008-02-08 13:49:28 -05001270 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1271 if (!del_item) {
1272 u32 newsize = inode->i_size - found_key.offset;
1273 dec_i_blocks(inode, item_end + 1 -
1274 found_key.offset - newsize);
1275 newsize =
1276 btrfs_file_extent_calc_inline_size(newsize);
1277 ret = btrfs_truncate_item(trans, root, path,
1278 newsize, 1);
1279 BUG_ON(ret);
1280 } else {
1281 dec_i_blocks(inode, item_end + 1 -
1282 found_key.offset);
1283 }
Chris Mason39279cc2007-06-12 06:35:45 -04001284 }
Chris Mason179e29e2007-11-01 11:28:41 -04001285delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001286 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001287 if (!pending_del_nr) {
1288 /* no pending yet, add ourselves */
1289 pending_del_slot = path->slots[0];
1290 pending_del_nr = 1;
1291 } else if (pending_del_nr &&
1292 path->slots[0] + 1 == pending_del_slot) {
1293 /* hop on the pending chunk */
1294 pending_del_nr++;
1295 pending_del_slot = path->slots[0];
1296 } else {
1297 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1298 }
Chris Mason39279cc2007-06-12 06:35:45 -04001299 } else {
1300 break;
1301 }
Chris Mason39279cc2007-06-12 06:35:45 -04001302 if (found_extent) {
1303 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001304 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001305 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001306 root_gen, inode->i_ino,
1307 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001308 BUG_ON(ret);
1309 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001310next:
1311 if (path->slots[0] == 0) {
1312 if (pending_del_nr)
1313 goto del_pending;
1314 btrfs_release_path(root, path);
1315 goto search_again;
1316 }
1317
1318 path->slots[0]--;
1319 if (pending_del_nr &&
1320 path->slots[0] + 1 != pending_del_slot) {
1321 struct btrfs_key debug;
1322del_pending:
1323 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1324 pending_del_slot);
1325 ret = btrfs_del_items(trans, root, path,
1326 pending_del_slot,
1327 pending_del_nr);
1328 BUG_ON(ret);
1329 pending_del_nr = 0;
1330 btrfs_release_path(root, path);
1331 goto search_again;
1332 }
Chris Mason39279cc2007-06-12 06:35:45 -04001333 }
1334 ret = 0;
1335error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001336 if (pending_del_nr) {
1337 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1338 pending_del_nr);
1339 }
Chris Mason39279cc2007-06-12 06:35:45 -04001340 btrfs_free_path(path);
1341 inode->i_sb->s_dirt = 1;
1342 return ret;
1343}
1344
Chris Masona52d9a82007-08-27 16:49:44 -04001345/*
1346 * taken from block_truncate_page, but does cow as it zeros out
1347 * any bytes left in the last page in the file.
1348 */
1349static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1350{
1351 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001352 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001353 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1354 struct btrfs_ordered_extent *ordered;
1355 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001356 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001357 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1358 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1359 struct page *page;
1360 int ret = 0;
1361 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001362 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001363
1364 if ((offset & (blocksize - 1)) == 0)
1365 goto out;
1366
1367 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001368again:
Chris Masona52d9a82007-08-27 16:49:44 -04001369 page = grab_cache_page(mapping, index);
1370 if (!page)
1371 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001372
1373 page_start = page_offset(page);
1374 page_end = page_start + PAGE_CACHE_SIZE - 1;
1375
Chris Masona52d9a82007-08-27 16:49:44 -04001376 if (!PageUptodate(page)) {
1377 ret = btrfs_readpage(NULL, page);
1378 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001379 if (page->mapping != mapping) {
1380 unlock_page(page);
1381 page_cache_release(page);
1382 goto again;
1383 }
Chris Masona52d9a82007-08-27 16:49:44 -04001384 if (!PageUptodate(page)) {
1385 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001386 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001387 }
1388 }
Chris Mason211c17f2008-05-15 09:13:45 -04001389 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001390
1391 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1392 set_page_extent_mapped(page);
1393
1394 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1395 if (ordered) {
1396 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1397 unlock_page(page);
1398 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001399 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001400 btrfs_put_ordered_extent(ordered);
1401 goto again;
1402 }
1403
1404 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1405 page_end, GFP_NOFS);
1406 ret = 0;
1407 if (offset != PAGE_CACHE_SIZE) {
1408 kaddr = kmap(page);
1409 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1410 flush_dcache_page(page);
1411 kunmap(page);
1412 }
Chris Mason247e7432008-07-17 12:53:51 -04001413 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001414 set_page_dirty(page);
1415 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001416
Chris Mason89642222008-07-24 09:41:53 -04001417out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001418 unlock_page(page);
1419 page_cache_release(page);
1420out:
1421 return ret;
1422}
1423
1424static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1425{
1426 struct inode *inode = dentry->d_inode;
1427 int err;
1428
1429 err = inode_change_ok(inode, attr);
1430 if (err)
1431 return err;
1432
1433 if (S_ISREG(inode->i_mode) &&
1434 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1435 struct btrfs_trans_handle *trans;
1436 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001437 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001438
Chris Mason5f39d392007-10-15 16:14:19 -04001439 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001440 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001441 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001442 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001443 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001444
Chris Mason1b0f7c22008-01-30 14:33:02 -05001445 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001446 goto out;
1447
Chris Mason1832a6d2007-12-21 16:27:21 -05001448 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001449 if (err)
1450 goto fail;
1451
Chris Mason39279cc2007-06-12 06:35:45 -04001452 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1453
Chris Mason5f564062008-01-22 16:47:59 -05001454 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001455 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1456 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001457
Chris Mason39279cc2007-06-12 06:35:45 -04001458 trans = btrfs_start_transaction(root, 1);
1459 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001460 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001461 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001462 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001463 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001464
Chris Mason179e29e2007-11-01 11:28:41 -04001465 if (alloc_hint != EXTENT_MAP_INLINE) {
1466 err = btrfs_insert_file_extent(trans, root,
1467 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001468 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001469 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001470 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001471 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001472 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001473 }
Chris Masonee6e6502008-07-17 12:54:40 -04001474 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001475 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001476 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001477 if (err)
1478 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001479 }
1480out:
1481 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001482
1483 if (!err && ((attr->ia_valid & ATTR_MODE)))
1484 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001485fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001486 return err;
1487}
Chris Mason61295eb2008-01-14 16:24:38 -05001488
Chris Mason39279cc2007-06-12 06:35:45 -04001489void btrfs_delete_inode(struct inode *inode)
1490{
1491 struct btrfs_trans_handle *trans;
1492 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001493 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001494 int ret;
1495
1496 truncate_inode_pages(&inode->i_data, 0);
1497 if (is_bad_inode(inode)) {
1498 goto no_delete;
1499 }
Chris Mason4a096752008-07-21 10:29:44 -04001500 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001501
Chris Masondbe674a2008-07-17 12:54:05 -04001502 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001503 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001504
Chris Mason39279cc2007-06-12 06:35:45 -04001505 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001506 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001507 if (ret)
1508 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001509
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001510 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001511 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001512
Chris Mason39279cc2007-06-12 06:35:45 -04001513 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001514 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001515 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001516
1517no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001518 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001519 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001520 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001521no_delete:
1522 clear_inode(inode);
1523}
1524
1525/*
1526 * this returns the key found in the dir entry in the location pointer.
1527 * If no dir entries were found, location->objectid is 0.
1528 */
1529static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1530 struct btrfs_key *location)
1531{
1532 const char *name = dentry->d_name.name;
1533 int namelen = dentry->d_name.len;
1534 struct btrfs_dir_item *di;
1535 struct btrfs_path *path;
1536 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001537 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001538
Chris Mason39544012007-12-12 14:38:19 -05001539 if (namelen == 1 && strcmp(name, ".") == 0) {
1540 location->objectid = dir->i_ino;
1541 location->type = BTRFS_INODE_ITEM_KEY;
1542 location->offset = 0;
1543 return 0;
1544 }
Chris Mason39279cc2007-06-12 06:35:45 -04001545 path = btrfs_alloc_path();
1546 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001547
Chris Mason7a720532007-12-13 09:06:59 -05001548 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001549 struct btrfs_key key;
1550 struct extent_buffer *leaf;
1551 u32 nritems;
1552 int slot;
1553
1554 key.objectid = dir->i_ino;
1555 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1556 key.offset = 0;
1557 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1558 BUG_ON(ret == 0);
1559 ret = 0;
1560
1561 leaf = path->nodes[0];
1562 slot = path->slots[0];
1563 nritems = btrfs_header_nritems(leaf);
1564 if (slot >= nritems)
1565 goto out_err;
1566
1567 btrfs_item_key_to_cpu(leaf, &key, slot);
1568 if (key.objectid != dir->i_ino ||
1569 key.type != BTRFS_INODE_REF_KEY) {
1570 goto out_err;
1571 }
1572 location->objectid = key.offset;
1573 location->type = BTRFS_INODE_ITEM_KEY;
1574 location->offset = 0;
1575 goto out;
1576 }
1577
Chris Mason39279cc2007-06-12 06:35:45 -04001578 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1579 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001580 if (IS_ERR(di))
1581 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001582 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001583 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001584 }
Chris Mason5f39d392007-10-15 16:14:19 -04001585 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001586out:
Chris Mason39279cc2007-06-12 06:35:45 -04001587 btrfs_free_path(path);
1588 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001589out_err:
1590 location->objectid = 0;
1591 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001592}
1593
1594/*
1595 * when we hit a tree root in a directory, the btrfs part of the inode
1596 * needs to be changed to reflect the root directory of the tree root. This
1597 * is kind of like crossing a mount point.
1598 */
1599static int fixup_tree_root_location(struct btrfs_root *root,
1600 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001601 struct btrfs_root **sub_root,
1602 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001603{
1604 struct btrfs_path *path;
1605 struct btrfs_root_item *ri;
1606
1607 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1608 return 0;
1609 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1610 return 0;
1611
1612 path = btrfs_alloc_path();
1613 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001614
Josef Bacik58176a92007-08-29 15:47:34 -04001615 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1616 dentry->d_name.name,
1617 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001618 if (IS_ERR(*sub_root))
1619 return PTR_ERR(*sub_root);
1620
1621 ri = &(*sub_root)->root_item;
1622 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001623 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1624 location->offset = 0;
1625
1626 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001627 return 0;
1628}
1629
1630static int btrfs_init_locked_inode(struct inode *inode, void *p)
1631{
1632 struct btrfs_iget_args *args = p;
1633 inode->i_ino = args->ino;
1634 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001635 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001636 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001637 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001638 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1639 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001640 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001641 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1642 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001643 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001644 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001645 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001646 return 0;
1647}
1648
1649static int btrfs_find_actor(struct inode *inode, void *opaque)
1650{
1651 struct btrfs_iget_args *args = opaque;
1652 return (args->ino == inode->i_ino &&
1653 args->root == BTRFS_I(inode)->root);
1654}
1655
Chris Masondc17ff82008-01-08 15:46:30 -05001656struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1657 u64 root_objectid)
1658{
1659 struct btrfs_iget_args args;
1660 args.ino = objectid;
1661 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1662
1663 if (!args.root)
1664 return NULL;
1665
1666 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1667}
1668
Chris Mason39279cc2007-06-12 06:35:45 -04001669struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1670 struct btrfs_root *root)
1671{
1672 struct inode *inode;
1673 struct btrfs_iget_args args;
1674 args.ino = objectid;
1675 args.root = root;
1676
1677 inode = iget5_locked(s, objectid, btrfs_find_actor,
1678 btrfs_init_locked_inode,
1679 (void *)&args);
1680 return inode;
1681}
1682
1683static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1684 struct nameidata *nd)
1685{
1686 struct inode * inode;
1687 struct btrfs_inode *bi = BTRFS_I(dir);
1688 struct btrfs_root *root = bi->root;
1689 struct btrfs_root *sub_root = root;
1690 struct btrfs_key location;
1691 int ret;
1692
1693 if (dentry->d_name.len > BTRFS_NAME_LEN)
1694 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001695
Chris Mason39279cc2007-06-12 06:35:45 -04001696 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001697
Chris Mason39279cc2007-06-12 06:35:45 -04001698 if (ret < 0)
1699 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001700
Chris Mason39279cc2007-06-12 06:35:45 -04001701 inode = NULL;
1702 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001703 ret = fixup_tree_root_location(root, &location, &sub_root,
1704 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001705 if (ret < 0)
1706 return ERR_PTR(ret);
1707 if (ret > 0)
1708 return ERR_PTR(-ENOENT);
1709 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1710 sub_root);
1711 if (!inode)
1712 return ERR_PTR(-EACCES);
1713 if (inode->i_state & I_NEW) {
1714 /* the inode and parent dir are two different roots */
1715 if (sub_root != root) {
1716 igrab(inode);
1717 sub_root->inode = inode;
1718 }
1719 BTRFS_I(inode)->root = sub_root;
1720 memcpy(&BTRFS_I(inode)->location, &location,
1721 sizeof(location));
1722 btrfs_read_locked_inode(inode);
1723 unlock_new_inode(inode);
1724 }
1725 }
1726 return d_splice_alias(inode, dentry);
1727}
1728
Chris Mason39279cc2007-06-12 06:35:45 -04001729static unsigned char btrfs_filetype_table[] = {
1730 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1731};
1732
1733static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1734{
Chris Mason6da6aba2007-12-18 16:15:09 -05001735 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001736 struct btrfs_root *root = BTRFS_I(inode)->root;
1737 struct btrfs_item *item;
1738 struct btrfs_dir_item *di;
1739 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001740 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001741 struct btrfs_path *path;
1742 int ret;
1743 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001744 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001745 int slot;
1746 int advance;
1747 unsigned char d_type;
1748 int over = 0;
1749 u32 di_cur;
1750 u32 di_total;
1751 u32 di_len;
1752 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001753 char tmp_name[32];
1754 char *name_ptr;
1755 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001756
1757 /* FIXME, use a real flag for deciding about the key type */
1758 if (root->fs_info->tree_root == root)
1759 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001760
Chris Mason39544012007-12-12 14:38:19 -05001761 /* special case for "." */
1762 if (filp->f_pos == 0) {
1763 over = filldir(dirent, ".", 1,
1764 1, inode->i_ino,
1765 DT_DIR);
1766 if (over)
1767 return 0;
1768 filp->f_pos = 1;
1769 }
1770
Chris Mason39279cc2007-06-12 06:35:45 -04001771 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001772 path = btrfs_alloc_path();
1773 path->reada = 2;
1774
1775 /* special case for .., just use the back ref */
1776 if (filp->f_pos == 1) {
1777 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1778 key.offset = 0;
1779 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1780 BUG_ON(ret == 0);
1781 leaf = path->nodes[0];
1782 slot = path->slots[0];
1783 nritems = btrfs_header_nritems(leaf);
1784 if (slot >= nritems) {
1785 btrfs_release_path(root, path);
1786 goto read_dir_items;
1787 }
1788 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1789 btrfs_release_path(root, path);
1790 if (found_key.objectid != key.objectid ||
1791 found_key.type != BTRFS_INODE_REF_KEY)
1792 goto read_dir_items;
1793 over = filldir(dirent, "..", 2,
1794 2, found_key.offset, DT_DIR);
1795 if (over)
1796 goto nopos;
1797 filp->f_pos = 2;
1798 }
1799
1800read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001801 btrfs_set_key_type(&key, key_type);
1802 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001803
Chris Mason39279cc2007-06-12 06:35:45 -04001804 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1805 if (ret < 0)
1806 goto err;
1807 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001808 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001809 leaf = path->nodes[0];
1810 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001811 slot = path->slots[0];
1812 if (advance || slot >= nritems) {
1813 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001814 ret = btrfs_next_leaf(root, path);
1815 if (ret)
1816 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001817 leaf = path->nodes[0];
1818 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001819 slot = path->slots[0];
1820 } else {
1821 slot++;
1822 path->slots[0]++;
1823 }
1824 }
1825 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001826 item = btrfs_item_nr(leaf, slot);
1827 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1828
1829 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001830 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001831 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001832 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001833 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001834 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001835
1836 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001837 advance = 1;
1838 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1839 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001840 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001841 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001842 struct btrfs_key location;
1843
1844 name_len = btrfs_dir_name_len(leaf, di);
1845 if (name_len < 32) {
1846 name_ptr = tmp_name;
1847 } else {
1848 name_ptr = kmalloc(name_len, GFP_NOFS);
1849 BUG_ON(!name_ptr);
1850 }
1851 read_extent_buffer(leaf, name_ptr,
1852 (unsigned long)(di + 1), name_len);
1853
1854 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1855 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001856 over = filldir(dirent, name_ptr, name_len,
1857 found_key.offset,
1858 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001859 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001860
1861 if (name_ptr != tmp_name)
1862 kfree(name_ptr);
1863
Chris Mason39279cc2007-06-12 06:35:45 -04001864 if (over)
1865 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001866 di_len = btrfs_dir_name_len(leaf, di) +
1867 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001868 di_cur += di_len;
1869 di = (struct btrfs_dir_item *)((char *)di + di_len);
1870 }
1871 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001872 if (key_type == BTRFS_DIR_INDEX_KEY)
1873 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1874 else
1875 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001876nopos:
1877 ret = 0;
1878err:
Chris Mason39279cc2007-06-12 06:35:45 -04001879 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001880 return ret;
1881}
1882
1883int btrfs_write_inode(struct inode *inode, int wait)
1884{
1885 struct btrfs_root *root = BTRFS_I(inode)->root;
1886 struct btrfs_trans_handle *trans;
1887 int ret = 0;
1888
1889 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04001890 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001891 btrfs_set_trans_block_group(trans, inode);
1892 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001893 }
1894 return ret;
1895}
1896
1897/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001898 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001899 * inode changes. But, it is most likely to find the inode in cache.
1900 * FIXME, needs more benchmarking...there are no reasons other than performance
1901 * to keep or drop this code.
1902 */
1903void btrfs_dirty_inode(struct inode *inode)
1904{
1905 struct btrfs_root *root = BTRFS_I(inode)->root;
1906 struct btrfs_trans_handle *trans;
1907
Chris Masonf9295742008-07-17 12:54:14 -04001908 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001909 btrfs_set_trans_block_group(trans, inode);
1910 btrfs_update_inode(trans, root, inode);
1911 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001912}
1913
Josef Bacikaec74772008-07-24 12:12:38 -04001914static int btrfs_set_inode_index_count(struct inode *inode)
1915{
1916 struct btrfs_root *root = BTRFS_I(inode)->root;
1917 struct btrfs_key key, found_key;
1918 struct btrfs_path *path;
1919 struct extent_buffer *leaf;
1920 int ret;
1921
1922 key.objectid = inode->i_ino;
1923 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
1924 key.offset = (u64)-1;
1925
1926 path = btrfs_alloc_path();
1927 if (!path)
1928 return -ENOMEM;
1929
1930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1931 if (ret < 0)
1932 goto out;
1933 /* FIXME: we should be able to handle this */
1934 if (ret == 0)
1935 goto out;
1936 ret = 0;
1937
1938 /*
1939 * MAGIC NUMBER EXPLANATION:
1940 * since we search a directory based on f_pos we have to start at 2
1941 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
1942 * else has to start at 2
1943 */
1944 if (path->slots[0] == 0) {
1945 BTRFS_I(inode)->index_cnt = 2;
1946 goto out;
1947 }
1948
1949 path->slots[0]--;
1950
1951 leaf = path->nodes[0];
1952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1953
1954 if (found_key.objectid != inode->i_ino ||
1955 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
1956 BTRFS_I(inode)->index_cnt = 2;
1957 goto out;
1958 }
1959
1960 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
1961out:
1962 btrfs_free_path(path);
1963 return ret;
1964}
1965
1966static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
1967{
1968 int ret = 0;
1969
1970 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
1971 ret = btrfs_set_inode_index_count(dir);
1972 if (ret)
1973 return ret;
1974 }
1975
1976 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
1977 BTRFS_I(dir)->index_cnt++;
1978
1979 return ret;
1980}
1981
Chris Mason39279cc2007-06-12 06:35:45 -04001982static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1983 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04001984 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05001985 const char *name, int name_len,
1986 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001987 u64 objectid,
1988 struct btrfs_block_group_cache *group,
1989 int mode)
1990{
1991 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001992 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001993 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001994 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001995 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001996 struct btrfs_inode_ref *ref;
1997 struct btrfs_key key[2];
1998 u32 sizes[2];
1999 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002000 int ret;
2001 int owner;
2002
Chris Mason5f39d392007-10-15 16:14:19 -04002003 path = btrfs_alloc_path();
2004 BUG_ON(!path);
2005
Chris Mason39279cc2007-06-12 06:35:45 -04002006 inode = new_inode(root->fs_info->sb);
2007 if (!inode)
2008 return ERR_PTR(-ENOMEM);
2009
Josef Bacikaec74772008-07-24 12:12:38 -04002010 if (dir) {
2011 ret = btrfs_set_inode_index(dir, inode);
2012 if (ret)
2013 return ERR_PTR(ret);
2014 } else {
2015 BTRFS_I(inode)->index = 0;
2016 }
2017 /*
2018 * index_cnt is ignored for everything but a dir,
2019 * btrfs_get_inode_index_count has an explanation for the magic
2020 * number
2021 */
2022 BTRFS_I(inode)->index_cnt = 2;
2023
Chris Masond1310b22008-01-24 16:13:08 -05002024 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2025 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002026 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002027 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2028 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002029 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002030 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002031 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002032 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002033 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002034 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002035
Chris Mason39279cc2007-06-12 06:35:45 -04002036 if (mode & S_IFDIR)
2037 owner = 0;
2038 else
2039 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002040 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002041 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002042 if (!new_inode_group) {
2043 printk("find_block group failed\n");
2044 new_inode_group = group;
2045 }
2046 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002047 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002048
2049 key[0].objectid = objectid;
2050 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2051 key[0].offset = 0;
2052
2053 key[1].objectid = objectid;
2054 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2055 key[1].offset = ref_objectid;
2056
2057 sizes[0] = sizeof(struct btrfs_inode_item);
2058 sizes[1] = name_len + sizeof(*ref);
2059
2060 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2061 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002062 goto fail;
2063
Chris Mason9c583092008-01-29 15:15:18 -05002064 if (objectid > root->highest_inode)
2065 root->highest_inode = objectid;
2066
Chris Mason39279cc2007-06-12 06:35:45 -04002067 inode->i_uid = current->fsuid;
2068 inode->i_gid = current->fsgid;
2069 inode->i_mode = mode;
2070 inode->i_ino = objectid;
2071 inode->i_blocks = 0;
2072 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002073 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2074 struct btrfs_inode_item);
2075 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002076
2077 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2078 struct btrfs_inode_ref);
2079 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002080 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002081 ptr = (unsigned long)(ref + 1);
2082 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2083
Chris Mason5f39d392007-10-15 16:14:19 -04002084 btrfs_mark_buffer_dirty(path->nodes[0]);
2085 btrfs_free_path(path);
2086
Chris Mason39279cc2007-06-12 06:35:45 -04002087 location = &BTRFS_I(inode)->location;
2088 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002089 location->offset = 0;
2090 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2091
Chris Mason39279cc2007-06-12 06:35:45 -04002092 insert_inode_hash(inode);
2093 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002094fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002095 if (dir)
2096 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_free_path(path);
2098 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002099}
2100
2101static inline u8 btrfs_inode_type(struct inode *inode)
2102{
2103 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2104}
2105
2106static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002107 struct dentry *dentry, struct inode *inode,
2108 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002109{
2110 int ret;
2111 struct btrfs_key key;
2112 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002113 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002114
Chris Mason39279cc2007-06-12 06:35:45 -04002115 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002116 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2117 key.offset = 0;
2118
2119 ret = btrfs_insert_dir_item(trans, root,
2120 dentry->d_name.name, dentry->d_name.len,
2121 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002122 &key, btrfs_inode_type(inode),
2123 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002124 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002125 if (add_backref) {
2126 ret = btrfs_insert_inode_ref(trans, root,
2127 dentry->d_name.name,
2128 dentry->d_name.len,
2129 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002130 parent_inode->i_ino,
2131 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002132 }
Chris Masondbe674a2008-07-17 12:54:05 -04002133 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2134 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002135 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002136 ret = btrfs_update_inode(trans, root,
2137 dentry->d_parent->d_inode);
2138 }
2139 return ret;
2140}
2141
2142static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002143 struct dentry *dentry, struct inode *inode,
2144 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002145{
Chris Mason9c583092008-01-29 15:15:18 -05002146 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002147 if (!err) {
2148 d_instantiate(dentry, inode);
2149 return 0;
2150 }
2151 if (err > 0)
2152 err = -EEXIST;
2153 return err;
2154}
2155
Josef Bacik618e21d2007-07-11 10:18:17 -04002156static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2157 int mode, dev_t rdev)
2158{
2159 struct btrfs_trans_handle *trans;
2160 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002161 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002162 int err;
2163 int drop_inode = 0;
2164 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002165 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002166
2167 if (!new_valid_dev(rdev))
2168 return -EINVAL;
2169
Chris Mason1832a6d2007-12-21 16:27:21 -05002170 err = btrfs_check_free_space(root, 1, 0);
2171 if (err)
2172 goto fail;
2173
Josef Bacik618e21d2007-07-11 10:18:17 -04002174 trans = btrfs_start_transaction(root, 1);
2175 btrfs_set_trans_block_group(trans, dir);
2176
2177 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2178 if (err) {
2179 err = -ENOSPC;
2180 goto out_unlock;
2181 }
2182
Josef Bacikaec74772008-07-24 12:12:38 -04002183 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002184 dentry->d_name.len,
2185 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002186 BTRFS_I(dir)->block_group, mode);
2187 err = PTR_ERR(inode);
2188 if (IS_ERR(inode))
2189 goto out_unlock;
2190
Josef Bacik33268ea2008-07-24 12:16:36 -04002191 err = btrfs_init_acl(inode, dir);
2192 if (err) {
2193 drop_inode = 1;
2194 goto out_unlock;
2195 }
2196
Josef Bacik618e21d2007-07-11 10:18:17 -04002197 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002198 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002199 if (err)
2200 drop_inode = 1;
2201 else {
2202 inode->i_op = &btrfs_special_inode_operations;
2203 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002204 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002205 }
2206 dir->i_sb->s_dirt = 1;
2207 btrfs_update_inode_block_group(trans, inode);
2208 btrfs_update_inode_block_group(trans, dir);
2209out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002210 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002211 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002212fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002213 if (drop_inode) {
2214 inode_dec_link_count(inode);
2215 iput(inode);
2216 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002217 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002218 return err;
2219}
2220
Chris Mason39279cc2007-06-12 06:35:45 -04002221static int btrfs_create(struct inode *dir, struct dentry *dentry,
2222 int mode, struct nameidata *nd)
2223{
2224 struct btrfs_trans_handle *trans;
2225 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002226 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002227 int err;
2228 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002229 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002230 u64 objectid;
2231
Chris Mason1832a6d2007-12-21 16:27:21 -05002232 err = btrfs_check_free_space(root, 1, 0);
2233 if (err)
2234 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002235 trans = btrfs_start_transaction(root, 1);
2236 btrfs_set_trans_block_group(trans, dir);
2237
2238 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2239 if (err) {
2240 err = -ENOSPC;
2241 goto out_unlock;
2242 }
2243
Josef Bacikaec74772008-07-24 12:12:38 -04002244 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002245 dentry->d_name.len,
2246 dentry->d_parent->d_inode->i_ino,
2247 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002248 err = PTR_ERR(inode);
2249 if (IS_ERR(inode))
2250 goto out_unlock;
2251
Josef Bacik33268ea2008-07-24 12:16:36 -04002252 err = btrfs_init_acl(inode, dir);
2253 if (err) {
2254 drop_inode = 1;
2255 goto out_unlock;
2256 }
2257
Chris Mason39279cc2007-06-12 06:35:45 -04002258 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002259 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002260 if (err)
2261 drop_inode = 1;
2262 else {
2263 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002264 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002265 inode->i_fop = &btrfs_file_operations;
2266 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002267 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2268 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002269 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002270 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2271 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002272 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002273 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002274 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002275 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002276 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002277 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002278 }
2279 dir->i_sb->s_dirt = 1;
2280 btrfs_update_inode_block_group(trans, inode);
2281 btrfs_update_inode_block_group(trans, dir);
2282out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002283 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002284 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002285fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002286 if (drop_inode) {
2287 inode_dec_link_count(inode);
2288 iput(inode);
2289 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002290 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002291 return err;
2292}
2293
2294static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2295 struct dentry *dentry)
2296{
2297 struct btrfs_trans_handle *trans;
2298 struct btrfs_root *root = BTRFS_I(dir)->root;
2299 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002300 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002301 int err;
2302 int drop_inode = 0;
2303
2304 if (inode->i_nlink == 0)
2305 return -ENOENT;
2306
Chris Mason6da6aba2007-12-18 16:15:09 -05002307#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2308 inode->i_nlink++;
2309#else
Chris Mason39279cc2007-06-12 06:35:45 -04002310 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002311#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002312 err = btrfs_check_free_space(root, 1, 0);
2313 if (err)
2314 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002315 err = btrfs_set_inode_index(dir, inode);
2316 if (err)
2317 goto fail;
2318
Chris Mason39279cc2007-06-12 06:35:45 -04002319 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002320
Chris Mason39279cc2007-06-12 06:35:45 -04002321 btrfs_set_trans_block_group(trans, dir);
2322 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002323
Chris Mason9c583092008-01-29 15:15:18 -05002324 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002325
Chris Mason39279cc2007-06-12 06:35:45 -04002326 if (err)
2327 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002328
Chris Mason39279cc2007-06-12 06:35:45 -04002329 dir->i_sb->s_dirt = 1;
2330 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002331 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002332
Chris Mason54aa1f42007-06-22 14:16:25 -04002333 if (err)
2334 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002335
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002336 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002337 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002338fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002339 if (drop_inode) {
2340 inode_dec_link_count(inode);
2341 iput(inode);
2342 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002343 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002344 return err;
2345}
2346
Chris Mason39279cc2007-06-12 06:35:45 -04002347static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2348{
Chris Masonb9d86662008-05-02 16:13:49 -04002349 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002350 struct btrfs_trans_handle *trans;
2351 struct btrfs_root *root = BTRFS_I(dir)->root;
2352 int err = 0;
2353 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002354 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002355 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002356
Chris Mason1832a6d2007-12-21 16:27:21 -05002357 err = btrfs_check_free_space(root, 1, 0);
2358 if (err)
2359 goto out_unlock;
2360
Chris Mason39279cc2007-06-12 06:35:45 -04002361 trans = btrfs_start_transaction(root, 1);
2362 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002363
Chris Mason39279cc2007-06-12 06:35:45 -04002364 if (IS_ERR(trans)) {
2365 err = PTR_ERR(trans);
2366 goto out_unlock;
2367 }
2368
2369 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2370 if (err) {
2371 err = -ENOSPC;
2372 goto out_unlock;
2373 }
2374
Josef Bacikaec74772008-07-24 12:12:38 -04002375 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002376 dentry->d_name.len,
2377 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002378 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2379 if (IS_ERR(inode)) {
2380 err = PTR_ERR(inode);
2381 goto out_fail;
2382 }
Chris Mason5f39d392007-10-15 16:14:19 -04002383
Chris Mason39279cc2007-06-12 06:35:45 -04002384 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002385
2386 err = btrfs_init_acl(inode, dir);
2387 if (err)
2388 goto out_fail;
2389
Chris Mason39279cc2007-06-12 06:35:45 -04002390 inode->i_op = &btrfs_dir_inode_operations;
2391 inode->i_fop = &btrfs_dir_file_operations;
2392 btrfs_set_trans_block_group(trans, inode);
2393
Chris Masondbe674a2008-07-17 12:54:05 -04002394 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002395 err = btrfs_update_inode(trans, root, inode);
2396 if (err)
2397 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002398
Chris Mason9c583092008-01-29 15:15:18 -05002399 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002400 if (err)
2401 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002402
Chris Mason39279cc2007-06-12 06:35:45 -04002403 d_instantiate(dentry, inode);
2404 drop_on_err = 0;
2405 dir->i_sb->s_dirt = 1;
2406 btrfs_update_inode_block_group(trans, inode);
2407 btrfs_update_inode_block_group(trans, dir);
2408
2409out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002410 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002411 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002412
Chris Mason39279cc2007-06-12 06:35:45 -04002413out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002414 if (drop_on_err)
2415 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002416 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002417 return err;
2418}
2419
Chris Mason3b951512008-04-17 11:29:12 -04002420static int merge_extent_mapping(struct extent_map_tree *em_tree,
2421 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002422 struct extent_map *em,
2423 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002424{
2425 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002426
Chris Masone6dcd2d2008-07-17 12:53:50 -04002427 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2428 start_diff = map_start - em->start;
2429 em->start = map_start;
2430 em->len = map_len;
2431 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2432 em->block_start += start_diff;
2433 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002434}
2435
Chris Masona52d9a82007-08-27 16:49:44 -04002436struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002437 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002438 int create)
2439{
2440 int ret;
2441 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002442 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002443 u64 extent_start = 0;
2444 u64 extent_end = 0;
2445 u64 objectid = inode->i_ino;
2446 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002447 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002448 struct btrfs_root *root = BTRFS_I(inode)->root;
2449 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002450 struct extent_buffer *leaf;
2451 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002452 struct extent_map *em = NULL;
2453 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002454 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002455 struct btrfs_trans_handle *trans = NULL;
2456
Chris Masona52d9a82007-08-27 16:49:44 -04002457again:
Chris Masond1310b22008-01-24 16:13:08 -05002458 spin_lock(&em_tree->lock);
2459 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002460 if (em)
2461 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002462 spin_unlock(&em_tree->lock);
2463
Chris Masona52d9a82007-08-27 16:49:44 -04002464 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002465 if (em->start > start || em->start + em->len <= start)
2466 free_extent_map(em);
2467 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002468 free_extent_map(em);
2469 else
2470 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002471 }
Chris Masond1310b22008-01-24 16:13:08 -05002472 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002473 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002474 err = -ENOMEM;
2475 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002476 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002477 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002478 em->start = EXTENT_MAP_HOLE;
2479 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002480
2481 if (!path) {
2482 path = btrfs_alloc_path();
2483 BUG_ON(!path);
2484 }
2485
Chris Mason179e29e2007-11-01 11:28:41 -04002486 ret = btrfs_lookup_file_extent(trans, root, path,
2487 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002488 if (ret < 0) {
2489 err = ret;
2490 goto out;
2491 }
2492
2493 if (ret != 0) {
2494 if (path->slots[0] == 0)
2495 goto not_found;
2496 path->slots[0]--;
2497 }
2498
Chris Mason5f39d392007-10-15 16:14:19 -04002499 leaf = path->nodes[0];
2500 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002501 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002502 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002503 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2504 found_type = btrfs_key_type(&found_key);
2505 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002506 found_type != BTRFS_EXTENT_DATA_KEY) {
2507 goto not_found;
2508 }
2509
Chris Mason5f39d392007-10-15 16:14:19 -04002510 found_type = btrfs_file_extent_type(leaf, item);
2511 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002512 if (found_type == BTRFS_FILE_EXTENT_REG) {
2513 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002514 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002515 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002516 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002517 em->start = start;
2518 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002519 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002520 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002521 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002522 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002523 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002524 }
2525 goto not_found_em;
2526 }
Chris Masondb945352007-10-15 16:15:53 -04002527 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2528 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002529 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002530 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002531 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002532 goto insert;
2533 }
Chris Masondb945352007-10-15 16:15:53 -04002534 bytenr += btrfs_file_extent_offset(leaf, item);
2535 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002536 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002537 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002538 goto insert;
2539 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002540 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002541 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002542 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002543 size_t size;
2544 size_t extent_offset;
2545 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002546
Chris Mason5f39d392007-10-15 16:14:19 -04002547 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2548 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002549 extent_end = (extent_start + size + root->sectorsize - 1) &
2550 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002551 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002552 em->start = start;
2553 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002554 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002555 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002556 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002557 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002558 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002559 }
2560 goto not_found_em;
2561 }
2562 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002563
2564 if (!page) {
2565 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002566 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002567 goto out;
2568 }
2569
Chris Mason70dec802008-01-29 09:59:12 -05002570 page_start = page_offset(page) + pg_offset;
2571 extent_offset = page_start - extent_start;
2572 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002573 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002574 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002575 em->len = (copy_size + root->sectorsize - 1) &
2576 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002577 map = kmap(page);
2578 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002579 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002580 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002581 copy_size);
2582 flush_dcache_page(page);
2583 } else if (create && PageUptodate(page)) {
2584 if (!trans) {
2585 kunmap(page);
2586 free_extent_map(em);
2587 em = NULL;
2588 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002589 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002590 goto again;
2591 }
Chris Mason70dec802008-01-29 09:59:12 -05002592 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002593 copy_size);
2594 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002595 }
Chris Masona52d9a82007-08-27 16:49:44 -04002596 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002597 set_extent_uptodate(io_tree, em->start,
2598 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002599 goto insert;
2600 } else {
2601 printk("unkknown found_type %d\n", found_type);
2602 WARN_ON(1);
2603 }
2604not_found:
2605 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002606 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002607not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002608 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002609insert:
2610 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002611 if (em->start > start || extent_map_end(em) <= start) {
2612 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002613 err = -EIO;
2614 goto out;
2615 }
Chris Masond1310b22008-01-24 16:13:08 -05002616
2617 err = 0;
2618 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002619 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002620 /* it is possible that someone inserted the extent into the tree
2621 * while we had the lock dropped. It is also possible that
2622 * an overlapping map exists in the tree
2623 */
Chris Masona52d9a82007-08-27 16:49:44 -04002624 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002625 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002626
2627 ret = 0;
2628
Chris Mason3b951512008-04-17 11:29:12 -04002629 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002630 if (existing && (existing->start > start ||
2631 existing->start + existing->len <= start)) {
2632 free_extent_map(existing);
2633 existing = NULL;
2634 }
Chris Mason3b951512008-04-17 11:29:12 -04002635 if (!existing) {
2636 existing = lookup_extent_mapping(em_tree, em->start,
2637 em->len);
2638 if (existing) {
2639 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640 em, start,
2641 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002642 free_extent_map(existing);
2643 if (err) {
2644 free_extent_map(em);
2645 em = NULL;
2646 }
2647 } else {
2648 err = -EIO;
2649 printk("failing to insert %Lu %Lu\n",
2650 start, len);
2651 free_extent_map(em);
2652 em = NULL;
2653 }
2654 } else {
2655 free_extent_map(em);
2656 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002657 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002658 }
Chris Masona52d9a82007-08-27 16:49:44 -04002659 }
Chris Masond1310b22008-01-24 16:13:08 -05002660 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002661out:
Chris Masonf4219502008-07-22 11:18:09 -04002662 if (path)
2663 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002664 if (trans) {
2665 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002666 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002667 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002668 }
Chris Masona52d9a82007-08-27 16:49:44 -04002669 }
Chris Masona52d9a82007-08-27 16:49:44 -04002670 if (err) {
2671 free_extent_map(em);
2672 WARN_ON(1);
2673 return ERR_PTR(err);
2674 }
2675 return em;
2676}
2677
Chris Masone1c4b742008-04-22 13:26:46 -04002678#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002679static int btrfs_get_block(struct inode *inode, sector_t iblock,
2680 struct buffer_head *bh_result, int create)
2681{
2682 struct extent_map *em;
2683 u64 start = (u64)iblock << inode->i_blkbits;
2684 struct btrfs_multi_bio *multi = NULL;
2685 struct btrfs_root *root = BTRFS_I(inode)->root;
2686 u64 len;
2687 u64 logical;
2688 u64 map_length;
2689 int ret = 0;
2690
2691 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2692
2693 if (!em || IS_ERR(em))
2694 goto out;
2695
Chris Masone1c4b742008-04-22 13:26:46 -04002696 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002697 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002698 }
Chris Mason16432982008-04-10 10:23:21 -04002699
2700 if (em->block_start == EXTENT_MAP_INLINE) {
2701 ret = -EINVAL;
2702 goto out;
2703 }
2704
Chris Mason16432982008-04-10 10:23:21 -04002705 len = em->start + em->len - start;
2706 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2707
Chris Masone1c4b742008-04-22 13:26:46 -04002708 if (em->block_start == EXTENT_MAP_HOLE ||
2709 em->block_start == EXTENT_MAP_DELALLOC) {
2710 bh_result->b_size = len;
2711 goto out;
2712 }
2713
Chris Mason16432982008-04-10 10:23:21 -04002714 logical = start - em->start;
2715 logical = em->block_start + logical;
2716
2717 map_length = len;
2718 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2719 logical, &map_length, &multi, 0);
2720 BUG_ON(ret);
2721 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2722 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002723
Chris Mason16432982008-04-10 10:23:21 -04002724 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2725 set_buffer_mapped(bh_result);
2726 kfree(multi);
2727out:
2728 free_extent_map(em);
2729 return ret;
2730}
Chris Masone1c4b742008-04-22 13:26:46 -04002731#endif
Chris Mason16432982008-04-10 10:23:21 -04002732
2733static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2734 const struct iovec *iov, loff_t offset,
2735 unsigned long nr_segs)
2736{
Chris Masone1c4b742008-04-22 13:26:46 -04002737 return -EINVAL;
2738#if 0
Chris Mason16432982008-04-10 10:23:21 -04002739 struct file *file = iocb->ki_filp;
2740 struct inode *inode = file->f_mapping->host;
2741
2742 if (rw == WRITE)
2743 return -EINVAL;
2744
2745 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2746 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002747#endif
Chris Mason16432982008-04-10 10:23:21 -04002748}
2749
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002750static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002751{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002752 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002753}
2754
Chris Mason9ebefb182007-06-15 13:50:00 -04002755int btrfs_readpage(struct file *file, struct page *page)
2756{
Chris Masond1310b22008-01-24 16:13:08 -05002757 struct extent_io_tree *tree;
2758 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002759 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002760}
Chris Mason1832a6d2007-12-21 16:27:21 -05002761
Chris Mason39279cc2007-06-12 06:35:45 -04002762static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2763{
Chris Masond1310b22008-01-24 16:13:08 -05002764 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002765
2766
2767 if (current->flags & PF_MEMALLOC) {
2768 redirty_page_for_writepage(wbc, page);
2769 unlock_page(page);
2770 return 0;
2771 }
Chris Masond1310b22008-01-24 16:13:08 -05002772 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002773 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2774}
Chris Mason39279cc2007-06-12 06:35:45 -04002775
Chris Masonf4219502008-07-22 11:18:09 -04002776int btrfs_writepages(struct address_space *mapping,
2777 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002778{
Chris Masond1310b22008-01-24 16:13:08 -05002779 struct extent_io_tree *tree;
2780 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002781 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2782}
2783
Chris Mason3ab2fb52007-11-08 10:59:22 -05002784static int
2785btrfs_readpages(struct file *file, struct address_space *mapping,
2786 struct list_head *pages, unsigned nr_pages)
2787{
Chris Masond1310b22008-01-24 16:13:08 -05002788 struct extent_io_tree *tree;
2789 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002790 return extent_readpages(tree, mapping, pages, nr_pages,
2791 btrfs_get_extent);
2792}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002793static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002794{
Chris Masond1310b22008-01-24 16:13:08 -05002795 struct extent_io_tree *tree;
2796 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002797 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002798
Chris Masond1310b22008-01-24 16:13:08 -05002799 tree = &BTRFS_I(page->mapping->host)->io_tree;
2800 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002801 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002802 if (ret == 1) {
2803 ClearPagePrivate(page);
2804 set_page_private(page, 0);
2805 page_cache_release(page);
2806 }
2807 return ret;
2808}
Chris Mason39279cc2007-06-12 06:35:45 -04002809
Chris Masone6dcd2d2008-07-17 12:53:50 -04002810static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2811{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002812 return __btrfs_releasepage(page, gfp_flags);
2813}
2814
Chris Masona52d9a82007-08-27 16:49:44 -04002815static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2816{
Chris Masond1310b22008-01-24 16:13:08 -05002817 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002818 struct btrfs_ordered_extent *ordered;
2819 u64 page_start = page_offset(page);
2820 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002821
Chris Masone6dcd2d2008-07-17 12:53:50 -04002822 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002823 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002824 if (offset) {
2825 btrfs_releasepage(page, GFP_NOFS);
2826 return;
2827 }
2828
2829 lock_extent(tree, page_start, page_end, GFP_NOFS);
2830 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2831 page_offset(page));
2832 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002833 /*
2834 * IO on this page will never be started, so we need
2835 * to account for any ordered extents now
2836 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002837 clear_extent_bit(tree, page_start, page_end,
2838 EXTENT_DIRTY | EXTENT_DELALLOC |
2839 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002840 btrfs_finish_ordered_io(page->mapping->host,
2841 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002842 btrfs_put_ordered_extent(ordered);
2843 lock_extent(tree, page_start, page_end, GFP_NOFS);
2844 }
2845 clear_extent_bit(tree, page_start, page_end,
2846 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2847 EXTENT_ORDERED,
2848 1, 1, GFP_NOFS);
2849 __btrfs_releasepage(page, GFP_NOFS);
2850
Chris Mason4a096752008-07-21 10:29:44 -04002851 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002852 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002853 ClearPagePrivate(page);
2854 set_page_private(page, 0);
2855 page_cache_release(page);
2856 }
Chris Mason39279cc2007-06-12 06:35:45 -04002857}
2858
Chris Mason9ebefb182007-06-15 13:50:00 -04002859/*
2860 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2861 * called from a page fault handler when a page is first dirtied. Hence we must
2862 * be careful to check for EOF conditions here. We set the page up correctly
2863 * for a written page which means we get ENOSPC checking when writing into
2864 * holes and correct delalloc and unwritten extent mapping on filesystems that
2865 * support these features.
2866 *
2867 * We are not allowed to take the i_mutex here so we have to play games to
2868 * protect against truncate races as the page could now be beyond EOF. Because
2869 * vmtruncate() writes the inode size before removing pages, once we have the
2870 * page lock we can determine safely if the page is beyond EOF. If it is not
2871 * beyond EOF, then the page is guaranteed safe against truncation until we
2872 * unlock the page.
2873 */
2874int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2875{
Chris Mason6da6aba2007-12-18 16:15:09 -05002876 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002877 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002878 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2879 struct btrfs_ordered_extent *ordered;
2880 char *kaddr;
2881 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002882 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002883 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002884 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002886
Chris Mason1832a6d2007-12-21 16:27:21 -05002887 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002888 if (ret)
2889 goto out;
2890
2891 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002892again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002893 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002894 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002895 page_start = page_offset(page);
2896 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002897
Chris Mason9ebefb182007-06-15 13:50:00 -04002898 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002899 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002900 /* page got truncated out from underneath us */
2901 goto out_unlock;
2902 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002903 wait_on_page_writeback(page);
2904
2905 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2906 set_page_extent_mapped(page);
2907
Chris Masoneb84ae02008-07-17 13:53:27 -04002908 /*
2909 * we can't set the delalloc bits if there are pending ordered
2910 * extents. Drop our locks and wait for them to finish
2911 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002912 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2913 if (ordered) {
2914 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2915 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002916 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002917 btrfs_put_ordered_extent(ordered);
2918 goto again;
2919 }
2920
2921 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2922 page_end, GFP_NOFS);
2923 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002924
2925 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002926 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002927 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002928 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002929 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002930
Chris Masone6dcd2d2008-07-17 12:53:50 -04002931 if (zero_start != PAGE_CACHE_SIZE) {
2932 kaddr = kmap(page);
2933 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2934 flush_dcache_page(page);
2935 kunmap(page);
2936 }
Chris Mason247e7432008-07-17 12:53:51 -04002937 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002938 set_page_dirty(page);
2939 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002940
2941out_unlock:
2942 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002943out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002944 return ret;
2945}
2946
Chris Mason39279cc2007-06-12 06:35:45 -04002947static void btrfs_truncate(struct inode *inode)
2948{
2949 struct btrfs_root *root = BTRFS_I(inode)->root;
2950 int ret;
2951 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002952 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04002953 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002954
2955 if (!S_ISREG(inode->i_mode))
2956 return;
2957 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2958 return;
2959
2960 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04002961 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002962
Chris Mason39279cc2007-06-12 06:35:45 -04002963 trans = btrfs_start_transaction(root, 1);
2964 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04002965 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002966
2967 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002968 ret = btrfs_truncate_in_trans(trans, root, inode,
2969 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002970 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002971 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002972
Chris Mason89ce8a62008-06-25 16:01:31 -04002973 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002974 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002975 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002976}
2977
Sven Wegener3b963622008-06-09 21:57:42 -04002978/*
2979 * Invalidate a single dcache entry at the root of the filesystem.
2980 * Needed after creation of snapshot or subvolume.
2981 */
2982void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2983 int namelen)
2984{
2985 struct dentry *alias, *entry;
2986 struct qstr qstr;
2987
2988 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2989 if (alias) {
2990 qstr.name = name;
2991 qstr.len = namelen;
2992 /* change me if btrfs ever gets a d_hash operation */
2993 qstr.hash = full_name_hash(qstr.name, qstr.len);
2994 entry = d_lookup(alias, &qstr);
2995 dput(alias);
2996 if (entry) {
2997 d_invalidate(entry);
2998 dput(entry);
2999 }
3000 }
3001}
3002
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003003int btrfs_create_subvol_root(struct btrfs_root *new_root,
3004 struct btrfs_trans_handle *trans, u64 new_dirid,
3005 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003006{
Chris Mason39279cc2007-06-12 06:35:45 -04003007 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003008
Josef Bacikaec74772008-07-24 12:12:38 -04003009 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003010 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003011 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003012 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003013 inode->i_op = &btrfs_dir_inode_operations;
3014 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003015 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003016
Chris Mason39279cc2007-06-12 06:35:45 -04003017 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003018 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003019
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003020 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003021}
3022
Chris Masonedbd8d42007-12-21 16:27:24 -05003023unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003024 struct file_ra_state *ra, struct file *file,
3025 pgoff_t offset, pgoff_t last_index)
3026{
Chris Mason8e7bf942008-04-28 09:02:36 -04003027 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003028
3029#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003030 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3031 return offset;
3032#else
Chris Mason86479a02007-09-10 19:58:16 -04003033 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3034 return offset + req_size;
3035#endif
3036}
3037
Chris Mason39279cc2007-06-12 06:35:45 -04003038struct inode *btrfs_alloc_inode(struct super_block *sb)
3039{
3040 struct btrfs_inode *ei;
3041
3042 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3043 if (!ei)
3044 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003045 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003046 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003047 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3048 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Chris Mason39279cc2007-06-12 06:35:45 -04003049 return &ei->vfs_inode;
3050}
3051
3052void btrfs_destroy_inode(struct inode *inode)
3053{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003054 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003055 WARN_ON(!list_empty(&inode->i_dentry));
3056 WARN_ON(inode->i_data.nrpages);
3057
Josef Bacik33268ea2008-07-24 12:16:36 -04003058 if (BTRFS_I(inode)->i_acl &&
3059 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3060 posix_acl_release(BTRFS_I(inode)->i_acl);
3061 if (BTRFS_I(inode)->i_default_acl &&
3062 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3063 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3064
Chris Masone6dcd2d2008-07-17 12:53:50 -04003065 while(1) {
3066 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3067 if (!ordered)
3068 break;
3069 else {
3070 printk("found ordered extent %Lu %Lu\n",
3071 ordered->file_offset, ordered->len);
3072 btrfs_remove_ordered_extent(inode, ordered);
3073 btrfs_put_ordered_extent(ordered);
3074 btrfs_put_ordered_extent(ordered);
3075 }
3076 }
Chris Mason8c416c92008-01-14 15:10:26 -05003077 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003078 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3079}
3080
Chris Mason44ec0b72007-10-29 10:55:05 -04003081#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3082static void init_once(struct kmem_cache * cachep, void *foo)
3083#else
Chris Mason39279cc2007-06-12 06:35:45 -04003084static void init_once(void * foo, struct kmem_cache * cachep,
3085 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003086#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003087{
3088 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3089
3090 inode_init_once(&ei->vfs_inode);
3091}
3092
3093void btrfs_destroy_cachep(void)
3094{
3095 if (btrfs_inode_cachep)
3096 kmem_cache_destroy(btrfs_inode_cachep);
3097 if (btrfs_trans_handle_cachep)
3098 kmem_cache_destroy(btrfs_trans_handle_cachep);
3099 if (btrfs_transaction_cachep)
3100 kmem_cache_destroy(btrfs_transaction_cachep);
3101 if (btrfs_bit_radix_cachep)
3102 kmem_cache_destroy(btrfs_bit_radix_cachep);
3103 if (btrfs_path_cachep)
3104 kmem_cache_destroy(btrfs_path_cachep);
3105}
3106
Chris Mason86479a02007-09-10 19:58:16 -04003107struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003108 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003109#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3110 void (*ctor)(struct kmem_cache *, void *)
3111#else
Chris Mason92fee662007-07-25 12:31:35 -04003112 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003113 unsigned long)
3114#endif
3115 )
Chris Mason92fee662007-07-25 12:31:35 -04003116{
3117 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3118 SLAB_MEM_SPREAD | extra_flags), ctor
3119#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3120 ,NULL
3121#endif
3122 );
3123}
3124
Chris Mason39279cc2007-06-12 06:35:45 -04003125int btrfs_init_cachep(void)
3126{
Chris Mason86479a02007-09-10 19:58:16 -04003127 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003128 sizeof(struct btrfs_inode),
3129 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003130 if (!btrfs_inode_cachep)
3131 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003132 btrfs_trans_handle_cachep =
3133 btrfs_cache_create("btrfs_trans_handle_cache",
3134 sizeof(struct btrfs_trans_handle),
3135 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003136 if (!btrfs_trans_handle_cachep)
3137 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003138 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003139 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003140 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003141 if (!btrfs_transaction_cachep)
3142 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003143 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003144 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003145 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003146 if (!btrfs_path_cachep)
3147 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003148 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003149 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003150 if (!btrfs_bit_radix_cachep)
3151 goto fail;
3152 return 0;
3153fail:
3154 btrfs_destroy_cachep();
3155 return -ENOMEM;
3156}
3157
3158static int btrfs_getattr(struct vfsmount *mnt,
3159 struct dentry *dentry, struct kstat *stat)
3160{
3161 struct inode *inode = dentry->d_inode;
3162 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003163 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003164 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003165 return 0;
3166}
3167
3168static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3169 struct inode * new_dir,struct dentry *new_dentry)
3170{
3171 struct btrfs_trans_handle *trans;
3172 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3173 struct inode *new_inode = new_dentry->d_inode;
3174 struct inode *old_inode = old_dentry->d_inode;
3175 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003176 int ret;
3177
3178 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3179 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3180 return -ENOTEMPTY;
3181 }
Chris Mason5f39d392007-10-15 16:14:19 -04003182
Chris Mason1832a6d2007-12-21 16:27:21 -05003183 ret = btrfs_check_free_space(root, 1, 0);
3184 if (ret)
3185 goto out_unlock;
3186
Chris Mason39279cc2007-06-12 06:35:45 -04003187 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003188
Chris Mason39279cc2007-06-12 06:35:45 -04003189 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003190
3191 old_dentry->d_inode->i_nlink++;
3192 old_dir->i_ctime = old_dir->i_mtime = ctime;
3193 new_dir->i_ctime = new_dir->i_mtime = ctime;
3194 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003195
Chris Mason39279cc2007-06-12 06:35:45 -04003196 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3197 if (ret)
3198 goto out_fail;
3199
3200 if (new_inode) {
3201 new_inode->i_ctime = CURRENT_TIME;
3202 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3203 if (ret)
3204 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003205 }
Josef Bacikaec74772008-07-24 12:12:38 -04003206 ret = btrfs_set_inode_index(new_dir, old_inode);
3207 if (ret)
3208 goto out_fail;
3209
Chris Mason9c583092008-01-29 15:15:18 -05003210 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003211 if (ret)
3212 goto out_fail;
3213
3214out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003215 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003216out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003217 return ret;
3218}
3219
3220static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3221 const char *symname)
3222{
3223 struct btrfs_trans_handle *trans;
3224 struct btrfs_root *root = BTRFS_I(dir)->root;
3225 struct btrfs_path *path;
3226 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003227 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003228 int err;
3229 int drop_inode = 0;
3230 u64 objectid;
3231 int name_len;
3232 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003233 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003234 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003235 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003236 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003237
3238 name_len = strlen(symname) + 1;
3239 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3240 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003241
Chris Mason1832a6d2007-12-21 16:27:21 -05003242 err = btrfs_check_free_space(root, 1, 0);
3243 if (err)
3244 goto out_fail;
3245
Chris Mason39279cc2007-06-12 06:35:45 -04003246 trans = btrfs_start_transaction(root, 1);
3247 btrfs_set_trans_block_group(trans, dir);
3248
3249 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3250 if (err) {
3251 err = -ENOSPC;
3252 goto out_unlock;
3253 }
3254
Josef Bacikaec74772008-07-24 12:12:38 -04003255 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003256 dentry->d_name.len,
3257 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003258 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3259 err = PTR_ERR(inode);
3260 if (IS_ERR(inode))
3261 goto out_unlock;
3262
Josef Bacik33268ea2008-07-24 12:16:36 -04003263 err = btrfs_init_acl(inode, dir);
3264 if (err) {
3265 drop_inode = 1;
3266 goto out_unlock;
3267 }
3268
Chris Mason39279cc2007-06-12 06:35:45 -04003269 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003270 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003271 if (err)
3272 drop_inode = 1;
3273 else {
3274 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003275 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003276 inode->i_fop = &btrfs_file_operations;
3277 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003278 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3279 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003280 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003281 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3282 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003283 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003284 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003285 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003286 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003287 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003288 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003289 }
3290 dir->i_sb->s_dirt = 1;
3291 btrfs_update_inode_block_group(trans, inode);
3292 btrfs_update_inode_block_group(trans, dir);
3293 if (drop_inode)
3294 goto out_unlock;
3295
3296 path = btrfs_alloc_path();
3297 BUG_ON(!path);
3298 key.objectid = inode->i_ino;
3299 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003300 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3301 datasize = btrfs_file_extent_calc_inline_size(name_len);
3302 err = btrfs_insert_empty_item(trans, root, path, &key,
3303 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003304 if (err) {
3305 drop_inode = 1;
3306 goto out_unlock;
3307 }
Chris Mason5f39d392007-10-15 16:14:19 -04003308 leaf = path->nodes[0];
3309 ei = btrfs_item_ptr(leaf, path->slots[0],
3310 struct btrfs_file_extent_item);
3311 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3312 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003313 BTRFS_FILE_EXTENT_INLINE);
3314 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003315 write_extent_buffer(leaf, symname, ptr, name_len);
3316 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003317 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003318
Chris Mason39279cc2007-06-12 06:35:45 -04003319 inode->i_op = &btrfs_symlink_inode_operations;
3320 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003321 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003322 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003323 err = btrfs_update_inode(trans, root, inode);
3324 if (err)
3325 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003326
3327out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003328 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003329 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003330out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003331 if (drop_inode) {
3332 inode_dec_link_count(inode);
3333 iput(inode);
3334 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003335 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003336 return err;
3337}
Chris Mason16432982008-04-10 10:23:21 -04003338
Chris Masone6dcd2d2008-07-17 12:53:50 -04003339static int btrfs_set_page_dirty(struct page *page)
3340{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003341 return __set_page_dirty_nobuffers(page);
3342}
3343
Yanfdebe2b2008-01-14 13:26:08 -05003344static int btrfs_permission(struct inode *inode, int mask,
3345 struct nameidata *nd)
3346{
3347 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3348 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003349 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003350}
Chris Mason39279cc2007-06-12 06:35:45 -04003351
3352static struct inode_operations btrfs_dir_inode_operations = {
3353 .lookup = btrfs_lookup,
3354 .create = btrfs_create,
3355 .unlink = btrfs_unlink,
3356 .link = btrfs_link,
3357 .mkdir = btrfs_mkdir,
3358 .rmdir = btrfs_rmdir,
3359 .rename = btrfs_rename,
3360 .symlink = btrfs_symlink,
3361 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003362 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003363 .setxattr = generic_setxattr,
3364 .getxattr = generic_getxattr,
3365 .listxattr = btrfs_listxattr,
3366 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003367 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003368};
Chris Mason39279cc2007-06-12 06:35:45 -04003369static struct inode_operations btrfs_dir_ro_inode_operations = {
3370 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003371 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003372};
Chris Mason39279cc2007-06-12 06:35:45 -04003373static struct file_operations btrfs_dir_file_operations = {
3374 .llseek = generic_file_llseek,
3375 .read = generic_read_dir,
3376 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003377 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003378#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003379 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003380#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003381 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003382};
3383
Chris Masond1310b22008-01-24 16:13:08 -05003384static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003385 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003386 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003387 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003388 .readpage_io_hook = btrfs_readpage_io_hook,
3389 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003390 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003391 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003392 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003393 .set_bit_hook = btrfs_set_bit_hook,
3394 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003395};
3396
Chris Mason39279cc2007-06-12 06:35:45 -04003397static struct address_space_operations btrfs_aops = {
3398 .readpage = btrfs_readpage,
3399 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003400 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003401 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003402 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003403 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003404 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003405 .invalidatepage = btrfs_invalidatepage,
3406 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003407 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003408};
3409
3410static struct address_space_operations btrfs_symlink_aops = {
3411 .readpage = btrfs_readpage,
3412 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003413 .invalidatepage = btrfs_invalidatepage,
3414 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003415};
3416
3417static struct inode_operations btrfs_file_inode_operations = {
3418 .truncate = btrfs_truncate,
3419 .getattr = btrfs_getattr,
3420 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003421 .setxattr = generic_setxattr,
3422 .getxattr = generic_getxattr,
3423 .listxattr = btrfs_listxattr,
3424 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003425 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003426};
Josef Bacik618e21d2007-07-11 10:18:17 -04003427static struct inode_operations btrfs_special_inode_operations = {
3428 .getattr = btrfs_getattr,
3429 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003430 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003431 .setxattr = generic_setxattr,
3432 .getxattr = generic_getxattr,
3433 .listxattr = btrfs_listxattr,
3434 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003435};
Chris Mason39279cc2007-06-12 06:35:45 -04003436static struct inode_operations btrfs_symlink_inode_operations = {
3437 .readlink = generic_readlink,
3438 .follow_link = page_follow_link_light,
3439 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003440 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003441};