[OpenAFS] Re: how to tell if a dir resides in AFS volume?

Andrew Deason adeason@sinenomine.net
Tue, 17 Sep 2013 12:23:49 -0500

On Tue, 17 Sep 2013 18:32:31 +0800
shuaijie wang <wangshuaijie@gmail.com> wrote:

> Is there any method to check if a dir resides in AFS volume? Since AFS
> can be mounted in any local logical path, simply checking prefix
> "/afs" seems not sufficient for all the cases. So is there any system
> call like "is_inafs()" to accomplish this?

The most portable and "correct" way is to issue a pioctl. Stuff based on
mountpoint information or statfs/statvfs info I think can vary depending
on the system, so may not be portable. If you don't care about that, you
can just look at the fs info from statfs/statvfs.

If this is for a script, just call an afs command like 'fs examine' or
'fs lq' or whatever. For a C program, you can call a variant on pioctl()
itself. From libkopenafs, you have k_pioctl. An example of usage:

$ cat foo.c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <sys/ioctl.h>
#include <kopenafs.h>

main(int argc, char *argv[])
    struct ViceIoctl iob;
    uint32_t fid[4];
    int code;

    if (argc < 2) {
        return 1;

    memset(&iob, 0, sizeof(iob));
    iob.out = (void*)&fid;
    iob.out_size = sizeof(fid);

    code = k_pioctl(argv[1], VIOCGETFID, &iob, 1);
    if (code) {
        printf("File %s is NOT in afs (or there was an error)\n", argv[1]);
        return 1;
    printf("File %s is in afs\n", argv[1]);
    return 0;
$ gcc foo.c -lkopenafs
$ ./a.out /etc/passwd
File /etc/passwd is NOT in afs (or there was an error)
$ ./a.out /afs/.localcell
File /afs/.localcell is in afs
$ ln -s /afs/.localcell /tmp/fooafs
$ ./a.out /tmp/fooafs
File /tmp/fooafs is in afs

The last argument to k_pioctl is a flag for whether or not to follow
symlinks (setting it to 0 would change the result of that last example).
If a file is actually in afs, k_pioctl can obviously still fail if we
can't resolve the whole path. The return code should be -1 for 'not in
afs', 0 for 'in afs', and something else for an error. Sometimes other
errors appear as -1, though, so I wouldn't interpret a -1 as an
authoritative answer.

That k_pioctl method also requires effectively stat()ing the file. If
you run it on a mountpoint, we will try to access the target directory.
You can avoid that (and possibly run more quickly) by using some
different pioctl calls, but if you don't care, just go with the above
simple example.

Andrew Deason