ref: 28414e1e7ea575d0d0e7ca747661875e3d954711
parent: ed195283a7de1e8d0cfdf8fcc5aeb73a253f6ca7
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Nov 15 14:20:00 EST 2020
add mkfs run
--- a/common.h
+++ b/common.h
@@ -11,6 +11,12 @@
int cachewb;
int linkmode;
int asroot;
+
+ int fstype;
+ int blksz;
+ int inodesz;
+ u32int ninode;
+ char *label;
};
struct Part {
--- a/ext4srv.c
+++ b/ext4srv.c
@@ -36,6 +36,12 @@
.cachewb = 0,
.asroot = 0,
.linkmode = Lhide,
+
+ .fstype = -1,
+ .blksz = 0,
+ .label = nil,
+ .inodesz = 256,
+ .ninode = 0,
};
static u8int zero[65536];
static char Eperm[] = "permission denied";
@@ -301,7 +307,7 @@
/* first make sure this is a directory */
t = ext4_inode_type(a->p->sb, &inode);
- if((t & EXT4_INODE_MODE_DIRECTORY) == 0){
+ if(t != EXT4_INODE_MODE_DIRECTORY){
err = "create in non-directory";
goto error;
}
@@ -556,13 +562,12 @@
if(a->uid == Root || ((g = findgroupid(&a->p->groups, ext4_inode_get_uid(&inode))) != nil && g->id == a->uid)){
t = ext4_inode_type(a->p->sb, &inode);
- if((t & EXT4_INODE_MODE_DIRECTORY) != 0 && ext4_dir_open(&dir, s) == 0){
- do{
- e = ext4_dir_entry_next(&dir);
- empty = e == nil;
- if(empty)
+ if(t == EXT4_INODE_MODE_DIRECTORY && ext4_dir_open(&dir, s) == 0){
+ for(empty = 1; empty;){
+ if((e = ext4_dir_entry_next(&dir)) == nil)
break;
- }while(strcmp((char*)e->name, ".") == 0 || strcmp((char*)e->name, "..") == 0);
+ empty = e->name == nil || strcmp((char*)e->name, ".") == 0 || strcmp((char*)e->name, "..") == 0;
+ }
ext4_dir_close(&dir);
if(!empty)
err = "directory not empty";
@@ -627,7 +632,7 @@
isowner = a->uid == Root || a->uid == uid;
/* permission to truncate */
- isdir = ext4_inode_type(a->p->sb, &inode) & EXT4_INODE_MODE_DIRECTORY;
+ isdir = ext4_inode_type(a->p->sb, &inode) == EXT4_INODE_MODE_DIRECTORY;
if(r->d.length >= 0 && (!wrperm || isdir || !ext4_inode_can_truncate(a->p->sb, &inode))){
err = Eperm;
goto error;
@@ -747,7 +752,7 @@
if((r = ext4_raw_inode_fill(s, &ino, &inode)) != 0)
goto error;
t = ext4_inode_type(a->p->sb, &inode);
- if((t & EXT4_INODE_MODE_DIRECTORY) == 0){
+ if(t != EXT4_INODE_MODE_DIRECTORY){
free(s);
return "not a directory";
}
@@ -768,11 +773,15 @@
}
if((r = ext4_raw_inode_fill(s, &ino, &inode)) != 0)
goto error;
+ t = ext4_inode_type(a->p->sb, &inode);
+ if(opts.linkmode == Lhide && t == EXT4_INODE_MODE_SOFTLINK){
+ r = ENOENT;
+ goto error;
+ }
qid->type = 0;
qid->path = a->p->qidmask.path | ino;
qid->vers = ext4_inode_get_generation(&inode);
- t = ext4_inode_type(a->p->sb, &inode);
- if(t & EXT4_INODE_MODE_DIRECTORY){
+ if(t == EXT4_INODE_MODE_DIRECTORY){
qid->type |= QTDIR;
a->type = Adir;
}else
@@ -878,6 +887,7 @@
usage(void)
{
fprint(2, "usage: %s [-C] [-R] [-g GROUPFILE] [-l resolve] [-s SRVNAME]\n", argv0);
+ fprint(2, "mkfs: %s -M (2|3|4) [-L LABEL] [-b BLKSIZE] [-N NUMINODES] [-I INODESZ] DEVICE\n", argv0);
threadexitsall("usage");
}
@@ -886,6 +896,8 @@
{
if(strcmp(m, "resolve") == 0)
return Lresolve;
+ if(strcmp(m, "hide") == 0)
+ return Lhide;
usage();
return -1;
@@ -902,16 +914,20 @@
ARGBEGIN{
case 'D':
chatty9p++;
+nomkfs:
+ if(opts.fstype > 0)
+ usage();
+ opts.fstype = 0;
break;
case 'C':
opts.cachewb = 1;
- break;
+ goto nomkfs;
case 'l':
opts.linkmode = linkmode(EARGF(usage()));
- break;
+ goto nomkfs;
case 's':
srv = EARGF(usage());
- break;
+ goto nomkfs;
case 'g':
gr = EARGF(usage());
if((f = open(gr, OREAD)) < 0)
@@ -925,15 +941,57 @@
sysfatal("%s: read failed", gr);
close(f);
opts.group[sz] = 0;
- break;
+ goto nomkfs;
case 'R':
opts.asroot = 1;
+ goto nomkfs;
+
+ case 'M':
+ if(!opts.fstype)
+ usage();
+ opts.fstype = atoi(EARGF(usage()));
+ if(opts.fstype < 2 || opts.fstype > 4)
+ usage();
break;
- }ARGEND
- if(argc != 0)
+ case 'b':
+ opts.blksz = atoi(EARGF(usage()));
+ if(opts.blksz != 1024 && opts.blksz != 2048 && opts.blksz != 4096)
+ usage();
+yesmkfs:
+ if(opts.fstype < 1)
+ usage();
+ break;
+ case 'L':
+ opts.label = EARGF(usage());
+ goto yesmkfs;
+ case 'I':
+ opts.inodesz = atoi(EARGF(usage()));
+ if(opts.inodesz < 128 || ((opts.inodesz-1) & opts.inodesz) != 0)
+ usage();
+ goto yesmkfs;
+ case 'N':
+ opts.ninode = atoi(EARGF(usage()));
+ if(opts.ninode < 1)
+ usage();
+ goto yesmkfs;
+
+ default:
usage();
+ }ARGEND
- threadpostmountsrv(&fs, srv, nil, 0);
- threadexits(nil);
+ if(opts.fstype > 1){
+ if(argc != 1)
+ usage();
+ if(openpart(argv[0], &opts) == nil)
+ sysfatal("%r");
+ closeallparts();
+ threadexitsall(nil);
+ }else{
+ if(argc != 0)
+ usage();
+
+ threadpostmountsrv(&fs, srv, nil, 0);
+ threadexits(nil);
+ }
}
--- a/part.c
+++ b/part.c
@@ -1,4 +1,5 @@
#include <ext4.h>
+#include <ext4_mkfs.h>
#include <thread.h>
#include "group.h"
#include "common.h"
@@ -275,11 +276,13 @@
Part *
openpart(char *dev, Opts *opts)
{
+ struct ext4_mkfs_info info;
+ struct ext4_fs fs;
u32int blksz;
+ int f, r;
Part *p;
char *s;
Dir *d;
- int f;
d = nil;
p = nil;
@@ -312,6 +315,20 @@
p->bdif.ph_bcnt = d->length/blksz;
p->bdif.ph_bbuf = p->blkbuf;
p->bdif.p_user = p;
+
+ if(opts->fstype > 1){
+ memset(&fs, 0, sizeof(fs));
+ memset(&info, 0, sizeof(info));
+ info.block_size = opts->blksz;
+ info.label = opts->label;
+ info.inode_size = opts->inodesz;
+ info.inodes = opts->ninode;
+ info.journal = true;
+ if((r = ext4_mkfs(&fs, &p->bdev, &info, opts->fstype)) != 0){
+ werrstr("mkfs: %s", errno2s(r));
+ goto error;
+ }
+ }
if(mountpart(p, opts) != 0)
goto error;