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