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