2 * This Cplant(TM) source code is the property of Sandia National
5 * This Cplant(TM) source code is copyrighted by Sandia National
8 * The redistribution of this Cplant(TM) source code is subject to the
9 * terms of the GNU Lesser General Public License
10 * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
12 * Cplant(TM) Copyright 1998-2003 Sandia Corporation.
13 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14 * license for use of this work by or on behalf of the US Government.
15 * Export of this program may require a license from the United States
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Questions or comments about this library should be sent to:
37 * Sandia National Laboratories, New Mexico
39 * Albuquerque, NM 87185-1110
48 #include <stdio.h> /* for NULL */
57 #include <sys/types.h>
59 #include <sys/fcntl.h>
64 #include <sys/statvfs.h>
67 #include <sys/queue.h>
78 * Remote file system driver
79 * calls are re-directed to the initiating yod
81 #include "cplant-yod.h"
83 /* stat struct used by yod, which
84 * is not compiled with __USE_FILE_OFFSET64
86 #define __yod_stat stat
88 #define COPY_STAT(src, dest) \
90 memset((dest), 0, sizeof((*dest))); \
91 (dest)->st_dev = (src)->st_dev; \
92 (dest)->st_ino = (src)->st_ino; \
93 (dest)->st_mode = (src)->st_mode; \
94 (dest)->st_nlink = (src)->st_nlink; \
95 (dest)->st_uid = (src)->st_uid; \
96 (dest)->st_gid = (src)->st_gid; \
97 (dest)->st_rdev = (src)->st_rdev; \
98 (dest)->st_size = (src)->st_size; \
99 (dest)->st_atime = (src)->st_atime; \
100 (dest)->st_mtime = (src)->st_mtime; \
101 (dest)->st_ctime = (src)->st_ctime; \
102 (dest)->st_blksize = (src)->st_blksize; \
103 (dest)->st_blocks = (src)->st_blocks; \
104 (dest)->st_flags = (src)->st_flags; \
105 (dest)->st_gen = (src)->st_gen; \
108 #define COPY_STAT(src, dest) \
110 memset((dest), 0, sizeof((*dest))); \
111 (dest)->st_dev = (src)->st_dev; \
112 (dest)->st_ino = (src)->st_ino; \
113 (dest)->st_mode = (src)->st_mode; \
114 (dest)->st_nlink = (src)->st_nlink; \
115 (dest)->st_uid = (src)->st_uid; \
116 (dest)->st_gid = (src)->st_gid; \
117 (dest)->st_rdev = (src)->st_rdev; \
118 (dest)->st_size = (src)->st_size; \
119 (dest)->st_atime = (src)->st_atime; \
120 (dest)->st_mtime = (src)->st_mtime; \
121 (dest)->st_ctime = (src)->st_ctime; \
122 (dest)->st_blksize = (src)->st_blksize; \
123 (dest)->st_blocks = (src)->st_blocks; \
128 * Yod file identifiers format.
130 struct yod_inode_identifier {
131 dev_t dev; /* device number */
132 ino_t ino; /* i-number */
133 #ifdef HAVE_GENERATION
134 unsigned int gen; /* generation number */
139 * Driver-private i-node information we keep about local host file
143 unsigned ni_seekok : 1; /* can seek? */
144 struct yod_inode_identifier ni_ident; /* unique identifier */
145 struct file_identifier ni_fileid; /* ditto */
146 int ni_fd; /* host fildes */
147 int ni_oflags; /* flags, from open */
148 unsigned ni_nopens; /* soft ref count */
149 _SYSIO_OFF_T ni_fpos; /* current pos */
152 static int yod_inop_lookup(struct pnode *pno,
154 struct intent *intnt,
156 static int yod_inop_getattr(struct pnode *pno,
158 struct intnl_stat *stbuf);
159 static int yod_inop_setattr(struct pnode *pno,
162 struct intnl_stat *stbuf);
163 static ssize_t yod_getdirentries(struct inode *ino,
167 static int yod_inop_mkdir(struct pnode *pno, mode_t mode);
168 static int yod_inop_rmdir(struct pnode *pno);
169 static int yod_inop_symlink(struct pnode *pno, const char *data);
170 static int yod_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz);
171 static int yod_inop_open(struct pnode *pno, int flags, mode_t mode);
172 static int yod_inop_close(struct inode *ino);
173 static int yod_inop_link(struct pnode *old, struct pnode *new);
174 static int yod_inop_unlink(struct pnode *pno);
175 static int yod_inop_rename(struct pnode *old, struct pnode *new);
176 static _SYSIO_OFF_T yod_inop_pos (struct inode *ino, _SYSIO_OFF_T off);
177 static int yod_inop_read(struct inode *ino, struct ioctx *ioctx);
178 static int yod_inop_write(struct inode *ino, struct ioctx *ioctx);
179 static int yod_inop_iodone(struct ioctx *ioctx);
180 static int yod_inop_fcntl(struct inode *ino, int cmd, va_list ap);
181 static int yod_inop_sync(struct inode *ino);
182 static int yod_inop_datasync(struct inode *ino);
183 static int yod_inop_ioctl(struct inode *ino,
184 unsigned long int request,
186 static int yod_inop_mknod(struct pnode *pno, mode_t mode, dev_t dev);
188 static int yod_inop_statvfs(struct pnode *pno,
190 struct intnl_statvfs *buf);
192 static void yod_inop_gone(struct inode *ino);
194 static struct inode_ops yod_i_ops = {
223 static int yod_fsswop_mount(const char *source,
226 struct pnode *tocover,
227 struct mount **mntp);
229 static struct fssw_ops yod_fssw_ops = {
233 static void yod_fsop_gone(struct filesys *fs);
235 static struct filesys_ops yod_inodesys_ops = {
240 * Placeholder internal mount as in native driver
242 static struct mount *yod_internal_mount = NULL;
245 * Given i-node, return driver private part.
247 #define I2NI(ino) ((struct yod_inode *)((ino)->i_private))
253 yod_stat(const char *path, struct intnl_stat *buf)
256 struct __yod_stat stbuf;
258 err = stat_yod(path, &stbuf);
261 COPY_STAT(&stbuf, buf);
270 yod_fstat(int fd, struct intnl_stat *buf)
273 struct __yod_stat stbuf;
275 err = fstat_yod(fd, &stbuf);
278 COPY_STAT(&stbuf, buf);
284 * Introduce an i-node to the system.
286 static struct inode *
287 yod_i_new(struct filesys *fs, struct intnl_stat *buf)
289 struct yod_inode *nino;
292 nino = malloc(sizeof(struct yod_inode));
295 bzero(&nino->ni_ident, sizeof(nino->ni_ident));
296 nino->ni_ident.dev = buf->st_dev;
297 nino->ni_ident.ino = buf->st_ino;
298 #ifdef HAVE_GENERATION
299 nino->ni_ident.gen = buf->st_gen;
301 nino->ni_fileid.fid_data = &nino->ni_ident;
302 nino->ni_fileid.fid_len = sizeof(nino->ni_ident);
310 #ifndef AUTOMOUNT_FILE_NAME
311 buf->st_mode & S_IFMT,
313 buf->st_mode, /* all of the bits! */
325 * Initialize this driver.
332 * Capture current process umask and reset our process umask to
333 * zero. All permission bits to open/creat/setattr are absolute --
334 * They've already had a umask applied, when appropriate.
336 _sysio_umask = syscall(SYS_umask, 0);
338 return _sysio_fssw_register("yod", &yod_fssw_ops);
342 * Create private, internal, view of the hosts name space.
345 create_internal_namespace()
349 struct inode *rootino;
350 struct pnode_base *rootpb;
351 static struct qstr noname = { NULL, 0, 0 };
353 struct intnl_stat stbuf;
355 if (yod_internal_mount) {
363 * We maintain an artificial, internal, name space in order to
364 * have access to fully qualified path names in the various routines.
365 * Initialize that name space now.
370 fs = _sysio_fs_new(&yod_inodesys_ops, 0, NULL);
379 err = yod_stat("/", &stbuf);
382 rootino = yod_i_new(fs, &stbuf);
389 * Generate base path-node for root.
391 rootpb = _sysio_pb_new(&noname, NULL, rootino);
398 * Mount it. This name space is disconnected from the
399 * rest of the system -- Only available within this driver.
401 err = _sysio_do_mount(fs, rootpb, 0, NULL, &mnt);
405 yod_internal_mount = mnt;
409 if (_sysio_do_unmount(mnt) != 0)
416 _sysio_pb_gone(rootpb);
426 yod_fsswop_mount(const char *source,
428 const void *data __IS_UNUSED,
429 struct pnode *tocover,
433 struct nameidata nameidata;
437 * Caller must use fully qualified path names when specifying
443 if (!yod_internal_mount) {
444 err = create_internal_namespace();
450 * Lookup the source in the internally maintained name space.
452 ND_INIT(&nameidata, 0, source, yod_internal_mount->mnt_root, NULL);
453 err = _sysio_path_walk(yod_internal_mount->mnt_root, &nameidata);
458 * Have path-node specified by the given source argument. Let the
459 * system finish the job, now.
462 _sysio_do_mount(yod_internal_mount->mnt_fs,
463 nameidata.nd_pno->p_base,
468 * Release the internal name space pnode and clean up any
469 * aliases we might have generated. We really don't need to cache them
470 * as they are only used at mount time..
472 P_RELE(nameidata.nd_pno);
473 (void )_sysio_p_prune(yod_internal_mount->mnt_root);
476 FS_REF(yod_internal_mount->mnt_fs);
483 yod_i_invalid(struct inode *inop, struct intnl_stat stbuf)
486 * Validate passed in inode against stat struct info
488 struct yod_inode *nino = I2NI(inop);
490 if ((nino->ni_ident.dev != stbuf.st_dev ||
491 nino->ni_ident.ino != stbuf.st_ino ||
492 #ifdef HAVE_GENERATION
493 nino->ni_ident.gen != stbuf.st_gen ||
495 ((inop)->i_mode & S_IFMT) != (stbuf.st_mode & S_IFMT)) ||
496 (((inop)->i_rdev != stbuf.st_rdev) &&
497 (S_ISCHR((inop)->i_mode) || S_ISBLK((inop)->i_mode))))
504 * Find, and validate, or create i-node by host-relative path. Returned i-node
508 yod_iget(struct filesys *fs,
515 struct intnl_stat stbuf;
516 struct yod_inode_identifier ident;
517 struct file_identifier fileid;
522 err = yod_stat(path, &stbuf);
532 if (!yod_i_invalid(*inop, stbuf))
541 * I-node is not already known. Find or create it.
543 bzero(&ident, sizeof(ident));
544 ident.dev = stbuf.st_dev;
545 ident.ino = stbuf.st_ino;
546 #ifdef HAVE_GENERATION
547 ident.gen = stbuf.st_gen;
549 fileid.fid_data = &ident;
550 fileid.fid_len = sizeof(ident);
551 ino = _sysio_i_find(fs, &fileid);
554 * Insertion was forced but it's already present!
556 if (yod_i_invalid(ino, stbuf)) {
558 * Cached inode has stale attrs
559 * make way for the new one
562 _sysio_i_undead(ino);
566 * OK to reuse cached inode
572 ino = yod_i_new(fs, &stbuf);
583 * Look up named object in host's name space by path.
586 yod_path_lookup(struct filesys *fs, const char *path, struct inode **inop)
589 return yod_iget(fs, path, inop, 0);
593 * Look up object by it's path node.
596 yod_i_lookup(struct filesys *fs, struct pnode_base *pb, struct inode **inop)
601 path = _sysio_pb_path(pb, '/');
604 err = yod_path_lookup(fs, path, inop);
610 yod_inop_lookup(struct pnode *pno,
612 struct intent *intnt __IS_UNUSED,
613 const char *path __IS_UNUSED)
617 *inop = pno->p_base->pb_ino;
620 * Don't have an inode yet. Because we translate everything back to
621 * a single name space for the host, we will assume the object the
622 * caller is looking for has no existing alias in our internal
623 * name space. We don't see the same file on different mounts in the
624 * underlying host FS as the same file.
626 * The file identifier *will* be unique. It's got to have a different
629 err = yod_i_lookup(pno->p_mount->mnt_fs, pno->p_base, inop);
636 yod_inop_getattr(struct pnode *pno, struct inode *ino, struct intnl_stat *stbuf)
642 if (!ino || I2NI(ino)->ni_fd < 0) {
643 path = _sysio_pb_path(pno->p_base, '/');
649 ? yod_stat(path, stbuf)
650 : yod_fstat(I2NI(ino)->ni_fd, stbuf);
657 yod_inop_setattr(struct pnode *pno,
660 struct intnl_stat *stbuf)
664 struct intnl_stat st;
668 fd = ino ? I2NI(ino)->ni_fd : -1;
669 if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {
672 path = _sysio_pb_path(pno->p_base, '/');
678 * Get current status for undo.
682 ? yod_stat(path, &st)
683 : yod_fstat(fd, &st);
687 if (mask & SETATTR_MODE) {
691 * Alter permissions attribute.
693 mode = stbuf->st_mode & 07777;
694 err = chmod_yod(path, mode);
697 mask &= ~SETATTR_MODE;
699 if (mask & (SETATTR_UID|SETATTR_GID)) {
702 * Alter owner and/or group identifiers.
704 err = chown_yod(path,
713 mask &= ~(SETATTR_UID|SETATTR_GID);
714 else if (mask & SETATTR_LEN) {
716 * Do the truncate last. It can't be undone.
719 ? truncate_yod(path, stbuf->st_size)
720 : ftruncate_yod(fd, stbuf->st_size));
725 * Undo after error. Some or all of this might not work... We
728 if (mask & (SETATTR_UID|SETATTR_GID)) {
729 (void )chown_yod(path,
737 if (mask & SETATTR_MODE) {
738 chmod_yod(path, st.st_mode & 07777);
747 yod_getdirentries(struct inode *ino,
752 struct yod_inode *nino = I2NI(ino);
756 assert(nino->ni_fd >= 0);
759 if (*basep != nino->ni_fpos &&
760 (result = lseek_yod(nino->ni_fd,
764 nino->ni_fpos = result;
765 cc = getdirentries_yod(nino->ni_fd, buf, nbytes, &result);
773 yod_inop_mkdir(struct pnode *pno, mode_t mode)
778 path = _sysio_pb_path(pno->p_base, '/');
782 err = mkdir_yod(path, mode);
788 yod_inop_rmdir(struct pnode *pno)
793 path = _sysio_pb_path(pno->p_base, '/');
797 err = rmdir_yod(path);
803 yod_inop_symlink(struct pnode *pno, const char *data)
808 path = _sysio_pb_path(pno->p_base, '/');
812 err = symlink_yod(data, path);
818 yod_inop_readlink(struct pnode *pno __IS_UNUSED,
819 char *buf __IS_UNUSED,
820 size_t bufsiz __IS_UNUSED)
827 yod_inop_open(struct pnode *pno, int flags, mode_t mode)
829 struct yod_inode *nino;
833 path = _sysio_pb_path(pno->p_base, '/');
838 * Whether the file is already open, or not, makes no difference.
839 * Want to always give the host OS a chance to authorize in case
840 * something has changed underneath us.
842 if (flags & O_WRONLY) {
844 * Promote write-only attempt to RW.
849 fd = open_yod(path, flags, mode);
850 if (!pno->p_base->pb_ino && fd >= 0) {
854 * Success but we need to return an i-node.
857 yod_iget(pno->p_mount->mnt_fs,
859 &pno->p_base->pb_ino,
862 (void )close_yod(fd);
873 * Remember this new open.
875 nino = I2NI(pno->p_base->pb_ino);
877 assert(nino->ni_nopens);
879 if (nino->ni_fd >= 0) {
880 if ((nino->ni_oflags & O_RDWR) ||
881 (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {
885 (void )close_yod(fd);
888 (void )close_yod(nino->ni_fd);
891 * Invariant; First open. Must init.
897 * Need to know whether we can seek on this
901 lseek_yod(nino->ni_fd, 0, SEEK_CUR) != 0 ? 0 : 1;
907 yod_inop_close(struct inode *ino)
909 struct yod_inode *nino = I2NI(ino);
915 assert(nino->ni_nopens);
916 if (--nino->ni_nopens)
919 err = close_yod(nino->ni_fd);
929 yod_inop_link(struct pnode *old, struct pnode *new)
936 opath = _sysio_pb_path(old->p_base, '/');
937 npath = _sysio_pb_path(new->p_base, '/');
938 if (!(opath && npath)) {
943 err = link_yod(opath, npath);
955 yod_inop_unlink(struct pnode *pno)
960 path = _sysio_pb_path(pno->p_base, '/');
965 * For this driver, unlink is easy with open files. Since the
966 * file remains open to the system, too, the descriptors are still
969 * Other drivers will have some difficulty here as the entry in the
970 * file system name space must be removed without sacrificing access
971 * to the file itself. In NFS this is done with a mechanism referred
972 * to as a `silly delete'. The file is moved to a temporary name
973 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some
974 * unique characters) in order to simulate the proper semantic.
976 if (unlink_yod(path) != 0)
983 * A helper function performing the real IO operation work.
985 * We don't really have async IO. We'll just perform the function
989 doio(ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, struct yod_inode *),
992 struct yod_inode *nino = I2NI(ioctx->ioctx_ino);
995 _sysio_doio(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
996 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
997 (ssize_t (*)(void *, size_t,
998 _SYSIO_OFF_T, void *))f,
1000 if (ioctx->ioctx_cc < 0) {
1001 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1002 ioctx->ioctx_cc = -1;
1005 nino->ni_fpos += ioctx->ioctx_cc;
1006 ioctx->ioctx_done = 1;
1011 yod_read_simple(void *buf,
1014 struct yod_inode *nino)
1016 if (off != nino->ni_fpos) {
1019 rtn = lseek_yod(nino->ni_fd, off, SEEK_SET);
1022 nino->ni_fpos = rtn;
1024 return read_yod(nino->ni_fd, buf, nbytes);
1028 yod_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1031 return doio(yod_read_simple, ioctx);
1035 yod_inop_rename(struct pnode *old, struct pnode *new)
1038 char *opath, *npath;
1040 opath = _sysio_pb_path(old->p_base, '/');
1041 npath = _sysio_pb_path(new->p_base, '/');
1042 if (!(opath && npath)) {
1047 err = rename_yod(opath, npath);
1059 yod_write_simple(void *buf,
1062 struct yod_inode *nino)
1065 if (off != nino->ni_fpos) {
1068 rtn = lseek_yod(nino->ni_fd, off, SEEK_SET);
1071 nino->ni_fpos = rtn;
1073 return write_yod(nino->ni_fd, buf, nbytes);
1077 yod_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1080 return doio(yod_write_simple, ioctx);
1084 yod_inop_pos(struct inode *ino, _SYSIO_OFF_T off)
1086 struct yod_inode *nino = I2NI(ino);
1089 err = lseek_yod(nino->ni_fd, off, SEEK_SET);
1090 return err < 0 ? err : off;
1094 yod_inop_iodone(struct ioctx *ioctxp __IS_UNUSED)
1098 * It's always done in this driver. It completed when posted.
1104 yod_inop_fcntl(struct inode *ino __IS_UNUSED, int cmd, va_list ap __IS_UNUSED)
1108 case F_DUPFD: /* do something to the ino */
1119 yod_inop_mknod(struct pnode *pno __IS_UNUSED,
1120 mode_t mode __IS_UNUSED,
1121 dev_t dev __IS_UNUSED)
1127 #ifdef _HAVE_STATVFS
1129 yod_inop_statvfs(struct pnode *pno,
1131 struct intnl_statvfs *buf)
1138 if (!ino || I2NI(ino)->ni_fd < 0) {
1139 path = _sysio_pb_path(pno->p_base, '/');
1145 * The syscall interface does not support SYS_fstatvfs.
1146 * Should possibly return ENOSYS, but thought it
1147 * better to use SYS_fstatfs and fill in as much of
1148 * the statvfs structure as possible. This allows
1149 * for more of a test of the sysio user interface.
1153 ? statfs_yod(path, &fs)
1154 : fstatfs_yod(I2NI(ino)->ni_fd, &fs);
1160 buf->f_bsize = fs.f_bsize; /* file system block size */
1161 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1162 buf->f_blocks = fs.f_blocks;
1163 buf->f_bfree = fs.f_bfree;
1164 buf->f_bavail = fs.f_bavail;
1165 buf->f_files = fs.f_files; /* Total number serial numbers */
1166 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1167 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1168 buf->f_fsid = fs.f_fsid.__val[1];
1169 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1170 buf->f_namemax = fs.f_namelen;
1176 yod_inop_sync(struct inode *ino)
1179 assert(I2NI(ino)->ni_fd >= 0);
1181 return fsync_yod(I2NI(ino)->ni_fd);
1185 yod_inop_datasync(struct inode *ino)
1188 assert(I2NI(ino)->ni_fd >= 0);
1190 return fsync_yod(I2NI(ino)->ni_fd);
1194 yod_inop_ioctl(struct inode *ino __IS_UNUSED,
1195 unsigned long int request __IS_UNUSED,
1196 va_list ap __IS_UNUSED)
1200 * I'm lazy. Maybe implemented later.
1207 yod_inop_gone(struct inode *ino)
1209 struct yod_inode *nino = I2NI(ino);
1212 (void )close(nino->ni_fd);
1213 free(ino->i_private);
1217 yod_fsop_gone(struct filesys *fs __IS_UNUSED)
1221 * Do nothing. There is no private part maintained for the
1222 * yod file interface.