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 | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 18 | |
| 19 | #define BTRFS_SUPER_MAGIC 0x9123682E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 20 | |
| 21 | static struct inode_operations btrfs_dir_inode_operations; |
| 22 | static struct super_operations btrfs_super_ops; |
| 23 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 24 | static struct inode_operations btrfs_file_inode_operations; |
| 25 | static struct address_space_operations btrfs_aops; |
| 26 | static struct file_operations btrfs_file_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 27 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 28 | static int check_inode(struct inode *inode) |
| 29 | { |
| 30 | struct btrfs_inode *ei = BTRFS_I(inode); |
| 31 | WARN_ON(ei->magic != 0xDEADBEEF); |
| 32 | WARN_ON(ei->magic2 != 0xDEADBEAF); |
| 33 | return 0; |
| 34 | } |
| 35 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 36 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 37 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 38 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 39 | struct btrfs_inode_item *inode_item; |
| 40 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 41 | int ret; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 42 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 43 | path = btrfs_alloc_path(); |
| 44 | BUG_ON(!path); |
| 45 | btrfs_init_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 46 | mutex_lock(&root->fs_info->fs_mutex); |
| 47 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 48 | check_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 49 | ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 50 | if (ret) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 51 | btrfs_release_path(root, path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 52 | mutex_unlock(&root->fs_info->fs_mutex); |
| 53 | make_bad_inode(inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 54 | return; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 55 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 56 | check_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 57 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 58 | path->slots[0], |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 59 | struct btrfs_inode_item); |
| 60 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 61 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 62 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 63 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 64 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 65 | inode->i_size = btrfs_inode_size(inode_item); |
| 66 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 67 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 68 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 69 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 70 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 71 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 72 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 73 | inode->i_generation = btrfs_inode_generation(inode_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 74 | |
| 75 | btrfs_release_path(root, path); |
| 76 | btrfs_free_path(path); |
| 77 | inode_item = NULL; |
| 78 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 79 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 80 | check_inode(inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 81 | switch (inode->i_mode & S_IFMT) { |
| 82 | #if 0 |
| 83 | default: |
| 84 | init_special_inode(inode, inode->i_mode, |
| 85 | btrfs_inode_rdev(inode_item)); |
| 86 | break; |
| 87 | #endif |
| 88 | case S_IFREG: |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 89 | inode->i_mapping->a_ops = &btrfs_aops; |
| 90 | inode->i_fop = &btrfs_file_operations; |
| 91 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 92 | break; |
| 93 | case S_IFDIR: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 94 | inode->i_op = &btrfs_dir_inode_operations; |
| 95 | inode->i_fop = &btrfs_dir_file_operations; |
| 96 | break; |
| 97 | case S_IFLNK: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 98 | // inode->i_op = &page_symlink_inode_operations; |
| 99 | break; |
| 100 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 101 | check_inode(inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 102 | return; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 105 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 106 | struct btrfs_root *root, |
| 107 | struct inode *dir, |
| 108 | struct dentry *dentry) |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 109 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 110 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 111 | const char *name = dentry->d_name.name; |
| 112 | int name_len = dentry->d_name.len; |
| 113 | int ret; |
| 114 | u64 objectid; |
| 115 | struct btrfs_dir_item *di; |
| 116 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 117 | path = btrfs_alloc_path(); |
| 118 | BUG_ON(!path); |
| 119 | btrfs_init_path(path); |
| 120 | ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 121 | name, name_len, -1); |
| 122 | if (ret < 0) |
| 123 | goto err; |
| 124 | if (ret > 0) { |
| 125 | ret = -ENOENT; |
| 126 | goto err; |
| 127 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 128 | 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] | 129 | struct btrfs_dir_item); |
| 130 | objectid = btrfs_dir_objectid(di); |
| 131 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 132 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 133 | BUG_ON(ret); |
| 134 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 135 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 136 | btrfs_release_path(root, path); |
| 137 | btrfs_free_path(path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 138 | if (ret == 0) |
| 139 | inode_dec_link_count(dentry->d_inode); |
| 140 | return ret; |
| 141 | } |
| 142 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 143 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 144 | { |
| 145 | struct btrfs_root *root; |
| 146 | struct btrfs_trans_handle *trans; |
| 147 | int ret; |
| 148 | |
| 149 | root = btrfs_sb(dir->i_sb); |
| 150 | mutex_lock(&root->fs_info->fs_mutex); |
| 151 | trans = btrfs_start_transaction(root, 1); |
| 152 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 153 | btrfs_end_transaction(trans, root); |
| 154 | mutex_unlock(&root->fs_info->fs_mutex); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 159 | { |
| 160 | struct inode *inode = dentry->d_inode; |
| 161 | int err; |
| 162 | int ret; |
| 163 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 164 | struct btrfs_path *path; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 165 | struct btrfs_key key; |
| 166 | struct btrfs_trans_handle *trans; |
| 167 | struct btrfs_disk_key *found_key; |
| 168 | struct btrfs_leaf *leaf; |
| 169 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 170 | path = btrfs_alloc_path(); |
| 171 | BUG_ON(!path); |
| 172 | btrfs_init_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 173 | mutex_lock(&root->fs_info->fs_mutex); |
| 174 | trans = btrfs_start_transaction(root, 1); |
| 175 | key.objectid = inode->i_ino; |
| 176 | key.offset = (u64)-1; |
| 177 | key.flags = 0; |
| 178 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 179 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 180 | if (ret < 0) { |
| 181 | err = ret; |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | BUG_ON(ret == 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 186 | BUG_ON(path->slots[0] == 0); |
| 187 | path->slots[0]--; |
| 188 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 189 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 190 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) { |
| 191 | err = -ENOENT; |
| 192 | goto out; |
| 193 | } |
| 194 | if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || |
| 195 | btrfs_disk_key_offset(found_key) != 2) { |
| 196 | err = -ENOTEMPTY; |
| 197 | goto out; |
| 198 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 199 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 200 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 201 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 202 | key.offset = 1; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 203 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 204 | if (ret < 0) { |
| 205 | err = ret; |
| 206 | goto out; |
| 207 | } |
| 208 | if (ret > 0) { |
| 209 | err = -ENOTEMPTY; |
| 210 | goto out; |
| 211 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 212 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 213 | if (ret) { |
| 214 | err = ret; |
| 215 | goto out; |
| 216 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 217 | btrfs_release_path(root, path); |
| 218 | btrfs_free_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 219 | |
| 220 | /* now the directory is empty */ |
| 221 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 222 | if (!err) { |
| 223 | inode->i_size = 0; |
| 224 | } |
| 225 | out: |
| 226 | mutex_unlock(&root->fs_info->fs_mutex); |
| 227 | ret = btrfs_end_transaction(trans, root); |
| 228 | if (ret && !err) |
| 229 | err = ret; |
| 230 | return err; |
| 231 | } |
| 232 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 233 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 234 | struct btrfs_root *root, |
| 235 | struct inode *inode) |
| 236 | { |
| 237 | u64 objectid = inode->i_ino; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 238 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 239 | struct btrfs_inode_map_item *map; |
| 240 | struct btrfs_key stat_data_key; |
| 241 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 242 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 243 | clear_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 244 | |
| 245 | path = btrfs_alloc_path(); |
| 246 | BUG_ON(!path); |
| 247 | btrfs_init_path(path); |
| 248 | ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 249 | if (ret) { |
| 250 | if (ret > 0) |
| 251 | ret = -ENOENT; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 252 | goto error; |
| 253 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 254 | map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 255 | struct btrfs_inode_map_item); |
| 256 | btrfs_disk_key_to_cpu(&stat_data_key, &map->key); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 257 | ret = btrfs_del_item(trans, root->fs_info->inode_root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 258 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 259 | btrfs_release_path(root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 260 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 261 | ret = btrfs_lookup_inode(trans, root, path, objectid, -1); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 262 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 263 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 264 | BUG_ON(ret); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 265 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 266 | btrfs_release_path(root, path); |
| 267 | btrfs_free_path(path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 268 | return ret; |
| 269 | } |
| 270 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 271 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 272 | struct btrfs_root *root, |
| 273 | struct inode *inode) |
| 274 | { |
| 275 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 276 | struct btrfs_path *path; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 277 | struct btrfs_key key; |
| 278 | struct btrfs_disk_key *found_key; |
| 279 | struct btrfs_leaf *leaf; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 280 | struct btrfs_file_extent_item *fi = NULL; |
| 281 | u64 extent_start = 0; |
| 282 | u64 extent_num_blocks = 0; |
| 283 | int found_extent; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 284 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 285 | path = btrfs_alloc_path(); |
| 286 | BUG_ON(!path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 287 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 288 | key.objectid = inode->i_ino; |
| 289 | key.offset = (u64)-1; |
| 290 | key.flags = 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 291 | btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 292 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 293 | btrfs_init_path(path); |
| 294 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 295 | if (ret < 0) { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 296 | goto error; |
| 297 | } |
| 298 | if (ret > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 299 | BUG_ON(path->slots[0] == 0); |
| 300 | path->slots[0]--; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 301 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 302 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 303 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 304 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 305 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 306 | if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY && |
| 307 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 308 | break; |
| 309 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 310 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 311 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 312 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 313 | path->slots[0], |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 314 | struct btrfs_file_extent_item); |
| 315 | extent_start = btrfs_file_extent_disk_blocknr(fi); |
| 316 | extent_num_blocks = |
| 317 | btrfs_file_extent_disk_num_blocks(fi); |
| 318 | inode->i_blocks -= |
| 319 | btrfs_file_extent_num_blocks(fi) >> 9; |
| 320 | found_extent = 1; |
| 321 | } else { |
| 322 | found_extent = 0; |
| 323 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 324 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 325 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 326 | btrfs_release_path(root, path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 327 | if (found_extent) { |
| 328 | ret = btrfs_free_extent(trans, root, extent_start, |
| 329 | extent_num_blocks, 0); |
| 330 | BUG_ON(ret); |
| 331 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 332 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 333 | ret = 0; |
| 334 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 335 | btrfs_release_path(root, path); |
| 336 | btrfs_free_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 340 | static void btrfs_delete_inode(struct inode *inode) |
| 341 | { |
| 342 | struct btrfs_trans_handle *trans; |
| 343 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 344 | int ret; |
| 345 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 346 | truncate_inode_pages(&inode->i_data, 0); |
| 347 | if (is_bad_inode(inode)) { |
| 348 | goto no_delete; |
| 349 | } |
| 350 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 351 | mutex_lock(&root->fs_info->fs_mutex); |
| 352 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 353 | if (S_ISREG(inode->i_mode)) { |
| 354 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 355 | BUG_ON(ret); |
| 356 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 357 | btrfs_free_inode(trans, root, inode); |
| 358 | btrfs_end_transaction(trans, root); |
| 359 | mutex_unlock(&root->fs_info->fs_mutex); |
| 360 | return; |
| 361 | no_delete: |
| 362 | clear_inode(inode); |
| 363 | } |
| 364 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 365 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 366 | ino_t *ino) |
| 367 | { |
| 368 | const char *name = dentry->d_name.name; |
| 369 | int namelen = dentry->d_name.len; |
| 370 | struct btrfs_dir_item *di; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 371 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 372 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 373 | int ret; |
| 374 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 375 | path = btrfs_alloc_path(); |
| 376 | BUG_ON(!path); |
| 377 | btrfs_init_path(path); |
| 378 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 379 | namelen, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 380 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 381 | *ino = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 382 | ret = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 383 | goto out; |
| 384 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 385 | 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] | 386 | struct btrfs_dir_item); |
| 387 | *ino = btrfs_dir_objectid(di); |
| 388 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 389 | btrfs_release_path(root, path); |
| 390 | btrfs_free_path(path); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 391 | check_inode(dir); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 396 | struct nameidata *nd) |
| 397 | { |
| 398 | struct inode * inode; |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 399 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 400 | ino_t ino; |
| 401 | int ret; |
| 402 | |
| 403 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 404 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 405 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 406 | ret = btrfs_inode_by_name(dir, dentry, &ino); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 407 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 408 | if (ret < 0) |
| 409 | return ERR_PTR(ret); |
| 410 | inode = NULL; |
| 411 | if (ino) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 412 | inode = iget(dir->i_sb, ino); |
| 413 | if (!inode) |
| 414 | return ERR_PTR(-EACCES); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 415 | check_inode(inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 416 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 417 | check_inode(dir); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 418 | return d_splice_alias(inode, dentry); |
| 419 | } |
| 420 | |
| 421 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 422 | { |
| 423 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 424 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 425 | struct btrfs_item *item; |
| 426 | struct btrfs_dir_item *di; |
| 427 | struct btrfs_key key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 428 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 429 | int ret; |
| 430 | u32 nritems; |
| 431 | struct btrfs_leaf *leaf; |
| 432 | int slot; |
| 433 | int advance; |
| 434 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 435 | int over = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 436 | |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 437 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 438 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 439 | key.flags = 0; |
| 440 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
| 441 | key.offset = filp->f_pos; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 442 | path = btrfs_alloc_path(); |
| 443 | btrfs_init_path(path); |
| 444 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 445 | if (ret < 0) { |
| 446 | goto err; |
| 447 | } |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 448 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 449 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 450 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 451 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 452 | slot = path->slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 453 | if (advance || slot >= nritems) { |
| 454 | if (slot >= nritems -1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 455 | ret = btrfs_next_leaf(root, path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 456 | if (ret) |
| 457 | break; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 458 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 459 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 460 | slot = path->slots[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 461 | } else { |
| 462 | slot++; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 463 | path->slots[0]++; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | advance = 1; |
| 467 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 468 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 469 | break; |
| 470 | if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY) |
| 471 | continue; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 472 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 473 | continue; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 474 | |
| 475 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 476 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 477 | over = filldir(dirent, (const char *)(di + 1), |
| 478 | btrfs_dir_name_len(di), |
| 479 | btrfs_disk_key_offset(&item->key), |
| 480 | btrfs_dir_objectid(di), d_type); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 481 | if (over) { |
| 482 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 483 | break; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 484 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 485 | filp->f_pos = btrfs_disk_key_offset(&item->key) + 1; |
| 486 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 487 | ret = 0; |
| 488 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 489 | btrfs_release_path(root, path); |
| 490 | btrfs_free_path(path); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 491 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static void btrfs_put_super (struct super_block * sb) |
| 496 | { |
| 497 | struct btrfs_root *root = btrfs_sb(sb); |
| 498 | int ret; |
| 499 | |
| 500 | ret = close_ctree(root); |
| 501 | if (ret) { |
| 502 | printk("close ctree returns %d\n", ret); |
| 503 | } |
| 504 | sb->s_fs_info = NULL; |
| 505 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 506 | |
| 507 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 508 | { |
| 509 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 510 | struct dentry * root_dentry; |
| 511 | struct btrfs_super_block *disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 512 | struct btrfs_root *root; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 513 | |
| 514 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 515 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 516 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 517 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 518 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 519 | root = open_ctree(sb); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 520 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 521 | if (!root) { |
| 522 | printk("btrfs: open_ctree failed\n"); |
| 523 | return -EIO; |
| 524 | } |
Chris Mason | 87cbda5 | 2007-03-28 19:44:27 -0400 | [diff] [blame] | 525 | sb->s_fs_info = root; |
| 526 | disk_super = root->fs_info->disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 527 | printk("read in super total blocks %Lu root %Lu\n", |
| 528 | btrfs_super_total_blocks(disk_super), |
| 529 | btrfs_super_root_dir(disk_super)); |
| 530 | |
| 531 | inode = iget_locked(sb, btrfs_super_root_dir(disk_super)); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 532 | if (!inode) |
| 533 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 534 | if (inode->i_state & I_NEW) { |
| 535 | btrfs_read_locked_inode(inode); |
| 536 | unlock_new_inode(inode); |
| 537 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 538 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 539 | root_dentry = d_alloc_root(inode); |
| 540 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 541 | iput(inode); |
| 542 | return -ENOMEM; |
| 543 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 544 | sb->s_root = root_dentry; |
| 545 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 549 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 550 | struct inode *inode) |
| 551 | { |
| 552 | btrfs_set_inode_uid(item, inode->i_uid); |
| 553 | btrfs_set_inode_gid(item, inode->i_gid); |
| 554 | btrfs_set_inode_size(item, inode->i_size); |
| 555 | btrfs_set_inode_mode(item, inode->i_mode); |
| 556 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 557 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 558 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 559 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 560 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 561 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 562 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 563 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 564 | btrfs_set_inode_generation(item, inode->i_generation); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 565 | check_inode(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 566 | } |
| 567 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 568 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 569 | struct btrfs_root *root, |
| 570 | struct inode *inode) |
| 571 | { |
| 572 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 573 | struct btrfs_path *path; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 574 | int ret; |
| 575 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 576 | path = btrfs_alloc_path(); |
| 577 | BUG_ON(!path); |
| 578 | btrfs_init_path(path); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 579 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 580 | ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 581 | if (ret) { |
| 582 | if (ret > 0) |
| 583 | ret = -ENOENT; |
| 584 | goto failed; |
| 585 | } |
| 586 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 587 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 588 | path->slots[0], |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 589 | struct btrfs_inode_item); |
| 590 | |
| 591 | fill_inode_item(inode_item, inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 592 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 593 | failed: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 594 | btrfs_release_path(root, path); |
| 595 | btrfs_free_path(path); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 596 | check_inode(inode); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 601 | { |
| 602 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 603 | struct btrfs_trans_handle *trans; |
| 604 | int ret; |
| 605 | |
| 606 | mutex_lock(&root->fs_info->fs_mutex); |
| 607 | trans = btrfs_start_transaction(root, 1); |
| 608 | ret = btrfs_update_inode(trans, root, inode); |
| 609 | if (wait) |
| 610 | btrfs_commit_transaction(trans, root); |
| 611 | else |
| 612 | btrfs_end_transaction(trans, root); |
| 613 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 614 | check_inode(inode); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 615 | return ret; |
| 616 | } |
| 617 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 618 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 619 | struct inode *dir, int mode) |
| 620 | { |
| 621 | struct inode *inode; |
| 622 | struct btrfs_inode_item inode_item; |
| 623 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 624 | struct btrfs_key key; |
| 625 | int ret; |
| 626 | u64 objectid; |
| 627 | |
| 628 | inode = new_inode(dir->i_sb); |
| 629 | if (!inode) |
| 630 | return ERR_PTR(-ENOMEM); |
| 631 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 632 | check_inode(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 633 | ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 634 | BUG_ON(ret); |
| 635 | |
| 636 | inode->i_uid = current->fsuid; |
| 637 | inode->i_gid = current->fsgid; |
| 638 | inode->i_mode = mode; |
| 639 | inode->i_ino = objectid; |
| 640 | inode->i_blocks = 0; |
| 641 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 642 | fill_inode_item(&inode_item, inode); |
| 643 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 644 | key.objectid = objectid; |
| 645 | key.flags = 0; |
| 646 | key.offset = 0; |
| 647 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 648 | ret = btrfs_insert_inode_map(trans, root, objectid, &key); |
| 649 | BUG_ON(ret); |
| 650 | |
| 651 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 652 | BUG_ON(ret); |
| 653 | |
| 654 | insert_inode_hash(inode); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 655 | check_inode(inode); |
| 656 | check_inode(dir); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 657 | return inode; |
| 658 | } |
| 659 | |
| 660 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 661 | struct dentry *dentry, struct inode *inode) |
| 662 | { |
| 663 | int ret; |
| 664 | ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb), |
| 665 | dentry->d_name.name, dentry->d_name.len, |
| 666 | dentry->d_parent->d_inode->i_ino, |
| 667 | inode->i_ino, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 668 | if (ret == 0) { |
| 669 | dentry->d_parent->d_inode->i_size += dentry->d_name.len; |
| 670 | ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb), |
| 671 | dentry->d_parent->d_inode); |
| 672 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 673 | check_inode(inode); |
| 674 | check_inode(dentry->d_parent->d_inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 679 | struct dentry *dentry, struct inode *inode) |
| 680 | { |
| 681 | int err = btrfs_add_link(trans, dentry, inode); |
| 682 | if (!err) { |
| 683 | d_instantiate(dentry, inode); |
| 684 | return 0; |
| 685 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 686 | if (err > 0) |
| 687 | err = -EEXIST; |
| 688 | check_inode(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 689 | return err; |
| 690 | } |
| 691 | |
| 692 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 693 | int mode, struct nameidata *nd) |
| 694 | { |
| 695 | struct btrfs_trans_handle *trans; |
| 696 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 697 | struct inode *inode; |
| 698 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 699 | int drop_inode = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 700 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 701 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 702 | trans = btrfs_start_transaction(root, 1); |
| 703 | inode = btrfs_new_inode(trans, dir, mode); |
| 704 | err = PTR_ERR(inode); |
| 705 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 706 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 707 | // FIXME mark the inode dirty |
| 708 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 709 | if (err) |
| 710 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 711 | else { |
| 712 | inode->i_mapping->a_ops = &btrfs_aops; |
| 713 | inode->i_fop = &btrfs_file_operations; |
| 714 | inode->i_op = &btrfs_file_inode_operations; |
| 715 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 716 | dir->i_sb->s_dirt = 1; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 717 | out_unlock: |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 718 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 719 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 720 | check_inode(inode); |
| 721 | check_inode(dir); |
| 722 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 723 | if (drop_inode) { |
| 724 | inode_dec_link_count(inode); |
| 725 | iput(inode); |
| 726 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 727 | return err; |
| 728 | } |
| 729 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 730 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 731 | struct inode *inode, struct inode *dir) |
| 732 | { |
| 733 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 734 | int ret; |
| 735 | char buf[2]; |
| 736 | buf[0] = '.'; |
| 737 | buf[1] = '.'; |
| 738 | |
| 739 | ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino, |
| 740 | inode->i_ino, 1); |
| 741 | if (ret) |
| 742 | goto error; |
| 743 | ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino, |
| 744 | dir->i_ino, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 745 | if (ret) |
| 746 | goto error; |
| 747 | inode->i_size = 3; |
| 748 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 749 | error: |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 754 | { |
| 755 | struct inode *inode; |
| 756 | struct btrfs_trans_handle *trans; |
| 757 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 758 | int err = 0; |
| 759 | int drop_on_err = 0; |
| 760 | |
| 761 | mutex_lock(&root->fs_info->fs_mutex); |
| 762 | trans = btrfs_start_transaction(root, 1); |
| 763 | if (IS_ERR(trans)) { |
| 764 | err = PTR_ERR(trans); |
| 765 | goto out_unlock; |
| 766 | } |
| 767 | inode = btrfs_new_inode(trans, dir, S_IFDIR | mode); |
| 768 | if (IS_ERR(inode)) { |
| 769 | err = PTR_ERR(inode); |
| 770 | goto out_fail; |
| 771 | } |
| 772 | drop_on_err = 1; |
| 773 | inode->i_op = &btrfs_dir_inode_operations; |
| 774 | inode->i_fop = &btrfs_dir_file_operations; |
| 775 | |
| 776 | err = btrfs_make_empty_dir(trans, inode, dir); |
| 777 | if (err) |
| 778 | goto out_fail; |
| 779 | err = btrfs_add_link(trans, dentry, inode); |
| 780 | if (err) |
| 781 | goto out_fail; |
| 782 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 783 | drop_on_err = 0; |
| 784 | |
| 785 | out_fail: |
| 786 | btrfs_end_transaction(trans, root); |
| 787 | out_unlock: |
| 788 | mutex_unlock(&root->fs_info->fs_mutex); |
| 789 | if (drop_on_err) |
| 790 | iput(inode); |
| 791 | return err; |
| 792 | } |
| 793 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 794 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 795 | { |
| 796 | struct btrfs_trans_handle *trans; |
| 797 | struct btrfs_root *root; |
| 798 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 799 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 800 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 801 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 802 | if (!wait) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 803 | // filemap_flush(root->fs_info->btree_inode->i_mapping); |
| 804 | filemap_flush(root->fs_info->sb->s_bdev->bd_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 805 | return 0; |
| 806 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 807 | filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 808 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 809 | trans = btrfs_start_transaction(root, 1); |
| 810 | ret = btrfs_commit_transaction(trans, root); |
| 811 | sb->s_dirt = 0; |
| 812 | BUG_ON(ret); |
| 813 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 814 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 818 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 819 | struct buffer_head *result, int create) |
| 820 | { |
| 821 | int ret; |
| 822 | int err = 0; |
| 823 | u64 blocknr; |
| 824 | u64 extent_start = 0; |
| 825 | u64 extent_end = 0; |
| 826 | u64 objectid = inode->i_ino; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 827 | struct btrfs_path *path; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 828 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 829 | struct btrfs_trans_handle *trans = NULL; |
| 830 | struct btrfs_file_extent_item *item; |
| 831 | struct btrfs_leaf *leaf; |
| 832 | struct btrfs_disk_key *found_key; |
| 833 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 834 | path = btrfs_alloc_path(); |
| 835 | BUG_ON(!path); |
| 836 | btrfs_init_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 837 | if (create) |
| 838 | trans = btrfs_start_transaction(root, 1); |
| 839 | |
| 840 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 841 | ret = btrfs_lookup_file_extent(trans, root, path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 842 | inode->i_ino, |
| 843 | iblock << inode->i_blkbits, 0); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 844 | if (ret < 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 845 | err = ret; |
| 846 | goto out; |
| 847 | } |
| 848 | |
| 849 | if (ret != 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 850 | if (path->slots[0] == 0) { |
| 851 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 852 | goto allocate; |
| 853 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 854 | path->slots[0]--; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 855 | } |
| 856 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 857 | 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] | 858 | struct btrfs_file_extent_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 859 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 860 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 861 | blocknr += btrfs_file_extent_offset(item); |
| 862 | |
| 863 | /* exact match found, use it */ |
| 864 | if (ret == 0) { |
| 865 | err = 0; |
| 866 | map_bh(result, inode->i_sb, blocknr); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | /* are we inside the extent that was found? */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 871 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 872 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 873 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 874 | extent_end = 0; |
| 875 | extent_start = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 876 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 877 | goto allocate; |
| 878 | } |
| 879 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 880 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 881 | extent_start = extent_start >> inode->i_blkbits; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 882 | extent_start += btrfs_file_extent_offset(item); |
| 883 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 884 | if (iblock >= extent_start && iblock < extent_end) { |
| 885 | err = 0; |
| 886 | map_bh(result, inode->i_sb, blocknr + iblock - extent_start); |
| 887 | goto out; |
| 888 | } |
| 889 | allocate: |
| 890 | /* ok, create a new extent */ |
| 891 | if (!create) { |
| 892 | err = 0; |
| 893 | goto out; |
| 894 | } |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 895 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
| 896 | iblock << inode->i_blkbits, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 897 | 1, extent_end, &blocknr); |
| 898 | if (ret) { |
| 899 | err = ret; |
| 900 | goto out; |
| 901 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 902 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 903 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 904 | map_bh(result, inode->i_sb, blocknr); |
| 905 | |
| 906 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame^] | 907 | btrfs_release_path(root, path); |
| 908 | btrfs_free_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 909 | if (trans) |
| 910 | btrfs_end_transaction(trans, root); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 911 | return err; |
| 912 | } |
| 913 | |
| 914 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 915 | struct buffer_head *result, int create) |
| 916 | { |
| 917 | int err; |
| 918 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 919 | mutex_lock(&root->fs_info->fs_mutex); |
| 920 | err = btrfs_get_block_lock(inode, iblock, result, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 921 | mutex_unlock(&root->fs_info->fs_mutex); |
| 922 | return err; |
| 923 | } |
| 924 | |
| 925 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 926 | unsigned from, unsigned to) |
| 927 | { |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 928 | WARN_ON(1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 929 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 930 | } |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 931 | static int btrfs_commit_write(struct file *file, struct page *page, |
| 932 | unsigned from, unsigned to) |
| 933 | { |
| 934 | WARN_ON(1); |
| 935 | return nobh_commit_write(file, page, from, to); |
| 936 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 937 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 938 | static void btrfs_write_super(struct super_block *sb) |
| 939 | { |
| 940 | btrfs_sync_fs(sb, 1); |
| 941 | } |
| 942 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 943 | static int btrfs_readpage(struct file *file, struct page *page) |
| 944 | { |
| 945 | return mpage_readpage(page, btrfs_get_block); |
| 946 | } |
| 947 | |
| 948 | static int btrfs_readpages(struct file *file, struct address_space *mapping, |
| 949 | struct list_head *pages, unsigned nr_pages) |
| 950 | { |
| 951 | return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block); |
| 952 | } |
| 953 | |
| 954 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 955 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 956 | return nobh_writepage(page, btrfs_get_block, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 957 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 958 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 959 | static void btrfs_truncate(struct inode *inode) |
| 960 | { |
| 961 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 962 | int ret; |
| 963 | struct btrfs_trans_handle *trans; |
| 964 | |
| 965 | if (!S_ISREG(inode->i_mode)) |
| 966 | return; |
| 967 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 968 | return; |
| 969 | |
| 970 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
| 971 | |
| 972 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 973 | mutex_lock(&root->fs_info->fs_mutex); |
| 974 | trans = btrfs_start_transaction(root, 1); |
| 975 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 976 | BUG_ON(ret); |
| 977 | ret = btrfs_end_transaction(trans, root); |
| 978 | BUG_ON(ret); |
| 979 | mutex_unlock(&root->fs_info->fs_mutex); |
| 980 | mark_inode_dirty(inode); |
| 981 | } |
| 982 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 983 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 984 | struct page **prepared_pages, |
| 985 | const char __user * buf) |
| 986 | { |
| 987 | long page_fault = 0; |
| 988 | int i; |
| 989 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 990 | |
| 991 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 992 | size_t count = min_t(size_t, |
| 993 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 994 | struct page *page = prepared_pages[i]; |
| 995 | fault_in_pages_readable(buf, count); |
| 996 | |
| 997 | /* Copy data from userspace to the current page */ |
| 998 | kmap(page); |
| 999 | page_fault = __copy_from_user(page_address(page) + offset, |
| 1000 | buf, count); |
| 1001 | /* Flush processor's dcache for this page */ |
| 1002 | flush_dcache_page(page); |
| 1003 | kunmap(page); |
| 1004 | buf += count; |
| 1005 | write_bytes -= count; |
| 1006 | |
| 1007 | if (page_fault) |
| 1008 | break; |
| 1009 | } |
| 1010 | return page_fault ? -EFAULT : 0; |
| 1011 | } |
| 1012 | |
| 1013 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 1014 | { |
| 1015 | size_t i; |
| 1016 | for (i = 0; i < num_pages; i++) { |
| 1017 | if (!pages[i]) |
| 1018 | break; |
| 1019 | unlock_page(pages[i]); |
| 1020 | mark_page_accessed(pages[i]); |
| 1021 | page_cache_release(pages[i]); |
| 1022 | } |
| 1023 | } |
| 1024 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 1025 | struct btrfs_root *root, |
| 1026 | struct file *file, |
| 1027 | struct page **pages, |
| 1028 | size_t num_pages, |
| 1029 | loff_t pos, |
| 1030 | size_t write_bytes) |
| 1031 | { |
| 1032 | int i; |
| 1033 | int offset; |
| 1034 | int err = 0; |
| 1035 | int ret; |
| 1036 | int this_write; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1037 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1038 | |
| 1039 | for (i = 0; i < num_pages; i++) { |
| 1040 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1041 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1042 | /* FIXME, one block at a time */ |
| 1043 | |
| 1044 | mutex_lock(&root->fs_info->fs_mutex); |
| 1045 | trans = btrfs_start_transaction(root, 1); |
| 1046 | btrfs_csum_file_block(trans, root, inode->i_ino, |
| 1047 | pages[i]->index << PAGE_CACHE_SHIFT, |
| 1048 | kmap(pages[i]), PAGE_CACHE_SIZE); |
| 1049 | kunmap(pages[i]); |
| 1050 | SetPageChecked(pages[i]); |
| 1051 | ret = btrfs_end_transaction(trans, root); |
| 1052 | BUG_ON(ret); |
| 1053 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1054 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1055 | ret = nobh_commit_write(file, pages[i], offset, |
| 1056 | offset + this_write); |
| 1057 | pos += this_write; |
| 1058 | if (ret) { |
| 1059 | err = ret; |
| 1060 | goto failed; |
| 1061 | } |
| 1062 | WARN_ON(this_write > write_bytes); |
| 1063 | write_bytes -= this_write; |
| 1064 | } |
| 1065 | failed: |
| 1066 | return err; |
| 1067 | } |
| 1068 | |
| 1069 | static int prepare_pages(struct btrfs_trans_handle *trans, |
| 1070 | struct btrfs_root *root, |
| 1071 | struct file *file, |
| 1072 | struct page **pages, |
| 1073 | size_t num_pages, |
| 1074 | loff_t pos, |
| 1075 | size_t write_bytes) |
| 1076 | { |
| 1077 | int i; |
| 1078 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 1079 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1080 | int offset; |
| 1081 | int err = 0; |
| 1082 | int ret; |
| 1083 | int this_write; |
| 1084 | loff_t isize = i_size_read(inode); |
| 1085 | |
| 1086 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 1087 | |
| 1088 | for (i = 0; i < num_pages; i++) { |
| 1089 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 1090 | if (!pages[i]) { |
| 1091 | err = -ENOMEM; |
| 1092 | goto failed_release; |
| 1093 | } |
| 1094 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1095 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
| 1096 | ret = nobh_prepare_write(pages[i], offset, |
| 1097 | offset + this_write, |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1098 | btrfs_get_block); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1099 | pos += this_write; |
| 1100 | if (ret) { |
| 1101 | err = ret; |
| 1102 | goto failed_truncate; |
| 1103 | } |
| 1104 | WARN_ON(this_write > write_bytes); |
| 1105 | write_bytes -= this_write; |
| 1106 | } |
| 1107 | return 0; |
| 1108 | |
| 1109 | failed_release: |
| 1110 | btrfs_drop_pages(pages, num_pages); |
| 1111 | return err; |
| 1112 | |
| 1113 | failed_truncate: |
| 1114 | btrfs_drop_pages(pages, num_pages); |
| 1115 | if (pos > isize) |
| 1116 | vmtruncate(inode, isize); |
| 1117 | return err; |
| 1118 | } |
| 1119 | |
| 1120 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 1121 | size_t count, loff_t *ppos) |
| 1122 | { |
| 1123 | loff_t pos; |
| 1124 | size_t num_written = 0; |
| 1125 | int err = 0; |
| 1126 | int ret = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1127 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1128 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 1129 | struct page *pages[1]; |
| 1130 | |
| 1131 | if (file->f_flags & O_DIRECT) |
| 1132 | return -EINVAL; |
| 1133 | pos = *ppos; |
| 1134 | |
| 1135 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1136 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1137 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1138 | if (err) |
| 1139 | goto out; |
| 1140 | if (count == 0) |
| 1141 | goto out; |
| 1142 | err = remove_suid(file->f_path.dentry); |
| 1143 | if (err) |
| 1144 | goto out; |
| 1145 | file_update_time(file); |
| 1146 | mutex_lock(&inode->i_mutex); |
| 1147 | while(count > 0) { |
| 1148 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1149 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1150 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 1151 | PAGE_CACHE_SHIFT; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1152 | ret = prepare_pages(NULL, root, file, pages, num_pages, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1153 | pos, write_bytes); |
| 1154 | BUG_ON(ret); |
| 1155 | ret = btrfs_copy_from_user(pos, num_pages, |
| 1156 | write_bytes, pages, buf); |
| 1157 | BUG_ON(ret); |
| 1158 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1159 | ret = dirty_and_release_pages(NULL, root, file, pages, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1160 | num_pages, pos, write_bytes); |
| 1161 | BUG_ON(ret); |
| 1162 | btrfs_drop_pages(pages, num_pages); |
| 1163 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1164 | buf += write_bytes; |
| 1165 | count -= write_bytes; |
| 1166 | pos += write_bytes; |
| 1167 | num_written += write_bytes; |
| 1168 | |
| 1169 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1170 | cond_resched(); |
| 1171 | } |
| 1172 | mutex_unlock(&inode->i_mutex); |
| 1173 | out: |
| 1174 | *ppos = pos; |
| 1175 | current->backing_dev_info = NULL; |
| 1176 | return num_written ? num_written : err; |
| 1177 | } |
| 1178 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1179 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
| 1180 | unsigned long offset, unsigned long size) |
| 1181 | { |
| 1182 | char *kaddr; |
| 1183 | unsigned long left, count = desc->count; |
| 1184 | |
| 1185 | if (size > count) |
| 1186 | size = count; |
| 1187 | |
| 1188 | if (!PageChecked(page)) { |
| 1189 | /* FIXME, do it per block */ |
| 1190 | struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb); |
| 1191 | int ret = btrfs_csum_verify_file_block(root, |
| 1192 | page->mapping->host->i_ino, |
| 1193 | page->index << PAGE_CACHE_SHIFT, |
| 1194 | kmap(page), PAGE_CACHE_SIZE); |
| 1195 | if (ret) { |
| 1196 | printk("failed to verify ino %lu page %lu\n", |
| 1197 | page->mapping->host->i_ino, |
| 1198 | page->index); |
| 1199 | memset(page_address(page), 0, PAGE_CACHE_SIZE); |
| 1200 | } |
| 1201 | SetPageChecked(page); |
| 1202 | kunmap(page); |
| 1203 | } |
| 1204 | /* |
| 1205 | * Faults on the destination of a read are common, so do it before |
| 1206 | * taking the kmap. |
| 1207 | */ |
| 1208 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 1209 | kaddr = kmap_atomic(page, KM_USER0); |
| 1210 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 1211 | kaddr + offset, size); |
| 1212 | kunmap_atomic(kaddr, KM_USER0); |
| 1213 | if (left == 0) |
| 1214 | goto success; |
| 1215 | } |
| 1216 | |
| 1217 | /* Do it the slow way */ |
| 1218 | kaddr = kmap(page); |
| 1219 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 1220 | kunmap(page); |
| 1221 | |
| 1222 | if (left) { |
| 1223 | size -= left; |
| 1224 | desc->error = -EFAULT; |
| 1225 | } |
| 1226 | success: |
| 1227 | desc->count = count - size; |
| 1228 | desc->written += size; |
| 1229 | desc->arg.buf += size; |
| 1230 | return size; |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * btrfs_file_aio_read - filesystem read routine |
| 1235 | * @iocb: kernel I/O control block |
| 1236 | * @iov: io vector request |
| 1237 | * @nr_segs: number of segments in the iovec |
| 1238 | * @pos: current file position |
| 1239 | */ |
| 1240 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 1241 | unsigned long nr_segs, loff_t pos) |
| 1242 | { |
| 1243 | struct file *filp = iocb->ki_filp; |
| 1244 | ssize_t retval; |
| 1245 | unsigned long seg; |
| 1246 | size_t count; |
| 1247 | loff_t *ppos = &iocb->ki_pos; |
| 1248 | |
| 1249 | count = 0; |
| 1250 | for (seg = 0; seg < nr_segs; seg++) { |
| 1251 | const struct iovec *iv = &iov[seg]; |
| 1252 | |
| 1253 | /* |
| 1254 | * If any segment has a negative length, or the cumulative |
| 1255 | * length ever wraps negative then return -EINVAL. |
| 1256 | */ |
| 1257 | count += iv->iov_len; |
| 1258 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 1259 | return -EINVAL; |
| 1260 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 1261 | continue; |
| 1262 | if (seg == 0) |
| 1263 | return -EFAULT; |
| 1264 | nr_segs = seg; |
| 1265 | count -= iv->iov_len; /* This segment is no good */ |
| 1266 | break; |
| 1267 | } |
| 1268 | retval = 0; |
| 1269 | if (count) { |
| 1270 | for (seg = 0; seg < nr_segs; seg++) { |
| 1271 | read_descriptor_t desc; |
| 1272 | |
| 1273 | desc.written = 0; |
| 1274 | desc.arg.buf = iov[seg].iov_base; |
| 1275 | desc.count = iov[seg].iov_len; |
| 1276 | if (desc.count == 0) |
| 1277 | continue; |
| 1278 | desc.error = 0; |
| 1279 | do_generic_file_read(filp, ppos, &desc, |
| 1280 | btrfs_read_actor); |
| 1281 | retval += desc.written; |
| 1282 | if (desc.error) { |
| 1283 | retval = retval ?: desc.error; |
| 1284 | break; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | return retval; |
| 1289 | } |
| 1290 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1291 | static struct kmem_cache *btrfs_inode_cachep; |
| 1292 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 1293 | struct kmem_cache *btrfs_transaction_cachep; |
| 1294 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 1295 | struct kmem_cache *btrfs_path_cachep; |
| 1296 | |
| 1297 | /* |
| 1298 | * Called inside transaction, so use GFP_NOFS |
| 1299 | */ |
| 1300 | static struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 1301 | { |
| 1302 | struct btrfs_inode *ei; |
| 1303 | |
| 1304 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 1305 | if (!ei) |
| 1306 | return NULL; |
| 1307 | ei->magic = 0xDEADBEEF; |
| 1308 | ei->magic2 = 0xDEADBEAF; |
| 1309 | return &ei->vfs_inode; |
| 1310 | } |
| 1311 | |
| 1312 | static void btrfs_destroy_inode(struct inode *inode) |
| 1313 | { |
| 1314 | struct btrfs_inode *ei = BTRFS_I(inode); |
| 1315 | WARN_ON(ei->magic != 0xDEADBEEF); |
| 1316 | WARN_ON(ei->magic2 != 0xDEADBEAF); |
| 1317 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 1318 | WARN_ON(inode->i_ino == 1); |
| 1319 | WARN_ON(inode->i_data.nrpages); |
| 1320 | |
| 1321 | ei->magic = 0; |
| 1322 | ei->magic2 = 0; |
| 1323 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 1324 | } |
| 1325 | |
| 1326 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 1327 | unsigned long flags) |
| 1328 | { |
| 1329 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 1330 | |
| 1331 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 1332 | SLAB_CTOR_CONSTRUCTOR) { |
| 1333 | inode_init_once(&ei->vfs_inode); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | static int init_inodecache(void) |
| 1338 | { |
| 1339 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 1340 | sizeof(struct btrfs_inode), |
| 1341 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1342 | SLAB_MEM_SPREAD), |
| 1343 | init_once, NULL); |
| 1344 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 1345 | sizeof(struct btrfs_trans_handle), |
| 1346 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1347 | SLAB_MEM_SPREAD), |
| 1348 | NULL, NULL); |
| 1349 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 1350 | sizeof(struct btrfs_transaction), |
| 1351 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1352 | SLAB_MEM_SPREAD), |
| 1353 | NULL, NULL); |
| 1354 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 1355 | sizeof(struct btrfs_transaction), |
| 1356 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1357 | SLAB_MEM_SPREAD), |
| 1358 | NULL, NULL); |
| 1359 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", |
| 1360 | 256, |
| 1361 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 1362 | SLAB_MEM_SPREAD | |
| 1363 | SLAB_DESTROY_BY_RCU), |
| 1364 | NULL, NULL); |
| 1365 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || |
| 1366 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) |
| 1367 | return -ENOMEM; |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | static void destroy_inodecache(void) |
| 1372 | { |
| 1373 | kmem_cache_destroy(btrfs_inode_cachep); |
| 1374 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 1375 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 1376 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 1377 | kmem_cache_destroy(btrfs_path_cachep); |
| 1378 | } |
| 1379 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1380 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 1381 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 1382 | { |
| 1383 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 1384 | btrfs_fill_super, mnt); |
| 1385 | } |
| 1386 | |
| 1387 | static struct file_system_type btrfs_fs_type = { |
| 1388 | .owner = THIS_MODULE, |
| 1389 | .name = "btrfs", |
| 1390 | .get_sb = btrfs_get_sb, |
| 1391 | .kill_sb = kill_block_super, |
| 1392 | .fs_flags = FS_REQUIRES_DEV, |
| 1393 | }; |
| 1394 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1395 | static struct super_operations btrfs_super_ops = { |
| 1396 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 1397 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1398 | .put_super = btrfs_put_super, |
| 1399 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1400 | .write_super = btrfs_write_super, |
| 1401 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 1402 | .write_inode = btrfs_write_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1403 | .alloc_inode = btrfs_alloc_inode, |
| 1404 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1405 | }; |
| 1406 | |
| 1407 | static struct inode_operations btrfs_dir_inode_operations = { |
| 1408 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1409 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 1410 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 1411 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 1412 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1413 | }; |
| 1414 | |
| 1415 | static struct file_operations btrfs_dir_file_operations = { |
| 1416 | .llseek = generic_file_llseek, |
| 1417 | .read = generic_read_dir, |
| 1418 | .readdir = btrfs_readdir, |
| 1419 | }; |
| 1420 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1421 | static struct address_space_operations btrfs_aops = { |
| 1422 | .readpage = btrfs_readpage, |
| 1423 | .readpages = btrfs_readpages, |
| 1424 | .writepage = btrfs_writepage, |
| 1425 | .sync_page = block_sync_page, |
| 1426 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1427 | .commit_write = btrfs_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1428 | }; |
| 1429 | |
| 1430 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1431 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1432 | }; |
| 1433 | |
| 1434 | static struct file_operations btrfs_file_operations = { |
| 1435 | .llseek = generic_file_llseek, |
| 1436 | .read = do_sync_read, |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1437 | .aio_read = btrfs_file_aio_read, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1438 | .write = btrfs_file_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1439 | .mmap = generic_file_mmap, |
| 1440 | .open = generic_file_open, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1441 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1442 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1443 | static int __init init_btrfs_fs(void) |
| 1444 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1445 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1446 | printk("btrfs loaded!\n"); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1447 | err = init_inodecache(); |
| 1448 | if (err) |
| 1449 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1450 | return register_filesystem(&btrfs_fs_type); |
| 1451 | } |
| 1452 | |
| 1453 | static void __exit exit_btrfs_fs(void) |
| 1454 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1455 | destroy_inodecache(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1456 | unregister_filesystem(&btrfs_fs_type); |
| 1457 | printk("btrfs unloaded\n"); |
| 1458 | } |
| 1459 | |
| 1460 | module_init(init_btrfs_fs) |
| 1461 | module_exit(exit_btrfs_fs) |
| 1462 | |
| 1463 | MODULE_LICENSE("GPL"); |