1 #ifndef __LUNAIX_EXT2_H
2 #define __LUNAIX_EXT2_H
4 #include <lunaix/fs/api.h>
5 #include <lunaix/types.h>
6 #include <lunaix/ds/llist.h>
7 #include <lunaix/ds/hashtable.h>
8 #include <lunaix/ds/lru.h>
10 #define FEAT_COMPRESSION 0b00000001
11 #define FEAT_RESIZE_INO 0b00000010
12 #define FEAT_FILETYPE 0b00000100
13 #define FEAT_SPARSE_SB 0b00001000
14 #define FEAT_LARGE_FILE 0b00010000
16 #define IMODE_IFSOCK 0xC000
17 #define IMODE_IFLNK 0xA000
18 #define IMODE_IFREG 0x8000
19 #define IMODE_IFBLK 0x6000
20 #define IMODE_IFDIR 0x4000
21 #define IMODE_IFCHR 0x2000
22 #define IMODE_IFFIFO 0x1000
24 #define IMODE_URD 0x0100
25 #define IMODE_UWR 0x0080
26 #define IMODE_UEX 0x0040
27 #define IMODE_GRD 0x0020
28 #define IMODE_GWR 0x0010
29 #define IMODE_GEX 0x0008
30 #define IMODE_ORD 0x0004
31 #define IMODE_OWR 0x0002
32 #define IMODE_OEX 0x0001
34 #define ext2_aligned compact align(4)
35 #define to_ext2ino_id(fsblock_id) ((fsblock_id) + 1)
36 #define to_fsblock_id(ext2_ino) ((ext2_ino) - 1)
38 extern bcache_zone_t gdesc_bcache_zone;
46 u32_t s_first_data_cnt;
66 u32_t s_checkinterval;
79 u32_t s_optional_feat;
80 u32_t s_required_feat;
96 u16_t bg_free_blk_cnt;
97 u16_t bg_free_ino_cnt;
98 u16_t bg_used_dir_cnt;
126 u32_t directs[12]; // directum
129 u32_t ind1; // prima indirecta
130 u32_t ind23[2]; // secunda et tertia indirecta
134 } ext2_aligned i_block;
135 u32_t i_block_arr[15];
157 #define EXT2_DRE(v_dnode) (fsapi_impl_data(v_dnode, struct ext2b_dirent))
160 #define GDESC_INO_SEL 0
161 #define GDESC_BLK_SEL 1
163 #define GDESC_FREE_LISTS \
166 struct llist_header free_grps_ino; \
167 struct llist_header free_grps_blk; \
169 struct llist_header free_list_sel[2]; \
172 #define check_gdesc_type_sel(sel) \
173 assert_msg(sel == GDESC_INO_SEL || sel == GDESC_BLK_SEL, \
180 * offset to inode table (in terms of blocks) within each block group.
181 * to account the difference of backup presence between rev 0/1
186 unsigned int block_size;
187 unsigned int nr_gdesc_pb;
188 unsigned int nr_gdesc;
189 unsigned int all_feature;
192 struct v_superblock* vsb;
194 struct ext2b_super* raw;
196 struct bcache gd_caches;
201 struct llist_header gds;
205 #define EXT2_SB(vsb) (fsapi_impl_data(vsb, struct ext2_sbinfo))
212 unsigned int nr_bytes;
218 struct llist_header groups;
223 struct ext2_bmp ino_bmp;
224 struct ext2_bmp blk_bmp;
226 struct ext2_bmp bmps[2];
230 unsigned int ino_base;
232 struct ext2b_gdesc* info;
233 struct ext2_sbinfo* sb;
239 Indriection Block Translation Look-aside Buffer
241 Provide a look-aside buffer for all last-level indirect block
242 that is at least two indirection away.
245 16 sets, 256 ways, capacity 4096 blocks
248 struct ext2_btlb_entry
257 struct ext2_btlb_entry buffer[BTLB_SETS];
260 struct ext2_fast_inode
262 struct ext2b_inode* ino;
268 bbuf_t buf; // partial inotab that holds this inode
269 unsigned int inds_lgents; // log2(# of block in an indirection level)
271 size_t indirect_blocks;
274 struct ext2b_inode* ino; // raw ext2 inode
275 struct ext2_btlb* btlb; // block-TLB
276 struct ext2_gdesc* blk_grp; // block group
282 dirent fragmentation degree, we will perform
283 full reconstruction on dirent table when this goes too high.
285 unsigned int dir_fragdeg;
289 // prefetched block for 1st order of indirection
293 #define EXT2_INO(v_inode) (fsapi_impl_data(v_inode, struct ext2_inode))
295 struct ext2_dnode_sub
298 struct ext2b_dirent* dirent;
303 struct ext2_dnode_sub self;
304 struct ext2_dnode_sub prev;
306 #define EXT2_DNO(v_dnode) (fsapi_impl_data(v_dnode, struct ext2_dnode))
310 * @brief General purpose iterator for ext2 objects
315 struct v_inode* inode;
318 struct ext2b_dirent* dirent;
337 struct ext2_iterator iter;
338 struct ext2_inode* b_ino;
340 #define EXT2_FILE(v_file) (fsapi_impl_data(v_file, struct ext2_file))
343 #define MAX_INDS_DEPTH 4
347 unsigned int tables[MAX_INDS_DEPTH];
348 unsigned int indices[MAX_INDS_DEPTH];
353 unsigned int* slot_ref;
358 struct walk_stack stack;
361 static inline unsigned int
362 ext2_datablock(struct v_superblock* vsb, unsigned int id)
364 return EXT2_SB(vsb)->raw->s_first_data_cnt + id;
368 ext2_feature(struct v_superblock* vsb, unsigned int feat)
370 return !!(EXT2_SB(vsb)->all_feature & feat);
373 /* ************ Inodes ************ */
376 ext2ino_init(struct v_superblock* vsb, struct v_inode* inode);
379 ext2ino_get(struct v_superblock* vsb,
380 unsigned int ino, struct ext2_inode** out);
383 ext2ino_get_fast(struct v_superblock* vsb,
384 unsigned int ino, struct ext2_fast_inode* fast_ino);
387 ext2ino_fill(struct v_inode* inode, ino_t ino_id);
390 ext2ino_make(struct v_superblock* vsb, unsigned int itype,
391 struct ext2_inode* hint, struct v_inode** out);
394 ext2ino_update(struct v_inode* inode);
397 ext2ino_resizing(struct v_inode* inode, size_t new_size);
400 ext2ino_linkto(struct ext2_inode* e_ino, struct ext2b_dirent* dirent)
402 dirent->inode = e_ino->ino_id;
403 e_ino->ino->i_lnk_cnt++;
404 fsblock_dirty(e_ino->buf);
408 ext2db_itbegin(struct ext2_iterator* iter, struct v_inode* inode);
411 ext2db_itend(struct ext2_iterator* iter);
414 ext2db_itnext(struct ext2_iterator* iter);
417 ext2db_itffw(struct ext2_iterator* iter, int count);
420 ext2db_itreset(struct ext2_iterator* iter);
424 * @brief Get the data block at given data pos associated with the
425 * inode, return NULL if not present
432 ext2db_get(struct v_inode* inode, unsigned int data_pos);
435 * @brief Get the data block at given data pos associated with the
436 * inode, allocate one if not present.
444 ext2db_acquire(struct v_inode* inode, unsigned int data_pos, bbuf_t* out);
447 ext2db_free_pos(struct v_inode* inode, unsigned int block_pos);
449 /* ************* Walker ************* */
452 ext2walk_init_state(struct walk_state* state)
454 *state = (struct walk_state) { };
458 ext2walk_free_state(struct walk_state* state)
460 fsblock_put(state->table);
463 /* ************* Iterator ************* */
466 ext2_iterror(struct ext2_iterator* iter) {
467 return iter->has_error;
471 ext2_itcheckbuf(struct ext2_iterator* iter) {
472 return !(iter->has_error = blkbuf_errbuf(iter->sel_buf));
475 #define itstate_sel(iter, value) \
476 (ext2_iterror(iter) ? EIO : (int)(value))
479 /* ************ Block Group ************ */
482 ext2gd_prepare_gdt(struct v_superblock* vsb);
485 ext2gd_release_gdt(struct v_superblock* vsb);
488 ext2gd_take(struct v_superblock* vsb,
489 unsigned int index, struct ext2_gdesc** out);
492 ext2gd_put(struct ext2_gdesc* gd) {
493 bcache_return(gd->cache_ref);
497 /* ************ Directory ************ */
500 ext2dr_lookup(struct v_inode* this, struct v_dnode* dnode);
503 ext2dr_read(struct v_file *file, struct dir_context *dctx);
506 ext2dr_itbegin(struct ext2_iterator* iter, struct v_inode* inode);
509 ext2dr_itend(struct ext2_iterator* iter);
512 ext2dr_itdrain(struct ext2_iterator* iter)
514 return iter->pos > iter->end_pos;
518 ext2dr_itnext(struct ext2_iterator* iter);
521 ext2dr_itffw(struct ext2_iterator* iter, int count);
524 ext2dr_itreset(struct ext2_iterator* iter);
527 ext2dr_open(struct v_inode* this, struct v_file* file);
530 ext2dr_close(struct v_inode* this, struct v_file* file);
533 ext2dr_seek(struct v_file* file, size_t offset);
536 ext2dr_insert(struct v_inode* this, struct ext2b_dirent* dirent,
537 struct ext2_dnode** e_dno_out);
540 ext2dr_remove(struct ext2_dnode* e_dno);
543 ext2_rmdir(struct v_inode* parent, struct v_dnode* dnode);
546 ext2_mkdir(struct v_inode* parent, struct v_dnode* dnode);
549 ext2_rename(struct v_inode* from_inode, struct v_dnode* from_dnode,
550 struct v_dnode* to_dnode);
553 ext2dr_setup_dirent(struct ext2b_dirent* dirent,
554 struct v_inode* inode, struct hstr* name);
557 /* ************ Files ************ */
560 ext2_open_inode(struct v_inode* this, struct v_file* file);
563 ext2_close_inode(struct v_file* file);
566 ext2_sync_inode(struct v_inode* inode);
569 ext2_file_sync(struct v_file* file);
572 ext2_inode_read(struct v_inode *inode, void *buffer, size_t len, size_t fpos);
575 ext2_inode_read_page(struct v_inode *inode, void *buffer, size_t fpos);
578 ext2_inode_write(struct v_inode *inode, void *buffer, size_t len, size_t fpos);
581 ext2_inode_write_page(struct v_inode *inode, void *buffer, size_t fpos);
584 ext2_seek_inode(struct v_file* file, size_t offset);
587 ext2_create(struct v_inode* this, struct v_dnode* dnode, unsigned int itype);
590 ext2_link(struct v_inode* this, struct v_dnode* new_name);
593 ext2_unlink(struct v_inode* this, struct v_dnode* name);
596 ext2_get_symlink(struct v_inode *this, const char **path_out);
599 ext2_set_symlink(struct v_inode *this, const char *target);
601 /* *********** Bitmap *********** */
604 ext2bmp_init(struct ext2_bmp* e_bmp, bbuf_t bmp_buf, unsigned int nr_bits);
607 ext2bmp_check_free(struct ext2_bmp* e_bmp);
610 ext2bmp_alloc_one(struct ext2_bmp* e_bmp);
613 ext2bmp_free_one(struct ext2_bmp* e_bmp, unsigned int pos);
616 ext2bmp_discard(struct ext2_bmp* e_bmp);
618 /* *********** Allocations *********** */
620 #define ALLOC_FAIL -1
623 valid_bmp_slot(int slot)
625 return slot != ALLOC_FAIL;
629 ext2gd_alloc_slot(struct ext2_gdesc* gd, int type_sel);
632 ext2gd_free_slot(struct ext2_gdesc* gd, int type_sel, int slot);
635 ext2gd_alloc_inode(struct ext2_gdesc* gd)
637 return ext2gd_alloc_slot(gd, GDESC_INO_SEL);
641 ext2gd_alloc_block(struct ext2_gdesc* gd)
643 return ext2gd_alloc_slot(gd, GDESC_BLK_SEL);
647 ext2gd_free_inode(struct ext2_gdesc* gd, int slot)
649 ext2gd_free_slot(gd, GDESC_INO_SEL, slot);
653 ext2gd_free_block(struct ext2_gdesc* gd, int slot)
655 ext2gd_free_slot(gd, GDESC_BLK_SEL, slot);
660 * @brief Allocate a free inode
663 * @param hint locality hint
668 ext2ino_alloc(struct v_superblock* vsb,
669 struct ext2_inode* hint, struct ext2_inode** out);
672 * @brief Allocate a free data block
674 * @param inode inode where the data block goes, also used as locality hint
678 ext2db_alloc(struct v_inode* inode, bbuf_t* out);
681 * @brief free an inode
684 * @param hint locality hint
689 ext2ino_free(struct v_inode* inode);
692 * @brief Free a data block
694 * @param inode inode where the data block goes, also used as locality hint
698 ext2db_free(struct v_inode* inode, bbuf_t buf);
701 ext2ino_alloc_slot(struct v_superblock* vsb, struct ext2_gdesc** gd_out);
704 ext2db_alloc_slot(struct v_superblock* vsb, struct ext2_gdesc** gd_out);
707 #endif /* __LUNAIX_EXT2_H */