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 | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 12 | #include <linux/swap.h> |
| 13 | #include <linux/writeback.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 14 | #include "ctree.h" |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 15 | #include "disk-io.h" |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 16 | #include "transaction.h" |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 17 | #include "btrfs_inode.h" |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 18 | #include "ioctl.h" |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 19 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 20 | void btrfs_fsinfo_release(struct kobject *obj) |
| 21 | { |
| 22 | struct btrfs_fs_info *fsinfo = container_of(obj, |
| 23 | struct btrfs_fs_info, kobj); |
| 24 | kfree(fsinfo); |
| 25 | } |
| 26 | |
| 27 | struct kobj_type btrfs_fsinfo_ktype = { |
| 28 | .release = btrfs_fsinfo_release, |
| 29 | }; |
| 30 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 31 | struct btrfs_iget_args { |
| 32 | u64 ino; |
| 33 | struct btrfs_root *root; |
| 34 | }; |
| 35 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 36 | decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL); |
| 37 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 38 | #define BTRFS_SUPER_MAGIC 0x9123682E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 39 | |
| 40 | static struct inode_operations btrfs_dir_inode_operations; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 41 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 42 | static struct super_operations btrfs_super_ops; |
| 43 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 44 | static struct inode_operations btrfs_file_inode_operations; |
| 45 | static struct address_space_operations btrfs_aops; |
| 46 | static struct file_operations btrfs_file_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 47 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 48 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 49 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 50 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 51 | struct btrfs_inode_item *inode_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 52 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 53 | struct btrfs_key location; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 54 | int ret; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 55 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 56 | path = btrfs_alloc_path(); |
| 57 | BUG_ON(!path); |
| 58 | btrfs_init_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 59 | mutex_lock(&root->fs_info->fs_mutex); |
| 60 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 61 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
| 62 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 63 | if (ret) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 64 | btrfs_free_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 65 | goto make_bad; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 66 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 67 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 68 | path->slots[0], |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 69 | struct btrfs_inode_item); |
| 70 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 71 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 72 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 73 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 74 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 75 | inode->i_size = btrfs_inode_size(inode_item); |
| 76 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 77 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 78 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 79 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 80 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 81 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 82 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 83 | inode->i_generation = btrfs_inode_generation(inode_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 84 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 85 | btrfs_free_path(path); |
| 86 | inode_item = NULL; |
| 87 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 88 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 89 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 90 | switch (inode->i_mode & S_IFMT) { |
| 91 | #if 0 |
| 92 | default: |
| 93 | init_special_inode(inode, inode->i_mode, |
| 94 | btrfs_inode_rdev(inode_item)); |
| 95 | break; |
| 96 | #endif |
| 97 | case S_IFREG: |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 98 | inode->i_mapping->a_ops = &btrfs_aops; |
| 99 | inode->i_fop = &btrfs_file_operations; |
| 100 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 101 | break; |
| 102 | case S_IFDIR: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 103 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 104 | if (root == root->fs_info->tree_root) |
| 105 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 106 | else |
| 107 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 108 | break; |
| 109 | case S_IFLNK: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 110 | // inode->i_op = &page_symlink_inode_operations; |
| 111 | break; |
| 112 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 113 | return; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 114 | |
| 115 | make_bad: |
| 116 | btrfs_release_path(root, path); |
| 117 | btrfs_free_path(path); |
| 118 | mutex_unlock(&root->fs_info->fs_mutex); |
| 119 | make_bad_inode(inode); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 122 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 123 | struct btrfs_root *root, |
| 124 | struct inode *dir, |
| 125 | struct dentry *dentry) |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 126 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 127 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 128 | const char *name = dentry->d_name.name; |
| 129 | int name_len = dentry->d_name.len; |
| 130 | int ret; |
| 131 | u64 objectid; |
| 132 | struct btrfs_dir_item *di; |
| 133 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 134 | path = btrfs_alloc_path(); |
| 135 | BUG_ON(!path); |
| 136 | btrfs_init_path(path); |
| 137 | ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 138 | name, name_len, -1); |
| 139 | if (ret < 0) |
| 140 | goto err; |
| 141 | if (ret > 0) { |
| 142 | ret = -ENOENT; |
| 143 | goto err; |
| 144 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 145 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 146 | struct btrfs_dir_item); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 147 | objectid = btrfs_disk_key_objectid(&di->location); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 148 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 149 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 150 | BUG_ON(ret); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 151 | |
| 152 | btrfs_release_path(root, path); |
| 153 | ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 154 | objectid, -1); |
| 155 | BUG_ON(ret); |
| 156 | ret = btrfs_del_item(trans, root, path); |
| 157 | BUG_ON(ret); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 158 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 159 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 160 | btrfs_release_path(root, path); |
| 161 | btrfs_free_path(path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 162 | if (ret == 0) { |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 163 | inode_dec_link_count(dentry->d_inode); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 164 | dir->i_size -= name_len * 2; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 165 | mark_inode_dirty(dir); |
| 166 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 170 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 171 | { |
| 172 | struct btrfs_root *root; |
| 173 | struct btrfs_trans_handle *trans; |
| 174 | int ret; |
| 175 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 176 | root = BTRFS_I(dir)->root; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 177 | mutex_lock(&root->fs_info->fs_mutex); |
| 178 | trans = btrfs_start_transaction(root, 1); |
| 179 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 180 | btrfs_end_transaction(trans, root); |
| 181 | mutex_unlock(&root->fs_info->fs_mutex); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 186 | { |
| 187 | struct inode *inode = dentry->d_inode; |
| 188 | int err; |
| 189 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 190 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 191 | struct btrfs_path *path; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 192 | struct btrfs_key key; |
| 193 | struct btrfs_trans_handle *trans; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 194 | struct btrfs_key found_key; |
| 195 | int found_type; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 196 | struct btrfs_leaf *leaf; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 197 | char *goodnames = ".."; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 198 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 199 | path = btrfs_alloc_path(); |
| 200 | BUG_ON(!path); |
| 201 | btrfs_init_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 202 | mutex_lock(&root->fs_info->fs_mutex); |
| 203 | trans = btrfs_start_transaction(root, 1); |
| 204 | key.objectid = inode->i_ino; |
| 205 | key.offset = (u64)-1; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 206 | key.flags = (u32)-1; |
| 207 | while(1) { |
| 208 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 209 | if (ret < 0) { |
| 210 | err = ret; |
| 211 | goto out; |
| 212 | } |
| 213 | BUG_ON(ret == 0); |
| 214 | if (path->slots[0] == 0) { |
| 215 | err = -ENOENT; |
| 216 | goto out; |
| 217 | } |
| 218 | path->slots[0]--; |
| 219 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 220 | btrfs_disk_key_to_cpu(&found_key, |
| 221 | &leaf->items[path->slots[0]].key); |
| 222 | found_type = btrfs_key_type(&found_key); |
| 223 | if (found_key.objectid != inode->i_ino) { |
| 224 | err = -ENOENT; |
| 225 | goto out; |
| 226 | } |
| 227 | if ((found_type != BTRFS_DIR_ITEM_KEY && |
| 228 | found_type != BTRFS_DIR_INDEX_KEY) || |
| 229 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && |
| 230 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { |
| 231 | err = -ENOTEMPTY; |
| 232 | goto out; |
| 233 | } |
| 234 | ret = btrfs_del_item(trans, root, path); |
| 235 | BUG_ON(ret); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 236 | |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 237 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
| 238 | break; |
| 239 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 240 | } |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 241 | ret = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 242 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 243 | |
| 244 | /* now the directory is empty */ |
| 245 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 246 | if (!err) { |
| 247 | inode->i_size = 0; |
| 248 | } |
| 249 | out: |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 250 | btrfs_release_path(root, path); |
| 251 | btrfs_free_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 252 | mutex_unlock(&root->fs_info->fs_mutex); |
| 253 | ret = btrfs_end_transaction(trans, root); |
| 254 | if (ret && !err) |
| 255 | err = ret; |
| 256 | return err; |
| 257 | } |
| 258 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 259 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 260 | struct btrfs_root *root, |
| 261 | struct inode *inode) |
| 262 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 263 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 264 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 265 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 266 | clear_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 267 | |
| 268 | path = btrfs_alloc_path(); |
| 269 | BUG_ON(!path); |
| 270 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 271 | ret = btrfs_lookup_inode(trans, root, path, |
| 272 | &BTRFS_I(inode)->location, -1); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 273 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 274 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 275 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 276 | btrfs_free_path(path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 277 | return ret; |
| 278 | } |
| 279 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 280 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 281 | struct btrfs_root *root, |
| 282 | struct inode *inode) |
| 283 | { |
| 284 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 285 | struct btrfs_path *path; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 286 | struct btrfs_key key; |
| 287 | struct btrfs_disk_key *found_key; |
| 288 | struct btrfs_leaf *leaf; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 289 | struct btrfs_file_extent_item *fi = NULL; |
| 290 | u64 extent_start = 0; |
| 291 | u64 extent_num_blocks = 0; |
| 292 | int found_extent; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 293 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 294 | path = btrfs_alloc_path(); |
| 295 | BUG_ON(!path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 296 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 297 | key.objectid = inode->i_ino; |
| 298 | key.offset = (u64)-1; |
| 299 | key.flags = 0; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 300 | /* |
| 301 | * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys |
| 302 | * or extent data |
| 303 | */ |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 304 | btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 305 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 306 | btrfs_init_path(path); |
| 307 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 308 | if (ret < 0) { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 309 | goto error; |
| 310 | } |
| 311 | if (ret > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 312 | BUG_ON(path->slots[0] == 0); |
| 313 | path->slots[0]--; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 314 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 315 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 316 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 317 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 318 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 319 | if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY && |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 320 | btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY && |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 321 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 322 | break; |
| 323 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 324 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 325 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 326 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 327 | path->slots[0], |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 328 | struct btrfs_file_extent_item); |
| 329 | extent_start = btrfs_file_extent_disk_blocknr(fi); |
| 330 | extent_num_blocks = |
| 331 | btrfs_file_extent_disk_num_blocks(fi); |
| 332 | inode->i_blocks -= |
| 333 | btrfs_file_extent_num_blocks(fi) >> 9; |
| 334 | found_extent = 1; |
| 335 | } else { |
| 336 | found_extent = 0; |
| 337 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 338 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 339 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 340 | btrfs_release_path(root, path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 341 | if (found_extent) { |
| 342 | ret = btrfs_free_extent(trans, root, extent_start, |
| 343 | extent_num_blocks, 0); |
| 344 | BUG_ON(ret); |
| 345 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 346 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 347 | ret = 0; |
| 348 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 349 | btrfs_release_path(root, path); |
| 350 | btrfs_free_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 351 | return ret; |
| 352 | } |
| 353 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 354 | static void btrfs_delete_inode(struct inode *inode) |
| 355 | { |
| 356 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 357 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 358 | int ret; |
| 359 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 360 | truncate_inode_pages(&inode->i_data, 0); |
| 361 | if (is_bad_inode(inode)) { |
| 362 | goto no_delete; |
| 363 | } |
| 364 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 365 | mutex_lock(&root->fs_info->fs_mutex); |
| 366 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 367 | if (S_ISREG(inode->i_mode)) { |
| 368 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 369 | BUG_ON(ret); |
| 370 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 371 | btrfs_free_inode(trans, root, inode); |
| 372 | btrfs_end_transaction(trans, root); |
| 373 | mutex_unlock(&root->fs_info->fs_mutex); |
| 374 | return; |
| 375 | no_delete: |
| 376 | clear_inode(inode); |
| 377 | } |
| 378 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 379 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 380 | struct btrfs_key *location) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 381 | { |
| 382 | const char *name = dentry->d_name.name; |
| 383 | int namelen = dentry->d_name.len; |
| 384 | struct btrfs_dir_item *di; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 385 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 386 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 387 | int ret; |
| 388 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 389 | path = btrfs_alloc_path(); |
| 390 | BUG_ON(!path); |
| 391 | btrfs_init_path(path); |
| 392 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 393 | namelen, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 394 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 395 | location->objectid = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 396 | ret = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 397 | goto out; |
| 398 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 399 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 400 | struct btrfs_dir_item); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 401 | btrfs_disk_key_to_cpu(location, &di->location); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 402 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 403 | btrfs_release_path(root, path); |
| 404 | btrfs_free_path(path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 405 | return ret; |
| 406 | } |
| 407 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 408 | int fixup_tree_root_location(struct btrfs_root *root, |
| 409 | struct btrfs_key *location, |
| 410 | struct btrfs_root **sub_root) |
| 411 | { |
| 412 | struct btrfs_path *path; |
| 413 | struct btrfs_root_item *ri; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 414 | |
| 415 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 416 | return 0; |
| 417 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 418 | return 0; |
| 419 | |
| 420 | path = btrfs_alloc_path(); |
| 421 | BUG_ON(!path); |
| 422 | mutex_lock(&root->fs_info->fs_mutex); |
| 423 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 424 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
| 425 | if (IS_ERR(*sub_root)) |
| 426 | return PTR_ERR(*sub_root); |
| 427 | |
| 428 | ri = &(*sub_root)->root_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 429 | location->objectid = btrfs_root_dirid(ri); |
| 430 | location->flags = 0; |
| 431 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 432 | location->offset = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 433 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 434 | btrfs_free_path(path); |
| 435 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 436 | return 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 439 | int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 440 | { |
| 441 | struct btrfs_iget_args *args = p; |
| 442 | inode->i_ino = args->ino; |
| 443 | BTRFS_I(inode)->root = args->root; |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | int btrfs_find_actor(struct inode *inode, void *opaque) |
| 448 | { |
| 449 | struct btrfs_iget_args *args = opaque; |
| 450 | return (args->ino == inode->i_ino && |
| 451 | args->root == BTRFS_I(inode)->root); |
| 452 | } |
| 453 | |
| 454 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 455 | struct btrfs_root *root) |
| 456 | { |
| 457 | struct inode *inode; |
| 458 | struct btrfs_iget_args args; |
| 459 | args.ino = objectid; |
| 460 | args.root = root; |
| 461 | |
| 462 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 463 | btrfs_init_locked_inode, |
| 464 | (void *)&args); |
| 465 | return inode; |
| 466 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 467 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 468 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 469 | struct nameidata *nd) |
| 470 | { |
| 471 | struct inode * inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 472 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 473 | struct btrfs_root *root = bi->root; |
| 474 | struct btrfs_root *sub_root = root; |
| 475 | struct btrfs_key location; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 476 | int ret; |
| 477 | |
| 478 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 479 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 480 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 481 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 482 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 483 | if (ret < 0) |
| 484 | return ERR_PTR(ret); |
| 485 | inode = NULL; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 486 | if (location.objectid) { |
| 487 | ret = fixup_tree_root_location(root, &location, &sub_root); |
| 488 | if (ret < 0) |
| 489 | return ERR_PTR(ret); |
| 490 | if (ret > 0) |
| 491 | return ERR_PTR(-ENOENT); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 492 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 493 | sub_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 494 | if (!inode) |
| 495 | return ERR_PTR(-EACCES); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 496 | if (inode->i_state & I_NEW) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 497 | if (sub_root != root) { |
| 498 | ret = radix_tree_insert( |
| 499 | &root->fs_info->fs_roots_radix, |
| 500 | (unsigned long)sub_root, |
| 501 | sub_root); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 502 | printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 503 | igrab(inode); |
| 504 | sub_root->inode = inode; |
| 505 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 506 | BTRFS_I(inode)->root = sub_root; |
| 507 | memcpy(&BTRFS_I(inode)->location, &location, |
| 508 | sizeof(location)); |
| 509 | btrfs_read_locked_inode(inode); |
| 510 | unlock_new_inode(inode); |
| 511 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 512 | } |
| 513 | return d_splice_alias(inode, dentry); |
| 514 | } |
| 515 | |
| 516 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 517 | { |
| 518 | struct inode *inode = filp->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 519 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 520 | struct btrfs_item *item; |
| 521 | struct btrfs_dir_item *di; |
| 522 | struct btrfs_key key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 523 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 524 | int ret; |
| 525 | u32 nritems; |
| 526 | struct btrfs_leaf *leaf; |
| 527 | int slot; |
| 528 | int advance; |
| 529 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 530 | int over = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 531 | int key_type = BTRFS_DIR_INDEX_KEY; |
| 532 | |
| 533 | /* FIXME, use a real flag for deciding about the key type */ |
| 534 | if (root->fs_info->tree_root == root) |
| 535 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 536 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 537 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 538 | key.flags = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 539 | btrfs_set_key_type(&key, key_type); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 540 | key.offset = filp->f_pos; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 541 | path = btrfs_alloc_path(); |
| 542 | btrfs_init_path(path); |
| 543 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 544 | if (ret < 0) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 545 | goto err; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 546 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 547 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 548 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 549 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 550 | slot = path->slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 551 | if (advance || slot >= nritems) { |
| 552 | if (slot >= nritems -1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 553 | ret = btrfs_next_leaf(root, path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 554 | if (ret) |
| 555 | break; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 556 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 557 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 558 | slot = path->slots[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 559 | } else { |
| 560 | slot++; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 561 | path->slots[0]++; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | advance = 1; |
| 565 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 566 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 567 | break; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 568 | if (key_type == BTRFS_DIR_INDEX_KEY && |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 569 | btrfs_disk_key_offset(&item->key) > root->highest_inode) |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 570 | break; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 571 | if (btrfs_disk_key_type(&item->key) != key_type) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 572 | continue; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 573 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 574 | continue; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 575 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 576 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 577 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 578 | over = filldir(dirent, (const char *)(di + 1), |
| 579 | btrfs_dir_name_len(di), |
| 580 | btrfs_disk_key_offset(&item->key), |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 581 | btrfs_disk_key_objectid(&di->location), d_type); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 582 | if (over) |
| 583 | goto nopos; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 584 | } |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 585 | filp->f_pos++; |
| 586 | nopos: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 587 | ret = 0; |
| 588 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 589 | btrfs_release_path(root, path); |
| 590 | btrfs_free_path(path); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 591 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | static void btrfs_put_super (struct super_block * sb) |
| 596 | { |
| 597 | struct btrfs_root *root = btrfs_sb(sb); |
| 598 | int ret; |
| 599 | |
| 600 | ret = close_ctree(root); |
| 601 | if (ret) { |
| 602 | printk("close ctree returns %d\n", ret); |
| 603 | } |
| 604 | sb->s_fs_info = NULL; |
| 605 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 606 | |
| 607 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 608 | { |
| 609 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 610 | struct dentry * root_dentry; |
| 611 | struct btrfs_super_block *disk_super; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 612 | struct btrfs_root *tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 613 | struct btrfs_inode *bi; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 614 | |
| 615 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 616 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 617 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 618 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 619 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 620 | tree_root = open_ctree(sb); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 621 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 622 | if (!tree_root) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 623 | printk("btrfs: open_ctree failed\n"); |
| 624 | return -EIO; |
| 625 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 626 | sb->s_fs_info = tree_root; |
| 627 | disk_super = tree_root->fs_info->disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 628 | printk("read in super total blocks %Lu root %Lu\n", |
| 629 | btrfs_super_total_blocks(disk_super), |
| 630 | btrfs_super_root_dir(disk_super)); |
| 631 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 632 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 633 | tree_root); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 634 | bi = BTRFS_I(inode); |
| 635 | bi->location.objectid = inode->i_ino; |
| 636 | bi->location.offset = 0; |
| 637 | bi->location.flags = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 638 | bi->root = tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 639 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 640 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 641 | if (!inode) |
| 642 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 643 | if (inode->i_state & I_NEW) { |
| 644 | btrfs_read_locked_inode(inode); |
| 645 | unlock_new_inode(inode); |
| 646 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 647 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 648 | root_dentry = d_alloc_root(inode); |
| 649 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 650 | iput(inode); |
| 651 | return -ENOMEM; |
| 652 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 653 | sb->s_root = root_dentry; |
| 654 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 658 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 659 | struct inode *inode) |
| 660 | { |
| 661 | btrfs_set_inode_uid(item, inode->i_uid); |
| 662 | btrfs_set_inode_gid(item, inode->i_gid); |
| 663 | btrfs_set_inode_size(item, inode->i_size); |
| 664 | btrfs_set_inode_mode(item, inode->i_mode); |
| 665 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 666 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 667 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 668 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 669 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 670 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 671 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 672 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 673 | btrfs_set_inode_generation(item, inode->i_generation); |
| 674 | } |
| 675 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 676 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 677 | struct btrfs_root *root, |
| 678 | struct inode *inode) |
| 679 | { |
| 680 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 681 | struct btrfs_path *path; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 682 | int ret; |
| 683 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 684 | path = btrfs_alloc_path(); |
| 685 | BUG_ON(!path); |
| 686 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 687 | ret = btrfs_lookup_inode(trans, root, path, |
| 688 | &BTRFS_I(inode)->location, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 689 | if (ret) { |
| 690 | if (ret > 0) |
| 691 | ret = -ENOENT; |
| 692 | goto failed; |
| 693 | } |
| 694 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 695 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 696 | path->slots[0], |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 697 | struct btrfs_inode_item); |
| 698 | |
| 699 | fill_inode_item(inode_item, inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 700 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 701 | ret = 0; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 702 | failed: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 703 | btrfs_release_path(root, path); |
| 704 | btrfs_free_path(path); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 705 | return ret; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 709 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 710 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 711 | struct btrfs_trans_handle *trans; |
| 712 | int ret; |
| 713 | |
| 714 | mutex_lock(&root->fs_info->fs_mutex); |
| 715 | trans = btrfs_start_transaction(root, 1); |
| 716 | ret = btrfs_update_inode(trans, root, inode); |
| 717 | if (wait) |
| 718 | btrfs_commit_transaction(trans, root); |
| 719 | else |
| 720 | btrfs_end_transaction(trans, root); |
| 721 | mutex_unlock(&root->fs_info->fs_mutex); |
| 722 | return ret; |
| 723 | } |
| 724 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 725 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 726 | struct inode *dir, int mode) |
| 727 | { |
| 728 | struct inode *inode; |
| 729 | struct btrfs_inode_item inode_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 730 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 731 | struct btrfs_key *location; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 732 | int ret; |
| 733 | u64 objectid; |
| 734 | |
| 735 | inode = new_inode(dir->i_sb); |
| 736 | if (!inode) |
| 737 | return ERR_PTR(-ENOMEM); |
| 738 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 739 | BTRFS_I(inode)->root = BTRFS_I(dir)->root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 740 | ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 741 | BUG_ON(ret); |
| 742 | |
| 743 | inode->i_uid = current->fsuid; |
| 744 | inode->i_gid = current->fsgid; |
| 745 | inode->i_mode = mode; |
| 746 | inode->i_ino = objectid; |
| 747 | inode->i_blocks = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 748 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 749 | fill_inode_item(&inode_item, inode); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 750 | location = &BTRFS_I(inode)->location; |
| 751 | location->objectid = objectid; |
| 752 | location->flags = 0; |
| 753 | location->offset = 0; |
| 754 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 755 | |
| 756 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 757 | BUG_ON(ret); |
| 758 | |
| 759 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 760 | return inode; |
| 761 | } |
| 762 | |
| 763 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 764 | struct dentry *dentry, struct inode *inode) |
| 765 | { |
| 766 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 767 | struct btrfs_key key; |
| 768 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
| 769 | key.objectid = inode->i_ino; |
| 770 | key.flags = 0; |
| 771 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 772 | key.offset = 0; |
| 773 | |
| 774 | ret = btrfs_insert_dir_item(trans, root, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 775 | dentry->d_name.name, dentry->d_name.len, |
| 776 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 777 | &key, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 778 | if (ret == 0) { |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 779 | dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 780 | ret = btrfs_update_inode(trans, root, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 781 | dentry->d_parent->d_inode); |
| 782 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 787 | struct dentry *dentry, struct inode *inode) |
| 788 | { |
| 789 | int err = btrfs_add_link(trans, dentry, inode); |
| 790 | if (!err) { |
| 791 | d_instantiate(dentry, inode); |
| 792 | return 0; |
| 793 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 794 | if (err > 0) |
| 795 | err = -EEXIST; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 796 | return err; |
| 797 | } |
| 798 | |
| 799 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 800 | int mode, struct nameidata *nd) |
| 801 | { |
| 802 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 803 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 804 | struct inode *inode; |
| 805 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 806 | int drop_inode = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 807 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 808 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 809 | trans = btrfs_start_transaction(root, 1); |
| 810 | inode = btrfs_new_inode(trans, dir, mode); |
| 811 | err = PTR_ERR(inode); |
| 812 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 813 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 814 | // FIXME mark the inode dirty |
| 815 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 816 | if (err) |
| 817 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 818 | else { |
| 819 | inode->i_mapping->a_ops = &btrfs_aops; |
| 820 | inode->i_fop = &btrfs_file_operations; |
| 821 | inode->i_op = &btrfs_file_inode_operations; |
| 822 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 823 | dir->i_sb->s_dirt = 1; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 824 | out_unlock: |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 825 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 826 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 827 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 828 | if (drop_inode) { |
| 829 | inode_dec_link_count(inode); |
| 830 | iput(inode); |
| 831 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 832 | return err; |
| 833 | } |
| 834 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 835 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 836 | struct inode *inode, struct inode *dir) |
| 837 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 838 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 839 | int ret; |
| 840 | char buf[2]; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 841 | struct btrfs_key key; |
| 842 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 843 | buf[0] = '.'; |
| 844 | buf[1] = '.'; |
| 845 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 846 | key.objectid = inode->i_ino; |
| 847 | key.offset = 0; |
| 848 | key.flags = 0; |
| 849 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 850 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 851 | ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 852 | &key, 1); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 853 | if (ret) |
| 854 | goto error; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 855 | key.objectid = dir->i_ino; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 856 | ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 857 | &key, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 858 | if (ret) |
| 859 | goto error; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 860 | inode->i_size = 6; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 861 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 862 | error: |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 867 | { |
| 868 | struct inode *inode; |
| 869 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 870 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 871 | int err = 0; |
| 872 | int drop_on_err = 0; |
| 873 | |
| 874 | mutex_lock(&root->fs_info->fs_mutex); |
| 875 | trans = btrfs_start_transaction(root, 1); |
| 876 | if (IS_ERR(trans)) { |
| 877 | err = PTR_ERR(trans); |
| 878 | goto out_unlock; |
| 879 | } |
| 880 | inode = btrfs_new_inode(trans, dir, S_IFDIR | mode); |
| 881 | if (IS_ERR(inode)) { |
| 882 | err = PTR_ERR(inode); |
| 883 | goto out_fail; |
| 884 | } |
| 885 | drop_on_err = 1; |
| 886 | inode->i_op = &btrfs_dir_inode_operations; |
| 887 | inode->i_fop = &btrfs_dir_file_operations; |
| 888 | |
| 889 | err = btrfs_make_empty_dir(trans, inode, dir); |
| 890 | if (err) |
| 891 | goto out_fail; |
| 892 | err = btrfs_add_link(trans, dentry, inode); |
| 893 | if (err) |
| 894 | goto out_fail; |
| 895 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 896 | drop_on_err = 0; |
| 897 | |
| 898 | out_fail: |
| 899 | btrfs_end_transaction(trans, root); |
| 900 | out_unlock: |
| 901 | mutex_unlock(&root->fs_info->fs_mutex); |
| 902 | if (drop_on_err) |
| 903 | iput(inode); |
| 904 | return err; |
| 905 | } |
| 906 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 907 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 908 | { |
| 909 | struct btrfs_trans_handle *trans; |
| 910 | struct btrfs_root *root; |
| 911 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 912 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 913 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 914 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 915 | if (!wait) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 916 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 917 | return 0; |
| 918 | } |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 919 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 920 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 921 | trans = btrfs_start_transaction(root, 1); |
| 922 | ret = btrfs_commit_transaction(trans, root); |
| 923 | sb->s_dirt = 0; |
| 924 | BUG_ON(ret); |
| 925 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 926 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 930 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 931 | static int btrfs_get_block_inline(struct inode *inode, sector_t iblock, |
| 932 | struct buffer_head *result, int create) |
| 933 | { |
| 934 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 935 | struct btrfs_path *path; |
| 936 | struct btrfs_key key; |
| 937 | struct btrfs_leaf *leaf; |
| 938 | int num_bytes = result->b_size; |
| 939 | int item_size; |
| 940 | int ret; |
| 941 | u64 pos; |
| 942 | char *ptr; |
| 943 | int copy_size; |
| 944 | int err = 0; |
| 945 | char *safe_ptr; |
| 946 | char *data_ptr; |
| 947 | |
| 948 | path = btrfs_alloc_path(); |
| 949 | BUG_ON(!path); |
| 950 | |
| 951 | WARN_ON(create); |
| 952 | if (create) { |
| 953 | return 0; |
| 954 | } |
| 955 | pos = iblock << inode->i_blkbits; |
| 956 | key.objectid = inode->i_ino; |
| 957 | key.flags = 0; |
| 958 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 959 | ptr = kmap(result->b_page); |
| 960 | safe_ptr = ptr; |
| 961 | ptr += (pos & (PAGE_CACHE_SIZE -1)); |
| 962 | again: |
| 963 | key.offset = pos; |
| 964 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 965 | if (ret) { |
| 966 | if (ret < 0) |
| 967 | err = ret; |
| 968 | else |
| 969 | err = 0; |
| 970 | goto out; |
| 971 | } |
| 972 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 973 | item_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 974 | copy_size = min(num_bytes, item_size); |
| 975 | data_ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
| 976 | WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size); |
| 977 | memcpy(ptr, data_ptr, copy_size); |
| 978 | pos += copy_size; |
| 979 | num_bytes -= copy_size; |
| 980 | WARN_ON(num_bytes < 0); |
| 981 | ptr += copy_size; |
| 982 | btrfs_release_path(root, path); |
| 983 | if (num_bytes != 0) { |
| 984 | if (pos >= i_size_read(inode)) |
| 985 | memset(ptr, 0, num_bytes); |
| 986 | else |
| 987 | goto again; |
| 988 | } |
| 989 | set_buffer_uptodate(result); |
| 990 | map_bh(result, inode->i_sb, 0); |
| 991 | err = 0; |
| 992 | out: |
| 993 | btrfs_free_path(path); |
| 994 | kunmap(result->b_page); |
| 995 | return err; |
| 996 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 997 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 998 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 999 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1000 | struct buffer_head *result, int create) |
| 1001 | { |
| 1002 | int ret; |
| 1003 | int err = 0; |
| 1004 | u64 blocknr; |
| 1005 | u64 extent_start = 0; |
| 1006 | u64 extent_end = 0; |
| 1007 | u64 objectid = inode->i_ino; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1008 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1009 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1010 | struct btrfs_trans_handle *trans = NULL; |
| 1011 | struct btrfs_file_extent_item *item; |
| 1012 | struct btrfs_leaf *leaf; |
| 1013 | struct btrfs_disk_key *found_key; |
| 1014 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1015 | path = btrfs_alloc_path(); |
| 1016 | BUG_ON(!path); |
| 1017 | btrfs_init_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1018 | if (create) |
| 1019 | trans = btrfs_start_transaction(root, 1); |
| 1020 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1021 | ret = btrfs_lookup_file_extent(trans, root, path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1022 | inode->i_ino, |
Chris Mason | 79b2cb1 | 2007-04-10 13:51:41 -0400 | [diff] [blame] | 1023 | iblock << inode->i_blkbits, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1024 | if (ret < 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1025 | err = ret; |
| 1026 | goto out; |
| 1027 | } |
| 1028 | |
| 1029 | if (ret != 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1030 | if (path->slots[0] == 0) { |
| 1031 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1032 | goto allocate; |
| 1033 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1034 | path->slots[0]--; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1037 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1038 | struct btrfs_file_extent_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1039 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1040 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 1041 | blocknr += btrfs_file_extent_offset(item); |
| 1042 | |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1043 | /* exact match found, use it, FIXME, deal with extents |
| 1044 | * other than the page size |
| 1045 | */ |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1046 | if (ret == 0) { |
| 1047 | err = 0; |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1048 | BUG_ON(btrfs_file_extent_disk_num_blocks(item) != 1); |
Chris Mason | 79b2cb1 | 2007-04-10 13:51:41 -0400 | [diff] [blame] | 1049 | if (create && |
| 1050 | btrfs_file_extent_generation(item) != trans->transid) { |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1051 | struct btrfs_key ins; |
| 1052 | ret = btrfs_alloc_extent(trans, root, 1, |
| 1053 | blocknr, (u64)-1, &ins); |
| 1054 | BUG_ON(ret); |
| 1055 | btrfs_set_file_extent_disk_blocknr(item, ins.objectid); |
| 1056 | mark_buffer_dirty(path->nodes[0]); |
| 1057 | ret = btrfs_free_extent(trans, root, |
| 1058 | blocknr, 1, 0); |
| 1059 | BUG_ON(ret); |
| 1060 | blocknr = ins.objectid; |
| 1061 | |
| 1062 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1063 | map_bh(result, inode->i_sb, blocknr); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1064 | goto out; |
| 1065 | } |
| 1066 | |
| 1067 | /* are we inside the extent that was found? */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1068 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1069 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 1070 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 1071 | extent_end = 0; |
| 1072 | extent_start = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1073 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1074 | goto allocate; |
| 1075 | } |
| 1076 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1077 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1078 | extent_start = extent_start >> inode->i_blkbits; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1079 | extent_start += btrfs_file_extent_offset(item); |
| 1080 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1081 | if (iblock >= extent_start && iblock < extent_end) { |
| 1082 | err = 0; |
| 1083 | map_bh(result, inode->i_sb, blocknr + iblock - extent_start); |
| 1084 | goto out; |
| 1085 | } |
| 1086 | allocate: |
| 1087 | /* ok, create a new extent */ |
| 1088 | if (!create) { |
| 1089 | err = 0; |
| 1090 | goto out; |
| 1091 | } |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1092 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
| 1093 | iblock << inode->i_blkbits, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1094 | 1, extent_end, &blocknr); |
| 1095 | if (ret) { |
| 1096 | err = ret; |
| 1097 | goto out; |
| 1098 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1099 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 1100 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1101 | map_bh(result, inode->i_sb, blocknr); |
| 1102 | |
| 1103 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1104 | btrfs_release_path(root, path); |
| 1105 | btrfs_free_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1106 | if (trans) |
| 1107 | btrfs_end_transaction(trans, root); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1108 | return err; |
| 1109 | } |
| 1110 | |
| 1111 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 1112 | struct buffer_head *result, int create) |
| 1113 | { |
| 1114 | int err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1115 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1116 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1117 | err = btrfs_get_block_lock(inode, iblock, result, create); |
| 1118 | // err = btrfs_get_block_inline(inode, iblock, result, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1119 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1120 | return err; |
| 1121 | } |
| 1122 | |
| 1123 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 1124 | unsigned from, unsigned to) |
| 1125 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1126 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1127 | } |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1128 | static int btrfs_commit_write(struct file *file, struct page *page, |
| 1129 | unsigned from, unsigned to) |
| 1130 | { |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1131 | return nobh_commit_write(file, page, from, to); |
| 1132 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1133 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1134 | static void btrfs_write_super(struct super_block *sb) |
| 1135 | { |
| 1136 | btrfs_sync_fs(sb, 1); |
| 1137 | } |
| 1138 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1139 | static int btrfs_readpage(struct file *file, struct page *page) |
| 1140 | { |
| 1141 | return mpage_readpage(page, btrfs_get_block); |
| 1142 | } |
| 1143 | |
| 1144 | static int btrfs_readpages(struct file *file, struct address_space *mapping, |
| 1145 | struct list_head *pages, unsigned nr_pages) |
| 1146 | { |
| 1147 | return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block); |
| 1148 | } |
| 1149 | |
| 1150 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 1151 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1152 | return nobh_writepage(page, btrfs_get_block, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1153 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1154 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1155 | static void btrfs_truncate(struct inode *inode) |
| 1156 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1157 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1158 | int ret; |
| 1159 | struct btrfs_trans_handle *trans; |
| 1160 | |
| 1161 | if (!S_ISREG(inode->i_mode)) |
| 1162 | return; |
| 1163 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1164 | return; |
| 1165 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1166 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1167 | |
| 1168 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 1169 | mutex_lock(&root->fs_info->fs_mutex); |
| 1170 | trans = btrfs_start_transaction(root, 1); |
| 1171 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 1172 | BUG_ON(ret); |
| 1173 | ret = btrfs_end_transaction(trans, root); |
| 1174 | BUG_ON(ret); |
| 1175 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1176 | mark_inode_dirty(inode); |
| 1177 | } |
| 1178 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1179 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 1180 | struct page **prepared_pages, |
| 1181 | const char __user * buf) |
| 1182 | { |
| 1183 | long page_fault = 0; |
| 1184 | int i; |
| 1185 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1186 | |
| 1187 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 1188 | size_t count = min_t(size_t, |
| 1189 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 1190 | struct page *page = prepared_pages[i]; |
| 1191 | fault_in_pages_readable(buf, count); |
| 1192 | |
| 1193 | /* Copy data from userspace to the current page */ |
| 1194 | kmap(page); |
| 1195 | page_fault = __copy_from_user(page_address(page) + offset, |
| 1196 | buf, count); |
| 1197 | /* Flush processor's dcache for this page */ |
| 1198 | flush_dcache_page(page); |
| 1199 | kunmap(page); |
| 1200 | buf += count; |
| 1201 | write_bytes -= count; |
| 1202 | |
| 1203 | if (page_fault) |
| 1204 | break; |
| 1205 | } |
| 1206 | return page_fault ? -EFAULT : 0; |
| 1207 | } |
| 1208 | |
| 1209 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 1210 | { |
| 1211 | size_t i; |
| 1212 | for (i = 0; i < num_pages; i++) { |
| 1213 | if (!pages[i]) |
| 1214 | break; |
| 1215 | unlock_page(pages[i]); |
| 1216 | mark_page_accessed(pages[i]); |
| 1217 | page_cache_release(pages[i]); |
| 1218 | } |
| 1219 | } |
| 1220 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 1221 | struct btrfs_root *root, |
| 1222 | struct file *file, |
| 1223 | struct page **pages, |
| 1224 | size_t num_pages, |
| 1225 | loff_t pos, |
| 1226 | size_t write_bytes) |
| 1227 | { |
| 1228 | int i; |
| 1229 | int offset; |
| 1230 | int err = 0; |
| 1231 | int ret; |
| 1232 | int this_write; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1233 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1234 | |
| 1235 | for (i = 0; i < num_pages; i++) { |
| 1236 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1237 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1238 | /* FIXME, one block at a time */ |
| 1239 | |
| 1240 | mutex_lock(&root->fs_info->fs_mutex); |
| 1241 | trans = btrfs_start_transaction(root, 1); |
| 1242 | btrfs_csum_file_block(trans, root, inode->i_ino, |
| 1243 | pages[i]->index << PAGE_CACHE_SHIFT, |
| 1244 | kmap(pages[i]), PAGE_CACHE_SIZE); |
| 1245 | kunmap(pages[i]); |
| 1246 | SetPageChecked(pages[i]); |
| 1247 | ret = btrfs_end_transaction(trans, root); |
| 1248 | BUG_ON(ret); |
| 1249 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1250 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1251 | ret = nobh_commit_write(file, pages[i], offset, |
| 1252 | offset + this_write); |
| 1253 | pos += this_write; |
| 1254 | if (ret) { |
| 1255 | err = ret; |
| 1256 | goto failed; |
| 1257 | } |
| 1258 | WARN_ON(this_write > write_bytes); |
| 1259 | write_bytes -= this_write; |
| 1260 | } |
| 1261 | failed: |
| 1262 | return err; |
| 1263 | } |
| 1264 | |
| 1265 | static int prepare_pages(struct btrfs_trans_handle *trans, |
| 1266 | struct btrfs_root *root, |
| 1267 | struct file *file, |
| 1268 | struct page **pages, |
| 1269 | size_t num_pages, |
| 1270 | loff_t pos, |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame^] | 1271 | unsigned long first_index, |
| 1272 | unsigned long last_index, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1273 | size_t write_bytes) |
| 1274 | { |
| 1275 | int i; |
| 1276 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 1277 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1278 | int offset; |
| 1279 | int err = 0; |
| 1280 | int ret; |
| 1281 | int this_write; |
| 1282 | loff_t isize = i_size_read(inode); |
| 1283 | |
| 1284 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 1285 | |
| 1286 | for (i = 0; i < num_pages; i++) { |
| 1287 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 1288 | if (!pages[i]) { |
| 1289 | err = -ENOMEM; |
| 1290 | goto failed_release; |
| 1291 | } |
| 1292 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1293 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame^] | 1294 | if (!PageUptodate(pages[i]) && |
| 1295 | (pages[i]->index == first_index || |
| 1296 | pages[i]->index == last_index) && pos < isize) { |
| 1297 | ret = mpage_readpage(pages[i], btrfs_get_block); |
| 1298 | BUG_ON(ret); |
| 1299 | lock_page(pages[i]); |
| 1300 | } |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1301 | ret = nobh_prepare_write(pages[i], offset, |
| 1302 | offset + this_write, |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1303 | btrfs_get_block); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1304 | pos += this_write; |
| 1305 | if (ret) { |
| 1306 | err = ret; |
| 1307 | goto failed_truncate; |
| 1308 | } |
| 1309 | WARN_ON(this_write > write_bytes); |
| 1310 | write_bytes -= this_write; |
| 1311 | } |
| 1312 | return 0; |
| 1313 | |
| 1314 | failed_release: |
| 1315 | btrfs_drop_pages(pages, num_pages); |
| 1316 | return err; |
| 1317 | |
| 1318 | failed_truncate: |
| 1319 | btrfs_drop_pages(pages, num_pages); |
| 1320 | if (pos > isize) |
| 1321 | vmtruncate(inode, isize); |
| 1322 | return err; |
| 1323 | } |
| 1324 | |
| 1325 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 1326 | size_t count, loff_t *ppos) |
| 1327 | { |
| 1328 | loff_t pos; |
| 1329 | size_t num_written = 0; |
| 1330 | int err = 0; |
| 1331 | int ret = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1332 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1333 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1334 | struct page *pages[1]; |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame^] | 1335 | unsigned long first_index; |
| 1336 | unsigned long last_index; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1337 | |
| 1338 | if (file->f_flags & O_DIRECT) |
| 1339 | return -EINVAL; |
| 1340 | pos = *ppos; |
| 1341 | |
| 1342 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1343 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1344 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1345 | if (err) |
| 1346 | goto out; |
| 1347 | if (count == 0) |
| 1348 | goto out; |
| 1349 | err = remove_suid(file->f_path.dentry); |
| 1350 | if (err) |
| 1351 | goto out; |
| 1352 | file_update_time(file); |
| 1353 | mutex_lock(&inode->i_mutex); |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame^] | 1354 | first_index = pos >> PAGE_CACHE_SHIFT; |
| 1355 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1356 | while(count > 0) { |
| 1357 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1358 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1359 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 1360 | PAGE_CACHE_SHIFT; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1361 | ret = prepare_pages(NULL, root, file, pages, num_pages, |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame^] | 1362 | pos, first_index, last_index, write_bytes); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1363 | BUG_ON(ret); |
| 1364 | ret = btrfs_copy_from_user(pos, num_pages, |
| 1365 | write_bytes, pages, buf); |
| 1366 | BUG_ON(ret); |
| 1367 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1368 | ret = dirty_and_release_pages(NULL, root, file, pages, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1369 | num_pages, pos, write_bytes); |
| 1370 | BUG_ON(ret); |
| 1371 | btrfs_drop_pages(pages, num_pages); |
| 1372 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1373 | buf += write_bytes; |
| 1374 | count -= write_bytes; |
| 1375 | pos += write_bytes; |
| 1376 | num_written += write_bytes; |
| 1377 | |
| 1378 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1379 | cond_resched(); |
| 1380 | } |
| 1381 | mutex_unlock(&inode->i_mutex); |
| 1382 | out: |
| 1383 | *ppos = pos; |
| 1384 | current->backing_dev_info = NULL; |
| 1385 | return num_written ? num_written : err; |
| 1386 | } |
| 1387 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1388 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1389 | static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode, |
| 1390 | struct page *page, loff_t pos, |
| 1391 | size_t offset, size_t write_bytes) |
| 1392 | { |
| 1393 | struct btrfs_path *path; |
| 1394 | struct btrfs_trans_handle *trans; |
| 1395 | struct btrfs_key key; |
| 1396 | struct btrfs_leaf *leaf; |
| 1397 | struct btrfs_key found_key; |
| 1398 | int ret; |
| 1399 | size_t copy_size = 0; |
| 1400 | char *dst = NULL; |
| 1401 | int err = 0; |
| 1402 | size_t num_written = 0; |
| 1403 | |
| 1404 | path = btrfs_alloc_path(); |
| 1405 | BUG_ON(!path); |
| 1406 | mutex_lock(&root->fs_info->fs_mutex); |
| 1407 | trans = btrfs_start_transaction(root, 1); |
| 1408 | key.objectid = inode->i_ino; |
| 1409 | key.flags = 0; |
| 1410 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 1411 | |
| 1412 | again: |
| 1413 | key.offset = pos; |
| 1414 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1415 | if (ret < 0) { |
| 1416 | err = ret; |
| 1417 | goto out; |
| 1418 | } |
| 1419 | if (ret == 0) { |
| 1420 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1421 | btrfs_disk_key_to_cpu(&found_key, |
| 1422 | &leaf->items[path->slots[0]].key); |
| 1423 | copy_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 1424 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1425 | copy_size = min(write_bytes, copy_size); |
| 1426 | goto copyit; |
| 1427 | } else { |
| 1428 | int slot = path->slots[0]; |
| 1429 | if (slot > 0) { |
| 1430 | slot--; |
| 1431 | } |
| 1432 | // FIXME find max key |
| 1433 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1434 | btrfs_disk_key_to_cpu(&found_key, |
| 1435 | &leaf->items[slot].key); |
| 1436 | if (found_key.objectid != inode->i_ino) |
| 1437 | goto insert; |
| 1438 | if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY) |
| 1439 | goto insert; |
| 1440 | copy_size = btrfs_item_size(leaf->items + slot); |
| 1441 | if (found_key.offset + copy_size <= pos) |
| 1442 | goto insert; |
| 1443 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1444 | dst += pos - found_key.offset; |
| 1445 | copy_size = copy_size - (pos - found_key.offset); |
| 1446 | BUG_ON(copy_size < 0); |
| 1447 | copy_size = min(write_bytes, copy_size); |
| 1448 | WARN_ON(copy_size == 0); |
| 1449 | goto copyit; |
| 1450 | } |
| 1451 | insert: |
| 1452 | btrfs_release_path(root, path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1453 | copy_size = min(write_bytes, |
| 1454 | (size_t)BTRFS_LEAF_DATA_SIZE(root) - |
| 1455 | sizeof(struct btrfs_item) * 4); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1456 | ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size); |
| 1457 | BUG_ON(ret); |
| 1458 | dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1459 | path->slots[0], char); |
| 1460 | copyit: |
| 1461 | WARN_ON(copy_size == 0); |
| 1462 | WARN_ON(dst + copy_size > |
| 1463 | btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1464 | path->slots[0], char) + |
| 1465 | btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items + |
| 1466 | path->slots[0])); |
| 1467 | btrfs_memcpy(root, path->nodes[0]->b_data, dst, |
| 1468 | page_address(page) + offset, copy_size); |
| 1469 | mark_buffer_dirty(path->nodes[0]); |
| 1470 | btrfs_release_path(root, path); |
| 1471 | pos += copy_size; |
| 1472 | offset += copy_size; |
| 1473 | num_written += copy_size; |
| 1474 | write_bytes -= copy_size; |
| 1475 | if (write_bytes) |
| 1476 | goto again; |
| 1477 | out: |
| 1478 | btrfs_free_path(path); |
| 1479 | ret = btrfs_end_transaction(trans, root); |
| 1480 | BUG_ON(ret); |
| 1481 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1482 | return num_written ? num_written : err; |
| 1483 | } |
| 1484 | |
| 1485 | static ssize_t btrfs_file_inline_write(struct file *file, |
| 1486 | const char __user *buf, |
| 1487 | size_t count, loff_t *ppos) |
| 1488 | { |
| 1489 | loff_t pos; |
| 1490 | size_t num_written = 0; |
| 1491 | int err = 0; |
| 1492 | int ret = 0; |
| 1493 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1494 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1495 | unsigned long page_index; |
| 1496 | |
| 1497 | if (file->f_flags & O_DIRECT) |
| 1498 | return -EINVAL; |
| 1499 | pos = *ppos; |
| 1500 | |
| 1501 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1502 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1503 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1504 | if (err) |
| 1505 | goto out; |
| 1506 | if (count == 0) |
| 1507 | goto out; |
| 1508 | err = remove_suid(file->f_path.dentry); |
| 1509 | if (err) |
| 1510 | goto out; |
| 1511 | file_update_time(file); |
| 1512 | mutex_lock(&inode->i_mutex); |
| 1513 | while(count > 0) { |
| 1514 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1515 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1516 | struct page *page; |
| 1517 | |
| 1518 | page_index = pos >> PAGE_CACHE_SHIFT; |
| 1519 | page = grab_cache_page(inode->i_mapping, page_index); |
| 1520 | if (!PageUptodate(page)) { |
| 1521 | ret = mpage_readpage(page, btrfs_get_block); |
| 1522 | BUG_ON(ret); |
| 1523 | lock_page(page); |
| 1524 | } |
| 1525 | ret = btrfs_copy_from_user(pos, 1, |
| 1526 | write_bytes, &page, buf); |
| 1527 | BUG_ON(ret); |
| 1528 | write_bytes = inline_one_page(root, inode, page, pos, |
| 1529 | offset, write_bytes); |
| 1530 | SetPageUptodate(page); |
| 1531 | if (write_bytes > 0 && pos + write_bytes > inode->i_size) { |
| 1532 | i_size_write(inode, pos + write_bytes); |
| 1533 | mark_inode_dirty(inode); |
| 1534 | } |
| 1535 | page_cache_release(page); |
| 1536 | unlock_page(page); |
| 1537 | if (write_bytes < 0) |
| 1538 | goto out_unlock; |
| 1539 | buf += write_bytes; |
| 1540 | count -= write_bytes; |
| 1541 | pos += write_bytes; |
| 1542 | num_written += write_bytes; |
| 1543 | |
| 1544 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1545 | cond_resched(); |
| 1546 | } |
| 1547 | out_unlock: |
| 1548 | mutex_unlock(&inode->i_mutex); |
| 1549 | out: |
| 1550 | *ppos = pos; |
| 1551 | current->backing_dev_info = NULL; |
| 1552 | return num_written ? num_written : err; |
| 1553 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1554 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1555 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1556 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
| 1557 | unsigned long offset, unsigned long size) |
| 1558 | { |
| 1559 | char *kaddr; |
| 1560 | unsigned long left, count = desc->count; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1561 | struct inode *inode = page->mapping->host; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1562 | |
| 1563 | if (size > count) |
| 1564 | size = count; |
| 1565 | |
| 1566 | if (!PageChecked(page)) { |
| 1567 | /* FIXME, do it per block */ |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1568 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1569 | int ret = btrfs_csum_verify_file_block(root, |
| 1570 | page->mapping->host->i_ino, |
| 1571 | page->index << PAGE_CACHE_SHIFT, |
| 1572 | kmap(page), PAGE_CACHE_SIZE); |
| 1573 | if (ret) { |
| 1574 | printk("failed to verify ino %lu page %lu\n", |
| 1575 | page->mapping->host->i_ino, |
| 1576 | page->index); |
| 1577 | memset(page_address(page), 0, PAGE_CACHE_SIZE); |
| 1578 | } |
| 1579 | SetPageChecked(page); |
| 1580 | kunmap(page); |
| 1581 | } |
| 1582 | /* |
| 1583 | * Faults on the destination of a read are common, so do it before |
| 1584 | * taking the kmap. |
| 1585 | */ |
| 1586 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 1587 | kaddr = kmap_atomic(page, KM_USER0); |
| 1588 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 1589 | kaddr + offset, size); |
| 1590 | kunmap_atomic(kaddr, KM_USER0); |
| 1591 | if (left == 0) |
| 1592 | goto success; |
| 1593 | } |
| 1594 | |
| 1595 | /* Do it the slow way */ |
| 1596 | kaddr = kmap(page); |
| 1597 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 1598 | kunmap(page); |
| 1599 | |
| 1600 | if (left) { |
| 1601 | size -= left; |
| 1602 | desc->error = -EFAULT; |
| 1603 | } |
| 1604 | success: |
| 1605 | desc->count = count - size; |
| 1606 | desc->written += size; |
| 1607 | desc->arg.buf += size; |
| 1608 | return size; |
| 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * btrfs_file_aio_read - filesystem read routine |
| 1613 | * @iocb: kernel I/O control block |
| 1614 | * @iov: io vector request |
| 1615 | * @nr_segs: number of segments in the iovec |
| 1616 | * @pos: current file position |
| 1617 | */ |
| 1618 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 1619 | unsigned long nr_segs, loff_t pos) |
| 1620 | { |
| 1621 | struct file *filp = iocb->ki_filp; |
| 1622 | ssize_t retval; |
| 1623 | unsigned long seg; |
| 1624 | size_t count; |
| 1625 | loff_t *ppos = &iocb->ki_pos; |
| 1626 | |
| 1627 | count = 0; |
| 1628 | for (seg = 0; seg < nr_segs; seg++) { |
| 1629 | const struct iovec *iv = &iov[seg]; |
| 1630 | |
| 1631 | /* |
| 1632 | * If any segment has a negative length, or the cumulative |
| 1633 | * length ever wraps negative then return -EINVAL. |
| 1634 | */ |
| 1635 | count += iv->iov_len; |
| 1636 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 1637 | return -EINVAL; |
| 1638 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 1639 | continue; |
| 1640 | if (seg == 0) |
| 1641 | return -EFAULT; |
| 1642 | nr_segs = seg; |
| 1643 | count -= iv->iov_len; /* This segment is no good */ |
| 1644 | break; |
| 1645 | } |
| 1646 | retval = 0; |
| 1647 | if (count) { |
| 1648 | for (seg = 0; seg < nr_segs; seg++) { |
| 1649 | read_descriptor_t desc; |
| 1650 | |
| 1651 | desc.written = 0; |
| 1652 | desc.arg.buf = iov[seg].iov_base; |
| 1653 | desc.count = iov[seg].iov_len; |
| 1654 | if (desc.count == 0) |
| 1655 | continue; |
| 1656 | desc.error = 0; |
| 1657 | do_generic_file_read(filp, ppos, &desc, |
| 1658 | btrfs_read_actor); |
| 1659 | retval += desc.written; |
| 1660 | if (desc.error) { |
| 1661 | retval = retval ?: desc.error; |
| 1662 | break; |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | return retval; |
| 1667 | } |
| 1668 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1669 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 1670 | { |
| 1671 | struct btrfs_trans_handle *trans; |
| 1672 | struct btrfs_key key; |
| 1673 | struct btrfs_root_item new_root_item; |
| 1674 | int ret; |
| 1675 | u64 objectid; |
| 1676 | |
| 1677 | mutex_lock(&root->fs_info->fs_mutex); |
| 1678 | trans = btrfs_start_transaction(root, 1); |
| 1679 | BUG_ON(!trans); |
| 1680 | |
| 1681 | ret = btrfs_update_inode(trans, root, root->inode); |
| 1682 | BUG_ON(ret); |
| 1683 | |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 1684 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 1685 | 0, &objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1686 | BUG_ON(ret); |
| 1687 | |
| 1688 | memset(&new_root_item, 0, sizeof(new_root_item)); |
| 1689 | memcpy(&new_root_item, &root->root_item, |
| 1690 | sizeof(new_root_item)); |
| 1691 | |
| 1692 | key.objectid = objectid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1693 | key.offset = 1; |
| 1694 | key.flags = 0; |
| 1695 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 1696 | btrfs_set_root_blocknr(&new_root_item, root->node->b_blocknr); |
| 1697 | |
| 1698 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 1699 | &new_root_item); |
| 1700 | BUG_ON(ret); |
| 1701 | |
| 1702 | printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags); |
| 1703 | |
| 1704 | /* |
| 1705 | * insert the directory item |
| 1706 | */ |
| 1707 | key.offset = (u64)-1; |
| 1708 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 1709 | name, namelen, |
| 1710 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 1711 | &key, 0); |
| 1712 | |
| 1713 | BUG_ON(ret); |
| 1714 | |
| 1715 | ret = btrfs_inc_root_ref(trans, root); |
| 1716 | BUG_ON(ret); |
| 1717 | |
| 1718 | ret = btrfs_commit_transaction(trans, root); |
| 1719 | BUG_ON(ret); |
| 1720 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
| 1724 | static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
| 1725 | cmd, unsigned long arg) |
| 1726 | { |
| 1727 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1728 | struct btrfs_ioctl_vol_args vol_args; |
| 1729 | int ret; |
| 1730 | int namelen; |
| 1731 | |
| 1732 | if (!root->ref_cows) |
| 1733 | return -EINVAL; |
| 1734 | switch (cmd) { |
| 1735 | case BTRFS_IOC_SNAP_CREATE: |
| 1736 | if (copy_from_user(&vol_args, |
| 1737 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 1738 | sizeof(vol_args))) |
| 1739 | return -EFAULT; |
| 1740 | namelen = strlen(vol_args.name); |
| 1741 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 1742 | return -EINVAL; |
| 1743 | ret = create_snapshot(root, vol_args.name, namelen); |
| 1744 | WARN_ON(ret); |
| 1745 | break; |
| 1746 | default: |
| 1747 | return -ENOTTY; |
| 1748 | } |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1752 | static struct kmem_cache *btrfs_inode_cachep; |
| 1753 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 1754 | struct kmem_cache *btrfs_transaction_cachep; |
| 1755 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 1756 | struct kmem_cache *btrfs_path_cachep; |
| 1757 | |
| 1758 | /* |
| 1759 | * Called inside transaction, so use GFP_NOFS |
| 1760 | */ |
| 1761 | static struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 1762 | { |
| 1763 | struct btrfs_inode *ei; |
| 1764 | |
| 1765 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 1766 | if (!ei) |
| 1767 | return NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1768 | return &ei->vfs_inode; |
| 1769 | } |
| 1770 | |
| 1771 | static void btrfs_destroy_inode(struct inode *inode) |
| 1772 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1773 | WARN_ON(!list_empty(&inode->i_dentry)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1774 | WARN_ON(inode->i_data.nrpages); |
| 1775 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1776 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 1777 | } |
| 1778 | |
| 1779 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 1780 | unsigned long flags) |
| 1781 | { |
| 1782 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 1783 | |
| 1784 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 1785 | SLAB_CTOR_CONSTRUCTOR) { |
| 1786 | inode_init_once(&ei->vfs_inode); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | static int init_inodecache(void) |
| 1791 | { |
| 1792 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 1793 | sizeof(struct btrfs_inode), |
| 1794 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1795 | SLAB_MEM_SPREAD), |
| 1796 | init_once, NULL); |
| 1797 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 1798 | sizeof(struct btrfs_trans_handle), |
| 1799 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1800 | SLAB_MEM_SPREAD), |
| 1801 | NULL, NULL); |
| 1802 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 1803 | sizeof(struct btrfs_transaction), |
| 1804 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1805 | SLAB_MEM_SPREAD), |
| 1806 | NULL, NULL); |
| 1807 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 1808 | sizeof(struct btrfs_transaction), |
| 1809 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1810 | SLAB_MEM_SPREAD), |
| 1811 | NULL, NULL); |
| 1812 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", |
| 1813 | 256, |
| 1814 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1815 | SLAB_MEM_SPREAD | |
| 1816 | SLAB_DESTROY_BY_RCU), |
| 1817 | NULL, NULL); |
| 1818 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || |
| 1819 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) |
| 1820 | return -ENOMEM; |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | static void destroy_inodecache(void) |
| 1825 | { |
| 1826 | kmem_cache_destroy(btrfs_inode_cachep); |
| 1827 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 1828 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 1829 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 1830 | kmem_cache_destroy(btrfs_path_cachep); |
| 1831 | } |
| 1832 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1833 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 1834 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 1835 | { |
| 1836 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 1837 | btrfs_fill_super, mnt); |
| 1838 | } |
| 1839 | |
| 1840 | static struct file_system_type btrfs_fs_type = { |
| 1841 | .owner = THIS_MODULE, |
| 1842 | .name = "btrfs", |
| 1843 | .get_sb = btrfs_get_sb, |
| 1844 | .kill_sb = kill_block_super, |
| 1845 | .fs_flags = FS_REQUIRES_DEV, |
| 1846 | }; |
| 1847 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1848 | static struct super_operations btrfs_super_ops = { |
| 1849 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 1850 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1851 | .put_super = btrfs_put_super, |
| 1852 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1853 | .write_super = btrfs_write_super, |
| 1854 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 1855 | .write_inode = btrfs_write_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1856 | .alloc_inode = btrfs_alloc_inode, |
| 1857 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1858 | }; |
| 1859 | |
| 1860 | static struct inode_operations btrfs_dir_inode_operations = { |
| 1861 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1862 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 1863 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 1864 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 1865 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1866 | }; |
| 1867 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1868 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 1869 | .lookup = btrfs_lookup, |
| 1870 | }; |
| 1871 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1872 | static struct file_operations btrfs_dir_file_operations = { |
| 1873 | .llseek = generic_file_llseek, |
| 1874 | .read = generic_read_dir, |
| 1875 | .readdir = btrfs_readdir, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1876 | .ioctl = btrfs_ioctl, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1877 | }; |
| 1878 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1879 | static struct address_space_operations btrfs_aops = { |
| 1880 | .readpage = btrfs_readpage, |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1881 | .readpages = btrfs_readpages, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1882 | .writepage = btrfs_writepage, |
| 1883 | .sync_page = block_sync_page, |
| 1884 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1885 | .commit_write = btrfs_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1886 | }; |
| 1887 | |
| 1888 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1889 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1890 | }; |
| 1891 | |
| 1892 | static struct file_operations btrfs_file_operations = { |
| 1893 | .llseek = generic_file_llseek, |
| 1894 | .read = do_sync_read, |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1895 | .aio_read = btrfs_file_aio_read, |
| 1896 | .write = btrfs_file_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1897 | .mmap = generic_file_mmap, |
| 1898 | .open = generic_file_open, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1899 | .ioctl = btrfs_ioctl, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1900 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1901 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1902 | static int __init init_btrfs_fs(void) |
| 1903 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1904 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1905 | printk("btrfs loaded!\n"); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1906 | err = init_inodecache(); |
| 1907 | if (err) |
| 1908 | return err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1909 | kset_set_kset_s(&btrfs_subsys, fs_subsys); |
| 1910 | err = subsystem_register(&btrfs_subsys); |
| 1911 | if (err) |
| 1912 | goto out; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1913 | return register_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1914 | out: |
| 1915 | destroy_inodecache(); |
| 1916 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | static void __exit exit_btrfs_fs(void) |
| 1920 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1921 | destroy_inodecache(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1922 | unregister_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1923 | subsystem_unregister(&btrfs_subsys); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1924 | printk("btrfs unloaded\n"); |
| 1925 | } |
| 1926 | |
| 1927 | module_init(init_btrfs_fs) |
| 1928 | module_exit(exit_btrfs_fs) |
| 1929 | |
| 1930 | MODULE_LICENSE("GPL"); |