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