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