blob: 5bbccbc7e3ccd5caf5e013529e874a8c222d5f21 [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 Masonf68cad02007-04-24 12:44:26 -0400123static void fill_inode_item(struct btrfs_inode_item *item,
124 struct inode *inode)
125{
126 btrfs_set_inode_uid(item, inode->i_uid);
127 btrfs_set_inode_gid(item, inode->i_gid);
128 btrfs_set_inode_size(item, inode->i_size);
129 btrfs_set_inode_mode(item, inode->i_mode);
130 btrfs_set_inode_nlink(item, inode->i_nlink);
131 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
132 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
133 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
134 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
135 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
136 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
137 btrfs_set_inode_nblocks(item, inode->i_blocks);
138 btrfs_set_inode_generation(item, inode->i_generation);
139}
140
141
142static int btrfs_update_inode(struct btrfs_trans_handle *trans,
143 struct btrfs_root *root,
144 struct inode *inode)
145{
146 struct btrfs_inode_item *inode_item;
147 struct btrfs_path *path;
148 int ret;
149
150 path = btrfs_alloc_path();
151 BUG_ON(!path);
152 btrfs_init_path(path);
153 ret = btrfs_lookup_inode(trans, root, path,
154 &BTRFS_I(inode)->location, 1);
155 if (ret) {
156 if (ret > 0)
157 ret = -ENOENT;
158 goto failed;
159 }
160
161 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
162 path->slots[0],
163 struct btrfs_inode_item);
164
165 fill_inode_item(inode_item, inode);
166 btrfs_mark_buffer_dirty(path->nodes[0]);
167 ret = 0;
168failed:
169 btrfs_release_path(root, path);
170 btrfs_free_path(path);
171 return ret;
172}
173
174
Chris Mason5f443fd2007-03-27 13:42:32 -0400175static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
176 struct btrfs_root *root,
177 struct inode *dir,
178 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400179{
Chris Mason5caf2a02007-04-02 11:20:42 -0400180 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400181 const char *name = dentry->d_name.name;
182 int name_len = dentry->d_name.len;
Chris Mason7e381802007-04-19 15:36:27 -0400183 int ret = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400184 u64 objectid;
185 struct btrfs_dir_item *di;
186
Chris Mason5caf2a02007-04-02 11:20:42 -0400187 path = btrfs_alloc_path();
188 BUG_ON(!path);
189 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400190 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400191 name, name_len, -1);
Chris Mason7e381802007-04-19 15:36:27 -0400192 if (IS_ERR(di)) {
193 ret = PTR_ERR(di);
Chris Mason134e9732007-03-25 13:44:56 -0400194 goto err;
Chris Mason7e381802007-04-19 15:36:27 -0400195 }
196 if (!di) {
Chris Mason134e9732007-03-25 13:44:56 -0400197 ret = -ENOENT;
198 goto err;
199 }
Chris Masond6e4a422007-04-06 15:37:36 -0400200 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason7e381802007-04-19 15:36:27 -0400201 ret = btrfs_delete_one_dir_name(trans, root, path, di);
202 BUG_ON(ret);
203 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400204
Chris Mason7e381802007-04-19 15:36:27 -0400205 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
206 objectid, name, name_len, -1);
207 if (IS_ERR(di)) {
208 ret = PTR_ERR(di);
209 goto err;
210 }
211 if (!di) {
212 ret = -ENOENT;
213 goto err;
214 }
215 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason134e9732007-03-25 13:44:56 -0400216 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400217
Chris Mason134e9732007-03-25 13:44:56 -0400218 dentry->d_inode->i_ctime = dir->i_ctime;
219err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400220 btrfs_free_path(path);
Chris Masonf68cad02007-04-24 12:44:26 -0400221 if (!ret) {
Chris Mason5f26f772007-04-05 10:38:44 -0400222 dir->i_size -= name_len * 2;
Chris Masonf68cad02007-04-24 12:44:26 -0400223 btrfs_update_inode(trans, root, dir);
224 drop_nlink(dentry->d_inode);
225 btrfs_update_inode(trans, root, dentry->d_inode);
Chris Masond4dbff92007-04-04 14:08:15 -0400226 }
Chris Mason134e9732007-03-25 13:44:56 -0400227 return ret;
228}
229
Chris Mason5f443fd2007-03-27 13:42:32 -0400230static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
231{
232 struct btrfs_root *root;
233 struct btrfs_trans_handle *trans;
234 int ret;
235
Chris Masond6e4a422007-04-06 15:37:36 -0400236 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400237 mutex_lock(&root->fs_info->fs_mutex);
238 trans = btrfs_start_transaction(root, 1);
239 ret = btrfs_unlink_trans(trans, root, dir, dentry);
240 btrfs_end_transaction(trans, root);
241 mutex_unlock(&root->fs_info->fs_mutex);
242 return ret;
243}
244
245static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
246{
247 struct inode *inode = dentry->d_inode;
248 int err;
249 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400250 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400251 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400252 struct btrfs_key key;
253 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400254 struct btrfs_key found_key;
255 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400256 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400257 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400258
Chris Mason5caf2a02007-04-02 11:20:42 -0400259 path = btrfs_alloc_path();
260 BUG_ON(!path);
261 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400262 mutex_lock(&root->fs_info->fs_mutex);
263 trans = btrfs_start_transaction(root, 1);
264 key.objectid = inode->i_ino;
265 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400266 key.flags = (u32)-1;
267 while(1) {
268 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
269 if (ret < 0) {
270 err = ret;
271 goto out;
272 }
273 BUG_ON(ret == 0);
274 if (path->slots[0] == 0) {
275 err = -ENOENT;
276 goto out;
277 }
278 path->slots[0]--;
279 leaf = btrfs_buffer_leaf(path->nodes[0]);
280 btrfs_disk_key_to_cpu(&found_key,
281 &leaf->items[path->slots[0]].key);
282 found_type = btrfs_key_type(&found_key);
283 if (found_key.objectid != inode->i_ino) {
284 err = -ENOENT;
285 goto out;
286 }
287 if ((found_type != BTRFS_DIR_ITEM_KEY &&
288 found_type != BTRFS_DIR_INDEX_KEY) ||
289 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
290 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
291 err = -ENOTEMPTY;
292 goto out;
293 }
294 ret = btrfs_del_item(trans, root, path);
295 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400296
Chris Mason5f26f772007-04-05 10:38:44 -0400297 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
298 break;
299 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400300 }
Chris Mason5f26f772007-04-05 10:38:44 -0400301 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400302 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400303
304 /* now the directory is empty */
305 err = btrfs_unlink_trans(trans, root, dir, dentry);
306 if (!err) {
307 inode->i_size = 0;
308 }
309out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400310 btrfs_release_path(root, path);
311 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400312 mutex_unlock(&root->fs_info->fs_mutex);
313 ret = btrfs_end_transaction(trans, root);
314 if (ret && !err)
315 err = ret;
316 return err;
317}
318
Chris Mason134e9732007-03-25 13:44:56 -0400319static int btrfs_free_inode(struct btrfs_trans_handle *trans,
320 struct btrfs_root *root,
321 struct inode *inode)
322{
Chris Mason5caf2a02007-04-02 11:20:42 -0400323 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400324 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400325
Chris Mason134e9732007-03-25 13:44:56 -0400326 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400327
328 path = btrfs_alloc_path();
329 BUG_ON(!path);
330 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400331 ret = btrfs_lookup_inode(trans, root, path,
332 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400333 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400334 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400335 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400336 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400337 return ret;
338}
339
Chris Masonf4b9aa82007-03-27 11:05:53 -0400340static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
341 struct btrfs_root *root,
342 struct inode *inode)
343{
344 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400345 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400346 struct btrfs_key key;
347 struct btrfs_disk_key *found_key;
348 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400349 struct btrfs_file_extent_item *fi = NULL;
350 u64 extent_start = 0;
351 u64 extent_num_blocks = 0;
352 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400353
Chris Mason5caf2a02007-04-02 11:20:42 -0400354 path = btrfs_alloc_path();
355 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400356 /* FIXME, add redo link to tree so we don't leak on crash */
357 key.objectid = inode->i_ino;
358 key.offset = (u64)-1;
359 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400360 /*
361 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
362 * or extent data
363 */
Chris Masonf254e522007-03-29 15:15:27 -0400364 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400365 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400366 btrfs_init_path(path);
367 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400368 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400369 goto error;
370 }
371 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400372 BUG_ON(path->slots[0] == 0);
373 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400374 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 leaf = btrfs_buffer_leaf(path->nodes[0]);
376 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400377 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
378 break;
Chris Masonf254e522007-03-29 15:15:27 -0400379 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400380 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400381 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400382 break;
383 if (btrfs_disk_key_offset(found_key) < inode->i_size)
384 break;
Chris Mason236454d2007-04-19 13:37:44 -0400385 found_extent = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400386 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
388 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400389 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400390 if (btrfs_file_extent_type(fi) !=
391 BTRFS_FILE_EXTENT_INLINE) {
392 extent_start =
393 btrfs_file_extent_disk_blocknr(fi);
394 extent_num_blocks =
395 btrfs_file_extent_disk_num_blocks(fi);
396 /* FIXME blocksize != 4096 */
397 inode->i_blocks -=
398 btrfs_file_extent_num_blocks(fi) << 3;
399 found_extent = 1;
400 }
Chris Masonf254e522007-03-29 15:15:27 -0400401 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400402 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400403 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400404 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400405 if (found_extent) {
406 ret = btrfs_free_extent(trans, root, extent_start,
407 extent_num_blocks, 0);
408 BUG_ON(ret);
409 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400410 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400411 ret = 0;
412error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400413 btrfs_release_path(root, path);
414 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400415 return ret;
416}
417
Chris Mason134e9732007-03-25 13:44:56 -0400418static void btrfs_delete_inode(struct inode *inode)
419{
420 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400421 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400422 int ret;
423
Chris Mason134e9732007-03-25 13:44:56 -0400424 truncate_inode_pages(&inode->i_data, 0);
425 if (is_bad_inode(inode)) {
426 goto no_delete;
427 }
428 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400429 mutex_lock(&root->fs_info->fs_mutex);
430 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400431 if (S_ISREG(inode->i_mode)) {
432 ret = btrfs_truncate_in_trans(trans, root, inode);
433 BUG_ON(ret);
434 }
Chris Mason134e9732007-03-25 13:44:56 -0400435 btrfs_free_inode(trans, root, inode);
436 btrfs_end_transaction(trans, root);
437 mutex_unlock(&root->fs_info->fs_mutex);
438 return;
439no_delete:
440 clear_inode(inode);
441}
442
Chris Masone20d96d2007-03-22 12:13:20 -0400443static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400444 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400445{
446 const char *name = dentry->d_name.name;
447 int namelen = dentry->d_name.len;
448 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400449 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400450 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400451 int ret;
452
Chris Mason5caf2a02007-04-02 11:20:42 -0400453 path = btrfs_alloc_path();
454 BUG_ON(!path);
455 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400456 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400457 namelen, 0);
Chris Mason7e381802007-04-19 15:36:27 -0400458 if (!di || IS_ERR(di)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400459 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400460 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400461 goto out;
462 }
Chris Masond6e4a422007-04-06 15:37:36 -0400463 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400464out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 btrfs_release_path(root, path);
466 btrfs_free_path(path);
Chris Masone20d96d2007-03-22 12:13:20 -0400467 return ret;
468}
469
Chris Masond6e4a422007-04-06 15:37:36 -0400470int fixup_tree_root_location(struct btrfs_root *root,
471 struct btrfs_key *location,
472 struct btrfs_root **sub_root)
473{
474 struct btrfs_path *path;
475 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400476
477 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
478 return 0;
479 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
480 return 0;
481
482 path = btrfs_alloc_path();
483 BUG_ON(!path);
484 mutex_lock(&root->fs_info->fs_mutex);
485
Chris Mason0f7d52f2007-04-09 10:42:37 -0400486 *sub_root = btrfs_read_fs_root(root->fs_info, location);
487 if (IS_ERR(*sub_root))
488 return PTR_ERR(*sub_root);
489
490 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400491 location->objectid = btrfs_root_dirid(ri);
492 location->flags = 0;
493 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
494 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400495
Chris Masond6e4a422007-04-06 15:37:36 -0400496 btrfs_free_path(path);
497 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400498 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400499}
500
Chris Masonc5739bb2007-04-10 09:27:04 -0400501int btrfs_init_locked_inode(struct inode *inode, void *p)
502{
503 struct btrfs_iget_args *args = p;
504 inode->i_ino = args->ino;
505 BTRFS_I(inode)->root = args->root;
506 return 0;
507}
508
509int btrfs_find_actor(struct inode *inode, void *opaque)
510{
511 struct btrfs_iget_args *args = opaque;
512 return (args->ino == inode->i_ino &&
513 args->root == BTRFS_I(inode)->root);
514}
515
516struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
517 struct btrfs_root *root)
518{
519 struct inode *inode;
520 struct btrfs_iget_args args;
521 args.ino = objectid;
522 args.root = root;
523
524 inode = iget5_locked(s, objectid, btrfs_find_actor,
525 btrfs_init_locked_inode,
526 (void *)&args);
527 return inode;
528}
Chris Masond6e4a422007-04-06 15:37:36 -0400529
Chris Masone20d96d2007-03-22 12:13:20 -0400530static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
531 struct nameidata *nd)
532{
533 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400534 struct btrfs_inode *bi = BTRFS_I(dir);
535 struct btrfs_root *root = bi->root;
536 struct btrfs_root *sub_root = root;
537 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400538 int ret;
539
540 if (dentry->d_name.len > BTRFS_NAME_LEN)
541 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400542 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400543 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400544 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400545 if (ret < 0)
546 return ERR_PTR(ret);
547 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400548 if (location.objectid) {
549 ret = fixup_tree_root_location(root, &location, &sub_root);
550 if (ret < 0)
551 return ERR_PTR(ret);
552 if (ret > 0)
553 return ERR_PTR(-ENOENT);
Chris Masonc5739bb2007-04-10 09:27:04 -0400554 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
555 sub_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400556 if (!inode)
557 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400558 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400559 if (sub_root != root) {
Chris Masonc5739bb2007-04-10 09:27:04 -0400560printk("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 -0400561 igrab(inode);
562 sub_root->inode = inode;
563 }
Chris Masond6e4a422007-04-06 15:37:36 -0400564 BTRFS_I(inode)->root = sub_root;
565 memcpy(&BTRFS_I(inode)->location, &location,
566 sizeof(location));
567 btrfs_read_locked_inode(inode);
568 unlock_new_inode(inode);
569 }
Chris Masone20d96d2007-03-22 12:13:20 -0400570 }
571 return d_splice_alias(inode, dentry);
572}
573
574static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
575{
576 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400577 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400578 struct btrfs_item *item;
579 struct btrfs_dir_item *di;
580 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400581 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400582 int ret;
583 u32 nritems;
584 struct btrfs_leaf *leaf;
585 int slot;
586 int advance;
587 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400588 int over = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400589 u32 di_cur;
590 u32 di_total;
591 u32 di_len;
592 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Masond6e4a422007-04-06 15:37:36 -0400593
594 /* FIXME, use a real flag for deciding about the key type */
595 if (root->fs_info->tree_root == root)
596 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400597 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400598 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400599 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400600 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400601 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400602 path = btrfs_alloc_path();
603 btrfs_init_path(path);
604 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400605 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400606 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400607 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400608 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400609 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400610 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400611 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400612 if (advance || slot >= nritems) {
613 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400614 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400615 if (ret)
616 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400617 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400618 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400619 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400620 } else {
621 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400622 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400623 }
624 }
625 advance = 1;
626 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400627 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
628 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400629 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masona429e512007-04-18 16:15:28 -0400630 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400631 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
632 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400633 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400634 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400635 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400636 di_cur = 0;
637 di_total = btrfs_item_size(leaf->items + slot);
638 while(di_cur < di_total) {
639 over = filldir(dirent, (const char *)(di + 1),
640 btrfs_dir_name_len(di),
641 btrfs_disk_key_offset(&item->key),
642 btrfs_disk_key_objectid(&di->location),
643 d_type);
644 if (over)
645 goto nopos;
646 di_len = btrfs_dir_name_len(di) + sizeof(*di);
647 di_cur += di_len;
648 di = (struct btrfs_dir_item *)((char *)di + di_len);
649 }
Chris Masone20d96d2007-03-22 12:13:20 -0400650 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400651 filp->f_pos++;
652nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400653 ret = 0;
654err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400655 btrfs_release_path(root, path);
656 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400657 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400658 return ret;
659}
660
661static void btrfs_put_super (struct super_block * sb)
662{
663 struct btrfs_root *root = btrfs_sb(sb);
664 int ret;
665
666 ret = close_ctree(root);
667 if (ret) {
668 printk("close ctree returns %d\n", ret);
669 }
670 sb->s_fs_info = NULL;
671}
Chris Mason2e635a22007-03-21 11:12:56 -0400672
673static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
674{
675 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400676 struct dentry * root_dentry;
677 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400678 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400679 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400680
681 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400682 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400683 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400684 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400685
Chris Mason0f7d52f2007-04-09 10:42:37 -0400686 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400687
Chris Mason0f7d52f2007-04-09 10:42:37 -0400688 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400689 printk("btrfs: open_ctree failed\n");
690 return -EIO;
691 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400692 sb->s_fs_info = tree_root;
693 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400694 printk("read in super total blocks %Lu root %Lu\n",
695 btrfs_super_total_blocks(disk_super),
696 btrfs_super_root_dir(disk_super));
697
Chris Masonc5739bb2007-04-10 09:27:04 -0400698 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
699 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400700 bi = BTRFS_I(inode);
701 bi->location.objectid = inode->i_ino;
702 bi->location.offset = 0;
703 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400704 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400705 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
706
Chris Mason2e635a22007-03-21 11:12:56 -0400707 if (!inode)
708 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400709 if (inode->i_state & I_NEW) {
710 btrfs_read_locked_inode(inode);
711 unlock_new_inode(inode);
712 }
Chris Mason2e635a22007-03-21 11:12:56 -0400713
Chris Masone20d96d2007-03-22 12:13:20 -0400714 root_dentry = d_alloc_root(inode);
715 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400716 iput(inode);
717 return -ENOMEM;
718 }
Chris Masone20d96d2007-03-22 12:13:20 -0400719 sb->s_root = root_dentry;
720
Chris Mason2e635a22007-03-21 11:12:56 -0400721 return 0;
722}
723
Chris Mason4730a4b2007-03-26 12:00:39 -0400724static int btrfs_write_inode(struct inode *inode, int wait)
725{
Chris Masond6e4a422007-04-06 15:37:36 -0400726 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400727 struct btrfs_trans_handle *trans;
Chris Masonb5133862007-04-24 11:52:22 -0400728 int ret = 0;
729
730 if (wait) {
731 mutex_lock(&root->fs_info->fs_mutex);
732 trans = btrfs_start_transaction(root, 1);
733 ret = btrfs_commit_transaction(trans, root);
734 mutex_unlock(&root->fs_info->fs_mutex);
735 }
736 return ret;
737}
738
739static void btrfs_dirty_inode(struct inode *inode)
740{
741 struct btrfs_root *root = BTRFS_I(inode)->root;
742 struct btrfs_trans_handle *trans;
Chris Mason4730a4b2007-03-26 12:00:39 -0400743
744 mutex_lock(&root->fs_info->fs_mutex);
745 trans = btrfs_start_transaction(root, 1);
Chris Masonb5133862007-04-24 11:52:22 -0400746 btrfs_update_inode(trans, root, inode);
747 btrfs_end_transaction(trans, root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400748 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason4730a4b2007-03-26 12:00:39 -0400749}
750
Chris Masond5719762007-03-23 10:01:08 -0400751static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400752 struct btrfs_root *root,
753 u64 objectid, int mode)
Chris Masond5719762007-03-23 10:01:08 -0400754{
755 struct inode *inode;
756 struct btrfs_inode_item inode_item;
Chris Mason1b05da22007-04-10 12:13:09 -0400757 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400758 int ret;
Chris Masond5719762007-03-23 10:01:08 -0400759
Chris Mason2619ba12007-04-10 16:58:11 -0400760 inode = new_inode(root->fs_info->sb);
Chris Masond5719762007-03-23 10:01:08 -0400761 if (!inode)
762 return ERR_PTR(-ENOMEM);
763
Chris Mason2619ba12007-04-10 16:58:11 -0400764 BTRFS_I(inode)->root = root;
Chris Masond5719762007-03-23 10:01:08 -0400765
766 inode->i_uid = current->fsuid;
767 inode->i_gid = current->fsgid;
768 inode->i_mode = mode;
769 inode->i_ino = objectid;
770 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -0400771 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -0400772 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -0400773 location = &BTRFS_I(inode)->location;
774 location->objectid = objectid;
775 location->flags = 0;
776 location->offset = 0;
777 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -0400778
779 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
780 BUG_ON(ret);
781
782 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400783 return inode;
784}
785
786static int btrfs_add_link(struct btrfs_trans_handle *trans,
787 struct dentry *dentry, struct inode *inode)
788{
789 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400790 struct btrfs_key key;
791 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
792 key.objectid = inode->i_ino;
793 key.flags = 0;
794 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
795 key.offset = 0;
796
797 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400798 dentry->d_name.name, dentry->d_name.len,
799 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400800 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400801 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400802 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400803 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400804 dentry->d_parent->d_inode);
805 }
Chris Masond5719762007-03-23 10:01:08 -0400806 return ret;
807}
808
809static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
810 struct dentry *dentry, struct inode *inode)
811{
812 int err = btrfs_add_link(trans, dentry, inode);
813 if (!err) {
814 d_instantiate(dentry, inode);
815 return 0;
816 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400817 if (err > 0)
818 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -0400819 return err;
820}
821
822static int btrfs_create(struct inode *dir, struct dentry *dentry,
823 int mode, struct nameidata *nd)
824{
825 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400826 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400827 struct inode *inode;
828 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400829 int drop_inode = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400830 u64 objectid;
Chris Masond5719762007-03-23 10:01:08 -0400831
Chris Masond561c022007-03-23 19:47:49 -0400832 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400833 trans = btrfs_start_transaction(root, 1);
Chris Mason2619ba12007-04-10 16:58:11 -0400834
835 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
836 if (err) {
837 err = -ENOSPC;
838 goto out_unlock;
839 }
840
841 inode = btrfs_new_inode(trans, root, objectid, mode);
Chris Masond5719762007-03-23 10:01:08 -0400842 err = PTR_ERR(inode);
843 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400844 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400845 // FIXME mark the inode dirty
846 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400847 if (err)
848 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400849 else {
850 inode->i_mapping->a_ops = &btrfs_aops;
851 inode->i_fop = &btrfs_file_operations;
852 inode->i_op = &btrfs_file_inode_operations;
853 }
Chris Masond5719762007-03-23 10:01:08 -0400854 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400855out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400856 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400857 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400858
Chris Mason134e9732007-03-25 13:44:56 -0400859 if (drop_inode) {
860 inode_dec_link_count(inode);
861 iput(inode);
862 }
Chris Masond5719762007-03-23 10:01:08 -0400863 return err;
864}
865
Chris Masonf7922032007-03-25 20:17:36 -0400866static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400867 struct btrfs_root *root,
868 u64 objectid, u64 dirid)
Chris Masonf7922032007-03-25 20:17:36 -0400869{
Chris Masonf7922032007-03-25 20:17:36 -0400870 int ret;
871 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400872 struct btrfs_key key;
873
Chris Masonf7922032007-03-25 20:17:36 -0400874 buf[0] = '.';
875 buf[1] = '.';
876
Chris Mason2619ba12007-04-10 16:58:11 -0400877 key.objectid = objectid;
Chris Masond6e4a422007-04-06 15:37:36 -0400878 key.offset = 0;
879 key.flags = 0;
880 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
881
Chris Mason2619ba12007-04-10 16:58:11 -0400882 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400883 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400884 if (ret)
885 goto error;
Chris Mason2619ba12007-04-10 16:58:11 -0400886 key.objectid = dirid;
887 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400888 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400889 if (ret)
890 goto error;
Chris Masonf7922032007-03-25 20:17:36 -0400891error:
892 return ret;
893}
894
895static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
896{
897 struct inode *inode;
898 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400899 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400900 int err = 0;
901 int drop_on_err = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400902 u64 objectid;
Chris Masonf7922032007-03-25 20:17:36 -0400903
904 mutex_lock(&root->fs_info->fs_mutex);
905 trans = btrfs_start_transaction(root, 1);
906 if (IS_ERR(trans)) {
907 err = PTR_ERR(trans);
908 goto out_unlock;
909 }
Chris Mason2619ba12007-04-10 16:58:11 -0400910
911 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
912 if (err) {
913 err = -ENOSPC;
914 goto out_unlock;
915 }
916
917 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
Chris Masonf7922032007-03-25 20:17:36 -0400918 if (IS_ERR(inode)) {
919 err = PTR_ERR(inode);
920 goto out_fail;
921 }
922 drop_on_err = 1;
923 inode->i_op = &btrfs_dir_inode_operations;
924 inode->i_fop = &btrfs_dir_file_operations;
925
Chris Mason2619ba12007-04-10 16:58:11 -0400926 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
927 if (err)
928 goto out_fail;
929
930 inode->i_size = 6;
931 err = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400932 if (err)
933 goto out_fail;
934 err = btrfs_add_link(trans, dentry, inode);
935 if (err)
936 goto out_fail;
937 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400938 drop_on_err = 0;
939
940out_fail:
941 btrfs_end_transaction(trans, root);
942out_unlock:
943 mutex_unlock(&root->fs_info->fs_mutex);
944 if (drop_on_err)
945 iput(inode);
946 return err;
947}
948
Chris Mason8fd17792007-04-19 21:01:03 -0400949static int btrfs_sync_file(struct file *file,
950 struct dentry *dentry, int datasync)
951{
952 struct inode *inode = dentry->d_inode;
953 struct btrfs_root *root = BTRFS_I(inode)->root;
954 int ret;
955 struct btrfs_trans_handle *trans;
956
957 mutex_lock(&root->fs_info->fs_mutex);
958 trans = btrfs_start_transaction(root, 1);
959 if (!trans) {
960 ret = -ENOMEM;
961 goto out;
962 }
963 ret = btrfs_commit_transaction(trans, root);
964 mutex_unlock(&root->fs_info->fs_mutex);
965out:
966 return ret > 0 ? EIO : ret;
967}
968
Chris Masond5719762007-03-23 10:01:08 -0400969static int btrfs_sync_fs(struct super_block *sb, int wait)
970{
971 struct btrfs_trans_handle *trans;
972 struct btrfs_root *root;
973 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400974 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400975
Chris Masond5719762007-03-23 10:01:08 -0400976 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400977 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400978 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400979 return 0;
980 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400981 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400982 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400983 trans = btrfs_start_transaction(root, 1);
984 ret = btrfs_commit_transaction(trans, root);
985 sb->s_dirt = 0;
986 BUG_ON(ret);
987printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400988 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400989 return 0;
990}
991
Chris Mason75dfe392007-03-29 11:56:46 -0400992static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400993 struct buffer_head *result, int create)
994{
995 int ret;
996 int err = 0;
997 u64 blocknr;
998 u64 extent_start = 0;
999 u64 extent_end = 0;
1000 u64 objectid = inode->i_ino;
Chris Mason236454d2007-04-19 13:37:44 -04001001 u32 found_type;
Chris Mason5caf2a02007-04-02 11:20:42 -04001002 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001003 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001004 struct btrfs_file_extent_item *item;
1005 struct btrfs_leaf *leaf;
1006 struct btrfs_disk_key *found_key;
1007
Chris Mason5caf2a02007-04-02 11:20:42 -04001008 path = btrfs_alloc_path();
1009 BUG_ON(!path);
1010 btrfs_init_path(path);
Chris Mason6567e832007-04-16 09:22:45 -04001011 if (create) {
Chris Mason6567e832007-04-16 09:22:45 -04001012 WARN_ON(1);
1013 }
Chris Masondee26a92007-03-26 16:00:06 -04001014
Chris Mason236454d2007-04-19 13:37:44 -04001015 ret = btrfs_lookup_file_extent(NULL, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001016 inode->i_ino,
Chris Mason236454d2007-04-19 13:37:44 -04001017 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001018 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001019 err = ret;
1020 goto out;
1021 }
1022
1023 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001024 if (path->slots[0] == 0) {
1025 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001026 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001027 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001028 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001029 }
1030
Chris Mason5caf2a02007-04-02 11:20:42 -04001031 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001032 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001033 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001034 blocknr = btrfs_file_extent_disk_blocknr(item);
1035 blocknr += btrfs_file_extent_offset(item);
1036
Chris Masondee26a92007-03-26 16:00:06 -04001037 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001038 found_key = &leaf->items[path->slots[0]].key;
Chris Mason236454d2007-04-19 13:37:44 -04001039 found_type = btrfs_disk_key_type(found_key);
Chris Masondee26a92007-03-26 16:00:06 -04001040 if (btrfs_disk_key_objectid(found_key) != objectid ||
Chris Mason236454d2007-04-19 13:37:44 -04001041 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masondee26a92007-03-26 16:00:06 -04001042 extent_end = 0;
1043 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001044 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001045 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001046 }
Chris Mason236454d2007-04-19 13:37:44 -04001047 found_type = btrfs_file_extent_type(item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason236454d2007-04-19 13:37:44 -04001049 if (found_type == BTRFS_FILE_EXTENT_REG) {
1050 extent_start = extent_start >> inode->i_blkbits;
1051 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1052 if (iblock >= extent_start && iblock < extent_end) {
1053 err = 0;
1054 btrfs_map_bh_to_logical(root, result, blocknr +
1055 iblock - extent_start);
1056 goto out;
1057 }
1058 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1059 char *ptr;
1060 char *map;
1061 u32 size;
1062 size = btrfs_file_extent_inline_len(leaf->items +
1063 path->slots[0]);
1064 extent_end = (extent_start + size) >> inode->i_blkbits;
1065 extent_start >>= inode->i_blkbits;
1066 if (iblock < extent_start || iblock > extent_end) {
1067 goto out;
1068 }
1069 ptr = btrfs_file_extent_inline_start(item);
1070 map = kmap(result->b_page);
1071 memcpy(map, ptr, size);
1072 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1073 flush_dcache_page(result->b_page);
1074 kunmap(result->b_page);
1075 set_buffer_uptodate(result);
1076 SetPageChecked(result->b_page);
1077 btrfs_map_bh_to_logical(root, result, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001078 }
Chris Masondee26a92007-03-26 16:00:06 -04001079out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001080 btrfs_release_path(root, path);
1081 btrfs_free_path(path);
Chris Mason75dfe392007-03-29 11:56:46 -04001082 return err;
1083}
1084
1085static int btrfs_get_block(struct inode *inode, sector_t iblock,
1086 struct buffer_head *result, int create)
1087{
1088 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001089 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001090 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001091 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001092 mutex_unlock(&root->fs_info->fs_mutex);
1093 return err;
1094}
1095
1096static int btrfs_prepare_write(struct file *file, struct page *page,
1097 unsigned from, unsigned to)
1098{
Chris Mason6407bf62007-03-27 06:33:00 -04001099 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001100}
1101
Chris Masond561c022007-03-23 19:47:49 -04001102static void btrfs_write_super(struct super_block *sb)
1103{
1104 btrfs_sync_fs(sb, 1);
1105}
1106
Chris Masondee26a92007-03-26 16:00:06 -04001107static int btrfs_readpage(struct file *file, struct page *page)
1108{
1109 return mpage_readpage(page, btrfs_get_block);
1110}
1111
Chris Mason236454d2007-04-19 13:37:44 -04001112/*
1113 * While block_write_full_page is writing back the dirty buffers under
1114 * the page lock, whoever dirtied the buffers may decide to clean them
1115 * again at any time. We handle that by only looking at the buffer
1116 * state inside lock_buffer().
1117 *
1118 * If block_write_full_page() is called for regular writeback
1119 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1120 * locked buffer. This only can happen if someone has written the buffer
1121 * directly, with submit_bh(). At the address_space level PageWriteback
1122 * prevents this contention from occurring.
1123 */
1124static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1125 struct writeback_control *wbc)
1126{
1127 int err;
1128 sector_t block;
1129 sector_t last_block;
1130 struct buffer_head *bh, *head;
1131 const unsigned blocksize = 1 << inode->i_blkbits;
1132 int nr_underway = 0;
1133
1134 BUG_ON(!PageLocked(page));
1135
1136 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1137
1138 if (!page_has_buffers(page)) {
1139 create_empty_buffers(page, blocksize,
1140 (1 << BH_Dirty)|(1 << BH_Uptodate));
1141 }
1142
1143 /*
1144 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1145 * here, and the (potentially unmapped) buffers may become dirty at
1146 * any time. If a buffer becomes dirty here after we've inspected it
1147 * then we just miss that fact, and the page stays dirty.
1148 *
1149 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1150 * handle that here by just cleaning them.
1151 */
1152
1153 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1154 head = page_buffers(page);
1155 bh = head;
1156
1157 /*
1158 * Get all the dirty buffers mapped to disk addresses and
1159 * handle any aliases from the underlying blockdev's mapping.
1160 */
1161 do {
1162 if (block > last_block) {
1163 /*
1164 * mapped buffers outside i_size will occur, because
1165 * this page can be outside i_size when there is a
1166 * truncate in progress.
1167 */
1168 /*
1169 * The buffer was zeroed by block_write_full_page()
1170 */
1171 clear_buffer_dirty(bh);
1172 set_buffer_uptodate(bh);
1173 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1174 WARN_ON(bh->b_size != blocksize);
1175 err = btrfs_get_block(inode, block, bh, 0);
1176 if (err)
1177 goto recover;
1178 if (buffer_new(bh)) {
1179 /* blockdev mappings never come here */
1180 clear_buffer_new(bh);
1181 unmap_underlying_metadata(bh->b_bdev,
1182 bh->b_blocknr);
1183 }
1184 }
1185 bh = bh->b_this_page;
1186 block++;
1187 } while (bh != head);
1188
1189 do {
1190 if (!buffer_mapped(bh))
1191 continue;
1192 /*
1193 * If it's a fully non-blocking write attempt and we cannot
1194 * lock the buffer then redirty the page. Note that this can
1195 * potentially cause a busy-wait loop from pdflush and kswapd
1196 * activity, but those code paths have their own higher-level
1197 * throttling.
1198 */
1199 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1200 lock_buffer(bh);
1201 } else if (test_set_buffer_locked(bh)) {
1202 redirty_page_for_writepage(wbc, page);
1203 continue;
1204 }
1205 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1206 mark_buffer_async_write(bh);
1207 } else {
1208 unlock_buffer(bh);
1209 }
1210 } while ((bh = bh->b_this_page) != head);
1211
1212 /*
1213 * The page and its buffers are protected by PageWriteback(), so we can
1214 * drop the bh refcounts early.
1215 */
1216 BUG_ON(PageWriteback(page));
1217 set_page_writeback(page);
1218
1219 do {
1220 struct buffer_head *next = bh->b_this_page;
1221 if (buffer_async_write(bh)) {
1222 submit_bh(WRITE, bh);
1223 nr_underway++;
1224 }
1225 bh = next;
1226 } while (bh != head);
1227 unlock_page(page);
1228
1229 err = 0;
1230done:
1231 if (nr_underway == 0) {
1232 /*
1233 * The page was marked dirty, but the buffers were
1234 * clean. Someone wrote them back by hand with
1235 * ll_rw_block/submit_bh. A rare case.
1236 */
1237 int uptodate = 1;
1238 do {
1239 if (!buffer_uptodate(bh)) {
1240 uptodate = 0;
1241 break;
1242 }
1243 bh = bh->b_this_page;
1244 } while (bh != head);
1245 if (uptodate)
1246 SetPageUptodate(page);
1247 end_page_writeback(page);
1248 /*
1249 * The page and buffer_heads can be released at any time from
1250 * here on.
1251 */
1252 wbc->pages_skipped++; /* We didn't write this page */
1253 }
1254 return err;
1255
1256recover:
1257 /*
1258 * ENOSPC, or some other error. We may already have added some
1259 * blocks to the file, so we need to write these out to avoid
1260 * exposing stale data.
1261 * The page is currently locked and not marked for writeback
1262 */
1263 bh = head;
1264 /* Recovery: lock and submit the mapped buffers */
1265 do {
1266 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1267 lock_buffer(bh);
1268 mark_buffer_async_write(bh);
1269 } else {
1270 /*
1271 * The buffer may have been set dirty during
1272 * attachment to a dirty page.
1273 */
1274 clear_buffer_dirty(bh);
1275 }
1276 } while ((bh = bh->b_this_page) != head);
1277 SetPageError(page);
1278 BUG_ON(PageWriteback(page));
1279 set_page_writeback(page);
1280 do {
1281 struct buffer_head *next = bh->b_this_page;
1282 if (buffer_async_write(bh)) {
1283 clear_buffer_dirty(bh);
1284 submit_bh(WRITE, bh);
1285 nr_underway++;
1286 }
1287 bh = next;
1288 } while (bh != head);
1289 unlock_page(page);
1290 goto done;
1291}
1292
1293/*
1294 * The generic ->writepage function for buffer-backed address_spaces
1295 */
Chris Masondee26a92007-03-26 16:00:06 -04001296static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1297{
Chris Mason236454d2007-04-19 13:37:44 -04001298 struct inode * const inode = page->mapping->host;
1299 loff_t i_size = i_size_read(inode);
1300 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1301 unsigned offset;
1302 void *kaddr;
1303
1304 /* Is the page fully inside i_size? */
1305 if (page->index < end_index)
1306 return __btrfs_write_full_page(inode, page, wbc);
1307
1308 /* Is the page fully outside i_size? (truncate in progress) */
1309 offset = i_size & (PAGE_CACHE_SIZE-1);
1310 if (page->index >= end_index+1 || !offset) {
1311 /*
1312 * The page may have dirty, unmapped buffers. For example,
1313 * they may have been added in ext3_writepage(). Make them
1314 * freeable here, so the page does not leak.
1315 */
1316 block_invalidatepage(page, 0);
1317 unlock_page(page);
1318 return 0; /* don't care */
1319 }
1320
1321 /*
1322 * The page straddles i_size. It must be zeroed out on each and every
1323 * writepage invokation because it may be mmapped. "A file is mapped
1324 * in multiples of the page size. For a file that is not a multiple of
1325 * the page size, the remaining memory is zeroed when mapped, and
1326 * writes to that region are not written out to the file."
1327 */
1328 kaddr = kmap_atomic(page, KM_USER0);
1329 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1330 flush_dcache_page(page);
1331 kunmap_atomic(kaddr, KM_USER0);
1332 return __btrfs_write_full_page(inode, page, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001333}
Chris Masond561c022007-03-23 19:47:49 -04001334
Chris Masonf4b9aa82007-03-27 11:05:53 -04001335static void btrfs_truncate(struct inode *inode)
1336{
Chris Masond6e4a422007-04-06 15:37:36 -04001337 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001338 int ret;
1339 struct btrfs_trans_handle *trans;
1340
1341 if (!S_ISREG(inode->i_mode))
1342 return;
1343 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1344 return;
1345
Chris Masone8f05c42007-04-04 14:30:09 -04001346 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001347
1348 /* FIXME, add redo link to tree so we don't leak on crash */
1349 mutex_lock(&root->fs_info->fs_mutex);
1350 trans = btrfs_start_transaction(root, 1);
1351 ret = btrfs_truncate_in_trans(trans, root, inode);
1352 BUG_ON(ret);
1353 ret = btrfs_end_transaction(trans, root);
1354 BUG_ON(ret);
1355 mutex_unlock(&root->fs_info->fs_mutex);
1356 mark_inode_dirty(inode);
1357}
1358
Chris Mason236454d2007-04-19 13:37:44 -04001359/*
1360 * Make sure any changes to nobh_commit_write() are reflected in
1361 * nobh_truncate_page(), since it doesn't call commit_write().
1362 */
1363static int btrfs_commit_write(struct file *file, struct page *page,
1364 unsigned from, unsigned to)
1365{
1366 struct inode *inode = page->mapping->host;
1367 struct buffer_head *bh;
1368 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1369
1370 SetPageUptodate(page);
1371 bh = page_buffers(page);
1372 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1373 set_page_dirty(page);
1374 }
1375 if (pos > inode->i_size) {
1376 i_size_write(inode, pos);
1377 mark_inode_dirty(inode);
1378 }
1379 return 0;
1380}
1381
Chris Mason75dfe392007-03-29 11:56:46 -04001382static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1383 struct page **prepared_pages,
1384 const char __user * buf)
1385{
1386 long page_fault = 0;
1387 int i;
1388 int offset = pos & (PAGE_CACHE_SIZE - 1);
1389
1390 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1391 size_t count = min_t(size_t,
1392 PAGE_CACHE_SIZE - offset, write_bytes);
1393 struct page *page = prepared_pages[i];
1394 fault_in_pages_readable(buf, count);
1395
1396 /* Copy data from userspace to the current page */
1397 kmap(page);
1398 page_fault = __copy_from_user(page_address(page) + offset,
1399 buf, count);
1400 /* Flush processor's dcache for this page */
1401 flush_dcache_page(page);
1402 kunmap(page);
1403 buf += count;
1404 write_bytes -= count;
1405
1406 if (page_fault)
1407 break;
1408 }
1409 return page_fault ? -EFAULT : 0;
1410}
1411
1412static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1413{
1414 size_t i;
1415 for (i = 0; i < num_pages; i++) {
1416 if (!pages[i])
1417 break;
1418 unlock_page(pages[i]);
1419 mark_page_accessed(pages[i]);
1420 page_cache_release(pages[i]);
1421 }
1422}
1423static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1424 struct btrfs_root *root,
1425 struct file *file,
1426 struct page **pages,
1427 size_t num_pages,
1428 loff_t pos,
1429 size_t write_bytes)
1430{
1431 int i;
1432 int offset;
1433 int err = 0;
1434 int ret;
1435 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001436 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason236454d2007-04-19 13:37:44 -04001437 struct buffer_head *bh;
1438 struct btrfs_file_extent_item *ei;
Chris Mason75dfe392007-03-29 11:56:46 -04001439
1440 for (i = 0; i < num_pages; i++) {
1441 offset = pos & (PAGE_CACHE_SIZE -1);
1442 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001443 /* FIXME, one block at a time */
1444
1445 mutex_lock(&root->fs_info->fs_mutex);
1446 trans = btrfs_start_transaction(root, 1);
Chris Mason236454d2007-04-19 13:37:44 -04001447
1448 bh = page_buffers(pages[i]);
1449 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1450 struct btrfs_key key;
1451 struct btrfs_path *path;
1452 char *ptr;
1453 u32 datasize;
1454
1455 path = btrfs_alloc_path();
1456 BUG_ON(!path);
1457 key.objectid = inode->i_ino;
1458 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1459 key.flags = 0;
1460 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1461 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1462 datasize = offset +
1463 btrfs_file_extent_calc_inline_size(write_bytes);
1464 ret = btrfs_insert_empty_item(trans, root, path, &key,
1465 datasize);
1466 BUG_ON(ret);
1467 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1468 path->slots[0], struct btrfs_file_extent_item);
1469 btrfs_set_file_extent_generation(ei, trans->transid);
1470 btrfs_set_file_extent_type(ei,
1471 BTRFS_FILE_EXTENT_INLINE);
1472 ptr = btrfs_file_extent_inline_start(ei);
1473 memcpy(ptr, bh->b_data, offset + write_bytes);
1474 mark_buffer_dirty(path->nodes[0]);
1475 btrfs_free_path(path);
1476 } else {
1477 btrfs_csum_file_block(trans, root, inode->i_ino,
Chris Masonf254e522007-03-29 15:15:27 -04001478 pages[i]->index << PAGE_CACHE_SHIFT,
1479 kmap(pages[i]), PAGE_CACHE_SIZE);
Chris Mason236454d2007-04-19 13:37:44 -04001480 kunmap(pages[i]);
1481 }
Chris Masonf254e522007-03-29 15:15:27 -04001482 SetPageChecked(pages[i]);
1483 ret = btrfs_end_transaction(trans, root);
1484 BUG_ON(ret);
1485 mutex_unlock(&root->fs_info->fs_mutex);
1486
Chris Mason236454d2007-04-19 13:37:44 -04001487 ret = btrfs_commit_write(file, pages[i], offset,
Chris Mason75dfe392007-03-29 11:56:46 -04001488 offset + this_write);
1489 pos += this_write;
1490 if (ret) {
1491 err = ret;
1492 goto failed;
1493 }
1494 WARN_ON(this_write > write_bytes);
1495 write_bytes -= this_write;
1496 }
1497failed:
1498 return err;
1499}
1500
Chris Masonb18c6682007-04-17 13:26:50 -04001501static int drop_extents(struct btrfs_trans_handle *trans,
1502 struct btrfs_root *root,
1503 struct inode *inode,
1504 u64 start, u64 end)
1505{
1506 int ret;
1507 struct btrfs_key key;
1508 struct btrfs_leaf *leaf;
1509 int slot;
1510 struct btrfs_file_extent_item *extent;
Chris Mason236454d2007-04-19 13:37:44 -04001511 u64 extent_end = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001512 int keep;
1513 struct btrfs_file_extent_item old;
1514 struct btrfs_path *path;
1515 u64 search_start = start;
1516 int bookend;
Chris Mason236454d2007-04-19 13:37:44 -04001517 int found_type;
1518 int found_extent;
1519 int found_inline;
1520
Chris Masonb18c6682007-04-17 13:26:50 -04001521 path = btrfs_alloc_path();
1522 if (!path)
1523 return -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001524 while(1) {
Chris Masona429e512007-04-18 16:15:28 -04001525 btrfs_release_path(root, path);
1526 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1527 search_start, -1);
1528 if (ret < 0)
1529 goto out;
1530 if (ret > 0) {
1531 if (path->slots[0] == 0) {
Chris Mason236454d2007-04-19 13:37:44 -04001532 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001533 goto out;
1534 }
1535 path->slots[0]--;
1536 }
Chris Masonb18c6682007-04-17 13:26:50 -04001537 keep = 0;
1538 bookend = 0;
Chris Mason236454d2007-04-19 13:37:44 -04001539 found_extent = 0;
1540 found_inline = 0;
1541 extent = NULL;
Chris Masonb18c6682007-04-17 13:26:50 -04001542 leaf = btrfs_buffer_leaf(path->nodes[0]);
1543 slot = path->slots[0];
1544 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
Chris Masonb18c6682007-04-17 13:26:50 -04001545 if (key.offset >= end || key.objectid != inode->i_ino) {
1546 ret = 0;
1547 goto out;
1548 }
Chris Mason236454d2007-04-19 13:37:44 -04001549 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1550 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001551 goto out;
Chris Mason236454d2007-04-19 13:37:44 -04001552 }
1553 extent = btrfs_item_ptr(leaf, slot,
1554 struct btrfs_file_extent_item);
1555 found_type = btrfs_file_extent_type(extent);
1556 if (found_type == BTRFS_FILE_EXTENT_REG) {
1557 extent_end = key.offset +
1558 (btrfs_file_extent_num_blocks(extent) <<
1559 inode->i_blkbits);
1560 found_extent = 1;
1561 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1562 found_inline = 1;
1563 extent_end = key.offset +
1564 btrfs_file_extent_inline_len(leaf->items + slot);
1565 }
1566
1567 if (!found_extent && !found_inline) {
1568 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001569 goto out;
Chris Mason236454d2007-04-19 13:37:44 -04001570 }
1571
1572 if (search_start >= extent_end) {
1573 ret = 0;
1574 goto out;
1575 }
1576
Chris Masona429e512007-04-18 16:15:28 -04001577 search_start = extent_end;
Chris Masonb18c6682007-04-17 13:26:50 -04001578
1579 if (end < extent_end && end >= key.offset) {
Chris Mason236454d2007-04-19 13:37:44 -04001580 if (found_extent) {
1581 memcpy(&old, extent, sizeof(old));
1582 ret = btrfs_inc_extent_ref(trans, root,
1583 btrfs_file_extent_disk_blocknr(&old),
1584 btrfs_file_extent_disk_num_blocks(&old));
1585 BUG_ON(ret);
1586 }
1587 WARN_ON(found_inline);
Chris Masonb18c6682007-04-17 13:26:50 -04001588 bookend = 1;
1589 }
1590
1591 if (start > key.offset) {
1592 u64 new_num;
Chris Masona429e512007-04-18 16:15:28 -04001593 u64 old_num;
Chris Masonb18c6682007-04-17 13:26:50 -04001594 /* truncate existing extent */
1595 keep = 1;
1596 WARN_ON(start & (root->blocksize - 1));
Chris Mason236454d2007-04-19 13:37:44 -04001597 if (found_extent) {
1598 new_num = (start - key.offset) >>
1599 inode->i_blkbits;
1600 old_num = btrfs_file_extent_num_blocks(extent);
1601 inode->i_blocks -= (old_num - new_num) << 3;
1602 btrfs_set_file_extent_num_blocks(extent,
1603 new_num);
1604 mark_buffer_dirty(path->nodes[0]);
1605 } else {
1606 WARN_ON(1);
1607 /*
1608 ret = btrfs_truncate_item(trans, root, path,
1609 start - key.offset);
1610 BUG_ON(ret);
1611 */
1612 }
Chris Masonb18c6682007-04-17 13:26:50 -04001613 }
1614 if (!keep) {
Chris Mason236454d2007-04-19 13:37:44 -04001615 u64 disk_blocknr = 0;
1616 u64 disk_num_blocks = 0;
1617 u64 extent_num_blocks = 0;
1618 if (found_extent) {
1619 disk_blocknr =
1620 btrfs_file_extent_disk_blocknr(extent);
1621 disk_num_blocks =
1622 btrfs_file_extent_disk_num_blocks(extent);
1623 extent_num_blocks =
1624 btrfs_file_extent_num_blocks(extent);
1625 }
Chris Masonb18c6682007-04-17 13:26:50 -04001626 ret = btrfs_del_item(trans, root, path);
1627 BUG_ON(ret);
1628 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001629 if (found_extent) {
1630 inode->i_blocks -=
1631 btrfs_file_extent_num_blocks(extent) << 3;
1632 ret = btrfs_free_extent(trans, root,
1633 disk_blocknr,
1634 disk_num_blocks, 0);
1635 }
Chris Masonb18c6682007-04-17 13:26:50 -04001636
1637 BUG_ON(ret);
1638 if (!bookend && search_start >= end) {
1639 ret = 0;
1640 goto out;
1641 }
1642 if (!bookend)
Chris Masona429e512007-04-18 16:15:28 -04001643 continue;
Chris Masonb18c6682007-04-17 13:26:50 -04001644 }
Chris Mason236454d2007-04-19 13:37:44 -04001645 if (bookend && found_extent) {
Chris Masonb18c6682007-04-17 13:26:50 -04001646 /* create bookend */
1647 struct btrfs_key ins;
Chris Masonb18c6682007-04-17 13:26:50 -04001648 ins.objectid = inode->i_ino;
1649 ins.offset = end;
1650 ins.flags = 0;
1651 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1652
1653 btrfs_release_path(root, path);
Chris Masonb18c6682007-04-17 13:26:50 -04001654 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1655 sizeof(*extent));
1656 BUG_ON(ret);
1657 extent = btrfs_item_ptr(
1658 btrfs_buffer_leaf(path->nodes[0]),
1659 path->slots[0],
1660 struct btrfs_file_extent_item);
1661 btrfs_set_file_extent_disk_blocknr(extent,
1662 btrfs_file_extent_disk_blocknr(&old));
1663 btrfs_set_file_extent_disk_num_blocks(extent,
1664 btrfs_file_extent_disk_num_blocks(&old));
1665
1666 btrfs_set_file_extent_offset(extent,
1667 btrfs_file_extent_offset(&old) +
1668 ((end - key.offset) >> inode->i_blkbits));
1669 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1670 (end - key.offset) >> inode->i_blkbits);
1671 btrfs_set_file_extent_num_blocks(extent,
1672 btrfs_file_extent_num_blocks(&old) -
1673 ((end - key.offset) >> inode->i_blkbits));
1674
Chris Mason236454d2007-04-19 13:37:44 -04001675 btrfs_set_file_extent_type(extent,
1676 BTRFS_FILE_EXTENT_REG);
Chris Masonb18c6682007-04-17 13:26:50 -04001677 btrfs_set_file_extent_generation(extent,
1678 btrfs_file_extent_generation(&old));
Chris Masonb18c6682007-04-17 13:26:50 -04001679 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona429e512007-04-18 16:15:28 -04001680 inode->i_blocks +=
1681 btrfs_file_extent_num_blocks(extent) << 3;
Chris Masonb18c6682007-04-17 13:26:50 -04001682 ret = 0;
Chris Mason70b2bef2007-04-17 15:39:32 -04001683 goto out;
Chris Masonb18c6682007-04-17 13:26:50 -04001684 }
Chris Masonb18c6682007-04-17 13:26:50 -04001685 }
Chris Masonb18c6682007-04-17 13:26:50 -04001686out:
Chris Masonb18c6682007-04-17 13:26:50 -04001687 btrfs_free_path(path);
1688 return ret;
1689}
1690
1691static int prepare_pages(struct btrfs_root *root,
Chris Mason75dfe392007-03-29 11:56:46 -04001692 struct file *file,
1693 struct page **pages,
1694 size_t num_pages,
1695 loff_t pos,
Chris Mason2932f3e2007-04-10 14:22:02 -04001696 unsigned long first_index,
1697 unsigned long last_index,
Chris Mason6567e832007-04-16 09:22:45 -04001698 size_t write_bytes,
1699 u64 alloc_extent_start)
Chris Mason75dfe392007-03-29 11:56:46 -04001700{
1701 int i;
1702 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1703 struct inode *inode = file->f_path.dentry->d_inode;
1704 int offset;
1705 int err = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001706 int this_write;
Chris Mason6567e832007-04-16 09:22:45 -04001707 struct buffer_head *bh;
1708 struct buffer_head *head;
Chris Mason75dfe392007-03-29 11:56:46 -04001709 loff_t isize = i_size_read(inode);
1710
1711 memset(pages, 0, num_pages * sizeof(struct page *));
1712
1713 for (i = 0; i < num_pages; i++) {
1714 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1715 if (!pages[i]) {
1716 err = -ENOMEM;
1717 goto failed_release;
1718 }
1719 offset = pos & (PAGE_CACHE_SIZE -1);
1720 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Mason6567e832007-04-16 09:22:45 -04001721 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1722 (1 << BH_Uptodate));
1723 head = page_buffers(pages[i]);
1724 bh = head;
1725 do {
1726 err = btrfs_map_bh_to_logical(root, bh,
1727 alloc_extent_start);
1728 BUG_ON(err);
1729 if (err)
1730 goto failed_truncate;
1731 bh = bh->b_this_page;
Chris Mason236454d2007-04-19 13:37:44 -04001732 if (alloc_extent_start)
1733 alloc_extent_start++;
Chris Mason6567e832007-04-16 09:22:45 -04001734 } while (bh != head);
Chris Mason75dfe392007-03-29 11:56:46 -04001735 pos += this_write;
Chris Mason75dfe392007-03-29 11:56:46 -04001736 WARN_ON(this_write > write_bytes);
1737 write_bytes -= this_write;
1738 }
1739 return 0;
1740
1741failed_release:
1742 btrfs_drop_pages(pages, num_pages);
1743 return err;
1744
1745failed_truncate:
1746 btrfs_drop_pages(pages, num_pages);
1747 if (pos > isize)
1748 vmtruncate(inode, isize);
1749 return err;
1750}
1751
1752static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1753 size_t count, loff_t *ppos)
1754{
1755 loff_t pos;
1756 size_t num_written = 0;
1757 int err = 0;
1758 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001759 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001760 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonb18c6682007-04-17 13:26:50 -04001761 struct page *pages[8];
Chris Masona429e512007-04-18 16:15:28 -04001762 struct page *pinned[2] = { NULL, NULL };
Chris Mason2932f3e2007-04-10 14:22:02 -04001763 unsigned long first_index;
1764 unsigned long last_index;
Chris Mason6567e832007-04-16 09:22:45 -04001765 u64 start_pos;
1766 u64 num_blocks;
1767 u64 alloc_extent_start;
Chris Mason6567e832007-04-16 09:22:45 -04001768 struct btrfs_trans_handle *trans;
Chris Masonb18c6682007-04-17 13:26:50 -04001769 struct btrfs_key ins;
Chris Mason75dfe392007-03-29 11:56:46 -04001770
1771 if (file->f_flags & O_DIRECT)
1772 return -EINVAL;
1773 pos = *ppos;
Chris Mason75dfe392007-03-29 11:56:46 -04001774 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1775 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1776 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1777 if (err)
1778 goto out;
1779 if (count == 0)
1780 goto out;
1781 err = remove_suid(file->f_path.dentry);
1782 if (err)
1783 goto out;
1784 file_update_time(file);
Chris Masona429e512007-04-18 16:15:28 -04001785
1786 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1787 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1788 inode->i_blkbits;
1789
Chris Mason75dfe392007-03-29 11:56:46 -04001790 mutex_lock(&inode->i_mutex);
Chris Mason2932f3e2007-04-10 14:22:02 -04001791 first_index = pos >> PAGE_CACHE_SHIFT;
1792 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
Chris Mason6567e832007-04-16 09:22:45 -04001793
Chris Masona429e512007-04-18 16:15:28 -04001794 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1795 (pos & (PAGE_CACHE_SIZE - 1))) {
1796 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1797 if (!PageUptodate(pinned[0])) {
1798 ret = mpage_readpage(pinned[0], btrfs_get_block);
1799 BUG_ON(ret);
1800 } else {
1801 unlock_page(pinned[0]);
1802 }
1803 }
1804 if (first_index != last_index &&
1805 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1806 (count & (PAGE_CACHE_SIZE - 1))) {
1807 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1808 if (!PageUptodate(pinned[1])) {
1809 ret = mpage_readpage(pinned[1], btrfs_get_block);
1810 BUG_ON(ret);
1811 } else {
1812 unlock_page(pinned[1]);
1813 }
1814 }
1815
Chris Mason6567e832007-04-16 09:22:45 -04001816 mutex_lock(&root->fs_info->fs_mutex);
1817 trans = btrfs_start_transaction(root, 1);
1818 if (!trans) {
1819 err = -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001820 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -04001821 goto out_unlock;
1822 }
Chris Masona429e512007-04-18 16:15:28 -04001823 /* FIXME blocksize != 4096 */
1824 inode->i_blocks += num_blocks << 3;
Chris Masonb18c6682007-04-17 13:26:50 -04001825 if (start_pos < inode->i_size) {
Chris Masona429e512007-04-18 16:15:28 -04001826 /* FIXME blocksize != pagesize */
Chris Masonb18c6682007-04-17 13:26:50 -04001827 ret = drop_extents(trans, root, inode,
1828 start_pos,
1829 (pos + count + root->blocksize -1) &
Chris Masona429e512007-04-18 16:15:28 -04001830 ~((u64)root->blocksize - 1));
Chris Mason236454d2007-04-19 13:37:44 -04001831 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001832 }
Chris Mason236454d2007-04-19 13:37:44 -04001833 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
1834 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
Chris Mason4d775672007-04-20 20:23:12 -04001835 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
Chris Masonc62a1922007-04-24 12:07:39 -04001836 num_blocks, 1, (u64)-1, &ins);
Chris Mason236454d2007-04-19 13:37:44 -04001837 BUG_ON(ret);
1838 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
Chris Masonb18c6682007-04-17 13:26:50 -04001839 start_pos, ins.objectid, ins.offset);
Chris Mason236454d2007-04-19 13:37:44 -04001840 BUG_ON(ret);
1841 } else {
1842 ins.offset = 0;
1843 ins.objectid = 0;
1844 }
Chris Masonb18c6682007-04-17 13:26:50 -04001845 BUG_ON(ret);
1846 alloc_extent_start = ins.objectid;
1847 ret = btrfs_end_transaction(trans, root);
Chris Mason6567e832007-04-16 09:22:45 -04001848 mutex_unlock(&root->fs_info->fs_mutex);
1849
Chris Mason75dfe392007-03-29 11:56:46 -04001850 while(count > 0) {
1851 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1852 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1853 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1854 PAGE_CACHE_SHIFT;
Chris Masonb18c6682007-04-17 13:26:50 -04001855
1856 memset(pages, 0, sizeof(pages));
1857 ret = prepare_pages(root, file, pages, num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04001858 pos, first_index, last_index,
1859 write_bytes, alloc_extent_start);
Chris Mason75dfe392007-03-29 11:56:46 -04001860 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001861
Chris Mason6567e832007-04-16 09:22:45 -04001862 /* FIXME blocks != pagesize */
Chris Mason236454d2007-04-19 13:37:44 -04001863 if (alloc_extent_start)
1864 alloc_extent_start += num_pages;
Chris Mason75dfe392007-03-29 11:56:46 -04001865 ret = btrfs_copy_from_user(pos, num_pages,
1866 write_bytes, pages, buf);
1867 BUG_ON(ret);
1868
Chris Masonf254e522007-03-29 15:15:27 -04001869 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason70b2bef2007-04-17 15:39:32 -04001870 num_pages, pos, write_bytes);
Chris Mason75dfe392007-03-29 11:56:46 -04001871 BUG_ON(ret);
1872 btrfs_drop_pages(pages, num_pages);
1873
Chris Mason75dfe392007-03-29 11:56:46 -04001874 buf += write_bytes;
1875 count -= write_bytes;
1876 pos += write_bytes;
1877 num_written += write_bytes;
1878
1879 balance_dirty_pages_ratelimited(inode->i_mapping);
1880 cond_resched();
1881 }
Chris Mason6567e832007-04-16 09:22:45 -04001882out_unlock:
Chris Mason75dfe392007-03-29 11:56:46 -04001883 mutex_unlock(&inode->i_mutex);
1884out:
Chris Masona429e512007-04-18 16:15:28 -04001885 if (pinned[0])
1886 page_cache_release(pinned[0]);
1887 if (pinned[1])
1888 page_cache_release(pinned[1]);
Chris Mason75dfe392007-03-29 11:56:46 -04001889 *ppos = pos;
1890 current->backing_dev_info = NULL;
Chris Masona429e512007-04-18 16:15:28 -04001891 mark_inode_dirty(inode);
Chris Mason75dfe392007-03-29 11:56:46 -04001892 return num_written ? num_written : err;
1893}
1894
Chris Masonf254e522007-03-29 15:15:27 -04001895static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1896 unsigned long offset, unsigned long size)
1897{
1898 char *kaddr;
1899 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001900 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001901
1902 if (size > count)
1903 size = count;
1904
1905 if (!PageChecked(page)) {
1906 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04001907 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason236454d2007-04-19 13:37:44 -04001908
Chris Masonf254e522007-03-29 15:15:27 -04001909 int ret = btrfs_csum_verify_file_block(root,
Chris Mason236454d2007-04-19 13:37:44 -04001910 page->mapping->host->i_ino,
1911 page->index << PAGE_CACHE_SHIFT,
1912 kmap(page), PAGE_CACHE_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -04001913 if (ret) {
1914 printk("failed to verify ino %lu page %lu\n",
1915 page->mapping->host->i_ino,
1916 page->index);
1917 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1918 }
1919 SetPageChecked(page);
1920 kunmap(page);
1921 }
1922 /*
1923 * Faults on the destination of a read are common, so do it before
1924 * taking the kmap.
1925 */
1926 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1927 kaddr = kmap_atomic(page, KM_USER0);
1928 left = __copy_to_user_inatomic(desc->arg.buf,
1929 kaddr + offset, size);
1930 kunmap_atomic(kaddr, KM_USER0);
1931 if (left == 0)
1932 goto success;
1933 }
1934
1935 /* Do it the slow way */
1936 kaddr = kmap(page);
1937 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1938 kunmap(page);
1939
1940 if (left) {
1941 size -= left;
1942 desc->error = -EFAULT;
1943 }
1944success:
1945 desc->count = count - size;
1946 desc->written += size;
1947 desc->arg.buf += size;
1948 return size;
1949}
1950
1951/**
1952 * btrfs_file_aio_read - filesystem read routine
1953 * @iocb: kernel I/O control block
1954 * @iov: io vector request
1955 * @nr_segs: number of segments in the iovec
1956 * @pos: current file position
1957 */
1958static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1959 unsigned long nr_segs, loff_t pos)
1960{
1961 struct file *filp = iocb->ki_filp;
1962 ssize_t retval;
1963 unsigned long seg;
1964 size_t count;
1965 loff_t *ppos = &iocb->ki_pos;
1966
1967 count = 0;
1968 for (seg = 0; seg < nr_segs; seg++) {
1969 const struct iovec *iv = &iov[seg];
1970
1971 /*
1972 * If any segment has a negative length, or the cumulative
1973 * length ever wraps negative then return -EINVAL.
1974 */
1975 count += iv->iov_len;
1976 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1977 return -EINVAL;
1978 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1979 continue;
1980 if (seg == 0)
1981 return -EFAULT;
1982 nr_segs = seg;
1983 count -= iv->iov_len; /* This segment is no good */
1984 break;
1985 }
1986 retval = 0;
1987 if (count) {
1988 for (seg = 0; seg < nr_segs; seg++) {
1989 read_descriptor_t desc;
1990
1991 desc.written = 0;
1992 desc.arg.buf = iov[seg].iov_base;
1993 desc.count = iov[seg].iov_len;
1994 if (desc.count == 0)
1995 continue;
1996 desc.error = 0;
1997 do_generic_file_read(filp, ppos, &desc,
1998 btrfs_read_actor);
1999 retval += desc.written;
2000 if (desc.error) {
2001 retval = retval ?: desc.error;
2002 break;
2003 }
2004 }
2005 }
2006 return retval;
2007}
2008
Chris Mason2619ba12007-04-10 16:58:11 -04002009static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2010{
2011 struct btrfs_trans_handle *trans;
2012 struct btrfs_key key;
2013 struct btrfs_root_item root_item;
2014 struct btrfs_inode_item *inode_item;
2015 struct buffer_head *subvol;
2016 struct btrfs_leaf *leaf;
2017 struct btrfs_root *new_root;
2018 struct inode *inode;
2019 int ret;
2020 u64 objectid;
2021 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2022
2023 mutex_lock(&root->fs_info->fs_mutex);
2024 trans = btrfs_start_transaction(root, 1);
2025 BUG_ON(!trans);
2026
2027 subvol = btrfs_alloc_free_block(trans, root);
Chris Mason5e828492007-04-23 19:10:49 -04002028 if (subvol == NULL)
2029 return -ENOSPC;
Chris Mason2619ba12007-04-10 16:58:11 -04002030 leaf = btrfs_buffer_leaf(subvol);
2031 btrfs_set_header_nritems(&leaf->header, 0);
2032 btrfs_set_header_level(&leaf->header, 0);
Chris Mason7eccb902007-04-11 15:53:25 -04002033 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002034 btrfs_set_header_generation(&leaf->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04002035 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
Chris Mason2619ba12007-04-10 16:58:11 -04002036 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2037 sizeof(leaf->header.fsid));
Chris Mason4d775672007-04-20 20:23:12 -04002038 mark_buffer_dirty(subvol);
Chris Mason2619ba12007-04-10 16:58:11 -04002039
2040 inode_item = &root_item.inode;
2041 memset(inode_item, 0, sizeof(*inode_item));
2042 btrfs_set_inode_generation(inode_item, 1);
2043 btrfs_set_inode_size(inode_item, 3);
2044 btrfs_set_inode_nlink(inode_item, 1);
2045 btrfs_set_inode_nblocks(inode_item, 1);
2046 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2047
Chris Mason7eccb902007-04-11 15:53:25 -04002048 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002049 btrfs_set_root_refs(&root_item, 1);
Chris Mason5e828492007-04-23 19:10:49 -04002050 brelse(subvol);
2051 subvol = NULL;
Chris Mason2619ba12007-04-10 16:58:11 -04002052
Chris Mason2619ba12007-04-10 16:58:11 -04002053 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2054 0, &objectid);
2055 BUG_ON(ret);
2056
2057 btrfs_set_root_dirid(&root_item, new_dirid);
2058
2059 key.objectid = objectid;
2060 key.offset = 1;
2061 key.flags = 0;
2062 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2063 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2064 &root_item);
2065 BUG_ON(ret);
2066
2067 /*
2068 * insert the directory item
2069 */
2070 key.offset = (u64)-1;
2071 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2072 name, namelen,
2073 root->fs_info->sb->s_root->d_inode->i_ino,
2074 &key, 0);
2075 BUG_ON(ret);
2076
2077 ret = btrfs_commit_transaction(trans, root);
2078 BUG_ON(ret);
2079
2080 new_root = btrfs_read_fs_root(root->fs_info, &key);
2081 BUG_ON(!new_root);
2082
2083 trans = btrfs_start_transaction(new_root, 1);
2084 BUG_ON(!trans);
2085
2086 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
2087 inode->i_op = &btrfs_dir_inode_operations;
2088 inode->i_fop = &btrfs_dir_file_operations;
2089
2090 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2091 BUG_ON(ret);
2092
2093 inode->i_nlink = 1;
2094 inode->i_size = 6;
2095 ret = btrfs_update_inode(trans, new_root, inode);
2096 BUG_ON(ret);
2097
2098 ret = btrfs_commit_transaction(trans, new_root);
2099 BUG_ON(ret);
2100
2101 iput(inode);
2102
2103 mutex_unlock(&root->fs_info->fs_mutex);
2104 return 0;
2105}
2106
Chris Masonc5739bb2007-04-10 09:27:04 -04002107static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2108{
2109 struct btrfs_trans_handle *trans;
2110 struct btrfs_key key;
2111 struct btrfs_root_item new_root_item;
2112 int ret;
2113 u64 objectid;
2114
Chris Mason2619ba12007-04-10 16:58:11 -04002115 if (!root->ref_cows)
2116 return -EINVAL;
2117
Chris Masonc5739bb2007-04-10 09:27:04 -04002118 mutex_lock(&root->fs_info->fs_mutex);
2119 trans = btrfs_start_transaction(root, 1);
2120 BUG_ON(!trans);
2121
2122 ret = btrfs_update_inode(trans, root, root->inode);
2123 BUG_ON(ret);
2124
Chris Mason1b05da22007-04-10 12:13:09 -04002125 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2126 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04002127 BUG_ON(ret);
2128
Chris Masonc5739bb2007-04-10 09:27:04 -04002129 memcpy(&new_root_item, &root->root_item,
2130 sizeof(new_root_item));
2131
2132 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002133 key.offset = 1;
2134 key.flags = 0;
2135 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason7eccb902007-04-11 15:53:25 -04002136 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
Chris Masonc5739bb2007-04-10 09:27:04 -04002137
2138 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2139 &new_root_item);
2140 BUG_ON(ret);
2141
Chris Masonc5739bb2007-04-10 09:27:04 -04002142 /*
2143 * insert the directory item
2144 */
2145 key.offset = (u64)-1;
2146 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2147 name, namelen,
2148 root->fs_info->sb->s_root->d_inode->i_ino,
2149 &key, 0);
2150
2151 BUG_ON(ret);
2152
2153 ret = btrfs_inc_root_ref(trans, root);
2154 BUG_ON(ret);
2155
2156 ret = btrfs_commit_transaction(trans, root);
2157 BUG_ON(ret);
2158 mutex_unlock(&root->fs_info->fs_mutex);
2159 return 0;
2160}
2161
Chris Mason8352d8a2007-04-12 10:43:05 -04002162static int add_disk(struct btrfs_root *root, char *name, int namelen)
2163{
2164 struct block_device *bdev;
2165 struct btrfs_path *path;
2166 struct super_block *sb = root->fs_info->sb;
2167 struct btrfs_root *dev_root = root->fs_info->dev_root;
2168 struct btrfs_trans_handle *trans;
2169 struct btrfs_device_item *dev_item;
2170 struct btrfs_key key;
2171 u16 item_size;
2172 u64 num_blocks;
2173 u64 new_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -04002174 u64 device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -04002175 int ret;
Chris Masonb4100d62007-04-12 12:14:00 -04002176
Chris Mason8352d8a2007-04-12 10:43:05 -04002177printk("adding disk %s\n", name);
2178 path = btrfs_alloc_path();
2179 if (!path)
2180 return -ENOMEM;
2181 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2182 bdev = open_bdev_excl(name, O_RDWR, sb);
2183 if (IS_ERR(bdev)) {
2184 ret = PTR_ERR(bdev);
2185printk("open bdev excl failed ret %d\n", ret);
2186 goto out_nolock;
2187 }
2188 set_blocksize(bdev, sb->s_blocksize);
2189 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2190 key.objectid = num_blocks;
2191 key.offset = new_blocks;
2192 key.flags = 0;
2193 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2194
2195 mutex_lock(&dev_root->fs_info->fs_mutex);
2196 trans = btrfs_start_transaction(dev_root, 1);
2197 item_size = sizeof(*dev_item) + namelen;
2198printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2199 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2200 if (ret) {
2201printk("insert failed %d\n", ret);
2202 close_bdev_excl(bdev);
2203 if (ret > 0)
2204 ret = -EEXIST;
2205 goto out;
2206 }
2207 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2208 path->slots[0], struct btrfs_device_item);
2209 btrfs_set_device_pathlen(dev_item, namelen);
2210 memcpy(dev_item + 1, name, namelen);
Chris Masonb4100d62007-04-12 12:14:00 -04002211
2212 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2213 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2214 btrfs_set_device_id(dev_item, device_id);
Chris Mason8352d8a2007-04-12 10:43:05 -04002215 mark_buffer_dirty(path->nodes[0]);
2216
Chris Masonb4100d62007-04-12 12:14:00 -04002217 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2218 new_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -04002219
2220 if (!ret) {
2221 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2222 num_blocks + new_blocks);
2223 i_size_write(root->fs_info->btree_inode,
2224 (num_blocks + new_blocks) <<
2225 root->fs_info->btree_inode->i_blkbits);
2226 }
2227
2228out:
2229 ret = btrfs_commit_transaction(trans, dev_root);
2230 BUG_ON(ret);
2231 mutex_unlock(&root->fs_info->fs_mutex);
2232out_nolock:
2233 btrfs_free_path(path);
2234
2235 return ret;
2236}
2237
Chris Masonc5739bb2007-04-10 09:27:04 -04002238static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2239 cmd, unsigned long arg)
2240{
2241 struct btrfs_root *root = BTRFS_I(inode)->root;
2242 struct btrfs_ioctl_vol_args vol_args;
Chris Mason8352d8a2007-04-12 10:43:05 -04002243 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -04002244 struct btrfs_dir_item *di;
Chris Masonc5739bb2007-04-10 09:27:04 -04002245 int namelen;
Chris Mason2619ba12007-04-10 16:58:11 -04002246 struct btrfs_path *path;
2247 u64 root_dirid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002248
Chris Masonc5739bb2007-04-10 09:27:04 -04002249 switch (cmd) {
2250 case BTRFS_IOC_SNAP_CREATE:
2251 if (copy_from_user(&vol_args,
2252 (struct btrfs_ioctl_vol_args __user *)arg,
2253 sizeof(vol_args)))
2254 return -EFAULT;
2255 namelen = strlen(vol_args.name);
2256 if (namelen > BTRFS_VOL_NAME_MAX)
2257 return -EINVAL;
Chris Mason2619ba12007-04-10 16:58:11 -04002258 path = btrfs_alloc_path();
2259 if (!path)
2260 return -ENOMEM;
Chris Mason2d13d8d2007-04-10 20:07:20 -04002261 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Chris Mason2619ba12007-04-10 16:58:11 -04002262 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason7e381802007-04-19 15:36:27 -04002263 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
Chris Mason2619ba12007-04-10 16:58:11 -04002264 path, root_dirid,
2265 vol_args.name, namelen, 0);
2266 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2d13d8d2007-04-10 20:07:20 -04002267 btrfs_free_path(path);
Chris Mason7e381802007-04-19 15:36:27 -04002268 if (di && !IS_ERR(di))
Chris Mason2619ba12007-04-10 16:58:11 -04002269 return -EEXIST;
2270
2271 if (root == root->fs_info->tree_root)
2272 ret = create_subvol(root, vol_args.name, namelen);
2273 else
2274 ret = create_snapshot(root, vol_args.name, namelen);
Chris Masonc5739bb2007-04-10 09:27:04 -04002275 WARN_ON(ret);
2276 break;
Chris Mason8352d8a2007-04-12 10:43:05 -04002277 case BTRFS_IOC_ADD_DISK:
2278 if (copy_from_user(&vol_args,
2279 (struct btrfs_ioctl_vol_args __user *)arg,
2280 sizeof(vol_args)))
2281 return -EFAULT;
2282 namelen = strlen(vol_args.name);
2283 if (namelen > BTRFS_VOL_NAME_MAX)
2284 return -EINVAL;
2285 vol_args.name[namelen] = '\0';
2286 ret = add_disk(root, vol_args.name, namelen);
2287 break;
Chris Masonc5739bb2007-04-10 09:27:04 -04002288 default:
2289 return -ENOTTY;
2290 }
Chris Mason8352d8a2007-04-12 10:43:05 -04002291 return ret;
Chris Masonc5739bb2007-04-10 09:27:04 -04002292}
2293
Chris Mason2c90e5d2007-04-02 10:50:19 -04002294static struct kmem_cache *btrfs_inode_cachep;
2295struct kmem_cache *btrfs_trans_handle_cachep;
2296struct kmem_cache *btrfs_transaction_cachep;
2297struct kmem_cache *btrfs_bit_radix_cachep;
2298struct kmem_cache *btrfs_path_cachep;
2299
2300/*
2301 * Called inside transaction, so use GFP_NOFS
2302 */
2303static struct inode *btrfs_alloc_inode(struct super_block *sb)
2304{
2305 struct btrfs_inode *ei;
2306
2307 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2308 if (!ei)
2309 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002310 return &ei->vfs_inode;
2311}
2312
2313static void btrfs_destroy_inode(struct inode *inode)
2314{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002315 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04002316 WARN_ON(inode->i_data.nrpages);
2317
Chris Mason2c90e5d2007-04-02 10:50:19 -04002318 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2319}
2320
2321static void init_once(void * foo, struct kmem_cache * cachep,
2322 unsigned long flags)
2323{
2324 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2325
2326 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2327 SLAB_CTOR_CONSTRUCTOR) {
2328 inode_init_once(&ei->vfs_inode);
2329 }
2330}
2331
2332static int init_inodecache(void)
2333{
2334 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2335 sizeof(struct btrfs_inode),
2336 0, (SLAB_RECLAIM_ACCOUNT|
2337 SLAB_MEM_SPREAD),
2338 init_once, NULL);
2339 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2340 sizeof(struct btrfs_trans_handle),
2341 0, (SLAB_RECLAIM_ACCOUNT|
2342 SLAB_MEM_SPREAD),
2343 NULL, NULL);
2344 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2345 sizeof(struct btrfs_transaction),
2346 0, (SLAB_RECLAIM_ACCOUNT|
2347 SLAB_MEM_SPREAD),
2348 NULL, NULL);
2349 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2350 sizeof(struct btrfs_transaction),
2351 0, (SLAB_RECLAIM_ACCOUNT|
2352 SLAB_MEM_SPREAD),
2353 NULL, NULL);
2354 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2355 256,
2356 0, (SLAB_RECLAIM_ACCOUNT|
2357 SLAB_MEM_SPREAD |
2358 SLAB_DESTROY_BY_RCU),
2359 NULL, NULL);
2360 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2361 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2362 return -ENOMEM;
2363 return 0;
2364}
2365
2366static void destroy_inodecache(void)
2367{
2368 kmem_cache_destroy(btrfs_inode_cachep);
2369 kmem_cache_destroy(btrfs_trans_handle_cachep);
2370 kmem_cache_destroy(btrfs_transaction_cachep);
2371 kmem_cache_destroy(btrfs_bit_radix_cachep);
2372 kmem_cache_destroy(btrfs_path_cachep);
2373}
2374
Chris Mason2e635a22007-03-21 11:12:56 -04002375static int btrfs_get_sb(struct file_system_type *fs_type,
2376 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2377{
2378 return get_sb_bdev(fs_type, flags, dev_name, data,
2379 btrfs_fill_super, mnt);
2380}
2381
Chris Mason236454d2007-04-19 13:37:44 -04002382
2383static int btrfs_getattr(struct vfsmount *mnt,
2384 struct dentry *dentry, struct kstat *stat)
2385{
2386 struct inode *inode = dentry->d_inode;
2387 generic_fillattr(inode, stat);
2388 stat->blksize = 256 * 1024;
2389 return 0;
2390}
2391
Chris Mason8fd17792007-04-19 21:01:03 -04002392static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2393{
2394 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2395 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2396
2397 buf->f_namelen = BTRFS_NAME_LEN;
2398 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2399 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2400 buf->f_bavail = buf->f_bfree;
2401 buf->f_bsize = dentry->d_sb->s_blocksize;
2402 buf->f_type = BTRFS_SUPER_MAGIC;
2403 return 0;
2404}
Chris Masonb5133862007-04-24 11:52:22 -04002405
Chris Mason2e635a22007-03-21 11:12:56 -04002406static struct file_system_type btrfs_fs_type = {
2407 .owner = THIS_MODULE,
2408 .name = "btrfs",
2409 .get_sb = btrfs_get_sb,
2410 .kill_sb = kill_block_super,
2411 .fs_flags = FS_REQUIRES_DEV,
2412};
2413
Chris Masone20d96d2007-03-22 12:13:20 -04002414static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -04002415 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002416 .put_super = btrfs_put_super,
2417 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04002418 .write_super = btrfs_write_super,
2419 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04002420 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -04002421 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002422 .alloc_inode = btrfs_alloc_inode,
2423 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -04002424 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -04002425};
2426
2427static struct inode_operations btrfs_dir_inode_operations = {
2428 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04002429 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04002430 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04002431 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04002432 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04002433};
2434
Chris Masond6e4a422007-04-06 15:37:36 -04002435static struct inode_operations btrfs_dir_ro_inode_operations = {
2436 .lookup = btrfs_lookup,
2437};
2438
Chris Masone20d96d2007-03-22 12:13:20 -04002439static struct file_operations btrfs_dir_file_operations = {
2440 .llseek = generic_file_llseek,
2441 .read = generic_read_dir,
2442 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04002443 .ioctl = btrfs_ioctl,
Chris Masone20d96d2007-03-22 12:13:20 -04002444};
2445
Chris Masondee26a92007-03-26 16:00:06 -04002446static struct address_space_operations btrfs_aops = {
2447 .readpage = btrfs_readpage,
Chris Masondee26a92007-03-26 16:00:06 -04002448 .writepage = btrfs_writepage,
2449 .sync_page = block_sync_page,
2450 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04002451 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04002452};
2453
2454static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04002455 .truncate = btrfs_truncate,
Chris Mason236454d2007-04-19 13:37:44 -04002456 .getattr = btrfs_getattr,
Chris Masondee26a92007-03-26 16:00:06 -04002457};
2458
2459static struct file_operations btrfs_file_operations = {
2460 .llseek = generic_file_llseek,
2461 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04002462 .aio_read = btrfs_file_aio_read,
2463 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04002464 .mmap = generic_file_mmap,
2465 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04002466 .ioctl = btrfs_ioctl,
Chris Mason8fd17792007-04-19 21:01:03 -04002467 .fsync = btrfs_sync_file,
Chris Masondee26a92007-03-26 16:00:06 -04002468};
Chris Masone20d96d2007-03-22 12:13:20 -04002469
Chris Mason2e635a22007-03-21 11:12:56 -04002470static int __init init_btrfs_fs(void)
2471{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002472 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04002473 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04002474 err = init_inodecache();
2475 if (err)
2476 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04002477 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2478 err = subsystem_register(&btrfs_subsys);
2479 if (err)
2480 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04002481 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002482out:
2483 destroy_inodecache();
2484 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002485}
2486
2487static void __exit exit_btrfs_fs(void)
2488{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002489 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04002490 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002491 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04002492 printk("btrfs unloaded\n");
2493}
2494
2495module_init(init_btrfs_fs)
2496module_exit(exit_btrfs_fs)
2497
2498MODULE_LICENSE("GPL");