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> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 35 | #include "ctree.h" |
| 36 | #include "disk-io.h" |
| 37 | #include "transaction.h" |
| 38 | #include "btrfs_inode.h" |
| 39 | #include "ioctl.h" |
| 40 | #include "print-tree.h" |
| 41 | |
| 42 | struct btrfs_iget_args { |
| 43 | u64 ino; |
| 44 | struct btrfs_root *root; |
| 45 | }; |
| 46 | |
| 47 | static struct inode_operations btrfs_dir_inode_operations; |
| 48 | static struct inode_operations btrfs_symlink_inode_operations; |
| 49 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 50 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | static struct inode_operations btrfs_file_inode_operations; |
| 52 | static struct address_space_operations btrfs_aops; |
| 53 | static struct address_space_operations btrfs_symlink_aops; |
| 54 | static struct file_operations btrfs_dir_file_operations; |
| 55 | |
| 56 | static struct kmem_cache *btrfs_inode_cachep; |
| 57 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 58 | struct kmem_cache *btrfs_transaction_cachep; |
| 59 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 60 | struct kmem_cache *btrfs_path_cachep; |
| 61 | |
| 62 | #define S_SHIFT 12 |
| 63 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 64 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 65 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 66 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 67 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 68 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 69 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 70 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 71 | }; |
| 72 | |
| 73 | void btrfs_read_locked_inode(struct inode *inode) |
| 74 | { |
| 75 | struct btrfs_path *path; |
| 76 | struct btrfs_inode_item *inode_item; |
| 77 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 78 | struct btrfs_key location; |
| 79 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 80 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 81 | int ret; |
| 82 | |
| 83 | path = btrfs_alloc_path(); |
| 84 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 85 | mutex_lock(&root->fs_info->fs_mutex); |
| 86 | |
| 87 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
| 88 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
| 89 | if (ret) { |
| 90 | btrfs_free_path(path); |
| 91 | goto make_bad; |
| 92 | } |
| 93 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 94 | path->slots[0], |
| 95 | struct btrfs_inode_item); |
| 96 | |
| 97 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 98 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 99 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 100 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 101 | inode->i_size = btrfs_inode_size(inode_item); |
| 102 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 103 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 104 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 105 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 106 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 107 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 108 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 109 | inode->i_generation = btrfs_inode_generation(inode_item); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 110 | inode->i_rdev = 0; |
| 111 | rdev = btrfs_inode_rdev(inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 112 | alloc_group_block = btrfs_inode_block_group(inode_item); |
| 113 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
| 114 | alloc_group_block); |
| 115 | |
| 116 | btrfs_free_path(path); |
| 117 | inode_item = NULL; |
| 118 | |
| 119 | mutex_unlock(&root->fs_info->fs_mutex); |
| 120 | |
| 121 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 122 | case S_IFREG: |
| 123 | inode->i_mapping->a_ops = &btrfs_aops; |
| 124 | inode->i_fop = &btrfs_file_operations; |
| 125 | inode->i_op = &btrfs_file_inode_operations; |
| 126 | break; |
| 127 | case S_IFDIR: |
| 128 | inode->i_fop = &btrfs_dir_file_operations; |
| 129 | if (root == root->fs_info->tree_root) |
| 130 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 131 | else |
| 132 | inode->i_op = &btrfs_dir_inode_operations; |
| 133 | break; |
| 134 | case S_IFLNK: |
| 135 | inode->i_op = &btrfs_symlink_inode_operations; |
| 136 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 137 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 138 | default: |
| 139 | init_special_inode(inode, inode->i_mode, rdev); |
| 140 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 141 | } |
| 142 | return; |
| 143 | |
| 144 | make_bad: |
| 145 | btrfs_release_path(root, path); |
| 146 | btrfs_free_path(path); |
| 147 | mutex_unlock(&root->fs_info->fs_mutex); |
| 148 | make_bad_inode(inode); |
| 149 | } |
| 150 | |
| 151 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 152 | struct inode *inode) |
| 153 | { |
| 154 | btrfs_set_inode_uid(item, inode->i_uid); |
| 155 | btrfs_set_inode_gid(item, inode->i_gid); |
| 156 | btrfs_set_inode_size(item, inode->i_size); |
| 157 | btrfs_set_inode_mode(item, inode->i_mode); |
| 158 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 159 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 160 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 161 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 162 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 163 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 164 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 165 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 166 | btrfs_set_inode_generation(item, inode->i_generation); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 167 | btrfs_set_inode_rdev(item, inode->i_rdev); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 168 | btrfs_set_inode_block_group(item, |
| 169 | BTRFS_I(inode)->block_group->key.objectid); |
| 170 | } |
| 171 | |
| 172 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 173 | struct btrfs_root *root, |
| 174 | struct inode *inode) |
| 175 | { |
| 176 | struct btrfs_inode_item *inode_item; |
| 177 | struct btrfs_path *path; |
| 178 | int ret; |
| 179 | |
| 180 | path = btrfs_alloc_path(); |
| 181 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 182 | ret = btrfs_lookup_inode(trans, root, path, |
| 183 | &BTRFS_I(inode)->location, 1); |
| 184 | if (ret) { |
| 185 | if (ret > 0) |
| 186 | ret = -ENOENT; |
| 187 | goto failed; |
| 188 | } |
| 189 | |
| 190 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 191 | path->slots[0], |
| 192 | struct btrfs_inode_item); |
| 193 | |
| 194 | fill_inode_item(inode_item, inode); |
| 195 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame^] | 196 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 197 | ret = 0; |
| 198 | failed: |
| 199 | btrfs_release_path(root, path); |
| 200 | btrfs_free_path(path); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 206 | struct btrfs_root *root, |
| 207 | struct inode *dir, |
| 208 | struct dentry *dentry) |
| 209 | { |
| 210 | struct btrfs_path *path; |
| 211 | const char *name = dentry->d_name.name; |
| 212 | int name_len = dentry->d_name.len; |
| 213 | int ret = 0; |
| 214 | u64 objectid; |
| 215 | struct btrfs_dir_item *di; |
| 216 | |
| 217 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 218 | if (!path) { |
| 219 | ret = -ENOMEM; |
| 220 | goto err; |
| 221 | } |
| 222 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 223 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 224 | name, name_len, -1); |
| 225 | if (IS_ERR(di)) { |
| 226 | ret = PTR_ERR(di); |
| 227 | goto err; |
| 228 | } |
| 229 | if (!di) { |
| 230 | ret = -ENOENT; |
| 231 | goto err; |
| 232 | } |
| 233 | objectid = btrfs_disk_key_objectid(&di->location); |
| 234 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 235 | if (ret) |
| 236 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 237 | btrfs_release_path(root, path); |
| 238 | |
| 239 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 240 | objectid, name, name_len, -1); |
| 241 | if (IS_ERR(di)) { |
| 242 | ret = PTR_ERR(di); |
| 243 | goto err; |
| 244 | } |
| 245 | if (!di) { |
| 246 | ret = -ENOENT; |
| 247 | goto err; |
| 248 | } |
| 249 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 250 | |
| 251 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 252 | err: |
| 253 | btrfs_free_path(path); |
| 254 | if (!ret) { |
| 255 | dir->i_size -= name_len * 2; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 256 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 257 | btrfs_update_inode(trans, root, dir); |
| 258 | drop_nlink(dentry->d_inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 259 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 260 | dir->i_sb->s_dirt = 1; |
| 261 | } |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 266 | { |
| 267 | struct btrfs_root *root; |
| 268 | struct btrfs_trans_handle *trans; |
| 269 | int ret; |
| 270 | |
| 271 | root = BTRFS_I(dir)->root; |
| 272 | mutex_lock(&root->fs_info->fs_mutex); |
| 273 | trans = btrfs_start_transaction(root, 1); |
| 274 | btrfs_set_trans_block_group(trans, dir); |
| 275 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 276 | btrfs_end_transaction(trans, root); |
| 277 | mutex_unlock(&root->fs_info->fs_mutex); |
| 278 | btrfs_btree_balance_dirty(root); |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 283 | { |
| 284 | struct inode *inode = dentry->d_inode; |
| 285 | int err; |
| 286 | int ret; |
| 287 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 288 | struct btrfs_path *path; |
| 289 | struct btrfs_key key; |
| 290 | struct btrfs_trans_handle *trans; |
| 291 | struct btrfs_key found_key; |
| 292 | int found_type; |
| 293 | struct btrfs_leaf *leaf; |
| 294 | char *goodnames = ".."; |
| 295 | |
| 296 | path = btrfs_alloc_path(); |
| 297 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 298 | mutex_lock(&root->fs_info->fs_mutex); |
| 299 | trans = btrfs_start_transaction(root, 1); |
| 300 | btrfs_set_trans_block_group(trans, dir); |
| 301 | key.objectid = inode->i_ino; |
| 302 | key.offset = (u64)-1; |
| 303 | key.flags = (u32)-1; |
| 304 | while(1) { |
| 305 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 306 | if (ret < 0) { |
| 307 | err = ret; |
| 308 | goto out; |
| 309 | } |
| 310 | BUG_ON(ret == 0); |
| 311 | if (path->slots[0] == 0) { |
| 312 | err = -ENOENT; |
| 313 | goto out; |
| 314 | } |
| 315 | path->slots[0]--; |
| 316 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 317 | btrfs_disk_key_to_cpu(&found_key, |
| 318 | &leaf->items[path->slots[0]].key); |
| 319 | found_type = btrfs_key_type(&found_key); |
| 320 | if (found_key.objectid != inode->i_ino) { |
| 321 | err = -ENOENT; |
| 322 | goto out; |
| 323 | } |
| 324 | if ((found_type != BTRFS_DIR_ITEM_KEY && |
| 325 | found_type != BTRFS_DIR_INDEX_KEY) || |
| 326 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && |
| 327 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { |
| 328 | err = -ENOTEMPTY; |
| 329 | goto out; |
| 330 | } |
| 331 | ret = btrfs_del_item(trans, root, path); |
| 332 | BUG_ON(ret); |
| 333 | |
| 334 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
| 335 | break; |
| 336 | btrfs_release_path(root, path); |
| 337 | } |
| 338 | ret = 0; |
| 339 | btrfs_release_path(root, path); |
| 340 | |
| 341 | /* now the directory is empty */ |
| 342 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 343 | if (!err) { |
| 344 | inode->i_size = 0; |
| 345 | } |
| 346 | out: |
| 347 | btrfs_release_path(root, path); |
| 348 | btrfs_free_path(path); |
| 349 | mutex_unlock(&root->fs_info->fs_mutex); |
| 350 | ret = btrfs_end_transaction(trans, root); |
| 351 | btrfs_btree_balance_dirty(root); |
| 352 | if (ret && !err) |
| 353 | err = ret; |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 358 | struct btrfs_root *root, |
| 359 | struct inode *inode) |
| 360 | { |
| 361 | struct btrfs_path *path; |
| 362 | int ret; |
| 363 | |
| 364 | clear_inode(inode); |
| 365 | |
| 366 | path = btrfs_alloc_path(); |
| 367 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 368 | ret = btrfs_lookup_inode(trans, root, path, |
| 369 | &BTRFS_I(inode)->location, -1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 370 | if (ret > 0) |
| 371 | ret = -ENOENT; |
| 372 | if (!ret) |
| 373 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 374 | btrfs_free_path(path); |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 379 | * this can truncate away extent items, csum items and directory items. |
| 380 | * It starts at a high offset and removes keys until it can't find |
| 381 | * any higher than i_size. |
| 382 | * |
| 383 | * csum items that cross the new i_size are truncated to the new size |
| 384 | * as well. |
| 385 | */ |
| 386 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 387 | struct btrfs_root *root, |
| 388 | struct inode *inode) |
| 389 | { |
| 390 | int ret; |
| 391 | struct btrfs_path *path; |
| 392 | struct btrfs_key key; |
| 393 | struct btrfs_disk_key *found_key; |
| 394 | u32 found_type; |
| 395 | struct btrfs_leaf *leaf; |
| 396 | struct btrfs_file_extent_item *fi; |
| 397 | u64 extent_start = 0; |
| 398 | u64 extent_num_blocks = 0; |
| 399 | u64 item_end = 0; |
| 400 | int found_extent; |
| 401 | int del_item; |
| 402 | |
| 403 | path = btrfs_alloc_path(); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 404 | path->reada = -1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 405 | BUG_ON(!path); |
| 406 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 407 | key.objectid = inode->i_ino; |
| 408 | key.offset = (u64)-1; |
| 409 | key.flags = (u32)-1; |
| 410 | while(1) { |
| 411 | btrfs_init_path(path); |
| 412 | fi = NULL; |
| 413 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 414 | if (ret < 0) { |
| 415 | goto error; |
| 416 | } |
| 417 | if (ret > 0) { |
| 418 | BUG_ON(path->slots[0] == 0); |
| 419 | path->slots[0]--; |
| 420 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 421 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 422 | found_key = &leaf->items[path->slots[0]].key; |
| 423 | found_type = btrfs_disk_key_type(found_key); |
| 424 | |
| 425 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 426 | break; |
| 427 | if (found_type != BTRFS_CSUM_ITEM_KEY && |
| 428 | found_type != BTRFS_DIR_ITEM_KEY && |
| 429 | found_type != BTRFS_DIR_INDEX_KEY && |
| 430 | found_type != BTRFS_EXTENT_DATA_KEY) |
| 431 | break; |
| 432 | |
| 433 | item_end = btrfs_disk_key_offset(found_key); |
| 434 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
| 435 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 436 | path->slots[0], |
| 437 | struct btrfs_file_extent_item); |
| 438 | if (btrfs_file_extent_type(fi) != |
| 439 | BTRFS_FILE_EXTENT_INLINE) { |
| 440 | item_end += btrfs_file_extent_num_blocks(fi) << |
| 441 | inode->i_blkbits; |
| 442 | } |
| 443 | } |
| 444 | if (found_type == BTRFS_CSUM_ITEM_KEY) { |
| 445 | ret = btrfs_csum_truncate(trans, root, path, |
| 446 | inode->i_size); |
| 447 | BUG_ON(ret); |
| 448 | } |
| 449 | if (item_end < inode->i_size) { |
| 450 | if (found_type) { |
| 451 | btrfs_set_key_type(&key, found_type - 1); |
| 452 | continue; |
| 453 | } |
| 454 | break; |
| 455 | } |
| 456 | if (btrfs_disk_key_offset(found_key) >= inode->i_size) |
| 457 | del_item = 1; |
| 458 | else |
| 459 | del_item = 0; |
| 460 | found_extent = 0; |
| 461 | |
| 462 | /* FIXME, shrink the extent if the ref count is only 1 */ |
| 463 | if (found_type == BTRFS_EXTENT_DATA_KEY && |
| 464 | btrfs_file_extent_type(fi) != |
| 465 | BTRFS_FILE_EXTENT_INLINE) { |
| 466 | u64 num_dec; |
| 467 | if (!del_item) { |
| 468 | u64 orig_num_blocks = |
| 469 | btrfs_file_extent_num_blocks(fi); |
| 470 | extent_num_blocks = inode->i_size - |
| 471 | btrfs_disk_key_offset(found_key) + |
| 472 | root->blocksize - 1; |
| 473 | extent_num_blocks >>= inode->i_blkbits; |
| 474 | btrfs_set_file_extent_num_blocks(fi, |
| 475 | extent_num_blocks); |
| 476 | inode->i_blocks -= (orig_num_blocks - |
| 477 | extent_num_blocks) << 3; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 478 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 479 | } else { |
| 480 | extent_start = |
| 481 | btrfs_file_extent_disk_blocknr(fi); |
| 482 | extent_num_blocks = |
| 483 | btrfs_file_extent_disk_num_blocks(fi); |
| 484 | /* FIXME blocksize != 4096 */ |
| 485 | num_dec = btrfs_file_extent_num_blocks(fi) << 3; |
| 486 | if (extent_start != 0) { |
| 487 | found_extent = 1; |
| 488 | inode->i_blocks -= num_dec; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | if (del_item) { |
| 493 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 494 | if (ret) |
| 495 | goto error; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 496 | } else { |
| 497 | break; |
| 498 | } |
| 499 | btrfs_release_path(root, path); |
| 500 | if (found_extent) { |
| 501 | ret = btrfs_free_extent(trans, root, extent_start, |
| 502 | extent_num_blocks, 0); |
| 503 | BUG_ON(ret); |
| 504 | } |
| 505 | } |
| 506 | ret = 0; |
| 507 | error: |
| 508 | btrfs_release_path(root, path); |
| 509 | btrfs_free_path(path); |
| 510 | inode->i_sb->s_dirt = 1; |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * taken from block_truncate_page, but does cow as it zeros out |
| 516 | * any bytes left in the last page in the file. |
| 517 | */ |
| 518 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 519 | { |
| 520 | struct inode *inode = mapping->host; |
| 521 | unsigned blocksize = 1 << inode->i_blkbits; |
| 522 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 523 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 524 | struct page *page; |
| 525 | char *kaddr; |
| 526 | int ret = 0; |
| 527 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 528 | u64 alloc_hint = 0; |
| 529 | struct btrfs_key ins; |
| 530 | struct btrfs_trans_handle *trans; |
| 531 | |
| 532 | if ((offset & (blocksize - 1)) == 0) |
| 533 | goto out; |
| 534 | |
| 535 | ret = -ENOMEM; |
| 536 | page = grab_cache_page(mapping, index); |
| 537 | if (!page) |
| 538 | goto out; |
| 539 | |
| 540 | if (!PageUptodate(page)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 541 | ret = btrfs_readpage(NULL, page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 542 | lock_page(page); |
| 543 | if (!PageUptodate(page)) { |
| 544 | ret = -EIO; |
| 545 | goto out; |
| 546 | } |
| 547 | } |
| 548 | mutex_lock(&root->fs_info->fs_mutex); |
| 549 | trans = btrfs_start_transaction(root, 1); |
| 550 | btrfs_set_trans_block_group(trans, inode); |
| 551 | |
| 552 | ret = btrfs_drop_extents(trans, root, inode, |
| 553 | page->index << PAGE_CACHE_SHIFT, |
| 554 | (page->index + 1) << PAGE_CACHE_SHIFT, |
| 555 | &alloc_hint); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 556 | if (ret) |
| 557 | goto out; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 558 | ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1, 0, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 559 | alloc_hint, (u64)-1, &ins, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 560 | if (ret) |
| 561 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 562 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 563 | page->index << PAGE_CACHE_SHIFT, |
| 564 | ins.objectid, 1, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 565 | if (ret) |
| 566 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 567 | SetPageChecked(page); |
| 568 | kaddr = kmap(page); |
| 569 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 570 | flush_dcache_page(page); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 571 | ret = btrfs_csum_file_block(trans, root, inode->i_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 572 | page->index << PAGE_CACHE_SHIFT, |
| 573 | kaddr, PAGE_CACHE_SIZE); |
| 574 | kunmap(page); |
| 575 | btrfs_end_transaction(trans, root); |
| 576 | mutex_unlock(&root->fs_info->fs_mutex); |
| 577 | |
| 578 | set_page_dirty(page); |
| 579 | unlock_page(page); |
| 580 | page_cache_release(page); |
| 581 | out: |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 586 | { |
| 587 | struct inode *inode = dentry->d_inode; |
| 588 | int err; |
| 589 | |
| 590 | err = inode_change_ok(inode, attr); |
| 591 | if (err) |
| 592 | return err; |
| 593 | |
| 594 | if (S_ISREG(inode->i_mode) && |
| 595 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { |
| 596 | struct btrfs_trans_handle *trans; |
| 597 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 598 | u64 mask = root->blocksize - 1; |
| 599 | u64 pos = (inode->i_size + mask) & ~mask; |
| 600 | u64 hole_size; |
| 601 | |
| 602 | if (attr->ia_size <= pos) |
| 603 | goto out; |
| 604 | |
| 605 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 606 | |
| 607 | hole_size = (attr->ia_size - pos + mask) & ~mask; |
| 608 | hole_size >>= inode->i_blkbits; |
| 609 | |
| 610 | mutex_lock(&root->fs_info->fs_mutex); |
| 611 | trans = btrfs_start_transaction(root, 1); |
| 612 | btrfs_set_trans_block_group(trans, inode); |
| 613 | err = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 614 | pos, 0, 0, hole_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 615 | btrfs_end_transaction(trans, root); |
| 616 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 617 | if (err) |
| 618 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 619 | } |
| 620 | out: |
| 621 | err = inode_setattr(inode, attr); |
| 622 | |
| 623 | return err; |
| 624 | } |
| 625 | void btrfs_delete_inode(struct inode *inode) |
| 626 | { |
| 627 | struct btrfs_trans_handle *trans; |
| 628 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 629 | int ret; |
| 630 | |
| 631 | truncate_inode_pages(&inode->i_data, 0); |
| 632 | if (is_bad_inode(inode)) { |
| 633 | goto no_delete; |
| 634 | } |
| 635 | inode->i_size = 0; |
| 636 | mutex_lock(&root->fs_info->fs_mutex); |
| 637 | trans = btrfs_start_transaction(root, 1); |
| 638 | btrfs_set_trans_block_group(trans, inode); |
| 639 | ret = btrfs_truncate_in_trans(trans, root, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 640 | if (ret) |
| 641 | goto no_delete_lock; |
| 642 | ret = btrfs_free_inode(trans, root, inode); |
| 643 | if (ret) |
| 644 | goto no_delete_lock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 645 | btrfs_end_transaction(trans, root); |
| 646 | mutex_unlock(&root->fs_info->fs_mutex); |
| 647 | btrfs_btree_balance_dirty(root); |
| 648 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 649 | |
| 650 | no_delete_lock: |
| 651 | btrfs_end_transaction(trans, root); |
| 652 | mutex_unlock(&root->fs_info->fs_mutex); |
| 653 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 654 | no_delete: |
| 655 | clear_inode(inode); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * this returns the key found in the dir entry in the location pointer. |
| 660 | * If no dir entries were found, location->objectid is 0. |
| 661 | */ |
| 662 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 663 | struct btrfs_key *location) |
| 664 | { |
| 665 | const char *name = dentry->d_name.name; |
| 666 | int namelen = dentry->d_name.len; |
| 667 | struct btrfs_dir_item *di; |
| 668 | struct btrfs_path *path; |
| 669 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 670 | int ret; |
| 671 | |
| 672 | path = btrfs_alloc_path(); |
| 673 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 674 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 675 | namelen, 0); |
| 676 | if (!di || IS_ERR(di)) { |
| 677 | location->objectid = 0; |
| 678 | ret = 0; |
| 679 | goto out; |
| 680 | } |
| 681 | btrfs_disk_key_to_cpu(location, &di->location); |
| 682 | out: |
| 683 | btrfs_release_path(root, path); |
| 684 | btrfs_free_path(path); |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 690 | * needs to be changed to reflect the root directory of the tree root. This |
| 691 | * is kind of like crossing a mount point. |
| 692 | */ |
| 693 | static int fixup_tree_root_location(struct btrfs_root *root, |
| 694 | struct btrfs_key *location, |
| 695 | struct btrfs_root **sub_root) |
| 696 | { |
| 697 | struct btrfs_path *path; |
| 698 | struct btrfs_root_item *ri; |
| 699 | |
| 700 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 701 | return 0; |
| 702 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 703 | return 0; |
| 704 | |
| 705 | path = btrfs_alloc_path(); |
| 706 | BUG_ON(!path); |
| 707 | mutex_lock(&root->fs_info->fs_mutex); |
| 708 | |
| 709 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
| 710 | if (IS_ERR(*sub_root)) |
| 711 | return PTR_ERR(*sub_root); |
| 712 | |
| 713 | ri = &(*sub_root)->root_item; |
| 714 | location->objectid = btrfs_root_dirid(ri); |
| 715 | location->flags = 0; |
| 716 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 717 | location->offset = 0; |
| 718 | |
| 719 | btrfs_free_path(path); |
| 720 | mutex_unlock(&root->fs_info->fs_mutex); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 725 | { |
| 726 | struct btrfs_iget_args *args = p; |
| 727 | inode->i_ino = args->ino; |
| 728 | BTRFS_I(inode)->root = args->root; |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 733 | { |
| 734 | struct btrfs_iget_args *args = opaque; |
| 735 | return (args->ino == inode->i_ino && |
| 736 | args->root == BTRFS_I(inode)->root); |
| 737 | } |
| 738 | |
| 739 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 740 | struct btrfs_root *root) |
| 741 | { |
| 742 | struct inode *inode; |
| 743 | struct btrfs_iget_args args; |
| 744 | args.ino = objectid; |
| 745 | args.root = root; |
| 746 | |
| 747 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 748 | btrfs_init_locked_inode, |
| 749 | (void *)&args); |
| 750 | return inode; |
| 751 | } |
| 752 | |
| 753 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 754 | struct nameidata *nd) |
| 755 | { |
| 756 | struct inode * inode; |
| 757 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 758 | struct btrfs_root *root = bi->root; |
| 759 | struct btrfs_root *sub_root = root; |
| 760 | struct btrfs_key location; |
| 761 | int ret; |
| 762 | |
| 763 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 764 | return ERR_PTR(-ENAMETOOLONG); |
| 765 | mutex_lock(&root->fs_info->fs_mutex); |
| 766 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 767 | mutex_unlock(&root->fs_info->fs_mutex); |
| 768 | if (ret < 0) |
| 769 | return ERR_PTR(ret); |
| 770 | inode = NULL; |
| 771 | if (location.objectid) { |
| 772 | ret = fixup_tree_root_location(root, &location, &sub_root); |
| 773 | if (ret < 0) |
| 774 | return ERR_PTR(ret); |
| 775 | if (ret > 0) |
| 776 | return ERR_PTR(-ENOENT); |
| 777 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 778 | sub_root); |
| 779 | if (!inode) |
| 780 | return ERR_PTR(-EACCES); |
| 781 | if (inode->i_state & I_NEW) { |
| 782 | /* the inode and parent dir are two different roots */ |
| 783 | if (sub_root != root) { |
| 784 | igrab(inode); |
| 785 | sub_root->inode = inode; |
| 786 | } |
| 787 | BTRFS_I(inode)->root = sub_root; |
| 788 | memcpy(&BTRFS_I(inode)->location, &location, |
| 789 | sizeof(location)); |
| 790 | btrfs_read_locked_inode(inode); |
| 791 | unlock_new_inode(inode); |
| 792 | } |
| 793 | } |
| 794 | return d_splice_alias(inode, dentry); |
| 795 | } |
| 796 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 797 | static unsigned char btrfs_filetype_table[] = { |
| 798 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 799 | }; |
| 800 | |
| 801 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 802 | { |
| 803 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 804 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 805 | struct btrfs_item *item; |
| 806 | struct btrfs_dir_item *di; |
| 807 | struct btrfs_key key; |
| 808 | struct btrfs_path *path; |
| 809 | int ret; |
| 810 | u32 nritems; |
| 811 | struct btrfs_leaf *leaf; |
| 812 | int slot; |
| 813 | int advance; |
| 814 | unsigned char d_type; |
| 815 | int over = 0; |
| 816 | u32 di_cur; |
| 817 | u32 di_total; |
| 818 | u32 di_len; |
| 819 | int key_type = BTRFS_DIR_INDEX_KEY; |
| 820 | |
| 821 | /* FIXME, use a real flag for deciding about the key type */ |
| 822 | if (root->fs_info->tree_root == root) |
| 823 | key_type = BTRFS_DIR_ITEM_KEY; |
| 824 | mutex_lock(&root->fs_info->fs_mutex); |
| 825 | key.objectid = inode->i_ino; |
| 826 | key.flags = 0; |
| 827 | btrfs_set_key_type(&key, key_type); |
| 828 | key.offset = filp->f_pos; |
| 829 | path = btrfs_alloc_path(); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 830 | path->reada = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 831 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 832 | if (ret < 0) |
| 833 | goto err; |
| 834 | advance = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 835 | while(1) { |
| 836 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 837 | nritems = btrfs_header_nritems(&leaf->header); |
| 838 | slot = path->slots[0]; |
| 839 | if (advance || slot >= nritems) { |
| 840 | if (slot >= nritems -1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 841 | ret = btrfs_next_leaf(root, path); |
| 842 | if (ret) |
| 843 | break; |
| 844 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 845 | nritems = btrfs_header_nritems(&leaf->header); |
| 846 | slot = path->slots[0]; |
| 847 | } else { |
| 848 | slot++; |
| 849 | path->slots[0]++; |
| 850 | } |
| 851 | } |
| 852 | advance = 1; |
| 853 | item = leaf->items + slot; |
| 854 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 855 | break; |
| 856 | if (btrfs_disk_key_type(&item->key) != key_type) |
| 857 | break; |
| 858 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 859 | continue; |
| 860 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
| 861 | advance = 1; |
| 862 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 863 | di_cur = 0; |
| 864 | di_total = btrfs_item_size(leaf->items + slot); |
| 865 | while(di_cur < di_total) { |
| 866 | d_type = btrfs_filetype_table[btrfs_dir_type(di)]; |
| 867 | over = filldir(dirent, (const char *)(di + 1), |
| 868 | btrfs_dir_name_len(di), |
| 869 | btrfs_disk_key_offset(&item->key), |
| 870 | btrfs_disk_key_objectid(&di->location), |
| 871 | d_type); |
| 872 | if (over) |
| 873 | goto nopos; |
| 874 | di_len = btrfs_dir_name_len(di) + sizeof(*di); |
| 875 | di_cur += di_len; |
| 876 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 877 | } |
| 878 | } |
| 879 | filp->f_pos++; |
| 880 | nopos: |
| 881 | ret = 0; |
| 882 | err: |
| 883 | btrfs_release_path(root, path); |
| 884 | btrfs_free_path(path); |
| 885 | mutex_unlock(&root->fs_info->fs_mutex); |
| 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | int btrfs_write_inode(struct inode *inode, int wait) |
| 890 | { |
| 891 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 892 | struct btrfs_trans_handle *trans; |
| 893 | int ret = 0; |
| 894 | |
| 895 | if (wait) { |
| 896 | mutex_lock(&root->fs_info->fs_mutex); |
| 897 | trans = btrfs_start_transaction(root, 1); |
| 898 | btrfs_set_trans_block_group(trans, inode); |
| 899 | ret = btrfs_commit_transaction(trans, root); |
| 900 | mutex_unlock(&root->fs_info->fs_mutex); |
| 901 | } |
| 902 | return ret; |
| 903 | } |
| 904 | |
| 905 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 906 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 907 | * inode changes. But, it is most likely to find the inode in cache. |
| 908 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 909 | * to keep or drop this code. |
| 910 | */ |
| 911 | void btrfs_dirty_inode(struct inode *inode) |
| 912 | { |
| 913 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 914 | struct btrfs_trans_handle *trans; |
| 915 | |
| 916 | mutex_lock(&root->fs_info->fs_mutex); |
| 917 | trans = btrfs_start_transaction(root, 1); |
| 918 | btrfs_set_trans_block_group(trans, inode); |
| 919 | btrfs_update_inode(trans, root, inode); |
| 920 | btrfs_end_transaction(trans, root); |
| 921 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 925 | struct btrfs_root *root, |
| 926 | u64 objectid, |
| 927 | struct btrfs_block_group_cache *group, |
| 928 | int mode) |
| 929 | { |
| 930 | struct inode *inode; |
| 931 | struct btrfs_inode_item inode_item; |
| 932 | struct btrfs_key *location; |
| 933 | int ret; |
| 934 | int owner; |
| 935 | |
| 936 | inode = new_inode(root->fs_info->sb); |
| 937 | if (!inode) |
| 938 | return ERR_PTR(-ENOMEM); |
| 939 | |
| 940 | BTRFS_I(inode)->root = root; |
| 941 | if (mode & S_IFDIR) |
| 942 | owner = 0; |
| 943 | else |
| 944 | owner = 1; |
| 945 | group = btrfs_find_block_group(root, group, 0, 0, owner); |
| 946 | BTRFS_I(inode)->block_group = group; |
| 947 | |
| 948 | inode->i_uid = current->fsuid; |
| 949 | inode->i_gid = current->fsgid; |
| 950 | inode->i_mode = mode; |
| 951 | inode->i_ino = objectid; |
| 952 | inode->i_blocks = 0; |
| 953 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 954 | fill_inode_item(&inode_item, inode); |
| 955 | location = &BTRFS_I(inode)->location; |
| 956 | location->objectid = objectid; |
| 957 | location->flags = 0; |
| 958 | location->offset = 0; |
| 959 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 960 | |
| 961 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 962 | if (ret) |
| 963 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 964 | insert_inode_hash(inode); |
| 965 | return inode; |
| 966 | } |
| 967 | |
| 968 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 969 | { |
| 970 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 971 | } |
| 972 | |
| 973 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 974 | struct dentry *dentry, struct inode *inode) |
| 975 | { |
| 976 | int ret; |
| 977 | struct btrfs_key key; |
| 978 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 979 | struct inode *parent_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 980 | key.objectid = inode->i_ino; |
| 981 | key.flags = 0; |
| 982 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 983 | key.offset = 0; |
| 984 | |
| 985 | ret = btrfs_insert_dir_item(trans, root, |
| 986 | dentry->d_name.name, dentry->d_name.len, |
| 987 | dentry->d_parent->d_inode->i_ino, |
| 988 | &key, btrfs_inode_type(inode)); |
| 989 | if (ret == 0) { |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 990 | parent_inode = dentry->d_parent->d_inode; |
| 991 | parent_inode->i_size += dentry->d_name.len * 2; |
| 992 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 993 | ret = btrfs_update_inode(trans, root, |
| 994 | dentry->d_parent->d_inode); |
| 995 | } |
| 996 | return ret; |
| 997 | } |
| 998 | |
| 999 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 1000 | struct dentry *dentry, struct inode *inode) |
| 1001 | { |
| 1002 | int err = btrfs_add_link(trans, dentry, inode); |
| 1003 | if (!err) { |
| 1004 | d_instantiate(dentry, inode); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | if (err > 0) |
| 1008 | err = -EEXIST; |
| 1009 | return err; |
| 1010 | } |
| 1011 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1012 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 1013 | int mode, dev_t rdev) |
| 1014 | { |
| 1015 | struct btrfs_trans_handle *trans; |
| 1016 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1017 | struct inode *inode; |
| 1018 | int err; |
| 1019 | int drop_inode = 0; |
| 1020 | u64 objectid; |
| 1021 | |
| 1022 | if (!new_valid_dev(rdev)) |
| 1023 | return -EINVAL; |
| 1024 | |
| 1025 | mutex_lock(&root->fs_info->fs_mutex); |
| 1026 | trans = btrfs_start_transaction(root, 1); |
| 1027 | btrfs_set_trans_block_group(trans, dir); |
| 1028 | |
| 1029 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1030 | if (err) { |
| 1031 | err = -ENOSPC; |
| 1032 | goto out_unlock; |
| 1033 | } |
| 1034 | |
| 1035 | inode = btrfs_new_inode(trans, root, objectid, |
| 1036 | BTRFS_I(dir)->block_group, mode); |
| 1037 | err = PTR_ERR(inode); |
| 1038 | if (IS_ERR(inode)) |
| 1039 | goto out_unlock; |
| 1040 | |
| 1041 | btrfs_set_trans_block_group(trans, inode); |
| 1042 | err = btrfs_add_nondir(trans, dentry, inode); |
| 1043 | if (err) |
| 1044 | drop_inode = 1; |
| 1045 | else { |
| 1046 | inode->i_op = &btrfs_special_inode_operations; |
| 1047 | init_special_inode(inode, inode->i_mode, rdev); |
| 1048 | } |
| 1049 | dir->i_sb->s_dirt = 1; |
| 1050 | btrfs_update_inode_block_group(trans, inode); |
| 1051 | btrfs_update_inode_block_group(trans, dir); |
| 1052 | out_unlock: |
| 1053 | btrfs_end_transaction(trans, root); |
| 1054 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1055 | |
| 1056 | if (drop_inode) { |
| 1057 | inode_dec_link_count(inode); |
| 1058 | iput(inode); |
| 1059 | } |
| 1060 | btrfs_btree_balance_dirty(root); |
| 1061 | return err; |
| 1062 | } |
| 1063 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1064 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 1065 | int mode, struct nameidata *nd) |
| 1066 | { |
| 1067 | struct btrfs_trans_handle *trans; |
| 1068 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1069 | struct inode *inode; |
| 1070 | int err; |
| 1071 | int drop_inode = 0; |
| 1072 | u64 objectid; |
| 1073 | |
| 1074 | mutex_lock(&root->fs_info->fs_mutex); |
| 1075 | trans = btrfs_start_transaction(root, 1); |
| 1076 | btrfs_set_trans_block_group(trans, dir); |
| 1077 | |
| 1078 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1079 | if (err) { |
| 1080 | err = -ENOSPC; |
| 1081 | goto out_unlock; |
| 1082 | } |
| 1083 | |
| 1084 | inode = btrfs_new_inode(trans, root, objectid, |
| 1085 | BTRFS_I(dir)->block_group, mode); |
| 1086 | err = PTR_ERR(inode); |
| 1087 | if (IS_ERR(inode)) |
| 1088 | goto out_unlock; |
| 1089 | |
| 1090 | btrfs_set_trans_block_group(trans, inode); |
| 1091 | err = btrfs_add_nondir(trans, dentry, inode); |
| 1092 | if (err) |
| 1093 | drop_inode = 1; |
| 1094 | else { |
| 1095 | inode->i_mapping->a_ops = &btrfs_aops; |
| 1096 | inode->i_fop = &btrfs_file_operations; |
| 1097 | inode->i_op = &btrfs_file_inode_operations; |
| 1098 | } |
| 1099 | dir->i_sb->s_dirt = 1; |
| 1100 | btrfs_update_inode_block_group(trans, inode); |
| 1101 | btrfs_update_inode_block_group(trans, dir); |
| 1102 | out_unlock: |
| 1103 | btrfs_end_transaction(trans, root); |
| 1104 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1105 | |
| 1106 | if (drop_inode) { |
| 1107 | inode_dec_link_count(inode); |
| 1108 | iput(inode); |
| 1109 | } |
| 1110 | btrfs_btree_balance_dirty(root); |
| 1111 | return err; |
| 1112 | } |
| 1113 | |
| 1114 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 1115 | struct dentry *dentry) |
| 1116 | { |
| 1117 | struct btrfs_trans_handle *trans; |
| 1118 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1119 | struct inode *inode = old_dentry->d_inode; |
| 1120 | int err; |
| 1121 | int drop_inode = 0; |
| 1122 | |
| 1123 | if (inode->i_nlink == 0) |
| 1124 | return -ENOENT; |
| 1125 | |
| 1126 | inc_nlink(inode); |
| 1127 | mutex_lock(&root->fs_info->fs_mutex); |
| 1128 | trans = btrfs_start_transaction(root, 1); |
| 1129 | btrfs_set_trans_block_group(trans, dir); |
| 1130 | atomic_inc(&inode->i_count); |
| 1131 | err = btrfs_add_nondir(trans, dentry, inode); |
| 1132 | if (err) |
| 1133 | drop_inode = 1; |
| 1134 | dir->i_sb->s_dirt = 1; |
| 1135 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1136 | err = btrfs_update_inode(trans, root, inode); |
| 1137 | if (err) |
| 1138 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1139 | |
| 1140 | btrfs_end_transaction(trans, root); |
| 1141 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1142 | |
| 1143 | if (drop_inode) { |
| 1144 | inode_dec_link_count(inode); |
| 1145 | iput(inode); |
| 1146 | } |
| 1147 | btrfs_btree_balance_dirty(root); |
| 1148 | return err; |
| 1149 | } |
| 1150 | |
| 1151 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 1152 | struct btrfs_root *root, |
| 1153 | u64 objectid, u64 dirid) |
| 1154 | { |
| 1155 | int ret; |
| 1156 | char buf[2]; |
| 1157 | struct btrfs_key key; |
| 1158 | |
| 1159 | buf[0] = '.'; |
| 1160 | buf[1] = '.'; |
| 1161 | |
| 1162 | key.objectid = objectid; |
| 1163 | key.offset = 0; |
| 1164 | key.flags = 0; |
| 1165 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 1166 | |
| 1167 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
| 1168 | &key, BTRFS_FT_DIR); |
| 1169 | if (ret) |
| 1170 | goto error; |
| 1171 | key.objectid = dirid; |
| 1172 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, |
| 1173 | &key, BTRFS_FT_DIR); |
| 1174 | if (ret) |
| 1175 | goto error; |
| 1176 | error: |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1181 | { |
| 1182 | struct inode *inode; |
| 1183 | struct btrfs_trans_handle *trans; |
| 1184 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1185 | int err = 0; |
| 1186 | int drop_on_err = 0; |
| 1187 | u64 objectid; |
| 1188 | |
| 1189 | mutex_lock(&root->fs_info->fs_mutex); |
| 1190 | trans = btrfs_start_transaction(root, 1); |
| 1191 | btrfs_set_trans_block_group(trans, dir); |
| 1192 | if (IS_ERR(trans)) { |
| 1193 | err = PTR_ERR(trans); |
| 1194 | goto out_unlock; |
| 1195 | } |
| 1196 | |
| 1197 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1198 | if (err) { |
| 1199 | err = -ENOSPC; |
| 1200 | goto out_unlock; |
| 1201 | } |
| 1202 | |
| 1203 | inode = btrfs_new_inode(trans, root, objectid, |
| 1204 | BTRFS_I(dir)->block_group, S_IFDIR | mode); |
| 1205 | if (IS_ERR(inode)) { |
| 1206 | err = PTR_ERR(inode); |
| 1207 | goto out_fail; |
| 1208 | } |
| 1209 | drop_on_err = 1; |
| 1210 | inode->i_op = &btrfs_dir_inode_operations; |
| 1211 | inode->i_fop = &btrfs_dir_file_operations; |
| 1212 | btrfs_set_trans_block_group(trans, inode); |
| 1213 | |
| 1214 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
| 1215 | if (err) |
| 1216 | goto out_fail; |
| 1217 | |
| 1218 | inode->i_size = 6; |
| 1219 | err = btrfs_update_inode(trans, root, inode); |
| 1220 | if (err) |
| 1221 | goto out_fail; |
| 1222 | err = btrfs_add_link(trans, dentry, inode); |
| 1223 | if (err) |
| 1224 | goto out_fail; |
| 1225 | d_instantiate(dentry, inode); |
| 1226 | drop_on_err = 0; |
| 1227 | dir->i_sb->s_dirt = 1; |
| 1228 | btrfs_update_inode_block_group(trans, inode); |
| 1229 | btrfs_update_inode_block_group(trans, dir); |
| 1230 | |
| 1231 | out_fail: |
| 1232 | btrfs_end_transaction(trans, root); |
| 1233 | out_unlock: |
| 1234 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1235 | if (drop_on_err) |
| 1236 | iput(inode); |
| 1237 | btrfs_btree_balance_dirty(root); |
| 1238 | return err; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * FIBMAP and others want to pass in a fake buffer head. They need to |
| 1243 | * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy |
| 1244 | * any packed file data into the fake bh |
| 1245 | */ |
| 1246 | #define BTRFS_GET_BLOCK_NO_CREATE 0 |
| 1247 | #define BTRFS_GET_BLOCK_CREATE 1 |
| 1248 | #define BTRFS_GET_BLOCK_NO_DIRECT 2 |
| 1249 | |
| 1250 | /* |
| 1251 | * FIXME create==1 doe not work. |
| 1252 | */ |
| 1253 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
| 1254 | struct buffer_head *result, int create) |
| 1255 | { |
| 1256 | int ret; |
| 1257 | int err = 0; |
| 1258 | u64 blocknr; |
| 1259 | u64 extent_start = 0; |
| 1260 | u64 extent_end = 0; |
| 1261 | u64 objectid = inode->i_ino; |
| 1262 | u32 found_type; |
| 1263 | u64 alloc_hint = 0; |
| 1264 | struct btrfs_path *path; |
| 1265 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1266 | struct btrfs_file_extent_item *item; |
| 1267 | struct btrfs_leaf *leaf; |
| 1268 | struct btrfs_disk_key *found_key; |
| 1269 | struct btrfs_trans_handle *trans = NULL; |
| 1270 | |
| 1271 | path = btrfs_alloc_path(); |
| 1272 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1273 | if (create & BTRFS_GET_BLOCK_CREATE) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1274 | /* |
| 1275 | * danger!, this only works if the page is properly up |
| 1276 | * to date somehow |
| 1277 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1278 | trans = btrfs_start_transaction(root, 1); |
| 1279 | if (!trans) { |
| 1280 | err = -ENOMEM; |
| 1281 | goto out; |
| 1282 | } |
| 1283 | ret = btrfs_drop_extents(trans, root, inode, |
| 1284 | iblock << inode->i_blkbits, |
| 1285 | (iblock + 1) << inode->i_blkbits, |
| 1286 | &alloc_hint); |
| 1287 | BUG_ON(ret); |
| 1288 | } |
| 1289 | |
| 1290 | ret = btrfs_lookup_file_extent(NULL, root, path, |
Aneesh | f1ace24 | 2007-06-13 16:18:26 -0400 | [diff] [blame] | 1291 | objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1292 | iblock << inode->i_blkbits, 0); |
| 1293 | if (ret < 0) { |
| 1294 | err = ret; |
| 1295 | goto out; |
| 1296 | } |
| 1297 | |
| 1298 | if (ret != 0) { |
| 1299 | if (path->slots[0] == 0) { |
| 1300 | btrfs_release_path(root, path); |
| 1301 | goto not_found; |
| 1302 | } |
| 1303 | path->slots[0]--; |
| 1304 | } |
| 1305 | |
| 1306 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
| 1307 | struct btrfs_file_extent_item); |
| 1308 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1309 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 1310 | blocknr += btrfs_file_extent_offset(item); |
| 1311 | |
| 1312 | /* are we inside the extent that was found? */ |
| 1313 | found_key = &leaf->items[path->slots[0]].key; |
| 1314 | found_type = btrfs_disk_key_type(found_key); |
| 1315 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 1316 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 1317 | extent_end = 0; |
| 1318 | extent_start = 0; |
| 1319 | goto not_found; |
| 1320 | } |
| 1321 | found_type = btrfs_file_extent_type(item); |
| 1322 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
| 1323 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 1324 | extent_start = extent_start >> inode->i_blkbits; |
| 1325 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
| 1326 | err = 0; |
| 1327 | if (btrfs_file_extent_disk_blocknr(item) == 0) |
| 1328 | goto out; |
| 1329 | if (iblock >= extent_start && iblock < extent_end) { |
| 1330 | btrfs_map_bh_to_logical(root, result, blocknr + |
| 1331 | iblock - extent_start); |
| 1332 | goto out; |
| 1333 | } |
| 1334 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1335 | char *ptr; |
| 1336 | char *map; |
| 1337 | u32 size; |
| 1338 | |
| 1339 | if (create & BTRFS_GET_BLOCK_NO_DIRECT) { |
| 1340 | err = -EINVAL; |
| 1341 | goto out; |
| 1342 | } |
| 1343 | size = btrfs_file_extent_inline_len(leaf->items + |
| 1344 | path->slots[0]); |
| 1345 | extent_end = (extent_start + size) >> inode->i_blkbits; |
| 1346 | extent_start >>= inode->i_blkbits; |
| 1347 | if (iblock < extent_start || iblock > extent_end) { |
| 1348 | goto not_found; |
| 1349 | } |
| 1350 | ptr = btrfs_file_extent_inline_start(item); |
| 1351 | map = kmap(result->b_page); |
| 1352 | memcpy(map, ptr, size); |
| 1353 | memset(map + size, 0, PAGE_CACHE_SIZE - size); |
| 1354 | flush_dcache_page(result->b_page); |
| 1355 | kunmap(result->b_page); |
| 1356 | set_buffer_uptodate(result); |
| 1357 | SetPageChecked(result->b_page); |
| 1358 | btrfs_map_bh_to_logical(root, result, 0); |
| 1359 | } |
| 1360 | not_found: |
| 1361 | if (create & BTRFS_GET_BLOCK_CREATE) { |
| 1362 | struct btrfs_key ins; |
| 1363 | ret = btrfs_alloc_extent(trans, root, inode->i_ino, |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1364 | 1, 0, alloc_hint, (u64)-1, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1365 | &ins, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1366 | if (ret) { |
| 1367 | err = ret; |
| 1368 | goto out; |
| 1369 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1370 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 1371 | iblock << inode->i_blkbits, |
| 1372 | ins.objectid, ins.offset, |
| 1373 | ins.offset); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1374 | if (ret) { |
| 1375 | err = ret; |
| 1376 | goto out; |
| 1377 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1378 | btrfs_map_bh_to_logical(root, result, ins.objectid); |
| 1379 | } |
| 1380 | out: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1381 | if (trans) { |
| 1382 | ret = btrfs_end_transaction(trans, root); |
| 1383 | if (!err) |
| 1384 | err = ret; |
| 1385 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1386 | btrfs_free_path(path); |
| 1387 | return err; |
| 1388 | } |
| 1389 | |
| 1390 | int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 1391 | struct buffer_head *result, int create) |
| 1392 | { |
| 1393 | int err; |
| 1394 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1395 | mutex_lock(&root->fs_info->fs_mutex); |
| 1396 | err = btrfs_get_block_lock(inode, iblock, result, create); |
| 1397 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1398 | return err; |
| 1399 | } |
| 1400 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1401 | static int btrfs_get_block_csum(struct inode *inode, sector_t iblock, |
| 1402 | struct buffer_head *result, int create) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1403 | { |
| 1404 | int ret; |
| 1405 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1406 | struct page *page = result->b_page; |
| 1407 | u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result); |
| 1408 | struct btrfs_csum_item *item; |
| 1409 | struct btrfs_path *path = NULL; |
| 1410 | |
| 1411 | mutex_lock(&root->fs_info->fs_mutex); |
| 1412 | ret = btrfs_get_block_lock(inode, iblock, result, create); |
| 1413 | if (ret) |
| 1414 | goto out; |
| 1415 | |
| 1416 | path = btrfs_alloc_path(); |
| 1417 | item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0); |
| 1418 | if (IS_ERR(item)) { |
| 1419 | ret = PTR_ERR(item); |
| 1420 | /* a csum that isn't present is a preallocated region. */ |
| 1421 | if (ret == -ENOENT || ret == -EFBIG) |
| 1422 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1423 | result->b_private = NULL; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1424 | goto out; |
| 1425 | } |
| 1426 | memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1427 | out: |
| 1428 | if (path) |
| 1429 | btrfs_free_path(path); |
| 1430 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1431 | return ret; |
| 1432 | } |
| 1433 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1434 | static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock, |
| 1435 | struct buffer_head *result, int create) |
| 1436 | { |
| 1437 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1438 | mutex_lock(&root->fs_info->fs_mutex); |
| 1439 | btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT); |
| 1440 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | static sector_t btrfs_bmap(struct address_space *as, sector_t block) |
| 1445 | { |
| 1446 | return generic_block_bmap(as, block, btrfs_get_block_bmap); |
| 1447 | } |
| 1448 | |
| 1449 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 1450 | unsigned from, unsigned to) |
| 1451 | { |
| 1452 | return block_prepare_write(page, from, to, btrfs_get_block); |
| 1453 | } |
| 1454 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1455 | static void buffer_io_error(struct buffer_head *bh) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1456 | { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1457 | char b[BDEVNAME_SIZE]; |
| 1458 | |
| 1459 | printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n", |
| 1460 | bdevname(bh->b_bdev, b), |
| 1461 | (unsigned long long)bh->b_blocknr); |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * I/O completion handler for block_read_full_page() - pages |
| 1466 | * which come unlocked at the end of I/O. |
| 1467 | */ |
| 1468 | static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate) |
| 1469 | { |
| 1470 | unsigned long flags; |
| 1471 | struct buffer_head *first; |
| 1472 | struct buffer_head *tmp; |
| 1473 | struct page *page; |
| 1474 | int page_uptodate = 1; |
| 1475 | struct inode *inode; |
| 1476 | int ret; |
| 1477 | |
| 1478 | BUG_ON(!buffer_async_read(bh)); |
| 1479 | |
| 1480 | page = bh->b_page; |
| 1481 | inode = page->mapping->host; |
| 1482 | if (uptodate) { |
| 1483 | void *kaddr; |
| 1484 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1485 | if (bh->b_private) { |
| 1486 | char csum[BTRFS_CRC32_SIZE]; |
| 1487 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 1488 | ret = btrfs_csum_data(root, kaddr + bh_offset(bh), |
| 1489 | bh->b_size, csum); |
| 1490 | BUG_ON(ret); |
| 1491 | if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) { |
| 1492 | u64 offset; |
| 1493 | offset = (page->index << PAGE_CACHE_SHIFT) + |
| 1494 | bh_offset(bh); |
| 1495 | printk("btrfs csum failed ino %lu off %llu\n", |
| 1496 | page->mapping->host->i_ino, |
| 1497 | (unsigned long long)offset); |
| 1498 | memset(kaddr + bh_offset(bh), 1, bh->b_size); |
| 1499 | flush_dcache_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1500 | } |
| 1501 | kunmap_atomic(kaddr, KM_IRQ0); |
| 1502 | } |
| 1503 | set_buffer_uptodate(bh); |
| 1504 | } else { |
| 1505 | clear_buffer_uptodate(bh); |
| 1506 | if (printk_ratelimit()) |
| 1507 | buffer_io_error(bh); |
| 1508 | SetPageError(page); |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Be _very_ careful from here on. Bad things can happen if |
| 1513 | * two buffer heads end IO at almost the same time and both |
| 1514 | * decide that the page is now completely done. |
| 1515 | */ |
| 1516 | first = page_buffers(page); |
| 1517 | local_irq_save(flags); |
| 1518 | bit_spin_lock(BH_Uptodate_Lock, &first->b_state); |
| 1519 | clear_buffer_async_read(bh); |
| 1520 | unlock_buffer(bh); |
| 1521 | tmp = bh; |
| 1522 | do { |
| 1523 | if (!buffer_uptodate(tmp)) |
| 1524 | page_uptodate = 0; |
| 1525 | if (buffer_async_read(tmp)) { |
| 1526 | BUG_ON(!buffer_locked(tmp)); |
| 1527 | goto still_busy; |
| 1528 | } |
| 1529 | tmp = tmp->b_this_page; |
| 1530 | } while (tmp != bh); |
| 1531 | bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); |
| 1532 | local_irq_restore(flags); |
| 1533 | |
| 1534 | /* |
| 1535 | * If none of the buffers had errors and they are all |
| 1536 | * uptodate then we can set the page uptodate. |
| 1537 | */ |
| 1538 | if (page_uptodate && !PageError(page)) |
| 1539 | SetPageUptodate(page); |
| 1540 | unlock_page(page); |
| 1541 | return; |
| 1542 | |
| 1543 | still_busy: |
| 1544 | bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); |
| 1545 | local_irq_restore(flags); |
| 1546 | return; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Generic "read page" function for block devices that have the normal |
| 1551 | * get_block functionality. This is most of the block device filesystems. |
| 1552 | * Reads the page asynchronously --- the unlock_buffer() and |
| 1553 | * set/clear_buffer_uptodate() functions propagate buffer state into the |
| 1554 | * page struct once IO has completed. |
| 1555 | */ |
| 1556 | int btrfs_readpage(struct file *file, struct page *page) |
| 1557 | { |
| 1558 | struct inode *inode = page->mapping->host; |
| 1559 | sector_t iblock, lblock; |
| 1560 | struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; |
| 1561 | unsigned int blocksize; |
| 1562 | int nr, i; |
| 1563 | int fully_mapped = 1; |
| 1564 | |
| 1565 | BUG_ON(!PageLocked(page)); |
| 1566 | blocksize = 1 << inode->i_blkbits; |
| 1567 | if (!page_has_buffers(page)) |
| 1568 | create_empty_buffers(page, blocksize, 0); |
| 1569 | head = page_buffers(page); |
| 1570 | |
| 1571 | iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1572 | lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits; |
| 1573 | bh = head; |
| 1574 | nr = 0; |
| 1575 | i = 0; |
| 1576 | |
| 1577 | do { |
| 1578 | if (buffer_uptodate(bh)) |
| 1579 | continue; |
| 1580 | |
| 1581 | if (!buffer_mapped(bh)) { |
| 1582 | int err = 0; |
| 1583 | |
| 1584 | fully_mapped = 0; |
| 1585 | if (iblock < lblock) { |
| 1586 | WARN_ON(bh->b_size != blocksize); |
| 1587 | err = btrfs_get_block_csum(inode, iblock, |
| 1588 | bh, 0); |
| 1589 | if (err) |
| 1590 | SetPageError(page); |
| 1591 | } |
| 1592 | if (!buffer_mapped(bh)) { |
| 1593 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 1594 | memset(kaddr + i * blocksize, 0, blocksize); |
| 1595 | flush_dcache_page(page); |
| 1596 | kunmap_atomic(kaddr, KM_USER0); |
| 1597 | if (!err) |
| 1598 | set_buffer_uptodate(bh); |
| 1599 | continue; |
| 1600 | } |
| 1601 | /* |
| 1602 | * get_block() might have updated the buffer |
| 1603 | * synchronously |
| 1604 | */ |
| 1605 | if (buffer_uptodate(bh)) |
| 1606 | continue; |
| 1607 | } |
| 1608 | arr[nr++] = bh; |
| 1609 | } while (i++, iblock++, (bh = bh->b_this_page) != head); |
| 1610 | |
| 1611 | if (fully_mapped) |
| 1612 | SetPageMappedToDisk(page); |
| 1613 | |
| 1614 | if (!nr) { |
| 1615 | /* |
| 1616 | * All buffers are uptodate - we can set the page uptodate |
| 1617 | * as well. But not if get_block() returned an error. |
| 1618 | */ |
| 1619 | if (!PageError(page)) |
| 1620 | SetPageUptodate(page); |
| 1621 | unlock_page(page); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | /* Stage two: lock the buffers */ |
| 1626 | for (i = 0; i < nr; i++) { |
| 1627 | bh = arr[i]; |
| 1628 | lock_buffer(bh); |
| 1629 | bh->b_end_io = btrfs_end_buffer_async_read; |
| 1630 | set_buffer_async_read(bh); |
| 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | * Stage 3: start the IO. Check for uptodateness |
| 1635 | * inside the buffer lock in case another process reading |
| 1636 | * the underlying blockdev brought it uptodate (the sct fix). |
| 1637 | */ |
| 1638 | for (i = 0; i < nr; i++) { |
| 1639 | bh = arr[i]; |
| 1640 | if (buffer_uptodate(bh)) |
| 1641 | btrfs_end_buffer_async_read(bh, 1); |
| 1642 | else |
| 1643 | submit_bh(READ, bh); |
| 1644 | } |
| 1645 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * Aside from a tiny bit of packed file data handling, this is the |
| 1650 | * same as the generic code. |
| 1651 | * |
| 1652 | * While block_write_full_page is writing back the dirty buffers under |
| 1653 | * the page lock, whoever dirtied the buffers may decide to clean them |
| 1654 | * again at any time. We handle that by only looking at the buffer |
| 1655 | * state inside lock_buffer(). |
| 1656 | * |
| 1657 | * If block_write_full_page() is called for regular writeback |
| 1658 | * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a |
| 1659 | * locked buffer. This only can happen if someone has written the buffer |
| 1660 | * directly, with submit_bh(). At the address_space level PageWriteback |
| 1661 | * prevents this contention from occurring. |
| 1662 | */ |
| 1663 | static int __btrfs_write_full_page(struct inode *inode, struct page *page, |
| 1664 | struct writeback_control *wbc) |
| 1665 | { |
| 1666 | int err; |
| 1667 | sector_t block; |
| 1668 | sector_t last_block; |
| 1669 | struct buffer_head *bh, *head; |
| 1670 | const unsigned blocksize = 1 << inode->i_blkbits; |
| 1671 | int nr_underway = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1672 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1673 | |
| 1674 | BUG_ON(!PageLocked(page)); |
| 1675 | |
| 1676 | last_block = (i_size_read(inode) - 1) >> inode->i_blkbits; |
| 1677 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1678 | /* no csumming allowed when from PF_MEMALLOC */ |
| 1679 | if (current->flags & PF_MEMALLOC) { |
| 1680 | redirty_page_for_writepage(wbc, page); |
| 1681 | unlock_page(page); |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1685 | if (!page_has_buffers(page)) { |
| 1686 | create_empty_buffers(page, blocksize, |
| 1687 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Be very careful. We have no exclusion from __set_page_dirty_buffers |
| 1692 | * here, and the (potentially unmapped) buffers may become dirty at |
| 1693 | * any time. If a buffer becomes dirty here after we've inspected it |
| 1694 | * then we just miss that fact, and the page stays dirty. |
| 1695 | * |
| 1696 | * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; |
| 1697 | * handle that here by just cleaning them. |
| 1698 | */ |
| 1699 | |
| 1700 | block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1701 | head = page_buffers(page); |
| 1702 | bh = head; |
| 1703 | |
| 1704 | /* |
| 1705 | * Get all the dirty buffers mapped to disk addresses and |
| 1706 | * handle any aliases from the underlying blockdev's mapping. |
| 1707 | */ |
| 1708 | do { |
| 1709 | if (block > last_block) { |
| 1710 | /* |
| 1711 | * mapped buffers outside i_size will occur, because |
| 1712 | * this page can be outside i_size when there is a |
| 1713 | * truncate in progress. |
| 1714 | */ |
| 1715 | /* |
| 1716 | * The buffer was zeroed by block_write_full_page() |
| 1717 | */ |
| 1718 | clear_buffer_dirty(bh); |
| 1719 | set_buffer_uptodate(bh); |
| 1720 | } else if (!buffer_mapped(bh) && buffer_dirty(bh)) { |
| 1721 | WARN_ON(bh->b_size != blocksize); |
| 1722 | err = btrfs_get_block(inode, block, bh, 0); |
| 1723 | if (err) { |
| 1724 | goto recover; |
| 1725 | } |
| 1726 | if (buffer_new(bh)) { |
| 1727 | /* blockdev mappings never come here */ |
| 1728 | clear_buffer_new(bh); |
| 1729 | } |
| 1730 | } |
| 1731 | bh = bh->b_this_page; |
| 1732 | block++; |
| 1733 | } while (bh != head); |
| 1734 | |
| 1735 | do { |
| 1736 | if (!buffer_mapped(bh)) |
| 1737 | continue; |
| 1738 | /* |
| 1739 | * If it's a fully non-blocking write attempt and we cannot |
| 1740 | * lock the buffer then redirty the page. Note that this can |
| 1741 | * potentially cause a busy-wait loop from pdflush and kswapd |
| 1742 | * activity, but those code paths have their own higher-level |
| 1743 | * throttling. |
| 1744 | */ |
| 1745 | if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) { |
| 1746 | lock_buffer(bh); |
| 1747 | } else if (test_set_buffer_locked(bh)) { |
| 1748 | redirty_page_for_writepage(wbc, page); |
| 1749 | continue; |
| 1750 | } |
| 1751 | if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1752 | struct btrfs_trans_handle *trans; |
| 1753 | int ret; |
| 1754 | u64 off = page->index << PAGE_CACHE_SHIFT; |
| 1755 | char *kaddr; |
| 1756 | |
| 1757 | off += bh_offset(bh); |
| 1758 | mutex_lock(&root->fs_info->fs_mutex); |
| 1759 | trans = btrfs_start_transaction(root, 1); |
| 1760 | btrfs_set_trans_block_group(trans, inode); |
| 1761 | kaddr = kmap(page); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1762 | btrfs_csum_file_block(trans, root, inode->i_ino, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1763 | off, kaddr + bh_offset(bh), |
| 1764 | bh->b_size); |
| 1765 | kunmap(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1766 | ret = btrfs_end_transaction(trans, root); |
| 1767 | BUG_ON(ret); |
| 1768 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1769 | mark_buffer_async_write(bh); |
| 1770 | } else { |
| 1771 | unlock_buffer(bh); |
| 1772 | } |
| 1773 | } while ((bh = bh->b_this_page) != head); |
| 1774 | |
| 1775 | /* |
| 1776 | * The page and its buffers are protected by PageWriteback(), so we can |
| 1777 | * drop the bh refcounts early. |
| 1778 | */ |
| 1779 | BUG_ON(PageWriteback(page)); |
| 1780 | set_page_writeback(page); |
| 1781 | |
| 1782 | do { |
| 1783 | struct buffer_head *next = bh->b_this_page; |
| 1784 | if (buffer_async_write(bh)) { |
| 1785 | submit_bh(WRITE, bh); |
| 1786 | nr_underway++; |
| 1787 | } |
| 1788 | bh = next; |
| 1789 | } while (bh != head); |
| 1790 | unlock_page(page); |
| 1791 | |
| 1792 | err = 0; |
| 1793 | done: |
| 1794 | if (nr_underway == 0) { |
| 1795 | /* |
| 1796 | * The page was marked dirty, but the buffers were |
| 1797 | * clean. Someone wrote them back by hand with |
| 1798 | * ll_rw_block/submit_bh. A rare case. |
| 1799 | */ |
| 1800 | int uptodate = 1; |
| 1801 | do { |
| 1802 | if (!buffer_uptodate(bh)) { |
| 1803 | uptodate = 0; |
| 1804 | break; |
| 1805 | } |
| 1806 | bh = bh->b_this_page; |
| 1807 | } while (bh != head); |
| 1808 | if (uptodate) |
| 1809 | SetPageUptodate(page); |
| 1810 | end_page_writeback(page); |
| 1811 | } |
| 1812 | return err; |
| 1813 | |
| 1814 | recover: |
| 1815 | /* |
| 1816 | * ENOSPC, or some other error. We may already have added some |
| 1817 | * blocks to the file, so we need to write these out to avoid |
| 1818 | * exposing stale data. |
| 1819 | * The page is currently locked and not marked for writeback |
| 1820 | */ |
| 1821 | bh = head; |
| 1822 | /* Recovery: lock and submit the mapped buffers */ |
| 1823 | do { |
| 1824 | if (buffer_mapped(bh) && buffer_dirty(bh)) { |
| 1825 | lock_buffer(bh); |
| 1826 | mark_buffer_async_write(bh); |
| 1827 | } else { |
| 1828 | /* |
| 1829 | * The buffer may have been set dirty during |
| 1830 | * attachment to a dirty page. |
| 1831 | */ |
| 1832 | clear_buffer_dirty(bh); |
| 1833 | } |
| 1834 | } while ((bh = bh->b_this_page) != head); |
| 1835 | SetPageError(page); |
| 1836 | BUG_ON(PageWriteback(page)); |
| 1837 | set_page_writeback(page); |
| 1838 | do { |
| 1839 | struct buffer_head *next = bh->b_this_page; |
| 1840 | if (buffer_async_write(bh)) { |
| 1841 | clear_buffer_dirty(bh); |
| 1842 | submit_bh(WRITE, bh); |
| 1843 | nr_underway++; |
| 1844 | } |
| 1845 | bh = next; |
| 1846 | } while (bh != head); |
| 1847 | unlock_page(page); |
| 1848 | goto done; |
| 1849 | } |
| 1850 | |
| 1851 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 1852 | { |
| 1853 | struct inode * const inode = page->mapping->host; |
| 1854 | loff_t i_size = i_size_read(inode); |
| 1855 | const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 1856 | unsigned offset; |
| 1857 | void *kaddr; |
| 1858 | |
| 1859 | /* Is the page fully inside i_size? */ |
| 1860 | if (page->index < end_index) |
| 1861 | return __btrfs_write_full_page(inode, page, wbc); |
| 1862 | |
| 1863 | /* Is the page fully outside i_size? (truncate in progress) */ |
| 1864 | offset = i_size & (PAGE_CACHE_SIZE-1); |
| 1865 | if (page->index >= end_index+1 || !offset) { |
| 1866 | /* |
| 1867 | * The page may have dirty, unmapped buffers. For example, |
| 1868 | * they may have been added in ext3_writepage(). Make them |
| 1869 | * freeable here, so the page does not leak. |
| 1870 | */ |
| 1871 | block_invalidatepage(page, 0); |
| 1872 | unlock_page(page); |
| 1873 | return 0; /* don't care */ |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * The page straddles i_size. It must be zeroed out on each and every |
| 1878 | * writepage invokation because it may be mmapped. "A file is mapped |
| 1879 | * in multiples of the page size. For a file that is not a multiple of |
| 1880 | * the page size, the remaining memory is zeroed when mapped, and |
| 1881 | * writes to that region are not written out to the file." |
| 1882 | */ |
| 1883 | kaddr = kmap_atomic(page, KM_USER0); |
| 1884 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 1885 | flush_dcache_page(page); |
| 1886 | kunmap_atomic(kaddr, KM_USER0); |
| 1887 | return __btrfs_write_full_page(inode, page, wbc); |
| 1888 | } |
| 1889 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1890 | /* |
| 1891 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 1892 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 1893 | * be careful to check for EOF conditions here. We set the page up correctly |
| 1894 | * for a written page which means we get ENOSPC checking when writing into |
| 1895 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 1896 | * support these features. |
| 1897 | * |
| 1898 | * We are not allowed to take the i_mutex here so we have to play games to |
| 1899 | * protect against truncate races as the page could now be beyond EOF. Because |
| 1900 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 1901 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 1902 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 1903 | * unlock the page. |
| 1904 | */ |
| 1905 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 1906 | { |
| 1907 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 1908 | unsigned long end; |
| 1909 | loff_t size; |
| 1910 | int ret = -EINVAL; |
| 1911 | |
| 1912 | lock_page(page); |
| 1913 | wait_on_page_writeback(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1914 | size = i_size_read(inode); |
| 1915 | if ((page->mapping != inode->i_mapping) || |
| 1916 | ((page->index << PAGE_CACHE_SHIFT) > size)) { |
| 1917 | /* page got truncated out from underneath us */ |
| 1918 | goto out_unlock; |
| 1919 | } |
| 1920 | |
| 1921 | /* page is wholly or partially inside EOF */ |
| 1922 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > size) |
| 1923 | end = size & ~PAGE_CACHE_MASK; |
| 1924 | else |
| 1925 | end = PAGE_CACHE_SIZE; |
| 1926 | |
| 1927 | ret = btrfs_prepare_write(NULL, page, 0, end); |
| 1928 | if (!ret) |
| 1929 | ret = btrfs_commit_write(NULL, page, 0, end); |
| 1930 | |
| 1931 | out_unlock: |
| 1932 | unlock_page(page); |
| 1933 | return ret; |
| 1934 | } |
| 1935 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1936 | static void btrfs_truncate(struct inode *inode) |
| 1937 | { |
| 1938 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1939 | int ret; |
| 1940 | struct btrfs_trans_handle *trans; |
| 1941 | |
| 1942 | if (!S_ISREG(inode->i_mode)) |
| 1943 | return; |
| 1944 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1945 | return; |
| 1946 | |
| 1947 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 1948 | |
| 1949 | mutex_lock(&root->fs_info->fs_mutex); |
| 1950 | trans = btrfs_start_transaction(root, 1); |
| 1951 | btrfs_set_trans_block_group(trans, inode); |
| 1952 | |
| 1953 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 1954 | ret = btrfs_truncate_in_trans(trans, root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1955 | btrfs_update_inode(trans, root, inode); |
| 1956 | ret = btrfs_end_transaction(trans, root); |
| 1957 | BUG_ON(ret); |
| 1958 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1959 | btrfs_btree_balance_dirty(root); |
| 1960 | } |
| 1961 | |
| 1962 | int btrfs_commit_write(struct file *file, struct page *page, |
| 1963 | unsigned from, unsigned to) |
| 1964 | { |
| 1965 | struct inode *inode = page->mapping->host; |
| 1966 | struct buffer_head *bh; |
| 1967 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 1968 | |
| 1969 | SetPageUptodate(page); |
| 1970 | bh = page_buffers(page); |
| 1971 | set_buffer_uptodate(bh); |
| 1972 | if (buffer_mapped(bh) && bh->b_blocknr != 0) { |
| 1973 | set_page_dirty(page); |
| 1974 | } |
| 1975 | if (pos > inode->i_size) { |
| 1976 | i_size_write(inode, pos); |
| 1977 | mark_inode_dirty(inode); |
| 1978 | } |
| 1979 | return 0; |
| 1980 | } |
| 1981 | |
| 1982 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
| 1983 | { |
| 1984 | struct btrfs_trans_handle *trans; |
| 1985 | struct btrfs_key key; |
| 1986 | struct btrfs_root_item root_item; |
| 1987 | struct btrfs_inode_item *inode_item; |
| 1988 | struct buffer_head *subvol; |
| 1989 | struct btrfs_leaf *leaf; |
| 1990 | struct btrfs_root *new_root; |
| 1991 | struct inode *inode; |
| 1992 | struct inode *dir; |
| 1993 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1994 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1995 | u64 objectid; |
| 1996 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 1997 | |
| 1998 | mutex_lock(&root->fs_info->fs_mutex); |
| 1999 | trans = btrfs_start_transaction(root, 1); |
| 2000 | BUG_ON(!trans); |
| 2001 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2002 | subvol = btrfs_alloc_free_block(trans, root, 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2003 | if (IS_ERR(subvol)) |
| 2004 | return PTR_ERR(subvol); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2005 | leaf = btrfs_buffer_leaf(subvol); |
| 2006 | btrfs_set_header_nritems(&leaf->header, 0); |
| 2007 | btrfs_set_header_level(&leaf->header, 0); |
| 2008 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
| 2009 | btrfs_set_header_generation(&leaf->header, trans->transid); |
| 2010 | btrfs_set_header_owner(&leaf->header, root->root_key.objectid); |
| 2011 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, |
| 2012 | sizeof(leaf->header.fsid)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2013 | btrfs_mark_buffer_dirty(subvol); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2014 | |
| 2015 | inode_item = &root_item.inode; |
| 2016 | memset(inode_item, 0, sizeof(*inode_item)); |
| 2017 | btrfs_set_inode_generation(inode_item, 1); |
| 2018 | btrfs_set_inode_size(inode_item, 3); |
| 2019 | btrfs_set_inode_nlink(inode_item, 1); |
| 2020 | btrfs_set_inode_nblocks(inode_item, 1); |
| 2021 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); |
| 2022 | |
| 2023 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
| 2024 | btrfs_set_root_refs(&root_item, 1); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2025 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 2026 | root_item.drop_level = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2027 | brelse(subvol); |
| 2028 | subvol = NULL; |
| 2029 | |
| 2030 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2031 | 0, &objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2032 | if (ret) |
| 2033 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2034 | |
| 2035 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 2036 | |
| 2037 | key.objectid = objectid; |
| 2038 | key.offset = 1; |
| 2039 | key.flags = 0; |
| 2040 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2041 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2042 | &root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2043 | if (ret) |
| 2044 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2045 | |
| 2046 | /* |
| 2047 | * insert the directory item |
| 2048 | */ |
| 2049 | key.offset = (u64)-1; |
| 2050 | dir = root->fs_info->sb->s_root->d_inode; |
| 2051 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2052 | name, namelen, dir->i_ino, &key, |
| 2053 | BTRFS_FT_DIR); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2054 | if (ret) |
| 2055 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2056 | |
| 2057 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2058 | if (ret) |
| 2059 | goto fail_commit; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2060 | |
| 2061 | new_root = btrfs_read_fs_root(root->fs_info, &key); |
| 2062 | BUG_ON(!new_root); |
| 2063 | |
| 2064 | trans = btrfs_start_transaction(new_root, 1); |
| 2065 | BUG_ON(!trans); |
| 2066 | |
| 2067 | inode = btrfs_new_inode(trans, new_root, new_dirid, |
| 2068 | BTRFS_I(dir)->block_group, S_IFDIR | 0700); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2069 | if (IS_ERR(inode)) |
| 2070 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2071 | inode->i_op = &btrfs_dir_inode_operations; |
| 2072 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 2073 | new_root->inode = inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2074 | |
| 2075 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2076 | if (ret) |
| 2077 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2078 | |
| 2079 | inode->i_nlink = 1; |
| 2080 | inode->i_size = 6; |
| 2081 | ret = btrfs_update_inode(trans, new_root, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2082 | if (ret) |
| 2083 | goto fail; |
| 2084 | fail: |
| 2085 | err = btrfs_commit_transaction(trans, root); |
| 2086 | if (err && !ret) |
| 2087 | ret = err; |
| 2088 | fail_commit: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2089 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2090 | btrfs_btree_balance_dirty(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2091 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 2095 | { |
| 2096 | struct btrfs_trans_handle *trans; |
| 2097 | struct btrfs_key key; |
| 2098 | struct btrfs_root_item new_root_item; |
| 2099 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2100 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2101 | u64 objectid; |
| 2102 | |
| 2103 | if (!root->ref_cows) |
| 2104 | return -EINVAL; |
| 2105 | |
| 2106 | mutex_lock(&root->fs_info->fs_mutex); |
| 2107 | trans = btrfs_start_transaction(root, 1); |
| 2108 | BUG_ON(!trans); |
| 2109 | |
| 2110 | ret = btrfs_update_inode(trans, root, root->inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2111 | if (ret) |
| 2112 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2113 | |
| 2114 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2115 | 0, &objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2116 | if (ret) |
| 2117 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2118 | |
| 2119 | memcpy(&new_root_item, &root->root_item, |
| 2120 | sizeof(new_root_item)); |
| 2121 | |
| 2122 | key.objectid = objectid; |
| 2123 | key.offset = 1; |
| 2124 | key.flags = 0; |
| 2125 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2126 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
| 2127 | |
| 2128 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2129 | &new_root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2130 | if (ret) |
| 2131 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2132 | |
| 2133 | /* |
| 2134 | * insert the directory item |
| 2135 | */ |
| 2136 | key.offset = (u64)-1; |
| 2137 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2138 | name, namelen, |
| 2139 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 2140 | &key, BTRFS_FT_DIR); |
| 2141 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2142 | if (ret) |
| 2143 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2144 | |
| 2145 | ret = btrfs_inc_root_ref(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2146 | if (ret) |
| 2147 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2148 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2149 | fail: |
| 2150 | err = btrfs_commit_transaction(trans, root); |
| 2151 | if (err && !ret) |
| 2152 | ret = err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2153 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2154 | btrfs_btree_balance_dirty(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2155 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
| 2159 | cmd, unsigned long arg) |
| 2160 | { |
| 2161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2162 | struct btrfs_ioctl_vol_args vol_args; |
| 2163 | int ret = 0; |
| 2164 | struct btrfs_dir_item *di; |
| 2165 | int namelen; |
| 2166 | struct btrfs_path *path; |
| 2167 | u64 root_dirid; |
| 2168 | |
| 2169 | switch (cmd) { |
| 2170 | case BTRFS_IOC_SNAP_CREATE: |
| 2171 | if (copy_from_user(&vol_args, |
| 2172 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 2173 | sizeof(vol_args))) |
| 2174 | return -EFAULT; |
| 2175 | namelen = strlen(vol_args.name); |
| 2176 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 2177 | return -EINVAL; |
Chris Mason | 8a71264 | 2007-06-12 08:21:35 -0400 | [diff] [blame] | 2178 | if (strchr(vol_args.name, '/')) |
| 2179 | return -EINVAL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2180 | path = btrfs_alloc_path(); |
| 2181 | if (!path) |
| 2182 | return -ENOMEM; |
| 2183 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
| 2184 | mutex_lock(&root->fs_info->fs_mutex); |
| 2185 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 2186 | path, root_dirid, |
| 2187 | vol_args.name, namelen, 0); |
| 2188 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2189 | btrfs_free_path(path); |
| 2190 | if (di && !IS_ERR(di)) |
| 2191 | return -EEXIST; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2192 | if (IS_ERR(di)) |
| 2193 | return PTR_ERR(di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2194 | |
| 2195 | if (root == root->fs_info->tree_root) |
| 2196 | ret = create_subvol(root, vol_args.name, namelen); |
| 2197 | else |
| 2198 | ret = create_snapshot(root, vol_args.name, namelen); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2199 | break; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2200 | |
| 2201 | case BTRFS_IOC_DEFRAG: |
| 2202 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2203 | btrfs_defrag_root(root, 0); |
| 2204 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2205 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2206 | ret = 0; |
| 2207 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2208 | default: |
| 2209 | return -ENOTTY; |
| 2210 | } |
| 2211 | return ret; |
| 2212 | } |
| 2213 | |
| 2214 | #ifdef CONFIG_COMPAT |
| 2215 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, |
| 2216 | unsigned long arg) |
| 2217 | { |
| 2218 | struct inode *inode = file->f_path.dentry->d_inode; |
| 2219 | int ret; |
| 2220 | lock_kernel(); |
| 2221 | ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg)); |
| 2222 | unlock_kernel(); |
| 2223 | return ret; |
| 2224 | |
| 2225 | } |
| 2226 | #endif |
| 2227 | |
| 2228 | /* |
| 2229 | * Called inside transaction, so use GFP_NOFS |
| 2230 | */ |
| 2231 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 2232 | { |
| 2233 | struct btrfs_inode *ei; |
| 2234 | |
| 2235 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 2236 | if (!ei) |
| 2237 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame^] | 2238 | ei->last_trans = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2239 | return &ei->vfs_inode; |
| 2240 | } |
| 2241 | |
| 2242 | void btrfs_destroy_inode(struct inode *inode) |
| 2243 | { |
| 2244 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 2245 | WARN_ON(inode->i_data.nrpages); |
| 2246 | |
| 2247 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 2248 | } |
| 2249 | |
| 2250 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 2251 | unsigned long flags) |
| 2252 | { |
| 2253 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 2254 | |
| 2255 | inode_init_once(&ei->vfs_inode); |
| 2256 | } |
| 2257 | |
| 2258 | void btrfs_destroy_cachep(void) |
| 2259 | { |
| 2260 | if (btrfs_inode_cachep) |
| 2261 | kmem_cache_destroy(btrfs_inode_cachep); |
| 2262 | if (btrfs_trans_handle_cachep) |
| 2263 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 2264 | if (btrfs_transaction_cachep) |
| 2265 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 2266 | if (btrfs_bit_radix_cachep) |
| 2267 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 2268 | if (btrfs_path_cachep) |
| 2269 | kmem_cache_destroy(btrfs_path_cachep); |
| 2270 | } |
| 2271 | |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2272 | static struct kmem_cache *cache_create(const char *name, size_t size, |
| 2273 | unsigned long extra_flags, |
| 2274 | void (*ctor)(void *, struct kmem_cache *, |
| 2275 | unsigned long)) |
| 2276 | { |
| 2277 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | |
| 2278 | SLAB_MEM_SPREAD | extra_flags), ctor |
| 2279 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 2280 | ,NULL |
| 2281 | #endif |
| 2282 | ); |
| 2283 | } |
| 2284 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2285 | int btrfs_init_cachep(void) |
| 2286 | { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2287 | btrfs_inode_cachep = cache_create("btrfs_inode_cache", |
| 2288 | sizeof(struct btrfs_inode), |
| 2289 | 0, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2290 | if (!btrfs_inode_cachep) |
| 2291 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2292 | btrfs_trans_handle_cachep = cache_create("btrfs_trans_handle_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2293 | sizeof(struct btrfs_trans_handle), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2294 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2295 | if (!btrfs_trans_handle_cachep) |
| 2296 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2297 | btrfs_transaction_cachep = cache_create("btrfs_transaction_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2298 | sizeof(struct btrfs_transaction), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2299 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2300 | if (!btrfs_transaction_cachep) |
| 2301 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2302 | btrfs_path_cachep = cache_create("btrfs_path_cache", |
| 2303 | sizeof(struct btrfs_transaction), |
| 2304 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2305 | if (!btrfs_path_cachep) |
| 2306 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2307 | btrfs_bit_radix_cachep = cache_create("btrfs_radix", 256, |
| 2308 | SLAB_DESTROY_BY_RCU, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2309 | if (!btrfs_bit_radix_cachep) |
| 2310 | goto fail; |
| 2311 | return 0; |
| 2312 | fail: |
| 2313 | btrfs_destroy_cachep(); |
| 2314 | return -ENOMEM; |
| 2315 | } |
| 2316 | |
| 2317 | static int btrfs_getattr(struct vfsmount *mnt, |
| 2318 | struct dentry *dentry, struct kstat *stat) |
| 2319 | { |
| 2320 | struct inode *inode = dentry->d_inode; |
| 2321 | generic_fillattr(inode, stat); |
| 2322 | stat->blksize = 256 * 1024; |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, |
| 2327 | struct inode * new_dir,struct dentry *new_dentry) |
| 2328 | { |
| 2329 | struct btrfs_trans_handle *trans; |
| 2330 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 2331 | struct inode *new_inode = new_dentry->d_inode; |
| 2332 | struct inode *old_inode = old_dentry->d_inode; |
| 2333 | struct timespec ctime = CURRENT_TIME; |
| 2334 | struct btrfs_path *path; |
| 2335 | struct btrfs_dir_item *di; |
| 2336 | int ret; |
| 2337 | |
| 2338 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
| 2339 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
| 2340 | return -ENOTEMPTY; |
| 2341 | } |
| 2342 | mutex_lock(&root->fs_info->fs_mutex); |
| 2343 | trans = btrfs_start_transaction(root, 1); |
| 2344 | btrfs_set_trans_block_group(trans, new_dir); |
| 2345 | path = btrfs_alloc_path(); |
| 2346 | if (!path) { |
| 2347 | ret = -ENOMEM; |
| 2348 | goto out_fail; |
| 2349 | } |
| 2350 | |
| 2351 | old_dentry->d_inode->i_nlink++; |
| 2352 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 2353 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 2354 | old_inode->i_ctime = ctime; |
| 2355 | if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { |
| 2356 | struct btrfs_key *location = &BTRFS_I(new_dir)->location; |
| 2357 | u64 old_parent_oid; |
| 2358 | di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino, |
| 2359 | "..", 2, -1); |
| 2360 | if (IS_ERR(di)) { |
| 2361 | ret = PTR_ERR(di); |
| 2362 | goto out_fail; |
| 2363 | } |
| 2364 | if (!di) { |
| 2365 | ret = -ENOENT; |
| 2366 | goto out_fail; |
| 2367 | } |
| 2368 | old_parent_oid = btrfs_disk_key_objectid(&di->location); |
| 2369 | ret = btrfs_del_item(trans, root, path); |
| 2370 | if (ret) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2371 | goto out_fail; |
| 2372 | } |
| 2373 | btrfs_release_path(root, path); |
| 2374 | |
| 2375 | di = btrfs_lookup_dir_index_item(trans, root, path, |
| 2376 | old_inode->i_ino, |
| 2377 | old_parent_oid, |
| 2378 | "..", 2, -1); |
| 2379 | if (IS_ERR(di)) { |
| 2380 | ret = PTR_ERR(di); |
| 2381 | goto out_fail; |
| 2382 | } |
| 2383 | if (!di) { |
| 2384 | ret = -ENOENT; |
| 2385 | goto out_fail; |
| 2386 | } |
| 2387 | ret = btrfs_del_item(trans, root, path); |
| 2388 | if (ret) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2389 | goto out_fail; |
| 2390 | } |
| 2391 | btrfs_release_path(root, path); |
| 2392 | |
| 2393 | ret = btrfs_insert_dir_item(trans, root, "..", 2, |
| 2394 | old_inode->i_ino, location, |
| 2395 | BTRFS_FT_DIR); |
| 2396 | if (ret) |
| 2397 | goto out_fail; |
| 2398 | } |
| 2399 | |
| 2400 | |
| 2401 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
| 2402 | if (ret) |
| 2403 | goto out_fail; |
| 2404 | |
| 2405 | if (new_inode) { |
| 2406 | new_inode->i_ctime = CURRENT_TIME; |
| 2407 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); |
| 2408 | if (ret) |
| 2409 | goto out_fail; |
| 2410 | if (S_ISDIR(new_inode->i_mode)) |
| 2411 | clear_nlink(new_inode); |
| 2412 | else |
| 2413 | drop_nlink(new_inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2414 | ret = btrfs_update_inode(trans, root, new_inode); |
| 2415 | if (ret) |
| 2416 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2417 | } |
| 2418 | ret = btrfs_add_link(trans, new_dentry, old_inode); |
| 2419 | if (ret) |
| 2420 | goto out_fail; |
| 2421 | |
| 2422 | out_fail: |
| 2423 | btrfs_free_path(path); |
| 2424 | btrfs_end_transaction(trans, root); |
| 2425 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2426 | return ret; |
| 2427 | } |
| 2428 | |
| 2429 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 2430 | const char *symname) |
| 2431 | { |
| 2432 | struct btrfs_trans_handle *trans; |
| 2433 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2434 | struct btrfs_path *path; |
| 2435 | struct btrfs_key key; |
| 2436 | struct inode *inode; |
| 2437 | int err; |
| 2438 | int drop_inode = 0; |
| 2439 | u64 objectid; |
| 2440 | int name_len; |
| 2441 | int datasize; |
| 2442 | char *ptr; |
| 2443 | struct btrfs_file_extent_item *ei; |
| 2444 | |
| 2445 | name_len = strlen(symname) + 1; |
| 2446 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 2447 | return -ENAMETOOLONG; |
| 2448 | mutex_lock(&root->fs_info->fs_mutex); |
| 2449 | trans = btrfs_start_transaction(root, 1); |
| 2450 | btrfs_set_trans_block_group(trans, dir); |
| 2451 | |
| 2452 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2453 | if (err) { |
| 2454 | err = -ENOSPC; |
| 2455 | goto out_unlock; |
| 2456 | } |
| 2457 | |
| 2458 | inode = btrfs_new_inode(trans, root, objectid, |
| 2459 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO); |
| 2460 | err = PTR_ERR(inode); |
| 2461 | if (IS_ERR(inode)) |
| 2462 | goto out_unlock; |
| 2463 | |
| 2464 | btrfs_set_trans_block_group(trans, inode); |
| 2465 | err = btrfs_add_nondir(trans, dentry, inode); |
| 2466 | if (err) |
| 2467 | drop_inode = 1; |
| 2468 | else { |
| 2469 | inode->i_mapping->a_ops = &btrfs_aops; |
| 2470 | inode->i_fop = &btrfs_file_operations; |
| 2471 | inode->i_op = &btrfs_file_inode_operations; |
| 2472 | } |
| 2473 | dir->i_sb->s_dirt = 1; |
| 2474 | btrfs_update_inode_block_group(trans, inode); |
| 2475 | btrfs_update_inode_block_group(trans, dir); |
| 2476 | if (drop_inode) |
| 2477 | goto out_unlock; |
| 2478 | |
| 2479 | path = btrfs_alloc_path(); |
| 2480 | BUG_ON(!path); |
| 2481 | key.objectid = inode->i_ino; |
| 2482 | key.offset = 0; |
| 2483 | key.flags = 0; |
| 2484 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 2485 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 2486 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 2487 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2488 | if (err) { |
| 2489 | drop_inode = 1; |
| 2490 | goto out_unlock; |
| 2491 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2492 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 2493 | path->slots[0], struct btrfs_file_extent_item); |
| 2494 | btrfs_set_file_extent_generation(ei, trans->transid); |
| 2495 | btrfs_set_file_extent_type(ei, |
| 2496 | BTRFS_FILE_EXTENT_INLINE); |
| 2497 | ptr = btrfs_file_extent_inline_start(ei); |
| 2498 | btrfs_memcpy(root, path->nodes[0]->b_data, |
| 2499 | ptr, symname, name_len); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2500 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2501 | btrfs_free_path(path); |
| 2502 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2503 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 2504 | inode->i_size = name_len - 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2505 | err = btrfs_update_inode(trans, root, inode); |
| 2506 | if (err) |
| 2507 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2508 | |
| 2509 | out_unlock: |
| 2510 | btrfs_end_transaction(trans, root); |
| 2511 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2512 | if (drop_inode) { |
| 2513 | inode_dec_link_count(inode); |
| 2514 | iput(inode); |
| 2515 | } |
| 2516 | btrfs_btree_balance_dirty(root); |
| 2517 | return err; |
| 2518 | } |
| 2519 | |
| 2520 | static struct inode_operations btrfs_dir_inode_operations = { |
| 2521 | .lookup = btrfs_lookup, |
| 2522 | .create = btrfs_create, |
| 2523 | .unlink = btrfs_unlink, |
| 2524 | .link = btrfs_link, |
| 2525 | .mkdir = btrfs_mkdir, |
| 2526 | .rmdir = btrfs_rmdir, |
| 2527 | .rename = btrfs_rename, |
| 2528 | .symlink = btrfs_symlink, |
| 2529 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2530 | .mknod = btrfs_mknod, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2531 | }; |
| 2532 | |
| 2533 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 2534 | .lookup = btrfs_lookup, |
| 2535 | }; |
| 2536 | |
| 2537 | static struct file_operations btrfs_dir_file_operations = { |
| 2538 | .llseek = generic_file_llseek, |
| 2539 | .read = generic_read_dir, |
| 2540 | .readdir = btrfs_readdir, |
| 2541 | .ioctl = btrfs_ioctl, |
| 2542 | #ifdef CONFIG_COMPAT |
| 2543 | .compat_ioctl = btrfs_compat_ioctl, |
| 2544 | #endif |
| 2545 | }; |
| 2546 | |
| 2547 | static struct address_space_operations btrfs_aops = { |
| 2548 | .readpage = btrfs_readpage, |
| 2549 | .writepage = btrfs_writepage, |
| 2550 | .sync_page = block_sync_page, |
| 2551 | .prepare_write = btrfs_prepare_write, |
| 2552 | .commit_write = btrfs_commit_write, |
| 2553 | .bmap = btrfs_bmap, |
| 2554 | }; |
| 2555 | |
| 2556 | static struct address_space_operations btrfs_symlink_aops = { |
| 2557 | .readpage = btrfs_readpage, |
| 2558 | .writepage = btrfs_writepage, |
| 2559 | }; |
| 2560 | |
| 2561 | static struct inode_operations btrfs_file_inode_operations = { |
| 2562 | .truncate = btrfs_truncate, |
| 2563 | .getattr = btrfs_getattr, |
| 2564 | .setattr = btrfs_setattr, |
| 2565 | }; |
| 2566 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2567 | static struct inode_operations btrfs_special_inode_operations = { |
| 2568 | .getattr = btrfs_getattr, |
| 2569 | .setattr = btrfs_setattr, |
| 2570 | }; |
| 2571 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2572 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 2573 | .readlink = generic_readlink, |
| 2574 | .follow_link = page_follow_link_light, |
| 2575 | .put_link = page_put_link, |
| 2576 | }; |