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-2004 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 */
54 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
61 #include <sys/types.h>
63 #include <sys/fcntl.h>
68 #include <sys/statvfs.h>
69 #include <sys/statfs.h>
73 #include <sys/queue.h>
82 #include "fs_native.h"
88 #if defined(SYSIO_SYS_getdirentries)
90 #elif defined(SYSIO_SYS_getdents64)
92 #elif defined(SYSIO_SYS_getdents)
93 #if defined(_LARGEFILE64_SOURCE)
96 * Kernel version of directory entry.
100 unsigned long ld_off;
101 unsigned short ld_reclen;
105 #else /* !defined(_LARGEFILE64_SOURCE) */
107 #endif /* defined(_LARGEFILE64_SOURCE) */
108 #else /* catch-none */
109 #error No usable directory fill entries interface available
113 * Native file system information we keep per FS.
115 struct native_filesystem {
116 time_t nfs_atimo; /* attr timeout (sec) */
120 * Given fs, return driver private part.
123 ((struct native_filesystem *)(fs)->fs_private)
126 * Native file identifiers format.
128 struct native_inode_identifier {
129 dev_t dev; /* device number */
130 ino_t ino; /* i-number */
131 #ifdef HAVE_GENERATION
132 unsigned int gen; /* generation number */
137 * Driver-private i-node information we keep about local host file
140 struct native_inode {
142 ni_seekok : 1, /* can seek? */
143 ni_attrvalid : 1, /* cached attrs ok? */
144 ni_resetfpos : 1; /* reset fpos? */
145 struct native_inode_identifier ni_ident; /* unique identifier */
146 struct file_identifier ni_fileid; /* ditto */
147 int ni_fd; /* host fildes */
148 int ni_oflags; /* flags, from open */
149 unsigned ni_nopens; /* soft ref count */
150 _SYSIO_OFF_T ni_fpos; /* current pos */
151 time_t ni_attrtim; /* attrs expire time */
155 * Cached attributes usable?
157 #define NATIVE_ATTRS_VALID(nino, t) \
158 ((nino)->ni_attrtim && (t) < (nino)->ni_attrtim)
161 * Native IO path arguments.
164 char nio_op; /* 'r' or 'w' */
165 struct native_inode *nio_nino; /* native ino */
168 static int native_inop_lookup(struct pnode *pno,
170 struct intent *intnt,
172 static int native_inop_getattr(struct pnode *pno,
174 struct intnl_stat *stbuf);
175 static int native_inop_setattr(struct pnode *pno,
178 struct intnl_stat *stbuf);
179 static ssize_t native_filldirentries(struct inode *ino,
183 static int native_inop_mkdir(struct pnode *pno, mode_t mode);
184 static int native_inop_rmdir(struct pnode *pno);
185 static int native_inop_symlink(struct pnode *pno, const char *data);
186 static int native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz);
187 static int native_inop_open(struct pnode *pno, int flags, mode_t mode);
188 static int native_inop_close(struct inode *ino);
189 static int native_inop_link(struct pnode *old, struct pnode *new);
190 static int native_inop_unlink(struct pnode *pno);
191 static int native_inop_rename(struct pnode *old, struct pnode *new);
192 static int native_inop_read(struct inode *ino, struct ioctx *ioctx);
193 static int native_inop_write(struct inode *ino, struct ioctx *ioctx);
194 static _SYSIO_OFF_T native_inop_pos(struct inode *ino, _SYSIO_OFF_T off);
195 static int native_inop_iodone(struct ioctx *ioctx);
196 static int native_inop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn);
197 static int native_inop_sync(struct inode *ino);
198 static int native_inop_datasync(struct inode *ino);
199 static int native_inop_ioctl(struct inode *ino,
200 unsigned long int request,
202 static int native_inop_mknod(struct pnode *pno, mode_t mode, dev_t dev);
204 static int native_inop_statvfs(struct pnode *pno,
206 struct intnl_statvfs *buf);
208 static void native_inop_gone(struct inode *ino);
210 static struct inode_ops native_i_ops = {
214 native_filldirentries,
218 native_inop_readlink,
230 native_inop_datasync,
239 static int native_fsswop_mount(const char *source,
242 struct pnode *tocover,
243 struct mount **mntp);
245 static struct fssw_ops native_fssw_ops = {
249 static void native_fsop_gone(struct filesys *fs);
251 static struct filesys_ops native_inodesys_ops = {
256 * This example driver plays a strange game. It maintains a private,
257 * internal mount -- It's own separate, rooted, name space. The local
258 * file system's entire name space is available via this tree.
260 * This simplifies the implementation. At mount time, we need to generate
261 * a path-node to be used as a root. This allows us to look up the needed
262 * node in the host name space and leverage a whole lot of support from
265 static struct mount *native_internal_mount = NULL;
268 * Given i-node, return driver private part.
270 #define I2NI(ino) ((struct native_inode *)((ino)->i_private))
276 native_stat(const char *path,
279 struct intnl_stat *buf)
281 struct native_inode *nino;
283 struct _sysio_native_stat stbuf;
285 nino = ino ? I2NI(ino) : NULL;
288 err = syscall(SYSIO_SYS_stat, path, &stbuf);
289 else if (nino && nino->ni_fd >= 0)
290 err = syscall(SYSIO_SYS_fstat, nino->ni_fd, &stbuf);
295 nino->ni_attrtim = 0;
299 nino->ni_attrtim = t;
300 SYSIO_COPY_STAT(&stbuf, &ino->i_stbuf);
307 SYSIO_COPY_STAT(&stbuf, buf);
312 * Introduce an i-node to the system.
314 static struct inode *
315 native_i_new(struct filesys *fs, time_t expiration, struct intnl_stat *buf)
317 struct native_inode *nino;
320 nino = malloc(sizeof(struct native_inode));
323 bzero(&nino->ni_ident, sizeof(nino->ni_ident));
325 nino->ni_attrvalid = 0;
326 nino->ni_resetfpos = 0;
327 nino->ni_ident.dev = buf->st_dev;
328 nino->ni_ident.ino = buf->st_ino;
329 #ifdef HAVE_GENERATION
330 nino->ni_ident.gen = buf->st_gen;
332 nino->ni_fileid.fid_data = &nino->ni_ident;
333 nino->ni_fileid.fid_len = sizeof(nino->ni_ident);
338 nino->ni_attrtim = expiration;
352 * Initialize this driver.
359 * Capture current process umask and reset our process umask to
360 * zero. All permission bits to open/creat/setattr are absolute --
361 * They've already had a umask applied, when appropriate.
364 _sysio_umask = syscall(SYSIO_SYS_umask, 0);
366 * For Red Storm, this functionality is handled in cstart.
367 * The mask to be "captured" has been sent already.
368 * This eliminates a system call from every node!
370 #endif /* REDSTORM */
372 return _sysio_fssw_register("native", &native_fssw_ops);
376 * Create private, internal, view of the hosts name space.
379 create_internal_namespace(const void *data)
384 struct native_filesystem *nfs;
387 struct inode *rootino;
388 struct pnode_base *rootpb;
389 static struct qstr noname = { NULL, 0, 0 };
392 struct intnl_stat stbuf;
394 static struct option_value_info v[] = {
399 if (native_internal_mount) {
410 if (data && (len = strlen((char *)data))) {
411 opts = malloc(len + 1);
414 (void )strcpy(opts, data);
415 if (_sysio_get_args(opts, v) - opts != (ssize_t )len)
418 ul = strtoul(v[0].ovi_value, &cp, 0);
419 if (*cp != '\0' || ul >= UINT_MAX)
427 * We maintain an artificial, internal, name space in order to
428 * have access to fully qualified path names in the various routines.
429 * Initialize that name space now.
436 * This really should be per-mount. Hmm, but that's best done
437 * as proper sub-mounts in the core and not this driver. We reconcile
438 * now, here, by putting the mount options on the file system. That
439 * means they are global and only can be passed at the initial mount.
441 * Maybe do it right some day?
443 nfs = malloc(sizeof(struct native_filesystem));
449 if ((unsigned long)nfs->nfs_atimo != ul) {
453 fs = _sysio_fs_new(&native_inodesys_ops, 0, nfs);
462 t = _SYSIO_LOCAL_TIME();
463 err = native_stat("/", NULL, 0, &stbuf);
466 rootino = native_i_new(fs, t + FS2NFS(fs)->nfs_atimo, &stbuf);
473 * Generate base path-node for root.
475 rootpb = _sysio_pb_new(&noname, NULL, rootino);
482 * Mount it. This name space is disconnected from the
483 * rest of the system -- Only available within this driver.
485 err = _sysio_do_mount(fs, rootpb, 0, NULL, &mnt);
489 native_internal_mount = mnt;
493 if (_sysio_do_unmount(mnt) != 0)
501 _sysio_pb_gone(rootpb);
515 native_fsswop_mount(const char *source,
518 struct pnode *tocover,
522 struct nameidata nameidata;
526 * Caller must use fully qualified path names when specifying
532 if (!native_internal_mount) {
533 err = create_internal_namespace(data);
536 } else if (data && *(char *)data)
540 * Lookup the source in the internally maintained name space.
542 ND_INIT(&nameidata, 0, source, native_internal_mount->mnt_root, NULL);
543 err = _sysio_path_walk(native_internal_mount->mnt_root, &nameidata);
548 * Have path-node specified by the given source argument. Let the
549 * system finish the job, now.
552 _sysio_do_mount(native_internal_mount->mnt_fs,
553 nameidata.nd_pno->p_base,
558 * Release the internal name space pnode and clean up any
559 * aliases we might have generated. We really don't need to cache them
560 * as they are only used at mount time..
562 P_RELE(nameidata.nd_pno);
563 (void )_sysio_p_prune(native_internal_mount->mnt_root);
566 FS_REF(native_internal_mount->mnt_fs);
573 native_i_invalid(struct inode *inop, struct intnl_stat *stat)
575 struct native_inode *nino;
578 * Validate passed in inode against stat struct info
582 if (!nino->ni_attrtim ||
583 (nino->ni_ident.dev != stat->st_dev ||
584 nino->ni_ident.ino != stat->st_ino ||
585 #ifdef HAVE_GENERATION
586 nino->ni_ident.gen != stat->st_gen ||
588 ((inop)->i_stbuf.st_mode & S_IFMT) != (stat->st_mode & S_IFMT)) ||
589 (((inop)->i_stbuf.st_rdev != stat->st_rdev) &&
590 (S_ISCHR((inop)->i_stbuf.st_mode) ||
591 S_ISBLK((inop)->i_stbuf.st_mode)))) {
592 nino->ni_attrtim = 0; /* invalidate attrs */
598 static struct inode *
599 native_iget(struct filesys *fs, time_t expire, struct intnl_stat *stbp)
602 struct native_inode_identifier ident;
603 struct file_identifier fileid;
605 bzero(&ident, sizeof(ident));
606 ident.dev = stbp->st_dev;
607 ident.ino = stbp->st_ino;
608 #ifdef HAVE_GENERATION
609 ident.gen = stbp->st_gen;
611 fileid.fid_data = &ident;
612 fileid.fid_len = sizeof(ident);
613 ino = _sysio_i_find(fs, &fileid);
615 ino->i_stbuf = *stbp;
616 I2NI(ino)->ni_attrtim = expire;
619 return native_i_new(fs, expire, stbp);
623 * Find, and validate, or create i-node by host-relative path. Returned i-node
627 native_ibind(struct filesys *fs,
632 struct intnl_stat ostbuf, stbuf;
637 ostbuf = (*inop)->i_stbuf;
639 err = native_stat(path, *inop, t, &stbuf);
647 if (!native_i_invalid(*inop, &ostbuf))
652 _sysio_i_undead(*inop);
656 if (!(ino = native_iget(fs, t + FS2NFS(fs)->nfs_atimo, &stbuf)))
664 native_inop_lookup(struct pnode *pno,
666 struct intent *intnt __IS_UNUSED,
667 const char *path __IS_UNUSED)
674 *inop = pno->p_base->pb_ino;
677 * Try to use the cached attributes unless the intent
678 * indicates we are looking up the last component and
679 * caller wants attributes. In that case, force a refresh.
681 t = _SYSIO_LOCAL_TIME();
683 (path || !intnt || (intnt->int_opmask & INT_GETATTR) == 0) &&
684 NATIVE_ATTRS_VALID(I2NI(*inop), t))
688 * Don't have an inode yet. Because we translate everything back to
689 * a single name space for the host, we will assume the object the
690 * caller is looking for has no existing alias in our internal
691 * name space. We don't see the same file on different mounts in the
692 * underlying host FS as the same file.
694 * The file identifier *will* be unique. It's got to have a different
697 fqpath = _sysio_pb_path(pno->p_base, '/');
700 fs = pno->p_mount->mnt_fs;
701 err = native_ibind(fs, fqpath, t + FS2NFS(fs)->nfs_atimo, inop);
709 native_inop_getattr(struct pnode *pno,
711 struct intnl_stat *stat)
713 struct native_inode *nino;
717 * We just cannot use the cached attributes when getattr is
718 * called. Had the caller felt those were sufficient then
719 * they could have (would have?) simply used what was cached
720 * after revalidating. In this case, there's a good chance the
721 * caller is looking for the current time stamps and/or size. Something
722 * pretty volatile anyway.
724 err = 0; /* compiler cookie */
730 path = _sysio_pb_path(pno->p_base, '/');
733 fs = pno->p_mount->mnt_fs;
734 t = _SYSIO_LOCAL_TIME();
735 err = native_stat(path, ino, t + FS2NFS(fs)->nfs_atimo, stat);
737 } else if ((nino = I2NI(ino))->ni_fd >= 0)
739 * Don't have access to the fs record anymore. Just
740 * refresh but keep the current timeout.
742 err = native_stat(NULL, ino, nino->ni_attrtim, stat);
745 * Dev inodes don't open in this driver. We won't have
746 * a file descriptor with which to do the deed then. Satisfy
747 * the request from the cached copy of the attributes.
751 sizeof(struct intnl_stat));
758 #ifdef SYSIO_SYS_utime
760 _ut(const char *path, time_t actime, time_t modtime)
765 ut.modtime = modtime;
766 return syscall(SYSIO_SYS_utime, path, &ut);
770 _ut(const char *path, time_t actime, time_t modtime)
772 struct timeval tv[2];
774 tv[0].tv_sec = actime;
776 tv[1].tv_sec = modtime;
778 return syscall(SYSIO_SYS_utimes, path, &tv);
783 native_inop_setattr(struct pnode *pno,
786 struct intnl_stat *stat)
789 struct native_inode *nino;
794 nino = ino ? I2NI(ino) : NULL;
798 if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {
801 path = _sysio_pb_path(pno->p_base, '/');
807 * Get current status for undo.
809 err = native_stat(path, ino, 0, NULL);
813 if (mask & SETATTR_MODE) {
817 * Alter permissions attribute.
819 mode = stat->st_mode & 07777;
822 ? syscall(SYSIO_SYS_chmod, path, mode)
823 : syscall(SYSIO_SYS_fchmod, fd, mode);
828 mask &= ~SETATTR_MODE;
829 else if (mask & (SETATTR_MTIME|SETATTR_ATIME)) {
830 time_t actime, modtime;
833 * Alter access and/or modify time attributes.
835 actime = ino->i_stbuf.st_atime;
836 modtime = ino->i_stbuf.st_mtime;
837 if (mask & SETATTR_ATIME)
838 actime = stat->st_atime;
839 if (mask & SETATTR_MTIME)
840 modtime = stat->st_mtime;
841 if (_ut(path, actime, modtime) != 0)
845 mask &= ~(SETATTR_MTIME|SETATTR_ATIME);
846 else if (mask & (SETATTR_UID|SETATTR_GID)) {
849 * Alter owner and/or group identifiers.
853 ? syscall(SYSIO_SYS_chown,
861 : syscall(SYSIO_SYS_fchown,
873 mask &= ~(SETATTR_UID|SETATTR_GID);
874 else if (mask & SETATTR_LEN) {
876 * Do the truncate last. It can't be undone.
879 ? syscall(SYSIO_SYS_truncate, path, stat->st_size)
880 : syscall(SYSIO_SYS_ftruncate, fd, stat->st_size));
885 * Undo after error. Some or all of this might not work... We
888 if (mask & (SETATTR_UID|SETATTR_GID)) {
890 ? syscall(SYSIO_SYS_chown,
893 ? ino->i_stbuf.st_uid
896 ? ino->i_stbuf.st_gid
898 : syscall(SYSIO_SYS_fchown,
901 ? ino->i_stbuf.st_uid
904 ? ino->i_stbuf.st_gid
907 if (mask & (SETATTR_MTIME|SETATTR_ATIME))
908 (void )_ut(path, ino->i_stbuf.st_atime, ino->i_stbuf.st_mtime);
909 if (mask & SETATTR_MODE) {
911 ? syscall(SYSIO_SYS_chmod, path, ino->i_stbuf.st_mode & 07777)
912 : syscall(SYSIO_SYS_fchmod, ino->i_stbuf.st_mode & 07777);
916 * We must refresh the cached attributes.
918 if (!err && native_stat(path, ino, _SYSIO_LOCAL_TIME(), NULL) != 0)
926 native_pos(int fd, _SYSIO_OFF_T *offset, int whence)
931 assert(*offset >= 0);
934 #if _LARGEFILE64_SOURCE && defined(SYSIO_SYS__llseek)
938 syscall(SYSIO_SYS__llseek,
940 (unsigned int)(off >> 32),
949 syscall(SYSIO_SYS_lseek,
962 native_ifilldirentries(struct native_inode *nino,
969 #if defined(SYSIO_SYS_getdirentries)
977 * Stream-oriented access requires that we reposition prior to the
980 assert(nino->ni_seekok);
981 if (*posp != nino->ni_fpos || nino->ni_resetfpos) {
982 nino->ni_fpos = *posp;
983 err = native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_SET);
985 nino->ni_resetfpos = 1;
988 nino->ni_resetfpos = 0;
992 #if defined(SYSIO_SYS_getdirentries)
993 syscall(SYSIO_SYS_getdirentries,
998 #elif defined(SYSIO_SYS_getdents64)
999 syscall(SYSIO_SYS_getdents64, nino->ni_fd, buf, nbytes);
1000 #elif defined(SYSIO_SYS_getdents)
1001 syscall(SYSIO_SYS_getdents, nino->ni_fd, buf, nbytes);
1007 * Stream-oriented access requires that we discover where we are
1010 if ((err = native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_CUR)) != 0) {
1012 * Leave the position at the old I suppose.
1014 nino->ni_resetfpos = 1;
1017 *posp = nino->ni_fpos;
1022 native_filldirentries(struct inode *ino,
1027 struct native_inode *nino = I2NI(ino);
1031 struct linux_dirent *ldp;
1032 struct dirent64 *d64p;
1037 #define count nbytes
1041 assert(nino->ni_fd >= 0);
1045 while (!(bp = malloc(count))) {
1047 if (count < sizeof(struct dirent))
1051 cc = native_ifilldirentries(nino, posp, bp, count);
1059 ldp = (struct linux_dirent *)bp;
1060 d64p = (struct dirent64 *)buf;
1062 namlen = strlen(ldp->ld_name);
1063 reclen = sizeof(*d64p) - sizeof(d64p->d_name) + namlen;
1064 if (nbytes <= reclen)
1066 d64p->d_ino = ldp->ld_ino;
1067 d64p->d_off = nino->ni_fpos = ldp->ld_off;
1069 (((reclen + sizeof(long))) / sizeof(long)) * sizeof(long);
1070 if (nbytes < d64p->d_reclen)
1071 d64p->d_reclen = reclen + 1;
1072 d64p->d_type = DT_UNKNOWN; /* you lose -- sorry. */
1073 (void )memcpy(d64p->d_name, ldp->ld_name, namlen);
1075 * Zero pad the rest.
1077 for (cp = d64p->d_name + namlen, n = d64p->d_reclen - reclen;
1081 cc -= ldp->ld_reclen;
1082 ldp = (struct linux_dirent *)((char *)ldp + ldp->ld_reclen);
1083 nbytes -= d64p->d_reclen;
1084 d64p = (struct dirent64 *)((char *)d64p + d64p->d_reclen);
1088 (d64p == (struct dirent64 *)buf && cc)
1090 : (char *)d64p - buf;
1099 native_inop_mkdir(struct pnode *pno, mode_t mode)
1104 path = _sysio_pb_path(pno->p_base, '/');
1108 err = syscall(SYSIO_SYS_mkdir, path, mode);
1116 native_inop_rmdir(struct pnode *pno)
1121 path = _sysio_pb_path(pno->p_base, '/');
1125 err = syscall(SYSIO_SYS_rmdir, path);
1133 native_inop_symlink(struct pnode *pno, const char *data)
1138 path = _sysio_pb_path(pno->p_base, '/');
1142 err = syscall(SYSIO_SYS_symlink, data, path);
1150 native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz)
1155 path = _sysio_pb_path(pno->p_base, '/');
1158 i = syscall(SYSIO_SYS_readlink, path, buf, bufsiz);
1166 native_inop_open(struct pnode *pno, int flags, mode_t mode)
1168 struct native_inode *nino;
1172 path = _sysio_pb_path(pno->p_base, '/');
1177 * Whether the file is already open, or not, makes no difference.
1178 * Want to always give the host OS a chance to authorize in case
1179 * something has changed underneath us.
1181 if (flags & O_WRONLY) {
1183 * Promote write-only attempt to RW.
1189 flags |= O_LARGEFILE;
1191 fd = syscall(SYSIO_SYS_open, path, flags, mode);
1192 if (!pno->p_base->pb_ino && fd >= 0) {
1197 * Success but we need to return an i-node.
1199 fs = pno->p_mount->mnt_fs;
1203 _SYSIO_LOCAL_TIME() + FS2NFS(fs)->nfs_atimo,
1204 &pno->p_base->pb_ino);
1206 (void )syscall(SYSIO_SYS_close, fd);
1217 * Remember this new open.
1219 nino = I2NI(pno->p_base->pb_ino);
1221 assert(nino->ni_nopens);
1223 if (nino->ni_fd >= 0) {
1224 if ((nino->ni_oflags & O_RDWR) ||
1225 (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {
1229 (void )syscall(SYSIO_SYS_close, fd);
1232 (void )syscall(SYSIO_SYS_close, nino->ni_fd);
1235 * Invariant; First open. Must init.
1237 nino->ni_resetfpos = 0;
1241 * Need to know whether we can seek on this
1245 native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_CUR) != 0 ? 0 : 1;
1251 native_inop_close(struct inode *ino)
1253 struct native_inode *nino = I2NI(ino);
1256 if (nino->ni_fd < 0)
1259 assert(nino->ni_nopens);
1260 if (--nino->ni_nopens) {
1262 * Hmmm. We really don't need anything else. However, some
1263 * filesystems try to implement a sync-on-close semantic.
1264 * As this appears now, that is lost. Might want to change
1265 * it somehow in the future?
1270 err = syscall(SYSIO_SYS_close, nino->ni_fd);
1275 nino->ni_resetfpos = 0;
1281 native_inop_link(struct pnode *old, struct pnode *new)
1284 char *opath, *npath;
1288 opath = _sysio_pb_path(old->p_base, '/');
1289 npath = _sysio_pb_path(new->p_base, '/');
1290 if (!(opath && npath)) {
1295 err = syscall(SYSIO_SYS_link, opath, npath);
1308 native_inop_unlink(struct pnode *pno)
1313 path = _sysio_pb_path(pno->p_base, '/');
1318 * For this driver, unlink is easy with open files. Since the
1319 * file remains open to the system, too, the descriptors are still
1322 * Other drivers will have some difficulty here as the entry in the
1323 * file system name space must be removed without sacrificing access
1324 * to the file itself. In NFS this is done with a mechanism referred
1325 * to as a `silly delete'. The file is moved to a temporary name
1326 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some
1327 * unique characters) in order to simulate the proper semantic.
1329 if (syscall(SYSIO_SYS_unlink, path) != 0)
1336 native_inop_rename(struct pnode *old, struct pnode *new)
1339 char *opath, *npath;
1341 opath = _sysio_pb_path(old->p_base, '/');
1342 npath = _sysio_pb_path(new->p_base, '/');
1343 if (!(opath && npath)) {
1348 err = syscall(SYSIO_SYS_rename, opath, npath);
1361 dopio(void *buf, size_t count, _SYSIO_OFF_T off, struct native_io *nio)
1365 if (!nio->nio_nino->ni_seekok) {
1366 if (off != nio->nio_nino->ni_fpos) {
1368 * They're trying to reposition. Can't
1369 * seek on this descriptor so we err out now.
1375 syscall(nio->nio_op == 'r'
1378 nio->nio_nino->ni_fd,
1382 nio->nio_nino->ni_fpos += cc;
1385 syscall((nio->nio_op == 'r'
1387 : SYSIO_SYS_pwrite),
1388 nio->nio_nino->ni_fd,
1397 doiov(const struct iovec *iov,
1401 struct native_io *nio)
1405 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1406 #define MAX_IOVEC INT_MAX
1414 * Avoid the reposition call if we're already at the right place.
1415 * Allows us to access pipes and fifos.
1417 if (off != nio->nio_nino->ni_fpos) {
1420 err = native_pos(nio->nio_nino->ni_fd, &off, SEEK_SET);
1422 nio->nio_nino->ni_resetfpos = 1;
1425 nio->nio_nino->ni_resetfpos = 0;
1426 nio->nio_nino->ni_fpos = off;
1430 * The {read,write}v is safe as this routine is only ever called
1431 * by _sysio_enumerate_extents() and that routine is exact. It never
1432 * passes iovectors including tails.
1437 ? syscall(nio->nio_op == 'r' ? SYSIO_SYS_readv : SYSIO_SYS_writev,
1438 nio->nio_nino->ni_fd,
1443 _sysio_enumerate_iovec(iov,
1447 (ssize_t (*)(void *,
1455 nio->nio_nino->ni_fpos += cc;
1458 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1465 lockop_all(struct native_inode *nino,
1466 struct intnl_xtvec *xtv,
1476 flock.l_whence = SEEK_SET;
1478 flock.l_start = xtv->xtv_off;
1479 flock.l_len = xtv->xtv_len;
1482 syscall(SYSIO_SYS_fcntl,
1493 order_xtv(const struct intnl_xtvec *xtv1, const struct intnl_xtvec *xtv2)
1496 if (xtv1->xtv_off < xtv2->xtv_off)
1498 if (xtv1->xtv_off > xtv2->xtv_off)
1505 doio(char op, struct ioctx *ioctx)
1507 struct native_inode *nino;
1510 struct intnl_xtvec *oxtv;
1513 struct native_io arguments;
1516 struct intnl_xtvec *front, *rear, tmp;
1519 nino = I2NI(ioctx->ioctx_ino);
1521 dolocks = ioctx->ioctx_xtvlen > 1 && nino->ni_seekok;
1524 * Must lock the regions (in order!) since we can't do
1525 * strided-IO as a single atomic operation.
1527 oxtv = malloc(ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1532 ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1534 ioctx->ioctx_xtvlen,
1535 sizeof(struct intnl_xtvec),
1536 (int (*)(const void *, const void *))order_xtv);
1539 oxtv, ioctx->ioctx_xtvlen,
1540 op == 'r' ? F_RDLCK : F_WRLCK);
1547 arguments.nio_op = op;
1548 arguments.nio_nino = nino;
1550 _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1551 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1552 (ssize_t (*)(const struct iovec *,
1561 * Must unlock in reverse order.
1564 rear = front + ioctx->ioctx_xtvlen - 1;
1565 while (front < rear) {
1570 if (lockop_all(nino, oxtv, ioctx->ioctx_xtvlen, F_UNLCK) != 0)
1575 if ((ioctx->ioctx_cc = cc) < 0) {
1576 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1577 ioctx->ioctx_cc = -1;
1583 native_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1586 return doio('r', ioctx);
1590 native_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1593 return doio('w', ioctx);
1597 native_inop_pos(struct inode *ino, _SYSIO_OFF_T off)
1599 struct native_inode *nino = I2NI(ino);
1602 err = native_pos(nino->ni_fd, &off, SEEK_SET);
1603 return err < 0 ? err : off;
1607 native_inop_iodone(struct ioctx *ioctxp __IS_UNUSED)
1611 * It's always done in this driver. It completed when posted.
1617 native_inop_fcntl(struct inode *ino,
1622 struct native_inode *nino = I2NI(ino);
1626 if (nino->ni_fd < 0)
1636 *rtn = syscall(SYSIO_SYS_fcntl, nino->ni_fd, cmd);
1649 arg = va_arg(ap, long);
1650 *rtn = syscall(SYSIO_SYS_fcntl, nino->ni_fd, cmd, arg);
1662 native_inop_mknod(struct pnode *pno __IS_UNUSED,
1663 mode_t mode __IS_UNUSED,
1664 dev_t dev __IS_UNUSED)
1670 #ifdef _HAVE_STATVFS
1672 native_inop_statvfs(struct pnode *pno,
1674 struct intnl_statvfs *buf)
1681 if (!ino || I2NI(ino)->ni_fd < 0) {
1682 path = _sysio_pb_path(pno->p_base, '/');
1688 * The syscall interface does not support SYSIO_SYS_fstatvfs.
1689 * Should possibly return ENOSYS, but thought it
1690 * better to use SYSIO_SYS_fstatfs and fill in as much of
1691 * the statvfs structure as possible. This allows
1692 * for more of a test of the sysio user interface.
1696 ? syscall(SYSIO_SYS_statfs, path, &fs)
1697 : syscall(SYSIO_SYS_fstatfs, I2NI(ino)->ni_fd, &fs);
1703 buf->f_bsize = fs.f_bsize; /* file system block size */
1704 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1705 buf->f_blocks = fs.f_blocks;
1706 buf->f_bfree = fs.f_bfree;
1707 buf->f_bavail = fs.f_bavail;
1708 buf->f_files = fs.f_files; /* Total number serial numbers */
1709 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1710 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1711 buf->f_fsid = fs.f_fsid.__val[1];
1712 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1713 buf->f_namemax = fs.f_namelen;
1719 native_inop_sync(struct inode *ino)
1723 assert(I2NI(ino)->ni_fd >= 0);
1725 err = syscall(SYSIO_SYS_fsync, I2NI(ino)->ni_fd);
1732 native_inop_datasync(struct inode *ino)
1734 struct native_inode *nino;
1738 assert(nino->ni_fd >= 0);
1740 #ifdef SYSIO_SYS_fdatasync
1741 err = syscall(SYSIO_SYS_fdatasync, I2NI(ino)->ni_fd);
1744 #warning No fdatasync system call -- Using fsync instead!
1746 err = syscall(SYSIO_SYS_fsync, I2NI(ino)->ni_fd);
1753 #ifdef HAVE_LUSTRE_HACK
1755 native_inop_ioctl(struct inode *ino,
1756 unsigned long int request,
1759 struct native_inode *nino;
1760 long arg1, arg2, arg3, arg4;
1764 assert(nino->ni_fd >= 0);
1765 arg1 = va_arg(ap, long);
1766 arg2 = va_arg(ap, long);
1767 arg3 = va_arg(ap, long);
1768 arg4 = va_arg(ap, long);
1771 syscall(SYSIO_SYS_ioctl, I2NI(ino)->ni_fd, request,
1772 arg1, arg2, arg3, arg4);
1779 native_inop_ioctl(struct inode *ino __IS_UNUSED,
1780 unsigned long int request __IS_UNUSED,
1781 va_list ap __IS_UNUSED)
1785 * I'm lazy. Maybe implemented later.
1792 native_inop_gone(struct inode *ino)
1794 struct native_inode *nino = I2NI(ino);
1796 if (nino->ni_fd >= 0)
1797 (void )syscall(SYSIO_SYS_close, nino->ni_fd);
1799 free(ino->i_private);
1803 native_fsop_gone(struct filesys *fs __IS_UNUSED)
1806 free(fs->fs_private);
1808 * Do nothing. There is no private part maintained for the
1809 * native file interface.