6334 Cannot unlink files when over quota
[illumos-gate.git] / usr / src / uts / common / fs / fs_subr.c
blob744d269716c373239aafa80c7b429aceba87af88
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
31 * Generic vnode operations.
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/fcntl.h>
38 #include <sys/flock.h>
39 #include <sys/statvfs.h>
40 #include <sys/vfs.h>
41 #include <sys/vnode.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/unistd.h>
45 #include <sys/cred.h>
46 #include <sys/poll.h>
47 #include <sys/debug.h>
48 #include <sys/cmn_err.h>
49 #include <sys/stream.h>
50 #include <fs/fs_subr.h>
51 #include <fs/fs_reparse.h>
52 #include <sys/door.h>
53 #include <sys/acl.h>
54 #include <sys/share.h>
55 #include <sys/file.h>
56 #include <sys/kmem.h>
57 #include <sys/file.h>
58 #include <sys/nbmlock.h>
59 #include <acl/acl_common.h>
60 #include <sys/pathname.h>
62 static callb_cpr_t *frlock_serialize_blocked(flk_cb_when_t, void *);
65 * Tunable to limit the number of retry to recover from STALE error.
67 int fs_estale_retry = 5;
70 * supports for reparse point door upcall
72 static door_handle_t reparsed_door;
73 static kmutex_t reparsed_door_lock;
76 * The associated operation is not supported by the file system.
78 int
79 fs_nosys()
81 return (ENOSYS);
85 * The associated operation is invalid (on this vnode).
87 int
88 fs_inval()
90 return (EINVAL);
94 * The associated operation is valid only for directories.
96 int
97 fs_notdir()
99 return (ENOTDIR);
103 * Free the file system specific resources. For the file systems that
104 * do not support the forced unmount, it will be a nop function.
107 /*ARGSUSED*/
108 void
109 fs_freevfs(vfs_t *vfsp)
113 /* ARGSUSED */
115 fs_nosys_map(struct vnode *vp,
116 offset_t off,
117 struct as *as,
118 caddr_t *addrp,
119 size_t len,
120 uchar_t prot,
121 uchar_t maxprot,
122 uint_t flags,
123 struct cred *cr,
124 caller_context_t *ct)
126 return (ENOSYS);
129 /* ARGSUSED */
131 fs_nosys_addmap(struct vnode *vp,
132 offset_t off,
133 struct as *as,
134 caddr_t addr,
135 size_t len,
136 uchar_t prot,
137 uchar_t maxprot,
138 uint_t flags,
139 struct cred *cr,
140 caller_context_t *ct)
142 return (ENOSYS);
145 /* ARGSUSED */
147 fs_nosys_poll(vnode_t *vp,
148 register short events,
149 int anyyet,
150 register short *reventsp,
151 struct pollhead **phpp,
152 caller_context_t *ct)
154 return (ENOSYS);
159 * The file system has nothing to sync to disk. However, the
160 * VFS_SYNC operation must not fail.
162 /* ARGSUSED */
164 fs_sync(struct vfs *vfspp, short flag, cred_t *cr)
166 return (0);
170 * Does nothing but VOP_FSYNC must not fail.
172 /* ARGSUSED */
174 fs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
176 return (0);
180 * Does nothing but VOP_PUTPAGE must not fail.
182 /* ARGSUSED */
184 fs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
185 caller_context_t *ctp)
187 return (0);
191 * Does nothing but VOP_IOCTL must not fail.
193 /* ARGSUSED */
195 fs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
196 int *rvalp)
198 return (0);
202 * Read/write lock/unlock. Does nothing.
204 /* ARGSUSED */
206 fs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
208 return (-1);
211 /* ARGSUSED */
212 void
213 fs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
218 * Compare two vnodes.
220 /*ARGSUSED2*/
222 fs_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
224 return (vp1 == vp2);
228 * No-op seek operation.
230 /* ARGSUSED */
232 fs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
234 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
238 * File and record locking.
240 /* ARGSUSED */
242 fs_frlock(register vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
243 offset_t offset, flk_callback_t *flk_cbp, cred_t *cr,
244 caller_context_t *ct)
246 int frcmd;
247 int nlmid;
248 int error = 0;
249 flk_callback_t serialize_callback;
250 int serialize = 0;
251 v_mode_t mode;
253 switch (cmd) {
255 case F_GETLK:
256 case F_O_GETLK:
257 if (flag & F_REMOTELOCK) {
258 frcmd = RCMDLCK;
259 } else if (flag & F_PXFSLOCK) {
260 frcmd = PCMDLCK;
261 } else {
262 frcmd = 0;
263 bfp->l_pid = ttoproc(curthread)->p_pid;
264 bfp->l_sysid = 0;
266 break;
268 case F_SETLK_NBMAND:
270 * Are NBMAND locks allowed on this file?
272 if (!vp->v_vfsp ||
273 !(vp->v_vfsp->vfs_flag & VFS_NBMAND)) {
274 error = EINVAL;
275 goto done;
277 if (vp->v_type != VREG) {
278 error = EINVAL;
279 goto done;
281 /*FALLTHROUGH*/
283 case F_SETLK:
284 if (flag & F_REMOTELOCK) {
285 frcmd = SETFLCK|RCMDLCK;
286 } else if (flag & F_PXFSLOCK) {
287 frcmd = SETFLCK|PCMDLCK;
288 } else {
289 frcmd = SETFLCK;
290 bfp->l_pid = ttoproc(curthread)->p_pid;
291 bfp->l_sysid = 0;
293 if (cmd == F_SETLK_NBMAND &&
294 (bfp->l_type == F_RDLCK || bfp->l_type == F_WRLCK)) {
295 frcmd |= NBMLCK;
298 if (nbl_need_check(vp)) {
299 nbl_start_crit(vp, RW_WRITER);
300 serialize = 1;
301 if (frcmd & NBMLCK) {
302 mode = (bfp->l_type == F_RDLCK) ?
303 V_READ : V_RDANDWR;
304 if (vn_is_mapped(vp, mode)) {
305 error = EAGAIN;
306 goto done;
310 break;
312 case F_SETLKW:
313 if (flag & F_REMOTELOCK) {
314 frcmd = SETFLCK|SLPFLCK|RCMDLCK;
315 } else if (flag & F_PXFSLOCK) {
316 frcmd = SETFLCK|SLPFLCK|PCMDLCK;
317 } else {
318 frcmd = SETFLCK|SLPFLCK;
319 bfp->l_pid = ttoproc(curthread)->p_pid;
320 bfp->l_sysid = 0;
323 if (nbl_need_check(vp)) {
324 nbl_start_crit(vp, RW_WRITER);
325 serialize = 1;
327 break;
329 case F_HASREMOTELOCKS:
330 nlmid = GETNLMID(bfp->l_sysid);
331 if (nlmid != 0) { /* booted as a cluster */
332 l_has_rmt(bfp) =
333 cl_flk_has_remote_locks_for_nlmid(vp, nlmid);
334 } else { /* not booted as a cluster */
335 l_has_rmt(bfp) = flk_has_remote_locks(vp);
338 goto done;
340 default:
341 error = EINVAL;
342 goto done;
346 * If this is a blocking lock request and we're serializing lock
347 * requests, modify the callback list to leave the critical region
348 * while we're waiting for the lock.
351 if (serialize && (frcmd & SLPFLCK) != 0) {
352 flk_add_callback(&serialize_callback,
353 frlock_serialize_blocked, vp, flk_cbp);
354 flk_cbp = &serialize_callback;
357 error = reclock(vp, bfp, frcmd, flag, offset, flk_cbp);
359 done:
360 if (serialize)
361 nbl_end_crit(vp);
363 return (error);
367 * Callback when a lock request blocks and we are serializing requests. If
368 * before sleeping, leave the critical region. If after wakeup, reenter
369 * the critical region.
372 static callb_cpr_t *
373 frlock_serialize_blocked(flk_cb_when_t when, void *infop)
375 vnode_t *vp = (vnode_t *)infop;
377 if (when == FLK_BEFORE_SLEEP)
378 nbl_end_crit(vp);
379 else {
380 nbl_start_crit(vp, RW_WRITER);
383 return (NULL);
387 * Allow any flags.
389 /* ARGSUSED */
391 fs_setfl(
392 vnode_t *vp,
393 int oflags,
394 int nflags,
395 cred_t *cr,
396 caller_context_t *ct)
398 return (0);
402 * Return the answer requested to poll() for non-device files.
403 * Only POLLIN, POLLRDNORM, and POLLOUT are recognized.
405 struct pollhead fs_pollhd;
407 /* ARGSUSED */
409 fs_poll(vnode_t *vp,
410 register short events,
411 int anyyet,
412 register short *reventsp,
413 struct pollhead **phpp,
414 caller_context_t *ct)
416 *reventsp = 0;
417 if (events & POLLIN)
418 *reventsp |= POLLIN;
419 if (events & POLLRDNORM)
420 *reventsp |= POLLRDNORM;
421 if (events & POLLRDBAND)
422 *reventsp |= POLLRDBAND;
423 if (events & POLLOUT)
424 *reventsp |= POLLOUT;
425 if (events & POLLWRBAND)
426 *reventsp |= POLLWRBAND;
427 *phpp = !anyyet && !*reventsp ? &fs_pollhd : (struct pollhead *)NULL;
428 return (0);
432 * POSIX pathconf() support.
434 /* ARGSUSED */
436 fs_pathconf(
437 vnode_t *vp,
438 int cmd,
439 ulong_t *valp,
440 cred_t *cr,
441 caller_context_t *ct)
443 register ulong_t val;
444 register int error = 0;
445 struct statvfs64 vfsbuf;
447 switch (cmd) {
449 case _PC_LINK_MAX:
450 val = MAXLINK;
451 break;
453 case _PC_MAX_CANON:
454 val = MAX_CANON;
455 break;
457 case _PC_MAX_INPUT:
458 val = MAX_INPUT;
459 break;
461 case _PC_NAME_MAX:
462 bzero(&vfsbuf, sizeof (vfsbuf));
463 if (error = VFS_STATVFS(vp->v_vfsp, &vfsbuf))
464 break;
465 val = vfsbuf.f_namemax;
466 break;
468 case _PC_PATH_MAX:
469 case _PC_SYMLINK_MAX:
470 val = MAXPATHLEN;
471 break;
473 case _PC_PIPE_BUF:
474 val = PIPE_BUF;
475 break;
477 case _PC_NO_TRUNC:
478 if (vp->v_vfsp->vfs_flag & VFS_NOTRUNC)
479 val = 1; /* NOTRUNC is enabled for vp */
480 else
481 val = (ulong_t)-1;
482 break;
484 case _PC_VDISABLE:
485 val = _POSIX_VDISABLE;
486 break;
488 case _PC_CHOWN_RESTRICTED:
489 if (rstchown)
490 val = rstchown; /* chown restricted enabled */
491 else
492 val = (ulong_t)-1;
493 break;
495 case _PC_FILESIZEBITS:
498 * If ever we come here it means that underlying file system
499 * does not recognise the command and therefore this
500 * configurable limit cannot be determined. We return -1
501 * and don't change errno.
504 val = (ulong_t)-1; /* large file support */
505 break;
507 case _PC_ACL_ENABLED:
508 val = 0;
509 break;
511 case _PC_CASE_BEHAVIOR:
512 val = _CASE_SENSITIVE;
513 if (vfs_has_feature(vp->v_vfsp, VFSFT_CASEINSENSITIVE) == 1)
514 val |= _CASE_INSENSITIVE;
515 if (vfs_has_feature(vp->v_vfsp, VFSFT_NOCASESENSITIVE) == 1)
516 val &= ~_CASE_SENSITIVE;
517 break;
519 case _PC_SATTR_ENABLED:
520 case _PC_SATTR_EXISTS:
521 val = 0;
522 break;
524 case _PC_ACCESS_FILTERING:
525 val = 0;
526 break;
528 default:
529 error = EINVAL;
530 break;
533 if (error == 0)
534 *valp = val;
535 return (error);
539 * Dispose of a page.
541 /* ARGSUSED */
542 void
543 fs_dispose(
544 struct vnode *vp,
545 page_t *pp,
546 int fl,
547 int dn,
548 struct cred *cr,
549 caller_context_t *ct)
552 ASSERT(fl == B_FREE || fl == B_INVAL);
554 if (fl == B_FREE)
555 page_free(pp, dn);
556 else
557 page_destroy(pp, dn);
560 /* ARGSUSED */
561 void
562 fs_nodispose(
563 struct vnode *vp,
564 page_t *pp,
565 int fl,
566 int dn,
567 struct cred *cr,
568 caller_context_t *ct)
570 cmn_err(CE_PANIC, "fs_nodispose invoked");
574 * fabricate acls for file systems that do not support acls.
576 /* ARGSUSED */
578 fs_fab_acl(
579 vnode_t *vp,
580 vsecattr_t *vsecattr,
581 int flag,
582 cred_t *cr,
583 caller_context_t *ct)
585 aclent_t *aclentp;
586 struct vattr vattr;
587 int error;
588 size_t aclsize;
590 vsecattr->vsa_aclcnt = 0;
591 vsecattr->vsa_aclentsz = 0;
592 vsecattr->vsa_aclentp = NULL;
593 vsecattr->vsa_dfaclcnt = 0; /* Default ACLs are not fabricated */
594 vsecattr->vsa_dfaclentp = NULL;
596 vattr.va_mask = AT_MODE | AT_UID | AT_GID;
597 if (error = VOP_GETATTR(vp, &vattr, 0, cr, ct))
598 return (error);
600 if (vsecattr->vsa_mask & (VSA_ACLCNT | VSA_ACL)) {
601 aclsize = 4 * sizeof (aclent_t);
602 vsecattr->vsa_aclcnt = 4; /* USER, GROUP, OTHER, and CLASS */
603 vsecattr->vsa_aclentp = kmem_zalloc(aclsize, KM_SLEEP);
604 aclentp = vsecattr->vsa_aclentp;
606 aclentp->a_type = USER_OBJ; /* Owner */
607 aclentp->a_perm = ((ushort_t)(vattr.va_mode & 0700)) >> 6;
608 aclentp->a_id = vattr.va_uid; /* Really undefined */
609 aclentp++;
611 aclentp->a_type = GROUP_OBJ; /* Group */
612 aclentp->a_perm = ((ushort_t)(vattr.va_mode & 0070)) >> 3;
613 aclentp->a_id = vattr.va_gid; /* Really undefined */
614 aclentp++;
616 aclentp->a_type = OTHER_OBJ; /* Other */
617 aclentp->a_perm = vattr.va_mode & 0007;
618 aclentp->a_id = (gid_t)-1; /* Really undefined */
619 aclentp++;
621 aclentp->a_type = CLASS_OBJ; /* Class */
622 aclentp->a_perm = (ushort_t)(0007);
623 aclentp->a_id = (gid_t)-1; /* Really undefined */
624 } else if (vsecattr->vsa_mask & (VSA_ACECNT | VSA_ACE)) {
625 VERIFY(0 == acl_trivial_create(vattr.va_mode,
626 (vp->v_type == VDIR), (ace_t **)&vsecattr->vsa_aclentp,
627 &vsecattr->vsa_aclcnt));
628 vsecattr->vsa_aclentsz = vsecattr->vsa_aclcnt * sizeof (ace_t);
631 return (error);
635 * Common code for implementing DOS share reservations
637 /* ARGSUSED4 */
639 fs_shrlock(
640 struct vnode *vp,
641 int cmd,
642 struct shrlock *shr,
643 int flag,
644 cred_t *cr,
645 caller_context_t *ct)
647 int error;
650 * Make sure that the file was opened with permissions appropriate
651 * for the request, and make sure the caller isn't trying to sneak
652 * in an NBMAND request.
654 if (cmd == F_SHARE) {
655 if (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
656 ((shr->s_access & F_WRACC) && (flag & FWRITE) == 0))
657 return (EBADF);
658 if (shr->s_access & (F_RMACC | F_MDACC))
659 return (EINVAL);
660 if (shr->s_deny & (F_MANDDNY | F_RMDNY))
661 return (EINVAL);
663 if (cmd == F_SHARE_NBMAND) {
664 /* make sure nbmand is allowed on the file */
665 if (!vp->v_vfsp ||
666 !(vp->v_vfsp->vfs_flag & VFS_NBMAND)) {
667 return (EINVAL);
669 if (vp->v_type != VREG) {
670 return (EINVAL);
674 nbl_start_crit(vp, RW_WRITER);
676 switch (cmd) {
678 case F_SHARE_NBMAND:
679 shr->s_deny |= F_MANDDNY;
680 /*FALLTHROUGH*/
681 case F_SHARE:
682 error = add_share(vp, shr);
683 break;
685 case F_UNSHARE:
686 error = del_share(vp, shr);
687 break;
689 case F_HASREMOTELOCKS:
691 * We are overloading this command to refer to remote
692 * shares as well as remote locks, despite its name.
694 shr->s_access = shr_has_remote_shares(vp, shr->s_sysid);
695 error = 0;
696 break;
698 default:
699 error = EINVAL;
700 break;
703 nbl_end_crit(vp);
704 return (error);
707 /*ARGSUSED1*/
709 fs_vnevent_nosupport(vnode_t *vp, vnevent_t e, vnode_t *dvp, char *fnm,
710 caller_context_t *ct)
712 ASSERT(vp != NULL);
713 return (ENOTSUP);
716 /*ARGSUSED1*/
718 fs_vnevent_support(vnode_t *vp, vnevent_t e, vnode_t *dvp, char *fnm,
719 caller_context_t *ct)
721 ASSERT(vp != NULL);
722 return (0);
726 * return 1 for non-trivial ACL.
728 * NB: It is not necessary for the caller to VOP_RWLOCK since
729 * we only issue VOP_GETSECATTR.
731 * Returns 0 == trivial
732 * 1 == NOT Trivial
733 * <0 could not determine.
736 fs_acl_nontrivial(vnode_t *vp, cred_t *cr)
738 ulong_t acl_styles;
739 ulong_t acl_flavor;
740 vsecattr_t vsecattr;
741 int error;
742 int isnontrivial;
744 /* determine the forms of ACLs maintained */
745 error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &acl_styles, cr, NULL);
747 /* clear bits we don't understand and establish default acl_style */
748 acl_styles &= (_ACL_ACLENT_ENABLED | _ACL_ACE_ENABLED);
749 if (error || (acl_styles == 0))
750 acl_styles = _ACL_ACLENT_ENABLED;
752 vsecattr.vsa_aclentp = NULL;
753 vsecattr.vsa_dfaclentp = NULL;
754 vsecattr.vsa_aclcnt = 0;
755 vsecattr.vsa_dfaclcnt = 0;
757 while (acl_styles) {
758 /* select one of the styles as current flavor */
759 acl_flavor = 0;
760 if (acl_styles & _ACL_ACLENT_ENABLED) {
761 acl_flavor = _ACL_ACLENT_ENABLED;
762 vsecattr.vsa_mask = VSA_ACLCNT | VSA_DFACLCNT;
763 } else if (acl_styles & _ACL_ACE_ENABLED) {
764 acl_flavor = _ACL_ACE_ENABLED;
765 vsecattr.vsa_mask = VSA_ACECNT | VSA_ACE;
768 ASSERT(vsecattr.vsa_mask && acl_flavor);
769 error = VOP_GETSECATTR(vp, &vsecattr, 0, cr, NULL);
770 if (error == 0)
771 break;
773 /* that flavor failed */
774 acl_styles &= ~acl_flavor;
777 /* if all styles fail then assume trivial */
778 if (acl_styles == 0)
779 return (0);
781 /* process the flavor that worked */
782 isnontrivial = 0;
783 if (acl_flavor & _ACL_ACLENT_ENABLED) {
784 if (vsecattr.vsa_aclcnt > MIN_ACL_ENTRIES)
785 isnontrivial = 1;
786 if (vsecattr.vsa_aclcnt && vsecattr.vsa_aclentp != NULL)
787 kmem_free(vsecattr.vsa_aclentp,
788 vsecattr.vsa_aclcnt * sizeof (aclent_t));
789 if (vsecattr.vsa_dfaclcnt && vsecattr.vsa_dfaclentp != NULL)
790 kmem_free(vsecattr.vsa_dfaclentp,
791 vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
793 if (acl_flavor & _ACL_ACE_ENABLED) {
794 isnontrivial = ace_trivial(vsecattr.vsa_aclentp,
795 vsecattr.vsa_aclcnt);
797 if (vsecattr.vsa_aclcnt && vsecattr.vsa_aclentp != NULL)
798 kmem_free(vsecattr.vsa_aclentp,
799 vsecattr.vsa_aclcnt * sizeof (ace_t));
800 /* ACE has no vsecattr.vsa_dfaclcnt */
802 return (isnontrivial);
806 * Check whether we need a retry to recover from STALE error.
809 fs_need_estale_retry(int retry_count)
811 if (retry_count < fs_estale_retry)
812 return (1);
813 else
814 return (0);
818 static int (*fs_av_scan)(vnode_t *, cred_t *, int) = NULL;
821 * Routine for anti-virus scanner to call to register its scanning routine.
823 void
824 fs_vscan_register(int (*av_scan)(vnode_t *, cred_t *, int))
826 fs_av_scan = av_scan;
830 * Routine for file systems to call to initiate anti-virus scanning.
831 * Scanning will only be done on REGular files (currently).
834 fs_vscan(vnode_t *vp, cred_t *cr, int async)
836 int ret = 0;
838 if (fs_av_scan && vp->v_type == VREG)
839 ret = (*fs_av_scan)(vp, cr, async);
841 return (ret);
845 * support functions for reparse point
848 * reparse_vnode_parse
850 * Read the symlink data of a reparse point specified by the vnode
851 * and return the reparse data as name-value pair in the nvlist.
854 reparse_vnode_parse(vnode_t *vp, nvlist_t *nvl)
856 int err;
857 char *lkdata;
858 struct uio uio;
859 struct iovec iov;
861 if (vp == NULL || nvl == NULL)
862 return (EINVAL);
864 lkdata = kmem_alloc(MAXREPARSELEN, KM_SLEEP);
867 * Set up io vector to read sym link data
869 iov.iov_base = lkdata;
870 iov.iov_len = MAXREPARSELEN;
871 uio.uio_iov = &iov;
872 uio.uio_iovcnt = 1;
873 uio.uio_segflg = UIO_SYSSPACE;
874 uio.uio_extflg = UIO_COPY_CACHED;
875 uio.uio_loffset = (offset_t)0;
876 uio.uio_resid = MAXREPARSELEN;
878 if ((err = VOP_READLINK(vp, &uio, kcred, NULL)) == 0) {
879 *(lkdata + MAXREPARSELEN - uio.uio_resid) = '\0';
880 err = reparse_parse(lkdata, nvl);
882 kmem_free(lkdata, MAXREPARSELEN); /* done with lkdata */
884 return (err);
887 void
888 reparse_point_init()
890 mutex_init(&reparsed_door_lock, NULL, MUTEX_DEFAULT, NULL);
893 static door_handle_t
894 reparse_door_get_handle()
896 door_handle_t dh;
898 mutex_enter(&reparsed_door_lock);
899 if ((dh = reparsed_door) == NULL) {
900 if (door_ki_open(REPARSED_DOOR, &reparsed_door) != 0) {
901 reparsed_door = NULL;
902 dh = NULL;
903 } else
904 dh = reparsed_door;
906 mutex_exit(&reparsed_door_lock);
907 return (dh);
910 static void
911 reparse_door_reset_handle()
913 mutex_enter(&reparsed_door_lock);
914 reparsed_door = NULL;
915 mutex_exit(&reparsed_door_lock);
919 * reparse_kderef
921 * Accepts the service-specific item from the reparse point and returns
922 * the service-specific data requested. The caller specifies the size of
923 * the buffer provided via *bufsz; the routine will fail with EOVERFLOW
924 * if the results will not fit in the buffer, in which case, *bufsz will
925 * contain the number of bytes needed to hold the results.
927 * if ok return 0 and update *bufsize with length of actual result
928 * else return error code.
931 reparse_kderef(const char *svc_type, const char *svc_data, char *buf,
932 size_t *bufsize)
934 int err, retries, need_free, retried_doorhd;
935 size_t dlen, res_len;
936 char *darg;
937 door_arg_t door_args;
938 reparsed_door_res_t *resp;
939 door_handle_t rp_door;
941 if (svc_type == NULL || svc_data == NULL || buf == NULL ||
942 bufsize == NULL)
943 return (EINVAL);
945 /* get reparsed's door handle */
946 if ((rp_door = reparse_door_get_handle()) == NULL)
947 return (EBADF);
949 /* setup buffer for door_call args and results */
950 dlen = strlen(svc_type) + strlen(svc_data) + 2;
951 if (*bufsize < dlen) {
952 darg = kmem_alloc(dlen, KM_SLEEP);
953 need_free = 1;
954 } else {
955 darg = buf; /* use same buffer for door's args & results */
956 need_free = 0;
959 /* build argument string of door call */
960 (void) snprintf(darg, dlen, "%s:%s", svc_type, svc_data);
962 /* setup args for door call */
963 door_args.data_ptr = darg;
964 door_args.data_size = dlen;
965 door_args.desc_ptr = NULL;
966 door_args.desc_num = 0;
967 door_args.rbuf = buf;
968 door_args.rsize = *bufsize;
970 /* do the door_call */
971 retried_doorhd = 0;
972 retries = 0;
973 door_ki_hold(rp_door);
974 while ((err = door_ki_upcall_limited(rp_door, &door_args,
975 NULL, SIZE_MAX, 0)) != 0) {
976 if (err == EAGAIN || err == EINTR) {
977 if (++retries < REPARSED_DOORCALL_MAX_RETRY) {
978 delay(SEC_TO_TICK(1));
979 continue;
981 } else if (err == EBADF) {
982 /* door server goes away... */
983 reparse_door_reset_handle();
985 if (retried_doorhd == 0) {
986 door_ki_rele(rp_door);
987 retried_doorhd++;
988 rp_door = reparse_door_get_handle();
989 if (rp_door != NULL) {
990 door_ki_hold(rp_door);
991 continue;
995 break;
998 if (rp_door)
999 door_ki_rele(rp_door);
1001 if (need_free)
1002 kmem_free(darg, dlen); /* done with args buffer */
1004 if (err != 0)
1005 return (err);
1007 resp = (reparsed_door_res_t *)door_args.rbuf;
1008 if ((err = resp->res_status) == 0) {
1010 * have to save the length of the results before the
1011 * bcopy below since it's can be an overlap copy that
1012 * overwrites the reparsed_door_res_t structure at
1013 * the beginning of the buffer.
1015 res_len = (size_t)resp->res_len;
1017 /* deref call is ok */
1018 if (res_len > *bufsize)
1019 err = EOVERFLOW;
1020 else
1021 bcopy(resp->res_data, buf, res_len);
1022 *bufsize = res_len;
1024 if (door_args.rbuf != buf)
1025 kmem_free(door_args.rbuf, door_args.rsize);
1027 return (err);