Просмотр исходного кода

Keep the system responsive during format

Johannes Zellner 2 недель назад
Родитель
Сommit
15f8e88c90
6 измененных файлов с 168 добавлено и 46 удалено
  1. 61 6
      fatformat.c
  2. 5 0
      fatformat.h
  3. 19 7
      format.c
  4. 11 0
      sys_fs.c
  5. 69 33
      vfs.c
  6. 3 0
      vfs.h

+ 61 - 6
fatformat.c

@@ -2,7 +2,9 @@
  * Floppy: FAT12 superfloppy (no MBR). IDE/SATA: MBR + single FAT16/32 partition. */
 
 #include "fatformat.h"
+#include "io.h"
 #include "mm.h"
+#include "sched.h"
 
 #define SEC_SIZE 512
 
@@ -11,6 +13,52 @@
  * ~2 M FAT sectors). */
 #define ZERO_CHUNK_PAGES 32
 
+volatile int format_busy;
+volatile unsigned int format_owner_pid;
+
+static unsigned int fmt_total, fmt_done, fmt_last_pct;
+
+static void format_pct(unsigned int pct)
+{
+	char b[4];
+	unsigned int n = 0;
+
+	b[n++] = (char)('0' + pct / 100);
+	b[n++] = (char)('0' + (pct / 10) % 10);
+	b[n++] = (char)('0' + pct % 10);
+	io_putstr("format: ");
+	{ unsigned int i; for (i = 0; i < n; i++) io_putchar(b[i]); }
+	io_putstr("%\n");
+}
+
+static void format_progress(unsigned int sectors)
+{
+	unsigned int pct;
+
+	if (!fmt_total)
+		return;
+	fmt_done += sectors;
+	pct = (fmt_done * 100u) / fmt_total;
+	if (pct > 100) pct = 100;
+	if (pct >= fmt_last_pct + 10 || pct == 100) {
+		fmt_last_pct = pct;
+		format_pct(pct);
+	}
+}
+
+/* A chunk write releases the BKL so the WM/netlog/terminal can run between
+ * disk transfers; format_busy keeps other filesystem syscalls out. */
+static int format_write(blk_dev_t *d, unsigned int lba, const void *buf,
+			unsigned int count)
+{
+	int r;
+
+	bkl_pause();
+	r = blk_write(d, lba, buf, count);
+	bkl_resume();
+	return r;
+}
+
 /* Write `count` zero sectors at `lba` in `chunk`-sector pieces. */
 static int write_zeros(blk_dev_t *d, unsigned int lba, unsigned int count,
 		       const unsigned char *buf, unsigned int chunk)
@@ -21,8 +69,9 @@ static int write_zeros(blk_dev_t *d, unsigned int lba, unsigned int count,
 		unsigned int n = count - done;
 		if (n > chunk)
 			n = chunk;
-		if (blk_write(d, lba + done, buf, n) != 0)
+		if (format_write(d, lba + done, buf, n) != 0)
 			return -1;
+		format_progress(n);
 		done += n;
 	}
 	return 0;
@@ -213,6 +262,12 @@ int fat_format(blk_dev_t *d)
 	root_start = reserved + num_fats * sec_per_fat;
 	data_start = root_start + root_dir_sectors;
 
+	fmt_total = reserved + num_fats * sec_per_fat + root_dir_sectors;
+	if (type == 32)
+		fmt_total += spc;
+	fmt_done = 0;
+	fmt_last_pct = 0;
+
 	zero_sector(zero);
 
 	/* Bulk zero buffer (a 1 TB FAT32 has ~2 M FAT sectors; single-sector
@@ -248,7 +303,7 @@ int fat_format(blk_dev_t *d)
 		put32(e + 12, fs_sectors);
 		sec[510] = 0x55;
 		sec[511] = 0xAA;
-		if (blk_write(d, 0, sec, 1) != 0)
+		if (format_write(d, 0, sec, 1) != 0)
 			return -1;
 	}
 
@@ -257,13 +312,13 @@ int fat_format(blk_dev_t *d)
 			  sec_per_fat, fs_sectors, part_start, media);
 	if (write_zeros(d, part_start, reserved, zbuf, zchunk) != 0)
 		return -1;
-	if (blk_write(d, part_start, sec, 1) != 0)
+	if (format_write(d, part_start, sec, 1) != 0)
 		return -1;
 	if (type == 32) {
-		if (blk_write(d, part_start + 6, sec, 1) != 0)   /* backup boot */
+		if (format_write(d, part_start + 6, sec, 1) != 0)   /* backup boot */
 			return -1;
 		build_fsinfo_sector(sec);
-		if (blk_write(d, part_start + 1, sec, 1) != 0)   /* FSInfo */
+		if (format_write(d, part_start + 1, sec, 1) != 0)   /* FSInfo */
 			return -1;
 	}
 
@@ -272,7 +327,7 @@ int fat_format(blk_dev_t *d)
 	build_fat_first_sector(fat_sec, type, media);
 	for (i = 0; i < num_fats; i++) {
 		unsigned int base = part_start + fat_start + i * sec_per_fat;
-		if (blk_write(d, base, fat_sec, 1) != 0)
+		if (format_write(d, base, fat_sec, 1) != 0)
 			return -1;
 		if (sec_per_fat > 1 &&
 		    write_zeros(d, base + 1, sec_per_fat - 1, zbuf, zchunk) != 0)

+ 5 - 0
fatformat.h

@@ -8,4 +8,9 @@
 /* Format device d. Returns 0 on success, -1 on error. */
 int fat_format(blk_dev_t *d);
 
+/* Set while a destructive format owns the disks.  Filesystem syscalls wait
+ * for it instead of touching a half-written FAT; the owner is exempt. */
+extern volatile int format_busy;
+extern volatile unsigned int format_owner_pid;
+
 #endif

+ 19 - 7
format.c

@@ -3,6 +3,7 @@
 #include "blkdev.h"
 #include "fatformat.h"
 #include "io.h"
+#include "process.h"
 #include "vga.h"
 #include "vfs.h"
 
@@ -24,9 +25,13 @@ void program_format_main(const char *arg)
 	unsigned int i = 0, j;
 	int k;
 
+	if (format_busy && process_is_alive(format_owner_pid)) {
+		io_putstr("format: another format is in progress\n");
+		return;
+	}
+	format_busy = 0;
 	if (!arg) {
 		io_putstr("format: usage format <device>\n");
-		blk_scan();
 		list_devices();
 		return;
 	}
@@ -40,13 +45,10 @@ void program_format_main(const char *arg)
 
 	if (name[0] == '\0') {
 		io_putstr("format: usage format <device>\n");
-		blk_scan();
 		list_devices();
 		return;
 	}
 
-	blk_scan();
-
 	d = blk_find(name);
 	if (!d) {
 		io_putstr("format: unknown device '");
@@ -80,6 +82,11 @@ void program_format_main(const char *arg)
 			io_putstr("aborted\n");
 			return;
 		}
+		/* This kernel command holds the BKL.  Sleep briefly so the
+		 * kworker can poll USB HID and the terminal can render the
+		 * confirmation prompt; otherwise a USB-only keyboard deadlocks
+		 * here. */
+		process_sleep_ms(10);
 	}
 	io_set_raw_keyboard(0);
 	io_putchar('\n');
@@ -89,13 +96,18 @@ void program_format_main(const char *arg)
 	io_putstr("...\n");
 	vga_fb_flush();
 
+	/* Other filesystem syscalls wait while the FAT is half-written. */
+	format_owner_pid = current_proc()->pid;
+	format_busy = 1;
 	if (fat_format(d) != 0) {
+		format_busy = 0;
 		io_putstr("format: failed\n");
 		return;
 	}
-
-	/* Make the fresh filesystem immediately usable (save/ls/cat). */
-	vfs_mount_all();
+	/* Remount only the formatted device; a full vfs_mount_all() would probe
+	 * every disk again and was a second large stall. */
+	vfs_remount_device(d);
+	format_busy = 0;
 
 	io_putstr("done\n");
 }

+ 11 - 0
sys_fs.c

@@ -13,11 +13,22 @@
  * syscall, so no copy in/out is needed. */
 
 #include "syscall.h"
+#include "fatformat.h"
 #include "process.h"
 #include "vfs.h"
 
 void sys_fs_enter(void)
 {
+	/* A format owns the disks while its FAT is half-written.  Do not let a
+	 * file syscall read that inode/FAT state; sleeping drops the BKL so the
+	 * formatter can finish. */
+	while (format_busy && current_proc()->pid != format_owner_pid) {
+		if (!process_is_alive(format_owner_pid)) {
+			format_busy = 0;
+			break;
+		}
+		process_sleep_ms(10);
+	}
 	preempt_disable();
 	__asm__ volatile ("sti");   /* the disk may need its IRQ */
 }

