4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 /* 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>
41 #include <sys/vnode.h>
44 #include <sys/unistd.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>
54 #include <sys/share.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.
85 * The associated operation is invalid (on this vnode).
94 * The associated operation is valid only for directories.
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.
109 fs_freevfs(vfs_t
*vfsp
)
115 fs_nosys_map(struct vnode
*vp
,
124 caller_context_t
*ct
)
131 fs_nosys_addmap(struct vnode
*vp
,
140 caller_context_t
*ct
)
147 fs_nosys_poll(vnode_t
*vp
,
148 register short events
,
150 register short *reventsp
,
151 struct pollhead
**phpp
,
152 caller_context_t
*ct
)
159 * The file system has nothing to sync to disk. However, the
160 * VFS_SYNC operation must not fail.
164 fs_sync(struct vfs
*vfspp
, short flag
, cred_t
*cr
)
170 * Does nothing but VOP_FSYNC must not fail.
174 fs_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
180 * Does nothing but VOP_PUTPAGE must not fail.
184 fs_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
185 caller_context_t
*ctp
)
191 * Does nothing but VOP_IOCTL must not fail.
195 fs_ioctl(vnode_t
*vp
, int com
, intptr_t data
, int flag
, cred_t
*cred
,
202 * Read/write lock/unlock. Does nothing.
206 fs_rwlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
213 fs_rwunlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
218 * Compare two vnodes.
222 fs_cmp(vnode_t
*vp1
, vnode_t
*vp2
, caller_context_t
*ct
)
228 * No-op seek operation.
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.
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
)
249 flk_callback_t serialize_callback
;
257 if (flag
& F_REMOTELOCK
) {
259 } else if (flag
& F_PXFSLOCK
) {
263 bfp
->l_pid
= ttoproc(curthread
)->p_pid
;
270 * Are NBMAND locks allowed on this file?
273 !(vp
->v_vfsp
->vfs_flag
& VFS_NBMAND
)) {
277 if (vp
->v_type
!= VREG
) {
284 if (flag
& F_REMOTELOCK
) {
285 frcmd
= SETFLCK
|RCMDLCK
;
286 } else if (flag
& F_PXFSLOCK
) {
287 frcmd
= SETFLCK
|PCMDLCK
;
290 bfp
->l_pid
= ttoproc(curthread
)->p_pid
;
293 if (cmd
== F_SETLK_NBMAND
&&
294 (bfp
->l_type
== F_RDLCK
|| bfp
->l_type
== F_WRLCK
)) {
298 if (nbl_need_check(vp
)) {
299 nbl_start_crit(vp
, RW_WRITER
);
301 if (frcmd
& NBMLCK
) {
302 mode
= (bfp
->l_type
== F_RDLCK
) ?
304 if (vn_is_mapped(vp
, mode
)) {
313 if (flag
& F_REMOTELOCK
) {
314 frcmd
= SETFLCK
|SLPFLCK
|RCMDLCK
;
315 } else if (flag
& F_PXFSLOCK
) {
316 frcmd
= SETFLCK
|SLPFLCK
|PCMDLCK
;
318 frcmd
= SETFLCK
|SLPFLCK
;
319 bfp
->l_pid
= ttoproc(curthread
)->p_pid
;
323 if (nbl_need_check(vp
)) {
324 nbl_start_crit(vp
, RW_WRITER
);
329 case F_HASREMOTELOCKS
:
330 nlmid
= GETNLMID(bfp
->l_sysid
);
331 if (nlmid
!= 0) { /* booted as a cluster */
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
);
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
);
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.
373 frlock_serialize_blocked(flk_cb_when_t when
, void *infop
)
375 vnode_t
*vp
= (vnode_t
*)infop
;
377 if (when
== FLK_BEFORE_SLEEP
)
380 nbl_start_crit(vp
, RW_WRITER
);
396 caller_context_t
*ct
)
402 * Return the answer requested to poll() for non-device files.
403 * Only POLLIN, POLLRDNORM, and POLLOUT are recognized.
405 struct pollhead fs_pollhd
;
410 register short events
,
412 register short *reventsp
,
413 struct pollhead
**phpp
,
414 caller_context_t
*ct
)
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
;
432 * POSIX pathconf() support.
441 caller_context_t
*ct
)
443 register ulong_t val
;
444 register int error
= 0;
445 struct statvfs64 vfsbuf
;
462 bzero(&vfsbuf
, sizeof (vfsbuf
));
463 if (error
= VFS_STATVFS(vp
->v_vfsp
, &vfsbuf
))
465 val
= vfsbuf
.f_namemax
;
469 case _PC_SYMLINK_MAX
:
478 if (vp
->v_vfsp
->vfs_flag
& VFS_NOTRUNC
)
479 val
= 1; /* NOTRUNC is enabled for vp */
485 val
= _POSIX_VDISABLE
;
488 case _PC_CHOWN_RESTRICTED
:
490 val
= rstchown
; /* chown restricted enabled */
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 */
507 case _PC_ACL_ENABLED
:
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
;
519 case _PC_SATTR_ENABLED
:
520 case _PC_SATTR_EXISTS
:
524 case _PC_ACCESS_FILTERING
:
549 caller_context_t
*ct
)
552 ASSERT(fl
== B_FREE
|| fl
== B_INVAL
);
557 page_destroy(pp
, dn
);
568 caller_context_t
*ct
)
570 cmn_err(CE_PANIC
, "fs_nodispose invoked");
574 * fabricate acls for file systems that do not support acls.
580 vsecattr_t
*vsecattr
,
583 caller_context_t
*ct
)
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
))
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 */
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 */
616 aclentp
->a_type
= OTHER_OBJ
; /* Other */
617 aclentp
->a_perm
= vattr
.va_mode
& 0007;
618 aclentp
->a_id
= (gid_t
)-1; /* Really undefined */
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
);
635 * Common code for implementing DOS share reservations
645 caller_context_t
*ct
)
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))
658 if (shr
->s_access
& (F_RMACC
| F_MDACC
))
660 if (shr
->s_deny
& (F_MANDDNY
| F_RMDNY
))
663 if (cmd
== F_SHARE_NBMAND
) {
664 /* make sure nbmand is allowed on the file */
666 !(vp
->v_vfsp
->vfs_flag
& VFS_NBMAND
)) {
669 if (vp
->v_type
!= VREG
) {
674 nbl_start_crit(vp
, RW_WRITER
);
679 shr
->s_deny
|= F_MANDDNY
;
682 error
= add_share(vp
, shr
);
686 error
= del_share(vp
, shr
);
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
);
709 fs_vnevent_nosupport(vnode_t
*vp
, vnevent_t e
, vnode_t
*dvp
, char *fnm
,
710 caller_context_t
*ct
)
718 fs_vnevent_support(vnode_t
*vp
, vnevent_t e
, vnode_t
*dvp
, char *fnm
,
719 caller_context_t
*ct
)
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
733 * <0 could not determine.
736 fs_acl_nontrivial(vnode_t
*vp
, cred_t
*cr
)
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;
758 /* select one of the styles as current flavor */
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
);
773 /* that flavor failed */
774 acl_styles
&= ~acl_flavor
;
777 /* if all styles fail then assume trivial */
781 /* process the flavor that worked */
783 if (acl_flavor
& _ACL_ACLENT_ENABLED
) {
784 if (vsecattr
.vsa_aclcnt
> MIN_ACL_ENTRIES
)
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
)
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.
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
)
838 if (fs_av_scan
&& vp
->v_type
== VREG
)
839 ret
= (*fs_av_scan
)(vp
, cr
, async
);
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
)
861 if (vp
== NULL
|| nvl
== NULL
)
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
;
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 */
890 mutex_init(&reparsed_door_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
894 reparse_door_get_handle()
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
;
906 mutex_exit(&reparsed_door_lock
);
911 reparse_door_reset_handle()
913 mutex_enter(&reparsed_door_lock
);
914 reparsed_door
= NULL
;
915 mutex_exit(&reparsed_door_lock
);
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
,
934 int err
, retries
, need_free
, retried_doorhd
;
935 size_t dlen
, res_len
;
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
||
945 /* get reparsed's door handle */
946 if ((rp_door
= reparse_door_get_handle()) == NULL
)
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
);
955 darg
= buf
; /* use same buffer for door's args & results */
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 */
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));
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
);
988 rp_door
= reparse_door_get_handle();
989 if (rp_door
!= NULL
) {
990 door_ki_hold(rp_door
);
999 door_ki_rele(rp_door
);
1002 kmem_free(darg
, dlen
); /* done with args buffer */
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
)
1021 bcopy(resp
->res_data
, buf
, res_len
);
1024 if (door_args
.rbuf
!= buf
)
1025 kmem_free(door_args
.rbuf
, door_args
.rsize
);