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