blob: c5a62f0b9595ffd5e5e6bd3848b1936ef6d78dcd [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040046#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047
48struct btrfs_iget_args {
49 u64 ino;
50 struct btrfs_root *root;
51};
52
53static struct inode_operations btrfs_dir_inode_operations;
54static struct inode_operations btrfs_symlink_inode_operations;
55static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040056static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040057static struct inode_operations btrfs_file_inode_operations;
58static struct address_space_operations btrfs_aops;
59static struct address_space_operations btrfs_symlink_aops;
60static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050061static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040062
63static struct kmem_cache *btrfs_inode_cachep;
64struct kmem_cache *btrfs_trans_handle_cachep;
65struct kmem_cache *btrfs_transaction_cachep;
66struct kmem_cache *btrfs_bit_radix_cachep;
67struct kmem_cache *btrfs_path_cachep;
68
69#define S_SHIFT 12
70static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
71 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
72 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
73 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
74 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
75 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
76 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
77 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
78};
79
Chris Mason1832a6d2007-12-21 16:27:21 -050080int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
81 int for_del)
82{
Chris Masona2135012008-06-25 16:01:30 -040083 u64 total;
84 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040086 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 int ret = 0;
88
Chris Masona2135012008-06-25 16:01:30 -040089 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
90 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
91 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050092 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050093 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050094 else
Chris Masonf9ef6602008-01-03 09:22:38 -050095 thresh = total * 85;
96
97 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050098
Chris Mason1832a6d2007-12-21 16:27:21 -050099 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
100 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400101 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 return ret;
103}
104
Chris Masonbe20aa92007-12-17 20:14:01 -0500105static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400106{
107 struct btrfs_root *root = BTRFS_I(inode)->root;
108 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400109 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400110 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500111 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400112 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500113 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400115 struct extent_map *em;
116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
117 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400118
Chris Masonb888db22007-08-27 16:49:44 -0400119 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400120 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500121 btrfs_set_trans_block_group(trans, inode);
122
Chris Masondb945352007-10-15 16:15:53 -0400123 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500125 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400126
Chris Mason179e29e2007-11-01 11:28:41 -0400127 if (alloc_hint == EXTENT_MAP_INLINE)
128 goto out;
129
Chris Mason3b951512008-04-17 11:29:12 -0400130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400131 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Mason3b951512008-04-17 11:29:12 -0400132
Chris Masonc59f8952007-12-17 20:14:04 -0500133 while(num_bytes > 0) {
134 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
136 root->sectorsize, 0, 0,
137 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500138 if (ret) {
139 WARN_ON(1);
140 goto out;
141 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400142 em = alloc_extent_map(GFP_NOFS);
143 em->start = start;
144 em->len = ins.offset;
145 em->block_start = ins.objectid;
146 em->bdev = root->fs_info->fs_devices->latest_bdev;
147 while(1) {
148 spin_lock(&em_tree->lock);
149 ret = add_extent_mapping(em_tree, em);
150 spin_unlock(&em_tree->lock);
151 if (ret != -EEXIST) {
152 free_extent_map(em);
153 break;
154 }
155 btrfs_drop_extent_cache(inode, start,
156 start + ins.offset - 1);
157 }
158
Chris Mason98d20f62008-04-14 09:46:10 -0400159 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400160 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
161 ins.offset);
162 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400163 if (num_bytes < cur_alloc_size) {
164 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
165 cur_alloc_size);
166 break;
167 }
Chris Masonc59f8952007-12-17 20:14:04 -0500168 num_bytes -= cur_alloc_size;
169 alloc_hint = ins.objectid + ins.offset;
170 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400171 }
Chris Masonb888db22007-08-27 16:49:44 -0400172out:
173 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500174 return ret;
175}
176
177static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
178{
179 u64 extent_start;
180 u64 extent_end;
181 u64 bytenr;
182 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500183 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500184 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500185 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400186 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500187 struct extent_buffer *leaf;
188 int found_type;
189 struct btrfs_path *path;
190 struct btrfs_file_extent_item *item;
191 int ret;
192 int err;
193 struct btrfs_key found_key;
194
Chris Masonc31f8832008-01-08 15:46:31 -0500195 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 path = btrfs_alloc_path();
197 BUG_ON(!path);
198again:
199 ret = btrfs_lookup_file_extent(NULL, root, path,
200 inode->i_ino, start, 0);
201 if (ret < 0) {
202 btrfs_free_path(path);
203 return ret;
204 }
205
206 cow_end = end;
207 if (ret != 0) {
208 if (path->slots[0] == 0)
209 goto not_found;
210 path->slots[0]--;
211 }
212
213 leaf = path->nodes[0];
214 item = btrfs_item_ptr(leaf, path->slots[0],
215 struct btrfs_file_extent_item);
216
217 /* are we inside the extent that was found? */
218 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
219 found_type = btrfs_key_type(&found_key);
220 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400221 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
224 found_type = btrfs_file_extent_type(leaf, item);
225 extent_start = found_key.offset;
226 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500227 u64 extent_num_bytes;
228
229 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
230 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 err = 0;
232
Chris Mason1832a6d2007-12-21 16:27:21 -0500233 if (loops && start != extent_start)
234 goto not_found;
235
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 if (start < extent_start || start >= extent_end)
237 goto not_found;
238
239 cow_end = min(end, extent_end - 1);
240 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
241 if (bytenr == 0)
242 goto not_found;
243
Chris Masona68d5932008-05-08 14:11:56 -0400244 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
245 bytenr) != 1) {
246 goto not_found;
247 }
248
Chris Masonc31f8832008-01-08 15:46:31 -0500249 /*
250 * we may be called by the resizer, make sure we're inside
251 * the limits of the FS
252 */
Chris Masona68d5932008-05-08 14:11:56 -0400253 block_group = btrfs_lookup_block_group(root->fs_info,
254 bytenr);
255 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500256 goto not_found;
257
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500259 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 goto not_found;
261 }
262loop:
263 if (start > end) {
264 btrfs_free_path(path);
265 return 0;
266 }
267 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500268 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500269 goto again;
270
271not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400272 cow_file_range(inode, start, end);
273 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto loop;
275}
276
277static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
278{
279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400281
Yanb98b6762008-01-08 15:54:37 -0500282 if (btrfs_test_opt(root, NODATACOW) ||
283 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500284 ret = run_delalloc_nocow(inode, start, end);
285 else
286 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500287
Chris Masonb888db22007-08-27 16:49:44 -0400288 return ret;
289}
290
Chris Mason291d6732008-01-29 15:55:23 -0500291int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500293{
Chris Masonbcbfce82008-04-22 13:26:47 -0400294 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500295 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500296 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400297 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500298 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500299 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500301 }
302 return 0;
303}
304
305int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500306 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Chris Masonb0c68f82008-01-31 11:05:37 -0500308 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500309 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400310 unsigned long flags;
311
312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500313 if (end - start + 1 > root->fs_info->delalloc_bytes) {
314 printk("warning: delalloc account %Lu %Lu\n",
315 end - start + 1, root->fs_info->delalloc_bytes);
316 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500317 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 } else {
319 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500320 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500321 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400322 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500323 }
324 return 0;
325}
326
Chris Mason239b14b2008-03-24 15:02:07 -0400327int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
328 size_t size, struct bio *bio)
329{
330 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
331 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400332 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400333 u64 length = 0;
334 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400335 int ret;
336
Chris Masonf2d8d742008-04-21 10:03:05 -0400337 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 map_tree = &root->fs_info->mapping_tree;
339 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400340 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400341 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400342
Chris Mason239b14b2008-03-24 15:02:07 -0400343 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400344 return 1;
345 }
346 return 0;
347}
348
Chris Mason44b8bd72008-04-16 11:14:51 -0400349int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400350 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500351{
Chris Mason065631f2008-02-20 12:07:25 -0500352 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500353 int ret = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400354 struct btrfs_ordered_sum *sums;
Chris Masone0156402008-04-16 11:15:20 -0400355
356 ret = btrfs_csum_one_bio(root, bio, &sums);
357 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500358
Chris Masone6dcd2d2008-07-17 12:53:50 -0400359 ret = btrfs_add_ordered_sum(inode, sums);
Chris Mason44b8bd72008-04-16 11:14:51 -0400360 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400361
Chris Mason8b712842008-06-11 16:50:36 -0400362 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400363}
364
365int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
366 int mirror_num)
367{
368 struct btrfs_root *root = BTRFS_I(inode)->root;
369 int ret = 0;
370
Chris Masone6dcd2d2008-07-17 12:53:50 -0400371 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
372 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500373
Chris Masone6dcd2d2008-07-17 12:53:50 -0400374 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400375 goto mapit;
376 }
Chris Mason065631f2008-02-20 12:07:25 -0500377
Chris Mason44b8bd72008-04-16 11:14:51 -0400378 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
379 inode, rw, bio, mirror_num,
380 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400381mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400382 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500383}
Chris Mason6885f302008-02-20 16:11:05 -0500384
Chris Masone6dcd2d2008-07-17 12:53:50 -0400385static int add_pending_csums(struct btrfs_trans_handle *trans,
386 struct inode *inode, u64 file_offset,
387 struct list_head *list)
388{
389 struct list_head *cur;
390 struct btrfs_ordered_sum *sum;
391
392 btrfs_set_trans_block_group(trans, inode);
393 while(!list_empty(list)) {
394 cur = list->next;
395 sum = list_entry(cur, struct btrfs_ordered_sum, list);
396 mutex_lock(&BTRFS_I(inode)->csum_mutex);
397 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
398 inode, sum);
399 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
400 list_del(&sum->list);
401 kfree(sum);
402 }
403 return 0;
404}
405
406int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
407 struct extent_state *state, int uptodate)
408{
409 struct inode *inode = page->mapping->host;
410 struct btrfs_root *root = BTRFS_I(inode)->root;
411 struct btrfs_trans_handle *trans;
412 struct btrfs_ordered_extent *ordered_extent;
413 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
414 u64 alloc_hint = 0;
415 struct list_head list;
416 struct btrfs_key ins;
417 int ret;
418
419 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
420 if (!ret) {
421 return 0;
422 }
423
424 trans = btrfs_start_transaction(root, 1);
425
426 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
427 BUG_ON(!ordered_extent);
428
429 lock_extent(io_tree, ordered_extent->file_offset,
430 ordered_extent->file_offset + ordered_extent->len - 1,
431 GFP_NOFS);
432
433 INIT_LIST_HEAD(&list);
434
435 ins.objectid = ordered_extent->start;
436 ins.offset = ordered_extent->len;
437 ins.type = BTRFS_EXTENT_ITEM_KEY;
438 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
439 trans->transid, inode->i_ino,
440 ordered_extent->file_offset, &ins);
441 BUG_ON(ret);
442 ret = btrfs_drop_extents(trans, root, inode,
443 ordered_extent->file_offset,
444 ordered_extent->file_offset +
445 ordered_extent->len,
446 ordered_extent->file_offset, &alloc_hint);
447 BUG_ON(ret);
448 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
449 ordered_extent->file_offset,
450 ordered_extent->start,
451 ordered_extent->len,
452 ordered_extent->len, 0);
453 BUG_ON(ret);
454 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
455 ordered_extent->file_offset +
456 ordered_extent->len - 1);
457 inode->i_blocks += ordered_extent->len >> 9;
458 unlock_extent(io_tree, ordered_extent->file_offset,
459 ordered_extent->file_offset + ordered_extent->len - 1,
460 GFP_NOFS);
461 add_pending_csums(trans, inode, ordered_extent->file_offset,
462 &ordered_extent->list);
463
464 btrfs_remove_ordered_extent(inode, ordered_extent);
465 /* once for us */
466 btrfs_put_ordered_extent(ordered_extent);
467 /* once for the tree */
468 btrfs_put_ordered_extent(ordered_extent);
469
470 btrfs_update_inode(trans, root, inode);
471 btrfs_end_transaction(trans, root);
472 return 0;
473}
474
Chris Mason07157aa2007-08-30 08:50:51 -0400475int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
476{
477 int ret = 0;
478 struct inode *inode = page->mapping->host;
479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500480 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400481 struct btrfs_csum_item *item;
482 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400483 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400484
Yanb98b6762008-01-08 15:54:37 -0500485 if (btrfs_test_opt(root, NODATASUM) ||
486 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500487 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400488
Chris Mason07157aa2007-08-30 08:50:51 -0400489 path = btrfs_alloc_path();
490 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
491 if (IS_ERR(item)) {
492 ret = PTR_ERR(item);
493 /* a csum that isn't present is a preallocated region. */
494 if (ret == -ENOENT || ret == -EFBIG)
495 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400496 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400497 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
498 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400499 goto out;
500 }
Chris Masonff79f812007-10-15 16:22:25 -0400501 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
502 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500503 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400504out:
505 if (path)
506 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400507 return ret;
508}
509
Chris Mason7e383262008-04-09 16:28:12 -0400510struct io_failure_record {
511 struct page *page;
512 u64 start;
513 u64 len;
514 u64 logical;
515 int last_mirror;
516};
517
Chris Mason1259ab72008-05-12 13:39:03 -0400518int btrfs_io_failed_hook(struct bio *failed_bio,
519 struct page *page, u64 start, u64 end,
520 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400521{
522 struct io_failure_record *failrec = NULL;
523 u64 private;
524 struct extent_map *em;
525 struct inode *inode = page->mapping->host;
526 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400527 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400528 struct bio *bio;
529 int num_copies;
530 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400531 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400532 u64 logical;
533
534 ret = get_state_private(failure_tree, start, &private);
535 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400536 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
537 if (!failrec)
538 return -ENOMEM;
539 failrec->start = start;
540 failrec->len = end - start + 1;
541 failrec->last_mirror = 0;
542
Chris Mason3b951512008-04-17 11:29:12 -0400543 spin_lock(&em_tree->lock);
544 em = lookup_extent_mapping(em_tree, start, failrec->len);
545 if (em->start > start || em->start + em->len < start) {
546 free_extent_map(em);
547 em = NULL;
548 }
549 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400550
551 if (!em || IS_ERR(em)) {
552 kfree(failrec);
553 return -EIO;
554 }
555 logical = start - em->start;
556 logical = em->block_start + logical;
557 failrec->logical = logical;
558 free_extent_map(em);
559 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
560 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400561 set_state_private(failure_tree, start,
562 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400563 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400564 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400565 }
566 num_copies = btrfs_num_copies(
567 &BTRFS_I(inode)->root->fs_info->mapping_tree,
568 failrec->logical, failrec->len);
569 failrec->last_mirror++;
570 if (!state) {
571 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
572 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
573 failrec->start,
574 EXTENT_LOCKED);
575 if (state && state->start != failrec->start)
576 state = NULL;
577 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
578 }
579 if (!state || failrec->last_mirror > num_copies) {
580 set_state_private(failure_tree, failrec->start, 0);
581 clear_extent_bits(failure_tree, failrec->start,
582 failrec->start + failrec->len - 1,
583 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
584 kfree(failrec);
585 return -EIO;
586 }
587 bio = bio_alloc(GFP_NOFS, 1);
588 bio->bi_private = state;
589 bio->bi_end_io = failed_bio->bi_end_io;
590 bio->bi_sector = failrec->logical >> 9;
591 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400592 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400593 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400594 if (failed_bio->bi_rw & (1 << BIO_RW))
595 rw = WRITE;
596 else
597 rw = READ;
598
599 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
600 failrec->last_mirror);
601 return 0;
602}
603
604int btrfs_clean_io_failures(struct inode *inode, u64 start)
605{
606 u64 private;
607 u64 private_failure;
608 struct io_failure_record *failure;
609 int ret;
610
611 private = 0;
612 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
613 (u64)-1, 1, EXTENT_DIRTY)) {
614 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
615 start, &private_failure);
616 if (ret == 0) {
617 failure = (struct io_failure_record *)(unsigned long)
618 private_failure;
619 set_state_private(&BTRFS_I(inode)->io_failure_tree,
620 failure->start, 0);
621 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
622 failure->start,
623 failure->start + failure->len - 1,
624 EXTENT_DIRTY | EXTENT_LOCKED,
625 GFP_NOFS);
626 kfree(failure);
627 }
628 }
Chris Mason7e383262008-04-09 16:28:12 -0400629 return 0;
630}
631
Chris Mason70dec802008-01-29 09:59:12 -0500632int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
633 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400634{
Chris Mason35ebb932007-10-30 16:56:53 -0400635 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400636 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500637 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400638 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500639 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400640 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400641 struct btrfs_root *root = BTRFS_I(inode)->root;
642 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400643 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500644
Yanb98b6762008-01-08 15:54:37 -0500645 if (btrfs_test_opt(root, NODATASUM) ||
646 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500647 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500648 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500649 private = state->private;
650 ret = 0;
651 } else {
652 ret = get_state_private(io_tree, start, &private);
653 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400654 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400655 kaddr = kmap_atomic(page, KM_IRQ0);
656 if (ret) {
657 goto zeroit;
658 }
Chris Masonff79f812007-10-15 16:22:25 -0400659 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
660 btrfs_csum_final(csum, (char *)&csum);
661 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400662 goto zeroit;
663 }
664 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400665 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400666
667 /* if the io failure tree for this inode is non-empty,
668 * check to see if we've recovered from a failed IO
669 */
Chris Mason1259ab72008-05-12 13:39:03 -0400670 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400671 return 0;
672
673zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500674 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
675 page->mapping->host->i_ino, (unsigned long long)start, csum,
676 private);
Chris Masondb945352007-10-15 16:15:53 -0400677 memset(kaddr + offset, 1, end - start + 1);
678 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400679 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400680 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400681 if (private == 0)
682 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400683 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400684}
Chris Masonb888db22007-08-27 16:49:44 -0400685
Chris Mason39279cc2007-06-12 06:35:45 -0400686void btrfs_read_locked_inode(struct inode *inode)
687{
688 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400689 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400690 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400691 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400692 struct btrfs_root *root = BTRFS_I(inode)->root;
693 struct btrfs_key location;
694 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400695 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400696 int ret;
697
698 path = btrfs_alloc_path();
699 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400700 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500701
Chris Mason39279cc2007-06-12 06:35:45 -0400702 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400703 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400704 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400705
Chris Mason5f39d392007-10-15 16:14:19 -0400706 leaf = path->nodes[0];
707 inode_item = btrfs_item_ptr(leaf, path->slots[0],
708 struct btrfs_inode_item);
709
710 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
711 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
712 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
713 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
714 inode->i_size = btrfs_inode_size(leaf, inode_item);
715
716 tspec = btrfs_inode_atime(inode_item);
717 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
718 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
719
720 tspec = btrfs_inode_mtime(inode_item);
721 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
722 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
723
724 tspec = btrfs_inode_ctime(inode_item);
725 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
726 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
727
728 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
729 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400730 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400731 rdev = btrfs_inode_rdev(leaf, inode_item);
732
733 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400734 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
735 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500736 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500737 if (!BTRFS_I(inode)->block_group) {
738 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400739 NULL, 0,
740 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500741 }
Chris Mason39279cc2007-06-12 06:35:45 -0400742 btrfs_free_path(path);
743 inode_item = NULL;
744
Chris Mason39279cc2007-06-12 06:35:45 -0400745 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400746 case S_IFREG:
747 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400748 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500749 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400750 inode->i_fop = &btrfs_file_operations;
751 inode->i_op = &btrfs_file_inode_operations;
752 break;
753 case S_IFDIR:
754 inode->i_fop = &btrfs_dir_file_operations;
755 if (root == root->fs_info->tree_root)
756 inode->i_op = &btrfs_dir_ro_inode_operations;
757 else
758 inode->i_op = &btrfs_dir_inode_operations;
759 break;
760 case S_IFLNK:
761 inode->i_op = &btrfs_symlink_inode_operations;
762 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400763 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400764 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400765 default:
766 init_special_inode(inode, inode->i_mode, rdev);
767 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400768 }
769 return;
770
771make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400772 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400773 make_bad_inode(inode);
774}
775
Chris Mason5f39d392007-10-15 16:14:19 -0400776static void fill_inode_item(struct extent_buffer *leaf,
777 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400778 struct inode *inode)
779{
Chris Mason5f39d392007-10-15 16:14:19 -0400780 btrfs_set_inode_uid(leaf, item, inode->i_uid);
781 btrfs_set_inode_gid(leaf, item, inode->i_gid);
782 btrfs_set_inode_size(leaf, item, inode->i_size);
783 btrfs_set_inode_mode(leaf, item, inode->i_mode);
784 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
785
786 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
787 inode->i_atime.tv_sec);
788 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
789 inode->i_atime.tv_nsec);
790
791 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
792 inode->i_mtime.tv_sec);
793 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
794 inode->i_mtime.tv_nsec);
795
796 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
797 inode->i_ctime.tv_sec);
798 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
799 inode->i_ctime.tv_nsec);
800
801 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
802 btrfs_set_inode_generation(leaf, item, inode->i_generation);
803 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500804 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400805 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400806 BTRFS_I(inode)->block_group->key.objectid);
807}
808
Chris Masona52d9a82007-08-27 16:49:44 -0400809int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400810 struct btrfs_root *root,
811 struct inode *inode)
812{
813 struct btrfs_inode_item *inode_item;
814 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400815 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400816 int ret;
817
818 path = btrfs_alloc_path();
819 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400820 ret = btrfs_lookup_inode(trans, root, path,
821 &BTRFS_I(inode)->location, 1);
822 if (ret) {
823 if (ret > 0)
824 ret = -ENOENT;
825 goto failed;
826 }
827
Chris Mason5f39d392007-10-15 16:14:19 -0400828 leaf = path->nodes[0];
829 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400830 struct btrfs_inode_item);
831
Chris Mason5f39d392007-10-15 16:14:19 -0400832 fill_inode_item(leaf, inode_item, inode);
833 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400834 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400835 ret = 0;
836failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400837 btrfs_free_path(path);
838 return ret;
839}
840
841
842static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
843 struct btrfs_root *root,
844 struct inode *dir,
845 struct dentry *dentry)
846{
847 struct btrfs_path *path;
848 const char *name = dentry->d_name.name;
849 int name_len = dentry->d_name.len;
850 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400851 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400852 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400853 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400854
855 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 if (!path) {
857 ret = -ENOMEM;
858 goto err;
859 }
860
Chris Mason39279cc2007-06-12 06:35:45 -0400861 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
862 name, name_len, -1);
863 if (IS_ERR(di)) {
864 ret = PTR_ERR(di);
865 goto err;
866 }
867 if (!di) {
868 ret = -ENOENT;
869 goto err;
870 }
Chris Mason5f39d392007-10-15 16:14:19 -0400871 leaf = path->nodes[0];
872 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400873 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400874 if (ret)
875 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400876 btrfs_release_path(root, path);
877
878 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400879 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400880 if (IS_ERR(di)) {
881 ret = PTR_ERR(di);
882 goto err;
883 }
884 if (!di) {
885 ret = -ENOENT;
886 goto err;
887 }
888 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -0400889 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400890
891 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500892 ret = btrfs_del_inode_ref(trans, root, name, name_len,
893 dentry->d_inode->i_ino,
894 dentry->d_parent->d_inode->i_ino);
895 if (ret) {
896 printk("failed to delete reference to %.*s, "
897 "inode %lu parent %lu\n", name_len, name,
898 dentry->d_inode->i_ino,
899 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500900 }
Chris Mason39279cc2007-06-12 06:35:45 -0400901err:
902 btrfs_free_path(path);
903 if (!ret) {
904 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400905 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400906 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500907#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
908 dentry->d_inode->i_nlink--;
909#else
Chris Mason39279cc2007-06-12 06:35:45 -0400910 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500911#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400912 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400913 dir->i_sb->s_dirt = 1;
914 }
915 return ret;
916}
917
918static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
919{
920 struct btrfs_root *root;
921 struct btrfs_trans_handle *trans;
922 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500923 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400924
925 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500926
927 ret = btrfs_check_free_space(root, 1, 1);
928 if (ret)
929 goto fail;
930
Chris Mason39279cc2007-06-12 06:35:45 -0400931 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400932
Chris Mason39279cc2007-06-12 06:35:45 -0400933 btrfs_set_trans_block_group(trans, dir);
934 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400935 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400936
Chris Mason89ce8a62008-06-25 16:01:31 -0400937 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500938fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -0400939 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400940 return ret;
941}
942
943static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
944{
945 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500946 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400947 int ret;
948 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400949 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500950 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400951
Chris Mason925baed2008-06-25 16:01:30 -0400952 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -0400953 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -0400954 }
Yan134d4512007-10-25 15:49:25 -0400955
Chris Mason1832a6d2007-12-21 16:27:21 -0500956 ret = btrfs_check_free_space(root, 1, 1);
957 if (ret)
958 goto fail;
959
Chris Mason39279cc2007-06-12 06:35:45 -0400960 trans = btrfs_start_transaction(root, 1);
961 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400962
963 /* now the directory is empty */
964 err = btrfs_unlink_trans(trans, root, dir, dentry);
965 if (!err) {
966 inode->i_size = 0;
967 }
Chris Mason39544012007-12-12 14:38:19 -0500968
Chris Masond3c2fdc2007-09-17 10:58:06 -0400969 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -0400970 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500971fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -0400972 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500973
Chris Mason39279cc2007-06-12 06:35:45 -0400974 if (ret && !err)
975 err = ret;
976 return err;
977}
978
Chris Mason39279cc2007-06-12 06:35:45 -0400979/*
Chris Mason39279cc2007-06-12 06:35:45 -0400980 * this can truncate away extent items, csum items and directory items.
981 * It starts at a high offset and removes keys until it can't find
982 * any higher than i_size.
983 *
984 * csum items that cross the new i_size are truncated to the new size
985 * as well.
986 */
987static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500989 struct inode *inode,
990 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400991{
992 int ret;
993 struct btrfs_path *path;
994 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400995 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400996 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400997 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400998 struct btrfs_file_extent_item *fi;
999 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001000 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001001 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001002 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001003 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001004 int found_extent;
1005 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001006 int pending_del_nr = 0;
1007 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001008 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001009 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001010
Chris Masone6dcd2d2008-07-17 12:53:50 -04001011 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason3b951512008-04-17 11:29:12 -04001012 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001013 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001014 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001015 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001016
Chris Mason39279cc2007-06-12 06:35:45 -04001017 /* FIXME, add redo link to tree so we don't leak on crash */
1018 key.objectid = inode->i_ino;
1019 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001020 key.type = (u8)-1;
1021
Chris Mason85e21ba2008-01-29 15:11:36 -05001022 btrfs_init_path(path);
1023search_again:
1024 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1025 if (ret < 0) {
1026 goto error;
1027 }
1028 if (ret > 0) {
1029 BUG_ON(path->slots[0] == 0);
1030 path->slots[0]--;
1031 }
1032
Chris Mason39279cc2007-06-12 06:35:45 -04001033 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001034 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001035 leaf = path->nodes[0];
1036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1037 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001038
Chris Mason5f39d392007-10-15 16:14:19 -04001039 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001040 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001041
Chris Mason85e21ba2008-01-29 15:11:36 -05001042 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001043 break;
1044
Chris Mason5f39d392007-10-15 16:14:19 -04001045 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001046 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001047 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001048 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001049 extent_type = btrfs_file_extent_type(leaf, fi);
1050 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001051 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001052 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001053 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1054 struct btrfs_item *item = btrfs_item_nr(leaf,
1055 path->slots[0]);
1056 item_end += btrfs_file_extent_inline_len(leaf,
1057 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001058 }
Yan008630c2007-11-07 13:31:09 -05001059 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001060 }
1061 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1062 ret = btrfs_csum_truncate(trans, root, path,
1063 inode->i_size);
1064 BUG_ON(ret);
1065 }
Yan008630c2007-11-07 13:31:09 -05001066 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001067 if (found_type == BTRFS_DIR_ITEM_KEY) {
1068 found_type = BTRFS_INODE_ITEM_KEY;
1069 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1070 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001071 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1072 found_type = BTRFS_XATTR_ITEM_KEY;
1073 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1074 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001075 } else if (found_type) {
1076 found_type--;
1077 } else {
1078 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001079 }
Yana61721d2007-09-17 11:08:38 -04001080 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001081 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001082 }
Chris Mason5f39d392007-10-15 16:14:19 -04001083 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001084 del_item = 1;
1085 else
1086 del_item = 0;
1087 found_extent = 0;
1088
1089 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001090 if (found_type != BTRFS_EXTENT_DATA_KEY)
1091 goto delete;
1092
1093 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001094 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001095 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001096 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001097 u64 orig_num_bytes =
1098 btrfs_file_extent_num_bytes(leaf, fi);
1099 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001100 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05001101 extent_num_bytes = extent_num_bytes &
1102 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001103 btrfs_set_file_extent_num_bytes(leaf, fi,
1104 extent_num_bytes);
1105 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001106 extent_num_bytes);
1107 if (extent_start != 0)
1108 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001109 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001110 } else {
Chris Masondb945352007-10-15 16:15:53 -04001111 extent_num_bytes =
1112 btrfs_file_extent_disk_num_bytes(leaf,
1113 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001114 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001115 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001116 if (extent_start != 0) {
1117 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001118 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001119 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001120 root_gen = btrfs_header_generation(leaf);
1121 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001122 }
Chris Mason90692182008-02-08 13:49:28 -05001123 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1124 if (!del_item) {
1125 u32 newsize = inode->i_size - found_key.offset;
1126 dec_i_blocks(inode, item_end + 1 -
1127 found_key.offset - newsize);
1128 newsize =
1129 btrfs_file_extent_calc_inline_size(newsize);
1130 ret = btrfs_truncate_item(trans, root, path,
1131 newsize, 1);
1132 BUG_ON(ret);
1133 } else {
1134 dec_i_blocks(inode, item_end + 1 -
1135 found_key.offset);
1136 }
Chris Mason39279cc2007-06-12 06:35:45 -04001137 }
Chris Mason179e29e2007-11-01 11:28:41 -04001138delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001139 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001140 if (!pending_del_nr) {
1141 /* no pending yet, add ourselves */
1142 pending_del_slot = path->slots[0];
1143 pending_del_nr = 1;
1144 } else if (pending_del_nr &&
1145 path->slots[0] + 1 == pending_del_slot) {
1146 /* hop on the pending chunk */
1147 pending_del_nr++;
1148 pending_del_slot = path->slots[0];
1149 } else {
1150 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1151 }
Chris Mason39279cc2007-06-12 06:35:45 -04001152 } else {
1153 break;
1154 }
Chris Mason39279cc2007-06-12 06:35:45 -04001155 if (found_extent) {
1156 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001157 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001158 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001159 root_gen, inode->i_ino,
1160 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001161 BUG_ON(ret);
1162 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001163next:
1164 if (path->slots[0] == 0) {
1165 if (pending_del_nr)
1166 goto del_pending;
1167 btrfs_release_path(root, path);
1168 goto search_again;
1169 }
1170
1171 path->slots[0]--;
1172 if (pending_del_nr &&
1173 path->slots[0] + 1 != pending_del_slot) {
1174 struct btrfs_key debug;
1175del_pending:
1176 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1177 pending_del_slot);
1178 ret = btrfs_del_items(trans, root, path,
1179 pending_del_slot,
1180 pending_del_nr);
1181 BUG_ON(ret);
1182 pending_del_nr = 0;
1183 btrfs_release_path(root, path);
1184 goto search_again;
1185 }
Chris Mason39279cc2007-06-12 06:35:45 -04001186 }
1187 ret = 0;
1188error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001189 if (pending_del_nr) {
1190 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1191 pending_del_nr);
1192 }
Chris Mason39279cc2007-06-12 06:35:45 -04001193 btrfs_free_path(path);
1194 inode->i_sb->s_dirt = 1;
1195 return ret;
1196}
1197
Chris Masona52d9a82007-08-27 16:49:44 -04001198/*
1199 * taken from block_truncate_page, but does cow as it zeros out
1200 * any bytes left in the last page in the file.
1201 */
1202static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1203{
1204 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001205 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1207 struct btrfs_ordered_extent *ordered;
1208 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001209 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001210 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1211 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1212 struct page *page;
1213 int ret = 0;
1214 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001215 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001216
1217 if ((offset & (blocksize - 1)) == 0)
1218 goto out;
1219
1220 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001221again:
Chris Masona52d9a82007-08-27 16:49:44 -04001222 page = grab_cache_page(mapping, index);
1223 if (!page)
1224 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001225
1226 page_start = page_offset(page);
1227 page_end = page_start + PAGE_CACHE_SIZE - 1;
1228
Chris Masona52d9a82007-08-27 16:49:44 -04001229 if (!PageUptodate(page)) {
1230 ret = btrfs_readpage(NULL, page);
1231 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001232 if (page->mapping != mapping) {
1233 unlock_page(page);
1234 page_cache_release(page);
1235 goto again;
1236 }
Chris Masona52d9a82007-08-27 16:49:44 -04001237 if (!PageUptodate(page)) {
1238 ret = -EIO;
1239 goto out;
1240 }
1241 }
Chris Mason211c17f2008-05-15 09:13:45 -04001242 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001243
1244 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1245 set_page_extent_mapped(page);
1246
1247 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1248 if (ordered) {
1249 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1250 unlock_page(page);
1251 page_cache_release(page);
1252 btrfs_wait_ordered_extent(inode, ordered);
1253 btrfs_put_ordered_extent(ordered);
1254 goto again;
1255 }
1256
1257 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1258 page_end, GFP_NOFS);
1259 ret = 0;
1260 if (offset != PAGE_CACHE_SIZE) {
1261 kaddr = kmap(page);
1262 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1263 flush_dcache_page(page);
1264 kunmap(page);
1265 }
1266 set_page_dirty(page);
1267 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001268
Chris Mason39279cc2007-06-12 06:35:45 -04001269 unlock_page(page);
1270 page_cache_release(page);
1271out:
1272 return ret;
1273}
1274
1275static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1276{
1277 struct inode *inode = dentry->d_inode;
1278 int err;
1279
1280 err = inode_change_ok(inode, attr);
1281 if (err)
1282 return err;
1283
1284 if (S_ISREG(inode->i_mode) &&
1285 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1286 struct btrfs_trans_handle *trans;
1287 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001288 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001289
Chris Mason5f39d392007-10-15 16:14:19 -04001290 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001291 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001292 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001293 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001294 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001295
Chris Mason1b0f7c22008-01-30 14:33:02 -05001296 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001297 goto out;
1298
Chris Mason1832a6d2007-12-21 16:27:21 -05001299 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001300 if (err)
1301 goto fail;
1302
Chris Mason39279cc2007-06-12 06:35:45 -04001303 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1304
Chris Mason5f564062008-01-22 16:47:59 -05001305 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001306 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1307 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001308
Chris Mason39279cc2007-06-12 06:35:45 -04001309 trans = btrfs_start_transaction(root, 1);
1310 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001311 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001312 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001313 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001314
Chris Mason179e29e2007-11-01 11:28:41 -04001315 if (alloc_hint != EXTENT_MAP_INLINE) {
1316 err = btrfs_insert_file_extent(trans, root,
1317 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001318 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001319 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001320 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001321 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001322 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001323 }
Chris Mason39279cc2007-06-12 06:35:45 -04001324 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001325 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001326 if (err)
1327 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001328 }
1329out:
1330 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001331fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001332 return err;
1333}
Chris Mason61295eb2008-01-14 16:24:38 -05001334
Chris Mason39279cc2007-06-12 06:35:45 -04001335void btrfs_delete_inode(struct inode *inode)
1336{
1337 struct btrfs_trans_handle *trans;
1338 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001339 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001340 int ret;
1341
Chris Masone6dcd2d2008-07-17 12:53:50 -04001342 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001343 truncate_inode_pages(&inode->i_data, 0);
1344 if (is_bad_inode(inode)) {
1345 goto no_delete;
1346 }
Chris Mason5f39d392007-10-15 16:14:19 -04001347
Chris Mason39279cc2007-06-12 06:35:45 -04001348 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001349 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001350
Chris Mason39279cc2007-06-12 06:35:45 -04001351 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001352 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001353 if (ret)
1354 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001355
Chris Masond3c2fdc2007-09-17 10:58:06 -04001356 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001357 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001358
Chris Mason39279cc2007-06-12 06:35:45 -04001359 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001360 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001361 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001362
1363no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001364 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001365 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001366 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001367no_delete:
1368 clear_inode(inode);
1369}
1370
1371/*
1372 * this returns the key found in the dir entry in the location pointer.
1373 * If no dir entries were found, location->objectid is 0.
1374 */
1375static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1376 struct btrfs_key *location)
1377{
1378 const char *name = dentry->d_name.name;
1379 int namelen = dentry->d_name.len;
1380 struct btrfs_dir_item *di;
1381 struct btrfs_path *path;
1382 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001383 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001384
Chris Mason39544012007-12-12 14:38:19 -05001385 if (namelen == 1 && strcmp(name, ".") == 0) {
1386 location->objectid = dir->i_ino;
1387 location->type = BTRFS_INODE_ITEM_KEY;
1388 location->offset = 0;
1389 return 0;
1390 }
Chris Mason39279cc2007-06-12 06:35:45 -04001391 path = btrfs_alloc_path();
1392 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001393
Chris Mason7a720532007-12-13 09:06:59 -05001394 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001395 struct btrfs_key key;
1396 struct extent_buffer *leaf;
1397 u32 nritems;
1398 int slot;
1399
1400 key.objectid = dir->i_ino;
1401 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1402 key.offset = 0;
1403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1404 BUG_ON(ret == 0);
1405 ret = 0;
1406
1407 leaf = path->nodes[0];
1408 slot = path->slots[0];
1409 nritems = btrfs_header_nritems(leaf);
1410 if (slot >= nritems)
1411 goto out_err;
1412
1413 btrfs_item_key_to_cpu(leaf, &key, slot);
1414 if (key.objectid != dir->i_ino ||
1415 key.type != BTRFS_INODE_REF_KEY) {
1416 goto out_err;
1417 }
1418 location->objectid = key.offset;
1419 location->type = BTRFS_INODE_ITEM_KEY;
1420 location->offset = 0;
1421 goto out;
1422 }
1423
Chris Mason39279cc2007-06-12 06:35:45 -04001424 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1425 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001426 if (IS_ERR(di))
1427 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001428 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001429 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001430 }
Chris Mason5f39d392007-10-15 16:14:19 -04001431 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001432out:
Chris Mason39279cc2007-06-12 06:35:45 -04001433 btrfs_free_path(path);
1434 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001435out_err:
1436 location->objectid = 0;
1437 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001438}
1439
1440/*
1441 * when we hit a tree root in a directory, the btrfs part of the inode
1442 * needs to be changed to reflect the root directory of the tree root. This
1443 * is kind of like crossing a mount point.
1444 */
1445static int fixup_tree_root_location(struct btrfs_root *root,
1446 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001447 struct btrfs_root **sub_root,
1448 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001449{
1450 struct btrfs_path *path;
1451 struct btrfs_root_item *ri;
1452
1453 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1454 return 0;
1455 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1456 return 0;
1457
1458 path = btrfs_alloc_path();
1459 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001460
Josef Bacik58176a92007-08-29 15:47:34 -04001461 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1462 dentry->d_name.name,
1463 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001464 if (IS_ERR(*sub_root))
1465 return PTR_ERR(*sub_root);
1466
1467 ri = &(*sub_root)->root_item;
1468 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001469 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1470 location->offset = 0;
1471
1472 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001473 return 0;
1474}
1475
1476static int btrfs_init_locked_inode(struct inode *inode, void *p)
1477{
1478 struct btrfs_iget_args *args = p;
1479 inode->i_ino = args->ino;
1480 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001481 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001482 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1483 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001484 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001485 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1486 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04001487 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001488 return 0;
1489}
1490
1491static int btrfs_find_actor(struct inode *inode, void *opaque)
1492{
1493 struct btrfs_iget_args *args = opaque;
1494 return (args->ino == inode->i_ino &&
1495 args->root == BTRFS_I(inode)->root);
1496}
1497
Chris Masondc17ff82008-01-08 15:46:30 -05001498struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1499 u64 root_objectid)
1500{
1501 struct btrfs_iget_args args;
1502 args.ino = objectid;
1503 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1504
1505 if (!args.root)
1506 return NULL;
1507
1508 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1509}
1510
Chris Mason39279cc2007-06-12 06:35:45 -04001511struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1512 struct btrfs_root *root)
1513{
1514 struct inode *inode;
1515 struct btrfs_iget_args args;
1516 args.ino = objectid;
1517 args.root = root;
1518
1519 inode = iget5_locked(s, objectid, btrfs_find_actor,
1520 btrfs_init_locked_inode,
1521 (void *)&args);
1522 return inode;
1523}
1524
1525static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1526 struct nameidata *nd)
1527{
1528 struct inode * inode;
1529 struct btrfs_inode *bi = BTRFS_I(dir);
1530 struct btrfs_root *root = bi->root;
1531 struct btrfs_root *sub_root = root;
1532 struct btrfs_key location;
1533 int ret;
1534
1535 if (dentry->d_name.len > BTRFS_NAME_LEN)
1536 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001537
Chris Mason39279cc2007-06-12 06:35:45 -04001538 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001539
Chris Mason39279cc2007-06-12 06:35:45 -04001540 if (ret < 0)
1541 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001542
Chris Mason39279cc2007-06-12 06:35:45 -04001543 inode = NULL;
1544 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001545 ret = fixup_tree_root_location(root, &location, &sub_root,
1546 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001547 if (ret < 0)
1548 return ERR_PTR(ret);
1549 if (ret > 0)
1550 return ERR_PTR(-ENOENT);
1551 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1552 sub_root);
1553 if (!inode)
1554 return ERR_PTR(-EACCES);
1555 if (inode->i_state & I_NEW) {
1556 /* the inode and parent dir are two different roots */
1557 if (sub_root != root) {
1558 igrab(inode);
1559 sub_root->inode = inode;
1560 }
1561 BTRFS_I(inode)->root = sub_root;
1562 memcpy(&BTRFS_I(inode)->location, &location,
1563 sizeof(location));
1564 btrfs_read_locked_inode(inode);
1565 unlock_new_inode(inode);
1566 }
1567 }
1568 return d_splice_alias(inode, dentry);
1569}
1570
Chris Mason39279cc2007-06-12 06:35:45 -04001571static unsigned char btrfs_filetype_table[] = {
1572 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1573};
1574
1575static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1576{
Chris Mason6da6aba2007-12-18 16:15:09 -05001577 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001578 struct btrfs_root *root = BTRFS_I(inode)->root;
1579 struct btrfs_item *item;
1580 struct btrfs_dir_item *di;
1581 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001582 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001583 struct btrfs_path *path;
1584 int ret;
1585 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001586 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001587 int slot;
1588 int advance;
1589 unsigned char d_type;
1590 int over = 0;
1591 u32 di_cur;
1592 u32 di_total;
1593 u32 di_len;
1594 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001595 char tmp_name[32];
1596 char *name_ptr;
1597 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001598
1599 /* FIXME, use a real flag for deciding about the key type */
1600 if (root->fs_info->tree_root == root)
1601 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001602
Chris Mason39544012007-12-12 14:38:19 -05001603 /* special case for "." */
1604 if (filp->f_pos == 0) {
1605 over = filldir(dirent, ".", 1,
1606 1, inode->i_ino,
1607 DT_DIR);
1608 if (over)
1609 return 0;
1610 filp->f_pos = 1;
1611 }
1612
Chris Mason39279cc2007-06-12 06:35:45 -04001613 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001614 path = btrfs_alloc_path();
1615 path->reada = 2;
1616
1617 /* special case for .., just use the back ref */
1618 if (filp->f_pos == 1) {
1619 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1620 key.offset = 0;
1621 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1622 BUG_ON(ret == 0);
1623 leaf = path->nodes[0];
1624 slot = path->slots[0];
1625 nritems = btrfs_header_nritems(leaf);
1626 if (slot >= nritems) {
1627 btrfs_release_path(root, path);
1628 goto read_dir_items;
1629 }
1630 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1631 btrfs_release_path(root, path);
1632 if (found_key.objectid != key.objectid ||
1633 found_key.type != BTRFS_INODE_REF_KEY)
1634 goto read_dir_items;
1635 over = filldir(dirent, "..", 2,
1636 2, found_key.offset, DT_DIR);
1637 if (over)
1638 goto nopos;
1639 filp->f_pos = 2;
1640 }
1641
1642read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001643 btrfs_set_key_type(&key, key_type);
1644 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001645
Chris Mason39279cc2007-06-12 06:35:45 -04001646 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1647 if (ret < 0)
1648 goto err;
1649 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001650 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001651 leaf = path->nodes[0];
1652 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001653 slot = path->slots[0];
1654 if (advance || slot >= nritems) {
1655 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001656 ret = btrfs_next_leaf(root, path);
1657 if (ret)
1658 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001659 leaf = path->nodes[0];
1660 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001661 slot = path->slots[0];
1662 } else {
1663 slot++;
1664 path->slots[0]++;
1665 }
1666 }
1667 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001668 item = btrfs_item_nr(leaf, slot);
1669 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1670
1671 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001672 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001673 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001674 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001675 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001676 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001677
1678 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001679 advance = 1;
1680 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1681 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001682 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001683 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001684 struct btrfs_key location;
1685
1686 name_len = btrfs_dir_name_len(leaf, di);
1687 if (name_len < 32) {
1688 name_ptr = tmp_name;
1689 } else {
1690 name_ptr = kmalloc(name_len, GFP_NOFS);
1691 BUG_ON(!name_ptr);
1692 }
1693 read_extent_buffer(leaf, name_ptr,
1694 (unsigned long)(di + 1), name_len);
1695
1696 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1697 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001698 over = filldir(dirent, name_ptr, name_len,
1699 found_key.offset,
1700 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001701 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001702
1703 if (name_ptr != tmp_name)
1704 kfree(name_ptr);
1705
Chris Mason39279cc2007-06-12 06:35:45 -04001706 if (over)
1707 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001708 di_len = btrfs_dir_name_len(leaf, di) +
1709 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001710 di_cur += di_len;
1711 di = (struct btrfs_dir_item *)((char *)di + di_len);
1712 }
1713 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001714 if (key_type == BTRFS_DIR_INDEX_KEY)
1715 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1716 else
1717 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001718nopos:
1719 ret = 0;
1720err:
Chris Mason39279cc2007-06-12 06:35:45 -04001721 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001722 return ret;
1723}
1724
1725int btrfs_write_inode(struct inode *inode, int wait)
1726{
1727 struct btrfs_root *root = BTRFS_I(inode)->root;
1728 struct btrfs_trans_handle *trans;
1729 int ret = 0;
1730
1731 if (wait) {
Chris Mason39279cc2007-06-12 06:35:45 -04001732 trans = btrfs_start_transaction(root, 1);
1733 btrfs_set_trans_block_group(trans, inode);
1734 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001735 }
1736 return ret;
1737}
1738
1739/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001740 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001741 * inode changes. But, it is most likely to find the inode in cache.
1742 * FIXME, needs more benchmarking...there are no reasons other than performance
1743 * to keep or drop this code.
1744 */
1745void btrfs_dirty_inode(struct inode *inode)
1746{
1747 struct btrfs_root *root = BTRFS_I(inode)->root;
1748 struct btrfs_trans_handle *trans;
1749
Chris Mason39279cc2007-06-12 06:35:45 -04001750 trans = btrfs_start_transaction(root, 1);
1751 btrfs_set_trans_block_group(trans, inode);
1752 btrfs_update_inode(trans, root, inode);
1753 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001754}
1755
1756static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1757 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001758 const char *name, int name_len,
1759 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001760 u64 objectid,
1761 struct btrfs_block_group_cache *group,
1762 int mode)
1763{
1764 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001765 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001766 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001767 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001768 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001769 struct btrfs_inode_ref *ref;
1770 struct btrfs_key key[2];
1771 u32 sizes[2];
1772 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001773 int ret;
1774 int owner;
1775
Chris Mason5f39d392007-10-15 16:14:19 -04001776 path = btrfs_alloc_path();
1777 BUG_ON(!path);
1778
Chris Mason39279cc2007-06-12 06:35:45 -04001779 inode = new_inode(root->fs_info->sb);
1780 if (!inode)
1781 return ERR_PTR(-ENOMEM);
1782
Chris Masond1310b22008-01-24 16:13:08 -05001783 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1784 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001785 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001786 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1787 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04001788 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05001789 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001790 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001791
Chris Mason39279cc2007-06-12 06:35:45 -04001792 if (mode & S_IFDIR)
1793 owner = 0;
1794 else
1795 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001796 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001797 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001798 if (!new_inode_group) {
1799 printk("find_block group failed\n");
1800 new_inode_group = group;
1801 }
1802 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001803 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001804
1805 key[0].objectid = objectid;
1806 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1807 key[0].offset = 0;
1808
1809 key[1].objectid = objectid;
1810 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1811 key[1].offset = ref_objectid;
1812
1813 sizes[0] = sizeof(struct btrfs_inode_item);
1814 sizes[1] = name_len + sizeof(*ref);
1815
1816 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1817 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001818 goto fail;
1819
Chris Mason9c583092008-01-29 15:15:18 -05001820 if (objectid > root->highest_inode)
1821 root->highest_inode = objectid;
1822
Chris Mason39279cc2007-06-12 06:35:45 -04001823 inode->i_uid = current->fsuid;
1824 inode->i_gid = current->fsgid;
1825 inode->i_mode = mode;
1826 inode->i_ino = objectid;
1827 inode->i_blocks = 0;
1828 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001829 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1830 struct btrfs_inode_item);
1831 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001832
1833 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1834 struct btrfs_inode_ref);
1835 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1836 ptr = (unsigned long)(ref + 1);
1837 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1838
Chris Mason5f39d392007-10-15 16:14:19 -04001839 btrfs_mark_buffer_dirty(path->nodes[0]);
1840 btrfs_free_path(path);
1841
Chris Mason39279cc2007-06-12 06:35:45 -04001842 location = &BTRFS_I(inode)->location;
1843 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001844 location->offset = 0;
1845 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1846
Chris Mason39279cc2007-06-12 06:35:45 -04001847 insert_inode_hash(inode);
1848 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001849fail:
1850 btrfs_free_path(path);
1851 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001852}
1853
1854static inline u8 btrfs_inode_type(struct inode *inode)
1855{
1856 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1857}
1858
1859static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001860 struct dentry *dentry, struct inode *inode,
1861 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001862{
1863 int ret;
1864 struct btrfs_key key;
1865 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001866 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001867
Chris Mason39279cc2007-06-12 06:35:45 -04001868 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001869 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1870 key.offset = 0;
1871
1872 ret = btrfs_insert_dir_item(trans, root,
1873 dentry->d_name.name, dentry->d_name.len,
1874 dentry->d_parent->d_inode->i_ino,
1875 &key, btrfs_inode_type(inode));
1876 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001877 if (add_backref) {
1878 ret = btrfs_insert_inode_ref(trans, root,
1879 dentry->d_name.name,
1880 dentry->d_name.len,
1881 inode->i_ino,
1882 dentry->d_parent->d_inode->i_ino);
1883 }
Chris Mason79c44582007-06-25 10:09:33 -04001884 parent_inode = dentry->d_parent->d_inode;
1885 parent_inode->i_size += dentry->d_name.len * 2;
1886 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001887 ret = btrfs_update_inode(trans, root,
1888 dentry->d_parent->d_inode);
1889 }
1890 return ret;
1891}
1892
1893static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001894 struct dentry *dentry, struct inode *inode,
1895 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001896{
Chris Mason9c583092008-01-29 15:15:18 -05001897 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001898 if (!err) {
1899 d_instantiate(dentry, inode);
1900 return 0;
1901 }
1902 if (err > 0)
1903 err = -EEXIST;
1904 return err;
1905}
1906
Josef Bacik618e21d2007-07-11 10:18:17 -04001907static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1908 int mode, dev_t rdev)
1909{
1910 struct btrfs_trans_handle *trans;
1911 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001912 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001913 int err;
1914 int drop_inode = 0;
1915 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001916 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001917
1918 if (!new_valid_dev(rdev))
1919 return -EINVAL;
1920
Chris Mason1832a6d2007-12-21 16:27:21 -05001921 err = btrfs_check_free_space(root, 1, 0);
1922 if (err)
1923 goto fail;
1924
Josef Bacik618e21d2007-07-11 10:18:17 -04001925 trans = btrfs_start_transaction(root, 1);
1926 btrfs_set_trans_block_group(trans, dir);
1927
1928 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1929 if (err) {
1930 err = -ENOSPC;
1931 goto out_unlock;
1932 }
1933
Chris Mason9c583092008-01-29 15:15:18 -05001934 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1935 dentry->d_name.len,
1936 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001937 BTRFS_I(dir)->block_group, mode);
1938 err = PTR_ERR(inode);
1939 if (IS_ERR(inode))
1940 goto out_unlock;
1941
1942 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001943 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001944 if (err)
1945 drop_inode = 1;
1946 else {
1947 inode->i_op = &btrfs_special_inode_operations;
1948 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001949 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001950 }
1951 dir->i_sb->s_dirt = 1;
1952 btrfs_update_inode_block_group(trans, inode);
1953 btrfs_update_inode_block_group(trans, dir);
1954out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001955 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001956 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001957fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001958 if (drop_inode) {
1959 inode_dec_link_count(inode);
1960 iput(inode);
1961 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001962 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001963 return err;
1964}
1965
Chris Mason39279cc2007-06-12 06:35:45 -04001966static int btrfs_create(struct inode *dir, struct dentry *dentry,
1967 int mode, struct nameidata *nd)
1968{
1969 struct btrfs_trans_handle *trans;
1970 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001971 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001972 int err;
1973 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001974 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001975 u64 objectid;
1976
Chris Mason1832a6d2007-12-21 16:27:21 -05001977 err = btrfs_check_free_space(root, 1, 0);
1978 if (err)
1979 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001980 trans = btrfs_start_transaction(root, 1);
1981 btrfs_set_trans_block_group(trans, dir);
1982
1983 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1984 if (err) {
1985 err = -ENOSPC;
1986 goto out_unlock;
1987 }
1988
Chris Mason9c583092008-01-29 15:15:18 -05001989 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1990 dentry->d_name.len,
1991 dentry->d_parent->d_inode->i_ino,
1992 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001993 err = PTR_ERR(inode);
1994 if (IS_ERR(inode))
1995 goto out_unlock;
1996
1997 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001998 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001999 if (err)
2000 drop_inode = 1;
2001 else {
2002 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002003 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002004 inode->i_fop = &btrfs_file_operations;
2005 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002006 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2007 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002008 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002009 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2010 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002011 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002012 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002013 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002014 }
2015 dir->i_sb->s_dirt = 1;
2016 btrfs_update_inode_block_group(trans, inode);
2017 btrfs_update_inode_block_group(trans, dir);
2018out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002019 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002020 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002021fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002022 if (drop_inode) {
2023 inode_dec_link_count(inode);
2024 iput(inode);
2025 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002026 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002027 return err;
2028}
2029
2030static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2031 struct dentry *dentry)
2032{
2033 struct btrfs_trans_handle *trans;
2034 struct btrfs_root *root = BTRFS_I(dir)->root;
2035 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002036 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002037 int err;
2038 int drop_inode = 0;
2039
2040 if (inode->i_nlink == 0)
2041 return -ENOENT;
2042
Chris Mason6da6aba2007-12-18 16:15:09 -05002043#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2044 inode->i_nlink++;
2045#else
Chris Mason39279cc2007-06-12 06:35:45 -04002046 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002047#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002048 err = btrfs_check_free_space(root, 1, 0);
2049 if (err)
2050 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002051 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002052
Chris Mason39279cc2007-06-12 06:35:45 -04002053 btrfs_set_trans_block_group(trans, dir);
2054 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002055 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002056
Chris Mason39279cc2007-06-12 06:35:45 -04002057 if (err)
2058 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002059
Chris Mason39279cc2007-06-12 06:35:45 -04002060 dir->i_sb->s_dirt = 1;
2061 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002062 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002063
Chris Mason54aa1f42007-06-22 14:16:25 -04002064 if (err)
2065 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002066
Chris Masond3c2fdc2007-09-17 10:58:06 -04002067 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002068 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002069fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002070 if (drop_inode) {
2071 inode_dec_link_count(inode);
2072 iput(inode);
2073 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002074 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002075 return err;
2076}
2077
Chris Mason39279cc2007-06-12 06:35:45 -04002078static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2079{
Chris Masonb9d86662008-05-02 16:13:49 -04002080 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002081 struct btrfs_trans_handle *trans;
2082 struct btrfs_root *root = BTRFS_I(dir)->root;
2083 int err = 0;
2084 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002085 u64 objectid = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002086 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002087
Chris Mason1832a6d2007-12-21 16:27:21 -05002088 err = btrfs_check_free_space(root, 1, 0);
2089 if (err)
2090 goto out_unlock;
2091
Chris Mason39279cc2007-06-12 06:35:45 -04002092 trans = btrfs_start_transaction(root, 1);
2093 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002094
Chris Mason39279cc2007-06-12 06:35:45 -04002095 if (IS_ERR(trans)) {
2096 err = PTR_ERR(trans);
2097 goto out_unlock;
2098 }
2099
2100 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2101 if (err) {
2102 err = -ENOSPC;
2103 goto out_unlock;
2104 }
2105
Chris Mason9c583092008-01-29 15:15:18 -05002106 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2107 dentry->d_name.len,
2108 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002109 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2110 if (IS_ERR(inode)) {
2111 err = PTR_ERR(inode);
2112 goto out_fail;
2113 }
Chris Mason5f39d392007-10-15 16:14:19 -04002114
Chris Mason39279cc2007-06-12 06:35:45 -04002115 drop_on_err = 1;
2116 inode->i_op = &btrfs_dir_inode_operations;
2117 inode->i_fop = &btrfs_dir_file_operations;
2118 btrfs_set_trans_block_group(trans, inode);
2119
Chris Mason39544012007-12-12 14:38:19 -05002120 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002121 err = btrfs_update_inode(trans, root, inode);
2122 if (err)
2123 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002124
Chris Mason9c583092008-01-29 15:15:18 -05002125 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002126 if (err)
2127 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002128
Chris Mason39279cc2007-06-12 06:35:45 -04002129 d_instantiate(dentry, inode);
2130 drop_on_err = 0;
2131 dir->i_sb->s_dirt = 1;
2132 btrfs_update_inode_block_group(trans, inode);
2133 btrfs_update_inode_block_group(trans, dir);
2134
2135out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002136 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002137 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002138
Chris Mason39279cc2007-06-12 06:35:45 -04002139out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002140 if (drop_on_err)
2141 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002142 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002143 return err;
2144}
2145
Chris Mason3b951512008-04-17 11:29:12 -04002146static int merge_extent_mapping(struct extent_map_tree *em_tree,
2147 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002148 struct extent_map *em,
2149 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002150{
2151 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002152
Chris Masone6dcd2d2008-07-17 12:53:50 -04002153 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2154 start_diff = map_start - em->start;
2155 em->start = map_start;
2156 em->len = map_len;
2157 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2158 em->block_start += start_diff;
2159 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002160}
2161
Chris Masona52d9a82007-08-27 16:49:44 -04002162struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002163 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002164 int create)
2165{
2166 int ret;
2167 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002168 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002169 u64 extent_start = 0;
2170 u64 extent_end = 0;
2171 u64 objectid = inode->i_ino;
2172 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002173 struct btrfs_path *path;
2174 struct btrfs_root *root = BTRFS_I(inode)->root;
2175 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002176 struct extent_buffer *leaf;
2177 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002178 struct extent_map *em = NULL;
2179 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002180 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002181 struct btrfs_trans_handle *trans = NULL;
2182
2183 path = btrfs_alloc_path();
2184 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002185
2186again:
Chris Masond1310b22008-01-24 16:13:08 -05002187 spin_lock(&em_tree->lock);
2188 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002189 if (em)
2190 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002191 spin_unlock(&em_tree->lock);
2192
Chris Masona52d9a82007-08-27 16:49:44 -04002193 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002194 if (em->start > start || em->start + em->len <= start)
2195 free_extent_map(em);
2196 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002197 free_extent_map(em);
2198 else
2199 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002200 }
Chris Masond1310b22008-01-24 16:13:08 -05002201 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002202 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002203 err = -ENOMEM;
2204 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002205 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002206 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002207 em->start = EXTENT_MAP_HOLE;
2208 em->len = (u64)-1;
Chris Mason179e29e2007-11-01 11:28:41 -04002209 ret = btrfs_lookup_file_extent(trans, root, path,
2210 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002211 if (ret < 0) {
2212 err = ret;
2213 goto out;
2214 }
2215
2216 if (ret != 0) {
2217 if (path->slots[0] == 0)
2218 goto not_found;
2219 path->slots[0]--;
2220 }
2221
Chris Mason5f39d392007-10-15 16:14:19 -04002222 leaf = path->nodes[0];
2223 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002224 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002225 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2227 found_type = btrfs_key_type(&found_key);
2228 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002229 found_type != BTRFS_EXTENT_DATA_KEY) {
2230 goto not_found;
2231 }
2232
Chris Mason5f39d392007-10-15 16:14:19 -04002233 found_type = btrfs_file_extent_type(leaf, item);
2234 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002235 if (found_type == BTRFS_FILE_EXTENT_REG) {
2236 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002237 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002238 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002239 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002240 em->start = start;
2241 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002242 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002243 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002244 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002245 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002246 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002247 }
2248 goto not_found_em;
2249 }
Chris Masondb945352007-10-15 16:15:53 -04002250 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2251 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002252 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002253 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002254 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002255 goto insert;
2256 }
Chris Masondb945352007-10-15 16:15:53 -04002257 bytenr += btrfs_file_extent_offset(leaf, item);
2258 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002259 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002260 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002261 goto insert;
2262 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002263 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002264 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002265 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002266 size_t size;
2267 size_t extent_offset;
2268 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002269
Chris Mason5f39d392007-10-15 16:14:19 -04002270 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2271 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002272 extent_end = (extent_start + size + root->sectorsize - 1) &
2273 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002274 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002275 em->start = start;
2276 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002277 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002278 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002279 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002280 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002281 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002282 }
2283 goto not_found_em;
2284 }
2285 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002286
2287 if (!page) {
2288 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002289 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002290 goto out;
2291 }
2292
Chris Mason70dec802008-01-29 09:59:12 -05002293 page_start = page_offset(page) + pg_offset;
2294 extent_offset = page_start - extent_start;
2295 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002296 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002297 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002298 em->len = (copy_size + root->sectorsize - 1) &
2299 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002300 map = kmap(page);
2301 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002302 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002303 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002304 copy_size);
2305 flush_dcache_page(page);
2306 } else if (create && PageUptodate(page)) {
2307 if (!trans) {
2308 kunmap(page);
2309 free_extent_map(em);
2310 em = NULL;
2311 btrfs_release_path(root, path);
2312 trans = btrfs_start_transaction(root, 1);
2313 goto again;
2314 }
Chris Mason70dec802008-01-29 09:59:12 -05002315 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002316 copy_size);
2317 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002318 }
Chris Masona52d9a82007-08-27 16:49:44 -04002319 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002320 set_extent_uptodate(io_tree, em->start,
2321 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002322 goto insert;
2323 } else {
2324 printk("unkknown found_type %d\n", found_type);
2325 WARN_ON(1);
2326 }
2327not_found:
2328 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002329 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002330not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002331 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002332insert:
2333 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002334 if (em->start > start || extent_map_end(em) <= start) {
2335 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002336 err = -EIO;
2337 goto out;
2338 }
Chris Masond1310b22008-01-24 16:13:08 -05002339
2340 err = 0;
2341 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002342 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002343 /* it is possible that someone inserted the extent into the tree
2344 * while we had the lock dropped. It is also possible that
2345 * an overlapping map exists in the tree
2346 */
Chris Masona52d9a82007-08-27 16:49:44 -04002347 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002348 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002349
2350 ret = 0;
2351
Chris Mason3b951512008-04-17 11:29:12 -04002352 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002353 if (existing && (existing->start > start ||
2354 existing->start + existing->len <= start)) {
2355 free_extent_map(existing);
2356 existing = NULL;
2357 }
Chris Mason3b951512008-04-17 11:29:12 -04002358 if (!existing) {
2359 existing = lookup_extent_mapping(em_tree, em->start,
2360 em->len);
2361 if (existing) {
2362 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002363 em, start,
2364 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002365 free_extent_map(existing);
2366 if (err) {
2367 free_extent_map(em);
2368 em = NULL;
2369 }
2370 } else {
2371 err = -EIO;
2372 printk("failing to insert %Lu %Lu\n",
2373 start, len);
2374 free_extent_map(em);
2375 em = NULL;
2376 }
2377 } else {
2378 free_extent_map(em);
2379 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002380 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002381 }
Chris Masona52d9a82007-08-27 16:49:44 -04002382 }
Chris Masond1310b22008-01-24 16:13:08 -05002383 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002384out:
2385 btrfs_free_path(path);
2386 if (trans) {
2387 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002388 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002389 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002390 }
Chris Masona52d9a82007-08-27 16:49:44 -04002391 }
Chris Masona52d9a82007-08-27 16:49:44 -04002392 if (err) {
2393 free_extent_map(em);
2394 WARN_ON(1);
2395 return ERR_PTR(err);
2396 }
2397 return em;
2398}
2399
Chris Masone1c4b742008-04-22 13:26:46 -04002400#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002401static int btrfs_get_block(struct inode *inode, sector_t iblock,
2402 struct buffer_head *bh_result, int create)
2403{
2404 struct extent_map *em;
2405 u64 start = (u64)iblock << inode->i_blkbits;
2406 struct btrfs_multi_bio *multi = NULL;
2407 struct btrfs_root *root = BTRFS_I(inode)->root;
2408 u64 len;
2409 u64 logical;
2410 u64 map_length;
2411 int ret = 0;
2412
2413 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2414
2415 if (!em || IS_ERR(em))
2416 goto out;
2417
Chris Masone1c4b742008-04-22 13:26:46 -04002418 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002419 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002420 }
Chris Mason16432982008-04-10 10:23:21 -04002421
2422 if (em->block_start == EXTENT_MAP_INLINE) {
2423 ret = -EINVAL;
2424 goto out;
2425 }
2426
Chris Mason16432982008-04-10 10:23:21 -04002427 len = em->start + em->len - start;
2428 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2429
Chris Masone1c4b742008-04-22 13:26:46 -04002430 if (em->block_start == EXTENT_MAP_HOLE ||
2431 em->block_start == EXTENT_MAP_DELALLOC) {
2432 bh_result->b_size = len;
2433 goto out;
2434 }
2435
Chris Mason16432982008-04-10 10:23:21 -04002436 logical = start - em->start;
2437 logical = em->block_start + logical;
2438
2439 map_length = len;
2440 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2441 logical, &map_length, &multi, 0);
2442 BUG_ON(ret);
2443 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2444 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002445
Chris Mason16432982008-04-10 10:23:21 -04002446 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2447 set_buffer_mapped(bh_result);
2448 kfree(multi);
2449out:
2450 free_extent_map(em);
2451 return ret;
2452}
Chris Masone1c4b742008-04-22 13:26:46 -04002453#endif
Chris Mason16432982008-04-10 10:23:21 -04002454
2455static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2456 const struct iovec *iov, loff_t offset,
2457 unsigned long nr_segs)
2458{
Chris Masone1c4b742008-04-22 13:26:46 -04002459 return -EINVAL;
2460#if 0
Chris Mason16432982008-04-10 10:23:21 -04002461 struct file *file = iocb->ki_filp;
2462 struct inode *inode = file->f_mapping->host;
2463
2464 if (rw == WRITE)
2465 return -EINVAL;
2466
2467 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2468 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002469#endif
Chris Mason16432982008-04-10 10:23:21 -04002470}
2471
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002472static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002473{
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002474 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002475}
2476
Chris Mason9ebefb182007-06-15 13:50:00 -04002477int btrfs_readpage(struct file *file, struct page *page)
2478{
Chris Masond1310b22008-01-24 16:13:08 -05002479 struct extent_io_tree *tree;
2480 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002481 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002482}
Chris Mason1832a6d2007-12-21 16:27:21 -05002483
Chris Mason39279cc2007-06-12 06:35:45 -04002484static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2485{
Chris Masond1310b22008-01-24 16:13:08 -05002486 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002487
2488
2489 if (current->flags & PF_MEMALLOC) {
2490 redirty_page_for_writepage(wbc, page);
2491 unlock_page(page);
2492 return 0;
2493 }
Chris Masond1310b22008-01-24 16:13:08 -05002494 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002495 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2496}
Chris Mason39279cc2007-06-12 06:35:45 -04002497
Chris Masonb293f022007-11-01 19:45:34 -04002498static int btrfs_writepages(struct address_space *mapping,
2499 struct writeback_control *wbc)
2500{
Chris Masond1310b22008-01-24 16:13:08 -05002501 struct extent_io_tree *tree;
2502 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002503 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2504}
2505
Chris Mason3ab2fb52007-11-08 10:59:22 -05002506static int
2507btrfs_readpages(struct file *file, struct address_space *mapping,
2508 struct list_head *pages, unsigned nr_pages)
2509{
Chris Masond1310b22008-01-24 16:13:08 -05002510 struct extent_io_tree *tree;
2511 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002512 return extent_readpages(tree, mapping, pages, nr_pages,
2513 btrfs_get_extent);
2514}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002515static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002516{
Chris Masond1310b22008-01-24 16:13:08 -05002517 struct extent_io_tree *tree;
2518 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002519 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002520
Chris Masond1310b22008-01-24 16:13:08 -05002521 tree = &BTRFS_I(page->mapping->host)->io_tree;
2522 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002523 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002524 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002525 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002526 ClearPagePrivate(page);
2527 set_page_private(page, 0);
2528 page_cache_release(page);
2529 }
2530 return ret;
2531}
Chris Mason39279cc2007-06-12 06:35:45 -04002532
Chris Masone6dcd2d2008-07-17 12:53:50 -04002533static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2534{
2535 struct btrfs_ordered_extent *ordered;
2536
2537 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2538 page_offset(page));
2539 if (ordered) {
2540 btrfs_put_ordered_extent(ordered);
2541 return 0;
2542 }
2543 return __btrfs_releasepage(page, gfp_flags);
2544}
2545
Chris Masona52d9a82007-08-27 16:49:44 -04002546static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2547{
Chris Masond1310b22008-01-24 16:13:08 -05002548 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002549 struct btrfs_ordered_extent *ordered;
2550 u64 page_start = page_offset(page);
2551 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002552
Chris Masone6dcd2d2008-07-17 12:53:50 -04002553 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002554 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002555 if (offset) {
2556 btrfs_releasepage(page, GFP_NOFS);
2557 return;
2558 }
2559
2560 lock_extent(tree, page_start, page_end, GFP_NOFS);
2561 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2562 page_offset(page));
2563 if (ordered) {
2564 clear_extent_bit(tree, page_start, page_end,
2565 EXTENT_DIRTY | EXTENT_DELALLOC |
2566 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2567 btrfs_writepage_end_io_hook(page, page_start,
2568 page_end, NULL, 1);
2569 btrfs_put_ordered_extent(ordered);
2570 lock_extent(tree, page_start, page_end, GFP_NOFS);
2571 }
2572 clear_extent_bit(tree, page_start, page_end,
2573 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2574 EXTENT_ORDERED,
2575 1, 1, GFP_NOFS);
2576 __btrfs_releasepage(page, GFP_NOFS);
2577
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002578 if (PagePrivate(page)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002579 invalidate_extent_lru(tree, page_offset(page),
2580 PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002581 ClearPagePrivate(page);
2582 set_page_private(page, 0);
2583 page_cache_release(page);
2584 }
Chris Mason39279cc2007-06-12 06:35:45 -04002585}
2586
Chris Mason9ebefb182007-06-15 13:50:00 -04002587/*
2588 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2589 * called from a page fault handler when a page is first dirtied. Hence we must
2590 * be careful to check for EOF conditions here. We set the page up correctly
2591 * for a written page which means we get ENOSPC checking when writing into
2592 * holes and correct delalloc and unwritten extent mapping on filesystems that
2593 * support these features.
2594 *
2595 * We are not allowed to take the i_mutex here so we have to play games to
2596 * protect against truncate races as the page could now be beyond EOF. Because
2597 * vmtruncate() writes the inode size before removing pages, once we have the
2598 * page lock we can determine safely if the page is beyond EOF. If it is not
2599 * beyond EOF, then the page is guaranteed safe against truncation until we
2600 * unlock the page.
2601 */
2602int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2603{
Chris Mason6da6aba2007-12-18 16:15:09 -05002604 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002605 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2607 struct btrfs_ordered_extent *ordered;
2608 char *kaddr;
2609 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002610 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002611 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002612 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002614
Chris Mason1832a6d2007-12-21 16:27:21 -05002615 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002616 if (ret)
2617 goto out;
2618
2619 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002620again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002621 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002622 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002623 page_start = page_offset(page);
2624 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002625
Chris Mason9ebefb182007-06-15 13:50:00 -04002626 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002627 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002628 /* page got truncated out from underneath us */
2629 goto out_unlock;
2630 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002631 wait_on_page_writeback(page);
2632
2633 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2634 set_page_extent_mapped(page);
2635
2636 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2637 if (ordered) {
2638 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2639 unlock_page(page);
2640 btrfs_wait_ordered_extent(inode, ordered);
2641 btrfs_put_ordered_extent(ordered);
2642 goto again;
2643 }
2644
2645 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2646 page_end, GFP_NOFS);
2647 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002648
2649 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002650 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002651 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002652 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002653 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002654
Chris Masone6dcd2d2008-07-17 12:53:50 -04002655 if (zero_start != PAGE_CACHE_SIZE) {
2656 kaddr = kmap(page);
2657 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2658 flush_dcache_page(page);
2659 kunmap(page);
2660 }
2661 set_page_dirty(page);
2662 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002663
2664out_unlock:
2665 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002666out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002667 return ret;
2668}
2669
Chris Mason39279cc2007-06-12 06:35:45 -04002670static void btrfs_truncate(struct inode *inode)
2671{
2672 struct btrfs_root *root = BTRFS_I(inode)->root;
2673 int ret;
2674 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002675 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002676
2677 if (!S_ISREG(inode->i_mode))
2678 return;
2679 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2680 return;
2681
2682 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2683
Chris Mason39279cc2007-06-12 06:35:45 -04002684 trans = btrfs_start_transaction(root, 1);
2685 btrfs_set_trans_block_group(trans, inode);
2686
2687 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002688 ret = btrfs_truncate_in_trans(trans, root, inode,
2689 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002690 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002691 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002692
Chris Mason89ce8a62008-06-25 16:01:31 -04002693 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002694 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002695 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002696}
2697
Sven Wegener3b963622008-06-09 21:57:42 -04002698/*
2699 * Invalidate a single dcache entry at the root of the filesystem.
2700 * Needed after creation of snapshot or subvolume.
2701 */
2702void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2703 int namelen)
2704{
2705 struct dentry *alias, *entry;
2706 struct qstr qstr;
2707
2708 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2709 if (alias) {
2710 qstr.name = name;
2711 qstr.len = namelen;
2712 /* change me if btrfs ever gets a d_hash operation */
2713 qstr.hash = full_name_hash(qstr.name, qstr.len);
2714 entry = d_lookup(alias, &qstr);
2715 dput(alias);
2716 if (entry) {
2717 d_invalidate(entry);
2718 dput(entry);
2719 }
2720 }
2721}
2722
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002723int btrfs_create_subvol_root(struct btrfs_root *new_root,
2724 struct btrfs_trans_handle *trans, u64 new_dirid,
2725 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002726{
Chris Mason39279cc2007-06-12 06:35:45 -04002727 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002728 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002729
Chris Mason9c583092008-01-29 15:15:18 -05002730 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002731 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002732 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002733 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002734 inode->i_op = &btrfs_dir_inode_operations;
2735 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002736 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002737
Chris Mason39544012007-12-12 14:38:19 -05002738 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2739 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002740 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002741 inode->i_size = 0;
Sven Wegener3b963622008-06-09 21:57:42 -04002742
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002743 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002744}
2745
Chris Masonedbd8d42007-12-21 16:27:24 -05002746unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002747 struct file_ra_state *ra, struct file *file,
2748 pgoff_t offset, pgoff_t last_index)
2749{
Chris Mason8e7bf942008-04-28 09:02:36 -04002750 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002751
2752#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002753 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2754 return offset;
2755#else
Chris Mason86479a02007-09-10 19:58:16 -04002756 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2757 return offset + req_size;
2758#endif
2759}
2760
Chris Mason39279cc2007-06-12 06:35:45 -04002761struct inode *btrfs_alloc_inode(struct super_block *sb)
2762{
2763 struct btrfs_inode *ei;
2764
2765 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2766 if (!ei)
2767 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002768 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002770 return &ei->vfs_inode;
2771}
2772
2773void btrfs_destroy_inode(struct inode *inode)
2774{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002775 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04002776 WARN_ON(!list_empty(&inode->i_dentry));
2777 WARN_ON(inode->i_data.nrpages);
2778
Chris Masone6dcd2d2008-07-17 12:53:50 -04002779 while(1) {
2780 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2781 if (!ordered)
2782 break;
2783 else {
2784 printk("found ordered extent %Lu %Lu\n",
2785 ordered->file_offset, ordered->len);
2786 btrfs_remove_ordered_extent(inode, ordered);
2787 btrfs_put_ordered_extent(ordered);
2788 btrfs_put_ordered_extent(ordered);
2789 }
2790 }
Chris Mason8c416c92008-01-14 15:10:26 -05002791 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002792 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2793}
2794
Chris Mason44ec0b72007-10-29 10:55:05 -04002795#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2796static void init_once(struct kmem_cache * cachep, void *foo)
2797#else
Chris Mason39279cc2007-06-12 06:35:45 -04002798static void init_once(void * foo, struct kmem_cache * cachep,
2799 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002800#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002801{
2802 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2803
2804 inode_init_once(&ei->vfs_inode);
2805}
2806
2807void btrfs_destroy_cachep(void)
2808{
2809 if (btrfs_inode_cachep)
2810 kmem_cache_destroy(btrfs_inode_cachep);
2811 if (btrfs_trans_handle_cachep)
2812 kmem_cache_destroy(btrfs_trans_handle_cachep);
2813 if (btrfs_transaction_cachep)
2814 kmem_cache_destroy(btrfs_transaction_cachep);
2815 if (btrfs_bit_radix_cachep)
2816 kmem_cache_destroy(btrfs_bit_radix_cachep);
2817 if (btrfs_path_cachep)
2818 kmem_cache_destroy(btrfs_path_cachep);
2819}
2820
Chris Mason86479a02007-09-10 19:58:16 -04002821struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002822 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002823#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2824 void (*ctor)(struct kmem_cache *, void *)
2825#else
Chris Mason92fee662007-07-25 12:31:35 -04002826 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002827 unsigned long)
2828#endif
2829 )
Chris Mason92fee662007-07-25 12:31:35 -04002830{
2831 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2832 SLAB_MEM_SPREAD | extra_flags), ctor
2833#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2834 ,NULL
2835#endif
2836 );
2837}
2838
Chris Mason39279cc2007-06-12 06:35:45 -04002839int btrfs_init_cachep(void)
2840{
Chris Mason86479a02007-09-10 19:58:16 -04002841 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002842 sizeof(struct btrfs_inode),
2843 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002844 if (!btrfs_inode_cachep)
2845 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002846 btrfs_trans_handle_cachep =
2847 btrfs_cache_create("btrfs_trans_handle_cache",
2848 sizeof(struct btrfs_trans_handle),
2849 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002850 if (!btrfs_trans_handle_cachep)
2851 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002852 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002853 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002854 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002855 if (!btrfs_transaction_cachep)
2856 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002857 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002858 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002859 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002860 if (!btrfs_path_cachep)
2861 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002862 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002863 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002864 if (!btrfs_bit_radix_cachep)
2865 goto fail;
2866 return 0;
2867fail:
2868 btrfs_destroy_cachep();
2869 return -ENOMEM;
2870}
2871
2872static int btrfs_getattr(struct vfsmount *mnt,
2873 struct dentry *dentry, struct kstat *stat)
2874{
2875 struct inode *inode = dentry->d_inode;
2876 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002877 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002878 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002879 return 0;
2880}
2881
2882static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2883 struct inode * new_dir,struct dentry *new_dentry)
2884{
2885 struct btrfs_trans_handle *trans;
2886 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2887 struct inode *new_inode = new_dentry->d_inode;
2888 struct inode *old_inode = old_dentry->d_inode;
2889 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002890 int ret;
2891
2892 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2893 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2894 return -ENOTEMPTY;
2895 }
Chris Mason5f39d392007-10-15 16:14:19 -04002896
Chris Mason1832a6d2007-12-21 16:27:21 -05002897 ret = btrfs_check_free_space(root, 1, 0);
2898 if (ret)
2899 goto out_unlock;
2900
Chris Mason39279cc2007-06-12 06:35:45 -04002901 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002902
Chris Mason39279cc2007-06-12 06:35:45 -04002903 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002904
2905 old_dentry->d_inode->i_nlink++;
2906 old_dir->i_ctime = old_dir->i_mtime = ctime;
2907 new_dir->i_ctime = new_dir->i_mtime = ctime;
2908 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002909
Chris Mason39279cc2007-06-12 06:35:45 -04002910 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2911 if (ret)
2912 goto out_fail;
2913
2914 if (new_inode) {
2915 new_inode->i_ctime = CURRENT_TIME;
2916 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2917 if (ret)
2918 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002919 }
Chris Mason9c583092008-01-29 15:15:18 -05002920 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002921 if (ret)
2922 goto out_fail;
2923
2924out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002925 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002926out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002927 return ret;
2928}
2929
2930static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2931 const char *symname)
2932{
2933 struct btrfs_trans_handle *trans;
2934 struct btrfs_root *root = BTRFS_I(dir)->root;
2935 struct btrfs_path *path;
2936 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002937 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002938 int err;
2939 int drop_inode = 0;
2940 u64 objectid;
2941 int name_len;
2942 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002943 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002944 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002945 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002946 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002947
2948 name_len = strlen(symname) + 1;
2949 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2950 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002951
Chris Mason1832a6d2007-12-21 16:27:21 -05002952 err = btrfs_check_free_space(root, 1, 0);
2953 if (err)
2954 goto out_fail;
2955
Chris Mason39279cc2007-06-12 06:35:45 -04002956 trans = btrfs_start_transaction(root, 1);
2957 btrfs_set_trans_block_group(trans, dir);
2958
2959 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2960 if (err) {
2961 err = -ENOSPC;
2962 goto out_unlock;
2963 }
2964
Chris Mason9c583092008-01-29 15:15:18 -05002965 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2966 dentry->d_name.len,
2967 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002968 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2969 err = PTR_ERR(inode);
2970 if (IS_ERR(inode))
2971 goto out_unlock;
2972
2973 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002974 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002975 if (err)
2976 drop_inode = 1;
2977 else {
2978 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002979 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002980 inode->i_fop = &btrfs_file_operations;
2981 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002982 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2983 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002984 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002985 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2986 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002987 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002988 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002989 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002990 }
2991 dir->i_sb->s_dirt = 1;
2992 btrfs_update_inode_block_group(trans, inode);
2993 btrfs_update_inode_block_group(trans, dir);
2994 if (drop_inode)
2995 goto out_unlock;
2996
2997 path = btrfs_alloc_path();
2998 BUG_ON(!path);
2999 key.objectid = inode->i_ino;
3000 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003001 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3002 datasize = btrfs_file_extent_calc_inline_size(name_len);
3003 err = btrfs_insert_empty_item(trans, root, path, &key,
3004 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003005 if (err) {
3006 drop_inode = 1;
3007 goto out_unlock;
3008 }
Chris Mason5f39d392007-10-15 16:14:19 -04003009 leaf = path->nodes[0];
3010 ei = btrfs_item_ptr(leaf, path->slots[0],
3011 struct btrfs_file_extent_item);
3012 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3013 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003014 BTRFS_FILE_EXTENT_INLINE);
3015 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003016 write_extent_buffer(leaf, symname, ptr, name_len);
3017 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003018 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003019
Chris Mason39279cc2007-06-12 06:35:45 -04003020 inode->i_op = &btrfs_symlink_inode_operations;
3021 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003022 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003023 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003024 err = btrfs_update_inode(trans, root, inode);
3025 if (err)
3026 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003027
3028out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003029 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003030 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003031out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003032 if (drop_inode) {
3033 inode_dec_link_count(inode);
3034 iput(inode);
3035 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003036 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003037 return err;
3038}
Chris Mason16432982008-04-10 10:23:21 -04003039
Chris Masone6dcd2d2008-07-17 12:53:50 -04003040static int btrfs_set_page_dirty(struct page *page)
3041{
3042 struct inode *inode = page->mapping->host;
3043 u64 page_start = page_offset(page);
3044 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3045
3046 if (!test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
3047 EXTENT_DELALLOC, 0)) {
3048printk("inode %lu page %Lu not delalloc\n", inode->i_ino, page_offset(page));
3049WARN_ON(1);
3050 }
3051 return __set_page_dirty_nobuffers(page);
3052}
3053
Yanfdebe2b2008-01-14 13:26:08 -05003054static int btrfs_permission(struct inode *inode, int mask,
3055 struct nameidata *nd)
3056{
3057 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3058 return -EACCES;
3059 return generic_permission(inode, mask, NULL);
3060}
Chris Mason39279cc2007-06-12 06:35:45 -04003061
3062static struct inode_operations btrfs_dir_inode_operations = {
3063 .lookup = btrfs_lookup,
3064 .create = btrfs_create,
3065 .unlink = btrfs_unlink,
3066 .link = btrfs_link,
3067 .mkdir = btrfs_mkdir,
3068 .rmdir = btrfs_rmdir,
3069 .rename = btrfs_rename,
3070 .symlink = btrfs_symlink,
3071 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003072 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003073 .setxattr = generic_setxattr,
3074 .getxattr = generic_getxattr,
3075 .listxattr = btrfs_listxattr,
3076 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003077 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003078};
Chris Mason39279cc2007-06-12 06:35:45 -04003079static struct inode_operations btrfs_dir_ro_inode_operations = {
3080 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003081 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003082};
Chris Mason39279cc2007-06-12 06:35:45 -04003083static struct file_operations btrfs_dir_file_operations = {
3084 .llseek = generic_file_llseek,
3085 .read = generic_read_dir,
3086 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003087 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003088#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003089 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003090#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003091 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003092};
3093
Chris Masond1310b22008-01-24 16:13:08 -05003094static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003095 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003096 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003097 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003098 .readpage_io_hook = btrfs_readpage_io_hook,
3099 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003100 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003101 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003102 .set_bit_hook = btrfs_set_bit_hook,
3103 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003104};
3105
Chris Mason39279cc2007-06-12 06:35:45 -04003106static struct address_space_operations btrfs_aops = {
3107 .readpage = btrfs_readpage,
3108 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003109 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003110 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003111 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003112 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003113 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003114 .invalidatepage = btrfs_invalidatepage,
3115 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003116 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003117};
3118
3119static struct address_space_operations btrfs_symlink_aops = {
3120 .readpage = btrfs_readpage,
3121 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003122 .invalidatepage = btrfs_invalidatepage,
3123 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003124};
3125
3126static struct inode_operations btrfs_file_inode_operations = {
3127 .truncate = btrfs_truncate,
3128 .getattr = btrfs_getattr,
3129 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003130 .setxattr = generic_setxattr,
3131 .getxattr = generic_getxattr,
3132 .listxattr = btrfs_listxattr,
3133 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003134 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003135};
Josef Bacik618e21d2007-07-11 10:18:17 -04003136static struct inode_operations btrfs_special_inode_operations = {
3137 .getattr = btrfs_getattr,
3138 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003139 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003140};
Chris Mason39279cc2007-06-12 06:35:45 -04003141static struct inode_operations btrfs_symlink_inode_operations = {
3142 .readlink = generic_readlink,
3143 .follow_link = page_follow_link_light,
3144 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003145 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003146};