1*bbaa8b60SDan Kruchinin /* 2*bbaa8b60SDan Kruchinin * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ 3*bbaa8b60SDan Kruchinin * Authors: Doug Rabson <dfr@rabson.org> 4*bbaa8b60SDan Kruchinin * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org> 5*bbaa8b60SDan Kruchinin * 6*bbaa8b60SDan Kruchinin * Redistribution and use in source and binary forms, with or without 7*bbaa8b60SDan Kruchinin * modification, are permitted provided that the following conditions 8*bbaa8b60SDan Kruchinin * are met: 9*bbaa8b60SDan Kruchinin * 1. Redistributions of source code must retain the above copyright 10*bbaa8b60SDan Kruchinin * notice, this list of conditions and the following disclaimer. 11*bbaa8b60SDan Kruchinin * 2. Redistributions in binary form must reproduce the above copyright 12*bbaa8b60SDan Kruchinin * notice, this list of conditions and the following disclaimer in the 13*bbaa8b60SDan Kruchinin * documentation and/or other materials provided with the distribution. 14*bbaa8b60SDan Kruchinin * 15*bbaa8b60SDan Kruchinin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16*bbaa8b60SDan Kruchinin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17*bbaa8b60SDan Kruchinin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*bbaa8b60SDan Kruchinin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19*bbaa8b60SDan Kruchinin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*bbaa8b60SDan Kruchinin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21*bbaa8b60SDan Kruchinin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22*bbaa8b60SDan Kruchinin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23*bbaa8b60SDan Kruchinin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24*bbaa8b60SDan Kruchinin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25*bbaa8b60SDan Kruchinin * SUCH DAMAGE. 26*bbaa8b60SDan Kruchinin */ 27*bbaa8b60SDan Kruchinin 28*bbaa8b60SDan Kruchinin /* 29*bbaa8b60SDan Kruchinin * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 30*bbaa8b60SDan Kruchinin */ 31*bbaa8b60SDan Kruchinin 32*bbaa8b60SDan Kruchinin /* 33*bbaa8b60SDan Kruchinin * Client-side RPC wrappers (nlm_..._rpc) 34*bbaa8b60SDan Kruchinin * Called from nlm_client.c 35*bbaa8b60SDan Kruchinin * 36*bbaa8b60SDan Kruchinin * Source code derived from FreeBSD nlm_advlock.c 37*bbaa8b60SDan Kruchinin */ 38*bbaa8b60SDan Kruchinin 39*bbaa8b60SDan Kruchinin #include <sys/param.h> 40*bbaa8b60SDan Kruchinin #include <sys/fcntl.h> 41*bbaa8b60SDan Kruchinin #include <sys/lock.h> 42*bbaa8b60SDan Kruchinin #include <sys/flock.h> 43*bbaa8b60SDan Kruchinin #include <sys/mount.h> 44*bbaa8b60SDan Kruchinin #include <sys/mutex.h> 45*bbaa8b60SDan Kruchinin #include <sys/proc.h> 46*bbaa8b60SDan Kruchinin #include <sys/syslog.h> 47*bbaa8b60SDan Kruchinin #include <sys/systm.h> 48*bbaa8b60SDan Kruchinin #include <sys/unistd.h> 49*bbaa8b60SDan Kruchinin #include <sys/vnode.h> 50*bbaa8b60SDan Kruchinin #include <sys/queue.h> 51*bbaa8b60SDan Kruchinin 52*bbaa8b60SDan Kruchinin #include <rpcsvc/nlm_prot.h> 53*bbaa8b60SDan Kruchinin 54*bbaa8b60SDan Kruchinin #include <nfs/nfs.h> 55*bbaa8b60SDan Kruchinin #include <nfs/nfs_clnt.h> 56*bbaa8b60SDan Kruchinin #include <nfs/export.h> 57*bbaa8b60SDan Kruchinin #include <nfs/rnode.h> 58*bbaa8b60SDan Kruchinin 59*bbaa8b60SDan Kruchinin #include "nlm_impl.h" 60*bbaa8b60SDan Kruchinin 61*bbaa8b60SDan Kruchinin static void 62*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src) 63*bbaa8b60SDan Kruchinin { 64*bbaa8b60SDan Kruchinin dst->caller_name = src->caller_name; 65*bbaa8b60SDan Kruchinin dst->fh = src->fh; 66*bbaa8b60SDan Kruchinin dst->oh = src->oh; 67*bbaa8b60SDan Kruchinin dst->svid = src->svid; 68*bbaa8b60SDan Kruchinin dst->l_offset = src->l_offset; 69*bbaa8b60SDan Kruchinin dst->l_len = src->l_len; 70*bbaa8b60SDan Kruchinin } 71*bbaa8b60SDan Kruchinin 72*bbaa8b60SDan Kruchinin static void 73*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src) 74*bbaa8b60SDan Kruchinin { 75*bbaa8b60SDan Kruchinin dst->exclusive = src->exclusive; 76*bbaa8b60SDan Kruchinin dst->svid = src->svid; 77*bbaa8b60SDan Kruchinin dst->oh = src->oh; 78*bbaa8b60SDan Kruchinin dst->l_offset = src->l_offset; 79*bbaa8b60SDan Kruchinin dst->l_len = src->l_len; 80*bbaa8b60SDan Kruchinin } 81*bbaa8b60SDan Kruchinin 82*bbaa8b60SDan Kruchinin static void 83*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src) 84*bbaa8b60SDan Kruchinin { 85*bbaa8b60SDan Kruchinin dst->cookie = src->cookie; 86*bbaa8b60SDan Kruchinin dst->stat.stat = (enum nlm4_stats) src->stat.stat; 87*bbaa8b60SDan Kruchinin } 88*bbaa8b60SDan Kruchinin 89*bbaa8b60SDan Kruchinin enum clnt_stat 90*bbaa8b60SDan Kruchinin nlm_test_rpc(nlm4_testargs *args, nlm4_testres *res, 91*bbaa8b60SDan Kruchinin CLIENT *client, rpcvers_t vers) 92*bbaa8b60SDan Kruchinin { 93*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) { 94*bbaa8b60SDan Kruchinin return (nlm4_test_4(args, res, client)); 95*bbaa8b60SDan Kruchinin } else { 96*bbaa8b60SDan Kruchinin nlm_testargs args1; 97*bbaa8b60SDan Kruchinin nlm_testres res1; 98*bbaa8b60SDan Kruchinin enum clnt_stat stat; 99*bbaa8b60SDan Kruchinin 100*bbaa8b60SDan Kruchinin args1.cookie = args->cookie; 101*bbaa8b60SDan Kruchinin args1.exclusive = args->exclusive; 102*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 103*bbaa8b60SDan Kruchinin (void) memset(&res1, 0, sizeof (res1)); 104*bbaa8b60SDan Kruchinin 105*bbaa8b60SDan Kruchinin stat = nlm_test_1(&args1, &res1, client); 106*bbaa8b60SDan Kruchinin 107*bbaa8b60SDan Kruchinin if (stat == RPC_SUCCESS) { 108*bbaa8b60SDan Kruchinin res->cookie = res1.cookie; 109*bbaa8b60SDan Kruchinin res->stat.stat = (enum nlm4_stats) res1.stat.stat; 110*bbaa8b60SDan Kruchinin if (res1.stat.stat == nlm_denied) 111*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_holder( 112*bbaa8b60SDan Kruchinin &res->stat.nlm4_testrply_u.holder, 113*bbaa8b60SDan Kruchinin &res1.stat.nlm_testrply_u.holder); 114*bbaa8b60SDan Kruchinin } 115*bbaa8b60SDan Kruchinin 116*bbaa8b60SDan Kruchinin return (stat); 117*bbaa8b60SDan Kruchinin } 118*bbaa8b60SDan Kruchinin } 119*bbaa8b60SDan Kruchinin 120*bbaa8b60SDan Kruchinin enum clnt_stat 121*bbaa8b60SDan Kruchinin nlm_lock_rpc(nlm4_lockargs *args, nlm4_res *res, 122*bbaa8b60SDan Kruchinin CLIENT *client, rpcvers_t vers) 123*bbaa8b60SDan Kruchinin { 124*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) { 125*bbaa8b60SDan Kruchinin return (nlm4_lock_4(args, res, client)); 126*bbaa8b60SDan Kruchinin } else { 127*bbaa8b60SDan Kruchinin nlm_lockargs args1; 128*bbaa8b60SDan Kruchinin nlm_res res1; 129*bbaa8b60SDan Kruchinin enum clnt_stat stat; 130*bbaa8b60SDan Kruchinin 131*bbaa8b60SDan Kruchinin args1.cookie = args->cookie; 132*bbaa8b60SDan Kruchinin args1.block = args->block; 133*bbaa8b60SDan Kruchinin args1.exclusive = args->exclusive; 134*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 135*bbaa8b60SDan Kruchinin args1.reclaim = args->reclaim; 136*bbaa8b60SDan Kruchinin args1.state = args->state; 137*bbaa8b60SDan Kruchinin (void) memset(&res1, 0, sizeof (res1)); 138*bbaa8b60SDan Kruchinin 139*bbaa8b60SDan Kruchinin stat = nlm_lock_1(&args1, &res1, client); 140*bbaa8b60SDan Kruchinin 141*bbaa8b60SDan Kruchinin if (stat == RPC_SUCCESS) { 142*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_res(res, &res1); 143*bbaa8b60SDan Kruchinin } 144*bbaa8b60SDan Kruchinin 145*bbaa8b60SDan Kruchinin return (stat); 146*bbaa8b60SDan Kruchinin } 147*bbaa8b60SDan Kruchinin } 148*bbaa8b60SDan Kruchinin 149*bbaa8b60SDan Kruchinin enum clnt_stat 150*bbaa8b60SDan Kruchinin nlm_cancel_rpc(nlm4_cancargs *args, nlm4_res *res, 151*bbaa8b60SDan Kruchinin CLIENT *client, rpcvers_t vers) 152*bbaa8b60SDan Kruchinin { 153*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) { 154*bbaa8b60SDan Kruchinin return (nlm4_cancel_4(args, res, client)); 155*bbaa8b60SDan Kruchinin } else { 156*bbaa8b60SDan Kruchinin nlm_cancargs args1; 157*bbaa8b60SDan Kruchinin nlm_res res1; 158*bbaa8b60SDan Kruchinin enum clnt_stat stat; 159*bbaa8b60SDan Kruchinin 160*bbaa8b60SDan Kruchinin args1.cookie = args->cookie; 161*bbaa8b60SDan Kruchinin args1.block = args->block; 162*bbaa8b60SDan Kruchinin args1.exclusive = args->exclusive; 163*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 164*bbaa8b60SDan Kruchinin (void) memset(&res1, 0, sizeof (res1)); 165*bbaa8b60SDan Kruchinin 166*bbaa8b60SDan Kruchinin stat = nlm_cancel_1(&args1, &res1, client); 167*bbaa8b60SDan Kruchinin 168*bbaa8b60SDan Kruchinin if (stat == RPC_SUCCESS) { 169*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_res(res, &res1); 170*bbaa8b60SDan Kruchinin } 171*bbaa8b60SDan Kruchinin 172*bbaa8b60SDan Kruchinin return (stat); 173*bbaa8b60SDan Kruchinin } 174*bbaa8b60SDan Kruchinin } 175*bbaa8b60SDan Kruchinin 176*bbaa8b60SDan Kruchinin enum clnt_stat 177*bbaa8b60SDan Kruchinin nlm_unlock_rpc(nlm4_unlockargs *args, nlm4_res *res, 178*bbaa8b60SDan Kruchinin CLIENT *client, rpcvers_t vers) 179*bbaa8b60SDan Kruchinin { 180*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) { 181*bbaa8b60SDan Kruchinin return (nlm4_unlock_4(args, res, client)); 182*bbaa8b60SDan Kruchinin } else { 183*bbaa8b60SDan Kruchinin nlm_unlockargs args1; 184*bbaa8b60SDan Kruchinin nlm_res res1; 185*bbaa8b60SDan Kruchinin enum clnt_stat stat; 186*bbaa8b60SDan Kruchinin 187*bbaa8b60SDan Kruchinin args1.cookie = args->cookie; 188*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_lock(&args1.alock, &args->alock); 189*bbaa8b60SDan Kruchinin (void) memset(&res1, 0, sizeof (res1)); 190*bbaa8b60SDan Kruchinin 191*bbaa8b60SDan Kruchinin stat = nlm_unlock_1(&args1, &res1, client); 192*bbaa8b60SDan Kruchinin 193*bbaa8b60SDan Kruchinin if (stat == RPC_SUCCESS) { 194*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_res(res, &res1); 195*bbaa8b60SDan Kruchinin } 196*bbaa8b60SDan Kruchinin 197*bbaa8b60SDan Kruchinin return (stat); 198*bbaa8b60SDan Kruchinin } 199*bbaa8b60SDan Kruchinin } 200*bbaa8b60SDan Kruchinin 201*bbaa8b60SDan Kruchinin enum clnt_stat 202*bbaa8b60SDan Kruchinin nlm_null_rpc(CLIENT *client, rpcvers_t vers) 203*bbaa8b60SDan Kruchinin { 204*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) 205*bbaa8b60SDan Kruchinin return (nlm4_null_4(NULL, NULL, client)); 206*bbaa8b60SDan Kruchinin 207*bbaa8b60SDan Kruchinin return (nlm_null_1(NULL, NULL, client)); 208*bbaa8b60SDan Kruchinin } 209*bbaa8b60SDan Kruchinin 210*bbaa8b60SDan Kruchinin /* 211*bbaa8b60SDan Kruchinin * Share reservations 212*bbaa8b60SDan Kruchinin */ 213*bbaa8b60SDan Kruchinin 214*bbaa8b60SDan Kruchinin static void 215*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_share(struct nlm_share *dst, struct nlm4_share *src) 216*bbaa8b60SDan Kruchinin { 217*bbaa8b60SDan Kruchinin 218*bbaa8b60SDan Kruchinin dst->caller_name = src->caller_name; 219*bbaa8b60SDan Kruchinin dst->fh = src->fh; 220*bbaa8b60SDan Kruchinin dst->oh = src->oh; 221*bbaa8b60SDan Kruchinin dst->mode = src->mode; 222*bbaa8b60SDan Kruchinin dst->access = src->access; 223*bbaa8b60SDan Kruchinin } 224*bbaa8b60SDan Kruchinin 225*bbaa8b60SDan Kruchinin static void 226*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_shres(struct nlm4_shareres *dst, 227*bbaa8b60SDan Kruchinin struct nlm_shareres *src) 228*bbaa8b60SDan Kruchinin { 229*bbaa8b60SDan Kruchinin dst->cookie = src->cookie; 230*bbaa8b60SDan Kruchinin dst->stat = (enum nlm4_stats) src->stat; 231*bbaa8b60SDan Kruchinin dst->sequence = src->sequence; 232*bbaa8b60SDan Kruchinin } 233*bbaa8b60SDan Kruchinin 234*bbaa8b60SDan Kruchinin 235*bbaa8b60SDan Kruchinin enum clnt_stat 236*bbaa8b60SDan Kruchinin nlm_share_rpc(nlm4_shareargs *args, nlm4_shareres *res, 237*bbaa8b60SDan Kruchinin CLIENT *client, rpcvers_t vers) 238*bbaa8b60SDan Kruchinin { 239*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) { 240*bbaa8b60SDan Kruchinin return (nlm4_share_4(args, res, client)); 241*bbaa8b60SDan Kruchinin } else { 242*bbaa8b60SDan Kruchinin nlm_shareargs args3; 243*bbaa8b60SDan Kruchinin nlm_shareres res3; 244*bbaa8b60SDan Kruchinin enum clnt_stat stat; 245*bbaa8b60SDan Kruchinin 246*bbaa8b60SDan Kruchinin args3.cookie = args->cookie; 247*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_share(&args3.share, &args->share); 248*bbaa8b60SDan Kruchinin args3.reclaim = args->reclaim; 249*bbaa8b60SDan Kruchinin (void) memset(&res3, 0, sizeof (res3)); 250*bbaa8b60SDan Kruchinin 251*bbaa8b60SDan Kruchinin stat = nlm_share_3(&args3, &res3, client); 252*bbaa8b60SDan Kruchinin 253*bbaa8b60SDan Kruchinin if (stat == RPC_SUCCESS) { 254*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_shres(res, &res3); 255*bbaa8b60SDan Kruchinin } 256*bbaa8b60SDan Kruchinin 257*bbaa8b60SDan Kruchinin return (stat); 258*bbaa8b60SDan Kruchinin } 259*bbaa8b60SDan Kruchinin } 260*bbaa8b60SDan Kruchinin 261*bbaa8b60SDan Kruchinin enum clnt_stat 262*bbaa8b60SDan Kruchinin nlm_unshare_rpc(nlm4_shareargs *args, nlm4_shareres *res, 263*bbaa8b60SDan Kruchinin CLIENT *client, rpcvers_t vers) 264*bbaa8b60SDan Kruchinin { 265*bbaa8b60SDan Kruchinin if (vers == NLM4_VERS) { 266*bbaa8b60SDan Kruchinin return (nlm4_unshare_4(args, res, client)); 267*bbaa8b60SDan Kruchinin } else { 268*bbaa8b60SDan Kruchinin nlm_shareargs args3; 269*bbaa8b60SDan Kruchinin nlm_shareres res3; 270*bbaa8b60SDan Kruchinin enum clnt_stat stat; 271*bbaa8b60SDan Kruchinin 272*bbaa8b60SDan Kruchinin args3.cookie = args->cookie; 273*bbaa8b60SDan Kruchinin nlm_convert_to_nlm_share(&args3.share, &args->share); 274*bbaa8b60SDan Kruchinin args3.reclaim = args->reclaim; 275*bbaa8b60SDan Kruchinin (void) memset(&res3, 0, sizeof (res3)); 276*bbaa8b60SDan Kruchinin 277*bbaa8b60SDan Kruchinin stat = nlm_unshare_3(&args3, &res3, client); 278*bbaa8b60SDan Kruchinin 279*bbaa8b60SDan Kruchinin if (stat == RPC_SUCCESS) { 280*bbaa8b60SDan Kruchinin nlm_convert_to_nlm4_shres(res, &res3); 281*bbaa8b60SDan Kruchinin } 282*bbaa8b60SDan Kruchinin 283*bbaa8b60SDan Kruchinin return (stat); 284*bbaa8b60SDan Kruchinin } 285*bbaa8b60SDan Kruchinin } 286