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 5da6c28aaSamw * Common Development and Distribution License (the "License"). 6da6c28aaSamw * 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*b89a8333Snatalie li - Sun Microsystems - Irvine United States * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23da6c28aaSamw * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * Utility routines and top-level conflict detection code for NBMAND 287c478bd9Sstevel@tonic-gate * locks. 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h> 327c478bd9Sstevel@tonic-gate #include <sys/rwlock.h> 337c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 357c478bd9Sstevel@tonic-gate #include <sys/types.h> 367c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 377c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Enter the critical region for synchronizing I/O requests with lock/share 417c478bd9Sstevel@tonic-gate * requests. "mode" specifies whether the caller intends to update 427c478bd9Sstevel@tonic-gate * lock/share state (as opposed to just query it). 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate void 467c478bd9Sstevel@tonic-gate nbl_start_crit(vnode_t *vp, krw_t mode) 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate rw_enter(&vp->v_nbllock, mode); 497c478bd9Sstevel@tonic-gate } 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * Leave the critical region. 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate void 567c478bd9Sstevel@tonic-gate nbl_end_crit(vnode_t *vp) 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate rw_exit(&vp->v_nbllock); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * Return non-zero if some thread is in the critical region. 637c478bd9Sstevel@tonic-gate * Note that this is appropriate for use in ASSERT()s only. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate int 677c478bd9Sstevel@tonic-gate nbl_in_crit(vnode_t *vp) 687c478bd9Sstevel@tonic-gate { 697c478bd9Sstevel@tonic-gate return (RW_LOCK_HELD(&vp->v_nbllock)); 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * Returns non-zero if we need to look further for an NBMAND lock or 747c478bd9Sstevel@tonic-gate * share conflict. 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate int 777c478bd9Sstevel@tonic-gate nbl_need_check(vnode_t *vp) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate /* 807c478bd9Sstevel@tonic-gate * Currently we only check if NBMAND locks/shares are allowed on 817c478bd9Sstevel@tonic-gate * the filesystem. An option for the future would be to have a 827c478bd9Sstevel@tonic-gate * flag on the vnode, though the locking for that can get tricky. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate return ((vp->v_vfsp) && (vp->v_vfsp->vfs_flag & VFS_NBMAND)); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * Top-level conflict detection routine. The arguments describe the 897c478bd9Sstevel@tonic-gate * operation that is being attempted. If the operation conflicts with an 907c478bd9Sstevel@tonic-gate * existing lock or share reservation, a non-zero value is returned. If 917c478bd9Sstevel@tonic-gate * the operation is allowed, zero is returned. Note that there is an 927c478bd9Sstevel@tonic-gate * implicit argument, which is the process ID of the requester. 937c478bd9Sstevel@tonic-gate * 947c478bd9Sstevel@tonic-gate * svmand indicates that the file has System V mandatory locking enabled, 957c478bd9Sstevel@tonic-gate * so we should look at all record locks, not just NBMAND record locks. 967c478bd9Sstevel@tonic-gate * (This is to avoid a deadlock between a process making an I/O request and 977c478bd9Sstevel@tonic-gate * a process trying to release a lock. Instead of letting the first 987c478bd9Sstevel@tonic-gate * process block in the filesystem code, we flag a conflict here.) 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate int 1027c478bd9Sstevel@tonic-gate nbl_conflict(vnode_t *vp, 1037c478bd9Sstevel@tonic-gate nbl_op_t op, /* attempted operation */ 1047c478bd9Sstevel@tonic-gate u_offset_t offset, /* ignore if not I/O */ 1057c478bd9Sstevel@tonic-gate ssize_t length, /* ignore if not I/O */ 106da6c28aaSamw int svmand, /* System V mandatory locking */ 107da6c28aaSamw caller_context_t *ct) /* caller context */ 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate ASSERT(nbl_in_crit(vp)); 1107c478bd9Sstevel@tonic-gate ASSERT(op == NBL_READ || op == NBL_WRITE || op == NBL_RENAME || 1117c478bd9Sstevel@tonic-gate op == NBL_REMOVE || op == NBL_READWRITE); 1127c478bd9Sstevel@tonic-gate 113da6c28aaSamw if (nbl_share_conflict(vp, op, ct)) { 1147c478bd9Sstevel@tonic-gate return (1); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * If this is not an I/O request, there's no need to check against 1197c478bd9Sstevel@tonic-gate * the locks on the file. 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate if (op == NBL_REMOVE || op == NBL_RENAME) 1227c478bd9Sstevel@tonic-gate return (0); 1237c478bd9Sstevel@tonic-gate 124da6c28aaSamw return (nbl_lock_conflict(vp, op, offset, length, svmand, ct)); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Determine if the given file has mode bits for System V mandatory locks. 1297c478bd9Sstevel@tonic-gate * If there was an error, the errno value is returned. Otherwise, zero is 1307c478bd9Sstevel@tonic-gate * returned and *svp is set appropriately (non-zero for mandatory locks, 1317c478bd9Sstevel@tonic-gate * zero for no mandatory locks). 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate int 1357c478bd9Sstevel@tonic-gate nbl_svmand(vnode_t *vp, cred_t *cr, int *svp) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate struct vattr va; 1387c478bd9Sstevel@tonic-gate int error; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate va.va_mask = AT_MODE; 141da6c28aaSamw error = VOP_GETATTR(vp, &va, 0, cr, NULL); 1427c478bd9Sstevel@tonic-gate if (error != 0) 1437c478bd9Sstevel@tonic-gate return (error); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate *svp = MANDLOCK(vp, va.va_mode); 1467c478bd9Sstevel@tonic-gate return (0); 1477c478bd9Sstevel@tonic-gate } 148