Re: [RFC] preliminary patch for files struct

Linus Torvalds (torvalds@transmeta.com)
Fri, 30 Jan 1998 09:57:38 -0800 (PST)


On Fri, 30 Jan 1998, Bill Hawes wrote:
>
> A while back there was a discussion here of the problem of clone tasks closing a
> file descriptor while another clone was using it. (I'm referring to the case
> where multiple tasks share a files structure.) The basic problem is simply that
> with a shared files structure, any clone can close one of the fds while it's in
> use.
>
> As I haven't seen any other proposals to address this problem, I've appended a
> preliminary patch implementing a reasonable solution, which is to simply use a
> wrapper function to bump the f_count value while the file is being used. This
> function replaces the common idiom
>
> if (fd < NR_OPEN && (file = current->files->fd[fd])) ...
>
> with a file = fd_get(fd) to do the checking.

Heh. This function already exists, and is already in use in various
places, notably read() and write() which are the most easily triggered
offenders of the close/use problem. See

include/linux/file.h

for fget() and fput()..

> When the operation is complete, a check needs to be made that the file
> is still installed for the original fd slot, and then the use count can be
> decremented. If the fd slot has been closed (and maybe reassigned), the file
> pointer must be closed to clean up potential lock files. This operation is
> provided by a fd_put(fd, file) routine.

Again, the only difference is the naming, and the fact that the way I
implemented it you don't actually need to remember the file descriptor (so
it's just a simple fput(file) in my routines).

> Note that the operations in fs/read_write.c were already using fget() to protect
> the file pointer, but there was a slight problem with using fput() to decrement
> the count. If the file had already been closed (by a clone task), this would
> have omitted to call to remove locks for the current process. I've replaced the
> fget/fputs with fd_get/fd_puts.

What was the problem? The locks should be removed when the last fput(file)
happens - if that doesn't work then _that_ is the bug rather than any
interface issues..

Linus