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