]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/gfs.c
MFC r363988:
[FreeBSD/stable/9.git] / sys / cddl / contrib / opensolaris / uts / common / fs / gfs.c
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /* Portions Copyright 2007 Shivakumar GN */
22 /*
23  * Copyright 2008 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/sunddi.h>
39 #include <sys/uio.h>
40 #include <sys/vfs.h>
41 #include <sys/vnode.h>
42 #include <sys/cred.h>
43
44 #include <sys/gfs.h>
45
46 /*
47  * Generic pseudo-filesystem routines.
48  *
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.
56  *
57  * There are three basic classes of routines:
58  *
59  * 1) Lowlevel support routines
60  *
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:
65  *
66  *      gfs_readdir_init()
67  *      gfs_readdir_emit()
68  *      gfs_readdir_emitn()
69  *      gfs_readdir_pred()
70  *      gfs_readdir_fini()
71  *      gfs_lookup_dot()
72  *
73  * 2) Complete GFS management
74  *
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.
79  *
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
82  *    to GFS.
83  *
84  *      gfs_file_create()
85  *      gfs_dir_create()
86  *      gfs_root_create()
87  *
88  *      gfs_file_inactive()
89  *      gfs_dir_inactive()
90  *      gfs_dir_lookup()
91  *      gfs_dir_readdir()
92  *
93  *      gfs_vop_reclaim()
94  *      gfs_vop_lookup()
95  *      gfs_vop_readdir()
96  *      gfs_vop_map()
97  *
98  * 3) Single File pseudo-filesystems
99  *
100  *    This routine creates a rooted file to be overlayed ontop of another
101  *    file in the physical filespace.
102  *
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.
106  *
107  *      gfs_root_create_file()
108  */
109
110 #ifdef sun
111 /*
112  * gfs_make_opsvec: take an array of vnode type definitions and create
113  * their vnodeops_t structures
114  *
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.
121  */
122 int
123 gfs_make_opsvec(gfs_opsvec_t *vec)
124 {
125         int error, i;
126
127         for (i = 0; ; i++) {
128                 if (vec[i].gfsv_name == NULL)
129                         return (0);
130                 error = vn_make_ops(vec[i].gfsv_name, vec[i].gfsv_template,
131                     vec[i].gfsv_ops);
132                 if (error)
133                         break;
134         }
135
136         cmn_err(CE_WARN, "gfs_make_opsvec: bad vnode ops template for '%s'",
137             vec[i].gfsv_name);
138         for (i--; i >= 0; i--) {
139                 vn_freevnodeops(*vec[i].gfsv_ops);
140                 *vec[i].gfsv_ops = NULL;
141         }
142         return (error);
143 }
144 #endif  /* sun */
145
146 /*
147  * Low level directory routines
148  *
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.
152  */
153
154 /*
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.
157  */
158 int
159 gfs_get_parent_ino(vnode_t *dvp, cred_t *cr, caller_context_t *ct,
160     ino64_t *pino, ino64_t *ino)
161 {
162         vnode_t *parent;
163         gfs_dir_t *dp = dvp->v_data;
164         int error;
165
166         *ino = dp->gfsd_file.gfs_ino;
167         parent = dp->gfsd_file.gfs_parent;
168
169         if (parent == NULL) {
170                 *pino = *ino;           /* root of filesystem */
171         } else if (dvp->v_flag & V_XATTRDIR) {
172 #ifdef TODO
173                 vattr_t va;
174
175                 va.va_mask = AT_NODEID;
176                 error = VOP_GETATTR(parent, &va, 0, cr, ct);
177                 if (error)
178                         return (error);
179                 *pino = va.va_nodeid;
180 #else
181                 panic("%s:%u: not implemented", __func__, __LINE__);
182 #endif
183         } else {
184                 *pino = ((gfs_file_t *)(parent->v_data))->gfs_ino;
185         }
186
187         return (0);
188 }
189
190 /*
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
199  *
200  * Returns 0 or a non-zero errno.
201  *
202  * Typical VOP_READDIR usage of gfs_readdir_*:
203  *
204  *      if ((error = gfs_readdir_init(...)) != 0)
205  *              return (error);
206  *      eof = 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)) {
211  *                      eof = 1
212  *                      break;
213  *              }
214  *              if ((error = gfs_readdir_emit(..., voffset,
215  *                  consumer_ino(voffset), consumer_name(voffset))) != 0)
216  *                      break;
217  *      }
218  *      return (gfs_readdir_fini(..., error, eofp, eof));
219  *
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
225  * value.
226  */
227 int
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)
230 {
231         size_t dirent_size;
232
233         if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0 ||
234             (uiop->uio_loffset % ureclen) != 0)
235                 return (EINVAL);
236
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);
242         else
243                 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
244         st->grd_dirent = kmem_zalloc(dirent_size, KM_SLEEP);
245         st->grd_parent = parent;
246         st->grd_self = self;
247         st->grd_flags = flags;
248
249         return (0);
250 }
251
252 /*
253  * gfs_readdir_emit_int: internal routine to emit directory entry
254  *
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
259  */
260 static int
261 gfs_readdir_emit_int(gfs_readdir_state_t *st, uio_t *uiop, offset_t next,
262     int *ncookies, u_long **cookies)
263 {
264         int reclen, namlen;
265         dirent64_t *dp;
266         edirent_t *edp;
267
268         if (st->grd_flags & V_RDDIR_ENTFLAGS) {
269                 edp = st->grd_dirent;
270                 namlen = strlen(edp->ed_name);
271                 reclen = EDIRENT_RECLEN(namlen);
272         } else {
273                 dp = st->grd_dirent;
274                 namlen = strlen(dp->d_name);
275                 reclen = DIRENT64_RECLEN(namlen);
276         }
277
278         if (reclen > uiop->uio_resid) {
279                 /*
280                  * Error if no entries were returned yet
281                  */
282                 if (uiop->uio_resid == st->grd_oresid)
283                         return (EINVAL);
284                 return (-1);
285         }
286
287         if (st->grd_flags & V_RDDIR_ENTFLAGS) {
288                 edp->ed_off = next;
289                 edp->ed_reclen = (ushort_t)reclen;
290         } else {
291                 /* XXX: This can change in the future. */
292                 dp->d_reclen = (ushort_t)reclen;
293                 dp->d_type = DT_DIR;
294                 dp->d_namlen = namlen;
295         }
296
297         if (uiomove((caddr_t)st->grd_dirent, reclen, UIO_READ, uiop))
298                 return (EFAULT);
299
300         uiop->uio_loffset = next;
301         if (*cookies != NULL) {
302                 **cookies = next;
303                 (*cookies)++;
304                 (*ncookies)--;
305                 KASSERT(*ncookies >= 0, ("ncookies=%d", *ncookies));
306         }
307
308         return (0);
309 }
310
311 /*
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)
317  *
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().
322  */
323 int
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)
326 {
327         offset_t off = (voff + 2) * st->grd_ureclen;
328
329         if (st->grd_flags & V_RDDIR_ENTFLAGS) {
330                 edirent_t *edp = st->grd_dirent;
331
332                 edp->ed_ino = ino;
333                 (void) strncpy(edp->ed_name, name, st->grd_namlen);
334                 edp->ed_eflags = eflags;
335         } else {
336                 dirent64_t *dp = st->grd_dirent;
337
338                 dp->d_ino = ino;
339                 (void) strncpy(dp->d_name, name, st->grd_namlen);
340         }
341
342         /*
343          * Inter-entry offsets are invalid, so we assume a record size of
344          * grd_ureclen and explicitly set the offset appropriately.
345          */
346         return (gfs_readdir_emit_int(st, uiop, off + st->grd_ureclen, ncookies,
347             cookies));
348 }
349
350 #ifdef sun
351 /*
352  * gfs_readdir_emitn: like gfs_readdir_emit(), but takes an integer
353  * instead of a string for the entry's name.
354  */
355 int
356 gfs_readdir_emitn(gfs_readdir_state_t *st, uio_t *uiop, offset_t voff,
357     ino64_t ino, unsigned long num)
358 {
359         char buf[40];
360
361         numtos(num, buf);
362         return (gfs_readdir_emit(st, uiop, voff, ino, buf, 0));
363 }
364 #endif
365
366 /*
367  * gfs_readdir_pred: readdir loop predicate
368  *   voffp - a pointer in which the next virtual offset should be stored
369  *
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().
374  */
375 int
376 gfs_readdir_pred(gfs_readdir_state_t *st, uio_t *uiop, offset_t *voffp,
377     int *ncookies, u_long **cookies)
378 {
379         offset_t off, voff;
380         int error;
381
382 top:
383         if (uiop->uio_resid <= 0)
384                 return (-1);
385
386         off = uiop->uio_loffset / st->grd_ureclen;
387         voff = off - 2;
388         if (off == 0) {
389                 if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_self,
390                     ".", 0, ncookies, cookies)) == 0)
391                         goto top;
392         } else if (off == 1) {
393                 if ((error = gfs_readdir_emit(st, uiop, voff, st->grd_parent,
394                     "..", 0, ncookies, cookies)) == 0)
395                         goto top;
396         } else {
397                 *voffp = voff;
398                 return (0);
399         }
400
401         return (error);
402 }
403
404 /*
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
409  *
410  * Returns a 0 on success, a non-zero errno on failure.  This result
411  * should be returned from readdir.
412  */
413 int
414 gfs_readdir_fini(gfs_readdir_state_t *st, int error, int *eofp, int eof)
415 {
416         size_t dirent_size;
417
418         if (st->grd_flags & V_RDDIR_ENTFLAGS)
419                 dirent_size = EDIRENT_RECLEN(st->grd_namlen);
420         else
421                 dirent_size = DIRENT64_RECLEN(st->grd_namlen);
422         kmem_free(st->grd_dirent, dirent_size);
423         if (error > 0)
424                 return (error);
425         if (eofp)
426                 *eofp = eof;
427         return (0);
428 }
429
430 /*
431  * gfs_lookup_dot
432  *
433  * Performs a basic check for "." and ".." directory entries.
434  */
435 int
436 gfs_lookup_dot(vnode_t **vpp, vnode_t *dvp, vnode_t *pvp, const char *nm)
437 {
438         if (*nm == '\0' || strcmp(nm, ".") == 0) {
439                 VN_HOLD(dvp);
440                 *vpp = dvp;
441                 return (0);
442         } else if (strcmp(nm, "..") == 0) {
443                 ASSERT(pvp != NULL);
444                 VN_HOLD(pvp);
445                 *vpp = pvp;
446                 return (0);
447         }
448
449         return (-1);
450 }
451
452 /*
453  * gfs_file_create(): create a new GFS file
454  *
455  *   size       - size of private data structure (v_data)
456  *   pvp        - parent vnode (GFS directory)
457  *   ops        - vnode operations vector
458  *
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.
462  *
463  * Given these constraints, this routine will automatically:
464  *
465  *      - Allocate v_data for the vnode
466  *      - Initialize necessary fields in the vnode
467  *      - Hold the parent
468  */
469 vnode_t *
470 gfs_file_create(size_t size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops)
471 {
472         gfs_file_t *fp;
473         vnode_t *vp;
474         int error;
475
476         /*
477          * Allocate vnode and internal data structure
478          */
479         fp = kmem_zalloc(size, KM_SLEEP);
480         error = getnewvnode("zfs", vfsp, ops, &vp);
481         ASSERT(error == 0);
482         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
483         vp->v_data = (caddr_t)fp;
484
485         /*
486          * Set up various pointers
487          */
488         fp->gfs_vnode = vp;
489         fp->gfs_parent = pvp;
490         fp->gfs_size = size;
491         fp->gfs_type = GFS_FILE;
492
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));
497
498         /*
499          * Initialize vnode and hold parent.
500          */
501         if (pvp)
502                 VN_HOLD(pvp);
503
504         return (vp);
505 }
506
507 /*
508  * gfs_dir_create: creates a new directory in the parent
509  *
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)
518  *
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.
523  *
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.
529  *
530  * This function also performs the same initialization as gfs_file_create().
531  */
532 vnode_t *
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)
536 {
537         vnode_t *vp;
538         gfs_dir_t *dp;
539         gfs_dirent_t *de;
540
541         vp = gfs_file_create(struct_size, pvp, vfsp, ops);
542         vp->v_type = VDIR;
543
544         dp = vp->v_data;
545         dp->gfsd_file.gfs_type = GFS_DIR;
546         dp->gfsd_maxlen = maxlen;
547
548         if (entries != NULL) {
549                 for (de = entries; de->gfse_name != NULL; de++)
550                         dp->gfsd_nstatic++;
551
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));
556         }
557
558         dp->gfsd_readdir = readdir_cb;
559         dp->gfsd_lookup = lookup_cb;
560         dp->gfsd_inode = inode_cb;
561
562         mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
563
564         return (vp);
565 }
566
567 /*
568  * gfs_root_create(): create a root vnode for a GFS filesystem
569  *
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.
572  */
573 vnode_t *
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)
577 {
578         vnode_t *vp;
579
580 #ifdef illumos
581         VFS_HOLD(vfsp);
582 #endif
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;
587         vp->v_flag |= VROOT;
588
589         return (vp);
590 }
591
592 #ifdef sun
593 /*
594  * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
595  *
596  * Similar to gfs_root_create(), this creates a root vnode for a file to
597  * be the pseudo-filesystem.
598  */
599 vnode_t *
600 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
601 {
602         vnode_t *vp = gfs_file_create(size, NULL, ops);
603
604         ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
605
606         VFS_HOLD(vfsp);
607         VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
608         vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
609
610         return (vp);
611 }
612 #endif  /* sun */
613
614 /*
615  * gfs_file_inactive()
616  *
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
619  * in the VFS layer.
620  *
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.
623  */
624 void *
625 gfs_file_inactive(vnode_t *vp)
626 {
627         int i;
628         gfs_dirent_t *ge = NULL;
629         gfs_file_t *fp = vp->v_data;
630         gfs_dir_t *dp = NULL;
631         void *data;
632
633         if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
634                 goto found;
635
636         /*
637          * XXX cope with a FreeBSD-specific race wherein the parent's
638          * snapshot data can be freed before the parent is
639          */
640         if ((dp = fp->gfs_parent->v_data) == NULL)
641                 return (NULL);
642
643         /*
644          * First, see if this vnode is cached in the parent.
645          */
646         gfs_dir_lock(dp);
647
648         /*
649          * Find it in the set of static entries.
650          */
651         for (i = 0; i < dp->gfsd_nstatic; i++)  {
652                 ge = &dp->gfsd_static[i];
653
654                 if (ge->gfse_vnode == vp)
655                         goto found;
656         }
657
658         /*
659          * If 'ge' is NULL, then it is a dynamic entry.
660          */
661         ge = NULL;
662
663 found:
664 #ifdef TODO
665         if (vp->v_flag & V_XATTRDIR)
666                 VI_LOCK(fp->gfs_parent);
667 #endif
668         VI_LOCK(vp);
669         /*
670          * Really remove this vnode
671          */
672         data = vp->v_data;
673         if (ge != NULL) {
674                 /*
675                  * If this was a statically cached entry, simply set the
676                  * cached vnode to NULL.
677                  */
678                 ge->gfse_vnode = NULL;
679         }
680         VI_UNLOCK(vp);
681
682         /*
683          * Free vnode and release parent
684          */
685         if (fp->gfs_parent) {
686                 if (dp)
687                         gfs_dir_unlock(dp);
688                 VOP_UNLOCK(vp, 0);
689                 VN_RELE(fp->gfs_parent);
690                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
691         } else {
692                 ASSERT(vp->v_vfsp != NULL);
693 #ifdef illumos
694                 VFS_RELE(vp->v_vfsp);
695 #endif
696         }
697 #ifdef TODO
698         if (vp->v_flag & V_XATTRDIR)
699                 VI_UNLOCK(fp->gfs_parent);
700 #endif
701         return (data);
702 }
703
704 /*
705  * gfs_dir_inactive()
706  *
707  * Same as above, but for directories.
708  */
709 void *
710 gfs_dir_inactive(vnode_t *vp)
711 {
712         gfs_dir_t *dp;
713
714         ASSERT(vp->v_type == VDIR);
715
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));
721         }
722
723         return (dp);
724 }
725
726 /*
727  * gfs_dir_lookup_dynamic()
728  *
729  * This routine looks up the provided name amongst the dynamic entries
730  * in the gfs directory and returns the corresponding vnode, if found.
731  *
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.
736  *
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:
740  *
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);
743  *
744  *      pvp     - parent vnode
745  *      nm      - name of entry
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.
757  *
758  *      Returns 0 on success, non-zero on error.
759  */
760 static int
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)
764 {
765         gfs_file_t *fp;
766         ino64_t ino;
767         int ret;
768
769         ASSERT(GFS_DIR_LOCKED(dp));
770
771         /*
772          * Drop the directory lock, as the lookup routine
773          * will need to allocate memory, or otherwise deadlock on this
774          * directory.
775          */
776         gfs_dir_unlock(dp);
777         ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
778         gfs_dir_lock(dp);
779
780         /*
781          * The callback for extended attributes returns a vnode
782          * with v_data from an underlying fs.
783          */
784         if (ret == 0 && !IS_XATTRDIR(dvp)) {
785                 fp = (gfs_file_t *)((*vpp)->v_data);
786                 fp->gfs_index = -1;
787                 fp->gfs_ino = ino;
788         }
789
790         return (ret);
791 }
792
793 /*
794  * gfs_dir_lookup_static()
795  *
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.
800  *
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.
806  *
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.
810  *
811  * This function returns 0 if a match is found, ENOENT if not.
812  */
813 static int
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)
817 {
818         gfs_dirent_t *ge;
819         vnode_t *vp = NULL;
820         int i;
821
822         ASSERT(GFS_DIR_LOCKED(dp));
823
824         /*
825          * Search static entries.
826          */
827         for (i = 0; i < dp->gfsd_nstatic; i++) {
828                 ge = &dp->gfsd_static[i];
829
830                 if (compare(ge->gfse_name, nm) == 0) {
831                         if (rpnp)
832                                 (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
833                                     rpnp->pn_bufsize);
834
835                         if (ge->gfse_vnode) {
836                                 ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
837                                 vp = ge->gfse_vnode;
838                                 VN_HOLD(vp);
839                                 break;
840                         }
841
842                         /*
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
848                          * the cached vnode.
849                          */
850                         gfs_dir_unlock(dp);
851                         vp = ge->gfse_ctor(dvp);
852                         gfs_dir_lock(dp);
853
854                         ((gfs_file_t *)vp->v_data)->gfs_index = i;
855
856                         /* Set the inode according to the callback. */
857                         ((gfs_file_t *)vp->v_data)->gfs_ino =
858                             dp->gfsd_inode(dvp, i);
859
860                         if (ge->gfse_flags & GFS_CACHE_VNODE) {
861                                 if (ge->gfse_vnode == NULL) {
862                                         ge->gfse_vnode = vp;
863                                 } else {
864                                         /*
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.
871                                          */
872                                         vnode_t *oldvp = vp;
873                                         vp = ge->gfse_vnode;
874                                         VN_HOLD(vp);
875
876                                         gfs_dir_unlock(dp);
877                                         VN_RELE(oldvp);
878                                         gfs_dir_lock(dp);
879                                 }
880                         }
881                         break;
882                 }
883         }
884
885         if (vp == NULL)
886                 return (ENOENT);
887         else if (idx)
888                 *idx = i;
889         *vpp = vp;
890         return (0);
891 }
892
893 /*
894  * gfs_dir_lookup()
895  *
896  * Looks up the given name in the directory and returns the corresponding
897  * vnode, if found.
898  *
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().
902  *
903  * This function returns 0 on success, non-zero on error.
904  */
905 int
906 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
907     int flags, int *direntflags, pathname_t *realpnp)
908 {
909         gfs_dir_t *dp = dvp->v_data;
910         boolean_t casecheck;
911         vnode_t *dynvp = NULL;
912         vnode_t *vp = NULL;
913         int (*compare)(const char *, const char *);
914         int error, idx;
915
916         ASSERT(dvp->v_type == VDIR);
917
918         if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
919                 return (0);
920
921         casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
922         if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
923             (flags & FIGNORECASE))
924                 compare = strcasecmp;
925         else
926                 compare = strcmp;
927
928         gfs_dir_lock(dp);
929
930         error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
931
932         if (vp && casecheck) {
933                 gfs_dirent_t *ge;
934                 int i;
935
936                 for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
937                         ge = &dp->gfsd_static[i];
938
939                         if (strcasecmp(ge->gfse_name, nm) == 0) {
940                                 *direntflags |= ED_CASE_CONFLICT;
941                                 goto out;
942                         }
943                 }
944         }
945
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);
949
950         if (vp && dynvp) {
951                 /* static and dynamic entries are case-insensitive conflict */
952                 ASSERT(casecheck);
953                 *direntflags |= ED_CASE_CONFLICT;
954                 VN_RELE(dynvp);
955         } else if (vp == NULL) {
956                 vp = dynvp;
957         } else if (error == ENOENT) {
958                 error = 0;
959         } else if (error) {
960                 VN_RELE(vp);
961                 vp = NULL;
962         }
963
964 out:
965         gfs_dir_unlock(dp);
966
967         *vpp = vp;
968         return (error);
969 }
970
971 /*
972  * gfs_dir_readdir: does a readdir() on the given directory
973  *
974  *    dvp       - directory vnode
975  *    uiop      - uio structure
976  *    eofp      - eof pointer
977  *    data      - arbitrary data passed to readdir callback
978  *
979  * This routine does all the readdir() dirty work.  Even so, the caller must
980  * supply two callbacks in order to get full compatibility.
981  *
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:
985  *
986  *      ino_t gfs_inode_cb(vnode_t *vp, int index);
987  *
988  *      vp      - vnode for the directory
989  *      index   - index in original gfs_dirent_t array
990  *
991  *      Returns the inode number for the given entry.
992  *
993  * For directories with dynamic entries, a readdir callback must be provided.
994  * This is significantly more complex, thanks to the particulars of
995  * VOP_READDIR().
996  *
997  *      int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
998  *          offset_t *off, offset_t *nextoff, void *data, int flags)
999  *
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
1009  *                untouched.
1010  *      nextoff - callback must set to offset of next entry.  Typically
1011  *                (off + 1)
1012  *      data    - caller-supplied data
1013  *      flags   - VOP_READDIR flags
1014  *
1015  *      Return 0 on success, or error on failure.
1016  */
1017 int
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)
1020 {
1021         gfs_readdir_state_t gstate;
1022         int error, eof = 0;
1023         ino64_t ino, pino;
1024         offset_t off, next;
1025         gfs_dir_t *dp = dvp->v_data;
1026
1027         error = gfs_get_parent_ino(dvp, cr, NULL, &pino, &ino);
1028         if (error)
1029                 return (error);
1030
1031         if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
1032             pino, ino, flags)) != 0)
1033                 return (error);
1034
1035         while ((error = gfs_readdir_pred(&gstate, uiop, &off, ncookies,
1036             cookies)) == 0 && !eof) {
1037
1038                 if (off >= 0 && off < dp->gfsd_nstatic) {
1039                         ino = dp->gfsd_inode(dvp, off);
1040
1041                         if ((error = gfs_readdir_emit(&gstate, uiop,
1042                             off, ino, dp->gfsd_static[off].gfse_name, 0,
1043                             ncookies, cookies)) != 0)
1044                                 break;
1045
1046                 } else if (dp->gfsd_readdir) {
1047                         off -= dp->gfsd_nstatic;
1048
1049                         if ((error = dp->gfsd_readdir(dvp,
1050                             gstate.grd_dirent, &eof, &off, &next,
1051                             data, flags)) != 0 || eof)
1052                                 break;
1053
1054                         off += dp->gfsd_nstatic + 2;
1055                         next += dp->gfsd_nstatic + 2;
1056
1057                         if ((error = gfs_readdir_emit_int(&gstate, uiop,
1058                             next, ncookies, cookies)) != 0)
1059                                 break;
1060                 } else {
1061                         /*
1062                          * Offset is beyond the end of the static entries, and
1063                          * we have no dynamic entries.  Set EOF.
1064                          */
1065                         eof = 1;
1066                 }
1067         }
1068
1069         return (gfs_readdir_fini(&gstate, error, eofp, eof));
1070 }
1071
1072
1073 /*
1074  * gfs_vop_lookup: VOP_LOOKUP() entry point
1075  *
1076  * For use directly in vnode ops table.  Given a GFS directory, calls
1077  * gfs_dir_lookup() as necessary.
1078  */
1079 /* ARGSUSED */
1080 int
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)
1084 {
1085         return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
1086 }
1087
1088 /*
1089  * gfs_vop_readdir: VOP_READDIR() entry point
1090  *
1091  * For use directly in vnode ops table.  Given a GFS directory, calls
1092  * gfs_dir_readdir() as necessary.
1093  */
1094 /* ARGSUSED */
1095 int
1096 gfs_vop_readdir(ap)
1097         struct vop_readdir_args /* {
1098                 struct vnode *a_vp;
1099                 struct uio *a_uio;
1100                 struct ucred *a_cred;
1101                 int *a_eofflag;
1102                 int *ncookies;
1103                 u_long **a_cookies;
1104         } */ *ap;
1105 {
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;
1110         int ncookies = 0;
1111         u_long *cookies = NULL;
1112         int error;
1113
1114         if (ap->a_ncookies) {
1115                 /*
1116                  * Minimum entry size is dirent size and 1 byte for a file name.
1117                  */
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;
1122         }
1123
1124         error = gfs_dir_readdir(vp, uiop, eofp, &ncookies, &cookies, NULL,
1125             cr, 0);
1126
1127         if (error == 0) {
1128                 /* Subtract unused cookies */
1129                 if (ap->a_ncookies)
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;
1135         }
1136
1137         return (error);
1138 }
1139
1140
1141 #ifdef sun
1142 /*
1143  * gfs_vop_map: VOP_MAP() entry point
1144  *
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.
1150  */
1151 /* ARGSUSED */
1152 int
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)
1156 {
1157         int rv;
1158         ssize_t resid = len;
1159
1160         /*
1161          * Check for bad parameters
1162          */
1163 #ifdef _ILP32
1164         if (len > MAXOFF_T)
1165                 return (ENOMEM);
1166 #endif
1167         if (vp->v_flag & VNOMAP)
1168                 return (ENOTSUP);
1169         if (off > MAXOFF_T)
1170                 return (EFBIG);
1171         if ((long)off < 0 || (long)(off + len) < 0)
1172                 return (EINVAL);
1173         if (vp->v_type != VREG)
1174                 return (ENODEV);
1175         if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
1176                 return (EACCES);
1177
1178         /*
1179          * Find appropriate address if needed, otherwise clear address range.
1180          */
1181         as_rangelock(as);
1182         rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1183         if (rv != 0) {
1184                 as_rangeunlock(as);
1185                 return (rv);
1186         }
1187
1188         /*
1189          * Create mapping
1190          */
1191         rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
1192         as_rangeunlock(as);
1193         if (rv != 0)
1194                 return (rv);
1195
1196         /*
1197          * Fill with data from read()
1198          */
1199         rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
1200             0, (rlim64_t)0, cred, &resid);
1201
1202         if (rv == 0 && resid != 0)
1203                 rv = ENXIO;
1204
1205         if (rv != 0) {
1206                 as_rangelock(as);
1207                 (void) as_unmap(as, *addrp, len);
1208                 as_rangeunlock(as);
1209         }
1210
1211         return (rv);
1212 }
1213 #endif  /* sun */
1214
1215 /*
1216  * gfs_vop_reclaim: VOP_RECLAIM() entry point (solaris' VOP_INACTIVE())
1217  *
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.
1220  */
1221 /* ARGSUSED */
1222 int
1223 gfs_vop_reclaim(ap)
1224         struct vop_reclaim_args /* {
1225                 struct vnode *a_vp;
1226                 struct thread *a_td;
1227         } */ *ap;
1228 {
1229         vnode_t *vp = ap->a_vp;
1230         gfs_file_t *fp = vp->v_data;
1231
1232         if (fp->gfs_type == GFS_DIR)
1233                 gfs_dir_inactive(vp);
1234         else
1235                 gfs_file_inactive(vp);
1236
1237         vnode_destroy_vobject(vp);
1238         VI_LOCK(vp);
1239         vp->v_data = NULL;
1240         VI_UNLOCK(vp);
1241         kmem_free(fp, fp->gfs_size);
1242
1243         return (0);
1244 }