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 */
56 #include <sys/syscall.h>
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_filldirentries(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, int *rtn);
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));
297 nino->ni_ident.dev = buf->st_dev;
298 nino->ni_ident.ino = buf->st_ino;
299 #ifdef HAVE_GENERATION
300 nino->ni_ident.gen = buf->st_gen;
302 nino->ni_fileid.fid_data = &nino->ni_ident;
303 nino->ni_fileid.fid_len = sizeof(nino->ni_ident);
321 * Initialize this driver.
328 * Capture current process umask and reset our process umask to
329 * zero. All permission bits to open/creat/setattr are absolute --
330 * They've already had a umask applied, when appropriate.
332 _sysio_umask = syscall(SYS_umask, 0);
334 return _sysio_fssw_register("yod", &yod_fssw_ops);
338 * Create private, internal, view of the hosts name space.
341 create_internal_namespace()
345 struct inode *rootino;
346 struct pnode_base *rootpb;
347 static struct qstr noname = { NULL, 0, 0 };
349 struct intnl_stat stbuf;
351 if (yod_internal_mount) {
359 * We maintain an artificial, internal, name space in order to
360 * have access to fully qualified path names in the various routines.
361 * Initialize that name space now.
366 fs = _sysio_fs_new(&yod_inodesys_ops, 0, NULL);
375 err = yod_stat("/", &stbuf);
378 rootino = yod_i_new(fs, &stbuf);
385 * Generate base path-node for root.
387 rootpb = _sysio_pb_new(&noname, NULL, rootino);
394 * Mount it. This name space is disconnected from the
395 * rest of the system -- Only available within this driver.
397 err = _sysio_do_mount(fs, rootpb, 0, NULL, &mnt);
401 yod_internal_mount = mnt;
405 if (_sysio_do_unmount(mnt) != 0)
412 _sysio_pb_gone(rootpb);
421 yod_fsswop_mount(const char *source,
423 const void *data __IS_UNUSED,
424 struct pnode *tocover,
428 struct nameidata nameidata;
432 * Caller must use fully qualified path names when specifying
438 if (!yod_internal_mount) {
439 err = create_internal_namespace();
445 * Lookup the source in the internally maintained name space.
447 ND_INIT(&nameidata, 0, source, yod_internal_mount->mnt_root, NULL);
448 err = _sysio_path_walk(yod_internal_mount->mnt_root, &nameidata);
453 * Have path-node specified by the given source argument. Let the
454 * system finish the job, now.
457 _sysio_do_mount(yod_internal_mount->mnt_fs,
458 nameidata.nd_pno->p_base,
463 * Release the internal name space pnode and clean up any
464 * aliases we might have generated. We really don't need to cache them
465 * as they are only used at mount time..
467 P_RELE(nameidata.nd_pno);
468 (void )_sysio_p_prune(yod_internal_mount->mnt_root);
471 FS_REF(yod_internal_mount->mnt_fs);
478 yod_i_invalid(struct inode *inop, struct intnl_stat *stat)
481 * Validate passed in inode against stat struct info
483 struct yod_inode *nino = I2NI(inop);
485 if ((nino->ni_ident.dev != stat->st_dev ||
486 nino->ni_ident.ino != stat->st_ino ||
487 #ifdef HAVE_GENERATION
488 nino->ni_ident.gen != stat->st_gen ||
490 ((inop)->i_stbuf.st_mode & S_IFMT) != (stat->st_mode & S_IFMT)) ||
491 (((inop)->i_stbuf.st_rdev != stat->st_rdev) &&
492 (S_ISCHR((inop)->i_stbuf.st_mode) ||
493 S_ISBLK((inop)->i_stbuf.st_mode))))
500 * Find, and validate, or create i-node by host-relative path. Returned i-node
504 yod_iget(struct filesys *fs,
511 struct intnl_stat stbuf;
512 struct yod_inode_identifier ident;
513 struct file_identifier fileid;
518 err = yod_stat(path, &stbuf);
528 if (!yod_i_invalid(*inop, &stbuf))
537 * I-node is not already known. Find or create it.
539 bzero(&ident, sizeof(ident));
540 ident.dev = stbuf.st_dev;
541 ident.ino = stbuf.st_ino;
542 #ifdef HAVE_GENERATION
543 ident.gen = stbuf.st_gen;
545 fileid.fid_data = &ident;
546 fileid.fid_len = sizeof(ident);
547 ino = _sysio_i_find(fs, &fileid);
550 * Insertion was forced but it's already present!
552 if (yod_i_invalid(ino, &stbuf)) {
554 * Cached inode has stale attrs
555 * make way for the new one
558 _sysio_i_undead(ino);
562 * OK to reuse cached inode
568 ino = yod_i_new(fs, &stbuf);
579 * Look up named object in host's name space by path.
582 yod_path_lookup(struct filesys *fs, const char *path, struct inode **inop)
585 return yod_iget(fs, path, inop, 0);
589 * Look up object by it's path node.
592 yod_i_lookup(struct filesys *fs, struct pnode_base *pb, struct inode **inop)
597 path = _sysio_pb_path(pb, '/');
600 err = yod_path_lookup(fs, path, inop);
606 yod_inop_lookup(struct pnode *pno,
608 struct intent *intnt __IS_UNUSED,
609 const char *path __IS_UNUSED)
613 *inop = pno->p_base->pb_ino;
616 * Don't have an inode yet. Because we translate everything back to
617 * a single name space for the host, we will assume the object the
618 * caller is looking for has no existing alias in our internal
619 * name space. We don't see the same file on different mounts in the
620 * underlying host FS as the same file.
622 * The file identifier *will* be unique. It's got to have a different
625 err = yod_i_lookup(pno->p_mount->mnt_fs, pno->p_base, inop);
632 yod_inop_getattr(struct pnode *pno, struct inode *ino, struct intnl_stat *stbuf)
638 if (!ino || I2NI(ino)->ni_fd < 0) {
639 path = _sysio_pb_path(pno->p_base, '/');
645 ? yod_stat(path, stbuf)
646 : yod_fstat(I2NI(ino)->ni_fd, stbuf);
653 yod_inop_setattr(struct pnode *pno,
656 struct intnl_stat *stbuf)
660 struct intnl_stat st;
664 fd = ino ? I2NI(ino)->ni_fd : -1;
665 if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {
668 path = _sysio_pb_path(pno->p_base, '/');
674 * Get current status for undo.
678 ? yod_stat(path, &st)
679 : yod_fstat(fd, &st);
683 if (mask & SETATTR_MODE) {
687 * Alter permissions attribute.
689 mode = stbuf->st_mode & 07777;
690 err = chmod_yod(path, mode);
693 mask &= ~SETATTR_MODE;
695 if (mask & (SETATTR_UID|SETATTR_GID)) {
698 * Alter owner and/or group identifiers.
700 err = chown_yod(path,
709 mask &= ~(SETATTR_UID|SETATTR_GID);
710 else if (mask & SETATTR_LEN) {
712 * Do the truncate last. It can't be undone.
715 ? truncate_yod(path, stbuf->st_size)
716 : ftruncate_yod(fd, stbuf->st_size));
721 * Undo after error. Some or all of this might not work... We
724 if (mask & (SETATTR_UID|SETATTR_GID)) {
725 (void )chown_yod(path,
733 if (mask & SETATTR_MODE) {
734 chmod_yod(path, st.st_mode & 07777);
743 yod_filldirentries(struct inode *ino,
748 struct yod_inode *nino = I2NI(ino);
752 assert(nino->ni_fd >= 0);
755 if (*basep != nino->ni_fpos &&
756 (result = lseek_yod(nino->ni_fd,
760 nino->ni_fpos = result;
761 memset(buf, 0, nbytes);
763 * This is almost certainly broken. The resulting position parameter
764 * points to the block just filled, not the next.
766 cc = getdirentries_yod(nino->ni_fd, buf, nbytes, &result);
769 nino->ni_fpos = *posp = result;
774 yod_inop_mkdir(struct pnode *pno, mode_t mode)
779 path = _sysio_pb_path(pno->p_base, '/');
783 err = mkdir_yod(path, mode);
789 yod_inop_rmdir(struct pnode *pno)
794 path = _sysio_pb_path(pno->p_base, '/');
798 err = rmdir_yod(path);
804 yod_inop_symlink(struct pnode *pno, const char *data)
809 path = _sysio_pb_path(pno->p_base, '/');
813 err = symlink_yod(data, path);
819 yod_inop_readlink(struct pnode *pno __IS_UNUSED,
820 char *buf __IS_UNUSED,
821 size_t bufsiz __IS_UNUSED)
828 yod_inop_open(struct pnode *pno, int flags, mode_t mode)
830 struct yod_inode *nino;
834 path = _sysio_pb_path(pno->p_base, '/');
839 * Whether the file is already open, or not, makes no difference.
840 * Want to always give the host OS a chance to authorize in case
841 * something has changed underneath us.
843 if (flags & O_WRONLY) {
845 * Promote write-only attempt to RW.
850 fd = open_yod(path, flags, mode);
851 if (!pno->p_base->pb_ino && fd >= 0) {
855 * Success but we need to return an i-node.
858 yod_iget(pno->p_mount->mnt_fs,
860 &pno->p_base->pb_ino,
863 (void )close_yod(fd);
874 * Remember this new open.
876 nino = I2NI(pno->p_base->pb_ino);
878 assert(nino->ni_nopens);
880 if (nino->ni_fd >= 0) {
881 if ((nino->ni_oflags & O_RDWR) ||
882 (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {
886 (void )close_yod(fd);
889 (void )close_yod(nino->ni_fd);
892 * Invariant; First open. Must init.
898 * Need to know whether we can seek on this
902 lseek_yod(nino->ni_fd, 0, SEEK_CUR) != 0 ? 0 : 1;
908 yod_inop_close(struct inode *ino)
910 struct yod_inode *nino = I2NI(ino);
916 assert(nino->ni_nopens);
917 if (--nino->ni_nopens)
920 err = close_yod(nino->ni_fd);
930 yod_inop_link(struct pnode *old, struct pnode *new)
937 opath = _sysio_pb_path(old->p_base, '/');
938 npath = _sysio_pb_path(new->p_base, '/');
939 if (!(opath && npath)) {
944 err = link_yod(opath, npath);
956 yod_inop_unlink(struct pnode *pno)
961 path = _sysio_pb_path(pno->p_base, '/');
966 * For this driver, unlink is easy with open files. Since the
967 * file remains open to the system, too, the descriptors are still
970 * Other drivers will have some difficulty here as the entry in the
971 * file system name space must be removed without sacrificing access
972 * to the file itself. In NFS this is done with a mechanism referred
973 * to as a `silly delete'. The file is moved to a temporary name
974 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some
975 * unique characters) in order to simulate the proper semantic.
977 if (unlink_yod(path) != 0)
984 * A helper function performing the real IO operation work.
986 * We don't really have async IO. We'll just perform the function
990 doio(ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, struct yod_inode *),
993 struct yod_inode *nino = I2NI(ioctx->ioctx_ino);
996 _sysio_doio(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
997 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
998 (ssize_t (*)(void *, size_t,
999 _SYSIO_OFF_T, void *))f,
1001 if (ioctx->ioctx_cc < 0) {
1002 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1003 ioctx->ioctx_cc = -1;
1006 nino->ni_fpos += ioctx->ioctx_cc;
1007 ioctx->ioctx_done = 1;
1012 yod_read_simple(void *buf,
1015 struct yod_inode *nino)
1017 if (off != nino->ni_fpos) {
1020 rtn = lseek_yod(nino->ni_fd, off, SEEK_SET);
1023 nino->ni_fpos = rtn;
1025 return read_yod(nino->ni_fd, buf, nbytes);
1029 yod_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1032 return doio(yod_read_simple, ioctx);
1036 yod_inop_rename(struct pnode *old, struct pnode *new)
1039 char *opath, *npath;
1041 opath = _sysio_pb_path(old->p_base, '/');
1042 npath = _sysio_pb_path(new->p_base, '/');
1043 if (!(opath && npath)) {
1048 err = rename_yod(opath, npath);
1060 yod_write_simple(void *buf,
1063 struct yod_inode *nino)
1066 if (off != nino->ni_fpos) {
1069 rtn = lseek_yod(nino->ni_fd, off, SEEK_SET);
1072 nino->ni_fpos = rtn;
1074 return write_yod(nino->ni_fd, buf, nbytes);
1078 yod_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1081 return doio(yod_write_simple, ioctx);
1085 yod_inop_pos(struct inode *ino, _SYSIO_OFF_T off)
1087 struct yod_inode *nino = I2NI(ino);
1090 err = lseek_yod(nino->ni_fd, off, SEEK_SET);
1091 return err < 0 ? err : off;
1095 yod_inop_iodone(struct ioctx *ioctxp __IS_UNUSED)
1099 * It's always done in this driver. It completed when posted.
1105 yod_inop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1107 struct yod_inode *nino = I2NI(ino);
1111 if (nino->ni_fd < 0)
1121 *rtn = syscall(SYS_fcntl, nino->ni_fd, cmd);
1134 arg = va_arg(ap, long);
1135 *rtn = syscall(SYS_fcntl, nino->ni_fd, cmd, arg);
1147 yod_inop_mknod(struct pnode *pno __IS_UNUSED,
1148 mode_t mode __IS_UNUSED,
1149 dev_t dev __IS_UNUSED)
1155 #ifdef _HAVE_STATVFS
1157 yod_inop_statvfs(struct pnode *pno,
1159 struct intnl_statvfs *buf)
1166 if (!ino || I2NI(ino)->ni_fd < 0) {
1167 path = _sysio_pb_path(pno->p_base, '/');
1173 * The syscall interface does not support SYS_fstatvfs.
1174 * Should possibly return ENOSYS, but thought it
1175 * better to use SYS_fstatfs and fill in as much of
1176 * the statvfs structure as possible. This allows
1177 * for more of a test of the sysio user interface.
1181 ? statfs_yod(path, &fs)
1182 : fstatfs_yod(I2NI(ino)->ni_fd, &fs);
1188 buf->f_bsize = fs.f_bsize; /* file system block size */
1189 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1190 buf->f_blocks = fs.f_blocks;
1191 buf->f_bfree = fs.f_bfree;
1192 buf->f_bavail = fs.f_bavail;
1193 buf->f_files = fs.f_files; /* Total number serial numbers */
1194 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1195 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1196 buf->f_fsid = fs.f_fsid.__val[1];
1197 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1198 buf->f_namemax = fs.f_namelen;
1204 yod_inop_sync(struct inode *ino)
1207 assert(I2NI(ino)->ni_fd >= 0);
1209 return fsync_yod(I2NI(ino)->ni_fd);
1213 yod_inop_datasync(struct inode *ino)
1216 assert(I2NI(ino)->ni_fd >= 0);
1218 return fsync_yod(I2NI(ino)->ni_fd);
1222 yod_inop_ioctl(struct inode *ino __IS_UNUSED,
1223 unsigned long int request __IS_UNUSED,
1224 va_list ap __IS_UNUSED)
1228 * I'm lazy. Maybe implemented later.
1235 yod_inop_gone(struct inode *ino)
1237 struct yod_inode *nino = I2NI(ino);
1240 (void )close(nino->ni_fd);
1241 free(ino->i_private);
1245 yod_fsop_gone(struct filesys *fs __IS_UNUSED)
1249 * Do nothing. There is no private part maintained for the
1250 * yod file interface.