10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53898Srsb * Common Development and Distribution License (the "License").
63898Srsb * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
223898Srsb * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/time.h>
310Sstevel@tonic-gate #include <sys/cred.h>
320Sstevel@tonic-gate #include <sys/vfs.h>
333898Srsb #include <sys/vfs_opreg.h>
340Sstevel@tonic-gate #include <sys/gfs.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <fs/fs_subr.h>
400Sstevel@tonic-gate #include <sys/contract.h>
410Sstevel@tonic-gate #include <sys/contract_impl.h>
420Sstevel@tonic-gate #include <sys/ctfs.h>
430Sstevel@tonic-gate #include <sys/ctfs_impl.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/pathname.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * Entries in a /system/contract/<type>/<ctid> directory.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate static gfs_dirent_t ctfs_ctls[] = {
510Sstevel@tonic-gate { "ctl", ctfs_create_ctlnode, GFS_CACHE_VNODE, },
520Sstevel@tonic-gate { "status", ctfs_create_statnode, GFS_CACHE_VNODE },
530Sstevel@tonic-gate { "events", ctfs_create_evnode },
540Sstevel@tonic-gate { NULL }
550Sstevel@tonic-gate };
560Sstevel@tonic-gate #define CTFS_NCTLS ((sizeof ctfs_ctls / sizeof (gfs_dirent_t)) - 1)
570Sstevel@tonic-gate
580Sstevel@tonic-gate static ino64_t ctfs_cdir_do_inode(vnode_t *, int);
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * ctfs_create_cdirnode
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * If necessary, creates a cdirnode for the specified contract and
640Sstevel@tonic-gate * inserts it into the contract's list of vnodes. Returns either the
650Sstevel@tonic-gate * existing vnode or the new one.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate vnode_t *
ctfs_create_cdirnode(vnode_t * pvp,contract_t * ct)680Sstevel@tonic-gate ctfs_create_cdirnode(vnode_t *pvp, contract_t *ct)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate vnode_t *vp;
710Sstevel@tonic-gate ctfs_cdirnode_t *cdir;
720Sstevel@tonic-gate
730Sstevel@tonic-gate if ((vp = contract_vnode_get(ct, pvp->v_vfsp)) != NULL)
740Sstevel@tonic-gate return (vp);
750Sstevel@tonic-gate
760Sstevel@tonic-gate vp = gfs_dir_create(sizeof (ctfs_cdirnode_t), pvp, ctfs_ops_cdir,
770Sstevel@tonic-gate ctfs_ctls, ctfs_cdir_do_inode, CTFS_NAME_MAX, NULL, NULL);
780Sstevel@tonic-gate cdir = vp->v_data;
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * We must set the inode because this is called explicitly rather than
820Sstevel@tonic-gate * through GFS callbacks.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate gfs_file_set_inode(vp, CTFS_INO_CT_DIR(ct->ct_id));
850Sstevel@tonic-gate
860Sstevel@tonic-gate cdir->ctfs_cn_contract = ct;
870Sstevel@tonic-gate contract_hold(ct);
880Sstevel@tonic-gate contract_vnode_set(ct, &cdir->ctfs_cn_linkage, vp);
890Sstevel@tonic-gate
900Sstevel@tonic-gate return (vp);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * ctfs_cdir_getattr - VOP_GETATTR entry point
950Sstevel@tonic-gate */
960Sstevel@tonic-gate /* ARGSUSED */
970Sstevel@tonic-gate static int
ctfs_cdir_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)98*5331Samw ctfs_cdir_getattr(
99*5331Samw vnode_t *vp,
100*5331Samw vattr_t *vap,
101*5331Samw int flags,
102*5331Samw cred_t *cr,
103*5331Samw caller_context_t *ct)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate ctfs_cdirnode_t *cdirnode = vp->v_data;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate vap->va_type = VDIR;
1080Sstevel@tonic-gate vap->va_mode = 0555;
1090Sstevel@tonic-gate vap->va_nlink = 2 + CTFS_NCTLS;
1100Sstevel@tonic-gate vap->va_size = vap->va_nlink;
1110Sstevel@tonic-gate vap->va_ctime = cdirnode->ctfs_cn_contract->ct_ctime;
1120Sstevel@tonic-gate mutex_enter(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
1130Sstevel@tonic-gate vap->va_atime = vap->va_mtime =
1140Sstevel@tonic-gate cdirnode->ctfs_cn_contract->ct_events.ctq_atime;
1150Sstevel@tonic-gate mutex_exit(&cdirnode->ctfs_cn_contract->ct_events.ctq_lock);
1160Sstevel@tonic-gate ctfs_common_getattr(vp, vap);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate return (0);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * ctfs_cdir_do_inode - return inode number based on static index
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate static ino64_t
ctfs_cdir_do_inode(vnode_t * vp,int index)1250Sstevel@tonic-gate ctfs_cdir_do_inode(vnode_t *vp, int index)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate ctfs_cdirnode_t *cdirnode = vp->v_data;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate return (CTFS_INO_CT_FILE(cdirnode->ctfs_cn_contract->ct_id, index));
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * ctfs_cdir_inactive - VOP_INACTIVE entry point
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate /* ARGSUSED */
1360Sstevel@tonic-gate static void
ctfs_cdir_inactive(vnode_t * vp,cred_t * cr,caller_context_t * cct)137*5331Samw ctfs_cdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *cct)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate ctfs_cdirnode_t *cdirnode = vp->v_data;
1400Sstevel@tonic-gate contract_t *ct = cdirnode->ctfs_cn_contract;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate mutex_enter(&ct->ct_lock);
1430Sstevel@tonic-gate if (gfs_dir_inactive(vp) == NULL) {
1440Sstevel@tonic-gate mutex_exit(&ct->ct_lock);
1450Sstevel@tonic-gate return;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate list_remove(&ct->ct_vnodes, &cdirnode->ctfs_cn_linkage);
1490Sstevel@tonic-gate mutex_exit(&ct->ct_lock);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate contract_rele(ct);
1520Sstevel@tonic-gate kmem_free(cdirnode, sizeof (ctfs_cdirnode_t));
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_cdir[] = {
1573898Srsb { VOPNAME_OPEN, { .vop_open = ctfs_open } },
1583898Srsb { VOPNAME_CLOSE, { .vop_close = ctfs_close } },
1593898Srsb { VOPNAME_IOCTL, { .error = fs_inval } },
1603898Srsb { VOPNAME_GETATTR, { .vop_getattr = ctfs_cdir_getattr } },
1613898Srsb { VOPNAME_ACCESS, { .vop_access = ctfs_access_dir } },
1623898Srsb { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
1633898Srsb { VOPNAME_LOOKUP, { .vop_lookup = gfs_vop_lookup } },
1643898Srsb { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1653898Srsb { VOPNAME_INACTIVE, { .vop_inactive = ctfs_cdir_inactive } },
1660Sstevel@tonic-gate { NULL, NULL }
1670Sstevel@tonic-gate };
168