shithub: gefs

Download patch

ref: e7398383ed59cfcf02d8916fed0d0be3421822cd
parent: 2ed6139f5d5ce9a369c44fd3a88b86a309964891
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Dec 18 11:20:45 EST 2023

fs: add flag to disable autosnaps

--- a/cons.c
+++ b/cons.c
@@ -57,7 +57,7 @@
 static void
 listsnap(int fd)
 {
-	char *fmut, *fauto, pfx[Snapsz];
+	char pfx[Snapsz];
 	Scan s;
 	uint flg;
 	int sz;
@@ -70,9 +70,14 @@
 		if(!btnext(&s, &s.kv))
 			break;
 		flg = UNPACK32(s.kv.v+1+8);
-		fmut = (flg & Lmut) ? " mutable" : "";
-		fauto = (flg & Lauto) ? " auto" : "";
-		fprint(fd, "snap %.*s%s%s\n", s.kv.nk-1, s.kv.k+1, fmut, fauto);
+		fprint(fd, "snap %.*s", s.kv.nk-1, s.kv.k+1);
+		if(flg & Lmut)
+			fprint(fd, " mutable");
+		if(flg & Lauto)
+			fprint(fd, " auto");
+		if(flg & Lnoauto)
+			fprint(fd, " auto=off");
+		fprint(fd, "\n");
 	}
 	btexit(&s);
 }
@@ -81,6 +86,7 @@
 snapfs(int fd, char **ap, int na)
 {
 	Amsg *a;
+	int i;
 
 	if((a = mallocz(sizeof(Amsg), 1)) == nil){
 		fprint(fd, "alloc sync msg: %r\n");
@@ -88,17 +94,21 @@
 	}
 	a->op = AOsnap;
 	a->fd = fd;
-	if(ap[0][0] == '-'){
-		switch(ap[0][1]){
-		case 'm':	a->flag = Lmut;	break;
-		case 'd':	a->delete++;	break;
-		case 'l':
-			listsnap(fd);
-			return;
-		default:
-			fprint(fd, "usage: snap -[md] old [new]\n");
-			free(a);
-			return;
+	while(ap[0][0] == '-'){
+		for(i = 1; ap[0][i]; i++){
+			switch(ap[0][i]){
+			case 'S':	a->flag |= Lnoauto;	break;
+			case 'm':	a->flag |= Lmut;	break;
+			case 'd':	a->delete++;		break;
+			case 'l':
+				listsnap(fd);
+				free(a);
+				return;
+			default:
+				fprint(fd, "usage: snap -[Smdl] [old [new]]\n");
+				free(a);
+				return;
+			}
 		}
 		na--;
 		ap++;
@@ -210,6 +220,7 @@
 		size += a->size;
 		used += a->used;
 		qunlock(a);
+		fprint(fd, "arena %d: %llx/%llx (%.2f)\n", i, a->used, a->size, (double)a->used/(double)a->size);
 	}
 	hsize = size;
 	hused = used;
--- a/dat.h
+++ b/dat.h
@@ -116,8 +116,9 @@
 };
 
 enum {
-	Lmut	= 1 << 0,
-	Lauto	= 1 << 1,
+	Lmut	= 1 << 0,	/* can we modify snaps via this label */
+	Lauto	= 1 << 1,	/* was this label generated automatically */
+	Lnoauto	= 1 << 2,	/* should we skip the timed snapshots */
 };
 
 enum {
@@ -615,8 +616,10 @@
 	long	ref;
 	vlong	gen;
 	char	name[64];
-	int	mutable;
 	Tree	*root;	/* EBR protected */
+
+	char	mutable;
+	char	noauto;
 
 	/* snapshot history */
 	char	minutely[60][128];
--- a/fs.c
+++ b/fs.c
@@ -449,6 +449,7 @@
 {
 	Mount *mnt;
 	Tree *t;
+	int flg;
 
 	if(strcmp(name, "dump") == 0){
 		ainc(&fs->snapmnt->ref);
@@ -472,9 +473,11 @@
 	}
 	mnt->ref = 1;
 	snprint(mnt->name, sizeof(mnt->name), "%s", name);
-	if((t = opensnap(name, &mnt->mutable)) == nil)
+	if((t = opensnap(name, &flg)) == nil)
 		error(Enosnap);
 	loadautos(mnt);
+	mnt->mutable = (flg & Lmut) != 0;
+	mnt->noauto = (flg & Lnoauto) != 0;
 	mnt->root = t;
 	mnt->next = fs->mounts;
 	fs->mounts = mnt;
@@ -2394,6 +2397,8 @@
 		tmnow(&now, nil);
 		lock(&fs->mountlk);
 		for(mnt = fs->mounts; mnt != nil; mnt = mnt->next){
+			if(mnt->noauto)
+				continue;
 			if(now.yday != then.yday){
 				snprint(buf, sizeof(buf),
 					"%s@day.%τ", mnt->name, tmfmt(&now, "YYYY.MM.DD[_]hh:mm:ss"));
--- a/snap.c
+++ b/snap.c
@@ -308,8 +308,8 @@
 	assert(nm <= nelem(m));
 	flushdlcache(1);
 	btupsert(&fs->snap, m, nm);
+	reclaimblocks(t->gen, succ, t->pred);
 	if(deltree){
-		reclaimblocks(t->gen, succ, t->pred);
 		for(mnt = fs->mounts; mnt != nil; mnt = mnt->next){
 			if(mnt->root->gen == t->succ)
 				mnt->root->pred = t->pred;
@@ -459,11 +459,10 @@
  * open snapshot by label, returning a tree.
  */
 Tree*
-opensnap(char *label, int *mut)
+opensnap(char *label, int *flg)
 {
 	char *p, buf[Kvmax];
 	Tree *t;
-	uint flg;
 	vlong gen;
 	Kvp kv;
 	Key k;
@@ -477,9 +476,8 @@
 		return nil;
 	assert(kv.nv == 1+8+4);
 	gen = UNPACK64(kv.v + 1);
-	flg = UNPACK32(kv.v + 1+8);
-	if(mut != nil)
-		*mut = !!(flg&Lmut);
+	if(flg != nil)
+		*flg = UNPACK32(kv.v + 1+8);
 
 	t = mallocz(sizeof(Tree), 1);
 	if(waserror()){