Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2 | #include <linux/buffer_head.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 3 | #include <linux/fs.h> |
| 4 | #include <linux/pagemap.h> |
| 5 | #include <linux/highmem.h> |
| 6 | #include <linux/time.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/string.h> |
| 9 | #include <linux/smp_lock.h> |
| 10 | #include <linux/backing-dev.h> |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 11 | #include <linux/mpage.h> |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 12 | #include <linux/swap.h> |
| 13 | #include <linux/writeback.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 14 | #include "ctree.h" |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 15 | #include "disk-io.h" |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 16 | #include "transaction.h" |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 17 | |
| 18 | #define BTRFS_SUPER_MAGIC 0x9123682E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 19 | |
| 20 | static struct inode_operations btrfs_dir_inode_operations; |
| 21 | static struct super_operations btrfs_super_ops; |
| 22 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 23 | static struct inode_operations btrfs_file_inode_operations; |
| 24 | static struct address_space_operations btrfs_aops; |
| 25 | static struct file_operations btrfs_file_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 26 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 27 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 28 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 29 | struct btrfs_path path; |
| 30 | struct btrfs_inode_item *inode_item; |
| 31 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 32 | int ret; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 33 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 34 | btrfs_init_path(&path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 35 | mutex_lock(&root->fs_info->fs_mutex); |
| 36 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 37 | ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0); |
| 38 | if (ret) { |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 39 | btrfs_release_path(root, &path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 40 | mutex_unlock(&root->fs_info->fs_mutex); |
| 41 | make_bad_inode(inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 42 | return; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 43 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 44 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 45 | path.slots[0], |
| 46 | struct btrfs_inode_item); |
| 47 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 48 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 49 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 50 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 51 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 52 | inode->i_size = btrfs_inode_size(inode_item); |
| 53 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 54 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 55 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 56 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 57 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 58 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 59 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 60 | inode->i_generation = btrfs_inode_generation(inode_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 61 | btrfs_release_path(root, &path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 62 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 63 | switch (inode->i_mode & S_IFMT) { |
| 64 | #if 0 |
| 65 | default: |
| 66 | init_special_inode(inode, inode->i_mode, |
| 67 | btrfs_inode_rdev(inode_item)); |
| 68 | break; |
| 69 | #endif |
| 70 | case S_IFREG: |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 71 | inode->i_mapping->a_ops = &btrfs_aops; |
| 72 | inode->i_fop = &btrfs_file_operations; |
| 73 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 74 | break; |
| 75 | case S_IFDIR: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 76 | inode->i_op = &btrfs_dir_inode_operations; |
| 77 | inode->i_fop = &btrfs_dir_file_operations; |
| 78 | break; |
| 79 | case S_IFLNK: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 80 | // inode->i_op = &page_symlink_inode_operations; |
| 81 | break; |
| 82 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 83 | return; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 86 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 87 | struct btrfs_root *root, |
| 88 | struct inode *dir, |
| 89 | struct dentry *dentry) |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 90 | { |
| 91 | struct btrfs_path path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 92 | const char *name = dentry->d_name.name; |
| 93 | int name_len = dentry->d_name.len; |
| 94 | int ret; |
| 95 | u64 objectid; |
| 96 | struct btrfs_dir_item *di; |
| 97 | |
| 98 | btrfs_init_path(&path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 99 | ret = btrfs_lookup_dir_item(trans, root, &path, dir->i_ino, |
| 100 | name, name_len, -1); |
| 101 | if (ret < 0) |
| 102 | goto err; |
| 103 | if (ret > 0) { |
| 104 | ret = -ENOENT; |
| 105 | goto err; |
| 106 | } |
| 107 | di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 108 | struct btrfs_dir_item); |
| 109 | objectid = btrfs_dir_objectid(di); |
| 110 | |
| 111 | ret = btrfs_del_item(trans, root, &path); |
| 112 | BUG_ON(ret); |
| 113 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 114 | err: |
| 115 | btrfs_release_path(root, &path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 116 | if (ret == 0) |
| 117 | inode_dec_link_count(dentry->d_inode); |
| 118 | return ret; |
| 119 | } |
| 120 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 121 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 122 | { |
| 123 | struct btrfs_root *root; |
| 124 | struct btrfs_trans_handle *trans; |
| 125 | int ret; |
| 126 | |
| 127 | root = btrfs_sb(dir->i_sb); |
| 128 | mutex_lock(&root->fs_info->fs_mutex); |
| 129 | trans = btrfs_start_transaction(root, 1); |
| 130 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 131 | btrfs_end_transaction(trans, root); |
| 132 | mutex_unlock(&root->fs_info->fs_mutex); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 137 | { |
| 138 | struct inode *inode = dentry->d_inode; |
| 139 | int err; |
| 140 | int ret; |
| 141 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 142 | struct btrfs_path path; |
| 143 | struct btrfs_key key; |
| 144 | struct btrfs_trans_handle *trans; |
| 145 | struct btrfs_disk_key *found_key; |
| 146 | struct btrfs_leaf *leaf; |
| 147 | |
| 148 | btrfs_init_path(&path); |
| 149 | mutex_lock(&root->fs_info->fs_mutex); |
| 150 | trans = btrfs_start_transaction(root, 1); |
| 151 | key.objectid = inode->i_ino; |
| 152 | key.offset = (u64)-1; |
| 153 | key.flags = 0; |
| 154 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
| 155 | ret = btrfs_search_slot(trans, root, &key, &path, -1, 1); |
| 156 | if (ret < 0) { |
| 157 | err = ret; |
| 158 | goto out; |
| 159 | } |
| 160 | |
| 161 | BUG_ON(ret == 0); |
| 162 | BUG_ON(path.slots[0] == 0); |
| 163 | path.slots[0]--; |
| 164 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 165 | found_key = &leaf->items[path.slots[0]].key; |
| 166 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) { |
| 167 | err = -ENOENT; |
| 168 | goto out; |
| 169 | } |
| 170 | if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || |
| 171 | btrfs_disk_key_offset(found_key) != 2) { |
| 172 | err = -ENOTEMPTY; |
| 173 | goto out; |
| 174 | } |
| 175 | ret = btrfs_del_item(trans, root, &path); |
| 176 | BUG_ON(ret); |
| 177 | btrfs_release_path(root, &path); |
| 178 | key.offset = 1; |
| 179 | ret = btrfs_search_slot(trans, root, &key, &path, -1, 1); |
| 180 | if (ret < 0) { |
| 181 | err = ret; |
| 182 | goto out; |
| 183 | } |
| 184 | if (ret > 0) { |
| 185 | err = -ENOTEMPTY; |
| 186 | goto out; |
| 187 | } |
| 188 | ret = btrfs_del_item(trans, root, &path); |
| 189 | if (ret) { |
| 190 | err = ret; |
| 191 | goto out; |
| 192 | } |
| 193 | btrfs_release_path(root, &path); |
| 194 | |
| 195 | /* now the directory is empty */ |
| 196 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 197 | if (!err) { |
| 198 | inode->i_size = 0; |
| 199 | } |
| 200 | out: |
| 201 | mutex_unlock(&root->fs_info->fs_mutex); |
| 202 | ret = btrfs_end_transaction(trans, root); |
| 203 | if (ret && !err) |
| 204 | err = ret; |
| 205 | return err; |
| 206 | } |
| 207 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 208 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 209 | struct btrfs_root *root, |
| 210 | struct inode *inode) |
| 211 | { |
| 212 | u64 objectid = inode->i_ino; |
| 213 | struct btrfs_path path; |
| 214 | struct btrfs_inode_map_item *map; |
| 215 | struct btrfs_key stat_data_key; |
| 216 | int ret; |
| 217 | clear_inode(inode); |
| 218 | btrfs_init_path(&path); |
| 219 | ret = btrfs_lookup_inode_map(trans, root, &path, objectid, -1); |
| 220 | if (ret) { |
| 221 | if (ret > 0) |
| 222 | ret = -ENOENT; |
| 223 | btrfs_release_path(root, &path); |
| 224 | goto error; |
| 225 | } |
| 226 | map = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 227 | struct btrfs_inode_map_item); |
| 228 | btrfs_disk_key_to_cpu(&stat_data_key, &map->key); |
| 229 | ret = btrfs_del_item(trans, root->fs_info->inode_root, &path); |
| 230 | BUG_ON(ret); |
| 231 | btrfs_release_path(root, &path); |
| 232 | btrfs_init_path(&path); |
| 233 | |
| 234 | ret = btrfs_lookup_inode(trans, root, &path, objectid, -1); |
| 235 | BUG_ON(ret); |
| 236 | ret = btrfs_del_item(trans, root, &path); |
| 237 | BUG_ON(ret); |
| 238 | btrfs_release_path(root, &path); |
| 239 | error: |
| 240 | return ret; |
| 241 | } |
| 242 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 243 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 244 | struct btrfs_root *root, |
| 245 | struct inode *inode) |
| 246 | { |
| 247 | int ret; |
| 248 | struct btrfs_path path; |
| 249 | struct btrfs_key key; |
| 250 | struct btrfs_disk_key *found_key; |
| 251 | struct btrfs_leaf *leaf; |
| 252 | struct btrfs_file_extent_item *fi; |
| 253 | u64 extent_start; |
| 254 | u64 extent_num_blocks; |
| 255 | |
| 256 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 257 | key.objectid = inode->i_ino; |
| 258 | key.offset = (u64)-1; |
| 259 | key.flags = 0; |
| 260 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 261 | while(1) { |
| 262 | btrfs_init_path(&path); |
| 263 | ret = btrfs_search_slot(trans, root, &key, &path, -1, 1); |
| 264 | if (ret < 0) { |
| 265 | btrfs_release_path(root, &path); |
| 266 | goto error; |
| 267 | } |
| 268 | if (ret > 0) { |
| 269 | BUG_ON(path.slots[0] == 0); |
| 270 | path.slots[0]--; |
| 271 | } |
| 272 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 273 | found_key = &leaf->items[path.slots[0]].key; |
| 274 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 275 | break; |
| 276 | if (btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
| 277 | break; |
| 278 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 279 | break; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 280 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 281 | path.slots[0], |
| 282 | struct btrfs_file_extent_item); |
| 283 | extent_start = btrfs_file_extent_disk_blocknr(fi); |
| 284 | extent_num_blocks = btrfs_file_extent_disk_num_blocks(fi); |
| 285 | key.offset = btrfs_disk_key_offset(found_key) - 1; |
| 286 | ret = btrfs_del_item(trans, root, &path); |
| 287 | BUG_ON(ret); |
| 288 | inode->i_blocks -= btrfs_file_extent_num_blocks(fi) >> 9; |
| 289 | btrfs_release_path(root, &path); |
| 290 | ret = btrfs_free_extent(trans, root, extent_start, |
| 291 | extent_num_blocks, 0); |
| 292 | BUG_ON(ret); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 293 | if (key.offset + 1 == 0) |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | btrfs_release_path(root, &path); |
| 297 | ret = 0; |
| 298 | error: |
| 299 | return ret; |
| 300 | } |
| 301 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 302 | static void btrfs_delete_inode(struct inode *inode) |
| 303 | { |
| 304 | struct btrfs_trans_handle *trans; |
| 305 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 306 | int ret; |
| 307 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 308 | truncate_inode_pages(&inode->i_data, 0); |
| 309 | if (is_bad_inode(inode)) { |
| 310 | goto no_delete; |
| 311 | } |
| 312 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 313 | mutex_lock(&root->fs_info->fs_mutex); |
| 314 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 315 | if (S_ISREG(inode->i_mode)) { |
| 316 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 317 | BUG_ON(ret); |
| 318 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 319 | btrfs_free_inode(trans, root, inode); |
| 320 | btrfs_end_transaction(trans, root); |
| 321 | mutex_unlock(&root->fs_info->fs_mutex); |
| 322 | return; |
| 323 | no_delete: |
| 324 | clear_inode(inode); |
| 325 | } |
| 326 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 327 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 328 | ino_t *ino) |
| 329 | { |
| 330 | const char *name = dentry->d_name.name; |
| 331 | int namelen = dentry->d_name.len; |
| 332 | struct btrfs_dir_item *di; |
| 333 | struct btrfs_path path; |
| 334 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 335 | int ret; |
| 336 | |
| 337 | btrfs_init_path(&path); |
| 338 | ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name, |
| 339 | namelen, 0); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 340 | if (ret || !btrfs_match_dir_item_name(root, &path, name, namelen)) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 341 | *ino = 0; |
| 342 | goto out; |
| 343 | } |
| 344 | di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 345 | struct btrfs_dir_item); |
| 346 | *ino = btrfs_dir_objectid(di); |
| 347 | out: |
| 348 | btrfs_release_path(root, &path); |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 353 | struct nameidata *nd) |
| 354 | { |
| 355 | struct inode * inode; |
| 356 | ino_t ino; |
| 357 | int ret; |
| 358 | |
| 359 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 360 | return ERR_PTR(-ENAMETOOLONG); |
| 361 | |
| 362 | ret = btrfs_inode_by_name(dir, dentry, &ino); |
| 363 | if (ret < 0) |
| 364 | return ERR_PTR(ret); |
| 365 | inode = NULL; |
| 366 | if (ino) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 367 | inode = iget(dir->i_sb, ino); |
| 368 | if (!inode) |
| 369 | return ERR_PTR(-EACCES); |
| 370 | } |
| 371 | return d_splice_alias(inode, dentry); |
| 372 | } |
| 373 | |
| 374 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 375 | { |
| 376 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 377 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 378 | struct btrfs_item *item; |
| 379 | struct btrfs_dir_item *di; |
| 380 | struct btrfs_key key; |
| 381 | struct btrfs_path path; |
| 382 | int ret; |
| 383 | u32 nritems; |
| 384 | struct btrfs_leaf *leaf; |
| 385 | int slot; |
| 386 | int advance; |
| 387 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 388 | int over = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 389 | |
| 390 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 391 | key.flags = 0; |
| 392 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
| 393 | key.offset = filp->f_pos; |
| 394 | btrfs_init_path(&path); |
| 395 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 396 | if (ret < 0) { |
| 397 | goto err; |
| 398 | } |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 399 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 400 | while(1) { |
| 401 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 402 | nritems = btrfs_header_nritems(&leaf->header); |
| 403 | slot = path.slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 404 | if (advance || slot >= nritems) { |
| 405 | if (slot >= nritems -1) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 406 | ret = btrfs_next_leaf(root, &path); |
| 407 | if (ret) |
| 408 | break; |
| 409 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 410 | nritems = btrfs_header_nritems(&leaf->header); |
| 411 | slot = path.slots[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 412 | } else { |
| 413 | slot++; |
| 414 | path.slots[0]++; |
| 415 | } |
| 416 | } |
| 417 | advance = 1; |
| 418 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 419 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 420 | break; |
| 421 | if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY) |
| 422 | continue; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 423 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 424 | continue; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 425 | |
| 426 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 427 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 428 | over = filldir(dirent, (const char *)(di + 1), |
| 429 | btrfs_dir_name_len(di), |
| 430 | btrfs_disk_key_offset(&item->key), |
| 431 | btrfs_dir_objectid(di), d_type); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 432 | if (over) { |
| 433 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 434 | break; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 435 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 436 | filp->f_pos = btrfs_disk_key_offset(&item->key) + 1; |
| 437 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 438 | ret = 0; |
| 439 | err: |
| 440 | btrfs_release_path(root, &path); |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | static void btrfs_put_super (struct super_block * sb) |
| 445 | { |
| 446 | struct btrfs_root *root = btrfs_sb(sb); |
| 447 | int ret; |
| 448 | |
| 449 | ret = close_ctree(root); |
| 450 | if (ret) { |
| 451 | printk("close ctree returns %d\n", ret); |
| 452 | } |
| 453 | sb->s_fs_info = NULL; |
| 454 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 455 | |
| 456 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 457 | { |
| 458 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 459 | struct dentry * root_dentry; |
| 460 | struct btrfs_super_block *disk_super; |
| 461 | struct buffer_head *bh; |
| 462 | struct btrfs_root *root; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 463 | |
| 464 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 465 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 466 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 467 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 468 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 469 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 470 | |
| 471 | bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize); |
| 472 | if (!bh) { |
| 473 | printk("btrfs: unable to read on disk super\n"); |
| 474 | return -EIO; |
| 475 | } |
| 476 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 477 | root = open_ctree(sb, bh, disk_super); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 478 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 479 | if (!root) { |
| 480 | printk("btrfs: open_ctree failed\n"); |
| 481 | return -EIO; |
| 482 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 483 | sb->s_fs_info = root; |
| 484 | disk_super = root->fs_info->disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 485 | printk("read in super total blocks %Lu root %Lu\n", |
| 486 | btrfs_super_total_blocks(disk_super), |
| 487 | btrfs_super_root_dir(disk_super)); |
| 488 | |
| 489 | inode = iget_locked(sb, btrfs_super_root_dir(disk_super)); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 490 | if (!inode) |
| 491 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 492 | if (inode->i_state & I_NEW) { |
| 493 | btrfs_read_locked_inode(inode); |
| 494 | unlock_new_inode(inode); |
| 495 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 496 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 497 | root_dentry = d_alloc_root(inode); |
| 498 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 499 | iput(inode); |
| 500 | return -ENOMEM; |
| 501 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 502 | sb->s_root = root_dentry; |
| 503 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 507 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 508 | struct inode *inode) |
| 509 | { |
| 510 | btrfs_set_inode_uid(item, inode->i_uid); |
| 511 | btrfs_set_inode_gid(item, inode->i_gid); |
| 512 | btrfs_set_inode_size(item, inode->i_size); |
| 513 | btrfs_set_inode_mode(item, inode->i_mode); |
| 514 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 515 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 516 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 517 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 518 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 519 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 520 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 521 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 522 | btrfs_set_inode_generation(item, inode->i_generation); |
| 523 | } |
| 524 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 525 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 526 | struct btrfs_root *root, |
| 527 | struct inode *inode) |
| 528 | { |
| 529 | struct btrfs_inode_item *inode_item; |
| 530 | struct btrfs_path path; |
| 531 | int ret; |
| 532 | |
| 533 | btrfs_init_path(&path); |
| 534 | |
| 535 | ret = btrfs_lookup_inode(trans, root, &path, inode->i_ino, 1); |
| 536 | if (ret) { |
| 537 | if (ret > 0) |
| 538 | ret = -ENOENT; |
| 539 | goto failed; |
| 540 | } |
| 541 | |
| 542 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 543 | path.slots[0], |
| 544 | struct btrfs_inode_item); |
| 545 | |
| 546 | fill_inode_item(inode_item, inode); |
| 547 | mark_buffer_dirty(path.nodes[0]); |
| 548 | failed: |
| 549 | btrfs_release_path(root, &path); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 554 | { |
| 555 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 556 | struct btrfs_trans_handle *trans; |
| 557 | int ret; |
| 558 | |
| 559 | mutex_lock(&root->fs_info->fs_mutex); |
| 560 | trans = btrfs_start_transaction(root, 1); |
| 561 | ret = btrfs_update_inode(trans, root, inode); |
| 562 | if (wait) |
| 563 | btrfs_commit_transaction(trans, root); |
| 564 | else |
| 565 | btrfs_end_transaction(trans, root); |
| 566 | mutex_unlock(&root->fs_info->fs_mutex); |
| 567 | return ret; |
| 568 | } |
| 569 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 570 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 571 | struct inode *dir, int mode) |
| 572 | { |
| 573 | struct inode *inode; |
| 574 | struct btrfs_inode_item inode_item; |
| 575 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 576 | struct btrfs_key key; |
| 577 | int ret; |
| 578 | u64 objectid; |
| 579 | |
| 580 | inode = new_inode(dir->i_sb); |
| 581 | if (!inode) |
| 582 | return ERR_PTR(-ENOMEM); |
| 583 | |
| 584 | ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 585 | BUG_ON(ret); |
| 586 | |
| 587 | inode->i_uid = current->fsuid; |
| 588 | inode->i_gid = current->fsgid; |
| 589 | inode->i_mode = mode; |
| 590 | inode->i_ino = objectid; |
| 591 | inode->i_blocks = 0; |
| 592 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 593 | fill_inode_item(&inode_item, inode); |
| 594 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 595 | key.objectid = objectid; |
| 596 | key.flags = 0; |
| 597 | key.offset = 0; |
| 598 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 599 | ret = btrfs_insert_inode_map(trans, root, objectid, &key); |
| 600 | BUG_ON(ret); |
| 601 | |
| 602 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 603 | BUG_ON(ret); |
| 604 | |
| 605 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 606 | return inode; |
| 607 | } |
| 608 | |
| 609 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 610 | struct dentry *dentry, struct inode *inode) |
| 611 | { |
| 612 | int ret; |
| 613 | ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb), |
| 614 | dentry->d_name.name, dentry->d_name.len, |
| 615 | dentry->d_parent->d_inode->i_ino, |
| 616 | inode->i_ino, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 617 | if (ret == 0) { |
| 618 | dentry->d_parent->d_inode->i_size += dentry->d_name.len; |
| 619 | ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb), |
| 620 | dentry->d_parent->d_inode); |
| 621 | } |
| 622 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 627 | struct dentry *dentry, struct inode *inode) |
| 628 | { |
| 629 | int err = btrfs_add_link(trans, dentry, inode); |
| 630 | if (!err) { |
| 631 | d_instantiate(dentry, inode); |
| 632 | return 0; |
| 633 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 634 | return err; |
| 635 | } |
| 636 | |
| 637 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 638 | int mode, struct nameidata *nd) |
| 639 | { |
| 640 | struct btrfs_trans_handle *trans; |
| 641 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 642 | struct inode *inode; |
| 643 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 644 | int drop_inode = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 645 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 646 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 647 | trans = btrfs_start_transaction(root, 1); |
| 648 | inode = btrfs_new_inode(trans, dir, mode); |
| 649 | err = PTR_ERR(inode); |
| 650 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 651 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 652 | // FIXME mark the inode dirty |
| 653 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 654 | if (err) |
| 655 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 656 | else { |
| 657 | inode->i_mapping->a_ops = &btrfs_aops; |
| 658 | inode->i_fop = &btrfs_file_operations; |
| 659 | inode->i_op = &btrfs_file_inode_operations; |
| 660 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 661 | dir->i_sb->s_dirt = 1; |
| 662 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 663 | out_unlock: |
| 664 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 665 | if (drop_inode) { |
| 666 | inode_dec_link_count(inode); |
| 667 | iput(inode); |
| 668 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 669 | return err; |
| 670 | } |
| 671 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 672 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 673 | struct inode *inode, struct inode *dir) |
| 674 | { |
| 675 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 676 | int ret; |
| 677 | char buf[2]; |
| 678 | buf[0] = '.'; |
| 679 | buf[1] = '.'; |
| 680 | |
| 681 | ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino, |
| 682 | inode->i_ino, 1); |
| 683 | if (ret) |
| 684 | goto error; |
| 685 | ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino, |
| 686 | dir->i_ino, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 687 | if (ret) |
| 688 | goto error; |
| 689 | inode->i_size = 3; |
| 690 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 691 | error: |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 696 | { |
| 697 | struct inode *inode; |
| 698 | struct btrfs_trans_handle *trans; |
| 699 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 700 | int err = 0; |
| 701 | int drop_on_err = 0; |
| 702 | |
| 703 | mutex_lock(&root->fs_info->fs_mutex); |
| 704 | trans = btrfs_start_transaction(root, 1); |
| 705 | if (IS_ERR(trans)) { |
| 706 | err = PTR_ERR(trans); |
| 707 | goto out_unlock; |
| 708 | } |
| 709 | inode = btrfs_new_inode(trans, dir, S_IFDIR | mode); |
| 710 | if (IS_ERR(inode)) { |
| 711 | err = PTR_ERR(inode); |
| 712 | goto out_fail; |
| 713 | } |
| 714 | drop_on_err = 1; |
| 715 | inode->i_op = &btrfs_dir_inode_operations; |
| 716 | inode->i_fop = &btrfs_dir_file_operations; |
| 717 | |
| 718 | err = btrfs_make_empty_dir(trans, inode, dir); |
| 719 | if (err) |
| 720 | goto out_fail; |
| 721 | err = btrfs_add_link(trans, dentry, inode); |
| 722 | if (err) |
| 723 | goto out_fail; |
| 724 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 725 | drop_on_err = 0; |
| 726 | |
| 727 | out_fail: |
| 728 | btrfs_end_transaction(trans, root); |
| 729 | out_unlock: |
| 730 | mutex_unlock(&root->fs_info->fs_mutex); |
| 731 | if (drop_on_err) |
| 732 | iput(inode); |
| 733 | return err; |
| 734 | } |
| 735 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 736 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 737 | { |
| 738 | struct btrfs_trans_handle *trans; |
| 739 | struct btrfs_root *root; |
| 740 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 741 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 742 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 743 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 744 | if (!wait) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 745 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 746 | return 0; |
| 747 | } |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 748 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 749 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 750 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 751 | trans = btrfs_start_transaction(root, 1); |
| 752 | ret = btrfs_commit_transaction(trans, root); |
| 753 | sb->s_dirt = 0; |
| 754 | BUG_ON(ret); |
| 755 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 756 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 757 | return 0; |
| 758 | } |
| 759 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 760 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 761 | struct buffer_head *result, int create) |
| 762 | { |
| 763 | int ret; |
| 764 | int err = 0; |
| 765 | u64 blocknr; |
| 766 | u64 extent_start = 0; |
| 767 | u64 extent_end = 0; |
| 768 | u64 objectid = inode->i_ino; |
| 769 | struct btrfs_path path; |
| 770 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 771 | struct btrfs_trans_handle *trans = NULL; |
| 772 | struct btrfs_file_extent_item *item; |
| 773 | struct btrfs_leaf *leaf; |
| 774 | struct btrfs_disk_key *found_key; |
| 775 | |
| 776 | btrfs_init_path(&path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 777 | if (create) |
| 778 | trans = btrfs_start_transaction(root, 1); |
| 779 | |
| 780 | |
| 781 | ret = btrfs_lookup_file_extent(trans, root, &path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 782 | inode->i_ino, |
| 783 | iblock << inode->i_blkbits, 0); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 784 | if (ret < 0) { |
| 785 | btrfs_release_path(root, &path); |
| 786 | err = ret; |
| 787 | goto out; |
| 788 | } |
| 789 | |
| 790 | if (ret != 0) { |
| 791 | if (path.slots[0] == 0) { |
| 792 | btrfs_release_path(root, &path); |
| 793 | goto allocate; |
| 794 | } |
| 795 | path.slots[0]--; |
| 796 | } |
| 797 | |
| 798 | item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 799 | struct btrfs_file_extent_item); |
| 800 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 801 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 802 | blocknr += btrfs_file_extent_offset(item); |
| 803 | |
| 804 | /* exact match found, use it */ |
| 805 | if (ret == 0) { |
| 806 | err = 0; |
| 807 | map_bh(result, inode->i_sb, blocknr); |
| 808 | btrfs_release_path(root, &path); |
| 809 | goto out; |
| 810 | } |
| 811 | |
| 812 | /* are we inside the extent that was found? */ |
| 813 | found_key = &leaf->items[path.slots[0]].key; |
| 814 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 815 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 816 | extent_end = 0; |
| 817 | extent_start = 0; |
| 818 | btrfs_release_path(root, &path); |
| 819 | goto allocate; |
| 820 | } |
| 821 | |
| 822 | extent_start = btrfs_disk_key_offset(&leaf->items[path.slots[0]].key); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 823 | extent_start = extent_start >> inode->i_blkbits; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 824 | extent_start += btrfs_file_extent_offset(item); |
| 825 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
| 826 | btrfs_release_path(root, &path); |
| 827 | if (iblock >= extent_start && iblock < extent_end) { |
| 828 | err = 0; |
| 829 | map_bh(result, inode->i_sb, blocknr + iblock - extent_start); |
| 830 | goto out; |
| 831 | } |
| 832 | allocate: |
| 833 | /* ok, create a new extent */ |
| 834 | if (!create) { |
| 835 | err = 0; |
| 836 | goto out; |
| 837 | } |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 838 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
| 839 | iblock << inode->i_blkbits, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 840 | 1, extent_end, &blocknr); |
| 841 | if (ret) { |
| 842 | err = ret; |
| 843 | goto out; |
| 844 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 845 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 846 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 847 | map_bh(result, inode->i_sb, blocknr); |
| 848 | |
| 849 | out: |
| 850 | if (trans) |
| 851 | btrfs_end_transaction(trans, root); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 852 | return err; |
| 853 | } |
| 854 | |
| 855 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 856 | struct buffer_head *result, int create) |
| 857 | { |
| 858 | int err; |
| 859 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 860 | mutex_lock(&root->fs_info->fs_mutex); |
| 861 | err = btrfs_get_block_lock(inode, iblock, result, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 862 | mutex_unlock(&root->fs_info->fs_mutex); |
| 863 | return err; |
| 864 | } |
| 865 | |
| 866 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 867 | unsigned from, unsigned to) |
| 868 | { |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 869 | WARN_ON(1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 870 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 871 | } |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 872 | static int btrfs_commit_write(struct file *file, struct page *page, |
| 873 | unsigned from, unsigned to) |
| 874 | { |
| 875 | WARN_ON(1); |
| 876 | return nobh_commit_write(file, page, from, to); |
| 877 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 878 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 879 | static void btrfs_write_super(struct super_block *sb) |
| 880 | { |
| 881 | btrfs_sync_fs(sb, 1); |
| 882 | } |
| 883 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 884 | static int btrfs_readpage(struct file *file, struct page *page) |
| 885 | { |
| 886 | return mpage_readpage(page, btrfs_get_block); |
| 887 | } |
| 888 | |
| 889 | static int btrfs_readpages(struct file *file, struct address_space *mapping, |
| 890 | struct list_head *pages, unsigned nr_pages) |
| 891 | { |
| 892 | return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block); |
| 893 | } |
| 894 | |
| 895 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 896 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 897 | return nobh_writepage(page, btrfs_get_block, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 898 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 899 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 900 | static void btrfs_truncate(struct inode *inode) |
| 901 | { |
| 902 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 903 | int ret; |
| 904 | struct btrfs_trans_handle *trans; |
| 905 | |
| 906 | if (!S_ISREG(inode->i_mode)) |
| 907 | return; |
| 908 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 909 | return; |
| 910 | |
| 911 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
| 912 | |
| 913 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 914 | mutex_lock(&root->fs_info->fs_mutex); |
| 915 | trans = btrfs_start_transaction(root, 1); |
| 916 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 917 | BUG_ON(ret); |
| 918 | ret = btrfs_end_transaction(trans, root); |
| 919 | BUG_ON(ret); |
| 920 | mutex_unlock(&root->fs_info->fs_mutex); |
| 921 | mark_inode_dirty(inode); |
| 922 | } |
| 923 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 924 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 925 | struct page **prepared_pages, |
| 926 | const char __user * buf) |
| 927 | { |
| 928 | long page_fault = 0; |
| 929 | int i; |
| 930 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 931 | |
| 932 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 933 | size_t count = min_t(size_t, |
| 934 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 935 | struct page *page = prepared_pages[i]; |
| 936 | fault_in_pages_readable(buf, count); |
| 937 | |
| 938 | /* Copy data from userspace to the current page */ |
| 939 | kmap(page); |
| 940 | page_fault = __copy_from_user(page_address(page) + offset, |
| 941 | buf, count); |
| 942 | /* Flush processor's dcache for this page */ |
| 943 | flush_dcache_page(page); |
| 944 | kunmap(page); |
| 945 | buf += count; |
| 946 | write_bytes -= count; |
| 947 | |
| 948 | if (page_fault) |
| 949 | break; |
| 950 | } |
| 951 | return page_fault ? -EFAULT : 0; |
| 952 | } |
| 953 | |
| 954 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 955 | { |
| 956 | size_t i; |
| 957 | for (i = 0; i < num_pages; i++) { |
| 958 | if (!pages[i]) |
| 959 | break; |
| 960 | unlock_page(pages[i]); |
| 961 | mark_page_accessed(pages[i]); |
| 962 | page_cache_release(pages[i]); |
| 963 | } |
| 964 | } |
| 965 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 966 | struct btrfs_root *root, |
| 967 | struct file *file, |
| 968 | struct page **pages, |
| 969 | size_t num_pages, |
| 970 | loff_t pos, |
| 971 | size_t write_bytes) |
| 972 | { |
| 973 | int i; |
| 974 | int offset; |
| 975 | int err = 0; |
| 976 | int ret; |
| 977 | int this_write; |
| 978 | |
| 979 | for (i = 0; i < num_pages; i++) { |
| 980 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 981 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
| 982 | ret = nobh_commit_write(file, pages[i], offset, |
| 983 | offset + this_write); |
| 984 | pos += this_write; |
| 985 | if (ret) { |
| 986 | err = ret; |
| 987 | goto failed; |
| 988 | } |
| 989 | WARN_ON(this_write > write_bytes); |
| 990 | write_bytes -= this_write; |
| 991 | } |
| 992 | failed: |
| 993 | return err; |
| 994 | } |
| 995 | |
| 996 | static int prepare_pages(struct btrfs_trans_handle *trans, |
| 997 | struct btrfs_root *root, |
| 998 | struct file *file, |
| 999 | struct page **pages, |
| 1000 | size_t num_pages, |
| 1001 | loff_t pos, |
| 1002 | size_t write_bytes) |
| 1003 | { |
| 1004 | int i; |
| 1005 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 1006 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1007 | int offset; |
| 1008 | int err = 0; |
| 1009 | int ret; |
| 1010 | int this_write; |
| 1011 | loff_t isize = i_size_read(inode); |
| 1012 | |
| 1013 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 1014 | |
| 1015 | for (i = 0; i < num_pages; i++) { |
| 1016 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 1017 | if (!pages[i]) { |
| 1018 | err = -ENOMEM; |
| 1019 | goto failed_release; |
| 1020 | } |
| 1021 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1022 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
| 1023 | ret = nobh_prepare_write(pages[i], offset, |
| 1024 | offset + this_write, |
| 1025 | btrfs_get_block_lock); |
| 1026 | pos += this_write; |
| 1027 | if (ret) { |
| 1028 | err = ret; |
| 1029 | goto failed_truncate; |
| 1030 | } |
| 1031 | WARN_ON(this_write > write_bytes); |
| 1032 | write_bytes -= this_write; |
| 1033 | } |
| 1034 | return 0; |
| 1035 | |
| 1036 | failed_release: |
| 1037 | btrfs_drop_pages(pages, num_pages); |
| 1038 | return err; |
| 1039 | |
| 1040 | failed_truncate: |
| 1041 | btrfs_drop_pages(pages, num_pages); |
| 1042 | if (pos > isize) |
| 1043 | vmtruncate(inode, isize); |
| 1044 | return err; |
| 1045 | } |
| 1046 | |
| 1047 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 1048 | size_t count, loff_t *ppos) |
| 1049 | { |
| 1050 | loff_t pos; |
| 1051 | size_t num_written = 0; |
| 1052 | int err = 0; |
| 1053 | int ret = 0; |
| 1054 | struct btrfs_trans_handle *trans; |
| 1055 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1056 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 1057 | struct page *pages[1]; |
| 1058 | |
| 1059 | if (file->f_flags & O_DIRECT) |
| 1060 | return -EINVAL; |
| 1061 | pos = *ppos; |
| 1062 | |
| 1063 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1064 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1065 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1066 | if (err) |
| 1067 | goto out; |
| 1068 | if (count == 0) |
| 1069 | goto out; |
| 1070 | err = remove_suid(file->f_path.dentry); |
| 1071 | if (err) |
| 1072 | goto out; |
| 1073 | file_update_time(file); |
| 1074 | mutex_lock(&inode->i_mutex); |
| 1075 | while(count > 0) { |
| 1076 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1077 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1078 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 1079 | PAGE_CACHE_SHIFT; |
| 1080 | mutex_lock(&root->fs_info->fs_mutex); |
| 1081 | trans = btrfs_start_transaction(root, 1); |
| 1082 | |
| 1083 | ret = prepare_pages(trans, root, file, pages, num_pages, |
| 1084 | pos, write_bytes); |
| 1085 | BUG_ON(ret); |
| 1086 | ret = btrfs_copy_from_user(pos, num_pages, |
| 1087 | write_bytes, pages, buf); |
| 1088 | BUG_ON(ret); |
| 1089 | |
| 1090 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1091 | |
| 1092 | ret = dirty_and_release_pages(trans, root, file, pages, |
| 1093 | num_pages, pos, write_bytes); |
| 1094 | BUG_ON(ret); |
| 1095 | btrfs_drop_pages(pages, num_pages); |
| 1096 | |
| 1097 | ret = btrfs_end_transaction(trans, root); |
| 1098 | |
| 1099 | buf += write_bytes; |
| 1100 | count -= write_bytes; |
| 1101 | pos += write_bytes; |
| 1102 | num_written += write_bytes; |
| 1103 | |
| 1104 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1105 | cond_resched(); |
| 1106 | } |
| 1107 | mutex_unlock(&inode->i_mutex); |
| 1108 | out: |
| 1109 | *ppos = pos; |
| 1110 | current->backing_dev_info = NULL; |
| 1111 | return num_written ? num_written : err; |
| 1112 | } |
| 1113 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1114 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 1115 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 1116 | { |
| 1117 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 1118 | btrfs_fill_super, mnt); |
| 1119 | } |
| 1120 | |
| 1121 | static struct file_system_type btrfs_fs_type = { |
| 1122 | .owner = THIS_MODULE, |
| 1123 | .name = "btrfs", |
| 1124 | .get_sb = btrfs_get_sb, |
| 1125 | .kill_sb = kill_block_super, |
| 1126 | .fs_flags = FS_REQUIRES_DEV, |
| 1127 | }; |
| 1128 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1129 | static struct super_operations btrfs_super_ops = { |
| 1130 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 1131 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1132 | .put_super = btrfs_put_super, |
| 1133 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1134 | .write_super = btrfs_write_super, |
| 1135 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 1136 | .write_inode = btrfs_write_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1137 | }; |
| 1138 | |
| 1139 | static struct inode_operations btrfs_dir_inode_operations = { |
| 1140 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1141 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 1142 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 1143 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 1144 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1145 | }; |
| 1146 | |
| 1147 | static struct file_operations btrfs_dir_file_operations = { |
| 1148 | .llseek = generic_file_llseek, |
| 1149 | .read = generic_read_dir, |
| 1150 | .readdir = btrfs_readdir, |
| 1151 | }; |
| 1152 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1153 | static struct address_space_operations btrfs_aops = { |
| 1154 | .readpage = btrfs_readpage, |
| 1155 | .readpages = btrfs_readpages, |
| 1156 | .writepage = btrfs_writepage, |
| 1157 | .sync_page = block_sync_page, |
| 1158 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 1159 | .commit_write = btrfs_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1160 | }; |
| 1161 | |
| 1162 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1163 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1164 | }; |
| 1165 | |
| 1166 | static struct file_operations btrfs_file_operations = { |
| 1167 | .llseek = generic_file_llseek, |
| 1168 | .read = do_sync_read, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame^] | 1169 | .aio_read = generic_file_aio_read, |
| 1170 | .write = btrfs_file_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1171 | .mmap = generic_file_mmap, |
| 1172 | .open = generic_file_open, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1173 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1174 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1175 | static int __init init_btrfs_fs(void) |
| 1176 | { |
| 1177 | printk("btrfs loaded!\n"); |
| 1178 | return register_filesystem(&btrfs_fs_type); |
| 1179 | } |
| 1180 | |
| 1181 | static void __exit exit_btrfs_fs(void) |
| 1182 | { |
| 1183 | unregister_filesystem(&btrfs_fs_type); |
| 1184 | printk("btrfs unloaded\n"); |
| 1185 | } |
| 1186 | |
| 1187 | module_init(init_btrfs_fs) |
| 1188 | module_exit(exit_btrfs_fs) |
| 1189 | |
| 1190 | MODULE_LICENSE("GPL"); |