All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: Amy Parker <enbyamy@gmail.com>
Cc: linux-fsdevel@vger.kernel.org, Christoph Hellwig <hch@infradead.org>
Subject: Re: Using bit shifts for VXFS file modes
Date: Mon, 1 Feb 2021 20:31:05 -0500	[thread overview]
Message-ID: <YBirWYRuq2ONxt/y@mit.edu> (raw)
In-Reply-To: <CAE1WUT63RUz0r2LaJZ7hvayzfLadEdsZjymg8UYU481de+6wLA@mail.gmail.com>

On Mon, Feb 01, 2021 at 03:49:20PM -0800, Amy Parker wrote:
> Hello filesystem developers!
> 
> I was scouting through the FreeVXFS code, when I came across this in
> fs/freevxfs/vxfs.h:
> 
> enum vxfs_mode {
>         VXFS_ISUID = 0x00000800, /* setuid */
>         VXFS_ISGID = 0x00000400, /* setgid */
>         VXFS_ISVTX = 0x00000200, /* sticky bit */
>         VXFS_IREAD = 0x00000100, /* read */
>         VXFS_IWRITE = 0x00000080, /* write */
>         VXFS_IEXEC = 0x00000040, /* exec */

The main reason why some developers prefer to using enum is because it
allows the compiler to do type checking.  Also some people prefer
using hex digits because it becomes easier for people who are looking
at hex dumps.  So for example:

typedef enum {
        EXT4_IGET_NORMAL =      0,
        EXT4_IGET_SPECIAL =     0x0001, /* OK to iget a system inode */
        EXT4_IGET_HANDLE =      0x0002  /* Inode # is from a handle */
} ext4_iget_flags;

> Anyways, I believe using bit shifts to represent different file modes
> would be a much better idea - no runtime penalty as they get
> calculated into constants at compile time, and significantly easier
> for the average user to read.

That's a matter of personal preference; and I'll note that it's not a
matter of what is better for average users, but rather the average
file system developer.  Some people find octal easier, because that
was what Digital Equipment Corporation (DEC) systems tended to use,
and early Unix was developed on PDP-11.  So that's why octal gets used
in the man page for chmod, e.g.:

#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

Personally, *I* find this easier to read than

#define S_IRGRP (1U << 5)
#define S_IWGRP (1U << 4)
#define S_IXGRP (1U << 3)

But perhaps that's because I can convert between octal and binary in
my sleep (having learned how to toggle in disk bootstraps into the
front console of a PDP-8i[1] when I was in grade school).

[1] https://www.vintagecomputer.net/digital/pdp8i/Digital_PDP8i_a.JPG

> Any thoughts on this?

I don't think there's a right answer here.  In some cases, hex will be
better; in some cases, octal (especially as far as Unix permissions is
concerned); and in other cases, perhaps using bit shifts is more important.

A lot depends on how you plan can use it, and your past experiewnce.
Maybe you can take left shift numbers and be able to translate that to
hex when looking at kernel oops messages; I can't, but I can take hex
definiions and can take something like 0xA453 and map that to what
flags are set that are defined using hex constants.

Cheers,

						- Ted

  parent reply	other threads:[~2021-02-02  1:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-01 23:49 Using bit shifts for VXFS file modes Amy Parker
2021-02-01 23:57 ` Chaitanya Kulkarni
2021-02-02  0:40   ` Amy Parker
2021-02-02  0:21 ` Randy Dunlap
2021-02-02  0:46   ` Amy Parker
2021-02-02  1:31 ` Theodore Ts'o [this message]
2021-02-02  1:48 ` Darrick J. Wong
2021-02-02  2:36   ` Amy Parker

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=YBirWYRuq2ONxt/y@mit.edu \
    --to=tytso@mit.edu \
    --cc=enbyamy@gmail.com \
    --cc=hch@infradead.org \
    --cc=linux-fsdevel@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.