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 */
57 #include <sys/types.h>
59 #include <sys/fcntl.h>
64 #include <sys/statvfs.h>
65 #include <sys/statfs.h>
68 #include <sys/queue.h>
69 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
79 #include "fs_native.h"
85 #if defined(SYS_getdirentries)
86 #define DIR_STREAMED 0
88 #elif defined(SYS_getdents64)
89 #define DIR_STREAMED 1
91 #elif defined(SYS_getdents)
92 #define DIR_STREAMED 1
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 * Local host file system driver.
116 #if defined(ALPHA_LINUX)
118 /* stat struct from asm/stat.h, as returned
119 * by alpha linux kernel
121 struct __native_stat {
124 unsigned int st_mode;
125 unsigned int st_nlink;
128 unsigned int st_rdev;
130 unsigned long st_atime;
131 unsigned long st_mtime;
132 unsigned long st_ctime;
133 unsigned int st_blksize;
135 unsigned int st_flags;
139 #define COPY_STAT(src, dest) \
141 memset((dest), 0, sizeof((*dest))); \
142 (dest)->st_dev = (src)->st_dev; \
143 (dest)->st_ino = (src)->st_ino; \
144 (dest)->st_mode = (src)->st_mode; \
145 (dest)->st_nlink = (src)->st_nlink; \
146 (dest)->st_uid = (src)->st_uid; \
147 (dest)->st_gid = (src)->st_gid; \
148 (dest)->st_rdev = (src)->st_rdev; \
149 (dest)->st_size = (src)->st_size; \
150 (dest)->st_atime = (src)->st_atime; \
151 (dest)->st_mtime = (src)->st_mtime; \
152 (dest)->st_ctime = (src)->st_ctime; \
153 (dest)->st_blksize = (src)->st_blksize; \
154 (dest)->st_blocks = (src)->st_blocks; \
155 (dest)->st_flags = (src)->st_flags; \
156 (dest)->st_gen = (src)->st_gen; \
160 #define __native_stat intnl_stat
161 #define COPY_STAT(src, dest) *(dest) = *(src)
164 #if defined(USE_NATIVE_STAT)
165 #define __SYS_STAT SYS_lstat
166 #define __SYS_FSTAT SYS_fstat
167 #define __SYS_TRUNCATE SYS_truncate
168 #define __SYS_FTRUNCATE SYS_ftruncate
170 #define __SYS_STAT SYS_lstat64
171 #define __SYS_FSTAT SYS_fstat64
172 #define __SYS_TRUNCATE SYS_truncate64
173 #define __SYS_FTRUNCATE SYS_ftruncate64
176 #if defined(USE_NATIVE_FDATASYNC)
177 #define __SYS_FDATASYNC SYS_osf_fdatasync
179 #define __SYS_FDATASYNC SYS_fdatasync
182 #if defined(USE_NATIVE_UTIME)
183 #define __SYS_UTIME SYS_utimes
185 #define __SYS_UTIME SYS_utime
189 * Native file identifiers format.
191 struct native_inode_identifier {
192 dev_t dev; /* device number */
193 ino_t ino; /* i-number */
194 #ifdef HAVE_GENERATION
195 unsigned int gen; /* generation number */
200 * Driver-private i-node information we keep about local host file
203 struct native_inode {
205 ni_seekok : 1; /* can seek? */
206 struct native_inode_identifier ni_ident; /* unique identifier */
207 struct file_identifier ni_fileid; /* ditto */
208 int ni_fd; /* host fildes */
209 int ni_oflags; /* flags, from open */
210 unsigned ni_nopens; /* soft ref count */
211 _SYSIO_OFF_T ni_fpos; /* current pos */
215 * Native IO path arguments.
218 char nio_op; /* 'r' or 'w' */
219 struct native_inode *nio_nino; /* native ino */
222 static int native_inop_lookup(struct pnode *pno,
224 struct intent *intnt,
226 static int native_inop_getattr(struct pnode *pno,
228 struct intnl_stat *stbuf);
229 static int native_inop_setattr(struct pnode *pno,
232 struct intnl_stat *stbuf);
233 static ssize_t native_getdirentries(struct inode *ino,
236 _SYSIO_OFF_T *basep);
237 static int native_inop_mkdir(struct pnode *pno, mode_t mode);
238 static int native_inop_rmdir(struct pnode *pno);
239 static int native_inop_symlink(struct pnode *pno, const char *data);
240 static int native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz);
241 static int native_inop_open(struct pnode *pno, int flags, mode_t mode);
242 static int native_inop_close(struct inode *ino);
243 static int native_inop_link(struct pnode *old, struct pnode *new);
244 static int native_inop_unlink(struct pnode *pno);
245 static int native_inop_rename(struct pnode *old, struct pnode *new);
246 static int native_inop_read(struct inode *ino, struct ioctx *ioctx);
247 static int native_inop_write(struct inode *ino, struct ioctx *ioctx);
248 static _SYSIO_OFF_T native_inop_pos(struct inode *ino, _SYSIO_OFF_T off);
249 static int native_inop_iodone(struct ioctx *ioctx);
250 static int native_inop_fcntl(struct inode *ino, int cmd, va_list ap);
251 static int native_inop_sync(struct inode *ino);
252 static int native_inop_datasync(struct inode *ino);
253 static int native_inop_ioctl(struct inode *ino,
254 unsigned long int request,
256 static int native_inop_mknod(struct pnode *pno, mode_t mode, dev_t dev);
258 static int native_inop_statvfs(struct pnode *pno,
260 struct intnl_statvfs *buf);
262 static void native_inop_gone(struct inode *ino);
264 static struct inode_ops native_i_ops = {
268 native_getdirentries,
272 native_inop_readlink,
284 native_inop_datasync,
293 static int native_fsswop_mount(const char *source,
296 struct pnode *tocover,
297 struct mount **mntp);
299 static struct fssw_ops native_fssw_ops = {
303 static void native_fsop_gone(struct filesys *fs);
305 static struct filesys_ops native_inodesys_ops = {
310 * This example driver plays a strange game. It maintains a private,
311 * internal mount -- It's own separate, rooted, name space. The local
312 * file system's entire name space is available via this tree.
314 * This simplifies the implementation. At mount time, we need to generate
315 * a path-node to be used as a root. This allows us to look up the needed
316 * node in the host name space and leverage a whole lot of support from
319 static struct mount *native_internal_mount = NULL;
322 * Given i-node, return driver private part.
324 #define I2NI(ino) ((struct native_inode *)((ino)->i_private))
330 native_stat(const char *path, struct intnl_stat *buf)
333 struct __native_stat stbuf;
335 err = syscall(__SYS_STAT, path, &stbuf);
338 COPY_STAT(&stbuf, buf);
347 native_fstat(int fd, struct intnl_stat *buf)
350 struct __native_stat stbuf;
352 err = syscall(__SYS_FSTAT, fd, &stbuf);
355 COPY_STAT(&stbuf, buf);
361 * Introduce an i-node to the system.
363 static struct inode *
364 native_i_new(struct filesys *fs, struct intnl_stat *buf)
366 struct native_inode *nino;
369 nino = malloc(sizeof(struct native_inode));
372 bzero(&nino->ni_ident, sizeof(nino->ni_ident));
373 nino->ni_ident.dev = buf->st_dev;
374 nino->ni_ident.ino = buf->st_ino;
375 #ifdef HAVE_GENERATION
376 nino->ni_ident.gen = buf->st_gen;
378 nino->ni_fileid.fid_data = &nino->ni_ident;
379 nino->ni_fileid.fid_len = sizeof(nino->ni_ident);
387 #ifndef AUTOMOUNT_FILE_NAME
388 buf->st_mode & S_IFMT,
390 buf->st_mode, /* all of the bits! */
402 * Initialize this driver.
409 * Capture current process umask and reset our process umask to
410 * zero. All permission bits to open/creat/setattr are absolute --
411 * They've already had a umask applied, when appropriate.
413 _sysio_umask = syscall(SYS_umask, 0);
415 return _sysio_fssw_register("native", &native_fssw_ops);
419 * Create private, internal, view of the hosts name space.
422 create_internal_namespace()
426 struct inode *rootino;
427 struct pnode_base *rootpb;
428 static struct qstr noname = { NULL, 0, 0 };
430 struct intnl_stat stbuf;
432 if (native_internal_mount) {
440 * We maintain an artificial, internal, name space in order to
441 * have access to fully qualified path names in the various routines.
442 * Initialize that name space now.
447 fs = _sysio_fs_new(&native_inodesys_ops, 0, NULL);
456 err = native_stat("/", &stbuf);
459 rootino = native_i_new(fs, &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)
493 _sysio_pb_gone(rootpb);
503 native_fsswop_mount(const char *source,
505 const void *data __IS_UNUSED,
506 struct pnode *tocover,
510 struct nameidata nameidata;
514 * Caller must use fully qualified path names when specifying
520 if (!native_internal_mount) {
521 err = create_internal_namespace();
527 * Lookup the source in the internally maintained name space.
529 ND_INIT(&nameidata, 0, source, native_internal_mount->mnt_root, NULL);
530 err = _sysio_path_walk(native_internal_mount->mnt_root, &nameidata);
535 * Have path-node specified by the given source argument. Let the
536 * system finish the job, now.
539 _sysio_do_mount(native_internal_mount->mnt_fs,
540 nameidata.nd_pno->p_base,
545 * Release the internal name space pnode and clean up any
546 * aliases we might have generated. We really don't need to cache them
547 * as they are only used at mount time..
549 P_RELE(nameidata.nd_pno);
550 (void )_sysio_p_prune(native_internal_mount->mnt_root);
553 FS_REF(native_internal_mount->mnt_fs);
560 native_i_invalid(struct inode *inop, struct intnl_stat stbuf)
563 * Validate passed in inode against stat struct info
565 struct native_inode *nino = I2NI(inop);
567 if ((nino->ni_ident.dev != stbuf.st_dev ||
568 nino->ni_ident.ino != stbuf.st_ino ||
569 #ifdef HAVE_GENERATION
570 nino->ni_ident.gen != stbuf.st_gen ||
572 ((inop)->i_mode & S_IFMT) != (stbuf.st_mode & S_IFMT)) ||
573 (((inop)->i_rdev != stbuf.st_rdev) &&
574 (S_ISCHR((inop)->i_mode) || S_ISBLK((inop)->i_mode))))
581 * Find, and validate, or create i-node by host-relative path. Returned i-node
585 native_iget(struct filesys *fs,
592 struct intnl_stat stbuf;
593 struct native_inode_identifier ident;
594 struct file_identifier fileid;
599 err = native_stat(path, &stbuf);
609 if (!native_i_invalid(*inop, stbuf))
618 * I-node is not already known. Find or create it.
620 bzero(&ident, sizeof(ident));
621 ident.dev = stbuf.st_dev;
622 ident.ino = stbuf.st_ino;
623 #ifdef HAVE_GENERATION
624 ident.gen = stbuf.st_gen;
626 fileid.fid_data = &ident;
627 fileid.fid_len = sizeof(ident);
628 ino = _sysio_i_find(fs, &fileid);
631 * Insertion was forced but it's already present!
633 if (native_i_invalid(ino, stbuf)) {
635 * Cached inode has stale attrs
636 * make way for the new one
642 * OK to reuse cached inode
648 ino = native_i_new(fs, &stbuf);
659 * Look up named object in host's name space by path.
662 native_path_lookup(struct filesys *fs, const char *path, struct inode **inop)
665 return native_iget(fs, path, inop, 0);
669 * Look up object by it's path node.
672 native_i_lookup(struct filesys *fs, struct pnode_base *pb, struct inode **inop)
677 path = _sysio_pb_path(pb, '/');
680 err = native_path_lookup(fs, path, inop);
686 native_inop_lookup(struct pnode *pno,
688 struct intent *intnt __IS_UNUSED,
689 const char *path __IS_UNUSED)
693 *inop = pno->p_base->pb_ino;
696 * Don't have an inode yet. Because we translate everything back to
697 * a single name space for the host, we will assume the object the
698 * caller is looking for has no existing alias in our internal
699 * name space. We don't see the same file on different mounts in the
700 * underlying host FS as the same file.
702 * The file identifier *will* be unique. It's got to have a different
705 err = native_i_lookup(pno->p_mount->mnt_fs, pno->p_base, inop);
712 native_inop_getattr(struct pnode *pno, struct inode *ino, struct intnl_stat *stbuf)
718 if (!ino || I2NI(ino)->ni_fd < 0) {
719 path = _sysio_pb_path(pno->p_base, '/');
725 ? native_stat(path, stbuf)
726 : native_fstat(I2NI(ino)->ni_fd, stbuf);
733 native_inop_setattr(struct pnode *pno,
736 struct intnl_stat *stbuf)
740 struct intnl_stat st;
744 fd = ino ? I2NI(ino)->ni_fd : -1;
745 if (fd < 0 || mask & (SETATTR_MTIME|SETATTR_ATIME)) {
748 path = _sysio_pb_path(pno->p_base, '/');
754 * Get current status for undo.
758 ? native_stat(path, &st)
759 : native_fstat(fd, &st);
763 if (mask & SETATTR_MODE) {
767 * Alter permissions attribute.
769 mode = stbuf->st_mode & 07777;
772 ? syscall(SYS_chmod, path, mode)
773 : syscall(SYS_fchmod, fd, mode);
778 mask &= ~SETATTR_MODE;
779 else if (mask & (SETATTR_MTIME|SETATTR_ATIME)) {
783 * Alter access and/or modify time attributes.
785 ut.actime = st.st_atime;
786 ut.modtime = st.st_mtime;
787 if (mask & SETATTR_MTIME)
788 ut.modtime = stbuf->st_mtime;
789 if (mask & SETATTR_ATIME)
790 ut.actime = stbuf->st_atime;
791 err = syscall(__SYS_UTIME, path, &ut);
796 mask &= ~(SETATTR_MTIME|SETATTR_ATIME);
797 else if (mask & (SETATTR_UID|SETATTR_GID)) {
800 * Alter owner and/or group identifiers.
812 : syscall(SYS_fchown,
824 mask &= ~(SETATTR_UID|SETATTR_GID);
825 else if (mask & SETATTR_LEN) {
827 * Do the truncate last. It can't be undone.
830 ? syscall(__SYS_TRUNCATE, path, stbuf->st_size)
831 : syscall(__SYS_FTRUNCATE, fd, stbuf->st_size));
836 * Undo after error. Some or all of this might not work... We
839 if (mask & (SETATTR_UID|SETATTR_GID)) {
849 : syscall(SYS_fchown,
858 if (mask & (SETATTR_MTIME|SETATTR_ATIME)) {
861 ut.actime = st.st_atime;
862 ut.modtime = st.st_mtime;
863 (void )syscall(__SYS_UTIME, path, &ut);
865 if (mask & SETATTR_MODE) {
867 ? syscall(SYS_chmod, path, st.st_mode & 07777)
868 : syscall(SYS_fchmod, fd, st.st_mode & 07777);
877 native_pos(int fd, _SYSIO_OFF_T *offset, int whence)
882 assert(*offset >= 0);
885 #if _LARGEFILE64_SOURCE && defined(SYS__llseek)
891 (unsigned int)(off >> 32),
913 native_filldirentries(struct native_inode *nino,
926 * Stream-oriented access requires that we reposition prior to the
929 if ((err = native_pos(nino->ni_fd, basep, SEEK_SET)) != 0)
932 nino->ni_fpos = *basep;
935 #if defined(SYS_getdirentries)
936 syscall(SYS_getdirentries,
941 #elif defined(SYS_getdents64)
942 syscall(SYS_getdents64, nino->ni_fd, buf, nbytes);
943 #elif defined(SYS_getdents)
944 syscall(SYS_getdents, nino->ni_fd, buf, nbytes);
951 * Stream-oriented access requires that we discover where we are
955 if ((err = native_pos(nino->ni_fd, basep, SEEK_CUR)) != 0)
958 nino->ni_fpos = *basep;
963 native_getdirentries(struct inode *ino,
968 struct native_inode *nino = I2NI(ino);
972 struct linux_dirent *ldp;
973 struct dirent64 *d64p;
982 assert(nino->ni_fd >= 0);
986 while (!(bp = malloc(count))) {
988 if (count < sizeof(struct dirent))
992 cc = native_filldirentries(nino, bp, count, basep);
1000 ldp = (struct linux_dirent *)bp;
1001 d64p = (struct dirent64 *)buf;
1003 if (cc < 0 || (size_t )cc <= sizeof(*ldp))
1005 namlen = strlen(ldp->ld_name);
1006 reclen = sizeof(*d64p) - sizeof(d64p->d_name) + namlen + 1;
1007 if (nbytes < reclen)
1009 d64p->d_ino = ldp->ld_ino;
1010 d64p->d_off = ldp->ld_off;
1012 (((reclen + sizeof(long) - 1)) / sizeof(long)) *
1014 if (nbytes < d64p->d_reclen)
1015 d64p->d_reclen = reclen;
1016 d64p->d_type = DT_UNKNOWN; /* you lose -- sorry. */
1017 (void )strncpy(d64p->d_name, ldp->ld_name, namlen);
1018 *(d64p->d_name + namlen) = '\0';
1019 cc -= ldp->ld_reclen;
1020 ldp = (struct linux_dirent *)((char *)ldp + ldp->ld_reclen);
1021 nbytes -= d64p->d_reclen;
1022 d64p = (struct dirent64 *)((char *)d64p + d64p->d_reclen);
1025 if (d64p == (struct dirent64 *)buf && cc)
1026 cc = -EINVAL; /* buf too small */
1027 cc = (char *)d64p - buf;
1036 native_inop_mkdir(struct pnode *pno, mode_t mode)
1041 path = _sysio_pb_path(pno->p_base, '/');
1045 err = syscall(SYS_mkdir, path, mode);
1053 native_inop_rmdir(struct pnode *pno)
1058 path = _sysio_pb_path(pno->p_base, '/');
1062 err = syscall(SYS_rmdir, path);
1070 native_inop_symlink(struct pnode *pno, const char *data)
1075 path = _sysio_pb_path(pno->p_base, '/');
1079 err = syscall(SYS_symlink, data, path);
1087 native_inop_readlink(struct pnode *pno, char *buf, size_t bufsiz)
1092 path = _sysio_pb_path(pno->p_base, '/');
1095 i = syscall(SYS_readlink, path, buf, bufsiz);
1103 native_inop_open(struct pnode *pno, int flags, mode_t mode)
1105 struct native_inode *nino;
1109 path = _sysio_pb_path(pno->p_base, '/');
1114 * Whether the file is already open, or not, makes no difference.
1115 * Want to always give the host OS a chance to authorize in case
1116 * something has changed underneath us.
1118 if (flags & O_WRONLY) {
1120 * Promote write-only attempt to RW.
1126 flags |= O_LARGEFILE;
1128 fd = syscall(SYS_open, path, flags, mode);
1129 if (!pno->p_base->pb_ino && fd >= 0) {
1133 * Success but we need to return an i-node.
1136 native_iget(pno->p_mount->mnt_fs,
1138 &pno->p_base->pb_ino,
1141 (void )syscall(SYS_close, fd);
1152 * Remember this new open.
1154 nino = I2NI(pno->p_base->pb_ino);
1156 assert(nino->ni_nopens);
1158 if (nino->ni_fd >= 0) {
1159 if ((nino->ni_oflags & O_RDWR) ||
1160 (flags & (O_RDONLY|O_WRONLY|O_RDWR)) == O_RDONLY) {
1164 (void )syscall(SYS_close, fd);
1167 (void )syscall(SYS_close, nino->ni_fd);
1170 * Invariant; First open. Must init.
1175 * Need to know whether we can seek on this
1179 native_pos(nino->ni_fd, &nino->ni_fpos, SEEK_CUR) != 0 ? 0 : 1;
1185 native_inop_close(struct inode *ino)
1187 struct native_inode *nino = I2NI(ino);
1190 if (nino->ni_fd < 0)
1193 assert(nino->ni_nopens);
1194 if (--nino->ni_nopens) {
1196 * Hmmm. We really don't need anything else. However, some
1197 * filesystems try to implement a sync-on-close semantic.
1198 * As this appears now, that is lost. Might want to change
1199 * it somehow in the future?
1204 err = syscall(SYS_close, nino->ni_fd);
1214 native_inop_link(struct pnode *old, struct pnode *new)
1217 char *opath, *npath;
1221 opath = _sysio_pb_path(old->p_base, '/');
1222 npath = _sysio_pb_path(new->p_base, '/');
1223 if (!(opath && npath)) {
1228 err = syscall(SYS_link, opath, npath);
1242 native_inop_unlink(struct pnode *pno)
1247 path = _sysio_pb_path(pno->p_base, '/');
1252 * For this driver, unlink is easy with open files. Since the
1253 * file remains open to the system, too, the descriptors are still
1256 * Other drivers will have some difficulty here as the entry in the
1257 * file system name space must be removed without sacrificing access
1258 * to the file itself. In NFS this is done with a mechanism referred
1259 * to as a `silly delete'. The file is moved to a temporary name
1260 * (usually .NFSXXXXXX, where the X's are replaced by the PID and some
1261 * unique characters) in order to simulate the proper semantic.
1263 if (syscall(SYS_unlink, path) != 0)
1270 native_inop_rename(struct pnode *old, struct pnode *new)
1273 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(SYS_rename, opath, npath);
1296 dopio(void *buf, size_t count, _SYSIO_OFF_T off, struct native_io *nio)
1298 #if defined(_LARGEFILE64_SOURCE) && \
1299 defined(SYS_pread64) && \
1300 defined(SYS_pwrite64)
1301 #define _NATIVE_SYSCALL_PREAD SYS_pread64
1302 #define _NATIVE_SYSCALL_PWRITE SYS_pwrite64
1304 #define _NATIVE_SYSCALL_PREAD SYS_pread
1305 #define _NATIVE_SYSCALL_PWRITE SYS_pwrite
1309 if (!(off == nio->nio_nino->ni_fpos || nio->nio_nino->ni_seekok))
1312 if (!nio->nio_nino->ni_seekok) {
1313 if (off != nio->nio_nino->ni_fpos) {
1315 * They've done a p{read,write} or somesuch. Can't
1316 * seek on this descriptor so we err out now.
1322 syscall(nio->nio_op == 'r' ? SYS_read : SYS_write,
1323 nio->nio_nino->ni_fd,
1327 nio->nio_nino->ni_fpos += cc;
1330 syscall((nio->nio_op == 'r'
1331 ? _NATIVE_SYSCALL_PREAD
1332 : _NATIVE_SYSCALL_PWRITE),
1333 nio->nio_nino->ni_fd,
1339 #undef _NATIVE_SYSCALL_PREAD
1340 #undef _NATIVE_SYSCALL_PWRITE
1344 doiov(const struct iovec *iov,
1348 struct native_io *nio)
1352 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1353 #define MAX_IOVEC INT_MAX
1360 * Avoid the reposition call if we're already at the right place.
1361 * Allows us to access pipes and fifos.
1363 if (off != nio->nio_nino->ni_fpos) {
1366 err = native_pos(nio->nio_nino->ni_fd, &off, SEEK_SET);
1369 nio->nio_nino->ni_fpos = off;
1373 * The {read,write}v is safe as this routine is only ever called
1374 * by _sysio_enumerate_extents() and that routine is exact. It never
1375 * passes iovectors including tails.
1380 ? syscall(nio->nio_op == 'r' ? SYS_readv : SYS_writev,
1381 nio->nio_nino->ni_fd,
1386 _sysio_enumerate_iovec(iov,
1390 (ssize_t (*)(void *,
1398 nio->nio_nino->ni_fpos += cc;
1401 #if !(defined(REDSTORM) || defined(MAX_IOVEC))
1408 lockop_all(struct native_inode *nino,
1409 struct intnl_xtvec *xtv,
1419 flock.l_whence = SEEK_SET;
1421 flock.l_start = xtv->xtv_off;
1422 flock.l_len = xtv->xtv_len;
1426 #if !_LARGEFILE64_SOURCE
1442 order_xtv(const struct intnl_xtvec *xtv1, const struct intnl_xtvec *xtv2)
1445 if (xtv1->xtv_off < xtv2->xtv_off)
1447 if (xtv1->xtv_off > xtv2->xtv_off)
1454 doio(char op, struct ioctx *ioctx)
1456 struct native_inode *nino;
1459 struct intnl_xtvec *oxtv;
1462 struct native_io arguments;
1465 struct intnl_xtvec *front, *rear, tmp;
1468 nino = I2NI(ioctx->ioctx_ino);
1470 dolocks = ioctx->ioctx_xtvlen > 1 && nino->ni_seekok;
1473 * Must lock the regions (in order!) since we can't do
1474 * strided-IO as a single atomic operation.
1476 oxtv = malloc(ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1481 ioctx->ioctx_xtvlen * sizeof(struct intnl_xtvec));
1483 ioctx->ioctx_xtvlen,
1484 sizeof(struct intnl_xtvec),
1485 (int (*)(const void *, const void *))order_xtv);
1488 oxtv, ioctx->ioctx_xtvlen,
1489 op == 'r' ? F_RDLCK : F_WRLCK);
1496 arguments.nio_op = op;
1497 arguments.nio_nino = nino;
1499 _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1500 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1501 (ssize_t (*)(const struct iovec *,
1510 * Must unlock in reverse order.
1513 rear = front + ioctx->ioctx_xtvlen - 1;
1514 while (front < rear) {
1519 if (lockop_all(nino, oxtv, ioctx->ioctx_xtvlen, F_UNLCK) != 0)
1524 if ((ioctx->ioctx_cc = cc) < 0) {
1525 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1526 ioctx->ioctx_cc = -1;
1532 native_inop_read(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1535 return doio('r', ioctx);
1539 native_inop_write(struct inode *ino __IS_UNUSED, struct ioctx *ioctx)
1542 return doio('w', ioctx);
1546 native_inop_pos(struct inode *ino, _SYSIO_OFF_T off)
1548 struct native_inode *nino = I2NI(ino);
1551 err = native_pos(nino->ni_fd, &off, SEEK_SET);
1552 return err < 0 ? err : off;
1556 native_inop_iodone(struct ioctx *ioctxp __IS_UNUSED)
1560 * It's always done in this driver. It completed when posted.
1566 native_inop_fcntl(struct inode *ino,
1570 struct native_inode *nino = I2NI(ino);
1574 if (nino->ni_fd < 0)
1581 err = syscall(SYS_fcntl, nino->ni_fd, cmd);
1591 arg = va_arg(ap, long);
1592 err = syscall(SYS_fcntl, nino->ni_fd, cmd, arg);
1602 native_inop_mknod(struct pnode *pno __IS_UNUSED,
1603 mode_t mode __IS_UNUSED,
1604 dev_t dev __IS_UNUSED)
1610 #ifdef _HAVE_STATVFS
1612 native_inop_statvfs(struct pnode *pno,
1614 struct intnl_statvfs *buf)
1621 if (!ino || I2NI(ino)->ni_fd < 0) {
1622 path = _sysio_pb_path(pno->p_base, '/');
1628 * The syscall interface does not support SYS_fstatvfs.
1629 * Should possibly return ENOSYS, but thought it
1630 * better to use SYS_fstatfs and fill in as much of
1631 * the statvfs structure as possible. This allows
1632 * for more of a test of the sysio user interface.
1636 ? syscall(SYS_statfs, path, &fs)
1637 : syscall(SYS_fstatfs, I2NI(ino)->ni_fd, &fs);
1643 buf->f_bsize = fs.f_bsize; /* file system block size */
1644 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1645 buf->f_blocks = fs.f_blocks;
1646 buf->f_bfree = fs.f_bfree;
1647 buf->f_bavail = fs.f_bavail;
1648 buf->f_files = fs.f_files; /* Total number serial numbers */
1649 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1650 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1651 buf->f_fsid = fs.f_fsid.__val[1];
1652 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1653 buf->f_namemax = fs.f_namelen;
1659 native_inop_sync(struct inode *ino)
1663 assert(I2NI(ino)->ni_fd >= 0);
1665 err = syscall(SYS_fsync, I2NI(ino)->ni_fd);
1672 native_inop_datasync(struct inode *ino)
1676 assert(I2NI(ino)->ni_fd >= 0);
1678 #ifdef NATIVE_FDATASYNC
1679 err = syscall(NATIVE_FDATASYNC, I2NI(ino)->ni_fd);
1682 #warning No fdatasync system call -- Using fsync instead!
1684 err = syscall(SYS_fsync, I2NI(ino)->ni_fd);
1692 native_inop_ioctl(struct inode *ino __IS_UNUSED,
1693 unsigned long int request __IS_UNUSED,
1694 va_list ap __IS_UNUSED)
1698 * I'm lazy. Maybe implemented later.
1705 native_inop_gone(struct inode *ino)
1707 struct native_inode *nino = I2NI(ino);
1709 if (nino->ni_fd >= 0)
1710 (void )syscall(SYS_close, nino->ni_fd);
1711 free(ino->i_private);
1715 native_fsop_gone(struct filesys *fs __IS_UNUSED)
1719 * Do nothing. There is no private part maintained for the
1720 * native file interface.