blob: f99c764a59dbb9fbd8aa0059acdc48dd87907d38 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -04002#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -04003#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 Masondee26a92007-03-26 16:00:06 -040011#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040012#include <linux/swap.h>
13#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040014#include <linux/statfs.h>
Chris Mason2e635a22007-03-21 11:12:56 -040015#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040016#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040017#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040018#include "btrfs_inode.h"
Chris Masonc5739bb2007-04-10 09:27:04 -040019#include "ioctl.h"
Chris Mason2e635a22007-03-21 11:12:56 -040020
Chris Masond6e4a422007-04-06 15:37:36 -040021void btrfs_fsinfo_release(struct kobject *obj)
22{
23 struct btrfs_fs_info *fsinfo = container_of(obj,
24 struct btrfs_fs_info, kobj);
25 kfree(fsinfo);
26}
27
28struct kobj_type btrfs_fsinfo_ktype = {
29 .release = btrfs_fsinfo_release,
30};
31
Chris Masonc5739bb2007-04-10 09:27:04 -040032struct btrfs_iget_args {
33 u64 ino;
34 struct btrfs_root *root;
35};
36
Chris Masond6e4a422007-04-06 15:37:36 -040037decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
38
Chris Mason2e635a22007-03-21 11:12:56 -040039#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040040
41static struct inode_operations btrfs_dir_inode_operations;
Chris Masond6e4a422007-04-06 15:37:36 -040042static struct inode_operations btrfs_dir_ro_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040043static struct super_operations btrfs_super_ops;
44static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040045static struct inode_operations btrfs_file_inode_operations;
46static struct address_space_operations btrfs_aops;
47static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040048
Chris Masone20d96d2007-03-22 12:13:20 -040049static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040050{
Chris Mason5caf2a02007-04-02 11:20:42 -040051 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040052 struct btrfs_inode_item *inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -040053 struct btrfs_root *root = BTRFS_I(inode)->root;
54 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -040055 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040056
Chris Mason5caf2a02007-04-02 11:20:42 -040057 path = btrfs_alloc_path();
58 BUG_ON(!path);
59 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040060 mutex_lock(&root->fs_info->fs_mutex);
61
Chris Masond6e4a422007-04-06 15:37:36 -040062 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
63 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040064 if (ret) {
Chris Mason7cfcc172007-04-02 14:53:59 -040065 btrfs_free_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -040066 goto make_bad;
Chris Mason2e635a22007-03-21 11:12:56 -040067 }
Chris Mason5caf2a02007-04-02 11:20:42 -040068 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
69 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040070 struct btrfs_inode_item);
71
Chris Masone20d96d2007-03-22 12:13:20 -040072 inode->i_mode = btrfs_inode_mode(inode_item);
73 inode->i_nlink = btrfs_inode_nlink(inode_item);
74 inode->i_uid = btrfs_inode_uid(inode_item);
75 inode->i_gid = btrfs_inode_gid(inode_item);
76 inode->i_size = btrfs_inode_size(inode_item);
77 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
78 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
79 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
80 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
81 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
82 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
83 inode->i_blocks = btrfs_inode_nblocks(inode_item);
84 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040085
Chris Mason5caf2a02007-04-02 11:20:42 -040086 btrfs_free_path(path);
87 inode_item = NULL;
88
Chris Masonf4b9aa82007-03-27 11:05:53 -040089 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b05da22007-04-10 12:13:09 -040090
Chris Masone20d96d2007-03-22 12:13:20 -040091 switch (inode->i_mode & S_IFMT) {
92#if 0
93 default:
94 init_special_inode(inode, inode->i_mode,
95 btrfs_inode_rdev(inode_item));
96 break;
97#endif
98 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040099 inode->i_mapping->a_ops = &btrfs_aops;
100 inode->i_fop = &btrfs_file_operations;
101 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400102 break;
103 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400104 inode->i_fop = &btrfs_dir_file_operations;
Chris Masond6e4a422007-04-06 15:37:36 -0400105 if (root == root->fs_info->tree_root)
106 inode->i_op = &btrfs_dir_ro_inode_operations;
107 else
108 inode->i_op = &btrfs_dir_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400109 break;
110 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400111 // inode->i_op = &page_symlink_inode_operations;
112 break;
113 }
Chris Masone20d96d2007-03-22 12:13:20 -0400114 return;
Chris Masond6e4a422007-04-06 15:37:36 -0400115
116make_bad:
117 btrfs_release_path(root, path);
118 btrfs_free_path(path);
119 mutex_unlock(&root->fs_info->fs_mutex);
120 make_bad_inode(inode);
Chris Mason2e635a22007-03-21 11:12:56 -0400121}
122
Chris Mason5f443fd2007-03-27 13:42:32 -0400123static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root,
125 struct inode *dir,
126 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400127{
Chris Mason5caf2a02007-04-02 11:20:42 -0400128 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400129 const char *name = dentry->d_name.name;
130 int name_len = dentry->d_name.len;
Chris Mason7e381802007-04-19 15:36:27 -0400131 int ret = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400132 u64 objectid;
133 struct btrfs_dir_item *di;
134
Chris Mason5caf2a02007-04-02 11:20:42 -0400135 path = btrfs_alloc_path();
136 BUG_ON(!path);
137 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400138 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400139 name, name_len, -1);
Chris Mason7e381802007-04-19 15:36:27 -0400140 if (IS_ERR(di)) {
141 ret = PTR_ERR(di);
Chris Mason134e9732007-03-25 13:44:56 -0400142 goto err;
Chris Mason7e381802007-04-19 15:36:27 -0400143 }
144 if (!di) {
Chris Mason134e9732007-03-25 13:44:56 -0400145 ret = -ENOENT;
146 goto err;
147 }
Chris Masond6e4a422007-04-06 15:37:36 -0400148 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason7e381802007-04-19 15:36:27 -0400149 ret = btrfs_delete_one_dir_name(trans, root, path, di);
150 BUG_ON(ret);
151 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400152
Chris Mason7e381802007-04-19 15:36:27 -0400153 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
154 objectid, name, name_len, -1);
155 if (IS_ERR(di)) {
156 ret = PTR_ERR(di);
157 goto err;
158 }
159 if (!di) {
160 ret = -ENOENT;
161 goto err;
162 }
163 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason134e9732007-03-25 13:44:56 -0400164 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400165
Chris Mason134e9732007-03-25 13:44:56 -0400166 dentry->d_inode->i_ctime = dir->i_ctime;
167err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400168 btrfs_free_path(path);
Chris Masond4dbff92007-04-04 14:08:15 -0400169 if (ret == 0) {
Chris Mason134e9732007-03-25 13:44:56 -0400170 inode_dec_link_count(dentry->d_inode);
Chris Mason5f26f772007-04-05 10:38:44 -0400171 dir->i_size -= name_len * 2;
Chris Masond4dbff92007-04-04 14:08:15 -0400172 mark_inode_dirty(dir);
173 }
Chris Mason134e9732007-03-25 13:44:56 -0400174 return ret;
175}
176
Chris Mason5f443fd2007-03-27 13:42:32 -0400177static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
178{
179 struct btrfs_root *root;
180 struct btrfs_trans_handle *trans;
181 int ret;
182
Chris Masond6e4a422007-04-06 15:37:36 -0400183 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400184 mutex_lock(&root->fs_info->fs_mutex);
185 trans = btrfs_start_transaction(root, 1);
186 ret = btrfs_unlink_trans(trans, root, dir, dentry);
187 btrfs_end_transaction(trans, root);
188 mutex_unlock(&root->fs_info->fs_mutex);
189 return ret;
190}
191
192static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
193{
194 struct inode *inode = dentry->d_inode;
195 int err;
196 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400197 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400198 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400199 struct btrfs_key key;
200 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400201 struct btrfs_key found_key;
202 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400203 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400204 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400205
Chris Mason5caf2a02007-04-02 11:20:42 -0400206 path = btrfs_alloc_path();
207 BUG_ON(!path);
208 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400209 mutex_lock(&root->fs_info->fs_mutex);
210 trans = btrfs_start_transaction(root, 1);
211 key.objectid = inode->i_ino;
212 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400213 key.flags = (u32)-1;
214 while(1) {
215 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
216 if (ret < 0) {
217 err = ret;
218 goto out;
219 }
220 BUG_ON(ret == 0);
221 if (path->slots[0] == 0) {
222 err = -ENOENT;
223 goto out;
224 }
225 path->slots[0]--;
226 leaf = btrfs_buffer_leaf(path->nodes[0]);
227 btrfs_disk_key_to_cpu(&found_key,
228 &leaf->items[path->slots[0]].key);
229 found_type = btrfs_key_type(&found_key);
230 if (found_key.objectid != inode->i_ino) {
231 err = -ENOENT;
232 goto out;
233 }
234 if ((found_type != BTRFS_DIR_ITEM_KEY &&
235 found_type != BTRFS_DIR_INDEX_KEY) ||
236 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
237 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
238 err = -ENOTEMPTY;
239 goto out;
240 }
241 ret = btrfs_del_item(trans, root, path);
242 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400243
Chris Mason5f26f772007-04-05 10:38:44 -0400244 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
245 break;
246 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400247 }
Chris Mason5f26f772007-04-05 10:38:44 -0400248 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400249 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400250
251 /* now the directory is empty */
252 err = btrfs_unlink_trans(trans, root, dir, dentry);
253 if (!err) {
254 inode->i_size = 0;
255 }
256out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400257 btrfs_release_path(root, path);
258 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400259 mutex_unlock(&root->fs_info->fs_mutex);
260 ret = btrfs_end_transaction(trans, root);
261 if (ret && !err)
262 err = ret;
263 return err;
264}
265
Chris Mason134e9732007-03-25 13:44:56 -0400266static int btrfs_free_inode(struct btrfs_trans_handle *trans,
267 struct btrfs_root *root,
268 struct inode *inode)
269{
Chris Mason5caf2a02007-04-02 11:20:42 -0400270 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400271 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400272
Chris Mason134e9732007-03-25 13:44:56 -0400273 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400274
275 path = btrfs_alloc_path();
276 BUG_ON(!path);
277 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400278 ret = btrfs_lookup_inode(trans, root, path,
279 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400280 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400281 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400282 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400283 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400284 return ret;
285}
286
Chris Masonf4b9aa82007-03-27 11:05:53 -0400287static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
288 struct btrfs_root *root,
289 struct inode *inode)
290{
291 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400292 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400293 struct btrfs_key key;
294 struct btrfs_disk_key *found_key;
295 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400296 struct btrfs_file_extent_item *fi = NULL;
297 u64 extent_start = 0;
298 u64 extent_num_blocks = 0;
299 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400300
Chris Mason5caf2a02007-04-02 11:20:42 -0400301 path = btrfs_alloc_path();
302 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400303 /* FIXME, add redo link to tree so we don't leak on crash */
304 key.objectid = inode->i_ino;
305 key.offset = (u64)-1;
306 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400307 /*
308 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
309 * or extent data
310 */
Chris Masonf254e522007-03-29 15:15:27 -0400311 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400312 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400313 btrfs_init_path(path);
314 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400315 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400316 goto error;
317 }
318 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400319 BUG_ON(path->slots[0] == 0);
320 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400321 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400322 leaf = btrfs_buffer_leaf(path->nodes[0]);
323 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400324 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
325 break;
Chris Masonf254e522007-03-29 15:15:27 -0400326 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400327 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400328 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400329 break;
330 if (btrfs_disk_key_offset(found_key) < inode->i_size)
331 break;
Chris Mason236454d2007-04-19 13:37:44 -0400332 found_extent = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400333 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400334 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
335 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400336 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400337 if (btrfs_file_extent_type(fi) !=
338 BTRFS_FILE_EXTENT_INLINE) {
339 extent_start =
340 btrfs_file_extent_disk_blocknr(fi);
341 extent_num_blocks =
342 btrfs_file_extent_disk_num_blocks(fi);
343 /* FIXME blocksize != 4096 */
344 inode->i_blocks -=
345 btrfs_file_extent_num_blocks(fi) << 3;
346 found_extent = 1;
347 }
Chris Masonf254e522007-03-29 15:15:27 -0400348 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400349 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400350 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400351 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400352 if (found_extent) {
353 ret = btrfs_free_extent(trans, root, extent_start,
354 extent_num_blocks, 0);
355 BUG_ON(ret);
356 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400357 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400358 ret = 0;
359error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400360 btrfs_release_path(root, path);
361 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400362 return ret;
363}
364
Chris Mason134e9732007-03-25 13:44:56 -0400365static void btrfs_delete_inode(struct inode *inode)
366{
367 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400368 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400369 int ret;
370
Chris Mason134e9732007-03-25 13:44:56 -0400371 truncate_inode_pages(&inode->i_data, 0);
372 if (is_bad_inode(inode)) {
373 goto no_delete;
374 }
375 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400376 mutex_lock(&root->fs_info->fs_mutex);
377 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400378 if (S_ISREG(inode->i_mode)) {
379 ret = btrfs_truncate_in_trans(trans, root, inode);
380 BUG_ON(ret);
381 }
Chris Mason134e9732007-03-25 13:44:56 -0400382 btrfs_free_inode(trans, root, inode);
383 btrfs_end_transaction(trans, root);
384 mutex_unlock(&root->fs_info->fs_mutex);
385 return;
386no_delete:
387 clear_inode(inode);
388}
389
Chris Masone20d96d2007-03-22 12:13:20 -0400390static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400391 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400392{
393 const char *name = dentry->d_name.name;
394 int namelen = dentry->d_name.len;
395 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400396 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400397 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400398 int ret;
399
Chris Mason5caf2a02007-04-02 11:20:42 -0400400 path = btrfs_alloc_path();
401 BUG_ON(!path);
402 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400403 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400404 namelen, 0);
Chris Mason7e381802007-04-19 15:36:27 -0400405 if (!di || IS_ERR(di)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400406 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400407 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400408 goto out;
409 }
Chris Masond6e4a422007-04-06 15:37:36 -0400410 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400411out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400412 btrfs_release_path(root, path);
413 btrfs_free_path(path);
Chris Masone20d96d2007-03-22 12:13:20 -0400414 return ret;
415}
416
Chris Masond6e4a422007-04-06 15:37:36 -0400417int fixup_tree_root_location(struct btrfs_root *root,
418 struct btrfs_key *location,
419 struct btrfs_root **sub_root)
420{
421 struct btrfs_path *path;
422 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400423
424 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
425 return 0;
426 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
427 return 0;
428
429 path = btrfs_alloc_path();
430 BUG_ON(!path);
431 mutex_lock(&root->fs_info->fs_mutex);
432
Chris Mason0f7d52f2007-04-09 10:42:37 -0400433 *sub_root = btrfs_read_fs_root(root->fs_info, location);
434 if (IS_ERR(*sub_root))
435 return PTR_ERR(*sub_root);
436
437 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400438 location->objectid = btrfs_root_dirid(ri);
439 location->flags = 0;
440 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
441 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400442
Chris Masond6e4a422007-04-06 15:37:36 -0400443 btrfs_free_path(path);
444 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400445 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400446}
447
Chris Masonc5739bb2007-04-10 09:27:04 -0400448int btrfs_init_locked_inode(struct inode *inode, void *p)
449{
450 struct btrfs_iget_args *args = p;
451 inode->i_ino = args->ino;
452 BTRFS_I(inode)->root = args->root;
453 return 0;
454}
455
456int btrfs_find_actor(struct inode *inode, void *opaque)
457{
458 struct btrfs_iget_args *args = opaque;
459 return (args->ino == inode->i_ino &&
460 args->root == BTRFS_I(inode)->root);
461}
462
463struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
464 struct btrfs_root *root)
465{
466 struct inode *inode;
467 struct btrfs_iget_args args;
468 args.ino = objectid;
469 args.root = root;
470
471 inode = iget5_locked(s, objectid, btrfs_find_actor,
472 btrfs_init_locked_inode,
473 (void *)&args);
474 return inode;
475}
Chris Masond6e4a422007-04-06 15:37:36 -0400476
Chris Masone20d96d2007-03-22 12:13:20 -0400477static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
478 struct nameidata *nd)
479{
480 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400481 struct btrfs_inode *bi = BTRFS_I(dir);
482 struct btrfs_root *root = bi->root;
483 struct btrfs_root *sub_root = root;
484 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400485 int ret;
486
487 if (dentry->d_name.len > BTRFS_NAME_LEN)
488 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400489 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400490 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400491 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400492 if (ret < 0)
493 return ERR_PTR(ret);
494 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400495 if (location.objectid) {
496 ret = fixup_tree_root_location(root, &location, &sub_root);
497 if (ret < 0)
498 return ERR_PTR(ret);
499 if (ret > 0)
500 return ERR_PTR(-ENOENT);
Chris Masonc5739bb2007-04-10 09:27:04 -0400501 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
502 sub_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400503 if (!inode)
504 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400505 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400506 if (sub_root != root) {
Chris Masonc5739bb2007-04-10 09:27:04 -0400507printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400508 igrab(inode);
509 sub_root->inode = inode;
510 }
Chris Masond6e4a422007-04-06 15:37:36 -0400511 BTRFS_I(inode)->root = sub_root;
512 memcpy(&BTRFS_I(inode)->location, &location,
513 sizeof(location));
514 btrfs_read_locked_inode(inode);
515 unlock_new_inode(inode);
516 }
Chris Masone20d96d2007-03-22 12:13:20 -0400517 }
518 return d_splice_alias(inode, dentry);
519}
520
521static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
522{
523 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400524 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400525 struct btrfs_item *item;
526 struct btrfs_dir_item *di;
527 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400528 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400529 int ret;
530 u32 nritems;
531 struct btrfs_leaf *leaf;
532 int slot;
533 int advance;
534 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400535 int over = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400536 u32 di_cur;
537 u32 di_total;
538 u32 di_len;
539 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Masond6e4a422007-04-06 15:37:36 -0400540
541 /* FIXME, use a real flag for deciding about the key type */
542 if (root->fs_info->tree_root == root)
543 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400544 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400545 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400546 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400547 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400548 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400549 path = btrfs_alloc_path();
550 btrfs_init_path(path);
551 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400552 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400553 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400554 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400555 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400556 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400557 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400558 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400559 if (advance || slot >= nritems) {
560 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400561 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400562 if (ret)
563 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400564 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400565 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400566 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400567 } else {
568 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400569 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400570 }
571 }
572 advance = 1;
573 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400574 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
575 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400576 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masona429e512007-04-18 16:15:28 -0400577 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400578 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
579 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400580 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400581 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400582 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400583 di_cur = 0;
584 di_total = btrfs_item_size(leaf->items + slot);
585 while(di_cur < di_total) {
586 over = filldir(dirent, (const char *)(di + 1),
587 btrfs_dir_name_len(di),
588 btrfs_disk_key_offset(&item->key),
589 btrfs_disk_key_objectid(&di->location),
590 d_type);
591 if (over)
592 goto nopos;
593 di_len = btrfs_dir_name_len(di) + sizeof(*di);
594 di_cur += di_len;
595 di = (struct btrfs_dir_item *)((char *)di + di_len);
596 }
Chris Masone20d96d2007-03-22 12:13:20 -0400597 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400598 filp->f_pos++;
599nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400600 ret = 0;
601err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400602 btrfs_release_path(root, path);
603 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400604 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400605 return ret;
606}
607
608static void btrfs_put_super (struct super_block * sb)
609{
610 struct btrfs_root *root = btrfs_sb(sb);
611 int ret;
612
613 ret = close_ctree(root);
614 if (ret) {
615 printk("close ctree returns %d\n", ret);
616 }
617 sb->s_fs_info = NULL;
618}
Chris Mason2e635a22007-03-21 11:12:56 -0400619
620static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
621{
622 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400623 struct dentry * root_dentry;
624 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400625 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400626 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400627
628 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400629 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400630 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400631 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400632
Chris Mason0f7d52f2007-04-09 10:42:37 -0400633 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400634
Chris Mason0f7d52f2007-04-09 10:42:37 -0400635 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400636 printk("btrfs: open_ctree failed\n");
637 return -EIO;
638 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400639 sb->s_fs_info = tree_root;
640 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400641 printk("read in super total blocks %Lu root %Lu\n",
642 btrfs_super_total_blocks(disk_super),
643 btrfs_super_root_dir(disk_super));
644
Chris Masonc5739bb2007-04-10 09:27:04 -0400645 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
646 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400647 bi = BTRFS_I(inode);
648 bi->location.objectid = inode->i_ino;
649 bi->location.offset = 0;
650 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400651 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400652 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
653
Chris Mason2e635a22007-03-21 11:12:56 -0400654 if (!inode)
655 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400656 if (inode->i_state & I_NEW) {
657 btrfs_read_locked_inode(inode);
658 unlock_new_inode(inode);
659 }
Chris Mason2e635a22007-03-21 11:12:56 -0400660
Chris Masone20d96d2007-03-22 12:13:20 -0400661 root_dentry = d_alloc_root(inode);
662 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400663 iput(inode);
664 return -ENOMEM;
665 }
Chris Masone20d96d2007-03-22 12:13:20 -0400666 sb->s_root = root_dentry;
667
Chris Mason2e635a22007-03-21 11:12:56 -0400668 return 0;
669}
670
Chris Masond5719762007-03-23 10:01:08 -0400671static void fill_inode_item(struct btrfs_inode_item *item,
672 struct inode *inode)
673{
674 btrfs_set_inode_uid(item, inode->i_uid);
675 btrfs_set_inode_gid(item, inode->i_gid);
676 btrfs_set_inode_size(item, inode->i_size);
677 btrfs_set_inode_mode(item, inode->i_mode);
678 btrfs_set_inode_nlink(item, inode->i_nlink);
679 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
680 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
681 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
682 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
683 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
684 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
685 btrfs_set_inode_nblocks(item, inode->i_blocks);
686 btrfs_set_inode_generation(item, inode->i_generation);
687}
688
Chris Mason4730a4b2007-03-26 12:00:39 -0400689static int btrfs_update_inode(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root,
691 struct inode *inode)
692{
693 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400694 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400695 int ret;
696
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 path = btrfs_alloc_path();
698 BUG_ON(!path);
699 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400700 ret = btrfs_lookup_inode(trans, root, path,
701 &BTRFS_I(inode)->location, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400702 if (ret) {
703 if (ret > 0)
704 ret = -ENOENT;
705 goto failed;
706 }
707
Chris Mason5caf2a02007-04-02 11:20:42 -0400708 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
709 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400710 struct btrfs_inode_item);
711
712 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400713 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason1b05da22007-04-10 12:13:09 -0400714 ret = 0;
Chris Mason4730a4b2007-03-26 12:00:39 -0400715failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400716 btrfs_release_path(root, path);
717 btrfs_free_path(path);
Chris Mason1b05da22007-04-10 12:13:09 -0400718 return ret;
Chris Mason4730a4b2007-03-26 12:00:39 -0400719}
720
721static int btrfs_write_inode(struct inode *inode, int wait)
722{
Chris Masond6e4a422007-04-06 15:37:36 -0400723 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400724 struct btrfs_trans_handle *trans;
725 int ret;
726
727 mutex_lock(&root->fs_info->fs_mutex);
728 trans = btrfs_start_transaction(root, 1);
729 ret = btrfs_update_inode(trans, root, inode);
730 if (wait)
731 btrfs_commit_transaction(trans, root);
732 else
733 btrfs_end_transaction(trans, root);
734 mutex_unlock(&root->fs_info->fs_mutex);
735 return ret;
736}
737
Chris Masond5719762007-03-23 10:01:08 -0400738static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400739 struct btrfs_root *root,
740 u64 objectid, int mode)
Chris Masond5719762007-03-23 10:01:08 -0400741{
742 struct inode *inode;
743 struct btrfs_inode_item inode_item;
Chris Mason1b05da22007-04-10 12:13:09 -0400744 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400745 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400746
Chris Mason2619ba12007-04-10 16:58:11 -0400747 inode = new_inode(root->fs_info->sb);
Chris Masond5719762007-03-23 10:01:08 -0400748 if (!inode)
749 return ERR_PTR(-ENOMEM);
750
Chris Mason2619ba12007-04-10 16:58:11 -0400751 BTRFS_I(inode)->root = root;
Chris Masond5719762007-03-23 10:01:08 -0400752
753 inode->i_uid = current->fsuid;
754 inode->i_gid = current->fsgid;
755 inode->i_mode = mode;
756 inode->i_ino = objectid;
757 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -0400758 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -0400759 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -0400760 location = &BTRFS_I(inode)->location;
761 location->objectid = objectid;
762 location->flags = 0;
763 location->offset = 0;
764 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -0400765
766 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
767 BUG_ON(ret);
768
769 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400770 return inode;
771}
772
773static int btrfs_add_link(struct btrfs_trans_handle *trans,
774 struct dentry *dentry, struct inode *inode)
775{
776 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400777 struct btrfs_key key;
778 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
779 key.objectid = inode->i_ino;
780 key.flags = 0;
781 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
782 key.offset = 0;
783
784 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400785 dentry->d_name.name, dentry->d_name.len,
786 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400787 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400788 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400789 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400790 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400791 dentry->d_parent->d_inode);
792 }
Chris Masond5719762007-03-23 10:01:08 -0400793 return ret;
794}
795
796static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
797 struct dentry *dentry, struct inode *inode)
798{
799 int err = btrfs_add_link(trans, dentry, inode);
800 if (!err) {
801 d_instantiate(dentry, inode);
802 return 0;
803 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400804 if (err > 0)
805 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -0400806 return err;
807}
808
809static int btrfs_create(struct inode *dir, struct dentry *dentry,
810 int mode, struct nameidata *nd)
811{
812 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400813 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400814 struct inode *inode;
815 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400816 int drop_inode = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400817 u64 objectid;
Chris Masond5719762007-03-23 10:01:08 -0400818
Chris Masond561c022007-03-23 19:47:49 -0400819 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400820 trans = btrfs_start_transaction(root, 1);
Chris Mason2619ba12007-04-10 16:58:11 -0400821
822 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
823 if (err) {
824 err = -ENOSPC;
825 goto out_unlock;
826 }
827
828 inode = btrfs_new_inode(trans, root, objectid, mode);
Chris Masond5719762007-03-23 10:01:08 -0400829 err = PTR_ERR(inode);
830 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400831 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400832 // FIXME mark the inode dirty
833 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400834 if (err)
835 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400836 else {
837 inode->i_mapping->a_ops = &btrfs_aops;
838 inode->i_fop = &btrfs_file_operations;
839 inode->i_op = &btrfs_file_inode_operations;
840 }
Chris Masond5719762007-03-23 10:01:08 -0400841 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400842out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400843 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400844 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400845
Chris Mason134e9732007-03-25 13:44:56 -0400846 if (drop_inode) {
847 inode_dec_link_count(inode);
848 iput(inode);
849 }
Chris Masond5719762007-03-23 10:01:08 -0400850 return err;
851}
852
Chris Masonf7922032007-03-25 20:17:36 -0400853static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400854 struct btrfs_root *root,
855 u64 objectid, u64 dirid)
Chris Masonf7922032007-03-25 20:17:36 -0400856{
Chris Masonf7922032007-03-25 20:17:36 -0400857 int ret;
858 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400859 struct btrfs_key key;
860
Chris Masonf7922032007-03-25 20:17:36 -0400861 buf[0] = '.';
862 buf[1] = '.';
863
Chris Mason2619ba12007-04-10 16:58:11 -0400864 key.objectid = objectid;
Chris Masond6e4a422007-04-06 15:37:36 -0400865 key.offset = 0;
866 key.flags = 0;
867 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
868
Chris Mason2619ba12007-04-10 16:58:11 -0400869 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400870 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400871 if (ret)
872 goto error;
Chris Mason2619ba12007-04-10 16:58:11 -0400873 key.objectid = dirid;
874 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400875 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400876 if (ret)
877 goto error;
Chris Masonf7922032007-03-25 20:17:36 -0400878error:
879 return ret;
880}
881
882static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
883{
884 struct inode *inode;
885 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400886 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400887 int err = 0;
888 int drop_on_err = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400889 u64 objectid;
Chris Masonf7922032007-03-25 20:17:36 -0400890
891 mutex_lock(&root->fs_info->fs_mutex);
892 trans = btrfs_start_transaction(root, 1);
893 if (IS_ERR(trans)) {
894 err = PTR_ERR(trans);
895 goto out_unlock;
896 }
Chris Mason2619ba12007-04-10 16:58:11 -0400897
898 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
899 if (err) {
900 err = -ENOSPC;
901 goto out_unlock;
902 }
903
904 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
Chris Masonf7922032007-03-25 20:17:36 -0400905 if (IS_ERR(inode)) {
906 err = PTR_ERR(inode);
907 goto out_fail;
908 }
909 drop_on_err = 1;
910 inode->i_op = &btrfs_dir_inode_operations;
911 inode->i_fop = &btrfs_dir_file_operations;
912
Chris Mason2619ba12007-04-10 16:58:11 -0400913 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
914 if (err)
915 goto out_fail;
916
917 inode->i_size = 6;
918 err = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400919 if (err)
920 goto out_fail;
921 err = btrfs_add_link(trans, dentry, inode);
922 if (err)
923 goto out_fail;
924 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400925 drop_on_err = 0;
926
927out_fail:
928 btrfs_end_transaction(trans, root);
929out_unlock:
930 mutex_unlock(&root->fs_info->fs_mutex);
931 if (drop_on_err)
932 iput(inode);
933 return err;
934}
935
Chris Mason8fd17792007-04-19 21:01:03 -0400936static int btrfs_sync_file(struct file *file,
937 struct dentry *dentry, int datasync)
938{
939 struct inode *inode = dentry->d_inode;
940 struct btrfs_root *root = BTRFS_I(inode)->root;
941 int ret;
942 struct btrfs_trans_handle *trans;
943
944 mutex_lock(&root->fs_info->fs_mutex);
945 trans = btrfs_start_transaction(root, 1);
946 if (!trans) {
947 ret = -ENOMEM;
948 goto out;
949 }
950 ret = btrfs_commit_transaction(trans, root);
951 mutex_unlock(&root->fs_info->fs_mutex);
952out:
953 return ret > 0 ? EIO : ret;
954}
955
Chris Masond5719762007-03-23 10:01:08 -0400956static int btrfs_sync_fs(struct super_block *sb, int wait)
957{
958 struct btrfs_trans_handle *trans;
959 struct btrfs_root *root;
960 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400961 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400962
Chris Masond5719762007-03-23 10:01:08 -0400963 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400964 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400965 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400966 return 0;
967 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400968 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400969 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400970 trans = btrfs_start_transaction(root, 1);
971 ret = btrfs_commit_transaction(trans, root);
972 sb->s_dirt = 0;
973 BUG_ON(ret);
974printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400975 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400976 return 0;
977}
978
Chris Mason75dfe392007-03-29 11:56:46 -0400979static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400980 struct buffer_head *result, int create)
981{
982 int ret;
983 int err = 0;
984 u64 blocknr;
985 u64 extent_start = 0;
986 u64 extent_end = 0;
987 u64 objectid = inode->i_ino;
Chris Mason236454d2007-04-19 13:37:44 -0400988 u32 found_type;
Chris Mason5caf2a02007-04-02 11:20:42 -0400989 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400990 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -0400991 struct btrfs_file_extent_item *item;
992 struct btrfs_leaf *leaf;
993 struct btrfs_disk_key *found_key;
994
Chris Mason5caf2a02007-04-02 11:20:42 -0400995 path = btrfs_alloc_path();
996 BUG_ON(!path);
997 btrfs_init_path(path);
Chris Mason6567e832007-04-16 09:22:45 -0400998 if (create) {
Chris Mason6567e832007-04-16 09:22:45 -0400999 WARN_ON(1);
1000 }
Chris Masondee26a92007-03-26 16:00:06 -04001001
Chris Mason236454d2007-04-19 13:37:44 -04001002 ret = btrfs_lookup_file_extent(NULL, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001003 inode->i_ino,
Chris Mason236454d2007-04-19 13:37:44 -04001004 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001005 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001006 err = ret;
1007 goto out;
1008 }
1009
1010 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001011 if (path->slots[0] == 0) {
1012 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001013 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001014 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001015 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001016 }
1017
Chris Mason5caf2a02007-04-02 11:20:42 -04001018 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001019 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001020 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001021 blocknr = btrfs_file_extent_disk_blocknr(item);
1022 blocknr += btrfs_file_extent_offset(item);
1023
Chris Masondee26a92007-03-26 16:00:06 -04001024 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001025 found_key = &leaf->items[path->slots[0]].key;
Chris Mason236454d2007-04-19 13:37:44 -04001026 found_type = btrfs_disk_key_type(found_key);
Chris Masondee26a92007-03-26 16:00:06 -04001027 if (btrfs_disk_key_objectid(found_key) != objectid ||
Chris Mason236454d2007-04-19 13:37:44 -04001028 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masondee26a92007-03-26 16:00:06 -04001029 extent_end = 0;
1030 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001031 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001032 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001033 }
Chris Mason236454d2007-04-19 13:37:44 -04001034 found_type = btrfs_file_extent_type(item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001035 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason236454d2007-04-19 13:37:44 -04001036 if (found_type == BTRFS_FILE_EXTENT_REG) {
1037 extent_start = extent_start >> inode->i_blkbits;
1038 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1039 if (iblock >= extent_start && iblock < extent_end) {
1040 err = 0;
1041 btrfs_map_bh_to_logical(root, result, blocknr +
1042 iblock - extent_start);
1043 goto out;
1044 }
1045 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1046 char *ptr;
1047 char *map;
1048 u32 size;
1049 size = btrfs_file_extent_inline_len(leaf->items +
1050 path->slots[0]);
1051 extent_end = (extent_start + size) >> inode->i_blkbits;
1052 extent_start >>= inode->i_blkbits;
1053 if (iblock < extent_start || iblock > extent_end) {
1054 goto out;
1055 }
1056 ptr = btrfs_file_extent_inline_start(item);
1057 map = kmap(result->b_page);
1058 memcpy(map, ptr, size);
1059 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1060 flush_dcache_page(result->b_page);
1061 kunmap(result->b_page);
1062 set_buffer_uptodate(result);
1063 SetPageChecked(result->b_page);
1064 btrfs_map_bh_to_logical(root, result, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001065 }
Chris Masondee26a92007-03-26 16:00:06 -04001066out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001067 btrfs_release_path(root, path);
1068 btrfs_free_path(path);
Chris Mason75dfe392007-03-29 11:56:46 -04001069 return err;
1070}
1071
1072static int btrfs_get_block(struct inode *inode, sector_t iblock,
1073 struct buffer_head *result, int create)
1074{
1075 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001076 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001077 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001078 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001079 mutex_unlock(&root->fs_info->fs_mutex);
1080 return err;
1081}
1082
1083static int btrfs_prepare_write(struct file *file, struct page *page,
1084 unsigned from, unsigned to)
1085{
Chris Mason6407bf62007-03-27 06:33:00 -04001086 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001087}
1088
Chris Masond561c022007-03-23 19:47:49 -04001089static void btrfs_write_super(struct super_block *sb)
1090{
1091 btrfs_sync_fs(sb, 1);
1092}
1093
Chris Masondee26a92007-03-26 16:00:06 -04001094static int btrfs_readpage(struct file *file, struct page *page)
1095{
1096 return mpage_readpage(page, btrfs_get_block);
1097}
1098
Chris Mason236454d2007-04-19 13:37:44 -04001099/*
1100 * While block_write_full_page is writing back the dirty buffers under
1101 * the page lock, whoever dirtied the buffers may decide to clean them
1102 * again at any time. We handle that by only looking at the buffer
1103 * state inside lock_buffer().
1104 *
1105 * If block_write_full_page() is called for regular writeback
1106 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1107 * locked buffer. This only can happen if someone has written the buffer
1108 * directly, with submit_bh(). At the address_space level PageWriteback
1109 * prevents this contention from occurring.
1110 */
1111static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1112 struct writeback_control *wbc)
1113{
1114 int err;
1115 sector_t block;
1116 sector_t last_block;
1117 struct buffer_head *bh, *head;
1118 const unsigned blocksize = 1 << inode->i_blkbits;
1119 int nr_underway = 0;
1120
1121 BUG_ON(!PageLocked(page));
1122
1123 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1124
1125 if (!page_has_buffers(page)) {
1126 create_empty_buffers(page, blocksize,
1127 (1 << BH_Dirty)|(1 << BH_Uptodate));
1128 }
1129
1130 /*
1131 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1132 * here, and the (potentially unmapped) buffers may become dirty at
1133 * any time. If a buffer becomes dirty here after we've inspected it
1134 * then we just miss that fact, and the page stays dirty.
1135 *
1136 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1137 * handle that here by just cleaning them.
1138 */
1139
1140 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1141 head = page_buffers(page);
1142 bh = head;
1143
1144 /*
1145 * Get all the dirty buffers mapped to disk addresses and
1146 * handle any aliases from the underlying blockdev's mapping.
1147 */
1148 do {
1149 if (block > last_block) {
1150 /*
1151 * mapped buffers outside i_size will occur, because
1152 * this page can be outside i_size when there is a
1153 * truncate in progress.
1154 */
1155 /*
1156 * The buffer was zeroed by block_write_full_page()
1157 */
1158 clear_buffer_dirty(bh);
1159 set_buffer_uptodate(bh);
1160 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1161 WARN_ON(bh->b_size != blocksize);
1162 err = btrfs_get_block(inode, block, bh, 0);
1163 if (err)
1164 goto recover;
1165 if (buffer_new(bh)) {
1166 /* blockdev mappings never come here */
1167 clear_buffer_new(bh);
1168 unmap_underlying_metadata(bh->b_bdev,
1169 bh->b_blocknr);
1170 }
1171 }
1172 bh = bh->b_this_page;
1173 block++;
1174 } while (bh != head);
1175
1176 do {
1177 if (!buffer_mapped(bh))
1178 continue;
1179 /*
1180 * If it's a fully non-blocking write attempt and we cannot
1181 * lock the buffer then redirty the page. Note that this can
1182 * potentially cause a busy-wait loop from pdflush and kswapd
1183 * activity, but those code paths have their own higher-level
1184 * throttling.
1185 */
1186 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1187 lock_buffer(bh);
1188 } else if (test_set_buffer_locked(bh)) {
1189 redirty_page_for_writepage(wbc, page);
1190 continue;
1191 }
1192 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1193 mark_buffer_async_write(bh);
1194 } else {
1195 unlock_buffer(bh);
1196 }
1197 } while ((bh = bh->b_this_page) != head);
1198
1199 /*
1200 * The page and its buffers are protected by PageWriteback(), so we can
1201 * drop the bh refcounts early.
1202 */
1203 BUG_ON(PageWriteback(page));
1204 set_page_writeback(page);
1205
1206 do {
1207 struct buffer_head *next = bh->b_this_page;
1208 if (buffer_async_write(bh)) {
1209 submit_bh(WRITE, bh);
1210 nr_underway++;
1211 }
1212 bh = next;
1213 } while (bh != head);
1214 unlock_page(page);
1215
1216 err = 0;
1217done:
1218 if (nr_underway == 0) {
1219 /*
1220 * The page was marked dirty, but the buffers were
1221 * clean. Someone wrote them back by hand with
1222 * ll_rw_block/submit_bh. A rare case.
1223 */
1224 int uptodate = 1;
1225 do {
1226 if (!buffer_uptodate(bh)) {
1227 uptodate = 0;
1228 break;
1229 }
1230 bh = bh->b_this_page;
1231 } while (bh != head);
1232 if (uptodate)
1233 SetPageUptodate(page);
1234 end_page_writeback(page);
1235 /*
1236 * The page and buffer_heads can be released at any time from
1237 * here on.
1238 */
1239 wbc->pages_skipped++; /* We didn't write this page */
1240 }
1241 return err;
1242
1243recover:
1244 /*
1245 * ENOSPC, or some other error. We may already have added some
1246 * blocks to the file, so we need to write these out to avoid
1247 * exposing stale data.
1248 * The page is currently locked and not marked for writeback
1249 */
1250 bh = head;
1251 /* Recovery: lock and submit the mapped buffers */
1252 do {
1253 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1254 lock_buffer(bh);
1255 mark_buffer_async_write(bh);
1256 } else {
1257 /*
1258 * The buffer may have been set dirty during
1259 * attachment to a dirty page.
1260 */
1261 clear_buffer_dirty(bh);
1262 }
1263 } while ((bh = bh->b_this_page) != head);
1264 SetPageError(page);
1265 BUG_ON(PageWriteback(page));
1266 set_page_writeback(page);
1267 do {
1268 struct buffer_head *next = bh->b_this_page;
1269 if (buffer_async_write(bh)) {
1270 clear_buffer_dirty(bh);
1271 submit_bh(WRITE, bh);
1272 nr_underway++;
1273 }
1274 bh = next;
1275 } while (bh != head);
1276 unlock_page(page);
1277 goto done;
1278}
1279
1280/*
1281 * The generic ->writepage function for buffer-backed address_spaces
1282 */
Chris Masondee26a92007-03-26 16:00:06 -04001283static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1284{
Chris Mason236454d2007-04-19 13:37:44 -04001285 struct inode * const inode = page->mapping->host;
1286 loff_t i_size = i_size_read(inode);
1287 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1288 unsigned offset;
1289 void *kaddr;
1290
1291 /* Is the page fully inside i_size? */
1292 if (page->index < end_index)
1293 return __btrfs_write_full_page(inode, page, wbc);
1294
1295 /* Is the page fully outside i_size? (truncate in progress) */
1296 offset = i_size & (PAGE_CACHE_SIZE-1);
1297 if (page->index >= end_index+1 || !offset) {
1298 /*
1299 * The page may have dirty, unmapped buffers. For example,
1300 * they may have been added in ext3_writepage(). Make them
1301 * freeable here, so the page does not leak.
1302 */
1303 block_invalidatepage(page, 0);
1304 unlock_page(page);
1305 return 0; /* don't care */
1306 }
1307
1308 /*
1309 * The page straddles i_size. It must be zeroed out on each and every
1310 * writepage invokation because it may be mmapped. "A file is mapped
1311 * in multiples of the page size. For a file that is not a multiple of
1312 * the page size, the remaining memory is zeroed when mapped, and
1313 * writes to that region are not written out to the file."
1314 */
1315 kaddr = kmap_atomic(page, KM_USER0);
1316 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1317 flush_dcache_page(page);
1318 kunmap_atomic(kaddr, KM_USER0);
1319 return __btrfs_write_full_page(inode, page, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001320}
Chris Masond561c022007-03-23 19:47:49 -04001321
Chris Masonf4b9aa82007-03-27 11:05:53 -04001322static void btrfs_truncate(struct inode *inode)
1323{
Chris Masond6e4a422007-04-06 15:37:36 -04001324 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001325 int ret;
1326 struct btrfs_trans_handle *trans;
1327
1328 if (!S_ISREG(inode->i_mode))
1329 return;
1330 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1331 return;
1332
Chris Masone8f05c42007-04-04 14:30:09 -04001333 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001334
1335 /* FIXME, add redo link to tree so we don't leak on crash */
1336 mutex_lock(&root->fs_info->fs_mutex);
1337 trans = btrfs_start_transaction(root, 1);
1338 ret = btrfs_truncate_in_trans(trans, root, inode);
1339 BUG_ON(ret);
1340 ret = btrfs_end_transaction(trans, root);
1341 BUG_ON(ret);
1342 mutex_unlock(&root->fs_info->fs_mutex);
1343 mark_inode_dirty(inode);
1344}
1345
Chris Mason236454d2007-04-19 13:37:44 -04001346/*
1347 * Make sure any changes to nobh_commit_write() are reflected in
1348 * nobh_truncate_page(), since it doesn't call commit_write().
1349 */
1350static int btrfs_commit_write(struct file *file, struct page *page,
1351 unsigned from, unsigned to)
1352{
1353 struct inode *inode = page->mapping->host;
1354 struct buffer_head *bh;
1355 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1356
1357 SetPageUptodate(page);
1358 bh = page_buffers(page);
1359 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1360 set_page_dirty(page);
1361 }
1362 if (pos > inode->i_size) {
1363 i_size_write(inode, pos);
1364 mark_inode_dirty(inode);
1365 }
1366 return 0;
1367}
1368
Chris Mason75dfe392007-03-29 11:56:46 -04001369static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1370 struct page **prepared_pages,
1371 const char __user * buf)
1372{
1373 long page_fault = 0;
1374 int i;
1375 int offset = pos & (PAGE_CACHE_SIZE - 1);
1376
1377 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1378 size_t count = min_t(size_t,
1379 PAGE_CACHE_SIZE - offset, write_bytes);
1380 struct page *page = prepared_pages[i];
1381 fault_in_pages_readable(buf, count);
1382
1383 /* Copy data from userspace to the current page */
1384 kmap(page);
1385 page_fault = __copy_from_user(page_address(page) + offset,
1386 buf, count);
1387 /* Flush processor's dcache for this page */
1388 flush_dcache_page(page);
1389 kunmap(page);
1390 buf += count;
1391 write_bytes -= count;
1392
1393 if (page_fault)
1394 break;
1395 }
1396 return page_fault ? -EFAULT : 0;
1397}
1398
1399static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1400{
1401 size_t i;
1402 for (i = 0; i < num_pages; i++) {
1403 if (!pages[i])
1404 break;
1405 unlock_page(pages[i]);
1406 mark_page_accessed(pages[i]);
1407 page_cache_release(pages[i]);
1408 }
1409}
1410static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1411 struct btrfs_root *root,
1412 struct file *file,
1413 struct page **pages,
1414 size_t num_pages,
1415 loff_t pos,
1416 size_t write_bytes)
1417{
1418 int i;
1419 int offset;
1420 int err = 0;
1421 int ret;
1422 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001423 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason236454d2007-04-19 13:37:44 -04001424 struct buffer_head *bh;
1425 struct btrfs_file_extent_item *ei;
Chris Mason75dfe392007-03-29 11:56:46 -04001426
1427 for (i = 0; i < num_pages; i++) {
1428 offset = pos & (PAGE_CACHE_SIZE -1);
1429 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001430 /* FIXME, one block at a time */
1431
1432 mutex_lock(&root->fs_info->fs_mutex);
1433 trans = btrfs_start_transaction(root, 1);
Chris Mason236454d2007-04-19 13:37:44 -04001434
1435 bh = page_buffers(pages[i]);
1436 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1437 struct btrfs_key key;
1438 struct btrfs_path *path;
1439 char *ptr;
1440 u32 datasize;
1441
1442 path = btrfs_alloc_path();
1443 BUG_ON(!path);
1444 key.objectid = inode->i_ino;
1445 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1446 key.flags = 0;
1447 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1448 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1449 datasize = offset +
1450 btrfs_file_extent_calc_inline_size(write_bytes);
1451 ret = btrfs_insert_empty_item(trans, root, path, &key,
1452 datasize);
1453 BUG_ON(ret);
1454 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1455 path->slots[0], struct btrfs_file_extent_item);
1456 btrfs_set_file_extent_generation(ei, trans->transid);
1457 btrfs_set_file_extent_type(ei,
1458 BTRFS_FILE_EXTENT_INLINE);
1459 ptr = btrfs_file_extent_inline_start(ei);
1460 memcpy(ptr, bh->b_data, offset + write_bytes);
1461 mark_buffer_dirty(path->nodes[0]);
1462 btrfs_free_path(path);
1463 } else {
1464 btrfs_csum_file_block(trans, root, inode->i_ino,
Chris Masonf254e522007-03-29 15:15:27 -04001465 pages[i]->index << PAGE_CACHE_SHIFT,
1466 kmap(pages[i]), PAGE_CACHE_SIZE);
Chris Mason236454d2007-04-19 13:37:44 -04001467 kunmap(pages[i]);
1468 }
Chris Masonf254e522007-03-29 15:15:27 -04001469 SetPageChecked(pages[i]);
1470 ret = btrfs_end_transaction(trans, root);
1471 BUG_ON(ret);
1472 mutex_unlock(&root->fs_info->fs_mutex);
1473
Chris Mason236454d2007-04-19 13:37:44 -04001474 ret = btrfs_commit_write(file, pages[i], offset,
Chris Mason75dfe392007-03-29 11:56:46 -04001475 offset + this_write);
1476 pos += this_write;
1477 if (ret) {
1478 err = ret;
1479 goto failed;
1480 }
1481 WARN_ON(this_write > write_bytes);
1482 write_bytes -= this_write;
1483 }
1484failed:
1485 return err;
1486}
1487
Chris Masonb18c6682007-04-17 13:26:50 -04001488static int drop_extents(struct btrfs_trans_handle *trans,
1489 struct btrfs_root *root,
1490 struct inode *inode,
1491 u64 start, u64 end)
1492{
1493 int ret;
1494 struct btrfs_key key;
1495 struct btrfs_leaf *leaf;
1496 int slot;
1497 struct btrfs_file_extent_item *extent;
Chris Mason236454d2007-04-19 13:37:44 -04001498 u64 extent_end = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001499 int keep;
1500 struct btrfs_file_extent_item old;
1501 struct btrfs_path *path;
1502 u64 search_start = start;
1503 int bookend;
Chris Mason236454d2007-04-19 13:37:44 -04001504 int found_type;
1505 int found_extent;
1506 int found_inline;
1507
Chris Masonb18c6682007-04-17 13:26:50 -04001508 path = btrfs_alloc_path();
1509 if (!path)
1510 return -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001511 while(1) {
Chris Masona429e512007-04-18 16:15:28 -04001512 btrfs_release_path(root, path);
1513 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1514 search_start, -1);
1515 if (ret < 0)
1516 goto out;
1517 if (ret > 0) {
1518 if (path->slots[0] == 0) {
Chris Mason236454d2007-04-19 13:37:44 -04001519 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001520 goto out;
1521 }
1522 path->slots[0]--;
1523 }
Chris Masonb18c6682007-04-17 13:26:50 -04001524 keep = 0;
1525 bookend = 0;
Chris Mason236454d2007-04-19 13:37:44 -04001526 found_extent = 0;
1527 found_inline = 0;
1528 extent = NULL;
Chris Masonb18c6682007-04-17 13:26:50 -04001529 leaf = btrfs_buffer_leaf(path->nodes[0]);
1530 slot = path->slots[0];
1531 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
Chris Masonb18c6682007-04-17 13:26:50 -04001532 if (key.offset >= end || key.objectid != inode->i_ino) {
1533 ret = 0;
1534 goto out;
1535 }
Chris Mason236454d2007-04-19 13:37:44 -04001536 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1537 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001538 goto out;
Chris Mason236454d2007-04-19 13:37:44 -04001539 }
1540 extent = btrfs_item_ptr(leaf, slot,
1541 struct btrfs_file_extent_item);
1542 found_type = btrfs_file_extent_type(extent);
1543 if (found_type == BTRFS_FILE_EXTENT_REG) {
1544 extent_end = key.offset +
1545 (btrfs_file_extent_num_blocks(extent) <<
1546 inode->i_blkbits);
1547 found_extent = 1;
1548 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1549 found_inline = 1;
1550 extent_end = key.offset +
1551 btrfs_file_extent_inline_len(leaf->items + slot);
1552 }
1553
1554 if (!found_extent && !found_inline) {
1555 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001556 goto out;
Chris Mason236454d2007-04-19 13:37:44 -04001557 }
1558
1559 if (search_start >= extent_end) {
1560 ret = 0;
1561 goto out;
1562 }
1563
Chris Masona429e512007-04-18 16:15:28 -04001564 search_start = extent_end;
Chris Masonb18c6682007-04-17 13:26:50 -04001565
1566 if (end < extent_end && end >= key.offset) {
Chris Mason236454d2007-04-19 13:37:44 -04001567 if (found_extent) {
1568 memcpy(&old, extent, sizeof(old));
1569 ret = btrfs_inc_extent_ref(trans, root,
1570 btrfs_file_extent_disk_blocknr(&old),
1571 btrfs_file_extent_disk_num_blocks(&old));
1572 BUG_ON(ret);
1573 }
1574 WARN_ON(found_inline);
Chris Masonb18c6682007-04-17 13:26:50 -04001575 bookend = 1;
1576 }
1577
1578 if (start > key.offset) {
1579 u64 new_num;
Chris Masona429e512007-04-18 16:15:28 -04001580 u64 old_num;
Chris Masonb18c6682007-04-17 13:26:50 -04001581 /* truncate existing extent */
1582 keep = 1;
1583 WARN_ON(start & (root->blocksize - 1));
Chris Mason236454d2007-04-19 13:37:44 -04001584 if (found_extent) {
1585 new_num = (start - key.offset) >>
1586 inode->i_blkbits;
1587 old_num = btrfs_file_extent_num_blocks(extent);
1588 inode->i_blocks -= (old_num - new_num) << 3;
1589 btrfs_set_file_extent_num_blocks(extent,
1590 new_num);
1591 mark_buffer_dirty(path->nodes[0]);
1592 } else {
1593 WARN_ON(1);
1594 /*
1595 ret = btrfs_truncate_item(trans, root, path,
1596 start - key.offset);
1597 BUG_ON(ret);
1598 */
1599 }
Chris Masonb18c6682007-04-17 13:26:50 -04001600 }
1601 if (!keep) {
Chris Mason236454d2007-04-19 13:37:44 -04001602 u64 disk_blocknr = 0;
1603 u64 disk_num_blocks = 0;
1604 u64 extent_num_blocks = 0;
1605 if (found_extent) {
1606 disk_blocknr =
1607 btrfs_file_extent_disk_blocknr(extent);
1608 disk_num_blocks =
1609 btrfs_file_extent_disk_num_blocks(extent);
1610 extent_num_blocks =
1611 btrfs_file_extent_num_blocks(extent);
1612 }
Chris Masonb18c6682007-04-17 13:26:50 -04001613 ret = btrfs_del_item(trans, root, path);
1614 BUG_ON(ret);
1615 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001616 if (found_extent) {
1617 inode->i_blocks -=
1618 btrfs_file_extent_num_blocks(extent) << 3;
1619 ret = btrfs_free_extent(trans, root,
1620 disk_blocknr,
1621 disk_num_blocks, 0);
1622 }
Chris Masonb18c6682007-04-17 13:26:50 -04001623
1624 BUG_ON(ret);
1625 if (!bookend && search_start >= end) {
1626 ret = 0;
1627 goto out;
1628 }
1629 if (!bookend)
Chris Masona429e512007-04-18 16:15:28 -04001630 continue;
Chris Masonb18c6682007-04-17 13:26:50 -04001631 }
Chris Mason236454d2007-04-19 13:37:44 -04001632 if (bookend && found_extent) {
Chris Masonb18c6682007-04-17 13:26:50 -04001633 /* create bookend */
1634 struct btrfs_key ins;
Chris Masonb18c6682007-04-17 13:26:50 -04001635 ins.objectid = inode->i_ino;
1636 ins.offset = end;
1637 ins.flags = 0;
1638 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1639
1640 btrfs_release_path(root, path);
Chris Masonb18c6682007-04-17 13:26:50 -04001641 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1642 sizeof(*extent));
1643 BUG_ON(ret);
1644 extent = btrfs_item_ptr(
1645 btrfs_buffer_leaf(path->nodes[0]),
1646 path->slots[0],
1647 struct btrfs_file_extent_item);
1648 btrfs_set_file_extent_disk_blocknr(extent,
1649 btrfs_file_extent_disk_blocknr(&old));
1650 btrfs_set_file_extent_disk_num_blocks(extent,
1651 btrfs_file_extent_disk_num_blocks(&old));
1652
1653 btrfs_set_file_extent_offset(extent,
1654 btrfs_file_extent_offset(&old) +
1655 ((end - key.offset) >> inode->i_blkbits));
1656 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1657 (end - key.offset) >> inode->i_blkbits);
1658 btrfs_set_file_extent_num_blocks(extent,
1659 btrfs_file_extent_num_blocks(&old) -
1660 ((end - key.offset) >> inode->i_blkbits));
1661
Chris Mason236454d2007-04-19 13:37:44 -04001662 btrfs_set_file_extent_type(extent,
1663 BTRFS_FILE_EXTENT_REG);
Chris Masonb18c6682007-04-17 13:26:50 -04001664 btrfs_set_file_extent_generation(extent,
1665 btrfs_file_extent_generation(&old));
Chris Masonb18c6682007-04-17 13:26:50 -04001666 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona429e512007-04-18 16:15:28 -04001667 inode->i_blocks +=
1668 btrfs_file_extent_num_blocks(extent) << 3;
Chris Masonb18c6682007-04-17 13:26:50 -04001669 ret = 0;
Chris Mason70b2bef2007-04-17 15:39:32 -04001670 goto out;
Chris Masonb18c6682007-04-17 13:26:50 -04001671 }
Chris Masonb18c6682007-04-17 13:26:50 -04001672 }
Chris Masonb18c6682007-04-17 13:26:50 -04001673out:
Chris Masonb18c6682007-04-17 13:26:50 -04001674 btrfs_free_path(path);
1675 return ret;
1676}
1677
1678static int prepare_pages(struct btrfs_root *root,
Chris Mason75dfe392007-03-29 11:56:46 -04001679 struct file *file,
1680 struct page **pages,
1681 size_t num_pages,
1682 loff_t pos,
Chris Mason2932f3e2007-04-10 14:22:02 -04001683 unsigned long first_index,
1684 unsigned long last_index,
Chris Mason6567e832007-04-16 09:22:45 -04001685 size_t write_bytes,
1686 u64 alloc_extent_start)
Chris Mason75dfe392007-03-29 11:56:46 -04001687{
1688 int i;
1689 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1690 struct inode *inode = file->f_path.dentry->d_inode;
1691 int offset;
1692 int err = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001693 int this_write;
Chris Mason6567e832007-04-16 09:22:45 -04001694 struct buffer_head *bh;
1695 struct buffer_head *head;
Chris Mason75dfe392007-03-29 11:56:46 -04001696 loff_t isize = i_size_read(inode);
1697
1698 memset(pages, 0, num_pages * sizeof(struct page *));
1699
1700 for (i = 0; i < num_pages; i++) {
1701 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1702 if (!pages[i]) {
1703 err = -ENOMEM;
1704 goto failed_release;
1705 }
1706 offset = pos & (PAGE_CACHE_SIZE -1);
1707 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Mason6567e832007-04-16 09:22:45 -04001708 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1709 (1 << BH_Uptodate));
1710 head = page_buffers(pages[i]);
1711 bh = head;
1712 do {
1713 err = btrfs_map_bh_to_logical(root, bh,
1714 alloc_extent_start);
1715 BUG_ON(err);
1716 if (err)
1717 goto failed_truncate;
1718 bh = bh->b_this_page;
Chris Mason236454d2007-04-19 13:37:44 -04001719 if (alloc_extent_start)
1720 alloc_extent_start++;
Chris Mason6567e832007-04-16 09:22:45 -04001721 } while (bh != head);
Chris Mason75dfe392007-03-29 11:56:46 -04001722 pos += this_write;
Chris Mason75dfe392007-03-29 11:56:46 -04001723 WARN_ON(this_write > write_bytes);
1724 write_bytes -= this_write;
1725 }
1726 return 0;
1727
1728failed_release:
1729 btrfs_drop_pages(pages, num_pages);
1730 return err;
1731
1732failed_truncate:
1733 btrfs_drop_pages(pages, num_pages);
1734 if (pos > isize)
1735 vmtruncate(inode, isize);
1736 return err;
1737}
1738
1739static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1740 size_t count, loff_t *ppos)
1741{
1742 loff_t pos;
1743 size_t num_written = 0;
1744 int err = 0;
1745 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001746 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001747 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonb18c6682007-04-17 13:26:50 -04001748 struct page *pages[8];
Chris Masona429e512007-04-18 16:15:28 -04001749 struct page *pinned[2] = { NULL, NULL };
Chris Mason2932f3e2007-04-10 14:22:02 -04001750 unsigned long first_index;
1751 unsigned long last_index;
Chris Mason6567e832007-04-16 09:22:45 -04001752 u64 start_pos;
1753 u64 num_blocks;
1754 u64 alloc_extent_start;
Chris Mason6567e832007-04-16 09:22:45 -04001755 struct btrfs_trans_handle *trans;
Chris Masonb18c6682007-04-17 13:26:50 -04001756 struct btrfs_key ins;
Chris Mason75dfe392007-03-29 11:56:46 -04001757
1758 if (file->f_flags & O_DIRECT)
1759 return -EINVAL;
1760 pos = *ppos;
Chris Mason75dfe392007-03-29 11:56:46 -04001761 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1762 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1763 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1764 if (err)
1765 goto out;
1766 if (count == 0)
1767 goto out;
1768 err = remove_suid(file->f_path.dentry);
1769 if (err)
1770 goto out;
1771 file_update_time(file);
Chris Masona429e512007-04-18 16:15:28 -04001772
1773 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1774 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1775 inode->i_blkbits;
1776
Chris Mason75dfe392007-03-29 11:56:46 -04001777 mutex_lock(&inode->i_mutex);
Chris Mason2932f3e2007-04-10 14:22:02 -04001778 first_index = pos >> PAGE_CACHE_SHIFT;
1779 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
Chris Mason6567e832007-04-16 09:22:45 -04001780
Chris Masona429e512007-04-18 16:15:28 -04001781 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1782 (pos & (PAGE_CACHE_SIZE - 1))) {
1783 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1784 if (!PageUptodate(pinned[0])) {
1785 ret = mpage_readpage(pinned[0], btrfs_get_block);
1786 BUG_ON(ret);
1787 } else {
1788 unlock_page(pinned[0]);
1789 }
1790 }
1791 if (first_index != last_index &&
1792 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1793 (count & (PAGE_CACHE_SIZE - 1))) {
1794 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1795 if (!PageUptodate(pinned[1])) {
1796 ret = mpage_readpage(pinned[1], btrfs_get_block);
1797 BUG_ON(ret);
1798 } else {
1799 unlock_page(pinned[1]);
1800 }
1801 }
1802
Chris Mason6567e832007-04-16 09:22:45 -04001803 mutex_lock(&root->fs_info->fs_mutex);
1804 trans = btrfs_start_transaction(root, 1);
1805 if (!trans) {
1806 err = -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001807 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -04001808 goto out_unlock;
1809 }
Chris Masona429e512007-04-18 16:15:28 -04001810 /* FIXME blocksize != 4096 */
1811 inode->i_blocks += num_blocks << 3;
Chris Masonb18c6682007-04-17 13:26:50 -04001812 if (start_pos < inode->i_size) {
Chris Masona429e512007-04-18 16:15:28 -04001813 /* FIXME blocksize != pagesize */
Chris Masonb18c6682007-04-17 13:26:50 -04001814 ret = drop_extents(trans, root, inode,
1815 start_pos,
1816 (pos + count + root->blocksize -1) &
Chris Masona429e512007-04-18 16:15:28 -04001817 ~((u64)root->blocksize - 1));
Chris Mason236454d2007-04-19 13:37:44 -04001818 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001819 }
Chris Mason236454d2007-04-19 13:37:44 -04001820 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
1821 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
1822 ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
Chris Masonb18c6682007-04-17 13:26:50 -04001823 (u64)-1, &ins);
Chris Mason236454d2007-04-19 13:37:44 -04001824 BUG_ON(ret);
1825 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
Chris Masonb18c6682007-04-17 13:26:50 -04001826 start_pos, ins.objectid, ins.offset);
Chris Mason236454d2007-04-19 13:37:44 -04001827 BUG_ON(ret);
1828 } else {
1829 ins.offset = 0;
1830 ins.objectid = 0;
1831 }
Chris Masonb18c6682007-04-17 13:26:50 -04001832 BUG_ON(ret);
1833 alloc_extent_start = ins.objectid;
1834 ret = btrfs_end_transaction(trans, root);
Chris Mason6567e832007-04-16 09:22:45 -04001835 mutex_unlock(&root->fs_info->fs_mutex);
1836
Chris Mason75dfe392007-03-29 11:56:46 -04001837 while(count > 0) {
1838 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1839 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1840 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1841 PAGE_CACHE_SHIFT;
Chris Masonb18c6682007-04-17 13:26:50 -04001842
1843 memset(pages, 0, sizeof(pages));
1844 ret = prepare_pages(root, file, pages, num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04001845 pos, first_index, last_index,
1846 write_bytes, alloc_extent_start);
Chris Mason75dfe392007-03-29 11:56:46 -04001847 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001848
Chris Mason6567e832007-04-16 09:22:45 -04001849 /* FIXME blocks != pagesize */
Chris Mason236454d2007-04-19 13:37:44 -04001850 if (alloc_extent_start)
1851 alloc_extent_start += num_pages;
Chris Mason75dfe392007-03-29 11:56:46 -04001852 ret = btrfs_copy_from_user(pos, num_pages,
1853 write_bytes, pages, buf);
1854 BUG_ON(ret);
1855
Chris Masonf254e522007-03-29 15:15:27 -04001856 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason70b2bef2007-04-17 15:39:32 -04001857 num_pages, pos, write_bytes);
Chris Mason75dfe392007-03-29 11:56:46 -04001858 BUG_ON(ret);
1859 btrfs_drop_pages(pages, num_pages);
1860
Chris Mason75dfe392007-03-29 11:56:46 -04001861 buf += write_bytes;
1862 count -= write_bytes;
1863 pos += write_bytes;
1864 num_written += write_bytes;
1865
1866 balance_dirty_pages_ratelimited(inode->i_mapping);
1867 cond_resched();
1868 }
Chris Mason6567e832007-04-16 09:22:45 -04001869out_unlock:
Chris Mason75dfe392007-03-29 11:56:46 -04001870 mutex_unlock(&inode->i_mutex);
1871out:
Chris Masona429e512007-04-18 16:15:28 -04001872 if (pinned[0])
1873 page_cache_release(pinned[0]);
1874 if (pinned[1])
1875 page_cache_release(pinned[1]);
Chris Mason75dfe392007-03-29 11:56:46 -04001876 *ppos = pos;
1877 current->backing_dev_info = NULL;
Chris Masona429e512007-04-18 16:15:28 -04001878 mark_inode_dirty(inode);
Chris Mason75dfe392007-03-29 11:56:46 -04001879 return num_written ? num_written : err;
1880}
1881
Chris Masonf254e522007-03-29 15:15:27 -04001882static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1883 unsigned long offset, unsigned long size)
1884{
1885 char *kaddr;
1886 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001887 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001888
1889 if (size > count)
1890 size = count;
1891
1892 if (!PageChecked(page)) {
1893 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04001894 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason236454d2007-04-19 13:37:44 -04001895
Chris Masonf254e522007-03-29 15:15:27 -04001896 int ret = btrfs_csum_verify_file_block(root,
Chris Mason236454d2007-04-19 13:37:44 -04001897 page->mapping->host->i_ino,
1898 page->index << PAGE_CACHE_SHIFT,
1899 kmap(page), PAGE_CACHE_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -04001900 if (ret) {
1901 printk("failed to verify ino %lu page %lu\n",
1902 page->mapping->host->i_ino,
1903 page->index);
1904 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1905 }
1906 SetPageChecked(page);
1907 kunmap(page);
1908 }
1909 /*
1910 * Faults on the destination of a read are common, so do it before
1911 * taking the kmap.
1912 */
1913 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1914 kaddr = kmap_atomic(page, KM_USER0);
1915 left = __copy_to_user_inatomic(desc->arg.buf,
1916 kaddr + offset, size);
1917 kunmap_atomic(kaddr, KM_USER0);
1918 if (left == 0)
1919 goto success;
1920 }
1921
1922 /* Do it the slow way */
1923 kaddr = kmap(page);
1924 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1925 kunmap(page);
1926
1927 if (left) {
1928 size -= left;
1929 desc->error = -EFAULT;
1930 }
1931success:
1932 desc->count = count - size;
1933 desc->written += size;
1934 desc->arg.buf += size;
1935 return size;
1936}
1937
1938/**
1939 * btrfs_file_aio_read - filesystem read routine
1940 * @iocb: kernel I/O control block
1941 * @iov: io vector request
1942 * @nr_segs: number of segments in the iovec
1943 * @pos: current file position
1944 */
1945static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1946 unsigned long nr_segs, loff_t pos)
1947{
1948 struct file *filp = iocb->ki_filp;
1949 ssize_t retval;
1950 unsigned long seg;
1951 size_t count;
1952 loff_t *ppos = &iocb->ki_pos;
1953
1954 count = 0;
1955 for (seg = 0; seg < nr_segs; seg++) {
1956 const struct iovec *iv = &iov[seg];
1957
1958 /*
1959 * If any segment has a negative length, or the cumulative
1960 * length ever wraps negative then return -EINVAL.
1961 */
1962 count += iv->iov_len;
1963 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1964 return -EINVAL;
1965 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1966 continue;
1967 if (seg == 0)
1968 return -EFAULT;
1969 nr_segs = seg;
1970 count -= iv->iov_len; /* This segment is no good */
1971 break;
1972 }
1973 retval = 0;
1974 if (count) {
1975 for (seg = 0; seg < nr_segs; seg++) {
1976 read_descriptor_t desc;
1977
1978 desc.written = 0;
1979 desc.arg.buf = iov[seg].iov_base;
1980 desc.count = iov[seg].iov_len;
1981 if (desc.count == 0)
1982 continue;
1983 desc.error = 0;
1984 do_generic_file_read(filp, ppos, &desc,
1985 btrfs_read_actor);
1986 retval += desc.written;
1987 if (desc.error) {
1988 retval = retval ?: desc.error;
1989 break;
1990 }
1991 }
1992 }
1993 return retval;
1994}
1995
Chris Mason2619ba12007-04-10 16:58:11 -04001996static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1997{
1998 struct btrfs_trans_handle *trans;
1999 struct btrfs_key key;
2000 struct btrfs_root_item root_item;
2001 struct btrfs_inode_item *inode_item;
2002 struct buffer_head *subvol;
2003 struct btrfs_leaf *leaf;
2004 struct btrfs_root *new_root;
2005 struct inode *inode;
2006 int ret;
2007 u64 objectid;
2008 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2009
2010 mutex_lock(&root->fs_info->fs_mutex);
2011 trans = btrfs_start_transaction(root, 1);
2012 BUG_ON(!trans);
2013
2014 subvol = btrfs_alloc_free_block(trans, root);
2015 leaf = btrfs_buffer_leaf(subvol);
2016 btrfs_set_header_nritems(&leaf->header, 0);
2017 btrfs_set_header_level(&leaf->header, 0);
Chris Mason7eccb902007-04-11 15:53:25 -04002018 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002019 btrfs_set_header_generation(&leaf->header, trans->transid);
2020 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2021 sizeof(leaf->header.fsid));
2022
2023 inode_item = &root_item.inode;
2024 memset(inode_item, 0, sizeof(*inode_item));
2025 btrfs_set_inode_generation(inode_item, 1);
2026 btrfs_set_inode_size(inode_item, 3);
2027 btrfs_set_inode_nlink(inode_item, 1);
2028 btrfs_set_inode_nblocks(inode_item, 1);
2029 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2030
Chris Mason7eccb902007-04-11 15:53:25 -04002031 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002032 btrfs_set_root_refs(&root_item, 1);
2033
2034 mark_buffer_dirty(subvol);
2035 brelse(subvol);
2036 subvol = NULL;
2037
2038 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2039 0, &objectid);
2040 BUG_ON(ret);
2041
2042 btrfs_set_root_dirid(&root_item, new_dirid);
2043
2044 key.objectid = objectid;
2045 key.offset = 1;
2046 key.flags = 0;
2047 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2048 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2049 &root_item);
2050 BUG_ON(ret);
2051
2052 /*
2053 * insert the directory item
2054 */
2055 key.offset = (u64)-1;
2056 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2057 name, namelen,
2058 root->fs_info->sb->s_root->d_inode->i_ino,
2059 &key, 0);
2060 BUG_ON(ret);
2061
2062 ret = btrfs_commit_transaction(trans, root);
2063 BUG_ON(ret);
2064
2065 new_root = btrfs_read_fs_root(root->fs_info, &key);
2066 BUG_ON(!new_root);
2067
2068 trans = btrfs_start_transaction(new_root, 1);
2069 BUG_ON(!trans);
2070
2071 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
2072 inode->i_op = &btrfs_dir_inode_operations;
2073 inode->i_fop = &btrfs_dir_file_operations;
2074
2075 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2076 BUG_ON(ret);
2077
2078 inode->i_nlink = 1;
2079 inode->i_size = 6;
2080 ret = btrfs_update_inode(trans, new_root, inode);
2081 BUG_ON(ret);
2082
2083 ret = btrfs_commit_transaction(trans, new_root);
2084 BUG_ON(ret);
2085
2086 iput(inode);
2087
2088 mutex_unlock(&root->fs_info->fs_mutex);
2089 return 0;
2090}
2091
Chris Masonc5739bb2007-04-10 09:27:04 -04002092static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2093{
2094 struct btrfs_trans_handle *trans;
2095 struct btrfs_key key;
2096 struct btrfs_root_item new_root_item;
2097 int ret;
2098 u64 objectid;
2099
Chris Mason2619ba12007-04-10 16:58:11 -04002100 if (!root->ref_cows)
2101 return -EINVAL;
2102
Chris Masonc5739bb2007-04-10 09:27:04 -04002103 mutex_lock(&root->fs_info->fs_mutex);
2104 trans = btrfs_start_transaction(root, 1);
2105 BUG_ON(!trans);
2106
2107 ret = btrfs_update_inode(trans, root, root->inode);
2108 BUG_ON(ret);
2109
Chris Mason1b05da22007-04-10 12:13:09 -04002110 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2111 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04002112 BUG_ON(ret);
2113
Chris Masonc5739bb2007-04-10 09:27:04 -04002114 memcpy(&new_root_item, &root->root_item,
2115 sizeof(new_root_item));
2116
2117 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002118 key.offset = 1;
2119 key.flags = 0;
2120 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason7eccb902007-04-11 15:53:25 -04002121 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
Chris Masonc5739bb2007-04-10 09:27:04 -04002122
2123 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2124 &new_root_item);
2125 BUG_ON(ret);
2126
Chris Masonc5739bb2007-04-10 09:27:04 -04002127 /*
2128 * insert the directory item
2129 */
2130 key.offset = (u64)-1;
2131 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2132 name, namelen,
2133 root->fs_info->sb->s_root->d_inode->i_ino,
2134 &key, 0);
2135
2136 BUG_ON(ret);
2137
2138 ret = btrfs_inc_root_ref(trans, root);
2139 BUG_ON(ret);
2140
2141 ret = btrfs_commit_transaction(trans, root);
2142 BUG_ON(ret);
2143 mutex_unlock(&root->fs_info->fs_mutex);
2144 return 0;
2145}
2146
Chris Mason8352d8a2007-04-12 10:43:05 -04002147static int add_disk(struct btrfs_root *root, char *name, int namelen)
2148{
2149 struct block_device *bdev;
2150 struct btrfs_path *path;
2151 struct super_block *sb = root->fs_info->sb;
2152 struct btrfs_root *dev_root = root->fs_info->dev_root;
2153 struct btrfs_trans_handle *trans;
2154 struct btrfs_device_item *dev_item;
2155 struct btrfs_key key;
2156 u16 item_size;
2157 u64 num_blocks;
2158 u64 new_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -04002159 u64 device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -04002160 int ret;
Chris Masonb4100d62007-04-12 12:14:00 -04002161
Chris Mason8352d8a2007-04-12 10:43:05 -04002162printk("adding disk %s\n", name);
2163 path = btrfs_alloc_path();
2164 if (!path)
2165 return -ENOMEM;
2166 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2167 bdev = open_bdev_excl(name, O_RDWR, sb);
2168 if (IS_ERR(bdev)) {
2169 ret = PTR_ERR(bdev);
2170printk("open bdev excl failed ret %d\n", ret);
2171 goto out_nolock;
2172 }
2173 set_blocksize(bdev, sb->s_blocksize);
2174 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2175 key.objectid = num_blocks;
2176 key.offset = new_blocks;
2177 key.flags = 0;
2178 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2179
2180 mutex_lock(&dev_root->fs_info->fs_mutex);
2181 trans = btrfs_start_transaction(dev_root, 1);
2182 item_size = sizeof(*dev_item) + namelen;
2183printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2184 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2185 if (ret) {
2186printk("insert failed %d\n", ret);
2187 close_bdev_excl(bdev);
2188 if (ret > 0)
2189 ret = -EEXIST;
2190 goto out;
2191 }
2192 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2193 path->slots[0], struct btrfs_device_item);
2194 btrfs_set_device_pathlen(dev_item, namelen);
2195 memcpy(dev_item + 1, name, namelen);
Chris Masonb4100d62007-04-12 12:14:00 -04002196
2197 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2198 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2199 btrfs_set_device_id(dev_item, device_id);
Chris Mason8352d8a2007-04-12 10:43:05 -04002200 mark_buffer_dirty(path->nodes[0]);
2201
Chris Masonb4100d62007-04-12 12:14:00 -04002202 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2203 new_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -04002204
2205 if (!ret) {
2206 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2207 num_blocks + new_blocks);
2208 i_size_write(root->fs_info->btree_inode,
2209 (num_blocks + new_blocks) <<
2210 root->fs_info->btree_inode->i_blkbits);
2211 }
2212
2213out:
2214 ret = btrfs_commit_transaction(trans, dev_root);
2215 BUG_ON(ret);
2216 mutex_unlock(&root->fs_info->fs_mutex);
2217out_nolock:
2218 btrfs_free_path(path);
2219
2220 return ret;
2221}
2222
Chris Masonc5739bb2007-04-10 09:27:04 -04002223static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2224 cmd, unsigned long arg)
2225{
2226 struct btrfs_root *root = BTRFS_I(inode)->root;
2227 struct btrfs_ioctl_vol_args vol_args;
Chris Mason8352d8a2007-04-12 10:43:05 -04002228 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -04002229 struct btrfs_dir_item *di;
Chris Masonc5739bb2007-04-10 09:27:04 -04002230 int namelen;
Chris Mason2619ba12007-04-10 16:58:11 -04002231 struct btrfs_path *path;
2232 u64 root_dirid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002233
Chris Masonc5739bb2007-04-10 09:27:04 -04002234 switch (cmd) {
2235 case BTRFS_IOC_SNAP_CREATE:
2236 if (copy_from_user(&vol_args,
2237 (struct btrfs_ioctl_vol_args __user *)arg,
2238 sizeof(vol_args)))
2239 return -EFAULT;
2240 namelen = strlen(vol_args.name);
2241 if (namelen > BTRFS_VOL_NAME_MAX)
2242 return -EINVAL;
Chris Mason2619ba12007-04-10 16:58:11 -04002243 path = btrfs_alloc_path();
2244 if (!path)
2245 return -ENOMEM;
Chris Mason2d13d8d2007-04-10 20:07:20 -04002246 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Chris Mason2619ba12007-04-10 16:58:11 -04002247 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason7e381802007-04-19 15:36:27 -04002248 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
Chris Mason2619ba12007-04-10 16:58:11 -04002249 path, root_dirid,
2250 vol_args.name, namelen, 0);
2251 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2d13d8d2007-04-10 20:07:20 -04002252 btrfs_free_path(path);
Chris Mason7e381802007-04-19 15:36:27 -04002253 if (di && !IS_ERR(di))
Chris Mason2619ba12007-04-10 16:58:11 -04002254 return -EEXIST;
2255
2256 if (root == root->fs_info->tree_root)
2257 ret = create_subvol(root, vol_args.name, namelen);
2258 else
2259 ret = create_snapshot(root, vol_args.name, namelen);
Chris Masonc5739bb2007-04-10 09:27:04 -04002260 WARN_ON(ret);
2261 break;
Chris Mason8352d8a2007-04-12 10:43:05 -04002262 case BTRFS_IOC_ADD_DISK:
2263 if (copy_from_user(&vol_args,
2264 (struct btrfs_ioctl_vol_args __user *)arg,
2265 sizeof(vol_args)))
2266 return -EFAULT;
2267 namelen = strlen(vol_args.name);
2268 if (namelen > BTRFS_VOL_NAME_MAX)
2269 return -EINVAL;
2270 vol_args.name[namelen] = '\0';
2271 ret = add_disk(root, vol_args.name, namelen);
2272 break;
Chris Masonc5739bb2007-04-10 09:27:04 -04002273 default:
2274 return -ENOTTY;
2275 }
Chris Mason8352d8a2007-04-12 10:43:05 -04002276 return ret;
Chris Masonc5739bb2007-04-10 09:27:04 -04002277}
2278
Chris Mason2c90e5d2007-04-02 10:50:19 -04002279static struct kmem_cache *btrfs_inode_cachep;
2280struct kmem_cache *btrfs_trans_handle_cachep;
2281struct kmem_cache *btrfs_transaction_cachep;
2282struct kmem_cache *btrfs_bit_radix_cachep;
2283struct kmem_cache *btrfs_path_cachep;
2284
2285/*
2286 * Called inside transaction, so use GFP_NOFS
2287 */
2288static struct inode *btrfs_alloc_inode(struct super_block *sb)
2289{
2290 struct btrfs_inode *ei;
2291
2292 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2293 if (!ei)
2294 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002295 return &ei->vfs_inode;
2296}
2297
2298static void btrfs_destroy_inode(struct inode *inode)
2299{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002300 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04002301 WARN_ON(inode->i_data.nrpages);
2302
Chris Mason2c90e5d2007-04-02 10:50:19 -04002303 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2304}
2305
2306static void init_once(void * foo, struct kmem_cache * cachep,
2307 unsigned long flags)
2308{
2309 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2310
2311 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2312 SLAB_CTOR_CONSTRUCTOR) {
2313 inode_init_once(&ei->vfs_inode);
2314 }
2315}
2316
2317static int init_inodecache(void)
2318{
2319 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2320 sizeof(struct btrfs_inode),
2321 0, (SLAB_RECLAIM_ACCOUNT|
2322 SLAB_MEM_SPREAD),
2323 init_once, NULL);
2324 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2325 sizeof(struct btrfs_trans_handle),
2326 0, (SLAB_RECLAIM_ACCOUNT|
2327 SLAB_MEM_SPREAD),
2328 NULL, NULL);
2329 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2330 sizeof(struct btrfs_transaction),
2331 0, (SLAB_RECLAIM_ACCOUNT|
2332 SLAB_MEM_SPREAD),
2333 NULL, NULL);
2334 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2335 sizeof(struct btrfs_transaction),
2336 0, (SLAB_RECLAIM_ACCOUNT|
2337 SLAB_MEM_SPREAD),
2338 NULL, NULL);
2339 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2340 256,
2341 0, (SLAB_RECLAIM_ACCOUNT|
2342 SLAB_MEM_SPREAD |
2343 SLAB_DESTROY_BY_RCU),
2344 NULL, NULL);
2345 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2346 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2347 return -ENOMEM;
2348 return 0;
2349}
2350
2351static void destroy_inodecache(void)
2352{
2353 kmem_cache_destroy(btrfs_inode_cachep);
2354 kmem_cache_destroy(btrfs_trans_handle_cachep);
2355 kmem_cache_destroy(btrfs_transaction_cachep);
2356 kmem_cache_destroy(btrfs_bit_radix_cachep);
2357 kmem_cache_destroy(btrfs_path_cachep);
2358}
2359
Chris Mason2e635a22007-03-21 11:12:56 -04002360static int btrfs_get_sb(struct file_system_type *fs_type,
2361 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2362{
2363 return get_sb_bdev(fs_type, flags, dev_name, data,
2364 btrfs_fill_super, mnt);
2365}
2366
Chris Mason236454d2007-04-19 13:37:44 -04002367
2368static int btrfs_getattr(struct vfsmount *mnt,
2369 struct dentry *dentry, struct kstat *stat)
2370{
2371 struct inode *inode = dentry->d_inode;
2372 generic_fillattr(inode, stat);
2373 stat->blksize = 256 * 1024;
2374 return 0;
2375}
2376
Chris Mason8fd17792007-04-19 21:01:03 -04002377static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2378{
2379 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2380 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2381
2382 buf->f_namelen = BTRFS_NAME_LEN;
2383 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2384 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2385 buf->f_bavail = buf->f_bfree;
2386 buf->f_bsize = dentry->d_sb->s_blocksize;
2387 buf->f_type = BTRFS_SUPER_MAGIC;
2388 return 0;
2389}
Chris Mason2e635a22007-03-21 11:12:56 -04002390static struct file_system_type btrfs_fs_type = {
2391 .owner = THIS_MODULE,
2392 .name = "btrfs",
2393 .get_sb = btrfs_get_sb,
2394 .kill_sb = kill_block_super,
2395 .fs_flags = FS_REQUIRES_DEV,
2396};
2397
Chris Masone20d96d2007-03-22 12:13:20 -04002398static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -04002399 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002400 .put_super = btrfs_put_super,
2401 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04002402 .write_super = btrfs_write_super,
2403 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04002404 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002405 .alloc_inode = btrfs_alloc_inode,
2406 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -04002407 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -04002408};
2409
2410static struct inode_operations btrfs_dir_inode_operations = {
2411 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04002412 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04002413 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04002414 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04002415 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04002416};
2417
Chris Masond6e4a422007-04-06 15:37:36 -04002418static struct inode_operations btrfs_dir_ro_inode_operations = {
2419 .lookup = btrfs_lookup,
2420};
2421
Chris Masone20d96d2007-03-22 12:13:20 -04002422static struct file_operations btrfs_dir_file_operations = {
2423 .llseek = generic_file_llseek,
2424 .read = generic_read_dir,
2425 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04002426 .ioctl = btrfs_ioctl,
Chris Masone20d96d2007-03-22 12:13:20 -04002427};
2428
Chris Masondee26a92007-03-26 16:00:06 -04002429static struct address_space_operations btrfs_aops = {
2430 .readpage = btrfs_readpage,
Chris Masondee26a92007-03-26 16:00:06 -04002431 .writepage = btrfs_writepage,
2432 .sync_page = block_sync_page,
2433 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04002434 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04002435};
2436
2437static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04002438 .truncate = btrfs_truncate,
Chris Mason236454d2007-04-19 13:37:44 -04002439 .getattr = btrfs_getattr,
Chris Masondee26a92007-03-26 16:00:06 -04002440};
2441
2442static struct file_operations btrfs_file_operations = {
2443 .llseek = generic_file_llseek,
2444 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04002445 .aio_read = btrfs_file_aio_read,
2446 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04002447 .mmap = generic_file_mmap,
2448 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04002449 .ioctl = btrfs_ioctl,
Chris Mason8fd17792007-04-19 21:01:03 -04002450 .fsync = btrfs_sync_file,
Chris Masondee26a92007-03-26 16:00:06 -04002451};
Chris Masone20d96d2007-03-22 12:13:20 -04002452
Chris Mason2e635a22007-03-21 11:12:56 -04002453static int __init init_btrfs_fs(void)
2454{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002455 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04002456 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04002457 err = init_inodecache();
2458 if (err)
2459 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04002460 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2461 err = subsystem_register(&btrfs_subsys);
2462 if (err)
2463 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04002464 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002465out:
2466 destroy_inodecache();
2467 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002468}
2469
2470static void __exit exit_btrfs_fs(void)
2471{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002472 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04002473 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002474 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04002475 printk("btrfs unloaded\n");
2476}
2477
2478module_init(init_btrfs_fs)
2479module_exit(exit_btrfs_fs)
2480
2481MODULE_LICENSE("GPL");