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