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