SSDFS: noGC ZNS/FDP-friendly LFS file system
v2 cover SSDFS: noGC ZNS/FDP-friendly LFS file system
Hello,
Complete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
[PROBLEM DECLARATION]
SSD is a sophisticated device capable of managing in-place updates. However, in-place updates generate significant FTL GC responsibilities that increase write amplification factor, require substantial NAND flash overprovisioning, decrease SSD lifetime, and introduce performance spikes. Log-structured File System (LFS) approach can introduce a more flash-friendly Copy-On-Write (COW) model. However, F2FS and NILFS2 issue in-place updates anyway, even by using the COW policy for main volume area. Also, GC is an inevitable subsystem of any LFS file system that introduces write amplification, retention issue, excessive copy operations, and performance degradation for aged volume. Generally speaking, available file system technologies have side effects: (1) write amplification issue, (2) significant FTL GC responsibilities, (3) inevitable FS GC overhead, (4) read disturbance,
(5) retention issue. As a result, SSD lifetime reduction, perfomance degradation, early SSD failure, and increased TCO cost are reality of data infrastructure.
[WHY YET ANOTHER FS?]
QLC NAND flash makes really tough requirements for file systems to be really flash friendly. ZNS SSD and FDP SSD technologies try to help to manage these strict requirements. But, anyway, file systems need to play properly to employ ZNS/FDP SSDs benefits. Ideally, file system needs to use only Copy-On-Write (COW) or append-only policy, to be a Log-structured (LFS) file system, and to be capable to work without GC subsystem. However, F2FS and NILFS2 file systems heavily rely on GC subsystem that inevitably increase write amplification. These file systems use the COW policy not for the whole volume.
Generally speaking, it will be good to see LFS file system architecture that is capable:
(1) eliminate FS GC overhead,
(2) decrease/eliminate FTL GC responsibilities,
(3) decrease write amplification factor,
(4) introduce native architectural support of ZNS SSD + SMR HDD,
(5) increase compression ratio by using delta-encoding and deduplication,
(6) introduce smart management of "cold" data and efficient TRIM policy,
(7) employ parallelism of multiple NAND dies/channels,
(8) prolong SSD lifetime and decrease TCO cost,
(9) guarantee strong reliability and capability to reconstruct heavily corrupted file system volume,
(10) guarantee stable performance.
SSDFS is an open-source, kernel-space LFS file system designed:
(1) eliminate GC overhead, (2) prolong SSD lifetime, (3) natively support a strict append-only mode (ZNS SSD + SMR HDD compatible), (4) guarantee strong reliability, (5) guarantee stable performance.
[SSDFS ARCHITECTURE]
One of the key goals of SSDFS is to decrease the write amplification factor. Logical extent concept is the fundamental technique to achieve the goal. Logical extent describes any volume extent on the basis of {segment ID, logical block ID, and length}. Segment is a portion of file system volume that has to be aligned on erase block size and always located at the same offset. It is basic unit to allocate and to manage free space of file system volume. Every segment can include one or several Logical Erase Blocks (LEB). LEB can be mapped into "Physical" Erase Block (PEB). Generally speaking, PEB is fixed-sized container that includes a number of logical blocks (physical sectors or NAND flash pages). SSDFS is pure Log-structured File System (LFS). It means that any write operation into erase block is the creation of log. Content of every erase block is a sequence of logs. PEB has block bitmap with the goal of tracking the state (free, pre-allocated, allocated, invalid) of logical blocks and to account the physical space is used for storing log's metadata (segment header, partial log header, footer). Also, log contains an offset translation table that converts logical block ID into particular offset inside of log's payload. Log concept implements a support of compression, delta-encoding, and compaction scheme. As a result, it provides the way: (1) decrease write amplification, (2) decrease FTL GC responsibilities, (3) improve compression ration and decrease payload size. Finally, SSD lifetime can be longer and write I/O performance can be improved.
SSDFS file system is based on the concept of logical segment that is the aggregation of Logical Erase Blocks (LEB). Moreover, initially, LEB hasn’t association with a particular "Physical" Erase Block (PEB). It means that segment could have the association not for all LEBs or, even, to have no association at all with any PEB (for example, in the case of clean segment). Generally speaking, SSDFS file system needs a special metadata structure (PEB mapping table) that is capable of associating any LEB with any PEB. The PEB mapping table is the crucial metadata structure that has several goals: (1) mapping LEB to PEB,
(2) implementation of the logical extent concept, (3) implementation of the concept of PEB migration, (4) implementation of the delayed erase operation by specialized thread.
SSDFS implements a migration scheme. Migration scheme is a fundamental technique of GC overhead management. The key responsibility of the migration scheme is to guarantee the presence of data in the same segment for any update operations. Generally speaking, the migration scheme’s model is implemented on the basis of association an exhausted "Physical" Erase Block (PEB) with a clean one. The goal of such association of two PEBs is to implement the gradual migration of data by means of the update operations in the initial (exhausted) PEB. As a result, the old, exhausted PEB becomes invalidated after complete data migration and it will be possible to apply the erase operation to convert it to a clean state. The migration scheme is capable of decreasing GC activity significantly by means of excluding the necessity to update metadata and by means of self-migration of data between PEBs is triggered by regular update operations. Finally, migration scheme can: (1) eliminate GC overhead,
(2) implement efficient TRIM policy, (3) prolong SDD lifetime,
(4) guarantee stable performance.
Generally speaking, SSDFS doesn't need a classical model of garbage collection that is used in NILFS2 or F2FS. However, SSDFS has several global GC threads (dirty, pre-dirty, used, using segment states) and segment bitmap. The main responsibility of global GC threads is:
(1) find segment in a particular state, (2) check that segment object is constructed and initialized by file system driver logic,
(3) check the necessity to stimulate or finish the migration (if segment is under update operations or has update operations recently, then migration stimulation is not necessary),
(4) define valid blocks that require migration, (5) add recommended migration request to PEB update queue, (6) destroy in-core segment object if no migration is necessary and no create/update requests have been received by segment object recently. Global GC threads are used to recommend migration stimulation for particular PEBs and to destroy in-core segment objects that have no requests for processing. Segment bitmap is the critical metadata structure of SSDFS file system that implements several goals: (1) searching for a candidate for a current segment capable of storing new data,
(2) searching by GC subsystem for the most optimal segment (dirty state, for example) with the goal of preparing the segment in background for storing new data (converting in a clean state).
SSDFS file system uses b-tree architecture for metadata representation (for example, inodes tree, extents tree, dentries tree, xattr tree) because it provides the compact way of reserving the metadata space without the necessity to use the excessive overprovisioning of metadata reservation (for example, in the case of plain table or array). SSDFS file system uses a hybrid b-tree architecture with the goal to eliminate the index nodes’ side effect. The hybrid b-tree operates by three node types: (1) index node, (2) hybrid node, (3) leaf node. Generally speaking, the peculiarity of hybrid node is the mixture as index as data records into one node. Hybrid b-tree starts with root node that is capable to keep the two index records or two data records inline (if size of data record is equal or lesser than size of index record). If the b-tree needs to contain more than two items then it should be added the first hybrid node into the b-tree. The root level of b-tree is able to contain only two nodes because the root node is capable to store only two index records. Generally speaking, the initial goal of hybrid node is to store the data records in the presence of reserved index area. B-tree implements compact and flexible metadata structure that can decrease payload size and isolate hot, warm, and cold metadata types in different erase blocks.
Migration scheme is completely enough for the case of conventional SSDs as for metadata as for user data. But ZNS SSD has huge zone size and limited number of active/open zones. As a result, it requires introducing a moving scheme for user data in the case of ZNS SSD. Finally, migration scheme works for metadata and moving scheme works for user data (ZNS SSD case). Initially, user data can be stored into current user data segment/zone. And user data can be updated at the same zone until exhaustion. Next, moving scheme starts to work. Updated user data is moved into current user data zone for updates. As a result, it needs to update the extents tree and to store invalidated extents of old zone into invalidated extents tree. Invalidated extents tree needs to track the moment when the old zone is completely invalidated and is ready to be erased.
[BENCHMARKING]
Benchmarking results show that SSDFS is capable:
(1) generate smaller amount of write I/O requests compared with:
1.4x - 116x (ext4),
14x - 42x (xfs),
6.2x - 9.8x (btrfs),
1.5x - 41x (f2fs),
0.6x - 22x (nilfs2);
(2) create smaller payload compared with:
0.3x - 300x (ext4),
0.3x - 190x (xfs),
0.7x - 400x (btrfs),
1.2x - 400x (f2fs),
0.9x - 190x (nilfs2);
(3) decrease the write amplification factor compared with:
1.3x - 116x (ext4),
14x - 42x (xfs),
6x - 9x (btrfs),
1.5x - 50x (f2fs),
1.2x - 20x (nilfs2);
(4) prolong SSD lifetime compared with:
1.4x - 7.8x (ext4),
15x - 60x (xfs),
6x - 12x (btrfs),
1.5x - 7x (f2fs),
1x - 4.6x (nilfs2).v2
(*) File system code has been completely switched on memory folios. (*) PEB-based deduplication model has been introduced.
(*) 8K, 16K, 32K logical block size support has been stabilized. (*) PEB inflation model has been implemented.
(*) Shared dictionary and b-tree subsystem has been reworked significantly.
[CURRENT ISSUES]
(*) FSCK tool is not fully implemented.
(*) Multiple issues during xfstests run.
(*) ZNS SSD + SMR HDD support is not stable.
(*) Multiple erase blocks in segment model functionality is not stable. (*) Collaboration of PEB inflation model, migration scheme, and
moving scheme has issues.
[TODO]
(*) Multi-drive support.
[REFERENCES]
[1] SSDFS tools: https://github.com/dubeyko/ssdfs-tools.git
[2] SSDFS driver: https://github.com/dubeyko/ssdfs-driver.git
[3] Linux kernel with SSDFS support: https://github.com/dubeyko/linux.git
[4] SSDFS (paper): https://arxiv.org/abs/1907.11825
[5] Embedded Linux 2022: https://www.youtube.com/watch?v=x5gklnkvi_Q
[6] Linux Plumbers 2022: https://www.youtube.com/watch?v=sBGddJBHsIo
[7] Why do you need SSDFS?: https://www.youtube.com/watch?v=7b_vrtRvsGM
[8] Linux Plumbers 2024: https://www.youtube.com/watch?v=0_f1kD7fGnE
Viacheslav Dubeyko (79):
ssdfs: introduce SSDFS on-disk layout
ssdfs: add key file system declarations
ssdfs: add key file system's function declarations
ssdfs: implement raw device operations
ssdfs: implement basic read/write primitives
ssdfs: implement super operations
ssdfs: implement commit superblock logic
ssdfs: segment header + log footer operations
ssdfs: add declaration of functions for superblock search
ssdfs: basic mount logic implementation
ssdfs: implement folio vector
ssdfs: implement dynamic array
ssdfs: implement sequence array
ssdfs: implement folio array
ssdfs: introduce PEB's block bitmap
ssdfs: implement PEB's block bitmap functionality
ssdfs: implement support of migration scheme in PEB bitmap
ssdfs: implement functionality of migration scheme in PEB bitmap
ssdfs: introduce segment block bitmap
ssdfs: implement functionality of segment block bitmap
ssdfs: introduce segment request queue
ssdfs: introduce offset translation table
ssdfs: implement offsets translation table functionality
ssdfs: introduce PEB object
ssdfs: implement PEB object functionality
ssdfs: implement compression logic support
ssdfs: introduce PEB container
ssdfs: implement PEB container functionality
ssdfs: implement migration scheme
ssdfs: PEB read thread logic
ssdfs: PEB flush thread's finite state machine
ssdfs: auxilairy GC threads logic
ssdfs: introduce segment object
ssdfs: implement segment object's functionality
ssdfs: implement current segment functionality
ssdfs: implement segment tree functionality
ssdfs: introduce PEB mapping queue
ssdfs: introduce PEB mapping table
ssdfs: implement PEB mapping table functionality
ssdfs: introduce PEB mapping table cache
ssdfs: implement PEB mapping table cache logic
ssdfs: introduce segment bitmap
ssdfs: implement segment bitmap's functionality
ssdfs: introduce b-tree object
ssdfs: implement b-tree object's functionality
ssdfs: introduce b-tree node object
ssdfs: implement b-tree node's functionality
ssdfs: introduce b-tree hierarchy object
ssdfs: implement b-tree hierarchy logic
ssdfs: introduce inodes b-tree
ssdfs: implement inodes b-tree functionality
ssdfs: introduce dentries b-tree
ssdfs: implement dentries b-tree functionality
ssdfs: introduce extents queue object
ssdfs: introduce extents b-tree
ssdfs: implement extents b-tree functionality
ssdfs: introduce invalidated extents b-tree
ssdfs: implement invalidated extents b-tree functionality
ssdfs: introduce shared extents b-tree
ssdfs: implement shared extents b-tree functionality
ssdfs: introduce PEB-based deduplication technique
ssdfs: introduce shared dictionary b-tree
ssdfs: implement shared dictionary b-tree functionality
ssdfs: implement snapshot requests queue functionality
ssdfs: introduce snapshots b-tree
ssdfs: implement snapshots b-tree functionality
ssdfs: implement extended attributes support
ssdfs: implement extended attributes b-tree functionality
ssdfs: introduce Diff-On-Write approach
ssdfs: implement sysfs support
ssdfs: implement IOCTL operations
ssdfs: introduce online FSCK stub logic
ssdfs: introduce application-based unit-tests
ssdfs: introduce Kunit-based unit-tests
ssdfs: implement inode operations support
ssdfs: implement directory operations support
ssdfs: implement file operations support
ssdfs: implement initial support of tunefs operations Introduce SSDFS file system
fs/Kconfig | 1 + fs/Makefile | 1 + fs/ssdfs/.kunitconfig | 9 + fs/ssdfs/Kconfig | 408 + fs/ssdfs/Makefile | 63 + fs/ssdfs/acl.c | 260 + fs/ssdfs/acl.h | 54 + fs/ssdfs/block_bitmap.c | 6948 +++++++ fs/ssdfs/block_bitmap.h | 393 + fs/ssdfs/block_bitmap_tables.c | 311 + fs/ssdfs/block_bitmap_test.c | 2380 +++ fs/ssdfs/btree.c | 8506 +++++++++ fs/ssdfs/btree.h | 219 + fs/ssdfs/btree_hierarchy.c | 11632 ++++++++++++ fs/ssdfs/btree_hierarchy.h | 336 + fs/ssdfs/btree_node.c | 18780 ++++++++++++++++++ fs/ssdfs/btree_node.h | 891 + fs/ssdfs/btree_search.c | 1114 ++ fs/ssdfs/btree_search.h | 424 + fs/ssdfs/common_bitmap.h | 230 + fs/ssdfs/compr_lzo.c | 268 + fs/ssdfs/compr_lzo_test.c | 570 + fs/ssdfs/compr_zlib.c | 374 + fs/ssdfs/compr_zlib_test.c | 401 + fs/ssdfs/compression.c | 569 + fs/ssdfs/compression.h | 108 + fs/ssdfs/compression_test.c | 310 + fs/ssdfs/current_segment.c | 949 + fs/ssdfs/current_segment.h | 116 + fs/ssdfs/dentries_tree.c | 10485 ++++++++++ fs/ssdfs/dentries_tree.h | 158 + fs/ssdfs/dev_bdev.c | 1065 ++ fs/ssdfs/dev_mtd.c | 650 + fs/ssdfs/dev_zns.c | 1344 ++ fs/ssdfs/diff_on_write.c | 158 + fs/ssdfs/diff_on_write.h | 106 + fs/ssdfs/diff_on_write_metadata.c | 2969 +++ fs/ssdfs/diff_on_write_user_data.c | 851 + fs/ssdfs/dir.c | 2197 +++ fs/ssdfs/dynamic_array.c | 1594 ++ fs/ssdfs/dynamic_array.h | 103 + fs/ssdfs/dynamic_array_test.c | 660 + fs/ssdfs/extents_queue.c | 2013 ++ fs/ssdfs/extents_queue.h | 110 + fs/ssdfs/extents_tree.c | 15349 +++++++++++++++ fs/ssdfs/extents_tree.h | 188 + fs/ssdfs/file.c | 4341 +++++ fs/ssdfs/fingerprint.h | 261 + fs/ssdfs/fingerprint_array.c | 795 + fs/ssdfs/fingerprint_array.h | 82 + fs/ssdfs/folio_array.c | 1781 ++ fs/ssdfs/folio_array.h | 146 + fs/ssdfs/folio_array_test.c | 1107 ++ fs/ssdfs/folio_vector.c | 523 + fs/ssdfs/folio_vector.h | 70 + fs/ssdfs/folio_vector_test.c | 495 + fs/ssdfs/fs_error.c | 265 + fs/ssdfs/global_fsck.c | 598 + fs/ssdfs/inode.c | 1262 ++ fs/ssdfs/inodes_tree.c | 6261 ++++++ fs/ssdfs/inodes_tree.h | 181 + fs/ssdfs/invalidated_extents_tree.c | 7128 +++++++ fs/ssdfs/invalidated_extents_tree.h | 96 + fs/ssdfs/ioctl.c | 453 + fs/ssdfs/ioctl.h | 58 + fs/ssdfs/log_footer.c | 991 + fs/ssdfs/offset_translation_table.c | 12175 ++++++++++++ fs/ssdfs/offset_translation_table.h | 459 + fs/ssdfs/options.c | 170 + fs/ssdfs/peb.c | 1120 ++ fs/ssdfs/peb.h | 600 + fs/ssdfs/peb_block_bitmap.c | 5740 ++++++ fs/ssdfs/peb_block_bitmap.h | 179 + fs/ssdfs/peb_container.c | 6605 +++++++ fs/ssdfs/peb_container.h | 631 + fs/ssdfs/peb_deduplication.c | 483 + fs/ssdfs/peb_flush_thread.c | 24221 ++++++++++++++++++++++++ fs/ssdfs/peb_fsck_thread.c | 242 + fs/ssdfs/peb_gc_thread.c | 3734 ++++ fs/ssdfs/peb_init.c | 1338 ++ fs/ssdfs/peb_init.h | 364 + fs/ssdfs/peb_mapping_queue.c | 340 + fs/ssdfs/peb_mapping_queue.h | 68 + fs/ssdfs/peb_mapping_table.c | 13868 ++++++++++++++ fs/ssdfs/peb_mapping_table.h | 784 + fs/ssdfs/peb_mapping_table_cache.c | 4897 +++++ fs/ssdfs/peb_mapping_table_cache.h | 120 + fs/ssdfs/peb_mapping_table_thread.c | 2959 +++ fs/ssdfs/peb_migration_scheme.c | 1445 ++ fs/ssdfs/peb_read_thread.c | 14978 +++++++++++++++ fs/ssdfs/readwrite.c | 973 + fs/ssdfs/recovery.c | 3706 ++++ fs/ssdfs/recovery.h | 451 + fs/ssdfs/recovery_fast_search.c | 1200 ++ fs/ssdfs/recovery_slow_search.c | 587 + fs/ssdfs/recovery_thread.c | 1215 ++ fs/ssdfs/request_queue.c | 1726 ++ fs/ssdfs/request_queue.h | 818 + fs/ssdfs/segment.c | 8525 +++++++++ fs/ssdfs/segment.h | 1367 ++ fs/ssdfs/segment_bitmap.c | 5157 +++++ fs/ssdfs/segment_bitmap.h | 482 + fs/ssdfs/segment_bitmap_tables.c | 887 + fs/ssdfs/segment_block_bitmap.c | 1929 ++ fs/ssdfs/segment_block_bitmap.h | 240 + fs/ssdfs/segment_tree.c | 996 + fs/ssdfs/segment_tree.h | 107 + fs/ssdfs/sequence_array.c | 1160 ++ fs/ssdfs/sequence_array.h | 140 + fs/ssdfs/shared_dictionary.c | 21342 +++++++++++++++++++++ fs/ssdfs/shared_dictionary.h | 204 + fs/ssdfs/shared_dictionary_thread.c | 457 + fs/ssdfs/shared_extents_tree.c | 6866 +++++++ fs/ssdfs/shared_extents_tree.h | 146 + fs/ssdfs/shared_extents_tree_thread.c | 808 + fs/ssdfs/snapshot.c | 99 + fs/ssdfs/snapshot.h | 283 + fs/ssdfs/snapshot_requests_queue.c | 1249 ++ fs/ssdfs/snapshot_requests_queue.h | 65 + fs/ssdfs/snapshot_rules.c | 739 + fs/ssdfs/snapshot_rules.h | 55 + fs/ssdfs/snapshots_tree.c | 8917 +++++++++ fs/ssdfs/snapshots_tree.h | 248 + fs/ssdfs/snapshots_tree_thread.c | 665 + fs/ssdfs/ssdfs.h | 502 + fs/ssdfs/ssdfs_constants.h | 214 + fs/ssdfs/ssdfs_fs_info.h | 821 + fs/ssdfs/ssdfs_inline.h | 3037 +++ fs/ssdfs/ssdfs_inode_info.h | 144 + fs/ssdfs/ssdfs_thread_info.h | 43 + fs/ssdfs/super.c | 4873 +++++ fs/ssdfs/sysfs.c | 6558 +++++++ fs/ssdfs/sysfs.h | 305 + fs/ssdfs/testing.c | 5949 ++++++ fs/ssdfs/testing.h | 226 + fs/ssdfs/tunefs.c | 487 + fs/ssdfs/version.h | 9 + fs/ssdfs/volume_header.c | 1431 ++ fs/ssdfs/xattr.c | 1700 ++ fs/ssdfs/xattr.h | 88 + fs/ssdfs/xattr_security.c | 159 + fs/ssdfs/xattr_tree.c | 10132 ++++++++++ fs/ssdfs/xattr_tree.h | 143 + fs/ssdfs/xattr_trusted.c | 93 + fs/ssdfs/xattr_user.c | 93 + include/linux/ssdfs_fs.h | 3565 ++++ include/trace/events/ssdfs.h | 256 + include/uapi/linux/magic.h | 1 + include/uapi/linux/ssdfs_fs.h | 126 + 149 files changed, 335803 insertions(+) create mode 100644 fs/ssdfs/.kunitconfig create mode 100644 fs/ssdfs/Kconfig create mode 100644 fs/ssdfs/Makefile create mode 100644 fs/ssdfs/acl.c create mode 100644 fs/ssdfs/acl.h create mode 100644 fs/ssdfs/block_bitmap.c create mode 100644 fs/ssdfs/block_bitmap.h create mode 100644 fs/ssdfs/block_bitmap_tables.c create mode 100644 fs/ssdfs/block_bitmap_test.c create mode 100644 fs/ssdfs/btree.c create mode 100644 fs/ssdfs/btree.h create mode 100644 fs/ssdfs/btree_hierarchy.c create mode 100644 fs/ssdfs/btree_hierarchy.h create mode 100644 fs/ssdfs/btree_node.c create mode 100644 fs/ssdfs/btree_node.h create mode 100644 fs/ssdfs/btree_search.c create mode 100644 fs/ssdfs/btree_search.h create mode 100644 fs/ssdfs/common_bitmap.h create mode 100644 fs/ssdfs/compr_lzo.c create mode 100644 fs/ssdfs/compr_lzo_test.c create mode 100644 fs/ssdfs/compr_zlib.c create mode 100644 fs/ssdfs/compr_zlib_test.c create mode 100644 fs/ssdfs/compression.c create mode 100644 fs/ssdfs/compression.h create mode 100644 fs/ssdfs/compression_test.c create mode 100644 fs/ssdfs/current_segment.c create mode 100644 fs/ssdfs/current_segment.h create mode 100644 fs/ssdfs/dentries_tree.c create mode 100644 fs/ssdfs/dentries_tree.h create mode 100644 fs/ssdfs/dev_bdev.c create mode 100644 fs/ssdfs/dev_mtd.c create mode 100644 fs/ssdfs/dev_zns.c create mode 100644 fs/ssdfs/diff_on_write.c create mode 100644 fs/ssdfs/diff_on_write.h create mode 100644 fs/ssdfs/diff_on_write_metadata.c create mode 100644 fs/ssdfs/diff_on_write_user_data.c create mode 100644 fs/ssdfs/dir.c create mode 100644 fs/ssdfs/dynamic_array.c create mode 100644 fs/ssdfs/dynamic_array.h create mode 100644 fs/ssdfs/dynamic_array_test.c create mode 100644 fs/ssdfs/extents_queue.c create mode 100644 fs/ssdfs/extents_queue.h create mode 100644 fs/ssdfs/extents_tree.c create mode 100644 fs/ssdfs/extents_tree.h create mode 100644 fs/ssdfs/file.c create mode 100644 fs/ssdfs/fingerprint.h create mode 100644 fs/ssdfs/fingerprint_array.c create mode 100644 fs/ssdfs/fingerprint_array.h create mode 100644 fs/ssdfs/folio_array.c create mode 100644 fs/ssdfs/folio_array.h create mode 100644 fs/ssdfs/folio_array_test.c create mode 100644 fs/ssdfs/folio_vector.c create mode 100644 fs/ssdfs/folio_vector.h create mode 100644 fs/ssdfs/folio_vector_test.c create mode 100644 fs/ssdfs/fs_error.c create mode 100644 fs/ssdfs/global_fsck.c create mode 100644 fs/ssdfs/inode.c create mode 100644 fs/ssdfs/inodes_tree.c create mode 100644 fs/ssdfs/inodes_tree.h create mode 100644 fs/ssdfs/invalidated_extents_tree.c create mode 100644 fs/ssdfs/invalidated_extents_tree.h create mode 100644 fs/ssdfs/ioctl.c create mode 100644 fs/ssdfs/ioctl.h create mode 100644 fs/ssdfs/log_footer.c create mode 100644 fs/ssdfs/offset_translation_table.c create mode 100644 fs/ssdfs/offset_translation_table.h create mode 100644 fs/ssdfs/options.c create mode 100644 fs/ssdfs/peb.c create mode 100644 fs/ssdfs/peb.h create mode 100644 fs/ssdfs/peb_block_bitmap.c create mode 100644 fs/ssdfs/peb_block_bitmap.h create mode 100644 fs/ssdfs/peb_container.c create mode 100644 fs/ssdfs/peb_container.h create mode 100644 fs/ssdfs/peb_deduplication.c create mode 100644 fs/ssdfs/peb_flush_thread.c create mode 100644 fs/ssdfs/peb_fsck_thread.c create mode 100644 fs/ssdfs/peb_gc_thread.c create mode 100644 fs/ssdfs/peb_init.c create mode 100644 fs/ssdfs/peb_init.h create mode 100644 fs/ssdfs/peb_mapping_queue.c create mode 100644 fs/ssdfs/peb_mapping_queue.h create mode 100644 fs/ssdfs/peb_mapping_table.c create mode 100644 fs/ssdfs/peb_mapping_table.h create mode 100644 fs/ssdfs/peb_mapping_table_cache.c create mode 100644 fs/ssdfs/peb_mapping_table_cache.h create mode 100644 fs/ssdfs/peb_mapping_table_thread.c create mode 100644 fs/ssdfs/peb_migration_scheme.c create mode 100644 fs/ssdfs/peb_read_thread.c create mode 100644 fs/ssdfs/readwrite.c create mode 100644 fs/ssdfs/recovery.c create mode 100644 fs/ssdfs/recovery.h create mode 100644 fs/ssdfs/recovery_fast_search.c create mode 100644 fs/ssdfs/recovery_slow_search.c create mode 100644 fs/ssdfs/recovery_thread.c create mode 100644 fs/ssdfs/request_queue.c create mode 100644 fs/ssdfs/request_queue.h create mode 100644 fs/ssdfs/segment.c create mode 100644 fs/ssdfs/segment.h create mode 100644 fs/ssdfs/segment_bitmap.c create mode 100644 fs/ssdfs/segment_bitmap.h create mode 100644 fs/ssdfs/segment_bitmap_tables.c create mode 100644 fs/ssdfs/segment_block_bitmap.c create mode 100644 fs/ssdfs/segment_block_bitmap.h create mode 100644 fs/ssdfs/segment_tree.c create mode 100644 fs/ssdfs/segment_tree.h create mode 100644 fs/ssdfs/sequence_array.c create mode 100644 fs/ssdfs/sequence_array.h create mode 100644 fs/ssdfs/shared_dictionary.c create mode 100644 fs/ssdfs/shared_dictionary.h create mode 100644 fs/ssdfs/shared_dictionary_thread.c create mode 100644 fs/ssdfs/shared_extents_tree.c create mode 100644 fs/ssdfs/shared_extents_tree.h create mode 100644 fs/ssdfs/shared_extents_tree_thread.c create mode 100644 fs/ssdfs/snapshot.c create mode 100644 fs/ssdfs/snapshot.h create mode 100644 fs/ssdfs/snapshot_requests_queue.c create mode 100644 fs/ssdfs/snapshot_requests_queue.h create mode 100644 fs/ssdfs/snapshot_rules.c create mode 100644 fs/ssdfs/snapshot_rules.h create mode 100644 fs/ssdfs/snapshots_tree.c create mode 100644 fs/ssdfs/snapshots_tree.h create mode 100644 fs/ssdfs/snapshots_tree_thread.c create mode 100644 fs/ssdfs/ssdfs.h create mode 100644 fs/ssdfs/ssdfs_constants.h create mode 100644 fs/ssdfs/ssdfs_fs_info.h create mode 100644 fs/ssdfs/ssdfs_inline.h create mode 100644 fs/ssdfs/ssdfs_inode_info.h create mode 100644 fs/ssdfs/ssdfs_thread_info.h create mode 100644 fs/ssdfs/super.c create mode 100644 fs/ssdfs/sysfs.c create mode 100644 fs/ssdfs/sysfs.h create mode 100644 fs/ssdfs/testing.c create mode 100644 fs/ssdfs/testing.h create mode 100644 fs/ssdfs/tunefs.c create mode 100644 fs/ssdfs/version.h create mode 100644 fs/ssdfs/volume_header.c create mode 100644 fs/ssdfs/xattr.c create mode 100644 fs/ssdfs/xattr.h create mode 100644 fs/ssdfs/xattr_security.c create mode 100644 fs/ssdfs/xattr_tree.c create mode 100644 fs/ssdfs/xattr_tree.h create mode 100644 fs/ssdfs/xattr_trusted.c create mode 100644 fs/ssdfs/xattr_user.c create mode 100644 include/linux/ssdfs_fs.h create mode 100644 include/trace/events/ssdfs.h create mode 100644 include/uapi/linux/ssdfs_fs.h
Signature · 1 line
2.34.1
PATCH v2 1/79 ssdfs: introduce SSDFS on-disk layout
Complete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS architecture is based on segment concept. Segment is a portion of file system volume that has to be aligned on erase block size. Segment can include one or several erase blocks. It is basic unit to allocate and to manage a free space of file system volume. Erase block is a basic unit to keep metadata and user data. Every erase block contains a sequence of logs. Log starts from segment header (struct ssdfs_segment_header) or partial log header (struct ssdfs_partial_log_header). Full log can be finished with log footer (struct ssdfs_log_footer).
Log's header (+footer) contains all necessary metadata describing the log's payload. The log's metadata includes:
(1) block bitmap (struct ssdfs_block_bitmap_fragment) + (struct ssdfs_block_bitmap_header): tracking the state of logical blocks (free, pre-allocated, valid, invalid) in segment.
(2) offset translation table (struct ssdfs_blk2off_table_header) + (struct ssdfs_phys_offset_table_header) +
(struct ssdfs_area_block_table): converts logical block into position inside of particular erase block.
Additionally, log's header is the copy of superblock that keeps knowledge of location the all SSDFS metadata structures. SSDFS has:
(1) mapping table (struct ssdfs_leb_table_fragment_header) + (struct ssdfs_peb_table_fragment_header): implements the mapping of logical erase blocks into "physical" ones.
(2) mapping table cache (struct ssdfs_maptbl_cache_header): copy of content of mapping table for some type of erase blocks. The cache is used for conversion logical erase block ID into "physical" erase block ID in the case when the fragment of mapping table is not initialized yet.
(3) segment bitmap (struct ssdfs_segbmap_fragment_header): tracking state (clean, using, used, pre-dirty, dirty, reserved) of segments with the goal of searching, allocation, erase, and garbage collection.
(4) b-tree (struct ssdfs_btree_descriptor) + (struct ssdfs_btree_index_key) + (struct ssdfs_btree_node_header): all the rest metadata structures are represented by b-trees.
(5) inodes b-tree (struct ssdfs_inodes_btree) +
(struct ssdfs_inodes_btree_node_header): keeps raw inodes of existing file system objects (struct ssdfs_inode).
(6) dentries b-tree (struct ssdfs_dentries_btree_descriptor) + (struct ssdfs_dentries_btree_node_header): keeps directory entries (struct ssdfs_dir_entry).
(7) extents b-tree (struct ssdfs_extents_btree_descriptor) + (struct ssdfs_extents_btree_node_header): keeps raw extents describing the location of piece of data (struct ssdfs_raw_fork) + (struct ssdfs_raw_extent).
(8) xattr b-tree (struct ssdfs_xattr_btree_descriptor) +
(struct ssdfs_xattrs_btree_node_header): keeps extended attributes of file or folder (struct ssdfs_xattr_entry).
(9) invalidated extents b-tree (struct ssdfs_invalidated_extents_btree) + (struct ssdfs_invextree_node_header): keeps information about invalidated extents for ZNS SSD + SMR HDD use cases.
(10) shared dictionary b-tree (struct ssdfs_shared_dictionary_btree) + (struct ssdfs_shared_dictionary_node_header): keeps long names (more than 12 symbols) in the form of tries.
(11) snapshots b-tree (struct ssdfs_snapshots_btree) +
(struct ssdfs_snapshots_btree_node_header): keeps snapshots info (struct ssdfs_snapshot) and association of erase block IDs with timestamps (struct ssdfs_peb2time_set) + (struct ssdfs_peb2time_pair).
--- include/linux/ssdfs_fs.h | 3565 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 3565 insertions(+) create mode 100644 include/linux/ssdfs_fs.h
include/linux/ssdfs_fs.h new file +3565 -0
@@ -0,0 +1,3565 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* include/linux/ssdfs_fs.h - SSDFS on-disk structures and common declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _LINUX_SSDFS_H +#define _LINUX_SSDFS_H + +#include <uapi/linux/ssdfs_fs.h> + +typedef u8 __le8; + +struct ssdfs_inode; + +/* + * struct ssdfs_revision - metadata structure version + * @major: major version number + * @minor: minor version number + */ +struct ssdfs_revision { +/* 0x0000 */ + __le8 major; + __le8 minor; + +/* 0x0002 */ +} __packed; + +/* + * struct ssdfs_signature - metadata structure magic signature + * @common: common magic value + * @key: detailed magic value + */ +struct ssdfs_signature { +/* 0x0000 */ + __le32 common; + __le16 key; + struct ssdfs_revision version; + +/* 0x0008 */ +} __packed; + +/* + * struct ssdfs_metadata_check - metadata structure checksum + * @bytes: bytes count of CRC calculation for the structure + * @flags: flags + * @csum: checksum + */ +struct ssdfs_metadata_check { +/* 0x0000 */ + __le16 bytes; +#define SSDFS_CRC32 (1 << 0) +#define SSDFS_ZLIB_COMPRESSED (1 << 1) +#define SSDFS_LZO_COMPRESSED (1 << 2) + __le16 flags; + __le32 csum; + +/* 0x0008 */ +} __packed; + +/* + * struct ssdfs_padding_header - padding block header + * @magic: magic signature + revision + * @check: metadata checksum + * @blob: padding blob + */ +struct ssdfs_padding_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + struct ssdfs_metadata_check check; + +/* 0x0010 */ + __le64 blob; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_raw_extent - raw (on-disk) extent + * @seg_id: segment number + * @logical_blk: logical block number + * @len: count of blocks in extent + */ +struct ssdfs_raw_extent { +/* 0x0000 */ + __le64 seg_id; + __le32 logical_blk; + __le32 len; + +/* 0x0010 */ +} __packed; + +/* + * struct ssdfs_meta_area_extent - metadata area extent + * @start_id: starting identification number + * @len: count of items in metadata area + * @type: item's type + * @flags: flags + */ +struct ssdfs_meta_area_extent { +/* 0x0000 */ + __le64 start_id; + __le32 len; + __le16 type; + __le16 flags; + +/* 0x0010 */ +} __packed; + +/* Type of item in metadata area */ +enum { + SSDFS_EMPTY_EXTENT_TYPE, + SSDFS_SEG_EXTENT_TYPE, + SSDFS_PEB_EXTENT_TYPE, + SSDFS_BLK_EXTENT_TYPE, +}; + +/* Type of segbmap's segments */ +enum { + SSDFS_MAIN_SEGBMAP_SEG, + SSDFS_COPY_SEGBMAP_SEG, + SSDFS_SEGBMAP_SEG_COPY_MAX, +}; + +#define SSDFS_SEGBMAP_SEGS 8 + +/* + * struct ssdfs_segbmap_sb_header - superblock's segment bitmap header + * @fragments_count: fragments count in segment bitmap + * @fragments_per_seg: segbmap's fragments per segment + * @fragments_per_peb: segbmap's fragments per PEB + * @fragment_size: size of fragment in bytes + * @bytes_count: size of segment bitmap in bytes (payload part) + * @flags: segment bitmap's flags + * @segs_count: count of really reserved segments in one chain + * @segs: array of segbmap's segment numbers + */ +struct ssdfs_segbmap_sb_header { +/* 0x0000 */ + __le16 fragments_count; + __le16 fragments_per_seg; + __le16 fragments_per_peb; + __le16 fragment_size; + +/* 0x0008 */ + __le32 bytes_count; + __le16 flags; + __le16 segs_count; + +/* 0x0010 */ + __le64 segs[SSDFS_SEGBMAP_SEGS][SSDFS_SEGBMAP_SEG_COPY_MAX]; + +/* 0x0090 */ +} __packed; + +/* Segment bitmap's flags */ +#define SSDFS_SEGBMAP_HAS_COPY (1 << 0) +#define SSDFS_SEGBMAP_ERROR (1 << 1) +#define SSDFS_SEGBMAP_MAKE_ZLIB_COMPR (1 << 2) +#define SSDFS_SEGBMAP_MAKE_LZO_COMPR (1 << 3) +#define SSDFS_SEGBMAP_FLAGS_MASK (0xF) + +enum { + SSDFS_MAIN_MAPTBL_SEG, + SSDFS_COPY_MAPTBL_SEG, + SSDFS_MAPTBL_SEG_COPY_MAX, +}; + +#define SSDFS_MAPTBL_RESERVED_EXTENTS (3) + +/* + * struct ssdfs_maptbl_sb_header - superblock's mapping table header + * @fragments_count: count of fragments in mapping table + * @fragment_bytes: bytes in one mapping table's fragment + * @last_peb_recover_cno: checkpoint of last trying to recover PEBs + * @lebs_count: count of Logical Erase Blocks (LEBs) are described by table + * @pebs_count: count of Physical Erase Blocks (PEBs) are described by table + * @fragments_per_seg: count of mapping table's fragments in segment + * @fragments_per_peb: count of mapping table's fragments in PEB + * @flags: mapping table's flags + * @pre_erase_pebs: count of PEBs in pre-erase state + * @lebs_per_fragment: count of LEBs are described by fragment + * @pebs_per_fragment: count of PEBs are described by fragment + * @pebs_per_stripe: count of PEBs are described by stripe + * @stripes_per_fragment: count of stripes in fragment + * @extents: metadata extents that describe mapping table location + */ +struct ssdfs_maptbl_sb_header { +/* 0x0000 */ + __le32 fragments_count; + __le32 fragment_bytes; + __le64 last_peb_recover_cno; + +/* 0x0010 */ + __le64 lebs_count; + __le64 pebs_count; + +/* 0x0020 */ + __le16 fragments_per_seg; + __le16 fragments_per_peb; + __le16 flags; + __le16 pre_erase_pebs; + +/* 0x0028 */ + __le16 lebs_per_fragment; + __le16 pebs_per_fragment; + __le16 pebs_per_stripe; + __le16 stripes_per_fragment; + +/* 0x0030 */ +#define MAPTBL_LIMIT1 (SSDFS_MAPTBL_RESERVED_EXTENTS) +#define MAPTBL_LIMIT2 (SSDFS_MAPTBL_SEG_COPY_MAX) + struct ssdfs_meta_area_extent extents[MAPTBL_LIMIT1][MAPTBL_LIMIT2]; + +/* 0x0090 */ +} __packed; + +/* Mapping table's flags */ +#define SSDFS_MAPTBL_HAS_COPY (1 << 0) +#define SSDFS_MAPTBL_ERROR (1 << 1) +#define SSDFS_MAPTBL_MAKE_ZLIB_COMPR (1 << 2) +#define SSDFS_MAPTBL_MAKE_LZO_COMPR (1 << 3) +#define SSDFS_MAPTBL_UNDER_FLUSH (1 << 4) +#define SSDFS_MAPTBL_START_MIGRATION (1 << 5) +#define SSDFS_MAPTBL_FLAGS_MASK (0x3F) + +/* + * struct ssdfs_btree_descriptor - generic btree descriptor + * @magic: magic signature + * @flags: btree flags + * @type: btree type + * @log_node_size: log2(node size in bytes) + * @pages_per_node: physical pages per btree node + * @node_ptr_size: size in bytes of pointer on btree node + * @index_size: size in bytes of btree's index + * @item_size: size in bytes of btree's item + * @index_area_min_size: minimal size in bytes of index area in btree node + * + * The goal of a btree descriptor is to keep + * the main features of a tree. + */ +struct ssdfs_btree_descriptor { +/* 0x0000 */ + __le32 magic; +#define SSDFS_BTREE_DESC_INDEX_AREA_RESIZABLE (1 << 0) +#define SSDFS_BTREE_DESC_FLAGS_MASK 0x1 + __le16 flags; + __le8 type; + __le8 log_node_size; + +/* 0x0008 */ + __le8 pages_per_node; + __le8 node_ptr_size; + __le16 index_size; + __le16 item_size; + __le16 index_area_min_size; + +/* 0x0010 */ +} __packed; + +/* Btree types */ +enum { + SSDFS_BTREE_UNKNOWN_TYPE, + SSDFS_INODES_BTREE, + SSDFS_DENTRIES_BTREE, + SSDFS_EXTENTS_BTREE, + SSDFS_SHARED_EXTENTS_BTREE, + SSDFS_XATTR_BTREE, + SSDFS_SHARED_XATTR_BTREE, + SSDFS_SHARED_DICTIONARY_BTREE, + SSDFS_SNAPSHOTS_BTREE, + SSDFS_INVALIDATED_EXTENTS_BTREE, + SSDFS_BTREE_TYPE_MAX +}; + +/* + * struct ssdfs_dentries_btree_descriptor - dentries btree descriptor + * @desc: btree descriptor + */ +struct ssdfs_dentries_btree_descriptor { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x10]; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_extents_btree_descriptor - extents btree descriptor + * @desc: btree descriptor + */ +struct ssdfs_extents_btree_descriptor { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x10]; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_xattr_btree_descriptor - extended attr btree descriptor + * @desc: btree descriptor + */ +struct ssdfs_xattr_btree_descriptor { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x10]; + +/* 0x0020 */ +} __packed; + +/* Type of superblock segments */ +enum { + SSDFS_MAIN_SB_SEG, + SSDFS_COPY_SB_SEG, + SSDFS_SB_SEG_COPY_MAX, +}; + +/* Different phases of superblok segment */ +enum { + SSDFS_CUR_SB_SEG, + SSDFS_NEXT_SB_SEG, + SSDFS_RESERVED_SB_SEG, + SSDFS_PREV_SB_SEG, + SSDFS_SB_CHAIN_MAX, +}; + +/* + * struct ssdfs_leb2peb_pair - LEB/PEB numbers association + * @leb_id: LEB ID number + * @peb_id: PEB ID number + */ +struct ssdfs_leb2peb_pair { +/* 0x0000 */ + __le64 leb_id; + __le64 peb_id; + +/* 0x0010 */ +} __packed; + +/* + * struct ssdfs_btree_index - btree index + * @hash: hash value + * @extent: btree node's extent + * + * The goal of btree index is to provide the way to search + * a proper btree node by means of hash value. The hash + * value could be inode_id, string hash and so on. + */ +struct ssdfs_btree_index { +/* 0x0000 */ + __le64 hash; + +/* 0x0008 */ + struct ssdfs_raw_extent extent; + +/* 0x0018 */ +} __packed; + +#define SSDFS_BTREE_NODE_INVALID_ID (U32_MAX) + +/* + * struct ssdfs_btree_index_key - node identification key + * @node_id: node identification key + * @node_type: type of the node + * @height: node's height + * @flags: index flags + * @index: node's index + */ +struct ssdfs_btree_index_key { +/* 0x0000 */ + __le32 node_id; + __le8 node_type; + __le8 height; +#define SSDFS_BTREE_INDEX_HAS_VALID_EXTENT (1 << 0) +#define SSDFS_BTREE_INDEX_SHOW_EMPTY_NODE (1 << 1) +#define SSDFS_BTREE_INDEX_SHOW_FREE_ITEMS (1 << 2) +#define SSDFS_BTREE_INDEX_HAS_CHILD_WITH_FREE_ITEMS (1 << 3) +#define SSDFS_BTREE_INDEX_SHOW_PREALLOCATED_CHILD (1 << 4) +#define SSDFS_BTREE_INDEX_FLAGS_MASK 0x1F + __le16 flags; + +/* 0x0008 */ + struct ssdfs_btree_index index; + +/* 0x0020 */ +} __packed; + +#define SSDFS_BTREE_ROOT_NODE_INDEX_COUNT (2) + +/* + * struct ssdfs_btree_root_node_header - root node header + * @height: btree height + * @items_count: count of items in the root node + * @flags: root node flags + * @type: root node type + * @upper_node_id: last allocated the node identification number + * @node_ids: root node's children IDs + */ +struct ssdfs_btree_root_node_header { +/* 0x0000 */ +#define SSDFS_BTREE_LEAF_NODE_HEIGHT (0) + __le8 height; + __le8 items_count; + __le8 flags; + __le8 type; + +/* 0x0004 */ +#define SSDFS_BTREE_ROOT_NODE_ID (0) + __le32 upper_node_id; + +/* 0x0008 */ + __le32 node_ids[SSDFS_BTREE_ROOT_NODE_INDEX_COUNT]; + +/* 0x0010 */ +} __packed; + +/* + * struct ssdfs_btree_inline_root_node - btree root node + * @header: node header + * @indexes: root node's index array + * + * The goal of root node is to live inside of 0x40 bytes + * space and to keep the root index node of the tree. + * The inline root node could be the part of inode + * structure or the part of btree root. The inode has + * 0x80 bytes space. But inode needs to store as + * extent/dentries tree as extended attributes tree. + * So, 0x80 bytes is used for storing two btrees. + * + * The root node's indexes has pre-defined type. + * If height of the tree equals to 1 - 3 range then + * root node's indexes define hybrid nodes. Otherwise, + * if tree's height is greater than 3 then root node's + * indexes define pure index nodes. + */ +struct ssdfs_btree_inline_root_node { +/* 0x0000 */ + struct ssdfs_btree_root_node_header header; + +/* 0x0010 */ +#define SSDFS_ROOT_NODE_LEFT_LEAF_NODE (0) +#define SSDFS_ROOT_NODE_RIGHT_LEAF_NODE (1) +#define SSDFS_BTREE_ROOT_NODE_INDEX_COUNT (2) + struct ssdfs_btree_index indexes[SSDFS_BTREE_ROOT_NODE_INDEX_COUNT]; + +/* 0x0040 */ +} __packed; + +/* + * struct ssdfs_inodes_btree - inodes btree + * @desc: btree descriptor + * @allocated_inodes: count of allocated inodes + * @free_inodes: count of free inodes + * @inodes_capacity: count of inodes in the whole btree + * @leaf_nodes: count of leaf btree nodes + * @nodes_count: count of nodes in the whole btree + * @upper_allocated_ino: maximal allocated inode ID number + * @root_node: btree's root node + * + * The goal of a btree root is to keep + * the main features of a tree and knowledge + * about two root indexes. These indexes splits + * the whole btree on two branches. + */ +struct ssdfs_inodes_btree { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le64 allocated_inodes; + __le64 free_inodes; + +/* 0x0020 */ + __le64 inodes_capacity; + __le32 leaf_nodes; + __le32 nodes_count; + +/* 0x0030 */ + __le64 upper_allocated_ino; + __le8 reserved[0x8]; + +/* 0x0040 */ + struct ssdfs_btree_inline_root_node root_node; + +/* 0x0080 */ +} __packed; + +/* + * struct ssdfs_shared_extents_btree - shared extents btree + * @desc: btree descriptor + * @root_node: btree's root node + * + * The goal of a btree root is to keep + * the main features of a tree and knowledge + * about two root indexes. These indexes splits + * the whole btree on two branches. + */ +struct ssdfs_shared_extents_btree { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x30]; + +/* 0x0040 */ + struct ssdfs_btree_inline_root_node root_node; + +/* 0x0080 */ +} __packed; + +/* + * ssdfs_shared_dictionary_btree - shared strings dictionary btree + * @desc: btree descriptor + * @root_node: btree's root node + * + * The goal of a btree root is to keep + * the main features of a tree and knowledge + * about two root indexes. These indexes splits + * the whole btree on two branches. + */ +struct ssdfs_shared_dictionary_btree { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x30]; + +/* 0x0040 */ + struct ssdfs_btree_inline_root_node root_node; + +/* 0x0080 */ +} __packed; + +/* + * struct ssdfs_shared_xattr_btree - shared extended attributes btree + * @desc: btree descriptor + * @root_node: btree's root node + * + * The goal of a btree root is to keep + * the main features of a tree and knowledge + * about two root indexes. These indexes splits + * the whole btree on two branches. + */ +struct ssdfs_shared_xattr_btree { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x30]; + +/* 0x0040 */ + struct ssdfs_btree_inline_root_node root_node; + +/* 0x0080 */ +} __packed; + +/* + * struct ssdfs_snapshots_btree - snapshots btree + * @desc: btree descriptor + * @root_node: btree's root node + * + * The goal of a btree root is to keep + * the main features of a tree and knowledge + * about two root indexes. These indexes splits + * the whole btree on two branches. + */ +struct ssdfs_snapshots_btree { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x30]; + +/* 0x0040 */ + struct ssdfs_btree_inline_root_node root_node; + +/* 0x0080 */ +} __packed; + +/* + * struct ssdfs_invalidated_extents_btree - invalidated extents btree + * @desc: btree descriptor + * @root_node: btree's root node + * + * The goal of a btree root is to keep + * the main features of a tree and knowledge + * about two root indexes. These indexes splits + * the whole btree on two branches. + */ +struct ssdfs_invalidated_extents_btree { +/* 0x0000 */ + struct ssdfs_btree_descriptor desc; + +/* 0x0010 */ + __le8 reserved[0x30]; + +/* 0x0040 */ + struct ssdfs_btree_inline_root_node root_node; + +/* 0x0080 */ +} __packed; + +enum { + SSDFS_CUR_DATA_SEG, + SSDFS_CUR_LNODE_SEG, + SSDFS_CUR_HNODE_SEG, + SSDFS_CUR_IDXNODE_SEG, + SSDFS_CUR_DATA_UPDATE_SEG, + SSDFS_CUR_SEGS_COUNT, +}; + +/* + * struct ssdfs_blk_bmap_options - block bitmap options + * @flags: block bitmap's flags + * @compression: compression type + */ +struct ssdfs_blk_bmap_options { +/* 0x0000 */ +#define SSDFS_BLK_BMAP_CREATE_COPY (1 << 0) +#define SSDFS_BLK_BMAP_MAKE_COMPRESSION (1 << 1) +#define SSDFS_BLK_BMAP_OPTIONS_MASK (0x3) + __le16 flags; +#define SSDFS_BLK_BMAP_NOCOMPR_TYPE (0) +#define SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE (1) +#define SSDFS_BLK_BMAP_LZO_COMPR_TYPE (2) + __le8 compression; + __le8 reserved; + +/* 0x0004 */ +} __packed; + +/* + * struct ssdfs_blk2off_tbl_options - offset translation table options + * @flags: offset translation table's flags + * @compression: compression type + */ +struct ssdfs_blk2off_tbl_options { +/* 0x0000 */ +#define SSDFS_BLK2OFF_TBL_CREATE_COPY (1 << 0) +#define SSDFS_BLK2OFF_TBL_MAKE_COMPRESSION (1 << 1) +#define SSDFS_BLK2OFF_TBL_OPTIONS_MASK (0x3) + __le16 flags; +#define SSDFS_BLK2OFF_TBL_NOCOMPR_TYPE (0) +#define SSDFS_BLK2OFF_TBL_ZLIB_COMPR_TYPE (1) +#define SSDFS_BLK2OFF_TBL_LZO_COMPR_TYPE (2) + __le8 compression; + __le8 reserved; + +/* 0x0004 */ +} __packed; + +/* + * struct ssdfs_user_data_options - user data options + * @flags: user data's flags + * @compression: compression type + * @migration_threshold: default value of destination PEBs in migration + */ +struct ssdfs_user_data_options { +/* 0x0000 */ +#define SSDFS_USER_DATA_MAKE_COMPRESSION (1 << 0) +#define SSDFS_USER_DATA_OPTIONS_MASK (0x1) + __le16 flags; +#define SSDFS_USER_DATA_NOCOMPR_TYPE (0) +#define SSDFS_USER_DATA_ZLIB_COMPR_TYPE (1) +#define SSDFS_USER_DATA_LZO_COMPR_TYPE (2) + __le8 compression; + __le8 reserved1; + __le16 migration_threshold; + __le16 reserved2; + +/* 0x0008 */ +} __packed; + +#define SSDFS_INODE_HASNT_INLINE_FORKS (0) +#define SSDFS_INLINE_FORKS_COUNT (2) +#define SSDFS_INLINE_EXTENTS_COUNT (3) + +/* + * struct ssdfs_raw_fork - contiguous sequence of raw (on-disk) extents + * @start_offset: start logical offset in pages (blocks) from file's beginning + * @blks_count: count of logical blocks in the fork (no holes) + * @extents: sequence of raw (on-disk) extents + */ +struct ssdfs_raw_fork { +/* 0x0000 */ + __le64 start_offset; + __le64 blks_count; + +/* 0x0010 */ + struct ssdfs_raw_extent extents[SSDFS_INLINE_EXTENTS_COUNT]; + +/* 0x0040 */ +} __packed; + +/* + * struct ssdfs_name_hash - hash of the name + * @raw: raw value of the hash64 + * + * The name's hash is 64 bits wide (8 bytes). But the hash64 has + * special structure. The first 4 bytes are the low hash (hash32_lo) + * of the name. The second 4 bytes is the high hash (hash32_hi) + * of the name. If the name lesser or equal to 12 symbols (inline + * name's string) then hash32_hi will be equal to zero always. + * If the name is greater than 12 symbols then the hash32_hi + * will be the hash of the rest of the name (excluding the + * first 12 symbols). The hash32_lo will be defined by inline + * name's length. The inline names (12 symbols long) will be + * stored into dentries only. The regular names will be stored + * partially in the dentry (12 symbols) and the whole name string + * will be stored into shared dictionary. + */ +struct ssdfs_name_hash { +/* 0x0000 */ + __le64 raw; + +/* 0x0008 */ +} __packed; + +/* Name hash related macros */ +#define SSDFS_NAME_HASH(hash32_lo, hash32_hi)({ \ + u64 hash64 = (u32)hash32_lo; \ + hash64 <<= 32; \ + hash64 |= hash32_hi; \ + hash64; \ +}) +#define SSDFS_NAME_HASH_LE64(hash32_lo, hash32_hi) \ + (cpu_to_le64(SSDFS_NAME_HASH(hash32_lo, hash32_hi))) +#define LE64_TO_SSDFS_HASH32_LO(hash_le64) \ + ((u32)(le64_to_cpu(hash_le64) >> 32)) +#define SSDFS_HASH32_LO(hash64) \ + ((u32)(hash64 >> 32)) +#define LE64_TO_SSDFS_HASH32_HI(hash_le64) \ + ((u32)(le64_to_cpu(hash_le64) & 0xFFFFFFFF)) +#define SSDFS_HASH32_HI(hash64) \ + ((u32)(hash64 & 0xFFFFFFFF)) + +/* + * struct ssdfs_dir_entry - directory entry + * @ino: inode number + * @hash_code: name string's hash code + * @name_len: name length in bytes + * @dentry_type: dentry type + * @file_type: directory file types + * @flags: dentry's flags + * @inline_string: inline copy of the name or exclusive storage of short name + */ +struct ssdfs_dir_entry { +/* 0x0000 */ + __le64 ino; + __le64 hash_code; + +/* 0x0010 */ + __le8 name_len; + __le8 dentry_type; + __le8 file_type; + __le8 flags; +#define SSDFS_DENTRY_INLINE_NAME_MAX_LEN (12) + __le8 inline_string[SSDFS_DENTRY_INLINE_NAME_MAX_LEN]; + +/* 0x0020 */ +} __packed; + +/* Dentry types */ +enum { + SSDFS_DENTRY_UNKNOWN_TYPE, + SSDFS_INLINE_DENTRY, + SSDFS_REGULAR_DENTRY, + SSDFS_DENTRY_TYPE_MAX +}; + +/* + * SSDFS directory file types. + */ +enum { + SSDFS_FT_UNKNOWN, + SSDFS_FT_REG_FILE, + SSDFS_FT_DIR, + SSDFS_FT_CHRDEV, + SSDFS_FT_BLKDEV, + SSDFS_FT_FIFO, + SSDFS_FT_SOCK, + SSDFS_FT_SYMLINK, + SSDFS_FT_MAX +}; + +/* Dentry flags */ +#define SSDFS_DENTRY_HAS_EXTERNAL_STRING (1 << 0) +#define SSDFS_DENTRY_FLAGS_MASK 0x1 + +/* + * struct ssdfs_blob_extent - blob's extent descriptor + * @hash: blob's hash + * @extent: blob's extent + */ +struct ssdfs_blob_extent { +/* 0x0000 */ + __le64 hash; + __le64 reserved; + struct ssdfs_raw_extent extent; + +/* 0x0020 */ +} __packed; + +#define SSDFS_XATTR_INLINE_BLOB_MAX_LEN (32) +#define SSDFS_XATTR_EXTERNAL_BLOB_MAX_LEN (32768) + +/* + * struct ssdfs_blob_bytes - inline blob's byte stream + * @bytes: byte stream + */ +struct ssdfs_blob_bytes { +/* 0x0000 */ + __le8 bytes[SSDFS_XATTR_INLINE_BLOB_MAX_LEN]; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_xattr_entry - extended attribute entry + * @name_hash: hash of the name + * @inline_index: index of the inline xattr + * @name_len: length of the name + * @name_type: type of the name + * @name_flags: flags of the name + * @blob_len: blob length in bytes + * @blob_type: type of the blob + * @blob_flags: flags of the blob + * @inline_string: inline string of the name + * @blob.descriptor.hash: hash of the blob + * @blob.descriptor.extent: extent of the blob + * @blob.inline_value: inline value of the blob + * + * The extended attribute can be described by fixed size + * descriptor. The name of extended attribute can be inline + * or to be stored into the shared dictionary. If the name + * is greater than 16 symbols then it will be stored into shared + * dictionary. The blob part can be stored inline or, + * otherwise, the descriptor contains the hash of the blob + * and blob will be stored as ordinary file inside + * of logical blocks. + */ +struct ssdfs_xattr_entry { +/* 0x0000 */ + __le64 name_hash; + +/* 0x0008 */ + __le8 inline_index; + __le8 name_len; + __le8 name_type; + __le8 name_flags; + +/* 0x000C */ + __le16 blob_len; + __le8 blob_type; + __le8 blob_flags; + +/* 0x0010 */ +#define SSDFS_XATTR_INLINE_NAME_MAX_LEN (16) + __le8 inline_string[SSDFS_XATTR_INLINE_NAME_MAX_LEN]; + +/* 0x0020 */ + union { + struct ssdfs_blob_extent descriptor; + struct ssdfs_blob_bytes inline_value; + } blob; + +/* 0x0040 */ +} __packed; + +/* registered names' prefixes */ +enum { + SSDFS_USER_NS_INDEX, + SSDFS_TRUSTED_NS_INDEX, + SSDFS_SYSTEM_NS_INDEX, + SSDFS_SECURITY_NS_INDEX, + SSDFS_REGISTERED_NS_NUMBER +}; + +static const char * const SSDFS_NS_PREFIX[] = { + "user.", + "trusted.", + "system.", + "security.", +}; + +/* xattr name types */ +enum { + SSDFS_XATTR_NAME_UNKNOWN_TYPE, + SSDFS_XATTR_INLINE_NAME, + SSDFS_XATTR_USER_INLINE_NAME, + SSDFS_XATTR_TRUSTED_INLINE_NAME, + SSDFS_XATTR_SYSTEM_INLINE_NAME, + SSDFS_XATTR_SECURITY_INLINE_NAME, + SSDFS_XATTR_REGULAR_NAME, + SSDFS_XATTR_USER_REGULAR_NAME, + SSDFS_XATTR_TRUSTED_REGULAR_NAME, + SSDFS_XATTR_SYSTEM_REGULAR_NAME, + SSDFS_XATTR_SECURITY_REGULAR_NAME, + SSDFS_XATTR_NAME_TYPE_MAX +}; + +/* xattr name flags */ +#define SSDFS_XATTR_HAS_EXTERNAL_STRING (1 << 0) +#define SSDFS_XATTR_NAME_FLAGS_MASK 0x1 + +/* xattr blob types */ +enum { + SSDFS_XATTR_BLOB_UNKNOWN_TYPE, + SSDFS_XATTR_INLINE_BLOB, + SSDFS_XATTR_REGULAR_BLOB, + SSDFS_XATTR_BLOB_TYPE_MAX +}; + +/* xattr blob flags */ +#define SSDFS_XATTR_HAS_EXTERNAL_BLOB (1 << 0) +#define SSDFS_XATTR_BLOB_FLAGS_MASK 0x1 + +#define SSDFS_INLINE_DENTRIES_PER_AREA (2) +#define SSDFS_INLINE_STREAM_SIZE_PER_AREA (64) +#define SSDFS_DEFAULT_INLINE_XATTR_COUNT (1) + +/* + * struct ssdfs_inode_inline_stream - inode's inline stream + * @bytes: bytes array + */ +struct ssdfs_inode_inline_stream { +/* 0x0000 */ + __le8 bytes[SSDFS_INLINE_STREAM_SIZE_PER_AREA]; + +/* 0x0040 */ +} __packed; + +/* + * struct ssdfs_inode_inline_dentries - inline dentries array + * @array: dentries array + */ +struct ssdfs_inode_inline_dentries { +/* 0x0000 */ + struct ssdfs_dir_entry array[SSDFS_INLINE_DENTRIES_PER_AREA]; + +/* 0x0040 */ +} __packed; + +/* + * struct ssdfs_inode_private_area - inode's private area + * @area1.inline_stream: inline file's content + * @area1.extents_root: extents btree root node + * @area1.fork: inline fork + * @area1.dentries_root: dentries btree root node + * @area1.dentries: inline dentries + * @area2.inline_stream: inline file's content + * @area2.inline_xattr: inline extended attribute + * @area2.xattr_root: extended attributes btree root node + * @area2.fork: inline fork + * @area2.dentries: inline dentries + */ +struct ssdfs_inode_private_area { +/* 0x0000 */ + union { + struct ssdfs_inode_inline_stream inline_stream; + struct ssdfs_btree_inline_root_node extents_root; + struct ssdfs_raw_fork fork; + struct ssdfs_btree_inline_root_node dentries_root; + struct ssdfs_inode_inline_dentries dentries; + } area1; + +/* 0x0040 */ + union { + struct ssdfs_inode_inline_stream inline_stream; + struct ssdfs_xattr_entry inline_xattr; + struct ssdfs_btree_inline_root_node xattr_root; + struct ssdfs_raw_fork fork; + struct ssdfs_inode_inline_dentries dentries; + } area2; + +/* 0x0080 */ +} __packed; + +/* + * struct ssdfs_inode - raw (on-disk) inode + * @magic: inode magic + * @mode: file mode + * @flags: file attributes + * @uid: owner user ID + * @gid: owner group ID + * @atime: access time (seconds) + * @ctime: change time (seconds) + * @mtime: modification time (seconds) + * @birthtime: inode creation time (seconds) + * @atime_nsec: access time in nano scale + * @ctime_nsec: change time in nano scale + * @mtime_nsec: modification time in nano scale + * @birthtime_nsec: creation time in nano scale + * @generation: file version (for NFS) + * @size: file size in bytes + * @blocks: file size in blocks + * @parent_ino: parent inode number + * @refcount: links count + * @checksum: inode checksum + * @ino: inode number + * @hash_code: hash code of file name + * @name_len: lengh of file name + * @forks_count: count of forks + * @internal: array of inline private areas of inode + */ +struct ssdfs_inode { +/* 0x0000 */ + __le16 magic; /* Inode magic */ + __le16 mode; /* File mode */ + __le32 flags; /* file attributes */ + +/* 0x0008 */ + __le32 uid; /* user ID */ + __le32 gid; /* group ID */ + +/* 0x0010 */ + __le64 atime; /* access time */ + __le64 ctime; /* change time */ + __le64 mtime; /* modification time */ + __le64 birthtime; /* inode creation time */ + +/* 0x0030 */ + __le32 atime_nsec; /* access time in nano scale */ + __le32 ctime_nsec; /* change time in nano scale */ + __le32 mtime_nsec; /* modification time in nano scale */ + __le32 birthtime_nsec; /* creation time in nano scale */ + +/* 0x0040 */ + __le64 generation; /* file version (for NFS) */ + __le64 size; /* file size in bytes */ + __le64 blocks; /* file size in blocks */ + __le64 parent_ino; /* parent inode number */ + +/* 0x0060 */ + __le32 refcount; /* links count */ + __le32 checksum; /* inode checksum */ + +/* 0x0068 */ + __le64 ino; /* Inode number */ + __le64 hash_code; /* hash code of file name */ + __le16 name_len; /* lengh of file name */ +#define SSDFS_INODE_HAS_INLINE_EXTENTS (1 << 0) +#define SSDFS_INODE_HAS_EXTENTS_BTREE (1 << 1) +#define SSDFS_INODE_HAS_INLINE_DENTRIES (1 << 2) +#define SSDFS_INODE_HAS_DENTRIES_BTREE (1 << 3) +#define SSDFS_INODE_HAS_INLINE_XATTR (1 << 4) +#define SSDFS_INODE_HAS_XATTR_BTREE (1 << 5) +#define SSDFS_INODE_HAS_INLINE_FILE (1 << 6) +#define SSDFS_INODE_PRIVATE_FLAGS_MASK 0x7F + __le16 private_flags; + + union { + __le32 forks; + __le32 dentries; + } count_of __packed; + +/* 0x0080 */ + struct ssdfs_inode_private_area internal[1]; + +/* 0x0100 */ +} __packed; + +#define SSDFS_IFREG_PRIVATE_FLAG_MASK \ + (SSDFS_INODE_HAS_INLINE_EXTENTS | \ + SSDFS_INODE_HAS_EXTENTS_BTREE | \ + SSDFS_INODE_HAS_INLINE_XATTR | \ + SSDFS_INODE_HAS_XATTR_BTREE | \ + SSDFS_INODE_HAS_INLINE_FILE) + +#define SSDFS_IFDIR_PRIVATE_FLAG_MASK \ + (SSDFS_INODE_HAS_INLINE_DENTRIES | \ + SSDFS_INODE_HAS_DENTRIES_BTREE | \ + SSDFS_INODE_HAS_INLINE_XATTR | \ + SSDFS_INODE_HAS_XATTR_BTREE) + +/* + * struct ssdfs_volume_header - static part of superblock + * @magic: magic signature + revision + * @check: metadata checksum + * @log_pagesize: log2(page size) + * @log_erasesize: log2(erase block size) + * @log_segsize: log2(segment size) + * @log_pebs_per_seg: log2(erase blocks per segment) + * @megabytes_per_peb: MBs in one PEB + * @pebs_per_seg: number of PEBs per segment + * @create_time: volume create timestamp (mkfs phase) + * @create_cno: volume create checkpoint + * @flags: volume creation flags + * @lebs_per_peb_index: difference of LEB IDs between PEB indexes in segment + * @sb_pebs: array of prev, cur and next superblock's PEB numbers + * @segbmap: superblock's segment bitmap header + * @maptbl: superblock's mapping table header + * @sb_seg_log_pages: full log size in sb segment (pages count) + * @segbmap_log_pages: full log size in segbmap segment (pages count) + * @maptbl_log_pages: full log size in maptbl segment (pages count) + * @lnodes_seg_log_pages: full log size in leaf nodes segment (pages count) + * @hnodes_seg_log_pages: full log size in hybrid nodes segment (pages count) + * @inodes_seg_log_pages: full log size in index nodes segment (pages count) + * @user_data_log_pages: full log size in user data segment (pages count) + * @create_threads_per_seg: number of creation threads per segment + * @dentries_btree: descriptor of all dentries btrees + * @extents_btree: descriptor of all extents btrees + * @xattr_btree: descriptor of all extended attributes btrees + * @invextree: b-tree of invalidated extents (ZNS SSD) + * @uuid: 128-bit uuid for volume + */ +struct ssdfs_volume_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + struct ssdfs_metadata_check check; + +/* 0x0010 */ + __le8 log_pagesize; + __le8 log_erasesize; + __le8 log_segsize; + __le8 log_pebs_per_seg; + __le16 megabytes_per_peb; + __le16 pebs_per_seg; + +/* 0x0018 */ + __le64 create_time; + __le64 create_cno; +#define SSDFS_VH_ZNS_BASED_VOLUME (1 << 0) +#define SSDFS_VH_UNALIGNED_ZONE (1 << 1) +#define SSDFS_VH_FLAGS_MASK (0x3) + __le32 flags; + __le32 lebs_per_peb_index; + +/* 0x0030 */ +#define VH_LIMIT1 SSDFS_SB_CHAIN_MAX +#define VH_LIMIT2 SSDFS_SB_SEG_COPY_MAX + struct ssdfs_leb2peb_pair sb_pebs[VH_LIMIT1][VH_LIMIT2]; + +/* 0x00B0 */ + struct ssdfs_segbmap_sb_header segbmap; + +/* 0x0140 */ + struct ssdfs_maptbl_sb_header maptbl; + +/* 0x01D0 */ + __le16 sb_seg_log_pages; + __le16 segbmap_log_pages; + __le16 maptbl_log_pages; + __le16 lnodes_seg_log_pages; + __le16 hnodes_seg_log_pages; + __le16 inodes_seg_log_pages; + __le16 user_data_log_pages; + __le16 create_threads_per_seg; + +/* 0x01E0 */ + struct ssdfs_dentries_btree_descriptor dentries_btree; + +/* 0x0200 */ + struct ssdfs_extents_btree_descriptor extents_btree; + +/* 0x0220 */ + struct ssdfs_xattr_btree_descriptor xattr_btree; + +/* 0x0240 */ + struct ssdfs_invalidated_extents_btree invextree; + +/* 0x02C0 */ + __le8 uuid[SSDFS_UUID_SIZE]; + +/* 0x02D0 */ + __le8 reserved4[0x130]; + +/* 0x0400 */ +} __packed; + +#define SSDFS_LEBS_PER_PEB_INDEX_DEFAULT (1) + +/* + * struct ssdfs_volume_state - changeable part of superblock + * @magic: magic signature + revision + * @check: metadata checksum + * @nsegs: segments count + * @free_pages: free pages count + * @timestamp: write timestamp + * @cno: write checkpoint + * @flags: volume flags + * @state: file system state + * @errors: behaviour when detecting errors + * @feature_compat: compatible feature set + * @feature_compat_ro: read-only compatible feature set + * @feature_incompat: incompatible feature set + * @uuid: 128-bit uuid for volume + * @label: volume name + * @cur_segs: array of current segment numbers + * @migration_threshold: default value of destination PEBs in migration + * @blkbmap: block bitmap options + * @blk2off_tbl: offset translation table options + * @user_data: user data options + * @open_zones: number of open/active zones + * @root_folder: copy of root folder's inode + * @inodes_btree: inodes btree root + * @shared_extents_btree: shared extents btree root + * @shared_dict_btree: shared dictionary btree root + * @snapshots_btree: snapshots btree root + */ +struct ssdfs_volume_state { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + struct ssdfs_metadata_check check; + +/* 0x0010 */ + __le64 nsegs; + __le64 free_pages; + +/* 0x0020 */ + __le64 timestamp; + __le64 cno; + +/* 0x0030 */ +#define SSDFS_HAS_INLINE_INODES_TREE (1 << 0) +#define SSDFS_VOLUME_STATE_FLAGS_MASK 0x1 + __le32 flags; + __le16 state; + __le16 errors; + +/* 0x0038 */ + __le64 feature_compat; + __le64 feature_compat_ro; + __le64 feature_incompat; + +/* 0x0050 */ + __le8 uuid[SSDFS_UUID_SIZE]; + char label[SSDFS_VOLUME_LABEL_MAX]; + +/* 0x0070 */ + __le64 cur_segs[SSDFS_CUR_SEGS_COUNT]; + +/* 0x0098 */ + __le16 migration_threshold; + __le16 reserved1; + +/* 0x009C */ + struct ssdfs_blk_bmap_options blkbmap; + struct ssdfs_blk2off_tbl_options blk2off_tbl; + +/* 0x00A4 */ + struct ssdfs_user_data_options user_data; + +/* 0x00AC */ + __le32 open_zones; + +/* 0x00B0 */ + struct ssdfs_inode root_folder; + +/* 0x01B0 */ + __le8 reserved3[0x50]; + +/* 0x0200 */ + struct ssdfs_inodes_btree inodes_btree; + +/* 0x0280 */ + struct ssdfs_shared_extents_btree shared_extents_btree; + +/* 0x0300 */ + struct ssdfs_shared_dictionary_btree shared_dict_btree; + +/* 0x0380 */ + struct ssdfs_snapshots_btree snapshots_btree; + +/* 0x0400 */ +} __packed; + +/* Compatible feature flags */ +#define SSDFS_HAS_SEGBMAP_COMPAT_FLAG (1 << 0) +#define SSDFS_HAS_MAPTBL_COMPAT_FLAG (1 << 1) +#define SSDFS_HAS_SHARED_EXTENTS_COMPAT_FLAG (1 << 2) +#define SSDFS_HAS_SHARED_XATTRS_COMPAT_FLAG (1 << 3) +#define SSDFS_HAS_SHARED_DICT_COMPAT_FLAG (1 << 4) +#define SSDFS_HAS_INODES_TREE_COMPAT_FLAG (1 << 5) +#define SSDFS_HAS_SNAPSHOTS_TREE_COMPAT_FLAG (1 << 6) +#define SSDFS_HAS_INVALID_EXTENTS_TREE_COMPAT_FLAG (1 << 7) + +/* Read-Only compatible feature flags */ +#define SSDFS_ZLIB_COMPAT_RO_FLAG (1 << 0) +#define SSDFS_LZO_COMPAT_RO_FLAG (1 << 1) + +#define SSDFS_FEATURE_COMPAT_SUPP \ + (SSDFS_HAS_SEGBMAP_COMPAT_FLAG | SSDFS_HAS_MAPTBL_COMPAT_FLAG | \ + SSDFS_HAS_SHARED_EXTENTS_COMPAT_FLAG | \ + SSDFS_HAS_SHARED_XATTRS_COMPAT_FLAG | \ + SSDFS_HAS_SHARED_DICT_COMPAT_FLAG | \ + SSDFS_HAS_INODES_TREE_COMPAT_FLAG | \ + SSDFS_HAS_SNAPSHOTS_TREE_COMPAT_FLAG | \ + SSDFS_HAS_INVALID_EXTENTS_TREE_COMPAT_FLAG) + +#define SSDFS_FEATURE_COMPAT_RO_SUPP \ + (SSDFS_ZLIB_COMPAT_RO_FLAG | SSDFS_LZO_COMPAT_RO_FLAG) + +#define SSDFS_FEATURE_INCOMPAT_SUPP 0ULL + +/* + * struct ssdfs_metadata_descriptor - metadata descriptor + * @offset: offset in bytes + * @size: size in bytes + * @check: metadata checksum + */ +struct ssdfs_metadata_descriptor { +/* 0x0000 */ + __le32 offset; + __le32 size; + struct ssdfs_metadata_check check; + +/* 0x0010 */ +} __packed; + +enum { + SSDFS_BLK_BMAP_INDEX, + SSDFS_SNAPSHOT_RULES_AREA_INDEX, + SSDFS_OFF_TABLE_INDEX, + SSDFS_COLD_PAYLOAD_AREA_INDEX, + SSDFS_WARM_PAYLOAD_AREA_INDEX, + SSDFS_HOT_PAYLOAD_AREA_INDEX, + SSDFS_BLK_DESC_AREA_INDEX, + SSDFS_MAPTBL_CACHE_INDEX, + SSDFS_LOG_FOOTER_INDEX, + SSDFS_SEG_HDR_DESC_MAX = SSDFS_LOG_FOOTER_INDEX + 1, + SSDFS_LOG_FOOTER_DESC_MAX = SSDFS_OFF_TABLE_INDEX + 1, +}; + +enum { + SSDFS_PREV_MIGRATING_PEB, + SSDFS_CUR_MIGRATING_PEB, + SSDFS_MIGRATING_PEBS_CHAIN +}; + +/* + * struct ssdfs_segment_header - header of segment + * @volume_hdr: copy of static part of superblock + * @timestamp: log creation timestamp + * @cno: log checkpoint + * @log_pages: size of log (partial segment) in pages count + * @seg_type: type of segment + * @seg_flags: flags of segment + * @desc_array: array of segment's metadata descriptors + * @peb_migration_id: identification number of PEB in migration sequence + * @peb_create_time: PEB creation timestamp + * @seg_id: segment ID that contains this PEB + * @leb_id: LEB ID that mapped with this PEB + * @peb_id: PEB ID + * @relation_peb_id: source PEB ID during migration + * @payload: space for segment header's payload + */ +struct ssdfs_segment_header { +/* 0x0000 */ + struct ssdfs_volume_header volume_hdr; + +/* 0x0400 */ + __le64 timestamp; + __le64 cno; + +/* 0x0410 */ + __le16 log_pages; + __le16 seg_type; + __le32 seg_flags; + +/* 0x0418 */ + struct ssdfs_metadata_descriptor desc_array[SSDFS_SEG_HDR_DESC_MAX]; + +/* 0x04A8 */ +#define SSDFS_PEB_UNKNOWN_MIGRATION_ID (0) +#define SSDFS_PEB_MIGRATION_ID_START (1) +#define SSDFS_PEB_MIGRATION_ID_MAX (U8_MAX) + __le8 peb_migration_id[SSDFS_MIGRATING_PEBS_CHAIN]; + __le8 reserved[0x6]; + +/* 0x4B0 */ + __le64 peb_create_time; + +/* 0x4B8 */ + __le64 seg_id; + __le64 leb_id; + __le64 peb_id; + __le64 relation_peb_id; + +/* 0x4D8 */ + __le8 payload[0x328]; + +/* 0x0800 */ +} __packed; + +/* Possible segment types */ +#define SSDFS_UNKNOWN_SEG_TYPE (0) +#define SSDFS_SB_SEG_TYPE (1) +#define SSDFS_INITIAL_SNAPSHOT_SEG_TYPE (2) +#define SSDFS_SEGBMAP_SEG_TYPE (3) +#define SSDFS_MAPTBL_SEG_TYPE (4) +#define SSDFS_LEAF_NODE_SEG_TYPE (5) +#define SSDFS_HYBRID_NODE_SEG_TYPE (6) +#define SSDFS_INDEX_NODE_SEG_TYPE (7) +#define SSDFS_USER_DATA_SEG_TYPE (8) +#define SSDFS_LAST_KNOWN_SEG_TYPE SSDFS_USER_DATA_SEG_TYPE + +/* Segment flags' bits */ +#define SSDFS_BLK_BMAP_BIT (0) +#define SSDFS_OFFSET_TABLE_BIT (1) +#define SSDFS_COLD_PAYLOAD_BIT (2) +#define SSDFS_WARM_PAYLOAD_BIT (3) +#define SSDFS_HOT_PAYLOAD_BIT (4) +#define SSDFS_BLK_DESC_CHAIN_BIT (5) +#define SSDFS_MAPTBL_CACHE_BIT (6) +#define SSDFS_FOOTER_BIT (7) +#define SSDFS_PARTIAL_LOG_BIT (8) +#define SSDFS_PARTIAL_LOG_HEADER_BIT (9) +#define SSDFS_PLH_INSTEAD_FOOTER_BIT (10) + +/* Segment flags */ +#define SSDFS_SEG_HDR_HAS_BLK_BMAP (1 << SSDFS_BLK_BMAP_BIT) +#define SSDFS_SEG_HDR_HAS_OFFSET_TABLE (1 << SSDFS_OFFSET_TABLE_BIT) +#define SSDFS_LOG_HAS_COLD_PAYLOAD (1 << SSDFS_COLD_PAYLOAD_BIT) +#define SSDFS_LOG_HAS_WARM_PAYLOAD (1 << SSDFS_WARM_PAYLOAD_BIT) +#define SSDFS_LOG_HAS_HOT_PAYLOAD (1 << SSDFS_HOT_PAYLOAD_BIT) +#define SSDFS_LOG_HAS_BLK_DESC_CHAIN (1 << SSDFS_BLK_DESC_CHAIN_BIT) +#define SSDFS_LOG_HAS_MAPTBL_CACHE (1 << SSDFS_MAPTBL_CACHE_BIT) +#define SSDFS_LOG_HAS_FOOTER (1 << SSDFS_FOOTER_BIT) +#define SSDFS_LOG_IS_PARTIAL (1 << SSDFS_PARTIAL_LOG_BIT) +#define SSDFS_LOG_HAS_PARTIAL_HEADER (1 << SSDFS_PARTIAL_LOG_HEADER_BIT) +#define SSDFS_PARTIAL_HEADER_INSTEAD_FOOTER (1 << SSDFS_PLH_INSTEAD_FOOTER_BIT) +#define SSDFS_SEG_HDR_FLAG_MASK 0x7FF + +/* Segment flags manipulation functions */ +#define SSDFS_SEG_HDR_FNS(bit, name) \ +static inline void ssdfs_set_##name(struct ssdfs_segment_header *hdr) \ +{ \ + unsigned long seg_flags = le32_to_cpu(hdr->seg_flags); \ + set_bit(SSDFS_##bit, &seg_flags); \ + hdr->seg_flags = cpu_to_le32((u32)seg_flags); \ +} \ +static inline void ssdfs_clear_##name(struct ssdfs_segment_header *hdr) \ +{ \ + unsigned long seg_flags = le32_to_cpu(hdr->seg_flags); \ + clear_bit(SSDFS_##bit, &seg_flags); \ + hdr->seg_flags = cpu_to_le32((u32)seg_flags); \ +} \ +static inline int ssdfs_##name(struct ssdfs_segment_header *hdr) \ +{ \ + unsigned long seg_flags = le32_to_cpu(hdr->seg_flags); \ + return test_bit(SSDFS_##bit, &seg_flags); \ +} + +/* + * ssdfs_set_seg_hdr_has_blk_bmap() + * ssdfs_clear_seg_hdr_has_blk_bmap() + * ssdfs_seg_hdr_has_blk_bmap() + */ +SSDFS_SEG_HDR_FNS(BLK_BMAP_BIT, seg_hdr_has_blk_bmap) + +/* + * ssdfs_set_seg_hdr_has_offset_table() + * ssdfs_clear_seg_hdr_has_offset_table() + * ssdfs_seg_hdr_has_offset_table() + */ +SSDFS_SEG_HDR_FNS(OFFSET_TABLE_BIT, seg_hdr_has_offset_table) + +/* + * ssdfs_set_log_has_cold_payload() + * ssdfs_clear_log_has_cold_payload() + * ssdfs_log_has_cold_payload() + */ +SSDFS_SEG_HDR_FNS(COLD_PAYLOAD_BIT, log_has_cold_payload) + +/* + * ssdfs_set_log_has_warm_payload() + * ssdfs_clear_log_has_warm_payload() + * ssdfs_log_has_warm_payload() + */ +SSDFS_SEG_HDR_FNS(WARM_PAYLOAD_BIT, log_has_warm_payload) + +/* + * ssdfs_set_log_has_hot_payload() + * ssdfs_clear_log_has_hot_payload() + * ssdfs_log_has_hot_payload() + */ +SSDFS_SEG_HDR_FNS(HOT_PAYLOAD_BIT, log_has_hot_payload) + +/* + * ssdfs_set_log_has_blk_desc_chain() + * ssdfs_clear_log_has_blk_desc_chain() + * ssdfs_log_has_blk_desc_chain() + */ +SSDFS_SEG_HDR_FNS(BLK_DESC_CHAIN_BIT, log_has_blk_desc_chain) + +/* + * ssdfs_set_log_has_maptbl_cache() + * ssdfs_clear_log_has_maptbl_cache() + * ssdfs_log_has_maptbl_cache() + */ +SSDFS_SEG_HDR_FNS(MAPTBL_CACHE_BIT, log_has_maptbl_cache) + +/* + * ssdfs_set_log_has_footer() + * ssdfs_clear_log_has_footer() + * ssdfs_log_has_footer() + */ +SSDFS_SEG_HDR_FNS(FOOTER_BIT, log_has_footer) + +/* + * ssdfs_set_log_is_partial() + * ssdfs_clear_log_is_partial() + * ssdfs_log_is_partial() + */ +SSDFS_SEG_HDR_FNS(PARTIAL_LOG_BIT, log_is_partial) + +/* + * ssdfs_set_log_has_partial_header() + * ssdfs_clear_log_has_partial_header() + * ssdfs_log_has_partial_header() + */ +SSDFS_SEG_HDR_FNS(PARTIAL_LOG_HEADER_BIT, log_has_partial_header) + +/* + * ssdfs_set_partial_header_instead_footer() + * ssdfs_clear_partial_header_instead_footer() + * ssdfs_partial_header_instead_footer() + */ +SSDFS_SEG_HDR_FNS(PLH_INSTEAD_FOOTER_BIT, partial_header_instead_footer) + +/* + * struct ssdfs_log_footer - footer of partial log + * @volume_state: changeable part of superblock + * @timestamp: writing timestamp + * @cno: writing checkpoint + * @log_bytes: payload size in bytes + * @log_flags: flags of log + * @reserved1: reserved field + * @desc_array: array of footer's metadata descriptors + * @peb_create_time: PEB creation timestamp + * @payload: space for log footer's payload + */ +struct ssdfs_log_footer { +/* 0x0000 */ + struct ssdfs_volume_state volume_state; + +/* 0x0400 */ + __le64 timestamp; + __le64 cno; + +/* 0x0410 */ + __le32 log_bytes; + __le32 log_flags; + __le64 reserved1; + +/* 0x0420 */ + struct ssdfs_metadata_descriptor desc_array[SSDFS_LOG_FOOTER_DESC_MAX]; + +/* 0x0450 */ + __le64 peb_create_time; + +/* 0x0458 */ + __le8 payload[0x3A8]; + +/* 0x0800 */ +} __packed; + +/* Log footer flags' bits */ +#define __SSDFS_BLK_BMAP_BIT (0) +#define __SSDFS_OFFSET_TABLE_BIT (1) +#define __SSDFS_PARTIAL_LOG_BIT (2) +#define __SSDFS_ENDING_LOG_BIT (3) +#define __SSDFS_SNAPSHOT_RULE_AREA_BIT (4) + +/* Log footer flags */ +#define SSDFS_LOG_FOOTER_HAS_BLK_BMAP (1 << __SSDFS_BLK_BMAP_BIT) +#define SSDFS_LOG_FOOTER_HAS_OFFSET_TABLE (1 << __SSDFS_OFFSET_TABLE_BIT) +#define SSDFS_PARTIAL_LOG_FOOTER (1 << __SSDFS_PARTIAL_LOG_BIT) +#define SSDFS_ENDING_LOG_FOOTER (1 << __SSDFS_ENDING_LOG_BIT) +#define SSDFS_LOG_FOOTER_HAS_SNAPSHOT_RULES (1 << __SSDFS_SNAPSHOT_RULE_AREA_BIT) +#define SSDFS_LOG_FOOTER_FLAG_MASK 0x1F + +/* Log footer flags manipulation functions */ +#define SSDFS_LOG_FOOTER_FNS(bit, name) \ +static inline void ssdfs_set_##name(struct ssdfs_log_footer *footer) \ +{ \ + unsigned long log_flags = le32_to_cpu(footer->log_flags); \ + set_bit(__SSDFS_##bit, &log_flags); \ + footer->log_flags = cpu_to_le32((u32)log_flags); \ +} \ +static inline void ssdfs_clear_##name(struct ssdfs_log_footer *footer) \ +{ \ + unsigned long log_flags = le32_to_cpu(footer->log_flags); \ + clear_bit(__SSDFS_##bit, &log_flags); \ + footer->log_flags = cpu_to_le32((u32)log_flags); \ +} \ +static inline int ssdfs_##name(struct ssdfs_log_footer *footer) \ +{ \ + unsigned long log_flags = le32_to_cpu(footer->log_flags); \ + return test_bit(__SSDFS_##bit, &log_flags); \ +} + +/* + * ssdfs_set_log_footer_has_blk_bmap() + * ssdfs_clear_log_footer_has_blk_bmap() + * ssdfs_log_footer_has_blk_bmap() + */ +SSDFS_LOG_FOOTER_FNS(BLK_BMAP_BIT, log_footer_has_blk_bmap) + +/* + * ssdfs_set_log_footer_has_offset_table() + * ssdfs_clear_log_footer_has_offset_table() + * ssdfs_log_footer_has_offset_table() + */ +SSDFS_LOG_FOOTER_FNS(OFFSET_TABLE_BIT, log_footer_has_offset_table) + +/* + * ssdfs_set_partial_log_footer() + * ssdfs_clear_partial_log_footer() + * ssdfs_partial_log_footer() + */ +SSDFS_LOG_FOOTER_FNS(PARTIAL_LOG_BIT, partial_log_footer) + +/* + * ssdfs_set_ending_log_footer() + * ssdfs_clear_ending_log_footer() + * ssdfs_ending_log_footer() + */ +SSDFS_LOG_FOOTER_FNS(ENDING_LOG_BIT, ending_log_footer) + +/* + * ssdfs_set_log_footer_has_snapshot_rules() + * ssdfs_clear_log_footer_has_snapshot_rules() + * ssdfs_log_footer_has_snapshot_rules() + */ +SSDFS_LOG_FOOTER_FNS(SNAPSHOT_RULE_AREA_BIT, log_footer_has_snapshot_rules) + +/* + * struct ssdfs_partial_log_header - header of partial log + * @magic: magic signature + revision + * @check: metadata checksum + * @timestamp: writing timestamp + * @cno: writing checkpoint + * @log_pages: size of log in pages count + * @seg_type: type of segment + * @pl_flags: flags of log + * @log_bytes: payload size in bytes + * @flags: volume flags + * @desc_array: array of log's metadata descriptors + * @nsegs: segments count + * @free_pages: free pages count + * @root_folder: copy of root folder's inode + * @inodes_btree: inodes btree root + * @shared_extents_btree: shared extents btree root + * @shared_dict_btree: shared dictionary btree root + * @sequence_id: index of partial log in the sequence + * @log_pagesize: log2(page size) + * @log_erasesize: log2(erase block size) + * @log_segsize: log2(segment size) + * @log_pebs_per_seg: log2(erase blocks per segment) + * @lebs_per_peb_index: difference of LEB IDs between PEB indexes in segment + * @create_threads_per_seg: number of creation threads per segment + * @snapshots_btree: snapshots btree root + * @open_zones: number of open/active zones + * @peb_create_time: PEB creation timestamp + * @invextree: invalidated extents btree root + * @seg_id: segment ID that contains this PEB + * @leb_id: LEB ID that mapped with this PEB + * @peb_id: PEB ID + * @relation_peb_id: source PEB ID during migration + * @uuid: 128-bit uuid for volume + * @volume_create_time: volume create timestamp (mkfs phase) + * + * This header is used when the full log needs to be built from several + * partial logs. The header represents the combination of the most + * essential fields of segment header and log footer. The first partial + * log starts from the segment header and partial log header. The next + * every partial log starts from the partial log header. Only the latest + * log ends with the log footer. + */ +struct ssdfs_partial_log_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + struct ssdfs_metadata_check check; + +/* 0x0010 */ + __le64 timestamp; + __le64 cno; + +/* 0x0020 */ + __le16 log_pages; + __le16 seg_type; + __le32 pl_flags; + +/* 0x0028 */ + __le32 log_bytes; + __le32 flags; + +/* 0x0030 */ + struct ssdfs_metadata_descriptor desc_array[SSDFS_SEG_HDR_DESC_MAX]; + +/* 0x00C0 */ + __le64 nsegs; + __le64 free_pages; + +/* 0x00D0 */ + struct ssdfs_inode root_folder; + +/* 0x01D0 */ + struct ssdfs_inodes_btree inodes_btree; + +/* 0x0250 */ + struct ssdfs_shared_extents_btree shared_extents_btree; + +/* 0x02D0 */ + struct ssdfs_shared_dictionary_btree shared_dict_btree; + +/* 0x0350 */ + __le32 sequence_id; + __le8 log_pagesize; + __le8 log_erasesize; + __le8 log_segsize; + __le8 log_pebs_per_seg; + __le32 lebs_per_peb_index; + __le16 create_threads_per_seg; + __le8 reserved1[0x2]; + +/* 0x0360 */ + struct ssdfs_snapshots_btree snapshots_btree; + +/* 0x03E0 */ + __le32 open_zones; + __le8 reserved2[0x4]; + __le64 peb_create_time; + __le8 reserved3[0x10]; + +/* 0x0400 */ + struct ssdfs_invalidated_extents_btree invextree; + +/* 0x0480 */ + __le64 seg_id; + __le64 leb_id; + __le64 peb_id; + __le64 relation_peb_id; + +/* 0x04A0 */ + __le8 uuid[SSDFS_UUID_SIZE]; + +/* 0x04B0 */ + __le64 volume_create_time; + +/* 0x04B8 */ + __le8 payload[0x348]; + +/* 0x0800 */ +} __packed; + +/* Partial log flags manipulation functions */ +#define SSDFS_PL_HDR_FNS(bit, name) \ +static inline void ssdfs_set_##name(struct ssdfs_partial_log_header *hdr) \ +{ \ + unsigned long pl_flags = le32_to_cpu(hdr->pl_flags); \ + set_bit(SSDFS_##bit, &pl_flags); \ + hdr->pl_flags = cpu_to_le32((u32)pl_flags); \ +} \ +static inline void ssdfs_clear_##name(struct ssdfs_partial_log_header *hdr) \ +{ \ + unsigned long pl_flags = le32_to_cpu(hdr->pl_flags); \ + clear_bit(SSDFS_##bit, &pl_flags); \ + hdr->pl_flags = cpu_to_le32((u32)pl_flags); \ +} \ +static inline int ssdfs_##name(struct ssdfs_partial_log_header *hdr) \ +{ \ + unsigned long pl_flags = le32_to_cpu(hdr->pl_flags); \ + return test_bit(SSDFS_##bit, &pl_flags); \ +} + +/* + * ssdfs_set_pl_hdr_has_blk_bmap() + * ssdfs_clear_pl_hdr_has_blk_bmap() + * ssdfs_pl_hdr_has_blk_bmap() + */ +SSDFS_PL_HDR_FNS(BLK_BMAP_BIT, pl_hdr_has_blk_bmap) + +/* + * ssdfs_set_pl_hdr_has_offset_table() + * ssdfs_clear_pl_hdr_has_offset_table() + * ssdfs_pl_hdr_has_offset_table() + */ +SSDFS_PL_HDR_FNS(OFFSET_TABLE_BIT, pl_hdr_has_offset_table) + +/* + * ssdfs_set_pl_has_cold_payload() + * ssdfs_clear_pl_has_cold_payload() + * ssdfs_pl_has_cold_payload() + */ +SSDFS_PL_HDR_FNS(COLD_PAYLOAD_BIT, pl_has_cold_payload) + +/* + * ssdfs_set_pl_has_warm_payload() + * ssdfs_clear_pl_has_warm_payload() + * ssdfs_pl_has_warm_payload() + */ +SSDFS_PL_HDR_FNS(WARM_PAYLOAD_BIT, pl_has_warm_payload) + +/* + * ssdfs_set_pl_has_hot_payload() + * ssdfs_clear_pl_has_hot_payload() + * ssdfs_pl_has_hot_payload() + */ +SSDFS_PL_HDR_FNS(HOT_PAYLOAD_BIT, pl_has_hot_payload) + +/* + * ssdfs_set_pl_has_blk_desc_chain() + * ssdfs_clear_pl_has_blk_desc_chain() + * ssdfs_pl_has_blk_desc_chain() + */ +SSDFS_PL_HDR_FNS(BLK_DESC_CHAIN_BIT, pl_has_blk_desc_chain) + +/* + * ssdfs_set_pl_has_maptbl_cache() + * ssdfs_clear_pl_has_maptbl_cache() + * ssdfs_pl_has_maptbl_cache() + */ +SSDFS_PL_HDR_FNS(MAPTBL_CACHE_BIT, pl_has_maptbl_cache) + +/* + * ssdfs_set_pl_has_footer() + * ssdfs_clear_pl_has_footer() + * ssdfs_pl_has_footer() + */ +SSDFS_PL_HDR_FNS(FOOTER_BIT, pl_has_footer) + +/* + * ssdfs_set_pl_is_partial() + * ssdfs_clear_pl_is_partial() + * ssdfs_pl_is_partial() + */ +SSDFS_PL_HDR_FNS(PARTIAL_LOG_BIT, pl_is_partial) + +/* + * ssdfs_set_pl_has_partial_header() + * ssdfs_clear_pl_has_partial_header() + * ssdfs_pl_has_partial_header() + */ +SSDFS_PL_HDR_FNS(PARTIAL_LOG_HEADER_BIT, pl_has_partial_header) + +/* + * ssdfs_set_pl_header_instead_footer() + * ssdfs_clear_pl_header_instead_footer() + * ssdfs_pl_header_instead_footer() + */ +SSDFS_PL_HDR_FNS(PLH_INSTEAD_FOOTER_BIT, pl_header_instead_footer) + +/* + * struct ssdfs_diff_blob_header - diff blob header + * @magic: diff blob's magic + * @type: diff blob's type + * @desc_size: size of diff blob's descriptor in bytes + * @blob_size: size of diff blob in bytes + * @flags: diff blob's flags + */ +struct ssdfs_diff_blob_header { +/* 0x0000 */ + __le16 magic; + __le8 type; + __le8 desc_size; + __le16 blob_size; + __le16 flags; + +/* 0x0008 */ +} __packed; + +/* Diff blob flags */ +#define SSDFS_DIFF_BLOB_HAS_BTREE_NODE_HEADER (1 << 0) +#define SSDFS_DIFF_CHAIN_CONTAINS_NEXT_BLOB (1 << 1) +#define SSDFS_DIFF_BLOB_FLAGS_MASK (0x3) + +/* + * struct ssdfs_metadata_diff_blob_header - metadata diff blob header + * @diff: generic diff blob header + * @bits_count: count of bits in bitmap + * @item_start_bit: item starting bit in bitmap + * @index_start_bit: index starting bit in bitmap + * @item_size: size of item in bytes + */ +struct ssdfs_metadata_diff_blob_header { +/* 0x0000 */ + struct ssdfs_diff_blob_header diff; + +/* 0x0008 */ + __le16 bits_count; + __le16 item_start_bit; + __le16 index_start_bit; + __le16 item_size; + +/* 0x0010 */ +} __packed; + +/* Diff blob types */ +enum { + SSDFS_UNKNOWN_DIFF_BLOB_TYPE, + SSDFS_BTREE_NODE_DIFF_BLOB, + SSDFS_USER_DATA_DIFF_BLOB, + SSDFS_DIFF_BLOB_TYPE_MAX +}; + +/* + * struct ssdfs_fragments_chain_header - header of fragments' chain + * @compr_bytes: size of the whole fragments' chain in compressed state + * @uncompr_bytes: size of the whole fragments' chain in decompressed state + * @fragments_count: count of fragments in the chain + * @desc_size: size of one descriptor item + * @magic: fragments chain header magic + * @type: fragments chain header type + * @flags: flags of fragments' chain + */ +struct ssdfs_fragments_chain_header { +/* 0x0000 */ + __le32 compr_bytes; + __le32 uncompr_bytes; + +/* 0x0008 */ + __le16 fragments_count; + __le16 desc_size; + +/* 0x000C */ + __le8 magic; + __le8 type; + __le16 flags; + +/* 0x0010 */ +} __packed; + +/* Fragments chain types */ +#define SSDFS_UNKNOWN_CHAIN_HDR 0x0 +#define SSDFS_LOG_AREA_CHAIN_HDR 0x1 +#define SSDFS_BLK_STATE_CHAIN_HDR 0x2 +#define SSDFS_BLK_DESC_CHAIN_HDR 0x3 +#define SSDFS_BLK_DESC_ZLIB_CHAIN_HDR 0x4 +#define SSDFS_BLK_DESC_LZO_CHAIN_HDR 0x5 +#define SSDFS_BLK2OFF_CHAIN_HDR 0x6 +#define SSDFS_BLK2OFF_ZLIB_CHAIN_HDR 0x7 +#define SSDFS_BLK2OFF_LZO_CHAIN_HDR 0x8 +#define SSDFS_BLK_BMAP_CHAIN_HDR 0x9 +#define SSDFS_CHAIN_HDR_TYPE_MAX (SSDFS_BLK_BMAP_CHAIN_HDR + 1) + +/* Fragments chain flags */ +#define SSDFS_MULTIPLE_HDR_CHAIN (1 << 0) +#define SSDFS_CHAIN_HDR_FLAG_MASK 0x1 + +/* Fragments chain constants */ +#define SSDFS_FRAGMENTS_CHAIN_MAX 14 +#define SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX 64 + +/* + * struct ssdfs_fragment_desc - fragment descriptor + * @offset: fragment's offset + * @compr_size: size of fragment in compressed state + * @uncompr_size: size of fragment after decompression + * @checksum: fragment checksum + * @sequence_id: fragment's sequential id number + * @magic: fragment descriptor's magic + * @type: fragment descriptor's type + * @flags: fragment descriptor's flags + */ +struct ssdfs_fragment_desc { +/* 0x0000 */ + __le32 offset; + __le16 compr_size; + __le16 uncompr_size; + +/* 0x0008 */ + __le32 checksum; + __le8 sequence_id; + __le8 magic; + __le8 type; + __le8 flags; + +/* 0x0010 */ +} __packed; + +/* Fragment descriptor types */ +#define SSDFS_UNKNOWN_FRAGMENT_TYPE 0 +#define SSDFS_FRAGMENT_UNCOMPR_BLOB 1 +#define SSDFS_FRAGMENT_ZLIB_BLOB 2 +#define SSDFS_FRAGMENT_LZO_BLOB 3 +#define SSDFS_DATA_BLK_STATE_DESC 4 +#define SSDFS_DATA_BLK_DESC 5 +#define SSDFS_DATA_BLK_DESC_ZLIB 6 +#define SSDFS_DATA_BLK_DESC_LZO 7 +#define SSDFS_BLK2OFF_EXTENT_DESC 8 +#define SSDFS_BLK2OFF_EXTENT_DESC_ZLIB 9 +#define SSDFS_BLK2OFF_EXTENT_DESC_LZO 10 +#define SSDFS_BLK2OFF_DESC 11 +#define SSDFS_BLK2OFF_DESC_ZLIB 12 +#define SSDFS_BLK2OFF_DESC_LZO 13 +#define SSDFS_NEXT_TABLE_DESC 14 +#define SSDFS_FRAGMENT_DESC_MAX_TYPE (SSDFS_NEXT_TABLE_DESC + 1) + +/* Fragment descriptor flags */ +#define SSDFS_FRAGMENT_HAS_CSUM (1 << 0) +#define SSDFS_FRAGMENT_DESC_FLAGS_MASK 0x1 + +/* + * struct ssdfs_block_bitmap_header - header of segment's block bitmap + * @magic: magic signature and flags + * @fragments_count: count of block bitmap's fragments + * @bytes_count: count of bytes in fragments' sequence + * @flags: block bitmap's flags + * @type: type of block bitmap + */ +struct ssdfs_block_bitmap_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + __le16 fragments_count; + __le32 bytes_count; + +#define SSDFS_BLK_BMAP_BACKUP (1 << 0) +#define SSDFS_BLK_BMAP_COMPRESSED (1 << 1) +#define SSDFS_BLK_BMAP_FLAG_MASK 0x3 + __le8 flags; + +#define SSDFS_BLK_BMAP_UNCOMPRESSED_BLOB (0) +#define SSDFS_BLK_BMAP_ZLIB_BLOB (1) +#define SSDFS_BLK_BMAP_LZO_BLOB (2) +#define SSDFS_BLK_BMAP_TYPE_MAX (SSDFS_BLK_BMAP_LZO_BLOB + 1) + __le8 type; + +/* 0x0010 */ +} __packed; + +/* + * struct ssdfs_block_bitmap_fragment - block bitmap's fragment header + * @peb_index: PEB's index + * @sequence_id: ID of block bitmap's fragment in the sequence + * @flags: fragment's flags + * @type: fragment type + * @last_free_blk: last logical free block + * @metadata_blks: count of physical pages are used by metadata + * @invalid_blks: count of invalid blocks + * @chain_hdr: descriptor of block bitmap's fragments' chain + */ +struct ssdfs_block_bitmap_fragment { +/* 0x0000 */ + __le16 peb_index; + __le8 sequence_id; + +#define SSDFS_MIGRATING_BLK_BMAP (1 << 0) +#define SSDFS_PEB_HAS_EXT_PTR (1 << 1) +#define SSDFS_PEB_HAS_RELATION (1 << 2) +#define SSDFS_INFLATED_BLK_BMAP (1 << 3) +#define SSDFS_FRAG_BLK_BMAP_FLAG_MASK 0xF + __le8 flags : 6; + +#define SSDFS_SRC_BLK_BMAP (0) +#define SSDFS_DST_BLK_BMAP (1) +#define SSDFS_FRAG_BLK_BMAP_TYPE_MAX (SSDFS_DST_BLK_BMAP + 1) + __le8 type : 2; + + __le32 last_free_blk; + +/* 0x0008 */ + __le32 metadata_blks; + __le32 invalid_blks; + +/* 0x0010 */ + struct ssdfs_fragments_chain_header chain_hdr; + +/* 0x0020 */ +} __packed; + +/* + * The block to offset table has structure: + * + * ---------------------------- + * | | + * | Blk2Off table Header | + * | | + * ---------------------------- + * | | + * | Translation extents | + * | sequence | + * | | + * ---------------------------- + * | | + * | Physical offsets table | + * | header | + * | | + * ---------------------------- + * | | + * | Physical offset | + * | descriptors sequence | + * | | + * ---------------------------- + */ + +/* Possible log's area types */ +enum { + SSDFS_LOG_BLK_DESC_AREA, + SSDFS_LOG_MAIN_AREA, + SSDFS_LOG_DIFFS_AREA, + SSDFS_LOG_JOURNAL_AREA, + SSDFS_LOG_AREA_MAX, +}; + +/* + * struct ssdfs_peb_page_descriptor - PEB's page descriptor + * @logical_offset: logical offset from file's begin in pages + * @logical_blk: logical number of the block in segment + * @peb_page: PEB's page index + */ +struct ssdfs_peb_page_descriptor { +/* 0x0000 */ + __le32 logical_offset; + __le16 logical_blk; + __le16 peb_page; + +/* 0x0008 */ +} __packed; + +/* + * struct ssdfs_blk_state_offset - block's state offset + * @log_start_page: start page of the log + * @log_area: identification number of log area + * @peb_migration_id: identification number of PEB in migration sequence + * @byte_offset: offset in bytes from area's beginning + */ +struct ssdfs_blk_state_offset { +/* 0x0000 */ + __le16 log_start_page; + __le8 log_area; + __le8 peb_migration_id; + __le32 byte_offset; + +/* 0x0008 */ +} __packed; + +/* + * struct ssdfs_phys_offset_descriptor - descriptor of physical offset + * @page_desc: PEB's page descriptor + * @blk_state: logical block's state offset + */ +struct ssdfs_phys_offset_descriptor { +/* 0x0000 */ + struct ssdfs_peb_page_descriptor page_desc; + struct ssdfs_blk_state_offset blk_state; + +/* 0x0010 */ +} __packed; + +/* + * struct ssdfs_phys_offset_table_header - physical offset table header + * @start_id: start id in the table's fragment + * @id_count: number of unique physical offsets in log's fragments chain + * @byte_size: size in bytes of table's fragment + * @peb_index: PEB index + * @sequence_id: table's fragment's sequential id number + * @type: table's type + * @flags: table's flags + * @magic: table's magic + * @checksum: table checksum + * @used_logical_blks: count of allocated logical blocks + * @free_logical_blks: count of free logical blocks + * @last_allocated_blk: last allocated block (hint for allocation) + * @next_fragment_off: offset till next table's fragment + * + * This table contains offsets of block descriptors in a segment. + * Generally speaking, table can be represented as array of + * ssdfs_phys_offset_descriptor structures are ordered by id + * numbers. The whole table can be split on several fragments. + * Every table's fragment begins from header. + */ +struct ssdfs_phys_offset_table_header { +/* 0x0000 */ + __le16 start_id; + __le16 id_count; + __le32 byte_size; + +/* 0x0008 */ + __le16 peb_index; + __le16 sequence_id; + __le16 type; + __le16 flags; + +/* 0x0010 */ + __le32 magic; + __le32 checksum; + +/* 0x0018 */ + __le16 used_logical_blks; + __le16 free_logical_blks; + __le16 last_allocated_blk; + __le16 next_fragment_off; + +/* 0x0020 */ +} __packed; + +/* Physical offset table types */ +#define SSDFS_UNKNOWN_OFF_TABLE_TYPE 0 +#define SSDFS_SEG_OFF_TABLE 1 +#define SSDFS_OFF_TABLE_MAX_TYPE (SSDFS_SEG_OFF_TABLE + 1) + +/* Physical offset table flags */ +#define SSDFS_OFF_TABLE_HAS_CSUM (1 << 0) +#define SSDFS_OFF_TABLE_HAS_NEXT_FRAGMENT (1 << 1) +#define SSDFS_BLK_DESC_TBL_COMPRESSED (1 << 2) +#define SSDFS_OFF_TABLE_HAS_OLD_LOG_FRAGMENT (1 << 3) +#define SSDFS_INFLATED_OFF_TABLE (1 << 4) +#define SSDFS_OFF_TABLE_FLAGS_MASK 0x1F + +/* + * struct ssdfs_translation_extent - logical block to offset id translation + * @logical_blk: starting logical block + * @offset_id: starting offset id + * @len: count of items in extent + * @sequence_id: id in sequence of extents + * @state: logical blocks' sequence state + */ +struct ssdfs_translation_extent { +/* 0x0000 */ + __le16 logical_blk; +#define SSDFS_INVALID_OFFSET_ID (U16_MAX) + __le16 offset_id; + __le16 len; + __le8 sequence_id; + __le8 state; + +/* 0x0008 */ +} __packed; + +enum { + SSDFS_LOGICAL_BLK_UNKNOWN_STATE, + SSDFS_LOGICAL_BLK_FREE, + SSDFS_LOGICAL_BLK_USED, + SSDFS_LOGICAL_BLK_STATE_MAX, +}; + +/* + * struct ssdfs_blk2off_table_header - translation table header + * @magic: magic signature + * @check: metadata checksum + flags + * @extents_off: offset in bytes from header begin till extents sequence + * @extents_count: count of extents in the sequence + * @offset_table_off: offset in bytes from header begin till phys offsets table + * @fragments_count: count of table's fragments for the whole PEB + * @sequence: first translation extent in the sequence + */ +struct ssdfs_blk2off_table_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ +#define SSDFS_BLK2OFF_TBL_ZLIB_COMPR (1 << 1) +#define SSDFS_BLK2OFF_TBL_LZO_COMPR (1 << 2) + struct ssdfs_metadata_check check; + +/* 0x0010 */ + struct ssdfs_fragments_chain_header chain_hdr; + +/* 0x0020 */ +#define SSDFS_BLK2OFF_FRAG_CHAIN_MAX (5) +#define SSDFS_NEXT_BLK2OFF_TBL_INDEX SSDFS_BLK2OFF_FRAG_CHAIN_MAX +#define SSDFS_BLK2OFF_TBL_MAX (SSDFS_BLK2OFF_FRAG_CHAIN_MAX + 1) + struct ssdfs_fragment_desc blk[SSDFS_BLK2OFF_TBL_MAX]; + +/* 0x0080 */ +} __packed; + +/* + * The block's descriptor table has structure: + * + * ---------------------------- + * | | + * | Area block table #0 | + * | Fragment descriptor #0 | + * | *** | + * | Fragment descriptor #14 | + * | Next area block table | + * | descriptor | + * | | + * ---------------------------- + * | | + * | Block descriptor #0 | + * | *** | + * | Block descriptor #N | + * | | + * ---------------------------- + * | | + * | *** | + * | | + * ---------------------------- + * | | + * | Block descriptor #0 | + * | *** | + * | Block descriptor #N | + * | | + * ---------------------------- + * | | + * | *** | + * | | + * ---------------------------- + * | | + * | Area block table #N | + * | Fragment descriptor #0 | + * | *** | + * | Fragment descriptor #14 | + * | Next area block table | + * | descriptor | + * | | + * ---------------------------- + * | | + * | Block descriptor #0 | + * | *** | + * | Block descriptor #N | + * | | + * ---------------------------- + * | | + * | *** | + * | | + * ---------------------------- + * | | + * | Block descriptor #0 | + * | *** | + * | Block descriptor #N | + * | | + * ---------------------------- + */ + +#define SSDFS_BLK_STATE_OFF_MAX 6 + +/* + * struct ssdfs_block_descriptor - block descriptor + * @ino: inode identification number + * @logical_offset: logical offset from file's begin in pages + * @peb_index: PEB's index + * @peb_page: PEB's page index + * @state: array of fragment's offsets + */ +struct ssdfs_block_descriptor { +/* 0x0000 */ + __le64 ino; + __le32 logical_offset; + __le16 peb_index; + __le16 peb_page; + +/* 0x0010 */ + struct ssdfs_blk_state_offset state[SSDFS_BLK_STATE_OFF_MAX]; + +/* 0x0040 */ +} __packed; + +/* + * struct ssdfs_area_block_table - descriptor of block state sequence in area + * @chain_hdr: descriptor of block states' chain + * @blk: table of fragment descriptors + * + * This table describes block state sequence in PEB's area. This table + * can consists from several parts. Every part can describe 14 blocks + * in partial sequence. If sequence contains more block descriptors + * then last fragment descriptor describes placement of next part of + * block table and so on. + */ +struct ssdfs_area_block_table { +/* 0x0000 */ + struct ssdfs_fragments_chain_header chain_hdr; + +/* 0x0010 */ +#define SSDFS_NEXT_BLK_TABLE_INDEX SSDFS_FRAGMENTS_CHAIN_MAX +#define SSDFS_BLK_TABLE_MAX (SSDFS_FRAGMENTS_CHAIN_MAX + 1) + struct ssdfs_fragment_desc blk[SSDFS_BLK_TABLE_MAX]; + +/* 0x0100 */ +} __packed; + +/* + * The data (diff, journaling) area has structure: + * ----------------------------- + * | | + * | Block state descriptor #0 | + * | Fragment descriptor #0 | + * | *** | + * | Fragment descriptor #N | + * | | + * ----------------------------- + * | | + * | Data portion #0 | + * | *** | + * | Data portion #N | + * | | + * ----------------------------- + * | | + * | *** | + * | | + * ----------------------------- + * | | + * | Block state descriptor #N | + * | Fragment descriptor #0 | + * | *** | + * | Fragment descriptor #N | + * | | + * ----------------------------- + * | | + * | Data portion #0 | + * | *** | + * | Data portion #N | + * | | + * ----------------------------- + */ + +/* + * ssdfs_block_state_descriptor - block's state descriptor + * @cno: checkpoint + * @parent_snapshot: parent snapshot + * @chain_hdr: descriptor of data fragments' chain + */ +struct ssdfs_block_state_descriptor { +/* 0x0000 */ + __le64 cno; + __le64 parent_snapshot; + +/* 0x0010 */ + struct ssdfs_fragments_chain_header chain_hdr; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_segbmap_fragment_header - segment bitmap fragment header + * @magic: magic signature + * @seg_index: segment index in segment bitmap fragments' chain + * @peb_index: PEB's index in segment + * @flags: fragment's flags + * @seg_type: segment type (main/backup) + * @start_item: fragment's start item number + * @sequence_id: fragment identification number + * @fragment_bytes: bytes count in fragment + * @checksum: fragment checksum + * @total_segs: count of total segments in fragment + * @clean_or_using_segs: count of clean or using segments in fragment + * @used_or_dirty_segs: count of used or dirty segments in fragment + * @bad_segs: count of bad segments in fragment + */ +struct ssdfs_segbmap_fragment_header { +/* 0x0000 */ + __le16 magic; + __le16 seg_index; + __le16 peb_index; +#define SSDFS_SEGBMAP_FRAG_ZLIB_COMPR (1 << 0) +#define SSDFS_SEGBMAP_FRAG_LZO_COMPR (1 << 1) + __le8 flags; + __le8 seg_type; + +/* 0x0008 */ + __le64 start_item; + +/* 0x0010 */ + __le16 sequence_id; + __le16 fragment_bytes; + __le32 checksum; + +/* 0x0018 */ + __le16 total_segs; + __le16 clean_or_using_segs; + __le16 used_or_dirty_segs; + __le16 bad_segs; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_peb_descriptor - descriptor of PEB + * @erase_cycles: count of P/E cycles of PEB + * @type: PEB's type + * @state: PEB's state + * @flags: PEB's flags + * @shared_peb_index: index of external shared destination PEB + */ +struct ssdfs_peb_descriptor { +/* 0x0000 */ + __le32 erase_cycles; + __le8 type; + __le8 state; + __le8 flags; + __le8 shared_peb_index; + +/* 0x0008 */ +} __packed; + +/* PEB's types */ +enum { + SSDFS_MAPTBL_UNKNOWN_PEB_TYPE, /* 0x00 */ + SSDFS_MAPTBL_DATA_PEB_TYPE, /* 0x01 */ + SSDFS_MAPTBL_LNODE_PEB_TYPE, /* 0x02 */ + SSDFS_MAPTBL_HNODE_PEB_TYPE, /* 0x03 */ + SSDFS_MAPTBL_IDXNODE_PEB_TYPE, /* 0x04 */ + SSDFS_MAPTBL_INIT_SNAP_PEB_TYPE, /* 0x05 */ + SSDFS_MAPTBL_SBSEG_PEB_TYPE, /* 0x06 */ + SSDFS_MAPTBL_SEGBMAP_PEB_TYPE, /* 0x07 */ + SSDFS_MAPTBL_MAPTBL_PEB_TYPE, /* 0x08 */ + SSDFS_MAPTBL_PEB_TYPE_MAX /* 0x09 */ +}; + +/* PEB's states */ +enum { + SSDFS_MAPTBL_UNKNOWN_PEB_STATE, /* 0x00 */ + SSDFS_MAPTBL_BAD_PEB_STATE, /* 0x01 */ + SSDFS_MAPTBL_CLEAN_PEB_STATE, /* 0x02 */ + SSDFS_MAPTBL_USING_PEB_STATE, /* 0x03 */ + SSDFS_MAPTBL_USED_PEB_STATE, /* 0x04 */ + SSDFS_MAPTBL_PRE_DIRTY_PEB_STATE, /* 0x05 */ + SSDFS_MAPTBL_DIRTY_PEB_STATE, /* 0x06 */ + SSDFS_MAPTBL_MIGRATION_SRC_USING_STATE, /* 0x07 */ + SSDFS_MAPTBL_MIGRATION_SRC_USED_STATE, /* 0x08 */ + SSDFS_MAPTBL_MIGRATION_SRC_PRE_DIRTY_STATE, /* 0x09 */ + SSDFS_MAPTBL_MIGRATION_SRC_DIRTY_STATE, /* 0x0A */ + SSDFS_MAPTBL_MIGRATION_DST_CLEAN_STATE, /* 0x0B */ + SSDFS_MAPTBL_MIGRATION_DST_USING_STATE, /* 0x0C */ + SSDFS_MAPTBL_MIGRATION_DST_USED_STATE, /* 0x0D */ + SSDFS_MAPTBL_MIGRATION_DST_PRE_DIRTY_STATE, /* 0x0E */ + SSDFS_MAPTBL_MIGRATION_DST_DIRTY_STATE, /* 0x0F */ + SSDFS_MAPTBL_PRE_ERASE_STATE, /* 0x10 */ + SSDFS_MAPTBL_UNDER_ERASE_STATE, /* 0x11 */ + SSDFS_MAPTBL_SNAPSHOT_STATE, /* 0x12 */ + SSDFS_MAPTBL_RECOVERING_STATE, /* 0x13 */ + SSDFS_MAPTBL_USING_INVALIDATED_PEB_STATE, /* 0x14 */ + SSDFS_MAPTBL_MIGRATION_SRC_USING_INVALIDATED_STATE, /* 0x15 */ + SSDFS_MAPTBL_MIGRATION_DST_USING_INVALIDATED_STATE, /* 0x16 */ + SSDFS_MAPTBL_PEB_STATE_MAX /* 0x17 */ +}; + +/* PEB's flags */ +#define SSDFS_MAPTBL_SHARED_DESTINATION_PEB (1 << 0) +#define SSDFS_MAPTBL_SOURCE_PEB_HAS_EXT_PTR (1 << 1) +#define SSDFS_MAPTBL_SOURCE_PEB_HAS_ZONE_PTR (1 << 2) + +#define SSDFS_PEBTBL_BMAP_SIZE \ + ((PAGE_SIZE / sizeof(struct ssdfs_peb_descriptor)) / \ + BITS_PER_BYTE) + +/* PEB table's bitmap types */ +enum { + SSDFS_PEBTBL_USED_BMAP, + SSDFS_PEBTBL_DIRTY_BMAP, + SSDFS_PEBTBL_RECOVER_BMAP, + SSDFS_PEBTBL_BADBLK_BMAP, + SSDFS_PEBTBL_BMAP_MAX +}; + +/* + * struct ssdfs_peb_table_fragment_header - header of PEB table fragment + * @magic: signature of PEB table's fragment + * @flags: flags of PEB table's fragment + * @recover_months: recovering duration in months + * @recover_threshold: recover threshold + * @checksum: checksum of PEB table's fragment + * @start_peb: starting PEB number + * @pebs_count: count of PEB's descriptors in table's fragment + * @last_selected_peb: index of last selected unused PEB + * @reserved_pebs: count of reserved PEBs in table's fragment + * @stripe_id: stripe identification number + * @portion_id: sequential ID of mapping table fragment + * @fragment_id: sequential ID of PEB table fragment in the portion + * @bytes_count: table's fragment size in bytes + * @bmap: PEB table fragment's bitmap + */ +struct ssdfs_peb_table_fragment_header { +/* 0x0000 */ + __le16 magic; + __le8 flags; + __le8 recover_months : 4; + __le8 recover_threshold : 4; + __le32 checksum; + +/* 0x0008 */ + __le64 start_peb; + +/* 0x0010 */ + __le16 pebs_count; + __le16 last_selected_peb; + __le16 reserved_pebs; + __le16 stripe_id; + +/* 0x0018 */ + __le16 portion_id; + __le16 fragment_id; + __le32 bytes_count; + +/* 0x0020 */ + __le8 bmaps[SSDFS_PEBTBL_BMAP_MAX][SSDFS_PEBTBL_BMAP_SIZE]; + +/* 0x0120 */ +} __packed; + +/* PEB table fragment's flags */ +#define SSDFS_PEBTBL_FRAG_ZLIB_COMPR (1 << 0) +#define SSDFS_PEBTBL_FRAG_LZO_COMPR (1 << 1) +#define SSDFS_PEBTBL_UNDER_RECOVERING (1 << 2) +#define SSDFS_PEBTBL_BADBLK_EXIST (1 << 3) +#define SSDFS_PEBTBL_TRY_CORRECT_PEBS_AGAIN (1 << 4) +#define SSDFS_PEBTBL_FIND_RECOVERING_PEBS \ + (SSDFS_PEBTBL_UNDER_RECOVERING | SSDFS_PEBTBL_BADBLK_EXIST) +#define SSDFS_PEBTBL_FLAGS_MASK 0x1F + +/* PEB table recover thresholds */ +#define SSDFS_PEBTBL_FIRST_RECOVER_TRY (0) +#define SSDFS_PEBTBL_SECOND_RECOVER_TRY (1) +#define SSDFS_PEBTBL_THIRD_RECOVER_TRY (2) +#define SSDFS_PEBTBL_FOURTH_RECOVER_TRY (3) +#define SSDFS_PEBTBL_FIFTH_RECOVER_TRY (4) +#define SSDFS_PEBTBL_SIX_RECOVER_TRY (5) +#define SSDFS_PEBTBL_BADBLK_THRESHOLD (6) + +#define SSDFS_PEBTBL_FRAGMENT_HDR_SIZE \ + (sizeof(struct ssdfs_peb_table_fragment_header)) + +#define SSDFS_PEB_DESC_PER_FRAGMENT(fragment_size) \ + ((fragment_size - SSDFS_PEBTBL_FRAGMENT_HDR_SIZE) / \ + sizeof(struct ssdfs_peb_descriptor)) + +/* + * struct ssdfs_leb_descriptor - logical descriptor of erase block + * @physical_index: PEB table's offset till PEB's descriptor + * @relation_index: PEB table's offset till associated PEB's descriptor + */ +struct ssdfs_leb_descriptor { +/* 0x0000 */ + __le16 physical_index; + __le16 relation_index; + +/* 0x0004 */ +} __packed; + +/* + * struct ssdfs_leb_table_fragment_header - header of LEB table fragment + * @magic: signature of LEB table's fragment + * @flags: flags of LEB table's fragment + * @checksum: checksum of LEB table's fragment + * @start_leb: starting LEB number + * @lebs_count: count of LEB's descriptors in table's fragment + * @mapped_lebs: count of LEBs are mapped on PEBs + * @migrating_lebs: count of LEBs under migration + * @portion_id: sequential ID of mapping table fragment + * @fragment_id: sequential ID of LEB table fragment in the portion + * @bytes_count: table's fragment size in bytes + */ +struct ssdfs_leb_table_fragment_header { +/* 0x0000 */ + __le16 magic; +#define SSDFS_LEBTBL_FRAG_ZLIB_COMPR (1 << 0) +#define SSDFS_LEBTBL_FRAG_LZO_COMPR (1 << 1) + __le16 flags; + __le32 checksum; + +/* 0x0008 */ + __le64 start_leb; + +/* 0x0010 */ + __le16 lebs_count; + __le16 mapped_lebs; + __le16 migrating_lebs; + __le16 reserved1; + +/* 0x0018 */ + __le16 portion_id; + __le16 fragment_id; + __le32 bytes_count; + +/* 0x0020 */ +} __packed; + +#define SSDFS_LEBTBL_FRAGMENT_HDR_SIZE \ + (sizeof(struct ssdfs_leb_table_fragment_header)) + +#define SSDFS_LEB_DESC_PER_FRAGMENT(fragment_size) \ + ((fragment_size - SSDFS_LEBTBL_FRAGMENT_HDR_SIZE) / \ + sizeof(struct ssdfs_leb_descriptor)) + +/* + * The mapping table cache is the copy of content of mapping + * table for some type of PEBs. The goal of cache is to provide + * the space for storing the copy of LEB_ID/PEB_ID pairs with + * PEB state record. The cache is using for conversion LEB ID + * to PEB ID and retrieving the PEB state record in the case + * when the fragment of mapping table is not initialized yet. + * Also the cache needs for storing modified PEB state during + * the mapping table destruction. The fragment of mapping table + * cache has structure: + * + * ---------------------------- + * | | + * | Header | + * | | + * ---------------------------- + * | | + * | LEB_ID/PEB_ID pairs | + * | | + * ---------------------------- + * | | + * | PEB state records | + * | | + * ---------------------------- + */ + +/* + * struct ssdfs_maptbl_cache_header - maptbl cache header + * @magic: magic signature + * @sequence_id: ID of fragment in the sequence + * @flags: maptbl cache header's flags + * @items_count: count of items in maptbl cache's fragment + * @bytes_count: size of fragment in bytes + * @start_leb: start LEB ID in fragment + * @end_leb: ending LEB ID in fragment + */ +struct ssdfs_maptbl_cache_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + __le16 sequence_id; +#define SSDFS_MAPTBL_CACHE_ZLIB_COMPR (1 << 0) +#define SSDFS_MAPTBL_CACHE_LZO_COMPR (1 << 1) + __le16 flags; + __le16 items_count; + __le16 bytes_count; + +/* 0x0010 */ + __le64 start_leb; + __le64 end_leb; + +/* 0x0020 */ +} __packed; + +/* + * struct ssdfs_maptbl_cache_peb_state - PEB state descriptor + * @consistency: PEB state consistency type + * @state: PEB's state + * @flags: PEB's flags + * @shared_peb_index: index of external shared destination PEB + * + * The mapping table cache is the copy of content of mapping + * table for some type of PEBs. If the mapping table cache and + * the mapping table contain the same content for the PEB then + * the PEB state record is consistent. Otherwise, the PEB state + * record is inconsistent. For example, the inconsistency takes + * place if a PEB state record was modified in the mapping table + * cache during the destruction of the mapping table. + */ +struct ssdfs_maptbl_cache_peb_state { +/* 0x0000 */ + __le8 consistency; + __le8 state; + __le8 flags; + __le8 shared_peb_index; + +/* 0x0004 */ +} __packed; + +/* PEB state consistency type */ +enum { + SSDFS_PEB_STATE_UNKNOWN, + SSDFS_PEB_STATE_CONSISTENT, + SSDFS_PEB_STATE_INCONSISTENT, + SSDFS_PEB_STATE_PRE_DELETED, + SSDFS_PEB_STATE_MAX +}; + +#define SSDFS_MAPTBL_CACHE_HDR_SIZE \ + (sizeof(struct ssdfs_maptbl_cache_header)) +#define SSDFS_LEB2PEB_PAIR_SIZE \ + (sizeof(struct ssdfs_leb2peb_pair)) +#define SSDFS_PEB_STATE_SIZE \ + (sizeof(struct ssdfs_maptbl_cache_peb_state)) + +#define SSDFS_LEB2PEB_PAIR_PER_FRAGMENT(fragment_size) \ + ((fragment_size - SSDFS_MAPTBL_CACHE_HDR_SIZE - \ + SSDFS_PEB_STATE_SIZE) / \ + (SSDFS_LEB2PEB_PAIR_SIZE + SSDFS_PEB_STATE_SIZE)) + +/* + * struct ssdfs_btree_node_header - btree's node header + * @magic: magic signature + revision + * @check: metadata checksum + * @height: btree node's height + * @log_node_size: log2(node size) + * @log_index_area_size: log2(index area size) + * @type: btree node type + * @flags: btree node flags + * @index_area_offset: offset of index area in bytes + * @index_count: count of indexes in index area + * @index_size: size of index in bytes + * @min_item_size: min size of item in bytes + * @max_item_size: max possible size of item in bytes + * @items_capacity: capacity of items in the node + * @start_hash: start hash value + * @end_hash: end hash value + * @create_cno: create checkpoint + * @node_id: node identification number + * @item_area_offset: offset of items area in bytes + */ +struct ssdfs_btree_node_header { +/* 0x0000 */ + struct ssdfs_signature magic; + +/* 0x0008 */ + struct ssdfs_metadata_check check; + +/* 0x0010 */ + __le8 height; + __le8 log_node_size; + __le8 log_index_area_size; + __le8 type; + +/* 0x0014 */ +#define SSDFS_BTREE_NODE_HAS_INDEX_AREA (1 << 0) +#define SSDFS_BTREE_NODE_HAS_ITEMS_AREA (1 << 1) +#define SSDFS_BTREE_NODE_HAS_L1TBL (1 << 2) +#define SSDFS_BTREE_NODE_HAS_L2TBL (1 << 3) +#define SSDFS_BTREE_NODE_HAS_HASH_TBL (1 << 4) +#define SSDFS_BTREE_NODE_PRE_ALLOCATED (1 << 5) +#define SSDFS_BTREE_NODE_FLAGS_MASK 0x3F + __le16 flags; + __le16 index_area_offset; + +/* 0x0018 */ + __le16 index_count; + __le8 index_size; + __le8 min_item_size; + __le16 max_item_size; + __le16 items_capacity; + +/* 0x0020 */ + __le64 start_hash; + __le64 end_hash; + +/* 0x0030 */ + __le64 create_cno; + __le32 node_id; + __le32 item_area_offset; + +/* 0x0040 */ +} __packed; + +/* Index of btree node in node's items sequence */ +#define SSDFS_BTREE_NODE_HEADER_INDEX (0) + +/* Btree node types */ +enum { + SSDFS_BTREE_NODE_UNKNOWN_TYPE, + SSDFS_BTREE_ROOT_NODE, + SSDFS_BTREE_INDEX_NODE, + SSDFS_BTREE_HYBRID_NODE, + SSDFS_BTREE_LEAF_NODE, + SSDFS_BTREE_NODE_TYPE_MAX +}; + +#define SSDFS_DENTRIES_PAGES_PER_NODE_MAX (32) +#define SSDFS_DENTRIES_INDEX_BMAP_SIZE \ + ((((SSDFS_DENTRIES_PAGES_PER_NODE_MAX * PAGE_SIZE) / \ + sizeof(struct ssdfs_btree_index_key)) + BITS_PER_LONG) / BITS_PER_BYTE) +#define SSDFS_RAW_DENTRIES_BMAP_SIZE \ + ((((SSDFS_DENTRIES_PAGES_PER_NODE_MAX * PAGE_SIZE) / \ + sizeof(struct ssdfs_dir_entry)) + BITS_PER_LONG) / BITS_PER_BYTE) +#define SSDFS_DENTRIES_BMAP_SIZE \ + (SSDFS_DENTRIES_INDEX_BMAP_SIZE + SSDFS_RAW_DENTRIES_BMAP_SIZE) + +/* + * struct ssdfs_dentries_btree_node_header - directory entries node's header + * @node: generic btree node's header + * @parent_ino: parent inode number + * @dentries_count: count of allocated dentries in the node + * @inline_names: count of dentries with inline names + * @flags: dentries node's flags + * @free_space: free space of the node in bytes + * @lookup_table: table for clustering search in the node + * + * The @lookup_table has goal to provide the way of clustering + * the dentries in the node with the goal to speed-up the search. + */ +struct ssdfs_dentries_btree_node_header { +/* 0x0000 */ + struct ssdfs_btree_node_header node; + +/* 0x0040 */ + __le64 parent_ino; + +/* 0x0048 */ + __le16 dentries_count; + __le16 inline_names; + __le16 flags; + __le16 free_space; + +/* 0x0050 */ +#define SSDFS_DENTRIES_BTREE_LOOKUP_TABLE_SIZE (22) + __le64 lookup_table[SSDFS_DENTRIES_BTREE_LOOKUP_TABLE_SIZE]; + +/* 0x0100 */ +} __packed; + +#define SSDFS_SHARED_DICT_PAGES_PER_NODE_MAX (32) +#define SSDFS_SHARED_DICT_INDEX_BMAP_SIZE \ + ((((SSDFS_SHARED_DICT_PAGES_PER_NODE_MAX * PAGE_SIZE) / \ + sizeof(struct ssdfs_btree_index_key)) + BITS_PER_LONG) / BITS_PER_BYTE) +#define SSDFS_RAW_SHARED_DICT_BMAP_SIZE \ + (((SSDFS_SHARED_DICT_PAGES_PER_NODE_MAX * PAGE_SIZE) / \ + SSDFS_DENTRY_INLINE_NAME_MAX_LEN) / BITS_PER_BYTE) +#define SSDFS_SHARED_DICT_BMAP_SIZE \ + (SSDFS_SHARED_DICT_INDEX_BMAP_SIZE + SSDFS_RAW_SHARED_DICT_BMAP_SIZE) + +/* + * struct ssdfs_shdict_ltbl1_item - shared dictionary lookup table1 item + * @hash_lo: low hash32 value + * @start_index: starting index into lookup table2 + * @range_len: number of items in the range of lookup table2 + * + * The header of shared dictionary node contains the lookup table1. + * This table is responsible for clustering the items in lookup + * table2. The @hash_lo is hash32 of the first part of the name. + * The length of the first part is the inline name length. + */ +struct ssdfs_shdict_ltbl1_item { +/* 0x0000 */ + __le32 hash_lo; + __le16 start_index; + __le16 range_len; + +/* 0x0008 */ +} __packed; + +/* + * struct ssdfs_shdict_ltbl2_item - shared dictionary lookup table2 item + * @hash: hash value + * @prefix_len: prefix length in bytes + * @str_count: count of strings in the range + * @hash_index: index of the hash in the hash table + * + * The lookup table2 is located at the end of the node. It begins from + * the bottom and is growing in the node's beginning direction. + * Every item of the lookup table2 describes a position of the starting + * keyword of a name. The goal of such descriptor is to describe + * the starting position of the deduplicated keyword that is shared by + * several following names. But the keyword is used only in the beginning + * of the sequence because the rest of the names are represented by + * suffixes only (for example, the sequence of names "absurd, abcissa, + * abacus" can be reprensented by "abacuscissasurd" deduplicated range + * of names). + */ +struct ssdfs_shdict_ltbl2_item { +/* 0x0000 */ + __le64 hash; + +/* 0x0008 */ + __le8 prefix_len; + __le8 str_count; + __le16 hash_index; + __le32 reserved; + +/* 0x0010 */ +} __packed; + +/* + * struct ssdfs_shdict_htbl_item - shared dictionary hash table item + * @hash: hash of the name + * @str_offset: offset in bytes to string + * @str_len: string length + * @type: string type + * + * The hash table contains descriptors of all strings in + * string area. The @str_offset is the offset in bytes from + * the items (strings) area's beginning. + */ +struct ssdfs_shdict_htbl_item { +/* 0x0000 */ + __le64 hash; + +/* 0x0008 */
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH v2 2/79 ssdfs: add key file system declarations
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
This patch adds declarations of key in-core file system's metadata structures and key constants.
--- fs/ssdfs/ssdfs_constants.h | 214 +++++++++ fs/ssdfs/ssdfs_fs_info.h | 821 ++++++++++++++++++++++++++++++++++ fs/ssdfs/ssdfs_inode_info.h | 144 ++++++ fs/ssdfs/ssdfs_thread_info.h | 43 ++ fs/ssdfs/version.h | 9 + include/trace/events/ssdfs.h | 256 +++++++++++ include/uapi/linux/ssdfs_fs.h | 126 ++++++ 7 files changed, 1613 insertions(+) create mode 100644 fs/ssdfs/ssdfs_constants.h create mode 100644 fs/ssdfs/ssdfs_fs_info.h create mode 100644 fs/ssdfs/ssdfs_inode_info.h create mode 100644 fs/ssdfs/ssdfs_thread_info.h create mode 100644 fs/ssdfs/version.h create mode 100644 include/trace/events/ssdfs.h create mode 100644 include/uapi/linux/ssdfs_fs.h
fs/ssdfs/ssdfs_constants.h new file +214 -0
@@ -0,0 +1,214 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/ssdfs_constants.h - SSDFS constant declarations. +* +* Copyright (c) 2019-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* All rights reserved. +* +* Authors: Viacheslav Dubeyko <[email protected]> +*/ + +#ifndef _SSDFS_CONSTANTS_H +#define _SSDFS_CONSTANTS_H + +/* +* Thread types +*/ +enum { +SSDFS_PEB_READ_THREAD , +SSDFS_PEB_FLUSH_THREAD , +SSDFS_PEB_GC_THREAD , +#ifdef CONFIG_SSDFS_ONLINE_FSCK +SSDFS_PEB_FSCK_THREAD , +#endif /* CONFIG_SSDFS_ONLINE_FSCK */ +SSDFS_PEB_THREAD_TYPE_MAX , +}; + +enum { +SSDFS_SEG_USING_GC_THREAD , +SSDFS_SEG_USED_GC_THREAD , +SSDFS_SEG_PRE_DIRTY_GC_THREAD , +SSDFS_SEG_DIRTY_GC_THREAD , +SSDFS_DESTROY_SEG_GC_THREAD , +SSDFS_BTREE_NODE_GC_THREAD , +SSDFS_GC_THREAD_TYPE_MAX , +}; + +enum { +SSDFS_256B =256 , +SSDFS_512B =512 , +SSDFS_1KB =1024 , +SSDFS_2KB =2048 , +SSDFS_4KB =4096 , +SSDFS_8KB =8192 , +SSDFS_16KB =16384 , +SSDFS_32KB =32768 , +SSDFS_64KB =65536 , +SSDFS_128KB =131072 , +SSDFS_256KB =262144 , +SSDFS_512KB =524288 , +SSDFS_1MB =1048576 , +SSDFS_2MB =2097152 , +SSDFS_4MB =4194304 , +SSDFS_8MB =8388608 , +SSDFS_16MB =16777216 , +SSDFS_32MB =33554432 , +SSDFS_64MB =67108864 , +SSDFS_128MB =134217728 , +SSDFS_256MB =268435456 , +SSDFS_512MB =536870912 , +SSDFS_1GB =1073741824 , +SSDFS_2GB =2147483648 , +SSDFS_4GB =4294967296 , +SSDFS_8GB =8589934592 , +SSDFS_16GB =17179869184 , +SSDFS_32GB =34359738368 , +SSDFS_64GB =68719476736 , +}; + +#define SSDFS_256B_STRING "265B" +#define SSDFS_512B_STRING "512B" +#define SSDFS_1KB_STRING "1KB" +#define SSDFS_2KB_STRING "2KB" +#define SSDFS_4KB_STRING "4KB" +#define SSDFS_8KB_STRING "8KB" +#define SSDFS_16KB_STRING "16KB" +#define SSDFS_32KB_STRING "32KB" +#define SSDFS_64KB_STRING "64KB" +#define SSDFS_128KB_STRING "128KB" +#define SSDFS_256KB_STRING "256KB" +#define SSDFS_512KB_STRING "512KB" +#define SSDFS_1MB_STRING "1MB" +#define SSDFS_2MB_STRING "2MB" +#define SSDFS_4MB_STRING "4MB" +#define SSDFS_8MB_STRING "8MB" +#define SSDFS_16MB_STRING "16MB" +#define SSDFS_32MB_STRING "32MB" +#define SSDFS_64MB_STRING "64MB" +#define SSDFS_128MB_STRING "128MB" +#define SSDFS_256MB_STRING "256MB" +#define SSDFS_512MB_STRING "512MB" +#define SSDFS_1GB_STRING "1GB" +#define SSDFS_2GB_STRING "2GB" +#define SSDFS_4GB_STRING "4GB" +#define SSDFS_8GB_STRING "8GB" +#define SSDFS_16GB_STRING "16GB" +#define SSDFS_32GB_STRING "32GB" +#define SSDFS_64GB_STRING "64GB" +#define SSDFS_NUMBER_UNKNOWN "unknown" + +enum { +SSDFS_UNKNOWN_PAGE_TYPE , +SSDFS_USER_DATA_PAGES , +SSDFS_METADATA_PAGES , +SSDFS_PAGES_TYPE_MAX +}; + +#define SSDFS_INVALID_CNO U64_MAX +#define SSDFS_SECTOR_SHIFT (9) +#define SSDFS_DEFAULT_TIMEOUT (msecs_to_jiffies(120000)) +#define SSDFS_DEFAULT_TIMEOUT_NS (jiffies64_to_nsecs(SSDFS_DEFAULT_TIMEOUT)) +#define SSDFS_USER_DATA_TIME_FACTOR (3) +#define SSDFS_INDEX_NODE_TIME_FACTOR (5) +#define SSDFS_LEAF_NODE_TIME_FACTOR (5) +#define SSDFS_HYBRID_NODE_TIME_FACTOR (10) +#define SSDFS_NANOSECS_PER_SEC (1000000000) +#define SSDFS_SECS_PER_MINUTE (60) +#define SSDFS_NANOSECS_PER_MINUTE ((u64)SSDFS_SECS_PER_MINUTE * \ + SSDFS_NANOSECS_PER_SEC) +#define SSDFS_SECS_PER_HOUR (60 * 60) +#define SSDFS_HOURS_PER_DAY (24) +#define SSDFS_DAYS_PER_WEEK (7) +#define SSDFS_WEEKS_PER_MONTH (4) +#define SSDFS_EXTENT_LEN_MAX (8) +#define SSDFS_MAX_NUMBER_OF_TRIES (10) +#define SSDFS_UNMOUNT_NUMBER_OF_TRIES (300) +#define SSDFS_SB_SNAPSHOT_LOG_PAGES (2) +#define SSDFS_DEFAULT_PROTECTION_RANGE ((u64)20 * SSDFS_NANOSECS_PER_MINUTE) + +/* +* Every PEB contains a sequence of logs. Log starts from +* header and could be ended by footer. Header and footer +* requires as minimum 2 logical blocks for metadata. +* It needs to prevent 2 logical blocks from allocation +* in every erase block (PEB). +*/ +#define SSDFS_RESERVED_FREE_PAGE_THRESHOLD_PER_PEB (2) + +static inline +const char *GRANULARITY2STRING (u64 value) +{ +switch (value) { +case SSDFS_256B : +return SSDFS_256B_STRING ; +case SSDFS_512B : +return SSDFS_512B_STRING ; +case SSDFS_1KB : +return SSDFS_1KB_STRING ; +case SSDFS_2KB : +return SSDFS_2KB_STRING ; +case SSDFS_4KB : +return SSDFS_4KB_STRING ; +case SSDFS_8KB : +return SSDFS_8KB_STRING ; +case SSDFS_16KB : +return SSDFS_16KB_STRING ; +case SSDFS_32KB : +return SSDFS_32KB_STRING ; +case SSDFS_64KB : +return SSDFS_64KB_STRING ; +case SSDFS_128KB : +return SSDFS_128KB_STRING ; +case SSDFS_256KB : +return SSDFS_256KB_STRING ; +case SSDFS_512KB : +return SSDFS_512KB_STRING ; +case SSDFS_1MB : +return SSDFS_1MB_STRING ; +case SSDFS_2MB : +return SSDFS_2MB_STRING ; +case SSDFS_4MB : +return SSDFS_4MB_STRING ; +case SSDFS_8MB : +return SSDFS_8MB_STRING ; +case SSDFS_16MB : +return SSDFS_16MB_STRING ; +case SSDFS_32MB : +return SSDFS_32MB_STRING ; +case SSDFS_64MB : +return SSDFS_64MB_STRING ; +case SSDFS_128MB : +return SSDFS_128MB_STRING ; +case SSDFS_256MB : +return SSDFS_256MB_STRING ; +case SSDFS_512MB : +return SSDFS_512MB_STRING ; +case SSDFS_1GB : +return SSDFS_1GB_STRING ; +case SSDFS_2GB : +return SSDFS_2GB_STRING ; +case SSDFS_4GB : +return SSDFS_4GB_STRING ; +case SSDFS_8GB : +return SSDFS_8GB_STRING ; +case SSDFS_16GB : +return SSDFS_16GB_STRING ; +case SSDFS_32GB : +return SSDFS_32GB_STRING ; +case SSDFS_64GB : +return SSDFS_64GB_STRING ; + } + +return SSDFS_NUMBER_UNKNOWN ; +} + +enum { +SSDFS_PARENT_LOCK =0 , +SSDFS_CHILD_LOCK , +}; + +#endif /* _SSDFS_CONSTANTS_H */
fs/ssdfs/ssdfs_fs_info.h new file +821 -0
821 lines not on this page · raw message
fs/ssdfs/ssdfs_inode_info.h new file +144 -0
144 lines not on this page · raw message
fs/ssdfs/ssdfs_thread_info.h new file +43 -0
43 lines not on this page · raw message
fs/ssdfs/version.h new file +9 -0
@@ -0,0 +1,9 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +*/ +#ifndef _SSDFS_VERSION_H +#define _SSDFS_VERSION_H + +#define SSDFS_VERSION "SSDFS v.5.47" + +#endif /* _SSDFS_VERSION_H */
include/trace/events/ssdfs.h new file +256 -0
256 lines not on this page · raw message
include/uapi/linux/ssdfs_fs.h new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 3/79 ssdfs: add key file system's function declarations
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
This patch adds key file system's function declarations and inline functions implementations.
--- fs/ssdfs/ssdfs.h | 502 +++++++ fs/ssdfs/ssdfs_inline.h | 3037 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 3539 insertions(+) create mode 100644 fs/ssdfs/ssdfs.h create mode 100644 fs/ssdfs/ssdfs_inline.h
fs/ssdfs/ssdfs.h new file +502 -0
@@ -0,0 +1,502 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/ssdfs.h - in-core declarations. +* +* Copyright (c) 2019-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* All rights reserved. +* +* Authors: Viacheslav Dubeyko <[email protected]> +*/ + +#ifndef _SSDFS_H +#define _SSDFS_H + +#ifdef pr_fmt +#undef pr_fmt +#endif + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/kobject.h> +#include <linux/sched.h> +#include <linux/fs.h> +#include <linux/crc32.h> +#include <linux/pagemap.h> +#include <linux/fs_parser.h> +#include <linux/fs_context.h> +#include <linux/ssdfs_fs.h> + +#include "ssdfs_constants.h" +#include "ssdfs_thread_info.h" +#include "ssdfs_inode_info.h" +#include "snapshot.h" +#include "snapshot_requests_queue.h" +#include "snapshot_rules.h" +#include "ssdfs_fs_info.h" +#include "ssdfs_inline.h" +#include "fingerprint_array.h" + +/* +* struct ssdfs_value_pair - value/position pair +* @value: some value +* @pos: position of value +*/ +struct ssdfs_value_pair { +int value; +int pos; +}; + +/* +* struct ssdfs_min_max_pair - minimum and maximum values pair +* @min: minimum value/position pair +* @max: maximum value/position pair +*/ +struct ssdfs_min_max_pair { +struct ssdfs_value_pair min; +struct ssdfs_value_pair max; +}; + +/* +* struct ssdfs_block_bmap_range - block bitmap items range +* @start: begin item +* @len: count of items in the range +*/ +struct ssdfs_block_bmap_range { +u32 start; +u32 len; +}; + +/* +* struct ssdfs_blk2off_range - extent of logical blocks +* @start_lblk: start logical block number +* @len: count of logical blocks in extent +*/ +struct ssdfs_blk2off_range { +u16 start_lblk; +u16 len; +}; + +struct ssdfs_mount_context { +unsigned long s_mount_opts; +}; + +struct ssdfs_peb_info ; +struct ssdfs_peb_container ; +struct ssdfs_segment_info ; +struct ssdfs_peb_blk_bmap ; + +/* btree_node.c */ +void ssdfs_zero_btree_node_obj_cache_ptr (void ); +int ssdfs_init_btree_node_obj_cache (void ); +void ssdfs_shrink_btree_node_obj_cache (void ); +void ssdfs_destroy_btree_node_obj_cache (void ); + +/* btree_search.c */ +void ssdfs_zero_btree_search_obj_cache_ptr (void ); +int ssdfs_init_btree_search_obj_cache (void ); +void ssdfs_shrink_btree_search_obj_cache (void ); +void ssdfs_destroy_btree_search_obj_cache (void ); + +/* compression.c */ +int ssdfs_compressors_init (void ); +void ssdfs_free_workspaces (void ); +void ssdfs_compressors_exit (void ); + +/* dev_bdev.c */ +struct bio *ssdfs_bdev_bio_alloc (struct block_device *bdev, +unsigned int nr_iovecs, +unsigned int op, +gfp_t gfp_mask); +void ssdfs_bdev_bio_put (struct bio *bio); +int ssdfs_bdev_bio_add_folio (struct bio *bio,struct folio *folio, +unsigned int offset); +int ssdfs_bdev_read_block (struct super_block *sb,struct folio *folio, +loff_t offset); +int ssdfs_bdev_read_blocks (struct super_block *sb,struct folio_batch *batch, +loff_t offset); +int ssdfs_bdev_read (struct super_block *sb,u32 block_size,loff_t offset, +size_t len,void *buf); +int ssdfs_bdev_can_write_block (struct super_block *sb,u32 block_size, +loff_t offset,bool need_check); +int ssdfs_bdev_write_block (struct super_block *sb,loff_t offset, +struct folio *folio); +int ssdfs_bdev_write_blocks (struct super_block *sb,loff_t offset, +struct folio_batch *batch); + +/* dev_zns.c */ +u64 ssdfs_zns_zone_size (struct super_block *sb,loff_t offset); +u64 ssdfs_zns_zone_capacity (struct super_block *sb,loff_t offset); +u64 ssdfs_zns_zone_write_pointer (struct super_block *sb,loff_t offset); + +/* dir.c */ +int ssdfs_inode_by_name (struct inode *dir, +const struct qstr *child, +ino_t *ino); +int ssdfs_create (struct mnt_idmap *idmap, +struct inode *dir,struct dentry *dentry, +umode_t mode,bool excl); + +/* file.c */ +int ssdfs_allocate_inline_file_buffer (struct inode *inode); +void ssdfs_destroy_inline_file_buffer (struct inode *inode); +int ssdfs_fsync (struct file *file,loff_t start,loff_t end,int datasync); + +/* fs_error.c */ +extern __printf(5, 6) +void ssdfs_fs_error(struct super_block *sb, const char *file, + const char *function, unsigned int line, + const char *fmt, ...); +int ssdfs_set_folio_dirty(struct folio *folio); +int __ssdfs_clear_dirty_folio(struct folio *folio); +int ssdfs_clear_dirty_folio(struct folio *folio); +void ssdfs_clear_dirty_folios(struct address_space *mapping); + +/* global_fsck.c */ +int ssdfs_start_global_fsck_thread(struct ssdfs_fs_info *fsi); +int ssdfs_stop_global_fsck_thread (struct ssdfs_fs_info *fsi); + +/* inode.c */ +bool is_raw_inode_checksum_correct (struct ssdfs_fs_info *fsi, +void *buf,size_t size); +struct inode *ssdfs_iget (struct super_block *sb,ino_t ino); +struct inode *ssdfs_new_inode (struct mnt_idmap *idmap, +struct inode *dir,umode_t mode, +const struct qstr *qstr); +int ssdfs_getattr (struct mnt_idmap *idmap, +const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags); +int ssdfs_setattr (struct mnt_idmap *idmap, +struct dentry *dentry,struct iattr *attr); +void ssdfs_evict_inode (struct inode *inode); +int ssdfs_write_inode (struct inode *inode,struct writeback_control *wbc); +int ssdfs_statfs (struct dentry *dentry,struct kstatfs *buf); +void ssdfs_set_inode_flags (struct inode *inode); + +/* inodes_tree.c */ +void ssdfs_zero_free_ino_desc_cache_ptr (void ); +int ssdfs_init_free_ino_desc_cache (void ); +void ssdfs_shrink_free_ino_desc_cache (void ); +void ssdfs_destroy_free_ino_desc_cache (void ); + +/* ioctl.c */ +long ssdfs_ioctl (struct file *file,unsigned int cmd,unsigned long arg); + +/* log_footer.c */ +bool __is_ssdfs_log_footer_magic_valid (struct ssdfs_signature *magic); +bool is_ssdfs_log_footer_magic_valid (struct ssdfs_log_footer *footer); +bool is_ssdfs_log_footer_csum_valid (void *buf,size_t buf_size); +bool is_ssdfs_volume_state_info_consistent (struct ssdfs_fs_info *fsi, +void *buf, +struct ssdfs_log_footer *footer, +u64 dev_size); +int ssdfs_read_unchecked_log_footer (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 block_size,u32 bytes_off, +void *buf,bool silent, +u32 *log_pages); +int ssdfs_check_log_footer (struct ssdfs_fs_info *fsi, +void *buf, +struct ssdfs_log_footer *footer, +bool silent); +int ssdfs_read_checked_log_footer (struct ssdfs_fs_info *fsi,void *log_hdr, +u64 peb_id,u32 block_size,u32 bytes_off, +void *buf,bool silent); +int ssdfs_prepare_current_segment_ids (struct ssdfs_fs_info *fsi, +__le64 *array, +size_t size); +int ssdfs_prepare_volume_state_info_for_commit (struct ssdfs_fs_info *fsi, +u16 fs_state, +__le64 *cur_segs, +size_t size, +u64 last_log_time, +u64 last_log_cno, +struct ssdfs_volume_state *vs); +int ssdfs_prepare_log_footer_for_commit (struct ssdfs_fs_info *fsi, +u32 block_size, +u32 log_pages, +u32 log_flags, +u64 last_log_time, +u64 last_log_cno, +struct ssdfs_log_footer *footer); + +/* offset_translation_table.c */ +void ssdfs_zero_blk2off_frag_obj_cache_ptr (void ); +int ssdfs_init_blk2off_frag_obj_cache (void ); +void ssdfs_shrink_blk2off_frag_obj_cache (void ); +void ssdfs_destroy_blk2off_frag_obj_cache (void ); + +/* options.c */ +int ssdfs_parse_param (struct fs_context *fc,struct fs_parameter *param); +void ssdfs_initialize_fs_errors_option (struct ssdfs_fs_info *fsi); +int ssdfs_show_options (struct seq_file *seq,struct dentry *root); + +/* peb_migration_scheme.c */ +int ssdfs_peb_start_migration (struct ssdfs_peb_container *pebc); +bool is_peb_under_migration (struct ssdfs_peb_container *pebc); +bool is_pebs_relation_alive (struct ssdfs_peb_container *pebc); +bool has_peb_migration_done (struct ssdfs_peb_container *pebc); +bool should_migration_be_finished (struct ssdfs_peb_container *pebc); +int ssdfs_peb_finish_migration (struct ssdfs_peb_container *pebc); +bool has_ssdfs_source_peb_valid_blocks (struct ssdfs_peb_container *pebc); +int ssdfs_peb_prepare_range_migration (struct ssdfs_peb_container *pebc, +u32 range_len,int blk_type); +int ssdfs_peb_migrate_valid_blocks_range (struct ssdfs_segment_info *si, +struct ssdfs_peb_container *pebc, +struct ssdfs_peb_blk_bmap *peb_blkbmap, +struct ssdfs_block_bmap_range *range); + +/* readwrite.c */ +int ssdfs_read_folio_from_volume (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 bytes_offset, +struct folio *folio); +int ssdfs_read_folio_batch_from_volume (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 bytes_offset, +struct folio_batch *batch); +int ssdfs_aligned_read_buffer (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 block_size,u32 bytes_off, +void *buf,size_t size, +size_t *read_bytes); +int ssdfs_unaligned_read_buffer (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 block_size,u32 bytes_off, +void *buf,size_t size); +int ssdfs_can_write_sb_log (struct super_block *sb, +struct ssdfs_peb_extent *sb_log); +int ssdfs_unaligned_read_folio_batch (struct folio_batch *batch, +u32 offset,u32 size, +void *buf); +int ssdfs_unaligned_write_folio_batch (struct ssdfs_fs_info *fsi, +struct folio_batch *batch, +u32 offset,u32 size, +void *buf); +int ssdfs_unaligned_read_folio_vector (struct ssdfs_fs_info *fsi, +struct ssdfs_folio_vector *vec, +u32 offset,u32 size, +void *buf); +int ssdfs_unaligned_write_folio_vector (struct ssdfs_fs_info *fsi, +struct ssdfs_folio_vector *batch, +u32 offset,u32 size, +void *buf); + +/* recovery.c */ +int ssdfs_init_sb_info (struct ssdfs_fs_info *fsi, +struct ssdfs_sb_info *sbi); +void ssdfs_destruct_sb_info (struct ssdfs_sb_info *sbi); +void ssdfs_backup_sb_info (struct ssdfs_fs_info *fsi); +void ssdfs_restore_sb_info (struct ssdfs_fs_info *fsi); +int ssdfs_init_sb_snap_info (struct ssdfs_fs_info *fsi, +struct ssdfs_sb_snapshot_seg_info *sb_snapi); +void ssdfs_destruct_sb_snap_info (struct ssdfs_sb_snapshot_seg_info *sb_snapi); +int ssdfs_gather_superblock_info (struct ssdfs_fs_info *fsi,int silent); + +/* segment.c */ +void ssdfs_zero_seg_obj_cache_ptr (void ); +int ssdfs_init_seg_obj_cache (void ); +void ssdfs_shrink_seg_obj_cache (void ); +void ssdfs_destroy_seg_obj_cache (void ); +int ssdfs_segment_get_used_data_pages (struct ssdfs_segment_info *si); + +/* super.c */ +void ssdfs_destroy_btree_of_inode (struct inode *inode); +void ssdfs_destroy_and_decrement_btree_of_inode (struct inode *inode); + +/* sysfs.c */ +int ssdfs_sysfs_init (void ); +void ssdfs_sysfs_exit (void ); +int ssdfs_sysfs_create_device_group (struct super_block *sb); +void ssdfs_sysfs_delete_device_group (struct ssdfs_fs_info *fsi); +int ssdfs_sysfs_create_seg_group (struct ssdfs_segment_info *si); +void ssdfs_sysfs_delete_seg_group (struct ssdfs_segment_info *si); +int ssdfs_sysfs_create_peb_group (struct ssdfs_peb_container *pebc); +void ssdfs_sysfs_delete_peb_group (struct ssdfs_peb_container *pebc); +int ssdfs_sysfs_create_maptbl_group (struct ssdfs_fs_info *fsi); +void ssdfs_sysfs_delete_maptbl_group (struct ssdfs_fs_info *fsi); +int ssdfs_sysfs_create_segbmap_group (struct ssdfs_fs_info *fsi); +void ssdfs_sysfs_delete_segbmap_group (struct ssdfs_fs_info *fsi); +int ssdfs_sysfs_create_inodes_tree_group (struct ssdfs_fs_info *fsi); +void ssdfs_sysfs_delete_inodes_tree_group (struct ssdfs_fs_info *fsi); +int ssdfs_sysfs_create_snapshots_tree_group (struct ssdfs_fs_info *fsi); +void ssdfs_sysfs_delete_snapshots_tree_group (struct ssdfs_fs_info *fsi); +int ssdfs_sysfs_create_shared_dict_group (struct ssdfs_fs_info *fsi); +void ssdfs_sysfs_delete_shared_dict_group (struct ssdfs_fs_info *fsi); +int ssdfs_sysfs_create_invextree_group (struct ssdfs_fs_info *fsi); +void ssdfs_sysfs_delete_invextree_group (struct ssdfs_fs_info *fsi); + +/* tunefs.c */ +bool IS_TUNEFS_REQUESTED (struct ssdfs_tunefs_request_copy *request); +bool IS_OPTION_ENABLE_REQUESTED (struct ssdfs_tunefs_option *option); +bool IS_OPTION_DISABLE_REQUESTED (struct ssdfs_tunefs_option *option); +bool IS_VOLUME_LABEL_NEED2CHANGE (struct ssdfs_tunefs_volume_label_option *option); +void ssdfs_tunefs_get_current_volume_config (struct ssdfs_fs_info *fsi, +struct ssdfs_current_volume_config *config); +int ssdfs_tunefs_check_requested_volume_config (struct ssdfs_fs_info *fsi, +struct ssdfs_tunefs_options *options); +void ssdfs_tunefs_get_new_config_request (struct ssdfs_fs_info *fsi, +struct ssdfs_tunefs_config_request *new_config); +void ssdfs_tunefs_save_new_config_request (struct ssdfs_fs_info *fsi, +struct ssdfs_tunefs_options *options); + +/* volume_header.c */ +bool __is_ssdfs_segment_header_magic_valid (struct ssdfs_signature *magic); +bool is_ssdfs_segment_header_magic_valid (struct ssdfs_segment_header *hdr); +bool is_ssdfs_partial_log_header_magic_valid (struct ssdfs_signature *magic); +bool is_ssdfs_volume_header_csum_valid (void *vh_buf,size_t buf_size); +bool is_ssdfs_partial_log_header_csum_valid (void *plh_buf,size_t buf_size); +bool is_ssdfs_volume_header_consistent (struct ssdfs_fs_info *fsi, +struct ssdfs_volume_header *vh, +u64 dev_size); +int ssdfs_check_segment_header (struct ssdfs_fs_info *fsi, +struct ssdfs_segment_header *hdr, +bool silent); +int ssdfs_read_checked_segment_header (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 block_size, +u32 pages_off, +void *buf,bool silent); +int ssdfs_check_partial_log_header (struct ssdfs_fs_info *fsi, +struct ssdfs_partial_log_header *hdr, +bool silent); +void ssdfs_create_volume_header (struct ssdfs_fs_info *fsi, +struct ssdfs_volume_header *vh); +int ssdfs_prepare_volume_header_for_commit (struct ssdfs_fs_info *fsi, +struct ssdfs_volume_header *vh); +int ssdfs_prepare_segment_header_for_commit (struct ssdfs_fs_info *fsi, +u64 seg_id, +u64 leb_id, +u64 peb_id, +u64 relation_peb_id, +u32 log_pages, +u16 seg_type, +u32 seg_flags, +u64 last_log_time, +u64 last_log_cno, +struct ssdfs_segment_header *hdr); +int ssdfs_prepare_partial_log_header_for_commit (struct ssdfs_fs_info *fsi, +int sequence_id, +u64 seg_id, +u64 leb_id, +u64 peb_id, +u64 relation_peb_id, +u32 log_pages, +u16 seg_type, +u32 pl_flags, +u64 last_log_time, +u64 last_log_cno, +struct ssdfs_partial_log_header *hdr); + +/* memory leaks checker */ +void ssdfs_acl_memory_leaks_init (void ); +void ssdfs_acl_check_memory_leaks (void ); +void ssdfs_block_bmap_memory_leaks_init (void ); +void ssdfs_block_bmap_check_memory_leaks (void ); +void ssdfs_blk2off_memory_leaks_init (void ); +void ssdfs_blk2off_check_memory_leaks (void ); +void ssdfs_btree_memory_leaks_init (void ); +void ssdfs_btree_check_memory_leaks (void ); +void ssdfs_btree_hierarchy_memory_leaks_init (void ); +void ssdfs_btree_hierarchy_check_memory_leaks (void ); +void ssdfs_btree_node_memory_leaks_init (void ); +void ssdfs_btree_node_check_memory_leaks (void ); +void ssdfs_btree_search_memory_leaks_init (void ); +void ssdfs_btree_search_check_memory_leaks (void ); +void ssdfs_lzo_memory_leaks_init (void ); +void ssdfs_lzo_check_memory_leaks (void ); +void ssdfs_zlib_memory_leaks_init (void ); +void ssdfs_zlib_check_memory_leaks (void ); +void ssdfs_compr_memory_leaks_init (void ); +void ssdfs_compr_check_memory_leaks (void ); +void ssdfs_cur_seg_memory_leaks_init (void ); +void ssdfs_cur_seg_check_memory_leaks (void ); +void ssdfs_dentries_memory_leaks_init (void ); +void ssdfs_dentries_check_memory_leaks (void ); +void ssdfs_dev_bdev_memory_leaks_init (void ); +void ssdfs_dev_bdev_check_memory_leaks (void ); +void ssdfs_dev_zns_memory_leaks_init (void ); +void ssdfs_dev_zns_check_memory_leaks (void ); +void ssdfs_dev_mtd_memory_leaks_init (void ); +void ssdfs_dev_mtd_check_memory_leaks (void ); +void ssdfs_dir_memory_leaks_init (void ); +void ssdfs_dir_check_memory_leaks (void ); +void ssdfs_diff_memory_leaks_init (void ); +void ssdfs_diff_check_memory_leaks (void ); +void ssdfs_dynamic_array_memory_leaks_init (void ); +void ssdfs_dynamic_array_check_memory_leaks (void ); +void ssdfs_ext_queue_memory_leaks_init (void ); +void ssdfs_ext_queue_check_memory_leaks (void ); +void ssdfs_ext_tree_memory_leaks_init (void ); +void ssdfs_ext_tree_check_memory_leaks (void ); +void ssdfs_farray_memory_leaks_init (void ); +void ssdfs_farray_check_memory_leaks (void ); +void ssdfs_folio_vector_memory_leaks_init (void ); +void ssdfs_folio_vector_check_memory_leaks (void ); +#ifdef CONFIG_SSDFS_PEB_DEDUPLICATION +void ssdfs_fingerprint_array_memory_leaks_init (void ); +void ssdfs_fingerprint_array_check_memory_leaks (void ); +#endif /* CONFIG_SSDFS_PEB_DEDUPLICATION */ +void ssdfs_file_memory_leaks_init (void ); +void ssdfs_file_check_memory_leaks (void ); +void ssdfs_fs_error_memory_leaks_init (void ); +void ssdfs_fs_error_check_memory_leaks (void ); +void ssdfs_flush_memory_leaks_init (void ); +void ssdfs_flush_check_memory_leaks (void ); +void ssdfs_gc_memory_leaks_init (void ); +void ssdfs_gc_check_memory_leaks (void ); +void ssdfs_global_fsck_memory_leaks_init (void ); +void ssdfs_global_fsck_check_memory_leaks (void ); +#ifdef CONFIG_SSDFS_ONLINE_FSCK +void ssdfs_fsck_memory_leaks_init (void ); +void ssdfs_fsck_check_memory_leaks (void ); +#endif /* CONFIG_SSDFS_ONLINE_FSCK */ +void ssdfs_inode_memory_leaks_init (void ); +void ssdfs_inode_check_memory_leaks (void ); +void ssdfs_ino_tree_memory_leaks_init (void ); +void ssdfs_ino_tree_check_memory_leaks (void ); +void ssdfs_invext_tree_memory_leaks_init (void ); +void ssdfs_invext_tree_check_memory_leaks (void ); +void ssdfs_parray_memory_leaks_init (void ); +void ssdfs_parray_check_memory_leaks (void ); +void ssdfs_page_vector_memory_leaks_init (void ); +void ssdfs_page_vector_check_memory_leaks (void ); +void ssdfs_map_queue_memory_leaks_init (void ); +void ssdfs_map_queue_check_memory_leaks (void ); +void ssdfs_map_tbl_memory_leaks_init (void ); +void ssdfs_map_tbl_check_memory_leaks (void ); +void ssdfs_map_cache_memory_leaks_init (void ); +void ssdfs_map_cache_check_memory_leaks (void ); +void ssdfs_map_thread_memory_leaks_init (void ); +void ssdfs_map_thread_check_memory_leaks (void ); +void ssdfs_migration_memory_leaks_init (void ); +void ssdfs_migration_check_memory_leaks (void ); +void ssdfs_peb_memory_leaks_init (void ); +void ssdfs_peb_check_memory_leaks (void ); +void ssdfs_read_memory_leaks_init (void ); +void ssdfs_read_check_memory_leaks (void ); +void ssdfs_recovery_memory_leaks_init (void ); +void ssdfs_recovery_check_memory_leaks (void ); +void ssdfs_req_queue_memory_leaks_init (void ); +void ssdfs_req_queue_check_memory_leaks (void ); +void ssdfs_seg_obj_memory_leaks_init (void ); +void ssdfs_seg_obj_check_memory_leaks (void ); +void ssdfs_seg_bmap_memory_leaks_init (void ); +void ssdfs_seg_bmap_check_memory_leaks (void ); +void ssdfs_seg_blk_memory_leaks_init (void ); +void ssdfs_seg_blk_check_memory_leaks (void ); +void ssdfs_seg_tree_memory_leaks_init (void ); +void ssdfs_seg_tree_check_memory_leaks (void ); +void ssdfs_seq_arr_memory_leaks_init (void ); +void ssdfs_seq_arr_check_memory_leaks (void ); +void ssdfs_dict_memory_leaks_init (void ); +void ssdfs_dict_check_memory_leaks (void ); +void ssdfs_shextree_memory_leaks_init (void ); +void ssdfs_shextree_check_memory_leaks (void ); +void ssdfs_snap_reqs_queue_memory_leaks_init (void ); +void ssdfs_snap_reqs_queue_check_memory_leaks (void ); +void ssdfs_snap_rules_list_memory_leaks_init (void ); +void ssdfs_snap_rules_list_check_memory_leaks (void ); +void ssdfs_snap_tree_memory_leaks_init (void ); +void ssdfs_snap_tree_check_memory_leaks (void ); +void ssdfs_xattr_memory_leaks_init (void ); +void ssdfs_xattr_check_memory_leaks (void ); + +#endif /* _SSDFS_H */
fs/ssdfs/ssdfs_inline.h new file +3037 -0
2,498 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH v2 4/79 ssdfs: implement raw device operations
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
Implement raw device operations:
(1) device_name: get device name
(2) device_size: get device size in bytes
(3) open_zone: open zone
(4) reopen_zone: reopen closed zone
(5) close_zone: close zone
(6) read: read from device
(7) read_block: read logical block
(8) read_blocks: read sequence of logical blocks
(9) can_write_block: can we write into logical block?
(10) write_block: write logical block to device
(11) write_blocks: write sequence of logical blocks to device
(12) erase: erase the whole erase block
(13) trim: support of background erase operation
(14) sync: synchronize page cache with device
--- fs/ssdfs/dev_bdev.c | 1065 ++++++++++++++++++++++++++++++++++ fs/ssdfs/dev_mtd.c | 650 +++++++++++++++++++++ fs/ssdfs/dev_zns.c | 1344 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 3059 insertions(+) create mode 100644 fs/ssdfs/dev_bdev.c create mode 100644 fs/ssdfs/dev_mtd.c create mode 100644 fs/ssdfs/dev_zns.c
fs/ssdfs/dev_bdev.c new file +1065 -0
@@ -0,0 +1,1065 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/dev_bdev.c - Block device access code. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#include <linux/mm.h> +#include <linux/slab.h> +#include <linux/highmem.h> +#include <linux/pagemap.h> +#include <linux/pagevec.h> +#include <linux/bio.h> +#include <linux/blkdev.h> +#include <linux/backing-dev.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "folio_vector.h" +#include "ssdfs.h" + +#include <trace/events/ssdfs.h> + +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_t ssdfs_dev_bdev_folio_leaks; +atomic64_t ssdfs_dev_bdev_memory_leaks; +atomic64_t ssdfs_dev_bdev_cache_leaks; +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +/* +* void ssdfs_dev_bdev_cache_leaks_increment(void *kaddr) +* void ssdfs_dev_bdev_cache_leaks_decrement(void *kaddr) +* void *ssdfs_dev_bdev_kmalloc(size_t size, gfp_t flags) +* void *ssdfs_dev_bdev_kzalloc(size_t size, gfp_t flags) +* void *ssdfs_dev_bdev_kcalloc(size_t n, size_t size, gfp_t flags) +* void ssdfs_dev_bdev_kfree(void *kaddr) +* struct folio *ssdfs_dev_bdev_alloc_folio(gfp_t gfp_mask, +* unsigned int order) +* struct folio *ssdfs_dev_bdev_add_batch_folio(struct folio_batch *batch, +* unsigned int order) +* void ssdfs_dev_bdev_free_folio(struct folio *folio) +* void ssdfs_dev_bdev_folio_batch_release(struct folio_batch *batch) +*/ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING + SSDFS_MEMORY_LEAKS_CHECKER_FNS(dev_bdev) +#else + SSDFS_MEMORY_ALLOCATOR_FNS(dev_bdev) +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +void ssdfs_dev_bdev_memory_leaks_init (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_set (&ssdfs_dev_bdev_folio_leaks,0 ); +atomic64_set (&ssdfs_dev_bdev_memory_leaks,0 ); +atomic64_set (&ssdfs_dev_bdev_cache_leaks,0 ); +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +void ssdfs_dev_bdev_check_memory_leaks (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +if (atomic64_read (&ssdfs_dev_bdev_folio_leaks) !=0 ) { +SSDFS_ERR ("BLOCK DEV: " +"memory leaks include %lld folios\n" , +atomic64_read (&ssdfs_dev_bdev_folio_leaks)); + } + +if (atomic64_read (&ssdfs_dev_bdev_memory_leaks) !=0 ) { +SSDFS_ERR ("BLOCK DEV: " +"memory allocator suffers from %lld leaks\n" , +atomic64_read (&ssdfs_dev_bdev_memory_leaks)); + } + +if (atomic64_read (&ssdfs_dev_bdev_cache_leaks) !=0 ) { +SSDFS_ERR ("BLOCK DEV: " +"caches suffers from %lld leaks\n" , +atomic64_read (&ssdfs_dev_bdev_cache_leaks)); + } +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +static DECLARE_WAIT_QUEUE_HEAD (wq); + +/* +* ssdfs_bdev_device_name() - get device name +* @sb: superblock object +*/ +static const char *ssdfs_bdev_device_name (struct super_block *sb) +{ +return sb->s_id; +} + +/* +* ssdfs_bdev_device_size() - get partition size in bytes +* @sb: superblock object +*/ +static __u64 ssdfs_bdev_device_size (struct super_block *sb) +{ +return i_size_read (sb->s_bdev->bd_mapping->host); +} + +static int ssdfs_bdev_open_zone (struct super_block *sb,loff_t offset) +{ +return -EOPNOTSUPP ; +} + +static int ssdfs_bdev_reopen_zone (struct super_block *sb,loff_t offset) +{ +return -EOPNOTSUPP ; +} + +static int ssdfs_bdev_close_zone (struct super_block *sb,loff_t offset) +{ +return -EOPNOTSUPP ; +} + +/* +* ssdfs_bdev_bio_alloc() - allocate bio object +* @bdev: block device +* @nr_iovecs: number of items in biovec +* @op: direction of I/O +* @gfp_mask: mask of creation flags +*/ +struct bio *ssdfs_bdev_bio_alloc (struct block_device *bdev, +unsigned int nr_iovecs, +unsigned int op, +gfp_t gfp_mask) +{ +struct bio *bio; + + bio =bio_alloc (bdev, nr_iovecs, op, gfp_mask); +if (!bio) { +SSDFS_ERR ("fail to allocate bio\n" ); +return ERR_PTR (-ENOMEM ); + } + +return bio; +} + +/* +* ssdfs_bdev_bio_put() - free bio object +*/ +void ssdfs_bdev_bio_put (struct bio *bio) +{ +if (!bio) +return ; + +bio_put (bio); +} + +/* +* ssdfs_bdev_bio_add_folio() - add folio into bio +* @bio: pointer on bio object +* @folio: memory folio +* @offset: vec entry offset +*/ +int ssdfs_bdev_bio_add_folio (struct bio *bio,struct folio *folio, +unsigned int offset) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bio || !folio); + +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!bio_add_folio (bio, folio,folio_size (folio), offset)) { +SSDFS_ERR ("fail to add folio: " +"offset %u, size %zu\n" , + offset,folio_size (folio)); +return -ERANGE ; + } + +return 0 ; +} + +/* +* ssdfs_bdev_sync_folio_request() - submit folio request +* @sb: superblock object +* @folio: memory folio +* @offset: offset in bytes from partition's begin +* @op: direction of I/O +* @op_flags: request op flags +*/ +static int ssdfs_bdev_sync_folio_request (struct super_block *sb, +struct folio *folio, +loff_t offset, +unsigned int op,int op_flags) +{ +struct bio *bio; +loff_t folio_index; +sector_t sector; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); +#endif /* CONFIG_SSDFS_DEBUG */ + + folio_index =div_u64 (offset,folio_size (folio)); + sector = (pgoff_t)(((u64 )folio_index *folio_size (folio)) >> +SECTOR_SHIFT ); + + bio =ssdfs_bdev_bio_alloc (sb->s_bdev,1 , op,GFP_NOIO ); +if (IS_ERR_OR_NULL (bio)) { + err = !bio ? -ERANGE :PTR_ERR (bio); +SSDFS_ERR ("fail to allocate bio: err %d\n" , + err); +return err; + } + + bio->bi_iter.bi_sector = sector; +bio_set_dev (bio, sb->s_bdev); + bio->bi_opf = op | op_flags; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_bdev_bio_add_folio (bio, folio,0 ); +if (unlikely (err)) { +SSDFS_ERR ("fail to add folio into bio: " +"err %d\n" , + err); + goto finish_sync_folio_request; + } + + err =submit_bio_wait (bio); +if (unlikely (err)) { +SSDFS_ERR ("fail to process request: " +"err %d\n" , + err); + goto finish_sync_folio_request; + } + +finish_sync_folio_request: +ssdfs_bdev_bio_put (bio); + +return err; +} + +/* +* ssdfs_bdev_sync_batch_request() - submit folio batch request +* @sb: superblock object +* @batch: folio batch +* @offset: offset in bytes from partition's begin +* @op: direction of I/O +* @op_flags: request op flags +*/ +static int ssdfs_bdev_sync_batch_request (struct super_block *sb, +struct folio_batch *batch, +loff_t offset, +unsigned int op,int op_flags) +{ +struct bio *bio; +loff_t folio_index; +sector_t sector; +u32 block_size; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!batch); + +SSDFS_DBG ("offset %llu, op %#x, op_flags %#x\n" , + offset, op, op_flags); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (folio_batch_count (batch) ==0 ) { +SSDFS_WARN ("empty folio batch\n" ); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!batch->folios[0 ]); +#endif /* CONFIG_SSDFS_DEBUG */ + + block_size =folio_size (batch->folios[0 ]); + + folio_index =div_u64 (offset, block_size); + sector = (pgoff_t)(((u64 )folio_index * block_size) >>SECTOR_SHIFT ); + + bio =ssdfs_bdev_bio_alloc (sb->s_bdev,folio_batch_count (batch), + op,GFP_NOIO ); +if (IS_ERR_OR_NULL (bio)) { + err = !bio ? -ERANGE :PTR_ERR (bio); +SSDFS_ERR ("fail to allocate bio: err %d\n" , + err); +return err; + } + + bio->bi_iter.bi_sector = sector; +bio_set_dev (bio, sb->s_bdev); + bio->bi_opf = op | op_flags; + +for (i =0 ; i <folio_batch_count (batch); i++) { +struct folio *folio = batch->folios[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); + +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_bdev_bio_add_folio (bio, folio,0 ); +if (unlikely (err)) { +SSDFS_ERR ("fail to add folio %d into bio: " +"err %d\n" , + i, err); + goto finish_sync_batch_request; + } + } + + err =submit_bio_wait (bio); +if (unlikely (err)) { +SSDFS_ERR ("fail to process request: " +"err %d\n" , + err); + goto finish_sync_batch_request; + } + +finish_sync_batch_request: +ssdfs_bdev_bio_put (bio); + +return err; +} + +/* +* ssdfs_bdev_read_block() - read logical block from the volume +* @sb: superblock object +* @folio: memory folio +* @offset: offset in bytes from partition's begin +* +* This function tries to read data on @offset +* from partition's begin in memory folio. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - I/O error. +*/ +int ssdfs_bdev_read_block (struct super_block *sb,struct folio *folio, +loff_t offset) +{ +int err; + + err =ssdfs_bdev_sync_folio_request (sb, folio, offset, +REQ_OP_READ ,REQ_SYNC ); +if (err) { +folio_clear_uptodate (folio); + }else { +folio_mark_uptodate (folio); +flush_dcache_folio (folio); + } + +ssdfs_folio_unlock (folio); + +return err; +} + +/* +* ssdfs_bdev_read_blocks() - read logical blocks from the volume +* @sb: superblock object +* @batch: folio batch +* @offset: offset in bytes from partition's begin +* +* This function tries to read data on @offset +* from partition's begin in folio batch. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - I/O error. +*/ +int ssdfs_bdev_read_blocks (struct super_block *sb,struct folio_batch *batch, +loff_t offset) +{ +int i; +int err =0 ; + + err =ssdfs_bdev_sync_batch_request (sb, batch, offset, +REQ_OP_READ ,REQ_RAHEAD ); + +for (i =0 ; i <folio_batch_count (batch); i++) { +struct folio *folio = batch->folios[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (err) { +folio_clear_uptodate (folio); + }else { +folio_mark_uptodate (folio); +flush_dcache_folio (folio); + } + +ssdfs_folio_unlock (folio); + } + +return err; +} + +/* +* ssdfs_bdev_read_batch() - read from volume into buffer +* @sb: superblock object +* @block_size: block size in bytes +* @offset: offset in bytes from partition's begin +* @len: size of buffer in bytes +* @buf: buffer +* @read_bytes: pointer on read bytes [out] +* +* This function tries to read data on @offset +* from partition's begin with @len bytes in size +* from the volume into @buf. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - I/O error. +*/ +static int ssdfs_bdev_read_batch (struct super_block *sb, +u32 block_size, +loff_t offset,size_t len, +void *buf,size_t *read_bytes) +{ +struct folio_batch batch; +struct folio *folio; +loff_t folio_start, folio_end; +u32 folios_count; +u32 read_len; +loff_t cur_offset = offset; +u32 offset_inside_folio; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, block_size %u, offset %llu, len %zu, buf %p\n" , + sb, block_size, (unsigned long long )offset, len, buf); +#endif /* CONFIG_SSDFS_DEBUG */ + + *read_bytes =0 ; + + folio_start =div_u64 (offset, block_size); + folio_end =div_u64 (offset + len + block_size -1 , block_size); + folios_count = (u32)(folio_end - folio_start); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu, len %zu, block_size %u, " +"folio_start %llu, folio_end %llu, folios_count %u\n" , + (unsigned long long )offset, len, block_size, + (unsigned long long )folio_start, + (unsigned long long )folio_end, + folios_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (folios_count >SSDFS_EXTENT_LEN_MAX ) { +SSDFS_WARN ("folios_count %u > batch_capacity %u, " +"offset %llu, len %zu, block_size %u, " +"folio_start %llu, folio_end %llu\n" , + folios_count,SSDFS_EXTENT_LEN_MAX , + (unsigned long long )offset, len, + block_size, folio_start, folio_end); +return -ERANGE ; + } + +folio_batch_init (&batch); + +for (i =0 ; i < folios_count; i++) { + folio =ssdfs_dev_bdev_alloc_folio (GFP_KERNEL | __GFP_ZERO, +get_order (block_size)); +if (IS_ERR_OR_NULL (folio)) { + err = (folio ==NULL ? -ENOMEM :PTR_ERR (folio)); +SSDFS_ERR ("unable to allocate memory folio\n" ); + goto finish_bdev_read_batch; + } + +ssdfs_folio_get (folio); +ssdfs_folio_lock (folio); +folio_batch_add (&batch, folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + + err =ssdfs_bdev_sync_batch_request (sb, &batch, offset, +REQ_OP_READ ,REQ_SYNC ); +if (unlikely (err)) { +SSDFS_ERR ("fail to read folio batch: err %d\n" , + err); + goto finish_bdev_read_batch; + } + +for (i =0 ; i <folio_batch_count (&batch); i++) { + folio = batch.folios[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (*read_bytes >= len) { + err = -ERANGE ; +SSDFS_ERR ("read_bytes %zu >= len %zu\n" , + *read_bytes, len); + goto finish_bdev_read_batch; + } + +div_u64_rem (cur_offset, block_size, &offset_inside_folio); + read_len =min_t (size_t, (size_t )(block_size - + offset_inside_folio), + (size_t )(len - *read_bytes)); + + err =__ssdfs_memcpy_from_folio (buf, *read_bytes, + len, + folio, offset_inside_folio, + block_size, + read_len); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_bdev_read_batch; + } + + *read_bytes += read_len; + cur_offset += read_len; + } + +finish_bdev_read_batch: +for (i =folio_batch_count (&batch) -1 ; i >=0 ; i--) { + folio = batch.folios[i]; + +if (folio) { +ssdfs_folio_unlock (folio); +ssdfs_folio_put (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_dev_bdev_free_folio (folio); + batch.folios[i] =NULL ; + } + } + +folio_batch_reinit (&batch); + +if (*read_bytes != len) { + err = -EIO ; +SSDFS_ERR ("read_bytes (%zu) != len (%zu)\n" , + *read_bytes, len); + } + +return err; +} + +/* +* ssdfs_bdev_read() - read from volume into buffer +* @sb: superblock object +* @block_size: block size in bytes +* @offset: offset in bytes from partition's begin +* @len: size of buffer in bytes +* @buf: buffer +* +* This function tries to read data on @offset +* from partition's begin with @len bytes in size +* from the volume into @buf. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - I/O error. +*/ +int ssdfs_bdev_read (struct super_block *sb,u32 block_size, +loff_t offset,size_t len,void *buf) +{ +size_t read_bytes =0 ; +loff_t cur_offset = offset; +u8 *ptr = (u8 *)buf; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, block_size %u, offset %llu, len %zu, buf %p\n" , + sb, block_size, (unsigned long long )offset, len, buf); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (len ==0 ) { +SSDFS_WARN ("len is zero\n" ); +return 0 ; + } + +while (read_bytes < len) { +size_t iter_read; + + err =ssdfs_bdev_read_batch (sb, block_size, + cur_offset, + len - read_bytes, + ptr, + &iter_read); +if (unlikely (err)) { +SSDFS_ERR ("fail to read batch: " +"block_size %u, cur_offset %llu, " +"read_bytes %zu, err %d\n" , + block_size, cur_offset, + read_bytes, err); +return err; + } + + cur_offset += iter_read; + ptr += iter_read; + read_bytes += iter_read; + } + +return 0 ; +} + +/* +* ssdfs_bdev_can_write_block() - check that logical block can be written +* @sb: superblock object +* @block_size: size of block in bytes +* @offset: offset in bytes from partition's begin +* @need_check: make check or not? +* +* This function checks that logical block can be written. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EROFS - file system in RO mode. +* %-ENOMEM - fail to allocate memory. +* %-EIO - I/O error. +*/ +int ssdfs_bdev_can_write_block (struct super_block *sb,u32 block_size, +loff_t offset,bool need_check) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +struct ssdfs_signature *magic; +void *buf; +bool is_ssdfs_log_found; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, offset %llu, block_size %u, need_check %d\n" , + sb, (unsigned long long )offset, + block_size, (int )need_check); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!need_check) +return 0 ; + + buf =ssdfs_dev_bdev_kzalloc (block_size,GFP_KERNEL ); +if (!buf) { +SSDFS_ERR ("unable to allocate %d bytes\n" , block_size); +return -ENOMEM ; + } + + err =ssdfs_bdev_read (sb, block_size, offset, block_size, buf); +if (err) + goto free_buf; + +if (memchr_inv (buf,0xff , block_size)) { +if (memchr_inv (buf,0x00 , block_size)) { + magic = (struct ssdfs_signature *)buf; + + is_ssdfs_log_found = +__is_ssdfs_segment_header_magic_valid (magic) || +is_ssdfs_partial_log_header_magic_valid (magic) || +__is_ssdfs_log_footer_magic_valid (magic); + +if (is_ssdfs_log_found && +is_ssdfs_uuid_and_fs_ctime_actual (fsi, buf)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area with offset %llu contains data\n" , + (unsigned long long )offset); + +SSDFS_DBG ("PAGE DUMP:\n" ); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + buf, + block_size); +SSDFS_DBG ("\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + err = -EIO ; + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area with offset %llu contains data\n" , + (unsigned long long )offset); +#endif /* CONFIG_SSDFS_DEBUG */ + err = -EIO ; + } + } + } + +free_buf: +ssdfs_dev_bdev_kfree (buf); +return err; +} + +/* +* ssdfs_bdev_write_block() - write logical block to volume +* @sb: superblock object +* @offset: offset in bytes from partition's begin +* @folio: memory folio +* +* This function tries to write from @folio data +* on @offset from partition's begin. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EROFS - file system in RO mode. +* %-EIO - I/O error. +*/ +int ssdfs_bdev_write_block (struct super_block *sb,loff_t offset, +struct folio *folio) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +#ifdef CONFIG_SSDFS_DEBUG +u32 remainder; +#endif /* CONFIG_SSDFS_DEBUG */ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, offset %llu, folio %p\n" , + sb, offset, folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (sb->s_flags &SB_RDONLY ) { +SSDFS_WARN ("unable to write on RO file system\n" ); +return -EROFS ; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); +BUG_ON ((offset >=ssdfs_bdev_device_size (sb)) || + (folio_size (folio) > (ssdfs_bdev_device_size (sb) - offset))); +div_u64_rem ((u64 )offset, (u64 )folio_size (folio), &remainder); +BUG_ON (remainder); +BUG_ON (!folio_test_dirty (folio)); +BUG_ON (folio_test_locked (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); +atomic_inc (&fsi->pending_bios); + + err =ssdfs_bdev_sync_folio_request (sb, folio, offset, +REQ_OP_WRITE ,REQ_SYNC ); +if (err) { +SSDFS_ERR ("failed to write (err %d): offset %llu\n" , + err, (unsigned long long )offset); + }else { +ssdfs_clear_dirty_folio (folio); +folio_mark_uptodate (folio); + } + +ssdfs_folio_unlock (folio); +ssdfs_folio_put (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_dec_and_test (&fsi->pending_bios)) +wake_up_all (&wq); + +return err; +} + +/* +* ssdfs_bdev_write_blocks() - write batch on volume +* @sb: superblock object +* @offset: offset in bytes from partition's begin +* @batch: memory folios batch +* +* This function tries to write from @batch data +* on @offset from partition's beginning. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EROFS - file system in RO mode. +* %-EIO - I/O error. +*/ +int ssdfs_bdev_write_blocks (struct super_block *sb,loff_t offset, +struct folio_batch *batch) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +struct folio *folio; +int i; +#ifdef CONFIG_SSDFS_DEBUG +u32 remainder; +#endif /* CONFIG_SSDFS_DEBUG */ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, offset %llu, batch %p\n" , + sb, offset, batch); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (sb->s_flags &SB_RDONLY ) { +SSDFS_WARN ("unable to write on RO file system\n" ); +return -EROFS ; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!batch); +BUG_ON (offset >=ssdfs_bdev_device_size (sb)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (folio_batch_count (batch) ==0 ) { +SSDFS_WARN ("empty batch\n" ); +return 0 ; + } + +#ifdef CONFIG_SSDFS_DEBUG +div_u64_rem ((u64 )offset, (u64 )folio_size (batch->folios[0 ]), &remainder); +BUG_ON (remainder); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <folio_batch_count (batch); i++) { + folio = batch->folios[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); + +SSDFS_DBG ("folio_index %lu, folio_size %zu, " +"folio_dirty %#x\n" , + folio->index,folio_size (folio), +folio_test_dirty (folio)); + +BUG_ON (!folio_test_dirty (folio)); +BUG_ON (folio_test_locked (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); + } + +atomic_inc (&fsi->pending_bios); + + err =ssdfs_bdev_sync_batch_request (sb, batch, offset, +REQ_OP_WRITE ,REQ_SYNC ); + +for (i =0 ; i <folio_batch_count (batch); i++) { + folio = batch->folios[i]; + +if (err) { +SSDFS_ERR ("failed to write (err %d): " +"folio_index %llu\n" , + err, + (unsigned long long )folio->index); + }else { +ssdfs_clear_dirty_folio (folio); +folio_mark_uptodate (folio); + } + +ssdfs_folio_unlock (folio); +ssdfs_folio_put (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +if (atomic_dec_and_test (&fsi->pending_bios)) +wake_up_all (&wq); + +return err; +} + +/* +* ssdfs_bdev_support_discard() - check that block device supports discard +*/ +static inline bool ssdfs_bdev_support_discard (struct block_device *bdev) +{ +return bdev_max_discard_sectors (bdev) || +bdev_is_zoned (bdev); +} + +/* +* ssdfs_bdev_trim() - initiate background erase operation +* @sb: superblock object +* @offset: offset in bytes from partition's begin +* @len: size in bytes +* +* This function tries to initiate background erase operation. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EROFS - file system in RO mode. +* %-EFAULT - erase operation error. +*/ +static int ssdfs_bdev_trim (struct super_block *sb,loff_t offset,size_t len) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +u32 erase_size = fsi->erasesize; +loff_t page_start, page_end; +u32 pages_count; +u32 remainder; +sector_t start_sector; +sector_t sectors_count; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, offset %llu, len %zu\n" , + sb, (unsigned long long )offset, len); + +div_u64_rem ((u64 )len, (u64 )erase_size, &remainder); +BUG_ON (remainder); +div_u64_rem ((u64 )offset, (u64 )erase_size, &remainder); +BUG_ON (remainder); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (sb->s_flags &SB_RDONLY ) +return -EROFS ; + +div_u64_rem ((u64 )len, (u64 )erase_size, &remainder); +if (remainder) { +SSDFS_WARN ("len %llu, erase_size %u, remainder %u\n" , + (unsigned long long )len, + erase_size, remainder); +return -ERANGE ; + } + + page_start = offset >>PAGE_SHIFT ; + page_end = (offset + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + pages_count = (u32)(page_end - page_start); + +if (pages_count ==0 ) { +SSDFS_WARN ("pages_count equals to zero\n" ); +return -ERANGE ; + } + + start_sector = page_start << (PAGE_SHIFT -SSDFS_SECTOR_SHIFT ); + sectors_count = pages_count << (PAGE_SHIFT -SSDFS_SECTOR_SHIFT ); + +if (ssdfs_bdev_support_discard (sb->s_bdev)) { + err =blkdev_issue_secure_erase (sb->s_bdev, + start_sector, sectors_count, +GFP_NOFS ); +if (err) + goto try_zeroout; + }else { +try_zeroout: + err =blkdev_issue_zeroout (sb->s_bdev, + start_sector, sectors_count, +GFP_NOFS ,0 ); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to discard: " +"start_sector %llu, sectors_count %llu, " +"err %d\n" , + start_sector, sectors_count, err); +return err; + } + +return 0 ; +} + +/* +* ssdfs_bdev_erase() - make erase operation +* @sb: superblock object +* @offset: offset in bytes from partition's begin +* @len: size in bytes +* +* This function tries to make erase operation. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EROFS - file system in RO mode. +* %-EFAULT - erase operation error. +*/ +static int ssdfs_bdev_erase (struct super_block *sb,loff_t offset,size_t len) +{ +return ssdfs_bdev_trim (sb, offset, len); +} + +/* +* ssdfs_bdev_peb_isbad() - check that PEB is bad +* @sb: superblock object +* @offset: offset in bytes from partition's begin +* +* This function tries to detect that PEB is bad or not. +*/ +static int ssdfs_bdev_peb_isbad (struct super_block *sb,loff_t offset) +{ +/* do nothing */ +return 0 ; +} + +/* +* ssdfs_bdev_mark_peb_bad() - mark PEB as bad +* @sb: superblock object +* @offset: offset in bytes from partition's begin +* +* This function tries to mark PEB as bad. +*/ +static int ssdfs_bdev_mark_peb_bad (struct super_block *sb,loff_t offset) +{ +/* do nothing */ +return 0 ; +} + +/* +* ssdfs_bdev_sync() - make sync operation +* @sb: superblock object +*/ +static void ssdfs_bdev_sync (struct super_block *sb) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("device %s\n" , sb->s_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +wait_event (wq,atomic_read (&fsi->pending_bios) ==0 ); +} + +const struct ssdfs_device_ops ssdfs_bdev_devops = { + .device_name = ssdfs_bdev_device_name, + .device_size = ssdfs_bdev_device_size, + .open_zone = ssdfs_bdev_open_zone, + .reopen_zone = ssdfs_bdev_reopen_zone, + .close_zone = ssdfs_bdev_close_zone, + .read = ssdfs_bdev_read, + .read_block = ssdfs_bdev_read_block, + .read_blocks = ssdfs_bdev_read_blocks, + .can_write_block = ssdfs_bdev_can_write_block, + .write_block = ssdfs_bdev_write_block, + .write_blocks = ssdfs_bdev_write_blocks, + .erase = ssdfs_bdev_erase, + .trim = ssdfs_bdev_trim, + .peb_isbad = ssdfs_bdev_peb_isbad, + .mark_peb_bad = ssdfs_bdev_mark_peb_bad, + .sync = ssdfs_bdev_sync, +};
fs/ssdfs/dev_mtd.c new file +650 -0
650 lines not on this page · raw message
fs/ssdfs/dev_zns.c new file +1344 -0
1,285 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH v2 6/79 ssdfs: implement super operations
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS has specialized superblock segment (erase block) that has goal to keep the sequence of committed superblocks. Superblock instance is stored on successful mount operation and during unmount operation. At first, logic tries to detect the state of current superblock segment. If segment (erase block) is completely full, then a new superblock segment is reserved. As a result, new superblock instance is stored into the sequence. Actually, SSDFS has main and backup copy of current superblock segments. Additionally, SSDFS keeps information about previous, current, next, and reserved superblock segments. SSDFS can use two policy of segment superblock allocation: (1) reserve a new segment for every new allocation, (2) use only set of superblock segments that have been reserved by mkfs tool.
Every commit operation stores log into superblock segment. This log contains:
(1) segment header,
(2) payload (mapping table cache, for example),
(3) log footer.
Segment header can be considered like static superblock info. It contains metadata that not changed at all after volume creation (logical block size, for example) or changed rarely (number of segments in the volume, for example). Log footer can be considered like dynamic part of superblock because it contains frequently updated metadata (for example, root node of inodes b-tree).
Patch implements register/unregister file system logic. The register FS logic includes caches creation/initialization, compression support initialization, sysfs subsystem initialization. Oppositely, unregister FS logic executes destruction of caches, compression subsystem, and sysfs entries.
Also, patch implements basic mount/unmount logic. The ssdfs_fill_super() implements mount logic that includes:
(1) parsing mount options,
(2) extract superblock info,
(3) create key in-core metadata structures (mapping table, segment bitmap, b-trees),
(4) create root inode,
(5) start metadata structures' threads,
(6) commit superblock on finish of mount operation.
The ssdfs_put_super() implements unmount logic:
(1) stop metadata threads,
(2) wait unfinished user data requests,
(3) flush dirty metadata structures,
(4) commit superblock,
(5) destroy in-core metadata structures.
--- fs/ssdfs/options.c | 170 ++++ fs/ssdfs/super.c | 2077 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 2247 insertions(+) create mode 100644 fs/ssdfs/options.c create mode 100644 fs/ssdfs/super.c
fs/ssdfs/options.c new file +170 -0
@@ -0,0 +1,170 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/options.c - mount options parsing. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#include <linux/string.h> +#include <linux/kernel.h> +#include <linux/parser.h> +#include <linux/mount.h> +#include <linux/slab.h> +#include <linux/seq_file.h> +#include <linux/pagevec.h> +#include <linux/fs_parser.h> +#include <linux/fs_context.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "folio_vector.h" +#include "ssdfs.h" +#include "segment_bitmap.h" + +/* +* SSDFS mount options. +* +* Opt_err: behavior if fs error is detected +* Opt_compr: change default compressor +* Opt_ignore_fs_state: ignore on-disk file system state during mount +*/ +enum { + Opt_err, + Opt_compr, + Opt_ignore_fs_state, +}; + +static const struct constant_table ssdfs_param_err[] = { + {"panic" ,SSDFS_MOUNT_ERRORS_PANIC }, + {"remount-ro" ,SSDFS_MOUNT_ERRORS_RO }, + {"continue" ,SSDFS_MOUNT_ERRORS_CONT }, + {} +}; + +static const struct constant_table ssdfs_param_compr[] = { + {"none" ,SSDFS_MOUNT_COMPR_MODE_NONE }, +#ifdef CONFIG_SSDFS_ZLIB + {"zlib" ,SSDFS_MOUNT_COMPR_MODE_ZLIB }, +#endif +#ifdef CONFIG_SSDFS_LZO + {"lzo" ,SSDFS_MOUNT_COMPR_MODE_LZO }, +#endif + {} +}; + +static const struct constant_table ssdfs_param_fs_state[] = { + {"ignore" ,SSDFS_MOUNT_IGNORE_FS_STATE }, + {} +}; + +static const struct fs_parameter_spec ssdfs_fs_parameters[] = { +fsparam_enum ("errors" , Opt_err, ssdfs_param_err), +fsparam_enum ("compr" , Opt_compr, ssdfs_param_compr), +fsparam_enum ("fs_state" , Opt_ignore_fs_state, ssdfs_param_fs_state), + {} +}; + +int ssdfs_parse_param (struct fs_context *fc,struct fs_parameter *param) +{ +struct ssdfs_mount_context *ctx = fc->fs_private; +struct fs_parse_result result; +int opt; + + opt =fs_parse (fc, ssdfs_fs_parameters, param, &result); +if (opt <0 ) +return opt; + +switch (opt) { +case Opt_err: +ssdfs_clear_opt (ctx->s_mount_opts,ERRORS_PANIC ); +ssdfs_clear_opt (ctx->s_mount_opts,ERRORS_RO ); +ssdfs_clear_opt (ctx->s_mount_opts,ERRORS_CONT ); + ctx->s_mount_opts |= result.uint_32; +break ; + +case Opt_compr: +ssdfs_clear_opt (ctx->s_mount_opts,COMPR_MODE_NONE ); +ssdfs_clear_opt (ctx->s_mount_opts,COMPR_MODE_ZLIB ); +ssdfs_clear_opt (ctx->s_mount_opts,COMPR_MODE_LZO ); + ctx->s_mount_opts |= result.uint_32; +break ; + +case Opt_ignore_fs_state: + ctx->s_mount_opts |= result.uint_32; +break ; + +default : +SSDFS_ERR ("unrecognized mount option\n" ); +return -EINVAL ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("DONE: parse options\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +void ssdfs_initialize_fs_errors_option (struct ssdfs_fs_info *fsi) +{ +if (fsi->fs_errors ==SSDFS_ERRORS_PANIC ) +ssdfs_set_opt (fsi->mount_opts,ERRORS_PANIC ); +else if (fsi->fs_errors ==SSDFS_ERRORS_RO ) +ssdfs_set_opt (fsi->mount_opts,ERRORS_RO ); +else if (fsi->fs_errors ==SSDFS_ERRORS_CONTINUE ) +ssdfs_set_opt (fsi->mount_opts,ERRORS_CONT ); +else { +u16 def_behaviour =SSDFS_ERRORS_DEFAULT ; + +switch (def_behaviour) { +case SSDFS_ERRORS_PANIC : +ssdfs_set_opt (fsi->mount_opts,ERRORS_PANIC ); +break ; + +case SSDFS_ERRORS_RO : +ssdfs_set_opt (fsi->mount_opts,ERRORS_RO ); +break ; + } + } +} + +int ssdfs_show_options (struct seq_file *seq,struct dentry *root) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (root->d_sb); +char *compress_type; + +if (ssdfs_test_opt (fsi->mount_opts,COMPR_MODE_ZLIB )) { + compress_type ="zlib" ; +seq_printf (seq,",compress=%s" , compress_type); + }else if (ssdfs_test_opt (fsi->mount_opts,COMPR_MODE_LZO )) { + compress_type ="lzo" ; +seq_printf (seq,",compress=%s" , compress_type); + } + +if (ssdfs_test_opt (fsi->mount_opts,ERRORS_PANIC )) +seq_puts (seq,",errors=panic" ); +else if (ssdfs_test_opt (fsi->mount_opts,ERRORS_RO )) +seq_puts (seq,",errors=remount-ro" ); +else if (ssdfs_test_opt (fsi->mount_opts,ERRORS_CONT )) +seq_puts (seq,",errors=continue" ); + +if (ssdfs_test_opt (fsi->mount_opts,IGNORE_FS_STATE )) +seq_puts (seq,",fs_state=ignore" ); + +return 0 ; +}
fs/ssdfs/super.c new file +2077 -0
2,077 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 7/79 ssdfs: implement commit superblock logic
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
This patch implements commit superblock logic.
--- fs/ssdfs/super.c | 2796 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2796 insertions(+)
fs/ssdfs/super.c +2796 -0
@@ -394,6 +394,2802 @@ static int ssdfs_remount_fs(struct fs_context *fc, struct super_block *sb)return err; } +static inline +bool unfinished_read_data_requests_exist (struct ssdfs_fs_info *fsi) +{ +u64 read_requests =0 ; + +spin_lock (&fsi->volume_state_lock); + read_requests = fsi->read_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +return read_requests >0 ; +} + +static inline +void wait_unfinished_read_data_requests (struct ssdfs_fs_info *fsi) +{ +if (unfinished_read_data_requests_exist (fsi)) { +wait_queue_head_t *wq = &fsi->finish_user_data_read_wq; +u64 old_read_requests, new_read_requests; +int number_of_tries =0 ; +int err; + +while (number_of_tries <SSDFS_UNMOUNT_NUMBER_OF_TRIES ) { +spin_lock (&fsi->volume_state_lock); + old_read_requests = fsi->read_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +DEFINE_WAIT_FUNC (wait, woken_wake_function); +add_wait_queue (wq, &wait); +if (unfinished_read_data_requests_exist (fsi)) { +wait_woken (&wait,TASK_INTERRUPTIBLE ,HZ ); + } +remove_wait_queue (wq, &wait); + +if (!unfinished_read_data_requests_exist (fsi)) +break ; + +spin_lock (&fsi->volume_state_lock); + new_read_requests = fsi->read_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +if (old_read_requests != new_read_requests) { +if (number_of_tries >0 ) + number_of_tries--; + }else + number_of_tries++; + } + +if (unfinished_read_data_requests_exist (fsi)) { +spin_lock (&fsi->volume_state_lock); + new_read_requests = fsi->read_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +SSDFS_WARN ("there are unfinished requests: " +"unfinished_read_data_requests %llu, " +"number_of_tries %d, err %d\n" , + new_read_requests, + number_of_tries, err); + } + } +} + +static inline +bool unfinished_user_data_requests_exist (struct ssdfs_fs_info *fsi) +{ +u64 flush_requests =0 ; + +spin_lock (&fsi->volume_state_lock); + flush_requests = fsi->flushing_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +return flush_requests >0 ; +} + +#ifdef CONFIG_SSDFS_DEBUG +static inline +void ssdfs_show_unfinished_user_data_requests (struct ssdfs_fs_info *fsi) +{ +bool is_empty; +struct list_head *this, *next; + +spin_lock (&fsi->requests_lock); + is_empty =list_empty_careful (&fsi->user_data_requests_list); +if (!is_empty) { +list_for_each_safe (this, next, &fsi->user_data_requests_list) { +struct ssdfs_segment_request *req; + + req = list_entry(this, struct ssdfs_segment_request, + list); + +if (!req) { +SSDFS_ERR_DBG ("empty request ptr\n" ); +continue ; + } + +SSDFS_ERR_DBG ("request: " +"class %#x, cmd %#x, " +"type %#x, refs_count %u, " +"seg %llu, extent (start %u, len %u)\n" , + req->private.class, req->private.cmd, + req->private.type, +atomic_read (&req->private.refs_count), + req->place.start.seg_id, + req->place.start.blk_index, + req->place.len); + } + } +spin_unlock (&fsi->requests_lock); + +if (is_empty) { +SSDFS_ERR_DBG ("list is empty\n" ); +return ; + } +} +#endif /* CONFIG_SSDFS_DEBUG */ + +static inline +void wait_unfinished_user_data_requests (struct ssdfs_fs_info *fsi) +{ +if (unfinished_user_data_requests_exist (fsi)) { +wait_queue_head_t *wq = &fsi->finish_user_data_flush_wq; +u64 old_flush_requests, new_flush_requests; +int number_of_tries =0 ; +int err; + +while (number_of_tries <SSDFS_UNMOUNT_NUMBER_OF_TRIES ) { +spin_lock (&fsi->volume_state_lock); + old_flush_requests = + fsi->flushing_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +DEFINE_WAIT_FUNC (wait, woken_wake_function); +add_wait_queue (wq, &wait); +if (unfinished_user_data_requests_exist (fsi)) { +wait_woken (&wait,TASK_INTERRUPTIBLE ,HZ ); + } +remove_wait_queue (wq, &wait); + +if (!unfinished_user_data_requests_exist (fsi)) +break ; + +spin_lock (&fsi->volume_state_lock); + new_flush_requests = + fsi->flushing_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +if (old_flush_requests != new_flush_requests) { +if (number_of_tries >0 ) + number_of_tries--; + }else + number_of_tries++; + } + +if (unfinished_user_data_requests_exist (fsi)) { +spin_lock (&fsi->volume_state_lock); + new_flush_requests = + fsi->flushing_user_data_requests; +spin_unlock (&fsi->volume_state_lock); + +SSDFS_WARN ("there are unfinished requests: " +"unfinished_user_data_requests %llu, " +"number_of_tries %d, err %d\n" , + new_flush_requests, + number_of_tries, err); + +#ifdef CONFIG_SSDFS_DEBUG +ssdfs_show_unfinished_user_data_requests (fsi); +#endif /* CONFIG_SSDFS_DEBUG */ + } + } +} + +static int ssdfs_sync_fs (struct super_block *sb,int wait) +{ +struct ssdfs_fs_info *fsi; +int err =0 ; + + fsi =SSDFS_FS_I (sb); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("sb %p\n" , sb); +#else +SSDFS_DBG ("sb %p\n" , sb); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +#ifdef CONFIG_SSDFS_SHOW_CONSUMED_MEMORY +SSDFS_ERR ("SYNCFS is starting...\n" ); +ssdfs_check_memory_leaks (); +#endif /* CONFIG_SSDFS_SHOW_CONSUMED_MEMORY */ + +atomic_set (&fsi->global_fs_state,SSDFS_METADATA_GOING_FLUSHING ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("SSDFS_METADATA_GOING_FLUSHING\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +wake_up_all (&fsi->pending_wq); +wait_unfinished_user_data_requests (fsi); + +atomic_set (&fsi->global_fs_state,SSDFS_METADATA_UNDER_FLUSH ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("SSDFS_METADATA_UNDER_FLUSH\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&fsi->volume_sem); + +if (fsi->fs_feature_compat & +SSDFS_HAS_INVALID_EXTENTS_TREE_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush invalidated extents btree\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_invextree_flush (fsi); +if (err) { +SSDFS_ERR ("fail to flush invalidated extents btree: " +"err %d\n" , err); + } + } + +if (fsi->fs_feature_compat &SSDFS_HAS_SHARED_EXTENTS_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush shared extents btree\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_shextree_flush (fsi); +if (err) { +SSDFS_ERR ("fail to flush shared extents btree: " +"err %d\n" , err); + } + } + +if (fsi->fs_feature_compat &SSDFS_HAS_INODES_TREE_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush inodes btree\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_inodes_btree_flush (fsi->inodes_tree); +if (err) { +SSDFS_ERR ("fail to flush inodes btree: " +"err %d\n" , err); + } + } + +if (fsi->fs_feature_compat &SSDFS_HAS_SHARED_DICT_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush shared dictionary\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_shared_dict_btree_flush (fsi->shdictree); +if (err) { +SSDFS_ERR ("fail to flush shared dictionary: " +"err %d\n" , err); + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("process the snapshots creation\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_execute_create_snapshots (fsi); +if (err) { +SSDFS_ERR ("fail to process the snapshots creation\n" ); + } + +if (fsi->fs_feature_compat &SSDFS_HAS_SNAPSHOTS_TREE_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush snapshots btree\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_snapshots_btree_flush (fsi); +if (err) { +SSDFS_ERR ("fail to flush snapshots btree: " +"err %d\n" , err); + } + } + +if (fsi->fs_feature_compat &SSDFS_HAS_SEGBMAP_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush segment bitmap\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_segbmap_flush (fsi->segbmap); +if (err) { +SSDFS_ERR ("fail to flush segment bitmap: " +"err %d\n" , err); + } + } + +if (fsi->fs_feature_compat &SSDFS_HAS_MAPTBL_COMPAT_FLAG ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush mapping table\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_maptbl_flush (fsi->maptbl); +if (err) { +SSDFS_ERR ("fail to flush mapping table: " +"err %d\n" , err); + } + } + +up_write (&fsi->volume_sem); + +atomic_set (&fsi->global_fs_state,SSDFS_REGULAR_FS_OPERATIONS ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("SSDFS_REGULAR_FS_OPERATIONS\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +wake_up_all (&fsi->pending_wq); + +#ifdef CONFIG_SSDFS_SHOW_CONSUMED_MEMORY +SSDFS_ERR ("SYNCFS has been finished...\n" ); +ssdfs_check_memory_leaks (); +#endif /* CONFIG_SSDFS_SHOW_CONSUMED_MEMORY */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (unlikely (err)) + goto fail_sync_fs; + +trace_ssdfs_sync_fs (sb, wait); + +return 0 ; + +fail_sync_fs: +trace_ssdfs_sync_fs_exit (sb, wait, err); +return err; +} + +static struct inode *ssdfs_nfs_get_inode (struct super_block *sb, +u64 ino,u32 generation) +{ +struct inode *inode; + +if (ino <SSDFS_ROOT_INO ) +return ERR_PTR (-ESTALE ); + + inode =ssdfs_iget (sb, ino); +if (IS_ERR (inode)) +return ERR_CAST (inode); +if (generation && inode->i_generation != generation) { +iput (inode); +return ERR_PTR (-ESTALE ); + } +return inode; +} + +static struct dentry *ssdfs_fh_to_dentry (struct super_block *sb, +struct fid *fid, +int fh_len,int fh_type) +{ +return generic_fh_to_dentry (sb, fid, fh_len, fh_type, + ssdfs_nfs_get_inode); +} + +static struct dentry *ssdfs_fh_to_parent (struct super_block *sb, +struct fid *fid, +int fh_len,int fh_type) +{ +return generic_fh_to_parent (sb, fid, fh_len, fh_type, + ssdfs_nfs_get_inode); +} + +static struct dentry *ssdfs_get_parent (struct dentry *child) +{ +struct qstr dotdot =QSTR_INIT (".." ,2 ); +ino_t ino; +int err; + +down_read (&SSDFS_I (d_inode (child))->lock); + err =ssdfs_inode_by_name (d_inode (child), &dotdot, &ino); +up_read (&SSDFS_I (d_inode (child))->lock); + +if (unlikely (err)) +return ERR_PTR (err); + +return d_obtain_alias (ssdfs_iget (child->d_sb, ino)); +} + +static const struct export_operations ssdfs_export_ops = { + .get_parent = ssdfs_get_parent, + .fh_to_dentry = ssdfs_fh_to_dentry, + .fh_to_parent = ssdfs_fh_to_parent, +}; + +static const struct super_operations ssdfs_super_operations = { + .alloc_inode = ssdfs_alloc_inode, + .destroy_inode = ssdfs_destroy_inode, + .evict_inode = ssdfs_evict_inode, + .write_inode = ssdfs_write_inode, + .statfs = ssdfs_statfs, + .show_options = ssdfs_show_options, + .put_super = ssdfs_put_super, + .sync_fs = ssdfs_sync_fs, +}; + +static inline +u32 ssdfs_sb_payload_size (struct folio_batch *batch) +{ +struct ssdfs_maptbl_cache_header *hdr; +struct folio *folio; +void *kaddr; +u16 fragment_bytes_count; +u32 bytes_count =0 ; +int i; + +for (i =0 ; i <folio_batch_count (batch); i++) { + folio = batch->folios[i]; + +ssdfs_folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); + hdr = (struct ssdfs_maptbl_cache_header *)kaddr; + fragment_bytes_count =le16_to_cpu (hdr->bytes_count); +kunmap_local (kaddr); +ssdfs_folio_unlock (folio); + +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (fragment_bytes_count >PAGE_SIZE ); +#endif /* CONFIG_SSDFS_DEBUG */ + + bytes_count += fragment_bytes_count; + } + +return bytes_count; +} + +static u32 ssdfs_define_sb_log_size (struct super_block *sb) +{ +struct ssdfs_fs_info *fsi; +size_t hdr_size =sizeof (struct ssdfs_segment_header ); +u32 inline_capacity; +u32 log_size =0 ; +u32 payload_size; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb); + +SSDFS_DBG ("sb %p\n" , sb); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + payload_size =ssdfs_sb_payload_size (&fsi->maptbl_cache.batch); + inline_capacity =PAGE_SIZE - hdr_size; + +if (payload_size > inline_capacity) { + log_size +=PAGE_SIZE ; + log_size +=atomic_read (&fsi->maptbl_cache.bytes_count); + log_size +=PAGE_SIZE ; + }else { + log_size +=PAGE_SIZE ; + log_size +=PAGE_SIZE ; + } + + log_size = (log_size +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + +return log_size; +} + +static int ssdfs_snapshot_sb_log_payload (struct super_block *sb, +struct ssdfs_sb_log_payload *payload) +{ +struct ssdfs_fs_info *fsi; +struct folio *sfolio, *dfolio; +unsigned folios_count; +unsigned i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !payload); +BUG_ON (folio_batch_count (&payload->maptbl_cache.batch) !=0 ); + +SSDFS_DBG ("sb %p, payload %p\n" , + sb, payload); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + +down_read (&fsi->maptbl_cache.lock); + + folios_count =folio_batch_count (&fsi->maptbl_cache.batch); + +for (i =0 ; i < folios_count; i++) { + dfolio =ssdfs_super_add_batch_folio (&payload->maptbl_cache.batch, +get_order (PAGE_SIZE )); +if (unlikely (IS_ERR_OR_NULL (dfolio))) { + err = !dfolio ? -ENOMEM :PTR_ERR (dfolio); +SSDFS_ERR ("fail to add folio into batch: " +"index %u, err %d\n" , + i, err); + goto finish_maptbl_snapshot; + } + + sfolio = fsi->maptbl_cache.batch.folios[i]; +if (unlikely (!sfolio)) { + err = -ERANGE ; +SSDFS_ERR ("source folio is absent: index %u\n" , + i); + goto finish_maptbl_snapshot; + } + +ssdfs_folio_lock (sfolio); +ssdfs_folio_lock (dfolio); +__ssdfs_memcpy_folio (dfolio,0 ,PAGE_SIZE , + sfolio,0 ,PAGE_SIZE , +PAGE_SIZE ); +ssdfs_folio_unlock (dfolio); +ssdfs_folio_unlock (sfolio); + } + + payload->maptbl_cache.bytes_count = +atomic_read (&fsi->maptbl_cache.bytes_count); + +finish_maptbl_snapshot: +up_read (&fsi->maptbl_cache.lock); + +if (unlikely (err)) +ssdfs_super_folio_batch_release (&payload->maptbl_cache.batch); + +return err; +} + +static inline +int ssdfs_write_padding_block (struct super_block *sb, +struct folio *folio, +loff_t offset) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_padding_header *hdr; +size_t hdr_size =sizeof (struct ssdfs_padding_header ); +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !folio); +BUG_ON (!SSDFS_FS_I (sb)->devops); +BUG_ON (!SSDFS_FS_I (sb)->devops->write_block); + +SSDFS_DBG ("offset %llu\n" , offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); + + err =__ssdfs_memset_folio (folio, hdr_size,folio_size (folio), +0xdada ,folio_size (folio) - hdr_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare padding block: err %d\n" , err); +ssdfs_folio_unlock (folio); + goto unlock_folio; + } + + hdr = (struct ssdfs_padding_header *)kmap_local_folio (folio,0 ); + hdr->magic.common =cpu_to_le32 (SSDFS_SUPER_MAGIC ); + hdr->magic.key =cpu_to_le16 (SSDFS_PADDING_HDR_MAGIC ); + hdr->magic.version.major =SSDFS_MAJOR_REVISION ; + hdr->magic.version.minor =SSDFS_MINOR_REVISION ; + hdr->blob =cpu_to_le64 (SSDFS_PADDING_BLOB ); + hdr->check.bytes =cpu_to_le16 (folio_size (folio)); + hdr->check.flags =cpu_to_le16 (0 ); + hdr->check.csum =cpu_to_le32 (0xdada ); +kunmap_local (hdr); + +folio_mark_uptodate (folio); +folio_set_dirty (folio); + +ssdfs_folio_unlock (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err = fsi->devops->write_block (sb, offset, folio); +if (err) { +SSDFS_ERR ("fail to write segment header: " +"offset %llu, size %zu\n" , + (u64 )offset,folio_size (folio)); + } + +ssdfs_folio_lock (folio); +folio_clear_uptodate (folio); +unlock_folio: +ssdfs_folio_unlock (folio); + +return err; +} + +static int ssdfs_write_padding (struct super_block *sb, +loff_t offset,loff_t size) +{ +struct folio *folio; +loff_t written =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb); +BUG_ON (!SSDFS_FS_I (sb)->devops); +BUG_ON (!SSDFS_FS_I (sb)->devops->write_block); + +SSDFS_DBG ("offset %llu, size %llu\n" , offset, size); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (size ==0 || size %PAGE_SIZE ) { +SSDFS_ERR ("invalid size %llu\n" , size); +return -EINVAL ; + } + + folio =ssdfs_super_alloc_folio (GFP_KERNEL | __GFP_ZERO, +get_order (PAGE_SIZE )); +if (IS_ERR_OR_NULL (folio)) { + err = (folio ==NULL ? -ENOMEM :PTR_ERR (folio)); +SSDFS_ERR ("unable to allocate memory folio\n" ); +return err; + } + +while (written < size) { + offset = offset + written; + + err =ssdfs_write_padding_block (sb, folio, offset); +if (unlikely (err)) { +SSDFS_ERR ("fail to write padding block: " +"offset %llu\n" , offset); + goto free_folio; + } + + written +=folio_size (folio); + } + +free_folio: +ssdfs_super_free_folio (folio); + +return err; +} + +static inline +int ssdfs_write_padding_in_sb_segs_pair (struct super_block *sb, +u32 log_page) +{ +struct ssdfs_fs_info *fsi; +u64 cur_peb; +u32 pages_per_peb; +loff_t peb_offset; +loff_t padding_offset; +loff_t padding_size; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb); +BUG_ON (!SSDFS_FS_I (sb)->devops); +BUG_ON (!SSDFS_FS_I (sb)->devops->write_block); + +SSDFS_DBG ("log_page %u,\n" , log_page); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + +/* +* Superblock segment uses 4KB page always. +* It needs to calculate pages_per_peb value. +*/ + pages_per_peb = fsi->erasesize /PAGE_SIZE ; + +for (i =0 ; i <SSDFS_SB_SEG_COPY_MAX ; i++) { + cur_peb = fsi->sb_pebs[SSDFS_CUR_SB_SEG ][i]; + + padding_size = pages_per_peb - log_page; + padding_size <<=PAGE_SHIFT ; + + peb_offset = cur_peb * fsi->pages_per_peb; + peb_offset <<= fsi->log_pagesize; + + padding_offset = log_page; + padding_offset <<=PAGE_SHIFT ; + padding_offset += peb_offset; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_peb %llu, padding_offset %llu, " +"padding_size %llu\n" , + cur_peb, (u64 )padding_offset, + (u64 )padding_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_write_padding (sb, padding_offset, padding_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to write padding: " +"padding_offset %llu, " +"padding_size %llu, " +"err %d\n" , + padding_offset, + padding_size, + err); +return err; + } + } + +return 0 ; +} + +static int ssdfs_define_next_sb_log_place (struct super_block *sb, +struct ssdfs_peb_extent *last_sb_log) +{ +struct ssdfs_fs_info *fsi; +u32 offset; +u32 log_size; +u64 cur_peb, prev_peb; +u64 cur_leb; +u32 pages_per_peb; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !last_sb_log); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p, last_sb_log %p\n" , + sb, last_sb_log); +SSDFS_DBG ("fsi->sbi.last_log: (leb_id %llu, peb_id %llu, " +"page_offset %u, pages_count %u), " +"last_sb_log: (leb_id %llu, peb_id %llu, " +"page_offset %u, pages_count %u)\n" , + fsi->sbi.last_log.leb_id, + fsi->sbi.last_log.peb_id, + fsi->sbi.last_log.page_offset, + fsi->sbi.last_log.pages_count, + last_sb_log->leb_id, + last_sb_log->peb_id, + last_sb_log->page_offset, + last_sb_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +/* +* Superblock segment uses 4KB page always. +* It needs to calculate pages_per_peb value. +*/ + pages_per_peb = fsi->erasesize /PAGE_SIZE ; + + offset = fsi->sbi.last_log.page_offset; + + log_size =ssdfs_define_sb_log_size (sb); +if (log_size > pages_per_peb) { +SSDFS_ERR ("log_size %u > pages_per_peb %u\n" , + log_size, pages_per_peb); +return -ERANGE ; + } + + log_size =max_t (u32, log_size, fsi->sbi.last_log.pages_count); + +if (offset > pages_per_peb || offset > (UINT_MAX - log_size)) { +SSDFS_ERR ("inconsistent metadata state: " +"last_sb_log.page_offset %u, " +"pages_per_peb %u, log_size %u\n" , + offset, pages_per_peb, log_size); +return -EINVAL ; + } + +for (err = -EINVAL , i =0 ; i <SSDFS_SB_SEG_COPY_MAX ; i++) { + cur_peb = fsi->sb_pebs[SSDFS_CUR_SB_SEG ][i]; + prev_peb = fsi->sb_pebs[SSDFS_PREV_SB_SEG ][i]; + cur_leb = fsi->sb_lebs[SSDFS_CUR_SB_SEG ][i]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_peb %llu, prev_peb %llu, " +"last_sb_log.peb_id %llu, log_size %u, " +"err %d\n" , + cur_peb, prev_peb, + fsi->sbi.last_log.peb_id, + log_size, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (fsi->sbi.last_log.peb_id == cur_peb) { + offset += fsi->sbi.last_log.pages_count; + +if (offset > pages_per_peb) { +SSDFS_ERR ("inconsistent metadata state: " +"last_sb_log.page_offset %u, " +"pages_per_peb %u, log_size %u\n" , + offset, pages_per_peb, + fsi->sbi.last_log.pages_count); +return -EIO ; + }else if (offset == pages_per_peb) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb PEB %llu is full: " +"offset %u, log_size %u, " +"pages_per_peb %u\n" , + cur_peb, offset, + fsi->sbi.last_log.pages_count, + pages_per_peb); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EFBIG ; + }else if ((offset + log_size) > pages_per_peb) { + err =ssdfs_write_padding_in_sb_segs_pair (sb, + offset); +if (unlikely (err)) { +SSDFS_ERR ("fail to write padding: " +"err %d\n" , err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb PEB %llu is full: " +"offset %u, log_size %u, " +"pages_per_peb %u\n" , + cur_peb, offset, + fsi->sbi.last_log.pages_count, + pages_per_peb); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EFBIG ; + } + + last_sb_log->leb_id = cur_leb; + last_sb_log->peb_id = cur_peb; + last_sb_log->page_offset = offset; + last_sb_log->pages_count = log_size; + + err =0 ; +break ; + }else if (fsi->sbi.last_log.peb_id != cur_peb && + fsi->sbi.last_log.peb_id == prev_peb) { + + last_sb_log->leb_id = cur_leb; + last_sb_log->peb_id = cur_peb; + last_sb_log->page_offset =0 ; + last_sb_log->pages_count = log_size; + + err =0 ; +break ; + }else { +/* continue to check */ + err = -ERANGE ; + } + } + +if (err) { +SSDFS_ERR ("inconsistent metadata state: " +"cur_peb %llu, prev_peb %llu, " +"last_sb_log.peb_id %llu\n" , + cur_peb, prev_peb, fsi->sbi.last_log.peb_id); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_sb_log->leb_id %llu, " +"last_sb_log->peb_id %llu, " +"last_sb_log->page_offset %u, " +"last_sb_log->pages_count %u\n" , + last_sb_log->leb_id, + last_sb_log->peb_id, + last_sb_log->page_offset, + last_sb_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <SSDFS_SB_SEG_COPY_MAX ; i++) { + last_sb_log->leb_id = fsi->sb_lebs[SSDFS_CUR_SB_SEG ][i]; + last_sb_log->peb_id = fsi->sb_pebs[SSDFS_CUR_SB_SEG ][i]; + err =ssdfs_can_write_sb_log (sb, last_sb_log); +if (err) { +SSDFS_DBG ("possibly logical block has data: " +"PEB %llu: " +"last_sb_log->page_offset %u, " +"last_sb_log->pages_count %u\n" , + last_sb_log->peb_id, + last_sb_log->page_offset, + last_sb_log->pages_count); + } + } + + last_sb_log->leb_id = cur_leb; + last_sb_log->peb_id = cur_peb; + +if (fsi->sb_snapi.need_snapshot_sb) { +struct ssdfs_peb_extent *last_sb_snap_log; + + last_sb_snap_log = &fsi->sb_snapi.last_log; + + offset = last_sb_snap_log->page_offset; + +if (offset >= pages_per_peb) { +SSDFS_ERR ("inconsistent metadata state: " +"last_sb_snap_log.page_offset %u, " +"pages_per_peb %u\n" , + offset, pages_per_peb); +return -EINVAL ; + } + + offset += last_sb_snap_log->pages_count; + +if (offset >= pages_per_peb) { +SSDFS_ERR ("inconsistent metadata state: " +"last_sb_snap_log.page_offset %u, " +"last_sb_snap_log.pages_count %u, " +"pages_per_peb %u\n" , + last_sb_snap_log->page_offset, + last_sb_snap_log->pages_count, + pages_per_peb); +return -ERANGE ; + } + + last_sb_snap_log->page_offset = offset; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_sb_snap_log (page_offset %u, pages_count %u)\n" , + last_sb_snap_log->page_offset, + last_sb_snap_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_can_write_sb_log (sb, last_sb_snap_log); +if (err) { +SSDFS_DBG ("possibly logical block has data: " +"PEB %llu: " +"last_sb_log->page_offset %u, " +"last_sb_log->pages_count %u\n" , + last_sb_log->peb_id, + last_sb_log->page_offset, + last_sb_log->pages_count); + } + } + +return 0 ; +} + +#ifndef CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET +static u64 ssdfs_correct_start_leb_id (struct ssdfs_fs_info *fsi, +int seg_type,u64 leb_id) +{ +struct completion *init_end; +struct ssdfs_maptbl_peb_relation pebr; +struct ssdfs_maptbl_peb_descriptor *ptr; +u8 peb_type =SSDFS_MAPTBL_UNKNOWN_PEB_TYPE ; +u32 pebs_per_seg; +u64 seg_id; +u64 cur_leb; +u64 peb_id1, peb_id2; +u64 found_peb_id; +u64 peb_id_off; +u16 pebs_per_fragment; +u16 pebs_per_stripe; +u16 stripes_per_fragment; +u64 calculated_leb_id = leb_id; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi); + +SSDFS_DBG ("fsi %p, seg_type %#x, leb_id %llu\n" , + fsi, seg_type, leb_id); +#endif /* CONFIG_SSDFS_DEBUG */ + + found_peb_id = leb_id; + peb_type =SEG2PEB_TYPE (seg_type); + pebs_per_seg = fsi->pebs_per_seg; + + seg_id =ssdfs_get_seg_id_for_leb_id (fsi, leb_id); +if (unlikely (seg_id >=U64_MAX )) { +SSDFS_ERR ("invalid seg_id: " +"leb_id %llu\n" , leb_id); +return -ERANGE ; + } + + err =ssdfs_maptbl_define_fragment_info (fsi, leb_id, + &pebs_per_fragment, + &pebs_per_stripe, + &stripes_per_fragment); +if (unlikely (err)) { +SSDFS_ERR ("fail to define fragment info: " +"err %d\n" , err); +return err; + } + +for (i =0 ; i < pebs_per_seg; i++) { + cur_leb =ssdfs_get_leb_id_for_peb_index (fsi, seg_id, i); +if (cur_leb >=U64_MAX ) { +SSDFS_ERR ("fail to convert PEB index into LEB ID: " +"seg %llu, peb_index %u\n" , + seg_id, i); +return -ERANGE ; + } + + err =ssdfs_maptbl_convert_leb2peb (fsi, cur_leb, + peb_type, &pebr, + &init_end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (init_end); +if (unlikely (err)) { +SSDFS_ERR ("maptbl init failed: " +"err %d\n" , err); + goto finish_leb_id_correction; + } + + err =ssdfs_maptbl_convert_leb2peb (fsi, cur_leb, + peb_type, &pebr, + &init_end); + } + +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("LEB is not mapped: leb_id %llu\n" , + cur_leb); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_leb_id_correction; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to convert LEB to PEB: " +"leb_id %llu, peb_type %#x, err %d\n" , + cur_leb, peb_type, err); + goto finish_leb_id_correction; + } + + ptr = &pebr.pebs[SSDFS_MAPTBL_MAIN_INDEX ]; + peb_id1 = ptr->peb_id; + ptr = &pebr.pebs[SSDFS_MAPTBL_RELATION_INDEX ]; + peb_id2 = ptr->peb_id; + +if (peb_id1 <U64_MAX ) + found_peb_id =max_t (u64, peb_id1, found_peb_id); + +if (peb_id2 <U64_MAX ) + found_peb_id =max_t (u64, peb_id2, found_peb_id); + + peb_id_off = found_peb_id % pebs_per_stripe; +if (peb_id_off >= (pebs_per_stripe /2 )) { + calculated_leb_id = found_peb_id / pebs_per_stripe; + calculated_leb_id++; + calculated_leb_id *= pebs_per_stripe; + }else { + calculated_leb_id = found_peb_id; + calculated_leb_id++; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found_peb_id %llu, pebs_per_stripe %u, " +"calculated_leb_id %llu\n" , + found_peb_id, pebs_per_stripe, + calculated_leb_id); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +finish_leb_id_correction: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("leb_id %llu, calculated_leb_id %llu\n" , + leb_id, calculated_leb_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +return calculated_leb_id; +} +#endif /* CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET */ + +#ifndef CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET +static int __ssdfs_reserve_clean_segment (struct ssdfs_fs_info *fsi, +int sb_seg_type, +u64 start_search_id, +u64 *reserved_seg) +{ +struct ssdfs_segment_bmap *segbmap = fsi->segbmap; +u64 start_seg = start_search_id; +u64 end_seg =U64_MAX ; +struct ssdfs_maptbl_peb_relation pebr; +struct completion *end; +u8 peb_type =SSDFS_MAPTBL_SBSEG_PEB_TYPE ; +u64 leb_id; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!reserved_seg); +BUG_ON (sb_seg_type >=SSDFS_SB_SEG_COPY_MAX ); + +SSDFS_DBG ("fsi %p, sb_seg_type %#x, start_search_id %llu\n" , + fsi, sb_seg_type, start_search_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (sb_seg_type) { +case SSDFS_MAIN_SB_SEG : +case SSDFS_COPY_SB_SEG : + err =ssdfs_segment_detect_search_range (fsi, + &start_seg, + &end_seg); +if (err == -ENOENT ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find fragment for search: " +"start_seg %llu, end_seg %llu\n" , + start_seg, end_seg); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to define a search range: " +"start_seg %llu, err %d\n" , + start_seg, err); +return err; + } +break ; + +default : +BUG (); + }; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_seg %llu, end_seg %llu\n" , + start_seg, end_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_segbmap_reserve_clean_segment (segbmap, + start_seg, end_seg, + reserved_seg, &end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("segbmap init failed: " +"err %d\n" , err); + goto finish_search; + } + + err =ssdfs_segbmap_reserve_clean_segment (segbmap, + start_seg, end_seg, + reserved_seg, + &end); + } + +if (err == -ENODATA ) { + err = -ENOENT ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to reserve segment: " +"type %#x, start_seg %llu, end_seg %llu\n" , + sb_seg_type, start_seg, end_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_search; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to reserve segment: " +"type %#x, start_seg %llu, " +"end_seg %llu, err %d\n" , + sb_seg_type, start_seg, end_seg, err); + goto finish_search; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("reserved_seg %llu\n" , *reserved_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + + leb_id =ssdfs_get_leb_id_for_peb_index (fsi, *reserved_seg, i); +if (leb_id >=U64_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("fail to convert PEB index into LEB ID: " +"seg %llu, peb_index %u\n" , + *reserved_seg, i); + goto finish_search; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("leb_id %llu\n" , leb_id); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_maptbl_map_leb2peb (fsi, leb_id, peb_type, + &pebr, &end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("maptbl init failed: " +"err %d\n" , err); + goto finish_search; + } + + err =ssdfs_maptbl_map_leb2peb (fsi, leb_id, + peb_type, + &pebr, &end); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to map LEB to PEB: " +"reserved_seg %llu, leb_id %llu, " +"err %d\n" , + *reserved_seg, leb_id, err); + goto finish_search; + } + +finish_search: +if (err == -ENOENT ) + *reserved_seg = end_seg; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("reserved_seg %llu\n" , *reserved_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} +#endif /* CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET */ + +#ifndef CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET +static int ssdfs_reserve_clean_segment (struct super_block *sb, +int sb_seg_type,u64 start_leb, +u64 *reserved_seg) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +u64 start_search_id; +u64 cur_id; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!reserved_seg); +BUG_ON (sb_seg_type >=SSDFS_SB_SEG_COPY_MAX ); + +SSDFS_DBG ("sb %p, sb_seg_type %#x, start_leb %llu\n" , + sb, sb_seg_type, start_leb); +#endif /* CONFIG_SSDFS_DEBUG */ + + *reserved_seg =U64_MAX ; + + start_leb =ssdfs_correct_start_leb_id (fsi, +SSDFS_SB_SEG_TYPE , + start_leb); + + start_search_id =SSDFS_LEB2SEG (fsi, start_leb); +if (start_search_id >= fsi->nsegs) + start_search_id =0 ; + + cur_id = start_search_id; + +while (cur_id < fsi->nsegs) { + err =__ssdfs_reserve_clean_segment (fsi, sb_seg_type, + cur_id, reserved_seg); +if (err == -ENOENT ) { + err =0 ; + cur_id = *reserved_seg; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_id %llu\n" , cur_id); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find a new segment: " +"cur_id %llu, err %d\n" , + cur_id, err); +return err; + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found seg_id %llu\n" , *reserved_seg); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + } + } + + cur_id =0 ; + +while (cur_id < start_search_id) { + err =__ssdfs_reserve_clean_segment (fsi, sb_seg_type, + cur_id, reserved_seg); +if (err == -ENOENT ) { + err =0 ; + cur_id = *reserved_seg; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_id %llu\n" , cur_id); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find a new segment: " +"cur_id %llu, err %d\n" , + cur_id, err); +return err; + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found seg_id %llu\n" , *reserved_seg); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("no free space for a new segment\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return -ENOSPC ; +} +#endif /* CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET */ + +typedef u64 sb_pebs_array [SSDFS_SB_CHAIN_MAX ][SSDFS_SB_SEG_COPY_MAX ]; + +static int ssdfs_erase_dirty_prev_sb_segs (struct ssdfs_fs_info *fsi, +u64 prev_leb) +{ +struct completion *init_end; +u8 peb_type =SSDFS_MAPTBL_UNKNOWN_PEB_TYPE ; +u32 pebs_per_seg; +u64 seg_id; +u64 cur_leb; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi); + +SSDFS_DBG ("fsi %p, prev_leb %llu\n" , + fsi, prev_leb); +#endif /* CONFIG_SSDFS_DEBUG */ + + peb_type =SEG2PEB_TYPE (SSDFS_SB_SEG_TYPE ); + pebs_per_seg = fsi->pebs_per_seg; + + seg_id =SSDFS_LEB2SEG (fsi, prev_leb); +if (seg_id >=U64_MAX ) { +SSDFS_ERR ("invalid seg_id for leb_id %llu\n" , + prev_leb); +return -ERANGE ; + } + + cur_leb =ssdfs_get_leb_id_for_peb_index (fsi, seg_id,0 ); +if (cur_leb >=U64_MAX ) { +SSDFS_ERR ("invalid leb_id for seg_id %llu\n" , + seg_id); +return -ERANGE ; + } + + err =ssdfs_maptbl_erase_reserved_peb_now (fsi, + cur_leb, + peb_type, + &init_end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (init_end); +if (unlikely (err)) { +SSDFS_ERR ("maptbl init failed: " +"err %d\n" , err); +return err; + } + + err =ssdfs_maptbl_erase_reserved_peb_now (fsi, + cur_leb, + peb_type, + &init_end); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to erase reserved dirty PEB: " +"leb_id %llu, err %d\n" , + cur_leb, err); +return err; + } + +return 0 ; +} + +#ifdef CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET +static int ssdfs_move_on_first_peb_next_sb_seg (struct super_block *sb, +int sb_seg_type, +sb_pebs_array *sb_lebs, +sb_pebs_array *sb_pebs) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +u64 prev_leb, cur_leb, next_leb, reserved_leb; +u64 prev_peb, cur_peb, next_peb, reserved_peb; +loff_t offset; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !sb_lebs || !sb_pebs); + +if (sb_seg_type >=SSDFS_SB_SEG_COPY_MAX ) { +SSDFS_ERR ("invalid sb_seg_type %#x\n" , + sb_seg_type); +return -EINVAL ; + } + +SSDFS_DBG ("sb %p, sb_seg_type %#x\n" , sb, sb_seg_type); +#endif /* CONFIG_SSDFS_DEBUG */ + + prev_leb = (*sb_lebs)[SSDFS_PREV_SB_SEG ][sb_seg_type]; + cur_leb = (*sb_lebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + next_leb = (*sb_lebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type]; + reserved_leb = (*sb_lebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type]; + + prev_peb = (*sb_pebs)[SSDFS_PREV_SB_SEG ][sb_seg_type]; + cur_peb = (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + next_peb = (*sb_pebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type]; + reserved_peb = (*sb_pebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_peb %llu, next_peb %llu, " +"cur_leb %llu, next_leb %llu\n" , + cur_peb, next_peb, cur_leb, next_leb); +#endif /* CONFIG_SSDFS_DEBUG */ + + (*sb_lebs)[SSDFS_CUR_SB_SEG ][sb_seg_type] = next_leb; + (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type] = next_peb; + +if (prev_leb >=U64_MAX ) { + (*sb_lebs)[SSDFS_PREV_SB_SEG ][sb_seg_type] = cur_leb; + (*sb_pebs)[SSDFS_PREV_SB_SEG ][sb_seg_type] = cur_peb; + + (*sb_lebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type] = reserved_leb; + (*sb_pebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type] = reserved_peb; + + (*sb_lebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type] =U64_MAX ; + (*sb_pebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type] =U64_MAX ; + }else { + err =ssdfs_erase_dirty_prev_sb_segs (fsi, prev_leb); +if (unlikely (err)) { +SSDFS_ERR ("fail erase dirty PEBs: " +"prev_leb %llu, err %d\n" , + prev_leb, err); + goto finish_move_sb_seg; + } + + (*sb_lebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type] = prev_leb; + (*sb_pebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type] = prev_peb; + + (*sb_lebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type] =U64_MAX ; + (*sb_pebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type] =U64_MAX ; + + (*sb_lebs)[SSDFS_PREV_SB_SEG ][sb_seg_type] = cur_leb; + (*sb_pebs)[SSDFS_PREV_SB_SEG ][sb_seg_type] = cur_peb; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_leb %llu, cur_peb %llu, " +"next_leb %llu, next_peb %llu, " +"reserved_leb %llu, reserved_peb %llu, " +"prev_leb %llu, prev_peb %llu\n" , + (*sb_lebs)[SSDFS_CUR_SB_SEG ][sb_seg_type], + (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type], + (*sb_lebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type], + (*sb_pebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type], + (*sb_lebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type], + (*sb_pebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type], + (*sb_lebs)[SSDFS_PREV_SB_SEG ][sb_seg_type], + (*sb_pebs)[SSDFS_PREV_SB_SEG ][sb_seg_type]); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (fsi->is_zns_device) { + cur_peb = (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + offset = cur_peb * fsi->erasesize; + + err = fsi->devops->open_zone (fsi->sb, offset); +if (unlikely (err)) { +SSDFS_ERR ("fail to open zone: " +"offset %llu, err %d\n" , + offset, err); +return err; + } + } + +finish_move_sb_seg: +return err; +} +#else +static int ssdfs_move_on_first_peb_next_sb_seg (struct super_block *sb, +int sb_seg_type, +sb_pebs_array *sb_lebs, +sb_pebs_array *sb_pebs) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +struct ssdfs_segment_bmap *segbmap = fsi->segbmap; +struct ssdfs_maptbl_cache *maptbl_cache = &fsi->maptbl_cache; +u64 prev_leb, cur_leb, next_leb, reserved_leb; +u64 prev_peb, cur_peb, next_peb, reserved_peb; +u64 new_leb =U64_MAX , new_peb =U64_MAX ; +u64 reserved_seg; +u64 prev_seg, cur_seg; +struct ssdfs_maptbl_peb_relation pebr; +u8 peb_type =SSDFS_MAPTBL_SBSEG_PEB_TYPE ; +struct completion *end =NULL ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !sb_lebs || !sb_pebs); + +if (sb_seg_type >=SSDFS_SB_SEG_COPY_MAX ) { +SSDFS_ERR ("invalid sb_seg_type %#x\n" , + sb_seg_type); +return -EINVAL ; + } + +SSDFS_DBG ("sb %p, sb_seg_type %#x\n" , sb, sb_seg_type); +#endif /* CONFIG_SSDFS_DEBUG */ + + prev_leb = (*sb_lebs)[SSDFS_PREV_SB_SEG ][sb_seg_type]; + cur_leb = (*sb_lebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + next_leb = (*sb_lebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type]; + reserved_leb = (*sb_lebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type]; + + prev_peb = (*sb_pebs)[SSDFS_PREV_SB_SEG ][sb_seg_type]; + cur_peb = (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + next_peb = (*sb_pebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type]; + reserved_peb = (*sb_pebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_peb %llu, next_peb %llu, " +"cur_leb %llu, next_leb %llu\n" , + cur_peb, next_peb, cur_leb, next_leb); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_reserve_clean_segment (sb, sb_seg_type, cur_leb, + &reserved_seg); +if (unlikely (err)) { +SSDFS_ERR ("fail to reserve clean segment: err %d\n" , err); + goto finish_move_sb_seg; + } + + new_leb =ssdfs_get_leb_id_for_peb_index (fsi, reserved_seg,0 ); +if (new_leb >=U64_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("fail to convert PEB index into LEB ID: " +"seg %llu\n" , reserved_seg); + goto finish_move_sb_seg; + } + + err =ssdfs_maptbl_convert_leb2peb (fsi, new_leb, + peb_type, + &pebr, &end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("maptbl init failed: " +"err %d\n" , err); + goto finish_move_sb_seg; + } + + err =ssdfs_maptbl_convert_leb2peb (fsi, new_leb, + peb_type, + &pebr, &end); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to convert LEB %llu to PEB: err %d\n" , + new_leb, err); + goto finish_move_sb_seg; + } + + new_peb = pebr.pebs[SSDFS_MAPTBL_MAIN_INDEX ].peb_id; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (new_peb ==U64_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + (*sb_lebs)[SSDFS_PREV_SB_SEG ][sb_seg_type] = cur_leb; + (*sb_pebs)[SSDFS_PREV_SB_SEG ][sb_seg_type] = cur_peb; + + (*sb_lebs)[SSDFS_CUR_SB_SEG ][sb_seg_type] = next_leb; + (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type] = next_peb; + + (*sb_lebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type] = reserved_leb; + (*sb_pebs)[SSDFS_NEXT_SB_SEG ][sb_seg_type] = reserved_peb; + + (*sb_lebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type] = new_leb; + (*sb_pebs)[SSDFS_RESERVED_SB_SEG ][sb_seg_type] = new_peb; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_leb %llu, cur_peb %llu, " +"next_leb %llu, next_peb %llu, " +"reserved_leb %llu, reserved_peb %llu, " +"new_leb %llu, new_peb %llu\n" , + cur_leb, cur_peb, + next_leb, next_peb, + reserved_leb, reserved_peb, + new_leb, new_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (prev_leb ==U64_MAX ) + goto finish_move_sb_seg; + + prev_seg =SSDFS_LEB2SEG (fsi, prev_leb); + cur_seg =SSDFS_LEB2SEG (fsi, cur_leb); + +if (prev_seg != cur_seg) { + err =ssdfs_segbmap_change_state (segbmap, prev_seg, +SSDFS_SEG_DIRTY , &end); +if (err == -EAGAIN ) { + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("segbmap init failed: " +"err %d\n" , err); + goto finish_move_sb_seg; + } + + err =ssdfs_segbmap_change_state (segbmap, prev_seg, +SSDFS_SEG_DIRTY , &end); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to change segment state: " +"seg %llu, state %#x, err %d\n" , + prev_seg,SSDFS_SEG_DIRTY , err); + goto finish_move_sb_seg; + } + } + + err =ssdfs_maptbl_wait_and_change_peb_state (fsi, prev_leb, peb_type, +SSDFS_MAPTBL_DIRTY_PEB_STATE ); +if (unlikely (err)) { +SSDFS_ERR ("fail to change the PEB state: " +"leb_id %llu, new_state %#x, err %d\n" , + prev_leb,SSDFS_MAPTBL_DIRTY_PEB_STATE , err); + goto finish_move_sb_seg; + } + + err =ssdfs_maptbl_cache_forget_leb2peb (maptbl_cache, prev_leb, +SSDFS_PEB_STATE_CONSISTENT ); +if (unlikely (err)) { +SSDFS_ERR ("fail to forget prev_leb %llu, err %d\n" , + prev_leb, err); + goto finish_move_sb_seg; + } + +finish_move_sb_seg: +return err; +} +#endif /* CONFIG_SSDFS_FIXED_SUPERBLOCK_SEGMENTS_SET */ + +static int ssdfs_move_on_next_sb_seg (struct super_block *sb, +int sb_seg_type, +sb_pebs_array *sb_lebs, +sb_pebs_array *sb_pebs) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +u64 cur_leb, next_leb; +u64 cur_peb; +u8 peb_type =SSDFS_MAPTBL_SBSEG_PEB_TYPE ; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !sb_lebs || !sb_pebs); + +if (sb_seg_type >=SSDFS_SB_SEG_COPY_MAX ) { +SSDFS_ERR ("invalid sb_seg_type %#x\n" , + sb_seg_type); +return -EINVAL ; + } + +SSDFS_DBG ("sb %p, sb_seg_type %#x\n" , sb, sb_seg_type); +#endif /* CONFIG_SSDFS_DEBUG */ + + cur_leb = (*sb_lebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + cur_peb = (*sb_pebs)[SSDFS_CUR_SB_SEG ][sb_seg_type]; + + next_leb = cur_leb +1 ; + + err =ssdfs_maptbl_wait_and_change_peb_state (fsi, cur_leb, peb_type, +SSDFS_MAPTBL_USED_PEB_STATE ); +if (unlikely (err)) { +SSDFS_ERR ("fail to change the PEB state: " +"leb_id %llu, new_state %#x, err %d\n" , + cur_leb,SSDFS_MAPTBL_USED_PEB_STATE , err); +return err; + } + + err =ssdfs_move_on_first_peb_next_sb_seg (sb, sb_seg_type, + sb_lebs, sb_pebs); +if (unlikely (err)) { +SSDFS_ERR ("fail to move on next sb segment: " +"sb_seg_type %#x, cur_leb %llu, " +"cur_peb %llu, err %d\n" , + sb_seg_type, cur_leb, + cur_peb, err); +return err; + } + +return 0 ; +} + +static int ssdfs_snapshot_new_sb_pebs_array (struct super_block *sb) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_peb_extent *last_log; +struct ssdfs_segment_request *req; +struct ssdfs_requests_queue *rq; +u64 offset; +u32 log_blocks; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb); + +SSDFS_DBG ("sb %p" , sb); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + + fsi->sb_snapi.need_snapshot_sb = true; + + last_log = &fsi->sb_snapi.last_log; + offset = last_log->page_offset + last_log->pages_count; + log_blocks = last_log->pages_count; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_log (page_offset %u, pages_count %u)\n" , + last_log->page_offset, last_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (offset > fsi->pages_per_peb) { +SSDFS_ERR ("last log has inconsistent state: " +"offset %llu > pages_per_peb %u\n" , + offset, fsi->pages_per_peb); +return -ERANGE ; + }else if ((offset + log_blocks) > fsi->pages_per_peb) { + req =ssdfs_request_alloc (); +if (IS_ERR_OR_NULL (req)) { + err = (req ==NULL ? -ENOMEM :PTR_ERR (req)); +SSDFS_ERR ("fail to allocate segment request: err %d\n" , + err); +return err; + } + +ssdfs_request_init (req, fsi->pagesize); +ssdfs_get_request (req); + +ssdfs_request_prepare_internal_data (SSDFS_GLOBAL_FSCK_REQ , +SSDFS_FSCK_ERASE_RE_WRITE_SB_SNAP_SEG , +SSDFS_REQ_ASYNC_NO_FREE , req); + + rq = &fsi->global_fsck.rq; +ssdfs_requests_queue_add_tail_inc (fsi, rq, req); + fsi->sb_snapi.req = req; +wake_up_all (&fsi->global_fsck.wait_queue); + + last_log->page_offset =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_log (page_offset %u, pages_count %u)\n" , + last_log->page_offset, last_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return 0 ; +} + +static int ssdfs_move_on_next_sb_segs_pair (struct super_block *sb) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +sb_pebs_array sb_lebs; +sb_pebs_array sb_pebs; +size_t array_size; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sb %p" , sb); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!(fsi->fs_feature_compat &SSDFS_HAS_SEGBMAP_COMPAT_FLAG ) || + !(fsi->fs_feature_compat &SSDFS_HAS_MAPTBL_COMPAT_FLAG )) { +SSDFS_ERR ("volume hasn't segbmap or maptbl\n" ); +return -EIO ; + } + + array_size =sizeof (u64); + array_size *=SSDFS_SB_CHAIN_MAX ; + array_size *=SSDFS_SB_SEG_COPY_MAX ; + +down_read (&fsi->sb_segs_sem); +ssdfs_memcpy (sb_lebs,0 , array_size, + fsi->sb_lebs,0 , array_size, + array_size); +ssdfs_memcpy (sb_pebs,0 , array_size, + fsi->sb_pebs,0 , array_size, + array_size); +up_read (&fsi->sb_segs_sem); + +for (i =0 ; i <SSDFS_SB_SEG_COPY_MAX ; i++) { + err =ssdfs_move_on_next_sb_seg (sb, i, &sb_lebs, &sb_pebs); +if (unlikely (err)) { +SSDFS_ERR ("fail to move on next sb PEB: err %d\n" , + err); +return err; + } + } + +down_write (&fsi->sb_segs_sem); +ssdfs_memcpy (fsi->sb_lebs,0 , array_size, + sb_lebs,0 , array_size, + array_size); +ssdfs_memcpy (fsi->sb_pebs,0 , array_size, + sb_pebs,0 , array_size, + array_size); +up_write (&fsi->sb_segs_sem); + + err =ssdfs_snapshot_new_sb_pebs_array (sb); +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare snapshot of new superblock chain: " +"err %d\n" , err); +return err; + } + +return 0 ; +} + +static +int ssdfs_prepare_sb_log (struct super_block *sb, +struct ssdfs_peb_extent *last_sb_log) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !last_sb_log); + +SSDFS_DBG ("sb %p, last_sb_log %p\n" , + sb, last_sb_log); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_define_next_sb_log_place (sb, last_sb_log); +switch (err) { +case -EFBIG :/* current sb segment is exhausted */ +case -EIO :/* current sb segment is corrupted */ + err =ssdfs_move_on_next_sb_segs_pair (sb); +if (err) { +SSDFS_ERR ("fail to move on next sb segs pair: err %d\n" , + err); +return err; + } + err =ssdfs_define_next_sb_log_place (sb, last_sb_log); +if (unlikely (err)) { +SSDFS_ERR ("unable to define next sb log place: err %d\n" , + err); +return err; + } +break ; + +default : +if (err) { +SSDFS_ERR ("unable to define next sb log place: err %d\n" , + err); +return err; + } +break ; + } + +return 0 ; +} + +static void +ssdfs_prepare_maptbl_cache_descriptor (struct ssdfs_metadata_descriptor *desc, +u32 offset, +struct ssdfs_payload_content *payload, +u32 payload_size) +{ +unsigned i; +u32 csum = ~0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!desc || !payload); + +SSDFS_DBG ("desc %p, offset %u, payload %p\n" , + desc, offset, payload); +#endif /* CONFIG_SSDFS_DEBUG */ + + desc->offset =cpu_to_le32 (offset); + desc->size =cpu_to_le32 (payload_size); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (payload_size >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + desc->check.bytes =cpu_to_le16 ((u16 )payload_size); + desc->check.flags =cpu_to_le16 (SSDFS_CRC32 ); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (folio_batch_count (&payload->batch) ==0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <folio_batch_count (&payload->batch); i++) { +struct folio *folio = payload->batch.folios[i]; +struct ssdfs_maptbl_cache_header *hdr; +void *kaddr; +u16 bytes_count; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); + + hdr = (struct ssdfs_maptbl_cache_header *)kaddr; + bytes_count =le16_to_cpu (hdr->bytes_count); + + csum =crc32 (csum, kaddr, bytes_count); + +kunmap_local (kaddr); +ssdfs_folio_unlock (folio); + } + + desc->check.csum =cpu_to_le32 (csum); +} + +static +int ssdfs_prepare_snapshot_rules_for_commit (struct ssdfs_fs_info *fsi, +struct ssdfs_metadata_descriptor *desc, +u32 offset) +{ +struct ssdfs_snapshot_rules_header *hdr; +size_t hdr_size =sizeof (struct ssdfs_snapshot_rules_header ); +size_t info_size =sizeof (struct ssdfs_snapshot_rule_info ); +struct ssdfs_snapshot_rule_item *item =NULL ; +u32 payload_off; +u32 item_off; +u32 pagesize = fsi->pagesize; +u16 items_count =0 ; +u16 items_capacity =0 ; +u32 area_size =0 ; +struct list_head *this, *next; +u32 csum = ~0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !desc); + +SSDFS_DBG ("fsi %p, offset %u\n" , + fsi, offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_ssdfs_snapshot_rules_list_empty (&fsi->snapshots.rules_list)) { +SSDFS_DBG ("snapshot rules list is empty\n" ); +return -ENODATA ; + } + + payload_off = offsetof(struct ssdfs_log_footer , payload); + hdr =SSDFS_SNRU_HDR ((u8 *)fsi->sbi.vs_buf + payload_off); +memset (hdr,0 , hdr_size); + area_size = pagesize - payload_off; + item_off = payload_off + hdr_size; + + items_capacity = (u16)((area_size - hdr_size) / info_size); + area_size =min_t (u32, area_size, (u32 )items_capacity * info_size); + +spin_lock (&fsi->snapshots.rules_list.lock); +list_for_each_safe (this, next, &fsi->snapshots.rules_list.list) { + item = list_entry(this, struct ssdfs_snapshot_rule_item, list); + + err =ssdfs_memcpy (fsi->sbi.vs_buf, item_off, pagesize, + &item->rule,0 , info_size, + info_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_copy_items; + } + + item_off += info_size; + items_count++; + } +finish_copy_items: +spin_unlock (&fsi->snapshots.rules_list.lock); + +if (unlikely (err)) +return err; + + hdr->magic =cpu_to_le32 (SSDFS_SNAPSHOT_RULES_MAGIC ); + hdr->item_size =cpu_to_le16 (info_size); + hdr->flags =cpu_to_le16 (0 ); + +if (items_count ==0 || items_count > items_capacity) { +SSDFS_ERR ("invalid items number: " +"items_count %u, items_capacity %u, " +"area_size %u, item_size %zu\n" , + items_count, items_capacity, + area_size, info_size); +return -ERANGE ; + } + + hdr->items_count =cpu_to_le16 (items_count); + hdr->items_capacity =cpu_to_le16 (items_capacity); + hdr->area_size =cpu_to_le32 (area_size); + + desc->offset =cpu_to_le32 (offset); + desc->size =cpu_to_le32 (area_size); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (area_size >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + desc->check.bytes =cpu_to_le16 (area_size); + desc->check.flags =cpu_to_le16 (SSDFS_CRC32 ); + + csum =crc32 (csum, hdr, area_size); + desc->check.csum =cpu_to_le32 (csum); + +return 0 ; +} + +static int __ssdfs_commit_sb_log (struct super_block *sb, +u64 timestamp,u64 cno, +struct ssdfs_peb_extent *last_sb_log, +struct ssdfs_sb_log_payload *payload) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_metadata_descriptor hdr_desc[SSDFS_SEG_HDR_DESC_MAX ]; +struct ssdfs_metadata_descriptor footer_desc[SSDFS_LOG_FOOTER_DESC_MAX ]; +size_t desc_size =sizeof (struct ssdfs_metadata_descriptor ); +size_t hdr_array_bytes = desc_size *SSDFS_SEG_HDR_DESC_MAX ; +size_t footer_array_bytes = desc_size *SSDFS_LOG_FOOTER_DESC_MAX ; +struct ssdfs_metadata_descriptor *cur_hdr_desc; +struct folio *folio; +struct ssdfs_segment_header *hdr; +size_t hdr_size =sizeof (struct ssdfs_segment_header ); +struct ssdfs_log_footer *footer; +size_t footer_size =sizeof (struct ssdfs_log_footer ); +#ifdef CONFIG_SSDFS_DEBUG +void *kaddr =NULL ; +#endif /* CONFIG_SSDFS_DEBUG */ +loff_t peb_offset; +loff_t sb_offset, sb_snap_offset; +u32 flags =0 ; +u32 written =0 ; +u64 seg_id; +u32 log_pages_count; +unsigned i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !last_sb_log); +BUG_ON (!SSDFS_FS_I (sb)->devops); +BUG_ON (!SSDFS_FS_I (sb)->devops->write_block); +BUG_ON ((last_sb_log->page_offset + last_sb_log->pages_count) > + (ULLONG_MAX >>PAGE_SHIFT )); +BUG_ON ((last_sb_log->leb_id *SSDFS_FS_I (sb)->pebs_per_seg) >= +SSDFS_FS_I (sb)->nsegs); +BUG_ON (last_sb_log->peb_id > +div_u64 (ULLONG_MAX ,SSDFS_FS_I (sb)->pages_per_peb)); +BUG_ON ((last_sb_log->peb_id *SSDFS_FS_I (sb)->pages_per_peb) > + (ULLONG_MAX >>SSDFS_FS_I (sb)->log_pagesize)); + +SSDFS_DBG ("sb %p, last_sb_log->leb_id %llu, last_sb_log->peb_id %llu, " +"last_sb_log->page_offset %u, last_sb_log->pages_count %u\n" , + sb, last_sb_log->leb_id, last_sb_log->peb_id, + last_sb_log->page_offset, last_sb_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + hdr =SSDFS_SEG_HDR (fsi->sbi.vh_buf); + footer =SSDFS_LF (fsi->sbi.vs_buf); + +memset (hdr_desc,0 , hdr_array_bytes); +memset (footer_desc,0 , footer_array_bytes); + + sb_offset = (loff_t )last_sb_log->page_offset <<PAGE_SHIFT ; + sb_offset +=PAGE_SIZE ; + + cur_hdr_desc = &hdr_desc[SSDFS_MAPTBL_CACHE_INDEX ]; +ssdfs_prepare_maptbl_cache_descriptor (cur_hdr_desc, (u32 )sb_offset, + &payload->maptbl_cache, + payload->maptbl_cache.bytes_count); + + sb_offset += payload->maptbl_cache.bytes_count; + + cur_hdr_desc = &hdr_desc[SSDFS_LOG_FOOTER_INDEX ]; + cur_hdr_desc->offset =cpu_to_le32 (sb_offset); + cur_hdr_desc->size =cpu_to_le32 (footer_size); + +ssdfs_memcpy (hdr->desc_array,0 , hdr_array_bytes, + hdr_desc,0 , hdr_array_bytes, + hdr_array_bytes); + + hdr->peb_migration_id[SSDFS_PREV_MIGRATING_PEB ] = +SSDFS_PEB_UNKNOWN_MIGRATION_ID ; + hdr->peb_migration_id[SSDFS_CUR_MIGRATING_PEB ] = +SSDFS_PEB_UNKNOWN_MIGRATION_ID ; + + seg_id =ssdfs_get_seg_id_for_leb_id (fsi, last_sb_log->leb_id); + + log_pages_count =PAGE_SIZE ;/* header size */ + log_pages_count += payload->maptbl_cache.bytes_count; + log_pages_count +=PAGE_SIZE ;/* footer size */ + log_pages_count >>=PAGE_SHIFT ; + + err =ssdfs_prepare_segment_header_for_commit (fsi, + seg_id, + last_sb_log->leb_id, + last_sb_log->peb_id, +U64_MAX , + log_pages_count, +SSDFS_SB_SEG_TYPE , +SSDFS_LOG_HAS_FOOTER | +SSDFS_LOG_HAS_MAPTBL_CACHE , + timestamp, cno, + hdr); +if (err) { +SSDFS_ERR ("fail to prepare segment header: err %d\n" , err); +return err; + } + + sb_offset += offsetof(struct ssdfs_log_footer , payload); + cur_hdr_desc = &footer_desc[SSDFS_SNAPSHOT_RULES_AREA_INDEX ]; + + err =ssdfs_prepare_snapshot_rules_for_commit (fsi, cur_hdr_desc, + (u32 )sb_offset); +if (err == -ENODATA ) { + err =0 ; +SSDFS_DBG ("snapshot rules list is empty\n" ); + }else if (err) { +SSDFS_ERR ("fail to prepare snapshot rules: err %d\n" , err); +return err; + }else + flags |=SSDFS_LOG_FOOTER_HAS_SNAPSHOT_RULES ; + +ssdfs_memcpy (footer->desc_array,0 , footer_array_bytes, + footer_desc,0 , footer_array_bytes, + footer_array_bytes); + + err =ssdfs_prepare_log_footer_for_commit (fsi,PAGE_SIZE , + log_pages_count, + flags, timestamp, + cno, footer); +if (err) { +SSDFS_ERR ("fail to prepare log footer: err %d\n" , err); +return err; + } + + folio =ssdfs_super_alloc_folio (GFP_KERNEL | __GFP_ZERO, +get_order (PAGE_SIZE )); +if (IS_ERR_OR_NULL (folio)) { + err = (folio ==NULL ? -ENOMEM :PTR_ERR (folio)); +SSDFS_ERR ("unable to allocate memory folio\n" ); +return err; + } + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +/* write segment header */ +ssdfs_folio_lock (folio); +__ssdfs_memcpy_to_folio (folio,0 ,PAGE_SIZE , + fsi->sbi.vh_buf,0 , hdr_size, + hdr_size); +folio_mark_uptodate (folio); +folio_set_dirty (folio); +ssdfs_folio_unlock (folio); + + peb_offset = last_sb_log->peb_id * fsi->pages_per_peb; + peb_offset <<= fsi->log_pagesize; + sb_offset = (loff_t )last_sb_log->page_offset <<PAGE_SHIFT ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (peb_offset > (ULLONG_MAX - (sb_offset +PAGE_SIZE ))); +#endif /* CONFIG_SSDFS_DEBUG */ + + sb_offset += peb_offset; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err = fsi->devops->write_block (sb, sb_offset, folio); +if (err) { +SSDFS_ERR ("fail to write segment header: " +"offset %llu, size %zu\n" , + (u64 )sb_offset, hdr_size); + goto cleanup_after_failure; + } + +if (fsi->sb_snapi.need_snapshot_sb) { +struct ssdfs_peb_extent *last_sb_snap_log; + + last_sb_snap_log = &fsi->sb_snapi.last_log; + + peb_offset = last_sb_snap_log->peb_id * fsi->pages_per_peb; + peb_offset <<= fsi->log_pagesize; + sb_snap_offset = + (loff_t )last_sb_snap_log->page_offset <<PAGE_SHIFT ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (peb_offset > (ULLONG_MAX - (sb_snap_offset +PAGE_SIZE ))); +#endif /* CONFIG_SSDFS_DEBUG */ + + sb_snap_offset += peb_offset; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_snap_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +ssdfs_folio_lock (folio); + hdr =SSDFS_SEG_HDR (kmap_local_folio (folio,0 )); + hdr->seg_id =cpu_to_le64 (SSDFS_INITIAL_SNAPSHOT_SEG_ID ); + hdr->leb_id =cpu_to_le64 (SSDFS_INITIAL_SNAPSHOT_SEG_LEB_ID ); + hdr->peb_id =cpu_to_le64 (SSDFS_INITIAL_SNAPSHOT_SEG_PEB_ID ); + hdr->seg_type =cpu_to_le16 (SSDFS_INITIAL_SNAPSHOT_SEG_TYPE ); + hdr->seg_flags =cpu_to_le32 (SSDFS_LOG_HAS_FOOTER ); + hdr->volume_hdr.check.bytes = +cpu_to_le16 (sizeof (struct ssdfs_segment_header )); + hdr->volume_hdr.check.flags =cpu_to_le16 (SSDFS_CRC32 ); + err =ssdfs_calculate_csum (&hdr->volume_hdr.check, + hdr, +sizeof (struct ssdfs_segment_header )); +if (unlikely (err)) { +SSDFS_ERR ("unable to calculate checksum: err %d\n" , err); + }else { +folio_mark_uptodate (folio); +folio_set_dirty (folio); + } +kunmap_local (hdr); +ssdfs_folio_unlock (folio); + +if (err) + goto cleanup_after_failure; + + err = fsi->devops->write_block (sb, sb_snap_offset, folio); +if (err) { +SSDFS_ERR ("fail to write segment header: " +"offset %llu, size %zu\n" , + (u64 )sb_snap_offset, + hdr_size); + goto cleanup_after_failure; + } + } + +ssdfs_folio_lock (folio); +folio_clear_uptodate (folio); +ssdfs_folio_unlock (folio); + + sb_offset +=PAGE_SIZE ; + written =0 ; + +for (i =0 ; i <folio_batch_count (&payload->maptbl_cache.batch); i++) { +struct folio *payload_folio = + payload->maptbl_cache.batch.folios[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!payload_folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (payload_folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + payload_folio, +folio_ref_count (payload_folio)); + + kaddr =kmap_local_folio (payload_folio,0 ); +SSDFS_DBG ("PAYLOAD FOLIO %d\n" , i); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + kaddr,PAGE_SIZE ); +kunmap_local (kaddr); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (payload_folio); +folio_mark_uptodate (payload_folio); +folio_set_dirty (payload_folio); +ssdfs_folio_unlock (payload_folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err = fsi->devops->write_block (sb, sb_offset, payload_folio); +if (err) { +SSDFS_ERR ("fail to write maptbl cache page: " +"offset %llu, folio_index %u, size %zu\n" , + (u64 )sb_offset, i,PAGE_SIZE ); + goto cleanup_after_failure; + } + +ssdfs_folio_lock (payload_folio); +folio_clear_uptodate (payload_folio); +ssdfs_folio_unlock (payload_folio); + + sb_offset +=PAGE_SIZE ; + } + +/* TODO: write metadata payload */ + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +/* write log footer */ +ssdfs_folio_lock (folio); +__ssdfs_memset_folio (folio,0 ,PAGE_SIZE , +0 ,PAGE_SIZE ); +__ssdfs_memcpy_to_folio (folio,0 ,PAGE_SIZE , + fsi->sbi.vs_buf,0 , fsi->sbi.vs_buf_size, +PAGE_SIZE ); +folio_mark_uptodate (folio); +folio_set_dirty (folio); +ssdfs_folio_unlock (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err = fsi->devops->write_block (sb, sb_offset, folio); +if (err) { +SSDFS_ERR ("fail to write log footer: " +"offset %llu, size %zu\n" , + (u64 )sb_offset, fsi->sbi.vs_buf_size); + goto cleanup_after_failure; + } + +if (fsi->sb_snapi.need_snapshot_sb) { + sb_snap_offset +=PAGE_SIZE ; + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_snap_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); +folio_mark_uptodate (folio); +folio_set_dirty (folio); +ssdfs_folio_unlock (folio); + + err = fsi->devops->write_block (sb, sb_snap_offset, folio); +if (err) { +SSDFS_ERR ("fail to write log footer: " +"offset %llu, size %zu\n" , + (u64 )sb_snap_offset, fsi->sbi.vs_buf_size); + goto cleanup_after_failure; + } + } + +ssdfs_folio_lock (folio); +folio_clear_uptodate (folio); +ssdfs_folio_unlock (folio); + + fsi->sb_snapi.need_snapshot_sb = false; + +ssdfs_super_free_folio (folio); +return 0 ; + +cleanup_after_failure: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_super_free_folio (folio); + +return err; +} + +static int +__ssdfs_commit_sb_log_inline (struct super_block *sb, +u64 timestamp,u64 cno, +struct ssdfs_peb_extent *last_sb_log, +struct ssdfs_sb_log_payload *payload, +u32 payload_size) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_metadata_descriptor hdr_desc[SSDFS_SEG_HDR_DESC_MAX ]; +struct ssdfs_metadata_descriptor footer_desc[SSDFS_LOG_FOOTER_DESC_MAX ]; +size_t desc_size =sizeof (struct ssdfs_metadata_descriptor ); +size_t hdr_array_bytes = desc_size *SSDFS_SEG_HDR_DESC_MAX ; +size_t footer_array_bytes = desc_size *SSDFS_LOG_FOOTER_DESC_MAX ; +struct ssdfs_metadata_descriptor *cur_hdr_desc; +struct folio *folio; +struct folio *payload_folio; +struct ssdfs_segment_header *hdr; +size_t hdr_size =sizeof (struct ssdfs_segment_header ); +struct ssdfs_log_footer *footer; +size_t footer_size =sizeof (struct ssdfs_log_footer ); +void *kaddr =NULL ; +loff_t peb_offset; +loff_t sb_offset, sb_snap_offset; +u32 inline_capacity; +void *payload_buf; +u32 flags =0 ; +u64 seg_id; +u32 log_pages_count; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !last_sb_log); +BUG_ON (!SSDFS_FS_I (sb)->devops); +BUG_ON (!SSDFS_FS_I (sb)->devops->write_block); +BUG_ON ((last_sb_log->page_offset + last_sb_log->pages_count) > + (ULLONG_MAX >>PAGE_SHIFT )); +BUG_ON ((last_sb_log->leb_id *SSDFS_FS_I (sb)->pebs_per_seg) >= +SSDFS_FS_I (sb)->nsegs); +BUG_ON (last_sb_log->peb_id > +div_u64 (ULLONG_MAX ,SSDFS_FS_I (sb)->pages_per_peb)); +BUG_ON ((last_sb_log->peb_id *SSDFS_FS_I (sb)->pages_per_peb) > + (ULLONG_MAX >>SSDFS_FS_I (sb)->log_pagesize)); + +SSDFS_DBG ("sb %p, last_sb_log->leb_id %llu, last_sb_log->peb_id %llu, " +"last_sb_log->page_offset %u, last_sb_log->pages_count %u\n" , + sb, last_sb_log->leb_id, last_sb_log->peb_id, + last_sb_log->page_offset, last_sb_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi =SSDFS_FS_I (sb); + hdr =SSDFS_SEG_HDR (fsi->sbi.vh_buf); + footer =SSDFS_LF (fsi->sbi.vs_buf); + +memset (hdr_desc,0 , hdr_array_bytes); +memset (footer_desc,0 , footer_array_bytes); + + sb_offset = (loff_t )last_sb_log->page_offset <<PAGE_SHIFT ; + sb_offset += hdr_size; + + cur_hdr_desc = &hdr_desc[SSDFS_MAPTBL_CACHE_INDEX ]; +ssdfs_prepare_maptbl_cache_descriptor (cur_hdr_desc, (u32 )sb_offset, + &payload->maptbl_cache, + payload_size); + + sb_offset += payload_size; + + sb_offset +=PAGE_SIZE -1 ; + sb_offset = (sb_offset >>PAGE_SHIFT ) <<PAGE_SHIFT ; + + cur_hdr_desc = &hdr_desc[SSDFS_LOG_FOOTER_INDEX ]; + cur_hdr_desc->offset =cpu_to_le32 (sb_offset); + cur_hdr_desc->size =cpu_to_le32 (footer_size); + +ssdfs_memcpy (hdr->desc_array,0 , hdr_array_bytes, + hdr_desc,0 , hdr_array_bytes, + hdr_array_bytes); + + hdr->peb_migration_id[SSDFS_PREV_MIGRATING_PEB ] = +SSDFS_PEB_UNKNOWN_MIGRATION_ID ; + hdr->peb_migration_id[SSDFS_CUR_MIGRATING_PEB ] = +SSDFS_PEB_UNKNOWN_MIGRATION_ID ; + + seg_id =ssdfs_get_seg_id_for_leb_id (fsi, last_sb_log->leb_id); + + log_pages_count = hdr_size + payload_size; + log_pages_count +=PAGE_SIZE -1 ; + log_pages_count = (log_pages_count >>PAGE_SHIFT ) <<PAGE_SHIFT ; + log_pages_count +=PAGE_SIZE ;/* footer size */ + log_pages_count >>=PAGE_SHIFT ; + + err =ssdfs_prepare_segment_header_for_commit (fsi, + seg_id, + last_sb_log->leb_id, + last_sb_log->peb_id, +U64_MAX , + log_pages_count, +SSDFS_SB_SEG_TYPE , +SSDFS_LOG_HAS_FOOTER | +SSDFS_LOG_HAS_MAPTBL_CACHE , + timestamp, cno, + hdr); +if (err) { +SSDFS_ERR ("fail to prepare segment header: err %d\n" , err); +return err; + } + + sb_offset += offsetof(struct ssdfs_log_footer , payload); + cur_hdr_desc = &footer_desc[SSDFS_SNAPSHOT_RULES_AREA_INDEX ]; + + err =ssdfs_prepare_snapshot_rules_for_commit (fsi, cur_hdr_desc, + (u32 )sb_offset); +if (err == -ENODATA ) { + err =0 ; +SSDFS_DBG ("snapshot rules list is empty\n" ); + }else if (err) { +SSDFS_ERR ("fail to prepare snapshot rules: err %d\n" , err); +return err; + }else + flags |=SSDFS_LOG_FOOTER_HAS_SNAPSHOT_RULES ; + +ssdfs_memcpy (footer->desc_array,0 , footer_array_bytes, + footer_desc,0 , footer_array_bytes, + footer_array_bytes); + + err =ssdfs_prepare_log_footer_for_commit (fsi,PAGE_SIZE , + log_pages_count, + flags, timestamp, + cno, footer); +if (err) { +SSDFS_ERR ("fail to prepare log footer: err %d\n" , err); +return err; + } + +if (folio_batch_count (&payload->maptbl_cache.batch) !=1 ) { +SSDFS_WARN ("payload contains several memory folios\n" ); +return -ERANGE ; + } + + inline_capacity =PAGE_SIZE - hdr_size; + +if (payload_size > inline_capacity) { +SSDFS_ERR ("payload_size %u > inline_capacity %u\n" , + payload_size, inline_capacity); +return -ERANGE ; + } + + payload_buf =ssdfs_super_kmalloc (inline_capacity,GFP_KERNEL ); +if (!payload_buf) { +SSDFS_ERR ("fail to allocate payload buffer\n" ); +return -ENOMEM ; + } + + folio =ssdfs_super_alloc_folio (GFP_KERNEL | __GFP_ZERO, +get_order (PAGE_SIZE )); +if (IS_ERR_OR_NULL (folio)) { + err = (folio ==NULL ? -ENOMEM :PTR_ERR (folio)); +SSDFS_ERR ("unable to allocate memory folio\n" ); +ssdfs_super_kfree (payload_buf); +return err; + } + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + + payload_folio = payload->maptbl_cache.batch.folios[0 ]; +if (!payload_folio) { + err = -ERANGE ; +SSDFS_ERR ("invalid payload folio\n" ); + goto free_payload_buffer; + } + +ssdfs_folio_lock (payload_folio); + err =__ssdfs_memcpy_from_folio (payload_buf,0 , inline_capacity, + payload_folio,0 ,PAGE_SIZE , + payload_size); +ssdfs_folio_unlock (payload_folio); + +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto free_payload_buffer; + } + +/* write segment header + payload */ +ssdfs_folio_lock (folio); + kaddr =kmap_local_folio (folio,0 ); +ssdfs_memcpy (kaddr,0 ,PAGE_SIZE , + fsi->sbi.vh_buf,0 , hdr_size, + hdr_size); + err =ssdfs_memcpy (kaddr, hdr_size,PAGE_SIZE , + payload_buf,0 , inline_capacity, + payload_size); +flush_dcache_folio (folio); +kunmap_local (kaddr); +if (!err) { +folio_mark_uptodate (folio); +folio_set_dirty (folio); + } +ssdfs_folio_unlock (folio); + +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto free_payload_buffer; + } + +free_payload_buffer: +ssdfs_super_kfree (payload_buf); + +if (unlikely (err)) + goto cleanup_after_failure; + + peb_offset = last_sb_log->peb_id * fsi->pages_per_peb; + peb_offset <<= fsi->log_pagesize; + sb_offset = (loff_t )last_sb_log->page_offset <<PAGE_SHIFT ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (peb_offset > (ULLONG_MAX - (sb_offset +PAGE_SIZE ))); +#endif /* CONFIG_SSDFS_DEBUG */ + + sb_offset += peb_offset; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err = fsi->devops->write_block (sb, sb_offset, folio); +if (err) { +SSDFS_ERR ("fail to write segment header: " +"offset %llu, size %zu\n" , + (u64 )sb_offset, hdr_size + payload_size); + goto cleanup_after_failure; + } + +if (fsi->sb_snapi.need_snapshot_sb) { +struct ssdfs_peb_extent *last_sb_snap_log; + + last_sb_snap_log = &fsi->sb_snapi.last_log; + + peb_offset = last_sb_snap_log->peb_id * fsi->pages_per_peb; + peb_offset <<= fsi->log_pagesize; + sb_snap_offset = + (loff_t )last_sb_snap_log->page_offset <<PAGE_SHIFT ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (peb_offset > (ULLONG_MAX - (sb_snap_offset +PAGE_SIZE ))); +#endif /* CONFIG_SSDFS_DEBUG */ + + sb_snap_offset += peb_offset; + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_snap_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); + hdr =SSDFS_SEG_HDR (kmap_local_folio (folio,0 )); + hdr->seg_id =cpu_to_le64 (SSDFS_INITIAL_SNAPSHOT_SEG_ID ); + hdr->leb_id =cpu_to_le64 (SSDFS_INITIAL_SNAPSHOT_SEG_LEB_ID ); + hdr->peb_id =cpu_to_le64 (SSDFS_INITIAL_SNAPSHOT_SEG_PEB_ID ); + hdr->seg_type =cpu_to_le16 (SSDFS_INITIAL_SNAPSHOT_SEG_TYPE ); + hdr->seg_flags =cpu_to_le32 (SSDFS_LOG_HAS_FOOTER ); + hdr->volume_hdr.check.bytes = +cpu_to_le16 (sizeof (struct ssdfs_segment_header )); + hdr->volume_hdr.check.flags =cpu_to_le16 (SSDFS_CRC32 ); + err =ssdfs_calculate_csum (&hdr->volume_hdr.check, + hdr, +sizeof (struct ssdfs_segment_header )); +if (unlikely (err)) { +SSDFS_ERR ("unable to calculate checksum: err %d\n" , err); + }else { +folio_mark_uptodate (folio); +folio_set_dirty (folio); + } +kunmap_local (hdr); +ssdfs_folio_unlock (folio); + +if (err) + goto cleanup_after_failure; + + err = fsi->devops->write_block (sb, sb_snap_offset, folio); +if (err) { +SSDFS_ERR ("fail to write segment header: " +"offset %llu, size %zu\n" , + (u64 )sb_snap_offset, + hdr_size + payload_size); + goto cleanup_after_failure; + } + } + +ssdfs_folio_lock (folio); +folio_clear_uptodate (folio); +ssdfs_folio_unlock (folio); + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +/* write log footer */ +ssdfs_folio_lock (folio); +__ssdfs_memset_folio (folio,0 ,PAGE_SIZE , +0 ,PAGE_SIZE ); +__ssdfs_memcpy_to_folio (folio,0 ,PAGE_SIZE , + fsi->sbi.vs_buf,0 , fsi->sbi.vs_buf_size, +PAGE_SIZE ); +folio_mark_uptodate (folio); +folio_set_dirty (folio); +ssdfs_folio_unlock (folio); + + sb_offset +=PAGE_SIZE ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err = fsi->devops->write_block (sb, sb_offset, folio); +if (err) { +SSDFS_ERR ("fail to write log footer: " +"offset %llu, size %zu\n" , + (u64 )sb_offset, fsi->sbi.vs_buf_size); + goto cleanup_after_failure; + } + +if (fsi->sb_snapi.need_snapshot_sb) { + sb_snap_offset +=PAGE_SIZE ; + +/* ->writepage() calls put_folio() */ +ssdfs_folio_get (folio); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %llu\n" , sb_snap_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_folio_lock (folio); +folio_mark_uptodate (folio); +folio_set_dirty (folio); +ssdfs_folio_unlock (folio); + + err = fsi->devops->write_block (sb, sb_snap_offset, folio); +if (err) { +SSDFS_ERR ("fail to write log footer: " +"offset %llu, size %zu\n" , + (u64 )sb_snap_offset, fsi->sbi.vs_buf_size); + goto cleanup_after_failure; + } + } + +ssdfs_folio_lock (folio); +folio_clear_uptodate (folio); +ssdfs_folio_unlock (folio); + + fsi->sb_snapi.need_snapshot_sb = false; + +ssdfs_super_free_folio (folio); +return 0 ; + +cleanup_after_failure: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_super_free_folio (folio); + +return err; +} + +static int ssdfs_commit_sb_log (struct super_block *sb, +u64 timestamp,u64 cno, +struct ssdfs_peb_extent *last_sb_log, +struct ssdfs_sb_log_payload *payload) +{ +struct ssdfs_fs_info *fsi =SSDFS_FS_I (sb); +size_t hdr_size =sizeof (struct ssdfs_segment_header ); +u32 inline_capacity; +u32 payload_size; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sb || !last_sb_log || !payload); + +SSDFS_DBG ("sb %p, last_sb_log->leb_id %llu, last_sb_log->peb_id %llu, " +"last_sb_log->page_offset %u, last_sb_log->pages_count %u\n" , + sb, last_sb_log->leb_id, last_sb_log->peb_id, + last_sb_log->page_offset, last_sb_log->pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + inline_capacity =PAGE_SIZE - hdr_size; + payload_size =ssdfs_sb_payload_size (&payload->maptbl_cache.batch); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("inline_capacity %u, payload_size %u\n" , + inline_capacity, payload_size); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (fsi->sb_snapi.req !=NULL ) { +struct ssdfs_segment_request *req = fsi->sb_snapi.req; + +switch (atomic_read (&req->result.state)) { +case SSDFS_REQ_CREATED : +case SSDFS_REQ_STARTED : + fsi->sb_snapi.need_snapshot_sb = false; +break ; + +case SSDFS_REQ_FINISHED : +ssdfs_request_free (fsi->sb_snapi.req,NULL ); + fsi->sb_snapi.req =NULL ; +break ; + +case SSDFS_REQ_FAILED : +SSDFS_ERR ("erase and re-write request failed\n" ); +ssdfs_request_free (fsi->sb_snapi.req,NULL ); + fsi->sb_snapi.req =NULL ; + fsi->sb_snapi.need_snapshot_sb = false; +break ; + +default : +SSDFS_ERR ("invalid result's state %#x\n" , +atomic_read (&req->result.state)); +ssdfs_request_free (fsi->sb_snapi.req,NULL ); + fsi->sb_snapi.req =NULL ; + fsi->sb_snapi.need_snapshot_sb = false; +break ; + } + } + +if (payload_size > inline_capacity) { + err =__ssdfs_commit_sb_log (sb, timestamp, cno, + last_sb_log, payload); + }else { + err =__ssdfs_commit_sb_log_inline (sb, timestamp, cno, + last_sb_log, + payload, payload_size); + } + +if (unlikely (err)) +SSDFS_ERR ("fail to commit sb log: err %d\n" , err); + +return err; +} + static int ssdfs_commit_super(struct super_block *sb, u16 fs_state, struct ssdfs_peb_extent *last_sb_log,
Signature · 1 line
2.34.1
PATCH v2 8/79 ssdfs: segment header + log footer operations
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS is Log-structured File System (LFS). It means that volume is a sequence of segments that can contain one or several erase blocks. Any write operation into erase block is a creation of log. And content of every erase block is a sequence of logs. Log can be full or partial. Full log starts by header and it is finished by footer. The size of full log is fixed and it is defined during mkfs phase. However, tunefs tool can change this value. But if commit operation has not enough data to prepare the full log, then partial log is created. Partial log starts with partial log header and it hasn't footer. The partial log can be imagined like mixture of segment header and log footer.
Segment header can be considered like static superblock info. It contains metadata that not changed at all after volume creation (logical block size, for example) or changed rarely (number of segments in the volume, for example). Log footer can be considered like dynamic part of superblock because it contains frequently updated metadata (for example, root node of inodes b-tree).
--- fs/ssdfs/log_footer.c | 991 ++++++++++++++++++++++++++ fs/ssdfs/volume_header.c | 1431 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 2422 insertions(+) create mode 100644 fs/ssdfs/log_footer.c create mode 100644 fs/ssdfs/volume_header.c
fs/ssdfs/log_footer.c new file +991 -0
@@ -0,0 +1,991 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/log_footer.c - operations with log footer. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#include <linux/kernel.h> +#include <linux/rwsem.h> +#include <linux/pagevec.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "folio_vector.h" +#include "ssdfs.h" +#include "segment_bitmap.h" +#include "folio_array.h" +#include "peb.h" +#include "offset_translation_table.h" +#include "peb_container.h" +#include "segment.h" +#include "current_segment.h" + +#include <trace/events/ssdfs.h> + +/* +* is_ssdfs_volume_state_info_consistent() - check volume state consistency +* @fsi: pointer on shared file system object +* @buf: log header +* @footer: log footer +* @dev_size: partition size in bytes +* +* RETURN: +* [true] - volume state metadata is consistent. +* [false] - volume state metadata is corrupted. +*/ +bool is_ssdfs_volume_state_info_consistent (struct ssdfs_fs_info *fsi, +void *buf, +struct ssdfs_log_footer *footer, +u64 dev_size) +{ +struct ssdfs_signature *magic; +u64 nsegs; +u64 free_pages; +u8 log_segsize =U8_MAX ; +u32 seg_size =U32_MAX ; +u32 page_size =U32_MAX ; +u64 cno =U64_MAX ; +u16 log_pages =U16_MAX ; +u32 log_bytes =U32_MAX ; +u64 pages_count; +u32 pages_per_seg; +u32 remainder; +u16 fs_state; +u16 fs_errors; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!buf || !footer); + +SSDFS_DBG ("buf %p, footer %p, dev_size %llu\n" , + buf, footer, dev_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + magic = (struct ssdfs_signature *)buf; + +if (!is_ssdfs_magic_valid (magic)) { +SSDFS_DBG ("valid magic is not detected\n" ); +return false; + } + +if (__is_ssdfs_segment_header_magic_valid (magic)) { +struct ssdfs_segment_header *hdr; +struct ssdfs_volume_header *vh; + + hdr =SSDFS_SEG_HDR (buf); + vh =SSDFS_VH (buf); + + log_segsize = vh->log_segsize; + seg_size =1 << vh->log_segsize; + page_size =1 << vh->log_pagesize; + cno =le64_to_cpu (hdr->cno); + log_pages =le16_to_cpu (hdr->log_pages); + }else if (is_ssdfs_partial_log_header_magic_valid (magic)) { +struct ssdfs_partial_log_header *pl_hdr; + + pl_hdr =SSDFS_PLH (buf); + + log_segsize = pl_hdr->log_segsize; + seg_size =1 << pl_hdr->log_segsize; + page_size =1 << pl_hdr->log_pagesize; + cno =le64_to_cpu (pl_hdr->cno); + log_pages =le16_to_cpu (pl_hdr->log_pages); + }else { +SSDFS_DBG ("log header is corrupted\n" ); +return false; + } + + nsegs =le64_to_cpu (footer->volume_state.nsegs); + +if (nsegs ==0 || nsegs > (dev_size >> log_segsize)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("invalid nsegs %llu, dev_size %llu, seg_size) %u\n" , + nsegs, dev_size, seg_size); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + + free_pages =le64_to_cpu (footer->volume_state.free_pages); + + pages_count =div_u64_rem (dev_size, page_size, &remainder); +if (remainder) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("dev_size %llu is unaligned on page_size %u\n" , + dev_size, page_size); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +if (free_pages > pages_count) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_pages %llu is greater than pages_count %llu\n" , + free_pages, pages_count); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + + pages_per_seg = seg_size / page_size; +if (nsegs <div_u64 (free_pages, pages_per_seg)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("invalid nsegs %llu, free_pages %llu, " +"pages_per_seg %u\n" , + nsegs, free_pages, pages_per_seg); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + +if (cno >le64_to_cpu (footer->cno)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("create_cno %llu is greater than write_cno %llu\n" , + cno,le64_to_cpu (footer->cno)); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + + log_bytes = (u32 )log_pages * fsi->pagesize; +if (le32_to_cpu (footer->log_bytes) > log_bytes) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hdr log_bytes %u > footer log_bytes %u\n" , + log_bytes, +le32_to_cpu (footer->log_bytes)); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + + fs_state =le16_to_cpu (footer->volume_state.state); +if (fs_state >SSDFS_LAST_KNOWN_FS_STATE ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unknown FS state %#x\n" , + fs_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + + fs_errors =le16_to_cpu (footer->volume_state.errors); +if (fs_errors >SSDFS_LAST_KNOWN_FS_ERROR ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unknown FS error %#x\n" , + fs_errors); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + +return true; +} + +/* +* ssdfs_check_log_footer() - check log footer consistency +* @fsi: pointer on shared file system object +* @buf: log header +* @footer: log footer +* @silent: show error or not? +* +* This function checks consistency of log footer. +* +* RETURN: +* [success] - log footer is consistent. +* [failure] - error code: +* +* %-ENODATA - valid magic doesn't detected. +* %-EIO - log footer is corrupted. +*/ +int ssdfs_check_log_footer (struct ssdfs_fs_info *fsi, +void *buf, +struct ssdfs_log_footer *footer, +bool silent) +{ +struct ssdfs_volume_state *vs; +size_t footer_size =sizeof (struct ssdfs_log_footer ); +u64 dev_size; +bool major_magic_valid, minor_magic_valid; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !buf || !footer); + +SSDFS_DBG ("fsi %p, buf %p, footer %p, silent %#x\n" , + fsi, buf, footer, silent); +#endif /* CONFIG_SSDFS_DEBUG */ + + vs =SSDFS_VS (footer); + + major_magic_valid =is_ssdfs_magic_valid (&vs->magic); + minor_magic_valid =is_ssdfs_log_footer_magic_valid (footer); + +if (!major_magic_valid && !minor_magic_valid) { +if (!silent) +SSDFS_ERR ("valid magic doesn't detected\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("valid magic doesn't detected\n" ); +#else +SSDFS_DBG ("valid magic doesn't detected\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -ENODATA ; + }else if (!major_magic_valid) { +if (!silent) +SSDFS_ERR ("invalid SSDFS magic signature\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid SSDFS magic signature\n" ); +#else +SSDFS_DBG ("invalid SSDFS magic signature\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + }else if (!minor_magic_valid) { +if (!silent) +SSDFS_ERR ("invalid log footer magic signature\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid log footer magic signature\n" ); +#else +SSDFS_DBG ("invalid log footer magic signature\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + +if (!is_ssdfs_log_footer_csum_valid (footer, footer_size)) { +if (!silent) +SSDFS_ERR ("invalid checksum of log footer\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid checksum of log footer\n" ); +#else +SSDFS_DBG ("invalid checksum of log footer\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + + dev_size = fsi->devops->device_size (fsi->sb); +if (!is_ssdfs_volume_state_info_consistent (fsi, buf, + footer, dev_size)) { +if (!silent) +SSDFS_ERR ("log footer is corrupted\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("log footer is corrupted\n" ); +#else +SSDFS_DBG ("log footer is corrupted\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + +if (le32_to_cpu (footer->log_flags) & ~SSDFS_LOG_FOOTER_FLAG_MASK ) { +if (!silent) { +SSDFS_ERR ("corrupted log_flags %#x\n" , +le32_to_cpu (footer->log_flags)); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("corrupted log_flags %#x\n" , +le32_to_cpu (footer->log_flags)); +#else +SSDFS_DBG ("corrupted log_flags %#x\n" , +le32_to_cpu (footer->log_flags)); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + +return 0 ; +} + +/* +* ssdfs_read_unchecked_log_footer() - read log footer without check +* @fsi: pointer on shared file system object +* @peb_id: PEB identification number +* @block_size: block size in bytes +* @bytes_off: offset inside PEB in bytes +* @buf: buffer for log footer +* @silent: show error or not? +* @log_pages: number of pages in the log +* +* This function reads log footer without +* the consistency check. +* +* RETURN: +* [success] - log footer is consistent. +* [failure] - error code: +* +* %-ENODATA - valid magic doesn't detected. +* %-EIO - log footer is corrupted. +*/ +int ssdfs_read_unchecked_log_footer (struct ssdfs_fs_info *fsi, +u64 peb_id,u32 block_size,u32 bytes_off, +void *buf,bool silent, +u32 *log_pages) +{ +struct ssdfs_signature *magic; +struct ssdfs_log_footer *footer; +struct ssdfs_volume_state *vs; +size_t footer_size =sizeof (struct ssdfs_log_footer ); +struct ssdfs_partial_log_header *pl_hdr; +size_t hdr_size =sizeof (struct ssdfs_partial_log_header ); +bool major_magic_valid, minor_magic_valid; +u32 pages_per_peb; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !fsi->devops->read); +BUG_ON (!buf || !log_pages); +BUG_ON (bytes_off >= fsi->erasesize); + +SSDFS_DBG ("peb_id %llu, block_size %u, bytes_off %u, buf %p\n" , + peb_id, block_size, bytes_off, buf); +#endif /* CONFIG_SSDFS_DEBUG */ + + *log_pages =U32_MAX ; + pages_per_peb = fsi->erasesize / block_size; + + err =ssdfs_unaligned_read_buffer (fsi, peb_id, block_size, bytes_off, + buf, footer_size); +if (unlikely (err)) { +if (!silent) { +SSDFS_ERR ("fail to read log footer: " +"peb_id %llu, block_size %u, " +"bytes_off %u, err %d\n" , + peb_id, block_size, bytes_off, err); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to read log footer: " +"peb_id %llu, block_size %u, " +"bytes_off %u, err %d\n" , + peb_id, block_size, bytes_off, err); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return err; + } + + magic = (struct ssdfs_signature *)buf; + +if (!is_ssdfs_magic_valid (magic)) { +if (!silent) +SSDFS_ERR ("valid magic is not detected\n" ); +else +SSDFS_DBG ("valid magic is not detected\n" ); + +return -ENODATA ; + } + +if (__is_ssdfs_log_footer_magic_valid (magic)) { + footer =SSDFS_LF (buf); + vs =SSDFS_VS (footer); + + major_magic_valid =is_ssdfs_magic_valid (&vs->magic); + minor_magic_valid =is_ssdfs_log_footer_magic_valid (footer); + +if (!major_magic_valid && !minor_magic_valid) { +if (!silent) +SSDFS_ERR ("valid magic doesn't detected\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("valid magic doesn't detected\n" ); +#else +SSDFS_DBG ("valid magic doesn't detected\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -ENODATA ; + }else if (!major_magic_valid) { +if (!silent) +SSDFS_ERR ("invalid SSDFS magic signature\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid SSDFS magic signature\n" ); +#else +SSDFS_DBG ("invalid SSDFS magic signature\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + }else if (!minor_magic_valid) { +if (!silent) +SSDFS_ERR ("invalid log footer magic\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid log footer magic\n" ); +#else +SSDFS_DBG ("invalid log footer magic\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + +if (!is_ssdfs_log_footer_csum_valid (footer, footer_size)) { +if (!silent) +SSDFS_ERR ("invalid checksum of log footer\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid checksum of log footer\n" ); +#else +SSDFS_DBG ("invalid checksum of log footer\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + + *log_pages =le32_to_cpu (footer->log_bytes); + *log_pages /= block_size; + +if (*log_pages ==0 || *log_pages >= pages_per_peb) { +if (!silent) +SSDFS_ERR ("invalid log pages %u\n" , *log_pages); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid log pages %u\n" , *log_pages); +#else +SSDFS_DBG ("invalid log pages %u\n" , *log_pages); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + }else if (is_ssdfs_partial_log_header_magic_valid (magic)) { + pl_hdr =SSDFS_PLH (buf); + + major_magic_valid =is_ssdfs_magic_valid (&pl_hdr->magic); + minor_magic_valid = +is_ssdfs_partial_log_header_magic_valid (&pl_hdr->magic); + +if (!major_magic_valid && !minor_magic_valid) { +if (!silent) +SSDFS_ERR ("valid magic doesn't detected\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("valid magic doesn't detected\n" ); +#else +SSDFS_DBG ("valid magic doesn't detected\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -ENODATA ; + }else if (!major_magic_valid) { +if (!silent) +SSDFS_ERR ("invalid SSDFS magic signature\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid SSDFS magic signature\n" ); +#else +SSDFS_DBG ("invalid SSDFS magic signature\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + }else if (!minor_magic_valid) { +if (!silent) +SSDFS_ERR ("invalid partial log header magic\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid partial log header magic\n" ); +#else +SSDFS_DBG ("invalid partial log header magic\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + +if (!is_ssdfs_partial_log_header_csum_valid (pl_hdr, hdr_size)) { +if (!silent) +SSDFS_ERR ("invalid checksum of footer\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid checksum of footer\n" ); +#else +SSDFS_DBG ("invalid checksum of footer\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + + *log_pages =le32_to_cpu (pl_hdr->log_bytes); + *log_pages /= block_size; + +if (*log_pages ==0 || *log_pages >= pages_per_peb) { +if (!silent) +SSDFS_ERR ("invalid log pages %u\n" , *log_pages); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("invalid log pages %u\n" , *log_pages); +#else +SSDFS_DBG ("invalid log pages %u\n" , *log_pages); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return -EIO ; + } + }else { +if (!silent) { +SSDFS_ERR ("log footer is corrupted: " +"peb_id %llu, block_size %u, bytes_off %u\n" , + peb_id, block_size, bytes_off); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("log footer is corrupted: " +"peb_id %llu, block_size %u, bytes_off %u\n" , + peb_id, block_size, bytes_off); +#else +SSDFS_DBG ("log footer is corrupted: " +"peb_id %llu, block_size %u, bytes_off %u\n" , + peb_id, block_size, bytes_off); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return -EIO ; + } + +return 0 ; +} + +/* +* ssdfs_read_checked_log_footer() - read and check log footer +* @fsi: pointer on shared file system object +* @log_hdr: log header +* @peb_id: PEB identification number +* @block_size: block size in bytes +* @bytes_off: offset inside PEB in bytes +* @buf: buffer for log footer +* @silent: show error or not? +* +* This function reads and checks consistency of log footer. +* +* RETURN: +* [success] - log footer is consistent. +* [failure] - error code: +* +* %-ENODATA - valid magic doesn't detected. +* %-EIO - log footer is corrupted. +*/ +int ssdfs_read_checked_log_footer (struct ssdfs_fs_info *fsi,void *log_hdr, +u64 peb_id,u32 block_size, +u32 bytes_off,void *buf, +bool silent) +{ +struct ssdfs_signature *magic; +struct ssdfs_log_footer *footer; +struct ssdfs_partial_log_header *pl_hdr; +size_t footer_size =sizeof (struct ssdfs_log_footer ); +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !fsi->devops->read); +BUG_ON (!log_hdr || !buf); +BUG_ON (bytes_off >= (fsi->pages_per_peb * fsi->pagesize)); + +SSDFS_DBG ("peb_id %llu, block_size %u, bytes_off %u, buf %p\n" , + peb_id, block_size, bytes_off, buf); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_unaligned_read_buffer (fsi, peb_id, block_size, bytes_off, + buf, footer_size); +if (unlikely (err)) { +if (!silent) { +SSDFS_ERR ("fail to read log footer: " +"peb_id %llu, block_size %u, " +"bytes_off %u, err %d\n" , + peb_id, block_size, bytes_off, err); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("fail to read log footer: " +"peb_id %llu, block_size %u, " +"bytes_off %u, err %d\n" , + peb_id, block_size, bytes_off, err); +#else +SSDFS_DBG ("fail to read log footer: " +"peb_id %llu, block_size %u, " +"bytes_off %u, err %d\n" , + peb_id, block_size, bytes_off, err); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return err; + } + + magic = (struct ssdfs_signature *)buf; + +if (!is_ssdfs_magic_valid (magic)) { +if (!silent) +SSDFS_ERR ("valid magic is not detected\n" ); +else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("valid magic is not detected\n" ); +#else +SSDFS_DBG ("valid magic is not detected\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return -ENODATA ; + } + +if (__is_ssdfs_log_footer_magic_valid (magic)) { + footer =SSDFS_LF (buf); + + err =ssdfs_check_log_footer (fsi, log_hdr, footer, silent); +if (err) { +if (!silent) { +SSDFS_ERR ("log footer is corrupted: " +"peb_id %llu, bytes_off %u, err %d\n" , + peb_id, bytes_off, err); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("log footer is corrupted: " +"peb_id %llu, bytes_off %u, err %d\n" , + peb_id, bytes_off, err); +#else +SSDFS_DBG ("log footer is corrupted: " +"peb_id %llu, bytes_off %u, err %d\n" , + peb_id, bytes_off, err); +#endif /* CONFIG_SSDFS_DEBUG */ + } +return err; + } + }else if (is_ssdfs_partial_log_header_magic_valid (magic)) { + pl_hdr =SSDFS_PLH (buf); + + err =ssdfs_check_partial_log_header (fsi, pl_hdr, silent); +if (unlikely (err)) { +if (!silent) { +SSDFS_ERR ("partial log header is corrupted: " +"peb_id %llu, bytes_off %u\n" , + peb_id, bytes_off); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("partial log header is corrupted: " +"peb_id %llu, bytes_off %u\n" , + peb_id, bytes_off); +#else +SSDFS_DBG ("partial log header is corrupted: " +"peb_id %llu, bytes_off %u\n" , + peb_id, bytes_off); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return err; + } + }else { +if (!silent) { +SSDFS_ERR ("log footer is corrupted: " +"peb_id %llu, bytes_off %u\n" , + peb_id, bytes_off); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_ERR ("log footer is corrupted: " +"peb_id %llu, bytes_off %u\n" , + peb_id, bytes_off); +#else +SSDFS_DBG ("log footer is corrupted: " +"peb_id %llu, bytes_off %u\n" , + peb_id, bytes_off); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return -EIO ; + } + +return 0 ; +} + +/* +* ssdfs_store_nsegs() - store volume's segments number in volume state +* @fsi: pointer on shared file system object +* @vs: volume state [out] +* +* This function stores volume's segments number in volume state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENOLCK - volume is under resize. +*/ +static inline +int ssdfs_store_nsegs (struct ssdfs_fs_info *fsi, +struct ssdfs_volume_state *vs) +{ +mutex_lock (&fsi->resize_mutex); + vs->nsegs =cpu_to_le64 (fsi->nsegs); +mutex_unlock (&fsi->resize_mutex); + +return 0 ; +} + +/* +* ssdfs_prepare_current_segment_ids() - prepare current segment IDs +* @fsi: pointer on shared file system object +* @array: pointer on array of IDs [out] +* @size: size the array in bytes +* +* This function prepares the current segment IDs. +* +* RETURN: +* [success] +* [failure] - error code. +*/ +int ssdfs_prepare_current_segment_ids (struct ssdfs_fs_info *fsi, +__le64 *array, +size_t size) +{ +size_t count = size /sizeof (__le64); +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !array); + +SSDFS_DBG ("fsi %p, array %p, size %zu\n" , + fsi, array, size); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (size != (sizeof (__le64) *SSDFS_CUR_SEGS_COUNT )) { +SSDFS_ERR ("invalid array size %zu\n" , + size); +return -EINVAL ; + } + +memset (array,0xFF , size); + +if (fsi->cur_segs) { +down_read (&fsi->cur_segs->lock); +for (i =0 ; i < count; i++) { +struct ssdfs_segment_info *real_seg; +u64 seg; + +if (!fsi->cur_segs->objects[i]) +continue ; + +ssdfs_current_segment_lock (fsi->cur_segs->objects[i]); + + real_seg = fsi->cur_segs->objects[i]->real_seg; +if (real_seg) { + seg = real_seg->seg_id; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index %d, seg_id %llu\n" , + i, seg); +#endif /* CONFIG_SSDFS_DEBUG */ + array[i] =cpu_to_le64 (seg); + }else { + seg = fsi->cur_segs->objects[i]->seg_id; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("index %d, seg_id %llu\n" , + i, seg); +#endif /* CONFIG_SSDFS_DEBUG */ + array[i] =cpu_to_le64 (seg); + } + +ssdfs_current_segment_unlock (fsi->cur_segs->objects[i]); + } +up_read (&fsi->cur_segs->lock); + } + +return 0 ; +} + +/* +* ssdfs_prepare_volume_state_info_for_commit() - prepare volume state +* @fsi: pointer on shared file system object +* @fs_state: file system state +* @array: pointer on array of IDs +* @size: size the array in bytes +* @last_log_time: log creation timestamp +* @last_log_cno: last log checkpoint +* @vs: volume state [out] +* +* This function prepares volume state info for commit. +* +* RETURN: +* [success] +* [failure] - error code. +*/ +int ssdfs_prepare_volume_state_info_for_commit (struct ssdfs_fs_info *fsi, +u16 fs_state, +__le64 *cur_segs, +size_t size, +u64 last_log_time, +u64 last_log_cno, +struct ssdfs_volume_state *vs) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !vs); + +SSDFS_DBG ("fsi %p, fs_state %#x\n" , fsi, fs_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (size != (sizeof (__le64) *SSDFS_CUR_SEGS_COUNT )) { +SSDFS_ERR ("invalid array size %zu\n" , + size); +return -EINVAL ; + } + + err =ssdfs_store_nsegs (fsi, vs); +if (err) { +SSDFS_DBG ("unable to store segments number: err %d\n" , err); +return err; + } + + vs->magic.common =cpu_to_le32 (SSDFS_SUPER_MAGIC ); + vs->magic.version.major =SSDFS_MAJOR_REVISION ; + vs->magic.version.minor =SSDFS_MINOR_REVISION ; + +spin_lock (&fsi->volume_state_lock); + + fsi->fs_mod_time = last_log_time; + fsi->fs_state = fs_state; + + vs->free_pages =cpu_to_le64 (fsi->free_pages); + vs->timestamp =cpu_to_le64 (last_log_time); + vs->cno =cpu_to_le64 (last_log_cno); + vs->flags =cpu_to_le32 (fsi->fs_flags); + vs->state =cpu_to_le16 (fs_state); + vs->errors =cpu_to_le16 (fsi->fs_errors); + vs->feature_compat =cpu_to_le64 (fsi->fs_feature_compat); + vs->feature_compat_ro =cpu_to_le64 (fsi->fs_feature_compat_ro); + vs->feature_incompat =cpu_to_le64 (fsi->fs_feature_incompat); + +ssdfs_memcpy (vs->uuid,0 ,SSDFS_UUID_SIZE , + fsi->vs->uuid,0 ,SSDFS_UUID_SIZE , +SSDFS_UUID_SIZE ); +ssdfs_memcpy (vs->label,0 ,SSDFS_VOLUME_LABEL_MAX , + fsi->vs->label,0 ,SSDFS_VOLUME_LABEL_MAX , +SSDFS_VOLUME_LABEL_MAX ); +ssdfs_memcpy (vs->cur_segs,0 , size, + cur_segs,0 , size, + size); + + vs->migration_threshold =cpu_to_le16 (fsi->migration_threshold); + vs->open_zones =cpu_to_le32 (atomic_read (&fsi->open_zones)); + +spin_unlock (&fsi->volume_state_lock); + +if (atomic_read (&fsi->open_zones) <0 ) { +SSDFS_ERR ("invalid open_zones %d\n" , +atomic_read (&fsi->open_zones)); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("open_zones %d\n" , +atomic_read (&fsi->open_zones)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_memcpy (&vs->blkbmap, +0 ,sizeof (struct ssdfs_blk_bmap_options ), + &fsi->vs->blkbmap, +0 ,sizeof (struct ssdfs_blk_bmap_options ), +sizeof (struct ssdfs_blk_bmap_options )); +ssdfs_memcpy (&vs->blk2off_tbl, +0 ,sizeof (struct ssdfs_blk2off_tbl_options ), + &fsi->vs->blk2off_tbl, +0 ,sizeof (struct ssdfs_blk2off_tbl_options ), +sizeof (struct ssdfs_blk2off_tbl_options )); + +ssdfs_memcpy (&vs->user_data, +0 ,sizeof (struct ssdfs_user_data_options ), + &fsi->vs->user_data, +0 ,sizeof (struct ssdfs_user_data_options ), +sizeof (struct ssdfs_user_data_options )); +ssdfs_memcpy (&vs->root_folder, +0 ,sizeof (struct ssdfs_inode ), + &fsi->vs->root_folder, +0 ,sizeof (struct ssdfs_inode ), +sizeof (struct ssdfs_inode )); + +ssdfs_memcpy (&vs->inodes_btree, +0 ,sizeof (struct ssdfs_inodes_btree ), + &fsi->vs->inodes_btree, +0 ,sizeof (struct ssdfs_inodes_btree ), +sizeof (struct ssdfs_inodes_btree )); +ssdfs_memcpy (&vs->shared_extents_btree, +0 ,sizeof (struct ssdfs_shared_extents_btree ), + &fsi->vs->shared_extents_btree, +0 ,sizeof (struct ssdfs_shared_extents_btree ), +sizeof (struct ssdfs_shared_extents_btree )); +ssdfs_memcpy (&vs->shared_dict_btree, +0 ,sizeof (struct ssdfs_shared_dictionary_btree ), + &fsi->vs->shared_dict_btree, +0 ,sizeof (struct ssdfs_shared_dictionary_btree ), +sizeof (struct ssdfs_shared_dictionary_btree )); +ssdfs_memcpy (&vs->snapshots_btree, +0 ,sizeof (struct ssdfs_snapshots_btree ), + &fsi->vs->snapshots_btree, +0 ,sizeof (struct ssdfs_snapshots_btree ), +sizeof (struct ssdfs_snapshots_btree )); + +return 0 ; +} + +/* +* ssdfs_prepare_log_footer_for_commit() - prepare log footer for commit +* @fsi: pointer on shared file system object +* @block_size: block size in bytes +* @log_pages: count of pages in the log +* @log_flags: log's flags +* @last_log_time: log creation timestamp +* @last_log_cno: last log checkpoint +* @footer: log footer [out] +* +* This function prepares log footer for commit. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input values. +*/ +int ssdfs_prepare_log_footer_for_commit (struct ssdfs_fs_info *fsi, +u32 block_size, +u32 log_pages, +u32 log_flags, +u64 last_log_time, +u64 last_log_cno, +struct ssdfs_log_footer *footer) +{ +u16 data_size =sizeof (struct ssdfs_log_footer ); +int err; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fsi %p, block_size %u, log_pages %u, " +"log_flags %#x, footer %p\n" , + fsi, block_size, log_pages, log_flags, footer); +#endif /* CONFIG_SSDFS_DEBUG */ + + footer->volume_state.magic.key =cpu_to_le16 (SSDFS_LOG_FOOTER_MAGIC ); + + footer->timestamp =cpu_to_le64 (last_log_time); + footer->cno =cpu_to_le64 (last_log_cno); + +if (log_pages >= (U32_MAX / block_size)) { +SSDFS_ERR ("invalid value of log_pages %u\n" , log_pages); +return -EINVAL ; + } + + footer->log_bytes =cpu_to_le32 (log_pages * block_size); + +if (log_flags & ~SSDFS_LOG_FOOTER_FLAG_MASK ) { +SSDFS_ERR ("unknow log flags %#x\n" , log_flags); +return -EINVAL ; + } + + footer->log_flags =cpu_to_le32 (log_flags); + + footer->volume_state.check.bytes =cpu_to_le16 (data_size); + footer->volume_state.check.flags =cpu_to_le16 (SSDFS_CRC32 ); + + err =ssdfs_calculate_csum (&footer->volume_state.check, + footer, data_size); +if (unlikely (err)) { +SSDFS_ERR ("unable to calculate checksum: err %d\n" , err); +return err; + } + +return 0 ; +}
fs/ssdfs/volume_header.c new file +1431 -0
1,431 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 15/79 ssdfs: introduce PEB's block bitmap
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS splits a partition/volume on sequence of fixed-sized segments. Every segment can include one or several Logical Erase Blocks (LEB). LEB can be mapped into "Physical" Erase Block (PEB). Generally speaking, PEB is fixed-sized container that include some number of logical blocks (or NAND flash pages). PEB has block bitmap with the goal to track the state (free, pre-allocated, allocated, invalid) of logical blocks and to account the physical space is used for storing log's metadata (segment header, partial log header, footer).
Block bitmap implements API:
(1) create - create empty block bitmap
(2) destroy - destroy block bitmap object
(3) init - intialize block bitmap by metadata from PEB's log
(4) snapshot - take block bitmap snapshot for flush operation
(5) forget_snapshot - free block bitmap's snapshot resources
(6) lock/unlock - lock/unlock block bitmap
(7) test_block/test_range - check state of block or range of blocks
(8) get_free_pages - get number of free pages
(9) get_used_pages - get number of used pages
(10) get_invalid_pages - get number of invalid pages
(11) pre_allocate - pre_allocate logical block or range of blocks
(12) allocate - allocate logical block or range of blocks
(13) invalidate - invalidate logical block or range of blocks
(14) collect_garbage - get contigous range of blocks in state
(15) clean - convert the whole block bitmap into clean state
--- fs/ssdfs/block_bitmap.h | 393 +++++++++++++++++++++++++++++++++ fs/ssdfs/block_bitmap_tables.c | 311 ++++++++++++++++++++++++++ fs/ssdfs/common_bitmap.h | 230 +++++++++++++++++++ 3 files changed, 934 insertions(+) create mode 100644 fs/ssdfs/block_bitmap.h create mode 100644 fs/ssdfs/block_bitmap_tables.c create mode 100644 fs/ssdfs/common_bitmap.h
fs/ssdfs/block_bitmap.h new file +393 -0
@@ -0,0 +1,393 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/block_bitmap.h - PEB's block bitmap declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_BLOCK_BITMAP_H +#define _SSDFS_BLOCK_BITMAP_H + +#include "common_bitmap.h" + +#define SSDFS_BLK_STATE_BITS 2 +#define SSDFS_BLK_STATE_MASK 0x3 + +enum { +SSDFS_BLK_FREE =0x0 , +SSDFS_BLK_PRE_ALLOCATED =0x1 , +SSDFS_BLK_VALID =0x3 , +SSDFS_BLK_INVALID =0x2 , +SSDFS_BLK_STATE_MAX =SSDFS_BLK_VALID +1 , +}; + +#define SSDFS_BLK_BMAP_CAPACITY_MAX U16_MAX + +#define SSDFS_FREE_STATES_BYTE 0x00 +#define SSDFS_PRE_ALLOC_STATES_BYTE 0x55 +#define SSDFS_VALID_STATES_BYTE 0xFF +#define SSDFS_INVALID_STATES_BYTE 0xAA + +#define SSDFS_BLK_BMAP_BYTE (blk_state)({ \ + u8 value; \ + switch (blk_state) { \ + case SSDFS_BLK_FREE: \ + value = SSDFS_FREE_STATES_BYTE; \ + break; \ + case SSDFS_BLK_PRE_ALLOCATED: \ + value = SSDFS_PRE_ALLOC_STATES_BYTE; \ + break; \ + case SSDFS_BLK_VALID: \ + value = SSDFS_VALID_STATES_BYTE; \ + break; \ + case SSDFS_BLK_INVALID: \ + value = SSDFS_INVALID_STATES_BYTE; \ + break; \ + default: \ + BUG(); \ + }; \ + value; \ +}) + +#define BLK_BMAP_BYTES (items_count) \ + (((items_count + SSDFS_ITEMS_PER_LONG(SSDFS_BLK_STATE_BITS) - 1) / \ + SSDFS_ITEMS_PER_LONG(SSDFS_BLK_STATE_BITS)) * sizeof(unsigned long)) + +static inline +int SSDFS_BLK2FOLIO (u32 blk,u8 item_bits,u16 *offset) +{ +u32 blks_per_byte =SSDFS_ITEMS_PER_BYTE (item_bits); +u32 blks_per_long =SSDFS_ITEMS_PER_LONG (item_bits); +u32 blks_per_folio =PAGE_SIZE * blks_per_byte; +u32 off; + +if (offset) { + off = (blk % blks_per_folio) / blks_per_long; + off *=sizeof (unsigned long ); +BUG_ON (off >=U16_MAX ); + *offset = off; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk %u, item_bits %u, blks_per_byte %u, " +"blks_per_long %u, blks_per_folio %u, " +"folio_index %u\n" , + blk, item_bits, blks_per_byte, + blks_per_long, blks_per_folio, + blk / blks_per_folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +return blk / blks_per_folio; +} + +/* +* struct ssdfs_last_bmap_search - last search in bitmap +* @folio_index: index of folio in folio vector +* @offset: offset of cache from page's begining +* @cache: cached bmap's part +*/ +struct ssdfs_last_bmap_search { +int folio_index; +u16 offset; +unsigned long cache; +}; + +static inline +u32 SSDFS_FIRST_CACHED_BLOCK (struct ssdfs_last_bmap_search *search) +{ +u32 blks_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +u32 blks_per_folio =PAGE_SIZE * blks_per_byte; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("folio_index %d, offset %u, " +"blks_per_byte %u, blks_per_folio %u\n" , + search->folio_index, + search->offset, + blks_per_byte, blks_per_folio); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (search->folio_index * blks_per_folio) + + (search->offset * blks_per_byte); +} + +enum { +SSDFS_FREE_BLK_SEARCH , +SSDFS_VALID_BLK_SEARCH , +SSDFS_OTHER_BLK_SEARCH , +SSDFS_SEARCH_TYPE_MAX , +}; + +static inline +int SSDFS_GET_CACHE_TYPE (int blk_state) +{ +switch (blk_state) { +case SSDFS_BLK_FREE : +return SSDFS_FREE_BLK_SEARCH ; + +case SSDFS_BLK_VALID : +return SSDFS_VALID_BLK_SEARCH ; + +case SSDFS_BLK_PRE_ALLOCATED : +case SSDFS_BLK_INVALID : +return SSDFS_OTHER_BLK_SEARCH ; + }; + +return SSDFS_SEARCH_TYPE_MAX ; +} + +#define SSDFS_BLK_BMAP_INITIALIZED (1 << 0) +#define SSDFS_BLK_BMAP_DIRTY (1 << 1) + +/* +* struct ssdfs_block_bmap_storage - block bitmap's storage +* @state: storage state +* @array: vector of folios +* @buf: pointer on memory buffer +*/ +struct ssdfs_block_bmap_storage { +int state; +struct ssdfs_folio_vector array; +void *buf; +}; + +/* Block bitmap's storage's states */ +enum { +SSDFS_BLOCK_BMAP_STORAGE_ABSENT , +SSDFS_BLOCK_BMAP_STORAGE_FOLIO_VEC , +SSDFS_BLOCK_BMAP_STORAGE_BUFFER , +SSDFS_BLOCK_BMAP_STORAGE_STATE_MAX +}; + +/* +* struct ssdfs_block_bmap - in-core segment's block bitmap +* @lock: block bitmap lock +* @flags: block bitmap state flags +* @storage: block bitmap's storage +* @bytes_count: block bitmap size in bytes +* @items_capacity: total available items in bitmap +* @allocation_pool: number of items available for allocation +* @metadata_items: count of metadata items +* @used_blks: count of valid blocks +* @invalid_blks: count of invalid blocks +* @last_search: last search/access cache array +*/ +struct ssdfs_block_bmap { +struct mutex lock; +atomic_t flags; +struct ssdfs_block_bmap_storage storage; +size_t bytes_count; +size_t items_capacity; +size_t allocation_pool; +u32 metadata_items; +u32 used_blks; +u32 invalid_blks; +struct ssdfs_last_bmap_search last_search[SSDFS_SEARCH_TYPE_MAX ]; +}; + +/* +* compare_block_bmap_ranges() - compare two ranges +* @range1: left range +* @range2: right range +* +* RETURN: +* 0: range1 == range2 +* -1: range1 < range2 +* 1: range1 > range2 +*/ +static inline +int compare_block_bmap_ranges (struct ssdfs_block_bmap_range *range1, +struct ssdfs_block_bmap_range *range2) +{ +u32 range1_end, range2_end; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!range1 || !range2); + +SSDFS_DBG ("range1 (start %u, len %u), range2 (start %u, len %u)\n" , + range1->start, range1->len, range2->start, range2->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (range1->start == range2->start) { +if (range1->len == range2->len) +return 0 ; +else if (range1->len < range2->len) +return -1 ; +else +return 1 ; + }else if (range1->start < range2->start) { + range1_end = range1->start + range1->len; + range2_end = range2->start + range2->len; + +if (range2_end <= range1_end) +return 1 ; +else +return -1 ; + } + +/* range1->start > range2->start */ +return -1 ; +} + +/* +* ranges_have_intersection() - have ranges intersection? +* @range1: left range +* @range2: right range +* +* RETURN: +* [true] - ranges have intersection +* [false] - ranges doesn't intersect +*/ +static inline +bool ranges_have_intersection (struct ssdfs_block_bmap_range *range1, +struct ssdfs_block_bmap_range *range2) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!range1 || !range2); + +SSDFS_DBG ("range1 (start %u, len %u), range2 (start %u, len %u)\n" , + range1->start, range1->len, range2->start, range2->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if ((range2->start + range2->len) <= range1->start) +return false; +else if ((range1->start + range1->len) <= range2->start) +return false; + +return true; +} + +enum { +SSDFS_BLK_BMAP_CREATE , +SSDFS_BLK_BMAP_INIT , +}; + +/* Function prototypes */ +int ssdfs_block_bmap_create (struct ssdfs_fs_info *fsi, +struct ssdfs_block_bmap *bmap, +u32 items_capacity, +u32 allocation_pool, +int flag,int init_state); +int ssdfs_block_bmap_destroy (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_init (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_folio_vector *source, +u8 flags, +u32 last_free_blk, +u32 metadata_blks, +u32 invalid_blks, +u32 bmap_bytes); +int ssdfs_block_bmap_inflate (struct ssdfs_block_bmap *blk_bmap, +u32 free_items); +int ssdfs_block_bmap_correct_capacity (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range); +int ssdfs_block_bmap_snapshot (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_folio_vector *snapshot, +u32 *last_free_page, +u32 *metadata_blks, +u32 *invalid_blks, +size_t *items_capacity, +size_t *bytes_count); +void ssdfs_block_bmap_forget_snapshot (struct ssdfs_folio_vector *snapshot); + +int ssdfs_block_bmap_lock (struct ssdfs_block_bmap *blk_bmap); +bool ssdfs_block_bmap_is_locked (struct ssdfs_block_bmap *blk_bmap); +void ssdfs_block_bmap_unlock (struct ssdfs_block_bmap *blk_bmap); + +bool ssdfs_block_bmap_dirtied (struct ssdfs_block_bmap *blk_bmap); +void ssdfs_block_bmap_set_dirty_state (struct ssdfs_block_bmap *blk_bmap); +void ssdfs_block_bmap_clear_dirty_state (struct ssdfs_block_bmap *blk_bmap); +bool ssdfs_block_bmap_initialized (struct ssdfs_block_bmap *blk_bmap); +void ssdfs_set_block_bmap_initialized (struct ssdfs_block_bmap *blk_bmap); + +bool ssdfs_block_bmap_test_block (struct ssdfs_block_bmap *blk_bmap, +u32 blk,int blk_state); +bool ssdfs_block_bmap_test_range (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range, +int blk_state); +int ssdfs_get_block_state (struct ssdfs_block_bmap *blk_bmap,u32 blk); +int ssdfs_get_range_state (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range); +int ssdfs_block_bmap_reserve_metadata_pages (struct ssdfs_block_bmap *blk_bmap, +u32 count); +int ssdfs_block_bmap_free_metadata_pages (struct ssdfs_block_bmap *blk_bmap, +u32 count,u32 *freed_items); +int ssdfs_block_bmap_get_free_pages (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_get_used_pages (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_get_invalid_pages (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_get_pages_capacity (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_get_allocation_pool (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_get_metadata_pages (struct ssdfs_block_bmap *blk_bmap); +int ssdfs_block_bmap_pre_allocate (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 *len, +struct ssdfs_block_bmap_range *range); +int ssdfs_block_bmap_allocate (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 *len, +struct ssdfs_block_bmap_range *range); +int ssdfs_block_bmap_invalidate (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range); +int ssdfs_block_bmap_collect_garbage (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_len, +int blk_state, +struct ssdfs_block_bmap_range *range); +int ssdfs_block_bmap_clean (struct ssdfs_block_bmap *blk_bmap, +size_t items_capacity); +int ssdfs_block_bmap_invalid2clean (struct ssdfs_block_bmap *blk_bmap); + +#if IS_ENABLED (CONFIG_KUNIT ) +bool BLK_BMAP_BYTE_CONTAINS_STATE (u8 *value,int blk_state); +#endif + +#define SSDFS_BLK_BMAP_FNS (state, name) \ +static inline \ +bool is_block_##name(struct ssdfs_block_bmap *blk_bmap, u32 blk) \ +{ \ + return ssdfs_block_bmap_test_block(blk_bmap, blk, \ + SSDFS_BLK_##state); \ +} \ +static inline \ +bool is_range_##name(struct ssdfs_block_bmap *blk_bmap, \ + struct ssdfs_block_bmap_range *range) \ +{ \ + return ssdfs_block_bmap_test_range(blk_bmap, range, \ + SSDFS_BLK_##state); \ +} \ + +/* +* is_block_free() +* is_range_free() +*/ +SSDFS_BLK_BMAP_FNS (FREE , free) + +/* +* is_block_pre_allocated() +* is_range_pre_allocated() + */ +SSDFS_BLK_BMAP_FNS (PRE_ALLOCATED , pre_allocated) + +/* +* is_block_valid() +* is_range_valid() + */ +SSDFS_BLK_BMAP_FNS (VALID , valid) + +/* +* is_block_invalid() +* is_range_invalid() + */ +SSDFS_BLK_BMAP_FNS(INVALID, invalid) + +#endif /* _SSDFS_BLOCK_BITMAP_H */
fs/ssdfs/block_bitmap_tables.c new file +311 -0
311 lines not on this page · raw message
fs/ssdfs/common_bitmap.h new file +230 -0
230 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 17/79 ssdfs: implement support of migration scheme in PEB bitmap
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS implements a migration scheme. Migration scheme is a fundamental technique of GC overhead management. The key responsibility of the migration scheme is to guarantee the presence of data in the same segment for any update operations. Generally speaking, the migration scheme’s model is implemented on the basis of association an exhausted "Physical" Erase Block (PEB) with a clean one. The goal such association of two PEBs is to implement the gradual migration of data by means of the update operations in the initial (exhausted) PEB. As a result, the old, exhausted PEB becomes invalidated after complete data migration and it will be possible to apply the erase operation to convert it in the clean state. Moreover, the destination PEB in the association changes the initial PEB for some index in the segment and, finally, it becomes the only PEB for this position. Namely such technique implements the concept of logical extent with the goal to decrease the write amplification issue and to manage the GC overhead. Because the logical extent concept excludes the necessity to update metadata is tracking the position of user data on the file system’s volume. Generally speaking, the migration scheme is capable to decrease the GC activity significantly by means of excluding the necessity to update metadata and by means of self-migration of data between of PEBs is triggered by regular update operations.
To implement the migration scheme concept, SSDFS introduces
PEB container that includes source and destination erase blocks. As a result, PEB block bitmap object represents the same aggregation for source PEB's block bitmap and destination PEB's block bitmap. PEB block bitmap implements API:
(1) create - create PEB block bitmap
(2) destroy - destroy PEB block bitmap
(3) init - initialize PEB block bitmap by metadata from a log
(4) get_free_pages - get free pages in aggregation of block bitmaps
(5) get_used_pages - get used pages in aggregation of block bitmaps
(6) get_invalid_pages - get invalid pages in aggregation of block bitmaps
(7) pre_allocate - pre_allocate page/range in aggregation of block bitmaps
(8) allocate - allocate page/range in aggregation of block bitmaps
(9) invalidate - invalidate page/range in aggregation of block bitmaps
(10) update_range - change the state of range in aggregation of block bitmaps
(11) collect_garbage - find contiguous range for requested state
(12) start_migration - prepare PEB's environment for migration
(13) migrate - move range from source block bitmap into destination one
(14) finish_migration - clean source block bitmap and swap block bitmaps
--- fs/ssdfs/peb_block_bitmap.h | 179 ++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 fs/ssdfs/peb_block_bitmap.h
fs/ssdfs/peb_block_bitmap.h new file +179 -0
@@ -0,0 +1,179 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/peb_block_bitmap.h - PEB's block bitmap declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_PEB_BLOCK_BITMAP_H +#define _SSDFS_PEB_BLOCK_BITMAP_H + +#include "block_bitmap.h" + +/* PEB's block bitmap indexes */ +enum { +SSDFS_PEB_BLK_BMAP1 , +SSDFS_PEB_BLK_BMAP2 , +SSDFS_PEB_BLK_BMAP_ITEMS_MAX +}; + +/* +* struct ssdfs_peb_blk_bmap - PEB container's block bitmap object +* @state: PEB container's block bitmap's state +* @peb_index: PEB index in array +* @pages_per_peb: pages per physical erase block +* @modification_lock: lock for modification operations +* @peb_valid_blks: PEB container's valid logical blocks count +* @peb_invalid_blks: PEB container's invalid logical blocks count +* @peb_free_blks: PEB container's free logical blocks count +* @peb_blks_capacity: PEB container's logical blocks capacity +* @buffers_state: buffers state +* @lock: buffers lock +* @init_cno: initialization checkpoint +* @src: source PEB's block bitmap object's pointer +* @dst: destination PEB's block bitmap object's pointer +* @buffers: block bitmap buffers +* @init_end: wait of init ending +* @parent: pointer on parent segment block bitmap +*/ +struct ssdfs_peb_blk_bmap { +atomic_t state; + +u16 peb_index; +u32 pages_per_peb; + +struct rw_semaphore modification_lock; +atomic_t peb_valid_blks; +atomic_t peb_invalid_blks; +atomic_t peb_free_blks; +atomic_t peb_blks_capacity; + +atomic_t buffers_state; +struct rw_semaphore lock; +u64 init_cno; +struct ssdfs_block_bmap *src; +struct ssdfs_block_bmap *dst; +struct ssdfs_block_bmap buffer[SSDFS_PEB_BLK_BMAP_ITEMS_MAX ]; +struct completion init_end; + +struct ssdfs_segment_blk_bmap *parent; +}; + +/* PEB container's block bitmap's possible states */ +enum { +SSDFS_PEB_BLK_BMAP_STATE_UNKNOWN , +SSDFS_PEB_BLK_BMAP_CREATED , +SSDFS_PEB_BLK_BMAP_HAS_CLEAN_DST , +SSDFS_PEB_BLK_BMAP_INITIALIZED , +SSDFS_PEB_BLK_BMAP_STATE_MAX , +}; + +/* PEB's buffer array possible states */ +enum { +SSDFS_PEB_BMAP_BUFFERS_EMPTY , +SSDFS_PEB_BMAP1_SRC , +SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST , +SSDFS_PEB_BMAP2_SRC , +SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST , +SSDFS_PEB_BMAP_BUFFERS_STATE_MAX +}; + +/* PEB's block bitmap operation destination */ +enum { +SSDFS_PEB_BLK_BMAP_SOURCE , +SSDFS_PEB_BLK_BMAP_DESTINATION , +SSDFS_PEB_BLK_BMAP_INDEX_MAX +}; + +/* +* PEB block bitmap API +*/ +int ssdfs_peb_blk_bmap_create (struct ssdfs_segment_blk_bmap *parent, +u16 peb_index,u32 items_count, +int init_flag,int init_state); +int ssdfs_peb_blk_bmap_destroy (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_peb_blk_bmap_init (struct ssdfs_peb_blk_bmap *bmap, +struct ssdfs_folio_vector *source, +struct ssdfs_block_bitmap_fragment *hdr, +u32 peb_free_pages, +u64 cno); +int ssdfs_peb_blk_bmap_clean_init (struct ssdfs_peb_blk_bmap *bmap); +void ssdfs_peb_blk_bmap_init_failed (struct ssdfs_peb_blk_bmap *bmap); + +bool has_ssdfs_peb_blk_bmap_initialized (struct ssdfs_peb_blk_bmap *bmap); +int ssdfs_peb_blk_bmap_wait_init_end (struct ssdfs_peb_blk_bmap *bmap); + +bool ssdfs_peb_blk_bmap_initialized (struct ssdfs_peb_blk_bmap *ptr); +bool is_ssdfs_peb_blk_bmap_dirty (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_peb_blk_bmap_inflate (struct ssdfs_peb_blk_bmap *ptr, +u32 free_items); + +int ssdfs_peb_blk_bmap_get_free_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_peb_blk_bmap_get_used_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_peb_blk_bmap_get_invalid_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_peb_blk_bmap_get_metadata_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_peb_blk_bmap_get_pages_capacity (struct ssdfs_peb_blk_bmap *ptr); + +int ssdfs_peb_define_reserved_pages_per_log (struct ssdfs_peb_blk_bmap *bmap); +int ssdfs_peb_blk_bmap_reserve_metapages (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 count); +int ssdfs_peb_blk_bmap_free_metapages (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 count); +int ssdfs_peb_blk_bmap_get_block_state (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 blk); +int ssdfs_peb_blk_bmap_pre_allocate (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +struct ssdfs_block_bmap_range *range); +int ssdfs_peb_blk_bmap_allocate (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +struct ssdfs_block_bmap_range *range); +int ssdfs_peb_blk_bmap_invalidate (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +struct ssdfs_block_bmap_range *range); +int ssdfs_peb_blk_bmap_update_range (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +int new_range_state, +struct ssdfs_block_bmap_range *range); +int ssdfs_peb_blk_bmap_collect_garbage (struct ssdfs_peb_blk_bmap *bmap, +u32 start,u32 max_len, +int blk_state, +struct ssdfs_block_bmap_range *range); +int ssdfs_peb_blk_bmap_start_migration (struct ssdfs_peb_blk_bmap *bmap); +int ssdfs_peb_blk_bmap_migrate (struct ssdfs_peb_blk_bmap *bmap, +int new_range_state, +struct ssdfs_block_bmap_range *range); +int ssdfs_peb_blk_bmap_finish_migration (struct ssdfs_peb_blk_bmap *bmap); + +/* +* PEB block bitmap internal API +*/ +int ssdfs_src_blk_bmap_get_free_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_src_blk_bmap_get_used_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_src_blk_bmap_get_invalid_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_src_blk_bmap_get_metadata_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_src_blk_bmap_get_pages_capacity (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_dst_blk_bmap_get_free_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_dst_blk_bmap_get_used_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_dst_blk_bmap_get_invalid_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_dst_blk_bmap_get_metadata_pages (struct ssdfs_peb_blk_bmap *ptr); +int ssdfs_dst_blk_bmap_get_pages_capacity (struct ssdfs_peb_blk_bmap *ptr); + +#endif /* _SSDFS_PEB_BLOCK_BITMAP_H */
Signature · 1 line
2.34.1
PATCH v2 19/79 ssdfs: introduce segment block bitmap
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS splits a partition/volume on sequence of fixed-sized segments. Every segment can include one or several Logical Erase Blocks (LEB). LEB can be mapped into "Physical" Erase Block (PEB). PEB block bitmap object represents the aggregation of source PEB's block bitmap and destination PEB's block bitmap. Finally, segment block bitmap implements an array of PEB block bitmaps. Segment block bitmap has API:
(1) create - create segment block bitmap
(2) destroy - destroy segment block bitmap
(3) partial_init - initialize by state of one PEB block bitmap
(4) get_free_pages - get free pages in segment block bitmap
(5) get_used_pages - get used pages in segment block bitmap
(6) get_invalid_pages - get invalid pages in segment block bitmap
(7) reserve_block - reserve a free block
(8) reserved_extent - reserve some number of free blocks
(9) pre_allocate - pre_allocate page/range in segment block bitmap
(10) allocate - allocate page/range in segment block bitmap
(11) update_range - change the state of range in segment block bitmap
--- fs/ssdfs/segment_block_bitmap.h | 240 ++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 fs/ssdfs/segment_block_bitmap.h
fs/ssdfs/segment_block_bitmap.h new file +240 -0
@@ -0,0 +1,240 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/segment_block_bitmap.h - segment's block bitmap declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_SEGMENT_BLOCK_BITMAP_H +#define _SSDFS_SEGMENT_BLOCK_BITMAP_H + +#include "peb_block_bitmap.h" + +/* +* struct ssdfs_segment_blk_bmap - segment block bitmap object +* @state: segment block bitmap's state +* @pages_per_peb: pages per physical erase block +* @pages_per_seg: pages per segment +* @modification_lock: lock for modification operations +* @seg_valid_blks: segment's valid logical blocks count +* @seg_invalid_blks: segment's invalid logical blocks count +* @seg_free_blks: segment's free logical blocks count +* @seg_reserved_metapages: number of reserved metapages +* @peb: array of PEB block bitmap objects +* @pebs_count: PEBs count in segment +* @parent_si: pointer on parent segment object +*/ +struct ssdfs_segment_blk_bmap { +atomic_t state; + +u32 pages_per_peb; +u32 pages_per_seg; + +struct rw_semaphore modification_lock; +atomic_t seg_valid_blks; +atomic_t seg_invalid_blks; +atomic_t seg_free_blks; +atomic_t seg_reserved_metapages; + +struct ssdfs_peb_blk_bmap *peb; +u16 pebs_count; + +struct ssdfs_segment_info *parent_si; +}; + +/* Segment block bitmap's possible states */ +enum { +SSDFS_SEG_BLK_BMAP_STATE_UNKNOWN , +SSDFS_SEG_BLK_BMAP_CREATED , +SSDFS_SEG_BLK_BMAP_STATE_MAX , +}; + +/* +* Segment block bitmap API +*/ +int ssdfs_segment_blk_bmap_create (struct ssdfs_segment_info *si, +int init_flag,int init_state); +void ssdfs_segment_blk_bmap_destroy (struct ssdfs_segment_blk_bmap *ptr); +int ssdfs_segment_blk_bmap_partial_init (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index, +struct ssdfs_folio_vector *source, +struct ssdfs_block_bitmap_fragment *hdr, +u32 peb_free_pages,u64 cno); +int ssdfs_segment_blk_bmap_partial_inflate (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index,u32 free_items); +int +ssdfs_segment_blk_bmap_partial_clean_init (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index); +void ssdfs_segment_blk_bmap_init_failed (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index); + +bool is_ssdfs_segment_blk_bmap_dirty (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index); + +bool has_ssdfs_segment_blk_bmap_initialized (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc); +int ssdfs_segment_blk_bmap_wait_init_end (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc); + +int ssdfs_segment_blk_bmap_get_block_state (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_info *pebi, +u32 blk); +int ssdfs_segment_blk_bmap_reserve_metapages (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_info *pebi, +u32 count); +int ssdfs_segment_blk_bmap_free_metapages (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_info *pebi, +u32 count); +int ssdfs_segment_blk_bmap_reserve_block (struct ssdfs_segment_blk_bmap *ptr); +int ssdfs_segment_blk_bmap_reserve_extent (struct ssdfs_segment_blk_bmap *ptr, +u32 count,u32 *reserved_blks); +int ssdfs_segment_blk_bmap_release_extent (struct ssdfs_segment_blk_bmap *ptr, +u32 count); +int ssdfs_segment_blk_bmap_pre_allocate (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_info *pebi, +struct ssdfs_block_bmap_range *range); +int ssdfs_segment_blk_bmap_allocate (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_info *pebi, +struct ssdfs_block_bmap_range *range); +int ssdfs_segment_blk_bmap_update_range (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_info *pebi, +u8 peb_migration_id, +int range_state, +struct ssdfs_block_bmap_range *range); + +/* Inline methods */ + +static inline +int __ssdfs_segment_blk_bmap_get_capacity (struct ssdfs_segment_blk_bmap *ptr) +{ +int capacity =0 ; +int i; + +for (i =0 ; i < ptr->pebs_count; i++) { + capacity +=atomic_read (&ptr->peb[i].peb_blks_capacity); + } + +return capacity; +} + +static inline +int ssdfs_segment_blk_bmap_get_capacity (struct ssdfs_segment_blk_bmap *ptr) +{ +int capacity; + +down_read (&ptr->modification_lock); + capacity =__ssdfs_segment_blk_bmap_get_capacity (ptr); +up_read (&ptr->modification_lock); + +return capacity; +} + +static inline +bool is_pages_balance_correct (struct ssdfs_segment_blk_bmap *ptr) +{ +#ifdef CONFIG_SSDFS_DEBUG +int free_blks; +int valid_blks; +int invalid_blks; +int capacity; +int calculated; + +BUG_ON (!ptr); + +down_read (&ptr->modification_lock); + free_blks =atomic_read (&ptr->seg_free_blks); + valid_blks =atomic_read (&ptr->seg_valid_blks); + invalid_blks =atomic_read (&ptr->seg_invalid_blks); + calculated = free_blks + valid_blks + invalid_blks; + capacity =__ssdfs_segment_blk_bmap_get_capacity (ptr); +up_read (&ptr->modification_lock); + +BUG_ON (free_blks <0 ); +BUG_ON (valid_blks <0 ); +BUG_ON (invalid_blks <0 ); +BUG_ON (capacity <0 ); + +SSDFS_DBG ("free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, capacity %d\n" , + free_blks, valid_blks, invalid_blks, + capacity); + +if (calculated > capacity) { +SSDFS_ERR ("free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, calculated %d, " +"capacity %d\n" , + free_blks, valid_blks, invalid_blks, + calculated, capacity); +return false; + } + +return true; +#else +return true; +#endif /* CONFIG_SSDFS_DEBUG */ +} + +static inline +int ssdfs_segment_blk_bmap_get_free_pages (struct ssdfs_segment_blk_bmap *ptr) +{ +int free_blks; + +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (!is_pages_balance_correct (ptr)); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&ptr->modification_lock); + free_blks =atomic_read (&ptr->seg_free_blks); +up_read (&ptr->modification_lock); + +return free_blks; +} + +static inline +int ssdfs_segment_blk_bmap_get_used_pages (struct ssdfs_segment_blk_bmap *ptr) +{ +int valid_blks; + +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (!is_pages_balance_correct (ptr)); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&ptr->modification_lock); + valid_blks =atomic_read (&ptr->seg_valid_blks); +up_read (&ptr->modification_lock); + +return valid_blks; +} + +static inline +int ssdfs_segment_blk_bmap_get_invalid_pages (struct ssdfs_segment_blk_bmap *ptr) +{ +int invalid_blks; + +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (!is_pages_balance_correct (ptr)); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&ptr->modification_lock); + invalid_blks =atomic_read (&ptr->seg_invalid_blks); +up_read (&ptr->modification_lock); + +return invalid_blks; +} + +#endif /* _SSDFS_SEGMENT_BLOCK_BITMAP_H */
Signature · 1 line
2.34.1
PATCH v2 22/79 ssdfs: introduce offset translation table
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
One of the key goal of SSDFS is to decrease the write amplification factor. Logical extent concept is the fundamental technique to achieve the goal. Logical extent describes any volume extent on the basis of segment ID, logical block ID, and length. Migration scheme guarantee that segment ID will be always the same for any logical extent. Offset translation table provides the way to convert logical block ID into offset inside of a log of particular "Physical" Erase Block (PEB). As a result, extents b-tree never needs to be updated because logical extent will never change until intentional movement from one segment into another one.
Offset translation table is a metadata structure that is stored as metadata in every log. The responsibility of offset translation table keeps the knowledge which particular logical blocks are stored into log's payload and which offset in the payload should be used to access and retrieve the content of logical block.
Offset translation table can be imagined like a sequence of fragments. Every fragment contains array of physical offset descriptors that provides the way to convert logical block ID into the physical offset in the log. Flush logic identifies dirty fragments and stores these fragments as part of log's metadata during log commit operation.
The responsibility of offset translation table is to implement the mechanism of converting logical block ID into the physical offset in the log. As a result, offset translation table implements API:
(1) create - create empty offset translation table
(2) destroy - destroy offset translation table
(3) partial_init - initialize offset translation table by one fragment
(4) store_offsets_table - flush dirty fragments
(5) convert - convert logical block ID into offset descriptor
(6) allocate_block - allocate logical block
(7) allocate_extent - allocate logical extent
(8) change_offset - initialize offset of allocated logical block
(9) free_block - free logical block
(10) free_extent - free logical extent
--- fs/ssdfs/offset_translation_table.h | 459 ++++++++++++++++++++++++++++ 1 file changed, 459 insertions(+) create mode 100644 fs/ssdfs/offset_translation_table.h
fs/ssdfs/offset_translation_table.h new file +459 -0
@@ -0,0 +1,459 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/offset_translation_table.h - offset table declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* Copyright (c) 2022-2023 Bytedance Ltd. and/or its affiliates. +* https://www.bytedance.com/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +* Cong Wang +*/ + +#ifndef _SSDFS_OFFSET_TRANSLATION_TABLE_H +#define _SSDFS_OFFSET_TRANSLATION_TABLE_H + +#include <linux/pagevec.h> + +#include "request_queue.h" +#include "sequence_array.h" +#include "dynamic_array.h" + +/* +* struct ssdfs_phys_offset_table_fragment - fragment of phys offsets table +* @lock: table fragment lock +* @start_id: starting physical offset id number in fragment +* @sequence_id: fragment's sequence_id in PEB +* @id_count: count of id numbers in sequence +* @state: fragment state +* @peb_id: PEB ID containing the fragment +* @migrating_blocks: number of logical blocks under migration +* @actual_records: number of actual records in fragment +* @hdr: pointer on fragment's header +* @phys_offs: array of physical offsets in fragment +* @buf: buffer of fragment +* @buf_size: size of buffer in bytes +* +* One fragment can be used for one PEB's log. But one log can contain +* several fragments too. In memory exists the same count of fragments +* as on volume. +*/ +struct ssdfs_phys_offset_table_fragment { +struct rw_semaphore lock; +u16 start_id; +u16 sequence_id; +atomic_t id_count; +atomic_t state; +u64 peb_id; +atomic_t migrating_blocks; +atomic_t actual_records; + +struct ssdfs_phys_offset_table_header *hdr; +struct ssdfs_phys_offset_descriptor *phys_offs; +unsigned char *buf; +size_t buf_size; +}; + +enum { +SSDFS_BLK2OFF_FRAG_UNDEFINED , +SSDFS_BLK2OFF_FRAG_CREATED , +SSDFS_BLK2OFF_FRAG_INITIALIZED , +SSDFS_BLK2OFF_FRAG_DIRTY , +SSDFS_BLK2OFF_FRAG_UNDER_COMMIT , +SSDFS_BLK2OFF_FRAG_COMMITED , +SSDFS_BLK2OFF_PLEASE_DELETE_FRAGMENT , +SSDFS_BLK2OFF_FRAG_STATE_MAX , +}; + +#define SSDFS_INVALID_FRAG_ID U16_MAX +#define SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD (U16_MAX - 1) +#define SSDFS_BLK2OFF_TBL_REVERT_PIVOT (U16_MAX / 2) +#define SSDFS_BLK2OFF_TBL_LBLK2OFF_CAPACITY_MAX U16_MAX + +/* +* struct ssdfs_phys_offset_table_array - array of log's fragments in PEB +* @state: PEB's translation table state +* @fragment_count: fragments count +* @array: array of fragments +*/ +struct ssdfs_phys_offset_table_array { +atomic_t state; +atomic_t fragment_count; +struct ssdfs_sequence_array *sequence; +}; + +enum { +SSDFS_BLK2OFF_TABLE_UNDEFINED , +SSDFS_BLK2OFF_TABLE_CREATED , +SSDFS_BLK2OFF_TABLE_PARTIAL_INIT , +SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT , +SSDFS_BLK2OFF_TABLE_COMPLETE_INIT , +SSDFS_BLK2OFF_TABLE_DIRTY , +SSDFS_BLK2OFF_TABLE_STATE_MAX , +}; + +#define SSDFS_BLK2OFF_TABLE_INVALID_ID U16_MAX + +/* +* struct ssdfs_block_descriptor_state - block descriptor state +* @status: state of block descriptor buffer +* @buf: block descriptor buffer +*/ +struct ssdfs_block_descriptor_state { +u32 status; +struct ssdfs_block_descriptor buf; +}; + +/* +* Block descriptor buffer state +*/ +enum { +SSDFS_BLK_DESC_BUF_UNKNOWN_STATE , +SSDFS_BLK_DESC_BUF_INITIALIZED , +SSDFS_BLK_DESC_BUF_STATE_MAX , +SSDFS_BLK_DESC_BUF_ALLOCATED =U32_MAX , +}; + +/* +* struct ssdfs_offset_position - defines offset id and position +* @cno: checkpoint of change +* @id: physical offset ID +* @peb_index: PEB's index +* @sequence_id: sequence ID of physical offset table's fragment +* @offset_index: offset index inside of fragment +* @blk_desc: logical block descriptor +*/ +struct ssdfs_offset_position { +u64 cno; +u16 id; +u16 peb_index; +u16 sequence_id; +u16 offset_index; + +struct ssdfs_block_descriptor_state blk_desc; +}; + +/* +* struct ssdfs_migrating_block - migrating block state +* @state: logical block's state +* @peb_index: PEB's index +* @sequence_id: fragment's sequence_id in PEB +* @batch: copy of logical block's content (under migration only) +*/ +struct ssdfs_migrating_block { +int state; +u16 peb_index; +u16 sequence_id; +struct folio_batch batch; +}; + +/* +* Migrating block's states +*/ +enum { +SSDFS_LBLOCK_UNKNOWN_STATE , +SSDFS_LBLOCK_UNDER_MIGRATION , +SSDFS_LBLOCK_UNDER_COMMIT , +SSDFS_LBLOCK_STATE_MAX +}; + +enum { +SSDFS_LBMAP_INIT_INDEX , +SSDFS_LBMAP_STATE_INDEX , +SSDFS_LBMAP_MODIFICATION_INDEX , +SSDFS_LBMAP_ARRAY_MAX , +}; + + +/* +* struct ssdfs_bitmap_array - bitmap array +* @bits_count: number of available bits in every bitmap +* @bytes_count: number of allocated bytes in every bitmap +* @array: array of bitmaps +*/ +struct ssdfs_bitmap_array { +u32 bits_count; +u32 bytes_count; +unsigned long *array[SSDFS_LBMAP_ARRAY_MAX ]; +}; + +/* +* struct ssdfs_blk2off_table - in-core translation table +* @flags: flags of translation table +* @state: translation table object state +* @pages_per_peb: pages per physical erase block +* @pages_per_seg: pages per segment +* @type: translation table type +* @translation_lock: lock of translation operation +* @init_cno: last actual checkpoint +* @used_logical_blks: count of used logical blocks +* @free_logical_blks: count of free logical blocks +* @last_allocated_blk: last allocated block (hint for allocation) +* @lbmap: array of block bitmaps +* @lblk2off: array of correspondence between logical numbers and phys off ids +* @migrating_blks: array of migrating blocks +* @lblk2off_capacity: capacity of correspondence array +* @peb: sequence of physical offset arrays +* @pebs_count: count of PEBs in segment +* @partial_init_end: wait of partial init ending +* @full_init_end: wait of full init ending +* @wait_queue: wait queue of blk2off table +* @fsi: pointer on shared file system object +*/ +struct ssdfs_blk2off_table { +atomic_t flags; +atomic_t state; + +u32 pages_per_peb; +u32 pages_per_seg; +u8 type; + +struct rw_semaphore translation_lock; +u64 init_cno; +u16 used_logical_blks; +u16 free_logical_blks; +u16 last_allocated_blk; +struct ssdfs_bitmap_array lbmap; +struct ssdfs_dynamic_array lblk2off; +struct ssdfs_dynamic_array migrating_blks; +u16 lblk2off_capacity; + +struct ssdfs_phys_offset_table_array *peb; +u16 pebs_count; + +struct completion partial_init_end; +struct completion full_init_end; +wait_queue_head_t wait_queue; + +struct ssdfs_fs_info *fsi; +}; + +#define SSDFS_OFF_POS (ptr) \ + ((struct ssdfs_offset_position *)(ptr)) +#define SSDFS_MIGRATING_BLK (ptr) \ + ((struct ssdfs_migrating_block *)(ptr)) +#define SSDFS_TRANS_EXT (ptr) \ + ((struct ssdfs_translation_extent *)(ptr)) + +enum { +SSDFS_BLK2OFF_OBJECT_UNKNOWN , +SSDFS_BLK2OFF_OBJECT_CREATED , +SSDFS_BLK2OFF_OBJECT_PARTIAL_INIT , +SSDFS_BLK2OFF_OBJECT_COMPLETE_INIT , +SSDFS_BLK2OFF_OBJECT_STATE_MAX , +}; + +/* +* struct ssdfs_blk2off_table_snapshot - table state snapshot +* @cno: checkpoint of snapshot +* @bmap_copy: copy of modification bitmap +* @tbl_copy: copy of translation table +* @capacity: capacity of table +* @used_logical_blks: count of used logical blocks +* @free_logical_blks: count of free logical blocks +* @last_allocated_blk: last allocated block (hint for allocation) +* @peb_index: PEB index +* @start_sequence_id: sequence ID of the first dirty fragment +* @new_sequence_id: sequence ID of the first newly added fragment +* @start_offset_id: starting offset ID +* @end_offset_id: ending offset ID +* @dirty_fragments: count of dirty fragments +* @fragments_count: total count of fragments +* +* The @bmap_copy and @tbl_copy are allocated during getting +* snapshot inside of called function. Freeing of allocated +* memory SHOULD BE MADE by caller. +*/ +struct ssdfs_blk2off_table_snapshot { +u64 cno; + +unsigned long *bmap_copy; +struct ssdfs_offset_position *tbl_copy; +u16 capacity; + +u16 used_logical_blks; +u16 free_logical_blks; +u16 last_allocated_blk; + +u16 peb_index; +u16 start_sequence_id; +u16 new_sequence_id; +u16 start_offset_id; +u16 end_offset_id; +u16 dirty_fragments; +u32 fragments_count; +}; + +/* +* Inline functions +*/ + +/* +* ssdfs_blk2off_table_bmap_bytes() - calculate bmap bytes count +* @items_count: bits count in bitmap +*/ +static inline +size_t ssdfs_blk2off_table_bmap_bytes (size_t items_count) +{ +size_t bytes; + + bytes = (items_count +BITS_PER_LONG -1 ) /BITS_PER_LONG ; + bytes *=sizeof (unsigned long ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_count %zu, bmap_bytes %zu\n" , + items_count, bytes); +#endif /* CONFIG_SSDFS_DEBUG */ + +return bytes; +} + +static inline +bool is_ssdfs_logical_block_migrating (int blk_state) +{ +bool is_migrating = false; + +switch (blk_state) { +case SSDFS_LBLOCK_UNDER_MIGRATION : +case SSDFS_LBLOCK_UNDER_COMMIT : + is_migrating = true; +break ; + +default : +/* do nothing */ +break ; + } + +return is_migrating; +} + +/* Function prototypes */ +struct ssdfs_blk2off_table * +ssdfs_blk2off_table_create (struct ssdfs_fs_info *fsi, +u16 items_count,u8 type, +int state); +void ssdfs_blk2off_table_destroy (struct ssdfs_blk2off_table *table); +int ssdfs_blk2off_table_partial_clean_init (struct ssdfs_blk2off_table *table, +u16 peb_index); +int ssdfs_blk2off_table_partial_init (struct ssdfs_blk2off_table *table, +struct ssdfs_read_init_env *env, +u16 peb_index,u64 peb_id,u64 cno); +int ssdfs_blk2off_table_blk_desc_init (struct ssdfs_blk2off_table *table, +u16 logical_blk, +struct ssdfs_offset_position *pos); +int ssdfs_blk2off_table_resize (struct ssdfs_blk2off_table *table, +u16 new_items_count); +int ssdfs_blk2off_table_snapshot (struct ssdfs_blk2off_table *table, +u16 peb_index,u64 peb_id, +struct ssdfs_blk2off_table_snapshot *snapshot); +void ssdfs_blk2off_table_free_snapshot (struct ssdfs_blk2off_table_snapshot *sp); +int ssdfs_blk2off_table_extract_extents (struct ssdfs_blk2off_table_snapshot *sp, +struct ssdfs_dynamic_array *array, +u16 capacity,u16 *extent_count); + +int +ssdfs_blk2off_table_prepare_for_commit (struct ssdfs_blk2off_table *table, +u16 peb_index,u16 sequence_id, +struct ssdfs_blk2off_table_snapshot *sp); +int ssdfs_peb_store_offsets_table_header (struct ssdfs_peb_info *pebi, +struct ssdfs_blk2off_table_header *hdr, +struct ssdfs_peb_log_offset *log_offset); +int +ssdfs_peb_store_offsets_table_extents (struct ssdfs_peb_info *pebi, +struct ssdfs_dynamic_array *array, +u16 extent_count, +struct ssdfs_peb_log_offset *log_offset); +int ssdfs_peb_store_offsets_table_fragment (struct ssdfs_peb_info *pebi, +struct ssdfs_blk2off_table *table, +u16 peb_index,u16 sequence_id, +struct ssdfs_peb_log_offset *log_offset); +int ssdfs_blk2off_table_pre_delete_fragment (void *item,u64 peb_id); +int ssdfs_peb_store_offsets_table (struct ssdfs_peb_info *pebi, +struct ssdfs_metadata_descriptor *desc, +struct ssdfs_peb_log_offset *log_offset); +int +ssdfs_blk2off_table_forget_snapshot (struct ssdfs_blk2off_table *table, +struct ssdfs_blk2off_table_snapshot *sp, +struct ssdfs_dynamic_array *array, +u16 extent_count); + +bool ssdfs_blk2off_table_dirtied (struct ssdfs_blk2off_table *table, +u16 peb_index); +bool ssdfs_blk2off_table_initialized (struct ssdfs_blk2off_table *table, +u16 peb_index); + +int ssdfs_blk2off_table_get_used_logical_blks (struct ssdfs_blk2off_table *tbl, +u16 *used_blks); +int ssdfs_blk2off_table_get_offset_position (struct ssdfs_blk2off_table *table, +u16 logical_blk, +struct ssdfs_offset_position *pos); +struct ssdfs_phys_offset_descriptor * +ssdfs_blk2off_table_convert (struct ssdfs_blk2off_table *table, +u16 logical_blk,u16 *peb_index, +int *migration_state, +struct ssdfs_offset_position *pos); +int ssdfs_blk2off_table_allocate_block (struct ssdfs_blk2off_table *table, +u32 max_blk, +u16 *logical_blk); +int ssdfs_blk2off_table_allocate_extent (struct ssdfs_blk2off_table *table, +u16 len,u32 max_blk, +struct ssdfs_blk2off_range *extent); +int ssdfs_blk2off_table_change_offset (struct ssdfs_blk2off_table *table, +u16 logical_blk, +u16 peb_index, +u64 peb_id, +struct ssdfs_block_descriptor *blk_desc, +struct ssdfs_phys_offset_descriptor *off); +int ssdfs_blk2off_table_free_block (struct ssdfs_blk2off_table *table, +u16 peb_index,u64 peb_id,u16 logical_blk); +int ssdfs_blk2off_table_free_extent (struct ssdfs_blk2off_table *table, +u16 peb_index,u64 peb_id, +struct ssdfs_blk2off_range *extent); +bool is_ssdfs_blk2off_table_extent_modified (struct ssdfs_blk2off_table *table, +u16 peb_index, +struct ssdfs_blk2off_range *extent); + +int ssdfs_blk2off_table_get_block_migration (struct ssdfs_blk2off_table *table, +u16 logical_blk, +u16 peb_index); +int ssdfs_blk2off_table_set_block_migration (struct ssdfs_blk2off_table *table, +u16 logical_blk, +u16 peb_index, +struct ssdfs_segment_request *req); +int ssdfs_blk2off_table_get_block_state (struct ssdfs_blk2off_table *table, +struct ssdfs_segment_request *req); +int ssdfs_blk2off_table_update_block_state (struct ssdfs_blk2off_table *table, +struct ssdfs_segment_request *req); +int ssdfs_blk2off_table_set_block_commit (struct ssdfs_blk2off_table *table, +u16 logical_blk, +u16 peb_index); +int ssdfs_blk2off_table_revert_migration_state (struct ssdfs_blk2off_table *tbl, +u16 peb_index); + +#ifdef CONFIG_SSDFS_TESTING +int ssdfs_blk2off_table_fragment_set_clean (struct ssdfs_blk2off_table *table, +u16 peb_index,u16 sequence_id); +#else +static inline +int ssdfs_blk2off_table_fragment_set_clean (struct ssdfs_blk2off_table *table, +u16 peb_index,u16 sequence_id) +{ +SSDFS_ERR ("set fragment clean is not supported\n" ); +return -EOPNOTSUPP ; +} +#endif /* CONFIG_SSDFS_TESTING */ + +#endif /* _SSDFS_OFFSET_TRANSLATION_TABLE_H */
Signature · 1 line
2.34.1
PATCH v2 24/79 ssdfs: introduce PEB object
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS splits a partition/volume on sequence of fixed-sized segments. Every segment can include one or several Logical Erase Blocks (LEB). LEB can be mapped into "Physical" Erase Block (PEB). PEB represent concept of erase block or zone that can be allocated, be filled by logs, and be erased. PEB object keeps knowledge about PEB ID, index in segment, and current log details.
--- fs/ssdfs/peb.h | 600 ++++++++++++++++++++++++++++++++++++++++++++ fs/ssdfs/peb_init.h | 364 +++++++++++++++++++++++++++ 2 files changed, 964 insertions(+) create mode 100644 fs/ssdfs/peb.h create mode 100644 fs/ssdfs/peb_init.h
fs/ssdfs/peb.h new file +600 -0
@@ -0,0 +1,600 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/peb.h - Physical Erase Block (PEB) object declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_PEB_H +#define _SSDFS_PEB_H + +#include "request_queue.h" +#include "fingerprint_array.h" +#include "peb_init.h" + +/* +* struct ssdfs_protection_window - protection window length +* @cno_lock: lock of checkpoints set +* @create_cno: creation checkpoint +* @last_request_cno: last request checkpoint +* @reqs_count: current number of active requests +* @protected_range: last measured protected range length +* @future_request_cno: expectation to receive a next request in the future +*/ +struct ssdfs_protection_window { +spinlock_t cno_lock; +u64 create_cno; +u64 last_request_cno; +u32 reqs_count; +u64 protected_range; +u64 future_request_cno; +}; + +/* +* struct ssdfs_peb_diffs_area_metadata - diffs area's metadata +* @hdr: diffs area's table header +*/ +struct ssdfs_peb_diffs_area_metadata { +struct ssdfs_block_state_descriptor hdr; +}; + +/* +* struct ssdfs_peb_journal_area_metadata - journal area's metadata +* @hdr: journal area's table header +*/ +struct ssdfs_peb_journal_area_metadata { +struct ssdfs_block_state_descriptor hdr; +}; + +/* +* struct ssdfs_peb_area_metadata - descriptor of area's items chain +* @area.blk_desc.table: block descriptors area table +* @area.blk_desc.flush_buf: write block descriptors buffer (compression case) +* @area.blk_desc.capacity: max number of block descriptors in reserved space +* @area.blk_desc.items_count: number of items in the whole table +* @area.diffs.table: diffs area's table +* @area.journal.table: journal area's table +* @area.main.desc: main area's descriptor +* @reserved_offset: reserved write offset of table +* @sequence_id: fragment's sequence number +*/ +struct ssdfs_peb_area_metadata { +union { +struct { +struct ssdfs_area_block_table table; +struct ssdfs_peb_temp_buffer flush_buf; +int capacity; +int items_count; + } blk_desc; + +struct { +struct ssdfs_peb_diffs_area_metadata table; + } diffs; + +struct { +struct ssdfs_peb_journal_area_metadata table; + } journal; + +struct { +struct ssdfs_block_state_descriptor desc; + } main; + } area; + +u32 reserved_offset; +u8 sequence_id; +}; + +/* +* struct ssdfs_peb_area - log's area descriptor +* @has_metadata: does area contain metadata? +* @metadata: descriptor of area's items chain +* @write_offset: current write offset +* @compressed_offset: current write offset for compressed data +* @frag_offset: offset of current fragment +* @array: area's memory folios +*/ +struct ssdfs_peb_area { +bool has_metadata; +struct ssdfs_peb_area_metadata metadata; + +u32 write_offset; +u32 compressed_offset; +u32 frag_offset; +struct ssdfs_folio_array array; +}; + +/* +* struct ssdfs_blk2off_table_area - blk2off table descriptor +* @hdr: offset descriptors area table's header +* @reserved_offset: reserved header offset +* @compressed_offset: current write offset for compressed data +* @sequence_id: fragment's sequence number +*/ +struct ssdfs_blk2off_table_area { +struct ssdfs_blk2off_table_header hdr; + +u32 reserved_offset; +u32 compressed_offset; +u8 sequence_id; +}; + +/* Log possible states */ +enum { +SSDFS_LOG_UNKNOWN , +SSDFS_LOG_PREPARED , +SSDFS_LOG_INITIALIZED , +SSDFS_LOG_CREATED , +SSDFS_LOG_COMMITTED , +SSDFS_LOG_STATE_MAX , +}; + +/* +* struct ssdfs_peb_prev_log - previous log's details +* @bmap_bytes: bytes count in block bitmap of previous log +* @blk2off_bytes: bytes count in blk2off table of previous log +* @blk_desc_bytes: bytes count in blk desc table of previous log +*/ +struct ssdfs_peb_prev_log { +u32 bmap_bytes; +u32 blk2off_bytes; +u32 blk_desc_bytes; +}; + +/* +* struct ssdfs_peb_log - current log +* @lock: exclusive lock of current log +* @state: current log's state +* @sequence_id: index of partial log in the sequence +* @start_block: current log's start block index +* @reserved_blocks: metadata blocks in the log +* @free_data_blocks: free data blocks capacity +* @seg_flags: segment header's flags for the log +* @prev_log: previous log's details +* @last_log_time: creation timestamp of last log +* @last_log_cno: last log checkpoint +* @bmap_snapshot: snapshot of block bitmap +* @blk2off_tbl: blk2off table descriptor +* @area: log's areas (main, diff updates, journal) +*/ +struct ssdfs_peb_log { +struct mutex lock; +atomic_t state; +atomic_t sequence_id; +u32 start_block; +u32 reserved_blocks;/* metadata blocks in the log */ +u32 free_data_blocks;/* free data blocks capacity */ +u32 seg_flags; +struct ssdfs_peb_prev_log prev_log; +u64 last_log_time; +u64 last_log_cno; +struct ssdfs_folio_vector bmap_snapshot; +struct ssdfs_blk2off_table_area blk2off_tbl; +struct ssdfs_peb_area area[SSDFS_LOG_AREA_MAX ]; +}; + +/* +* struct ssdfs_peb_log_offset - current log offset +* @blocksize_shift: log2(block size) +* @log_blocks: count of blocks in full partial log +* @start_block: current log's start block index +* @cur_block: current block in the log +* @offset_into_block: current offset into block +*/ +struct ssdfs_peb_log_offset { +u32 blocksize_shift; +u32 log_blocks; +pgoff_t start_block; +pgoff_t cur_block; +u32 offset_into_block; +}; + +/* +* struct ssdfs_peb_deduplication - PEB deduplication environment +* @shash_tfm: message digest handle +* @fingerprints: fingeprints array +*/ +struct ssdfs_peb_deduplication { +struct crypto_shash *shash_tfm; +struct ssdfs_fingerprint_array fingerprints; +}; + +/* +* struct ssdfs_peb_info - Physical Erase Block (PEB) description +* @peb_id: PEB number +* @peb_index: PEB index +* @log_blocks: count of blocks in full partial log +* @peb_create_time: PEB creation timestamp +* @peb_migration_id: identification number of PEB in migration sequence +* @state: PEB object state +* @init_end: wait of full init ending +* @peb_state: current PEB state +* @reserved_bytes.blk_bmap: reserved bytes for block bitmap +* @reserved_bytes.blk2off_tbl: reserved bytes for blk2off table +* @reserved_bytes.blk_desc_tbl: reserved bytes for block descriptor table +* @current_log: PEB's current log +* @dedup: PEB's deduplication environment +* @read_buffer: temporary read buffers (compression case) +* @env: init environment +* @cache: PEB's memory folios +* @pebc: pointer on parent container +*/ +struct ssdfs_peb_info { +/* Static data */ +u64 peb_id; +u16 peb_index; +u32 log_blocks; + +u64 peb_create_time; + +/* +* The peb_migration_id is stored in two places: +* (1) struct ssdfs_segment_header; +* (2) struct ssdfs_blk_state_offset. +* +* The goal of peb_migration_id is to distinguish PEB +* objects during PEB object's migration. Every +* destination PEB is received the migration_id that +* is incremented migration_id value of source PEB +* object. If peb_migration_id is achieved value of +* SSDFS_PEB_MIGRATION_ID_MAX then peb_migration_id +* is started from SSDFS_PEB_MIGRATION_ID_START again. +* +* A PEB object is received the peb_migration_id value +* during the PEB object creation operation. The "clean" +* PEB object receives SSDFS_PEB_MIGRATION_ID_START +* value. The destinaton PEB object receives incremented +* peb_migration_id value of source PEB object during +* creation operation. Otherwise, the real peb_migration_id +* value is set during PEB's initialization +* by means of extracting the actual value from segment +* header. +*/ +atomic_t peb_migration_id; + +atomic_t state; +struct completion init_end; + +atomic_t peb_state; + +/* Reserved bytes */ +struct { +atomic_t blk_bmap; +atomic_t blk2off_tbl; +atomic_t blk_desc_tbl; + } reserved_bytes; + +/* Current log */ +struct ssdfs_peb_log current_log; + +/* Fingerprints array */ +#ifdef CONFIG_SSDFS_PEB_DEDUPLICATION +struct ssdfs_peb_deduplication dedup; +#endif /* CONFIG_SSDFS_PEB_DEDUPLICATION */ + +/* Read buffer */ +struct ssdfs_peb_temp_read_buffers read_buffer; + +/* Init environment */ +struct ssdfs_read_init_env env; + +/* PEB's memory folios */ +struct ssdfs_folio_array cache; + +/* Parent container */ +struct ssdfs_peb_container *pebc; +}; + +/* PEB object states */ +enum { +SSDFS_PEB_OBJECT_UNKNOWN_STATE , +SSDFS_PEB_OBJECT_CREATED , +SSDFS_PEB_OBJECT_INITIALIZED , +SSDFS_PEB_OBJECT_STATE_MAX +}; + +#define SSDFS_AREA_TYPE2INDEX (type)({ \ + int index; \ + switch (type) { \ + case SSDFS_LOG_BLK_DESC_AREA: \ + index = SSDFS_BLK_DESC_AREA_INDEX; \ + break; \ + case SSDFS_LOG_MAIN_AREA: \ + index = SSDFS_COLD_PAYLOAD_AREA_INDEX; \ + break; \ + case SSDFS_LOG_DIFFS_AREA: \ + index = SSDFS_WARM_PAYLOAD_AREA_INDEX; \ + break; \ + case SSDFS_LOG_JOURNAL_AREA: \ + index = SSDFS_HOT_PAYLOAD_AREA_INDEX; \ + break; \ + default: \ + BUG(); \ + }; \ + index; \ +}) + +#define SSDFS_AREA_TYPE2FLAG (type)({ \ + int flag; \ + switch (type) { \ + case SSDFS_LOG_BLK_DESC_AREA: \ + flag = SSDFS_LOG_HAS_BLK_DESC_CHAIN; \ + break; \ + case SSDFS_LOG_MAIN_AREA: \ + flag = SSDFS_LOG_HAS_COLD_PAYLOAD; \ + break; \ + case SSDFS_LOG_DIFFS_AREA: \ + flag = SSDFS_LOG_HAS_WARM_PAYLOAD; \ + break; \ + case SSDFS_LOG_JOURNAL_AREA: \ + flag = SSDFS_LOG_HAS_HOT_PAYLOAD; \ + break; \ + default: \ + BUG(); \ + }; \ + flag; \ +}) + +/* +* Inline functions +*/ + +/* +* ssdfs_peb_current_log_state() - check current log's state +* @pebi: pointer on PEB object +* @state: checked state +*/ +static inline +bool ssdfs_peb_current_log_state (struct ssdfs_peb_info *pebi, +int state) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (state <SSDFS_LOG_UNKNOWN || state >=SSDFS_LOG_STATE_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return atomic_read (&pebi->current_log.state) >= state; +} + +/* +* ssdfs_peb_set_current_log_state() - set current log's state +* @pebi: pointer on PEB object +* @state: new log's state +*/ +static inline +void ssdfs_peb_set_current_log_state (struct ssdfs_peb_info *pebi, +int state) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (state <SSDFS_LOG_UNKNOWN || state >=SSDFS_LOG_STATE_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return atomic_set (&pebi->current_log.state, state); +} + +/* +* IS_SSDFS_BLK_STATE_OFFSET_INVALID() - check that block state offset invalid +* @desc: block state offset +*/ +static inline +bool IS_SSDFS_BLK_STATE_OFFSET_INVALID (struct ssdfs_blk_state_offset *desc) +{ +if (!desc) +return true; + +if (le16_to_cpu (desc->log_start_page) ==U16_MAX && + desc->log_area ==U8_MAX && + desc->peb_migration_id ==U8_MAX && +le32_to_cpu (desc->byte_offset) ==U32_MAX ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("log_start_block %u, log_area %u, " +"peb_migration_id %u, byte_offset %u\n" , +le16_to_cpu (desc->log_start_page), + desc->log_area, + desc->peb_migration_id, +le32_to_cpu (desc->byte_offset)); +#endif /* CONFIG_SSDFS_DEBUG */ +return true; + } + +if (desc->peb_migration_id ==SSDFS_PEB_UNKNOWN_MIGRATION_ID ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("log_start_block %u, log_area %u, " +"peb_migration_id %u, byte_offset %u\n" , +le16_to_cpu (desc->log_start_page), + desc->log_area, + desc->peb_migration_id, +le32_to_cpu (desc->byte_offset)); +#endif /* CONFIG_SSDFS_DEBUG */ +return true; + } + +return false; +} + +/* +* SSDFS_BLK_DESC_INIT() - init block descriptor +* @blk_desc: block descriptor +*/ +static inline +void SSDFS_BLK_DESC_INIT (struct ssdfs_block_descriptor *blk_desc) +{ +if (!blk_desc) { +SSDFS_WARN ("block descriptor pointer is NULL\n" ); +return ; + } + +memset (blk_desc,0xFF ,sizeof (struct ssdfs_block_descriptor )); +} + +/* +* IS_SSDFS_BLK_DESC_EXHAUSTED() - check that block descriptor is exhausted +* @blk_desc: block descriptor +*/ +static inline +bool IS_SSDFS_BLK_DESC_EXHAUSTED (struct ssdfs_block_descriptor *blk_desc) +{ +struct ssdfs_blk_state_offset *offset =NULL ; + +if (!blk_desc) +return true; + + offset = &blk_desc->state[SSDFS_BLK_STATE_OFF_MAX -1 ]; + +if (!IS_SSDFS_BLK_STATE_OFFSET_INVALID (offset)) +return true; + +return false; +} + +static inline +bool IS_SSDFS_BLK_DESC_READY_FOR_DIFF (struct ssdfs_block_descriptor *blk_desc) +{ +return !IS_SSDFS_BLK_STATE_OFFSET_INVALID (&blk_desc->state[0 ]); +} + +static inline +u8 SSDFS_GET_BLK_DESC_MIGRATION_ID (struct ssdfs_block_descriptor *blk_desc) +{ +if (IS_SSDFS_BLK_STATE_OFFSET_INVALID (&blk_desc->state[0 ])) +return U8_MAX ; + +return blk_desc->state[0 ].peb_migration_id; +} + +static inline +void DEBUG_BLOCK_DESCRIPTOR (u64 seg_id,u64 peb_id, +struct ssdfs_block_descriptor *blk_desc) +{ +#ifdef CONFIG_SSDFS_DEBUG +int i; + +SSDFS_DBG ("seg_id %llu, peb_id %llu, ino %llu, " +"logical_offset %u, peb_index %u, peb_page %u\n" , + seg_id, peb_id, +le64_to_cpu (blk_desc->ino), +le32_to_cpu (blk_desc->logical_offset), +le16_to_cpu (blk_desc->peb_index), +le16_to_cpu (blk_desc->peb_page)); + +for (i =0 ; i <SSDFS_BLK_STATE_OFF_MAX ; i++) { +SSDFS_DBG ("BLK STATE OFFSET %d: " +"log_start_page %u, log_area %#x, " +"byte_offset %u, peb_migration_id %u\n" , + i, +le16_to_cpu (blk_desc->state[i].log_start_page), + blk_desc->state[i].log_area, +le32_to_cpu (blk_desc->state[i].byte_offset), + blk_desc->state[i].peb_migration_id); + } +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* PEB object's API +*/ +int ssdfs_peb_object_create (struct ssdfs_peb_info *pebi, +struct ssdfs_peb_container *pebc, +u64 peb_id,int peb_state, +u8 peb_migration_id); +int ssdfs_peb_object_destroy (struct ssdfs_peb_info *pebi); +void ssdfs_peb_current_log_init (struct ssdfs_peb_info *pebi, +u32 free_blocks, +u32 start_block, +int sequence_id, +struct ssdfs_peb_prev_log *prev_log); +u64 ssdfs_get_leb_id_for_peb_index (struct ssdfs_fs_info *fsi, +u64 seg,u32 peb_index); +u64 ssdfs_get_seg_id_for_leb_id (struct ssdfs_fs_info *fsi, +u64 leb_id); +int ssdfs_get_peb_migration_id (struct ssdfs_peb_info *pebi); +bool is_peb_migration_id_valid (int peb_migration_id); +int ssdfs_get_peb_migration_id_checked (struct ssdfs_peb_info *pebi); +void ssdfs_set_peb_migration_id (struct ssdfs_peb_info *pebi, +int id); +int __ssdfs_define_next_peb_migration_id (int prev_id); +int ssdfs_define_next_peb_migration_id (struct ssdfs_peb_info *src_peb); +int ssdfs_define_prev_peb_migration_id (struct ssdfs_peb_info *pebi); + +#ifdef CONFIG_SSDFS_PEB_DEDUPLICATION +bool is_ssdfs_block_duplicated (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_fingerprint_pair *pair); +int ssdfs_peb_deduplicate_logical_block (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_fingerprint_pair *pair, +struct ssdfs_block_descriptor *blk_desc); +bool should_ssdfs_save_fingerprint (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req); +int ssdfs_peb_save_fingerprint (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_block_descriptor *blk_desc, +struct ssdfs_fingerprint_pair *pair); +#else +static inline +bool is_ssdfs_block_duplicated (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_fingerprint_pair *pair) +{ +return false; +} +static inline +int ssdfs_peb_deduplicate_logical_block (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_fingerprint_pair *pair, +struct ssdfs_block_descriptor *blk_desc) +{ +return -EOPNOTSUPP ; +} +static inline +bool should_ssdfs_save_fingerprint (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req) +{ +return false; +} +static inline +int ssdfs_peb_save_fingerprint (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_block_descriptor *blk_desc, +struct ssdfs_fingerprint_pair *pair) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_SSDFS_PEB_DEDUPLICATION */ + +/* +* PEB internal functions declaration +*/ +int ssdfs_unaligned_read_cache (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +u32 area_offset,u32 area_size, +void *buf); +int ssdfs_peb_read_log_hdr_desc_array (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +u16 log_start_block, +struct ssdfs_metadata_descriptor *array, +size_t array_size); +u16 ssdfs_peb_estimate_min_partial_log_pages (struct ssdfs_peb_info *pebi); +u32 ssdfs_request_rest_bytes (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req); +bool is_ssdfs_peb_exhausted (struct ssdfs_fs_info *fsi, +struct ssdfs_peb_info *pebi); +bool is_ssdfs_peb_ready_to_exhaust (struct ssdfs_fs_info *fsi, +struct ssdfs_peb_info *pebi); + +#endif /* _SSDFS_PEB_H */
fs/ssdfs/peb_init.h new file +364 -0
364 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 27/79 ssdfs: introduce PEB container
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS implements a migration scheme. Migration scheme is a fundamental technique of GC overhead management. The key responsibility of the migration scheme is to guarantee the presence of data in the same segment for any update operations. Generally speaking, the migration scheme’s model is implemented on the basis of association an exhausted "Physical" Erase Block (PEB) with a clean one. The goal such association of two PEBs is to implement the gradual migration of data by means of the update operations in the initial (exhausted) PEB. As a result, the old, exhausted PEB becomes invalidated after complete data migration and it will be possible to apply the erase operation to convert it in the clean state. To implement the migration scheme concept, SSDFS introduces PEB container that includes source and destination erase blocks. PEB container object keeps the pointers on source and destination PEB objects during migration logic execution.
"Physical" Erase Block object can be in one of the possible state (clean, using, used, pre-dirty, dirty). It means that PEB container creation logic needs to define the state of particular erase block and detect that it's under migration or not. As a result, creation logic prepare proper sequence of initialization requests, add these request into request queue, and start threads that executes PEB container initialization.
--- fs/ssdfs/peb_container.h | 631 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 631 insertions(+) create mode 100644 fs/ssdfs/peb_container.h
fs/ssdfs/peb_container.h new file +631 -0
@@ -0,0 +1,631 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/peb_container.h - PEB container declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_PEB_CONTAINER_H +#define _SSDFS_PEB_CONTAINER_H + +#include "block_bitmap.h" +#include "peb.h" + +/* PEB container's array indexes */ +enum { +SSDFS_SEG_PEB1 , +SSDFS_SEG_PEB2 , +SSDFS_SEG_PEB_ITEMS_MAX +}; + +/* PEB container possible states */ +enum { +SSDFS_PEB_CONTAINER_EMPTY , +SSDFS_PEB1_SRC_CONTAINER , +SSDFS_PEB1_DST_CONTAINER , +SSDFS_PEB1_SRC_PEB2_DST_CONTAINER , +SSDFS_PEB1_SRC_EXT_PTR_DST_CONTAINER , +SSDFS_PEB2_SRC_CONTAINER , +SSDFS_PEB2_DST_CONTAINER , +SSDFS_PEB2_SRC_PEB1_DST_CONTAINER , +SSDFS_PEB2_SRC_EXT_PTR_DST_CONTAINER , +SSDFS_PEB_CONTAINER_STATE_MAX +}; + +/* +* PEB migration state +*/ +enum { +SSDFS_PEB_UNKNOWN_MIGRATION_STATE , +SSDFS_PEB_NOT_MIGRATING , +SSDFS_PEB_MIGRATION_PREPARATION , +SSDFS_PEB_RELATION_PREPARATION , +SSDFS_PEB_UNDER_MIGRATION , +SSDFS_PEB_FINISHING_MIGRATION , +SSDFS_PEB_MIGRATION_STATE_MAX +}; + +/* +* PEB migration phase +*/ +enum { +SSDFS_PEB_MIGRATION_STATUS_UNKNOWN , +SSDFS_SRC_PEB_NOT_EXHAUSTED , +SSDFS_DST_PEB_RECEIVES_DATA , +SSDFS_SHARED_ZONE_RECEIVES_DATA , +SSDFS_PEB_MIGRATION_PHASE_MAX +}; + +/* +* struct ssdfs_thread_execution_point - execution point in thread logic +* @file: file name +* @function: function name +* @code_line: line number +*/ +struct ssdfs_thread_execution_point { +const char *file; +const char *function; +u32 code_line; +}; + +/* +* struct ssdfs_thread_call_stack - thread's call stack +* @points: execution points array +* @count: current number of execution points in array +*/ +struct ssdfs_thread_call_stack { +#define SSDFS_CALL_STACK_CAPACITY (16) +struct ssdfs_thread_execution_point points[SSDFS_CALL_STACK_CAPACITY ]; +u32 count; +}; + +/* +* struct ssdfs_thread_state - PEB container's thread state +* @state: current state of the thread +* @req: pointer on segment request +* @postponed_req: pointer on postponed segment request +* @skip_finish_flush_request: should commit skip the finish request? +* @has_extent_been_invalidated: has been extent invalidated? +* @has_migration_check_requested: has migration check been requested? +* @err: current error +*/ +struct ssdfs_thread_state { +#define SSDFS_THREAD_UNKNOWN_STATE (-1) +int state; +struct ssdfs_segment_request *req; +struct ssdfs_segment_request *postponed_req; +bool has_extent_been_invalidated; +bool has_migration_check_requested; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +struct ssdfs_thread_call_stack call_stack; +int unfinished_reqs; +#endif /* CONFIG_SSDFS_DEBUG */ +}; + +/* +* struct ssdfs_peb_container - PEB container +* @peb_type: type of PEB +* @peb_index: index of PEB in the array +* @log_blocks: count of logical blocks in full log +* @peb_state: aggregated PEB container state +* @thread_state: threads state +* @threads: PEB container's threads array +* @read_rq: read requests queue +* @update_rq: update requests queue +* @crq_ptr_lock: lock of pointer on create requests queue +* @create_rq: pointer on shared new page requests queue +* @fsck_rq: online FSCK requests queue +* @parent_si: pointer on parent segment object +* @migration_lock: migration lock +* @migration_state: PEB migration state +* @migration_phase: PEB migration phase +* @items_state: items array state +* @shared_free_dst_blks: count of blocks that destination is able to share +* @migration_wq: wait queue for migration operations +* @cache_protection: PEB cache protection window +* @lock: container's internals lock +* @src_peb: pointer on source PEB +* @dst_peb: pointer on destination PEB +* @dst_peb_refs: reference counter of destination PEB (sharing counter) +* @items: buffers for PEB objects +* @peb_kobj: /sys/fs/ssdfs/<device>/<segN>/<pebN> kernel object +* @peb_kobj_unregister: completion state for <pebN> kernel object +*/ +struct ssdfs_peb_container { +/* Static data */ +u8 peb_type; +u16 peb_index; +u32 log_blocks; + +atomic_t peb_state; + +/* PEB container's threads */ +struct ssdfs_thread_state thread_state[SSDFS_PEB_THREAD_TYPE_MAX ]; +struct ssdfs_thread_info thread[SSDFS_PEB_THREAD_TYPE_MAX ]; + +/* Read requests queue */ +struct ssdfs_requests_queue read_rq; + +/* Update requests queue */ +struct ssdfs_requests_queue update_rq; + +spinlock_t pending_lock; +u32 pending_updated_user_data_pages; + +/* Shared new page requests queue */ +spinlock_t crq_ptr_lock; +struct ssdfs_requests_queue *create_rq; + +/* Online FSCK requests queue */ +#ifdef CONFIG_SSDFS_ONLINE_FSCK +struct ssdfs_requests_queue fsck_rq; +#endif /* CONFIG_SSDFS_ONLINE_FSCK */ + +/* Parent segment */ +struct ssdfs_segment_info *parent_si; + +/* Migration info */ +struct mutex migration_lock; +atomic_t migration_state; +atomic_t migration_phase; +atomic_t items_state; +atomic_t shared_free_dst_blks; +wait_queue_head_t migration_wq; + +/* PEB cache protection window */ +struct ssdfs_protection_window cache_protection; + +/* PEB objects */ +struct rw_semaphore lock; +struct ssdfs_peb_info *src_peb; +struct ssdfs_peb_info *dst_peb; +atomic_t dst_peb_refs; +struct ssdfs_peb_info items[SSDFS_SEG_PEB_ITEMS_MAX ]; + +/* /sys/fs/ssdfs/<device>/<segN>/<pebN> */ +struct kobject peb_kobj; +struct completion peb_kobj_unregister; + +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_t writeback_folios; +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +}; + +#define PEBI_PTR (pebi) \ + ((struct ssdfs_peb_info *)(pebi)) +#define PEBC_PTR (pebc) \ + ((struct ssdfs_peb_container *)(pebc)) +#define READ_RQ_PTR (pebc) \ + (&PEBC_PTR(pebc)->read_rq) + +#define SSDFS_GC_FINISH_MIGRATION (4) + +/* +* Inline functions +*/ +static inline +bool is_peb_container_empty (struct ssdfs_peb_container *pebc) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc); +#endif /* CONFIG_SSDFS_DEBUG */ + +return atomic_read (&pebc->items_state) ==SSDFS_PEB_CONTAINER_EMPTY ; +} + +/* +* is_create_requests_queue_empty() - check that create queue has requests +* @pebc: pointer on PEB container +*/ +static inline +bool is_create_requests_queue_empty (struct ssdfs_peb_container *pebc) +{ +bool is_create_rq_empty = true; + +spin_lock (&pebc->crq_ptr_lock); +if (pebc->create_rq) { + is_create_rq_empty = +is_ssdfs_requests_queue_empty (pebc->create_rq); + } +spin_unlock (&pebc->crq_ptr_lock); + +return is_create_rq_empty; +} + +/* +* have_flush_requests() - check that create or update queue have requests +* @pebc: pointer on PEB container +*/ +static inline +bool have_flush_requests (struct ssdfs_peb_container *pebc) +{ +bool is_create_rq_empty = true; +bool is_update_rq_empty = true; + + is_create_rq_empty =is_create_requests_queue_empty (pebc); + is_update_rq_empty =is_ssdfs_requests_queue_empty (&pebc->update_rq); + +return !is_create_rq_empty || !is_update_rq_empty; +} + +/* +* is_fsck_requests_queue_empty() - check that FSCK queue has requests +* @pebc: pointer on PEB container +*/ +static inline +bool is_fsck_requests_queue_empty (struct ssdfs_peb_container *pebc) +{ +#ifdef CONFIG_SSDFS_ONLINE_FSCK +return is_ssdfs_requests_queue_empty (&pebc->fsck_rq); +#else +return true; +#endif /* CONFIG_SSDFS_ONLINE_FSCK */ +} + +static inline +bool is_ssdfs_peb_containing_user_data (struct ssdfs_peb_container *pebc) +{ +return pebc->peb_type ==SSDFS_MAPTBL_DATA_PEB_TYPE ; +} + +static inline +void SSDFS_THREAD_CALL_STACK_INIT (struct ssdfs_thread_call_stack *call_stack) +{ +#ifdef CONFIG_SSDFS_DEBUG +struct ssdfs_thread_execution_point *point; +int i; + +for (i =0 ; i <SSDFS_CALL_STACK_CAPACITY ; i++) { + point = &call_stack->points[i]; + point->file =NULL ; + point->function =NULL ; + point->code_line =U32_MAX ; + } + + call_stack->count =0 ; +#endif /* CONFIG_SSDFS_DEBUG */ +} + +static inline +void SSDFS_THREAD_STATE_INIT (struct ssdfs_thread_state *thread_state) +{ + thread_state->state =SSDFS_THREAD_UNKNOWN_STATE ; + thread_state->req =NULL ; + thread_state->postponed_req =NULL ; + thread_state->has_extent_been_invalidated = false; + thread_state->has_migration_check_requested = false; + thread_state->err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_THREAD_CALL_STACK_INIT (&thread_state->call_stack); + thread_state->unfinished_reqs =0 ; +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* ssdfs_peb_container_lock() - lock PEB container +* @pebc: pointer on PEB container object +*/ +static inline +void ssdfs_peb_container_lock (struct ssdfs_peb_container *pebc) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&pebc->lock); +mutex_lock (&pebc->migration_lock); +} + +/* +* ssdfs_peb_container_unlock() - unlock PEB container +* @pebc: pointer on PEB container object +*/ +static inline +void ssdfs_peb_container_unlock (struct ssdfs_peb_container *pebc) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc); +WARN_ON (!mutex_is_locked (&pebc->migration_lock)); +WARN_ON (!rwsem_is_locked (&pebc->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +mutex_unlock (&pebc->migration_lock); +up_read (&pebc->lock); +} + +/* +* is_ssdfs_peb_container_locked() - is PEB container locked? +* @pebc: pointer on PEB container object +*/ +static inline +bool is_ssdfs_peb_container_locked (struct ssdfs_peb_container *pebc) +{ +return rwsem_is_locked (&pebc->lock) && +mutex_is_locked (&pebc->migration_lock); +} + +/* +* ssdfs_peb_current_log_lock() - lock current log object +* @pebi: pointer on PEB object +*/ +static inline +void ssdfs_peb_current_log_lock (struct ssdfs_peb_info *pebi) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (!rwsem_is_locked (&pebi->pebc->lock)); +BUG_ON (!mutex_is_locked (&pebi->pebc->migration_lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =mutex_lock_killable (&pebi->current_log.lock); +WARN_ON (err); +} + +/* +* ssdfs_peb_current_log_unlock() - unlock current log object +* @pebi: pointer on PEB object +*/ +static inline +void ssdfs_peb_current_log_unlock (struct ssdfs_peb_info *pebi) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (!pebi->pebc); +WARN_ON (!mutex_is_locked (&pebi->current_log.lock)); +WARN_ON (!mutex_is_locked (&pebi->pebc->migration_lock)); +WARN_ON (!rwsem_is_locked (&pebi->pebc->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +mutex_unlock (&pebi->current_log.lock); +} + +static inline +bool is_ssdfs_peb_current_log_locked (struct ssdfs_peb_info *pebi) +{ +bool is_locked; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +WARN_ON (!mutex_is_locked (&pebi->pebc->migration_lock)); +WARN_ON (!rwsem_is_locked (&pebi->pebc->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + is_locked =mutex_is_locked (&pebi->current_log.lock); + +return is_locked; +} + +/* +* can_peb_receive_new_blocks() - check that PEB can receive new blocks +* @fsi: shared file system information +* @pebc: PEB container object +* @peb_used_pages: number of used pages in PEB +*/ +static inline +bool can_peb_receive_new_blocks (struct ssdfs_fs_info *fsi, +struct ssdfs_peb_container *pebc, +int peb_used_pages) +{ +bool is_peb_under_migration = false; +bool is_peb_inflated = false; + +switch (atomic_read (&pebc->migration_state)) { +case SSDFS_PEB_MIGRATION_PREPARATION : +case SSDFS_PEB_RELATION_PREPARATION : +case SSDFS_PEB_UNDER_MIGRATION : +case SSDFS_PEB_FINISHING_MIGRATION : + is_peb_under_migration = true; +break ; + +default : + is_peb_under_migration = false; +break ; + } + + is_peb_inflated = peb_used_pages > fsi->pages_per_peb; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("is_peb_under_migration %#x, " +"is_peb_inflated %#x\n" , + is_peb_under_migration, + is_peb_inflated); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_peb_under_migration && is_peb_inflated) { +return false; + } + +return true; +} + +/* +* is_peb_preparing_migration() - check that PEB preparing migration +* @pebc: pointer on PEB container +*/ +static inline +bool is_peb_preparing_migration (struct ssdfs_peb_container *pebc) +{ +bool is_preparing = false; + +switch (atomic_read (&pebc->migration_state)) { +case SSDFS_PEB_MIGRATION_PREPARATION : +case SSDFS_PEB_RELATION_PREPARATION : +case SSDFS_PEB_FINISHING_MIGRATION : + is_preparing = true; +break ; + +default : +/* do nothing */ +break ; + } + +return is_preparing; +} + +/* +* ssdfs_thread_call_stack_remember() - remember execution point +* @stack: thread's call stack +* @file: file name pointer +* @function: function name pointer +* @code_line: code line in file +*/ +static inline +void ssdfs_thread_call_stack_remember (struct ssdfs_thread_call_stack *stack, +const char *file, +const char *function, +u32 code_line) +{ +#ifdef CONFIG_SSDFS_DEBUG +struct ssdfs_thread_execution_point *point; + +if (stack->count <SSDFS_CALL_STACK_CAPACITY ) { +if (stack->count >0 ) { + point = &stack->points[stack->count -1 ]; + +if (point->function && function) { +if (strcmp (point->function, function) ==0 ) + point->code_line = code_line; +else + goto process_new_execution_point; + }else + goto process_new_execution_point; + }else { +process_new_execution_point: + point = &stack->points[stack->count]; + point->file = file; + point->function = function; + point->code_line = code_line; + stack->count++; + } + }else + stack->count++; +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* ssdfs_thread_call_stack_forget() - forget execution point +* @stack: thread's call stack +*/ +static inline +void ssdfs_thread_call_stack_forget (struct ssdfs_thread_call_stack *stack) +{ +#ifdef CONFIG_SSDFS_DEBUG +struct ssdfs_thread_execution_point *point; + + stack->count--; + +if (stack->count <SSDFS_CALL_STACK_CAPACITY ) { + point = &stack->points[stack->count]; + + point->file =NULL ; + point->function =NULL ; + point->code_line =U32_MAX ; + } +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* PEB container's API +*/ +int ssdfs_peb_container_create (struct ssdfs_fs_info *fsi, +u64 seg,u32 peb_index, +u8 peb_type, +u32 log_pages, +struct ssdfs_segment_info *si); +void ssdfs_peb_container_destroy (struct ssdfs_peb_container *pebc); + +int ssdfs_peb_container_invalidate_block (struct ssdfs_peb_container *pebc, +struct ssdfs_phys_offset_descriptor *desc); +int ssdfs_peb_get_free_pages (struct ssdfs_peb_container *pebc); +int ssdfs_peb_get_used_data_pages (struct ssdfs_peb_container *pebc); +int ssdfs_peb_get_invalid_pages (struct ssdfs_peb_container *pebc); +int ssdfs_peb_get_pages_capacity (struct ssdfs_peb_container *pebc); + +int ssdfs_peb_join_create_requests_queue (struct ssdfs_peb_container *pebc, +struct ssdfs_requests_queue *create_rq); +void ssdfs_peb_forget_create_requests_queue (struct ssdfs_peb_container *pebc); +bool is_peb_joined_into_create_requests_queue (struct ssdfs_peb_container *pebc); + +struct ssdfs_peb_info * +ssdfs_get_current_peb_locked (struct ssdfs_peb_container *pebc); +void ssdfs_unlock_current_peb (struct ssdfs_peb_container *pebc); +struct ssdfs_peb_info * +ssdfs_get_peb_for_migration_id (struct ssdfs_peb_container *pebc, +u8 migration_id); + +int ssdfs_peb_container_create_destination (struct ssdfs_peb_container *ptr); +int ssdfs_peb_container_forget_source (struct ssdfs_peb_container *pebc); +int ssdfs_peb_container_forget_relation (struct ssdfs_peb_container *pebc); +int ssdfs_peb_container_change_state (struct ssdfs_peb_container *pebc); + +/* +* PEB container's private API +*/ +int ssdfs_peb_gc_thread_func (void *data); +int ssdfs_peb_read_thread_func (void *data); +int ssdfs_peb_flush_thread_func (void *data); +int ssdfs_peb_fsck_thread_func (void *data); + +u16 ssdfs_peb_estimate_reserved_metapages (u32 page_size,u32 pages_per_peb, +u16 log_pages,u32 pebs_per_seg, +bool is_migrating); +int ssdfs_peb_read_page (struct ssdfs_peb_container *pebc, +struct ssdfs_segment_request *req, +struct completion **end); +int ssdfs_peb_readahead_pages (struct ssdfs_peb_container *pebc, +struct ssdfs_segment_request *req, +struct completion **end); +void ssdfs_peb_mark_request_block_uptodate (struct ssdfs_peb_container *pebc, +struct ssdfs_segment_request *req, +int blk_index); +int ssdfs_peb_copy_block (struct ssdfs_peb_container *pebc, +u32 logical_blk, +struct ssdfs_segment_request *req); +int ssdfs_peb_copy_blocks_range (struct ssdfs_peb_container *pebc, +struct ssdfs_block_bmap_range *range, +struct ssdfs_segment_request *req); +int ssdfs_peb_copy_pre_alloc_block (struct ssdfs_peb_container *pebc, +u32 logical_blk, +struct ssdfs_segment_request *req); +int __ssdfs_peb_get_block_state_desc (struct ssdfs_peb_info *pebi, +struct ssdfs_segment_request *req, +struct ssdfs_metadata_descriptor *area_desc, +struct ssdfs_block_state_descriptor *desc, +u64 *cno,u64 *parent_snapshot); +int ssdfs_blk_desc_buffer_init (struct ssdfs_peb_container *pebc, +struct ssdfs_segment_request *req, +struct ssdfs_phys_offset_descriptor *desc_off, +struct ssdfs_offset_position *pos, +struct ssdfs_metadata_descriptor *array, +size_t array_size); +int ssdfs_peb_read_block_state (struct ssdfs_peb_container *pebc, +struct ssdfs_segment_request *req, +struct ssdfs_phys_offset_descriptor *desc_off, +struct ssdfs_offset_position *pos, +struct ssdfs_metadata_descriptor *array, +size_t array_size); +bool ssdfs_peb_has_dirty_folios (struct ssdfs_peb_info *pebi); +int ssdfs_collect_dirty_segments_now (struct ssdfs_fs_info *fsi); +bool can_peb_process_create_requests (struct ssdfs_peb_container *pebc); + +#endif /* _SSDFS_PEB_CONTAINER_H */
Signature · 1 line
2.34.1
PATCH v2 33/79 ssdfs: introduce segment object
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
Segment is a basic unit for allocation, manage and free a file system volume space. It's a fixed size portion of volume that can contain one or several Logical Erase Blocks (LEB). Initially, segment is empty container that has clean state. File system logic can find a clean segment by means of search operation in segment bitmap. LEBs of clean segment need to be mapped into "Physical" Erase Blocks (PEB) by using PEB mapping table. Technically speaking, not every LEB can be mapped into PEB if mapping table hasn't any clean PEB. Segment can be imagined like a container that includes an array of PEB containers. Segment object implements the logic of logical blocks allocation, prepare create and update requests. Current segment has create queue that is used to add new data into file, for example. PEB container has update queue that is used for adding update requests. Flush thread is woken up after every operation of adding request into queue. Finally, flush thread executes create/update requests and commit logs with compressed and compacted user data or metadata.
Segment object implements API of adding logical blocks into user files or metadata structures. It means that if file or metadata structure (for example, b-tree) needs to grow, then file system logic has to add/allocate new block or extent. Add/Allocate logical block operation requires several steps:
(1) Reserve logical block(s) by means decrementing/checking
the counter of free logical blocks for the whole volume;
(2) Allocate logical block ID(s) by offset translation table
of segment object;(3) Add create request into flush thread's queue;
(4) Flush thread processes create request by means of compressing user data or metadata and compact several compressed logical block into one or several memory pages;
(5) Flush thread execute commit operation by means of preparing the log (header + payload + footer) and stores into offset translation table the association of logical block ID with particular offset into log's payload.
Any file or metadata structure can be updated, truncated, or deleted. Segment object supports the update and invalidate operations with user data or metadata. SSDFS uses logical extent concept to track the location of any user data or metadata. It means that every metadata structure is described by a sequence of extents. Inode object keeps inline extents or root node of extents b-tree that tracks the location of a file's content. Extent identifies a segment ID, logical block ID, and length of extent. Segment ID is used to create or access the segment object. The segment object has offset translation table that provides the mechanism to convert a logical block ID into "Physical" Erase Block (PEB) ID. Finally, it is possible to add update or invalidation request into PEB's update queue. PEB's flush thread takes the update/invalidate requests from the queue and executes the requests. Execution of request means the creation of new log that will contain the actual state of updated or invalidated data in the log's metadata (header, block bitmap, offset translation table) and payload.
--- fs/ssdfs/current_segment.h | 116 +++ fs/ssdfs/segment.h | 1367 ++++++++++++++++++++++++++++++++++++ fs/ssdfs/segment_tree.h | 107 +++ 3 files changed, 1590 insertions(+) create mode 100644 fs/ssdfs/current_segment.h create mode 100644 fs/ssdfs/segment.h create mode 100644 fs/ssdfs/segment_tree.h
fs/ssdfs/current_segment.h new file +116 -0
@@ -0,0 +1,116 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/current_segment.h - current segment abstraction declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_CURRENT_SEGMENT_H +#define _SSDFS_CURRENT_SEGMENT_H + +/* +* struct ssdfs_current_segment - current segment container +* @lock: exclusive lock of current segment object +* @type: current segment type +* @seg_id: last known segment ID +* @real_seg: concrete current segment +* @fsi: pointer on shared file system object +*/ +struct ssdfs_current_segment { +struct mutex *lock; +int type; +u64 seg_id; +struct ssdfs_segment_info *real_seg; +struct ssdfs_fs_info *fsi; +}; + +enum { +SSDFS_CUR_DATA_SEG_LOCK , +SSDFS_CUR_LNODE_SEG_LOCK , +SSDFS_CUR_HNODE_SEG_LOCK , +SSDFS_CUR_IDXNODE_SEG_LOCK , +SSDFS_CUR_SEG_LOCK_COUNT +}; + +/* +* struct ssdfs_current_segs_array - array of current segments +* @lock: current segments array's lock +* @objects: array of pointers on current segment objects +* @buffer: buffer for all current segment objects +* @lock_buffer: array of current segments' locks +*/ +struct ssdfs_current_segs_array { +struct rw_semaphore lock; +struct ssdfs_current_segment *objects[SSDFS_CUR_SEGS_COUNT ]; +u8 buffer[sizeof (struct ssdfs_current_segment ) *SSDFS_CUR_SEGS_COUNT ]; +struct mutex lock_buffer[SSDFS_CUR_SEG_LOCK_COUNT ]; +}; + +/* +* Inline functions +*/ +static inline +bool is_ssdfs_current_segment_empty (struct ssdfs_current_segment *cur_seg) +{ +return cur_seg->real_seg ==NULL ; +} + +static inline +struct mutex *CUR_SEG2LOCK (struct ssdfs_fs_info *fsi,int seg_type) +{ +struct ssdfs_current_segs_array *array = fsi->cur_segs; + +switch (seg_type) { +case SSDFS_CUR_DATA_SEG : +case SSDFS_CUR_DATA_UPDATE_SEG : +return &array->lock_buffer[SSDFS_CUR_DATA_SEG_LOCK ]; + +case SSDFS_CUR_LNODE_SEG : +return &array->lock_buffer[SSDFS_CUR_LNODE_SEG_LOCK ]; + +case SSDFS_CUR_HNODE_SEG : +return &array->lock_buffer[SSDFS_CUR_HNODE_SEG_LOCK ]; + +case SSDFS_CUR_IDXNODE_SEG : +return &array->lock_buffer[SSDFS_CUR_IDXNODE_SEG_LOCK ]; + +default : +/* do nothing */ +break ; + } + +return NULL ; +} + +/* +* Current segment container's API +*/ +int ssdfs_current_segment_array_create (struct ssdfs_fs_info *fsi); +void ssdfs_destroy_all_curent_segments (struct ssdfs_fs_info *fsi); +void ssdfs_current_segment_array_destroy (struct ssdfs_fs_info *fsi); + +void ssdfs_current_segment_lock (struct ssdfs_current_segment *cur_seg); +void ssdfs_current_segment_unlock (struct ssdfs_current_segment *cur_seg); +bool is_ssdfs_current_segment_locked (struct ssdfs_current_segment *cur_seg); + +int ssdfs_current_segment_add (struct ssdfs_current_segment *cur_seg, +struct ssdfs_segment_info *si, +struct ssdfs_segment_search_state *state); +void ssdfs_current_segment_remove (struct ssdfs_current_segment *cur_seg); + +#endif /* _SSDFS_CURRENT_SEGMENT_H */
fs/ssdfs/segment.h new file +1367 -0
1,367 lines not on this page · raw message
fs/ssdfs/segment_tree.h new file +107 -0
107 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 38/79 ssdfs: introduce PEB mapping table
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS file system is based on the concept of logical segment that is the aggregation of Logical Erase Blocks (LEB). Moreover, initially, LEB hasn’t association with a particular "Physical" Erase Block (PEB). It means that segment could have the association not for all LEBs or, even, to have no association at all with any PEB (for example, in the case of clean segment). Generally speaking, SSDFS file system needs a special metadata structure (PEB mapping table) that is capable of associating any LEB with any PEB. The PEB mapping table is the crucial metadata structure that has several goals:
(1) mapping LEB to PEB, (2) implementation the logical extent concept,
(3) implementation the concept of PEB migration, (4) implementation of the delayed erase operation by specialized thread.
PEB mapping table describes the state of all PEBs on a particular SSDFS file system’s volume. These descriptors are split on several fragments that are distributed amongst PEBs of specialized segments. Every fragment of PEB mapping table represents the log’s payload in a specialized segment. Generally speaking, the payload’s content is split on: (1) LEB table, and (2) PEB table. The LEB table starts from the header and it contains the array of records are ordered by LEB IDs. It means that LEB ID plays the role of index in the array of records. As a result, the responsibility of LEB table is to define an index inside of PEB table. Moreover, every LEB table’s record defines two indexes. The first index (physical index) associates the LEB ID with some PEB ID. Additionally, the second index (relation index) is able to define a PEB ID that plays the role of destination PEB during the migration process from the exhausted PEB into a new one. It is possible to see that PEB table starts from the header and it contains the array of PEB’s state records is ordered by PEB ID. The most important fields of the PEB’s state record
are: (1) erase cycles, (2) PEB type, (3) PEB state.
PEB type describes possible types of data that PEB could contain:
(1) user data, (2) leaf b-tree node, (3) hybrid b-tree node,
(4) index b-tree node, (5) snapshot, (6) superblock, (7) segment bitmap,
(8) PEB mapping table. PEB state describes possible states of PEB during the lifecycle: (1) clean state means that PEB contains only free NAND flash pages are ready for write operations, (2) using state means that PEB could contain valid, invalid, and free pages, (3) used state means that PEB contains only valid pages, (4) pre-dirty state means that PEB contains as valid as invalid pages only, (5) dirty state means that PEB contains only invalid pages, (6) migrating state means that PEB is under migration,
(7) pre-erase state means that PEB is added into the queue of PEBs are waiting the erase operation, (8) recovering state means that PEB will be untouched during some amount of time with the goal to recover the ability to fulfill the erase operation, (9) bad state means that PEB is unable to be used for storing the data. Generally speaking, the responsibility of PEB state is to track the passing of PEBs through various phases of their lifetime with the goal to manage the PEBs’ pool of the file system’s volume efficiently.
"Physical" Erase Block (PEB) mapping table is represented by a sequence of fragments are distributed among several segments. Every map or unmap operation marks a fragment as dirty. Flush operation requires to check the dirty state of all fragments and to flush dirty fragments on the volume by means of creation of log(s) into PEB(s) is dedicated to store mapping table's content. Flush operation is executed in several
steps: (1) prepare migration, (2) flush dirty fragments,
(3) commit logs.
Prepare migration operation is requested before mapping table flush with the goal to check the necessity to finish/start migration. Because, start/finish migration requires the modification of mapping table. However, mapping table's flush operation needs to be finished without any modifications of mapping table itself. Flush dirty fragments step implies the searching of dirty fragments and preparation of update requests for PEB(s) flush thread. Finally, commit log should be requested because metadata flush operation must be finished by storing new metadata state persistently.
Logical extent represents fundamental concept of SSDFS file system. Any piece of data or metadata on file system volume is identified by: (1) segment ID, (2) logical block ID, and
(3) length. As a result, any logical block is always located at the same segment because segment is logical portion of file system volume is always located at the same position. However, logical block's content should be located into some erase block. "Physical" Erase Block (PEB) mapping table implements mapping of Logical Erase Block (LEB) into PEB because any segment is a container for one or several LEBs. Moreover, mapping table supports migration scheme implementation. The migration scheme guarantee that logical block will be always located at the same segment even for the case of update requests.
PEB mapping table implements two fundamental methods:
(1) convert LEB to PEB; (2) map LEB to PEB. Conversion operation is required if we need to identify which particular PEB contains data for a LEB of particular segment. Mapping operation is required if a clean segment has been allocated because LEB(s) of clean segment need to be associated with PEB(s) that can store logs with user data or metadata.
Migration scheme is the fundamental technique of GC overhead management in the SSDFS file system. The key responsibility of the migration scheme is to guarantee the presence of data at the same segment for any update operations. Generally speaking, the migration scheme’s model is implemented on the basis of association an exhausted PEB with a clean one. The goal of such association of two PEBs is to implement the gradual migration of data by means of the update operations in the initial (exhausted) PEB. As a result, the old, exhausted PEB becomes invalidated after complete data migration and it will be possible to apply the erase operation to convert it in the clean state. Moreover, the destination PEB in the association changes the initial PEB for some index in the segment and, finally, it becomes the only PEB for this position. Such technique implements the concept of logical extent with the goal to decrease the write amplification issue and to manage the GC overhead. Because the logical extent concept excludes the necessity to update metadata tracking the position of user data on the file system’s volume. Generally speaking, the migration scheme is capable to decrease the GC activity significantly by means of the excluding the necessity to update metadata and by means of self-migration of data between of PEBs is triggered by regular update operations.
Mapping table supports two principal operations:
(1) add migration PEB, (2) exclude migration PEB. Operation of adding migration PEB is required for the case of starting migration. Exclude migration PEB operation is executed during finishing migration. Adding migration PEB operation implies the association an exhausted PEB with a clean one. Excluding migration PEB operation implies removing completely invalidated PEB from the association and request to TRIM/erase this PEB.
"Physical" Erase Block (PEB) mapping table has dedicated thread. This thread has goal to track the presence of dirty PEB(s) in mapping table and to execute TRIM/erase operation for dirty PEBs in the background. However, if the number of dirty PEBs is big enough, then erase operation(s) can be executed at the context of the thread that marks PEB as dirty.
--- fs/ssdfs/peb_mapping_table.h | 784 +++++++++++++++++++++++++++++++++++ 1 file changed, 784 insertions(+) create mode 100644 fs/ssdfs/peb_mapping_table.h
fs/ssdfs/peb_mapping_table.h new file +784 -0
@@ -0,0 +1,784 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/peb_mapping_table.h - PEB mapping table declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_PEB_MAPPING_TABLE_H +#define _SSDFS_PEB_MAPPING_TABLE_H + +#include "request_queue.h" + +#define SSDFS_MAPTBL_FIRST_PROTECTED_INDEX 0 +#define SSDFS_MAPTBL_PROTECTION_STEP 50 +#define SSDFS_MAPTBL_PROTECTION_RANGE 3 + +#define SSDFS_PRE_ERASE_PEB_THRESHOLD_PCT (3) +#define SSDFS_UNUSED_LEB_THRESHOLD_PCT (1) + +/* +* struct ssdfs_maptbl_flush_pair - segment/request pair +* @si: pointer on segment object +* @req: request object +*/ +struct ssdfs_maptbl_flush_pair { +struct ssdfs_segment_info *si; +struct ssdfs_segment_request req; +}; + +/* +* struct ssdfs_maptbl_fragment_desc - fragment descriptor +* @lock: fragment lock +* @state: fragment state +* @fragment_id: fragment's ID in the whole sequence +* @fragment_folios: count of memory folios in fragment +* @start_leb: start LEB of fragment +* @lebs_count: count of LEB descriptors in the whole fragment +* @lebs_per_page: count of LEB descriptors in memory folio +* @lebtbl_pages: count of memory folios are used for LEBs description +* @pebs_per_page: count of PEB descriptors in memory folio +* @stripe_pages: count of memory folios in one stripe +* @mapped_lebs: mapped LEBs count in the fragment +* @migrating_lebs: migrating LEBs count in the fragment +* @reserved_pebs: count of reserved PEBs in fragment +* @pre_erase_pebs: count of PEBs in pre-erase state per fragment +* @recovering_pebs: count of recovering PEBs per fragment +* @array: fragment's memory folios +* @init_end: wait of init ending +* @flush_req1: main flush requests array +* @flush_req2: backup flush requests array +* @flush_req_count: number of flush requests in the array +* @flush_seq_size: flush requests' array capacity +*/ +struct ssdfs_maptbl_fragment_desc { +struct rw_semaphore lock; +atomic_t state; + +u32 fragment_id; +u32 fragment_folios; + +u64 start_leb; +u32 lebs_count; + +u16 lebs_per_page; +u16 lebtbl_pages; + +u16 pebs_per_page; +u16 stripe_pages; + +u32 mapped_lebs; +u32 migrating_lebs; +u32 reserved_pebs; +u32 pre_erase_pebs; +u32 recovering_pebs; + +struct ssdfs_folio_array array; +struct completion init_end; + +struct ssdfs_maptbl_flush_pair *flush_pair1; +struct ssdfs_maptbl_flush_pair *flush_pair2; +u32 flush_req_count; +u32 flush_seq_size; + +/* /sys/fs/<ssdfs>/<device>/maptbl/fragments/fragment<N> */ +struct kobject frag_kobj; +struct completion frag_kobj_unregister; +}; + +/* Fragment's state */ +enum { +SSDFS_MAPTBL_FRAG_CREATED =0 , +SSDFS_MAPTBL_FRAG_INIT_FAILED =1 , +SSDFS_MAPTBL_FRAG_INITIALIZED =2 , +SSDFS_MAPTBL_FRAG_DIRTY =3 , +SSDFS_MAPTBL_FRAG_TOWRITE =4 , +SSDFS_MAPTBL_FRAG_STATE_MAX =5 , +}; + +/* +* struct ssdfs_maptbl_area - mapping table area +* @portion_id: sequential ID of mapping table fragment +* @folios: array of memory folio pointers +* @folios_capacity: capacity of array +* @folios_count: count of folios in array +*/ +struct ssdfs_maptbl_area { +u16 portion_id; +struct folio **folios; +size_t folios_capacity; +size_t folios_count; +}; + +/* +* struct ssdfs_peb_mapping_table - mapping table object +* @tbl_lock: mapping table lock +* @fragments_count: count of fragments +* @fragments_per_seg: count of fragments in segment +* @fragments_per_peb: count of fragments in PEB +* @fragment_bytes: count of bytes in one fragment +* @fragment_folios: count of memory folios in one fragment +* @flags: mapping table flags +* @lebs_count: count of LEBs are described by mapping table +* @pebs_count: count of PEBs are described by mapping table +* @lebs_per_fragment: count of LEB descriptors in fragment +* @pebs_per_fragment: count of PEB descriptors in fragment +* @pebs_per_stripe: count of PEB descriptors in stripe +* @stripes_per_fragment: count of stripes in fragment +* @extents: metadata extents that describe mapping table location +* @segs: array of pointers on segment objects +* @segs_count: count of segment objects are used for mapping table +* @state: mapping table's state +* @erase_op_state: state of erase operation +* @min_pre_erase_pebs: minimum number of PEBs in pre-erase state +* @total_pre_erase_pebs: total number of PEBs in pre-erase state +* @max_erase_ops: upper bound of erase operations for one iteration +* @erase_ops_end_wq: wait queue of threads are waiting end of erase operation +* @bmap_lock: dirty bitmap's lock +* @dirty_bmap: bitmap of dirty fragments +* @desc_array: array of fragment descriptors +* @wait_queue: wait queue of mapping table's thread +* @flush_end: wait of flush ending +* @thread: descriptor of mapping table's thread +* @fsi: pointer on shared file system object +*/ +struct ssdfs_peb_mapping_table { +struct rw_semaphore tbl_lock; +u32 fragments_count; +u16 fragments_per_seg; +u16 fragments_per_peb; +u32 fragment_bytes; +u32 fragment_folios; +atomic_t flags; +u64 lebs_count; +u64 pebs_count; +u16 lebs_per_fragment; +u16 pebs_per_fragment; +u16 pebs_per_stripe; +u16 stripes_per_fragment; +struct ssdfs_meta_area_extent extents[MAPTBL_LIMIT1 ][MAPTBL_LIMIT2 ]; +struct ssdfs_segment_info **segs[SSDFS_MAPTBL_SEG_COPY_MAX ]; +u16 segs_count; + +atomic_t state; + +atomic_t erase_op_state; +atomic_t min_pre_erase_pebs; +atomic_t total_pre_erase_pebs; +atomic_t max_erase_ops; +wait_queue_head_t erase_ops_end_wq; + +atomic64_t last_peb_recover_cno; + +struct mutex bmap_lock; +unsigned long *dirty_bmap; +struct ssdfs_maptbl_fragment_desc *desc_array; + +wait_queue_head_t wait_queue; +struct completion flush_end; +struct ssdfs_thread_info thread; +struct ssdfs_fs_info *fsi; +}; + +/* PEB mapping table's state */ +enum { +SSDFS_MAPTBL_CREATED =0 , +SSDFS_MAPTBL_GOING_TO_BE_DESTROY =1 , +SSDFS_MAPTBL_STATE_MAX =2 , +}; + +/* +* struct ssdfs_maptbl_peb_descriptor - PEB descriptor +* @peb_id: PEB identification number +* @shared_peb_index: index of external shared destination PEB +* @erase_cycles: P/E cycles +* @type: PEB type +* @state: PEB state +* @flags: PEB flags +* @consistency: PEB state consistency type +*/ +struct ssdfs_maptbl_peb_descriptor { +u64 peb_id; +u8 shared_peb_index; +u32 erase_cycles; +u8 type; +u8 state; +u8 flags; +u8 consistency; +}; + +/* +* struct ssdfs_maptbl_peb_relation - PEBs association +* @pebs: array of PEB descriptors +*/ +struct ssdfs_maptbl_peb_relation { +struct ssdfs_maptbl_peb_descriptor pebs[SSDFS_MAPTBL_RELATION_MAX ]; +}; + +/* +* Erase operation state +*/ +enum { +SSDFS_MAPTBL_NO_ERASE , +SSDFS_MAPTBL_ERASE_IN_PROGRESS +}; + +/* Stage of recovering try */ +enum { +SSDFS_CHECK_RECOVERABILITY , +SSDFS_MAKE_RECOVERING , +SSDFS_RECOVER_STAGE_MAX +}; + +/* Possible states of erase operation */ +enum { +SSDFS_ERASE_RESULT_UNKNOWN , +SSDFS_ERASE_DONE , +SSDFS_ERASE_SB_PEB_DONE , +SSDFS_IGNORE_ERASE , +SSDFS_ERASE_FAILURE , +SSDFS_BAD_BLOCK_DETECTED , +SSDFS_ERASE_RESULT_MAX +}; + +/* +* struct ssdfs_erase_result - PEB's erase operation result +* @fragment_index: index of mapping table's fragment +* @peb_index: PEB's index in fragment +* @peb_id: PEB ID number +* @state: state of erase operation +*/ +struct ssdfs_erase_result { +u32 fragment_index; +u16 peb_index; +u64 peb_id; +int state; +}; + +/* +* struct ssdfs_erase_result_array - array of erase operation results +* @ptr: pointer on memory buffer +* @capacity: maximal number of erase operation results in array +* @size: count of erase operation results in array +*/ +struct ssdfs_erase_result_array { +struct ssdfs_erase_result *ptr; +u32 capacity; +u32 size; +}; + +#define SSDFS_ERASE_RESULTS_PER_FRAGMENT (10) + +/* +* Inline functions +*/ + +/* +* SSDFS_ERASE_RESULT_INIT() - init erase result +* @fragment_index: index of mapping table's fragment +* @peb_index: PEB's index in fragment +* @peb_id: PEB ID number +* @state: state of erase operation +* @result: erase operation result [out] +* +* This method initializes the erase operation result. +*/ +static inline +void SSDFS_ERASE_RESULT_INIT (u32 fragment_index,u16 peb_index, +u64 peb_id,int state, +struct ssdfs_erase_result *result) +{ + result->fragment_index = fragment_index; + result->peb_index = peb_index; + result->peb_id = peb_id; + result->state = state; +} + +/* +* DEFINE_PEB_INDEX_IN_FRAGMENT() - define PEB index in the whole fragment +* @fdesc: fragment descriptor +* @folio_index: folio index in the fragment +* @item_index: item index in the memory folio +*/ +static inline +u16 DEFINE_PEB_INDEX_IN_FRAGMENT (struct ssdfs_maptbl_fragment_desc *fdesc, +pgoff_t folio_index, +u16 item_index) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fdesc); +BUG_ON (folio_index < fdesc->lebtbl_pages); + +SSDFS_DBG ("fdesc %p, folio_index %lu, item_index %u\n" , + fdesc, folio_index, item_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + folio_index -= fdesc->lebtbl_pages; + folio_index *= fdesc->pebs_per_page; + folio_index += item_index; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (folio_index >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (u16 )folio_index; +} + +/* +* GET_PEB_ID() - define PEB ID for the index +* @kaddr: pointer on memory folio's content +* @item_index: item index inside of the folio +* +* This method tries to convert @item_index into +* PEB ID value. +* +* RETURN: +* [success] - PEB ID +* [failure] - U64_MAX +*/ +static inline +u64 GET_PEB_ID (void *kaddr,u16 item_index) +{ +struct ssdfs_peb_table_fragment_header *hdr; +u64 start_peb; +u16 pebs_count; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!kaddr); + +SSDFS_DBG ("kaddr %p, item_index %u\n" , + kaddr, item_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr = (struct ssdfs_peb_table_fragment_header *)kaddr; + +if (le16_to_cpu (hdr->magic) !=SSDFS_PEB_TABLE_MAGIC ) { +SSDFS_ERR ("corrupted folio\n" ); +return U64_MAX ; + } + + start_peb =le64_to_cpu (hdr->start_peb); + pebs_count =le16_to_cpu (hdr->pebs_count); + +if (item_index >= pebs_count) { +SSDFS_ERR ("item_index %u >= pebs_count %u\n" , + item_index, pebs_count); +return U64_MAX ; + } + +return start_peb + item_index; +} + +/* +* PEBTBL_FOLIO_INDEX() - define PEB table folio index +* @fdesc: fragment descriptor +* @peb_index: index of PEB in the fragment +*/ +static inline +pgoff_t PEBTBL_FOLIO_INDEX (struct ssdfs_maptbl_fragment_desc *fdesc, +u16 peb_index) +{ +pgoff_t folio_index; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fdesc); + +SSDFS_DBG ("fdesc %p, peb_index %u\n" , + fdesc, peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + folio_index = fdesc->lebtbl_pages; + folio_index += peb_index / fdesc->pebs_per_page; +return folio_index; +} + +/* +* GET_PEB_DESCRIPTOR() - retrieve PEB descriptor +* @kaddr: pointer on memory folio's content +* @item_index: item index inside of the folio +* +* This method tries to return the pointer on +* PEB descriptor for @item_index. +* +* RETURN: +* [success] - pointer on PEB descriptor +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static inline +struct ssdfs_peb_descriptor *GET_PEB_DESCRIPTOR (void *kaddr,u16 item_index) +{ +struct ssdfs_peb_table_fragment_header *hdr; +u16 pebs_count; +u32 peb_desc_off; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!kaddr); + +SSDFS_DBG ("kaddr %p, item_index %u\n" , + kaddr, item_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr = (struct ssdfs_peb_table_fragment_header *)kaddr; + +if (le16_to_cpu (hdr->magic) !=SSDFS_PEB_TABLE_MAGIC ) { +SSDFS_ERR ("corrupted folio\n" ); +return ERR_PTR (-ERANGE ); + } + + pebs_count =le16_to_cpu (hdr->pebs_count); + +if (item_index >= pebs_count) { +SSDFS_ERR ("item_index %u >= pebs_count %u\n" , + item_index, pebs_count); +return ERR_PTR (-ERANGE ); + } + + peb_desc_off =SSDFS_PEBTBL_FRAGMENT_HDR_SIZE ; + peb_desc_off += item_index *sizeof (struct ssdfs_peb_descriptor ); + +if (peb_desc_off >=PAGE_SIZE ) { +SSDFS_ERR ("invalid offset %u\n" , peb_desc_off); +return ERR_PTR (-ERANGE ); + } + +return (struct ssdfs_peb_descriptor *)((u8 *)kaddr + peb_desc_off); +} + +/* +* SEG2PEB_TYPE() - convert segment into PEB type +*/ +static inline +int SEG2PEB_TYPE (int seg_type) +{ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_type %d\n" , seg_type); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (seg_type) { +case SSDFS_USER_DATA_SEG_TYPE : +return SSDFS_MAPTBL_DATA_PEB_TYPE ; + +case SSDFS_LEAF_NODE_SEG_TYPE : +return SSDFS_MAPTBL_LNODE_PEB_TYPE ; + +case SSDFS_HYBRID_NODE_SEG_TYPE : +return SSDFS_MAPTBL_HNODE_PEB_TYPE ; + +case SSDFS_INDEX_NODE_SEG_TYPE : +return SSDFS_MAPTBL_IDXNODE_PEB_TYPE ; + +case SSDFS_INITIAL_SNAPSHOT_SEG_TYPE : +return SSDFS_MAPTBL_INIT_SNAP_PEB_TYPE ; + +case SSDFS_SB_SEG_TYPE : +return SSDFS_MAPTBL_SBSEG_PEB_TYPE ; + +case SSDFS_SEGBMAP_SEG_TYPE : +return SSDFS_MAPTBL_SEGBMAP_PEB_TYPE ; + +case SSDFS_MAPTBL_SEG_TYPE : +return SSDFS_MAPTBL_MAPTBL_PEB_TYPE ; + } + +return SSDFS_MAPTBL_PEB_TYPE_MAX ; +} + +/* +* PEB2SEG_TYPE() - convert PEB into segment type +*/ +static inline +int PEB2SEG_TYPE (int peb_type) +{ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("peb_type %d\n" , peb_type); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (peb_type) { +case SSDFS_MAPTBL_DATA_PEB_TYPE : +return SSDFS_USER_DATA_SEG_TYPE ; + +case SSDFS_MAPTBL_LNODE_PEB_TYPE : +return SSDFS_LEAF_NODE_SEG_TYPE ; + +case SSDFS_MAPTBL_HNODE_PEB_TYPE : +return SSDFS_HYBRID_NODE_SEG_TYPE ; + +case SSDFS_MAPTBL_IDXNODE_PEB_TYPE : +return SSDFS_INDEX_NODE_SEG_TYPE ; + +case SSDFS_MAPTBL_INIT_SNAP_PEB_TYPE : +return SSDFS_INITIAL_SNAPSHOT_SEG_TYPE ; + +case SSDFS_MAPTBL_SBSEG_PEB_TYPE : +return SSDFS_SB_SEG_TYPE ; + +case SSDFS_MAPTBL_SEGBMAP_PEB_TYPE : +return SSDFS_SEGBMAP_SEG_TYPE ; + +case SSDFS_MAPTBL_MAPTBL_PEB_TYPE : +return SSDFS_MAPTBL_SEG_TYPE ; + } + +return SSDFS_UNKNOWN_SEG_TYPE ; +} + +static inline +bool is_ssdfs_maptbl_under_flush (struct ssdfs_fs_info *fsi) +{ +return atomic_read (&fsi->maptbl->flags) &SSDFS_MAPTBL_UNDER_FLUSH ; +} + +static inline +bool is_ssdfs_maptbl_start_migration (struct ssdfs_fs_info *fsi) +{ +return atomic_read (&fsi->maptbl->flags) &SSDFS_MAPTBL_START_MIGRATION ; +} + +/* +* is_peb_protected() - check that PEB is protected +* @found_item: PEB index in the fragment +*/ +static inline +bool is_peb_protected (unsigned long found_item) +{ +unsigned long remainder; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found_item %lu\n" , found_item); +#endif /* CONFIG_SSDFS_DEBUG */ + + remainder = found_item %SSDFS_MAPTBL_PROTECTION_STEP ; +return remainder ==0 ; +} + +static inline +bool is_ssdfs_maptbl_going_to_be_destroyed (struct ssdfs_peb_mapping_table *tbl) +{ +return atomic_read (&tbl->state) ==SSDFS_MAPTBL_GOING_TO_BE_DESTROY ; +} + +static inline +void set_maptbl_going_to_be_destroyed (struct ssdfs_fs_info *fsi) +{ +atomic_set (&fsi->maptbl->state,SSDFS_MAPTBL_GOING_TO_BE_DESTROY ); +} + +static inline +void ssdfs_account_updated_user_data_pages (struct ssdfs_fs_info *fsi, +u32 count) +{ +#ifdef CONFIG_SSDFS_DEBUG +u64 updated =0 ; + +BUG_ON (!fsi); + +SSDFS_DBG ("fsi %p, count %u\n" , + fsi, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&fsi->volume_state_lock); + fsi->updated_user_data_pages += count; +#ifdef CONFIG_SSDFS_DEBUG + updated = fsi->updated_user_data_pages; +#endif /* CONFIG_SSDFS_DEBUG */ +spin_unlock (&fsi->volume_state_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("updated %llu\n" , updated); +#endif /* CONFIG_SSDFS_DEBUG */ +} + +int ssdfs_maptbl_change_peb_state (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +int peb_state, +struct completion **end); + +/* +* ssdfs_maptbl_wait_and_change_peb_state() - wait and change PEB state +* @fsi: file system info object +* @leb_id: LEB ID number +* @peb_type: type of the PEB +* @peb_state: new state of the PEB +*/ +static inline +int ssdfs_maptbl_wait_and_change_peb_state (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +int peb_state) +{ +struct completion *end; +int number_of_tries =0 ; +int err =0 ; + + err =ssdfs_maptbl_change_peb_state (fsi, leb_id, + peb_type, peb_state, + &end); +if (err == -EAGAIN ) { +wait_completion_end: + err =SSDFS_WAIT_COMPLETION (end); +if (unlikely (err)) { +SSDFS_ERR ("waiting failed: " +"err %d\n" , err); +return err; + } + + err =ssdfs_maptbl_change_peb_state (fsi, + leb_id, + peb_type, + peb_state, + &end); +if (err == -EAGAIN &&is_ssdfs_maptbl_under_flush (fsi)) { +if (number_of_tries <SSDFS_MAX_NUMBER_OF_TRIES ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("mapping table is flushing: " +"leb_id %llu, peb_type %#x, " +"new_state %#x, number_of_tries %d\n" , + leb_id, peb_type, peb_state, + number_of_tries); +#endif /* CONFIG_SSDFS_DEBUG */ + number_of_tries++; + goto wait_completion_end; + } + } + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to change the PEB state: " +"leb_id %llu, peb_type %#x, " +"new_state %#x, err %d\n" , + leb_id, peb_type, + peb_state, err); + } + +return err; +} + +/* +* PEB mapping table's API +*/ +int ssdfs_maptbl_create (struct ssdfs_fs_info *fsi); +void ssdfs_maptbl_destroy (struct ssdfs_fs_info *fsi); +int ssdfs_maptbl_fragment_init (struct ssdfs_peb_container *pebc, +struct ssdfs_maptbl_area *area); +int ssdfs_maptbl_flush (struct ssdfs_peb_mapping_table *tbl); +int ssdfs_maptbl_resize (struct ssdfs_peb_mapping_table *tbl, +u64 new_pebs_count); + +int ssdfs_maptbl_convert_leb2peb (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +struct ssdfs_maptbl_peb_relation *pebr, +struct completion **end); +int ssdfs_maptbl_map_leb2peb (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +struct ssdfs_maptbl_peb_relation *pebr, +struct completion **end); +int ssdfs_maptbl_recommend_search_range (struct ssdfs_fs_info *fsi, +u64 *start_leb, +u64 *end_leb, +struct completion **end); +int ssdfs_maptbl_change_peb_state (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +int peb_state, +struct completion **end); +int ssdfs_maptbl_prepare_pre_erase_state (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +struct completion **end); +int ssdfs_maptbl_set_pre_erased_snapshot_peb (struct ssdfs_fs_info *fsi, +u64 peb_id, +struct completion **end); +int ssdfs_maptbl_add_migration_peb (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +struct ssdfs_maptbl_peb_relation *pebr, +struct completion **end); +int ssdfs_maptbl_exclude_migration_peb (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +u64 peb_create_time, +u64 last_log_time, +struct completion **end); +int ssdfs_maptbl_set_indirect_relation (struct ssdfs_peb_mapping_table *tbl, +u64 leb_id,u8 peb_type, +u64 dst_leb_id,u16 dst_peb_index, +struct completion **end); +int ssdfs_maptbl_break_indirect_relation (struct ssdfs_peb_mapping_table *tbl, +u64 leb_id,u8 peb_type, +u64 dst_leb_id,int dst_peb_refs, +struct completion **end); +int ssdfs_maptbl_set_zns_indirect_relation (struct ssdfs_peb_mapping_table *tbl, +u64 leb_id,u8 peb_type, +struct completion **end); +int ssdfs_maptbl_break_zns_indirect_relation (struct ssdfs_peb_mapping_table *tbl, +u64 leb_id,u8 peb_type, +struct completion **end); + +int ssdfs_reserve_free_pages (struct ssdfs_fs_info *fsi, +u32 count,int type); + +/* +* It makes sense to have special thread for the whole mapping table. +* The goal of the thread will be clearing of dirty PEBs, +* tracking P/E cycles, excluding bad PEBs and recovering PEBs +* in the background. Knowledge about PEBs will be hidden by +* mapping table. All other subsystems will operate by LEBs. +*/ + +/* +* PEB mapping table's internal API +*/ +int ssdfs_maptbl_start_thread (struct ssdfs_peb_mapping_table *tbl); +int ssdfs_maptbl_stop_thread (struct ssdfs_peb_mapping_table *tbl); + +int ssdfs_maptbl_define_fragment_info (struct ssdfs_fs_info *fsi, +u64 leb_id, +u16 *pebs_per_fragment, +u16 *pebs_per_stripe, +u16 *stripes_per_fragment); +struct ssdfs_maptbl_fragment_desc * +ssdfs_maptbl_get_fragment_descriptor (struct ssdfs_peb_mapping_table *tbl, +u64 leb_id); +void ssdfs_maptbl_set_fragment_dirty (struct ssdfs_peb_mapping_table *tbl, +struct ssdfs_maptbl_fragment_desc *fdesc, +u64 leb_id,u8 peb_type); +int ssdfs_maptbl_solve_inconsistency (struct ssdfs_peb_mapping_table *tbl, +struct ssdfs_maptbl_fragment_desc *fdesc, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr); +int ssdfs_maptbl_solve_pre_deleted_state (struct ssdfs_peb_mapping_table *tbl, +struct ssdfs_maptbl_fragment_desc *fdesc, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr); +void ssdfs_maptbl_move_fragment_folios (struct ssdfs_segment_request *req, +struct ssdfs_maptbl_area *area, +u16 folios_count); +int ssdfs_maptbl_erase_peb (struct ssdfs_fs_info *fsi, +struct ssdfs_erase_result *result); +int ssdfs_maptbl_correct_dirty_peb (struct ssdfs_peb_mapping_table *tbl, +struct ssdfs_maptbl_fragment_desc *fdesc, +struct ssdfs_erase_result *result); +int __ssdfs_maptbl_correct_peb_state (struct ssdfs_peb_mapping_table *tbl, +struct ssdfs_maptbl_fragment_desc *fdesc, +struct ssdfs_peb_table_fragment_header *hdr, +struct ssdfs_erase_result *res); +int ssdfs_maptbl_erase_reserved_peb_now (struct ssdfs_fs_info *fsi, +u64 leb_id,u8 peb_type, +struct completion **end); +int ssdfs_maptbl_erase_dirty_pebs_now (struct ssdfs_peb_mapping_table *tbl); + +void ssdfs_debug_maptbl_object (struct ssdfs_peb_mapping_table *tbl); + +#endif /* _SSDFS_PEB_MAPPING_TABLE_H */
Signature · 1 line
2.34.1
PATCH v2 40/79 ssdfs: introduce PEB mapping table cache
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
"Physical" Erase Block (PEB) mapping table is enhanced by special cache is stored in the payload of superblock segment’s log. Generally speaking, the cache stores the copy of records of PEBs’ state. The goal of PEB mapping table’s cache is to resolve the case when a PEB’s descriptor is associated with a LEB of PEB mapping table itself, for example. If unmount operation triggers the flush of PEB mapping table then there are the cases when the PEB mapping table could be modified during the flush operation’s activity. As a result, actual PEB’s state is stored only into PEB mapping table’s cache. Such record is marked as inconsistent and the inconsistency has to be resolved during the next mount operation by means of storing the actual PEB’s state into the PEB mapping table by specialized thread. Moreover, the cache plays another very important role. Namely, PEB mapping table’s cache is used for conversion the LEB ID into PEB ID for the case of basic metadata structures (PEB mapping table, segment bitmap, for example) before the finishing of PEB mapping table initialization during the mount operation.
PEB mapping table’s cache starts from the header that precedes
to: (1) LEB ID / PEB ID pairs, (2) PEB state records. The pairs’ area associates the LEB IDs with PEB IDs. Additionally, PEB state records’ area contains information about the last actual state of PEBs for every record in the pairs’ area. It makes sense to point out that the most important fields in PEB state area are: (1) consistency, (2) PEB state, and (3) PEB flags. Generally speaking, the consistency field simply shows that a record in the cache and mapping table is identical or not. If some record in the cache has marked as inconsistent then it means that the PEB mapping table has to be modified with the goal to keep the actual value of the cache. As a result, finally, the value in the table and the cache will be consistent.
"Physical" Erase Block (PEB) mapping table cache supports operations:
(1) convert LEB to PEB - convert LEB to PEB if mapping table
is not initialized yet
(2) map LEB to PEB - cache information about LEB to PEB mapping
(3) forget LEB to PEB - exclude information about LEB to PEB mapping from the cache
(4) change PEB state - update cached information about LEB to PEB mapping
(5) add migration PEB - cache information about migration destination
(6) exclude migration PEB - exclude information about migration destination
--- fs/ssdfs/peb_mapping_table_cache.h | 120 +++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 fs/ssdfs/peb_mapping_table_cache.h
fs/ssdfs/peb_mapping_table_cache.h new file +120 -0
@@ -0,0 +1,120 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/peb_mapping_table_cache.h - PEB mapping table cache declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_PEB_MAPPING_TABLE_CACHE_H +#define _SSDFS_PEB_MAPPING_TABLE_CACHE_H + +#include <linux/ssdfs_fs.h> + +/* +* struct ssdfs_maptbl_cache - maptbl cache +* @lock: lock of maptbl cache +* @batch: memory folios of maptbl cache +* @bytes_count: count of bytes in maptbl cache +* @pm_queue: PEB mappings queue +*/ +struct ssdfs_maptbl_cache { +struct rw_semaphore lock; +struct folio_batch batch; +atomic_t bytes_count; + +struct ssdfs_peb_mapping_queue pm_queue; +}; + +/* +* struct ssdfs_maptbl_cache_item - cache item descriptor +* @folio_index: index of the found memory folio +* @item_index: item of found index +* @found: found LEB2PEB pair +*/ +struct ssdfs_maptbl_cache_item { +#define SSDFS_MAPTBL_CACHE_ITEM_UNKNOWN (0) +#define SSDFS_MAPTBL_CACHE_ITEM_FOUND (1) +#define SSDFS_MAPTBL_CACHE_ITEM_ABSENT (2) +#define SSDFS_MAPTBL_CACHE_SEARCH_ERROR (3) +#define SSDFS_MAPTBL_CACHE_SEARCH_MAX (4) +int state; +unsigned folio_index; +u16 item_index; +struct ssdfs_leb2peb_pair found; +}; + +#define SSDFS_MAPTBL_MAIN_INDEX (0) +#define SSDFS_MAPTBL_RELATION_INDEX (1) +#define SSDFS_MAPTBL_RELATION_MAX (2) + +/* +* struct ssdfs_maptbl_cache_search_result - PEBs association +* @pebs: array of PEB descriptors +*/ +struct ssdfs_maptbl_cache_search_result { +struct ssdfs_maptbl_cache_item pebs[SSDFS_MAPTBL_RELATION_MAX ]; +}; + +struct ssdfs_maptbl_peb_relation ; + +/* +* PEB mapping table cache's API +*/ +void ssdfs_maptbl_cache_init (struct ssdfs_maptbl_cache *cache); +void ssdfs_maptbl_cache_destroy (struct ssdfs_maptbl_cache *cache); + +int ssdfs_maptbl_cache_convert_leb2peb (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr); +int ssdfs_maptbl_cache_map_leb2peb (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr, +int consistency); +int ssdfs_maptbl_cache_forget_leb2peb (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +int consistency); +int ssdfs_maptbl_cache_change_peb_state (struct ssdfs_maptbl_cache *cache, +u64 leb_id,int peb_state, +int consistency); +int ssdfs_maptbl_cache_add_migration_peb (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr, +int consistency); +int ssdfs_maptbl_cache_exclude_migration_peb (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +int consistency); + +/* +* PEB mapping table cache's internal API +*/ +struct folio * +ssdfs_maptbl_cache_add_batch_folio (struct ssdfs_maptbl_cache *cache); +int ssdfs_maptbl_cache_convert_leb2peb_nolock (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr); +int __ssdfs_maptbl_cache_convert_leb2peb (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +struct ssdfs_maptbl_peb_relation *pebr); +int ssdfs_maptbl_cache_change_peb_state_nolock (struct ssdfs_maptbl_cache *cache, +u64 leb_id,int peb_state, +int consistency); +int ssdfs_maptbl_cache_forget_leb2peb_nolock (struct ssdfs_maptbl_cache *cache, +u64 leb_id, +int consistency); + +#endif /* _SSDFS_PEB_MAPPING_TABLE_CACHE_H */
Signature · 1 line
2.34.1
PATCH v2 42/79 ssdfs: introduce segment bitmap
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
Segment bitmap is the critical metadata structure of SSDFS file system that implements several goals: (1) searching for a candidate for a current segment capable of storing new data, (2) searching by GC subsystem for a most optimal segment (dirty state, for example) with the goal of preparing the segment in background for storing new data (converting in clean state). Segment bitmap is able to represent a set of states: (1) clean state means that a segment contains the free logical blocks only, (2) using state means that a segment could contain valid, invalid, and free logical blocks,
(3) used state means that a segment contains the valid logical blocks only, (4) pre-dirty state means that a segment contains valid and invalid logical blocks, (5) dirty state means that a segment contains only invalid blocks, (6) reserved state is used for reservation the segment numbers for some metadata structures (for example, for the case of superblock segment).
PEB migration scheme implies that segments are able to migrate from one state into another one without the explicit using of GC subsystem. For example, if some segment receives enough truncate operations (data invalidation) then the segment could change the used state in pre-dirty state. Additionally, the segment is able to migrate from pre-dirty into using state by means of PEBs migration in the case of receiving enough data update requests. As a result, the segment in using state could be selected like the current segment without any GC-related activity. However, a segment is able to stick in pre-dirty state in the case of absence the update requests. Finally, such situation can be resolved by GC subsystem by means of valid blocks migration in the background and pre-dirty segment can be transformed into the using state.
Segment bitmap is implemented like the bitmap metadata structure that is split on several fragments. Every fragment is stored into a log of specialized PEB. As a result, the full size of segment bitmap and PEB’s capacity define the number of fragments. The mkfs utility reserves the necessary number of segments for storing the segment bitmap’s fragments during a SSDFS file system’s volume creation. Finally, the numbers of reserved segments are stored into the superblock metadata structure. The segment bitmap ”lives” in the same set of reserved segments during the whole lifetime of the volume. However, the update operations of segment bitmap could trigger the PEBs migration in the case of exhaustion of any PEB used for keeping the segment bitmap’s content.
Segment bitmap implements API:
(1) create - create empty segment bitmap object
(2) destroy - destroy segment bitmap object
(3) fragment_init - init fragment of segment bitmap
(4) flush - flush dirty segment bitmap
(5) check_state - check that segment has particular state
(6) get_state - get current state of particular segment
(7) change_state - change state of segment
(8) find - find segment for requested state or state mask
(9) find_and_set - find segment for requested state and change state
--- fs/ssdfs/segment_bitmap.h | 482 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 482 insertions(+) create mode 100644 fs/ssdfs/segment_bitmap.h
fs/ssdfs/segment_bitmap.h new file +482 -0
@@ -0,0 +1,482 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/segment_bitmap.h - segment bitmap declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Badic +*/ + +#ifndef _SSDFS_SEGMENT_BITMAP_H +#define _SSDFS_SEGMENT_BITMAP_H + +#include "common_bitmap.h" +#include "request_queue.h" +#include "folio_array.h" + +/* Segment states */ +enum { +SSDFS_SEG_CLEAN =0x0 , +SSDFS_SEG_DATA_USING =0x1 , +SSDFS_SEG_LEAF_NODE_USING =0x2 , +SSDFS_SEG_HYBRID_NODE_USING =0x5 , +SSDFS_SEG_INDEX_NODE_USING =0x3 , +SSDFS_SEG_USED =0x7 , +SSDFS_SEG_PRE_DIRTY =0x6 , +SSDFS_SEG_DIRTY =0x4 , +SSDFS_SEG_BAD =0x8 , +SSDFS_SEG_RESERVED =0x9 , +SSDFS_SEG_DATA_USING_INVALIDATED =0xA , +SSDFS_SEG_STATE_MAX =0xB +}; + +/* Segment state flags */ +#define SSDFS_SEG_CLEAN_STATE_FLAG (1 << 0) +#define SSDFS_SEG_DATA_USING_STATE_FLAG (1 << 1) +#define SSDFS_SEG_LEAF_NODE_USING_STATE_FLAG (1 << 2) +#define SSDFS_SEG_HYBRID_NODE_USING_STATE_FLAG (1 << 3) +#define SSDFS_SEG_INDEX_NODE_USING_STATE_FLAG (1 << 4) +#define SSDFS_SEG_USED_STATE_FLAG (1 << 5) +#define SSDFS_SEG_PRE_DIRTY_STATE_FLAG (1 << 6) +#define SSDFS_SEG_DIRTY_STATE_FLAG (1 << 7) +#define SSDFS_SEG_BAD_STATE_FLAG (1 << 8) +#define SSDFS_SEG_RESERVED_STATE_FLAG (1 << 9) +#define SSDFS_SEG_DATA_USING_INVALIDATED_STATE_FLAG (1 << 10) + +/* Segment state masks */ +#define SSDFS_SEG_CLEAN_USING_MASK \ + (SSDFS_SEG_CLEAN_STATE_FLAG | \ + SSDFS_SEG_DATA_USING_STATE_FLAG | \ + SSDFS_SEG_LEAF_NODE_USING_STATE_FLAG | \ + SSDFS_SEG_HYBRID_NODE_USING_STATE_FLAG | \ + SSDFS_SEG_INDEX_NODE_USING_STATE_FLAG | \ + SSDFS_SEG_DATA_USING_INVALIDATED_STATE_FLAG) +#define SSDFS_SEG_USED_DIRTY_MASK \ + (SSDFS_SEG_USED_STATE_FLAG | \ + SSDFS_SEG_PRE_DIRTY_STATE_FLAG | \ + SSDFS_SEG_DIRTY_STATE_FLAG) +#define SSDFS_SEG_BAD_STATE_MASK \ + (SSDFS_SEG_BAD_STATE_FLAG) + +#define SSDFS_SEG_STATE_BITS 4 +#define SSDFS_SEG_STATE_MASK 0xF + +struct ssdfs_segment_bmap ; + +/* +* struct ssdfs_segbmap_fragment_desc - fragment descriptor +* @state: fragment's state +* @fragment_id: fragment's ID in the whole sequence +* @total_segs: total count of segments in fragment +* @clean_or_using_segs: count of clean or using segments in fragment +* @used_or_dirty_segs: count of used, pre-dirty, dirty or reserved segments +* @bad_segs: count of bad segments in fragment +* @init_end: wait of init ending +* @flush_pairs: array of flush requests +* @segbmap: pointer on segment bitmap object +* @frag_kobj: fragment kobject for sysfs +* @frag_kobj_unregister: completion for fragment kobject cleanup +*/ +struct ssdfs_segbmap_fragment_desc { +int state; +u16 fragment_id; +u16 total_segs; +u16 clean_or_using_segs; +u16 used_or_dirty_segs; +u16 bad_segs; +struct completion init_end; + +#define SSDFS_SEGBMAP_FLUSH_REQS_MAX (2) +struct { +struct ssdfs_segment_info *si; +struct ssdfs_segment_request req; + } flush_pairs[SSDFS_SEGBMAP_FLUSH_REQS_MAX ]; + +struct ssdfs_segment_bmap *segbmap; + +/* /sys/fs/<ssdfs>/<device>/segbmap/fragments/fragment<N> */ +struct kobject frag_kobj; +struct completion frag_kobj_unregister; +}; + +/* Fragment's state */ +enum { +SSDFS_SEGBMAP_FRAG_CREATED =0 , +SSDFS_SEGBMAP_FRAG_INIT_FAILED =1 , +SSDFS_SEGBMAP_FRAG_INITIALIZED =2 , +SSDFS_SEGBMAP_FRAG_DIRTY =3 , +SSDFS_SEGBMAP_FRAG_TOWRITE =4 , +SSDFS_SEGBMAP_FRAG_STATE_MAX =5 , +}; + +/* Fragments bitmap types */ +enum { +SSDFS_SEGBMAP_CLEAN_USING_FBMAP , +SSDFS_SEGBMAP_USED_DIRTY_FBMAP , +SSDFS_SEGBMAP_BAD_FBMAP , +SSDFS_SEGBMAP_MODIFICATION_FBMAP , +SSDFS_SEGBMAP_FBMAP_TYPE_MAX , +}; + +/* +* struct ssdfs_segment_bmap - segments bitmap +* @resize_lock: lock for possible resize operation +* @flags: bitmap flags +* @bytes_count: count of bytes in the whole segment bitmap +* @items_count: count of volume's segments +* @fragments_count: count of fragments in the whole segment bitmap +* @fragments_per_seg: segbmap's fragments per segment +* @fragments_per_peb: segbmap's fragments per PEB +* @fragment_size: size of fragment in bytes +* @seg_numbers: array of segment bitmap's segment numbers +* @segs_count: count of segment objects are used for segment bitmap +* @segs: array of pointers on segment objects +* @search_lock: lock for search and change state operations +* @fbmap: array of fragment bitmaps +* @desc_array: array of fragments' descriptors +* @folios: memory folios of the whole segment bitmap +* @fsi: pointer on shared file system object +*/ +struct ssdfs_segment_bmap { +struct rw_semaphore resize_lock; +u16 flags; +u32 bytes_count; +u64 items_count; +u16 fragments_count; +u16 fragments_per_seg; +u16 fragments_per_peb; +u16 fragment_size; +#define SEGS_LIMIT1 SSDFS_SEGBMAP_SEGS +#define SEGS_LIMIT2 SSDFS_SEGBMAP_SEG_COPY_MAX +u64 seg_numbers[SEGS_LIMIT1 ][SEGS_LIMIT2 ]; +u16 segs_count; +struct ssdfs_segment_info *segs[SEGS_LIMIT1 ][SEGS_LIMIT2 ]; + +struct rw_semaphore search_lock; +unsigned long *fbmap[SSDFS_SEGBMAP_FBMAP_TYPE_MAX ]; +struct ssdfs_segbmap_fragment_desc *desc_array; +struct ssdfs_folio_array folios; + +struct ssdfs_fs_info *fsi; +}; + +/* +* Inline functions +*/ +static inline +u32 SEG_BMAP_BYTES (u64 items_count) +{ +u64 bytes; + + bytes = items_count +SSDFS_ITEMS_PER_BYTE (SSDFS_SEG_STATE_BITS ) -1 ; + bytes /=SSDFS_ITEMS_PER_BYTE (SSDFS_SEG_STATE_BITS ); + +BUG_ON (bytes >=U32_MAX ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_count %llu, bytes %llu\n" , + items_count, bytes); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (u32 )bytes; +} + +static inline +u16 SEG_BMAP_FRAGMENTS (u64 items_count) +{ +u32 hdr_size =sizeof (struct ssdfs_segbmap_fragment_header ); +u32 bytes =SEG_BMAP_BYTES (items_count); +u32 pages, fragments; + + pages = (bytes +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + bytes += pages * hdr_size; + + fragments = (bytes +PAGE_SIZE -1 ) >>PAGE_SHIFT ; +BUG_ON (fragments >=U16_MAX ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_count %llu, pages %u, " +"bytes %u, fragments %u\n" , + items_count, pages, + bytes, fragments); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (u16 )fragments; +} + +static inline +u16 ssdfs_segbmap_seg_2_fragment_index (u64 seg) +{ +u16 fragments_count =SEG_BMAP_FRAGMENTS (seg +1 ); + +BUG_ON (fragments_count ==0 ); +return fragments_count -1 ; +} + +static inline +u32 ssdfs_segbmap_items_per_fragment (size_t fragment_size) +{ +u32 hdr_size =sizeof (struct ssdfs_segbmap_fragment_header ); +u32 payload_bytes; +u64 items; + +BUG_ON (hdr_size >= fragment_size); + + payload_bytes = fragment_size - hdr_size; + items = payload_bytes *SSDFS_ITEMS_PER_BYTE (SSDFS_SEG_STATE_BITS ); + +BUG_ON (items >=U32_MAX ); + +return (u32 )items; +} + +static inline +u64 ssdfs_segbmap_define_first_fragment_item (pgoff_t fragment_index, +size_t fragment_size) +{ +return fragment_index *ssdfs_segbmap_items_per_fragment (fragment_size); +} + +static inline +u32 ssdfs_segbmap_get_item_byte_offset (u32 fragment_item) +{ +u32 hdr_size =sizeof (struct ssdfs_segbmap_fragment_header ); +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_SEG_STATE_BITS ); +return hdr_size + (fragment_item / items_per_byte); +} + +static inline +int ssdfs_segbmap_seg_id_2_seg_index (struct ssdfs_segment_bmap *segbmap, +u64 seg_id) +{ +int i; + +if (seg_id ==U64_MAX ) +return -ENODATA ; + +for (i =0 ; i < segbmap->segs_count; i++) { +if (seg_id == segbmap->seg_numbers[i][SSDFS_MAIN_SEGBMAP_SEG ]) +return i; +if (seg_id == segbmap->seg_numbers[i][SSDFS_COPY_SEGBMAP_SEG ]) +return i; + } + +return -ENODATA ; +} + +static inline +bool ssdfs_segbmap_fragment_has_content (struct folio *folio) +{ +bool has_content = false; +void *kaddr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!folio); + +SSDFS_DBG ("folio %p\n" , folio); +#endif /* CONFIG_SSDFS_DEBUG */ + + kaddr =kmap_local_folio (folio,0 ); +if (memchr_inv (kaddr,0xff ,PAGE_SIZE ) !=NULL ) + has_content = true; +kunmap_local (kaddr); + +return has_content; +} + +static inline +bool IS_STATE_GOOD_FOR_MASK (int mask,int state) +{ +bool is_good = false; + +switch (state) { +case SSDFS_SEG_CLEAN : + is_good = mask &SSDFS_SEG_CLEAN_STATE_FLAG ; +break ; + +case SSDFS_SEG_DATA_USING : + is_good = mask &SSDFS_SEG_DATA_USING_STATE_FLAG ; +break ; + +case SSDFS_SEG_DATA_USING_INVALIDATED : + is_good = mask &SSDFS_SEG_DATA_USING_INVALIDATED_STATE_FLAG ; +break ; + +case SSDFS_SEG_LEAF_NODE_USING : + is_good = mask &SSDFS_SEG_LEAF_NODE_USING_STATE_FLAG ; +break ; + +case SSDFS_SEG_HYBRID_NODE_USING : + is_good = mask &SSDFS_SEG_HYBRID_NODE_USING_STATE_FLAG ; +break ; + +case SSDFS_SEG_INDEX_NODE_USING : + is_good = mask &SSDFS_SEG_INDEX_NODE_USING_STATE_FLAG ; +break ; + +case SSDFS_SEG_USED : + is_good = mask &SSDFS_SEG_USED_STATE_FLAG ; +break ; + +case SSDFS_SEG_PRE_DIRTY : + is_good = mask &SSDFS_SEG_PRE_DIRTY_STATE_FLAG ; +break ; + +case SSDFS_SEG_DIRTY : + is_good = mask &SSDFS_SEG_DIRTY_STATE_FLAG ; +break ; + +case SSDFS_SEG_BAD : + is_good = mask &SSDFS_SEG_BAD_STATE_FLAG ; +break ; + +case SSDFS_SEG_RESERVED : + is_good = mask &SSDFS_SEG_RESERVED_STATE_FLAG ; +break ; + +default : +BUG (); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("mask %#x, state %#x, is_good %#x\n" , + mask, state, is_good); +#endif /* CONFIG_SSDFS_DEBUG */ + +return is_good; +} + +static inline +void ssdfs_debug_segbmap_object (struct ssdfs_segment_bmap *bmap) +{ +#ifdef CONFIG_SSDFS_DEBUG +int i, j; +size_t bytes; + +BUG_ON (!bmap); + +SSDFS_DBG ("flags %#x, bytes_count %u, items_count %llu, " +"fragments_count %u, fragments_per_seg %u, " +"fragments_per_peb %u, fragment_size %u\n" , + bmap->flags, bmap->bytes_count, bmap->items_count, + bmap->fragments_count, bmap->fragments_per_seg, + bmap->fragments_per_peb, bmap->fragment_size); + +for (i =0 ; i <SSDFS_SEGBMAP_SEGS ; i++) { +for (j =0 ; j <SSDFS_SEGBMAP_SEG_COPY_MAX ; j++) { +SSDFS_DBG ("seg_numbers[%d][%d] = %llu\n" , + i, j, bmap->seg_numbers[i][j]); + } + } + +SSDFS_DBG ("segs_count %u\n" , bmap->segs_count); + +for (i =0 ; i <SSDFS_SEGBMAP_SEGS ; i++) { +for (j =0 ; j <SSDFS_SEGBMAP_SEG_COPY_MAX ; j++) { +SSDFS_DBG ("segs[%d][%d] = %p\n" , + i, j, bmap->segs[i][j]); + } + } + + bytes = bmap->fragments_count +BITS_PER_LONG -1 ; + bytes /=BITS_PER_BYTE ; + +for (i =0 ; i <SSDFS_SEGBMAP_FBMAP_TYPE_MAX ; i++) { +SSDFS_DBG ("fbmap[%d]\n" , i); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + bmap->fbmap[i], bytes); + } + +for (i =0 ; i < bmap->fragments_count; i++) { +struct ssdfs_segbmap_fragment_desc *desc; + + desc = &bmap->desc_array[i]; + +SSDFS_DBG ("state %#x, total_segs %u, " +"clean_or_using_segs %u, used_or_dirty_segs %u, " +"bad_segs %u\n" , + desc->state, desc->total_segs, + desc->clean_or_using_segs, + desc->used_or_dirty_segs, + desc->bad_segs); + } + +for (i =0 ; i < bmap->fragments_count; i++) { +struct folio *folio; +void *kaddr; + + folio =ssdfs_folio_array_get_folio_locked (&bmap->folios, i); + +SSDFS_DBG ("folio[%d] %p\n" , i, folio); +if (!folio) +continue ; + +SSDFS_DBG ("folio_index %llu, flags %#lx\n" , + (u64 )folio->index, folio->flags.f); + + kaddr =kmap_local_folio (folio,0 ); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + kaddr,PAGE_SIZE ); +kunmap_local (kaddr); + +ssdfs_folio_unlock (folio); +ssdfs_folio_put (folio); + +SSDFS_DBG ("folio %p, count %d\n" , + folio,folio_ref_count (folio)); + } +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* Segment bitmap's API +*/ +int ssdfs_segbmap_create (struct ssdfs_fs_info *fsi); +void ssdfs_segbmap_destroy (struct ssdfs_fs_info *fsi); +int ssdfs_segbmap_check_fragment_header (struct ssdfs_peb_container *pebc, +u16 seg_index, +u16 sequence_id, +struct folio *folio); +int ssdfs_segbmap_fragment_init (struct ssdfs_peb_container *pebc, +u16 sequence_id, +struct folio *folio, +int state); +int ssdfs_segbmap_flush (struct ssdfs_segment_bmap *segbmap); +int ssdfs_segbmap_resize (struct ssdfs_segment_bmap *segbmap, +u64 new_items_count); + +int ssdfs_segbmap_check_state (struct ssdfs_segment_bmap *segbmap, +u64 seg,int state, +struct completion **end); +int ssdfs_segbmap_get_state (struct ssdfs_segment_bmap *segbmap, +u64 seg,struct completion **end); +int ssdfs_segbmap_change_state (struct ssdfs_segment_bmap *segbmap, +u64 seg,int new_state, +struct completion **end); +int ssdfs_segbmap_find (struct ssdfs_segment_bmap *segbmap, +u64 start,u64 max, +int state,int mask, +u64 *seg,struct completion **end); +int ssdfs_segbmap_find_and_set (struct ssdfs_segment_bmap *segbmap, +u64 start,u64 max, +int state,int mask, +int new_state, +u64 *seg,struct completion **end); +int ssdfs_segbmap_reserve_clean_segment (struct ssdfs_segment_bmap *segbmap, +u64 start,u64 max, +u64 *seg,struct completion **end); + +#endif /* _SSDFS_SEGMENT_BITMAP_H */
Signature · 1 line
2.34.1
PATCH v2 44/79 ssdfs: introduce b-tree object
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS file system is using the logical segment, logical extent concepts, and the "Physical" Erase Blocks (PEB) migration scheme. Generally speaking, these techniques provide the opportunity to exclude completely the wandering tree issue and to decrease significantly the write amplification. SSDFS file system introduces the technique of storing the data on the basis of logical extent that describes this data’s position by means of segment ID and logical block ID. Finally, PEBs migration technique guarantee that data will be described by the same logical extent until the direct change of segment ID or logical block ID. As a result, it means that logical extent will be the same if data is sitting in the same logical segment. The responsibility of PEBs migration technique is to implement the continuous migration of data between PEBs inside of the logical segment for the case of data updates. Generally speaking, SSDFS file system’s internal techniques guarantee that COW policy will not update the content of b-tree. But content of b-tree will be updated only by regular operations of end-user with the file system.
SSDFS file system uses b-tree architecture for metadata representation (for example, inodes tree, extents tree, dentries tree, xattr tree) because it provides the compact way of reserving the metadata space without the necessity to use the excessive overprovisioning of metadata reservation (for example, in the case of plain table or array).
The b-tree provides the efficient technique of items lookup, especially, for the case of aged or sparse b-tree that is capable to contain the mixture of used and deleted (or freed) items. Such b-tree’s feature could be very useful for the case of extent invalidation, for example. Also SSDFS file system aggregates the b-tree’s root node in the superblock (for example, inodes tree case) or in the inode (for example, extents tree case). As a result, it means that an empty b-tree will contain only the root node without the necessity to reserve any b-tree’s node on the file system’s volume. Moreover, if a b-tree needs to contain only several items (two items, for example) then the root node’s space can be used to store these items inline without the necessity to create the full-featured b-tree’s node. As a result, SSDFS uses b-trees with the goal to achieve the compact representation of metadata, the flexible way to expend or to shrink the b-tree’s space capacity, and the efficient mechanism of items’ lookup.
SSDFS file system uses a hybrid b-tree architecture with the goal to eliminate the index nodes’ side effect. The hybrid b-tree operates by three node types: (1) index node, (2) hybrid node, (3) leaf node. Generally speaking, the peculiarity of hybrid node is the mixture as index as data records into one node. Hybrid b-tree starts with root node that is capable to keep the two index records or two data records inline (if size of data record is equal or lesser than size of index record). If the b-tree needs to contain more than two items then it should be added the first hybrid node into the b-tree. The root level of b-tree is able to contain only two nodes because the root node is capable to store only two index records. Generally speaking, the initial goal of hybrid node is to store the data records in the presence of reserved index area.
Generalized b-tree functionality implements API:
(1) create - create empty b-tree object
(2) destroy - destroy b-tree object
(3) flush - flush dirty b-tree object
(4) find_item - find item in b-tree hierarchy
(5) find_range - find range of items in b-tree hierarchy
(6) allocate_item - allocate item in existing b-tree's node
(7) allocate_range - allocate range of items in existing b-tree's node
(8) add_item - add item into b-tree
(9) add_range - add range of items into b-tree
(10) change_item - change existing item in b-tree
(11) delete_item - delete item from b-tree
(12) delete_range - delete range of items from b-tree
(13) delete_all - delete all items from b-tree
--- fs/ssdfs/btree.h | 219 +++++++++++++++++++++ fs/ssdfs/btree_search.h | 424 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 643 insertions(+) create mode 100644 fs/ssdfs/btree.h create mode 100644 fs/ssdfs/btree_search.h
fs/ssdfs/btree.h new file +219 -0
@@ -0,0 +1,219 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/btree.h - btree declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_BTREE_H +#define _SSDFS_BTREE_H + +struct ssdfs_btree ; + +/* +* struct ssdfs_btree_descriptor_operations - btree descriptor operations +* @init: initialize btree object by descriptor +* @flush: save btree descriptor into superblock +*/ +struct ssdfs_btree_descriptor_operations { +int (*init)(struct ssdfs_fs_info *fsi, +struct ssdfs_btree *tree); +int (*flush)(struct ssdfs_btree *tree); +}; + +/* +* struct ssdfs_btree_operations - btree operations specialization +* @create_root_node: specialization of root node creation +* @create_node: specialization of node's construction operation +* @init_node: specialization of node's init operation +* @destroy_node: specialization of node's destroy operation +* @add_node: specialization of adding into the tree a new empty node +* @delete_node: specialization of deletion a node from the tree +* @pre_flush_root_node: specialized flush preparation of root node +* @flush_root_node: specialized method of root node flushing +* @pre_flush_node: specialized flush preparation of common node +* @flush_node: specialized method of common node flushing +*/ +struct ssdfs_btree_operations { +int (*create_root_node)(struct ssdfs_fs_info *fsi, +struct ssdfs_btree_node *node); +int (*create_node)(struct ssdfs_btree_node *node); +int (*init_node)(struct ssdfs_btree_node *node); +void (*destroy_node)(struct ssdfs_btree_node *node); +int (*add_node)(struct ssdfs_btree_node *node); +int (*delete_node)(struct ssdfs_btree_node *node); +int (*pre_flush_root_node)(struct ssdfs_btree_node *node); +int (*flush_root_node)(struct ssdfs_btree_node *node); +int (*pre_flush_node)(struct ssdfs_btree_node *node); +int (*flush_node)(struct ssdfs_btree_node *node); +}; + +/* +* struct ssdfs_btree - generic btree +* @type: btree type +* @owner_ino: inode identification number of btree owner +* @node_size: size of the node in bytes +* @pages_per_node: physical pages per node +* @node_ptr_size: size in bytes of pointer on btree node +* @index_size: size in bytes of btree's index +* @item_size: default size of item in bytes +* @min_item_size: min size of item in bytes +* @max_item_size: max possible size of item in bytes +* @index_area_min_size: minimal size in bytes of index area in btree node +* @create_cno: btree's create checkpoint +* @state: btree state +* @flags: btree flags +* @height: current height of the tree +* @lock: btree's lock +* @nodes_lock: radix tree lock +* @upper_node_id: last allocated node id +* @nodes: nodes' radix tree +* @fsi: pointer on shared file system object +* +* Btree nodes are organized by radix tree. +* Another good point about radix tree is +* supporting of knowledge about dirty items. +*/ +struct ssdfs_btree { +/* static data */ +u8 type; +u64 owner_ino; +u32 node_size; +u8 pages_per_node; +u8 node_ptr_size; +u16 index_size; +u16 item_size; +u8 min_item_size; +u16 max_item_size; +u16 index_area_min_size; +u64 create_cno; + +/* operation specializations */ +const struct ssdfs_btree_descriptor_operations *desc_ops; +const struct ssdfs_btree_operations *btree_ops; + +/* mutable data */ +atomic_t state; +atomic_t flags; +atomic_t height; + +struct rw_semaphore lock; + +spinlock_t nodes_lock; +u32 upper_node_id; +struct radix_tree_root nodes; + +struct ssdfs_fs_info *fsi; +}; + +/* Btree object states */ +enum { +SSDFS_BTREE_UNKNOWN_STATE , +SSDFS_BTREE_CREATED , +SSDFS_BTREE_DIRTY , +SSDFS_BTREE_STATE_MAX +}; + +/* Radix tree tags */ +#define SSDFS_BTREE_NODE_DIRTY_TAG PAGECACHE_TAG_DIRTY +#define SSDFS_BTREE_NODE_TOWRITE_TAG PAGECACHE_TAG_TOWRITE + +/* +* Btree API +*/ +int ssdfs_btree_create (struct ssdfs_fs_info *fsi, +u64 owner_ino, +const struct ssdfs_btree_descriptor_operations *desc_ops, +const struct ssdfs_btree_operations *btree_ops, +struct ssdfs_btree *tree); +void ssdfs_btree_destroy (struct ssdfs_btree *tree); +int ssdfs_btree_flush (struct ssdfs_btree *tree); + +int ssdfs_btree_find_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_find_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +bool is_ssdfs_btree_empty (struct ssdfs_btree *tree); +int ssdfs_btree_allocate_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_allocate_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_add_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_add_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_change_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_delete_item (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_delete_range (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_delete_all (struct ssdfs_btree *tree); + +/* +* Internal Btree API +*/ +bool need_migrate_generic2inline_btree (struct ssdfs_btree *tree, +int items_threshold); +int ssdfs_btree_desc_init (struct ssdfs_fs_info *fsi, +struct ssdfs_btree *tree, +struct ssdfs_btree_descriptor *desc, +u8 min_item_size, +u16 max_item_size); +int ssdfs_btree_desc_flush (struct ssdfs_btree *tree, +struct ssdfs_btree_descriptor *desc); +struct ssdfs_btree_node * +ssdfs_btree_get_child_node_for_hash (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent, +u64 hash); +int ssdfs_btree_update_parent_node_pointer (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent); +int ssdfs_btree_add_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_insert_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_delete_node (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search); +int ssdfs_btree_get_head_range (struct ssdfs_btree *tree, +u32 expected_len, +struct ssdfs_btree_search *search); +int ssdfs_btree_extract_range (struct ssdfs_btree *tree, +u16 start_index,u16 count, +struct ssdfs_btree_search *search); +int ssdfs_btree_destroy_node_range (struct ssdfs_btree *tree, +u64 start_hash); +struct ssdfs_btree_node * +__ssdfs_btree_read_node (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent, +struct ssdfs_btree_index_key *node_index, +u8 node_type,u32 node_id); +int ssdfs_btree_radix_tree_find (struct ssdfs_btree *tree, +unsigned long node_id, +struct ssdfs_btree_node **node); +int ssdfs_btree_synchronize_root_node (struct ssdfs_btree *tree, +struct ssdfs_btree_inline_root_node *root); +int ssdfs_btree_get_next_hash (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +u64 *next_hash); + +void ssdfs_debug_show_btree_node_indexes (struct ssdfs_btree *tree, +struct ssdfs_btree_node *parent); +void ssdfs_check_btree_consistency (struct ssdfs_btree *tree); +void ssdfs_debug_btree_object (struct ssdfs_btree *tree); + +#endif /* _SSDFS_BTREE_H */
fs/ssdfs/btree_search.h new file +424 -0
424 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 46/79 ssdfs: introduce b-tree node object
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS file system uses a hybrid b-tree architecture with the goal to eliminate the index nodes’ side effect. The hybrid b-tree operates by three node types: (1) index node, (2) hybrid node, (3) leaf node. Generally speaking, the peculiarity of hybrid node is the mixture as index as data records into one node. Hybrid b-tree starts with root node that is capable to keep the two index records or two data records inline (if size of data record is equal or lesser than size of index record). If the b-tree needs to contain more than two items then it should be added the first hybrid node into the b-tree. The root level of b-tree is able to contain only two nodes because the root node is capable to store only two index records. Generally speaking, the initial goal of hybrid node is to store the data records in the presence of reserved index area.
Dirty b-tree implies the presence of one or several dirty b-tree node. B-tree flush logic detects the dirty b-tree nodes and request the flush operation for every dirty b-tree node. B-tree node can include several memory pages (8K, for example). It means that one b-tree node can be located in one or several logical blocks. Finally, flush operation means that b-tree node's flush logic has to issue update request(s) for all logical blocks that contain the b-tree node content. Every b-tree node is described by index record (or key) that
includes: (1) node ID, (2) node type, (3) node height,
(4) starting hash value, (5) raw extent. The raw extent describes the segment ID, logical block ID, and length. As a result, flush logic needs to add update request into an update queue of particular PEB for segment ID. Also, flush logic has to request the log commit operation because b-tree node has to be stored peristently right now. Flush thread(s) of particular PEB(s) executes the update requests. Finally, b-tree flush logic requires to wait the finish of update operations for all dirty b-tree node(s).
Every b-tree node is described by index record (or key) that
includes: (1) node ID, (2) node type, (3) node height,
(4) starting hash value, (5) raw extent. The raw extent describes the segment ID, logical block ID, and length. Index records are stored into index and hybrid b-tree nodes. These records implement mechanism of lookup and traverse operations in b-tree.
Index operations include:
(1) resize_index_area - operation of increasing size of index area in hybrid b-tree node by means of redistribution of free space between index and item area and shift of item area
(2) find_index - find index in hybrid or index b-tree node
(3) add_index - add index in hybrid of index b-tree node
(4) change_index - change index record in hybrid of index b-tree node
(5) delete_index - delete index record from hybrid of index b-tree node
B-tree node implements search, allocation, and insert item or range of items in the node:
(1) find_item - find item in b-tree node
(2) find_range - find range of items in b-tree node
(3) allocate_item - allocate item in b-tree node
(4) allocate_range - allocate range of items in b-tree node
(5) insert_item - insert/add item into b-tree node
(6) insert_range - insert/add range of items into b-tree node
B-tree node implements change and delete item or range of items in the node:
(1) change_item - change item in the b-tre node
(2) delete_item - delete item from b-tree node
(3) delete_range - delete range of items from b-tree node
Hybrid b-tree node plays very special role in b-tree architecture. First of all, hybrid node includes index and item areas. The goal is to combine in one node index and items records for the case of small b-trees. B-tree starts by creation of root node that can contain only two index keys. It means that root node keeps knowledge about two nodes only. At first, b-tree logic creates leaf nodes until the root node will contain two index keys for leaf nodes. Next step implies the creation of hybrid node that contains index records for two existing leaf nodes. The root node contains index key for hybrid node. Now hybrid node becomes to play. New items will be added into items area of hybrid node until this area becomes completely full. B-tree logic allocates new leaf node, all existing items in hybrid node are moved into newly created leaf node, and index key is added into hybrid node's index area. Such operation repeat multiple times until index area of hybrid node becomes completely full. Now index area is resized by increasing twice in size after moving existing items into newly created node. Finally, hybrid node will be converted into index node. Important point that small b-tree has one hybrid node with index keys and items instead of two nodes (index + leaf). Hybrid node combines as index as items operations that makes this type of node by "hot" type of metadata and it provides the way to isolate/distinguish hot, warm, and cold data. As a result, it provides the way to make b-tree more compact by decreasing number of nodes, makes GC operations not neccessary because update operations of "hot" hybrid node(s) makes migration scheme efficient, and decrease write amplification.
Hybrid nodes require range operations that are represented by:
(1) extract_range - extract range of items (or all items) from node
(2) insert_range - insert range of items into node
(3) delete_range - remove range of items from node
--- fs/ssdfs/btree_node.h | 891 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 891 insertions(+) create mode 100644 fs/ssdfs/btree_node.h
fs/ssdfs/btree_node.h new file +891 -0
@@ -0,0 +1,891 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/btree_node.h - btree node declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_BTREE_NODE_H +#define _SSDFS_BTREE_NODE_H + +#include "request_queue.h" +#include "folio_array.h" +#include "peb.h" + +/* +* struct ssdfs_btree_node_operations - node operations specialization +* @find_item: specialized item searching algorithm +* @find_range: specialized range searching algorithm +* @extract_range: specialized extract range operation +* @allocate_item: specialized item allocation operation +* @allocate_range: specialized range allocation operation +* @insert_item: specialized insert item operation +* @insert_range: specialized insert range operation +* @change_item: specialized change item operation +* @delete_item: specialized delete item operation +* @delete_range: specialized delete range operation +* @move_items_range: specialized move items operation +* @resize_items_area: specialized resize items area operation +*/ +struct ssdfs_btree_node_operations { +int (*find_item)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*find_range)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*extract_range)(struct ssdfs_btree_node *node, +u16 start_index,u16 count, +struct ssdfs_btree_search *search); +int (*allocate_item)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*allocate_range)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*insert_item)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*insert_range)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*change_item)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*delete_item)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*delete_range)(struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search); +int (*move_items_range)(struct ssdfs_btree_node *src, +struct ssdfs_btree_node *dst, +u16 start_item,u16 count); +int (*resize_items_area)(struct ssdfs_btree_node *node, +u32 new_size); +}; + +/* Btree node area's states */ +enum { +SSDFS_BTREE_NODE_AREA_UNKNOWN_STATE , +SSDFS_BTREE_NODE_AREA_ABSENT , +SSDFS_BTREE_NODE_INDEX_AREA_EXIST , +SSDFS_BTREE_NODE_ITEMS_AREA_EXIST , +SSDFS_BTREE_NODE_LOOKUP_TBL_EXIST , +SSDFS_BTREE_NODE_HASH_TBL_EXIST , +SSDFS_BTREE_NODE_AREA_STATE_MAX +}; + +/* +* struct ssdfs_btree_node_index_area - btree node's index area +* @state: area state +* @flags: index area's flags +* @offset: area offset from node's beginning +* @area_size: area size in bytes +* @index_size: index size in bytes +* @index_count: count of indexes in area +* @index_capacity: index area capacity +* @start_hash: starting hash in index area +* @end_hash: ending hash in index area +*/ +struct ssdfs_btree_node_index_area { +atomic_t state; + +#define SSDFS_PLEASE_ADD_HYBRID_NODE_SELF_INDEX (1 << 0) +#define SSDFS_BTREE_NODE_INDEX_AREA_FLAGS_MASK 0x1 +atomic_t flags; + +u32 offset; +u32 area_size; + +u8 index_size; +u16 index_count; +u16 index_capacity; + +u64 start_hash; +u64 end_hash; +}; + +/* +* struct ssdfs_btree_node_items_area - btree node's data area +* @state: area state +* @flags: items area's flags +* @offset: area offset from node's beginning +* @area_size: area size in bytes +* @free_space: free space in bytes +* @item_size: item size in bytes +* @min_item_size: minimal possible item size in bytes +* @max_item_size: maximal possible item size in bytes +* @items_count: count of allocated items in area +* @items_capacity: items area capacity +* @start_hash: starting hash in items area +* @end_hash: ending hash in items area +*/ +struct ssdfs_btree_node_items_area { +atomic_t state; + +#define SSDFS_PLEASE_ADD_FREE_ITEMS_RANGE (1 << 0) +#define SSDFS_BTREE_NODE_ITEMS_AREA_FLAGS_MASK 0x1 +atomic_t flags; + +u32 offset; +u32 area_size; +u32 free_space; + +u16 item_size; +u8 min_item_size; +u16 max_item_size; + +u16 items_count; +u16 items_capacity; + +u64 start_hash; +u64 end_hash; +}; + +struct ssdfs_btree ; + +/* +* struct ssdfs_state_bitmap - bitmap of states +* @lock: bitmap lock +* @flags: bitmap's flags +* @ptr: bitmap +*/ +struct ssdfs_state_bitmap { +spinlock_t lock; + +#define SSDFS_LOOKUP_TBL2_IS_USING (1 << 0) +#define SSDFS_HASH_TBL_IS_USING (1 << 1) +#define SSDFS_BMAP_ARRAY_FLAGS_MASK 0x3 +u32 flags; + +unsigned long *ptr; +}; + +/* +* struct ssdfs_state_bitmap_array - array of bitmaps +* @lock: bitmap array lock +* @bits_count: whole bits count in the bitmap +* @bmap_bytes: size in bytes of every bitmap +* @index_start_bit: starting bit of index area in the bitmap +* @item_start_bit: starting bit of items area in the bitmap +* @locks_count: count of sucessful locks +* @bmap: partial locks, alloc and dirty bitmaps +*/ +struct ssdfs_state_bitmap_array { +struct rw_semaphore lock; +unsigned long bits_count; +size_t bmap_bytes; +unsigned long index_start_bit; +unsigned long item_start_bit; +atomic_t locks_count; + +#define SSDFS_BTREE_NODE_LOCK_BMAP (0) +#define SSDFS_BTREE_NODE_ALLOC_BMAP (1) +#define SSDFS_BTREE_NODE_DIRTY_BMAP (2) +#define SSDFS_BTREE_NODE_BMAP_COUNT (3) +struct ssdfs_state_bitmap bmap[SSDFS_BTREE_NODE_BMAP_COUNT ]; +}; + +/* +* struct ssdfs_btree_node_content - btree node's content +* @protection: btree node's content protection window +* @blocks: array of logical blocks +* @count: number of blocks in extent +*/ +struct ssdfs_btree_node_content { +struct ssdfs_protection_window protection; +#define SSDFS_BTREE_NODE_EXTENT_LEN_MAX (SSDFS_EXTENT_LEN_MAX) +struct ssdfs_content_block blocks[SSDFS_BTREE_NODE_EXTENT_LEN_MAX ]; +int count; +}; + +union ssdfs_aggregated_btree_node_header { +struct ssdfs_inodes_btree_node_header inodes_header; +struct ssdfs_dentries_btree_node_header dentries_header; +struct ssdfs_extents_btree_node_header extents_header; +struct ssdfs_xattrs_btree_node_header xattrs_header; +}; + +/* +* struct ssdfs_btree_node - btree node +* @list: btree nodes' list +* @height: node's height +* @node_size: node size in bytes +* @pages_per_node: count of logical blocks per node +* @create_cno: create checkpoint +* @node_id: node identification number +* @tree: pointer on node's parent tree +* @node_ops: btree's node operation specialization +* @refs_count: reference counter +* @state: node state +* @flags: node's flags +* @type: node type +* @header_lock: header lock +* @raw.root_node: root node copy +* @raw.generic_header: generic node's header +* @raw.inodes_header: inodes node's header +* @raw.dentries_header: dentries node's header +* @raw.extents_header: extents node's header +* @raw.dict_header: shared dictionary node's header +* @raw.xattrs_header: xattrs node's header +* @raw.shextree_header: shared extents tree's header +* @raw.snapshots_header: snapshots node's header +* @raw.invextree_header: invalidated extents tree's header +* @index_area: index area descriptor +* @items_area: items area descriptor +* @lookup_tbl_area: lookup table's area descriptor +* @hash_tbl_area: hash table's area descriptor +* @descriptor_lock: node's descriptor lock +* @update_cno: last update checkpoint +* @parent_node: pointer on parent node +* @node_index: node's index (for using in search operations) +* @extent: node's location +* @seg: pointer on segment object +* @init_end: wait of init ending +* @flush_req: flush request +* @bmap_array: partial locks, alloc and dirty bitmaps +* @wait_queue: queue of threads are waiting partial lock +* @full_lock: the whole node lock +* @content: node's content +*/ +struct ssdfs_btree_node { +struct list_head list; + +/* static data */ +atomic_t height; +u32 node_size; +u8 pages_per_node; +u64 create_cno; +u32 node_id; + +struct ssdfs_btree *tree; + +/* btree's node operation specialization */ +const struct ssdfs_btree_node_operations *node_ops; + +/* +* Reference counter +* The goal of reference counter is to account how +* many btree search objects are referencing the +* node's object. If some thread deletes all records +* in a node then the node will be left undeleted +* from the tree in the case of @refs_count is greater +* than one. +*/ +atomic_t refs_count; + +/* mutable data */ +atomic_t state; +atomic_t flags; +atomic_t type; + +/* node's header */ +struct rw_semaphore header_lock; +union { +struct ssdfs_btree_inline_root_node root_node; +struct ssdfs_btree_node_header generic_header; +struct ssdfs_inodes_btree_node_header inodes_header; +struct ssdfs_dentries_btree_node_header dentries_header; +struct ssdfs_extents_btree_node_header extents_header; +struct ssdfs_shared_dictionary_node_header dict_header; +struct ssdfs_xattrs_btree_node_header xattrs_header; +struct ssdfs_shextree_node_header shextree_header; +struct ssdfs_snapshots_btree_node_header snapshots_header; +struct ssdfs_invextree_node_header invextree_header; + } raw; +struct ssdfs_btree_node_index_area index_area; +struct ssdfs_btree_node_items_area items_area; +struct ssdfs_btree_node_index_area lookup_tbl_area; +struct ssdfs_btree_node_index_area hash_tbl_area; + +/* node's descriptor */ +spinlock_t descriptor_lock; +u64 update_cno; +struct ssdfs_btree_node *parent_node; +struct ssdfs_btree_index_key node_index; +struct ssdfs_raw_extent extent; +struct ssdfs_segment_info *seg; +struct completion init_end; +struct ssdfs_segment_request flush_req; + +/* partial locks, alloc and dirty bitmaps */ +struct ssdfs_state_bitmap_array bmap_array; +wait_queue_head_t wait_queue; + +/* node raw content */ +struct rw_semaphore full_lock; +struct ssdfs_btree_node_content content; +}; + +/* Btree node states */ +enum { +SSDFS_BTREE_NODE_UNKNOWN_STATE , +SSDFS_BTREE_NODE_CREATED , +SSDFS_BTREE_NODE_NONE_CONTENT , +SSDFS_BTREE_NODE_CONTENT_PREPARED , +SSDFS_BTREE_NODE_CONTENT_UNDER_FREE , +SSDFS_BTREE_NODE_INITIALIZED , +SSDFS_BTREE_NODE_DIRTY , +SSDFS_BTREE_NODE_PRE_DELETED , +SSDFS_BTREE_NODE_INVALID , +SSDFS_BTREE_NODE_CORRUPTED , +SSDFS_BTREE_NODE_STATE_MAX +}; + +/* +* It is possible to use knowledge about partial +* updates and to send only changed pieces of +* data for the case of Diff-On-Write approach. +* Metadata is good case for determination of +* partial updates and to send changed part(s) +* only. For example, bitmap could show dirty +* items in the node. +*/ + +/* +* Inline functions +*/ + +/* +* ssdfs_btree_node_content_init() - init btree node's content +* @content: btree node's content +*/ +static inline +void ssdfs_btree_node_content_init (struct ssdfs_btree_node_content *content) +{ +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!content); +#endif /* CONFIG_SSDFS_DEBUG */ + + content->count =0 ; + +for (i =0 ; i <SSDFS_BTREE_NODE_EXTENT_LEN_MAX ; i++) +folio_batch_init (&content->blocks[i].batch); +} + +/* +* NODE2SEG_TYPE() - convert node type into segment type +* @node_type: node type +*/ +static inline +u8 NODE2SEG_TYPE (u8 node_type) +{ +switch (node_type) { +case SSDFS_BTREE_INDEX_NODE : +return SSDFS_INDEX_NODE_SEG_TYPE ; + +case SSDFS_BTREE_HYBRID_NODE : +return SSDFS_HYBRID_NODE_SEG_TYPE ; + +case SSDFS_BTREE_LEAF_NODE : +return SSDFS_LEAF_NODE_SEG_TYPE ; + } + +SSDFS_WARN ("invalid node type %#x\n" , node_type); + +return SSDFS_UNKNOWN_SEG_TYPE ; +} + +/* +* RANGE_WITHOUT_INTERSECTION() - check that ranges have intersection +* @start1: starting hash of the first range +* @end1: ending hash of the first range +* @start2: starting hash of the second range +* @end2: ending hash of the second range +* +* This method checks that ranges have intersection. +* +* RETURN: +* 0 - ranges have intersection +* 1 - range1 > range2 +* -1 - range1 < range2 +*/ +static inline +int RANGE_WITHOUT_INTERSECTION (u64 start1,u64 end1,u64 start2,u64 end2) +{ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start1 %llx, end1 %llx, start2 %llx, end2 %llx\n" , + start1, end1, start2, end2); + +BUG_ON (start1 >=U64_MAX || end1 >=U64_MAX || + start2 >=U64_MAX || end2 >=U64_MAX ); +BUG_ON (start1 > end1); +BUG_ON (start2 > end2); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (start1 > end2) +return 1 ; + +if (end1 < start2) +return -1 ; + +return 0 ; +} + +/* +* RANGE_HAS_PARTIAL_INTERSECTION() - check that ranges intersect partially +* @start1: starting hash of the first range +* @end1: ending hash of the first range +* @start2: starting hash of the second range +* @end2: ending hash of the second range +*/ +static inline +bool RANGE_HAS_PARTIAL_INTERSECTION (u64 start1,u64 end1, +u64 start2,u64 end2) +{ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start1 %llx, end1 %llx, start2 %llx, end2 %llx\n" , + start1, end1, start2, end2); + +BUG_ON (start1 >=U64_MAX || end1 >=U64_MAX || + start2 >=U64_MAX || end2 >=U64_MAX ); +BUG_ON (start1 > end1); +BUG_ON (start2 > end2); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (start1 > end2) +return false; + +if (end1 < start2) +return false; + +return true; +} + +/* +* __ssdfs_items_per_lookup_index() - calculate items per lookup index +* @items_per_node: number of items per node +* @lookup_table_capacity: maximal number of items in lookup table +*/ +static inline +u16 __ssdfs_items_per_lookup_index (u32 items_per_node, +int lookup_table_capacity) +{ +u32 items_per_lookup_index; + + items_per_lookup_index = items_per_node / lookup_table_capacity; + +if (items_per_node % lookup_table_capacity) + items_per_lookup_index++; + +SSDFS_DBG ("items_per_lookup_index %u\n" , items_per_lookup_index); + +return items_per_lookup_index; +} + +/* +* __ssdfs_convert_lookup2item_index() - convert lookup into item index +* @lookup_index: lookup index +* @node_size: size of the node in bytes +* @item_size: size of the item in bytes +* @lookup_table_capacity: maximal number of items in lookup table +*/ +static inline +u16 __ssdfs_convert_lookup2item_index (u16 lookup_index, +u32 node_size, +size_t item_size, +int lookup_table_capacity) +{ +u32 items_per_node; +u32 items_per_lookup_index; +u32 item_index; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("lookup_index %u, node_size %u, " +"item_size %zu, table_capacity %d\n" , + lookup_index, node_size, + item_size, lookup_table_capacity); + +BUG_ON (lookup_index >= lookup_table_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + + items_per_node = node_size / item_size; + items_per_lookup_index =__ssdfs_items_per_lookup_index (items_per_node, + lookup_table_capacity); + + item_index = (u32 )lookup_index * items_per_lookup_index; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("lookup_index %u, item_index %u\n" , + lookup_index, item_index); + +BUG_ON (item_index >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (u16 )item_index; +} + +/* +* __ssdfs_convert_item2lookup_index() - convert item into lookup index +* @item_index: item index +* @node_size: size of the node in bytes +* @item_size: size of the item in bytes +* @lookup_table_capacity: maximal number of items in lookup table +*/ +static inline +u16 __ssdfs_convert_item2lookup_index (u16 item_index, +u32 node_size, +size_t item_size, +int lookup_table_capacity) +{ +u32 items_per_node; +u32 items_per_lookup_index; +u16 lookup_index; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("item_index %u, node_size %u, " +"item_size %zu, table_capacity %d\n" , + item_index, node_size, + item_size, lookup_table_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + + items_per_node = node_size / item_size; + items_per_lookup_index =__ssdfs_items_per_lookup_index (items_per_node, + lookup_table_capacity); + lookup_index = item_index / items_per_lookup_index; + +SSDFS_DBG ("item_index %u, lookup_index %u, table_capacity %d\n" , + item_index, lookup_index, lookup_table_capacity); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (lookup_index >= lookup_table_capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + +return lookup_index; +} + +/* +* ssdfs_find_node_content_folio() - find node content's folio +* @content: node's content +* @folio: smart folio descriptor +*/ +static inline +int ssdfs_find_node_content_folio (struct ssdfs_btree_node_content *content, +struct ssdfs_smart_folio *folio) +{ +struct folio_batch *batch; +u32 offset =0 ; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!content || !folio); + +BUG_ON (!IS_SSDFS_OFF2FOLIO_VALID (&folio->desc)); +#endif /* CONFIG_SSDFS_DEBUG */ + + folio->ptr =NULL ; + +if (folio->desc.folio_index >= content->count) { +SSDFS_ERR ("folio_index %u > blocks_count %u\n" , + folio->desc.folio_index, + content->count); +return -ERANGE ; + } + + batch = &content->blocks[folio->desc.folio_index].batch; + +for (i =0 ; i <folio_batch_count (batch); i++) { +struct folio *cur_folio = batch->folios[i]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!cur_folio); +#endif /* CONFIG_SSDFS_DEBUG */ + + offset +=folio_size (cur_folio); + +if (offset >= folio->desc.page_offset) { + folio->ptr = cur_folio; +break ; + } + } + +if (!folio->ptr) { +SSDFS_ERR ("fail to find folio\n" ); +return -ERANGE ; + } + +return 0 ; +} + +/* +* Btree nodes list's API +*/ +void ssdfs_btree_nodes_list_init (struct ssdfs_btree_nodes_list *bnl); +bool is_ssdfs_btree_nodes_list_empty (struct ssdfs_btree_nodes_list *bnl); +void ssdfs_btree_nodes_list_add (struct ssdfs_btree_nodes_list *bnl, +struct ssdfs_btree_node *node); +void ssdfs_btree_nodes_list_delete (struct ssdfs_btree_nodes_list *bnl, +struct ssdfs_btree_node *node); + +void ssdfs_btree_node_start_request_cno (struct ssdfs_btree_node *node); +void ssdfs_btree_node_finish_request_cno (struct ssdfs_btree_node *node); +bool is_it_time_free_btree_node_content (struct ssdfs_btree_node *node); + +/* +* Btree node API +*/ +struct ssdfs_btree_node * +ssdfs_btree_node_create (struct ssdfs_btree *tree, +u32 node_id, +struct ssdfs_btree_node *parent, +u8 height,int type,u64 start_hash); +void ssdfs_btree_node_destroy (struct ssdfs_btree_node *node); +int ssdfs_btree_node_prepare_content (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *index); +int ssdfs_btree_init_node (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr, +size_t hdr_size); +int ssdfs_btree_pre_flush_root_node (struct ssdfs_btree_node *node); +void ssdfs_btree_flush_root_node (struct ssdfs_btree_node *node, +struct ssdfs_btree_inline_root_node *root_node); +int ssdfs_btree_node_pre_flush (struct ssdfs_btree_node *node); +int ssdfs_btree_node_flush (struct ssdfs_btree_node *node); + +void ssdfs_btree_node_get (struct ssdfs_btree_node *node); +void ssdfs_btree_node_put (struct ssdfs_btree_node *node); +bool is_ssdfs_node_shared (struct ssdfs_btree_node *node); + +bool is_ssdfs_btree_node_dirty (struct ssdfs_btree_node *node); +void set_ssdfs_btree_node_dirty (struct ssdfs_btree_node *node); +void clear_ssdfs_btree_node_dirty (struct ssdfs_btree_node *node); +bool is_ssdfs_btree_node_pre_deleted (struct ssdfs_btree_node *node); +void set_ssdfs_btree_node_pre_deleted (struct ssdfs_btree_node *node); +void clear_ssdfs_btree_node_pre_deleted (struct ssdfs_btree_node *node); + +bool is_ssdfs_btree_node_index_area_exist (struct ssdfs_btree_node *node); +bool is_ssdfs_btree_node_index_area_empty (struct ssdfs_btree_node *node); +int ssdfs_btree_node_resize_index_area (struct ssdfs_btree_node *node, +u32 new_size); +int ssdfs_btree_node_find_index (struct ssdfs_btree_search *search); +bool can_add_new_index (struct ssdfs_btree_node *node); +int ssdfs_btree_node_add_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *key); +int ssdfs_btree_node_change_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_index_key *old_key, +struct ssdfs_btree_index_key *new_key); +int ssdfs_btree_node_delete_index (struct ssdfs_btree_node *node, +u64 hash); + +bool is_ssdfs_btree_node_items_area_exist (struct ssdfs_btree_node *node); +bool is_ssdfs_btree_node_items_area_empty (struct ssdfs_btree_node *node); +int ssdfs_btree_node_find_item (struct ssdfs_btree_search *search); +int ssdfs_btree_node_find_range (struct ssdfs_btree_search *search); +int ssdfs_btree_node_allocate_item (struct ssdfs_btree_search *search); +int ssdfs_btree_node_allocate_range (struct ssdfs_btree_search *search); +int ssdfs_btree_node_insert_item (struct ssdfs_btree_search *search); +int ssdfs_btree_node_insert_range (struct ssdfs_btree_search *search); +int ssdfs_btree_node_change_item (struct ssdfs_btree_search *search); +int ssdfs_btree_node_delete_item (struct ssdfs_btree_search *search); +int ssdfs_btree_node_delete_range (struct ssdfs_btree_search *search); + +/* +* Internal Btree node API +*/ +void ssdfs_btree_node_free_content_space (struct ssdfs_btree_node *node); +int ssdfs_lock_items_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count); +void ssdfs_unlock_items_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count); +int ssdfs_lock_whole_index_area (struct ssdfs_btree_node *node); +void ssdfs_unlock_whole_index_area (struct ssdfs_btree_node *node); +int ssdfs_allocate_items_range (struct ssdfs_btree_node *node, +struct ssdfs_btree_search *search, +u16 items_capacity, +u16 start_index,u16 count); +bool is_ssdfs_node_items_range_allocated (struct ssdfs_btree_node *node, +u16 items_capacity, +u16 start_index,u16 count); +int ssdfs_free_items_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count); +int ssdfs_set_node_header_dirty (struct ssdfs_btree_node *node, +u16 items_capacity); +void ssdfs_clear_node_header_dirty_state (struct ssdfs_btree_node *node); +int ssdfs_set_dirty_items_range (struct ssdfs_btree_node *node, +u16 items_capacity, +u16 start_index,u16 count); +void ssdfs_clear_dirty_items_range_state (struct ssdfs_btree_node *node, +u16 start_index,u16 count); + +int ssdfs_btree_node_allocate_bmaps (void *addr[SSDFS_BTREE_NODE_BMAP_COUNT ], +size_t bmap_bytes); +void ssdfs_btree_node_init_bmaps (struct ssdfs_btree_node *node, +void *addr[SSDFS_BTREE_NODE_BMAP_COUNT ]); +int ssdfs_btree_node_allocate_content_space (struct ssdfs_btree_node *node, +u32 node_size); +int __ssdfs_btree_node_prepare_content (struct ssdfs_fs_info *fsi, +struct ssdfs_btree_index_key *ptr, +u32 node_size, +u64 owner_id, +struct ssdfs_segment_info **si, +struct ssdfs_btree_node_content *content); +int ssdfs_btree_create_root_node (struct ssdfs_btree_node *node, +struct ssdfs_btree_inline_root_node *root_node); +int ssdfs_btree_node_pre_flush_header (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_header *hdr); +int ssdfs_btree_common_node_flush (struct ssdfs_btree_node *node); +int ssdfs_btree_node_commit_log (struct ssdfs_btree_node *node); +int ssdfs_btree_deleted_node_commit_log (struct ssdfs_btree_node *node); +int __ssdfs_btree_root_node_extract_index (struct ssdfs_btree_node *node, +u16 found_index, +struct ssdfs_btree_index_key *ptr); +int ssdfs_btree_root_node_delete_index (struct ssdfs_btree_node *node, +u16 position); +int ssdfs_btree_common_node_delete_index (struct ssdfs_btree_node *node, +u16 position); +int ssdfs_find_index_by_hash (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u64 hash, +u16 *found_index); +int ssdfs_btree_node_find_index_position (struct ssdfs_btree_node *node, +u64 hash, +u16 *found_position); +int ssdfs_btree_node_extract_range (u16 start_index,u16 count, +struct ssdfs_btree_search *search); +int ssdfs_btree_node_get_index (struct ssdfs_fs_info *fsi, +struct ssdfs_btree_node_content *content, +u32 area_offset,u32 area_size, +u32 node_size,u16 position, +struct ssdfs_btree_index_key *ptr); +int ssdfs_btree_node_move_index_range (struct ssdfs_btree_node *src, +u16 src_start, +struct ssdfs_btree_node *dst, +u16 dst_start,u16 count); +int ssdfs_btree_node_move_items_range (struct ssdfs_btree_node *src, +struct ssdfs_btree_node *dst, +u16 start_item,u16 count); +int ssdfs_copy_item_in_buffer (struct ssdfs_btree_node *node, +u16 index, +size_t item_size, +struct ssdfs_btree_search *search); +bool is_last_leaf_node_found (struct ssdfs_btree_search *search); +int ssdfs_btree_node_find_lookup_index_nolock (struct ssdfs_btree_search *search, +__le64 *lookup_table, +int table_capacity, +u16 *lookup_index); +typedef int (*ssdfs_check_found_item )(struct ssdfs_fs_info *fsi, +struct ssdfs_btree_search *search, +void *kaddr, +u16 item_index, +u64 *start_hash, +u64 *end_hash, +u16 *found_index); +typedef int (*ssdfs_prepare_result_buffer )(struct ssdfs_btree_search *search, +u16 found_index, +u64 start_hash, +u64 end_hash, +u16 items_count, +size_t item_size); +typedef int (*ssdfs_extract_found_item )(struct ssdfs_fs_info *fsi, +struct ssdfs_btree_search *search, +size_t item_size, +void *kaddr, +u64 *start_hash, +u64 *end_hash); +int __ssdfs_extract_range_by_lookup_index (struct ssdfs_btree_node *node, +u16 lookup_index, +int lookup_table_capacity, +size_t item_size, +struct ssdfs_btree_search *search, +ssdfs_check_found_item check_item, +ssdfs_prepare_result_buffer prepare_buffer, +ssdfs_extract_found_item extract_item); +int ssdfs_shift_range_right (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +size_t item_size, +u16 start_index,u16 range_len, +u16 shift); +int ssdfs_shift_range_right2 (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +size_t item_size, +u16 start_index,u16 range_len, +u16 shift); +int ssdfs_shift_range_left (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +size_t item_size, +u16 start_index,u16 range_len, +u16 shift); +int ssdfs_shift_range_left2 (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +size_t item_size, +u16 start_index,u16 range_len, +u16 shift); +int ssdfs_shift_memory_range_right (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +u16 offset,u16 range_len, +u16 shift); +int ssdfs_shift_memory_range_right2 (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 offset,u16 range_len, +u16 shift); +int ssdfs_shift_memory_range_left (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +u16 offset,u16 range_len, +u16 shift); +int ssdfs_shift_memory_range_left2 (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 offset,u16 range_len, +u16 shift); +int ssdfs_generic_insert_range (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +size_t item_size, +struct ssdfs_btree_search *search); +int ssdfs_invalidate_root_node_hierarchy (struct ssdfs_btree_node *node); +int __ssdfs_btree_node_extract_range (struct ssdfs_btree_node *node, +u16 start_index,u16 count, +size_t item_size, +struct ssdfs_btree_search *search); +int __ssdfs_btree_node_resize_items_area (struct ssdfs_btree_node *node, +size_t item_size, +size_t index_size, +u32 new_size); +int __ssdfs_define_memory_folio (struct ssdfs_fs_info *fsi, +u32 area_offset,u32 area_size, +u32 node_size,size_t item_size, +u16 position, +struct ssdfs_offset2folio *desc); +int ssdfs_btree_node_get_hash_range (struct ssdfs_btree_search *search, +u64 *start_hash,u64 *end_hash, +u16 *items_count); +int __ssdfs_btree_common_node_extract_index (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_index_area *area, +u16 found_index, +struct ssdfs_btree_index_key *ptr); +int ssdfs_btree_node_check_hash_range (struct ssdfs_btree_node *node, +u16 items_count, +u16 items_capacity, +u64 start_hash, +u64 end_hash, +struct ssdfs_btree_search *search); +int ssdfs_btree_node_clear_range (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +size_t item_size, +struct ssdfs_btree_search *search); +int __ssdfs_btree_node_clear_range (struct ssdfs_btree_node *node, +struct ssdfs_btree_node_items_area *area, +size_t item_size, +u16 start_index, +unsigned int range_len); +int ssdfs_btree_node_copy_header_nolock (struct ssdfs_btree_node *node, +struct folio *folio, +u32 *write_offset); +int ssdfs_btree_node_read_content (struct ssdfs_btree_node *node, +u32 offset,u32 size, +void *buf,u32 buf_size); +int ssdfs_btree_node_write_content (struct ssdfs_btree_node *node, +u32 offset,u32 size, +void *buf,u32 buf_size); + +void ssdfs_btree_node_forget_folio_batch (struct folio_batch *batch); +void ssdfs_show_btree_node_info (struct ssdfs_btree_node *node); +void ssdfs_debug_btree_node_object (struct ssdfs_btree_node *node); + +#endif /* _SSDFS_BTREE_NODE_H */
Signature · 1 line
2.34.1
PATCH v2 48/79 ssdfs: introduce b-tree hierarchy object
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
B-tree needs to serve the operations of adding items, inserting items, and deleting items. These operations could require modification of b-tree structure (adding and deleting nodes). Also, indexes need to be updated in parent nodes. SSDFS file system uses the special b-tree hierarchy object to manage the b-tree structure. For every b-tree modification request, file system logic creates the hierarchy object and executes the b-tree hierarchy check. The checking logic defines the actions that should be done for every level of b-tree to execute b-tree node's add or delete operation. Finally, b-tree hierarchy object represents the actions plan that modification logic has to execute.
The main goal of checking b-tree hierarchy for add operation is to define how many new nodes and which type(s) should be added. Any b-tree starts with creation of root node. The root node can store only two index keys. Initially, SSDFS logic adds leaf nodes into empty b-tree. If root node already contains two index keys on leaf nodes, then hybrid node needs to be added into the b-tree. Hybrid node contains as index as item areas. New items will be added into items area of hybrid node until this area becomes completely full. B-tree logic allocates new leaf node, all existing items in hybrid node are moved into newly created leaf node, and index key is added into hybrid node's index area. Such operation repeat multiple times until index area of hybrid node becomes completely full. Now index area is resized by increasing twice in size after moving existing items into newly created node. Finally, hybrid node will be converted into index node. If root node contains two index keys on hybrid nodes, then index node will be added in the b-tree. Generaly speaking, the leaf nodes are always allocated for the lowest level. Next level contains hybrid nodes. And the rest of b-tree levels contain index nodes.
Every b-tree node has associated hash value that represents starting hash value of records sequence in the node. This hash value is stored in index key that keeps parent node. Finally, hash values are used for search items in the b-tree. If modification operation changes starting hash value in the node, then index key in parent node has to be updated. The checking logic identifies all parent nodes that requires index keys update. As a result, modification logic executes index keys update in all parent nodes that were selected for update by checking logic. Delete operation requires to identify which nodes are empty and should be deleted/invalidated. This invalidation plan is executed by modification logic, finally.
For every b-tree modification request, file system logic creates the hierarchy object and executes the b-tree hierarchy check. The checking logic defines the actions that should be done for every level of b-tree to execute b-tree node's add or delete operation. Finally, b-tree hierarchy object represents the actions plan that modification logic has to execute. The execution logic simply starts from the bottom of the hierarchy and executes planned action for every level of b-tree. The planned actions could include adding a new empty node, moving items from hybrid parent node into leaf one, rebalancing b-tree, updating indexes. Finally, b-tree should be able to receive new items/indexes and be consistent.
--- fs/ssdfs/btree_hierarchy.h | 336 +++++++++++++++++++++++++++++++++++++ 1 file changed, 336 insertions(+) create mode 100644 fs/ssdfs/btree_hierarchy.h
fs/ssdfs/btree_hierarchy.h new file +336 -0
@@ -0,0 +1,336 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/btree_hierarchy.h - btree hierarchy declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_BTREE_HIERARCHY_H +#define _SSDFS_BTREE_HIERARCHY_H + +/* +* struct ssdfs_hash_range - hash range +* @start: start hash +* @end: end hash +*/ +struct ssdfs_hash_range { +u64 start; +u64 end; +}; + +/* +* struct ssdfs_btree_node_position - node's position range +* @state: intersection state +* @start: starting node's position +* @count: number of positions in the range +*/ +struct ssdfs_btree_node_position { +int state; +u16 start; +u16 count; +}; + +/* Intersection states */ +enum { +SSDFS_HASH_RANGE_INTERSECTION_UNDEFINED , +SSDFS_HASH_RANGE_LEFT_ADJACENT , +SSDFS_HASH_RANGE_INTERSECTION , +SSDFS_HASH_RANGE_RIGHT_ADJACENT , +SSDFS_HASH_RANGE_OUT_OF_NODE , +SSDFS_HASH_RANGE_INTERSECTION_STATE_MAX +}; + +/* +* struct ssdfs_btree_node_insert - insert position +* @op_state: operation state +* @hash: hash range of insertion +* @pos: position descriptor +*/ +struct ssdfs_btree_node_insert { +int op_state; +struct ssdfs_hash_range hash; +struct ssdfs_btree_node_position pos; +}; + +/* +* struct ssdfs_btree_node_move - moving range descriptor +* @op_state: operation state +* @direction: moving direction +* @pos: position descriptor +*/ +struct ssdfs_btree_node_move { +int op_state; +int direction; +struct ssdfs_btree_node_position pos; +}; + +/* +* struct ssdfs_btree_node_delete - deleting node's index descriptor +* @op_state: operation state +* @node_index: node index for deletion +*/ +struct ssdfs_btree_node_delete { +int op_state; +struct ssdfs_btree_index_key node_index; +}; + +/* Possible operation states */ +enum { +SSDFS_BTREE_AREA_OP_UNKNOWN , +SSDFS_BTREE_AREA_OP_REQUESTED , +SSDFS_BTREE_AREA_OP_DONE , +SSDFS_BTREE_AREA_OP_FAILED , +SSDFS_BTREE_AREA_OP_STATE_MAX +}; + +/* Possible moving directions */ +enum { +SSDFS_BTREE_MOVE_NOWHERE , +SSDFS_BTREE_MOVE_TO_PARENT , +SSDFS_BTREE_MOVE_TO_CHILD , +SSDFS_BTREE_MOVE_TO_LEFT , +SSDFS_BTREE_MOVE_TO_RIGHT , +SSDFS_BTREE_MOVE_DIRECTION_MAX +}; + +/* Btree level's flags */ +#define SSDFS_BTREE_LEVEL_ADD_NODE (1 << 0) +#define SSDFS_BTREE_LEVEL_ADD_INDEX (1 << 1) +#define SSDFS_BTREE_LEVEL_UPDATE_INDEX (1 << 2) +#define SSDFS_BTREE_LEVEL_ADD_ITEM (1 << 3) +#define SSDFS_BTREE_INDEX_AREA_NEED_MOVE (1 << 4) +#define SSDFS_BTREE_ITEMS_AREA_NEED_MOVE (1 << 5) +#define SSDFS_BTREE_TRY_RESIZE_INDEX_AREA (1 << 6) +#define SSDFS_BTREE_LEVEL_DELETE_NODE (1 << 7) +#define SSDFS_BTREE_LEVEL_DELETE_INDEX (1 << 8) +#define SSDFS_BTREE_LEVEL_FLAGS_MASK 0x1FF + +#define SSDFS_BTREE_ADD_NODE_MASK \ + (SSDFS_BTREE_LEVEL_ADD_NODE | SSDFS_BTREE_LEVEL_ADD_INDEX | \ + SSDFS_BTREE_LEVEL_UPDATE_INDEX | SSDFS_BTREE_LEVEL_ADD_ITEM | \ + SSDFS_BTREE_INDEX_AREA_NEED_MOVE | \ + SSDFS_BTREE_ITEMS_AREA_NEED_MOVE | \ + SSDFS_BTREE_TRY_RESIZE_INDEX_AREA) + +#define SSDFS_BTREE_DELETE_NODE_MASK \ + (SSDFS_BTREE_LEVEL_UPDATE_INDEX | SSDFS_BTREE_LEVEL_DELETE_NODE | \ + SSDFS_BTREE_LEVEL_DELETE_INDEX | SSDFS_BTREE_ITEMS_AREA_NEED_MOVE) + +/* +* struct ssdfs_btree_level_node - node descriptor +* @type: node's type +* @index_hash: old index area's hash pair +* @items_hash: old items area's hash pair +* @ptr: pointer on node's object +*/ +struct ssdfs_btree_level_node { +int type; +struct ssdfs_hash_range index_hash; +struct ssdfs_hash_range items_hash; +struct ssdfs_btree_node *ptr; +}; + +/* +* struct ssdfs_btree_node_details - b-tree node details +* @ptr: node pointer +* @index_area.index_count: count of indexes in index area +* @index_area.index_capacity: capacity of indexes in index area +* @items_area.area_size: items area's size in bytes +* @items_area.free_space: items area's free space in bytes +* @items_area.item_size: size of item in bytes +* @items_area.min_item_size: minimal size of item in bytes +* @items_area.max_item_size: maximum size of item in bytes +* @items_area.items_count: count of items in items area +* @items_area.items_capacity: capacity of items in items area +*/ +struct ssdfs_btree_node_details { +struct ssdfs_btree_node *ptr; + +struct { +u16 index_count; +u16 index_capacity; + } index_area; + +struct { +u32 area_size; +u32 free_space; +u16 item_size; +u8 min_item_size; +u16 max_item_size; +u16 items_count; +u16 items_capacity; + } items_area; +}; + +/* +* struct ssdfs_btree_level_node_desc - descriptor of level's nodes +* @left_node: left node from the old node of the level +* @old_node: old node of the level +* @right_node: right node from the old node of the level +* @new_node: created empty node +*/ +struct ssdfs_btree_level_node_desc { +struct ssdfs_btree_level_node left_node; +struct ssdfs_btree_level_node old_node; +struct ssdfs_btree_level_node right_node; +struct ssdfs_btree_level_node new_node; +}; + +/* +* struct ssdfs_btree_level - btree level descriptor +* @flags: level's flags +* @index_area.area_size: size of the index area +* @index_area.free_space: free space in index area +* @index_area.hash: hash range of index area +* @items_area.add: adding index descriptor +* @index_area.insert: insert position descriptor +* @index_area.move: move range descriptor +* @index_area.delete: delete index descriptor +* @items_area.area_size: size of the items area +* @items_area.free_space: free space in items area +* @items_area.hash: hash range of items area +* @items_area.add: adding item descriptor +* @items_area.insert: insert position descriptor +* @items_area.child2parent: child to/from parent move range descriptor +* @items_area.old2sibling: sibling to/from sibling move range descriptor +* @nodes: descriptor of level's nodes +*/ +struct ssdfs_btree_level { +u32 flags; + +struct { +u32 area_size; +u32 free_space; +struct ssdfs_hash_range hash; +struct ssdfs_btree_node_insert add; +struct ssdfs_btree_node_insert insert; +struct ssdfs_btree_node_move move; +struct ssdfs_btree_node_delete delete; + } index_area; + +struct { +u32 area_size; +u32 free_space; +struct ssdfs_hash_range hash; +struct ssdfs_btree_node_insert add; +struct ssdfs_btree_node_insert insert; +struct ssdfs_btree_node_move child2parent; +struct ssdfs_btree_node_move old2sibling; + } items_area; + +struct ssdfs_btree_level_node_desc nodes; +}; + +/* +* struct ssdfs_btree_state_descriptor - btree's state descriptor +* @height: btree height +* @increment_height: request to increment tree's height +* @node_size: size of the node in bytes +* @index_size: size of the index record in bytes +* @min_item_size: minimum item size in bytes +* @max_item_size: maximum item size in bytes +* @index_area_min_size: minimum size of index area in bytes +*/ +struct ssdfs_btree_state_descriptor { +int height; +bool increment_height; +u32 node_size; +u16 index_size; +u16 min_item_size; +u16 max_item_size; +u16 index_area_min_size; +}; + +/* +* struct ssdfs_btree_hierarchy - btree's hierarchy descriptor +* @desc: btree state's descriptor +* @array_ptr: btree level's array +*/ +struct ssdfs_btree_hierarchy { +struct ssdfs_btree_state_descriptor desc; +struct ssdfs_btree_level **array_ptr; +}; + +/* Btree hierarchy inline methods */ +static inline +bool need_add_node (struct ssdfs_btree_level *level) +{ +return level->flags &SSDFS_BTREE_LEVEL_ADD_NODE ; +} + +static inline +bool need_delete_node (struct ssdfs_btree_level *level) +{ +return level->flags &SSDFS_BTREE_LEVEL_DELETE_NODE ; +} + +/* Btree hierarchy API */ +struct ssdfs_btree_hierarchy * +ssdfs_btree_hierarchy_allocate (struct ssdfs_btree *tree); +void ssdfs_btree_hierarchy_init (struct ssdfs_btree *tree, +struct ssdfs_btree_hierarchy *hierarchy); +void ssdfs_btree_hierarchy_free (struct ssdfs_btree_hierarchy *hierarchy); + +bool need_update_parent_index_area (u64 start_hash, +struct ssdfs_btree_node *child); +int ssdfs_btree_check_hierarchy_for_add (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *ptr); +int ssdfs_btree_process_level_for_add (struct ssdfs_btree_hierarchy *ptr, +int cur_height, +struct ssdfs_btree_search *search); +int ssdfs_btree_check_hierarchy_for_delete (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *ptr); +int ssdfs_btree_process_level_for_delete (struct ssdfs_btree_hierarchy *ptr, +int cur_height, +struct ssdfs_btree_search *search); +int ssdfs_btree_check_hierarchy_for_update (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *ptr); +int ssdfs_btree_process_level_for_update (struct ssdfs_btree_hierarchy *ptr, +int cur_height, +struct ssdfs_btree_search *search); +bool __need_migrate_items_to_parent_node (struct ssdfs_btree_node *parent, +struct ssdfs_btree_node *child); +int ssdfs_btree_check_hierarchy_for_parent_child_merge (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *hierarchy); +int ssdfs_btree_process_level_for_node_merge (struct ssdfs_btree_hierarchy *ptr, +int cur_height, +struct ssdfs_btree_search *search); +bool __need_merge_sibling_nodes (struct ssdfs_btree_node *parent, +struct ssdfs_btree_node *child); +int ssdfs_btree_check_hierarchy_for_siblings_merge (struct ssdfs_btree *tree, +struct ssdfs_btree_search *search, +struct ssdfs_btree_hierarchy *hierarchy); + +/* Btree hierarchy internal API*/ +void ssdfs_btree_prepare_add_node (struct ssdfs_btree *tree, +int node_type, +u64 start_hash,u64 end_hash, +struct ssdfs_btree_level *level, +struct ssdfs_btree_node *node); +int ssdfs_btree_prepare_add_index (struct ssdfs_btree_level *level, +u64 start_hash,u64 end_hash, +struct ssdfs_btree_node *node); + +void ssdfs_show_btree_hierarchy_object (struct ssdfs_btree_hierarchy *ptr); +void ssdfs_debug_btree_hierarchy_object (struct ssdfs_btree_hierarchy *ptr); + +#endif /* _SSDFS_BTREE_HIERARCHY_H */
Signature · 1 line
2.34.1
PATCH v2 50/79 ssdfs: introduce inodes b-tree
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS raw inode is the metadata structure of fixed size that can vary from 256 bytes to several KBs. The size of inode is defined during the file system’s volume creation. The most special part of the SSDFS raw inode is the private area that is used for storing:
(1) small file inline, (2) root node of extents, dentries, and/or xattr b-tree.
SSDFS inodes b-tree is the hybrid b-tree that includes the hybrid nodes with the goal to use the node’s space in more efficient way by means of combination the index and data records inside of the node. Root node of inodes b-tree is stored into the log footer or partial log header of every log. Generally speaking, it means that SSDFS file system is using the massive replication of the root node of inodes b-tree. Actually, inodes b-tree node’s space includes header, index area (in the case of hybrid node), and array of inodes are ordered by ID values. If node has 8 KB in size and inode structure is 256 bytes in size then the maximum capacity of one inodes b-tree’s node is 32 inodes.
Generally speaking, inodes table can be imagined like an imaginary array that is extended by means of adding the new inodes into the tail of the array. However, inode can be allocated or deleted by virtue of create file or delete file operations, for example. As a result, every b-tree node has an allocation bitmap that is tracking the state (used or free) of every inode in the b-tree node. The allocation bitmap provides the mechanism of fast lookup a free inode with the goal to reuse the inodes of deleted files.
Additionally, every b-tree node has a dirty bitmap that has goal to track modification of inodes. Generally speaking, the dirty bitmap provides the opportunity to flush not the whole node but the modified inodes only. As a result, such bitmap could play the cornerstone role in
the delta-encoding or in the Diff-On-Write approach. Moreover, b-tree node has a lock bitmap that has responsibility to implement the mechanism of exclusive lock a particular inode without the necessity to lock exclusively the whole node. Generally speaking, the lock bitmap was introduced with the goal to improve the granularity of lock operation. As a result, it provides the way to modify the different inodes in the same b-tree node without the using of exclusive lock the whole b-tree node. However, the exclusive lock of the whole tree has to be used for the case of addition or deletion a b-tree node.
Inodes b-tree supports operations:
(1) find_item - find item in the b-tree
(2) find_range - find range of items in the b-tree
(3) extract_range - extract range of items from the node of b-tree
(4) allocate_item - allocate item in b-tree
(5) allocate_range - allocate range of items in b-tree
(6) insert_item - insert item into node of the b-tree
(7) insert_range - insert range of items into node of the b-tree
(8) change_item - change item in the b-tree
(9) delete_item - delete item from the b-tree
(10) delete_range - delete range of items from a node of b-tree
--- fs/ssdfs/inodes_tree.h | 181 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 fs/ssdfs/inodes_tree.h
fs/ssdfs/inodes_tree.h new file +181 -0
@@ -0,0 +1,181 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/inodes_tree.h - inodes btree declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_INODES_TREE_H +#define _SSDFS_INODES_TREE_H + +/* +* struct ssdfs_inodes_range - items range +* @start_hash: starting hash +* @start_index: staring index in the node +* @count: count of items in the range +*/ +struct ssdfs_inodes_range { +#define SSDFS_INODES_RANGE_INVALID_START (U64_MAX) +u64 start_hash; +#define SSDFS_INODES_RANGE_INVALID_INDEX (U16_MAX) +u16 start_index; +u16 count; +}; + +/* +* struct ssdfs_inodes_btree_range - node's items range descriptor +* @list: free inode ranges queue +* @node_id: node identification number +* @area: items range +*/ +struct ssdfs_inodes_btree_range { +struct list_head list; +u32 node_id; +struct ssdfs_inodes_range area; +}; + +/* +* struct ssdfs_free_inode_range_queue - free inode ranges queue +* @lock: queue's lock +* @list: queue's list +*/ +struct ssdfs_free_inode_range_queue { +spinlock_t lock; +struct list_head list; +}; + +/* +* struct ssdfs_inodes_btree_info - inodes btree info +* @generic_tree: generic btree description +* @lock: inodes btree lock +* @root_folder: copy of root folder's inode +* @upper_allocated_ino: maximal allocated inode ID number +* @last_free_ino: latest free inode ID number +* @allocated_inodes: allocated inodes count in the whole tree +* @free_inodes: free inodes count in the whole tree +* @inodes_capacity: inodes capacity in the whole tree +* @leaf_nodes: count of leaf nodes in the whole tree +* @nodes_count: count of all nodes in the whole tree +* @raw_inode_size: size in bytes of raw inode +* @free_inodes_queue: queue of free inode descriptors +* @fsi: pointer on shared file system object +*/ +struct ssdfs_inodes_btree_info { +struct ssdfs_btree generic_tree; + +spinlock_t lock; +struct ssdfs_inode root_folder; +u64 upper_allocated_ino; +u64 last_free_ino; +u64 allocated_inodes; +u64 free_inodes; +u64 inodes_capacity; +u32 leaf_nodes; +u32 nodes_count; +u16 raw_inode_size; + +/* +* Inodes btree should have special allocation queue. +* If a btree nodes has free (not allocated) inodes +* items then the information about such btree node +* should be added into queue. Moreover, queue should +* contain as so many node's descriptors as free items +* in the node. +* +* If some btree node has deleted inodes (free items) +* then all node's descriptors should be added into +* the head of allocation queue. Descriptors of the last +* btree's node should be added into tail of the queue. +* Information about node's descriptors should be added +* into the allocation queue during btree node creation +* or reading from the volume. Otherwise, allocation of +* new items should be done from last leaf btree's node. +*/ +struct ssdfs_free_inode_range_queue free_inodes_queue; + +struct ssdfs_fs_info *fsi; +}; + +/* +* Inline methods +*/ +static inline +bool is_free_inodes_range_invalid (struct ssdfs_inodes_btree_range *range) +{ +bool is_invalid; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!range); +#endif /* CONFIG_SSDFS_DEBUG */ + + is_invalid = range->node_id ==SSDFS_BTREE_NODE_INVALID_ID || + range->area.start_hash ==SSDFS_INODES_RANGE_INVALID_START || + range->area.start_index ==SSDFS_INODES_RANGE_INVALID_INDEX || + range->area.count ==0 ; + +if (is_invalid) { +SSDFS_ERR ("node_id %u, start_hash %llx, " +"start_index %u, count %u\n" , + range->node_id, + range->area.start_hash, + range->area.start_index, + range->area.count); + } + +return is_invalid; +} + +/* +* Free inodes range API +*/ +struct ssdfs_inodes_btree_range *ssdfs_free_inodes_range_alloc (void ); +void ssdfs_free_inodes_range_free (struct ssdfs_inodes_btree_range *range); +void ssdfs_free_inodes_range_init (struct ssdfs_inodes_btree_range *range); + +/* +* Inodes btree API +*/ +int ssdfs_inodes_btree_create (struct ssdfs_fs_info *fsi); +void ssdfs_inodes_btree_destroy (struct ssdfs_fs_info *fsi); +int ssdfs_inodes_btree_flush (struct ssdfs_inodes_btree_info *tree); + +int ssdfs_inodes_btree_allocate (struct ssdfs_inodes_btree_info *tree, +ino_t *ino, +struct ssdfs_btree_search *search); +int ssdfs_inodes_btree_find (struct ssdfs_inodes_btree_info *tree, +ino_t ino, +struct ssdfs_btree_search *search); +int ssdfs_inodes_btree_change (struct ssdfs_inodes_btree_info *tree, +ino_t ino, +struct ssdfs_btree_search *search); +int ssdfs_inodes_btree_delete (struct ssdfs_inodes_btree_info *tree, +ino_t ino); +int ssdfs_inodes_btree_delete_range (struct ssdfs_inodes_btree_info *tree, +ino_t ino,u16 count); + +void ssdfs_debug_inodes_btree_object (struct ssdfs_inodes_btree_info *tree); + +/* +* Inodes btree specialized operations +*/ +extern const struct ssdfs_btree_descriptor_operations + ssdfs_inodes_btree_desc_ops; +extern const struct ssdfs_btree_operations ssdfs_inodes_btree_ops; +extern const struct ssdfs_btree_node_operations ssdfs_inodes_btree_node_ops; + +#endif /* _SSDFS_INODES_TREE_H */
Signature · 1 line
2.34.1
PATCH v2 52/79 ssdfs: introduce dentries b-tree
In reply to Viacheslav DubeykoComplete patchset is available here:
https://github.com/dubeyko/ssdfs-driver/tree/master/patchset/linux-kernel-6.18.0
SSDFS dentry is the metadata structure of fixed size (32 bytes). It contains inode ID, name hash, name length, and inline string for 12 symbols. Generally speaking, the dentry is able to store 8.3 filename inline. If the name of file/folder has longer name (more than 12 symbols) then the dentry will keep only the portion of the name but the whole name will be stored into a shared dictionary. The goal of such approach is to represent the dentry by compact metadata structure of fixed size for the fast and efficient operations with the dentries. It is possible to point out that there are a lot of use-cases when the length of file or folder is not very long. As a result, dentry’s inline string could be only storage for the file/folder name. Moreover, the goal of shared dictionary is to store the long names efficiently by means of using the deduplication mechanism.
Dentries b-tree is the hybrid b-tree with the root node is stored into the private inode’s area. By default, inode’s private area has 128 bytes in size. Also SSDFS dentry has 32 bytes in size. As a result, inode’s private area provides enough space for 4 inline dentries. Generally speaking, if a folder contains 4 or lesser files then the dentries can be stored into the inode’s private area without the necessity to create the dentries b-tree. Otherwise, if a folder includes more than 4 files or folders then it needs to create the regular dentries b-tree with the root node is stored into the private area of inode. Actually, every node of dentries b-tree contains the header, index area (for the case of hybrid node), and array of dentries are ordered by hash value of filename. Moreover, if a b-tree node has 8 KB size then it is capable to contain maximum 256 dentries. Generally speaking, the hybrid b-tree was opted for the dentries metadata structure by virtue of compactness of metadata structure representation and efficient lookup mechanism. Dentries is ordered on the basis of name’s hash. Every node of dentries b-tree has: (1) dirty bitmap - tracking modified dentries, (2) lock bitmap - exclusive locking of particular dentries without the necessity to lock the whole b-tree node. Actually, it is expected that dentries b-tree could contain not many nodes in average because the two nodes (8K in size) of dentries b-tree is capable to store about 400 dentries.
Dentries b-tree implements API:
(1) create - create dentries b-tree
(2) destroy - destroy dentries b-tree
(3) flush - flush dirty dentries b-tree
(4) find - find dentry for a name in b-tree
(5) add - add dentry object into b-tree
(6) change - change/update dentry object in b-tree
(7) delete - delete dentry object from b-tree
(8) delete_all - delete all dentries from b-tree
Dentries b-tree node implements a specialized API:
(1) find_item - find item in the node
(2) find_range - find range of items in the node
(3) extract_range - extract range of items (or all items) from node
(4) insert_item - insert item in the node
(5) insert_range - insert range of items in the node
(6) change_item - change item in the node
(7) delete_item - delete item from the node
(8) delete_range - delete range of items from the node
--- fs/ssdfs/dentries_tree.h | 158 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 fs/ssdfs/dentries_tree.h
fs/ssdfs/dentries_tree.h new file +158 -0
@@ -0,0 +1,158 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause-Clear +* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/dentries_tree.h - dentries btree declarations. +* +* Copyright (c) 2014-2019 HGST, a Western Digital Company. +* http://www.hgst.com/ +* Copyright (c) 2014-2026 Viacheslav Dubeyko <[email protected]> +* http://www.ssdfs.org/ +* +* (C) Copyright 2014-2019, HGST, Inc., All rights reserved. +* +* Created by HGST, San Jose Research Center, Storage Architecture Group +* +* Authors: Viacheslav Dubeyko <[email protected]> +* +* Acknowledgement: Cyril Guyot +* Zvonimir Bandic +*/ + +#ifndef _SSDFS_DENTRIES_TREE_H +#define _SSDFS_DENTRIES_TREE_H + +#define SSDFS_INLINE_DENTRIES_COUNT (2 * SSDFS_INLINE_DENTRIES_PER_AREA) +#define SSDFS_FOLDER_DEFAULT_SHORTCUTS_COUNT (2) + +/* +* struct ssdfs_dentries_btree_info - dentries btree info +* @type: dentries btree type +* @state: dentries btree state +* @dentries_count: count of the dentries in the whole dentries tree +* @lock: dentries btree lock +* @generic_tree: pointer on generic btree object +* @inline_dentries: pointer on inline dentries array +* @buffer.tree: piece of memory for generic btree object +* @buffer.dentries: piece of memory for the inline dentries +* @root: pointer on root node +* @root_buffer: buffer for root node +* @desc: b-tree descriptor +* @owner: pointer on owner inode object +* @fsi: pointer on shared file system object +* +* A newly created inode tries to store dentries into inline +* dentries. The raw on-disk inode has internal private area +* that is able to contain the four inline dentries or +* root node of extents btree and extended attributes btree. +* If inode hasn't extended attributes and the amount of dentries +* are lesser than four then everithing can be stored inside of +* inline dentries. Otherwise, the real dentries btree should +* be created. +*/ +struct ssdfs_dentries_btree_info { +atomic_t type; +atomic_t state; +atomic64_t dentries_count; + +struct rw_semaphore lock; +struct ssdfs_btree *generic_tree; +struct ssdfs_dir_entry *inline_dentries; +union { +struct ssdfs_btree tree; +struct ssdfs_dir_entry dentries[SSDFS_INLINE_DENTRIES_COUNT ]; + } buffer; +struct ssdfs_btree_inline_root_node *root; +struct ssdfs_btree_inline_root_node root_buffer; + +struct ssdfs_dentries_btree_descriptor desc; +struct ssdfs_inode_info *owner; +struct ssdfs_fs_info *fsi; +}; + +/* Dentries tree types */ +enum { +SSDFS_DENTRIES_BTREE_UNKNOWN_TYPE , +SSDFS_INLINE_DENTRIES_ARRAY , +SSDFS_PRIVATE_DENTRIES_BTREE , +SSDFS_DENTRIES_BTREE_TYPE_MAX +}; + +/* Dentries tree states */ +enum { +SSDFS_DENTRIES_BTREE_UNKNOWN_STATE , +SSDFS_DENTRIES_BTREE_CREATED , +SSDFS_DENTRIES_BTREE_INITIALIZED , +SSDFS_DENTRIES_BTREE_DIRTY , +SSDFS_DENTRIES_BTREE_CORRUPTED , +SSDFS_DENTRIES_BTREE_STATE_MAX +}; + +/* +* Inline methods +*/ +static inline +size_t ssdfs_inline_dentries_size (void ) +{ +size_t dentry_size =sizeof (struct ssdfs_dir_entry ); +return dentry_size *SSDFS_INLINE_DENTRIES_COUNT ; +} + +static inline +size_t ssdfs_area_dentries_size (void ) +{ +size_t dentry_size =sizeof (struct ssdfs_dir_entry ); +return dentry_size *SSDFS_INLINE_DENTRIES_PER_AREA ; +} + +/* +* Dentries tree API +*/ +int ssdfs_dentries_tree_create (struct ssdfs_fs_info *fsi, +struct ssdfs_inode_info *ii); +int ssdfs_dentries_tree_init (struct ssdfs_fs_info *fsi, +struct ssdfs_inode_info *ii); +void ssdfs_dentries_tree_destroy (struct ssdfs_inode_info *ii); +int ssdfs_dentries_tree_flush (struct ssdfs_fs_info *fsi, +struct ssdfs_inode_info *ii); + +int ssdfs_dentries_tree_find (struct ssdfs_dentries_btree_info *tree, +const char *name,size_t len, +struct ssdfs_btree_search *search); +int ssdfs_dentries_tree_add (struct ssdfs_dentries_btree_info *tree, +const struct qstr *str, +struct ssdfs_inode_info *ii, +struct ssdfs_btree_search *search); +int ssdfs_dentries_tree_change (struct ssdfs_dentries_btree_info *tree, +u64 name_hash,ino_t old_ino, +const struct qstr *str, +struct ssdfs_inode_info *new_ii, +struct ssdfs_btree_search *search); +int ssdfs_dentries_tree_delete (struct ssdfs_dentries_btree_info *tree, +u64 name_hash,ino_t ino, +struct ssdfs_btree_search *search); +int ssdfs_dentries_tree_delete_all (struct ssdfs_dentries_btree_info *tree); + +/* +* Internal dentries tree API +*/ +u64 ssdfs_generate_name_hash (const struct qstr *str); +int ssdfs_dentries_tree_find_leaf_node (struct ssdfs_dentries_btree_info *tree, +u64 name_hash, +struct ssdfs_btree_search *search); +int ssdfs_dentries_tree_extract_range (struct ssdfs_dentries_btree_info *tree, +u16 start_index,u16 count, +struct ssdfs_btree_search *search); + +void ssdfs_debug_dentries_btree_object (struct ssdfs_dentries_btree_info *tree); + +/* +* Dentries btree specialized operations +*/ +extern const struct ssdfs_btree_descriptor_operations + ssdfs_dentries_btree_desc_ops; +extern const struct ssdfs_btree_operations ssdfs_dentries_btree_ops; +extern const struct ssdfs_btree_node_operations ssdfs_dentries_btree_node_ops; + +#endif /* _SSDFS_DENTRIES_TREE_H */
Signature · 1 line
2.34.1