From: Nathan Scott Date: Mon, 15 Jan 2001 05:56:08 +0000 (+0000) Subject: cmd/xfsprogs/handle/Makefile 1.1 Renamed to cmd/xfsprogs/libhandle/Makefile X-Git-Tag: v1.1.0~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e9688c1de9e390d287c54122b3df9ddf128a655d;p=thirdparty%2Fxfsprogs-dev.git cmd/xfsprogs/handle/Makefile 1.1 Renamed to cmd/xfsprogs/libhandle/Makefile --- diff --git a/libhandle/Makefile b/libhandle/Makefile new file mode 100644 index 000000000..1a69d335b --- /dev/null +++ b/libhandle/Makefile @@ -0,0 +1,45 @@ +# +# Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# +# Further, this software is distributed without any warranty that it is +# free of the rightful claim of any third person regarding infringement +# or the like. Any license provided herein, whether implied or +# otherwise, applies only to this software file. Patent licenses, if +# any, provided herein do not apply to combinations of this program with +# other software, or any other product whatsoever. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write the Free Software Foundation, Inc., 59 +# Temple Place - Suite 330, Boston MA 02111-1307, USA. +# +# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, +# Mountain View, CA 94043, or: +# +# http://www.sgi.com +# +# For further information regarding this notice, see: +# +# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ +# + +TOPDIR = .. +include $(TOPDIR)/include/builddefs + +STATICLIBTARGET = libhandle.a +CFILES = handle.c jdm.c + +default: $(STATICLIBTARGET) + +include $(BUILDRULES) + +install: default + #$(INSTALL) -m 755 -d $(XFS_CMDS_LIB_DIR) + #$(INSTALL) -m 755 $(STATICLIBTARGET) $(XFS_CMDS_LIB_DIR) diff --git a/libhandle/handle.c b/libhandle/handle.c new file mode 100644 index 000000000..390912975 --- /dev/null +++ b/libhandle/handle.c @@ -0,0 +1,318 @@ +/* + * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., 59 + * Temple Place - Suite 330, Boston MA 02111-1307, USA. + * + * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, + * Mountain View, CA 94043, or: + * + * http://www.sgi.com + * + * For further information regarding this notice, see: + * + * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "handle.h" +#include + + +/* just pick a value we know is more than big enough */ +#define MAXHANSIZ 64 + +/* + * The actual content of a handle is supposed to be opaque here. + * But, to do handle_to_fshandle, we need to know what it is. Sigh. + * However we can get by with knowing only that the first 8 bytes of + * a file handle are the file system ID, and that a file system handle + * consists of only those 8 bytes. + */ + +#define FSIDSIZE 8 + +typedef union { + int fd; + char *path; +} comarg_t; + +int obj_to_handle ( + int fsfd, + int opcode, + comarg_t obj, + void **hanp, + size_t *hlen); + +struct fdhash { + int fsfd; + char fsh[FSIDSIZE]; + struct fdhash *fnxt; +}; + +struct fdhash *fdhash_head = NULL; + +int +path_to_fshandle ( + char *path, /* input, path to convert */ + void **hanp, /* output, pointer to data */ + size_t *hlen) /* output, size of returned data */ +{ + int result; + int fd; + comarg_t obj; + struct fdhash *fdhp; + + fd = open(path, O_RDONLY); + + if (fd < 0) { + perror(path); + exit(1); + } + + obj.path = path; + + result = obj_to_handle (fd, XFS_IOC_PATH_TO_FSHANDLE, + obj, hanp, hlen); + + if (result >= 0) { + fdhp = malloc(sizeof(struct fdhash)); + + if (fdhp == NULL) { + errno = ENOMEM; + return -1; + } + + fdhp->fsfd = fd; + fdhp->fnxt = NULL; + + memcpy(fdhp->fsh, *hanp, FSIDSIZE); + + if (fdhash_head) + fdhash_head->fnxt = fdhp; + else + fdhash_head = fdhp; + } + + return result; +} + + +int +path_to_handle ( + char *path, /* input, path to convert */ + void **hanp, /* output, pointer to data */ + size_t *hlen) /* output, size of returned data */ +{ + int fd; + int result; + comarg_t obj; + + fd = open(path, O_RDONLY); + + if (fd < 0) { + perror(path); + exit(1); + } + + obj.path = path; + + result = obj_to_handle (fd, XFS_IOC_PATH_TO_HANDLE, obj, hanp, hlen); + + close(fd); + + return result; +} + + +int +fd_to_handle ( + int fd, /* input, file descriptor */ + void **hanp, /* output, pointer to data */ + size_t *hlen) /* output, size of returned data */ +{ + comarg_t obj; + + obj.fd = fd; + + return obj_to_handle (fd, XFS_IOC_FD_TO_HANDLE, obj, hanp, hlen); +} + + +int +handle_to_fshandle ( + void *hanp, + size_t hlen, + void **fshanp, + size_t *fshlen) +{ + if (hlen < FSIDSIZE) + return EINVAL; + + *fshanp = malloc (FSIDSIZE); + + if (*fshanp == NULL) + return ENOMEM; + + *fshlen = FSIDSIZE; + + memcpy(*fshanp, hanp, FSIDSIZE); + + return 0; +} + + +int +obj_to_handle ( + int fsfd, + int opcode, + comarg_t obj, + void **hanp, + size_t *hlen) +{ + char hbuf [MAXHANSIZ]; + int ret; + xfs_fsop_handlereq_t hreq; + + if (opcode == XFS_IOC_FD_TO_HANDLE) { + hreq.fd = obj.fd; + hreq.path = NULL; + } else { + hreq.fd = 0; + hreq.path = obj.path; + } + + hreq.oflags = 0; + hreq.ihandle = NULL; + hreq.ihandlen = 0; + hreq.ohandle = hbuf; + hreq.ohandlen = hlen; + + ret = (int) ioctl(fsfd, opcode, &hreq); + + if (ret) + return ret; + + *hanp = malloc(*hlen); + + if (*hanp == NULL) { + errno = ENOMEM; + return -1; + } + + memcpy(*hanp, hbuf, (int) *hlen); + + return 0; +} + + +int +open_by_handle ( + void *hanp, + size_t hlen, + int rw) +{ + int fd; + int result; + struct fdhash *fdhp; + xfs_fsop_handlereq_t hreq; + + fd = -1; + + for (fdhp = fdhash_head; fdhp != NULL; fdhp = fdhp->fnxt) { + + if (memcmp(fdhp->fsh, hanp, FSIDSIZE) == 0) { + fd = fdhp->fsfd; + break; + } + } + + if (fd < 0) { + errno = EBADF; + return -1; + } + + hreq.fd = 0; + hreq.path = NULL; + hreq.oflags = rw; + hreq.ihandle = hanp; + hreq.ihandlen = hlen; + hreq.ohandle = NULL; + hreq.ohandlen = NULL; + + result = ioctl(fd, XFS_IOC_OPEN_BY_HANDLE, &hreq); + + return result; +} + +int +readlink_by_handle ( + void *hanp, + size_t hlen, + void *buf, + size_t bufsiz) +{ + int fd; + struct fdhash *fdhp; + xfs_fsop_handlereq_t hreq; + + + fd = -1; + + for (fdhp = fdhash_head; fdhp != NULL; fdhp = fdhp->fnxt) { + + if (memcmp(fdhp->fsh, hanp, FSIDSIZE) == 0) { + fd = fdhp->fsfd; + break; + } + } + + if (fd < 0) { + errno = EBADF; + return -1; + } + + hreq.fd = 0; + hreq.path = NULL; + hreq.oflags = 0; + hreq.ihandle = hanp; + hreq.ihandlen = hlen; + hreq.ohandle = buf; + hreq.ohandlen = &bufsiz; + + return (int) ioctl(fd, XFS_IOC_READLINK_BY_HANDLE, &hreq); +} + +/*ARGSUSED*/ +void +free_handle ( + void *hanp, + size_t hlen) +{ + free (hanp); +} diff --git a/libhandle/jdm.c b/libhandle/jdm.c new file mode 100644 index 000000000..aae9be18a --- /dev/null +++ b/libhandle/jdm.c @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write the Free Software Foundation, Inc., 59 + * Temple Place - Suite 330, Boston MA 02111-1307, USA. + * + * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, + * Mountain View, CA 94043, or: + * + * http://www.sgi.com + * + * For further information regarding this notice, see: + * + * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ + */ + +#include +#include +#include + +/* internal fshandle - typecast to a void for external use */ +#define FSHANDLE_SZ 8 +typedef struct fshandle { + char fsh_space[FSHANDLE_SZ]; +} fshandle_t; + +/* private file handle - for use by open_by_handle */ +#define FILEHANDLE_SZ 24 +#define FILEHANDLE_SZ_FOLLOWING 14 +#define FILEHANDLE_SZ_PAD 2 +typedef struct filehandle { + fshandle_t fh_fshandle; /* handle of fs containing this inode */ + int16_t fh_sz_following; /* bytes in handle after this member */ + char fh_pad[FILEHANDLE_SZ_PAD]; /* padding, must be zeroed */ + __uint32_t fh_gen; /* generation count */ + xfs_ino_t fh_ino; /* 64 bit ino */ +} filehandle_t; + +static void +jdm_fill_filehandle( filehandle_t *handlep, + fshandle_t *fshandlep, + xfs_bstat_t *statp ) +{ + handlep->fh_fshandle = *fshandlep; + handlep->fh_sz_following = FILEHANDLE_SZ_FOLLOWING; + bzero(handlep->fh_pad, FILEHANDLE_SZ_PAD); + handlep->fh_gen = statp->bs_gen; + handlep->fh_ino = statp->bs_ino; +} + +jdm_fshandle_t * +jdm_getfshandle( char *mntpnt ) +{ + fshandle_t *fshandlep; + size_t fshandlesz; + char resolved[MAXPATHLEN]; + + /* sanity checks */ + ASSERT( sizeof( fshandle_t ) == FSHANDLE_SZ ); + ASSERT( sizeof( filehandle_t ) == FILEHANDLE_SZ ); + ASSERT( sizeof( filehandle_t ) + - + offsetofmember( filehandle_t, fh_pad ) + == + FILEHANDLE_SZ_FOLLOWING ); + ASSERT( sizeofmember( filehandle_t, fh_pad ) == FILEHANDLE_SZ_PAD ); + ASSERT( FILEHANDLE_SZ_PAD == sizeof( int16_t )); + + fshandlep = 0; /* for lint */ + fshandlesz = sizeof( *fshandlep ); + + if (!realpath( mntpnt, resolved )) + return NULL; + + if (path_to_fshandle( resolved, ( void ** )&fshandlep, &fshandlesz )) + return NULL; + + assert( fshandlesz == sizeof( *fshandlep )); + + return ( jdm_fshandle_t * )fshandlep; +} + +intgen_t +jdm_open( jdm_fshandle_t *fshp, xfs_bstat_t *statp, intgen_t oflags ) +{ + register fshandle_t *fshandlep = ( fshandle_t * )fshp; + filehandle_t filehandle; + intgen_t fd; + + jdm_fill_filehandle( &filehandle, fshandlep, statp ); + fd = open_by_handle( ( void * )&filehandle, + sizeof( filehandle ), + oflags ); + return fd; +} + +intgen_t +jdm_readlink( jdm_fshandle_t *fshp, + xfs_bstat_t *statp, + char *bufp, size_t bufsz ) +{ + register fshandle_t *fshandlep = ( fshandle_t * )fshp; + filehandle_t filehandle; + intgen_t rval; + + jdm_fill_filehandle( &filehandle, fshandlep, statp ); + rval = readlink_by_handle( ( void * )&filehandle, + sizeof( filehandle ), + ( void * )bufp, + bufsz ); + return rval; +} + +#ifdef EXTATTR +intgen_t +jdm_attr_multi( jdm_fshandle_t *fshp, + xfs_bstat_t *statp, + char *bufp, int rtrvcnt, int flags) +{ + register fshandle_t *fshandlep = ( fshandle_t * )fshp; + filehandle_t filehandle; + intgen_t rval; + + jdm_fill_filehandle( &filehandle, fshandlep, statp ); + rval = attr_multi_by_handle ( ( void * )&filehandle, + sizeof( filehandle ), + (void *) bufp, + rtrvcnt, flags); + return rval; +} + +intgen_t +jdm_attr_list( jdm_fshandle_t *fshp, + xfs_bstat_t *statp, + char *bufp, size_t bufsz, int flags, + struct attrlist_cursor *cursor) +{ + register fshandle_t *fshandlep = ( fshandle_t * )fshp; + filehandle_t filehandle; + intgen_t rval; + + jdm_fill_filehandle( &filehandle, fshandlep, statp ); + rval = attr_list_by_handle (( void * )&filehandle, + sizeof( filehandle ), + bufp, bufsz, flags, cursor); + return rval; +} +#endif