2 * This Cplant(TM) source code is the property of Sandia National
5 * This Cplant(TM) source code is copyrighted by Sandia National
8 * The redistribution of this Cplant(TM) source code is subject to the
9 * terms of the GNU Lesser General Public License
10 * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
12 * Cplant(TM) Copyright 1998-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
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Questions or comments about this library should be sent to:
37 * Sandia National Laboratories, New Mexico
39 * Albuquerque, NM 87185-1110
56 #include <sys/types.h>
60 #include <sys/statvfs.h>
62 #include <sys/queue.h>
71 #include "fs_incore.h"
75 * In-core file system pseudo-driver.
81 #define INCORE_BLKSIZE (8192)
84 * Format of an 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 */
94 * Given pointer to inode, return pointer to incore-inode.
96 #define I2IC(ino) ((struct incore_inode *)(ino)->i_private)
98 struct incore_filesys {
99 LIST_HEAD(, incore_inode) icfs_icinodes; /* all i-nodes list */
103 * Given pointer to filesys, return pointer to incore-filesys.
105 #define FS2ICFS(fs) ((struct incore_filesys *)(fs)->fs_private)
107 static int _sysio_incore_fsswop_mount(const char *source,
110 struct pnode *tocover,
111 struct mount **mntp);
113 static struct fssw_ops incore_fssw_ops = {
114 _sysio_incore_fsswop_mount
117 static void _sysio_incore_fsop_gone(struct filesys *fs);
119 static struct filesys_ops incore_fs_ops = {
120 _sysio_incore_fsop_gone,
123 static int _sysio_incore_dirop_lookup(struct pnode *pno,
125 struct intent *intnt,
127 static int _sysio_incore_inop_getattr(struct pnode *pno,
129 struct intnl_stat *stbuf);
130 static int _sysio_incore_inop_setattr(struct pnode *pno,
133 struct intnl_stat *stbuf);
134 static ssize_t _sysio_incore_dirop_filldirentries(struct inode *ino,
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,
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,
156 static int _sysio_incore_dirop_mknod(struct pnode *pno, mode_t mode, dev_t dev);
158 static int _sysio_incore_inop_statvfs(struct pnode *pno,
160 struct intnl_statvfs *buf);
162 static void _sysio_incore_inop_gone(struct inode *ino);
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 *, \
184 va_list))_sysio_do_eisdir
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,
210 _sysio_incore_inop_statvfs,
212 _sysio_incore_inop_gone
215 #define _sysio_incore_filop_lookup \
216 (int (*)(struct pnode *, \
219 const char *))_sysio_do_illop
220 #define _sysio_incore_filop_filldirentries \
221 (ssize_t (*)(struct inode *, \
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
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,
266 _sysio_incore_inop_statvfs,
268 _sysio_incore_inop_gone
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,
295 _sysio_incore_inop_statvfs,
297 _sysio_incore_inop_gone
300 typedef void *(*probe_ty)(void *data, size_t len, void *arg);
303 * Lookup data argument bundle record.
306 struct qstr *name; /* desired entry name */
307 struct intnl_dirent *de; /* last dirent */
308 size_t minsiz; /* min hole needed */
310 void *p; /* best hole */
311 size_t len; /* best hole len */
316 * Initialize lookup data argument bundle.
318 #define INCORE_LD_INIT(ld, minsz, qs) \
322 (ld)->minsiz = (minsz); \
323 (ld)->hole.p = NULL; \
324 (ld)->hole.len = 0; \
328 * Calculate size of a directory entry given length of the entry name.
330 #define INCORE_D_RECLEN(namlen) \
331 (((size_t )&((struct intnl_dirent *)0)->d_name + \
332 (namlen) + 1 + sizeof(void *)) & \
333 ~(sizeof(void *) - 1))
336 * Given mode bits, return directory entry type code.
338 #define INCORE_D_TYPEOF(m) (((m) & S_IFMT) >> 12)
340 static char incore_dir_template[INCORE_D_RECLEN(1) + INCORE_D_RECLEN(2)];
342 static struct intnl_dirent incore_dir_template[] = {
347 INCORE_D_TYPEOF(S_IFDIR),
352 INCORE_D_RECLEN(1) + INCORE_D_RECLEN(2),
354 INCORE_D_TYPEOF(S_IFDIR),
361 * Initialize this driver.
366 struct intnl_dirent *de;
370 * Fill in the directory template.
372 de = (struct intnl_dirent *)incore_dir_template;
373 #ifdef _DIRENT_HAVE_D_OFF
376 off = de->d_reclen = INCORE_D_RECLEN(1);
377 de->d_type = INCORE_D_TYPEOF(S_IFDIR);
379 #ifdef _DIRENT_HAVE_D_NAMLEN
383 * Move to entry for `..'
385 de = (struct intnl_dirent *)((char *)de + off);
386 de->d_reclen = INCORE_D_RECLEN(2);
387 #ifdef _DIRENT_HAVE_D_NAMLEN
390 #ifdef _DIRENT_HAVE_D_OFF
394 de->d_type = INCORE_D_TYPEOF(S_IFDIR);
395 de->d_name[0] = de->d_name[1] = '.';
398 return _sysio_fssw_register("incore", &incore_fssw_ops);
404 static ino_t nxtnum = 1;
410 static struct incore_inode *
411 incore_i_alloc(struct incore_filesys *icfs, struct intnl_stat *st)
413 struct incore_inode *icino;
416 assert(!st->st_size);
418 icino = malloc(sizeof(struct incore_inode));
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;
426 LIST_INSERT_HEAD(&icfs->icfs_icinodes, icino, ici_link);
432 incore_trunc(struct incore_inode *icino, _SYSIO_OFF_T size, int clear)
441 if (icino->ici_data) {
442 free(icino->ici_data);
443 icino->ici_data = NULL;
448 p = realloc(icino->ici_data, (size_t )n);
452 if (clear && n > icino->ici_st.st_size)
453 (void )memset((char *)icino->ici_data + icino->ici_st.st_size,
455 (size_t )(n - icino->ici_st.st_size));
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);
465 incore_i_destroy(struct incore_inode *icino)
468 LIST_REMOVE(icino, ici_link);
469 (void )incore_trunc(icino, 0, 0);
473 static struct incore_inode *
474 incore_directory_new(struct incore_filesys *icfs,
475 struct incore_inode *parent,
476 struct intnl_stat *st)
478 struct incore_inode *icino;
480 struct intnl_dirent *de;
482 icino = incore_i_alloc(icfs, st);
487 parent = icino; /* root */
490 * Allocate and init directory data.
492 err = incore_trunc(icino, sizeof(incore_dir_template), 1);
494 incore_i_destroy(icino);
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;
503 (struct intnl_dirent *)((char *)de +
504 #ifdef _DIRENT_HAVE_D_OFF
510 de->d_ino = parent->ici_st.st_ino;
513 * Set creation time to modify time set by truncate.
515 st->st_ctime = st->st_mtime;
521 _sysio_incore_fsswop_mount(const char *source,
523 const void *data __IS_UNUSED,
524 struct pnode *tocover,
535 struct intnl_stat stat;
536 struct incore_filesys *icfs;
538 struct incore_inode *icino;
541 struct pnode_base *rootpb;
543 static struct qstr noname = { NULL, 0, 0 };
546 * Source is a specification for the root attributes of this
547 * new file system in the format:
549 * <permissions>[+<owner>][-<group>]
551 ul = strtoul(source, &cp, 0);
552 mode = (mode_t )ul & 07777;
553 uid = getuid(); /* default */
554 gid = getgid(); /* default */
557 * Get user and/or group.
560 (ul == ULONG_MAX && errno == ERANGE) ||
561 (unsigned long)mode != ul ||
565 l = strtol(source, &cp, 0);
567 if (((l == LONG_MIN || l == LONG_MAX) &&
574 l = strtol(source, &cp, 0);
576 if (((l == LONG_MIN || l == LONG_MAX) &&
586 dev = _sysio_dev_alloc();
598 icfs = malloc(sizeof(struct incore_filesys));
603 (void )memset(icfs, 0, sizeof(struct incore_filesys));
604 LIST_INIT(&icfs->icfs_icinodes);
607 * Create root i-node.
609 (void )memset(&stat, 0, sizeof(stat));
611 inum = incore_inum_alloc();
613 stat.__st_ino = inum;
615 stat.st_mode = S_IFDIR | (mode & 07777);
620 stat.st_blksize = INCORE_BLKSIZE;
622 stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
624 icino = incore_directory_new(icfs, NULL, &stat);
627 icino->ici_st.st_atime = icino->ici_st.st_mtime;
630 _sysio_fs_new(&incore_fs_ops,
631 (flags & MOUNT_F_RO) ? FS_F_RO : 0,
639 * Create root for system.
641 * Persistent across remounts because we ask for immunity.
648 &_sysio_incore_dir_ops,
654 rootpb = _sysio_pb_new(&noname, NULL, rooti);
661 * Have path-node specified by the given source argument. Let the
662 * system finish the job, now.
679 if (mnt && _sysio_do_unmount(mnt) != 0)
682 _sysio_pb_gone(rootpb);
692 incore_i_destroy(icino);
705 _sysio_incore_fsop_gone(struct filesys *fs)
707 struct incore_filesys *icfs;
708 struct incore_inode *icino, *oicino;
713 * Free up i-node resource associated with this file system.
715 icino = icfs->icfs_icinodes.lh_first;
718 icino = icino->ici_link.le_next;
719 incore_i_destroy(oicino);
723 * Free the FS record.
729 * A directory search engine. Various functions are carried out by
730 * supplying appropriate callback functions.
732 * The two arguments, entry and hole, are called, if not null, for each
733 * directory entry and hole, respectively.
736 incore_directory_probe(void *data,
739 #ifndef _DIRENT_HAVE_D_OFF
747 struct intnl_dirent *de;
753 #ifdef _DIRENT_HAVE_D_OFF
756 assert(de->d_reclen);
758 if (entry && (p = (*entry)(de, de->d_reclen, arg)))
761 #ifdef _DIRENT_HAVE_D_OFF
764 ((void *)de - data) + de->d_reclen;
767 p = (*hole)((void *)de, de->d_reclen, arg);
773 de = (struct intnl_dirent *)((char *)data + n);
779 static struct intnl_dirent *
780 incore_directory_match(struct intnl_dirent *de,
782 struct lookup_data *ld)
786 #if defined(BSD) || defined(REDSTORM)
787 if (IFTODT(de->d_type) == DT_WHT)
790 #ifdef _DIRENT_HAVE_D_NAMLEN
794 const char *cp, *end;
797 end = (const char *)de + reclen;
798 while (cp < end && *cp != '\0')
800 len = cp - de->d_name;
803 if (ld->name->len == len &&
804 strncmp(de->d_name, ld->name->name, ld->name->len) == 0)
811 _sysio_incore_dirop_lookup(struct pnode *pno,
813 struct intent *intnt __IS_UNUSED,
814 const char *path __IS_UNUSED)
817 struct intnl_dirent *de;
818 struct incore_inode *icino;
819 struct lookup_data lookup_data;
820 struct file_identifier fileid;
822 struct inode_ops *ops;
831 (*inop)->i_stbuf = icino->ici_st;
835 ino = pno->p_parent->p_base->pb_ino;
837 INCORE_LD_INIT(&lookup_data,
839 &pno->p_base->pb_name);
841 incore_directory_probe(icino->ici_data,
842 icino->ici_st.st_size,
844 (probe_ty )incore_directory_match,
850 fileid.fid_data = &de->d_ino;
851 fileid.fid_len = sizeof(de->d_ino);
853 _sysio_i_find(ino->i_fs, &fileid);
857 icino->ici_fileid.fid_data = &icino->ici_st.st_ino;
858 icino->ici_fileid.fid_len = sizeof(icino->ici_st.st_ino);
860 switch (icino->ici_st.st_mode & S_IFMT) {
862 ops = &_sysio_incore_dir_ops;
865 ops = &_sysio_incore_file_ops;
873 _sysio_i_new(ino->i_fs,
891 _sysio_incore_inop_getattr(struct pnode *pno,
893 struct intnl_stat *stbuf)
895 struct incore_inode *icino;
898 ino = pno->p_base->pb_ino;
900 *stbuf = icino->ici_st;
905 _sysio_incore_inop_setattr(struct pnode *pno,
908 struct intnl_stat *stbuf)
910 struct incore_inode *icino;
914 ino = pno->p_base->pb_ino;
920 if (mask & SETATTR_LEN) {
921 err = incore_trunc(icino, stbuf->st_size, 1);
924 mask &= ~SETATTR_LEN;
926 if (mask & SETATTR_MODE) {
927 icino->ici_st.st_mode =
928 (icino->ici_st.st_mode & S_IFMT) | (stbuf->st_mode & 07777);
930 if (mask & SETATTR_MTIME)
931 icino->ici_st.st_mtime = stbuf->st_mtime;
932 if (mask & SETATTR_ATIME)
933 icino->ici_st.st_atime = stbuf->st_atime;
934 if (mask & SETATTR_UID)
935 icino->ici_st.st_uid = stbuf->st_uid;
936 if (mask & SETATTR_GID)
937 icino->ici_st.st_gid = stbuf->st_gid;
938 icino->ici_st.st_ctime = time(NULL);
940 ino->i_stbuf = icino->ici_st;
946 incore_directory_position(struct intnl_dirent *de,
947 size_t reclen __IS_UNUSED,
951 return (void *)de >= p ? de : NULL;
961 * Eumeration callback.
964 * Whiteout entries are never returned.
967 incore_directory_enumerate(struct intnl_dirent *de,
969 struct copy_info *cinfo) {
972 if (de->d_type == DT_WHT) {
974 * Keep going but skip the copy.
980 if (reclen > cinfo->nbytes)
982 (void *)memcpy(cinfo->data, de, reclen);
983 cinfo->data = (char *)cinfo->data + reclen;
984 cinfo->nbytes -= reclen;
989 _sysio_incore_dirop_filldirentries(struct inode *ino,
994 struct incore_inode *icino = I2IC(ino);
996 struct intnl_dirent *de;
997 struct copy_info copy_info;
999 if (*posp >= icino->ici_st.st_size)
1003 incore_directory_probe(icino->ici_data,
1004 icino->ici_st.st_size,
1006 (probe_ty )incore_directory_position,
1008 (char *)icino->ici_data + *posp);
1016 copy_info.data = buf;
1017 copy_info.nbytes = nbytes;
1018 copy_info.count = 0;
1019 off = (char *)de - (char *)icino->ici_data;
1021 incore_directory_probe(de,
1022 icino->ici_st.st_size - off,
1024 (probe_ty )incore_directory_enumerate,
1027 icino->ici_st.st_atime = time(NULL);
1028 if (nbytes == copy_info.nbytes && copy_info.count)
1030 nbytes -= copy_info.nbytes;
1036 return (ssize_t )nbytes;
1039 static struct intnl_dirent *
1040 incore_directory_best_fit(void *data, size_t len, struct lookup_data *ld)
1043 if (!ld->hole.len || len < ld->hole.len) {
1052 incore_directory_insert(struct incore_inode *parent,
1058 struct lookup_data lookup_data;
1059 struct intnl_dirent *de;
1064 reclen = INCORE_D_RECLEN(name->len);
1065 INCORE_LD_INIT(&lookup_data, reclen, name);
1067 incore_directory_probe(parent->ici_data,
1068 parent->ici_st.st_size,
1070 (probe_ty )incore_directory_match,
1071 (probe_ty )incore_directory_best_fit,
1075 de = lookup_data.de;
1076 xt = (char *)lookup_data.de - (char *)parent->ici_data;
1078 #ifdef _DIRENT_HAVE_D_OFF
1084 #ifdef _DIRENT_HAVE_D_OFF
1087 INCORE_D_RECLEN(de->d_namlen);
1089 if (!parent->ici_st.st_size ||
1090 xt + r + reclen > (size_t )parent->ici_st.st_size) {
1093 err = incore_trunc(parent, xt + r + reclen, 1);
1096 de = (struct intnl_dirent *)((char *)parent->ici_data + xt);
1097 n = parent->ici_st.st_size;
1100 #ifdef _DIRENT_HAVE_D_OFF
1101 de->d_off = xt + r; /* trim */
1105 de = (struct intnl_dirent *)((char *)de + r); /* reposition */
1108 #ifndef _DIRENT_HAVE_D_OFF
1110 * Will we split this hole or use all of it?
1112 if (lookup_data.hole.len - reclen &&
1113 lookup_data.hole.len - reclen <= INCORE_D_RECLEN(1))
1114 reclen = lookup_data.hole.len;
1121 #ifdef _DIRENT_HAVE_D_OFF
1124 de->d_reclen = reclen;
1126 (void )memcpy(de->d_name, name->name, name->len);
1127 #ifdef _DIRENT_HAVE_D_NAMLEN
1128 de->d_namlen = name->len;
1131 #ifndef _DIRENT_HAVE_D_OFF
1135 * White-out remaining part of the hole.
1137 (void *)de += reclen;
1139 de->d_reclen = n - xt;
1140 de->d_type = DT_WHT;
1146 * Update attributes to reflect the new entry.
1148 parent->ici_st.st_nlink++;
1149 assert(parent->ici_st.st_nlink);
1150 parent->ici_st.st_atime = parent->ici_st.st_mtime = time(NULL);
1156 _sysio_incore_dirop_mkdir(struct pnode *pno, mode_t mode)
1158 struct intnl_stat stat;
1159 struct incore_inode *icino, *parent;
1162 struct intnl_dirent *de = NULL;
1165 ino = pno->p_parent->p_base->pb_ino;
1168 if (!S_ISDIR(parent->ici_st.st_mode))
1171 (void )memset(&stat, 0, sizeof(stat));
1172 stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1173 inum = incore_inum_alloc();
1175 stat.__st_ino = inum;
1177 stat.st_mode = S_IFDIR | (mode & 07777);
1179 stat.st_uid = getuid();
1180 stat.st_gid = getgid();
1182 stat.st_blksize = 4096;
1184 stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1186 icino = incore_directory_new(FS2ICFS(ino->i_fs), parent, &stat);
1191 * Tell the system about the new inode.
1193 * Persistent across remounts because we ask for immunity.
1196 _sysio_i_new(pno->p_parent->p_base->pb_ino->i_fs,
1200 &_sysio_incore_dir_ops,
1203 incore_i_destroy(icino);
1208 * Insert into parent.
1211 incore_directory_insert(parent,
1212 &pno->p_base->pb_name,
1214 INCORE_D_TYPEOF(S_IFDIR));
1217 de->d_ino = 0; /* bad parent */
1223 pno->p_base->pb_ino = ino;
1228 incore_unlink_entry(struct incore_inode *icino,
1231 struct lookup_data lookup_data;
1232 struct intnl_dirent *de;
1234 #ifdef _DIRENT_HAVE_D_OFF
1238 if (!S_ISDIR(icino->ici_st.st_mode))
1241 INCORE_LD_INIT(&lookup_data, 0, name);
1243 incore_directory_probe(icino->ici_data,
1244 icino->ici_st.st_size,
1246 (probe_ty )incore_directory_match,
1251 assert((size_t )((char *)de - (char *)icino->ici_data) >=
1252 sizeof(incore_dir_template));
1253 #ifndef _DIRENT_HAVE_D_OFF
1254 reclen = de->d_reclen;
1257 reclen = off - ((char *)de - (char *)icino->ici_data);
1259 (void )memset(de, 0, reclen);
1260 #ifndef _DIRENT_HAVE_D_OFF
1261 de->d_type = (__uint8_t )DTTOIF(DT_WHT);
1262 de->d_reclen = reclen;
1264 lookup_data.de->d_off = off;
1268 * Adjust link count.
1270 assert(icino->ici_st.st_nlink > 2);
1271 icino->ici_st.st_nlink--;
1277 _sysio_incore_dirop_rmdir(struct pnode *pno)
1279 struct inode *ino = pno->p_base->pb_ino;
1280 struct incore_inode *icino = I2IC(ino);
1283 if (!pno->p_base->pb_name.len ||
1284 (pno->p_base->pb_name.name[0] == '.' &&
1285 (pno->p_base->pb_name.len == 1 ||
1286 (pno->p_base->pb_name.len == 2 &&
1287 pno->p_base->pb_name.name[1] == '.'))))
1290 if (!S_ISDIR(icino->ici_st.st_mode))
1293 if (icino->ici_st.st_nlink > 2)
1296 pno->p_base->pb_ino = NULL;
1298 incore_unlink_entry(I2IC(pno->p_parent->p_base->pb_ino),
1299 &pno->p_base->pb_name);
1304 incore_create(struct pnode *pno, struct intnl_stat *stat)
1306 struct inode *dino, *ino;
1307 struct incore_inode *icino;
1310 dino = pno->p_parent->p_base->pb_ino;
1313 icino = incore_i_alloc(FS2ICFS(dino->i_fs), stat);
1318 * Tell the system about the new inode.
1321 _sysio_i_new(dino->i_fs,
1325 S_ISREG(stat->st_mode)
1326 ? &_sysio_incore_file_ops
1327 : &_sysio_incore_dev_ops,
1330 incore_i_destroy(icino);
1335 * Insert into parent.
1338 incore_directory_insert(I2IC(dino),
1339 &pno->p_base->pb_name,
1341 INCORE_D_TYPEOF(icino->ici_st.st_mode));
1348 pno->p_base->pb_ino = ino;
1353 _sysio_incore_inop_open(struct pnode *pno, int flags __IS_UNUSED, mode_t mode)
1355 struct intnl_stat stat;
1359 * File exists. Nothing to do.
1361 if (pno->p_base->pb_ino)
1365 * Must create a new, regular, file.
1367 (void )memset(&stat, 0, sizeof(stat));
1368 stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1369 inum = incore_inum_alloc();
1371 stat.__st_ino = inum;
1373 stat.st_mode = S_IFREG | (mode & 07777);
1375 stat.st_uid = getuid();
1376 stat.st_gid = getgid();
1379 stat.st_blksize = 4096;
1381 stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1384 return incore_create(pno, &stat);
1388 _sysio_incore_inop_close(struct inode *ino __IS_UNUSED)
1395 _sysio_incore_dirop_link(struct pnode *old, struct pnode *new)
1397 struct incore_inode *icino = I2IC(old->p_base->pb_ino);
1400 assert(!new->p_base->pb_ino);
1401 assert(!S_ISDIR(old->p_base->pb_ino->i_stbuf.st_mode));
1404 * Can bump the link count?
1406 if (!(icino->ici_st.st_nlink + 1))
1409 * Insert into parent.
1412 incore_directory_insert(I2IC(new->p_parent->p_base->pb_ino),
1413 &new->p_base->pb_name,
1414 icino->ici_st.st_ino,
1415 INCORE_D_TYPEOF(icino->ici_st.st_mode));
1419 * Bump the link count.
1421 icino->ici_st.st_nlink++;
1427 _sysio_incore_dirop_rename(struct pnode *old, struct pnode *new)
1430 struct incore_inode *icino = I2IC(old->p_base->pb_ino);
1432 if (new->p_base->pb_ino) {
1434 * Have to kill off the target first.
1436 if (S_ISDIR(I2IC(new->p_base->pb_ino)->ici_st.st_mode) &&
1437 I2IC(new->p_base->pb_ino)->ici_st.st_nlink > 2)
1440 incore_unlink_entry(I2IC(new->p_parent->p_base->pb_ino),
1441 &new->p_base->pb_name);
1447 * Insert into new parent.
1450 incore_directory_insert(I2IC(new->p_parent->p_base->pb_ino),
1451 &new->p_base->pb_name,
1452 icino->ici_st.st_ino,
1453 INCORE_D_TYPEOF(icino->ici_st.st_mode));
1457 * Remove from the old parent.
1460 incore_unlink_entry(I2IC(old->p_parent->p_base->pb_ino),
1461 &old->p_base->pb_name);
1465 if (S_ISDIR(icino->ici_st.st_mode)) {
1466 struct intnl_dirent *de;
1469 * We moved a directory. The entry for `..' must be corrected.
1471 de = icino->ici_data;
1473 assert(strcmp(de->d_name, "..") == 0);
1474 de->d_ino = I2IC(new->p_parent->p_base->pb_ino)->ici_st.st_ino;
1480 _sysio_incore_dirop_unlink(struct pnode *pno)
1482 struct inode *ino = pno->p_base->pb_ino;
1483 struct incore_inode *icino = I2IC(ino);
1486 if (S_ISDIR(icino->ici_st.st_mode))
1490 incore_unlink_entry(I2IC(pno->p_parent->p_base->pb_ino),
1491 &pno->p_base->pb_name);
1496 doio(ssize_t (*f)(void *, size_t, _SYSIO_OFF_T, struct incore_inode *),
1498 struct ioctx *ioctx)
1502 _sysio_doio(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
1503 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
1504 (ssize_t (*)(void *, size_t, _SYSIO_OFF_T, void *))f,
1506 if (ioctx->ioctx_cc < 0) {
1507 ioctx->ioctx_errno = -ioctx->ioctx_cc;
1508 ioctx->ioctx_cc = -1;
1510 ioctx->ioctx_done = 1;
1516 incore_read(void *buf, size_t nbytes,
1518 struct incore_inode *icino)
1524 if (!nbytes || off > icino->ici_st.st_size)
1526 n = icino->ici_st.st_size - (size_t )off;
1529 (void )memcpy(buf, (char *)icino->ici_data + off, (size_t )n);
1535 _sysio_incore_filop_read(struct inode *ino, struct ioctx *ioctx)
1539 return doio(incore_read, ino, ioctx);
1543 incore_write(const void *buf, size_t nbytes,
1545 struct incore_inode *icino)
1551 if (!nbytes || off > icino->ici_st.st_size)
1554 if (off && pos <= off) {
1556 * It's all or nothing. We won't write just part of
1561 if (pos > icino->ici_st.st_size) {
1564 err = incore_trunc(icino, (size_t )pos, 0);
1568 (void )memcpy((char *)icino->ici_data + off, buf, nbytes);
1570 return (ssize_t )nbytes;
1574 _sysio_incore_filop_write(struct inode *ino, struct ioctx *ioctx)
1577 return doio((ssize_t (*)(void *, size_t,
1579 struct incore_inode *))incore_write,
1585 _sysio_incore_filop_pos(struct inode *ino __IS_UNUSED, _SYSIO_OFF_T off)
1592 _sysio_incore_filop_iodone(struct ioctx *iocp __IS_UNUSED)
1596 * It's always done in this driver. It completed when posted.
1602 _sysio_incore_filop_fcntl(struct inode *ino __IS_UNUSED,
1603 int cmd __IS_UNUSED,
1604 va_list ap __IS_UNUSED,
1609 * No fcntl's supported.
1616 _sysio_incore_inop_sync(struct inode *ino __IS_UNUSED)
1626 _sysio_incore_filop_ioctl(struct inode *ino __IS_UNUSED,
1627 unsigned long int request __IS_UNUSED,
1628 va_list ap __IS_UNUSED)
1632 * No ioctl's supported.
1638 _sysio_incore_dirop_mknod(struct pnode *pno, mode_t mode, dev_t dev)
1641 struct intnl_stat stat;
1644 assert(!pno->p_base->pb_ino);
1649 else if (S_ISFIFO(m))
1651 else if (S_ISBLK(m))
1659 * Initialize attributes.
1661 (void )memset(&stat, 0, sizeof(stat));
1662 stat.st_dev = pno->p_parent->p_base->pb_ino->i_fs->fs_dev;
1663 inum = incore_inum_alloc();
1665 stat.__st_ino = inum;
1667 stat.st_mode = mode;
1669 stat.st_uid = getuid();
1670 stat.st_gid = getgid();
1673 stat.st_blksize = 4096;
1675 stat.st_ctime = stat.st_mtime = stat.st_atime = 0;
1678 return incore_create(pno, &stat);
1681 #ifdef _HAVE_STATVFS
1683 _sysio_incore_inop_statvfs(struct pnode *pno,
1685 struct intnl_statvfs *buf)
1690 ino = pno->p_base->pb_ino;
1693 fs = pno->p_base->pb_ino->i_fs;
1695 (void )memset(buf, 0, sizeof(struct intnl_statvfs));
1700 buf->f_bsize = fs->fs_bsize;
1701 buf->f_frsize = buf->f_bsize;
1703 buf->f_blocks /= buf->f_bsize;
1704 buf->f_bfree = buf->f_blocks - 1;
1705 buf->f_bavail = buf->f_bfree;
1706 buf->f_files = buf->f_blocks;
1707 buf->f_ffree = buf->f_files - 1;
1708 buf->f_favail = buf->f_ffree;
1709 buf->f_fsid = fs->fs_id;
1711 buf->f_namemax = ULONG_MAX;
1718 _sysio_incore_inop_gone(struct inode *ino)
1720 struct incore_inode *icino = I2IC(ino);
1722 incore_i_destroy(icino);