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 | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 20 | #include <linux/bio.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 22 | #include <linux/file.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/smp_lock.h> |
| 30 | #include <linux/backing-dev.h> |
| 31 | #include <linux/mpage.h> |
| 32 | #include <linux/swap.h> |
| 33 | #include <linux/writeback.h> |
| 34 | #include <linux/statfs.h> |
| 35 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 36 | #include <linux/bit_spinlock.h> |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 37 | #include <linux/version.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 38 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 39 | #include <linux/posix_acl.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 40 | #include "ctree.h" |
| 41 | #include "disk-io.h" |
| 42 | #include "transaction.h" |
| 43 | #include "btrfs_inode.h" |
| 44 | #include "ioctl.h" |
| 45 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 46 | #include "volumes.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 47 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 48 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 49 | #include "compat.h" |
| 50 | #include "tree-log.h" |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 51 | #include "ref-cache.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 52 | |
| 53 | struct btrfs_iget_args { |
| 54 | u64 ino; |
| 55 | struct btrfs_root *root; |
| 56 | }; |
| 57 | |
| 58 | static struct inode_operations btrfs_dir_inode_operations; |
| 59 | static struct inode_operations btrfs_symlink_inode_operations; |
| 60 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 61 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 62 | static struct inode_operations btrfs_file_inode_operations; |
| 63 | static struct address_space_operations btrfs_aops; |
| 64 | static struct address_space_operations btrfs_symlink_aops; |
| 65 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 66 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 67 | |
| 68 | static struct kmem_cache *btrfs_inode_cachep; |
| 69 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 70 | struct kmem_cache *btrfs_transaction_cachep; |
| 71 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 72 | struct kmem_cache *btrfs_path_cachep; |
| 73 | |
| 74 | #define S_SHIFT 12 |
| 75 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 76 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 77 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 78 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 79 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 80 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 81 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 82 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 83 | }; |
| 84 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 85 | static void btrfs_truncate(struct inode *inode); |
| 86 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 87 | /* |
| 88 | * a very lame attempt at stopping writes when the FS is 85% full. There |
| 89 | * are countless ways this is incorrect, but it is better than nothing. |
| 90 | */ |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 91 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
| 92 | int for_del) |
| 93 | { |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 94 | u64 total; |
| 95 | u64 used; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 96 | u64 thresh; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 97 | unsigned long flags; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 98 | int ret = 0; |
| 99 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 100 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
| 101 | total = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 102 | used = btrfs_super_bytes_used(&root->fs_info->super_copy); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 103 | if (for_del) |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 104 | thresh = total * 90; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 105 | else |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 106 | thresh = total * 85; |
| 107 | |
| 108 | do_div(thresh, 100); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 109 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 110 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) |
| 111 | ret = -ENOSPC; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 112 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 113 | return ret; |
| 114 | } |
| 115 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 116 | /* |
| 117 | * when extent_io.c finds a delayed allocation range in the file, |
| 118 | * the call backs end up in this code. The basic idea is to |
| 119 | * allocate extents on disk for the range, and create ordered data structs |
| 120 | * in ram to track those extents. |
| 121 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 122 | static int cow_file_range(struct inode *inode, u64 start, u64 end) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 123 | { |
| 124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 125 | struct btrfs_trans_handle *trans; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 126 | u64 alloc_hint = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 127 | u64 num_bytes; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 128 | u64 cur_alloc_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 129 | u64 blocksize = root->sectorsize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 130 | u64 orig_num_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 131 | struct btrfs_key ins; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 132 | struct extent_map *em; |
| 133 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 134 | int ret = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 135 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 136 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 137 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 138 | btrfs_set_trans_block_group(trans, inode); |
| 139 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 140 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 141 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 142 | orig_num_bytes = num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 143 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 144 | if (alloc_hint == EXTENT_MAP_INLINE) |
| 145 | goto out; |
| 146 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 147 | BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy)); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 148 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 149 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 150 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 151 | |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 152 | while(num_bytes > 0) { |
| 153 | cur_alloc_size = min(num_bytes, root->fs_info->max_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 154 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 155 | root->sectorsize, 0, alloc_hint, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 156 | (u64)-1, &ins, 1); |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 157 | if (ret) { |
| 158 | WARN_ON(1); |
| 159 | goto out; |
| 160 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 161 | em = alloc_extent_map(GFP_NOFS); |
| 162 | em->start = start; |
| 163 | em->len = ins.offset; |
| 164 | em->block_start = ins.objectid; |
| 165 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 166 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 167 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 168 | while(1) { |
| 169 | spin_lock(&em_tree->lock); |
| 170 | ret = add_extent_mapping(em_tree, em); |
| 171 | spin_unlock(&em_tree->lock); |
| 172 | if (ret != -EEXIST) { |
| 173 | free_extent_map(em); |
| 174 | break; |
| 175 | } |
| 176 | btrfs_drop_extent_cache(inode, start, |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 177 | start + ins.offset - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 178 | } |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 179 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 180 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 181 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 182 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 183 | ins.offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 184 | BUG_ON(ret); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 185 | if (num_bytes < cur_alloc_size) { |
| 186 | printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes, |
| 187 | cur_alloc_size); |
| 188 | break; |
| 189 | } |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 190 | num_bytes -= cur_alloc_size; |
| 191 | alloc_hint = ins.objectid + ins.offset; |
| 192 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 193 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 194 | out: |
| 195 | btrfs_end_transaction(trans, root); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 196 | return ret; |
| 197 | } |
| 198 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 199 | /* |
| 200 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 201 | * of the extents that exist in the file, and COWs the file as required. |
| 202 | * |
| 203 | * If no cow copies or snapshots exist, we write directly to the existing |
| 204 | * blocks on disk |
| 205 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 206 | static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end) |
| 207 | { |
| 208 | u64 extent_start; |
| 209 | u64 extent_end; |
| 210 | u64 bytenr; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 211 | u64 loops = 0; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 212 | u64 total_fs_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 213 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a68d593 | 2008-05-08 14:11:56 -0400 | [diff] [blame] | 214 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 215 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 216 | struct extent_buffer *leaf; |
| 217 | int found_type; |
| 218 | struct btrfs_path *path; |
| 219 | struct btrfs_file_extent_item *item; |
| 220 | int ret; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 221 | int err = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 222 | struct btrfs_key found_key; |
| 223 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 224 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 225 | path = btrfs_alloc_path(); |
| 226 | BUG_ON(!path); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 227 | trans = btrfs_join_transaction(root, 1); |
| 228 | BUG_ON(!trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 229 | again: |
| 230 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 231 | inode->i_ino, start, 0); |
| 232 | if (ret < 0) { |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 233 | err = ret; |
| 234 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 237 | if (ret != 0) { |
| 238 | if (path->slots[0] == 0) |
| 239 | goto not_found; |
| 240 | path->slots[0]--; |
| 241 | } |
| 242 | |
| 243 | leaf = path->nodes[0]; |
| 244 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 245 | struct btrfs_file_extent_item); |
| 246 | |
| 247 | /* are we inside the extent that was found? */ |
| 248 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 249 | found_type = btrfs_key_type(&found_key); |
| 250 | if (found_key.objectid != inode->i_ino || |
Chris Mason | bbaf549 | 2008-05-08 16:31:21 -0400 | [diff] [blame] | 251 | found_type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 252 | goto not_found; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 253 | |
| 254 | found_type = btrfs_file_extent_type(leaf, item); |
| 255 | extent_start = found_key.offset; |
| 256 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 257 | u64 extent_num_bytes; |
| 258 | |
| 259 | extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item); |
| 260 | extent_end = extent_start + extent_num_bytes; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 261 | err = 0; |
| 262 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 263 | if (loops && start != extent_start) |
| 264 | goto not_found; |
| 265 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 266 | if (start < extent_start || start >= extent_end) |
| 267 | goto not_found; |
| 268 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 269 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 270 | if (bytenr == 0) |
| 271 | goto not_found; |
| 272 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 273 | if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr)) |
Chris Mason | a68d593 | 2008-05-08 14:11:56 -0400 | [diff] [blame] | 274 | goto not_found; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 275 | /* |
| 276 | * we may be called by the resizer, make sure we're inside |
| 277 | * the limits of the FS |
| 278 | */ |
Chris Mason | a68d593 | 2008-05-08 14:11:56 -0400 | [diff] [blame] | 279 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 280 | bytenr); |
| 281 | if (!block_group || block_group->ro) |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 282 | goto not_found; |
| 283 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 284 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 285 | extent_num_bytes = min(end + 1, extent_end) - start; |
| 286 | ret = btrfs_add_ordered_extent(inode, start, bytenr, |
| 287 | extent_num_bytes, 1); |
| 288 | if (ret) { |
| 289 | err = ret; |
| 290 | goto out; |
| 291 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 292 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 293 | btrfs_release_path(root, path); |
| 294 | start = extent_end; |
| 295 | if (start <= end) { |
| 296 | loops++; |
| 297 | goto again; |
| 298 | } |
| 299 | } else { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 300 | not_found: |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 301 | btrfs_end_transaction(trans, root); |
| 302 | btrfs_free_path(path); |
| 303 | return cow_file_range(inode, start, end); |
| 304 | } |
| 305 | out: |
| 306 | WARN_ON(err); |
| 307 | btrfs_end_transaction(trans, root); |
| 308 | btrfs_free_path(path); |
| 309 | return err; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 312 | /* |
| 313 | * extent_io.c call back to do delayed allocation processing |
| 314 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 315 | static int run_delalloc_range(struct inode *inode, u64 start, u64 end) |
| 316 | { |
| 317 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 318 | int ret; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 319 | |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 320 | if (btrfs_test_opt(root, NODATACOW) || |
| 321 | btrfs_test_flag(inode, NODATACOW)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 322 | ret = run_delalloc_nocow(inode, start, end); |
| 323 | else |
| 324 | ret = cow_file_range(inode, start, end); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 325 | |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 326 | return ret; |
| 327 | } |
| 328 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 329 | /* |
| 330 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 331 | * bytes in this file, and to maintain the list of inodes that |
| 332 | * have pending delalloc work to be done. |
| 333 | */ |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 334 | int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 335 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 336 | { |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 337 | unsigned long flags; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 338 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 340 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 341 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 342 | root->fs_info->delalloc_bytes += end - start + 1; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 343 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 344 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 345 | &root->fs_info->delalloc_inodes); |
| 346 | } |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 347 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 352 | /* |
| 353 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 354 | */ |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 355 | int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 356 | unsigned long old, unsigned long bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 357 | { |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 358 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 359 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 360 | unsigned long flags; |
| 361 | |
| 362 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 363 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
| 364 | printk("warning: delalloc account %Lu %Lu\n", |
| 365 | end - start + 1, root->fs_info->delalloc_bytes); |
| 366 | root->fs_info->delalloc_bytes = 0; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 367 | BTRFS_I(inode)->delalloc_bytes = 0; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 368 | } else { |
| 369 | root->fs_info->delalloc_bytes -= end - start + 1; |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 370 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 371 | } |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 372 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
| 373 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 374 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 375 | } |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 376 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 381 | /* |
| 382 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 383 | * we don't create bios that span stripes or chunks |
| 384 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 385 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
| 386 | size_t size, struct bio *bio) |
| 387 | { |
| 388 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 389 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 390 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 391 | u64 length = 0; |
| 392 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 393 | int ret; |
| 394 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 395 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 396 | map_tree = &root->fs_info->mapping_tree; |
| 397 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 398 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 399 | &map_length, NULL, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 400 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 401 | if (map_length < length + size) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 402 | return 1; |
| 403 | } |
| 404 | return 0; |
| 405 | } |
| 406 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 407 | /* |
| 408 | * in order to insert checksums into the metadata in large chunks, |
| 409 | * we wait until bio submission time. All the pages in the bio are |
| 410 | * checksummed and sums are attached onto the ordered extent record. |
| 411 | * |
| 412 | * At IO completion time the cums attached on the ordered extent record |
| 413 | * are inserted into the btree |
| 414 | */ |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 415 | int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 416 | int mirror_num) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 417 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 418 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 419 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 420 | |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 421 | ret = btrfs_csum_one_bio(root, inode, bio); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 422 | BUG_ON(ret); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 423 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 424 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 427 | /* |
| 428 | * extent_io.c submission hook. This does the right thing for csum calculation on write, |
| 429 | * or reading the csums from the tree before a read |
| 430 | */ |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 431 | int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
| 432 | int mirror_num) |
| 433 | { |
| 434 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 435 | int ret = 0; |
| 436 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 437 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 438 | BUG_ON(ret); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 439 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 440 | if (btrfs_test_opt(root, NODATASUM) || |
| 441 | btrfs_test_flag(inode, NODATASUM)) { |
| 442 | goto mapit; |
| 443 | } |
| 444 | |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 445 | if (!(rw & (1 << BIO_RW))) { |
| 446 | btrfs_lookup_bio_sums(root, inode, bio); |
| 447 | goto mapit; |
| 448 | } |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 449 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
| 450 | inode, rw, bio, mirror_num, |
| 451 | __btrfs_submit_bio_hook); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 452 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 453 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 454 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 455 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 456 | /* |
| 457 | * given a list of ordered sums record them in the inode. This happens |
| 458 | * at IO completion time based on sums calculated at bio submission time. |
| 459 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 460 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 461 | struct inode *inode, u64 file_offset, |
| 462 | struct list_head *list) |
| 463 | { |
| 464 | struct list_head *cur; |
| 465 | struct btrfs_ordered_sum *sum; |
| 466 | |
| 467 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 468 | list_for_each(cur, list) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 469 | sum = list_entry(cur, struct btrfs_ordered_sum, list); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 470 | btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root, |
| 471 | inode, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 476 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
| 477 | { |
| 478 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
| 479 | GFP_NOFS); |
| 480 | } |
| 481 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 482 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 483 | struct btrfs_writepage_fixup { |
| 484 | struct page *page; |
| 485 | struct btrfs_work work; |
| 486 | }; |
| 487 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 488 | void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
| 489 | { |
| 490 | struct btrfs_writepage_fixup *fixup; |
| 491 | struct btrfs_ordered_extent *ordered; |
| 492 | struct page *page; |
| 493 | struct inode *inode; |
| 494 | u64 page_start; |
| 495 | u64 page_end; |
| 496 | |
| 497 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 498 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 499 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 500 | lock_page(page); |
| 501 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 502 | ClearPageChecked(page); |
| 503 | goto out_page; |
| 504 | } |
| 505 | |
| 506 | inode = page->mapping->host; |
| 507 | page_start = page_offset(page); |
| 508 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 509 | |
| 510 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 511 | |
| 512 | /* already ordered? We're done */ |
| 513 | if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 514 | EXTENT_ORDERED, 0)) { |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 515 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 519 | if (ordered) { |
| 520 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, |
| 521 | page_end, GFP_NOFS); |
| 522 | unlock_page(page); |
| 523 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 524 | goto again; |
| 525 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 526 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 527 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 528 | ClearPageChecked(page); |
| 529 | out: |
| 530 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); |
| 531 | out_page: |
| 532 | unlock_page(page); |
| 533 | page_cache_release(page); |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * There are a few paths in the higher layers of the kernel that directly |
| 538 | * set the page dirty bit without asking the filesystem if it is a |
| 539 | * good idea. This causes problems because we want to make sure COW |
| 540 | * properly happens and the data=ordered rules are followed. |
| 541 | * |
| 542 | * In our case any range that doesn't have the EXTENT_ORDERED bit set |
| 543 | * hasn't been properly setup for IO. We kick off an async process |
| 544 | * to fix it up. The async helper will wait for ordered extents, set |
| 545 | * the delalloc bit and make it safe to write the page. |
| 546 | */ |
| 547 | int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
| 548 | { |
| 549 | struct inode *inode = page->mapping->host; |
| 550 | struct btrfs_writepage_fixup *fixup; |
| 551 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 552 | int ret; |
| 553 | |
| 554 | ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, |
| 555 | EXTENT_ORDERED, 0); |
| 556 | if (ret) |
| 557 | return 0; |
| 558 | |
| 559 | if (PageChecked(page)) |
| 560 | return -EAGAIN; |
| 561 | |
| 562 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 563 | if (!fixup) |
| 564 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 565 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 566 | SetPageChecked(page); |
| 567 | page_cache_get(page); |
| 568 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 569 | fixup->page = page; |
| 570 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
| 571 | return -EAGAIN; |
| 572 | } |
| 573 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 574 | /* as ordered data IO finishes, this gets called so we can finish |
| 575 | * an ordered extent if the range of bytes in the file it covers are |
| 576 | * fully written. |
| 577 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 578 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 579 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 580 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 581 | struct btrfs_trans_handle *trans; |
| 582 | struct btrfs_ordered_extent *ordered_extent; |
| 583 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 584 | struct btrfs_file_extent_item *extent_item; |
| 585 | struct btrfs_path *path = NULL; |
| 586 | struct extent_buffer *leaf; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 587 | u64 alloc_hint = 0; |
| 588 | struct list_head list; |
| 589 | struct btrfs_key ins; |
| 590 | int ret; |
| 591 | |
| 592 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 593 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 594 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 595 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 596 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 597 | |
| 598 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); |
| 599 | BUG_ON(!ordered_extent); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 600 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) |
| 601 | goto nocow; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 602 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 603 | path = btrfs_alloc_path(); |
| 604 | BUG_ON(!path); |
| 605 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 606 | lock_extent(io_tree, ordered_extent->file_offset, |
| 607 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 608 | GFP_NOFS); |
| 609 | |
| 610 | INIT_LIST_HEAD(&list); |
| 611 | |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 612 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 613 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 614 | ret = btrfs_drop_extents(trans, root, inode, |
| 615 | ordered_extent->file_offset, |
| 616 | ordered_extent->file_offset + |
| 617 | ordered_extent->len, |
| 618 | ordered_extent->file_offset, &alloc_hint); |
| 619 | BUG_ON(ret); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 620 | |
| 621 | ins.objectid = inode->i_ino; |
| 622 | ins.offset = ordered_extent->file_offset; |
| 623 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 624 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 625 | sizeof(*extent_item)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 626 | BUG_ON(ret); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 627 | leaf = path->nodes[0]; |
| 628 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 629 | struct btrfs_file_extent_item); |
| 630 | btrfs_set_file_extent_generation(leaf, extent_item, trans->transid); |
| 631 | btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG); |
| 632 | btrfs_set_file_extent_disk_bytenr(leaf, extent_item, |
| 633 | ordered_extent->start); |
| 634 | btrfs_set_file_extent_disk_num_bytes(leaf, extent_item, |
| 635 | ordered_extent->len); |
| 636 | btrfs_set_file_extent_offset(leaf, extent_item, 0); |
| 637 | btrfs_set_file_extent_num_bytes(leaf, extent_item, |
| 638 | ordered_extent->len); |
| 639 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 640 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 641 | btrfs_drop_extent_cache(inode, ordered_extent->file_offset, |
| 642 | ordered_extent->file_offset + |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 643 | ordered_extent->len - 1, 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 644 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
| 645 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 646 | ins.objectid = ordered_extent->start; |
| 647 | ins.offset = ordered_extent->len; |
| 648 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 649 | ret = btrfs_alloc_reserved_extent(trans, root, leaf->start, |
| 650 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 651 | trans->transid, inode->i_ino, &ins); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 652 | BUG_ON(ret); |
| 653 | btrfs_release_path(root, path); |
| 654 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 655 | inode_add_bytes(inode, ordered_extent->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 656 | unlock_extent(io_tree, ordered_extent->file_offset, |
| 657 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 658 | GFP_NOFS); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 659 | nocow: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 660 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 661 | &ordered_extent->list); |
| 662 | |
Chris Mason | 3435302 | 2008-09-23 20:19:49 -0400 | [diff] [blame] | 663 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 664 | btrfs_ordered_update_i_size(inode, ordered_extent); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 665 | btrfs_update_inode(trans, root, inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 666 | btrfs_remove_ordered_extent(inode, ordered_extent); |
Chris Mason | 3435302 | 2008-09-23 20:19:49 -0400 | [diff] [blame] | 667 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 668 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 669 | /* once for us */ |
| 670 | btrfs_put_ordered_extent(ordered_extent); |
| 671 | /* once for the tree */ |
| 672 | btrfs_put_ordered_extent(ordered_extent); |
| 673 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 674 | btrfs_end_transaction(trans, root); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 675 | if (path) |
| 676 | btrfs_free_path(path); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 680 | int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
| 681 | struct extent_state *state, int uptodate) |
| 682 | { |
| 683 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 684 | } |
| 685 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 686 | /* |
| 687 | * When IO fails, either with EIO or csum verification fails, we |
| 688 | * try other mirrors that might have a good copy of the data. This |
| 689 | * io_failure_record is used to record state as we go through all the |
| 690 | * mirrors. If another mirror has good data, the page is set up to date |
| 691 | * and things continue. If a good mirror can't be found, the original |
| 692 | * bio end_io callback is called to indicate things have failed. |
| 693 | */ |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 694 | struct io_failure_record { |
| 695 | struct page *page; |
| 696 | u64 start; |
| 697 | u64 len; |
| 698 | u64 logical; |
| 699 | int last_mirror; |
| 700 | }; |
| 701 | |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 702 | int btrfs_io_failed_hook(struct bio *failed_bio, |
| 703 | struct page *page, u64 start, u64 end, |
| 704 | struct extent_state *state) |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 705 | { |
| 706 | struct io_failure_record *failrec = NULL; |
| 707 | u64 private; |
| 708 | struct extent_map *em; |
| 709 | struct inode *inode = page->mapping->host; |
| 710 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 711 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 712 | struct bio *bio; |
| 713 | int num_copies; |
| 714 | int ret; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 715 | int rw; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 716 | u64 logical; |
| 717 | |
| 718 | ret = get_state_private(failure_tree, start, &private); |
| 719 | if (ret) { |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 720 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
| 721 | if (!failrec) |
| 722 | return -ENOMEM; |
| 723 | failrec->start = start; |
| 724 | failrec->len = end - start + 1; |
| 725 | failrec->last_mirror = 0; |
| 726 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 727 | spin_lock(&em_tree->lock); |
| 728 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 729 | if (em->start > start || em->start + em->len < start) { |
| 730 | free_extent_map(em); |
| 731 | em = NULL; |
| 732 | } |
| 733 | spin_unlock(&em_tree->lock); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 734 | |
| 735 | if (!em || IS_ERR(em)) { |
| 736 | kfree(failrec); |
| 737 | return -EIO; |
| 738 | } |
| 739 | logical = start - em->start; |
| 740 | logical = em->block_start + logical; |
| 741 | failrec->logical = logical; |
| 742 | free_extent_map(em); |
| 743 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | |
| 744 | EXTENT_DIRTY, GFP_NOFS); |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 745 | set_state_private(failure_tree, start, |
| 746 | (u64)(unsigned long)failrec); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 747 | } else { |
Chris Mason | 587f770 | 2008-04-11 12:16:46 -0400 | [diff] [blame] | 748 | failrec = (struct io_failure_record *)(unsigned long)private; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 749 | } |
| 750 | num_copies = btrfs_num_copies( |
| 751 | &BTRFS_I(inode)->root->fs_info->mapping_tree, |
| 752 | failrec->logical, failrec->len); |
| 753 | failrec->last_mirror++; |
| 754 | if (!state) { |
| 755 | spin_lock_irq(&BTRFS_I(inode)->io_tree.lock); |
| 756 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, |
| 757 | failrec->start, |
| 758 | EXTENT_LOCKED); |
| 759 | if (state && state->start != failrec->start) |
| 760 | state = NULL; |
| 761 | spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock); |
| 762 | } |
| 763 | if (!state || failrec->last_mirror > num_copies) { |
| 764 | set_state_private(failure_tree, failrec->start, 0); |
| 765 | clear_extent_bits(failure_tree, failrec->start, |
| 766 | failrec->start + failrec->len - 1, |
| 767 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); |
| 768 | kfree(failrec); |
| 769 | return -EIO; |
| 770 | } |
| 771 | bio = bio_alloc(GFP_NOFS, 1); |
| 772 | bio->bi_private = state; |
| 773 | bio->bi_end_io = failed_bio->bi_end_io; |
| 774 | bio->bi_sector = failrec->logical >> 9; |
| 775 | bio->bi_bdev = failed_bio->bi_bdev; |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 776 | bio->bi_size = 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 777 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 778 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
| 779 | rw = WRITE; |
| 780 | else |
| 781 | rw = READ; |
| 782 | |
| 783 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, |
| 784 | failrec->last_mirror); |
| 785 | return 0; |
| 786 | } |
| 787 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 788 | /* |
| 789 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 790 | * to see if we need to process or clean up an io_failure_record |
| 791 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 792 | int btrfs_clean_io_failures(struct inode *inode, u64 start) |
| 793 | { |
| 794 | u64 private; |
| 795 | u64 private_failure; |
| 796 | struct io_failure_record *failure; |
| 797 | int ret; |
| 798 | |
| 799 | private = 0; |
| 800 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, |
| 801 | (u64)-1, 1, EXTENT_DIRTY)) { |
| 802 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 803 | start, &private_failure); |
| 804 | if (ret == 0) { |
| 805 | failure = (struct io_failure_record *)(unsigned long) |
| 806 | private_failure; |
| 807 | set_state_private(&BTRFS_I(inode)->io_failure_tree, |
| 808 | failure->start, 0); |
| 809 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, |
| 810 | failure->start, |
| 811 | failure->start + failure->len - 1, |
| 812 | EXTENT_DIRTY | EXTENT_LOCKED, |
| 813 | GFP_NOFS); |
| 814 | kfree(failure); |
| 815 | } |
| 816 | } |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 817 | return 0; |
| 818 | } |
| 819 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 820 | /* |
| 821 | * when reads are done, we need to check csums to verify the data is correct |
| 822 | * if there's a match, we allow the bio to finish. If not, we go through |
| 823 | * the io_failure_record routines to find good copies |
| 824 | */ |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 825 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
| 826 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 827 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 828 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 829 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 830 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 831 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 832 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 833 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 834 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 835 | u32 csum = ~(u32)0; |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 836 | unsigned long flags; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 837 | |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 838 | if (btrfs_test_opt(root, NODATASUM) || |
| 839 | btrfs_test_flag(inode, NODATASUM)) |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 840 | return 0; |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 841 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 842 | private = state->private; |
| 843 | ret = 0; |
| 844 | } else { |
| 845 | ret = get_state_private(io_tree, start, &private); |
| 846 | } |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 847 | local_irq_save(flags); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 848 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 849 | if (ret) { |
| 850 | goto zeroit; |
| 851 | } |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 852 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 853 | btrfs_csum_final(csum, (char *)&csum); |
| 854 | if (csum != private) { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 855 | goto zeroit; |
| 856 | } |
| 857 | kunmap_atomic(kaddr, KM_IRQ0); |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 858 | local_irq_restore(flags); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 859 | |
| 860 | /* if the io failure tree for this inode is non-empty, |
| 861 | * check to see if we've recovered from a failed IO |
| 862 | */ |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 863 | btrfs_clean_io_failures(inode, start); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 864 | return 0; |
| 865 | |
| 866 | zeroit: |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 867 | printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n", |
| 868 | page->mapping->host->i_ino, (unsigned long long)start, csum, |
| 869 | private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 870 | memset(kaddr + offset, 1, end - start + 1); |
| 871 | flush_dcache_page(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 872 | kunmap_atomic(kaddr, KM_IRQ0); |
Jens Axboe | bbf0d00 | 2007-10-19 09:23:07 -0400 | [diff] [blame] | 873 | local_irq_restore(flags); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 874 | if (private == 0) |
| 875 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 876 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 877 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 878 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 879 | /* |
| 880 | * This creates an orphan entry for the given inode in case something goes |
| 881 | * wrong in the middle of an unlink/truncate. |
| 882 | */ |
| 883 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 884 | { |
| 885 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 886 | int ret = 0; |
| 887 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 888 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 889 | |
| 890 | /* already on the orphan list, we're good */ |
| 891 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 892 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 897 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 898 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 899 | |
| 900 | /* |
| 901 | * insert an orphan item to track this unlinked/truncated file |
| 902 | */ |
| 903 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 910 | * item for this particular inode. |
| 911 | */ |
| 912 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 913 | { |
| 914 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 915 | int ret = 0; |
| 916 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 917 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 918 | |
| 919 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 920 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 925 | if (!trans) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 926 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 930 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 931 | |
| 932 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); |
| 933 | |
| 934 | return ret; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * this cleans up any orphans that may be left on the list from the last use |
| 939 | * of this root. |
| 940 | */ |
| 941 | void btrfs_orphan_cleanup(struct btrfs_root *root) |
| 942 | { |
| 943 | struct btrfs_path *path; |
| 944 | struct extent_buffer *leaf; |
| 945 | struct btrfs_item *item; |
| 946 | struct btrfs_key key, found_key; |
| 947 | struct btrfs_trans_handle *trans; |
| 948 | struct inode *inode; |
| 949 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 950 | |
| 951 | /* don't do orphan cleanup if the fs is readonly. */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 952 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 953 | return; |
| 954 | |
| 955 | path = btrfs_alloc_path(); |
| 956 | if (!path) |
| 957 | return; |
| 958 | path->reada = -1; |
| 959 | |
| 960 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 961 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 962 | key.offset = (u64)-1; |
| 963 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 964 | |
| 965 | while (1) { |
| 966 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 967 | if (ret < 0) { |
| 968 | printk(KERN_ERR "Error searching slot for orphan: %d" |
| 969 | "\n", ret); |
| 970 | break; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * if ret == 0 means we found what we were searching for, which |
| 975 | * is weird, but possible, so only screw with path if we didnt |
| 976 | * find the key and see if we have stuff that matches |
| 977 | */ |
| 978 | if (ret > 0) { |
| 979 | if (path->slots[0] == 0) |
| 980 | break; |
| 981 | path->slots[0]--; |
| 982 | } |
| 983 | |
| 984 | /* pull out the item */ |
| 985 | leaf = path->nodes[0]; |
| 986 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 987 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 988 | |
| 989 | /* make sure the item matches what we want */ |
| 990 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 991 | break; |
| 992 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 993 | break; |
| 994 | |
| 995 | /* release the path since we're done with it */ |
| 996 | btrfs_release_path(root, path); |
| 997 | |
| 998 | /* |
| 999 | * this is where we are basically btrfs_lookup, without the |
| 1000 | * crossing root thing. we store the inode number in the |
| 1001 | * offset of the orphan item. |
| 1002 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1003 | inode = btrfs_iget_locked(root->fs_info->sb, |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1004 | found_key.offset, root); |
| 1005 | if (!inode) |
| 1006 | break; |
| 1007 | |
| 1008 | if (inode->i_state & I_NEW) { |
| 1009 | BTRFS_I(inode)->root = root; |
| 1010 | |
| 1011 | /* have to set the location manually */ |
| 1012 | BTRFS_I(inode)->location.objectid = inode->i_ino; |
| 1013 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 1014 | BTRFS_I(inode)->location.offset = 0; |
| 1015 | |
| 1016 | btrfs_read_locked_inode(inode); |
| 1017 | unlock_new_inode(inode); |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * add this inode to the orphan list so btrfs_orphan_del does |
| 1022 | * the proper thing when we hit it |
| 1023 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1024 | spin_lock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1025 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1026 | spin_unlock(&root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1027 | |
| 1028 | /* |
| 1029 | * if this is a bad inode, means we actually succeeded in |
| 1030 | * removing the inode, but not the orphan record, which means |
| 1031 | * we need to manually delete the orphan since iput will just |
| 1032 | * do a destroy_inode |
| 1033 | */ |
| 1034 | if (is_bad_inode(inode)) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1035 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1036 | btrfs_orphan_del(trans, inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1037 | btrfs_end_transaction(trans, root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1038 | iput(inode); |
| 1039 | continue; |
| 1040 | } |
| 1041 | |
| 1042 | /* if we have links, this was a truncate, lets do that */ |
| 1043 | if (inode->i_nlink) { |
| 1044 | nr_truncate++; |
| 1045 | btrfs_truncate(inode); |
| 1046 | } else { |
| 1047 | nr_unlink++; |
| 1048 | } |
| 1049 | |
| 1050 | /* this will do delete_inode and everything for us */ |
| 1051 | iput(inode); |
| 1052 | } |
| 1053 | |
| 1054 | if (nr_unlink) |
| 1055 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 1056 | if (nr_truncate) |
| 1057 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
| 1058 | |
| 1059 | btrfs_free_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1062 | /* |
| 1063 | * read an inode from the btree into the in-memory inode |
| 1064 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1065 | void btrfs_read_locked_inode(struct inode *inode) |
| 1066 | { |
| 1067 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1068 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1069 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1070 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1071 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1072 | struct btrfs_key location; |
| 1073 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1074 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1075 | int ret; |
| 1076 | |
| 1077 | path = btrfs_alloc_path(); |
| 1078 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1079 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1080 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1081 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1082 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1083 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1084 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1085 | leaf = path->nodes[0]; |
| 1086 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 1087 | struct btrfs_inode_item); |
| 1088 | |
| 1089 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
| 1090 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); |
| 1091 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 1092 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 1093 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1094 | |
| 1095 | tspec = btrfs_inode_atime(inode_item); |
| 1096 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 1097 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 1098 | |
| 1099 | tspec = btrfs_inode_mtime(inode_item); |
| 1100 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 1101 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 1102 | |
| 1103 | tspec = btrfs_inode_ctime(inode_item); |
| 1104 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 1105 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 1106 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 1107 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1108 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
| 1109 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1110 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1111 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 1112 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 1113 | BTRFS_I(inode)->index_cnt = (u64)-1; |
| 1114 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1115 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1116 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
| 1117 | alloc_group_block); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 1118 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Chris Mason | e52ec0e | 2007-12-21 16:36:24 -0500 | [diff] [blame] | 1119 | if (!BTRFS_I(inode)->block_group) { |
| 1120 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1121 | NULL, 0, |
| 1122 | BTRFS_BLOCK_GROUP_METADATA, 0); |
Chris Mason | e52ec0e | 2007-12-21 16:36:24 -0500 | [diff] [blame] | 1123 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1124 | btrfs_free_path(path); |
| 1125 | inode_item = NULL; |
| 1126 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1127 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1128 | case S_IFREG: |
| 1129 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 1130 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1131 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1132 | inode->i_fop = &btrfs_file_operations; |
| 1133 | inode->i_op = &btrfs_file_inode_operations; |
| 1134 | break; |
| 1135 | case S_IFDIR: |
| 1136 | inode->i_fop = &btrfs_dir_file_operations; |
| 1137 | if (root == root->fs_info->tree_root) |
| 1138 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 1139 | else |
| 1140 | inode->i_op = &btrfs_dir_inode_operations; |
| 1141 | break; |
| 1142 | case S_IFLNK: |
| 1143 | inode->i_op = &btrfs_symlink_inode_operations; |
| 1144 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 1145 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1146 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1147 | default: |
| 1148 | init_special_inode(inode, inode->i_mode, rdev); |
| 1149 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1150 | } |
| 1151 | return; |
| 1152 | |
| 1153 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1154 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1155 | make_bad_inode(inode); |
| 1156 | } |
| 1157 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1158 | /* |
| 1159 | * given a leaf and an inode, copy the inode fields into the leaf |
| 1160 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1161 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 1162 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1163 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1164 | struct inode *inode) |
| 1165 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1166 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 1167 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 1168 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1169 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 1170 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 1171 | |
| 1172 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 1173 | inode->i_atime.tv_sec); |
| 1174 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 1175 | inode->i_atime.tv_nsec); |
| 1176 | |
| 1177 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 1178 | inode->i_mtime.tv_sec); |
| 1179 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 1180 | inode->i_mtime.tv_nsec); |
| 1181 | |
| 1182 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 1183 | inode->i_ctime.tv_sec); |
| 1184 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 1185 | inode->i_ctime.tv_nsec); |
| 1186 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 1187 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1188 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
| 1189 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1190 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 1191 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1192 | btrfs_set_inode_block_group(leaf, item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1193 | BTRFS_I(inode)->block_group->key.objectid); |
| 1194 | } |
| 1195 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1196 | /* |
| 1197 | * copy everything in the in-memory inode into the btree. |
| 1198 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1199 | int noinline btrfs_update_inode(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1200 | struct btrfs_root *root, |
| 1201 | struct inode *inode) |
| 1202 | { |
| 1203 | struct btrfs_inode_item *inode_item; |
| 1204 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1205 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1206 | int ret; |
| 1207 | |
| 1208 | path = btrfs_alloc_path(); |
| 1209 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1210 | ret = btrfs_lookup_inode(trans, root, path, |
| 1211 | &BTRFS_I(inode)->location, 1); |
| 1212 | if (ret) { |
| 1213 | if (ret > 0) |
| 1214 | ret = -ENOENT; |
| 1215 | goto failed; |
| 1216 | } |
| 1217 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1218 | leaf = path->nodes[0]; |
| 1219 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1220 | struct btrfs_inode_item); |
| 1221 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1222 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1223 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1224 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1225 | ret = 0; |
| 1226 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1227 | btrfs_free_path(path); |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1232 | /* |
| 1233 | * unlink helper that gets used here in inode.c and in the tree logging |
| 1234 | * recovery code. It remove a link in a directory with a given name, and |
| 1235 | * also drops the back refs in the inode to the directory |
| 1236 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1237 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 1238 | struct btrfs_root *root, |
| 1239 | struct inode *dir, struct inode *inode, |
| 1240 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1241 | { |
| 1242 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1243 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1244 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1245 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1246 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 1247 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1248 | |
| 1249 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1250 | if (!path) { |
| 1251 | ret = -ENOMEM; |
| 1252 | goto err; |
| 1253 | } |
| 1254 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1255 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 1256 | name, name_len, -1); |
| 1257 | if (IS_ERR(di)) { |
| 1258 | ret = PTR_ERR(di); |
| 1259 | goto err; |
| 1260 | } |
| 1261 | if (!di) { |
| 1262 | ret = -ENOENT; |
| 1263 | goto err; |
| 1264 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1265 | leaf = path->nodes[0]; |
| 1266 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1267 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1268 | if (ret) |
| 1269 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1270 | btrfs_release_path(root, path); |
| 1271 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 1272 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1273 | inode->i_ino, |
| 1274 | dir->i_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 1275 | if (ret) { |
| 1276 | printk("failed to delete reference to %.*s, " |
| 1277 | "inode %lu parent %lu\n", name_len, name, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1278 | inode->i_ino, dir->i_ino); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 1279 | goto err; |
| 1280 | } |
| 1281 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1282 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 1283 | index, name, name_len, -1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1284 | if (IS_ERR(di)) { |
| 1285 | ret = PTR_ERR(di); |
| 1286 | goto err; |
| 1287 | } |
| 1288 | if (!di) { |
| 1289 | ret = -ENOENT; |
| 1290 | goto err; |
| 1291 | } |
| 1292 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1293 | btrfs_release_path(root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1294 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1295 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
| 1296 | inode, dir->i_ino); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1297 | BUG_ON(ret != 0 && ret != -ENOENT); |
| 1298 | if (ret != -ENOENT) |
| 1299 | BTRFS_I(dir)->log_dirty_trans = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1300 | |
| 1301 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 1302 | dir, index); |
| 1303 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1304 | err: |
| 1305 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1306 | if (ret) |
| 1307 | goto out; |
| 1308 | |
| 1309 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 1310 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 1311 | btrfs_update_inode(trans, root, dir); |
| 1312 | btrfs_drop_nlink(inode); |
| 1313 | ret = btrfs_update_inode(trans, root, inode); |
| 1314 | dir->i_sb->s_dirt = 1; |
| 1315 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1316 | return ret; |
| 1317 | } |
| 1318 | |
| 1319 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 1320 | { |
| 1321 | struct btrfs_root *root; |
| 1322 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1323 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1324 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1325 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1326 | |
| 1327 | root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1328 | |
| 1329 | ret = btrfs_check_free_space(root, 1, 1); |
| 1330 | if (ret) |
| 1331 | goto fail; |
| 1332 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1333 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1334 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1335 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1336 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 1337 | dentry->d_name.name, dentry->d_name.len); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1338 | |
| 1339 | if (inode->i_nlink == 0) |
| 1340 | ret = btrfs_orphan_add(trans, inode); |
| 1341 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1342 | nr = trans->blocks_used; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1343 | |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1344 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1345 | fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1346 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1347 | return ret; |
| 1348 | } |
| 1349 | |
| 1350 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 1351 | { |
| 1352 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1353 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1354 | int ret; |
| 1355 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1356 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1357 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1358 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1359 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 1360 | return -ENOTEMPTY; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1361 | } |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 1362 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1363 | ret = btrfs_check_free_space(root, 1, 1); |
| 1364 | if (ret) |
| 1365 | goto fail; |
| 1366 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1367 | trans = btrfs_start_transaction(root, 1); |
| 1368 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1369 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1370 | err = btrfs_orphan_add(trans, inode); |
| 1371 | if (err) |
| 1372 | goto fail_trans; |
| 1373 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1374 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1375 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 1376 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1377 | if (!err) { |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 1378 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1379 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1380 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1381 | fail_trans: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1382 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1383 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1384 | fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1385 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 1386 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1387 | if (ret && !err) |
| 1388 | err = ret; |
| 1389 | return err; |
| 1390 | } |
| 1391 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1392 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1393 | * when truncating bytes in a file, it is possible to avoid reading |
| 1394 | * the leaves that contain only checksum items. This can be the |
| 1395 | * majority of the IO required to delete a large file, but it must |
| 1396 | * be done carefully. |
| 1397 | * |
| 1398 | * The keys in the level just above the leaves are checked to make sure |
| 1399 | * the lowest key in a given leaf is a csum key, and starts at an offset |
| 1400 | * after the new size. |
| 1401 | * |
| 1402 | * Then the key for the next leaf is checked to make sure it also has |
| 1403 | * a checksum item for the same file. If it does, we know our target leaf |
| 1404 | * contains only checksum items, and it can be safely freed without reading |
| 1405 | * it. |
| 1406 | * |
| 1407 | * This is just an optimization targeted at large files. It may do |
| 1408 | * nothing. It will return 0 unless things went badly. |
| 1409 | */ |
| 1410 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, |
| 1411 | struct btrfs_root *root, |
| 1412 | struct btrfs_path *path, |
| 1413 | struct inode *inode, u64 new_size) |
| 1414 | { |
| 1415 | struct btrfs_key key; |
| 1416 | int ret; |
| 1417 | int nritems; |
| 1418 | struct btrfs_key found_key; |
| 1419 | struct btrfs_key other_key; |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1420 | struct btrfs_leaf_ref *ref; |
| 1421 | u64 leaf_gen; |
| 1422 | u64 leaf_start; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1423 | |
| 1424 | path->lowest_level = 1; |
| 1425 | key.objectid = inode->i_ino; |
| 1426 | key.type = BTRFS_CSUM_ITEM_KEY; |
| 1427 | key.offset = new_size; |
| 1428 | again: |
| 1429 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1430 | if (ret < 0) |
| 1431 | goto out; |
| 1432 | |
| 1433 | if (path->nodes[1] == NULL) { |
| 1434 | ret = 0; |
| 1435 | goto out; |
| 1436 | } |
| 1437 | ret = 0; |
| 1438 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); |
| 1439 | nritems = btrfs_header_nritems(path->nodes[1]); |
| 1440 | |
| 1441 | if (!nritems) |
| 1442 | goto out; |
| 1443 | |
| 1444 | if (path->slots[1] >= nritems) |
| 1445 | goto next_node; |
| 1446 | |
| 1447 | /* did we find a key greater than anything we want to delete? */ |
| 1448 | if (found_key.objectid > inode->i_ino || |
| 1449 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) |
| 1450 | goto out; |
| 1451 | |
| 1452 | /* we check the next key in the node to make sure the leave contains |
| 1453 | * only checksum items. This comparison doesn't work if our |
| 1454 | * leaf is the last one in the node |
| 1455 | */ |
| 1456 | if (path->slots[1] + 1 >= nritems) { |
| 1457 | next_node: |
| 1458 | /* search forward from the last key in the node, this |
| 1459 | * will bring us into the next node in the tree |
| 1460 | */ |
| 1461 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); |
| 1462 | |
| 1463 | /* unlikely, but we inc below, so check to be safe */ |
| 1464 | if (found_key.offset == (u64)-1) |
| 1465 | goto out; |
| 1466 | |
| 1467 | /* search_forward needs a path with locks held, do the |
| 1468 | * search again for the original key. It is possible |
| 1469 | * this will race with a balance and return a path that |
| 1470 | * we could modify, but this drop is just an optimization |
| 1471 | * and is allowed to miss some leaves. |
| 1472 | */ |
| 1473 | btrfs_release_path(root, path); |
| 1474 | found_key.offset++; |
| 1475 | |
| 1476 | /* setup a max key for search_forward */ |
| 1477 | other_key.offset = (u64)-1; |
| 1478 | other_key.type = key.type; |
| 1479 | other_key.objectid = key.objectid; |
| 1480 | |
| 1481 | path->keep_locks = 1; |
| 1482 | ret = btrfs_search_forward(root, &found_key, &other_key, |
| 1483 | path, 0, 0); |
| 1484 | path->keep_locks = 0; |
| 1485 | if (ret || found_key.objectid != key.objectid || |
| 1486 | found_key.type != key.type) { |
| 1487 | ret = 0; |
| 1488 | goto out; |
| 1489 | } |
| 1490 | |
| 1491 | key.offset = found_key.offset; |
| 1492 | btrfs_release_path(root, path); |
| 1493 | cond_resched(); |
| 1494 | goto again; |
| 1495 | } |
| 1496 | |
| 1497 | /* we know there's one more slot after us in the tree, |
| 1498 | * read that key so we can verify it is also a checksum item |
| 1499 | */ |
| 1500 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); |
| 1501 | |
| 1502 | if (found_key.objectid < inode->i_ino) |
| 1503 | goto next_key; |
| 1504 | |
| 1505 | if (found_key.type != key.type || found_key.offset < new_size) |
| 1506 | goto next_key; |
| 1507 | |
| 1508 | /* |
| 1509 | * if the key for the next leaf isn't a csum key from this objectid, |
| 1510 | * we can't be sure there aren't good items inside this leaf. |
| 1511 | * Bail out |
| 1512 | */ |
| 1513 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) |
| 1514 | goto out; |
| 1515 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1516 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
| 1517 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1518 | /* |
| 1519 | * it is safe to delete this leaf, it contains only |
| 1520 | * csum items from this inode at an offset >= new_size |
| 1521 | */ |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1522 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1523 | BUG_ON(ret); |
| 1524 | |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1525 | if (root->ref_cows && leaf_gen < trans->transid) { |
| 1526 | ref = btrfs_alloc_leaf_ref(root, 0); |
| 1527 | if (ref) { |
| 1528 | ref->root_gen = root->root_key.offset; |
| 1529 | ref->bytenr = leaf_start; |
| 1530 | ref->owner = 0; |
| 1531 | ref->generation = leaf_gen; |
| 1532 | ref->nritems = 0; |
| 1533 | |
| 1534 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 1535 | WARN_ON(ret); |
| 1536 | btrfs_free_leaf_ref(root, ref); |
| 1537 | } else { |
| 1538 | WARN_ON(1); |
| 1539 | } |
| 1540 | } |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1541 | next_key: |
| 1542 | btrfs_release_path(root, path); |
| 1543 | |
| 1544 | if (other_key.objectid == inode->i_ino && |
| 1545 | other_key.type == key.type && other_key.offset > key.offset) { |
| 1546 | key.offset = other_key.offset; |
| 1547 | cond_resched(); |
| 1548 | goto again; |
| 1549 | } |
| 1550 | ret = 0; |
| 1551 | out: |
| 1552 | /* fixup any changes we've made to the path */ |
| 1553 | path->lowest_level = 0; |
| 1554 | path->keep_locks = 0; |
| 1555 | btrfs_release_path(root, path); |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1560 | * this can truncate away extent items, csum items and directory items. |
| 1561 | * It starts at a high offset and removes keys until it can't find |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1562 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1563 | * |
| 1564 | * csum items that cross the new i_size are truncated to the new size |
| 1565 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1566 | * |
| 1567 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 1568 | * will kill all the items on this inode, including the INODE_ITEM_KEY. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1569 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1570 | noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 1571 | struct btrfs_root *root, |
| 1572 | struct inode *inode, |
| 1573 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1574 | { |
| 1575 | int ret; |
| 1576 | struct btrfs_path *path; |
| 1577 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1578 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1579 | u32 found_type; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1580 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1581 | struct btrfs_file_extent_item *fi; |
| 1582 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1583 | u64 extent_num_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1584 | u64 item_end = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1585 | u64 root_gen = 0; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1586 | u64 root_owner = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1587 | int found_extent; |
| 1588 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1589 | int pending_del_nr = 0; |
| 1590 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1591 | int extent_type = -1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1592 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1593 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1594 | if (root->ref_cows) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1595 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1596 | path = btrfs_alloc_path(); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1597 | path->reada = -1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1598 | BUG_ON(!path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1599 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1600 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 1601 | key.objectid = inode->i_ino; |
| 1602 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1603 | key.type = (u8)-1; |
| 1604 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1605 | btrfs_init_path(path); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1606 | |
| 1607 | ret = drop_csum_leaves(trans, root, path, inode, new_size); |
| 1608 | BUG_ON(ret); |
| 1609 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1610 | search_again: |
| 1611 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1612 | if (ret < 0) { |
| 1613 | goto error; |
| 1614 | } |
| 1615 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1616 | /* there are no items in the tree for us to truncate, we're |
| 1617 | * done |
| 1618 | */ |
| 1619 | if (path->slots[0] == 0) { |
| 1620 | ret = 0; |
| 1621 | goto error; |
| 1622 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1623 | path->slots[0]--; |
| 1624 | } |
| 1625 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1626 | while(1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1627 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1628 | leaf = path->nodes[0]; |
| 1629 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1630 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1631 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1632 | if (found_key.objectid != inode->i_ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1633 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1634 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1635 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1636 | break; |
| 1637 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1638 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1639 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1640 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1641 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1642 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 1643 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1644 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1645 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1646 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1647 | struct btrfs_item *item = btrfs_item_nr(leaf, |
| 1648 | path->slots[0]); |
| 1649 | item_end += btrfs_file_extent_inline_len(leaf, |
| 1650 | item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1651 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 1652 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1653 | } |
| 1654 | if (found_type == BTRFS_CSUM_ITEM_KEY) { |
| 1655 | ret = btrfs_csum_truncate(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1656 | new_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1657 | BUG_ON(ret); |
| 1658 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1659 | if (item_end < new_size) { |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1660 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
| 1661 | found_type = BTRFS_INODE_ITEM_KEY; |
| 1662 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { |
| 1663 | found_type = BTRFS_CSUM_ITEM_KEY; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1664 | } else if (found_type == BTRFS_EXTENT_DATA_KEY) { |
| 1665 | found_type = BTRFS_XATTR_ITEM_KEY; |
| 1666 | } else if (found_type == BTRFS_XATTR_ITEM_KEY) { |
| 1667 | found_type = BTRFS_INODE_REF_KEY; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1668 | } else if (found_type) { |
| 1669 | found_type--; |
| 1670 | } else { |
| 1671 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1672 | } |
Yan | a61721d | 2007-09-17 11:08:38 -0400 | [diff] [blame] | 1673 | btrfs_set_key_type(&key, found_type); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1674 | goto next; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1675 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1676 | if (found_key.offset >= new_size) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1677 | del_item = 1; |
| 1678 | else |
| 1679 | del_item = 0; |
| 1680 | found_extent = 0; |
| 1681 | |
| 1682 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1683 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 1684 | goto delete; |
| 1685 | |
| 1686 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1687 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1688 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1689 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1690 | u64 orig_num_bytes = |
| 1691 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1692 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1693 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 1694 | extent_num_bytes = extent_num_bytes & |
| 1695 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1696 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1697 | extent_num_bytes); |
| 1698 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1699 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1700 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 1701 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1702 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1703 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1704 | extent_num_bytes = |
| 1705 | btrfs_file_extent_disk_num_bytes(leaf, |
| 1706 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1707 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1708 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1709 | if (extent_start != 0) { |
| 1710 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1711 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 1712 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1713 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1714 | root_gen = btrfs_header_generation(leaf); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1715 | root_owner = btrfs_header_owner(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1716 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1717 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1718 | if (!del_item) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1719 | u32 size = new_size - found_key.offset; |
| 1720 | |
| 1721 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 1722 | inode_sub_bytes(inode, item_end + 1 - |
| 1723 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1724 | } |
| 1725 | size = |
| 1726 | btrfs_file_extent_calc_inline_size(size); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1727 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1728 | size, 1); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1729 | BUG_ON(ret); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1730 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 1731 | inode_sub_bytes(inode, item_end + 1 - |
| 1732 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 1733 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1734 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1735 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1736 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1737 | if (!pending_del_nr) { |
| 1738 | /* no pending yet, add ourselves */ |
| 1739 | pending_del_slot = path->slots[0]; |
| 1740 | pending_del_nr = 1; |
| 1741 | } else if (pending_del_nr && |
| 1742 | path->slots[0] + 1 == pending_del_slot) { |
| 1743 | /* hop on the pending chunk */ |
| 1744 | pending_del_nr++; |
| 1745 | pending_del_slot = path->slots[0]; |
| 1746 | } else { |
| 1747 | printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot); |
| 1748 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1749 | } else { |
| 1750 | break; |
| 1751 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1752 | if (found_extent) { |
| 1753 | ret = btrfs_free_extent(trans, root, extent_start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1754 | extent_num_bytes, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1755 | leaf->start, root_owner, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1756 | root_gen, inode->i_ino, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1757 | BUG_ON(ret); |
| 1758 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1759 | next: |
| 1760 | if (path->slots[0] == 0) { |
| 1761 | if (pending_del_nr) |
| 1762 | goto del_pending; |
| 1763 | btrfs_release_path(root, path); |
| 1764 | goto search_again; |
| 1765 | } |
| 1766 | |
| 1767 | path->slots[0]--; |
| 1768 | if (pending_del_nr && |
| 1769 | path->slots[0] + 1 != pending_del_slot) { |
| 1770 | struct btrfs_key debug; |
| 1771 | del_pending: |
| 1772 | btrfs_item_key_to_cpu(path->nodes[0], &debug, |
| 1773 | pending_del_slot); |
| 1774 | ret = btrfs_del_items(trans, root, path, |
| 1775 | pending_del_slot, |
| 1776 | pending_del_nr); |
| 1777 | BUG_ON(ret); |
| 1778 | pending_del_nr = 0; |
| 1779 | btrfs_release_path(root, path); |
| 1780 | goto search_again; |
| 1781 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1782 | } |
| 1783 | ret = 0; |
| 1784 | error: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1785 | if (pending_del_nr) { |
| 1786 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 1787 | pending_del_nr); |
| 1788 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1789 | btrfs_free_path(path); |
| 1790 | inode->i_sb->s_dirt = 1; |
| 1791 | return ret; |
| 1792 | } |
| 1793 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1794 | /* |
| 1795 | * taken from block_truncate_page, but does cow as it zeros out |
| 1796 | * any bytes left in the last page in the file. |
| 1797 | */ |
| 1798 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 1799 | { |
| 1800 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1801 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1802 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 1803 | struct btrfs_ordered_extent *ordered; |
| 1804 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1805 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1806 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 1807 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 1808 | struct page *page; |
| 1809 | int ret = 0; |
| 1810 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1811 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1812 | |
| 1813 | if ((offset & (blocksize - 1)) == 0) |
| 1814 | goto out; |
| 1815 | |
| 1816 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 1817 | again: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1818 | page = grab_cache_page(mapping, index); |
| 1819 | if (!page) |
| 1820 | goto out; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1821 | |
| 1822 | page_start = page_offset(page); |
| 1823 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 1824 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1825 | if (!PageUptodate(page)) { |
| 1826 | ret = btrfs_readpage(NULL, page); |
| 1827 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 1828 | if (page->mapping != mapping) { |
| 1829 | unlock_page(page); |
| 1830 | page_cache_release(page); |
| 1831 | goto again; |
| 1832 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1833 | if (!PageUptodate(page)) { |
| 1834 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 1835 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1836 | } |
| 1837 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 1838 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1839 | |
| 1840 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 1841 | set_page_extent_mapped(page); |
| 1842 | |
| 1843 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1844 | if (ordered) { |
| 1845 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 1846 | unlock_page(page); |
| 1847 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 1848 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1849 | btrfs_put_ordered_extent(ordered); |
| 1850 | goto again; |
| 1851 | } |
| 1852 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1853 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1854 | ret = 0; |
| 1855 | if (offset != PAGE_CACHE_SIZE) { |
| 1856 | kaddr = kmap(page); |
| 1857 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 1858 | flush_dcache_page(page); |
| 1859 | kunmap(page); |
| 1860 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1861 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1862 | set_page_dirty(page); |
| 1863 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1864 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 1865 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1866 | unlock_page(page); |
| 1867 | page_cache_release(page); |
| 1868 | out: |
| 1869 | return ret; |
| 1870 | } |
| 1871 | |
| 1872 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 1873 | { |
| 1874 | struct inode *inode = dentry->d_inode; |
| 1875 | int err; |
| 1876 | |
| 1877 | err = inode_change_ok(inode, attr); |
| 1878 | if (err) |
| 1879 | return err; |
| 1880 | |
| 1881 | if (S_ISREG(inode->i_mode) && |
| 1882 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { |
| 1883 | struct btrfs_trans_handle *trans; |
| 1884 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1885 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1886 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1887 | u64 mask = root->sectorsize - 1; |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1888 | u64 hole_start = (inode->i_size + mask) & ~mask; |
Chris Mason | f392a93 | 2008-01-30 11:54:05 -0500 | [diff] [blame] | 1889 | u64 block_end = (attr->ia_size + mask) & ~mask; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1890 | u64 hole_size; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1891 | u64 alloc_hint = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1892 | |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1893 | if (attr->ia_size <= hole_start) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1894 | goto out; |
| 1895 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1896 | err = btrfs_check_free_space(root, 1, 0); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1897 | if (err) |
| 1898 | goto fail; |
| 1899 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1900 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 1901 | |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1902 | hole_size = block_end - hole_start; |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 1903 | while(1) { |
| 1904 | struct btrfs_ordered_extent *ordered; |
| 1905 | btrfs_wait_ordered_range(inode, hole_start, hole_size); |
| 1906 | |
| 1907 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
| 1908 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 1909 | if (ordered) { |
| 1910 | unlock_extent(io_tree, hole_start, |
| 1911 | block_end - 1, GFP_NOFS); |
| 1912 | btrfs_put_ordered_extent(ordered); |
| 1913 | } else { |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1917 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1918 | trans = btrfs_start_transaction(root, 1); |
| 1919 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 1920 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1921 | err = btrfs_drop_extents(trans, root, inode, |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1922 | hole_start, block_end, hole_start, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 1923 | &alloc_hint); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 1924 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1925 | if (alloc_hint != EXTENT_MAP_INLINE) { |
| 1926 | err = btrfs_insert_file_extent(trans, root, |
| 1927 | inode->i_ino, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1928 | hole_start, 0, 0, |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 1929 | hole_size, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1930 | btrfs_drop_extent_cache(inode, hole_start, |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1931 | (u64)-1, 0); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 1932 | btrfs_check_file(root, inode); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1933 | } |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 1934 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1935 | btrfs_end_transaction(trans, root); |
Chris Mason | 1b0f7c2 | 2008-01-30 14:33:02 -0500 | [diff] [blame] | 1936 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1937 | if (err) |
| 1938 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1939 | } |
| 1940 | out: |
| 1941 | err = inode_setattr(inode, attr); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 1942 | |
| 1943 | if (!err && ((attr->ia_valid & ATTR_MODE))) |
| 1944 | err = btrfs_acl_chmod(inode); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1945 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1946 | return err; |
| 1947 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 1948 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1949 | void btrfs_delete_inode(struct inode *inode) |
| 1950 | { |
| 1951 | struct btrfs_trans_handle *trans; |
| 1952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1953 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1954 | int ret; |
| 1955 | |
| 1956 | truncate_inode_pages(&inode->i_data, 0); |
| 1957 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1958 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1959 | goto no_delete; |
| 1960 | } |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1961 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1962 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 1963 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1964 | trans = btrfs_start_transaction(root, 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 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1967 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1968 | if (ret) { |
| 1969 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1970 | goto no_delete_lock; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | btrfs_orphan_del(trans, inode); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1974 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1975 | nr = trans->blocks_used; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 1976 | clear_inode(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1977 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1978 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1979 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1980 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1981 | |
| 1982 | no_delete_lock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1983 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1984 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1985 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1986 | no_delete: |
| 1987 | clear_inode(inode); |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * this returns the key found in the dir entry in the location pointer. |
| 1992 | * If no dir entries were found, location->objectid is 0. |
| 1993 | */ |
| 1994 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 1995 | struct btrfs_key *location) |
| 1996 | { |
| 1997 | const char *name = dentry->d_name.name; |
| 1998 | int namelen = dentry->d_name.len; |
| 1999 | struct btrfs_dir_item *di; |
| 2000 | struct btrfs_path *path; |
| 2001 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 2002 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2003 | |
| 2004 | path = btrfs_alloc_path(); |
| 2005 | BUG_ON(!path); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2006 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2007 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 2008 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 2009 | if (IS_ERR(di)) |
| 2010 | ret = PTR_ERR(di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2011 | if (!di || IS_ERR(di)) { |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2012 | goto out_err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2013 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2014 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2015 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2016 | btrfs_free_path(path); |
| 2017 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2018 | out_err: |
| 2019 | location->objectid = 0; |
| 2020 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | /* |
| 2024 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 2025 | * needs to be changed to reflect the root directory of the tree root. This |
| 2026 | * is kind of like crossing a mount point. |
| 2027 | */ |
| 2028 | static int fixup_tree_root_location(struct btrfs_root *root, |
| 2029 | struct btrfs_key *location, |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2030 | struct btrfs_root **sub_root, |
| 2031 | struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2032 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2033 | struct btrfs_root_item *ri; |
| 2034 | |
| 2035 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 2036 | return 0; |
| 2037 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 2038 | return 0; |
| 2039 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2040 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
| 2041 | dentry->d_name.name, |
| 2042 | dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2043 | if (IS_ERR(*sub_root)) |
| 2044 | return PTR_ERR(*sub_root); |
| 2045 | |
| 2046 | ri = &(*sub_root)->root_item; |
| 2047 | location->objectid = btrfs_root_dirid(ri); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2048 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 2049 | location->offset = 0; |
| 2050 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2051 | return 0; |
| 2052 | } |
| 2053 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2054 | static noinline void init_btrfs_i(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2055 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2056 | struct btrfs_inode *bi = BTRFS_I(inode); |
| 2057 | |
| 2058 | bi->i_acl = NULL; |
| 2059 | bi->i_default_acl = NULL; |
| 2060 | |
| 2061 | bi->generation = 0; |
| 2062 | bi->last_trans = 0; |
| 2063 | bi->logged_trans = 0; |
| 2064 | bi->delalloc_bytes = 0; |
| 2065 | bi->disk_i_size = 0; |
| 2066 | bi->flags = 0; |
| 2067 | bi->index_cnt = (u64)-1; |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2068 | bi->log_dirty_trans = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2069 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
| 2070 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2071 | inode->i_mapping, GFP_NOFS); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2072 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
| 2073 | inode->i_mapping, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 2074 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 2075 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
Chris Mason | 1b1e213 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2076 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 2077 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2078 | mutex_init(&BTRFS_I(inode)->log_mutex); |
| 2079 | } |
| 2080 | |
| 2081 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 2082 | { |
| 2083 | struct btrfs_iget_args *args = p; |
| 2084 | inode->i_ino = args->ino; |
| 2085 | init_btrfs_i(inode); |
| 2086 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 2091 | { |
| 2092 | struct btrfs_iget_args *args = opaque; |
| 2093 | return (args->ino == inode->i_ino && |
| 2094 | args->root == BTRFS_I(inode)->root); |
| 2095 | } |
| 2096 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2097 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
| 2098 | struct btrfs_root *root, int wait) |
| 2099 | { |
| 2100 | struct inode *inode; |
| 2101 | struct btrfs_iget_args args; |
| 2102 | args.ino = objectid; |
| 2103 | args.root = root; |
| 2104 | |
| 2105 | if (wait) { |
| 2106 | inode = ilookup5(s, objectid, btrfs_find_actor, |
| 2107 | (void *)&args); |
| 2108 | } else { |
| 2109 | inode = ilookup5_nowait(s, objectid, btrfs_find_actor, |
| 2110 | (void *)&args); |
| 2111 | } |
| 2112 | return inode; |
| 2113 | } |
| 2114 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2115 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 2116 | struct btrfs_root *root) |
| 2117 | { |
| 2118 | struct inode *inode; |
| 2119 | struct btrfs_iget_args args; |
| 2120 | args.ino = objectid; |
| 2121 | args.root = root; |
| 2122 | |
| 2123 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 2124 | btrfs_init_locked_inode, |
| 2125 | (void *)&args); |
| 2126 | return inode; |
| 2127 | } |
| 2128 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 2129 | /* Get an inode object given its location and corresponding root. |
| 2130 | * Returns in *is_new if the inode was read from disk |
| 2131 | */ |
| 2132 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
| 2133 | struct btrfs_root *root, int *is_new) |
| 2134 | { |
| 2135 | struct inode *inode; |
| 2136 | |
| 2137 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 2138 | if (!inode) |
| 2139 | return ERR_PTR(-EACCES); |
| 2140 | |
| 2141 | if (inode->i_state & I_NEW) { |
| 2142 | BTRFS_I(inode)->root = root; |
| 2143 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 2144 | btrfs_read_locked_inode(inode); |
| 2145 | unlock_new_inode(inode); |
| 2146 | if (is_new) |
| 2147 | *is_new = 1; |
| 2148 | } else { |
| 2149 | if (is_new) |
| 2150 | *is_new = 0; |
| 2151 | } |
| 2152 | |
| 2153 | return inode; |
| 2154 | } |
| 2155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2156 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 2157 | struct nameidata *nd) |
| 2158 | { |
| 2159 | struct inode * inode; |
| 2160 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 2161 | struct btrfs_root *root = bi->root; |
| 2162 | struct btrfs_root *sub_root = root; |
| 2163 | struct btrfs_key location; |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 2164 | int ret, new, do_orphan = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2165 | |
| 2166 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 2167 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2168 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2169 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2170 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2171 | if (ret < 0) |
| 2172 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2173 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2174 | inode = NULL; |
| 2175 | if (location.objectid) { |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2176 | ret = fixup_tree_root_location(root, &location, &sub_root, |
| 2177 | dentry); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2178 | if (ret < 0) |
| 2179 | return ERR_PTR(ret); |
| 2180 | if (ret > 0) |
| 2181 | return ERR_PTR(-ENOENT); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 2182 | inode = btrfs_iget(dir->i_sb, &location, sub_root, &new); |
| 2183 | if (IS_ERR(inode)) |
| 2184 | return ERR_CAST(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2185 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 2186 | /* the inode and parent dir are two different roots */ |
| 2187 | if (new && root != sub_root) { |
| 2188 | igrab(inode); |
| 2189 | sub_root->inode = inode; |
| 2190 | do_orphan = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2191 | } |
| 2192 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2193 | |
| 2194 | if (unlikely(do_orphan)) |
| 2195 | btrfs_orphan_cleanup(sub_root); |
| 2196 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2197 | return d_splice_alias(inode, dentry); |
| 2198 | } |
| 2199 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2200 | static unsigned char btrfs_filetype_table[] = { |
| 2201 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 2202 | }; |
| 2203 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 2204 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 2205 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2206 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 2207 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2208 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2209 | struct btrfs_item *item; |
| 2210 | struct btrfs_dir_item *di; |
| 2211 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2212 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2213 | struct btrfs_path *path; |
| 2214 | int ret; |
| 2215 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2216 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2217 | int slot; |
| 2218 | int advance; |
| 2219 | unsigned char d_type; |
| 2220 | int over = 0; |
| 2221 | u32 di_cur; |
| 2222 | u32 di_total; |
| 2223 | u32 di_len; |
| 2224 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2225 | char tmp_name[32]; |
| 2226 | char *name_ptr; |
| 2227 | int name_len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2228 | |
| 2229 | /* FIXME, use a real flag for deciding about the key type */ |
| 2230 | if (root->fs_info->tree_root == root) |
| 2231 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2232 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2233 | /* special case for "." */ |
| 2234 | if (filp->f_pos == 0) { |
| 2235 | over = filldir(dirent, ".", 1, |
| 2236 | 1, inode->i_ino, |
| 2237 | DT_DIR); |
| 2238 | if (over) |
| 2239 | return 0; |
| 2240 | filp->f_pos = 1; |
| 2241 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2242 | /* special case for .., just use the back ref */ |
| 2243 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 2244 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2245 | over = filldir(dirent, "..", 2, |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 2246 | 2, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2247 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2248 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 2249 | filp->f_pos = 2; |
| 2250 | } |
| 2251 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2252 | path = btrfs_alloc_path(); |
| 2253 | path->reada = 2; |
| 2254 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2255 | btrfs_set_key_type(&key, key_type); |
| 2256 | key.offset = filp->f_pos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2257 | key.objectid = inode->i_ino; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2258 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2259 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2260 | if (ret < 0) |
| 2261 | goto err; |
| 2262 | advance = 0; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2263 | |
| 2264 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2265 | leaf = path->nodes[0]; |
| 2266 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2267 | slot = path->slots[0]; |
| 2268 | if (advance || slot >= nritems) { |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2269 | if (slot >= nritems - 1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2270 | ret = btrfs_next_leaf(root, path); |
| 2271 | if (ret) |
| 2272 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2273 | leaf = path->nodes[0]; |
| 2274 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2275 | slot = path->slots[0]; |
| 2276 | } else { |
| 2277 | slot++; |
| 2278 | path->slots[0]++; |
| 2279 | } |
| 2280 | } |
| 2281 | advance = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2282 | item = btrfs_item_nr(leaf, slot); |
| 2283 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2284 | |
| 2285 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2286 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2287 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2288 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2289 | if (found_key.offset < filp->f_pos) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2290 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2291 | |
| 2292 | filp->f_pos = found_key.offset; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2293 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2294 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 2295 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2296 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2297 | |
| 2298 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2299 | struct btrfs_key location; |
| 2300 | |
| 2301 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2302 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2303 | name_ptr = tmp_name; |
| 2304 | } else { |
| 2305 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2306 | if (!name_ptr) { |
| 2307 | ret = -ENOMEM; |
| 2308 | goto err; |
| 2309 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2310 | } |
| 2311 | read_extent_buffer(leaf, name_ptr, |
| 2312 | (unsigned long)(di + 1), name_len); |
| 2313 | |
| 2314 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 2315 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2316 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2317 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2318 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2319 | |
| 2320 | if (name_ptr != tmp_name) |
| 2321 | kfree(name_ptr); |
| 2322 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2323 | if (over) |
| 2324 | goto nopos; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2325 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 2326 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2327 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2328 | di_cur += di_len; |
| 2329 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 2330 | } |
| 2331 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 2332 | |
| 2333 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 2334 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 2335 | filp->f_pos = INT_LIMIT(typeof(filp->f_pos)); |
| 2336 | else |
| 2337 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2338 | nopos: |
| 2339 | ret = 0; |
| 2340 | err: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2341 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2342 | return ret; |
| 2343 | } |
| 2344 | |
| 2345 | int btrfs_write_inode(struct inode *inode, int wait) |
| 2346 | { |
| 2347 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2348 | struct btrfs_trans_handle *trans; |
| 2349 | int ret = 0; |
| 2350 | |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 2351 | if (root->fs_info->closing > 1) |
| 2352 | return 0; |
| 2353 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2354 | if (wait) { |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 2355 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2356 | btrfs_set_trans_block_group(trans, inode); |
| 2357 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2358 | } |
| 2359 | return ret; |
| 2360 | } |
| 2361 | |
| 2362 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2363 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2364 | * inode changes. But, it is most likely to find the inode in cache. |
| 2365 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 2366 | * to keep or drop this code. |
| 2367 | */ |
| 2368 | void btrfs_dirty_inode(struct inode *inode) |
| 2369 | { |
| 2370 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2371 | struct btrfs_trans_handle *trans; |
| 2372 | |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 2373 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2374 | btrfs_set_trans_block_group(trans, inode); |
| 2375 | btrfs_update_inode(trans, root, inode); |
| 2376 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2377 | } |
| 2378 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2379 | /* |
| 2380 | * find the highest existing sequence number in a directory |
| 2381 | * and then set the in-memory index_cnt variable to reflect |
| 2382 | * free sequence numbers |
| 2383 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2384 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 2385 | { |
| 2386 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2387 | struct btrfs_key key, found_key; |
| 2388 | struct btrfs_path *path; |
| 2389 | struct extent_buffer *leaf; |
| 2390 | int ret; |
| 2391 | |
| 2392 | key.objectid = inode->i_ino; |
| 2393 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 2394 | key.offset = (u64)-1; |
| 2395 | |
| 2396 | path = btrfs_alloc_path(); |
| 2397 | if (!path) |
| 2398 | return -ENOMEM; |
| 2399 | |
| 2400 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2401 | if (ret < 0) |
| 2402 | goto out; |
| 2403 | /* FIXME: we should be able to handle this */ |
| 2404 | if (ret == 0) |
| 2405 | goto out; |
| 2406 | ret = 0; |
| 2407 | |
| 2408 | /* |
| 2409 | * MAGIC NUMBER EXPLANATION: |
| 2410 | * since we search a directory based on f_pos we have to start at 2 |
| 2411 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 2412 | * else has to start at 2 |
| 2413 | */ |
| 2414 | if (path->slots[0] == 0) { |
| 2415 | BTRFS_I(inode)->index_cnt = 2; |
| 2416 | goto out; |
| 2417 | } |
| 2418 | |
| 2419 | path->slots[0]--; |
| 2420 | |
| 2421 | leaf = path->nodes[0]; |
| 2422 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2423 | |
| 2424 | if (found_key.objectid != inode->i_ino || |
| 2425 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 2426 | BTRFS_I(inode)->index_cnt = 2; |
| 2427 | goto out; |
| 2428 | } |
| 2429 | |
| 2430 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 2431 | out: |
| 2432 | btrfs_free_path(path); |
| 2433 | return ret; |
| 2434 | } |
| 2435 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2436 | /* |
| 2437 | * helper to find a free sequence number in a given directory. This current |
| 2438 | * code is very simple, later versions will do smarter things in the btree |
| 2439 | */ |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2440 | static int btrfs_set_inode_index(struct inode *dir, struct inode *inode, |
| 2441 | u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2442 | { |
| 2443 | int ret = 0; |
| 2444 | |
| 2445 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
| 2446 | ret = btrfs_set_inode_index_count(dir); |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 2447 | if (ret) { |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2448 | return ret; |
Chris Mason | 8d5bf1c | 2008-09-11 15:51:21 -0400 | [diff] [blame] | 2449 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2450 | } |
| 2451 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2452 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2453 | BTRFS_I(dir)->index_cnt++; |
| 2454 | |
| 2455 | return ret; |
| 2456 | } |
| 2457 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2458 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 2459 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2460 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2461 | const char *name, int name_len, |
| 2462 | u64 ref_objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2463 | u64 objectid, |
| 2464 | struct btrfs_block_group_cache *group, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2465 | int mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2466 | { |
| 2467 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2468 | struct btrfs_inode_item *inode_item; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2469 | struct btrfs_block_group_cache *new_inode_group; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2470 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2471 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2472 | struct btrfs_inode_ref *ref; |
| 2473 | struct btrfs_key key[2]; |
| 2474 | u32 sizes[2]; |
| 2475 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2476 | int ret; |
| 2477 | int owner; |
| 2478 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2479 | path = btrfs_alloc_path(); |
| 2480 | BUG_ON(!path); |
| 2481 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2482 | inode = new_inode(root->fs_info->sb); |
| 2483 | if (!inode) |
| 2484 | return ERR_PTR(-ENOMEM); |
| 2485 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2486 | if (dir) { |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2487 | ret = btrfs_set_inode_index(dir, inode, index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2488 | if (ret) |
| 2489 | return ERR_PTR(ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2490 | } |
| 2491 | /* |
| 2492 | * index_cnt is ignored for everything but a dir, |
| 2493 | * btrfs_get_inode_index_count has an explanation for the magic |
| 2494 | * number |
| 2495 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2496 | init_btrfs_i(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2497 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2498 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2499 | BTRFS_I(inode)->generation = trans->transid; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2500 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2501 | if (mode & S_IFDIR) |
| 2502 | owner = 0; |
| 2503 | else |
| 2504 | owner = 1; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2505 | new_inode_group = btrfs_find_block_group(root, group, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2506 | BTRFS_BLOCK_GROUP_METADATA, owner); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2507 | if (!new_inode_group) { |
| 2508 | printk("find_block group failed\n"); |
| 2509 | new_inode_group = group; |
| 2510 | } |
| 2511 | BTRFS_I(inode)->block_group = new_inode_group; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2512 | |
| 2513 | key[0].objectid = objectid; |
| 2514 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 2515 | key[0].offset = 0; |
| 2516 | |
| 2517 | key[1].objectid = objectid; |
| 2518 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 2519 | key[1].offset = ref_objectid; |
| 2520 | |
| 2521 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 2522 | sizes[1] = name_len + sizeof(*ref); |
| 2523 | |
| 2524 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 2525 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2526 | goto fail; |
| 2527 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2528 | if (objectid > root->highest_inode) |
| 2529 | root->highest_inode = objectid; |
| 2530 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2531 | inode->i_uid = current->fsuid; |
| 2532 | inode->i_gid = current->fsgid; |
| 2533 | inode->i_mode = mode; |
| 2534 | inode->i_ino = objectid; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2535 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2536 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2537 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2538 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2539 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2540 | |
| 2541 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 2542 | struct btrfs_inode_ref); |
| 2543 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2544 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2545 | ptr = (unsigned long)(ref + 1); |
| 2546 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 2547 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2548 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2549 | btrfs_free_path(path); |
| 2550 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2551 | location = &BTRFS_I(inode)->location; |
| 2552 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2553 | location->offset = 0; |
| 2554 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 2555 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2556 | insert_inode_hash(inode); |
| 2557 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2558 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2559 | if (dir) |
| 2560 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2561 | btrfs_free_path(path); |
| 2562 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 2566 | { |
| 2567 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 2568 | } |
| 2569 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2570 | /* |
| 2571 | * utility function to add 'inode' into 'parent_inode' with |
| 2572 | * a give name and a given sequence number. |
| 2573 | * if 'add_backref' is true, also insert a backref from the |
| 2574 | * inode to the parent directory. |
| 2575 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2576 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 2577 | struct inode *parent_inode, struct inode *inode, |
| 2578 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2579 | { |
| 2580 | int ret; |
| 2581 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2582 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2583 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2584 | key.objectid = inode->i_ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2585 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 2586 | key.offset = 0; |
| 2587 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2588 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 2589 | parent_inode->i_ino, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2590 | &key, btrfs_inode_type(inode), |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2591 | index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2592 | if (ret == 0) { |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2593 | if (add_backref) { |
| 2594 | ret = btrfs_insert_inode_ref(trans, root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2595 | name, name_len, |
| 2596 | inode->i_ino, |
| 2597 | parent_inode->i_ino, |
| 2598 | index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2599 | } |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2600 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2601 | name_len * 2); |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 2602 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2603 | ret = btrfs_update_inode(trans, root, parent_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2604 | } |
| 2605 | return ret; |
| 2606 | } |
| 2607 | |
| 2608 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2609 | struct dentry *dentry, struct inode *inode, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2610 | int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2611 | { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2612 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 2613 | inode, dentry->d_name.name, |
| 2614 | dentry->d_name.len, backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2615 | if (!err) { |
| 2616 | d_instantiate(dentry, inode); |
| 2617 | return 0; |
| 2618 | } |
| 2619 | if (err > 0) |
| 2620 | err = -EEXIST; |
| 2621 | return err; |
| 2622 | } |
| 2623 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2624 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 2625 | int mode, dev_t rdev) |
| 2626 | { |
| 2627 | struct btrfs_trans_handle *trans; |
| 2628 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2629 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2630 | int err; |
| 2631 | int drop_inode = 0; |
| 2632 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2633 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2634 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2635 | |
| 2636 | if (!new_valid_dev(rdev)) |
| 2637 | return -EINVAL; |
| 2638 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2639 | err = btrfs_check_free_space(root, 1, 0); |
| 2640 | if (err) |
| 2641 | goto fail; |
| 2642 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2643 | trans = btrfs_start_transaction(root, 1); |
| 2644 | btrfs_set_trans_block_group(trans, dir); |
| 2645 | |
| 2646 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2647 | if (err) { |
| 2648 | err = -ENOSPC; |
| 2649 | goto out_unlock; |
| 2650 | } |
| 2651 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2652 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2653 | dentry->d_name.len, |
| 2654 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2655 | BTRFS_I(dir)->block_group, mode, &index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2656 | err = PTR_ERR(inode); |
| 2657 | if (IS_ERR(inode)) |
| 2658 | goto out_unlock; |
| 2659 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 2660 | err = btrfs_init_acl(inode, dir); |
| 2661 | if (err) { |
| 2662 | drop_inode = 1; |
| 2663 | goto out_unlock; |
| 2664 | } |
| 2665 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2666 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2667 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2668 | if (err) |
| 2669 | drop_inode = 1; |
| 2670 | else { |
| 2671 | inode->i_op = &btrfs_special_inode_operations; |
| 2672 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 2673 | btrfs_update_inode(trans, root, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2674 | } |
| 2675 | dir->i_sb->s_dirt = 1; |
| 2676 | btrfs_update_inode_block_group(trans, inode); |
| 2677 | btrfs_update_inode_block_group(trans, dir); |
| 2678 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2679 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2680 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2681 | fail: |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2682 | if (drop_inode) { |
| 2683 | inode_dec_link_count(inode); |
| 2684 | iput(inode); |
| 2685 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2686 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2687 | return err; |
| 2688 | } |
| 2689 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2690 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 2691 | int mode, struct nameidata *nd) |
| 2692 | { |
| 2693 | struct btrfs_trans_handle *trans; |
| 2694 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2695 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2696 | int err; |
| 2697 | int drop_inode = 0; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2698 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2699 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2700 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2701 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2702 | err = btrfs_check_free_space(root, 1, 0); |
| 2703 | if (err) |
| 2704 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2705 | trans = btrfs_start_transaction(root, 1); |
| 2706 | btrfs_set_trans_block_group(trans, dir); |
| 2707 | |
| 2708 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2709 | if (err) { |
| 2710 | err = -ENOSPC; |
| 2711 | goto out_unlock; |
| 2712 | } |
| 2713 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2714 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2715 | dentry->d_name.len, |
| 2716 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2717 | objectid, BTRFS_I(dir)->block_group, mode, |
| 2718 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2719 | err = PTR_ERR(inode); |
| 2720 | if (IS_ERR(inode)) |
| 2721 | goto out_unlock; |
| 2722 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 2723 | err = btrfs_init_acl(inode, dir); |
| 2724 | if (err) { |
| 2725 | drop_inode = 1; |
| 2726 | goto out_unlock; |
| 2727 | } |
| 2728 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2729 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2730 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2731 | if (err) |
| 2732 | drop_inode = 1; |
| 2733 | else { |
| 2734 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2735 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2736 | inode->i_fop = &btrfs_file_operations; |
| 2737 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2738 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2739 | } |
| 2740 | dir->i_sb->s_dirt = 1; |
| 2741 | btrfs_update_inode_block_group(trans, inode); |
| 2742 | btrfs_update_inode_block_group(trans, dir); |
| 2743 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2744 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 2745 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2746 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2747 | if (drop_inode) { |
| 2748 | inode_dec_link_count(inode); |
| 2749 | iput(inode); |
| 2750 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2751 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2752 | return err; |
| 2753 | } |
| 2754 | |
| 2755 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 2756 | struct dentry *dentry) |
| 2757 | { |
| 2758 | struct btrfs_trans_handle *trans; |
| 2759 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2760 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2761 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2762 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2763 | int err; |
| 2764 | int drop_inode = 0; |
| 2765 | |
| 2766 | if (inode->i_nlink == 0) |
| 2767 | return -ENOENT; |
| 2768 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2769 | btrfs_inc_nlink(inode); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2770 | err = btrfs_check_free_space(root, 1, 0); |
| 2771 | if (err) |
| 2772 | goto fail; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2773 | err = btrfs_set_inode_index(dir, inode, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2774 | if (err) |
| 2775 | goto fail; |
| 2776 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2777 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2778 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2779 | btrfs_set_trans_block_group(trans, dir); |
| 2780 | atomic_inc(&inode->i_count); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2781 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2782 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2783 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2784 | if (err) |
| 2785 | drop_inode = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2786 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2787 | dir->i_sb->s_dirt = 1; |
| 2788 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2789 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2790 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2791 | if (err) |
| 2792 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2793 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2794 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 2795 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2796 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2797 | if (drop_inode) { |
| 2798 | inode_dec_link_count(inode); |
| 2799 | iput(inode); |
| 2800 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2801 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2802 | return err; |
| 2803 | } |
| 2804 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2805 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 2806 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 2807 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2808 | struct btrfs_trans_handle *trans; |
| 2809 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2810 | int err = 0; |
| 2811 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 2812 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2813 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2814 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2815 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2816 | err = btrfs_check_free_space(root, 1, 0); |
| 2817 | if (err) |
| 2818 | goto out_unlock; |
| 2819 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2820 | trans = btrfs_start_transaction(root, 1); |
| 2821 | btrfs_set_trans_block_group(trans, dir); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2822 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2823 | if (IS_ERR(trans)) { |
| 2824 | err = PTR_ERR(trans); |
| 2825 | goto out_unlock; |
| 2826 | } |
| 2827 | |
| 2828 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2829 | if (err) { |
| 2830 | err = -ENOSPC; |
| 2831 | goto out_unlock; |
| 2832 | } |
| 2833 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2834 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 2835 | dentry->d_name.len, |
| 2836 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 2837 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
| 2838 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2839 | if (IS_ERR(inode)) { |
| 2840 | err = PTR_ERR(inode); |
| 2841 | goto out_fail; |
| 2842 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2843 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2844 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 2845 | |
| 2846 | err = btrfs_init_acl(inode, dir); |
| 2847 | if (err) |
| 2848 | goto out_fail; |
| 2849 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2850 | inode->i_op = &btrfs_dir_inode_operations; |
| 2851 | inode->i_fop = &btrfs_dir_file_operations; |
| 2852 | btrfs_set_trans_block_group(trans, inode); |
| 2853 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2854 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2855 | err = btrfs_update_inode(trans, root, inode); |
| 2856 | if (err) |
| 2857 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2858 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2859 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
| 2860 | inode, dentry->d_name.name, |
| 2861 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2862 | if (err) |
| 2863 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2864 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2865 | d_instantiate(dentry, inode); |
| 2866 | drop_on_err = 0; |
| 2867 | dir->i_sb->s_dirt = 1; |
| 2868 | btrfs_update_inode_block_group(trans, inode); |
| 2869 | btrfs_update_inode_block_group(trans, dir); |
| 2870 | |
| 2871 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2872 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 2873 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2874 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2875 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2876 | if (drop_on_err) |
| 2877 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 2878 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2879 | return err; |
| 2880 | } |
| 2881 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2882 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 2883 | * and an extent that you want to insert, deal with overlap and insert |
| 2884 | * the new extent into the tree. |
| 2885 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2886 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 2887 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2888 | struct extent_map *em, |
| 2889 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2890 | { |
| 2891 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2892 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2893 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 2894 | start_diff = map_start - em->start; |
| 2895 | em->start = map_start; |
| 2896 | em->len = map_len; |
| 2897 | if (em->block_start < EXTENT_MAP_LAST_BYTE) |
| 2898 | em->block_start += start_diff; |
| 2899 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2900 | } |
| 2901 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2902 | /* |
| 2903 | * a bit scary, this does extent mapping from logical file offset to the disk. |
| 2904 | * the ugly parts come from merging extents from the disk with the |
| 2905 | * in-ram representation. This gets more complex because of the data=ordered code, |
| 2906 | * where the in-ram extents might be locked pending data=ordered completion. |
| 2907 | * |
| 2908 | * This also copies inline extents directly into the page. |
| 2909 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2910 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2911 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2912 | int create) |
| 2913 | { |
| 2914 | int ret; |
| 2915 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2916 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2917 | u64 extent_start = 0; |
| 2918 | u64 extent_end = 0; |
| 2919 | u64 objectid = inode->i_ino; |
| 2920 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2921 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2922 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2923 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2924 | struct extent_buffer *leaf; |
| 2925 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2926 | struct extent_map *em = NULL; |
| 2927 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2928 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2929 | struct btrfs_trans_handle *trans = NULL; |
| 2930 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2931 | again: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2932 | spin_lock(&em_tree->lock); |
| 2933 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2934 | if (em) |
| 2935 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2936 | spin_unlock(&em_tree->lock); |
| 2937 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2938 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 2939 | if (em->start > start || em->start + em->len <= start) |
| 2940 | free_extent_map(em); |
| 2941 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2942 | free_extent_map(em); |
| 2943 | else |
| 2944 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2945 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2946 | em = alloc_extent_map(GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2947 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2948 | err = -ENOMEM; |
| 2949 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2950 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2951 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2952 | em->start = EXTENT_MAP_HOLE; |
| 2953 | em->len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 2954 | |
| 2955 | if (!path) { |
| 2956 | path = btrfs_alloc_path(); |
| 2957 | BUG_ON(!path); |
| 2958 | } |
| 2959 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2960 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 2961 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2962 | if (ret < 0) { |
| 2963 | err = ret; |
| 2964 | goto out; |
| 2965 | } |
| 2966 | |
| 2967 | if (ret != 0) { |
| 2968 | if (path->slots[0] == 0) |
| 2969 | goto not_found; |
| 2970 | path->slots[0]--; |
| 2971 | } |
| 2972 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2973 | leaf = path->nodes[0]; |
| 2974 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2975 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2976 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2977 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2978 | found_type = btrfs_key_type(&found_key); |
| 2979 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2980 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 2981 | goto not_found; |
| 2982 | } |
| 2983 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2984 | found_type = btrfs_file_extent_type(leaf, item); |
| 2985 | extent_start = found_key.offset; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2986 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 2987 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2988 | btrfs_file_extent_num_bytes(leaf, item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2989 | err = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2990 | if (start < extent_start || start >= extent_end) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2991 | em->start = start; |
| 2992 | if (start < extent_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2993 | if (start + len <= extent_start) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2994 | goto not_found; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2995 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2996 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2997 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2998 | } |
| 2999 | goto not_found_em; |
| 3000 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3001 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 3002 | if (bytenr == 0) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3003 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3004 | em->len = extent_end - extent_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3005 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3006 | goto insert; |
| 3007 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3008 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 3009 | em->block_start = bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3010 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3011 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3012 | goto insert; |
| 3013 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3014 | u64 page_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3015 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3016 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 3017 | size_t size; |
| 3018 | size_t extent_offset; |
| 3019 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3020 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3021 | size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf, |
| 3022 | path->slots[0])); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3023 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 3024 | ~((u64)root->sectorsize - 1); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3025 | if (start < extent_start || start >= extent_end) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3026 | em->start = start; |
| 3027 | if (start < extent_start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3028 | if (start + len <= extent_start) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3029 | goto not_found; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3030 | em->len = extent_end - extent_start; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3031 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3032 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3033 | } |
| 3034 | goto not_found_em; |
| 3035 | } |
| 3036 | em->block_start = EXTENT_MAP_INLINE; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 3037 | |
| 3038 | if (!page) { |
| 3039 | em->start = extent_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3040 | em->len = size; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 3041 | goto out; |
| 3042 | } |
| 3043 | |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3044 | page_start = page_offset(page) + pg_offset; |
| 3045 | extent_offset = page_start - extent_start; |
| 3046 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 3047 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 3048 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3049 | em->len = (copy_size + root->sectorsize - 1) & |
| 3050 | ~((u64)root->sectorsize - 1); |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 3051 | map = kmap(page); |
| 3052 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3053 | if (create == 0 && !PageUptodate(page)) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3054 | read_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3055 | copy_size); |
| 3056 | flush_dcache_page(page); |
| 3057 | } else if (create && PageUptodate(page)) { |
| 3058 | if (!trans) { |
| 3059 | kunmap(page); |
| 3060 | free_extent_map(em); |
| 3061 | em = NULL; |
| 3062 | btrfs_release_path(root, path); |
Chris Mason | f929574 | 2008-07-17 12:54:14 -0400 | [diff] [blame] | 3063 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3064 | goto again; |
| 3065 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3066 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3067 | copy_size); |
| 3068 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3069 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3070 | kunmap(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3071 | set_extent_uptodate(io_tree, em->start, |
| 3072 | extent_map_end(em) - 1, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3073 | goto insert; |
| 3074 | } else { |
| 3075 | printk("unkknown found_type %d\n", found_type); |
| 3076 | WARN_ON(1); |
| 3077 | } |
| 3078 | not_found: |
| 3079 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3080 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3081 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3082 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3083 | insert: |
| 3084 | btrfs_release_path(root, path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3085 | if (em->start > start || extent_map_end(em) <= start) { |
| 3086 | 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] | 3087 | err = -EIO; |
| 3088 | goto out; |
| 3089 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3090 | |
| 3091 | err = 0; |
| 3092 | spin_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3093 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3094 | /* it is possible that someone inserted the extent into the tree |
| 3095 | * while we had the lock dropped. It is also possible that |
| 3096 | * an overlapping map exists in the tree |
| 3097 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3098 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3099 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3100 | |
| 3101 | ret = 0; |
| 3102 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3103 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 3104 | if (existing && (existing->start > start || |
| 3105 | existing->start + existing->len <= start)) { |
| 3106 | free_extent_map(existing); |
| 3107 | existing = NULL; |
| 3108 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3109 | if (!existing) { |
| 3110 | existing = lookup_extent_mapping(em_tree, em->start, |
| 3111 | em->len); |
| 3112 | if (existing) { |
| 3113 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3114 | em, start, |
| 3115 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3116 | free_extent_map(existing); |
| 3117 | if (err) { |
| 3118 | free_extent_map(em); |
| 3119 | em = NULL; |
| 3120 | } |
| 3121 | } else { |
| 3122 | err = -EIO; |
| 3123 | printk("failing to insert %Lu %Lu\n", |
| 3124 | start, len); |
| 3125 | free_extent_map(em); |
| 3126 | em = NULL; |
| 3127 | } |
| 3128 | } else { |
| 3129 | free_extent_map(em); |
| 3130 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3131 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3132 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3133 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3134 | spin_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3135 | out: |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 3136 | if (path) |
| 3137 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3138 | if (trans) { |
| 3139 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3140 | if (!err) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3141 | err = ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3142 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3143 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3144 | if (err) { |
| 3145 | free_extent_map(em); |
| 3146 | WARN_ON(1); |
| 3147 | return ERR_PTR(err); |
| 3148 | } |
| 3149 | return em; |
| 3150 | } |
| 3151 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 3152 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 3153 | const struct iovec *iov, loff_t offset, |
| 3154 | unsigned long nr_segs) |
| 3155 | { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 3156 | return -EINVAL; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 3157 | } |
| 3158 | |
Christoph Hellwig | d396c6f | 2007-09-10 20:02:30 -0400 | [diff] [blame] | 3159 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3160 | { |
Christoph Hellwig | d396c6f | 2007-09-10 20:02:30 -0400 | [diff] [blame] | 3161 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3162 | } |
| 3163 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3164 | int btrfs_readpage(struct file *file, struct page *page) |
| 3165 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3166 | struct extent_io_tree *tree; |
| 3167 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3168 | return extent_read_full_page(tree, page, btrfs_get_extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3169 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3170 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3171 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 3172 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3173 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3174 | |
| 3175 | |
| 3176 | if (current->flags & PF_MEMALLOC) { |
| 3177 | redirty_page_for_writepage(wbc, page); |
| 3178 | unlock_page(page); |
| 3179 | return 0; |
| 3180 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3181 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3182 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 3183 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3184 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 3185 | int btrfs_writepages(struct address_space *mapping, |
| 3186 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 3187 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3188 | struct extent_io_tree *tree; |
| 3189 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 3190 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 3191 | } |
| 3192 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 3193 | static int |
| 3194 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 3195 | struct list_head *pages, unsigned nr_pages) |
| 3196 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3197 | struct extent_io_tree *tree; |
| 3198 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 3199 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 3200 | btrfs_get_extent); |
| 3201 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3202 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3203 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3204 | struct extent_io_tree *tree; |
| 3205 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3206 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3207 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3208 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 3209 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3210 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3211 | if (ret == 1) { |
| 3212 | ClearPagePrivate(page); |
| 3213 | set_page_private(page, 0); |
| 3214 | page_cache_release(page); |
| 3215 | } |
| 3216 | return ret; |
| 3217 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3218 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3219 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 3220 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 3221 | if (PageWriteback(page) || PageDirty(page)) |
| 3222 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3223 | return __btrfs_releasepage(page, gfp_flags); |
| 3224 | } |
| 3225 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3226 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 3227 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3228 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3229 | struct btrfs_ordered_extent *ordered; |
| 3230 | u64 page_start = page_offset(page); |
| 3231 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3232 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3233 | wait_on_page_writeback(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3234 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3235 | if (offset) { |
| 3236 | btrfs_releasepage(page, GFP_NOFS); |
| 3237 | return; |
| 3238 | } |
| 3239 | |
| 3240 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 3241 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 3242 | page_offset(page)); |
| 3243 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3244 | /* |
| 3245 | * IO on this page will never be started, so we need |
| 3246 | * to account for any ordered extents now |
| 3247 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3248 | clear_extent_bit(tree, page_start, page_end, |
| 3249 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 3250 | EXTENT_LOCKED, 1, 0, GFP_NOFS); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 3251 | btrfs_finish_ordered_io(page->mapping->host, |
| 3252 | page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3253 | btrfs_put_ordered_extent(ordered); |
| 3254 | lock_extent(tree, page_start, page_end, GFP_NOFS); |
| 3255 | } |
| 3256 | clear_extent_bit(tree, page_start, page_end, |
| 3257 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 3258 | EXTENT_ORDERED, |
| 3259 | 1, 1, GFP_NOFS); |
| 3260 | __btrfs_releasepage(page, GFP_NOFS); |
| 3261 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3262 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 3263 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 3264 | ClearPagePrivate(page); |
| 3265 | set_page_private(page, 0); |
| 3266 | page_cache_release(page); |
| 3267 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3268 | } |
| 3269 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3270 | /* |
| 3271 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 3272 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 3273 | * be careful to check for EOF conditions here. We set the page up correctly |
| 3274 | * for a written page which means we get ENOSPC checking when writing into |
| 3275 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 3276 | * support these features. |
| 3277 | * |
| 3278 | * We are not allowed to take the i_mutex here so we have to play games to |
| 3279 | * protect against truncate races as the page could now be beyond EOF. Because |
| 3280 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 3281 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 3282 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 3283 | * unlock the page. |
| 3284 | */ |
| 3285 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 3286 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 3287 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3288 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3289 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3290 | struct btrfs_ordered_extent *ordered; |
| 3291 | char *kaddr; |
| 3292 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3293 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3294 | int ret; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3295 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3296 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3297 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3298 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3299 | if (ret) |
| 3300 | goto out; |
| 3301 | |
| 3302 | ret = -EINVAL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3303 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3304 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3305 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3306 | page_start = page_offset(page); |
| 3307 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3308 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3309 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3310 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3311 | /* page got truncated out from underneath us */ |
| 3312 | goto out_unlock; |
| 3313 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3314 | wait_on_page_writeback(page); |
| 3315 | |
| 3316 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3317 | set_page_extent_mapped(page); |
| 3318 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3319 | /* |
| 3320 | * we can't set the delalloc bits if there are pending ordered |
| 3321 | * extents. Drop our locks and wait for them to finish |
| 3322 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3323 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3324 | if (ordered) { |
| 3325 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3326 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3327 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3328 | btrfs_put_ordered_extent(ordered); |
| 3329 | goto again; |
| 3330 | } |
| 3331 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3332 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3333 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3334 | |
| 3335 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3336 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3337 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3338 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3339 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3340 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3341 | if (zero_start != PAGE_CACHE_SIZE) { |
| 3342 | kaddr = kmap(page); |
| 3343 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 3344 | flush_dcache_page(page); |
| 3345 | kunmap(page); |
| 3346 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3347 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3348 | set_page_dirty(page); |
| 3349 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3350 | |
| 3351 | out_unlock: |
| 3352 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3353 | out: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3354 | return ret; |
| 3355 | } |
| 3356 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3357 | static void btrfs_truncate(struct inode *inode) |
| 3358 | { |
| 3359 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3360 | int ret; |
| 3361 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3362 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3363 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3364 | |
| 3365 | if (!S_ISREG(inode->i_mode)) |
| 3366 | return; |
| 3367 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 3368 | return; |
| 3369 | |
| 3370 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3371 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3372 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3373 | trans = btrfs_start_transaction(root, 1); |
| 3374 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3375 | btrfs_i_size_write(inode, inode->i_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3376 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3377 | ret = btrfs_orphan_add(trans, inode); |
| 3378 | if (ret) |
| 3379 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3380 | /* FIXME, add redo link to tree so we don't leak on crash */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3381 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3382 | BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3383 | btrfs_update_inode(trans, root, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3384 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3385 | ret = btrfs_orphan_del(trans, inode); |
| 3386 | BUG_ON(ret); |
| 3387 | |
| 3388 | out: |
| 3389 | nr = trans->blocks_used; |
Chris Mason | 89ce8a6 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3390 | ret = btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3391 | BUG_ON(ret); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3392 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3393 | } |
| 3394 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 3395 | /* |
| 3396 | * Invalidate a single dcache entry at the root of the filesystem. |
| 3397 | * Needed after creation of snapshot or subvolume. |
| 3398 | */ |
| 3399 | void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name, |
| 3400 | int namelen) |
| 3401 | { |
| 3402 | struct dentry *alias, *entry; |
| 3403 | struct qstr qstr; |
| 3404 | |
| 3405 | alias = d_find_alias(root->fs_info->sb->s_root->d_inode); |
| 3406 | if (alias) { |
| 3407 | qstr.name = name; |
| 3408 | qstr.len = namelen; |
| 3409 | /* change me if btrfs ever gets a d_hash operation */ |
| 3410 | qstr.hash = full_name_hash(qstr.name, qstr.len); |
| 3411 | entry = d_lookup(alias, &qstr); |
| 3412 | dput(alias); |
| 3413 | if (entry) { |
| 3414 | d_invalidate(entry); |
| 3415 | dput(entry); |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3420 | /* |
| 3421 | * create a new subvolume directory/inode (helper for the ioctl). |
| 3422 | */ |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 3423 | int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3424 | struct btrfs_trans_handle *trans, u64 new_dirid, |
| 3425 | struct btrfs_block_group_cache *block_group) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3426 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3427 | struct inode *inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 3428 | int error; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3429 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3430 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3431 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3432 | new_dirid, block_group, S_IFDIR | 0700, &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3433 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3434 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3435 | inode->i_op = &btrfs_dir_inode_operations; |
| 3436 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 3437 | new_root->inode = inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3438 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3439 | inode->i_nlink = 1; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3440 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 3441 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 3442 | error = btrfs_update_inode(trans, new_root, inode); |
| 3443 | if (error) |
| 3444 | return error; |
| 3445 | |
| 3446 | d_instantiate(dentry, inode); |
| 3447 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3448 | } |
| 3449 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3450 | /* helper function for file defrag and space balancing. This |
| 3451 | * forces readahead on a given range of bytes in an inode |
| 3452 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3453 | unsigned long btrfs_force_ra(struct address_space *mapping, |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3454 | struct file_ra_state *ra, struct file *file, |
| 3455 | pgoff_t offset, pgoff_t last_index) |
| 3456 | { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3457 | pgoff_t req_size = last_index - offset + 1; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3458 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3459 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
| 3460 | return offset + req_size; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3461 | } |
| 3462 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3463 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 3464 | { |
| 3465 | struct btrfs_inode *ei; |
| 3466 | |
| 3467 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 3468 | if (!ei) |
| 3469 | return NULL; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3470 | ei->last_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3471 | ei->logged_trans = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3472 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3473 | ei->i_acl = BTRFS_ACL_NOT_CACHED; |
| 3474 | ei->i_default_acl = BTRFS_ACL_NOT_CACHED; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3475 | INIT_LIST_HEAD(&ei->i_orphan); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3476 | return &ei->vfs_inode; |
| 3477 | } |
| 3478 | |
| 3479 | void btrfs_destroy_inode(struct inode *inode) |
| 3480 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3481 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3482 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 3483 | WARN_ON(inode->i_data.nrpages); |
| 3484 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3485 | if (BTRFS_I(inode)->i_acl && |
| 3486 | BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED) |
| 3487 | posix_acl_release(BTRFS_I(inode)->i_acl); |
| 3488 | if (BTRFS_I(inode)->i_default_acl && |
| 3489 | BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED) |
| 3490 | posix_acl_release(BTRFS_I(inode)->i_default_acl); |
| 3491 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 3492 | spin_lock(&BTRFS_I(inode)->root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3493 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 3494 | printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan" |
| 3495 | " list\n", inode->i_ino); |
| 3496 | dump_stack(); |
| 3497 | } |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 3498 | spin_unlock(&BTRFS_I(inode)->root->list_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3499 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3500 | while(1) { |
| 3501 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 3502 | if (!ordered) |
| 3503 | break; |
| 3504 | else { |
| 3505 | printk("found ordered extent %Lu %Lu\n", |
| 3506 | ordered->file_offset, ordered->len); |
| 3507 | btrfs_remove_ordered_extent(inode, ordered); |
| 3508 | btrfs_put_ordered_extent(ordered); |
| 3509 | btrfs_put_ordered_extent(ordered); |
| 3510 | } |
| 3511 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3512 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3513 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 3514 | } |
| 3515 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 3516 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3517 | { |
| 3518 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 3519 | |
| 3520 | inode_init_once(&ei->vfs_inode); |
| 3521 | } |
| 3522 | |
| 3523 | void btrfs_destroy_cachep(void) |
| 3524 | { |
| 3525 | if (btrfs_inode_cachep) |
| 3526 | kmem_cache_destroy(btrfs_inode_cachep); |
| 3527 | if (btrfs_trans_handle_cachep) |
| 3528 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 3529 | if (btrfs_transaction_cachep) |
| 3530 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 3531 | if (btrfs_bit_radix_cachep) |
| 3532 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 3533 | if (btrfs_path_cachep) |
| 3534 | kmem_cache_destroy(btrfs_path_cachep); |
| 3535 | } |
| 3536 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3537 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3538 | unsigned long extra_flags, |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 3539 | void (*ctor)(void *)) |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3540 | { |
| 3541 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 3542 | SLAB_MEM_SPREAD | extra_flags), ctor); |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3543 | } |
| 3544 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3545 | int btrfs_init_cachep(void) |
| 3546 | { |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3547 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3548 | sizeof(struct btrfs_inode), |
| 3549 | 0, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3550 | if (!btrfs_inode_cachep) |
| 3551 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3552 | btrfs_trans_handle_cachep = |
| 3553 | btrfs_cache_create("btrfs_trans_handle_cache", |
| 3554 | sizeof(struct btrfs_trans_handle), |
| 3555 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3556 | if (!btrfs_trans_handle_cachep) |
| 3557 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3558 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3559 | sizeof(struct btrfs_transaction), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3560 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3561 | if (!btrfs_transaction_cachep) |
| 3562 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3563 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
Yan | 2322358 | 2007-09-17 11:08:52 -0400 | [diff] [blame] | 3564 | sizeof(struct btrfs_path), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3565 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3566 | if (!btrfs_path_cachep) |
| 3567 | goto fail; |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 3568 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 3569 | SLAB_DESTROY_BY_RCU, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3570 | if (!btrfs_bit_radix_cachep) |
| 3571 | goto fail; |
| 3572 | return 0; |
| 3573 | fail: |
| 3574 | btrfs_destroy_cachep(); |
| 3575 | return -ENOMEM; |
| 3576 | } |
| 3577 | |
| 3578 | static int btrfs_getattr(struct vfsmount *mnt, |
| 3579 | struct dentry *dentry, struct kstat *stat) |
| 3580 | { |
| 3581 | struct inode *inode = dentry->d_inode; |
| 3582 | generic_fillattr(inode, stat); |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 3583 | stat->blksize = PAGE_CACHE_SIZE; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3584 | stat->blocks = (inode_get_bytes(inode) + |
| 3585 | BTRFS_I(inode)->delalloc_bytes) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3586 | return 0; |
| 3587 | } |
| 3588 | |
| 3589 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, |
| 3590 | struct inode * new_dir,struct dentry *new_dentry) |
| 3591 | { |
| 3592 | struct btrfs_trans_handle *trans; |
| 3593 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 3594 | struct inode *new_inode = new_dentry->d_inode; |
| 3595 | struct inode *old_inode = old_dentry->d_inode; |
| 3596 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3597 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3598 | int ret; |
| 3599 | |
| 3600 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
| 3601 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
| 3602 | return -ENOTEMPTY; |
| 3603 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3604 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3605 | ret = btrfs_check_free_space(root, 1, 0); |
| 3606 | if (ret) |
| 3607 | goto out_unlock; |
| 3608 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3609 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3610 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3611 | btrfs_set_trans_block_group(trans, new_dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3612 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3613 | btrfs_inc_nlink(old_dentry->d_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3614 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 3615 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 3616 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3617 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3618 | ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, |
| 3619 | old_dentry->d_name.name, |
| 3620 | old_dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3621 | if (ret) |
| 3622 | goto out_fail; |
| 3623 | |
| 3624 | if (new_inode) { |
| 3625 | new_inode->i_ctime = CURRENT_TIME; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3626 | ret = btrfs_unlink_inode(trans, root, new_dir, |
| 3627 | new_dentry->d_inode, |
| 3628 | new_dentry->d_name.name, |
| 3629 | new_dentry->d_name.len); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3630 | if (ret) |
| 3631 | goto out_fail; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3632 | if (new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3633 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3634 | if (ret) |
| 3635 | goto out_fail; |
| 3636 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3637 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3638 | } |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3639 | ret = btrfs_set_inode_index(new_dir, old_inode, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3640 | if (ret) |
| 3641 | goto out_fail; |
| 3642 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3643 | ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, |
| 3644 | old_inode, new_dentry->d_name.name, |
| 3645 | new_dentry->d_name.len, 1, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3646 | if (ret) |
| 3647 | goto out_fail; |
| 3648 | |
| 3649 | out_fail: |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 3650 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3651 | out_unlock: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3652 | return ret; |
| 3653 | } |
| 3654 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3655 | /* |
| 3656 | * some fairly slow code that needs optimization. This walks the list |
| 3657 | * of all the inodes with pending delalloc and forces them to disk. |
| 3658 | */ |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3659 | int btrfs_start_delalloc_inodes(struct btrfs_root *root) |
| 3660 | { |
| 3661 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 3662 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3663 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3664 | unsigned long flags; |
| 3665 | |
| 3666 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
| 3667 | while(!list_empty(head)) { |
| 3668 | binode = list_entry(head->next, struct btrfs_inode, |
| 3669 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3670 | inode = igrab(&binode->vfs_inode); |
| 3671 | if (!inode) |
| 3672 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3673 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3674 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 3675 | filemap_flush(inode->i_mapping); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3676 | iput(inode); |
| 3677 | } |
| 3678 | cond_resched(); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3679 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
| 3680 | } |
| 3681 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 3682 | |
| 3683 | /* the filemap_flush will queue IO into the worker threads, but |
| 3684 | * we have to make sure the IO is actually started and that |
| 3685 | * ordered extents get created before we return |
| 3686 | */ |
| 3687 | atomic_inc(&root->fs_info->async_submit_draining); |
| 3688 | while(atomic_read(&root->fs_info->nr_async_submits)) { |
| 3689 | wait_event(root->fs_info->async_submit_wait, |
| 3690 | (atomic_read(&root->fs_info->nr_async_submits) == 0)); |
| 3691 | } |
| 3692 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3693 | return 0; |
| 3694 | } |
| 3695 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3696 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 3697 | const char *symname) |
| 3698 | { |
| 3699 | struct btrfs_trans_handle *trans; |
| 3700 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 3701 | struct btrfs_path *path; |
| 3702 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3703 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3704 | int err; |
| 3705 | int drop_inode = 0; |
| 3706 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3707 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3708 | int name_len; |
| 3709 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3710 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3711 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3712 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3713 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3714 | |
| 3715 | name_len = strlen(symname) + 1; |
| 3716 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 3717 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3718 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3719 | err = btrfs_check_free_space(root, 1, 0); |
| 3720 | if (err) |
| 3721 | goto out_fail; |
| 3722 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3723 | trans = btrfs_start_transaction(root, 1); |
| 3724 | btrfs_set_trans_block_group(trans, dir); |
| 3725 | |
| 3726 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 3727 | if (err) { |
| 3728 | err = -ENOSPC; |
| 3729 | goto out_unlock; |
| 3730 | } |
| 3731 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3732 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3733 | dentry->d_name.len, |
| 3734 | dentry->d_parent->d_inode->i_ino, objectid, |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3735 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
| 3736 | &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3737 | err = PTR_ERR(inode); |
| 3738 | if (IS_ERR(inode)) |
| 3739 | goto out_unlock; |
| 3740 | |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3741 | err = btrfs_init_acl(inode, dir); |
| 3742 | if (err) { |
| 3743 | drop_inode = 1; |
| 3744 | goto out_unlock; |
| 3745 | } |
| 3746 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3747 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 3748 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3749 | if (err) |
| 3750 | drop_inode = 1; |
| 3751 | else { |
| 3752 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3753 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3754 | inode->i_fop = &btrfs_file_operations; |
| 3755 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3756 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3757 | } |
| 3758 | dir->i_sb->s_dirt = 1; |
| 3759 | btrfs_update_inode_block_group(trans, inode); |
| 3760 | btrfs_update_inode_block_group(trans, dir); |
| 3761 | if (drop_inode) |
| 3762 | goto out_unlock; |
| 3763 | |
| 3764 | path = btrfs_alloc_path(); |
| 3765 | BUG_ON(!path); |
| 3766 | key.objectid = inode->i_ino; |
| 3767 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3768 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 3769 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 3770 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 3771 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3772 | if (err) { |
| 3773 | drop_inode = 1; |
| 3774 | goto out_unlock; |
| 3775 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3776 | leaf = path->nodes[0]; |
| 3777 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 3778 | struct btrfs_file_extent_item); |
| 3779 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 3780 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3781 | BTRFS_FILE_EXTENT_INLINE); |
| 3782 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3783 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 3784 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3785 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3786 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3787 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3788 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3789 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3790 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3791 | err = btrfs_update_inode(trans, root, inode); |
| 3792 | if (err) |
| 3793 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3794 | |
| 3795 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3796 | nr = trans->blocks_used; |
Chris Mason | ab78c84 | 2008-07-29 16:15:18 -0400 | [diff] [blame] | 3797 | btrfs_end_transaction_throttle(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3798 | out_fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3799 | if (drop_inode) { |
| 3800 | inode_dec_link_count(inode); |
| 3801 | iput(inode); |
| 3802 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3803 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3804 | return err; |
| 3805 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 3806 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3807 | static int btrfs_set_page_dirty(struct page *page) |
| 3808 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3809 | return __set_page_dirty_nobuffers(page); |
| 3810 | } |
| 3811 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 3812 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3813 | { |
| 3814 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) |
| 3815 | return -EACCES; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3816 | return generic_permission(inode, mask, btrfs_check_acl); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3817 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3818 | |
| 3819 | static struct inode_operations btrfs_dir_inode_operations = { |
| 3820 | .lookup = btrfs_lookup, |
| 3821 | .create = btrfs_create, |
| 3822 | .unlink = btrfs_unlink, |
| 3823 | .link = btrfs_link, |
| 3824 | .mkdir = btrfs_mkdir, |
| 3825 | .rmdir = btrfs_rmdir, |
| 3826 | .rename = btrfs_rename, |
| 3827 | .symlink = btrfs_symlink, |
| 3828 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3829 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 3830 | .setxattr = btrfs_setxattr, |
| 3831 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3832 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 3833 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3834 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3835 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3836 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 3837 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3838 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3839 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3840 | static struct file_operations btrfs_dir_file_operations = { |
| 3841 | .llseek = generic_file_llseek, |
| 3842 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 3843 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3844 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3845 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3846 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3847 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 3848 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3849 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3850 | }; |
| 3851 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3852 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3853 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 3854 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3855 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3856 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3857 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3858 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3859 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 3860 | .set_bit_hook = btrfs_set_bit_hook, |
| 3861 | .clear_bit_hook = btrfs_clear_bit_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 3862 | }; |
| 3863 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | static struct address_space_operations btrfs_aops = { |
| 3865 | .readpage = btrfs_readpage, |
| 3866 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 3867 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 3868 | .readpages = btrfs_readpages, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3869 | .sync_page = block_sync_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3870 | .bmap = btrfs_bmap, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 3871 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3872 | .invalidatepage = btrfs_invalidatepage, |
| 3873 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3874 | .set_page_dirty = btrfs_set_page_dirty, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3875 | }; |
| 3876 | |
| 3877 | static struct address_space_operations btrfs_symlink_aops = { |
| 3878 | .readpage = btrfs_readpage, |
| 3879 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 3880 | .invalidatepage = btrfs_invalidatepage, |
| 3881 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3882 | }; |
| 3883 | |
| 3884 | static struct inode_operations btrfs_file_inode_operations = { |
| 3885 | .truncate = btrfs_truncate, |
| 3886 | .getattr = btrfs_getattr, |
| 3887 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 3888 | .setxattr = btrfs_setxattr, |
| 3889 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 3890 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 3891 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3892 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3893 | }; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3894 | static struct inode_operations btrfs_special_inode_operations = { |
| 3895 | .getattr = btrfs_getattr, |
| 3896 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3897 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 3898 | .setxattr = btrfs_setxattr, |
| 3899 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3900 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 3901 | .removexattr = btrfs_removexattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3902 | }; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3903 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 3904 | .readlink = generic_readlink, |
| 3905 | .follow_link = page_follow_link_light, |
| 3906 | .put_link = page_put_link, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 3907 | .permission = btrfs_permission, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3908 | }; |