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