+ 69 - 33
vfs.c

@@ -492,53 +492,89 @@ static void part_name(char *out, const char *base, unsigned int idx)
 	out[i] = '\0';
 }
 
-void vfs_mount_all(void)
+static void mount_device(blk_dev_t *d, unsigned char *buf)
 {
-	static unsigned char buf[512];
-	unsigned int i;
-
-	vfs_mount_count = 0;
-	blk_scan();
+	mbr_part_t parts[4];
+	int n, j;
+	int any_fat = 0;
 
-	for (i = 0; i < blk_dev_count; i++) {
-		blk_dev_t *d = &blk_devs[i];
+	if (d->kind != BLK_FLOPPY && !d->present)
+		return;
+	if (d->kind == BLK_FLOPPY) {
+		mount_one(d, 0, d->name);
+		return;
+	}
 
-		if (d->kind != BLK_FLOPPY && !d->present)
+	/* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
+	if (blk_read(d, 0, buf, 1) != 0)
+		return;
+	n = mbr_parse(buf, parts);
+	for (j = 0; j < n; j++) {
+		char pname[16];
+		if (!parts[j].is_fat || parts[j].start_lba == 0 ||
+		    parts[j].sectors == 0)
 			continue;
+		any_fat = 1;
+		part_name(pname, d->name, parts[j].index);
+		mount_one(d, parts[j].start_lba, pname);
+	}
+	if (!any_fat)
+		mount_one(d, 0, d->name);
+}
 
-		if (d->kind == BLK_FLOPPY) {
-			mount_one(d, 0, d->name);
-			continue;
-		}
+/* Mount names for a device are the device itself or "<device>pN". */
+static int mount_belongs_to(const char *mount, const char *dev)
+{
+	unsigned int i = 0;
 
-		/* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
-		{
-			mbr_part_t parts[4];
-			int n, j;
-			int any_fat = 0;
-
-			if (blk_read(d, 0, buf, 1) != 0)
-				continue;
-			n = mbr_parse(buf, parts);
-			for (j = 0; j < n; j++) {
-				char pname[16];
-				if (!parts[j].is_fat || parts[j].start_lba == 0 ||
-				    parts[j].sectors == 0)
-					continue;
-				any_fat = 1;
-				part_name(pname, d->name, parts[j].index);
-				mount_one(d, parts[j].start_lba, pname);
-			}
-			if (!any_fat)
-				mount_one(d, 0, d->name);
+	while (dev[i] && mount[i] == dev[i])
+		i++;
+	if (dev[i] != '\0')
+		return 0;
+	return mount[i] == '\0' || mount[i] == 'p';
+}
+
+static void unmount_device(const char *dev)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < vfs_mount_count; ) {
+		if (mount_belongs_to(vfs_mounts[i].name, dev)) {
+			for (j = i + 1; j < vfs_mount_count; j++)
+				vfs_mounts[j - 1] = vfs_mounts[j];
+			vfs_mount_count--;
+		} else {
+			i++;
 		}
 	}
+}
+
+void vfs_mount_all(void)
+{
+	static unsigned char buf[512];
+	unsigned int i;
+
+	vfs_mount_count = 0;
+	blk_scan();
+
+	for (i = 0; i < blk_dev_count; i++)
+		mount_device(&blk_devs[i], buf);
 
 	/* The ramfs is not a block device; keep it mounted across a refresh
 	 * (the `storage` command calls vfs_mount_all()). */
 	vfs_mount_ramfs();
 }
 
+void vfs_remount_device(blk_dev_t *d)
+{
+	unsigned char buf[512];
+
+	if (!d)
+		return;
+	unmount_device(d->name);
+	mount_device(d, buf);
+}
+
 vfs_mount_t *vfs_find(const char *name)
 {
 	unsigned int i;

+ 3 - 0
vfs.h

@@ -4,6 +4,7 @@
 #ifndef VFS_H
 #define VFS_H
 
+#include "blkdev.h"
 #include "fat.h"
 
 /* Largest file the shell will load from a filesystem into a kernel buffer. */
@@ -71,6 +72,8 @@ extern unsigned int vfs_mount_count;
 
 /* Scan devices and mount all detected FAT filesystems. */
 void vfs_mount_all(void);
+/* Re-mount only one block device after it was formatted. */
+void vfs_remount_device(blk_dev_t *d);
 
 /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
 void vfs_mount_ramfs(void);