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