Skip to contentSkip to conversation

open_by_handle on a file ?

DISCUSSION DENIEL Philippe 2 participants 3 messages May 12, 2010 – May 25, 2010

lore
Share & tools
lore
#0 lore raw

Hi,

I start using libhandle.so from xfsprogs-3.0.3 package. I meet an issue here : I can get a handle from files or directory.
When used on diretories, open_by_handle works fine : I can read entries in it by using getdents, create stuff / removing stuff by using the ATFILE_FUNCTION (mkdirat, renameat, ...)

Trouble start when I want to open a file with open_by_handle. I have a small test program that does this :

       rc = path_to_fshandle( path_dir,  (void **)(&fshandle),
   &fshandlelen) ;
     if( rc < 0 ) exit( -1)
       
     rc = path_to_handle( path_dir,  (void **)(&filehandle), &handlelen) ;
     if( rc < 0 ) exit( -1 )

     fd = open_by_handle( filehandle, handlelen, O_RDONLY ) ;
     printf( "open_by_handle: fd=%d \n", fd ) ;
     if( fd < 0 )
          printf( "----> Error=%d | %s\n", errno, strerror( errno ) ) ;

The open_by_handle failed with errno=20 aka ENOTDIR, which is true, this is a file and no directory.
Question is : Can I use open_by_handle to open a regular file ? If yes, how should I proceed. If no, how should I do to open a file knowing its handle ?

Thanks in advance for your time and answer.

Regards

Philippe

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#1 lore raw
Quoted 22 lines · DENIEL Philippe

Hi,

I start using libhandle.so from xfsprogs-3.0.3 package. I meet an issue here : I can get a handle from files or directory. When used on diretories, open_by_handle works fine : I can read entries in it by using getdents, create stuff / removing stuff by using the ATFILE_FUNCTION (mkdirat, renameat, ...)

Trouble start when I want to open a file with open_by_handle. I have a small test program that does this :

       rc = path_to_fshandle( path_dir,  (void **)(&fshandle),
   &fshandlelen) ;
     if( rc < 0 ) exit( -1)
     rc = path_to_handle( path_dir,  (void **)(&filehandle), &handlelen) ;
     if( rc < 0 ) exit( -1 )

     fd = open_by_handle( filehandle, handlelen, O_RDONLY ) ;
     printf( "open_by_handle: fd=%d \n", fd ) ;
     if( fd < 0 )
          printf( "----> Error=%d | %s\n", errno, strerror( errno ) ) ;
Quoted 2 lines

The open_by_handle failed with errno=20 aka ENOTDIR, which is true, this is a file and no directory.

I think that is because the path_dir points to a regular file and so path_to_fshandle() is generating a fshandle that points to a file instead of a directory. This handle is cached inside libhandle, and then use for subsequent handle calls like open_by_handle(). The kernel rejects the request is the fshandle does not point to a directory.

Try using the mount point or a directory within the mount for the path_to_fshandle() call and see if that fixes the problem.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#2 lore raw

Thank you Dave, you were definitely right. I used the XFS mount point as the argument to path_to_fshandle and it now woks perfectly. :-)
This leads me to another question : now that I can convert a path to fhandle and use it to open a file or a directory, I can use getdents and the ATFILE_SOURCE functions (mkdirat, linkat, ....) to implement the NFS logic (I just have to "open_by_handle" to get the fs to the related fs object and operate on it). The idea behind this is implementing a NFS server in Userspace with XFS specific capabilites. In fact, the NFS on which a minor issue remains is LOOKUP (and it could become a major issue since LOOKUP is called very often). To lookup on an object knowing its parent directory's handle and its name, I can perform an "openat" followed by a xfs related "fd_to_handle" and close the fd once the operation is done. But it seems a bit "heavy" to me. Is there another (lighter) way of getting the handle to an object knowing its name and parent directory (may be by a call to xfsctl ?).

Regards,

Philippe

Quoted 35 lines · Dave Chinner
Quoted 22 lines · DENIEL Philippe

Hi,

I start using libhandle.so from xfsprogs-3.0.3 package. I meet an issue here : I can get a handle from files or directory. When used on diretories, open_by_handle works fine : I can read entries in it by using getdents, create stuff / removing stuff by using the ATFILE_FUNCTION (mkdirat, renameat, ...)

Trouble start when I want to open a file with open_by_handle. I have a small test program that does this :

       rc = path_to_fshandle( path_dir,  (void **)(&fshandle),
   &fshandlelen) ;
     if( rc < 0 ) exit( -1)
     rc = path_to_handle( path_dir,  (void **)(&filehandle), &handlelen) ;
     if( rc < 0 ) exit( -1 )

     fd = open_by_handle( filehandle, handlelen, O_RDONLY ) ;
     printf( "open_by_handle: fd=%d \n", fd ) ;
     if( fd < 0 )
          printf( "----> Error=%d | %s\n", errno, strerror( errno ) ) ;
Quoted 3 lines

The open_by_handle failed with errno=20 aka ENOTDIR, which is true, this is a file and no directory.

I think that is because the path_dir points to a regular file and so path_to_fshandle() is generating a fshandle that points to a file instead of a directory. This handle is cached inside libhandle, and then use for subsequent handle calls like open_by_handle(). The kernel rejects the request is the fshandle does not point to a directory.

Quoted 7 lines

Try using the mount point or a directory within the mount for the path_to_fshandle() call and see if that fixes the problem.

Cheers,

Dave.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs