forked from vdfs-team/vdfs4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.c
2389 lines (2029 loc) · 62.8 KB
/
super.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* VDFS4 -- Vertically Deliberate improved performance File System
*
* Copyright 2012 by Samsung Electronics, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/vfs.h>
#include <linux/buffer_head.h>
#include <linux/crc32.h>
#include <linux/version.h>
#include <linux/genhd.h>
#include <linux/exportfs.h>
#include <linux/vmalloc.h>
#include <linux/writeback.h>
#include <linux/bootmem.h>
#include <linux/mutex.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/freezer.h>
#include <linux/blkdev.h>
#include <linux/crypto.h>
#include <linux/random.h>
#include "vdfs4_layout.h"
#include "vdfs4.h"
#include "btree.h"
#include "debug.h"
#include "cattree.h"
#include "exttree.h"
#include "xattrtree.h"
#ifdef CONFIG_VDFS4_AUTHENTICATION
#include <crypto/crypto_wrapper.h>
#include "public_key.h"
#endif
#define CREATE_TRACE_POINTS
#include <trace/events/vdfs4.h>
#include <linux/vdfs_trace.h> /* FlashFS : vdfs-trace */
#include "lock_trace.h"
static inline void vdfs4_check_layout(void)
{
BUILD_BUG_ON(sizeof(struct vdfs4_timespec) != 12);
BUILD_BUG_ON(sizeof(struct vdfs4_extent) != 16);
BUILD_BUG_ON(sizeof(struct vdfs4_iextent) != 24);
BUILD_BUG_ON(sizeof(struct vdfs4_catalog_folder_record) != 10 * 8);
BUILD_BUG_ON(sizeof(struct vdfs4_fork) != 29 * 8);
BUILD_BUG_ON(sizeof(struct vdfs4_catalog_file_record) != 39 * 8);
BUILD_BUG_ON(sizeof(struct vdfs4_snapshot_descriptor) != 24);
BUILD_BUG_ON(sizeof(struct vdfs4_base_table) != 24 + VDFS4_SF_NR * 16);
BUILD_BUG_ON(sizeof(struct vdfs4_extended_table) != 24 + 8);
BUILD_BUG_ON(sizeof(struct vdfs4_super_block) != 512);
BUILD_BUG_ON(sizeof(struct vdfs4_volume_begins) != 512);
BUILD_BUG_ON(sizeof(struct vdfs4_extended_super_block) != 512 * 5);
BUILD_BUG_ON(sizeof(struct vdfs4_gen_node_descr) != 8 * 4);
BUILD_BUG_ON(sizeof(struct generic_index_value) != 4);
BUILD_BUG_ON(sizeof(struct vdfs4_catalog_hlink_record) != 1 * 8);
BUILD_BUG_ON(sizeof(struct vdfs4_comp_extent) != 2 * 8);
BUILD_BUG_ON(sizeof(struct vdfs4_comp_file_descr) != 5 * 8);
}
static struct lock_class_key catalog_tree_lock_key;
static struct lock_class_key extents_tree_lock_key;
static struct lock_class_key xattr_tree_lock_key;
static struct kset *vdfs4_kset;
static const char * const supported_layouts[] = {
/* current layout */
VDFS4_LAYOUT_VERSION_2007,
/* with RSA1024 hardcoded */
VDFS4_LAYOUT_VERSION_2006,
};
#define VDFS4_NUM_OF_SUPPORTED_LAYOUTS ARRAY_SIZE(supported_layouts)
enum sign_type get_sign_type_from_sb(struct vdfs4_super_block *sb)
{
enum sign_type type;
#ifdef CONFIG_VDFS4_ALLOW_LEGACY_SIGN
if (!memcmp(sb->layout_version, VDFS4_LAYOUT_VERSION_2006,
strlen(VDFS4_LAYOUT_VERSION_2006)))
return VDFS4_SIGN_RSA1024;
#endif
if (sb->sign_type < VDFS4_SIGN_MAX) {
type = sb->sign_type;
#ifndef CONFIG_VDFS4_ALLOW_LEGACY_SIGN
if (type == VDFS4_SIGN_RSA1024)
return VDFS4_SIGN_NONE;
#endif
} else
type = VDFS4_SIGN_NONE;
return type;
}
/**
* @brief B-tree destructor.
* @param [in,out] btree Pointer to btree that will be destroyed
* @return void
*/
void vdfs4_put_btree(struct vdfs4_btree *btree, int iput_inode)
{
int i;
vdfs4_put_bnode(btree->head_bnode);
for (i = 0; i < VDFS4_BNODE_HASH_SIZE; i++)
VDFS4_BUG_ON(!hlist_empty(btree->hash_table + i), btree->sbi);
vdfs4_destroy_free_bnode_bitmap(btree->bitmap);
VDFS4_BUG_ON(!btree->head_bnode, btree->sbi);
if (iput_inode)
iput(btree->inode);
kfree(btree->split_buff);
kfree(btree);
}
/**
* @brief Inode bitmap destructor.
* @param [in,out] sbi Pointer to sb info which free_inode_bitmap
* will be destroyed
* @return void
*/
static void destroy_free_inode_bitmap(struct vdfs4_sb_info *sbi)
{
iput(sbi->free_inode_bitmap.inode);
sbi->free_inode_bitmap.inode = NULL;
}
/** Debug mask is a module parameter. This parameter enables particular debug
* type printing (see VDFS4_DBG_* in fs/vdfs4/debug.h). */
unsigned int vdfs4_debug_mask = 0
/*+ VDFS4_DBG_INO*/
/*+ VDFS4_DBG_FSM*/
/*+ VDFS4_DBG_SNAPSHOT*/
/*+ VDFS4_DBG_TRANSACTION*/
+ VDFS4_DBG_TMP
;
/** The eMMCFS inode cache
*/
static struct kmem_cache *vdfs4_inode_cachep;
void vdfs4_init_inode(struct vdfs4_inode_info *inode)
{
inode->name = NULL;
inode->fork.total_block_count = 0;
inode->record_type = 0;
inode->fork.prealloc_block_count = 0;
inode->fork.prealloc_start_block = 0;
inode->flags = 0;
inode->fbc = NULL;
#ifdef CONFIG_VDFS4_SQUEEZE
inode->sci = NULL;
INIT_LIST_HEAD(&inode->to_read_list);
spin_lock_init(&inode->to_read_lock);
#endif
#ifdef CONFIG_VDFS4_TRACE
inode->write_size = 0;
#endif
INIT_LIST_HEAD(&inode->orphan_list);
inode->next_orphan_id = (u64)(-1);
}
/**
* @brief Method to allocate inode.
* @param [in,out] sb Pointer to eMMCFS superblock
* @return Returns pointer to inode, NULL on failure
*/
static struct inode *vdfs4_alloc_inode(struct super_block *sb)
{
struct vdfs4_inode_info *inode;
trace_vdfs4_alloc_inode(sb);
inode = kmem_cache_alloc(vdfs4_inode_cachep, GFP_NOFS);
if (!inode)
return NULL;
vdfs4_init_inode(inode);
return &inode->vfs_inode;
}
static void vdfs4_i_callback(struct rcu_head *head)
{
struct inode *inode = container_of(head, struct inode, i_rcu);
struct vdfs4_inode_info *inode_info = VDFS4_I(inode);
kmem_cache_free(vdfs4_inode_cachep, inode_info);
}
/**
* @brief Method to destroy inode.
* @param [in,out] inode Pointer to inode for destroy
* @return void
*/
static void vdfs4_destroy_inode(struct inode *inode)
{
struct vdfs4_inode_info *inode_info = VDFS4_I(inode);
trace_vdfs4_destroy_inode(inode);
kfree(inode_info->name);
kfree(inode_info->fbc);
#ifdef CONFIG_VDFS4_SQUEEZE
kfree(inode_info->sci);
#endif
call_rcu(&inode->i_rcu, vdfs4_i_callback);
}
/**
* @brief Sync superblock.
* @param [in] sb Superblock information
* @param [in] no 0:first / 1:second super block
* @return Returns error code
*/
int vdfs4_sync_exsb(struct vdfs4_sb_info *sbi, int no)
{
int ret = 0;
__u32 checksum;
struct vdfs4_layout_sb *l_sb = sbi->raw_superblock;
struct vdfs4_extended_super_block *exsb = &l_sb->exsb;
struct vdfs4_snapshot_info *snapshot = sbi->snapshot_info;
int offset;
lock_page(sbi->superblocks);
exsb->files_count = cpu_to_le64(sbi->files_count);
exsb->folders_count = cpu_to_le64(sbi->folders_count);
exsb->sync_counter = cpu_to_le32(snapshot->sync_count);
/* Update cheksum */
checksum = crc32(0, exsb, sizeof(*exsb) - sizeof(exsb->checksum));
exsb->checksum = cpu_to_le32(checksum);
if (no == 0)
offset = VDFS4_1ST_EXSB_ADDR;
else
offset = VDFS4_2ND_EXSB_ADDR;
set_page_writeback(sbi->superblocks);
ret = vdfs4_write_page(sbi, offset, sbi->superblocks,
SECTOR_TO_BYTE(VDFS4_EXSB_OFFSET),
SECTOR_TO_BYTE(VDFS4_EXSB_SIZE), 1);
unlock_page(sbi->superblocks);
return ret;
}
/**
* @brief Method to write out all dirty data associated
* with the superblock.
* @param [in,out] sb Pointer to the eMMCFS superblock
* @param [in,out] wait Block on write completion
* @return Returns 0 on success, errno on failure
*/
int vdfs4_sync_fs(struct super_block *sb, int wait)
{
struct vdfs4_sb_info *sbi = sb->s_fs_info;
struct vdfs4_snapshot_info *si = sbi->snapshot_info;
int ret = 0;
VT_PREPARE_PARAM(vt_data);
VT_SBOPS_START(vt_data, vdfs_trace_sb_sync_fs, sb, NULL, 0, 0);
trace_vdfs4_sync_fs_enter(sb, wait);
if (wait) {
int *transaction_count = (int *)¤t->journal_info;
struct list_head *iter;
down_write(&si->transaction_lock);
(*transaction_count)++;
sync_inodes_sb(sb);
down_write(&si->writeback_lock);
list_for_each(iter, &sbi->orphan_inodes) {
struct vdfs4_inode_info *inode_i = list_entry(iter,
struct vdfs4_inode_info, orphan_list);
struct inode *inode = &inode_i->vfs_inode;
struct address_space *mapping = inode->i_mapping;
if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
(mapping->nrpages == 0))
if ((S_ISREG(inode->i_mode) ||
S_ISLNK(inode->i_mode)) &&
(inode_i->vfs_inode.i_ino != 1)) {
vdfs4_cattree_w_lock(sbi);
/* an error is handled in __vdfs4_write_inode */
__vdfs4_write_inode(sbi, inode);
vdfs4_cattree_w_unlock(sbi);
}
}
ret = vdfs4_sync_metadata(sbi);
if (ret)
vdfs4_fatal_error(sbi, VDFS4_DEBUG_ERR_META_COMMIT,
0, "metadata commit failed:%d", ret);
up_write(&si->writeback_lock);
(*transaction_count)--;
up_write(&si->transaction_lock);
}
trace_vdfs4_sync_fs_exit(sb, ret);
VT_FINISH(vt_data);
return ret;
}
static void vdfs4_delayed_commit(struct work_struct *work)
{
struct vdfs4_sb_info *sbi = container_of(to_delayed_work(work),
struct vdfs4_sb_info, delayed_commit);
struct super_block *sb = sbi->sb;
if (down_read_trylock(&sb->s_umount)) {
sync_filesystem(sb);
up_read(&sb->s_umount);
} else if (sb->s_flags & MS_ACTIVE) {
/* Try again later */
mod_delayed_work(system_wq, &sbi->delayed_commit, HZ);
}
}
#ifdef CONFIG_VDFS4_SQUEEZE_PROFILING
static void vdfs4_delayed_prof_write(struct work_struct *work)
{
struct vdfs4_sb_info *sbi = container_of(to_delayed_work(work),
struct vdfs4_sb_info,
prof_task);
struct super_block *sb = sbi->sb;
if (down_read_trylock(&sb->s_umount)) {
if (IS_ERR(sbi->prof_file)) {
const char path[] = "/prof.bin";
VDFS4_DEBUG_TMP("opening profiling file %s", path);
sbi->prof_file = filp_open((const char *)path, O_CREAT |
O_WRONLY | O_TRUNC, S_IRWXU);
if (IS_ERR(sbi->prof_file))
VDFS4_DEBUG_TMP("fail %p", sbi->prof_file);
}
if (!IS_ERR(sbi->prof_file)) {
loff_t pos;
ssize_t written;
struct vdfs4_prof_data prof_data;
u32 size;
do {
size = kfifo_out(&sbi->prof_fifo, &prof_data,
sizeof(prof_data));
if (size) {
mm_segment_t fs = get_fs();
set_fs(KERNEL_DS);
pos = sbi->prof_file->f_path.dentry->d_inode->i_size;
written = vfs_write(sbi->prof_file, (char *)&prof_data,
size, &pos);
if (written < 0)
VDFS4_ERR("cannot write to file err:%zd", written);
set_fs(fs);
}
} while (size);
}
up_read(&sb->s_umount);
}
mod_delayed_work(system_wq, &sbi->prof_task, HZ);
}
#endif
/**
* @brief Method to free superblock (unmount).
* @param [in,out] sbi Pointer to the eMMCFS superblock
* @return void
*/
static void destroy_super(struct vdfs4_sb_info *sbi)
{
sbi->raw_superblock_copy = NULL;
sbi->raw_superblock = NULL;
if (sbi->superblocks) {
kunmap(sbi->superblocks);
__free_pages(sbi->superblocks, 0);
}
if (sbi->superblocks_copy) {
kunmap(sbi->superblocks_copy);
__free_pages(sbi->superblocks_copy, 0);
}
if (sbi->raw_meta_hashtable)
vfree(sbi->raw_meta_hashtable);
}
/**
* @brief Method to free sbi info.
* @param [in,out] sb Pointer to a superblock
* @return void
*/
static void vdfs4_put_super(struct super_block *sb)
{
struct vdfs4_sb_info *sbi = sb->s_fs_info;
VT_PREPARE_PARAM(vt_data);
VT_SBOPS_START(vt_data, vdfs_trace_sb_put_super, sb, NULL, 0, 0);
cancel_delayed_work_sync(&sbi->delayed_commit);
sbi->umount_time = 1;
vdfs4_put_btree(sbi->catalog_tree, 1);
sbi->catalog_tree = NULL;
vdfs4_put_btree(sbi->extents_tree, 1);
sbi->extents_tree = NULL;
vdfs4_put_btree(sbi->xattr_tree, 1);
sbi->xattr_tree = NULL;
if (sbi->free_inode_bitmap.inode)
destroy_free_inode_bitmap(sbi);
if (sbi->fsm_info)
vdfs4_fsm_destroy_management(sb);
#ifdef CONFIG_VDFS4_AUTHENTICATION
if (sbi->rsa_key)
destroy_rsa_key(sbi->rsa_key);
#ifdef CONFIG_VDFS4_ALLOW_LEGACY_SIGN
if (sbi->rsa_key_legacy)
destroy_rsa_key(sbi->rsa_key_legacy);
#endif
#endif
/* if (VDFS4_DEBUG_PAGES(sbi))
vdfs4_free_debug_area(sb);*/
if (sbi->snapshot_info)
vdfs4_destroy_snapshot_manager(sbi);
kobject_del(&sbi->s_kobj);
kobject_put(&sbi->s_kobj);
wait_for_completion(&sbi->s_kobj_unregister);
destroy_super(sbi);
kfree(sbi);
VDFS4_DEBUG_SB("finished");
VT_FINISH(vt_data);
}
/**
* @brief This function handle umount start.
* @param [in,out] sb Pointer to a superblock
* @return void
*/
static void vdfs4_umount_begin(struct super_block *sb) {}
/**
* @brief Force FS into a consistency state and
* lock it (for LVM).
* @param [in,out] sb Pointer to the eMMCFS superblock
* @remark TODO: detailed description
* @return Returns 0 on success, errno on failure
*/
static int vdfs4_freeze(struct super_block *sb)
{
/* d.voytik-TODO-29-12-2011-17-24-00: [vdfs4_freeze]
* implement vdfs4_freeze() */
int ret = 0;
VDFS4_DEBUG_SB("finished (ret = %d)", ret);
return ret;
}
/**
* @brief Calculates metadata size, using extended
* superblock's forks
* @param [in,out] sbi Pointer to the eMMCFS superblock
* @return metadata size in 4K-blocks
*/
static u64 calc_special_files_size(struct vdfs4_sb_info *sbi)
{
u64 res = 0;
struct vdfs4_layout_sb *l_sb = sbi->raw_superblock;
struct vdfs4_extended_super_block *exsb = &l_sb->exsb;
res += le64_to_cpu(exsb->meta_tbc);
res += le64_to_cpu(exsb->tables.length);
return res;
}
/**
* @brief Get FS statistics.
* @param [in,out] dentry Pointer to directory entry
* @param [in,out] buf Point to kstatfs buffer where information
* will be placed
* @return Returns 0 on success, errno on failure
*/
static int vdfs4_statfs(struct dentry *dentry, struct kstatfs *buf)
{
struct super_block *sb = dentry->d_sb;
struct vdfs4_sb_info *sbi = sb->s_fs_info;
struct vdfs4_layout_sb *l_sb = sbi->raw_superblock;
buf->f_type = (long) VDFS4_SB_SIGNATURE;
buf->f_bsize = (long int)sbi->block_size;
buf->f_blocks = sbi->volume_blocks_count;
buf->f_bavail = buf->f_bfree = VDFS4_GET_FREE_BLOCKS_COUNT(sbi);
buf->f_files = sbi->files_count + sbi->folders_count + 0xfefefe;
memcpy((void *)&buf->f_fsid.val[0], l_sb->exsb.volume_uuid,
sizeof(int));
memcpy((void *)&buf->f_fsid.val[1], l_sb->exsb.volume_uuid +
sizeof(int), sizeof(int));
buf->f_namelen = VDFS4_FILE_NAME_LEN;
/* the vdfs4 has no limitation
* but in case if end of space it could be wrong*/
buf->f_ffree = 0xfefefe;
return 0;
}
/**
* @brief Evict inode.
* @param [in,out] inode Pointer to inode that will be evicted
* @return void
*/
static void vdfs4_evict_inode(struct inode *inode)
{
struct vdfs4_sb_info *sbi = VDFS4_SB(inode->i_sb);
int error = 0;
sector_t freed_runtime_iblocks;
VT_PREPARE_PARAM(vt_data);
VT_SBOPS_START(vt_data, vdfs_trace_sb_evict_inode, inode->i_sb,
inode, 0, 0);
trace_vdfs4_evict_inode_enter(inode);
VDFS4_DEBUG_INO("evict inode %lu nlink\t%u",
inode->i_ino, inode->i_nlink);
truncate_inode_pages(&inode->i_data, 0);
invalidate_inode_buffers(inode);
/* Inode isn't present in catalog tree */
if (!inode->i_ino)
goto no_delete;
if (VDFS4_I(inode)->record_type == VDFS4_CATALOG_UNPACK_INODE) {
inode->i_state = I_FREEING | I_CLEAR;
trace_vdfs4_evict_inode_exit(inode, 1);
VT_FINISH(vt_data);
return;
}
if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode))) {
freed_runtime_iblocks = vdfs4_truncate_runtime_blocks(0,
&VDFS4_I(inode)->runtime_extents);
vdfs4_free_reserved_space(inode, freed_runtime_iblocks);
}
if (inode->i_nlink)
goto no_delete;
vdfs4_start_transaction(sbi);
error = vdfs4_xattrtree_remove_all(sbi->xattr_tree, inode->i_ino);
if (error) {
vdfs4_fatal_error(sbi, VDFS4_DEBUG_ERR_INODE_EVICT,
inode->i_ino, "cannot clear xattrs for ino#%lu: %d",
inode->i_ino, error);
goto out_trans;
}
if (S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
error = vdfs4_truncate_blocks(inode, 0);
if (error) {
vdfs4_fatal_error(sbi, VDFS4_DEBUG_ERR_INODE_EVICT,
inode->i_ino,
"cannot truncate ino#%lu blocks: %d",
inode->i_ino, error);
goto out_trans;
}
inode->i_size = 0;
}
vdfs4_cattree_w_lock(sbi);
if (is_vdfs4_inode_flag_set(inode, HARD_LINK))
error = vdfs4_cattree_remove(sbi->catalog_tree, inode->i_ino,
inode->i_ino, NULL, 0,
VDFS4_I(inode)->record_type);
else
error = vdfs4_cattree_remove(sbi->catalog_tree, inode->i_ino,
VDFS4_I(inode)->parent_id,
VDFS4_I(inode)->name,
strlen(VDFS4_I(inode)->name),
VDFS4_I(inode)->record_type);
if (error) {
vdfs4_fatal_error(sbi, VDFS4_DEBUG_ERR_INODE_EVICT,
inode->i_ino,
"cannot remove inode ino#%lu: %d",
inode->i_ino, error);
goto out_unlock;
}
vdfs4_del_from_orphan(sbi, inode);
error = 0;
out_unlock:
vdfs4_cattree_w_unlock(sbi);
if (!error) {
vdfs4_free_inode_n(sbi, inode->i_ino, 1);
if (S_ISDIR(inode->i_mode))
sbi->folders_count--;
else
sbi->files_count--;
}
out_trans:
vdfs4_stop_transaction(sbi);
no_delete:
clear_inode(inode);
trace_vdfs4_evict_inode_exit(inode, 0);
VT_FINISH(vt_data);
}
/*
* Structure of the eMMCFS super block operations
*/
static struct super_operations vdfs4_sops = {
.alloc_inode = vdfs4_alloc_inode,
.destroy_inode = vdfs4_destroy_inode,
.write_inode = vdfs4_write_inode,
.put_super = vdfs4_put_super,
.sync_fs = vdfs4_sync_fs,
.freeze_fs = vdfs4_freeze,
.statfs = vdfs4_statfs,
.umount_begin = vdfs4_umount_begin,
.remount_fs = vdfs4_remount_fs,
.show_options = vdfs4_show_options,
.evict_inode = vdfs4_evict_inode,
};
/**
* @brief Sanity check of eMMCFS super block.
* @param [in] esb The eMMCFS super block
* @param [in] str_sb_type Determines which SB is verified on error
* printing
* @param [in] silent Doesn't print errors if silent is true
* @return Returns 0 on success, errno on failure
*/
static int vdfs4_verify_sb(struct vdfs4_super_block *esb, char *str_sb_type,
int silent, int check_signature,
struct vdfs4_sb_info *sbi)
{
__le32 checksum;
int i, supported = 0;
/* check magic number */
if (memcmp(esb->signature, VDFS4_SB_SIGNATURE,
strlen(VDFS4_SB_SIGNATURE))) {
if (!silent || vdfs4_debug_mask & VDFS4_DBG_SB)
VDFS4_WARNING("%s: bad signature - %.8s, expected - %.8s",
str_sb_type, esb->signature, VDFS4_SB_SIGNATURE);
return -EINVAL;
}
for (i = 0; i < VDFS4_NUM_OF_SUPPORTED_LAYOUTS; i++) {
const char *layout_version = supported_layouts[i];
if (!memcmp(esb->layout_version, layout_version,
strlen(layout_version))) {
supported = 1;
break;
}
}
if (!supported) {
VDFS4_ERR("Invalid mkfs layout version: %.4s,\n"
"driver uses %.4s version\n", esb->layout_version,
VDFS4_LAYOUT_VERSION);
return -EINVAL;
}
/* check crc32 */
checksum = crc32(0, esb, sizeof(*esb) - sizeof(esb->checksum));
if (esb->checksum != checksum) {
VDFS4_ERR("%s: bad checksum - 0x%x, must be 0x%x\n",
str_sb_type, esb->checksum, checksum);
return -EINVAL;
}
#ifdef CONFIG_VDFS4_AUTHENTICATION
/* check the superblock rsa signature */
if (check_signature) {
enum sign_type type = get_sign_type_from_sb(esb);
int sign_length = get_sign_length(type);
rsakey_t *rsa_key = get_rsa_key_by_type(sbi, type);
__u8 *hash_ptr = esb->sb_hash;
if (sign_length <= 0) {
VDFS4_ERR("Cannot check signature. Bad superblock"
" or unknown signature type %d", type);
#ifndef CONFIG_VDFS4_DEBUG_AUTHENTICAION
return -EINVAL;
#endif
}
if (!rsa_key) {
VDFS4_ERR("Cannot check signature."
" No matching public key found");
#ifndef CONFIG_VDFS4_DEBUG_AUTHENTICAION
return -EINVAL;
#endif
}
#ifdef CONFIG_VDFS4_ALLOW_LEGACY_SIGN
if (type == VDFS4_SIGN_RSA1024)
hash_ptr += (VDFS4_MAX_CRYPTED_HASH_LEN - sign_length);
#endif
if (vdfs4_verify_superblock_rsa_signature(esb->hash_type, type,
(unsigned char *)esb, sizeof(*esb) - sizeof(esb->checksum)
- sign_length, hash_ptr, rsa_key)) {
VDFS4_ERR("bad superblock hash!!!");
#ifndef CONFIG_VDFS4_DEBUG_AUTHENTICAION
return -EINVAL;
#endif
}
}
#endif
return 0;
}
/**
* @brief Fill run-time superblock from on-disk superblock.
* @param [in] esb The eMMCFS super block
* @param [in] sb VFS superblock
* @return Returns 0 on success, errno on failure
*/
static int fill_runtime_superblock(struct vdfs4_super_block *esb,
struct super_block *sb)
{
int ret = 0;
unsigned long blck_size;
struct vdfs4_sb_info *sbi = sb->s_fs_info;
/* check total block count in SB */
VDFS4_NOTICE("volume mkfs version \"%s\"\n", esb->mkfs_version);
/* check if block size is supported and set it */
blck_size = (unsigned long)(1 << esb->log_block_size);
if (blck_size & (unsigned long)(~(512 | 1024 | 2048 | 4096))) {
VDFS4_ERR("unsupported block size (%lu)\n", blck_size);
ret = -EINVAL;
goto err_exit;
}
if (blck_size == 512) {
sbi->log_blocks_in_page = 3;
sbi->log_block_size = 9;
} else if (blck_size == 1024) {
sbi->log_blocks_in_page = 2;
sbi->log_block_size = 10;
} else if (blck_size == 2048) {
sbi->log_blocks_in_page = 1;
sbi->log_block_size = 11;
} else {
sbi->log_blocks_in_page = 0;
sbi->log_block_size = 12;
}
sbi->block_size = blck_size;
if (!sb_set_blocksize(sb, (int)sbi->block_size))
VDFS4_ERR("can't set block size\n");
sbi->block_size_shift = esb->log_block_size;
sbi->log_sectors_per_block = sbi->block_size_shift - SECTOR_SIZE_SHIFT;
sbi->offset_msk_inblock = 0xFFFFFFFF >> (32 - sbi->block_size_shift);
/* Check if superpage oredr is supported and set it */
if (esb->log_super_page_size < sbi->log_block_size) {
VDFS4_ERR("unsupported superpage order (%u)\n",
sbi->log_super_page_size);
ret = -EINVAL;
goto err_exit;
}
sbi->log_super_page_size = esb->log_super_page_size;
sbi->log_blocks_in_leb = (unsigned int)(
esb->log_super_page_size - esb->log_block_size);
sbi->btree_node_size_blks =
(unsigned long)(1lu << sbi->log_blocks_in_leb);
if (esb->case_insensitive)
set_option(sbi, CASE_INSENSITIVE);
err_exit:
return ret;
}
static void add_reformat_record(struct vdfs4_sb_info *sbi, void *buffer)
{
struct vdfs4_superblock *sb = (struct vdfs4_superblock *)buffer;
struct vdfs4_volume_begins *reformat_block =
(struct vdfs4_volume_begins *) &sb->sign2;
struct vdfs4_reformat_history *item =
(struct vdfs4_reformat_history *) reformat_block->command_line;
struct vdfs4_reformat_history *oldest_item = item;
int last_number = 0;
memcpy(sb->sing1.signature, VDFS4_SB_SIGNATURE_REFORMATTED,
sizeof(VDFS4_SB_SIGNATURE_REFORMATTED) - 1);
memcpy(sb->sign2.signature, VDFS4_SB_SIGNATURE_REFORMATTED,
sizeof(VDFS4_SB_SIGNATURE_REFORMATTED) - 1);
while ((void *)item < (void *)&reformat_block->checksum) {
if (memcmp(item->magic, VDFS4_REFORMAT_HISTORY_ITEM_MAGIC,
sizeof(item->magic))) {
oldest_item = item;
break;
}
if (le32_to_cpu(item->reformat_number) > last_number)
last_number = le32_to_cpu(item->reformat_number);
++item;
if (le32_to_cpu(item->reformat_number) <
le32_to_cpu(oldest_item->reformat_number))
oldest_item = item;
}
memcpy(oldest_item->magic, VDFS4_REFORMAT_HISTORY_ITEM_MAGIC,
sizeof(oldest_item->magic));
oldest_item->reformat_number = cpu_to_le32(last_number + 1);
oldest_item->mount_count = sb->ext_superblock.mount_counter;
oldest_item->sync_count = sb->ext_superblock.sync_counter;
memset(oldest_item->driver_version, 0,
sizeof(oldest_item->driver_version));
memset(oldest_item->mkfs_version, 0,
sizeof(oldest_item->driver_version));
memcpy(oldest_item->mkfs_version, sb->superblock.mkfs_version,
sizeof(oldest_item->mkfs_version));
}
void destroy_layout(struct vdfs4_sb_info *sbi)
{
if (test_option(sbi, DESTROY_LAYOUT)) {
int ret = 0;
VDFS4_DEBUG_TMP("Destroying layout");
lock_page(sbi->superblocks);
add_reformat_record(sbi, sbi->raw_superblock);
set_page_writeback(sbi->superblocks);
ret = vdfs4_write_page(sbi, VDFS4_1ST_BASE, sbi->superblocks,
0, SECTOR_TO_BYTE(2), 1);
if (ret) {
VDFS4_ERR("Failed to read first page from disk");
goto err_exit;
}
set_page_writeback(sbi->superblocks);
ret = vdfs4_write_page(sbi, VDFS4_2ND_BASE, sbi->superblocks,
0, SECTOR_TO_BYTE(2), 1);
if (ret)
VDFS4_ERR("Failed to read first page from disk");
err_exit:
unlock_page(sbi->superblocks);
}
}
static void vdfs4_print_reformat_history(struct vdfs4_sb_info *sbi)
{
struct vdfs4_superblock *sb;
struct vdfs4_volume_begins *reformat_block;
struct vdfs4_reformat_history *item;
lock_page(sbi->superblocks);
sb = (struct vdfs4_superblock *)sbi->raw_superblock;
reformat_block = (struct vdfs4_volume_begins *) &sb->sign2;
item = (struct vdfs4_reformat_history *) reformat_block->command_line;
if (!memcmp(item->magic, VDFS4_REFORMAT_HISTORY_ITEM_MAGIC,
sizeof(item->magic)))
VDFS4_DEBUG_TMP("There is a reformat history at volume");
while ((void *)item < (void *)&reformat_block->checksum &&
!memcmp(item->magic, VDFS4_REFORMAT_HISTORY_ITEM_MAGIC,
sizeof(item->magic))) {
VDFS4_DEBUG_TMP("Reformat #%d: mount #%d, sync#%d, "
"driver version: %.4s, mkfs_version: %.4s\n",
le32_to_cpu(item->reformat_number),
le32_to_cpu(item->mount_count),
le32_to_cpu(item->sync_count),
item->driver_version, item->mkfs_version);
++item;
}
unlock_page(sbi->superblocks);
}
void vdfs4_fatal_error(struct vdfs4_sb_info *sbi, unsigned int err_type,
unsigned long i_ino, const char *fmt, ...)
{
const char *device = sbi->sb->s_id;
struct va_format vaf;
va_list args;
va_start(args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
VDFS4_ERR("VDFS4(%s): error in %pf, %pV\n", device,
__builtin_return_address(0), &vaf);
va_end(args);
if (err_type)
vdfs4_record_err_dump_disk(sbi, err_type, i_ino, 0,
fmt, NULL, 0);
#ifdef CONFIG_VDFS4_PANIC_ON_ERROR
panic("VDFS4(%s): forced kernel panic after fatal error\n", device);
#else
if (!(sbi->sb->s_flags & MS_RDONLY)) {
VDFS4_WARNING("VDFS4(%s): remount to read-only mode\n", device);
sbi->sb->s_flags |= MS_RDONLY;
}
#endif
}
/**
* @brief VDFS4 volume sainty check : it is vdfs4 or not
* @param [in] sb VFS superblock info stucture
* @return Returns 0 on success, errno on failure
*
*/
static int vdfs4_volume_check(struct super_block *sb)
{
int ret = 0;
struct page *superblocks;
void *raw_superblocks;
struct vdfs4_super_block *esb;
struct vdfs4_sb_info *sbi = sb->s_fs_info;
superblocks = alloc_page(GFP_NOFS | __GFP_ZERO);
if (!superblocks)
return -ENOMEM;
lock_page(superblocks);
/* size of the VDFS4 superblock is fixed = 4K or 8 sectors */
ret = vdfs4_read_page(sb->s_bdev, superblocks,
VDFS4_1ST_BASE, PAGE_TO_SECTORS(1), 0);
unlock_page(superblocks);
if (ret)
goto error_exit;
raw_superblocks = kmap(superblocks);
sbi->raw_superblock = raw_superblocks;
/* check superblocks */
esb = (struct vdfs4_super_block *)raw_superblocks;
ret = vdfs4_verify_sb(esb, "SB", 0, 0, sbi);
if (ret)
goto error_exit;
sbi->superblocks = superblocks;
return 0;
error_exit:
sbi->superblocks = NULL;
sbi->raw_superblock = NULL;