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