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>
48 * Generic pseudo-filesystem routines.
50 * There are significant similarities between the implementation of certain file
51 * system entry points across different filesystems. While one could attempt to
52 * "choke up on the bat" and incorporate common functionality into a VOP
53 * preamble or postamble, such an approach is limited in the benefit it can
54 * provide. In this file we instead define a toolkit of routines which can be
55 * called from a filesystem (with in-kernel pseudo-filesystems being the focus
56 * of the exercise) in a more component-like fashion.
58 * There are three basic classes of routines:
60 * 1) Lowlevel support routines
62 * These routines are designed to play a support role for existing
63 * pseudo-filesystems (such as procfs). They simplify common tasks,
64 * without forcing the filesystem to hand over management to GFS. The
65 * routines covered are:
74 * 2) Complete GFS management
76 * These routines take a more active role in management of the
77 * pseudo-filesystem. They handle the relationship between vnode private
78 * data and VFS data, as well as the relationship between vnodes in the
79 * directory hierarchy.
81 * In order to use these interfaces, the first member of every private
82 * v_data must be a gfs_file_t or a gfs_dir_t. This hands over all control
99 * 3) Single File pseudo-filesystems
101 * This routine creates a rooted file to be overlayed ontop of another
102 * file in the physical filespace.
104 * Note that the parent is NULL (actually the vfs), but there is nothing
105 * technically keeping such a file from utilizing the "Complete GFS
106 * management" set of routines.
108 * gfs_root_create_file()
112 * Low level directory routines
114 * These routines provide some simple abstractions for reading directories.
115 * They are designed to be used by existing pseudo filesystems (namely procfs)
116 * that already have a complicated management infrastructure.
120 * gfs_get_parent_ino: used to obtain a parent inode number and the
121 * inode number of the given vnode in preparation for calling gfs_readdir_init.
124 gfs_get_parent_ino(vnode_t *dvp, cred_t *cr, caller_context_t *ct,
125 ino64_t *pino, ino64_t *ino)
128 gfs_dir_t *dp = dvp->v_data;
131 *ino = dp->gfsd_file.gfs_ino;
132 parent = dp->gfsd_file.gfs_parent;
134 if (parent == NULL) {
135 *pino = *ino; /* root of filesystem */
136 } else if (dvp->v_flag & V_XATTRDIR) {
140 va.va_mask = AT_NODEID;
141 error = VOP_GETATTR(parent, &va, 0, cr, ct);
144 *pino = va.va_nodeid;
146 panic("%s:%u: not implemented", __func__, __LINE__);
149 *pino = ((gfs_file_t *)(parent->v_data))->gfs_ino;
156 * gfs_readdir_init: initiate a generic readdir
157 * st - a pointer to an uninitialized gfs_readdir_state_t structure
158 * name_max - the directory's maximum file name length
159 * ureclen - the exported file-space record length (1 for non-legacy FSs)
160 * uiop - the uiop passed to readdir
161 * parent - the parent directory's inode
162 * self - this directory's inode
163 * flags - flags from VOP_READDIR
165 * Returns 0 or a non-zero errno.
167 * Typical VOP_READDIR usage of gfs_readdir_*:
169 * if ((error = gfs_readdir_init(...)) != 0)
172 * while ((error = gfs_readdir_pred(..., &voffset)) != 0) {
173 * if (!consumer_entry_at(voffset))
174 * voffset = consumer_next_entry(voffset);
175 * if (consumer_eof(voffset)) {
179 * if ((error = gfs_readdir_emit(..., voffset,
180 * consumer_ino(voffset), consumer_name(voffset))) != 0)
183 * return (gfs_readdir_fini(..., error, eofp, eof));
185 * As you can see, a zero result from gfs_readdir_pred() or
186 * gfs_readdir_emit() indicates that processing should continue,
187 * whereas a non-zero result indicates that the loop should terminate.
188 * Most consumers need do nothing more than let gfs_readdir_fini()
189 * determine what the cause of failure was and return the appropriate
193 gfs_readdir_init(gfs_readdir_state_t *st, int name_max, int ureclen,
194 uio_t *uiop, ino64_t parent, ino64_t self, int flags)
198 if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
199 (uiop->uio_loffset % ureclen) != 0)
202 st->grd_ureclen = ureclen;
203 st->grd_oresid = uiop->uio_resid;
204 st->grd_namlen = name_max;
205 if (flags & V_RDDIR_ENTFLAGS)
206 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
208 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
209 st->grd_dirent = kmem_zalloc(dirent_size, KM_SLEEP);
210 st->grd_parent = parent;
212 st->grd_flags = flags;
218 * gfs_readdir_emit_int: internal routine to emit directory entry
220 * st - the current readdir state, which must have d_ino/ed_ino
221 * and d_name/ed_name set
222 * uiop - caller-supplied uio pointer
223 * next - the offset of the next entry
226 gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next,
227 int *ncookies, u_long **cookies)
233 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
234 edp = st->grd_dirent;
235 namlen = strlen(edp->ed_name);
236 reclen = EDIRENT_RECLEN(namlen);
239 namlen = strlen(dp->d_name);
240 reclen = DIRENT64_RECLEN(namlen);
243 if (reclen > uiop->uio_resid) {
245 * Error if no entries were returned yet
247 if (uiop->uio_resid == st->grd_oresid)
252 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
254 edp->ed_reclen = (ushort_t)reclen;
256 /* XXX: This can change in the future. */
257 dp->d_reclen = (ushort_t)reclen;
259 dp->d_namlen = namlen;
262 if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
265 uiop->uio_loffset = next;
266 if (*cookies != NULL) {
270 KASSERT(*ncookies >= 0, ("ncookies=%d", *ncookies));
277 * gfs_readdir_emit: emit a directory entry
278 * voff - the virtual offset (obtained from gfs_readdir_pred)
279 * ino - the entry's inode
280 * name - the entry's name
281 * eflags - value for ed_eflags (if processing edirent_t)
283 * Returns a 0 on success, a non-zero errno on failure, or -1 if the
284 * readdir loop should terminate. A non-zero result (either errno or
285 * -1) from this function is typically passed directly to
286 * gfs_readdir_fini().
289 gfs_readdir_emit(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
290 ino64_t ino, const char *name, int eflags, int *ncookies, u_long **cookies)
292 offset_t off = (voff + 2) * st->grd_ureclen;
294 if (st->grd_flags & V_RDDIR_ENTFLAGS) {
295 edirent_t *edp = st->grd_dirent;
298 (void) strncpy(edp->ed_name, name, st->grd_namlen);
299 edp->ed_eflags = eflags;
301 dirent64_t *dp = st->grd_dirent;
304 (void) strncpy(dp->d_name, name, st->grd_namlen);
308 * Inter-entry offsets are invalid, so we assume a record size of
309 * grd_ureclen and explicitly set the offset appropriately.
311 return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen, ncookies,
316 * gfs_readdir_pred: readdir loop predicate
317 * voffp - a pointer in which the next virtual offset should be stored
319 * Returns a 0 on success, a non-zero errno on failure, or -1 if the
320 * readdir loop should terminate. A non-zero result (either errno or
321 * -1) from this function is typically passed directly to
322 * gfs_readdir_fini().
325 gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp,
326 int *ncookies, u_long **cookies)
332 if (uiop->uio_resid <= 0)
335 off = uiop->uio_loffset / st->grd_ureclen;
338 if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
339 ".", 0, ncookies, cookies)) == 0)
341 } else if (off == 1) {
342 if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
343 "..", 0, ncookies, cookies)) == 0)
354 * gfs_readdir_fini: generic readdir cleanup
355 * error - if positive, an error to return
356 * eofp - the eofp passed to readdir
357 * eof - the eof value
359 * Returns a 0 on success, a non-zero errno on failure. This result
360 * should be returned from readdir.
363 gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
367 if (st->grd_flags & V_RDDIR_ENTFLAGS)
368 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
370 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
371 kmem_free(st->grd_dirent, dirent_size);
382 * Performs a basic check for "." and ".." directory entries.
385 gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
387 if (*nm == '\0' || strcmp(nm, ".") == 0) {
391 } else if (strcmp(nm, "..") == 0) {
393 ASSERT(dvp->v_flag & VROOT);
400 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
408 * gfs_file_create(): create a new GFS file
410 * size - size of private data structure (v_data)
411 * pvp - parent vnode (GFS directory)
412 * ops - vnode operations vector
414 * In order to use this interface, the parent vnode must have been created by
415 * gfs_dir_create(), and the private data stored in v_data must have a
416 * 'gfs_file_t' as its first field.
418 * Given these constraints, this routine will automatically:
420 * - Allocate v_data for the vnode
421 * - Initialize necessary fields in the vnode
425 gfs_file_create(size_t size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops)
432 * Allocate vnode and internal data structure
434 fp = kmem_zalloc(size, KM_SLEEP);
435 error = getnewvnode("zfs", vfsp, ops, &vp);
437 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
438 vp->v_data = (caddr_t)fp;
441 * Set up various pointers
444 fp->gfs_parent = pvp;
446 fp->gfs_type = GFS_FILE;
448 vp->v_vflag |= VV_FORCEINSMQ;
449 error = insmntque(vp, vfsp);
450 vp->v_vflag &= ~VV_FORCEINSMQ;
451 KASSERT(error == 0, ("insmntque() failed: error %d", error));
454 * Initialize vnode and hold parent.
463 * gfs_dir_create: creates a new directory in the parent
465 * size - size of private data structure (v_data)
466 * pvp - parent vnode (GFS directory)
467 * ops - vnode operations vector
468 * entries - NULL-terminated list of static entries (if any)
469 * maxlen - maximum length of a directory entry
470 * readdir_cb - readdir callback (see gfs_dir_readdir)
471 * inode_cb - inode callback (see gfs_dir_readdir)
472 * lookup_cb - lookup callback (see gfs_dir_lookup)
474 * In order to use this function, the first member of the private vnode
475 * structure (v_data) must be a gfs_dir_t. For each directory, there are
476 * static entries, defined when the structure is initialized, and dynamic
477 * entries, retrieved through callbacks.
479 * If a directory has static entries, then it must supply a inode callback,
480 * which will compute the inode number based on the parent and the index.
481 * For a directory with dynamic entries, the caller must supply a readdir
482 * callback and a lookup callback. If a static lookup fails, we fall back to
483 * the supplied lookup callback, if any.
485 * This function also performs the same initialization as gfs_file_create().
488 gfs_dir_create(size_t struct_size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops,
489 gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
490 gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
496 vp = gfs_file_create(struct_size, pvp, vfsp, ops);
500 dp->gfsd_file.gfs_type = GFS_DIR;
501 dp->gfsd_maxlen = maxlen;
503 if (entries != NULL) {
504 for (de = entries; de->gfse_name != NULL; de++)
507 dp->gfsd_static = kmem_alloc(
508 dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
509 bcopy(entries, dp->gfsd_static,
510 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
513 dp->gfsd_readdir = readdir_cb;
514 dp->gfsd_lookup = lookup_cb;
515 dp->gfsd_inode = inode_cb;
517 mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
523 * gfs_root_create(): create a root vnode for a GFS filesystem
525 * Similar to gfs_dir_create(), this creates a root vnode for a filesystem. The
526 * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
529 gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
530 gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
531 gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
536 vp = gfs_dir_create(size, NULL, vfsp, ops, entries, inode_cb,
537 maxlen, readdir_cb, lookup_cb);
538 /* Manually set the inode */
539 ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
546 * gfs_file_inactive()
548 * Called from the VOP_INACTIVE() routine. If necessary, this routine will
549 * remove the given vnode from the parent directory and clean up any references
552 * If the vnode was not removed (due to a race with vget), then NULL is
553 * returned. Otherwise, a pointer to the private data is returned.
556 gfs_file_inactive(vnode_t *vp)
559 gfs_dirent_t *ge = NULL;
560 gfs_file_t *fp = vp->v_data;
561 gfs_dir_t *dp = NULL;
564 if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
568 * XXX cope with a FreeBSD-specific race wherein the parent's
569 * snapshot data can be freed before the parent is
571 if ((dp = fp->gfs_parent->v_data) == NULL)
575 * First, see if this vnode is cached in the parent.
580 * Find it in the set of static entries.
582 for (i = 0; i < dp->gfsd_nstatic; i++) {
583 ge = &dp->gfsd_static[i];
585 if (ge->gfse_vnode == vp)
590 * If 'ge' is NULL, then it is a dynamic entry.
595 if (vp->v_flag & V_XATTRDIR)
596 VI_LOCK(fp->gfs_parent);
598 ASSERT(vp->v_count < 2);
600 * Really remove this vnode
605 * If this was a statically cached entry, simply set the
606 * cached vnode to NULL.
608 ge->gfse_vnode = NULL;
610 if (vp->v_count == 1) {
618 * Free vnode and release parent
620 if (fp->gfs_parent) {
623 VI_LOCK(fp->gfs_parent);
624 fp->gfs_parent->v_usecount--;
625 VI_UNLOCK(fp->gfs_parent);
627 ASSERT(vp->v_vfsp != NULL);
628 VFS_RELE(vp->v_vfsp);
630 if (vp->v_flag & V_XATTRDIR)
631 VI_UNLOCK(fp->gfs_parent);
639 * Same as above, but for directories.
642 gfs_dir_inactive(vnode_t *vp)
646 ASSERT(vp->v_type == VDIR);
648 if ((dp = gfs_file_inactive(vp)) != NULL) {
649 mutex_destroy(&dp->gfsd_lock);
650 if (dp->gfsd_nstatic)
651 kmem_free(dp->gfsd_static,
652 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
659 * gfs_dir_lookup_dynamic()
661 * This routine looks up the provided name amongst the dynamic entries
662 * in the gfs directory and returns the corresponding vnode, if found.
664 * The gfs directory is expected to be locked by the caller prior to
665 * calling this function. The directory will be unlocked during the
666 * execution of this function, but will be locked upon return from the
667 * function. This function returns 0 on success, non-zero on error.
669 * The dynamic lookups are performed by invoking the lookup
670 * callback, which is passed to this function as the first argument.
671 * The arguments to the callback are:
673 * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
674 * int flags, int *deflgs, pathname_t *rpnp);
678 * vpp - pointer to resulting vnode
679 * cr - pointer to cred
680 * flags - flags value from lookup request
681 * ignored here; currently only used to request
682 * insensitive lookups
683 * direntflgs - output parameter, directory entry flags
684 * ignored here; currently only used to indicate a lookup
685 * has more than one possible match when case is not considered
686 * realpnp - output parameter, real pathname
687 * ignored here; when lookup was performed case-insensitively,
688 * this field contains the "real" name of the file.
690 * Returns 0 on success, non-zero on error.
693 gfs_dir_lookup_dynamic(gfs_lookup_cb callback, gfs_dir_t *dp,
694 const char *nm, vnode_t *dvp, vnode_t **vpp, cred_t *cr, int flags,
695 int *direntflags, pathname_t *realpnp)
701 ASSERT(GFS_DIR_LOCKED(dp));
704 * Drop the directory lock, as the lookup routine
705 * will need to allocate memory, or otherwise deadlock on this
709 ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
713 * The callback for extended attributes returns a vnode
714 * with v_data from an underlying fs.
716 if (ret == 0 && !IS_XATTRDIR(dvp)) {
717 fp = (gfs_file_t *)((*vpp)->v_data);
726 * gfs_dir_lookup_static()
728 * This routine looks up the provided name amongst the static entries
729 * in the gfs directory and returns the corresponding vnode, if found.
730 * The first argument to the function is a pointer to the comparison
731 * function this function should use to decide if names are a match.
733 * If a match is found, and GFS_CACHE_VNODE is set and the vnode
734 * exists, we simply return the existing vnode. Otherwise, we call
735 * the static entry's callback routine, caching the result if
736 * necessary. If the idx pointer argument is non-NULL, we use it to
737 * return the index of the matching static entry.
739 * The gfs directory is expected to be locked by the caller prior to calling
740 * this function. The directory may be unlocked during the execution of
741 * this function, but will be locked upon return from the function.
743 * This function returns 0 if a match is found, ENOENT if not.
746 gfs_dir_lookup_static(int (*compare)(const char *, const char *),
747 gfs_dir_t *dp, const char *nm, vnode_t *dvp, int *idx,
748 vnode_t **vpp, pathname_t *rpnp)
754 ASSERT(GFS_DIR_LOCKED(dp));
757 * Search static entries.
759 for (i = 0; i < dp->gfsd_nstatic; i++) {
760 ge = &dp->gfsd_static[i];
762 if (compare(ge->gfse_name, nm) == 0) {
764 (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
767 if (ge->gfse_vnode) {
768 ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
775 * We drop the directory lock, as the constructor will
776 * need to do KM_SLEEP allocations. If we return from
777 * the constructor only to find that a parallel
778 * operation has completed, and GFS_CACHE_VNODE is set
779 * for this entry, we discard the result in favor of
783 vp = ge->gfse_ctor(dvp);
786 ((gfs_file_t *)vp->v_data)->gfs_index = i;
788 /* Set the inode according to the callback. */
789 ((gfs_file_t *)vp->v_data)->gfs_ino =
790 dp->gfsd_inode(dvp, i);
792 if (ge->gfse_flags & GFS_CACHE_VNODE) {
793 if (ge->gfse_vnode == NULL) {
797 * A parallel constructor beat us to it;
798 * return existing vnode. We have to be
799 * careful because we can't release the
800 * current vnode while holding the
801 * directory lock; its inactive routine
802 * will try to lock this directory.
828 * Looks up the given name in the directory and returns the corresponding
831 * First, we search statically defined entries, if any, with a call to
832 * gfs_dir_lookup_static(). If no static entry is found, and we have
833 * a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
835 * This function returns 0 on success, non-zero on error.
838 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
839 int flags, int *direntflags, pathname_t *realpnp)
841 gfs_dir_t *dp = dvp->v_data;
843 vnode_t *dynvp = NULL;
845 int (*compare)(const char *, const char *);
848 ASSERT(dvp->v_type == VDIR);
850 if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
853 casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
854 if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
855 (flags & FIGNORECASE))
856 compare = strcasecmp;
862 error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
864 if (vp && casecheck) {
868 for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
869 ge = &dp->gfsd_static[i];
871 if (strcasecmp(ge->gfse_name, nm) == 0) {
872 *direntflags |= ED_CASE_CONFLICT;
878 if ((error || casecheck) && dp->gfsd_lookup)
879 error = gfs_dir_lookup_dynamic(dp->gfsd_lookup, dp, nm, dvp,
880 &dynvp, cr, flags, direntflags, vp ? NULL : realpnp);
883 /* static and dynamic entries are case-insensitive conflict */
885 *direntflags |= ED_CASE_CONFLICT;
887 } else if (vp == NULL) {
889 } else if (error == ENOENT) {
904 * gfs_dir_readdir: does a readdir() on the given directory
906 * dvp - directory vnode
907 * uiop - uio structure
909 * data - arbitrary data passed to readdir callback
911 * This routine does all the readdir() dirty work. Even so, the caller must
912 * supply two callbacks in order to get full compatibility.
914 * If the directory contains static entries, an inode callback must be
915 * specified. This avoids having to create every vnode and call VOP_GETATTR()
916 * when reading the directory. This function has the following arguments:
918 * ino_t gfs_inode_cb(vnode_t *vp, int index);
920 * vp - vnode for the directory
921 * index - index in original gfs_dirent_t array
923 * Returns the inode number for the given entry.
925 * For directories with dynamic entries, a readdir callback must be provided.
926 * This is significantly more complex, thanks to the particulars of
929 * int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
930 * offset_t *off, offset_t *nextoff, void *data, int flags)
932 * vp - directory vnode
933 * dp - directory entry, sized according to maxlen given to
934 * gfs_dir_create(). callback must fill in d_name and
935 * d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
936 * (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
938 * eofp - callback must set to 1 when EOF has been reached
939 * off - on entry, the last offset read from the directory. Callback
940 * must set to the offset of the current entry, typically left
942 * nextoff - callback must set to offset of next entry. Typically
944 * data - caller-supplied data
945 * flags - VOP_READDIR flags
947 * Return 0 on success, or error on failure.
950 gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, int *ncookies,
951 u_long **cookies, void *data, cred_t *cr, int flags)
953 gfs_readdir_state_t gstate;
957 gfs_dir_t *dp = dvp->v_data;
959 error = gfs_get_parent_ino(dvp, cr, NULL, &pino, &ino);
963 if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
964 pino, ino, flags)) != 0)
967 while ((error = gfs_readdir_pred(&gstate, uiop, &off, ncookies,
968 cookies)) == 0 && !eof) {
970 if (off >= 0 && off < dp->gfsd_nstatic) {
971 ino = dp->gfsd_inode(dvp, off);
973 if ((error = gfs_readdir_emit(&gstate, uiop,
974 off, ino, dp->gfsd_static[off].gfse_name, 0,
975 ncookies, cookies)) != 0)
978 } else if (dp->gfsd_readdir) {
979 off -= dp->gfsd_nstatic;
981 if ((error = dp->gfsd_readdir(dvp,
982 gstate.grd_dirent, &eof, &off, &next,
983 data, flags)) != 0 || eof)
986 off += dp->gfsd_nstatic + 2;
987 next += dp->gfsd_nstatic + 2;
989 if ((error = gfs_readdir_emit_int(&gstate, uiop,
990 next, ncookies, cookies)) != 0)
994 * Offset is beyond the end of the static entries, and
995 * we have no dynamic entries. Set EOF.
1001 return (gfs_readdir_fini(&gstate, error, eofp, eof));
1005 * gfs_vop_lookup: VOP_LOOKUP() entry point
1007 * For use directly in vnode ops table. Given a GFS directory, calls
1008 * gfs_dir_lookup() as necessary.
1012 gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
1013 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1014 int *direntflags, pathname_t *realpnp)
1016 return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
1020 * gfs_vop_readdir: VOP_READDIR() entry point
1022 * For use directly in vnode ops table. Given a GFS directory, calls
1023 * gfs_dir_readdir() as necessary.
1028 struct vop_readdir_args /* {
1031 struct ucred *a_cred;
1037 vnode_t *vp = ap->a_vp;
1038 uio_t *uiop = ap->a_uio;
1039 cred_t *cr = ap->a_cred;
1040 int *eofp = ap->a_eofflag;
1042 u_long *cookies = NULL;
1045 if (ap->a_ncookies) {
1047 * Minimum entry size is dirent size and 1 byte for a file name.
1049 ncookies = uiop->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
1050 cookies = malloc(ncookies * sizeof(u_long), M_TEMP, M_WAITOK);
1051 *ap->a_cookies = cookies;
1052 *ap->a_ncookies = ncookies;
1055 error = gfs_dir_readdir(vp, uiop, eofp, &ncookies, &cookies, NULL,
1059 /* Subtract unused cookies */
1061 *ap->a_ncookies -= ncookies;
1062 } else if (ap->a_ncookies) {
1063 free(*ap->a_cookies, M_TEMP);
1064 *ap->a_cookies = NULL;
1065 *ap->a_ncookies = 0;
1072 * gfs_vop_inactive: VOP_INACTIVE() entry point
1074 * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
1075 * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
1079 gfs_vop_inactive(ap)
1080 struct vop_inactive_args /* {
1082 struct thread *a_td;
1085 vnode_t *vp = ap->a_vp;
1086 gfs_file_t *fp = vp->v_data;
1089 if (fp->gfs_type == GFS_DIR)
1090 data = gfs_dir_inactive(vp);
1092 data = gfs_file_inactive(vp);
1095 kmem_free(data, fp->gfs_size);