4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 /* Portions Copyright 2007 Shivakumar GN */
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/cmn_err.h>
31 #include <sys/debug.h>
32 #include <sys/dirent.h>
35 #include <sys/mutex.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/sunddi.h>
41 #include <sys/vnode.h>
47 * Generic pseudo-filesystem routines.
49 * There are significant similarities between the implementation of certain file
50 * system entry points across different filesystems. While one could attempt to
51 * "choke up on the bat" and incorporate common functionality into a VOP
52 * preamble or postamble, such an approach is limited in the benefit it can
53 * provide. In this file we instead define a toolkit of routines which can be
54 * called from a filesystem (with in-kernel pseudo-filesystems being the focus
55 * of the exercise) in a more component-like fashion.
57 * There are three basic classes of routines:
59 * 1) Lowlevel support routines
61 * These routines are designed to play a support role for existing
62 * pseudo-filesystems (such as procfs). They simplify common tasks,
63 * without forcing the filesystem to hand over management to GFS. The
64 * routines covered are:
73 * 2) Complete GFS management
75 * These routines take a more active role in management of the
76 * pseudo-filesystem. They handle the relationship between vnode private
77 * data and VFS data, as well as the relationship between vnodes in the
78 * directory hierarchy.
80 * In order to use these interfaces, the first member of every private
81 * v_data must be a gfs_file_t or a gfs_dir_t. This hands over all control
98 * 3) Single File pseudo-filesystems
100 * This routine creates a rooted file to be overlayed ontop of another
101 * file in the physical filespace.
103 * Note that the parent is NULL (actually the vfs), but there is nothing
104 * technically keeping such a file from utilizing the "Complete GFS
105 * management" set of routines.
107 * gfs_root_create_file()
112 * gfs_make_opsvec: take an array of vnode type definitions and create
113 * their vnodeops_t structures
115 * This routine takes an array of gfs_opsvec_t's. It could
116 * alternatively take an array of gfs_opsvec_t*'s, which would allow
117 * vnode types to be completely defined in files external to the caller
118 * of gfs_make_opsvec(). As it stands, much more sharing takes place --
119 * both the caller and the vnode type provider need to access gfsv_ops
120 * and gfsv_template, and the caller also needs to know gfsv_name.
123 gfs_make_opsvec(gfs_opsvec_t *vec)
128 if (vec[i].gfsv_name == NULL)
130 error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template,
136 cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'",
138 for (i--; i >= 0; i--) {
139 vn_freevnodeops(*vec[i].gfsv_ops);
140 *vec[i].gfsv_ops = NULL;
147 * Low level directory routines
149 * These routines provide some simple abstractions for reading directories.
150 * They are designed to be used by existing pseudo filesystems (namely procfs)
151 * that already have a complicated management infrastructure.
155 * gfs_get_parent_ino: used to obtain a parent inode number and the
156 * inode number of the given vnode in preparation for calling gfs_readdir_init.
159 gfs_get_parent_ino(vnode_t *dvp, cred_t *cr, caller_context_t *ct,
160 ino64_t *pino, ino64_t *ino)
163 gfs_dir_t *dp = dvp->v_data;
166 *ino = dp->gfsd_file.gfs_ino;
167 parent = dp->gfsd_file.gfs_parent;
169 if (parent == NULL) {
170 *pino = *ino; /* root of filesystem */
171 } else if (dvp->v_flag & V_XATTRDIR) {
175 va.va_mask = AT_NODEID;
176 error = VOP_GETATTR(parent, &va, 0, cr, ct);
179 *pino = va.va_nodeid;
181 panic("%s:%u: not implemented", __func__, __LINE__);
184 *pino = ((gfs_file_t *)(parent->v_data))->gfs_ino;
191 * gfs_readdir_init: initiate a generic readdir
192 * st - a pointer to an uninitialized gfs_readdir_state_t structure
193 * name_max - the directory's maximum file name length
194 * ureclen - the exported file-space record length (1 for non-legacy FSs)
195 * uiop - the uiop passed to readdir
196 * parent - the parent directory's inode
197 * self - this directory's inode
198 * flags - flags from VOP_READDIR
200 * Returns 0 or a non-zero errno.
202 * Typical VOP_READDIR usage of gfs_readdir_*:
204 * if ((error = gfs_readdir_init(...)) != 0)
207 * while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
208 * if (!consumer_entry_at(voffset))
209 * voffset = consumer_next_entry(voffset);
210 * if (consumer_eof(voffset)) {
214 * if ((error = gfs_readdir_emit(..., voffset,
215 * consumer_ino(voffset), consumer_name(voffset))) != 0)
218 * return (gfs_readdir_fini(..., error, eofp, eof));
220 * As you can see, a zero result from gfs_readdir_pred() or
221 * gfs_readdir_emit() indicates that processing should continue,
222 * whereas a non-zero result indicates that the loop should terminate.
223 * Most consumers need do nothing more than let gfs_readdir_fini()
224 * determine what the cause of failure was and return the appropriate
228 gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen,
229 uio_t *uiop, ino64_t parent, ino64_t self, int flags)
233 if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
234 (uiop->uio_loffset % ureclen) != 0)
237 st->grd_ureclen = ureclen;
238 st->grd_oresid = uiop->uio_resid;
239 st->grd_namlen = name_max;
240 if (flags & V_RDDIR_ENTFLAGS)
241 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
243 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
244 st->grd_dirent = kmem_zalloc(dirent_size, KM_SLEEP);
245 st->grd_parent = parent;
247 st->grd_flags = flags;
253 * gfs_readdir_emit_int: internal routine to emit directory entry
255 * st - the current readdir state, which must have d_ino/ed_ino
256 * and d_name/ed_name set
257 * uiop - caller-supplied uio pointer
258 * next - the offset of the next entry
261 gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next,
262 int *ncookies, u_long **cookies)
268 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
269 edp = st->grd_dirent;
270 namlen = strlen(edp->ed_name);
271 reclen = EDIRENT_RECLEN(namlen);
274 namlen = strlen(dp->d_name);
275 reclen = DIRENT64_RECLEN(namlen);
278 if (reclen > uiop->uio_resid) {
280 * Error if no entries were returned yet
282 if (uiop->uio_resid == st->grd_oresid)
287 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
289 edp->ed_reclen = (ushort_t)reclen;
291 /* XXX: This can change in the future. */
292 dp->d_reclen = (ushort_t)reclen;
294 dp->d_namlen = namlen;
297 if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
300 uiop->uio_loffset = next;
301 if (*cookies != NULL) {
305 KASSERT(*ncookies >= 0, ("ncookies=%d", *ncookies));
312 * gfs_readdir_emit: emit a directory entry
313 * voff - the virtual offset (obtained from gfs_readdir_pred)
314 * ino - the entry's inode
315 * name - the entry's name
316 * eflags - value for ed_eflags (if processing edirent_t)
318 * Returns a 0 on success, a non-zero errno on failure, or -1 if the
319 * readdir loop should terminate. A non-zero result (either errno or
320 * -1) from this function is typically passed directly to
321 * gfs_readdir_fini().
324 gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
325 ino64_t ino, const char *name, int eflags, int *ncookies, u_long **cookies)
327 offset_t off = (voff + 2) * st->grd_ureclen;
329 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
330 edirent_t *edp = st->grd_dirent;
333 (void) strncpy(edp->ed_name, name, st->grd_namlen);
334 edp->ed_eflags = eflags;
336 dirent64_t *dp = st->grd_dirent;
339 (void) strncpy(dp->d_name, name, st->grd_namlen);
343 * Inter-entry offsets are invalid, so we assume a record size of
344 * grd_ureclen and explicitly set the offset appropriately.
346 return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen, ncookies,
352 * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
353 * instead of a string for the entry's name.
356 gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
357 ino64_t ino, unsigned long num)
362 return (gfs_readdir_emit(st, uiop, voff, ino, buf, 0));
367 * gfs_readdir_pred: readdir loop predicate
368 * voffp - a pointer in which the next virtual offset should be stored
370 * Returns a 0 on success, a non-zero errno on failure, or -1 if the
371 * readdir loop should terminate. A non-zero result (either errno or
372 * -1) from this function is typically passed directly to
373 * gfs_readdir_fini().
376 gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp,
377 int *ncookies, u_long **cookies)
383 if (uiop->uio_resid <= 0)
386 off = uiop->uio_loffset / st->grd_ureclen;
389 if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
390 ".", 0, ncookies, cookies)) == 0)
392 } else if (off == 1) {
393 if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
394 "..", 0, ncookies, cookies)) == 0)
405 * gfs_readdir_fini: generic readdir cleanup
406 * error - if positive, an error to return
407 * eofp - the eofp passed to readdir
408 * eof - the eof value
410 * Returns a 0 on success, a non-zero errno on failure. This result
411 * should be returned from readdir.
414 gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
418 if (st->grd_flags & V_RDDIR_ENTFLAGS)
419 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
421 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
422 kmem_free(st->grd_dirent, dirent_size);
433 * Performs a basic check for "." and ".." directory entries.
436 gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
438 if (*nm == '\0' || strcmp(nm, ".") == 0) {
442 } else if (strcmp(nm, "..") == 0) {
453 * gfs_file_create(): create a new GFS file
455 * size - size of private data structure (v_data)
456 * pvp - parent vnode (GFS directory)
457 * ops - vnode operations vector
459 * In order to use this interface, the parent vnode must have been created by
460 * gfs_dir_create(), and the private data stored in v_data must have a
461 * 'gfs_file_t' as its first field.
463 * Given these constraints, this routine will automatically:
465 * - Allocate v_data for the vnode
466 * - Initialize necessary fields in the vnode
470 gfs_file_create(size_t size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops)
477 * Allocate vnode and internal data structure
479 fp = kmem_zalloc(size, KM_SLEEP);
480 error = getnewvnode("zfs", vfsp, ops, &vp);
482 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
483 vp->v_data = (caddr_t)fp;
486 * Set up various pointers
489 fp->gfs_parent = pvp;
491 fp->gfs_type = GFS_FILE;
493 vp->v_vflag |= VV_FORCEINSMQ;
494 error = insmntque(vp, vfsp);
495 vp->v_vflag &= ~VV_FORCEINSMQ;
496 KASSERT(error == 0, ("insmntque() failed: error %d", error));
499 * Initialize vnode and hold parent.
508 * gfs_dir_create: creates a new directory in the parent
510 * size - size of private data structure (v_data)
511 * pvp - parent vnode (GFS directory)
512 * ops - vnode operations vector
513 * entries - NULL-terminated list of static entries (if any)
514 * maxlen - maximum length of a directory entry
515 * readdir_cb - readdir callback (see gfs_dir_readdir)
516 * inode_cb - inode callback (see gfs_dir_readdir)
517 * lookup_cb - lookup callback (see gfs_dir_lookup)
519 * In order to use this function, the first member of the private vnode
520 * structure (v_data) must be a gfs_dir_t. For each directory, there are
521 * static entries, defined when the structure is initialized, and dynamic
522 * entries, retrieved through callbacks.
524 * If a directory has static entries, then it must supply a inode callback,
525 * which will compute the inode number based on the parent and the index.
526 * For a directory with dynamic entries, the caller must supply a readdir
527 * callback and a lookup callback. If a static lookup fails, we fall back to
528 * the supplied lookup callback, if any.
530 * This function also performs the same initialization as gfs_file_create().
533 gfs_dir_create(size_t struct_size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops,
534 gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
535 gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
541 vp = gfs_file_create(struct_size, pvp, vfsp, ops);
545 dp->gfsd_file.gfs_type = GFS_DIR;
546 dp->gfsd_maxlen = maxlen;
548 if (entries != NULL) {
549 for (de = entries; de->gfse_name != NULL; de++)
552 dp->gfsd_static = kmem_alloc(
553 dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
554 bcopy(entries, dp->gfsd_static,
555 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
558 dp->gfsd_readdir = readdir_cb;
559 dp->gfsd_lookup = lookup_cb;
560 dp->gfsd_inode = inode_cb;
562 mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
568 * gfs_root_create(): create a root vnode for a GFS filesystem
570 * Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The
571 * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
574 gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
575 gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
576 gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
583 vp = gfs_dir_create(size, NULL, vfsp, ops, entries, inode_cb,
584 maxlen, readdir_cb, lookup_cb);
585 /* Manually set the inode */
586 ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
594 * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
596 * Similar to gfs_root_create(), this creates a root vnode for a file to
597 * be the pseudo-filesystem.
600 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
602 vnode_t *vp = gfs_file_create(size, NULL, ops);
604 ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
607 VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
608 vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
615 * gfs_file_inactive()
617 * Called from the VOP_RECLAIM() routine. If necessary, this routine will
618 * remove the given vnode from the parent directory and clean up any references
621 * If the vnode was not removed (due to a race with vget), then NULL is
622 * returned. Otherwise, a pointer to the private data is returned.
625 gfs_file_inactive(vnode_t *vp)
628 gfs_dirent_t *ge = NULL;
629 gfs_file_t *fp = vp->v_data;
630 gfs_dir_t *dp = NULL;
633 if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
637 * XXX cope with a FreeBSD-specific race wherein the parent's
638 * snapshot data can be freed before the parent is
640 if ((dp = fp->gfs_parent->v_data) == NULL)
644 * First, see if this vnode is cached in the parent.
649 * Find it in the set of static entries.
651 for (i = 0; i < dp->gfsd_nstatic; i++) {
652 ge = &dp->gfsd_static[i];
654 if (ge->gfse_vnode == vp)
659 * If 'ge' is NULL, then it is a dynamic entry.
665 if (vp->v_flag & V_XATTRDIR)
666 VI_LOCK(fp->gfs_parent);
670 * Really remove this vnode
675 * If this was a statically cached entry, simply set the
676 * cached vnode to NULL.
678 ge->gfse_vnode = NULL;
683 * Free vnode and release parent
685 if (fp->gfs_parent) {
689 VN_RELE(fp->gfs_parent);
690 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
692 ASSERT(vp->v_vfsp != NULL);
694 VFS_RELE(vp->v_vfsp);
698 if (vp->v_flag & V_XATTRDIR)
699 VI_UNLOCK(fp->gfs_parent);
707 * Same as above, but for directories.
710 gfs_dir_inactive(vnode_t *vp)
714 ASSERT(vp->v_type == VDIR);
716 if ((dp = gfs_file_inactive(vp)) != NULL) {
717 mutex_destroy(&dp->gfsd_lock);
718 if (dp->gfsd_nstatic)
719 kmem_free(dp->gfsd_static,
720 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
727 * gfs_dir_lookup_dynamic()
729 * This routine looks up the provided name amongst the dynamic entries
730 * in the gfs directory and returns the corresponding vnode, if found.
732 * The gfs directory is expected to be locked by the caller prior to
733 * calling this function. The directory will be unlocked during the
734 * execution of this function, but will be locked upon return from the
735 * function. This function returns 0 on success, non-zero on error.
737 * The dynamic lookups are performed by invoking the lookup
738 * callback, which is passed to this function as the first argument.
739 * The arguments to the callback are:
741 * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
742 * int flags, int *deflgs, pathname_t *rpnp);
746 * vpp - pointer to resulting vnode
747 * cr - pointer to cred
748 * flags - flags value from lookup request
749 * ignored here; currently only used to request
750 * insensitive lookups
751 * direntflgs - output parameter, directory entry flags
752 * ignored here; currently only used to indicate a lookup
753 * has more than one possible match when case is not considered
754 * realpnp - output parameter, real pathname
755 * ignored here; when lookup was performed case-insensitively,
756 * this field contains the "real" name of the file.
758 * Returns 0 on success, non-zero on error.
761 gfs_dir_lookup_dynamic(gfs_lookup_cb callback, gfs_dir_t *dp,
762 const char *nm, vnode_t *dvp, vnode_t **vpp, cred_t *cr, int flags,
763 int *direntflags, pathname_t *realpnp)
769 ASSERT(GFS_DIR_LOCKED(dp));
772 * Drop the directory lock, as the lookup routine
773 * will need to allocate memory, or otherwise deadlock on this
777 ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
781 * The callback for extended attributes returns a vnode
782 * with v_data from an underlying fs.
784 if (ret == 0 && !IS_XATTRDIR(dvp)) {
785 fp = (gfs_file_t *)((*vpp)->v_data);
794 * gfs_dir_lookup_static()
796 * This routine looks up the provided name amongst the static entries
797 * in the gfs directory and returns the corresponding vnode, if found.
798 * The first argument to the function is a pointer to the comparison
799 * function this function should use to decide if names are a match.
801 * If a match is found, and GFS_CACHE_VNODE is set and the vnode
802 * exists, we simply return the existing vnode. Otherwise, we call
803 * the static entry's callback routine, caching the result if
804 * necessary. If the idx pointer argument is non-NULL, we use it to
805 * return the index of the matching static entry.
807 * The gfs directory is expected to be locked by the caller prior to calling
808 * this function. The directory may be unlocked during the execution of
809 * this function, but will be locked upon return from the function.
811 * This function returns 0 if a match is found, ENOENT if not.
814 gfs_dir_lookup_static(int (*compare)(const char *, const char *),
815 gfs_dir_t *dp, const char *nm, vnode_t *dvp, int *idx,
816 vnode_t **vpp, pathname_t *rpnp)
822 ASSERT(GFS_DIR_LOCKED(dp));
825 * Search static entries.
827 for (i = 0; i < dp->gfsd_nstatic; i++) {
828 ge = &dp->gfsd_static[i];
830 if (compare(ge->gfse_name, nm) == 0) {
832 (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
835 if (ge->gfse_vnode) {
836 ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
843 * We drop the directory lock, as the constructor will
844 * need to do KM_SLEEP allocations. If we return from
845 * the constructor only to find that a parallel
846 * operation has completed, and GFS_CACHE_VNODE is set
847 * for this entry, we discard the result in favor of
851 vp = ge->gfse_ctor(dvp);
854 ((gfs_file_t *)vp->v_data)->gfs_index = i;
856 /* Set the inode according to the callback. */
857 ((gfs_file_t *)vp->v_data)->gfs_ino =
858 dp->gfsd_inode(dvp, i);
860 if (ge->gfse_flags & GFS_CACHE_VNODE) {
861 if (ge->gfse_vnode == NULL) {
865 * A parallel constructor beat us to it;
866 * return existing vnode. We have to be
867 * careful because we can't release the
868 * current vnode while holding the
869 * directory lock; its inactive routine
870 * will try to lock this directory.
896 * Looks up the given name in the directory and returns the corresponding
899 * First, we search statically defined entries, if any, with a call to
900 * gfs_dir_lookup_static(). If no static entry is found, and we have
901 * a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
903 * This function returns 0 on success, non-zero on error.
906 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
907 int flags, int *direntflags, pathname_t *realpnp)
909 gfs_dir_t *dp = dvp->v_data;
911 vnode_t *dynvp = NULL;
913 int (*compare)(const char *, const char *);
916 ASSERT(dvp->v_type == VDIR);
918 if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
921 casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
922 if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
923 (flags & FIGNORECASE))
924 compare = strcasecmp;
930 error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
932 if (vp && casecheck) {
936 for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
937 ge = &dp->gfsd_static[i];
939 if (strcasecmp(ge->gfse_name, nm) == 0) {
940 *direntflags |= ED_CASE_CONFLICT;
946 if ((error || casecheck) && dp->gfsd_lookup)
947 error = gfs_dir_lookup_dynamic(dp->gfsd_lookup, dp, nm, dvp,
948 &dynvp, cr, flags, direntflags, vp ? NULL : realpnp);
951 /* static and dynamic entries are case-insensitive conflict */
953 *direntflags |= ED_CASE_CONFLICT;
955 } else if (vp == NULL) {
957 } else if (error == ENOENT) {
972 * gfs_dir_readdir: does a readdir() on the given directory
974 * dvp - directory vnode
975 * uiop - uio structure
977 * data - arbitrary data passed to readdir callback
979 * This routine does all the readdir() dirty work. Even so, the caller must
980 * supply two callbacks in order to get full compatibility.
982 * If the directory contains static entries, an inode callback must be
983 * specified. This avoids having to create every vnode and call VOP_GETATTR()
984 * when reading the directory. This function has the following arguments:
986 * ino_t gfs_inode_cb(vnode_t *vp, int index);
988 * vp - vnode for the directory
989 * index - index in original gfs_dirent_t array
991 * Returns the inode number for the given entry.
993 * For directories with dynamic entries, a readdir callback must be provided.
994 * This is significantly more complex, thanks to the particulars of
997 * int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
998 * offset_t *off, offset_t *nextoff, void *data, int flags)
1000 * vp - directory vnode
1001 * dp - directory entry, sized according to maxlen given to
1002 * gfs_dir_create(). callback must fill in d_name and
1003 * d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
1004 * (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
1005 * is set in 'flags'.
1006 * eofp - callback must set to 1 when EOF has been reached
1007 * off - on entry, the last offset read from the directory. Callback
1008 * must set to the offset of the current entry, typically left
1010 * nextoff - callback must set to offset of next entry. Typically
1012 * data - caller-supplied data
1013 * flags - VOP_READDIR flags
1015 * Return 0 on success, or error on failure.
1018 gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, int *ncookies,
1019 u_long **cookies, void *data, cred_t *cr, int flags)
1021 gfs_readdir_state_t gstate;
1025 gfs_dir_t *dp = dvp->v_data;
1027 error = gfs_get_parent_ino(dvp, cr, NULL, &pino, &ino);
1031 if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
1032 pino, ino, flags)) != 0)
1035 while ((error = gfs_readdir_pred(&gstate, uiop, &off, ncookies,
1036 cookies)) == 0 && !eof) {
1038 if (off >= 0 && off < dp->gfsd_nstatic) {
1039 ino = dp->gfsd_inode(dvp, off);
1041 if ((error = gfs_readdir_emit(&gstate, uiop,
1042 off, ino, dp->gfsd_static[off].gfse_name, 0,
1043 ncookies, cookies)) != 0)
1046 } else if (dp->gfsd_readdir) {
1047 off -= dp->gfsd_nstatic;
1049 if ((error = dp->gfsd_readdir(dvp,
1050 gstate.grd_dirent, &eof, &off, &next,
1051 data, flags)) != 0 || eof)
1054 off += dp->gfsd_nstatic + 2;
1055 next += dp->gfsd_nstatic + 2;
1057 if ((error = gfs_readdir_emit_int(&gstate, uiop,
1058 next, ncookies, cookies)) != 0)
1062 * Offset is beyond the end of the static entries, and
1063 * we have no dynamic entries. Set EOF.
1069 return (gfs_readdir_fini(&gstate, error, eofp, eof));
1074 * gfs_vop_lookup: VOP_LOOKUP() entry point
1076 * For use directly in vnode ops table. Given a GFS directory, calls
1077 * gfs_dir_lookup() as necessary.
1081 gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
1082 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1083 int *direntflags, pathname_t *realpnp)
1085 return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
1089 * gfs_vop_readdir: VOP_READDIR() entry point
1091 * For use directly in vnode ops table. Given a GFS directory, calls
1092 * gfs_dir_readdir() as necessary.
1097 struct vop_readdir_args /* {
1100 struct ucred *a_cred;
1106 vnode_t *vp = ap->a_vp;
1107 uio_t *uiop = ap->a_uio;
1108 cred_t *cr = ap->a_cred;
1109 int *eofp = ap->a_eofflag;
1111 u_long *cookies = NULL;
1114 if (ap->a_ncookies) {
1116 * Minimum entry size is dirent size and 1 byte for a file name.
1118 ncookies = uiop->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
1119 cookies = malloc(ncookies * sizeof(u_long), M_TEMP, M_WAITOK);
1120 *ap->a_cookies = cookies;
1121 *ap->a_ncookies = ncookies;
1124 error = gfs_dir_readdir(vp, uiop, eofp, &ncookies, &cookies, NULL,
1128 /* Subtract unused cookies */
1130 *ap->a_ncookies -= ncookies;
1131 } else if (ap->a_ncookies) {
1132 free(*ap->a_cookies, M_TEMP);
1133 *ap->a_cookies = NULL;
1134 *ap->a_ncookies = 0;
1143 * gfs_vop_map: VOP_MAP() entry point
1145 * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
1146 * This function only works for readonly files, and uses the read function for
1147 * the vnode to fill in the data. The mapped data is immediately faulted in and
1148 * filled with the necessary data during this call; there are no getpage() or
1149 * putpage() routines.
1153 gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
1154 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred,
1155 caller_context_t *ct)
1158 ssize_t resid = len;
1161 * Check for bad parameters
1167 if (vp->v_flag & VNOMAP)
1171 if ((long)off < 0 || (long)(off + len) < 0)
1173 if (vp->v_type != VREG)
1175 if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
1179 * Find appropriate address if needed, otherwise clear address range.
1182 rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1191 rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
1197 * Fill with data from read()
1199 rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
1200 0, (rlim64_t)0, cred, &resid);
1202 if (rv == 0 && resid != 0)
1207 (void) as_unmap(as, *addrp, len);
1216 * gfs_vop_reclaim: VOP_RECLAIM() entry point (solaris' VOP_INACTIVE())
1218 * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
1219 * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
1224 struct vop_reclaim_args /* {
1226 struct thread *a_td;
1229 vnode_t *vp = ap->a_vp;
1230 gfs_file_t *fp = vp->v_data;
1232 if (fp->gfs_type == GFS_DIR)
1233 gfs_dir_inactive(vp);
1235 gfs_file_inactive(vp);
1237 vnode_destroy_vobject(vp);
1241 kmem_free(fp, fp->gfs_size);