Btrfs: Switch to libcrc32c to avoid problems with cryptomgr on highmem machines
[firefly-linux-kernel-4.4.55.git] / fs / btrfs / disk-io.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/blkdev.h>
22 #include <linux/crc32c.h>
23 #include <linux/scatterlist.h>
24 #include <linux/swap.h>
25 #include <linux/radix-tree.h>
26 #include <linux/writeback.h>
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "btrfs_inode.h"
31
32 u64 bh_blocknr(struct buffer_head *bh)
33 {
34         return bh->b_blocknr;
35 }
36
37 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
38 {
39         struct btrfs_node *node = btrfs_buffer_node(buf);
40         if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
41                 printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
42                        (unsigned long long)bh_blocknr(buf),
43                        (unsigned long long)btrfs_header_blocknr(&node->header));
44                 return 1;
45         }
46         return 0;
47 }
48
49 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
50 {
51         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
52         int blockbits = root->fs_info->sb->s_blocksize_bits;
53         unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
54         struct page *page;
55         struct buffer_head *bh;
56         struct buffer_head *head;
57         struct buffer_head *ret = NULL;
58
59
60         page = find_lock_page(mapping, index);
61         if (!page)
62                 return NULL;
63
64         if (!page_has_buffers(page))
65                 goto out_unlock;
66
67         head = page_buffers(page);
68         bh = head;
69         do {
70                 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
71                         ret = bh;
72                         get_bh(bh);
73                         goto out_unlock;
74                 }
75                 bh = bh->b_this_page;
76         } while (bh != head);
77 out_unlock:
78         unlock_page(page);
79         page_cache_release(page);
80         return ret;
81 }
82
83 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
84                              u64 logical)
85 {
86         if (logical == 0) {
87                 bh->b_bdev = NULL;
88                 bh->b_blocknr = 0;
89                 set_buffer_mapped(bh);
90         } else {
91                 map_bh(bh, root->fs_info->sb, logical);
92         }
93         return 0;
94 }
95
96 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
97                                                  u64 blocknr)
98 {
99         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
100         int blockbits = root->fs_info->sb->s_blocksize_bits;
101         unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
102         struct page *page;
103         struct buffer_head *bh;
104         struct buffer_head *head;
105         struct buffer_head *ret = NULL;
106         int err;
107         u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
108
109         page = find_or_create_page(mapping, index, GFP_NOFS);
110         if (!page)
111                 return NULL;
112
113         if (!page_has_buffers(page))
114                 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
115         head = page_buffers(page);
116         bh = head;
117         do {
118                 if (!buffer_mapped(bh)) {
119                         err = btrfs_map_bh_to_logical(root, bh, first_block);
120                         BUG_ON(err);
121                 }
122                 if (bh_blocknr(bh) == blocknr) {
123                         ret = bh;
124                         get_bh(bh);
125                         goto out_unlock;
126                 }
127                 bh = bh->b_this_page;
128                 first_block++;
129         } while (bh != head);
130 out_unlock:
131         unlock_page(page);
132         if (ret)
133                 touch_buffer(ret);
134         page_cache_release(page);
135         return ret;
136 }
137
138 static int btree_get_block(struct inode *inode, sector_t iblock,
139                            struct buffer_head *bh, int create)
140 {
141         int err;
142         struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
143         err = btrfs_map_bh_to_logical(root, bh, iblock);
144         return err;
145 }
146
147 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
148                     char *result)
149 {
150         u32 crc;
151         crc = crc32c(0, data, len);
152         memcpy(result, &crc, BTRFS_CRC32_SIZE);
153         return 0;
154 }
155
156 static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
157                            int verify)
158 {
159         char result[BTRFS_CRC32_SIZE];
160         int ret;
161         struct btrfs_node *node;
162
163         ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
164                               bh->b_size - BTRFS_CSUM_SIZE, result);
165         if (ret)
166                 return ret;
167         if (verify) {
168                 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
169                         printk("btrfs: %s checksum verify failed on %llu\n",
170                                root->fs_info->sb->s_id,
171                                (unsigned long long)bh_blocknr(bh));
172                         return 1;
173                 }
174         } else {
175                 node = btrfs_buffer_node(bh);
176                 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
177         }
178         return 0;
179 }
180
181 static int btree_writepage(struct page *page, struct writeback_control *wbc)
182 {
183         struct buffer_head *bh;
184         struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
185         struct buffer_head *head;
186         if (!page_has_buffers(page)) {
187                 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
188                                         (1 << BH_Dirty)|(1 << BH_Uptodate));
189         }
190         head = page_buffers(page);
191         bh = head;
192         do {
193                 if (buffer_dirty(bh))
194                         csum_tree_block(root, bh, 0);
195                 bh = bh->b_this_page;
196         } while (bh != head);
197         return block_write_full_page(page, btree_get_block, wbc);
198 }
199
200 static int btree_readpage(struct file * file, struct page * page)
201 {
202         return block_read_full_page(page, btree_get_block);
203 }
204
205 static struct address_space_operations btree_aops = {
206         .readpage       = btree_readpage,
207         .writepage      = btree_writepage,
208         .sync_page      = block_sync_page,
209 };
210
211 int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
212 {
213         struct buffer_head *bh = NULL;
214         int ret = 0;
215
216         bh = btrfs_find_create_tree_block(root, blocknr);
217         if (!bh)
218                 return 0;
219         if (buffer_uptodate(bh)) {
220                 ret = 1;
221                 goto done;
222         }
223         if (test_set_buffer_locked(bh)) {
224                 ret = 1;
225                 goto done;
226         }
227         if (!buffer_uptodate(bh)) {
228                 get_bh(bh);
229                 bh->b_end_io = end_buffer_read_sync;
230                 submit_bh(READ, bh);
231         } else {
232                 unlock_buffer(bh);
233                 ret = 1;
234         }
235 done:
236         brelse(bh);
237         return ret;
238 }
239
240 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
241 {
242         struct buffer_head *bh = NULL;
243
244         bh = btrfs_find_create_tree_block(root, blocknr);
245         if (!bh)
246                 return bh;
247         if (buffer_uptodate(bh))
248                 goto uptodate;
249         lock_buffer(bh);
250         if (!buffer_uptodate(bh)) {
251                 get_bh(bh);
252                 bh->b_end_io = end_buffer_read_sync;
253                 submit_bh(READ, bh);
254                 wait_on_buffer(bh);
255                 if (!buffer_uptodate(bh))
256                         goto fail;
257         } else {
258                 unlock_buffer(bh);
259         }
260 uptodate:
261         if (!buffer_checked(bh)) {
262                 csum_tree_block(root, bh, 1);
263                 set_buffer_checked(bh);
264         }
265         if (check_tree_block(root, bh))
266                 goto fail;
267         return bh;
268 fail:
269         brelse(bh);
270         return NULL;
271 }
272
273 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
274                      struct buffer_head *buf)
275 {
276         WARN_ON(atomic_read(&buf->b_count) == 0);
277         mark_buffer_dirty(buf);
278         return 0;
279 }
280
281 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
282                      struct buffer_head *buf)
283 {
284         WARN_ON(atomic_read(&buf->b_count) == 0);
285         clear_buffer_dirty(buf);
286         return 0;
287 }
288
289 static int __setup_root(int blocksize,
290                         struct btrfs_root *root,
291                         struct btrfs_fs_info *fs_info,
292                         u64 objectid)
293 {
294         root->node = NULL;
295         root->inode = NULL;
296         root->commit_root = NULL;
297         root->blocksize = blocksize;
298         root->ref_cows = 0;
299         root->fs_info = fs_info;
300         root->objectid = objectid;
301         root->last_trans = 0;
302         root->highest_inode = 0;
303         root->last_inode_alloc = 0;
304         memset(&root->root_key, 0, sizeof(root->root_key));
305         memset(&root->root_item, 0, sizeof(root->root_item));
306         root->root_key.objectid = objectid;
307         return 0;
308 }
309
310 static int find_and_setup_root(int blocksize,
311                                struct btrfs_root *tree_root,
312                                struct btrfs_fs_info *fs_info,
313                                u64 objectid,
314                                struct btrfs_root *root)
315 {
316         int ret;
317
318         __setup_root(blocksize, root, fs_info, objectid);
319         ret = btrfs_find_last_root(tree_root, objectid,
320                                    &root->root_item, &root->root_key);
321         BUG_ON(ret);
322
323         root->node = read_tree_block(root,
324                                      btrfs_root_blocknr(&root->root_item));
325         BUG_ON(!root->node);
326         return 0;
327 }
328
329 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
330                                       struct btrfs_key *location)
331 {
332         struct btrfs_root *root;
333         struct btrfs_root *tree_root = fs_info->tree_root;
334         struct btrfs_path *path;
335         struct btrfs_leaf *l;
336         u64 highest_inode;
337         int ret = 0;
338
339         root = radix_tree_lookup(&fs_info->fs_roots_radix,
340                                  (unsigned long)location->objectid);
341         if (root)
342                 return root;
343         root = kmalloc(sizeof(*root), GFP_NOFS);
344         if (!root)
345                 return ERR_PTR(-ENOMEM);
346         if (location->offset == (u64)-1) {
347                 ret = find_and_setup_root(fs_info->sb->s_blocksize,
348                                           fs_info->tree_root, fs_info,
349                                           location->objectid, root);
350                 if (ret) {
351                         kfree(root);
352                         return ERR_PTR(ret);
353                 }
354                 goto insert;
355         }
356
357         __setup_root(fs_info->sb->s_blocksize, root, fs_info,
358                      location->objectid);
359
360         path = btrfs_alloc_path();
361         BUG_ON(!path);
362         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
363         if (ret != 0) {
364                 if (ret > 0)
365                         ret = -ENOENT;
366                 goto out;
367         }
368         l = btrfs_buffer_leaf(path->nodes[0]);
369         memcpy(&root->root_item,
370                btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
371                sizeof(root->root_item));
372         memcpy(&root->root_key, location, sizeof(*location));
373         ret = 0;
374 out:
375         btrfs_release_path(root, path);
376         btrfs_free_path(path);
377         if (ret) {
378                 kfree(root);
379                 return ERR_PTR(ret);
380         }
381         root->node = read_tree_block(root,
382                                      btrfs_root_blocknr(&root->root_item));
383         BUG_ON(!root->node);
384 insert:
385         root->ref_cows = 1;
386         ret = radix_tree_insert(&fs_info->fs_roots_radix,
387                                 (unsigned long)root->root_key.objectid,
388                                 root);
389         if (ret) {
390                 brelse(root->node);
391                 kfree(root);
392                 return ERR_PTR(ret);
393         }
394         ret = btrfs_find_highest_inode(root, &highest_inode);
395         if (ret == 0) {
396                 root->highest_inode = highest_inode;
397                 root->last_inode_alloc = highest_inode;
398         }
399         return root;
400 }
401
402 struct btrfs_root *open_ctree(struct super_block *sb)
403 {
404         struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
405                                                  GFP_NOFS);
406         struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
407                                                GFP_NOFS);
408         struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
409                                                 GFP_NOFS);
410         int ret;
411         int err = -EIO;
412         struct btrfs_super_block *disk_super;
413
414         if (!extent_root || !tree_root || !fs_info) {
415                 err = -ENOMEM;
416                 goto fail;
417         }
418         init_bit_radix(&fs_info->pinned_radix);
419         init_bit_radix(&fs_info->pending_del_radix);
420         init_bit_radix(&fs_info->extent_map_radix);
421         INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
422         INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
423         INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
424         INIT_LIST_HEAD(&fs_info->trans_list);
425         INIT_LIST_HEAD(&fs_info->dead_roots);
426         sb_set_blocksize(sb, 4096);
427         fs_info->running_transaction = NULL;
428         fs_info->tree_root = tree_root;
429         fs_info->extent_root = extent_root;
430         fs_info->sb = sb;
431         fs_info->btree_inode = new_inode(sb);
432         fs_info->btree_inode->i_ino = 1;
433         fs_info->btree_inode->i_nlink = 1;
434         fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
435         fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
436         fs_info->do_barriers = 1;
437         fs_info->extent_tree_insert_nr = 0;
438         fs_info->extent_tree_prealloc_nr = 0;
439         fs_info->closing = 0;
440
441         INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
442         BTRFS_I(fs_info->btree_inode)->root = tree_root;
443         memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
444                sizeof(struct btrfs_key));
445         insert_inode_hash(fs_info->btree_inode);
446         mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
447
448         mutex_init(&fs_info->trans_mutex);
449         mutex_init(&fs_info->fs_mutex);
450
451         __setup_root(sb->s_blocksize, tree_root,
452                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
453
454         fs_info->sb_buffer = read_tree_block(tree_root,
455                                              BTRFS_SUPER_INFO_OFFSET /
456                                              sb->s_blocksize);
457
458         if (!fs_info->sb_buffer)
459                 goto fail_iput;
460         disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
461
462         if (!btrfs_super_root(disk_super))
463                 goto fail_sb_buffer;
464
465         i_size_write(fs_info->btree_inode,
466                      btrfs_super_total_blocks(disk_super) <<
467                      fs_info->btree_inode->i_blkbits);
468
469         fs_info->disk_super = disk_super;
470
471         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
472                     sizeof(disk_super->magic))) {
473                 printk("btrfs: valid FS not found on %s\n", sb->s_id);
474                 goto fail_sb_buffer;
475         }
476         tree_root->node = read_tree_block(tree_root,
477                                           btrfs_super_root(disk_super));
478         if (!tree_root->node)
479                 goto fail_sb_buffer;
480
481         mutex_lock(&fs_info->fs_mutex);
482         ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
483                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
484         if (ret) {
485                 mutex_unlock(&fs_info->fs_mutex);
486                 goto fail_tree_root;
487         }
488
489         btrfs_read_block_groups(extent_root);
490
491         fs_info->generation = btrfs_super_generation(disk_super) + 1;
492         mutex_unlock(&fs_info->fs_mutex);
493         return tree_root;
494
495 fail_tree_root:
496         btrfs_block_release(tree_root, tree_root->node);
497 fail_sb_buffer:
498         btrfs_block_release(tree_root, fs_info->sb_buffer);
499 fail_iput:
500         iput(fs_info->btree_inode);
501 fail:
502         kfree(extent_root);
503         kfree(tree_root);
504         kfree(fs_info);
505         return ERR_PTR(err);
506 }
507
508 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
509                       *root)
510 {
511         int ret;
512         struct buffer_head *bh = root->fs_info->sb_buffer;
513
514         btrfs_set_super_root(root->fs_info->disk_super,
515                              bh_blocknr(root->fs_info->tree_root->node));
516         lock_buffer(bh);
517         WARN_ON(atomic_read(&bh->b_count) < 1);
518         clear_buffer_dirty(bh);
519         csum_tree_block(root, bh, 0);
520         bh->b_end_io = end_buffer_write_sync;
521         get_bh(bh);
522         if (root->fs_info->do_barriers)
523                 ret = submit_bh(WRITE_BARRIER, bh);
524         else
525                 ret = submit_bh(WRITE, bh);
526         if (ret == -EOPNOTSUPP) {
527                 get_bh(bh);
528                 lock_buffer(bh);
529                 set_buffer_uptodate(bh);
530                 root->fs_info->do_barriers = 0;
531                 ret = submit_bh(WRITE, bh);
532         }
533         wait_on_buffer(bh);
534         if (!buffer_uptodate(bh)) {
535                 WARN_ON(1);
536                 return -EIO;
537         }
538         return 0;
539 }
540
541 static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
542 {
543         radix_tree_delete(&fs_info->fs_roots_radix,
544                           (unsigned long)root->root_key.objectid);
545         if (root->inode)
546                 iput(root->inode);
547         if (root->node)
548                 brelse(root->node);
549         if (root->commit_root)
550                 brelse(root->commit_root);
551         kfree(root);
552         return 0;
553 }
554
555 static int del_fs_roots(struct btrfs_fs_info *fs_info)
556 {
557         int ret;
558         struct btrfs_root *gang[8];
559         int i;
560
561         while(1) {
562                 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
563                                              (void **)gang, 0,
564                                              ARRAY_SIZE(gang));
565                 if (!ret)
566                         break;
567                 for (i = 0; i < ret; i++)
568                         free_fs_root(fs_info, gang[i]);
569         }
570         return 0;
571 }
572
573 int close_ctree(struct btrfs_root *root)
574 {
575         int ret;
576         struct btrfs_trans_handle *trans;
577         struct btrfs_fs_info *fs_info = root->fs_info;
578
579         fs_info->closing = 1;
580         btrfs_transaction_flush_work(root);
581         mutex_lock(&fs_info->fs_mutex);
582         trans = btrfs_start_transaction(root, 1);
583         btrfs_commit_transaction(trans, root);
584         /* run commit again to  drop the original snapshot */
585         trans = btrfs_start_transaction(root, 1);
586         btrfs_commit_transaction(trans, root);
587         ret = btrfs_write_and_wait_transaction(NULL, root);
588         BUG_ON(ret);
589         write_ctree_super(NULL, root);
590         mutex_unlock(&fs_info->fs_mutex);
591
592         if (fs_info->extent_root->node)
593                 btrfs_block_release(fs_info->extent_root,
594                                     fs_info->extent_root->node);
595         if (fs_info->tree_root->node)
596                 btrfs_block_release(fs_info->tree_root,
597                                     fs_info->tree_root->node);
598         btrfs_block_release(root, fs_info->sb_buffer);
599         truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
600         iput(fs_info->btree_inode);
601
602         btrfs_free_block_groups(root->fs_info);
603         del_fs_roots(fs_info);
604         kfree(fs_info->extent_root);
605         kfree(fs_info->tree_root);
606         return 0;
607 }
608
609 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
610 {
611         brelse(buf);
612 }
613
614 void btrfs_btree_balance_dirty(struct btrfs_root *root)
615 {
616         balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
617 }