Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 07 Dec 2000 11:54:08 +1030
From:      Matthew Thyer <Matthew.Thyer@dsto.defence.gov.au>
To:        freebsd-questions@freebsd.org
Subject:   write(2) returns error saying read only filesystem when trying to  write to a partition
Message-ID:  <3A2EE6B8.69624B48@dsto.defence.gov.au>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------12E4E8D0908F652BEE36EF01
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Attached is scrub.c used to scrub a hard disk.

Examine the lines:

/*      if ( (fd = open("/dev/da12s1c",O_RDWR)) < 0 ) { */
        if ( (fd = open("/mnt/foo",O_RDWR)) < 0 ) {


If I newfs the 'a' partition of da12s1 (a is the same as 'c'), mount
it as /mnt, touch the file foo and then run the program it works fine.

If instead I open the c partition (as in the commented out line),
the open succeeds, the lseek succeeds but the writes fail with an
error saying read only filesystem.

Why ??

I want to scrub the whole disk, not just write to a file.

-- 
 Matthew Thyer                                 Phone:  +61 8 8259 7249
 Science Corporate Information Systems         Fax:    +61 8 8259 5537
 Defence Science and Technology Organisation, Salisbury
 PO Box 1500 Salisbury South Australia 5108
--------------12E4E8D0908F652BEE36EF01
Content-Type: text/plain; charset=us-ascii;
 name="scrub.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="scrub.c"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/uio.h>
#include <unistd.h>

#define	BUFSIZE		8192


main()
{
	/* program to write 1's and 0's to disk */

	off_t result;
	long int i;
	long int nwrite;
	long int count;
	int passes;
	int fd;
	char buf0[BUFSIZE], buf1[BUFSIZE];

	/* initialize write buffers */

	for (i=0; i < BUFSIZE; i++) {
		buf0[i] = 0;
		buf1[i] = 1;
	}

/*	if ( (fd = open("/dev/da12s1c",O_RDWR)) < 0 ) { */
	if ( (fd = open("/mnt/foo",O_RDWR)) < 0 ) {
		perror("open");
		exit(1);
	}

	/* write alternating 1s and zeros to disk */

	for (i = 1; i <= 5; i++) {

		/* rewind to start file partition */
		if ((result = lseek(fd, 0L, SEEK_SET)) != 0) {
			perror("lseek");
			exit(1);
		}
		count = 0L;
		do {

			if ( (nwrite = write(fd, buf0, BUFSIZE)) != BUFSIZE) {
				printf("wrote last %ld bytes to disk\n",nwrite);
				printf("Total bytes written were %ld bytes\n",
					BUFSIZE*count + nwrite);
				perror("write");
			}
			else {
				++count;
				if ( count % 1000 == 0) 
					printf("overwrote %ld bytes w/ 0s\n",
						BUFSIZE*count);
			}

		} while ( nwrite == BUFSIZE && nwrite > 0 );

		/* rewind to start file partition */
		if ((result = lseek(fd, 0L, SEEK_SET)) != 0) {
			perror("lseek");
			exit(1);
		}
		count = 0L;

		do {
			if ( (nwrite = write(fd, buf1, BUFSIZE)) != BUFSIZE) {
				printf("wrote last %ld bytes to disk\n",nwrite);
				printf("Total bytes written were %ld bytes\n",
					BUFSIZE*count + nwrite);
				perror("write");
			}
			else {
				++count;
				if ( count % 1000 == 0) 
					printf("overwrote %ld bytes w/ 1s\n",
						BUFSIZE*count);
			}

		} while ( nwrite == BUFSIZE && nwrite > 0 );

		printf("Pass %d complete\n",i);
	}
	printf("All passes complete\n");
}

--------------12E4E8D0908F652BEE36EF01--



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3A2EE6B8.69624B48>