Home | History | Annotate | Download | only in sysvbfs

Lines Matching defs:bfs

1 /*	$NetBSD: bfs.c,v 1.19 2019/11/10 21:16:38 chs Exp $	*/
34 __KERNEL_RCSID(0, "$NetBSD: bfs.c,v 1.19 2019/11/10 21:16:38 chs Exp $");
59 #include <fs/sysvbfs/bfs.h>
72 STATIC int bfs_init_superblock(struct bfs *, int, size_t *);
73 STATIC int bfs_init_inode(struct bfs *, uint8_t *, size_t *);
74 STATIC int bfs_init_dirent(struct bfs *, uint8_t *);
78 STATIC bool bfs_writeback_dirent(const struct bfs *, struct bfs_dirent *,
80 STATIC bool bfs_writeback_inode(const struct bfs *, struct bfs_inode *);
83 bfs_init2(struct bfs **bfsp, int bfs_sector, struct sector_io_ops *io,
86 struct bfs *bfs;
92 DPRINTF(debug, "bfs sector = %d\n", bfs_sector);
93 if ((bfs = (void *)__MALLOC(sizeof(struct bfs), M_BFS, M_WAITOK)) == 0)
95 memset(bfs, 0, sizeof *bfs);
96 bfs->io = io;
97 bfs->debug = debug;
100 if ((err = bfs_init_superblock(bfs, bfs_sector, &memsize)) != 0) {
101 bfs_fini(bfs);
104 DPRINTF(debug, "bfs super block + inode area = %zd\n", memsize);
105 bfs->super_block_size = memsize;
107 bfs_fini(bfs);
111 if ((err = bfs_init_inode(bfs, p, &memsize)) != 0) {
112 bfs_fini(bfs);
115 DPRINTF(debug, "bfs dirent area = %zd\n", memsize);
116 bfs->dirent_size = memsize;
118 bfs_fini(bfs);
122 if ((err = bfs_init_dirent(bfs, p)) != 0) {
123 bfs_fini(bfs);
128 bfs_dump(bfs);
130 *bfsp = bfs;
136 bfs_fini(struct bfs *bfs)
139 if (bfs == 0)
141 if (bfs->super_block)
142 __FREE(bfs->super_block, bfs->super_block_size, M_BFS);
143 if (bfs->dirent)
144 __FREE(bfs->dirent, bfs->dirent_size, M_BFS);
145 __FREE(bfs, sizeof(struct bfs), M_BFS);
149 bfs_init_superblock(struct bfs *bfs, int bfs_sector, size_t *required_memory)
153 bfs->start_sector = bfs_sector;
156 if (!bfs->io->read(bfs->io, (uint8_t *)&super, bfs_sector))
163 bfs->data_start = super.header.data_start_byte;
164 bfs->data_end = super.header.data_end_byte;
166 bfs->max_inode = (bfs->data_start - sizeof(struct bfs_super_block)) /
169 *required_memory = ROUND_SECTOR(bfs->data_start);
175 bfs_init_inode(struct bfs *bfs, uint8_t *p, size_t *required_memory)
180 if (!bfs->io->read_n(bfs->io, p, bfs->start_sector,
181 bfs->data_start >> DEV_BSHIFT))
184 bfs->super_block = (struct bfs_super_block *)p;
185 bfs->inode = (struct bfs_inode *)(p + sizeof(struct bfs_super_block));
186 p += bfs->data_start;
188 bfs->n_inode = 0;
189 inode = bfs->inode;
191 for (i = 0; i < bfs->max_inode; i++, inode++) {
193 bfs->n_inode++;
198 DPRINTF(bfs->debug, "inode: %d/%d\n", bfs->n_inode, bfs->max_inode);
201 DPRINTF(bfs->debug, "no root directory.\n");
205 DPRINTF(bfs->debug, "root inode: %d-%d\n", root_inode->start_sector,
207 bfs->root_inode = root_inode;
216 bfs_init_dirent(struct bfs *bfs, uint8_t *p)
219 struct bfs_inode *inode = bfs->root_inode;
224 if (!bfs->io->read_n(bfs->io, p,
225 bfs->start_sector + inode->start_sector, n))
228 bfs->dirent = (struct bfs_dirent *)p;
229 bfs->max_dirent = (n << DEV_BSHIFT) / sizeof(struct bfs_dirent);
231 file = bfs->dirent;
232 bfs->n_dirent = 0;
233 for (i = 0; i < bfs->max_dirent; i++, file++)
235 bfs->n_dirent++;
237 DPRINTF(bfs->debug, "dirent: %d/%d\n", bfs->n_dirent, bfs->max_dirent);
243 bfs_file_read(const struct bfs *bfs, const char *fname, void *buf, size_t bufsz,
251 if (!bfs_file_lookup(bfs, fname, &start, &end, &sz))
259 if (!bfs->io->read_n(bfs->io, p, start, n))
263 if (!bfs->io->read(bfs->io, tmpbuf, end))
274 bfs_file_write(struct bfs *bfs, const char *fname, void *buf,
284 if (bfs_dirent_lookup_by_name(bfs, name, &dirent)) {
286 if (!bfs_inode_lookup(bfs, dirent->inode, &inode)) {
287 DPRINTF(bfs->debug, "%s: dirent found, but inode "
293 bfs_file_delete(bfs, name, false);
294 if ((err = bfs_file_create(bfs, name, buf, bufsz, &attr)) != 0)
303 if ((err = bfs_file_create(bfs, name, buf, bufsz, &attr)) != 0)
311 bfs_file_delete(struct bfs *bfs, const char *fname, bool keep_inode)
316 if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
319 if (!keep_inode && !bfs_inode_lookup(bfs, dirent->inode, &inode))
323 bfs->n_dirent--;
324 bfs_writeback_dirent(bfs, dirent, false);
328 bfs->n_inode--;
329 bfs_writeback_inode(bfs, inode);
331 DPRINTF(bfs->debug, "%s: \"%s\" deleted.\n", __func__, fname);
337 bfs_file_rename(struct bfs *bfs, const char *from_name, const char *to_name)
342 if (!bfs_dirent_lookup_by_name(bfs, from_name, &dirent)) {
348 bfs_writeback_dirent(bfs, dirent, false);
351 DPRINTF(bfs->debug, "%s: \"%s\" -> \"%s\" error=%d.\n", __func__,
358 bfs_file_create(struct bfs *bfs, const char *fname, void *buf, size_t bufsz,
368 if ((err = bfs_inode_alloc(bfs, &inode, &j, &start)) != 0)
376 if ((start + n) * DEV_BSIZE >= bfs->data_end) {
377 DPRINTF(bfs->debug, "disk full.\n");
382 for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
385 if (i == bfs->max_dirent) {
386 DPRINTF(bfs->debug, "dirent full.\n");
400 bfs_inode_set_attr(bfs, inode, attr);
407 DPRINTF(bfs->debug, "%s: start %d end %d\n", __func__,
415 if (!bfs->io->write(bfs->io, p, bfs->start_sector + i))
423 if (!bfs->io->write(bfs->io, tmpbuf, bfs->start_sector + i))
427 bfs->n_inode++;
428 bfs->n_dirent++;
429 bfs_writeback_dirent(bfs, file, true);
430 bfs_writeback_inode(bfs, inode);
436 bfs_writeback_dirent(const struct bfs *bfs, struct bfs_dirent *dir,
439 struct bfs_dirent *dir_base = bfs->dirent;
440 struct bfs_inode *root_inode = bfs->root_inode;
464 bfs_writeback_inode(bfs, root_inode);
467 return bfs->io->write(bfs->io, (uint8_t *)dir_base + (i << DEV_BSHIFT),
468 bfs->start_sector + bfs->root_inode->start_sector + i);
472 bfs_writeback_inode(const struct bfs *bfs, struct bfs_inode *inode)
474 struct bfs_inode *inode_base = bfs->inode;
479 return bfs->io->write(bfs->io,
481 bfs->start_sector + 1/*super block*/ + i);
485 bfs_file_lookup(const struct bfs *bfs, const char *fname, int *start, int *end,
491 if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
493 if (!bfs_inode_lookup(bfs, dirent->inode, &inode))
497 *start = inode->start_sector + bfs->start_sector;
499 *end = inode->end_sector + bfs->start_sector;
503 DPRINTF(bfs->debug, "%s: %d + %d -> %d (%zd)\n",
504 fname, bfs->start_sector, inode->start_sector,
511 bfs_dirent_lookup_by_inode(const struct bfs *bfs, int inode,
517 for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
521 if (i == bfs->max_dirent)
530 bfs_dirent_lookup_by_name(const struct bfs *bfs, const char *fname,
536 for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
541 if (i == bfs->max_dirent)
550 bfs_inode_lookup(const struct bfs *bfs, ino_t n, struct bfs_inode **iinode)
555 for (inode = bfs->inode, i = 0; i < bfs->max_inode; i++, inode++)
559 if (i == bfs->max_inode)
568 bfs_inode_delete(struct bfs *bfs, ino_t ino)
572 if (!bfs_inode_lookup(bfs, ino, &inode))
576 bfs->n_inode--;
578 bfs_writeback_inode(bfs, inode);
579 DPRINTF(bfs->debug, "%s: %lld deleted.\n", __func__, (long long)ino);
593 bfs_inode_alloc(const struct bfs *bfs, struct bfs_inode **free_inode,
600 inode = bfs->inode;
603 for (i = BFS_ROOT_INODE; i < bfs->max_inode; i++, inode++) {
623 DPRINTF(bfs->debug, "i-node full.\n");
627 if (start * DEV_BSIZE >= bfs->data_end) {
628 DPRINTF(bfs->debug, "data block full.\n");
643 bfs_inode_set_attr(const struct bfs *bfs, struct bfs_inode *inode,
662 bfs_writeback_inode(bfs, inode);
673 bfs_dump(const struct bfs *bfs)
682 if (!bfs_superblock_valid(bfs->super_block)) {
683 DPRINTF(bfs->debug, "invalid bfs super block.\n");
686 h = &bfs->super_block->header;
687 compaction = &bfs->super_block->compaction;
689 DPRINTF(bfs
690 sizeof *bfs->super_block, sizeof *inode, sizeof *file);
692 DPRINTF(bfs->debug, "magic=%x\n", h->magic);
693 DPRINTF(bfs->debug, "data_start_byte=0x%x\n", h->data_start_byte);
694 DPRINTF(bfs->debug, "data_end_byte=0x%x\n", h->data_end_byte);
695 DPRINTF(bfs->debug, "from=%#x\n", compaction->from);
696 DPRINTF(bfs->debug, "to=%#x\n", compaction->to);
697 DPRINTF(bfs->debug, "from_backup=%#x\n", compaction->from_backup);
698 DPRINTF(bfs->debug, "to_backup=%#x\n", compaction->to_backup);
699 DPRINTF(bfs->debug, "fsname=%s\n", bfs->super_block->fsname);
700 DPRINTF(bfs->debug, "volume=%s\n", bfs->super_block->volume);
703 DPRINTF(bfs->debug, "[inode index list]\n");
704 for (inode = bfs->inode, i = j = 0; i < bfs->max_inode; inode++, i++) {
707 DPRINTF(bfs->debug, "%3d %8d %8d %8d (%d) ",
714 DPRINTF(bfs->debug, "%d %d %d %d %d %08x %08x %08x\n",
720 if (j != bfs->n_inode) {
721 DPRINTF(bfs->debug, "inconsistent cached data. (i-node)\n");
724 DPRINTF(bfs->debug, "total %d i-node.\n", j);
727 DPRINTF(bfs->debug, "[dirent index list]\n");
728 DPRINTF(bfs->debug, "%d file entries.\n", bfs->max_dirent);
729 file = bfs->dirent;
730 for (i = j = 0; i < bfs->max_dirent; i++, file++) {
732 if (bfs_file_lookup(bfs, file->name, &s, &e, &bytes))
733 DPRINTF(bfs->debug, "%3d %14s %8d %8d %8zd\n",
738 if (j != bfs->n_dirent) {
739 DPRINTF(bfs->debug, "inconsistent cached data. (dirent)\n");
742 DPRINTF(bfs->debug, "%d files.\n", j);