Search code examples
clinuxlinux-kernelkernel-modulemanpage

When to use fcheck() or fcheck_files() and for what?


In Linux kernel module, while calculating absolute path by file descriptor, fcheck() or fcheck_files() are used. I didn't get much information about these functions. I need to know which function is appropriate for which situation. When these functions fails? Where to find documentation about such functions? As stated here,

... To look up the file structure given an fd, a reader must use either fcheck() or fcheck_files() APIs....

But no information is given about which function should be used when and for what.

Thanks in advance!


Solution

  • fcheck() is defined as a preprocessor macro in include/linux/fdtable.h, line 90 as:

    #define fcheck(fd)      fcheck_files(current->files, fd)
    

    And fcheck_files() is defined as a function in include/linux/fdtable.h, line 77 as:

    static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
    {
            struct file * file = NULL;
            struct fdtable *fdt = files_fdtable(files);
    
            if (fd < fdt->max_fds)
                    file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
            return file;
    }
    

    They are not two functions thus no need to confuse which is appropriate for which function.

    The fcheck() function is used to

    Check whether the specified fd has an open file.