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