diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 8a752925d0a5..520b41c8b88f 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1,1301 +1,1303 @@ # # Please see the file src/etc/mtree/README before making changes to this file. # /set type=dir uname=root gname=wheel mode=0755 tags=package=tests . atf_python sys net .. netlink .. netpfil ipfw .. .. .. .. bin cat .. chflags .. chmod .. cp .. date .. dd .. echo .. expr .. hostname .. ln .. ls .. mkdir .. mv .. pax .. pkill .. pwait .. rm .. rmdir .. sh builtins .. errors .. execution .. expansion .. invocation .. parameters .. parser .. set-e .. .. sleep .. test .. timeout .. .. cddl lib libtpool .. .. sbin .. usr.bin ctfconvert .. ztest .. .. usr.sbin dtrace amd64 arrays .. .. common aggs .. arithmetic .. arrays .. assocs .. begin .. bitfields .. buffering .. builtinvar .. cg .. clauses .. cpc .. decls .. drops .. dtraceUtil .. end .. enum .. env .. error .. exit .. fbtprovider .. funcs .. grammar .. include .. inline .. io .. ip .. java_api .. json .. kinst .. lexer .. llquantize .. mdb .. mib .. misc .. multiaggs .. offsetof .. oformat .. operators .. pid .. plockstat .. pointers .. pragma .. predicates .. preprocessor .. print .. printa .. printf .. privs .. probes .. proc .. profile-n .. providers .. raise .. rates .. safety .. scalars .. sched .. scripting .. sdt .. sizeof .. speculation .. stability .. stack .. stackdepth .. stop .. strlen .. strtoll .. struct .. sugar .. syscall .. sysevent .. tick-n .. trace .. tracemem .. translators .. typedef .. types .. uctf .. union .. usdt .. ustack .. vars .. version .. .. i386 arrays .. funcs .. pid .. ustack .. .. .. zfsd .. .. .. etc rc.d .. .. examples .. games .. gnu lib .. usr.bin diff .. .. .. include .. lib atf libatf-c detail .. .. libatf-c++ detail .. .. test-programs .. .. csu dynamic .. dynamiclib .. dynamicpie .. errno .. static .. .. googletest gmock .. gmock_main .. gtest .. gtest_main .. .. libarchive .. libbe .. libbsnmp .. libc c063 .. db .. gen execve .. posix_spawn .. .. hash data .. .. iconv .. inet .. locale .. net getaddrinfo data .. .. .. nss .. regex data .. .. resolv .. rpc .. secure .. setjmp .. ssp .. stdio .. stdlib .. stdtime .. string .. sys .. termios .. time .. tls dso .. .. ttyio .. .. libcam .. libcasper services cap_dns .. cap_fileargs .. cap_grp .. cap_net .. cap_netdb .. cap_pwd .. cap_sysctl .. .. .. libcrypt .. libdevdctl .. libdiff .. libexecinfo .. libkvm .. libmd .. libmp .. libnv .. libproc .. libregex data .. .. librt .. libsbuf .. libsysdecode .. libthr dlopen .. .. libutil .. libxo .. msun .. .. libexec atf atf-check .. atf-pytest-wrapper .. atf-sh .. .. nuageinit .. rc .. rtld-elf rtld_deepbind .. .. tftpd .. .. sbin bectl .. devd .. dhclient .. growfs .. ifconfig .. ipfw .. md5 .. mdconfig .. newfs_msdos .. nvmecontrol .. pfctl files .. .. ping .. route .. savecore .. swapon .. sysctl .. .. secure lib libcrypto .. .. libexec .. usr.bin .. usr.sbin .. .. share examples tests atf .. googletest .. plain .. tap .. .. .. zoneinfo .. .. sys acl .. aio .. audit .. auditpipe .. cam ctl .. .. capsicum .. cddl zfs bin .. include .. tests acl cifs .. nontrivial .. trivial .. .. atime .. bootfs .. cache .. cachefile .. clean_mirror .. cli_root zdb .. zfs .. zfs_clone .. zfs_copies .. zfs_create .. zfs_destroy .. zfs_diff .. zfs_get .. zfs_inherit .. zfs_mount .. zfs_promote .. zfs_property .. zfs_receive .. zfs_rename .. zfs_reservation .. zfs_rollback .. zfs_send .. zfs_set .. zfs_share .. zfs_snapshot .. zfs_unmount .. zfs_unshare .. zfs_upgrade .. zpool .. zpool_add .. zpool_attach .. zpool_clear .. zpool_create .. zpool_destroy .. zpool_detach .. zpool_expand .. zpool_export .. zpool_get .. zpool_history .. zpool_import blockfiles .. .. zpool_offline .. zpool_online .. zpool_remove .. zpool_replace .. zpool_scrub .. zpool_set .. zpool_status .. zpool_upgrade blockfiles .. .. .. cli_user misc .. zfs_list .. zpool_iostat .. zpool_list .. .. compression .. ctime .. delegate .. devices .. exec .. grow_pool .. grow_replicas .. history .. hotplug .. hotspare .. inheritance .. interop .. inuse .. iscsi .. large_files .. largest_pool .. link_count .. migration .. mmap .. mount .. mv_files .. nestedfs .. no_space .. online_offline .. pool_names .. poolversion .. quota .. redundancy .. refquota .. refreserv .. rename_dirs .. replacement .. reservation .. rootpool .. rsend .. scrub_mirror .. slog .. snapshot .. snapused .. sparse .. threadsappend .. truncate .. txg_integrity .. userquota .. utils_test .. write_dirs .. xattr .. zfsd .. zil .. zinject .. zones .. zvol zvol_ENOSPC .. zvol_cli .. zvol_misc .. zvol_swap .. .. zvol_thrash .. .. .. .. common .. compat32 .. devrandom .. dtrace .. fifo .. file .. fs fusefs .. tarfs .. tmpfs .. .. geom class concat .. eli .. gate .. gpt .. mirror .. multipath .. nop .. part .. raid3 .. shsec .. stripe .. union .. uzip etalon .. .. virstor .. .. .. kern acct .. execve .. pipe .. tty .. .. kqueue libkqueue .. .. mac bsdextended .. ipacl .. portacl .. .. mqueue .. net bpf .. if_ovpn ccd .. .. routing .. .. netgraph .. netinet libalias .. .. netinet6 frag6 .. .. netipsec tunnel .. .. netlink .. netmap .. netpfil common .. ipfw .. pf ioctl .. .. .. opencrypto .. pjdfstest chflags .. chmod .. chown .. ftruncate .. granular .. link .. mkdir .. mkfifo .. mknod .. open .. rename .. rmdir .. symlink .. truncate .. unlink .. utimensat .. .. posixshm .. ses .. sound .. sys .. vfs .. vm stack .. .. vmm .. .. usr.bin apply .. asa .. awk bugs-fixed .. netbsd .. .. basename .. bintrans .. bmake archives fmt_44bsd .. fmt_44bsd_mod .. fmt_oldbsd .. .. basic t0 .. t1 .. t2 .. t3 .. .. execution ellipsis .. empty .. joberr .. plus .. .. shell builtin .. meta .. path .. path_select .. replace .. select .. .. suffixes basic .. src_wild1 .. src_wild2 .. .. syntax directive-t0 .. enl .. funny-targets .. semi .. .. sysmk t0 2 1 .. .. mk .. .. t1 2 1 .. .. mk .. .. t2 2 1 .. .. mk .. .. .. variables modifier_M .. modifier_t .. opt_V .. t0 .. .. .. bsdcat .. calendar .. cmp .. col .. column .. comm .. compress .. cpio .. csplit .. cut .. diff .. diff3 .. dirname .. du .. env .. factor .. file .. file2c .. find .. fold .. getconf .. gh-bc scripts .. tests bc errors .. scripts .. .. dc errors .. scripts .. .. .. .. grep .. gzip .. head .. hexdump .. ident .. indent .. join .. jot .. lastcomm .. limits .. locale .. lockf .. lorder .. m4 .. mail .. mkimg .. mktemp .. ncal .. opensm .. patch .. pr .. printenv .. printf .. procstat .. renice .. rs .. sdiff .. sed regress.multitest.out .. .. seq .. sockstat .. soelim .. sort .. split .. stat .. tail .. tar .. tee .. tftp .. touch .. tr .. truncate .. tsort .. unifdef .. uniq .. units .. unzip .. vmstat .. wc .. xargs .. xinstall .. xo .. yacc yacc .. .. .. usr.sbin certctl .. chown .. ctladm .. daemon .. etcupdate .. extattr .. fstyp .. jail .. makefs .. mixer .. newsyslog .. nmtree .. praudit .. pw .. + quot + .. rpcbind .. sa .. syslogd .. sysrc .. traceroute .. .. .. # vim: set expandtab ts=4 sw=4: diff --git a/usr.sbin/quot/Makefile b/usr.sbin/quot/Makefile index 34ebcb1009c8..2f32c8f2df8b 100644 --- a/usr.sbin/quot/Makefile +++ b/usr.sbin/quot/Makefile @@ -1,5 +1,9 @@ +.include + PROG= quot MAN= quot.8 -LIBADD= ufs +LIBADD= ufs util +HAS_TESTS= +SUBDIR.${MK_TESTS}= tests .include diff --git a/usr.sbin/quot/quot.c b/usr.sbin/quot/quot.c index 323648d8d550..879580f649b9 100644 --- a/usr.sbin/quot/quot.c +++ b/usr.sbin/quot/quot.c @@ -1,523 +1,530 @@ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright (C) 1991, 1994 Wolfgang Solfrank. * Copyright (C) 1991, 1994 TooLs GmbH. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by TooLs GmbH. * 4. The name of TooLs GmbH may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #include #include /* some flags of what to do: */ static bool all; static bool count; +static bool noname; static bool unused; static void (*func)(int, struct fs *); static long blocksize; static char *header; static int headerlen; static union dinode *get_inode(int, struct fs *, ino_t); static int isfree(struct fs *, union dinode *); static void inituser(void); static void usrrehash(void); static struct user *user(uid_t); static int cmpusers(const void *, const void *); static void uses(uid_t, daddr_t, time_t); static void initfsizes(void); static void dofsizes(int, struct fs *); static void douser(int, struct fs *); static void donames(int, struct fs *); static void usage(void); static void quot(char *, char *); /* * Original BSD quot doesn't round to number of frags/blocks, * doesn't account for indirection blocks and gets it totally * wrong if the size is a multiple of the blocksize. * The new code always counts the number of 512 byte blocks * instead of the number of kilobytes and converts them to * kByte when done (on request). * * Due to the size of modern disks, we must cast intermediate * values to 64 bits to prevent potential overflows. */ #define SIZE(n) ((int)(((intmax_t)(n) * 512 + blocksize - 1) / blocksize)) #define INOCNT(fs) ((fs)->fs_ipg) #define INOSZ(fs) \ (((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \ sizeof(struct ufs2_dinode)) * INOCNT(fs)) #define DIP(fs, dp, field) \ (((fs)->fs_magic == FS_UFS1_MAGIC) ? \ (dp)->dp1.field : (dp)->dp2.field) static union dinode * get_inode(int fd, struct fs *super, ino_t ino) { static union dinode *ipbuf; static struct cg *cgp; static ino_t last; static unsigned long cg; struct ufs2_dinode *di2; off_t off; if (fd < 0) { /* flush cache */ free(ipbuf); ipbuf = NULL; free(cgp); cgp = NULL; return (NULL); } if (ipbuf == NULL || ino < last || ino >= last + INOCNT(super)) { if (super->fs_magic == FS_UFS2_MAGIC && (cgp == NULL || cg != ino_to_cg(super, ino))) { cg = ino_to_cg(super, ino); if (cgp == NULL && (cgp = malloc(super->fs_cgsize)) == NULL) errx(1, "allocate cg"); if (lseek(fd, (off_t)cgtod(super, cg) << super->fs_fshift, 0) < 0) err(1, "lseek cg"); if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize) err(1, "read cg"); if (!cg_chkmagic(cgp)) errx(1, "cg has bad magic"); } if (ipbuf == NULL && (ipbuf = malloc(INOSZ(super))) == NULL) errx(1, "allocate inodes"); last = rounddown(ino, INOCNT(super)); off = (off_t)ino_to_fsba(super, last) << super->fs_fshift; if (lseek(fd, off, SEEK_SET) != off || read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super)) err(1, "read inodes"); } if (super->fs_magic == FS_UFS1_MAGIC) return ((union dinode *) &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]); di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)]; /* If the inode is unused, it might be unallocated too, so zero it. */ if (isclr(cg_inosused(cgp), ino % super->fs_ipg)) memset(di2, 0, sizeof(*di2)); return ((union dinode *)di2); } static int isfree(struct fs *super, union dinode *dp) { switch (DIP(super, dp, di_mode) & IFMT) { case IFIFO: case IFLNK: /* should check FASTSYMLINK? */ case IFDIR: case IFREG: return 0; case IFCHR: case IFBLK: case IFSOCK: case IFWHT: case 0: return 1; default: errx(1, "unknown IFMT 0%o", DIP(super, dp, di_mode) & IFMT); } } static struct user { uid_t uid; char *name; daddr_t space; long count; daddr_t spc30; daddr_t spc60; daddr_t spc90; } *users; static int nusers; static void inituser(void) { int i; struct user *usr; if (nusers == 0) { nusers = 8; if ((users = calloc(nusers, sizeof(*users))) == NULL) errx(1, "allocate users"); } else { for (usr = users, i = nusers; --i >= 0; usr++) { usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0; usr->count = 0; } } } static void usrrehash(void) { int i; struct user *usr, *usrn; struct user *svusr; svusr = users; nusers *= 2; if ((users = calloc(nusers, sizeof(*users))) == NULL) errx(1, "allocate users"); for (usr = svusr, i = nusers / 2; --i >= 0; usr++) { for (usrn = users + usr->uid % nusers; usrn->name; usrn--) { if (usrn <= users) usrn += nusers; } *usrn = *usr; } } static struct user * user(uid_t uid) { struct user *usr; struct passwd *pwd; int i; while (1) { for (usr = users + uid % nusers, i = nusers; --i >= 0; usr--) { if (usr->name == NULL) { usr->uid = uid; - if ((pwd = getpwuid(uid)) == NULL) + if (noname || (pwd = getpwuid(uid)) == NULL) asprintf(&usr->name, "#%u", uid); else usr->name = strdup(pwd->pw_name); if (usr->name == NULL) errx(1, "allocate users"); } if (usr->uid == uid) return (usr); if (usr <= users) usr += nusers; } usrrehash(); } } static int cmpusers(const void *v1, const void *v2) { const struct user *u1 = v1, *u2 = v2; - return (u2->space - u1->space); + return (u2->space > u1->space ? 1 : + u2->space < u1->space ? -1 : + u1->uid > u2->uid ? 1 : + u1->uid < u2->uid ? -1 : 0); } #define sortusers(users) \ qsort((users), nusers, sizeof(struct user), cmpusers) static void uses(uid_t uid, daddr_t blks, time_t act) { static time_t today; struct user *usr; if (!today) time(&today); usr = user(uid); usr->count++; usr->space += blks; if (today - act > 90L * 24L * 60L * 60L) usr->spc90 += blks; if (today - act > 60L * 24L * 60L * 60L) usr->spc60 += blks; if (today - act > 30L * 24L * 60L * 60L) usr->spc30 += blks; } #define FSZCNT 512 static struct fsizes { struct fsizes *fsz_next; daddr_t fsz_first, fsz_last; ino_t fsz_count[FSZCNT]; daddr_t fsz_sz[FSZCNT]; } *fsizes; static void initfsizes(void) { struct fsizes *fp; int i; for (fp = fsizes; fp; fp = fp->fsz_next) { for (i = FSZCNT; --i >= 0;) { fp->fsz_count[i] = 0; fp->fsz_sz[i] = 0; } } } static void dofsizes(int fd, struct fs *super) { ino_t inode, maxino; union dinode *dp; daddr_t sz, ksz; struct fsizes *fp, **fsp; int i; maxino = super->fs_ncg * super->fs_ipg - 1; for (inode = 0; inode < maxino; inode++) { if ((dp = get_inode(fd, super, inode)) != NULL && !isfree(super, dp) ) { sz = DIP(super, dp, di_blocks); ksz = SIZE(sz); for (fsp = &fsizes; (fp = *fsp); fsp = &fp->fsz_next) { if (ksz < fp->fsz_last) break; } if (fp == NULL || ksz < fp->fsz_first) { if ((fp = malloc(sizeof(*fp))) == NULL) errx(1, "allocate fsize structure"); fp->fsz_next = *fsp; *fsp = fp; fp->fsz_first = rounddown(ksz, FSZCNT); fp->fsz_last = fp->fsz_first + FSZCNT; for (i = FSZCNT; --i >= 0;) { fp->fsz_count[i] = 0; fp->fsz_sz[i] = 0; } } fp->fsz_count[ksz % FSZCNT]++; fp->fsz_sz[ksz % FSZCNT] += sz; } } sz = 0; for (fp = fsizes; fp != NULL; fp = fp->fsz_next) { for (i = 0; i < FSZCNT; i++) { if (fp->fsz_count[i] != 0) { printf("%jd\t%jd\t%d\n", (intmax_t)(fp->fsz_first + i), (intmax_t)fp->fsz_count[i], SIZE(sz += fp->fsz_sz[i])); } } } } static void douser(int fd, struct fs *super) { ino_t inode, maxino; struct user *usr, *usrs; union dinode *dp; int n; maxino = super->fs_ncg * super->fs_ipg - 1; for (inode = 0; inode < maxino; inode++) { if ((dp = get_inode(fd, super, inode)) != NULL && !isfree(super, dp)) { uses(DIP(super, dp, di_uid), DIP(super, dp, di_blocks), DIP(super, dp, di_atime)); } } if ((usrs = malloc(nusers * sizeof(*usrs))) == NULL) errx(1, "allocate users"); memcpy(usrs, users, nusers * sizeof(*usrs)); sortusers(usrs); for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) { printf("%5d", SIZE(usr->space)); if (count) printf("\t%5ld", usr->count); printf("\t%-8s", usr->name); if (unused) { printf("\t%5d\t%5d\t%5d", SIZE(usr->spc30), SIZE(usr->spc60), SIZE(usr->spc90)); } printf("\n"); } free(usrs); } static void donames(int fd, struct fs *super) { int c; ino_t maxino; uintmax_t inode; union dinode *dp; maxino = super->fs_ncg * super->fs_ipg - 1; /* first skip the name of the filesystem */ while ((c = getchar()) != EOF && (c < '0' || c > '9')) while ((c = getchar()) != EOF && c != '\n'); ungetc(c, stdin); while (scanf("%ju", &inode) == 1) { if (inode > maxino) { warnx("illegal inode %ju", inode); return; } if ((dp = get_inode(fd, super, inode)) != NULL && !isfree(super, dp)) { printf("%s\t", user(DIP(super, dp, di_uid))->name); /* now skip whitespace */ while ((c = getchar()) == ' ' || c == '\t') /* nothing */; /* and print out the remainder of the input line */ while (c != EOF && c != '\n') { putchar(c); c = getchar(); } putchar('\n'); } else { /* skip this line */ while ((c = getchar()) != EOF && c != '\n') /* nothing */; } if (c == EOF) break; } } static void usage(void) { fprintf(stderr, "usage: quot [-cfknv] [-a | filesystem ...]\n"); exit(1); } void quot(char *name, char *mp) { int fd; struct fs *fs; get_inode(-1, NULL, 0); /* flush cache */ inituser(); initfsizes(); if ((fd = open(name, 0)) < 0) { warn("%s", name); close(fd); return; } switch (errno = sbget(fd, &fs, UFS_STDSB, UFS_NOCSUM)) { case 0: break; case ENOENT: warn("Cannot find file system superblock"); close(fd); return; default: warn("Unable to read file system superblock"); close(fd); return; } printf("%s:", name); if (mp) printf(" (%s)", mp); putchar('\n'); (*func)(fd, fs); free(fs); close(fd); } int main(int argc, char *argv[]) { struct statfs *mp; - struct fstab *fs; int ch, cnt; func = douser; header = getbsize(&headerlen, &blocksize); - while ((ch = getopt(argc, argv, "acfhknv")) != -1) { + while ((ch = getopt(argc, argv, "acfhkNnv")) != -1) { switch (ch) { case 'a': all = true; break; case 'c': func = dofsizes; break; case 'f': count = true; break; case 'h': /* ignored for backward compatibility */ break; case 'k': blocksize = 1024; break; + case 'N': + noname = true; + break; case 'n': func = donames; break; case 'v': unused = true; break; default: usage(); } } argc -= optind; argv += optind; if ((argc == 0 && !all) || (all && argc)) usage(); if (all) { for (cnt = getmntinfo(&mp, MNT_NOWAIT); --cnt >= 0; mp++) if (strncmp(mp->f_fstypename, "ufs", MFSNAMELEN) == 0) quot(mp->f_mntfromname, mp->f_mntonname); } while (argc-- > 0) { - if ((fs = getfsfile(*argv)) != NULL) - quot(fs->fs_spec, 0); + if ((mp = getmntpoint(*argv)) != NULL) + quot(mp->f_mntfromname, mp->f_mntonname); else quot(*argv, 0); argv++; } return (0); } diff --git a/usr.sbin/quot/tests/Makefile b/usr.sbin/quot/tests/Makefile new file mode 100644 index 000000000000..d4e64691f905 --- /dev/null +++ b/usr.sbin/quot/tests/Makefile @@ -0,0 +1,4 @@ +PACKAGE= tests +ATF_TESTS_SH= quot_test + +.include diff --git a/usr.sbin/quot/tests/quot_test.sh b/usr.sbin/quot/tests/quot_test.sh new file mode 100644 index 000000000000..21088d162a53 --- /dev/null +++ b/usr.sbin/quot/tests/quot_test.sh @@ -0,0 +1,102 @@ +# +# Copyright (c) 2025 Dag-Erling Smørgrav +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# Create and mount a UFS filesystem on a small memory disk +quot_setup() +{ + atf_check -o save:dev mdconfig -t malloc -s 16M + local dev=$(cat dev) + atf_check -o ignore newfs "$@" /dev/$dev + atf_check mkdir mnt + local mnt=$(realpath mnt) + atf_check mount /dev/$dev "$mnt" + echo "/dev/$dev: ($mnt)" >expect + printf "%5d\t%5d\t%-8s\n" 8 2 "#0" >>expect +} + +# Create a directory owned by a given UID +quot_adduid() +{ + local uid=$1 + atf_check install -d -o $uid -g 0 mnt/$uid + printf "%5d\t%5d\t%-8s\n" 4 1 "#$uid" >>expect +} + +# Perform the tests +quot_test() +{ + local dev=$(cat dev) + # Create inodes owned by a large number of users to exercise + # hash collisions and rehashing. The code uses an open hash + # table that starts out with only 8 entries and doubles every + # time it fills up. + local uid + for uid in $(seq 1 32); do + quot_adduid $uid + done + # Also create inodes owned by users with long UIDs, up to the + # highest possible value (2^32 - 2, because chown(2) and + # friends interpret 2^32 - 1 as “leave unchanged”). + local shift + for shift in $(seq 6 32); do + quot_adduid $(((1 << shift) - 2)) + done + # Since quot operates directly on the underlying device, not + # on the mounted filesystem, we remount read-only to ensure + # that everything gets flushed to the memory disk. + atf_check mount -ur /dev/$dev + atf_check -o file:expect quot -fkN /dev/$dev + atf_check -o file:expect quot -fkN $(realpath mnt) +} + +# Unmount and release the memory disk +quot_cleanup() +{ + if [ -d mnt ]; then + umount mnt || true + fi + if [ -f dev ]; then + mdconfig -d -u $(cat dev) || true + fi +} + +atf_test_case ufs1 cleanup +ufs1_head() +{ + atf_set descr "Test quot on UFS1" + atf_set require.user root +} +ufs1_body() +{ + quot_setup -O1 + quot_test +} +ufs1_cleanup() +{ + quot_cleanup +} + +atf_test_case ufs2 cleanup +ufs2_head() +{ + atf_set descr "Test quot on UFS2" + atf_set require.user root +} +ufs2_body() +{ + quot_setup -O2 + quot_test +} +ufs2_cleanup() +{ + quot_cleanup +} + +atf_init_test_cases() +{ + atf_add_test_case ufs1 + atf_add_test_case ufs2 +}