Whamcloud - gitweb
new upstream libsysio snapshot (20041101)
[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 "xtio.h"
65 #include "sysio.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_getdirentries(struct inode *ino,
135                                            char *buf,
136                                            size_t nbytes,
137                                            _SYSIO_OFF_T *basep);
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_getdirentries,
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_getdirentries \
221         (ssize_t (*)(struct inode *, \
222                  char *, \
223                  size_t, \
224                  _SYSIO_OFF_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_getdirentries,
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_getdirentries,
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_getdirentries(struct inode *ino,
959                                  char *buf,
960                                  size_t nbytes,
961                                  _SYSIO_OFF_T *basep)
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 (*basep > 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                                    *basep,
975                                    (probe_ty )incore_directory_position,
976                                    NULL,
977                                    (char *)icino->ici_data + *basep);
978         if (!de) {
979                 /*
980                  * Past EOF.
981                  */
982                 *basep = 0;
983                 return 0;
984         }
985
986         copy_info.data = buf;
987         copy_info.nbytes = nbytes;
988         off = (char *)de - (char *)icino->ici_data;
989         de =
990             incore_directory_probe(de,
991                                    icino->ici_st.st_size - off,
992                                    off,
993                                    (probe_ty )incore_directory_enumerate,
994                                    NULL,
995                                    &copy_info);
996         nbytes -= copy_info.nbytes;
997         icino->ici_st.st_atime = time(NULL);
998         if (!nbytes)
999                 return -EOVERFLOW;
1000         *basep = nbytes;
1001         return (ssize_t )nbytes;
1002 }
1003
1004 static struct intnl_dirent *
1005 incore_directory_best_fit(void *data, size_t len, struct lookup_data *ld)
1006 {
1007
1008         if (!ld->hole.len || len < ld->hole.len) {
1009                 ld->hole.p = data;
1010                 ld->hole.len = len;
1011         }
1012
1013         return NULL;
1014 }
1015
1016 static int
1017 incore_directory_insert(struct incore_inode *parent,
1018                         struct qstr *name,
1019                         ino_t inum,
1020                         unsigned char type)
1021 {
1022         size_t  reclen;
1023         struct lookup_data lookup_data;
1024         struct intnl_dirent *de;
1025         size_t  xt;
1026         size_t  n;
1027         size_t  r;
1028
1029         reclen = INCORE_D_RECLEN(name->len);
1030         INCORE_LD_INIT(&lookup_data, reclen, name);
1031         de =
1032             incore_directory_probe(parent->ici_data,
1033                                    parent->ici_st.st_size,
1034                                    0,
1035                                    (probe_ty )incore_directory_match,
1036                                    (probe_ty )incore_directory_best_fit,
1037                                    &lookup_data);
1038         if (de)
1039                 return -EEXIST;
1040         de = lookup_data.de;
1041         xt = (char *)lookup_data.de - (char *)parent->ici_data;
1042         n =
1043 #ifdef _DIRENT_HAVE_D_OFF
1044             de->d_off;
1045 #else
1046             xt + de->d_reclen;
1047 #endif
1048         r =
1049 #ifdef _DIRENT_HAVE_D_OFF
1050             de->d_reclen;
1051 #else
1052             INCORE_D_RECLEN(de->d_namlen);
1053 #endif
1054         if (!parent->ici_st.st_size ||
1055             xt + r + reclen > (size_t )parent->ici_st.st_size) {
1056                 int     err;
1057
1058                 err = incore_trunc(parent, xt + r + reclen, 1);
1059                 if (err)
1060                         return err;
1061                 de = (struct intnl_dirent *)((char *)parent->ici_data + xt);
1062                 n = parent->ici_st.st_size;
1063         }
1064
1065 #ifdef _DIRENT_HAVE_D_OFF
1066         de->d_off = xt + r;                             /* trim */
1067 #else
1068         de->d_reclen = r;
1069 #endif
1070         de = (struct intnl_dirent *)((char *)de + r);                           /* reposition */
1071         xt += r;
1072
1073 #ifndef _DIRENT_HAVE_D_OFF
1074         /*
1075          * Will we split this hole or use all of it?
1076          */
1077         if (lookup_data.hole.len - reclen &&
1078             lookup_data.hole.len - reclen <= INCORE_D_RECLEN(1))
1079                 reclen = lookup_data.hole.len;
1080 #endif
1081
1082         /*
1083          * Insert new.
1084          */
1085         de->d_ino = inum;
1086 #ifdef _DIRENT_HAVE_D_OFF
1087         de->d_off = n;
1088 #endif
1089         de->d_reclen = reclen;
1090         de->d_type = type;
1091         (void )memcpy(de->d_name, name->name, name->len);
1092 #ifdef _DIRENT_HAVE_D_NAMLEN
1093         de->d_namlen = name->len;
1094 #endif
1095
1096 #ifndef _DIRENT_HAVE_D_OFF
1097         xt += reclen;
1098         if (n - xt) {
1099                 /*
1100                  * White-out remaining part of the hole.
1101                  */
1102                 (void *)de += reclen;
1103                 de->d_ino = 0;
1104                 de->d_reclen = n - xt;
1105                 de->d_type = DT_WHT;
1106                 de->d_namlen = 0;
1107         }
1108 #endif
1109
1110         /*
1111          * Update attributes to reflect the new entry.
1112          */
1113         parent->ici_st.st_nlink++;
1114         assert(parent->ici_st.st_nlink);
1115         parent->ici_st.st_atime = parent->ici_st.st_mtime = time(NULL);
1116
1117         return 0;
1118 }
1119
1120 static int
1121 _sysio_incore_dirop_mkdir(struct pnode *pno, mode_t mode)
1122 {
1123         struct intnl_stat stat;
1124         struct incore_inode *icino, *parent;
1125         ino_t   inum;
1126         int     err;
1127         struct intnl_dirent *de = NULL;
1128         struct inode *ino;
1129
1130         ino = pno->p_parent->p_base->pb_ino;
1131         parent = I2IC(ino);
1132
1133         if (!S_ISDIR(parent->ici_st.st_mode))
1134                 return -ENOTDIR;
1135
1136         (void )memset(&stat, 0, sizeof(stat));
1137         stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1138         inum = incore_inum_alloc();
1139 #ifdef HAVE__ST_INO
1140         stat.__st_ino = inum;
1141 #endif
1142         stat.st_mode = S_IFDIR | (mode & 07777);
1143         stat.st_nlink = 2;
1144         stat.st_uid = getuid();
1145         stat.st_gid = getgid();
1146         stat.st_size = 0;
1147         stat.st_blksize = 4096;
1148         stat.st_blocks = 0;
1149         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1150         stat.st_ino = inum;
1151         icino = incore_directory_new(FS2ICFS(ino->i_fs), parent, &stat);
1152         if (!icino)
1153                 return -ENOSPC;
1154
1155         /*
1156          * Tell the system about the new inode.
1157          *
1158          * Persistent across remounts because we ask for immunity.
1159          */
1160         ino =
1161             _sysio_i_new(pno->p_parent->p_base->pb_ino->i_fs,
1162                          &icino->ici_fileid,
1163                          &stat,
1164                          1,
1165                          &_sysio_incore_dir_ops,
1166                          icino);
1167         if (!ino) {
1168                 incore_i_destroy(icino);
1169                 return -ENOMEM;
1170         }
1171
1172         /*
1173          * Insert into parent.
1174          */
1175         err =
1176             incore_directory_insert(parent,
1177                                     &pno->p_base->pb_name,
1178                                     stat.st_ino,
1179                                     INCORE_D_TYPEOF(S_IFDIR));
1180
1181         if (err) {
1182                 de->d_ino = 0;                          /* bad parent */
1183                 I_RELE(ino);
1184                 _sysio_i_gone(ino);
1185                 return err;
1186         }
1187
1188         pno->p_base->pb_ino = ino;
1189         return 0;
1190 }
1191
1192 static int
1193 incore_unlink_entry(struct incore_inode *icino,
1194                     struct qstr *name)
1195 {
1196         struct lookup_data lookup_data;
1197         struct intnl_dirent *de;
1198         size_t  reclen;
1199 #ifdef _DIRENT_HAVE_D_OFF
1200         size_t  off;
1201 #endif
1202
1203         if (!S_ISDIR(icino->ici_st.st_mode))
1204                 return -ENOTDIR;
1205
1206         INCORE_LD_INIT(&lookup_data, 0, name);
1207         de =
1208             incore_directory_probe(icino->ici_data,
1209                                    icino->ici_st.st_size,
1210                                    0,
1211                                    (probe_ty )incore_directory_match,
1212                                    NULL,
1213                                    &lookup_data);
1214         if (!de)
1215                 return -ENOENT;
1216         assert((size_t )((char *)de - (char *)icino->ici_data) >=
1217                sizeof(incore_dir_template));
1218 #ifndef _DIRENT_HAVE_D_OFF
1219         reclen = de->d_reclen;
1220 #else
1221         off = de->d_off;
1222         reclen = off - ((char *)de - (char *)icino->ici_data);
1223 #endif
1224         (void )memset(de, 0, reclen);
1225 #ifndef _DIRENT_HAVE_D_OFF
1226         de->d_type = (__uint8_t )DTTOIF(DT_WHT);
1227         de->d_reclen = reclen;
1228 #else
1229         lookup_data.de->d_off = off;
1230 #endif
1231
1232         /*
1233          * Adjust link count.
1234          */
1235         assert(icino->ici_st.st_nlink > 2);
1236         icino->ici_st.st_nlink--;
1237
1238         return 0;
1239 }
1240
1241 static int
1242 _sysio_incore_dirop_rmdir(struct pnode *pno)
1243 {
1244         struct inode *ino = pno->p_base->pb_ino;
1245         struct incore_inode *icino = I2IC(ino);
1246         int     err;
1247
1248         if (!pno->p_base->pb_name.len ||
1249             (pno->p_base->pb_name.name[0] == '.' &&
1250              (pno->p_base->pb_name.len == 1 ||
1251               (pno->p_base->pb_name.len == 2 &&
1252                pno->p_base->pb_name.name[1] == '.'))))
1253                 return -EINVAL;
1254
1255         if (!S_ISDIR(icino->ici_st.st_mode))
1256                 return -ENOTDIR;
1257
1258         if (icino->ici_st.st_nlink > 2)
1259                 return -ENOTEMPTY;
1260
1261         pno->p_base->pb_ino = NULL;
1262         err =
1263             incore_unlink_entry(I2IC(pno->p_parent->p_base->pb_ino),
1264                                 &pno->p_base->pb_name);
1265         return err;
1266 }
1267
1268 static int
1269 incore_create(struct pnode *pno, struct intnl_stat *stat)
1270 {
1271         struct inode *dino, *ino;
1272         struct incore_inode *icino;
1273         int     err;
1274
1275         dino = pno->p_parent->p_base->pb_ino;
1276         assert(dino);
1277
1278         icino = incore_i_alloc(FS2ICFS(dino->i_fs), stat);
1279         if (!icino)
1280                 return -ENOSPC;
1281
1282         /*
1283          * Tell the system about the new inode.
1284          */
1285         ino =
1286             _sysio_i_new(dino->i_fs,
1287                          &icino->ici_fileid,
1288                          stat,
1289                          1,
1290                          S_ISREG(stat->st_mode)
1291                            ? &_sysio_incore_file_ops
1292                            : &_sysio_incore_dev_ops,
1293                          icino);
1294         if (!ino) {
1295                 incore_i_destroy(icino);
1296                 return -ENOMEM;
1297         }
1298
1299         /*
1300          * Insert into parent.
1301          */
1302         err =
1303             incore_directory_insert(I2IC(dino),
1304                                     &pno->p_base->pb_name,
1305                                     stat->st_ino,
1306                                     INCORE_D_TYPEOF(icino->ici_st.st_mode));
1307         if (err) {
1308                 I_RELE(ino);
1309                 _sysio_i_gone(ino);
1310                 return err;
1311         }
1312
1313         pno->p_base->pb_ino = ino;
1314         return 0;
1315 }
1316
1317 static int
1318 _sysio_incore_inop_open(struct pnode *pno, int flags __IS_UNUSED, mode_t mode)
1319 {
1320         struct intnl_stat stat;
1321         ino_t   inum;
1322
1323         /*
1324          * File exists. Nothing to do.
1325          */
1326         if (pno->p_base->pb_ino)
1327                 return 0;
1328
1329         /*
1330          * Must create a new, regular, file.
1331          */
1332         (void )memset(&stat, 0, sizeof(stat));
1333         stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1334         inum = incore_inum_alloc();
1335 #ifdef HAVE__ST_INO
1336         stat.__st_ino = inum;
1337 #endif
1338         stat.st_mode = S_IFREG | (mode & 07777);
1339         stat.st_nlink = 1;
1340         stat.st_uid = getuid();
1341         stat.st_gid = getgid();
1342         stat.st_rdev = 0;
1343         stat.st_size = 0;
1344         stat.st_blksize = 4096;
1345         stat.st_blocks = 0;
1346         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1347         stat.st_ino = inum;
1348
1349         return incore_create(pno, &stat);
1350 }
1351
1352 static int
1353 _sysio_incore_inop_close(struct inode *ino __IS_UNUSED)
1354 {
1355
1356         return 0;
1357 }
1358
1359 static int
1360 _sysio_incore_dirop_link(struct pnode *old, struct pnode *new)
1361 {
1362         struct incore_inode *icino = I2IC(old->p_base->pb_ino);
1363         int     err;
1364
1365         assert(!new->p_base->pb_ino);
1366         assert(!S_ISDIR(old->p_base->pb_ino->i_stbuf.st_mode));
1367
1368         /*
1369          * Can bump the link count?
1370          */
1371         if (!(icino->ici_st.st_nlink + 1))
1372                 return -EMLINK;
1373         /*
1374          * Insert into parent.
1375          */
1376         err =
1377             incore_directory_insert(I2IC(new->p_parent->p_base->pb_ino),
1378                                     &new->p_base->pb_name,
1379                                     icino->ici_st.st_ino,
1380                                     INCORE_D_TYPEOF(icino->ici_st.st_mode));
1381         if (err)
1382                 return err;
1383         /*
1384          * Bump the link count.
1385          */
1386         icino->ici_st.st_nlink++;
1387
1388         return 0;
1389 }
1390
1391 static int
1392 _sysio_incore_dirop_rename(struct pnode *old, struct pnode *new)
1393 {
1394         int     err;
1395         struct incore_inode *icino = I2IC(old->p_base->pb_ino);
1396
1397         if (new->p_base->pb_ino) {
1398                 /*
1399                  * Have to kill off the target first.
1400                  */
1401                 if (S_ISDIR(I2IC(new->p_base->pb_ino)->ici_st.st_mode) &&
1402                     I2IC(new->p_base->pb_ino)->ici_st.st_nlink > 2)
1403                         return -ENOTEMPTY;
1404                 err =
1405                     incore_unlink_entry(I2IC(new->p_parent->p_base->pb_ino),
1406                                         &new->p_base->pb_name);
1407                 if (err)
1408                         return err;
1409         }
1410
1411         /*
1412          * Insert into new parent.
1413          */
1414         err =
1415             incore_directory_insert(I2IC(new->p_parent->p_base->pb_ino),
1416                                     &new->p_base->pb_name,
1417                                     icino->ici_st.st_ino,
1418                                     INCORE_D_TYPEOF(icino->ici_st.st_mode));
1419         if (err)
1420                 abort();
1421         /*
1422          * Remove from the old parent.
1423          */
1424         err =
1425             incore_unlink_entry(I2IC(old->p_parent->p_base->pb_ino),
1426                                 &old->p_base->pb_name);
1427         if (err)
1428                 abort();
1429
1430         if (S_ISDIR(icino->ici_st.st_mode)) {
1431                 struct intnl_dirent *de;
1432
1433                 /*
1434                  * We moved a directory. The entry for `..' must be corrected.
1435                  */
1436                 de = icino->ici_data;
1437                 de++;
1438                 assert(strcmp(de->d_name, "..") == 0);
1439                 de->d_ino = I2IC(new->p_parent->p_base->pb_ino)->ici_st.st_ino;
1440         }
1441         return 0;
1442 }
1443
1444 static int
1445 _sysio_incore_dirop_unlink(struct pnode *pno)
1446 {
1447         struct inode *ino = pno->p_base->pb_ino;
1448         struct incore_inode *icino = I2IC(ino);
1449         int     err;
1450
1451         if (S_ISDIR(icino->ici_st.st_mode))
1452                 return -EISDIR;
1453
1454         err =
1455             incore_unlink_entry(I2IC(pno->p_parent->p_base->pb_ino),
1456                                 &pno->p_base->pb_name);
1457         return err;
1458 }
1459
1460 static int
1461 doio(ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, struct incore_inode *),
1462      struct inode *ino,
1463      struct ioctx *ioctx)
1464 {
1465
1466         ioctx->ioctx_cc =
1467             _sysio_doio(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1468                         ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1469                         (ssize_t (*)(void *, size_t, _SYSIO_OFF_T, void *))f,
1470                         I2IC(ino));
1471         if (ioctx->ioctx_cc  < 0) {
1472                 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1473                 ioctx->ioctx_cc = -1;
1474         }
1475         ioctx->ioctx_done = 1;
1476
1477         return 0;
1478 }
1479
1480 static ssize_t
1481 incore_read(void *buf, size_t nbytes,
1482             _SYSIO_OFF_T off,
1483             struct incore_inode *icino)
1484 {
1485         size_t  n;
1486
1487         if (off < 0)
1488                 return -EINVAL;
1489         if (!nbytes || off > icino->ici_st.st_size)
1490                 return 0;
1491         n = icino->ici_st.st_size - (size_t )off;
1492         if (n > nbytes)
1493                 n = nbytes;
1494         (void )memcpy(buf, (char *)icino->ici_data + off, (size_t )n);
1495
1496         return (ssize_t )n;
1497 }
1498
1499 static int
1500 _sysio_incore_filop_read(struct inode *ino, struct ioctx *ioctx)
1501 {
1502         
1503
1504         return doio(incore_read, ino, ioctx);
1505 }
1506
1507 static ssize_t
1508 incore_write(const void *buf, size_t nbytes,
1509              _SYSIO_OFF_T off,
1510              struct incore_inode *icino)
1511 {
1512         _SYSIO_OFF_T pos;
1513
1514         if (off < 0)
1515                 return -EINVAL;
1516         if (!nbytes || off > icino->ici_st.st_size)
1517                 return 0;
1518         pos = off + nbytes;
1519         if (off && pos <= off) {
1520                 /*
1521                  * It's all or nothing. We won't write just part of
1522                  * the buffer.
1523                  */
1524                 return -EFBIG;
1525         }
1526         if (pos > icino->ici_st.st_size) {
1527                 int     err;
1528
1529                 err = incore_trunc(icino, (size_t )pos, 0);
1530                 if (err)
1531                         return err;
1532         }
1533         (void )memcpy((char *)icino->ici_data + off, buf, nbytes);
1534
1535         return (ssize_t )nbytes;
1536 }
1537
1538 static int
1539 _sysio_incore_filop_write(struct inode *ino, struct ioctx *ioctx)
1540 {
1541
1542         return doio((ssize_t (*)(void *, size_t,
1543                                  _SYSIO_OFF_T,
1544                                  struct incore_inode *))incore_write,
1545                     ino,
1546                     ioctx);
1547 }
1548
1549 static _SYSIO_OFF_T
1550 _sysio_incore_filop_pos(struct inode *ino __IS_UNUSED, _SYSIO_OFF_T off)
1551 {
1552
1553         return off;
1554 }
1555
1556 static int
1557 _sysio_incore_filop_iodone(struct ioctx *iocp __IS_UNUSED)
1558 {
1559
1560         /*
1561          * It's always done in this driver. It completed when posted.
1562          */
1563         return 1;
1564 }
1565
1566 static int
1567 _sysio_incore_filop_fcntl(struct inode *ino __IS_UNUSED,
1568                           int cmd __IS_UNUSED,
1569                           va_list ap __IS_UNUSED,
1570                           int *rtn)
1571 {
1572
1573         /*
1574          * No fcntl's supported.
1575          */
1576         *rtn = -1;
1577         return -ENOTTY;
1578 }
1579
1580 static int
1581 _sysio_incore_inop_sync(struct inode *ino __IS_UNUSED)
1582 {
1583
1584         /*
1585          * With what?
1586          */
1587         return 0;
1588 }
1589
1590 static int
1591 _sysio_incore_filop_ioctl(struct inode *ino __IS_UNUSED,
1592                           unsigned long int request __IS_UNUSED,
1593                           va_list ap __IS_UNUSED)
1594 {
1595
1596         /*
1597          * No ioctl's supported.
1598          */
1599         return -ENOTTY;
1600 }
1601
1602 static int
1603 _sysio_incore_dirop_mknod(struct pnode *pno, mode_t mode, dev_t dev)
1604 {
1605         mode_t  m;
1606         struct intnl_stat stat;
1607         ino_t   inum;
1608
1609         assert(!pno->p_base->pb_ino);
1610
1611         m = mode & S_IFMT;
1612         if (S_ISCHR(m))
1613                 m &= ~S_IFCHR;
1614         else if (S_ISFIFO(m))
1615                 m &= ~S_IFIFO;
1616         else if (S_ISBLK(m))
1617                 m &= ~S_IFCHR;
1618         else
1619                 return -EINVAL;
1620         if (m)
1621                 return -EINVAL;
1622
1623         /*
1624          * Initialize attributes.
1625          */
1626         (void )memset(&stat, 0, sizeof(stat));
1627         stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1628         inum = incore_inum_alloc();
1629 #ifdef HAVE__ST_INO
1630         stat.__st_ino = inum;
1631 #endif
1632         stat.st_mode = mode;
1633         stat.st_nlink = 1;
1634         stat.st_uid = getuid();
1635         stat.st_gid = getgid();
1636         stat.st_rdev = dev;
1637         stat.st_size = 0;
1638         stat.st_blksize = 4096;
1639         stat.st_blocks = 0;
1640         stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1641         stat.st_ino = inum;
1642
1643         return incore_create(pno, &stat);
1644 }
1645
1646 #ifdef _HAVE_STATVFS
1647 static int
1648 _sysio_incore_inop_statvfs(struct pnode *pno,
1649                            struct inode *ino,
1650                            struct intnl_statvfs *buf)
1651 {
1652         struct filesys *fs;
1653
1654         if (!ino)
1655                 ino = pno->p_base->pb_ino;
1656         assert(ino);
1657
1658         fs = pno->p_base->pb_ino->i_fs;
1659
1660         (void )memset(buf, 0, sizeof(struct intnl_statvfs));
1661
1662         /*
1663          * Mostly, we lie.
1664          */
1665         buf->f_bsize = fs->fs_bsize;
1666         buf->f_frsize = buf->f_bsize;
1667         buf->f_blocks = ~0;
1668         buf->f_blocks /= buf->f_bsize;
1669         buf->f_bfree = buf->f_blocks - 1;
1670         buf->f_bavail = buf->f_bfree;
1671         buf->f_files = buf->f_blocks;
1672         buf->f_ffree = buf->f_files - 1;
1673         buf->f_favail = buf->f_ffree;
1674         buf->f_fsid = fs->fs_id;
1675         buf->f_flag = 0;
1676         buf->f_namemax = ULONG_MAX;
1677
1678         return 0;
1679 }
1680 #endif
1681
1682 void
1683 _sysio_incore_inop_gone(struct inode *ino)
1684 {
1685         struct incore_inode *icino = I2IC(ino);
1686
1687         incore_i_destroy(icino);
1688 }