blob: 61bd8953a683b5edf048bc64c1a027f0cd6a335c [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 Mason39279cc2007-06-12 06:35:45 -040046
47struct btrfs_iget_args {
48 u64 ino;
49 struct btrfs_root *root;
50};
51
52static struct inode_operations btrfs_dir_inode_operations;
53static struct inode_operations btrfs_symlink_inode_operations;
54static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040055static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040056static struct inode_operations btrfs_file_inode_operations;
57static struct address_space_operations btrfs_aops;
58static struct address_space_operations btrfs_symlink_aops;
59static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050060static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040061
62static struct kmem_cache *btrfs_inode_cachep;
63struct kmem_cache *btrfs_trans_handle_cachep;
64struct kmem_cache *btrfs_transaction_cachep;
65struct kmem_cache *btrfs_bit_radix_cachep;
66struct kmem_cache *btrfs_path_cachep;
67
68#define S_SHIFT 12
69static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
70 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
71 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
72 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
73 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
74 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
75 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
76 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
77};
78
Chris Mason1832a6d2007-12-21 16:27:21 -050079int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
80 int for_del)
81{
82 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
83 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
84 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040085 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050086 int ret = 0;
87
88 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050089 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 else
Chris Masonf9ef6602008-01-03 09:22:38 -050091 thresh = total * 85;
92
93 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050094
Chris Masonbcbfce82008-04-22 13:26:47 -040095 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050096 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
97 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -040098 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050099 return ret;
100}
101
Chris Masonbe20aa92007-12-17 20:14:01 -0500102static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400103{
104 struct btrfs_root *root = BTRFS_I(inode)->root;
105 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400106 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400107 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500108 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400109 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500110 u64 orig_start = start;
111 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 struct btrfs_key ins;
113 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400114
Chris Masonb888db22007-08-27 16:49:44 -0400115 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400116 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 btrfs_set_trans_block_group(trans, inode);
Chris Mason925baed2008-06-25 16:01:30 -0400118 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500119
Chris Masondb945352007-10-15 16:15:53 -0400120 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500121 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400122 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400123 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500124 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400125
Chris Mason179e29e2007-11-01 11:28:41 -0400126 if (alloc_hint == EXTENT_MAP_INLINE)
127 goto out;
128
Chris Mason3b951512008-04-17 11:29:12 -0400129 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
130
Chris Masonc59f8952007-12-17 20:14:04 -0500131 while(num_bytes > 0) {
132 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
133 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
Chris Mason98d20f62008-04-14 09:46:10 -0400134 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500135 root->root_key.objectid,
136 trans->transid,
137 inode->i_ino, start, 0,
138 alloc_hint, (u64)-1, &ins, 1);
139 if (ret) {
140 WARN_ON(1);
141 goto out;
142 }
Chris Mason98d20f62008-04-14 09:46:10 -0400143 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500144 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
145 start, ins.objectid, ins.offset,
Sage Weilf2eb0a22008-05-02 14:43:14 -0400146 ins.offset, 0);
Chris Mason90692182008-02-08 13:49:28 -0500147 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500148 btrfs_check_file(root, inode);
Chris Mason3b951512008-04-17 11:29:12 -0400149 if (num_bytes < cur_alloc_size) {
150 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
151 cur_alloc_size);
152 break;
153 }
Chris Masonc59f8952007-12-17 20:14:04 -0500154 num_bytes -= cur_alloc_size;
155 alloc_hint = ins.objectid + ins.offset;
156 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400157 }
Chris Masond1310b22008-01-24 16:13:08 -0500158 btrfs_drop_extent_cache(inode, orig_start,
159 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500160 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500161 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400162out:
Chris Mason925baed2008-06-25 16:01:30 -0400163 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonb888db22007-08-27 16:49:44 -0400164 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500165 return ret;
166}
167
168static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
169{
170 u64 extent_start;
171 u64 extent_end;
172 u64 bytenr;
173 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500174 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500175 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500176 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400177 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500178 struct extent_buffer *leaf;
179 int found_type;
180 struct btrfs_path *path;
181 struct btrfs_file_extent_item *item;
182 int ret;
183 int err;
184 struct btrfs_key found_key;
185
Chris Masonc31f8832008-01-08 15:46:31 -0500186 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500187 path = btrfs_alloc_path();
188 BUG_ON(!path);
189again:
190 ret = btrfs_lookup_file_extent(NULL, root, path,
191 inode->i_ino, start, 0);
192 if (ret < 0) {
193 btrfs_free_path(path);
194 return ret;
195 }
196
197 cow_end = end;
198 if (ret != 0) {
199 if (path->slots[0] == 0)
200 goto not_found;
201 path->slots[0]--;
202 }
203
204 leaf = path->nodes[0];
205 item = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207
208 /* are we inside the extent that was found? */
209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
210 found_type = btrfs_key_type(&found_key);
211 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400212 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500214
215 found_type = btrfs_file_extent_type(leaf, item);
216 extent_start = found_key.offset;
217 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500218 u64 extent_num_bytes;
219
220 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
221 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 err = 0;
223
Chris Mason1832a6d2007-12-21 16:27:21 -0500224 if (loops && start != extent_start)
225 goto not_found;
226
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 if (start < extent_start || start >= extent_end)
228 goto not_found;
229
230 cow_end = min(end, extent_end - 1);
231 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
232 if (bytenr == 0)
233 goto not_found;
234
Chris Masona68d5932008-05-08 14:11:56 -0400235 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
236 bytenr) != 1) {
237 goto not_found;
238 }
239
Chris Masonc31f8832008-01-08 15:46:31 -0500240 /*
241 * we may be called by the resizer, make sure we're inside
242 * the limits of the FS
243 */
Chris Masona68d5932008-05-08 14:11:56 -0400244 block_group = btrfs_lookup_block_group(root->fs_info,
245 bytenr);
246 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500247 goto not_found;
248
Chris Masonbe20aa92007-12-17 20:14:01 -0500249 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500250 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500251 goto not_found;
252 }
253loop:
254 if (start > end) {
255 btrfs_free_path(path);
256 return 0;
257 }
258 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500259 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 goto again;
261
262not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400263 cow_file_range(inode, start, end);
264 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto loop;
266}
267
268static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
269{
270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500272 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500273 if (btrfs_test_opt(root, NODATACOW) ||
274 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500275 ret = run_delalloc_nocow(inode, start, end);
276 else
277 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500278
Chris Masonb888db22007-08-27 16:49:44 -0400279 mutex_unlock(&root->fs_info->fs_mutex);
280 return ret;
281}
282
Chris Mason291d6732008-01-29 15:55:23 -0500283int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500284 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500285{
Chris Masonbcbfce82008-04-22 13:26:47 -0400286 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500287 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500288 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400289 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500290 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500291 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400292 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500293 }
294 return 0;
295}
296
297int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Chris Masonb0c68f82008-01-31 11:05:37 -0500300 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500301 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400302 unsigned long flags;
303
304 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500305 if (end - start + 1 > root->fs_info->delalloc_bytes) {
306 printk("warning: delalloc account %Lu %Lu\n",
307 end - start + 1, root->fs_info->delalloc_bytes);
308 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500309 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500310 } else {
311 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500312 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500313 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400314 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500315 }
316 return 0;
317}
318
Chris Mason239b14b2008-03-24 15:02:07 -0400319int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
320 size_t size, struct bio *bio)
321{
322 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
323 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400324 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400325 u64 length = 0;
326 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400327 int ret;
328
Chris Masonf2d8d742008-04-21 10:03:05 -0400329 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400330 map_tree = &root->fs_info->mapping_tree;
331 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400332 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400333 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400334
Chris Mason239b14b2008-03-24 15:02:07 -0400335 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400336 return 1;
337 }
338 return 0;
339}
340
Chris Mason44b8bd72008-04-16 11:14:51 -0400341int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400342 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500343{
Chris Mason065631f2008-02-20 12:07:25 -0500344 struct btrfs_root *root = BTRFS_I(inode)->root;
345 struct btrfs_trans_handle *trans;
346 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400347 char *sums = NULL;
348
349 ret = btrfs_csum_one_bio(root, bio, &sums);
350 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500351
Chris Mason44b8bd72008-04-16 11:14:51 -0400352 mutex_lock(&root->fs_info->fs_mutex);
353 trans = btrfs_start_transaction(root, 1);
Chris Mason925baed2008-06-25 16:01:30 -0400354 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400355
Chris Mason44b8bd72008-04-16 11:14:51 -0400356 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400357 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
358
Chris Mason925baed2008-06-25 16:01:30 -0400359 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason44b8bd72008-04-16 11:14:51 -0400360 ret = btrfs_end_transaction(trans, root);
361 BUG_ON(ret);
362 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400363
364 kfree(sums);
365
Chris Mason8b712842008-06-11 16:50:36 -0400366 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400367}
368
369int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
370 int mirror_num)
371{
372 struct btrfs_root *root = BTRFS_I(inode)->root;
373 int ret = 0;
374
Chris Mason22c59942008-04-09 16:28:12 -0400375 if (!(rw & (1 << BIO_RW))) {
376 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
377 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400378 goto mapit;
379 }
Chris Mason065631f2008-02-20 12:07:25 -0500380
381 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400382 btrfs_test_flag(inode, NODATASUM)) {
383 goto mapit;
384 }
Chris Mason065631f2008-02-20 12:07:25 -0500385
Chris Mason44b8bd72008-04-16 11:14:51 -0400386 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
387 inode, rw, bio, mirror_num,
388 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400389mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400390 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500391}
Chris Mason6885f302008-02-20 16:11:05 -0500392
Chris Mason07157aa2007-08-30 08:50:51 -0400393int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
394{
395 int ret = 0;
396 struct inode *inode = page->mapping->host;
397 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500398 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400399 struct btrfs_csum_item *item;
400 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400401 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400402
Yanb98b6762008-01-08 15:54:37 -0500403 if (btrfs_test_opt(root, NODATASUM) ||
404 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500405 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400406
Chris Mason07157aa2007-08-30 08:50:51 -0400407 mutex_lock(&root->fs_info->fs_mutex);
408 path = btrfs_alloc_path();
409 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
410 if (IS_ERR(item)) {
411 ret = PTR_ERR(item);
412 /* a csum that isn't present is a preallocated region. */
413 if (ret == -ENOENT || ret == -EFBIG)
414 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400415 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500416 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400417 goto out;
418 }
Chris Masonff79f812007-10-15 16:22:25 -0400419 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
420 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500421 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400422out:
423 if (path)
424 btrfs_free_path(path);
425 mutex_unlock(&root->fs_info->fs_mutex);
426 return ret;
427}
428
Chris Mason7e383262008-04-09 16:28:12 -0400429struct io_failure_record {
430 struct page *page;
431 u64 start;
432 u64 len;
433 u64 logical;
434 int last_mirror;
435};
436
Chris Mason1259ab72008-05-12 13:39:03 -0400437int btrfs_io_failed_hook(struct bio *failed_bio,
438 struct page *page, u64 start, u64 end,
439 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400440{
441 struct io_failure_record *failrec = NULL;
442 u64 private;
443 struct extent_map *em;
444 struct inode *inode = page->mapping->host;
445 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400446 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400447 struct bio *bio;
448 int num_copies;
449 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400450 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400451 u64 logical;
452
453 ret = get_state_private(failure_tree, start, &private);
454 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400455 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
456 if (!failrec)
457 return -ENOMEM;
458 failrec->start = start;
459 failrec->len = end - start + 1;
460 failrec->last_mirror = 0;
461
Chris Mason3b951512008-04-17 11:29:12 -0400462 spin_lock(&em_tree->lock);
463 em = lookup_extent_mapping(em_tree, start, failrec->len);
464 if (em->start > start || em->start + em->len < start) {
465 free_extent_map(em);
466 em = NULL;
467 }
468 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400469
470 if (!em || IS_ERR(em)) {
471 kfree(failrec);
472 return -EIO;
473 }
474 logical = start - em->start;
475 logical = em->block_start + logical;
476 failrec->logical = logical;
477 free_extent_map(em);
478 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
479 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400480 set_state_private(failure_tree, start,
481 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400482 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400483 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400484 }
485 num_copies = btrfs_num_copies(
486 &BTRFS_I(inode)->root->fs_info->mapping_tree,
487 failrec->logical, failrec->len);
488 failrec->last_mirror++;
489 if (!state) {
490 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
491 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
492 failrec->start,
493 EXTENT_LOCKED);
494 if (state && state->start != failrec->start)
495 state = NULL;
496 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
497 }
498 if (!state || failrec->last_mirror > num_copies) {
499 set_state_private(failure_tree, failrec->start, 0);
500 clear_extent_bits(failure_tree, failrec->start,
501 failrec->start + failrec->len - 1,
502 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
503 kfree(failrec);
504 return -EIO;
505 }
506 bio = bio_alloc(GFP_NOFS, 1);
507 bio->bi_private = state;
508 bio->bi_end_io = failed_bio->bi_end_io;
509 bio->bi_sector = failrec->logical >> 9;
510 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400511 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400512 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400513 if (failed_bio->bi_rw & (1 << BIO_RW))
514 rw = WRITE;
515 else
516 rw = READ;
517
518 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
519 failrec->last_mirror);
520 return 0;
521}
522
523int btrfs_clean_io_failures(struct inode *inode, u64 start)
524{
525 u64 private;
526 u64 private_failure;
527 struct io_failure_record *failure;
528 int ret;
529
530 private = 0;
531 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
532 (u64)-1, 1, EXTENT_DIRTY)) {
533 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
534 start, &private_failure);
535 if (ret == 0) {
536 failure = (struct io_failure_record *)(unsigned long)
537 private_failure;
538 set_state_private(&BTRFS_I(inode)->io_failure_tree,
539 failure->start, 0);
540 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
541 failure->start,
542 failure->start + failure->len - 1,
543 EXTENT_DIRTY | EXTENT_LOCKED,
544 GFP_NOFS);
545 kfree(failure);
546 }
547 }
Chris Mason7e383262008-04-09 16:28:12 -0400548 return 0;
549}
550
Chris Mason70dec802008-01-29 09:59:12 -0500551int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
552 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400553{
Chris Mason35ebb932007-10-30 16:56:53 -0400554 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400555 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500556 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400557 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500558 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400559 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400560 struct btrfs_root *root = BTRFS_I(inode)->root;
561 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400562 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500563
Yanb98b6762008-01-08 15:54:37 -0500564 if (btrfs_test_opt(root, NODATASUM) ||
565 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500566 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500567 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500568 private = state->private;
569 ret = 0;
570 } else {
571 ret = get_state_private(io_tree, start, &private);
572 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400573 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400574 kaddr = kmap_atomic(page, KM_IRQ0);
575 if (ret) {
576 goto zeroit;
577 }
Chris Masonff79f812007-10-15 16:22:25 -0400578 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
579 btrfs_csum_final(csum, (char *)&csum);
580 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400581 goto zeroit;
582 }
583 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400584 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400585
586 /* if the io failure tree for this inode is non-empty,
587 * check to see if we've recovered from a failed IO
588 */
Chris Mason1259ab72008-05-12 13:39:03 -0400589 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400590 return 0;
591
592zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500593 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
594 page->mapping->host->i_ino, (unsigned long long)start, csum,
595 private);
Chris Masondb945352007-10-15 16:15:53 -0400596 memset(kaddr + offset, 1, end - start + 1);
597 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400598 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400599 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400600 if (private == 0)
601 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400602 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400603}
Chris Masonb888db22007-08-27 16:49:44 -0400604
Chris Mason39279cc2007-06-12 06:35:45 -0400605void btrfs_read_locked_inode(struct inode *inode)
606{
607 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400608 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400609 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400610 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400611 struct btrfs_root *root = BTRFS_I(inode)->root;
612 struct btrfs_key location;
613 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400614 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400615 int ret;
616
617 path = btrfs_alloc_path();
618 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400619 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400620 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500621
Chris Mason39279cc2007-06-12 06:35:45 -0400622 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400623 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400624 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400625
Chris Mason5f39d392007-10-15 16:14:19 -0400626 leaf = path->nodes[0];
627 inode_item = btrfs_item_ptr(leaf, path->slots[0],
628 struct btrfs_inode_item);
629
630 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
631 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
632 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
633 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
634 inode->i_size = btrfs_inode_size(leaf, inode_item);
635
636 tspec = btrfs_inode_atime(inode_item);
637 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
638 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
639
640 tspec = btrfs_inode_mtime(inode_item);
641 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
642 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
643
644 tspec = btrfs_inode_ctime(inode_item);
645 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
646 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
647
648 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
649 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400650 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400651 rdev = btrfs_inode_rdev(leaf, inode_item);
652
653 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400654 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
655 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500656 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500657 if (!BTRFS_I(inode)->block_group) {
658 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400659 NULL, 0,
660 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500661 }
Chris Mason39279cc2007-06-12 06:35:45 -0400662 btrfs_free_path(path);
663 inode_item = NULL;
664
665 mutex_unlock(&root->fs_info->fs_mutex);
666
667 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400668 case S_IFREG:
669 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400670 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500671 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400672 inode->i_fop = &btrfs_file_operations;
673 inode->i_op = &btrfs_file_inode_operations;
674 break;
675 case S_IFDIR:
676 inode->i_fop = &btrfs_dir_file_operations;
677 if (root == root->fs_info->tree_root)
678 inode->i_op = &btrfs_dir_ro_inode_operations;
679 else
680 inode->i_op = &btrfs_dir_inode_operations;
681 break;
682 case S_IFLNK:
683 inode->i_op = &btrfs_symlink_inode_operations;
684 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400685 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400686 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400687 default:
688 init_special_inode(inode, inode->i_mode, rdev);
689 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400690 }
691 return;
692
693make_bad:
694 btrfs_release_path(root, path);
695 btrfs_free_path(path);
696 mutex_unlock(&root->fs_info->fs_mutex);
697 make_bad_inode(inode);
698}
699
Chris Mason5f39d392007-10-15 16:14:19 -0400700static void fill_inode_item(struct extent_buffer *leaf,
701 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400702 struct inode *inode)
703{
Chris Mason5f39d392007-10-15 16:14:19 -0400704 btrfs_set_inode_uid(leaf, item, inode->i_uid);
705 btrfs_set_inode_gid(leaf, item, inode->i_gid);
706 btrfs_set_inode_size(leaf, item, inode->i_size);
707 btrfs_set_inode_mode(leaf, item, inode->i_mode);
708 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
709
710 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
711 inode->i_atime.tv_sec);
712 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
713 inode->i_atime.tv_nsec);
714
715 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
716 inode->i_mtime.tv_sec);
717 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
718 inode->i_mtime.tv_nsec);
719
720 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
721 inode->i_ctime.tv_sec);
722 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
723 inode->i_ctime.tv_nsec);
724
725 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
726 btrfs_set_inode_generation(leaf, item, inode->i_generation);
727 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500728 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400729 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400730 BTRFS_I(inode)->block_group->key.objectid);
731}
732
Chris Masona52d9a82007-08-27 16:49:44 -0400733int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400734 struct btrfs_root *root,
735 struct inode *inode)
736{
737 struct btrfs_inode_item *inode_item;
738 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400739 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400740 int ret;
741
742 path = btrfs_alloc_path();
743 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400744 ret = btrfs_lookup_inode(trans, root, path,
745 &BTRFS_I(inode)->location, 1);
746 if (ret) {
747 if (ret > 0)
748 ret = -ENOENT;
749 goto failed;
750 }
751
Chris Mason5f39d392007-10-15 16:14:19 -0400752 leaf = path->nodes[0];
753 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400754 struct btrfs_inode_item);
755
Chris Mason5f39d392007-10-15 16:14:19 -0400756 fill_inode_item(leaf, inode_item, inode);
757 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400758 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400759 ret = 0;
760failed:
761 btrfs_release_path(root, path);
762 btrfs_free_path(path);
763 return ret;
764}
765
766
767static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
768 struct btrfs_root *root,
769 struct inode *dir,
770 struct dentry *dentry)
771{
772 struct btrfs_path *path;
773 const char *name = dentry->d_name.name;
774 int name_len = dentry->d_name.len;
775 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400776 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400778 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400779
780 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400781 if (!path) {
782 ret = -ENOMEM;
783 goto err;
784 }
785
Chris Mason39279cc2007-06-12 06:35:45 -0400786 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
787 name, name_len, -1);
788 if (IS_ERR(di)) {
789 ret = PTR_ERR(di);
790 goto err;
791 }
792 if (!di) {
793 ret = -ENOENT;
794 goto err;
795 }
Chris Mason5f39d392007-10-15 16:14:19 -0400796 leaf = path->nodes[0];
797 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400798 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400799 if (ret)
800 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400801 btrfs_release_path(root, path);
802
803 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400804 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400805 if (IS_ERR(di)) {
806 ret = PTR_ERR(di);
807 goto err;
808 }
809 if (!di) {
810 ret = -ENOENT;
811 goto err;
812 }
813 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -0400814 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400815
816 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500817 ret = btrfs_del_inode_ref(trans, root, name, name_len,
818 dentry->d_inode->i_ino,
819 dentry->d_parent->d_inode->i_ino);
820 if (ret) {
821 printk("failed to delete reference to %.*s, "
822 "inode %lu parent %lu\n", name_len, name,
823 dentry->d_inode->i_ino,
824 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500825 }
Chris Mason39279cc2007-06-12 06:35:45 -0400826err:
827 btrfs_free_path(path);
828 if (!ret) {
829 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400830 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400831 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500832#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
833 dentry->d_inode->i_nlink--;
834#else
Chris Mason39279cc2007-06-12 06:35:45 -0400835 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500836#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400837 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400838 dir->i_sb->s_dirt = 1;
839 }
840 return ret;
841}
842
843static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
844{
845 struct btrfs_root *root;
846 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500847 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400848 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500849 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400850
851 root = BTRFS_I(dir)->root;
852 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500853
854 ret = btrfs_check_free_space(root, 1, 1);
855 if (ret)
856 goto fail;
857
Chris Mason39279cc2007-06-12 06:35:45 -0400858 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400859
Chris Mason39279cc2007-06-12 06:35:45 -0400860 btrfs_set_trans_block_group(trans, dir);
861 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400862 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400863
Chris Mason2da98f02008-01-16 11:44:43 -0500864 if (inode->i_nlink == 0) {
Chris Mason2da98f02008-01-16 11:44:43 -0500865 /* if the inode isn't linked anywhere,
866 * we don't need to worry about
867 * data=ordered
868 */
Mingminge1b81e62008-05-27 10:55:43 -0400869 btrfs_del_ordered_inode(inode);
Chris Mason2da98f02008-01-16 11:44:43 -0500870 }
871
Chris Mason39279cc2007-06-12 06:35:45 -0400872 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500873fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400874 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400875 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500876 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400877 return ret;
878}
879
880static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
881{
882 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500883 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400884 int ret;
885 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400886 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500887 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400888
Chris Mason925baed2008-06-25 16:01:30 -0400889 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -0400890 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -0400891 }
Yan134d4512007-10-25 15:49:25 -0400892
Chris Mason39279cc2007-06-12 06:35:45 -0400893 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500894 ret = btrfs_check_free_space(root, 1, 1);
895 if (ret)
896 goto fail;
897
Chris Mason39279cc2007-06-12 06:35:45 -0400898 trans = btrfs_start_transaction(root, 1);
899 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400900
901 /* now the directory is empty */
902 err = btrfs_unlink_trans(trans, root, dir, dentry);
903 if (!err) {
904 inode->i_size = 0;
905 }
Chris Mason39544012007-12-12 14:38:19 -0500906
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400907 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400908 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500909fail:
Yan134d4512007-10-25 15:49:25 -0400910 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400911 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500912 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500913
Chris Mason39279cc2007-06-12 06:35:45 -0400914 if (ret && !err)
915 err = ret;
916 return err;
917}
918
Chris Mason39279cc2007-06-12 06:35:45 -0400919/*
Chris Mason39279cc2007-06-12 06:35:45 -0400920 * this can truncate away extent items, csum items and directory items.
921 * It starts at a high offset and removes keys until it can't find
922 * any higher than i_size.
923 *
924 * csum items that cross the new i_size are truncated to the new size
925 * as well.
926 */
927static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
928 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500929 struct inode *inode,
930 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400931{
932 int ret;
933 struct btrfs_path *path;
934 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400935 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400936 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400937 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400938 struct btrfs_file_extent_item *fi;
939 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400940 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400941 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500942 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500943 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400944 int found_extent;
945 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500946 int pending_del_nr = 0;
947 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400948 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400949 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400950
Chris Mason3b951512008-04-17 11:29:12 -0400951 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400952 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400953 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400954 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400955
Chris Mason39279cc2007-06-12 06:35:45 -0400956 /* FIXME, add redo link to tree so we don't leak on crash */
957 key.objectid = inode->i_ino;
958 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400959 key.type = (u8)-1;
960
Chris Mason85e21ba2008-01-29 15:11:36 -0500961 btrfs_init_path(path);
962search_again:
963 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
964 if (ret < 0) {
965 goto error;
966 }
967 if (ret > 0) {
968 BUG_ON(path->slots[0] == 0);
969 path->slots[0]--;
970 }
971
Chris Mason39279cc2007-06-12 06:35:45 -0400972 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400973 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400974 leaf = path->nodes[0];
975 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
976 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400977
Chris Mason5f39d392007-10-15 16:14:19 -0400978 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400979 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400980
Chris Mason85e21ba2008-01-29 15:11:36 -0500981 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400982 break;
983
Chris Mason5f39d392007-10-15 16:14:19 -0400984 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400985 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400986 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400987 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400988 extent_type = btrfs_file_extent_type(leaf, fi);
989 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400990 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400991 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400992 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
993 struct btrfs_item *item = btrfs_item_nr(leaf,
994 path->slots[0]);
995 item_end += btrfs_file_extent_inline_len(leaf,
996 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400997 }
Yan008630c2007-11-07 13:31:09 -0500998 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400999 }
1000 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1001 ret = btrfs_csum_truncate(trans, root, path,
1002 inode->i_size);
1003 BUG_ON(ret);
1004 }
Yan008630c2007-11-07 13:31:09 -05001005 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001006 if (found_type == BTRFS_DIR_ITEM_KEY) {
1007 found_type = BTRFS_INODE_ITEM_KEY;
1008 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1009 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001010 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1011 found_type = BTRFS_XATTR_ITEM_KEY;
1012 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1013 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001014 } else if (found_type) {
1015 found_type--;
1016 } else {
1017 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001018 }
Yana61721d2007-09-17 11:08:38 -04001019 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001020 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001021 }
Chris Mason5f39d392007-10-15 16:14:19 -04001022 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001023 del_item = 1;
1024 else
1025 del_item = 0;
1026 found_extent = 0;
1027
1028 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001029 if (found_type != BTRFS_EXTENT_DATA_KEY)
1030 goto delete;
1031
1032 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001033 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001034 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001035 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001036 u64 orig_num_bytes =
1037 btrfs_file_extent_num_bytes(leaf, fi);
1038 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001039 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001040 extent_num_bytes = extent_num_bytes &
1041 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001042 btrfs_set_file_extent_num_bytes(leaf, fi,
1043 extent_num_bytes);
1044 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001045 extent_num_bytes);
1046 if (extent_start != 0)
1047 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001048 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001049 } else {
Chris Masondb945352007-10-15 16:15:53 -04001050 extent_num_bytes =
1051 btrfs_file_extent_disk_num_bytes(leaf,
1052 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001053 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001054 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001055 if (extent_start != 0) {
1056 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001057 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001058 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001059 root_gen = btrfs_header_generation(leaf);
1060 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001061 }
Chris Mason90692182008-02-08 13:49:28 -05001062 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1063 if (!del_item) {
1064 u32 newsize = inode->i_size - found_key.offset;
1065 dec_i_blocks(inode, item_end + 1 -
1066 found_key.offset - newsize);
1067 newsize =
1068 btrfs_file_extent_calc_inline_size(newsize);
1069 ret = btrfs_truncate_item(trans, root, path,
1070 newsize, 1);
1071 BUG_ON(ret);
1072 } else {
1073 dec_i_blocks(inode, item_end + 1 -
1074 found_key.offset);
1075 }
Chris Mason39279cc2007-06-12 06:35:45 -04001076 }
Chris Mason179e29e2007-11-01 11:28:41 -04001077delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001078 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001079 if (!pending_del_nr) {
1080 /* no pending yet, add ourselves */
1081 pending_del_slot = path->slots[0];
1082 pending_del_nr = 1;
1083 } else if (pending_del_nr &&
1084 path->slots[0] + 1 == pending_del_slot) {
1085 /* hop on the pending chunk */
1086 pending_del_nr++;
1087 pending_del_slot = path->slots[0];
1088 } else {
1089 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1090 }
Chris Mason39279cc2007-06-12 06:35:45 -04001091 } else {
1092 break;
1093 }
Chris Mason39279cc2007-06-12 06:35:45 -04001094 if (found_extent) {
1095 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001096 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001097 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001098 root_gen, inode->i_ino,
1099 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001100 BUG_ON(ret);
1101 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001102next:
1103 if (path->slots[0] == 0) {
1104 if (pending_del_nr)
1105 goto del_pending;
1106 btrfs_release_path(root, path);
1107 goto search_again;
1108 }
1109
1110 path->slots[0]--;
1111 if (pending_del_nr &&
1112 path->slots[0] + 1 != pending_del_slot) {
1113 struct btrfs_key debug;
1114del_pending:
1115 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1116 pending_del_slot);
1117 ret = btrfs_del_items(trans, root, path,
1118 pending_del_slot,
1119 pending_del_nr);
1120 BUG_ON(ret);
1121 pending_del_nr = 0;
1122 btrfs_release_path(root, path);
1123 goto search_again;
1124 }
Chris Mason39279cc2007-06-12 06:35:45 -04001125 }
1126 ret = 0;
1127error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001128 if (pending_del_nr) {
1129 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1130 pending_del_nr);
1131 }
Chris Mason39279cc2007-06-12 06:35:45 -04001132 btrfs_release_path(root, path);
1133 btrfs_free_path(path);
1134 inode->i_sb->s_dirt = 1;
1135 return ret;
1136}
1137
Chris Masonb888db22007-08-27 16:49:44 -04001138static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001139 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001140{
Chris Mason39279cc2007-06-12 06:35:45 -04001141 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001142 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001143 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001144 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001145 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001146
Chris Mason190662b2007-12-18 16:25:45 -05001147 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001148 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001149
Chris Masond1310b22008-01-24 16:13:08 -05001150 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001151 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001152 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001153
Chris Masona52d9a82007-08-27 16:49:44 -04001154 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001155 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001156 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1157 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001158 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001159 }
Chris Masonb888db22007-08-27 16:49:44 -04001160 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001161 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001162
Chris Masona52d9a82007-08-27 16:49:44 -04001163 return ret;
1164}
1165
1166/*
1167 * taken from block_truncate_page, but does cow as it zeros out
1168 * any bytes left in the last page in the file.
1169 */
1170static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1171{
1172 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001173 struct btrfs_root *root = BTRFS_I(inode)->root;
1174 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001175 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1176 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1177 struct page *page;
1178 int ret = 0;
1179 u64 page_start;
1180
1181 if ((offset & (blocksize - 1)) == 0)
1182 goto out;
1183
1184 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001185again:
Chris Masona52d9a82007-08-27 16:49:44 -04001186 page = grab_cache_page(mapping, index);
1187 if (!page)
1188 goto out;
1189 if (!PageUptodate(page)) {
1190 ret = btrfs_readpage(NULL, page);
1191 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001192 if (page->mapping != mapping) {
1193 unlock_page(page);
1194 page_cache_release(page);
1195 goto again;
1196 }
Chris Masona52d9a82007-08-27 16:49:44 -04001197 if (!PageUptodate(page)) {
1198 ret = -EIO;
1199 goto out;
1200 }
1201 }
Chris Masona52d9a82007-08-27 16:49:44 -04001202
Chris Mason211c17f2008-05-15 09:13:45 -04001203 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1204 wait_on_page_writeback(page);
Chris Masonb888db22007-08-27 16:49:44 -04001205 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001206
Chris Mason39279cc2007-06-12 06:35:45 -04001207 unlock_page(page);
1208 page_cache_release(page);
1209out:
1210 return ret;
1211}
1212
1213static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1214{
1215 struct inode *inode = dentry->d_inode;
1216 int err;
1217
1218 err = inode_change_ok(inode, attr);
1219 if (err)
1220 return err;
1221
1222 if (S_ISREG(inode->i_mode) &&
1223 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1224 struct btrfs_trans_handle *trans;
1225 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001226 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001227
Chris Mason5f39d392007-10-15 16:14:19 -04001228 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001229 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001230 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001231 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001232 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001233
Chris Mason1b0f7c22008-01-30 14:33:02 -05001234 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001235 goto out;
1236
Chris Mason1832a6d2007-12-21 16:27:21 -05001237 mutex_lock(&root->fs_info->fs_mutex);
1238 err = btrfs_check_free_space(root, 1, 0);
1239 mutex_unlock(&root->fs_info->fs_mutex);
1240 if (err)
1241 goto fail;
1242
Chris Mason39279cc2007-06-12 06:35:45 -04001243 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1244
Chris Mason1b0f7c22008-01-30 14:33:02 -05001245 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001246 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001247
1248 mutex_lock(&root->fs_info->fs_mutex);
1249 trans = btrfs_start_transaction(root, 1);
1250 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001251 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001252 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001253 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001254
Chris Mason179e29e2007-11-01 11:28:41 -04001255 if (alloc_hint != EXTENT_MAP_INLINE) {
1256 err = btrfs_insert_file_extent(trans, root,
1257 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001258 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001259 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001260 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001261 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001262 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001263 }
Chris Mason39279cc2007-06-12 06:35:45 -04001264 btrfs_end_transaction(trans, root);
1265 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001266 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001267 if (err)
1268 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001269 }
1270out:
1271 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001272fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001273 return err;
1274}
Chris Mason61295eb2008-01-14 16:24:38 -05001275
Chris Mason39279cc2007-06-12 06:35:45 -04001276void btrfs_delete_inode(struct inode *inode)
1277{
1278 struct btrfs_trans_handle *trans;
1279 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001280 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001281 int ret;
1282
1283 truncate_inode_pages(&inode->i_data, 0);
1284 if (is_bad_inode(inode)) {
1285 goto no_delete;
1286 }
Chris Mason5f39d392007-10-15 16:14:19 -04001287
Chris Mason39279cc2007-06-12 06:35:45 -04001288 inode->i_size = 0;
1289 mutex_lock(&root->fs_info->fs_mutex);
1290 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001291
Chris Mason39279cc2007-06-12 06:35:45 -04001292 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001293 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001294 if (ret)
1295 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001296
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001297 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001298 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001299
Chris Mason39279cc2007-06-12 06:35:45 -04001300 btrfs_end_transaction(trans, root);
1301 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001302 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001303 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001304 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001305
1306no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001307 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001308 btrfs_end_transaction(trans, root);
1309 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001310 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001311 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001312no_delete:
1313 clear_inode(inode);
1314}
1315
1316/*
1317 * this returns the key found in the dir entry in the location pointer.
1318 * If no dir entries were found, location->objectid is 0.
1319 */
1320static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1321 struct btrfs_key *location)
1322{
1323 const char *name = dentry->d_name.name;
1324 int namelen = dentry->d_name.len;
1325 struct btrfs_dir_item *di;
1326 struct btrfs_path *path;
1327 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001328 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001329
Chris Mason39544012007-12-12 14:38:19 -05001330 if (namelen == 1 && strcmp(name, ".") == 0) {
1331 location->objectid = dir->i_ino;
1332 location->type = BTRFS_INODE_ITEM_KEY;
1333 location->offset = 0;
1334 return 0;
1335 }
Chris Mason39279cc2007-06-12 06:35:45 -04001336 path = btrfs_alloc_path();
1337 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001338
Chris Mason7a720532007-12-13 09:06:59 -05001339 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001340 struct btrfs_key key;
1341 struct extent_buffer *leaf;
1342 u32 nritems;
1343 int slot;
1344
1345 key.objectid = dir->i_ino;
1346 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1347 key.offset = 0;
1348 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1349 BUG_ON(ret == 0);
1350 ret = 0;
1351
1352 leaf = path->nodes[0];
1353 slot = path->slots[0];
1354 nritems = btrfs_header_nritems(leaf);
1355 if (slot >= nritems)
1356 goto out_err;
1357
1358 btrfs_item_key_to_cpu(leaf, &key, slot);
1359 if (key.objectid != dir->i_ino ||
1360 key.type != BTRFS_INODE_REF_KEY) {
1361 goto out_err;
1362 }
1363 location->objectid = key.offset;
1364 location->type = BTRFS_INODE_ITEM_KEY;
1365 location->offset = 0;
1366 goto out;
1367 }
1368
Chris Mason39279cc2007-06-12 06:35:45 -04001369 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1370 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001371 if (IS_ERR(di))
1372 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001373 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001374 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001375 }
Chris Mason5f39d392007-10-15 16:14:19 -04001376 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001377out:
Chris Mason39279cc2007-06-12 06:35:45 -04001378 btrfs_free_path(path);
1379 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001380out_err:
1381 location->objectid = 0;
1382 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001383}
1384
1385/*
1386 * when we hit a tree root in a directory, the btrfs part of the inode
1387 * needs to be changed to reflect the root directory of the tree root. This
1388 * is kind of like crossing a mount point.
1389 */
1390static int fixup_tree_root_location(struct btrfs_root *root,
1391 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001392 struct btrfs_root **sub_root,
1393 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001394{
1395 struct btrfs_path *path;
1396 struct btrfs_root_item *ri;
1397
1398 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1399 return 0;
1400 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1401 return 0;
1402
1403 path = btrfs_alloc_path();
1404 BUG_ON(!path);
1405 mutex_lock(&root->fs_info->fs_mutex);
1406
Josef Bacik58176a92007-08-29 15:47:34 -04001407 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1408 dentry->d_name.name,
1409 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001410 if (IS_ERR(*sub_root))
1411 return PTR_ERR(*sub_root);
1412
1413 ri = &(*sub_root)->root_item;
1414 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001415 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1416 location->offset = 0;
1417
1418 btrfs_free_path(path);
1419 mutex_unlock(&root->fs_info->fs_mutex);
1420 return 0;
1421}
1422
1423static int btrfs_init_locked_inode(struct inode *inode, void *p)
1424{
1425 struct btrfs_iget_args *args = p;
1426 inode->i_ino = args->ino;
1427 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001428 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001429 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1430 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001431 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001432 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1433 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001434 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001435 return 0;
1436}
1437
1438static int btrfs_find_actor(struct inode *inode, void *opaque)
1439{
1440 struct btrfs_iget_args *args = opaque;
1441 return (args->ino == inode->i_ino &&
1442 args->root == BTRFS_I(inode)->root);
1443}
1444
Chris Masondc17ff82008-01-08 15:46:30 -05001445struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1446 u64 root_objectid)
1447{
1448 struct btrfs_iget_args args;
1449 args.ino = objectid;
1450 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1451
1452 if (!args.root)
1453 return NULL;
1454
1455 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1456}
1457
Chris Mason39279cc2007-06-12 06:35:45 -04001458struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1459 struct btrfs_root *root)
1460{
1461 struct inode *inode;
1462 struct btrfs_iget_args args;
1463 args.ino = objectid;
1464 args.root = root;
1465
1466 inode = iget5_locked(s, objectid, btrfs_find_actor,
1467 btrfs_init_locked_inode,
1468 (void *)&args);
1469 return inode;
1470}
1471
1472static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1473 struct nameidata *nd)
1474{
1475 struct inode * inode;
1476 struct btrfs_inode *bi = BTRFS_I(dir);
1477 struct btrfs_root *root = bi->root;
1478 struct btrfs_root *sub_root = root;
1479 struct btrfs_key location;
1480 int ret;
1481
1482 if (dentry->d_name.len > BTRFS_NAME_LEN)
1483 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001484
Chris Mason39279cc2007-06-12 06:35:45 -04001485 mutex_lock(&root->fs_info->fs_mutex);
1486 ret = btrfs_inode_by_name(dir, dentry, &location);
1487 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001488
Chris Mason39279cc2007-06-12 06:35:45 -04001489 if (ret < 0)
1490 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001491
Chris Mason39279cc2007-06-12 06:35:45 -04001492 inode = NULL;
1493 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001494 ret = fixup_tree_root_location(root, &location, &sub_root,
1495 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001496 if (ret < 0)
1497 return ERR_PTR(ret);
1498 if (ret > 0)
1499 return ERR_PTR(-ENOENT);
1500 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1501 sub_root);
1502 if (!inode)
1503 return ERR_PTR(-EACCES);
1504 if (inode->i_state & I_NEW) {
1505 /* the inode and parent dir are two different roots */
1506 if (sub_root != root) {
1507 igrab(inode);
1508 sub_root->inode = inode;
1509 }
1510 BTRFS_I(inode)->root = sub_root;
1511 memcpy(&BTRFS_I(inode)->location, &location,
1512 sizeof(location));
1513 btrfs_read_locked_inode(inode);
1514 unlock_new_inode(inode);
1515 }
1516 }
1517 return d_splice_alias(inode, dentry);
1518}
1519
Chris Mason39279cc2007-06-12 06:35:45 -04001520static unsigned char btrfs_filetype_table[] = {
1521 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1522};
1523
1524static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1525{
Chris Mason6da6aba2007-12-18 16:15:09 -05001526 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001527 struct btrfs_root *root = BTRFS_I(inode)->root;
1528 struct btrfs_item *item;
1529 struct btrfs_dir_item *di;
1530 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001531 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001532 struct btrfs_path *path;
1533 int ret;
1534 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001535 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001536 int slot;
1537 int advance;
1538 unsigned char d_type;
1539 int over = 0;
1540 u32 di_cur;
1541 u32 di_total;
1542 u32 di_len;
1543 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001544 char tmp_name[32];
1545 char *name_ptr;
1546 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001547
1548 /* FIXME, use a real flag for deciding about the key type */
1549 if (root->fs_info->tree_root == root)
1550 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001551
Chris Mason39544012007-12-12 14:38:19 -05001552 /* special case for "." */
1553 if (filp->f_pos == 0) {
1554 over = filldir(dirent, ".", 1,
1555 1, inode->i_ino,
1556 DT_DIR);
1557 if (over)
1558 return 0;
1559 filp->f_pos = 1;
1560 }
1561
Chris Mason39279cc2007-06-12 06:35:45 -04001562 mutex_lock(&root->fs_info->fs_mutex);
1563 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001564 path = btrfs_alloc_path();
1565 path->reada = 2;
1566
1567 /* special case for .., just use the back ref */
1568 if (filp->f_pos == 1) {
1569 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1570 key.offset = 0;
1571 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1572 BUG_ON(ret == 0);
1573 leaf = path->nodes[0];
1574 slot = path->slots[0];
1575 nritems = btrfs_header_nritems(leaf);
1576 if (slot >= nritems) {
1577 btrfs_release_path(root, path);
1578 goto read_dir_items;
1579 }
1580 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1581 btrfs_release_path(root, path);
1582 if (found_key.objectid != key.objectid ||
1583 found_key.type != BTRFS_INODE_REF_KEY)
1584 goto read_dir_items;
1585 over = filldir(dirent, "..", 2,
1586 2, found_key.offset, DT_DIR);
1587 if (over)
1588 goto nopos;
1589 filp->f_pos = 2;
1590 }
1591
1592read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001593 btrfs_set_key_type(&key, key_type);
1594 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001595
Chris Mason39279cc2007-06-12 06:35:45 -04001596 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1597 if (ret < 0)
1598 goto err;
1599 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001600 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001601 leaf = path->nodes[0];
1602 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001603 slot = path->slots[0];
1604 if (advance || slot >= nritems) {
1605 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001606 ret = btrfs_next_leaf(root, path);
1607 if (ret)
1608 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001609 leaf = path->nodes[0];
1610 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001611 slot = path->slots[0];
1612 } else {
1613 slot++;
1614 path->slots[0]++;
1615 }
1616 }
1617 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001618 item = btrfs_item_nr(leaf, slot);
1619 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1620
1621 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001622 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001623 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001624 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001625 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001626 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001627
1628 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001629 advance = 1;
1630 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1631 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001632 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001633 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001634 struct btrfs_key location;
1635
1636 name_len = btrfs_dir_name_len(leaf, di);
1637 if (name_len < 32) {
1638 name_ptr = tmp_name;
1639 } else {
1640 name_ptr = kmalloc(name_len, GFP_NOFS);
1641 BUG_ON(!name_ptr);
1642 }
1643 read_extent_buffer(leaf, name_ptr,
1644 (unsigned long)(di + 1), name_len);
1645
1646 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1647 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001648 over = filldir(dirent, name_ptr, name_len,
1649 found_key.offset,
1650 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001651 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001652
1653 if (name_ptr != tmp_name)
1654 kfree(name_ptr);
1655
Chris Mason39279cc2007-06-12 06:35:45 -04001656 if (over)
1657 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001658 di_len = btrfs_dir_name_len(leaf, di) +
1659 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001660 di_cur += di_len;
1661 di = (struct btrfs_dir_item *)((char *)di + di_len);
1662 }
1663 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001664 if (key_type == BTRFS_DIR_INDEX_KEY)
1665 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1666 else
1667 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001668nopos:
1669 ret = 0;
1670err:
1671 btrfs_release_path(root, path);
1672 btrfs_free_path(path);
1673 mutex_unlock(&root->fs_info->fs_mutex);
1674 return ret;
1675}
1676
1677int btrfs_write_inode(struct inode *inode, int wait)
1678{
1679 struct btrfs_root *root = BTRFS_I(inode)->root;
1680 struct btrfs_trans_handle *trans;
1681 int ret = 0;
1682
1683 if (wait) {
1684 mutex_lock(&root->fs_info->fs_mutex);
1685 trans = btrfs_start_transaction(root, 1);
1686 btrfs_set_trans_block_group(trans, inode);
1687 ret = btrfs_commit_transaction(trans, root);
1688 mutex_unlock(&root->fs_info->fs_mutex);
1689 }
1690 return ret;
1691}
1692
1693/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001694 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001695 * inode changes. But, it is most likely to find the inode in cache.
1696 * FIXME, needs more benchmarking...there are no reasons other than performance
1697 * to keep or drop this code.
1698 */
1699void btrfs_dirty_inode(struct inode *inode)
1700{
1701 struct btrfs_root *root = BTRFS_I(inode)->root;
1702 struct btrfs_trans_handle *trans;
1703
1704 mutex_lock(&root->fs_info->fs_mutex);
1705 trans = btrfs_start_transaction(root, 1);
1706 btrfs_set_trans_block_group(trans, inode);
1707 btrfs_update_inode(trans, root, inode);
1708 btrfs_end_transaction(trans, root);
1709 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001710}
1711
1712static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1713 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001714 const char *name, int name_len,
1715 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001716 u64 objectid,
1717 struct btrfs_block_group_cache *group,
1718 int mode)
1719{
1720 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001721 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001722 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001723 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001724 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001725 struct btrfs_inode_ref *ref;
1726 struct btrfs_key key[2];
1727 u32 sizes[2];
1728 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001729 int ret;
1730 int owner;
1731
Chris Mason5f39d392007-10-15 16:14:19 -04001732 path = btrfs_alloc_path();
1733 BUG_ON(!path);
1734
Chris Mason39279cc2007-06-12 06:35:45 -04001735 inode = new_inode(root->fs_info->sb);
1736 if (!inode)
1737 return ERR_PTR(-ENOMEM);
1738
Chris Masond1310b22008-01-24 16:13:08 -05001739 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1740 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001741 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001742 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1743 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001744 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason90692182008-02-08 13:49:28 -05001745 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001746 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001747
Chris Mason39279cc2007-06-12 06:35:45 -04001748 if (mode & S_IFDIR)
1749 owner = 0;
1750 else
1751 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001752 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001753 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001754 if (!new_inode_group) {
1755 printk("find_block group failed\n");
1756 new_inode_group = group;
1757 }
1758 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001759 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001760
1761 key[0].objectid = objectid;
1762 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1763 key[0].offset = 0;
1764
1765 key[1].objectid = objectid;
1766 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1767 key[1].offset = ref_objectid;
1768
1769 sizes[0] = sizeof(struct btrfs_inode_item);
1770 sizes[1] = name_len + sizeof(*ref);
1771
1772 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1773 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001774 goto fail;
1775
Chris Mason9c583092008-01-29 15:15:18 -05001776 if (objectid > root->highest_inode)
1777 root->highest_inode = objectid;
1778
Chris Mason39279cc2007-06-12 06:35:45 -04001779 inode->i_uid = current->fsuid;
1780 inode->i_gid = current->fsgid;
1781 inode->i_mode = mode;
1782 inode->i_ino = objectid;
1783 inode->i_blocks = 0;
1784 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001785 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1786 struct btrfs_inode_item);
1787 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001788
1789 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1790 struct btrfs_inode_ref);
1791 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1792 ptr = (unsigned long)(ref + 1);
1793 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1794
Chris Mason5f39d392007-10-15 16:14:19 -04001795 btrfs_mark_buffer_dirty(path->nodes[0]);
1796 btrfs_free_path(path);
1797
Chris Mason39279cc2007-06-12 06:35:45 -04001798 location = &BTRFS_I(inode)->location;
1799 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001800 location->offset = 0;
1801 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1802
Chris Mason39279cc2007-06-12 06:35:45 -04001803 insert_inode_hash(inode);
1804 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001805fail:
1806 btrfs_free_path(path);
1807 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001808}
1809
1810static inline u8 btrfs_inode_type(struct inode *inode)
1811{
1812 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1813}
1814
1815static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001816 struct dentry *dentry, struct inode *inode,
1817 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001818{
1819 int ret;
1820 struct btrfs_key key;
1821 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001822 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001823
Chris Mason39279cc2007-06-12 06:35:45 -04001824 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001825 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1826 key.offset = 0;
1827
1828 ret = btrfs_insert_dir_item(trans, root,
1829 dentry->d_name.name, dentry->d_name.len,
1830 dentry->d_parent->d_inode->i_ino,
1831 &key, btrfs_inode_type(inode));
1832 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001833 if (add_backref) {
1834 ret = btrfs_insert_inode_ref(trans, root,
1835 dentry->d_name.name,
1836 dentry->d_name.len,
1837 inode->i_ino,
1838 dentry->d_parent->d_inode->i_ino);
1839 }
Chris Mason79c44582007-06-25 10:09:33 -04001840 parent_inode = dentry->d_parent->d_inode;
1841 parent_inode->i_size += dentry->d_name.len * 2;
1842 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001843 ret = btrfs_update_inode(trans, root,
1844 dentry->d_parent->d_inode);
1845 }
1846 return ret;
1847}
1848
1849static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001850 struct dentry *dentry, struct inode *inode,
1851 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001852{
Chris Mason9c583092008-01-29 15:15:18 -05001853 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001854 if (!err) {
1855 d_instantiate(dentry, inode);
1856 return 0;
1857 }
1858 if (err > 0)
1859 err = -EEXIST;
1860 return err;
1861}
1862
Josef Bacik618e21d2007-07-11 10:18:17 -04001863static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1864 int mode, dev_t rdev)
1865{
1866 struct btrfs_trans_handle *trans;
1867 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001868 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001869 int err;
1870 int drop_inode = 0;
1871 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001872 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001873
1874 if (!new_valid_dev(rdev))
1875 return -EINVAL;
1876
1877 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001878 err = btrfs_check_free_space(root, 1, 0);
1879 if (err)
1880 goto fail;
1881
Josef Bacik618e21d2007-07-11 10:18:17 -04001882 trans = btrfs_start_transaction(root, 1);
1883 btrfs_set_trans_block_group(trans, dir);
1884
1885 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1886 if (err) {
1887 err = -ENOSPC;
1888 goto out_unlock;
1889 }
1890
Chris Mason9c583092008-01-29 15:15:18 -05001891 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1892 dentry->d_name.len,
1893 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001894 BTRFS_I(dir)->block_group, mode);
1895 err = PTR_ERR(inode);
1896 if (IS_ERR(inode))
1897 goto out_unlock;
1898
1899 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001900 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001901 if (err)
1902 drop_inode = 1;
1903 else {
1904 inode->i_op = &btrfs_special_inode_operations;
1905 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001906 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001907 }
1908 dir->i_sb->s_dirt = 1;
1909 btrfs_update_inode_block_group(trans, inode);
1910 btrfs_update_inode_block_group(trans, dir);
1911out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001912 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001913 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001914fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001915 mutex_unlock(&root->fs_info->fs_mutex);
1916
1917 if (drop_inode) {
1918 inode_dec_link_count(inode);
1919 iput(inode);
1920 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001921 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001922 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001923 return err;
1924}
1925
Chris Mason39279cc2007-06-12 06:35:45 -04001926static int btrfs_create(struct inode *dir, struct dentry *dentry,
1927 int mode, struct nameidata *nd)
1928{
1929 struct btrfs_trans_handle *trans;
1930 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001931 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001932 int err;
1933 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001934 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001935 u64 objectid;
1936
1937 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001938 err = btrfs_check_free_space(root, 1, 0);
1939 if (err)
1940 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001941 trans = btrfs_start_transaction(root, 1);
1942 btrfs_set_trans_block_group(trans, dir);
1943
1944 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1945 if (err) {
1946 err = -ENOSPC;
1947 goto out_unlock;
1948 }
1949
Chris Mason9c583092008-01-29 15:15:18 -05001950 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1951 dentry->d_name.len,
1952 dentry->d_parent->d_inode->i_ino,
1953 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001954 err = PTR_ERR(inode);
1955 if (IS_ERR(inode))
1956 goto out_unlock;
1957
1958 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001959 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001960 if (err)
1961 drop_inode = 1;
1962 else {
1963 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001964 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001965 inode->i_fop = &btrfs_file_operations;
1966 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001967 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1968 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001969 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001970 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1971 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001972 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04001973 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001974 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001975 }
1976 dir->i_sb->s_dirt = 1;
1977 btrfs_update_inode_block_group(trans, inode);
1978 btrfs_update_inode_block_group(trans, dir);
1979out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001980 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001981 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001982fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001983 mutex_unlock(&root->fs_info->fs_mutex);
1984
1985 if (drop_inode) {
1986 inode_dec_link_count(inode);
1987 iput(inode);
1988 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001989 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001990 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001991 return err;
1992}
1993
1994static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1995 struct dentry *dentry)
1996{
1997 struct btrfs_trans_handle *trans;
1998 struct btrfs_root *root = BTRFS_I(dir)->root;
1999 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002000 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002001 int err;
2002 int drop_inode = 0;
2003
2004 if (inode->i_nlink == 0)
2005 return -ENOENT;
2006
Chris Mason6da6aba2007-12-18 16:15:09 -05002007#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2008 inode->i_nlink++;
2009#else
Chris Mason39279cc2007-06-12 06:35:45 -04002010 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002011#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002012 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002013 err = btrfs_check_free_space(root, 1, 0);
2014 if (err)
2015 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002016 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002017
Chris Mason39279cc2007-06-12 06:35:45 -04002018 btrfs_set_trans_block_group(trans, dir);
2019 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002020 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002021
Chris Mason39279cc2007-06-12 06:35:45 -04002022 if (err)
2023 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002024
Chris Mason39279cc2007-06-12 06:35:45 -04002025 dir->i_sb->s_dirt = 1;
2026 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002027 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002028
Chris Mason54aa1f42007-06-22 14:16:25 -04002029 if (err)
2030 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002031
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002032 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002033 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002034fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002035 mutex_unlock(&root->fs_info->fs_mutex);
2036
2037 if (drop_inode) {
2038 inode_dec_link_count(inode);
2039 iput(inode);
2040 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002041 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002042 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002043 return err;
2044}
2045
Chris Mason39279cc2007-06-12 06:35:45 -04002046static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2047{
Chris Masonb9d86662008-05-02 16:13:49 -04002048 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002049 struct btrfs_trans_handle *trans;
2050 struct btrfs_root *root = BTRFS_I(dir)->root;
2051 int err = 0;
2052 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002053 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002054 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002055
2056 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002057 err = btrfs_check_free_space(root, 1, 0);
2058 if (err)
2059 goto out_unlock;
2060
Chris Mason39279cc2007-06-12 06:35:45 -04002061 trans = btrfs_start_transaction(root, 1);
2062 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002063
Chris Mason39279cc2007-06-12 06:35:45 -04002064 if (IS_ERR(trans)) {
2065 err = PTR_ERR(trans);
2066 goto out_unlock;
2067 }
2068
2069 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2070 if (err) {
2071 err = -ENOSPC;
2072 goto out_unlock;
2073 }
2074
Chris Mason9c583092008-01-29 15:15:18 -05002075 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2076 dentry->d_name.len,
2077 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002078 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2079 if (IS_ERR(inode)) {
2080 err = PTR_ERR(inode);
2081 goto out_fail;
2082 }
Chris Mason5f39d392007-10-15 16:14:19 -04002083
Chris Mason39279cc2007-06-12 06:35:45 -04002084 drop_on_err = 1;
2085 inode->i_op = &btrfs_dir_inode_operations;
2086 inode->i_fop = &btrfs_dir_file_operations;
2087 btrfs_set_trans_block_group(trans, inode);
2088
Chris Mason39544012007-12-12 14:38:19 -05002089 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002090 err = btrfs_update_inode(trans, root, inode);
2091 if (err)
2092 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002093
Chris Mason9c583092008-01-29 15:15:18 -05002094 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002095 if (err)
2096 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002097
Chris Mason39279cc2007-06-12 06:35:45 -04002098 d_instantiate(dentry, inode);
2099 drop_on_err = 0;
2100 dir->i_sb->s_dirt = 1;
2101 btrfs_update_inode_block_group(trans, inode);
2102 btrfs_update_inode_block_group(trans, dir);
2103
2104out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002105 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002106 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002107
Chris Mason39279cc2007-06-12 06:35:45 -04002108out_unlock:
2109 mutex_unlock(&root->fs_info->fs_mutex);
2110 if (drop_on_err)
2111 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002112 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002113 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002114 return err;
2115}
2116
Chris Mason3b951512008-04-17 11:29:12 -04002117static int merge_extent_mapping(struct extent_map_tree *em_tree,
2118 struct extent_map *existing,
2119 struct extent_map *em)
2120{
2121 u64 start_diff;
2122 u64 new_end;
2123 int ret = 0;
2124 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2125
2126 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2127 goto invalid;
2128
2129 if (!real_blocks && em->block_start != existing->block_start)
2130 goto invalid;
2131
2132 new_end = max(existing->start + existing->len, em->start + em->len);
2133
2134 if (existing->start >= em->start) {
2135 if (em->start + em->len < existing->start)
2136 goto invalid;
2137
2138 start_diff = existing->start - em->start;
2139 if (real_blocks && em->block_start + start_diff !=
2140 existing->block_start)
2141 goto invalid;
2142
2143 em->len = new_end - em->start;
2144
2145 remove_extent_mapping(em_tree, existing);
2146 /* free for the tree */
2147 free_extent_map(existing);
2148 ret = add_extent_mapping(em_tree, em);
2149
2150 } else if (em->start > existing->start) {
2151
2152 if (existing->start + existing->len < em->start)
2153 goto invalid;
2154
2155 start_diff = em->start - existing->start;
2156 if (real_blocks && existing->block_start + start_diff !=
2157 em->block_start)
2158 goto invalid;
2159
2160 remove_extent_mapping(em_tree, existing);
2161 em->block_start = existing->block_start;
2162 em->start = existing->start;
2163 em->len = new_end - existing->start;
2164 free_extent_map(existing);
2165
2166 ret = add_extent_mapping(em_tree, em);
2167 } else {
2168 goto invalid;
2169 }
2170 return ret;
2171
2172invalid:
2173 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2174 existing->start, existing->len, existing->block_start,
2175 em->start, em->len, em->block_start);
2176 return -EIO;
2177}
2178
Chris Masona52d9a82007-08-27 16:49:44 -04002179struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002180 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002181 int create)
2182{
2183 int ret;
2184 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002185 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002186 u64 extent_start = 0;
2187 u64 extent_end = 0;
2188 u64 objectid = inode->i_ino;
2189 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002190 struct btrfs_path *path;
2191 struct btrfs_root *root = BTRFS_I(inode)->root;
2192 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002193 struct extent_buffer *leaf;
2194 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002195 struct extent_map *em = NULL;
2196 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002197 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002198 struct btrfs_trans_handle *trans = NULL;
2199
2200 path = btrfs_alloc_path();
2201 BUG_ON(!path);
2202 mutex_lock(&root->fs_info->fs_mutex);
2203
2204again:
Chris Masond1310b22008-01-24 16:13:08 -05002205 spin_lock(&em_tree->lock);
2206 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002207 if (em)
2208 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002209 spin_unlock(&em_tree->lock);
2210
Chris Masona52d9a82007-08-27 16:49:44 -04002211 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002212 if (em->start > start || em->start + em->len <= start)
2213 free_extent_map(em);
2214 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002215 free_extent_map(em);
2216 else
2217 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002218 }
Chris Masond1310b22008-01-24 16:13:08 -05002219 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002220 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002221 err = -ENOMEM;
2222 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002223 }
Chris Masond1310b22008-01-24 16:13:08 -05002224
2225 em->start = EXTENT_MAP_HOLE;
2226 em->len = (u64)-1;
Chris Masona061fc82008-05-07 11:43:44 -04002227 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002228 ret = btrfs_lookup_file_extent(trans, root, path,
2229 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002230 if (ret < 0) {
2231 err = ret;
2232 goto out;
2233 }
2234
2235 if (ret != 0) {
2236 if (path->slots[0] == 0)
2237 goto not_found;
2238 path->slots[0]--;
2239 }
2240
Chris Mason5f39d392007-10-15 16:14:19 -04002241 leaf = path->nodes[0];
2242 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002243 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002244 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002245 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2246 found_type = btrfs_key_type(&found_key);
2247 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002248 found_type != BTRFS_EXTENT_DATA_KEY) {
2249 goto not_found;
2250 }
2251
Chris Mason5f39d392007-10-15 16:14:19 -04002252 found_type = btrfs_file_extent_type(leaf, item);
2253 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002254 if (found_type == BTRFS_FILE_EXTENT_REG) {
2255 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002256 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002257 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002258 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002259 em->start = start;
2260 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002261 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002262 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002263 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002264 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002265 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002266 }
2267 goto not_found_em;
2268 }
Chris Masondb945352007-10-15 16:15:53 -04002269 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2270 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002271 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002272 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002273 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002274 goto insert;
2275 }
Chris Masondb945352007-10-15 16:15:53 -04002276 bytenr += btrfs_file_extent_offset(leaf, item);
2277 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002278 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002279 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002280 goto insert;
2281 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002282 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002283 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002284 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002285 size_t size;
2286 size_t extent_offset;
2287 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002288
Chris Mason5f39d392007-10-15 16:14:19 -04002289 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2290 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002291 extent_end = (extent_start + size + root->sectorsize - 1) &
2292 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002293 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002294 em->start = start;
2295 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002296 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002297 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002298 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002299 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002300 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002301 }
2302 goto not_found_em;
2303 }
2304 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002305
2306 if (!page) {
2307 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002308 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002309 goto out;
2310 }
2311
Chris Mason70dec802008-01-29 09:59:12 -05002312 page_start = page_offset(page) + pg_offset;
2313 extent_offset = page_start - extent_start;
2314 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002315 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002316 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002317 em->len = (copy_size + root->sectorsize - 1) &
2318 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002319 map = kmap(page);
2320 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002321 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002322 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002323 copy_size);
2324 flush_dcache_page(page);
2325 } else if (create && PageUptodate(page)) {
2326 if (!trans) {
2327 kunmap(page);
2328 free_extent_map(em);
2329 em = NULL;
2330 btrfs_release_path(root, path);
2331 trans = btrfs_start_transaction(root, 1);
2332 goto again;
2333 }
Chris Mason70dec802008-01-29 09:59:12 -05002334 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002335 copy_size);
2336 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002337 }
Chris Masona52d9a82007-08-27 16:49:44 -04002338 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002339 set_extent_uptodate(io_tree, em->start,
2340 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002341 goto insert;
2342 } else {
2343 printk("unkknown found_type %d\n", found_type);
2344 WARN_ON(1);
2345 }
2346not_found:
2347 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002348 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002349not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002350 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002351insert:
2352 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002353 if (em->start > start || extent_map_end(em) <= start) {
2354 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002355 err = -EIO;
2356 goto out;
2357 }
Chris Masond1310b22008-01-24 16:13:08 -05002358
2359 err = 0;
2360 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002361 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002362 /* it is possible that someone inserted the extent into the tree
2363 * while we had the lock dropped. It is also possible that
2364 * an overlapping map exists in the tree
2365 */
Chris Masona52d9a82007-08-27 16:49:44 -04002366 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002367 struct extent_map *existing;
2368 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002369 if (existing && (existing->start > start ||
2370 existing->start + existing->len <= start)) {
2371 free_extent_map(existing);
2372 existing = NULL;
2373 }
Chris Mason3b951512008-04-17 11:29:12 -04002374 if (!existing) {
2375 existing = lookup_extent_mapping(em_tree, em->start,
2376 em->len);
2377 if (existing) {
2378 err = merge_extent_mapping(em_tree, existing,
2379 em);
2380 free_extent_map(existing);
2381 if (err) {
2382 free_extent_map(em);
2383 em = NULL;
2384 }
2385 } else {
2386 err = -EIO;
2387 printk("failing to insert %Lu %Lu\n",
2388 start, len);
2389 free_extent_map(em);
2390 em = NULL;
2391 }
2392 } else {
2393 free_extent_map(em);
2394 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002395 }
Chris Masona52d9a82007-08-27 16:49:44 -04002396 }
Chris Masond1310b22008-01-24 16:13:08 -05002397 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002398out:
2399 btrfs_free_path(path);
2400 if (trans) {
2401 ret = btrfs_end_transaction(trans, root);
2402 if (!err)
2403 err = ret;
2404 }
2405 mutex_unlock(&root->fs_info->fs_mutex);
2406 if (err) {
2407 free_extent_map(em);
2408 WARN_ON(1);
2409 return ERR_PTR(err);
2410 }
2411 return em;
2412}
2413
Chris Masone1c4b742008-04-22 13:26:46 -04002414#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002415static int btrfs_get_block(struct inode *inode, sector_t iblock,
2416 struct buffer_head *bh_result, int create)
2417{
2418 struct extent_map *em;
2419 u64 start = (u64)iblock << inode->i_blkbits;
2420 struct btrfs_multi_bio *multi = NULL;
2421 struct btrfs_root *root = BTRFS_I(inode)->root;
2422 u64 len;
2423 u64 logical;
2424 u64 map_length;
2425 int ret = 0;
2426
2427 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2428
2429 if (!em || IS_ERR(em))
2430 goto out;
2431
Chris Masone1c4b742008-04-22 13:26:46 -04002432 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002433 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002434 }
Chris Mason16432982008-04-10 10:23:21 -04002435
2436 if (em->block_start == EXTENT_MAP_INLINE) {
2437 ret = -EINVAL;
2438 goto out;
2439 }
2440
Chris Mason16432982008-04-10 10:23:21 -04002441 len = em->start + em->len - start;
2442 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2443
Chris Masone1c4b742008-04-22 13:26:46 -04002444 if (em->block_start == EXTENT_MAP_HOLE ||
2445 em->block_start == EXTENT_MAP_DELALLOC) {
2446 bh_result->b_size = len;
2447 goto out;
2448 }
2449
Chris Mason16432982008-04-10 10:23:21 -04002450 logical = start - em->start;
2451 logical = em->block_start + logical;
2452
2453 map_length = len;
2454 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2455 logical, &map_length, &multi, 0);
2456 BUG_ON(ret);
2457 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2458 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002459
Chris Mason16432982008-04-10 10:23:21 -04002460 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2461 set_buffer_mapped(bh_result);
2462 kfree(multi);
2463out:
2464 free_extent_map(em);
2465 return ret;
2466}
Chris Masone1c4b742008-04-22 13:26:46 -04002467#endif
Chris Mason16432982008-04-10 10:23:21 -04002468
2469static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2470 const struct iovec *iov, loff_t offset,
2471 unsigned long nr_segs)
2472{
Chris Masone1c4b742008-04-22 13:26:46 -04002473 return -EINVAL;
2474#if 0
Chris Mason16432982008-04-10 10:23:21 -04002475 struct file *file = iocb->ki_filp;
2476 struct inode *inode = file->f_mapping->host;
2477
2478 if (rw == WRITE)
2479 return -EINVAL;
2480
2481 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2482 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002483#endif
Chris Mason16432982008-04-10 10:23:21 -04002484}
2485
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002486static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002487{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002488 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002489}
2490
Chris Mason9ebefb182007-06-15 13:50:00 -04002491int btrfs_readpage(struct file *file, struct page *page)
2492{
Chris Masond1310b22008-01-24 16:13:08 -05002493 struct extent_io_tree *tree;
2494 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002495 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002496}
Chris Mason1832a6d2007-12-21 16:27:21 -05002497
Chris Mason39279cc2007-06-12 06:35:45 -04002498static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2499{
Chris Masond1310b22008-01-24 16:13:08 -05002500 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002501
2502
2503 if (current->flags & PF_MEMALLOC) {
2504 redirty_page_for_writepage(wbc, page);
2505 unlock_page(page);
2506 return 0;
2507 }
Chris Masond1310b22008-01-24 16:13:08 -05002508 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002509 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2510}
Chris Mason39279cc2007-06-12 06:35:45 -04002511
Chris Masonb293f022007-11-01 19:45:34 -04002512static int btrfs_writepages(struct address_space *mapping,
2513 struct writeback_control *wbc)
2514{
Chris Masond1310b22008-01-24 16:13:08 -05002515 struct extent_io_tree *tree;
2516 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002517 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2518}
2519
Chris Mason3ab2fb52007-11-08 10:59:22 -05002520static int
2521btrfs_readpages(struct file *file, struct address_space *mapping,
2522 struct list_head *pages, unsigned nr_pages)
2523{
Chris Masond1310b22008-01-24 16:13:08 -05002524 struct extent_io_tree *tree;
2525 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002526 return extent_readpages(tree, mapping, pages, nr_pages,
2527 btrfs_get_extent);
2528}
2529
Chris Mason70dec802008-01-29 09:59:12 -05002530static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002531{
Chris Masond1310b22008-01-24 16:13:08 -05002532 struct extent_io_tree *tree;
2533 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002534 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002535
Chris Masond1310b22008-01-24 16:13:08 -05002536 tree = &BTRFS_I(page->mapping->host)->io_tree;
2537 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002538 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002539 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002540 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002541 ClearPagePrivate(page);
2542 set_page_private(page, 0);
2543 page_cache_release(page);
2544 }
2545 return ret;
2546}
Chris Mason39279cc2007-06-12 06:35:45 -04002547
Chris Masona52d9a82007-08-27 16:49:44 -04002548static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2549{
Chris Masond1310b22008-01-24 16:13:08 -05002550 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002551
Chris Masond1310b22008-01-24 16:13:08 -05002552 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002553 extent_invalidatepage(tree, page, offset);
2554 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002555 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002556 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002557 ClearPagePrivate(page);
2558 set_page_private(page, 0);
2559 page_cache_release(page);
2560 }
Chris Mason39279cc2007-06-12 06:35:45 -04002561}
2562
Chris Mason9ebefb182007-06-15 13:50:00 -04002563/*
2564 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2565 * called from a page fault handler when a page is first dirtied. Hence we must
2566 * be careful to check for EOF conditions here. We set the page up correctly
2567 * for a written page which means we get ENOSPC checking when writing into
2568 * holes and correct delalloc and unwritten extent mapping on filesystems that
2569 * support these features.
2570 *
2571 * We are not allowed to take the i_mutex here so we have to play games to
2572 * protect against truncate races as the page could now be beyond EOF. Because
2573 * vmtruncate() writes the inode size before removing pages, once we have the
2574 * page lock we can determine safely if the page is beyond EOF. If it is not
2575 * beyond EOF, then the page is guaranteed safe against truncation until we
2576 * unlock the page.
2577 */
2578int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2579{
Chris Mason6da6aba2007-12-18 16:15:09 -05002580 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002581 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002582 unsigned long end;
2583 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002584 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002585 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002586
Chris Mason1832a6d2007-12-21 16:27:21 -05002587 mutex_lock(&root->fs_info->fs_mutex);
2588 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002589 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002590 if (ret)
2591 goto out;
2592
2593 ret = -EINVAL;
2594
Chris Mason9ebefb182007-06-15 13:50:00 -04002595 lock_page(page);
2596 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002597 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002598 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002599
Chris Mason9ebefb182007-06-15 13:50:00 -04002600 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002601 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002602 /* page got truncated out from underneath us */
2603 goto out_unlock;
2604 }
2605
2606 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002607 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002608 end = size & ~PAGE_CACHE_MASK;
2609 else
2610 end = PAGE_CACHE_SIZE;
2611
Chris Masonb888db22007-08-27 16:49:44 -04002612 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002613
2614out_unlock:
2615 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002616out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002617 return ret;
2618}
2619
Chris Mason39279cc2007-06-12 06:35:45 -04002620static void btrfs_truncate(struct inode *inode)
2621{
2622 struct btrfs_root *root = BTRFS_I(inode)->root;
2623 int ret;
2624 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002625 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002626
2627 if (!S_ISREG(inode->i_mode))
2628 return;
2629 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2630 return;
2631
2632 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2633
2634 mutex_lock(&root->fs_info->fs_mutex);
2635 trans = btrfs_start_transaction(root, 1);
2636 btrfs_set_trans_block_group(trans, inode);
2637
2638 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002639 ret = btrfs_truncate_in_trans(trans, root, inode,
2640 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002641 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002642 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002643
Chris Mason39279cc2007-06-12 06:35:45 -04002644 ret = btrfs_end_transaction(trans, root);
2645 BUG_ON(ret);
2646 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002647 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002648 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002649}
2650
Sven Wegener3b963622008-06-09 21:57:42 -04002651/*
2652 * Invalidate a single dcache entry at the root of the filesystem.
2653 * Needed after creation of snapshot or subvolume.
2654 */
2655void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2656 int namelen)
2657{
2658 struct dentry *alias, *entry;
2659 struct qstr qstr;
2660
2661 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2662 if (alias) {
2663 qstr.name = name;
2664 qstr.len = namelen;
2665 /* change me if btrfs ever gets a d_hash operation */
2666 qstr.hash = full_name_hash(qstr.name, qstr.len);
2667 entry = d_lookup(alias, &qstr);
2668 dput(alias);
2669 if (entry) {
2670 d_invalidate(entry);
2671 dput(entry);
2672 }
2673 }
2674}
2675
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002676int btrfs_create_subvol_root(struct btrfs_root *new_root,
2677 struct btrfs_trans_handle *trans, u64 new_dirid,
2678 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002679{
Chris Mason39279cc2007-06-12 06:35:45 -04002680 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002681 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002682
Chris Mason9c583092008-01-29 15:15:18 -05002683 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002684 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002685 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002686 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002687 inode->i_op = &btrfs_dir_inode_operations;
2688 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002689 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002690
Chris Mason39544012007-12-12 14:38:19 -05002691 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2692 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002693 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002694 inode->i_size = 0;
Sven Wegener3b963622008-06-09 21:57:42 -04002695
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002696 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002697}
2698
Chris Masonedbd8d42007-12-21 16:27:24 -05002699unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002700 struct file_ra_state *ra, struct file *file,
2701 pgoff_t offset, pgoff_t last_index)
2702{
Chris Mason8e7bf942008-04-28 09:02:36 -04002703 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002704
2705#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002706 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2707 return offset;
2708#else
Chris Mason86479a02007-09-10 19:58:16 -04002709 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2710 return offset + req_size;
2711#endif
2712}
2713
Chris Mason39279cc2007-06-12 06:35:45 -04002714struct inode *btrfs_alloc_inode(struct super_block *sb)
2715{
2716 struct btrfs_inode *ei;
2717
2718 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2719 if (!ei)
2720 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002721 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002722 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002723 return &ei->vfs_inode;
2724}
2725
2726void btrfs_destroy_inode(struct inode *inode)
2727{
2728 WARN_ON(!list_empty(&inode->i_dentry));
2729 WARN_ON(inode->i_data.nrpages);
2730
Chris Mason8c416c92008-01-14 15:10:26 -05002731 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002732 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2733}
2734
Chris Mason44ec0b72007-10-29 10:55:05 -04002735#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2736static void init_once(struct kmem_cache * cachep, void *foo)
2737#else
Chris Mason39279cc2007-06-12 06:35:45 -04002738static void init_once(void * foo, struct kmem_cache * cachep,
2739 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002740#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002741{
2742 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2743
2744 inode_init_once(&ei->vfs_inode);
2745}
2746
2747void btrfs_destroy_cachep(void)
2748{
2749 if (btrfs_inode_cachep)
2750 kmem_cache_destroy(btrfs_inode_cachep);
2751 if (btrfs_trans_handle_cachep)
2752 kmem_cache_destroy(btrfs_trans_handle_cachep);
2753 if (btrfs_transaction_cachep)
2754 kmem_cache_destroy(btrfs_transaction_cachep);
2755 if (btrfs_bit_radix_cachep)
2756 kmem_cache_destroy(btrfs_bit_radix_cachep);
2757 if (btrfs_path_cachep)
2758 kmem_cache_destroy(btrfs_path_cachep);
2759}
2760
Chris Mason86479a02007-09-10 19:58:16 -04002761struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002762 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002763#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2764 void (*ctor)(struct kmem_cache *, void *)
2765#else
Chris Mason92fee662007-07-25 12:31:35 -04002766 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002767 unsigned long)
2768#endif
2769 )
Chris Mason92fee662007-07-25 12:31:35 -04002770{
2771 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2772 SLAB_MEM_SPREAD | extra_flags), ctor
2773#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2774 ,NULL
2775#endif
2776 );
2777}
2778
Chris Mason39279cc2007-06-12 06:35:45 -04002779int btrfs_init_cachep(void)
2780{
Chris Mason86479a02007-09-10 19:58:16 -04002781 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002782 sizeof(struct btrfs_inode),
2783 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002784 if (!btrfs_inode_cachep)
2785 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002786 btrfs_trans_handle_cachep =
2787 btrfs_cache_create("btrfs_trans_handle_cache",
2788 sizeof(struct btrfs_trans_handle),
2789 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002790 if (!btrfs_trans_handle_cachep)
2791 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002792 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002793 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002794 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002795 if (!btrfs_transaction_cachep)
2796 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002797 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002798 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002799 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002800 if (!btrfs_path_cachep)
2801 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002802 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002803 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002804 if (!btrfs_bit_radix_cachep)
2805 goto fail;
2806 return 0;
2807fail:
2808 btrfs_destroy_cachep();
2809 return -ENOMEM;
2810}
2811
2812static int btrfs_getattr(struct vfsmount *mnt,
2813 struct dentry *dentry, struct kstat *stat)
2814{
2815 struct inode *inode = dentry->d_inode;
2816 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002817 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002818 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002819 return 0;
2820}
2821
2822static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2823 struct inode * new_dir,struct dentry *new_dentry)
2824{
2825 struct btrfs_trans_handle *trans;
2826 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2827 struct inode *new_inode = new_dentry->d_inode;
2828 struct inode *old_inode = old_dentry->d_inode;
2829 struct timespec ctime = CURRENT_TIME;
2830 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002831 int ret;
2832
2833 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2834 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2835 return -ENOTEMPTY;
2836 }
Chris Mason5f39d392007-10-15 16:14:19 -04002837
Chris Mason39279cc2007-06-12 06:35:45 -04002838 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002839 ret = btrfs_check_free_space(root, 1, 0);
2840 if (ret)
2841 goto out_unlock;
2842
Chris Mason39279cc2007-06-12 06:35:45 -04002843 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002844
Chris Mason39279cc2007-06-12 06:35:45 -04002845 btrfs_set_trans_block_group(trans, new_dir);
2846 path = btrfs_alloc_path();
2847 if (!path) {
2848 ret = -ENOMEM;
2849 goto out_fail;
2850 }
2851
2852 old_dentry->d_inode->i_nlink++;
2853 old_dir->i_ctime = old_dir->i_mtime = ctime;
2854 new_dir->i_ctime = new_dir->i_mtime = ctime;
2855 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002856
Chris Mason39279cc2007-06-12 06:35:45 -04002857 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2858 if (ret)
2859 goto out_fail;
2860
2861 if (new_inode) {
2862 new_inode->i_ctime = CURRENT_TIME;
2863 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2864 if (ret)
2865 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002866 }
Chris Mason9c583092008-01-29 15:15:18 -05002867 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002868 if (ret)
2869 goto out_fail;
2870
2871out_fail:
2872 btrfs_free_path(path);
2873 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002874out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002875 mutex_unlock(&root->fs_info->fs_mutex);
2876 return ret;
2877}
2878
2879static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2880 const char *symname)
2881{
2882 struct btrfs_trans_handle *trans;
2883 struct btrfs_root *root = BTRFS_I(dir)->root;
2884 struct btrfs_path *path;
2885 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002886 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002887 int err;
2888 int drop_inode = 0;
2889 u64 objectid;
2890 int name_len;
2891 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002892 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002893 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002894 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002895 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002896
2897 name_len = strlen(symname) + 1;
2898 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2899 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002900
Chris Mason39279cc2007-06-12 06:35:45 -04002901 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002902 err = btrfs_check_free_space(root, 1, 0);
2903 if (err)
2904 goto out_fail;
2905
Chris Mason39279cc2007-06-12 06:35:45 -04002906 trans = btrfs_start_transaction(root, 1);
2907 btrfs_set_trans_block_group(trans, dir);
2908
2909 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2910 if (err) {
2911 err = -ENOSPC;
2912 goto out_unlock;
2913 }
2914
Chris Mason9c583092008-01-29 15:15:18 -05002915 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2916 dentry->d_name.len,
2917 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002918 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2919 err = PTR_ERR(inode);
2920 if (IS_ERR(inode))
2921 goto out_unlock;
2922
2923 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002924 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002925 if (err)
2926 drop_inode = 1;
2927 else {
2928 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002929 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002930 inode->i_fop = &btrfs_file_operations;
2931 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002932 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2933 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002934 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002935 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2936 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002937 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04002938 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002939 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002940 }
2941 dir->i_sb->s_dirt = 1;
2942 btrfs_update_inode_block_group(trans, inode);
2943 btrfs_update_inode_block_group(trans, dir);
2944 if (drop_inode)
2945 goto out_unlock;
2946
2947 path = btrfs_alloc_path();
2948 BUG_ON(!path);
2949 key.objectid = inode->i_ino;
2950 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002951 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2952 datasize = btrfs_file_extent_calc_inline_size(name_len);
2953 err = btrfs_insert_empty_item(trans, root, path, &key,
2954 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002955 if (err) {
2956 drop_inode = 1;
2957 goto out_unlock;
2958 }
Chris Mason5f39d392007-10-15 16:14:19 -04002959 leaf = path->nodes[0];
2960 ei = btrfs_item_ptr(leaf, path->slots[0],
2961 struct btrfs_file_extent_item);
2962 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2963 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002964 BTRFS_FILE_EXTENT_INLINE);
2965 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002966 write_extent_buffer(leaf, symname, ptr, name_len);
2967 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002968 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002969
Chris Mason39279cc2007-06-12 06:35:45 -04002970 inode->i_op = &btrfs_symlink_inode_operations;
2971 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002972 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002973 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002974 err = btrfs_update_inode(trans, root, inode);
2975 if (err)
2976 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002977
2978out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002979 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002980 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002981out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002982 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002983 if (drop_inode) {
2984 inode_dec_link_count(inode);
2985 iput(inode);
2986 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002987 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002988 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002989 return err;
2990}
Chris Mason16432982008-04-10 10:23:21 -04002991
Yanfdebe2b2008-01-14 13:26:08 -05002992static int btrfs_permission(struct inode *inode, int mask,
2993 struct nameidata *nd)
2994{
2995 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2996 return -EACCES;
2997 return generic_permission(inode, mask, NULL);
2998}
Chris Mason39279cc2007-06-12 06:35:45 -04002999
3000static struct inode_operations btrfs_dir_inode_operations = {
3001 .lookup = btrfs_lookup,
3002 .create = btrfs_create,
3003 .unlink = btrfs_unlink,
3004 .link = btrfs_link,
3005 .mkdir = btrfs_mkdir,
3006 .rmdir = btrfs_rmdir,
3007 .rename = btrfs_rename,
3008 .symlink = btrfs_symlink,
3009 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003010 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003011 .setxattr = generic_setxattr,
3012 .getxattr = generic_getxattr,
3013 .listxattr = btrfs_listxattr,
3014 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003015 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003016};
Chris Mason39279cc2007-06-12 06:35:45 -04003017static struct inode_operations btrfs_dir_ro_inode_operations = {
3018 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003019 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003020};
Chris Mason39279cc2007-06-12 06:35:45 -04003021static struct file_operations btrfs_dir_file_operations = {
3022 .llseek = generic_file_llseek,
3023 .read = generic_read_dir,
3024 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003025 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003026#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003027 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003028#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003029 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003030};
3031
Chris Masond1310b22008-01-24 16:13:08 -05003032static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003033 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003034 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003035 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003036 .readpage_io_hook = btrfs_readpage_io_hook,
3037 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003038 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003039 .set_bit_hook = btrfs_set_bit_hook,
3040 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003041};
3042
Chris Mason39279cc2007-06-12 06:35:45 -04003043static struct address_space_operations btrfs_aops = {
3044 .readpage = btrfs_readpage,
3045 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003046 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003047 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003048 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003049 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003050 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003051 .invalidatepage = btrfs_invalidatepage,
3052 .releasepage = btrfs_releasepage,
3053 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003054};
3055
3056static struct address_space_operations btrfs_symlink_aops = {
3057 .readpage = btrfs_readpage,
3058 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003059 .invalidatepage = btrfs_invalidatepage,
3060 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003061};
3062
3063static struct inode_operations btrfs_file_inode_operations = {
3064 .truncate = btrfs_truncate,
3065 .getattr = btrfs_getattr,
3066 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003067 .setxattr = generic_setxattr,
3068 .getxattr = generic_getxattr,
3069 .listxattr = btrfs_listxattr,
3070 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003071 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003072};
Josef Bacik618e21d2007-07-11 10:18:17 -04003073static struct inode_operations btrfs_special_inode_operations = {
3074 .getattr = btrfs_getattr,
3075 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003076 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003077};
Chris Mason39279cc2007-06-12 06:35:45 -04003078static struct inode_operations btrfs_symlink_inode_operations = {
3079 .readlink = generic_readlink,
3080 .follow_link = page_follow_link_light,
3081 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003082 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003083};