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)
89 #define DIR_STREAMED 0
91 #elif defined(SYSIO_SYS_getdents64)
92 #define DIR_STREAMED 1
94 #elif defined(SYSIO_SYS_getdents)
95 #define DIR_STREAMED 1
96 #if defined(_LARGEFILE64_SOURCE)
99 * Kernel version of directory entry.
101 struct linux_dirent {
102 unsigned long ld_ino;
103 unsigned long ld_off;
104 unsigned short ld_reclen;
108 #else /* !defined(_LARGEFILE64_SOURCE) */
110 #endif /* defined(_LARGEFILE64_SOURCE) */
111 #else /* catch-none */
112 #error No usable directory fill entries interface available
116 * Native file system information we keep per FS.
118 struct native_filesystem {
119 time_t nfs_atimo; /* attr timeout (sec) */
123 * Given fs, return driver private part.
126 ((struct native_filesystem *)(fs)->fs_private)
129 * Native file identifiers format.
131 struct native_inode_identifier {
132 dev_t dev; /* device number */
133 ino_t ino; /* i-number */
134 #ifdef HAVE_GENERATION
135 unsigned int gen; /* generation number */
140 * Driver-private i-node information we keep about local host file
143 struct native_inode {
145 ni_seekok : 1, /* can seek? */
146 ni_attrvalid : 1; /* cached attrs ok? */
147 struct native_inode_identifier ni_ident; /* unique identifier */
148 struct file_identifier ni_fileid; /* ditto */
149 int ni_fd; /* host fildes */
150 int ni_oflags; /* flags, from open */
151 unsigned ni_nopens; /* soft ref count */
152 _SYSIO_OFF_T ni_fpos; /* current pos */
153 time_t ni_attrtim; /* attrs expire time */
157 * Cached attributes usable?
159 #define NATIVE_ATTRS_VALID(nino, t) \
160 ((nino)->ni_attrtim && (t) < (nino)->ni_attrtim)
163 * Native IO path arguments.
166 char nio_op; /* 'r' or 'w' */
167 struct native_inode *nio_nino; /* native ino */
170 static int native_inop_lookup(struct pnode *pno,
172 struct intent *intnt,
174 static int native_inop_getattr(struct pnode *pno,
176 struct intnl_stat *stbuf);
177 static int native_inop_setattr(struct pnode *pno,
180 struct intnl_stat *stbuf);
181 static ssize_t native_getdirentries(struct inode *ino,
184 _SYSIO_OFF_T *basep);
185 static int native_inop_mkdir(struct pnode *pno, mode_t mode);
186 static int native_inop_rmdir(struct pnode *pno);
187 static int native_inop_symlink(struct pnode *pno, const char *data);
188 static int native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz);
189 static int native_inop_open(struct pnode *pno, int flags, mode_t mode);
190 static int native_inop_close(struct inode *ino);
191 static int native_inop_link(struct pnode *old, struct pnode *new);
192 static int native_inop_unlink(struct pnode *pno);
193 static int native_inop_rename(struct pnode *old, struct pnode *new);
194 static int native_inop_read(struct inode *ino, struct ioctx *ioctx);
195 static int native_inop_write(struct inode *ino, struct ioctx *ioctx);
196 static _SYSIO_OFF_T native_inop_pos(struct inode *ino, _SYSIO_OFF_T off);
197 static int native_inop_iodone(struct ioctx *ioctx);
198 static int native_inop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn);
199 static int native_inop_sync(struct inode *ino);
200 static int native_inop_datasync(struct inode *ino);
201 static int native_inop_ioctl(struct inode *ino,
202 unsigned long int request,
204 static int native_inop_mknod(struct pnode *pno, mode_t mode, dev_t dev);
206 static int native_inop_statvfs(struct pnode *pno,
208 struct intnl_statvfs *buf);
210 static void native_inop_gone(struct inode *ino);
212 static struct inode_ops native_i_ops = {
216 native_getdirentries,
220 native_inop_readlink,
232 native_inop_datasync,
241 static int native_fsswop_mount(const char *source,
244 struct pnode *tocover,
245 struct mount **mntp);
247 static struct fssw_ops native_fssw_ops = {
251 static void native_fsop_gone(struct filesys *fs);
253 static struct filesys_ops native_inodesys_ops = {
258 * This example driver plays a strange game. It maintains a private,
259 * internal mount -- It's own separate, rooted, name space. The local
260 * file system's entire name space is available via this tree.
262 * This simplifies the implementation. At mount time, we need to generate
263 * a path-node to be used as a root. This allows us to look up the needed
264 * node in the host name space and leverage a whole lot of support from
267 static struct mount *native_internal_mount = NULL;
270 * Given i-node, return driver private part.
272 #define I2NI(ino) ((struct native_inode *)((ino)->i_private))
278 native_stat(const char *path,
281 struct intnl_stat *buf)
283 struct native_inode *nino;
285 struct _sysio_native_stat stbuf;
287 nino = ino ? I2NI(ino) : NULL;
290 err = syscall(SYSIO_SYS_stat, path, &stbuf);
291 else if (nino && nino->ni_fd >= 0)
292 err = syscall(SYSIO_SYS_fstat, nino->ni_fd, &stbuf);
297 nino->ni_attrtim = 0;
301 nino->ni_attrtim = t;
302 SYSIO_COPY_STAT(&stbuf, &ino->i_stbuf);
309 SYSIO_COPY_STAT(&stbuf, buf);
314 * Introduce an i-node to the system.
316 static struct inode *
317 native_i_new(struct filesys *fs, time_t expiration, struct intnl_stat *buf)
319 struct native_inode *nino;
322 nino = malloc(sizeof(struct native_inode));
325 bzero(&nino->ni_ident, sizeof(nino->ni_ident));
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.
363 _sysio_umask = syscall(SYSIO_SYS_umask, 0);
365 return _sysio_fssw_register("native", &native_fssw_ops);
369 * Create private, internal, view of the hosts name space.
372 create_internal_namespace(const void *data)
377 struct native_filesystem *nfs;
380 struct inode *rootino;
381 struct pnode_base *rootpb;
382 static struct qstr noname = { NULL, 0, 0 };
385 struct intnl_stat stbuf;
387 static struct option_value_info v[] = {
392 if (native_internal_mount) {
403 if (data && (len = strlen((char *)data))) {
404 opts = malloc(len + 1);
407 (void )strcpy(opts, data);
408 if (_sysio_get_args(opts, v) - opts != (ssize_t )len)
411 ul = strtoul(v[0].ovi_value, &cp, 0);
412 if (*cp != '\0' || ul >= UINT_MAX)
420 * We maintain an artificial, internal, name space in order to
421 * have access to fully qualified path names in the various routines.
422 * Initialize that name space now.
429 * This really should be per-mount. Hmm, but that's best done
430 * as proper sub-mounts in the core and not this driver. We reconcile
431 * now, here, by putting the mount options on the file system. That
432 * means they are global and only can be passed at the initial mount.
434 * Maybe do it right some day?
436 nfs = malloc(sizeof(struct native_filesystem));
442 if ((unsigned long)nfs->nfs_atimo != ul) {
446 fs = _sysio_fs_new(&native_inodesys_ops, 0, nfs);
455 t = _SYSIO_LOCAL_TIME();
456 err = native_stat("/", NULL, 0, &stbuf);
459 rootino = native_i_new(fs, t + FS2NFS(fs)->nfs_atimo, &stbuf);
466 * Generate base path-node for root.
468 rootpb = _sysio_pb_new(&noname, NULL, rootino);
475 * Mount it. This name space is disconnected from the
476 * rest of the system -- Only available within this driver.
478 err = _sysio_do_mount(fs, rootpb, 0, NULL, &mnt);
482 native_internal_mount = mnt;
486 if (_sysio_do_unmount(mnt) != 0)
494 _sysio_pb_gone(rootpb);
509 native_fsswop_mount(const char *source,
512 struct pnode *tocover,
516 struct nameidata nameidata;
520 * Caller must use fully qualified path names when specifying
526 if (!native_internal_mount) {
527 err = create_internal_namespace(data);
530 } else if (data && *(char *)data)
534 * Lookup the source in the internally maintained name space.
536 ND_INIT(&nameidata, 0, source, native_internal_mount->mnt_root, NULL);
537 err = _sysio_path_walk(native_internal_mount->mnt_root, &nameidata);
542 * Have path-node specified by the given source argument. Let the
543 * system finish the job, now.
546 _sysio_do_mount(native_internal_mount->mnt_fs,
547 nameidata.nd_pno->p_base,
552 * Release the internal name space pnode and clean up any
553 * aliases we might have generated. We really don't need to cache them
554 * as they are only used at mount time..
556 P_RELE(nameidata.nd_pno);
557 (void )_sysio_p_prune(native_internal_mount->mnt_root);
560 FS_REF(native_internal_mount->mnt_fs);
567 native_i_invalid(struct inode *inop, struct intnl_stat *stat)
569 struct native_inode *nino;
572 * Validate passed in inode against stat struct info
576 if (!nino->ni_attrtim ||
577 (nino->ni_ident.dev != stat->st_dev ||
578 nino->ni_ident.ino != stat->st_ino ||
579 #ifdef HAVE_GENERATION
580 nino->ni_ident.gen != stat->st_gen ||
582 ((inop)->i_stbuf.st_mode & S_IFMT) != (stat->st_mode & S_IFMT)) ||
583 (((inop)->i_stbuf.st_rdev != stat->st_rdev) &&
584 (S_ISCHR((inop)->i_stbuf.st_mode) ||
585 S_ISBLK((inop)->i_stbuf.st_mode)))) {
586 nino->ni_attrtim = 0; /* invalidate attrs */
592 static struct inode *
593 native_iget(struct filesys *fs, time_t expire, struct intnl_stat *stbp)
596 struct native_inode_identifier ident;
597 struct file_identifier fileid;
599 bzero(&ident, sizeof(ident));
600 ident.dev = stbp->st_dev;
601 ident.ino = stbp->st_ino;
602 #ifdef HAVE_GENERATION
603 ident.gen = stbp->st_gen;
605 fileid.fid_data = &ident;
606 fileid.fid_len = sizeof(ident);
607 ino = _sysio_i_find(fs, &fileid);
609 ino->i_stbuf = *stbp;
610 I2NI(ino)->ni_attrtim = expire;
613 return native_i_new(fs, expire, stbp);
617 * Find, and validate, or create i-node by host-relative path. Returned i-node
621 native_ibind(struct filesys *fs,
626 struct intnl_stat ostbuf, stbuf;
631 ostbuf = (*inop)->i_stbuf;
633 err = native_stat(path, *inop, t, &stbuf);
641 if (!native_i_invalid(*inop, &ostbuf))
646 _sysio_i_undead(*inop);
650 if (!(ino = native_iget(fs, t + FS2NFS(fs)->nfs_atimo, &stbuf)))
658 native_inop_lookup(struct pnode *pno,
660 struct intent *intnt __IS_UNUSED,
661 const char *path __IS_UNUSED)
668 *inop = pno->p_base->pb_ino;
671 * Try to use the cached attributes unless the intent
672 * indicates we are looking up the last component and
673 * caller wants attributes. In that case, force a refresh.
675 t = _SYSIO_LOCAL_TIME();
677 (path || !intnt || (intnt->int_opmask & INT_GETATTR) == 0) &&
678 NATIVE_ATTRS_VALID(I2NI(*inop), t))
682 * Don't have an inode yet. Because we translate everything back to
683 * a single name space for the host, we will assume the object the
684 * caller is looking for has no existing alias in our internal
685 * name space. We don't see the same file on different mounts in the
686 * underlying host FS as the same file.
688 * The file identifier *will* be unique. It's got to have a different
691 fqpath = _sysio_pb_path(pno->p_base, '/');
694 fs = pno->p_mount->mnt_fs;
695 err = native_ibind(fs, fqpath, t + FS2NFS(fs)->nfs_atimo, inop);
703 native_inop_getattr(struct pnode *pno,
705 struct intnl_stat *stat)
707 struct native_inode *nino;
711 * We just cannot use the cached attributes when getattr is
712 * called. Had the caller felt those were sufficient then
713 * they could have (would have?) simply used what was cached
714 * after revalidating. In this case, there's a good chance the
715 * caller is looking for the current time stamps and/or size. Something
716 * pretty volatile anyway.
718 err = 0; /* compiler cookie */
724 path = _sysio_pb_path(pno->p_base, '/');
727 fs = pno->p_mount->mnt_fs;
728 t = _SYSIO_LOCAL_TIME();
729 err = native_stat(path, ino, t + FS2NFS(fs)->nfs_atimo, stat);
731 } else if ((nino = I2NI(ino))->ni_fd >= 0)
733 * Don't have access to the fs record anymore. Just
734 * refresh but keep the current timeout.
736 err = native_stat(NULL, ino, nino->ni_attrtim, stat);
739 * Dev inodes don't open in this driver. We won't have
740 * a file descriptor with which to do the deed then. Satisfy
741 * the request from the cached copy of the attributes.
745 sizeof(struct intnl_stat));
752 #ifdef SYSIO_SYS_utime
754 _ut(const char *path, time_t actime, time_t modtime)
759 ut.modtime = modtime;
760 return syscall(SYSIO_SYS_utime, path, &ut);
764 _ut(const char *path, time_t actime, time_t modtime)
766 struct timeval tv[2];
768 tv[0].tv_sec = actime;
770 tv[1].tv_sec = modtime;
772 return syscall(SYSIO_SYS_utimes, path, &tv);
777 native_inop_setattr(struct pnode *pno,
780 struct intnl_stat *stat)
783 struct native_inode *nino;
788 nino = ino ? I2NI(ino) : NULL;
792 if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {
795 path = _sysio_pb_path(pno->p_base, '/');
801 * Get current status for undo.
803 err = native_stat(path, ino, 0, NULL);
807 if (mask & SETATTR_MODE) {
811 * Alter permissions attribute.
813 mode = stat->st_mode & 07777;
816 ? syscall(SYSIO_SYS_chmod, path, mode)
817 : syscall(SYSIO_SYS_fchmod, fd, mode);
822 mask &= ~SETATTR_MODE;
823 else if (mask & (SETATTR_MTIME|SETATTR_ATIME)) {
824 time_t actime, modtime;
827 * Alter access and/or modify time attributes.
829 actime = ino->i_stbuf.st_atime;
830 modtime = ino->i_stbuf.st_mtime;
831 if (mask & SETATTR_ATIME)
832 actime = stat->st_atime;
833 if (mask & SETATTR_MTIME)
834 modtime = stat->st_mtime;
835 if (_ut(path, actime, modtime) != 0)
839 mask &= ~(SETATTR_MTIME|SETATTR_ATIME);
840 else if (mask & (SETATTR_UID|SETATTR_GID)) {
843 * Alter owner and/or group identifiers.
847 ? syscall(SYSIO_SYS_chown,
855 : syscall(SYSIO_SYS_fchown,
867 mask &= ~(SETATTR_UID|SETATTR_GID);
868 else if (mask & SETATTR_LEN) {
870 * Do the truncate last. It can't be undone.
873 ? syscall(SYSIO_SYS_truncate, path, stat->st_size)
874 : syscall(SYSIO_SYS_ftruncate, fd, stat->st_size));
879 * Undo after error. Some or all of this might not work... We
882 if (mask & (SETATTR_UID|SETATTR_GID)) {
884 ? syscall(SYSIO_SYS_chown,
887 ? ino->i_stbuf.st_uid
890 ? ino->i_stbuf.st_gid
892 : syscall(SYSIO_SYS_fchown,
895 ? ino->i_stbuf.st_uid
898 ? ino->i_stbuf.st_gid
901 if (mask & (SETATTR_MTIME|SETATTR_ATIME))
902 (void )_ut(path, ino->i_stbuf.st_atime, ino->i_stbuf.st_mtime);
903 if (mask & SETATTR_MODE) {
905 ? syscall(SYSIO_SYS_chmod, path, ino->i_stbuf.st_mode & 07777)
906 : syscall(SYSIO_SYS_fchmod, ino->i_stbuf.st_mode & 07777);
910 * We must refresh the cached attributes.
912 if (!err && native_stat(path, ino, _SYSIO_LOCAL_TIME(), NULL) != 0)
920 native_pos(int fd, _SYSIO_OFF_T *offset, int whence)
925 assert(*offset >= 0);
928 #if _LARGEFILE64_SOURCE && defined(SYSIO_SYS__llseek)
932 syscall(SYSIO_SYS__llseek,
934 (unsigned int)(off >> 32),
943 syscall(SYSIO_SYS_lseek,
956 native_filldirentries(struct native_inode *nino,
969 * Stream-oriented access requires that we reposition prior to the
972 if ((err = native_pos(nino->ni_fd, basep, SEEK_SET)) != 0)
975 nino->ni_fpos = *basep;
978 #if defined(SYSIO_SYS_getdirentries)
979 syscall(SYSIO_SYS_getdirentries,
984 #elif defined(SYSIO_SYS_getdents64)
985 syscall(SYSIO_SYS_getdents64, nino->ni_fd, buf, nbytes);
986 #elif defined(SYSIO_SYS_getdents)
987 syscall(SYSIO_SYS_getdents, nino->ni_fd, buf, nbytes);
994 * Stream-oriented access requires that we discover where we are
998 if ((err = native_pos(nino->ni_fd, basep, SEEK_CUR)) != 0)
1001 nino->ni_fpos = *basep;
1006 native_getdirentries(struct inode *ino,
1009 _SYSIO_OFF_T *basep)
1011 struct native_inode *nino = I2NI(ino);
1015 struct linux_dirent *ldp;
1016 struct dirent64 *d64p;
1020 * Work-around for broken 64 bit basep update
1021 * Get value of basep to return from last directory
1024 _SYSIO_OFF_T last_offset = *basep;
1027 #define count nbytes
1031 assert(nino->ni_fd >= 0);
1035 while (!(bp = malloc(count))) {
1037 if (count < sizeof(struct dirent))
1041 cc = native_filldirentries(nino, bp, count, basep);
1049 ldp = (struct linux_dirent *)bp;
1050 d64p = (struct dirent64 *)buf;
1052 if (cc < 0 || (size_t )cc <= sizeof(*ldp))
1054 namlen = strlen(ldp->ld_name);
1055 reclen = sizeof(*d64p) - sizeof(d64p->d_name) + namlen + 1;
1056 if (nbytes < reclen)
1058 d64p->d_ino = ldp->ld_ino;
1059 d64p->d_off = ldp->ld_off;
1061 (((reclen + sizeof(long) - 1)) / sizeof(long)) *
1063 if (nbytes < d64p->d_reclen)
1064 d64p->d_reclen = reclen;
1065 d64p->d_type = DT_UNKNOWN; /* you lose -- sorry. */
1066 (void )strncpy(d64p->d_name, ldp->ld_name, namlen);
1067 *(d64p->d_name + namlen) = '\0';
1068 cc -= ldp->ld_reclen;
1069 ldp = (struct linux_dirent *)((char *)ldp + ldp->ld_reclen);
1070 nbytes -= d64p->d_reclen;
1071 last_offset = d64p->d_off;
1072 d64p = (struct dirent64 *)((char *)d64p + d64p->d_reclen);
1074 nino->ni_fpos = *basep = last_offset;
1077 (d64p == (struct dirent64 *)buf && cc)
1079 : (char *)d64p - buf;
1088 native_inop_mkdir(struct pnode *pno, mode_t mode)
1093 path = _sysio_pb_path(pno->p_base, '/');
1097 err = syscall(SYSIO_SYS_mkdir, path, mode);
1105 native_inop_rmdir(struct pnode *pno)
1110 path = _sysio_pb_path(pno->p_base, '/');
1114 err = syscall(SYSIO_SYS_rmdir, path);
1122 native_inop_symlink(struct pnode *pno, const char *data)
1127 path = _sysio_pb_path(pno->p_base, '/');
1131 err = syscall(SYSIO_SYS_symlink, data, path);
1139 native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz)
1144 path = _sysio_pb_path(pno->p_base, '/');
1147 i = syscall(SYSIO_SYS_readlink, path, buf, bufsiz);
1155 native_inop_open(struct pnode *pno, int flags, mode_t mode)
1157 struct native_inode *nino;
1161 path = _sysio_pb_path(pno->p_base, '/');
1166 * Whether the file is already open, or not, makes no difference.
1167 * Want to always give the host OS a chance to authorize in case
1168 * something has changed underneath us.
1170 if (flags & O_WRONLY) {
1172 * Promote write-only attempt to RW.
1178 flags |= O_LARGEFILE;
1180 fd = syscall(SYSIO_SYS_open, path, flags, mode);
1181 if (!pno->p_base->pb_ino && fd >= 0) {
1186 * Success but we need to return an i-node.
1188 fs = pno->p_mount->mnt_fs;
1192 _SYSIO_LOCAL_TIME() + FS2NFS(fs)->nfs_atimo,
1193 &pno->p_base->pb_ino);
1195 (void )syscall(SYSIO_SYS_close, fd);
1206 * Remember this new open.
1208 nino = I2NI(pno->p_base->pb_ino);
1210 assert(nino->ni_nopens);
1212 if (nino->ni_fd >= 0) {
1213 if ((nino->ni_oflags & O_RDWR) ||
1214 (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {
1218 (void )syscall(SYSIO_SYS_close, fd);
1221 (void )syscall(SYSIO_SYS_close, nino->ni_fd);
1224 * Invariant; First open. Must init.
1229 * Need to know whether we can seek on this
1233 native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_CUR) != 0 ? 0 : 1;
1239 native_inop_close(struct inode *ino)
1241 struct native_inode *nino = I2NI(ino);
1244 if (nino->ni_fd < 0)
1247 assert(nino->ni_nopens);
1248 if (--nino->ni_nopens) {
1250 * Hmmm. We really don't need anything else. However, some
1251 * filesystems try to implement a sync-on-close semantic.
1252 * As this appears now, that is lost. Might want to change
1253 * it somehow in the future?
1258 err = syscall(SYSIO_SYS_close, nino->ni_fd);
1268 native_inop_link(struct pnode *old, struct pnode *new)
1271 char *opath, *npath;
1275 opath = _sysio_pb_path(old->p_base, '/');
1276 npath = _sysio_pb_path(new->p_base, '/');
1277 if (!(opath && npath)) {
1282 err = syscall(SYSIO_SYS_link, opath, npath);
1295 native_inop_unlink(struct pnode *pno)
1300 path = _sysio_pb_path(pno->p_base, '/');
1305 * For this driver, unlink is easy with open files. Since the
1306 * file remains open to the system, too, the descriptors are still
1309 * Other drivers will have some difficulty here as the entry in the
1310 * file system name space must be removed without sacrificing access
1311 * to the file itself. In NFS this is done with a mechanism referred
1312 * to as a `silly delete'. The file is moved to a temporary name
1313 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some
1314 * unique characters) in order to simulate the proper semantic.
1316 if (syscall(SYSIO_SYS_unlink, path) != 0)
1323 native_inop_rename(struct pnode *old, struct pnode *new)
1326 char *opath, *npath;
1328 opath = _sysio_pb_path(old->p_base, '/');
1329 npath = _sysio_pb_path(new->p_base, '/');
1330 if (!(opath && npath)) {
1335 err = syscall(SYSIO_SYS_rename, opath, npath);
1348 dopio(void *buf, size_t count, _SYSIO_OFF_T off, struct native_io *nio)
1352 if (!(off == nio->nio_nino->ni_fpos || nio->nio_nino->ni_seekok))
1355 if (!nio->nio_nino->ni_seekok) {
1356 if (off != nio->nio_nino->ni_fpos) {
1358 * They've done a p{read,write} or somesuch. Can't
1359 * seek on this descriptor so we err out now.
1365 syscall(nio->nio_op == 'r'
1368 nio->nio_nino->ni_fd,
1372 nio->nio_nino->ni_fpos += cc;
1375 syscall((nio->nio_op == 'r'
1377 : SYSIO_SYS_pwrite),
1378 nio->nio_nino->ni_fd,
1387 doiov(const struct iovec *iov,
1391 struct native_io *nio)
1395 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1396 #define MAX_IOVEC INT_MAX
1404 * Avoid the reposition call if we're already at the right place.
1405 * Allows us to access pipes and fifos.
1407 if (off != nio->nio_nino->ni_fpos) {
1410 err = native_pos(nio->nio_nino->ni_fd, &off, SEEK_SET);
1413 nio->nio_nino->ni_fpos = off;
1417 * The {read,write}v is safe as this routine is only ever called
1418 * by _sysio_enumerate_extents() and that routine is exact. It never
1419 * passes iovectors including tails.
1424 ? syscall(nio->nio_op == 'r' ? SYSIO_SYS_readv : SYSIO_SYS_writev,
1425 nio->nio_nino->ni_fd,
1430 _sysio_enumerate_iovec(iov,
1434 (ssize_t (*)(void *,
1442 nio->nio_nino->ni_fpos += cc;
1445 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1452 lockop_all(struct native_inode *nino,
1453 struct intnl_xtvec *xtv,
1463 flock.l_whence = SEEK_SET;
1465 flock.l_start = xtv->xtv_off;
1466 flock.l_len = xtv->xtv_len;
1469 syscall(SYSIO_SYS_fcntl,
1480 order_xtv(const struct intnl_xtvec *xtv1, const struct intnl_xtvec *xtv2)
1483 if (xtv1->xtv_off < xtv2->xtv_off)
1485 if (xtv1->xtv_off > xtv2->xtv_off)
1492 doio(char op, struct ioctx *ioctx)
1494 struct native_inode *nino;
1497 struct intnl_xtvec *oxtv;
1500 struct native_io arguments;
1503 struct intnl_xtvec *front, *rear, tmp;
1506 nino = I2NI(ioctx->ioctx_ino);
1508 dolocks = ioctx->ioctx_xtvlen > 1 && nino->ni_seekok;
1511 * Must lock the regions (in order!) since we can't do
1512 * strided-IO as a single atomic operation.
1514 oxtv = malloc(ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1519 ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1521 ioctx->ioctx_xtvlen,
1522 sizeof(struct intnl_xtvec),
1523 (int (*)(const void *, const void *))order_xtv);
1526 oxtv, ioctx->ioctx_xtvlen,
1527 op == 'r' ? F_RDLCK : F_WRLCK);
1534 arguments.nio_op = op;
1535 arguments.nio_nino = nino;
1537 _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1538 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1539 (ssize_t (*)(const struct iovec *,
1548 * Must unlock in reverse order.
1551 rear = front + ioctx->ioctx_xtvlen - 1;
1552 while (front < rear) {
1557 if (lockop_all(nino, oxtv, ioctx->ioctx_xtvlen, F_UNLCK) != 0)
1562 if ((ioctx->ioctx_cc = cc) < 0) {
1563 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1564 ioctx->ioctx_cc = -1;
1570 native_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1573 return doio('r', ioctx);
1577 native_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1580 return doio('w', ioctx);
1584 native_inop_pos(struct inode *ino, _SYSIO_OFF_T off)
1586 struct native_inode *nino = I2NI(ino);
1589 err = native_pos(nino->ni_fd, &off, SEEK_SET);
1590 return err < 0 ? err : off;
1594 native_inop_iodone(struct ioctx *ioctxp __IS_UNUSED)
1598 * It's always done in this driver. It completed when posted.
1604 native_inop_fcntl(struct inode *ino,
1609 struct native_inode *nino = I2NI(ino);
1613 if (nino->ni_fd < 0)
1623 *rtn = syscall(SYSIO_SYS_fcntl, nino->ni_fd, cmd);
1636 arg = va_arg(ap, long);
1637 *rtn = syscall(SYSIO_SYS_fcntl, nino->ni_fd, cmd, arg);
1649 native_inop_mknod(struct pnode *pno __IS_UNUSED,
1650 mode_t mode __IS_UNUSED,
1651 dev_t dev __IS_UNUSED)
1657 #ifdef _HAVE_STATVFS
1659 native_inop_statvfs(struct pnode *pno,
1661 struct intnl_statvfs *buf)
1668 if (!ino || I2NI(ino)->ni_fd < 0) {
1669 path = _sysio_pb_path(pno->p_base, '/');
1675 * The syscall interface does not support SYSIO_SYS_fstatvfs.
1676 * Should possibly return ENOSYS, but thought it
1677 * better to use SYSIO_SYS_fstatfs and fill in as much of
1678 * the statvfs structure as possible. This allows
1679 * for more of a test of the sysio user interface.
1683 ? syscall(SYSIO_SYS_statfs, path, &fs)
1684 : syscall(SYSIO_SYS_fstatfs, I2NI(ino)->ni_fd, &fs);
1690 buf->f_bsize = fs.f_bsize; /* file system block size */
1691 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1692 buf->f_blocks = fs.f_blocks;
1693 buf->f_bfree = fs.f_bfree;
1694 buf->f_bavail = fs.f_bavail;
1695 buf->f_files = fs.f_files; /* Total number serial numbers */
1696 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1697 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1698 buf->f_fsid = fs.f_fsid.__val[1];
1699 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1700 buf->f_namemax = fs.f_namelen;
1706 native_inop_sync(struct inode *ino)
1710 assert(I2NI(ino)->ni_fd >= 0);
1712 err = syscall(SYSIO_SYS_fsync, I2NI(ino)->ni_fd);
1719 native_inop_datasync(struct inode *ino)
1721 struct native_inode *nino;
1725 assert(nino->ni_fd >= 0);
1727 #ifdef SYSIO_SYS_fdatasync
1728 err = syscall(SYSIO_SYS_fdatasync, I2NI(ino)->ni_fd);
1731 #warning No fdatasync system call -- Using fsync instead!
1733 err = syscall(SYSIO_SYS_fsync, I2NI(ino)->ni_fd);
1740 #ifdef HAVE_LUSTRE_HACK
1742 native_inop_ioctl(struct inode *ino,
1743 unsigned long int request,
1746 struct native_inode *nino;
1747 long arg1, arg2, arg3, arg4;
1750 assert(nino->ni_fd >= 0);
1751 arg1 = va_arg(ap, long);
1752 arg2 = va_arg(ap, long);
1753 arg3 = va_arg(ap, long);
1754 arg4 = va_arg(ap, long);
1756 return syscall(SYSIO_SYS_ioctl, I2NI(ino)->ni_fd, request,
1757 arg1, arg2, arg3, arg4);
1761 native_inop_ioctl(struct inode *ino __IS_UNUSED,
1762 unsigned long int request __IS_UNUSED,
1763 va_list ap __IS_UNUSED)
1767 * I'm lazy. Maybe implemented later.
1775 native_inop_gone(struct inode *ino)
1777 struct native_inode *nino = I2NI(ino);
1779 if (nino->ni_fd >= 0)
1780 (void )syscall(SYSIO_SYS_close, nino->ni_fd);
1782 free(ino->i_private);
1786 native_fsop_gone(struct filesys *fs __IS_UNUSED)
1789 free(fs->fs_private);
1791 * Do nothing. There is no private part maintained for the
1792 * native file interface.