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