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