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