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