]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/gfs.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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_inactive()
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                 if (pvp == NULL) {
444                         ASSERT(dvp->v_flag & VROOT);
445                         VN_HOLD(dvp);
446                         *vpp = dvp;
447                 } else {
448                         VN_HOLD(pvp);
449                         *vpp = pvp;
450                 }
451                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
452                 return (0);
453         }
454
455         return (-1);
456 }
457
458 /*
459  * gfs_file_create(): create a new GFS file
460  *
461  *   size       - size of private data structure (v_data)
462  *   pvp        - parent vnode (GFS directory)
463  *   ops        - vnode operations vector
464  *
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.
468  *
469  * Given these constraints, this routine will automatically:
470  *
471  *      - Allocate v_data for the vnode
472  *      - Initialize necessary fields in the vnode
473  *      - Hold the parent
474  */
475 vnode_t *
476 gfs_file_create(size_t size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops)
477 {
478         gfs_file_t *fp;
479         vnode_t *vp;
480         int error;
481
482         /*
483          * Allocate vnode and internal data structure
484          */
485         fp = kmem_zalloc(size, KM_SLEEP);
486         error = getnewvnode("zfs", vfsp, ops, &vp);
487         ASSERT(error == 0);
488         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
489         vp->v_data = (caddr_t)fp;
490
491         /*
492          * Set up various pointers
493          */
494         fp->gfs_vnode = vp;
495         fp->gfs_parent = pvp;
496         fp->gfs_size = size;
497         fp->gfs_type = GFS_FILE;
498
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));
503
504         /*
505          * Initialize vnode and hold parent.
506          */
507         if (pvp)
508                 VN_HOLD(pvp);
509
510         return (vp);
511 }
512
513 /*
514  * gfs_dir_create: creates a new directory in the parent
515  *
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)
524  *
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.
529  *
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.
535  *
536  * This function also performs the same initialization as gfs_file_create().
537  */
538 vnode_t *
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)
542 {
543         vnode_t *vp;
544         gfs_dir_t *dp;
545         gfs_dirent_t *de;
546
547         vp = gfs_file_create(struct_size, pvp, vfsp, ops);
548         vp->v_type = VDIR;
549
550         dp = vp->v_data;
551         dp->gfsd_file.gfs_type = GFS_DIR;
552         dp->gfsd_maxlen = maxlen;
553
554         if (entries != NULL) {
555                 for (de = entries; de->gfse_name != NULL; de++)
556                         dp->gfsd_nstatic++;
557
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));
562         }
563
564         dp->gfsd_readdir = readdir_cb;
565         dp->gfsd_lookup = lookup_cb;
566         dp->gfsd_inode = inode_cb;
567
568         mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
569
570         return (vp);
571 }
572
573 /*
574  * gfs_root_create(): create a root vnode for a GFS filesystem
575  *
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.
578  */
579 vnode_t *
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)
583 {
584         vnode_t *vp;
585
586         VFS_HOLD(vfsp);
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;
591         vp->v_flag |= VROOT;
592
593         return (vp);
594 }
595
596 #ifdef sun
597 /*
598  * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
599  *
600  * Similar to gfs_root_create(), this creates a root vnode for a file to
601  * be the pseudo-filesystem.
602  */
603 vnode_t *
604 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
605 {
606         vnode_t *vp = gfs_file_create(size, NULL, ops);
607
608         ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
609
610         VFS_HOLD(vfsp);
611         VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
612         vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
613
614         return (vp);
615 }
616 #endif  /* sun */
617
618 /*
619  * gfs_file_inactive()
620  *
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
623  * in the VFS layer.
624  *
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.
627  */
628 void *
629 gfs_file_inactive(vnode_t *vp)
630 {
631         int i;
632         gfs_dirent_t *ge = NULL;
633         gfs_file_t *fp = vp->v_data;
634         gfs_dir_t *dp = NULL;
635         void *data;
636
637         if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
638                 goto found;
639
640         /*
641          * XXX cope with a FreeBSD-specific race wherein the parent's
642          * snapshot data can be freed before the parent is
643          */
644         if ((dp = fp->gfs_parent->v_data) == NULL)
645                 return (NULL);
646
647         /*
648          * First, see if this vnode is cached in the parent.
649          */
650         gfs_dir_lock(dp);
651
652         /*
653          * Find it in the set of static entries.
654          */
655         for (i = 0; i < dp->gfsd_nstatic; i++)  {
656                 ge = &dp->gfsd_static[i];
657
658                 if (ge->gfse_vnode == vp)
659                         goto found;
660         }
661
662         /*
663          * If 'ge' is NULL, then it is a dynamic entry.
664          */
665         ge = NULL;
666
667 found:
668 #ifdef TODO
669         if (vp->v_flag & V_XATTRDIR)
670                 VI_LOCK(fp->gfs_parent);
671 #endif
672         VI_LOCK(vp);
673         /*
674          * Really remove this vnode
675          */
676         data = vp->v_data;
677         if (ge != NULL) {
678                 /*
679                  * If this was a statically cached entry, simply set the
680                  * cached vnode to NULL.
681                  */
682                 ge->gfse_vnode = NULL;
683         }
684         VI_UNLOCK(vp);
685
686         /*
687          * Free vnode and release parent
688          */
689         if (fp->gfs_parent) {
690                 if (dp)
691                         gfs_dir_unlock(dp);
692                 VOP_UNLOCK(vp, 0);
693                 VN_RELE(fp->gfs_parent);
694                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
695         } else {
696                 ASSERT(vp->v_vfsp != NULL);
697                 VFS_RELE(vp->v_vfsp);
698         }
699 #ifdef TODO
700         if (vp->v_flag & V_XATTRDIR)
701                 VI_UNLOCK(fp->gfs_parent);
702 #endif
703         return (data);
704 }
705
706 /*
707  * gfs_dir_inactive()
708  *
709  * Same as above, but for directories.
710  */
711 void *
712 gfs_dir_inactive(vnode_t *vp)
713 {
714         gfs_dir_t *dp;
715
716         ASSERT(vp->v_type == VDIR);
717
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));
723         }
724
725         return (dp);
726 }
727
728 /*
729  * gfs_dir_lookup_dynamic()
730  *
731  * This routine looks up the provided name amongst the dynamic entries
732  * in the gfs directory and returns the corresponding vnode, if found.
733  *
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.
738  *
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:
742  *
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);
745  *
746  *      pvp     - parent vnode
747  *      nm      - name of entry
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.
759  *
760  *      Returns 0 on success, non-zero on error.
761  */
762 static int
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)
766 {
767         gfs_file_t *fp;
768         ino64_t ino;
769         int ret;
770
771         ASSERT(GFS_DIR_LOCKED(dp));
772
773         /*
774          * Drop the directory lock, as the lookup routine
775          * will need to allocate memory, or otherwise deadlock on this
776          * directory.
777          */
778         gfs_dir_unlock(dp);
779         ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
780         gfs_dir_lock(dp);
781
782         /*
783          * The callback for extended attributes returns a vnode
784          * with v_data from an underlying fs.
785          */
786         if (ret == 0 && !IS_XATTRDIR(dvp)) {
787                 fp = (gfs_file_t *)((*vpp)->v_data);
788                 fp->gfs_index = -1;
789                 fp->gfs_ino = ino;
790         }
791
792         return (ret);
793 }
794
795 /*
796  * gfs_dir_lookup_static()
797  *
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.
802  *
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.
808  *
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.
812  *
813  * This function returns 0 if a match is found, ENOENT if not.
814  */
815 static int
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)
819 {
820         gfs_dirent_t *ge;
821         vnode_t *vp = NULL;
822         int i;
823
824         ASSERT(GFS_DIR_LOCKED(dp));
825
826         /*
827          * Search static entries.
828          */
829         for (i = 0; i < dp->gfsd_nstatic; i++) {
830                 ge = &dp->gfsd_static[i];
831
832                 if (compare(ge->gfse_name, nm) == 0) {
833                         if (rpnp)
834                                 (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
835                                     rpnp->pn_bufsize);
836
837                         if (ge->gfse_vnode) {
838                                 ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
839                                 vp = ge->gfse_vnode;
840                                 VN_HOLD(vp);
841                                 break;
842                         }
843
844                         /*
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
850                          * the cached vnode.
851                          */
852                         gfs_dir_unlock(dp);
853                         vp = ge->gfse_ctor(dvp);
854                         gfs_dir_lock(dp);
855
856                         ((gfs_file_t *)vp->v_data)->gfs_index = i;
857
858                         /* Set the inode according to the callback. */
859                         ((gfs_file_t *)vp->v_data)->gfs_ino =
860                             dp->gfsd_inode(dvp, i);
861
862                         if (ge->gfse_flags & GFS_CACHE_VNODE) {
863                                 if (ge->gfse_vnode == NULL) {
864                                         ge->gfse_vnode = vp;
865                                 } else {
866                                         /*
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.
873                                          */
874                                         vnode_t *oldvp = vp;
875                                         vp = ge->gfse_vnode;
876                                         VN_HOLD(vp);
877
878                                         gfs_dir_unlock(dp);
879                                         VN_RELE(oldvp);
880                                         gfs_dir_lock(dp);
881                                 }
882                         }
883                         break;
884                 }
885         }
886
887         if (vp == NULL)
888                 return (ENOENT);
889         else if (idx)
890                 *idx = i;
891         *vpp = vp;
892         return (0);
893 }
894
895 /*
896  * gfs_dir_lookup()
897  *
898  * Looks up the given name in the directory and returns the corresponding
899  * vnode, if found.
900  *
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().
904  *
905  * This function returns 0 on success, non-zero on error.
906  */
907 int
908 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
909     int flags, int *direntflags, pathname_t *realpnp)
910 {
911         gfs_dir_t *dp = dvp->v_data;
912         boolean_t casecheck;
913         vnode_t *dynvp = NULL;
914         vnode_t *vp = NULL;
915         int (*compare)(const char *, const char *);
916         int error, idx;
917
918         ASSERT(dvp->v_type == VDIR);
919
920         if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
921                 return (0);
922
923         casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
924         if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
925             (flags & FIGNORECASE))
926                 compare = strcasecmp;
927         else
928                 compare = strcmp;
929
930         gfs_dir_lock(dp);
931
932         error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
933
934         if (vp && casecheck) {
935                 gfs_dirent_t *ge;
936                 int i;
937
938                 for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
939                         ge = &dp->gfsd_static[i];
940
941                         if (strcasecmp(ge->gfse_name, nm) == 0) {
942                                 *direntflags |= ED_CASE_CONFLICT;
943                                 goto out;
944                         }
945                 }
946         }
947
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);
951
952         if (vp && dynvp) {
953                 /* static and dynamic entries are case-insensitive conflict */
954                 ASSERT(casecheck);
955                 *direntflags |= ED_CASE_CONFLICT;
956                 VN_RELE(dynvp);
957         } else if (vp == NULL) {
958                 vp = dynvp;
959         } else if (error == ENOENT) {
960                 error = 0;
961         } else if (error) {
962                 VN_RELE(vp);
963                 vp = NULL;
964         }
965
966 out:
967         gfs_dir_unlock(dp);
968
969         *vpp = vp;
970         return (error);
971 }
972
973 /*
974  * gfs_dir_readdir: does a readdir() on the given directory
975  *
976  *    dvp       - directory vnode
977  *    uiop      - uio structure
978  *    eofp      - eof pointer
979  *    data      - arbitrary data passed to readdir callback
980  *
981  * This routine does all the readdir() dirty work.  Even so, the caller must
982  * supply two callbacks in order to get full compatibility.
983  *
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:
987  *
988  *      ino_t gfs_inode_cb(vnode_t *vp, int index);
989  *
990  *      vp      - vnode for the directory
991  *      index   - index in original gfs_dirent_t array
992  *
993  *      Returns the inode number for the given entry.
994  *
995  * For directories with dynamic entries, a readdir callback must be provided.
996  * This is significantly more complex, thanks to the particulars of
997  * VOP_READDIR().
998  *
999  *      int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1000  *          offset_t *off, offset_t *nextoff, void *data, int flags)
1001  *
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
1011  *                untouched.
1012  *      nextoff - callback must set to offset of next entry.  Typically
1013  *                (off + 1)
1014  *      data    - caller-supplied data
1015  *      flags   - VOP_READDIR flags
1016  *
1017  *      Return 0 on success, or error on failure.
1018  */
1019 int
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)
1022 {
1023         gfs_readdir_state_t gstate;
1024         int error, eof = 0;
1025         ino64_t ino, pino;
1026         offset_t off, next;
1027         gfs_dir_t *dp = dvp->v_data;
1028
1029         error = gfs_get_parent_ino(dvp, cr, NULL, &pino, &ino);
1030         if (error)
1031                 return (error);
1032
1033         if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
1034             pino, ino, flags)) != 0)
1035                 return (error);
1036
1037         while ((error = gfs_readdir_pred(&gstate, uiop, &off, ncookies,
1038             cookies)) == 0 && !eof) {
1039
1040                 if (off >= 0 && off < dp->gfsd_nstatic) {
1041                         ino = dp->gfsd_inode(dvp, off);
1042
1043                         if ((error = gfs_readdir_emit(&gstate, uiop,
1044                             off, ino, dp->gfsd_static[off].gfse_name, 0,
1045                             ncookies, cookies)) != 0)
1046                                 break;
1047
1048                 } else if (dp->gfsd_readdir) {
1049                         off -= dp->gfsd_nstatic;
1050
1051                         if ((error = dp->gfsd_readdir(dvp,
1052                             gstate.grd_dirent, &eof, &off, &next,
1053                             data, flags)) != 0 || eof)
1054                                 break;
1055
1056                         off += dp->gfsd_nstatic + 2;
1057                         next += dp->gfsd_nstatic + 2;
1058
1059                         if ((error = gfs_readdir_emit_int(&gstate, uiop,
1060                             next, ncookies, cookies)) != 0)
1061                                 break;
1062                 } else {
1063                         /*
1064                          * Offset is beyond the end of the static entries, and
1065                          * we have no dynamic entries.  Set EOF.
1066                          */
1067                         eof = 1;
1068                 }
1069         }
1070
1071         return (gfs_readdir_fini(&gstate, error, eofp, eof));
1072 }
1073
1074
1075 /*
1076  * gfs_vop_lookup: VOP_LOOKUP() entry point
1077  *
1078  * For use directly in vnode ops table.  Given a GFS directory, calls
1079  * gfs_dir_lookup() as necessary.
1080  */
1081 /* ARGSUSED */
1082 int
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)
1086 {
1087         return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
1088 }
1089
1090 /*
1091  * gfs_vop_readdir: VOP_READDIR() entry point
1092  *
1093  * For use directly in vnode ops table.  Given a GFS directory, calls
1094  * gfs_dir_readdir() as necessary.
1095  */
1096 /* ARGSUSED */
1097 int
1098 gfs_vop_readdir(ap)
1099         struct vop_readdir_args /* {
1100                 struct vnode *a_vp;
1101                 struct uio *a_uio;
1102                 struct ucred *a_cred;
1103                 int *a_eofflag;
1104                 int *ncookies;
1105                 u_long **a_cookies;
1106         } */ *ap;
1107 {
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;
1112         int ncookies = 0;
1113         u_long *cookies = NULL;
1114         int error;
1115
1116         if (ap->a_ncookies) {
1117                 /*
1118                  * Minimum entry size is dirent size and 1 byte for a file name.
1119                  */
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;
1124         }
1125
1126         error = gfs_dir_readdir(vp, uiop, eofp, &ncookies, &cookies, NULL,
1127             cr, 0);
1128
1129         if (error == 0) {
1130                 /* Subtract unused cookies */
1131                 if (ap->a_ncookies)
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;
1137         }
1138
1139         return (error);
1140 }
1141
1142
1143 #ifdef sun
1144 /*
1145  * gfs_vop_map: VOP_MAP() entry point
1146  *
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.
1152  */
1153 /* ARGSUSED */
1154 int
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)
1158 {
1159         int rv;
1160         ssize_t resid = len;
1161
1162         /*
1163          * Check for bad parameters
1164          */
1165 #ifdef _ILP32
1166         if (len > MAXOFF_T)
1167                 return (ENOMEM);
1168 #endif
1169         if (vp->v_flag & VNOMAP)
1170                 return (ENOTSUP);
1171         if (off > MAXOFF_T)
1172                 return (EFBIG);
1173         if ((long)off < 0 || (long)(off + len) < 0)
1174                 return (EINVAL);
1175         if (vp->v_type != VREG)
1176                 return (ENODEV);
1177         if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
1178                 return (EACCES);
1179
1180         /*
1181          * Find appropriate address if needed, otherwise clear address range.
1182          */
1183         as_rangelock(as);
1184         rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1185         if (rv != 0) {
1186                 as_rangeunlock(as);
1187                 return (rv);
1188         }
1189
1190         /*
1191          * Create mapping
1192          */
1193         rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
1194         as_rangeunlock(as);
1195         if (rv != 0)
1196                 return (rv);
1197
1198         /*
1199          * Fill with data from read()
1200          */
1201         rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
1202             0, (rlim64_t)0, cred, &resid);
1203
1204         if (rv == 0 && resid != 0)
1205                 rv = ENXIO;
1206
1207         if (rv != 0) {
1208                 as_rangelock(as);
1209                 (void) as_unmap(as, *addrp, len);
1210                 as_rangeunlock(as);
1211         }
1212
1213         return (rv);
1214 }
1215 #endif  /* sun */
1216
1217 /*
1218  * gfs_vop_inactive: VOP_INACTIVE() entry point
1219  *
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.
1222  */
1223 /* ARGSUSED */
1224 int
1225 gfs_vop_inactive(ap)
1226         struct vop_inactive_args /* {
1227                 struct vnode *a_vp;
1228                 struct thread *a_td;
1229         } */ *ap;
1230 {
1231         vnode_t *vp = ap->a_vp;
1232         gfs_file_t *fp = vp->v_data;
1233
1234         if (fp->gfs_type == GFS_DIR)
1235                 gfs_dir_inactive(vp);
1236         else
1237                 gfs_file_inactive(vp);
1238
1239         VI_LOCK(vp);
1240         vp->v_data = NULL;
1241         VI_UNLOCK(vp);
1242         kmem_free(fp, fp->gfs_size);
1243
1244         return (0);
1245 }