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