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; |
| 477 | if (!root) { |
| 478 | printk("btrfs: open_ctree failed\n"); |
| 479 | return -EIO; |
| 480 | } |
| 481 | printk("read in super total blocks %Lu root %Lu\n", |
| 482 | btrfs_super_total_blocks(disk_super), |
| 483 | btrfs_super_root_dir(disk_super)); |
| 484 | |
| 485 | inode = iget_locked(sb, btrfs_super_root_dir(disk_super)); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 486 | if (!inode) |
| 487 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 488 | if (inode->i_state & I_NEW) { |
| 489 | btrfs_read_locked_inode(inode); |
| 490 | unlock_new_inode(inode); |
| 491 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 492 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 493 | root_dentry = d_alloc_root(inode); |
| 494 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 495 | iput(inode); |
| 496 | return -ENOMEM; |
| 497 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 498 | sb->s_root = root_dentry; |
| 499 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 503 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 504 | struct inode *inode) |
| 505 | { |
| 506 | btrfs_set_inode_uid(item, inode->i_uid); |
| 507 | btrfs_set_inode_gid(item, inode->i_gid); |
| 508 | btrfs_set_inode_size(item, inode->i_size); |
| 509 | btrfs_set_inode_mode(item, inode->i_mode); |
| 510 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 511 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 512 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 513 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 514 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 515 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 516 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 517 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 518 | btrfs_set_inode_generation(item, inode->i_generation); |
| 519 | } |
| 520 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 521 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 522 | struct btrfs_root *root, |
| 523 | struct inode *inode) |
| 524 | { |
| 525 | struct btrfs_inode_item *inode_item; |
| 526 | struct btrfs_path path; |
| 527 | int ret; |
| 528 | |
| 529 | btrfs_init_path(&path); |
| 530 | |
| 531 | ret = btrfs_lookup_inode(trans, root, &path, inode->i_ino, 1); |
| 532 | if (ret) { |
| 533 | if (ret > 0) |
| 534 | ret = -ENOENT; |
| 535 | goto failed; |
| 536 | } |
| 537 | |
| 538 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 539 | path.slots[0], |
| 540 | struct btrfs_inode_item); |
| 541 | |
| 542 | fill_inode_item(inode_item, inode); |
| 543 | mark_buffer_dirty(path.nodes[0]); |
| 544 | failed: |
| 545 | btrfs_release_path(root, &path); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 550 | { |
| 551 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 552 | struct btrfs_trans_handle *trans; |
| 553 | int ret; |
| 554 | |
| 555 | mutex_lock(&root->fs_info->fs_mutex); |
| 556 | trans = btrfs_start_transaction(root, 1); |
| 557 | ret = btrfs_update_inode(trans, root, inode); |
| 558 | if (wait) |
| 559 | btrfs_commit_transaction(trans, root); |
| 560 | else |
| 561 | btrfs_end_transaction(trans, root); |
| 562 | mutex_unlock(&root->fs_info->fs_mutex); |
| 563 | return ret; |
| 564 | } |
| 565 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 566 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 567 | struct inode *dir, int mode) |
| 568 | { |
| 569 | struct inode *inode; |
| 570 | struct btrfs_inode_item inode_item; |
| 571 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 572 | struct btrfs_key key; |
| 573 | int ret; |
| 574 | u64 objectid; |
| 575 | |
| 576 | inode = new_inode(dir->i_sb); |
| 577 | if (!inode) |
| 578 | return ERR_PTR(-ENOMEM); |
| 579 | |
| 580 | ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 581 | BUG_ON(ret); |
| 582 | |
| 583 | inode->i_uid = current->fsuid; |
| 584 | inode->i_gid = current->fsgid; |
| 585 | inode->i_mode = mode; |
| 586 | inode->i_ino = objectid; |
| 587 | inode->i_blocks = 0; |
| 588 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 589 | fill_inode_item(&inode_item, inode); |
| 590 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 591 | key.objectid = objectid; |
| 592 | key.flags = 0; |
| 593 | key.offset = 0; |
| 594 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 595 | ret = btrfs_insert_inode_map(trans, root, objectid, &key); |
| 596 | BUG_ON(ret); |
| 597 | |
| 598 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 599 | BUG_ON(ret); |
| 600 | |
| 601 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 602 | return inode; |
| 603 | } |
| 604 | |
| 605 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 606 | struct dentry *dentry, struct inode *inode) |
| 607 | { |
| 608 | int ret; |
| 609 | ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb), |
| 610 | dentry->d_name.name, dentry->d_name.len, |
| 611 | dentry->d_parent->d_inode->i_ino, |
| 612 | inode->i_ino, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 613 | if (ret == 0) { |
| 614 | dentry->d_parent->d_inode->i_size += dentry->d_name.len; |
| 615 | ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb), |
| 616 | dentry->d_parent->d_inode); |
| 617 | } |
| 618 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 623 | struct dentry *dentry, struct inode *inode) |
| 624 | { |
| 625 | int err = btrfs_add_link(trans, dentry, inode); |
| 626 | if (!err) { |
| 627 | d_instantiate(dentry, inode); |
| 628 | return 0; |
| 629 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 630 | return err; |
| 631 | } |
| 632 | |
| 633 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 634 | int mode, struct nameidata *nd) |
| 635 | { |
| 636 | struct btrfs_trans_handle *trans; |
| 637 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 638 | struct inode *inode; |
| 639 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 640 | int drop_inode = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 641 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 642 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 643 | trans = btrfs_start_transaction(root, 1); |
| 644 | inode = btrfs_new_inode(trans, dir, mode); |
| 645 | err = PTR_ERR(inode); |
| 646 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 647 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 648 | // FIXME mark the inode dirty |
| 649 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 650 | if (err) |
| 651 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 652 | else { |
| 653 | inode->i_mapping->a_ops = &btrfs_aops; |
| 654 | inode->i_fop = &btrfs_file_operations; |
| 655 | inode->i_op = &btrfs_file_inode_operations; |
| 656 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 657 | dir->i_sb->s_dirt = 1; |
| 658 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 659 | out_unlock: |
| 660 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 661 | if (drop_inode) { |
| 662 | inode_dec_link_count(inode); |
| 663 | iput(inode); |
| 664 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 665 | return err; |
| 666 | } |
| 667 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 668 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 669 | struct inode *inode, struct inode *dir) |
| 670 | { |
| 671 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 672 | int ret; |
| 673 | char buf[2]; |
| 674 | buf[0] = '.'; |
| 675 | buf[1] = '.'; |
| 676 | |
| 677 | ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino, |
| 678 | inode->i_ino, 1); |
| 679 | if (ret) |
| 680 | goto error; |
| 681 | ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino, |
| 682 | dir->i_ino, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 683 | if (ret) |
| 684 | goto error; |
| 685 | inode->i_size = 3; |
| 686 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 687 | error: |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 692 | { |
| 693 | struct inode *inode; |
| 694 | struct btrfs_trans_handle *trans; |
| 695 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 696 | int err = 0; |
| 697 | int drop_on_err = 0; |
| 698 | |
| 699 | mutex_lock(&root->fs_info->fs_mutex); |
| 700 | trans = btrfs_start_transaction(root, 1); |
| 701 | if (IS_ERR(trans)) { |
| 702 | err = PTR_ERR(trans); |
| 703 | goto out_unlock; |
| 704 | } |
| 705 | inode = btrfs_new_inode(trans, dir, S_IFDIR | mode); |
| 706 | if (IS_ERR(inode)) { |
| 707 | err = PTR_ERR(inode); |
| 708 | goto out_fail; |
| 709 | } |
| 710 | drop_on_err = 1; |
| 711 | inode->i_op = &btrfs_dir_inode_operations; |
| 712 | inode->i_fop = &btrfs_dir_file_operations; |
| 713 | |
| 714 | err = btrfs_make_empty_dir(trans, inode, dir); |
| 715 | if (err) |
| 716 | goto out_fail; |
| 717 | err = btrfs_add_link(trans, dentry, inode); |
| 718 | if (err) |
| 719 | goto out_fail; |
| 720 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 721 | drop_on_err = 0; |
| 722 | |
| 723 | out_fail: |
| 724 | btrfs_end_transaction(trans, root); |
| 725 | out_unlock: |
| 726 | mutex_unlock(&root->fs_info->fs_mutex); |
| 727 | if (drop_on_err) |
| 728 | iput(inode); |
| 729 | return err; |
| 730 | } |
| 731 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 732 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 733 | { |
| 734 | struct btrfs_trans_handle *trans; |
| 735 | struct btrfs_root *root; |
| 736 | int ret; |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 737 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 738 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 739 | if (!wait) { |
| 740 | filemap_flush(sb->s_bdev->bd_inode->i_mapping); |
| 741 | return 0; |
| 742 | } |
| 743 | filemap_write_and_wait(sb->s_bdev->bd_inode->i_mapping); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 744 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 745 | root = btrfs_sb(sb); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 746 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 747 | trans = btrfs_start_transaction(root, 1); |
| 748 | ret = btrfs_commit_transaction(trans, root); |
| 749 | sb->s_dirt = 0; |
| 750 | BUG_ON(ret); |
| 751 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 752 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 756 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 757 | struct buffer_head *result, int create) |
| 758 | { |
| 759 | int ret; |
| 760 | int err = 0; |
| 761 | u64 blocknr; |
| 762 | u64 extent_start = 0; |
| 763 | u64 extent_end = 0; |
| 764 | u64 objectid = inode->i_ino; |
| 765 | struct btrfs_path path; |
| 766 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 767 | struct btrfs_trans_handle *trans = NULL; |
| 768 | struct btrfs_file_extent_item *item; |
| 769 | struct btrfs_leaf *leaf; |
| 770 | struct btrfs_disk_key *found_key; |
| 771 | |
| 772 | btrfs_init_path(&path); |
| 773 | mutex_lock(&root->fs_info->fs_mutex); |
| 774 | if (create) |
| 775 | trans = btrfs_start_transaction(root, 1); |
| 776 | |
| 777 | |
| 778 | ret = btrfs_lookup_file_extent(trans, root, &path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 779 | inode->i_ino, |
| 780 | iblock << inode->i_blkbits, 0); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 781 | if (ret < 0) { |
| 782 | btrfs_release_path(root, &path); |
| 783 | err = ret; |
| 784 | goto out; |
| 785 | } |
| 786 | |
| 787 | if (ret != 0) { |
| 788 | if (path.slots[0] == 0) { |
| 789 | btrfs_release_path(root, &path); |
| 790 | goto allocate; |
| 791 | } |
| 792 | path.slots[0]--; |
| 793 | } |
| 794 | |
| 795 | item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 796 | struct btrfs_file_extent_item); |
| 797 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 798 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 799 | blocknr += btrfs_file_extent_offset(item); |
| 800 | |
| 801 | /* exact match found, use it */ |
| 802 | if (ret == 0) { |
| 803 | err = 0; |
| 804 | map_bh(result, inode->i_sb, blocknr); |
| 805 | btrfs_release_path(root, &path); |
| 806 | goto out; |
| 807 | } |
| 808 | |
| 809 | /* are we inside the extent that was found? */ |
| 810 | found_key = &leaf->items[path.slots[0]].key; |
| 811 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 812 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 813 | extent_end = 0; |
| 814 | extent_start = 0; |
| 815 | btrfs_release_path(root, &path); |
| 816 | goto allocate; |
| 817 | } |
| 818 | |
| 819 | extent_start = btrfs_disk_key_offset(&leaf->items[path.slots[0]].key); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 820 | extent_start = extent_start >> inode->i_blkbits; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 821 | extent_start += btrfs_file_extent_offset(item); |
| 822 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
| 823 | btrfs_release_path(root, &path); |
| 824 | if (iblock >= extent_start && iblock < extent_end) { |
| 825 | err = 0; |
| 826 | map_bh(result, inode->i_sb, blocknr + iblock - extent_start); |
| 827 | goto out; |
| 828 | } |
| 829 | allocate: |
| 830 | /* ok, create a new extent */ |
| 831 | if (!create) { |
| 832 | err = 0; |
| 833 | goto out; |
| 834 | } |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 835 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
| 836 | iblock << inode->i_blkbits, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 837 | 1, extent_end, &blocknr); |
| 838 | if (ret) { |
| 839 | err = ret; |
| 840 | goto out; |
| 841 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 842 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 843 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 844 | map_bh(result, inode->i_sb, blocknr); |
| 845 | |
| 846 | out: |
| 847 | if (trans) |
| 848 | btrfs_end_transaction(trans, root); |
| 849 | mutex_unlock(&root->fs_info->fs_mutex); |
| 850 | return err; |
| 851 | } |
| 852 | |
| 853 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 854 | unsigned from, unsigned to) |
| 855 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 856 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 857 | } |
| 858 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 859 | static void btrfs_write_super(struct super_block *sb) |
| 860 | { |
| 861 | btrfs_sync_fs(sb, 1); |
| 862 | } |
| 863 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 864 | static int btrfs_readpage(struct file *file, struct page *page) |
| 865 | { |
| 866 | return mpage_readpage(page, btrfs_get_block); |
| 867 | } |
| 868 | |
| 869 | static int btrfs_readpages(struct file *file, struct address_space *mapping, |
| 870 | struct list_head *pages, unsigned nr_pages) |
| 871 | { |
| 872 | return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block); |
| 873 | } |
| 874 | |
| 875 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 876 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 877 | return nobh_writepage(page, btrfs_get_block, wbc); |
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 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 880 | static void btrfs_truncate(struct inode *inode) |
| 881 | { |
| 882 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 883 | int ret; |
| 884 | struct btrfs_trans_handle *trans; |
| 885 | |
| 886 | if (!S_ISREG(inode->i_mode)) |
| 887 | return; |
| 888 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 889 | return; |
| 890 | |
| 891 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
| 892 | |
| 893 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 894 | mutex_lock(&root->fs_info->fs_mutex); |
| 895 | trans = btrfs_start_transaction(root, 1); |
| 896 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 897 | BUG_ON(ret); |
| 898 | ret = btrfs_end_transaction(trans, root); |
| 899 | BUG_ON(ret); |
| 900 | mutex_unlock(&root->fs_info->fs_mutex); |
| 901 | mark_inode_dirty(inode); |
| 902 | } |
| 903 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 904 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 905 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 906 | { |
| 907 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 908 | btrfs_fill_super, mnt); |
| 909 | } |
| 910 | |
| 911 | static struct file_system_type btrfs_fs_type = { |
| 912 | .owner = THIS_MODULE, |
| 913 | .name = "btrfs", |
| 914 | .get_sb = btrfs_get_sb, |
| 915 | .kill_sb = kill_block_super, |
| 916 | .fs_flags = FS_REQUIRES_DEV, |
| 917 | }; |
| 918 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 919 | static struct super_operations btrfs_super_ops = { |
| 920 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 921 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 922 | .put_super = btrfs_put_super, |
| 923 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 924 | .write_super = btrfs_write_super, |
| 925 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 926 | .write_inode = btrfs_write_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 927 | }; |
| 928 | |
| 929 | static struct inode_operations btrfs_dir_inode_operations = { |
| 930 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 931 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 932 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 933 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame^] | 934 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 935 | }; |
| 936 | |
| 937 | static struct file_operations btrfs_dir_file_operations = { |
| 938 | .llseek = generic_file_llseek, |
| 939 | .read = generic_read_dir, |
| 940 | .readdir = btrfs_readdir, |
| 941 | }; |
| 942 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 943 | static struct address_space_operations btrfs_aops = { |
| 944 | .readpage = btrfs_readpage, |
| 945 | .readpages = btrfs_readpages, |
| 946 | .writepage = btrfs_writepage, |
| 947 | .sync_page = block_sync_page, |
| 948 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 949 | .commit_write = nobh_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 950 | }; |
| 951 | |
| 952 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 953 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 954 | }; |
| 955 | |
| 956 | static struct file_operations btrfs_file_operations = { |
| 957 | .llseek = generic_file_llseek, |
| 958 | .read = do_sync_read, |
| 959 | .write = do_sync_write, |
| 960 | .aio_read = generic_file_aio_read, |
| 961 | .aio_write = generic_file_aio_write, |
| 962 | .mmap = generic_file_mmap, |
| 963 | .open = generic_file_open, |
| 964 | .sendfile = generic_file_sendfile, |
| 965 | .splice_read = generic_file_splice_read, |
| 966 | .splice_write = generic_file_splice_write, |
| 967 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 968 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 969 | static int __init init_btrfs_fs(void) |
| 970 | { |
| 971 | printk("btrfs loaded!\n"); |
| 972 | return register_filesystem(&btrfs_fs_type); |
| 973 | } |
| 974 | |
| 975 | static void __exit exit_btrfs_fs(void) |
| 976 | { |
| 977 | unregister_filesystem(&btrfs_fs_type); |
| 978 | printk("btrfs unloaded\n"); |
| 979 | } |
| 980 | |
| 981 | module_init(init_btrfs_fs) |
| 982 | module_exit(exit_btrfs_fs) |
| 983 | |
| 984 | MODULE_LICENSE("GPL"); |