]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/gfs.c
MFV r299449: 6763 aclinherit=restricted masks inherited permissions by group
[FreeBSD/FreeBSD.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 illumos
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  /* illumos */
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 illumos
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         int ltype;
439
440         if (*nm == '\0' || strcmp(nm, ".") == 0) {
441                 VN_HOLD(dvp);
442                 *vpp = dvp;
443                 return (0);
444         } else if (strcmp(nm, "..") == 0) {
445                 if (pvp == NULL) {
446                         ASSERT(dvp->v_flag & VROOT);
447                         VN_HOLD(dvp);
448                         *vpp = dvp;
449                         ASSERT_VOP_ELOCKED(dvp, "gfs_lookup_dot: non-locked dvp");
450                 } else {
451                         ltype = VOP_ISLOCKED(dvp);
452                         VOP_UNLOCK(dvp, 0);
453                         VN_HOLD(pvp);
454                         *vpp = pvp;
455                         vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
456                         vn_lock(dvp, ltype | LK_RETRY);
457                 }
458                 return (0);
459         }
460
461         return (-1);
462 }
463
464 /*
465  * gfs_file_create(): create a new GFS file
466  *
467  *   size       - size of private data structure (v_data)
468  *   pvp        - parent vnode (GFS directory)
469  *   ops        - vnode operations vector
470  *
471  * In order to use this interface, the parent vnode must have been created by
472  * gfs_dir_create(), and the private data stored in v_data must have a
473  * 'gfs_file_t' as its first field.
474  *
475  * Given these constraints, this routine will automatically:
476  *
477  *      - Allocate v_data for the vnode
478  *      - Initialize necessary fields in the vnode
479  *      - Hold the parent
480  */
481 vnode_t *
482 gfs_file_create(size_t size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops)
483 {
484         gfs_file_t *fp;
485         vnode_t *vp;
486         int error;
487
488         /*
489          * Allocate vnode and internal data structure
490          */
491         fp = kmem_zalloc(size, KM_SLEEP);
492         error = getnewvnode("zfs_gfs", vfsp, ops, &vp);
493         ASSERT(error == 0);
494         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
495         vp->v_data = (caddr_t)fp;
496
497         /*
498          * Set up various pointers
499          */
500         fp->gfs_vnode = vp;
501         fp->gfs_parent = pvp;
502         fp->gfs_size = size;
503         fp->gfs_type = GFS_FILE;
504
505         vp->v_vflag |= VV_FORCEINSMQ;
506         error = insmntque(vp, vfsp);
507         vp->v_vflag &= ~VV_FORCEINSMQ;
508         KASSERT(error == 0, ("insmntque() failed: error %d", error));
509
510         /*
511          * Initialize vnode and hold parent.
512          */
513         if (pvp)
514                 VN_HOLD(pvp);
515
516         return (vp);
517 }
518
519 /*
520  * gfs_dir_create: creates a new directory in the parent
521  *
522  *   size       - size of private data structure (v_data)
523  *   pvp        - parent vnode (GFS directory)
524  *   ops        - vnode operations vector
525  *   entries    - NULL-terminated list of static entries (if any)
526  *   maxlen     - maximum length of a directory entry
527  *   readdir_cb - readdir callback (see gfs_dir_readdir)
528  *   inode_cb   - inode callback (see gfs_dir_readdir)
529  *   lookup_cb  - lookup callback (see gfs_dir_lookup)
530  *
531  * In order to use this function, the first member of the private vnode
532  * structure (v_data) must be a gfs_dir_t.  For each directory, there are
533  * static entries, defined when the structure is initialized, and dynamic
534  * entries, retrieved through callbacks.
535  *
536  * If a directory has static entries, then it must supply a inode callback,
537  * which will compute the inode number based on the parent and the index.
538  * For a directory with dynamic entries, the caller must supply a readdir
539  * callback and a lookup callback.  If a static lookup fails, we fall back to
540  * the supplied lookup callback, if any.
541  *
542  * This function also performs the same initialization as gfs_file_create().
543  */
544 vnode_t *
545 gfs_dir_create(size_t struct_size, vnode_t *pvp, vfs_t *vfsp, vnodeops_t *ops,
546     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
547     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
548 {
549         vnode_t *vp;
550         gfs_dir_t *dp;
551         gfs_dirent_t *de;
552
553         vp = gfs_file_create(struct_size, pvp, vfsp, ops);
554         vp->v_type = VDIR;
555
556         dp = vp->v_data;
557         dp->gfsd_file.gfs_type = GFS_DIR;
558         dp->gfsd_maxlen = maxlen;
559
560         if (entries != NULL) {
561                 for (de = entries; de->gfse_name != NULL; de++)
562                         dp->gfsd_nstatic++;
563
564                 dp->gfsd_static = kmem_alloc(
565                     dp->gfsd_nstatic * sizeof (gfs_dirent_t), KM_SLEEP);
566                 bcopy(entries, dp->gfsd_static,
567                     dp->gfsd_nstatic * sizeof (gfs_dirent_t));
568         }
569
570         dp->gfsd_readdir = readdir_cb;
571         dp->gfsd_lookup = lookup_cb;
572         dp->gfsd_inode = inode_cb;
573
574         mutex_init(&dp->gfsd_lock, NULL, MUTEX_DEFAULT, NULL);
575
576         return (vp);
577 }
578
579 /*
580  * gfs_root_create(): create a root vnode for a GFS filesystem
581  *
582  * Similar to gfs_dir_create(), this creates a root vnode for a filesystem.  The
583  * only difference is that it takes a vfs_t instead of a vnode_t as its parent.
584  */
585 vnode_t *
586 gfs_root_create(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino,
587     gfs_dirent_t *entries, gfs_inode_cb inode_cb, int maxlen,
588     gfs_readdir_cb readdir_cb, gfs_lookup_cb lookup_cb)
589 {
590         vnode_t *vp;
591
592 #ifdef illumos
593         VFS_HOLD(vfsp);
594 #endif
595         vp = gfs_dir_create(size, NULL, vfsp, ops, entries, inode_cb,
596             maxlen, readdir_cb, lookup_cb);
597         /* Manually set the inode */
598         ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
599         vp->v_flag |= VROOT;
600
601         return (vp);
602 }
603
604 #ifdef illumos
605 /*
606  * gfs_root_create_file(): create a root vnode for a GFS file as a filesystem
607  *
608  * Similar to gfs_root_create(), this creates a root vnode for a file to
609  * be the pseudo-filesystem.
610  */
611 vnode_t *
612 gfs_root_create_file(size_t size, vfs_t *vfsp, vnodeops_t *ops, ino64_t ino)
613 {
614         vnode_t *vp = gfs_file_create(size, NULL, ops);
615
616         ((gfs_file_t *)vp->v_data)->gfs_ino = ino;
617
618         VFS_HOLD(vfsp);
619         VN_SET_VFS_TYPE_DEV(vp, vfsp, VREG, 0);
620         vp->v_flag |= VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT;
621
622         return (vp);
623 }
624 #endif  /* illumos */
625
626 /*
627  * gfs_file_inactive()
628  *
629  * Called from the VOP_RECLAIM() routine.  If necessary, this routine will
630  * remove the given vnode from the parent directory and clean up any references
631  * in the VFS layer.
632  *
633  * If the vnode was not removed (due to a race with vget), then NULL is
634  * returned.  Otherwise, a pointer to the private data is returned.
635  */
636 void *
637 gfs_file_inactive(vnode_t *vp)
638 {
639         int i;
640         gfs_dirent_t *ge = NULL;
641         gfs_file_t *fp = vp->v_data;
642         gfs_dir_t *dp = NULL;
643         void *data;
644
645         if (fp->gfs_parent == NULL || (vp->v_flag & V_XATTRDIR))
646                 goto found;
647
648         /*
649          * XXX cope with a FreeBSD-specific race wherein the parent's
650          * snapshot data can be freed before the parent is
651          */
652         if ((dp = fp->gfs_parent->v_data) == NULL)
653                 return (NULL);
654
655         /*
656          * First, see if this vnode is cached in the parent.
657          */
658         gfs_dir_lock(dp);
659
660         /*
661          * Find it in the set of static entries.
662          */
663         for (i = 0; i < dp->gfsd_nstatic; i++)  {
664                 ge = &dp->gfsd_static[i];
665
666                 if (ge->gfse_vnode == vp)
667                         goto found;
668         }
669
670         /*
671          * If 'ge' is NULL, then it is a dynamic entry.
672          */
673         ge = NULL;
674
675 found:
676 #ifdef TODO
677         if (vp->v_flag & V_XATTRDIR)
678                 VI_LOCK(fp->gfs_parent);
679 #endif
680         VI_LOCK(vp);
681         /*
682          * Really remove this vnode
683          */
684         data = vp->v_data;
685         if (ge != NULL) {
686                 /*
687                  * If this was a statically cached entry, simply set the
688                  * cached vnode to NULL.
689                  */
690                 ge->gfse_vnode = NULL;
691         }
692         VI_UNLOCK(vp);
693
694         /*
695          * Free vnode and release parent
696          */
697         if (fp->gfs_parent) {
698                 if (dp)
699                         gfs_dir_unlock(dp);
700                 VOP_UNLOCK(vp, 0);
701                 VN_RELE(fp->gfs_parent);
702                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
703         } else {
704                 ASSERT(vp->v_vfsp != NULL);
705 #ifdef illumos
706                 VFS_RELE(vp->v_vfsp);
707 #endif
708         }
709 #ifdef TODO
710         if (vp->v_flag & V_XATTRDIR)
711                 VI_UNLOCK(fp->gfs_parent);
712 #endif
713         return (data);
714 }
715
716 /*
717  * gfs_dir_inactive()
718  *
719  * Same as above, but for directories.
720  */
721 void *
722 gfs_dir_inactive(vnode_t *vp)
723 {
724         gfs_dir_t *dp;
725
726         ASSERT(vp->v_type == VDIR);
727
728         if ((dp = gfs_file_inactive(vp)) != NULL) {
729                 mutex_destroy(&dp->gfsd_lock);
730                 if (dp->gfsd_nstatic)
731                         kmem_free(dp->gfsd_static,
732                             dp->gfsd_nstatic * sizeof (gfs_dirent_t));
733         }
734
735         return (dp);
736 }
737
738 /*
739  * gfs_dir_lookup_dynamic()
740  *
741  * This routine looks up the provided name amongst the dynamic entries
742  * in the gfs directory and returns the corresponding vnode, if found.
743  *
744  * The gfs directory is expected to be locked by the caller prior to
745  * calling this function.  The directory will be unlocked during the
746  * execution of this function, but will be locked upon return from the
747  * function.  This function returns 0 on success, non-zero on error.
748  *
749  * The dynamic lookups are performed by invoking the lookup
750  * callback, which is passed to this function as the first argument.
751  * The arguments to the callback are:
752  *
753  * int gfs_lookup_cb(vnode_t *pvp, const char *nm, vnode_t **vpp, cred_t *cr,
754  *     int flags, int *deflgs, pathname_t *rpnp);
755  *
756  *      pvp     - parent vnode
757  *      nm      - name of entry
758  *      vpp     - pointer to resulting vnode
759  *      cr      - pointer to cred
760  *      flags   - flags value from lookup request
761  *              ignored here; currently only used to request
762  *              insensitive lookups
763  *      direntflgs - output parameter, directory entry flags
764  *              ignored here; currently only used to indicate a lookup
765  *              has more than one possible match when case is not considered
766  *      realpnp - output parameter, real pathname
767  *              ignored here; when lookup was performed case-insensitively,
768  *              this field contains the "real" name of the file.
769  *
770  *      Returns 0 on success, non-zero on error.
771  */
772 static int
773 gfs_dir_lookup_dynamic(gfs_lookup_cb callback, gfs_dir_t *dp,
774     const char *nm, vnode_t *dvp, vnode_t **vpp, cred_t *cr, int flags,
775     int *direntflags, pathname_t *realpnp)
776 {
777         gfs_file_t *fp;
778         ino64_t ino;
779         int ret;
780
781         ASSERT(GFS_DIR_LOCKED(dp));
782
783         /*
784          * Drop the directory lock, as the lookup routine
785          * will need to allocate memory, or otherwise deadlock on this
786          * directory.
787          */
788         gfs_dir_unlock(dp);
789         ret = callback(dvp, nm, vpp, &ino, cr, flags, direntflags, realpnp);
790         gfs_dir_lock(dp);
791
792         /*
793          * The callback for extended attributes returns a vnode
794          * with v_data from an underlying fs.
795          */
796         if (ret == 0 && !IS_XATTRDIR(dvp)) {
797                 fp = (gfs_file_t *)((*vpp)->v_data);
798                 fp->gfs_index = -1;
799                 fp->gfs_ino = ino;
800         }
801
802         return (ret);
803 }
804
805 /*
806  * gfs_dir_lookup_static()
807  *
808  * This routine looks up the provided name amongst the static entries
809  * in the gfs directory and returns the corresponding vnode, if found.
810  * The first argument to the function is a pointer to the comparison
811  * function this function should use to decide if names are a match.
812  *
813  * If a match is found, and GFS_CACHE_VNODE is set and the vnode
814  * exists, we simply return the existing vnode.  Otherwise, we call
815  * the static entry's callback routine, caching the result if
816  * necessary.  If the idx pointer argument is non-NULL, we use it to
817  * return the index of the matching static entry.
818  *
819  * The gfs directory is expected to be locked by the caller prior to calling
820  * this function.  The directory may be unlocked during the execution of
821  * this function, but will be locked upon return from the function.
822  *
823  * This function returns 0 if a match is found, ENOENT if not.
824  */
825 static int
826 gfs_dir_lookup_static(int (*compare)(const char *, const char *),
827     gfs_dir_t *dp, const char *nm, vnode_t *dvp, int *idx,
828     vnode_t **vpp, pathname_t *rpnp)
829 {
830         gfs_dirent_t *ge;
831         vnode_t *vp = NULL;
832         int i;
833
834         ASSERT(GFS_DIR_LOCKED(dp));
835
836         /*
837          * Search static entries.
838          */
839         for (i = 0; i < dp->gfsd_nstatic; i++) {
840                 ge = &dp->gfsd_static[i];
841
842                 if (compare(ge->gfse_name, nm) == 0) {
843                         if (rpnp)
844                                 (void) strlcpy(rpnp->pn_buf, ge->gfse_name,
845                                     rpnp->pn_bufsize);
846
847                         if (ge->gfse_vnode) {
848                                 ASSERT(ge->gfse_flags & GFS_CACHE_VNODE);
849                                 vp = ge->gfse_vnode;
850                                 VN_HOLD(vp);
851                                 break;
852                         }
853
854                         /*
855                          * We drop the directory lock, as the constructor will
856                          * need to do KM_SLEEP allocations.  If we return from
857                          * the constructor only to find that a parallel
858                          * operation has completed, and GFS_CACHE_VNODE is set
859                          * for this entry, we discard the result in favor of
860                          * the cached vnode.
861                          */
862                         gfs_dir_unlock(dp);
863                         vp = ge->gfse_ctor(dvp);
864                         gfs_dir_lock(dp);
865
866                         ((gfs_file_t *)vp->v_data)->gfs_index = i;
867
868                         /* Set the inode according to the callback. */
869                         ((gfs_file_t *)vp->v_data)->gfs_ino =
870                             dp->gfsd_inode(dvp, i);
871
872                         if (ge->gfse_flags & GFS_CACHE_VNODE) {
873                                 if (ge->gfse_vnode == NULL) {
874                                         ge->gfse_vnode = vp;
875                                 } else {
876                                         /*
877                                          * A parallel constructor beat us to it;
878                                          * return existing vnode.  We have to be
879                                          * careful because we can't release the
880                                          * current vnode while holding the
881                                          * directory lock; its inactive routine
882                                          * will try to lock this directory.
883                                          */
884                                         vnode_t *oldvp = vp;
885                                         vp = ge->gfse_vnode;
886                                         VN_HOLD(vp);
887
888                                         gfs_dir_unlock(dp);
889                                         VN_RELE(oldvp);
890                                         gfs_dir_lock(dp);
891                                 }
892                         }
893                         break;
894                 }
895         }
896
897         if (vp == NULL)
898                 return (ENOENT);
899         else if (idx)
900                 *idx = i;
901         *vpp = vp;
902         return (0);
903 }
904
905 /*
906  * gfs_dir_lookup()
907  *
908  * Looks up the given name in the directory and returns the corresponding
909  * vnode, if found.
910  *
911  * First, we search statically defined entries, if any, with a call to
912  * gfs_dir_lookup_static().  If no static entry is found, and we have
913  * a callback function we try a dynamic lookup via gfs_dir_lookup_dynamic().
914  *
915  * This function returns 0 on success, non-zero on error.
916  */
917 int
918 gfs_dir_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, cred_t *cr,
919     int flags, int *direntflags, pathname_t *realpnp)
920 {
921         gfs_dir_t *dp = dvp->v_data;
922         boolean_t casecheck;
923         vnode_t *dynvp = NULL;
924         vnode_t *vp = NULL;
925         int (*compare)(const char *, const char *);
926         int error, idx;
927
928         ASSERT(dvp->v_type == VDIR);
929
930         if (gfs_lookup_dot(vpp, dvp, dp->gfsd_file.gfs_parent, nm) == 0)
931                 return (0);
932
933         casecheck = (flags & FIGNORECASE) != 0 && direntflags != NULL;
934         if (vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) ||
935             (flags & FIGNORECASE))
936                 compare = strcasecmp;
937         else
938                 compare = strcmp;
939
940         gfs_dir_lock(dp);
941
942         error = gfs_dir_lookup_static(compare, dp, nm, dvp, &idx, &vp, realpnp);
943
944         if (vp && casecheck) {
945                 gfs_dirent_t *ge;
946                 int i;
947
948                 for (i = idx + 1; i < dp->gfsd_nstatic; i++) {
949                         ge = &dp->gfsd_static[i];
950
951                         if (strcasecmp(ge->gfse_name, nm) == 0) {
952                                 *direntflags |= ED_CASE_CONFLICT;
953                                 goto out;
954                         }
955                 }
956         }
957
958         if ((error || casecheck) && dp->gfsd_lookup)
959                 error = gfs_dir_lookup_dynamic(dp->gfsd_lookup, dp, nm, dvp,
960                     &dynvp, cr, flags, direntflags, vp ? NULL : realpnp);
961
962         if (vp && dynvp) {
963                 /* static and dynamic entries are case-insensitive conflict */
964                 ASSERT(casecheck);
965                 *direntflags |= ED_CASE_CONFLICT;
966                 VN_RELE(dynvp);
967         } else if (vp == NULL) {
968                 vp = dynvp;
969         } else if (error == ENOENT) {
970                 error = 0;
971         } else if (error) {
972                 VN_RELE(vp);
973                 vp = NULL;
974         }
975
976 out:
977         gfs_dir_unlock(dp);
978
979         *vpp = vp;
980         return (error);
981 }
982
983 /*
984  * gfs_dir_readdir: does a readdir() on the given directory
985  *
986  *    dvp       - directory vnode
987  *    uiop      - uio structure
988  *    eofp      - eof pointer
989  *    data      - arbitrary data passed to readdir callback
990  *
991  * This routine does all the readdir() dirty work.  Even so, the caller must
992  * supply two callbacks in order to get full compatibility.
993  *
994  * If the directory contains static entries, an inode callback must be
995  * specified.  This avoids having to create every vnode and call VOP_GETATTR()
996  * when reading the directory.  This function has the following arguments:
997  *
998  *      ino_t gfs_inode_cb(vnode_t *vp, int index);
999  *
1000  *      vp      - vnode for the directory
1001  *      index   - index in original gfs_dirent_t array
1002  *
1003  *      Returns the inode number for the given entry.
1004  *
1005  * For directories with dynamic entries, a readdir callback must be provided.
1006  * This is significantly more complex, thanks to the particulars of
1007  * VOP_READDIR().
1008  *
1009  *      int gfs_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1010  *          offset_t *off, offset_t *nextoff, void *data, int flags)
1011  *
1012  *      vp      - directory vnode
1013  *      dp      - directory entry, sized according to maxlen given to
1014  *                gfs_dir_create().  callback must fill in d_name and
1015  *                d_ino (if a dirent64_t), or ed_name, ed_ino, and ed_eflags
1016  *                (if an edirent_t). edirent_t is used if V_RDDIR_ENTFLAGS
1017  *                is set in 'flags'.
1018  *      eofp    - callback must set to 1 when EOF has been reached
1019  *      off     - on entry, the last offset read from the directory.  Callback
1020  *                must set to the offset of the current entry, typically left
1021  *                untouched.
1022  *      nextoff - callback must set to offset of next entry.  Typically
1023  *                (off + 1)
1024  *      data    - caller-supplied data
1025  *      flags   - VOP_READDIR flags
1026  *
1027  *      Return 0 on success, or error on failure.
1028  */
1029 int
1030 gfs_dir_readdir(vnode_t *dvp, uio_t *uiop, int *eofp, int *ncookies,
1031     u_long **cookies, void *data, cred_t *cr, int flags)
1032 {
1033         gfs_readdir_state_t gstate;
1034         int error, eof = 0;
1035         ino64_t ino, pino;
1036         offset_t off, next;
1037         gfs_dir_t *dp = dvp->v_data;
1038
1039         error = gfs_get_parent_ino(dvp, cr, NULL, &pino, &ino);
1040         if (error)
1041                 return (error);
1042
1043         if ((error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1, uiop,
1044             pino, ino, flags)) != 0)
1045                 return (error);
1046
1047         while ((error = gfs_readdir_pred(&gstate, uiop, &off, ncookies,
1048             cookies)) == 0 && !eof) {
1049
1050                 if (off >= 0 && off < dp->gfsd_nstatic) {
1051                         ino = dp->gfsd_inode(dvp, off);
1052
1053                         if ((error = gfs_readdir_emit(&gstate, uiop,
1054                             off, ino, dp->gfsd_static[off].gfse_name, 0,
1055                             ncookies, cookies)) != 0)
1056                                 break;
1057
1058                 } else if (dp->gfsd_readdir) {
1059                         off -= dp->gfsd_nstatic;
1060
1061                         if ((error = dp->gfsd_readdir(dvp,
1062                             gstate.grd_dirent, &eof, &off, &next,
1063                             data, flags)) != 0 || eof)
1064                                 break;
1065
1066                         off += dp->gfsd_nstatic + 2;
1067                         next += dp->gfsd_nstatic + 2;
1068
1069                         if ((error = gfs_readdir_emit_int(&gstate, uiop,
1070                             next, ncookies, cookies)) != 0)
1071                                 break;
1072                 } else {
1073                         /*
1074                          * Offset is beyond the end of the static entries, and
1075                          * we have no dynamic entries.  Set EOF.
1076                          */
1077                         eof = 1;
1078                 }
1079         }
1080
1081         return (gfs_readdir_fini(&gstate, error, eofp, eof));
1082 }
1083
1084
1085 /*
1086  * gfs_vop_lookup: VOP_LOOKUP() entry point
1087  *
1088  * For use directly in vnode ops table.  Given a GFS directory, calls
1089  * gfs_dir_lookup() as necessary.
1090  */
1091 /* ARGSUSED */
1092 int
1093 gfs_vop_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
1094     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1095     int *direntflags, pathname_t *realpnp)
1096 {
1097         return (gfs_dir_lookup(dvp, nm, vpp, cr, flags, direntflags, realpnp));
1098 }
1099
1100 /*
1101  * gfs_vop_readdir: VOP_READDIR() entry point
1102  *
1103  * For use directly in vnode ops table.  Given a GFS directory, calls
1104  * gfs_dir_readdir() as necessary.
1105  */
1106 /* ARGSUSED */
1107 int
1108 gfs_vop_readdir(ap)
1109         struct vop_readdir_args /* {
1110                 struct vnode *a_vp;
1111                 struct uio *a_uio;
1112                 struct ucred *a_cred;
1113                 int *a_eofflag;
1114                 int *ncookies;
1115                 u_long **a_cookies;
1116         } */ *ap;
1117 {
1118         vnode_t *vp = ap->a_vp;
1119         uio_t *uiop = ap->a_uio;
1120         cred_t *cr = ap->a_cred;
1121         int *eofp = ap->a_eofflag;
1122         int ncookies = 0;
1123         u_long *cookies = NULL;
1124         int error;
1125
1126         if (ap->a_ncookies) {
1127                 /*
1128                  * Minimum entry size is dirent size and 1 byte for a file name.
1129                  */
1130                 ncookies = uiop->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
1131                 cookies = malloc(ncookies * sizeof(u_long), M_TEMP, M_WAITOK);
1132                 *ap->a_cookies = cookies;
1133                 *ap->a_ncookies = ncookies;
1134         }
1135
1136         error = gfs_dir_readdir(vp, uiop, eofp, &ncookies, &cookies, NULL,
1137             cr, 0);
1138
1139         if (error == 0) {
1140                 /* Subtract unused cookies */
1141                 if (ap->a_ncookies)
1142                         *ap->a_ncookies -= ncookies;
1143         } else if (ap->a_ncookies) {
1144                 free(*ap->a_cookies, M_TEMP);
1145                 *ap->a_cookies = NULL;
1146                 *ap->a_ncookies = 0;
1147         }
1148
1149         return (error);
1150 }
1151
1152
1153 #ifdef illumos
1154 /*
1155  * gfs_vop_map: VOP_MAP() entry point
1156  *
1157  * Convenient routine for handling pseudo-files that wish to allow mmap() calls.
1158  * This function only works for readonly files, and uses the read function for
1159  * the vnode to fill in the data.  The mapped data is immediately faulted in and
1160  * filled with the necessary data during this call; there are no getpage() or
1161  * putpage() routines.
1162  */
1163 /* ARGSUSED */
1164 int
1165 gfs_vop_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
1166     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cred,
1167     caller_context_t *ct)
1168 {
1169         int rv;
1170         ssize_t resid = len;
1171
1172         /*
1173          * Check for bad parameters
1174          */
1175 #ifdef _ILP32
1176         if (len > MAXOFF_T)
1177                 return (ENOMEM);
1178 #endif
1179         if (vp->v_flag & VNOMAP)
1180                 return (ENOTSUP);
1181         if (off > MAXOFF_T)
1182                 return (EFBIG);
1183         if ((long)off < 0 || (long)(off + len) < 0)
1184                 return (EINVAL);
1185         if (vp->v_type != VREG)
1186                 return (ENODEV);
1187         if ((prot & (PROT_EXEC | PROT_WRITE)) != 0)
1188                 return (EACCES);
1189
1190         /*
1191          * Find appropriate address if needed, otherwise clear address range.
1192          */
1193         as_rangelock(as);
1194         rv = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1195         if (rv != 0) {
1196                 as_rangeunlock(as);
1197                 return (rv);
1198         }
1199
1200         /*
1201          * Create mapping
1202          */
1203         rv = as_map(as, *addrp, len, segvn_create, zfod_argsp);
1204         as_rangeunlock(as);
1205         if (rv != 0)
1206                 return (rv);
1207
1208         /*
1209          * Fill with data from read()
1210          */
1211         rv = vn_rdwr(UIO_READ, vp, *addrp, len, off, UIO_USERSPACE,
1212             0, (rlim64_t)0, cred, &resid);
1213
1214         if (rv == 0 && resid != 0)
1215                 rv = ENXIO;
1216
1217         if (rv != 0) {
1218                 as_rangelock(as);
1219                 (void) as_unmap(as, *addrp, len);
1220                 as_rangeunlock(as);
1221         }
1222
1223         return (rv);
1224 }
1225 #endif  /* illumos */
1226
1227 /*
1228  * gfs_vop_reclaim: VOP_RECLAIM() entry point (solaris' VOP_INACTIVE())
1229  *
1230  * Given a vnode that is a GFS file or directory, call gfs_file_inactive() or
1231  * gfs_dir_inactive() as necessary, and kmem_free()s associated private data.
1232  */
1233 /* ARGSUSED */
1234 int
1235 gfs_vop_reclaim(ap)
1236         struct vop_reclaim_args /* {
1237                 struct vnode *a_vp;
1238                 struct thread *a_td;
1239         } */ *ap;
1240 {
1241         vnode_t *vp = ap->a_vp;
1242         gfs_file_t *fp = vp->v_data;
1243
1244         if (fp->gfs_type == GFS_DIR)
1245                 gfs_dir_inactive(vp);
1246         else
1247                 gfs_file_inactive(vp);
1248
1249         vnode_destroy_vobject(vp);
1250         VI_LOCK(vp);
1251         vp->v_data = NULL;
1252         VI_UNLOCK(vp);
1253         kmem_free(fp, fp->gfs_size);
1254
1255         return (0);
1256 }