blob: f49cad603ee8c807b2c37b41b4fb001c45e69acb [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 Masonc5739bb2007-04-10 09:27:04 -040021struct btrfs_iget_args {
22 u64 ino;
23 struct btrfs_root *root;
24};
25
Chris Mason2e635a22007-03-21 11:12:56 -040026#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040027
28static struct inode_operations btrfs_dir_inode_operations;
Chris Masond6e4a422007-04-06 15:37:36 -040029static struct inode_operations btrfs_dir_ro_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040030static struct super_operations btrfs_super_ops;
31static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040032static struct inode_operations btrfs_file_inode_operations;
33static struct address_space_operations btrfs_aops;
34static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040035
Chris Masone20d96d2007-03-22 12:13:20 -040036static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040037{
Chris Mason5caf2a02007-04-02 11:20:42 -040038 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct btrfs_inode_item *inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -040040 struct btrfs_root *root = BTRFS_I(inode)->root;
41 struct btrfs_key location;
Chris Mason31f3c992007-04-30 15:25:45 -040042 struct btrfs_block_group_cache *alloc_group;
43 u64 alloc_group_block;
Chris Masone20d96d2007-03-22 12:13:20 -040044 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040045
Chris Mason5caf2a02007-04-02 11:20:42 -040046 path = btrfs_alloc_path();
47 BUG_ON(!path);
48 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040049 mutex_lock(&root->fs_info->fs_mutex);
50
Chris Masond6e4a422007-04-06 15:37:36 -040051 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
52 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040053 if (ret) {
Chris Mason7cfcc172007-04-02 14:53:59 -040054 btrfs_free_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -040055 goto make_bad;
Chris Mason2e635a22007-03-21 11:12:56 -040056 }
Chris Mason5caf2a02007-04-02 11:20:42 -040057 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
58 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040059 struct btrfs_inode_item);
60
Chris Masone20d96d2007-03-22 12:13:20 -040061 inode->i_mode = btrfs_inode_mode(inode_item);
62 inode->i_nlink = btrfs_inode_nlink(inode_item);
63 inode->i_uid = btrfs_inode_uid(inode_item);
64 inode->i_gid = btrfs_inode_gid(inode_item);
65 inode->i_size = btrfs_inode_size(inode_item);
66 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
67 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
68 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
69 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
70 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
71 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
72 inode->i_blocks = btrfs_inode_nblocks(inode_item);
73 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason31f3c992007-04-30 15:25:45 -040074 alloc_group_block = btrfs_inode_block_group(inode_item);
75 ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
76 (void **)&alloc_group,
77 alloc_group_block, 1);
78 BUG_ON(!ret);
79 BTRFS_I(inode)->block_group = alloc_group;
Chris Mason5caf2a02007-04-02 11:20:42 -040080
Chris Mason5caf2a02007-04-02 11:20:42 -040081 btrfs_free_path(path);
82 inode_item = NULL;
83
Chris Masonf4b9aa82007-03-27 11:05:53 -040084 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b05da22007-04-10 12:13:09 -040085
Chris Masone20d96d2007-03-22 12:13:20 -040086 switch (inode->i_mode & S_IFMT) {
87#if 0
88 default:
89 init_special_inode(inode, inode->i_mode,
90 btrfs_inode_rdev(inode_item));
91 break;
92#endif
93 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040094 inode->i_mapping->a_ops = &btrfs_aops;
95 inode->i_fop = &btrfs_file_operations;
96 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040097 break;
98 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -040099 inode->i_fop = &btrfs_dir_file_operations;
Chris Masond6e4a422007-04-06 15:37:36 -0400100 if (root == root->fs_info->tree_root)
101 inode->i_op = &btrfs_dir_ro_inode_operations;
102 else
103 inode->i_op = &btrfs_dir_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400104 break;
105 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400106 // inode->i_op = &page_symlink_inode_operations;
107 break;
108 }
Chris Masone20d96d2007-03-22 12:13:20 -0400109 return;
Chris Masond6e4a422007-04-06 15:37:36 -0400110
111make_bad:
112 btrfs_release_path(root, path);
113 btrfs_free_path(path);
114 mutex_unlock(&root->fs_info->fs_mutex);
115 make_bad_inode(inode);
Chris Mason2e635a22007-03-21 11:12:56 -0400116}
117
Chris Masonf68cad02007-04-24 12:44:26 -0400118static void fill_inode_item(struct btrfs_inode_item *item,
119 struct inode *inode)
120{
121 btrfs_set_inode_uid(item, inode->i_uid);
122 btrfs_set_inode_gid(item, inode->i_gid);
123 btrfs_set_inode_size(item, inode->i_size);
124 btrfs_set_inode_mode(item, inode->i_mode);
125 btrfs_set_inode_nlink(item, inode->i_nlink);
126 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
127 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
128 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
129 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
130 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
131 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
132 btrfs_set_inode_nblocks(item, inode->i_blocks);
133 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason31f3c992007-04-30 15:25:45 -0400134 btrfs_set_inode_block_group(item,
135 BTRFS_I(inode)->block_group->key.objectid);
Chris Masonf68cad02007-04-24 12:44:26 -0400136}
137
Chris Masonf68cad02007-04-24 12:44:26 -0400138static int btrfs_update_inode(struct btrfs_trans_handle *trans,
139 struct btrfs_root *root,
140 struct inode *inode)
141{
142 struct btrfs_inode_item *inode_item;
143 struct btrfs_path *path;
144 int ret;
145
146 path = btrfs_alloc_path();
147 BUG_ON(!path);
148 btrfs_init_path(path);
149 ret = btrfs_lookup_inode(trans, root, path,
150 &BTRFS_I(inode)->location, 1);
151 if (ret) {
152 if (ret > 0)
153 ret = -ENOENT;
154 goto failed;
155 }
156
157 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
158 path->slots[0],
159 struct btrfs_inode_item);
160
161 fill_inode_item(inode_item, inode);
162 btrfs_mark_buffer_dirty(path->nodes[0]);
163 ret = 0;
164failed:
165 btrfs_release_path(root, path);
166 btrfs_free_path(path);
167 return ret;
168}
169
170
Chris Mason5f443fd2007-03-27 13:42:32 -0400171static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root,
173 struct inode *dir,
174 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400175{
Chris Mason5caf2a02007-04-02 11:20:42 -0400176 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400177 const char *name = dentry->d_name.name;
178 int name_len = dentry->d_name.len;
Chris Mason7e381802007-04-19 15:36:27 -0400179 int ret = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400180 u64 objectid;
181 struct btrfs_dir_item *di;
182
Chris Mason5caf2a02007-04-02 11:20:42 -0400183 path = btrfs_alloc_path();
184 BUG_ON(!path);
185 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400186 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400187 name, name_len, -1);
Chris Mason7e381802007-04-19 15:36:27 -0400188 if (IS_ERR(di)) {
189 ret = PTR_ERR(di);
Chris Mason134e9732007-03-25 13:44:56 -0400190 goto err;
Chris Mason7e381802007-04-19 15:36:27 -0400191 }
192 if (!di) {
Chris Mason134e9732007-03-25 13:44:56 -0400193 ret = -ENOENT;
194 goto err;
195 }
Chris Masond6e4a422007-04-06 15:37:36 -0400196 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason7e381802007-04-19 15:36:27 -0400197 ret = btrfs_delete_one_dir_name(trans, root, path, di);
198 BUG_ON(ret);
199 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400200
Chris Mason7e381802007-04-19 15:36:27 -0400201 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
202 objectid, name, name_len, -1);
203 if (IS_ERR(di)) {
204 ret = PTR_ERR(di);
205 goto err;
206 }
207 if (!di) {
208 ret = -ENOENT;
209 goto err;
210 }
211 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason134e9732007-03-25 13:44:56 -0400212 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400213
Chris Mason134e9732007-03-25 13:44:56 -0400214 dentry->d_inode->i_ctime = dir->i_ctime;
215err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400216 btrfs_free_path(path);
Chris Masonf68cad02007-04-24 12:44:26 -0400217 if (!ret) {
Chris Mason5f26f772007-04-05 10:38:44 -0400218 dir->i_size -= name_len * 2;
Chris Masonf68cad02007-04-24 12:44:26 -0400219 btrfs_update_inode(trans, root, dir);
220 drop_nlink(dentry->d_inode);
221 btrfs_update_inode(trans, root, dentry->d_inode);
Chris Masoncd1bc462007-04-27 10:08:34 -0400222 dir->i_sb->s_dirt = 1;
Chris Masond4dbff92007-04-04 14:08:15 -0400223 }
Chris Mason134e9732007-03-25 13:44:56 -0400224 return ret;
225}
226
Chris Mason5f443fd2007-03-27 13:42:32 -0400227static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
228{
229 struct btrfs_root *root;
230 struct btrfs_trans_handle *trans;
231 int ret;
232
Chris Masond6e4a422007-04-06 15:37:36 -0400233 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400234 mutex_lock(&root->fs_info->fs_mutex);
235 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400236 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f443fd2007-03-27 13:42:32 -0400237 ret = btrfs_unlink_trans(trans, root, dir, dentry);
238 btrfs_end_transaction(trans, root);
239 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400240 btrfs_btree_balance_dirty(root);
Chris Mason5f443fd2007-03-27 13:42:32 -0400241 return ret;
242}
243
244static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
245{
246 struct inode *inode = dentry->d_inode;
247 int err;
248 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400249 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400250 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400251 struct btrfs_key key;
252 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400253 struct btrfs_key found_key;
254 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400255 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400256 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400257
Chris Mason5caf2a02007-04-02 11:20:42 -0400258 path = btrfs_alloc_path();
259 BUG_ON(!path);
260 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400261 mutex_lock(&root->fs_info->fs_mutex);
262 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400263 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f443fd2007-03-27 13:42:32 -0400264 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);
Chris Mason35b7e472007-05-02 15:53:43 -0400314 btrfs_btree_balance_dirty(root);
Chris Mason5f443fd2007-03-27 13:42:32 -0400315 if (ret && !err)
316 err = ret;
317 return err;
318}
319
Chris Mason134e9732007-03-25 13:44:56 -0400320static int btrfs_free_inode(struct btrfs_trans_handle *trans,
321 struct btrfs_root *root,
322 struct inode *inode)
323{
Chris Mason5caf2a02007-04-02 11:20:42 -0400324 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400325 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400326
Chris Mason134e9732007-03-25 13:44:56 -0400327 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400328
329 path = btrfs_alloc_path();
330 BUG_ON(!path);
331 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400332 ret = btrfs_lookup_inode(trans, root, path,
333 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400334 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400335 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400336 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400337 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400338 return ret;
339}
340
Chris Masonde428b62007-05-18 13:28:27 -0400341static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
342 u64 objectid)
343{
344 struct btrfs_node *node;
345 int i;
346 int nritems;
347 u64 item_objectid;
348 u64 blocknr;
349 int slot;
350 int ret;
351
352 if (!path->nodes[1])
353 return;
354 node = btrfs_buffer_node(path->nodes[1]);
355 slot = path->slots[1];
356 if (slot == 0)
357 return;
358 nritems = btrfs_header_nritems(&node->header);
359 for (i = slot - 1; i >= 0; i--) {
360 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
361 if (item_objectid != objectid)
362 break;
363 blocknr = btrfs_node_blockptr(node, i);
364 ret = readahead_tree_block(root, blocknr);
365 if (ret)
366 break;
367 }
368}
369
Chris Masonf4b9aa82007-03-27 11:05:53 -0400370static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
371 struct btrfs_root *root,
372 struct inode *inode)
373{
374 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400376 struct btrfs_key key;
377 struct btrfs_disk_key *found_key;
Chris Masone06afa82007-05-23 15:44:28 -0400378 u32 found_type;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400379 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400380 struct btrfs_file_extent_item *fi = NULL;
381 u64 extent_start = 0;
382 u64 extent_num_blocks = 0;
383 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400384
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 path = btrfs_alloc_path();
386 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400387 /* FIXME, add redo link to tree so we don't leak on crash */
388 key.objectid = inode->i_ino;
389 key.offset = (u64)-1;
Chris Masone06afa82007-05-23 15:44:28 -0400390 key.flags = (u32)-1;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400391 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 btrfs_init_path(path);
393 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400394 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400395 goto error;
396 }
397 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 BUG_ON(path->slots[0] == 0);
399 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400400 }
Chris Masonde428b62007-05-18 13:28:27 -0400401 reada_truncate(root, path, inode->i_ino);
Chris Mason5caf2a02007-04-02 11:20:42 -0400402 leaf = btrfs_buffer_leaf(path->nodes[0]);
403 found_key = &leaf->items[path->slots[0]].key;
Chris Masone06afa82007-05-23 15:44:28 -0400404 found_type = btrfs_disk_key_type(found_key);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400405 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
406 break;
Chris Masone06afa82007-05-23 15:44:28 -0400407 if (found_type != BTRFS_CSUM_ITEM_KEY &&
408 found_type != BTRFS_DIR_ITEM_KEY &&
409 found_type != BTRFS_DIR_INDEX_KEY &&
410 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400411 break;
412 if (btrfs_disk_key_offset(found_key) < inode->i_size)
413 break;
Chris Mason236454d2007-04-19 13:37:44 -0400414 found_extent = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400415 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
417 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400418 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400419 if (btrfs_file_extent_type(fi) !=
420 BTRFS_FILE_EXTENT_INLINE) {
421 extent_start =
422 btrfs_file_extent_disk_blocknr(fi);
423 extent_num_blocks =
424 btrfs_file_extent_disk_num_blocks(fi);
425 /* FIXME blocksize != 4096 */
426 inode->i_blocks -=
427 btrfs_file_extent_num_blocks(fi) << 3;
428 found_extent = 1;
429 }
Chris Masonf254e522007-03-29 15:15:27 -0400430 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400432 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400433 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400434 if (found_extent) {
435 ret = btrfs_free_extent(trans, root, extent_start,
436 extent_num_blocks, 0);
437 BUG_ON(ret);
438 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400439 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400440 ret = 0;
441error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400442 btrfs_release_path(root, path);
443 btrfs_free_path(path);
Chris Masoncd1bc462007-04-27 10:08:34 -0400444 inode->i_sb->s_dirt = 1;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400445 return ret;
446}
447
Chris Mason134e9732007-03-25 13:44:56 -0400448static void btrfs_delete_inode(struct inode *inode)
449{
450 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400451 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400452 int ret;
453
Chris Mason134e9732007-03-25 13:44:56 -0400454 truncate_inode_pages(&inode->i_data, 0);
455 if (is_bad_inode(inode)) {
456 goto no_delete;
457 }
458 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400459 mutex_lock(&root->fs_info->fs_mutex);
460 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400461 btrfs_set_trans_block_group(trans, inode);
Chris Masone06afa82007-05-23 15:44:28 -0400462 ret = btrfs_truncate_in_trans(trans, root, inode);
463 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400464 btrfs_free_inode(trans, root, inode);
465 btrfs_end_transaction(trans, root);
466 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400467 btrfs_btree_balance_dirty(root);
Chris Mason134e9732007-03-25 13:44:56 -0400468 return;
469no_delete:
470 clear_inode(inode);
471}
472
Chris Masone20d96d2007-03-22 12:13:20 -0400473static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400474 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400475{
476 const char *name = dentry->d_name.name;
477 int namelen = dentry->d_name.len;
478 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400479 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400480 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400481 int ret;
482
Chris Mason5caf2a02007-04-02 11:20:42 -0400483 path = btrfs_alloc_path();
484 BUG_ON(!path);
485 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400486 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400487 namelen, 0);
Chris Mason7e381802007-04-19 15:36:27 -0400488 if (!di || IS_ERR(di)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400489 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400490 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400491 goto out;
492 }
Chris Masond6e4a422007-04-06 15:37:36 -0400493 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400494out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400495 btrfs_release_path(root, path);
496 btrfs_free_path(path);
Chris Masone20d96d2007-03-22 12:13:20 -0400497 return ret;
498}
499
Chris Mason35b7e472007-05-02 15:53:43 -0400500static int fixup_tree_root_location(struct btrfs_root *root,
Chris Masond6e4a422007-04-06 15:37:36 -0400501 struct btrfs_key *location,
502 struct btrfs_root **sub_root)
503{
504 struct btrfs_path *path;
505 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400506
507 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
508 return 0;
509 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
510 return 0;
511
512 path = btrfs_alloc_path();
513 BUG_ON(!path);
514 mutex_lock(&root->fs_info->fs_mutex);
515
Chris Mason0f7d52f2007-04-09 10:42:37 -0400516 *sub_root = btrfs_read_fs_root(root->fs_info, location);
517 if (IS_ERR(*sub_root))
518 return PTR_ERR(*sub_root);
519
520 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400521 location->objectid = btrfs_root_dirid(ri);
522 location->flags = 0;
523 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
524 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400525
Chris Masond6e4a422007-04-06 15:37:36 -0400526 btrfs_free_path(path);
527 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400528 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400529}
530
Chris Mason35b7e472007-05-02 15:53:43 -0400531static int btrfs_init_locked_inode(struct inode *inode, void *p)
Chris Masonc5739bb2007-04-10 09:27:04 -0400532{
533 struct btrfs_iget_args *args = p;
534 inode->i_ino = args->ino;
535 BTRFS_I(inode)->root = args->root;
536 return 0;
537}
538
Chris Mason35b7e472007-05-02 15:53:43 -0400539static int btrfs_find_actor(struct inode *inode, void *opaque)
Chris Masonc5739bb2007-04-10 09:27:04 -0400540{
541 struct btrfs_iget_args *args = opaque;
542 return (args->ino == inode->i_ino &&
543 args->root == BTRFS_I(inode)->root);
544}
545
Chris Mason35b7e472007-05-02 15:53:43 -0400546static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
547 struct btrfs_root *root)
Chris Masonc5739bb2007-04-10 09:27:04 -0400548{
549 struct inode *inode;
550 struct btrfs_iget_args args;
551 args.ino = objectid;
552 args.root = root;
553
554 inode = iget5_locked(s, objectid, btrfs_find_actor,
555 btrfs_init_locked_inode,
556 (void *)&args);
557 return inode;
558}
Chris Masond6e4a422007-04-06 15:37:36 -0400559
Chris Masone20d96d2007-03-22 12:13:20 -0400560static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
561 struct nameidata *nd)
562{
563 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400564 struct btrfs_inode *bi = BTRFS_I(dir);
565 struct btrfs_root *root = bi->root;
566 struct btrfs_root *sub_root = root;
567 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400568 int ret;
569
570 if (dentry->d_name.len > BTRFS_NAME_LEN)
571 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400572 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400573 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400574 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400575 if (ret < 0)
576 return ERR_PTR(ret);
577 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400578 if (location.objectid) {
579 ret = fixup_tree_root_location(root, &location, &sub_root);
580 if (ret < 0)
581 return ERR_PTR(ret);
582 if (ret > 0)
583 return ERR_PTR(-ENOENT);
Chris Masonc5739bb2007-04-10 09:27:04 -0400584 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
585 sub_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400586 if (!inode)
587 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400588 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400589 if (sub_root != root) {
Chris Masonc5739bb2007-04-10 09:27:04 -0400590printk("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 -0400591 igrab(inode);
592 sub_root->inode = inode;
593 }
Chris Masond6e4a422007-04-06 15:37:36 -0400594 BTRFS_I(inode)->root = sub_root;
595 memcpy(&BTRFS_I(inode)->location, &location,
596 sizeof(location));
597 btrfs_read_locked_inode(inode);
598 unlock_new_inode(inode);
599 }
Chris Masone20d96d2007-03-22 12:13:20 -0400600 }
601 return d_splice_alias(inode, dentry);
602}
603
Chris Masonde428b62007-05-18 13:28:27 -0400604static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
605 u64 objectid)
Chris Mason090d1872007-05-01 08:53:32 -0400606{
607 struct btrfs_node *node;
608 int i;
Chris Masonde428b62007-05-18 13:28:27 -0400609 u32 nritems;
Chris Mason090d1872007-05-01 08:53:32 -0400610 u64 item_objectid;
611 u64 blocknr;
612 int slot;
Chris Masonde428b62007-05-18 13:28:27 -0400613 int ret;
Chris Mason090d1872007-05-01 08:53:32 -0400614
615 if (!path->nodes[1])
616 return;
617 node = btrfs_buffer_node(path->nodes[1]);
618 slot = path->slots[1];
Chris Mason090d1872007-05-01 08:53:32 -0400619 nritems = btrfs_header_nritems(&node->header);
Chris Masonde428b62007-05-18 13:28:27 -0400620 for (i = slot + 1; i < nritems; i++) {
Chris Mason090d1872007-05-01 08:53:32 -0400621 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
622 if (item_objectid != objectid)
623 break;
624 blocknr = btrfs_node_blockptr(node, i);
Chris Masonde428b62007-05-18 13:28:27 -0400625 ret = readahead_tree_block(root, blocknr);
626 if (ret)
627 break;
Chris Mason090d1872007-05-01 08:53:32 -0400628 }
629}
630
Chris Masone20d96d2007-03-22 12:13:20 -0400631static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
632{
633 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400634 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400635 struct btrfs_item *item;
636 struct btrfs_dir_item *di;
637 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400638 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400639 int ret;
640 u32 nritems;
641 struct btrfs_leaf *leaf;
642 int slot;
643 int advance;
644 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400645 int over = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400646 u32 di_cur;
647 u32 di_total;
648 u32 di_len;
649 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Masond6e4a422007-04-06 15:37:36 -0400650
651 /* FIXME, use a real flag for deciding about the key type */
652 if (root->fs_info->tree_root == root)
653 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400654 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400655 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400656 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400657 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400658 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400659 path = btrfs_alloc_path();
660 btrfs_init_path(path);
661 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400662 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400663 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400664 advance = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400665 reada_leaves(root, path, inode->i_ino);
Chris Masone20d96d2007-03-22 12:13:20 -0400666 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400667 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400668 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400669 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400670 if (advance || slot >= nritems) {
671 if (slot >= nritems -1) {
Chris Masonde428b62007-05-18 13:28:27 -0400672 reada_leaves(root, path, inode->i_ino);
Chris Mason5caf2a02007-04-02 11:20:42 -0400673 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400674 if (ret)
675 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400676 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400677 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400678 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400679 } else {
680 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400681 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400682 }
683 }
684 advance = 1;
685 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400686 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
687 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400688 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masona429e512007-04-18 16:15:28 -0400689 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400690 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
691 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400692 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400693 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400694 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400695 di_cur = 0;
696 di_total = btrfs_item_size(leaf->items + slot);
697 while(di_cur < di_total) {
698 over = filldir(dirent, (const char *)(di + 1),
699 btrfs_dir_name_len(di),
700 btrfs_disk_key_offset(&item->key),
701 btrfs_disk_key_objectid(&di->location),
702 d_type);
703 if (over)
704 goto nopos;
705 di_len = btrfs_dir_name_len(di) + sizeof(*di);
706 di_cur += di_len;
707 di = (struct btrfs_dir_item *)((char *)di + di_len);
708 }
Chris Masone20d96d2007-03-22 12:13:20 -0400709 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400710 filp->f_pos++;
711nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400712 ret = 0;
713err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400716 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400717 return ret;
718}
719
720static void btrfs_put_super (struct super_block * sb)
721{
722 struct btrfs_root *root = btrfs_sb(sb);
723 int ret;
724
725 ret = close_ctree(root);
726 if (ret) {
727 printk("close ctree returns %d\n", ret);
728 }
729 sb->s_fs_info = NULL;
730}
Chris Mason2e635a22007-03-21 11:12:56 -0400731
732static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
733{
734 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400735 struct dentry * root_dentry;
736 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400737 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400738 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400739
740 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400741 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400742 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400743 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400744
Chris Mason0f7d52f2007-04-09 10:42:37 -0400745 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400746
Chris Mason0f7d52f2007-04-09 10:42:37 -0400747 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400748 printk("btrfs: open_ctree failed\n");
749 return -EIO;
750 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400751 sb->s_fs_info = tree_root;
752 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400753 printk("read in super total blocks %Lu root %Lu\n",
754 btrfs_super_total_blocks(disk_super),
755 btrfs_super_root_dir(disk_super));
756
Chris Masonc5739bb2007-04-10 09:27:04 -0400757 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
758 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400759 bi = BTRFS_I(inode);
760 bi->location.objectid = inode->i_ino;
761 bi->location.offset = 0;
762 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400764 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
765
Chris Mason2e635a22007-03-21 11:12:56 -0400766 if (!inode)
767 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400768 if (inode->i_state & I_NEW) {
769 btrfs_read_locked_inode(inode);
770 unlock_new_inode(inode);
771 }
Chris Mason2e635a22007-03-21 11:12:56 -0400772
Chris Masone20d96d2007-03-22 12:13:20 -0400773 root_dentry = d_alloc_root(inode);
774 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400775 iput(inode);
776 return -ENOMEM;
777 }
Chris Masone20d96d2007-03-22 12:13:20 -0400778 sb->s_root = root_dentry;
779
Chris Mason2e635a22007-03-21 11:12:56 -0400780 return 0;
781}
782
Chris Mason4730a4b2007-03-26 12:00:39 -0400783static int btrfs_write_inode(struct inode *inode, int wait)
784{
Chris Masond6e4a422007-04-06 15:37:36 -0400785 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400786 struct btrfs_trans_handle *trans;
Chris Masonb5133862007-04-24 11:52:22 -0400787 int ret = 0;
788
789 if (wait) {
790 mutex_lock(&root->fs_info->fs_mutex);
791 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400792 btrfs_set_trans_block_group(trans, inode);
Chris Masonb5133862007-04-24 11:52:22 -0400793 ret = btrfs_commit_transaction(trans, root);
794 mutex_unlock(&root->fs_info->fs_mutex);
795 }
796 return ret;
797}
798
799static void btrfs_dirty_inode(struct inode *inode)
800{
801 struct btrfs_root *root = BTRFS_I(inode)->root;
802 struct btrfs_trans_handle *trans;
Chris Mason4730a4b2007-03-26 12:00:39 -0400803
804 mutex_lock(&root->fs_info->fs_mutex);
805 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400806 btrfs_set_trans_block_group(trans, inode);
Chris Masonb5133862007-04-24 11:52:22 -0400807 btrfs_update_inode(trans, root, inode);
808 btrfs_end_transaction(trans, root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400809 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400810 btrfs_btree_balance_dirty(root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400811}
812
Chris Masond5719762007-03-23 10:01:08 -0400813static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400814 struct btrfs_root *root,
Chris Mason31f3c992007-04-30 15:25:45 -0400815 u64 objectid,
816 struct btrfs_block_group_cache *group,
817 int mode)
Chris Masond5719762007-03-23 10:01:08 -0400818{
819 struct inode *inode;
820 struct btrfs_inode_item inode_item;
Chris Mason1b05da22007-04-10 12:13:09 -0400821 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400822 int ret;
Chris Masonde428b62007-05-18 13:28:27 -0400823 int owner;
Chris Masond5719762007-03-23 10:01:08 -0400824
Chris Mason2619ba12007-04-10 16:58:11 -0400825 inode = new_inode(root->fs_info->sb);
Chris Masond5719762007-03-23 10:01:08 -0400826 if (!inode)
827 return ERR_PTR(-ENOMEM);
828
Chris Mason2619ba12007-04-10 16:58:11 -0400829 BTRFS_I(inode)->root = root;
Chris Masonde428b62007-05-18 13:28:27 -0400830 if (mode & S_IFDIR)
831 owner = 0;
832 else
833 owner = 1;
834 group = btrfs_find_block_group(root, group, 0, 0, owner);
Chris Mason31f3c992007-04-30 15:25:45 -0400835 BTRFS_I(inode)->block_group = group;
Chris Masond5719762007-03-23 10:01:08 -0400836
837 inode->i_uid = current->fsuid;
838 inode->i_gid = current->fsgid;
839 inode->i_mode = mode;
840 inode->i_ino = objectid;
841 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -0400842 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -0400843 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -0400844 location = &BTRFS_I(inode)->location;
845 location->objectid = objectid;
846 location->flags = 0;
847 location->offset = 0;
848 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -0400849
850 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
851 BUG_ON(ret);
852
853 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400854 return inode;
855}
856
857static int btrfs_add_link(struct btrfs_trans_handle *trans,
858 struct dentry *dentry, struct inode *inode)
859{
860 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400861 struct btrfs_key key;
862 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
863 key.objectid = inode->i_ino;
864 key.flags = 0;
865 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
866 key.offset = 0;
867
868 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400869 dentry->d_name.name, dentry->d_name.len,
870 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400871 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400872 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400873 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400874 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400875 dentry->d_parent->d_inode);
876 }
Chris Masond5719762007-03-23 10:01:08 -0400877 return ret;
878}
879
880static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
881 struct dentry *dentry, struct inode *inode)
882{
883 int err = btrfs_add_link(trans, dentry, inode);
884 if (!err) {
885 d_instantiate(dentry, inode);
886 return 0;
887 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400888 if (err > 0)
889 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -0400890 return err;
891}
892
893static int btrfs_create(struct inode *dir, struct dentry *dentry,
894 int mode, struct nameidata *nd)
895{
896 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400897 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400898 struct inode *inode;
899 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400900 int drop_inode = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400901 u64 objectid;
Chris Masond5719762007-03-23 10:01:08 -0400902
Chris Masond561c022007-03-23 19:47:49 -0400903 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400904 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400905 btrfs_set_trans_block_group(trans, dir);
Chris Mason2619ba12007-04-10 16:58:11 -0400906
907 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
908 if (err) {
909 err = -ENOSPC;
910 goto out_unlock;
911 }
912
Chris Mason31f3c992007-04-30 15:25:45 -0400913 inode = btrfs_new_inode(trans, root, objectid,
914 BTRFS_I(dir)->block_group, mode);
Chris Masond5719762007-03-23 10:01:08 -0400915 err = PTR_ERR(inode);
916 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400917 goto out_unlock;
Chris Mason31f3c992007-04-30 15:25:45 -0400918
919 btrfs_set_trans_block_group(trans, inode);
Chris Masond5719762007-03-23 10:01:08 -0400920 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400921 if (err)
922 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400923 else {
924 inode->i_mapping->a_ops = &btrfs_aops;
925 inode->i_fop = &btrfs_file_operations;
926 inode->i_op = &btrfs_file_inode_operations;
927 }
Chris Masond5719762007-03-23 10:01:08 -0400928 dir->i_sb->s_dirt = 1;
Chris Mason31f3c992007-04-30 15:25:45 -0400929 btrfs_update_inode_block_group(trans, inode);
930 btrfs_update_inode_block_group(trans, dir);
Chris Masond561c022007-03-23 19:47:49 -0400931out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400932 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400933 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400934
Chris Mason134e9732007-03-25 13:44:56 -0400935 if (drop_inode) {
936 inode_dec_link_count(inode);
937 iput(inode);
938 }
Chris Mason35b7e472007-05-02 15:53:43 -0400939 btrfs_btree_balance_dirty(root);
Chris Masond5719762007-03-23 10:01:08 -0400940 return err;
941}
942
Chris Masonf7922032007-03-25 20:17:36 -0400943static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400944 struct btrfs_root *root,
945 u64 objectid, u64 dirid)
Chris Masonf7922032007-03-25 20:17:36 -0400946{
Chris Masonf7922032007-03-25 20:17:36 -0400947 int ret;
948 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400949 struct btrfs_key key;
950
Chris Masonf7922032007-03-25 20:17:36 -0400951 buf[0] = '.';
952 buf[1] = '.';
953
Chris Mason2619ba12007-04-10 16:58:11 -0400954 key.objectid = objectid;
Chris Masond6e4a422007-04-06 15:37:36 -0400955 key.offset = 0;
956 key.flags = 0;
957 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
958
Chris Mason2619ba12007-04-10 16:58:11 -0400959 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400960 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400961 if (ret)
962 goto error;
Chris Mason2619ba12007-04-10 16:58:11 -0400963 key.objectid = dirid;
964 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400965 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400966 if (ret)
967 goto error;
Chris Masonf7922032007-03-25 20:17:36 -0400968error:
969 return ret;
970}
971
972static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
973{
974 struct inode *inode;
975 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400976 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400977 int err = 0;
978 int drop_on_err = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400979 u64 objectid;
Chris Masonf7922032007-03-25 20:17:36 -0400980
981 mutex_lock(&root->fs_info->fs_mutex);
982 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400983 btrfs_set_trans_block_group(trans, dir);
Chris Masonf7922032007-03-25 20:17:36 -0400984 if (IS_ERR(trans)) {
985 err = PTR_ERR(trans);
986 goto out_unlock;
987 }
Chris Mason2619ba12007-04-10 16:58:11 -0400988
989 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
990 if (err) {
991 err = -ENOSPC;
992 goto out_unlock;
993 }
994
Chris Mason31f3c992007-04-30 15:25:45 -0400995 inode = btrfs_new_inode(trans, root, objectid,
996 BTRFS_I(dir)->block_group, S_IFDIR | mode);
Chris Masonf7922032007-03-25 20:17:36 -0400997 if (IS_ERR(inode)) {
998 err = PTR_ERR(inode);
999 goto out_fail;
1000 }
1001 drop_on_err = 1;
1002 inode->i_op = &btrfs_dir_inode_operations;
1003 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason31f3c992007-04-30 15:25:45 -04001004 btrfs_set_trans_block_group(trans, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001005
Chris Mason2619ba12007-04-10 16:58:11 -04001006 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1007 if (err)
1008 goto out_fail;
1009
1010 inode->i_size = 6;
1011 err = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001012 if (err)
1013 goto out_fail;
1014 err = btrfs_add_link(trans, dentry, inode);
1015 if (err)
1016 goto out_fail;
1017 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001018 drop_on_err = 0;
Chris Masoncd1bc462007-04-27 10:08:34 -04001019 dir->i_sb->s_dirt = 1;
Chris Mason31f3c992007-04-30 15:25:45 -04001020 btrfs_update_inode_block_group(trans, inode);
1021 btrfs_update_inode_block_group(trans, dir);
Chris Masonf7922032007-03-25 20:17:36 -04001022
1023out_fail:
1024 btrfs_end_transaction(trans, root);
1025out_unlock:
1026 mutex_unlock(&root->fs_info->fs_mutex);
1027 if (drop_on_err)
1028 iput(inode);
Chris Mason35b7e472007-05-02 15:53:43 -04001029 btrfs_btree_balance_dirty(root);
Chris Masonf7922032007-03-25 20:17:36 -04001030 return err;
1031}
1032
Chris Mason8fd17792007-04-19 21:01:03 -04001033static int btrfs_sync_file(struct file *file,
1034 struct dentry *dentry, int datasync)
1035{
1036 struct inode *inode = dentry->d_inode;
1037 struct btrfs_root *root = BTRFS_I(inode)->root;
1038 int ret;
1039 struct btrfs_trans_handle *trans;
1040
1041 mutex_lock(&root->fs_info->fs_mutex);
1042 trans = btrfs_start_transaction(root, 1);
1043 if (!trans) {
1044 ret = -ENOMEM;
1045 goto out;
1046 }
1047 ret = btrfs_commit_transaction(trans, root);
1048 mutex_unlock(&root->fs_info->fs_mutex);
1049out:
1050 return ret > 0 ? EIO : ret;
1051}
1052
Chris Masond5719762007-03-23 10:01:08 -04001053static int btrfs_sync_fs(struct super_block *sb, int wait)
1054{
1055 struct btrfs_trans_handle *trans;
1056 struct btrfs_root *root;
1057 int ret;
Chris Masond98237b2007-03-28 13:57:48 -04001058 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -04001059
Chris Masond5719762007-03-23 10:01:08 -04001060 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -04001061 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -04001062 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -04001063 return 0;
1064 }
Chris Masond561c022007-03-23 19:47:49 -04001065 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001066 trans = btrfs_start_transaction(root, 1);
1067 ret = btrfs_commit_transaction(trans, root);
1068 sb->s_dirt = 0;
1069 BUG_ON(ret);
1070printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -04001071 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001072 return 0;
1073}
1074
Chris Mason75dfe392007-03-29 11:56:46 -04001075static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001076 struct buffer_head *result, int create)
1077{
1078 int ret;
1079 int err = 0;
1080 u64 blocknr;
1081 u64 extent_start = 0;
1082 u64 extent_end = 0;
1083 u64 objectid = inode->i_ino;
Chris Mason236454d2007-04-19 13:37:44 -04001084 u32 found_type;
Chris Mason5caf2a02007-04-02 11:20:42 -04001085 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001086 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001087 struct btrfs_file_extent_item *item;
1088 struct btrfs_leaf *leaf;
1089 struct btrfs_disk_key *found_key;
1090
Chris Mason5caf2a02007-04-02 11:20:42 -04001091 path = btrfs_alloc_path();
1092 BUG_ON(!path);
1093 btrfs_init_path(path);
Chris Mason6567e832007-04-16 09:22:45 -04001094 if (create) {
Chris Mason6567e832007-04-16 09:22:45 -04001095 WARN_ON(1);
1096 }
Chris Masondee26a92007-03-26 16:00:06 -04001097
Chris Mason236454d2007-04-19 13:37:44 -04001098 ret = btrfs_lookup_file_extent(NULL, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001099 inode->i_ino,
Chris Mason236454d2007-04-19 13:37:44 -04001100 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001101 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001102 err = ret;
1103 goto out;
1104 }
1105
1106 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001107 if (path->slots[0] == 0) {
1108 btrfs_release_path(root, path);
Chris Mason236454d2007-04-19 13:37:44 -04001109 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001110 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001111 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001112 }
1113
Chris Mason5caf2a02007-04-02 11:20:42 -04001114 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001115 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001116 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001117 blocknr = btrfs_file_extent_disk_blocknr(item);
1118 blocknr += btrfs_file_extent_offset(item);
1119
Chris Masondee26a92007-03-26 16:00:06 -04001120 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001121 found_key = &leaf->items[path->slots[0]].key;
Chris Mason236454d2007-04-19 13:37:44 -04001122 found_type = btrfs_disk_key_type(found_key);
Chris Masondee26a92007-03-26 16:00:06 -04001123 if (btrfs_disk_key_objectid(found_key) != objectid ||
Chris Mason236454d2007-04-19 13:37:44 -04001124 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masondee26a92007-03-26 16:00:06 -04001125 extent_end = 0;
1126 extent_start = 0;
Chris Mason236454d2007-04-19 13:37:44 -04001127 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001128 }
Chris Mason236454d2007-04-19 13:37:44 -04001129 found_type = btrfs_file_extent_type(item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001130 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason236454d2007-04-19 13:37:44 -04001131 if (found_type == BTRFS_FILE_EXTENT_REG) {
1132 extent_start = extent_start >> inode->i_blkbits;
1133 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1134 if (iblock >= extent_start && iblock < extent_end) {
1135 err = 0;
1136 btrfs_map_bh_to_logical(root, result, blocknr +
1137 iblock - extent_start);
1138 goto out;
1139 }
1140 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1141 char *ptr;
1142 char *map;
1143 u32 size;
1144 size = btrfs_file_extent_inline_len(leaf->items +
1145 path->slots[0]);
1146 extent_end = (extent_start + size) >> inode->i_blkbits;
1147 extent_start >>= inode->i_blkbits;
1148 if (iblock < extent_start || iblock > extent_end) {
1149 goto out;
1150 }
1151 ptr = btrfs_file_extent_inline_start(item);
1152 map = kmap(result->b_page);
1153 memcpy(map, ptr, size);
1154 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1155 flush_dcache_page(result->b_page);
1156 kunmap(result->b_page);
1157 set_buffer_uptodate(result);
1158 SetPageChecked(result->b_page);
1159 btrfs_map_bh_to_logical(root, result, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001160 }
Chris Masondee26a92007-03-26 16:00:06 -04001161out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001162 btrfs_free_path(path);
Chris Mason75dfe392007-03-29 11:56:46 -04001163 return err;
1164}
1165
1166static int btrfs_get_block(struct inode *inode, sector_t iblock,
1167 struct buffer_head *result, int create)
1168{
1169 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001170 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001171 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001172 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001173 mutex_unlock(&root->fs_info->fs_mutex);
1174 return err;
1175}
1176
1177static int btrfs_prepare_write(struct file *file, struct page *page,
1178 unsigned from, unsigned to)
1179{
Chris Mason6407bf62007-03-27 06:33:00 -04001180 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001181}
1182
Chris Masond561c022007-03-23 19:47:49 -04001183static void btrfs_write_super(struct super_block *sb)
1184{
1185 btrfs_sync_fs(sb, 1);
1186}
1187
Chris Masondee26a92007-03-26 16:00:06 -04001188static int btrfs_readpage(struct file *file, struct page *page)
1189{
1190 return mpage_readpage(page, btrfs_get_block);
1191}
1192
Chris Mason236454d2007-04-19 13:37:44 -04001193/*
1194 * While block_write_full_page is writing back the dirty buffers under
1195 * the page lock, whoever dirtied the buffers may decide to clean them
1196 * again at any time. We handle that by only looking at the buffer
1197 * state inside lock_buffer().
1198 *
1199 * If block_write_full_page() is called for regular writeback
1200 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1201 * locked buffer. This only can happen if someone has written the buffer
1202 * directly, with submit_bh(). At the address_space level PageWriteback
1203 * prevents this contention from occurring.
1204 */
1205static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1206 struct writeback_control *wbc)
1207{
1208 int err;
1209 sector_t block;
1210 sector_t last_block;
1211 struct buffer_head *bh, *head;
1212 const unsigned blocksize = 1 << inode->i_blkbits;
1213 int nr_underway = 0;
1214
1215 BUG_ON(!PageLocked(page));
1216
1217 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1218
1219 if (!page_has_buffers(page)) {
1220 create_empty_buffers(page, blocksize,
1221 (1 << BH_Dirty)|(1 << BH_Uptodate));
1222 }
1223
1224 /*
1225 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1226 * here, and the (potentially unmapped) buffers may become dirty at
1227 * any time. If a buffer becomes dirty here after we've inspected it
1228 * then we just miss that fact, and the page stays dirty.
1229 *
1230 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1231 * handle that here by just cleaning them.
1232 */
1233
1234 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1235 head = page_buffers(page);
1236 bh = head;
1237
1238 /*
1239 * Get all the dirty buffers mapped to disk addresses and
1240 * handle any aliases from the underlying blockdev's mapping.
1241 */
1242 do {
1243 if (block > last_block) {
1244 /*
1245 * mapped buffers outside i_size will occur, because
1246 * this page can be outside i_size when there is a
1247 * truncate in progress.
1248 */
1249 /*
1250 * The buffer was zeroed by block_write_full_page()
1251 */
1252 clear_buffer_dirty(bh);
1253 set_buffer_uptodate(bh);
1254 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1255 WARN_ON(bh->b_size != blocksize);
1256 err = btrfs_get_block(inode, block, bh, 0);
Chris Mason35b7e472007-05-02 15:53:43 -04001257 if (err) {
1258printk("writepage going to recovery err %d\n", err);
Chris Mason236454d2007-04-19 13:37:44 -04001259 goto recover;
Chris Mason35b7e472007-05-02 15:53:43 -04001260 }
Chris Mason236454d2007-04-19 13:37:44 -04001261 if (buffer_new(bh)) {
1262 /* blockdev mappings never come here */
1263 clear_buffer_new(bh);
Chris Mason236454d2007-04-19 13:37:44 -04001264 }
1265 }
1266 bh = bh->b_this_page;
1267 block++;
1268 } while (bh != head);
1269
1270 do {
1271 if (!buffer_mapped(bh))
1272 continue;
1273 /*
1274 * If it's a fully non-blocking write attempt and we cannot
1275 * lock the buffer then redirty the page. Note that this can
1276 * potentially cause a busy-wait loop from pdflush and kswapd
1277 * activity, but those code paths have their own higher-level
1278 * throttling.
1279 */
1280 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1281 lock_buffer(bh);
1282 } else if (test_set_buffer_locked(bh)) {
1283 redirty_page_for_writepage(wbc, page);
1284 continue;
1285 }
1286 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1287 mark_buffer_async_write(bh);
1288 } else {
1289 unlock_buffer(bh);
1290 }
1291 } while ((bh = bh->b_this_page) != head);
1292
1293 /*
1294 * The page and its buffers are protected by PageWriteback(), so we can
1295 * drop the bh refcounts early.
1296 */
1297 BUG_ON(PageWriteback(page));
1298 set_page_writeback(page);
1299
1300 do {
1301 struct buffer_head *next = bh->b_this_page;
1302 if (buffer_async_write(bh)) {
1303 submit_bh(WRITE, bh);
1304 nr_underway++;
1305 }
1306 bh = next;
1307 } while (bh != head);
1308 unlock_page(page);
1309
1310 err = 0;
1311done:
1312 if (nr_underway == 0) {
1313 /*
1314 * The page was marked dirty, but the buffers were
1315 * clean. Someone wrote them back by hand with
1316 * ll_rw_block/submit_bh. A rare case.
1317 */
1318 int uptodate = 1;
1319 do {
1320 if (!buffer_uptodate(bh)) {
1321 uptodate = 0;
1322 break;
1323 }
1324 bh = bh->b_this_page;
1325 } while (bh != head);
1326 if (uptodate)
1327 SetPageUptodate(page);
1328 end_page_writeback(page);
Chris Mason236454d2007-04-19 13:37:44 -04001329 }
1330 return err;
1331
1332recover:
1333 /*
1334 * ENOSPC, or some other error. We may already have added some
1335 * blocks to the file, so we need to write these out to avoid
1336 * exposing stale data.
1337 * The page is currently locked and not marked for writeback
1338 */
1339 bh = head;
1340 /* Recovery: lock and submit the mapped buffers */
1341 do {
1342 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1343 lock_buffer(bh);
1344 mark_buffer_async_write(bh);
1345 } else {
1346 /*
1347 * The buffer may have been set dirty during
1348 * attachment to a dirty page.
1349 */
1350 clear_buffer_dirty(bh);
1351 }
1352 } while ((bh = bh->b_this_page) != head);
1353 SetPageError(page);
1354 BUG_ON(PageWriteback(page));
1355 set_page_writeback(page);
1356 do {
1357 struct buffer_head *next = bh->b_this_page;
1358 if (buffer_async_write(bh)) {
1359 clear_buffer_dirty(bh);
1360 submit_bh(WRITE, bh);
1361 nr_underway++;
1362 }
1363 bh = next;
1364 } while (bh != head);
1365 unlock_page(page);
1366 goto done;
1367}
1368
1369/*
1370 * The generic ->writepage function for buffer-backed address_spaces
1371 */
Chris Masondee26a92007-03-26 16:00:06 -04001372static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1373{
Chris Mason236454d2007-04-19 13:37:44 -04001374 struct inode * const inode = page->mapping->host;
1375 loff_t i_size = i_size_read(inode);
1376 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1377 unsigned offset;
1378 void *kaddr;
1379
1380 /* Is the page fully inside i_size? */
1381 if (page->index < end_index)
1382 return __btrfs_write_full_page(inode, page, wbc);
1383
1384 /* Is the page fully outside i_size? (truncate in progress) */
1385 offset = i_size & (PAGE_CACHE_SIZE-1);
1386 if (page->index >= end_index+1 || !offset) {
1387 /*
1388 * The page may have dirty, unmapped buffers. For example,
1389 * they may have been added in ext3_writepage(). Make them
1390 * freeable here, so the page does not leak.
1391 */
1392 block_invalidatepage(page, 0);
1393 unlock_page(page);
1394 return 0; /* don't care */
1395 }
1396
1397 /*
1398 * The page straddles i_size. It must be zeroed out on each and every
1399 * writepage invokation because it may be mmapped. "A file is mapped
1400 * in multiples of the page size. For a file that is not a multiple of
1401 * the page size, the remaining memory is zeroed when mapped, and
1402 * writes to that region are not written out to the file."
1403 */
1404 kaddr = kmap_atomic(page, KM_USER0);
1405 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1406 flush_dcache_page(page);
1407 kunmap_atomic(kaddr, KM_USER0);
1408 return __btrfs_write_full_page(inode, page, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001409}
Chris Masond561c022007-03-23 19:47:49 -04001410
Chris Masonf4b9aa82007-03-27 11:05:53 -04001411static void btrfs_truncate(struct inode *inode)
1412{
Chris Masond6e4a422007-04-06 15:37:36 -04001413 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001414 int ret;
1415 struct btrfs_trans_handle *trans;
1416
1417 if (!S_ISREG(inode->i_mode))
1418 return;
1419 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1420 return;
1421
Chris Masone8f05c42007-04-04 14:30:09 -04001422 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001423
1424 /* FIXME, add redo link to tree so we don't leak on crash */
1425 mutex_lock(&root->fs_info->fs_mutex);
1426 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001427 btrfs_set_trans_block_group(trans, inode);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001428 ret = btrfs_truncate_in_trans(trans, root, inode);
1429 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001430 btrfs_update_inode(trans, root, inode);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001431 ret = btrfs_end_transaction(trans, root);
1432 BUG_ON(ret);
1433 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04001434 btrfs_btree_balance_dirty(root);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001435}
1436
Chris Mason236454d2007-04-19 13:37:44 -04001437/*
1438 * Make sure any changes to nobh_commit_write() are reflected in
1439 * nobh_truncate_page(), since it doesn't call commit_write().
1440 */
1441static int btrfs_commit_write(struct file *file, struct page *page,
1442 unsigned from, unsigned to)
1443{
1444 struct inode *inode = page->mapping->host;
1445 struct buffer_head *bh;
1446 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1447
1448 SetPageUptodate(page);
1449 bh = page_buffers(page);
1450 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1451 set_page_dirty(page);
1452 }
1453 if (pos > inode->i_size) {
1454 i_size_write(inode, pos);
1455 mark_inode_dirty(inode);
1456 }
1457 return 0;
1458}
1459
Chris Mason75dfe392007-03-29 11:56:46 -04001460static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1461 struct page **prepared_pages,
1462 const char __user * buf)
1463{
1464 long page_fault = 0;
1465 int i;
1466 int offset = pos & (PAGE_CACHE_SIZE - 1);
1467
1468 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1469 size_t count = min_t(size_t,
1470 PAGE_CACHE_SIZE - offset, write_bytes);
1471 struct page *page = prepared_pages[i];
1472 fault_in_pages_readable(buf, count);
1473
1474 /* Copy data from userspace to the current page */
1475 kmap(page);
1476 page_fault = __copy_from_user(page_address(page) + offset,
1477 buf, count);
1478 /* Flush processor's dcache for this page */
1479 flush_dcache_page(page);
1480 kunmap(page);
1481 buf += count;
1482 write_bytes -= count;
1483
1484 if (page_fault)
1485 break;
1486 }
1487 return page_fault ? -EFAULT : 0;
1488}
1489
1490static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1491{
1492 size_t i;
1493 for (i = 0; i < num_pages; i++) {
1494 if (!pages[i])
1495 break;
1496 unlock_page(pages[i]);
1497 mark_page_accessed(pages[i]);
1498 page_cache_release(pages[i]);
1499 }
1500}
1501static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1502 struct btrfs_root *root,
1503 struct file *file,
1504 struct page **pages,
1505 size_t num_pages,
1506 loff_t pos,
1507 size_t write_bytes)
1508{
1509 int i;
1510 int offset;
1511 int err = 0;
1512 int ret;
1513 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001514 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason236454d2007-04-19 13:37:44 -04001515 struct buffer_head *bh;
1516 struct btrfs_file_extent_item *ei;
Chris Mason75dfe392007-03-29 11:56:46 -04001517
1518 for (i = 0; i < num_pages; i++) {
1519 offset = pos & (PAGE_CACHE_SIZE -1);
1520 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001521 /* FIXME, one block at a time */
1522
1523 mutex_lock(&root->fs_info->fs_mutex);
1524 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001525 btrfs_set_trans_block_group(trans, inode);
Chris Mason236454d2007-04-19 13:37:44 -04001526
1527 bh = page_buffers(pages[i]);
1528 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1529 struct btrfs_key key;
1530 struct btrfs_path *path;
1531 char *ptr;
1532 u32 datasize;
1533
1534 path = btrfs_alloc_path();
1535 BUG_ON(!path);
1536 key.objectid = inode->i_ino;
1537 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1538 key.flags = 0;
1539 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1540 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1541 datasize = offset +
1542 btrfs_file_extent_calc_inline_size(write_bytes);
1543 ret = btrfs_insert_empty_item(trans, root, path, &key,
1544 datasize);
1545 BUG_ON(ret);
1546 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1547 path->slots[0], struct btrfs_file_extent_item);
1548 btrfs_set_file_extent_generation(ei, trans->transid);
1549 btrfs_set_file_extent_type(ei,
1550 BTRFS_FILE_EXTENT_INLINE);
1551 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason098f59c2007-05-11 11:33:21 -04001552 btrfs_memcpy(root, path->nodes[0]->b_data,
1553 ptr, bh->b_data, offset + write_bytes);
Chris Mason236454d2007-04-19 13:37:44 -04001554 mark_buffer_dirty(path->nodes[0]);
1555 btrfs_free_path(path);
1556 } else {
1557 btrfs_csum_file_block(trans, root, inode->i_ino,
Chris Masonf254e522007-03-29 15:15:27 -04001558 pages[i]->index << PAGE_CACHE_SHIFT,
1559 kmap(pages[i]), PAGE_CACHE_SIZE);
Chris Mason236454d2007-04-19 13:37:44 -04001560 kunmap(pages[i]);
1561 }
Chris Masonf254e522007-03-29 15:15:27 -04001562 SetPageChecked(pages[i]);
Chris Masone37c9e62007-05-09 20:13:14 -04001563 // btrfs_update_inode_block_group(trans, inode);
Chris Masonf254e522007-03-29 15:15:27 -04001564 ret = btrfs_end_transaction(trans, root);
1565 BUG_ON(ret);
1566 mutex_unlock(&root->fs_info->fs_mutex);
1567
Chris Mason236454d2007-04-19 13:37:44 -04001568 ret = btrfs_commit_write(file, pages[i], offset,
Chris Mason75dfe392007-03-29 11:56:46 -04001569 offset + this_write);
1570 pos += this_write;
1571 if (ret) {
1572 err = ret;
1573 goto failed;
1574 }
1575 WARN_ON(this_write > write_bytes);
1576 write_bytes -= this_write;
1577 }
1578failed:
1579 return err;
1580}
1581
Chris Masonb18c6682007-04-17 13:26:50 -04001582static int drop_extents(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct inode *inode,
Chris Masonde428b62007-05-18 13:28:27 -04001585 u64 start, u64 end, u64 *hint_block)
Chris Masonb18c6682007-04-17 13:26:50 -04001586{
1587 int ret;
1588 struct btrfs_key key;
1589 struct btrfs_leaf *leaf;
1590 int slot;
1591 struct btrfs_file_extent_item *extent;
Chris Mason236454d2007-04-19 13:37:44 -04001592 u64 extent_end = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001593 int keep;
1594 struct btrfs_file_extent_item old;
1595 struct btrfs_path *path;
1596 u64 search_start = start;
1597 int bookend;
Chris Mason236454d2007-04-19 13:37:44 -04001598 int found_type;
1599 int found_extent;
1600 int found_inline;
1601
Chris Masonb18c6682007-04-17 13:26:50 -04001602 path = btrfs_alloc_path();
1603 if (!path)
1604 return -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001605 while(1) {
Chris Masona429e512007-04-18 16:15:28 -04001606 btrfs_release_path(root, path);
1607 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1608 search_start, -1);
1609 if (ret < 0)
1610 goto out;
1611 if (ret > 0) {
1612 if (path->slots[0] == 0) {
Chris Mason236454d2007-04-19 13:37:44 -04001613 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001614 goto out;
1615 }
1616 path->slots[0]--;
1617 }
Chris Masonb18c6682007-04-17 13:26:50 -04001618 keep = 0;
1619 bookend = 0;
Chris Mason236454d2007-04-19 13:37:44 -04001620 found_extent = 0;
1621 found_inline = 0;
1622 extent = NULL;
Chris Masonb18c6682007-04-17 13:26:50 -04001623 leaf = btrfs_buffer_leaf(path->nodes[0]);
1624 slot = path->slots[0];
1625 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
Chris Masonb18c6682007-04-17 13:26:50 -04001626 if (key.offset >= end || key.objectid != inode->i_ino) {
1627 ret = 0;
1628 goto out;
1629 }
Chris Mason236454d2007-04-19 13:37:44 -04001630 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1631 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001632 goto out;
Chris Mason236454d2007-04-19 13:37:44 -04001633 }
1634 extent = btrfs_item_ptr(leaf, slot,
1635 struct btrfs_file_extent_item);
1636 found_type = btrfs_file_extent_type(extent);
1637 if (found_type == BTRFS_FILE_EXTENT_REG) {
1638 extent_end = key.offset +
1639 (btrfs_file_extent_num_blocks(extent) <<
1640 inode->i_blkbits);
1641 found_extent = 1;
1642 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1643 found_inline = 1;
1644 extent_end = key.offset +
1645 btrfs_file_extent_inline_len(leaf->items + slot);
1646 }
1647
1648 if (!found_extent && !found_inline) {
1649 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001650 goto out;
Chris Mason236454d2007-04-19 13:37:44 -04001651 }
1652
1653 if (search_start >= extent_end) {
1654 ret = 0;
1655 goto out;
1656 }
1657
Chris Masona429e512007-04-18 16:15:28 -04001658 search_start = extent_end;
Chris Masonb18c6682007-04-17 13:26:50 -04001659
1660 if (end < extent_end && end >= key.offset) {
Chris Mason236454d2007-04-19 13:37:44 -04001661 if (found_extent) {
1662 memcpy(&old, extent, sizeof(old));
1663 ret = btrfs_inc_extent_ref(trans, root,
1664 btrfs_file_extent_disk_blocknr(&old),
1665 btrfs_file_extent_disk_num_blocks(&old));
1666 BUG_ON(ret);
1667 }
1668 WARN_ON(found_inline);
Chris Masonb18c6682007-04-17 13:26:50 -04001669 bookend = 1;
1670 }
1671
1672 if (start > key.offset) {
1673 u64 new_num;
Chris Masona429e512007-04-18 16:15:28 -04001674 u64 old_num;
Chris Masonb18c6682007-04-17 13:26:50 -04001675 /* truncate existing extent */
1676 keep = 1;
1677 WARN_ON(start & (root->blocksize - 1));
Chris Mason236454d2007-04-19 13:37:44 -04001678 if (found_extent) {
1679 new_num = (start - key.offset) >>
1680 inode->i_blkbits;
1681 old_num = btrfs_file_extent_num_blocks(extent);
Chris Masonde428b62007-05-18 13:28:27 -04001682 *hint_block =
1683 btrfs_file_extent_disk_blocknr(extent);
Chris Mason236454d2007-04-19 13:37:44 -04001684 inode->i_blocks -= (old_num - new_num) << 3;
1685 btrfs_set_file_extent_num_blocks(extent,
1686 new_num);
1687 mark_buffer_dirty(path->nodes[0]);
1688 } else {
1689 WARN_ON(1);
Chris Mason236454d2007-04-19 13:37:44 -04001690 }
Chris Masonb18c6682007-04-17 13:26:50 -04001691 }
1692 if (!keep) {
Chris Mason236454d2007-04-19 13:37:44 -04001693 u64 disk_blocknr = 0;
1694 u64 disk_num_blocks = 0;
1695 u64 extent_num_blocks = 0;
1696 if (found_extent) {
1697 disk_blocknr =
1698 btrfs_file_extent_disk_blocknr(extent);
1699 disk_num_blocks =
1700 btrfs_file_extent_disk_num_blocks(extent);
1701 extent_num_blocks =
1702 btrfs_file_extent_num_blocks(extent);
Chris Masonde428b62007-05-18 13:28:27 -04001703 *hint_block =
1704 btrfs_file_extent_disk_blocknr(extent);
Chris Mason236454d2007-04-19 13:37:44 -04001705 }
Chris Masonb18c6682007-04-17 13:26:50 -04001706 ret = btrfs_del_item(trans, root, path);
1707 BUG_ON(ret);
1708 btrfs_release_path(root, path);
Chris Mason098f59c2007-05-11 11:33:21 -04001709 extent = NULL;
Chris Mason236454d2007-04-19 13:37:44 -04001710 if (found_extent) {
Chris Mason098f59c2007-05-11 11:33:21 -04001711 inode->i_blocks -= extent_num_blocks << 3;
Chris Mason236454d2007-04-19 13:37:44 -04001712 ret = btrfs_free_extent(trans, root,
1713 disk_blocknr,
1714 disk_num_blocks, 0);
1715 }
Chris Masonb18c6682007-04-17 13:26:50 -04001716
1717 BUG_ON(ret);
1718 if (!bookend && search_start >= end) {
1719 ret = 0;
1720 goto out;
1721 }
1722 if (!bookend)
Chris Masona429e512007-04-18 16:15:28 -04001723 continue;
Chris Masonb18c6682007-04-17 13:26:50 -04001724 }
Chris Mason236454d2007-04-19 13:37:44 -04001725 if (bookend && found_extent) {
Chris Masonb18c6682007-04-17 13:26:50 -04001726 /* create bookend */
1727 struct btrfs_key ins;
Chris Masonb18c6682007-04-17 13:26:50 -04001728 ins.objectid = inode->i_ino;
1729 ins.offset = end;
1730 ins.flags = 0;
1731 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1732
1733 btrfs_release_path(root, path);
Chris Masonb18c6682007-04-17 13:26:50 -04001734 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1735 sizeof(*extent));
1736 BUG_ON(ret);
1737 extent = btrfs_item_ptr(
1738 btrfs_buffer_leaf(path->nodes[0]),
1739 path->slots[0],
1740 struct btrfs_file_extent_item);
1741 btrfs_set_file_extent_disk_blocknr(extent,
1742 btrfs_file_extent_disk_blocknr(&old));
1743 btrfs_set_file_extent_disk_num_blocks(extent,
1744 btrfs_file_extent_disk_num_blocks(&old));
1745
1746 btrfs_set_file_extent_offset(extent,
1747 btrfs_file_extent_offset(&old) +
1748 ((end - key.offset) >> inode->i_blkbits));
1749 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1750 (end - key.offset) >> inode->i_blkbits);
1751 btrfs_set_file_extent_num_blocks(extent,
1752 btrfs_file_extent_num_blocks(&old) -
1753 ((end - key.offset) >> inode->i_blkbits));
1754
Chris Mason236454d2007-04-19 13:37:44 -04001755 btrfs_set_file_extent_type(extent,
1756 BTRFS_FILE_EXTENT_REG);
Chris Masonb18c6682007-04-17 13:26:50 -04001757 btrfs_set_file_extent_generation(extent,
1758 btrfs_file_extent_generation(&old));
Chris Masonb18c6682007-04-17 13:26:50 -04001759 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona429e512007-04-18 16:15:28 -04001760 inode->i_blocks +=
1761 btrfs_file_extent_num_blocks(extent) << 3;
Chris Masonb18c6682007-04-17 13:26:50 -04001762 ret = 0;
Chris Mason70b2bef2007-04-17 15:39:32 -04001763 goto out;
Chris Masonb18c6682007-04-17 13:26:50 -04001764 }
Chris Masonb18c6682007-04-17 13:26:50 -04001765 }
Chris Masonb18c6682007-04-17 13:26:50 -04001766out:
Chris Masonb18c6682007-04-17 13:26:50 -04001767 btrfs_free_path(path);
1768 return ret;
1769}
1770
1771static int prepare_pages(struct btrfs_root *root,
Chris Mason75dfe392007-03-29 11:56:46 -04001772 struct file *file,
1773 struct page **pages,
1774 size_t num_pages,
1775 loff_t pos,
Chris Mason2932f3e2007-04-10 14:22:02 -04001776 unsigned long first_index,
1777 unsigned long last_index,
Chris Mason6567e832007-04-16 09:22:45 -04001778 size_t write_bytes,
1779 u64 alloc_extent_start)
Chris Mason75dfe392007-03-29 11:56:46 -04001780{
1781 int i;
1782 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1783 struct inode *inode = file->f_path.dentry->d_inode;
1784 int offset;
1785 int err = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001786 int this_write;
Chris Mason6567e832007-04-16 09:22:45 -04001787 struct buffer_head *bh;
1788 struct buffer_head *head;
Chris Mason75dfe392007-03-29 11:56:46 -04001789 loff_t isize = i_size_read(inode);
1790
1791 memset(pages, 0, num_pages * sizeof(struct page *));
1792
1793 for (i = 0; i < num_pages; i++) {
1794 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1795 if (!pages[i]) {
1796 err = -ENOMEM;
1797 goto failed_release;
1798 }
Chris Mason35b7e472007-05-02 15:53:43 -04001799 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
1800 wait_on_page_writeback(pages[i]);
Chris Mason75dfe392007-03-29 11:56:46 -04001801 offset = pos & (PAGE_CACHE_SIZE -1);
1802 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Mason35b7e472007-05-02 15:53:43 -04001803 if (!page_has_buffers(pages[i])) {
1804 create_empty_buffers(pages[i],
1805 root->fs_info->sb->s_blocksize,
1806 (1 << BH_Uptodate));
1807 }
Chris Mason6567e832007-04-16 09:22:45 -04001808 head = page_buffers(pages[i]);
1809 bh = head;
1810 do {
1811 err = btrfs_map_bh_to_logical(root, bh,
1812 alloc_extent_start);
1813 BUG_ON(err);
1814 if (err)
1815 goto failed_truncate;
1816 bh = bh->b_this_page;
Chris Mason236454d2007-04-19 13:37:44 -04001817 if (alloc_extent_start)
1818 alloc_extent_start++;
Chris Mason6567e832007-04-16 09:22:45 -04001819 } while (bh != head);
Chris Mason75dfe392007-03-29 11:56:46 -04001820 pos += this_write;
Chris Mason75dfe392007-03-29 11:56:46 -04001821 WARN_ON(this_write > write_bytes);
1822 write_bytes -= this_write;
1823 }
1824 return 0;
1825
1826failed_release:
1827 btrfs_drop_pages(pages, num_pages);
1828 return err;
1829
1830failed_truncate:
1831 btrfs_drop_pages(pages, num_pages);
1832 if (pos > isize)
1833 vmtruncate(inode, isize);
1834 return err;
1835}
1836
1837static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1838 size_t count, loff_t *ppos)
1839{
1840 loff_t pos;
1841 size_t num_written = 0;
1842 int err = 0;
1843 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001844 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001845 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonb18c6682007-04-17 13:26:50 -04001846 struct page *pages[8];
Chris Mason35b7e472007-05-02 15:53:43 -04001847 struct page *pinned[2];
Chris Mason2932f3e2007-04-10 14:22:02 -04001848 unsigned long first_index;
1849 unsigned long last_index;
Chris Mason6567e832007-04-16 09:22:45 -04001850 u64 start_pos;
1851 u64 num_blocks;
1852 u64 alloc_extent_start;
Chris Masonde428b62007-05-18 13:28:27 -04001853 u64 hint_block;
Chris Mason6567e832007-04-16 09:22:45 -04001854 struct btrfs_trans_handle *trans;
Chris Masonb18c6682007-04-17 13:26:50 -04001855 struct btrfs_key ins;
Chris Mason35b7e472007-05-02 15:53:43 -04001856 pinned[0] = NULL;
1857 pinned[1] = NULL;
Chris Mason75dfe392007-03-29 11:56:46 -04001858 if (file->f_flags & O_DIRECT)
1859 return -EINVAL;
1860 pos = *ppos;
Chris Mason75dfe392007-03-29 11:56:46 -04001861 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1862 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1863 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1864 if (err)
1865 goto out;
1866 if (count == 0)
1867 goto out;
1868 err = remove_suid(file->f_path.dentry);
1869 if (err)
1870 goto out;
1871 file_update_time(file);
Chris Masona429e512007-04-18 16:15:28 -04001872
1873 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1874 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1875 inode->i_blkbits;
1876
Chris Mason75dfe392007-03-29 11:56:46 -04001877 mutex_lock(&inode->i_mutex);
Chris Mason2932f3e2007-04-10 14:22:02 -04001878 first_index = pos >> PAGE_CACHE_SHIFT;
1879 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
Chris Mason6567e832007-04-16 09:22:45 -04001880
Chris Masona429e512007-04-18 16:15:28 -04001881 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1882 (pos & (PAGE_CACHE_SIZE - 1))) {
1883 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1884 if (!PageUptodate(pinned[0])) {
1885 ret = mpage_readpage(pinned[0], btrfs_get_block);
1886 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001887 wait_on_page_locked(pinned[0]);
Chris Masona429e512007-04-18 16:15:28 -04001888 } else {
1889 unlock_page(pinned[0]);
1890 }
1891 }
1892 if (first_index != last_index &&
1893 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
Chris Masonde428b62007-05-18 13:28:27 -04001894 pos + count < inode->i_size &&
Chris Masona429e512007-04-18 16:15:28 -04001895 (count & (PAGE_CACHE_SIZE - 1))) {
1896 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1897 if (!PageUptodate(pinned[1])) {
1898 ret = mpage_readpage(pinned[1], btrfs_get_block);
1899 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001900 wait_on_page_locked(pinned[1]);
Chris Masona429e512007-04-18 16:15:28 -04001901 } else {
1902 unlock_page(pinned[1]);
1903 }
1904 }
1905
Chris Mason6567e832007-04-16 09:22:45 -04001906 mutex_lock(&root->fs_info->fs_mutex);
1907 trans = btrfs_start_transaction(root, 1);
1908 if (!trans) {
1909 err = -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001910 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -04001911 goto out_unlock;
1912 }
Chris Mason31f3c992007-04-30 15:25:45 -04001913 btrfs_set_trans_block_group(trans, inode);
Chris Masona429e512007-04-18 16:15:28 -04001914 /* FIXME blocksize != 4096 */
1915 inode->i_blocks += num_blocks << 3;
Chris Masonde428b62007-05-18 13:28:27 -04001916 hint_block = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001917 if (start_pos < inode->i_size) {
Chris Masona429e512007-04-18 16:15:28 -04001918 /* FIXME blocksize != pagesize */
Chris Masonb18c6682007-04-17 13:26:50 -04001919 ret = drop_extents(trans, root, inode,
1920 start_pos,
1921 (pos + count + root->blocksize -1) &
Chris Masonde428b62007-05-18 13:28:27 -04001922 ~((u64)root->blocksize - 1), &hint_block);
Chris Mason236454d2007-04-19 13:37:44 -04001923 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001924 }
Chris Mason236454d2007-04-19 13:37:44 -04001925 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
1926 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
Chris Mason4d775672007-04-20 20:23:12 -04001927 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
Chris Masonde428b62007-05-18 13:28:27 -04001928 num_blocks, hint_block, (u64)-1,
1929 &ins, 1);
Chris Mason236454d2007-04-19 13:37:44 -04001930 BUG_ON(ret);
1931 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
Chris Masonb18c6682007-04-17 13:26:50 -04001932 start_pos, ins.objectid, ins.offset);
Chris Mason236454d2007-04-19 13:37:44 -04001933 BUG_ON(ret);
1934 } else {
1935 ins.offset = 0;
1936 ins.objectid = 0;
1937 }
Chris Masonb18c6682007-04-17 13:26:50 -04001938 BUG_ON(ret);
1939 alloc_extent_start = ins.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001940 // btrfs_update_inode_block_group(trans, inode);
Chris Masonb18c6682007-04-17 13:26:50 -04001941 ret = btrfs_end_transaction(trans, root);
Chris Mason6567e832007-04-16 09:22:45 -04001942 mutex_unlock(&root->fs_info->fs_mutex);
1943
Chris Mason75dfe392007-03-29 11:56:46 -04001944 while(count > 0) {
1945 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1946 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1947 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1948 PAGE_CACHE_SHIFT;
Chris Masonb18c6682007-04-17 13:26:50 -04001949
1950 memset(pages, 0, sizeof(pages));
1951 ret = prepare_pages(root, file, pages, num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04001952 pos, first_index, last_index,
1953 write_bytes, alloc_extent_start);
Chris Mason75dfe392007-03-29 11:56:46 -04001954 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001955
Chris Mason6567e832007-04-16 09:22:45 -04001956 /* FIXME blocks != pagesize */
Chris Mason236454d2007-04-19 13:37:44 -04001957 if (alloc_extent_start)
1958 alloc_extent_start += num_pages;
Chris Mason75dfe392007-03-29 11:56:46 -04001959 ret = btrfs_copy_from_user(pos, num_pages,
1960 write_bytes, pages, buf);
1961 BUG_ON(ret);
1962
Chris Masonf254e522007-03-29 15:15:27 -04001963 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason70b2bef2007-04-17 15:39:32 -04001964 num_pages, pos, write_bytes);
Chris Mason75dfe392007-03-29 11:56:46 -04001965 BUG_ON(ret);
1966 btrfs_drop_pages(pages, num_pages);
1967
Chris Mason75dfe392007-03-29 11:56:46 -04001968 buf += write_bytes;
1969 count -= write_bytes;
1970 pos += write_bytes;
1971 num_written += write_bytes;
1972
1973 balance_dirty_pages_ratelimited(inode->i_mapping);
Chris Mason35b7e472007-05-02 15:53:43 -04001974 btrfs_btree_balance_dirty(root);
Chris Mason75dfe392007-03-29 11:56:46 -04001975 cond_resched();
1976 }
Chris Mason6567e832007-04-16 09:22:45 -04001977out_unlock:
Chris Mason75dfe392007-03-29 11:56:46 -04001978 mutex_unlock(&inode->i_mutex);
1979out:
Chris Masona429e512007-04-18 16:15:28 -04001980 if (pinned[0])
1981 page_cache_release(pinned[0]);
1982 if (pinned[1])
1983 page_cache_release(pinned[1]);
Chris Mason75dfe392007-03-29 11:56:46 -04001984 *ppos = pos;
1985 current->backing_dev_info = NULL;
Chris Masona429e512007-04-18 16:15:28 -04001986 mark_inode_dirty(inode);
Chris Mason75dfe392007-03-29 11:56:46 -04001987 return num_written ? num_written : err;
1988}
1989
Chris Masonf254e522007-03-29 15:15:27 -04001990static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1991 unsigned long offset, unsigned long size)
1992{
1993 char *kaddr;
1994 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001995 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001996
1997 if (size > count)
1998 size = count;
1999
2000 if (!PageChecked(page)) {
2001 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04002002 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason236454d2007-04-19 13:37:44 -04002003
Chris Masonf254e522007-03-29 15:15:27 -04002004 int ret = btrfs_csum_verify_file_block(root,
Chris Mason236454d2007-04-19 13:37:44 -04002005 page->mapping->host->i_ino,
2006 page->index << PAGE_CACHE_SHIFT,
2007 kmap(page), PAGE_CACHE_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -04002008 if (ret) {
2009 printk("failed to verify ino %lu page %lu\n",
2010 page->mapping->host->i_ino,
2011 page->index);
2012 memset(page_address(page), 0, PAGE_CACHE_SIZE);
2013 }
2014 SetPageChecked(page);
2015 kunmap(page);
2016 }
2017 /*
2018 * Faults on the destination of a read are common, so do it before
2019 * taking the kmap.
2020 */
2021 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2022 kaddr = kmap_atomic(page, KM_USER0);
2023 left = __copy_to_user_inatomic(desc->arg.buf,
2024 kaddr + offset, size);
2025 kunmap_atomic(kaddr, KM_USER0);
2026 if (left == 0)
2027 goto success;
2028 }
2029
2030 /* Do it the slow way */
2031 kaddr = kmap(page);
2032 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2033 kunmap(page);
2034
2035 if (left) {
2036 size -= left;
2037 desc->error = -EFAULT;
2038 }
2039success:
2040 desc->count = count - size;
2041 desc->written += size;
2042 desc->arg.buf += size;
2043 return size;
2044}
2045
2046/**
2047 * btrfs_file_aio_read - filesystem read routine
2048 * @iocb: kernel I/O control block
2049 * @iov: io vector request
2050 * @nr_segs: number of segments in the iovec
2051 * @pos: current file position
2052 */
2053static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2054 unsigned long nr_segs, loff_t pos)
2055{
2056 struct file *filp = iocb->ki_filp;
2057 ssize_t retval;
2058 unsigned long seg;
2059 size_t count;
2060 loff_t *ppos = &iocb->ki_pos;
2061
2062 count = 0;
2063 for (seg = 0; seg < nr_segs; seg++) {
2064 const struct iovec *iv = &iov[seg];
2065
2066 /*
2067 * If any segment has a negative length, or the cumulative
2068 * length ever wraps negative then return -EINVAL.
2069 */
2070 count += iv->iov_len;
2071 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2072 return -EINVAL;
2073 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2074 continue;
2075 if (seg == 0)
2076 return -EFAULT;
2077 nr_segs = seg;
2078 count -= iv->iov_len; /* This segment is no good */
2079 break;
2080 }
2081 retval = 0;
2082 if (count) {
2083 for (seg = 0; seg < nr_segs; seg++) {
2084 read_descriptor_t desc;
2085
2086 desc.written = 0;
2087 desc.arg.buf = iov[seg].iov_base;
2088 desc.count = iov[seg].iov_len;
2089 if (desc.count == 0)
2090 continue;
2091 desc.error = 0;
2092 do_generic_file_read(filp, ppos, &desc,
2093 btrfs_read_actor);
2094 retval += desc.written;
2095 if (desc.error) {
2096 retval = retval ?: desc.error;
2097 break;
2098 }
2099 }
2100 }
2101 return retval;
2102}
2103
Chris Mason2619ba12007-04-10 16:58:11 -04002104static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2105{
2106 struct btrfs_trans_handle *trans;
2107 struct btrfs_key key;
2108 struct btrfs_root_item root_item;
2109 struct btrfs_inode_item *inode_item;
2110 struct buffer_head *subvol;
2111 struct btrfs_leaf *leaf;
2112 struct btrfs_root *new_root;
2113 struct inode *inode;
Chris Mason31f3c992007-04-30 15:25:45 -04002114 struct inode *dir;
Chris Mason2619ba12007-04-10 16:58:11 -04002115 int ret;
2116 u64 objectid;
2117 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2118
2119 mutex_lock(&root->fs_info->fs_mutex);
2120 trans = btrfs_start_transaction(root, 1);
2121 BUG_ON(!trans);
2122
Chris Mason31f3c992007-04-30 15:25:45 -04002123 subvol = btrfs_alloc_free_block(trans, root, 0);
Chris Mason5e828492007-04-23 19:10:49 -04002124 if (subvol == NULL)
2125 return -ENOSPC;
Chris Mason2619ba12007-04-10 16:58:11 -04002126 leaf = btrfs_buffer_leaf(subvol);
2127 btrfs_set_header_nritems(&leaf->header, 0);
2128 btrfs_set_header_level(&leaf->header, 0);
Chris Mason7eccb902007-04-11 15:53:25 -04002129 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002130 btrfs_set_header_generation(&leaf->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04002131 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
Chris Mason2619ba12007-04-10 16:58:11 -04002132 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2133 sizeof(leaf->header.fsid));
Chris Mason4d775672007-04-20 20:23:12 -04002134 mark_buffer_dirty(subvol);
Chris Mason2619ba12007-04-10 16:58:11 -04002135
2136 inode_item = &root_item.inode;
2137 memset(inode_item, 0, sizeof(*inode_item));
2138 btrfs_set_inode_generation(inode_item, 1);
2139 btrfs_set_inode_size(inode_item, 3);
2140 btrfs_set_inode_nlink(inode_item, 1);
2141 btrfs_set_inode_nblocks(inode_item, 1);
2142 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2143
Chris Mason7eccb902007-04-11 15:53:25 -04002144 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002145 btrfs_set_root_refs(&root_item, 1);
Chris Mason5e828492007-04-23 19:10:49 -04002146 brelse(subvol);
2147 subvol = NULL;
Chris Mason2619ba12007-04-10 16:58:11 -04002148
Chris Mason2619ba12007-04-10 16:58:11 -04002149 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2150 0, &objectid);
2151 BUG_ON(ret);
2152
2153 btrfs_set_root_dirid(&root_item, new_dirid);
2154
2155 key.objectid = objectid;
2156 key.offset = 1;
2157 key.flags = 0;
2158 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2159 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2160 &root_item);
2161 BUG_ON(ret);
2162
2163 /*
2164 * insert the directory item
2165 */
2166 key.offset = (u64)-1;
Chris Mason31f3c992007-04-30 15:25:45 -04002167 dir = root->fs_info->sb->s_root->d_inode;
Chris Mason2619ba12007-04-10 16:58:11 -04002168 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Chris Mason31f3c992007-04-30 15:25:45 -04002169 name, namelen, dir->i_ino, &key, 0);
Chris Mason2619ba12007-04-10 16:58:11 -04002170 BUG_ON(ret);
2171
2172 ret = btrfs_commit_transaction(trans, root);
2173 BUG_ON(ret);
2174
2175 new_root = btrfs_read_fs_root(root->fs_info, &key);
2176 BUG_ON(!new_root);
2177
2178 trans = btrfs_start_transaction(new_root, 1);
2179 BUG_ON(!trans);
2180
Chris Mason31f3c992007-04-30 15:25:45 -04002181 inode = btrfs_new_inode(trans, new_root, new_dirid,
2182 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason2619ba12007-04-10 16:58:11 -04002183 inode->i_op = &btrfs_dir_inode_operations;
2184 inode->i_fop = &btrfs_dir_file_operations;
2185
2186 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2187 BUG_ON(ret);
2188
2189 inode->i_nlink = 1;
2190 inode->i_size = 6;
2191 ret = btrfs_update_inode(trans, new_root, inode);
2192 BUG_ON(ret);
2193
2194 ret = btrfs_commit_transaction(trans, new_root);
2195 BUG_ON(ret);
2196
2197 iput(inode);
2198
2199 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04002200 btrfs_btree_balance_dirty(root);
Chris Mason2619ba12007-04-10 16:58:11 -04002201 return 0;
2202}
2203
Chris Masonc5739bb2007-04-10 09:27:04 -04002204static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2205{
2206 struct btrfs_trans_handle *trans;
2207 struct btrfs_key key;
2208 struct btrfs_root_item new_root_item;
2209 int ret;
2210 u64 objectid;
2211
Chris Mason2619ba12007-04-10 16:58:11 -04002212 if (!root->ref_cows)
2213 return -EINVAL;
2214
Chris Masonc5739bb2007-04-10 09:27:04 -04002215 mutex_lock(&root->fs_info->fs_mutex);
2216 trans = btrfs_start_transaction(root, 1);
2217 BUG_ON(!trans);
2218
2219 ret = btrfs_update_inode(trans, root, root->inode);
2220 BUG_ON(ret);
2221
Chris Mason1b05da22007-04-10 12:13:09 -04002222 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2223 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04002224 BUG_ON(ret);
2225
Chris Masonc5739bb2007-04-10 09:27:04 -04002226 memcpy(&new_root_item, &root->root_item,
2227 sizeof(new_root_item));
2228
2229 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002230 key.offset = 1;
2231 key.flags = 0;
2232 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason7eccb902007-04-11 15:53:25 -04002233 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
Chris Masonc5739bb2007-04-10 09:27:04 -04002234
2235 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2236 &new_root_item);
2237 BUG_ON(ret);
2238
Chris Masonc5739bb2007-04-10 09:27:04 -04002239 /*
2240 * insert the directory item
2241 */
2242 key.offset = (u64)-1;
2243 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2244 name, namelen,
2245 root->fs_info->sb->s_root->d_inode->i_ino,
2246 &key, 0);
2247
2248 BUG_ON(ret);
2249
2250 ret = btrfs_inc_root_ref(trans, root);
2251 BUG_ON(ret);
2252
2253 ret = btrfs_commit_transaction(trans, root);
2254 BUG_ON(ret);
2255 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04002256 btrfs_btree_balance_dirty(root);
Chris Masonc5739bb2007-04-10 09:27:04 -04002257 return 0;
2258}
2259
Chris Mason8352d8a2007-04-12 10:43:05 -04002260static int add_disk(struct btrfs_root *root, char *name, int namelen)
2261{
2262 struct block_device *bdev;
2263 struct btrfs_path *path;
2264 struct super_block *sb = root->fs_info->sb;
2265 struct btrfs_root *dev_root = root->fs_info->dev_root;
2266 struct btrfs_trans_handle *trans;
2267 struct btrfs_device_item *dev_item;
2268 struct btrfs_key key;
2269 u16 item_size;
2270 u64 num_blocks;
2271 u64 new_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -04002272 u64 device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -04002273 int ret;
Chris Masonb4100d62007-04-12 12:14:00 -04002274
Chris Mason8352d8a2007-04-12 10:43:05 -04002275printk("adding disk %s\n", name);
2276 path = btrfs_alloc_path();
2277 if (!path)
2278 return -ENOMEM;
2279 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2280 bdev = open_bdev_excl(name, O_RDWR, sb);
2281 if (IS_ERR(bdev)) {
2282 ret = PTR_ERR(bdev);
2283printk("open bdev excl failed ret %d\n", ret);
2284 goto out_nolock;
2285 }
2286 set_blocksize(bdev, sb->s_blocksize);
2287 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2288 key.objectid = num_blocks;
2289 key.offset = new_blocks;
2290 key.flags = 0;
2291 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2292
2293 mutex_lock(&dev_root->fs_info->fs_mutex);
2294 trans = btrfs_start_transaction(dev_root, 1);
2295 item_size = sizeof(*dev_item) + namelen;
2296printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2297 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2298 if (ret) {
2299printk("insert failed %d\n", ret);
2300 close_bdev_excl(bdev);
2301 if (ret > 0)
2302 ret = -EEXIST;
2303 goto out;
2304 }
2305 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2306 path->slots[0], struct btrfs_device_item);
2307 btrfs_set_device_pathlen(dev_item, namelen);
2308 memcpy(dev_item + 1, name, namelen);
Chris Masonb4100d62007-04-12 12:14:00 -04002309
2310 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2311 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2312 btrfs_set_device_id(dev_item, device_id);
Chris Mason8352d8a2007-04-12 10:43:05 -04002313 mark_buffer_dirty(path->nodes[0]);
2314
Chris Masonb4100d62007-04-12 12:14:00 -04002315 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2316 new_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -04002317
2318 if (!ret) {
2319 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2320 num_blocks + new_blocks);
2321 i_size_write(root->fs_info->btree_inode,
2322 (num_blocks + new_blocks) <<
2323 root->fs_info->btree_inode->i_blkbits);
2324 }
2325
2326out:
2327 ret = btrfs_commit_transaction(trans, dev_root);
2328 BUG_ON(ret);
2329 mutex_unlock(&root->fs_info->fs_mutex);
2330out_nolock:
2331 btrfs_free_path(path);
Chris Mason35b7e472007-05-02 15:53:43 -04002332 btrfs_btree_balance_dirty(root);
Chris Mason8352d8a2007-04-12 10:43:05 -04002333
2334 return ret;
2335}
2336
Chris Masonc5739bb2007-04-10 09:27:04 -04002337static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2338 cmd, unsigned long arg)
2339{
2340 struct btrfs_root *root = BTRFS_I(inode)->root;
2341 struct btrfs_ioctl_vol_args vol_args;
Chris Mason8352d8a2007-04-12 10:43:05 -04002342 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -04002343 struct btrfs_dir_item *di;
Chris Masonc5739bb2007-04-10 09:27:04 -04002344 int namelen;
Chris Mason2619ba12007-04-10 16:58:11 -04002345 struct btrfs_path *path;
2346 u64 root_dirid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002347
Chris Masonc5739bb2007-04-10 09:27:04 -04002348 switch (cmd) {
2349 case BTRFS_IOC_SNAP_CREATE:
2350 if (copy_from_user(&vol_args,
2351 (struct btrfs_ioctl_vol_args __user *)arg,
2352 sizeof(vol_args)))
2353 return -EFAULT;
2354 namelen = strlen(vol_args.name);
2355 if (namelen > BTRFS_VOL_NAME_MAX)
2356 return -EINVAL;
Chris Mason2619ba12007-04-10 16:58:11 -04002357 path = btrfs_alloc_path();
2358 if (!path)
2359 return -ENOMEM;
Chris Mason2d13d8d2007-04-10 20:07:20 -04002360 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Chris Mason2619ba12007-04-10 16:58:11 -04002361 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason7e381802007-04-19 15:36:27 -04002362 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
Chris Mason2619ba12007-04-10 16:58:11 -04002363 path, root_dirid,
2364 vol_args.name, namelen, 0);
2365 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2d13d8d2007-04-10 20:07:20 -04002366 btrfs_free_path(path);
Chris Mason7e381802007-04-19 15:36:27 -04002367 if (di && !IS_ERR(di))
Chris Mason2619ba12007-04-10 16:58:11 -04002368 return -EEXIST;
2369
2370 if (root == root->fs_info->tree_root)
2371 ret = create_subvol(root, vol_args.name, namelen);
2372 else
2373 ret = create_snapshot(root, vol_args.name, namelen);
Chris Masonc5739bb2007-04-10 09:27:04 -04002374 WARN_ON(ret);
2375 break;
Chris Mason8352d8a2007-04-12 10:43:05 -04002376 case BTRFS_IOC_ADD_DISK:
2377 if (copy_from_user(&vol_args,
2378 (struct btrfs_ioctl_vol_args __user *)arg,
2379 sizeof(vol_args)))
2380 return -EFAULT;
2381 namelen = strlen(vol_args.name);
2382 if (namelen > BTRFS_VOL_NAME_MAX)
2383 return -EINVAL;
2384 vol_args.name[namelen] = '\0';
2385 ret = add_disk(root, vol_args.name, namelen);
2386 break;
Chris Masonc5739bb2007-04-10 09:27:04 -04002387 default:
2388 return -ENOTTY;
2389 }
Chris Mason8352d8a2007-04-12 10:43:05 -04002390 return ret;
Chris Masonc5739bb2007-04-10 09:27:04 -04002391}
2392
Chris Mason2c90e5d2007-04-02 10:50:19 -04002393static struct kmem_cache *btrfs_inode_cachep;
2394struct kmem_cache *btrfs_trans_handle_cachep;
2395struct kmem_cache *btrfs_transaction_cachep;
2396struct kmem_cache *btrfs_bit_radix_cachep;
2397struct kmem_cache *btrfs_path_cachep;
2398
2399/*
2400 * Called inside transaction, so use GFP_NOFS
2401 */
2402static struct inode *btrfs_alloc_inode(struct super_block *sb)
2403{
2404 struct btrfs_inode *ei;
2405
2406 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2407 if (!ei)
2408 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002409 return &ei->vfs_inode;
2410}
2411
2412static void btrfs_destroy_inode(struct inode *inode)
2413{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002414 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04002415 WARN_ON(inode->i_data.nrpages);
2416
Chris Mason2c90e5d2007-04-02 10:50:19 -04002417 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2418}
2419
2420static void init_once(void * foo, struct kmem_cache * cachep,
2421 unsigned long flags)
2422{
2423 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2424
Chris Masonf9f3c6b2007-05-21 14:05:12 -04002425 if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
Chris Mason2c90e5d2007-04-02 10:50:19 -04002426 SLAB_CTOR_CONSTRUCTOR) {
2427 inode_init_once(&ei->vfs_inode);
2428 }
2429}
2430
2431static int init_inodecache(void)
2432{
2433 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2434 sizeof(struct btrfs_inode),
2435 0, (SLAB_RECLAIM_ACCOUNT|
2436 SLAB_MEM_SPREAD),
2437 init_once, NULL);
2438 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2439 sizeof(struct btrfs_trans_handle),
2440 0, (SLAB_RECLAIM_ACCOUNT|
2441 SLAB_MEM_SPREAD),
2442 NULL, NULL);
2443 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2444 sizeof(struct btrfs_transaction),
2445 0, (SLAB_RECLAIM_ACCOUNT|
2446 SLAB_MEM_SPREAD),
2447 NULL, NULL);
2448 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2449 sizeof(struct btrfs_transaction),
2450 0, (SLAB_RECLAIM_ACCOUNT|
2451 SLAB_MEM_SPREAD),
2452 NULL, NULL);
2453 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2454 256,
2455 0, (SLAB_RECLAIM_ACCOUNT|
2456 SLAB_MEM_SPREAD |
2457 SLAB_DESTROY_BY_RCU),
2458 NULL, NULL);
2459 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2460 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2461 return -ENOMEM;
2462 return 0;
2463}
2464
2465static void destroy_inodecache(void)
2466{
2467 kmem_cache_destroy(btrfs_inode_cachep);
2468 kmem_cache_destroy(btrfs_trans_handle_cachep);
2469 kmem_cache_destroy(btrfs_transaction_cachep);
2470 kmem_cache_destroy(btrfs_bit_radix_cachep);
2471 kmem_cache_destroy(btrfs_path_cachep);
2472}
2473
Chris Mason2e635a22007-03-21 11:12:56 -04002474static int btrfs_get_sb(struct file_system_type *fs_type,
2475 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2476{
2477 return get_sb_bdev(fs_type, flags, dev_name, data,
2478 btrfs_fill_super, mnt);
2479}
2480
Chris Mason236454d2007-04-19 13:37:44 -04002481static int btrfs_getattr(struct vfsmount *mnt,
2482 struct dentry *dentry, struct kstat *stat)
2483{
2484 struct inode *inode = dentry->d_inode;
2485 generic_fillattr(inode, stat);
2486 stat->blksize = 256 * 1024;
2487 return 0;
2488}
2489
Chris Mason8fd17792007-04-19 21:01:03 -04002490static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2491{
2492 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2493 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2494
2495 buf->f_namelen = BTRFS_NAME_LEN;
2496 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2497 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2498 buf->f_bavail = buf->f_bfree;
2499 buf->f_bsize = dentry->d_sb->s_blocksize;
2500 buf->f_type = BTRFS_SUPER_MAGIC;
2501 return 0;
2502}
Chris Masonb5133862007-04-24 11:52:22 -04002503
Chris Masone06afa82007-05-23 15:44:28 -04002504static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2505 struct inode * new_dir,struct dentry *new_dentry)
2506{
2507 struct btrfs_trans_handle *trans;
2508 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2509 struct inode *new_inode = new_dentry->d_inode;
2510 struct inode *old_inode = old_dentry->d_inode;
2511 struct timespec ctime = CURRENT_TIME;
2512 struct btrfs_path *path;
2513 struct btrfs_dir_item *di;
2514 int ret;
2515
2516 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2517 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2518 return -ENOTEMPTY;
2519 }
2520 mutex_lock(&root->fs_info->fs_mutex);
2521 trans = btrfs_start_transaction(root, 1);
2522 btrfs_set_trans_block_group(trans, new_dir);
2523 path = btrfs_alloc_path();
2524 if (!path) {
2525 ret = -ENOMEM;
2526 goto out_fail;
2527 }
2528
2529 old_dentry->d_inode->i_nlink++;
2530 old_dir->i_ctime = old_dir->i_mtime = ctime;
2531 new_dir->i_ctime = new_dir->i_mtime = ctime;
2532 old_inode->i_ctime = ctime;
2533 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2534 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2535 u64 old_parent_oid;
2536 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2537 "..", 2, -1);
2538 if (IS_ERR(di)) {
2539 ret = PTR_ERR(di);
2540 goto out_fail;
2541 }
2542 if (!di) {
2543 ret = -ENOENT;
2544 goto out_fail;
2545 }
2546 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2547 ret = btrfs_del_item(trans, root, path);
2548 if (ret) {
2549 ret = -EIO;
2550 goto out_fail;
2551 }
2552 btrfs_release_path(root, path);
2553
2554 di = btrfs_lookup_dir_index_item(trans, root, path,
2555 old_inode->i_ino,
2556 old_parent_oid,
2557 "..", 2, -1);
2558 if (IS_ERR(di)) {
2559 ret = PTR_ERR(di);
2560 goto out_fail;
2561 }
2562 if (!di) {
2563 ret = -ENOENT;
2564 goto out_fail;
2565 }
2566 ret = btrfs_del_item(trans, root, path);
2567 if (ret) {
2568 ret = -EIO;
2569 goto out_fail;
2570 }
2571 btrfs_release_path(root, path);
2572
2573 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2574 old_inode->i_ino, location, 0);
2575 if (ret)
2576 goto out_fail;
2577 }
2578
2579
2580 ret = btrfs_add_link(trans, new_dentry, old_inode);
2581 if (ret == -EEXIST && new_inode)
2582 ret = 0;
2583 else if (ret)
2584 goto out_fail;
2585
2586 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2587 if (ret)
2588 goto out_fail;
2589
2590 if (new_inode) {
2591 new_inode->i_ctime = CURRENT_TIME;
2592 di = btrfs_lookup_dir_index_item(trans, root, path,
2593 new_dir->i_ino,
2594 new_inode->i_ino,
2595 new_dentry->d_name.name,
2596 new_dentry->d_name.len, -1);
2597 if (di && !IS_ERR(di)) {
2598 btrfs_del_item(trans, root, path);
2599 btrfs_release_path(root, path);
2600 }
2601 if (S_ISDIR(new_inode->i_mode))
2602 clear_nlink(new_inode);
2603 else
2604 drop_nlink(new_inode);
2605 btrfs_update_inode(trans, root, new_inode);
2606 }
2607out_fail:
2608 btrfs_free_path(path);
2609 btrfs_end_transaction(trans, root);
2610 mutex_unlock(&root->fs_info->fs_mutex);
2611 return ret;
2612}
2613
Chris Mason2e635a22007-03-21 11:12:56 -04002614static struct file_system_type btrfs_fs_type = {
2615 .owner = THIS_MODULE,
2616 .name = "btrfs",
2617 .get_sb = btrfs_get_sb,
2618 .kill_sb = kill_block_super,
2619 .fs_flags = FS_REQUIRES_DEV,
2620};
2621
Chris Masone20d96d2007-03-22 12:13:20 -04002622static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -04002623 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002624 .put_super = btrfs_put_super,
2625 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04002626 .write_super = btrfs_write_super,
2627 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04002628 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -04002629 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002630 .alloc_inode = btrfs_alloc_inode,
2631 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -04002632 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -04002633};
2634
2635static struct inode_operations btrfs_dir_inode_operations = {
2636 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04002637 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04002638 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04002639 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04002640 .rmdir = btrfs_rmdir,
Chris Masone06afa82007-05-23 15:44:28 -04002641 .rename = btrfs_rename,
Chris Masone20d96d2007-03-22 12:13:20 -04002642};
2643
Chris Masond6e4a422007-04-06 15:37:36 -04002644static struct inode_operations btrfs_dir_ro_inode_operations = {
2645 .lookup = btrfs_lookup,
2646};
2647
Chris Masone20d96d2007-03-22 12:13:20 -04002648static struct file_operations btrfs_dir_file_operations = {
2649 .llseek = generic_file_llseek,
2650 .read = generic_read_dir,
2651 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04002652 .ioctl = btrfs_ioctl,
Chris Masone20d96d2007-03-22 12:13:20 -04002653};
2654
Chris Masondee26a92007-03-26 16:00:06 -04002655static struct address_space_operations btrfs_aops = {
2656 .readpage = btrfs_readpage,
Chris Masondee26a92007-03-26 16:00:06 -04002657 .writepage = btrfs_writepage,
2658 .sync_page = block_sync_page,
2659 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04002660 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04002661};
2662
2663static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04002664 .truncate = btrfs_truncate,
Chris Mason236454d2007-04-19 13:37:44 -04002665 .getattr = btrfs_getattr,
Chris Masondee26a92007-03-26 16:00:06 -04002666};
2667
2668static struct file_operations btrfs_file_operations = {
2669 .llseek = generic_file_llseek,
2670 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04002671 .aio_read = btrfs_file_aio_read,
2672 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04002673 .mmap = generic_file_mmap,
2674 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04002675 .ioctl = btrfs_ioctl,
Chris Mason8fd17792007-04-19 21:01:03 -04002676 .fsync = btrfs_sync_file,
Chris Masondee26a92007-03-26 16:00:06 -04002677};
Chris Masone20d96d2007-03-22 12:13:20 -04002678
Chris Mason2e635a22007-03-21 11:12:56 -04002679static int __init init_btrfs_fs(void)
2680{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002681 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04002682 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04002683 err = init_inodecache();
2684 if (err)
2685 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002686 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002687 destroy_inodecache();
2688 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002689}
2690
2691static void __exit exit_btrfs_fs(void)
2692{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002693 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04002694 unregister_filesystem(&btrfs_fs_type);
Chris Mason2e635a22007-03-21 11:12:56 -04002695 printk("btrfs unloaded\n");
2696}
2697
2698module_init(init_btrfs_fs)
2699module_exit(exit_btrfs_fs)
2700
2701MODULE_LICENSE("GPL");