Whamcloud - gitweb
26b1911c3bca8e73b73e3cf61fca58e4cc0ef989
[fs/lustre-release.git] / libsysio / drivers / incore / fs_incore.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
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)
11  *
12  *    Cplant(TM) Copyright 1998-2003 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
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.
24  * 
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.
29  * 
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
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #ifdef __linux__
45 #define _BSD_SOURCE
46 #endif
47
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <time.h>
52 #include <limits.h>
53 #include <errno.h>
54 #include <assert.h>
55 #include <sys/uio.h>
56 #include <sys/types.h>
57 #include <dirent.h>
58 #include <sys/stat.h>
59 #ifdef _HAVE_STATVFS
60 #include <sys/statvfs.h>
61 #endif
62 #include <sys/queue.h>
63
64 #include "sysio.h"
65 #include "xtio.h"
66 #include "fs.h"
67 #include "mount.h"
68 #include "inode.h"
69 #include "dev.h"
70
71 #include "fs_incore.h"
72
73
74 /*
75  * In-core file system pseudo-driver.
76  */
77
78 /*
79  * Pseudo-blocksize.
80  */
81 #define INCORE_BLKSIZE          (8192)
82
83 /*
84  * Format of an incore inode.
85  */
86 struct incore_inode {
87         LIST_ENTRY(incore_inode) ici_link;              /* i-nodes list link */
88         struct intnl_stat ici_st;                       /* attrs */
89         struct file_identifier ici_fileid;              /* file ID */
90         void    *ici_data;                              /* file data */
91 };
92
93 /*
94  * Given pointer to inode, return pointer to incore-inode.
95  */
96 #define I2IC(ino)       ((struct incore_inode *)(ino)->i_private)
97
98 struct incore_filesys {
99         LIST_HEAD(, incore_inode) icfs_icinodes;        /* all i-nodes list */
100 };
101
102 /*
103  * Given pointer to filesys, return pointer to incore-filesys.
104  */
105 #define FS2ICFS(fs)     ((struct incore_filesys *)(fs)->fs_private)
106
107 static int _sysio_incore_fsswop_mount(const char *source,
108                                       unsigned flags,
109                                       const void *data,
110                                       struct pnode *tocover,
111                                       struct mount **mntp);
112
113 static struct fssw_ops incore_fssw_ops = {
114                 _sysio_incore_fsswop_mount
115 };
116
117 static void _sysio_incore_fsop_gone(struct filesys *fs);
118
119 static struct filesys_ops incore_fs_ops = {
120                 _sysio_incore_fsop_gone,
121 };
122
123 static int _sysio_incore_dirop_lookup(struct pnode *pno,
124                                       struct inode **inop,
125                                       struct intent *intnt,
126                                       const char *path);
127 static int _sysio_incore_inop_getattr(struct pnode *pno,
128                                       struct inode *ino,
129                                       struct intnl_stat *stbuf);
130 static int _sysio_incore_inop_setattr(struct pnode *pno,
131                                       struct inode *ino,
132                                       unsigned mask,
133                                       struct intnl_stat *stbuf);
134 static ssize_t _sysio_incore_dirop_filldirentries(struct inode *ino,
135                                                   _SYSIO_OFF_T *posp,
136                                                   char *buf,
137                                                   size_t nbytes);
138 static int _sysio_incore_dirop_mkdir(struct pnode *pno, mode_t mode);
139 static int _sysio_incore_dirop_rmdir(struct pnode *pno);
140 static int _sysio_incore_inop_open(struct pnode *pno, int flags, mode_t mode);
141 static int _sysio_incore_inop_close(struct inode *ino);
142 static int _sysio_incore_dirop_link(struct pnode *old, struct pnode *new);
143 static int _sysio_incore_dirop_unlink(struct pnode *pno);
144 static int _sysio_incore_dirop_rename(struct pnode *old, struct pnode *new);
145 static int _sysio_incore_filop_read(struct inode *ino, struct ioctx *ioctx);
146 static int _sysio_incore_filop_write(struct inode *ino, struct ioctx *ioctx);
147 static _SYSIO_OFF_T _sysio_incore_filop_pos(struct inode *ino,
148                                             _SYSIO_OFF_T off);
149 static int _sysio_incore_filop_iodone(struct ioctx *ioctx);
150 static int _sysio_incore_filop_fcntl(struct inode *ino, 
151                                      int cmd, va_list ap, int *rtn);
152 static int _sysio_incore_inop_sync(struct inode *ino);
153 static int _sysio_incore_filop_ioctl(struct inode *ino,
154                                     unsigned long int request,
155                                     va_list ap);
156 static int _sysio_incore_dirop_mknod(struct pnode *pno, mode_t mode, dev_t dev);
157 #ifdef _HAVE_STATVFS
158 static int _sysio_incore_inop_statvfs(struct pnode *pno,
159                                       struct inode *ino,
160                                       struct intnl_statvfs *buf);
161 #endif
162 static void _sysio_incore_inop_gone(struct inode *ino);
163
164 #define _sysio_incore_dirop_symlink \
165         (int (*)(struct pnode *, const char *))_sysio_do_enosys
166 #define _sysio_incore_dirop_readlink \
167         (int (*)(struct pnode *, char *, size_t))_sysio_do_enosys
168 #define _sysio_incore_dirop_read \
169         (int (*)(struct inode *, \
170                  struct ioctx *))_sysio_do_eisdir
171 #define _sysio_incore_dirop_write \
172         (int (*)(struct inode *, \
173                  struct ioctx *))_sysio_do_eisdir
174 #define _sysio_incore_dirop_pos \
175         (_SYSIO_OFF_T (*)(struct inode *, \
176                           _SYSIO_OFF_T))_sysio_do_eisdir
177 #define _sysio_incore_dirop_iodone \
178         (int (*)(struct ioctx *))_sysio_do_illop
179 #define _sysio_incore_dirop_fcntl \
180         (int (*)(struct inode *, int, va_list, int *))_sysio_do_eisdir
181 #define _sysio_incore_dirop_ioctl \
182         (int (*)(struct inode *, \
183                  unsigned long int, \
184                  va_list))_sysio_do_eisdir
185
186 static struct inode_ops _sysio_incore_dir_ops = {
187         _sysio_incore_dirop_lookup,
188         _sysio_incore_inop_getattr,
189         _sysio_incore_inop_setattr,
190         _sysio_incore_dirop_filldirentries,
191         _sysio_incore_dirop_mkdir,
192         _sysio_incore_dirop_rmdir,
193         _sysio_incore_dirop_symlink,
194         _sysio_incore_dirop_readlink,
195         _sysio_incore_inop_open,
196         _sysio_incore_inop_close,
197         _sysio_incore_dirop_link,
198         _sysio_incore_dirop_unlink,
199         _sysio_incore_dirop_rename,
200         _sysio_incore_dirop_read,
201         _sysio_incore_dirop_write,
202         _sysio_incore_dirop_pos,
203         _sysio_incore_dirop_iodone,
204         _sysio_incore_dirop_fcntl,
205         _sysio_incore_inop_sync,
206         _sysio_incore_inop_sync,
207         _sysio_incore_dirop_ioctl,
208         _sysio_incore_dirop_mknod,
209 #ifdef _HAVE_STATVFS
210         _sysio_incore_inop_statvfs,
211 #endif
212         _sysio_incore_inop_gone
213 };
214
215 #define _sysio_incore_filop_lookup \
216         (int (*)(struct pnode *, \
217                  struct inode **, \
218                  struct intent *, \
219                  const char *))_sysio_do_illop
220 #define _sysio_incore_filop_filldirentries \
221         (ssize_t (*)(struct inode *, \
222                      _SYSIO_OFF_T *, \
223                      char *, \
224                      size_t))_sysio_do_illop
225 #define _sysio_incore_filop_mkdir \
226         (int (*)(struct pnode *, mode_t))_sysio_do_illop
227 #define _sysio_incore_filop_rmdir \
228         (int (*)(struct pnode *))_sysio_do_illop
229 #define _sysio_incore_filop_symlink \
230         (int (*)(struct pnode *, const char *))_sysio_do_illop
231 #define _sysio_incore_symlinkop_readlink \
232         (int (*)(struct pnode *, char *, size_t))_sysio_do_illop
233 #define _sysio_incore_filop_link \
234         (int (*)(struct pnode *old, struct pnode *new))_sysio_do_illop
235 #define _sysio_incore_filop_unlink \
236         (int (*)(struct pnode *pno))_sysio_do_illop
237 #define _sysio_incore_filop_rename \
238         (int (*)(struct pnode *old, struct pnode *new))_sysio_do_illop
239 #define _sysio_incore_filop_mknod \
240         (int (*)(struct pnode *pno, mode_t, dev_t))_sysio_do_illop
241
242 static struct inode_ops _sysio_incore_file_ops = {
243         _sysio_incore_filop_lookup,
244         _sysio_incore_inop_getattr,
245         _sysio_incore_inop_setattr,
246         _sysio_incore_filop_filldirentries,
247         _sysio_incore_filop_mkdir,
248         _sysio_incore_filop_rmdir,
249         _sysio_incore_filop_symlink,
250         _sysio_incore_symlinkop_readlink,
251         _sysio_incore_inop_open,
252         _sysio_incore_inop_close,
253         _sysio_incore_filop_link,
254         _sysio_incore_filop_unlink,
255         _sysio_incore_filop_rename,
256         _sysio_incore_filop_read,
257         _sysio_incore_filop_write,
258         _sysio_incore_filop_pos,
259         _sysio_incore_filop_iodone,
260         _sysio_incore_filop_fcntl,
261         _sysio_incore_inop_sync,
262         _sysio_incore_inop_sync,
263         _sysio_incore_filop_ioctl,
264         _sysio_incore_filop_mknod,
265 #ifdef _HAVE_STATVFS
266         _sysio_incore_inop_statvfs,
267 #endif
268         _sysio_incore_inop_gone
269 };
270
271 static struct inode_ops _sysio_incore_dev_ops = {
272         _sysio_incore_filop_lookup,
273         _sysio_incore_inop_getattr,
274         _sysio_incore_inop_setattr,
275         _sysio_incore_filop_filldirentries,
276         _sysio_incore_filop_mkdir,
277         _sysio_incore_filop_rmdir,
278         _sysio_incore_filop_symlink,
279         _sysio_incore_symlinkop_readlink,
280         _sysio_nodev_inop_open,
281         _sysio_nodev_inop_close,
282         _sysio_incore_filop_link,
283         _sysio_incore_filop_unlink,
284         _sysio_incore_filop_rename,
285         _sysio_nodev_inop_read,
286         _sysio_nodev_inop_write,
287         _sysio_nodev_inop_pos,
288         _sysio_nodev_inop_iodone,
289         _sysio_incore_filop_fcntl,
290         _sysio_incore_inop_sync,
291         _sysio_nodev_inop_sync,
292         _sysio_nodev_inop_ioctl,
293         _sysio_incore_filop_mknod,
294 #ifdef _HAVE_STATVFS
295         _sysio_incore_inop_statvfs,
296 #endif
297         _sysio_incore_inop_gone
298 };
299
300 typedef void *(*probe_ty)(void *data, size_t len, void *arg);
301
302 /*
303  * Lookup data argument bundle record.
304  */
305 struct lookup_data {
306         struct qstr *name;                              /* desired entry name */
307         struct intnl_dirent *de;                        /* last dirent */
308         size_t  minsiz;                                 /* min hole needed */
309         struct {
310                 void    *p;                             /* best hole */
311                 size_t  len;                            /* best hole len */
312         } hole;
313 };
314
315 /*
316  * Initialize lookup data argument bundle.
317  */
318 #define INCORE_LD_INIT(ld, minsz, qs) \
319         do { \
320                 (ld)->name = (qs); \
321                 (ld)->de = NULL; \
322                 (ld)->minsiz = (minsz); \
323                 (ld)->hole.p = NULL; \
324                 (ld)->hole.len = 0; \
325         } while (0)
326
327 /*
328  * Calculate size of a directory entry given length of the entry name.
329  */
330 #define INCORE_D_RECLEN(namlen) \
331         (((size_t )&((struct intnl_dirent *)0)->d_name + \
332           (namlen) + 1 + sizeof(void *)) & \
333          ~(sizeof(void *) - 1))
334
335 /*
336  * Given mode bits, return directory entry type code.
337  */
338 #define INCORE_D_TYPEOF(m)      (((m) & S_IFMT) >> 12)
339
340 static char incore_dir_template[INCORE_D_RECLEN(1) + INCORE_D_RECLEN(2)];
341 #if 0
342 static struct intnl_dirent incore_dir_template[] = {
343         {
344                 0,
345                 INCORE_D_RECLEN(1),
346                 INCORE_D_RECLEN(1),
347                 INCORE_D_TYPEOF(S_IFDIR),
348                 { '.', '\0' }
349         },
350         {
351                 0,
352                 INCORE_D_RECLEN(1) + INCORE_D_RECLEN(2),
353                 INCORE_D_RECLEN(2),
354                 INCORE_D_TYPEOF(S_IFDIR),
355                 { '.', '.', '\0' }
356         }
357 };
358 #endif
359
360 /*
361  * Initialize this driver.
362  */
363 int
364 _sysio_incore_init()
365 {
366         struct intnl_dirent *de;
367         off_t   off;
368
369         /*
370          * Fill in the directory template.
371          */
372         de = (struct intnl_dirent *)incore_dir_template;
373 #ifdef _DIRENT_HAVE_D_OFF
374         de->d_off =
375 #endif
376             off = de->d_reclen = INCORE_D_RECLEN(1);
377         de->d_type = INCORE_D_TYPEOF(S_IFDIR);
378         de->d_name[0] = '.';
379 #ifdef _DIRENT_HAVE_D_NAMLEN
380         de->d_namlen = 1;
381 #endif
382         /*
383          * Move to entry for `..'
384          */
385         de = (struct intnl_dirent *)((char *)de + off);
386         de->d_reclen = INCORE_D_RECLEN(2);
387 #ifdef _DIRENT_HAVE_D_NAMLEN
388         de->d_namlen = 2;
389 #endif
390 #ifdef _DIRENT_HAVE_D_OFF
391         de->d_off =
392 #endif
393             off += de->d_reclen;
394         de->d_type = INCORE_D_TYPEOF(S_IFDIR);
395         de->d_name[0] = de->d_name[1] = '.';
396         de->d_name[2] = ' ';
397
398         return _sysio_fssw_register("incore", &incore_fssw_ops);
399 }
400
401 static ino_t
402 incore_inum_alloc()
403 {
404         static ino_t nxtnum = 1;
405
406         assert(nxtnum);
407         return nxtnum++;
408 }
409
410 static struct incore_inode *
411 incore_i_alloc(struct incore_filesys *icfs, struct intnl_stat *st)
412 {
413         struct incore_inode *icino;
414
415         assert(st->st_ino);
416         assert(!st->st_size);
417
418         icino = malloc(sizeof(struct incore_inode));
419         if (!icino)
420                 return NULL;
421         icino->ici_st = *st;
422         icino->ici_fileid.fid_data = &icino->ici_st.st_ino;
423         icino->ici_fileid.fid_len = sizeof(icino->ici_st.st_ino);
424         icino->ici_data = NULL;
425
426         LIST_INSERT_HEAD(&icfs->icfs_icinodes, icino, ici_link);
427
428         return icino;
429 }
430
431 static int
432 incore_trunc(struct incore_inode *icino, _SYSIO_OFF_T size, int clear)
433 {
434         _SYSIO_OFF_T n;
435         void    *p;
436
437         if (size < 0) 
438                 return -EINVAL;
439         n = size;
440         if (!size) {
441                 if (icino->ici_data) {
442                         free(icino->ici_data);
443                         icino->ici_data = NULL;
444                 }
445                 n = 0;
446                 goto out;
447         }
448         p = realloc(icino->ici_data, (size_t )n);
449         if (!p)
450                 return -ENOSPC;
451         icino->ici_data = p;
452         if (clear && n > icino->ici_st.st_size)
453                 (void )memset((char *)icino->ici_data + icino->ici_st.st_size,
454                               0,
455                               (size_t )(n - icino->ici_st.st_size));
456 out:
457         icino->ici_st.st_size = n;
458         icino->ici_st.st_blocks =
459             (n + icino->ici_st.st_blksize - 1) / icino->ici_st.st_blksize;
460         icino->ici_st.st_mtime = time(NULL);
461         return 0;
462 }
463
464 static void
465 incore_i_destroy(struct incore_inode *icino)
466 {
467
468         LIST_REMOVE(icino, ici_link);
469         (void )incore_trunc(icino, 0, 0);
470         free(icino);
471 }
472
473 static struct incore_inode *
474 incore_directory_new(struct incore_filesys *icfs,
475                      struct incore_inode *parent,
476                      struct intnl_stat *st)
477 {
478         struct incore_inode *icino;
479         int     err;
480         struct intnl_dirent *de;
481
482         icino = incore_i_alloc(icfs, st);
483         if (!icino)
484                 return NULL;
485
486         if (!parent)
487                 parent = icino;                         /* root */
488
489         /*
490          * Allocate and init directory data.
491          */
492         err = incore_trunc(icino, sizeof(incore_dir_template), 1);
493         if (err) {
494                 incore_i_destroy(icino);
495                 return NULL;
496         }
497         (void )memcpy(icino->ici_data,
498                       &incore_dir_template,
499                       sizeof(incore_dir_template));
500         de = icino->ici_data;
501         de->d_ino = st->st_ino;
502         de =
503             (struct intnl_dirent *)((char *)de +
504 #ifdef _DIRENT_HAVE_D_OFF
505                                     de->d_off
506 #else
507                                     de->d_reclen
508 #endif
509                                     );
510         de->d_ino = parent->ici_st.st_ino;
511
512         /*
513          * Set creation time to modify time set by truncate.
514          */
515         st->st_ctime = st->st_mtime;
516
517         return icino;
518 }
519
520 static int
521 _sysio_incore_fsswop_mount(const char *source,
522                            unsigned flags,
523                            const void *data __IS_UNUSED,
524                            struct pnode *tocover,
525                            struct mount **mntp)
526 {
527         char    *cp;
528         unsigned long ul;
529         long    l;
530         mode_t  mode;
531         uid_t   uid;
532         gid_t   gid;
533         int     err;
534         dev_t   dev;
535         struct intnl_stat stat;
536         struct incore_filesys *icfs;
537         ino_t   inum;
538         struct incore_inode *icino;
539         struct filesys *fs;
540         struct inode *rooti;
541         struct pnode_base *rootpb;
542         struct mount *mnt;
543         static struct qstr noname = { NULL, 0, 0 };
544
545         /*
546          * Source is a specification for the root attributes of this
547          * new file system in the format:
548          *
549          * <permissions>+<owner>+<group>
550          */
551         ul = strtoul(source, &cp, 0);
552         mode = (mode_t )ul & 07777;
553         if (*cp != '+' ||
554             (ul == ULONG_MAX && errno == ERANGE) ||
555             (unsigned long)mode != ul ||
556             mode > 07777)
557                 return -EINVAL;
558         source = cp;
559         l = strtol(source, &cp, 0);
560         uid = (uid_t )l;
561         if (*cp != '+' ||
562             ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
563             (long )uid != l)
564                 return -EINVAL;
565         source = cp;
566         l = strtol(source, &cp, 0);
567         gid = (gid_t )l;
568         if (*cp ||
569             ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
570             (long )gid != l)
571                 return -EINVAL;
572
573         err = 0;
574
575         dev = _sysio_dev_alloc();
576
577         mnt = NULL;
578         rootpb = NULL;
579         rooti = NULL;
580         fs = NULL;
581         icino = NULL;
582         icfs = NULL;
583
584         /*
585          * Create new FS.
586          */
587         icfs = malloc(sizeof(struct incore_filesys));
588         if (!icfs) {
589                 err = -ENOMEM;
590                 goto error;
591         }
592         (void )memset(icfs, 0, sizeof(struct incore_filesys));
593         LIST_INIT(&icfs->icfs_icinodes);
594
595         /*
596          * Create root i-node.
597          */
598         (void )memset(&stat, 0, sizeof(stat));
599         stat.st_dev = dev;
600         inum = incore_inum_alloc();
601 #ifdef HAVE__ST_INO
602         stat.__st_ino = inum; 
603 #endif
604         stat.st_mode = S_IFDIR | (mode & 07777);
605         stat.st_nlink = 2;
606         stat.st_uid = uid;
607         stat.st_gid = gid;
608         stat.st_size = 0;
609         stat.st_blksize = INCORE_BLKSIZE;
610         stat.st_blocks = 0;
611         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
612         stat.st_ino = inum;
613         icino = incore_directory_new(icfs, NULL, &stat);
614         if (!icino)
615                 return -ENOSPC;
616         icino->ici_st.st_atime = icino->ici_st.st_mtime;
617
618         fs =
619             _sysio_fs_new(&incore_fs_ops,
620                           (flags & MOUNT_F_RO) ? FS_F_RO : 0,
621                           icfs);
622         if (!fs) {
623                 err = -ENOMEM;
624                 goto error;
625         }
626
627         /*
628          * Create root for system.
629          *
630          * Persistent across remounts because we ask for immunity.
631          */
632         rooti =
633             _sysio_i_new(fs,
634                          &icino->ici_fileid,
635                          &icino->ici_st,
636                          1,
637                          &_sysio_incore_dir_ops,
638                          icino);
639         if (!rooti) {
640                 err = -ENOMEM;
641                 goto error;
642         }
643         rootpb = _sysio_pb_new(&noname, NULL, rooti);
644         if (!rootpb) {
645                 err = -ENOMEM;
646                 goto error;
647         }
648
649         /*
650          * Have path-node specified by the given source argument. Let the
651          * system finish the job, now.
652          */
653         mnt = NULL;
654         err =
655             _sysio_do_mount(fs,
656                             rootpb,
657                             flags,
658                             tocover,
659                             &mnt);
660         if (err)
661                 goto error;
662
663         *mntp = mnt;
664
665         goto out;
666
667 error:
668         if (mnt && _sysio_do_unmount(mnt) != 0)
669                         abort();
670         if (rootpb) {
671                 _sysio_pb_gone(rootpb);
672                 rooti = NULL;
673         }
674         if (rooti)
675                 I_RELE(rooti);
676         if (fs) {
677                 FS_RELE(fs);
678                 goto out;
679         }
680         if (icino) {
681                 incore_i_destroy(icino);
682                 goto out;
683         }
684         if (icfs) {
685                 free(icfs);
686                 goto out;
687         }
688
689 out:
690         return err;
691 }
692
693 static void
694 _sysio_incore_fsop_gone(struct filesys *fs)
695 {
696         struct incore_filesys *icfs;
697         struct incore_inode *icino, *oicino;
698
699         icfs = FS2ICFS(fs);
700
701         /*
702          * Free up i-node resource associated with this file system.
703          */
704         icino = icfs->icfs_icinodes.lh_first;
705         while (icino) {
706                 oicino = icino;
707                 icino = icino->ici_link.le_next;
708                 incore_i_destroy(oicino);
709         }
710
711         /*
712          * Free the FS record.
713          */
714         free(icfs);
715 }
716
717 /*
718  * A directory search engine. Various functions are carried out by
719  * supplying appropriate callback functions.
720  *
721  * The two arguments, entry and hole, are called, if not null, for each
722  * directory entry and hole, respectively.
723  */
724 static void *
725 incore_directory_probe(void *data,
726                        size_t siz,
727                        _SYSIO_OFF_T origin
728 #ifndef _DIRENT_HAVE_D_OFF
729                                 __IS_UNUSED
730 #endif
731                        ,
732                        probe_ty entry,
733                        probe_ty hole,
734                        void *arg)
735 {
736         struct intnl_dirent *de;
737         void    *p;
738         size_t  n;
739
740         de = data;
741         for (;;) {
742 #ifdef _DIRENT_HAVE_D_OFF
743                 assert(de->d_off);
744 #else
745                 assert(de->d_reclen);
746 #endif
747                 if (entry && (p = (*entry)(de, de->d_reclen, arg)))
748                         return p;
749                 n =
750 #ifdef _DIRENT_HAVE_D_OFF
751                     de->d_off - origin;
752 #else
753                     ((void *)de - data) + de->d_reclen;
754 #endif
755                 if (hole) {
756                         p = (*hole)((void *)de, de->d_reclen, arg);
757                         if (p)
758                                 return p;
759                 }
760                 if (n >= siz)
761                         break;
762                 de = (struct intnl_dirent *)((char *)data + n);
763         }
764
765         return NULL;
766 }
767
768 static struct intnl_dirent *
769 incore_directory_match(struct intnl_dirent *de,
770                        size_t reclen __IS_UNUSED,
771                        struct lookup_data *ld)
772 {
773
774 #if defined(BSD) || defined(REDSTORM)
775         if (IFTODT(de->d_type) == DT_WHT)
776                 return NULL;
777 #endif
778         if (
779 #ifdef _DIRENT_HAVE_D_NAMLEN
780             ld->name->len == de->d_namlen &&
781 #endif
782             strncmp(de->d_name, ld->name->name, ld->name->len) == 0)
783                 return de;
784         ld->de = de;
785         return NULL;
786 }
787
788 static int
789 _sysio_incore_dirop_lookup(struct pnode *pno,
790                            struct inode **inop,
791                            struct intent *intnt __IS_UNUSED,
792                            const char *path __IS_UNUSED)
793 {
794         struct inode *ino;
795         struct intnl_dirent *de;
796         struct incore_inode *icino;
797         struct lookup_data lookup_data;
798         struct file_identifier fileid;
799 #ifdef notdef
800         struct inode_ops *ops;
801 #endif
802
803         /*
804          * Revalidate?
805          */
806         if (*inop) {
807                 icino = I2IC(*inop);
808                 assert(icino);
809                 (*inop)->i_stbuf = icino->ici_st;
810                 return 0;
811         }
812
813         ino = pno->p_parent->p_base->pb_ino;
814         icino = I2IC(ino);
815         INCORE_LD_INIT(&lookup_data,
816                        ULONG_MAX,
817                        &pno->p_base->pb_name);
818         de =
819             incore_directory_probe(icino->ici_data,
820                                    icino->ici_st.st_size,
821                                    0,
822                                    (probe_ty )incore_directory_match,
823                                    NULL,
824                                    &lookup_data);
825         if (!de)
826                 return -ENOENT;
827
828         fileid.fid_data = &de->d_ino;
829         fileid.fid_len = sizeof(de->d_ino);
830         ino =
831             _sysio_i_find(ino->i_fs, &fileid);
832 #ifdef notdef
833         if (ino)
834                 goto out;
835         icino->ici_fileid.fid_data = &icino->ici_st.st_ino;
836         icino->ici_fileid.fid_len = sizeof(icino->ici_st.st_ino);
837         ops = NULL;
838         switch (icino->ici_st.st_mode & S_IFMT) {
839         case S_IFDIR:
840                 ops = &_sysio_incore_dir_ops;
841                 break;
842         case S_IFREG:
843                 ops = &_sysio_incore_file_ops;
844                 break;
845         default:
846                 break;
847         }
848         if (!ops)
849                 abort();
850         ino =
851             _sysio_i_new(ino->i_fs,
852                          &icino->ici_fileid,
853                          &icino->ici_st
854                          1,
855                          ops,
856                          icino);
857 #endif
858         if (!ino)
859                 return -ENOMEM;
860
861 #ifdef notdef
862 out:
863 #endif
864         *inop = ino;
865         return 0;
866 }
867
868 static int
869 _sysio_incore_inop_getattr(struct pnode *pno,
870                            struct inode *ino,
871                            struct intnl_stat *stbuf)
872 {
873         struct incore_inode *icino;
874
875         if (!ino)
876                 ino = pno->p_base->pb_ino;
877         icino = I2IC(ino);
878         *stbuf = icino->ici_st;
879         return 0;
880 }
881
882 static int
883 _sysio_incore_inop_setattr(struct pnode *pno,
884                            struct inode *ino,
885                            unsigned mask,
886                            struct intnl_stat *stbuf)
887 {
888         struct incore_inode *icino;
889         int     err;
890
891         if (!ino)
892                 ino = pno->p_base->pb_ino;
893         if (!ino)
894                 return -EBADF;
895         icino = I2IC(ino);
896
897         err = 0;
898         if (mask & SETATTR_LEN) {
899                 err = incore_trunc(icino, stbuf->st_size, 1);
900                 if (err)
901                         goto out;
902                 mask &= ~SETATTR_LEN;
903         }
904         if (mask & SETATTR_MODE) {
905                 icino->ici_st.st_mode =
906                     (icino->ici_st.st_mode & S_IFMT) | (stbuf->st_mode & 07777);
907         }
908         if (mask & SETATTR_MTIME)
909                 icino->ici_st.st_mtime = stbuf->st_mtime;
910         if (mask & SETATTR_ATIME)
911                 icino->ici_st.st_atime = stbuf->st_atime;
912         if (mask & SETATTR_UID)
913                 icino->ici_st.st_uid = stbuf->st_uid;
914         if (mask & SETATTR_GID)
915                 icino->ici_st.st_gid = stbuf->st_gid;
916         icino->ici_st.st_ctime = time(NULL);
917
918         ino->i_stbuf = icino->ici_st;
919 out:
920         return err;
921 }
922
923 static void *
924 incore_directory_position(struct intnl_dirent *de,
925                           size_t reclen __IS_UNUSED,
926                           void *p)
927 {
928
929         return (void *)de >= p ? de : NULL;
930 }
931
932 struct copy_info {
933         void    *data;
934         size_t  nbytes;
935 };
936
937 /*
938  * Eumeration callback.
939  *
940  * Note:
941  * On those systems supporting white-out entries, they are returned. On
942  * systems without, they are not.
943  */
944 static void *
945 incore_directory_enumerate(struct intnl_dirent *de,
946                            size_t reclen,
947                            struct copy_info *cinfo) {
948
949         if (reclen > cinfo->nbytes)
950                 return de;
951         (void *)memcpy(cinfo->data, de, reclen);
952         cinfo->data = (char *)cinfo->data + reclen;
953         cinfo->nbytes -= reclen;
954         return NULL;
955 }
956
957 static ssize_t
958 _sysio_incore_dirop_filldirentries(struct inode *ino,
959                                    _SYSIO_OFF_T *posp,
960                                    char *buf,
961                                    size_t nbytes)
962 {
963         struct incore_inode *icino = I2IC(ino);
964         off_t   off;
965         struct intnl_dirent *de;
966         struct copy_info copy_info;
967
968         if (*posp >= icino->ici_st.st_size)
969                 return 0;
970
971         de =
972             incore_directory_probe(icino->ici_data,
973                                    icino->ici_st.st_size,
974                                    *posp,
975                                    (probe_ty )incore_directory_position,
976                                    NULL,
977                                    (char *)icino->ici_data + *posp);
978         if (!de) {
979                 /*
980                  * Past EOF.
981                  */
982                 return 0;
983         }
984
985         copy_info.data = buf;
986         copy_info.nbytes = nbytes;
987         off = (char *)de - (char *)icino->ici_data;
988         de =
989             incore_directory_probe(de,
990                                    icino->ici_st.st_size - off,
991                                    off,
992                                    (probe_ty )incore_directory_enumerate,
993                                    NULL,
994                                    &copy_info);
995         nbytes -= copy_info.nbytes;
996         icino->ici_st.st_atime = time(NULL);
997         if (!nbytes)
998                 return -EOVERFLOW;
999         *posp += nbytes;
1000         return (ssize_t )nbytes;
1001 }
1002
1003 static struct intnl_dirent *
1004 incore_directory_best_fit(void *data, size_t len, struct lookup_data *ld)
1005 {
1006
1007         if (!ld->hole.len || len < ld->hole.len) {
1008                 ld->hole.p = data;
1009                 ld->hole.len = len;
1010         }
1011
1012         return NULL;
1013 }
1014
1015 static int
1016 incore_directory_insert(struct incore_inode *parent,
1017                         struct qstr *name,
1018                         ino_t inum,
1019                         unsigned char type)
1020 {
1021         size_t  reclen;
1022         struct lookup_data lookup_data;
1023         struct intnl_dirent *de;
1024         size_t  xt;
1025         size_t  n;
1026         size_t  r;
1027
1028         reclen = INCORE_D_RECLEN(name->len);
1029         INCORE_LD_INIT(&lookup_data, reclen, name);
1030         de =
1031             incore_directory_probe(parent->ici_data,
1032                                    parent->ici_st.st_size,
1033                                    0,
1034                                    (probe_ty )incore_directory_match,
1035                                    (probe_ty )incore_directory_best_fit,
1036                                    &lookup_data);
1037         if (de)
1038                 return -EEXIST;
1039         de = lookup_data.de;
1040         xt = (char *)lookup_data.de - (char *)parent->ici_data;
1041         n =
1042 #ifdef _DIRENT_HAVE_D_OFF
1043             de->d_off;
1044 #else
1045             xt + de->d_reclen;
1046 #endif
1047         r =
1048 #ifdef _DIRENT_HAVE_D_OFF
1049             de->d_reclen;
1050 #else
1051             INCORE_D_RECLEN(de->d_namlen);
1052 #endif
1053         if (!parent->ici_st.st_size ||
1054             xt + r + reclen > (size_t )parent->ici_st.st_size) {
1055                 int     err;
1056
1057                 err = incore_trunc(parent, xt + r + reclen, 1);
1058                 if (err)
1059                         return err;
1060                 de = (struct intnl_dirent *)((char *)parent->ici_data + xt);
1061                 n = parent->ici_st.st_size;
1062         }
1063
1064 #ifdef _DIRENT_HAVE_D_OFF
1065         de->d_off = xt + r;                             /* trim */
1066 #else
1067         de->d_reclen = r;
1068 #endif
1069         de = (struct intnl_dirent *)((char *)de + r);                           /* reposition */
1070         xt += r;
1071
1072 #ifndef _DIRENT_HAVE_D_OFF
1073         /*
1074          * Will we split this hole or use all of it?
1075          */
1076         if (lookup_data.hole.len - reclen &&
1077             lookup_data.hole.len - reclen <= INCORE_D_RECLEN(1))
1078                 reclen = lookup_data.hole.len;
1079 #endif
1080
1081         /*
1082          * Insert new.
1083          */
1084         de->d_ino = inum;
1085 #ifdef _DIRENT_HAVE_D_OFF
1086         de->d_off = n;
1087 #endif
1088         de->d_reclen = reclen;
1089         de->d_type = type;
1090         (void )memcpy(de->d_name, name->name, name->len);
1091 #ifdef _DIRENT_HAVE_D_NAMLEN
1092         de->d_namlen = name->len;
1093 #endif
1094
1095 #ifndef _DIRENT_HAVE_D_OFF
1096         xt += reclen;
1097         if (n - xt) {
1098                 /*
1099                  * White-out remaining part of the hole.
1100                  */
1101                 (void *)de += reclen;
1102                 de->d_ino = 0;
1103                 de->d_reclen = n - xt;
1104                 de->d_type = DT_WHT;
1105                 de->d_namlen = 0;
1106         }
1107 #endif
1108
1109         /*
1110          * Update attributes to reflect the new entry.
1111          */
1112         parent->ici_st.st_nlink++;
1113         assert(parent->ici_st.st_nlink);
1114         parent->ici_st.st_atime = parent->ici_st.st_mtime = time(NULL);
1115
1116         return 0;
1117 }
1118
1119 static int
1120 _sysio_incore_dirop_mkdir(struct pnode *pno, mode_t mode)
1121 {
1122         struct intnl_stat stat;
1123         struct incore_inode *icino, *parent;
1124         ino_t   inum;
1125         int     err;
1126         struct intnl_dirent *de = NULL;
1127         struct inode *ino;
1128
1129         ino = pno->p_parent->p_base->pb_ino;
1130         parent = I2IC(ino);
1131
1132         if (!S_ISDIR(parent->ici_st.st_mode))
1133                 return -ENOTDIR;
1134
1135         (void )memset(&stat, 0, sizeof(stat));
1136         stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1137         inum = incore_inum_alloc();
1138 #ifdef HAVE__ST_INO
1139         stat.__st_ino = inum;
1140 #endif
1141         stat.st_mode = S_IFDIR | (mode & 07777);
1142         stat.st_nlink = 2;
1143         stat.st_uid = getuid();
1144         stat.st_gid = getgid();
1145         stat.st_size = 0;
1146         stat.st_blksize = 4096;
1147         stat.st_blocks = 0;
1148         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1149         stat.st_ino = inum;
1150         icino = incore_directory_new(FS2ICFS(ino->i_fs), parent, &stat);
1151         if (!icino)
1152                 return -ENOSPC;
1153
1154         /*
1155          * Tell the system about the new inode.
1156          *
1157          * Persistent across remounts because we ask for immunity.
1158          */
1159         ino =
1160             _sysio_i_new(pno->p_parent->p_base->pb_ino->i_fs,
1161                          &icino->ici_fileid,
1162                          &stat,
1163                          1,
1164                          &_sysio_incore_dir_ops,
1165                          icino);
1166         if (!ino) {
1167                 incore_i_destroy(icino);
1168                 return -ENOMEM;
1169         }
1170
1171         /*
1172          * Insert into parent.
1173          */
1174         err =
1175             incore_directory_insert(parent,
1176                                     &pno->p_base->pb_name,
1177                                     stat.st_ino,
1178                                     INCORE_D_TYPEOF(S_IFDIR));
1179
1180         if (err) {
1181                 de->d_ino = 0;                          /* bad parent */
1182                 I_RELE(ino);
1183                 _sysio_i_gone(ino);
1184                 return err;
1185         }
1186
1187         pno->p_base->pb_ino = ino;
1188         return 0;
1189 }
1190
1191 static int
1192 incore_unlink_entry(struct incore_inode *icino,
1193                     struct qstr *name)
1194 {
1195         struct lookup_data lookup_data;
1196         struct intnl_dirent *de;
1197         size_t  reclen;
1198 #ifdef _DIRENT_HAVE_D_OFF
1199         size_t  off;
1200 #endif
1201
1202         if (!S_ISDIR(icino->ici_st.st_mode))
1203                 return -ENOTDIR;
1204
1205         INCORE_LD_INIT(&lookup_data, 0, name);
1206         de =
1207             incore_directory_probe(icino->ici_data,
1208                                    icino->ici_st.st_size,
1209                                    0,
1210                                    (probe_ty )incore_directory_match,
1211                                    NULL,
1212                                    &lookup_data);
1213         if (!de)
1214                 return -ENOENT;
1215         assert((size_t )((char *)de - (char *)icino->ici_data) >=
1216                sizeof(incore_dir_template));
1217 #ifndef _DIRENT_HAVE_D_OFF
1218         reclen = de->d_reclen;
1219 #else
1220         off = de->d_off;
1221         reclen = off - ((char *)de - (char *)icino->ici_data);
1222 #endif
1223         (void )memset(de, 0, reclen);
1224 #ifndef _DIRENT_HAVE_D_OFF
1225         de->d_type = (__uint8_t )DTTOIF(DT_WHT);
1226         de->d_reclen = reclen;
1227 #else
1228         lookup_data.de->d_off = off;
1229 #endif
1230
1231         /*
1232          * Adjust link count.
1233          */
1234         assert(icino->ici_st.st_nlink > 2);
1235         icino->ici_st.st_nlink--;
1236
1237         return 0;
1238 }
1239
1240 static int
1241 _sysio_incore_dirop_rmdir(struct pnode *pno)
1242 {
1243         struct inode *ino = pno->p_base->pb_ino;
1244         struct incore_inode *icino = I2IC(ino);
1245         int     err;
1246
1247         if (!pno->p_base->pb_name.len ||
1248             (pno->p_base->pb_name.name[0] == '.' &&
1249              (pno->p_base->pb_name.len == 1 ||
1250               (pno->p_base->pb_name.len == 2 &&
1251                pno->p_base->pb_name.name[1] == '.'))))
1252                 return -EINVAL;
1253
1254         if (!S_ISDIR(icino->ici_st.st_mode))
1255                 return -ENOTDIR;
1256
1257         if (icino->ici_st.st_nlink > 2)
1258                 return -ENOTEMPTY;
1259
1260         pno->p_base->pb_ino = NULL;
1261         err =
1262             incore_unlink_entry(I2IC(pno->p_parent->p_base->pb_ino),
1263                                 &pno->p_base->pb_name);
1264         return err;
1265 }
1266
1267 static int
1268 incore_create(struct pnode *pno, struct intnl_stat *stat)
1269 {
1270         struct inode *dino, *ino;
1271         struct incore_inode *icino;
1272         int     err;
1273
1274         dino = pno->p_parent->p_base->pb_ino;
1275         assert(dino);
1276
1277         icino = incore_i_alloc(FS2ICFS(dino->i_fs), stat);
1278         if (!icino)
1279                 return -ENOSPC;
1280
1281         /*
1282          * Tell the system about the new inode.
1283          */
1284         ino =
1285             _sysio_i_new(dino->i_fs,
1286                          &icino->ici_fileid,
1287                          stat,
1288                          1,
1289                          S_ISREG(stat->st_mode)
1290                            ? &_sysio_incore_file_ops
1291                            : &_sysio_incore_dev_ops,
1292                          icino);
1293         if (!ino) {
1294                 incore_i_destroy(icino);
1295                 return -ENOMEM;
1296         }
1297
1298         /*
1299          * Insert into parent.
1300          */
1301         err =
1302             incore_directory_insert(I2IC(dino),
1303                                     &pno->p_base->pb_name,
1304                                     stat->st_ino,
1305                                     INCORE_D_TYPEOF(icino->ici_st.st_mode));
1306         if (err) {
1307                 I_RELE(ino);
1308                 _sysio_i_gone(ino);
1309                 return err;
1310         }
1311
1312         pno->p_base->pb_ino = ino;
1313         return 0;
1314 }
1315
1316 static int
1317 _sysio_incore_inop_open(struct pnode *pno, int flags __IS_UNUSED, mode_t mode)
1318 {
1319         struct intnl_stat stat;
1320         ino_t   inum;
1321
1322         /*
1323          * File exists. Nothing to do.
1324          */
1325         if (pno->p_base->pb_ino)
1326                 return 0;
1327
1328         /*
1329          * Must create a new, regular, file.
1330          */
1331         (void )memset(&stat, 0, sizeof(stat));
1332         stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1333         inum = incore_inum_alloc();
1334 #ifdef HAVE__ST_INO
1335         stat.__st_ino = inum;
1336 #endif
1337         stat.st_mode = S_IFREG | (mode & 07777);
1338         stat.st_nlink = 1;
1339         stat.st_uid = getuid();
1340         stat.st_gid = getgid();
1341         stat.st_rdev = 0;
1342         stat.st_size = 0;
1343         stat.st_blksize = 4096;
1344         stat.st_blocks = 0;
1345         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1346         stat.st_ino = inum;
1347
1348         return incore_create(pno, &stat);
1349 }
1350
1351 static int
1352 _sysio_incore_inop_close(struct inode *ino __IS_UNUSED)
1353 {
1354
1355         return 0;
1356 }
1357
1358 static int
1359 _sysio_incore_dirop_link(struct pnode *old, struct pnode *new)
1360 {
1361         struct incore_inode *icino = I2IC(old->p_base->pb_ino);
1362         int     err;
1363
1364         assert(!new->p_base->pb_ino);
1365         assert(!S_ISDIR(old->p_base->pb_ino->i_stbuf.st_mode));
1366
1367         /*
1368          * Can bump the link count?
1369          */
1370         if (!(icino->ici_st.st_nlink + 1))
1371                 return -EMLINK;
1372         /*
1373          * Insert into parent.
1374          */
1375         err =
1376             incore_directory_insert(I2IC(new->p_parent->p_base->pb_ino),
1377                                     &new->p_base->pb_name,
1378                                     icino->ici_st.st_ino,
1379                                     INCORE_D_TYPEOF(icino->ici_st.st_mode));
1380         if (err)
1381                 return err;
1382         /*
1383          * Bump the link count.
1384          */
1385         icino->ici_st.st_nlink++;
1386
1387         return 0;
1388 }
1389
1390 static int
1391 _sysio_incore_dirop_rename(struct pnode *old, struct pnode *new)
1392 {
1393         int     err;
1394         struct incore_inode *icino = I2IC(old->p_base->pb_ino);
1395
1396         if (new->p_base->pb_ino) {
1397                 /*
1398                  * Have to kill off the target first.
1399                  */
1400                 if (S_ISDIR(I2IC(new->p_base->pb_ino)->ici_st.st_mode) &&
1401                     I2IC(new->p_base->pb_ino)->ici_st.st_nlink > 2)
1402                         return -ENOTEMPTY;
1403                 err =
1404                     incore_unlink_entry(I2IC(new->p_parent->p_base->pb_ino),
1405                                         &new->p_base->pb_name);
1406                 if (err)
1407                         return err;
1408         }
1409
1410         /*
1411          * Insert into new parent.
1412          */
1413         err =
1414             incore_directory_insert(I2IC(new->p_parent->p_base->pb_ino),
1415                                     &new->p_base->pb_name,
1416                                     icino->ici_st.st_ino,
1417                                     INCORE_D_TYPEOF(icino->ici_st.st_mode));
1418         if (err)
1419                 abort();
1420         /*
1421          * Remove from the old parent.
1422          */
1423         err =
1424             incore_unlink_entry(I2IC(old->p_parent->p_base->pb_ino),
1425                                 &old->p_base->pb_name);
1426         if (err)
1427                 abort();
1428
1429         if (S_ISDIR(icino->ici_st.st_mode)) {
1430                 struct intnl_dirent *de;
1431
1432                 /*
1433                  * We moved a directory. The entry for `..' must be corrected.
1434                  */
1435                 de = icino->ici_data;
1436                 de++;
1437                 assert(strcmp(de->d_name, "..") == 0);
1438                 de->d_ino = I2IC(new->p_parent->p_base->pb_ino)->ici_st.st_ino;
1439         }
1440         return 0;
1441 }
1442
1443 static int
1444 _sysio_incore_dirop_unlink(struct pnode *pno)
1445 {
1446         struct inode *ino = pno->p_base->pb_ino;
1447         struct incore_inode *icino = I2IC(ino);
1448         int     err;
1449
1450         if (S_ISDIR(icino->ici_st.st_mode))
1451                 return -EISDIR;
1452
1453         err =
1454             incore_unlink_entry(I2IC(pno->p_parent->p_base->pb_ino),
1455                                 &pno->p_base->pb_name);
1456         return err;
1457 }
1458
1459 static int
1460 doio(ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, struct incore_inode *),
1461      struct inode *ino,
1462      struct ioctx *ioctx)
1463 {
1464
1465         ioctx->ioctx_cc =
1466             _sysio_doio(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1467                         ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1468                         (ssize_t (*)(void *, size_t, _SYSIO_OFF_T, void *))f,
1469                         I2IC(ino));
1470         if (ioctx->ioctx_cc  < 0) {
1471                 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1472                 ioctx->ioctx_cc = -1;
1473         }
1474         ioctx->ioctx_done = 1;
1475
1476         return 0;
1477 }
1478
1479 static ssize_t
1480 incore_read(void *buf, size_t nbytes,
1481             _SYSIO_OFF_T off,
1482             struct incore_inode *icino)
1483 {
1484         size_t  n;
1485
1486         if (off < 0)
1487                 return -EINVAL;
1488         if (!nbytes || off > icino->ici_st.st_size)
1489                 return 0;
1490         n = icino->ici_st.st_size - (size_t )off;
1491         if (n > nbytes)
1492                 n = nbytes;
1493         (void )memcpy(buf, (char *)icino->ici_data + off, (size_t )n);
1494
1495         return (ssize_t )n;
1496 }
1497
1498 static int
1499 _sysio_incore_filop_read(struct inode *ino, struct ioctx *ioctx)
1500 {
1501         
1502
1503         return doio(incore_read, ino, ioctx);
1504 }
1505
1506 static ssize_t
1507 incore_write(const void *buf, size_t nbytes,
1508              _SYSIO_OFF_T off,
1509              struct incore_inode *icino)
1510 {
1511         _SYSIO_OFF_T pos;
1512
1513         if (off < 0)
1514                 return -EINVAL;
1515         if (!nbytes || off > icino->ici_st.st_size)
1516                 return 0;
1517         pos = off + nbytes;
1518         if (off && pos <= off) {
1519                 /*
1520                  * It's all or nothing. We won't write just part of
1521                  * the buffer.
1522                  */
1523                 return -EFBIG;
1524         }
1525         if (pos > icino->ici_st.st_size) {
1526                 int     err;
1527
1528                 err = incore_trunc(icino, (size_t )pos, 0);
1529                 if (err)
1530                         return err;
1531         }
1532         (void )memcpy((char *)icino->ici_data + off, buf, nbytes);
1533
1534         return (ssize_t )nbytes;
1535 }
1536
1537 static int
1538 _sysio_incore_filop_write(struct inode *ino, struct ioctx *ioctx)
1539 {
1540
1541         return doio((ssize_t (*)(void *, size_t,
1542                                  _SYSIO_OFF_T,
1543                                  struct incore_inode *))incore_write,
1544                     ino,
1545                     ioctx);
1546 }
1547
1548 static _SYSIO_OFF_T
1549 _sysio_incore_filop_pos(struct inode *ino __IS_UNUSED, _SYSIO_OFF_T off)
1550 {
1551
1552         return off;
1553 }
1554
1555 static int
1556 _sysio_incore_filop_iodone(struct ioctx *iocp __IS_UNUSED)
1557 {
1558
1559         /*
1560          * It's always done in this driver. It completed when posted.
1561          */
1562         return 1;
1563 }
1564
1565 static int
1566 _sysio_incore_filop_fcntl(struct inode *ino __IS_UNUSED,
1567                           int cmd __IS_UNUSED,
1568                           va_list ap __IS_UNUSED,
1569                           int *rtn)
1570 {
1571
1572         /*
1573          * No fcntl's supported.
1574          */
1575         *rtn = -1;
1576         return -ENOTTY;
1577 }
1578
1579 static int
1580 _sysio_incore_inop_sync(struct inode *ino __IS_UNUSED)
1581 {
1582
1583         /*
1584          * With what?
1585          */
1586         return 0;
1587 }
1588
1589 static int
1590 _sysio_incore_filop_ioctl(struct inode *ino __IS_UNUSED,
1591                           unsigned long int request __IS_UNUSED,
1592                           va_list ap __IS_UNUSED)
1593 {
1594
1595         /*
1596          * No ioctl's supported.
1597          */
1598         return -ENOTTY;
1599 }
1600
1601 static int
1602 _sysio_incore_dirop_mknod(struct pnode *pno, mode_t mode, dev_t dev)
1603 {
1604         mode_t  m;
1605         struct intnl_stat stat;
1606         ino_t   inum;
1607
1608         assert(!pno->p_base->pb_ino);
1609
1610         m = mode & S_IFMT;
1611         if (S_ISCHR(m))
1612                 m &= ~S_IFCHR;
1613         else if (S_ISFIFO(m))
1614                 m &= ~S_IFIFO;
1615         else if (S_ISBLK(m))
1616                 m &= ~S_IFCHR;
1617         else
1618                 return -EINVAL;
1619         if (m)
1620                 return -EINVAL;
1621
1622         /*
1623          * Initialize attributes.
1624          */
1625         (void )memset(&stat, 0, sizeof(stat));
1626         stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1627         inum = incore_inum_alloc();
1628 #ifdef HAVE__ST_INO
1629         stat.__st_ino = inum;
1630 #endif
1631         stat.st_mode = mode;
1632         stat.st_nlink = 1;
1633         stat.st_uid = getuid();
1634         stat.st_gid = getgid();
1635         stat.st_rdev = dev;
1636         stat.st_size = 0;
1637         stat.st_blksize = 4096;
1638         stat.st_blocks = 0;
1639         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1640         stat.st_ino = inum;
1641
1642         return incore_create(pno, &stat);
1643 }
1644
1645 #ifdef _HAVE_STATVFS
1646 static int
1647 _sysio_incore_inop_statvfs(struct pnode *pno,
1648                            struct inode *ino,
1649                            struct intnl_statvfs *buf)
1650 {
1651         struct filesys *fs;
1652
1653         if (!ino)
1654                 ino = pno->p_base->pb_ino;
1655         assert(ino);
1656
1657         fs = pno->p_base->pb_ino->i_fs;
1658
1659         (void )memset(buf, 0, sizeof(struct intnl_statvfs));
1660
1661         /*
1662          * Mostly, we lie.
1663          */
1664         buf->f_bsize = fs->fs_bsize;
1665         buf->f_frsize = buf->f_bsize;
1666         buf->f_blocks = ~0;
1667         buf->f_blocks /= buf->f_bsize;
1668         buf->f_bfree = buf->f_blocks - 1;
1669         buf->f_bavail = buf->f_bfree;
1670         buf->f_files = buf->f_blocks;
1671         buf->f_ffree = buf->f_files - 1;
1672         buf->f_favail = buf->f_ffree;
1673         buf->f_fsid = fs->fs_id;
1674         buf->f_flag = 0;
1675         buf->f_namemax = ULONG_MAX;
1676
1677         return 0;
1678 }
1679 #endif
1680
1681 void
1682 _sysio_incore_inop_gone(struct inode *ino)
1683 {
1684         struct incore_inode *icino = I2IC(ino);
1685
1686         incore_i_destroy(icino);
1687 }