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 | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 12 | #include "ctree.h" |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 13 | #include "disk-io.h" |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 14 | #include "transaction.h" |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 15 | |
| 16 | #define BTRFS_SUPER_MAGIC 0x9123682E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 17 | |
| 18 | static struct inode_operations btrfs_dir_inode_operations; |
| 19 | static struct super_operations btrfs_super_ops; |
| 20 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 21 | static struct inode_operations btrfs_file_inode_operations; |
| 22 | static struct address_space_operations btrfs_aops; |
| 23 | static struct file_operations btrfs_file_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 24 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 25 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 26 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 27 | struct btrfs_path path; |
| 28 | struct btrfs_inode_item *inode_item; |
| 29 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 30 | int ret; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 31 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 32 | btrfs_init_path(&path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 33 | mutex_lock(&root->fs_info->fs_mutex); |
| 34 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 35 | ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0); |
| 36 | if (ret) { |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 37 | btrfs_release_path(root, &path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 38 | mutex_unlock(&root->fs_info->fs_mutex); |
| 39 | make_bad_inode(inode); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 40 | return; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 41 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 42 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 43 | path.slots[0], |
| 44 | struct btrfs_inode_item); |
| 45 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 46 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 47 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 48 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 49 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 50 | inode->i_size = btrfs_inode_size(inode_item); |
| 51 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 52 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 53 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 54 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 55 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 56 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 57 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 58 | inode->i_generation = btrfs_inode_generation(inode_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 59 | btrfs_release_path(root, &path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 60 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 61 | switch (inode->i_mode & S_IFMT) { |
| 62 | #if 0 |
| 63 | default: |
| 64 | init_special_inode(inode, inode->i_mode, |
| 65 | btrfs_inode_rdev(inode_item)); |
| 66 | break; |
| 67 | #endif |
| 68 | case S_IFREG: |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 69 | inode->i_mapping->a_ops = &btrfs_aops; |
| 70 | inode->i_fop = &btrfs_file_operations; |
| 71 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 72 | break; |
| 73 | case S_IFDIR: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 74 | inode->i_op = &btrfs_dir_inode_operations; |
| 75 | inode->i_fop = &btrfs_dir_file_operations; |
| 76 | break; |
| 77 | case S_IFLNK: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 78 | // inode->i_op = &page_symlink_inode_operations; |
| 79 | break; |
| 80 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 81 | return; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 84 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 85 | { |
| 86 | struct btrfs_path path; |
| 87 | struct btrfs_root *root; |
| 88 | struct btrfs_trans_handle *trans; |
| 89 | const char *name = dentry->d_name.name; |
| 90 | int name_len = dentry->d_name.len; |
| 91 | int ret; |
| 92 | u64 objectid; |
| 93 | struct btrfs_dir_item *di; |
| 94 | |
| 95 | btrfs_init_path(&path); |
| 96 | root = btrfs_sb(dir->i_sb); |
| 97 | mutex_lock(&root->fs_info->fs_mutex); |
| 98 | trans = btrfs_start_transaction(root, 1); |
| 99 | |
| 100 | ret = btrfs_lookup_dir_item(trans, root, &path, dir->i_ino, |
| 101 | name, name_len, -1); |
| 102 | if (ret < 0) |
| 103 | goto err; |
| 104 | if (ret > 0) { |
| 105 | ret = -ENOENT; |
| 106 | goto err; |
| 107 | } |
| 108 | di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 109 | struct btrfs_dir_item); |
| 110 | objectid = btrfs_dir_objectid(di); |
| 111 | |
| 112 | ret = btrfs_del_item(trans, root, &path); |
| 113 | BUG_ON(ret); |
| 114 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 115 | err: |
| 116 | btrfs_release_path(root, &path); |
| 117 | btrfs_end_transaction(trans, root); |
| 118 | mutex_unlock(&root->fs_info->fs_mutex); |
| 119 | if (ret == 0) |
| 120 | inode_dec_link_count(dentry->d_inode); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 125 | struct btrfs_root *root, |
| 126 | struct inode *inode) |
| 127 | { |
| 128 | u64 objectid = inode->i_ino; |
| 129 | struct btrfs_path path; |
| 130 | struct btrfs_inode_map_item *map; |
| 131 | struct btrfs_key stat_data_key; |
| 132 | int ret; |
| 133 | clear_inode(inode); |
| 134 | btrfs_init_path(&path); |
| 135 | ret = btrfs_lookup_inode_map(trans, root, &path, objectid, -1); |
| 136 | if (ret) { |
| 137 | if (ret > 0) |
| 138 | ret = -ENOENT; |
| 139 | btrfs_release_path(root, &path); |
| 140 | goto error; |
| 141 | } |
| 142 | map = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 143 | struct btrfs_inode_map_item); |
| 144 | btrfs_disk_key_to_cpu(&stat_data_key, &map->key); |
| 145 | ret = btrfs_del_item(trans, root->fs_info->inode_root, &path); |
| 146 | BUG_ON(ret); |
| 147 | btrfs_release_path(root, &path); |
| 148 | btrfs_init_path(&path); |
| 149 | |
| 150 | ret = btrfs_lookup_inode(trans, root, &path, objectid, -1); |
| 151 | BUG_ON(ret); |
| 152 | ret = btrfs_del_item(trans, root, &path); |
| 153 | BUG_ON(ret); |
| 154 | btrfs_release_path(root, &path); |
| 155 | error: |
| 156 | return ret; |
| 157 | } |
| 158 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 159 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 160 | struct btrfs_root *root, |
| 161 | struct inode *inode) |
| 162 | { |
| 163 | int ret; |
| 164 | struct btrfs_path path; |
| 165 | struct btrfs_key key; |
| 166 | struct btrfs_disk_key *found_key; |
| 167 | struct btrfs_leaf *leaf; |
| 168 | struct btrfs_file_extent_item *fi; |
| 169 | u64 extent_start; |
| 170 | u64 extent_num_blocks; |
| 171 | |
| 172 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 173 | key.objectid = inode->i_ino; |
| 174 | key.offset = (u64)-1; |
| 175 | key.flags = 0; |
| 176 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 177 | while(1) { |
| 178 | btrfs_init_path(&path); |
| 179 | ret = btrfs_search_slot(trans, root, &key, &path, -1, 1); |
| 180 | if (ret < 0) { |
| 181 | btrfs_release_path(root, &path); |
| 182 | goto error; |
| 183 | } |
| 184 | if (ret > 0) { |
| 185 | BUG_ON(path.slots[0] == 0); |
| 186 | path.slots[0]--; |
| 187 | } |
| 188 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 189 | found_key = &leaf->items[path.slots[0]].key; |
| 190 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 191 | break; |
| 192 | if (btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
| 193 | break; |
| 194 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 195 | break; |
| 196 | /* FIXME: add extent truncation */ |
| 197 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 198 | break; |
| 199 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 200 | path.slots[0], |
| 201 | struct btrfs_file_extent_item); |
| 202 | extent_start = btrfs_file_extent_disk_blocknr(fi); |
| 203 | extent_num_blocks = btrfs_file_extent_disk_num_blocks(fi); |
| 204 | key.offset = btrfs_disk_key_offset(found_key) - 1; |
| 205 | ret = btrfs_del_item(trans, root, &path); |
| 206 | BUG_ON(ret); |
| 207 | inode->i_blocks -= btrfs_file_extent_num_blocks(fi) >> 9; |
| 208 | btrfs_release_path(root, &path); |
| 209 | ret = btrfs_free_extent(trans, root, extent_start, |
| 210 | extent_num_blocks, 0); |
| 211 | BUG_ON(ret); |
| 212 | if (btrfs_disk_key_offset(found_key) == 0) |
| 213 | break; |
| 214 | } |
| 215 | btrfs_release_path(root, &path); |
| 216 | ret = 0; |
| 217 | error: |
| 218 | return ret; |
| 219 | } |
| 220 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 221 | static void btrfs_delete_inode(struct inode *inode) |
| 222 | { |
| 223 | struct btrfs_trans_handle *trans; |
| 224 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 225 | int ret; |
| 226 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 227 | truncate_inode_pages(&inode->i_data, 0); |
| 228 | if (is_bad_inode(inode)) { |
| 229 | goto no_delete; |
| 230 | } |
| 231 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 232 | mutex_lock(&root->fs_info->fs_mutex); |
| 233 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 234 | if (S_ISREG(inode->i_mode)) { |
| 235 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 236 | BUG_ON(ret); |
| 237 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 238 | btrfs_free_inode(trans, root, inode); |
| 239 | btrfs_end_transaction(trans, root); |
| 240 | mutex_unlock(&root->fs_info->fs_mutex); |
| 241 | return; |
| 242 | no_delete: |
| 243 | clear_inode(inode); |
| 244 | } |
| 245 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 246 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 247 | ino_t *ino) |
| 248 | { |
| 249 | const char *name = dentry->d_name.name; |
| 250 | int namelen = dentry->d_name.len; |
| 251 | struct btrfs_dir_item *di; |
| 252 | struct btrfs_path path; |
| 253 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 254 | int ret; |
| 255 | |
| 256 | btrfs_init_path(&path); |
| 257 | ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name, |
| 258 | namelen, 0); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 259 | if (ret || !btrfs_match_dir_item_name(root, &path, name, namelen)) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 260 | *ino = 0; |
| 261 | goto out; |
| 262 | } |
| 263 | di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 264 | struct btrfs_dir_item); |
| 265 | *ino = btrfs_dir_objectid(di); |
| 266 | out: |
| 267 | btrfs_release_path(root, &path); |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 272 | struct nameidata *nd) |
| 273 | { |
| 274 | struct inode * inode; |
| 275 | ino_t ino; |
| 276 | int ret; |
| 277 | |
| 278 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 279 | return ERR_PTR(-ENAMETOOLONG); |
| 280 | |
| 281 | ret = btrfs_inode_by_name(dir, dentry, &ino); |
| 282 | if (ret < 0) |
| 283 | return ERR_PTR(ret); |
| 284 | inode = NULL; |
| 285 | if (ino) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 286 | inode = iget(dir->i_sb, ino); |
| 287 | if (!inode) |
| 288 | return ERR_PTR(-EACCES); |
| 289 | } |
| 290 | return d_splice_alias(inode, dentry); |
| 291 | } |
| 292 | |
| 293 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 294 | { |
| 295 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 296 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 297 | struct btrfs_item *item; |
| 298 | struct btrfs_dir_item *di; |
| 299 | struct btrfs_key key; |
| 300 | struct btrfs_path path; |
| 301 | int ret; |
| 302 | u32 nritems; |
| 303 | struct btrfs_leaf *leaf; |
| 304 | int slot; |
| 305 | int advance; |
| 306 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 307 | int over = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 308 | |
| 309 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 310 | key.flags = 0; |
| 311 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
| 312 | key.offset = filp->f_pos; |
| 313 | btrfs_init_path(&path); |
| 314 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); |
| 315 | if (ret < 0) { |
| 316 | goto err; |
| 317 | } |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 318 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 319 | while(1) { |
| 320 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 321 | nritems = btrfs_header_nritems(&leaf->header); |
| 322 | slot = path.slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 323 | if (advance || slot >= nritems) { |
| 324 | if (slot >= nritems -1) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 325 | ret = btrfs_next_leaf(root, &path); |
| 326 | if (ret) |
| 327 | break; |
| 328 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 329 | nritems = btrfs_header_nritems(&leaf->header); |
| 330 | slot = path.slots[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 331 | } else { |
| 332 | slot++; |
| 333 | path.slots[0]++; |
| 334 | } |
| 335 | } |
| 336 | advance = 1; |
| 337 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 338 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 339 | break; |
| 340 | if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY) |
| 341 | continue; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 342 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 343 | continue; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 344 | |
| 345 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 346 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 347 | over = filldir(dirent, (const char *)(di + 1), |
| 348 | btrfs_dir_name_len(di), |
| 349 | btrfs_disk_key_offset(&item->key), |
| 350 | btrfs_dir_objectid(di), d_type); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 351 | if (over) { |
| 352 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 353 | break; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 354 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 355 | filp->f_pos = btrfs_disk_key_offset(&item->key) + 1; |
| 356 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 357 | ret = 0; |
| 358 | err: |
| 359 | btrfs_release_path(root, &path); |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static void btrfs_put_super (struct super_block * sb) |
| 364 | { |
| 365 | struct btrfs_root *root = btrfs_sb(sb); |
| 366 | int ret; |
| 367 | |
| 368 | ret = close_ctree(root); |
| 369 | if (ret) { |
| 370 | printk("close ctree returns %d\n", ret); |
| 371 | } |
| 372 | sb->s_fs_info = NULL; |
| 373 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 374 | |
| 375 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 376 | { |
| 377 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 378 | struct dentry * root_dentry; |
| 379 | struct btrfs_super_block *disk_super; |
| 380 | struct buffer_head *bh; |
| 381 | struct btrfs_root *root; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 382 | |
| 383 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 384 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 385 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 386 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 387 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 388 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 389 | |
| 390 | bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize); |
| 391 | if (!bh) { |
| 392 | printk("btrfs: unable to read on disk super\n"); |
| 393 | return -EIO; |
| 394 | } |
| 395 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 396 | root = open_ctree(sb, bh, disk_super); |
| 397 | sb->s_fs_info = root; |
| 398 | if (!root) { |
| 399 | printk("btrfs: open_ctree failed\n"); |
| 400 | return -EIO; |
| 401 | } |
| 402 | printk("read in super total blocks %Lu root %Lu\n", |
| 403 | btrfs_super_total_blocks(disk_super), |
| 404 | btrfs_super_root_dir(disk_super)); |
| 405 | |
| 406 | inode = iget_locked(sb, btrfs_super_root_dir(disk_super)); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 407 | if (!inode) |
| 408 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 409 | if (inode->i_state & I_NEW) { |
| 410 | btrfs_read_locked_inode(inode); |
| 411 | unlock_new_inode(inode); |
| 412 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 413 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 414 | root_dentry = d_alloc_root(inode); |
| 415 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 416 | iput(inode); |
| 417 | return -ENOMEM; |
| 418 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 419 | sb->s_root = root_dentry; |
| 420 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 424 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 425 | struct inode *inode) |
| 426 | { |
| 427 | btrfs_set_inode_uid(item, inode->i_uid); |
| 428 | btrfs_set_inode_gid(item, inode->i_gid); |
| 429 | btrfs_set_inode_size(item, inode->i_size); |
| 430 | btrfs_set_inode_mode(item, inode->i_mode); |
| 431 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 432 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 433 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 434 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 435 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 436 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 437 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 438 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 439 | btrfs_set_inode_generation(item, inode->i_generation); |
| 440 | } |
| 441 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 442 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 443 | struct btrfs_root *root, |
| 444 | struct inode *inode) |
| 445 | { |
| 446 | struct btrfs_inode_item *inode_item; |
| 447 | struct btrfs_path path; |
| 448 | int ret; |
| 449 | |
| 450 | btrfs_init_path(&path); |
| 451 | |
| 452 | ret = btrfs_lookup_inode(trans, root, &path, inode->i_ino, 1); |
| 453 | if (ret) { |
| 454 | if (ret > 0) |
| 455 | ret = -ENOENT; |
| 456 | goto failed; |
| 457 | } |
| 458 | |
| 459 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 460 | path.slots[0], |
| 461 | struct btrfs_inode_item); |
| 462 | |
| 463 | fill_inode_item(inode_item, inode); |
| 464 | mark_buffer_dirty(path.nodes[0]); |
| 465 | failed: |
| 466 | btrfs_release_path(root, &path); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 471 | { |
| 472 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 473 | struct btrfs_trans_handle *trans; |
| 474 | int ret; |
| 475 | |
| 476 | mutex_lock(&root->fs_info->fs_mutex); |
| 477 | trans = btrfs_start_transaction(root, 1); |
| 478 | ret = btrfs_update_inode(trans, root, inode); |
| 479 | if (wait) |
| 480 | btrfs_commit_transaction(trans, root); |
| 481 | else |
| 482 | btrfs_end_transaction(trans, root); |
| 483 | mutex_unlock(&root->fs_info->fs_mutex); |
| 484 | return ret; |
| 485 | } |
| 486 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 487 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 488 | struct inode *dir, int mode) |
| 489 | { |
| 490 | struct inode *inode; |
| 491 | struct btrfs_inode_item inode_item; |
| 492 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 493 | struct btrfs_key key; |
| 494 | int ret; |
| 495 | u64 objectid; |
| 496 | |
| 497 | inode = new_inode(dir->i_sb); |
| 498 | if (!inode) |
| 499 | return ERR_PTR(-ENOMEM); |
| 500 | |
| 501 | ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 502 | BUG_ON(ret); |
| 503 | |
| 504 | inode->i_uid = current->fsuid; |
| 505 | inode->i_gid = current->fsgid; |
| 506 | inode->i_mode = mode; |
| 507 | inode->i_ino = objectid; |
| 508 | inode->i_blocks = 0; |
| 509 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 510 | fill_inode_item(&inode_item, inode); |
| 511 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 512 | key.objectid = objectid; |
| 513 | key.flags = 0; |
| 514 | key.offset = 0; |
| 515 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 516 | ret = btrfs_insert_inode_map(trans, root, objectid, &key); |
| 517 | BUG_ON(ret); |
| 518 | |
| 519 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 520 | BUG_ON(ret); |
| 521 | |
| 522 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 523 | return inode; |
| 524 | } |
| 525 | |
| 526 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 527 | struct dentry *dentry, struct inode *inode) |
| 528 | { |
| 529 | int ret; |
| 530 | ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb), |
| 531 | dentry->d_name.name, dentry->d_name.len, |
| 532 | dentry->d_parent->d_inode->i_ino, |
| 533 | inode->i_ino, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 534 | if (ret == 0) { |
| 535 | dentry->d_parent->d_inode->i_size += dentry->d_name.len; |
| 536 | ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb), |
| 537 | dentry->d_parent->d_inode); |
| 538 | } |
| 539 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 544 | struct dentry *dentry, struct inode *inode) |
| 545 | { |
| 546 | int err = btrfs_add_link(trans, dentry, inode); |
| 547 | if (!err) { |
| 548 | d_instantiate(dentry, inode); |
| 549 | return 0; |
| 550 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 551 | return err; |
| 552 | } |
| 553 | |
| 554 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 555 | int mode, struct nameidata *nd) |
| 556 | { |
| 557 | struct btrfs_trans_handle *trans; |
| 558 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 559 | struct inode *inode; |
| 560 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 561 | int drop_inode = 0; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 562 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 563 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 564 | trans = btrfs_start_transaction(root, 1); |
| 565 | inode = btrfs_new_inode(trans, dir, mode); |
| 566 | err = PTR_ERR(inode); |
| 567 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 568 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 569 | // FIXME mark the inode dirty |
| 570 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 571 | if (err) |
| 572 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 573 | else { |
| 574 | inode->i_mapping->a_ops = &btrfs_aops; |
| 575 | inode->i_fop = &btrfs_file_operations; |
| 576 | inode->i_op = &btrfs_file_inode_operations; |
| 577 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 578 | dir->i_sb->s_dirt = 1; |
| 579 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 580 | out_unlock: |
| 581 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 582 | if (drop_inode) { |
| 583 | inode_dec_link_count(inode); |
| 584 | iput(inode); |
| 585 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 586 | return err; |
| 587 | } |
| 588 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 589 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 590 | struct inode *inode, struct inode *dir) |
| 591 | { |
| 592 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 593 | int ret; |
| 594 | char buf[2]; |
| 595 | buf[0] = '.'; |
| 596 | buf[1] = '.'; |
| 597 | |
| 598 | ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino, |
| 599 | inode->i_ino, 1); |
| 600 | if (ret) |
| 601 | goto error; |
| 602 | ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino, |
| 603 | dir->i_ino, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 604 | if (ret) |
| 605 | goto error; |
| 606 | inode->i_size = 3; |
| 607 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 608 | error: |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 613 | { |
| 614 | struct inode *inode; |
| 615 | struct btrfs_trans_handle *trans; |
| 616 | struct btrfs_root *root = btrfs_sb(dir->i_sb); |
| 617 | int err = 0; |
| 618 | int drop_on_err = 0; |
| 619 | |
| 620 | mutex_lock(&root->fs_info->fs_mutex); |
| 621 | trans = btrfs_start_transaction(root, 1); |
| 622 | if (IS_ERR(trans)) { |
| 623 | err = PTR_ERR(trans); |
| 624 | goto out_unlock; |
| 625 | } |
| 626 | inode = btrfs_new_inode(trans, dir, S_IFDIR | mode); |
| 627 | if (IS_ERR(inode)) { |
| 628 | err = PTR_ERR(inode); |
| 629 | goto out_fail; |
| 630 | } |
| 631 | drop_on_err = 1; |
| 632 | inode->i_op = &btrfs_dir_inode_operations; |
| 633 | inode->i_fop = &btrfs_dir_file_operations; |
| 634 | |
| 635 | err = btrfs_make_empty_dir(trans, inode, dir); |
| 636 | if (err) |
| 637 | goto out_fail; |
| 638 | err = btrfs_add_link(trans, dentry, inode); |
| 639 | if (err) |
| 640 | goto out_fail; |
| 641 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 642 | drop_on_err = 0; |
| 643 | |
| 644 | out_fail: |
| 645 | btrfs_end_transaction(trans, root); |
| 646 | out_unlock: |
| 647 | mutex_unlock(&root->fs_info->fs_mutex); |
| 648 | if (drop_on_err) |
| 649 | iput(inode); |
| 650 | return err; |
| 651 | } |
| 652 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 653 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 654 | { |
| 655 | struct btrfs_trans_handle *trans; |
| 656 | struct btrfs_root *root; |
| 657 | int ret; |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 658 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 659 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 660 | if (!wait) { |
| 661 | filemap_flush(sb->s_bdev->bd_inode->i_mapping); |
| 662 | return 0; |
| 663 | } |
| 664 | filemap_write_and_wait(sb->s_bdev->bd_inode->i_mapping); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 665 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 666 | root = btrfs_sb(sb); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 667 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 668 | trans = btrfs_start_transaction(root, 1); |
| 669 | ret = btrfs_commit_transaction(trans, root); |
| 670 | sb->s_dirt = 0; |
| 671 | BUG_ON(ret); |
| 672 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 673 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 677 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 678 | struct buffer_head *result, int create) |
| 679 | { |
| 680 | int ret; |
| 681 | int err = 0; |
| 682 | u64 blocknr; |
| 683 | u64 extent_start = 0; |
| 684 | u64 extent_end = 0; |
| 685 | u64 objectid = inode->i_ino; |
| 686 | struct btrfs_path path; |
| 687 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 688 | struct btrfs_trans_handle *trans = NULL; |
| 689 | struct btrfs_file_extent_item *item; |
| 690 | struct btrfs_leaf *leaf; |
| 691 | struct btrfs_disk_key *found_key; |
| 692 | |
| 693 | btrfs_init_path(&path); |
| 694 | mutex_lock(&root->fs_info->fs_mutex); |
| 695 | if (create) |
| 696 | trans = btrfs_start_transaction(root, 1); |
| 697 | |
| 698 | |
| 699 | ret = btrfs_lookup_file_extent(trans, root, &path, |
| 700 | inode->i_ino, iblock, 1, 0); |
| 701 | if (ret < 0) { |
| 702 | btrfs_release_path(root, &path); |
| 703 | err = ret; |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | if (ret != 0) { |
| 708 | if (path.slots[0] == 0) { |
| 709 | btrfs_release_path(root, &path); |
| 710 | goto allocate; |
| 711 | } |
| 712 | path.slots[0]--; |
| 713 | } |
| 714 | |
| 715 | item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 716 | struct btrfs_file_extent_item); |
| 717 | leaf = btrfs_buffer_leaf(path.nodes[0]); |
| 718 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 719 | blocknr += btrfs_file_extent_offset(item); |
| 720 | |
| 721 | /* exact match found, use it */ |
| 722 | if (ret == 0) { |
| 723 | err = 0; |
| 724 | map_bh(result, inode->i_sb, blocknr); |
| 725 | btrfs_release_path(root, &path); |
| 726 | goto out; |
| 727 | } |
| 728 | |
| 729 | /* are we inside the extent that was found? */ |
| 730 | found_key = &leaf->items[path.slots[0]].key; |
| 731 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 732 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 733 | extent_end = 0; |
| 734 | extent_start = 0; |
| 735 | btrfs_release_path(root, &path); |
| 736 | goto allocate; |
| 737 | } |
| 738 | |
| 739 | extent_start = btrfs_disk_key_offset(&leaf->items[path.slots[0]].key); |
| 740 | extent_start += btrfs_file_extent_offset(item); |
| 741 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
| 742 | btrfs_release_path(root, &path); |
| 743 | if (iblock >= extent_start && iblock < extent_end) { |
| 744 | err = 0; |
| 745 | map_bh(result, inode->i_sb, blocknr + iblock - extent_start); |
| 746 | goto out; |
| 747 | } |
| 748 | allocate: |
| 749 | /* ok, create a new extent */ |
| 750 | if (!create) { |
| 751 | err = 0; |
| 752 | goto out; |
| 753 | } |
| 754 | ret = btrfs_alloc_file_extent(trans, root, objectid, iblock, |
| 755 | 1, extent_end, &blocknr); |
| 756 | if (ret) { |
| 757 | err = ret; |
| 758 | goto out; |
| 759 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 760 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 761 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 762 | map_bh(result, inode->i_sb, blocknr); |
| 763 | |
| 764 | out: |
| 765 | if (trans) |
| 766 | btrfs_end_transaction(trans, root); |
| 767 | mutex_unlock(&root->fs_info->fs_mutex); |
| 768 | return err; |
| 769 | } |
| 770 | |
| 771 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 772 | unsigned from, unsigned to) |
| 773 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 774 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 775 | } |
| 776 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 777 | static void btrfs_write_super(struct super_block *sb) |
| 778 | { |
| 779 | btrfs_sync_fs(sb, 1); |
| 780 | } |
| 781 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 782 | static int btrfs_readpage(struct file *file, struct page *page) |
| 783 | { |
| 784 | return mpage_readpage(page, btrfs_get_block); |
| 785 | } |
| 786 | |
| 787 | static int btrfs_readpages(struct file *file, struct address_space *mapping, |
| 788 | struct list_head *pages, unsigned nr_pages) |
| 789 | { |
| 790 | return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block); |
| 791 | } |
| 792 | |
| 793 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 794 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 795 | return nobh_writepage(page, btrfs_get_block, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 796 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 797 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 798 | static void btrfs_truncate(struct inode *inode) |
| 799 | { |
| 800 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 801 | int ret; |
| 802 | struct btrfs_trans_handle *trans; |
| 803 | |
| 804 | if (!S_ISREG(inode->i_mode)) |
| 805 | return; |
| 806 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 807 | return; |
| 808 | |
| 809 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
| 810 | |
| 811 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 812 | mutex_lock(&root->fs_info->fs_mutex); |
| 813 | trans = btrfs_start_transaction(root, 1); |
| 814 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 815 | BUG_ON(ret); |
| 816 | ret = btrfs_end_transaction(trans, root); |
| 817 | BUG_ON(ret); |
| 818 | mutex_unlock(&root->fs_info->fs_mutex); |
| 819 | mark_inode_dirty(inode); |
| 820 | } |
| 821 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 822 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 823 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 824 | { |
| 825 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 826 | btrfs_fill_super, mnt); |
| 827 | } |
| 828 | |
| 829 | static struct file_system_type btrfs_fs_type = { |
| 830 | .owner = THIS_MODULE, |
| 831 | .name = "btrfs", |
| 832 | .get_sb = btrfs_get_sb, |
| 833 | .kill_sb = kill_block_super, |
| 834 | .fs_flags = FS_REQUIRES_DEV, |
| 835 | }; |
| 836 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 837 | static struct super_operations btrfs_super_ops = { |
| 838 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 839 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 840 | .put_super = btrfs_put_super, |
| 841 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 842 | .write_super = btrfs_write_super, |
| 843 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 844 | .write_inode = btrfs_write_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 845 | }; |
| 846 | |
| 847 | static struct inode_operations btrfs_dir_inode_operations = { |
| 848 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 849 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 850 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 851 | .mkdir = btrfs_mkdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | static struct file_operations btrfs_dir_file_operations = { |
| 855 | .llseek = generic_file_llseek, |
| 856 | .read = generic_read_dir, |
| 857 | .readdir = btrfs_readdir, |
| 858 | }; |
| 859 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 860 | static struct address_space_operations btrfs_aops = { |
| 861 | .readpage = btrfs_readpage, |
| 862 | .readpages = btrfs_readpages, |
| 863 | .writepage = btrfs_writepage, |
| 864 | .sync_page = block_sync_page, |
| 865 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 866 | .commit_write = nobh_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 867 | }; |
| 868 | |
| 869 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame^] | 870 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 871 | }; |
| 872 | |
| 873 | static struct file_operations btrfs_file_operations = { |
| 874 | .llseek = generic_file_llseek, |
| 875 | .read = do_sync_read, |
| 876 | .write = do_sync_write, |
| 877 | .aio_read = generic_file_aio_read, |
| 878 | .aio_write = generic_file_aio_write, |
| 879 | .mmap = generic_file_mmap, |
| 880 | .open = generic_file_open, |
| 881 | .sendfile = generic_file_sendfile, |
| 882 | .splice_read = generic_file_splice_read, |
| 883 | .splice_write = generic_file_splice_write, |
| 884 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 885 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 886 | static int __init init_btrfs_fs(void) |
| 887 | { |
| 888 | printk("btrfs loaded!\n"); |
| 889 | return register_filesystem(&btrfs_fs_type); |
| 890 | } |
| 891 | |
| 892 | static void __exit exit_btrfs_fs(void) |
| 893 | { |
| 894 | unregister_filesystem(&btrfs_fs_type); |
| 895 | printk("btrfs unloaded\n"); |
| 896 | } |
| 897 | |
| 898 | module_init(init_btrfs_fs) |
| 899 | module_exit(exit_btrfs_fs) |
| 900 | |
| 901 | MODULE_LICENSE("GPL"); |