17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*da6c28aaSamw * Common Development and Distribution License (the "License"). 6*da6c28aaSamw * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*da6c28aaSamw * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*da6c28aaSamw * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Utility routines and top-level conflict detection code for NBMAND 307c478bd9Sstevel@tonic-gate * locks. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h> 347c478bd9Sstevel@tonic-gate #include <sys/rwlock.h> 357c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 397c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * Enter the critical region for synchronizing I/O requests with lock/share 437c478bd9Sstevel@tonic-gate * requests. "mode" specifies whether the caller intends to update 447c478bd9Sstevel@tonic-gate * lock/share state (as opposed to just query it). 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate void 487c478bd9Sstevel@tonic-gate nbl_start_crit(vnode_t *vp, krw_t mode) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate rw_enter(&vp->v_nbllock, mode); 517c478bd9Sstevel@tonic-gate } 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * Leave the critical region. 557c478bd9Sstevel@tonic-gate */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate void 587c478bd9Sstevel@tonic-gate nbl_end_crit(vnode_t *vp) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate rw_exit(&vp->v_nbllock); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * Return non-zero if some thread is in the critical region. 657c478bd9Sstevel@tonic-gate * Note that this is appropriate for use in ASSERT()s only. 667c478bd9Sstevel@tonic-gate */ 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate int 697c478bd9Sstevel@tonic-gate nbl_in_crit(vnode_t *vp) 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate return (RW_LOCK_HELD(&vp->v_nbllock)); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* 757c478bd9Sstevel@tonic-gate * Return the nbl_op_t that corresponds to the given lock type (read or 767c478bd9Sstevel@tonic-gate * write). 777c478bd9Sstevel@tonic-gate */ 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate nbl_op_t 807c478bd9Sstevel@tonic-gate nbl_lock_to_op(int lock_type) 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate int level; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate switch (lock_type) { 857c478bd9Sstevel@tonic-gate case F_WRLCK: 867c478bd9Sstevel@tonic-gate return (NBL_READWRITE); 877c478bd9Sstevel@tonic-gate case F_RDLCK: 887c478bd9Sstevel@tonic-gate return (NBL_READ); 897c478bd9Sstevel@tonic-gate default: 907c478bd9Sstevel@tonic-gate #ifdef DEBUG 917c478bd9Sstevel@tonic-gate level = CE_PANIC; 927c478bd9Sstevel@tonic-gate #else 937c478bd9Sstevel@tonic-gate level = CE_WARN; 947c478bd9Sstevel@tonic-gate #endif 957c478bd9Sstevel@tonic-gate cmn_err(level, "unexpected lock type: %d\n", lock_type); 967c478bd9Sstevel@tonic-gate return (NBL_WRITE); /* pick something restrictive */ 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Returns non-zero if we need to look further for an NBMAND lock or 1037c478bd9Sstevel@tonic-gate * share conflict. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate int 1067c478bd9Sstevel@tonic-gate nbl_need_check(vnode_t *vp) 1077c478bd9Sstevel@tonic-gate { 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * Currently we only check if NBMAND locks/shares are allowed on 1107c478bd9Sstevel@tonic-gate * the filesystem. An option for the future would be to have a 1117c478bd9Sstevel@tonic-gate * flag on the vnode, though the locking for that can get tricky. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate return ((vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND)); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * Top-level conflict detection routine. The arguments describe the 1187c478bd9Sstevel@tonic-gate * operation that is being attempted. If the operation conflicts with an 1197c478bd9Sstevel@tonic-gate * existing lock or share reservation, a non-zero value is returned. If 1207c478bd9Sstevel@tonic-gate * the operation is allowed, zero is returned. Note that there is an 1217c478bd9Sstevel@tonic-gate * implicit argument, which is the process ID of the requester. 1227c478bd9Sstevel@tonic-gate * 1237c478bd9Sstevel@tonic-gate * svmand indicates that the file has System V mandatory locking enabled, 1247c478bd9Sstevel@tonic-gate * so we should look at all record locks, not just NBMAND record locks. 1257c478bd9Sstevel@tonic-gate * (This is to avoid a deadlock between a process making an I/O request and 1267c478bd9Sstevel@tonic-gate * a process trying to release a lock. Instead of letting the first 1277c478bd9Sstevel@tonic-gate * process block in the filesystem code, we flag a conflict here.) 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate int 1317c478bd9Sstevel@tonic-gate nbl_conflict(vnode_t *vp, 1327c478bd9Sstevel@tonic-gate nbl_op_t op, /* attempted operation */ 1337c478bd9Sstevel@tonic-gate u_offset_t offset, /* ignore if not I/O */ 1347c478bd9Sstevel@tonic-gate ssize_t length, /* ignore if not I/O */ 135*da6c28aaSamw int svmand, /* System V mandatory locking */ 136*da6c28aaSamw caller_context_t *ct) /* caller context */ 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate ASSERT(nbl_in_crit(vp)); 1397c478bd9Sstevel@tonic-gate ASSERT(op == NBL_READ || op == NBL_WRITE || op == NBL_RENAME || 1407c478bd9Sstevel@tonic-gate op == NBL_REMOVE || op == NBL_READWRITE); 1417c478bd9Sstevel@tonic-gate 142*da6c28aaSamw if (nbl_share_conflict(vp, op, ct)) { 1437c478bd9Sstevel@tonic-gate return (1); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * If this is not an I/O request, there's no need to check against 1487c478bd9Sstevel@tonic-gate * the locks on the file. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate if (op == NBL_REMOVE || op == NBL_RENAME) 1517c478bd9Sstevel@tonic-gate return (0); 1527c478bd9Sstevel@tonic-gate 153*da6c28aaSamw return (nbl_lock_conflict(vp, op, offset, length, svmand, ct)); 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Determine if the given file has mode bits for System V mandatory locks. 1587c478bd9Sstevel@tonic-gate * If there was an error, the errno value is returned. Otherwise, zero is 1597c478bd9Sstevel@tonic-gate * returned and *svp is set appropriately (non-zero for mandatory locks, 1607c478bd9Sstevel@tonic-gate * zero for no mandatory locks). 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate int 1647c478bd9Sstevel@tonic-gate nbl_svmand(vnode_t *vp, cred_t *cr, int *svp) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate struct vattr va; 1677c478bd9Sstevel@tonic-gate int error; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate va.va_mask = AT_MODE; 170*da6c28aaSamw error = VOP_GETATTR(vp, &va, 0, cr, NULL); 1717c478bd9Sstevel@tonic-gate if (error != 0) 1727c478bd9Sstevel@tonic-gate return (error); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate *svp = MANDLOCK(vp, va.va_mode); 1757c478bd9Sstevel@tonic-gate return (0); 1767c478bd9Sstevel@tonic-gate } 177