All of lore.kernel.org
 help / color / mirror / Atom feed
* Question about `generic_write_checks()` FIXME comment
@ 2021-12-30  8:07 Hans Montero
  2021-12-30 13:59 ` Al Viro
  2021-12-30 19:04 ` Theodore Ts'o
  0 siblings, 2 replies; 5+ messages in thread
From: Hans Montero @ 2021-12-30  8:07 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: Tal Zussman, Xijiao Li, OS-TA

Hey there,

We're the teaching staff for Columbia University's graduate-level operating
systems course and something came up as we were improving our filesystem
assignment.

For context, we have students develop a very simple filesystem that implements
the `read()`/`write()` `struct file_operations` functions as opposed to
`read_iter()`/`write_iter()`.

We noticed that the following bash redirection didn't work as expected in our
filesystem: `echo test >> foo.txt`. The write would occur at the beginning of
the file because the `ppos` argument to `write()` was set to 0. Through
`strace`, we verified that bash opens the file with `O_APPEND` so we assumed
that VFS would handle setting `ppos` to the file size for us, yet it did not.

We dug through kernel code to see if other filesystems explicitly account for
this case and surely enough, they do! Most of the filesystems we saw
directly/indirectly call `generic_write_checks()` which is implemented for
`write_iter()` usage. We were able to solve our bug by simply porting the
following logic for our version of `write()`:

  /* FIXME: this is for backwards compatibility with 2.4 */
  if (iocb->ki_flags & IOCB_APPEND)
          iocb->ki_pos = i_size_read(inode);

We noticed the FIXME comment and we weren't sure exactly what it meant so we
kept tracing through older versions of `generic_write_checks()`, going as far as
Linux 2.5.75, before it was implemented for `write_iter()` usage:

  if (!isblk) {
          /* FIXME: this is for backwards compatibility with 2.4 */
          if (file->f_flags & O_APPEND)
                  *pos = inode->i_size;
          ...
  }

Can someone explain what this comment is referring to? And why does `!isblk`
matter?

Furthermore, why doesn't VFS do the `O_APPEND` check itself instead of
delegating the task to the filesystems? It seems like a universal check,
especially given the following snippet on `O_APPEND` from the man page for
open(2):

  APPEND
      The file is opened in append mode. Before each write(2), the file offset
      is positioned at the end of the file, as if with lseek(2).

Best,
Hans

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-12-31  8:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-30  8:07 Question about `generic_write_checks()` FIXME comment Hans Montero
2021-12-30 13:59 ` Al Viro
2021-12-30 19:04 ` Theodore Ts'o
2021-12-30 20:06   ` Al Viro
2021-12-31  7:23     ` Tal Zussman

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.