blob: 76365ad9bfbfe7eeaa4f63b17267e086d3010634 [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)) {
379 btrfs_lookup_bio_sums(root, inode, bio);
380 }
Chris Mason0b86a832008-03-24 15:01:56 -0400381 goto mapit;
382 }
Chris Mason065631f2008-02-20 12:07:25 -0500383
Chris Mason44b8bd72008-04-16 11:14:51 -0400384 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
385 inode, rw, bio, mirror_num,
386 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400387mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400388 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500389}
Chris Mason6885f302008-02-20 16:11:05 -0500390
Chris Masonba1da2f2008-07-17 12:54:15 -0400391static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400392 struct inode *inode, u64 file_offset,
393 struct list_head *list)
394{
395 struct list_head *cur;
396 struct btrfs_ordered_sum *sum;
397
398 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400399 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400400 sum = list_entry(cur, struct btrfs_ordered_sum, list);
401 mutex_lock(&BTRFS_I(inode)->csum_mutex);
402 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
403 inode, sum);
404 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 }
406 return 0;
407}
408
Chris Mason247e7432008-07-17 12:53:51 -0400409struct btrfs_writepage_fixup {
410 struct page *page;
411 struct btrfs_work work;
412};
413
414/* see btrfs_writepage_start_hook for details on why this is required */
415void btrfs_writepage_fixup_worker(struct btrfs_work *work)
416{
417 struct btrfs_writepage_fixup *fixup;
418 struct btrfs_ordered_extent *ordered;
419 struct page *page;
420 struct inode *inode;
421 u64 page_start;
422 u64 page_end;
423
424 fixup = container_of(work, struct btrfs_writepage_fixup, work);
425 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400426again:
Chris Mason247e7432008-07-17 12:53:51 -0400427 lock_page(page);
428 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
429 ClearPageChecked(page);
430 goto out_page;
431 }
432
433 inode = page->mapping->host;
434 page_start = page_offset(page);
435 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
436
437 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400438
439 /* already ordered? We're done */
440 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
441 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400442 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400443 }
444
445 ordered = btrfs_lookup_ordered_extent(inode, page_start);
446 if (ordered) {
447 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
448 page_end, GFP_NOFS);
449 unlock_page(page);
450 btrfs_start_ordered_extent(inode, ordered, 1);
451 goto again;
452 }
Chris Mason247e7432008-07-17 12:53:51 -0400453
454 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
455 GFP_NOFS);
456 ClearPageChecked(page);
457out:
458 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
459out_page:
460 unlock_page(page);
461 page_cache_release(page);
462}
463
464/*
465 * There are a few paths in the higher layers of the kernel that directly
466 * set the page dirty bit without asking the filesystem if it is a
467 * good idea. This causes problems because we want to make sure COW
468 * properly happens and the data=ordered rules are followed.
469 *
470 * In our case any range that doesn't have the EXTENT_ORDERED bit set
471 * hasn't been properly setup for IO. We kick off an async process
472 * to fix it up. The async helper will wait for ordered extents, set
473 * the delalloc bit and make it safe to write the page.
474 */
475int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
476{
477 struct inode *inode = page->mapping->host;
478 struct btrfs_writepage_fixup *fixup;
479 struct btrfs_root *root = BTRFS_I(inode)->root;
480 int ret;
481
482 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
483 EXTENT_ORDERED, 0);
484 if (ret)
485 return 0;
486
487 if (PageChecked(page))
488 return -EAGAIN;
489
490 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
491 if (!fixup)
492 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400493
Chris Mason247e7432008-07-17 12:53:51 -0400494 SetPageChecked(page);
495 page_cache_get(page);
496 fixup->work.func = btrfs_writepage_fixup_worker;
497 fixup->page = page;
498 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
499 return -EAGAIN;
500}
501
Chris Mason211f90e2008-07-18 11:56:15 -0400502static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400503{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400504 struct btrfs_root *root = BTRFS_I(inode)->root;
505 struct btrfs_trans_handle *trans;
506 struct btrfs_ordered_extent *ordered_extent;
507 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
508 u64 alloc_hint = 0;
509 struct list_head list;
510 struct btrfs_key ins;
511 int ret;
512
513 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400514 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400515 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400516
Chris Masonf9295742008-07-17 12:54:14 -0400517 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400518
519 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
520 BUG_ON(!ordered_extent);
521
522 lock_extent(io_tree, ordered_extent->file_offset,
523 ordered_extent->file_offset + ordered_extent->len - 1,
524 GFP_NOFS);
525
526 INIT_LIST_HEAD(&list);
527
528 ins.objectid = ordered_extent->start;
529 ins.offset = ordered_extent->len;
530 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400531
Chris Masone6dcd2d2008-07-17 12:53:50 -0400532 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
533 trans->transid, inode->i_ino,
534 ordered_extent->file_offset, &ins);
535 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400536
537 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400538
Chris Masone6dcd2d2008-07-17 12:53:50 -0400539 ret = btrfs_drop_extents(trans, root, inode,
540 ordered_extent->file_offset,
541 ordered_extent->file_offset +
542 ordered_extent->len,
543 ordered_extent->file_offset, &alloc_hint);
544 BUG_ON(ret);
545 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
546 ordered_extent->file_offset,
547 ordered_extent->start,
548 ordered_extent->len,
549 ordered_extent->len, 0);
550 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400551
Chris Masone6dcd2d2008-07-17 12:53:50 -0400552 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
553 ordered_extent->file_offset +
554 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400555 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
556
Chris Masone6dcd2d2008-07-17 12:53:50 -0400557 inode->i_blocks += ordered_extent->len >> 9;
558 unlock_extent(io_tree, ordered_extent->file_offset,
559 ordered_extent->file_offset + ordered_extent->len - 1,
560 GFP_NOFS);
561 add_pending_csums(trans, inode, ordered_extent->file_offset,
562 &ordered_extent->list);
563
Chris Masondbe674a2008-07-17 12:54:05 -0400564 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400565 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400566
Chris Masone6dcd2d2008-07-17 12:53:50 -0400567 /* once for us */
568 btrfs_put_ordered_extent(ordered_extent);
569 /* once for the tree */
570 btrfs_put_ordered_extent(ordered_extent);
571
572 btrfs_update_inode(trans, root, inode);
573 btrfs_end_transaction(trans, root);
574 return 0;
575}
576
Chris Mason211f90e2008-07-18 11:56:15 -0400577int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
578 struct extent_state *state, int uptodate)
579{
580 return btrfs_finish_ordered_io(page->mapping->host, start, end);
581}
582
Chris Mason7e383262008-04-09 16:28:12 -0400583struct io_failure_record {
584 struct page *page;
585 u64 start;
586 u64 len;
587 u64 logical;
588 int last_mirror;
589};
590
Chris Mason1259ab72008-05-12 13:39:03 -0400591int btrfs_io_failed_hook(struct bio *failed_bio,
592 struct page *page, u64 start, u64 end,
593 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400594{
595 struct io_failure_record *failrec = NULL;
596 u64 private;
597 struct extent_map *em;
598 struct inode *inode = page->mapping->host;
599 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400600 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400601 struct bio *bio;
602 int num_copies;
603 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400604 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400605 u64 logical;
606
607 ret = get_state_private(failure_tree, start, &private);
608 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400609 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
610 if (!failrec)
611 return -ENOMEM;
612 failrec->start = start;
613 failrec->len = end - start + 1;
614 failrec->last_mirror = 0;
615
Chris Mason3b951512008-04-17 11:29:12 -0400616 spin_lock(&em_tree->lock);
617 em = lookup_extent_mapping(em_tree, start, failrec->len);
618 if (em->start > start || em->start + em->len < start) {
619 free_extent_map(em);
620 em = NULL;
621 }
622 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400623
624 if (!em || IS_ERR(em)) {
625 kfree(failrec);
626 return -EIO;
627 }
628 logical = start - em->start;
629 logical = em->block_start + logical;
630 failrec->logical = logical;
631 free_extent_map(em);
632 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
633 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400634 set_state_private(failure_tree, start,
635 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400636 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400637 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400638 }
639 num_copies = btrfs_num_copies(
640 &BTRFS_I(inode)->root->fs_info->mapping_tree,
641 failrec->logical, failrec->len);
642 failrec->last_mirror++;
643 if (!state) {
644 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
645 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
646 failrec->start,
647 EXTENT_LOCKED);
648 if (state && state->start != failrec->start)
649 state = NULL;
650 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
651 }
652 if (!state || failrec->last_mirror > num_copies) {
653 set_state_private(failure_tree, failrec->start, 0);
654 clear_extent_bits(failure_tree, failrec->start,
655 failrec->start + failrec->len - 1,
656 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
657 kfree(failrec);
658 return -EIO;
659 }
660 bio = bio_alloc(GFP_NOFS, 1);
661 bio->bi_private = state;
662 bio->bi_end_io = failed_bio->bi_end_io;
663 bio->bi_sector = failrec->logical >> 9;
664 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400665 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400666 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400667 if (failed_bio->bi_rw & (1 << BIO_RW))
668 rw = WRITE;
669 else
670 rw = READ;
671
672 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
673 failrec->last_mirror);
674 return 0;
675}
676
677int btrfs_clean_io_failures(struct inode *inode, u64 start)
678{
679 u64 private;
680 u64 private_failure;
681 struct io_failure_record *failure;
682 int ret;
683
684 private = 0;
685 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
686 (u64)-1, 1, EXTENT_DIRTY)) {
687 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
688 start, &private_failure);
689 if (ret == 0) {
690 failure = (struct io_failure_record *)(unsigned long)
691 private_failure;
692 set_state_private(&BTRFS_I(inode)->io_failure_tree,
693 failure->start, 0);
694 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
695 failure->start,
696 failure->start + failure->len - 1,
697 EXTENT_DIRTY | EXTENT_LOCKED,
698 GFP_NOFS);
699 kfree(failure);
700 }
701 }
Chris Mason7e383262008-04-09 16:28:12 -0400702 return 0;
703}
704
Chris Mason70dec802008-01-29 09:59:12 -0500705int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
706 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400707{
Chris Mason35ebb932007-10-30 16:56:53 -0400708 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400709 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400711 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500712 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400713 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400714 struct btrfs_root *root = BTRFS_I(inode)->root;
715 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400716 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500717
Yanb98b6762008-01-08 15:54:37 -0500718 if (btrfs_test_opt(root, NODATASUM) ||
719 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500720 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500721 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500722 private = state->private;
723 ret = 0;
724 } else {
725 ret = get_state_private(io_tree, start, &private);
726 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400727 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400728 kaddr = kmap_atomic(page, KM_IRQ0);
729 if (ret) {
730 goto zeroit;
731 }
Chris Masonff79f812007-10-15 16:22:25 -0400732 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
733 btrfs_csum_final(csum, (char *)&csum);
734 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400735 goto zeroit;
736 }
737 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400738 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400739
740 /* if the io failure tree for this inode is non-empty,
741 * check to see if we've recovered from a failed IO
742 */
Chris Mason1259ab72008-05-12 13:39:03 -0400743 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400744 return 0;
745
746zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500747 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
748 page->mapping->host->i_ino, (unsigned long long)start, csum,
749 private);
Chris Masondb945352007-10-15 16:15:53 -0400750 memset(kaddr + offset, 1, end - start + 1);
751 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400752 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400753 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400754 if (private == 0)
755 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400756 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400757}
Chris Masonb888db22007-08-27 16:49:44 -0400758
Josef Bacik7b128762008-07-24 12:17:14 -0400759/*
760 * This creates an orphan entry for the given inode in case something goes
761 * wrong in the middle of an unlink/truncate.
762 */
763int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
764{
765 struct btrfs_root *root = BTRFS_I(inode)->root;
766 int ret = 0;
767
Yanbcc63ab2008-07-30 16:29:20 -0400768 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400769
770 /* already on the orphan list, we're good */
771 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400772 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400773 return 0;
774 }
775
776 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
777
Yanbcc63ab2008-07-30 16:29:20 -0400778 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400779
780 /*
781 * insert an orphan item to track this unlinked/truncated file
782 */
783 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
784
785 return ret;
786}
787
788/*
789 * We have done the truncate/delete so we can go ahead and remove the orphan
790 * item for this particular inode.
791 */
792int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
793{
794 struct btrfs_root *root = BTRFS_I(inode)->root;
795 int ret = 0;
796
Yanbcc63ab2008-07-30 16:29:20 -0400797 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400798
799 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400800 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400801 return 0;
802 }
803
804 list_del_init(&BTRFS_I(inode)->i_orphan);
805 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400806 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400807 return 0;
808 }
809
Yanbcc63ab2008-07-30 16:29:20 -0400810 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400811
812 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
813
814 return ret;
815}
816
817/*
818 * this cleans up any orphans that may be left on the list from the last use
819 * of this root.
820 */
821void btrfs_orphan_cleanup(struct btrfs_root *root)
822{
823 struct btrfs_path *path;
824 struct extent_buffer *leaf;
825 struct btrfs_item *item;
826 struct btrfs_key key, found_key;
827 struct btrfs_trans_handle *trans;
828 struct inode *inode;
829 int ret = 0, nr_unlink = 0, nr_truncate = 0;
830
831 /* don't do orphan cleanup if the fs is readonly. */
832 if (root->inode->i_sb->s_flags & MS_RDONLY)
833 return;
834
835 path = btrfs_alloc_path();
836 if (!path)
837 return;
838 path->reada = -1;
839
840 key.objectid = BTRFS_ORPHAN_OBJECTID;
841 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
842 key.offset = (u64)-1;
843
844 trans = btrfs_start_transaction(root, 1);
845 btrfs_set_trans_block_group(trans, root->inode);
846
847 while (1) {
848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
849 if (ret < 0) {
850 printk(KERN_ERR "Error searching slot for orphan: %d"
851 "\n", ret);
852 break;
853 }
854
855 /*
856 * if ret == 0 means we found what we were searching for, which
857 * is weird, but possible, so only screw with path if we didnt
858 * find the key and see if we have stuff that matches
859 */
860 if (ret > 0) {
861 if (path->slots[0] == 0)
862 break;
863 path->slots[0]--;
864 }
865
866 /* pull out the item */
867 leaf = path->nodes[0];
868 item = btrfs_item_nr(leaf, path->slots[0]);
869 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
870
871 /* make sure the item matches what we want */
872 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
873 break;
874 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
875 break;
876
877 /* release the path since we're done with it */
878 btrfs_release_path(root, path);
879
880 /*
881 * this is where we are basically btrfs_lookup, without the
882 * crossing root thing. we store the inode number in the
883 * offset of the orphan item.
884 */
885 inode = btrfs_iget_locked(root->inode->i_sb,
886 found_key.offset, root);
887 if (!inode)
888 break;
889
890 if (inode->i_state & I_NEW) {
891 BTRFS_I(inode)->root = root;
892
893 /* have to set the location manually */
894 BTRFS_I(inode)->location.objectid = inode->i_ino;
895 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
896 BTRFS_I(inode)->location.offset = 0;
897
898 btrfs_read_locked_inode(inode);
899 unlock_new_inode(inode);
900 }
901
902 /*
903 * add this inode to the orphan list so btrfs_orphan_del does
904 * the proper thing when we hit it
905 */
Yanbcc63ab2008-07-30 16:29:20 -0400906 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400907 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400908 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400909
910 /*
911 * if this is a bad inode, means we actually succeeded in
912 * removing the inode, but not the orphan record, which means
913 * we need to manually delete the orphan since iput will just
914 * do a destroy_inode
915 */
916 if (is_bad_inode(inode)) {
917 btrfs_orphan_del(trans, inode);
918 iput(inode);
919 continue;
920 }
921
922 /* if we have links, this was a truncate, lets do that */
923 if (inode->i_nlink) {
924 nr_truncate++;
925 btrfs_truncate(inode);
926 } else {
927 nr_unlink++;
928 }
929
930 /* this will do delete_inode and everything for us */
931 iput(inode);
932 }
933
934 if (nr_unlink)
935 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
936 if (nr_truncate)
937 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
938
939 btrfs_free_path(path);
940 btrfs_end_transaction(trans, root);
941}
942
Chris Mason39279cc2007-06-12 06:35:45 -0400943void btrfs_read_locked_inode(struct inode *inode)
944{
945 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400946 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400947 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400948 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400949 struct btrfs_root *root = BTRFS_I(inode)->root;
950 struct btrfs_key location;
951 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400952 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400953 int ret;
954
955 path = btrfs_alloc_path();
956 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400957 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500958
Chris Mason39279cc2007-06-12 06:35:45 -0400959 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400960 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400961 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400962
Chris Mason5f39d392007-10-15 16:14:19 -0400963 leaf = path->nodes[0];
964 inode_item = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_inode_item);
966
967 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
968 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
969 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
970 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400971 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400972
973 tspec = btrfs_inode_atime(inode_item);
974 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
975 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
976
977 tspec = btrfs_inode_mtime(inode_item);
978 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
979 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
980
981 tspec = btrfs_inode_ctime(inode_item);
982 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
983 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
984
985 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
986 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400987 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400988 rdev = btrfs_inode_rdev(leaf, inode_item);
989
Josef Bacikaec74772008-07-24 12:12:38 -0400990 BTRFS_I(inode)->index_cnt = (u64)-1;
991
Chris Mason5f39d392007-10-15 16:14:19 -0400992 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400993 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
994 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500995 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500996 if (!BTRFS_I(inode)->block_group) {
997 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400998 NULL, 0,
999 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001000 }
Chris Mason39279cc2007-06-12 06:35:45 -04001001 btrfs_free_path(path);
1002 inode_item = NULL;
1003
Chris Mason39279cc2007-06-12 06:35:45 -04001004 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001005 case S_IFREG:
1006 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001007 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001008 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001009 inode->i_fop = &btrfs_file_operations;
1010 inode->i_op = &btrfs_file_inode_operations;
1011 break;
1012 case S_IFDIR:
1013 inode->i_fop = &btrfs_dir_file_operations;
1014 if (root == root->fs_info->tree_root)
1015 inode->i_op = &btrfs_dir_ro_inode_operations;
1016 else
1017 inode->i_op = &btrfs_dir_inode_operations;
1018 break;
1019 case S_IFLNK:
1020 inode->i_op = &btrfs_symlink_inode_operations;
1021 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001022 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001023 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001024 default:
1025 init_special_inode(inode, inode->i_mode, rdev);
1026 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001027 }
1028 return;
1029
1030make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001031 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001032 make_bad_inode(inode);
1033}
1034
Chris Mason5f39d392007-10-15 16:14:19 -04001035static void fill_inode_item(struct extent_buffer *leaf,
1036 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001037 struct inode *inode)
1038{
Chris Mason5f39d392007-10-15 16:14:19 -04001039 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1040 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001041 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001042 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1043 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1044
1045 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1046 inode->i_atime.tv_sec);
1047 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1048 inode->i_atime.tv_nsec);
1049
1050 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1051 inode->i_mtime.tv_sec);
1052 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1053 inode->i_mtime.tv_nsec);
1054
1055 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1056 inode->i_ctime.tv_sec);
1057 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1058 inode->i_ctime.tv_nsec);
1059
1060 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1061 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1062 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001063 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001064 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001065 BTRFS_I(inode)->block_group->key.objectid);
1066}
1067
Chris Masonba1da2f2008-07-17 12:54:15 -04001068int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001069 struct btrfs_root *root,
1070 struct inode *inode)
1071{
1072 struct btrfs_inode_item *inode_item;
1073 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001074 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001075 int ret;
1076
1077 path = btrfs_alloc_path();
1078 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001079 ret = btrfs_lookup_inode(trans, root, path,
1080 &BTRFS_I(inode)->location, 1);
1081 if (ret) {
1082 if (ret > 0)
1083 ret = -ENOENT;
1084 goto failed;
1085 }
1086
Chris Mason5f39d392007-10-15 16:14:19 -04001087 leaf = path->nodes[0];
1088 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001089 struct btrfs_inode_item);
1090
Chris Mason5f39d392007-10-15 16:14:19 -04001091 fill_inode_item(leaf, inode_item, inode);
1092 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001093 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001094 ret = 0;
1095failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001096 btrfs_free_path(path);
1097 return ret;
1098}
1099
1100
1101static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1103 struct inode *dir,
1104 struct dentry *dentry)
1105{
1106 struct btrfs_path *path;
1107 const char *name = dentry->d_name.name;
1108 int name_len = dentry->d_name.len;
1109 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001110 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001111 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001112 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001113 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001114
1115 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001116 if (!path) {
1117 ret = -ENOMEM;
1118 goto err;
1119 }
1120
Chris Mason39279cc2007-06-12 06:35:45 -04001121 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1122 name, name_len, -1);
1123 if (IS_ERR(di)) {
1124 ret = PTR_ERR(di);
1125 goto err;
1126 }
1127 if (!di) {
1128 ret = -ENOENT;
1129 goto err;
1130 }
Chris Mason5f39d392007-10-15 16:14:19 -04001131 leaf = path->nodes[0];
1132 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001133 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001134 if (ret)
1135 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001136 btrfs_release_path(root, path);
1137
Josef Bacikaec74772008-07-24 12:12:38 -04001138 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1139 dentry->d_inode->i_ino,
1140 dentry->d_parent->d_inode->i_ino, &index);
1141 if (ret) {
1142 printk("failed to delete reference to %.*s, "
1143 "inode %lu parent %lu\n", name_len, name,
1144 dentry->d_inode->i_ino,
1145 dentry->d_parent->d_inode->i_ino);
1146 goto err;
1147 }
1148
Chris Mason39279cc2007-06-12 06:35:45 -04001149 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001150 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001151 if (IS_ERR(di)) {
1152 ret = PTR_ERR(di);
1153 goto err;
1154 }
1155 if (!di) {
1156 ret = -ENOENT;
1157 goto err;
1158 }
1159 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001160 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001161
1162 dentry->d_inode->i_ctime = dir->i_ctime;
1163err:
1164 btrfs_free_path(path);
1165 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001166 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001167 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001168 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001169#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1170 dentry->d_inode->i_nlink--;
1171#else
Chris Mason39279cc2007-06-12 06:35:45 -04001172 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001173#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001174 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001175 dir->i_sb->s_dirt = 1;
1176 }
1177 return ret;
1178}
1179
1180static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1181{
1182 struct btrfs_root *root;
1183 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001184 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001185 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001186 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001187
1188 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001189
1190 ret = btrfs_check_free_space(root, 1, 1);
1191 if (ret)
1192 goto fail;
1193
Chris Mason39279cc2007-06-12 06:35:45 -04001194 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001195
Chris Mason39279cc2007-06-12 06:35:45 -04001196 btrfs_set_trans_block_group(trans, dir);
1197 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001198
1199 if (inode->i_nlink == 0)
1200 ret = btrfs_orphan_add(trans, inode);
1201
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001202 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001203
Chris Mason89ce8a62008-06-25 16:01:31 -04001204 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001205fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001206 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001207 return ret;
1208}
1209
1210static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1211{
1212 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001213 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001214 int ret;
1215 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001216 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001217 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001218
Chris Mason925baed2008-06-25 16:01:30 -04001219 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001220 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001221 }
Yan134d4512007-10-25 15:49:25 -04001222
Chris Mason1832a6d2007-12-21 16:27:21 -05001223 ret = btrfs_check_free_space(root, 1, 1);
1224 if (ret)
1225 goto fail;
1226
Chris Mason39279cc2007-06-12 06:35:45 -04001227 trans = btrfs_start_transaction(root, 1);
1228 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001229
Josef Bacik7b128762008-07-24 12:17:14 -04001230 err = btrfs_orphan_add(trans, inode);
1231 if (err)
1232 goto fail_trans;
1233
Chris Mason39279cc2007-06-12 06:35:45 -04001234 /* now the directory is empty */
1235 err = btrfs_unlink_trans(trans, root, dir, dentry);
1236 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001237 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001238 }
Chris Mason39544012007-12-12 14:38:19 -05001239
Josef Bacik7b128762008-07-24 12:17:14 -04001240fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001241 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001242 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001243fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001244 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001245
Chris Mason39279cc2007-06-12 06:35:45 -04001246 if (ret && !err)
1247 err = ret;
1248 return err;
1249}
1250
Chris Mason39279cc2007-06-12 06:35:45 -04001251/*
Chris Mason39279cc2007-06-12 06:35:45 -04001252 * this can truncate away extent items, csum items and directory items.
1253 * It starts at a high offset and removes keys until it can't find
1254 * any higher than i_size.
1255 *
1256 * csum items that cross the new i_size are truncated to the new size
1257 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001258 *
1259 * min_type is the minimum key type to truncate down to. If set to 0, this
1260 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001261 */
1262static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001264 struct inode *inode,
1265 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001266{
1267 int ret;
1268 struct btrfs_path *path;
1269 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001270 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001271 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001272 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001273 struct btrfs_file_extent_item *fi;
1274 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001275 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001276 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001277 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001278 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001279 int found_extent;
1280 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001281 int pending_del_nr = 0;
1282 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001283 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001284 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001285
Chris Mason3b951512008-04-17 11:29:12 -04001286 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001287 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001288 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001289 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001290
Chris Mason39279cc2007-06-12 06:35:45 -04001291 /* FIXME, add redo link to tree so we don't leak on crash */
1292 key.objectid = inode->i_ino;
1293 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001294 key.type = (u8)-1;
1295
Chris Mason85e21ba2008-01-29 15:11:36 -05001296 btrfs_init_path(path);
1297search_again:
1298 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1299 if (ret < 0) {
1300 goto error;
1301 }
1302 if (ret > 0) {
1303 BUG_ON(path->slots[0] == 0);
1304 path->slots[0]--;
1305 }
1306
Chris Mason39279cc2007-06-12 06:35:45 -04001307 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001308 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1311 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001312
Chris Mason5f39d392007-10-15 16:14:19 -04001313 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001314 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001315
Chris Mason85e21ba2008-01-29 15:11:36 -05001316 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001317 break;
1318
Chris Mason5f39d392007-10-15 16:14:19 -04001319 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001320 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001321 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001322 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001323 extent_type = btrfs_file_extent_type(leaf, fi);
1324 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001325 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001326 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001327 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1328 struct btrfs_item *item = btrfs_item_nr(leaf,
1329 path->slots[0]);
1330 item_end += btrfs_file_extent_inline_len(leaf,
1331 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001332 }
Yan008630c2007-11-07 13:31:09 -05001333 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001334 }
1335 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1336 ret = btrfs_csum_truncate(trans, root, path,
1337 inode->i_size);
1338 BUG_ON(ret);
1339 }
Yan008630c2007-11-07 13:31:09 -05001340 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001341 if (found_type == BTRFS_DIR_ITEM_KEY) {
1342 found_type = BTRFS_INODE_ITEM_KEY;
1343 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1344 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001345 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1346 found_type = BTRFS_XATTR_ITEM_KEY;
1347 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1348 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001349 } else if (found_type) {
1350 found_type--;
1351 } else {
1352 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001353 }
Yana61721d2007-09-17 11:08:38 -04001354 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001355 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001356 }
Chris Mason5f39d392007-10-15 16:14:19 -04001357 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001358 del_item = 1;
1359 else
1360 del_item = 0;
1361 found_extent = 0;
1362
1363 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001364 if (found_type != BTRFS_EXTENT_DATA_KEY)
1365 goto delete;
1366
1367 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001368 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001369 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001370 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001371 u64 orig_num_bytes =
1372 btrfs_file_extent_num_bytes(leaf, fi);
1373 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001374 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001375 extent_num_bytes = extent_num_bytes &
1376 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001377 btrfs_set_file_extent_num_bytes(leaf, fi,
1378 extent_num_bytes);
1379 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001380 extent_num_bytes);
1381 if (extent_start != 0)
1382 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001383 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001384 } else {
Chris Masondb945352007-10-15 16:15:53 -04001385 extent_num_bytes =
1386 btrfs_file_extent_disk_num_bytes(leaf,
1387 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001388 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001389 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001390 if (extent_start != 0) {
1391 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001392 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001393 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001394 root_gen = btrfs_header_generation(leaf);
1395 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001396 }
Chris Mason90692182008-02-08 13:49:28 -05001397 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1398 if (!del_item) {
1399 u32 newsize = inode->i_size - found_key.offset;
1400 dec_i_blocks(inode, item_end + 1 -
1401 found_key.offset - newsize);
1402 newsize =
1403 btrfs_file_extent_calc_inline_size(newsize);
1404 ret = btrfs_truncate_item(trans, root, path,
1405 newsize, 1);
1406 BUG_ON(ret);
1407 } else {
1408 dec_i_blocks(inode, item_end + 1 -
1409 found_key.offset);
1410 }
Chris Mason39279cc2007-06-12 06:35:45 -04001411 }
Chris Mason179e29e2007-11-01 11:28:41 -04001412delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001413 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001414 if (!pending_del_nr) {
1415 /* no pending yet, add ourselves */
1416 pending_del_slot = path->slots[0];
1417 pending_del_nr = 1;
1418 } else if (pending_del_nr &&
1419 path->slots[0] + 1 == pending_del_slot) {
1420 /* hop on the pending chunk */
1421 pending_del_nr++;
1422 pending_del_slot = path->slots[0];
1423 } else {
1424 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1425 }
Chris Mason39279cc2007-06-12 06:35:45 -04001426 } else {
1427 break;
1428 }
Chris Mason39279cc2007-06-12 06:35:45 -04001429 if (found_extent) {
1430 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001431 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001432 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001433 root_gen, inode->i_ino,
1434 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001435 BUG_ON(ret);
1436 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001437next:
1438 if (path->slots[0] == 0) {
1439 if (pending_del_nr)
1440 goto del_pending;
1441 btrfs_release_path(root, path);
1442 goto search_again;
1443 }
1444
1445 path->slots[0]--;
1446 if (pending_del_nr &&
1447 path->slots[0] + 1 != pending_del_slot) {
1448 struct btrfs_key debug;
1449del_pending:
1450 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1451 pending_del_slot);
1452 ret = btrfs_del_items(trans, root, path,
1453 pending_del_slot,
1454 pending_del_nr);
1455 BUG_ON(ret);
1456 pending_del_nr = 0;
1457 btrfs_release_path(root, path);
1458 goto search_again;
1459 }
Chris Mason39279cc2007-06-12 06:35:45 -04001460 }
1461 ret = 0;
1462error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001463 if (pending_del_nr) {
1464 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1465 pending_del_nr);
1466 }
Chris Mason39279cc2007-06-12 06:35:45 -04001467 btrfs_free_path(path);
1468 inode->i_sb->s_dirt = 1;
1469 return ret;
1470}
1471
Chris Masona52d9a82007-08-27 16:49:44 -04001472/*
1473 * taken from block_truncate_page, but does cow as it zeros out
1474 * any bytes left in the last page in the file.
1475 */
1476static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1477{
1478 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001480 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1481 struct btrfs_ordered_extent *ordered;
1482 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001483 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001484 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1485 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1486 struct page *page;
1487 int ret = 0;
1488 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001489 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001490
1491 if ((offset & (blocksize - 1)) == 0)
1492 goto out;
1493
1494 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001495again:
Chris Masona52d9a82007-08-27 16:49:44 -04001496 page = grab_cache_page(mapping, index);
1497 if (!page)
1498 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001499
1500 page_start = page_offset(page);
1501 page_end = page_start + PAGE_CACHE_SIZE - 1;
1502
Chris Masona52d9a82007-08-27 16:49:44 -04001503 if (!PageUptodate(page)) {
1504 ret = btrfs_readpage(NULL, page);
1505 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001506 if (page->mapping != mapping) {
1507 unlock_page(page);
1508 page_cache_release(page);
1509 goto again;
1510 }
Chris Masona52d9a82007-08-27 16:49:44 -04001511 if (!PageUptodate(page)) {
1512 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001513 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001514 }
1515 }
Chris Mason211c17f2008-05-15 09:13:45 -04001516 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001517
1518 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1519 set_page_extent_mapped(page);
1520
1521 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1522 if (ordered) {
1523 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1524 unlock_page(page);
1525 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001526 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001527 btrfs_put_ordered_extent(ordered);
1528 goto again;
1529 }
1530
1531 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1532 page_end, GFP_NOFS);
1533 ret = 0;
1534 if (offset != PAGE_CACHE_SIZE) {
1535 kaddr = kmap(page);
1536 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1537 flush_dcache_page(page);
1538 kunmap(page);
1539 }
Chris Mason247e7432008-07-17 12:53:51 -04001540 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001541 set_page_dirty(page);
1542 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001543
Chris Mason89642222008-07-24 09:41:53 -04001544out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001545 unlock_page(page);
1546 page_cache_release(page);
1547out:
1548 return ret;
1549}
1550
1551static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1552{
1553 struct inode *inode = dentry->d_inode;
1554 int err;
1555
1556 err = inode_change_ok(inode, attr);
1557 if (err)
1558 return err;
1559
1560 if (S_ISREG(inode->i_mode) &&
1561 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1562 struct btrfs_trans_handle *trans;
1563 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001564 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001565
Chris Mason5f39d392007-10-15 16:14:19 -04001566 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001567 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001568 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001569 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001570 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001571
Chris Mason1b0f7c22008-01-30 14:33:02 -05001572 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001573 goto out;
1574
Chris Mason1832a6d2007-12-21 16:27:21 -05001575 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001576 if (err)
1577 goto fail;
1578
Chris Mason39279cc2007-06-12 06:35:45 -04001579 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1580
Chris Mason5f564062008-01-22 16:47:59 -05001581 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001582 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1583 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001584
Chris Mason39279cc2007-06-12 06:35:45 -04001585 trans = btrfs_start_transaction(root, 1);
1586 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001587 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001588 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001589 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001590 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001591
Chris Mason179e29e2007-11-01 11:28:41 -04001592 if (alloc_hint != EXTENT_MAP_INLINE) {
1593 err = btrfs_insert_file_extent(trans, root,
1594 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001595 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001596 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001597 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001598 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001599 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001600 }
Chris Masonee6e6502008-07-17 12:54:40 -04001601 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001602 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001603 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001604 if (err)
1605 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001606 }
1607out:
1608 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001609
1610 if (!err && ((attr->ia_valid & ATTR_MODE)))
1611 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001612fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001613 return err;
1614}
Chris Mason61295eb2008-01-14 16:24:38 -05001615
Chris Mason39279cc2007-06-12 06:35:45 -04001616void btrfs_delete_inode(struct inode *inode)
1617{
1618 struct btrfs_trans_handle *trans;
1619 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001620 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001621 int ret;
1622
1623 truncate_inode_pages(&inode->i_data, 0);
1624 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001625 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001626 goto no_delete;
1627 }
Chris Mason4a096752008-07-21 10:29:44 -04001628 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001629
Chris Masondbe674a2008-07-17 12:54:05 -04001630 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001631 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001632
Chris Mason39279cc2007-06-12 06:35:45 -04001633 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001634 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001635 if (ret) {
1636 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001637 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001638 }
1639
1640 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001641
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001642 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001643 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001644
Chris Mason39279cc2007-06-12 06:35:45 -04001645 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001646 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001647 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001648
1649no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001650 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001651 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001652 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001653no_delete:
1654 clear_inode(inode);
1655}
1656
1657/*
1658 * this returns the key found in the dir entry in the location pointer.
1659 * If no dir entries were found, location->objectid is 0.
1660 */
1661static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1662 struct btrfs_key *location)
1663{
1664 const char *name = dentry->d_name.name;
1665 int namelen = dentry->d_name.len;
1666 struct btrfs_dir_item *di;
1667 struct btrfs_path *path;
1668 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001669 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001670
Chris Mason39544012007-12-12 14:38:19 -05001671 if (namelen == 1 && strcmp(name, ".") == 0) {
1672 location->objectid = dir->i_ino;
1673 location->type = BTRFS_INODE_ITEM_KEY;
1674 location->offset = 0;
1675 return 0;
1676 }
Chris Mason39279cc2007-06-12 06:35:45 -04001677 path = btrfs_alloc_path();
1678 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001679
Chris Mason7a720532007-12-13 09:06:59 -05001680 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001681 struct btrfs_key key;
1682 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001683 int slot;
1684
1685 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001686 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001687 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001688 if (ret < 0 || path->slots[0] == 0)
1689 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001690 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1691 BUG_ON(ret == 0);
1692 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001693 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001694 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001695
1696 btrfs_item_key_to_cpu(leaf, &key, slot);
1697 if (key.objectid != dir->i_ino ||
1698 key.type != BTRFS_INODE_REF_KEY) {
1699 goto out_err;
1700 }
1701 location->objectid = key.offset;
1702 location->type = BTRFS_INODE_ITEM_KEY;
1703 location->offset = 0;
1704 goto out;
1705 }
1706
Chris Mason39279cc2007-06-12 06:35:45 -04001707 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1708 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001709 if (IS_ERR(di))
1710 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001711 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001712 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001713 }
Chris Mason5f39d392007-10-15 16:14:19 -04001714 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001715out:
Chris Mason39279cc2007-06-12 06:35:45 -04001716 btrfs_free_path(path);
1717 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001718out_err:
1719 location->objectid = 0;
1720 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001721}
1722
1723/*
1724 * when we hit a tree root in a directory, the btrfs part of the inode
1725 * needs to be changed to reflect the root directory of the tree root. This
1726 * is kind of like crossing a mount point.
1727 */
1728static int fixup_tree_root_location(struct btrfs_root *root,
1729 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001730 struct btrfs_root **sub_root,
1731 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001732{
Chris Mason39279cc2007-06-12 06:35:45 -04001733 struct btrfs_root_item *ri;
1734
1735 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1736 return 0;
1737 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1738 return 0;
1739
Josef Bacik58176a92007-08-29 15:47:34 -04001740 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1741 dentry->d_name.name,
1742 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001743 if (IS_ERR(*sub_root))
1744 return PTR_ERR(*sub_root);
1745
1746 ri = &(*sub_root)->root_item;
1747 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001748 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1749 location->offset = 0;
1750
Chris Mason39279cc2007-06-12 06:35:45 -04001751 return 0;
1752}
1753
1754static int btrfs_init_locked_inode(struct inode *inode, void *p)
1755{
1756 struct btrfs_iget_args *args = p;
1757 inode->i_ino = args->ino;
1758 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001759 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001760 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001761 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001762 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1763 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001764 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001765 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1766 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001767 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001768 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001769 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001770 return 0;
1771}
1772
1773static int btrfs_find_actor(struct inode *inode, void *opaque)
1774{
1775 struct btrfs_iget_args *args = opaque;
1776 return (args->ino == inode->i_ino &&
1777 args->root == BTRFS_I(inode)->root);
1778}
1779
Chris Masondc17ff82008-01-08 15:46:30 -05001780struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1781 u64 root_objectid)
1782{
1783 struct btrfs_iget_args args;
1784 args.ino = objectid;
1785 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1786
1787 if (!args.root)
1788 return NULL;
1789
1790 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1791}
1792
Chris Mason39279cc2007-06-12 06:35:45 -04001793struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1794 struct btrfs_root *root)
1795{
1796 struct inode *inode;
1797 struct btrfs_iget_args args;
1798 args.ino = objectid;
1799 args.root = root;
1800
1801 inode = iget5_locked(s, objectid, btrfs_find_actor,
1802 btrfs_init_locked_inode,
1803 (void *)&args);
1804 return inode;
1805}
1806
1807static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1808 struct nameidata *nd)
1809{
1810 struct inode * inode;
1811 struct btrfs_inode *bi = BTRFS_I(dir);
1812 struct btrfs_root *root = bi->root;
1813 struct btrfs_root *sub_root = root;
1814 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001815 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001816
1817 if (dentry->d_name.len > BTRFS_NAME_LEN)
1818 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001819
Chris Mason39279cc2007-06-12 06:35:45 -04001820 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001821
Chris Mason39279cc2007-06-12 06:35:45 -04001822 if (ret < 0)
1823 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001824
Chris Mason39279cc2007-06-12 06:35:45 -04001825 inode = NULL;
1826 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001827 ret = fixup_tree_root_location(root, &location, &sub_root,
1828 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001829 if (ret < 0)
1830 return ERR_PTR(ret);
1831 if (ret > 0)
1832 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001833
Chris Mason39279cc2007-06-12 06:35:45 -04001834 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1835 sub_root);
1836 if (!inode)
1837 return ERR_PTR(-EACCES);
1838 if (inode->i_state & I_NEW) {
1839 /* the inode and parent dir are two different roots */
1840 if (sub_root != root) {
1841 igrab(inode);
1842 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001843 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001844 }
1845 BTRFS_I(inode)->root = sub_root;
1846 memcpy(&BTRFS_I(inode)->location, &location,
1847 sizeof(location));
1848 btrfs_read_locked_inode(inode);
1849 unlock_new_inode(inode);
1850 }
1851 }
Josef Bacik7b128762008-07-24 12:17:14 -04001852
1853 if (unlikely(do_orphan))
1854 btrfs_orphan_cleanup(sub_root);
1855
Chris Mason39279cc2007-06-12 06:35:45 -04001856 return d_splice_alias(inode, dentry);
1857}
1858
Chris Mason39279cc2007-06-12 06:35:45 -04001859static unsigned char btrfs_filetype_table[] = {
1860 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1861};
1862
1863static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1864{
Chris Mason6da6aba2007-12-18 16:15:09 -05001865 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001866 struct btrfs_root *root = BTRFS_I(inode)->root;
1867 struct btrfs_item *item;
1868 struct btrfs_dir_item *di;
1869 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001870 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001871 struct btrfs_path *path;
1872 int ret;
1873 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001874 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001875 int slot;
1876 int advance;
1877 unsigned char d_type;
1878 int over = 0;
1879 u32 di_cur;
1880 u32 di_total;
1881 u32 di_len;
1882 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001883 char tmp_name[32];
1884 char *name_ptr;
1885 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001886
1887 /* FIXME, use a real flag for deciding about the key type */
1888 if (root->fs_info->tree_root == root)
1889 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001890
Chris Mason39544012007-12-12 14:38:19 -05001891 /* special case for "." */
1892 if (filp->f_pos == 0) {
1893 over = filldir(dirent, ".", 1,
1894 1, inode->i_ino,
1895 DT_DIR);
1896 if (over)
1897 return 0;
1898 filp->f_pos = 1;
1899 }
1900
Chris Mason39279cc2007-06-12 06:35:45 -04001901 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001902 path = btrfs_alloc_path();
1903 path->reada = 2;
1904
1905 /* special case for .., just use the back ref */
1906 if (filp->f_pos == 1) {
1907 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001908 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001909 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001910 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001911 btrfs_release_path(root, path);
1912 goto read_dir_items;
1913 }
Yan445dceb2008-07-24 12:19:32 -04001914 BUG_ON(ret == 0);
1915 leaf = path->nodes[0];
1916 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001917 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1918 btrfs_release_path(root, path);
1919 if (found_key.objectid != key.objectid ||
1920 found_key.type != BTRFS_INODE_REF_KEY)
1921 goto read_dir_items;
1922 over = filldir(dirent, "..", 2,
1923 2, found_key.offset, DT_DIR);
1924 if (over)
1925 goto nopos;
1926 filp->f_pos = 2;
1927 }
1928
1929read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001930 btrfs_set_key_type(&key, key_type);
1931 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001932
Chris Mason39279cc2007-06-12 06:35:45 -04001933 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1934 if (ret < 0)
1935 goto err;
1936 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001937 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001938 leaf = path->nodes[0];
1939 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001940 slot = path->slots[0];
1941 if (advance || slot >= nritems) {
1942 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001943 ret = btrfs_next_leaf(root, path);
1944 if (ret)
1945 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001946 leaf = path->nodes[0];
1947 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001948 slot = path->slots[0];
1949 } else {
1950 slot++;
1951 path->slots[0]++;
1952 }
1953 }
1954 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001955 item = btrfs_item_nr(leaf, slot);
1956 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1957
1958 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001959 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001960 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001961 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001962 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001963 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001964
1965 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001966 advance = 1;
1967 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1968 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001969 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001970 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001971 struct btrfs_key location;
1972
1973 name_len = btrfs_dir_name_len(leaf, di);
1974 if (name_len < 32) {
1975 name_ptr = tmp_name;
1976 } else {
1977 name_ptr = kmalloc(name_len, GFP_NOFS);
1978 BUG_ON(!name_ptr);
1979 }
1980 read_extent_buffer(leaf, name_ptr,
1981 (unsigned long)(di + 1), name_len);
1982
1983 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1984 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001985 over = filldir(dirent, name_ptr, name_len,
1986 found_key.offset,
1987 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001988 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001989
1990 if (name_ptr != tmp_name)
1991 kfree(name_ptr);
1992
Chris Mason39279cc2007-06-12 06:35:45 -04001993 if (over)
1994 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001995 di_len = btrfs_dir_name_len(leaf, di) +
1996 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001997 di_cur += di_len;
1998 di = (struct btrfs_dir_item *)((char *)di + di_len);
1999 }
2000 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002001 if (key_type == BTRFS_DIR_INDEX_KEY)
2002 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2003 else
2004 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002005nopos:
2006 ret = 0;
2007err:
Chris Mason39279cc2007-06-12 06:35:45 -04002008 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002009 return ret;
2010}
2011
2012int btrfs_write_inode(struct inode *inode, int wait)
2013{
2014 struct btrfs_root *root = BTRFS_I(inode)->root;
2015 struct btrfs_trans_handle *trans;
2016 int ret = 0;
2017
2018 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002019 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002020 btrfs_set_trans_block_group(trans, inode);
2021 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002022 }
2023 return ret;
2024}
2025
2026/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002027 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002028 * inode changes. But, it is most likely to find the inode in cache.
2029 * FIXME, needs more benchmarking...there are no reasons other than performance
2030 * to keep or drop this code.
2031 */
2032void btrfs_dirty_inode(struct inode *inode)
2033{
2034 struct btrfs_root *root = BTRFS_I(inode)->root;
2035 struct btrfs_trans_handle *trans;
2036
Chris Masonf9295742008-07-17 12:54:14 -04002037 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002038 btrfs_set_trans_block_group(trans, inode);
2039 btrfs_update_inode(trans, root, inode);
2040 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002041}
2042
Josef Bacikaec74772008-07-24 12:12:38 -04002043static int btrfs_set_inode_index_count(struct inode *inode)
2044{
2045 struct btrfs_root *root = BTRFS_I(inode)->root;
2046 struct btrfs_key key, found_key;
2047 struct btrfs_path *path;
2048 struct extent_buffer *leaf;
2049 int ret;
2050
2051 key.objectid = inode->i_ino;
2052 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2053 key.offset = (u64)-1;
2054
2055 path = btrfs_alloc_path();
2056 if (!path)
2057 return -ENOMEM;
2058
2059 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2060 if (ret < 0)
2061 goto out;
2062 /* FIXME: we should be able to handle this */
2063 if (ret == 0)
2064 goto out;
2065 ret = 0;
2066
2067 /*
2068 * MAGIC NUMBER EXPLANATION:
2069 * since we search a directory based on f_pos we have to start at 2
2070 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2071 * else has to start at 2
2072 */
2073 if (path->slots[0] == 0) {
2074 BTRFS_I(inode)->index_cnt = 2;
2075 goto out;
2076 }
2077
2078 path->slots[0]--;
2079
2080 leaf = path->nodes[0];
2081 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2082
2083 if (found_key.objectid != inode->i_ino ||
2084 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2085 BTRFS_I(inode)->index_cnt = 2;
2086 goto out;
2087 }
2088
2089 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2090out:
2091 btrfs_free_path(path);
2092 return ret;
2093}
2094
2095static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2096{
2097 int ret = 0;
2098
2099 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2100 ret = btrfs_set_inode_index_count(dir);
2101 if (ret)
2102 return ret;
2103 }
2104
2105 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2106 BTRFS_I(dir)->index_cnt++;
2107
2108 return ret;
2109}
2110
Chris Mason39279cc2007-06-12 06:35:45 -04002111static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2112 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002113 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002114 const char *name, int name_len,
2115 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002116 u64 objectid,
2117 struct btrfs_block_group_cache *group,
2118 int mode)
2119{
2120 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002121 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002122 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002123 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002124 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002125 struct btrfs_inode_ref *ref;
2126 struct btrfs_key key[2];
2127 u32 sizes[2];
2128 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002129 int ret;
2130 int owner;
2131
Chris Mason5f39d392007-10-15 16:14:19 -04002132 path = btrfs_alloc_path();
2133 BUG_ON(!path);
2134
Chris Mason39279cc2007-06-12 06:35:45 -04002135 inode = new_inode(root->fs_info->sb);
2136 if (!inode)
2137 return ERR_PTR(-ENOMEM);
2138
Josef Bacikaec74772008-07-24 12:12:38 -04002139 if (dir) {
2140 ret = btrfs_set_inode_index(dir, inode);
2141 if (ret)
2142 return ERR_PTR(ret);
2143 } else {
2144 BTRFS_I(inode)->index = 0;
2145 }
2146 /*
2147 * index_cnt is ignored for everything but a dir,
2148 * btrfs_get_inode_index_count has an explanation for the magic
2149 * number
2150 */
2151 BTRFS_I(inode)->index_cnt = 2;
2152
Chris Masond1310b22008-01-24 16:13:08 -05002153 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2154 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002155 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002156 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2157 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002158 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002159 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002160 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002161 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002162 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002163 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002164
Chris Mason39279cc2007-06-12 06:35:45 -04002165 if (mode & S_IFDIR)
2166 owner = 0;
2167 else
2168 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002169 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002170 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002171 if (!new_inode_group) {
2172 printk("find_block group failed\n");
2173 new_inode_group = group;
2174 }
2175 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002176 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002177
2178 key[0].objectid = objectid;
2179 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2180 key[0].offset = 0;
2181
2182 key[1].objectid = objectid;
2183 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2184 key[1].offset = ref_objectid;
2185
2186 sizes[0] = sizeof(struct btrfs_inode_item);
2187 sizes[1] = name_len + sizeof(*ref);
2188
2189 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2190 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002191 goto fail;
2192
Chris Mason9c583092008-01-29 15:15:18 -05002193 if (objectid > root->highest_inode)
2194 root->highest_inode = objectid;
2195
Chris Mason39279cc2007-06-12 06:35:45 -04002196 inode->i_uid = current->fsuid;
2197 inode->i_gid = current->fsgid;
2198 inode->i_mode = mode;
2199 inode->i_ino = objectid;
2200 inode->i_blocks = 0;
2201 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002202 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2203 struct btrfs_inode_item);
2204 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002205
2206 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2207 struct btrfs_inode_ref);
2208 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002209 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002210 ptr = (unsigned long)(ref + 1);
2211 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2212
Chris Mason5f39d392007-10-15 16:14:19 -04002213 btrfs_mark_buffer_dirty(path->nodes[0]);
2214 btrfs_free_path(path);
2215
Chris Mason39279cc2007-06-12 06:35:45 -04002216 location = &BTRFS_I(inode)->location;
2217 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002218 location->offset = 0;
2219 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2220
Chris Mason39279cc2007-06-12 06:35:45 -04002221 insert_inode_hash(inode);
2222 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002223fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002224 if (dir)
2225 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002226 btrfs_free_path(path);
2227 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002228}
2229
2230static inline u8 btrfs_inode_type(struct inode *inode)
2231{
2232 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2233}
2234
2235static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002236 struct dentry *dentry, struct inode *inode,
2237 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002238{
2239 int ret;
2240 struct btrfs_key key;
2241 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002242 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002243
Chris Mason39279cc2007-06-12 06:35:45 -04002244 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002245 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2246 key.offset = 0;
2247
2248 ret = btrfs_insert_dir_item(trans, root,
2249 dentry->d_name.name, dentry->d_name.len,
2250 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002251 &key, btrfs_inode_type(inode),
2252 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002253 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002254 if (add_backref) {
2255 ret = btrfs_insert_inode_ref(trans, root,
2256 dentry->d_name.name,
2257 dentry->d_name.len,
2258 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002259 parent_inode->i_ino,
2260 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002261 }
Chris Masondbe674a2008-07-17 12:54:05 -04002262 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2263 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002264 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002265 ret = btrfs_update_inode(trans, root,
2266 dentry->d_parent->d_inode);
2267 }
2268 return ret;
2269}
2270
2271static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002272 struct dentry *dentry, struct inode *inode,
2273 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002274{
Chris Mason9c583092008-01-29 15:15:18 -05002275 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002276 if (!err) {
2277 d_instantiate(dentry, inode);
2278 return 0;
2279 }
2280 if (err > 0)
2281 err = -EEXIST;
2282 return err;
2283}
2284
Josef Bacik618e21d2007-07-11 10:18:17 -04002285static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2286 int mode, dev_t rdev)
2287{
2288 struct btrfs_trans_handle *trans;
2289 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002290 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002291 int err;
2292 int drop_inode = 0;
2293 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002294 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002295
2296 if (!new_valid_dev(rdev))
2297 return -EINVAL;
2298
Chris Mason1832a6d2007-12-21 16:27:21 -05002299 err = btrfs_check_free_space(root, 1, 0);
2300 if (err)
2301 goto fail;
2302
Josef Bacik618e21d2007-07-11 10:18:17 -04002303 trans = btrfs_start_transaction(root, 1);
2304 btrfs_set_trans_block_group(trans, dir);
2305
2306 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2307 if (err) {
2308 err = -ENOSPC;
2309 goto out_unlock;
2310 }
2311
Josef Bacikaec74772008-07-24 12:12:38 -04002312 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002313 dentry->d_name.len,
2314 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002315 BTRFS_I(dir)->block_group, mode);
2316 err = PTR_ERR(inode);
2317 if (IS_ERR(inode))
2318 goto out_unlock;
2319
Josef Bacik33268ea2008-07-24 12:16:36 -04002320 err = btrfs_init_acl(inode, dir);
2321 if (err) {
2322 drop_inode = 1;
2323 goto out_unlock;
2324 }
2325
Josef Bacik618e21d2007-07-11 10:18:17 -04002326 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002327 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002328 if (err)
2329 drop_inode = 1;
2330 else {
2331 inode->i_op = &btrfs_special_inode_operations;
2332 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002333 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002334 }
2335 dir->i_sb->s_dirt = 1;
2336 btrfs_update_inode_block_group(trans, inode);
2337 btrfs_update_inode_block_group(trans, dir);
2338out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002339 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002340 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002341fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002342 if (drop_inode) {
2343 inode_dec_link_count(inode);
2344 iput(inode);
2345 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002346 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002347 return err;
2348}
2349
Chris Mason39279cc2007-06-12 06:35:45 -04002350static int btrfs_create(struct inode *dir, struct dentry *dentry,
2351 int mode, struct nameidata *nd)
2352{
2353 struct btrfs_trans_handle *trans;
2354 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002355 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002356 int err;
2357 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002358 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002359 u64 objectid;
2360
Chris Mason1832a6d2007-12-21 16:27:21 -05002361 err = btrfs_check_free_space(root, 1, 0);
2362 if (err)
2363 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002364 trans = btrfs_start_transaction(root, 1);
2365 btrfs_set_trans_block_group(trans, dir);
2366
2367 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2368 if (err) {
2369 err = -ENOSPC;
2370 goto out_unlock;
2371 }
2372
Josef Bacikaec74772008-07-24 12:12:38 -04002373 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002374 dentry->d_name.len,
2375 dentry->d_parent->d_inode->i_ino,
2376 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002377 err = PTR_ERR(inode);
2378 if (IS_ERR(inode))
2379 goto out_unlock;
2380
Josef Bacik33268ea2008-07-24 12:16:36 -04002381 err = btrfs_init_acl(inode, dir);
2382 if (err) {
2383 drop_inode = 1;
2384 goto out_unlock;
2385 }
2386
Chris Mason39279cc2007-06-12 06:35:45 -04002387 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002388 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002389 if (err)
2390 drop_inode = 1;
2391 else {
2392 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002393 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002394 inode->i_fop = &btrfs_file_operations;
2395 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002396 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2397 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002398 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002399 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2400 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002401 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002402 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002403 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002404 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002405 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002406 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002407 }
2408 dir->i_sb->s_dirt = 1;
2409 btrfs_update_inode_block_group(trans, inode);
2410 btrfs_update_inode_block_group(trans, dir);
2411out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002412 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002413 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002414fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002415 if (drop_inode) {
2416 inode_dec_link_count(inode);
2417 iput(inode);
2418 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002419 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002420 return err;
2421}
2422
2423static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2424 struct dentry *dentry)
2425{
2426 struct btrfs_trans_handle *trans;
2427 struct btrfs_root *root = BTRFS_I(dir)->root;
2428 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002429 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002430 int err;
2431 int drop_inode = 0;
2432
2433 if (inode->i_nlink == 0)
2434 return -ENOENT;
2435
Chris Mason6da6aba2007-12-18 16:15:09 -05002436#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2437 inode->i_nlink++;
2438#else
Chris Mason39279cc2007-06-12 06:35:45 -04002439 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002440#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002441 err = btrfs_check_free_space(root, 1, 0);
2442 if (err)
2443 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002444 err = btrfs_set_inode_index(dir, inode);
2445 if (err)
2446 goto fail;
2447
Chris Mason39279cc2007-06-12 06:35:45 -04002448 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002449
Chris Mason39279cc2007-06-12 06:35:45 -04002450 btrfs_set_trans_block_group(trans, dir);
2451 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002452
Chris Mason9c583092008-01-29 15:15:18 -05002453 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002454
Chris Mason39279cc2007-06-12 06:35:45 -04002455 if (err)
2456 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002457
Chris Mason39279cc2007-06-12 06:35:45 -04002458 dir->i_sb->s_dirt = 1;
2459 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002460 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002461
Chris Mason54aa1f42007-06-22 14:16:25 -04002462 if (err)
2463 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002464
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002465 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002466 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002467fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002468 if (drop_inode) {
2469 inode_dec_link_count(inode);
2470 iput(inode);
2471 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002472 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002473 return err;
2474}
2475
Chris Mason39279cc2007-06-12 06:35:45 -04002476static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2477{
Chris Masonb9d86662008-05-02 16:13:49 -04002478 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002479 struct btrfs_trans_handle *trans;
2480 struct btrfs_root *root = BTRFS_I(dir)->root;
2481 int err = 0;
2482 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002483 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002484 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002485
Chris Mason1832a6d2007-12-21 16:27:21 -05002486 err = btrfs_check_free_space(root, 1, 0);
2487 if (err)
2488 goto out_unlock;
2489
Chris Mason39279cc2007-06-12 06:35:45 -04002490 trans = btrfs_start_transaction(root, 1);
2491 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002492
Chris Mason39279cc2007-06-12 06:35:45 -04002493 if (IS_ERR(trans)) {
2494 err = PTR_ERR(trans);
2495 goto out_unlock;
2496 }
2497
2498 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2499 if (err) {
2500 err = -ENOSPC;
2501 goto out_unlock;
2502 }
2503
Josef Bacikaec74772008-07-24 12:12:38 -04002504 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002505 dentry->d_name.len,
2506 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002507 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2508 if (IS_ERR(inode)) {
2509 err = PTR_ERR(inode);
2510 goto out_fail;
2511 }
Chris Mason5f39d392007-10-15 16:14:19 -04002512
Chris Mason39279cc2007-06-12 06:35:45 -04002513 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002514
2515 err = btrfs_init_acl(inode, dir);
2516 if (err)
2517 goto out_fail;
2518
Chris Mason39279cc2007-06-12 06:35:45 -04002519 inode->i_op = &btrfs_dir_inode_operations;
2520 inode->i_fop = &btrfs_dir_file_operations;
2521 btrfs_set_trans_block_group(trans, inode);
2522
Chris Masondbe674a2008-07-17 12:54:05 -04002523 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002524 err = btrfs_update_inode(trans, root, inode);
2525 if (err)
2526 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002527
Chris Mason9c583092008-01-29 15:15:18 -05002528 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002529 if (err)
2530 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002531
Chris Mason39279cc2007-06-12 06:35:45 -04002532 d_instantiate(dentry, inode);
2533 drop_on_err = 0;
2534 dir->i_sb->s_dirt = 1;
2535 btrfs_update_inode_block_group(trans, inode);
2536 btrfs_update_inode_block_group(trans, dir);
2537
2538out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002539 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002540 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002541
Chris Mason39279cc2007-06-12 06:35:45 -04002542out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002543 if (drop_on_err)
2544 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002545 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002546 return err;
2547}
2548
Chris Mason3b951512008-04-17 11:29:12 -04002549static int merge_extent_mapping(struct extent_map_tree *em_tree,
2550 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002551 struct extent_map *em,
2552 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002553{
2554 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002555
Chris Masone6dcd2d2008-07-17 12:53:50 -04002556 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2557 start_diff = map_start - em->start;
2558 em->start = map_start;
2559 em->len = map_len;
2560 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2561 em->block_start += start_diff;
2562 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002563}
2564
Chris Masona52d9a82007-08-27 16:49:44 -04002565struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002566 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002567 int create)
2568{
2569 int ret;
2570 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002571 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002572 u64 extent_start = 0;
2573 u64 extent_end = 0;
2574 u64 objectid = inode->i_ino;
2575 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002576 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002577 struct btrfs_root *root = BTRFS_I(inode)->root;
2578 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002579 struct extent_buffer *leaf;
2580 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002581 struct extent_map *em = NULL;
2582 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002584 struct btrfs_trans_handle *trans = NULL;
2585
Chris Masona52d9a82007-08-27 16:49:44 -04002586again:
Chris Masond1310b22008-01-24 16:13:08 -05002587 spin_lock(&em_tree->lock);
2588 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002589 if (em)
2590 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002591 spin_unlock(&em_tree->lock);
2592
Chris Masona52d9a82007-08-27 16:49:44 -04002593 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002594 if (em->start > start || em->start + em->len <= start)
2595 free_extent_map(em);
2596 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002597 free_extent_map(em);
2598 else
2599 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002600 }
Chris Masond1310b22008-01-24 16:13:08 -05002601 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002602 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002603 err = -ENOMEM;
2604 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002605 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002607 em->start = EXTENT_MAP_HOLE;
2608 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002609
2610 if (!path) {
2611 path = btrfs_alloc_path();
2612 BUG_ON(!path);
2613 }
2614
Chris Mason179e29e2007-11-01 11:28:41 -04002615 ret = btrfs_lookup_file_extent(trans, root, path,
2616 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002617 if (ret < 0) {
2618 err = ret;
2619 goto out;
2620 }
2621
2622 if (ret != 0) {
2623 if (path->slots[0] == 0)
2624 goto not_found;
2625 path->slots[0]--;
2626 }
2627
Chris Mason5f39d392007-10-15 16:14:19 -04002628 leaf = path->nodes[0];
2629 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002630 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002631 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002632 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2633 found_type = btrfs_key_type(&found_key);
2634 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002635 found_type != BTRFS_EXTENT_DATA_KEY) {
2636 goto not_found;
2637 }
2638
Chris Mason5f39d392007-10-15 16:14:19 -04002639 found_type = btrfs_file_extent_type(leaf, item);
2640 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002641 if (found_type == BTRFS_FILE_EXTENT_REG) {
2642 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002643 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002644 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002645 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002646 em->start = start;
2647 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002648 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002649 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002650 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002651 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002652 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002653 }
2654 goto not_found_em;
2655 }
Chris Masondb945352007-10-15 16:15:53 -04002656 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2657 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002658 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002659 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002660 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002661 goto insert;
2662 }
Chris Masondb945352007-10-15 16:15:53 -04002663 bytenr += btrfs_file_extent_offset(leaf, item);
2664 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002665 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002666 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002667 goto insert;
2668 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002669 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002670 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002671 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002672 size_t size;
2673 size_t extent_offset;
2674 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002675
Chris Mason5f39d392007-10-15 16:14:19 -04002676 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2677 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002678 extent_end = (extent_start + size + root->sectorsize - 1) &
2679 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002680 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002681 em->start = start;
2682 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002683 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002684 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002685 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002686 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002687 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002688 }
2689 goto not_found_em;
2690 }
2691 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002692
2693 if (!page) {
2694 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002695 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002696 goto out;
2697 }
2698
Chris Mason70dec802008-01-29 09:59:12 -05002699 page_start = page_offset(page) + pg_offset;
2700 extent_offset = page_start - extent_start;
2701 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002702 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002703 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002704 em->len = (copy_size + root->sectorsize - 1) &
2705 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002706 map = kmap(page);
2707 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002708 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002709 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002710 copy_size);
2711 flush_dcache_page(page);
2712 } else if (create && PageUptodate(page)) {
2713 if (!trans) {
2714 kunmap(page);
2715 free_extent_map(em);
2716 em = NULL;
2717 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002718 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002719 goto again;
2720 }
Chris Mason70dec802008-01-29 09:59:12 -05002721 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002722 copy_size);
2723 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002724 }
Chris Masona52d9a82007-08-27 16:49:44 -04002725 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002726 set_extent_uptodate(io_tree, em->start,
2727 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002728 goto insert;
2729 } else {
2730 printk("unkknown found_type %d\n", found_type);
2731 WARN_ON(1);
2732 }
2733not_found:
2734 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002735 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002736not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002737 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002738insert:
2739 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002740 if (em->start > start || extent_map_end(em) <= start) {
2741 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002742 err = -EIO;
2743 goto out;
2744 }
Chris Masond1310b22008-01-24 16:13:08 -05002745
2746 err = 0;
2747 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002748 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002749 /* it is possible that someone inserted the extent into the tree
2750 * while we had the lock dropped. It is also possible that
2751 * an overlapping map exists in the tree
2752 */
Chris Masona52d9a82007-08-27 16:49:44 -04002753 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002754 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755
2756 ret = 0;
2757
Chris Mason3b951512008-04-17 11:29:12 -04002758 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002759 if (existing && (existing->start > start ||
2760 existing->start + existing->len <= start)) {
2761 free_extent_map(existing);
2762 existing = NULL;
2763 }
Chris Mason3b951512008-04-17 11:29:12 -04002764 if (!existing) {
2765 existing = lookup_extent_mapping(em_tree, em->start,
2766 em->len);
2767 if (existing) {
2768 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769 em, start,
2770 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002771 free_extent_map(existing);
2772 if (err) {
2773 free_extent_map(em);
2774 em = NULL;
2775 }
2776 } else {
2777 err = -EIO;
2778 printk("failing to insert %Lu %Lu\n",
2779 start, len);
2780 free_extent_map(em);
2781 em = NULL;
2782 }
2783 } else {
2784 free_extent_map(em);
2785 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002787 }
Chris Masona52d9a82007-08-27 16:49:44 -04002788 }
Chris Masond1310b22008-01-24 16:13:08 -05002789 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002790out:
Chris Masonf4219502008-07-22 11:18:09 -04002791 if (path)
2792 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002793 if (trans) {
2794 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002795 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002796 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002797 }
Chris Masona52d9a82007-08-27 16:49:44 -04002798 }
Chris Masona52d9a82007-08-27 16:49:44 -04002799 if (err) {
2800 free_extent_map(em);
2801 WARN_ON(1);
2802 return ERR_PTR(err);
2803 }
2804 return em;
2805}
2806
Chris Masone1c4b742008-04-22 13:26:46 -04002807#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002808static int btrfs_get_block(struct inode *inode, sector_t iblock,
2809 struct buffer_head *bh_result, int create)
2810{
2811 struct extent_map *em;
2812 u64 start = (u64)iblock << inode->i_blkbits;
2813 struct btrfs_multi_bio *multi = NULL;
2814 struct btrfs_root *root = BTRFS_I(inode)->root;
2815 u64 len;
2816 u64 logical;
2817 u64 map_length;
2818 int ret = 0;
2819
2820 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2821
2822 if (!em || IS_ERR(em))
2823 goto out;
2824
Chris Masone1c4b742008-04-22 13:26:46 -04002825 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002826 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002827 }
Chris Mason16432982008-04-10 10:23:21 -04002828
2829 if (em->block_start == EXTENT_MAP_INLINE) {
2830 ret = -EINVAL;
2831 goto out;
2832 }
2833
Chris Mason16432982008-04-10 10:23:21 -04002834 len = em->start + em->len - start;
2835 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2836
Chris Masone1c4b742008-04-22 13:26:46 -04002837 if (em->block_start == EXTENT_MAP_HOLE ||
2838 em->block_start == EXTENT_MAP_DELALLOC) {
2839 bh_result->b_size = len;
2840 goto out;
2841 }
2842
Chris Mason16432982008-04-10 10:23:21 -04002843 logical = start - em->start;
2844 logical = em->block_start + logical;
2845
2846 map_length = len;
2847 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2848 logical, &map_length, &multi, 0);
2849 BUG_ON(ret);
2850 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2851 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002852
Chris Mason16432982008-04-10 10:23:21 -04002853 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2854 set_buffer_mapped(bh_result);
2855 kfree(multi);
2856out:
2857 free_extent_map(em);
2858 return ret;
2859}
Chris Masone1c4b742008-04-22 13:26:46 -04002860#endif
Chris Mason16432982008-04-10 10:23:21 -04002861
2862static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2863 const struct iovec *iov, loff_t offset,
2864 unsigned long nr_segs)
2865{
Chris Masone1c4b742008-04-22 13:26:46 -04002866 return -EINVAL;
2867#if 0
Chris Mason16432982008-04-10 10:23:21 -04002868 struct file *file = iocb->ki_filp;
2869 struct inode *inode = file->f_mapping->host;
2870
2871 if (rw == WRITE)
2872 return -EINVAL;
2873
2874 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2875 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002876#endif
Chris Mason16432982008-04-10 10:23:21 -04002877}
2878
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002879static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002880{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002881 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002882}
2883
Chris Mason9ebefb182007-06-15 13:50:00 -04002884int btrfs_readpage(struct file *file, struct page *page)
2885{
Chris Masond1310b22008-01-24 16:13:08 -05002886 struct extent_io_tree *tree;
2887 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002888 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002889}
Chris Mason1832a6d2007-12-21 16:27:21 -05002890
Chris Mason39279cc2007-06-12 06:35:45 -04002891static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2892{
Chris Masond1310b22008-01-24 16:13:08 -05002893 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002894
2895
2896 if (current->flags & PF_MEMALLOC) {
2897 redirty_page_for_writepage(wbc, page);
2898 unlock_page(page);
2899 return 0;
2900 }
Chris Masond1310b22008-01-24 16:13:08 -05002901 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002902 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2903}
Chris Mason39279cc2007-06-12 06:35:45 -04002904
Chris Masonf4219502008-07-22 11:18:09 -04002905int btrfs_writepages(struct address_space *mapping,
2906 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002907{
Chris Masond1310b22008-01-24 16:13:08 -05002908 struct extent_io_tree *tree;
2909 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002910 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2911}
2912
Chris Mason3ab2fb52007-11-08 10:59:22 -05002913static int
2914btrfs_readpages(struct file *file, struct address_space *mapping,
2915 struct list_head *pages, unsigned nr_pages)
2916{
Chris Masond1310b22008-01-24 16:13:08 -05002917 struct extent_io_tree *tree;
2918 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002919 return extent_readpages(tree, mapping, pages, nr_pages,
2920 btrfs_get_extent);
2921}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002923{
Chris Masond1310b22008-01-24 16:13:08 -05002924 struct extent_io_tree *tree;
2925 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002926 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002927
Chris Masond1310b22008-01-24 16:13:08 -05002928 tree = &BTRFS_I(page->mapping->host)->io_tree;
2929 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002930 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002931 if (ret == 1) {
2932 ClearPagePrivate(page);
2933 set_page_private(page, 0);
2934 page_cache_release(page);
2935 }
2936 return ret;
2937}
Chris Mason39279cc2007-06-12 06:35:45 -04002938
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2940{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941 return __btrfs_releasepage(page, gfp_flags);
2942}
2943
Chris Masona52d9a82007-08-27 16:49:44 -04002944static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2945{
Chris Masond1310b22008-01-24 16:13:08 -05002946 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002947 struct btrfs_ordered_extent *ordered;
2948 u64 page_start = page_offset(page);
2949 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002950
Chris Masone6dcd2d2008-07-17 12:53:50 -04002951 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002952 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953 if (offset) {
2954 btrfs_releasepage(page, GFP_NOFS);
2955 return;
2956 }
2957
2958 lock_extent(tree, page_start, page_end, GFP_NOFS);
2959 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2960 page_offset(page));
2961 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002962 /*
2963 * IO on this page will never be started, so we need
2964 * to account for any ordered extents now
2965 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966 clear_extent_bit(tree, page_start, page_end,
2967 EXTENT_DIRTY | EXTENT_DELALLOC |
2968 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002969 btrfs_finish_ordered_io(page->mapping->host,
2970 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002971 btrfs_put_ordered_extent(ordered);
2972 lock_extent(tree, page_start, page_end, GFP_NOFS);
2973 }
2974 clear_extent_bit(tree, page_start, page_end,
2975 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2976 EXTENT_ORDERED,
2977 1, 1, GFP_NOFS);
2978 __btrfs_releasepage(page, GFP_NOFS);
2979
Chris Mason4a096752008-07-21 10:29:44 -04002980 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002981 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002982 ClearPagePrivate(page);
2983 set_page_private(page, 0);
2984 page_cache_release(page);
2985 }
Chris Mason39279cc2007-06-12 06:35:45 -04002986}
2987
Chris Mason9ebefb182007-06-15 13:50:00 -04002988/*
2989 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2990 * called from a page fault handler when a page is first dirtied. Hence we must
2991 * be careful to check for EOF conditions here. We set the page up correctly
2992 * for a written page which means we get ENOSPC checking when writing into
2993 * holes and correct delalloc and unwritten extent mapping on filesystems that
2994 * support these features.
2995 *
2996 * We are not allowed to take the i_mutex here so we have to play games to
2997 * protect against truncate races as the page could now be beyond EOF. Because
2998 * vmtruncate() writes the inode size before removing pages, once we have the
2999 * page lock we can determine safely if the page is beyond EOF. If it is not
3000 * beyond EOF, then the page is guaranteed safe against truncation until we
3001 * unlock the page.
3002 */
3003int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3004{
Chris Mason6da6aba2007-12-18 16:15:09 -05003005 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003006 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003007 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3008 struct btrfs_ordered_extent *ordered;
3009 char *kaddr;
3010 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003011 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003012 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003013 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003014 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003015
Chris Mason1832a6d2007-12-21 16:27:21 -05003016 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003017 if (ret)
3018 goto out;
3019
3020 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003022 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003023 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003024 page_start = page_offset(page);
3025 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003026
Chris Mason9ebefb182007-06-15 13:50:00 -04003027 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003028 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003029 /* page got truncated out from underneath us */
3030 goto out_unlock;
3031 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003032 wait_on_page_writeback(page);
3033
3034 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3035 set_page_extent_mapped(page);
3036
Chris Masoneb84ae02008-07-17 13:53:27 -04003037 /*
3038 * we can't set the delalloc bits if there are pending ordered
3039 * extents. Drop our locks and wait for them to finish
3040 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3042 if (ordered) {
3043 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3044 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003045 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003046 btrfs_put_ordered_extent(ordered);
3047 goto again;
3048 }
3049
3050 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3051 page_end, GFP_NOFS);
3052 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003053
3054 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003055 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003056 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003057 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003058 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003059
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060 if (zero_start != PAGE_CACHE_SIZE) {
3061 kaddr = kmap(page);
3062 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3063 flush_dcache_page(page);
3064 kunmap(page);
3065 }
Chris Mason247e7432008-07-17 12:53:51 -04003066 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003067 set_page_dirty(page);
3068 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003069
3070out_unlock:
3071 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003072out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003073 return ret;
3074}
3075
Chris Mason39279cc2007-06-12 06:35:45 -04003076static void btrfs_truncate(struct inode *inode)
3077{
3078 struct btrfs_root *root = BTRFS_I(inode)->root;
3079 int ret;
3080 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003081 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003082 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003083
3084 if (!S_ISREG(inode->i_mode))
3085 return;
3086 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3087 return;
3088
3089 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003090 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003091
Chris Mason39279cc2007-06-12 06:35:45 -04003092 trans = btrfs_start_transaction(root, 1);
3093 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003094 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003095
Josef Bacik7b128762008-07-24 12:17:14 -04003096 ret = btrfs_orphan_add(trans, inode);
3097 if (ret)
3098 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003099 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003100 ret = btrfs_truncate_in_trans(trans, root, inode,
3101 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003102 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003103
Josef Bacik7b128762008-07-24 12:17:14 -04003104 ret = btrfs_orphan_del(trans, inode);
3105 BUG_ON(ret);
3106
3107out:
3108 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003109 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003110 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003111 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003112}
3113
Sven Wegener3b963622008-06-09 21:57:42 -04003114/*
3115 * Invalidate a single dcache entry at the root of the filesystem.
3116 * Needed after creation of snapshot or subvolume.
3117 */
3118void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3119 int namelen)
3120{
3121 struct dentry *alias, *entry;
3122 struct qstr qstr;
3123
3124 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3125 if (alias) {
3126 qstr.name = name;
3127 qstr.len = namelen;
3128 /* change me if btrfs ever gets a d_hash operation */
3129 qstr.hash = full_name_hash(qstr.name, qstr.len);
3130 entry = d_lookup(alias, &qstr);
3131 dput(alias);
3132 if (entry) {
3133 d_invalidate(entry);
3134 dput(entry);
3135 }
3136 }
3137}
3138
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003139int btrfs_create_subvol_root(struct btrfs_root *new_root,
3140 struct btrfs_trans_handle *trans, u64 new_dirid,
3141 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003142{
Chris Mason39279cc2007-06-12 06:35:45 -04003143 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003144
Josef Bacikaec74772008-07-24 12:12:38 -04003145 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003146 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003147 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003148 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003149 inode->i_op = &btrfs_dir_inode_operations;
3150 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003151 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003152
Chris Mason39279cc2007-06-12 06:35:45 -04003153 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003154 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003155
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003156 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003157}
3158
Chris Masonedbd8d42007-12-21 16:27:24 -05003159unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003160 struct file_ra_state *ra, struct file *file,
3161 pgoff_t offset, pgoff_t last_index)
3162{
Chris Mason8e7bf942008-04-28 09:02:36 -04003163 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003164
3165#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003166 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3167 return offset;
3168#else
Chris Mason86479a02007-09-10 19:58:16 -04003169 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3170 return offset + req_size;
3171#endif
3172}
3173
Chris Mason39279cc2007-06-12 06:35:45 -04003174struct inode *btrfs_alloc_inode(struct super_block *sb)
3175{
3176 struct btrfs_inode *ei;
3177
3178 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3179 if (!ei)
3180 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003181 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003182 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003183 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3184 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003185 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003186 return &ei->vfs_inode;
3187}
3188
3189void btrfs_destroy_inode(struct inode *inode)
3190{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003191 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003192 WARN_ON(!list_empty(&inode->i_dentry));
3193 WARN_ON(inode->i_data.nrpages);
3194
Josef Bacik33268ea2008-07-24 12:16:36 -04003195 if (BTRFS_I(inode)->i_acl &&
3196 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3197 posix_acl_release(BTRFS_I(inode)->i_acl);
3198 if (BTRFS_I(inode)->i_default_acl &&
3199 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3200 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3201
Yanbcc63ab2008-07-30 16:29:20 -04003202 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003203 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3204 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3205 " list\n", inode->i_ino);
3206 dump_stack();
3207 }
Yanbcc63ab2008-07-30 16:29:20 -04003208 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003209
Chris Masone6dcd2d2008-07-17 12:53:50 -04003210 while(1) {
3211 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3212 if (!ordered)
3213 break;
3214 else {
3215 printk("found ordered extent %Lu %Lu\n",
3216 ordered->file_offset, ordered->len);
3217 btrfs_remove_ordered_extent(inode, ordered);
3218 btrfs_put_ordered_extent(ordered);
3219 btrfs_put_ordered_extent(ordered);
3220 }
3221 }
Chris Mason8c416c92008-01-14 15:10:26 -05003222 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003223 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3224}
3225
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003226#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3227static void init_once(void *foo)
3228#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003229static void init_once(struct kmem_cache * cachep, void *foo)
3230#else
Chris Mason39279cc2007-06-12 06:35:45 -04003231static void init_once(void * foo, struct kmem_cache * cachep,
3232 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003233#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003234{
3235 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3236
3237 inode_init_once(&ei->vfs_inode);
3238}
3239
3240void btrfs_destroy_cachep(void)
3241{
3242 if (btrfs_inode_cachep)
3243 kmem_cache_destroy(btrfs_inode_cachep);
3244 if (btrfs_trans_handle_cachep)
3245 kmem_cache_destroy(btrfs_trans_handle_cachep);
3246 if (btrfs_transaction_cachep)
3247 kmem_cache_destroy(btrfs_transaction_cachep);
3248 if (btrfs_bit_radix_cachep)
3249 kmem_cache_destroy(btrfs_bit_radix_cachep);
3250 if (btrfs_path_cachep)
3251 kmem_cache_destroy(btrfs_path_cachep);
3252}
3253
Chris Mason86479a02007-09-10 19:58:16 -04003254struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003255 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003256#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3257 void (*ctor)(void *)
3258#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003259 void (*ctor)(struct kmem_cache *, void *)
3260#else
Chris Mason92fee662007-07-25 12:31:35 -04003261 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003262 unsigned long)
3263#endif
3264 )
Chris Mason92fee662007-07-25 12:31:35 -04003265{
3266 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3267 SLAB_MEM_SPREAD | extra_flags), ctor
3268#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3269 ,NULL
3270#endif
3271 );
3272}
3273
Chris Mason39279cc2007-06-12 06:35:45 -04003274int btrfs_init_cachep(void)
3275{
Chris Mason86479a02007-09-10 19:58:16 -04003276 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003277 sizeof(struct btrfs_inode),
3278 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003279 if (!btrfs_inode_cachep)
3280 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003281 btrfs_trans_handle_cachep =
3282 btrfs_cache_create("btrfs_trans_handle_cache",
3283 sizeof(struct btrfs_trans_handle),
3284 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003285 if (!btrfs_trans_handle_cachep)
3286 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003287 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003288 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003289 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003290 if (!btrfs_transaction_cachep)
3291 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003292 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003293 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003294 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003295 if (!btrfs_path_cachep)
3296 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003297 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003298 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003299 if (!btrfs_bit_radix_cachep)
3300 goto fail;
3301 return 0;
3302fail:
3303 btrfs_destroy_cachep();
3304 return -ENOMEM;
3305}
3306
3307static int btrfs_getattr(struct vfsmount *mnt,
3308 struct dentry *dentry, struct kstat *stat)
3309{
3310 struct inode *inode = dentry->d_inode;
3311 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003312 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003313 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003314 return 0;
3315}
3316
3317static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3318 struct inode * new_dir,struct dentry *new_dentry)
3319{
3320 struct btrfs_trans_handle *trans;
3321 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3322 struct inode *new_inode = new_dentry->d_inode;
3323 struct inode *old_inode = old_dentry->d_inode;
3324 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 int ret;
3326
3327 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3328 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3329 return -ENOTEMPTY;
3330 }
Chris Mason5f39d392007-10-15 16:14:19 -04003331
Chris Mason1832a6d2007-12-21 16:27:21 -05003332 ret = btrfs_check_free_space(root, 1, 0);
3333 if (ret)
3334 goto out_unlock;
3335
Chris Mason39279cc2007-06-12 06:35:45 -04003336 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003337
Chris Mason39279cc2007-06-12 06:35:45 -04003338 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003339
3340 old_dentry->d_inode->i_nlink++;
3341 old_dir->i_ctime = old_dir->i_mtime = ctime;
3342 new_dir->i_ctime = new_dir->i_mtime = ctime;
3343 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003344
Chris Mason39279cc2007-06-12 06:35:45 -04003345 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3346 if (ret)
3347 goto out_fail;
3348
3349 if (new_inode) {
3350 new_inode->i_ctime = CURRENT_TIME;
3351 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3352 if (ret)
3353 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003354 if (new_inode->i_nlink == 0) {
3355 ret = btrfs_orphan_add(trans, new_inode);
3356 if (ret)
3357 goto out_fail;
3358 }
Chris Mason39279cc2007-06-12 06:35:45 -04003359 }
Josef Bacikaec74772008-07-24 12:12:38 -04003360 ret = btrfs_set_inode_index(new_dir, old_inode);
3361 if (ret)
3362 goto out_fail;
3363
Chris Mason9c583092008-01-29 15:15:18 -05003364 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003365 if (ret)
3366 goto out_fail;
3367
3368out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003369 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003370out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003371 return ret;
3372}
3373
3374static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3375 const char *symname)
3376{
3377 struct btrfs_trans_handle *trans;
3378 struct btrfs_root *root = BTRFS_I(dir)->root;
3379 struct btrfs_path *path;
3380 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003381 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003382 int err;
3383 int drop_inode = 0;
3384 u64 objectid;
3385 int name_len;
3386 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003387 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003388 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003389 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003390 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003391
3392 name_len = strlen(symname) + 1;
3393 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3394 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003395
Chris Mason1832a6d2007-12-21 16:27:21 -05003396 err = btrfs_check_free_space(root, 1, 0);
3397 if (err)
3398 goto out_fail;
3399
Chris Mason39279cc2007-06-12 06:35:45 -04003400 trans = btrfs_start_transaction(root, 1);
3401 btrfs_set_trans_block_group(trans, dir);
3402
3403 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3404 if (err) {
3405 err = -ENOSPC;
3406 goto out_unlock;
3407 }
3408
Josef Bacikaec74772008-07-24 12:12:38 -04003409 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003410 dentry->d_name.len,
3411 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003412 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3413 err = PTR_ERR(inode);
3414 if (IS_ERR(inode))
3415 goto out_unlock;
3416
Josef Bacik33268ea2008-07-24 12:16:36 -04003417 err = btrfs_init_acl(inode, dir);
3418 if (err) {
3419 drop_inode = 1;
3420 goto out_unlock;
3421 }
3422
Chris Mason39279cc2007-06-12 06:35:45 -04003423 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003424 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003425 if (err)
3426 drop_inode = 1;
3427 else {
3428 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003429 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003430 inode->i_fop = &btrfs_file_operations;
3431 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003432 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3433 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003434 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003435 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3436 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003437 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003438 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003439 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003440 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003441 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003442 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003443 }
3444 dir->i_sb->s_dirt = 1;
3445 btrfs_update_inode_block_group(trans, inode);
3446 btrfs_update_inode_block_group(trans, dir);
3447 if (drop_inode)
3448 goto out_unlock;
3449
3450 path = btrfs_alloc_path();
3451 BUG_ON(!path);
3452 key.objectid = inode->i_ino;
3453 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003454 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3455 datasize = btrfs_file_extent_calc_inline_size(name_len);
3456 err = btrfs_insert_empty_item(trans, root, path, &key,
3457 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003458 if (err) {
3459 drop_inode = 1;
3460 goto out_unlock;
3461 }
Chris Mason5f39d392007-10-15 16:14:19 -04003462 leaf = path->nodes[0];
3463 ei = btrfs_item_ptr(leaf, path->slots[0],
3464 struct btrfs_file_extent_item);
3465 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3466 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003467 BTRFS_FILE_EXTENT_INLINE);
3468 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003469 write_extent_buffer(leaf, symname, ptr, name_len);
3470 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003471 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003472
Chris Mason39279cc2007-06-12 06:35:45 -04003473 inode->i_op = &btrfs_symlink_inode_operations;
3474 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003475 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003476 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003477 err = btrfs_update_inode(trans, root, inode);
3478 if (err)
3479 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003480
3481out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003482 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003483 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003484out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003485 if (drop_inode) {
3486 inode_dec_link_count(inode);
3487 iput(inode);
3488 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003489 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003490 return err;
3491}
Chris Mason16432982008-04-10 10:23:21 -04003492
Chris Masone6dcd2d2008-07-17 12:53:50 -04003493static int btrfs_set_page_dirty(struct page *page)
3494{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003495 return __set_page_dirty_nobuffers(page);
3496}
3497
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003498#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3499static int btrfs_permission(struct inode *inode, int mask)
3500#else
Yanfdebe2b2008-01-14 13:26:08 -05003501static int btrfs_permission(struct inode *inode, int mask,
3502 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003503#endif
Yanfdebe2b2008-01-14 13:26:08 -05003504{
3505 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3506 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003507 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003508}
Chris Mason39279cc2007-06-12 06:35:45 -04003509
3510static struct inode_operations btrfs_dir_inode_operations = {
3511 .lookup = btrfs_lookup,
3512 .create = btrfs_create,
3513 .unlink = btrfs_unlink,
3514 .link = btrfs_link,
3515 .mkdir = btrfs_mkdir,
3516 .rmdir = btrfs_rmdir,
3517 .rename = btrfs_rename,
3518 .symlink = btrfs_symlink,
3519 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003520 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003521 .setxattr = generic_setxattr,
3522 .getxattr = generic_getxattr,
3523 .listxattr = btrfs_listxattr,
3524 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003525 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003526};
Chris Mason39279cc2007-06-12 06:35:45 -04003527static struct inode_operations btrfs_dir_ro_inode_operations = {
3528 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003529 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003530};
Chris Mason39279cc2007-06-12 06:35:45 -04003531static struct file_operations btrfs_dir_file_operations = {
3532 .llseek = generic_file_llseek,
3533 .read = generic_read_dir,
3534 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003535 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003536#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003537 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003538#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003539 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003540};
3541
Chris Masond1310b22008-01-24 16:13:08 -05003542static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003543 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003544 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003545 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003546 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003547 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003548 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003549 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003550 .set_bit_hook = btrfs_set_bit_hook,
3551 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003552};
3553
Chris Mason39279cc2007-06-12 06:35:45 -04003554static struct address_space_operations btrfs_aops = {
3555 .readpage = btrfs_readpage,
3556 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003557 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003558 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003559 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003560 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003561 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003562 .invalidatepage = btrfs_invalidatepage,
3563 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003564 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003565};
3566
3567static struct address_space_operations btrfs_symlink_aops = {
3568 .readpage = btrfs_readpage,
3569 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003570 .invalidatepage = btrfs_invalidatepage,
3571 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003572};
3573
3574static struct inode_operations btrfs_file_inode_operations = {
3575 .truncate = btrfs_truncate,
3576 .getattr = btrfs_getattr,
3577 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003578 .setxattr = generic_setxattr,
3579 .getxattr = generic_getxattr,
3580 .listxattr = btrfs_listxattr,
3581 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003582 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003583};
Josef Bacik618e21d2007-07-11 10:18:17 -04003584static struct inode_operations btrfs_special_inode_operations = {
3585 .getattr = btrfs_getattr,
3586 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003587 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003588 .setxattr = generic_setxattr,
3589 .getxattr = generic_getxattr,
3590 .listxattr = btrfs_listxattr,
3591 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003592};
Chris Mason39279cc2007-06-12 06:35:45 -04003593static struct inode_operations btrfs_symlink_inode_operations = {
3594 .readlink = generic_readlink,
3595 .follow_link = page_follow_link_light,
3596 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003597 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003598};