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