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