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