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