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
33 #define IMODE_ACL_MASK 0x01ff
35 #define ext2_aligned compact align(4)
36 #define to_ext2ino_id(fsblock_id) ((fsblock_id) + 1)
37 #define to_fsblock_id(ext2_ino) ((ext2_ino) - 1)
39 extern bcache_zone_t gdesc_bcache_zone;
47 u32_t s_first_data_cnt;
67 u32_t s_checkinterval;
80 u32_t s_optional_feat;
81 u32_t s_required_feat;
97 u16_t bg_free_blk_cnt;
98 u16_t bg_free_ino_cnt;
99 u16_t bg_used_dir_cnt;
127 u32_t directs[12]; // directum
130 u32_t ind1; // prima indirecta
131 u32_t ind23[2]; // secunda et tertia indirecta
135 } ext2_aligned i_block;
136 u32_t i_block_arr[15];
158 #define EXT2_DRE(v_dnode) (fsapi_impl_data(v_dnode, struct ext2b_dirent))
161 #define GDESC_INO_SEL 0
162 #define GDESC_BLK_SEL 1
164 #define GDESC_FREE_LISTS \
167 struct llist_header free_grps_ino; \
168 struct llist_header free_grps_blk; \
170 struct llist_header free_list_sel[2]; \
173 #define check_gdesc_type_sel(sel) \
174 assert_msg(sel == GDESC_INO_SEL || sel == GDESC_BLK_SEL, \
181 * offset to inode table (in terms of blocks) within each block group.
182 * to account the difference of backup presence between rev 0/1
187 unsigned int block_size;
188 unsigned int nr_gdesc_pb;
189 unsigned int nr_gdesc;
190 unsigned int all_feature;
193 struct v_superblock* vsb;
195 struct ext2b_super* raw;
197 struct bcache gd_caches;
202 struct llist_header gds;
206 #define EXT2_SB(vsb) (fsapi_impl_data(vsb, struct ext2_sbinfo))
213 unsigned int nr_bytes;
219 struct llist_header groups;
224 struct ext2_bmp ino_bmp;
225 struct ext2_bmp blk_bmp;
227 struct ext2_bmp bmps[2];
231 unsigned int ino_base;
233 struct ext2b_gdesc* info;
234 struct ext2_sbinfo* sb;
240 Indriection Block Translation Look-aside Buffer
242 Provide a look-aside buffer for all last-level indirect block
243 that is at least two indirection away.
246 16 sets, 256 ways, capacity 4096 blocks
249 struct ext2_btlb_entry
258 struct ext2_btlb_entry buffer[BTLB_SETS];
261 struct ext2_fast_inode
263 struct ext2b_inode* ino;
269 bbuf_t buf; // partial inotab that holds this inode
270 unsigned int inds_lgents; // log2(# of block in an indirection level)
272 size_t indirect_blocks;
275 struct ext2b_inode* ino; // raw ext2 inode
276 struct ext2_btlb* btlb; // block-TLB
277 struct ext2_gdesc* blk_grp; // block group
283 dirent fragmentation degree, we will perform
284 full reconstruction on dirent table when this goes too high.
286 unsigned int dir_fragdeg;
290 // prefetched block for 1st order of indirection
294 #define EXT2_INO(v_inode) (fsapi_impl_data(v_inode, struct ext2_inode))
296 struct ext2_dnode_sub
299 struct ext2b_dirent* dirent;
304 struct ext2_dnode_sub self;
305 struct ext2_dnode_sub prev;
307 #define EXT2_DNO(v_dnode) (fsapi_impl_data(v_dnode, struct ext2_dnode))
311 * @brief General purpose iterator for ext2 objects
316 struct v_inode* inode;
319 struct ext2b_dirent* dirent;
338 struct ext2_iterator iter;
339 struct ext2_inode* b_ino;
341 #define EXT2_FILE(v_file) (fsapi_impl_data(v_file, struct ext2_file))
344 #define MAX_INDS_DEPTH 4
348 unsigned int tables[MAX_INDS_DEPTH];
349 unsigned int indices[MAX_INDS_DEPTH];
354 unsigned int* slot_ref;
359 struct walk_stack stack;
362 static inline unsigned int
363 ext2_datablock(struct v_superblock* vsb, unsigned int id)
365 return EXT2_SB(vsb)->raw->s_first_data_cnt + id;
369 ext2_feature(struct v_superblock* vsb, unsigned int feat)
371 return !!(EXT2_SB(vsb)->all_feature & feat);
374 /* ************ Inodes ************ */
377 ext2ino_init(struct v_superblock* vsb, struct v_inode* inode);
380 ext2ino_get(struct v_superblock* vsb,
381 unsigned int ino, struct ext2_inode** out);
384 ext2ino_get_fast(struct v_superblock* vsb,
385 unsigned int ino, struct ext2_fast_inode* fast_ino);
388 ext2ino_fill(struct v_inode* inode, ino_t ino_id);
391 ext2ino_make(struct v_superblock* vsb, unsigned int itype,
392 struct ext2_inode* hint, struct v_inode** out);
395 ext2ino_update(struct v_inode* inode);
398 ext2ino_resizing(struct v_inode* inode, size_t new_size);
401 ext2ino_linkto(struct ext2_inode* e_ino, struct ext2b_dirent* dirent)
403 dirent->inode = e_ino->ino_id;
404 e_ino->ino->i_lnk_cnt++;
405 fsblock_dirty(e_ino->buf);
409 ext2db_itbegin(struct ext2_iterator* iter, struct v_inode* inode);
412 ext2db_itend(struct ext2_iterator* iter);
415 ext2db_itnext(struct ext2_iterator* iter);
418 ext2db_itffw(struct ext2_iterator* iter, int count);
421 ext2db_itreset(struct ext2_iterator* iter);
425 * @brief Get the data block at given data pos associated with the
426 * inode, return NULL if not present
433 ext2db_get(struct v_inode* inode, unsigned int data_pos);
436 * @brief Get the data block at given data pos associated with the
437 * inode, allocate one if not present.
445 ext2db_acquire(struct v_inode* inode, unsigned int data_pos, bbuf_t* out);
448 ext2db_free_pos(struct v_inode* inode, unsigned int block_pos);
450 /* ************* Walker ************* */
453 ext2walk_init_state(struct walk_state* state)
455 *state = (struct walk_state) { };
459 ext2walk_free_state(struct walk_state* state)
461 fsblock_put(state->table);
464 /* ************* Iterator ************* */
467 ext2_iterror(struct ext2_iterator* iter) {
468 return iter->has_error;
472 ext2_itcheckbuf(struct ext2_iterator* iter) {
473 return !(iter->has_error = blkbuf_errbuf(iter->sel_buf));
476 #define itstate_sel(iter, value) \
477 (ext2_iterror(iter) ? EIO : (int)(value))
480 /* ************ Block Group ************ */
483 ext2gd_prepare_gdt(struct v_superblock* vsb);
486 ext2gd_release_gdt(struct v_superblock* vsb);
489 ext2gd_take(struct v_superblock* vsb,
490 unsigned int index, struct ext2_gdesc** out);
493 ext2gd_put(struct ext2_gdesc* gd) {
494 bcache_return(gd->cache_ref);
498 /* ************ Directory ************ */
501 ext2dr_lookup(struct v_inode* this, struct v_dnode* dnode);
504 ext2dr_read(struct v_file *file, struct dir_context *dctx);
507 ext2dr_itbegin(struct ext2_iterator* iter, struct v_inode* inode);
510 ext2dr_itend(struct ext2_iterator* iter);
513 ext2dr_itdrain(struct ext2_iterator* iter)
515 return iter->pos > iter->end_pos;
519 ext2dr_itnext(struct ext2_iterator* iter);
522 ext2dr_itffw(struct ext2_iterator* iter, int count);
525 ext2dr_itreset(struct ext2_iterator* iter);
528 ext2dr_open(struct v_inode* this, struct v_file* file);
531 ext2dr_close(struct v_inode* this, struct v_file* file);
534 ext2dr_seek(struct v_file* file, size_t offset);
537 ext2dr_insert(struct v_inode* this, struct ext2b_dirent* dirent,
538 struct ext2_dnode** e_dno_out);
541 ext2dr_remove(struct ext2_dnode* e_dno);
544 ext2_rmdir(struct v_inode* parent, struct v_dnode* dnode);
547 ext2_mkdir(struct v_inode* parent, struct v_dnode* dnode);
550 ext2_rename(struct v_inode* from_inode, struct v_dnode* from_dnode,
551 struct v_dnode* to_dnode);
554 ext2dr_setup_dirent(struct ext2b_dirent* dirent,
555 struct v_inode* inode, struct hstr* name);
558 /* ************ Files ************ */
561 ext2_open_inode(struct v_inode* this, struct v_file* file);
564 ext2_close_inode(struct v_file* file);
567 ext2_sync_inode(struct v_inode* inode);
570 ext2_file_sync(struct v_file* file);
573 ext2_inode_read(struct v_inode *inode, void *buffer, size_t len, size_t fpos);
576 ext2_inode_read_page(struct v_inode *inode, void *buffer, size_t fpos);
579 ext2_inode_write(struct v_inode *inode, void *buffer, size_t len, size_t fpos);
582 ext2_inode_write_page(struct v_inode *inode, void *buffer, size_t fpos);
585 ext2_seek_inode(struct v_file* file, size_t offset);
588 ext2_create(struct v_inode* this, struct v_dnode* dnode, unsigned int itype);
591 ext2_link(struct v_inode* this, struct v_dnode* new_name);
594 ext2_unlink(struct v_inode* this, struct v_dnode* name);
597 ext2_get_symlink(struct v_inode *this, const char **path_out);
600 ext2_set_symlink(struct v_inode *this, const char *target);
602 /* *********** Bitmap *********** */
605 ext2bmp_init(struct ext2_bmp* e_bmp, bbuf_t bmp_buf, unsigned int nr_bits);
608 ext2bmp_check_free(struct ext2_bmp* e_bmp);
611 ext2bmp_alloc_one(struct ext2_bmp* e_bmp);
614 ext2bmp_free_one(struct ext2_bmp* e_bmp, unsigned int pos);
617 ext2bmp_discard(struct ext2_bmp* e_bmp);
619 /* *********** Allocations *********** */
621 #define ALLOC_FAIL -1
624 valid_bmp_slot(int slot)
626 return slot != ALLOC_FAIL;
630 ext2gd_alloc_slot(struct ext2_gdesc* gd, int type_sel);
633 ext2gd_free_slot(struct ext2_gdesc* gd, int type_sel, int slot);
636 ext2gd_alloc_inode(struct ext2_gdesc* gd)
638 return ext2gd_alloc_slot(gd, GDESC_INO_SEL);
642 ext2gd_alloc_block(struct ext2_gdesc* gd)
644 return ext2gd_alloc_slot(gd, GDESC_BLK_SEL);
648 ext2gd_free_inode(struct ext2_gdesc* gd, int slot)
650 ext2gd_free_slot(gd, GDESC_INO_SEL, slot);
654 ext2gd_free_block(struct ext2_gdesc* gd, int slot)
656 ext2gd_free_slot(gd, GDESC_BLK_SEL, slot);
661 * @brief Allocate a free inode
664 * @param hint locality hint
669 ext2ino_alloc(struct v_superblock* vsb,
670 struct ext2_inode* hint, struct ext2_inode** out);
673 * @brief Allocate a free data block
675 * @param inode inode where the data block goes, also used as locality hint
679 ext2db_alloc(struct v_inode* inode, bbuf_t* out);
682 * @brief free an inode
685 * @param hint locality hint
690 ext2ino_free(struct v_inode* inode);
693 * @brief Free a data block
695 * @param inode inode where the data block goes, also used as locality hint
699 ext2db_free(struct v_inode* inode, bbuf_t buf);
702 ext2ino_alloc_slot(struct v_superblock* vsb, struct ext2_gdesc** gd_out);
705 ext2db_alloc_slot(struct v_superblock* vsb, struct ext2_gdesc** gd_out);
708 #endif /* __LUNAIX_EXT2_H */