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); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 153 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 154 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 155 | btrfs_release_path(root, path); |
| 156 | btrfs_free_path(path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 157 | if (ret == 0) { |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 158 | inode_dec_link_count(dentry->d_inode); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 159 | dir->i_size -= name_len * 2; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 160 | mark_inode_dirty(dir); |
| 161 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 165 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 166 | { |
| 167 | struct btrfs_root *root; |
| 168 | struct btrfs_trans_handle *trans; |
| 169 | int ret; |
| 170 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 171 | root = BTRFS_I(dir)->root; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 172 | mutex_lock(&root->fs_info->fs_mutex); |
| 173 | trans = btrfs_start_transaction(root, 1); |
| 174 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 175 | btrfs_end_transaction(trans, root); |
| 176 | mutex_unlock(&root->fs_info->fs_mutex); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 181 | { |
| 182 | struct inode *inode = dentry->d_inode; |
| 183 | int err; |
| 184 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 185 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 186 | struct btrfs_path *path; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 187 | struct btrfs_key key; |
| 188 | struct btrfs_trans_handle *trans; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 189 | struct btrfs_key found_key; |
| 190 | int found_type; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 191 | struct btrfs_leaf *leaf; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 192 | char *goodnames = ".."; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 193 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 194 | path = btrfs_alloc_path(); |
| 195 | BUG_ON(!path); |
| 196 | btrfs_init_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 197 | mutex_lock(&root->fs_info->fs_mutex); |
| 198 | trans = btrfs_start_transaction(root, 1); |
| 199 | key.objectid = inode->i_ino; |
| 200 | key.offset = (u64)-1; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 201 | key.flags = (u32)-1; |
| 202 | while(1) { |
| 203 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 204 | if (ret < 0) { |
| 205 | err = ret; |
| 206 | goto out; |
| 207 | } |
| 208 | BUG_ON(ret == 0); |
| 209 | if (path->slots[0] == 0) { |
| 210 | err = -ENOENT; |
| 211 | goto out; |
| 212 | } |
| 213 | path->slots[0]--; |
| 214 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 215 | btrfs_disk_key_to_cpu(&found_key, |
| 216 | &leaf->items[path->slots[0]].key); |
| 217 | found_type = btrfs_key_type(&found_key); |
| 218 | if (found_key.objectid != inode->i_ino) { |
| 219 | err = -ENOENT; |
| 220 | goto out; |
| 221 | } |
| 222 | if ((found_type != BTRFS_DIR_ITEM_KEY && |
| 223 | found_type != BTRFS_DIR_INDEX_KEY) || |
| 224 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && |
| 225 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { |
| 226 | err = -ENOTEMPTY; |
| 227 | goto out; |
| 228 | } |
| 229 | ret = btrfs_del_item(trans, root, path); |
| 230 | BUG_ON(ret); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 231 | |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 232 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
| 233 | break; |
| 234 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 235 | } |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 236 | ret = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 237 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 238 | |
| 239 | /* now the directory is empty */ |
| 240 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 241 | if (!err) { |
| 242 | inode->i_size = 0; |
| 243 | } |
| 244 | out: |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 245 | btrfs_release_path(root, path); |
| 246 | btrfs_free_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 247 | mutex_unlock(&root->fs_info->fs_mutex); |
| 248 | ret = btrfs_end_transaction(trans, root); |
| 249 | if (ret && !err) |
| 250 | err = ret; |
| 251 | return err; |
| 252 | } |
| 253 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 254 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 255 | struct btrfs_root *root, |
| 256 | struct inode *inode) |
| 257 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 258 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 259 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 260 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 261 | clear_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 262 | |
| 263 | path = btrfs_alloc_path(); |
| 264 | BUG_ON(!path); |
| 265 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 266 | ret = btrfs_lookup_inode(trans, root, path, |
| 267 | &BTRFS_I(inode)->location, -1); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 268 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 269 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 270 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 271 | btrfs_free_path(path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 272 | return ret; |
| 273 | } |
| 274 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 275 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 276 | struct btrfs_root *root, |
| 277 | struct inode *inode) |
| 278 | { |
| 279 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 280 | struct btrfs_path *path; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 281 | struct btrfs_key key; |
| 282 | struct btrfs_disk_key *found_key; |
| 283 | struct btrfs_leaf *leaf; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 284 | struct btrfs_file_extent_item *fi = NULL; |
| 285 | u64 extent_start = 0; |
| 286 | u64 extent_num_blocks = 0; |
| 287 | int found_extent; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 288 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 289 | path = btrfs_alloc_path(); |
| 290 | BUG_ON(!path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 291 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 292 | key.objectid = inode->i_ino; |
| 293 | key.offset = (u64)-1; |
| 294 | key.flags = 0; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 295 | /* |
| 296 | * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys |
| 297 | * or extent data |
| 298 | */ |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 299 | btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 300 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 301 | btrfs_init_path(path); |
| 302 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 303 | if (ret < 0) { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 304 | goto error; |
| 305 | } |
| 306 | if (ret > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 307 | BUG_ON(path->slots[0] == 0); |
| 308 | path->slots[0]--; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 309 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 310 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 311 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 312 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 313 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 314 | if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY && |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 315 | btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY && |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 316 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 317 | break; |
| 318 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 319 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 320 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 321 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 322 | path->slots[0], |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 323 | struct btrfs_file_extent_item); |
| 324 | extent_start = btrfs_file_extent_disk_blocknr(fi); |
| 325 | extent_num_blocks = |
| 326 | btrfs_file_extent_disk_num_blocks(fi); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 327 | /* FIXME blocksize != 4096 */ |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 328 | inode->i_blocks -= |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 329 | btrfs_file_extent_num_blocks(fi) << 3; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 330 | found_extent = 1; |
| 331 | } else { |
| 332 | found_extent = 0; |
| 333 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 334 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 335 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 336 | btrfs_release_path(root, path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 337 | if (found_extent) { |
| 338 | ret = btrfs_free_extent(trans, root, extent_start, |
| 339 | extent_num_blocks, 0); |
| 340 | BUG_ON(ret); |
| 341 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 342 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 343 | ret = 0; |
| 344 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 345 | btrfs_release_path(root, path); |
| 346 | btrfs_free_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 350 | static void btrfs_delete_inode(struct inode *inode) |
| 351 | { |
| 352 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 353 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 354 | int ret; |
| 355 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 356 | truncate_inode_pages(&inode->i_data, 0); |
| 357 | if (is_bad_inode(inode)) { |
| 358 | goto no_delete; |
| 359 | } |
| 360 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 361 | mutex_lock(&root->fs_info->fs_mutex); |
| 362 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 363 | if (S_ISREG(inode->i_mode)) { |
| 364 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 365 | BUG_ON(ret); |
| 366 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 367 | btrfs_free_inode(trans, root, inode); |
| 368 | btrfs_end_transaction(trans, root); |
| 369 | mutex_unlock(&root->fs_info->fs_mutex); |
| 370 | return; |
| 371 | no_delete: |
| 372 | clear_inode(inode); |
| 373 | } |
| 374 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 375 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 376 | struct btrfs_key *location) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 377 | { |
| 378 | const char *name = dentry->d_name.name; |
| 379 | int namelen = dentry->d_name.len; |
| 380 | struct btrfs_dir_item *di; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 381 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 382 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 383 | int ret; |
| 384 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 385 | path = btrfs_alloc_path(); |
| 386 | BUG_ON(!path); |
| 387 | btrfs_init_path(path); |
| 388 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 389 | namelen, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 390 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 391 | location->objectid = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 392 | ret = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 393 | goto out; |
| 394 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 395 | 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] | 396 | struct btrfs_dir_item); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 397 | btrfs_disk_key_to_cpu(location, &di->location); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 398 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 399 | btrfs_release_path(root, path); |
| 400 | btrfs_free_path(path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 404 | int fixup_tree_root_location(struct btrfs_root *root, |
| 405 | struct btrfs_key *location, |
| 406 | struct btrfs_root **sub_root) |
| 407 | { |
| 408 | struct btrfs_path *path; |
| 409 | struct btrfs_root_item *ri; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 410 | |
| 411 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 412 | return 0; |
| 413 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 414 | return 0; |
| 415 | |
| 416 | path = btrfs_alloc_path(); |
| 417 | BUG_ON(!path); |
| 418 | mutex_lock(&root->fs_info->fs_mutex); |
| 419 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 420 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
| 421 | if (IS_ERR(*sub_root)) |
| 422 | return PTR_ERR(*sub_root); |
| 423 | |
| 424 | ri = &(*sub_root)->root_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 425 | location->objectid = btrfs_root_dirid(ri); |
| 426 | location->flags = 0; |
| 427 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 428 | location->offset = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 429 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 430 | btrfs_free_path(path); |
| 431 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 432 | return 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 435 | int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 436 | { |
| 437 | struct btrfs_iget_args *args = p; |
| 438 | inode->i_ino = args->ino; |
| 439 | BTRFS_I(inode)->root = args->root; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | int btrfs_find_actor(struct inode *inode, void *opaque) |
| 444 | { |
| 445 | struct btrfs_iget_args *args = opaque; |
| 446 | return (args->ino == inode->i_ino && |
| 447 | args->root == BTRFS_I(inode)->root); |
| 448 | } |
| 449 | |
| 450 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 451 | struct btrfs_root *root) |
| 452 | { |
| 453 | struct inode *inode; |
| 454 | struct btrfs_iget_args args; |
| 455 | args.ino = objectid; |
| 456 | args.root = root; |
| 457 | |
| 458 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 459 | btrfs_init_locked_inode, |
| 460 | (void *)&args); |
| 461 | return inode; |
| 462 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 463 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 464 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 465 | struct nameidata *nd) |
| 466 | { |
| 467 | struct inode * inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 468 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 469 | struct btrfs_root *root = bi->root; |
| 470 | struct btrfs_root *sub_root = root; |
| 471 | struct btrfs_key location; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 472 | int ret; |
| 473 | |
| 474 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 475 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 476 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 477 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 478 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 479 | if (ret < 0) |
| 480 | return ERR_PTR(ret); |
| 481 | inode = NULL; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 482 | if (location.objectid) { |
| 483 | ret = fixup_tree_root_location(root, &location, &sub_root); |
| 484 | if (ret < 0) |
| 485 | return ERR_PTR(ret); |
| 486 | if (ret > 0) |
| 487 | return ERR_PTR(-ENOENT); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 488 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 489 | sub_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 490 | if (!inode) |
| 491 | return ERR_PTR(-EACCES); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 492 | if (inode->i_state & I_NEW) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 493 | if (sub_root != root) { |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 494 | 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] | 495 | igrab(inode); |
| 496 | sub_root->inode = inode; |
| 497 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 498 | BTRFS_I(inode)->root = sub_root; |
| 499 | memcpy(&BTRFS_I(inode)->location, &location, |
| 500 | sizeof(location)); |
| 501 | btrfs_read_locked_inode(inode); |
| 502 | unlock_new_inode(inode); |
| 503 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 504 | } |
| 505 | return d_splice_alias(inode, dentry); |
| 506 | } |
| 507 | |
| 508 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 509 | { |
| 510 | struct inode *inode = filp->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 511 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 512 | struct btrfs_item *item; |
| 513 | struct btrfs_dir_item *di; |
| 514 | struct btrfs_key key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 515 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 516 | int ret; |
| 517 | u32 nritems; |
| 518 | struct btrfs_leaf *leaf; |
| 519 | int slot; |
| 520 | int advance; |
| 521 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 522 | int over = 0; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 523 | int key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 524 | |
| 525 | /* FIXME, use a real flag for deciding about the key type */ |
| 526 | if (root->fs_info->tree_root == root) |
| 527 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 528 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 529 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 530 | key.flags = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 531 | btrfs_set_key_type(&key, key_type); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 532 | key.offset = filp->f_pos; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 533 | path = btrfs_alloc_path(); |
| 534 | btrfs_init_path(path); |
| 535 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 536 | if (ret < 0) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 537 | goto err; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 538 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 539 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 540 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 541 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 542 | slot = path->slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 543 | if (advance || slot >= nritems) { |
| 544 | if (slot >= nritems -1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 545 | ret = btrfs_next_leaf(root, path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 546 | if (ret) |
| 547 | break; |
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 | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 551 | } else { |
| 552 | slot++; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 553 | path->slots[0]++; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | advance = 1; |
| 557 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 558 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 559 | break; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 560 | if (btrfs_disk_key_type(&item->key) != key_type) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 561 | break; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 562 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 563 | continue; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 564 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 565 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 566 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 567 | over = filldir(dirent, (const char *)(di + 1), |
| 568 | btrfs_dir_name_len(di), |
| 569 | btrfs_disk_key_offset(&item->key), |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 570 | btrfs_disk_key_objectid(&di->location), d_type); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 571 | if (over) |
| 572 | goto nopos; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 573 | } |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 574 | filp->f_pos++; |
| 575 | nopos: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 576 | ret = 0; |
| 577 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 578 | btrfs_release_path(root, path); |
| 579 | btrfs_free_path(path); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 580 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static void btrfs_put_super (struct super_block * sb) |
| 585 | { |
| 586 | struct btrfs_root *root = btrfs_sb(sb); |
| 587 | int ret; |
| 588 | |
| 589 | ret = close_ctree(root); |
| 590 | if (ret) { |
| 591 | printk("close ctree returns %d\n", ret); |
| 592 | } |
| 593 | sb->s_fs_info = NULL; |
| 594 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 595 | |
| 596 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 597 | { |
| 598 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 599 | struct dentry * root_dentry; |
| 600 | struct btrfs_super_block *disk_super; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 601 | struct btrfs_root *tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 602 | struct btrfs_inode *bi; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 603 | |
| 604 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 605 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 606 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 607 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 608 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 609 | tree_root = open_ctree(sb); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 610 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 611 | if (!tree_root) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 612 | printk("btrfs: open_ctree failed\n"); |
| 613 | return -EIO; |
| 614 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 615 | sb->s_fs_info = tree_root; |
| 616 | disk_super = tree_root->fs_info->disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 617 | printk("read in super total blocks %Lu root %Lu\n", |
| 618 | btrfs_super_total_blocks(disk_super), |
| 619 | btrfs_super_root_dir(disk_super)); |
| 620 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 621 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 622 | tree_root); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 623 | bi = BTRFS_I(inode); |
| 624 | bi->location.objectid = inode->i_ino; |
| 625 | bi->location.offset = 0; |
| 626 | bi->location.flags = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 627 | bi->root = tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 628 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 629 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 630 | if (!inode) |
| 631 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 632 | if (inode->i_state & I_NEW) { |
| 633 | btrfs_read_locked_inode(inode); |
| 634 | unlock_new_inode(inode); |
| 635 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 636 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 637 | root_dentry = d_alloc_root(inode); |
| 638 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 639 | iput(inode); |
| 640 | return -ENOMEM; |
| 641 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 642 | sb->s_root = root_dentry; |
| 643 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 647 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 648 | struct inode *inode) |
| 649 | { |
| 650 | btrfs_set_inode_uid(item, inode->i_uid); |
| 651 | btrfs_set_inode_gid(item, inode->i_gid); |
| 652 | btrfs_set_inode_size(item, inode->i_size); |
| 653 | btrfs_set_inode_mode(item, inode->i_mode); |
| 654 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 655 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 656 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 657 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 658 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 659 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 660 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 661 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 662 | btrfs_set_inode_generation(item, inode->i_generation); |
| 663 | } |
| 664 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 665 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 666 | struct btrfs_root *root, |
| 667 | struct inode *inode) |
| 668 | { |
| 669 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 670 | struct btrfs_path *path; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 671 | int ret; |
| 672 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 673 | path = btrfs_alloc_path(); |
| 674 | BUG_ON(!path); |
| 675 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 676 | ret = btrfs_lookup_inode(trans, root, path, |
| 677 | &BTRFS_I(inode)->location, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 678 | if (ret) { |
| 679 | if (ret > 0) |
| 680 | ret = -ENOENT; |
| 681 | goto failed; |
| 682 | } |
| 683 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 684 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 685 | path->slots[0], |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 686 | struct btrfs_inode_item); |
| 687 | |
| 688 | fill_inode_item(inode_item, inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 689 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 690 | ret = 0; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 691 | failed: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 692 | btrfs_release_path(root, path); |
| 693 | btrfs_free_path(path); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 694 | return ret; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 698 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 699 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 700 | struct btrfs_trans_handle *trans; |
| 701 | int ret; |
| 702 | |
| 703 | mutex_lock(&root->fs_info->fs_mutex); |
| 704 | trans = btrfs_start_transaction(root, 1); |
| 705 | ret = btrfs_update_inode(trans, root, inode); |
| 706 | if (wait) |
| 707 | btrfs_commit_transaction(trans, root); |
| 708 | else |
| 709 | btrfs_end_transaction(trans, root); |
| 710 | mutex_unlock(&root->fs_info->fs_mutex); |
| 711 | return ret; |
| 712 | } |
| 713 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 714 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 715 | struct btrfs_root *root, |
| 716 | u64 objectid, int mode) |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 717 | { |
| 718 | struct inode *inode; |
| 719 | struct btrfs_inode_item inode_item; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 720 | struct btrfs_key *location; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 721 | int ret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 722 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 723 | inode = new_inode(root->fs_info->sb); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 724 | if (!inode) |
| 725 | return ERR_PTR(-ENOMEM); |
| 726 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 727 | BTRFS_I(inode)->root = root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 728 | |
| 729 | inode->i_uid = current->fsuid; |
| 730 | inode->i_gid = current->fsgid; |
| 731 | inode->i_mode = mode; |
| 732 | inode->i_ino = objectid; |
| 733 | inode->i_blocks = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 734 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 735 | fill_inode_item(&inode_item, inode); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 736 | location = &BTRFS_I(inode)->location; |
| 737 | location->objectid = objectid; |
| 738 | location->flags = 0; |
| 739 | location->offset = 0; |
| 740 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 741 | |
| 742 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 743 | BUG_ON(ret); |
| 744 | |
| 745 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 746 | return inode; |
| 747 | } |
| 748 | |
| 749 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 750 | struct dentry *dentry, struct inode *inode) |
| 751 | { |
| 752 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 753 | struct btrfs_key key; |
| 754 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
| 755 | key.objectid = inode->i_ino; |
| 756 | key.flags = 0; |
| 757 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 758 | key.offset = 0; |
| 759 | |
| 760 | ret = btrfs_insert_dir_item(trans, root, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 761 | dentry->d_name.name, dentry->d_name.len, |
| 762 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 763 | &key, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 764 | if (ret == 0) { |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 765 | dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 766 | ret = btrfs_update_inode(trans, root, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 767 | dentry->d_parent->d_inode); |
| 768 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 773 | struct dentry *dentry, struct inode *inode) |
| 774 | { |
| 775 | int err = btrfs_add_link(trans, dentry, inode); |
| 776 | if (!err) { |
| 777 | d_instantiate(dentry, inode); |
| 778 | return 0; |
| 779 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 780 | if (err > 0) |
| 781 | err = -EEXIST; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 782 | return err; |
| 783 | } |
| 784 | |
| 785 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 786 | int mode, struct nameidata *nd) |
| 787 | { |
| 788 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 789 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 790 | struct inode *inode; |
| 791 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 792 | int drop_inode = 0; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 793 | u64 objectid; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 794 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 795 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 796 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 797 | |
| 798 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 799 | if (err) { |
| 800 | err = -ENOSPC; |
| 801 | goto out_unlock; |
| 802 | } |
| 803 | |
| 804 | inode = btrfs_new_inode(trans, root, objectid, mode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 805 | err = PTR_ERR(inode); |
| 806 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 807 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 808 | // FIXME mark the inode dirty |
| 809 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 810 | if (err) |
| 811 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 812 | else { |
| 813 | inode->i_mapping->a_ops = &btrfs_aops; |
| 814 | inode->i_fop = &btrfs_file_operations; |
| 815 | inode->i_op = &btrfs_file_inode_operations; |
| 816 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 817 | dir->i_sb->s_dirt = 1; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 818 | out_unlock: |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 819 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 820 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 821 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 822 | if (drop_inode) { |
| 823 | inode_dec_link_count(inode); |
| 824 | iput(inode); |
| 825 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 826 | return err; |
| 827 | } |
| 828 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 829 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 830 | struct btrfs_root *root, |
| 831 | u64 objectid, u64 dirid) |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 832 | { |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 833 | int ret; |
| 834 | char buf[2]; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 835 | struct btrfs_key key; |
| 836 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 837 | buf[0] = '.'; |
| 838 | buf[1] = '.'; |
| 839 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 840 | key.objectid = objectid; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 841 | key.offset = 0; |
| 842 | key.flags = 0; |
| 843 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 844 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 845 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 846 | &key, 1); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 847 | if (ret) |
| 848 | goto error; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 849 | key.objectid = dirid; |
| 850 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 851 | &key, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 852 | if (ret) |
| 853 | goto error; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 854 | error: |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 859 | { |
| 860 | struct inode *inode; |
| 861 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 862 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 863 | int err = 0; |
| 864 | int drop_on_err = 0; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 865 | u64 objectid; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 866 | |
| 867 | mutex_lock(&root->fs_info->fs_mutex); |
| 868 | trans = btrfs_start_transaction(root, 1); |
| 869 | if (IS_ERR(trans)) { |
| 870 | err = PTR_ERR(trans); |
| 871 | goto out_unlock; |
| 872 | } |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 873 | |
| 874 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 875 | if (err) { |
| 876 | err = -ENOSPC; |
| 877 | goto out_unlock; |
| 878 | } |
| 879 | |
| 880 | inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 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 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 889 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
| 890 | if (err) |
| 891 | goto out_fail; |
| 892 | |
| 893 | inode->i_size = 6; |
| 894 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 895 | if (err) |
| 896 | goto out_fail; |
| 897 | err = btrfs_add_link(trans, dentry, inode); |
| 898 | if (err) |
| 899 | goto out_fail; |
| 900 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 901 | drop_on_err = 0; |
| 902 | |
| 903 | out_fail: |
| 904 | btrfs_end_transaction(trans, root); |
| 905 | out_unlock: |
| 906 | mutex_unlock(&root->fs_info->fs_mutex); |
| 907 | if (drop_on_err) |
| 908 | iput(inode); |
| 909 | return err; |
| 910 | } |
| 911 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 912 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 913 | { |
| 914 | struct btrfs_trans_handle *trans; |
| 915 | struct btrfs_root *root; |
| 916 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 917 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 918 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 919 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 920 | if (!wait) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 921 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 922 | return 0; |
| 923 | } |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 924 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 925 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 926 | trans = btrfs_start_transaction(root, 1); |
| 927 | ret = btrfs_commit_transaction(trans, root); |
| 928 | sb->s_dirt = 0; |
| 929 | BUG_ON(ret); |
| 930 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 931 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 932 | return 0; |
| 933 | } |
| 934 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 935 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 936 | static int btrfs_get_block_inline(struct inode *inode, sector_t iblock, |
| 937 | struct buffer_head *result, int create) |
| 938 | { |
| 939 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 940 | struct btrfs_path *path; |
| 941 | struct btrfs_key key; |
| 942 | struct btrfs_leaf *leaf; |
| 943 | int num_bytes = result->b_size; |
| 944 | int item_size; |
| 945 | int ret; |
| 946 | u64 pos; |
| 947 | char *ptr; |
| 948 | int copy_size; |
| 949 | int err = 0; |
| 950 | char *safe_ptr; |
| 951 | char *data_ptr; |
| 952 | |
| 953 | path = btrfs_alloc_path(); |
| 954 | BUG_ON(!path); |
| 955 | |
| 956 | WARN_ON(create); |
| 957 | if (create) { |
| 958 | return 0; |
| 959 | } |
| 960 | pos = iblock << inode->i_blkbits; |
| 961 | key.objectid = inode->i_ino; |
| 962 | key.flags = 0; |
| 963 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 964 | ptr = kmap(result->b_page); |
| 965 | safe_ptr = ptr; |
| 966 | ptr += (pos & (PAGE_CACHE_SIZE -1)); |
| 967 | again: |
| 968 | key.offset = pos; |
| 969 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 970 | if (ret) { |
| 971 | if (ret < 0) |
| 972 | err = ret; |
| 973 | else |
| 974 | err = 0; |
| 975 | goto out; |
| 976 | } |
| 977 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 978 | item_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 979 | copy_size = min(num_bytes, item_size); |
| 980 | data_ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
| 981 | WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size); |
| 982 | memcpy(ptr, data_ptr, copy_size); |
| 983 | pos += copy_size; |
| 984 | num_bytes -= copy_size; |
| 985 | WARN_ON(num_bytes < 0); |
| 986 | ptr += copy_size; |
| 987 | btrfs_release_path(root, path); |
| 988 | if (num_bytes != 0) { |
| 989 | if (pos >= i_size_read(inode)) |
| 990 | memset(ptr, 0, num_bytes); |
| 991 | else |
| 992 | goto again; |
| 993 | } |
| 994 | set_buffer_uptodate(result); |
| 995 | map_bh(result, inode->i_sb, 0); |
| 996 | err = 0; |
| 997 | out: |
| 998 | btrfs_free_path(path); |
| 999 | kunmap(result->b_page); |
| 1000 | return err; |
| 1001 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1002 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1003 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1004 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1005 | struct buffer_head *result, int create) |
| 1006 | { |
| 1007 | int ret; |
| 1008 | int err = 0; |
| 1009 | u64 blocknr; |
| 1010 | u64 extent_start = 0; |
| 1011 | u64 extent_end = 0; |
| 1012 | u64 objectid = inode->i_ino; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1013 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1014 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1015 | struct btrfs_trans_handle *trans = NULL; |
| 1016 | struct btrfs_file_extent_item *item; |
| 1017 | struct btrfs_leaf *leaf; |
| 1018 | struct btrfs_disk_key *found_key; |
| 1019 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1020 | path = btrfs_alloc_path(); |
| 1021 | BUG_ON(!path); |
| 1022 | btrfs_init_path(path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1023 | if (create) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1024 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1025 | WARN_ON(1); |
| 1026 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1027 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1028 | ret = btrfs_lookup_file_extent(trans, root, path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1029 | inode->i_ino, |
Chris Mason | 79b2cb1 | 2007-04-10 13:51:41 -0400 | [diff] [blame] | 1030 | iblock << inode->i_blkbits, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1031 | if (ret < 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1032 | err = ret; |
| 1033 | goto out; |
| 1034 | } |
| 1035 | |
| 1036 | if (ret != 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1037 | if (path->slots[0] == 0) { |
| 1038 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1039 | goto allocate; |
| 1040 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1041 | path->slots[0]--; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1042 | } |
| 1043 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1044 | 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] | 1045 | struct btrfs_file_extent_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1046 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1047 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 1048 | blocknr += btrfs_file_extent_offset(item); |
| 1049 | |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1050 | /* exact match found, use it, FIXME, deal with extents |
| 1051 | * other than the page size |
| 1052 | */ |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1053 | if (0 && ret == 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1054 | err = 0; |
Chris Mason | 79b2cb1 | 2007-04-10 13:51:41 -0400 | [diff] [blame] | 1055 | if (create && |
| 1056 | btrfs_file_extent_generation(item) != trans->transid) { |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1057 | struct btrfs_key ins; |
| 1058 | ret = btrfs_alloc_extent(trans, root, 1, |
| 1059 | blocknr, (u64)-1, &ins); |
| 1060 | BUG_ON(ret); |
| 1061 | btrfs_set_file_extent_disk_blocknr(item, ins.objectid); |
| 1062 | mark_buffer_dirty(path->nodes[0]); |
| 1063 | ret = btrfs_free_extent(trans, root, |
| 1064 | blocknr, 1, 0); |
| 1065 | BUG_ON(ret); |
| 1066 | blocknr = ins.objectid; |
| 1067 | |
| 1068 | } |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1069 | btrfs_map_bh_to_logical(root, result, blocknr); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1070 | goto out; |
| 1071 | } |
| 1072 | |
| 1073 | /* are we inside the extent that was found? */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1074 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1075 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 1076 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 1077 | extent_end = 0; |
| 1078 | extent_start = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1079 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1080 | goto allocate; |
| 1081 | } |
| 1082 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1083 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1084 | extent_start = extent_start >> inode->i_blkbits; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1085 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1086 | if (iblock >= extent_start && iblock < extent_end) { |
| 1087 | err = 0; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1088 | btrfs_map_bh_to_logical(root, result, blocknr + iblock - |
| 1089 | extent_start); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1090 | goto out; |
| 1091 | } |
| 1092 | allocate: |
| 1093 | /* ok, create a new extent */ |
| 1094 | if (!create) { |
| 1095 | err = 0; |
| 1096 | goto out; |
| 1097 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1098 | #if 0 |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1099 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
| 1100 | iblock << inode->i_blkbits, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1101 | 1, extent_end, &blocknr); |
| 1102 | if (ret) { |
| 1103 | err = ret; |
| 1104 | goto out; |
| 1105 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1106 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 1107 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1108 | map_bh(result, inode->i_sb, blocknr); |
| 1109 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1110 | btrfs_map_bh_to_logical(root, result, blocknr); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1111 | #endif |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1112 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1113 | btrfs_release_path(root, path); |
| 1114 | btrfs_free_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1115 | if (trans) |
| 1116 | btrfs_end_transaction(trans, root); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1117 | return err; |
| 1118 | } |
| 1119 | |
| 1120 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 1121 | struct buffer_head *result, int create) |
| 1122 | { |
| 1123 | int err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1125 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1126 | err = btrfs_get_block_lock(inode, iblock, result, create); |
| 1127 | // err = btrfs_get_block_inline(inode, iblock, result, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1128 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1129 | return err; |
| 1130 | } |
| 1131 | |
| 1132 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 1133 | unsigned from, unsigned to) |
| 1134 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1135 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1136 | } |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1137 | static int btrfs_commit_write(struct file *file, struct page *page, |
| 1138 | unsigned from, unsigned to) |
| 1139 | { |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1140 | return nobh_commit_write(file, page, from, to); |
| 1141 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1142 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1143 | static void btrfs_write_super(struct super_block *sb) |
| 1144 | { |
| 1145 | btrfs_sync_fs(sb, 1); |
| 1146 | } |
| 1147 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1148 | static int btrfs_readpage(struct file *file, struct page *page) |
| 1149 | { |
| 1150 | return mpage_readpage(page, btrfs_get_block); |
| 1151 | } |
| 1152 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1153 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 1154 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1155 | return nobh_writepage(page, btrfs_get_block, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1156 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1157 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1158 | static void btrfs_truncate(struct inode *inode) |
| 1159 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1160 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1161 | int ret; |
| 1162 | struct btrfs_trans_handle *trans; |
| 1163 | |
| 1164 | if (!S_ISREG(inode->i_mode)) |
| 1165 | return; |
| 1166 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1167 | return; |
| 1168 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1169 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1170 | |
| 1171 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 1172 | mutex_lock(&root->fs_info->fs_mutex); |
| 1173 | trans = btrfs_start_transaction(root, 1); |
| 1174 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 1175 | BUG_ON(ret); |
| 1176 | ret = btrfs_end_transaction(trans, root); |
| 1177 | BUG_ON(ret); |
| 1178 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1179 | mark_inode_dirty(inode); |
| 1180 | } |
| 1181 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1182 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 1183 | struct page **prepared_pages, |
| 1184 | const char __user * buf) |
| 1185 | { |
| 1186 | long page_fault = 0; |
| 1187 | int i; |
| 1188 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1189 | |
| 1190 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 1191 | size_t count = min_t(size_t, |
| 1192 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 1193 | struct page *page = prepared_pages[i]; |
| 1194 | fault_in_pages_readable(buf, count); |
| 1195 | |
| 1196 | /* Copy data from userspace to the current page */ |
| 1197 | kmap(page); |
| 1198 | page_fault = __copy_from_user(page_address(page) + offset, |
| 1199 | buf, count); |
| 1200 | /* Flush processor's dcache for this page */ |
| 1201 | flush_dcache_page(page); |
| 1202 | kunmap(page); |
| 1203 | buf += count; |
| 1204 | write_bytes -= count; |
| 1205 | |
| 1206 | if (page_fault) |
| 1207 | break; |
| 1208 | } |
| 1209 | return page_fault ? -EFAULT : 0; |
| 1210 | } |
| 1211 | |
| 1212 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 1213 | { |
| 1214 | size_t i; |
| 1215 | for (i = 0; i < num_pages; i++) { |
| 1216 | if (!pages[i]) |
| 1217 | break; |
| 1218 | unlock_page(pages[i]); |
| 1219 | mark_page_accessed(pages[i]); |
| 1220 | page_cache_release(pages[i]); |
| 1221 | } |
| 1222 | } |
| 1223 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 1224 | struct btrfs_root *root, |
| 1225 | struct file *file, |
| 1226 | struct page **pages, |
| 1227 | size_t num_pages, |
| 1228 | loff_t pos, |
| 1229 | size_t write_bytes) |
| 1230 | { |
| 1231 | int i; |
| 1232 | int offset; |
| 1233 | int err = 0; |
| 1234 | int ret; |
| 1235 | int this_write; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1236 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1237 | |
| 1238 | for (i = 0; i < num_pages; i++) { |
| 1239 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1240 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1241 | /* FIXME, one block at a time */ |
| 1242 | |
| 1243 | mutex_lock(&root->fs_info->fs_mutex); |
| 1244 | trans = btrfs_start_transaction(root, 1); |
| 1245 | btrfs_csum_file_block(trans, root, inode->i_ino, |
| 1246 | pages[i]->index << PAGE_CACHE_SHIFT, |
| 1247 | kmap(pages[i]), PAGE_CACHE_SIZE); |
| 1248 | kunmap(pages[i]); |
| 1249 | SetPageChecked(pages[i]); |
| 1250 | ret = btrfs_end_transaction(trans, root); |
| 1251 | BUG_ON(ret); |
| 1252 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1253 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1254 | ret = nobh_commit_write(file, pages[i], offset, |
| 1255 | offset + this_write); |
| 1256 | pos += this_write; |
| 1257 | if (ret) { |
| 1258 | err = ret; |
| 1259 | goto failed; |
| 1260 | } |
| 1261 | WARN_ON(this_write > write_bytes); |
| 1262 | write_bytes -= this_write; |
| 1263 | } |
| 1264 | failed: |
| 1265 | return err; |
| 1266 | } |
| 1267 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1268 | static int drop_extents(struct btrfs_trans_handle *trans, |
| 1269 | struct btrfs_root *root, |
| 1270 | struct inode *inode, |
| 1271 | u64 start, u64 end) |
| 1272 | { |
| 1273 | int ret; |
| 1274 | struct btrfs_key key; |
| 1275 | struct btrfs_leaf *leaf; |
| 1276 | int slot; |
| 1277 | struct btrfs_file_extent_item *extent; |
| 1278 | u64 extent_end; |
| 1279 | int keep; |
| 1280 | struct btrfs_file_extent_item old; |
| 1281 | struct btrfs_path *path; |
| 1282 | u64 search_start = start; |
| 1283 | int bookend; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1284 | path = btrfs_alloc_path(); |
| 1285 | if (!path) |
| 1286 | return -ENOMEM; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1287 | while(1) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1288 | btrfs_release_path(root, path); |
| 1289 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1290 | search_start, -1); |
| 1291 | if (ret < 0) |
| 1292 | goto out; |
| 1293 | if (ret > 0) { |
| 1294 | if (path->slots[0] == 0) { |
| 1295 | ret = -ENOENT; |
| 1296 | goto out; |
| 1297 | } |
| 1298 | path->slots[0]--; |
| 1299 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1300 | keep = 0; |
| 1301 | bookend = 0; |
| 1302 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1303 | slot = path->slots[0]; |
| 1304 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1305 | extent = btrfs_item_ptr(leaf, slot, |
| 1306 | struct btrfs_file_extent_item); |
| 1307 | extent_end = key.offset + |
| 1308 | (btrfs_file_extent_num_blocks(extent) << |
| 1309 | inode->i_blkbits); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1310 | if (key.offset >= end || key.objectid != inode->i_ino) { |
| 1311 | ret = 0; |
| 1312 | goto out; |
| 1313 | } |
| 1314 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1315 | goto out; |
| 1316 | if (search_start >= extent_end) |
| 1317 | goto out; |
| 1318 | search_start = extent_end; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1319 | |
| 1320 | if (end < extent_end && end >= key.offset) { |
| 1321 | memcpy(&old, extent, sizeof(old)); |
| 1322 | ret = btrfs_inc_extent_ref(trans, root, |
| 1323 | btrfs_file_extent_disk_blocknr(&old), |
| 1324 | btrfs_file_extent_disk_num_blocks(&old)); |
| 1325 | BUG_ON(ret); |
| 1326 | bookend = 1; |
| 1327 | } |
| 1328 | |
| 1329 | if (start > key.offset) { |
| 1330 | u64 new_num; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1331 | u64 old_num; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1332 | /* truncate existing extent */ |
| 1333 | keep = 1; |
| 1334 | WARN_ON(start & (root->blocksize - 1)); |
| 1335 | new_num = (start - key.offset) >> inode->i_blkbits; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1336 | old_num = btrfs_file_extent_num_blocks(extent); |
| 1337 | inode->i_blocks -= (old_num - new_num) << 3; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1338 | btrfs_set_file_extent_num_blocks(extent, new_num); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1339 | mark_buffer_dirty(path->nodes[0]); |
| 1340 | } |
| 1341 | if (!keep) { |
| 1342 | u64 disk_blocknr; |
| 1343 | u64 disk_num_blocks; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1344 | disk_blocknr = btrfs_file_extent_disk_blocknr(extent); |
| 1345 | disk_num_blocks = |
| 1346 | btrfs_file_extent_disk_num_blocks(extent); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1347 | ret = btrfs_del_item(trans, root, path); |
| 1348 | BUG_ON(ret); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1349 | inode->i_blocks -= |
| 1350 | btrfs_file_extent_num_blocks(extent) << 3; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1351 | btrfs_release_path(root, path); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1352 | ret = btrfs_free_extent(trans, root, disk_blocknr, |
| 1353 | disk_num_blocks, 0); |
| 1354 | |
| 1355 | BUG_ON(ret); |
| 1356 | if (!bookend && search_start >= end) { |
| 1357 | ret = 0; |
| 1358 | goto out; |
| 1359 | } |
| 1360 | if (!bookend) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1361 | continue; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1362 | } |
| 1363 | if (bookend) { |
| 1364 | /* create bookend */ |
| 1365 | struct btrfs_key ins; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1366 | ins.objectid = inode->i_ino; |
| 1367 | ins.offset = end; |
| 1368 | ins.flags = 0; |
| 1369 | btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); |
| 1370 | |
| 1371 | btrfs_release_path(root, path); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1372 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 1373 | sizeof(*extent)); |
| 1374 | BUG_ON(ret); |
| 1375 | extent = btrfs_item_ptr( |
| 1376 | btrfs_buffer_leaf(path->nodes[0]), |
| 1377 | path->slots[0], |
| 1378 | struct btrfs_file_extent_item); |
| 1379 | btrfs_set_file_extent_disk_blocknr(extent, |
| 1380 | btrfs_file_extent_disk_blocknr(&old)); |
| 1381 | btrfs_set_file_extent_disk_num_blocks(extent, |
| 1382 | btrfs_file_extent_disk_num_blocks(&old)); |
| 1383 | |
| 1384 | btrfs_set_file_extent_offset(extent, |
| 1385 | btrfs_file_extent_offset(&old) + |
| 1386 | ((end - key.offset) >> inode->i_blkbits)); |
| 1387 | WARN_ON(btrfs_file_extent_num_blocks(&old) < |
| 1388 | (end - key.offset) >> inode->i_blkbits); |
| 1389 | btrfs_set_file_extent_num_blocks(extent, |
| 1390 | btrfs_file_extent_num_blocks(&old) - |
| 1391 | ((end - key.offset) >> inode->i_blkbits)); |
| 1392 | |
| 1393 | btrfs_set_file_extent_generation(extent, |
| 1394 | btrfs_file_extent_generation(&old)); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1395 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1396 | inode->i_blocks += |
| 1397 | btrfs_file_extent_num_blocks(extent) << 3; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1398 | ret = 0; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 1399 | goto out; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1400 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1401 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1402 | out: |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1403 | btrfs_free_path(path); |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | static int prepare_pages(struct btrfs_root *root, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1408 | struct file *file, |
| 1409 | struct page **pages, |
| 1410 | size_t num_pages, |
| 1411 | loff_t pos, |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1412 | unsigned long first_index, |
| 1413 | unsigned long last_index, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1414 | size_t write_bytes, |
| 1415 | u64 alloc_extent_start) |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1416 | { |
| 1417 | int i; |
| 1418 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 1419 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1420 | int offset; |
| 1421 | int err = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1422 | int this_write; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1423 | struct buffer_head *bh; |
| 1424 | struct buffer_head *head; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1425 | loff_t isize = i_size_read(inode); |
| 1426 | |
| 1427 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 1428 | |
| 1429 | for (i = 0; i < num_pages; i++) { |
| 1430 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 1431 | if (!pages[i]) { |
| 1432 | err = -ENOMEM; |
| 1433 | goto failed_release; |
| 1434 | } |
| 1435 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1436 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1437 | create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize, |
| 1438 | (1 << BH_Uptodate)); |
| 1439 | head = page_buffers(pages[i]); |
| 1440 | bh = head; |
| 1441 | do { |
| 1442 | err = btrfs_map_bh_to_logical(root, bh, |
| 1443 | alloc_extent_start); |
| 1444 | BUG_ON(err); |
| 1445 | if (err) |
| 1446 | goto failed_truncate; |
| 1447 | bh = bh->b_this_page; |
| 1448 | alloc_extent_start++; |
| 1449 | } while (bh != head); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1450 | pos += this_write; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1451 | WARN_ON(this_write > write_bytes); |
| 1452 | write_bytes -= this_write; |
| 1453 | } |
| 1454 | return 0; |
| 1455 | |
| 1456 | failed_release: |
| 1457 | btrfs_drop_pages(pages, num_pages); |
| 1458 | return err; |
| 1459 | |
| 1460 | failed_truncate: |
| 1461 | btrfs_drop_pages(pages, num_pages); |
| 1462 | if (pos > isize) |
| 1463 | vmtruncate(inode, isize); |
| 1464 | return err; |
| 1465 | } |
| 1466 | |
| 1467 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 1468 | size_t count, loff_t *ppos) |
| 1469 | { |
| 1470 | loff_t pos; |
| 1471 | size_t num_written = 0; |
| 1472 | int err = 0; |
| 1473 | int ret = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1474 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1475 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1476 | struct page *pages[8]; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1477 | struct page *pinned[2] = { NULL, NULL }; |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1478 | unsigned long first_index; |
| 1479 | unsigned long last_index; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1480 | u64 start_pos; |
| 1481 | u64 num_blocks; |
| 1482 | u64 alloc_extent_start; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1483 | struct btrfs_trans_handle *trans; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1484 | struct btrfs_key ins; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1485 | |
| 1486 | if (file->f_flags & O_DIRECT) |
| 1487 | return -EINVAL; |
| 1488 | pos = *ppos; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1489 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1490 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1491 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1492 | if (err) |
| 1493 | goto out; |
| 1494 | if (count == 0) |
| 1495 | goto out; |
| 1496 | err = remove_suid(file->f_path.dentry); |
| 1497 | if (err) |
| 1498 | goto out; |
| 1499 | file_update_time(file); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1500 | |
| 1501 | start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1); |
| 1502 | num_blocks = (count + pos - start_pos + root->blocksize - 1) >> |
| 1503 | inode->i_blkbits; |
| 1504 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1505 | mutex_lock(&inode->i_mutex); |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1506 | first_index = pos >> PAGE_CACHE_SHIFT; |
| 1507 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1508 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1509 | if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size && |
| 1510 | (pos & (PAGE_CACHE_SIZE - 1))) { |
| 1511 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); |
| 1512 | if (!PageUptodate(pinned[0])) { |
| 1513 | ret = mpage_readpage(pinned[0], btrfs_get_block); |
| 1514 | BUG_ON(ret); |
| 1515 | } else { |
| 1516 | unlock_page(pinned[0]); |
| 1517 | } |
| 1518 | } |
| 1519 | if (first_index != last_index && |
| 1520 | (last_index << PAGE_CACHE_SHIFT) < inode->i_size && |
| 1521 | (count & (PAGE_CACHE_SIZE - 1))) { |
| 1522 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); |
| 1523 | if (!PageUptodate(pinned[1])) { |
| 1524 | ret = mpage_readpage(pinned[1], btrfs_get_block); |
| 1525 | BUG_ON(ret); |
| 1526 | } else { |
| 1527 | unlock_page(pinned[1]); |
| 1528 | } |
| 1529 | } |
| 1530 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1531 | mutex_lock(&root->fs_info->fs_mutex); |
| 1532 | trans = btrfs_start_transaction(root, 1); |
| 1533 | if (!trans) { |
| 1534 | err = -ENOMEM; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1535 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1536 | goto out_unlock; |
| 1537 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1538 | /* FIXME blocksize != 4096 */ |
| 1539 | inode->i_blocks += num_blocks << 3; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1540 | if (start_pos < inode->i_size) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1541 | /* FIXME blocksize != pagesize */ |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1542 | ret = drop_extents(trans, root, inode, |
| 1543 | start_pos, |
| 1544 | (pos + count + root->blocksize -1) & |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1545 | ~((u64)root->blocksize - 1)); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1546 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1547 | ret = btrfs_alloc_extent(trans, root, num_blocks, 1, |
| 1548 | (u64)-1, &ins); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1549 | BUG_ON(ret); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1550 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 1551 | start_pos, ins.objectid, ins.offset); |
| 1552 | BUG_ON(ret); |
| 1553 | alloc_extent_start = ins.objectid; |
| 1554 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1555 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1556 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1557 | while(count > 0) { |
| 1558 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1559 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1560 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 1561 | PAGE_CACHE_SHIFT; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1562 | |
| 1563 | memset(pages, 0, sizeof(pages)); |
| 1564 | ret = prepare_pages(root, file, pages, num_pages, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1565 | pos, first_index, last_index, |
| 1566 | write_bytes, alloc_extent_start); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1567 | BUG_ON(ret); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1568 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1569 | /* FIXME blocks != pagesize */ |
| 1570 | alloc_extent_start += num_pages; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1571 | ret = btrfs_copy_from_user(pos, num_pages, |
| 1572 | write_bytes, pages, buf); |
| 1573 | BUG_ON(ret); |
| 1574 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1575 | ret = dirty_and_release_pages(NULL, root, file, pages, |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 1576 | num_pages, pos, write_bytes); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1577 | BUG_ON(ret); |
| 1578 | btrfs_drop_pages(pages, num_pages); |
| 1579 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1580 | buf += write_bytes; |
| 1581 | count -= write_bytes; |
| 1582 | pos += write_bytes; |
| 1583 | num_written += write_bytes; |
| 1584 | |
| 1585 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1586 | cond_resched(); |
| 1587 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1588 | out_unlock: |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1589 | mutex_unlock(&inode->i_mutex); |
| 1590 | out: |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1591 | if (pinned[0]) |
| 1592 | page_cache_release(pinned[0]); |
| 1593 | if (pinned[1]) |
| 1594 | page_cache_release(pinned[1]); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1595 | *ppos = pos; |
| 1596 | current->backing_dev_info = NULL; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame^] | 1597 | mark_inode_dirty(inode); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1598 | return num_written ? num_written : err; |
| 1599 | } |
| 1600 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1601 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1602 | static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode, |
| 1603 | struct page *page, loff_t pos, |
| 1604 | size_t offset, size_t write_bytes) |
| 1605 | { |
| 1606 | struct btrfs_path *path; |
| 1607 | struct btrfs_trans_handle *trans; |
| 1608 | struct btrfs_key key; |
| 1609 | struct btrfs_leaf *leaf; |
| 1610 | struct btrfs_key found_key; |
| 1611 | int ret; |
| 1612 | size_t copy_size = 0; |
| 1613 | char *dst = NULL; |
| 1614 | int err = 0; |
| 1615 | size_t num_written = 0; |
| 1616 | |
| 1617 | path = btrfs_alloc_path(); |
| 1618 | BUG_ON(!path); |
| 1619 | mutex_lock(&root->fs_info->fs_mutex); |
| 1620 | trans = btrfs_start_transaction(root, 1); |
| 1621 | key.objectid = inode->i_ino; |
| 1622 | key.flags = 0; |
| 1623 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 1624 | |
| 1625 | again: |
| 1626 | key.offset = pos; |
| 1627 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1628 | if (ret < 0) { |
| 1629 | err = ret; |
| 1630 | goto out; |
| 1631 | } |
| 1632 | if (ret == 0) { |
| 1633 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1634 | btrfs_disk_key_to_cpu(&found_key, |
| 1635 | &leaf->items[path->slots[0]].key); |
| 1636 | copy_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 1637 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1638 | copy_size = min(write_bytes, copy_size); |
| 1639 | goto copyit; |
| 1640 | } else { |
| 1641 | int slot = path->slots[0]; |
| 1642 | if (slot > 0) { |
| 1643 | slot--; |
| 1644 | } |
| 1645 | // FIXME find max key |
| 1646 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1647 | btrfs_disk_key_to_cpu(&found_key, |
| 1648 | &leaf->items[slot].key); |
| 1649 | if (found_key.objectid != inode->i_ino) |
| 1650 | goto insert; |
| 1651 | if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY) |
| 1652 | goto insert; |
| 1653 | copy_size = btrfs_item_size(leaf->items + slot); |
| 1654 | if (found_key.offset + copy_size <= pos) |
| 1655 | goto insert; |
| 1656 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1657 | dst += pos - found_key.offset; |
| 1658 | copy_size = copy_size - (pos - found_key.offset); |
| 1659 | BUG_ON(copy_size < 0); |
| 1660 | copy_size = min(write_bytes, copy_size); |
| 1661 | WARN_ON(copy_size == 0); |
| 1662 | goto copyit; |
| 1663 | } |
| 1664 | insert: |
| 1665 | btrfs_release_path(root, path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1666 | copy_size = min(write_bytes, |
| 1667 | (size_t)BTRFS_LEAF_DATA_SIZE(root) - |
| 1668 | sizeof(struct btrfs_item) * 4); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1669 | ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size); |
| 1670 | BUG_ON(ret); |
| 1671 | dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1672 | path->slots[0], char); |
| 1673 | copyit: |
| 1674 | WARN_ON(copy_size == 0); |
| 1675 | WARN_ON(dst + copy_size > |
| 1676 | btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1677 | path->slots[0], char) + |
| 1678 | btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items + |
| 1679 | path->slots[0])); |
| 1680 | btrfs_memcpy(root, path->nodes[0]->b_data, dst, |
| 1681 | page_address(page) + offset, copy_size); |
| 1682 | mark_buffer_dirty(path->nodes[0]); |
| 1683 | btrfs_release_path(root, path); |
| 1684 | pos += copy_size; |
| 1685 | offset += copy_size; |
| 1686 | num_written += copy_size; |
| 1687 | write_bytes -= copy_size; |
| 1688 | if (write_bytes) |
| 1689 | goto again; |
| 1690 | out: |
| 1691 | btrfs_free_path(path); |
| 1692 | ret = btrfs_end_transaction(trans, root); |
| 1693 | BUG_ON(ret); |
| 1694 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1695 | return num_written ? num_written : err; |
| 1696 | } |
| 1697 | |
| 1698 | static ssize_t btrfs_file_inline_write(struct file *file, |
| 1699 | const char __user *buf, |
| 1700 | size_t count, loff_t *ppos) |
| 1701 | { |
| 1702 | loff_t pos; |
| 1703 | size_t num_written = 0; |
| 1704 | int err = 0; |
| 1705 | int ret = 0; |
| 1706 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1708 | unsigned long page_index; |
| 1709 | |
| 1710 | if (file->f_flags & O_DIRECT) |
| 1711 | return -EINVAL; |
| 1712 | pos = *ppos; |
| 1713 | |
| 1714 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1715 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1716 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1717 | if (err) |
| 1718 | goto out; |
| 1719 | if (count == 0) |
| 1720 | goto out; |
| 1721 | err = remove_suid(file->f_path.dentry); |
| 1722 | if (err) |
| 1723 | goto out; |
| 1724 | file_update_time(file); |
| 1725 | mutex_lock(&inode->i_mutex); |
| 1726 | while(count > 0) { |
| 1727 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1728 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1729 | struct page *page; |
| 1730 | |
| 1731 | page_index = pos >> PAGE_CACHE_SHIFT; |
| 1732 | page = grab_cache_page(inode->i_mapping, page_index); |
| 1733 | if (!PageUptodate(page)) { |
| 1734 | ret = mpage_readpage(page, btrfs_get_block); |
| 1735 | BUG_ON(ret); |
| 1736 | lock_page(page); |
| 1737 | } |
| 1738 | ret = btrfs_copy_from_user(pos, 1, |
| 1739 | write_bytes, &page, buf); |
| 1740 | BUG_ON(ret); |
| 1741 | write_bytes = inline_one_page(root, inode, page, pos, |
| 1742 | offset, write_bytes); |
| 1743 | SetPageUptodate(page); |
| 1744 | if (write_bytes > 0 && pos + write_bytes > inode->i_size) { |
| 1745 | i_size_write(inode, pos + write_bytes); |
| 1746 | mark_inode_dirty(inode); |
| 1747 | } |
| 1748 | page_cache_release(page); |
| 1749 | unlock_page(page); |
| 1750 | if (write_bytes < 0) |
| 1751 | goto out_unlock; |
| 1752 | buf += write_bytes; |
| 1753 | count -= write_bytes; |
| 1754 | pos += write_bytes; |
| 1755 | num_written += write_bytes; |
| 1756 | |
| 1757 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1758 | cond_resched(); |
| 1759 | } |
| 1760 | out_unlock: |
| 1761 | mutex_unlock(&inode->i_mutex); |
| 1762 | out: |
| 1763 | *ppos = pos; |
| 1764 | current->backing_dev_info = NULL; |
| 1765 | return num_written ? num_written : err; |
| 1766 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1767 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1768 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1769 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
| 1770 | unsigned long offset, unsigned long size) |
| 1771 | { |
| 1772 | char *kaddr; |
| 1773 | unsigned long left, count = desc->count; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1774 | struct inode *inode = page->mapping->host; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1775 | |
| 1776 | if (size > count) |
| 1777 | size = count; |
| 1778 | |
| 1779 | if (!PageChecked(page)) { |
| 1780 | /* FIXME, do it per block */ |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1781 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1782 | int ret = btrfs_csum_verify_file_block(root, |
| 1783 | page->mapping->host->i_ino, |
| 1784 | page->index << PAGE_CACHE_SHIFT, |
| 1785 | kmap(page), PAGE_CACHE_SIZE); |
| 1786 | if (ret) { |
| 1787 | printk("failed to verify ino %lu page %lu\n", |
| 1788 | page->mapping->host->i_ino, |
| 1789 | page->index); |
| 1790 | memset(page_address(page), 0, PAGE_CACHE_SIZE); |
| 1791 | } |
| 1792 | SetPageChecked(page); |
| 1793 | kunmap(page); |
| 1794 | } |
| 1795 | /* |
| 1796 | * Faults on the destination of a read are common, so do it before |
| 1797 | * taking the kmap. |
| 1798 | */ |
| 1799 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 1800 | kaddr = kmap_atomic(page, KM_USER0); |
| 1801 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 1802 | kaddr + offset, size); |
| 1803 | kunmap_atomic(kaddr, KM_USER0); |
| 1804 | if (left == 0) |
| 1805 | goto success; |
| 1806 | } |
| 1807 | |
| 1808 | /* Do it the slow way */ |
| 1809 | kaddr = kmap(page); |
| 1810 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 1811 | kunmap(page); |
| 1812 | |
| 1813 | if (left) { |
| 1814 | size -= left; |
| 1815 | desc->error = -EFAULT; |
| 1816 | } |
| 1817 | success: |
| 1818 | desc->count = count - size; |
| 1819 | desc->written += size; |
| 1820 | desc->arg.buf += size; |
| 1821 | return size; |
| 1822 | } |
| 1823 | |
| 1824 | /** |
| 1825 | * btrfs_file_aio_read - filesystem read routine |
| 1826 | * @iocb: kernel I/O control block |
| 1827 | * @iov: io vector request |
| 1828 | * @nr_segs: number of segments in the iovec |
| 1829 | * @pos: current file position |
| 1830 | */ |
| 1831 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 1832 | unsigned long nr_segs, loff_t pos) |
| 1833 | { |
| 1834 | struct file *filp = iocb->ki_filp; |
| 1835 | ssize_t retval; |
| 1836 | unsigned long seg; |
| 1837 | size_t count; |
| 1838 | loff_t *ppos = &iocb->ki_pos; |
| 1839 | |
| 1840 | count = 0; |
| 1841 | for (seg = 0; seg < nr_segs; seg++) { |
| 1842 | const struct iovec *iv = &iov[seg]; |
| 1843 | |
| 1844 | /* |
| 1845 | * If any segment has a negative length, or the cumulative |
| 1846 | * length ever wraps negative then return -EINVAL. |
| 1847 | */ |
| 1848 | count += iv->iov_len; |
| 1849 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 1850 | return -EINVAL; |
| 1851 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 1852 | continue; |
| 1853 | if (seg == 0) |
| 1854 | return -EFAULT; |
| 1855 | nr_segs = seg; |
| 1856 | count -= iv->iov_len; /* This segment is no good */ |
| 1857 | break; |
| 1858 | } |
| 1859 | retval = 0; |
| 1860 | if (count) { |
| 1861 | for (seg = 0; seg < nr_segs; seg++) { |
| 1862 | read_descriptor_t desc; |
| 1863 | |
| 1864 | desc.written = 0; |
| 1865 | desc.arg.buf = iov[seg].iov_base; |
| 1866 | desc.count = iov[seg].iov_len; |
| 1867 | if (desc.count == 0) |
| 1868 | continue; |
| 1869 | desc.error = 0; |
| 1870 | do_generic_file_read(filp, ppos, &desc, |
| 1871 | btrfs_read_actor); |
| 1872 | retval += desc.written; |
| 1873 | if (desc.error) { |
| 1874 | retval = retval ?: desc.error; |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | return retval; |
| 1880 | } |
| 1881 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1882 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
| 1883 | { |
| 1884 | struct btrfs_trans_handle *trans; |
| 1885 | struct btrfs_key key; |
| 1886 | struct btrfs_root_item root_item; |
| 1887 | struct btrfs_inode_item *inode_item; |
| 1888 | struct buffer_head *subvol; |
| 1889 | struct btrfs_leaf *leaf; |
| 1890 | struct btrfs_root *new_root; |
| 1891 | struct inode *inode; |
| 1892 | int ret; |
| 1893 | u64 objectid; |
| 1894 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 1895 | |
| 1896 | mutex_lock(&root->fs_info->fs_mutex); |
| 1897 | trans = btrfs_start_transaction(root, 1); |
| 1898 | BUG_ON(!trans); |
| 1899 | |
| 1900 | subvol = btrfs_alloc_free_block(trans, root); |
| 1901 | leaf = btrfs_buffer_leaf(subvol); |
| 1902 | btrfs_set_header_nritems(&leaf->header, 0); |
| 1903 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1904 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1905 | btrfs_set_header_generation(&leaf->header, trans->transid); |
| 1906 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, |
| 1907 | sizeof(leaf->header.fsid)); |
| 1908 | |
| 1909 | inode_item = &root_item.inode; |
| 1910 | memset(inode_item, 0, sizeof(*inode_item)); |
| 1911 | btrfs_set_inode_generation(inode_item, 1); |
| 1912 | btrfs_set_inode_size(inode_item, 3); |
| 1913 | btrfs_set_inode_nlink(inode_item, 1); |
| 1914 | btrfs_set_inode_nblocks(inode_item, 1); |
| 1915 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); |
| 1916 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1917 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1918 | btrfs_set_root_refs(&root_item, 1); |
| 1919 | |
| 1920 | mark_buffer_dirty(subvol); |
| 1921 | brelse(subvol); |
| 1922 | subvol = NULL; |
| 1923 | |
| 1924 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 1925 | 0, &objectid); |
| 1926 | BUG_ON(ret); |
| 1927 | |
| 1928 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 1929 | |
| 1930 | key.objectid = objectid; |
| 1931 | key.offset = 1; |
| 1932 | key.flags = 0; |
| 1933 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 1934 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 1935 | &root_item); |
| 1936 | BUG_ON(ret); |
| 1937 | |
| 1938 | /* |
| 1939 | * insert the directory item |
| 1940 | */ |
| 1941 | key.offset = (u64)-1; |
| 1942 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 1943 | name, namelen, |
| 1944 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 1945 | &key, 0); |
| 1946 | BUG_ON(ret); |
| 1947 | |
| 1948 | ret = btrfs_commit_transaction(trans, root); |
| 1949 | BUG_ON(ret); |
| 1950 | |
| 1951 | new_root = btrfs_read_fs_root(root->fs_info, &key); |
| 1952 | BUG_ON(!new_root); |
| 1953 | |
| 1954 | trans = btrfs_start_transaction(new_root, 1); |
| 1955 | BUG_ON(!trans); |
| 1956 | |
| 1957 | inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700); |
| 1958 | inode->i_op = &btrfs_dir_inode_operations; |
| 1959 | inode->i_fop = &btrfs_dir_file_operations; |
| 1960 | |
| 1961 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); |
| 1962 | BUG_ON(ret); |
| 1963 | |
| 1964 | inode->i_nlink = 1; |
| 1965 | inode->i_size = 6; |
| 1966 | ret = btrfs_update_inode(trans, new_root, inode); |
| 1967 | BUG_ON(ret); |
| 1968 | |
| 1969 | ret = btrfs_commit_transaction(trans, new_root); |
| 1970 | BUG_ON(ret); |
| 1971 | |
| 1972 | iput(inode); |
| 1973 | |
| 1974 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1978 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 1979 | { |
| 1980 | struct btrfs_trans_handle *trans; |
| 1981 | struct btrfs_key key; |
| 1982 | struct btrfs_root_item new_root_item; |
| 1983 | int ret; |
| 1984 | u64 objectid; |
| 1985 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1986 | if (!root->ref_cows) |
| 1987 | return -EINVAL; |
| 1988 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1989 | mutex_lock(&root->fs_info->fs_mutex); |
| 1990 | trans = btrfs_start_transaction(root, 1); |
| 1991 | BUG_ON(!trans); |
| 1992 | |
| 1993 | ret = btrfs_update_inode(trans, root, root->inode); |
| 1994 | BUG_ON(ret); |
| 1995 | |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 1996 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 1997 | 0, &objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1998 | BUG_ON(ret); |
| 1999 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2000 | memcpy(&new_root_item, &root->root_item, |
| 2001 | sizeof(new_root_item)); |
| 2002 | |
| 2003 | key.objectid = objectid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2004 | key.offset = 1; |
| 2005 | key.flags = 0; |
| 2006 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 2007 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2008 | |
| 2009 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2010 | &new_root_item); |
| 2011 | BUG_ON(ret); |
| 2012 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2013 | /* |
| 2014 | * insert the directory item |
| 2015 | */ |
| 2016 | key.offset = (u64)-1; |
| 2017 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2018 | name, namelen, |
| 2019 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 2020 | &key, 0); |
| 2021 | |
| 2022 | BUG_ON(ret); |
| 2023 | |
| 2024 | ret = btrfs_inc_root_ref(trans, root); |
| 2025 | BUG_ON(ret); |
| 2026 | |
| 2027 | ret = btrfs_commit_transaction(trans, root); |
| 2028 | BUG_ON(ret); |
| 2029 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2033 | static int add_disk(struct btrfs_root *root, char *name, int namelen) |
| 2034 | { |
| 2035 | struct block_device *bdev; |
| 2036 | struct btrfs_path *path; |
| 2037 | struct super_block *sb = root->fs_info->sb; |
| 2038 | struct btrfs_root *dev_root = root->fs_info->dev_root; |
| 2039 | struct btrfs_trans_handle *trans; |
| 2040 | struct btrfs_device_item *dev_item; |
| 2041 | struct btrfs_key key; |
| 2042 | u16 item_size; |
| 2043 | u64 num_blocks; |
| 2044 | u64 new_blocks; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2045 | u64 device_id; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2046 | int ret; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2047 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2048 | printk("adding disk %s\n", name); |
| 2049 | path = btrfs_alloc_path(); |
| 2050 | if (!path) |
| 2051 | return -ENOMEM; |
| 2052 | num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super); |
| 2053 | bdev = open_bdev_excl(name, O_RDWR, sb); |
| 2054 | if (IS_ERR(bdev)) { |
| 2055 | ret = PTR_ERR(bdev); |
| 2056 | printk("open bdev excl failed ret %d\n", ret); |
| 2057 | goto out_nolock; |
| 2058 | } |
| 2059 | set_blocksize(bdev, sb->s_blocksize); |
| 2060 | new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits; |
| 2061 | key.objectid = num_blocks; |
| 2062 | key.offset = new_blocks; |
| 2063 | key.flags = 0; |
| 2064 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); |
| 2065 | |
| 2066 | mutex_lock(&dev_root->fs_info->fs_mutex); |
| 2067 | trans = btrfs_start_transaction(dev_root, 1); |
| 2068 | item_size = sizeof(*dev_item) + namelen; |
| 2069 | printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size); |
| 2070 | ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size); |
| 2071 | if (ret) { |
| 2072 | printk("insert failed %d\n", ret); |
| 2073 | close_bdev_excl(bdev); |
| 2074 | if (ret > 0) |
| 2075 | ret = -EEXIST; |
| 2076 | goto out; |
| 2077 | } |
| 2078 | dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 2079 | path->slots[0], struct btrfs_device_item); |
| 2080 | btrfs_set_device_pathlen(dev_item, namelen); |
| 2081 | memcpy(dev_item + 1, name, namelen); |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2082 | |
| 2083 | device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1; |
| 2084 | btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id); |
| 2085 | btrfs_set_device_id(dev_item, device_id); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2086 | mark_buffer_dirty(path->nodes[0]); |
| 2087 | |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2088 | ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks, |
| 2089 | new_blocks); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2090 | |
| 2091 | if (!ret) { |
| 2092 | btrfs_set_super_total_blocks(root->fs_info->disk_super, |
| 2093 | num_blocks + new_blocks); |
| 2094 | i_size_write(root->fs_info->btree_inode, |
| 2095 | (num_blocks + new_blocks) << |
| 2096 | root->fs_info->btree_inode->i_blkbits); |
| 2097 | } |
| 2098 | |
| 2099 | out: |
| 2100 | ret = btrfs_commit_transaction(trans, dev_root); |
| 2101 | BUG_ON(ret); |
| 2102 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2103 | out_nolock: |
| 2104 | btrfs_free_path(path); |
| 2105 | |
| 2106 | return ret; |
| 2107 | } |
| 2108 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2109 | static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
| 2110 | cmd, unsigned long arg) |
| 2111 | { |
| 2112 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2113 | struct btrfs_ioctl_vol_args vol_args; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2114 | int ret = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2115 | int namelen; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2116 | struct btrfs_path *path; |
| 2117 | u64 root_dirid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2118 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2119 | switch (cmd) { |
| 2120 | case BTRFS_IOC_SNAP_CREATE: |
| 2121 | if (copy_from_user(&vol_args, |
| 2122 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 2123 | sizeof(vol_args))) |
| 2124 | return -EFAULT; |
| 2125 | namelen = strlen(vol_args.name); |
| 2126 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 2127 | return -EINVAL; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2128 | path = btrfs_alloc_path(); |
| 2129 | if (!path) |
| 2130 | return -ENOMEM; |
Chris Mason | 2d13d8d | 2007-04-10 20:07:20 -0400 | [diff] [blame] | 2131 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2132 | mutex_lock(&root->fs_info->fs_mutex); |
| 2133 | ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 2134 | path, root_dirid, |
| 2135 | vol_args.name, namelen, 0); |
| 2136 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2d13d8d | 2007-04-10 20:07:20 -0400 | [diff] [blame] | 2137 | btrfs_free_path(path); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2138 | if (ret == 0) |
| 2139 | return -EEXIST; |
| 2140 | |
| 2141 | if (root == root->fs_info->tree_root) |
| 2142 | ret = create_subvol(root, vol_args.name, namelen); |
| 2143 | else |
| 2144 | ret = create_snapshot(root, vol_args.name, namelen); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2145 | WARN_ON(ret); |
| 2146 | break; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2147 | case BTRFS_IOC_ADD_DISK: |
| 2148 | if (copy_from_user(&vol_args, |
| 2149 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 2150 | sizeof(vol_args))) |
| 2151 | return -EFAULT; |
| 2152 | namelen = strlen(vol_args.name); |
| 2153 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 2154 | return -EINVAL; |
| 2155 | vol_args.name[namelen] = '\0'; |
| 2156 | ret = add_disk(root, vol_args.name, namelen); |
| 2157 | break; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2158 | default: |
| 2159 | return -ENOTTY; |
| 2160 | } |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2161 | return ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2162 | } |
| 2163 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2164 | static struct kmem_cache *btrfs_inode_cachep; |
| 2165 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 2166 | struct kmem_cache *btrfs_transaction_cachep; |
| 2167 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 2168 | struct kmem_cache *btrfs_path_cachep; |
| 2169 | |
| 2170 | /* |
| 2171 | * Called inside transaction, so use GFP_NOFS |
| 2172 | */ |
| 2173 | static struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 2174 | { |
| 2175 | struct btrfs_inode *ei; |
| 2176 | |
| 2177 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 2178 | if (!ei) |
| 2179 | return NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2180 | return &ei->vfs_inode; |
| 2181 | } |
| 2182 | |
| 2183 | static void btrfs_destroy_inode(struct inode *inode) |
| 2184 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2185 | WARN_ON(!list_empty(&inode->i_dentry)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2186 | WARN_ON(inode->i_data.nrpages); |
| 2187 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2188 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 2189 | } |
| 2190 | |
| 2191 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 2192 | unsigned long flags) |
| 2193 | { |
| 2194 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 2195 | |
| 2196 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 2197 | SLAB_CTOR_CONSTRUCTOR) { |
| 2198 | inode_init_once(&ei->vfs_inode); |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | static int init_inodecache(void) |
| 2203 | { |
| 2204 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 2205 | sizeof(struct btrfs_inode), |
| 2206 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2207 | SLAB_MEM_SPREAD), |
| 2208 | init_once, NULL); |
| 2209 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 2210 | sizeof(struct btrfs_trans_handle), |
| 2211 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2212 | SLAB_MEM_SPREAD), |
| 2213 | NULL, NULL); |
| 2214 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 2215 | sizeof(struct btrfs_transaction), |
| 2216 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2217 | SLAB_MEM_SPREAD), |
| 2218 | NULL, NULL); |
| 2219 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 2220 | sizeof(struct btrfs_transaction), |
| 2221 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2222 | SLAB_MEM_SPREAD), |
| 2223 | NULL, NULL); |
| 2224 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", |
| 2225 | 256, |
| 2226 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2227 | SLAB_MEM_SPREAD | |
| 2228 | SLAB_DESTROY_BY_RCU), |
| 2229 | NULL, NULL); |
| 2230 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || |
| 2231 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) |
| 2232 | return -ENOMEM; |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | static void destroy_inodecache(void) |
| 2237 | { |
| 2238 | kmem_cache_destroy(btrfs_inode_cachep); |
| 2239 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 2240 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 2241 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 2242 | kmem_cache_destroy(btrfs_path_cachep); |
| 2243 | } |
| 2244 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2245 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 2246 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 2247 | { |
| 2248 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 2249 | btrfs_fill_super, mnt); |
| 2250 | } |
| 2251 | |
| 2252 | static struct file_system_type btrfs_fs_type = { |
| 2253 | .owner = THIS_MODULE, |
| 2254 | .name = "btrfs", |
| 2255 | .get_sb = btrfs_get_sb, |
| 2256 | .kill_sb = kill_block_super, |
| 2257 | .fs_flags = FS_REQUIRES_DEV, |
| 2258 | }; |
| 2259 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2260 | static struct super_operations btrfs_super_ops = { |
| 2261 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 2262 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2263 | .put_super = btrfs_put_super, |
| 2264 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2265 | .write_super = btrfs_write_super, |
| 2266 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 2267 | .write_inode = btrfs_write_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2268 | .alloc_inode = btrfs_alloc_inode, |
| 2269 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2270 | }; |
| 2271 | |
| 2272 | static struct inode_operations btrfs_dir_inode_operations = { |
| 2273 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2274 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 2275 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 2276 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 2277 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2278 | }; |
| 2279 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2280 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 2281 | .lookup = btrfs_lookup, |
| 2282 | }; |
| 2283 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2284 | static struct file_operations btrfs_dir_file_operations = { |
| 2285 | .llseek = generic_file_llseek, |
| 2286 | .read = generic_read_dir, |
| 2287 | .readdir = btrfs_readdir, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2288 | .ioctl = btrfs_ioctl, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2289 | }; |
| 2290 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2291 | static struct address_space_operations btrfs_aops = { |
| 2292 | .readpage = btrfs_readpage, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2293 | .writepage = btrfs_writepage, |
| 2294 | .sync_page = block_sync_page, |
| 2295 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 2296 | .commit_write = btrfs_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2297 | }; |
| 2298 | |
| 2299 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2300 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2301 | }; |
| 2302 | |
| 2303 | static struct file_operations btrfs_file_operations = { |
| 2304 | .llseek = generic_file_llseek, |
| 2305 | .read = do_sync_read, |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 2306 | .aio_read = btrfs_file_aio_read, |
| 2307 | .write = btrfs_file_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2308 | .mmap = generic_file_mmap, |
| 2309 | .open = generic_file_open, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2310 | .ioctl = btrfs_ioctl, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2311 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2312 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2313 | static int __init init_btrfs_fs(void) |
| 2314 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2315 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2316 | printk("btrfs loaded!\n"); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2317 | err = init_inodecache(); |
| 2318 | if (err) |
| 2319 | return err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2320 | kset_set_kset_s(&btrfs_subsys, fs_subsys); |
| 2321 | err = subsystem_register(&btrfs_subsys); |
| 2322 | if (err) |
| 2323 | goto out; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2324 | return register_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2325 | out: |
| 2326 | destroy_inodecache(); |
| 2327 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | static void __exit exit_btrfs_fs(void) |
| 2331 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2332 | destroy_inodecache(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2333 | unregister_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2334 | subsystem_unregister(&btrfs_subsys); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2335 | printk("btrfs unloaded\n"); |
| 2336 | } |
| 2337 | |
| 2338 | module_init(init_btrfs_fs) |
| 2339 | module_exit(exit_btrfs_fs) |
| 2340 | |
| 2341 | MODULE_LICENSE("GPL"); |