xref: /titanic_41/usr/src/uts/common/fs/gfs.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/cmn_err.h>
31 #include <sys/debug.h>
32 #include <sys/dirent.h>
33 #include <sys/kmem.h>
34 #include <sys/mman.h>
35 #include <sys/mutex.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/uio.h>
39 #include <sys/vmsystm.h>
40 #include <sys/vfs.h>
41 #include <sys/vnode.h>
42 
43 #include <vm/as.h>
44 #include <vm/seg_vn.h>
45 
46 #include <sys/gfs.h>
47 
48 /*
49  * Generic pseudo-filesystem routines.
50  *
51  * There are significant similarities between the implementation of certain file
52  * system entry points across different filesystems.  While one could attempt to
53  * "choke up on the bat" and incorporate common functionality into a VOP
54  * preamable or postamble, such an approach is limited in the benefit it can
55  * provide.  In this file we instead define a toolkit of routines which can be
56  * called from a filesystem (with in-kernel pseudo-filesystems being the focus
57  * of the exercise) in a more component-like fashion.
58  *
59  * There are three basic classes of routines:
60  *
61  * 1) Lowlevel support routines
62  *
63  *    These routines are designed to play a support role for existing
64  *    pseudo-filesystems (such as procfs).  They simplif ycommon tasks,
65  *    without enforcing the filesystem to hand over management to GFS.  The
66  *    routines covered are:
67  *
68  *	gfs_readdir_init()
69  *	gfs_readdir_emit()
70  *	gfs_readdir_emitn()
71  *	gfs_readdir_pred()
72  *	gfs_readdir_fini()
73  *	gfs_lookup_dot()
74  *
75  * 2) Complete GFS management
76  *
77  *    These routines take a more active role in management of the
78  *    pseudo-filesystem.  They handle the relationship between vnode private
79  *    data and VFS data, as well as the relationship between vnodes in the
80  *    directory heirarchy.
81  *
82  *    In order to use these interfaces, the first member of every private
83  *    v_data must be a gfs_file_t or a gfs_dir_t.  This hands over all control
84  *    to GFS.
85  *
86  * 	gfs_file_create()
87  * 	gfs_dir_create()
88  * 	gfs_root_create()
89  *
90  *	gfs_file_inactive()
91  *	gfs_dir_inactive()
92  *	gfs_dir_lookup()
93  *	gfs_dir_readdir()
94  *
95  * 	gfs_vop_inactive()
96  * 	gfs_vop_lookup()
97  * 	gfs_vop_readdir()
98  * 	gfs_vop_map()
99  */
100 
101 /*
102  * gfs_make_opsvec: take an array of vnode type definitions and create
103  * their vnodeops_t structures
104  *
105  * This routine takes an array of gfs_opsvec_t's.  It could
106  * alternatively take an array of gfs_opsvec_t*'s, which would allow
107  * vnode types to be completely defined in files external to the caller
108  * of gfs_make_opsvec().  As it stands, much more sharing takes place --
109  * both the caller and the vnode type provider need to access gfsv_ops
110  * and gfsv_template, and the caller also needs to know gfsv_name.
111  */
112 int
113 gfs_make_opsvec(gfs_opsvec_t *vec)
114 {
115 	int error, i;
116 
117 	for (i = 0; ; i++) {
118 		if (vec[i].gfsv_name == NULL)
119 			return (0);
120 		error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template,
121 		    vec[i].gfsv_ops);
122 		if (error)
123 			break;
124 	}
125 
126 	cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'",
127 	    vec[i].gfsv_name);
128 	for (i--; i >= 0; i--) {
129 		vn_freevnodeops(*vec[i].gfsv_ops);
130 		*vec[i].gfsv_ops = NULL;
131 	}
132 	return (error);
133 }
134 
135 /*
136  * Low level directory routines
137  *
138  * These routines provide some simple abstractions for reading directories.
139  * They are designed to be used by existing pseudo filesystems (namely procfs)
140  * that already have a complicated management infrastructure.
141  */
142 
143 /*
144  * gfs_readdir_init: initiate a generic readdir
145  *   st		- a pointer to an uninitialized gfs_readdir_state_t structure
146  *   name_max	- the directory's maximum file name length
147  *   ureclen	- the exported file-space record length (1 for non-legacy FSs)
148  *   uiop	- the uiop passed to readdir
149  *   parent	- the parent directory's inode
150  *   self	- this directory's inode
151  *
152  * Returns 0 or a non-zero errno.
153  *
154  * Typical VOP_READDIR usage of gfs_readdir_*:
155  *
156  *	if ((error = gfs_readdir_init(...)) != 0)
157  *		return (error);
158  *	eof = 0;
159  *	while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
160  *		if (!consumer_entry_at(voffset))
161  *			voffset = consumer_next_entry(voffset);
162  *		if (consumer_eof(voffset)) {
163  *			eof = 1
164  *			break;
165  *		}
166  *		if ((error = gfs_readdir_emit(..., voffset,
167  *		    consumer_ino(voffset), consumer_name(voffset))) != 0)
168  *			break;
169  *	}
170  *	return (gfs_readdir_fini(..., error, eofp, eof));
171  *
172  * As you can see, a zero result from gfs_readdir_pred() or
173  * gfs_readdir_emit() indicates that processing should continue,
174  * whereas a non-zero result indicates that the loop should terminate.
175  * Most consumers need do nothing more than let gfs_readdir_fini()
176  * determine what the cause of failure was and return the appropriate
177  * value.
178  */
179 int
180 gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen,
181     uio_t *uiop, ino64_t parent, ino64_t self)
182 {
183 	if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
184 	    (uiop->uio_loffset % ureclen) != 0)
185 		return (EINVAL);
186 
187 	st->grd_ureclen = ureclen;
188 	st->grd_oresid = uiop->uio_resid;
189 	st->grd_namlen = name_max;
190 	st->grd_dirent = kmem_zalloc(DIRENT64_RECLEN(st->grd_namlen), KM_SLEEP);
191 	st->grd_parent = parent;
192 	st->grd_self = self;
193 
194 	return (0);
195 }
196 
197 /*
198  * gfs_readdir_emit_int: internal routine to emit directory entry
199  *
200  *   st		- the current readdir state, which must have d_ino and d_name
201  *                set
202  *   uiop	- caller-supplied uio pointer
203  *   off	- the offset of the current entry
204  *   next	- the offset of the next entry
205  */
206 static int
207 gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t off,
208     offset_t next)
209 {
210 	int reclen;
211 
212 	reclen = DIRENT64_RECLEN(strlen(st->grd_dirent->d_name));
213 
214 	if (reclen > uiop->uio_resid) {
215 		/*
216 		 * Error if no entries were returned yet
217 		 */
218 		if (uiop->uio_resid == st->grd_oresid)
219 			return (EINVAL);
220 		return (-1);
221 	}
222 
223 	st->grd_dirent->d_off = off;
224 	st->grd_dirent->d_reclen = (ushort_t)reclen;
225 
226 	if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
227 		return (EFAULT);
228 
229 	uiop->uio_loffset = next;
230 
231 	return (0);
232 }
233 
234 /*
235  * gfs_readdir_emit: emit a directory entry
236  *   voff       - the virtual offset (obtained from gfs_readdir_pred)
237  *   ino        - the entry's inode
238  *   name       - the entry's name
239  *
240  * Returns a 0 on success, a non-zero errno on failure, or -1 if the
241  * readdir loop should terminate.  A non-zero result (either errno or
242  * -1) from this function is typically passed directly to
243  * gfs_readdir_fini().
244  */
245 int
246 gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
247     ino64_t ino, const char *name)
248 {
249 	offset_t off = (voff + 2) * st->grd_ureclen;
250 
251 	st->grd_dirent->d_ino = ino;
252 	(void) strncpy(st->grd_dirent->d_name, name, st->grd_namlen);
253 
254 	/*
255 	 * Inter-entry offsets are invalid, so we assume a record size of
256 	 * grd_ureclen and explicitly set the offset appropriately.
257 	 */
258 	return (gfs_readdir_emit_int(st, uiop, off, off + st->grd_ureclen));
259 }
260 
261 /*
262  * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
263  * instead of a string for the entry's name.
264  */
265 int
266 gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
267     ino64_t ino, unsigned long num)
268 {
269 	char buf[40];
270 
271 	numtos(num, buf);
272 	return (gfs_readdir_emit(st, uiop, voff, ino, buf));
273 }
274 
275 /*
276  * gfs_readdir_pred: readdir loop predicate
277  *   voffp - a pointer in which the next virtual offset should be stored
278  *
279  * Returns a 0 on success, a non-zero errno on failure, or -1 if the
280  * readdir loop should terminate.  A non-zero result (either errno or
281  * -1) from this function is typically passed directly to
282  * gfs_readdir_fini().
283  */
284 int
285 gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp)
286 {
287 	offset_t off, voff;
288 	int error;
289 
290 top:
291 	if (uiop->uio_resid <= 0)
292 		return (-1);
293 
294 	off = uiop->uio_loffset / st->grd_ureclen;
295 	voff = off - 2;
296 	if (off == 0) {
297 		if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
298 		    ".")) == 0)
299 			goto top;
300 	} else if (off == 1) {
301 		if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
302 		    "..")) == 0)
303 			goto top;
304 	} else {
305 		*voffp = voff;
306 		return (0);
307 	}
308 
309 	return (error);
310 }
311 
312 /*
313  * gfs_readdir_fini: generic readdir cleanup
314  *   error	- if positive, an error to return
315  *   eofp	- the eofp passed to readdir
316  *   eof	- the eof value
317  *
318  * Returns a 0 on success, a non-zero errno on failure.  This result
319  * should be returned from readdir.
320  */
321 int
322 gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
323 {
324 	kmem_free(st->grd_dirent, DIRENT64_RECLEN(st->grd_namlen));
325 	if (error > 0)
326 		return (error);
327 	if (eofp)
328 		*eofp = eof;
329 	return (0);
330 }
331 
332 /*
333  * gfs_lookup_dot
334  *
335  * Performs a basic check for "." and ".." directory entries.
336  */
337 int
338 gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
339 {
340 	if (*nm == '\0' || strcmp(nm, ".") == 0) {
341 		VN_HOLD(dvp);
342 		*vpp = dvp;
343 		return (0);
344 	} else if (strcmp(nm, "..") == 0) {
345 		if (pvp == NULL) {
346 			ASSERT(dvp->v_flag & VROOT);
347 			VN_HOLD(dvp);
348 			*vpp = dvp;
349 		} else {
350 			VN_HOLD(pvp);
351 			*vpp = pvp;
352 		}
353 		return (0);
354 	}
355 
356 	return (-1);
357 }
358 
359 /*
360  * gfs_file_create(): create a new GFS file
361  *
362  *   size	- size of private data structure (v_data)
363  *   pvp	- parent vnode (GFS directory)
364  *   ops	- vnode operations vector
365  *
366  * In order to use this interface, the parent vnode must have been created by
367  * gfs_dir_create(), and the private data stored in v_data must have a
368  * 'gfs_file_t' as its first field.
369  *
370  * Given these constraints, this routine will automatically:
371  *
372  * 	- Allocate v_data for the vnode
373  * 	- Initialize necessary fields in the vnode
374  * 	- Hold the parent
375  */
376 vnode_t *
377 gfs_file_create(size_t size, vnode_t *pvp, vnodeops_t *ops)
378 {
379 	gfs_file_t *fp;
380 	vnode_t *vp;
381 
382 	/*
383 	 * Allocate vnode and internal data structure
384 	 */
385 	fp = kmem_zalloc(size, KM_SLEEP);
386 	vp = vn_alloc(KM_SLEEP);
387 
388 	/*
389 	 * Set up various pointers
390 	 */
391 	fp->gfs_vnode = vp;
392 	fp->gfs_parent = pvp;
393 	vp->v_data = fp;
394 	fp->gfs_size = size;
395 	fp->gfs_type = GFS_FILE;
396 
397 	/*
398 	 * Initialize vnode and hold parent.
399 	 */
400 	vn_setops(vp, ops);
401 	if (pvp) {
402 		VN_SET_VFS_TYPE_DEV(vp, pvp->v_vfsp, VREG, 0);
403 		VN_HOLD(pvp);
404 	}
405 
406 	return (vp);
407 }
408 
409 /*
410  * gfs_dir_create: creates a new directory in the parent
411  *
412  *   size	- size of private data structure (v_data)
413  *   pvp	- parent vnode (GFS directory)
414  *   ops	- vnode operations vector
415  *   entries	- NULL-terminated list of static entries (if any)
416  *   maxlen	- maximum length of a directory entry
417  *   readdir_cb	- readdir callback (see gfs_dir_readdir)
418  *   inode_cb	- inode callback (see gfs_dir_readdir)
419  *   lookup_cb	- lookup callback (see gfs_dir_lookup)
420  *
421  * In order to use this function, the first member of the private vnode
422  * structure (v_data) must be a gfs_dir_t.  For each directory, there are
423  * static entries, defined when the structure is initialized, and dynamic
424  * entries, retrieved through callbacks.
425  *
426  * If a directory has static entries, then it must supply a inode callback,
427  * which will compute the inode number based on the parent and the index.
428  * For a directory with dynamic entries, the caller must supply a readdir
429  * callback and a lookup callback.  If a static lookup fails, we fall back to
430  * the supplied lookup callback, if any.
431  *
432  * This function also performs the same initialization as gfs_file_create().
433  */
434 vnode_t *
435 gfs_dir_create(size_t struct_size, vnode_t *pvp, vnodeops_t *ops,
436     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
437     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
438 {
439 	vnode_t *vp;
440 	gfs_dir_t *dp;
441 	gfs_dirent_t *de;
442 
443 	vp = gfs_file_create(struct_size, pvp, ops);
444 	vp->v_type = VDIR;
445 
446 	dp = vp->v_data;
447 	dp->gfsd_file.gfs_type = GFS_DIR;
448 	dp->gfsd_maxlen = maxlen;
449 
450 	if (entries != NULL) {
451 		for (de = entries; de->gfse_name != NULL; de++)
452 			dp->gfsd_nstatic++;
453 
454 		dp->gfsd_static = kmem_alloc(
455 		    dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
456 		bcopy(entries, dp->gfsd_static,
457 		    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
458 	}
459 
460 	dp->gfsd_readdir = readdir_cb;
461 	dp->gfsd_lookup = lookup_cb;
462 	dp->gfsd_inode = inode_cb;
463 
464 	mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
465 
466 	return (vp);
467 }
468 
469 /*
470  * gfs_root_create(): create a root vnode for a GFS filesystem
471  *
472  * Similar to gfs_dir_create(), this creates a root vnode for a filesystem.  The
473  * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
474  */
475 vnode_t *
476 gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
477     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
478     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
479 {
480 	vnode_t *vp = gfs_dir_create(size, NULL, ops, entries, inode_cb,
481 	    maxlen, readdir_cb, lookup_cb);
482 
483 	/* Manually set the inode */
484 	((gfs_file_t *)vp->v_data)->gfs_ino = ino;
485 
486 	VFS_HOLD(vfsp);
487 	VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0);
488 	vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
489 
490 	return (vp);
491 }
492 
493 /*
494  * gfs_file_inactive()
495  *
496  * Called from the VOP_INACTIVE() routine.  If necessary, this routine will
497  * remove the given vnode from the parent directory and clean up any references
498  * in the VFS layer.
499  *
500  * If the vnode was not removed (due to a race with vget), then NULL is
501  * returned.  Otherwise, a pointer to the private data is returned.
502  */
503 void *
504 gfs_file_inactive(vnode_t *vp)
505 {
506 	int i;
507 	gfs_dirent_t *ge = NULL;
508 	gfs_file_t *fp = vp->v_data;
509 	gfs_dir_t *dp = NULL;
510 	void *data;
511 
512 	if (fp->gfs_parent == NULL)
513 		goto found;
514 
515 	dp = fp->gfs_parent->v_data;
516 
517 	/*
518 	 * First, see if this vnode is cached in the parent.
519 	 */
520 	gfs_dir_lock(dp);
521 
522 	/*
523 	 * Find it in the set of static entries.
524 	 */
525 	for (i = 0; i < dp->gfsd_nstatic; i++)  {
526 		ge = &dp->gfsd_static[i];
527 
528 		if (ge->gfse_vnode == vp)
529 			goto found;
530 	}
531 
532 	/*
533 	 * If 'ge' is NULL, then it is a dynamic entry.
534 	 */
535 	ge = NULL;
536 
537 found:
538 	mutex_enter(&vp->v_lock);
539 	if (vp->v_count == 1) {
540 		/*
541 		 * Really remove this vnode
542 		 */
543 		data = vp->v_data;
544 		if (ge != NULL) {
545 			/*
546 			 * If this was a statically cached entry, simply set the
547 			 * cached vnode to NULL.
548 			 */
549 			ge->gfse_vnode = NULL;
550 		}
551 		mutex_exit(&vp->v_lock);
552 
553 		/*
554 		 * Free vnode and release parent
555 		 */
556 		if (fp->gfs_parent) {
557 			gfs_dir_unlock(dp);
558 			VN_RELE(fp->gfs_parent);
559 		} else {
560 			ASSERT(vp->v_vfsp != NULL);
561 			VFS_RELE(vp->v_vfsp);
562 		}
563 		vn_free(vp);
564 	} else {
565 		vp->v_count--;
566 		data = NULL;
567 		mutex_exit(&vp->v_lock);
568 		if (dp)
569 			gfs_dir_unlock(dp);
570 	}
571 
572 	return (data);
573 }
574 
575 /*
576  * gfs_dir_inactive()
577  *
578  * Same as above, but for directories.
579  */
580 void *
581 gfs_dir_inactive(vnode_t *vp)
582 {
583 	gfs_dir_t *dp;
584 
585 	ASSERT(vp->v_type == VDIR);
586 
587 	if ((dp = gfs_file_inactive(vp)) != NULL) {
588 		mutex_destroy(&dp->gfsd_lock);
589 		if (dp->gfsd_nstatic)
590 			kmem_free(dp->gfsd_static,
591 			    dp->gfsd_nstatic * sizeof (gfs_dirent_t));
592 	}
593 
594 	return (dp);
595 }
596 
597 /*
598  * gfs_dir_lookup()
599  *
600  * Looks up the given name in the directory and returns the corresponding vnode,
601  * if found.
602  *
603  * First, we search statically defined entries, if any.  If a match is found,
604  * and GFS_CACHE_VNODE is set and the vnode exists, we simply return the
605  * existing vnode.  Otherwise, we call the static entry's callback routine,
606  * caching the result if necessary.
607  *
608  * If no static entry is found, we invoke the lookup callback, if any.  The
609  * arguments to this callback are:
610  *
611  *	int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp);
612  *
613  *	pvp	- parent vnode
614  *	nm	- name of entry
615  *	vpp	- pointer to resulting vnode
616  *
617  * 	Returns 0 on success, non-zero on error.
618  */
619 int
620 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp)
621 {
622 	int i;
623 	gfs_dirent_t *ge;
624 	vnode_t *vp;
625 	gfs_dir_t *dp = dvp->v_data;
626 	int ret = 0;
627 
628 	ASSERT(dvp->v_type == VDIR);
629 
630 	if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
631 		return (0);
632 
633 	gfs_dir_lock(dp);
634 
635 	/*
636 	 * Search static entries.
637 	 */
638 	for (i = 0; i < dp->gfsd_nstatic; i++) {
639 		ge = &dp->gfsd_static[i];
640 
641 		if (strcmp(ge->gfse_name, nm) == 0) {
642 			if (ge->gfse_vnode) {
643 				ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
644 				vp = ge->gfse_vnode;
645 				VN_HOLD(vp);
646 				goto out;
647 			}
648 
649 			/*
650 			 * We drop the directory lock, as the constuctor will
651 			 * need to do KM_SLEEP allocations.  If we return from
652 			 * the constructor only to find that a parallel
653 			 * operation has completed, and GFS_CACHE_VNODE is set
654 			 * for this entry, we discard the result in favor of the
655 			 * cached vnode.
656 			 */
657 			gfs_dir_unlock(dp);
658 			vp = ge->gfse_ctor(dvp);
659 			gfs_dir_lock(dp);
660 
661 			((gfs_file_t *)vp->v_data)->gfs_index = i;
662 
663 			/* Set the inode according to the callback. */
664 			((gfs_file_t *)vp->v_data)->gfs_ino =
665 			    dp->gfsd_inode(dvp, i);
666 
667 			if (ge->gfse_flags & GFS_CACHE_VNODE) {
668 				if (ge->gfse_vnode == NULL) {
669 					ge->gfse_vnode = vp;
670 				} else {
671 					/*
672 					 * A parallel constructor beat us to it;
673 					 * return existing vnode.  We have to be
674 					 * careful because we can't release the
675 					 * current vnode while holding the
676 					 * directory lock; its inactive routine
677 					 * will try to lock this directory.
678 					 */
679 					vnode_t *oldvp = vp;
680 					vp = ge->gfse_vnode;
681 					VN_HOLD(vp);
682 
683 					gfs_dir_unlock(dp);
684 					VN_RELE(oldvp);
685 					gfs_dir_lock(dp);
686 				}
687 			}
688 
689 			goto out;
690 		}
691 	}
692 
693 	/*
694 	 * See if there is a dynamic constructor.
695 	 */
696 	if (dp->gfsd_lookup) {
697 		ino64_t ino;
698 		gfs_file_t *fp;
699 
700 		/*
701 		 * Once again, drop the directory lock, as the lookup routine
702 		 * will need to allocate memory, or otherwise deadlock on this
703 		 * directory.
704 		 */
705 		gfs_dir_unlock(dp);
706 		ret = dp->gfsd_lookup(dvp, nm, &vp, &ino);
707 		gfs_dir_lock(dp);
708 		if (ret != 0)
709 			goto out;
710 
711 		fp = (gfs_file_t *)vp->v_data;
712 		fp->gfs_index = -1;
713 		fp->gfs_ino = ino;
714 	} else {
715 		/*
716 		 * No static entry found, and there is no lookup callback, so
717 		 * return ENOENT.
718 		 */
719 		ret = ENOENT;
720 	}
721 
722 out:
723 	gfs_dir_unlock(dp);
724 
725 	*vpp = vp;
726 
727 	return (ret);
728 }
729 
730 /*
731  * gfs_dir_readdir: does a readdir() on the given directory
732  *
733  *    dvp	- directory vnode
734  *    uiop	- uio structure
735  *    eofp	- eof pointer
736  *    data	- arbitrary data passed to readdir callback
737  *
738  * This routine does all the readdir() dirty work.  Even so, the caller must
739  * supply two callbacks in order to get full compatibility.
740  *
741  * If the directory contains static entries, an inode callback must be
742  * specified.  This avoids having to create every vnode and call VOP_GETATTR()
743  * when reading the directory.  This function has the following arguments:
744  *
745  *	ino_t gfs_inode_cb(vnode_t *vp, int index);
746  *
747  * 	vp	- vnode for the directory
748  * 	index	- index in original gfs_dirent_t array
749  *
750  * 	Returns the inode number for the given entry.
751  *
752  * For directories with dynamic entries, a readdir callback must be provided.
753  * This is significantly more complex, thanks to the particulars of
754  * VOP_READDIR().
755  *
756  *	int gfs_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp,
757  *	    offset_t *off, offset_t *nextoff, void *data)
758  *
759  *	vp	- directory vnode
760  *	dp	- directory entry, sized according to maxlen given to
761  *		  gfs_dir_create().  callback must fill in d_name and
762  *		  d_ino.
763  *	eofp	- callback must set to 1 when EOF has been reached
764  *	off	- on entry, the last offset read from the directory.  Callback
765  *		  must set to the offset of the current entry, typically left
766  *		  untouched.
767  *	nextoff	- callback must set to offset of next entry.  Typically
768  *		  (off + 1)
769  *	data	- caller-supplied data
770  *
771  *	Return 0 on success, or error on failure.
772  */
773 int
774 gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, void *data)
775 {
776 	gfs_readdir_state_t gstate;
777 	int error, eof = 0;
778 	ino64_t ino, pino;
779 	offset_t off, next;
780 	gfs_dir_t *dp = dvp->v_data;
781 
782 	ino = dp->gfsd_file.gfs_ino;
783 
784 	if (dp->gfsd_file.gfs_parent == NULL)
785 		pino = ino;		/* root of filesystem */
786 	else
787 		pino = ((gfs_file_t *)
788 		    (dp->gfsd_file.gfs_parent->v_data))->gfs_ino;
789 
790 	if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
791 	    pino, ino)) != 0)
792 		return (error);
793 
794 	while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 &&
795 	    !eof) {
796 
797 		if (off >= 0 && off < dp->gfsd_nstatic) {
798 			ino = dp->gfsd_inode(dvp, off);
799 
800 			if ((error = gfs_readdir_emit(&gstate, uiop,
801 			    off, ino, dp->gfsd_static[off].gfse_name))
802 			    != 0)
803 				break;
804 
805 		} else if (dp->gfsd_readdir) {
806 			off -= dp->gfsd_nstatic;
807 
808 			if ((error = dp->gfsd_readdir(dvp,
809 			    gstate.grd_dirent, &eof, &off, &next,
810 			    data)) != 0 || eof)
811 				break;
812 
813 			off += dp->gfsd_nstatic + 2;
814 			next += dp->gfsd_nstatic + 2;
815 
816 			if ((error = gfs_readdir_emit_int(&gstate, uiop,
817 			    off, next)) != 0)
818 				break;
819 		} else {
820 			/*
821 			 * Offset is beyond the end of the static entries, and
822 			 * we have no dynamic entries.  Set EOF.
823 			 */
824 			eof = 1;
825 		}
826 	}
827 
828 	return (gfs_readdir_fini(&gstate, error, eofp, eof));
829 }
830 
831 
832 /*
833  * gfs_vop_lookup: VOP_LOOKUP() entry point
834  *
835  * For use directly in vnode ops table.  Given a GFS directory, calls
836  * gfs_dir_lookup() as necessary.
837  */
838 /* ARGSUSED */
839 int
840 gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
841     int flags, vnode_t *rdir, cred_t *cr)
842 {
843 	return (gfs_dir_lookup(dvp, nm, vpp));
844 }
845 
846 /*
847  * gfs_vop_readdir: VOP_READDIR() entry point
848  *
849  * For use directly in vnode ops table.  Given a GFS directory, calls
850  * gfs_dir_readdir() as necessary.
851  */
852 /* ARGSUSED */
853 int
854 gfs_vop_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp)
855 {
856 	return (gfs_dir_readdir(vp, uiop, eofp, NULL));
857 }
858 
859 
860 /*
861  * gfs_vop_map: VOP_MAP() entry point
862  *
863  * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
864  * This function only works for readonly files, and uses the read function for
865  * the vnode to fill in the data.  The mapped data is immediately faulted in and
866  * filled with the necessary data during this call; there are no getpage() or
867  * putpage() routines.
868  */
869 /* ARGSUSED */
870 int
871 gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
872     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred)
873 {
874 	int rv;
875 	ssize_t resid = len;
876 
877 	/*
878 	 * Check for bad parameters
879 	 */
880 #ifdef _ILP32
881 	if (len > MAXOFF_T)
882 		return (ENOMEM);
883 #endif
884 	if (vp->v_flag & VNOMAP)
885 		return (ENOTSUP);
886 	if (off > MAXOFF_T)
887 		return (EFBIG);
888 	if ((long)off < 0 || (long)(off + len) < 0)
889 		return (EINVAL);
890 	if (vp->v_type != VREG)
891 		return (ENODEV);
892 	if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
893 		return (EACCES);
894 
895 	/*
896 	 * Find appropriate address if needed, otherwise clear address range.
897 	 */
898 	as_rangelock(as);
899 	if ((flags & MAP_FIXED) == 0) {
900 		map_addr(addrp, len, (offset_t)off, 1, flags);
901 		if (*addrp == NULL) {
902 			as_rangeunlock(as);
903 			return (ENOMEM);
904 		}
905 	} else {
906 		(void) as_unmap(as, *addrp, len);
907 	}
908 
909 	/*
910 	 * Create mapping
911 	 */
912 	rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
913 	as_rangeunlock(as);
914 	if (rv != 0)
915 		return (rv);
916 
917 	/*
918 	 * Fill with data from read()
919 	 */
920 	rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
921 	    0, (rlim64_t)0, cred, &resid);
922 
923 	if (rv == 0 && resid != 0)
924 		rv = ENXIO;
925 
926 	if (rv != 0) {
927 		as_rangelock(as);
928 		(void) as_unmap(as, *addrp, len);
929 		as_rangeunlock(as);
930 	}
931 
932 	return (rv);
933 }
934 
935 /*
936  * gfs_vop_inactive: VOP_INACTIVE() entry point
937  *
938  * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
939  * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
940  */
941 /* ARGSUSED */
942 void
943 gfs_vop_inactive(vnode_t *vp, cred_t *cr)
944 {
945 	gfs_file_t *fp = vp->v_data;
946 	void *data;
947 
948 	if (fp->gfs_type == GFS_DIR)
949 		data = gfs_dir_inactive(vp);
950 	else
951 		data = gfs_file_inactive(vp);
952 
953 	if (data != NULL)
954 		kmem_free(data, fp->gfs_size);
955 }
956