1

when dealing with files in xv6, I can see an integer variable called Omode. what is it? what values could it have?

for example, this is the open system call from Xv6:

int sys_open(void)
{
  char *path;
  int fd, omode;
  struct file *f;
  struct inode *ip;

  if (argstr(0, &path) < 0 || argint(1, &omode) < 0)
    return -1;

  begin_op();

  if (omode & O_CREATE) {
    ip = create(path, T_FILE, 0, 0);
    if (ip == 0) {
      end_op();
      return -1;
    }
  } else {
    if ((ip = namei(path)) == 0) {
      end_op();
      return -1;
    }
    ilock(ip);
    if (ip->type == T_DIR && omode != O_RDONLY) {
      iunlockput(ip);
      end_op();
      return -1;
    }
  }

  if ((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0) {
    if (f)
      fileclose(f);
    iunlockput(ip);
    end_op();
    return -1;
  }
  iunlock(ip);
  end_op();

  f->type = FD_INODE;
  f->ip = ip;
  f->off = 0;
  f->readable = !(omode & O_WRONLY);
  f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
  return fd;
}

it seems that it could be O_WRONLY, O_RDWR, or O_CREATE. what does these values represent?

mohammed
  • 21
  • 2

1 Answers1

1

omode (stands for Open Mode), is the 2nd argument for open system call in xv6 operating system and represents the mode(s) to be used when opening a file who's name & path were given on the 1st argument.

From xv6's official book:

open(filename, flags) Open a file; the flags indicate read/write

Valid options for this field are (defines are located at fcntl.h):

#define O_RDONLY  0x000
#define O_WRONLY  0x001
#define O_RDWR    0x002
#define O_CREATE  0x200

Where:

  • O_RDONLY - States the file should be opened in read only mode. Do not let writing to the file represented by the file descriptor returned from the open call.
  • O_WRONLY - Same as above but allows only writing an no reading.
  • O_RDWR - Allows both read & write.
  • O_CREATE - Allows open to create the given file if it doesn't already exists.

You can also follow the code a bit further and see where readable & writable are used:

readable blocks reading when not allowed:

// Read from file f.
int
fileread(struct file *f, char *addr, int n)
{
  int r;

  if(f->readable == 0)
    return -1;
...

writable works similar for writes:

// Write to file f.
int
filewrite(struct file *f, char *addr, int n)
{
  int r;

  if(f->writable == 0)
    return -1;
...
Omer Efrat
  • 295
  • 1
  • 5