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.
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);
508 native_fsswop_mount(const char *source,
511 struct pnode *tocover,
515 struct nameidata nameidata;
519 * Caller must use fully qualified path names when specifying
525 if (!native_internal_mount) {
526 err = create_internal_namespace(data);
529 } else if (data && *(char *)data)
533 * Lookup the source in the internally maintained name space.
535 ND_INIT(&nameidata, 0, source, native_internal_mount->mnt_root, NULL);
536 err = _sysio_path_walk(native_internal_mount->mnt_root, &nameidata);
541 * Have path-node specified by the given source argument. Let the
542 * system finish the job, now.
545 _sysio_do_mount(native_internal_mount->mnt_fs,
546 nameidata.nd_pno->p_base,
551 * Release the internal name space pnode and clean up any
552 * aliases we might have generated. We really don't need to cache them
553 * as they are only used at mount time..
555 P_RELE(nameidata.nd_pno);
556 (void )_sysio_p_prune(native_internal_mount->mnt_root);
559 FS_REF(native_internal_mount->mnt_fs);
566 native_i_invalid(struct inode *inop, struct intnl_stat *stat)
568 struct native_inode *nino;
571 * Validate passed in inode against stat struct info
575 if (!nino->ni_attrtim ||
576 (nino->ni_ident.dev != stat->st_dev ||
577 nino->ni_ident.ino != stat->st_ino ||
578 #ifdef HAVE_GENERATION
579 nino->ni_ident.gen != stat->st_gen ||
581 ((inop)->i_stbuf.st_mode & S_IFMT) != (stat->st_mode & S_IFMT)) ||
582 (((inop)->i_stbuf.st_rdev != stat->st_rdev) &&
583 (S_ISCHR((inop)->i_stbuf.st_mode) ||
584 S_ISBLK((inop)->i_stbuf.st_mode)))) {
585 nino->ni_attrtim = 0; /* invalidate attrs */
591 static struct inode *
592 native_iget(struct filesys *fs, time_t expire, struct intnl_stat *stbp)
595 struct native_inode_identifier ident;
596 struct file_identifier fileid;
598 bzero(&ident, sizeof(ident));
599 ident.dev = stbp->st_dev;
600 ident.ino = stbp->st_ino;
601 #ifdef HAVE_GENERATION
602 ident.gen = stbp->st_gen;
604 fileid.fid_data = &ident;
605 fileid.fid_len = sizeof(ident);
606 ino = _sysio_i_find(fs, &fileid);
608 ino->i_stbuf = *stbp;
609 I2NI(ino)->ni_attrtim = expire;
612 return native_i_new(fs, expire, stbp);
616 * Find, and validate, or create i-node by host-relative path. Returned i-node
620 native_ibind(struct filesys *fs,
625 struct intnl_stat ostbuf, stbuf;
630 ostbuf = (*inop)->i_stbuf;
632 err = native_stat(path, *inop, t, &stbuf);
640 if (!native_i_invalid(*inop, &ostbuf))
645 _sysio_i_undead(*inop);
649 if (!(ino = native_iget(fs, t + FS2NFS(fs)->nfs_atimo, &stbuf)))
657 native_inop_lookup(struct pnode *pno,
659 struct intent *intnt __IS_UNUSED,
660 const char *path __IS_UNUSED)
667 *inop = pno->p_base->pb_ino;
670 * Try to use the cached attributes unless the intent
671 * indicates we are looking up the last component and
672 * caller wants attributes. In that case, force a refresh.
674 t = _SYSIO_LOCAL_TIME();
676 (path || !intnt || (intnt->int_opmask & INT_GETATTR) == 0) &&
677 NATIVE_ATTRS_VALID(I2NI(*inop), t))
681 * Don't have an inode yet. Because we translate everything back to
682 * a single name space for the host, we will assume the object the
683 * caller is looking for has no existing alias in our internal
684 * name space. We don't see the same file on different mounts in the
685 * underlying host FS as the same file.
687 * The file identifier *will* be unique. It's got to have a different
690 fqpath = _sysio_pb_path(pno->p_base, '/');
693 fs = pno->p_mount->mnt_fs;
694 err = native_ibind(fs, fqpath, t + FS2NFS(fs)->nfs_atimo, inop);
702 native_inop_getattr(struct pnode *pno,
704 struct intnl_stat *stat)
706 struct native_inode *nino;
710 * We just cannot use the cached attributes when getattr is
711 * called. Had the caller felt those were sufficient then
712 * they could have (would have?) simply used what was cached
713 * after revalidating. In this case, there's a good chance the
714 * caller is looking for the current time stamps and/or size. Something
715 * pretty volatile anyway.
717 err = 0; /* compiler cookie */
723 path = _sysio_pb_path(pno->p_base, '/');
726 fs = pno->p_mount->mnt_fs;
727 t = _SYSIO_LOCAL_TIME();
728 err = native_stat(path, ino, t + FS2NFS(fs)->nfs_atimo, stat);
730 } else if ((nino = I2NI(ino))->ni_fd >= 0)
732 * Don't have access to the fs record anymore. Just
733 * refresh but keep the current timeout.
735 err = native_stat(NULL, ino, nino->ni_attrtim, stat);
738 * Dev inodes don't open in this driver. We won't have
739 * a file descriptor with which to do the deed then. Satisfy
740 * the request from the cached copy of the attributes.
744 sizeof(struct intnl_stat));
751 #ifdef SYSIO_SYS_utime
753 _ut(const char *path, time_t actime, time_t modtime)
758 ut.modtime = modtime;
759 return syscall(SYSIO_SYS_utime, path, &ut);
763 _ut(const char *path, time_t actime, time_t modtime)
765 struct timeval tv[2];
767 tv[0].tv_sec = actime;
769 tv[1].tv_sec = modtime;
771 return syscall(SYSIO_SYS_utimes, path, &tv);
776 native_inop_setattr(struct pnode *pno,
779 struct intnl_stat *stat)
782 struct native_inode *nino;
787 nino = ino ? I2NI(ino) : NULL;
791 if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {
794 path = _sysio_pb_path(pno->p_base, '/');
800 * Get current status for undo.
802 err = native_stat(path, ino, 0, NULL);
806 if (mask & SETATTR_MODE) {
810 * Alter permissions attribute.
812 mode = stat->st_mode & 07777;
815 ? syscall(SYSIO_SYS_chmod, path, mode)
816 : syscall(SYSIO_SYS_fchmod, fd, mode);
821 mask &= ~SETATTR_MODE;
822 else if (mask & (SETATTR_MTIME|SETATTR_ATIME)) {
823 time_t actime, modtime;
826 * Alter access and/or modify time attributes.
828 actime = ino->i_stbuf.st_atime;
829 modtime = ino->i_stbuf.st_mtime;
830 if (mask & SETATTR_ATIME)
831 actime = stat->st_atime;
832 if (mask & SETATTR_MTIME)
833 modtime = stat->st_mtime;
834 if (_ut(path, actime, modtime) != 0)
838 mask &= ~(SETATTR_MTIME|SETATTR_ATIME);
839 else if (mask & (SETATTR_UID|SETATTR_GID)) {
842 * Alter owner and/or group identifiers.
846 ? syscall(SYSIO_SYS_chown,
854 : syscall(SYSIO_SYS_fchown,
866 mask &= ~(SETATTR_UID|SETATTR_GID);
867 else if (mask & SETATTR_LEN) {
869 * Do the truncate last. It can't be undone.
872 ? syscall(SYSIO_SYS_truncate, path, stat->st_size)
873 : syscall(SYSIO_SYS_ftruncate, fd, stat->st_size));
878 * Undo after error. Some or all of this might not work... We
881 if (mask & (SETATTR_UID|SETATTR_GID)) {
883 ? syscall(SYSIO_SYS_chown,
886 ? ino->i_stbuf.st_uid
889 ? ino->i_stbuf.st_gid
891 : syscall(SYSIO_SYS_fchown,
894 ? ino->i_stbuf.st_uid
897 ? ino->i_stbuf.st_gid
900 if (mask & (SETATTR_MTIME|SETATTR_ATIME))
901 (void )_ut(path, ino->i_stbuf.st_atime, ino->i_stbuf.st_mtime);
902 if (mask & SETATTR_MODE) {
904 ? syscall(SYSIO_SYS_chmod, path, ino->i_stbuf.st_mode & 07777)
905 : syscall(SYSIO_SYS_fchmod, ino->i_stbuf.st_mode & 07777);
909 * We must refresh the cached attributes.
911 if (!err && native_stat(path, ino, _SYSIO_LOCAL_TIME(), NULL) != 0)
919 native_pos(int fd, _SYSIO_OFF_T *offset, int whence)
924 assert(*offset >= 0);
927 #if _LARGEFILE64_SOURCE && defined(SYSIO_SYS__llseek)
931 syscall(SYSIO_SYS__llseek,
933 (unsigned int)(off >> 32),
942 syscall(SYSIO_SYS_lseek,
955 native_ifilldirentries(struct native_inode *nino,
962 #if defined(SYSIO_SYS_getdirentries)
970 * Stream-oriented access requires that we reposition prior to the
973 assert(nino->ni_seekok);
974 if (*posp != nino->ni_fpos || nino->ni_resetfpos) {
975 nino->ni_fpos = *posp;
976 err = native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_SET);
978 nino->ni_resetfpos = 1;
981 nino->ni_resetfpos = 0;
985 #if defined(SYSIO_SYS_getdirentries)
986 syscall(SYSIO_SYS_getdirentries,
991 #elif defined(SYSIO_SYS_getdents64)
992 syscall(SYSIO_SYS_getdents64, nino->ni_fd, buf, nbytes);
993 #elif defined(SYSIO_SYS_getdents)
994 syscall(SYSIO_SYS_getdents, nino->ni_fd, buf, nbytes);
1000 * Stream-oriented access requires that we discover where we are
1003 if ((err = native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_CUR)) != 0) {
1005 * Leave the position at the old I suppose.
1007 nino->ni_resetfpos = 1;
1010 *posp = nino->ni_fpos;
1015 native_filldirentries(struct inode *ino,
1020 struct native_inode *nino = I2NI(ino);
1024 struct linux_dirent *ldp;
1025 struct dirent64 *d64p;
1030 #define count nbytes
1034 assert(nino->ni_fd >= 0);
1038 while (!(bp = malloc(count))) {
1040 if (count < sizeof(struct dirent))
1044 cc = native_ifilldirentries(nino, posp, bp, count);
1052 ldp = (struct linux_dirent *)bp;
1053 d64p = (struct dirent64 *)buf;
1055 namlen = strlen(ldp->ld_name);
1056 reclen = sizeof(*d64p) - sizeof(d64p->d_name) + namlen;
1057 if (nbytes <= reclen)
1059 d64p->d_ino = ldp->ld_ino;
1060 d64p->d_off = nino->ni_fpos = ldp->ld_off;
1062 (((reclen + sizeof(long))) / sizeof(long)) * sizeof(long);
1063 if (nbytes < d64p->d_reclen)
1064 d64p->d_reclen = reclen + 1;
1065 d64p->d_type = DT_UNKNOWN; /* you lose -- sorry. */
1066 (void )memcpy(d64p->d_name, ldp->ld_name, namlen);
1068 * Zero pad the rest.
1070 for (cp = d64p->d_name + namlen, n = d64p->d_reclen - reclen;
1074 cc -= ldp->ld_reclen;
1075 ldp = (struct linux_dirent *)((char *)ldp + ldp->ld_reclen);
1076 nbytes -= d64p->d_reclen;
1077 d64p = (struct dirent64 *)((char *)d64p + d64p->d_reclen);
1081 (d64p == (struct dirent64 *)buf && cc)
1083 : (char *)d64p - buf;
1092 native_inop_mkdir(struct pnode *pno, mode_t mode)
1097 path = _sysio_pb_path(pno->p_base, '/');
1101 err = syscall(SYSIO_SYS_mkdir, path, mode);
1109 native_inop_rmdir(struct pnode *pno)
1114 path = _sysio_pb_path(pno->p_base, '/');
1118 err = syscall(SYSIO_SYS_rmdir, path);
1126 native_inop_symlink(struct pnode *pno, const char *data)
1131 path = _sysio_pb_path(pno->p_base, '/');
1135 err = syscall(SYSIO_SYS_symlink, data, path);
1143 native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz)
1148 path = _sysio_pb_path(pno->p_base, '/');
1151 i = syscall(SYSIO_SYS_readlink, path, buf, bufsiz);
1159 native_inop_open(struct pnode *pno, int flags, mode_t mode)
1161 struct native_inode *nino;
1165 path = _sysio_pb_path(pno->p_base, '/');
1170 * Whether the file is already open, or not, makes no difference.
1171 * Want to always give the host OS a chance to authorize in case
1172 * something has changed underneath us.
1174 if (flags & O_WRONLY) {
1176 * Promote write-only attempt to RW.
1182 flags |= O_LARGEFILE;
1184 fd = syscall(SYSIO_SYS_open, path, flags, mode);
1185 if (!pno->p_base->pb_ino && fd >= 0) {
1190 * Success but we need to return an i-node.
1192 fs = pno->p_mount->mnt_fs;
1196 _SYSIO_LOCAL_TIME() + FS2NFS(fs)->nfs_atimo,
1197 &pno->p_base->pb_ino);
1199 (void )syscall(SYSIO_SYS_close, fd);
1210 * Remember this new open.
1212 nino = I2NI(pno->p_base->pb_ino);
1214 assert(nino->ni_nopens);
1216 if (nino->ni_fd >= 0) {
1217 if ((nino->ni_oflags & O_RDWR) ||
1218 (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {
1222 (void )syscall(SYSIO_SYS_close, fd);
1225 (void )syscall(SYSIO_SYS_close, nino->ni_fd);
1228 * Invariant; First open. Must init.
1230 nino->ni_resetfpos = 0;
1234 * Need to know whether we can seek on this
1238 native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_CUR) != 0 ? 0 : 1;
1244 native_inop_close(struct inode *ino)
1246 struct native_inode *nino = I2NI(ino);
1249 if (nino->ni_fd < 0)
1252 assert(nino->ni_nopens);
1253 if (--nino->ni_nopens) {
1255 * Hmmm. We really don't need anything else. However, some
1256 * filesystems try to implement a sync-on-close semantic.
1257 * As this appears now, that is lost. Might want to change
1258 * it somehow in the future?
1263 err = syscall(SYSIO_SYS_close, nino->ni_fd);
1268 nino->ni_resetfpos = 0;
1274 native_inop_link(struct pnode *old, struct pnode *new)
1277 char *opath, *npath;
1281 opath = _sysio_pb_path(old->p_base, '/');
1282 npath = _sysio_pb_path(new->p_base, '/');
1283 if (!(opath && npath)) {
1288 err = syscall(SYSIO_SYS_link, opath, npath);
1301 native_inop_unlink(struct pnode *pno)
1306 path = _sysio_pb_path(pno->p_base, '/');
1311 * For this driver, unlink is easy with open files. Since the
1312 * file remains open to the system, too, the descriptors are still
1315 * Other drivers will have some difficulty here as the entry in the
1316 * file system name space must be removed without sacrificing access
1317 * to the file itself. In NFS this is done with a mechanism referred
1318 * to as a `silly delete'. The file is moved to a temporary name
1319 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some
1320 * unique characters) in order to simulate the proper semantic.
1322 if (syscall(SYSIO_SYS_unlink, path) != 0)
1329 native_inop_rename(struct pnode *old, struct pnode *new)
1332 char *opath, *npath;
1334 opath = _sysio_pb_path(old->p_base, '/');
1335 npath = _sysio_pb_path(new->p_base, '/');
1336 if (!(opath && npath)) {
1341 err = syscall(SYSIO_SYS_rename, opath, npath);
1354 dopio(void *buf, size_t count, _SYSIO_OFF_T off, struct native_io *nio)
1358 if (!nio->nio_nino->ni_seekok) {
1359 if (off != nio->nio_nino->ni_fpos) {
1361 * They're trying to reposition. Can't
1362 * seek on this descriptor so we err out now.
1368 syscall(nio->nio_op == 'r'
1371 nio->nio_nino->ni_fd,
1375 nio->nio_nino->ni_fpos += cc;
1378 syscall((nio->nio_op == 'r'
1380 : SYSIO_SYS_pwrite),
1381 nio->nio_nino->ni_fd,
1390 doiov(const struct iovec *iov,
1394 struct native_io *nio)
1398 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1399 #define MAX_IOVEC INT_MAX
1407 * Avoid the reposition call if we're already at the right place.
1408 * Allows us to access pipes and fifos.
1410 if (off != nio->nio_nino->ni_fpos) {
1413 err = native_pos(nio->nio_nino->ni_fd, &off, SEEK_SET);
1415 nio->nio_nino->ni_resetfpos = 1;
1418 nio->nio_nino->ni_resetfpos = 0;
1419 nio->nio_nino->ni_fpos = off;
1423 * The {read,write}v is safe as this routine is only ever called
1424 * by _sysio_enumerate_extents() and that routine is exact. It never
1425 * passes iovectors including tails.
1430 ? syscall(nio->nio_op == 'r' ? SYSIO_SYS_readv : SYSIO_SYS_writev,
1431 nio->nio_nino->ni_fd,
1436 _sysio_enumerate_iovec(iov,
1440 (ssize_t (*)(void *,
1448 nio->nio_nino->ni_fpos += cc;
1451 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1458 lockop_all(struct native_inode *nino,
1459 struct intnl_xtvec *xtv,
1469 flock.l_whence = SEEK_SET;
1471 flock.l_start = xtv->xtv_off;
1472 flock.l_len = xtv->xtv_len;
1475 syscall(SYSIO_SYS_fcntl,
1486 order_xtv(const struct intnl_xtvec *xtv1, const struct intnl_xtvec *xtv2)
1489 if (xtv1->xtv_off < xtv2->xtv_off)
1491 if (xtv1->xtv_off > xtv2->xtv_off)
1498 doio(char op, struct ioctx *ioctx)
1500 struct native_inode *nino;
1503 struct intnl_xtvec *oxtv;
1506 struct native_io arguments;
1509 struct intnl_xtvec *front, *rear, tmp;
1512 nino = I2NI(ioctx->ioctx_ino);
1514 dolocks = ioctx->ioctx_xtvlen > 1 && nino->ni_seekok;
1517 * Must lock the regions (in order!) since we can't do
1518 * strided-IO as a single atomic operation.
1520 oxtv = malloc(ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1525 ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1527 ioctx->ioctx_xtvlen,
1528 sizeof(struct intnl_xtvec),
1529 (int (*)(const void *, const void *))order_xtv);
1532 oxtv, ioctx->ioctx_xtvlen,
1533 op == 'r' ? F_RDLCK : F_WRLCK);
1540 arguments.nio_op = op;
1541 arguments.nio_nino = nino;
1543 _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1544 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1545 (ssize_t (*)(const struct iovec *,
1554 * Must unlock in reverse order.
1557 rear = front + ioctx->ioctx_xtvlen - 1;
1558 while (front < rear) {
1563 if (lockop_all(nino, oxtv, ioctx->ioctx_xtvlen, F_UNLCK) != 0)
1568 if ((ioctx->ioctx_cc = cc) < 0) {
1569 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1570 ioctx->ioctx_cc = -1;
1576 native_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1579 return doio('r', ioctx);
1583 native_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1586 return doio('w', ioctx);
1590 native_inop_pos(struct inode *ino, _SYSIO_OFF_T off)
1592 struct native_inode *nino = I2NI(ino);
1595 err = native_pos(nino->ni_fd, &off, SEEK_SET);
1596 return err < 0 ? err : off;
1600 native_inop_iodone(struct ioctx *ioctxp __IS_UNUSED)
1604 * It's always done in this driver. It completed when posted.
1610 native_inop_fcntl(struct inode *ino,
1615 struct native_inode *nino = I2NI(ino);
1619 if (nino->ni_fd < 0)
1629 *rtn = syscall(SYSIO_SYS_fcntl, nino->ni_fd, cmd);
1642 arg = va_arg(ap, long);
1643 *rtn = syscall(SYSIO_SYS_fcntl, nino->ni_fd, cmd, arg);
1655 native_inop_mknod(struct pnode *pno __IS_UNUSED,
1656 mode_t mode __IS_UNUSED,
1657 dev_t dev __IS_UNUSED)
1663 #ifdef _HAVE_STATVFS
1665 native_inop_statvfs(struct pnode *pno,
1667 struct intnl_statvfs *buf)
1674 if (!ino || I2NI(ino)->ni_fd < 0) {
1675 path = _sysio_pb_path(pno->p_base, '/');
1681 * The syscall interface does not support SYSIO_SYS_fstatvfs.
1682 * Should possibly return ENOSYS, but thought it
1683 * better to use SYSIO_SYS_fstatfs and fill in as much of
1684 * the statvfs structure as possible. This allows
1685 * for more of a test of the sysio user interface.
1689 ? syscall(SYSIO_SYS_statfs, path, &fs)
1690 : syscall(SYSIO_SYS_fstatfs, I2NI(ino)->ni_fd, &fs);
1696 buf->f_bsize = fs.f_bsize; /* file system block size */
1697 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1698 buf->f_blocks = fs.f_blocks;
1699 buf->f_bfree = fs.f_bfree;
1700 buf->f_bavail = fs.f_bavail;
1701 buf->f_files = fs.f_files; /* Total number serial numbers */
1702 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1703 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1704 buf->f_fsid = fs.f_fsid.__val[1];
1705 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1706 buf->f_namemax = fs.f_namelen;
1712 native_inop_sync(struct inode *ino)
1716 assert(I2NI(ino)->ni_fd >= 0);
1718 err = syscall(SYSIO_SYS_fsync, I2NI(ino)->ni_fd);
1725 native_inop_datasync(struct inode *ino)
1727 struct native_inode *nino;
1731 assert(nino->ni_fd >= 0);
1733 #ifdef SYSIO_SYS_fdatasync
1734 err = syscall(SYSIO_SYS_fdatasync, I2NI(ino)->ni_fd);
1737 #warning No fdatasync system call -- Using fsync instead!
1739 err = syscall(SYSIO_SYS_fsync, I2NI(ino)->ni_fd);
1746 #ifdef HAVE_LUSTRE_HACK
1748 native_inop_ioctl(struct inode *ino,
1749 unsigned long int request,
1752 struct native_inode *nino;
1753 long arg1, arg2, arg3, arg4;
1757 assert(nino->ni_fd >= 0);
1758 arg1 = va_arg(ap, long);
1759 arg2 = va_arg(ap, long);
1760 arg3 = va_arg(ap, long);
1761 arg4 = va_arg(ap, long);
1764 syscall(SYSIO_SYS_ioctl, I2NI(ino)->ni_fd, request,
1765 arg1, arg2, arg3, arg4);
1772 native_inop_ioctl(struct inode *ino __IS_UNUSED,
1773 unsigned long int request __IS_UNUSED,
1774 va_list ap __IS_UNUSED)
1778 * I'm lazy. Maybe implemented later.
1785 native_inop_gone(struct inode *ino)
1787 struct native_inode *nino = I2NI(ino);
1789 if (nino->ni_fd >= 0)
1790 (void )syscall(SYSIO_SYS_close, nino->ni_fd);
1792 free(ino->i_private);
1796 native_fsop_gone(struct filesys *fs __IS_UNUSED)
1799 free(fs->fs_private);
1801 * Do nothing. There is no private part maintained for the
1802 * native file interface.