Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 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 Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 19 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <linux/backing-dev.h> |
| 29 | #include <linux/mpage.h> |
| 30 | #include <linux/swap.h> |
| 31 | #include <linux/writeback.h> |
| 32 | #include <linux/statfs.h> |
| 33 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 34 | #include <linux/bit_spinlock.h> |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 35 | #include <linux/version.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | #include <linux/xattr.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 37 | #include "ctree.h" |
| 38 | #include "disk-io.h" |
| 39 | #include "transaction.h" |
| 40 | #include "btrfs_inode.h" |
| 41 | #include "ioctl.h" |
| 42 | #include "print-tree.h" |
| 43 | |
| 44 | struct btrfs_iget_args { |
| 45 | u64 ino; |
| 46 | struct btrfs_root *root; |
| 47 | }; |
| 48 | |
| 49 | static struct inode_operations btrfs_dir_inode_operations; |
| 50 | static struct inode_operations btrfs_symlink_inode_operations; |
| 51 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 52 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 53 | static struct inode_operations btrfs_file_inode_operations; |
| 54 | static struct address_space_operations btrfs_aops; |
| 55 | static struct address_space_operations btrfs_symlink_aops; |
| 56 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 57 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 58 | |
| 59 | static struct kmem_cache *btrfs_inode_cachep; |
| 60 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 61 | struct kmem_cache *btrfs_transaction_cachep; |
| 62 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 63 | struct kmem_cache *btrfs_path_cachep; |
| 64 | |
| 65 | #define S_SHIFT 12 |
| 66 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 67 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 68 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 69 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 70 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 71 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 72 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 73 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 74 | }; |
| 75 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 76 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
| 77 | int for_del) |
| 78 | { |
| 79 | u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 80 | u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy); |
| 81 | u64 thresh; |
| 82 | int ret = 0; |
| 83 | |
| 84 | if (for_del) |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 85 | thresh = total * 90; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 86 | else |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 87 | thresh = total * 85; |
| 88 | |
| 89 | do_div(thresh, 100); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 90 | |
| 91 | spin_lock(&root->fs_info->delalloc_lock); |
| 92 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) |
| 93 | ret = -ENOSPC; |
| 94 | spin_unlock(&root->fs_info->delalloc_lock); |
| 95 | return ret; |
| 96 | } |
| 97 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 98 | static int cow_file_range(struct inode *inode, u64 start, u64 end) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 99 | { |
| 100 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 101 | struct btrfs_trans_handle *trans; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 102 | u64 alloc_hint = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 103 | u64 num_bytes; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 104 | u64 cur_alloc_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 105 | u64 blocksize = root->sectorsize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 106 | u64 orig_start = start; |
| 107 | u64 orig_num_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 108 | struct btrfs_key ins; |
| 109 | int ret; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 110 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 111 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 112 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 113 | btrfs_set_trans_block_group(trans, inode); |
| 114 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 115 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 116 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 117 | ret = btrfs_drop_extents(trans, root, inode, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 118 | start, start + num_bytes, start, &alloc_hint); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 119 | orig_num_bytes = num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 120 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 121 | if (alloc_hint == EXTENT_MAP_INLINE) |
| 122 | goto out; |
| 123 | |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 124 | while(num_bytes > 0) { |
| 125 | cur_alloc_size = min(num_bytes, root->fs_info->max_extent); |
| 126 | ret = btrfs_alloc_extent(trans, root, cur_alloc_size, |
| 127 | root->root_key.objectid, |
| 128 | trans->transid, |
| 129 | inode->i_ino, start, 0, |
| 130 | alloc_hint, (u64)-1, &ins, 1); |
| 131 | if (ret) { |
| 132 | WARN_ON(1); |
| 133 | goto out; |
| 134 | } |
| 135 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 136 | start, ins.objectid, ins.offset, |
| 137 | ins.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 138 | inode->i_blocks += ins.offset >> 9; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 139 | btrfs_check_file(root, inode); |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 140 | num_bytes -= cur_alloc_size; |
| 141 | alloc_hint = ins.objectid + ins.offset; |
| 142 | start += cur_alloc_size; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 143 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 144 | btrfs_drop_extent_cache(inode, orig_start, |
| 145 | orig_start + orig_num_bytes - 1); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 146 | btrfs_add_ordered_inode(inode); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 147 | btrfs_update_inode(trans, root, inode); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 148 | out: |
| 149 | btrfs_end_transaction(trans, root); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end) |
| 154 | { |
| 155 | u64 extent_start; |
| 156 | u64 extent_end; |
| 157 | u64 bytenr; |
| 158 | u64 cow_end; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 159 | u64 loops = 0; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 160 | u64 total_fs_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 162 | struct extent_buffer *leaf; |
| 163 | int found_type; |
| 164 | struct btrfs_path *path; |
| 165 | struct btrfs_file_extent_item *item; |
| 166 | int ret; |
| 167 | int err; |
| 168 | struct btrfs_key found_key; |
| 169 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 170 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 171 | path = btrfs_alloc_path(); |
| 172 | BUG_ON(!path); |
| 173 | again: |
| 174 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 175 | inode->i_ino, start, 0); |
| 176 | if (ret < 0) { |
| 177 | btrfs_free_path(path); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | cow_end = end; |
| 182 | if (ret != 0) { |
| 183 | if (path->slots[0] == 0) |
| 184 | goto not_found; |
| 185 | path->slots[0]--; |
| 186 | } |
| 187 | |
| 188 | leaf = path->nodes[0]; |
| 189 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 190 | struct btrfs_file_extent_item); |
| 191 | |
| 192 | /* are we inside the extent that was found? */ |
| 193 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 194 | found_type = btrfs_key_type(&found_key); |
| 195 | if (found_key.objectid != inode->i_ino || |
| 196 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 197 | goto not_found; |
| 198 | } |
| 199 | |
| 200 | found_type = btrfs_file_extent_type(leaf, item); |
| 201 | extent_start = found_key.offset; |
| 202 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 203 | u64 extent_num_bytes; |
| 204 | |
| 205 | extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item); |
| 206 | extent_end = extent_start + extent_num_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 207 | err = 0; |
| 208 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 209 | if (loops && start != extent_start) |
| 210 | goto not_found; |
| 211 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 212 | if (start < extent_start || start >= extent_end) |
| 213 | goto not_found; |
| 214 | |
| 215 | cow_end = min(end, extent_end - 1); |
| 216 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 217 | if (bytenr == 0) |
| 218 | goto not_found; |
| 219 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 220 | /* |
| 221 | * we may be called by the resizer, make sure we're inside |
| 222 | * the limits of the FS |
| 223 | */ |
| 224 | if (bytenr + extent_num_bytes > total_fs_bytes) |
| 225 | goto not_found; |
| 226 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 227 | if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) { |
| 228 | goto not_found; |
| 229 | } |
| 230 | |
| 231 | start = extent_end; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 232 | } else { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 233 | goto not_found; |
| 234 | } |
| 235 | loop: |
| 236 | if (start > end) { |
| 237 | btrfs_free_path(path); |
| 238 | return 0; |
| 239 | } |
| 240 | btrfs_release_path(root, path); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 241 | loops++; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 242 | goto again; |
| 243 | |
| 244 | not_found: |
| 245 | cow_file_range(inode, start, cow_end); |
| 246 | start = cow_end + 1; |
| 247 | goto loop; |
| 248 | } |
| 249 | |
| 250 | static int run_delalloc_range(struct inode *inode, u64 start, u64 end) |
| 251 | { |
| 252 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 253 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 254 | mutex_lock(&root->fs_info->fs_mutex); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 255 | if (btrfs_test_opt(root, NODATACOW) || |
| 256 | btrfs_test_flag(inode, NODATACOW)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 257 | ret = run_delalloc_nocow(inode, start, end); |
| 258 | else |
| 259 | ret = cow_file_range(inode, start, end); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 260 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 261 | mutex_unlock(&root->fs_info->fs_mutex); |
| 262 | return ret; |
| 263 | } |
| 264 | |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 265 | int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 266 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 267 | { |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 268 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 269 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 270 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 271 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 272 | root->fs_info->delalloc_bytes += end - start + 1; |
| 273 | spin_unlock(&root->fs_info->delalloc_lock); |
| 274 | } |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 279 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 280 | { |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 281 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 282 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 283 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 284 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
| 285 | printk("warning: delalloc account %Lu %Lu\n", |
| 286 | end - start + 1, root->fs_info->delalloc_bytes); |
| 287 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 288 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 289 | } else { |
| 290 | root->fs_info->delalloc_bytes -= end - start + 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 291 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 292 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 293 | spin_unlock(&root->fs_info->delalloc_lock); |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 298 | int btrfs_submit_bio_hook(int rw, struct bio *bio) |
| 299 | { |
| 300 | // struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 301 | struct bio_vec *bvec = bio->bi_io_vec; |
| 302 | struct inode *inode = bvec->bv_page->mapping->host; |
| 303 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 304 | struct btrfs_trans_handle *trans; |
| 305 | int ret = 0; |
| 306 | |
| 307 | if (rw != WRITE) |
| 308 | return 0; |
| 309 | |
| 310 | if (btrfs_test_opt(root, NODATASUM) || |
| 311 | btrfs_test_flag(inode, NODATASUM)) |
| 312 | return 0; |
| 313 | |
| 314 | mutex_lock(&root->fs_info->fs_mutex); |
| 315 | trans = btrfs_start_transaction(root, 1); |
| 316 | btrfs_set_trans_block_group(trans, inode); |
| 317 | btrfs_csum_file_blocks(trans, root, inode, bio); |
| 318 | ret = btrfs_end_transaction(trans, root); |
| 319 | BUG_ON(ret); |
| 320 | mutex_unlock(&root->fs_info->fs_mutex); |
| 321 | return ret; |
| 322 | } |
| 323 | #if 0 |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 324 | int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end) |
| 325 | { |
| 326 | struct inode *inode = page->mapping->host; |
| 327 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 328 | struct btrfs_trans_handle *trans; |
| 329 | char *kaddr; |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 330 | int ret = 0; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 331 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 332 | size_t offset = start - page_start; |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 333 | if (btrfs_test_opt(root, NODATASUM) || |
| 334 | btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 335 | return 0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 336 | mutex_lock(&root->fs_info->fs_mutex); |
| 337 | trans = btrfs_start_transaction(root, 1); |
| 338 | btrfs_set_trans_block_group(trans, inode); |
| 339 | kaddr = kmap(page); |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 340 | btrfs_csum_file_block(trans, root, inode, inode->i_ino, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 341 | start, kaddr + offset, end - start + 1); |
| 342 | kunmap(page); |
| 343 | ret = btrfs_end_transaction(trans, root); |
| 344 | BUG_ON(ret); |
| 345 | mutex_unlock(&root->fs_info->fs_mutex); |
| 346 | return ret; |
| 347 | } |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 348 | #endif |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 349 | int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end) |
| 350 | { |
| 351 | int ret = 0; |
| 352 | struct inode *inode = page->mapping->host; |
| 353 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 354 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 355 | struct btrfs_csum_item *item; |
| 356 | struct btrfs_path *path = NULL; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 357 | u32 csum; |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 358 | if (btrfs_test_opt(root, NODATASUM) || |
| 359 | btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 360 | return 0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 361 | mutex_lock(&root->fs_info->fs_mutex); |
| 362 | path = btrfs_alloc_path(); |
| 363 | item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0); |
| 364 | if (IS_ERR(item)) { |
| 365 | ret = PTR_ERR(item); |
| 366 | /* a csum that isn't present is a preallocated region. */ |
| 367 | if (ret == -ENOENT || ret == -EFBIG) |
| 368 | ret = 0; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 369 | csum = 0; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 370 | printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 371 | goto out; |
| 372 | } |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 373 | read_extent_buffer(path->nodes[0], &csum, (unsigned long)item, |
| 374 | BTRFS_CRC32_SIZE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 375 | set_state_private(io_tree, start, csum); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 376 | out: |
| 377 | if (path) |
| 378 | btrfs_free_path(path); |
| 379 | mutex_unlock(&root->fs_info->fs_mutex); |
| 380 | return ret; |
| 381 | } |
| 382 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 383 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
| 384 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 385 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 386 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 387 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 388 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 389 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 390 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 391 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 392 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 393 | u32 csum = ~(u32)0; |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 394 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 395 | |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 396 | if (btrfs_test_opt(root, NODATASUM) || |
| 397 | btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 398 | return 0; |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 399 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 400 | private = state->private; |
| 401 | ret = 0; |
| 402 | } else { |
| 403 | ret = get_state_private(io_tree, start, &private); |
| 404 | } |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 405 | local_irq_save(flags); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 406 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 407 | if (ret) { |
| 408 | goto zeroit; |
| 409 | } |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 410 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 411 | btrfs_csum_final(csum, (char *)&csum); |
| 412 | if (csum != private) { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 413 | goto zeroit; |
| 414 | } |
| 415 | kunmap_atomic(kaddr, KM_IRQ0); |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 416 | local_irq_restore(flags); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 417 | return 0; |
| 418 | |
| 419 | zeroit: |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 420 | printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n", |
| 421 | page->mapping->host->i_ino, (unsigned long long)start, csum, |
| 422 | private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 423 | memset(kaddr + offset, 1, end - start + 1); |
| 424 | flush_dcache_page(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 425 | kunmap_atomic(kaddr, KM_IRQ0); |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 426 | local_irq_restore(flags); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 427 | return 0; |
| 428 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 429 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 430 | void btrfs_read_locked_inode(struct inode *inode) |
| 431 | { |
| 432 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 433 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 434 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 435 | struct btrfs_inode_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 436 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 437 | struct btrfs_key location; |
| 438 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 439 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 440 | int ret; |
| 441 | |
| 442 | path = btrfs_alloc_path(); |
| 443 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 444 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 445 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 446 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 447 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 448 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 449 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 450 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 451 | leaf = path->nodes[0]; |
| 452 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 453 | struct btrfs_inode_item); |
| 454 | |
| 455 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 456 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 457 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 458 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
| 459 | inode->i_size = btrfs_inode_size(leaf, inode_item); |
| 460 | |
| 461 | tspec = btrfs_inode_atime(inode_item); |
| 462 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 463 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 464 | |
| 465 | tspec = btrfs_inode_mtime(inode_item); |
| 466 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 467 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 468 | |
| 469 | tspec = btrfs_inode_ctime(inode_item); |
| 470 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 471 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 472 | |
| 473 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); |
| 474 | inode->i_generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 475 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 476 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 477 | |
| 478 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 479 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
| 480 | alloc_group_block); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 481 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Chris Mason | e52ec0e | 2007-12-21 16:36:24 -0500 | [diff] [blame] | 482 | if (!BTRFS_I(inode)->block_group) { |
| 483 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, |
| 484 | NULL, 0, 0, 0); |
| 485 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 486 | btrfs_free_path(path); |
| 487 | inode_item = NULL; |
| 488 | |
| 489 | mutex_unlock(&root->fs_info->fs_mutex); |
| 490 | |
| 491 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 492 | case S_IFREG: |
| 493 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 494 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 495 | inode->i_fop = &btrfs_file_operations; |
| 496 | inode->i_op = &btrfs_file_inode_operations; |
| 497 | break; |
| 498 | case S_IFDIR: |
| 499 | inode->i_fop = &btrfs_dir_file_operations; |
| 500 | if (root == root->fs_info->tree_root) |
| 501 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 502 | else |
| 503 | inode->i_op = &btrfs_dir_inode_operations; |
| 504 | break; |
| 505 | case S_IFLNK: |
| 506 | inode->i_op = &btrfs_symlink_inode_operations; |
| 507 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 508 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 509 | default: |
| 510 | init_special_inode(inode, inode->i_mode, rdev); |
| 511 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 512 | } |
| 513 | return; |
| 514 | |
| 515 | make_bad: |
| 516 | btrfs_release_path(root, path); |
| 517 | btrfs_free_path(path); |
| 518 | mutex_unlock(&root->fs_info->fs_mutex); |
| 519 | make_bad_inode(inode); |
| 520 | } |
| 521 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 522 | static void fill_inode_item(struct extent_buffer *leaf, |
| 523 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 524 | struct inode *inode) |
| 525 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 526 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 527 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
| 528 | btrfs_set_inode_size(leaf, item, inode->i_size); |
| 529 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 530 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 531 | |
| 532 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 533 | inode->i_atime.tv_sec); |
| 534 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 535 | inode->i_atime.tv_nsec); |
| 536 | |
| 537 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 538 | inode->i_mtime.tv_sec); |
| 539 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 540 | inode->i_mtime.tv_nsec); |
| 541 | |
| 542 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 543 | inode->i_ctime.tv_sec); |
| 544 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 545 | inode->i_ctime.tv_nsec); |
| 546 | |
| 547 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); |
| 548 | btrfs_set_inode_generation(leaf, item, inode->i_generation); |
| 549 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 550 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 551 | btrfs_set_inode_block_group(leaf, item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 552 | BTRFS_I(inode)->block_group->key.objectid); |
| 553 | } |
| 554 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 555 | int btrfs_update_inode(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 556 | struct btrfs_root *root, |
| 557 | struct inode *inode) |
| 558 | { |
| 559 | struct btrfs_inode_item *inode_item; |
| 560 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 561 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 562 | int ret; |
| 563 | |
| 564 | path = btrfs_alloc_path(); |
| 565 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 566 | ret = btrfs_lookup_inode(trans, root, path, |
| 567 | &BTRFS_I(inode)->location, 1); |
| 568 | if (ret) { |
| 569 | if (ret > 0) |
| 570 | ret = -ENOENT; |
| 571 | goto failed; |
| 572 | } |
| 573 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 574 | leaf = path->nodes[0]; |
| 575 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 576 | struct btrfs_inode_item); |
| 577 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 578 | fill_inode_item(leaf, inode_item, inode); |
| 579 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 580 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 581 | ret = 0; |
| 582 | failed: |
| 583 | btrfs_release_path(root, path); |
| 584 | btrfs_free_path(path); |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 590 | struct btrfs_root *root, |
| 591 | struct inode *dir, |
| 592 | struct dentry *dentry) |
| 593 | { |
| 594 | struct btrfs_path *path; |
| 595 | const char *name = dentry->d_name.name; |
| 596 | int name_len = dentry->d_name.len; |
| 597 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 598 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 599 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 600 | struct btrfs_key key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 601 | |
| 602 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 603 | if (!path) { |
| 604 | ret = -ENOMEM; |
| 605 | goto err; |
| 606 | } |
| 607 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 608 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 609 | name, name_len, -1); |
| 610 | if (IS_ERR(di)) { |
| 611 | ret = PTR_ERR(di); |
| 612 | goto err; |
| 613 | } |
| 614 | if (!di) { |
| 615 | ret = -ENOENT; |
| 616 | goto err; |
| 617 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 618 | leaf = path->nodes[0]; |
| 619 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 620 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 621 | if (ret) |
| 622 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 623 | btrfs_release_path(root, path); |
| 624 | |
| 625 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 626 | key.objectid, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 627 | if (IS_ERR(di)) { |
| 628 | ret = PTR_ERR(di); |
| 629 | goto err; |
| 630 | } |
| 631 | if (!di) { |
| 632 | ret = -ENOENT; |
| 633 | goto err; |
| 634 | } |
| 635 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 636 | |
| 637 | dentry->d_inode->i_ctime = dir->i_ctime; |
Chris Mason | 76fea00 | 2007-12-13 09:06:01 -0500 | [diff] [blame] | 638 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
| 639 | dentry->d_inode->i_ino, |
| 640 | dentry->d_parent->d_inode->i_ino); |
| 641 | if (ret) { |
| 642 | printk("failed to delete reference to %.*s, " |
| 643 | "inode %lu parent %lu\n", name_len, name, |
| 644 | dentry->d_inode->i_ino, |
| 645 | dentry->d_parent->d_inode->i_ino); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 646 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 647 | err: |
| 648 | btrfs_free_path(path); |
| 649 | if (!ret) { |
| 650 | dir->i_size -= name_len * 2; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 651 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 652 | btrfs_update_inode(trans, root, dir); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 653 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 654 | dentry->d_inode->i_nlink--; |
| 655 | #else |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 656 | drop_nlink(dentry->d_inode); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 657 | #endif |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 658 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 659 | dir->i_sb->s_dirt = 1; |
| 660 | } |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 665 | { |
| 666 | struct btrfs_root *root; |
| 667 | struct btrfs_trans_handle *trans; |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 668 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 669 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 670 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 671 | |
| 672 | root = BTRFS_I(dir)->root; |
| 673 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 674 | |
| 675 | ret = btrfs_check_free_space(root, 1, 1); |
| 676 | if (ret) |
| 677 | goto fail; |
| 678 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 679 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 680 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 681 | btrfs_set_trans_block_group(trans, dir); |
| 682 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 683 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 684 | |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 685 | if (inode->i_nlink == 0) { |
| 686 | int found; |
| 687 | /* if the inode isn't linked anywhere, |
| 688 | * we don't need to worry about |
| 689 | * data=ordered |
| 690 | */ |
| 691 | found = btrfs_del_ordered_inode(inode); |
| 692 | if (found == 1) { |
| 693 | atomic_dec(&inode->i_count); |
| 694 | } |
| 695 | } |
| 696 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 697 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 698 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 699 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 700 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 701 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 706 | { |
| 707 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 708 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 709 | int ret; |
| 710 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 711 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 712 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 713 | |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 714 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
| 715 | return -ENOTEMPTY; |
| 716 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 717 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 718 | ret = btrfs_check_free_space(root, 1, 1); |
| 719 | if (ret) |
| 720 | goto fail; |
| 721 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 722 | trans = btrfs_start_transaction(root, 1); |
| 723 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 724 | |
| 725 | /* now the directory is empty */ |
| 726 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 727 | if (!err) { |
| 728 | inode->i_size = 0; |
| 729 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 730 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 731 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 732 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 733 | fail: |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 734 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 735 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 736 | btrfs_throttle(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 737 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 738 | if (ret && !err) |
| 739 | err = ret; |
| 740 | return err; |
| 741 | } |
| 742 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 743 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 744 | * this can truncate away extent items, csum items and directory items. |
| 745 | * It starts at a high offset and removes keys until it can't find |
| 746 | * any higher than i_size. |
| 747 | * |
| 748 | * csum items that cross the new i_size are truncated to the new size |
| 749 | * as well. |
| 750 | */ |
| 751 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 752 | struct btrfs_root *root, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 753 | struct inode *inode, |
| 754 | u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 755 | { |
| 756 | int ret; |
| 757 | struct btrfs_path *path; |
| 758 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 759 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 760 | u32 found_type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 761 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 762 | struct btrfs_file_extent_item *fi; |
| 763 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 764 | u64 extent_num_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 765 | u64 item_end = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 766 | u64 root_gen = 0; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 767 | u64 root_owner = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 768 | int found_extent; |
| 769 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 770 | int pending_del_nr = 0; |
| 771 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 772 | int extent_type = -1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 773 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 774 | btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 775 | path = btrfs_alloc_path(); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 776 | path->reada = -1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 777 | BUG_ON(!path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 778 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 779 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 780 | key.objectid = inode->i_ino; |
| 781 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 782 | key.type = (u8)-1; |
| 783 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 784 | btrfs_init_path(path); |
| 785 | search_again: |
| 786 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 787 | if (ret < 0) { |
| 788 | goto error; |
| 789 | } |
| 790 | if (ret > 0) { |
| 791 | BUG_ON(path->slots[0] == 0); |
| 792 | path->slots[0]--; |
| 793 | } |
| 794 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 795 | while(1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 796 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 797 | leaf = path->nodes[0]; |
| 798 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 799 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 800 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 801 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 802 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 803 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 804 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 805 | break; |
| 806 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 807 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 808 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 809 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 810 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 811 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 812 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 813 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 814 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 815 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 816 | struct btrfs_item *item = btrfs_item_nr(leaf, |
| 817 | path->slots[0]); |
| 818 | item_end += btrfs_file_extent_inline_len(leaf, |
| 819 | item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 820 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 821 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 822 | } |
| 823 | if (found_type == BTRFS_CSUM_ITEM_KEY) { |
| 824 | ret = btrfs_csum_truncate(trans, root, path, |
| 825 | inode->i_size); |
| 826 | BUG_ON(ret); |
| 827 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 828 | if (item_end < inode->i_size) { |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 829 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
| 830 | found_type = BTRFS_INODE_ITEM_KEY; |
| 831 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { |
| 832 | found_type = BTRFS_CSUM_ITEM_KEY; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 833 | } else if (found_type == BTRFS_EXTENT_DATA_KEY) { |
| 834 | found_type = BTRFS_XATTR_ITEM_KEY; |
| 835 | } else if (found_type == BTRFS_XATTR_ITEM_KEY) { |
| 836 | found_type = BTRFS_INODE_REF_KEY; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 837 | } else if (found_type) { |
| 838 | found_type--; |
| 839 | } else { |
| 840 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 841 | } |
Yan | a61721d | 2007-09-17 11:08:38 -0400 | [diff] [blame] | 842 | btrfs_set_key_type(&key, found_type); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 843 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 844 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 845 | if (found_key.offset >= inode->i_size) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 846 | del_item = 1; |
| 847 | else |
| 848 | del_item = 0; |
| 849 | found_extent = 0; |
| 850 | |
| 851 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 852 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 853 | goto delete; |
| 854 | |
| 855 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 856 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 857 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 858 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 859 | u64 orig_num_bytes = |
| 860 | btrfs_file_extent_num_bytes(leaf, fi); |
| 861 | extent_num_bytes = inode->i_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 862 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 863 | extent_num_bytes = extent_num_bytes & |
| 864 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 865 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 866 | extent_num_bytes); |
| 867 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 868 | extent_num_bytes); |
| 869 | if (extent_start != 0) |
| 870 | dec_i_blocks(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 871 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 872 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 873 | extent_num_bytes = |
| 874 | btrfs_file_extent_disk_num_bytes(leaf, |
| 875 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 876 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 877 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 878 | if (extent_start != 0) { |
| 879 | found_extent = 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 880 | dec_i_blocks(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 881 | } |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 882 | root_gen = btrfs_header_generation(leaf); |
| 883 | root_owner = btrfs_header_owner(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 884 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 885 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 886 | if (!del_item) { |
| 887 | u32 newsize = inode->i_size - found_key.offset; |
| 888 | dec_i_blocks(inode, item_end + 1 - |
| 889 | found_key.offset - newsize); |
| 890 | newsize = |
| 891 | btrfs_file_extent_calc_inline_size(newsize); |
| 892 | ret = btrfs_truncate_item(trans, root, path, |
| 893 | newsize, 1); |
| 894 | BUG_ON(ret); |
| 895 | } else { |
| 896 | dec_i_blocks(inode, item_end + 1 - |
| 897 | found_key.offset); |
| 898 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 899 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 900 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 901 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 902 | if (!pending_del_nr) { |
| 903 | /* no pending yet, add ourselves */ |
| 904 | pending_del_slot = path->slots[0]; |
| 905 | pending_del_nr = 1; |
| 906 | } else if (pending_del_nr && |
| 907 | path->slots[0] + 1 == pending_del_slot) { |
| 908 | /* hop on the pending chunk */ |
| 909 | pending_del_nr++; |
| 910 | pending_del_slot = path->slots[0]; |
| 911 | } else { |
| 912 | printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot); |
| 913 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 914 | } else { |
| 915 | break; |
| 916 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 917 | if (found_extent) { |
| 918 | ret = btrfs_free_extent(trans, root, extent_start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 919 | extent_num_bytes, |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 920 | root_owner, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 921 | root_gen, inode->i_ino, |
| 922 | found_key.offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 923 | BUG_ON(ret); |
| 924 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 925 | next: |
| 926 | if (path->slots[0] == 0) { |
| 927 | if (pending_del_nr) |
| 928 | goto del_pending; |
| 929 | btrfs_release_path(root, path); |
| 930 | goto search_again; |
| 931 | } |
| 932 | |
| 933 | path->slots[0]--; |
| 934 | if (pending_del_nr && |
| 935 | path->slots[0] + 1 != pending_del_slot) { |
| 936 | struct btrfs_key debug; |
| 937 | del_pending: |
| 938 | btrfs_item_key_to_cpu(path->nodes[0], &debug, |
| 939 | pending_del_slot); |
| 940 | ret = btrfs_del_items(trans, root, path, |
| 941 | pending_del_slot, |
| 942 | pending_del_nr); |
| 943 | BUG_ON(ret); |
| 944 | pending_del_nr = 0; |
| 945 | btrfs_release_path(root, path); |
| 946 | goto search_again; |
| 947 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 948 | } |
| 949 | ret = 0; |
| 950 | error: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 951 | if (pending_del_nr) { |
| 952 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 953 | pending_del_nr); |
| 954 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 955 | btrfs_release_path(root, path); |
| 956 | btrfs_free_path(path); |
| 957 | inode->i_sb->s_dirt = 1; |
| 958 | return ret; |
| 959 | } |
| 960 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 961 | static int btrfs_cow_one_page(struct inode *inode, struct page *page, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 962 | size_t zero_start) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 963 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 964 | char *kaddr; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 965 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 966 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 967 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 968 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 969 | |
Chris Mason | 190662b | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 970 | WARN_ON(!PageLocked(page)); |
Christoph Hellwig | b3cfa35 | 2007-09-17 11:25:58 -0400 | [diff] [blame] | 971 | set_page_extent_mapped(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 972 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 973 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 974 | set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 975 | page_end, GFP_NOFS); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 976 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 977 | if (zero_start != PAGE_CACHE_SIZE) { |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 978 | kaddr = kmap(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 979 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 980 | flush_dcache_page(page); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 981 | kunmap(page); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 982 | } |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 983 | set_page_dirty(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 984 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 985 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * taken from block_truncate_page, but does cow as it zeros out |
| 991 | * any bytes left in the last page in the file. |
| 992 | */ |
| 993 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 994 | { |
| 995 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 996 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 997 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 998 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 999 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 1000 | struct page *page; |
| 1001 | int ret = 0; |
| 1002 | u64 page_start; |
| 1003 | |
| 1004 | if ((offset & (blocksize - 1)) == 0) |
| 1005 | goto out; |
| 1006 | |
| 1007 | ret = -ENOMEM; |
| 1008 | page = grab_cache_page(mapping, index); |
| 1009 | if (!page) |
| 1010 | goto out; |
| 1011 | if (!PageUptodate(page)) { |
| 1012 | ret = btrfs_readpage(NULL, page); |
| 1013 | lock_page(page); |
| 1014 | if (!PageUptodate(page)) { |
| 1015 | ret = -EIO; |
| 1016 | goto out; |
| 1017 | } |
| 1018 | } |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1019 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1020 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1021 | ret = btrfs_cow_one_page(inode, page, offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1022 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1023 | unlock_page(page); |
| 1024 | page_cache_release(page); |
| 1025 | out: |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
| 1029 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 1030 | { |
| 1031 | struct inode *inode = dentry->d_inode; |
| 1032 | int err; |
| 1033 | |
| 1034 | err = inode_change_ok(inode, attr); |
| 1035 | if (err) |
| 1036 | return err; |
| 1037 | |
| 1038 | if (S_ISREG(inode->i_mode) && |
| 1039 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { |
| 1040 | struct btrfs_trans_handle *trans; |
| 1041 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1042 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1043 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1044 | u64 mask = root->sectorsize - 1; |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1045 | u64 hole_start = (inode->i_size + mask) & ~mask; |
Chris Mason | f392a93 | 2008-01-30 11:54:05 -0500 | [diff] [blame] | 1046 | u64 block_end = (attr->ia_size + mask) & ~mask; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1047 | u64 hole_size; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1048 | u64 alloc_hint = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1049 | |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1050 | if (attr->ia_size <= hole_start) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1051 | goto out; |
| 1052 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1053 | mutex_lock(&root->fs_info->fs_mutex); |
| 1054 | err = btrfs_check_free_space(root, 1, 0); |
| 1055 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1056 | if (err) |
| 1057 | goto fail; |
| 1058 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1059 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 1060 | |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1061 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1062 | hole_size = block_end - hole_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1063 | |
| 1064 | mutex_lock(&root->fs_info->fs_mutex); |
| 1065 | trans = btrfs_start_transaction(root, 1); |
| 1066 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1067 | err = btrfs_drop_extents(trans, root, inode, |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1068 | hole_start, block_end, hole_start, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 1069 | &alloc_hint); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1070 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1071 | if (alloc_hint != EXTENT_MAP_INLINE) { |
| 1072 | err = btrfs_insert_file_extent(trans, root, |
| 1073 | inode->i_ino, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1074 | hole_start, 0, 0, |
| 1075 | hole_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1076 | btrfs_drop_extent_cache(inode, hole_start, |
| 1077 | hole_size - 1); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1078 | btrfs_check_file(root, inode); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1079 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1080 | btrfs_end_transaction(trans, root); |
| 1081 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1082 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1083 | if (err) |
| 1084 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1085 | } |
| 1086 | out: |
| 1087 | err = inode_setattr(inode, attr); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1088 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1089 | return err; |
| 1090 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1091 | |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 1092 | void btrfs_put_inode(struct inode *inode) |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1093 | { |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 1094 | int ret; |
| 1095 | |
| 1096 | if (!BTRFS_I(inode)->ordered_trans) { |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1097 | return; |
| 1098 | } |
Chris Mason | 2da98f0 | 2008-01-16 11:44:43 -0500 | [diff] [blame] | 1099 | |
| 1100 | if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) || |
| 1101 | mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) |
| 1102 | return; |
| 1103 | |
| 1104 | ret = btrfs_del_ordered_inode(inode); |
| 1105 | if (ret == 1) { |
| 1106 | atomic_dec(&inode->i_count); |
| 1107 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1108 | } |
| 1109 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1110 | void btrfs_delete_inode(struct inode *inode) |
| 1111 | { |
| 1112 | struct btrfs_trans_handle *trans; |
| 1113 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1114 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1115 | int ret; |
| 1116 | |
| 1117 | truncate_inode_pages(&inode->i_data, 0); |
| 1118 | if (is_bad_inode(inode)) { |
| 1119 | goto no_delete; |
| 1120 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1121 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1122 | inode->i_size = 0; |
| 1123 | mutex_lock(&root->fs_info->fs_mutex); |
| 1124 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1125 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1126 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1127 | ret = btrfs_truncate_in_trans(trans, root, inode, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1128 | if (ret) |
| 1129 | goto no_delete_lock; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1130 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1131 | nr = trans->blocks_used; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1132 | clear_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1133 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1134 | btrfs_end_transaction(trans, root); |
| 1135 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1136 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1137 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1138 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1139 | |
| 1140 | no_delete_lock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1141 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1142 | btrfs_end_transaction(trans, root); |
| 1143 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1144 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1145 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1146 | no_delete: |
| 1147 | clear_inode(inode); |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * this returns the key found in the dir entry in the location pointer. |
| 1152 | * If no dir entries were found, location->objectid is 0. |
| 1153 | */ |
| 1154 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 1155 | struct btrfs_key *location) |
| 1156 | { |
| 1157 | const char *name = dentry->d_name.name; |
| 1158 | int namelen = dentry->d_name.len; |
| 1159 | struct btrfs_dir_item *di; |
| 1160 | struct btrfs_path *path; |
| 1161 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 1162 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1163 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1164 | if (namelen == 1 && strcmp(name, ".") == 0) { |
| 1165 | location->objectid = dir->i_ino; |
| 1166 | location->type = BTRFS_INODE_ITEM_KEY; |
| 1167 | location->offset = 0; |
| 1168 | return 0; |
| 1169 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1170 | path = btrfs_alloc_path(); |
| 1171 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1172 | |
Chris Mason | 7a72053 | 2007-12-13 09:06:59 -0500 | [diff] [blame] | 1173 | if (namelen == 2 && strcmp(name, "..") == 0) { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1174 | struct btrfs_key key; |
| 1175 | struct extent_buffer *leaf; |
| 1176 | u32 nritems; |
| 1177 | int slot; |
| 1178 | |
| 1179 | key.objectid = dir->i_ino; |
| 1180 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); |
| 1181 | key.offset = 0; |
| 1182 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1183 | BUG_ON(ret == 0); |
| 1184 | ret = 0; |
| 1185 | |
| 1186 | leaf = path->nodes[0]; |
| 1187 | slot = path->slots[0]; |
| 1188 | nritems = btrfs_header_nritems(leaf); |
| 1189 | if (slot >= nritems) |
| 1190 | goto out_err; |
| 1191 | |
| 1192 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 1193 | if (key.objectid != dir->i_ino || |
| 1194 | key.type != BTRFS_INODE_REF_KEY) { |
| 1195 | goto out_err; |
| 1196 | } |
| 1197 | location->objectid = key.offset; |
| 1198 | location->type = BTRFS_INODE_ITEM_KEY; |
| 1199 | location->offset = 0; |
| 1200 | goto out; |
| 1201 | } |
| 1202 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1203 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 1204 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 1205 | if (IS_ERR(di)) |
| 1206 | ret = PTR_ERR(di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1207 | if (!di || IS_ERR(di)) { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1208 | goto out_err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1209 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1210 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1211 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1212 | btrfs_free_path(path); |
| 1213 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1214 | out_err: |
| 1215 | location->objectid = 0; |
| 1216 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 1221 | * needs to be changed to reflect the root directory of the tree root. This |
| 1222 | * is kind of like crossing a mount point. |
| 1223 | */ |
| 1224 | static int fixup_tree_root_location(struct btrfs_root *root, |
| 1225 | struct btrfs_key *location, |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1226 | struct btrfs_root **sub_root, |
| 1227 | struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1228 | { |
| 1229 | struct btrfs_path *path; |
| 1230 | struct btrfs_root_item *ri; |
| 1231 | |
| 1232 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 1233 | return 0; |
| 1234 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 1235 | return 0; |
| 1236 | |
| 1237 | path = btrfs_alloc_path(); |
| 1238 | BUG_ON(!path); |
| 1239 | mutex_lock(&root->fs_info->fs_mutex); |
| 1240 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1241 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
| 1242 | dentry->d_name.name, |
| 1243 | dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1244 | if (IS_ERR(*sub_root)) |
| 1245 | return PTR_ERR(*sub_root); |
| 1246 | |
| 1247 | ri = &(*sub_root)->root_item; |
| 1248 | location->objectid = btrfs_root_dirid(ri); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1249 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 1250 | location->offset = 0; |
| 1251 | |
| 1252 | btrfs_free_path(path); |
| 1253 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 1258 | { |
| 1259 | struct btrfs_iget_args *args = p; |
| 1260 | inode->i_ino = args->ino; |
| 1261 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1262 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1263 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 1264 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1265 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 1270 | { |
| 1271 | struct btrfs_iget_args *args = opaque; |
| 1272 | return (args->ino == inode->i_ino && |
| 1273 | args->root == BTRFS_I(inode)->root); |
| 1274 | } |
| 1275 | |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1276 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
| 1277 | u64 root_objectid) |
| 1278 | { |
| 1279 | struct btrfs_iget_args args; |
| 1280 | args.ino = objectid; |
| 1281 | args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid); |
| 1282 | |
| 1283 | if (!args.root) |
| 1284 | return NULL; |
| 1285 | |
| 1286 | return ilookup5(s, objectid, btrfs_find_actor, (void *)&args); |
| 1287 | } |
| 1288 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1289 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 1290 | struct btrfs_root *root) |
| 1291 | { |
| 1292 | struct inode *inode; |
| 1293 | struct btrfs_iget_args args; |
| 1294 | args.ino = objectid; |
| 1295 | args.root = root; |
| 1296 | |
| 1297 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 1298 | btrfs_init_locked_inode, |
| 1299 | (void *)&args); |
| 1300 | return inode; |
| 1301 | } |
| 1302 | |
| 1303 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 1304 | struct nameidata *nd) |
| 1305 | { |
| 1306 | struct inode * inode; |
| 1307 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 1308 | struct btrfs_root *root = bi->root; |
| 1309 | struct btrfs_root *sub_root = root; |
| 1310 | struct btrfs_key location; |
| 1311 | int ret; |
| 1312 | |
| 1313 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 1314 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1315 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1316 | mutex_lock(&root->fs_info->fs_mutex); |
| 1317 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 1318 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1319 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1320 | if (ret < 0) |
| 1321 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1322 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1323 | inode = NULL; |
| 1324 | if (location.objectid) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1325 | ret = fixup_tree_root_location(root, &location, &sub_root, |
| 1326 | dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1327 | if (ret < 0) |
| 1328 | return ERR_PTR(ret); |
| 1329 | if (ret > 0) |
| 1330 | return ERR_PTR(-ENOENT); |
| 1331 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 1332 | sub_root); |
| 1333 | if (!inode) |
| 1334 | return ERR_PTR(-EACCES); |
| 1335 | if (inode->i_state & I_NEW) { |
| 1336 | /* the inode and parent dir are two different roots */ |
| 1337 | if (sub_root != root) { |
| 1338 | igrab(inode); |
| 1339 | sub_root->inode = inode; |
| 1340 | } |
| 1341 | BTRFS_I(inode)->root = sub_root; |
| 1342 | memcpy(&BTRFS_I(inode)->location, &location, |
| 1343 | sizeof(location)); |
| 1344 | btrfs_read_locked_inode(inode); |
| 1345 | unlock_new_inode(inode); |
| 1346 | } |
| 1347 | } |
| 1348 | return d_splice_alias(inode, dentry); |
| 1349 | } |
| 1350 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1351 | static unsigned char btrfs_filetype_table[] = { |
| 1352 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 1353 | }; |
| 1354 | |
| 1355 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 1356 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1357 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1358 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1359 | struct btrfs_item *item; |
| 1360 | struct btrfs_dir_item *di; |
| 1361 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1362 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1363 | struct btrfs_path *path; |
| 1364 | int ret; |
| 1365 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1366 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1367 | int slot; |
| 1368 | int advance; |
| 1369 | unsigned char d_type; |
| 1370 | int over = 0; |
| 1371 | u32 di_cur; |
| 1372 | u32 di_total; |
| 1373 | u32 di_len; |
| 1374 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1375 | char tmp_name[32]; |
| 1376 | char *name_ptr; |
| 1377 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1378 | |
| 1379 | /* FIXME, use a real flag for deciding about the key type */ |
| 1380 | if (root->fs_info->tree_root == root) |
| 1381 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1382 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1383 | /* special case for "." */ |
| 1384 | if (filp->f_pos == 0) { |
| 1385 | over = filldir(dirent, ".", 1, |
| 1386 | 1, inode->i_ino, |
| 1387 | DT_DIR); |
| 1388 | if (over) |
| 1389 | return 0; |
| 1390 | filp->f_pos = 1; |
| 1391 | } |
| 1392 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1393 | mutex_lock(&root->fs_info->fs_mutex); |
| 1394 | key.objectid = inode->i_ino; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1395 | path = btrfs_alloc_path(); |
| 1396 | path->reada = 2; |
| 1397 | |
| 1398 | /* special case for .., just use the back ref */ |
| 1399 | if (filp->f_pos == 1) { |
| 1400 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); |
| 1401 | key.offset = 0; |
| 1402 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1403 | BUG_ON(ret == 0); |
| 1404 | leaf = path->nodes[0]; |
| 1405 | slot = path->slots[0]; |
| 1406 | nritems = btrfs_header_nritems(leaf); |
| 1407 | if (slot >= nritems) { |
| 1408 | btrfs_release_path(root, path); |
| 1409 | goto read_dir_items; |
| 1410 | } |
| 1411 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1412 | btrfs_release_path(root, path); |
| 1413 | if (found_key.objectid != key.objectid || |
| 1414 | found_key.type != BTRFS_INODE_REF_KEY) |
| 1415 | goto read_dir_items; |
| 1416 | over = filldir(dirent, "..", 2, |
| 1417 | 2, found_key.offset, DT_DIR); |
| 1418 | if (over) |
| 1419 | goto nopos; |
| 1420 | filp->f_pos = 2; |
| 1421 | } |
| 1422 | |
| 1423 | read_dir_items: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1424 | btrfs_set_key_type(&key, key_type); |
| 1425 | key.offset = filp->f_pos; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1426 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1427 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1428 | if (ret < 0) |
| 1429 | goto err; |
| 1430 | advance = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1431 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1432 | leaf = path->nodes[0]; |
| 1433 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1434 | slot = path->slots[0]; |
| 1435 | if (advance || slot >= nritems) { |
| 1436 | if (slot >= nritems -1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1437 | ret = btrfs_next_leaf(root, path); |
| 1438 | if (ret) |
| 1439 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1440 | leaf = path->nodes[0]; |
| 1441 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1442 | slot = path->slots[0]; |
| 1443 | } else { |
| 1444 | slot++; |
| 1445 | path->slots[0]++; |
| 1446 | } |
| 1447 | } |
| 1448 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1449 | item = btrfs_item_nr(leaf, slot); |
| 1450 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1451 | |
| 1452 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1453 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1454 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1455 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1456 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1457 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1458 | |
| 1459 | filp->f_pos = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1460 | advance = 1; |
| 1461 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 1462 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1463 | di_total = btrfs_item_size(leaf, item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1464 | while(di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1465 | struct btrfs_key location; |
| 1466 | |
| 1467 | name_len = btrfs_dir_name_len(leaf, di); |
| 1468 | if (name_len < 32) { |
| 1469 | name_ptr = tmp_name; |
| 1470 | } else { |
| 1471 | name_ptr = kmalloc(name_len, GFP_NOFS); |
| 1472 | BUG_ON(!name_ptr); |
| 1473 | } |
| 1474 | read_extent_buffer(leaf, name_ptr, |
| 1475 | (unsigned long)(di + 1), name_len); |
| 1476 | |
| 1477 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 1478 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1479 | over = filldir(dirent, name_ptr, name_len, |
| 1480 | found_key.offset, |
| 1481 | location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1482 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1483 | |
| 1484 | if (name_ptr != tmp_name) |
| 1485 | kfree(name_ptr); |
| 1486 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1487 | if (over) |
| 1488 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 1489 | di_len = btrfs_dir_name_len(leaf, di) + |
| 1490 | btrfs_dir_data_len(leaf, di) +sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1491 | di_cur += di_len; |
| 1492 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 1493 | } |
| 1494 | } |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 1495 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 1496 | filp->f_pos = INT_LIMIT(typeof(filp->f_pos)); |
| 1497 | else |
| 1498 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1499 | nopos: |
| 1500 | ret = 0; |
| 1501 | err: |
| 1502 | btrfs_release_path(root, path); |
| 1503 | btrfs_free_path(path); |
| 1504 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1505 | return ret; |
| 1506 | } |
| 1507 | |
| 1508 | int btrfs_write_inode(struct inode *inode, int wait) |
| 1509 | { |
| 1510 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1511 | struct btrfs_trans_handle *trans; |
| 1512 | int ret = 0; |
| 1513 | |
| 1514 | if (wait) { |
| 1515 | mutex_lock(&root->fs_info->fs_mutex); |
| 1516 | trans = btrfs_start_transaction(root, 1); |
| 1517 | btrfs_set_trans_block_group(trans, inode); |
| 1518 | ret = btrfs_commit_transaction(trans, root); |
| 1519 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1520 | } |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1525 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1526 | * inode changes. But, it is most likely to find the inode in cache. |
| 1527 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 1528 | * to keep or drop this code. |
| 1529 | */ |
| 1530 | void btrfs_dirty_inode(struct inode *inode) |
| 1531 | { |
| 1532 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1533 | struct btrfs_trans_handle *trans; |
| 1534 | |
| 1535 | mutex_lock(&root->fs_info->fs_mutex); |
| 1536 | trans = btrfs_start_transaction(root, 1); |
| 1537 | btrfs_set_trans_block_group(trans, inode); |
| 1538 | btrfs_update_inode(trans, root, inode); |
| 1539 | btrfs_end_transaction(trans, root); |
| 1540 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 1544 | struct btrfs_root *root, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1545 | const char *name, int name_len, |
| 1546 | u64 ref_objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1547 | u64 objectid, |
| 1548 | struct btrfs_block_group_cache *group, |
| 1549 | int mode) |
| 1550 | { |
| 1551 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1552 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1553 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1554 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1555 | struct btrfs_inode_ref *ref; |
| 1556 | struct btrfs_key key[2]; |
| 1557 | u32 sizes[2]; |
| 1558 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1559 | int ret; |
| 1560 | int owner; |
| 1561 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1562 | path = btrfs_alloc_path(); |
| 1563 | BUG_ON(!path); |
| 1564 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1565 | inode = new_inode(root->fs_info->sb); |
| 1566 | if (!inode) |
| 1567 | return ERR_PTR(-ENOMEM); |
| 1568 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1569 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 1570 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1571 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1572 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1573 | BTRFS_I(inode)->root = root; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1574 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1575 | if (mode & S_IFDIR) |
| 1576 | owner = 0; |
| 1577 | else |
| 1578 | owner = 1; |
| 1579 | group = btrfs_find_block_group(root, group, 0, 0, owner); |
| 1580 | BTRFS_I(inode)->block_group = group; |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 1581 | BTRFS_I(inode)->flags = 0; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1582 | |
| 1583 | key[0].objectid = objectid; |
| 1584 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 1585 | key[0].offset = 0; |
| 1586 | |
| 1587 | key[1].objectid = objectid; |
| 1588 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 1589 | key[1].offset = ref_objectid; |
| 1590 | |
| 1591 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 1592 | sizes[1] = name_len + sizeof(*ref); |
| 1593 | |
| 1594 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 1595 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1596 | goto fail; |
| 1597 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1598 | if (objectid > root->highest_inode) |
| 1599 | root->highest_inode = objectid; |
| 1600 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1601 | inode->i_uid = current->fsuid; |
| 1602 | inode->i_gid = current->fsgid; |
| 1603 | inode->i_mode = mode; |
| 1604 | inode->i_ino = objectid; |
| 1605 | inode->i_blocks = 0; |
| 1606 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1607 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1608 | struct btrfs_inode_item); |
| 1609 | fill_inode_item(path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1610 | |
| 1611 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 1612 | struct btrfs_inode_ref); |
| 1613 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 1614 | ptr = (unsigned long)(ref + 1); |
| 1615 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 1616 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1617 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 1618 | btrfs_free_path(path); |
| 1619 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1620 | location = &BTRFS_I(inode)->location; |
| 1621 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1622 | location->offset = 0; |
| 1623 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 1624 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1625 | insert_inode_hash(inode); |
| 1626 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1627 | fail: |
| 1628 | btrfs_free_path(path); |
| 1629 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 1633 | { |
| 1634 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 1635 | } |
| 1636 | |
| 1637 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1638 | struct dentry *dentry, struct inode *inode, |
| 1639 | int add_backref) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1640 | { |
| 1641 | int ret; |
| 1642 | struct btrfs_key key; |
| 1643 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 1644 | struct inode *parent_inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1645 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1646 | key.objectid = inode->i_ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1647 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 1648 | key.offset = 0; |
| 1649 | |
| 1650 | ret = btrfs_insert_dir_item(trans, root, |
| 1651 | dentry->d_name.name, dentry->d_name.len, |
| 1652 | dentry->d_parent->d_inode->i_ino, |
| 1653 | &key, btrfs_inode_type(inode)); |
| 1654 | if (ret == 0) { |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1655 | if (add_backref) { |
| 1656 | ret = btrfs_insert_inode_ref(trans, root, |
| 1657 | dentry->d_name.name, |
| 1658 | dentry->d_name.len, |
| 1659 | inode->i_ino, |
| 1660 | dentry->d_parent->d_inode->i_ino); |
| 1661 | } |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 1662 | parent_inode = dentry->d_parent->d_inode; |
| 1663 | parent_inode->i_size += dentry->d_name.len * 2; |
| 1664 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1665 | ret = btrfs_update_inode(trans, root, |
| 1666 | dentry->d_parent->d_inode); |
| 1667 | } |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1672 | struct dentry *dentry, struct inode *inode, |
| 1673 | int backref) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1674 | { |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1675 | int err = btrfs_add_link(trans, dentry, inode, backref); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1676 | if (!err) { |
| 1677 | d_instantiate(dentry, inode); |
| 1678 | return 0; |
| 1679 | } |
| 1680 | if (err > 0) |
| 1681 | err = -EEXIST; |
| 1682 | return err; |
| 1683 | } |
| 1684 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1685 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 1686 | int mode, dev_t rdev) |
| 1687 | { |
| 1688 | struct btrfs_trans_handle *trans; |
| 1689 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1690 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1691 | int err; |
| 1692 | int drop_inode = 0; |
| 1693 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1694 | unsigned long nr = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1695 | |
| 1696 | if (!new_valid_dev(rdev)) |
| 1697 | return -EINVAL; |
| 1698 | |
| 1699 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1700 | err = btrfs_check_free_space(root, 1, 0); |
| 1701 | if (err) |
| 1702 | goto fail; |
| 1703 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1704 | trans = btrfs_start_transaction(root, 1); |
| 1705 | btrfs_set_trans_block_group(trans, dir); |
| 1706 | |
| 1707 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1708 | if (err) { |
| 1709 | err = -ENOSPC; |
| 1710 | goto out_unlock; |
| 1711 | } |
| 1712 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1713 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 1714 | dentry->d_name.len, |
| 1715 | dentry->d_parent->d_inode->i_ino, objectid, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1716 | BTRFS_I(dir)->block_group, mode); |
| 1717 | err = PTR_ERR(inode); |
| 1718 | if (IS_ERR(inode)) |
| 1719 | goto out_unlock; |
| 1720 | |
| 1721 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1722 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1723 | if (err) |
| 1724 | drop_inode = 1; |
| 1725 | else { |
| 1726 | inode->i_op = &btrfs_special_inode_operations; |
| 1727 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1728 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1729 | } |
| 1730 | dir->i_sb->s_dirt = 1; |
| 1731 | btrfs_update_inode_block_group(trans, inode); |
| 1732 | btrfs_update_inode_block_group(trans, dir); |
| 1733 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1734 | nr = trans->blocks_used; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1735 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1736 | fail: |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1737 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1738 | |
| 1739 | if (drop_inode) { |
| 1740 | inode_dec_link_count(inode); |
| 1741 | iput(inode); |
| 1742 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1743 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1744 | btrfs_throttle(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1745 | return err; |
| 1746 | } |
| 1747 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1748 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 1749 | int mode, struct nameidata *nd) |
| 1750 | { |
| 1751 | struct btrfs_trans_handle *trans; |
| 1752 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1753 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1754 | int err; |
| 1755 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1756 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1757 | u64 objectid; |
| 1758 | |
| 1759 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1760 | err = btrfs_check_free_space(root, 1, 0); |
| 1761 | if (err) |
| 1762 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1763 | trans = btrfs_start_transaction(root, 1); |
| 1764 | btrfs_set_trans_block_group(trans, dir); |
| 1765 | |
| 1766 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1767 | if (err) { |
| 1768 | err = -ENOSPC; |
| 1769 | goto out_unlock; |
| 1770 | } |
| 1771 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1772 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 1773 | dentry->d_name.len, |
| 1774 | dentry->d_parent->d_inode->i_ino, |
| 1775 | objectid, BTRFS_I(dir)->block_group, mode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1776 | err = PTR_ERR(inode); |
| 1777 | if (IS_ERR(inode)) |
| 1778 | goto out_unlock; |
| 1779 | |
| 1780 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1781 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1782 | if (err) |
| 1783 | drop_inode = 1; |
| 1784 | else { |
| 1785 | inode->i_mapping->a_ops = &btrfs_aops; |
| 1786 | inode->i_fop = &btrfs_file_operations; |
| 1787 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1788 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 1789 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1790 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1791 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1792 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1793 | } |
| 1794 | dir->i_sb->s_dirt = 1; |
| 1795 | btrfs_update_inode_block_group(trans, inode); |
| 1796 | btrfs_update_inode_block_group(trans, dir); |
| 1797 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1798 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1799 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1800 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1801 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1802 | |
| 1803 | if (drop_inode) { |
| 1804 | inode_dec_link_count(inode); |
| 1805 | iput(inode); |
| 1806 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1807 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1808 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1809 | return err; |
| 1810 | } |
| 1811 | |
| 1812 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 1813 | struct dentry *dentry) |
| 1814 | { |
| 1815 | struct btrfs_trans_handle *trans; |
| 1816 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1817 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1818 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1819 | int err; |
| 1820 | int drop_inode = 0; |
| 1821 | |
| 1822 | if (inode->i_nlink == 0) |
| 1823 | return -ENOENT; |
| 1824 | |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1825 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
| 1826 | inode->i_nlink++; |
| 1827 | #else |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1828 | inc_nlink(inode); |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1829 | #endif |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1830 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1831 | err = btrfs_check_free_space(root, 1, 0); |
| 1832 | if (err) |
| 1833 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1834 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1835 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1836 | btrfs_set_trans_block_group(trans, dir); |
| 1837 | atomic_inc(&inode->i_count); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1838 | err = btrfs_add_nondir(trans, dentry, inode, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1839 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1840 | if (err) |
| 1841 | drop_inode = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1842 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1843 | dir->i_sb->s_dirt = 1; |
| 1844 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1845 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1846 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1847 | if (err) |
| 1848 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1849 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1850 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1851 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1852 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1853 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1854 | |
| 1855 | if (drop_inode) { |
| 1856 | inode_dec_link_count(inode); |
| 1857 | iput(inode); |
| 1858 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1859 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1860 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1861 | return err; |
| 1862 | } |
| 1863 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1864 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1865 | { |
| 1866 | struct inode *inode; |
| 1867 | struct btrfs_trans_handle *trans; |
| 1868 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1869 | int err = 0; |
| 1870 | int drop_on_err = 0; |
| 1871 | u64 objectid; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1872 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1873 | |
| 1874 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1875 | err = btrfs_check_free_space(root, 1, 0); |
| 1876 | if (err) |
| 1877 | goto out_unlock; |
| 1878 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1879 | trans = btrfs_start_transaction(root, 1); |
| 1880 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1881 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1882 | if (IS_ERR(trans)) { |
| 1883 | err = PTR_ERR(trans); |
| 1884 | goto out_unlock; |
| 1885 | } |
| 1886 | |
| 1887 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1888 | if (err) { |
| 1889 | err = -ENOSPC; |
| 1890 | goto out_unlock; |
| 1891 | } |
| 1892 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1893 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 1894 | dentry->d_name.len, |
| 1895 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1896 | BTRFS_I(dir)->block_group, S_IFDIR | mode); |
| 1897 | if (IS_ERR(inode)) { |
| 1898 | err = PTR_ERR(inode); |
| 1899 | goto out_fail; |
| 1900 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1901 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1902 | drop_on_err = 1; |
| 1903 | inode->i_op = &btrfs_dir_inode_operations; |
| 1904 | inode->i_fop = &btrfs_dir_file_operations; |
| 1905 | btrfs_set_trans_block_group(trans, inode); |
| 1906 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1907 | inode->i_size = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1908 | err = btrfs_update_inode(trans, root, inode); |
| 1909 | if (err) |
| 1910 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1911 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 1912 | err = btrfs_add_link(trans, dentry, inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1913 | if (err) |
| 1914 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1915 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1916 | d_instantiate(dentry, inode); |
| 1917 | drop_on_err = 0; |
| 1918 | dir->i_sb->s_dirt = 1; |
| 1919 | btrfs_update_inode_block_group(trans, inode); |
| 1920 | btrfs_update_inode_block_group(trans, dir); |
| 1921 | |
| 1922 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1923 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1924 | btrfs_end_transaction(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1925 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1926 | out_unlock: |
| 1927 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1928 | if (drop_on_err) |
| 1929 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1930 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1931 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1932 | return err; |
| 1933 | } |
| 1934 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1935 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1936 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1937 | int create) |
| 1938 | { |
| 1939 | int ret; |
| 1940 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1941 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1942 | u64 extent_start = 0; |
| 1943 | u64 extent_end = 0; |
| 1944 | u64 objectid = inode->i_ino; |
| 1945 | u32 found_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1946 | struct btrfs_path *path; |
| 1947 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1948 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1949 | struct extent_buffer *leaf; |
| 1950 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1951 | struct extent_map *em = NULL; |
| 1952 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1953 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1954 | struct btrfs_trans_handle *trans = NULL; |
| 1955 | |
| 1956 | path = btrfs_alloc_path(); |
| 1957 | BUG_ON(!path); |
| 1958 | mutex_lock(&root->fs_info->fs_mutex); |
| 1959 | |
| 1960 | again: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1961 | spin_lock(&em_tree->lock); |
| 1962 | em = lookup_extent_mapping(em_tree, start, len); |
| 1963 | spin_unlock(&em_tree->lock); |
| 1964 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1965 | if (em) { |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 1966 | if (em->start > start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1967 | printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n", |
| 1968 | start, len, em->start, em->len); |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 1969 | WARN_ON(1); |
| 1970 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1971 | if (em->block_start == EXTENT_MAP_INLINE && page) |
| 1972 | free_extent_map(em); |
| 1973 | else |
| 1974 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1975 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1976 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1977 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1978 | err = -ENOMEM; |
| 1979 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1980 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1981 | |
| 1982 | em->start = EXTENT_MAP_HOLE; |
| 1983 | em->len = (u64)-1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1984 | em->bdev = inode->i_sb->s_bdev; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1985 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 1986 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1987 | if (ret < 0) { |
| 1988 | err = ret; |
| 1989 | goto out; |
| 1990 | } |
| 1991 | |
| 1992 | if (ret != 0) { |
| 1993 | if (path->slots[0] == 0) |
| 1994 | goto not_found; |
| 1995 | path->slots[0]--; |
| 1996 | } |
| 1997 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1998 | leaf = path->nodes[0]; |
| 1999 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2000 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2001 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2002 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2003 | found_type = btrfs_key_type(&found_key); |
| 2004 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2005 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 2006 | goto not_found; |
| 2007 | } |
| 2008 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2009 | found_type = btrfs_file_extent_type(leaf, item); |
| 2010 | extent_start = found_key.offset; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2011 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 2012 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2013 | btrfs_file_extent_num_bytes(leaf, item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2014 | err = 0; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2015 | if (start < extent_start || start >= extent_end) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2016 | em->start = start; |
| 2017 | if (start < extent_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2018 | if (start + len <= extent_start) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2019 | goto not_found; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2020 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2021 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2022 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2023 | } |
| 2024 | goto not_found_em; |
| 2025 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2026 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 2027 | if (bytenr == 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2028 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2029 | em->len = extent_end - extent_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2030 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2031 | goto insert; |
| 2032 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2033 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 2034 | em->block_start = bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2035 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2036 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2037 | goto insert; |
| 2038 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2039 | u64 page_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2040 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2041 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2042 | size_t size; |
| 2043 | size_t extent_offset; |
| 2044 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2045 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2046 | size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf, |
| 2047 | path->slots[0])); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2048 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 2049 | ~((u64)root->sectorsize - 1); |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2050 | if (start < extent_start || start >= extent_end) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2051 | em->start = start; |
| 2052 | if (start < extent_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2053 | if (start + len <= extent_start) |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2054 | goto not_found; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2055 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2056 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2057 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2058 | } |
| 2059 | goto not_found_em; |
| 2060 | } |
| 2061 | em->block_start = EXTENT_MAP_INLINE; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2062 | |
| 2063 | if (!page) { |
| 2064 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2065 | em->len = size; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2066 | goto out; |
| 2067 | } |
| 2068 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2069 | page_start = page_offset(page) + pg_offset; |
| 2070 | extent_offset = page_start - extent_start; |
| 2071 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2072 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2073 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2074 | em->len = (copy_size + root->sectorsize - 1) & |
| 2075 | ~((u64)root->sectorsize - 1); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 2076 | map = kmap(page); |
| 2077 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2078 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2079 | read_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2080 | copy_size); |
| 2081 | flush_dcache_page(page); |
| 2082 | } else if (create && PageUptodate(page)) { |
| 2083 | if (!trans) { |
| 2084 | kunmap(page); |
| 2085 | free_extent_map(em); |
| 2086 | em = NULL; |
| 2087 | btrfs_release_path(root, path); |
| 2088 | trans = btrfs_start_transaction(root, 1); |
| 2089 | goto again; |
| 2090 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2091 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2092 | copy_size); |
| 2093 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2094 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2095 | kunmap(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2096 | set_extent_uptodate(io_tree, em->start, |
| 2097 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2098 | goto insert; |
| 2099 | } else { |
| 2100 | printk("unkknown found_type %d\n", found_type); |
| 2101 | WARN_ON(1); |
| 2102 | } |
| 2103 | not_found: |
| 2104 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2105 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2106 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2107 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2108 | insert: |
| 2109 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2110 | if (em->start > start || extent_map_end(em) <= start) { |
| 2111 | printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2112 | err = -EIO; |
| 2113 | goto out; |
| 2114 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2115 | |
| 2116 | err = 0; |
| 2117 | spin_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2118 | ret = add_extent_mapping(em_tree, em); |
| 2119 | if (ret == -EEXIST) { |
| 2120 | free_extent_map(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2121 | em = lookup_extent_mapping(em_tree, start, len); |
| 2122 | if (!em) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2123 | err = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2124 | printk("failing to insert %Lu %Lu\n", start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2125 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2126 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2127 | spin_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2128 | out: |
| 2129 | btrfs_free_path(path); |
| 2130 | if (trans) { |
| 2131 | ret = btrfs_end_transaction(trans, root); |
| 2132 | if (!err) |
| 2133 | err = ret; |
| 2134 | } |
| 2135 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2136 | if (err) { |
| 2137 | free_extent_map(em); |
| 2138 | WARN_ON(1); |
| 2139 | return ERR_PTR(err); |
| 2140 | } |
| 2141 | return em; |
| 2142 | } |
| 2143 | |
Christoph Hellwig | d396c6f | 2007-09-10 20:02:30 -0400 | [diff] [blame] | 2144 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2145 | { |
Christoph Hellwig | d396c6f | 2007-09-10 20:02:30 -0400 | [diff] [blame] | 2146 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2147 | } |
| 2148 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2149 | int btrfs_readpage(struct file *file, struct page *page) |
| 2150 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2151 | struct extent_io_tree *tree; |
| 2152 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2153 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2154 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2156 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 2157 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2158 | struct extent_io_tree *tree; |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2159 | |
| 2160 | |
| 2161 | if (current->flags & PF_MEMALLOC) { |
| 2162 | redirty_page_for_writepage(wbc, page); |
| 2163 | unlock_page(page); |
| 2164 | return 0; |
| 2165 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2166 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2167 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 2168 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2169 | |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 2170 | static int btrfs_writepages(struct address_space *mapping, |
| 2171 | struct writeback_control *wbc) |
| 2172 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2173 | struct extent_io_tree *tree; |
| 2174 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 2175 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 2176 | } |
| 2177 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 2178 | static int |
| 2179 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 2180 | struct list_head *pages, unsigned nr_pages) |
| 2181 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2182 | struct extent_io_tree *tree; |
| 2183 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 2184 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 2185 | btrfs_get_extent); |
| 2186 | } |
| 2187 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2188 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2189 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2190 | struct extent_io_tree *tree; |
| 2191 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2192 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2193 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2194 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 2195 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2196 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2197 | if (ret == 1) { |
| 2198 | ClearPagePrivate(page); |
| 2199 | set_page_private(page, 0); |
| 2200 | page_cache_release(page); |
| 2201 | } |
| 2202 | return ret; |
| 2203 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2204 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2205 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 2206 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2207 | struct extent_io_tree *tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2208 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2209 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2210 | extent_invalidatepage(tree, page, offset); |
| 2211 | btrfs_releasepage(page, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2212 | } |
| 2213 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2214 | /* |
| 2215 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 2216 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 2217 | * be careful to check for EOF conditions here. We set the page up correctly |
| 2218 | * for a written page which means we get ENOSPC checking when writing into |
| 2219 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 2220 | * support these features. |
| 2221 | * |
| 2222 | * We are not allowed to take the i_mutex here so we have to play games to |
| 2223 | * protect against truncate races as the page could now be beyond EOF. Because |
| 2224 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 2225 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 2226 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 2227 | * unlock the page. |
| 2228 | */ |
| 2229 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 2230 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2231 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2232 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2233 | unsigned long end; |
| 2234 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2235 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2236 | u64 page_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2237 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2238 | mutex_lock(&root->fs_info->fs_mutex); |
| 2239 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 2240 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2241 | if (ret) |
| 2242 | goto out; |
| 2243 | |
| 2244 | ret = -EINVAL; |
| 2245 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2246 | lock_page(page); |
| 2247 | wait_on_page_writeback(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2248 | size = i_size_read(inode); |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 2249 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2250 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2251 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2252 | (page_start > size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2253 | /* page got truncated out from underneath us */ |
| 2254 | goto out_unlock; |
| 2255 | } |
| 2256 | |
| 2257 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2258 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2259 | end = size & ~PAGE_CACHE_MASK; |
| 2260 | else |
| 2261 | end = PAGE_CACHE_SIZE; |
| 2262 | |
Chris Mason | b888db2b | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2263 | ret = btrfs_cow_one_page(inode, page, end); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2264 | |
| 2265 | out_unlock: |
| 2266 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2267 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2268 | return ret; |
| 2269 | } |
| 2270 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2271 | static void btrfs_truncate(struct inode *inode) |
| 2272 | { |
| 2273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2274 | int ret; |
| 2275 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2276 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2277 | |
| 2278 | if (!S_ISREG(inode->i_mode)) |
| 2279 | return; |
| 2280 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 2281 | return; |
| 2282 | |
| 2283 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 2284 | |
| 2285 | mutex_lock(&root->fs_info->fs_mutex); |
| 2286 | trans = btrfs_start_transaction(root, 1); |
| 2287 | btrfs_set_trans_block_group(trans, inode); |
| 2288 | |
| 2289 | /* FIXME, add redo link to tree so we don't leak on crash */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 2290 | ret = btrfs_truncate_in_trans(trans, root, inode, |
| 2291 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2292 | btrfs_update_inode(trans, root, inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2293 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2294 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2295 | ret = btrfs_end_transaction(trans, root); |
| 2296 | BUG_ON(ret); |
| 2297 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2298 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2299 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2300 | } |
| 2301 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2302 | static int noinline create_subvol(struct btrfs_root *root, char *name, |
| 2303 | int namelen) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2304 | { |
| 2305 | struct btrfs_trans_handle *trans; |
| 2306 | struct btrfs_key key; |
| 2307 | struct btrfs_root_item root_item; |
| 2308 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2309 | struct extent_buffer *leaf; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2310 | struct btrfs_root *new_root = root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2311 | struct inode *inode; |
| 2312 | struct inode *dir; |
| 2313 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2314 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2315 | u64 objectid; |
| 2316 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2317 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2318 | |
| 2319 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2320 | ret = btrfs_check_free_space(root, 1, 0); |
| 2321 | if (ret) |
| 2322 | goto fail_commit; |
| 2323 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2324 | trans = btrfs_start_transaction(root, 1); |
| 2325 | BUG_ON(!trans); |
| 2326 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2327 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2328 | 0, &objectid); |
| 2329 | if (ret) |
| 2330 | goto fail; |
| 2331 | |
| 2332 | leaf = __btrfs_alloc_free_block(trans, root, root->leafsize, |
| 2333 | objectid, trans->transid, 0, 0, |
| 2334 | 0, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2335 | if (IS_ERR(leaf)) |
| 2336 | return PTR_ERR(leaf); |
| 2337 | |
| 2338 | btrfs_set_header_nritems(leaf, 0); |
| 2339 | btrfs_set_header_level(leaf, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2340 | btrfs_set_header_bytenr(leaf, leaf->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2341 | btrfs_set_header_generation(leaf, trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2342 | btrfs_set_header_owner(leaf, objectid); |
| 2343 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2344 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 2345 | (unsigned long)btrfs_header_fsid(leaf), |
| 2346 | BTRFS_FSID_SIZE); |
| 2347 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2348 | |
| 2349 | inode_item = &root_item.inode; |
| 2350 | memset(inode_item, 0, sizeof(*inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2351 | inode_item->generation = cpu_to_le64(1); |
| 2352 | inode_item->size = cpu_to_le64(3); |
| 2353 | inode_item->nlink = cpu_to_le32(1); |
| 2354 | inode_item->nblocks = cpu_to_le64(1); |
| 2355 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2356 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2357 | btrfs_set_root_bytenr(&root_item, leaf->start); |
| 2358 | btrfs_set_root_level(&root_item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2359 | btrfs_set_root_refs(&root_item, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2360 | btrfs_set_root_used(&root_item, 0); |
| 2361 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2362 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 2363 | root_item.drop_level = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2364 | |
| 2365 | free_extent_buffer(leaf); |
| 2366 | leaf = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2367 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2368 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 2369 | |
| 2370 | key.objectid = objectid; |
| 2371 | key.offset = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2372 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2373 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2374 | &root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2375 | if (ret) |
| 2376 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2377 | |
| 2378 | /* |
| 2379 | * insert the directory item |
| 2380 | */ |
| 2381 | key.offset = (u64)-1; |
| 2382 | dir = root->fs_info->sb->s_root->d_inode; |
| 2383 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2384 | name, namelen, dir->i_ino, &key, |
| 2385 | BTRFS_FT_DIR); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2386 | if (ret) |
| 2387 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2388 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2389 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
| 2390 | name, namelen, objectid, |
| 2391 | root->fs_info->sb->s_root->d_inode->i_ino); |
| 2392 | if (ret) |
| 2393 | goto fail; |
| 2394 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2395 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2396 | if (ret) |
| 2397 | goto fail_commit; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2398 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2399 | new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2400 | BUG_ON(!new_root); |
| 2401 | |
| 2402 | trans = btrfs_start_transaction(new_root, 1); |
| 2403 | BUG_ON(!trans); |
| 2404 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2405 | inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid, |
| 2406 | new_dirid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2407 | BTRFS_I(dir)->block_group, S_IFDIR | 0700); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2408 | if (IS_ERR(inode)) |
| 2409 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2410 | inode->i_op = &btrfs_dir_inode_operations; |
| 2411 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 2412 | new_root->inode = inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2413 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2414 | ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid, |
| 2415 | new_dirid); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2416 | inode->i_nlink = 1; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2417 | inode->i_size = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2418 | ret = btrfs_update_inode(trans, new_root, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2419 | if (ret) |
| 2420 | goto fail; |
| 2421 | fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2422 | nr = trans->blocks_used; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2423 | err = btrfs_commit_transaction(trans, new_root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2424 | if (err && !ret) |
| 2425 | ret = err; |
| 2426 | fail_commit: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2427 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2428 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2429 | btrfs_throttle(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2430 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 2434 | { |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2435 | struct btrfs_pending_snapshot *pending_snapshot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2436 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2437 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2438 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2439 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2440 | |
| 2441 | if (!root->ref_cows) |
| 2442 | return -EINVAL; |
| 2443 | |
| 2444 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2445 | ret = btrfs_check_free_space(root, 1, 0); |
| 2446 | if (ret) |
| 2447 | goto fail_unlock; |
| 2448 | |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2449 | pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 2450 | if (!pending_snapshot) { |
| 2451 | ret = -ENOMEM; |
| 2452 | goto fail_unlock; |
| 2453 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 2454 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2455 | if (!pending_snapshot->name) { |
| 2456 | ret = -ENOMEM; |
| 2457 | kfree(pending_snapshot); |
| 2458 | goto fail_unlock; |
| 2459 | } |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 2460 | memcpy(pending_snapshot->name, name, namelen); |
| 2461 | pending_snapshot->name[namelen] = '\0'; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2462 | trans = btrfs_start_transaction(root, 1); |
| 2463 | BUG_ON(!trans); |
Chris Mason | 3063d29 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2464 | pending_snapshot->root = root; |
| 2465 | list_add(&pending_snapshot->list, |
| 2466 | &trans->transaction->pending_snapshots); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2467 | ret = btrfs_update_inode(trans, root, root->inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2468 | err = btrfs_commit_transaction(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2469 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2470 | fail_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2471 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2472 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2473 | btrfs_throttle(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2474 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2475 | } |
| 2476 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2477 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2478 | struct file_ra_state *ra, struct file *file, |
| 2479 | pgoff_t offset, pgoff_t last_index) |
| 2480 | { |
| 2481 | pgoff_t req_size; |
| 2482 | |
| 2483 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 2484 | req_size = last_index - offset + 1; |
| 2485 | offset = page_cache_readahead(mapping, ra, file, offset, req_size); |
| 2486 | return offset; |
| 2487 | #else |
| 2488 | req_size = min(last_index - offset + 1, (pgoff_t)128); |
| 2489 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 2490 | return offset + req_size; |
| 2491 | #endif |
| 2492 | } |
| 2493 | |
| 2494 | int btrfs_defrag_file(struct file *file) { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2495 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2496 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2497 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2498 | struct page *page; |
| 2499 | unsigned long last_index; |
| 2500 | unsigned long ra_index = 0; |
| 2501 | u64 page_start; |
| 2502 | u64 page_end; |
| 2503 | unsigned long i; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2504 | int ret; |
| 2505 | |
| 2506 | mutex_lock(&root->fs_info->fs_mutex); |
| 2507 | ret = btrfs_check_free_space(root, inode->i_size, 0); |
| 2508 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2509 | if (ret) |
| 2510 | return -ENOSPC; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2511 | |
| 2512 | mutex_lock(&inode->i_mutex); |
| 2513 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 2514 | for (i = 0; i <= last_index; i++) { |
| 2515 | if (i == ra_index) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2516 | ra_index = btrfs_force_ra(inode->i_mapping, |
| 2517 | &file->f_ra, |
| 2518 | file, ra_index, last_index); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2519 | } |
| 2520 | page = grab_cache_page(inode->i_mapping, i); |
| 2521 | if (!page) |
| 2522 | goto out_unlock; |
| 2523 | if (!PageUptodate(page)) { |
| 2524 | btrfs_readpage(NULL, page); |
| 2525 | lock_page(page); |
| 2526 | if (!PageUptodate(page)) { |
| 2527 | unlock_page(page); |
| 2528 | page_cache_release(page); |
| 2529 | goto out_unlock; |
| 2530 | } |
| 2531 | } |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 2532 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2533 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2534 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2535 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2536 | set_extent_delalloc(io_tree, page_start, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2537 | page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2538 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2539 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2540 | set_page_dirty(page); |
| 2541 | unlock_page(page); |
| 2542 | page_cache_release(page); |
| 2543 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 2544 | } |
| 2545 | |
| 2546 | out_unlock: |
| 2547 | mutex_unlock(&inode->i_mutex); |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2551 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
| 2552 | { |
| 2553 | u64 new_size; |
| 2554 | u64 old_size; |
| 2555 | struct btrfs_ioctl_vol_args *vol_args; |
| 2556 | struct btrfs_trans_handle *trans; |
| 2557 | char *sizestr; |
| 2558 | int ret = 0; |
| 2559 | int namelen; |
| 2560 | int mod = 0; |
| 2561 | |
| 2562 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 2563 | |
| 2564 | if (!vol_args) |
| 2565 | return -ENOMEM; |
| 2566 | |
| 2567 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 2568 | ret = -EFAULT; |
| 2569 | goto out; |
| 2570 | } |
| 2571 | namelen = strlen(vol_args->name); |
| 2572 | if (namelen > BTRFS_VOL_NAME_MAX) { |
| 2573 | ret = -EINVAL; |
| 2574 | goto out; |
| 2575 | } |
| 2576 | |
| 2577 | sizestr = vol_args->name; |
| 2578 | if (!strcmp(sizestr, "max")) |
| 2579 | new_size = root->fs_info->sb->s_bdev->bd_inode->i_size; |
| 2580 | else { |
| 2581 | if (sizestr[0] == '-') { |
| 2582 | mod = -1; |
| 2583 | sizestr++; |
| 2584 | } else if (sizestr[0] == '+') { |
| 2585 | mod = 1; |
| 2586 | sizestr++; |
| 2587 | } |
| 2588 | new_size = btrfs_parse_size(sizestr); |
| 2589 | if (new_size == 0) { |
| 2590 | ret = -EINVAL; |
| 2591 | goto out; |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | mutex_lock(&root->fs_info->fs_mutex); |
| 2596 | old_size = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 2597 | |
| 2598 | if (mod < 0) { |
| 2599 | if (new_size > old_size) { |
| 2600 | ret = -EINVAL; |
| 2601 | goto out_unlock; |
| 2602 | } |
| 2603 | new_size = old_size - new_size; |
| 2604 | } else if (mod > 0) { |
| 2605 | new_size = old_size + new_size; |
| 2606 | } |
| 2607 | |
| 2608 | if (new_size < 256 * 1024 * 1024) { |
| 2609 | ret = -EINVAL; |
| 2610 | goto out_unlock; |
| 2611 | } |
| 2612 | if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) { |
| 2613 | ret = -EFBIG; |
| 2614 | goto out_unlock; |
| 2615 | } |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 2616 | |
| 2617 | do_div(new_size, root->sectorsize); |
| 2618 | new_size *= root->sectorsize; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2619 | |
| 2620 | printk("new size is %Lu\n", new_size); |
| 2621 | if (new_size > old_size) { |
| 2622 | trans = btrfs_start_transaction(root, 1); |
| 2623 | ret = btrfs_grow_extent_tree(trans, root, new_size); |
| 2624 | btrfs_commit_transaction(trans, root); |
| 2625 | } else { |
| 2626 | ret = btrfs_shrink_extent_tree(root, new_size); |
| 2627 | } |
| 2628 | |
| 2629 | out_unlock: |
| 2630 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2631 | out: |
| 2632 | kfree(vol_args); |
| 2633 | return ret; |
| 2634 | } |
| 2635 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2636 | static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root, |
| 2637 | void __user *arg) |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2638 | { |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2639 | struct btrfs_ioctl_vol_args *vol_args; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2640 | struct btrfs_dir_item *di; |
| 2641 | struct btrfs_path *path; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2642 | u64 root_dirid; |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2643 | int namelen; |
| 2644 | int ret; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2645 | |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2646 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2647 | |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2648 | if (!vol_args) |
| 2649 | return -ENOMEM; |
| 2650 | |
| 2651 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 2652 | ret = -EFAULT; |
| 2653 | goto out; |
| 2654 | } |
| 2655 | |
| 2656 | namelen = strlen(vol_args->name); |
| 2657 | if (namelen > BTRFS_VOL_NAME_MAX) { |
| 2658 | ret = -EINVAL; |
| 2659 | goto out; |
| 2660 | } |
| 2661 | if (strchr(vol_args->name, '/')) { |
| 2662 | ret = -EINVAL; |
| 2663 | goto out; |
| 2664 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2665 | |
| 2666 | path = btrfs_alloc_path(); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2667 | if (!path) { |
| 2668 | ret = -ENOMEM; |
| 2669 | goto out; |
| 2670 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2671 | |
| 2672 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
| 2673 | mutex_lock(&root->fs_info->fs_mutex); |
| 2674 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 2675 | path, root_dirid, |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2676 | vol_args->name, namelen, 0); |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2677 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2678 | btrfs_free_path(path); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2679 | |
| 2680 | if (di && !IS_ERR(di)) { |
| 2681 | ret = -EEXIST; |
| 2682 | goto out; |
| 2683 | } |
| 2684 | |
| 2685 | if (IS_ERR(di)) { |
| 2686 | ret = PTR_ERR(di); |
| 2687 | goto out; |
| 2688 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2689 | |
| 2690 | if (root == root->fs_info->tree_root) |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 2691 | ret = create_subvol(root, vol_args->name, namelen); |
| 2692 | else |
| 2693 | ret = create_snapshot(root, vol_args->name, namelen); |
| 2694 | out: |
| 2695 | kfree(vol_args); |
| 2696 | return ret; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | static int btrfs_ioctl_defrag(struct file *file) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2700 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2701 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2702 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2703 | |
| 2704 | switch (inode->i_mode & S_IFMT) { |
| 2705 | case S_IFDIR: |
| 2706 | mutex_lock(&root->fs_info->fs_mutex); |
| 2707 | btrfs_defrag_root(root, 0); |
| 2708 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
| 2709 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2710 | break; |
| 2711 | case S_IFREG: |
| 2712 | btrfs_defrag_file(file); |
| 2713 | break; |
| 2714 | } |
| 2715 | |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
| 2719 | long btrfs_ioctl(struct file *file, unsigned int |
| 2720 | cmd, unsigned long arg) |
| 2721 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2722 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2723 | |
| 2724 | switch (cmd) { |
| 2725 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2726 | return btrfs_ioctl_snap_create(root, (void __user *)arg); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2727 | case BTRFS_IOC_DEFRAG: |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2728 | return btrfs_ioctl_defrag(file); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2729 | case BTRFS_IOC_RESIZE: |
| 2730 | return btrfs_ioctl_resize(root, (void __user *)arg); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2731 | } |
Christoph Hellwig | d03581f | 2007-09-14 10:22:57 -0400 | [diff] [blame] | 2732 | |
| 2733 | return -ENOTTY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2734 | } |
| 2735 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2736 | /* |
| 2737 | * Called inside transaction, so use GFP_NOFS |
| 2738 | */ |
| 2739 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 2740 | { |
| 2741 | struct btrfs_inode *ei; |
| 2742 | |
| 2743 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 2744 | if (!ei) |
| 2745 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2746 | ei->last_trans = 0; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2747 | ei->ordered_trans = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2748 | return &ei->vfs_inode; |
| 2749 | } |
| 2750 | |
| 2751 | void btrfs_destroy_inode(struct inode *inode) |
| 2752 | { |
| 2753 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 2754 | WARN_ON(inode->i_data.nrpages); |
| 2755 | |
Chris Mason | 8c416c9 | 2008-01-14 15:10:26 -0500 | [diff] [blame] | 2756 | btrfs_drop_extent_cache(inode, 0, (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2757 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 2758 | } |
| 2759 | |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 2760 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 2761 | static void init_once(struct kmem_cache * cachep, void *foo) |
| 2762 | #else |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2763 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 2764 | unsigned long flags) |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 2765 | #endif |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2766 | { |
| 2767 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 2768 | |
| 2769 | inode_init_once(&ei->vfs_inode); |
| 2770 | } |
| 2771 | |
| 2772 | void btrfs_destroy_cachep(void) |
| 2773 | { |
| 2774 | if (btrfs_inode_cachep) |
| 2775 | kmem_cache_destroy(btrfs_inode_cachep); |
| 2776 | if (btrfs_trans_handle_cachep) |
| 2777 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 2778 | if (btrfs_transaction_cachep) |
| 2779 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 2780 | if (btrfs_bit_radix_cachep) |
| 2781 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 2782 | if (btrfs_path_cachep) |
| 2783 | kmem_cache_destroy(btrfs_path_cachep); |
| 2784 | } |
| 2785 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2786 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2787 | unsigned long extra_flags, |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 2788 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
| 2789 | void (*ctor)(struct kmem_cache *, void *) |
| 2790 | #else |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2791 | void (*ctor)(void *, struct kmem_cache *, |
Chris Mason | 44ec0b7 | 2007-10-29 10:55:05 -0400 | [diff] [blame] | 2792 | unsigned long) |
| 2793 | #endif |
| 2794 | ) |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2795 | { |
| 2796 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | |
| 2797 | SLAB_MEM_SPREAD | extra_flags), ctor |
| 2798 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 2799 | ,NULL |
| 2800 | #endif |
| 2801 | ); |
| 2802 | } |
| 2803 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2804 | int btrfs_init_cachep(void) |
| 2805 | { |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2806 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2807 | sizeof(struct btrfs_inode), |
| 2808 | 0, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2809 | if (!btrfs_inode_cachep) |
| 2810 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2811 | btrfs_trans_handle_cachep = |
| 2812 | btrfs_cache_create("btrfs_trans_handle_cache", |
| 2813 | sizeof(struct btrfs_trans_handle), |
| 2814 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2815 | if (!btrfs_trans_handle_cachep) |
| 2816 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2817 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2818 | sizeof(struct btrfs_transaction), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2819 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2820 | if (!btrfs_transaction_cachep) |
| 2821 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2822 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
Yan | 2322358 | 2007-09-17 11:08:52 -0400 | [diff] [blame] | 2823 | sizeof(struct btrfs_path), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2824 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2825 | if (!btrfs_path_cachep) |
| 2826 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 2827 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2828 | SLAB_DESTROY_BY_RCU, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2829 | if (!btrfs_bit_radix_cachep) |
| 2830 | goto fail; |
| 2831 | return 0; |
| 2832 | fail: |
| 2833 | btrfs_destroy_cachep(); |
| 2834 | return -ENOMEM; |
| 2835 | } |
| 2836 | |
| 2837 | static int btrfs_getattr(struct vfsmount *mnt, |
| 2838 | struct dentry *dentry, struct kstat *stat) |
| 2839 | { |
| 2840 | struct inode *inode = dentry->d_inode; |
| 2841 | generic_fillattr(inode, stat); |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 2842 | stat->blksize = PAGE_CACHE_SIZE; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2843 | stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2844 | return 0; |
| 2845 | } |
| 2846 | |
| 2847 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, |
| 2848 | struct inode * new_dir,struct dentry *new_dentry) |
| 2849 | { |
| 2850 | struct btrfs_trans_handle *trans; |
| 2851 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 2852 | struct inode *new_inode = new_dentry->d_inode; |
| 2853 | struct inode *old_inode = old_dentry->d_inode; |
| 2854 | struct timespec ctime = CURRENT_TIME; |
| 2855 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2856 | int ret; |
| 2857 | |
| 2858 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
| 2859 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
| 2860 | return -ENOTEMPTY; |
| 2861 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2862 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2863 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2864 | ret = btrfs_check_free_space(root, 1, 0); |
| 2865 | if (ret) |
| 2866 | goto out_unlock; |
| 2867 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2868 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2869 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2870 | btrfs_set_trans_block_group(trans, new_dir); |
| 2871 | path = btrfs_alloc_path(); |
| 2872 | if (!path) { |
| 2873 | ret = -ENOMEM; |
| 2874 | goto out_fail; |
| 2875 | } |
| 2876 | |
| 2877 | old_dentry->d_inode->i_nlink++; |
| 2878 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 2879 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 2880 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2881 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2882 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
| 2883 | if (ret) |
| 2884 | goto out_fail; |
| 2885 | |
| 2886 | if (new_inode) { |
| 2887 | new_inode->i_ctime = CURRENT_TIME; |
| 2888 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); |
| 2889 | if (ret) |
| 2890 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2891 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2892 | ret = btrfs_add_link(trans, new_dentry, old_inode, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2893 | if (ret) |
| 2894 | goto out_fail; |
| 2895 | |
| 2896 | out_fail: |
| 2897 | btrfs_free_path(path); |
| 2898 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2899 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2900 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2901 | return ret; |
| 2902 | } |
| 2903 | |
| 2904 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 2905 | const char *symname) |
| 2906 | { |
| 2907 | struct btrfs_trans_handle *trans; |
| 2908 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2909 | struct btrfs_path *path; |
| 2910 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2911 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2912 | int err; |
| 2913 | int drop_inode = 0; |
| 2914 | u64 objectid; |
| 2915 | int name_len; |
| 2916 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2917 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2918 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2919 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2920 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2921 | |
| 2922 | name_len = strlen(symname) + 1; |
| 2923 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 2924 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2925 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2926 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2927 | err = btrfs_check_free_space(root, 1, 0); |
| 2928 | if (err) |
| 2929 | goto out_fail; |
| 2930 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2931 | trans = btrfs_start_transaction(root, 1); |
| 2932 | btrfs_set_trans_block_group(trans, dir); |
| 2933 | |
| 2934 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2935 | if (err) { |
| 2936 | err = -ENOSPC; |
| 2937 | goto out_unlock; |
| 2938 | } |
| 2939 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2940 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
| 2941 | dentry->d_name.len, |
| 2942 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2943 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO); |
| 2944 | err = PTR_ERR(inode); |
| 2945 | if (IS_ERR(inode)) |
| 2946 | goto out_unlock; |
| 2947 | |
| 2948 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2949 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2950 | if (err) |
| 2951 | drop_inode = 1; |
| 2952 | else { |
| 2953 | inode->i_mapping->a_ops = &btrfs_aops; |
| 2954 | inode->i_fop = &btrfs_file_operations; |
| 2955 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2956 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 2957 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2958 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 2959 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2960 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2961 | } |
| 2962 | dir->i_sb->s_dirt = 1; |
| 2963 | btrfs_update_inode_block_group(trans, inode); |
| 2964 | btrfs_update_inode_block_group(trans, dir); |
| 2965 | if (drop_inode) |
| 2966 | goto out_unlock; |
| 2967 | |
| 2968 | path = btrfs_alloc_path(); |
| 2969 | BUG_ON(!path); |
| 2970 | key.objectid = inode->i_ino; |
| 2971 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2972 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 2973 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 2974 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 2975 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2976 | if (err) { |
| 2977 | drop_inode = 1; |
| 2978 | goto out_unlock; |
| 2979 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2980 | leaf = path->nodes[0]; |
| 2981 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 2982 | struct btrfs_file_extent_item); |
| 2983 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 2984 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2985 | BTRFS_FILE_EXTENT_INLINE); |
| 2986 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2987 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 2988 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2989 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2990 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2991 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2992 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 2993 | inode->i_size = name_len - 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2994 | err = btrfs_update_inode(trans, root, inode); |
| 2995 | if (err) |
| 2996 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2997 | |
| 2998 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2999 | nr = trans->blocks_used; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3000 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3001 | out_fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3002 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3003 | if (drop_inode) { |
| 3004 | inode_dec_link_count(inode); |
| 3005 | iput(inode); |
| 3006 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3007 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | e2008b6 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3008 | btrfs_throttle(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3009 | return err; |
| 3010 | } |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3011 | static int btrfs_permission(struct inode *inode, int mask, |
| 3012 | struct nameidata *nd) |
| 3013 | { |
| 3014 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) |
| 3015 | return -EACCES; |
| 3016 | return generic_permission(inode, mask, NULL); |
| 3017 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3018 | |
| 3019 | static struct inode_operations btrfs_dir_inode_operations = { |
| 3020 | .lookup = btrfs_lookup, |
| 3021 | .create = btrfs_create, |
| 3022 | .unlink = btrfs_unlink, |
| 3023 | .link = btrfs_link, |
| 3024 | .mkdir = btrfs_mkdir, |
| 3025 | .rmdir = btrfs_rmdir, |
| 3026 | .rename = btrfs_rename, |
| 3027 | .symlink = btrfs_symlink, |
| 3028 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3029 | .mknod = btrfs_mknod, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3030 | .setxattr = generic_setxattr, |
| 3031 | .getxattr = generic_getxattr, |
| 3032 | .listxattr = btrfs_listxattr, |
| 3033 | .removexattr = generic_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3034 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3035 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3036 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 3037 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3038 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3039 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3040 | static struct file_operations btrfs_dir_file_operations = { |
| 3041 | .llseek = generic_file_llseek, |
| 3042 | .read = generic_read_dir, |
| 3043 | .readdir = btrfs_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3044 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3045 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3046 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3047 | #endif |
| 3048 | }; |
| 3049 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3050 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3051 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 3052 | // .writepage_io_hook = btrfs_writepage_io_hook, |
| 3053 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3054 | .readpage_io_hook = btrfs_readpage_io_hook, |
| 3055 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 3056 | .set_bit_hook = btrfs_set_bit_hook, |
| 3057 | .clear_bit_hook = btrfs_clear_bit_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3058 | }; |
| 3059 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3060 | static struct address_space_operations btrfs_aops = { |
| 3061 | .readpage = btrfs_readpage, |
| 3062 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 3063 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 3064 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3065 | .sync_page = block_sync_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3066 | .bmap = btrfs_bmap, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3067 | .invalidatepage = btrfs_invalidatepage, |
| 3068 | .releasepage = btrfs_releasepage, |
| 3069 | .set_page_dirty = __set_page_dirty_nobuffers, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3070 | }; |
| 3071 | |
| 3072 | static struct address_space_operations btrfs_symlink_aops = { |
| 3073 | .readpage = btrfs_readpage, |
| 3074 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 3075 | .invalidatepage = btrfs_invalidatepage, |
| 3076 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3077 | }; |
| 3078 | |
| 3079 | static struct inode_operations btrfs_file_inode_operations = { |
| 3080 | .truncate = btrfs_truncate, |
| 3081 | .getattr = btrfs_getattr, |
| 3082 | .setattr = btrfs_setattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3083 | .setxattr = generic_setxattr, |
| 3084 | .getxattr = generic_getxattr, |
| 3085 | .listxattr = btrfs_listxattr, |
| 3086 | .removexattr = generic_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3087 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3088 | }; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3089 | static struct inode_operations btrfs_special_inode_operations = { |
| 3090 | .getattr = btrfs_getattr, |
| 3091 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3092 | .permission = btrfs_permission, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3093 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3094 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 3095 | .readlink = generic_readlink, |
| 3096 | .follow_link = page_follow_link_light, |
| 3097 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3098 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3099 | }; |