blob: 640648c66b22da9699fe63a42af37763b91ddab9 [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 Masonbcbfce82008-04-22 13:26:47 -0400306 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500307 }
308 return 0;
309}
310
311int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500312 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Chris Masonb0c68f82008-01-31 11:05:37 -0500314 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500315 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400316 unsigned long flags;
317
318 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500319 if (end - start + 1 > root->fs_info->delalloc_bytes) {
320 printk("warning: delalloc account %Lu %Lu\n",
321 end - start + 1, root->fs_info->delalloc_bytes);
322 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500323 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500324 } else {
325 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500326 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500327 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400328 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500329 }
330 return 0;
331}
332
Chris Mason239b14b2008-03-24 15:02:07 -0400333int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
334 size_t size, struct bio *bio)
335{
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
337 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400339 u64 length = 0;
340 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400341 int ret;
342
Chris Masonf2d8d742008-04-21 10:03:05 -0400343 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400344 map_tree = &root->fs_info->mapping_tree;
345 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400346 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400347 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400348
Chris Mason239b14b2008-03-24 15:02:07 -0400349 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400350 return 1;
351 }
352 return 0;
353}
354
Chris Mason44b8bd72008-04-16 11:14:51 -0400355int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400356 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500357{
Chris Mason065631f2008-02-20 12:07:25 -0500358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500359 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400360
Chris Mason3edf7d32008-07-18 06:17:13 -0400361 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400362 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400363
Chris Mason8b712842008-06-11 16:50:36 -0400364 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400365}
366
367int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
368 int mirror_num)
369{
370 struct btrfs_root *root = BTRFS_I(inode)->root;
371 int ret = 0;
372
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
374 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500375
Chris Masone6dcd2d2008-07-17 12:53:50 -0400376 if (!(rw & (1 << BIO_RW))) {
Chris Mason61b49442008-07-31 15:42:53 -0400377 if (!btrfs_test_opt(root, NODATASUM) &&
378 !btrfs_test_flag(inode, NODATASUM)) {
Chris Mason6dab8152008-08-04 08:35:53 -0400379 mutex_lock(&BTRFS_I(inode)->csum_mutex);
Chris Mason61b49442008-07-31 15:42:53 -0400380 btrfs_lookup_bio_sums(root, inode, bio);
Chris Mason6dab8152008-08-04 08:35:53 -0400381 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Mason61b49442008-07-31 15:42:53 -0400382 }
Chris Mason0b86a832008-03-24 15:01:56 -0400383 goto mapit;
384 }
Chris Mason065631f2008-02-20 12:07:25 -0500385
Chris Mason44b8bd72008-04-16 11:14:51 -0400386 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
387 inode, rw, bio, mirror_num,
388 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400389mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400390 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500391}
Chris Mason6885f302008-02-20 16:11:05 -0500392
Chris Masonba1da2f2008-07-17 12:54:15 -0400393static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400394 struct inode *inode, u64 file_offset,
395 struct list_head *list)
396{
397 struct list_head *cur;
398 struct btrfs_ordered_sum *sum;
399
400 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400401 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400402 sum = list_entry(cur, struct btrfs_ordered_sum, list);
403 mutex_lock(&BTRFS_I(inode)->csum_mutex);
404 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
405 inode, sum);
406 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400407 }
408 return 0;
409}
410
Chris Mason247e7432008-07-17 12:53:51 -0400411struct btrfs_writepage_fixup {
412 struct page *page;
413 struct btrfs_work work;
414};
415
416/* see btrfs_writepage_start_hook for details on why this is required */
417void btrfs_writepage_fixup_worker(struct btrfs_work *work)
418{
419 struct btrfs_writepage_fixup *fixup;
420 struct btrfs_ordered_extent *ordered;
421 struct page *page;
422 struct inode *inode;
423 u64 page_start;
424 u64 page_end;
425
426 fixup = container_of(work, struct btrfs_writepage_fixup, work);
427 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400428again:
Chris Mason247e7432008-07-17 12:53:51 -0400429 lock_page(page);
430 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
431 ClearPageChecked(page);
432 goto out_page;
433 }
434
435 inode = page->mapping->host;
436 page_start = page_offset(page);
437 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
438
439 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400440
441 /* already ordered? We're done */
442 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
443 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400444 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400445 }
446
447 ordered = btrfs_lookup_ordered_extent(inode, page_start);
448 if (ordered) {
449 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
450 page_end, GFP_NOFS);
451 unlock_page(page);
452 btrfs_start_ordered_extent(inode, ordered, 1);
453 goto again;
454 }
Chris Mason247e7432008-07-17 12:53:51 -0400455
456 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
457 GFP_NOFS);
458 ClearPageChecked(page);
459out:
460 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
461out_page:
462 unlock_page(page);
463 page_cache_release(page);
464}
465
466/*
467 * There are a few paths in the higher layers of the kernel that directly
468 * set the page dirty bit without asking the filesystem if it is a
469 * good idea. This causes problems because we want to make sure COW
470 * properly happens and the data=ordered rules are followed.
471 *
472 * In our case any range that doesn't have the EXTENT_ORDERED bit set
473 * hasn't been properly setup for IO. We kick off an async process
474 * to fix it up. The async helper will wait for ordered extents, set
475 * the delalloc bit and make it safe to write the page.
476 */
477int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
478{
479 struct inode *inode = page->mapping->host;
480 struct btrfs_writepage_fixup *fixup;
481 struct btrfs_root *root = BTRFS_I(inode)->root;
482 int ret;
483
484 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
485 EXTENT_ORDERED, 0);
486 if (ret)
487 return 0;
488
489 if (PageChecked(page))
490 return -EAGAIN;
491
492 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
493 if (!fixup)
494 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400495
Chris Mason247e7432008-07-17 12:53:51 -0400496 SetPageChecked(page);
497 page_cache_get(page);
498 fixup->work.func = btrfs_writepage_fixup_worker;
499 fixup->page = page;
500 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
501 return -EAGAIN;
502}
503
Chris Mason211f90e2008-07-18 11:56:15 -0400504static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400505{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400506 struct btrfs_root *root = BTRFS_I(inode)->root;
507 struct btrfs_trans_handle *trans;
508 struct btrfs_ordered_extent *ordered_extent;
509 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
510 u64 alloc_hint = 0;
511 struct list_head list;
512 struct btrfs_key ins;
513 int ret;
514
515 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400516 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400517 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400518
Chris Masonf9295742008-07-17 12:54:14 -0400519 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400520
521 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
522 BUG_ON(!ordered_extent);
523
524 lock_extent(io_tree, ordered_extent->file_offset,
525 ordered_extent->file_offset + ordered_extent->len - 1,
526 GFP_NOFS);
527
528 INIT_LIST_HEAD(&list);
529
530 ins.objectid = ordered_extent->start;
531 ins.offset = ordered_extent->len;
532 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400533
Chris Masone6dcd2d2008-07-17 12:53:50 -0400534 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
535 trans->transid, inode->i_ino,
536 ordered_extent->file_offset, &ins);
537 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400538
539 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400540
Chris Masone6dcd2d2008-07-17 12:53:50 -0400541 ret = btrfs_drop_extents(trans, root, inode,
542 ordered_extent->file_offset,
543 ordered_extent->file_offset +
544 ordered_extent->len,
545 ordered_extent->file_offset, &alloc_hint);
546 BUG_ON(ret);
547 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
548 ordered_extent->file_offset,
549 ordered_extent->start,
550 ordered_extent->len,
551 ordered_extent->len, 0);
552 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400553
Chris Masone6dcd2d2008-07-17 12:53:50 -0400554 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
555 ordered_extent->file_offset +
556 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400557 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
558
Chris Masone6dcd2d2008-07-17 12:53:50 -0400559 inode->i_blocks += ordered_extent->len >> 9;
560 unlock_extent(io_tree, ordered_extent->file_offset,
561 ordered_extent->file_offset + ordered_extent->len - 1,
562 GFP_NOFS);
563 add_pending_csums(trans, inode, ordered_extent->file_offset,
564 &ordered_extent->list);
565
Chris Masondbe674a2008-07-17 12:54:05 -0400566 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400567 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400568
Chris Masone6dcd2d2008-07-17 12:53:50 -0400569 /* once for us */
570 btrfs_put_ordered_extent(ordered_extent);
571 /* once for the tree */
572 btrfs_put_ordered_extent(ordered_extent);
573
574 btrfs_update_inode(trans, root, inode);
575 btrfs_end_transaction(trans, root);
576 return 0;
577}
578
Chris Mason211f90e2008-07-18 11:56:15 -0400579int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
580 struct extent_state *state, int uptodate)
581{
582 return btrfs_finish_ordered_io(page->mapping->host, start, end);
583}
584
Chris Mason7e383262008-04-09 16:28:12 -0400585struct io_failure_record {
586 struct page *page;
587 u64 start;
588 u64 len;
589 u64 logical;
590 int last_mirror;
591};
592
Chris Mason1259ab72008-05-12 13:39:03 -0400593int btrfs_io_failed_hook(struct bio *failed_bio,
594 struct page *page, u64 start, u64 end,
595 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400596{
597 struct io_failure_record *failrec = NULL;
598 u64 private;
599 struct extent_map *em;
600 struct inode *inode = page->mapping->host;
601 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400602 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400603 struct bio *bio;
604 int num_copies;
605 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400606 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400607 u64 logical;
608
609 ret = get_state_private(failure_tree, start, &private);
610 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400611 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
612 if (!failrec)
613 return -ENOMEM;
614 failrec->start = start;
615 failrec->len = end - start + 1;
616 failrec->last_mirror = 0;
617
Chris Mason3b951512008-04-17 11:29:12 -0400618 spin_lock(&em_tree->lock);
619 em = lookup_extent_mapping(em_tree, start, failrec->len);
620 if (em->start > start || em->start + em->len < start) {
621 free_extent_map(em);
622 em = NULL;
623 }
624 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400625
626 if (!em || IS_ERR(em)) {
627 kfree(failrec);
628 return -EIO;
629 }
630 logical = start - em->start;
631 logical = em->block_start + logical;
632 failrec->logical = logical;
633 free_extent_map(em);
634 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
635 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400636 set_state_private(failure_tree, start,
637 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400638 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400639 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400640 }
641 num_copies = btrfs_num_copies(
642 &BTRFS_I(inode)->root->fs_info->mapping_tree,
643 failrec->logical, failrec->len);
644 failrec->last_mirror++;
645 if (!state) {
646 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
647 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
648 failrec->start,
649 EXTENT_LOCKED);
650 if (state && state->start != failrec->start)
651 state = NULL;
652 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
653 }
654 if (!state || failrec->last_mirror > num_copies) {
655 set_state_private(failure_tree, failrec->start, 0);
656 clear_extent_bits(failure_tree, failrec->start,
657 failrec->start + failrec->len - 1,
658 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
659 kfree(failrec);
660 return -EIO;
661 }
662 bio = bio_alloc(GFP_NOFS, 1);
663 bio->bi_private = state;
664 bio->bi_end_io = failed_bio->bi_end_io;
665 bio->bi_sector = failrec->logical >> 9;
666 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400667 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400668 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400669 if (failed_bio->bi_rw & (1 << BIO_RW))
670 rw = WRITE;
671 else
672 rw = READ;
673
674 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
675 failrec->last_mirror);
676 return 0;
677}
678
679int btrfs_clean_io_failures(struct inode *inode, u64 start)
680{
681 u64 private;
682 u64 private_failure;
683 struct io_failure_record *failure;
684 int ret;
685
686 private = 0;
687 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
688 (u64)-1, 1, EXTENT_DIRTY)) {
689 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
690 start, &private_failure);
691 if (ret == 0) {
692 failure = (struct io_failure_record *)(unsigned long)
693 private_failure;
694 set_state_private(&BTRFS_I(inode)->io_failure_tree,
695 failure->start, 0);
696 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
697 failure->start,
698 failure->start + failure->len - 1,
699 EXTENT_DIRTY | EXTENT_LOCKED,
700 GFP_NOFS);
701 kfree(failure);
702 }
703 }
Chris Mason7e383262008-04-09 16:28:12 -0400704 return 0;
705}
706
Chris Mason70dec802008-01-29 09:59:12 -0500707int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
708 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400709{
Chris Mason35ebb932007-10-30 16:56:53 -0400710 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400711 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500712 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400713 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500714 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400715 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400716 struct btrfs_root *root = BTRFS_I(inode)->root;
717 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400718 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500719
Yanb98b6762008-01-08 15:54:37 -0500720 if (btrfs_test_opt(root, NODATASUM) ||
721 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500722 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500723 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500724 private = state->private;
725 ret = 0;
726 } else {
727 ret = get_state_private(io_tree, start, &private);
728 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400729 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400730 kaddr = kmap_atomic(page, KM_IRQ0);
731 if (ret) {
732 goto zeroit;
733 }
Chris Masonff79f812007-10-15 16:22:25 -0400734 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
735 btrfs_csum_final(csum, (char *)&csum);
736 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400737 goto zeroit;
738 }
739 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400740 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400741
742 /* if the io failure tree for this inode is non-empty,
743 * check to see if we've recovered from a failed IO
744 */
Chris Mason1259ab72008-05-12 13:39:03 -0400745 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400746 return 0;
747
748zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500749 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
750 page->mapping->host->i_ino, (unsigned long long)start, csum,
751 private);
Chris Masondb945352007-10-15 16:15:53 -0400752 memset(kaddr + offset, 1, end - start + 1);
753 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400754 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400755 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400756 if (private == 0)
757 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400758 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400759}
Chris Masonb888db22007-08-27 16:49:44 -0400760
Josef Bacik7b128762008-07-24 12:17:14 -0400761/*
762 * This creates an orphan entry for the given inode in case something goes
763 * wrong in the middle of an unlink/truncate.
764 */
765int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
766{
767 struct btrfs_root *root = BTRFS_I(inode)->root;
768 int ret = 0;
769
Yanbcc63ab2008-07-30 16:29:20 -0400770 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400771
772 /* already on the orphan list, we're good */
773 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400774 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400775 return 0;
776 }
777
778 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
779
Yanbcc63ab2008-07-30 16:29:20 -0400780 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400781
782 /*
783 * insert an orphan item to track this unlinked/truncated file
784 */
785 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
786
787 return ret;
788}
789
790/*
791 * We have done the truncate/delete so we can go ahead and remove the orphan
792 * item for this particular inode.
793 */
794int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
795{
796 struct btrfs_root *root = BTRFS_I(inode)->root;
797 int ret = 0;
798
Yanbcc63ab2008-07-30 16:29:20 -0400799 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400800
801 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400802 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400803 return 0;
804 }
805
806 list_del_init(&BTRFS_I(inode)->i_orphan);
807 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400808 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400809 return 0;
810 }
811
Yanbcc63ab2008-07-30 16:29:20 -0400812 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400813
814 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
815
816 return ret;
817}
818
819/*
820 * this cleans up any orphans that may be left on the list from the last use
821 * of this root.
822 */
823void btrfs_orphan_cleanup(struct btrfs_root *root)
824{
825 struct btrfs_path *path;
826 struct extent_buffer *leaf;
827 struct btrfs_item *item;
828 struct btrfs_key key, found_key;
829 struct btrfs_trans_handle *trans;
830 struct inode *inode;
831 int ret = 0, nr_unlink = 0, nr_truncate = 0;
832
833 /* don't do orphan cleanup if the fs is readonly. */
834 if (root->inode->i_sb->s_flags & MS_RDONLY)
835 return;
836
837 path = btrfs_alloc_path();
838 if (!path)
839 return;
840 path->reada = -1;
841
842 key.objectid = BTRFS_ORPHAN_OBJECTID;
843 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
844 key.offset = (u64)-1;
845
846 trans = btrfs_start_transaction(root, 1);
847 btrfs_set_trans_block_group(trans, root->inode);
848
849 while (1) {
850 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
851 if (ret < 0) {
852 printk(KERN_ERR "Error searching slot for orphan: %d"
853 "\n", ret);
854 break;
855 }
856
857 /*
858 * if ret == 0 means we found what we were searching for, which
859 * is weird, but possible, so only screw with path if we didnt
860 * find the key and see if we have stuff that matches
861 */
862 if (ret > 0) {
863 if (path->slots[0] == 0)
864 break;
865 path->slots[0]--;
866 }
867
868 /* pull out the item */
869 leaf = path->nodes[0];
870 item = btrfs_item_nr(leaf, path->slots[0]);
871 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
872
873 /* make sure the item matches what we want */
874 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
875 break;
876 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
877 break;
878
879 /* release the path since we're done with it */
880 btrfs_release_path(root, path);
881
882 /*
883 * this is where we are basically btrfs_lookup, without the
884 * crossing root thing. we store the inode number in the
885 * offset of the orphan item.
886 */
887 inode = btrfs_iget_locked(root->inode->i_sb,
888 found_key.offset, root);
889 if (!inode)
890 break;
891
892 if (inode->i_state & I_NEW) {
893 BTRFS_I(inode)->root = root;
894
895 /* have to set the location manually */
896 BTRFS_I(inode)->location.objectid = inode->i_ino;
897 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
898 BTRFS_I(inode)->location.offset = 0;
899
900 btrfs_read_locked_inode(inode);
901 unlock_new_inode(inode);
902 }
903
904 /*
905 * add this inode to the orphan list so btrfs_orphan_del does
906 * the proper thing when we hit it
907 */
Yanbcc63ab2008-07-30 16:29:20 -0400908 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400909 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400910 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400911
912 /*
913 * if this is a bad inode, means we actually succeeded in
914 * removing the inode, but not the orphan record, which means
915 * we need to manually delete the orphan since iput will just
916 * do a destroy_inode
917 */
918 if (is_bad_inode(inode)) {
919 btrfs_orphan_del(trans, inode);
920 iput(inode);
921 continue;
922 }
923
924 /* if we have links, this was a truncate, lets do that */
925 if (inode->i_nlink) {
926 nr_truncate++;
927 btrfs_truncate(inode);
928 } else {
929 nr_unlink++;
930 }
931
932 /* this will do delete_inode and everything for us */
933 iput(inode);
934 }
935
936 if (nr_unlink)
937 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
938 if (nr_truncate)
939 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
940
941 btrfs_free_path(path);
942 btrfs_end_transaction(trans, root);
943}
944
Chris Mason39279cc2007-06-12 06:35:45 -0400945void btrfs_read_locked_inode(struct inode *inode)
946{
947 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400948 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400949 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400950 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400951 struct btrfs_root *root = BTRFS_I(inode)->root;
952 struct btrfs_key location;
953 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400954 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400955 int ret;
956
957 path = btrfs_alloc_path();
958 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400959 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500960
Chris Mason39279cc2007-06-12 06:35:45 -0400961 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400962 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400963 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400964
Chris Mason5f39d392007-10-15 16:14:19 -0400965 leaf = path->nodes[0];
966 inode_item = btrfs_item_ptr(leaf, path->slots[0],
967 struct btrfs_inode_item);
968
969 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
970 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
971 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
972 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400973 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400974
975 tspec = btrfs_inode_atime(inode_item);
976 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
977 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
978
979 tspec = btrfs_inode_mtime(inode_item);
980 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
981 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
982
983 tspec = btrfs_inode_ctime(inode_item);
984 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
985 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
986
987 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
988 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400989 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400990 rdev = btrfs_inode_rdev(leaf, inode_item);
991
Josef Bacikaec74772008-07-24 12:12:38 -0400992 BTRFS_I(inode)->index_cnt = (u64)-1;
993
Chris Mason5f39d392007-10-15 16:14:19 -0400994 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400995 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
996 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500997 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500998 if (!BTRFS_I(inode)->block_group) {
999 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001000 NULL, 0,
1001 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001002 }
Chris Mason39279cc2007-06-12 06:35:45 -04001003 btrfs_free_path(path);
1004 inode_item = NULL;
1005
Chris Mason39279cc2007-06-12 06:35:45 -04001006 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001007 case S_IFREG:
1008 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001009 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001010 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001011 inode->i_fop = &btrfs_file_operations;
1012 inode->i_op = &btrfs_file_inode_operations;
1013 break;
1014 case S_IFDIR:
1015 inode->i_fop = &btrfs_dir_file_operations;
1016 if (root == root->fs_info->tree_root)
1017 inode->i_op = &btrfs_dir_ro_inode_operations;
1018 else
1019 inode->i_op = &btrfs_dir_inode_operations;
1020 break;
1021 case S_IFLNK:
1022 inode->i_op = &btrfs_symlink_inode_operations;
1023 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001024 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001025 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001026 default:
1027 init_special_inode(inode, inode->i_mode, rdev);
1028 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001029 }
1030 return;
1031
1032make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001033 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001034 make_bad_inode(inode);
1035}
1036
Chris Mason5f39d392007-10-15 16:14:19 -04001037static void fill_inode_item(struct extent_buffer *leaf,
1038 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001039 struct inode *inode)
1040{
Chris Mason5f39d392007-10-15 16:14:19 -04001041 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1042 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001043 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001044 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1045 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1046
1047 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1048 inode->i_atime.tv_sec);
1049 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1050 inode->i_atime.tv_nsec);
1051
1052 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1053 inode->i_mtime.tv_sec);
1054 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1055 inode->i_mtime.tv_nsec);
1056
1057 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1058 inode->i_ctime.tv_sec);
1059 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1060 inode->i_ctime.tv_nsec);
1061
1062 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1063 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1064 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001065 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001067 BTRFS_I(inode)->block_group->key.objectid);
1068}
1069
Chris Masonba1da2f2008-07-17 12:54:15 -04001070int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001071 struct btrfs_root *root,
1072 struct inode *inode)
1073{
1074 struct btrfs_inode_item *inode_item;
1075 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001076 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001077 int ret;
1078
1079 path = btrfs_alloc_path();
1080 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001081 ret = btrfs_lookup_inode(trans, root, path,
1082 &BTRFS_I(inode)->location, 1);
1083 if (ret) {
1084 if (ret > 0)
1085 ret = -ENOENT;
1086 goto failed;
1087 }
1088
Chris Mason5f39d392007-10-15 16:14:19 -04001089 leaf = path->nodes[0];
1090 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001091 struct btrfs_inode_item);
1092
Chris Mason5f39d392007-10-15 16:14:19 -04001093 fill_inode_item(leaf, inode_item, inode);
1094 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001095 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001096 ret = 0;
1097failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001098 btrfs_free_path(path);
1099 return ret;
1100}
1101
1102
1103static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1104 struct btrfs_root *root,
1105 struct inode *dir,
1106 struct dentry *dentry)
1107{
1108 struct btrfs_path *path;
1109 const char *name = dentry->d_name.name;
1110 int name_len = dentry->d_name.len;
1111 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001112 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001113 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001115 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001116
1117 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001118 if (!path) {
1119 ret = -ENOMEM;
1120 goto err;
1121 }
1122
Chris Mason39279cc2007-06-12 06:35:45 -04001123 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1124 name, name_len, -1);
1125 if (IS_ERR(di)) {
1126 ret = PTR_ERR(di);
1127 goto err;
1128 }
1129 if (!di) {
1130 ret = -ENOENT;
1131 goto err;
1132 }
Chris Mason5f39d392007-10-15 16:14:19 -04001133 leaf = path->nodes[0];
1134 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001135 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001136 if (ret)
1137 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001138 btrfs_release_path(root, path);
1139
Josef Bacikaec74772008-07-24 12:12:38 -04001140 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1141 dentry->d_inode->i_ino,
1142 dentry->d_parent->d_inode->i_ino, &index);
1143 if (ret) {
1144 printk("failed to delete reference to %.*s, "
1145 "inode %lu parent %lu\n", name_len, name,
1146 dentry->d_inode->i_ino,
1147 dentry->d_parent->d_inode->i_ino);
1148 goto err;
1149 }
1150
Chris Mason39279cc2007-06-12 06:35:45 -04001151 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001152 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001153 if (IS_ERR(di)) {
1154 ret = PTR_ERR(di);
1155 goto err;
1156 }
1157 if (!di) {
1158 ret = -ENOENT;
1159 goto err;
1160 }
1161 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001162 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001163
1164 dentry->d_inode->i_ctime = dir->i_ctime;
1165err:
1166 btrfs_free_path(path);
1167 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001168 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001169 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001170 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001171#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1172 dentry->d_inode->i_nlink--;
1173#else
Chris Mason39279cc2007-06-12 06:35:45 -04001174 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001175#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001177 dir->i_sb->s_dirt = 1;
1178 }
1179 return ret;
1180}
1181
1182static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1183{
1184 struct btrfs_root *root;
1185 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001186 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001187 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001188 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001189
1190 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001191
1192 ret = btrfs_check_free_space(root, 1, 1);
1193 if (ret)
1194 goto fail;
1195
Chris Mason39279cc2007-06-12 06:35:45 -04001196 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001197
Chris Mason39279cc2007-06-12 06:35:45 -04001198 btrfs_set_trans_block_group(trans, dir);
1199 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001200
1201 if (inode->i_nlink == 0)
1202 ret = btrfs_orphan_add(trans, inode);
1203
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001204 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001205
Chris Mason89ce8a62008-06-25 16:01:31 -04001206 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001207fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001208 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001209 return ret;
1210}
1211
1212static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1213{
1214 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001215 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001216 int ret;
1217 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001218 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001219 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001220
Chris Mason925baed2008-06-25 16:01:30 -04001221 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001222 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001223 }
Yan134d4512007-10-25 15:49:25 -04001224
Chris Mason1832a6d2007-12-21 16:27:21 -05001225 ret = btrfs_check_free_space(root, 1, 1);
1226 if (ret)
1227 goto fail;
1228
Chris Mason39279cc2007-06-12 06:35:45 -04001229 trans = btrfs_start_transaction(root, 1);
1230 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001231
Josef Bacik7b128762008-07-24 12:17:14 -04001232 err = btrfs_orphan_add(trans, inode);
1233 if (err)
1234 goto fail_trans;
1235
Chris Mason39279cc2007-06-12 06:35:45 -04001236 /* now the directory is empty */
1237 err = btrfs_unlink_trans(trans, root, dir, dentry);
1238 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001239 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001240 }
Chris Mason39544012007-12-12 14:38:19 -05001241
Josef Bacik7b128762008-07-24 12:17:14 -04001242fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001243 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001244 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001245fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001246 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001247
Chris Mason39279cc2007-06-12 06:35:45 -04001248 if (ret && !err)
1249 err = ret;
1250 return err;
1251}
1252
Chris Mason39279cc2007-06-12 06:35:45 -04001253/*
Chris Mason39279cc2007-06-12 06:35:45 -04001254 * this can truncate away extent items, csum items and directory items.
1255 * It starts at a high offset and removes keys until it can't find
1256 * any higher than i_size.
1257 *
1258 * csum items that cross the new i_size are truncated to the new size
1259 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001260 *
1261 * min_type is the minimum key type to truncate down to. If set to 0, this
1262 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001263 */
1264static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1265 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001266 struct inode *inode,
1267 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001268{
1269 int ret;
1270 struct btrfs_path *path;
1271 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001272 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001273 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001274 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001275 struct btrfs_file_extent_item *fi;
1276 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001277 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001278 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001279 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001280 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001281 int found_extent;
1282 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001283 int pending_del_nr = 0;
1284 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001285 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001286 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001287
Chris Mason3b951512008-04-17 11:29:12 -04001288 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001289 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001290 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001291 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001292
Chris Mason39279cc2007-06-12 06:35:45 -04001293 /* FIXME, add redo link to tree so we don't leak on crash */
1294 key.objectid = inode->i_ino;
1295 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001296 key.type = (u8)-1;
1297
Chris Mason85e21ba2008-01-29 15:11:36 -05001298 btrfs_init_path(path);
1299search_again:
1300 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1301 if (ret < 0) {
1302 goto error;
1303 }
1304 if (ret > 0) {
1305 BUG_ON(path->slots[0] == 0);
1306 path->slots[0]--;
1307 }
1308
Chris Mason39279cc2007-06-12 06:35:45 -04001309 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001310 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001311 leaf = path->nodes[0];
1312 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1313 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001314
Chris Mason5f39d392007-10-15 16:14:19 -04001315 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001316 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001317
Chris Mason85e21ba2008-01-29 15:11:36 -05001318 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001319 break;
1320
Chris Mason5f39d392007-10-15 16:14:19 -04001321 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001322 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001323 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001324 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001325 extent_type = btrfs_file_extent_type(leaf, fi);
1326 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001327 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001328 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001329 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1330 struct btrfs_item *item = btrfs_item_nr(leaf,
1331 path->slots[0]);
1332 item_end += btrfs_file_extent_inline_len(leaf,
1333 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001334 }
Yan008630c2007-11-07 13:31:09 -05001335 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001336 }
1337 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1338 ret = btrfs_csum_truncate(trans, root, path,
1339 inode->i_size);
1340 BUG_ON(ret);
1341 }
Yan008630c2007-11-07 13:31:09 -05001342 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001343 if (found_type == BTRFS_DIR_ITEM_KEY) {
1344 found_type = BTRFS_INODE_ITEM_KEY;
1345 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1346 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001347 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1348 found_type = BTRFS_XATTR_ITEM_KEY;
1349 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1350 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001351 } else if (found_type) {
1352 found_type--;
1353 } else {
1354 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001355 }
Yana61721d2007-09-17 11:08:38 -04001356 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001357 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001358 }
Chris Mason5f39d392007-10-15 16:14:19 -04001359 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001360 del_item = 1;
1361 else
1362 del_item = 0;
1363 found_extent = 0;
1364
1365 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001366 if (found_type != BTRFS_EXTENT_DATA_KEY)
1367 goto delete;
1368
1369 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001370 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001371 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001372 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001373 u64 orig_num_bytes =
1374 btrfs_file_extent_num_bytes(leaf, fi);
1375 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001376 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001377 extent_num_bytes = extent_num_bytes &
1378 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001379 btrfs_set_file_extent_num_bytes(leaf, fi,
1380 extent_num_bytes);
1381 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001382 extent_num_bytes);
1383 if (extent_start != 0)
1384 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001385 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001386 } else {
Chris Masondb945352007-10-15 16:15:53 -04001387 extent_num_bytes =
1388 btrfs_file_extent_disk_num_bytes(leaf,
1389 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001390 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001391 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001392 if (extent_start != 0) {
1393 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001394 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001395 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001396 root_gen = btrfs_header_generation(leaf);
1397 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001398 }
Chris Mason90692182008-02-08 13:49:28 -05001399 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1400 if (!del_item) {
1401 u32 newsize = inode->i_size - found_key.offset;
1402 dec_i_blocks(inode, item_end + 1 -
1403 found_key.offset - newsize);
1404 newsize =
1405 btrfs_file_extent_calc_inline_size(newsize);
1406 ret = btrfs_truncate_item(trans, root, path,
1407 newsize, 1);
1408 BUG_ON(ret);
1409 } else {
1410 dec_i_blocks(inode, item_end + 1 -
1411 found_key.offset);
1412 }
Chris Mason39279cc2007-06-12 06:35:45 -04001413 }
Chris Mason179e29e2007-11-01 11:28:41 -04001414delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001415 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001416 if (!pending_del_nr) {
1417 /* no pending yet, add ourselves */
1418 pending_del_slot = path->slots[0];
1419 pending_del_nr = 1;
1420 } else if (pending_del_nr &&
1421 path->slots[0] + 1 == pending_del_slot) {
1422 /* hop on the pending chunk */
1423 pending_del_nr++;
1424 pending_del_slot = path->slots[0];
1425 } else {
1426 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1427 }
Chris Mason39279cc2007-06-12 06:35:45 -04001428 } else {
1429 break;
1430 }
Chris Mason39279cc2007-06-12 06:35:45 -04001431 if (found_extent) {
1432 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001433 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001434 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001435 root_gen, inode->i_ino,
1436 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001437 BUG_ON(ret);
1438 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001439next:
1440 if (path->slots[0] == 0) {
1441 if (pending_del_nr)
1442 goto del_pending;
1443 btrfs_release_path(root, path);
1444 goto search_again;
1445 }
1446
1447 path->slots[0]--;
1448 if (pending_del_nr &&
1449 path->slots[0] + 1 != pending_del_slot) {
1450 struct btrfs_key debug;
1451del_pending:
1452 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1453 pending_del_slot);
1454 ret = btrfs_del_items(trans, root, path,
1455 pending_del_slot,
1456 pending_del_nr);
1457 BUG_ON(ret);
1458 pending_del_nr = 0;
1459 btrfs_release_path(root, path);
1460 goto search_again;
1461 }
Chris Mason39279cc2007-06-12 06:35:45 -04001462 }
1463 ret = 0;
1464error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001465 if (pending_del_nr) {
1466 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1467 pending_del_nr);
1468 }
Chris Mason39279cc2007-06-12 06:35:45 -04001469 btrfs_free_path(path);
1470 inode->i_sb->s_dirt = 1;
1471 return ret;
1472}
1473
Chris Masona52d9a82007-08-27 16:49:44 -04001474/*
1475 * taken from block_truncate_page, but does cow as it zeros out
1476 * any bytes left in the last page in the file.
1477 */
1478static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1479{
1480 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001481 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001482 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1483 struct btrfs_ordered_extent *ordered;
1484 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001485 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001486 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1487 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1488 struct page *page;
1489 int ret = 0;
1490 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001491 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001492
1493 if ((offset & (blocksize - 1)) == 0)
1494 goto out;
1495
1496 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001497again:
Chris Masona52d9a82007-08-27 16:49:44 -04001498 page = grab_cache_page(mapping, index);
1499 if (!page)
1500 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001501
1502 page_start = page_offset(page);
1503 page_end = page_start + PAGE_CACHE_SIZE - 1;
1504
Chris Masona52d9a82007-08-27 16:49:44 -04001505 if (!PageUptodate(page)) {
1506 ret = btrfs_readpage(NULL, page);
1507 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001508 if (page->mapping != mapping) {
1509 unlock_page(page);
1510 page_cache_release(page);
1511 goto again;
1512 }
Chris Masona52d9a82007-08-27 16:49:44 -04001513 if (!PageUptodate(page)) {
1514 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001515 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001516 }
1517 }
Chris Mason211c17f2008-05-15 09:13:45 -04001518 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001519
1520 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1521 set_page_extent_mapped(page);
1522
1523 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1524 if (ordered) {
1525 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1526 unlock_page(page);
1527 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001528 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001529 btrfs_put_ordered_extent(ordered);
1530 goto again;
1531 }
1532
1533 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1534 page_end, GFP_NOFS);
1535 ret = 0;
1536 if (offset != PAGE_CACHE_SIZE) {
1537 kaddr = kmap(page);
1538 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1539 flush_dcache_page(page);
1540 kunmap(page);
1541 }
Chris Mason247e7432008-07-17 12:53:51 -04001542 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001543 set_page_dirty(page);
1544 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001545
Chris Mason89642222008-07-24 09:41:53 -04001546out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001547 unlock_page(page);
1548 page_cache_release(page);
1549out:
1550 return ret;
1551}
1552
1553static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1554{
1555 struct inode *inode = dentry->d_inode;
1556 int err;
1557
1558 err = inode_change_ok(inode, attr);
1559 if (err)
1560 return err;
1561
1562 if (S_ISREG(inode->i_mode) &&
1563 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1564 struct btrfs_trans_handle *trans;
1565 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001566 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001567
Chris Mason5f39d392007-10-15 16:14:19 -04001568 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001569 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001570 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001571 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001572 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001573
Chris Mason1b0f7c22008-01-30 14:33:02 -05001574 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001575 goto out;
1576
Chris Mason1832a6d2007-12-21 16:27:21 -05001577 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001578 if (err)
1579 goto fail;
1580
Chris Mason39279cc2007-06-12 06:35:45 -04001581 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1582
Chris Mason5f564062008-01-22 16:47:59 -05001583 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001584 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1585 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001586
Chris Mason39279cc2007-06-12 06:35:45 -04001587 trans = btrfs_start_transaction(root, 1);
1588 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001589 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001590 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001591 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001592 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001593
Chris Mason179e29e2007-11-01 11:28:41 -04001594 if (alloc_hint != EXTENT_MAP_INLINE) {
1595 err = btrfs_insert_file_extent(trans, root,
1596 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001597 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001598 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001599 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001600 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001601 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001602 }
Chris Masonee6e6502008-07-17 12:54:40 -04001603 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001604 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001605 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001606 if (err)
1607 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001608 }
1609out:
1610 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001611
1612 if (!err && ((attr->ia_valid & ATTR_MODE)))
1613 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001614fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001615 return err;
1616}
Chris Mason61295eb2008-01-14 16:24:38 -05001617
Chris Mason39279cc2007-06-12 06:35:45 -04001618void btrfs_delete_inode(struct inode *inode)
1619{
1620 struct btrfs_trans_handle *trans;
1621 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001622 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001623 int ret;
1624
1625 truncate_inode_pages(&inode->i_data, 0);
1626 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001627 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001628 goto no_delete;
1629 }
Chris Mason4a096752008-07-21 10:29:44 -04001630 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001631
Chris Masondbe674a2008-07-17 12:54:05 -04001632 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001633 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001634
Chris Mason39279cc2007-06-12 06:35:45 -04001635 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001636 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001637 if (ret) {
1638 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001639 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001640 }
1641
1642 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001643
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001644 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001645 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001646
Chris Mason39279cc2007-06-12 06:35:45 -04001647 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001648 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001649 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001650
1651no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001652 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001653 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001654 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001655no_delete:
1656 clear_inode(inode);
1657}
1658
1659/*
1660 * this returns the key found in the dir entry in the location pointer.
1661 * If no dir entries were found, location->objectid is 0.
1662 */
1663static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1664 struct btrfs_key *location)
1665{
1666 const char *name = dentry->d_name.name;
1667 int namelen = dentry->d_name.len;
1668 struct btrfs_dir_item *di;
1669 struct btrfs_path *path;
1670 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001671 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001672
Chris Mason39544012007-12-12 14:38:19 -05001673 if (namelen == 1 && strcmp(name, ".") == 0) {
1674 location->objectid = dir->i_ino;
1675 location->type = BTRFS_INODE_ITEM_KEY;
1676 location->offset = 0;
1677 return 0;
1678 }
Chris Mason39279cc2007-06-12 06:35:45 -04001679 path = btrfs_alloc_path();
1680 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001681
Chris Mason7a720532007-12-13 09:06:59 -05001682 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001683 struct btrfs_key key;
1684 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001685 int slot;
1686
1687 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001688 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001689 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001690 if (ret < 0 || path->slots[0] == 0)
1691 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001692 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1693 BUG_ON(ret == 0);
1694 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001695 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001696 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001697
1698 btrfs_item_key_to_cpu(leaf, &key, slot);
1699 if (key.objectid != dir->i_ino ||
1700 key.type != BTRFS_INODE_REF_KEY) {
1701 goto out_err;
1702 }
1703 location->objectid = key.offset;
1704 location->type = BTRFS_INODE_ITEM_KEY;
1705 location->offset = 0;
1706 goto out;
1707 }
1708
Chris Mason39279cc2007-06-12 06:35:45 -04001709 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1710 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001711 if (IS_ERR(di))
1712 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001713 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001714 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001715 }
Chris Mason5f39d392007-10-15 16:14:19 -04001716 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001717out:
Chris Mason39279cc2007-06-12 06:35:45 -04001718 btrfs_free_path(path);
1719 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001720out_err:
1721 location->objectid = 0;
1722 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001723}
1724
1725/*
1726 * when we hit a tree root in a directory, the btrfs part of the inode
1727 * needs to be changed to reflect the root directory of the tree root. This
1728 * is kind of like crossing a mount point.
1729 */
1730static int fixup_tree_root_location(struct btrfs_root *root,
1731 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001732 struct btrfs_root **sub_root,
1733 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001734{
Chris Mason39279cc2007-06-12 06:35:45 -04001735 struct btrfs_root_item *ri;
1736
1737 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1738 return 0;
1739 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1740 return 0;
1741
Josef Bacik58176a92007-08-29 15:47:34 -04001742 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1743 dentry->d_name.name,
1744 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001745 if (IS_ERR(*sub_root))
1746 return PTR_ERR(*sub_root);
1747
1748 ri = &(*sub_root)->root_item;
1749 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001750 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1751 location->offset = 0;
1752
Chris Mason39279cc2007-06-12 06:35:45 -04001753 return 0;
1754}
1755
1756static int btrfs_init_locked_inode(struct inode *inode, void *p)
1757{
1758 struct btrfs_iget_args *args = p;
1759 inode->i_ino = args->ino;
1760 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001761 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001762 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001763 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001764 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1765 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001766 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001767 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1768 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001769 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001770 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001771 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001772 return 0;
1773}
1774
1775static int btrfs_find_actor(struct inode *inode, void *opaque)
1776{
1777 struct btrfs_iget_args *args = opaque;
1778 return (args->ino == inode->i_ino &&
1779 args->root == BTRFS_I(inode)->root);
1780}
1781
Chris Masondc17ff82008-01-08 15:46:30 -05001782struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1783 u64 root_objectid)
1784{
1785 struct btrfs_iget_args args;
1786 args.ino = objectid;
1787 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1788
1789 if (!args.root)
1790 return NULL;
1791
1792 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1793}
1794
Chris Mason39279cc2007-06-12 06:35:45 -04001795struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1796 struct btrfs_root *root)
1797{
1798 struct inode *inode;
1799 struct btrfs_iget_args args;
1800 args.ino = objectid;
1801 args.root = root;
1802
1803 inode = iget5_locked(s, objectid, btrfs_find_actor,
1804 btrfs_init_locked_inode,
1805 (void *)&args);
1806 return inode;
1807}
1808
1809static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1810 struct nameidata *nd)
1811{
1812 struct inode * inode;
1813 struct btrfs_inode *bi = BTRFS_I(dir);
1814 struct btrfs_root *root = bi->root;
1815 struct btrfs_root *sub_root = root;
1816 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001817 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001818
1819 if (dentry->d_name.len > BTRFS_NAME_LEN)
1820 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001821
Chris Mason39279cc2007-06-12 06:35:45 -04001822 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001823
Chris Mason39279cc2007-06-12 06:35:45 -04001824 if (ret < 0)
1825 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001826
Chris Mason39279cc2007-06-12 06:35:45 -04001827 inode = NULL;
1828 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001829 ret = fixup_tree_root_location(root, &location, &sub_root,
1830 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001831 if (ret < 0)
1832 return ERR_PTR(ret);
1833 if (ret > 0)
1834 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001835
Chris Mason39279cc2007-06-12 06:35:45 -04001836 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1837 sub_root);
1838 if (!inode)
1839 return ERR_PTR(-EACCES);
1840 if (inode->i_state & I_NEW) {
1841 /* the inode and parent dir are two different roots */
1842 if (sub_root != root) {
1843 igrab(inode);
1844 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001845 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001846 }
1847 BTRFS_I(inode)->root = sub_root;
1848 memcpy(&BTRFS_I(inode)->location, &location,
1849 sizeof(location));
1850 btrfs_read_locked_inode(inode);
1851 unlock_new_inode(inode);
1852 }
1853 }
Josef Bacik7b128762008-07-24 12:17:14 -04001854
1855 if (unlikely(do_orphan))
1856 btrfs_orphan_cleanup(sub_root);
1857
Chris Mason39279cc2007-06-12 06:35:45 -04001858 return d_splice_alias(inode, dentry);
1859}
1860
Chris Mason39279cc2007-06-12 06:35:45 -04001861static unsigned char btrfs_filetype_table[] = {
1862 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1863};
1864
1865static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1866{
Chris Mason6da6aba2007-12-18 16:15:09 -05001867 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001868 struct btrfs_root *root = BTRFS_I(inode)->root;
1869 struct btrfs_item *item;
1870 struct btrfs_dir_item *di;
1871 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001872 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001873 struct btrfs_path *path;
1874 int ret;
1875 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001876 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001877 int slot;
1878 int advance;
1879 unsigned char d_type;
1880 int over = 0;
1881 u32 di_cur;
1882 u32 di_total;
1883 u32 di_len;
1884 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001885 char tmp_name[32];
1886 char *name_ptr;
1887 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001888
1889 /* FIXME, use a real flag for deciding about the key type */
1890 if (root->fs_info->tree_root == root)
1891 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001892
Chris Mason39544012007-12-12 14:38:19 -05001893 /* special case for "." */
1894 if (filp->f_pos == 0) {
1895 over = filldir(dirent, ".", 1,
1896 1, inode->i_ino,
1897 DT_DIR);
1898 if (over)
1899 return 0;
1900 filp->f_pos = 1;
1901 }
1902
Chris Mason39279cc2007-06-12 06:35:45 -04001903 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001904 path = btrfs_alloc_path();
1905 path->reada = 2;
1906
1907 /* special case for .., just use the back ref */
1908 if (filp->f_pos == 1) {
1909 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001910 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001911 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001912 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001913 btrfs_release_path(root, path);
1914 goto read_dir_items;
1915 }
Yan445dceb2008-07-24 12:19:32 -04001916 BUG_ON(ret == 0);
1917 leaf = path->nodes[0];
1918 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001919 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1920 btrfs_release_path(root, path);
1921 if (found_key.objectid != key.objectid ||
1922 found_key.type != BTRFS_INODE_REF_KEY)
1923 goto read_dir_items;
1924 over = filldir(dirent, "..", 2,
1925 2, found_key.offset, DT_DIR);
1926 if (over)
1927 goto nopos;
1928 filp->f_pos = 2;
1929 }
1930
1931read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001932 btrfs_set_key_type(&key, key_type);
1933 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001934
Chris Mason39279cc2007-06-12 06:35:45 -04001935 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1936 if (ret < 0)
1937 goto err;
1938 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001939 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001940 leaf = path->nodes[0];
1941 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001942 slot = path->slots[0];
1943 if (advance || slot >= nritems) {
1944 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001945 ret = btrfs_next_leaf(root, path);
1946 if (ret)
1947 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001948 leaf = path->nodes[0];
1949 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001950 slot = path->slots[0];
1951 } else {
1952 slot++;
1953 path->slots[0]++;
1954 }
1955 }
1956 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001957 item = btrfs_item_nr(leaf, slot);
1958 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1959
1960 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001961 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001962 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001963 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001964 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001965 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001966
1967 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001968 advance = 1;
1969 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1970 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001971 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001972 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001973 struct btrfs_key location;
1974
1975 name_len = btrfs_dir_name_len(leaf, di);
1976 if (name_len < 32) {
1977 name_ptr = tmp_name;
1978 } else {
1979 name_ptr = kmalloc(name_len, GFP_NOFS);
1980 BUG_ON(!name_ptr);
1981 }
1982 read_extent_buffer(leaf, name_ptr,
1983 (unsigned long)(di + 1), name_len);
1984
1985 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1986 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001987 over = filldir(dirent, name_ptr, name_len,
1988 found_key.offset,
1989 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001990 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001991
1992 if (name_ptr != tmp_name)
1993 kfree(name_ptr);
1994
Chris Mason39279cc2007-06-12 06:35:45 -04001995 if (over)
1996 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001997 di_len = btrfs_dir_name_len(leaf, di) +
1998 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001999 di_cur += di_len;
2000 di = (struct btrfs_dir_item *)((char *)di + di_len);
2001 }
2002 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002003 if (key_type == BTRFS_DIR_INDEX_KEY)
2004 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2005 else
2006 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002007nopos:
2008 ret = 0;
2009err:
Chris Mason39279cc2007-06-12 06:35:45 -04002010 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002011 return ret;
2012}
2013
2014int btrfs_write_inode(struct inode *inode, int wait)
2015{
2016 struct btrfs_root *root = BTRFS_I(inode)->root;
2017 struct btrfs_trans_handle *trans;
2018 int ret = 0;
2019
2020 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002021 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002022 btrfs_set_trans_block_group(trans, inode);
2023 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002024 }
2025 return ret;
2026}
2027
2028/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002029 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002030 * inode changes. But, it is most likely to find the inode in cache.
2031 * FIXME, needs more benchmarking...there are no reasons other than performance
2032 * to keep or drop this code.
2033 */
2034void btrfs_dirty_inode(struct inode *inode)
2035{
2036 struct btrfs_root *root = BTRFS_I(inode)->root;
2037 struct btrfs_trans_handle *trans;
2038
Chris Masonf9295742008-07-17 12:54:14 -04002039 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002040 btrfs_set_trans_block_group(trans, inode);
2041 btrfs_update_inode(trans, root, inode);
2042 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002043}
2044
Josef Bacikaec74772008-07-24 12:12:38 -04002045static int btrfs_set_inode_index_count(struct inode *inode)
2046{
2047 struct btrfs_root *root = BTRFS_I(inode)->root;
2048 struct btrfs_key key, found_key;
2049 struct btrfs_path *path;
2050 struct extent_buffer *leaf;
2051 int ret;
2052
2053 key.objectid = inode->i_ino;
2054 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2055 key.offset = (u64)-1;
2056
2057 path = btrfs_alloc_path();
2058 if (!path)
2059 return -ENOMEM;
2060
2061 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2062 if (ret < 0)
2063 goto out;
2064 /* FIXME: we should be able to handle this */
2065 if (ret == 0)
2066 goto out;
2067 ret = 0;
2068
2069 /*
2070 * MAGIC NUMBER EXPLANATION:
2071 * since we search a directory based on f_pos we have to start at 2
2072 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2073 * else has to start at 2
2074 */
2075 if (path->slots[0] == 0) {
2076 BTRFS_I(inode)->index_cnt = 2;
2077 goto out;
2078 }
2079
2080 path->slots[0]--;
2081
2082 leaf = path->nodes[0];
2083 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2084
2085 if (found_key.objectid != inode->i_ino ||
2086 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2087 BTRFS_I(inode)->index_cnt = 2;
2088 goto out;
2089 }
2090
2091 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2092out:
2093 btrfs_free_path(path);
2094 return ret;
2095}
2096
2097static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2098{
2099 int ret = 0;
2100
2101 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2102 ret = btrfs_set_inode_index_count(dir);
2103 if (ret)
2104 return ret;
2105 }
2106
2107 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2108 BTRFS_I(dir)->index_cnt++;
2109
2110 return ret;
2111}
2112
Chris Mason39279cc2007-06-12 06:35:45 -04002113static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2114 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002115 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002116 const char *name, int name_len,
2117 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002118 u64 objectid,
2119 struct btrfs_block_group_cache *group,
2120 int mode)
2121{
2122 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002123 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002124 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002125 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002126 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002127 struct btrfs_inode_ref *ref;
2128 struct btrfs_key key[2];
2129 u32 sizes[2];
2130 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002131 int ret;
2132 int owner;
2133
Chris Mason5f39d392007-10-15 16:14:19 -04002134 path = btrfs_alloc_path();
2135 BUG_ON(!path);
2136
Chris Mason39279cc2007-06-12 06:35:45 -04002137 inode = new_inode(root->fs_info->sb);
2138 if (!inode)
2139 return ERR_PTR(-ENOMEM);
2140
Josef Bacikaec74772008-07-24 12:12:38 -04002141 if (dir) {
2142 ret = btrfs_set_inode_index(dir, inode);
2143 if (ret)
2144 return ERR_PTR(ret);
2145 } else {
2146 BTRFS_I(inode)->index = 0;
2147 }
2148 /*
2149 * index_cnt is ignored for everything but a dir,
2150 * btrfs_get_inode_index_count has an explanation for the magic
2151 * number
2152 */
2153 BTRFS_I(inode)->index_cnt = 2;
2154
Chris Masond1310b22008-01-24 16:13:08 -05002155 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2156 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002157 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002158 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2159 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002160 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002161 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002162 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002163 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002164 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002165 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002166
Chris Mason39279cc2007-06-12 06:35:45 -04002167 if (mode & S_IFDIR)
2168 owner = 0;
2169 else
2170 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002171 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002172 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002173 if (!new_inode_group) {
2174 printk("find_block group failed\n");
2175 new_inode_group = group;
2176 }
2177 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002178 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002179
2180 key[0].objectid = objectid;
2181 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2182 key[0].offset = 0;
2183
2184 key[1].objectid = objectid;
2185 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2186 key[1].offset = ref_objectid;
2187
2188 sizes[0] = sizeof(struct btrfs_inode_item);
2189 sizes[1] = name_len + sizeof(*ref);
2190
2191 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2192 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002193 goto fail;
2194
Chris Mason9c583092008-01-29 15:15:18 -05002195 if (objectid > root->highest_inode)
2196 root->highest_inode = objectid;
2197
Chris Mason39279cc2007-06-12 06:35:45 -04002198 inode->i_uid = current->fsuid;
2199 inode->i_gid = current->fsgid;
2200 inode->i_mode = mode;
2201 inode->i_ino = objectid;
2202 inode->i_blocks = 0;
2203 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002204 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2205 struct btrfs_inode_item);
2206 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002207
2208 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2209 struct btrfs_inode_ref);
2210 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002211 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002212 ptr = (unsigned long)(ref + 1);
2213 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2214
Chris Mason5f39d392007-10-15 16:14:19 -04002215 btrfs_mark_buffer_dirty(path->nodes[0]);
2216 btrfs_free_path(path);
2217
Chris Mason39279cc2007-06-12 06:35:45 -04002218 location = &BTRFS_I(inode)->location;
2219 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002220 location->offset = 0;
2221 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2222
Chris Mason39279cc2007-06-12 06:35:45 -04002223 insert_inode_hash(inode);
2224 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002225fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002226 if (dir)
2227 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002228 btrfs_free_path(path);
2229 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002230}
2231
2232static inline u8 btrfs_inode_type(struct inode *inode)
2233{
2234 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2235}
2236
2237static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002238 struct dentry *dentry, struct inode *inode,
2239 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002240{
2241 int ret;
2242 struct btrfs_key key;
2243 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002244 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002245
Chris Mason39279cc2007-06-12 06:35:45 -04002246 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002247 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2248 key.offset = 0;
2249
2250 ret = btrfs_insert_dir_item(trans, root,
2251 dentry->d_name.name, dentry->d_name.len,
2252 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002253 &key, btrfs_inode_type(inode),
2254 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002255 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002256 if (add_backref) {
2257 ret = btrfs_insert_inode_ref(trans, root,
2258 dentry->d_name.name,
2259 dentry->d_name.len,
2260 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002261 parent_inode->i_ino,
2262 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002263 }
Chris Masondbe674a2008-07-17 12:54:05 -04002264 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2265 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002266 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002267 ret = btrfs_update_inode(trans, root,
2268 dentry->d_parent->d_inode);
2269 }
2270 return ret;
2271}
2272
2273static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002274 struct dentry *dentry, struct inode *inode,
2275 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002276{
Chris Mason9c583092008-01-29 15:15:18 -05002277 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002278 if (!err) {
2279 d_instantiate(dentry, inode);
2280 return 0;
2281 }
2282 if (err > 0)
2283 err = -EEXIST;
2284 return err;
2285}
2286
Josef Bacik618e21d2007-07-11 10:18:17 -04002287static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2288 int mode, dev_t rdev)
2289{
2290 struct btrfs_trans_handle *trans;
2291 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002292 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002293 int err;
2294 int drop_inode = 0;
2295 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002296 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002297
2298 if (!new_valid_dev(rdev))
2299 return -EINVAL;
2300
Chris Mason1832a6d2007-12-21 16:27:21 -05002301 err = btrfs_check_free_space(root, 1, 0);
2302 if (err)
2303 goto fail;
2304
Josef Bacik618e21d2007-07-11 10:18:17 -04002305 trans = btrfs_start_transaction(root, 1);
2306 btrfs_set_trans_block_group(trans, dir);
2307
2308 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2309 if (err) {
2310 err = -ENOSPC;
2311 goto out_unlock;
2312 }
2313
Josef Bacikaec74772008-07-24 12:12:38 -04002314 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002315 dentry->d_name.len,
2316 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002317 BTRFS_I(dir)->block_group, mode);
2318 err = PTR_ERR(inode);
2319 if (IS_ERR(inode))
2320 goto out_unlock;
2321
Josef Bacik33268ea2008-07-24 12:16:36 -04002322 err = btrfs_init_acl(inode, dir);
2323 if (err) {
2324 drop_inode = 1;
2325 goto out_unlock;
2326 }
2327
Josef Bacik618e21d2007-07-11 10:18:17 -04002328 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002329 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002330 if (err)
2331 drop_inode = 1;
2332 else {
2333 inode->i_op = &btrfs_special_inode_operations;
2334 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002335 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002336 }
2337 dir->i_sb->s_dirt = 1;
2338 btrfs_update_inode_block_group(trans, inode);
2339 btrfs_update_inode_block_group(trans, dir);
2340out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002341 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002342 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002343fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002344 if (drop_inode) {
2345 inode_dec_link_count(inode);
2346 iput(inode);
2347 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002348 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002349 return err;
2350}
2351
Chris Mason39279cc2007-06-12 06:35:45 -04002352static int btrfs_create(struct inode *dir, struct dentry *dentry,
2353 int mode, struct nameidata *nd)
2354{
2355 struct btrfs_trans_handle *trans;
2356 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002357 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002358 int err;
2359 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002360 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002361 u64 objectid;
2362
Chris Mason1832a6d2007-12-21 16:27:21 -05002363 err = btrfs_check_free_space(root, 1, 0);
2364 if (err)
2365 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002366 trans = btrfs_start_transaction(root, 1);
2367 btrfs_set_trans_block_group(trans, dir);
2368
2369 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2370 if (err) {
2371 err = -ENOSPC;
2372 goto out_unlock;
2373 }
2374
Josef Bacikaec74772008-07-24 12:12:38 -04002375 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002376 dentry->d_name.len,
2377 dentry->d_parent->d_inode->i_ino,
2378 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002379 err = PTR_ERR(inode);
2380 if (IS_ERR(inode))
2381 goto out_unlock;
2382
Josef Bacik33268ea2008-07-24 12:16:36 -04002383 err = btrfs_init_acl(inode, dir);
2384 if (err) {
2385 drop_inode = 1;
2386 goto out_unlock;
2387 }
2388
Chris Mason39279cc2007-06-12 06:35:45 -04002389 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002390 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002391 if (err)
2392 drop_inode = 1;
2393 else {
2394 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002395 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002396 inode->i_fop = &btrfs_file_operations;
2397 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002398 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2399 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002400 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002401 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2402 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002403 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002404 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002405 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002406 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002407 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002408 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002409 }
2410 dir->i_sb->s_dirt = 1;
2411 btrfs_update_inode_block_group(trans, inode);
2412 btrfs_update_inode_block_group(trans, dir);
2413out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002414 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002415 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002416fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002417 if (drop_inode) {
2418 inode_dec_link_count(inode);
2419 iput(inode);
2420 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002421 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002422 return err;
2423}
2424
2425static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2426 struct dentry *dentry)
2427{
2428 struct btrfs_trans_handle *trans;
2429 struct btrfs_root *root = BTRFS_I(dir)->root;
2430 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002431 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002432 int err;
2433 int drop_inode = 0;
2434
2435 if (inode->i_nlink == 0)
2436 return -ENOENT;
2437
Chris Mason6da6aba2007-12-18 16:15:09 -05002438#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2439 inode->i_nlink++;
2440#else
Chris Mason39279cc2007-06-12 06:35:45 -04002441 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002442#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002443 err = btrfs_check_free_space(root, 1, 0);
2444 if (err)
2445 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002446 err = btrfs_set_inode_index(dir, inode);
2447 if (err)
2448 goto fail;
2449
Chris Mason39279cc2007-06-12 06:35:45 -04002450 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002451
Chris Mason39279cc2007-06-12 06:35:45 -04002452 btrfs_set_trans_block_group(trans, dir);
2453 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002454
Chris Mason9c583092008-01-29 15:15:18 -05002455 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002456
Chris Mason39279cc2007-06-12 06:35:45 -04002457 if (err)
2458 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002459
Chris Mason39279cc2007-06-12 06:35:45 -04002460 dir->i_sb->s_dirt = 1;
2461 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002462 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002463
Chris Mason54aa1f42007-06-22 14:16:25 -04002464 if (err)
2465 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002466
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002467 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002468 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002469fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002470 if (drop_inode) {
2471 inode_dec_link_count(inode);
2472 iput(inode);
2473 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002474 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002475 return err;
2476}
2477
Chris Mason39279cc2007-06-12 06:35:45 -04002478static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2479{
Chris Masonb9d86662008-05-02 16:13:49 -04002480 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002481 struct btrfs_trans_handle *trans;
2482 struct btrfs_root *root = BTRFS_I(dir)->root;
2483 int err = 0;
2484 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002485 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002486 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002487
Chris Mason1832a6d2007-12-21 16:27:21 -05002488 err = btrfs_check_free_space(root, 1, 0);
2489 if (err)
2490 goto out_unlock;
2491
Chris Mason39279cc2007-06-12 06:35:45 -04002492 trans = btrfs_start_transaction(root, 1);
2493 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002494
Chris Mason39279cc2007-06-12 06:35:45 -04002495 if (IS_ERR(trans)) {
2496 err = PTR_ERR(trans);
2497 goto out_unlock;
2498 }
2499
2500 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2501 if (err) {
2502 err = -ENOSPC;
2503 goto out_unlock;
2504 }
2505
Josef Bacikaec74772008-07-24 12:12:38 -04002506 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002507 dentry->d_name.len,
2508 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002509 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2510 if (IS_ERR(inode)) {
2511 err = PTR_ERR(inode);
2512 goto out_fail;
2513 }
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002516
2517 err = btrfs_init_acl(inode, dir);
2518 if (err)
2519 goto out_fail;
2520
Chris Mason39279cc2007-06-12 06:35:45 -04002521 inode->i_op = &btrfs_dir_inode_operations;
2522 inode->i_fop = &btrfs_dir_file_operations;
2523 btrfs_set_trans_block_group(trans, inode);
2524
Chris Masondbe674a2008-07-17 12:54:05 -04002525 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002526 err = btrfs_update_inode(trans, root, inode);
2527 if (err)
2528 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002529
Chris Mason9c583092008-01-29 15:15:18 -05002530 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002531 if (err)
2532 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002533
Chris Mason39279cc2007-06-12 06:35:45 -04002534 d_instantiate(dentry, inode);
2535 drop_on_err = 0;
2536 dir->i_sb->s_dirt = 1;
2537 btrfs_update_inode_block_group(trans, inode);
2538 btrfs_update_inode_block_group(trans, dir);
2539
2540out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002541 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002542 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002543
Chris Mason39279cc2007-06-12 06:35:45 -04002544out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002545 if (drop_on_err)
2546 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002547 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002548 return err;
2549}
2550
Chris Mason3b951512008-04-17 11:29:12 -04002551static int merge_extent_mapping(struct extent_map_tree *em_tree,
2552 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002553 struct extent_map *em,
2554 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002555{
2556 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002557
Chris Masone6dcd2d2008-07-17 12:53:50 -04002558 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2559 start_diff = map_start - em->start;
2560 em->start = map_start;
2561 em->len = map_len;
2562 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2563 em->block_start += start_diff;
2564 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002565}
2566
Chris Masona52d9a82007-08-27 16:49:44 -04002567struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002568 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002569 int create)
2570{
2571 int ret;
2572 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002573 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002574 u64 extent_start = 0;
2575 u64 extent_end = 0;
2576 u64 objectid = inode->i_ino;
2577 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002578 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002579 struct btrfs_root *root = BTRFS_I(inode)->root;
2580 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002581 struct extent_buffer *leaf;
2582 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002583 struct extent_map *em = NULL;
2584 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002585 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002586 struct btrfs_trans_handle *trans = NULL;
2587
Chris Masona52d9a82007-08-27 16:49:44 -04002588again:
Chris Masond1310b22008-01-24 16:13:08 -05002589 spin_lock(&em_tree->lock);
2590 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002591 if (em)
2592 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002593 spin_unlock(&em_tree->lock);
2594
Chris Masona52d9a82007-08-27 16:49:44 -04002595 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002596 if (em->start > start || em->start + em->len <= start)
2597 free_extent_map(em);
2598 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002599 free_extent_map(em);
2600 else
2601 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002602 }
Chris Masond1310b22008-01-24 16:13:08 -05002603 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002604 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002605 err = -ENOMEM;
2606 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002607 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002609 em->start = EXTENT_MAP_HOLE;
2610 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002611
2612 if (!path) {
2613 path = btrfs_alloc_path();
2614 BUG_ON(!path);
2615 }
2616
Chris Mason179e29e2007-11-01 11:28:41 -04002617 ret = btrfs_lookup_file_extent(trans, root, path,
2618 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002619 if (ret < 0) {
2620 err = ret;
2621 goto out;
2622 }
2623
2624 if (ret != 0) {
2625 if (path->slots[0] == 0)
2626 goto not_found;
2627 path->slots[0]--;
2628 }
2629
Chris Mason5f39d392007-10-15 16:14:19 -04002630 leaf = path->nodes[0];
2631 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002632 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002633 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002634 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2635 found_type = btrfs_key_type(&found_key);
2636 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002637 found_type != BTRFS_EXTENT_DATA_KEY) {
2638 goto not_found;
2639 }
2640
Chris Mason5f39d392007-10-15 16:14:19 -04002641 found_type = btrfs_file_extent_type(leaf, item);
2642 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002643 if (found_type == BTRFS_FILE_EXTENT_REG) {
2644 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002645 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002646 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002647 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002648 em->start = start;
2649 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002650 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002651 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002652 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002653 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002654 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002655 }
2656 goto not_found_em;
2657 }
Chris Masondb945352007-10-15 16:15:53 -04002658 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2659 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002660 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002661 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002662 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002663 goto insert;
2664 }
Chris Masondb945352007-10-15 16:15:53 -04002665 bytenr += btrfs_file_extent_offset(leaf, item);
2666 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002667 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002668 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002669 goto insert;
2670 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002671 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002672 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002673 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002674 size_t size;
2675 size_t extent_offset;
2676 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002677
Chris Mason5f39d392007-10-15 16:14:19 -04002678 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2679 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002680 extent_end = (extent_start + size + root->sectorsize - 1) &
2681 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002682 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002683 em->start = start;
2684 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002685 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002686 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002687 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002688 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002689 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002690 }
2691 goto not_found_em;
2692 }
2693 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002694
2695 if (!page) {
2696 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002697 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002698 goto out;
2699 }
2700
Chris Mason70dec802008-01-29 09:59:12 -05002701 page_start = page_offset(page) + pg_offset;
2702 extent_offset = page_start - extent_start;
2703 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002704 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002705 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002706 em->len = (copy_size + root->sectorsize - 1) &
2707 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002708 map = kmap(page);
2709 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002710 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002711 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002712 copy_size);
2713 flush_dcache_page(page);
2714 } else if (create && PageUptodate(page)) {
2715 if (!trans) {
2716 kunmap(page);
2717 free_extent_map(em);
2718 em = NULL;
2719 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002720 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002721 goto again;
2722 }
Chris Mason70dec802008-01-29 09:59:12 -05002723 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002724 copy_size);
2725 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002726 }
Chris Masona52d9a82007-08-27 16:49:44 -04002727 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002728 set_extent_uptodate(io_tree, em->start,
2729 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002730 goto insert;
2731 } else {
2732 printk("unkknown found_type %d\n", found_type);
2733 WARN_ON(1);
2734 }
2735not_found:
2736 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002737 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002738not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002739 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002740insert:
2741 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002742 if (em->start > start || extent_map_end(em) <= start) {
2743 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002744 err = -EIO;
2745 goto out;
2746 }
Chris Masond1310b22008-01-24 16:13:08 -05002747
2748 err = 0;
2749 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002750 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002751 /* it is possible that someone inserted the extent into the tree
2752 * while we had the lock dropped. It is also possible that
2753 * an overlapping map exists in the tree
2754 */
Chris Masona52d9a82007-08-27 16:49:44 -04002755 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002756 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002757
2758 ret = 0;
2759
Chris Mason3b951512008-04-17 11:29:12 -04002760 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002761 if (existing && (existing->start > start ||
2762 existing->start + existing->len <= start)) {
2763 free_extent_map(existing);
2764 existing = NULL;
2765 }
Chris Mason3b951512008-04-17 11:29:12 -04002766 if (!existing) {
2767 existing = lookup_extent_mapping(em_tree, em->start,
2768 em->len);
2769 if (existing) {
2770 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002771 em, start,
2772 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002773 free_extent_map(existing);
2774 if (err) {
2775 free_extent_map(em);
2776 em = NULL;
2777 }
2778 } else {
2779 err = -EIO;
2780 printk("failing to insert %Lu %Lu\n",
2781 start, len);
2782 free_extent_map(em);
2783 em = NULL;
2784 }
2785 } else {
2786 free_extent_map(em);
2787 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002788 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002789 }
Chris Masona52d9a82007-08-27 16:49:44 -04002790 }
Chris Masond1310b22008-01-24 16:13:08 -05002791 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002792out:
Chris Masonf4219502008-07-22 11:18:09 -04002793 if (path)
2794 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002795 if (trans) {
2796 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002797 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002798 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002799 }
Chris Masona52d9a82007-08-27 16:49:44 -04002800 }
Chris Masona52d9a82007-08-27 16:49:44 -04002801 if (err) {
2802 free_extent_map(em);
2803 WARN_ON(1);
2804 return ERR_PTR(err);
2805 }
2806 return em;
2807}
2808
Chris Masone1c4b742008-04-22 13:26:46 -04002809#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002810static int btrfs_get_block(struct inode *inode, sector_t iblock,
2811 struct buffer_head *bh_result, int create)
2812{
2813 struct extent_map *em;
2814 u64 start = (u64)iblock << inode->i_blkbits;
2815 struct btrfs_multi_bio *multi = NULL;
2816 struct btrfs_root *root = BTRFS_I(inode)->root;
2817 u64 len;
2818 u64 logical;
2819 u64 map_length;
2820 int ret = 0;
2821
2822 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2823
2824 if (!em || IS_ERR(em))
2825 goto out;
2826
Chris Masone1c4b742008-04-22 13:26:46 -04002827 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002828 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002829 }
Chris Mason16432982008-04-10 10:23:21 -04002830
2831 if (em->block_start == EXTENT_MAP_INLINE) {
2832 ret = -EINVAL;
2833 goto out;
2834 }
2835
Chris Mason16432982008-04-10 10:23:21 -04002836 len = em->start + em->len - start;
2837 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2838
Chris Masone1c4b742008-04-22 13:26:46 -04002839 if (em->block_start == EXTENT_MAP_HOLE ||
2840 em->block_start == EXTENT_MAP_DELALLOC) {
2841 bh_result->b_size = len;
2842 goto out;
2843 }
2844
Chris Mason16432982008-04-10 10:23:21 -04002845 logical = start - em->start;
2846 logical = em->block_start + logical;
2847
2848 map_length = len;
2849 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2850 logical, &map_length, &multi, 0);
2851 BUG_ON(ret);
2852 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2853 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002854
Chris Mason16432982008-04-10 10:23:21 -04002855 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2856 set_buffer_mapped(bh_result);
2857 kfree(multi);
2858out:
2859 free_extent_map(em);
2860 return ret;
2861}
Chris Masone1c4b742008-04-22 13:26:46 -04002862#endif
Chris Mason16432982008-04-10 10:23:21 -04002863
2864static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2865 const struct iovec *iov, loff_t offset,
2866 unsigned long nr_segs)
2867{
Chris Masone1c4b742008-04-22 13:26:46 -04002868 return -EINVAL;
2869#if 0
Chris Mason16432982008-04-10 10:23:21 -04002870 struct file *file = iocb->ki_filp;
2871 struct inode *inode = file->f_mapping->host;
2872
2873 if (rw == WRITE)
2874 return -EINVAL;
2875
2876 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2877 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002878#endif
Chris Mason16432982008-04-10 10:23:21 -04002879}
2880
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002881static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002882{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002883 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002884}
2885
Chris Mason9ebefb182007-06-15 13:50:00 -04002886int btrfs_readpage(struct file *file, struct page *page)
2887{
Chris Masond1310b22008-01-24 16:13:08 -05002888 struct extent_io_tree *tree;
2889 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002890 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002891}
Chris Mason1832a6d2007-12-21 16:27:21 -05002892
Chris Mason39279cc2007-06-12 06:35:45 -04002893static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2894{
Chris Masond1310b22008-01-24 16:13:08 -05002895 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002896
2897
2898 if (current->flags & PF_MEMALLOC) {
2899 redirty_page_for_writepage(wbc, page);
2900 unlock_page(page);
2901 return 0;
2902 }
Chris Masond1310b22008-01-24 16:13:08 -05002903 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002904 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2905}
Chris Mason39279cc2007-06-12 06:35:45 -04002906
Chris Masonf4219502008-07-22 11:18:09 -04002907int btrfs_writepages(struct address_space *mapping,
2908 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002909{
Chris Masond1310b22008-01-24 16:13:08 -05002910 struct extent_io_tree *tree;
2911 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002912 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2913}
2914
Chris Mason3ab2fb52007-11-08 10:59:22 -05002915static int
2916btrfs_readpages(struct file *file, struct address_space *mapping,
2917 struct list_head *pages, unsigned nr_pages)
2918{
Chris Masond1310b22008-01-24 16:13:08 -05002919 struct extent_io_tree *tree;
2920 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002921 return extent_readpages(tree, mapping, pages, nr_pages,
2922 btrfs_get_extent);
2923}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002924static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002925{
Chris Masond1310b22008-01-24 16:13:08 -05002926 struct extent_io_tree *tree;
2927 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002928 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002929
Chris Masond1310b22008-01-24 16:13:08 -05002930 tree = &BTRFS_I(page->mapping->host)->io_tree;
2931 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002932 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002933 if (ret == 1) {
2934 ClearPagePrivate(page);
2935 set_page_private(page, 0);
2936 page_cache_release(page);
2937 }
2938 return ret;
2939}
Chris Mason39279cc2007-06-12 06:35:45 -04002940
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2942{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002943 return __btrfs_releasepage(page, gfp_flags);
2944}
2945
Chris Masona52d9a82007-08-27 16:49:44 -04002946static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2947{
Chris Masond1310b22008-01-24 16:13:08 -05002948 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002949 struct btrfs_ordered_extent *ordered;
2950 u64 page_start = page_offset(page);
2951 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002952
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002954 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002955 if (offset) {
2956 btrfs_releasepage(page, GFP_NOFS);
2957 return;
2958 }
2959
2960 lock_extent(tree, page_start, page_end, GFP_NOFS);
2961 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2962 page_offset(page));
2963 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002964 /*
2965 * IO on this page will never be started, so we need
2966 * to account for any ordered extents now
2967 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002968 clear_extent_bit(tree, page_start, page_end,
2969 EXTENT_DIRTY | EXTENT_DELALLOC |
2970 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002971 btrfs_finish_ordered_io(page->mapping->host,
2972 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973 btrfs_put_ordered_extent(ordered);
2974 lock_extent(tree, page_start, page_end, GFP_NOFS);
2975 }
2976 clear_extent_bit(tree, page_start, page_end,
2977 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2978 EXTENT_ORDERED,
2979 1, 1, GFP_NOFS);
2980 __btrfs_releasepage(page, GFP_NOFS);
2981
Chris Mason4a096752008-07-21 10:29:44 -04002982 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002983 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002984 ClearPagePrivate(page);
2985 set_page_private(page, 0);
2986 page_cache_release(page);
2987 }
Chris Mason39279cc2007-06-12 06:35:45 -04002988}
2989
Chris Mason9ebefb182007-06-15 13:50:00 -04002990/*
2991 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2992 * called from a page fault handler when a page is first dirtied. Hence we must
2993 * be careful to check for EOF conditions here. We set the page up correctly
2994 * for a written page which means we get ENOSPC checking when writing into
2995 * holes and correct delalloc and unwritten extent mapping on filesystems that
2996 * support these features.
2997 *
2998 * We are not allowed to take the i_mutex here so we have to play games to
2999 * protect against truncate races as the page could now be beyond EOF. Because
3000 * vmtruncate() writes the inode size before removing pages, once we have the
3001 * page lock we can determine safely if the page is beyond EOF. If it is not
3002 * beyond EOF, then the page is guaranteed safe against truncation until we
3003 * unlock the page.
3004 */
3005int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3006{
Chris Mason6da6aba2007-12-18 16:15:09 -05003007 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003008 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3010 struct btrfs_ordered_extent *ordered;
3011 char *kaddr;
3012 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003013 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003014 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003015 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003016 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003017
Chris Mason1832a6d2007-12-21 16:27:21 -05003018 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003019 if (ret)
3020 goto out;
3021
3022 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003024 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003025 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003026 page_start = page_offset(page);
3027 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003028
Chris Mason9ebefb182007-06-15 13:50:00 -04003029 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003030 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003031 /* page got truncated out from underneath us */
3032 goto out_unlock;
3033 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003034 wait_on_page_writeback(page);
3035
3036 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3037 set_page_extent_mapped(page);
3038
Chris Masoneb84ae02008-07-17 13:53:27 -04003039 /*
3040 * we can't set the delalloc bits if there are pending ordered
3041 * extents. Drop our locks and wait for them to finish
3042 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003043 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3044 if (ordered) {
3045 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3046 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003047 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003048 btrfs_put_ordered_extent(ordered);
3049 goto again;
3050 }
3051
3052 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3053 page_end, GFP_NOFS);
3054 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003055
3056 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003057 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003058 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003059 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003061
Chris Masone6dcd2d2008-07-17 12:53:50 -04003062 if (zero_start != PAGE_CACHE_SIZE) {
3063 kaddr = kmap(page);
3064 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3065 flush_dcache_page(page);
3066 kunmap(page);
3067 }
Chris Mason247e7432008-07-17 12:53:51 -04003068 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003069 set_page_dirty(page);
3070 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003071
3072out_unlock:
3073 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003074out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003075 return ret;
3076}
3077
Chris Mason39279cc2007-06-12 06:35:45 -04003078static void btrfs_truncate(struct inode *inode)
3079{
3080 struct btrfs_root *root = BTRFS_I(inode)->root;
3081 int ret;
3082 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003083 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003084 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003085
3086 if (!S_ISREG(inode->i_mode))
3087 return;
3088 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3089 return;
3090
3091 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003092 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003093
Chris Mason39279cc2007-06-12 06:35:45 -04003094 trans = btrfs_start_transaction(root, 1);
3095 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003096 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003097
Josef Bacik7b128762008-07-24 12:17:14 -04003098 ret = btrfs_orphan_add(trans, inode);
3099 if (ret)
3100 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003101 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003102 ret = btrfs_truncate_in_trans(trans, root, inode,
3103 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003104 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003105
Josef Bacik7b128762008-07-24 12:17:14 -04003106 ret = btrfs_orphan_del(trans, inode);
3107 BUG_ON(ret);
3108
3109out:
3110 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003111 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003112 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003113 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003114}
3115
Sven Wegener3b963622008-06-09 21:57:42 -04003116/*
3117 * Invalidate a single dcache entry at the root of the filesystem.
3118 * Needed after creation of snapshot or subvolume.
3119 */
3120void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3121 int namelen)
3122{
3123 struct dentry *alias, *entry;
3124 struct qstr qstr;
3125
3126 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3127 if (alias) {
3128 qstr.name = name;
3129 qstr.len = namelen;
3130 /* change me if btrfs ever gets a d_hash operation */
3131 qstr.hash = full_name_hash(qstr.name, qstr.len);
3132 entry = d_lookup(alias, &qstr);
3133 dput(alias);
3134 if (entry) {
3135 d_invalidate(entry);
3136 dput(entry);
3137 }
3138 }
3139}
3140
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003141int btrfs_create_subvol_root(struct btrfs_root *new_root,
3142 struct btrfs_trans_handle *trans, u64 new_dirid,
3143 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003144{
Chris Mason39279cc2007-06-12 06:35:45 -04003145 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003146
Josef Bacikaec74772008-07-24 12:12:38 -04003147 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003148 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003149 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003150 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003151 inode->i_op = &btrfs_dir_inode_operations;
3152 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003153 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003154
Chris Mason39279cc2007-06-12 06:35:45 -04003155 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003156 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003157
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003158 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003159}
3160
Chris Masonedbd8d42007-12-21 16:27:24 -05003161unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003162 struct file_ra_state *ra, struct file *file,
3163 pgoff_t offset, pgoff_t last_index)
3164{
Chris Mason8e7bf942008-04-28 09:02:36 -04003165 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003166
3167#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003168 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3169 return offset;
3170#else
Chris Mason86479a02007-09-10 19:58:16 -04003171 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3172 return offset + req_size;
3173#endif
3174}
3175
Chris Mason39279cc2007-06-12 06:35:45 -04003176struct inode *btrfs_alloc_inode(struct super_block *sb)
3177{
3178 struct btrfs_inode *ei;
3179
3180 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3181 if (!ei)
3182 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003183 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003184 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003185 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3186 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003187 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003188 return &ei->vfs_inode;
3189}
3190
3191void btrfs_destroy_inode(struct inode *inode)
3192{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003193 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003194 WARN_ON(!list_empty(&inode->i_dentry));
3195 WARN_ON(inode->i_data.nrpages);
3196
Josef Bacik33268ea2008-07-24 12:16:36 -04003197 if (BTRFS_I(inode)->i_acl &&
3198 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3199 posix_acl_release(BTRFS_I(inode)->i_acl);
3200 if (BTRFS_I(inode)->i_default_acl &&
3201 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3202 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3203
Yanbcc63ab2008-07-30 16:29:20 -04003204 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003205 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3206 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3207 " list\n", inode->i_ino);
3208 dump_stack();
3209 }
Yanbcc63ab2008-07-30 16:29:20 -04003210 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003211
Chris Masone6dcd2d2008-07-17 12:53:50 -04003212 while(1) {
3213 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3214 if (!ordered)
3215 break;
3216 else {
3217 printk("found ordered extent %Lu %Lu\n",
3218 ordered->file_offset, ordered->len);
3219 btrfs_remove_ordered_extent(inode, ordered);
3220 btrfs_put_ordered_extent(ordered);
3221 btrfs_put_ordered_extent(ordered);
3222 }
3223 }
Chris Mason8c416c92008-01-14 15:10:26 -05003224 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003225 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3226}
3227
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003228#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3229static void init_once(void *foo)
3230#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003231static void init_once(struct kmem_cache * cachep, void *foo)
3232#else
Chris Mason39279cc2007-06-12 06:35:45 -04003233static void init_once(void * foo, struct kmem_cache * cachep,
3234 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003235#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003236{
3237 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3238
3239 inode_init_once(&ei->vfs_inode);
3240}
3241
3242void btrfs_destroy_cachep(void)
3243{
3244 if (btrfs_inode_cachep)
3245 kmem_cache_destroy(btrfs_inode_cachep);
3246 if (btrfs_trans_handle_cachep)
3247 kmem_cache_destroy(btrfs_trans_handle_cachep);
3248 if (btrfs_transaction_cachep)
3249 kmem_cache_destroy(btrfs_transaction_cachep);
3250 if (btrfs_bit_radix_cachep)
3251 kmem_cache_destroy(btrfs_bit_radix_cachep);
3252 if (btrfs_path_cachep)
3253 kmem_cache_destroy(btrfs_path_cachep);
3254}
3255
Chris Mason86479a02007-09-10 19:58:16 -04003256struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003257 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003258#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3259 void (*ctor)(void *)
3260#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003261 void (*ctor)(struct kmem_cache *, void *)
3262#else
Chris Mason92fee662007-07-25 12:31:35 -04003263 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003264 unsigned long)
3265#endif
3266 )
Chris Mason92fee662007-07-25 12:31:35 -04003267{
3268 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3269 SLAB_MEM_SPREAD | extra_flags), ctor
3270#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3271 ,NULL
3272#endif
3273 );
3274}
3275
Chris Mason39279cc2007-06-12 06:35:45 -04003276int btrfs_init_cachep(void)
3277{
Chris Mason86479a02007-09-10 19:58:16 -04003278 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003279 sizeof(struct btrfs_inode),
3280 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003281 if (!btrfs_inode_cachep)
3282 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003283 btrfs_trans_handle_cachep =
3284 btrfs_cache_create("btrfs_trans_handle_cache",
3285 sizeof(struct btrfs_trans_handle),
3286 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003287 if (!btrfs_trans_handle_cachep)
3288 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003289 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003290 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003291 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003292 if (!btrfs_transaction_cachep)
3293 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003294 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003295 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003296 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003297 if (!btrfs_path_cachep)
3298 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003299 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003300 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003301 if (!btrfs_bit_radix_cachep)
3302 goto fail;
3303 return 0;
3304fail:
3305 btrfs_destroy_cachep();
3306 return -ENOMEM;
3307}
3308
3309static int btrfs_getattr(struct vfsmount *mnt,
3310 struct dentry *dentry, struct kstat *stat)
3311{
3312 struct inode *inode = dentry->d_inode;
3313 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003314 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003315 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003316 return 0;
3317}
3318
3319static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3320 struct inode * new_dir,struct dentry *new_dentry)
3321{
3322 struct btrfs_trans_handle *trans;
3323 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3324 struct inode *new_inode = new_dentry->d_inode;
3325 struct inode *old_inode = old_dentry->d_inode;
3326 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003327 int ret;
3328
3329 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3330 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3331 return -ENOTEMPTY;
3332 }
Chris Mason5f39d392007-10-15 16:14:19 -04003333
Chris Mason1832a6d2007-12-21 16:27:21 -05003334 ret = btrfs_check_free_space(root, 1, 0);
3335 if (ret)
3336 goto out_unlock;
3337
Chris Mason39279cc2007-06-12 06:35:45 -04003338 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003339
Chris Mason39279cc2007-06-12 06:35:45 -04003340 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003341
3342 old_dentry->d_inode->i_nlink++;
3343 old_dir->i_ctime = old_dir->i_mtime = ctime;
3344 new_dir->i_ctime = new_dir->i_mtime = ctime;
3345 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003346
Chris Mason39279cc2007-06-12 06:35:45 -04003347 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3348 if (ret)
3349 goto out_fail;
3350
3351 if (new_inode) {
3352 new_inode->i_ctime = CURRENT_TIME;
3353 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3354 if (ret)
3355 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003356 if (new_inode->i_nlink == 0) {
3357 ret = btrfs_orphan_add(trans, new_inode);
3358 if (ret)
3359 goto out_fail;
3360 }
Chris Mason39279cc2007-06-12 06:35:45 -04003361 }
Josef Bacikaec74772008-07-24 12:12:38 -04003362 ret = btrfs_set_inode_index(new_dir, old_inode);
3363 if (ret)
3364 goto out_fail;
3365
Chris Mason9c583092008-01-29 15:15:18 -05003366 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003367 if (ret)
3368 goto out_fail;
3369
3370out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003371 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003372out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003373 return ret;
3374}
3375
3376static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3377 const char *symname)
3378{
3379 struct btrfs_trans_handle *trans;
3380 struct btrfs_root *root = BTRFS_I(dir)->root;
3381 struct btrfs_path *path;
3382 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003383 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003384 int err;
3385 int drop_inode = 0;
3386 u64 objectid;
3387 int name_len;
3388 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003389 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003390 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003391 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003392 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003393
3394 name_len = strlen(symname) + 1;
3395 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3396 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003397
Chris Mason1832a6d2007-12-21 16:27:21 -05003398 err = btrfs_check_free_space(root, 1, 0);
3399 if (err)
3400 goto out_fail;
3401
Chris Mason39279cc2007-06-12 06:35:45 -04003402 trans = btrfs_start_transaction(root, 1);
3403 btrfs_set_trans_block_group(trans, dir);
3404
3405 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3406 if (err) {
3407 err = -ENOSPC;
3408 goto out_unlock;
3409 }
3410
Josef Bacikaec74772008-07-24 12:12:38 -04003411 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003412 dentry->d_name.len,
3413 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003414 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3415 err = PTR_ERR(inode);
3416 if (IS_ERR(inode))
3417 goto out_unlock;
3418
Josef Bacik33268ea2008-07-24 12:16:36 -04003419 err = btrfs_init_acl(inode, dir);
3420 if (err) {
3421 drop_inode = 1;
3422 goto out_unlock;
3423 }
3424
Chris Mason39279cc2007-06-12 06:35:45 -04003425 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003426 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 if (err)
3428 drop_inode = 1;
3429 else {
3430 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003431 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003432 inode->i_fop = &btrfs_file_operations;
3433 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003434 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3435 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003436 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003437 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3438 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003439 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003440 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003441 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003442 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003443 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003444 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003445 }
3446 dir->i_sb->s_dirt = 1;
3447 btrfs_update_inode_block_group(trans, inode);
3448 btrfs_update_inode_block_group(trans, dir);
3449 if (drop_inode)
3450 goto out_unlock;
3451
3452 path = btrfs_alloc_path();
3453 BUG_ON(!path);
3454 key.objectid = inode->i_ino;
3455 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003456 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3457 datasize = btrfs_file_extent_calc_inline_size(name_len);
3458 err = btrfs_insert_empty_item(trans, root, path, &key,
3459 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003460 if (err) {
3461 drop_inode = 1;
3462 goto out_unlock;
3463 }
Chris Mason5f39d392007-10-15 16:14:19 -04003464 leaf = path->nodes[0];
3465 ei = btrfs_item_ptr(leaf, path->slots[0],
3466 struct btrfs_file_extent_item);
3467 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3468 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003469 BTRFS_FILE_EXTENT_INLINE);
3470 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003471 write_extent_buffer(leaf, symname, ptr, name_len);
3472 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003473 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003474
Chris Mason39279cc2007-06-12 06:35:45 -04003475 inode->i_op = &btrfs_symlink_inode_operations;
3476 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003477 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003478 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003479 err = btrfs_update_inode(trans, root, inode);
3480 if (err)
3481 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003482
3483out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003484 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003485 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003486out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003487 if (drop_inode) {
3488 inode_dec_link_count(inode);
3489 iput(inode);
3490 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003491 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003492 return err;
3493}
Chris Mason16432982008-04-10 10:23:21 -04003494
Chris Masone6dcd2d2008-07-17 12:53:50 -04003495static int btrfs_set_page_dirty(struct page *page)
3496{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003497 return __set_page_dirty_nobuffers(page);
3498}
3499
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003500#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3501static int btrfs_permission(struct inode *inode, int mask)
3502#else
Yanfdebe2b2008-01-14 13:26:08 -05003503static int btrfs_permission(struct inode *inode, int mask,
3504 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003505#endif
Yanfdebe2b2008-01-14 13:26:08 -05003506{
3507 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3508 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003509 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003510}
Chris Mason39279cc2007-06-12 06:35:45 -04003511
3512static struct inode_operations btrfs_dir_inode_operations = {
3513 .lookup = btrfs_lookup,
3514 .create = btrfs_create,
3515 .unlink = btrfs_unlink,
3516 .link = btrfs_link,
3517 .mkdir = btrfs_mkdir,
3518 .rmdir = btrfs_rmdir,
3519 .rename = btrfs_rename,
3520 .symlink = btrfs_symlink,
3521 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003522 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003523 .setxattr = generic_setxattr,
3524 .getxattr = generic_getxattr,
3525 .listxattr = btrfs_listxattr,
3526 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003527 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003528};
Chris Mason39279cc2007-06-12 06:35:45 -04003529static struct inode_operations btrfs_dir_ro_inode_operations = {
3530 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003531 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003532};
Chris Mason39279cc2007-06-12 06:35:45 -04003533static struct file_operations btrfs_dir_file_operations = {
3534 .llseek = generic_file_llseek,
3535 .read = generic_read_dir,
3536 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003537 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003538#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003539 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003540#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003541 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003542};
3543
Chris Masond1310b22008-01-24 16:13:08 -05003544static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003545 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003546 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003547 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003548 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003549 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003550 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003551 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003552 .set_bit_hook = btrfs_set_bit_hook,
3553 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003554};
3555
Chris Mason39279cc2007-06-12 06:35:45 -04003556static struct address_space_operations btrfs_aops = {
3557 .readpage = btrfs_readpage,
3558 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003559 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003560 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003561 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003562 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003563 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003564 .invalidatepage = btrfs_invalidatepage,
3565 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003566 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003567};
3568
3569static struct address_space_operations btrfs_symlink_aops = {
3570 .readpage = btrfs_readpage,
3571 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003572 .invalidatepage = btrfs_invalidatepage,
3573 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003574};
3575
3576static struct inode_operations btrfs_file_inode_operations = {
3577 .truncate = btrfs_truncate,
3578 .getattr = btrfs_getattr,
3579 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003580 .setxattr = generic_setxattr,
3581 .getxattr = generic_getxattr,
3582 .listxattr = btrfs_listxattr,
3583 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003584 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003585};
Josef Bacik618e21d2007-07-11 10:18:17 -04003586static struct inode_operations btrfs_special_inode_operations = {
3587 .getattr = btrfs_getattr,
3588 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003589 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003590 .setxattr = generic_setxattr,
3591 .getxattr = generic_getxattr,
3592 .listxattr = btrfs_listxattr,
3593 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003594};
Chris Mason39279cc2007-06-12 06:35:45 -04003595static struct inode_operations btrfs_symlink_inode_operations = {
3596 .readlink = generic_readlink,
3597 .follow_link = page_follow_link_light,
3598 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003599 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003600};