blob: 8a405a5fa6a31280bb91a5c1f84000ba6f092a13 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Josef Bacik7b128762008-07-24 12:17:14 -040081static void btrfs_truncate(struct inode *inode);
82
Chris Mason1832a6d2007-12-21 16:27:21 -050083int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
84 int for_del)
85{
Chris Masona2135012008-06-25 16:01:30 -040086 u64 total;
87 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040089 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 int ret = 0;
91
Chris Masona2135012008-06-25 16:01:30 -040092 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 else
Chris Masonf9ef6602008-01-03 09:22:38 -050098 thresh = total * 85;
99
100 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
103 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 return ret;
106}
107
Chris Masonbe20aa92007-12-17 20:14:01 -0500108static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400109{
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400112 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500114 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400115 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500116 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
120 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400121
Chris Masonf9295742008-07-17 12:54:14 -0400122 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400123 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 btrfs_set_trans_block_group(trans, inode);
125
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500127 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500128 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400129
Chris Mason179e29e2007-11-01 11:28:41 -0400130 if (alloc_hint == EXTENT_MAP_INLINE)
131 goto out;
132
Chris Mason3b951512008-04-17 11:29:12 -0400133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400137
Chris Masonc59f8952007-12-17 20:14:04 -0500138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
142 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 if (ret) {
144 WARN_ON(1);
145 goto out;
146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147 em = alloc_extent_map(GFP_NOFS);
148 em->start = start;
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 while(1) {
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
159 free_extent_map(em);
160 break;
161 }
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
164 }
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166
Chris Mason98d20f62008-04-14 09:46:10 -0400167 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
169 ins.offset);
170 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 cur_alloc_size);
174 break;
175 }
Chris Masonc59f8952007-12-17 20:14:04 -0500176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400179 }
Chris Masonb888db22007-08-27 16:49:44 -0400180out:
181 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 return ret;
183}
184
185static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
186{
187 u64 extent_start;
188 u64 extent_end;
189 u64 bytenr;
190 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500191 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500192 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400194 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 struct extent_buffer *leaf;
196 int found_type;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
199 int ret;
200 int err;
201 struct btrfs_key found_key;
202
Chris Masonc31f8832008-01-08 15:46:31 -0500203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 path = btrfs_alloc_path();
205 BUG_ON(!path);
206again:
207 ret = btrfs_lookup_file_extent(NULL, root, path,
208 inode->i_ino, start, 0);
209 if (ret < 0) {
210 btrfs_free_path(path);
211 return ret;
212 }
213
214 cow_end = end;
215 if (ret != 0) {
216 if (path->slots[0] == 0)
217 goto not_found;
218 path->slots[0]--;
219 }
220
221 leaf = path->nodes[0];
222 item = btrfs_item_ptr(leaf, path->slots[0],
223 struct btrfs_file_extent_item);
224
225 /* are we inside the extent that was found? */
226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
227 found_type = btrfs_key_type(&found_key);
228 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400229 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231
232 found_type = btrfs_file_extent_type(leaf, item);
233 extent_start = found_key.offset;
234 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500235 u64 extent_num_bytes;
236
237 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
238 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 err = 0;
240
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 if (loops && start != extent_start)
242 goto not_found;
243
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 if (start < extent_start || start >= extent_end)
245 goto not_found;
246
247 cow_end = min(end, extent_end - 1);
248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (bytenr == 0)
250 goto not_found;
251
Yan Zhengf321e492008-07-30 09:26:11 -0400252 if (btrfs_cross_ref_exists(root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400253 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500264 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto not_found;
266 }
267loop:
268 if (start > end) {
269 btrfs_free_path(path);
270 return 0;
271 }
272 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500273 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto again;
275
276not_found:
Yan Zhengf321e492008-07-30 09:26:11 -0400277 btrfs_release_path(root, path);
Chris Masonbbaf5492008-05-08 16:31:21 -0400278 cow_file_range(inode, start, end);
279 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 goto loop;
281}
282
283static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
284{
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400287
Yanb98b6762008-01-08 15:54:37 -0500288 if (btrfs_test_opt(root, NODATACOW) ||
289 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500290 ret = run_delalloc_nocow(inode, start, end);
291 else
292 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500293
Chris Masonb888db22007-08-27 16:49:44 -0400294 return ret;
295}
296
Chris Mason291d6732008-01-29 15:55:23 -0500297int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500301 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500302 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500304 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500305 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400306 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
307 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
308 &root->fs_info->delalloc_inodes);
309 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400310 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500311 }
312 return 0;
313}
314
315int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500316 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500317{
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500319 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400320 unsigned long flags;
321
322 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500323 if (end - start + 1 > root->fs_info->delalloc_bytes) {
324 printk("warning: delalloc account %Lu %Lu\n",
325 end - start + 1, root->fs_info->delalloc_bytes);
326 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500327 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500328 } else {
329 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500330 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500331 }
Chris Masonea8c2812008-08-04 23:17:27 -0400332 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
333 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
334 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
335 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400336 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500337 }
338 return 0;
339}
340
Chris Mason239b14b2008-03-24 15:02:07 -0400341int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
342 size_t size, struct bio *bio)
343{
344 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
345 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400346 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400347 u64 length = 0;
348 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400349 int ret;
350
Chris Masonf2d8d742008-04-21 10:03:05 -0400351 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400352 map_tree = &root->fs_info->mapping_tree;
353 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400354 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400355 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400356
Chris Mason239b14b2008-03-24 15:02:07 -0400357 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400358 return 1;
359 }
360 return 0;
361}
362
Chris Mason44b8bd72008-04-16 11:14:51 -0400363int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400364 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500365{
Chris Mason065631f2008-02-20 12:07:25 -0500366 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500367 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400368
Chris Mason3edf7d32008-07-18 06:17:13 -0400369 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400370 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400371
Chris Mason8b712842008-06-11 16:50:36 -0400372 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400373}
374
375int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
376 int mirror_num)
377{
378 struct btrfs_root *root = BTRFS_I(inode)->root;
379 int ret = 0;
380
Chris Masone6dcd2d2008-07-17 12:53:50 -0400381 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
382 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500383
Chris Masone6dcd2d2008-07-17 12:53:50 -0400384 if (!(rw & (1 << BIO_RW))) {
Chris Mason61b49442008-07-31 15:42:53 -0400385 if (!btrfs_test_opt(root, NODATASUM) &&
386 !btrfs_test_flag(inode, NODATASUM)) {
Chris Mason6dab8152008-08-04 08:35:53 -0400387 mutex_lock(&BTRFS_I(inode)->csum_mutex);
Chris Mason61b49442008-07-31 15:42:53 -0400388 btrfs_lookup_bio_sums(root, inode, bio);
Chris Mason6dab8152008-08-04 08:35:53 -0400389 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Mason61b49442008-07-31 15:42:53 -0400390 }
Chris Mason0b86a832008-03-24 15:01:56 -0400391 goto mapit;
392 }
Chris Mason065631f2008-02-20 12:07:25 -0500393
Chris Mason44b8bd72008-04-16 11:14:51 -0400394 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
395 inode, rw, bio, mirror_num,
396 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400397mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400398 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500399}
Chris Mason6885f302008-02-20 16:11:05 -0500400
Chris Masonba1da2f2008-07-17 12:54:15 -0400401static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400402 struct inode *inode, u64 file_offset,
403 struct list_head *list)
404{
405 struct list_head *cur;
406 struct btrfs_ordered_sum *sum;
407
408 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400409 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400410 sum = list_entry(cur, struct btrfs_ordered_sum, list);
411 mutex_lock(&BTRFS_I(inode)->csum_mutex);
412 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
413 inode, sum);
414 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400415 }
416 return 0;
417}
418
Chris Masonea8c2812008-08-04 23:17:27 -0400419int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
420{
421 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
422 GFP_NOFS);
423}
424
Chris Mason247e7432008-07-17 12:53:51 -0400425struct btrfs_writepage_fixup {
426 struct page *page;
427 struct btrfs_work work;
428};
429
430/* see btrfs_writepage_start_hook for details on why this is required */
431void btrfs_writepage_fixup_worker(struct btrfs_work *work)
432{
433 struct btrfs_writepage_fixup *fixup;
434 struct btrfs_ordered_extent *ordered;
435 struct page *page;
436 struct inode *inode;
437 u64 page_start;
438 u64 page_end;
439
440 fixup = container_of(work, struct btrfs_writepage_fixup, work);
441 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400442again:
Chris Mason247e7432008-07-17 12:53:51 -0400443 lock_page(page);
444 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
445 ClearPageChecked(page);
446 goto out_page;
447 }
448
449 inode = page->mapping->host;
450 page_start = page_offset(page);
451 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
452
453 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400454
455 /* already ordered? We're done */
456 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
457 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400458 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400459 }
460
461 ordered = btrfs_lookup_ordered_extent(inode, page_start);
462 if (ordered) {
463 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
464 page_end, GFP_NOFS);
465 unlock_page(page);
466 btrfs_start_ordered_extent(inode, ordered, 1);
467 goto again;
468 }
Chris Mason247e7432008-07-17 12:53:51 -0400469
Chris Masonea8c2812008-08-04 23:17:27 -0400470 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400471 ClearPageChecked(page);
472out:
473 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
474out_page:
475 unlock_page(page);
476 page_cache_release(page);
477}
478
479/*
480 * There are a few paths in the higher layers of the kernel that directly
481 * set the page dirty bit without asking the filesystem if it is a
482 * good idea. This causes problems because we want to make sure COW
483 * properly happens and the data=ordered rules are followed.
484 *
485 * In our case any range that doesn't have the EXTENT_ORDERED bit set
486 * hasn't been properly setup for IO. We kick off an async process
487 * to fix it up. The async helper will wait for ordered extents, set
488 * the delalloc bit and make it safe to write the page.
489 */
490int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
491{
492 struct inode *inode = page->mapping->host;
493 struct btrfs_writepage_fixup *fixup;
494 struct btrfs_root *root = BTRFS_I(inode)->root;
495 int ret;
496
497 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
498 EXTENT_ORDERED, 0);
499 if (ret)
500 return 0;
501
502 if (PageChecked(page))
503 return -EAGAIN;
504
505 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
506 if (!fixup)
507 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400508
Chris Mason247e7432008-07-17 12:53:51 -0400509 SetPageChecked(page);
510 page_cache_get(page);
511 fixup->work.func = btrfs_writepage_fixup_worker;
512 fixup->page = page;
513 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
514 return -EAGAIN;
515}
516
Chris Mason211f90e2008-07-18 11:56:15 -0400517static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400518{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400519 struct btrfs_root *root = BTRFS_I(inode)->root;
520 struct btrfs_trans_handle *trans;
521 struct btrfs_ordered_extent *ordered_extent;
522 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
523 u64 alloc_hint = 0;
524 struct list_head list;
525 struct btrfs_key ins;
526 int ret;
527
528 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400529 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400530 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400531
Chris Masonf9295742008-07-17 12:54:14 -0400532 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400533
534 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
535 BUG_ON(!ordered_extent);
536
537 lock_extent(io_tree, ordered_extent->file_offset,
538 ordered_extent->file_offset + ordered_extent->len - 1,
539 GFP_NOFS);
540
541 INIT_LIST_HEAD(&list);
542
543 ins.objectid = ordered_extent->start;
544 ins.offset = ordered_extent->len;
545 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400546
Chris Masone6dcd2d2008-07-17 12:53:50 -0400547 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
548 trans->transid, inode->i_ino,
549 ordered_extent->file_offset, &ins);
550 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400551
552 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400553
Chris Masone6dcd2d2008-07-17 12:53:50 -0400554 ret = btrfs_drop_extents(trans, root, inode,
555 ordered_extent->file_offset,
556 ordered_extent->file_offset +
557 ordered_extent->len,
558 ordered_extent->file_offset, &alloc_hint);
559 BUG_ON(ret);
560 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
561 ordered_extent->file_offset,
562 ordered_extent->start,
563 ordered_extent->len,
564 ordered_extent->len, 0);
565 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400566
Chris Masone6dcd2d2008-07-17 12:53:50 -0400567 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
568 ordered_extent->file_offset +
569 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400570 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
571
Chris Masone6dcd2d2008-07-17 12:53:50 -0400572 inode->i_blocks += ordered_extent->len >> 9;
573 unlock_extent(io_tree, ordered_extent->file_offset,
574 ordered_extent->file_offset + ordered_extent->len - 1,
575 GFP_NOFS);
576 add_pending_csums(trans, inode, ordered_extent->file_offset,
577 &ordered_extent->list);
578
Chris Masondbe674a2008-07-17 12:54:05 -0400579 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400580 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400581
Chris Masone6dcd2d2008-07-17 12:53:50 -0400582 /* once for us */
583 btrfs_put_ordered_extent(ordered_extent);
584 /* once for the tree */
585 btrfs_put_ordered_extent(ordered_extent);
586
587 btrfs_update_inode(trans, root, inode);
588 btrfs_end_transaction(trans, root);
589 return 0;
590}
591
Chris Mason211f90e2008-07-18 11:56:15 -0400592int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
593 struct extent_state *state, int uptodate)
594{
595 return btrfs_finish_ordered_io(page->mapping->host, start, end);
596}
597
Chris Mason7e383262008-04-09 16:28:12 -0400598struct io_failure_record {
599 struct page *page;
600 u64 start;
601 u64 len;
602 u64 logical;
603 int last_mirror;
604};
605
Chris Mason1259ab72008-05-12 13:39:03 -0400606int btrfs_io_failed_hook(struct bio *failed_bio,
607 struct page *page, u64 start, u64 end,
608 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400609{
610 struct io_failure_record *failrec = NULL;
611 u64 private;
612 struct extent_map *em;
613 struct inode *inode = page->mapping->host;
614 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400615 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400616 struct bio *bio;
617 int num_copies;
618 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400619 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400620 u64 logical;
621
622 ret = get_state_private(failure_tree, start, &private);
623 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400624 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
625 if (!failrec)
626 return -ENOMEM;
627 failrec->start = start;
628 failrec->len = end - start + 1;
629 failrec->last_mirror = 0;
630
Chris Mason3b951512008-04-17 11:29:12 -0400631 spin_lock(&em_tree->lock);
632 em = lookup_extent_mapping(em_tree, start, failrec->len);
633 if (em->start > start || em->start + em->len < start) {
634 free_extent_map(em);
635 em = NULL;
636 }
637 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400638
639 if (!em || IS_ERR(em)) {
640 kfree(failrec);
641 return -EIO;
642 }
643 logical = start - em->start;
644 logical = em->block_start + logical;
645 failrec->logical = logical;
646 free_extent_map(em);
647 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
648 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400649 set_state_private(failure_tree, start,
650 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400651 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400652 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400653 }
654 num_copies = btrfs_num_copies(
655 &BTRFS_I(inode)->root->fs_info->mapping_tree,
656 failrec->logical, failrec->len);
657 failrec->last_mirror++;
658 if (!state) {
659 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
660 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
661 failrec->start,
662 EXTENT_LOCKED);
663 if (state && state->start != failrec->start)
664 state = NULL;
665 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
666 }
667 if (!state || failrec->last_mirror > num_copies) {
668 set_state_private(failure_tree, failrec->start, 0);
669 clear_extent_bits(failure_tree, failrec->start,
670 failrec->start + failrec->len - 1,
671 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
672 kfree(failrec);
673 return -EIO;
674 }
675 bio = bio_alloc(GFP_NOFS, 1);
676 bio->bi_private = state;
677 bio->bi_end_io = failed_bio->bi_end_io;
678 bio->bi_sector = failrec->logical >> 9;
679 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400680 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400681 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400682 if (failed_bio->bi_rw & (1 << BIO_RW))
683 rw = WRITE;
684 else
685 rw = READ;
686
687 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
688 failrec->last_mirror);
689 return 0;
690}
691
692int btrfs_clean_io_failures(struct inode *inode, u64 start)
693{
694 u64 private;
695 u64 private_failure;
696 struct io_failure_record *failure;
697 int ret;
698
699 private = 0;
700 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
701 (u64)-1, 1, EXTENT_DIRTY)) {
702 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
703 start, &private_failure);
704 if (ret == 0) {
705 failure = (struct io_failure_record *)(unsigned long)
706 private_failure;
707 set_state_private(&BTRFS_I(inode)->io_failure_tree,
708 failure->start, 0);
709 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
710 failure->start,
711 failure->start + failure->len - 1,
712 EXTENT_DIRTY | EXTENT_LOCKED,
713 GFP_NOFS);
714 kfree(failure);
715 }
716 }
Chris Mason7e383262008-04-09 16:28:12 -0400717 return 0;
718}
719
Chris Mason70dec802008-01-29 09:59:12 -0500720int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
721 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400722{
Chris Mason35ebb932007-10-30 16:56:53 -0400723 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400724 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400726 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500727 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400728 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400729 struct btrfs_root *root = BTRFS_I(inode)->root;
730 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400731 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500732
Yanb98b6762008-01-08 15:54:37 -0500733 if (btrfs_test_opt(root, NODATASUM) ||
734 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500735 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500736 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500737 private = state->private;
738 ret = 0;
739 } else {
740 ret = get_state_private(io_tree, start, &private);
741 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400742 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400743 kaddr = kmap_atomic(page, KM_IRQ0);
744 if (ret) {
745 goto zeroit;
746 }
Chris Masonff79f812007-10-15 16:22:25 -0400747 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
748 btrfs_csum_final(csum, (char *)&csum);
749 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400750 goto zeroit;
751 }
752 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400753 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400754
755 /* if the io failure tree for this inode is non-empty,
756 * check to see if we've recovered from a failed IO
757 */
Chris Mason1259ab72008-05-12 13:39:03 -0400758 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400759 return 0;
760
761zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500762 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
763 page->mapping->host->i_ino, (unsigned long long)start, csum,
764 private);
Chris Masondb945352007-10-15 16:15:53 -0400765 memset(kaddr + offset, 1, end - start + 1);
766 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400767 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400768 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400769 if (private == 0)
770 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400771 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400772}
Chris Masonb888db22007-08-27 16:49:44 -0400773
Josef Bacik7b128762008-07-24 12:17:14 -0400774/*
775 * This creates an orphan entry for the given inode in case something goes
776 * wrong in the middle of an unlink/truncate.
777 */
778int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
779{
780 struct btrfs_root *root = BTRFS_I(inode)->root;
781 int ret = 0;
782
Yanbcc63ab2008-07-30 16:29:20 -0400783 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400784
785 /* already on the orphan list, we're good */
786 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400787 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400788 return 0;
789 }
790
791 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
792
Yanbcc63ab2008-07-30 16:29:20 -0400793 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400794
795 /*
796 * insert an orphan item to track this unlinked/truncated file
797 */
798 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
799
800 return ret;
801}
802
803/*
804 * We have done the truncate/delete so we can go ahead and remove the orphan
805 * item for this particular inode.
806 */
807int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
808{
809 struct btrfs_root *root = BTRFS_I(inode)->root;
810 int ret = 0;
811
Yanbcc63ab2008-07-30 16:29:20 -0400812 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400813
814 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400815 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400816 return 0;
817 }
818
819 list_del_init(&BTRFS_I(inode)->i_orphan);
820 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400821 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400822 return 0;
823 }
824
Yanbcc63ab2008-07-30 16:29:20 -0400825 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400826
827 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
828
829 return ret;
830}
831
832/*
833 * this cleans up any orphans that may be left on the list from the last use
834 * of this root.
835 */
836void btrfs_orphan_cleanup(struct btrfs_root *root)
837{
838 struct btrfs_path *path;
839 struct extent_buffer *leaf;
840 struct btrfs_item *item;
841 struct btrfs_key key, found_key;
842 struct btrfs_trans_handle *trans;
843 struct inode *inode;
844 int ret = 0, nr_unlink = 0, nr_truncate = 0;
845
846 /* don't do orphan cleanup if the fs is readonly. */
847 if (root->inode->i_sb->s_flags & MS_RDONLY)
848 return;
849
850 path = btrfs_alloc_path();
851 if (!path)
852 return;
853 path->reada = -1;
854
855 key.objectid = BTRFS_ORPHAN_OBJECTID;
856 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
857 key.offset = (u64)-1;
858
859 trans = btrfs_start_transaction(root, 1);
860 btrfs_set_trans_block_group(trans, root->inode);
861
862 while (1) {
863 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
864 if (ret < 0) {
865 printk(KERN_ERR "Error searching slot for orphan: %d"
866 "\n", ret);
867 break;
868 }
869
870 /*
871 * if ret == 0 means we found what we were searching for, which
872 * is weird, but possible, so only screw with path if we didnt
873 * find the key and see if we have stuff that matches
874 */
875 if (ret > 0) {
876 if (path->slots[0] == 0)
877 break;
878 path->slots[0]--;
879 }
880
881 /* pull out the item */
882 leaf = path->nodes[0];
883 item = btrfs_item_nr(leaf, path->slots[0]);
884 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
885
886 /* make sure the item matches what we want */
887 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
888 break;
889 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
890 break;
891
892 /* release the path since we're done with it */
893 btrfs_release_path(root, path);
894
895 /*
896 * this is where we are basically btrfs_lookup, without the
897 * crossing root thing. we store the inode number in the
898 * offset of the orphan item.
899 */
900 inode = btrfs_iget_locked(root->inode->i_sb,
901 found_key.offset, root);
902 if (!inode)
903 break;
904
905 if (inode->i_state & I_NEW) {
906 BTRFS_I(inode)->root = root;
907
908 /* have to set the location manually */
909 BTRFS_I(inode)->location.objectid = inode->i_ino;
910 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
911 BTRFS_I(inode)->location.offset = 0;
912
913 btrfs_read_locked_inode(inode);
914 unlock_new_inode(inode);
915 }
916
917 /*
918 * add this inode to the orphan list so btrfs_orphan_del does
919 * the proper thing when we hit it
920 */
Yanbcc63ab2008-07-30 16:29:20 -0400921 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400922 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400923 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400924
925 /*
926 * if this is a bad inode, means we actually succeeded in
927 * removing the inode, but not the orphan record, which means
928 * we need to manually delete the orphan since iput will just
929 * do a destroy_inode
930 */
931 if (is_bad_inode(inode)) {
932 btrfs_orphan_del(trans, inode);
933 iput(inode);
934 continue;
935 }
936
937 /* if we have links, this was a truncate, lets do that */
938 if (inode->i_nlink) {
939 nr_truncate++;
940 btrfs_truncate(inode);
941 } else {
942 nr_unlink++;
943 }
944
945 /* this will do delete_inode and everything for us */
946 iput(inode);
947 }
948
949 if (nr_unlink)
950 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
951 if (nr_truncate)
952 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
953
954 btrfs_free_path(path);
955 btrfs_end_transaction(trans, root);
956}
957
Chris Mason39279cc2007-06-12 06:35:45 -0400958void btrfs_read_locked_inode(struct inode *inode)
959{
960 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400961 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400962 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400963 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400964 struct btrfs_root *root = BTRFS_I(inode)->root;
965 struct btrfs_key location;
966 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400967 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400968 int ret;
969
970 path = btrfs_alloc_path();
971 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400972 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500973
Chris Mason39279cc2007-06-12 06:35:45 -0400974 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400975 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400976 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400977
Chris Mason5f39d392007-10-15 16:14:19 -0400978 leaf = path->nodes[0];
979 inode_item = btrfs_item_ptr(leaf, path->slots[0],
980 struct btrfs_inode_item);
981
982 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
983 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
984 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
985 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400986 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400987
988 tspec = btrfs_inode_atime(inode_item);
989 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
990 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
991
992 tspec = btrfs_inode_mtime(inode_item);
993 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
994 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
995
996 tspec = btrfs_inode_ctime(inode_item);
997 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
998 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
999
1000 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1001 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001002 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001003 rdev = btrfs_inode_rdev(leaf, inode_item);
1004
Josef Bacikaec74772008-07-24 12:12:38 -04001005 BTRFS_I(inode)->index_cnt = (u64)-1;
1006
Chris Mason5f39d392007-10-15 16:14:19 -04001007 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001008 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1009 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001010 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001011 if (!BTRFS_I(inode)->block_group) {
1012 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001013 NULL, 0,
1014 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001015 }
Chris Mason39279cc2007-06-12 06:35:45 -04001016 btrfs_free_path(path);
1017 inode_item = NULL;
1018
Chris Mason39279cc2007-06-12 06:35:45 -04001019 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001020 case S_IFREG:
1021 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001022 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001023 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001024 inode->i_fop = &btrfs_file_operations;
1025 inode->i_op = &btrfs_file_inode_operations;
1026 break;
1027 case S_IFDIR:
1028 inode->i_fop = &btrfs_dir_file_operations;
1029 if (root == root->fs_info->tree_root)
1030 inode->i_op = &btrfs_dir_ro_inode_operations;
1031 else
1032 inode->i_op = &btrfs_dir_inode_operations;
1033 break;
1034 case S_IFLNK:
1035 inode->i_op = &btrfs_symlink_inode_operations;
1036 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001037 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001038 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001039 default:
1040 init_special_inode(inode, inode->i_mode, rdev);
1041 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001042 }
1043 return;
1044
1045make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001046 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001047 make_bad_inode(inode);
1048}
1049
Chris Mason5f39d392007-10-15 16:14:19 -04001050static void fill_inode_item(struct extent_buffer *leaf,
1051 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001052 struct inode *inode)
1053{
Chris Mason5f39d392007-10-15 16:14:19 -04001054 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1055 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001056 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001057 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1058 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1059
1060 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1061 inode->i_atime.tv_sec);
1062 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1063 inode->i_atime.tv_nsec);
1064
1065 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1066 inode->i_mtime.tv_sec);
1067 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1068 inode->i_mtime.tv_nsec);
1069
1070 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1071 inode->i_ctime.tv_sec);
1072 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1073 inode->i_ctime.tv_nsec);
1074
1075 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1076 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1077 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001078 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001079 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001080 BTRFS_I(inode)->block_group->key.objectid);
1081}
1082
Chris Masonba1da2f2008-07-17 12:54:15 -04001083int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001084 struct btrfs_root *root,
1085 struct inode *inode)
1086{
1087 struct btrfs_inode_item *inode_item;
1088 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001089 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001090 int ret;
1091
1092 path = btrfs_alloc_path();
1093 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001094 ret = btrfs_lookup_inode(trans, root, path,
1095 &BTRFS_I(inode)->location, 1);
1096 if (ret) {
1097 if (ret > 0)
1098 ret = -ENOENT;
1099 goto failed;
1100 }
1101
Chris Mason5f39d392007-10-15 16:14:19 -04001102 leaf = path->nodes[0];
1103 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001104 struct btrfs_inode_item);
1105
Chris Mason5f39d392007-10-15 16:14:19 -04001106 fill_inode_item(leaf, inode_item, inode);
1107 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001108 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001109 ret = 0;
1110failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001111 btrfs_free_path(path);
1112 return ret;
1113}
1114
1115
1116static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1117 struct btrfs_root *root,
1118 struct inode *dir,
1119 struct dentry *dentry)
1120{
1121 struct btrfs_path *path;
1122 const char *name = dentry->d_name.name;
1123 int name_len = dentry->d_name.len;
1124 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001125 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001126 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001127 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001128 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001129
1130 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001131 if (!path) {
1132 ret = -ENOMEM;
1133 goto err;
1134 }
1135
Chris Mason39279cc2007-06-12 06:35:45 -04001136 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1137 name, name_len, -1);
1138 if (IS_ERR(di)) {
1139 ret = PTR_ERR(di);
1140 goto err;
1141 }
1142 if (!di) {
1143 ret = -ENOENT;
1144 goto err;
1145 }
Chris Mason5f39d392007-10-15 16:14:19 -04001146 leaf = path->nodes[0];
1147 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001148 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001149 if (ret)
1150 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001151 btrfs_release_path(root, path);
1152
Josef Bacikaec74772008-07-24 12:12:38 -04001153 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1154 dentry->d_inode->i_ino,
1155 dentry->d_parent->d_inode->i_ino, &index);
1156 if (ret) {
1157 printk("failed to delete reference to %.*s, "
1158 "inode %lu parent %lu\n", name_len, name,
1159 dentry->d_inode->i_ino,
1160 dentry->d_parent->d_inode->i_ino);
1161 goto err;
1162 }
1163
Chris Mason39279cc2007-06-12 06:35:45 -04001164 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001165 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001166 if (IS_ERR(di)) {
1167 ret = PTR_ERR(di);
1168 goto err;
1169 }
1170 if (!di) {
1171 ret = -ENOENT;
1172 goto err;
1173 }
1174 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001175 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001176
1177 dentry->d_inode->i_ctime = dir->i_ctime;
1178err:
1179 btrfs_free_path(path);
1180 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001181 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001182 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001183 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001184#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1185 dentry->d_inode->i_nlink--;
1186#else
Chris Mason39279cc2007-06-12 06:35:45 -04001187 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001188#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001189 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001190 dir->i_sb->s_dirt = 1;
1191 }
1192 return ret;
1193}
1194
1195static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1196{
1197 struct btrfs_root *root;
1198 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001199 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001200 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001201 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001202
1203 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001204
1205 ret = btrfs_check_free_space(root, 1, 1);
1206 if (ret)
1207 goto fail;
1208
Chris Mason39279cc2007-06-12 06:35:45 -04001209 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001210
Chris Mason39279cc2007-06-12 06:35:45 -04001211 btrfs_set_trans_block_group(trans, dir);
1212 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001213
1214 if (inode->i_nlink == 0)
1215 ret = btrfs_orphan_add(trans, inode);
1216
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001217 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001218
Chris Mason89ce8a62008-06-25 16:01:31 -04001219 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001220fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001221 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001222 return ret;
1223}
1224
1225static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1226{
1227 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001228 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001229 int ret;
1230 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001231 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001232 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001233
Chris Mason925baed2008-06-25 16:01:30 -04001234 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001235 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001236 }
Yan134d4512007-10-25 15:49:25 -04001237
Chris Mason1832a6d2007-12-21 16:27:21 -05001238 ret = btrfs_check_free_space(root, 1, 1);
1239 if (ret)
1240 goto fail;
1241
Chris Mason39279cc2007-06-12 06:35:45 -04001242 trans = btrfs_start_transaction(root, 1);
1243 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001244
Josef Bacik7b128762008-07-24 12:17:14 -04001245 err = btrfs_orphan_add(trans, inode);
1246 if (err)
1247 goto fail_trans;
1248
Chris Mason39279cc2007-06-12 06:35:45 -04001249 /* now the directory is empty */
1250 err = btrfs_unlink_trans(trans, root, dir, dentry);
1251 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001252 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001253 }
Chris Mason39544012007-12-12 14:38:19 -05001254
Josef Bacik7b128762008-07-24 12:17:14 -04001255fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001256 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001257 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001258fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001259 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001260
Chris Mason39279cc2007-06-12 06:35:45 -04001261 if (ret && !err)
1262 err = ret;
1263 return err;
1264}
1265
Chris Mason39279cc2007-06-12 06:35:45 -04001266/*
Chris Mason39279cc2007-06-12 06:35:45 -04001267 * this can truncate away extent items, csum items and directory items.
1268 * It starts at a high offset and removes keys until it can't find
1269 * any higher than i_size.
1270 *
1271 * csum items that cross the new i_size are truncated to the new size
1272 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001273 *
1274 * min_type is the minimum key type to truncate down to. If set to 0, this
1275 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001276 */
1277static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001279 struct inode *inode,
1280 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001281{
1282 int ret;
1283 struct btrfs_path *path;
1284 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001285 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001286 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001287 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001288 struct btrfs_file_extent_item *fi;
1289 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001290 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001291 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001292 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001293 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001294 int found_extent;
1295 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001296 int pending_del_nr = 0;
1297 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001298 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001299 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001300
Chris Mason3b951512008-04-17 11:29:12 -04001301 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001302 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001303 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001304 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001305
Chris Mason39279cc2007-06-12 06:35:45 -04001306 /* FIXME, add redo link to tree so we don't leak on crash */
1307 key.objectid = inode->i_ino;
1308 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001309 key.type = (u8)-1;
1310
Chris Mason85e21ba2008-01-29 15:11:36 -05001311 btrfs_init_path(path);
1312search_again:
1313 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1314 if (ret < 0) {
1315 goto error;
1316 }
1317 if (ret > 0) {
1318 BUG_ON(path->slots[0] == 0);
1319 path->slots[0]--;
1320 }
1321
Chris Mason39279cc2007-06-12 06:35:45 -04001322 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001323 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001324 leaf = path->nodes[0];
1325 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1326 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001327
Chris Mason5f39d392007-10-15 16:14:19 -04001328 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001329 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001330
Chris Mason85e21ba2008-01-29 15:11:36 -05001331 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001332 break;
1333
Chris Mason5f39d392007-10-15 16:14:19 -04001334 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001335 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001336 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001337 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001338 extent_type = btrfs_file_extent_type(leaf, fi);
1339 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001340 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001341 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1343 struct btrfs_item *item = btrfs_item_nr(leaf,
1344 path->slots[0]);
1345 item_end += btrfs_file_extent_inline_len(leaf,
1346 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001347 }
Yan008630c2007-11-07 13:31:09 -05001348 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001349 }
1350 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1351 ret = btrfs_csum_truncate(trans, root, path,
1352 inode->i_size);
1353 BUG_ON(ret);
1354 }
Yan008630c2007-11-07 13:31:09 -05001355 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001356 if (found_type == BTRFS_DIR_ITEM_KEY) {
1357 found_type = BTRFS_INODE_ITEM_KEY;
1358 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1359 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001360 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1361 found_type = BTRFS_XATTR_ITEM_KEY;
1362 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1363 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001364 } else if (found_type) {
1365 found_type--;
1366 } else {
1367 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001368 }
Yana61721d2007-09-17 11:08:38 -04001369 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001370 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001371 }
Chris Mason5f39d392007-10-15 16:14:19 -04001372 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001373 del_item = 1;
1374 else
1375 del_item = 0;
1376 found_extent = 0;
1377
1378 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001379 if (found_type != BTRFS_EXTENT_DATA_KEY)
1380 goto delete;
1381
1382 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001383 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001384 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001385 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001386 u64 orig_num_bytes =
1387 btrfs_file_extent_num_bytes(leaf, fi);
1388 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001389 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001390 extent_num_bytes = extent_num_bytes &
1391 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001392 btrfs_set_file_extent_num_bytes(leaf, fi,
1393 extent_num_bytes);
1394 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001395 extent_num_bytes);
1396 if (extent_start != 0)
1397 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001398 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001399 } else {
Chris Masondb945352007-10-15 16:15:53 -04001400 extent_num_bytes =
1401 btrfs_file_extent_disk_num_bytes(leaf,
1402 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001403 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001404 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001405 if (extent_start != 0) {
1406 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001407 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001408 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001409 root_gen = btrfs_header_generation(leaf);
1410 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001411 }
Chris Mason90692182008-02-08 13:49:28 -05001412 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1413 if (!del_item) {
1414 u32 newsize = inode->i_size - found_key.offset;
1415 dec_i_blocks(inode, item_end + 1 -
1416 found_key.offset - newsize);
1417 newsize =
1418 btrfs_file_extent_calc_inline_size(newsize);
1419 ret = btrfs_truncate_item(trans, root, path,
1420 newsize, 1);
1421 BUG_ON(ret);
1422 } else {
1423 dec_i_blocks(inode, item_end + 1 -
1424 found_key.offset);
1425 }
Chris Mason39279cc2007-06-12 06:35:45 -04001426 }
Chris Mason179e29e2007-11-01 11:28:41 -04001427delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001428 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001429 if (!pending_del_nr) {
1430 /* no pending yet, add ourselves */
1431 pending_del_slot = path->slots[0];
1432 pending_del_nr = 1;
1433 } else if (pending_del_nr &&
1434 path->slots[0] + 1 == pending_del_slot) {
1435 /* hop on the pending chunk */
1436 pending_del_nr++;
1437 pending_del_slot = path->slots[0];
1438 } else {
1439 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1440 }
Chris Mason39279cc2007-06-12 06:35:45 -04001441 } else {
1442 break;
1443 }
Chris Mason39279cc2007-06-12 06:35:45 -04001444 if (found_extent) {
1445 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001446 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001447 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001448 root_gen, inode->i_ino,
1449 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001450 BUG_ON(ret);
1451 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001452next:
1453 if (path->slots[0] == 0) {
1454 if (pending_del_nr)
1455 goto del_pending;
1456 btrfs_release_path(root, path);
1457 goto search_again;
1458 }
1459
1460 path->slots[0]--;
1461 if (pending_del_nr &&
1462 path->slots[0] + 1 != pending_del_slot) {
1463 struct btrfs_key debug;
1464del_pending:
1465 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1466 pending_del_slot);
1467 ret = btrfs_del_items(trans, root, path,
1468 pending_del_slot,
1469 pending_del_nr);
1470 BUG_ON(ret);
1471 pending_del_nr = 0;
1472 btrfs_release_path(root, path);
1473 goto search_again;
1474 }
Chris Mason39279cc2007-06-12 06:35:45 -04001475 }
1476 ret = 0;
1477error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001478 if (pending_del_nr) {
1479 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1480 pending_del_nr);
1481 }
Chris Mason39279cc2007-06-12 06:35:45 -04001482 btrfs_free_path(path);
1483 inode->i_sb->s_dirt = 1;
1484 return ret;
1485}
1486
Chris Masona52d9a82007-08-27 16:49:44 -04001487/*
1488 * taken from block_truncate_page, but does cow as it zeros out
1489 * any bytes left in the last page in the file.
1490 */
1491static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1492{
1493 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001494 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001495 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1496 struct btrfs_ordered_extent *ordered;
1497 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001498 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001499 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1500 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1501 struct page *page;
1502 int ret = 0;
1503 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001504 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001505
1506 if ((offset & (blocksize - 1)) == 0)
1507 goto out;
1508
1509 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001510again:
Chris Masona52d9a82007-08-27 16:49:44 -04001511 page = grab_cache_page(mapping, index);
1512 if (!page)
1513 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001514
1515 page_start = page_offset(page);
1516 page_end = page_start + PAGE_CACHE_SIZE - 1;
1517
Chris Masona52d9a82007-08-27 16:49:44 -04001518 if (!PageUptodate(page)) {
1519 ret = btrfs_readpage(NULL, page);
1520 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001521 if (page->mapping != mapping) {
1522 unlock_page(page);
1523 page_cache_release(page);
1524 goto again;
1525 }
Chris Masona52d9a82007-08-27 16:49:44 -04001526 if (!PageUptodate(page)) {
1527 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001528 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001529 }
1530 }
Chris Mason211c17f2008-05-15 09:13:45 -04001531 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001532
1533 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1534 set_page_extent_mapped(page);
1535
1536 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1537 if (ordered) {
1538 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1539 unlock_page(page);
1540 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001541 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001542 btrfs_put_ordered_extent(ordered);
1543 goto again;
1544 }
1545
Chris Masonea8c2812008-08-04 23:17:27 -04001546 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001547 ret = 0;
1548 if (offset != PAGE_CACHE_SIZE) {
1549 kaddr = kmap(page);
1550 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1551 flush_dcache_page(page);
1552 kunmap(page);
1553 }
Chris Mason247e7432008-07-17 12:53:51 -04001554 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001555 set_page_dirty(page);
1556 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001557
Chris Mason89642222008-07-24 09:41:53 -04001558out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001559 unlock_page(page);
1560 page_cache_release(page);
1561out:
1562 return ret;
1563}
1564
1565static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1566{
1567 struct inode *inode = dentry->d_inode;
1568 int err;
1569
1570 err = inode_change_ok(inode, attr);
1571 if (err)
1572 return err;
1573
1574 if (S_ISREG(inode->i_mode) &&
1575 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1576 struct btrfs_trans_handle *trans;
1577 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001578 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001579
Chris Mason5f39d392007-10-15 16:14:19 -04001580 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001581 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001582 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001583 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001584 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001585
Chris Mason1b0f7c22008-01-30 14:33:02 -05001586 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001587 goto out;
1588
Chris Mason1832a6d2007-12-21 16:27:21 -05001589 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001590 if (err)
1591 goto fail;
1592
Chris Mason39279cc2007-06-12 06:35:45 -04001593 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1594
Chris Mason5f564062008-01-22 16:47:59 -05001595 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001596 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1597 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001598
Chris Mason39279cc2007-06-12 06:35:45 -04001599 trans = btrfs_start_transaction(root, 1);
1600 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001601 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001602 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001603 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001604 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001605
Chris Mason179e29e2007-11-01 11:28:41 -04001606 if (alloc_hint != EXTENT_MAP_INLINE) {
1607 err = btrfs_insert_file_extent(trans, root,
1608 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001609 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001610 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001611 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001612 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001613 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001614 }
Chris Masonee6e6502008-07-17 12:54:40 -04001615 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001616 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001617 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001618 if (err)
1619 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001620 }
1621out:
1622 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001623
1624 if (!err && ((attr->ia_valid & ATTR_MODE)))
1625 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001626fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001627 return err;
1628}
Chris Mason61295eb2008-01-14 16:24:38 -05001629
Chris Mason39279cc2007-06-12 06:35:45 -04001630void btrfs_delete_inode(struct inode *inode)
1631{
1632 struct btrfs_trans_handle *trans;
1633 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001634 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001635 int ret;
1636
1637 truncate_inode_pages(&inode->i_data, 0);
1638 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001639 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001640 goto no_delete;
1641 }
Chris Mason4a096752008-07-21 10:29:44 -04001642 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001643
Chris Masondbe674a2008-07-17 12:54:05 -04001644 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001645 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001646
Chris Mason39279cc2007-06-12 06:35:45 -04001647 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001648 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001649 if (ret) {
1650 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001651 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001652 }
1653
1654 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001655
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001656 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001657 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001658
Chris Mason39279cc2007-06-12 06:35:45 -04001659 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001660 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001661 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001662
1663no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001664 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001665 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001666 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001667no_delete:
1668 clear_inode(inode);
1669}
1670
1671/*
1672 * this returns the key found in the dir entry in the location pointer.
1673 * If no dir entries were found, location->objectid is 0.
1674 */
1675static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1676 struct btrfs_key *location)
1677{
1678 const char *name = dentry->d_name.name;
1679 int namelen = dentry->d_name.len;
1680 struct btrfs_dir_item *di;
1681 struct btrfs_path *path;
1682 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001683 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001684
Chris Mason39544012007-12-12 14:38:19 -05001685 if (namelen == 1 && strcmp(name, ".") == 0) {
1686 location->objectid = dir->i_ino;
1687 location->type = BTRFS_INODE_ITEM_KEY;
1688 location->offset = 0;
1689 return 0;
1690 }
Chris Mason39279cc2007-06-12 06:35:45 -04001691 path = btrfs_alloc_path();
1692 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001693
Chris Mason7a720532007-12-13 09:06:59 -05001694 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001695 struct btrfs_key key;
1696 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001697 int slot;
1698
1699 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001700 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001701 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001702 if (ret < 0 || path->slots[0] == 0)
1703 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001704 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1705 BUG_ON(ret == 0);
1706 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001707 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001708 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001709
1710 btrfs_item_key_to_cpu(leaf, &key, slot);
1711 if (key.objectid != dir->i_ino ||
1712 key.type != BTRFS_INODE_REF_KEY) {
1713 goto out_err;
1714 }
1715 location->objectid = key.offset;
1716 location->type = BTRFS_INODE_ITEM_KEY;
1717 location->offset = 0;
1718 goto out;
1719 }
1720
Chris Mason39279cc2007-06-12 06:35:45 -04001721 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1722 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001723 if (IS_ERR(di))
1724 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001725 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001726 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001727 }
Chris Mason5f39d392007-10-15 16:14:19 -04001728 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001729out:
Chris Mason39279cc2007-06-12 06:35:45 -04001730 btrfs_free_path(path);
1731 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001732out_err:
1733 location->objectid = 0;
1734 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001735}
1736
1737/*
1738 * when we hit a tree root in a directory, the btrfs part of the inode
1739 * needs to be changed to reflect the root directory of the tree root. This
1740 * is kind of like crossing a mount point.
1741 */
1742static int fixup_tree_root_location(struct btrfs_root *root,
1743 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001744 struct btrfs_root **sub_root,
1745 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001746{
Chris Mason39279cc2007-06-12 06:35:45 -04001747 struct btrfs_root_item *ri;
1748
1749 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1750 return 0;
1751 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1752 return 0;
1753
Josef Bacik58176a92007-08-29 15:47:34 -04001754 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1755 dentry->d_name.name,
1756 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001757 if (IS_ERR(*sub_root))
1758 return PTR_ERR(*sub_root);
1759
1760 ri = &(*sub_root)->root_item;
1761 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001762 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1763 location->offset = 0;
1764
Chris Mason39279cc2007-06-12 06:35:45 -04001765 return 0;
1766}
1767
1768static int btrfs_init_locked_inode(struct inode *inode, void *p)
1769{
1770 struct btrfs_iget_args *args = p;
1771 inode->i_ino = args->ino;
1772 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001773 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001774 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001775 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001776 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1777 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001778 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001779 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1780 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001781 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001782 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001783 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001784 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001785 return 0;
1786}
1787
1788static int btrfs_find_actor(struct inode *inode, void *opaque)
1789{
1790 struct btrfs_iget_args *args = opaque;
1791 return (args->ino == inode->i_ino &&
1792 args->root == BTRFS_I(inode)->root);
1793}
1794
Chris Masondc17ff82008-01-08 15:46:30 -05001795struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1796 u64 root_objectid)
1797{
1798 struct btrfs_iget_args args;
1799 args.ino = objectid;
1800 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1801
1802 if (!args.root)
1803 return NULL;
1804
1805 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1806}
1807
Chris Mason39279cc2007-06-12 06:35:45 -04001808struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1809 struct btrfs_root *root)
1810{
1811 struct inode *inode;
1812 struct btrfs_iget_args args;
1813 args.ino = objectid;
1814 args.root = root;
1815
1816 inode = iget5_locked(s, objectid, btrfs_find_actor,
1817 btrfs_init_locked_inode,
1818 (void *)&args);
1819 return inode;
1820}
1821
1822static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1823 struct nameidata *nd)
1824{
1825 struct inode * inode;
1826 struct btrfs_inode *bi = BTRFS_I(dir);
1827 struct btrfs_root *root = bi->root;
1828 struct btrfs_root *sub_root = root;
1829 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001830 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001831
1832 if (dentry->d_name.len > BTRFS_NAME_LEN)
1833 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001834
Chris Mason39279cc2007-06-12 06:35:45 -04001835 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001836
Chris Mason39279cc2007-06-12 06:35:45 -04001837 if (ret < 0)
1838 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001839
Chris Mason39279cc2007-06-12 06:35:45 -04001840 inode = NULL;
1841 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001842 ret = fixup_tree_root_location(root, &location, &sub_root,
1843 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001844 if (ret < 0)
1845 return ERR_PTR(ret);
1846 if (ret > 0)
1847 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001848
Chris Mason39279cc2007-06-12 06:35:45 -04001849 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1850 sub_root);
1851 if (!inode)
1852 return ERR_PTR(-EACCES);
1853 if (inode->i_state & I_NEW) {
1854 /* the inode and parent dir are two different roots */
1855 if (sub_root != root) {
1856 igrab(inode);
1857 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001858 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001859 }
1860 BTRFS_I(inode)->root = sub_root;
1861 memcpy(&BTRFS_I(inode)->location, &location,
1862 sizeof(location));
1863 btrfs_read_locked_inode(inode);
1864 unlock_new_inode(inode);
1865 }
1866 }
Josef Bacik7b128762008-07-24 12:17:14 -04001867
1868 if (unlikely(do_orphan))
1869 btrfs_orphan_cleanup(sub_root);
1870
Chris Mason39279cc2007-06-12 06:35:45 -04001871 return d_splice_alias(inode, dentry);
1872}
1873
Chris Mason39279cc2007-06-12 06:35:45 -04001874static unsigned char btrfs_filetype_table[] = {
1875 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1876};
1877
1878static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1879{
Chris Mason6da6aba2007-12-18 16:15:09 -05001880 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001881 struct btrfs_root *root = BTRFS_I(inode)->root;
1882 struct btrfs_item *item;
1883 struct btrfs_dir_item *di;
1884 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001885 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001886 struct btrfs_path *path;
1887 int ret;
1888 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001889 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001890 int slot;
1891 int advance;
1892 unsigned char d_type;
1893 int over = 0;
1894 u32 di_cur;
1895 u32 di_total;
1896 u32 di_len;
1897 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001898 char tmp_name[32];
1899 char *name_ptr;
1900 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001901
1902 /* FIXME, use a real flag for deciding about the key type */
1903 if (root->fs_info->tree_root == root)
1904 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001905
Chris Mason39544012007-12-12 14:38:19 -05001906 /* special case for "." */
1907 if (filp->f_pos == 0) {
1908 over = filldir(dirent, ".", 1,
1909 1, inode->i_ino,
1910 DT_DIR);
1911 if (over)
1912 return 0;
1913 filp->f_pos = 1;
1914 }
1915
Chris Mason39279cc2007-06-12 06:35:45 -04001916 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001917 path = btrfs_alloc_path();
1918 path->reada = 2;
1919
1920 /* special case for .., just use the back ref */
1921 if (filp->f_pos == 1) {
1922 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001923 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001924 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001925 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001926 btrfs_release_path(root, path);
1927 goto read_dir_items;
1928 }
Yan445dceb2008-07-24 12:19:32 -04001929 BUG_ON(ret == 0);
1930 leaf = path->nodes[0];
1931 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001932 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1933 btrfs_release_path(root, path);
1934 if (found_key.objectid != key.objectid ||
1935 found_key.type != BTRFS_INODE_REF_KEY)
1936 goto read_dir_items;
1937 over = filldir(dirent, "..", 2,
1938 2, found_key.offset, DT_DIR);
1939 if (over)
1940 goto nopos;
1941 filp->f_pos = 2;
1942 }
1943
1944read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001945 btrfs_set_key_type(&key, key_type);
1946 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001947
Chris Mason39279cc2007-06-12 06:35:45 -04001948 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1949 if (ret < 0)
1950 goto err;
1951 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001952 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001953 leaf = path->nodes[0];
1954 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001955 slot = path->slots[0];
1956 if (advance || slot >= nritems) {
1957 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001958 ret = btrfs_next_leaf(root, path);
1959 if (ret)
1960 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001961 leaf = path->nodes[0];
1962 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001963 slot = path->slots[0];
1964 } else {
1965 slot++;
1966 path->slots[0]++;
1967 }
1968 }
1969 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001970 item = btrfs_item_nr(leaf, slot);
1971 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1972
1973 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001974 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001975 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001976 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001977 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001978 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001979
1980 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001981 advance = 1;
1982 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1983 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001984 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001985 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001986 struct btrfs_key location;
1987
1988 name_len = btrfs_dir_name_len(leaf, di);
1989 if (name_len < 32) {
1990 name_ptr = tmp_name;
1991 } else {
1992 name_ptr = kmalloc(name_len, GFP_NOFS);
1993 BUG_ON(!name_ptr);
1994 }
1995 read_extent_buffer(leaf, name_ptr,
1996 (unsigned long)(di + 1), name_len);
1997
1998 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1999 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002000 over = filldir(dirent, name_ptr, name_len,
2001 found_key.offset,
2002 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002003 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002004
2005 if (name_ptr != tmp_name)
2006 kfree(name_ptr);
2007
Chris Mason39279cc2007-06-12 06:35:45 -04002008 if (over)
2009 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002010 di_len = btrfs_dir_name_len(leaf, di) +
2011 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002012 di_cur += di_len;
2013 di = (struct btrfs_dir_item *)((char *)di + di_len);
2014 }
2015 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002016 if (key_type == BTRFS_DIR_INDEX_KEY)
2017 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2018 else
2019 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002020nopos:
2021 ret = 0;
2022err:
Chris Mason39279cc2007-06-12 06:35:45 -04002023 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002024 return ret;
2025}
2026
2027int btrfs_write_inode(struct inode *inode, int wait)
2028{
2029 struct btrfs_root *root = BTRFS_I(inode)->root;
2030 struct btrfs_trans_handle *trans;
2031 int ret = 0;
2032
2033 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002034 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002035 btrfs_set_trans_block_group(trans, inode);
2036 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002037 }
2038 return ret;
2039}
2040
2041/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002042 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002043 * inode changes. But, it is most likely to find the inode in cache.
2044 * FIXME, needs more benchmarking...there are no reasons other than performance
2045 * to keep or drop this code.
2046 */
2047void btrfs_dirty_inode(struct inode *inode)
2048{
2049 struct btrfs_root *root = BTRFS_I(inode)->root;
2050 struct btrfs_trans_handle *trans;
2051
Chris Masonf9295742008-07-17 12:54:14 -04002052 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002053 btrfs_set_trans_block_group(trans, inode);
2054 btrfs_update_inode(trans, root, inode);
2055 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002056}
2057
Josef Bacikaec74772008-07-24 12:12:38 -04002058static int btrfs_set_inode_index_count(struct inode *inode)
2059{
2060 struct btrfs_root *root = BTRFS_I(inode)->root;
2061 struct btrfs_key key, found_key;
2062 struct btrfs_path *path;
2063 struct extent_buffer *leaf;
2064 int ret;
2065
2066 key.objectid = inode->i_ino;
2067 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2068 key.offset = (u64)-1;
2069
2070 path = btrfs_alloc_path();
2071 if (!path)
2072 return -ENOMEM;
2073
2074 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2075 if (ret < 0)
2076 goto out;
2077 /* FIXME: we should be able to handle this */
2078 if (ret == 0)
2079 goto out;
2080 ret = 0;
2081
2082 /*
2083 * MAGIC NUMBER EXPLANATION:
2084 * since we search a directory based on f_pos we have to start at 2
2085 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2086 * else has to start at 2
2087 */
2088 if (path->slots[0] == 0) {
2089 BTRFS_I(inode)->index_cnt = 2;
2090 goto out;
2091 }
2092
2093 path->slots[0]--;
2094
2095 leaf = path->nodes[0];
2096 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2097
2098 if (found_key.objectid != inode->i_ino ||
2099 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2100 BTRFS_I(inode)->index_cnt = 2;
2101 goto out;
2102 }
2103
2104 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2105out:
2106 btrfs_free_path(path);
2107 return ret;
2108}
2109
2110static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2111{
2112 int ret = 0;
2113
2114 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2115 ret = btrfs_set_inode_index_count(dir);
2116 if (ret)
2117 return ret;
2118 }
2119
2120 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2121 BTRFS_I(dir)->index_cnt++;
2122
2123 return ret;
2124}
2125
Chris Mason39279cc2007-06-12 06:35:45 -04002126static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2127 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002128 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002129 const char *name, int name_len,
2130 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002131 u64 objectid,
2132 struct btrfs_block_group_cache *group,
2133 int mode)
2134{
2135 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002136 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002137 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002138 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002139 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002140 struct btrfs_inode_ref *ref;
2141 struct btrfs_key key[2];
2142 u32 sizes[2];
2143 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002144 int ret;
2145 int owner;
2146
Chris Mason5f39d392007-10-15 16:14:19 -04002147 path = btrfs_alloc_path();
2148 BUG_ON(!path);
2149
Chris Mason39279cc2007-06-12 06:35:45 -04002150 inode = new_inode(root->fs_info->sb);
2151 if (!inode)
2152 return ERR_PTR(-ENOMEM);
2153
Josef Bacikaec74772008-07-24 12:12:38 -04002154 if (dir) {
2155 ret = btrfs_set_inode_index(dir, inode);
2156 if (ret)
2157 return ERR_PTR(ret);
2158 } else {
2159 BTRFS_I(inode)->index = 0;
2160 }
2161 /*
2162 * index_cnt is ignored for everything but a dir,
2163 * btrfs_get_inode_index_count has an explanation for the magic
2164 * number
2165 */
2166 BTRFS_I(inode)->index_cnt = 2;
2167
Chris Masond1310b22008-01-24 16:13:08 -05002168 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2169 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002170 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002171 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2172 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002173 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonea8c2812008-08-04 23:17:27 -04002174 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002175 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002176 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002177 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002178 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002179 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002180
Chris Mason39279cc2007-06-12 06:35:45 -04002181 if (mode & S_IFDIR)
2182 owner = 0;
2183 else
2184 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002185 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002186 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002187 if (!new_inode_group) {
2188 printk("find_block group failed\n");
2189 new_inode_group = group;
2190 }
2191 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002192 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002193
2194 key[0].objectid = objectid;
2195 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2196 key[0].offset = 0;
2197
2198 key[1].objectid = objectid;
2199 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2200 key[1].offset = ref_objectid;
2201
2202 sizes[0] = sizeof(struct btrfs_inode_item);
2203 sizes[1] = name_len + sizeof(*ref);
2204
2205 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2206 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002207 goto fail;
2208
Chris Mason9c583092008-01-29 15:15:18 -05002209 if (objectid > root->highest_inode)
2210 root->highest_inode = objectid;
2211
Chris Mason39279cc2007-06-12 06:35:45 -04002212 inode->i_uid = current->fsuid;
2213 inode->i_gid = current->fsgid;
2214 inode->i_mode = mode;
2215 inode->i_ino = objectid;
2216 inode->i_blocks = 0;
2217 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002218 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2219 struct btrfs_inode_item);
2220 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002221
2222 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2223 struct btrfs_inode_ref);
2224 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002225 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002226 ptr = (unsigned long)(ref + 1);
2227 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2228
Chris Mason5f39d392007-10-15 16:14:19 -04002229 btrfs_mark_buffer_dirty(path->nodes[0]);
2230 btrfs_free_path(path);
2231
Chris Mason39279cc2007-06-12 06:35:45 -04002232 location = &BTRFS_I(inode)->location;
2233 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002234 location->offset = 0;
2235 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2236
Chris Mason39279cc2007-06-12 06:35:45 -04002237 insert_inode_hash(inode);
2238 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002239fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002240 if (dir)
2241 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002242 btrfs_free_path(path);
2243 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002244}
2245
2246static inline u8 btrfs_inode_type(struct inode *inode)
2247{
2248 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2249}
2250
2251static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002252 struct dentry *dentry, struct inode *inode,
2253 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002254{
2255 int ret;
2256 struct btrfs_key key;
2257 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002258 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002259
Chris Mason39279cc2007-06-12 06:35:45 -04002260 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002261 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2262 key.offset = 0;
2263
2264 ret = btrfs_insert_dir_item(trans, root,
2265 dentry->d_name.name, dentry->d_name.len,
2266 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002267 &key, btrfs_inode_type(inode),
2268 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002269 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002270 if (add_backref) {
2271 ret = btrfs_insert_inode_ref(trans, root,
2272 dentry->d_name.name,
2273 dentry->d_name.len,
2274 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002275 parent_inode->i_ino,
2276 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002277 }
Chris Masondbe674a2008-07-17 12:54:05 -04002278 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2279 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002280 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002281 ret = btrfs_update_inode(trans, root,
2282 dentry->d_parent->d_inode);
2283 }
2284 return ret;
2285}
2286
2287static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002288 struct dentry *dentry, struct inode *inode,
2289 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002290{
Chris Mason9c583092008-01-29 15:15:18 -05002291 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002292 if (!err) {
2293 d_instantiate(dentry, inode);
2294 return 0;
2295 }
2296 if (err > 0)
2297 err = -EEXIST;
2298 return err;
2299}
2300
Josef Bacik618e21d2007-07-11 10:18:17 -04002301static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2302 int mode, dev_t rdev)
2303{
2304 struct btrfs_trans_handle *trans;
2305 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002306 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002307 int err;
2308 int drop_inode = 0;
2309 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002310 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002311
2312 if (!new_valid_dev(rdev))
2313 return -EINVAL;
2314
Chris Mason1832a6d2007-12-21 16:27:21 -05002315 err = btrfs_check_free_space(root, 1, 0);
2316 if (err)
2317 goto fail;
2318
Josef Bacik618e21d2007-07-11 10:18:17 -04002319 trans = btrfs_start_transaction(root, 1);
2320 btrfs_set_trans_block_group(trans, dir);
2321
2322 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2323 if (err) {
2324 err = -ENOSPC;
2325 goto out_unlock;
2326 }
2327
Josef Bacikaec74772008-07-24 12:12:38 -04002328 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002329 dentry->d_name.len,
2330 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002331 BTRFS_I(dir)->block_group, mode);
2332 err = PTR_ERR(inode);
2333 if (IS_ERR(inode))
2334 goto out_unlock;
2335
Josef Bacik33268ea2008-07-24 12:16:36 -04002336 err = btrfs_init_acl(inode, dir);
2337 if (err) {
2338 drop_inode = 1;
2339 goto out_unlock;
2340 }
2341
Josef Bacik618e21d2007-07-11 10:18:17 -04002342 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002343 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002344 if (err)
2345 drop_inode = 1;
2346 else {
2347 inode->i_op = &btrfs_special_inode_operations;
2348 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002349 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002350 }
2351 dir->i_sb->s_dirt = 1;
2352 btrfs_update_inode_block_group(trans, inode);
2353 btrfs_update_inode_block_group(trans, dir);
2354out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002355 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002356 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002357fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002358 if (drop_inode) {
2359 inode_dec_link_count(inode);
2360 iput(inode);
2361 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002362 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002363 return err;
2364}
2365
Chris Mason39279cc2007-06-12 06:35:45 -04002366static int btrfs_create(struct inode *dir, struct dentry *dentry,
2367 int mode, struct nameidata *nd)
2368{
2369 struct btrfs_trans_handle *trans;
2370 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002371 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002372 int err;
2373 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002374 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002375 u64 objectid;
2376
Chris Mason1832a6d2007-12-21 16:27:21 -05002377 err = btrfs_check_free_space(root, 1, 0);
2378 if (err)
2379 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002380 trans = btrfs_start_transaction(root, 1);
2381 btrfs_set_trans_block_group(trans, dir);
2382
2383 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2384 if (err) {
2385 err = -ENOSPC;
2386 goto out_unlock;
2387 }
2388
Josef Bacikaec74772008-07-24 12:12:38 -04002389 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002390 dentry->d_name.len,
2391 dentry->d_parent->d_inode->i_ino,
2392 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002393 err = PTR_ERR(inode);
2394 if (IS_ERR(inode))
2395 goto out_unlock;
2396
Josef Bacik33268ea2008-07-24 12:16:36 -04002397 err = btrfs_init_acl(inode, dir);
2398 if (err) {
2399 drop_inode = 1;
2400 goto out_unlock;
2401 }
2402
Chris Mason39279cc2007-06-12 06:35:45 -04002403 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002404 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002405 if (err)
2406 drop_inode = 1;
2407 else {
2408 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002409 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002410 inode->i_fop = &btrfs_file_operations;
2411 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002412 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2413 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002414 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002415 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2416 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002417 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002418 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002419 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002420 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002421 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002422 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002423 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002424 }
2425 dir->i_sb->s_dirt = 1;
2426 btrfs_update_inode_block_group(trans, inode);
2427 btrfs_update_inode_block_group(trans, dir);
2428out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002429 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002430 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002431fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002432 if (drop_inode) {
2433 inode_dec_link_count(inode);
2434 iput(inode);
2435 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002436 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002437 return err;
2438}
2439
2440static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2441 struct dentry *dentry)
2442{
2443 struct btrfs_trans_handle *trans;
2444 struct btrfs_root *root = BTRFS_I(dir)->root;
2445 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002446 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002447 int err;
2448 int drop_inode = 0;
2449
2450 if (inode->i_nlink == 0)
2451 return -ENOENT;
2452
Chris Mason6da6aba2007-12-18 16:15:09 -05002453#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2454 inode->i_nlink++;
2455#else
Chris Mason39279cc2007-06-12 06:35:45 -04002456 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002457#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002458 err = btrfs_check_free_space(root, 1, 0);
2459 if (err)
2460 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002461 err = btrfs_set_inode_index(dir, inode);
2462 if (err)
2463 goto fail;
2464
Chris Mason39279cc2007-06-12 06:35:45 -04002465 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002466
Chris Mason39279cc2007-06-12 06:35:45 -04002467 btrfs_set_trans_block_group(trans, dir);
2468 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002469
Chris Mason9c583092008-01-29 15:15:18 -05002470 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002471
Chris Mason39279cc2007-06-12 06:35:45 -04002472 if (err)
2473 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002474
Chris Mason39279cc2007-06-12 06:35:45 -04002475 dir->i_sb->s_dirt = 1;
2476 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002477 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002478
Chris Mason54aa1f42007-06-22 14:16:25 -04002479 if (err)
2480 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002481
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002482 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002483 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002484fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002485 if (drop_inode) {
2486 inode_dec_link_count(inode);
2487 iput(inode);
2488 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002489 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002490 return err;
2491}
2492
Chris Mason39279cc2007-06-12 06:35:45 -04002493static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2494{
Chris Masonb9d86662008-05-02 16:13:49 -04002495 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002496 struct btrfs_trans_handle *trans;
2497 struct btrfs_root *root = BTRFS_I(dir)->root;
2498 int err = 0;
2499 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002500 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002501 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002502
Chris Mason1832a6d2007-12-21 16:27:21 -05002503 err = btrfs_check_free_space(root, 1, 0);
2504 if (err)
2505 goto out_unlock;
2506
Chris Mason39279cc2007-06-12 06:35:45 -04002507 trans = btrfs_start_transaction(root, 1);
2508 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Chris Mason39279cc2007-06-12 06:35:45 -04002510 if (IS_ERR(trans)) {
2511 err = PTR_ERR(trans);
2512 goto out_unlock;
2513 }
2514
2515 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2516 if (err) {
2517 err = -ENOSPC;
2518 goto out_unlock;
2519 }
2520
Josef Bacikaec74772008-07-24 12:12:38 -04002521 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002522 dentry->d_name.len,
2523 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002524 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2525 if (IS_ERR(inode)) {
2526 err = PTR_ERR(inode);
2527 goto out_fail;
2528 }
Chris Mason5f39d392007-10-15 16:14:19 -04002529
Chris Mason39279cc2007-06-12 06:35:45 -04002530 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002531
2532 err = btrfs_init_acl(inode, dir);
2533 if (err)
2534 goto out_fail;
2535
Chris Mason39279cc2007-06-12 06:35:45 -04002536 inode->i_op = &btrfs_dir_inode_operations;
2537 inode->i_fop = &btrfs_dir_file_operations;
2538 btrfs_set_trans_block_group(trans, inode);
2539
Chris Masondbe674a2008-07-17 12:54:05 -04002540 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002541 err = btrfs_update_inode(trans, root, inode);
2542 if (err)
2543 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002544
Chris Mason9c583092008-01-29 15:15:18 -05002545 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002546 if (err)
2547 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002548
Chris Mason39279cc2007-06-12 06:35:45 -04002549 d_instantiate(dentry, inode);
2550 drop_on_err = 0;
2551 dir->i_sb->s_dirt = 1;
2552 btrfs_update_inode_block_group(trans, inode);
2553 btrfs_update_inode_block_group(trans, dir);
2554
2555out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002556 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002557 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002558
Chris Mason39279cc2007-06-12 06:35:45 -04002559out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002560 if (drop_on_err)
2561 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002562 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002563 return err;
2564}
2565
Chris Mason3b951512008-04-17 11:29:12 -04002566static int merge_extent_mapping(struct extent_map_tree *em_tree,
2567 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002568 struct extent_map *em,
2569 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002570{
2571 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002572
Chris Masone6dcd2d2008-07-17 12:53:50 -04002573 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2574 start_diff = map_start - em->start;
2575 em->start = map_start;
2576 em->len = map_len;
2577 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2578 em->block_start += start_diff;
2579 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002580}
2581
Chris Masona52d9a82007-08-27 16:49:44 -04002582struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002583 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002584 int create)
2585{
2586 int ret;
2587 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002588 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002589 u64 extent_start = 0;
2590 u64 extent_end = 0;
2591 u64 objectid = inode->i_ino;
2592 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002593 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002594 struct btrfs_root *root = BTRFS_I(inode)->root;
2595 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002596 struct extent_buffer *leaf;
2597 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002598 struct extent_map *em = NULL;
2599 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002600 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002601 struct btrfs_trans_handle *trans = NULL;
2602
Chris Masona52d9a82007-08-27 16:49:44 -04002603again:
Chris Masond1310b22008-01-24 16:13:08 -05002604 spin_lock(&em_tree->lock);
2605 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002606 if (em)
2607 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002608 spin_unlock(&em_tree->lock);
2609
Chris Masona52d9a82007-08-27 16:49:44 -04002610 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002611 if (em->start > start || em->start + em->len <= start)
2612 free_extent_map(em);
2613 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002614 free_extent_map(em);
2615 else
2616 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002617 }
Chris Masond1310b22008-01-24 16:13:08 -05002618 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002619 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002620 err = -ENOMEM;
2621 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002622 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002623 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002624 em->start = EXTENT_MAP_HOLE;
2625 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002626
2627 if (!path) {
2628 path = btrfs_alloc_path();
2629 BUG_ON(!path);
2630 }
2631
Chris Mason179e29e2007-11-01 11:28:41 -04002632 ret = btrfs_lookup_file_extent(trans, root, path,
2633 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002634 if (ret < 0) {
2635 err = ret;
2636 goto out;
2637 }
2638
2639 if (ret != 0) {
2640 if (path->slots[0] == 0)
2641 goto not_found;
2642 path->slots[0]--;
2643 }
2644
Chris Mason5f39d392007-10-15 16:14:19 -04002645 leaf = path->nodes[0];
2646 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002647 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002648 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002649 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2650 found_type = btrfs_key_type(&found_key);
2651 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002652 found_type != BTRFS_EXTENT_DATA_KEY) {
2653 goto not_found;
2654 }
2655
Chris Mason5f39d392007-10-15 16:14:19 -04002656 found_type = btrfs_file_extent_type(leaf, item);
2657 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002658 if (found_type == BTRFS_FILE_EXTENT_REG) {
2659 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002660 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002661 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002662 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002663 em->start = start;
2664 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002665 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002666 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002667 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002668 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002669 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002670 }
2671 goto not_found_em;
2672 }
Chris Masondb945352007-10-15 16:15:53 -04002673 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2674 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002675 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002676 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002677 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002678 goto insert;
2679 }
Chris Masondb945352007-10-15 16:15:53 -04002680 bytenr += btrfs_file_extent_offset(leaf, item);
2681 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002682 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002683 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002684 goto insert;
2685 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002686 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002687 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002688 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002689 size_t size;
2690 size_t extent_offset;
2691 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002692
Chris Mason5f39d392007-10-15 16:14:19 -04002693 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2694 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002695 extent_end = (extent_start + size + root->sectorsize - 1) &
2696 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002697 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002698 em->start = start;
2699 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002700 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002701 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002702 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002703 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002704 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002705 }
2706 goto not_found_em;
2707 }
2708 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002709
2710 if (!page) {
2711 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002712 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002713 goto out;
2714 }
2715
Chris Mason70dec802008-01-29 09:59:12 -05002716 page_start = page_offset(page) + pg_offset;
2717 extent_offset = page_start - extent_start;
2718 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002719 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002720 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002721 em->len = (copy_size + root->sectorsize - 1) &
2722 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002723 map = kmap(page);
2724 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002725 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002726 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002727 copy_size);
2728 flush_dcache_page(page);
2729 } else if (create && PageUptodate(page)) {
2730 if (!trans) {
2731 kunmap(page);
2732 free_extent_map(em);
2733 em = NULL;
2734 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002735 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002736 goto again;
2737 }
Chris Mason70dec802008-01-29 09:59:12 -05002738 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002739 copy_size);
2740 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002741 }
Chris Masona52d9a82007-08-27 16:49:44 -04002742 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002743 set_extent_uptodate(io_tree, em->start,
2744 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002745 goto insert;
2746 } else {
2747 printk("unkknown found_type %d\n", found_type);
2748 WARN_ON(1);
2749 }
2750not_found:
2751 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002752 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002753not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002754 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002755insert:
2756 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002757 if (em->start > start || extent_map_end(em) <= start) {
2758 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002759 err = -EIO;
2760 goto out;
2761 }
Chris Masond1310b22008-01-24 16:13:08 -05002762
2763 err = 0;
2764 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002765 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002766 /* it is possible that someone inserted the extent into the tree
2767 * while we had the lock dropped. It is also possible that
2768 * an overlapping map exists in the tree
2769 */
Chris Masona52d9a82007-08-27 16:49:44 -04002770 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002771 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772
2773 ret = 0;
2774
Chris Mason3b951512008-04-17 11:29:12 -04002775 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002776 if (existing && (existing->start > start ||
2777 existing->start + existing->len <= start)) {
2778 free_extent_map(existing);
2779 existing = NULL;
2780 }
Chris Mason3b951512008-04-17 11:29:12 -04002781 if (!existing) {
2782 existing = lookup_extent_mapping(em_tree, em->start,
2783 em->len);
2784 if (existing) {
2785 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786 em, start,
2787 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002788 free_extent_map(existing);
2789 if (err) {
2790 free_extent_map(em);
2791 em = NULL;
2792 }
2793 } else {
2794 err = -EIO;
2795 printk("failing to insert %Lu %Lu\n",
2796 start, len);
2797 free_extent_map(em);
2798 em = NULL;
2799 }
2800 } else {
2801 free_extent_map(em);
2802 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002803 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002804 }
Chris Masona52d9a82007-08-27 16:49:44 -04002805 }
Chris Masond1310b22008-01-24 16:13:08 -05002806 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002807out:
Chris Masonf4219502008-07-22 11:18:09 -04002808 if (path)
2809 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002810 if (trans) {
2811 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002812 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002813 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002814 }
Chris Masona52d9a82007-08-27 16:49:44 -04002815 }
Chris Masona52d9a82007-08-27 16:49:44 -04002816 if (err) {
2817 free_extent_map(em);
2818 WARN_ON(1);
2819 return ERR_PTR(err);
2820 }
2821 return em;
2822}
2823
Chris Masone1c4b742008-04-22 13:26:46 -04002824#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002825static int btrfs_get_block(struct inode *inode, sector_t iblock,
2826 struct buffer_head *bh_result, int create)
2827{
2828 struct extent_map *em;
2829 u64 start = (u64)iblock << inode->i_blkbits;
2830 struct btrfs_multi_bio *multi = NULL;
2831 struct btrfs_root *root = BTRFS_I(inode)->root;
2832 u64 len;
2833 u64 logical;
2834 u64 map_length;
2835 int ret = 0;
2836
2837 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2838
2839 if (!em || IS_ERR(em))
2840 goto out;
2841
Chris Masone1c4b742008-04-22 13:26:46 -04002842 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002843 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002844 }
Chris Mason16432982008-04-10 10:23:21 -04002845
2846 if (em->block_start == EXTENT_MAP_INLINE) {
2847 ret = -EINVAL;
2848 goto out;
2849 }
2850
Chris Mason16432982008-04-10 10:23:21 -04002851 len = em->start + em->len - start;
2852 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2853
Chris Masone1c4b742008-04-22 13:26:46 -04002854 if (em->block_start == EXTENT_MAP_HOLE ||
2855 em->block_start == EXTENT_MAP_DELALLOC) {
2856 bh_result->b_size = len;
2857 goto out;
2858 }
2859
Chris Mason16432982008-04-10 10:23:21 -04002860 logical = start - em->start;
2861 logical = em->block_start + logical;
2862
2863 map_length = len;
2864 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2865 logical, &map_length, &multi, 0);
2866 BUG_ON(ret);
2867 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2868 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002869
Chris Mason16432982008-04-10 10:23:21 -04002870 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2871 set_buffer_mapped(bh_result);
2872 kfree(multi);
2873out:
2874 free_extent_map(em);
2875 return ret;
2876}
Chris Masone1c4b742008-04-22 13:26:46 -04002877#endif
Chris Mason16432982008-04-10 10:23:21 -04002878
2879static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2880 const struct iovec *iov, loff_t offset,
2881 unsigned long nr_segs)
2882{
Chris Masone1c4b742008-04-22 13:26:46 -04002883 return -EINVAL;
2884#if 0
Chris Mason16432982008-04-10 10:23:21 -04002885 struct file *file = iocb->ki_filp;
2886 struct inode *inode = file->f_mapping->host;
2887
2888 if (rw == WRITE)
2889 return -EINVAL;
2890
2891 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2892 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002893#endif
Chris Mason16432982008-04-10 10:23:21 -04002894}
2895
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002896static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002897{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002898 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002899}
2900
Chris Mason9ebefb182007-06-15 13:50:00 -04002901int btrfs_readpage(struct file *file, struct page *page)
2902{
Chris Masond1310b22008-01-24 16:13:08 -05002903 struct extent_io_tree *tree;
2904 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002905 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002906}
Chris Mason1832a6d2007-12-21 16:27:21 -05002907
Chris Mason39279cc2007-06-12 06:35:45 -04002908static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2909{
Chris Masond1310b22008-01-24 16:13:08 -05002910 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002911
2912
2913 if (current->flags & PF_MEMALLOC) {
2914 redirty_page_for_writepage(wbc, page);
2915 unlock_page(page);
2916 return 0;
2917 }
Chris Masond1310b22008-01-24 16:13:08 -05002918 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002919 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2920}
Chris Mason39279cc2007-06-12 06:35:45 -04002921
Chris Masonf4219502008-07-22 11:18:09 -04002922int btrfs_writepages(struct address_space *mapping,
2923 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002924{
Chris Masond1310b22008-01-24 16:13:08 -05002925 struct extent_io_tree *tree;
2926 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002927 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2928}
2929
Chris Mason3ab2fb52007-11-08 10:59:22 -05002930static int
2931btrfs_readpages(struct file *file, struct address_space *mapping,
2932 struct list_head *pages, unsigned nr_pages)
2933{
Chris Masond1310b22008-01-24 16:13:08 -05002934 struct extent_io_tree *tree;
2935 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002936 return extent_readpages(tree, mapping, pages, nr_pages,
2937 btrfs_get_extent);
2938}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002940{
Chris Masond1310b22008-01-24 16:13:08 -05002941 struct extent_io_tree *tree;
2942 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002943 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002944
Chris Masond1310b22008-01-24 16:13:08 -05002945 tree = &BTRFS_I(page->mapping->host)->io_tree;
2946 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002947 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002948 if (ret == 1) {
2949 ClearPagePrivate(page);
2950 set_page_private(page, 0);
2951 page_cache_release(page);
2952 }
2953 return ret;
2954}
Chris Mason39279cc2007-06-12 06:35:45 -04002955
Chris Masone6dcd2d2008-07-17 12:53:50 -04002956static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2957{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002958 return __btrfs_releasepage(page, gfp_flags);
2959}
2960
Chris Masona52d9a82007-08-27 16:49:44 -04002961static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2962{
Chris Masond1310b22008-01-24 16:13:08 -05002963 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964 struct btrfs_ordered_extent *ordered;
2965 u64 page_start = page_offset(page);
2966 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002967
Chris Masone6dcd2d2008-07-17 12:53:50 -04002968 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002969 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002970 if (offset) {
2971 btrfs_releasepage(page, GFP_NOFS);
2972 return;
2973 }
2974
2975 lock_extent(tree, page_start, page_end, GFP_NOFS);
2976 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2977 page_offset(page));
2978 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002979 /*
2980 * IO on this page will never be started, so we need
2981 * to account for any ordered extents now
2982 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002983 clear_extent_bit(tree, page_start, page_end,
2984 EXTENT_DIRTY | EXTENT_DELALLOC |
2985 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002986 btrfs_finish_ordered_io(page->mapping->host,
2987 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002988 btrfs_put_ordered_extent(ordered);
2989 lock_extent(tree, page_start, page_end, GFP_NOFS);
2990 }
2991 clear_extent_bit(tree, page_start, page_end,
2992 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2993 EXTENT_ORDERED,
2994 1, 1, GFP_NOFS);
2995 __btrfs_releasepage(page, GFP_NOFS);
2996
Chris Mason4a096752008-07-21 10:29:44 -04002997 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002998 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002999 ClearPagePrivate(page);
3000 set_page_private(page, 0);
3001 page_cache_release(page);
3002 }
Chris Mason39279cc2007-06-12 06:35:45 -04003003}
3004
Chris Mason9ebefb182007-06-15 13:50:00 -04003005/*
3006 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3007 * called from a page fault handler when a page is first dirtied. Hence we must
3008 * be careful to check for EOF conditions here. We set the page up correctly
3009 * for a written page which means we get ENOSPC checking when writing into
3010 * holes and correct delalloc and unwritten extent mapping on filesystems that
3011 * support these features.
3012 *
3013 * We are not allowed to take the i_mutex here so we have to play games to
3014 * protect against truncate races as the page could now be beyond EOF. Because
3015 * vmtruncate() writes the inode size before removing pages, once we have the
3016 * page lock we can determine safely if the page is beyond EOF. If it is not
3017 * beyond EOF, then the page is guaranteed safe against truncation until we
3018 * unlock the page.
3019 */
3020int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3021{
Chris Mason6da6aba2007-12-18 16:15:09 -05003022 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003023 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003024 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3025 struct btrfs_ordered_extent *ordered;
3026 char *kaddr;
3027 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003028 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003029 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003030 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003031 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003032
Chris Mason1832a6d2007-12-21 16:27:21 -05003033 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003034 if (ret)
3035 goto out;
3036
3037 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003038again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003039 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003040 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041 page_start = page_offset(page);
3042 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003043
Chris Mason9ebefb182007-06-15 13:50:00 -04003044 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003045 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003046 /* page got truncated out from underneath us */
3047 goto out_unlock;
3048 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003049 wait_on_page_writeback(page);
3050
3051 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3052 set_page_extent_mapped(page);
3053
Chris Masoneb84ae02008-07-17 13:53:27 -04003054 /*
3055 * we can't set the delalloc bits if there are pending ordered
3056 * extents. Drop our locks and wait for them to finish
3057 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003058 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3059 if (ordered) {
3060 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3061 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003062 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003063 btrfs_put_ordered_extent(ordered);
3064 goto again;
3065 }
3066
Chris Masonea8c2812008-08-04 23:17:27 -04003067 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003068 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003069
3070 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003071 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003072 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003073 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003074 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003075
Chris Masone6dcd2d2008-07-17 12:53:50 -04003076 if (zero_start != PAGE_CACHE_SIZE) {
3077 kaddr = kmap(page);
3078 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3079 flush_dcache_page(page);
3080 kunmap(page);
3081 }
Chris Mason247e7432008-07-17 12:53:51 -04003082 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003083 set_page_dirty(page);
3084 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003085
3086out_unlock:
3087 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003088out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003089 return ret;
3090}
3091
Chris Mason39279cc2007-06-12 06:35:45 -04003092static void btrfs_truncate(struct inode *inode)
3093{
3094 struct btrfs_root *root = BTRFS_I(inode)->root;
3095 int ret;
3096 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003097 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003098 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003099
3100 if (!S_ISREG(inode->i_mode))
3101 return;
3102 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3103 return;
3104
3105 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003106 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003107
Chris Mason39279cc2007-06-12 06:35:45 -04003108 trans = btrfs_start_transaction(root, 1);
3109 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003110 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003111
Josef Bacik7b128762008-07-24 12:17:14 -04003112 ret = btrfs_orphan_add(trans, inode);
3113 if (ret)
3114 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003115 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003116 ret = btrfs_truncate_in_trans(trans, root, inode,
3117 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003118 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003119
Josef Bacik7b128762008-07-24 12:17:14 -04003120 ret = btrfs_orphan_del(trans, inode);
3121 BUG_ON(ret);
3122
3123out:
3124 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003125 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003126 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003127 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003128}
3129
Sven Wegener3b963622008-06-09 21:57:42 -04003130/*
3131 * Invalidate a single dcache entry at the root of the filesystem.
3132 * Needed after creation of snapshot or subvolume.
3133 */
3134void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3135 int namelen)
3136{
3137 struct dentry *alias, *entry;
3138 struct qstr qstr;
3139
3140 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3141 if (alias) {
3142 qstr.name = name;
3143 qstr.len = namelen;
3144 /* change me if btrfs ever gets a d_hash operation */
3145 qstr.hash = full_name_hash(qstr.name, qstr.len);
3146 entry = d_lookup(alias, &qstr);
3147 dput(alias);
3148 if (entry) {
3149 d_invalidate(entry);
3150 dput(entry);
3151 }
3152 }
3153}
3154
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003155int btrfs_create_subvol_root(struct btrfs_root *new_root,
3156 struct btrfs_trans_handle *trans, u64 new_dirid,
3157 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003158{
Chris Mason39279cc2007-06-12 06:35:45 -04003159 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003160
Josef Bacikaec74772008-07-24 12:12:38 -04003161 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003162 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003163 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003164 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003165 inode->i_op = &btrfs_dir_inode_operations;
3166 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003167 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003168
Chris Mason39279cc2007-06-12 06:35:45 -04003169 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003170 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003171
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003172 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003173}
3174
Chris Masonedbd8d42007-12-21 16:27:24 -05003175unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003176 struct file_ra_state *ra, struct file *file,
3177 pgoff_t offset, pgoff_t last_index)
3178{
Chris Mason8e7bf942008-04-28 09:02:36 -04003179 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003180
3181#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003182 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3183 return offset;
3184#else
Chris Mason86479a02007-09-10 19:58:16 -04003185 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3186 return offset + req_size;
3187#endif
3188}
3189
Chris Mason39279cc2007-06-12 06:35:45 -04003190struct inode *btrfs_alloc_inode(struct super_block *sb)
3191{
3192 struct btrfs_inode *ei;
3193
3194 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3195 if (!ei)
3196 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003197 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003198 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003199 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3200 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003201 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003202 return &ei->vfs_inode;
3203}
3204
3205void btrfs_destroy_inode(struct inode *inode)
3206{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003207 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003208 WARN_ON(!list_empty(&inode->i_dentry));
3209 WARN_ON(inode->i_data.nrpages);
3210
Josef Bacik33268ea2008-07-24 12:16:36 -04003211 if (BTRFS_I(inode)->i_acl &&
3212 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3213 posix_acl_release(BTRFS_I(inode)->i_acl);
3214 if (BTRFS_I(inode)->i_default_acl &&
3215 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3216 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3217
Yanbcc63ab2008-07-30 16:29:20 -04003218 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003219 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3220 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3221 " list\n", inode->i_ino);
3222 dump_stack();
3223 }
Yanbcc63ab2008-07-30 16:29:20 -04003224 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003225
Chris Masone6dcd2d2008-07-17 12:53:50 -04003226 while(1) {
3227 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3228 if (!ordered)
3229 break;
3230 else {
3231 printk("found ordered extent %Lu %Lu\n",
3232 ordered->file_offset, ordered->len);
3233 btrfs_remove_ordered_extent(inode, ordered);
3234 btrfs_put_ordered_extent(ordered);
3235 btrfs_put_ordered_extent(ordered);
3236 }
3237 }
Chris Mason8c416c92008-01-14 15:10:26 -05003238 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003239 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3240}
3241
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003242#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3243static void init_once(void *foo)
3244#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003245static void init_once(struct kmem_cache * cachep, void *foo)
3246#else
Chris Mason39279cc2007-06-12 06:35:45 -04003247static void init_once(void * foo, struct kmem_cache * cachep,
3248 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003249#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003250{
3251 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3252
3253 inode_init_once(&ei->vfs_inode);
3254}
3255
3256void btrfs_destroy_cachep(void)
3257{
3258 if (btrfs_inode_cachep)
3259 kmem_cache_destroy(btrfs_inode_cachep);
3260 if (btrfs_trans_handle_cachep)
3261 kmem_cache_destroy(btrfs_trans_handle_cachep);
3262 if (btrfs_transaction_cachep)
3263 kmem_cache_destroy(btrfs_transaction_cachep);
3264 if (btrfs_bit_radix_cachep)
3265 kmem_cache_destroy(btrfs_bit_radix_cachep);
3266 if (btrfs_path_cachep)
3267 kmem_cache_destroy(btrfs_path_cachep);
3268}
3269
Chris Mason86479a02007-09-10 19:58:16 -04003270struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003271 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003272#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3273 void (*ctor)(void *)
3274#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003275 void (*ctor)(struct kmem_cache *, void *)
3276#else
Chris Mason92fee662007-07-25 12:31:35 -04003277 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003278 unsigned long)
3279#endif
3280 )
Chris Mason92fee662007-07-25 12:31:35 -04003281{
3282 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3283 SLAB_MEM_SPREAD | extra_flags), ctor
3284#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3285 ,NULL
3286#endif
3287 );
3288}
3289
Chris Mason39279cc2007-06-12 06:35:45 -04003290int btrfs_init_cachep(void)
3291{
Chris Mason86479a02007-09-10 19:58:16 -04003292 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003293 sizeof(struct btrfs_inode),
3294 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003295 if (!btrfs_inode_cachep)
3296 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003297 btrfs_trans_handle_cachep =
3298 btrfs_cache_create("btrfs_trans_handle_cache",
3299 sizeof(struct btrfs_trans_handle),
3300 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003301 if (!btrfs_trans_handle_cachep)
3302 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003303 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003304 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003305 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003306 if (!btrfs_transaction_cachep)
3307 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003308 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003309 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003310 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003311 if (!btrfs_path_cachep)
3312 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003313 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003314 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003315 if (!btrfs_bit_radix_cachep)
3316 goto fail;
3317 return 0;
3318fail:
3319 btrfs_destroy_cachep();
3320 return -ENOMEM;
3321}
3322
3323static int btrfs_getattr(struct vfsmount *mnt,
3324 struct dentry *dentry, struct kstat *stat)
3325{
3326 struct inode *inode = dentry->d_inode;
3327 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003328 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003329 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003330 return 0;
3331}
3332
3333static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3334 struct inode * new_dir,struct dentry *new_dentry)
3335{
3336 struct btrfs_trans_handle *trans;
3337 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3338 struct inode *new_inode = new_dentry->d_inode;
3339 struct inode *old_inode = old_dentry->d_inode;
3340 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 int ret;
3342
3343 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3344 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3345 return -ENOTEMPTY;
3346 }
Chris Mason5f39d392007-10-15 16:14:19 -04003347
Chris Mason1832a6d2007-12-21 16:27:21 -05003348 ret = btrfs_check_free_space(root, 1, 0);
3349 if (ret)
3350 goto out_unlock;
3351
Chris Mason39279cc2007-06-12 06:35:45 -04003352 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003353
Chris Mason39279cc2007-06-12 06:35:45 -04003354 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003355
3356 old_dentry->d_inode->i_nlink++;
3357 old_dir->i_ctime = old_dir->i_mtime = ctime;
3358 new_dir->i_ctime = new_dir->i_mtime = ctime;
3359 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003360
Chris Mason39279cc2007-06-12 06:35:45 -04003361 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3362 if (ret)
3363 goto out_fail;
3364
3365 if (new_inode) {
3366 new_inode->i_ctime = CURRENT_TIME;
3367 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3368 if (ret)
3369 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003370 if (new_inode->i_nlink == 0) {
3371 ret = btrfs_orphan_add(trans, new_inode);
3372 if (ret)
3373 goto out_fail;
3374 }
Chris Mason39279cc2007-06-12 06:35:45 -04003375 }
Josef Bacikaec74772008-07-24 12:12:38 -04003376 ret = btrfs_set_inode_index(new_dir, old_inode);
3377 if (ret)
3378 goto out_fail;
3379
Chris Mason9c583092008-01-29 15:15:18 -05003380 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003381 if (ret)
3382 goto out_fail;
3383
3384out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003385 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003386out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003387 return ret;
3388}
3389
Chris Masonea8c2812008-08-04 23:17:27 -04003390int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3391{
3392 struct list_head *head = &root->fs_info->delalloc_inodes;
3393 struct btrfs_inode *binode;
3394 unsigned long flags;
3395
3396 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3397 while(!list_empty(head)) {
3398 binode = list_entry(head->next, struct btrfs_inode,
3399 delalloc_inodes);
3400 atomic_inc(&binode->vfs_inode.i_count);
3401 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3402 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3403 iput(&binode->vfs_inode);
3404 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3405 }
3406 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3407 return 0;
3408}
3409
Chris Mason39279cc2007-06-12 06:35:45 -04003410static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3411 const char *symname)
3412{
3413 struct btrfs_trans_handle *trans;
3414 struct btrfs_root *root = BTRFS_I(dir)->root;
3415 struct btrfs_path *path;
3416 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003417 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003418 int err;
3419 int drop_inode = 0;
3420 u64 objectid;
3421 int name_len;
3422 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003423 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003425 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003426 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003427
3428 name_len = strlen(symname) + 1;
3429 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3430 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003431
Chris Mason1832a6d2007-12-21 16:27:21 -05003432 err = btrfs_check_free_space(root, 1, 0);
3433 if (err)
3434 goto out_fail;
3435
Chris Mason39279cc2007-06-12 06:35:45 -04003436 trans = btrfs_start_transaction(root, 1);
3437 btrfs_set_trans_block_group(trans, dir);
3438
3439 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3440 if (err) {
3441 err = -ENOSPC;
3442 goto out_unlock;
3443 }
3444
Josef Bacikaec74772008-07-24 12:12:38 -04003445 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003446 dentry->d_name.len,
3447 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003448 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3449 err = PTR_ERR(inode);
3450 if (IS_ERR(inode))
3451 goto out_unlock;
3452
Josef Bacik33268ea2008-07-24 12:16:36 -04003453 err = btrfs_init_acl(inode, dir);
3454 if (err) {
3455 drop_inode = 1;
3456 goto out_unlock;
3457 }
3458
Chris Mason39279cc2007-06-12 06:35:45 -04003459 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003460 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003461 if (err)
3462 drop_inode = 1;
3463 else {
3464 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003465 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003466 inode->i_fop = &btrfs_file_operations;
3467 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003468 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3469 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003470 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003471 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3472 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003473 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04003474 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003475 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003476 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003477 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003478 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003479 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003480 }
3481 dir->i_sb->s_dirt = 1;
3482 btrfs_update_inode_block_group(trans, inode);
3483 btrfs_update_inode_block_group(trans, dir);
3484 if (drop_inode)
3485 goto out_unlock;
3486
3487 path = btrfs_alloc_path();
3488 BUG_ON(!path);
3489 key.objectid = inode->i_ino;
3490 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3492 datasize = btrfs_file_extent_calc_inline_size(name_len);
3493 err = btrfs_insert_empty_item(trans, root, path, &key,
3494 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003495 if (err) {
3496 drop_inode = 1;
3497 goto out_unlock;
3498 }
Chris Mason5f39d392007-10-15 16:14:19 -04003499 leaf = path->nodes[0];
3500 ei = btrfs_item_ptr(leaf, path->slots[0],
3501 struct btrfs_file_extent_item);
3502 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3503 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003504 BTRFS_FILE_EXTENT_INLINE);
3505 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003506 write_extent_buffer(leaf, symname, ptr, name_len);
3507 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003508 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003509
Chris Mason39279cc2007-06-12 06:35:45 -04003510 inode->i_op = &btrfs_symlink_inode_operations;
3511 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003512 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003513 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003514 err = btrfs_update_inode(trans, root, inode);
3515 if (err)
3516 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003517
3518out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003519 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003520 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003521out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003522 if (drop_inode) {
3523 inode_dec_link_count(inode);
3524 iput(inode);
3525 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003526 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003527 return err;
3528}
Chris Mason16432982008-04-10 10:23:21 -04003529
Chris Masone6dcd2d2008-07-17 12:53:50 -04003530static int btrfs_set_page_dirty(struct page *page)
3531{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003532 return __set_page_dirty_nobuffers(page);
3533}
3534
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003535#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3536static int btrfs_permission(struct inode *inode, int mask)
3537#else
Yanfdebe2b2008-01-14 13:26:08 -05003538static int btrfs_permission(struct inode *inode, int mask,
3539 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003540#endif
Yanfdebe2b2008-01-14 13:26:08 -05003541{
3542 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3543 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003544 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003545}
Chris Mason39279cc2007-06-12 06:35:45 -04003546
3547static struct inode_operations btrfs_dir_inode_operations = {
3548 .lookup = btrfs_lookup,
3549 .create = btrfs_create,
3550 .unlink = btrfs_unlink,
3551 .link = btrfs_link,
3552 .mkdir = btrfs_mkdir,
3553 .rmdir = btrfs_rmdir,
3554 .rename = btrfs_rename,
3555 .symlink = btrfs_symlink,
3556 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003557 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003558 .setxattr = generic_setxattr,
3559 .getxattr = generic_getxattr,
3560 .listxattr = btrfs_listxattr,
3561 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003562 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003563};
Chris Mason39279cc2007-06-12 06:35:45 -04003564static struct inode_operations btrfs_dir_ro_inode_operations = {
3565 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003566 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003567};
Chris Mason39279cc2007-06-12 06:35:45 -04003568static struct file_operations btrfs_dir_file_operations = {
3569 .llseek = generic_file_llseek,
3570 .read = generic_read_dir,
3571 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003572 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003573#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003574 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003575#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003576 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003577};
3578
Chris Masond1310b22008-01-24 16:13:08 -05003579static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003580 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003581 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003582 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003583 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003584 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003585 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003586 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003587 .set_bit_hook = btrfs_set_bit_hook,
3588 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003589};
3590
Chris Mason39279cc2007-06-12 06:35:45 -04003591static struct address_space_operations btrfs_aops = {
3592 .readpage = btrfs_readpage,
3593 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003594 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003595 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003596 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003597 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003598 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003599 .invalidatepage = btrfs_invalidatepage,
3600 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003601 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003602};
3603
3604static struct address_space_operations btrfs_symlink_aops = {
3605 .readpage = btrfs_readpage,
3606 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003607 .invalidatepage = btrfs_invalidatepage,
3608 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003609};
3610
3611static struct inode_operations btrfs_file_inode_operations = {
3612 .truncate = btrfs_truncate,
3613 .getattr = btrfs_getattr,
3614 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003615 .setxattr = generic_setxattr,
3616 .getxattr = generic_getxattr,
3617 .listxattr = btrfs_listxattr,
3618 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003619 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003620};
Josef Bacik618e21d2007-07-11 10:18:17 -04003621static struct inode_operations btrfs_special_inode_operations = {
3622 .getattr = btrfs_getattr,
3623 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003624 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003625 .setxattr = generic_setxattr,
3626 .getxattr = generic_getxattr,
3627 .listxattr = btrfs_listxattr,
3628 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003629};
Chris Mason39279cc2007-06-12 06:35:45 -04003630static struct inode_operations btrfs_symlink_inode_operations = {
3631 .readlink = generic_readlink,
3632 .follow_link = page_follow_link_light,
3633 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003634 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003635};