blob: 47a008c19308ad5cbc03886b95473d05ed48a2a0 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040046#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047
48struct btrfs_iget_args {
49 u64 ino;
50 struct btrfs_root *root;
51};
52
53static struct inode_operations btrfs_dir_inode_operations;
54static struct inode_operations btrfs_symlink_inode_operations;
55static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040056static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040057static struct inode_operations btrfs_file_inode_operations;
58static struct address_space_operations btrfs_aops;
59static struct address_space_operations btrfs_symlink_aops;
60static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050061static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040062
63static struct kmem_cache *btrfs_inode_cachep;
64struct kmem_cache *btrfs_trans_handle_cachep;
65struct kmem_cache *btrfs_transaction_cachep;
66struct kmem_cache *btrfs_bit_radix_cachep;
67struct kmem_cache *btrfs_path_cachep;
68
69#define S_SHIFT 12
70static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
71 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
72 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
73 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
74 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
75 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
76 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
77 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
78};
79
Chris Mason1832a6d2007-12-21 16:27:21 -050080int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
81 int for_del)
82{
Chris Masona2135012008-06-25 16:01:30 -040083 u64 total;
84 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040086 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 int ret = 0;
88
Chris Masona2135012008-06-25 16:01:30 -040089 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
90 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
91 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050092 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050093 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050094 else
Chris Masonf9ef6602008-01-03 09:22:38 -050095 thresh = total * 85;
96
97 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050098
Chris Mason1832a6d2007-12-21 16:27:21 -050099 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
100 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400101 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 return ret;
103}
104
Chris Masonbe20aa92007-12-17 20:14:01 -0500105static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400106{
107 struct btrfs_root *root = BTRFS_I(inode)->root;
108 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400109 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400110 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500111 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400112 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500113 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400115 struct extent_map *em;
116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
117 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400118
Chris Masonb888db22007-08-27 16:49:44 -0400119 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400120 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500121 btrfs_set_trans_block_group(trans, inode);
122
Chris Masondb945352007-10-15 16:15:53 -0400123 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500125 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400126
Chris Mason179e29e2007-11-01 11:28:41 -0400127 if (alloc_hint == EXTENT_MAP_INLINE)
128 goto out;
129
Chris Mason3b951512008-04-17 11:29:12 -0400130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400131 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Mason3b951512008-04-17 11:29:12 -0400132
Chris Masonc59f8952007-12-17 20:14:04 -0500133 while(num_bytes > 0) {
134 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
136 root->sectorsize, 0, 0,
137 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500138 if (ret) {
139 WARN_ON(1);
140 goto out;
141 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400142 em = alloc_extent_map(GFP_NOFS);
143 em->start = start;
144 em->len = ins.offset;
145 em->block_start = ins.objectid;
146 em->bdev = root->fs_info->fs_devices->latest_bdev;
147 while(1) {
148 spin_lock(&em_tree->lock);
149 ret = add_extent_mapping(em_tree, em);
150 spin_unlock(&em_tree->lock);
151 if (ret != -EEXIST) {
152 free_extent_map(em);
153 break;
154 }
155 btrfs_drop_extent_cache(inode, start,
156 start + ins.offset - 1);
157 }
158
Chris Mason98d20f62008-04-14 09:46:10 -0400159 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400160 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
161 ins.offset);
162 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400163 if (num_bytes < cur_alloc_size) {
164 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
165 cur_alloc_size);
166 break;
167 }
Chris Masonc59f8952007-12-17 20:14:04 -0500168 num_bytes -= cur_alloc_size;
169 alloc_hint = ins.objectid + ins.offset;
170 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400171 }
Chris Masonb888db22007-08-27 16:49:44 -0400172out:
173 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500174 return ret;
175}
176
177static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
178{
179 u64 extent_start;
180 u64 extent_end;
181 u64 bytenr;
182 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500183 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500184 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500185 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400186 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500187 struct extent_buffer *leaf;
188 int found_type;
189 struct btrfs_path *path;
190 struct btrfs_file_extent_item *item;
191 int ret;
192 int err;
193 struct btrfs_key found_key;
194
Chris Masonc31f8832008-01-08 15:46:31 -0500195 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 path = btrfs_alloc_path();
197 BUG_ON(!path);
198again:
199 ret = btrfs_lookup_file_extent(NULL, root, path,
200 inode->i_ino, start, 0);
201 if (ret < 0) {
202 btrfs_free_path(path);
203 return ret;
204 }
205
206 cow_end = end;
207 if (ret != 0) {
208 if (path->slots[0] == 0)
209 goto not_found;
210 path->slots[0]--;
211 }
212
213 leaf = path->nodes[0];
214 item = btrfs_item_ptr(leaf, path->slots[0],
215 struct btrfs_file_extent_item);
216
217 /* are we inside the extent that was found? */
218 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
219 found_type = btrfs_key_type(&found_key);
220 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400221 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
224 found_type = btrfs_file_extent_type(leaf, item);
225 extent_start = found_key.offset;
226 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500227 u64 extent_num_bytes;
228
229 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
230 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 err = 0;
232
Chris Mason1832a6d2007-12-21 16:27:21 -0500233 if (loops && start != extent_start)
234 goto not_found;
235
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 if (start < extent_start || start >= extent_end)
237 goto not_found;
238
239 cow_end = min(end, extent_end - 1);
240 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
241 if (bytenr == 0)
242 goto not_found;
243
Chris Masona68d5932008-05-08 14:11:56 -0400244 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
245 bytenr) != 1) {
246 goto not_found;
247 }
248
Chris Masonc31f8832008-01-08 15:46:31 -0500249 /*
250 * we may be called by the resizer, make sure we're inside
251 * the limits of the FS
252 */
Chris Masona68d5932008-05-08 14:11:56 -0400253 block_group = btrfs_lookup_block_group(root->fs_info,
254 bytenr);
255 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500256 goto not_found;
257
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500259 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 goto not_found;
261 }
262loop:
263 if (start > end) {
264 btrfs_free_path(path);
265 return 0;
266 }
267 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500268 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500269 goto again;
270
271not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400272 cow_file_range(inode, start, end);
273 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto loop;
275}
276
277static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
278{
279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400281
Yanb98b6762008-01-08 15:54:37 -0500282 if (btrfs_test_opt(root, NODATACOW) ||
283 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500284 ret = run_delalloc_nocow(inode, start, end);
285 else
286 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500287
Chris Masonb888db22007-08-27 16:49:44 -0400288 return ret;
289}
290
Chris Mason291d6732008-01-29 15:55:23 -0500291int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500293{
Chris Masonbcbfce82008-04-22 13:26:47 -0400294 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500295 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500296 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400297 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500298 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500299 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500301 }
302 return 0;
303}
304
305int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500306 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Chris Masonb0c68f82008-01-31 11:05:37 -0500308 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500309 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400310 unsigned long flags;
311
312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500313 if (end - start + 1 > root->fs_info->delalloc_bytes) {
314 printk("warning: delalloc account %Lu %Lu\n",
315 end - start + 1, root->fs_info->delalloc_bytes);
316 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500317 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 } else {
319 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500320 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500321 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400322 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500323 }
324 return 0;
325}
326
Chris Mason239b14b2008-03-24 15:02:07 -0400327int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
328 size_t size, struct bio *bio)
329{
330 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
331 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400332 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400333 u64 length = 0;
334 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400335 int ret;
336
Chris Masonf2d8d742008-04-21 10:03:05 -0400337 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 map_tree = &root->fs_info->mapping_tree;
339 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400340 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400341 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400342
Chris Mason239b14b2008-03-24 15:02:07 -0400343 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400344 return 1;
345 }
346 return 0;
347}
348
Chris Mason44b8bd72008-04-16 11:14:51 -0400349int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400350 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500351{
Chris Mason065631f2008-02-20 12:07:25 -0500352 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500353 int ret = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400354 struct btrfs_ordered_sum *sums;
Chris Masone0156402008-04-16 11:15:20 -0400355
356 ret = btrfs_csum_one_bio(root, bio, &sums);
357 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500358
Chris Masone6dcd2d2008-07-17 12:53:50 -0400359 ret = btrfs_add_ordered_sum(inode, sums);
Chris Mason44b8bd72008-04-16 11:14:51 -0400360 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400361
Chris Mason8b712842008-06-11 16:50:36 -0400362 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400363}
364
365int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
366 int mirror_num)
367{
368 struct btrfs_root *root = BTRFS_I(inode)->root;
369 int ret = 0;
370
Chris Masone6dcd2d2008-07-17 12:53:50 -0400371 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
372 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500373
Chris Masone6dcd2d2008-07-17 12:53:50 -0400374 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400375 goto mapit;
376 }
Chris Mason065631f2008-02-20 12:07:25 -0500377
Chris Mason44b8bd72008-04-16 11:14:51 -0400378 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
379 inode, rw, bio, mirror_num,
380 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400381mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400382 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500383}
Chris Mason6885f302008-02-20 16:11:05 -0500384
Chris Masone6dcd2d2008-07-17 12:53:50 -0400385static int add_pending_csums(struct btrfs_trans_handle *trans,
386 struct inode *inode, u64 file_offset,
387 struct list_head *list)
388{
389 struct list_head *cur;
390 struct btrfs_ordered_sum *sum;
391
392 btrfs_set_trans_block_group(trans, inode);
393 while(!list_empty(list)) {
394 cur = list->next;
395 sum = list_entry(cur, struct btrfs_ordered_sum, list);
396 mutex_lock(&BTRFS_I(inode)->csum_mutex);
397 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
398 inode, sum);
399 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
400 list_del(&sum->list);
401 kfree(sum);
402 }
403 return 0;
404}
405
Chris Mason247e7432008-07-17 12:53:51 -0400406struct btrfs_writepage_fixup {
407 struct page *page;
408 struct btrfs_work work;
409};
410
411/* see btrfs_writepage_start_hook for details on why this is required */
412void btrfs_writepage_fixup_worker(struct btrfs_work *work)
413{
414 struct btrfs_writepage_fixup *fixup;
415 struct btrfs_ordered_extent *ordered;
416 struct page *page;
417 struct inode *inode;
418 u64 page_start;
419 u64 page_end;
420
421 fixup = container_of(work, struct btrfs_writepage_fixup, work);
422 page = fixup->page;
423
424 lock_page(page);
425 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
426 ClearPageChecked(page);
427 goto out_page;
428 }
429
430 inode = page->mapping->host;
431 page_start = page_offset(page);
432 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
433
434 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
435 ordered = btrfs_lookup_ordered_extent(inode, page_start);
436 if (ordered)
437 goto out;
438
439 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
440 GFP_NOFS);
441 ClearPageChecked(page);
442out:
443 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
444out_page:
445 unlock_page(page);
446 page_cache_release(page);
447}
448
449/*
450 * There are a few paths in the higher layers of the kernel that directly
451 * set the page dirty bit without asking the filesystem if it is a
452 * good idea. This causes problems because we want to make sure COW
453 * properly happens and the data=ordered rules are followed.
454 *
455 * In our case any range that doesn't have the EXTENT_ORDERED bit set
456 * hasn't been properly setup for IO. We kick off an async process
457 * to fix it up. The async helper will wait for ordered extents, set
458 * the delalloc bit and make it safe to write the page.
459 */
460int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
461{
462 struct inode *inode = page->mapping->host;
463 struct btrfs_writepage_fixup *fixup;
464 struct btrfs_root *root = BTRFS_I(inode)->root;
465 int ret;
466
467 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
468 EXTENT_ORDERED, 0);
469 if (ret)
470 return 0;
471
472 if (PageChecked(page))
473 return -EAGAIN;
474
475 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
476 if (!fixup)
477 return -EAGAIN;
478printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
479 SetPageChecked(page);
480 page_cache_get(page);
481 fixup->work.func = btrfs_writepage_fixup_worker;
482 fixup->page = page;
483 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
484 return -EAGAIN;
485}
486
Chris Masone6dcd2d2008-07-17 12:53:50 -0400487int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
488 struct extent_state *state, int uptodate)
489{
490 struct inode *inode = page->mapping->host;
491 struct btrfs_root *root = BTRFS_I(inode)->root;
492 struct btrfs_trans_handle *trans;
493 struct btrfs_ordered_extent *ordered_extent;
494 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
495 u64 alloc_hint = 0;
496 struct list_head list;
497 struct btrfs_key ins;
498 int ret;
499
500 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
501 if (!ret) {
502 return 0;
503 }
504
505 trans = btrfs_start_transaction(root, 1);
506
507 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
508 BUG_ON(!ordered_extent);
509
510 lock_extent(io_tree, ordered_extent->file_offset,
511 ordered_extent->file_offset + ordered_extent->len - 1,
512 GFP_NOFS);
513
514 INIT_LIST_HEAD(&list);
515
516 ins.objectid = ordered_extent->start;
517 ins.offset = ordered_extent->len;
518 ins.type = BTRFS_EXTENT_ITEM_KEY;
519 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
520 trans->transid, inode->i_ino,
521 ordered_extent->file_offset, &ins);
522 BUG_ON(ret);
523 ret = btrfs_drop_extents(trans, root, inode,
524 ordered_extent->file_offset,
525 ordered_extent->file_offset +
526 ordered_extent->len,
527 ordered_extent->file_offset, &alloc_hint);
528 BUG_ON(ret);
529 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
530 ordered_extent->file_offset,
531 ordered_extent->start,
532 ordered_extent->len,
533 ordered_extent->len, 0);
534 BUG_ON(ret);
535 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
536 ordered_extent->file_offset +
537 ordered_extent->len - 1);
538 inode->i_blocks += ordered_extent->len >> 9;
539 unlock_extent(io_tree, ordered_extent->file_offset,
540 ordered_extent->file_offset + ordered_extent->len - 1,
541 GFP_NOFS);
542 add_pending_csums(trans, inode, ordered_extent->file_offset,
543 &ordered_extent->list);
544
545 btrfs_remove_ordered_extent(inode, ordered_extent);
546 /* once for us */
547 btrfs_put_ordered_extent(ordered_extent);
548 /* once for the tree */
549 btrfs_put_ordered_extent(ordered_extent);
550
551 btrfs_update_inode(trans, root, inode);
552 btrfs_end_transaction(trans, root);
553 return 0;
554}
555
Chris Mason07157aa2007-08-30 08:50:51 -0400556int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
557{
558 int ret = 0;
559 struct inode *inode = page->mapping->host;
560 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500561 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400562 struct btrfs_csum_item *item;
563 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400564 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400565
Yanb98b6762008-01-08 15:54:37 -0500566 if (btrfs_test_opt(root, NODATASUM) ||
567 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500568 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400569
Chris Mason07157aa2007-08-30 08:50:51 -0400570 path = btrfs_alloc_path();
571 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
572 if (IS_ERR(item)) {
573 ret = PTR_ERR(item);
574 /* a csum that isn't present is a preallocated region. */
575 if (ret == -ENOENT || ret == -EFBIG)
576 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400577 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400578 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
579 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400580 goto out;
581 }
Chris Masonff79f812007-10-15 16:22:25 -0400582 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
583 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500584 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400585out:
586 if (path)
587 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400588 return ret;
589}
590
Chris Mason7e383262008-04-09 16:28:12 -0400591struct io_failure_record {
592 struct page *page;
593 u64 start;
594 u64 len;
595 u64 logical;
596 int last_mirror;
597};
598
Chris Mason1259ab72008-05-12 13:39:03 -0400599int btrfs_io_failed_hook(struct bio *failed_bio,
600 struct page *page, u64 start, u64 end,
601 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400602{
603 struct io_failure_record *failrec = NULL;
604 u64 private;
605 struct extent_map *em;
606 struct inode *inode = page->mapping->host;
607 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400608 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400609 struct bio *bio;
610 int num_copies;
611 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400612 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400613 u64 logical;
614
615 ret = get_state_private(failure_tree, start, &private);
616 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400617 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
618 if (!failrec)
619 return -ENOMEM;
620 failrec->start = start;
621 failrec->len = end - start + 1;
622 failrec->last_mirror = 0;
623
Chris Mason3b951512008-04-17 11:29:12 -0400624 spin_lock(&em_tree->lock);
625 em = lookup_extent_mapping(em_tree, start, failrec->len);
626 if (em->start > start || em->start + em->len < start) {
627 free_extent_map(em);
628 em = NULL;
629 }
630 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400631
632 if (!em || IS_ERR(em)) {
633 kfree(failrec);
634 return -EIO;
635 }
636 logical = start - em->start;
637 logical = em->block_start + logical;
638 failrec->logical = logical;
639 free_extent_map(em);
640 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
641 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400642 set_state_private(failure_tree, start,
643 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400644 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400645 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400646 }
647 num_copies = btrfs_num_copies(
648 &BTRFS_I(inode)->root->fs_info->mapping_tree,
649 failrec->logical, failrec->len);
650 failrec->last_mirror++;
651 if (!state) {
652 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
653 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
654 failrec->start,
655 EXTENT_LOCKED);
656 if (state && state->start != failrec->start)
657 state = NULL;
658 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
659 }
660 if (!state || failrec->last_mirror > num_copies) {
661 set_state_private(failure_tree, failrec->start, 0);
662 clear_extent_bits(failure_tree, failrec->start,
663 failrec->start + failrec->len - 1,
664 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
665 kfree(failrec);
666 return -EIO;
667 }
668 bio = bio_alloc(GFP_NOFS, 1);
669 bio->bi_private = state;
670 bio->bi_end_io = failed_bio->bi_end_io;
671 bio->bi_sector = failrec->logical >> 9;
672 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400673 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400674 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400675 if (failed_bio->bi_rw & (1 << BIO_RW))
676 rw = WRITE;
677 else
678 rw = READ;
679
680 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
681 failrec->last_mirror);
682 return 0;
683}
684
685int btrfs_clean_io_failures(struct inode *inode, u64 start)
686{
687 u64 private;
688 u64 private_failure;
689 struct io_failure_record *failure;
690 int ret;
691
692 private = 0;
693 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
694 (u64)-1, 1, EXTENT_DIRTY)) {
695 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
696 start, &private_failure);
697 if (ret == 0) {
698 failure = (struct io_failure_record *)(unsigned long)
699 private_failure;
700 set_state_private(&BTRFS_I(inode)->io_failure_tree,
701 failure->start, 0);
702 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
703 failure->start,
704 failure->start + failure->len - 1,
705 EXTENT_DIRTY | EXTENT_LOCKED,
706 GFP_NOFS);
707 kfree(failure);
708 }
709 }
Chris Mason7e383262008-04-09 16:28:12 -0400710 return 0;
711}
712
Chris Mason70dec802008-01-29 09:59:12 -0500713int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
714 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400715{
Chris Mason35ebb932007-10-30 16:56:53 -0400716 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400717 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500718 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400719 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500720 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400721 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400722 struct btrfs_root *root = BTRFS_I(inode)->root;
723 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400724 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500725
Yanb98b6762008-01-08 15:54:37 -0500726 if (btrfs_test_opt(root, NODATASUM) ||
727 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500728 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500729 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500730 private = state->private;
731 ret = 0;
732 } else {
733 ret = get_state_private(io_tree, start, &private);
734 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400735 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400736 kaddr = kmap_atomic(page, KM_IRQ0);
737 if (ret) {
738 goto zeroit;
739 }
Chris Masonff79f812007-10-15 16:22:25 -0400740 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
741 btrfs_csum_final(csum, (char *)&csum);
742 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400743 goto zeroit;
744 }
745 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400746 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400747
748 /* if the io failure tree for this inode is non-empty,
749 * check to see if we've recovered from a failed IO
750 */
Chris Mason1259ab72008-05-12 13:39:03 -0400751 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400752 return 0;
753
754zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500755 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
756 page->mapping->host->i_ino, (unsigned long long)start, csum,
757 private);
Chris Masondb945352007-10-15 16:15:53 -0400758 memset(kaddr + offset, 1, end - start + 1);
759 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400760 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400761 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400762 if (private == 0)
763 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400764 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400765}
Chris Masonb888db22007-08-27 16:49:44 -0400766
Chris Mason39279cc2007-06-12 06:35:45 -0400767void btrfs_read_locked_inode(struct inode *inode)
768{
769 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400770 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400772 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400773 struct btrfs_root *root = BTRFS_I(inode)->root;
774 struct btrfs_key location;
775 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400776 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 int ret;
778
779 path = btrfs_alloc_path();
780 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400781 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500782
Chris Mason39279cc2007-06-12 06:35:45 -0400783 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400784 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400785 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400786
Chris Mason5f39d392007-10-15 16:14:19 -0400787 leaf = path->nodes[0];
788 inode_item = btrfs_item_ptr(leaf, path->slots[0],
789 struct btrfs_inode_item);
790
791 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
792 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
793 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
794 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
795 inode->i_size = btrfs_inode_size(leaf, inode_item);
796
797 tspec = btrfs_inode_atime(inode_item);
798 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
799 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
800
801 tspec = btrfs_inode_mtime(inode_item);
802 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
803 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
804
805 tspec = btrfs_inode_ctime(inode_item);
806 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
807 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
808
809 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
810 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400811 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400812 rdev = btrfs_inode_rdev(leaf, inode_item);
813
814 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400815 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
816 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500817 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500818 if (!BTRFS_I(inode)->block_group) {
819 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400820 NULL, 0,
821 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500822 }
Chris Mason39279cc2007-06-12 06:35:45 -0400823 btrfs_free_path(path);
824 inode_item = NULL;
825
Chris Mason39279cc2007-06-12 06:35:45 -0400826 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400827 case S_IFREG:
828 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400829 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500830 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400831 inode->i_fop = &btrfs_file_operations;
832 inode->i_op = &btrfs_file_inode_operations;
833 break;
834 case S_IFDIR:
835 inode->i_fop = &btrfs_dir_file_operations;
836 if (root == root->fs_info->tree_root)
837 inode->i_op = &btrfs_dir_ro_inode_operations;
838 else
839 inode->i_op = &btrfs_dir_inode_operations;
840 break;
841 case S_IFLNK:
842 inode->i_op = &btrfs_symlink_inode_operations;
843 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400844 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400845 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400846 default:
847 init_special_inode(inode, inode->i_mode, rdev);
848 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400849 }
850 return;
851
852make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400853 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400854 make_bad_inode(inode);
855}
856
Chris Mason5f39d392007-10-15 16:14:19 -0400857static void fill_inode_item(struct extent_buffer *leaf,
858 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400859 struct inode *inode)
860{
Chris Mason5f39d392007-10-15 16:14:19 -0400861 btrfs_set_inode_uid(leaf, item, inode->i_uid);
862 btrfs_set_inode_gid(leaf, item, inode->i_gid);
863 btrfs_set_inode_size(leaf, item, inode->i_size);
864 btrfs_set_inode_mode(leaf, item, inode->i_mode);
865 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
866
867 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
868 inode->i_atime.tv_sec);
869 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
870 inode->i_atime.tv_nsec);
871
872 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
873 inode->i_mtime.tv_sec);
874 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
875 inode->i_mtime.tv_nsec);
876
877 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
878 inode->i_ctime.tv_sec);
879 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
880 inode->i_ctime.tv_nsec);
881
882 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
883 btrfs_set_inode_generation(leaf, item, inode->i_generation);
884 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500885 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400886 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400887 BTRFS_I(inode)->block_group->key.objectid);
888}
889
Chris Masona52d9a82007-08-27 16:49:44 -0400890int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400891 struct btrfs_root *root,
892 struct inode *inode)
893{
894 struct btrfs_inode_item *inode_item;
895 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400896 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400897 int ret;
898
899 path = btrfs_alloc_path();
900 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400901 ret = btrfs_lookup_inode(trans, root, path,
902 &BTRFS_I(inode)->location, 1);
903 if (ret) {
904 if (ret > 0)
905 ret = -ENOENT;
906 goto failed;
907 }
908
Chris Mason5f39d392007-10-15 16:14:19 -0400909 leaf = path->nodes[0];
910 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400911 struct btrfs_inode_item);
912
Chris Mason5f39d392007-10-15 16:14:19 -0400913 fill_inode_item(leaf, inode_item, inode);
914 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400915 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400916 ret = 0;
917failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400918 btrfs_free_path(path);
919 return ret;
920}
921
922
923static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
924 struct btrfs_root *root,
925 struct inode *dir,
926 struct dentry *dentry)
927{
928 struct btrfs_path *path;
929 const char *name = dentry->d_name.name;
930 int name_len = dentry->d_name.len;
931 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400932 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400933 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400934 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400935
936 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400937 if (!path) {
938 ret = -ENOMEM;
939 goto err;
940 }
941
Chris Mason39279cc2007-06-12 06:35:45 -0400942 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
943 name, name_len, -1);
944 if (IS_ERR(di)) {
945 ret = PTR_ERR(di);
946 goto err;
947 }
948 if (!di) {
949 ret = -ENOENT;
950 goto err;
951 }
Chris Mason5f39d392007-10-15 16:14:19 -0400952 leaf = path->nodes[0];
953 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400954 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400955 if (ret)
956 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400957 btrfs_release_path(root, path);
958
959 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400960 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400961 if (IS_ERR(di)) {
962 ret = PTR_ERR(di);
963 goto err;
964 }
965 if (!di) {
966 ret = -ENOENT;
967 goto err;
968 }
969 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -0400970 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400971
972 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500973 ret = btrfs_del_inode_ref(trans, root, name, name_len,
974 dentry->d_inode->i_ino,
975 dentry->d_parent->d_inode->i_ino);
976 if (ret) {
977 printk("failed to delete reference to %.*s, "
978 "inode %lu parent %lu\n", name_len, name,
979 dentry->d_inode->i_ino,
980 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500981 }
Chris Mason39279cc2007-06-12 06:35:45 -0400982err:
983 btrfs_free_path(path);
984 if (!ret) {
985 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400986 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400987 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500988#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
989 dentry->d_inode->i_nlink--;
990#else
Chris Mason39279cc2007-06-12 06:35:45 -0400991 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500992#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400993 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400994 dir->i_sb->s_dirt = 1;
995 }
996 return ret;
997}
998
999static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1000{
1001 struct btrfs_root *root;
1002 struct btrfs_trans_handle *trans;
1003 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001004 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001005
1006 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001007
1008 ret = btrfs_check_free_space(root, 1, 1);
1009 if (ret)
1010 goto fail;
1011
Chris Mason39279cc2007-06-12 06:35:45 -04001012 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001013
Chris Mason39279cc2007-06-12 06:35:45 -04001014 btrfs_set_trans_block_group(trans, dir);
1015 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001016 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001017
Chris Mason89ce8a62008-06-25 16:01:31 -04001018 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001019fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001020 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001021 return ret;
1022}
1023
1024static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1025{
1026 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001027 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001028 int ret;
1029 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001030 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001031 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001032
Chris Mason925baed2008-06-25 16:01:30 -04001033 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001034 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001035 }
Yan134d4512007-10-25 15:49:25 -04001036
Chris Mason1832a6d2007-12-21 16:27:21 -05001037 ret = btrfs_check_free_space(root, 1, 1);
1038 if (ret)
1039 goto fail;
1040
Chris Mason39279cc2007-06-12 06:35:45 -04001041 trans = btrfs_start_transaction(root, 1);
1042 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001043
1044 /* now the directory is empty */
1045 err = btrfs_unlink_trans(trans, root, dir, dentry);
1046 if (!err) {
1047 inode->i_size = 0;
1048 }
Chris Mason39544012007-12-12 14:38:19 -05001049
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001050 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001051 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001052fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001053 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001054
Chris Mason39279cc2007-06-12 06:35:45 -04001055 if (ret && !err)
1056 err = ret;
1057 return err;
1058}
1059
Chris Mason39279cc2007-06-12 06:35:45 -04001060/*
Chris Mason39279cc2007-06-12 06:35:45 -04001061 * this can truncate away extent items, csum items and directory items.
1062 * It starts at a high offset and removes keys until it can't find
1063 * any higher than i_size.
1064 *
1065 * csum items that cross the new i_size are truncated to the new size
1066 * as well.
1067 */
1068static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1069 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001070 struct inode *inode,
1071 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001072{
1073 int ret;
1074 struct btrfs_path *path;
1075 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001076 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001077 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001078 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001079 struct btrfs_file_extent_item *fi;
1080 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001081 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001082 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001083 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001084 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001085 int found_extent;
1086 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001087 int pending_del_nr = 0;
1088 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001089 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001090 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001091
Chris Masone6dcd2d2008-07-17 12:53:50 -04001092 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason3b951512008-04-17 11:29:12 -04001093 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001094 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001095 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001096 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001097
Chris Mason39279cc2007-06-12 06:35:45 -04001098 /* FIXME, add redo link to tree so we don't leak on crash */
1099 key.objectid = inode->i_ino;
1100 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001101 key.type = (u8)-1;
1102
Chris Mason85e21ba2008-01-29 15:11:36 -05001103 btrfs_init_path(path);
1104search_again:
1105 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1106 if (ret < 0) {
1107 goto error;
1108 }
1109 if (ret > 0) {
1110 BUG_ON(path->slots[0] == 0);
1111 path->slots[0]--;
1112 }
1113
Chris Mason39279cc2007-06-12 06:35:45 -04001114 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001115 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001116 leaf = path->nodes[0];
1117 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1118 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001119
Chris Mason5f39d392007-10-15 16:14:19 -04001120 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001121 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001122
Chris Mason85e21ba2008-01-29 15:11:36 -05001123 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001124 break;
1125
Chris Mason5f39d392007-10-15 16:14:19 -04001126 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001127 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001128 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001129 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001130 extent_type = btrfs_file_extent_type(leaf, fi);
1131 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001132 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001133 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001134 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1135 struct btrfs_item *item = btrfs_item_nr(leaf,
1136 path->slots[0]);
1137 item_end += btrfs_file_extent_inline_len(leaf,
1138 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001139 }
Yan008630c2007-11-07 13:31:09 -05001140 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001141 }
1142 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1143 ret = btrfs_csum_truncate(trans, root, path,
1144 inode->i_size);
1145 BUG_ON(ret);
1146 }
Yan008630c2007-11-07 13:31:09 -05001147 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001148 if (found_type == BTRFS_DIR_ITEM_KEY) {
1149 found_type = BTRFS_INODE_ITEM_KEY;
1150 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1151 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001152 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1153 found_type = BTRFS_XATTR_ITEM_KEY;
1154 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1155 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001156 } else if (found_type) {
1157 found_type--;
1158 } else {
1159 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001160 }
Yana61721d2007-09-17 11:08:38 -04001161 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001162 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001163 }
Chris Mason5f39d392007-10-15 16:14:19 -04001164 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001165 del_item = 1;
1166 else
1167 del_item = 0;
1168 found_extent = 0;
1169
1170 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001171 if (found_type != BTRFS_EXTENT_DATA_KEY)
1172 goto delete;
1173
1174 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001175 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001176 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001177 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001178 u64 orig_num_bytes =
1179 btrfs_file_extent_num_bytes(leaf, fi);
1180 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001181 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001182 extent_num_bytes = extent_num_bytes &
1183 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001184 btrfs_set_file_extent_num_bytes(leaf, fi,
1185 extent_num_bytes);
1186 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001187 extent_num_bytes);
1188 if (extent_start != 0)
1189 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001190 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001191 } else {
Chris Masondb945352007-10-15 16:15:53 -04001192 extent_num_bytes =
1193 btrfs_file_extent_disk_num_bytes(leaf,
1194 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001195 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001196 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001197 if (extent_start != 0) {
1198 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001199 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001200 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001201 root_gen = btrfs_header_generation(leaf);
1202 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001203 }
Chris Mason90692182008-02-08 13:49:28 -05001204 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1205 if (!del_item) {
1206 u32 newsize = inode->i_size - found_key.offset;
1207 dec_i_blocks(inode, item_end + 1 -
1208 found_key.offset - newsize);
1209 newsize =
1210 btrfs_file_extent_calc_inline_size(newsize);
1211 ret = btrfs_truncate_item(trans, root, path,
1212 newsize, 1);
1213 BUG_ON(ret);
1214 } else {
1215 dec_i_blocks(inode, item_end + 1 -
1216 found_key.offset);
1217 }
Chris Mason39279cc2007-06-12 06:35:45 -04001218 }
Chris Mason179e29e2007-11-01 11:28:41 -04001219delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001220 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001221 if (!pending_del_nr) {
1222 /* no pending yet, add ourselves */
1223 pending_del_slot = path->slots[0];
1224 pending_del_nr = 1;
1225 } else if (pending_del_nr &&
1226 path->slots[0] + 1 == pending_del_slot) {
1227 /* hop on the pending chunk */
1228 pending_del_nr++;
1229 pending_del_slot = path->slots[0];
1230 } else {
1231 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1232 }
Chris Mason39279cc2007-06-12 06:35:45 -04001233 } else {
1234 break;
1235 }
Chris Mason39279cc2007-06-12 06:35:45 -04001236 if (found_extent) {
1237 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001238 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001239 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001240 root_gen, inode->i_ino,
1241 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001242 BUG_ON(ret);
1243 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001244next:
1245 if (path->slots[0] == 0) {
1246 if (pending_del_nr)
1247 goto del_pending;
1248 btrfs_release_path(root, path);
1249 goto search_again;
1250 }
1251
1252 path->slots[0]--;
1253 if (pending_del_nr &&
1254 path->slots[0] + 1 != pending_del_slot) {
1255 struct btrfs_key debug;
1256del_pending:
1257 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1258 pending_del_slot);
1259 ret = btrfs_del_items(trans, root, path,
1260 pending_del_slot,
1261 pending_del_nr);
1262 BUG_ON(ret);
1263 pending_del_nr = 0;
1264 btrfs_release_path(root, path);
1265 goto search_again;
1266 }
Chris Mason39279cc2007-06-12 06:35:45 -04001267 }
1268 ret = 0;
1269error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001270 if (pending_del_nr) {
1271 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1272 pending_del_nr);
1273 }
Chris Mason39279cc2007-06-12 06:35:45 -04001274 btrfs_free_path(path);
1275 inode->i_sb->s_dirt = 1;
1276 return ret;
1277}
1278
Chris Masona52d9a82007-08-27 16:49:44 -04001279/*
1280 * taken from block_truncate_page, but does cow as it zeros out
1281 * any bytes left in the last page in the file.
1282 */
1283static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1284{
1285 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001286 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001287 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1288 struct btrfs_ordered_extent *ordered;
1289 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001290 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001291 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1292 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1293 struct page *page;
1294 int ret = 0;
1295 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001296 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001297
1298 if ((offset & (blocksize - 1)) == 0)
1299 goto out;
1300
1301 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001302again:
Chris Masona52d9a82007-08-27 16:49:44 -04001303 page = grab_cache_page(mapping, index);
1304 if (!page)
1305 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001306
1307 page_start = page_offset(page);
1308 page_end = page_start + PAGE_CACHE_SIZE - 1;
1309
Chris Masona52d9a82007-08-27 16:49:44 -04001310 if (!PageUptodate(page)) {
1311 ret = btrfs_readpage(NULL, page);
1312 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001313 if (page->mapping != mapping) {
1314 unlock_page(page);
1315 page_cache_release(page);
1316 goto again;
1317 }
Chris Masona52d9a82007-08-27 16:49:44 -04001318 if (!PageUptodate(page)) {
1319 ret = -EIO;
1320 goto out;
1321 }
1322 }
Chris Mason211c17f2008-05-15 09:13:45 -04001323 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001324
1325 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1326 set_page_extent_mapped(page);
1327
1328 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1329 if (ordered) {
1330 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1331 unlock_page(page);
1332 page_cache_release(page);
1333 btrfs_wait_ordered_extent(inode, ordered);
1334 btrfs_put_ordered_extent(ordered);
1335 goto again;
1336 }
1337
1338 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1339 page_end, GFP_NOFS);
1340 ret = 0;
1341 if (offset != PAGE_CACHE_SIZE) {
1342 kaddr = kmap(page);
1343 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1344 flush_dcache_page(page);
1345 kunmap(page);
1346 }
Chris Mason247e7432008-07-17 12:53:51 -04001347 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001348 set_page_dirty(page);
1349 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001350
Chris Mason39279cc2007-06-12 06:35:45 -04001351 unlock_page(page);
1352 page_cache_release(page);
1353out:
1354 return ret;
1355}
1356
1357static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1358{
1359 struct inode *inode = dentry->d_inode;
1360 int err;
1361
1362 err = inode_change_ok(inode, attr);
1363 if (err)
1364 return err;
1365
1366 if (S_ISREG(inode->i_mode) &&
1367 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1368 struct btrfs_trans_handle *trans;
1369 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001370 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001371
Chris Mason5f39d392007-10-15 16:14:19 -04001372 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001373 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001374 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001375 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001376 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001377
Chris Mason1b0f7c22008-01-30 14:33:02 -05001378 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001379 goto out;
1380
Chris Mason1832a6d2007-12-21 16:27:21 -05001381 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001382 if (err)
1383 goto fail;
1384
Chris Mason39279cc2007-06-12 06:35:45 -04001385 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1386
Chris Mason5f564062008-01-22 16:47:59 -05001387 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001388 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1389 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001390
Chris Mason39279cc2007-06-12 06:35:45 -04001391 trans = btrfs_start_transaction(root, 1);
1392 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001393 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001394 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001395 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001396
Chris Mason179e29e2007-11-01 11:28:41 -04001397 if (alloc_hint != EXTENT_MAP_INLINE) {
1398 err = btrfs_insert_file_extent(trans, root,
1399 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001400 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001401 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001402 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001403 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001404 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001405 }
Chris Mason39279cc2007-06-12 06:35:45 -04001406 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001407 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001408 if (err)
1409 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001410 }
1411out:
1412 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001413fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001414 return err;
1415}
Chris Mason61295eb2008-01-14 16:24:38 -05001416
Chris Mason39279cc2007-06-12 06:35:45 -04001417void btrfs_delete_inode(struct inode *inode)
1418{
1419 struct btrfs_trans_handle *trans;
1420 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001421 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001422 int ret;
1423
Chris Masone6dcd2d2008-07-17 12:53:50 -04001424 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001425 truncate_inode_pages(&inode->i_data, 0);
1426 if (is_bad_inode(inode)) {
1427 goto no_delete;
1428 }
Chris Mason5f39d392007-10-15 16:14:19 -04001429
Chris Mason39279cc2007-06-12 06:35:45 -04001430 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001431 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001432
Chris Mason39279cc2007-06-12 06:35:45 -04001433 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001434 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001435 if (ret)
1436 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001437
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001438 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001439 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001440
Chris Mason39279cc2007-06-12 06:35:45 -04001441 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001442 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001443 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001444
1445no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001446 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001447 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001448 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001449no_delete:
1450 clear_inode(inode);
1451}
1452
1453/*
1454 * this returns the key found in the dir entry in the location pointer.
1455 * If no dir entries were found, location->objectid is 0.
1456 */
1457static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1458 struct btrfs_key *location)
1459{
1460 const char *name = dentry->d_name.name;
1461 int namelen = dentry->d_name.len;
1462 struct btrfs_dir_item *di;
1463 struct btrfs_path *path;
1464 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001465 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001466
Chris Mason39544012007-12-12 14:38:19 -05001467 if (namelen == 1 && strcmp(name, ".") == 0) {
1468 location->objectid = dir->i_ino;
1469 location->type = BTRFS_INODE_ITEM_KEY;
1470 location->offset = 0;
1471 return 0;
1472 }
Chris Mason39279cc2007-06-12 06:35:45 -04001473 path = btrfs_alloc_path();
1474 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001475
Chris Mason7a720532007-12-13 09:06:59 -05001476 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001477 struct btrfs_key key;
1478 struct extent_buffer *leaf;
1479 u32 nritems;
1480 int slot;
1481
1482 key.objectid = dir->i_ino;
1483 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1484 key.offset = 0;
1485 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1486 BUG_ON(ret == 0);
1487 ret = 0;
1488
1489 leaf = path->nodes[0];
1490 slot = path->slots[0];
1491 nritems = btrfs_header_nritems(leaf);
1492 if (slot >= nritems)
1493 goto out_err;
1494
1495 btrfs_item_key_to_cpu(leaf, &key, slot);
1496 if (key.objectid != dir->i_ino ||
1497 key.type != BTRFS_INODE_REF_KEY) {
1498 goto out_err;
1499 }
1500 location->objectid = key.offset;
1501 location->type = BTRFS_INODE_ITEM_KEY;
1502 location->offset = 0;
1503 goto out;
1504 }
1505
Chris Mason39279cc2007-06-12 06:35:45 -04001506 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1507 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001508 if (IS_ERR(di))
1509 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001510 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001511 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001512 }
Chris Mason5f39d392007-10-15 16:14:19 -04001513 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001514out:
Chris Mason39279cc2007-06-12 06:35:45 -04001515 btrfs_free_path(path);
1516 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001517out_err:
1518 location->objectid = 0;
1519 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001520}
1521
1522/*
1523 * when we hit a tree root in a directory, the btrfs part of the inode
1524 * needs to be changed to reflect the root directory of the tree root. This
1525 * is kind of like crossing a mount point.
1526 */
1527static int fixup_tree_root_location(struct btrfs_root *root,
1528 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001529 struct btrfs_root **sub_root,
1530 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001531{
1532 struct btrfs_path *path;
1533 struct btrfs_root_item *ri;
1534
1535 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1536 return 0;
1537 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1538 return 0;
1539
1540 path = btrfs_alloc_path();
1541 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001542
Josef Bacik58176a92007-08-29 15:47:34 -04001543 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1544 dentry->d_name.name,
1545 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001546 if (IS_ERR(*sub_root))
1547 return PTR_ERR(*sub_root);
1548
1549 ri = &(*sub_root)->root_item;
1550 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001551 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1552 location->offset = 0;
1553
1554 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001555 return 0;
1556}
1557
1558static int btrfs_init_locked_inode(struct inode *inode, void *p)
1559{
1560 struct btrfs_iget_args *args = p;
1561 inode->i_ino = args->ino;
1562 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001563 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001564 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1565 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001566 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001567 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1568 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04001569 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001570 return 0;
1571}
1572
1573static int btrfs_find_actor(struct inode *inode, void *opaque)
1574{
1575 struct btrfs_iget_args *args = opaque;
1576 return (args->ino == inode->i_ino &&
1577 args->root == BTRFS_I(inode)->root);
1578}
1579
Chris Masondc17ff82008-01-08 15:46:30 -05001580struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1581 u64 root_objectid)
1582{
1583 struct btrfs_iget_args args;
1584 args.ino = objectid;
1585 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1586
1587 if (!args.root)
1588 return NULL;
1589
1590 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1591}
1592
Chris Mason39279cc2007-06-12 06:35:45 -04001593struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1594 struct btrfs_root *root)
1595{
1596 struct inode *inode;
1597 struct btrfs_iget_args args;
1598 args.ino = objectid;
1599 args.root = root;
1600
1601 inode = iget5_locked(s, objectid, btrfs_find_actor,
1602 btrfs_init_locked_inode,
1603 (void *)&args);
1604 return inode;
1605}
1606
1607static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1608 struct nameidata *nd)
1609{
1610 struct inode * inode;
1611 struct btrfs_inode *bi = BTRFS_I(dir);
1612 struct btrfs_root *root = bi->root;
1613 struct btrfs_root *sub_root = root;
1614 struct btrfs_key location;
1615 int ret;
1616
1617 if (dentry->d_name.len > BTRFS_NAME_LEN)
1618 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001619
Chris Mason39279cc2007-06-12 06:35:45 -04001620 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001621
Chris Mason39279cc2007-06-12 06:35:45 -04001622 if (ret < 0)
1623 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001624
Chris Mason39279cc2007-06-12 06:35:45 -04001625 inode = NULL;
1626 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001627 ret = fixup_tree_root_location(root, &location, &sub_root,
1628 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001629 if (ret < 0)
1630 return ERR_PTR(ret);
1631 if (ret > 0)
1632 return ERR_PTR(-ENOENT);
1633 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1634 sub_root);
1635 if (!inode)
1636 return ERR_PTR(-EACCES);
1637 if (inode->i_state & I_NEW) {
1638 /* the inode and parent dir are two different roots */
1639 if (sub_root != root) {
1640 igrab(inode);
1641 sub_root->inode = inode;
1642 }
1643 BTRFS_I(inode)->root = sub_root;
1644 memcpy(&BTRFS_I(inode)->location, &location,
1645 sizeof(location));
1646 btrfs_read_locked_inode(inode);
1647 unlock_new_inode(inode);
1648 }
1649 }
1650 return d_splice_alias(inode, dentry);
1651}
1652
Chris Mason39279cc2007-06-12 06:35:45 -04001653static unsigned char btrfs_filetype_table[] = {
1654 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1655};
1656
1657static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1658{
Chris Mason6da6aba2007-12-18 16:15:09 -05001659 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001660 struct btrfs_root *root = BTRFS_I(inode)->root;
1661 struct btrfs_item *item;
1662 struct btrfs_dir_item *di;
1663 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001664 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001665 struct btrfs_path *path;
1666 int ret;
1667 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001668 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001669 int slot;
1670 int advance;
1671 unsigned char d_type;
1672 int over = 0;
1673 u32 di_cur;
1674 u32 di_total;
1675 u32 di_len;
1676 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001677 char tmp_name[32];
1678 char *name_ptr;
1679 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001680
1681 /* FIXME, use a real flag for deciding about the key type */
1682 if (root->fs_info->tree_root == root)
1683 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001684
Chris Mason39544012007-12-12 14:38:19 -05001685 /* special case for "." */
1686 if (filp->f_pos == 0) {
1687 over = filldir(dirent, ".", 1,
1688 1, inode->i_ino,
1689 DT_DIR);
1690 if (over)
1691 return 0;
1692 filp->f_pos = 1;
1693 }
1694
Chris Mason39279cc2007-06-12 06:35:45 -04001695 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001696 path = btrfs_alloc_path();
1697 path->reada = 2;
1698
1699 /* special case for .., just use the back ref */
1700 if (filp->f_pos == 1) {
1701 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1702 key.offset = 0;
1703 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1704 BUG_ON(ret == 0);
1705 leaf = path->nodes[0];
1706 slot = path->slots[0];
1707 nritems = btrfs_header_nritems(leaf);
1708 if (slot >= nritems) {
1709 btrfs_release_path(root, path);
1710 goto read_dir_items;
1711 }
1712 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1713 btrfs_release_path(root, path);
1714 if (found_key.objectid != key.objectid ||
1715 found_key.type != BTRFS_INODE_REF_KEY)
1716 goto read_dir_items;
1717 over = filldir(dirent, "..", 2,
1718 2, found_key.offset, DT_DIR);
1719 if (over)
1720 goto nopos;
1721 filp->f_pos = 2;
1722 }
1723
1724read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001725 btrfs_set_key_type(&key, key_type);
1726 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001727
Chris Mason39279cc2007-06-12 06:35:45 -04001728 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1729 if (ret < 0)
1730 goto err;
1731 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001732 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001733 leaf = path->nodes[0];
1734 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001735 slot = path->slots[0];
1736 if (advance || slot >= nritems) {
1737 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001738 ret = btrfs_next_leaf(root, path);
1739 if (ret)
1740 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001741 leaf = path->nodes[0];
1742 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001743 slot = path->slots[0];
1744 } else {
1745 slot++;
1746 path->slots[0]++;
1747 }
1748 }
1749 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001750 item = btrfs_item_nr(leaf, slot);
1751 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1752
1753 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001754 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001755 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001756 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001757 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001758 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001759
1760 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001761 advance = 1;
1762 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1763 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001764 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001765 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001766 struct btrfs_key location;
1767
1768 name_len = btrfs_dir_name_len(leaf, di);
1769 if (name_len < 32) {
1770 name_ptr = tmp_name;
1771 } else {
1772 name_ptr = kmalloc(name_len, GFP_NOFS);
1773 BUG_ON(!name_ptr);
1774 }
1775 read_extent_buffer(leaf, name_ptr,
1776 (unsigned long)(di + 1), name_len);
1777
1778 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1779 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001780 over = filldir(dirent, name_ptr, name_len,
1781 found_key.offset,
1782 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001783 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001784
1785 if (name_ptr != tmp_name)
1786 kfree(name_ptr);
1787
Chris Mason39279cc2007-06-12 06:35:45 -04001788 if (over)
1789 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001790 di_len = btrfs_dir_name_len(leaf, di) +
1791 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001792 di_cur += di_len;
1793 di = (struct btrfs_dir_item *)((char *)di + di_len);
1794 }
1795 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001796 if (key_type == BTRFS_DIR_INDEX_KEY)
1797 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1798 else
1799 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001800nopos:
1801 ret = 0;
1802err:
Chris Mason39279cc2007-06-12 06:35:45 -04001803 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001804 return ret;
1805}
1806
1807int btrfs_write_inode(struct inode *inode, int wait)
1808{
1809 struct btrfs_root *root = BTRFS_I(inode)->root;
1810 struct btrfs_trans_handle *trans;
1811 int ret = 0;
1812
1813 if (wait) {
Chris Mason39279cc2007-06-12 06:35:45 -04001814 trans = btrfs_start_transaction(root, 1);
1815 btrfs_set_trans_block_group(trans, inode);
1816 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001817 }
1818 return ret;
1819}
1820
1821/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001822 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001823 * inode changes. But, it is most likely to find the inode in cache.
1824 * FIXME, needs more benchmarking...there are no reasons other than performance
1825 * to keep or drop this code.
1826 */
1827void btrfs_dirty_inode(struct inode *inode)
1828{
1829 struct btrfs_root *root = BTRFS_I(inode)->root;
1830 struct btrfs_trans_handle *trans;
1831
Chris Mason39279cc2007-06-12 06:35:45 -04001832 trans = btrfs_start_transaction(root, 1);
1833 btrfs_set_trans_block_group(trans, inode);
1834 btrfs_update_inode(trans, root, inode);
1835 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001836}
1837
1838static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1839 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001840 const char *name, int name_len,
1841 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001842 u64 objectid,
1843 struct btrfs_block_group_cache *group,
1844 int mode)
1845{
1846 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001847 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001848 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001849 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001850 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001851 struct btrfs_inode_ref *ref;
1852 struct btrfs_key key[2];
1853 u32 sizes[2];
1854 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001855 int ret;
1856 int owner;
1857
Chris Mason5f39d392007-10-15 16:14:19 -04001858 path = btrfs_alloc_path();
1859 BUG_ON(!path);
1860
Chris Mason39279cc2007-06-12 06:35:45 -04001861 inode = new_inode(root->fs_info->sb);
1862 if (!inode)
1863 return ERR_PTR(-ENOMEM);
1864
Chris Masond1310b22008-01-24 16:13:08 -05001865 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1866 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001867 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001868 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1869 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04001870 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05001871 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001872 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001873
Chris Mason39279cc2007-06-12 06:35:45 -04001874 if (mode & S_IFDIR)
1875 owner = 0;
1876 else
1877 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001878 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001879 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001880 if (!new_inode_group) {
1881 printk("find_block group failed\n");
1882 new_inode_group = group;
1883 }
1884 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001885 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001886
1887 key[0].objectid = objectid;
1888 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1889 key[0].offset = 0;
1890
1891 key[1].objectid = objectid;
1892 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1893 key[1].offset = ref_objectid;
1894
1895 sizes[0] = sizeof(struct btrfs_inode_item);
1896 sizes[1] = name_len + sizeof(*ref);
1897
1898 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1899 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001900 goto fail;
1901
Chris Mason9c583092008-01-29 15:15:18 -05001902 if (objectid > root->highest_inode)
1903 root->highest_inode = objectid;
1904
Chris Mason39279cc2007-06-12 06:35:45 -04001905 inode->i_uid = current->fsuid;
1906 inode->i_gid = current->fsgid;
1907 inode->i_mode = mode;
1908 inode->i_ino = objectid;
1909 inode->i_blocks = 0;
1910 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001911 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1912 struct btrfs_inode_item);
1913 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001914
1915 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1916 struct btrfs_inode_ref);
1917 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1918 ptr = (unsigned long)(ref + 1);
1919 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1920
Chris Mason5f39d392007-10-15 16:14:19 -04001921 btrfs_mark_buffer_dirty(path->nodes[0]);
1922 btrfs_free_path(path);
1923
Chris Mason39279cc2007-06-12 06:35:45 -04001924 location = &BTRFS_I(inode)->location;
1925 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001926 location->offset = 0;
1927 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1928
Chris Mason39279cc2007-06-12 06:35:45 -04001929 insert_inode_hash(inode);
1930 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001931fail:
1932 btrfs_free_path(path);
1933 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001934}
1935
1936static inline u8 btrfs_inode_type(struct inode *inode)
1937{
1938 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1939}
1940
1941static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001942 struct dentry *dentry, struct inode *inode,
1943 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001944{
1945 int ret;
1946 struct btrfs_key key;
1947 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001948 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001949
Chris Mason39279cc2007-06-12 06:35:45 -04001950 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001951 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1952 key.offset = 0;
1953
1954 ret = btrfs_insert_dir_item(trans, root,
1955 dentry->d_name.name, dentry->d_name.len,
1956 dentry->d_parent->d_inode->i_ino,
1957 &key, btrfs_inode_type(inode));
1958 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001959 if (add_backref) {
1960 ret = btrfs_insert_inode_ref(trans, root,
1961 dentry->d_name.name,
1962 dentry->d_name.len,
1963 inode->i_ino,
1964 dentry->d_parent->d_inode->i_ino);
1965 }
Chris Mason79c44582007-06-25 10:09:33 -04001966 parent_inode = dentry->d_parent->d_inode;
1967 parent_inode->i_size += dentry->d_name.len * 2;
1968 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001969 ret = btrfs_update_inode(trans, root,
1970 dentry->d_parent->d_inode);
1971 }
1972 return ret;
1973}
1974
1975static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001976 struct dentry *dentry, struct inode *inode,
1977 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001978{
Chris Mason9c583092008-01-29 15:15:18 -05001979 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001980 if (!err) {
1981 d_instantiate(dentry, inode);
1982 return 0;
1983 }
1984 if (err > 0)
1985 err = -EEXIST;
1986 return err;
1987}
1988
Josef Bacik618e21d2007-07-11 10:18:17 -04001989static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1990 int mode, dev_t rdev)
1991{
1992 struct btrfs_trans_handle *trans;
1993 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001994 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001995 int err;
1996 int drop_inode = 0;
1997 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001998 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001999
2000 if (!new_valid_dev(rdev))
2001 return -EINVAL;
2002
Chris Mason1832a6d2007-12-21 16:27:21 -05002003 err = btrfs_check_free_space(root, 1, 0);
2004 if (err)
2005 goto fail;
2006
Josef Bacik618e21d2007-07-11 10:18:17 -04002007 trans = btrfs_start_transaction(root, 1);
2008 btrfs_set_trans_block_group(trans, dir);
2009
2010 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2011 if (err) {
2012 err = -ENOSPC;
2013 goto out_unlock;
2014 }
2015
Chris Mason9c583092008-01-29 15:15:18 -05002016 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2017 dentry->d_name.len,
2018 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002019 BTRFS_I(dir)->block_group, mode);
2020 err = PTR_ERR(inode);
2021 if (IS_ERR(inode))
2022 goto out_unlock;
2023
2024 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002025 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002026 if (err)
2027 drop_inode = 1;
2028 else {
2029 inode->i_op = &btrfs_special_inode_operations;
2030 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002031 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002032 }
2033 dir->i_sb->s_dirt = 1;
2034 btrfs_update_inode_block_group(trans, inode);
2035 btrfs_update_inode_block_group(trans, dir);
2036out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002037 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002038 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002039fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002040 if (drop_inode) {
2041 inode_dec_link_count(inode);
2042 iput(inode);
2043 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002044 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002045 return err;
2046}
2047
Chris Mason39279cc2007-06-12 06:35:45 -04002048static int btrfs_create(struct inode *dir, struct dentry *dentry,
2049 int mode, struct nameidata *nd)
2050{
2051 struct btrfs_trans_handle *trans;
2052 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002053 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002054 int err;
2055 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002056 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002057 u64 objectid;
2058
Chris Mason1832a6d2007-12-21 16:27:21 -05002059 err = btrfs_check_free_space(root, 1, 0);
2060 if (err)
2061 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002062 trans = btrfs_start_transaction(root, 1);
2063 btrfs_set_trans_block_group(trans, dir);
2064
2065 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2066 if (err) {
2067 err = -ENOSPC;
2068 goto out_unlock;
2069 }
2070
Chris Mason9c583092008-01-29 15:15:18 -05002071 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2072 dentry->d_name.len,
2073 dentry->d_parent->d_inode->i_ino,
2074 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002075 err = PTR_ERR(inode);
2076 if (IS_ERR(inode))
2077 goto out_unlock;
2078
2079 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002080 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002081 if (err)
2082 drop_inode = 1;
2083 else {
2084 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002085 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002086 inode->i_fop = &btrfs_file_operations;
2087 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002088 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2089 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002090 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002091 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2092 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002093 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002094 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002095 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002096 }
2097 dir->i_sb->s_dirt = 1;
2098 btrfs_update_inode_block_group(trans, inode);
2099 btrfs_update_inode_block_group(trans, dir);
2100out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002101 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002102 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002103fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002104 if (drop_inode) {
2105 inode_dec_link_count(inode);
2106 iput(inode);
2107 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002108 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002109 return err;
2110}
2111
2112static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2113 struct dentry *dentry)
2114{
2115 struct btrfs_trans_handle *trans;
2116 struct btrfs_root *root = BTRFS_I(dir)->root;
2117 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002118 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002119 int err;
2120 int drop_inode = 0;
2121
2122 if (inode->i_nlink == 0)
2123 return -ENOENT;
2124
Chris Mason6da6aba2007-12-18 16:15:09 -05002125#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2126 inode->i_nlink++;
2127#else
Chris Mason39279cc2007-06-12 06:35:45 -04002128 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002129#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002130 err = btrfs_check_free_space(root, 1, 0);
2131 if (err)
2132 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002133 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002134
Chris Mason39279cc2007-06-12 06:35:45 -04002135 btrfs_set_trans_block_group(trans, dir);
2136 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002137 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002138
Chris Mason39279cc2007-06-12 06:35:45 -04002139 if (err)
2140 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002141
Chris Mason39279cc2007-06-12 06:35:45 -04002142 dir->i_sb->s_dirt = 1;
2143 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002144 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002145
Chris Mason54aa1f42007-06-22 14:16:25 -04002146 if (err)
2147 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002148
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002149 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002150 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002151fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002152 if (drop_inode) {
2153 inode_dec_link_count(inode);
2154 iput(inode);
2155 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002156 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002157 return err;
2158}
2159
Chris Mason39279cc2007-06-12 06:35:45 -04002160static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2161{
Chris Masonb9d86662008-05-02 16:13:49 -04002162 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002163 struct btrfs_trans_handle *trans;
2164 struct btrfs_root *root = BTRFS_I(dir)->root;
2165 int err = 0;
2166 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002167 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002168 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002169
Chris Mason1832a6d2007-12-21 16:27:21 -05002170 err = btrfs_check_free_space(root, 1, 0);
2171 if (err)
2172 goto out_unlock;
2173
Chris Mason39279cc2007-06-12 06:35:45 -04002174 trans = btrfs_start_transaction(root, 1);
2175 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002176
Chris Mason39279cc2007-06-12 06:35:45 -04002177 if (IS_ERR(trans)) {
2178 err = PTR_ERR(trans);
2179 goto out_unlock;
2180 }
2181
2182 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2183 if (err) {
2184 err = -ENOSPC;
2185 goto out_unlock;
2186 }
2187
Chris Mason9c583092008-01-29 15:15:18 -05002188 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2189 dentry->d_name.len,
2190 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002191 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2192 if (IS_ERR(inode)) {
2193 err = PTR_ERR(inode);
2194 goto out_fail;
2195 }
Chris Mason5f39d392007-10-15 16:14:19 -04002196
Chris Mason39279cc2007-06-12 06:35:45 -04002197 drop_on_err = 1;
2198 inode->i_op = &btrfs_dir_inode_operations;
2199 inode->i_fop = &btrfs_dir_file_operations;
2200 btrfs_set_trans_block_group(trans, inode);
2201
Chris Mason39544012007-12-12 14:38:19 -05002202 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002203 err = btrfs_update_inode(trans, root, inode);
2204 if (err)
2205 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002206
Chris Mason9c583092008-01-29 15:15:18 -05002207 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002208 if (err)
2209 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002210
Chris Mason39279cc2007-06-12 06:35:45 -04002211 d_instantiate(dentry, inode);
2212 drop_on_err = 0;
2213 dir->i_sb->s_dirt = 1;
2214 btrfs_update_inode_block_group(trans, inode);
2215 btrfs_update_inode_block_group(trans, dir);
2216
2217out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002218 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002219 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002220
Chris Mason39279cc2007-06-12 06:35:45 -04002221out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002222 if (drop_on_err)
2223 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002224 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002225 return err;
2226}
2227
Chris Mason3b951512008-04-17 11:29:12 -04002228static int merge_extent_mapping(struct extent_map_tree *em_tree,
2229 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002230 struct extent_map *em,
2231 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002232{
2233 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002234
Chris Masone6dcd2d2008-07-17 12:53:50 -04002235 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2236 start_diff = map_start - em->start;
2237 em->start = map_start;
2238 em->len = map_len;
2239 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2240 em->block_start += start_diff;
2241 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002242}
2243
Chris Masona52d9a82007-08-27 16:49:44 -04002244struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002245 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002246 int create)
2247{
2248 int ret;
2249 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002250 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002251 u64 extent_start = 0;
2252 u64 extent_end = 0;
2253 u64 objectid = inode->i_ino;
2254 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002255 struct btrfs_path *path;
2256 struct btrfs_root *root = BTRFS_I(inode)->root;
2257 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002258 struct extent_buffer *leaf;
2259 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002260 struct extent_map *em = NULL;
2261 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002262 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002263 struct btrfs_trans_handle *trans = NULL;
2264
2265 path = btrfs_alloc_path();
2266 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002267
2268again:
Chris Masond1310b22008-01-24 16:13:08 -05002269 spin_lock(&em_tree->lock);
2270 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002271 if (em)
2272 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002273 spin_unlock(&em_tree->lock);
2274
Chris Masona52d9a82007-08-27 16:49:44 -04002275 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002276 if (em->start > start || em->start + em->len <= start)
2277 free_extent_map(em);
2278 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002279 free_extent_map(em);
2280 else
2281 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002282 }
Chris Masond1310b22008-01-24 16:13:08 -05002283 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002284 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002285 err = -ENOMEM;
2286 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002287 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002288 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002289 em->start = EXTENT_MAP_HOLE;
2290 em->len = (u64)-1;
Chris Mason179e29e2007-11-01 11:28:41 -04002291 ret = btrfs_lookup_file_extent(trans, root, path,
2292 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002293 if (ret < 0) {
2294 err = ret;
2295 goto out;
2296 }
2297
2298 if (ret != 0) {
2299 if (path->slots[0] == 0)
2300 goto not_found;
2301 path->slots[0]--;
2302 }
2303
Chris Mason5f39d392007-10-15 16:14:19 -04002304 leaf = path->nodes[0];
2305 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002306 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002307 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002308 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2309 found_type = btrfs_key_type(&found_key);
2310 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002311 found_type != BTRFS_EXTENT_DATA_KEY) {
2312 goto not_found;
2313 }
2314
Chris Mason5f39d392007-10-15 16:14:19 -04002315 found_type = btrfs_file_extent_type(leaf, item);
2316 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002317 if (found_type == BTRFS_FILE_EXTENT_REG) {
2318 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002319 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002320 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002321 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002322 em->start = start;
2323 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002324 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002325 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002326 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002327 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002328 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002329 }
2330 goto not_found_em;
2331 }
Chris Masondb945352007-10-15 16:15:53 -04002332 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2333 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002334 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002335 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002336 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002337 goto insert;
2338 }
Chris Masondb945352007-10-15 16:15:53 -04002339 bytenr += btrfs_file_extent_offset(leaf, item);
2340 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002341 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002342 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002343 goto insert;
2344 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002345 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002346 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002347 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002348 size_t size;
2349 size_t extent_offset;
2350 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002351
Chris Mason5f39d392007-10-15 16:14:19 -04002352 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2353 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002354 extent_end = (extent_start + size + root->sectorsize - 1) &
2355 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002356 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002357 em->start = start;
2358 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002359 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002360 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002361 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002362 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002363 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002364 }
2365 goto not_found_em;
2366 }
2367 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002368
2369 if (!page) {
2370 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002371 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002372 goto out;
2373 }
2374
Chris Mason70dec802008-01-29 09:59:12 -05002375 page_start = page_offset(page) + pg_offset;
2376 extent_offset = page_start - extent_start;
2377 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002378 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002379 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002380 em->len = (copy_size + root->sectorsize - 1) &
2381 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002382 map = kmap(page);
2383 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002384 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002385 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002386 copy_size);
2387 flush_dcache_page(page);
2388 } else if (create && PageUptodate(page)) {
2389 if (!trans) {
2390 kunmap(page);
2391 free_extent_map(em);
2392 em = NULL;
2393 btrfs_release_path(root, path);
2394 trans = btrfs_start_transaction(root, 1);
2395 goto again;
2396 }
Chris Mason70dec802008-01-29 09:59:12 -05002397 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002398 copy_size);
2399 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002400 }
Chris Masona52d9a82007-08-27 16:49:44 -04002401 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002402 set_extent_uptodate(io_tree, em->start,
2403 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002404 goto insert;
2405 } else {
2406 printk("unkknown found_type %d\n", found_type);
2407 WARN_ON(1);
2408 }
2409not_found:
2410 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002411 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002412not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002413 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002414insert:
2415 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002416 if (em->start > start || extent_map_end(em) <= start) {
2417 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002418 err = -EIO;
2419 goto out;
2420 }
Chris Masond1310b22008-01-24 16:13:08 -05002421
2422 err = 0;
2423 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002424 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002425 /* it is possible that someone inserted the extent into the tree
2426 * while we had the lock dropped. It is also possible that
2427 * an overlapping map exists in the tree
2428 */
Chris Masona52d9a82007-08-27 16:49:44 -04002429 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002430 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002431
2432 ret = 0;
2433
Chris Mason3b951512008-04-17 11:29:12 -04002434 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002435 if (existing && (existing->start > start ||
2436 existing->start + existing->len <= start)) {
2437 free_extent_map(existing);
2438 existing = NULL;
2439 }
Chris Mason3b951512008-04-17 11:29:12 -04002440 if (!existing) {
2441 existing = lookup_extent_mapping(em_tree, em->start,
2442 em->len);
2443 if (existing) {
2444 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002445 em, start,
2446 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002447 free_extent_map(existing);
2448 if (err) {
2449 free_extent_map(em);
2450 em = NULL;
2451 }
2452 } else {
2453 err = -EIO;
2454 printk("failing to insert %Lu %Lu\n",
2455 start, len);
2456 free_extent_map(em);
2457 em = NULL;
2458 }
2459 } else {
2460 free_extent_map(em);
2461 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002462 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002463 }
Chris Masona52d9a82007-08-27 16:49:44 -04002464 }
Chris Masond1310b22008-01-24 16:13:08 -05002465 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002466out:
2467 btrfs_free_path(path);
2468 if (trans) {
2469 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002470 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002471 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002472 }
Chris Masona52d9a82007-08-27 16:49:44 -04002473 }
Chris Masona52d9a82007-08-27 16:49:44 -04002474 if (err) {
2475 free_extent_map(em);
2476 WARN_ON(1);
2477 return ERR_PTR(err);
2478 }
2479 return em;
2480}
2481
Chris Masone1c4b742008-04-22 13:26:46 -04002482#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002483static int btrfs_get_block(struct inode *inode, sector_t iblock,
2484 struct buffer_head *bh_result, int create)
2485{
2486 struct extent_map *em;
2487 u64 start = (u64)iblock << inode->i_blkbits;
2488 struct btrfs_multi_bio *multi = NULL;
2489 struct btrfs_root *root = BTRFS_I(inode)->root;
2490 u64 len;
2491 u64 logical;
2492 u64 map_length;
2493 int ret = 0;
2494
2495 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2496
2497 if (!em || IS_ERR(em))
2498 goto out;
2499
Chris Masone1c4b742008-04-22 13:26:46 -04002500 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002501 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002502 }
Chris Mason16432982008-04-10 10:23:21 -04002503
2504 if (em->block_start == EXTENT_MAP_INLINE) {
2505 ret = -EINVAL;
2506 goto out;
2507 }
2508
Chris Mason16432982008-04-10 10:23:21 -04002509 len = em->start + em->len - start;
2510 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2511
Chris Masone1c4b742008-04-22 13:26:46 -04002512 if (em->block_start == EXTENT_MAP_HOLE ||
2513 em->block_start == EXTENT_MAP_DELALLOC) {
2514 bh_result->b_size = len;
2515 goto out;
2516 }
2517
Chris Mason16432982008-04-10 10:23:21 -04002518 logical = start - em->start;
2519 logical = em->block_start + logical;
2520
2521 map_length = len;
2522 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2523 logical, &map_length, &multi, 0);
2524 BUG_ON(ret);
2525 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2526 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002527
Chris Mason16432982008-04-10 10:23:21 -04002528 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2529 set_buffer_mapped(bh_result);
2530 kfree(multi);
2531out:
2532 free_extent_map(em);
2533 return ret;
2534}
Chris Masone1c4b742008-04-22 13:26:46 -04002535#endif
Chris Mason16432982008-04-10 10:23:21 -04002536
2537static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2538 const struct iovec *iov, loff_t offset,
2539 unsigned long nr_segs)
2540{
Chris Masone1c4b742008-04-22 13:26:46 -04002541 return -EINVAL;
2542#if 0
Chris Mason16432982008-04-10 10:23:21 -04002543 struct file *file = iocb->ki_filp;
2544 struct inode *inode = file->f_mapping->host;
2545
2546 if (rw == WRITE)
2547 return -EINVAL;
2548
2549 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2550 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002551#endif
Chris Mason16432982008-04-10 10:23:21 -04002552}
2553
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002554static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002555{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002556 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002557}
2558
Chris Mason9ebefb182007-06-15 13:50:00 -04002559int btrfs_readpage(struct file *file, struct page *page)
2560{
Chris Masond1310b22008-01-24 16:13:08 -05002561 struct extent_io_tree *tree;
2562 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002563 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002564}
Chris Mason1832a6d2007-12-21 16:27:21 -05002565
Chris Mason39279cc2007-06-12 06:35:45 -04002566static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2567{
Chris Masond1310b22008-01-24 16:13:08 -05002568 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002569
2570
2571 if (current->flags & PF_MEMALLOC) {
2572 redirty_page_for_writepage(wbc, page);
2573 unlock_page(page);
2574 return 0;
2575 }
Chris Masond1310b22008-01-24 16:13:08 -05002576 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002577 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2578}
Chris Mason39279cc2007-06-12 06:35:45 -04002579
Chris Masonb293f022007-11-01 19:45:34 -04002580static int btrfs_writepages(struct address_space *mapping,
2581 struct writeback_control *wbc)
2582{
Chris Masond1310b22008-01-24 16:13:08 -05002583 struct extent_io_tree *tree;
2584 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002585 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2586}
2587
Chris Mason3ab2fb52007-11-08 10:59:22 -05002588static int
2589btrfs_readpages(struct file *file, struct address_space *mapping,
2590 struct list_head *pages, unsigned nr_pages)
2591{
Chris Masond1310b22008-01-24 16:13:08 -05002592 struct extent_io_tree *tree;
2593 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002594 return extent_readpages(tree, mapping, pages, nr_pages,
2595 btrfs_get_extent);
2596}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002597static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002598{
Chris Masond1310b22008-01-24 16:13:08 -05002599 struct extent_io_tree *tree;
2600 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002601 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002602
Chris Masond1310b22008-01-24 16:13:08 -05002603 tree = &BTRFS_I(page->mapping->host)->io_tree;
2604 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002605 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002606 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002607 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002608 ClearPagePrivate(page);
2609 set_page_private(page, 0);
2610 page_cache_release(page);
2611 }
2612 return ret;
2613}
Chris Mason39279cc2007-06-12 06:35:45 -04002614
Chris Masone6dcd2d2008-07-17 12:53:50 -04002615static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2616{
2617 struct btrfs_ordered_extent *ordered;
2618
2619 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2620 page_offset(page));
2621 if (ordered) {
2622 btrfs_put_ordered_extent(ordered);
2623 return 0;
2624 }
2625 return __btrfs_releasepage(page, gfp_flags);
2626}
2627
Chris Masona52d9a82007-08-27 16:49:44 -04002628static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2629{
Chris Masond1310b22008-01-24 16:13:08 -05002630 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002631 struct btrfs_ordered_extent *ordered;
2632 u64 page_start = page_offset(page);
2633 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002634
Chris Masone6dcd2d2008-07-17 12:53:50 -04002635 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002636 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002637 if (offset) {
2638 btrfs_releasepage(page, GFP_NOFS);
2639 return;
2640 }
2641
2642 lock_extent(tree, page_start, page_end, GFP_NOFS);
2643 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2644 page_offset(page));
2645 if (ordered) {
2646 clear_extent_bit(tree, page_start, page_end,
2647 EXTENT_DIRTY | EXTENT_DELALLOC |
2648 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2649 btrfs_writepage_end_io_hook(page, page_start,
2650 page_end, NULL, 1);
2651 btrfs_put_ordered_extent(ordered);
2652 lock_extent(tree, page_start, page_end, GFP_NOFS);
2653 }
2654 clear_extent_bit(tree, page_start, page_end,
2655 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2656 EXTENT_ORDERED,
2657 1, 1, GFP_NOFS);
2658 __btrfs_releasepage(page, GFP_NOFS);
2659
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002660 if (PagePrivate(page)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002661 invalidate_extent_lru(tree, page_offset(page),
2662 PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002663 ClearPagePrivate(page);
2664 set_page_private(page, 0);
2665 page_cache_release(page);
2666 }
Chris Mason39279cc2007-06-12 06:35:45 -04002667}
2668
Chris Mason9ebefb182007-06-15 13:50:00 -04002669/*
2670 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2671 * called from a page fault handler when a page is first dirtied. Hence we must
2672 * be careful to check for EOF conditions here. We set the page up correctly
2673 * for a written page which means we get ENOSPC checking when writing into
2674 * holes and correct delalloc and unwritten extent mapping on filesystems that
2675 * support these features.
2676 *
2677 * We are not allowed to take the i_mutex here so we have to play games to
2678 * protect against truncate races as the page could now be beyond EOF. Because
2679 * vmtruncate() writes the inode size before removing pages, once we have the
2680 * page lock we can determine safely if the page is beyond EOF. If it is not
2681 * beyond EOF, then the page is guaranteed safe against truncation until we
2682 * unlock the page.
2683 */
2684int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2685{
Chris Mason6da6aba2007-12-18 16:15:09 -05002686 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002687 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002688 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2689 struct btrfs_ordered_extent *ordered;
2690 char *kaddr;
2691 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002692 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002693 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002694 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002695 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002696
Chris Mason1832a6d2007-12-21 16:27:21 -05002697 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002698 if (ret)
2699 goto out;
2700
2701 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002702again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002703 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002704 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002705 page_start = page_offset(page);
2706 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002707
Chris Mason9ebefb182007-06-15 13:50:00 -04002708 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002709 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002710 /* page got truncated out from underneath us */
2711 goto out_unlock;
2712 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002713 wait_on_page_writeback(page);
2714
2715 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2716 set_page_extent_mapped(page);
2717
2718 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2719 if (ordered) {
2720 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2721 unlock_page(page);
2722 btrfs_wait_ordered_extent(inode, ordered);
2723 btrfs_put_ordered_extent(ordered);
2724 goto again;
2725 }
2726
2727 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2728 page_end, GFP_NOFS);
2729 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002730
2731 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002732 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002733 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002734 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002735 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002736
Chris Masone6dcd2d2008-07-17 12:53:50 -04002737 if (zero_start != PAGE_CACHE_SIZE) {
2738 kaddr = kmap(page);
2739 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2740 flush_dcache_page(page);
2741 kunmap(page);
2742 }
Chris Mason247e7432008-07-17 12:53:51 -04002743 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002744 set_page_dirty(page);
2745 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002746
2747out_unlock:
2748 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002749out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002750 return ret;
2751}
2752
Chris Mason39279cc2007-06-12 06:35:45 -04002753static void btrfs_truncate(struct inode *inode)
2754{
2755 struct btrfs_root *root = BTRFS_I(inode)->root;
2756 int ret;
2757 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002758 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002759
2760 if (!S_ISREG(inode->i_mode))
2761 return;
2762 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2763 return;
2764
2765 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2766
Chris Mason39279cc2007-06-12 06:35:45 -04002767 trans = btrfs_start_transaction(root, 1);
2768 btrfs_set_trans_block_group(trans, inode);
2769
2770 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002771 ret = btrfs_truncate_in_trans(trans, root, inode,
2772 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002773 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002774 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002775
Chris Mason89ce8a62008-06-25 16:01:31 -04002776 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002777 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002778 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002779}
2780
Sven Wegener3b963622008-06-09 21:57:42 -04002781/*
2782 * Invalidate a single dcache entry at the root of the filesystem.
2783 * Needed after creation of snapshot or subvolume.
2784 */
2785void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2786 int namelen)
2787{
2788 struct dentry *alias, *entry;
2789 struct qstr qstr;
2790
2791 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2792 if (alias) {
2793 qstr.name = name;
2794 qstr.len = namelen;
2795 /* change me if btrfs ever gets a d_hash operation */
2796 qstr.hash = full_name_hash(qstr.name, qstr.len);
2797 entry = d_lookup(alias, &qstr);
2798 dput(alias);
2799 if (entry) {
2800 d_invalidate(entry);
2801 dput(entry);
2802 }
2803 }
2804}
2805
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002806int btrfs_create_subvol_root(struct btrfs_root *new_root,
2807 struct btrfs_trans_handle *trans, u64 new_dirid,
2808 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002809{
Chris Mason39279cc2007-06-12 06:35:45 -04002810 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002811 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002812
Chris Mason9c583092008-01-29 15:15:18 -05002813 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002814 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002815 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002816 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002817 inode->i_op = &btrfs_dir_inode_operations;
2818 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002819 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002820
Chris Mason39544012007-12-12 14:38:19 -05002821 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2822 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002823 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002824 inode->i_size = 0;
Sven Wegener3b963622008-06-09 21:57:42 -04002825
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002826 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002827}
2828
Chris Masonedbd8d42007-12-21 16:27:24 -05002829unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002830 struct file_ra_state *ra, struct file *file,
2831 pgoff_t offset, pgoff_t last_index)
2832{
Chris Mason8e7bf942008-04-28 09:02:36 -04002833 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002834
2835#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002836 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2837 return offset;
2838#else
Chris Mason86479a02007-09-10 19:58:16 -04002839 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2840 return offset + req_size;
2841#endif
2842}
2843
Chris Mason39279cc2007-06-12 06:35:45 -04002844struct inode *btrfs_alloc_inode(struct super_block *sb)
2845{
2846 struct btrfs_inode *ei;
2847
2848 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2849 if (!ei)
2850 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002851 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002852 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002853 return &ei->vfs_inode;
2854}
2855
2856void btrfs_destroy_inode(struct inode *inode)
2857{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002858 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04002859 WARN_ON(!list_empty(&inode->i_dentry));
2860 WARN_ON(inode->i_data.nrpages);
2861
Chris Masone6dcd2d2008-07-17 12:53:50 -04002862 while(1) {
2863 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2864 if (!ordered)
2865 break;
2866 else {
2867 printk("found ordered extent %Lu %Lu\n",
2868 ordered->file_offset, ordered->len);
2869 btrfs_remove_ordered_extent(inode, ordered);
2870 btrfs_put_ordered_extent(ordered);
2871 btrfs_put_ordered_extent(ordered);
2872 }
2873 }
Chris Mason8c416c92008-01-14 15:10:26 -05002874 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002875 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2876}
2877
Chris Mason44ec0b72007-10-29 10:55:05 -04002878#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2879static void init_once(struct kmem_cache * cachep, void *foo)
2880#else
Chris Mason39279cc2007-06-12 06:35:45 -04002881static void init_once(void * foo, struct kmem_cache * cachep,
2882 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002883#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002884{
2885 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2886
2887 inode_init_once(&ei->vfs_inode);
2888}
2889
2890void btrfs_destroy_cachep(void)
2891{
2892 if (btrfs_inode_cachep)
2893 kmem_cache_destroy(btrfs_inode_cachep);
2894 if (btrfs_trans_handle_cachep)
2895 kmem_cache_destroy(btrfs_trans_handle_cachep);
2896 if (btrfs_transaction_cachep)
2897 kmem_cache_destroy(btrfs_transaction_cachep);
2898 if (btrfs_bit_radix_cachep)
2899 kmem_cache_destroy(btrfs_bit_radix_cachep);
2900 if (btrfs_path_cachep)
2901 kmem_cache_destroy(btrfs_path_cachep);
2902}
2903
Chris Mason86479a02007-09-10 19:58:16 -04002904struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002905 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002906#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2907 void (*ctor)(struct kmem_cache *, void *)
2908#else
Chris Mason92fee662007-07-25 12:31:35 -04002909 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002910 unsigned long)
2911#endif
2912 )
Chris Mason92fee662007-07-25 12:31:35 -04002913{
2914 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2915 SLAB_MEM_SPREAD | extra_flags), ctor
2916#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2917 ,NULL
2918#endif
2919 );
2920}
2921
Chris Mason39279cc2007-06-12 06:35:45 -04002922int btrfs_init_cachep(void)
2923{
Chris Mason86479a02007-09-10 19:58:16 -04002924 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002925 sizeof(struct btrfs_inode),
2926 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002927 if (!btrfs_inode_cachep)
2928 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002929 btrfs_trans_handle_cachep =
2930 btrfs_cache_create("btrfs_trans_handle_cache",
2931 sizeof(struct btrfs_trans_handle),
2932 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002933 if (!btrfs_trans_handle_cachep)
2934 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002935 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002936 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002937 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002938 if (!btrfs_transaction_cachep)
2939 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002940 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002941 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002942 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002943 if (!btrfs_path_cachep)
2944 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002945 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002946 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002947 if (!btrfs_bit_radix_cachep)
2948 goto fail;
2949 return 0;
2950fail:
2951 btrfs_destroy_cachep();
2952 return -ENOMEM;
2953}
2954
2955static int btrfs_getattr(struct vfsmount *mnt,
2956 struct dentry *dentry, struct kstat *stat)
2957{
2958 struct inode *inode = dentry->d_inode;
2959 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002960 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002961 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002962 return 0;
2963}
2964
2965static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2966 struct inode * new_dir,struct dentry *new_dentry)
2967{
2968 struct btrfs_trans_handle *trans;
2969 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2970 struct inode *new_inode = new_dentry->d_inode;
2971 struct inode *old_inode = old_dentry->d_inode;
2972 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002973 int ret;
2974
2975 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2976 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2977 return -ENOTEMPTY;
2978 }
Chris Mason5f39d392007-10-15 16:14:19 -04002979
Chris Mason1832a6d2007-12-21 16:27:21 -05002980 ret = btrfs_check_free_space(root, 1, 0);
2981 if (ret)
2982 goto out_unlock;
2983
Chris Mason39279cc2007-06-12 06:35:45 -04002984 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002985
Chris Mason39279cc2007-06-12 06:35:45 -04002986 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002987
2988 old_dentry->d_inode->i_nlink++;
2989 old_dir->i_ctime = old_dir->i_mtime = ctime;
2990 new_dir->i_ctime = new_dir->i_mtime = ctime;
2991 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002992
Chris Mason39279cc2007-06-12 06:35:45 -04002993 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2994 if (ret)
2995 goto out_fail;
2996
2997 if (new_inode) {
2998 new_inode->i_ctime = CURRENT_TIME;
2999 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3000 if (ret)
3001 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003002 }
Chris Mason9c583092008-01-29 15:15:18 -05003003 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003004 if (ret)
3005 goto out_fail;
3006
3007out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003008 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003009out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003010 return ret;
3011}
3012
3013static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3014 const char *symname)
3015{
3016 struct btrfs_trans_handle *trans;
3017 struct btrfs_root *root = BTRFS_I(dir)->root;
3018 struct btrfs_path *path;
3019 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003020 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003021 int err;
3022 int drop_inode = 0;
3023 u64 objectid;
3024 int name_len;
3025 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003026 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003027 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003028 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003029 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003030
3031 name_len = strlen(symname) + 1;
3032 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3033 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003034
Chris Mason1832a6d2007-12-21 16:27:21 -05003035 err = btrfs_check_free_space(root, 1, 0);
3036 if (err)
3037 goto out_fail;
3038
Chris Mason39279cc2007-06-12 06:35:45 -04003039 trans = btrfs_start_transaction(root, 1);
3040 btrfs_set_trans_block_group(trans, dir);
3041
3042 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3043 if (err) {
3044 err = -ENOSPC;
3045 goto out_unlock;
3046 }
3047
Chris Mason9c583092008-01-29 15:15:18 -05003048 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3049 dentry->d_name.len,
3050 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003051 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3052 err = PTR_ERR(inode);
3053 if (IS_ERR(inode))
3054 goto out_unlock;
3055
3056 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003057 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003058 if (err)
3059 drop_inode = 1;
3060 else {
3061 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003062 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003063 inode->i_fop = &btrfs_file_operations;
3064 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003065 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3066 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003067 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003068 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3069 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003070 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003071 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003072 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003073 }
3074 dir->i_sb->s_dirt = 1;
3075 btrfs_update_inode_block_group(trans, inode);
3076 btrfs_update_inode_block_group(trans, dir);
3077 if (drop_inode)
3078 goto out_unlock;
3079
3080 path = btrfs_alloc_path();
3081 BUG_ON(!path);
3082 key.objectid = inode->i_ino;
3083 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003084 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3085 datasize = btrfs_file_extent_calc_inline_size(name_len);
3086 err = btrfs_insert_empty_item(trans, root, path, &key,
3087 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003088 if (err) {
3089 drop_inode = 1;
3090 goto out_unlock;
3091 }
Chris Mason5f39d392007-10-15 16:14:19 -04003092 leaf = path->nodes[0];
3093 ei = btrfs_item_ptr(leaf, path->slots[0],
3094 struct btrfs_file_extent_item);
3095 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3096 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003097 BTRFS_FILE_EXTENT_INLINE);
3098 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003099 write_extent_buffer(leaf, symname, ptr, name_len);
3100 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003101 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003102
Chris Mason39279cc2007-06-12 06:35:45 -04003103 inode->i_op = &btrfs_symlink_inode_operations;
3104 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003105 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003106 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003107 err = btrfs_update_inode(trans, root, inode);
3108 if (err)
3109 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003110
3111out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003112 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003113 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003114out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003115 if (drop_inode) {
3116 inode_dec_link_count(inode);
3117 iput(inode);
3118 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003119 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003120 return err;
3121}
Chris Mason16432982008-04-10 10:23:21 -04003122
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123static int btrfs_set_page_dirty(struct page *page)
3124{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003125 return __set_page_dirty_nobuffers(page);
3126}
3127
Yanfdebe2b2008-01-14 13:26:08 -05003128static int btrfs_permission(struct inode *inode, int mask,
3129 struct nameidata *nd)
3130{
3131 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3132 return -EACCES;
3133 return generic_permission(inode, mask, NULL);
3134}
Chris Mason39279cc2007-06-12 06:35:45 -04003135
3136static struct inode_operations btrfs_dir_inode_operations = {
3137 .lookup = btrfs_lookup,
3138 .create = btrfs_create,
3139 .unlink = btrfs_unlink,
3140 .link = btrfs_link,
3141 .mkdir = btrfs_mkdir,
3142 .rmdir = btrfs_rmdir,
3143 .rename = btrfs_rename,
3144 .symlink = btrfs_symlink,
3145 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003146 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003147 .setxattr = generic_setxattr,
3148 .getxattr = generic_getxattr,
3149 .listxattr = btrfs_listxattr,
3150 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003151 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003152};
Chris Mason39279cc2007-06-12 06:35:45 -04003153static struct inode_operations btrfs_dir_ro_inode_operations = {
3154 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003155 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003156};
Chris Mason39279cc2007-06-12 06:35:45 -04003157static struct file_operations btrfs_dir_file_operations = {
3158 .llseek = generic_file_llseek,
3159 .read = generic_read_dir,
3160 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003161 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003162#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003163 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003164#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003165 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003166};
3167
Chris Masond1310b22008-01-24 16:13:08 -05003168static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003169 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003170 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003171 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003172 .readpage_io_hook = btrfs_readpage_io_hook,
3173 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003174 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003175 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003176 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003177 .set_bit_hook = btrfs_set_bit_hook,
3178 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003179};
3180
Chris Mason39279cc2007-06-12 06:35:45 -04003181static struct address_space_operations btrfs_aops = {
3182 .readpage = btrfs_readpage,
3183 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003184 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003185 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003186 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003187 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003188 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003189 .invalidatepage = btrfs_invalidatepage,
3190 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003191 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003192};
3193
3194static struct address_space_operations btrfs_symlink_aops = {
3195 .readpage = btrfs_readpage,
3196 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003197 .invalidatepage = btrfs_invalidatepage,
3198 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003199};
3200
3201static struct inode_operations btrfs_file_inode_operations = {
3202 .truncate = btrfs_truncate,
3203 .getattr = btrfs_getattr,
3204 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003205 .setxattr = generic_setxattr,
3206 .getxattr = generic_getxattr,
3207 .listxattr = btrfs_listxattr,
3208 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003209 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003210};
Josef Bacik618e21d2007-07-11 10:18:17 -04003211static struct inode_operations btrfs_special_inode_operations = {
3212 .getattr = btrfs_getattr,
3213 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003214 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003215};
Chris Mason39279cc2007-06-12 06:35:45 -04003216static struct inode_operations btrfs_symlink_inode_operations = {
3217 .readlink = generic_readlink,
3218 .follow_link = page_follow_link_light,
3219 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003220 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003221};