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