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