From: Mike Frysinger <vapier@gentoo.org>
To: Adrian McMenamin <lkmladrian@gmail.com>
Cc: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org,
LKML <linux-kernel@vger.kernel.org>,
"Linux-sh" <linux-sh@vger.kernel.org>,
Adrian McMenamin <adrianmcmenamin@gmail.com>
Subject: Re: Utility code to generate a VMUFAT filesystem
Date: Sun, 25 Mar 2012 02:32:27 -0400 [thread overview]
Message-ID: <201203250232.29843.vapier@gentoo.org> (raw)
In-Reply-To: <CAN628AHjJeLD_bUNGSDmBOo_EW=aFML-VZ-A4GRyAi2eNwA1yw@mail.gmail.com>
[-- Attachment #1: Type: Text/Plain, Size: 4189 bytes --]
On Wednesday 21 March 2012 00:41:17 Adrian McMenamin wrote:
> /*
> * mkfs.vmufat.c - make a linux (VMUFAT) filesystem
> *
> * Copyright (c) 2012 Adrian McMenamin adrianmcmenamin@gmail.com
> * Licensed under Version 2 of the GNU General Public Licence
> *
> * Parts shamelessly copied from other mkfs code
> * copyright Linus Torvalds and others
> */
might be useful to sneak into tools/ or Documentation/ or fs/vmufat/
> void usage()
(void)
seems like most (if not just about all) funcs in here should be static
> int checkmount(char* device_name)
> {
> FILE * f;
> struct mntent * mnt;
kernel style wise, this really should be:
char *foo;
FILE *f;
struct mntent *mnt;
etc...
device_name here should be const
> if ((f = setmntent(_PATH_MOUNTED, "r")) == NULL)
> return;
there is libmount now, but that might be overkill for this small tool
> int readforbad(struct badblocklist** root, char* filename, int verbose)
filename should be const
> {
> int error = 0;
> FILE *listfile;
> int badblocks = 0;
> unsigned long blockno;
> ...
>
> while (!feof(listfile)) {
> if (fscanf(listfile, "%ld\n", &blockno) != 1) {
%ld is "signed long" but blockno is "unsigned long". probably want %lu here
and elsewhere in this func.
> void _fill_root_block(char* buf, const struct vmuparam* param)
incoming buf is "char *" ...
> uint16_t* wordbuf;
>
> wordbuf = (uint16_t *)buf;
... which you cast up to "uint16_t *" ...
> int mark_root_block(int device_numb, const struct vmuparam *param, int
> verbose) {
> char zilches[BLOCKSIZE];
> int i, error = 0;
>
> for (i = 0; i < BLOCKSIZE; i++)
> zilches[i] = '\0';
>
> _fill_root_block(zilches, param);
... and is declared on the stack as "char *". there are no alignment
requirements here, and iirc, superh doesn't support unaligned loads. so you
should add gcc aligned attributes, or declare the buffer better:
uint16_t zilches[BLOCKSIZE / 2];
> int zero_blocks(int device_numb, const struct vmuparam *param, int verbose)
> {
> char zilches[BLOCKSIZE];
> int i, error = -1;
>
> for (i = 0; i < BLOCKSIZE; i++)
> zilches[i] = '\0';
memset(zilches, '\0', BLOCKSIZE);
> int scanforbad(int device_numb, struct badblocklist** root, int verbose)
> {
> int error = 0, i;
> struct badblocklist *lastbadblock = NULL;
> off_t size;
> long got;
> char buffer[BLOCKSIZE];
>
> size = lseek(device_numb, 0, SEEK_END);
if you use fstat() here ...
> for (i = 0; i < size/BLOCKSIZE; i++)
> {
> if (verbose > 0)
> printf("Testing block %i\n", i);
> if (lseek(device_numb, i * BLOCKSIZE, SEEK_SET) !=
> i * BLOCKSIZE) {
> printf("Seek failed on device\n");
> error = -1;
> goto out;
> }
> got = read(device_numb, buffer, BLOCKSIZE);
... and pread() here, there's no need for the lseek()'s at all.
> if (lseek(device_numb, fatblock * BLOCKSIZE, SEEK_SET) < 0)
> goto out;
> if (read(device_numb, buffer, BLOCKSIZE) != BLOCKSIZE)
> goto out;
use pread() instead of lseek() && read()
> if (lseek(device_numb, fatblock * BLOCKSIZE, SEEK_SET) < 0)
> goto out;
> if (write(device_numb, buffer, BLOCKSIZE) != BLOCKSIZE)
> goto out;
and pwrite() instead of lseek() && write()
> if (checkmount(device_name) < 0)
> goto out;
>
> if (stat(device_name, &statbuf) < 0) {
> printf("Cannot get status of %s\n", device_name);
> goto out;
> }
> if (!S_ISBLK(statbuf.st_mode)) {
> printf("%s must be a block device\n", device_name);
> goto out;
> }
> device_numb = open(device_name, O_RDWR|O_EXCL);
technically you've got a race condition here. really should do the open() and
then do fstat() on the fd you get back.
also, what's with the O_EXCL ? that only makes sense with O_CREAT.
should you open+fstat before checking for the mount point ? guess it doesn't
really matter.
it's useful to be able to format regular files as filesystems especially for
debugging. most mkfs tools issue a warning or prompt by default, and using
the -f (force) flag allows you to format regular files.
-mike
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]
next prev parent reply other threads:[~2012-03-25 6:32 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-21 4:41 Adrian McMenamin
2012-03-25 6:32 ` Mike Frysinger [this message]
2012-04-06 13:40 ` Adrian McMenamin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201203250232.29843.vapier@gentoo.org \
--to=vapier@gentoo.org \
--cc=adrianmcmenamin@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sh@vger.kernel.org \
--cc=lkmladrian@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome