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) {
444 ASSERT(dvp->v_flag & VROOT);
451 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
459 * gfs_file_create(): create a new GFS file
461 * size - size of private data structure (v_data)
462 * pvp - parent vnode (GFS directory)
463 * ops - vnode operations vector
465 * In order to use this interface, the parent vnode must have been created by
466 * gfs_dir_create(), and the private data stored in v_data must have a
467 * 'gfs_file_t' as its first field.
469 * Given these constraints, this routine will automatically:
471 * - Allocate v_data for the vnode
472 * - Initialize necessary fields in the vnode
476 gfs_file_create(size_t size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops)
483 * Allocate vnode and internal data structure
485 fp = kmem_zalloc(size, KM_SLEEP);
486 error = getnewvnode("zfs", vfsp, ops, &vp);
488 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
489 vp->v_data = (caddr_t)fp;
492 * Set up various pointers
495 fp->gfs_parent = pvp;
497 fp->gfs_type = GFS_FILE;
499 vp->v_vflag |= VV_FORCEINSMQ;
500 error = insmntque(vp, vfsp);
501 vp->v_vflag &= ~VV_FORCEINSMQ;
502 KASSERT(error == 0, ("insmntque() failed: error %d", error));
505 * Initialize vnode and hold parent.
514 * gfs_dir_create: creates a new directory in the parent
516 * size - size of private data structure (v_data)
517 * pvp - parent vnode (GFS directory)
518 * ops - vnode operations vector
519 * entries - NULL-terminated list of static entries (if any)
520 * maxlen - maximum length of a directory entry
521 * readdir_cb - readdir callback (see gfs_dir_readdir)
522 * inode_cb - inode callback (see gfs_dir_readdir)
523 * lookup_cb - lookup callback (see gfs_dir_lookup)
525 * In order to use this function, the first member of the private vnode
526 * structure (v_data) must be a gfs_dir_t. For each directory, there are
527 * static entries, defined when the structure is initialized, and dynamic
528 * entries, retrieved through callbacks.
530 * If a directory has static entries, then it must supply a inode callback,
531 * which will compute the inode number based on the parent and the index.
532 * For a directory with dynamic entries, the caller must supply a readdir
533 * callback and a lookup callback. If a static lookup fails, we fall back to
534 * the supplied lookup callback, if any.
536 * This function also performs the same initialization as gfs_file_create().
539 gfs_dir_create(size_t struct_size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops,
540 gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
541 gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
547 vp = gfs_file_create(struct_size, pvp, vfsp, ops);
551 dp->gfsd_file.gfs_type = GFS_DIR;
552 dp->gfsd_maxlen = maxlen;
554 if (entries != NULL) {
555 for (de = entries; de->gfse_name != NULL; de++)
558 dp->gfsd_static = kmem_alloc(
559 dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
560 bcopy(entries, dp->gfsd_static,
561 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
564 dp->gfsd_readdir = readdir_cb;
565 dp->gfsd_lookup = lookup_cb;
566 dp->gfsd_inode = inode_cb;
568 mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
574 * gfs_root_create(): create a root vnode for a GFS filesystem
576 * Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The
577 * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
580 gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
581 gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
582 gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
587 vp = gfs_dir_create(size, NULL, vfsp, ops, entries, inode_cb,
588 maxlen, readdir_cb, lookup_cb);
589 /* Manually set the inode */
590 ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
598 * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
600 * Similar to gfs_root_create(), this creates a root vnode for a file to
601 * be the pseudo-filesystem.
604 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
606 vnode_t *vp = gfs_file_create(size, NULL, ops);
608 ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
611 VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
612 vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
619 * gfs_file_inactive()
621 * Called from the VOP_INACTIVE() routine. If necessary, this routine will
622 * remove the given vnode from the parent directory and clean up any references
625 * If the vnode was not removed (due to a race with vget), then NULL is
626 * returned. Otherwise, a pointer to the private data is returned.
629 gfs_file_inactive(vnode_t *vp)
632 gfs_dirent_t *ge = NULL;
633 gfs_file_t *fp = vp->v_data;
634 gfs_dir_t *dp = NULL;
637 if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
641 * XXX cope with a FreeBSD-specific race wherein the parent's
642 * snapshot data can be freed before the parent is
644 if ((dp = fp->gfs_parent->v_data) == NULL)
648 * First, see if this vnode is cached in the parent.
653 * Find it in the set of static entries.
655 for (i = 0; i < dp->gfsd_nstatic; i++) {
656 ge = &dp->gfsd_static[i];
658 if (ge->gfse_vnode == vp)
663 * If 'ge' is NULL, then it is a dynamic entry.
669 if (vp->v_flag & V_XATTRDIR)
670 VI_LOCK(fp->gfs_parent);
674 * Really remove this vnode
679 * If this was a statically cached entry, simply set the
680 * cached vnode to NULL.
682 ge->gfse_vnode = NULL;
687 * Free vnode and release parent
689 if (fp->gfs_parent) {
693 VN_RELE(fp->gfs_parent);
694 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
696 ASSERT(vp->v_vfsp != NULL);
697 VFS_RELE(vp->v_vfsp);
700 if (vp->v_flag & V_XATTRDIR)
701 VI_UNLOCK(fp->gfs_parent);
709 * Same as above, but for directories.
712 gfs_dir_inactive(vnode_t *vp)
716 ASSERT(vp->v_type == VDIR);
718 if ((dp = gfs_file_inactive(vp)) != NULL) {
719 mutex_destroy(&dp->gfsd_lock);
720 if (dp->gfsd_nstatic)
721 kmem_free(dp->gfsd_static,
722 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
729 * gfs_dir_lookup_dynamic()
731 * This routine looks up the provided name amongst the dynamic entries
732 * in the gfs directory and returns the corresponding vnode, if found.
734 * The gfs directory is expected to be locked by the caller prior to
735 * calling this function. The directory will be unlocked during the
736 * execution of this function, but will be locked upon return from the
737 * function. This function returns 0 on success, non-zero on error.
739 * The dynamic lookups are performed by invoking the lookup
740 * callback, which is passed to this function as the first argument.
741 * The arguments to the callback are:
743 * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
744 * int flags, int *deflgs, pathname_t *rpnp);
748 * vpp - pointer to resulting vnode
749 * cr - pointer to cred
750 * flags - flags value from lookup request
751 * ignored here; currently only used to request
752 * insensitive lookups
753 * direntflgs - output parameter, directory entry flags
754 * ignored here; currently only used to indicate a lookup
755 * has more than one possible match when case is not considered
756 * realpnp - output parameter, real pathname
757 * ignored here; when lookup was performed case-insensitively,
758 * this field contains the "real" name of the file.
760 * Returns 0 on success, non-zero on error.
763 gfs_dir_lookup_dynamic(gfs_lookup_cb callback, gfs_dir_t *dp,
764 const char *nm, vnode_t *dvp, vnode_t **vpp, cred_t *cr, int flags,
765 int *direntflags, pathname_t *realpnp)
771 ASSERT(GFS_DIR_LOCKED(dp));
774 * Drop the directory lock, as the lookup routine
775 * will need to allocate memory, or otherwise deadlock on this
779 ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
783 * The callback for extended attributes returns a vnode
784 * with v_data from an underlying fs.
786 if (ret == 0 && !IS_XATTRDIR(dvp)) {
787 fp = (gfs_file_t *)((*vpp)->v_data);
796 * gfs_dir_lookup_static()
798 * This routine looks up the provided name amongst the static entries
799 * in the gfs directory and returns the corresponding vnode, if found.
800 * The first argument to the function is a pointer to the comparison
801 * function this function should use to decide if names are a match.
803 * If a match is found, and GFS_CACHE_VNODE is set and the vnode
804 * exists, we simply return the existing vnode. Otherwise, we call
805 * the static entry's callback routine, caching the result if
806 * necessary. If the idx pointer argument is non-NULL, we use it to
807 * return the index of the matching static entry.
809 * The gfs directory is expected to be locked by the caller prior to calling
810 * this function. The directory may be unlocked during the execution of
811 * this function, but will be locked upon return from the function.
813 * This function returns 0 if a match is found, ENOENT if not.
816 gfs_dir_lookup_static(int (*compare)(const char *, const char *),
817 gfs_dir_t *dp, const char *nm, vnode_t *dvp, int *idx,
818 vnode_t **vpp, pathname_t *rpnp)
824 ASSERT(GFS_DIR_LOCKED(dp));
827 * Search static entries.
829 for (i = 0; i < dp->gfsd_nstatic; i++) {
830 ge = &dp->gfsd_static[i];
832 if (compare(ge->gfse_name, nm) == 0) {
834 (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
837 if (ge->gfse_vnode) {
838 ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
845 * We drop the directory lock, as the constructor will
846 * need to do KM_SLEEP allocations. If we return from
847 * the constructor only to find that a parallel
848 * operation has completed, and GFS_CACHE_VNODE is set
849 * for this entry, we discard the result in favor of
853 vp = ge->gfse_ctor(dvp);
856 ((gfs_file_t *)vp->v_data)->gfs_index = i;
858 /* Set the inode according to the callback. */
859 ((gfs_file_t *)vp->v_data)->gfs_ino =
860 dp->gfsd_inode(dvp, i);
862 if (ge->gfse_flags & GFS_CACHE_VNODE) {
863 if (ge->gfse_vnode == NULL) {
867 * A parallel constructor beat us to it;
868 * return existing vnode. We have to be
869 * careful because we can't release the
870 * current vnode while holding the
871 * directory lock; its inactive routine
872 * will try to lock this directory.
898 * Looks up the given name in the directory and returns the corresponding
901 * First, we search statically defined entries, if any, with a call to
902 * gfs_dir_lookup_static(). If no static entry is found, and we have
903 * a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
905 * This function returns 0 on success, non-zero on error.
908 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
909 int flags, int *direntflags, pathname_t *realpnp)
911 gfs_dir_t *dp = dvp->v_data;
913 vnode_t *dynvp = NULL;
915 int (*compare)(const char *, const char *);
918 ASSERT(dvp->v_type == VDIR);
920 if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
923 casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
924 if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
925 (flags & FIGNORECASE))
926 compare = strcasecmp;
932 error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
934 if (vp && casecheck) {
938 for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
939 ge = &dp->gfsd_static[i];
941 if (strcasecmp(ge->gfse_name, nm) == 0) {
942 *direntflags |= ED_CASE_CONFLICT;
948 if ((error || casecheck) && dp->gfsd_lookup)
949 error = gfs_dir_lookup_dynamic(dp->gfsd_lookup, dp, nm, dvp,
950 &dynvp, cr, flags, direntflags, vp ? NULL : realpnp);
953 /* static and dynamic entries are case-insensitive conflict */
955 *direntflags |= ED_CASE_CONFLICT;
957 } else if (vp == NULL) {
959 } else if (error == ENOENT) {
974 * gfs_dir_readdir: does a readdir() on the given directory
976 * dvp - directory vnode
977 * uiop - uio structure
979 * data - arbitrary data passed to readdir callback
981 * This routine does all the readdir() dirty work. Even so, the caller must
982 * supply two callbacks in order to get full compatibility.
984 * If the directory contains static entries, an inode callback must be
985 * specified. This avoids having to create every vnode and call VOP_GETATTR()
986 * when reading the directory. This function has the following arguments:
988 * ino_t gfs_inode_cb(vnode_t *vp, int index);
990 * vp - vnode for the directory
991 * index - index in original gfs_dirent_t array
993 * Returns the inode number for the given entry.
995 * For directories with dynamic entries, a readdir callback must be provided.
996 * This is significantly more complex, thanks to the particulars of
999 * int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1000 * offset_t *off, offset_t *nextoff, void *data, int flags)
1002 * vp - directory vnode
1003 * dp - directory entry, sized according to maxlen given to
1004 * gfs_dir_create(). callback must fill in d_name and
1005 * d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
1006 * (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
1007 * is set in 'flags'.
1008 * eofp - callback must set to 1 when EOF has been reached
1009 * off - on entry, the last offset read from the directory. Callback
1010 * must set to the offset of the current entry, typically left
1012 * nextoff - callback must set to offset of next entry. Typically
1014 * data - caller-supplied data
1015 * flags - VOP_READDIR flags
1017 * Return 0 on success, or error on failure.
1020 gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, int *ncookies,
1021 u_long **cookies, void *data, cred_t *cr, int flags)
1023 gfs_readdir_state_t gstate;
1027 gfs_dir_t *dp = dvp->v_data;
1029 error = gfs_get_parent_ino(dvp, cr, NULL, &pino, &ino);
1033 if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
1034 pino, ino, flags)) != 0)
1037 while ((error = gfs_readdir_pred(&gstate, uiop, &off, ncookies,
1038 cookies)) == 0 && !eof) {
1040 if (off >= 0 && off < dp->gfsd_nstatic) {
1041 ino = dp->gfsd_inode(dvp, off);
1043 if ((error = gfs_readdir_emit(&gstate, uiop,
1044 off, ino, dp->gfsd_static[off].gfse_name, 0,
1045 ncookies, cookies)) != 0)
1048 } else if (dp->gfsd_readdir) {
1049 off -= dp->gfsd_nstatic;
1051 if ((error = dp->gfsd_readdir(dvp,
1052 gstate.grd_dirent, &eof, &off, &next,
1053 data, flags)) != 0 || eof)
1056 off += dp->gfsd_nstatic + 2;
1057 next += dp->gfsd_nstatic + 2;
1059 if ((error = gfs_readdir_emit_int(&gstate, uiop,
1060 next, ncookies, cookies)) != 0)
1064 * Offset is beyond the end of the static entries, and
1065 * we have no dynamic entries. Set EOF.
1071 return (gfs_readdir_fini(&gstate, error, eofp, eof));
1076 * gfs_vop_lookup: VOP_LOOKUP() entry point
1078 * For use directly in vnode ops table. Given a GFS directory, calls
1079 * gfs_dir_lookup() as necessary.
1083 gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
1084 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1085 int *direntflags, pathname_t *realpnp)
1087 return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
1091 * gfs_vop_readdir: VOP_READDIR() entry point
1093 * For use directly in vnode ops table. Given a GFS directory, calls
1094 * gfs_dir_readdir() as necessary.
1099 struct vop_readdir_args /* {
1102 struct ucred *a_cred;
1108 vnode_t *vp = ap->a_vp;
1109 uio_t *uiop = ap->a_uio;
1110 cred_t *cr = ap->a_cred;
1111 int *eofp = ap->a_eofflag;
1113 u_long *cookies = NULL;
1116 if (ap->a_ncookies) {
1118 * Minimum entry size is dirent size and 1 byte for a file name.
1120 ncookies = uiop->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
1121 cookies = malloc(ncookies * sizeof(u_long), M_TEMP, M_WAITOK);
1122 *ap->a_cookies = cookies;
1123 *ap->a_ncookies = ncookies;
1126 error = gfs_dir_readdir(vp, uiop, eofp, &ncookies, &cookies, NULL,
1130 /* Subtract unused cookies */
1132 *ap->a_ncookies -= ncookies;
1133 } else if (ap->a_ncookies) {
1134 free(*ap->a_cookies, M_TEMP);
1135 *ap->a_cookies = NULL;
1136 *ap->a_ncookies = 0;
1145 * gfs_vop_map: VOP_MAP() entry point
1147 * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
1148 * This function only works for readonly files, and uses the read function for
1149 * the vnode to fill in the data. The mapped data is immediately faulted in and
1150 * filled with the necessary data during this call; there are no getpage() or
1151 * putpage() routines.
1155 gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
1156 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred,
1157 caller_context_t *ct)
1160 ssize_t resid = len;
1163 * Check for bad parameters
1169 if (vp->v_flag & VNOMAP)
1173 if ((long)off < 0 || (long)(off + len) < 0)
1175 if (vp->v_type != VREG)
1177 if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
1181 * Find appropriate address if needed, otherwise clear address range.
1184 rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1193 rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
1199 * Fill with data from read()
1201 rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
1202 0, (rlim64_t)0, cred, &resid);
1204 if (rv == 0 && resid != 0)
1209 (void) as_unmap(as, *addrp, len);
1218 * gfs_vop_inactive: VOP_INACTIVE() entry point
1220 * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
1221 * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
1225 gfs_vop_inactive(ap)
1226 struct vop_inactive_args /* {
1228 struct thread *a_td;
1231 vnode_t *vp = ap->a_vp;
1232 gfs_file_t *fp = vp->v_data;
1234 if (fp->gfs_type == GFS_DIR)
1235 gfs_dir_inactive(vp);
1237 gfs_file_inactive(vp);
1242 kmem_free(fp, fp->gfs_size);