Btrfs: sparse files!
[deliverable/linux.git] / fs / btrfs / super.c
CommitLineData
2e635a27 1#include <linux/module.h>
e20d96d6 2#include <linux/buffer_head.h>
2e635a27
CM
3#include <linux/fs.h>
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
6#include <linux/time.h>
7#include <linux/init.h>
8#include <linux/string.h>
9#include <linux/smp_lock.h>
10#include <linux/backing-dev.h>
dee26a9f 11#include <linux/mpage.h>
75dfe396
CM
12#include <linux/swap.h>
13#include <linux/writeback.h>
8fd17795 14#include <linux/statfs.h>
2e635a27 15#include "ctree.h"
e20d96d6 16#include "disk-io.h"
d5719762 17#include "transaction.h"
2c90e5d6 18#include "btrfs_inode.h"
c5739bba 19#include "ioctl.h"
3a686375 20#include "print-tree.h"
2e635a27 21
c5739bba
CM
22struct btrfs_iget_args {
23 u64 ino;
24 struct btrfs_root *root;
25};
26
2e635a27 27#define BTRFS_SUPER_MAGIC 0x9123682E
e20d96d6
CM
28
29static struct inode_operations btrfs_dir_inode_operations;
2b8d99a7 30static struct inode_operations btrfs_symlink_inode_operations;
d6e4a428 31static struct inode_operations btrfs_dir_ro_inode_operations;
e20d96d6
CM
32static struct super_operations btrfs_super_ops;
33static struct file_operations btrfs_dir_file_operations;
dee26a9f
CM
34static struct inode_operations btrfs_file_inode_operations;
35static struct address_space_operations btrfs_aops;
2b8d99a7 36static struct address_space_operations btrfs_symlink_aops;
dee26a9f 37static struct file_operations btrfs_file_operations;
e20d96d6 38
e20d96d6 39static void btrfs_read_locked_inode(struct inode *inode)
2e635a27 40{
5caf2a00 41 struct btrfs_path *path;
e20d96d6 42 struct btrfs_inode_item *inode_item;
d6e4a428
CM
43 struct btrfs_root *root = BTRFS_I(inode)->root;
44 struct btrfs_key location;
31f3c99b
CM
45 struct btrfs_block_group_cache *alloc_group;
46 u64 alloc_group_block;
e20d96d6 47 int ret;
f4b9aa8d 48
5caf2a00
CM
49 path = btrfs_alloc_path();
50 BUG_ON(!path);
51 btrfs_init_path(path);
f4b9aa8d
CM
52 mutex_lock(&root->fs_info->fs_mutex);
53
d6e4a428
CM
54 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
55 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
e20d96d6 56 if (ret) {
7cfcc17e 57 btrfs_free_path(path);
d6e4a428 58 goto make_bad;
e20d96d6 59 }
5caf2a00
CM
60 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
61 path->slots[0],
e20d96d6 62 struct btrfs_inode_item);
2e635a27 63
e20d96d6
CM
64 inode->i_mode = btrfs_inode_mode(inode_item);
65 inode->i_nlink = btrfs_inode_nlink(inode_item);
66 inode->i_uid = btrfs_inode_uid(inode_item);
67 inode->i_gid = btrfs_inode_gid(inode_item);
68 inode->i_size = btrfs_inode_size(inode_item);
69 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
70 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
71 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
72 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
73 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
74 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
75 inode->i_blocks = btrfs_inode_nblocks(inode_item);
76 inode->i_generation = btrfs_inode_generation(inode_item);
31f3c99b
CM
77 alloc_group_block = btrfs_inode_block_group(inode_item);
78 ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
79 (void **)&alloc_group,
80 alloc_group_block, 1);
81 BUG_ON(!ret);
82 BTRFS_I(inode)->block_group = alloc_group;
5caf2a00 83
5caf2a00
CM
84 btrfs_free_path(path);
85 inode_item = NULL;
86
f4b9aa8d 87 mutex_unlock(&root->fs_info->fs_mutex);
1b05da2e 88
e20d96d6
CM
89 switch (inode->i_mode & S_IFMT) {
90#if 0
91 default:
92 init_special_inode(inode, inode->i_mode,
93 btrfs_inode_rdev(inode_item));
94 break;
95#endif
96 case S_IFREG:
dee26a9f
CM
97 inode->i_mapping->a_ops = &btrfs_aops;
98 inode->i_fop = &btrfs_file_operations;
99 inode->i_op = &btrfs_file_inode_operations;
e20d96d6
CM
100 break;
101 case S_IFDIR:
e20d96d6 102 inode->i_fop = &btrfs_dir_file_operations;
d6e4a428
CM
103 if (root == root->fs_info->tree_root)
104 inode->i_op = &btrfs_dir_ro_inode_operations;
105 else
106 inode->i_op = &btrfs_dir_inode_operations;
e20d96d6
CM
107 break;
108 case S_IFLNK:
2b8d99a7
CM
109 inode->i_op = &btrfs_symlink_inode_operations;
110 inode->i_mapping->a_ops = &btrfs_symlink_aops;
e20d96d6 111 break;
2e635a27 112 }
e20d96d6 113 return;
d6e4a428
CM
114
115make_bad:
116 btrfs_release_path(root, path);
117 btrfs_free_path(path);
118 mutex_unlock(&root->fs_info->fs_mutex);
119 make_bad_inode(inode);
2e635a27
CM
120}
121
f68cad0f
CM
122static void fill_inode_item(struct btrfs_inode_item *item,
123 struct inode *inode)
124{
125 btrfs_set_inode_uid(item, inode->i_uid);
126 btrfs_set_inode_gid(item, inode->i_gid);
127 btrfs_set_inode_size(item, inode->i_size);
128 btrfs_set_inode_mode(item, inode->i_mode);
129 btrfs_set_inode_nlink(item, inode->i_nlink);
130 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
131 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
132 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
133 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
134 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
135 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
136 btrfs_set_inode_nblocks(item, inode->i_blocks);
137 btrfs_set_inode_generation(item, inode->i_generation);
31f3c99b
CM
138 btrfs_set_inode_block_group(item,
139 BTRFS_I(inode)->block_group->key.objectid);
f68cad0f
CM
140}
141
f68cad0f
CM
142static int btrfs_update_inode(struct btrfs_trans_handle *trans,
143 struct btrfs_root *root,
144 struct inode *inode)
145{
146 struct btrfs_inode_item *inode_item;
147 struct btrfs_path *path;
148 int ret;
149
150 path = btrfs_alloc_path();
151 BUG_ON(!path);
152 btrfs_init_path(path);
153 ret = btrfs_lookup_inode(trans, root, path,
154 &BTRFS_I(inode)->location, 1);
155 if (ret) {
156 if (ret > 0)
157 ret = -ENOENT;
158 goto failed;
159 }
160
161 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
162 path->slots[0],
163 struct btrfs_inode_item);
164
165 fill_inode_item(inode_item, inode);
166 btrfs_mark_buffer_dirty(path->nodes[0]);
167 ret = 0;
168failed:
169 btrfs_release_path(root, path);
170 btrfs_free_path(path);
171 return ret;
172}
173
174
5f443fd2
CM
175static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
176 struct btrfs_root *root,
177 struct inode *dir,
178 struct dentry *dentry)
134e9731 179{
5caf2a00 180 struct btrfs_path *path;
134e9731
CM
181 const char *name = dentry->d_name.name;
182 int name_len = dentry->d_name.len;
7e38180e 183 int ret = 0;
134e9731
CM
184 u64 objectid;
185 struct btrfs_dir_item *di;
186
5caf2a00
CM
187 path = btrfs_alloc_path();
188 BUG_ON(!path);
189 btrfs_init_path(path);
7e38180e 190 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
134e9731 191 name, name_len, -1);
7e38180e
CM
192 if (IS_ERR(di)) {
193 ret = PTR_ERR(di);
134e9731 194 goto err;
7e38180e
CM
195 }
196 if (!di) {
134e9731
CM
197 ret = -ENOENT;
198 goto err;
199 }
d6e4a428 200 objectid = btrfs_disk_key_objectid(&di->location);
7e38180e
CM
201 ret = btrfs_delete_one_dir_name(trans, root, path, di);
202 BUG_ON(ret);
203 btrfs_release_path(root, path);
134e9731 204
7e38180e
CM
205 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
206 objectid, name, name_len, -1);
207 if (IS_ERR(di)) {
208 ret = PTR_ERR(di);
209 goto err;
210 }
211 if (!di) {
212 ret = -ENOENT;
213 goto err;
214 }
215 ret = btrfs_delete_one_dir_name(trans, root, path, di);
5f26f772
CM
216 BUG_ON(ret);
217
134e9731
CM
218 dentry->d_inode->i_ctime = dir->i_ctime;
219err:
5caf2a00 220 btrfs_free_path(path);
f68cad0f 221 if (!ret) {
5f26f772 222 dir->i_size -= name_len * 2;
f68cad0f
CM
223 btrfs_update_inode(trans, root, dir);
224 drop_nlink(dentry->d_inode);
225 btrfs_update_inode(trans, root, dentry->d_inode);
cd1bc465 226 dir->i_sb->s_dirt = 1;
d4dbff95 227 }
134e9731
CM
228 return ret;
229}
230
5f443fd2
CM
231static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
232{
233 struct btrfs_root *root;
234 struct btrfs_trans_handle *trans;
235 int ret;
236
d6e4a428 237 root = BTRFS_I(dir)->root;
5f443fd2
CM
238 mutex_lock(&root->fs_info->fs_mutex);
239 trans = btrfs_start_transaction(root, 1);
31f3c99b 240 btrfs_set_trans_block_group(trans, dir);
5f443fd2
CM
241 ret = btrfs_unlink_trans(trans, root, dir, dentry);
242 btrfs_end_transaction(trans, root);
243 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 244 btrfs_btree_balance_dirty(root);
5f443fd2
CM
245 return ret;
246}
247
248static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
249{
250 struct inode *inode = dentry->d_inode;
251 int err;
252 int ret;
d6e4a428 253 struct btrfs_root *root = BTRFS_I(dir)->root;
5caf2a00 254 struct btrfs_path *path;
5f443fd2
CM
255 struct btrfs_key key;
256 struct btrfs_trans_handle *trans;
5f26f772
CM
257 struct btrfs_key found_key;
258 int found_type;
5f443fd2 259 struct btrfs_leaf *leaf;
5f26f772 260 char *goodnames = "..";
5f443fd2 261
5caf2a00
CM
262 path = btrfs_alloc_path();
263 BUG_ON(!path);
264 btrfs_init_path(path);
5f443fd2
CM
265 mutex_lock(&root->fs_info->fs_mutex);
266 trans = btrfs_start_transaction(root, 1);
31f3c99b 267 btrfs_set_trans_block_group(trans, dir);
5f443fd2
CM
268 key.objectid = inode->i_ino;
269 key.offset = (u64)-1;
5f26f772
CM
270 key.flags = (u32)-1;
271 while(1) {
272 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
273 if (ret < 0) {
274 err = ret;
275 goto out;
276 }
277 BUG_ON(ret == 0);
278 if (path->slots[0] == 0) {
279 err = -ENOENT;
280 goto out;
281 }
282 path->slots[0]--;
283 leaf = btrfs_buffer_leaf(path->nodes[0]);
284 btrfs_disk_key_to_cpu(&found_key,
285 &leaf->items[path->slots[0]].key);
286 found_type = btrfs_key_type(&found_key);
287 if (found_key.objectid != inode->i_ino) {
288 err = -ENOENT;
289 goto out;
290 }
291 if ((found_type != BTRFS_DIR_ITEM_KEY &&
292 found_type != BTRFS_DIR_INDEX_KEY) ||
293 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
294 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
295 err = -ENOTEMPTY;
296 goto out;
297 }
298 ret = btrfs_del_item(trans, root, path);
299 BUG_ON(ret);
5f443fd2 300
5f26f772
CM
301 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
302 break;
303 btrfs_release_path(root, path);
5f443fd2 304 }
5f26f772 305 ret = 0;
5caf2a00 306 btrfs_release_path(root, path);
5f443fd2
CM
307
308 /* now the directory is empty */
309 err = btrfs_unlink_trans(trans, root, dir, dentry);
310 if (!err) {
311 inode->i_size = 0;
312 }
313out:
7cfcc17e
CM
314 btrfs_release_path(root, path);
315 btrfs_free_path(path);
5f443fd2
CM
316 mutex_unlock(&root->fs_info->fs_mutex);
317 ret = btrfs_end_transaction(trans, root);
35b7e476 318 btrfs_btree_balance_dirty(root);
5f443fd2
CM
319 if (ret && !err)
320 err = ret;
321 return err;
322}
323
134e9731
CM
324static int btrfs_free_inode(struct btrfs_trans_handle *trans,
325 struct btrfs_root *root,
326 struct inode *inode)
327{
5caf2a00 328 struct btrfs_path *path;
134e9731 329 int ret;
5caf2a00 330
134e9731 331 clear_inode(inode);
5caf2a00
CM
332
333 path = btrfs_alloc_path();
334 BUG_ON(!path);
335 btrfs_init_path(path);
d6e4a428
CM
336 ret = btrfs_lookup_inode(trans, root, path,
337 &BTRFS_I(inode)->location, -1);
134e9731 338 BUG_ON(ret);
5caf2a00 339 ret = btrfs_del_item(trans, root, path);
134e9731 340 BUG_ON(ret);
5caf2a00 341 btrfs_free_path(path);
134e9731
CM
342 return ret;
343}
344
de428b63
CM
345static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
346 u64 objectid)
347{
348 struct btrfs_node *node;
349 int i;
350 int nritems;
351 u64 item_objectid;
352 u64 blocknr;
353 int slot;
354 int ret;
355
356 if (!path->nodes[1])
357 return;
358 node = btrfs_buffer_node(path->nodes[1]);
359 slot = path->slots[1];
360 if (slot == 0)
361 return;
362 nritems = btrfs_header_nritems(&node->header);
363 for (i = slot - 1; i >= 0; i--) {
364 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
365 if (item_objectid != objectid)
366 break;
367 blocknr = btrfs_node_blockptr(node, i);
368 ret = readahead_tree_block(root, blocknr);
369 if (ret)
370 break;
371 }
372}
373
f4b9aa8d
CM
374static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
375 struct btrfs_root *root,
376 struct inode *inode)
377{
378 int ret;
5caf2a00 379 struct btrfs_path *path;
f4b9aa8d
CM
380 struct btrfs_key key;
381 struct btrfs_disk_key *found_key;
e06afa83 382 u32 found_type;
f4b9aa8d 383 struct btrfs_leaf *leaf;
f254e52c
CM
384 struct btrfs_file_extent_item *fi = NULL;
385 u64 extent_start = 0;
386 u64 extent_num_blocks = 0;
387 int found_extent;
f4b9aa8d 388
5caf2a00
CM
389 path = btrfs_alloc_path();
390 BUG_ON(!path);
f4b9aa8d
CM
391 /* FIXME, add redo link to tree so we don't leak on crash */
392 key.objectid = inode->i_ino;
393 key.offset = (u64)-1;
e06afa83 394 key.flags = (u32)-1;
f4b9aa8d 395 while(1) {
5caf2a00
CM
396 btrfs_init_path(path);
397 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
f4b9aa8d 398 if (ret < 0) {
f4b9aa8d
CM
399 goto error;
400 }
401 if (ret > 0) {
5caf2a00
CM
402 BUG_ON(path->slots[0] == 0);
403 path->slots[0]--;
f4b9aa8d 404 }
de428b63 405 reada_truncate(root, path, inode->i_ino);
5caf2a00
CM
406 leaf = btrfs_buffer_leaf(path->nodes[0]);
407 found_key = &leaf->items[path->slots[0]].key;
e06afa83 408 found_type = btrfs_disk_key_type(found_key);
f4b9aa8d
CM
409 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
410 break;
e06afa83
CM
411 if (found_type != BTRFS_CSUM_ITEM_KEY &&
412 found_type != BTRFS_DIR_ITEM_KEY &&
413 found_type != BTRFS_DIR_INDEX_KEY &&
414 found_type != BTRFS_EXTENT_DATA_KEY)
f4b9aa8d 415 break;
f4b9aa8d
CM
416 if (btrfs_disk_key_offset(found_key) < inode->i_size)
417 break;
236454df 418 found_extent = 0;
f254e52c 419 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
5caf2a00
CM
420 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
421 path->slots[0],
f254e52c 422 struct btrfs_file_extent_item);
236454df
CM
423 if (btrfs_file_extent_type(fi) !=
424 BTRFS_FILE_EXTENT_INLINE) {
3a686375 425 u64 num_dec;
236454df
CM
426 extent_start =
427 btrfs_file_extent_disk_blocknr(fi);
428 extent_num_blocks =
429 btrfs_file_extent_disk_num_blocks(fi);
430 /* FIXME blocksize != 4096 */
3a686375
CM
431 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
432 if (extent_start != 0) {
433 found_extent = 1;
434 inode->i_blocks -= num_dec;
435 }
236454df 436 }
f254e52c 437 }
5caf2a00 438 ret = btrfs_del_item(trans, root, path);
f4b9aa8d 439 BUG_ON(ret);
5caf2a00 440 btrfs_release_path(root, path);
f254e52c
CM
441 if (found_extent) {
442 ret = btrfs_free_extent(trans, root, extent_start,
443 extent_num_blocks, 0);
444 BUG_ON(ret);
445 }
f4b9aa8d 446 }
f4b9aa8d
CM
447 ret = 0;
448error:
5caf2a00
CM
449 btrfs_release_path(root, path);
450 btrfs_free_path(path);
cd1bc465 451 inode->i_sb->s_dirt = 1;
f4b9aa8d
CM
452 return ret;
453}
454
3a686375
CM
455static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
456{
457 struct inode *inode = dentry->d_inode;
458 int err;
459
460 err = inode_change_ok(inode, attr);
461 if (err)
462 return err;
463
464 if (S_ISREG(inode->i_mode) &&
465 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
466 struct btrfs_trans_handle *trans;
467 struct btrfs_root *root = BTRFS_I(inode)->root;
468 u64 mask = root->blocksize - 1;
469 u64 pos = (inode->i_size + mask) & ~mask;
470 u64 hole_size;
471
472 if (attr->ia_size < pos)
473 goto out;
474 hole_size = (attr->ia_size - pos + mask) & ~mask;
475 hole_size >>= inode->i_blkbits;
476
477 mutex_lock(&root->fs_info->fs_mutex);
478 trans = btrfs_start_transaction(root, 1);
479 btrfs_set_trans_block_group(trans, inode);
480 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
481 pos, 0, 0, hole_size);
482 BUG_ON(err);
483 btrfs_end_transaction(trans, root);
484 mutex_unlock(&root->fs_info->fs_mutex);
485 }
486
487 err = inode_setattr(inode, attr);
488
489out:
490 return err;
491}
134e9731
CM
492static void btrfs_delete_inode(struct inode *inode)
493{
494 struct btrfs_trans_handle *trans;
d6e4a428 495 struct btrfs_root *root = BTRFS_I(inode)->root;
f4b9aa8d
CM
496 int ret;
497
134e9731
CM
498 truncate_inode_pages(&inode->i_data, 0);
499 if (is_bad_inode(inode)) {
500 goto no_delete;
501 }
502 inode->i_size = 0;
134e9731
CM
503 mutex_lock(&root->fs_info->fs_mutex);
504 trans = btrfs_start_transaction(root, 1);
31f3c99b 505 btrfs_set_trans_block_group(trans, inode);
e06afa83
CM
506 ret = btrfs_truncate_in_trans(trans, root, inode);
507 BUG_ON(ret);
134e9731
CM
508 btrfs_free_inode(trans, root, inode);
509 btrfs_end_transaction(trans, root);
510 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 511 btrfs_btree_balance_dirty(root);
134e9731
CM
512 return;
513no_delete:
514 clear_inode(inode);
515}
516
e20d96d6 517static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
d6e4a428 518 struct btrfs_key *location)
e20d96d6
CM
519{
520 const char *name = dentry->d_name.name;
521 int namelen = dentry->d_name.len;
522 struct btrfs_dir_item *di;
5caf2a00 523 struct btrfs_path *path;
d6e4a428 524 struct btrfs_root *root = BTRFS_I(dir)->root;
e20d96d6
CM
525 int ret;
526
5caf2a00
CM
527 path = btrfs_alloc_path();
528 BUG_ON(!path);
529 btrfs_init_path(path);
7e38180e 530 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
e20d96d6 531 namelen, 0);
7e38180e 532 if (!di || IS_ERR(di)) {
d6e4a428 533 location->objectid = 0;
2c90e5d6 534 ret = 0;
e20d96d6
CM
535 goto out;
536 }
d6e4a428 537 btrfs_disk_key_to_cpu(location, &di->location);
e20d96d6 538out:
5caf2a00
CM
539 btrfs_release_path(root, path);
540 btrfs_free_path(path);
e20d96d6
CM
541 return ret;
542}
543
35b7e476 544static int fixup_tree_root_location(struct btrfs_root *root,
d6e4a428
CM
545 struct btrfs_key *location,
546 struct btrfs_root **sub_root)
547{
548 struct btrfs_path *path;
549 struct btrfs_root_item *ri;
d6e4a428
CM
550
551 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
552 return 0;
553 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
554 return 0;
555
556 path = btrfs_alloc_path();
557 BUG_ON(!path);
558 mutex_lock(&root->fs_info->fs_mutex);
559
0f7d52f4
CM
560 *sub_root = btrfs_read_fs_root(root->fs_info, location);
561 if (IS_ERR(*sub_root))
562 return PTR_ERR(*sub_root);
563
564 ri = &(*sub_root)->root_item;
d6e4a428
CM
565 location->objectid = btrfs_root_dirid(ri);
566 location->flags = 0;
567 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
568 location->offset = 0;
0f7d52f4 569
d6e4a428
CM
570 btrfs_free_path(path);
571 mutex_unlock(&root->fs_info->fs_mutex);
0f7d52f4 572 return 0;
d6e4a428
CM
573}
574
35b7e476 575static int btrfs_init_locked_inode(struct inode *inode, void *p)
c5739bba
CM
576{
577 struct btrfs_iget_args *args = p;
578 inode->i_ino = args->ino;
579 BTRFS_I(inode)->root = args->root;
580 return 0;
581}
582
35b7e476 583static int btrfs_find_actor(struct inode *inode, void *opaque)
c5739bba
CM
584{
585 struct btrfs_iget_args *args = opaque;
586 return (args->ino == inode->i_ino &&
587 args->root == BTRFS_I(inode)->root);
588}
589
35b7e476
CM
590static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
591 struct btrfs_root *root)
c5739bba
CM
592{
593 struct inode *inode;
594 struct btrfs_iget_args args;
595 args.ino = objectid;
596 args.root = root;
597
598 inode = iget5_locked(s, objectid, btrfs_find_actor,
599 btrfs_init_locked_inode,
600 (void *)&args);
601 return inode;
602}
d6e4a428 603
e20d96d6
CM
604static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
605 struct nameidata *nd)
606{
607 struct inode * inode;
d6e4a428
CM
608 struct btrfs_inode *bi = BTRFS_I(dir);
609 struct btrfs_root *root = bi->root;
610 struct btrfs_root *sub_root = root;
611 struct btrfs_key location;
e20d96d6
CM
612 int ret;
613
614 if (dentry->d_name.len > BTRFS_NAME_LEN)
615 return ERR_PTR(-ENAMETOOLONG);
22b0ebda 616 mutex_lock(&root->fs_info->fs_mutex);
d6e4a428 617 ret = btrfs_inode_by_name(dir, dentry, &location);
22b0ebda 618 mutex_unlock(&root->fs_info->fs_mutex);
e20d96d6
CM
619 if (ret < 0)
620 return ERR_PTR(ret);
621 inode = NULL;
d6e4a428
CM
622 if (location.objectid) {
623 ret = fixup_tree_root_location(root, &location, &sub_root);
624 if (ret < 0)
625 return ERR_PTR(ret);
626 if (ret > 0)
627 return ERR_PTR(-ENOENT);
c5739bba
CM
628 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
629 sub_root);
e20d96d6
CM
630 if (!inode)
631 return ERR_PTR(-EACCES);
d6e4a428 632 if (inode->i_state & I_NEW) {
0f7d52f4 633 if (sub_root != root) {
c5739bba 634printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
0f7d52f4
CM
635 igrab(inode);
636 sub_root->inode = inode;
637 }
d6e4a428
CM
638 BTRFS_I(inode)->root = sub_root;
639 memcpy(&BTRFS_I(inode)->location, &location,
640 sizeof(location));
641 btrfs_read_locked_inode(inode);
642 unlock_new_inode(inode);
643 }
e20d96d6
CM
644 }
645 return d_splice_alias(inode, dentry);
646}
647
de428b63
CM
648static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
649 u64 objectid)
090d1875
CM
650{
651 struct btrfs_node *node;
652 int i;
de428b63 653 u32 nritems;
090d1875
CM
654 u64 item_objectid;
655 u64 blocknr;
656 int slot;
de428b63 657 int ret;
090d1875
CM
658
659 if (!path->nodes[1])
660 return;
661 node = btrfs_buffer_node(path->nodes[1]);
662 slot = path->slots[1];
090d1875 663 nritems = btrfs_header_nritems(&node->header);
de428b63 664 for (i = slot + 1; i < nritems; i++) {
090d1875
CM
665 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
666 if (item_objectid != objectid)
667 break;
668 blocknr = btrfs_node_blockptr(node, i);
de428b63
CM
669 ret = readahead_tree_block(root, blocknr);
670 if (ret)
671 break;
090d1875
CM
672 }
673}
674
e20d96d6
CM
675static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
676{
677 struct inode *inode = filp->f_path.dentry->d_inode;
d6e4a428 678 struct btrfs_root *root = BTRFS_I(inode)->root;
e20d96d6
CM
679 struct btrfs_item *item;
680 struct btrfs_dir_item *di;
681 struct btrfs_key key;
5caf2a00 682 struct btrfs_path *path;
e20d96d6
CM
683 int ret;
684 u32 nritems;
685 struct btrfs_leaf *leaf;
686 int slot;
687 int advance;
688 unsigned char d_type = DT_UNKNOWN;
7f5c1516 689 int over = 0;
7e38180e
CM
690 u32 di_cur;
691 u32 di_total;
692 u32 di_len;
693 int key_type = BTRFS_DIR_INDEX_KEY;
d6e4a428
CM
694
695 /* FIXME, use a real flag for deciding about the key type */
696 if (root->fs_info->tree_root == root)
697 key_type = BTRFS_DIR_ITEM_KEY;
22b0ebda 698 mutex_lock(&root->fs_info->fs_mutex);
e20d96d6 699 key.objectid = inode->i_ino;
e20d96d6 700 key.flags = 0;
d6e4a428 701 btrfs_set_key_type(&key, key_type);
e20d96d6 702 key.offset = filp->f_pos;
5caf2a00
CM
703 path = btrfs_alloc_path();
704 btrfs_init_path(path);
705 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1b05da2e 706 if (ret < 0)
e20d96d6 707 goto err;
7f5c1516 708 advance = 0;
de428b63 709 reada_leaves(root, path, inode->i_ino);
e20d96d6 710 while(1) {
5caf2a00 711 leaf = btrfs_buffer_leaf(path->nodes[0]);
e20d96d6 712 nritems = btrfs_header_nritems(&leaf->header);
5caf2a00 713 slot = path->slots[0];
dee26a9f
CM
714 if (advance || slot >= nritems) {
715 if (slot >= nritems -1) {
de428b63 716 reada_leaves(root, path, inode->i_ino);
5caf2a00 717 ret = btrfs_next_leaf(root, path);
e20d96d6
CM
718 if (ret)
719 break;
5caf2a00 720 leaf = btrfs_buffer_leaf(path->nodes[0]);
e20d96d6 721 nritems = btrfs_header_nritems(&leaf->header);
5caf2a00 722 slot = path->slots[0];
e20d96d6
CM
723 } else {
724 slot++;
5caf2a00 725 path->slots[0]++;
e20d96d6
CM
726 }
727 }
728 advance = 1;
729 item = leaf->items + slot;
e20d96d6
CM
730 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
731 break;
d6e4a428 732 if (btrfs_disk_key_type(&item->key) != key_type)
a429e513 733 break;
7f5c1516
CM
734 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
735 continue;
7fcde0e3 736 filp->f_pos = btrfs_disk_key_offset(&item->key);
dee26a9f 737 advance = 1;
e20d96d6 738 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
7e38180e
CM
739 di_cur = 0;
740 di_total = btrfs_item_size(leaf->items + slot);
741 while(di_cur < di_total) {
742 over = filldir(dirent, (const char *)(di + 1),
743 btrfs_dir_name_len(di),
744 btrfs_disk_key_offset(&item->key),
745 btrfs_disk_key_objectid(&di->location),
746 d_type);
747 if (over)
748 goto nopos;
749 di_len = btrfs_dir_name_len(di) + sizeof(*di);
750 di_cur += di_len;
751 di = (struct btrfs_dir_item *)((char *)di + di_len);
752 }
e20d96d6 753 }
7fcde0e3
CM
754 filp->f_pos++;
755nopos:
e20d96d6
CM
756 ret = 0;
757err:
5caf2a00
CM
758 btrfs_release_path(root, path);
759 btrfs_free_path(path);
22b0ebda 760 mutex_unlock(&root->fs_info->fs_mutex);
e20d96d6
CM
761 return ret;
762}
763
764static void btrfs_put_super (struct super_block * sb)
765{
766 struct btrfs_root *root = btrfs_sb(sb);
767 int ret;
768
769 ret = close_ctree(root);
770 if (ret) {
771 printk("close ctree returns %d\n", ret);
772 }
773 sb->s_fs_info = NULL;
774}
2e635a27
CM
775
776static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
777{
778 struct inode * inode;
e20d96d6
CM
779 struct dentry * root_dentry;
780 struct btrfs_super_block *disk_super;
0f7d52f4 781 struct btrfs_root *tree_root;
d6e4a428 782 struct btrfs_inode *bi;
2e635a27
CM
783
784 sb->s_maxbytes = MAX_LFS_FILESIZE;
2e635a27 785 sb->s_magic = BTRFS_SUPER_MAGIC;
e20d96d6 786 sb->s_op = &btrfs_super_ops;
2e635a27 787 sb->s_time_gran = 1;
e20d96d6 788
0f7d52f4 789 tree_root = open_ctree(sb);
d98237b3 790
0f7d52f4 791 if (!tree_root) {
e20d96d6
CM
792 printk("btrfs: open_ctree failed\n");
793 return -EIO;
794 }
0f7d52f4
CM
795 sb->s_fs_info = tree_root;
796 disk_super = tree_root->fs_info->disk_super;
e20d96d6
CM
797 printk("read in super total blocks %Lu root %Lu\n",
798 btrfs_super_total_blocks(disk_super),
799 btrfs_super_root_dir(disk_super));
800
c5739bba
CM
801 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
802 tree_root);
d6e4a428
CM
803 bi = BTRFS_I(inode);
804 bi->location.objectid = inode->i_ino;
805 bi->location.offset = 0;
806 bi->location.flags = 0;
0f7d52f4 807 bi->root = tree_root;
d6e4a428
CM
808 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
809
2e635a27
CM
810 if (!inode)
811 return -ENOMEM;
e20d96d6
CM
812 if (inode->i_state & I_NEW) {
813 btrfs_read_locked_inode(inode);
814 unlock_new_inode(inode);
815 }
2e635a27 816
e20d96d6
CM
817 root_dentry = d_alloc_root(inode);
818 if (!root_dentry) {
2e635a27
CM
819 iput(inode);
820 return -ENOMEM;
821 }
e20d96d6
CM
822 sb->s_root = root_dentry;
823
2e635a27
CM
824 return 0;
825}
826
4730a4bc
CM
827static int btrfs_write_inode(struct inode *inode, int wait)
828{
d6e4a428 829 struct btrfs_root *root = BTRFS_I(inode)->root;
4730a4bc 830 struct btrfs_trans_handle *trans;
b5133862
CM
831 int ret = 0;
832
833 if (wait) {
834 mutex_lock(&root->fs_info->fs_mutex);
835 trans = btrfs_start_transaction(root, 1);
31f3c99b 836 btrfs_set_trans_block_group(trans, inode);
b5133862
CM
837 ret = btrfs_commit_transaction(trans, root);
838 mutex_unlock(&root->fs_info->fs_mutex);
839 }
840 return ret;
841}
842
843static void btrfs_dirty_inode(struct inode *inode)
844{
845 struct btrfs_root *root = BTRFS_I(inode)->root;
846 struct btrfs_trans_handle *trans;
4730a4bc
CM
847
848 mutex_lock(&root->fs_info->fs_mutex);
849 trans = btrfs_start_transaction(root, 1);
31f3c99b 850 btrfs_set_trans_block_group(trans, inode);
b5133862
CM
851 btrfs_update_inode(trans, root, inode);
852 btrfs_end_transaction(trans, root);
4730a4bc 853 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 854 btrfs_btree_balance_dirty(root);
4730a4bc
CM
855}
856
d5719762 857static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2619ba1f 858 struct btrfs_root *root,
31f3c99b
CM
859 u64 objectid,
860 struct btrfs_block_group_cache *group,
861 int mode)
d5719762
CM
862{
863 struct inode *inode;
864 struct btrfs_inode_item inode_item;
1b05da2e 865 struct btrfs_key *location;
d5719762 866 int ret;
de428b63 867 int owner;
d5719762 868
2619ba1f 869 inode = new_inode(root->fs_info->sb);
d5719762
CM
870 if (!inode)
871 return ERR_PTR(-ENOMEM);
872
2619ba1f 873 BTRFS_I(inode)->root = root;
de428b63
CM
874 if (mode & S_IFDIR)
875 owner = 0;
876 else
877 owner = 1;
878 group = btrfs_find_block_group(root, group, 0, 0, owner);
31f3c99b 879 BTRFS_I(inode)->block_group = group;
d5719762
CM
880
881 inode->i_uid = current->fsuid;
882 inode->i_gid = current->fsgid;
883 inode->i_mode = mode;
884 inode->i_ino = objectid;
885 inode->i_blocks = 0;
c5739bba 886 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
d5719762 887 fill_inode_item(&inode_item, inode);
1b05da2e
CM
888 location = &BTRFS_I(inode)->location;
889 location->objectid = objectid;
890 location->flags = 0;
891 location->offset = 0;
892 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
d5719762
CM
893
894 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
895 BUG_ON(ret);
896
897 insert_inode_hash(inode);
d5719762
CM
898 return inode;
899}
900
901static int btrfs_add_link(struct btrfs_trans_handle *trans,
902 struct dentry *dentry, struct inode *inode)
903{
904 int ret;
d6e4a428
CM
905 struct btrfs_key key;
906 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
907 key.objectid = inode->i_ino;
908 key.flags = 0;
909 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
910 key.offset = 0;
911
912 ret = btrfs_insert_dir_item(trans, root,
d5719762
CM
913 dentry->d_name.name, dentry->d_name.len,
914 dentry->d_parent->d_inode->i_ino,
d6e4a428 915 &key, 0);
4730a4bc 916 if (ret == 0) {
5f26f772 917 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
d6e4a428 918 ret = btrfs_update_inode(trans, root,
4730a4bc
CM
919 dentry->d_parent->d_inode);
920 }
d5719762
CM
921 return ret;
922}
923
924static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
925 struct dentry *dentry, struct inode *inode)
926{
927 int err = btrfs_add_link(trans, dentry, inode);
928 if (!err) {
929 d_instantiate(dentry, inode);
930 return 0;
931 }
2c90e5d6
CM
932 if (err > 0)
933 err = -EEXIST;
d5719762
CM
934 return err;
935}
936
937static int btrfs_create(struct inode *dir, struct dentry *dentry,
938 int mode, struct nameidata *nd)
939{
940 struct btrfs_trans_handle *trans;
d6e4a428 941 struct btrfs_root *root = BTRFS_I(dir)->root;
d5719762
CM
942 struct inode *inode;
943 int err;
134e9731 944 int drop_inode = 0;
2619ba1f 945 u64 objectid;
d5719762 946
d561c025 947 mutex_lock(&root->fs_info->fs_mutex);
d5719762 948 trans = btrfs_start_transaction(root, 1);
31f3c99b 949 btrfs_set_trans_block_group(trans, dir);
2619ba1f
CM
950
951 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
952 if (err) {
953 err = -ENOSPC;
954 goto out_unlock;
955 }
956
31f3c99b
CM
957 inode = btrfs_new_inode(trans, root, objectid,
958 BTRFS_I(dir)->block_group, mode);
d5719762
CM
959 err = PTR_ERR(inode);
960 if (IS_ERR(inode))
d561c025 961 goto out_unlock;
31f3c99b
CM
962
963 btrfs_set_trans_block_group(trans, inode);
d5719762 964 err = btrfs_add_nondir(trans, dentry, inode);
134e9731
CM
965 if (err)
966 drop_inode = 1;
dee26a9f
CM
967 else {
968 inode->i_mapping->a_ops = &btrfs_aops;
969 inode->i_fop = &btrfs_file_operations;
970 inode->i_op = &btrfs_file_inode_operations;
971 }
d5719762 972 dir->i_sb->s_dirt = 1;
31f3c99b
CM
973 btrfs_update_inode_block_group(trans, inode);
974 btrfs_update_inode_block_group(trans, dir);
d561c025 975out_unlock:
22b0ebda 976 btrfs_end_transaction(trans, root);
d561c025 977 mutex_unlock(&root->fs_info->fs_mutex);
2c90e5d6 978
134e9731
CM
979 if (drop_inode) {
980 inode_dec_link_count(inode);
981 iput(inode);
982 }
35b7e476 983 btrfs_btree_balance_dirty(root);
d5719762
CM
984 return err;
985}
986
2b8d99a7
CM
987static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
988 struct dentry *dentry)
989{
990 struct btrfs_trans_handle *trans;
991 struct btrfs_root *root = BTRFS_I(dir)->root;
992 struct inode *inode = old_dentry->d_inode;
993 int err;
994 int drop_inode = 0;
995
996 if (inode->i_nlink == 0)
997 return -ENOENT;
998
999 inc_nlink(inode);
1000 mutex_lock(&root->fs_info->fs_mutex);
1001 trans = btrfs_start_transaction(root, 1);
1002 btrfs_set_trans_block_group(trans, dir);
1003 atomic_inc(&inode->i_count);
1004 err = btrfs_add_nondir(trans, dentry, inode);
1005 if (err)
1006 drop_inode = 1;
1007 dir->i_sb->s_dirt = 1;
1008 btrfs_update_inode_block_group(trans, dir);
1009 btrfs_update_inode(trans, root, inode);
1010
1011 btrfs_end_transaction(trans, root);
1012 mutex_unlock(&root->fs_info->fs_mutex);
1013
1014 if (drop_inode) {
1015 inode_dec_link_count(inode);
1016 iput(inode);
1017 }
1018 btrfs_btree_balance_dirty(root);
1019 return err;
1020}
1021
f7922033 1022static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
2619ba1f
CM
1023 struct btrfs_root *root,
1024 u64 objectid, u64 dirid)
f7922033 1025{
f7922033
CM
1026 int ret;
1027 char buf[2];
d6e4a428
CM
1028 struct btrfs_key key;
1029
f7922033
CM
1030 buf[0] = '.';
1031 buf[1] = '.';
1032
2619ba1f 1033 key.objectid = objectid;
d6e4a428
CM
1034 key.offset = 0;
1035 key.flags = 0;
1036 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1037
2619ba1f 1038 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
d6e4a428 1039 &key, 1);
f7922033
CM
1040 if (ret)
1041 goto error;
2619ba1f
CM
1042 key.objectid = dirid;
1043 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
d6e4a428 1044 &key, 1);
4730a4bc
CM
1045 if (ret)
1046 goto error;
f7922033
CM
1047error:
1048 return ret;
1049}
1050
1051static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1052{
1053 struct inode *inode;
1054 struct btrfs_trans_handle *trans;
d6e4a428 1055 struct btrfs_root *root = BTRFS_I(dir)->root;
f7922033
CM
1056 int err = 0;
1057 int drop_on_err = 0;
2619ba1f 1058 u64 objectid;
f7922033
CM
1059
1060 mutex_lock(&root->fs_info->fs_mutex);
1061 trans = btrfs_start_transaction(root, 1);
31f3c99b 1062 btrfs_set_trans_block_group(trans, dir);
f7922033
CM
1063 if (IS_ERR(trans)) {
1064 err = PTR_ERR(trans);
1065 goto out_unlock;
1066 }
2619ba1f
CM
1067
1068 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1069 if (err) {
1070 err = -ENOSPC;
1071 goto out_unlock;
1072 }
1073
31f3c99b
CM
1074 inode = btrfs_new_inode(trans, root, objectid,
1075 BTRFS_I(dir)->block_group, S_IFDIR | mode);
f7922033
CM
1076 if (IS_ERR(inode)) {
1077 err = PTR_ERR(inode);
1078 goto out_fail;
1079 }
1080 drop_on_err = 1;
1081 inode->i_op = &btrfs_dir_inode_operations;
1082 inode->i_fop = &btrfs_dir_file_operations;
31f3c99b 1083 btrfs_set_trans_block_group(trans, inode);
f7922033 1084
2619ba1f
CM
1085 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1086 if (err)
1087 goto out_fail;
1088
1089 inode->i_size = 6;
1090 err = btrfs_update_inode(trans, root, inode);
f7922033
CM
1091 if (err)
1092 goto out_fail;
1093 err = btrfs_add_link(trans, dentry, inode);
1094 if (err)
1095 goto out_fail;
1096 d_instantiate(dentry, inode);
f7922033 1097 drop_on_err = 0;
cd1bc465 1098 dir->i_sb->s_dirt = 1;
31f3c99b
CM
1099 btrfs_update_inode_block_group(trans, inode);
1100 btrfs_update_inode_block_group(trans, dir);
f7922033
CM
1101
1102out_fail:
1103 btrfs_end_transaction(trans, root);
1104out_unlock:
1105 mutex_unlock(&root->fs_info->fs_mutex);
1106 if (drop_on_err)
1107 iput(inode);
35b7e476 1108 btrfs_btree_balance_dirty(root);
f7922033
CM
1109 return err;
1110}
1111
8fd17795
CM
1112static int btrfs_sync_file(struct file *file,
1113 struct dentry *dentry, int datasync)
1114{
1115 struct inode *inode = dentry->d_inode;
1116 struct btrfs_root *root = BTRFS_I(inode)->root;
1117 int ret;
1118 struct btrfs_trans_handle *trans;
1119
1120 mutex_lock(&root->fs_info->fs_mutex);
1121 trans = btrfs_start_transaction(root, 1);
1122 if (!trans) {
1123 ret = -ENOMEM;
1124 goto out;
1125 }
1126 ret = btrfs_commit_transaction(trans, root);
1127 mutex_unlock(&root->fs_info->fs_mutex);
1128out:
1129 return ret > 0 ? EIO : ret;
1130}
1131
d5719762
CM
1132static int btrfs_sync_fs(struct super_block *sb, int wait)
1133{
1134 struct btrfs_trans_handle *trans;
1135 struct btrfs_root *root;
1136 int ret;
d98237b3 1137 root = btrfs_sb(sb);
df2ce34c 1138
d5719762 1139 sb->s_dirt = 0;
d561c025 1140 if (!wait) {
7cfcc17e 1141 filemap_flush(root->fs_info->btree_inode->i_mapping);
d561c025
CM
1142 return 0;
1143 }
d561c025 1144 mutex_lock(&root->fs_info->fs_mutex);
d5719762
CM
1145 trans = btrfs_start_transaction(root, 1);
1146 ret = btrfs_commit_transaction(trans, root);
1147 sb->s_dirt = 0;
1148 BUG_ON(ret);
1149printk("btrfs sync_fs\n");
d561c025 1150 mutex_unlock(&root->fs_info->fs_mutex);
d5719762
CM
1151 return 0;
1152}
1153
75dfe396 1154static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
dee26a9f
CM
1155 struct buffer_head *result, int create)
1156{
1157 int ret;
1158 int err = 0;
1159 u64 blocknr;
1160 u64 extent_start = 0;
1161 u64 extent_end = 0;
1162 u64 objectid = inode->i_ino;
236454df 1163 u32 found_type;
5caf2a00 1164 struct btrfs_path *path;
d6e4a428 1165 struct btrfs_root *root = BTRFS_I(inode)->root;
dee26a9f
CM
1166 struct btrfs_file_extent_item *item;
1167 struct btrfs_leaf *leaf;
1168 struct btrfs_disk_key *found_key;
1169
5caf2a00
CM
1170 path = btrfs_alloc_path();
1171 BUG_ON(!path);
1172 btrfs_init_path(path);
6567e837 1173 if (create) {
6567e837
CM
1174 WARN_ON(1);
1175 }
dee26a9f 1176
236454df 1177 ret = btrfs_lookup_file_extent(NULL, root, path,
9773a788 1178 inode->i_ino,
236454df 1179 iblock << inode->i_blkbits, 0);
dee26a9f 1180 if (ret < 0) {
dee26a9f
CM
1181 err = ret;
1182 goto out;
1183 }
1184
1185 if (ret != 0) {
5caf2a00
CM
1186 if (path->slots[0] == 0) {
1187 btrfs_release_path(root, path);
236454df 1188 goto out;
dee26a9f 1189 }
5caf2a00 1190 path->slots[0]--;
dee26a9f
CM
1191 }
1192
5caf2a00 1193 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
dee26a9f 1194 struct btrfs_file_extent_item);
5caf2a00 1195 leaf = btrfs_buffer_leaf(path->nodes[0]);
dee26a9f
CM
1196 blocknr = btrfs_file_extent_disk_blocknr(item);
1197 blocknr += btrfs_file_extent_offset(item);
1198
dee26a9f 1199 /* are we inside the extent that was found? */
5caf2a00 1200 found_key = &leaf->items[path->slots[0]].key;
236454df 1201 found_type = btrfs_disk_key_type(found_key);
dee26a9f 1202 if (btrfs_disk_key_objectid(found_key) != objectid ||
236454df 1203 found_type != BTRFS_EXTENT_DATA_KEY) {
dee26a9f
CM
1204 extent_end = 0;
1205 extent_start = 0;
dee26a9f
CM
1206 goto out;
1207 }
236454df
CM
1208 found_type = btrfs_file_extent_type(item);
1209 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1210 if (found_type == BTRFS_FILE_EXTENT_REG) {
1211 extent_start = extent_start >> inode->i_blkbits;
1212 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
3a686375
CM
1213 err = 0;
1214 if (blocknr == 0)
1215 goto out;
236454df 1216 if (iblock >= extent_start && iblock < extent_end) {
236454df
CM
1217 btrfs_map_bh_to_logical(root, result, blocknr +
1218 iblock - extent_start);
1219 goto out;
1220 }
1221 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1222 char *ptr;
1223 char *map;
1224 u32 size;
1225 size = btrfs_file_extent_inline_len(leaf->items +
1226 path->slots[0]);
1227 extent_end = (extent_start + size) >> inode->i_blkbits;
1228 extent_start >>= inode->i_blkbits;
1229 if (iblock < extent_start || iblock > extent_end) {
1230 goto out;
1231 }
1232 ptr = btrfs_file_extent_inline_start(item);
1233 map = kmap(result->b_page);
1234 memcpy(map, ptr, size);
1235 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1236 flush_dcache_page(result->b_page);
1237 kunmap(result->b_page);
1238 set_buffer_uptodate(result);
1239 SetPageChecked(result->b_page);
1240 btrfs_map_bh_to_logical(root, result, 0);
dee26a9f 1241 }
dee26a9f 1242out:
5caf2a00 1243 btrfs_free_path(path);
75dfe396
CM
1244 return err;
1245}
1246
1247static int btrfs_get_block(struct inode *inode, sector_t iblock,
1248 struct buffer_head *result, int create)
1249{
1250 int err;
d6e4a428 1251 struct btrfs_root *root = BTRFS_I(inode)->root;
75dfe396 1252 mutex_lock(&root->fs_info->fs_mutex);
e8f05c45 1253 err = btrfs_get_block_lock(inode, iblock, result, create);
dee26a9f
CM
1254 mutex_unlock(&root->fs_info->fs_mutex);
1255 return err;
1256}
1257
1258static int btrfs_prepare_write(struct file *file, struct page *page,
1259 unsigned from, unsigned to)
1260{
6407bf6d 1261 return nobh_prepare_write(page, from, to, btrfs_get_block);
dee26a9f
CM
1262}
1263
d561c025
CM
1264static void btrfs_write_super(struct super_block *sb)
1265{
1266 btrfs_sync_fs(sb, 1);
1267}
1268
dee26a9f
CM
1269static int btrfs_readpage(struct file *file, struct page *page)
1270{
1271 return mpage_readpage(page, btrfs_get_block);
1272}
1273
236454df
CM
1274/*
1275 * While block_write_full_page is writing back the dirty buffers under
1276 * the page lock, whoever dirtied the buffers may decide to clean them
1277 * again at any time. We handle that by only looking at the buffer
1278 * state inside lock_buffer().
1279 *
1280 * If block_write_full_page() is called for regular writeback
1281 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1282 * locked buffer. This only can happen if someone has written the buffer
1283 * directly, with submit_bh(). At the address_space level PageWriteback
1284 * prevents this contention from occurring.
1285 */
1286static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1287 struct writeback_control *wbc)
1288{
1289 int err;
1290 sector_t block;
1291 sector_t last_block;
1292 struct buffer_head *bh, *head;
1293 const unsigned blocksize = 1 << inode->i_blkbits;
1294 int nr_underway = 0;
1295
1296 BUG_ON(!PageLocked(page));
1297
1298 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1299
1300 if (!page_has_buffers(page)) {
1301 create_empty_buffers(page, blocksize,
1302 (1 << BH_Dirty)|(1 << BH_Uptodate));
1303 }
1304
1305 /*
1306 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1307 * here, and the (potentially unmapped) buffers may become dirty at
1308 * any time. If a buffer becomes dirty here after we've inspected it
1309 * then we just miss that fact, and the page stays dirty.
1310 *
1311 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1312 * handle that here by just cleaning them.
1313 */
1314
1315 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1316 head = page_buffers(page);
1317 bh = head;
1318
1319 /*
1320 * Get all the dirty buffers mapped to disk addresses and
1321 * handle any aliases from the underlying blockdev's mapping.
1322 */
1323 do {
1324 if (block > last_block) {
1325 /*
1326 * mapped buffers outside i_size will occur, because
1327 * this page can be outside i_size when there is a
1328 * truncate in progress.
1329 */
1330 /*
1331 * The buffer was zeroed by block_write_full_page()
1332 */
1333 clear_buffer_dirty(bh);
1334 set_buffer_uptodate(bh);
1335 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1336 WARN_ON(bh->b_size != blocksize);
1337 err = btrfs_get_block(inode, block, bh, 0);
35b7e476
CM
1338 if (err) {
1339printk("writepage going to recovery err %d\n", err);
236454df 1340 goto recover;
35b7e476 1341 }
236454df
CM
1342 if (buffer_new(bh)) {
1343 /* blockdev mappings never come here */
1344 clear_buffer_new(bh);
236454df
CM
1345 }
1346 }
1347 bh = bh->b_this_page;
1348 block++;
1349 } while (bh != head);
1350
1351 do {
1352 if (!buffer_mapped(bh))
1353 continue;
1354 /*
1355 * If it's a fully non-blocking write attempt and we cannot
1356 * lock the buffer then redirty the page. Note that this can
1357 * potentially cause a busy-wait loop from pdflush and kswapd
1358 * activity, but those code paths have their own higher-level
1359 * throttling.
1360 */
1361 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1362 lock_buffer(bh);
1363 } else if (test_set_buffer_locked(bh)) {
1364 redirty_page_for_writepage(wbc, page);
1365 continue;
1366 }
1367 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1368 mark_buffer_async_write(bh);
1369 } else {
1370 unlock_buffer(bh);
1371 }
1372 } while ((bh = bh->b_this_page) != head);
1373
1374 /*
1375 * The page and its buffers are protected by PageWriteback(), so we can
1376 * drop the bh refcounts early.
1377 */
1378 BUG_ON(PageWriteback(page));
1379 set_page_writeback(page);
1380
1381 do {
1382 struct buffer_head *next = bh->b_this_page;
1383 if (buffer_async_write(bh)) {
1384 submit_bh(WRITE, bh);
1385 nr_underway++;
1386 }
1387 bh = next;
1388 } while (bh != head);
1389 unlock_page(page);
1390
1391 err = 0;
1392done:
1393 if (nr_underway == 0) {
1394 /*
1395 * The page was marked dirty, but the buffers were
1396 * clean. Someone wrote them back by hand with
1397 * ll_rw_block/submit_bh. A rare case.
1398 */
1399 int uptodate = 1;
1400 do {
1401 if (!buffer_uptodate(bh)) {
1402 uptodate = 0;
1403 break;
1404 }
1405 bh = bh->b_this_page;
1406 } while (bh != head);
1407 if (uptodate)
1408 SetPageUptodate(page);
1409 end_page_writeback(page);
236454df
CM
1410 }
1411 return err;
1412
1413recover:
1414 /*
1415 * ENOSPC, or some other error. We may already have added some
1416 * blocks to the file, so we need to write these out to avoid
1417 * exposing stale data.
1418 * The page is currently locked and not marked for writeback
1419 */
1420 bh = head;
1421 /* Recovery: lock and submit the mapped buffers */
1422 do {
1423 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1424 lock_buffer(bh);
1425 mark_buffer_async_write(bh);
1426 } else {
1427 /*
1428 * The buffer may have been set dirty during
1429 * attachment to a dirty page.
1430 */
1431 clear_buffer_dirty(bh);
1432 }
1433 } while ((bh = bh->b_this_page) != head);
1434 SetPageError(page);
1435 BUG_ON(PageWriteback(page));
1436 set_page_writeback(page);
1437 do {
1438 struct buffer_head *next = bh->b_this_page;
1439 if (buffer_async_write(bh)) {
1440 clear_buffer_dirty(bh);
1441 submit_bh(WRITE, bh);
1442 nr_underway++;
1443 }
1444 bh = next;
1445 } while (bh != head);
1446 unlock_page(page);
1447 goto done;
1448}
1449
1450/*
1451 * The generic ->writepage function for buffer-backed address_spaces
1452 */
dee26a9f
CM
1453static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1454{
236454df
CM
1455 struct inode * const inode = page->mapping->host;
1456 loff_t i_size = i_size_read(inode);
1457 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1458 unsigned offset;
1459 void *kaddr;
1460
1461 /* Is the page fully inside i_size? */
1462 if (page->index < end_index)
1463 return __btrfs_write_full_page(inode, page, wbc);
1464
1465 /* Is the page fully outside i_size? (truncate in progress) */
1466 offset = i_size & (PAGE_CACHE_SIZE-1);
1467 if (page->index >= end_index+1 || !offset) {
1468 /*
1469 * The page may have dirty, unmapped buffers. For example,
1470 * they may have been added in ext3_writepage(). Make them
1471 * freeable here, so the page does not leak.
1472 */
1473 block_invalidatepage(page, 0);
1474 unlock_page(page);
1475 return 0; /* don't care */
1476 }
1477
1478 /*
1479 * The page straddles i_size. It must be zeroed out on each and every
1480 * writepage invokation because it may be mmapped. "A file is mapped
1481 * in multiples of the page size. For a file that is not a multiple of
1482 * the page size, the remaining memory is zeroed when mapped, and
1483 * writes to that region are not written out to the file."
1484 */
1485 kaddr = kmap_atomic(page, KM_USER0);
1486 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1487 flush_dcache_page(page);
1488 kunmap_atomic(kaddr, KM_USER0);
1489 return __btrfs_write_full_page(inode, page, wbc);
dee26a9f 1490}
d561c025 1491
f4b9aa8d
CM
1492static void btrfs_truncate(struct inode *inode)
1493{
d6e4a428 1494 struct btrfs_root *root = BTRFS_I(inode)->root;
f4b9aa8d
CM
1495 int ret;
1496 struct btrfs_trans_handle *trans;
1497
1498 if (!S_ISREG(inode->i_mode))
1499 return;
1500 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1501 return;
1502
e8f05c45 1503 nobh_truncate_page(inode->i_mapping, inode->i_size);
f4b9aa8d
CM
1504
1505 /* FIXME, add redo link to tree so we don't leak on crash */
1506 mutex_lock(&root->fs_info->fs_mutex);
1507 trans = btrfs_start_transaction(root, 1);
31f3c99b 1508 btrfs_set_trans_block_group(trans, inode);
f4b9aa8d
CM
1509 ret = btrfs_truncate_in_trans(trans, root, inode);
1510 BUG_ON(ret);
35b7e476 1511 btrfs_update_inode(trans, root, inode);
f4b9aa8d
CM
1512 ret = btrfs_end_transaction(trans, root);
1513 BUG_ON(ret);
1514 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 1515 btrfs_btree_balance_dirty(root);
f4b9aa8d
CM
1516}
1517
236454df
CM
1518/*
1519 * Make sure any changes to nobh_commit_write() are reflected in
1520 * nobh_truncate_page(), since it doesn't call commit_write().
1521 */
1522static int btrfs_commit_write(struct file *file, struct page *page,
1523 unsigned from, unsigned to)
1524{
1525 struct inode *inode = page->mapping->host;
1526 struct buffer_head *bh;
1527 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1528
1529 SetPageUptodate(page);
1530 bh = page_buffers(page);
1531 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1532 set_page_dirty(page);
1533 }
1534 if (pos > inode->i_size) {
1535 i_size_write(inode, pos);
1536 mark_inode_dirty(inode);
1537 }
1538 return 0;
1539}
1540
75dfe396
CM
1541static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1542 struct page **prepared_pages,
1543 const char __user * buf)
1544{
1545 long page_fault = 0;
1546 int i;
1547 int offset = pos & (PAGE_CACHE_SIZE - 1);
1548
1549 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1550 size_t count = min_t(size_t,
1551 PAGE_CACHE_SIZE - offset, write_bytes);
1552 struct page *page = prepared_pages[i];
1553 fault_in_pages_readable(buf, count);
1554
1555 /* Copy data from userspace to the current page */
1556 kmap(page);
1557 page_fault = __copy_from_user(page_address(page) + offset,
1558 buf, count);
1559 /* Flush processor's dcache for this page */
1560 flush_dcache_page(page);
1561 kunmap(page);
1562 buf += count;
1563 write_bytes -= count;
1564
1565 if (page_fault)
1566 break;
1567 }
1568 return page_fault ? -EFAULT : 0;
1569}
1570
1571static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1572{
1573 size_t i;
1574 for (i = 0; i < num_pages; i++) {
1575 if (!pages[i])
1576 break;
1577 unlock_page(pages[i]);
1578 mark_page_accessed(pages[i]);
1579 page_cache_release(pages[i]);
1580 }
1581}
1582static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct file *file,
1585 struct page **pages,
1586 size_t num_pages,
1587 loff_t pos,
1588 size_t write_bytes)
1589{
1590 int i;
1591 int offset;
1592 int err = 0;
1593 int ret;
1594 int this_write;
f254e52c 1595 struct inode *inode = file->f_path.dentry->d_inode;
236454df
CM
1596 struct buffer_head *bh;
1597 struct btrfs_file_extent_item *ei;
75dfe396
CM
1598
1599 for (i = 0; i < num_pages; i++) {
1600 offset = pos & (PAGE_CACHE_SIZE -1);
1601 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
f254e52c
CM
1602 /* FIXME, one block at a time */
1603
1604 mutex_lock(&root->fs_info->fs_mutex);
1605 trans = btrfs_start_transaction(root, 1);
31f3c99b 1606 btrfs_set_trans_block_group(trans, inode);
236454df
CM
1607
1608 bh = page_buffers(pages[i]);
1609 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1610 struct btrfs_key key;
1611 struct btrfs_path *path;
1612 char *ptr;
1613 u32 datasize;
1614
1615 path = btrfs_alloc_path();
1616 BUG_ON(!path);
1617 key.objectid = inode->i_ino;
1618 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1619 key.flags = 0;
1620 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1621 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1622 datasize = offset +
1623 btrfs_file_extent_calc_inline_size(write_bytes);
1624 ret = btrfs_insert_empty_item(trans, root, path, &key,
1625 datasize);
1626 BUG_ON(ret);
1627 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1628 path->slots[0], struct btrfs_file_extent_item);
1629 btrfs_set_file_extent_generation(ei, trans->transid);
1630 btrfs_set_file_extent_type(ei,
1631 BTRFS_FILE_EXTENT_INLINE);
1632 ptr = btrfs_file_extent_inline_start(ei);
098f59c2
CM
1633 btrfs_memcpy(root, path->nodes[0]->b_data,
1634 ptr, bh->b_data, offset + write_bytes);
236454df
CM
1635 mark_buffer_dirty(path->nodes[0]);
1636 btrfs_free_path(path);
3a686375 1637 } else if (buffer_mapped(bh)) {
236454df 1638 btrfs_csum_file_block(trans, root, inode->i_ino,
f254e52c
CM
1639 pages[i]->index << PAGE_CACHE_SHIFT,
1640 kmap(pages[i]), PAGE_CACHE_SIZE);
236454df
CM
1641 kunmap(pages[i]);
1642 }
f254e52c 1643 SetPageChecked(pages[i]);
e37c9e69 1644 // btrfs_update_inode_block_group(trans, inode);
f254e52c
CM
1645 ret = btrfs_end_transaction(trans, root);
1646 BUG_ON(ret);
1647 mutex_unlock(&root->fs_info->fs_mutex);
1648
236454df 1649 ret = btrfs_commit_write(file, pages[i], offset,
75dfe396
CM
1650 offset + this_write);
1651 pos += this_write;
1652 if (ret) {
1653 err = ret;
1654 goto failed;
1655 }
1656 WARN_ON(this_write > write_bytes);
1657 write_bytes -= this_write;
1658 }
1659failed:
1660 return err;
1661}
1662
b18c6685
CM
1663static int drop_extents(struct btrfs_trans_handle *trans,
1664 struct btrfs_root *root,
1665 struct inode *inode,
de428b63 1666 u64 start, u64 end, u64 *hint_block)
b18c6685
CM
1667{
1668 int ret;
1669 struct btrfs_key key;
1670 struct btrfs_leaf *leaf;
1671 int slot;
1672 struct btrfs_file_extent_item *extent;
236454df 1673 u64 extent_end = 0;
b18c6685
CM
1674 int keep;
1675 struct btrfs_file_extent_item old;
1676 struct btrfs_path *path;
1677 u64 search_start = start;
1678 int bookend;
236454df
CM
1679 int found_type;
1680 int found_extent;
1681 int found_inline;
1682
b18c6685
CM
1683 path = btrfs_alloc_path();
1684 if (!path)
1685 return -ENOMEM;
a429e513
CM
1686 while(1) {
1687 btrfs_release_path(root, path);
1688 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1689 search_start, -1);
1690 if (ret < 0)
70b2befd 1691 goto out;
a429e513
CM
1692 if (ret > 0) {
1693 if (path->slots[0] == 0) {
236454df 1694 ret = 0;
a429e513
CM
1695 goto out;
1696 }
1697 path->slots[0]--;
70b2befd 1698 }
b18c6685
CM
1699 keep = 0;
1700 bookend = 0;
236454df
CM
1701 found_extent = 0;
1702 found_inline = 0;
1703 extent = NULL;
b18c6685
CM
1704 leaf = btrfs_buffer_leaf(path->nodes[0]);
1705 slot = path->slots[0];
1706 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
b18c6685
CM
1707 if (key.offset >= end || key.objectid != inode->i_ino) {
1708 ret = 0;
1709 goto out;
1710 }
236454df
CM
1711 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1712 ret = 0;
1713 goto out;
1714 }
1715 extent = btrfs_item_ptr(leaf, slot,
1716 struct btrfs_file_extent_item);
1717 found_type = btrfs_file_extent_type(extent);
1718 if (found_type == BTRFS_FILE_EXTENT_REG) {
1719 extent_end = key.offset +
1720 (btrfs_file_extent_num_blocks(extent) <<
1721 inode->i_blkbits);
1722 found_extent = 1;
1723 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1724 found_inline = 1;
1725 extent_end = key.offset +
1726 btrfs_file_extent_inline_len(leaf->items + slot);
1727 }
1728
1729 if (!found_extent && !found_inline) {
1730 ret = 0;
a429e513 1731 goto out;
236454df
CM
1732 }
1733
1734 if (search_start >= extent_end) {
1735 ret = 0;
a429e513 1736 goto out;
236454df
CM
1737 }
1738
3a686375
CM
1739 if (found_inline) {
1740 u64 mask = root->blocksize - 1;
1741 search_start = (extent_end + mask) & ~mask;
1742 } else
1743 search_start = extent_end;
b18c6685
CM
1744
1745 if (end < extent_end && end >= key.offset) {
236454df 1746 if (found_extent) {
3a686375
CM
1747 u64 disk_blocknr =
1748 btrfs_file_extent_disk_blocknr(extent);
1749 u64 disk_num_blocks =
1750 btrfs_file_extent_disk_num_blocks(extent);
236454df 1751 memcpy(&old, extent, sizeof(old));
3a686375
CM
1752 if (disk_blocknr != 0) {
1753 ret = btrfs_inc_extent_ref(trans, root,
1754 disk_blocknr, disk_num_blocks);
1755 BUG_ON(ret);
1756 }
236454df
CM
1757 }
1758 WARN_ON(found_inline);
b18c6685
CM
1759 bookend = 1;
1760 }
1761
1762 if (start > key.offset) {
1763 u64 new_num;
a429e513 1764 u64 old_num;
b18c6685
CM
1765 /* truncate existing extent */
1766 keep = 1;
1767 WARN_ON(start & (root->blocksize - 1));
236454df
CM
1768 if (found_extent) {
1769 new_num = (start - key.offset) >>
1770 inode->i_blkbits;
1771 old_num = btrfs_file_extent_num_blocks(extent);
de428b63
CM
1772 *hint_block =
1773 btrfs_file_extent_disk_blocknr(extent);
3a686375
CM
1774 if (btrfs_file_extent_disk_blocknr(extent)) {
1775 inode->i_blocks -=
1776 (old_num - new_num) << 3;
1777 }
236454df
CM
1778 btrfs_set_file_extent_num_blocks(extent,
1779 new_num);
1780 mark_buffer_dirty(path->nodes[0]);
1781 } else {
1782 WARN_ON(1);
236454df 1783 }
b18c6685
CM
1784 }
1785 if (!keep) {
236454df
CM
1786 u64 disk_blocknr = 0;
1787 u64 disk_num_blocks = 0;
1788 u64 extent_num_blocks = 0;
1789 if (found_extent) {
1790 disk_blocknr =
1791 btrfs_file_extent_disk_blocknr(extent);
1792 disk_num_blocks =
1793 btrfs_file_extent_disk_num_blocks(extent);
1794 extent_num_blocks =
1795 btrfs_file_extent_num_blocks(extent);
de428b63
CM
1796 *hint_block =
1797 btrfs_file_extent_disk_blocknr(extent);
236454df 1798 }
b18c6685
CM
1799 ret = btrfs_del_item(trans, root, path);
1800 BUG_ON(ret);
1801 btrfs_release_path(root, path);
098f59c2 1802 extent = NULL;
3a686375 1803 if (found_extent && disk_blocknr != 0) {
098f59c2 1804 inode->i_blocks -= extent_num_blocks << 3;
236454df
CM
1805 ret = btrfs_free_extent(trans, root,
1806 disk_blocknr,
1807 disk_num_blocks, 0);
1808 }
b18c6685
CM
1809
1810 BUG_ON(ret);
1811 if (!bookend && search_start >= end) {
1812 ret = 0;
1813 goto out;
1814 }
1815 if (!bookend)
a429e513 1816 continue;
b18c6685 1817 }
236454df 1818 if (bookend && found_extent) {
b18c6685
CM
1819 /* create bookend */
1820 struct btrfs_key ins;
b18c6685
CM
1821 ins.objectid = inode->i_ino;
1822 ins.offset = end;
1823 ins.flags = 0;
1824 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1825
1826 btrfs_release_path(root, path);
b18c6685
CM
1827 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1828 sizeof(*extent));
1829 BUG_ON(ret);
1830 extent = btrfs_item_ptr(
1831 btrfs_buffer_leaf(path->nodes[0]),
1832 path->slots[0],
1833 struct btrfs_file_extent_item);
1834 btrfs_set_file_extent_disk_blocknr(extent,
1835 btrfs_file_extent_disk_blocknr(&old));
1836 btrfs_set_file_extent_disk_num_blocks(extent,
1837 btrfs_file_extent_disk_num_blocks(&old));
1838
1839 btrfs_set_file_extent_offset(extent,
1840 btrfs_file_extent_offset(&old) +
1841 ((end - key.offset) >> inode->i_blkbits));
1842 WARN_ON(btrfs_file_extent_num_blocks(&old) <
3a686375 1843 (extent_end - end) >> inode->i_blkbits);
b18c6685 1844 btrfs_set_file_extent_num_blocks(extent,
3a686375 1845 (extent_end - end) >> inode->i_blkbits);
b18c6685 1846
236454df
CM
1847 btrfs_set_file_extent_type(extent,
1848 BTRFS_FILE_EXTENT_REG);
b18c6685
CM
1849 btrfs_set_file_extent_generation(extent,
1850 btrfs_file_extent_generation(&old));
b18c6685 1851 btrfs_mark_buffer_dirty(path->nodes[0]);
3a686375
CM
1852 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
1853 inode->i_blocks +=
1854 btrfs_file_extent_num_blocks(extent) << 3;
1855 }
b18c6685 1856 ret = 0;
70b2befd 1857 goto out;
b18c6685 1858 }
b18c6685 1859 }
b18c6685 1860out:
b18c6685
CM
1861 btrfs_free_path(path);
1862 return ret;
1863}
1864
1865static int prepare_pages(struct btrfs_root *root,
75dfe396
CM
1866 struct file *file,
1867 struct page **pages,
1868 size_t num_pages,
1869 loff_t pos,
2932f3ec
CM
1870 unsigned long first_index,
1871 unsigned long last_index,
6567e837
CM
1872 size_t write_bytes,
1873 u64 alloc_extent_start)
75dfe396
CM
1874{
1875 int i;
1876 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1877 struct inode *inode = file->f_path.dentry->d_inode;
1878 int offset;
1879 int err = 0;
75dfe396 1880 int this_write;
6567e837
CM
1881 struct buffer_head *bh;
1882 struct buffer_head *head;
75dfe396
CM
1883 loff_t isize = i_size_read(inode);
1884
1885 memset(pages, 0, num_pages * sizeof(struct page *));
1886
1887 for (i = 0; i < num_pages; i++) {
1888 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1889 if (!pages[i]) {
1890 err = -ENOMEM;
1891 goto failed_release;
1892 }
35b7e476
CM
1893 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
1894 wait_on_page_writeback(pages[i]);
75dfe396
CM
1895 offset = pos & (PAGE_CACHE_SIZE -1);
1896 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
35b7e476
CM
1897 if (!page_has_buffers(pages[i])) {
1898 create_empty_buffers(pages[i],
1899 root->fs_info->sb->s_blocksize,
1900 (1 << BH_Uptodate));
1901 }
6567e837
CM
1902 head = page_buffers(pages[i]);
1903 bh = head;
1904 do {
1905 err = btrfs_map_bh_to_logical(root, bh,
1906 alloc_extent_start);
1907 BUG_ON(err);
1908 if (err)
1909 goto failed_truncate;
1910 bh = bh->b_this_page;
236454df
CM
1911 if (alloc_extent_start)
1912 alloc_extent_start++;
6567e837 1913 } while (bh != head);
75dfe396 1914 pos += this_write;
75dfe396
CM
1915 WARN_ON(this_write > write_bytes);
1916 write_bytes -= this_write;
1917 }
1918 return 0;
1919
1920failed_release:
1921 btrfs_drop_pages(pages, num_pages);
1922 return err;
1923
1924failed_truncate:
1925 btrfs_drop_pages(pages, num_pages);
1926 if (pos > isize)
1927 vmtruncate(inode, isize);
1928 return err;
1929}
1930
1931static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1932 size_t count, loff_t *ppos)
1933{
1934 loff_t pos;
1935 size_t num_written = 0;
1936 int err = 0;
1937 int ret = 0;
75dfe396 1938 struct inode *inode = file->f_path.dentry->d_inode;
d6e4a428 1939 struct btrfs_root *root = BTRFS_I(inode)->root;
b18c6685 1940 struct page *pages[8];
35b7e476 1941 struct page *pinned[2];
2932f3ec
CM
1942 unsigned long first_index;
1943 unsigned long last_index;
6567e837
CM
1944 u64 start_pos;
1945 u64 num_blocks;
1946 u64 alloc_extent_start;
de428b63 1947 u64 hint_block;
6567e837 1948 struct btrfs_trans_handle *trans;
b18c6685 1949 struct btrfs_key ins;
35b7e476
CM
1950 pinned[0] = NULL;
1951 pinned[1] = NULL;
75dfe396
CM
1952 if (file->f_flags & O_DIRECT)
1953 return -EINVAL;
1954 pos = *ppos;
75dfe396
CM
1955 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1956 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1957 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1958 if (err)
1959 goto out;
1960 if (count == 0)
1961 goto out;
1962 err = remove_suid(file->f_path.dentry);
1963 if (err)
1964 goto out;
1965 file_update_time(file);
a429e513
CM
1966
1967 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1968 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1969 inode->i_blkbits;
1970
75dfe396 1971 mutex_lock(&inode->i_mutex);
2932f3ec
CM
1972 first_index = pos >> PAGE_CACHE_SHIFT;
1973 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
6567e837 1974
a429e513
CM
1975 if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
1976 (pos & (PAGE_CACHE_SIZE - 1))) {
1977 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
1978 if (!PageUptodate(pinned[0])) {
1979 ret = mpage_readpage(pinned[0], btrfs_get_block);
1980 BUG_ON(ret);
35b7e476 1981 wait_on_page_locked(pinned[0]);
a429e513
CM
1982 } else {
1983 unlock_page(pinned[0]);
1984 }
1985 }
1986 if (first_index != last_index &&
1987 (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
de428b63 1988 pos + count < inode->i_size &&
a429e513
CM
1989 (count & (PAGE_CACHE_SIZE - 1))) {
1990 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
1991 if (!PageUptodate(pinned[1])) {
1992 ret = mpage_readpage(pinned[1], btrfs_get_block);
1993 BUG_ON(ret);
35b7e476 1994 wait_on_page_locked(pinned[1]);
a429e513
CM
1995 } else {
1996 unlock_page(pinned[1]);
1997 }
1998 }
1999
6567e837
CM
2000 mutex_lock(&root->fs_info->fs_mutex);
2001 trans = btrfs_start_transaction(root, 1);
2002 if (!trans) {
2003 err = -ENOMEM;
b18c6685 2004 mutex_unlock(&root->fs_info->fs_mutex);
6567e837
CM
2005 goto out_unlock;
2006 }
31f3c99b 2007 btrfs_set_trans_block_group(trans, inode);
a429e513
CM
2008 /* FIXME blocksize != 4096 */
2009 inode->i_blocks += num_blocks << 3;
de428b63 2010 hint_block = 0;
b18c6685 2011 if (start_pos < inode->i_size) {
a429e513 2012 /* FIXME blocksize != pagesize */
b18c6685
CM
2013 ret = drop_extents(trans, root, inode,
2014 start_pos,
2015 (pos + count + root->blocksize -1) &
de428b63 2016 ~((u64)root->blocksize - 1), &hint_block);
236454df 2017 BUG_ON(ret);
b18c6685 2018 }
3a686375
CM
2019 if (inode->i_size < start_pos) {
2020 u64 last_pos_in_file;
2021 u64 hole_size;
2022 u64 mask = root->blocksize - 1;
2023 last_pos_in_file = (inode->i_size + mask) & ~mask;
2024 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
2025 hole_size >>= inode->i_blkbits;
2026 if (last_pos_in_file < start_pos) {
2027 ret = btrfs_insert_file_extent(trans, root,
2028 inode->i_ino,
2029 last_pos_in_file,
2030 0, 0, hole_size);
2031 }
2032 BUG_ON(ret);
2033 }
236454df
CM
2034 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
2035 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
4d775673 2036 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
de428b63
CM
2037 num_blocks, hint_block, (u64)-1,
2038 &ins, 1);
236454df
CM
2039 BUG_ON(ret);
2040 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
3a686375
CM
2041 start_pos, ins.objectid, ins.offset,
2042 ins.offset);
236454df
CM
2043 BUG_ON(ret);
2044 } else {
2045 ins.offset = 0;
2046 ins.objectid = 0;
2047 }
6567e837 2048 BUG_ON(ret);
b18c6685 2049 alloc_extent_start = ins.objectid;
e37c9e69 2050 // btrfs_update_inode_block_group(trans, inode);
b18c6685 2051 ret = btrfs_end_transaction(trans, root);
6567e837
CM
2052 mutex_unlock(&root->fs_info->fs_mutex);
2053
75dfe396
CM
2054 while(count > 0) {
2055 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
2056 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
2057 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
2058 PAGE_CACHE_SHIFT;
b18c6685
CM
2059
2060 memset(pages, 0, sizeof(pages));
2061 ret = prepare_pages(root, file, pages, num_pages,
6567e837
CM
2062 pos, first_index, last_index,
2063 write_bytes, alloc_extent_start);
75dfe396 2064 BUG_ON(ret);
b18c6685 2065
6567e837 2066 /* FIXME blocks != pagesize */
236454df
CM
2067 if (alloc_extent_start)
2068 alloc_extent_start += num_pages;
75dfe396
CM
2069 ret = btrfs_copy_from_user(pos, num_pages,
2070 write_bytes, pages, buf);
2071 BUG_ON(ret);
2072
f254e52c 2073 ret = dirty_and_release_pages(NULL, root, file, pages,
70b2befd 2074 num_pages, pos, write_bytes);
75dfe396
CM
2075 BUG_ON(ret);
2076 btrfs_drop_pages(pages, num_pages);
2077
75dfe396
CM
2078 buf += write_bytes;
2079 count -= write_bytes;
2080 pos += write_bytes;
2081 num_written += write_bytes;
2082
2083 balance_dirty_pages_ratelimited(inode->i_mapping);
35b7e476 2084 btrfs_btree_balance_dirty(root);
75dfe396
CM
2085 cond_resched();
2086 }
6567e837 2087out_unlock:
75dfe396
CM
2088 mutex_unlock(&inode->i_mutex);
2089out:
a429e513
CM
2090 if (pinned[0])
2091 page_cache_release(pinned[0]);
2092 if (pinned[1])
2093 page_cache_release(pinned[1]);
75dfe396
CM
2094 *ppos = pos;
2095 current->backing_dev_info = NULL;
a429e513 2096 mark_inode_dirty(inode);
75dfe396
CM
2097 return num_written ? num_written : err;
2098}
2099
f254e52c
CM
2100static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
2101 unsigned long offset, unsigned long size)
2102{
2103 char *kaddr;
2104 unsigned long left, count = desc->count;
d6e4a428 2105 struct inode *inode = page->mapping->host;
f254e52c
CM
2106
2107 if (size > count)
2108 size = count;
2109
2110 if (!PageChecked(page)) {
2111 /* FIXME, do it per block */
d6e4a428 2112 struct btrfs_root *root = BTRFS_I(inode)->root;
236454df 2113
f254e52c 2114 int ret = btrfs_csum_verify_file_block(root,
236454df
CM
2115 page->mapping->host->i_ino,
2116 page->index << PAGE_CACHE_SHIFT,
2117 kmap(page), PAGE_CACHE_SIZE);
f254e52c 2118 if (ret) {
3a686375
CM
2119 if (ret != -ENOENT) {
2120 printk("failed to verify ino %lu page %lu\n",
2121 page->mapping->host->i_ino,
2122 page->index);
2123 memset(page_address(page), 0, PAGE_CACHE_SIZE);
2124 flush_dcache_page(page);
2125 }
f254e52c
CM
2126 }
2127 SetPageChecked(page);
2128 kunmap(page);
2129 }
2130 /*
2131 * Faults on the destination of a read are common, so do it before
2132 * taking the kmap.
2133 */
2134 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2135 kaddr = kmap_atomic(page, KM_USER0);
2136 left = __copy_to_user_inatomic(desc->arg.buf,
2137 kaddr + offset, size);
2138 kunmap_atomic(kaddr, KM_USER0);
2139 if (left == 0)
2140 goto success;
2141 }
2142
2143 /* Do it the slow way */
2144 kaddr = kmap(page);
2145 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2146 kunmap(page);
2147
2148 if (left) {
2149 size -= left;
2150 desc->error = -EFAULT;
2151 }
2152success:
2153 desc->count = count - size;
2154 desc->written += size;
2155 desc->arg.buf += size;
2156 return size;
2157}
2158
2159/**
2160 * btrfs_file_aio_read - filesystem read routine
2161 * @iocb: kernel I/O control block
2162 * @iov: io vector request
2163 * @nr_segs: number of segments in the iovec
2164 * @pos: current file position
2165 */
2166static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2167 unsigned long nr_segs, loff_t pos)
2168{
2169 struct file *filp = iocb->ki_filp;
2170 ssize_t retval;
2171 unsigned long seg;
2172 size_t count;
2173 loff_t *ppos = &iocb->ki_pos;
2174
2175 count = 0;
2176 for (seg = 0; seg < nr_segs; seg++) {
2177 const struct iovec *iv = &iov[seg];
2178
2179 /*
2180 * If any segment has a negative length, or the cumulative
2181 * length ever wraps negative then return -EINVAL.
2182 */
2183 count += iv->iov_len;
2184 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2185 return -EINVAL;
2186 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2187 continue;
2188 if (seg == 0)
2189 return -EFAULT;
2190 nr_segs = seg;
2191 count -= iv->iov_len; /* This segment is no good */
2192 break;
2193 }
2194 retval = 0;
2195 if (count) {
2196 for (seg = 0; seg < nr_segs; seg++) {
2197 read_descriptor_t desc;
2198
2199 desc.written = 0;
2200 desc.arg.buf = iov[seg].iov_base;
2201 desc.count = iov[seg].iov_len;
2202 if (desc.count == 0)
2203 continue;
2204 desc.error = 0;
2205 do_generic_file_read(filp, ppos, &desc,
2206 btrfs_read_actor);
2207 retval += desc.written;
2208 if (desc.error) {
2209 retval = retval ?: desc.error;
2210 break;
2211 }
2212 }
2213 }
2214 return retval;
2215}
2216
2619ba1f
CM
2217static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2218{
2219 struct btrfs_trans_handle *trans;
2220 struct btrfs_key key;
2221 struct btrfs_root_item root_item;
2222 struct btrfs_inode_item *inode_item;
2223 struct buffer_head *subvol;
2224 struct btrfs_leaf *leaf;
2225 struct btrfs_root *new_root;
2226 struct inode *inode;
31f3c99b 2227 struct inode *dir;
2619ba1f
CM
2228 int ret;
2229 u64 objectid;
2230 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2231
2232 mutex_lock(&root->fs_info->fs_mutex);
2233 trans = btrfs_start_transaction(root, 1);
2234 BUG_ON(!trans);
2235
31f3c99b 2236 subvol = btrfs_alloc_free_block(trans, root, 0);
5e82849e
CM
2237 if (subvol == NULL)
2238 return -ENOSPC;
2619ba1f
CM
2239 leaf = btrfs_buffer_leaf(subvol);
2240 btrfs_set_header_nritems(&leaf->header, 0);
2241 btrfs_set_header_level(&leaf->header, 0);
7eccb903 2242 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2619ba1f 2243 btrfs_set_header_generation(&leaf->header, trans->transid);
4d775673 2244 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2619ba1f
CM
2245 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2246 sizeof(leaf->header.fsid));
4d775673 2247 mark_buffer_dirty(subvol);
2619ba1f
CM
2248
2249 inode_item = &root_item.inode;
2250 memset(inode_item, 0, sizeof(*inode_item));
2251 btrfs_set_inode_generation(inode_item, 1);
2252 btrfs_set_inode_size(inode_item, 3);
2253 btrfs_set_inode_nlink(inode_item, 1);
2254 btrfs_set_inode_nblocks(inode_item, 1);
2255 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2256
7eccb903 2257 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2619ba1f 2258 btrfs_set_root_refs(&root_item, 1);
5e82849e
CM
2259 brelse(subvol);
2260 subvol = NULL;
2619ba1f 2261
2619ba1f
CM
2262 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2263 0, &objectid);
2264 BUG_ON(ret);
2265
2266 btrfs_set_root_dirid(&root_item, new_dirid);
2267
2268 key.objectid = objectid;
2269 key.offset = 1;
2270 key.flags = 0;
2271 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2272 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2273 &root_item);
2274 BUG_ON(ret);
2275
2276 /*
2277 * insert the directory item
2278 */
2279 key.offset = (u64)-1;
31f3c99b 2280 dir = root->fs_info->sb->s_root->d_inode;
2619ba1f 2281 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
31f3c99b 2282 name, namelen, dir->i_ino, &key, 0);
2619ba1f
CM
2283 BUG_ON(ret);
2284
2285 ret = btrfs_commit_transaction(trans, root);
2286 BUG_ON(ret);
2287
2288 new_root = btrfs_read_fs_root(root->fs_info, &key);
2289 BUG_ON(!new_root);
2290
2291 trans = btrfs_start_transaction(new_root, 1);
2292 BUG_ON(!trans);
2293
31f3c99b
CM
2294 inode = btrfs_new_inode(trans, new_root, new_dirid,
2295 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2619ba1f
CM
2296 inode->i_op = &btrfs_dir_inode_operations;
2297 inode->i_fop = &btrfs_dir_file_operations;
2298
2299 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2300 BUG_ON(ret);
2301
2302 inode->i_nlink = 1;
2303 inode->i_size = 6;
2304 ret = btrfs_update_inode(trans, new_root, inode);
2305 BUG_ON(ret);
2306
2307 ret = btrfs_commit_transaction(trans, new_root);
2308 BUG_ON(ret);
2309
2310 iput(inode);
2311
2312 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 2313 btrfs_btree_balance_dirty(root);
2619ba1f
CM
2314 return 0;
2315}
2316
c5739bba
CM
2317static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2318{
2319 struct btrfs_trans_handle *trans;
2320 struct btrfs_key key;
2321 struct btrfs_root_item new_root_item;
2322 int ret;
2323 u64 objectid;
2324
2619ba1f
CM
2325 if (!root->ref_cows)
2326 return -EINVAL;
2327
c5739bba
CM
2328 mutex_lock(&root->fs_info->fs_mutex);
2329 trans = btrfs_start_transaction(root, 1);
2330 BUG_ON(!trans);
2331
2332 ret = btrfs_update_inode(trans, root, root->inode);
2333 BUG_ON(ret);
2334
1b05da2e
CM
2335 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2336 0, &objectid);
c5739bba
CM
2337 BUG_ON(ret);
2338
c5739bba
CM
2339 memcpy(&new_root_item, &root->root_item,
2340 sizeof(new_root_item));
2341
c5739bba
CM
2342 key.objectid = objectid;
2343 key.offset = 1;
2344 key.flags = 0;
2345 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7eccb903 2346 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
c5739bba
CM
2347
2348 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2349 &new_root_item);
2350 BUG_ON(ret);
2351
c5739bba
CM
2352 /*
2353 * insert the directory item
2354 */
2355 key.offset = (u64)-1;
2356 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2357 name, namelen,
2358 root->fs_info->sb->s_root->d_inode->i_ino,
2359 &key, 0);
2360
2361 BUG_ON(ret);
2362
2363 ret = btrfs_inc_root_ref(trans, root);
2364 BUG_ON(ret);
2365
2366 ret = btrfs_commit_transaction(trans, root);
2367 BUG_ON(ret);
2368 mutex_unlock(&root->fs_info->fs_mutex);
35b7e476 2369 btrfs_btree_balance_dirty(root);
c5739bba
CM
2370 return 0;
2371}
2372
8352d8a4
CM
2373static int add_disk(struct btrfs_root *root, char *name, int namelen)
2374{
2375 struct block_device *bdev;
2376 struct btrfs_path *path;
2377 struct super_block *sb = root->fs_info->sb;
2378 struct btrfs_root *dev_root = root->fs_info->dev_root;
2379 struct btrfs_trans_handle *trans;
2380 struct btrfs_device_item *dev_item;
2381 struct btrfs_key key;
2382 u16 item_size;
2383 u64 num_blocks;
2384 u64 new_blocks;
b4100d64 2385 u64 device_id;
8352d8a4 2386 int ret;
b4100d64 2387
8352d8a4
CM
2388printk("adding disk %s\n", name);
2389 path = btrfs_alloc_path();
2390 if (!path)
2391 return -ENOMEM;
2392 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2393 bdev = open_bdev_excl(name, O_RDWR, sb);
2394 if (IS_ERR(bdev)) {
2395 ret = PTR_ERR(bdev);
2396printk("open bdev excl failed ret %d\n", ret);
2397 goto out_nolock;
2398 }
2399 set_blocksize(bdev, sb->s_blocksize);
2400 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2401 key.objectid = num_blocks;
2402 key.offset = new_blocks;
2403 key.flags = 0;
2404 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2405
2406 mutex_lock(&dev_root->fs_info->fs_mutex);
2407 trans = btrfs_start_transaction(dev_root, 1);
2408 item_size = sizeof(*dev_item) + namelen;
2409printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2410 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2411 if (ret) {
2412printk("insert failed %d\n", ret);
2413 close_bdev_excl(bdev);
2414 if (ret > 0)
2415 ret = -EEXIST;
2416 goto out;
2417 }
2418 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2419 path->slots[0], struct btrfs_device_item);
2420 btrfs_set_device_pathlen(dev_item, namelen);
2421 memcpy(dev_item + 1, name, namelen);
b4100d64
CM
2422
2423 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2424 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2425 btrfs_set_device_id(dev_item, device_id);
8352d8a4
CM
2426 mark_buffer_dirty(path->nodes[0]);
2427
b4100d64
CM
2428 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2429 new_blocks);
8352d8a4
CM
2430
2431 if (!ret) {
2432 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2433 num_blocks + new_blocks);
2434 i_size_write(root->fs_info->btree_inode,
2435 (num_blocks + new_blocks) <<
2436 root->fs_info->btree_inode->i_blkbits);
2437 }
2438
2439out:
2440 ret = btrfs_commit_transaction(trans, dev_root);
2441 BUG_ON(ret);
2442 mutex_unlock(&root->fs_info->fs_mutex);
2443out_nolock:
2444 btrfs_free_path(path);
35b7e476 2445 btrfs_btree_balance_dirty(root);
8352d8a4
CM
2446
2447 return ret;
2448}
2449
c5739bba
CM
2450static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2451 cmd, unsigned long arg)
2452{
2453 struct btrfs_root *root = BTRFS_I(inode)->root;
2454 struct btrfs_ioctl_vol_args vol_args;
8352d8a4 2455 int ret = 0;
7e38180e 2456 struct btrfs_dir_item *di;
c5739bba 2457 int namelen;
2619ba1f
CM
2458 struct btrfs_path *path;
2459 u64 root_dirid;
c5739bba 2460
c5739bba
CM
2461 switch (cmd) {
2462 case BTRFS_IOC_SNAP_CREATE:
2463 if (copy_from_user(&vol_args,
2464 (struct btrfs_ioctl_vol_args __user *)arg,
2465 sizeof(vol_args)))
2466 return -EFAULT;
2467 namelen = strlen(vol_args.name);
2468 if (namelen > BTRFS_VOL_NAME_MAX)
2469 return -EINVAL;
2619ba1f
CM
2470 path = btrfs_alloc_path();
2471 if (!path)
2472 return -ENOMEM;
2d13d8d0 2473 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2619ba1f 2474 mutex_lock(&root->fs_info->fs_mutex);
7e38180e 2475 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2619ba1f
CM
2476 path, root_dirid,
2477 vol_args.name, namelen, 0);
2478 mutex_unlock(&root->fs_info->fs_mutex);
2d13d8d0 2479 btrfs_free_path(path);
7e38180e 2480 if (di && !IS_ERR(di))
2619ba1f
CM
2481 return -EEXIST;
2482
2483 if (root == root->fs_info->tree_root)
2484 ret = create_subvol(root, vol_args.name, namelen);
2485 else
2486 ret = create_snapshot(root, vol_args.name, namelen);
c5739bba
CM
2487 WARN_ON(ret);
2488 break;
8352d8a4
CM
2489 case BTRFS_IOC_ADD_DISK:
2490 if (copy_from_user(&vol_args,
2491 (struct btrfs_ioctl_vol_args __user *)arg,
2492 sizeof(vol_args)))
2493 return -EFAULT;
2494 namelen = strlen(vol_args.name);
2495 if (namelen > BTRFS_VOL_NAME_MAX)
2496 return -EINVAL;
2497 vol_args.name[namelen] = '\0';
2498 ret = add_disk(root, vol_args.name, namelen);
2499 break;
c5739bba
CM
2500 default:
2501 return -ENOTTY;
2502 }
8352d8a4 2503 return ret;
c5739bba
CM
2504}
2505
2c90e5d6
CM
2506static struct kmem_cache *btrfs_inode_cachep;
2507struct kmem_cache *btrfs_trans_handle_cachep;
2508struct kmem_cache *btrfs_transaction_cachep;
2509struct kmem_cache *btrfs_bit_radix_cachep;
2510struct kmem_cache *btrfs_path_cachep;
2511
2512/*
2513 * Called inside transaction, so use GFP_NOFS
2514 */
2515static struct inode *btrfs_alloc_inode(struct super_block *sb)
2516{
2517 struct btrfs_inode *ei;
2518
2519 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2520 if (!ei)
2521 return NULL;
2c90e5d6
CM
2522 return &ei->vfs_inode;
2523}
2524
2525static void btrfs_destroy_inode(struct inode *inode)
2526{
2c90e5d6 2527 WARN_ON(!list_empty(&inode->i_dentry));
2c90e5d6
CM
2528 WARN_ON(inode->i_data.nrpages);
2529
2c90e5d6
CM
2530 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2531}
2532
2533static void init_once(void * foo, struct kmem_cache * cachep,
2534 unsigned long flags)
2535{
2536 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2537
f9f3c6b6 2538 if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
2c90e5d6
CM
2539 SLAB_CTOR_CONSTRUCTOR) {
2540 inode_init_once(&ei->vfs_inode);
2541 }
2542}
2543
2544static int init_inodecache(void)
2545{
2546 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2547 sizeof(struct btrfs_inode),
2548 0, (SLAB_RECLAIM_ACCOUNT|
2549 SLAB_MEM_SPREAD),
2550 init_once, NULL);
2551 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2552 sizeof(struct btrfs_trans_handle),
2553 0, (SLAB_RECLAIM_ACCOUNT|
2554 SLAB_MEM_SPREAD),
2555 NULL, NULL);
2556 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2557 sizeof(struct btrfs_transaction),
2558 0, (SLAB_RECLAIM_ACCOUNT|
2559 SLAB_MEM_SPREAD),
2560 NULL, NULL);
2561 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2562 sizeof(struct btrfs_transaction),
2563 0, (SLAB_RECLAIM_ACCOUNT|
2564 SLAB_MEM_SPREAD),
2565 NULL, NULL);
2566 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2567 256,
2568 0, (SLAB_RECLAIM_ACCOUNT|
2569 SLAB_MEM_SPREAD |
2570 SLAB_DESTROY_BY_RCU),
2571 NULL, NULL);
2572 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2573 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2574 return -ENOMEM;
2575 return 0;
2576}
2577
2578static void destroy_inodecache(void)
2579{
2580 kmem_cache_destroy(btrfs_inode_cachep);
2581 kmem_cache_destroy(btrfs_trans_handle_cachep);
2582 kmem_cache_destroy(btrfs_transaction_cachep);
2583 kmem_cache_destroy(btrfs_bit_radix_cachep);
2584 kmem_cache_destroy(btrfs_path_cachep);
2585}
2586
2e635a27
CM
2587static int btrfs_get_sb(struct file_system_type *fs_type,
2588 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2589{
2590 return get_sb_bdev(fs_type, flags, dev_name, data,
2591 btrfs_fill_super, mnt);
2592}
2593
236454df
CM
2594static int btrfs_getattr(struct vfsmount *mnt,
2595 struct dentry *dentry, struct kstat *stat)
2596{
2597 struct inode *inode = dentry->d_inode;
2598 generic_fillattr(inode, stat);
2599 stat->blksize = 256 * 1024;
2600 return 0;
2601}
2602
8fd17795
CM
2603static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2604{
2605 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2606 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2607
2608 buf->f_namelen = BTRFS_NAME_LEN;
2609 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2610 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2611 buf->f_bavail = buf->f_bfree;
2612 buf->f_bsize = dentry->d_sb->s_blocksize;
2613 buf->f_type = BTRFS_SUPER_MAGIC;
2614 return 0;
2615}
b5133862 2616
e06afa83
CM
2617static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2618 struct inode * new_dir,struct dentry *new_dentry)
2619{
2620 struct btrfs_trans_handle *trans;
2621 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2622 struct inode *new_inode = new_dentry->d_inode;
2623 struct inode *old_inode = old_dentry->d_inode;
2624 struct timespec ctime = CURRENT_TIME;
2625 struct btrfs_path *path;
2626 struct btrfs_dir_item *di;
2627 int ret;
2628
2629 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2630 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2631 return -ENOTEMPTY;
2632 }
2633 mutex_lock(&root->fs_info->fs_mutex);
2634 trans = btrfs_start_transaction(root, 1);
2635 btrfs_set_trans_block_group(trans, new_dir);
2636 path = btrfs_alloc_path();
2637 if (!path) {
2638 ret = -ENOMEM;
2639 goto out_fail;
2640 }
2641
2642 old_dentry->d_inode->i_nlink++;
2643 old_dir->i_ctime = old_dir->i_mtime = ctime;
2644 new_dir->i_ctime = new_dir->i_mtime = ctime;
2645 old_inode->i_ctime = ctime;
2646 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2647 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2648 u64 old_parent_oid;
2649 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2650 "..", 2, -1);
2651 if (IS_ERR(di)) {
2652 ret = PTR_ERR(di);
2653 goto out_fail;
2654 }
2655 if (!di) {
2656 ret = -ENOENT;
2657 goto out_fail;
2658 }
2659 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2660 ret = btrfs_del_item(trans, root, path);
2661 if (ret) {
2662 ret = -EIO;
2663 goto out_fail;
2664 }
2665 btrfs_release_path(root, path);
2666
2667 di = btrfs_lookup_dir_index_item(trans, root, path,
2668 old_inode->i_ino,
2669 old_parent_oid,
2670 "..", 2, -1);
2671 if (IS_ERR(di)) {
2672 ret = PTR_ERR(di);
2673 goto out_fail;
2674 }
2675 if (!di) {
2676 ret = -ENOENT;
2677 goto out_fail;
2678 }
2679 ret = btrfs_del_item(trans, root, path);
2680 if (ret) {
2681 ret = -EIO;
2682 goto out_fail;
2683 }
2684 btrfs_release_path(root, path);
2685
2686 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2687 old_inode->i_ino, location, 0);
2688 if (ret)
2689 goto out_fail;
2690 }
2691
2692
e06afa83
CM
2693 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2694 if (ret)
2695 goto out_fail;
2696
2697 if (new_inode) {
2698 new_inode->i_ctime = CURRENT_TIME;
2b8d99a7
CM
2699 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2700 if (ret)
2701 goto out_fail;
e06afa83
CM
2702 if (S_ISDIR(new_inode->i_mode))
2703 clear_nlink(new_inode);
2704 else
2705 drop_nlink(new_inode);
2706 btrfs_update_inode(trans, root, new_inode);
2707 }
2b8d99a7
CM
2708 ret = btrfs_add_link(trans, new_dentry, old_inode);
2709 if (ret)
2710 goto out_fail;
2711
e06afa83
CM
2712out_fail:
2713 btrfs_free_path(path);
2714 btrfs_end_transaction(trans, root);
2715 mutex_unlock(&root->fs_info->fs_mutex);
2716 return ret;
2717}
2718
2b8d99a7
CM
2719static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2720 const char *symname)
2721{
2722 struct btrfs_trans_handle *trans;
2723 struct btrfs_root *root = BTRFS_I(dir)->root;
2724 struct btrfs_path *path;
2725 struct btrfs_key key;
2726 struct inode *inode;
2727 int err;
2728 int drop_inode = 0;
2729 u64 objectid;
2730 int name_len;
2731 int datasize;
2732 char *ptr;
2733 struct btrfs_file_extent_item *ei;
2734
2735 name_len = strlen(symname) + 1;
2736 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2737 return -ENAMETOOLONG;
2738 mutex_lock(&root->fs_info->fs_mutex);
2739 trans = btrfs_start_transaction(root, 1);
2740 btrfs_set_trans_block_group(trans, dir);
2741
2742 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2743 if (err) {
2744 err = -ENOSPC;
2745 goto out_unlock;
2746 }
2747
2748 inode = btrfs_new_inode(trans, root, objectid,
2749 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2750 err = PTR_ERR(inode);
2751 if (IS_ERR(inode))
2752 goto out_unlock;
2753
2754 btrfs_set_trans_block_group(trans, inode);
2755 err = btrfs_add_nondir(trans, dentry, inode);
2756 if (err)
2757 drop_inode = 1;
2758 else {
2759 inode->i_mapping->a_ops = &btrfs_aops;
2760 inode->i_fop = &btrfs_file_operations;
2761 inode->i_op = &btrfs_file_inode_operations;
2762 }
2763 dir->i_sb->s_dirt = 1;
2764 btrfs_update_inode_block_group(trans, inode);
2765 btrfs_update_inode_block_group(trans, dir);
2766 if (drop_inode)
2767 goto out_unlock;
2768
2769 path = btrfs_alloc_path();
2770 BUG_ON(!path);
2771 key.objectid = inode->i_ino;
2772 key.offset = 0;
2773 key.flags = 0;
2774 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2775 datasize = btrfs_file_extent_calc_inline_size(name_len);
2776 err = btrfs_insert_empty_item(trans, root, path, &key,
2777 datasize);
2778 BUG_ON(err);
2779 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2780 path->slots[0], struct btrfs_file_extent_item);
2781 btrfs_set_file_extent_generation(ei, trans->transid);
2782 btrfs_set_file_extent_type(ei,
2783 BTRFS_FILE_EXTENT_INLINE);
2784 ptr = btrfs_file_extent_inline_start(ei);
2785 btrfs_memcpy(root, path->nodes[0]->b_data,
2786 ptr, symname, name_len);
2787 mark_buffer_dirty(path->nodes[0]);
2788 btrfs_free_path(path);
2789 inode->i_op = &btrfs_symlink_inode_operations;
2790 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2791 inode->i_size = name_len - 1;
2792 btrfs_update_inode(trans, root, inode);
2793 err = 0;
2794
2795out_unlock:
2796 btrfs_end_transaction(trans, root);
2797 mutex_unlock(&root->fs_info->fs_mutex);
2798
2799 if (drop_inode) {
2800 inode_dec_link_count(inode);
2801 iput(inode);
2802 }
2803 btrfs_btree_balance_dirty(root);
2804 return err;
2805}
2806
2e635a27
CM
2807static struct file_system_type btrfs_fs_type = {
2808 .owner = THIS_MODULE,
2809 .name = "btrfs",
2810 .get_sb = btrfs_get_sb,
2811 .kill_sb = kill_block_super,
2812 .fs_flags = FS_REQUIRES_DEV,
2813};
2814
e20d96d6 2815static struct super_operations btrfs_super_ops = {
134e9731 2816 .delete_inode = btrfs_delete_inode,
e20d96d6
CM
2817 .put_super = btrfs_put_super,
2818 .read_inode = btrfs_read_locked_inode,
d5719762
CM
2819 .write_super = btrfs_write_super,
2820 .sync_fs = btrfs_sync_fs,
4730a4bc 2821 .write_inode = btrfs_write_inode,
b5133862 2822 .dirty_inode = btrfs_dirty_inode,
2c90e5d6
CM
2823 .alloc_inode = btrfs_alloc_inode,
2824 .destroy_inode = btrfs_destroy_inode,
8fd17795 2825 .statfs = btrfs_statfs,
e20d96d6
CM
2826};
2827
2828static struct inode_operations btrfs_dir_inode_operations = {
2829 .lookup = btrfs_lookup,
d5719762 2830 .create = btrfs_create,
134e9731 2831 .unlink = btrfs_unlink,
2b8d99a7 2832 .link = btrfs_link,
f7922033 2833 .mkdir = btrfs_mkdir,
5f443fd2 2834 .rmdir = btrfs_rmdir,
e06afa83 2835 .rename = btrfs_rename,
2b8d99a7 2836 .symlink = btrfs_symlink,
3a686375 2837 .setattr = btrfs_setattr,
e20d96d6
CM
2838};
2839
d6e4a428
CM
2840static struct inode_operations btrfs_dir_ro_inode_operations = {
2841 .lookup = btrfs_lookup,
2842};
2843
e20d96d6
CM
2844static struct file_operations btrfs_dir_file_operations = {
2845 .llseek = generic_file_llseek,
2846 .read = generic_read_dir,
2847 .readdir = btrfs_readdir,
c5739bba 2848 .ioctl = btrfs_ioctl,
e20d96d6
CM
2849};
2850
dee26a9f
CM
2851static struct address_space_operations btrfs_aops = {
2852 .readpage = btrfs_readpage,
dee26a9f
CM
2853 .writepage = btrfs_writepage,
2854 .sync_page = block_sync_page,
2855 .prepare_write = btrfs_prepare_write,
75dfe396 2856 .commit_write = btrfs_commit_write,
dee26a9f
CM
2857};
2858
2b8d99a7
CM
2859static struct address_space_operations btrfs_symlink_aops = {
2860 .readpage = btrfs_readpage,
2861 .writepage = btrfs_writepage,
2862};
2863
dee26a9f 2864static struct inode_operations btrfs_file_inode_operations = {
f4b9aa8d 2865 .truncate = btrfs_truncate,
236454df 2866 .getattr = btrfs_getattr,
3a686375 2867 .setattr = btrfs_setattr,
dee26a9f
CM
2868};
2869
2870static struct file_operations btrfs_file_operations = {
2871 .llseek = generic_file_llseek,
2872 .read = do_sync_read,
e8f05c45
CM
2873 .aio_read = btrfs_file_aio_read,
2874 .write = btrfs_file_write,
dee26a9f
CM
2875 .mmap = generic_file_mmap,
2876 .open = generic_file_open,
c5739bba 2877 .ioctl = btrfs_ioctl,
8fd17795 2878 .fsync = btrfs_sync_file,
dee26a9f 2879};
e20d96d6 2880
2b8d99a7
CM
2881static struct inode_operations btrfs_symlink_inode_operations = {
2882 .readlink = generic_readlink,
2883 .follow_link = page_follow_link_light,
2884 .put_link = page_put_link,
2885};
2886
2e635a27
CM
2887static int __init init_btrfs_fs(void)
2888{
2c90e5d6 2889 int err;
2e635a27 2890 printk("btrfs loaded!\n");
2c90e5d6
CM
2891 err = init_inodecache();
2892 if (err)
2893 return err;
2e635a27 2894 return register_filesystem(&btrfs_fs_type);
d6e4a428
CM
2895 destroy_inodecache();
2896 return err;
2e635a27
CM
2897}
2898
2899static void __exit exit_btrfs_fs(void)
2900{
2c90e5d6 2901 destroy_inodecache();
2e635a27
CM
2902 unregister_filesystem(&btrfs_fs_type);
2903 printk("btrfs unloaded\n");
2904}
2905
2906module_init(init_btrfs_fs)
2907module_exit(exit_btrfs_fs)
2908
2909MODULE_LICENSE("GPL");
This page took 0.173134 seconds and 5 git commands to generate.