blob: 0f79490123cf66384bdf2f094d167a345eb29e2d [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 Mason35b7e472007-05-02 15:53:43 -040021static void btrfs_fsinfo_release(struct kobject *obj)
Chris Masond6e4a422007-04-06 15:37:36 -040022{
23 struct btrfs_fs_info *fsinfo = container_of(obj,
24 struct btrfs_fs_info, kobj);
25 kfree(fsinfo);
26}
27
Chris Mason35b7e472007-05-02 15:53:43 -040028static struct kobj_type btrfs_fsinfo_ktype = {
Chris Masond6e4a422007-04-06 15:37:36 -040029 .release = btrfs_fsinfo_release,
30};
31
Chris Masonc5739bb2007-04-10 09:27:04 -040032struct btrfs_iget_args {
33 u64 ino;
34 struct btrfs_root *root;
35};
36
Chris Masond6e4a422007-04-06 15:37:36 -040037decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
38
Chris Mason2e635a22007-03-21 11:12:56 -040039#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040040
41static struct inode_operations btrfs_dir_inode_operations;
Chris Masond6e4a422007-04-06 15:37:36 -040042static struct inode_operations btrfs_dir_ro_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040043static struct super_operations btrfs_super_ops;
44static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040045static struct inode_operations btrfs_file_inode_operations;
46static struct address_space_operations btrfs_aops;
47static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040048
Chris Masone20d96d2007-03-22 12:13:20 -040049static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040050{
Chris Mason5caf2a02007-04-02 11:20:42 -040051 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040052 struct btrfs_inode_item *inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -040053 struct btrfs_root *root = BTRFS_I(inode)->root;
54 struct btrfs_key location;
Chris Mason31f3c992007-04-30 15:25:45 -040055 struct btrfs_block_group_cache *alloc_group;
56 u64 alloc_group_block;
Chris Masone20d96d2007-03-22 12:13:20 -040057 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040058
Chris Mason5caf2a02007-04-02 11:20:42 -040059 path = btrfs_alloc_path();
60 BUG_ON(!path);
61 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040062 mutex_lock(&root->fs_info->fs_mutex);
63
Chris Masond6e4a422007-04-06 15:37:36 -040064 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
65 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040066 if (ret) {
Chris Mason7cfcc172007-04-02 14:53:59 -040067 btrfs_free_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -040068 goto make_bad;
Chris Mason2e635a22007-03-21 11:12:56 -040069 }
Chris Mason5caf2a02007-04-02 11:20:42 -040070 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
71 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040072 struct btrfs_inode_item);
73
Chris Masone20d96d2007-03-22 12:13:20 -040074 inode->i_mode = btrfs_inode_mode(inode_item);
75 inode->i_nlink = btrfs_inode_nlink(inode_item);
76 inode->i_uid = btrfs_inode_uid(inode_item);
77 inode->i_gid = btrfs_inode_gid(inode_item);
78 inode->i_size = btrfs_inode_size(inode_item);
79 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
80 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
81 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
82 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
83 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
84 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
85 inode->i_blocks = btrfs_inode_nblocks(inode_item);
86 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason31f3c992007-04-30 15:25:45 -040087 alloc_group_block = btrfs_inode_block_group(inode_item);
88 ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
89 (void **)&alloc_group,
90 alloc_group_block, 1);
91 BUG_ON(!ret);
92 BTRFS_I(inode)->block_group = alloc_group;
Chris Mason5caf2a02007-04-02 11:20:42 -040093
Chris Mason5caf2a02007-04-02 11:20:42 -040094 btrfs_free_path(path);
95 inode_item = NULL;
96
Chris Masonf4b9aa82007-03-27 11:05:53 -040097 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b05da22007-04-10 12:13:09 -040098
Chris Masone20d96d2007-03-22 12:13:20 -040099 switch (inode->i_mode & S_IFMT) {
100#if 0
101 default:
102 init_special_inode(inode, inode->i_mode,
103 btrfs_inode_rdev(inode_item));
104 break;
105#endif
106 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -0400107 inode->i_mapping->a_ops = &btrfs_aops;
108 inode->i_fop = &btrfs_file_operations;
109 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400110 break;
111 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400112 inode->i_fop = &btrfs_dir_file_operations;
Chris Masond6e4a422007-04-06 15:37:36 -0400113 if (root == root->fs_info->tree_root)
114 inode->i_op = &btrfs_dir_ro_inode_operations;
115 else
116 inode->i_op = &btrfs_dir_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400117 break;
118 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400119 // inode->i_op = &page_symlink_inode_operations;
120 break;
121 }
Chris Masone20d96d2007-03-22 12:13:20 -0400122 return;
Chris Masond6e4a422007-04-06 15:37:36 -0400123
124make_bad:
125 btrfs_release_path(root, path);
126 btrfs_free_path(path);
127 mutex_unlock(&root->fs_info->fs_mutex);
128 make_bad_inode(inode);
Chris Mason2e635a22007-03-21 11:12:56 -0400129}
130
Chris Masonf68cad02007-04-24 12:44:26 -0400131static void fill_inode_item(struct btrfs_inode_item *item,
132 struct inode *inode)
133{
134 btrfs_set_inode_uid(item, inode->i_uid);
135 btrfs_set_inode_gid(item, inode->i_gid);
136 btrfs_set_inode_size(item, inode->i_size);
137 btrfs_set_inode_mode(item, inode->i_mode);
138 btrfs_set_inode_nlink(item, inode->i_nlink);
139 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
140 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
141 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
142 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
143 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
144 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
145 btrfs_set_inode_nblocks(item, inode->i_blocks);
146 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason31f3c992007-04-30 15:25:45 -0400147 btrfs_set_inode_block_group(item,
148 BTRFS_I(inode)->block_group->key.objectid);
Chris Masonf68cad02007-04-24 12:44:26 -0400149}
150
Chris Masonf68cad02007-04-24 12:44:26 -0400151static int btrfs_update_inode(struct btrfs_trans_handle *trans,
152 struct btrfs_root *root,
153 struct inode *inode)
154{
155 struct btrfs_inode_item *inode_item;
156 struct btrfs_path *path;
157 int ret;
158
159 path = btrfs_alloc_path();
160 BUG_ON(!path);
161 btrfs_init_path(path);
162 ret = btrfs_lookup_inode(trans, root, path,
163 &BTRFS_I(inode)->location, 1);
164 if (ret) {
165 if (ret > 0)
166 ret = -ENOENT;
167 goto failed;
168 }
169
170 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
171 path->slots[0],
172 struct btrfs_inode_item);
173
174 fill_inode_item(inode_item, inode);
175 btrfs_mark_buffer_dirty(path->nodes[0]);
176 ret = 0;
177failed:
178 btrfs_release_path(root, path);
179 btrfs_free_path(path);
180 return ret;
181}
182
183
Chris Mason5f443fd2007-03-27 13:42:32 -0400184static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
185 struct btrfs_root *root,
186 struct inode *dir,
187 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400188{
Chris Mason5caf2a02007-04-02 11:20:42 -0400189 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400190 const char *name = dentry->d_name.name;
191 int name_len = dentry->d_name.len;
Chris Mason7e381802007-04-19 15:36:27 -0400192 int ret = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400193 u64 objectid;
194 struct btrfs_dir_item *di;
195
Chris Mason5caf2a02007-04-02 11:20:42 -0400196 path = btrfs_alloc_path();
197 BUG_ON(!path);
198 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400199 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400200 name, name_len, -1);
Chris Mason7e381802007-04-19 15:36:27 -0400201 if (IS_ERR(di)) {
202 ret = PTR_ERR(di);
Chris Mason134e9732007-03-25 13:44:56 -0400203 goto err;
Chris Mason7e381802007-04-19 15:36:27 -0400204 }
205 if (!di) {
Chris Mason134e9732007-03-25 13:44:56 -0400206 ret = -ENOENT;
207 goto err;
208 }
Chris Masond6e4a422007-04-06 15:37:36 -0400209 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason7e381802007-04-19 15:36:27 -0400210 ret = btrfs_delete_one_dir_name(trans, root, path, di);
211 BUG_ON(ret);
212 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400213
Chris Mason7e381802007-04-19 15:36:27 -0400214 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
215 objectid, name, name_len, -1);
216 if (IS_ERR(di)) {
217 ret = PTR_ERR(di);
218 goto err;
219 }
220 if (!di) {
221 ret = -ENOENT;
222 goto err;
223 }
224 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason134e9732007-03-25 13:44:56 -0400225 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400226
Chris Mason134e9732007-03-25 13:44:56 -0400227 dentry->d_inode->i_ctime = dir->i_ctime;
228err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400229 btrfs_free_path(path);
Chris Masonf68cad02007-04-24 12:44:26 -0400230 if (!ret) {
Chris Mason5f26f772007-04-05 10:38:44 -0400231 dir->i_size -= name_len * 2;
Chris Masonf68cad02007-04-24 12:44:26 -0400232 btrfs_update_inode(trans, root, dir);
233 drop_nlink(dentry->d_inode);
234 btrfs_update_inode(trans, root, dentry->d_inode);
Chris Masoncd1bc462007-04-27 10:08:34 -0400235 dir->i_sb->s_dirt = 1;
Chris Masond4dbff92007-04-04 14:08:15 -0400236 }
Chris Mason134e9732007-03-25 13:44:56 -0400237 return ret;
238}
239
Chris Mason5f443fd2007-03-27 13:42:32 -0400240static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
241{
242 struct btrfs_root *root;
243 struct btrfs_trans_handle *trans;
244 int ret;
245
Chris Masond6e4a422007-04-06 15:37:36 -0400246 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400247 mutex_lock(&root->fs_info->fs_mutex);
248 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400249 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f443fd2007-03-27 13:42:32 -0400250 ret = btrfs_unlink_trans(trans, root, dir, dentry);
251 btrfs_end_transaction(trans, root);
252 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400253 btrfs_btree_balance_dirty(root);
Chris Mason5f443fd2007-03-27 13:42:32 -0400254 return ret;
255}
256
257static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
258{
259 struct inode *inode = dentry->d_inode;
260 int err;
261 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400262 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400263 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400264 struct btrfs_key key;
265 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400266 struct btrfs_key found_key;
267 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400268 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400269 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400270
Chris Mason5caf2a02007-04-02 11:20:42 -0400271 path = btrfs_alloc_path();
272 BUG_ON(!path);
273 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400274 mutex_lock(&root->fs_info->fs_mutex);
275 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400276 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f443fd2007-03-27 13:42:32 -0400277 key.objectid = inode->i_ino;
278 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400279 key.flags = (u32)-1;
280 while(1) {
281 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
282 if (ret < 0) {
283 err = ret;
284 goto out;
285 }
286 BUG_ON(ret == 0);
287 if (path->slots[0] == 0) {
288 err = -ENOENT;
289 goto out;
290 }
291 path->slots[0]--;
292 leaf = btrfs_buffer_leaf(path->nodes[0]);
293 btrfs_disk_key_to_cpu(&found_key,
294 &leaf->items[path->slots[0]].key);
295 found_type = btrfs_key_type(&found_key);
296 if (found_key.objectid != inode->i_ino) {
297 err = -ENOENT;
298 goto out;
299 }
300 if ((found_type != BTRFS_DIR_ITEM_KEY &&
301 found_type != BTRFS_DIR_INDEX_KEY) ||
302 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
303 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
304 err = -ENOTEMPTY;
305 goto out;
306 }
307 ret = btrfs_del_item(trans, root, path);
308 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400309
Chris Mason5f26f772007-04-05 10:38:44 -0400310 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
311 break;
312 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400313 }
Chris Mason5f26f772007-04-05 10:38:44 -0400314 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400315 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400316
317 /* now the directory is empty */
318 err = btrfs_unlink_trans(trans, root, dir, dentry);
319 if (!err) {
320 inode->i_size = 0;
321 }
322out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400323 btrfs_release_path(root, path);
324 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400325 mutex_unlock(&root->fs_info->fs_mutex);
326 ret = btrfs_end_transaction(trans, root);
Chris Mason35b7e472007-05-02 15:53:43 -0400327 btrfs_btree_balance_dirty(root);
Chris Mason5f443fd2007-03-27 13:42:32 -0400328 if (ret && !err)
329 err = ret;
330 return err;
331}
332
Chris Mason134e9732007-03-25 13:44:56 -0400333static int btrfs_free_inode(struct btrfs_trans_handle *trans,
334 struct btrfs_root *root,
335 struct inode *inode)
336{
Chris Mason5caf2a02007-04-02 11:20:42 -0400337 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400338 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400339
Chris Mason134e9732007-03-25 13:44:56 -0400340 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400341
342 path = btrfs_alloc_path();
343 BUG_ON(!path);
344 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400345 ret = btrfs_lookup_inode(trans, root, path,
346 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400347 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400348 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400349 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400350 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400351 return ret;
352}
353
Chris Masonde428b62007-05-18 13:28:27 -0400354static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
355 u64 objectid)
356{
357 struct btrfs_node *node;
358 int i;
359 int nritems;
360 u64 item_objectid;
361 u64 blocknr;
362 int slot;
363 int ret;
364
365 if (!path->nodes[1])
366 return;
367 node = btrfs_buffer_node(path->nodes[1]);
368 slot = path->slots[1];
369 if (slot == 0)
370 return;
371 nritems = btrfs_header_nritems(&node->header);
372 for (i = slot - 1; i >= 0; i--) {
373 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
374 if (item_objectid != objectid)
375 break;
376 blocknr = btrfs_node_blockptr(node, i);
377 ret = readahead_tree_block(root, blocknr);
378 if (ret)
379 break;
380 }
381}
382
Chris Masonf4b9aa82007-03-27 11:05:53 -0400383static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
384 struct btrfs_root *root,
385 struct inode *inode)
386{
387 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400388 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400389 struct btrfs_key key;
390 struct btrfs_disk_key *found_key;
391 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400392 struct btrfs_file_extent_item *fi = NULL;
393 u64 extent_start = 0;
394 u64 extent_num_blocks = 0;
395 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400396
Chris Mason5caf2a02007-04-02 11:20:42 -0400397 path = btrfs_alloc_path();
398 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400399 /* FIXME, add redo link to tree so we don't leak on crash */
400 key.objectid = inode->i_ino;
401 key.offset = (u64)-1;
402 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400403 /*
404 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
405 * or extent data
406 */
Chris Masonf254e522007-03-29 15:15:27 -0400407 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400408 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400409 btrfs_init_path(path);
410 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400411 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400412 goto error;
413 }
414 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400415 BUG_ON(path->slots[0] == 0);
416 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400417 }
Chris Masonde428b62007-05-18 13:28:27 -0400418 reada_truncate(root, path, inode->i_ino);
Chris Mason5caf2a02007-04-02 11:20:42 -0400419 leaf = btrfs_buffer_leaf(path->nodes[0]);
420 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400421 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
422 break;
Chris Masonf254e522007-03-29 15:15:27 -0400423 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
424 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400425 break;
426 if (btrfs_disk_key_offset(found_key) < inode->i_size)
427 break;
Chris Mason236454df2007-04-19 13:37:44 -0400428 found_extent = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400429 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400430 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
431 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400432 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400433 if (btrfs_file_extent_type(fi) !=
434 BTRFS_FILE_EXTENT_INLINE) {
435 extent_start =
436 btrfs_file_extent_disk_blocknr(fi);
437 extent_num_blocks =
438 btrfs_file_extent_disk_num_blocks(fi);
439 /* FIXME blocksize != 4096 */
440 inode->i_blocks -=
441 btrfs_file_extent_num_blocks(fi) << 3;
442 found_extent = 1;
443 }
Chris Masonf254e522007-03-29 15:15:27 -0400444 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400445 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400446 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400447 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400448 if (found_extent) {
449 ret = btrfs_free_extent(trans, root, extent_start,
450 extent_num_blocks, 0);
451 BUG_ON(ret);
452 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400453 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400454 ret = 0;
455error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400456 btrfs_release_path(root, path);
457 btrfs_free_path(path);
Chris Masoncd1bc462007-04-27 10:08:34 -0400458 inode->i_sb->s_dirt = 1;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400459 return ret;
460}
461
Chris Mason134e9732007-03-25 13:44:56 -0400462static void btrfs_delete_inode(struct inode *inode)
463{
464 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400465 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400466 int ret;
467
Chris Mason134e9732007-03-25 13:44:56 -0400468 truncate_inode_pages(&inode->i_data, 0);
469 if (is_bad_inode(inode)) {
470 goto no_delete;
471 }
472 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400473 mutex_lock(&root->fs_info->fs_mutex);
474 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400475 btrfs_set_trans_block_group(trans, inode);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400476 if (S_ISREG(inode->i_mode)) {
477 ret = btrfs_truncate_in_trans(trans, root, inode);
478 BUG_ON(ret);
479 }
Chris Mason134e9732007-03-25 13:44:56 -0400480 btrfs_free_inode(trans, root, inode);
481 btrfs_end_transaction(trans, root);
482 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400483 btrfs_btree_balance_dirty(root);
Chris Mason134e9732007-03-25 13:44:56 -0400484 return;
485no_delete:
486 clear_inode(inode);
487}
488
Chris Masone20d96d2007-03-22 12:13:20 -0400489static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400490 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400491{
492 const char *name = dentry->d_name.name;
493 int namelen = dentry->d_name.len;
494 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400495 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400496 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400497 int ret;
498
Chris Mason5caf2a02007-04-02 11:20:42 -0400499 path = btrfs_alloc_path();
500 BUG_ON(!path);
501 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400502 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400503 namelen, 0);
Chris Mason7e381802007-04-19 15:36:27 -0400504 if (!di || IS_ERR(di)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400505 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400506 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400507 goto out;
508 }
Chris Masond6e4a422007-04-06 15:37:36 -0400509 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400510out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400511 btrfs_release_path(root, path);
512 btrfs_free_path(path);
Chris Masone20d96d2007-03-22 12:13:20 -0400513 return ret;
514}
515
Chris Mason35b7e472007-05-02 15:53:43 -0400516static int fixup_tree_root_location(struct btrfs_root *root,
Chris Masond6e4a422007-04-06 15:37:36 -0400517 struct btrfs_key *location,
518 struct btrfs_root **sub_root)
519{
520 struct btrfs_path *path;
521 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400522
523 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
524 return 0;
525 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
526 return 0;
527
528 path = btrfs_alloc_path();
529 BUG_ON(!path);
530 mutex_lock(&root->fs_info->fs_mutex);
531
Chris Mason0f7d52f2007-04-09 10:42:37 -0400532 *sub_root = btrfs_read_fs_root(root->fs_info, location);
533 if (IS_ERR(*sub_root))
534 return PTR_ERR(*sub_root);
535
536 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400537 location->objectid = btrfs_root_dirid(ri);
538 location->flags = 0;
539 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
540 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400541
Chris Masond6e4a422007-04-06 15:37:36 -0400542 btrfs_free_path(path);
543 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400544 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400545}
546
Chris Mason35b7e472007-05-02 15:53:43 -0400547static int btrfs_init_locked_inode(struct inode *inode, void *p)
Chris Masonc5739bb2007-04-10 09:27:04 -0400548{
549 struct btrfs_iget_args *args = p;
550 inode->i_ino = args->ino;
551 BTRFS_I(inode)->root = args->root;
552 return 0;
553}
554
Chris Mason35b7e472007-05-02 15:53:43 -0400555static int btrfs_find_actor(struct inode *inode, void *opaque)
Chris Masonc5739bb2007-04-10 09:27:04 -0400556{
557 struct btrfs_iget_args *args = opaque;
558 return (args->ino == inode->i_ino &&
559 args->root == BTRFS_I(inode)->root);
560}
561
Chris Mason35b7e472007-05-02 15:53:43 -0400562static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
563 struct btrfs_root *root)
Chris Masonc5739bb2007-04-10 09:27:04 -0400564{
565 struct inode *inode;
566 struct btrfs_iget_args args;
567 args.ino = objectid;
568 args.root = root;
569
570 inode = iget5_locked(s, objectid, btrfs_find_actor,
571 btrfs_init_locked_inode,
572 (void *)&args);
573 return inode;
574}
Chris Masond6e4a422007-04-06 15:37:36 -0400575
Chris Masone20d96d2007-03-22 12:13:20 -0400576static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
577 struct nameidata *nd)
578{
579 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400580 struct btrfs_inode *bi = BTRFS_I(dir);
581 struct btrfs_root *root = bi->root;
582 struct btrfs_root *sub_root = root;
583 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400584 int ret;
585
586 if (dentry->d_name.len > BTRFS_NAME_LEN)
587 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400588 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400589 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400590 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400591 if (ret < 0)
592 return ERR_PTR(ret);
593 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400594 if (location.objectid) {
595 ret = fixup_tree_root_location(root, &location, &sub_root);
596 if (ret < 0)
597 return ERR_PTR(ret);
598 if (ret > 0)
599 return ERR_PTR(-ENOENT);
Chris Masonc5739bb2007-04-10 09:27:04 -0400600 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
601 sub_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400602 if (!inode)
603 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400604 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400605 if (sub_root != root) {
Chris Masonc5739bb2007-04-10 09:27:04 -0400606printk("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 -0400607 igrab(inode);
608 sub_root->inode = inode;
609 }
Chris Masond6e4a422007-04-06 15:37:36 -0400610 BTRFS_I(inode)->root = sub_root;
611 memcpy(&BTRFS_I(inode)->location, &location,
612 sizeof(location));
613 btrfs_read_locked_inode(inode);
614 unlock_new_inode(inode);
615 }
Chris Masone20d96d2007-03-22 12:13:20 -0400616 }
617 return d_splice_alias(inode, dentry);
618}
619
Chris Masonde428b62007-05-18 13:28:27 -0400620static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
621 u64 objectid)
Chris Mason090d1872007-05-01 08:53:32 -0400622{
623 struct btrfs_node *node;
624 int i;
Chris Masonde428b62007-05-18 13:28:27 -0400625 u32 nritems;
Chris Mason090d1872007-05-01 08:53:32 -0400626 u64 item_objectid;
627 u64 blocknr;
628 int slot;
Chris Masonde428b62007-05-18 13:28:27 -0400629 int ret;
Chris Mason090d1872007-05-01 08:53:32 -0400630
631 if (!path->nodes[1])
632 return;
633 node = btrfs_buffer_node(path->nodes[1]);
634 slot = path->slots[1];
Chris Mason090d1872007-05-01 08:53:32 -0400635 nritems = btrfs_header_nritems(&node->header);
Chris Masonde428b62007-05-18 13:28:27 -0400636 for (i = slot + 1; i < nritems; i++) {
Chris Mason090d1872007-05-01 08:53:32 -0400637 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
638 if (item_objectid != objectid)
639 break;
640 blocknr = btrfs_node_blockptr(node, i);
Chris Masonde428b62007-05-18 13:28:27 -0400641 ret = readahead_tree_block(root, blocknr);
642 if (ret)
643 break;
Chris Mason090d1872007-05-01 08:53:32 -0400644 }
645}
646
Chris Masone20d96d2007-03-22 12:13:20 -0400647static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
648{
649 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400650 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400651 struct btrfs_item *item;
652 struct btrfs_dir_item *di;
653 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400654 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400655 int ret;
656 u32 nritems;
657 struct btrfs_leaf *leaf;
658 int slot;
659 int advance;
660 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400661 int over = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400662 u32 di_cur;
663 u32 di_total;
664 u32 di_len;
665 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Masond6e4a422007-04-06 15:37:36 -0400666
667 /* FIXME, use a real flag for deciding about the key type */
668 if (root->fs_info->tree_root == root)
669 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400670 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400671 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400672 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400673 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400674 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400675 path = btrfs_alloc_path();
676 btrfs_init_path(path);
677 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400678 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400679 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400680 advance = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400681 reada_leaves(root, path, inode->i_ino);
Chris Masone20d96d2007-03-22 12:13:20 -0400682 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400683 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400684 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400685 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400686 if (advance || slot >= nritems) {
687 if (slot >= nritems -1) {
Chris Masonde428b62007-05-18 13:28:27 -0400688 reada_leaves(root, path, inode->i_ino);
Chris Mason5caf2a02007-04-02 11:20:42 -0400689 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400690 if (ret)
691 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400692 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400693 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400694 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400695 } else {
696 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400698 }
699 }
700 advance = 1;
701 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400702 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
703 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400704 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masona429e512007-04-18 16:15:28 -0400705 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400706 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
707 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400708 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400709 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400710 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400711 di_cur = 0;
712 di_total = btrfs_item_size(leaf->items + slot);
713 while(di_cur < di_total) {
714 over = filldir(dirent, (const char *)(di + 1),
715 btrfs_dir_name_len(di),
716 btrfs_disk_key_offset(&item->key),
717 btrfs_disk_key_objectid(&di->location),
718 d_type);
719 if (over)
720 goto nopos;
721 di_len = btrfs_dir_name_len(di) + sizeof(*di);
722 di_cur += di_len;
723 di = (struct btrfs_dir_item *)((char *)di + di_len);
724 }
Chris Masone20d96d2007-03-22 12:13:20 -0400725 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400726 filp->f_pos++;
727nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400728 ret = 0;
729err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400730 btrfs_release_path(root, path);
731 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400732 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400733 return ret;
734}
735
736static void btrfs_put_super (struct super_block * sb)
737{
738 struct btrfs_root *root = btrfs_sb(sb);
739 int ret;
740
741 ret = close_ctree(root);
742 if (ret) {
743 printk("close ctree returns %d\n", ret);
744 }
745 sb->s_fs_info = NULL;
746}
Chris Mason2e635a22007-03-21 11:12:56 -0400747
748static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
749{
750 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400751 struct dentry * root_dentry;
752 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400753 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400754 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400755
756 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400757 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400758 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400759 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400760
Chris Mason0f7d52f2007-04-09 10:42:37 -0400761 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400762
Chris Mason0f7d52f2007-04-09 10:42:37 -0400763 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400764 printk("btrfs: open_ctree failed\n");
765 return -EIO;
766 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400767 sb->s_fs_info = tree_root;
768 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400769 printk("read in super total blocks %Lu root %Lu\n",
770 btrfs_super_total_blocks(disk_super),
771 btrfs_super_root_dir(disk_super));
772
Chris Masonc5739bb2007-04-10 09:27:04 -0400773 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
774 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400775 bi = BTRFS_I(inode);
776 bi->location.objectid = inode->i_ino;
777 bi->location.offset = 0;
778 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400779 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400780 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
781
Chris Mason2e635a22007-03-21 11:12:56 -0400782 if (!inode)
783 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400784 if (inode->i_state & I_NEW) {
785 btrfs_read_locked_inode(inode);
786 unlock_new_inode(inode);
787 }
Chris Mason2e635a22007-03-21 11:12:56 -0400788
Chris Masone20d96d2007-03-22 12:13:20 -0400789 root_dentry = d_alloc_root(inode);
790 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400791 iput(inode);
792 return -ENOMEM;
793 }
Chris Masone20d96d2007-03-22 12:13:20 -0400794 sb->s_root = root_dentry;
795
Chris Mason2e635a22007-03-21 11:12:56 -0400796 return 0;
797}
798
Chris Mason4730a4b2007-03-26 12:00:39 -0400799static int btrfs_write_inode(struct inode *inode, int wait)
800{
Chris Masond6e4a422007-04-06 15:37:36 -0400801 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400802 struct btrfs_trans_handle *trans;
Chris Masonb5133862007-04-24 11:52:22 -0400803 int ret = 0;
804
805 if (wait) {
806 mutex_lock(&root->fs_info->fs_mutex);
807 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400808 btrfs_set_trans_block_group(trans, inode);
Chris Masonb5133862007-04-24 11:52:22 -0400809 ret = btrfs_commit_transaction(trans, root);
810 mutex_unlock(&root->fs_info->fs_mutex);
811 }
812 return ret;
813}
814
815static void btrfs_dirty_inode(struct inode *inode)
816{
817 struct btrfs_root *root = BTRFS_I(inode)->root;
818 struct btrfs_trans_handle *trans;
Chris Mason4730a4b2007-03-26 12:00:39 -0400819
820 mutex_lock(&root->fs_info->fs_mutex);
821 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400822 btrfs_set_trans_block_group(trans, inode);
Chris Masonb5133862007-04-24 11:52:22 -0400823 btrfs_update_inode(trans, root, inode);
824 btrfs_end_transaction(trans, root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400825 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400826 btrfs_btree_balance_dirty(root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400827}
828
Chris Masond5719762007-03-23 10:01:08 -0400829static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400830 struct btrfs_root *root,
Chris Mason31f3c992007-04-30 15:25:45 -0400831 u64 objectid,
832 struct btrfs_block_group_cache *group,
833 int mode)
Chris Masond5719762007-03-23 10:01:08 -0400834{
835 struct inode *inode;
836 struct btrfs_inode_item inode_item;
Chris Mason1b05da22007-04-10 12:13:09 -0400837 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400838 int ret;
Chris Masonde428b62007-05-18 13:28:27 -0400839 int owner;
Chris Masond5719762007-03-23 10:01:08 -0400840
Chris Mason2619ba12007-04-10 16:58:11 -0400841 inode = new_inode(root->fs_info->sb);
Chris Masond5719762007-03-23 10:01:08 -0400842 if (!inode)
843 return ERR_PTR(-ENOMEM);
844
Chris Mason2619ba12007-04-10 16:58:11 -0400845 BTRFS_I(inode)->root = root;
Chris Masonde428b62007-05-18 13:28:27 -0400846 if (mode & S_IFDIR)
847 owner = 0;
848 else
849 owner = 1;
850 group = btrfs_find_block_group(root, group, 0, 0, owner);
Chris Mason31f3c992007-04-30 15:25:45 -0400851 BTRFS_I(inode)->block_group = group;
Chris Masond5719762007-03-23 10:01:08 -0400852
853 inode->i_uid = current->fsuid;
854 inode->i_gid = current->fsgid;
855 inode->i_mode = mode;
856 inode->i_ino = objectid;
857 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -0400858 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -0400859 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -0400860 location = &BTRFS_I(inode)->location;
861 location->objectid = objectid;
862 location->flags = 0;
863 location->offset = 0;
864 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -0400865
866 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
867 BUG_ON(ret);
868
869 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400870 return inode;
871}
872
873static int btrfs_add_link(struct btrfs_trans_handle *trans,
874 struct dentry *dentry, struct inode *inode)
875{
876 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400877 struct btrfs_key key;
878 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
879 key.objectid = inode->i_ino;
880 key.flags = 0;
881 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
882 key.offset = 0;
883
884 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400885 dentry->d_name.name, dentry->d_name.len,
886 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400887 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400888 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400889 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400890 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400891 dentry->d_parent->d_inode);
892 }
Chris Masond5719762007-03-23 10:01:08 -0400893 return ret;
894}
895
896static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
897 struct dentry *dentry, struct inode *inode)
898{
899 int err = btrfs_add_link(trans, dentry, inode);
900 if (!err) {
901 d_instantiate(dentry, inode);
902 return 0;
903 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400904 if (err > 0)
905 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -0400906 return err;
907}
908
909static int btrfs_create(struct inode *dir, struct dentry *dentry,
910 int mode, struct nameidata *nd)
911{
912 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400913 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400914 struct inode *inode;
915 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400916 int drop_inode = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400917 u64 objectid;
Chris Masond5719762007-03-23 10:01:08 -0400918
Chris Masond561c022007-03-23 19:47:49 -0400919 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400920 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400921 btrfs_set_trans_block_group(trans, dir);
Chris Mason2619ba12007-04-10 16:58:11 -0400922
923 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
924 if (err) {
925 err = -ENOSPC;
926 goto out_unlock;
927 }
928
Chris Mason31f3c992007-04-30 15:25:45 -0400929 inode = btrfs_new_inode(trans, root, objectid,
930 BTRFS_I(dir)->block_group, mode);
Chris Masond5719762007-03-23 10:01:08 -0400931 err = PTR_ERR(inode);
932 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400933 goto out_unlock;
Chris Mason31f3c992007-04-30 15:25:45 -0400934
935 btrfs_set_trans_block_group(trans, inode);
Chris Masond5719762007-03-23 10:01:08 -0400936 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400937 if (err)
938 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400939 else {
940 inode->i_mapping->a_ops = &btrfs_aops;
941 inode->i_fop = &btrfs_file_operations;
942 inode->i_op = &btrfs_file_inode_operations;
943 }
Chris Masond5719762007-03-23 10:01:08 -0400944 dir->i_sb->s_dirt = 1;
Chris Mason31f3c992007-04-30 15:25:45 -0400945 btrfs_update_inode_block_group(trans, inode);
946 btrfs_update_inode_block_group(trans, dir);
Chris Masond561c022007-03-23 19:47:49 -0400947out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400948 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400949 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400950
Chris Mason134e9732007-03-25 13:44:56 -0400951 if (drop_inode) {
952 inode_dec_link_count(inode);
953 iput(inode);
954 }
Chris Mason35b7e472007-05-02 15:53:43 -0400955 btrfs_btree_balance_dirty(root);
Chris Masond5719762007-03-23 10:01:08 -0400956 return err;
957}
958
Chris Masonf7922032007-03-25 20:17:36 -0400959static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400960 struct btrfs_root *root,
961 u64 objectid, u64 dirid)
Chris Masonf7922032007-03-25 20:17:36 -0400962{
Chris Masonf7922032007-03-25 20:17:36 -0400963 int ret;
964 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400965 struct btrfs_key key;
966
Chris Masonf7922032007-03-25 20:17:36 -0400967 buf[0] = '.';
968 buf[1] = '.';
969
Chris Mason2619ba12007-04-10 16:58:11 -0400970 key.objectid = objectid;
Chris Masond6e4a422007-04-06 15:37:36 -0400971 key.offset = 0;
972 key.flags = 0;
973 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
974
Chris Mason2619ba12007-04-10 16:58:11 -0400975 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400976 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400977 if (ret)
978 goto error;
Chris Mason2619ba12007-04-10 16:58:11 -0400979 key.objectid = dirid;
980 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
Chris Masond6e4a422007-04-06 15:37:36 -0400981 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400982 if (ret)
983 goto error;
Chris Masonf7922032007-03-25 20:17:36 -0400984error:
985 return ret;
986}
987
988static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
989{
990 struct inode *inode;
991 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400992 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400993 int err = 0;
994 int drop_on_err = 0;
Chris Mason2619ba12007-04-10 16:58:11 -0400995 u64 objectid;
Chris Masonf7922032007-03-25 20:17:36 -0400996
997 mutex_lock(&root->fs_info->fs_mutex);
998 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400999 btrfs_set_trans_block_group(trans, dir);
Chris Masonf7922032007-03-25 20:17:36 -04001000 if (IS_ERR(trans)) {
1001 err = PTR_ERR(trans);
1002 goto out_unlock;
1003 }
Chris Mason2619ba12007-04-10 16:58:11 -04001004
1005 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1006 if (err) {
1007 err = -ENOSPC;
1008 goto out_unlock;
1009 }
1010
Chris Mason31f3c992007-04-30 15:25:45 -04001011 inode = btrfs_new_inode(trans, root, objectid,
1012 BTRFS_I(dir)->block_group, S_IFDIR | mode);
Chris Masonf7922032007-03-25 20:17:36 -04001013 if (IS_ERR(inode)) {
1014 err = PTR_ERR(inode);
1015 goto out_fail;
1016 }
1017 drop_on_err = 1;
1018 inode->i_op = &btrfs_dir_inode_operations;
1019 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason31f3c992007-04-30 15:25:45 -04001020 btrfs_set_trans_block_group(trans, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001021
Chris Mason2619ba12007-04-10 16:58:11 -04001022 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1023 if (err)
1024 goto out_fail;
1025
1026 inode->i_size = 6;
1027 err = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001028 if (err)
1029 goto out_fail;
1030 err = btrfs_add_link(trans, dentry, inode);
1031 if (err)
1032 goto out_fail;
1033 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001034 drop_on_err = 0;
Chris Masoncd1bc462007-04-27 10:08:34 -04001035 dir->i_sb->s_dirt = 1;
Chris Mason31f3c992007-04-30 15:25:45 -04001036 btrfs_update_inode_block_group(trans, inode);
1037 btrfs_update_inode_block_group(trans, dir);
Chris Masonf7922032007-03-25 20:17:36 -04001038
1039out_fail:
1040 btrfs_end_transaction(trans, root);
1041out_unlock:
1042 mutex_unlock(&root->fs_info->fs_mutex);
1043 if (drop_on_err)
1044 iput(inode);
Chris Mason35b7e472007-05-02 15:53:43 -04001045 btrfs_btree_balance_dirty(root);
Chris Masonf7922032007-03-25 20:17:36 -04001046 return err;
1047}
1048
Chris Mason8fd17792007-04-19 21:01:03 -04001049static int btrfs_sync_file(struct file *file,
1050 struct dentry *dentry, int datasync)
1051{
1052 struct inode *inode = dentry->d_inode;
1053 struct btrfs_root *root = BTRFS_I(inode)->root;
1054 int ret;
1055 struct btrfs_trans_handle *trans;
1056
1057 mutex_lock(&root->fs_info->fs_mutex);
1058 trans = btrfs_start_transaction(root, 1);
1059 if (!trans) {
1060 ret = -ENOMEM;
1061 goto out;
1062 }
1063 ret = btrfs_commit_transaction(trans, root);
1064 mutex_unlock(&root->fs_info->fs_mutex);
1065out:
1066 return ret > 0 ? EIO : ret;
1067}
1068
Chris Masond5719762007-03-23 10:01:08 -04001069static int btrfs_sync_fs(struct super_block *sb, int wait)
1070{
1071 struct btrfs_trans_handle *trans;
1072 struct btrfs_root *root;
1073 int ret;
Chris Masond98237b2007-03-28 13:57:48 -04001074 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -04001075
Chris Masond5719762007-03-23 10:01:08 -04001076 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -04001077 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -04001078 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -04001079 return 0;
1080 }
Chris Masond561c022007-03-23 19:47:49 -04001081 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001082 trans = btrfs_start_transaction(root, 1);
1083 ret = btrfs_commit_transaction(trans, root);
1084 sb->s_dirt = 0;
1085 BUG_ON(ret);
1086printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -04001087 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001088 return 0;
1089}
1090
Chris Mason75dfe392007-03-29 11:56:46 -04001091static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001092 struct buffer_head *result, int create)
1093{
1094 int ret;
1095 int err = 0;
1096 u64 blocknr;
1097 u64 extent_start = 0;
1098 u64 extent_end = 0;
1099 u64 objectid = inode->i_ino;
Chris Mason236454df2007-04-19 13:37:44 -04001100 u32 found_type;
Chris Mason5caf2a02007-04-02 11:20:42 -04001101 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001102 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001103 struct btrfs_file_extent_item *item;
1104 struct btrfs_leaf *leaf;
1105 struct btrfs_disk_key *found_key;
1106
Chris Mason5caf2a02007-04-02 11:20:42 -04001107 path = btrfs_alloc_path();
1108 BUG_ON(!path);
1109 btrfs_init_path(path);
Chris Mason6567e832007-04-16 09:22:45 -04001110 if (create) {
Chris Mason6567e832007-04-16 09:22:45 -04001111 WARN_ON(1);
1112 }
Chris Masondee26a92007-03-26 16:00:06 -04001113
Chris Mason236454df2007-04-19 13:37:44 -04001114 ret = btrfs_lookup_file_extent(NULL, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001115 inode->i_ino,
Chris Mason236454df2007-04-19 13:37:44 -04001116 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001117 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001118 err = ret;
1119 goto out;
1120 }
1121
1122 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001123 if (path->slots[0] == 0) {
1124 btrfs_release_path(root, path);
Chris Mason236454df2007-04-19 13:37:44 -04001125 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001126 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001127 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001128 }
1129
Chris Mason5caf2a02007-04-02 11:20:42 -04001130 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001131 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001132 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001133 blocknr = btrfs_file_extent_disk_blocknr(item);
1134 blocknr += btrfs_file_extent_offset(item);
1135
Chris Masondee26a92007-03-26 16:00:06 -04001136 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001137 found_key = &leaf->items[path->slots[0]].key;
Chris Mason236454df2007-04-19 13:37:44 -04001138 found_type = btrfs_disk_key_type(found_key);
Chris Masondee26a92007-03-26 16:00:06 -04001139 if (btrfs_disk_key_objectid(found_key) != objectid ||
Chris Mason236454df2007-04-19 13:37:44 -04001140 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masondee26a92007-03-26 16:00:06 -04001141 extent_end = 0;
1142 extent_start = 0;
Chris Mason236454df2007-04-19 13:37:44 -04001143 goto out;
Chris Masondee26a92007-03-26 16:00:06 -04001144 }
Chris Mason236454df2007-04-19 13:37:44 -04001145 found_type = btrfs_file_extent_type(item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001146 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason236454df2007-04-19 13:37:44 -04001147 if (found_type == BTRFS_FILE_EXTENT_REG) {
1148 extent_start = extent_start >> inode->i_blkbits;
1149 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1150 if (iblock >= extent_start && iblock < extent_end) {
1151 err = 0;
1152 btrfs_map_bh_to_logical(root, result, blocknr +
1153 iblock - extent_start);
1154 goto out;
1155 }
1156 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1157 char *ptr;
1158 char *map;
1159 u32 size;
1160 size = btrfs_file_extent_inline_len(leaf->items +
1161 path->slots[0]);
1162 extent_end = (extent_start + size) >> inode->i_blkbits;
1163 extent_start >>= inode->i_blkbits;
1164 if (iblock < extent_start || iblock > extent_end) {
1165 goto out;
1166 }
1167 ptr = btrfs_file_extent_inline_start(item);
1168 map = kmap(result->b_page);
1169 memcpy(map, ptr, size);
1170 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1171 flush_dcache_page(result->b_page);
1172 kunmap(result->b_page);
1173 set_buffer_uptodate(result);
1174 SetPageChecked(result->b_page);
1175 btrfs_map_bh_to_logical(root, result, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001176 }
Chris Masondee26a92007-03-26 16:00:06 -04001177out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001178 btrfs_free_path(path);
Chris Mason75dfe392007-03-29 11:56:46 -04001179 return err;
1180}
1181
1182static int btrfs_get_block(struct inode *inode, sector_t iblock,
1183 struct buffer_head *result, int create)
1184{
1185 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001186 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001187 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001188 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001189 mutex_unlock(&root->fs_info->fs_mutex);
1190 return err;
1191}
1192
1193static int btrfs_prepare_write(struct file *file, struct page *page,
1194 unsigned from, unsigned to)
1195{
Chris Mason6407bf62007-03-27 06:33:00 -04001196 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001197}
1198
Chris Masond561c022007-03-23 19:47:49 -04001199static void btrfs_write_super(struct super_block *sb)
1200{
1201 btrfs_sync_fs(sb, 1);
1202}
1203
Chris Masondee26a92007-03-26 16:00:06 -04001204static int btrfs_readpage(struct file *file, struct page *page)
1205{
1206 return mpage_readpage(page, btrfs_get_block);
1207}
1208
Chris Mason236454df2007-04-19 13:37:44 -04001209/*
1210 * While block_write_full_page is writing back the dirty buffers under
1211 * the page lock, whoever dirtied the buffers may decide to clean them
1212 * again at any time. We handle that by only looking at the buffer
1213 * state inside lock_buffer().
1214 *
1215 * If block_write_full_page() is called for regular writeback
1216 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1217 * locked buffer. This only can happen if someone has written the buffer
1218 * directly, with submit_bh(). At the address_space level PageWriteback
1219 * prevents this contention from occurring.
1220 */
1221static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1222 struct writeback_control *wbc)
1223{
1224 int err;
1225 sector_t block;
1226 sector_t last_block;
1227 struct buffer_head *bh, *head;
1228 const unsigned blocksize = 1 << inode->i_blkbits;
1229 int nr_underway = 0;
1230
1231 BUG_ON(!PageLocked(page));
1232
1233 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1234
1235 if (!page_has_buffers(page)) {
1236 create_empty_buffers(page, blocksize,
1237 (1 << BH_Dirty)|(1 << BH_Uptodate));
1238 }
1239
1240 /*
1241 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1242 * here, and the (potentially unmapped) buffers may become dirty at
1243 * any time. If a buffer becomes dirty here after we've inspected it
1244 * then we just miss that fact, and the page stays dirty.
1245 *
1246 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1247 * handle that here by just cleaning them.
1248 */
1249
1250 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1251 head = page_buffers(page);
1252 bh = head;
1253
1254 /*
1255 * Get all the dirty buffers mapped to disk addresses and
1256 * handle any aliases from the underlying blockdev's mapping.
1257 */
1258 do {
1259 if (block > last_block) {
1260 /*
1261 * mapped buffers outside i_size will occur, because
1262 * this page can be outside i_size when there is a
1263 * truncate in progress.
1264 */
1265 /*
1266 * The buffer was zeroed by block_write_full_page()
1267 */
1268 clear_buffer_dirty(bh);
1269 set_buffer_uptodate(bh);
1270 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1271 WARN_ON(bh->b_size != blocksize);
1272 err = btrfs_get_block(inode, block, bh, 0);
Chris Mason35b7e472007-05-02 15:53:43 -04001273 if (err) {
1274printk("writepage going to recovery err %d\n", err);
Chris Mason236454df2007-04-19 13:37:44 -04001275 goto recover;
Chris Mason35b7e472007-05-02 15:53:43 -04001276 }
Chris Mason236454df2007-04-19 13:37:44 -04001277 if (buffer_new(bh)) {
1278 /* blockdev mappings never come here */
1279 clear_buffer_new(bh);
Chris Mason236454df2007-04-19 13:37:44 -04001280 }
1281 }
1282 bh = bh->b_this_page;
1283 block++;
1284 } while (bh != head);
1285
1286 do {
1287 if (!buffer_mapped(bh))
1288 continue;
1289 /*
1290 * If it's a fully non-blocking write attempt and we cannot
1291 * lock the buffer then redirty the page. Note that this can
1292 * potentially cause a busy-wait loop from pdflush and kswapd
1293 * activity, but those code paths have their own higher-level
1294 * throttling.
1295 */
1296 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1297 lock_buffer(bh);
1298 } else if (test_set_buffer_locked(bh)) {
1299 redirty_page_for_writepage(wbc, page);
1300 continue;
1301 }
1302 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1303 mark_buffer_async_write(bh);
1304 } else {
1305 unlock_buffer(bh);
1306 }
1307 } while ((bh = bh->b_this_page) != head);
1308
1309 /*
1310 * The page and its buffers are protected by PageWriteback(), so we can
1311 * drop the bh refcounts early.
1312 */
1313 BUG_ON(PageWriteback(page));
1314 set_page_writeback(page);
1315
1316 do {
1317 struct buffer_head *next = bh->b_this_page;
1318 if (buffer_async_write(bh)) {
1319 submit_bh(WRITE, bh);
1320 nr_underway++;
1321 }
1322 bh = next;
1323 } while (bh != head);
1324 unlock_page(page);
1325
1326 err = 0;
1327done:
1328 if (nr_underway == 0) {
1329 /*
1330 * The page was marked dirty, but the buffers were
1331 * clean. Someone wrote them back by hand with
1332 * ll_rw_block/submit_bh. A rare case.
1333 */
1334 int uptodate = 1;
1335 do {
1336 if (!buffer_uptodate(bh)) {
1337 uptodate = 0;
1338 break;
1339 }
1340 bh = bh->b_this_page;
1341 } while (bh != head);
1342 if (uptodate)
1343 SetPageUptodate(page);
1344 end_page_writeback(page);
Chris Mason236454df2007-04-19 13:37:44 -04001345 }
1346 return err;
1347
1348recover:
1349 /*
1350 * ENOSPC, or some other error. We may already have added some
1351 * blocks to the file, so we need to write these out to avoid
1352 * exposing stale data.
1353 * The page is currently locked and not marked for writeback
1354 */
1355 bh = head;
1356 /* Recovery: lock and submit the mapped buffers */
1357 do {
1358 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1359 lock_buffer(bh);
1360 mark_buffer_async_write(bh);
1361 } else {
1362 /*
1363 * The buffer may have been set dirty during
1364 * attachment to a dirty page.
1365 */
1366 clear_buffer_dirty(bh);
1367 }
1368 } while ((bh = bh->b_this_page) != head);
1369 SetPageError(page);
1370 BUG_ON(PageWriteback(page));
1371 set_page_writeback(page);
1372 do {
1373 struct buffer_head *next = bh->b_this_page;
1374 if (buffer_async_write(bh)) {
1375 clear_buffer_dirty(bh);
1376 submit_bh(WRITE, bh);
1377 nr_underway++;
1378 }
1379 bh = next;
1380 } while (bh != head);
1381 unlock_page(page);
1382 goto done;
1383}
1384
1385/*
1386 * The generic ->writepage function for buffer-backed address_spaces
1387 */
Chris Masondee26a92007-03-26 16:00:06 -04001388static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1389{
Chris Mason236454df2007-04-19 13:37:44 -04001390 struct inode * const inode = page->mapping->host;
1391 loff_t i_size = i_size_read(inode);
1392 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1393 unsigned offset;
1394 void *kaddr;
1395
1396 /* Is the page fully inside i_size? */
1397 if (page->index < end_index)
1398 return __btrfs_write_full_page(inode, page, wbc);
1399
1400 /* Is the page fully outside i_size? (truncate in progress) */
1401 offset = i_size & (PAGE_CACHE_SIZE-1);
1402 if (page->index >= end_index+1 || !offset) {
1403 /*
1404 * The page may have dirty, unmapped buffers. For example,
1405 * they may have been added in ext3_writepage(). Make them
1406 * freeable here, so the page does not leak.
1407 */
1408 block_invalidatepage(page, 0);
1409 unlock_page(page);
1410 return 0; /* don't care */
1411 }
1412
1413 /*
1414 * The page straddles i_size. It must be zeroed out on each and every
1415 * writepage invokation because it may be mmapped. "A file is mapped
1416 * in multiples of the page size. For a file that is not a multiple of
1417 * the page size, the remaining memory is zeroed when mapped, and
1418 * writes to that region are not written out to the file."
1419 */
1420 kaddr = kmap_atomic(page, KM_USER0);
1421 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1422 flush_dcache_page(page);
1423 kunmap_atomic(kaddr, KM_USER0);
1424 return __btrfs_write_full_page(inode, page, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001425}
Chris Masond561c022007-03-23 19:47:49 -04001426
Chris Masonf4b9aa82007-03-27 11:05:53 -04001427static void btrfs_truncate(struct inode *inode)
1428{
Chris Masond6e4a422007-04-06 15:37:36 -04001429 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001430 int ret;
1431 struct btrfs_trans_handle *trans;
1432
1433 if (!S_ISREG(inode->i_mode))
1434 return;
1435 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1436 return;
1437
Chris Masone8f05c42007-04-04 14:30:09 -04001438 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001439
1440 /* FIXME, add redo link to tree so we don't leak on crash */
1441 mutex_lock(&root->fs_info->fs_mutex);
1442 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001443 btrfs_set_trans_block_group(trans, inode);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001444 ret = btrfs_truncate_in_trans(trans, root, inode);
1445 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001446 btrfs_update_inode(trans, root, inode);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001447 ret = btrfs_end_transaction(trans, root);
1448 BUG_ON(ret);
1449 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04001450 btrfs_btree_balance_dirty(root);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001451}
1452
Chris Mason236454df2007-04-19 13:37:44 -04001453/*
1454 * Make sure any changes to nobh_commit_write() are reflected in
1455 * nobh_truncate_page(), since it doesn't call commit_write().
1456 */
1457static int btrfs_commit_write(struct file *file, struct page *page,
1458 unsigned from, unsigned to)
1459{
1460 struct inode *inode = page->mapping->host;
1461 struct buffer_head *bh;
1462 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1463
1464 SetPageUptodate(page);
1465 bh = page_buffers(page);
1466 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1467 set_page_dirty(page);
1468 }
1469 if (pos > inode->i_size) {
1470 i_size_write(inode, pos);
1471 mark_inode_dirty(inode);
1472 }
1473 return 0;
1474}
1475
Chris Mason75dfe392007-03-29 11:56:46 -04001476static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1477 struct page **prepared_pages,
1478 const char __user * buf)
1479{
1480 long page_fault = 0;
1481 int i;
1482 int offset = pos & (PAGE_CACHE_SIZE - 1);
1483
1484 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1485 size_t count = min_t(size_t,
1486 PAGE_CACHE_SIZE - offset, write_bytes);
1487 struct page *page = prepared_pages[i];
1488 fault_in_pages_readable(buf, count);
1489
1490 /* Copy data from userspace to the current page */
1491 kmap(page);
1492 page_fault = __copy_from_user(page_address(page) + offset,
1493 buf, count);
1494 /* Flush processor's dcache for this page */
1495 flush_dcache_page(page);
1496 kunmap(page);
1497 buf += count;
1498 write_bytes -= count;
1499
1500 if (page_fault)
1501 break;
1502 }
1503 return page_fault ? -EFAULT : 0;
1504}
1505
1506static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1507{
1508 size_t i;
1509 for (i = 0; i < num_pages; i++) {
1510 if (!pages[i])
1511 break;
1512 unlock_page(pages[i]);
1513 mark_page_accessed(pages[i]);
1514 page_cache_release(pages[i]);
1515 }
1516}
1517static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root,
1519 struct file *file,
1520 struct page **pages,
1521 size_t num_pages,
1522 loff_t pos,
1523 size_t write_bytes)
1524{
1525 int i;
1526 int offset;
1527 int err = 0;
1528 int ret;
1529 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001530 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason236454df2007-04-19 13:37:44 -04001531 struct buffer_head *bh;
1532 struct btrfs_file_extent_item *ei;
Chris Mason75dfe392007-03-29 11:56:46 -04001533
1534 for (i = 0; i < num_pages; i++) {
1535 offset = pos & (PAGE_CACHE_SIZE -1);
1536 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001537 /* FIXME, one block at a time */
1538
1539 mutex_lock(&root->fs_info->fs_mutex);
1540 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001541 btrfs_set_trans_block_group(trans, inode);
Chris Mason236454df2007-04-19 13:37:44 -04001542
1543 bh = page_buffers(pages[i]);
1544 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1545 struct btrfs_key key;
1546 struct btrfs_path *path;
1547 char *ptr;
1548 u32 datasize;
1549
1550 path = btrfs_alloc_path();
1551 BUG_ON(!path);
1552 key.objectid = inode->i_ino;
1553 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1554 key.flags = 0;
1555 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1556 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1557 datasize = offset +
1558 btrfs_file_extent_calc_inline_size(write_bytes);
1559 ret = btrfs_insert_empty_item(trans, root, path, &key,
1560 datasize);
1561 BUG_ON(ret);
1562 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1563 path->slots[0], struct btrfs_file_extent_item);
1564 btrfs_set_file_extent_generation(ei, trans->transid);
1565 btrfs_set_file_extent_type(ei,
1566 BTRFS_FILE_EXTENT_INLINE);
1567 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason098f59c2007-05-11 11:33:21 -04001568 btrfs_memcpy(root, path->nodes[0]->b_data,
1569 ptr, bh->b_data, offset + write_bytes);
Chris Mason236454df2007-04-19 13:37:44 -04001570 mark_buffer_dirty(path->nodes[0]);
1571 btrfs_free_path(path);
1572 } else {
1573 btrfs_csum_file_block(trans, root, inode->i_ino,
Chris Masonf254e522007-03-29 15:15:27 -04001574 pages[i]->index << PAGE_CACHE_SHIFT,
1575 kmap(pages[i]), PAGE_CACHE_SIZE);
Chris Mason236454df2007-04-19 13:37:44 -04001576 kunmap(pages[i]);
1577 }
Chris Masonf254e522007-03-29 15:15:27 -04001578 SetPageChecked(pages[i]);
Chris Masone37c9e62007-05-09 20:13:14 -04001579 // btrfs_update_inode_block_group(trans, inode);
Chris Masonf254e522007-03-29 15:15:27 -04001580 ret = btrfs_end_transaction(trans, root);
1581 BUG_ON(ret);
1582 mutex_unlock(&root->fs_info->fs_mutex);
1583
Chris Mason236454df2007-04-19 13:37:44 -04001584 ret = btrfs_commit_write(file, pages[i], offset,
Chris Mason75dfe392007-03-29 11:56:46 -04001585 offset + this_write);
1586 pos += this_write;
1587 if (ret) {
1588 err = ret;
1589 goto failed;
1590 }
1591 WARN_ON(this_write > write_bytes);
1592 write_bytes -= this_write;
1593 }
1594failed:
1595 return err;
1596}
1597
Chris Masonb18c6682007-04-17 13:26:50 -04001598static int drop_extents(struct btrfs_trans_handle *trans,
1599 struct btrfs_root *root,
1600 struct inode *inode,
Chris Masonde428b62007-05-18 13:28:27 -04001601 u64 start, u64 end, u64 *hint_block)
Chris Masonb18c6682007-04-17 13:26:50 -04001602{
1603 int ret;
1604 struct btrfs_key key;
1605 struct btrfs_leaf *leaf;
1606 int slot;
1607 struct btrfs_file_extent_item *extent;
Chris Mason236454df2007-04-19 13:37:44 -04001608 u64 extent_end = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001609 int keep;
1610 struct btrfs_file_extent_item old;
1611 struct btrfs_path *path;
1612 u64 search_start = start;
1613 int bookend;
Chris Mason236454df2007-04-19 13:37:44 -04001614 int found_type;
1615 int found_extent;
1616 int found_inline;
1617
Chris Masonb18c6682007-04-17 13:26:50 -04001618 path = btrfs_alloc_path();
1619 if (!path)
1620 return -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001621 while(1) {
Chris Masona429e512007-04-18 16:15:28 -04001622 btrfs_release_path(root, path);
1623 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1624 search_start, -1);
1625 if (ret < 0)
1626 goto out;
1627 if (ret > 0) {
1628 if (path->slots[0] == 0) {
Chris Mason236454df2007-04-19 13:37:44 -04001629 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001630 goto out;
1631 }
1632 path->slots[0]--;
1633 }
Chris Masonb18c6682007-04-17 13:26:50 -04001634 keep = 0;
1635 bookend = 0;
Chris Mason236454df2007-04-19 13:37:44 -04001636 found_extent = 0;
1637 found_inline = 0;
1638 extent = NULL;
Chris Masonb18c6682007-04-17 13:26:50 -04001639 leaf = btrfs_buffer_leaf(path->nodes[0]);
1640 slot = path->slots[0];
1641 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
Chris Masonb18c6682007-04-17 13:26:50 -04001642 if (key.offset >= end || key.objectid != inode->i_ino) {
1643 ret = 0;
1644 goto out;
1645 }
Chris Mason236454df2007-04-19 13:37:44 -04001646 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1647 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001648 goto out;
Chris Mason236454df2007-04-19 13:37:44 -04001649 }
1650 extent = btrfs_item_ptr(leaf, slot,
1651 struct btrfs_file_extent_item);
1652 found_type = btrfs_file_extent_type(extent);
1653 if (found_type == BTRFS_FILE_EXTENT_REG) {
1654 extent_end = key.offset +
1655 (btrfs_file_extent_num_blocks(extent) <<
1656 inode->i_blkbits);
1657 found_extent = 1;
1658 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1659 found_inline = 1;
1660 extent_end = key.offset +
1661 btrfs_file_extent_inline_len(leaf->items + slot);
1662 }
1663
1664 if (!found_extent && !found_inline) {
1665 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001666 goto out;
Chris Mason236454df2007-04-19 13:37:44 -04001667 }
1668
1669 if (search_start >= extent_end) {
1670 ret = 0;
1671 goto out;
1672 }
1673
Chris Masona429e512007-04-18 16:15:28 -04001674 search_start = extent_end;
Chris Masonb18c6682007-04-17 13:26:50 -04001675
1676 if (end < extent_end && end >= key.offset) {
Chris Mason236454df2007-04-19 13:37:44 -04001677 if (found_extent) {
1678 memcpy(&old, extent, sizeof(old));
1679 ret = btrfs_inc_extent_ref(trans, root,
1680 btrfs_file_extent_disk_blocknr(&old),
1681 btrfs_file_extent_disk_num_blocks(&old));
1682 BUG_ON(ret);
1683 }
1684 WARN_ON(found_inline);
Chris Masonb18c6682007-04-17 13:26:50 -04001685 bookend = 1;
1686 }
1687
1688 if (start > key.offset) {
1689 u64 new_num;
Chris Masona429e512007-04-18 16:15:28 -04001690 u64 old_num;
Chris Masonb18c6682007-04-17 13:26:50 -04001691 /* truncate existing extent */
1692 keep = 1;
1693 WARN_ON(start & (root->blocksize - 1));
Chris Mason236454df2007-04-19 13:37:44 -04001694 if (found_extent) {
1695 new_num = (start - key.offset) >>
1696 inode->i_blkbits;
1697 old_num = btrfs_file_extent_num_blocks(extent);
Chris Masonde428b62007-05-18 13:28:27 -04001698 *hint_block =
1699 btrfs_file_extent_disk_blocknr(extent);
Chris Mason236454df2007-04-19 13:37:44 -04001700 inode->i_blocks -= (old_num - new_num) << 3;
1701 btrfs_set_file_extent_num_blocks(extent,
1702 new_num);
1703 mark_buffer_dirty(path->nodes[0]);
1704 } else {
1705 WARN_ON(1);
Chris Mason236454df2007-04-19 13:37:44 -04001706 }
Chris Masonb18c6682007-04-17 13:26:50 -04001707 }
1708 if (!keep) {
Chris Mason236454df2007-04-19 13:37:44 -04001709 u64 disk_blocknr = 0;
1710 u64 disk_num_blocks = 0;
1711 u64 extent_num_blocks = 0;
1712 if (found_extent) {
1713 disk_blocknr =
1714 btrfs_file_extent_disk_blocknr(extent);
1715 disk_num_blocks =
1716 btrfs_file_extent_disk_num_blocks(extent);
1717 extent_num_blocks =
1718 btrfs_file_extent_num_blocks(extent);
Chris Masonde428b62007-05-18 13:28:27 -04001719 *hint_block =
1720 btrfs_file_extent_disk_blocknr(extent);
Chris Mason236454df2007-04-19 13:37:44 -04001721 }
Chris Masonb18c6682007-04-17 13:26:50 -04001722 ret = btrfs_del_item(trans, root, path);
1723 BUG_ON(ret);
1724 btrfs_release_path(root, path);
Chris Mason098f59c2007-05-11 11:33:21 -04001725 extent = NULL;
Chris Mason236454df2007-04-19 13:37:44 -04001726 if (found_extent) {
Chris Mason098f59c2007-05-11 11:33:21 -04001727 inode->i_blocks -= extent_num_blocks << 3;
Chris Mason236454df2007-04-19 13:37:44 -04001728 ret = btrfs_free_extent(trans, root,
1729 disk_blocknr,
1730 disk_num_blocks, 0);
1731 }
Chris Masonb18c6682007-04-17 13:26:50 -04001732
1733 BUG_ON(ret);
1734 if (!bookend && search_start >= end) {
1735 ret = 0;
1736 goto out;
1737 }
1738 if (!bookend)
Chris Masona429e512007-04-18 16:15:28 -04001739 continue;
Chris Masonb18c6682007-04-17 13:26:50 -04001740 }
Chris Mason236454df2007-04-19 13:37:44 -04001741 if (bookend && found_extent) {
Chris Masonb18c6682007-04-17 13:26:50 -04001742 /* create bookend */
1743 struct btrfs_key ins;
Chris Masonb18c6682007-04-17 13:26:50 -04001744 ins.objectid = inode->i_ino;
1745 ins.offset = end;
1746 ins.flags = 0;
1747 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1748
1749 btrfs_release_path(root, path);
Chris Masonb18c6682007-04-17 13:26:50 -04001750 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1751 sizeof(*extent));
1752 BUG_ON(ret);
1753 extent = btrfs_item_ptr(
1754 btrfs_buffer_leaf(path->nodes[0]),
1755 path->slots[0],
1756 struct btrfs_file_extent_item);
1757 btrfs_set_file_extent_disk_blocknr(extent,
1758 btrfs_file_extent_disk_blocknr(&old));
1759 btrfs_set_file_extent_disk_num_blocks(extent,
1760 btrfs_file_extent_disk_num_blocks(&old));
1761
1762 btrfs_set_file_extent_offset(extent,
1763 btrfs_file_extent_offset(&old) +
1764 ((end - key.offset) >> inode->i_blkbits));
1765 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1766 (end - key.offset) >> inode->i_blkbits);
1767 btrfs_set_file_extent_num_blocks(extent,
1768 btrfs_file_extent_num_blocks(&old) -
1769 ((end - key.offset) >> inode->i_blkbits));
1770
Chris Mason236454df2007-04-19 13:37:44 -04001771 btrfs_set_file_extent_type(extent,
1772 BTRFS_FILE_EXTENT_REG);
Chris Masonb18c6682007-04-17 13:26:50 -04001773 btrfs_set_file_extent_generation(extent,
1774 btrfs_file_extent_generation(&old));
Chris Masonb18c6682007-04-17 13:26:50 -04001775 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona429e512007-04-18 16:15:28 -04001776 inode->i_blocks +=
1777 btrfs_file_extent_num_blocks(extent) << 3;
Chris Masonb18c6682007-04-17 13:26:50 -04001778 ret = 0;
Chris Mason70b2bef2007-04-17 15:39:32 -04001779 goto out;
Chris Masonb18c6682007-04-17 13:26:50 -04001780 }
Chris Masonb18c6682007-04-17 13:26:50 -04001781 }
Chris Masonb18c6682007-04-17 13:26:50 -04001782out:
Chris Masonb18c6682007-04-17 13:26:50 -04001783 btrfs_free_path(path);
1784 return ret;
1785}
1786
1787static int prepare_pages(struct btrfs_root *root,
Chris Mason75dfe392007-03-29 11:56:46 -04001788 struct file *file,
1789 struct page **pages,
1790 size_t num_pages,
1791 loff_t pos,
Chris Mason2932f3e2007-04-10 14:22:02 -04001792 unsigned long first_index,
1793 unsigned long last_index,
Chris Mason6567e832007-04-16 09:22:45 -04001794 size_t write_bytes,
1795 u64 alloc_extent_start)
Chris Mason75dfe392007-03-29 11:56:46 -04001796{
1797 int i;
1798 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1799 struct inode *inode = file->f_path.dentry->d_inode;
1800 int offset;
1801 int err = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001802 int this_write;
Chris Mason6567e832007-04-16 09:22:45 -04001803 struct buffer_head *bh;
1804 struct buffer_head *head;
Chris Mason75dfe392007-03-29 11:56:46 -04001805 loff_t isize = i_size_read(inode);
1806
1807 memset(pages, 0, num_pages * sizeof(struct page *));
1808
1809 for (i = 0; i < num_pages; i++) {
1810 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1811 if (!pages[i]) {
1812 err = -ENOMEM;
1813 goto failed_release;
1814 }
Chris Mason35b7e472007-05-02 15:53:43 -04001815 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
1816 wait_on_page_writeback(pages[i]);
Chris Mason75dfe392007-03-29 11:56:46 -04001817 offset = pos & (PAGE_CACHE_SIZE -1);
1818 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Mason35b7e472007-05-02 15:53:43 -04001819 if (!page_has_buffers(pages[i])) {
1820 create_empty_buffers(pages[i],
1821 root->fs_info->sb->s_blocksize,
1822 (1 << BH_Uptodate));
1823 }
Chris Mason6567e832007-04-16 09:22:45 -04001824 head = page_buffers(pages[i]);
1825 bh = head;
1826 do {
1827 err = btrfs_map_bh_to_logical(root, bh,
1828 alloc_extent_start);
1829 BUG_ON(err);
1830 if (err)
1831 goto failed_truncate;
1832 bh = bh->b_this_page;
Chris Mason236454df2007-04-19 13:37:44 -04001833 if (alloc_extent_start)
1834 alloc_extent_start++;
Chris Mason6567e832007-04-16 09:22:45 -04001835 } while (bh != head);
Chris Mason75dfe392007-03-29 11:56:46 -04001836 pos += this_write;
Chris Mason75dfe392007-03-29 11:56:46 -04001837 WARN_ON(this_write > write_bytes);
1838 write_bytes -= this_write;
1839 }
1840 return 0;
1841
1842failed_release:
1843 btrfs_drop_pages(pages, num_pages);
1844 return err;
1845
1846failed_truncate:
1847 btrfs_drop_pages(pages, num_pages);
1848 if (pos > isize)
1849 vmtruncate(inode, isize);
1850 return err;
1851}
1852
1853static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1854 size_t count, loff_t *ppos)
1855{
1856 loff_t pos;
1857 size_t num_written = 0;
1858 int err = 0;
1859 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001860 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001861 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonb18c6682007-04-17 13:26:50 -04001862 struct page *pages[8];
Chris Mason35b7e472007-05-02 15:53:43 -04001863 struct page *pinned[2];
Chris Mason2932f3e2007-04-10 14:22:02 -04001864 unsigned long first_index;
1865 unsigned long last_index;
Chris Mason6567e832007-04-16 09:22:45 -04001866 u64 start_pos;
1867 u64 num_blocks;
1868 u64 alloc_extent_start;
Chris Masonde428b62007-05-18 13:28:27 -04001869 u64 hint_block;
Chris Mason6567e832007-04-16 09:22:45 -04001870 struct btrfs_trans_handle *trans;
Chris Masonb18c6682007-04-17 13:26:50 -04001871 struct btrfs_key ins;
Chris Mason35b7e472007-05-02 15:53:43 -04001872 pinned[0] = NULL;
1873 pinned[1] = NULL;
Chris Mason75dfe392007-03-29 11:56:46 -04001874 if (file->f_flags & O_DIRECT)
1875 return -EINVAL;
1876 pos = *ppos;
Chris Mason75dfe392007-03-29 11:56:46 -04001877 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1878 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1879 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1880 if (err)
1881 goto out;
1882 if (count == 0)
1883 goto out;
1884 err = remove_suid(file->f_path.dentry);
1885 if (err)
1886 goto out;
1887 file_update_time(file);
Chris Masona429e512007-04-18 16:15:28 -04001888
1889 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1890 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1891 inode->i_blkbits;
1892
Chris Mason75dfe392007-03-29 11:56:46 -04001893 mutex_lock(&inode->i_mutex);
Chris Mason2932f3e2007-04-10 14:22:02 -04001894 first_index = pos >> PAGE_CACHE_SHIFT;
1895 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
Chris Mason6567e832007-04-16 09:22:45 -04001896
Chris Masona429e512007-04-18 16:15:28 -04001897 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1898 (pos & (PAGE_CACHE_SIZE - 1))) {
1899 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1900 if (!PageUptodate(pinned[0])) {
1901 ret = mpage_readpage(pinned[0], btrfs_get_block);
1902 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001903 wait_on_page_locked(pinned[0]);
Chris Masona429e512007-04-18 16:15:28 -04001904 } else {
1905 unlock_page(pinned[0]);
1906 }
1907 }
1908 if (first_index != last_index &&
1909 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
Chris Masonde428b62007-05-18 13:28:27 -04001910 pos + count < inode->i_size &&
Chris Masona429e512007-04-18 16:15:28 -04001911 (count & (PAGE_CACHE_SIZE - 1))) {
1912 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1913 if (!PageUptodate(pinned[1])) {
1914 ret = mpage_readpage(pinned[1], btrfs_get_block);
1915 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001916 wait_on_page_locked(pinned[1]);
Chris Masona429e512007-04-18 16:15:28 -04001917 } else {
1918 unlock_page(pinned[1]);
1919 }
1920 }
1921
Chris Mason6567e832007-04-16 09:22:45 -04001922 mutex_lock(&root->fs_info->fs_mutex);
1923 trans = btrfs_start_transaction(root, 1);
1924 if (!trans) {
1925 err = -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001926 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -04001927 goto out_unlock;
1928 }
Chris Mason31f3c992007-04-30 15:25:45 -04001929 btrfs_set_trans_block_group(trans, inode);
Chris Masona429e512007-04-18 16:15:28 -04001930 /* FIXME blocksize != 4096 */
1931 inode->i_blocks += num_blocks << 3;
Chris Masonde428b62007-05-18 13:28:27 -04001932 hint_block = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001933 if (start_pos < inode->i_size) {
Chris Masona429e512007-04-18 16:15:28 -04001934 /* FIXME blocksize != pagesize */
Chris Masonb18c6682007-04-17 13:26:50 -04001935 ret = drop_extents(trans, root, inode,
1936 start_pos,
1937 (pos + count + root->blocksize -1) &
Chris Masonde428b62007-05-18 13:28:27 -04001938 ~((u64)root->blocksize - 1), &hint_block);
Chris Mason236454df2007-04-19 13:37:44 -04001939 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001940 }
Chris Mason236454df2007-04-19 13:37:44 -04001941 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
1942 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
Chris Mason4d775672007-04-20 20:23:12 -04001943 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
Chris Masonde428b62007-05-18 13:28:27 -04001944 num_blocks, hint_block, (u64)-1,
1945 &ins, 1);
Chris Mason236454df2007-04-19 13:37:44 -04001946 BUG_ON(ret);
1947 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
Chris Masonb18c6682007-04-17 13:26:50 -04001948 start_pos, ins.objectid, ins.offset);
Chris Mason236454df2007-04-19 13:37:44 -04001949 BUG_ON(ret);
1950 } else {
1951 ins.offset = 0;
1952 ins.objectid = 0;
1953 }
Chris Masonb18c6682007-04-17 13:26:50 -04001954 BUG_ON(ret);
1955 alloc_extent_start = ins.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001956 // btrfs_update_inode_block_group(trans, inode);
Chris Masonb18c6682007-04-17 13:26:50 -04001957 ret = btrfs_end_transaction(trans, root);
Chris Mason6567e832007-04-16 09:22:45 -04001958 mutex_unlock(&root->fs_info->fs_mutex);
1959
Chris Mason75dfe392007-03-29 11:56:46 -04001960 while(count > 0) {
1961 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1962 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1963 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1964 PAGE_CACHE_SHIFT;
Chris Masonb18c6682007-04-17 13:26:50 -04001965
1966 memset(pages, 0, sizeof(pages));
1967 ret = prepare_pages(root, file, pages, num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04001968 pos, first_index, last_index,
1969 write_bytes, alloc_extent_start);
Chris Mason75dfe392007-03-29 11:56:46 -04001970 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04001971
Chris Mason6567e832007-04-16 09:22:45 -04001972 /* FIXME blocks != pagesize */
Chris Mason236454df2007-04-19 13:37:44 -04001973 if (alloc_extent_start)
1974 alloc_extent_start += num_pages;
Chris Mason75dfe392007-03-29 11:56:46 -04001975 ret = btrfs_copy_from_user(pos, num_pages,
1976 write_bytes, pages, buf);
1977 BUG_ON(ret);
1978
Chris Masonf254e522007-03-29 15:15:27 -04001979 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason70b2bef2007-04-17 15:39:32 -04001980 num_pages, pos, write_bytes);
Chris Mason75dfe392007-03-29 11:56:46 -04001981 BUG_ON(ret);
1982 btrfs_drop_pages(pages, num_pages);
1983
Chris Mason75dfe392007-03-29 11:56:46 -04001984 buf += write_bytes;
1985 count -= write_bytes;
1986 pos += write_bytes;
1987 num_written += write_bytes;
1988
1989 balance_dirty_pages_ratelimited(inode->i_mapping);
Chris Mason35b7e472007-05-02 15:53:43 -04001990 btrfs_btree_balance_dirty(root);
Chris Mason75dfe392007-03-29 11:56:46 -04001991 cond_resched();
1992 }
Chris Mason6567e832007-04-16 09:22:45 -04001993out_unlock:
Chris Mason75dfe392007-03-29 11:56:46 -04001994 mutex_unlock(&inode->i_mutex);
1995out:
Chris Masona429e512007-04-18 16:15:28 -04001996 if (pinned[0])
1997 page_cache_release(pinned[0]);
1998 if (pinned[1])
1999 page_cache_release(pinned[1]);
Chris Mason75dfe392007-03-29 11:56:46 -04002000 *ppos = pos;
2001 current->backing_dev_info = NULL;
Chris Masona429e512007-04-18 16:15:28 -04002002 mark_inode_dirty(inode);
Chris Mason75dfe392007-03-29 11:56:46 -04002003 return num_written ? num_written : err;
2004}
2005
Chris Masonf254e522007-03-29 15:15:27 -04002006static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
2007 unsigned long offset, unsigned long size)
2008{
2009 char *kaddr;
2010 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04002011 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04002012
2013 if (size > count)
2014 size = count;
2015
2016 if (!PageChecked(page)) {
2017 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04002018 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason236454df2007-04-19 13:37:44 -04002019
Chris Masonf254e522007-03-29 15:15:27 -04002020 int ret = btrfs_csum_verify_file_block(root,
Chris Mason236454df2007-04-19 13:37:44 -04002021 page->mapping->host->i_ino,
2022 page->index << PAGE_CACHE_SHIFT,
2023 kmap(page), PAGE_CACHE_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -04002024 if (ret) {
2025 printk("failed to verify ino %lu page %lu\n",
2026 page->mapping->host->i_ino,
2027 page->index);
2028 memset(page_address(page), 0, PAGE_CACHE_SIZE);
2029 }
2030 SetPageChecked(page);
2031 kunmap(page);
2032 }
2033 /*
2034 * Faults on the destination of a read are common, so do it before
2035 * taking the kmap.
2036 */
2037 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2038 kaddr = kmap_atomic(page, KM_USER0);
2039 left = __copy_to_user_inatomic(desc->arg.buf,
2040 kaddr + offset, size);
2041 kunmap_atomic(kaddr, KM_USER0);
2042 if (left == 0)
2043 goto success;
2044 }
2045
2046 /* Do it the slow way */
2047 kaddr = kmap(page);
2048 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2049 kunmap(page);
2050
2051 if (left) {
2052 size -= left;
2053 desc->error = -EFAULT;
2054 }
2055success:
2056 desc->count = count - size;
2057 desc->written += size;
2058 desc->arg.buf += size;
2059 return size;
2060}
2061
2062/**
2063 * btrfs_file_aio_read - filesystem read routine
2064 * @iocb: kernel I/O control block
2065 * @iov: io vector request
2066 * @nr_segs: number of segments in the iovec
2067 * @pos: current file position
2068 */
2069static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2070 unsigned long nr_segs, loff_t pos)
2071{
2072 struct file *filp = iocb->ki_filp;
2073 ssize_t retval;
2074 unsigned long seg;
2075 size_t count;
2076 loff_t *ppos = &iocb->ki_pos;
2077
2078 count = 0;
2079 for (seg = 0; seg < nr_segs; seg++) {
2080 const struct iovec *iv = &iov[seg];
2081
2082 /*
2083 * If any segment has a negative length, or the cumulative
2084 * length ever wraps negative then return -EINVAL.
2085 */
2086 count += iv->iov_len;
2087 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2088 return -EINVAL;
2089 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2090 continue;
2091 if (seg == 0)
2092 return -EFAULT;
2093 nr_segs = seg;
2094 count -= iv->iov_len; /* This segment is no good */
2095 break;
2096 }
2097 retval = 0;
2098 if (count) {
2099 for (seg = 0; seg < nr_segs; seg++) {
2100 read_descriptor_t desc;
2101
2102 desc.written = 0;
2103 desc.arg.buf = iov[seg].iov_base;
2104 desc.count = iov[seg].iov_len;
2105 if (desc.count == 0)
2106 continue;
2107 desc.error = 0;
2108 do_generic_file_read(filp, ppos, &desc,
2109 btrfs_read_actor);
2110 retval += desc.written;
2111 if (desc.error) {
2112 retval = retval ?: desc.error;
2113 break;
2114 }
2115 }
2116 }
2117 return retval;
2118}
2119
Chris Mason2619ba12007-04-10 16:58:11 -04002120static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2121{
2122 struct btrfs_trans_handle *trans;
2123 struct btrfs_key key;
2124 struct btrfs_root_item root_item;
2125 struct btrfs_inode_item *inode_item;
2126 struct buffer_head *subvol;
2127 struct btrfs_leaf *leaf;
2128 struct btrfs_root *new_root;
2129 struct inode *inode;
Chris Mason31f3c992007-04-30 15:25:45 -04002130 struct inode *dir;
Chris Mason2619ba12007-04-10 16:58:11 -04002131 int ret;
2132 u64 objectid;
2133 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2134
2135 mutex_lock(&root->fs_info->fs_mutex);
2136 trans = btrfs_start_transaction(root, 1);
2137 BUG_ON(!trans);
2138
Chris Mason31f3c992007-04-30 15:25:45 -04002139 subvol = btrfs_alloc_free_block(trans, root, 0);
Chris Mason5e828492007-04-23 19:10:49 -04002140 if (subvol == NULL)
2141 return -ENOSPC;
Chris Mason2619ba12007-04-10 16:58:11 -04002142 leaf = btrfs_buffer_leaf(subvol);
2143 btrfs_set_header_nritems(&leaf->header, 0);
2144 btrfs_set_header_level(&leaf->header, 0);
Chris Mason7eccb902007-04-11 15:53:25 -04002145 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002146 btrfs_set_header_generation(&leaf->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04002147 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
Chris Mason2619ba12007-04-10 16:58:11 -04002148 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2149 sizeof(leaf->header.fsid));
Chris Mason4d775672007-04-20 20:23:12 -04002150 mark_buffer_dirty(subvol);
Chris Mason2619ba12007-04-10 16:58:11 -04002151
2152 inode_item = &root_item.inode;
2153 memset(inode_item, 0, sizeof(*inode_item));
2154 btrfs_set_inode_generation(inode_item, 1);
2155 btrfs_set_inode_size(inode_item, 3);
2156 btrfs_set_inode_nlink(inode_item, 1);
2157 btrfs_set_inode_nblocks(inode_item, 1);
2158 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2159
Chris Mason7eccb902007-04-11 15:53:25 -04002160 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002161 btrfs_set_root_refs(&root_item, 1);
Chris Mason5e828492007-04-23 19:10:49 -04002162 brelse(subvol);
2163 subvol = NULL;
Chris Mason2619ba12007-04-10 16:58:11 -04002164
Chris Mason2619ba12007-04-10 16:58:11 -04002165 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2166 0, &objectid);
2167 BUG_ON(ret);
2168
2169 btrfs_set_root_dirid(&root_item, new_dirid);
2170
2171 key.objectid = objectid;
2172 key.offset = 1;
2173 key.flags = 0;
2174 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2175 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2176 &root_item);
2177 BUG_ON(ret);
2178
2179 /*
2180 * insert the directory item
2181 */
2182 key.offset = (u64)-1;
Chris Mason31f3c992007-04-30 15:25:45 -04002183 dir = root->fs_info->sb->s_root->d_inode;
Chris Mason2619ba12007-04-10 16:58:11 -04002184 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Chris Mason31f3c992007-04-30 15:25:45 -04002185 name, namelen, dir->i_ino, &key, 0);
Chris Mason2619ba12007-04-10 16:58:11 -04002186 BUG_ON(ret);
2187
2188 ret = btrfs_commit_transaction(trans, root);
2189 BUG_ON(ret);
2190
2191 new_root = btrfs_read_fs_root(root->fs_info, &key);
2192 BUG_ON(!new_root);
2193
2194 trans = btrfs_start_transaction(new_root, 1);
2195 BUG_ON(!trans);
2196
Chris Mason31f3c992007-04-30 15:25:45 -04002197 inode = btrfs_new_inode(trans, new_root, new_dirid,
2198 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason2619ba12007-04-10 16:58:11 -04002199 inode->i_op = &btrfs_dir_inode_operations;
2200 inode->i_fop = &btrfs_dir_file_operations;
2201
2202 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2203 BUG_ON(ret);
2204
2205 inode->i_nlink = 1;
2206 inode->i_size = 6;
2207 ret = btrfs_update_inode(trans, new_root, inode);
2208 BUG_ON(ret);
2209
2210 ret = btrfs_commit_transaction(trans, new_root);
2211 BUG_ON(ret);
2212
2213 iput(inode);
2214
2215 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04002216 btrfs_btree_balance_dirty(root);
Chris Mason2619ba12007-04-10 16:58:11 -04002217 return 0;
2218}
2219
Chris Masonc5739bb2007-04-10 09:27:04 -04002220static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2221{
2222 struct btrfs_trans_handle *trans;
2223 struct btrfs_key key;
2224 struct btrfs_root_item new_root_item;
2225 int ret;
2226 u64 objectid;
2227
Chris Mason2619ba12007-04-10 16:58:11 -04002228 if (!root->ref_cows)
2229 return -EINVAL;
2230
Chris Masonc5739bb2007-04-10 09:27:04 -04002231 mutex_lock(&root->fs_info->fs_mutex);
2232 trans = btrfs_start_transaction(root, 1);
2233 BUG_ON(!trans);
2234
2235 ret = btrfs_update_inode(trans, root, root->inode);
2236 BUG_ON(ret);
2237
Chris Mason1b05da22007-04-10 12:13:09 -04002238 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2239 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04002240 BUG_ON(ret);
2241
Chris Masonc5739bb2007-04-10 09:27:04 -04002242 memcpy(&new_root_item, &root->root_item,
2243 sizeof(new_root_item));
2244
2245 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002246 key.offset = 1;
2247 key.flags = 0;
2248 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason7eccb902007-04-11 15:53:25 -04002249 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
Chris Masonc5739bb2007-04-10 09:27:04 -04002250
2251 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2252 &new_root_item);
2253 BUG_ON(ret);
2254
Chris Masonc5739bb2007-04-10 09:27:04 -04002255 /*
2256 * insert the directory item
2257 */
2258 key.offset = (u64)-1;
2259 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2260 name, namelen,
2261 root->fs_info->sb->s_root->d_inode->i_ino,
2262 &key, 0);
2263
2264 BUG_ON(ret);
2265
2266 ret = btrfs_inc_root_ref(trans, root);
2267 BUG_ON(ret);
2268
2269 ret = btrfs_commit_transaction(trans, root);
2270 BUG_ON(ret);
2271 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04002272 btrfs_btree_balance_dirty(root);
Chris Masonc5739bb2007-04-10 09:27:04 -04002273 return 0;
2274}
2275
Chris Mason8352d8a2007-04-12 10:43:05 -04002276static int add_disk(struct btrfs_root *root, char *name, int namelen)
2277{
2278 struct block_device *bdev;
2279 struct btrfs_path *path;
2280 struct super_block *sb = root->fs_info->sb;
2281 struct btrfs_root *dev_root = root->fs_info->dev_root;
2282 struct btrfs_trans_handle *trans;
2283 struct btrfs_device_item *dev_item;
2284 struct btrfs_key key;
2285 u16 item_size;
2286 u64 num_blocks;
2287 u64 new_blocks;
Chris Masonb4100d62007-04-12 12:14:00 -04002288 u64 device_id;
Chris Mason8352d8a2007-04-12 10:43:05 -04002289 int ret;
Chris Masonb4100d62007-04-12 12:14:00 -04002290
Chris Mason8352d8a2007-04-12 10:43:05 -04002291printk("adding disk %s\n", name);
2292 path = btrfs_alloc_path();
2293 if (!path)
2294 return -ENOMEM;
2295 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2296 bdev = open_bdev_excl(name, O_RDWR, sb);
2297 if (IS_ERR(bdev)) {
2298 ret = PTR_ERR(bdev);
2299printk("open bdev excl failed ret %d\n", ret);
2300 goto out_nolock;
2301 }
2302 set_blocksize(bdev, sb->s_blocksize);
2303 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2304 key.objectid = num_blocks;
2305 key.offset = new_blocks;
2306 key.flags = 0;
2307 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2308
2309 mutex_lock(&dev_root->fs_info->fs_mutex);
2310 trans = btrfs_start_transaction(dev_root, 1);
2311 item_size = sizeof(*dev_item) + namelen;
2312printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2313 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2314 if (ret) {
2315printk("insert failed %d\n", ret);
2316 close_bdev_excl(bdev);
2317 if (ret > 0)
2318 ret = -EEXIST;
2319 goto out;
2320 }
2321 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2322 path->slots[0], struct btrfs_device_item);
2323 btrfs_set_device_pathlen(dev_item, namelen);
2324 memcpy(dev_item + 1, name, namelen);
Chris Masonb4100d62007-04-12 12:14:00 -04002325
2326 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2327 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2328 btrfs_set_device_id(dev_item, device_id);
Chris Mason8352d8a2007-04-12 10:43:05 -04002329 mark_buffer_dirty(path->nodes[0]);
2330
Chris Masonb4100d62007-04-12 12:14:00 -04002331 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2332 new_blocks);
Chris Mason8352d8a2007-04-12 10:43:05 -04002333
2334 if (!ret) {
2335 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2336 num_blocks + new_blocks);
2337 i_size_write(root->fs_info->btree_inode,
2338 (num_blocks + new_blocks) <<
2339 root->fs_info->btree_inode->i_blkbits);
2340 }
2341
2342out:
2343 ret = btrfs_commit_transaction(trans, dev_root);
2344 BUG_ON(ret);
2345 mutex_unlock(&root->fs_info->fs_mutex);
2346out_nolock:
2347 btrfs_free_path(path);
Chris Mason35b7e472007-05-02 15:53:43 -04002348 btrfs_btree_balance_dirty(root);
Chris Mason8352d8a2007-04-12 10:43:05 -04002349
2350 return ret;
2351}
2352
Chris Masonc5739bb2007-04-10 09:27:04 -04002353static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2354 cmd, unsigned long arg)
2355{
2356 struct btrfs_root *root = BTRFS_I(inode)->root;
2357 struct btrfs_ioctl_vol_args vol_args;
Chris Mason8352d8a2007-04-12 10:43:05 -04002358 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -04002359 struct btrfs_dir_item *di;
Chris Masonc5739bb2007-04-10 09:27:04 -04002360 int namelen;
Chris Mason2619ba12007-04-10 16:58:11 -04002361 struct btrfs_path *path;
2362 u64 root_dirid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002363
Chris Masonc5739bb2007-04-10 09:27:04 -04002364 switch (cmd) {
2365 case BTRFS_IOC_SNAP_CREATE:
2366 if (copy_from_user(&vol_args,
2367 (struct btrfs_ioctl_vol_args __user *)arg,
2368 sizeof(vol_args)))
2369 return -EFAULT;
2370 namelen = strlen(vol_args.name);
2371 if (namelen > BTRFS_VOL_NAME_MAX)
2372 return -EINVAL;
Chris Mason2619ba12007-04-10 16:58:11 -04002373 path = btrfs_alloc_path();
2374 if (!path)
2375 return -ENOMEM;
Chris Mason2d13d8d2007-04-10 20:07:20 -04002376 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Chris Mason2619ba12007-04-10 16:58:11 -04002377 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason7e381802007-04-19 15:36:27 -04002378 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
Chris Mason2619ba12007-04-10 16:58:11 -04002379 path, root_dirid,
2380 vol_args.name, namelen, 0);
2381 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2d13d8d2007-04-10 20:07:20 -04002382 btrfs_free_path(path);
Chris Mason7e381802007-04-19 15:36:27 -04002383 if (di && !IS_ERR(di))
Chris Mason2619ba12007-04-10 16:58:11 -04002384 return -EEXIST;
2385
2386 if (root == root->fs_info->tree_root)
2387 ret = create_subvol(root, vol_args.name, namelen);
2388 else
2389 ret = create_snapshot(root, vol_args.name, namelen);
Chris Masonc5739bb2007-04-10 09:27:04 -04002390 WARN_ON(ret);
2391 break;
Chris Mason8352d8a2007-04-12 10:43:05 -04002392 case BTRFS_IOC_ADD_DISK:
2393 if (copy_from_user(&vol_args,
2394 (struct btrfs_ioctl_vol_args __user *)arg,
2395 sizeof(vol_args)))
2396 return -EFAULT;
2397 namelen = strlen(vol_args.name);
2398 if (namelen > BTRFS_VOL_NAME_MAX)
2399 return -EINVAL;
2400 vol_args.name[namelen] = '\0';
2401 ret = add_disk(root, vol_args.name, namelen);
2402 break;
Chris Masonc5739bb2007-04-10 09:27:04 -04002403 default:
2404 return -ENOTTY;
2405 }
Chris Mason8352d8a2007-04-12 10:43:05 -04002406 return ret;
Chris Masonc5739bb2007-04-10 09:27:04 -04002407}
2408
Chris Mason2c90e5d2007-04-02 10:50:19 -04002409static struct kmem_cache *btrfs_inode_cachep;
2410struct kmem_cache *btrfs_trans_handle_cachep;
2411struct kmem_cache *btrfs_transaction_cachep;
2412struct kmem_cache *btrfs_bit_radix_cachep;
2413struct kmem_cache *btrfs_path_cachep;
2414
2415/*
2416 * Called inside transaction, so use GFP_NOFS
2417 */
2418static struct inode *btrfs_alloc_inode(struct super_block *sb)
2419{
2420 struct btrfs_inode *ei;
2421
2422 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2423 if (!ei)
2424 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002425 return &ei->vfs_inode;
2426}
2427
2428static void btrfs_destroy_inode(struct inode *inode)
2429{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002430 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04002431 WARN_ON(inode->i_data.nrpages);
2432
Chris Mason2c90e5d2007-04-02 10:50:19 -04002433 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2434}
2435
2436static void init_once(void * foo, struct kmem_cache * cachep,
2437 unsigned long flags)
2438{
2439 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2440
2441 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2442 SLAB_CTOR_CONSTRUCTOR) {
2443 inode_init_once(&ei->vfs_inode);
2444 }
2445}
2446
2447static int init_inodecache(void)
2448{
2449 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2450 sizeof(struct btrfs_inode),
2451 0, (SLAB_RECLAIM_ACCOUNT|
2452 SLAB_MEM_SPREAD),
2453 init_once, NULL);
2454 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2455 sizeof(struct btrfs_trans_handle),
2456 0, (SLAB_RECLAIM_ACCOUNT|
2457 SLAB_MEM_SPREAD),
2458 NULL, NULL);
2459 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2460 sizeof(struct btrfs_transaction),
2461 0, (SLAB_RECLAIM_ACCOUNT|
2462 SLAB_MEM_SPREAD),
2463 NULL, NULL);
2464 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2465 sizeof(struct btrfs_transaction),
2466 0, (SLAB_RECLAIM_ACCOUNT|
2467 SLAB_MEM_SPREAD),
2468 NULL, NULL);
2469 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2470 256,
2471 0, (SLAB_RECLAIM_ACCOUNT|
2472 SLAB_MEM_SPREAD |
2473 SLAB_DESTROY_BY_RCU),
2474 NULL, NULL);
2475 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2476 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2477 return -ENOMEM;
2478 return 0;
2479}
2480
2481static void destroy_inodecache(void)
2482{
2483 kmem_cache_destroy(btrfs_inode_cachep);
2484 kmem_cache_destroy(btrfs_trans_handle_cachep);
2485 kmem_cache_destroy(btrfs_transaction_cachep);
2486 kmem_cache_destroy(btrfs_bit_radix_cachep);
2487 kmem_cache_destroy(btrfs_path_cachep);
2488}
2489
Chris Mason2e635a22007-03-21 11:12:56 -04002490static int btrfs_get_sb(struct file_system_type *fs_type,
2491 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2492{
2493 return get_sb_bdev(fs_type, flags, dev_name, data,
2494 btrfs_fill_super, mnt);
2495}
2496
Chris Mason236454df2007-04-19 13:37:44 -04002497static int btrfs_getattr(struct vfsmount *mnt,
2498 struct dentry *dentry, struct kstat *stat)
2499{
2500 struct inode *inode = dentry->d_inode;
2501 generic_fillattr(inode, stat);
2502 stat->blksize = 256 * 1024;
2503 return 0;
2504}
2505
Chris Mason8fd17792007-04-19 21:01:03 -04002506static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2507{
2508 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2509 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2510
2511 buf->f_namelen = BTRFS_NAME_LEN;
2512 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2513 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2514 buf->f_bavail = buf->f_bfree;
2515 buf->f_bsize = dentry->d_sb->s_blocksize;
2516 buf->f_type = BTRFS_SUPER_MAGIC;
2517 return 0;
2518}
Chris Masonb5133862007-04-24 11:52:22 -04002519
Chris Mason2e635a22007-03-21 11:12:56 -04002520static struct file_system_type btrfs_fs_type = {
2521 .owner = THIS_MODULE,
2522 .name = "btrfs",
2523 .get_sb = btrfs_get_sb,
2524 .kill_sb = kill_block_super,
2525 .fs_flags = FS_REQUIRES_DEV,
2526};
2527
Chris Masone20d96d2007-03-22 12:13:20 -04002528static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -04002529 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002530 .put_super = btrfs_put_super,
2531 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04002532 .write_super = btrfs_write_super,
2533 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04002534 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -04002535 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002536 .alloc_inode = btrfs_alloc_inode,
2537 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -04002538 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -04002539};
2540
2541static struct inode_operations btrfs_dir_inode_operations = {
2542 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04002543 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04002544 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04002545 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04002546 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04002547};
2548
Chris Masond6e4a422007-04-06 15:37:36 -04002549static struct inode_operations btrfs_dir_ro_inode_operations = {
2550 .lookup = btrfs_lookup,
2551};
2552
Chris Masone20d96d2007-03-22 12:13:20 -04002553static struct file_operations btrfs_dir_file_operations = {
2554 .llseek = generic_file_llseek,
2555 .read = generic_read_dir,
2556 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04002557 .ioctl = btrfs_ioctl,
Chris Masone20d96d2007-03-22 12:13:20 -04002558};
2559
Chris Masondee26a92007-03-26 16:00:06 -04002560static struct address_space_operations btrfs_aops = {
2561 .readpage = btrfs_readpage,
Chris Masondee26a92007-03-26 16:00:06 -04002562 .writepage = btrfs_writepage,
2563 .sync_page = block_sync_page,
2564 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04002565 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04002566};
2567
2568static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04002569 .truncate = btrfs_truncate,
Chris Mason236454df2007-04-19 13:37:44 -04002570 .getattr = btrfs_getattr,
Chris Masondee26a92007-03-26 16:00:06 -04002571};
2572
2573static struct file_operations btrfs_file_operations = {
2574 .llseek = generic_file_llseek,
2575 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04002576 .aio_read = btrfs_file_aio_read,
2577 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04002578 .mmap = generic_file_mmap,
2579 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04002580 .ioctl = btrfs_ioctl,
Chris Mason8fd17792007-04-19 21:01:03 -04002581 .fsync = btrfs_sync_file,
Chris Masondee26a92007-03-26 16:00:06 -04002582};
Chris Masone20d96d2007-03-22 12:13:20 -04002583
Chris Mason2e635a22007-03-21 11:12:56 -04002584static int __init init_btrfs_fs(void)
2585{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002586 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04002587 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04002588 err = init_inodecache();
2589 if (err)
2590 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04002591 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2592 err = subsystem_register(&btrfs_subsys);
2593 if (err)
2594 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04002595 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002596out:
2597 destroy_inodecache();
2598 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002599}
2600
2601static void __exit exit_btrfs_fs(void)
2602{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002603 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04002604 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04002605 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04002606 printk("btrfs unloaded\n");
2607}
2608
2609module_init(init_btrfs_fs)
2610module_exit(exit_btrfs_fs)
2611
2612MODULE_LICENSE("GPL");