1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T. 28 * All Rights Reserved 29 */ 30 31 #include <sys/param.h> 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/cred.h> 35 #include <sys/buf.h> 36 #include <sys/vfs.h> 37 #include <sys/vfs_opreg.h> 38 #include <sys/vnode.h> 39 #include <sys/uio.h> 40 #include <sys/errno.h> 41 #include <sys/sysmacros.h> 42 #include <sys/statvfs.h> 43 #include <sys/kmem.h> 44 #include <sys/dirent.h> 45 #include <sys/cmn_err.h> 46 #include <sys/debug.h> 47 #include <sys/systeminfo.h> 48 #include <sys/flock.h> 49 #include <sys/pathname.h> 50 #include <sys/nbmlock.h> 51 #include <sys/share.h> 52 #include <sys/atomic.h> 53 #include <sys/policy.h> 54 #include <sys/fem.h> 55 #include <sys/sdt.h> 56 #include <sys/ddi.h> 57 #include <sys/zone.h> 58 59 #include <rpc/types.h> 60 #include <rpc/auth.h> 61 #include <rpc/rpcsec_gss.h> 62 #include <rpc/svc.h> 63 64 #include <nfs/nfs.h> 65 #include <nfs/export.h> 66 #include <nfs/nfs_cmd.h> 67 #include <nfs/lm.h> 68 #include <nfs/nfs4.h> 69 70 #include <sys/strsubr.h> 71 #include <sys/strsun.h> 72 73 #include <inet/common.h> 74 #include <inet/ip.h> 75 #include <inet/ip6.h> 76 77 #include <sys/tsol/label.h> 78 #include <sys/tsol/tndb.h> 79 80 #define RFS4_MAXLOCK_TRIES 4 /* Try to get the lock this many times */ 81 static int rfs4_maxlock_tries = RFS4_MAXLOCK_TRIES; 82 #define RFS4_LOCK_DELAY 10 /* Milliseconds */ 83 static clock_t rfs4_lock_delay = RFS4_LOCK_DELAY; 84 extern struct svc_ops rdma_svc_ops; 85 /* End of Tunables */ 86 87 static int rdma_setup_read_data4(READ4args *, READ4res *); 88 89 /* 90 * Used to bump the stateid4.seqid value and show changes in the stateid 91 */ 92 #define next_stateid(sp) (++(sp)->bits.chgseq) 93 94 /* 95 * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent. 96 * This is used to return NFS4ERR_TOOSMALL when clients specify 97 * maxcount that isn't large enough to hold the smallest possible 98 * XDR encoded dirent. 99 * 100 * sizeof cookie (8 bytes) + 101 * sizeof name_len (4 bytes) + 102 * sizeof smallest (padded) name (4 bytes) + 103 * sizeof bitmap4_len (12 bytes) + NOTE: we always encode len=2 bm4 104 * sizeof attrlist4_len (4 bytes) + 105 * sizeof next boolean (4 bytes) 106 * 107 * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing 108 * the smallest possible entry4 (assumes no attrs requested). 109 * sizeof nfsstat4 (4 bytes) + 110 * sizeof verifier4 (8 bytes) + 111 * sizeof entry4list bool (4 bytes) + 112 * sizeof entry4 (36 bytes) + 113 * sizeof eof bool (4 bytes) 114 * 115 * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to 116 * VOP_READDIR. Its value is the size of the maximum possible dirent 117 * for solaris. The DIRENT64_RECLEN macro returns the size of dirent 118 * required for a given name length. MAXNAMELEN is the maximum 119 * filename length allowed in Solaris. The first two DIRENT64_RECLEN() 120 * macros are to allow for . and .. entries -- just a minor tweak to try 121 * and guarantee that buffer we give to VOP_READDIR will be large enough 122 * to hold ., .., and the largest possible solaris dirent64. 123 */ 124 #define RFS4_MINLEN_ENTRY4 36 125 #define RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4) 126 #define RFS4_MINLEN_RDDIR_BUF \ 127 (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN)) 128 129 /* 130 * It would be better to pad to 4 bytes since that's what XDR would do, 131 * but the dirents UFS gives us are already padded to 8, so just take 132 * what we're given. Dircount is only a hint anyway. Currently the 133 * solaris kernel is ASCII only, so there's no point in calling the 134 * UTF8 functions. 135 * 136 * dirent64: named padded to provide 8 byte struct alignment 137 * d_ino(8) + d_off(8) + d_reclen(2) + d_name(namelen + null(1) + pad) 138 * 139 * cookie: uint64_t + utf8namelen: uint_t + utf8name padded to 8 bytes 140 * 141 */ 142 #define DIRENT64_TO_DIRCOUNT(dp) \ 143 (3 * BYTES_PER_XDR_UNIT + DIRENT64_NAMELEN((dp)->d_reclen)) 144 145 time_t rfs4_start_time; /* Initialized in rfs4_srvrinit */ 146 147 static sysid_t lockt_sysid; /* dummy sysid for all LOCKT calls */ 148 149 u_longlong_t nfs4_srv_caller_id; 150 uint_t nfs4_srv_vkey = 0; 151 152 verifier4 Write4verf; 153 verifier4 Readdir4verf; 154 155 void rfs4_init_compound_state(struct compound_state *); 156 157 static void nullfree(caddr_t); 158 static void rfs4_op_inval(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 159 struct compound_state *); 160 static void rfs4_op_access(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 161 struct compound_state *); 162 static void rfs4_op_close(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 163 struct compound_state *); 164 static void rfs4_op_commit(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 165 struct compound_state *); 166 static void rfs4_op_create(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 167 struct compound_state *); 168 static void rfs4_op_create_free(nfs_resop4 *resop); 169 static void rfs4_op_delegreturn(nfs_argop4 *, nfs_resop4 *, 170 struct svc_req *, struct compound_state *); 171 static void rfs4_op_delegpurge(nfs_argop4 *, nfs_resop4 *, 172 struct svc_req *, struct compound_state *); 173 static void rfs4_op_getattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 174 struct compound_state *); 175 static void rfs4_op_getattr_free(nfs_resop4 *); 176 static void rfs4_op_getfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 177 struct compound_state *); 178 static void rfs4_op_getfh_free(nfs_resop4 *); 179 static void rfs4_op_illegal(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 180 struct compound_state *); 181 static void rfs4_op_link(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 182 struct compound_state *); 183 static void rfs4_op_lock(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 184 struct compound_state *); 185 static void lock_denied_free(nfs_resop4 *); 186 static void rfs4_op_locku(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 187 struct compound_state *); 188 static void rfs4_op_lockt(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 189 struct compound_state *); 190 static void rfs4_op_lookup(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 191 struct compound_state *); 192 static void rfs4_op_lookupp(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 193 struct compound_state *); 194 static void rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, 195 struct svc_req *req, struct compound_state *cs); 196 static void rfs4_op_nverify(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 197 struct compound_state *); 198 static void rfs4_op_open(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 199 struct compound_state *); 200 static void rfs4_op_open_confirm(nfs_argop4 *, nfs_resop4 *, 201 struct svc_req *, struct compound_state *); 202 static void rfs4_op_open_downgrade(nfs_argop4 *, nfs_resop4 *, 203 struct svc_req *, struct compound_state *); 204 static void rfs4_op_putfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 205 struct compound_state *); 206 static void rfs4_op_putpubfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 207 struct compound_state *); 208 static void rfs4_op_putrootfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 209 struct compound_state *); 210 static void rfs4_op_read(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 211 struct compound_state *); 212 static void rfs4_op_read_free(nfs_resop4 *); 213 static void rfs4_op_readdir_free(nfs_resop4 *resop); 214 static void rfs4_op_readlink(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 215 struct compound_state *); 216 static void rfs4_op_readlink_free(nfs_resop4 *); 217 static void rfs4_op_release_lockowner(nfs_argop4 *, nfs_resop4 *, 218 struct svc_req *, struct compound_state *); 219 static void rfs4_op_remove(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 220 struct compound_state *); 221 static void rfs4_op_rename(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 222 struct compound_state *); 223 static void rfs4_op_renew(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 224 struct compound_state *); 225 static void rfs4_op_restorefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 226 struct compound_state *); 227 static void rfs4_op_savefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 228 struct compound_state *); 229 static void rfs4_op_setattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 230 struct compound_state *); 231 static void rfs4_op_verify(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 232 struct compound_state *); 233 static void rfs4_op_write(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 234 struct compound_state *); 235 static void rfs4_op_setclientid(nfs_argop4 *, nfs_resop4 *, 236 struct svc_req *, struct compound_state *); 237 static void rfs4_op_setclientid_confirm(nfs_argop4 *, nfs_resop4 *, 238 struct svc_req *req, struct compound_state *); 239 static void rfs4_op_secinfo(nfs_argop4 *, nfs_resop4 *, struct svc_req *, 240 struct compound_state *); 241 static void rfs4_op_secinfo_free(nfs_resop4 *); 242 243 static nfsstat4 check_open_access(uint32_t, 244 struct compound_state *, struct svc_req *); 245 nfsstat4 rfs4_client_sysid(rfs4_client_t *, sysid_t *); 246 static int vop_shrlock(vnode_t *, int, struct shrlock *, int); 247 static int rfs4_shrlock(rfs4_state_t *, int); 248 static int rfs4_share(rfs4_state_t *); 249 void rfs4_ss_clid(rfs4_client_t *, struct svc_req *); 250 251 /* 252 * translation table for attrs 253 */ 254 struct nfs4_ntov_table { 255 union nfs4_attr_u *na; 256 uint8_t amap[NFS4_MAXNUM_ATTRS]; 257 int attrcnt; 258 bool_t vfsstat; 259 }; 260 261 static void nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp); 262 static void nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp, 263 struct nfs4_svgetit_arg *sargp); 264 265 static nfsstat4 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, 266 struct compound_state *cs, struct nfs4_svgetit_arg *sargp, 267 struct nfs4_ntov_table *ntovp, nfs4_attr_cmd_t cmd); 268 269 fem_t *deleg_rdops; 270 fem_t *deleg_wrops; 271 272 rfs4_servinst_t *rfs4_cur_servinst = NULL; /* current server instance */ 273 kmutex_t rfs4_servinst_lock; /* protects linked list */ 274 int rfs4_seen_first_compound; /* set first time we see one */ 275 276 /* 277 * NFS4 op dispatch table 278 */ 279 280 struct rfsv4disp { 281 void (*dis_proc)(); /* proc to call */ 282 void (*dis_resfree)(); /* frees space allocated by proc */ 283 int dis_flags; /* RPC_IDEMPOTENT, etc... */ 284 }; 285 286 static struct rfsv4disp rfsv4disptab[] = { 287 /* 288 * NFS VERSION 4 289 */ 290 291 /* RFS_NULL = 0 */ 292 {rfs4_op_illegal, nullfree, 0}, 293 294 /* UNUSED = 1 */ 295 {rfs4_op_illegal, nullfree, 0}, 296 297 /* UNUSED = 2 */ 298 {rfs4_op_illegal, nullfree, 0}, 299 300 /* OP_ACCESS = 3 */ 301 {rfs4_op_access, nullfree, RPC_IDEMPOTENT}, 302 303 /* OP_CLOSE = 4 */ 304 {rfs4_op_close, nullfree, 0}, 305 306 /* OP_COMMIT = 5 */ 307 {rfs4_op_commit, nullfree, RPC_IDEMPOTENT}, 308 309 /* OP_CREATE = 6 */ 310 {rfs4_op_create, nullfree, 0}, 311 312 /* OP_DELEGPURGE = 7 */ 313 {rfs4_op_delegpurge, nullfree, 0}, 314 315 /* OP_DELEGRETURN = 8 */ 316 {rfs4_op_delegreturn, nullfree, 0}, 317 318 /* OP_GETATTR = 9 */ 319 {rfs4_op_getattr, rfs4_op_getattr_free, RPC_IDEMPOTENT}, 320 321 /* OP_GETFH = 10 */ 322 {rfs4_op_getfh, rfs4_op_getfh_free, RPC_ALL}, 323 324 /* OP_LINK = 11 */ 325 {rfs4_op_link, nullfree, 0}, 326 327 /* OP_LOCK = 12 */ 328 {rfs4_op_lock, lock_denied_free, 0}, 329 330 /* OP_LOCKT = 13 */ 331 {rfs4_op_lockt, lock_denied_free, 0}, 332 333 /* OP_LOCKU = 14 */ 334 {rfs4_op_locku, nullfree, 0}, 335 336 /* OP_LOOKUP = 15 */ 337 {rfs4_op_lookup, nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK)}, 338 339 /* OP_LOOKUPP = 16 */ 340 {rfs4_op_lookupp, nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK)}, 341 342 /* OP_NVERIFY = 17 */ 343 {rfs4_op_nverify, nullfree, RPC_IDEMPOTENT}, 344 345 /* OP_OPEN = 18 */ 346 {rfs4_op_open, rfs4_free_reply, 0}, 347 348 /* OP_OPENATTR = 19 */ 349 {rfs4_op_openattr, nullfree, 0}, 350 351 /* OP_OPEN_CONFIRM = 20 */ 352 {rfs4_op_open_confirm, nullfree, 0}, 353 354 /* OP_OPEN_DOWNGRADE = 21 */ 355 {rfs4_op_open_downgrade, nullfree, 0}, 356 357 /* OP_OPEN_PUTFH = 22 */ 358 {rfs4_op_putfh, nullfree, RPC_ALL}, 359 360 /* OP_PUTPUBFH = 23 */ 361 {rfs4_op_putpubfh, nullfree, RPC_ALL}, 362 363 /* OP_PUTROOTFH = 24 */ 364 {rfs4_op_putrootfh, nullfree, RPC_ALL}, 365 366 /* OP_READ = 25 */ 367 {rfs4_op_read, rfs4_op_read_free, RPC_IDEMPOTENT}, 368 369 /* OP_READDIR = 26 */ 370 {rfs4_op_readdir, rfs4_op_readdir_free, RPC_IDEMPOTENT}, 371 372 /* OP_READLINK = 27 */ 373 {rfs4_op_readlink, rfs4_op_readlink_free, RPC_IDEMPOTENT}, 374 375 /* OP_REMOVE = 28 */ 376 {rfs4_op_remove, nullfree, 0}, 377 378 /* OP_RENAME = 29 */ 379 {rfs4_op_rename, nullfree, 0}, 380 381 /* OP_RENEW = 30 */ 382 {rfs4_op_renew, nullfree, 0}, 383 384 /* OP_RESTOREFH = 31 */ 385 {rfs4_op_restorefh, nullfree, RPC_ALL}, 386 387 /* OP_SAVEFH = 32 */ 388 {rfs4_op_savefh, nullfree, RPC_ALL}, 389 390 /* OP_SECINFO = 33 */ 391 {rfs4_op_secinfo, rfs4_op_secinfo_free, 0}, 392 393 /* OP_SETATTR = 34 */ 394 {rfs4_op_setattr, nullfree, 0}, 395 396 /* OP_SETCLIENTID = 35 */ 397 {rfs4_op_setclientid, nullfree, 0}, 398 399 /* OP_SETCLIENTID_CONFIRM = 36 */ 400 {rfs4_op_setclientid_confirm, nullfree, 0}, 401 402 /* OP_VERIFY = 37 */ 403 {rfs4_op_verify, nullfree, RPC_IDEMPOTENT}, 404 405 /* OP_WRITE = 38 */ 406 {rfs4_op_write, nullfree, 0}, 407 408 /* OP_RELEASE_LOCKOWNER = 39 */ 409 {rfs4_op_release_lockowner, nullfree, 0}, 410 }; 411 412 static uint_t rfsv4disp_cnt = sizeof (rfsv4disptab) / sizeof (rfsv4disptab[0]); 413 414 #define OP_ILLEGAL_IDX (rfsv4disp_cnt) 415 416 #ifdef DEBUG 417 418 int rfs4_fillone_debug = 0; 419 int rfs4_shrlock_debug = 0; 420 int rfs4_no_stub_access = 1; 421 int rfs4_rddir_debug = 0; 422 423 static char *rfs4_op_string[] = { 424 "rfs4_op_null", 425 "rfs4_op_1 unused", 426 "rfs4_op_2 unused", 427 "rfs4_op_access", 428 "rfs4_op_close", 429 "rfs4_op_commit", 430 "rfs4_op_create", 431 "rfs4_op_delegpurge", 432 "rfs4_op_delegreturn", 433 "rfs4_op_getattr", 434 "rfs4_op_getfh", 435 "rfs4_op_link", 436 "rfs4_op_lock", 437 "rfs4_op_lockt", 438 "rfs4_op_locku", 439 "rfs4_op_lookup", 440 "rfs4_op_lookupp", 441 "rfs4_op_nverify", 442 "rfs4_op_open", 443 "rfs4_op_openattr", 444 "rfs4_op_open_confirm", 445 "rfs4_op_open_downgrade", 446 "rfs4_op_putfh", 447 "rfs4_op_putpubfh", 448 "rfs4_op_putrootfh", 449 "rfs4_op_read", 450 "rfs4_op_readdir", 451 "rfs4_op_readlink", 452 "rfs4_op_remove", 453 "rfs4_op_rename", 454 "rfs4_op_renew", 455 "rfs4_op_restorefh", 456 "rfs4_op_savefh", 457 "rfs4_op_secinfo", 458 "rfs4_op_setattr", 459 "rfs4_op_setclientid", 460 "rfs4_op_setclient_confirm", 461 "rfs4_op_verify", 462 "rfs4_op_write", 463 "rfs4_op_release_lockowner", 464 "rfs4_op_illegal" 465 }; 466 #endif 467 468 void rfs4_ss_chkclid(rfs4_client_t *); 469 470 extern size_t strlcpy(char *dst, const char *src, size_t dstsize); 471 472 #ifdef nextdp 473 #undef nextdp 474 #endif 475 #define nextdp(dp) ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen)) 476 477 static const fs_operation_def_t nfs4_rd_deleg_tmpl[] = { 478 VOPNAME_OPEN, { .femop_open = deleg_rd_open }, 479 VOPNAME_WRITE, { .femop_write = deleg_rd_write }, 480 VOPNAME_SETATTR, { .femop_setattr = deleg_rd_setattr }, 481 VOPNAME_RWLOCK, { .femop_rwlock = deleg_rd_rwlock }, 482 VOPNAME_SPACE, { .femop_space = deleg_rd_space }, 483 VOPNAME_SETSECATTR, { .femop_setsecattr = deleg_rd_setsecattr }, 484 VOPNAME_VNEVENT, { .femop_vnevent = deleg_rd_vnevent }, 485 NULL, NULL 486 }; 487 static const fs_operation_def_t nfs4_wr_deleg_tmpl[] = { 488 VOPNAME_OPEN, { .femop_open = deleg_wr_open }, 489 VOPNAME_READ, { .femop_read = deleg_wr_read }, 490 VOPNAME_WRITE, { .femop_write = deleg_wr_write }, 491 VOPNAME_SETATTR, { .femop_setattr = deleg_wr_setattr }, 492 VOPNAME_RWLOCK, { .femop_rwlock = deleg_wr_rwlock }, 493 VOPNAME_SPACE, { .femop_space = deleg_wr_space }, 494 VOPNAME_SETSECATTR, { .femop_setsecattr = deleg_wr_setsecattr }, 495 VOPNAME_VNEVENT, { .femop_vnevent = deleg_wr_vnevent }, 496 NULL, NULL 497 }; 498 499 int 500 rfs4_srvrinit(void) 501 { 502 timespec32_t verf; 503 int error; 504 extern void rfs4_attr_init(); 505 extern krwlock_t rfs4_deleg_policy_lock; 506 507 /* 508 * The following algorithm attempts to find a unique verifier 509 * to be used as the write verifier returned from the server 510 * to the client. It is important that this verifier change 511 * whenever the server reboots. Of secondary importance, it 512 * is important for the verifier to be unique between two 513 * different servers. 514 * 515 * Thus, an attempt is made to use the system hostid and the 516 * current time in seconds when the nfssrv kernel module is 517 * loaded. It is assumed that an NFS server will not be able 518 * to boot and then to reboot in less than a second. If the 519 * hostid has not been set, then the current high resolution 520 * time is used. This will ensure different verifiers each 521 * time the server reboots and minimize the chances that two 522 * different servers will have the same verifier. 523 * XXX - this is broken on LP64 kernels. 524 */ 525 verf.tv_sec = (time_t)zone_get_hostid(NULL); 526 if (verf.tv_sec != 0) { 527 verf.tv_nsec = gethrestime_sec(); 528 } else { 529 timespec_t tverf; 530 531 gethrestime(&tverf); 532 verf.tv_sec = (time_t)tverf.tv_sec; 533 verf.tv_nsec = tverf.tv_nsec; 534 } 535 536 Write4verf = *(uint64_t *)&verf; 537 538 rfs4_attr_init(); 539 mutex_init(&rfs4_deleg_lock, NULL, MUTEX_DEFAULT, NULL); 540 541 /* Used to manage create/destroy of server state */ 542 mutex_init(&rfs4_state_lock, NULL, MUTEX_DEFAULT, NULL); 543 544 /* Used to manage access to server instance linked list */ 545 mutex_init(&rfs4_servinst_lock, NULL, MUTEX_DEFAULT, NULL); 546 547 /* Used to manage access to rfs4_deleg_policy */ 548 rw_init(&rfs4_deleg_policy_lock, NULL, RW_DEFAULT, NULL); 549 550 error = fem_create("deleg_rdops", nfs4_rd_deleg_tmpl, &deleg_rdops); 551 if (error != 0) { 552 rfs4_disable_delegation(); 553 } else { 554 error = fem_create("deleg_wrops", nfs4_wr_deleg_tmpl, 555 &deleg_wrops); 556 if (error != 0) { 557 rfs4_disable_delegation(); 558 fem_free(deleg_rdops); 559 } 560 } 561 562 nfs4_srv_caller_id = fs_new_caller_id(); 563 564 lockt_sysid = lm_alloc_sysidt(); 565 566 vsd_create(&nfs4_srv_vkey, NULL); 567 568 return (0); 569 } 570 571 void 572 rfs4_srvrfini(void) 573 { 574 extern krwlock_t rfs4_deleg_policy_lock; 575 576 if (lockt_sysid != LM_NOSYSID) { 577 lm_free_sysidt(lockt_sysid); 578 lockt_sysid = LM_NOSYSID; 579 } 580 581 mutex_destroy(&rfs4_deleg_lock); 582 mutex_destroy(&rfs4_state_lock); 583 rw_destroy(&rfs4_deleg_policy_lock); 584 585 fem_free(deleg_rdops); 586 fem_free(deleg_wrops); 587 } 588 589 void 590 rfs4_init_compound_state(struct compound_state *cs) 591 { 592 bzero(cs, sizeof (*cs)); 593 cs->cont = TRUE; 594 cs->access = CS_ACCESS_DENIED; 595 cs->deleg = FALSE; 596 cs->mandlock = FALSE; 597 cs->fh.nfs_fh4_val = cs->fhbuf; 598 } 599 600 void 601 rfs4_grace_start(rfs4_servinst_t *sip) 602 { 603 rw_enter(&sip->rwlock, RW_WRITER); 604 sip->start_time = (time_t)TICK_TO_SEC(lbolt); 605 sip->grace_period = rfs4_grace_period; 606 rw_exit(&sip->rwlock); 607 } 608 609 /* 610 * returns true if the instance's grace period has never been started 611 */ 612 int 613 rfs4_servinst_grace_new(rfs4_servinst_t *sip) 614 { 615 time_t start_time; 616 617 rw_enter(&sip->rwlock, RW_READER); 618 start_time = sip->start_time; 619 rw_exit(&sip->rwlock); 620 621 return (start_time == 0); 622 } 623 624 /* 625 * Indicates if server instance is within the 626 * grace period. 627 */ 628 int 629 rfs4_servinst_in_grace(rfs4_servinst_t *sip) 630 { 631 time_t grace_expiry; 632 633 rw_enter(&sip->rwlock, RW_READER); 634 grace_expiry = sip->start_time + sip->grace_period; 635 rw_exit(&sip->rwlock); 636 637 return (((time_t)TICK_TO_SEC(lbolt)) < grace_expiry); 638 } 639 640 int 641 rfs4_clnt_in_grace(rfs4_client_t *cp) 642 { 643 ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0); 644 645 return (rfs4_servinst_in_grace(cp->server_instance)); 646 } 647 648 /* 649 * reset all currently active grace periods 650 */ 651 void 652 rfs4_grace_reset_all(void) 653 { 654 rfs4_servinst_t *sip; 655 656 mutex_enter(&rfs4_servinst_lock); 657 for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev) 658 if (rfs4_servinst_in_grace(sip)) 659 rfs4_grace_start(sip); 660 mutex_exit(&rfs4_servinst_lock); 661 } 662 663 /* 664 * start any new instances' grace periods 665 */ 666 void 667 rfs4_grace_start_new(void) 668 { 669 rfs4_servinst_t *sip; 670 671 mutex_enter(&rfs4_servinst_lock); 672 for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev) 673 if (rfs4_servinst_grace_new(sip)) 674 rfs4_grace_start(sip); 675 mutex_exit(&rfs4_servinst_lock); 676 } 677 678 static rfs4_dss_path_t * 679 rfs4_dss_newpath(rfs4_servinst_t *sip, char *path, unsigned index) 680 { 681 size_t len; 682 rfs4_dss_path_t *dss_path; 683 684 dss_path = kmem_alloc(sizeof (rfs4_dss_path_t), KM_SLEEP); 685 686 /* 687 * Take a copy of the string, since the original may be overwritten. 688 * Sadly, no strdup() in the kernel. 689 */ 690 /* allow for NUL */ 691 len = strlen(path) + 1; 692 dss_path->path = kmem_alloc(len, KM_SLEEP); 693 (void) strlcpy(dss_path->path, path, len); 694 695 /* associate with servinst */ 696 dss_path->sip = sip; 697 dss_path->index = index; 698 699 /* 700 * Add to list of served paths. 701 * No locking required, as we're only ever called at startup. 702 */ 703 if (rfs4_dss_pathlist == NULL) { 704 /* this is the first dss_path_t */ 705 706 /* needed for insque/remque */ 707 dss_path->next = dss_path->prev = dss_path; 708 709 rfs4_dss_pathlist = dss_path; 710 } else { 711 insque(dss_path, rfs4_dss_pathlist); 712 } 713 714 return (dss_path); 715 } 716 717 /* 718 * Create a new server instance, and make it the currently active instance. 719 * Note that starting the grace period too early will reduce the clients' 720 * recovery window. 721 */ 722 void 723 rfs4_servinst_create(int start_grace, int dss_npaths, char **dss_paths) 724 { 725 unsigned i; 726 rfs4_servinst_t *sip; 727 rfs4_oldstate_t *oldstate; 728 729 sip = kmem_alloc(sizeof (rfs4_servinst_t), KM_SLEEP); 730 rw_init(&sip->rwlock, NULL, RW_DEFAULT, NULL); 731 732 sip->start_time = (time_t)0; 733 sip->grace_period = (time_t)0; 734 sip->next = NULL; 735 sip->prev = NULL; 736 737 rw_init(&sip->oldstate_lock, NULL, RW_DEFAULT, NULL); 738 /* 739 * This initial dummy entry is required to setup for insque/remque. 740 * It must be skipped over whenever the list is traversed. 741 */ 742 oldstate = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP); 743 /* insque/remque require initial list entry to be self-terminated */ 744 oldstate->next = oldstate; 745 oldstate->prev = oldstate; 746 sip->oldstate = oldstate; 747 748 749 sip->dss_npaths = dss_npaths; 750 sip->dss_paths = kmem_alloc(dss_npaths * 751 sizeof (rfs4_dss_path_t *), KM_SLEEP); 752 753 for (i = 0; i < dss_npaths; i++) { 754 sip->dss_paths[i] = rfs4_dss_newpath(sip, dss_paths[i], i); 755 } 756 757 mutex_enter(&rfs4_servinst_lock); 758 if (rfs4_cur_servinst != NULL) { 759 /* add to linked list */ 760 sip->prev = rfs4_cur_servinst; 761 rfs4_cur_servinst->next = sip; 762 } 763 if (start_grace) 764 rfs4_grace_start(sip); 765 /* make the new instance "current" */ 766 rfs4_cur_servinst = sip; 767 768 mutex_exit(&rfs4_servinst_lock); 769 } 770 771 /* 772 * In future, we might add a rfs4_servinst_destroy(sip) but, for now, destroy 773 * all instances directly. 774 */ 775 void 776 rfs4_servinst_destroy_all(void) 777 { 778 rfs4_servinst_t *sip, *prev, *current; 779 #ifdef DEBUG 780 int n = 0; 781 #endif 782 783 mutex_enter(&rfs4_servinst_lock); 784 ASSERT(rfs4_cur_servinst != NULL); 785 current = rfs4_cur_servinst; 786 rfs4_cur_servinst = NULL; 787 for (sip = current; sip != NULL; sip = prev) { 788 prev = sip->prev; 789 rw_destroy(&sip->rwlock); 790 if (sip->oldstate) 791 kmem_free(sip->oldstate, sizeof (rfs4_oldstate_t)); 792 if (sip->dss_paths) 793 kmem_free(sip->dss_paths, 794 sip->dss_npaths * sizeof (rfs4_dss_path_t *)); 795 kmem_free(sip, sizeof (rfs4_servinst_t)); 796 #ifdef DEBUG 797 n++; 798 #endif 799 } 800 mutex_exit(&rfs4_servinst_lock); 801 } 802 803 /* 804 * Assign the current server instance to a client_t. 805 * Should be called with cp->dbe held. 806 */ 807 void 808 rfs4_servinst_assign(rfs4_client_t *cp, rfs4_servinst_t *sip) 809 { 810 ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0); 811 812 /* 813 * The lock ensures that if the current instance is in the process 814 * of changing, we will see the new one. 815 */ 816 mutex_enter(&rfs4_servinst_lock); 817 cp->server_instance = sip; 818 mutex_exit(&rfs4_servinst_lock); 819 } 820 821 rfs4_servinst_t * 822 rfs4_servinst(rfs4_client_t *cp) 823 { 824 ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0); 825 826 return (cp->server_instance); 827 } 828 829 /* ARGSUSED */ 830 static void 831 nullfree(caddr_t resop) 832 { 833 } 834 835 /* 836 * This is a fall-through for invalid or not implemented (yet) ops 837 */ 838 /* ARGSUSED */ 839 static void 840 rfs4_op_inval(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 841 struct compound_state *cs) 842 { 843 *cs->statusp = *((nfsstat4 *)&(resop)->nfs_resop4_u) = NFS4ERR_INVAL; 844 } 845 846 /* 847 * Check if the security flavor, nfsnum, is in the flavor_list. 848 */ 849 bool_t 850 in_flavor_list(int nfsnum, int *flavor_list, int count) 851 { 852 int i; 853 854 for (i = 0; i < count; i++) { 855 if (nfsnum == flavor_list[i]) 856 return (TRUE); 857 } 858 return (FALSE); 859 } 860 861 /* 862 * Used by rfs4_op_secinfo to get the security information from the 863 * export structure associated with the component. 864 */ 865 /* ARGSUSED */ 866 static nfsstat4 867 do_rfs4_op_secinfo(struct compound_state *cs, char *nm, SECINFO4res *resp) 868 { 869 int error, different_export = 0; 870 vnode_t *dvp, *vp, *tvp; 871 struct exportinfo *exi = NULL; 872 fid_t fid; 873 uint_t count, i; 874 secinfo4 *resok_val; 875 struct secinfo *secp; 876 seconfig_t *si; 877 bool_t did_traverse; 878 int dotdot, walk; 879 880 dvp = cs->vp; 881 dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0'); 882 883 /* 884 * If dotdotting, then need to check whether it's above the 885 * root of a filesystem, or above an export point. 886 */ 887 if (dotdot) { 888 889 /* 890 * If dotdotting at the root of a filesystem, then 891 * need to traverse back to the mounted-on filesystem 892 * and do the dotdot lookup there. 893 */ 894 if (cs->vp->v_flag & VROOT) { 895 896 /* 897 * If at the system root, then can 898 * go up no further. 899 */ 900 if (VN_CMP(dvp, rootdir)) 901 return (puterrno4(ENOENT)); 902 903 /* 904 * Traverse back to the mounted-on filesystem 905 */ 906 dvp = untraverse(cs->vp); 907 908 /* 909 * Set the different_export flag so we remember 910 * to pick up a new exportinfo entry for 911 * this new filesystem. 912 */ 913 different_export = 1; 914 } else { 915 916 /* 917 * If dotdotting above an export point then set 918 * the different_export to get new export info. 919 */ 920 different_export = nfs_exported(cs->exi, cs->vp); 921 } 922 } 923 924 /* 925 * Get the vnode for the component "nm". 926 */ 927 error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cs->cr, 928 NULL, NULL, NULL); 929 if (error) 930 return (puterrno4(error)); 931 932 /* 933 * If the vnode is in a pseudo filesystem, or if the security flavor 934 * used in the request is valid but not an explicitly shared flavor, 935 * or the access bit indicates that this is a limited access, 936 * check whether this vnode is visible. 937 */ 938 if (!different_export && 939 (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) || 940 cs->access & CS_ACCESS_LIMITED)) { 941 if (! nfs_visible(cs->exi, vp, &different_export)) { 942 VN_RELE(vp); 943 return (puterrno4(ENOENT)); 944 } 945 } 946 947 /* 948 * If it's a mountpoint, then traverse it. 949 */ 950 if (vn_ismntpt(vp)) { 951 tvp = vp; 952 if ((error = traverse(&tvp)) != 0) { 953 VN_RELE(vp); 954 return (puterrno4(error)); 955 } 956 /* remember that we had to traverse mountpoint */ 957 did_traverse = TRUE; 958 vp = tvp; 959 different_export = 1; 960 } else if (vp->v_vfsp != dvp->v_vfsp) { 961 /* 962 * If vp isn't a mountpoint and the vfs ptrs aren't the same, 963 * then vp is probably an LOFS object. We don't need the 964 * realvp, we just need to know that we might have crossed 965 * a server fs boundary and need to call checkexport4. 966 * (LOFS lookup hides server fs mountpoints, and actually calls 967 * traverse) 968 */ 969 different_export = 1; 970 did_traverse = FALSE; 971 } 972 973 /* 974 * Get the export information for it. 975 */ 976 if (different_export) { 977 978 bzero(&fid, sizeof (fid)); 979 fid.fid_len = MAXFIDSZ; 980 error = vop_fid_pseudo(vp, &fid); 981 if (error) { 982 VN_RELE(vp); 983 return (puterrno4(error)); 984 } 985 986 if (dotdot) 987 exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE); 988 else 989 exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp); 990 991 if (exi == NULL) { 992 if (did_traverse == TRUE) { 993 /* 994 * If this vnode is a mounted-on vnode, 995 * but the mounted-on file system is not 996 * exported, send back the secinfo for 997 * the exported node that the mounted-on 998 * vnode lives in. 999 */ 1000 exi = cs->exi; 1001 } else { 1002 VN_RELE(vp); 1003 return (puterrno4(EACCES)); 1004 } 1005 } 1006 } else { 1007 exi = cs->exi; 1008 } 1009 ASSERT(exi != NULL); 1010 1011 1012 /* 1013 * Create the secinfo result based on the security information 1014 * from the exportinfo structure (exi). 1015 * 1016 * Return all flavors for a pseudo node. 1017 * For a real export node, return the flavor that the client 1018 * has access with. 1019 */ 1020 ASSERT(RW_LOCK_HELD(&exported_lock)); 1021 if (PSEUDO(exi)) { 1022 count = exi->exi_export.ex_seccnt; /* total sec count */ 1023 resok_val = kmem_alloc(count * sizeof (secinfo4), KM_SLEEP); 1024 secp = exi->exi_export.ex_secinfo; 1025 1026 for (i = 0; i < count; i++) { 1027 si = &secp[i].s_secinfo; 1028 resok_val[i].flavor = si->sc_rpcnum; 1029 if (resok_val[i].flavor == RPCSEC_GSS) { 1030 rpcsec_gss_info *info; 1031 1032 info = &resok_val[i].flavor_info; 1033 info->qop = si->sc_qop; 1034 info->service = (rpc_gss_svc_t)si->sc_service; 1035 1036 /* get oid opaque data */ 1037 info->oid.sec_oid4_len = 1038 si->sc_gss_mech_type->length; 1039 info->oid.sec_oid4_val = kmem_alloc( 1040 si->sc_gss_mech_type->length, KM_SLEEP); 1041 bcopy( 1042 si->sc_gss_mech_type->elements, 1043 info->oid.sec_oid4_val, 1044 info->oid.sec_oid4_len); 1045 } 1046 } 1047 resp->SECINFO4resok_len = count; 1048 resp->SECINFO4resok_val = resok_val; 1049 } else { 1050 int ret_cnt = 0, k = 0; 1051 int *flavor_list; 1052 1053 count = exi->exi_export.ex_seccnt; /* total sec count */ 1054 secp = exi->exi_export.ex_secinfo; 1055 1056 flavor_list = kmem_alloc(count * sizeof (int), KM_SLEEP); 1057 /* find out which flavors to return */ 1058 for (i = 0; i < count; i ++) { 1059 int access, flavor, perm; 1060 1061 flavor = secp[i].s_secinfo.sc_nfsnum; 1062 perm = secp[i].s_flags; 1063 1064 access = nfsauth4_secinfo_access(exi, cs->req, 1065 flavor, perm); 1066 1067 if (! (access & NFSAUTH_DENIED) && 1068 ! (access & NFSAUTH_WRONGSEC)) { 1069 flavor_list[ret_cnt] = flavor; 1070 ret_cnt++; 1071 } 1072 } 1073 1074 /* Create the returning SECINFO value */ 1075 resok_val = kmem_alloc(ret_cnt * sizeof (secinfo4), KM_SLEEP); 1076 1077 for (i = 0; i < count; i++) { 1078 /* 1079 * If the flavor is in the flavor list, 1080 * fill in resok_val. 1081 */ 1082 si = &secp[i].s_secinfo; 1083 if (in_flavor_list(si->sc_nfsnum, 1084 flavor_list, ret_cnt)) { 1085 resok_val[k].flavor = si->sc_rpcnum; 1086 if (resok_val[k].flavor == RPCSEC_GSS) { 1087 rpcsec_gss_info *info; 1088 1089 info = &resok_val[k].flavor_info; 1090 info->qop = si->sc_qop; 1091 info->service = (rpc_gss_svc_t) 1092 si->sc_service; 1093 1094 /* get oid opaque data */ 1095 info->oid.sec_oid4_len = 1096 si->sc_gss_mech_type->length; 1097 info->oid.sec_oid4_val = kmem_alloc( 1098 si->sc_gss_mech_type->length, 1099 KM_SLEEP); 1100 bcopy(si->sc_gss_mech_type->elements, 1101 info->oid.sec_oid4_val, 1102 info->oid.sec_oid4_len); 1103 } 1104 k++; 1105 } 1106 if (k >= ret_cnt) 1107 break; 1108 } 1109 resp->SECINFO4resok_len = ret_cnt; 1110 resp->SECINFO4resok_val = resok_val; 1111 kmem_free(flavor_list, count * sizeof (int)); 1112 } 1113 1114 VN_RELE(vp); 1115 return (NFS4_OK); 1116 } 1117 1118 /* 1119 * SECINFO (Operation 33): Obtain required security information on 1120 * the component name in the format of (security-mechanism-oid, qop, service) 1121 * triplets. 1122 */ 1123 /* ARGSUSED */ 1124 static void 1125 rfs4_op_secinfo(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 1126 struct compound_state *cs) 1127 { 1128 SECINFO4args *args = &argop->nfs_argop4_u.opsecinfo; 1129 SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo; 1130 utf8string *utfnm = &args->name; 1131 uint_t len; 1132 char *nm; 1133 struct sockaddr *ca; 1134 char *name = NULL; 1135 1136 DTRACE_NFSV4_2(op__secinfo__start, struct compound_state *, cs, 1137 SECINFO4args *, args); 1138 1139 /* 1140 * Current file handle (cfh) should have been set before getting 1141 * into this function. If not, return error. 1142 */ 1143 if (cs->vp == NULL) { 1144 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 1145 goto out; 1146 } 1147 1148 if (cs->vp->v_type != VDIR) { 1149 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 1150 goto out; 1151 } 1152 1153 /* 1154 * Verify the component name. If failed, error out, but 1155 * do not error out if the component name is a "..". 1156 * SECINFO will return its parents secinfo data for SECINFO "..". 1157 */ 1158 if (!utf8_dir_verify(utfnm)) { 1159 if (utfnm->utf8string_len != 2 || 1160 utfnm->utf8string_val[0] != '.' || 1161 utfnm->utf8string_val[1] != '.') { 1162 *cs->statusp = resp->status = NFS4ERR_INVAL; 1163 goto out; 1164 } 1165 } 1166 1167 nm = utf8_to_str(utfnm, &len, NULL); 1168 if (nm == NULL) { 1169 *cs->statusp = resp->status = NFS4ERR_INVAL; 1170 goto out; 1171 } 1172 1173 if (len > MAXNAMELEN) { 1174 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 1175 kmem_free(nm, len); 1176 goto out; 1177 } 1178 /* If necessary, convert to UTF-8 for illbehaved clients */ 1179 1180 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 1181 name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND, 1182 MAXPATHLEN + 1); 1183 1184 if (name == NULL) { 1185 *cs->statusp = resp->status = NFS4ERR_INVAL; 1186 kmem_free(nm, len); 1187 goto out; 1188 } 1189 1190 1191 *cs->statusp = resp->status = do_rfs4_op_secinfo(cs, name, resp); 1192 1193 if (name != nm) 1194 kmem_free(name, MAXPATHLEN + 1); 1195 kmem_free(nm, len); 1196 1197 out: 1198 DTRACE_NFSV4_2(op__secinfo__done, struct compound_state *, cs, 1199 SECINFO4res *, resp); 1200 } 1201 1202 /* 1203 * Free SECINFO result. 1204 */ 1205 /* ARGSUSED */ 1206 static void 1207 rfs4_op_secinfo_free(nfs_resop4 *resop) 1208 { 1209 SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo; 1210 int count, i; 1211 secinfo4 *resok_val; 1212 1213 /* If this is not an Ok result, nothing to free. */ 1214 if (resp->status != NFS4_OK) { 1215 return; 1216 } 1217 1218 count = resp->SECINFO4resok_len; 1219 resok_val = resp->SECINFO4resok_val; 1220 1221 for (i = 0; i < count; i++) { 1222 if (resok_val[i].flavor == RPCSEC_GSS) { 1223 rpcsec_gss_info *info; 1224 1225 info = &resok_val[i].flavor_info; 1226 kmem_free(info->oid.sec_oid4_val, 1227 info->oid.sec_oid4_len); 1228 } 1229 } 1230 kmem_free(resok_val, count * sizeof (secinfo4)); 1231 resp->SECINFO4resok_len = 0; 1232 resp->SECINFO4resok_val = NULL; 1233 } 1234 1235 /* ARGSUSED */ 1236 static void 1237 rfs4_op_access(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 1238 struct compound_state *cs) 1239 { 1240 ACCESS4args *args = &argop->nfs_argop4_u.opaccess; 1241 ACCESS4res *resp = &resop->nfs_resop4_u.opaccess; 1242 int error; 1243 vnode_t *vp; 1244 struct vattr va; 1245 int checkwriteperm; 1246 cred_t *cr = cs->cr; 1247 bslabel_t *clabel, *slabel; 1248 ts_label_t *tslabel; 1249 boolean_t admin_low_client; 1250 1251 DTRACE_NFSV4_2(op__access__start, struct compound_state *, cs, 1252 ACCESS4args *, args); 1253 1254 #if 0 /* XXX allow access even if !cs->access. Eventually only pseudo fs */ 1255 if (cs->access == CS_ACCESS_DENIED) { 1256 *cs->statusp = resp->status = NFS4ERR_ACCESS; 1257 goto out; 1258 } 1259 #endif 1260 if (cs->vp == NULL) { 1261 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 1262 goto out; 1263 } 1264 1265 ASSERT(cr != NULL); 1266 1267 vp = cs->vp; 1268 1269 /* 1270 * If the file system is exported read only, it is not appropriate 1271 * to check write permissions for regular files and directories. 1272 * Special files are interpreted by the client, so the underlying 1273 * permissions are sent back to the client for interpretation. 1274 */ 1275 if (rdonly4(cs->exi, cs->vp, req) && 1276 (vp->v_type == VREG || vp->v_type == VDIR)) 1277 checkwriteperm = 0; 1278 else 1279 checkwriteperm = 1; 1280 1281 /* 1282 * XXX 1283 * We need the mode so that we can correctly determine access 1284 * permissions relative to a mandatory lock file. Access to 1285 * mandatory lock files is denied on the server, so it might 1286 * as well be reflected to the server during the open. 1287 */ 1288 va.va_mask = AT_MODE; 1289 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 1290 if (error) { 1291 *cs->statusp = resp->status = puterrno4(error); 1292 goto out; 1293 } 1294 resp->access = 0; 1295 resp->supported = 0; 1296 1297 if (is_system_labeled()) { 1298 ASSERT(req->rq_label != NULL); 1299 clabel = req->rq_label; 1300 DTRACE_PROBE2(tx__rfs4__log__info__opaccess__clabel, char *, 1301 "got client label from request(1)", 1302 struct svc_req *, req); 1303 if (!blequal(&l_admin_low->tsl_label, clabel)) { 1304 if ((tslabel = nfs_getflabel(vp, cs->exi)) == NULL) { 1305 *cs->statusp = resp->status = puterrno4(EACCES); 1306 goto out; 1307 } 1308 slabel = label2bslabel(tslabel); 1309 DTRACE_PROBE3(tx__rfs4__log__info__opaccess__slabel, 1310 char *, "got server label(1) for vp(2)", 1311 bslabel_t *, slabel, vnode_t *, vp); 1312 1313 admin_low_client = B_FALSE; 1314 } else 1315 admin_low_client = B_TRUE; 1316 } 1317 1318 if (args->access & ACCESS4_READ) { 1319 error = VOP_ACCESS(vp, VREAD, 0, cr, NULL); 1320 if (!error && !MANDLOCK(vp, va.va_mode) && 1321 (!is_system_labeled() || admin_low_client || 1322 bldominates(clabel, slabel))) 1323 resp->access |= ACCESS4_READ; 1324 resp->supported |= ACCESS4_READ; 1325 } 1326 if ((args->access & ACCESS4_LOOKUP) && vp->v_type == VDIR) { 1327 error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL); 1328 if (!error && (!is_system_labeled() || admin_low_client || 1329 bldominates(clabel, slabel))) 1330 resp->access |= ACCESS4_LOOKUP; 1331 resp->supported |= ACCESS4_LOOKUP; 1332 } 1333 if (checkwriteperm && 1334 (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND))) { 1335 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL); 1336 if (!error && !MANDLOCK(vp, va.va_mode) && 1337 (!is_system_labeled() || admin_low_client || 1338 blequal(clabel, slabel))) 1339 resp->access |= 1340 (args->access & (ACCESS4_MODIFY | ACCESS4_EXTEND)); 1341 resp->supported |= (ACCESS4_MODIFY | ACCESS4_EXTEND); 1342 } 1343 1344 if (checkwriteperm && 1345 (args->access & ACCESS4_DELETE) && vp->v_type == VDIR) { 1346 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL); 1347 if (!error && (!is_system_labeled() || admin_low_client || 1348 blequal(clabel, slabel))) 1349 resp->access |= ACCESS4_DELETE; 1350 resp->supported |= ACCESS4_DELETE; 1351 } 1352 if (args->access & ACCESS4_EXECUTE && vp->v_type != VDIR) { 1353 error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL); 1354 if (!error && !MANDLOCK(vp, va.va_mode) && 1355 (!is_system_labeled() || admin_low_client || 1356 bldominates(clabel, slabel))) 1357 resp->access |= ACCESS4_EXECUTE; 1358 resp->supported |= ACCESS4_EXECUTE; 1359 } 1360 1361 if (is_system_labeled() && !admin_low_client) 1362 label_rele(tslabel); 1363 1364 *cs->statusp = resp->status = NFS4_OK; 1365 out: 1366 DTRACE_NFSV4_2(op__access__done, struct compound_state *, cs, 1367 ACCESS4res *, resp); 1368 } 1369 1370 /* ARGSUSED */ 1371 static void 1372 rfs4_op_commit(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 1373 struct compound_state *cs) 1374 { 1375 COMMIT4args *args = &argop->nfs_argop4_u.opcommit; 1376 COMMIT4res *resp = &resop->nfs_resop4_u.opcommit; 1377 int error; 1378 vnode_t *vp = cs->vp; 1379 cred_t *cr = cs->cr; 1380 vattr_t va; 1381 1382 DTRACE_NFSV4_2(op__commit__start, struct compound_state *, cs, 1383 COMMIT4args *, args); 1384 1385 if (vp == NULL) { 1386 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 1387 goto out; 1388 } 1389 if (cs->access == CS_ACCESS_DENIED) { 1390 *cs->statusp = resp->status = NFS4ERR_ACCESS; 1391 goto out; 1392 } 1393 1394 if (args->offset + args->count < args->offset) { 1395 *cs->statusp = resp->status = NFS4ERR_INVAL; 1396 goto out; 1397 } 1398 1399 va.va_mask = AT_UID; 1400 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 1401 1402 /* 1403 * If we can't get the attributes, then we can't do the 1404 * right access checking. So, we'll fail the request. 1405 */ 1406 if (error) { 1407 *cs->statusp = resp->status = puterrno4(error); 1408 goto out; 1409 } 1410 if (rdonly4(cs->exi, cs->vp, req)) { 1411 *cs->statusp = resp->status = NFS4ERR_ROFS; 1412 goto out; 1413 } 1414 1415 if (vp->v_type != VREG) { 1416 if (vp->v_type == VDIR) 1417 resp->status = NFS4ERR_ISDIR; 1418 else 1419 resp->status = NFS4ERR_INVAL; 1420 *cs->statusp = resp->status; 1421 goto out; 1422 } 1423 1424 if (crgetuid(cr) != va.va_uid && 1425 (error = VOP_ACCESS(vp, VWRITE, 0, cs->cr, NULL))) { 1426 *cs->statusp = resp->status = puterrno4(error); 1427 goto out; 1428 } 1429 1430 error = VOP_PUTPAGE(vp, args->offset, args->count, 0, cr, NULL); 1431 if (!error) 1432 error = VOP_FSYNC(vp, FNODSYNC, cr, NULL); 1433 1434 if (error) { 1435 *cs->statusp = resp->status = puterrno4(error); 1436 goto out; 1437 } 1438 1439 *cs->statusp = resp->status = NFS4_OK; 1440 resp->writeverf = Write4verf; 1441 out: 1442 DTRACE_NFSV4_2(op__commit__done, struct compound_state *, cs, 1443 COMMIT4res *, resp); 1444 } 1445 1446 /* 1447 * rfs4_op_mknod is called from rfs4_op_create after all initial verification 1448 * was completed. It does the nfsv4 create for special files. 1449 */ 1450 /* ARGSUSED */ 1451 static vnode_t * 1452 do_rfs4_op_mknod(CREATE4args *args, CREATE4res *resp, struct svc_req *req, 1453 struct compound_state *cs, vattr_t *vap, char *nm) 1454 { 1455 int error; 1456 cred_t *cr = cs->cr; 1457 vnode_t *dvp = cs->vp; 1458 vnode_t *vp = NULL; 1459 int mode; 1460 enum vcexcl excl; 1461 1462 switch (args->type) { 1463 case NF4CHR: 1464 case NF4BLK: 1465 if (secpolicy_sys_devices(cr) != 0) { 1466 *cs->statusp = resp->status = NFS4ERR_PERM; 1467 return (NULL); 1468 } 1469 if (args->type == NF4CHR) 1470 vap->va_type = VCHR; 1471 else 1472 vap->va_type = VBLK; 1473 vap->va_rdev = makedevice(args->ftype4_u.devdata.specdata1, 1474 args->ftype4_u.devdata.specdata2); 1475 vap->va_mask |= AT_RDEV; 1476 break; 1477 case NF4SOCK: 1478 vap->va_type = VSOCK; 1479 break; 1480 case NF4FIFO: 1481 vap->va_type = VFIFO; 1482 break; 1483 default: 1484 *cs->statusp = resp->status = NFS4ERR_BADTYPE; 1485 return (NULL); 1486 } 1487 1488 /* 1489 * Must specify the mode. 1490 */ 1491 if (!(vap->va_mask & AT_MODE)) { 1492 *cs->statusp = resp->status = NFS4ERR_INVAL; 1493 return (NULL); 1494 } 1495 1496 excl = EXCL; 1497 1498 mode = 0; 1499 1500 error = VOP_CREATE(dvp, nm, vap, excl, mode, &vp, cr, 0, NULL, NULL); 1501 if (error) { 1502 *cs->statusp = resp->status = puterrno4(error); 1503 return (NULL); 1504 } 1505 return (vp); 1506 } 1507 1508 /* 1509 * nfsv4 create is used to create non-regular files. For regular files, 1510 * use nfsv4 open. 1511 */ 1512 /* ARGSUSED */ 1513 static void 1514 rfs4_op_create(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 1515 struct compound_state *cs) 1516 { 1517 CREATE4args *args = &argop->nfs_argop4_u.opcreate; 1518 CREATE4res *resp = &resop->nfs_resop4_u.opcreate; 1519 int error; 1520 struct vattr bva, iva, iva2, ava, *vap; 1521 cred_t *cr = cs->cr; 1522 vnode_t *dvp = cs->vp; 1523 vnode_t *vp = NULL; 1524 vnode_t *realvp; 1525 char *nm, *lnm; 1526 uint_t len, llen; 1527 int syncval = 0; 1528 struct nfs4_svgetit_arg sarg; 1529 struct nfs4_ntov_table ntov; 1530 struct statvfs64 sb; 1531 nfsstat4 status; 1532 struct sockaddr *ca; 1533 char *name = NULL; 1534 char *lname = NULL; 1535 1536 DTRACE_NFSV4_2(op__create__start, struct compound_state *, cs, 1537 CREATE4args *, args); 1538 1539 resp->attrset = 0; 1540 1541 if (dvp == NULL) { 1542 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 1543 goto out; 1544 } 1545 1546 /* 1547 * If there is an unshared filesystem mounted on this vnode, 1548 * do not allow to create an object in this directory. 1549 */ 1550 if (vn_ismntpt(dvp)) { 1551 *cs->statusp = resp->status = NFS4ERR_ACCESS; 1552 goto out; 1553 } 1554 1555 /* Verify that type is correct */ 1556 switch (args->type) { 1557 case NF4LNK: 1558 case NF4BLK: 1559 case NF4CHR: 1560 case NF4SOCK: 1561 case NF4FIFO: 1562 case NF4DIR: 1563 break; 1564 default: 1565 *cs->statusp = resp->status = NFS4ERR_BADTYPE; 1566 goto out; 1567 }; 1568 1569 if (cs->access == CS_ACCESS_DENIED) { 1570 *cs->statusp = resp->status = NFS4ERR_ACCESS; 1571 goto out; 1572 } 1573 if (dvp->v_type != VDIR) { 1574 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 1575 goto out; 1576 } 1577 if (!utf8_dir_verify(&args->objname)) { 1578 *cs->statusp = resp->status = NFS4ERR_INVAL; 1579 goto out; 1580 } 1581 1582 if (rdonly4(cs->exi, cs->vp, req)) { 1583 *cs->statusp = resp->status = NFS4ERR_ROFS; 1584 goto out; 1585 } 1586 1587 /* 1588 * Name of newly created object 1589 */ 1590 nm = utf8_to_fn(&args->objname, &len, NULL); 1591 if (nm == NULL) { 1592 *cs->statusp = resp->status = NFS4ERR_INVAL; 1593 goto out; 1594 } 1595 1596 if (len > MAXNAMELEN) { 1597 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 1598 kmem_free(nm, len); 1599 goto out; 1600 } 1601 1602 /* If necessary, convert to UTF-8 for poorly behaved clients */ 1603 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 1604 name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND, 1605 MAXPATHLEN + 1); 1606 1607 if (name == NULL) { 1608 *cs->statusp = resp->status = NFS4ERR_INVAL; 1609 kmem_free(nm, len); 1610 goto out; 1611 } 1612 1613 resp->attrset = 0; 1614 1615 sarg.sbp = &sb; 1616 nfs4_ntov_table_init(&ntov); 1617 1618 status = do_rfs4_set_attrs(&resp->attrset, 1619 &args->createattrs, cs, &sarg, &ntov, NFS4ATTR_SETIT); 1620 1621 if (sarg.vap->va_mask == 0 && status == NFS4_OK) 1622 status = NFS4ERR_INVAL; 1623 1624 if (status != NFS4_OK) { 1625 *cs->statusp = resp->status = status; 1626 kmem_free(nm, len); 1627 nfs4_ntov_table_free(&ntov, &sarg); 1628 resp->attrset = 0; 1629 goto out; 1630 } 1631 1632 /* Get "before" change value */ 1633 bva.va_mask = AT_CTIME|AT_SEQ; 1634 error = VOP_GETATTR(dvp, &bva, 0, cr, NULL); 1635 if (error) { 1636 *cs->statusp = resp->status = puterrno4(error); 1637 kmem_free(nm, len); 1638 nfs4_ntov_table_free(&ntov, &sarg); 1639 resp->attrset = 0; 1640 goto out; 1641 } 1642 NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bva.va_ctime) 1643 1644 vap = sarg.vap; 1645 1646 /* 1647 * Set default initial values for attributes when not specified 1648 * in createattrs. 1649 */ 1650 if ((vap->va_mask & AT_UID) == 0) { 1651 vap->va_uid = crgetuid(cr); 1652 vap->va_mask |= AT_UID; 1653 } 1654 if ((vap->va_mask & AT_GID) == 0) { 1655 vap->va_gid = crgetgid(cr); 1656 vap->va_mask |= AT_GID; 1657 } 1658 1659 vap->va_mask |= AT_TYPE; 1660 switch (args->type) { 1661 case NF4DIR: 1662 vap->va_type = VDIR; 1663 if ((vap->va_mask & AT_MODE) == 0) { 1664 vap->va_mode = 0700; /* default: owner rwx only */ 1665 vap->va_mask |= AT_MODE; 1666 } 1667 error = VOP_MKDIR(dvp, nm, vap, &vp, cr, NULL, 0, NULL); 1668 if (error) 1669 break; 1670 1671 /* 1672 * Get the initial "after" sequence number, if it fails, 1673 * set to zero 1674 */ 1675 iva.va_mask = AT_SEQ; 1676 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) 1677 iva.va_seq = 0; 1678 break; 1679 case NF4LNK: 1680 vap->va_type = VLNK; 1681 if ((vap->va_mask & AT_MODE) == 0) { 1682 vap->va_mode = 0700; /* default: owner rwx only */ 1683 vap->va_mask |= AT_MODE; 1684 } 1685 1686 /* 1687 * symlink names must be treated as data 1688 */ 1689 lnm = utf8_to_str(&args->ftype4_u.linkdata, &llen, NULL); 1690 1691 if (lnm == NULL) { 1692 *cs->statusp = resp->status = NFS4ERR_INVAL; 1693 if (name != nm) 1694 kmem_free(name, MAXPATHLEN + 1); 1695 kmem_free(nm, len); 1696 nfs4_ntov_table_free(&ntov, &sarg); 1697 resp->attrset = 0; 1698 goto out; 1699 } 1700 1701 if (llen > MAXPATHLEN) { 1702 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 1703 if (name != nm) 1704 kmem_free(name, MAXPATHLEN + 1); 1705 kmem_free(nm, len); 1706 kmem_free(lnm, llen); 1707 nfs4_ntov_table_free(&ntov, &sarg); 1708 resp->attrset = 0; 1709 goto out; 1710 } 1711 1712 lname = nfscmd_convname(ca, cs->exi, lnm, 1713 NFSCMD_CONV_INBOUND, MAXPATHLEN + 1); 1714 1715 if (lname == NULL) { 1716 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT; 1717 if (name != nm) 1718 kmem_free(name, MAXPATHLEN + 1); 1719 kmem_free(nm, len); 1720 kmem_free(lnm, llen); 1721 nfs4_ntov_table_free(&ntov, &sarg); 1722 resp->attrset = 0; 1723 goto out; 1724 } 1725 1726 error = VOP_SYMLINK(dvp, nm, vap, lnm, cr, NULL, 0); 1727 if (lname != lnm) 1728 kmem_free(lname, MAXPATHLEN + 1); 1729 if (lnm != NULL) 1730 kmem_free(lnm, llen); 1731 if (error) 1732 break; 1733 1734 /* 1735 * Get the initial "after" sequence number, if it fails, 1736 * set to zero 1737 */ 1738 iva.va_mask = AT_SEQ; 1739 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) 1740 iva.va_seq = 0; 1741 1742 error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, 1743 NULL, NULL, NULL); 1744 if (error) 1745 break; 1746 1747 /* 1748 * va_seq is not safe over VOP calls, check it again 1749 * if it has changed zero out iva to force atomic = FALSE. 1750 */ 1751 iva2.va_mask = AT_SEQ; 1752 if (VOP_GETATTR(dvp, &iva2, 0, cs->cr, NULL) || 1753 iva2.va_seq != iva.va_seq) 1754 iva.va_seq = 0; 1755 break; 1756 default: 1757 /* 1758 * probably a special file. 1759 */ 1760 if ((vap->va_mask & AT_MODE) == 0) { 1761 vap->va_mode = 0600; /* default: owner rw only */ 1762 vap->va_mask |= AT_MODE; 1763 } 1764 syncval = FNODSYNC; 1765 /* 1766 * We know this will only generate one VOP call 1767 */ 1768 vp = do_rfs4_op_mknod(args, resp, req, cs, vap, nm); 1769 1770 if (vp == NULL) { 1771 if (name != nm) 1772 kmem_free(name, MAXPATHLEN + 1); 1773 kmem_free(nm, len); 1774 nfs4_ntov_table_free(&ntov, &sarg); 1775 resp->attrset = 0; 1776 goto out; 1777 } 1778 1779 /* 1780 * Get the initial "after" sequence number, if it fails, 1781 * set to zero 1782 */ 1783 iva.va_mask = AT_SEQ; 1784 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) 1785 iva.va_seq = 0; 1786 1787 break; 1788 } 1789 if (name != nm) 1790 kmem_free(name, MAXPATHLEN + 1); 1791 kmem_free(nm, len); 1792 1793 if (error) { 1794 *cs->statusp = resp->status = puterrno4(error); 1795 } 1796 1797 /* 1798 * Force modified data and metadata out to stable storage. 1799 */ 1800 (void) VOP_FSYNC(dvp, 0, cr, NULL); 1801 1802 if (resp->status != NFS4_OK) { 1803 if (vp != NULL) 1804 VN_RELE(vp); 1805 nfs4_ntov_table_free(&ntov, &sarg); 1806 resp->attrset = 0; 1807 goto out; 1808 } 1809 1810 /* 1811 * Finish setup of cinfo response, "before" value already set. 1812 * Get "after" change value, if it fails, simply return the 1813 * before value. 1814 */ 1815 ava.va_mask = AT_CTIME|AT_SEQ; 1816 if (VOP_GETATTR(dvp, &ava, 0, cr, NULL)) { 1817 ava.va_ctime = bva.va_ctime; 1818 ava.va_seq = 0; 1819 } 1820 NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, ava.va_ctime); 1821 1822 /* 1823 * True verification that object was created with correct 1824 * attrs is impossible. The attrs could have been changed 1825 * immediately after object creation. If attributes did 1826 * not verify, the only recourse for the server is to 1827 * destroy the object. Maybe if some attrs (like gid) 1828 * are set incorrectly, the object should be destroyed; 1829 * however, seems bad as a default policy. Do we really 1830 * want to destroy an object over one of the times not 1831 * verifying correctly? For these reasons, the server 1832 * currently sets bits in attrset for createattrs 1833 * that were set; however, no verification is done. 1834 * 1835 * vmask_to_nmask accounts for vattr bits set on create 1836 * [do_rfs4_set_attrs() only sets resp bits for 1837 * non-vattr/vfs bits.] 1838 * Mask off any bits set by default so as not to return 1839 * more attrset bits than were requested in createattrs 1840 */ 1841 nfs4_vmask_to_nmask(sarg.vap->va_mask, &resp->attrset); 1842 resp->attrset &= args->createattrs.attrmask; 1843 nfs4_ntov_table_free(&ntov, &sarg); 1844 1845 error = makefh4(&cs->fh, vp, cs->exi); 1846 if (error) { 1847 *cs->statusp = resp->status = puterrno4(error); 1848 } 1849 1850 /* 1851 * The cinfo.atomic = TRUE only if we got no errors, we have 1852 * non-zero va_seq's, and it has incremented by exactly one 1853 * during the creation and it didn't change during the VOP_LOOKUP 1854 * or VOP_FSYNC. 1855 */ 1856 if (!error && bva.va_seq && iva.va_seq && ava.va_seq && 1857 iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq) 1858 resp->cinfo.atomic = TRUE; 1859 else 1860 resp->cinfo.atomic = FALSE; 1861 1862 /* 1863 * Force modified metadata out to stable storage. 1864 * 1865 * if a underlying vp exists, pass it to VOP_FSYNC 1866 */ 1867 if (VOP_REALVP(vp, &realvp, NULL) == 0) 1868 (void) VOP_FSYNC(realvp, syncval, cr, NULL); 1869 else 1870 (void) VOP_FSYNC(vp, syncval, cr, NULL); 1871 1872 if (resp->status != NFS4_OK) { 1873 VN_RELE(vp); 1874 goto out; 1875 } 1876 if (cs->vp) 1877 VN_RELE(cs->vp); 1878 1879 cs->vp = vp; 1880 *cs->statusp = resp->status = NFS4_OK; 1881 out: 1882 DTRACE_NFSV4_2(op__create__done, struct compound_state *, cs, 1883 CREATE4res *, resp); 1884 } 1885 1886 /*ARGSUSED*/ 1887 static void 1888 rfs4_op_delegpurge(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 1889 struct compound_state *cs) 1890 { 1891 DTRACE_NFSV4_2(op__delegpurge__start, struct compound_state *, cs, 1892 DELEGPURGE4args *, &argop->nfs_argop4_u.opdelegpurge); 1893 1894 rfs4_op_inval(argop, resop, req, cs); 1895 1896 DTRACE_NFSV4_2(op__delegpurge__done, struct compound_state *, cs, 1897 DELEGPURGE4res *, &resop->nfs_resop4_u.opdelegpurge); 1898 } 1899 1900 /*ARGSUSED*/ 1901 static void 1902 rfs4_op_delegreturn(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 1903 struct compound_state *cs) 1904 { 1905 DELEGRETURN4args *args = &argop->nfs_argop4_u.opdelegreturn; 1906 DELEGRETURN4res *resp = &resop->nfs_resop4_u.opdelegreturn; 1907 rfs4_deleg_state_t *dsp; 1908 nfsstat4 status; 1909 1910 DTRACE_NFSV4_2(op__delegreturn__start, struct compound_state *, cs, 1911 DELEGRETURN4args *, args); 1912 1913 status = rfs4_get_deleg_state(&args->deleg_stateid, &dsp); 1914 resp->status = *cs->statusp = status; 1915 if (status != NFS4_OK) 1916 goto out; 1917 1918 /* Ensure specified filehandle matches */ 1919 if (cs->vp != dsp->finfo->vp) { 1920 resp->status = *cs->statusp = NFS4ERR_BAD_STATEID; 1921 } else 1922 rfs4_return_deleg(dsp, FALSE); 1923 1924 rfs4_update_lease(dsp->client); 1925 1926 rfs4_deleg_state_rele(dsp); 1927 out: 1928 DTRACE_NFSV4_2(op__delegreturn__done, struct compound_state *, cs, 1929 DELEGRETURN4res *, resp); 1930 } 1931 1932 /* 1933 * Check to see if a given "flavor" is an explicitly shared flavor. 1934 * The assumption of this routine is the "flavor" is already a valid 1935 * flavor in the secinfo list of "exi". 1936 * 1937 * e.g. 1938 * # share -o sec=flavor1 /export 1939 * # share -o sec=flavor2 /export/home 1940 * 1941 * flavor2 is not an explicitly shared flavor for /export, 1942 * however it is in the secinfo list for /export thru the 1943 * server namespace setup. 1944 */ 1945 int 1946 is_exported_sec(int flavor, struct exportinfo *exi) 1947 { 1948 int i; 1949 struct secinfo *sp; 1950 1951 sp = exi->exi_export.ex_secinfo; 1952 for (i = 0; i < exi->exi_export.ex_seccnt; i++) { 1953 if (flavor == sp[i].s_secinfo.sc_nfsnum || 1954 sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) { 1955 return (SEC_REF_EXPORTED(&sp[i])); 1956 } 1957 } 1958 1959 /* Should not reach this point based on the assumption */ 1960 return (0); 1961 } 1962 1963 /* 1964 * Check if the security flavor used in the request matches what is 1965 * required at the export point or at the root pseudo node (exi_root). 1966 * 1967 * returns 1 if there's a match or if exported with AUTH_NONE; 0 otherwise. 1968 * 1969 */ 1970 static int 1971 secinfo_match_or_authnone(struct compound_state *cs) 1972 { 1973 int i; 1974 struct secinfo *sp; 1975 1976 /* 1977 * Check cs->nfsflavor (from the request) against 1978 * the current export data in cs->exi. 1979 */ 1980 sp = cs->exi->exi_export.ex_secinfo; 1981 for (i = 0; i < cs->exi->exi_export.ex_seccnt; i++) { 1982 if (cs->nfsflavor == sp[i].s_secinfo.sc_nfsnum || 1983 sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) 1984 return (1); 1985 } 1986 1987 return (0); 1988 } 1989 1990 /* 1991 * Check the access authority for the client and return the correct error. 1992 */ 1993 nfsstat4 1994 call_checkauth4(struct compound_state *cs, struct svc_req *req) 1995 { 1996 int authres; 1997 1998 /* 1999 * First, check if the security flavor used in the request 2000 * are among the flavors set in the server namespace. 2001 */ 2002 if (!secinfo_match_or_authnone(cs)) { 2003 *cs->statusp = NFS4ERR_WRONGSEC; 2004 return (*cs->statusp); 2005 } 2006 2007 authres = checkauth4(cs, req); 2008 2009 if (authres > 0) { 2010 *cs->statusp = NFS4_OK; 2011 if (! (cs->access & CS_ACCESS_LIMITED)) 2012 cs->access = CS_ACCESS_OK; 2013 } else if (authres == 0) { 2014 *cs->statusp = NFS4ERR_ACCESS; 2015 } else if (authres == -2) { 2016 *cs->statusp = NFS4ERR_WRONGSEC; 2017 } else { 2018 *cs->statusp = NFS4ERR_DELAY; 2019 } 2020 return (*cs->statusp); 2021 } 2022 2023 /* 2024 * bitmap4_to_attrmask is called by getattr and readdir. 2025 * It sets up the vattr mask and determines whether vfsstat call is needed 2026 * based on the input bitmap. 2027 * Returns nfsv4 status. 2028 */ 2029 static nfsstat4 2030 bitmap4_to_attrmask(bitmap4 breq, struct nfs4_svgetit_arg *sargp) 2031 { 2032 int i; 2033 uint_t va_mask; 2034 struct statvfs64 *sbp = sargp->sbp; 2035 2036 sargp->sbp = NULL; 2037 sargp->flag = 0; 2038 sargp->rdattr_error = NFS4_OK; 2039 sargp->mntdfid_set = FALSE; 2040 if (sargp->cs->vp) 2041 sargp->xattr = get_fh4_flag(&sargp->cs->fh, 2042 FH4_ATTRDIR | FH4_NAMEDATTR); 2043 else 2044 sargp->xattr = 0; 2045 2046 /* 2047 * Set rdattr_error_req to true if return error per 2048 * failed entry rather than fail the readdir. 2049 */ 2050 if (breq & FATTR4_RDATTR_ERROR_MASK) 2051 sargp->rdattr_error_req = 1; 2052 else 2053 sargp->rdattr_error_req = 0; 2054 2055 /* 2056 * generate the va_mask 2057 * Handle the easy cases first 2058 */ 2059 switch (breq) { 2060 case NFS4_NTOV_ATTR_MASK: 2061 sargp->vap->va_mask = NFS4_NTOV_ATTR_AT_MASK; 2062 return (NFS4_OK); 2063 2064 case NFS4_FS_ATTR_MASK: 2065 sargp->vap->va_mask = NFS4_FS_ATTR_AT_MASK; 2066 sargp->sbp = sbp; 2067 return (NFS4_OK); 2068 2069 case NFS4_NTOV_ATTR_CACHE_MASK: 2070 sargp->vap->va_mask = NFS4_NTOV_ATTR_CACHE_AT_MASK; 2071 return (NFS4_OK); 2072 2073 case FATTR4_LEASE_TIME_MASK: 2074 sargp->vap->va_mask = 0; 2075 return (NFS4_OK); 2076 2077 default: 2078 va_mask = 0; 2079 for (i = 0; i < nfs4_ntov_map_size; i++) { 2080 if ((breq & nfs4_ntov_map[i].fbit) && 2081 nfs4_ntov_map[i].vbit) 2082 va_mask |= nfs4_ntov_map[i].vbit; 2083 } 2084 2085 /* 2086 * Check is vfsstat is needed 2087 */ 2088 if (breq & NFS4_FS_ATTR_MASK) 2089 sargp->sbp = sbp; 2090 2091 sargp->vap->va_mask = va_mask; 2092 return (NFS4_OK); 2093 } 2094 /* NOTREACHED */ 2095 } 2096 2097 /* 2098 * bitmap4_get_sysattrs is called by getattr and readdir. 2099 * It calls both VOP_GETATTR and VFS_STATVFS calls to get the attrs. 2100 * Returns nfsv4 status. 2101 */ 2102 static nfsstat4 2103 bitmap4_get_sysattrs(struct nfs4_svgetit_arg *sargp) 2104 { 2105 int error; 2106 struct compound_state *cs = sargp->cs; 2107 vnode_t *vp = cs->vp; 2108 2109 if (sargp->sbp != NULL) { 2110 if (error = VFS_STATVFS(vp->v_vfsp, sargp->sbp)) { 2111 sargp->sbp = NULL; /* to identify error */ 2112 return (puterrno4(error)); 2113 } 2114 } 2115 2116 return (rfs4_vop_getattr(vp, sargp->vap, 0, cs->cr)); 2117 } 2118 2119 static void 2120 nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp) 2121 { 2122 ntovp->na = kmem_zalloc(sizeof (union nfs4_attr_u) * nfs4_ntov_map_size, 2123 KM_SLEEP); 2124 ntovp->attrcnt = 0; 2125 ntovp->vfsstat = FALSE; 2126 } 2127 2128 static void 2129 nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp, 2130 struct nfs4_svgetit_arg *sargp) 2131 { 2132 int i; 2133 union nfs4_attr_u *na; 2134 uint8_t *amap; 2135 2136 /* 2137 * XXX Should do the same checks for whether the bit is set 2138 */ 2139 for (i = 0, na = ntovp->na, amap = ntovp->amap; 2140 i < ntovp->attrcnt; i++, na++, amap++) { 2141 (void) (*nfs4_ntov_map[*amap].sv_getit)( 2142 NFS4ATTR_FREEIT, sargp, na); 2143 } 2144 if ((sargp->op == NFS4ATTR_SETIT) || (sargp->op == NFS4ATTR_VERIT)) { 2145 /* 2146 * xdr_free for getattr will be done later 2147 */ 2148 for (i = 0, na = ntovp->na, amap = ntovp->amap; 2149 i < ntovp->attrcnt; i++, na++, amap++) { 2150 xdr_free(nfs4_ntov_map[*amap].xfunc, (caddr_t)na); 2151 } 2152 } 2153 kmem_free(ntovp->na, sizeof (union nfs4_attr_u) * nfs4_ntov_map_size); 2154 } 2155 2156 /* 2157 * do_rfs4_op_getattr gets the system attrs and converts into fattr4. 2158 */ 2159 static nfsstat4 2160 do_rfs4_op_getattr(bitmap4 breq, fattr4 *fattrp, 2161 struct nfs4_svgetit_arg *sargp) 2162 { 2163 int error = 0; 2164 int i, k; 2165 struct nfs4_ntov_table ntov; 2166 XDR xdr; 2167 ulong_t xdr_size; 2168 char *xdr_attrs; 2169 nfsstat4 status = NFS4_OK; 2170 nfsstat4 prev_rdattr_error = sargp->rdattr_error; 2171 union nfs4_attr_u *na; 2172 uint8_t *amap; 2173 2174 sargp->op = NFS4ATTR_GETIT; 2175 sargp->flag = 0; 2176 2177 fattrp->attrmask = 0; 2178 /* if no bits requested, then return empty fattr4 */ 2179 if (breq == 0) { 2180 fattrp->attrlist4_len = 0; 2181 fattrp->attrlist4 = NULL; 2182 return (NFS4_OK); 2183 } 2184 2185 /* 2186 * return NFS4ERR_INVAL when client requests write-only attrs 2187 */ 2188 if (breq & (FATTR4_TIME_ACCESS_SET_MASK | FATTR4_TIME_MODIFY_SET_MASK)) 2189 return (NFS4ERR_INVAL); 2190 2191 nfs4_ntov_table_init(&ntov); 2192 na = ntov.na; 2193 amap = ntov.amap; 2194 2195 /* 2196 * Now loop to get or verify the attrs 2197 */ 2198 for (i = 0; i < nfs4_ntov_map_size; i++) { 2199 if (breq & nfs4_ntov_map[i].fbit) { 2200 if ((*nfs4_ntov_map[i].sv_getit)( 2201 NFS4ATTR_SUPPORTED, sargp, NULL) == 0) { 2202 2203 error = (*nfs4_ntov_map[i].sv_getit)( 2204 NFS4ATTR_GETIT, sargp, na); 2205 2206 /* 2207 * Possible error values: 2208 * >0 if sv_getit failed to 2209 * get the attr; 0 if succeeded; 2210 * <0 if rdattr_error and the 2211 * attribute cannot be returned. 2212 */ 2213 if (error && !(sargp->rdattr_error_req)) 2214 goto done; 2215 /* 2216 * If error then just for entry 2217 */ 2218 if (error == 0) { 2219 fattrp->attrmask |= 2220 nfs4_ntov_map[i].fbit; 2221 *amap++ = 2222 (uint8_t)nfs4_ntov_map[i].nval; 2223 na++; 2224 (ntov.attrcnt)++; 2225 } else if ((error > 0) && 2226 (sargp->rdattr_error == NFS4_OK)) { 2227 sargp->rdattr_error = puterrno4(error); 2228 } 2229 error = 0; 2230 } 2231 } 2232 } 2233 2234 /* 2235 * If rdattr_error was set after the return value for it was assigned, 2236 * update it. 2237 */ 2238 if (prev_rdattr_error != sargp->rdattr_error) { 2239 na = ntov.na; 2240 amap = ntov.amap; 2241 for (i = 0; i < ntov.attrcnt; i++, na++, amap++) { 2242 k = *amap; 2243 if (k < FATTR4_RDATTR_ERROR) { 2244 continue; 2245 } 2246 if ((k == FATTR4_RDATTR_ERROR) && 2247 ((*nfs4_ntov_map[k].sv_getit)( 2248 NFS4ATTR_SUPPORTED, sargp, NULL) == 0)) { 2249 2250 (void) (*nfs4_ntov_map[k].sv_getit)( 2251 NFS4ATTR_GETIT, sargp, na); 2252 } 2253 break; 2254 } 2255 } 2256 2257 xdr_size = 0; 2258 na = ntov.na; 2259 amap = ntov.amap; 2260 for (i = 0; i < ntov.attrcnt; i++, na++, amap++) { 2261 xdr_size += xdr_sizeof(nfs4_ntov_map[*amap].xfunc, na); 2262 } 2263 2264 fattrp->attrlist4_len = xdr_size; 2265 if (xdr_size) { 2266 /* freed by rfs4_op_getattr_free() */ 2267 fattrp->attrlist4 = xdr_attrs = kmem_zalloc(xdr_size, KM_SLEEP); 2268 2269 xdrmem_create(&xdr, xdr_attrs, xdr_size, XDR_ENCODE); 2270 2271 na = ntov.na; 2272 amap = ntov.amap; 2273 for (i = 0; i < ntov.attrcnt; i++, na++, amap++) { 2274 if (!(*nfs4_ntov_map[*amap].xfunc)(&xdr, na)) { 2275 DTRACE_PROBE1(nfss__e__getattr4_encfail, 2276 int, *amap); 2277 status = NFS4ERR_SERVERFAULT; 2278 break; 2279 } 2280 } 2281 /* xdrmem_destroy(&xdrs); */ /* NO-OP */ 2282 } else { 2283 fattrp->attrlist4 = NULL; 2284 } 2285 done: 2286 2287 nfs4_ntov_table_free(&ntov, sargp); 2288 2289 if (error != 0) 2290 status = puterrno4(error); 2291 2292 return (status); 2293 } 2294 2295 /* ARGSUSED */ 2296 static void 2297 rfs4_op_getattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 2298 struct compound_state *cs) 2299 { 2300 GETATTR4args *args = &argop->nfs_argop4_u.opgetattr; 2301 GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr; 2302 struct nfs4_svgetit_arg sarg; 2303 struct statvfs64 sb; 2304 nfsstat4 status; 2305 2306 DTRACE_NFSV4_2(op__getattr__start, struct compound_state *, cs, 2307 GETATTR4args *, args); 2308 2309 if (cs->vp == NULL) { 2310 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2311 goto out; 2312 } 2313 2314 if (cs->access == CS_ACCESS_DENIED) { 2315 *cs->statusp = resp->status = NFS4ERR_ACCESS; 2316 goto out; 2317 } 2318 2319 sarg.sbp = &sb; 2320 sarg.cs = cs; 2321 2322 status = bitmap4_to_attrmask(args->attr_request, &sarg); 2323 if (status == NFS4_OK) { 2324 status = bitmap4_get_sysattrs(&sarg); 2325 if (status == NFS4_OK) 2326 status = do_rfs4_op_getattr(args->attr_request, 2327 &resp->obj_attributes, &sarg); 2328 } 2329 *cs->statusp = resp->status = status; 2330 out: 2331 DTRACE_NFSV4_2(op__getattr__done, struct compound_state *, cs, 2332 GETATTR4res *, resp); 2333 } 2334 2335 static void 2336 rfs4_op_getattr_free(nfs_resop4 *resop) 2337 { 2338 GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr; 2339 2340 nfs4_fattr4_free(&resp->obj_attributes); 2341 } 2342 2343 /* ARGSUSED */ 2344 static void 2345 rfs4_op_getfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 2346 struct compound_state *cs) 2347 { 2348 GETFH4res *resp = &resop->nfs_resop4_u.opgetfh; 2349 2350 DTRACE_NFSV4_1(op__getfh__start, struct compound_state *, cs); 2351 2352 if (cs->vp == NULL) { 2353 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2354 goto out; 2355 } 2356 if (cs->access == CS_ACCESS_DENIED) { 2357 *cs->statusp = resp->status = NFS4ERR_ACCESS; 2358 goto out; 2359 } 2360 2361 resp->object.nfs_fh4_val = 2362 kmem_alloc(cs->fh.nfs_fh4_len, KM_SLEEP); 2363 nfs_fh4_copy(&cs->fh, &resp->object); 2364 *cs->statusp = resp->status = NFS4_OK; 2365 out: 2366 DTRACE_NFSV4_2(op__getfh__done, struct compound_state *, cs, 2367 GETFH4res *, resp); 2368 } 2369 2370 static void 2371 rfs4_op_getfh_free(nfs_resop4 *resop) 2372 { 2373 GETFH4res *resp = &resop->nfs_resop4_u.opgetfh; 2374 2375 if (resp->status == NFS4_OK && 2376 resp->object.nfs_fh4_val != NULL) { 2377 kmem_free(resp->object.nfs_fh4_val, resp->object.nfs_fh4_len); 2378 resp->object.nfs_fh4_val = NULL; 2379 resp->object.nfs_fh4_len = 0; 2380 } 2381 } 2382 2383 /* 2384 * illegal: args: void 2385 * res : status (NFS4ERR_OP_ILLEGAL) 2386 */ 2387 /* ARGSUSED */ 2388 static void 2389 rfs4_op_illegal(nfs_argop4 *argop, nfs_resop4 *resop, 2390 struct svc_req *req, struct compound_state *cs) 2391 { 2392 ILLEGAL4res *resp = &resop->nfs_resop4_u.opillegal; 2393 2394 resop->resop = OP_ILLEGAL; 2395 *cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL; 2396 } 2397 2398 /* 2399 * link: args: SAVED_FH: file, CURRENT_FH: target directory 2400 * res: status. If success - CURRENT_FH unchanged, return change_info 2401 */ 2402 /* ARGSUSED */ 2403 static void 2404 rfs4_op_link(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 2405 struct compound_state *cs) 2406 { 2407 LINK4args *args = &argop->nfs_argop4_u.oplink; 2408 LINK4res *resp = &resop->nfs_resop4_u.oplink; 2409 int error; 2410 vnode_t *vp; 2411 vnode_t *dvp; 2412 struct vattr bdva, idva, adva; 2413 char *nm; 2414 uint_t len; 2415 struct sockaddr *ca; 2416 char *name = NULL; 2417 2418 DTRACE_NFSV4_2(op__link__start, struct compound_state *, cs, 2419 LINK4args *, args); 2420 2421 /* SAVED_FH: source object */ 2422 vp = cs->saved_vp; 2423 if (vp == NULL) { 2424 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2425 goto out; 2426 } 2427 2428 /* CURRENT_FH: target directory */ 2429 dvp = cs->vp; 2430 if (dvp == NULL) { 2431 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2432 goto out; 2433 } 2434 2435 /* 2436 * If there is a non-shared filesystem mounted on this vnode, 2437 * do not allow to link any file in this directory. 2438 */ 2439 if (vn_ismntpt(dvp)) { 2440 *cs->statusp = resp->status = NFS4ERR_ACCESS; 2441 goto out; 2442 } 2443 2444 if (cs->access == CS_ACCESS_DENIED) { 2445 *cs->statusp = resp->status = NFS4ERR_ACCESS; 2446 goto out; 2447 } 2448 2449 /* Check source object's type validity */ 2450 if (vp->v_type == VDIR) { 2451 *cs->statusp = resp->status = NFS4ERR_ISDIR; 2452 goto out; 2453 } 2454 2455 /* Check target directory's type */ 2456 if (dvp->v_type != VDIR) { 2457 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 2458 goto out; 2459 } 2460 2461 if (cs->saved_exi != cs->exi) { 2462 *cs->statusp = resp->status = NFS4ERR_XDEV; 2463 goto out; 2464 } 2465 2466 if (!utf8_dir_verify(&args->newname)) { 2467 *cs->statusp = resp->status = NFS4ERR_INVAL; 2468 goto out; 2469 } 2470 2471 nm = utf8_to_fn(&args->newname, &len, NULL); 2472 if (nm == NULL) { 2473 *cs->statusp = resp->status = NFS4ERR_INVAL; 2474 goto out; 2475 } 2476 2477 if (len > MAXNAMELEN) { 2478 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 2479 kmem_free(nm, len); 2480 goto out; 2481 } 2482 2483 if (rdonly4(cs->exi, cs->vp, req)) { 2484 *cs->statusp = resp->status = NFS4ERR_ROFS; 2485 kmem_free(nm, len); 2486 goto out; 2487 } 2488 2489 /* Get "before" change value */ 2490 bdva.va_mask = AT_CTIME|AT_SEQ; 2491 error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL); 2492 if (error) { 2493 *cs->statusp = resp->status = puterrno4(error); 2494 kmem_free(nm, len); 2495 goto out; 2496 } 2497 2498 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 2499 name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND, 2500 MAXPATHLEN + 1); 2501 2502 if (name == NULL) { 2503 *cs->statusp = resp->status = NFS4ERR_INVAL; 2504 kmem_free(nm, len); 2505 goto out; 2506 } 2507 2508 NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime) 2509 2510 error = VOP_LINK(dvp, vp, name, cs->cr, NULL, 0); 2511 2512 if (nm != name) 2513 kmem_free(name, MAXPATHLEN + 1); 2514 kmem_free(nm, len); 2515 2516 /* 2517 * Get the initial "after" sequence number, if it fails, set to zero 2518 */ 2519 idva.va_mask = AT_SEQ; 2520 if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL)) 2521 idva.va_seq = 0; 2522 2523 /* 2524 * Force modified data and metadata out to stable storage. 2525 */ 2526 (void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL); 2527 (void) VOP_FSYNC(dvp, 0, cs->cr, NULL); 2528 2529 if (error) { 2530 *cs->statusp = resp->status = puterrno4(error); 2531 goto out; 2532 } 2533 2534 /* 2535 * Get "after" change value, if it fails, simply return the 2536 * before value. 2537 */ 2538 adva.va_mask = AT_CTIME|AT_SEQ; 2539 if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) { 2540 adva.va_ctime = bdva.va_ctime; 2541 adva.va_seq = 0; 2542 } 2543 2544 NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime) 2545 2546 /* 2547 * The cinfo.atomic = TRUE only if we have 2548 * non-zero va_seq's, and it has incremented by exactly one 2549 * during the VOP_LINK and it didn't change during the VOP_FSYNC. 2550 */ 2551 if (bdva.va_seq && idva.va_seq && adva.va_seq && 2552 idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq) 2553 resp->cinfo.atomic = TRUE; 2554 else 2555 resp->cinfo.atomic = FALSE; 2556 2557 *cs->statusp = resp->status = NFS4_OK; 2558 out: 2559 DTRACE_NFSV4_2(op__link__done, struct compound_state *, cs, 2560 LINK4res *, resp); 2561 } 2562 2563 /* 2564 * Used by rfs4_op_lookup and rfs4_op_lookupp to do the actual work. 2565 */ 2566 2567 /* ARGSUSED */ 2568 static nfsstat4 2569 do_rfs4_op_lookup(char *nm, uint_t buflen, struct svc_req *req, 2570 struct compound_state *cs) 2571 { 2572 int error; 2573 int different_export = 0; 2574 vnode_t *vp, *tvp, *pre_tvp = NULL, *oldvp = NULL; 2575 struct exportinfo *exi = NULL, *pre_exi = NULL; 2576 nfsstat4 stat; 2577 fid_t fid; 2578 int attrdir, dotdot, walk; 2579 bool_t is_newvp = FALSE; 2580 2581 if (cs->vp->v_flag & V_XATTRDIR) { 2582 attrdir = 1; 2583 ASSERT(get_fh4_flag(&cs->fh, FH4_ATTRDIR)); 2584 } else { 2585 attrdir = 0; 2586 ASSERT(! get_fh4_flag(&cs->fh, FH4_ATTRDIR)); 2587 } 2588 2589 dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0'); 2590 2591 /* 2592 * If dotdotting, then need to check whether it's 2593 * above the root of a filesystem, or above an 2594 * export point. 2595 */ 2596 if (dotdot) { 2597 2598 /* 2599 * If dotdotting at the root of a filesystem, then 2600 * need to traverse back to the mounted-on filesystem 2601 * and do the dotdot lookup there. 2602 */ 2603 if (cs->vp->v_flag & VROOT) { 2604 2605 /* 2606 * If at the system root, then can 2607 * go up no further. 2608 */ 2609 if (VN_CMP(cs->vp, rootdir)) 2610 return (puterrno4(ENOENT)); 2611 2612 /* 2613 * Traverse back to the mounted-on filesystem 2614 */ 2615 cs->vp = untraverse(cs->vp); 2616 2617 /* 2618 * Set the different_export flag so we remember 2619 * to pick up a new exportinfo entry for 2620 * this new filesystem. 2621 */ 2622 different_export = 1; 2623 } else { 2624 2625 /* 2626 * If dotdotting above an export point then set 2627 * the different_export to get new export info. 2628 */ 2629 different_export = nfs_exported(cs->exi, cs->vp); 2630 } 2631 } 2632 2633 error = VOP_LOOKUP(cs->vp, nm, &vp, NULL, 0, NULL, cs->cr, 2634 NULL, NULL, NULL); 2635 if (error) 2636 return (puterrno4(error)); 2637 2638 /* 2639 * If the vnode is in a pseudo filesystem, check whether it is visible. 2640 * 2641 * XXX if the vnode is a symlink and it is not visible in 2642 * a pseudo filesystem, return ENOENT (not following symlink). 2643 * V4 client can not mount such symlink. This is a regression 2644 * from V2/V3. 2645 * 2646 * In the same exported filesystem, if the security flavor used 2647 * is not an explicitly shared flavor, limit the view to the visible 2648 * list entries only. This is not a WRONGSEC case because it's already 2649 * checked via PUTROOTFH/PUTPUBFH or PUTFH. 2650 */ 2651 if (!different_export && 2652 (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) || 2653 cs->access & CS_ACCESS_LIMITED)) { 2654 if (! nfs_visible(cs->exi, vp, &different_export)) { 2655 VN_RELE(vp); 2656 return (puterrno4(ENOENT)); 2657 } 2658 } 2659 2660 /* 2661 * If it's a mountpoint, then traverse it. 2662 */ 2663 if (vn_ismntpt(vp)) { 2664 pre_exi = cs->exi; /* save pre-traversed exportinfo */ 2665 pre_tvp = vp; /* save pre-traversed vnode */ 2666 2667 /* 2668 * hold pre_tvp to counteract rele by traverse. We will 2669 * need pre_tvp below if checkexport4 fails 2670 */ 2671 VN_HOLD(pre_tvp); 2672 tvp = vp; 2673 if ((error = traverse(&tvp)) != 0) { 2674 VN_RELE(vp); 2675 VN_RELE(pre_tvp); 2676 return (puterrno4(error)); 2677 } 2678 vp = tvp; 2679 different_export = 1; 2680 } else if (vp->v_vfsp != cs->vp->v_vfsp) { 2681 /* 2682 * The vfsp comparison is to handle the case where 2683 * a LOFS mount is shared. lo_lookup traverses mount points, 2684 * and NFS is unaware of local fs transistions because 2685 * v_vfsmountedhere isn't set. For this special LOFS case, 2686 * the dir and the obj returned by lookup will have different 2687 * vfs ptrs. 2688 */ 2689 different_export = 1; 2690 } 2691 2692 if (different_export) { 2693 2694 bzero(&fid, sizeof (fid)); 2695 fid.fid_len = MAXFIDSZ; 2696 error = vop_fid_pseudo(vp, &fid); 2697 if (error) { 2698 VN_RELE(vp); 2699 if (pre_tvp) 2700 VN_RELE(pre_tvp); 2701 return (puterrno4(error)); 2702 } 2703 2704 if (dotdot) 2705 exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE); 2706 else 2707 exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp); 2708 2709 if (exi == NULL) { 2710 if (pre_tvp) { 2711 /* 2712 * If this vnode is a mounted-on vnode, 2713 * but the mounted-on file system is not 2714 * exported, send back the filehandle for 2715 * the mounted-on vnode, not the root of 2716 * the mounted-on file system. 2717 */ 2718 VN_RELE(vp); 2719 vp = pre_tvp; 2720 exi = pre_exi; 2721 } else { 2722 VN_RELE(vp); 2723 return (puterrno4(EACCES)); 2724 } 2725 } else if (pre_tvp) { 2726 /* we're done with pre_tvp now. release extra hold */ 2727 VN_RELE(pre_tvp); 2728 } 2729 2730 cs->exi = exi; 2731 2732 /* 2733 * Now we do a checkauth4. The reason is that 2734 * this client/user may not have access to the new 2735 * exported file system, and if he does, 2736 * the client/user may be mapped to a different uid. 2737 * 2738 * We start with a new cr, because the checkauth4 done 2739 * in the PUT*FH operation over wrote the cred's uid, 2740 * gid, etc, and we want the real thing before calling 2741 * checkauth4() 2742 */ 2743 crfree(cs->cr); 2744 cs->cr = crdup(cs->basecr); 2745 2746 if (cs->vp) 2747 oldvp = cs->vp; 2748 cs->vp = vp; 2749 is_newvp = TRUE; 2750 2751 stat = call_checkauth4(cs, req); 2752 if (stat != NFS4_OK) { 2753 VN_RELE(cs->vp); 2754 cs->vp = oldvp; 2755 return (stat); 2756 } 2757 } 2758 2759 /* 2760 * After various NFS checks, do a label check on the path 2761 * component. The label on this path should either be the 2762 * global zone's label or a zone's label. We are only 2763 * interested in the zone's label because exported files 2764 * in global zone is accessible (though read-only) to 2765 * clients. The exportability/visibility check is already 2766 * done before reaching this code. 2767 */ 2768 if (is_system_labeled()) { 2769 bslabel_t *clabel; 2770 2771 ASSERT(req->rq_label != NULL); 2772 clabel = req->rq_label; 2773 DTRACE_PROBE2(tx__rfs4__log__info__oplookup__clabel, char *, 2774 "got client label from request(1)", struct svc_req *, req); 2775 2776 if (!blequal(&l_admin_low->tsl_label, clabel)) { 2777 if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK, 2778 cs->exi)) { 2779 error = EACCES; 2780 goto err_out; 2781 } 2782 } else { 2783 /* 2784 * We grant access to admin_low label clients 2785 * only if the client is trusted, i.e. also 2786 * running Solaris Trusted Extension. 2787 */ 2788 struct sockaddr *ca; 2789 int addr_type; 2790 void *ipaddr; 2791 tsol_tpc_t *tp; 2792 2793 ca = (struct sockaddr *)svc_getrpccaller( 2794 req->rq_xprt)->buf; 2795 if (ca->sa_family == AF_INET) { 2796 addr_type = IPV4_VERSION; 2797 ipaddr = &((struct sockaddr_in *)ca)->sin_addr; 2798 } else if (ca->sa_family == AF_INET6) { 2799 addr_type = IPV6_VERSION; 2800 ipaddr = &((struct sockaddr_in6 *) 2801 ca)->sin6_addr; 2802 } 2803 tp = find_tpc(ipaddr, addr_type, B_FALSE); 2804 if (tp == NULL || tp->tpc_tp.tp_doi != 2805 l_admin_low->tsl_doi || tp->tpc_tp.host_type != 2806 SUN_CIPSO) { 2807 if (tp != NULL) 2808 TPC_RELE(tp); 2809 error = EACCES; 2810 goto err_out; 2811 } 2812 TPC_RELE(tp); 2813 } 2814 } 2815 2816 error = makefh4(&cs->fh, vp, cs->exi); 2817 2818 err_out: 2819 if (error) { 2820 if (is_newvp) { 2821 VN_RELE(cs->vp); 2822 cs->vp = oldvp; 2823 } else 2824 VN_RELE(vp); 2825 return (puterrno4(error)); 2826 } 2827 2828 if (!is_newvp) { 2829 if (cs->vp) 2830 VN_RELE(cs->vp); 2831 cs->vp = vp; 2832 } else if (oldvp) 2833 VN_RELE(oldvp); 2834 2835 /* 2836 * if did lookup on attrdir and didn't lookup .., set named 2837 * attr fh flag 2838 */ 2839 if (attrdir && ! dotdot) 2840 set_fh4_flag(&cs->fh, FH4_NAMEDATTR); 2841 2842 /* Assume false for now, open proc will set this */ 2843 cs->mandlock = FALSE; 2844 2845 return (NFS4_OK); 2846 } 2847 2848 /* ARGSUSED */ 2849 static void 2850 rfs4_op_lookup(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 2851 struct compound_state *cs) 2852 { 2853 LOOKUP4args *args = &argop->nfs_argop4_u.oplookup; 2854 LOOKUP4res *resp = &resop->nfs_resop4_u.oplookup; 2855 char *nm; 2856 uint_t len; 2857 struct sockaddr *ca; 2858 char *name = NULL; 2859 2860 DTRACE_NFSV4_2(op__lookup__start, struct compound_state *, cs, 2861 LOOKUP4args *, args); 2862 2863 if (cs->vp == NULL) { 2864 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2865 goto out; 2866 } 2867 2868 if (cs->vp->v_type == VLNK) { 2869 *cs->statusp = resp->status = NFS4ERR_SYMLINK; 2870 goto out; 2871 } 2872 2873 if (cs->vp->v_type != VDIR) { 2874 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 2875 goto out; 2876 } 2877 2878 if (!utf8_dir_verify(&args->objname)) { 2879 *cs->statusp = resp->status = NFS4ERR_INVAL; 2880 goto out; 2881 } 2882 2883 nm = utf8_to_str(&args->objname, &len, NULL); 2884 if (nm == NULL) { 2885 *cs->statusp = resp->status = NFS4ERR_INVAL; 2886 goto out; 2887 } 2888 2889 if (len > MAXNAMELEN) { 2890 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 2891 kmem_free(nm, len); 2892 goto out; 2893 } 2894 2895 /* If necessary, convert to UTF-8 for illbehaved clients */ 2896 2897 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 2898 name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND, 2899 MAXPATHLEN + 1); 2900 2901 if (name == NULL) { 2902 *cs->statusp = resp->status = NFS4ERR_INVAL; 2903 kmem_free(nm, len); 2904 goto out; 2905 } 2906 2907 *cs->statusp = resp->status = do_rfs4_op_lookup(name, len, req, cs); 2908 2909 if (name != nm) 2910 kmem_free(name, MAXPATHLEN + 1); 2911 kmem_free(nm, len); 2912 2913 out: 2914 DTRACE_NFSV4_2(op__lookup__done, struct compound_state *, cs, 2915 LOOKUP4res *, resp); 2916 } 2917 2918 /* ARGSUSED */ 2919 static void 2920 rfs4_op_lookupp(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req, 2921 struct compound_state *cs) 2922 { 2923 LOOKUPP4res *resp = &resop->nfs_resop4_u.oplookupp; 2924 2925 DTRACE_NFSV4_1(op__lookupp__start, struct compound_state *, cs); 2926 2927 if (cs->vp == NULL) { 2928 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2929 goto out; 2930 } 2931 2932 if (cs->vp->v_type != VDIR) { 2933 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 2934 goto out; 2935 } 2936 2937 *cs->statusp = resp->status = do_rfs4_op_lookup("..", 3, req, cs); 2938 2939 /* 2940 * From NFSV4 Specification, LOOKUPP should not check for 2941 * NFS4ERR_WRONGSEC. Retrun NFS4_OK instead. 2942 */ 2943 if (resp->status == NFS4ERR_WRONGSEC) { 2944 *cs->statusp = resp->status = NFS4_OK; 2945 } 2946 2947 out: 2948 DTRACE_NFSV4_2(op__lookupp__done, struct compound_state *, cs, 2949 LOOKUPP4res *, resp); 2950 } 2951 2952 2953 /*ARGSUSED2*/ 2954 static void 2955 rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 2956 struct compound_state *cs) 2957 { 2958 OPENATTR4args *args = &argop->nfs_argop4_u.opopenattr; 2959 OPENATTR4res *resp = &resop->nfs_resop4_u.opopenattr; 2960 vnode_t *avp = NULL; 2961 int lookup_flags = LOOKUP_XATTR, error; 2962 int exp_ro = 0; 2963 2964 DTRACE_NFSV4_2(op__openattr__start, struct compound_state *, cs, 2965 OPENATTR4args *, args); 2966 2967 if (cs->vp == NULL) { 2968 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 2969 goto out; 2970 } 2971 2972 if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0 && 2973 !vfs_has_feature(cs->vp->v_vfsp, VFSFT_SYSATTR_VIEWS)) { 2974 *cs->statusp = resp->status = puterrno4(ENOTSUP); 2975 goto out; 2976 } 2977 2978 /* 2979 * If file system supports passing ACE mask to VOP_ACCESS then 2980 * check for ACE_READ_NAMED_ATTRS, otherwise do legacy checks 2981 */ 2982 2983 if (vfs_has_feature(cs->vp->v_vfsp, VFSFT_ACEMASKONACCESS)) 2984 error = VOP_ACCESS(cs->vp, ACE_READ_NAMED_ATTRS, 2985 V_ACE_MASK, cs->cr, NULL); 2986 else 2987 error = ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr, NULL) != 0) && 2988 (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr, NULL) != 0) && 2989 (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr, NULL) != 0)); 2990 2991 if (error) { 2992 *cs->statusp = resp->status = puterrno4(EACCES); 2993 goto out; 2994 } 2995 2996 /* 2997 * The CREATE_XATTR_DIR VOP flag cannot be specified if 2998 * the file system is exported read-only -- regardless of 2999 * createdir flag. Otherwise the attrdir would be created 3000 * (assuming server fs isn't mounted readonly locally). If 3001 * VOP_LOOKUP returns ENOENT in this case, the error will 3002 * be translated into EROFS. ENOSYS is mapped to ENOTSUP 3003 * because specfs has no VOP_LOOKUP op, so the macro would 3004 * return ENOSYS. EINVAL is returned by all (current) 3005 * Solaris file system implementations when any of their 3006 * restrictions are violated (xattr(dir) can't have xattrdir). 3007 * Returning NOTSUPP is more appropriate in this case 3008 * because the object will never be able to have an attrdir. 3009 */ 3010 if (args->createdir && ! (exp_ro = rdonly4(cs->exi, cs->vp, req))) 3011 lookup_flags |= CREATE_XATTR_DIR; 3012 3013 error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr, 3014 NULL, NULL, NULL); 3015 3016 if (error) { 3017 if (error == ENOENT && args->createdir && exp_ro) 3018 *cs->statusp = resp->status = puterrno4(EROFS); 3019 else if (error == EINVAL || error == ENOSYS) 3020 *cs->statusp = resp->status = puterrno4(ENOTSUP); 3021 else 3022 *cs->statusp = resp->status = puterrno4(error); 3023 goto out; 3024 } 3025 3026 ASSERT(avp->v_flag & V_XATTRDIR); 3027 3028 error = makefh4(&cs->fh, avp, cs->exi); 3029 3030 if (error) { 3031 VN_RELE(avp); 3032 *cs->statusp = resp->status = puterrno4(error); 3033 goto out; 3034 } 3035 3036 VN_RELE(cs->vp); 3037 cs->vp = avp; 3038 3039 /* 3040 * There is no requirement for an attrdir fh flag 3041 * because the attrdir has a vnode flag to distinguish 3042 * it from regular (non-xattr) directories. The 3043 * FH4_ATTRDIR flag is set for future sanity checks. 3044 */ 3045 set_fh4_flag(&cs->fh, FH4_ATTRDIR); 3046 *cs->statusp = resp->status = NFS4_OK; 3047 3048 out: 3049 DTRACE_NFSV4_2(op__openattr__done, struct compound_state *, cs, 3050 OPENATTR4res *, resp); 3051 } 3052 3053 static int 3054 do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred, 3055 caller_context_t *ct) 3056 { 3057 int error; 3058 int i; 3059 clock_t delaytime; 3060 3061 delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay); 3062 3063 /* 3064 * Don't block on mandatory locks. If this routine returns 3065 * EAGAIN, the caller should return NFS4ERR_LOCKED. 3066 */ 3067 uio->uio_fmode = FNONBLOCK; 3068 3069 for (i = 0; i < rfs4_maxlock_tries; i++) { 3070 3071 3072 if (direction == FREAD) { 3073 (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, ct); 3074 error = VOP_READ(vp, uio, ioflag, cred, ct); 3075 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, ct); 3076 } else { 3077 (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, ct); 3078 error = VOP_WRITE(vp, uio, ioflag, cred, ct); 3079 VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, ct); 3080 } 3081 3082 if (error != EAGAIN) 3083 break; 3084 3085 if (i < rfs4_maxlock_tries - 1) { 3086 delay(delaytime); 3087 delaytime *= 2; 3088 } 3089 } 3090 3091 return (error); 3092 } 3093 3094 /* ARGSUSED */ 3095 static void 3096 rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 3097 struct compound_state *cs) 3098 { 3099 READ4args *args = &argop->nfs_argop4_u.opread; 3100 READ4res *resp = &resop->nfs_resop4_u.opread; 3101 int error; 3102 int verror; 3103 vnode_t *vp; 3104 struct vattr va; 3105 struct iovec iov; 3106 struct uio uio; 3107 u_offset_t offset; 3108 bool_t *deleg = &cs->deleg; 3109 nfsstat4 stat; 3110 int in_crit = 0; 3111 mblk_t *mp; 3112 int alloc_err = 0; 3113 caller_context_t ct; 3114 3115 DTRACE_NFSV4_2(op__read__start, struct compound_state *, cs, 3116 READ4args, args); 3117 3118 vp = cs->vp; 3119 if (vp == NULL) { 3120 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 3121 goto out; 3122 } 3123 if (cs->access == CS_ACCESS_DENIED) { 3124 *cs->statusp = resp->status = NFS4ERR_ACCESS; 3125 goto out; 3126 } 3127 3128 if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE, 3129 deleg, TRUE, &ct)) != NFS4_OK) { 3130 *cs->statusp = resp->status = stat; 3131 goto out; 3132 } 3133 3134 /* 3135 * Enter the critical region before calling VOP_RWLOCK 3136 * to avoid a deadlock with write requests. 3137 */ 3138 if (nbl_need_check(vp)) { 3139 nbl_start_crit(vp, RW_READER); 3140 in_crit = 1; 3141 if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0, 3142 &ct)) { 3143 *cs->statusp = resp->status = NFS4ERR_LOCKED; 3144 goto out; 3145 } 3146 } 3147 3148 if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE, 3149 deleg, TRUE, &ct)) != NFS4_OK) { 3150 *cs->statusp = resp->status = stat; 3151 goto out; 3152 } 3153 3154 va.va_mask = AT_MODE|AT_SIZE|AT_UID; 3155 verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct); 3156 3157 /* 3158 * If we can't get the attributes, then we can't do the 3159 * right access checking. So, we'll fail the request. 3160 */ 3161 if (verror) { 3162 *cs->statusp = resp->status = puterrno4(verror); 3163 goto out; 3164 } 3165 3166 if (vp->v_type != VREG) { 3167 *cs->statusp = resp->status = 3168 ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL); 3169 goto out; 3170 } 3171 3172 if (crgetuid(cs->cr) != va.va_uid && 3173 (error = VOP_ACCESS(vp, VREAD, 0, cs->cr, &ct)) && 3174 (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr, &ct))) { 3175 *cs->statusp = resp->status = puterrno4(error); 3176 goto out; 3177 } 3178 3179 if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */ 3180 *cs->statusp = resp->status = NFS4ERR_ACCESS; 3181 goto out; 3182 } 3183 3184 offset = args->offset; 3185 if (offset >= va.va_size) { 3186 *cs->statusp = resp->status = NFS4_OK; 3187 resp->eof = TRUE; 3188 resp->data_len = 0; 3189 resp->data_val = NULL; 3190 resp->mblk = NULL; 3191 /* RDMA */ 3192 resp->wlist = args->wlist; 3193 resp->wlist_len = resp->data_len; 3194 *cs->statusp = resp->status = NFS4_OK; 3195 if (resp->wlist) 3196 clist_zero_len(resp->wlist); 3197 goto out; 3198 } 3199 3200 if (args->count == 0) { 3201 *cs->statusp = resp->status = NFS4_OK; 3202 resp->eof = FALSE; 3203 resp->data_len = 0; 3204 resp->data_val = NULL; 3205 resp->mblk = NULL; 3206 /* RDMA */ 3207 resp->wlist = args->wlist; 3208 resp->wlist_len = resp->data_len; 3209 if (resp->wlist) 3210 clist_zero_len(resp->wlist); 3211 goto out; 3212 } 3213 3214 /* 3215 * Do not allocate memory more than maximum allowed 3216 * transfer size 3217 */ 3218 if (args->count > rfs4_tsize(req)) 3219 args->count = rfs4_tsize(req); 3220 3221 /* 3222 * If returning data via RDMA Write, then grab the chunk list. If we 3223 * aren't returning READ data w/RDMA_WRITE, then grab a mblk. 3224 */ 3225 if (args->wlist) { 3226 mp = NULL; 3227 (void) rdma_get_wchunk(req, &iov, args->wlist); 3228 } else { 3229 /* 3230 * mp will contain the data to be sent out in the read reply. 3231 * It will be freed after the reply has been sent. Let's 3232 * roundup the data to a BYTES_PER_XDR_UNIT multiple, so that 3233 * the call to xdrmblk_putmblk() never fails. If the first 3234 * alloc of the requested size fails, then decrease the size to 3235 * something more reasonable and wait for the allocation to 3236 * occur. 3237 */ 3238 mp = allocb(RNDUP(args->count), BPRI_MED); 3239 if (mp == NULL) { 3240 if (args->count > MAXBSIZE) 3241 args->count = MAXBSIZE; 3242 mp = allocb_wait(RNDUP(args->count), BPRI_MED, 3243 STR_NOSIG, &alloc_err); 3244 } 3245 ASSERT(mp != NULL); 3246 ASSERT(alloc_err == 0); 3247 3248 iov.iov_base = (caddr_t)mp->b_datap->db_base; 3249 iov.iov_len = args->count; 3250 } 3251 3252 uio.uio_iov = &iov; 3253 uio.uio_iovcnt = 1; 3254 uio.uio_segflg = UIO_SYSSPACE; 3255 uio.uio_extflg = UIO_COPY_CACHED; 3256 uio.uio_loffset = args->offset; 3257 uio.uio_resid = args->count; 3258 3259 error = do_io(FREAD, vp, &uio, 0, cs->cr, &ct); 3260 3261 va.va_mask = AT_SIZE; 3262 verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct); 3263 3264 if (error) { 3265 freeb(mp); 3266 *cs->statusp = resp->status = puterrno4(error); 3267 goto out; 3268 } 3269 3270 *cs->statusp = resp->status = NFS4_OK; 3271 3272 ASSERT(uio.uio_resid >= 0); 3273 resp->data_len = args->count - uio.uio_resid; 3274 if (mp) { 3275 resp->data_val = (char *)mp->b_datap->db_base; 3276 } else { 3277 resp->data_val = (caddr_t)iov.iov_base; 3278 } 3279 resp->mblk = mp; 3280 3281 if (!verror && offset + resp->data_len == va.va_size) 3282 resp->eof = TRUE; 3283 else 3284 resp->eof = FALSE; 3285 3286 if (args->wlist) { 3287 if (!rdma_setup_read_data4(args, resp)) { 3288 *cs->statusp = resp->status = NFS4ERR_INVAL; 3289 } 3290 } else { 3291 resp->wlist = NULL; 3292 } 3293 3294 out: 3295 if (in_crit) 3296 nbl_end_crit(vp); 3297 3298 DTRACE_NFSV4_2(op__read__done, struct compound_state *, cs, 3299 READ4res *, resp); 3300 } 3301 3302 static void 3303 rfs4_op_read_free(nfs_resop4 *resop) 3304 { 3305 READ4res *resp = &resop->nfs_resop4_u.opread; 3306 3307 if (resp->status == NFS4_OK && resp->mblk != NULL) { 3308 freeb(resp->mblk); 3309 resp->mblk = NULL; 3310 resp->data_val = NULL; 3311 resp->data_len = 0; 3312 } 3313 } 3314 3315 static void 3316 rfs4_op_readdir_free(nfs_resop4 * resop) 3317 { 3318 READDIR4res *resp = &resop->nfs_resop4_u.opreaddir; 3319 3320 if (resp->status == NFS4_OK && resp->mblk != NULL) { 3321 freeb(resp->mblk); 3322 resp->mblk = NULL; 3323 resp->data_len = 0; 3324 } 3325 } 3326 3327 3328 /* ARGSUSED */ 3329 static void 3330 rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req, 3331 struct compound_state *cs) 3332 { 3333 PUTPUBFH4res *resp = &resop->nfs_resop4_u.opputpubfh; 3334 int error; 3335 vnode_t *vp; 3336 struct exportinfo *exi, *sav_exi; 3337 nfs_fh4_fmt_t *fh_fmtp; 3338 3339 DTRACE_NFSV4_1(op__putpubfh__start, struct compound_state *, cs); 3340 3341 if (cs->vp) { 3342 VN_RELE(cs->vp); 3343 cs->vp = NULL; 3344 } 3345 3346 if (cs->cr) 3347 crfree(cs->cr); 3348 3349 cs->cr = crdup(cs->basecr); 3350 3351 vp = exi_public->exi_vp; 3352 if (vp == NULL) { 3353 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT; 3354 goto out; 3355 } 3356 3357 error = makefh4(&cs->fh, vp, exi_public); 3358 if (error != 0) { 3359 *cs->statusp = resp->status = puterrno4(error); 3360 goto out; 3361 } 3362 sav_exi = cs->exi; 3363 if (exi_public == exi_root) { 3364 /* 3365 * No filesystem is actually shared public, so we default 3366 * to exi_root. In this case, we must check whether root 3367 * is exported. 3368 */ 3369 fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val; 3370 3371 /* 3372 * if root filesystem is exported, the exportinfo struct that we 3373 * should use is what checkexport4 returns, because root_exi is 3374 * actually a mostly empty struct. 3375 */ 3376 exi = checkexport4(&fh_fmtp->fh4_fsid, 3377 (fid_t *)&fh_fmtp->fh4_xlen, NULL); 3378 cs->exi = ((exi != NULL) ? exi : exi_public); 3379 } else { 3380 /* 3381 * it's a properly shared filesystem 3382 */ 3383 cs->exi = exi_public; 3384 } 3385 3386 if (is_system_labeled()) { 3387 bslabel_t *clabel; 3388 3389 ASSERT(req->rq_label != NULL); 3390 clabel = req->rq_label; 3391 DTRACE_PROBE2(tx__rfs4__log__info__opputpubfh__clabel, char *, 3392 "got client label from request(1)", 3393 struct svc_req *, req); 3394 if (!blequal(&l_admin_low->tsl_label, clabel)) { 3395 if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK, 3396 cs->exi)) { 3397 *cs->statusp = resp->status = 3398 NFS4ERR_SERVERFAULT; 3399 goto out; 3400 } 3401 } 3402 } 3403 3404 VN_HOLD(vp); 3405 cs->vp = vp; 3406 3407 if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) { 3408 VN_RELE(cs->vp); 3409 cs->vp = NULL; 3410 cs->exi = sav_exi; 3411 goto out; 3412 } 3413 3414 *cs->statusp = resp->status = NFS4_OK; 3415 out: 3416 DTRACE_NFSV4_2(op__putpubfh__done, struct compound_state *, cs, 3417 PUTPUBFH4res *, resp); 3418 } 3419 3420 /* 3421 * XXX - issue with put*fh operations. Suppose /export/home is exported. 3422 * Suppose an NFS client goes to mount /export/home/joe. If /export, home, 3423 * or joe have restrictive search permissions, then we shouldn't let 3424 * the client get a file handle. This is easy to enforce. However, we 3425 * don't know what security flavor should be used until we resolve the 3426 * path name. Another complication is uid mapping. If root is 3427 * the user, then it will be mapped to the anonymous user by default, 3428 * but we won't know that till we've resolved the path name. And we won't 3429 * know what the anonymous user is. 3430 * Luckily, SECINFO is specified to take a full filename. 3431 * So what we will have to in rfs4_op_lookup is check that flavor of 3432 * the target object matches that of the request, and if root was the 3433 * caller, check for the root= and anon= options, and if necessary, 3434 * repeat the lookup using the right cred_t. But that's not done yet. 3435 */ 3436 /* ARGSUSED */ 3437 static void 3438 rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 3439 struct compound_state *cs) 3440 { 3441 PUTFH4args *args = &argop->nfs_argop4_u.opputfh; 3442 PUTFH4res *resp = &resop->nfs_resop4_u.opputfh; 3443 nfs_fh4_fmt_t *fh_fmtp; 3444 3445 DTRACE_NFSV4_2(op__putfh__start, struct compound_state *, cs, 3446 PUTFH4args *, args); 3447 3448 if (cs->vp) { 3449 VN_RELE(cs->vp); 3450 cs->vp = NULL; 3451 } 3452 3453 if (cs->cr) { 3454 crfree(cs->cr); 3455 cs->cr = NULL; 3456 } 3457 3458 3459 if (args->object.nfs_fh4_len < NFS_FH4_LEN) { 3460 *cs->statusp = resp->status = NFS4ERR_BADHANDLE; 3461 goto out; 3462 } 3463 3464 fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val; 3465 cs->exi = checkexport4(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen, 3466 NULL); 3467 3468 if (cs->exi == NULL) { 3469 *cs->statusp = resp->status = NFS4ERR_STALE; 3470 goto out; 3471 } 3472 3473 cs->cr = crdup(cs->basecr); 3474 3475 ASSERT(cs->cr != NULL); 3476 3477 if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) { 3478 *cs->statusp = resp->status; 3479 goto out; 3480 } 3481 3482 if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) { 3483 VN_RELE(cs->vp); 3484 cs->vp = NULL; 3485 goto out; 3486 } 3487 3488 nfs_fh4_copy(&args->object, &cs->fh); 3489 *cs->statusp = resp->status = NFS4_OK; 3490 cs->deleg = FALSE; 3491 3492 out: 3493 DTRACE_NFSV4_2(op__putfh__done, struct compound_state *, cs, 3494 PUTFH4res *, resp); 3495 } 3496 3497 /* ARGSUSED */ 3498 static void 3499 rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 3500 struct compound_state *cs) 3501 3502 { 3503 PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh; 3504 int error; 3505 fid_t fid; 3506 struct exportinfo *exi, *sav_exi; 3507 3508 DTRACE_NFSV4_1(op__putrootfh__start, struct compound_state *, cs); 3509 3510 if (cs->vp) { 3511 VN_RELE(cs->vp); 3512 cs->vp = NULL; 3513 } 3514 3515 if (cs->cr) 3516 crfree(cs->cr); 3517 3518 cs->cr = crdup(cs->basecr); 3519 3520 /* 3521 * Using rootdir, the system root vnode, 3522 * get its fid. 3523 */ 3524 bzero(&fid, sizeof (fid)); 3525 fid.fid_len = MAXFIDSZ; 3526 error = vop_fid_pseudo(rootdir, &fid); 3527 if (error != 0) { 3528 *cs->statusp = resp->status = puterrno4(error); 3529 goto out; 3530 } 3531 3532 /* 3533 * Then use the root fsid & fid it to find out if it's exported 3534 * 3535 * If the server root isn't exported directly, then 3536 * it should at least be a pseudo export based on 3537 * one or more exports further down in the server's 3538 * file tree. 3539 */ 3540 exi = checkexport4(&rootdir->v_vfsp->vfs_fsid, &fid, NULL); 3541 if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) { 3542 NFS4_DEBUG(rfs4_debug, 3543 (CE_WARN, "rfs4_op_putrootfh: export check failure")); 3544 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT; 3545 goto out; 3546 } 3547 3548 /* 3549 * Now make a filehandle based on the root 3550 * export and root vnode. 3551 */ 3552 error = makefh4(&cs->fh, rootdir, exi); 3553 if (error != 0) { 3554 *cs->statusp = resp->status = puterrno4(error); 3555 goto out; 3556 } 3557 3558 sav_exi = cs->exi; 3559 cs->exi = exi; 3560 3561 VN_HOLD(rootdir); 3562 cs->vp = rootdir; 3563 3564 if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) { 3565 VN_RELE(rootdir); 3566 cs->vp = NULL; 3567 cs->exi = sav_exi; 3568 goto out; 3569 } 3570 3571 *cs->statusp = resp->status = NFS4_OK; 3572 cs->deleg = FALSE; 3573 out: 3574 DTRACE_NFSV4_2(op__putrootfh__done, struct compound_state *, cs, 3575 PUTROOTFH4res *, resp); 3576 } 3577 3578 /* 3579 * A directory entry is a valid nfsv4 entry if 3580 * - it has a non-zero ino 3581 * - it is not a dot or dotdot name 3582 * - it is visible in a pseudo export or in a real export that can 3583 * only have a limited view. 3584 */ 3585 static bool_t 3586 valid_nfs4_entry(struct exportinfo *exi, struct dirent64 *dp, 3587 int *expseudo, int check_visible) 3588 { 3589 if (dp->d_ino == 0 || NFS_IS_DOTNAME(dp->d_name)) { 3590 *expseudo = 0; 3591 return (FALSE); 3592 } 3593 3594 if (! check_visible) { 3595 *expseudo = 0; 3596 return (TRUE); 3597 } 3598 3599 return (nfs_visible_inode(exi, dp->d_ino, expseudo)); 3600 } 3601 3602 /* 3603 * set_rdattr_params sets up the variables used to manage what information 3604 * to get for each directory entry. 3605 */ 3606 static nfsstat4 3607 set_rdattr_params(struct nfs4_svgetit_arg *sargp, 3608 bitmap4 attrs, bool_t *need_to_lookup) 3609 { 3610 uint_t va_mask; 3611 nfsstat4 status; 3612 bitmap4 objbits; 3613 3614 status = bitmap4_to_attrmask(attrs, sargp); 3615 if (status != NFS4_OK) { 3616 /* 3617 * could not even figure attr mask 3618 */ 3619 return (status); 3620 } 3621 va_mask = sargp->vap->va_mask; 3622 3623 /* 3624 * dirent's d_ino is always correct value for mounted_on_fileid. 3625 * mntdfid_set is set once here, but mounted_on_fileid is 3626 * set in main dirent processing loop for each dirent. 3627 * The mntdfid_set is a simple optimization that lets the 3628 * server attr code avoid work when caller is readdir. 3629 */ 3630 sargp->mntdfid_set = TRUE; 3631 3632 /* 3633 * Lookup entry only if client asked for any of the following: 3634 * a) vattr attrs 3635 * b) vfs attrs 3636 * c) attrs w/per-object scope requested (change, filehandle, etc) 3637 * other than mounted_on_fileid (which we can take from dirent) 3638 */ 3639 objbits = attrs ? attrs & NFS4_VP_ATTR_MASK : 0; 3640 3641 if (va_mask || sargp->sbp || (objbits & ~FATTR4_MOUNTED_ON_FILEID_MASK)) 3642 *need_to_lookup = TRUE; 3643 else 3644 *need_to_lookup = FALSE; 3645 3646 if (sargp->sbp == NULL) 3647 return (NFS4_OK); 3648 3649 /* 3650 * If filesystem attrs are requested, get them now from the 3651 * directory vp, as most entries will have same filesystem. The only 3652 * exception are mounted over entries but we handle 3653 * those as we go (XXX mounted over detection not yet implemented). 3654 */ 3655 sargp->vap->va_mask = 0; /* to avoid VOP_GETATTR */ 3656 status = bitmap4_get_sysattrs(sargp); 3657 sargp->vap->va_mask = va_mask; 3658 3659 if ((status != NFS4_OK) && sargp->rdattr_error_req) { 3660 /* 3661 * Failed to get filesystem attributes. 3662 * Return a rdattr_error for each entry, but don't fail. 3663 * However, don't get any obj-dependent attrs. 3664 */ 3665 sargp->rdattr_error = status; /* for rdattr_error */ 3666 *need_to_lookup = FALSE; 3667 /* 3668 * At least get fileid for regular readdir output 3669 */ 3670 sargp->vap->va_mask &= AT_NODEID; 3671 status = NFS4_OK; 3672 } 3673 3674 return (status); 3675 } 3676 3677 /* 3678 * readlink: args: CURRENT_FH. 3679 * res: status. If success - CURRENT_FH unchanged, return linktext. 3680 */ 3681 3682 /* ARGSUSED */ 3683 static void 3684 rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 3685 struct compound_state *cs) 3686 { 3687 READLINK4res *resp = &resop->nfs_resop4_u.opreadlink; 3688 int error; 3689 vnode_t *vp; 3690 struct iovec iov; 3691 struct vattr va; 3692 struct uio uio; 3693 char *data; 3694 struct sockaddr *ca; 3695 char *name = NULL; 3696 3697 DTRACE_NFSV4_1(op__readlink__start, struct compound_state *, cs); 3698 3699 /* CURRENT_FH: directory */ 3700 vp = cs->vp; 3701 if (vp == NULL) { 3702 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 3703 goto out; 3704 } 3705 3706 if (cs->access == CS_ACCESS_DENIED) { 3707 *cs->statusp = resp->status = NFS4ERR_ACCESS; 3708 goto out; 3709 } 3710 3711 if (vp->v_type == VDIR) { 3712 *cs->statusp = resp->status = NFS4ERR_ISDIR; 3713 goto out; 3714 } 3715 3716 if (vp->v_type != VLNK) { 3717 *cs->statusp = resp->status = NFS4ERR_INVAL; 3718 goto out; 3719 } 3720 3721 va.va_mask = AT_MODE; 3722 error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL); 3723 if (error) { 3724 *cs->statusp = resp->status = puterrno4(error); 3725 goto out; 3726 } 3727 3728 if (MANDLOCK(vp, va.va_mode)) { 3729 *cs->statusp = resp->status = NFS4ERR_ACCESS; 3730 goto out; 3731 } 3732 3733 data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP); 3734 3735 iov.iov_base = data; 3736 iov.iov_len = MAXPATHLEN; 3737 uio.uio_iov = &iov; 3738 uio.uio_iovcnt = 1; 3739 uio.uio_segflg = UIO_SYSSPACE; 3740 uio.uio_extflg = UIO_COPY_CACHED; 3741 uio.uio_loffset = 0; 3742 uio.uio_resid = MAXPATHLEN; 3743 3744 error = VOP_READLINK(vp, &uio, cs->cr, NULL); 3745 3746 if (error) { 3747 kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1); 3748 *cs->statusp = resp->status = puterrno4(error); 3749 goto out; 3750 } 3751 3752 *(data + MAXPATHLEN - uio.uio_resid) = '\0'; 3753 3754 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 3755 name = nfscmd_convname(ca, cs->exi, data, NFSCMD_CONV_OUTBOUND, 3756 MAXPATHLEN + 1); 3757 3758 if (name == NULL) { 3759 /* 3760 * Even though the conversion failed, we return 3761 * something. We just don't translate it. 3762 */ 3763 name = data; 3764 } 3765 3766 /* 3767 * treat link name as data 3768 */ 3769 (void) str_to_utf8(name, &resp->link); 3770 3771 if (name != data) 3772 kmem_free(name, MAXPATHLEN + 1); 3773 kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1); 3774 *cs->statusp = resp->status = NFS4_OK; 3775 3776 out: 3777 DTRACE_NFSV4_2(op__readlink__done, struct compound_state *, cs, 3778 READLINK4res *, resp); 3779 } 3780 3781 static void 3782 rfs4_op_readlink_free(nfs_resop4 *resop) 3783 { 3784 READLINK4res *resp = &resop->nfs_resop4_u.opreadlink; 3785 utf8string *symlink = &resp->link; 3786 3787 if (symlink->utf8string_val) { 3788 UTF8STRING_FREE(*symlink) 3789 } 3790 } 3791 3792 /* 3793 * release_lockowner: 3794 * Release any state associated with the supplied 3795 * lockowner. Note if any lo_state is holding locks we will not 3796 * rele that lo_state and thus the lockowner will not be destroyed. 3797 * A client using lock after the lock owner stateid has been released 3798 * will suffer the consequence of NFS4ERR_BAD_STATEID and would have 3799 * to reissue the lock with new_lock_owner set to TRUE. 3800 * args: lock_owner 3801 * res: status 3802 */ 3803 /* ARGSUSED */ 3804 static void 3805 rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop, 3806 struct svc_req *req, struct compound_state *cs) 3807 { 3808 RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner; 3809 RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner; 3810 rfs4_lockowner_t *lo; 3811 rfs4_openowner_t *oop; 3812 rfs4_state_t *sp; 3813 rfs4_lo_state_t *lsp; 3814 rfs4_client_t *cp; 3815 bool_t create = FALSE; 3816 locklist_t *llist; 3817 sysid_t sysid; 3818 3819 DTRACE_NFSV4_2(op__release__lockowner__start, struct compound_state *, 3820 cs, RELEASE_LOCKOWNER4args *, ap); 3821 3822 /* Make sure there is a clientid around for this request */ 3823 cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE); 3824 3825 if (cp == NULL) { 3826 *cs->statusp = resp->status = 3827 rfs4_check_clientid(&ap->lock_owner.clientid, 0); 3828 goto out; 3829 } 3830 rfs4_client_rele(cp); 3831 3832 lo = rfs4_findlockowner(&ap->lock_owner, &create); 3833 if (lo == NULL) { 3834 *cs->statusp = resp->status = NFS4_OK; 3835 goto out; 3836 } 3837 ASSERT(lo->client != NULL); 3838 3839 /* 3840 * Check for EXPIRED client. If so will reap state with in a lease 3841 * period or on next set_clientid_confirm step 3842 */ 3843 if (rfs4_lease_expired(lo->client)) { 3844 rfs4_lockowner_rele(lo); 3845 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 3846 goto out; 3847 } 3848 3849 /* 3850 * If no sysid has been assigned, then no locks exist; just return. 3851 */ 3852 rfs4_dbe_lock(lo->client->dbe); 3853 if (lo->client->sysidt == LM_NOSYSID) { 3854 rfs4_lockowner_rele(lo); 3855 rfs4_dbe_unlock(lo->client->dbe); 3856 goto out; 3857 } 3858 3859 sysid = lo->client->sysidt; 3860 rfs4_dbe_unlock(lo->client->dbe); 3861 3862 /* 3863 * Mark the lockowner invalid. 3864 */ 3865 rfs4_dbe_hide(lo->dbe); 3866 3867 /* 3868 * sysid-pid pair should now not be used since the lockowner is 3869 * invalid. If the client were to instantiate the lockowner again 3870 * it would be assigned a new pid. Thus we can get the list of 3871 * current locks. 3872 */ 3873 3874 llist = flk_get_active_locks(sysid, lo->pid); 3875 /* If we are still holding locks fail */ 3876 if (llist != NULL) { 3877 3878 *cs->statusp = resp->status = NFS4ERR_LOCKS_HELD; 3879 3880 flk_free_locklist(llist); 3881 /* 3882 * We need to unhide the lockowner so the client can 3883 * try it again. The bad thing here is if the client 3884 * has a logic error that took it here in the first place 3885 * he probably has lost accounting of the locks that it 3886 * is holding. So we may have dangling state until the 3887 * open owner state is reaped via close. One scenario 3888 * that could possibly occur is that the client has 3889 * sent the unlock request(s) in separate threads 3890 * and has not waited for the replies before sending the 3891 * RELEASE_LOCKOWNER request. Presumably, it would expect 3892 * and deal appropriately with NFS4ERR_LOCKS_HELD, by 3893 * reissuing the request. 3894 */ 3895 rfs4_dbe_unhide(lo->dbe); 3896 rfs4_lockowner_rele(lo); 3897 goto out; 3898 } 3899 3900 /* 3901 * For the corresponding client we need to check each open 3902 * owner for any opens that have lockowner state associated 3903 * with this lockowner. 3904 */ 3905 3906 rfs4_dbe_lock(lo->client->dbe); 3907 for (oop = lo->client->openownerlist.next->oop; oop != NULL; 3908 oop = oop->openownerlist.next->oop) { 3909 3910 rfs4_dbe_lock(oop->dbe); 3911 for (sp = oop->ownerstateids.next->sp; sp != NULL; 3912 sp = sp->ownerstateids.next->sp) { 3913 3914 rfs4_dbe_lock(sp->dbe); 3915 for (lsp = sp->lockownerlist.next->lsp; 3916 lsp != NULL; lsp = lsp->lockownerlist.next->lsp) { 3917 if (lsp->locker == lo) { 3918 rfs4_dbe_lock(lsp->dbe); 3919 rfs4_dbe_invalidate(lsp->dbe); 3920 rfs4_dbe_unlock(lsp->dbe); 3921 } 3922 } 3923 rfs4_dbe_unlock(sp->dbe); 3924 } 3925 rfs4_dbe_unlock(oop->dbe); 3926 } 3927 rfs4_dbe_unlock(lo->client->dbe); 3928 3929 rfs4_lockowner_rele(lo); 3930 3931 *cs->statusp = resp->status = NFS4_OK; 3932 3933 out: 3934 DTRACE_NFSV4_2(op__release__lockowner__done, struct compound_state *, 3935 cs, RELEASE_LOCKOWNER4res *, resp); 3936 } 3937 3938 /* 3939 * short utility function to lookup a file and recall the delegation 3940 */ 3941 static rfs4_file_t * 3942 rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp, 3943 int *lkup_error, cred_t *cr) 3944 { 3945 vnode_t *vp; 3946 rfs4_file_t *fp = NULL; 3947 bool_t fcreate = FALSE; 3948 int error; 3949 3950 if (vpp) 3951 *vpp = NULL; 3952 3953 if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, NULL, NULL, 3954 NULL)) == 0) { 3955 if (vp->v_type == VREG) 3956 fp = rfs4_findfile(vp, NULL, &fcreate); 3957 if (vpp) 3958 *vpp = vp; 3959 else 3960 VN_RELE(vp); 3961 } 3962 3963 if (lkup_error) 3964 *lkup_error = error; 3965 3966 return (fp); 3967 } 3968 3969 /* 3970 * remove: args: CURRENT_FH: directory; name. 3971 * res: status. If success - CURRENT_FH unchanged, return change_info 3972 * for directory. 3973 */ 3974 /* ARGSUSED */ 3975 static void 3976 rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 3977 struct compound_state *cs) 3978 { 3979 REMOVE4args *args = &argop->nfs_argop4_u.opremove; 3980 REMOVE4res *resp = &resop->nfs_resop4_u.opremove; 3981 int error; 3982 vnode_t *dvp, *vp; 3983 struct vattr bdva, idva, adva; 3984 char *nm; 3985 uint_t len; 3986 rfs4_file_t *fp; 3987 int in_crit = 0; 3988 bslabel_t *clabel; 3989 struct sockaddr *ca; 3990 char *name = NULL; 3991 3992 DTRACE_NFSV4_2(op__remove__start, struct compound_state *, cs, 3993 REMOVE4args *, args); 3994 3995 /* CURRENT_FH: directory */ 3996 dvp = cs->vp; 3997 if (dvp == NULL) { 3998 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 3999 goto out; 4000 } 4001 4002 if (cs->access == CS_ACCESS_DENIED) { 4003 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4004 goto out; 4005 } 4006 4007 /* 4008 * If there is an unshared filesystem mounted on this vnode, 4009 * Do not allow to remove anything in this directory. 4010 */ 4011 if (vn_ismntpt(dvp)) { 4012 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4013 goto out; 4014 } 4015 4016 if (dvp->v_type != VDIR) { 4017 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 4018 goto out; 4019 } 4020 4021 if (!utf8_dir_verify(&args->target)) { 4022 *cs->statusp = resp->status = NFS4ERR_INVAL; 4023 goto out; 4024 } 4025 4026 /* 4027 * Lookup the file so that we can check if it's a directory 4028 */ 4029 nm = utf8_to_fn(&args->target, &len, NULL); 4030 if (nm == NULL) { 4031 *cs->statusp = resp->status = NFS4ERR_INVAL; 4032 goto out; 4033 } 4034 4035 if (len > MAXNAMELEN) { 4036 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 4037 kmem_free(nm, len); 4038 goto out; 4039 } 4040 4041 if (rdonly4(cs->exi, cs->vp, req)) { 4042 *cs->statusp = resp->status = NFS4ERR_ROFS; 4043 kmem_free(nm, len); 4044 goto out; 4045 } 4046 4047 /* If necessary, convert to UTF-8 for illbehaved clients */ 4048 4049 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 4050 name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND, 4051 MAXPATHLEN + 1); 4052 4053 if (name == NULL) { 4054 *cs->statusp = resp->status = NFS4ERR_INVAL; 4055 kmem_free(nm, len); 4056 goto out; 4057 } 4058 4059 /* 4060 * Lookup the file to determine type and while we are see if 4061 * there is a file struct around and check for delegation. 4062 * We don't need to acquire va_seq before this lookup, if 4063 * it causes an update, cinfo.before will not match, which will 4064 * trigger a cache flush even if atomic is TRUE. 4065 */ 4066 if (fp = rfs4_lookup_and_findfile(dvp, name, &vp, &error, cs->cr)) { 4067 if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE, 4068 NULL)) { 4069 VN_RELE(vp); 4070 rfs4_file_rele(fp); 4071 *cs->statusp = resp->status = NFS4ERR_DELAY; 4072 if (nm != name) 4073 kmem_free(name, MAXPATHLEN + 1); 4074 kmem_free(nm, len); 4075 goto out; 4076 } 4077 } 4078 4079 /* Didn't find anything to remove */ 4080 if (vp == NULL) { 4081 *cs->statusp = resp->status = error; 4082 if (nm != name) 4083 kmem_free(name, MAXPATHLEN + 1); 4084 kmem_free(nm, len); 4085 goto out; 4086 } 4087 4088 if (nbl_need_check(vp)) { 4089 nbl_start_crit(vp, RW_READER); 4090 in_crit = 1; 4091 if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) { 4092 *cs->statusp = resp->status = NFS4ERR_FILE_OPEN; 4093 if (nm != name) 4094 kmem_free(name, MAXPATHLEN + 1); 4095 kmem_free(nm, len); 4096 nbl_end_crit(vp); 4097 VN_RELE(vp); 4098 if (fp) { 4099 rfs4_clear_dont_grant(fp); 4100 rfs4_file_rele(fp); 4101 } 4102 goto out; 4103 } 4104 } 4105 4106 /* check label before allowing removal */ 4107 if (is_system_labeled()) { 4108 ASSERT(req->rq_label != NULL); 4109 clabel = req->rq_label; 4110 DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *, 4111 "got client label from request(1)", 4112 struct svc_req *, req); 4113 if (!blequal(&l_admin_low->tsl_label, clabel)) { 4114 if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK, 4115 cs->exi)) { 4116 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4117 if (name != nm) 4118 kmem_free(name, MAXPATHLEN + 1); 4119 kmem_free(nm, len); 4120 if (in_crit) 4121 nbl_end_crit(vp); 4122 VN_RELE(vp); 4123 if (fp) { 4124 rfs4_clear_dont_grant(fp); 4125 rfs4_file_rele(fp); 4126 } 4127 goto out; 4128 } 4129 } 4130 } 4131 4132 /* Get dir "before" change value */ 4133 bdva.va_mask = AT_CTIME|AT_SEQ; 4134 error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL); 4135 if (error) { 4136 *cs->statusp = resp->status = puterrno4(error); 4137 if (nm != name) 4138 kmem_free(name, MAXPATHLEN + 1); 4139 kmem_free(nm, len); 4140 if (in_crit) 4141 nbl_end_crit(vp); 4142 VN_RELE(vp); 4143 if (fp) { 4144 rfs4_clear_dont_grant(fp); 4145 rfs4_file_rele(fp); 4146 } 4147 goto out; 4148 } 4149 NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime) 4150 4151 /* Actually do the REMOVE operation */ 4152 if (vp->v_type == VDIR) { 4153 /* 4154 * Can't remove a directory that has a mounted-on filesystem. 4155 */ 4156 if (vn_ismntpt(vp)) { 4157 error = EACCES; 4158 } else { 4159 /* 4160 * System V defines rmdir to return EEXIST, 4161 * not * ENOTEMPTY, if the directory is not 4162 * empty. A System V NFS server needs to map 4163 * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to 4164 * transmit over the wire. 4165 */ 4166 if ((error = VOP_RMDIR(dvp, nm, rootdir, cs->cr, 4167 NULL, 0)) == EEXIST) 4168 error = ENOTEMPTY; 4169 } 4170 } else { 4171 if ((error = VOP_REMOVE(dvp, name, cs->cr, NULL, 0)) == 0 && 4172 fp != NULL) { 4173 struct vattr va; 4174 vnode_t *tvp; 4175 4176 rfs4_dbe_lock(fp->dbe); 4177 tvp = fp->vp; 4178 if (tvp) 4179 VN_HOLD(tvp); 4180 rfs4_dbe_unlock(fp->dbe); 4181 4182 if (tvp) { 4183 /* 4184 * This is va_seq safe because we are not 4185 * manipulating dvp. 4186 */ 4187 va.va_mask = AT_NLINK; 4188 if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) && 4189 va.va_nlink == 0) { 4190 /* Remove state on file remove */ 4191 if (in_crit) { 4192 nbl_end_crit(vp); 4193 in_crit = 0; 4194 } 4195 rfs4_close_all_state(fp); 4196 } 4197 VN_RELE(tvp); 4198 } 4199 } 4200 } 4201 4202 if (in_crit) 4203 nbl_end_crit(vp); 4204 VN_RELE(vp); 4205 4206 if (fp) { 4207 rfs4_clear_dont_grant(fp); 4208 rfs4_file_rele(fp); 4209 } 4210 if (nm != name) 4211 kmem_free(name, MAXPATHLEN + 1); 4212 kmem_free(nm, len); 4213 4214 if (error) { 4215 *cs->statusp = resp->status = puterrno4(error); 4216 goto out; 4217 } 4218 4219 /* 4220 * Get the initial "after" sequence number, if it fails, set to zero 4221 */ 4222 idva.va_mask = AT_SEQ; 4223 if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL)) 4224 idva.va_seq = 0; 4225 4226 /* 4227 * Force modified data and metadata out to stable storage. 4228 */ 4229 (void) VOP_FSYNC(dvp, 0, cs->cr, NULL); 4230 4231 /* 4232 * Get "after" change value, if it fails, simply return the 4233 * before value. 4234 */ 4235 adva.va_mask = AT_CTIME|AT_SEQ; 4236 if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) { 4237 adva.va_ctime = bdva.va_ctime; 4238 adva.va_seq = 0; 4239 } 4240 4241 NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime) 4242 4243 /* 4244 * The cinfo.atomic = TRUE only if we have 4245 * non-zero va_seq's, and it has incremented by exactly one 4246 * during the VOP_REMOVE/RMDIR and it didn't change during 4247 * the VOP_FSYNC. 4248 */ 4249 if (bdva.va_seq && idva.va_seq && adva.va_seq && 4250 idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq) 4251 resp->cinfo.atomic = TRUE; 4252 else 4253 resp->cinfo.atomic = FALSE; 4254 4255 *cs->statusp = resp->status = NFS4_OK; 4256 4257 out: 4258 DTRACE_NFSV4_2(op__remove__done, struct compound_state *, cs, 4259 REMOVE4res *, resp); 4260 } 4261 4262 /* 4263 * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory, 4264 * oldname and newname. 4265 * res: status. If success - CURRENT_FH unchanged, return change_info 4266 * for both from and target directories. 4267 */ 4268 /* ARGSUSED */ 4269 static void 4270 rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 4271 struct compound_state *cs) 4272 { 4273 RENAME4args *args = &argop->nfs_argop4_u.oprename; 4274 RENAME4res *resp = &resop->nfs_resop4_u.oprename; 4275 int error; 4276 vnode_t *odvp; 4277 vnode_t *ndvp; 4278 vnode_t *srcvp, *targvp; 4279 struct vattr obdva, oidva, oadva; 4280 struct vattr nbdva, nidva, nadva; 4281 char *onm, *nnm; 4282 uint_t olen, nlen; 4283 rfs4_file_t *fp, *sfp; 4284 int in_crit_src, in_crit_targ; 4285 int fp_rele_grant_hold, sfp_rele_grant_hold; 4286 bslabel_t *clabel; 4287 struct sockaddr *ca; 4288 char *converted_onm = NULL; 4289 char *converted_nnm = NULL; 4290 4291 DTRACE_NFSV4_2(op__rename__start, struct compound_state *, cs, 4292 RENAME4args *, args); 4293 4294 fp = sfp = NULL; 4295 srcvp = targvp = NULL; 4296 in_crit_src = in_crit_targ = 0; 4297 fp_rele_grant_hold = sfp_rele_grant_hold = 0; 4298 4299 /* CURRENT_FH: target directory */ 4300 ndvp = cs->vp; 4301 if (ndvp == NULL) { 4302 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 4303 goto out; 4304 } 4305 4306 /* SAVED_FH: from directory */ 4307 odvp = cs->saved_vp; 4308 if (odvp == NULL) { 4309 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 4310 goto out; 4311 } 4312 4313 if (cs->access == CS_ACCESS_DENIED) { 4314 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4315 goto out; 4316 } 4317 4318 /* 4319 * If there is an unshared filesystem mounted on this vnode, 4320 * do not allow to rename objects in this directory. 4321 */ 4322 if (vn_ismntpt(odvp)) { 4323 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4324 goto out; 4325 } 4326 4327 /* 4328 * If there is an unshared filesystem mounted on this vnode, 4329 * do not allow to rename to this directory. 4330 */ 4331 if (vn_ismntpt(ndvp)) { 4332 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4333 goto out; 4334 } 4335 4336 if (odvp->v_type != VDIR || ndvp->v_type != VDIR) { 4337 *cs->statusp = resp->status = NFS4ERR_NOTDIR; 4338 goto out; 4339 } 4340 4341 if (cs->saved_exi != cs->exi) { 4342 *cs->statusp = resp->status = NFS4ERR_XDEV; 4343 goto out; 4344 } 4345 4346 if (!utf8_dir_verify(&args->oldname)) { 4347 *cs->statusp = resp->status = NFS4ERR_INVAL; 4348 goto out; 4349 } 4350 4351 if (!utf8_dir_verify(&args->newname)) { 4352 *cs->statusp = resp->status = NFS4ERR_INVAL; 4353 goto out; 4354 } 4355 4356 onm = utf8_to_fn(&args->oldname, &olen, NULL); 4357 if (onm == NULL) { 4358 *cs->statusp = resp->status = NFS4ERR_INVAL; 4359 goto out; 4360 } 4361 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 4362 nlen = MAXPATHLEN + 1; 4363 converted_onm = nfscmd_convname(ca, cs->exi, onm, NFSCMD_CONV_INBOUND, 4364 nlen); 4365 4366 if (converted_onm == NULL) { 4367 *cs->statusp = resp->status = NFS4ERR_INVAL; 4368 kmem_free(onm, olen); 4369 goto out; 4370 } 4371 4372 nnm = utf8_to_fn(&args->newname, &nlen, NULL); 4373 if (nnm == NULL) { 4374 *cs->statusp = resp->status = NFS4ERR_INVAL; 4375 if (onm != converted_onm) 4376 kmem_free(converted_onm, MAXPATHLEN + 1); 4377 kmem_free(onm, olen); 4378 goto out; 4379 } 4380 converted_nnm = nfscmd_convname(ca, cs->exi, nnm, NFSCMD_CONV_INBOUND, 4381 MAXPATHLEN + 1); 4382 4383 if (converted_nnm == NULL) { 4384 *cs->statusp = resp->status = NFS4ERR_INVAL; 4385 kmem_free(nnm, nlen); 4386 nnm = NULL; 4387 if (onm != converted_onm) 4388 kmem_free(converted_onm, MAXPATHLEN + 1); 4389 kmem_free(onm, olen); 4390 goto out; 4391 } 4392 4393 4394 if (olen > MAXNAMELEN || nlen > MAXNAMELEN) { 4395 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG; 4396 kmem_free(onm, olen); 4397 kmem_free(nnm, nlen); 4398 goto out; 4399 } 4400 4401 4402 if (rdonly4(cs->exi, cs->vp, req)) { 4403 *cs->statusp = resp->status = NFS4ERR_ROFS; 4404 if (onm != converted_onm) 4405 kmem_free(converted_onm, MAXPATHLEN + 1); 4406 kmem_free(onm, olen); 4407 if (nnm != converted_nnm) 4408 kmem_free(converted_nnm, MAXPATHLEN + 1); 4409 kmem_free(nnm, nlen); 4410 goto out; 4411 } 4412 4413 /* check label of the target dir */ 4414 if (is_system_labeled()) { 4415 ASSERT(req->rq_label != NULL); 4416 clabel = req->rq_label; 4417 DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *, 4418 "got client label from request(1)", 4419 struct svc_req *, req); 4420 if (!blequal(&l_admin_low->tsl_label, clabel)) { 4421 if (!do_rfs_label_check(clabel, ndvp, 4422 EQUALITY_CHECK, cs->exi)) { 4423 *cs->statusp = resp->status = NFS4ERR_ACCESS; 4424 goto err_out; 4425 } 4426 } 4427 } 4428 4429 /* 4430 * Is the source a file and have a delegation? 4431 * We don't need to acquire va_seq before these lookups, if 4432 * it causes an update, cinfo.before will not match, which will 4433 * trigger a cache flush even if atomic is TRUE. 4434 */ 4435 if (sfp = rfs4_lookup_and_findfile(odvp, converted_onm, &srcvp, 4436 &error, cs->cr)) { 4437 if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE, 4438 NULL)) { 4439 *cs->statusp = resp->status = NFS4ERR_DELAY; 4440 goto err_out; 4441 } 4442 } 4443 4444 if (srcvp == NULL) { 4445 *cs->statusp = resp->status = puterrno4(error); 4446 if (onm != converted_onm) 4447 kmem_free(converted_onm, MAXPATHLEN + 1); 4448 kmem_free(onm, olen); 4449 if (nnm != converted_nnm) 4450 kmem_free(converted_onm, MAXPATHLEN + 1); 4451 kmem_free(nnm, nlen); 4452 goto out; 4453 } 4454 4455 sfp_rele_grant_hold = 1; 4456 4457 /* Does the destination exist and a file and have a delegation? */ 4458 if (fp = rfs4_lookup_and_findfile(ndvp, converted_nnm, &targvp, 4459 NULL, cs->cr)) { 4460 if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE, 4461 NULL)) { 4462 *cs->statusp = resp->status = NFS4ERR_DELAY; 4463 goto err_out; 4464 } 4465 } 4466 fp_rele_grant_hold = 1; 4467 4468 4469 /* Check for NBMAND lock on both source and target */ 4470 if (nbl_need_check(srcvp)) { 4471 nbl_start_crit(srcvp, RW_READER); 4472 in_crit_src = 1; 4473 if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL)) { 4474 *cs->statusp = resp->status = NFS4ERR_FILE_OPEN; 4475 goto err_out; 4476 } 4477 } 4478 4479 if (targvp && nbl_need_check(targvp)) { 4480 nbl_start_crit(targvp, RW_READER); 4481 in_crit_targ = 1; 4482 if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) { 4483 *cs->statusp = resp->status = NFS4ERR_FILE_OPEN; 4484 goto err_out; 4485 } 4486 } 4487 4488 /* Get source "before" change value */ 4489 obdva.va_mask = AT_CTIME|AT_SEQ; 4490 error = VOP_GETATTR(odvp, &obdva, 0, cs->cr, NULL); 4491 if (!error) { 4492 nbdva.va_mask = AT_CTIME|AT_SEQ; 4493 error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr, NULL); 4494 } 4495 if (error) { 4496 *cs->statusp = resp->status = puterrno4(error); 4497 goto err_out; 4498 } 4499 4500 NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime) 4501 NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime) 4502 4503 if ((error = VOP_RENAME(odvp, converted_onm, ndvp, converted_nnm, 4504 cs->cr, NULL, 0)) == 0 && fp != NULL) { 4505 struct vattr va; 4506 vnode_t *tvp; 4507 4508 rfs4_dbe_lock(fp->dbe); 4509 tvp = fp->vp; 4510 if (tvp) 4511 VN_HOLD(tvp); 4512 rfs4_dbe_unlock(fp->dbe); 4513 4514 if (tvp) { 4515 va.va_mask = AT_NLINK; 4516 if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) && 4517 va.va_nlink == 0) { 4518 /* The file is gone and so should the state */ 4519 if (in_crit_targ) { 4520 nbl_end_crit(targvp); 4521 in_crit_targ = 0; 4522 } 4523 rfs4_close_all_state(fp); 4524 } 4525 VN_RELE(tvp); 4526 } 4527 } 4528 if (error == 0) 4529 vn_renamepath(ndvp, srcvp, nnm, nlen - 1); 4530 4531 if (in_crit_src) 4532 nbl_end_crit(srcvp); 4533 if (srcvp) 4534 VN_RELE(srcvp); 4535 if (in_crit_targ) 4536 nbl_end_crit(targvp); 4537 if (targvp) 4538 VN_RELE(targvp); 4539 4540 if (sfp) { 4541 rfs4_clear_dont_grant(sfp); 4542 rfs4_file_rele(sfp); 4543 } 4544 if (fp) { 4545 rfs4_clear_dont_grant(fp); 4546 rfs4_file_rele(fp); 4547 } 4548 4549 if (converted_onm != onm) 4550 kmem_free(converted_onm, MAXPATHLEN + 1); 4551 kmem_free(onm, olen); 4552 if (converted_nnm != nnm) 4553 kmem_free(converted_nnm, MAXPATHLEN + 1); 4554 kmem_free(nnm, nlen); 4555 4556 /* 4557 * Get the initial "after" sequence number, if it fails, set to zero 4558 */ 4559 oidva.va_mask = AT_SEQ; 4560 if (VOP_GETATTR(odvp, &oidva, 0, cs->cr, NULL)) 4561 oidva.va_seq = 0; 4562 4563 nidva.va_mask = AT_SEQ; 4564 if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr, NULL)) 4565 nidva.va_seq = 0; 4566 4567 /* 4568 * Force modified data and metadata out to stable storage. 4569 */ 4570 (void) VOP_FSYNC(odvp, 0, cs->cr, NULL); 4571 (void) VOP_FSYNC(ndvp, 0, cs->cr, NULL); 4572 4573 if (error) { 4574 *cs->statusp = resp->status = puterrno4(error); 4575 goto out; 4576 } 4577 4578 /* 4579 * Get "after" change values, if it fails, simply return the 4580 * before value. 4581 */ 4582 oadva.va_mask = AT_CTIME|AT_SEQ; 4583 if (VOP_GETATTR(odvp, &oadva, 0, cs->cr, NULL)) { 4584 oadva.va_ctime = obdva.va_ctime; 4585 oadva.va_seq = 0; 4586 } 4587 4588 nadva.va_mask = AT_CTIME|AT_SEQ; 4589 if (VOP_GETATTR(odvp, &nadva, 0, cs->cr, NULL)) { 4590 nadva.va_ctime = nbdva.va_ctime; 4591 nadva.va_seq = 0; 4592 } 4593 4594 NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime) 4595 NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime) 4596 4597 /* 4598 * The cinfo.atomic = TRUE only if we have 4599 * non-zero va_seq's, and it has incremented by exactly one 4600 * during the VOP_RENAME and it didn't change during the VOP_FSYNC. 4601 */ 4602 if (obdva.va_seq && oidva.va_seq && oadva.va_seq && 4603 oidva.va_seq == (obdva.va_seq + 1) && oidva.va_seq == oadva.va_seq) 4604 resp->source_cinfo.atomic = TRUE; 4605 else 4606 resp->source_cinfo.atomic = FALSE; 4607 4608 if (nbdva.va_seq && nidva.va_seq && nadva.va_seq && 4609 nidva.va_seq == (nbdva.va_seq + 1) && nidva.va_seq == nadva.va_seq) 4610 resp->target_cinfo.atomic = TRUE; 4611 else 4612 resp->target_cinfo.atomic = FALSE; 4613 4614 #ifdef VOLATILE_FH_TEST 4615 { 4616 extern void add_volrnm_fh(struct exportinfo *, vnode_t *); 4617 4618 /* 4619 * Add the renamed file handle to the volatile rename list 4620 */ 4621 if (cs->exi->exi_export.ex_flags & EX_VOLRNM) { 4622 /* file handles may expire on rename */ 4623 vnode_t *vp; 4624 4625 nnm = utf8_to_fn(&args->newname, &nlen, NULL); 4626 /* 4627 * Already know that nnm will be a valid string 4628 */ 4629 error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr, 4630 NULL, NULL, NULL); 4631 kmem_free(nnm, nlen); 4632 if (!error) { 4633 add_volrnm_fh(cs->exi, vp); 4634 VN_RELE(vp); 4635 } 4636 } 4637 } 4638 #endif /* VOLATILE_FH_TEST */ 4639 4640 *cs->statusp = resp->status = NFS4_OK; 4641 out: 4642 DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs, 4643 RENAME4res *, resp); 4644 return; 4645 4646 err_out: 4647 if (onm != converted_onm) 4648 kmem_free(converted_onm, MAXPATHLEN + 1); 4649 if (onm != NULL) 4650 kmem_free(onm, olen); 4651 if (nnm != converted_nnm) 4652 kmem_free(converted_nnm, MAXPATHLEN + 1); 4653 if (nnm != NULL) 4654 kmem_free(nnm, nlen); 4655 4656 if (in_crit_src) nbl_end_crit(srcvp); 4657 if (in_crit_targ) nbl_end_crit(targvp); 4658 if (targvp) VN_RELE(targvp); 4659 if (srcvp) VN_RELE(srcvp); 4660 if (sfp) { 4661 if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp); 4662 rfs4_file_rele(sfp); 4663 } 4664 if (fp) { 4665 if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp); 4666 rfs4_file_rele(fp); 4667 } 4668 4669 DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs, 4670 RENAME4res *, resp); 4671 } 4672 4673 /* ARGSUSED */ 4674 static void 4675 rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 4676 struct compound_state *cs) 4677 { 4678 RENEW4args *args = &argop->nfs_argop4_u.oprenew; 4679 RENEW4res *resp = &resop->nfs_resop4_u.oprenew; 4680 rfs4_client_t *cp; 4681 4682 DTRACE_NFSV4_2(op__renew__start, struct compound_state *, cs, 4683 RENEW4args *, args); 4684 4685 if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) { 4686 *cs->statusp = resp->status = 4687 rfs4_check_clientid(&args->clientid, 0); 4688 goto out; 4689 } 4690 4691 if (rfs4_lease_expired(cp)) { 4692 rfs4_client_rele(cp); 4693 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 4694 goto out; 4695 } 4696 4697 rfs4_update_lease(cp); 4698 4699 mutex_enter(cp->cbinfo.cb_lock); 4700 if (cp->cbinfo.cb_notified_of_cb_path_down == FALSE) { 4701 cp->cbinfo.cb_notified_of_cb_path_down = TRUE; 4702 *cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN; 4703 } else { 4704 *cs->statusp = resp->status = NFS4_OK; 4705 } 4706 mutex_exit(cp->cbinfo.cb_lock); 4707 4708 rfs4_client_rele(cp); 4709 4710 out: 4711 DTRACE_NFSV4_2(op__renew__done, struct compound_state *, cs, 4712 RENEW4res *, resp); 4713 } 4714 4715 /* ARGSUSED */ 4716 static void 4717 rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req, 4718 struct compound_state *cs) 4719 { 4720 RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh; 4721 4722 DTRACE_NFSV4_1(op__restorefh__start, struct compound_state *, cs); 4723 4724 /* No need to check cs->access - we are not accessing any object */ 4725 if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) { 4726 *cs->statusp = resp->status = NFS4ERR_RESTOREFH; 4727 goto out; 4728 } 4729 if (cs->vp != NULL) { 4730 VN_RELE(cs->vp); 4731 } 4732 cs->vp = cs->saved_vp; 4733 cs->saved_vp = NULL; 4734 cs->exi = cs->saved_exi; 4735 nfs_fh4_copy(&cs->saved_fh, &cs->fh); 4736 *cs->statusp = resp->status = NFS4_OK; 4737 cs->deleg = FALSE; 4738 4739 out: 4740 DTRACE_NFSV4_2(op__restorefh__done, struct compound_state *, cs, 4741 RESTOREFH4res *, resp); 4742 } 4743 4744 /* ARGSUSED */ 4745 static void 4746 rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 4747 struct compound_state *cs) 4748 { 4749 SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh; 4750 4751 DTRACE_NFSV4_1(op__savefh__start, struct compound_state *, cs); 4752 4753 /* No need to check cs->access - we are not accessing any object */ 4754 if (cs->vp == NULL) { 4755 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 4756 goto out; 4757 } 4758 if (cs->saved_vp != NULL) { 4759 VN_RELE(cs->saved_vp); 4760 } 4761 cs->saved_vp = cs->vp; 4762 VN_HOLD(cs->saved_vp); 4763 cs->saved_exi = cs->exi; 4764 /* 4765 * since SAVEFH is fairly rare, don't alloc space for its fh 4766 * unless necessary. 4767 */ 4768 if (cs->saved_fh.nfs_fh4_val == NULL) { 4769 cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP); 4770 } 4771 nfs_fh4_copy(&cs->fh, &cs->saved_fh); 4772 *cs->statusp = resp->status = NFS4_OK; 4773 4774 out: 4775 DTRACE_NFSV4_2(op__savefh__done, struct compound_state *, cs, 4776 SAVEFH4res *, resp); 4777 } 4778 4779 /* 4780 * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to 4781 * return the bitmap of attrs that were set successfully. It is also 4782 * called by Verify/Nverify to test the vattr/vfsstat attrs. It should 4783 * always be called only after rfs4_do_set_attrs(). 4784 * 4785 * Verify that the attributes are same as the expected ones. sargp->vap 4786 * and sargp->sbp contain the input attributes as translated from fattr4. 4787 * 4788 * This function verifies only the attrs that correspond to a vattr or 4789 * vfsstat struct. That is because of the extra step needed to get the 4790 * corresponding system structs. Other attributes have already been set or 4791 * verified by do_rfs4_set_attrs. 4792 * 4793 * Return 0 if all attrs match, -1 if some don't, error if error processing. 4794 */ 4795 static int 4796 rfs4_verify_attr(struct nfs4_svgetit_arg *sargp, 4797 bitmap4 *resp, struct nfs4_ntov_table *ntovp) 4798 { 4799 int error, ret_error = 0; 4800 int i, k; 4801 uint_t sva_mask = sargp->vap->va_mask; 4802 uint_t vbit; 4803 union nfs4_attr_u *na; 4804 uint8_t *amap; 4805 bool_t getsb = ntovp->vfsstat; 4806 4807 if (sva_mask != 0) { 4808 /* 4809 * Okay to overwrite sargp->vap because we verify based 4810 * on the incoming values. 4811 */ 4812 ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0, 4813 sargp->cs->cr, NULL); 4814 if (ret_error) { 4815 if (resp == NULL) 4816 return (ret_error); 4817 /* 4818 * Must return bitmap of successful attrs 4819 */ 4820 sva_mask = 0; /* to prevent checking vap later */ 4821 } else { 4822 /* 4823 * Some file systems clobber va_mask. it is probably 4824 * wrong of them to do so, nonethless we practice 4825 * defensive coding. 4826 * See bug id 4276830. 4827 */ 4828 sargp->vap->va_mask = sva_mask; 4829 } 4830 } 4831 4832 if (getsb) { 4833 /* 4834 * Now get the superblock and loop on the bitmap, as there is 4835 * no simple way of translating from superblock to bitmap4. 4836 */ 4837 ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp); 4838 if (ret_error) { 4839 if (resp == NULL) 4840 goto errout; 4841 getsb = FALSE; 4842 } 4843 } 4844 4845 /* 4846 * Now loop and verify each attribute which getattr returned 4847 * whether it's the same as the input. 4848 */ 4849 if (resp == NULL && !getsb && (sva_mask == 0)) 4850 goto errout; 4851 4852 na = ntovp->na; 4853 amap = ntovp->amap; 4854 k = 0; 4855 for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) { 4856 k = *amap; 4857 ASSERT(nfs4_ntov_map[k].nval == k); 4858 vbit = nfs4_ntov_map[k].vbit; 4859 4860 /* 4861 * If vattr attribute but VOP_GETATTR failed, or it's 4862 * superblock attribute but VFS_STATVFS failed, skip 4863 */ 4864 if (vbit) { 4865 if ((vbit & sva_mask) == 0) 4866 continue; 4867 } else if (!(getsb && nfs4_ntov_map[k].vfsstat)) { 4868 continue; 4869 } 4870 error = (*nfs4_ntov_map[k].sv_getit)(NFS4ATTR_VERIT, sargp, na); 4871 if (resp != NULL) { 4872 if (error) 4873 ret_error = -1; /* not all match */ 4874 else /* update response bitmap */ 4875 *resp |= nfs4_ntov_map[k].fbit; 4876 continue; 4877 } 4878 if (error) { 4879 ret_error = -1; /* not all match */ 4880 break; 4881 } 4882 } 4883 errout: 4884 return (ret_error); 4885 } 4886 4887 /* 4888 * Decode the attribute to be set/verified. If the attr requires a sys op 4889 * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't 4890 * call the sv_getit function for it, because the sys op hasn't yet been done. 4891 * Return 0 for success, error code if failed. 4892 * 4893 * Note: the decoded arg is not freed here but in nfs4_ntov_table_free. 4894 */ 4895 static int 4896 decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp, 4897 int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap) 4898 { 4899 int error = 0; 4900 bool_t set_later; 4901 4902 sargp->vap->va_mask |= nfs4_ntov_map[k].vbit; 4903 4904 if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) { 4905 set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat; 4906 /* 4907 * don't verify yet if a vattr or sb dependent attr, 4908 * because we don't have their sys values yet. 4909 * Will be done later. 4910 */ 4911 if (! (set_later && (cmd == NFS4ATTR_VERIT))) { 4912 /* 4913 * ACLs are a special case, since setting the MODE 4914 * conflicts with setting the ACL. We delay setting 4915 * the ACL until all other attributes have been set. 4916 * The ACL gets set in do_rfs4_op_setattr(). 4917 */ 4918 if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) { 4919 error = (*nfs4_ntov_map[k].sv_getit)(cmd, 4920 sargp, nap); 4921 if (error) { 4922 xdr_free(nfs4_ntov_map[k].xfunc, 4923 (caddr_t)nap); 4924 } 4925 } 4926 } 4927 } else { 4928 #ifdef DEBUG 4929 cmn_err(CE_NOTE, "decode_fattr4_attr: error " 4930 "decoding attribute %d\n", k); 4931 #endif 4932 error = EINVAL; 4933 } 4934 if (!error && resp_bval && !set_later) { 4935 *resp_bval |= nfs4_ntov_map[k].fbit; 4936 } 4937 4938 return (error); 4939 } 4940 4941 /* 4942 * Set vattr based on incoming fattr4 attrs - used by setattr. 4943 * Set response mask. Ignore any values that are not writable vattr attrs. 4944 */ 4945 static nfsstat4 4946 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs, 4947 struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp, 4948 nfs4_attr_cmd_t cmd) 4949 { 4950 int error = 0; 4951 int i; 4952 char *attrs = fattrp->attrlist4; 4953 uint32_t attrslen = fattrp->attrlist4_len; 4954 XDR xdr; 4955 nfsstat4 status = NFS4_OK; 4956 vnode_t *vp = cs->vp; 4957 union nfs4_attr_u *na; 4958 uint8_t *amap; 4959 4960 #ifndef lint 4961 /* 4962 * Make sure that maximum attribute number can be expressed as an 4963 * 8 bit quantity. 4964 */ 4965 ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1)); 4966 #endif 4967 4968 if (vp == NULL) { 4969 if (resp) 4970 *resp = 0; 4971 return (NFS4ERR_NOFILEHANDLE); 4972 } 4973 if (cs->access == CS_ACCESS_DENIED) { 4974 if (resp) 4975 *resp = 0; 4976 return (NFS4ERR_ACCESS); 4977 } 4978 4979 sargp->op = cmd; 4980 sargp->cs = cs; 4981 sargp->flag = 0; /* may be set later */ 4982 sargp->vap->va_mask = 0; 4983 sargp->rdattr_error = NFS4_OK; 4984 sargp->rdattr_error_req = FALSE; 4985 /* sargp->sbp is set by the caller */ 4986 4987 xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE); 4988 4989 na = ntovp->na; 4990 amap = ntovp->amap; 4991 4992 /* 4993 * The following loop iterates on the nfs4_ntov_map checking 4994 * if the fbit is set in the requested bitmap. 4995 * If set then we process the arguments using the 4996 * rfs4_fattr4 conversion functions to populate the setattr 4997 * vattr and va_mask. Any settable attrs that are not using vattr 4998 * will be set in this loop. 4999 */ 5000 for (i = 0; i < nfs4_ntov_map_size; i++) { 5001 if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) { 5002 continue; 5003 } 5004 /* 5005 * If setattr, must be a writable attr. 5006 * If verify/nverify, must be a readable attr. 5007 */ 5008 if ((error = (*nfs4_ntov_map[i].sv_getit)( 5009 NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) { 5010 /* 5011 * Client tries to set/verify an 5012 * unsupported attribute, tries to set 5013 * a read only attr or verify a write 5014 * only one - error! 5015 */ 5016 break; 5017 } 5018 /* 5019 * Decode the attribute to set/verify 5020 */ 5021 error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval, 5022 &xdr, resp ? resp : NULL, na); 5023 if (error) 5024 break; 5025 *amap++ = (uint8_t)nfs4_ntov_map[i].nval; 5026 na++; 5027 (ntovp->attrcnt)++; 5028 if (nfs4_ntov_map[i].vfsstat) 5029 ntovp->vfsstat = TRUE; 5030 } 5031 5032 if (error != 0) 5033 status = (error == ENOTSUP ? NFS4ERR_ATTRNOTSUPP : 5034 puterrno4(error)); 5035 /* xdrmem_destroy(&xdrs); */ /* NO-OP */ 5036 return (status); 5037 } 5038 5039 static nfsstat4 5040 do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs, 5041 stateid4 *stateid) 5042 { 5043 int error = 0; 5044 struct nfs4_svgetit_arg sarg; 5045 bool_t trunc; 5046 5047 nfsstat4 status = NFS4_OK; 5048 cred_t *cr = cs->cr; 5049 vnode_t *vp = cs->vp; 5050 struct nfs4_ntov_table ntov; 5051 struct statvfs64 sb; 5052 struct vattr bva; 5053 struct flock64 bf; 5054 int in_crit = 0; 5055 uint_t saved_mask = 0; 5056 caller_context_t ct; 5057 5058 *resp = 0; 5059 sarg.sbp = &sb; 5060 nfs4_ntov_table_init(&ntov); 5061 status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov, 5062 NFS4ATTR_SETIT); 5063 if (status != NFS4_OK) { 5064 /* 5065 * failed set attrs 5066 */ 5067 goto done; 5068 } 5069 if ((sarg.vap->va_mask == 0) && 5070 (! (fattrp->attrmask & FATTR4_ACL_MASK))) { 5071 /* 5072 * no further work to be done 5073 */ 5074 goto done; 5075 } 5076 5077 /* 5078 * If we got a request to set the ACL and the MODE, only 5079 * allow changing VSUID, VSGID, and VSVTX. Attempting 5080 * to change any other bits, along with setting an ACL, 5081 * gives NFS4ERR_INVAL. 5082 */ 5083 if ((fattrp->attrmask & FATTR4_ACL_MASK) && 5084 (fattrp->attrmask & FATTR4_MODE_MASK)) { 5085 vattr_t va; 5086 5087 va.va_mask = AT_MODE; 5088 error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL); 5089 if (error) { 5090 status = puterrno4(error); 5091 goto done; 5092 } 5093 if ((sarg.vap->va_mode ^ va.va_mode) & 5094 ~(VSUID | VSGID | VSVTX)) { 5095 status = NFS4ERR_INVAL; 5096 goto done; 5097 } 5098 } 5099 5100 /* Check stateid only if size has been set */ 5101 if (sarg.vap->va_mask & AT_SIZE) { 5102 trunc = (sarg.vap->va_size == 0); 5103 status = rfs4_check_stateid(FWRITE, cs->vp, stateid, 5104 trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE, &ct); 5105 if (status != NFS4_OK) 5106 goto done; 5107 } else { 5108 ct.cc_sysid = 0; 5109 ct.cc_pid = 0; 5110 ct.cc_caller_id = nfs4_srv_caller_id; 5111 ct.cc_flags = CC_DONTBLOCK; 5112 } 5113 5114 /* XXX start of possible race with delegations */ 5115 5116 /* 5117 * We need to specially handle size changes because it is 5118 * possible for the client to create a file with read-only 5119 * modes, but with the file opened for writing. If the client 5120 * then tries to set the file size, e.g. ftruncate(3C), 5121 * fcntl(F_FREESP), the normal access checking done in 5122 * VOP_SETATTR would prevent the client from doing it even though 5123 * it should be allowed to do so. To get around this, we do the 5124 * access checking for ourselves and use VOP_SPACE which doesn't 5125 * do the access checking. 5126 * Also the client should not be allowed to change the file 5127 * size if there is a conflicting non-blocking mandatory lock in 5128 * the region of the change. 5129 */ 5130 if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) { 5131 u_offset_t offset; 5132 ssize_t length; 5133 5134 /* 5135 * ufs_setattr clears AT_SIZE from vap->va_mask, but 5136 * before returning, sarg.vap->va_mask is used to 5137 * generate the setattr reply bitmap. We also clear 5138 * AT_SIZE below before calling VOP_SPACE. For both 5139 * of these cases, the va_mask needs to be saved here 5140 * and restored after calling VOP_SETATTR. 5141 */ 5142 saved_mask = sarg.vap->va_mask; 5143 5144 /* 5145 * Check any possible conflict due to NBMAND locks. 5146 * Get into critical region before VOP_GETATTR, so the 5147 * size attribute is valid when checking conflicts. 5148 */ 5149 if (nbl_need_check(vp)) { 5150 nbl_start_crit(vp, RW_READER); 5151 in_crit = 1; 5152 } 5153 5154 bva.va_mask = AT_UID|AT_SIZE; 5155 if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) { 5156 status = puterrno4(error); 5157 goto done; 5158 } 5159 5160 if (in_crit) { 5161 if (sarg.vap->va_size < bva.va_size) { 5162 offset = sarg.vap->va_size; 5163 length = bva.va_size - sarg.vap->va_size; 5164 } else { 5165 offset = bva.va_size; 5166 length = sarg.vap->va_size - bva.va_size; 5167 } 5168 if (nbl_conflict(vp, NBL_WRITE, offset, length, 0, 5169 &ct)) { 5170 status = NFS4ERR_LOCKED; 5171 goto done; 5172 } 5173 } 5174 5175 if (crgetuid(cr) == bva.va_uid) { 5176 sarg.vap->va_mask &= ~AT_SIZE; 5177 bf.l_type = F_WRLCK; 5178 bf.l_whence = 0; 5179 bf.l_start = (off64_t)sarg.vap->va_size; 5180 bf.l_len = 0; 5181 bf.l_sysid = 0; 5182 bf.l_pid = 0; 5183 error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE, 5184 (offset_t)sarg.vap->va_size, cr, &ct); 5185 } 5186 } 5187 5188 if (!error && sarg.vap->va_mask != 0) 5189 error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct); 5190 5191 /* restore va_mask -- ufs_setattr clears AT_SIZE */ 5192 if (saved_mask & AT_SIZE) 5193 sarg.vap->va_mask |= AT_SIZE; 5194 5195 /* 5196 * If an ACL was being set, it has been delayed until now, 5197 * in order to set the mode (via the VOP_SETATTR() above) first. 5198 */ 5199 if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) { 5200 int i; 5201 5202 for (i = 0; i < NFS4_MAXNUM_ATTRS; i++) 5203 if (ntov.amap[i] == FATTR4_ACL) 5204 break; 5205 if (i < NFS4_MAXNUM_ATTRS) { 5206 error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)( 5207 NFS4ATTR_SETIT, &sarg, &ntov.na[i]); 5208 if (error == 0) { 5209 *resp |= FATTR4_ACL_MASK; 5210 } else if (error == ENOTSUP) { 5211 (void) rfs4_verify_attr(&sarg, resp, &ntov); 5212 status = NFS4ERR_ATTRNOTSUPP; 5213 goto done; 5214 } 5215 } else { 5216 NFS4_DEBUG(rfs4_debug, 5217 (CE_NOTE, "do_rfs4_op_setattr: " 5218 "unable to find ACL in fattr4")); 5219 error = EINVAL; 5220 } 5221 } 5222 5223 if (error) { 5224 /* check if a monitor detected a delegation conflict */ 5225 if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) 5226 status = NFS4ERR_DELAY; 5227 else 5228 status = puterrno4(error); 5229 5230 /* 5231 * Set the response bitmap when setattr failed. 5232 * If VOP_SETATTR partially succeeded, test by doing a 5233 * VOP_GETATTR on the object and comparing the data 5234 * to the setattr arguments. 5235 */ 5236 (void) rfs4_verify_attr(&sarg, resp, &ntov); 5237 } else { 5238 /* 5239 * Force modified metadata out to stable storage. 5240 */ 5241 (void) VOP_FSYNC(vp, FNODSYNC, cr, &ct); 5242 /* 5243 * Set response bitmap 5244 */ 5245 nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp); 5246 } 5247 5248 /* Return early and already have a NFSv4 error */ 5249 done: 5250 /* 5251 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr 5252 * conversion sets both readable and writeable NFS4 attrs 5253 * for AT_MTIME and AT_ATIME. The line below masks out 5254 * unrequested attrs from the setattr result bitmap. This 5255 * is placed after the done: label to catch the ATTRNOTSUP 5256 * case. 5257 */ 5258 *resp &= fattrp->attrmask; 5259 5260 if (in_crit) 5261 nbl_end_crit(vp); 5262 5263 nfs4_ntov_table_free(&ntov, &sarg); 5264 5265 return (status); 5266 } 5267 5268 /* ARGSUSED */ 5269 static void 5270 rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 5271 struct compound_state *cs) 5272 { 5273 SETATTR4args *args = &argop->nfs_argop4_u.opsetattr; 5274 SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr; 5275 bslabel_t *clabel; 5276 5277 DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs, 5278 SETATTR4args *, args); 5279 5280 if (cs->vp == NULL) { 5281 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 5282 goto out; 5283 } 5284 5285 /* 5286 * If there is an unshared filesystem mounted on this vnode, 5287 * do not allow to setattr on this vnode. 5288 */ 5289 if (vn_ismntpt(cs->vp)) { 5290 *cs->statusp = resp->status = NFS4ERR_ACCESS; 5291 goto out; 5292 } 5293 5294 resp->attrsset = 0; 5295 5296 if (rdonly4(cs->exi, cs->vp, req)) { 5297 *cs->statusp = resp->status = NFS4ERR_ROFS; 5298 goto out; 5299 } 5300 5301 /* check label before setting attributes */ 5302 if (is_system_labeled()) { 5303 ASSERT(req->rq_label != NULL); 5304 clabel = req->rq_label; 5305 DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *, 5306 "got client label from request(1)", 5307 struct svc_req *, req); 5308 if (!blequal(&l_admin_low->tsl_label, clabel)) { 5309 if (!do_rfs_label_check(clabel, cs->vp, 5310 EQUALITY_CHECK, cs->exi)) { 5311 *cs->statusp = resp->status = NFS4ERR_ACCESS; 5312 goto out; 5313 } 5314 } 5315 } 5316 5317 *cs->statusp = resp->status = 5318 do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs, 5319 &args->stateid); 5320 5321 out: 5322 DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs, 5323 SETATTR4res *, resp); 5324 } 5325 5326 /* ARGSUSED */ 5327 static void 5328 rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 5329 struct compound_state *cs) 5330 { 5331 /* 5332 * verify and nverify are exactly the same, except that nverify 5333 * succeeds when some argument changed, and verify succeeds when 5334 * when none changed. 5335 */ 5336 5337 VERIFY4args *args = &argop->nfs_argop4_u.opverify; 5338 VERIFY4res *resp = &resop->nfs_resop4_u.opverify; 5339 5340 int error; 5341 struct nfs4_svgetit_arg sarg; 5342 struct statvfs64 sb; 5343 struct nfs4_ntov_table ntov; 5344 5345 DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs, 5346 VERIFY4args *, args); 5347 5348 if (cs->vp == NULL) { 5349 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 5350 goto out; 5351 } 5352 5353 sarg.sbp = &sb; 5354 nfs4_ntov_table_init(&ntov); 5355 resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs, 5356 &sarg, &ntov, NFS4ATTR_VERIT); 5357 if (resp->status != NFS4_OK) { 5358 /* 5359 * do_rfs4_set_attrs will try to verify systemwide attrs, 5360 * so could return -1 for "no match". 5361 */ 5362 if (resp->status == -1) 5363 resp->status = NFS4ERR_NOT_SAME; 5364 goto done; 5365 } 5366 error = rfs4_verify_attr(&sarg, NULL, &ntov); 5367 switch (error) { 5368 case 0: 5369 resp->status = NFS4_OK; 5370 break; 5371 case -1: 5372 resp->status = NFS4ERR_NOT_SAME; 5373 break; 5374 default: 5375 resp->status = puterrno4(error); 5376 break; 5377 } 5378 done: 5379 *cs->statusp = resp->status; 5380 nfs4_ntov_table_free(&ntov, &sarg); 5381 out: 5382 DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs, 5383 VERIFY4res *, resp); 5384 } 5385 5386 /* ARGSUSED */ 5387 static void 5388 rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 5389 struct compound_state *cs) 5390 { 5391 /* 5392 * verify and nverify are exactly the same, except that nverify 5393 * succeeds when some argument changed, and verify succeeds when 5394 * when none changed. 5395 */ 5396 5397 NVERIFY4args *args = &argop->nfs_argop4_u.opnverify; 5398 NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify; 5399 5400 int error; 5401 struct nfs4_svgetit_arg sarg; 5402 struct statvfs64 sb; 5403 struct nfs4_ntov_table ntov; 5404 5405 DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs, 5406 NVERIFY4args *, args); 5407 5408 if (cs->vp == NULL) { 5409 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 5410 DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs, 5411 NVERIFY4res *, resp); 5412 return; 5413 } 5414 sarg.sbp = &sb; 5415 nfs4_ntov_table_init(&ntov); 5416 resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs, 5417 &sarg, &ntov, NFS4ATTR_VERIT); 5418 if (resp->status != NFS4_OK) { 5419 /* 5420 * do_rfs4_set_attrs will try to verify systemwide attrs, 5421 * so could return -1 for "no match". 5422 */ 5423 if (resp->status == -1) 5424 resp->status = NFS4_OK; 5425 goto done; 5426 } 5427 error = rfs4_verify_attr(&sarg, NULL, &ntov); 5428 switch (error) { 5429 case 0: 5430 resp->status = NFS4ERR_SAME; 5431 break; 5432 case -1: 5433 resp->status = NFS4_OK; 5434 break; 5435 default: 5436 resp->status = puterrno4(error); 5437 break; 5438 } 5439 done: 5440 *cs->statusp = resp->status; 5441 nfs4_ntov_table_free(&ntov, &sarg); 5442 5443 DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs, 5444 NVERIFY4res *, resp); 5445 } 5446 5447 /* 5448 * XXX - This should live in an NFS header file. 5449 */ 5450 #define MAX_IOVECS 12 5451 5452 /* ARGSUSED */ 5453 static void 5454 rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req, 5455 struct compound_state *cs) 5456 { 5457 WRITE4args *args = &argop->nfs_argop4_u.opwrite; 5458 WRITE4res *resp = &resop->nfs_resop4_u.opwrite; 5459 int error; 5460 vnode_t *vp; 5461 struct vattr bva; 5462 u_offset_t rlimit; 5463 struct uio uio; 5464 struct iovec iov[MAX_IOVECS]; 5465 struct iovec *iovp; 5466 int iovcnt; 5467 int ioflag; 5468 cred_t *savecred, *cr; 5469 bool_t *deleg = &cs->deleg; 5470 nfsstat4 stat; 5471 int in_crit = 0; 5472 caller_context_t ct; 5473 5474 DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs, 5475 WRITE4args *, args); 5476 5477 vp = cs->vp; 5478 if (vp == NULL) { 5479 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 5480 goto out; 5481 } 5482 if (cs->access == CS_ACCESS_DENIED) { 5483 *cs->statusp = resp->status = NFS4ERR_ACCESS; 5484 goto out; 5485 } 5486 5487 cr = cs->cr; 5488 5489 if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE, 5490 deleg, TRUE, &ct)) != NFS4_OK) { 5491 *cs->statusp = resp->status = stat; 5492 goto out; 5493 } 5494 5495 /* 5496 * We have to enter the critical region before calling VOP_RWLOCK 5497 * to avoid a deadlock with ufs. 5498 */ 5499 if (nbl_need_check(vp)) { 5500 nbl_start_crit(vp, RW_READER); 5501 in_crit = 1; 5502 if (nbl_conflict(vp, NBL_WRITE, 5503 args->offset, args->data_len, 0, &ct)) { 5504 *cs->statusp = resp->status = NFS4ERR_LOCKED; 5505 goto out; 5506 } 5507 } 5508 5509 bva.va_mask = AT_MODE | AT_UID; 5510 error = VOP_GETATTR(vp, &bva, 0, cr, &ct); 5511 5512 /* 5513 * If we can't get the attributes, then we can't do the 5514 * right access checking. So, we'll fail the request. 5515 */ 5516 if (error) { 5517 *cs->statusp = resp->status = puterrno4(error); 5518 goto out; 5519 } 5520 5521 if (rdonly4(cs->exi, cs->vp, req)) { 5522 *cs->statusp = resp->status = NFS4ERR_ROFS; 5523 goto out; 5524 } 5525 5526 if (vp->v_type != VREG) { 5527 *cs->statusp = resp->status = 5528 ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL); 5529 goto out; 5530 } 5531 5532 if (crgetuid(cr) != bva.va_uid && 5533 (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) { 5534 *cs->statusp = resp->status = puterrno4(error); 5535 goto out; 5536 } 5537 5538 if (MANDLOCK(vp, bva.va_mode)) { 5539 *cs->statusp = resp->status = NFS4ERR_ACCESS; 5540 goto out; 5541 } 5542 5543 if (args->data_len == 0) { 5544 *cs->statusp = resp->status = NFS4_OK; 5545 resp->count = 0; 5546 resp->committed = args->stable; 5547 resp->writeverf = Write4verf; 5548 goto out; 5549 } 5550 5551 if (args->mblk != NULL) { 5552 mblk_t *m; 5553 uint_t bytes, round_len; 5554 5555 iovcnt = 0; 5556 bytes = 0; 5557 round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT); 5558 for (m = args->mblk; 5559 m != NULL && bytes < round_len; 5560 m = m->b_cont) { 5561 iovcnt++; 5562 bytes += MBLKL(m); 5563 } 5564 #ifdef DEBUG 5565 /* should have ended on an mblk boundary */ 5566 if (bytes != round_len) { 5567 printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n", 5568 bytes, round_len, args->data_len); 5569 printf("args=%p, args->mblk=%p, m=%p", (void *)args, 5570 (void *)args->mblk, (void *)m); 5571 ASSERT(bytes == round_len); 5572 } 5573 #endif 5574 if (iovcnt <= MAX_IOVECS) { 5575 iovp = iov; 5576 } else { 5577 iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP); 5578 } 5579 mblk_to_iov(args->mblk, iovcnt, iovp); 5580 } else if (args->rlist != NULL) { 5581 iovcnt = 1; 5582 iovp = iov; 5583 iovp->iov_base = (char *)((args->rlist)->u.c_daddr3); 5584 iovp->iov_len = args->data_len; 5585 } else { 5586 iovcnt = 1; 5587 iovp = iov; 5588 iovp->iov_base = args->data_val; 5589 iovp->iov_len = args->data_len; 5590 } 5591 5592 uio.uio_iov = iovp; 5593 uio.uio_iovcnt = iovcnt; 5594 5595 uio.uio_segflg = UIO_SYSSPACE; 5596 uio.uio_extflg = UIO_COPY_DEFAULT; 5597 uio.uio_loffset = args->offset; 5598 uio.uio_resid = args->data_len; 5599 uio.uio_llimit = curproc->p_fsz_ctl; 5600 rlimit = uio.uio_llimit - args->offset; 5601 if (rlimit < (u_offset_t)uio.uio_resid) 5602 uio.uio_resid = (int)rlimit; 5603 5604 if (args->stable == UNSTABLE4) 5605 ioflag = 0; 5606 else if (args->stable == FILE_SYNC4) 5607 ioflag = FSYNC; 5608 else if (args->stable == DATA_SYNC4) 5609 ioflag = FDSYNC; 5610 else { 5611 if (iovp != iov) 5612 kmem_free(iovp, sizeof (*iovp) * iovcnt); 5613 *cs->statusp = resp->status = NFS4ERR_INVAL; 5614 goto out; 5615 } 5616 5617 /* 5618 * We're changing creds because VM may fault and we need 5619 * the cred of the current thread to be used if quota 5620 * checking is enabled. 5621 */ 5622 savecred = curthread->t_cred; 5623 curthread->t_cred = cr; 5624 error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct); 5625 curthread->t_cred = savecred; 5626 5627 if (iovp != iov) 5628 kmem_free(iovp, sizeof (*iovp) * iovcnt); 5629 5630 if (error) { 5631 *cs->statusp = resp->status = puterrno4(error); 5632 goto out; 5633 } 5634 5635 *cs->statusp = resp->status = NFS4_OK; 5636 resp->count = args->data_len - uio.uio_resid; 5637 5638 if (ioflag == 0) 5639 resp->committed = UNSTABLE4; 5640 else 5641 resp->committed = FILE_SYNC4; 5642 5643 resp->writeverf = Write4verf; 5644 5645 out: 5646 if (in_crit) 5647 nbl_end_crit(vp); 5648 5649 DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs, 5650 WRITE4res *, resp); 5651 } 5652 5653 5654 /* XXX put in a header file */ 5655 extern int sec_svc_getcred(struct svc_req *, cred_t *, caddr_t *, int *); 5656 5657 void 5658 rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi, 5659 struct svc_req *req, cred_t *cr, int *rv) 5660 { 5661 uint_t i; 5662 struct compound_state cs; 5663 5664 if (rv != NULL) 5665 *rv = 0; 5666 rfs4_init_compound_state(&cs); 5667 /* 5668 * Form a reply tag by copying over the reqeuest tag. 5669 */ 5670 resp->tag.utf8string_val = 5671 kmem_alloc(args->tag.utf8string_len, KM_SLEEP); 5672 resp->tag.utf8string_len = args->tag.utf8string_len; 5673 bcopy(args->tag.utf8string_val, resp->tag.utf8string_val, 5674 resp->tag.utf8string_len); 5675 5676 cs.statusp = &resp->status; 5677 cs.req = req; 5678 5679 /* 5680 * XXX for now, minorversion should be zero 5681 */ 5682 if (args->minorversion != NFS4_MINORVERSION) { 5683 DTRACE_NFSV4_2(compound__start, struct compound_state *, 5684 &cs, COMPOUND4args *, args); 5685 resp->array_len = 0; 5686 resp->array = NULL; 5687 resp->status = NFS4ERR_MINOR_VERS_MISMATCH; 5688 DTRACE_NFSV4_2(compound__done, struct compound_state *, 5689 &cs, COMPOUND4res *, resp); 5690 return; 5691 } 5692 5693 ASSERT(exi == NULL); 5694 ASSERT(cr == NULL); 5695 5696 cr = crget(); 5697 ASSERT(cr != NULL); 5698 5699 if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) { 5700 DTRACE_NFSV4_2(compound__start, struct compound_state *, 5701 &cs, COMPOUND4args *, args); 5702 crfree(cr); 5703 DTRACE_NFSV4_2(compound__done, struct compound_state *, 5704 &cs, COMPOUND4res *, resp); 5705 svcerr_badcred(req->rq_xprt); 5706 if (rv != NULL) 5707 *rv = 1; 5708 return; 5709 } 5710 resp->array_len = args->array_len; 5711 resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4), 5712 KM_SLEEP); 5713 5714 cs.basecr = cr; 5715 5716 DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs, 5717 COMPOUND4args *, args); 5718 5719 /* 5720 * For now, NFS4 compound processing must be protected by 5721 * exported_lock because it can access more than one exportinfo 5722 * per compound and share/unshare can now change multiple 5723 * exinfo structs. The NFS2/3 code only refs 1 exportinfo 5724 * per proc (excluding public exinfo), and exi_count design 5725 * is sufficient to protect concurrent execution of NFS2/3 5726 * ops along with unexport. This lock will be removed as 5727 * part of the NFSv4 phase 2 namespace redesign work. 5728 */ 5729 rw_enter(&exported_lock, RW_READER); 5730 5731 /* 5732 * If this is the first compound we've seen, we need to start all 5733 * new instances' grace periods. 5734 */ 5735 if (rfs4_seen_first_compound == 0) { 5736 rfs4_grace_start_new(); 5737 /* 5738 * This must be set after rfs4_grace_start_new(), otherwise 5739 * another thread could proceed past here before the former 5740 * is finished. 5741 */ 5742 rfs4_seen_first_compound = 1; 5743 } 5744 5745 for (i = 0; i < args->array_len && cs.cont; i++) { 5746 nfs_argop4 *argop; 5747 nfs_resop4 *resop; 5748 uint_t op; 5749 5750 argop = &args->array[i]; 5751 resop = &resp->array[i]; 5752 resop->resop = argop->argop; 5753 op = (uint_t)resop->resop; 5754 5755 if (op < rfsv4disp_cnt) { 5756 /* 5757 * Count the individual ops here; NULL and COMPOUND 5758 * are counted in common_dispatch() 5759 */ 5760 rfsproccnt_v4_ptr[op].value.ui64++; 5761 5762 NFS4_DEBUG(rfs4_debug > 1, 5763 (CE_NOTE, "Executing %s", rfs4_op_string[op])); 5764 (*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs); 5765 NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d", 5766 rfs4_op_string[op], *cs.statusp)); 5767 if (*cs.statusp != NFS4_OK) 5768 cs.cont = FALSE; 5769 } else { 5770 /* 5771 * This is effectively dead code since XDR code 5772 * will have already returned BADXDR if op doesn't 5773 * decode to legal value. This only done for a 5774 * day when XDR code doesn't verify v4 opcodes. 5775 */ 5776 op = OP_ILLEGAL; 5777 rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++; 5778 5779 rfs4_op_illegal(argop, resop, req, &cs); 5780 cs.cont = FALSE; 5781 } 5782 5783 /* 5784 * If not at last op, and if we are to stop, then 5785 * compact the results array. 5786 */ 5787 if ((i + 1) < args->array_len && !cs.cont) { 5788 nfs_resop4 *new_res = kmem_alloc( 5789 (i+1) * sizeof (nfs_resop4), KM_SLEEP); 5790 bcopy(resp->array, 5791 new_res, (i+1) * sizeof (nfs_resop4)); 5792 kmem_free(resp->array, 5793 args->array_len * sizeof (nfs_resop4)); 5794 5795 resp->array_len = i + 1; 5796 resp->array = new_res; 5797 } 5798 } 5799 5800 rw_exit(&exported_lock); 5801 5802 DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs, 5803 COMPOUND4res *, resp); 5804 5805 if (cs.vp) 5806 VN_RELE(cs.vp); 5807 if (cs.saved_vp) 5808 VN_RELE(cs.saved_vp); 5809 if (cs.saved_fh.nfs_fh4_val) 5810 kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE); 5811 5812 if (cs.basecr) 5813 crfree(cs.basecr); 5814 if (cs.cr) 5815 crfree(cs.cr); 5816 /* 5817 * done with this compound request, free the label 5818 */ 5819 5820 if (req->rq_label != NULL) { 5821 kmem_free(req->rq_label, sizeof (bslabel_t)); 5822 req->rq_label = NULL; 5823 } 5824 } 5825 5826 /* 5827 * XXX because of what appears to be duplicate calls to rfs4_compound_free 5828 * XXX zero out the tag and array values. Need to investigate why the 5829 * XXX calls occur, but at least prevent the panic for now. 5830 */ 5831 void 5832 rfs4_compound_free(COMPOUND4res *resp) 5833 { 5834 uint_t i; 5835 5836 if (resp->tag.utf8string_val) { 5837 UTF8STRING_FREE(resp->tag) 5838 } 5839 5840 for (i = 0; i < resp->array_len; i++) { 5841 nfs_resop4 *resop; 5842 uint_t op; 5843 5844 resop = &resp->array[i]; 5845 op = (uint_t)resop->resop; 5846 if (op < rfsv4disp_cnt) { 5847 (*rfsv4disptab[op].dis_resfree)(resop); 5848 } 5849 } 5850 if (resp->array != NULL) { 5851 kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4)); 5852 } 5853 } 5854 5855 /* 5856 * Process the value of the compound request rpc flags, as a bit-AND 5857 * of the individual per-op flags (idempotent, allowork, publicfh_ok) 5858 */ 5859 void 5860 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp) 5861 { 5862 int i; 5863 int flag = RPC_ALL; 5864 5865 for (i = 0; flag && i < args->array_len; i++) { 5866 uint_t op; 5867 5868 op = (uint_t)args->array[i].argop; 5869 5870 if (op < rfsv4disp_cnt) 5871 flag &= rfsv4disptab[op].dis_flags; 5872 else 5873 flag = 0; 5874 } 5875 *flagp = flag; 5876 } 5877 5878 nfsstat4 5879 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp) 5880 { 5881 nfsstat4 e; 5882 5883 rfs4_dbe_lock(cp->dbe); 5884 5885 if (cp->sysidt != LM_NOSYSID) { 5886 *sp = cp->sysidt; 5887 e = NFS4_OK; 5888 5889 } else if ((cp->sysidt = lm_alloc_sysidt()) != LM_NOSYSID) { 5890 *sp = cp->sysidt; 5891 e = NFS4_OK; 5892 5893 NFS4_DEBUG(rfs4_debug, (CE_NOTE, 5894 "rfs4_client_sysid: allocated 0x%x\n", *sp)); 5895 } else 5896 e = NFS4ERR_DELAY; 5897 5898 rfs4_dbe_unlock(cp->dbe); 5899 return (e); 5900 } 5901 5902 #if defined(DEBUG) && ! defined(lint) 5903 static void lock_print(char *str, int operation, struct flock64 *flk) 5904 { 5905 char *op, *type; 5906 5907 switch (operation) { 5908 case F_GETLK: op = "F_GETLK"; 5909 break; 5910 case F_SETLK: op = "F_SETLK"; 5911 break; 5912 case F_SETLK_NBMAND: op = "F_SETLK_NBMAND"; 5913 break; 5914 default: op = "F_UNKNOWN"; 5915 break; 5916 } 5917 switch (flk->l_type) { 5918 case F_UNLCK: type = "F_UNLCK"; 5919 break; 5920 case F_RDLCK: type = "F_RDLCK"; 5921 break; 5922 case F_WRLCK: type = "F_WRLCK"; 5923 break; 5924 default: type = "F_UNKNOWN"; 5925 break; 5926 } 5927 5928 ASSERT(flk->l_whence == 0); 5929 cmn_err(CE_NOTE, "%s: %s, type = %s, off = %llx len = %llx pid = %d", 5930 str, op, type, (longlong_t)flk->l_start, 5931 flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid); 5932 } 5933 5934 #define LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f) 5935 #else 5936 #define LOCK_PRINT(d, s, t, f) 5937 #endif 5938 5939 /*ARGSUSED*/ 5940 static bool_t 5941 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs) 5942 { 5943 return (TRUE); 5944 } 5945 5946 /* 5947 * Look up the pathname using the vp in cs as the directory vnode. 5948 * cs->vp will be the vnode for the file on success 5949 */ 5950 5951 static nfsstat4 5952 rfs4_lookup(component4 *component, struct svc_req *req, 5953 struct compound_state *cs) 5954 { 5955 char *nm; 5956 uint32_t len; 5957 nfsstat4 status; 5958 5959 if (cs->vp == NULL) { 5960 return (NFS4ERR_NOFILEHANDLE); 5961 } 5962 if (cs->vp->v_type != VDIR) { 5963 return (NFS4ERR_NOTDIR); 5964 } 5965 5966 if (!utf8_dir_verify(component)) 5967 return (NFS4ERR_INVAL); 5968 5969 nm = utf8_to_fn(component, &len, NULL); 5970 if (nm == NULL) { 5971 return (NFS4ERR_INVAL); 5972 } 5973 5974 if (len > MAXNAMELEN) { 5975 kmem_free(nm, len); 5976 return (NFS4ERR_NAMETOOLONG); 5977 } 5978 5979 status = do_rfs4_op_lookup(nm, len, req, cs); 5980 5981 kmem_free(nm, len); 5982 5983 return (status); 5984 } 5985 5986 static nfsstat4 5987 rfs4_lookupfile(component4 *component, struct svc_req *req, 5988 struct compound_state *cs, uint32_t access, 5989 change_info4 *cinfo) 5990 { 5991 nfsstat4 status; 5992 vnode_t *dvp = cs->vp; 5993 vattr_t bva, ava, fva; 5994 int error; 5995 5996 /* Get "before" change value */ 5997 bva.va_mask = AT_CTIME|AT_SEQ; 5998 error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL); 5999 if (error) 6000 return (puterrno4(error)); 6001 6002 /* rfs4_lookup may VN_RELE directory */ 6003 VN_HOLD(dvp); 6004 6005 status = rfs4_lookup(component, req, cs); 6006 if (status != NFS4_OK) { 6007 VN_RELE(dvp); 6008 return (status); 6009 } 6010 6011 /* 6012 * Get "after" change value, if it fails, simply return the 6013 * before value. 6014 */ 6015 ava.va_mask = AT_CTIME|AT_SEQ; 6016 if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) { 6017 ava.va_ctime = bva.va_ctime; 6018 ava.va_seq = 0; 6019 } 6020 VN_RELE(dvp); 6021 6022 /* 6023 * Validate the file is a file 6024 */ 6025 fva.va_mask = AT_TYPE|AT_MODE; 6026 error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL); 6027 if (error) 6028 return (puterrno4(error)); 6029 6030 if (fva.va_type != VREG) { 6031 if (fva.va_type == VDIR) 6032 return (NFS4ERR_ISDIR); 6033 if (fva.va_type == VLNK) 6034 return (NFS4ERR_SYMLINK); 6035 return (NFS4ERR_INVAL); 6036 } 6037 6038 NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime); 6039 NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime); 6040 6041 /* 6042 * It is undefined if VOP_LOOKUP will change va_seq, so 6043 * cinfo.atomic = TRUE only if we have 6044 * non-zero va_seq's, and they have not changed. 6045 */ 6046 if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq) 6047 cinfo->atomic = TRUE; 6048 else 6049 cinfo->atomic = FALSE; 6050 6051 /* Check for mandatory locking */ 6052 cs->mandlock = MANDLOCK(cs->vp, fva.va_mode); 6053 return (check_open_access(access, cs, req)); 6054 } 6055 6056 static nfsstat4 6057 create_vnode(vnode_t *dvp, char *nm, vattr_t *vap, createmode4 mode, 6058 timespec32_t *mtime, cred_t *cr, vnode_t **vpp, bool_t *created) 6059 { 6060 int error; 6061 nfsstat4 status = NFS4_OK; 6062 vattr_t va; 6063 6064 tryagain: 6065 6066 /* 6067 * The file open mode used is VWRITE. If the client needs 6068 * some other semantic, then it should do the access checking 6069 * itself. It would have been nice to have the file open mode 6070 * passed as part of the arguments. 6071 */ 6072 6073 *created = TRUE; 6074 error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL); 6075 6076 if (error) { 6077 *created = FALSE; 6078 6079 /* 6080 * If we got something other than file already exists 6081 * then just return this error. Otherwise, we got 6082 * EEXIST. If we were doing a GUARDED create, then 6083 * just return this error. Otherwise, we need to 6084 * make sure that this wasn't a duplicate of an 6085 * exclusive create request. 6086 * 6087 * The assumption is made that a non-exclusive create 6088 * request will never return EEXIST. 6089 */ 6090 6091 if (error != EEXIST || mode == GUARDED4) { 6092 status = puterrno4(error); 6093 return (status); 6094 } 6095 error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr, 6096 NULL, NULL, NULL); 6097 6098 if (error) { 6099 /* 6100 * We couldn't find the file that we thought that 6101 * we just created. So, we'll just try creating 6102 * it again. 6103 */ 6104 if (error == ENOENT) 6105 goto tryagain; 6106 6107 status = puterrno4(error); 6108 return (status); 6109 } 6110 6111 if (mode == UNCHECKED4) { 6112 /* existing object must be regular file */ 6113 if ((*vpp)->v_type != VREG) { 6114 if ((*vpp)->v_type == VDIR) 6115 status = NFS4ERR_ISDIR; 6116 else if ((*vpp)->v_type == VLNK) 6117 status = NFS4ERR_SYMLINK; 6118 else 6119 status = NFS4ERR_INVAL; 6120 VN_RELE(*vpp); 6121 return (status); 6122 } 6123 6124 return (NFS4_OK); 6125 } 6126 6127 /* Check for duplicate request */ 6128 ASSERT(mtime != 0); 6129 va.va_mask = AT_MTIME; 6130 error = VOP_GETATTR(*vpp, &va, 0, cr, NULL); 6131 if (!error) { 6132 /* We found the file */ 6133 if (va.va_mtime.tv_sec != mtime->tv_sec || 6134 va.va_mtime.tv_nsec != mtime->tv_nsec) { 6135 /* but its not our creation */ 6136 VN_RELE(*vpp); 6137 return (NFS4ERR_EXIST); 6138 } 6139 *created = TRUE; /* retrans of create == created */ 6140 return (NFS4_OK); 6141 } 6142 VN_RELE(*vpp); 6143 return (NFS4ERR_EXIST); 6144 } 6145 6146 return (NFS4_OK); 6147 } 6148 6149 static nfsstat4 6150 check_open_access(uint32_t access, 6151 struct compound_state *cs, struct svc_req *req) 6152 { 6153 int error; 6154 vnode_t *vp; 6155 bool_t readonly; 6156 cred_t *cr = cs->cr; 6157 6158 /* For now we don't allow mandatory locking as per V2/V3 */ 6159 if (cs->access == CS_ACCESS_DENIED || cs->mandlock) { 6160 return (NFS4ERR_ACCESS); 6161 } 6162 6163 vp = cs->vp; 6164 ASSERT(cr != NULL && vp->v_type == VREG); 6165 6166 /* 6167 * If the file system is exported read only and we are trying 6168 * to open for write, then return NFS4ERR_ROFS 6169 */ 6170 6171 readonly = rdonly4(cs->exi, cs->vp, req); 6172 6173 if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly) 6174 return (NFS4ERR_ROFS); 6175 6176 if (access & OPEN4_SHARE_ACCESS_READ) { 6177 if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) && 6178 (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) { 6179 return (NFS4ERR_ACCESS); 6180 } 6181 } 6182 6183 if (access & OPEN4_SHARE_ACCESS_WRITE) { 6184 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL); 6185 if (error) 6186 return (NFS4ERR_ACCESS); 6187 } 6188 6189 return (NFS4_OK); 6190 } 6191 6192 static nfsstat4 6193 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs, 6194 change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid) 6195 { 6196 struct nfs4_svgetit_arg sarg; 6197 struct nfs4_ntov_table ntov; 6198 6199 bool_t ntov_table_init = FALSE; 6200 struct statvfs64 sb; 6201 nfsstat4 status; 6202 vnode_t *vp; 6203 vattr_t bva, ava, iva, cva, *vap; 6204 vnode_t *dvp; 6205 timespec32_t *mtime; 6206 char *nm = NULL; 6207 uint_t buflen; 6208 bool_t created; 6209 bool_t setsize = FALSE; 6210 len_t reqsize; 6211 int error; 6212 bool_t trunc; 6213 caller_context_t ct; 6214 component4 *component; 6215 bslabel_t *clabel; 6216 struct sockaddr *ca; 6217 char *name = NULL; 6218 6219 sarg.sbp = &sb; 6220 6221 dvp = cs->vp; 6222 6223 /* Check if the file system is read only */ 6224 if (rdonly4(cs->exi, dvp, req)) 6225 return (NFS4ERR_ROFS); 6226 6227 /* check the label of including directory */ 6228 if (is_system_labeled()) { 6229 ASSERT(req->rq_label != NULL); 6230 clabel = req->rq_label; 6231 DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *, 6232 "got client label from request(1)", 6233 struct svc_req *, req); 6234 if (!blequal(&l_admin_low->tsl_label, clabel)) { 6235 if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK, 6236 cs->exi)) { 6237 return (NFS4ERR_ACCESS); 6238 } 6239 } 6240 } 6241 6242 /* 6243 * Get the last component of path name in nm. cs will reference 6244 * the including directory on success. 6245 */ 6246 component = &args->open_claim4_u.file; 6247 if (!utf8_dir_verify(component)) 6248 return (NFS4ERR_INVAL); 6249 6250 nm = utf8_to_fn(component, &buflen, NULL); 6251 6252 if (nm == NULL) 6253 return (NFS4ERR_RESOURCE); 6254 6255 if (buflen > MAXNAMELEN) { 6256 kmem_free(nm, buflen); 6257 return (NFS4ERR_NAMETOOLONG); 6258 } 6259 6260 bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ; 6261 error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL); 6262 if (error) { 6263 kmem_free(nm, buflen); 6264 return (puterrno4(error)); 6265 } 6266 6267 if (bva.va_type != VDIR) { 6268 kmem_free(nm, buflen); 6269 return (NFS4ERR_NOTDIR); 6270 } 6271 6272 NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime) 6273 6274 switch (args->mode) { 6275 case GUARDED4: 6276 /*FALLTHROUGH*/ 6277 case UNCHECKED4: 6278 nfs4_ntov_table_init(&ntov); 6279 ntov_table_init = TRUE; 6280 6281 *attrset = 0; 6282 status = do_rfs4_set_attrs(attrset, 6283 &args->createhow4_u.createattrs, 6284 cs, &sarg, &ntov, NFS4ATTR_SETIT); 6285 6286 if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) && 6287 sarg.vap->va_type != VREG) { 6288 if (sarg.vap->va_type == VDIR) 6289 status = NFS4ERR_ISDIR; 6290 else if (sarg.vap->va_type == VLNK) 6291 status = NFS4ERR_SYMLINK; 6292 else 6293 status = NFS4ERR_INVAL; 6294 } 6295 6296 if (status != NFS4_OK) { 6297 kmem_free(nm, buflen); 6298 nfs4_ntov_table_free(&ntov, &sarg); 6299 *attrset = 0; 6300 return (status); 6301 } 6302 6303 vap = sarg.vap; 6304 vap->va_type = VREG; 6305 vap->va_mask |= AT_TYPE; 6306 6307 if ((vap->va_mask & AT_MODE) == 0) { 6308 vap->va_mask |= AT_MODE; 6309 vap->va_mode = (mode_t)0600; 6310 } 6311 6312 if (vap->va_mask & AT_SIZE) { 6313 6314 /* Disallow create with a non-zero size */ 6315 6316 if ((reqsize = sarg.vap->va_size) != 0) { 6317 kmem_free(nm, buflen); 6318 nfs4_ntov_table_free(&ntov, &sarg); 6319 *attrset = 0; 6320 return (NFS4ERR_INVAL); 6321 } 6322 setsize = TRUE; 6323 } 6324 break; 6325 6326 case EXCLUSIVE4: 6327 /* prohibit EXCL create of named attributes */ 6328 if (dvp->v_flag & V_XATTRDIR) { 6329 kmem_free(nm, buflen); 6330 *attrset = 0; 6331 return (NFS4ERR_INVAL); 6332 } 6333 6334 cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE; 6335 cva.va_type = VREG; 6336 /* 6337 * Ensure no time overflows. Assumes underlying 6338 * filesystem supports at least 32 bits. 6339 * Truncate nsec to usec resolution to allow valid 6340 * compares even if the underlying filesystem truncates. 6341 */ 6342 mtime = (timespec32_t *)&args->createhow4_u.createverf; 6343 cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX; 6344 cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000; 6345 cva.va_mode = (mode_t)0; 6346 vap = &cva; 6347 break; 6348 } 6349 6350 /* If necessary, convert to UTF-8 for illbehaved clients */ 6351 6352 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf; 6353 name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND, 6354 MAXPATHLEN + 1); 6355 6356 if (name == NULL) { 6357 kmem_free(nm, buflen); 6358 return (NFS4ERR_SERVERFAULT); 6359 } 6360 6361 status = create_vnode(dvp, name, vap, args->mode, mtime, 6362 cs->cr, &vp, &created); 6363 if (nm != name) 6364 kmem_free(name, MAXPATHLEN + 1); 6365 kmem_free(nm, buflen); 6366 6367 if (status != NFS4_OK) { 6368 if (ntov_table_init) 6369 nfs4_ntov_table_free(&ntov, &sarg); 6370 *attrset = 0; 6371 return (status); 6372 } 6373 6374 trunc = (setsize && !created); 6375 6376 if (args->mode != EXCLUSIVE4) { 6377 bitmap4 createmask = args->createhow4_u.createattrs.attrmask; 6378 6379 /* 6380 * True verification that object was created with correct 6381 * attrs is impossible. The attrs could have been changed 6382 * immediately after object creation. If attributes did 6383 * not verify, the only recourse for the server is to 6384 * destroy the object. Maybe if some attrs (like gid) 6385 * are set incorrectly, the object should be destroyed; 6386 * however, seems bad as a default policy. Do we really 6387 * want to destroy an object over one of the times not 6388 * verifying correctly? For these reasons, the server 6389 * currently sets bits in attrset for createattrs 6390 * that were set; however, no verification is done. 6391 * 6392 * vmask_to_nmask accounts for vattr bits set on create 6393 * [do_rfs4_set_attrs() only sets resp bits for 6394 * non-vattr/vfs bits.] 6395 * Mask off any bits we set by default so as not to return 6396 * more attrset bits than were requested in createattrs 6397 */ 6398 if (created) { 6399 nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset); 6400 *attrset &= createmask; 6401 } else { 6402 /* 6403 * We did not create the vnode (we tried but it 6404 * already existed). In this case, the only createattr 6405 * that the spec allows the server to set is size, 6406 * and even then, it can only be set if it is 0. 6407 */ 6408 *attrset = 0; 6409 if (trunc) 6410 *attrset = FATTR4_SIZE_MASK; 6411 } 6412 } 6413 if (ntov_table_init) 6414 nfs4_ntov_table_free(&ntov, &sarg); 6415 6416 /* 6417 * Get the initial "after" sequence number, if it fails, 6418 * set to zero, time to before. 6419 */ 6420 iva.va_mask = AT_CTIME|AT_SEQ; 6421 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) { 6422 iva.va_seq = 0; 6423 iva.va_ctime = bva.va_ctime; 6424 } 6425 6426 /* 6427 * create_vnode attempts to create the file exclusive, 6428 * if it already exists the VOP_CREATE will fail and 6429 * may not increase va_seq. It is atomic if 6430 * we haven't changed the directory, but if it has changed 6431 * we don't know what changed it. 6432 */ 6433 if (!created) { 6434 if (bva.va_seq && iva.va_seq && 6435 bva.va_seq == iva.va_seq) 6436 cinfo->atomic = TRUE; 6437 else 6438 cinfo->atomic = FALSE; 6439 NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime); 6440 } else { 6441 /* 6442 * The entry was created, we need to sync the 6443 * directory metadata. 6444 */ 6445 (void) VOP_FSYNC(dvp, 0, cs->cr, NULL); 6446 6447 /* 6448 * Get "after" change value, if it fails, simply return the 6449 * before value. 6450 */ 6451 ava.va_mask = AT_CTIME|AT_SEQ; 6452 if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) { 6453 ava.va_ctime = bva.va_ctime; 6454 ava.va_seq = 0; 6455 } 6456 6457 NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime); 6458 6459 /* 6460 * The cinfo->atomic = TRUE only if we have 6461 * non-zero va_seq's, and it has incremented by exactly one 6462 * during the create_vnode and it didn't 6463 * change during the VOP_FSYNC. 6464 */ 6465 if (bva.va_seq && iva.va_seq && ava.va_seq && 6466 iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq) 6467 cinfo->atomic = TRUE; 6468 else 6469 cinfo->atomic = FALSE; 6470 } 6471 6472 /* Check for mandatory locking and that the size gets set. */ 6473 cva.va_mask = AT_MODE; 6474 if (setsize) 6475 cva.va_mask |= AT_SIZE; 6476 6477 /* Assume the worst */ 6478 cs->mandlock = TRUE; 6479 6480 if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) { 6481 cs->mandlock = MANDLOCK(cs->vp, cva.va_mode); 6482 6483 /* 6484 * Truncate the file if necessary; this would be 6485 * the case for create over an existing file. 6486 */ 6487 6488 if (trunc) { 6489 int in_crit = 0; 6490 rfs4_file_t *fp; 6491 bool_t create = FALSE; 6492 6493 /* 6494 * We are writing over an existing file. 6495 * Check to see if we need to recall a delegation. 6496 */ 6497 rfs4_hold_deleg_policy(); 6498 if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) { 6499 if (rfs4_check_delegated_byfp(FWRITE, fp, 6500 (reqsize == 0), FALSE, FALSE, &clientid)) { 6501 rfs4_file_rele(fp); 6502 rfs4_rele_deleg_policy(); 6503 VN_RELE(vp); 6504 *attrset = 0; 6505 return (NFS4ERR_DELAY); 6506 } 6507 rfs4_file_rele(fp); 6508 } 6509 rfs4_rele_deleg_policy(); 6510 6511 if (nbl_need_check(vp)) { 6512 in_crit = 1; 6513 6514 ASSERT(reqsize == 0); 6515 6516 nbl_start_crit(vp, RW_READER); 6517 if (nbl_conflict(vp, NBL_WRITE, 0, 6518 cva.va_size, 0, NULL)) { 6519 in_crit = 0; 6520 nbl_end_crit(vp); 6521 VN_RELE(vp); 6522 *attrset = 0; 6523 return (NFS4ERR_ACCESS); 6524 } 6525 } 6526 ct.cc_sysid = 0; 6527 ct.cc_pid = 0; 6528 ct.cc_caller_id = nfs4_srv_caller_id; 6529 ct.cc_flags = CC_DONTBLOCK; 6530 6531 cva.va_mask = AT_SIZE; 6532 cva.va_size = reqsize; 6533 (void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct); 6534 if (in_crit) 6535 nbl_end_crit(vp); 6536 } 6537 } 6538 6539 error = makefh4(&cs->fh, vp, cs->exi); 6540 6541 /* 6542 * Force modified data and metadata out to stable storage. 6543 */ 6544 (void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL); 6545 6546 if (error) { 6547 VN_RELE(vp); 6548 *attrset = 0; 6549 return (puterrno4(error)); 6550 } 6551 6552 /* if parent dir is attrdir, set namedattr fh flag */ 6553 if (dvp->v_flag & V_XATTRDIR) 6554 set_fh4_flag(&cs->fh, FH4_NAMEDATTR); 6555 6556 if (cs->vp) 6557 VN_RELE(cs->vp); 6558 6559 cs->vp = vp; 6560 6561 /* 6562 * if we did not create the file, we will need to check 6563 * the access bits on the file 6564 */ 6565 6566 if (!created) { 6567 if (setsize) 6568 args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 6569 status = check_open_access(args->share_access, cs, req); 6570 if (status != NFS4_OK) 6571 *attrset = 0; 6572 } 6573 return (status); 6574 } 6575 6576 /*ARGSUSED*/ 6577 static void 6578 rfs4_do_open(struct compound_state *cs, struct svc_req *req, 6579 rfs4_openowner_t *oo, delegreq_t deleg, 6580 uint32_t access, uint32_t deny, 6581 OPEN4res *resp, int deleg_cur) 6582 { 6583 /* XXX Currently not using req */ 6584 rfs4_state_t *state; 6585 rfs4_file_t *file; 6586 bool_t screate = TRUE; 6587 bool_t fcreate = TRUE; 6588 uint32_t amodes; 6589 uint32_t dmodes; 6590 rfs4_deleg_state_t *dsp; 6591 struct shrlock shr; 6592 struct shr_locowner shr_loco; 6593 sysid_t sysid; 6594 nfsstat4 status; 6595 caller_context_t ct; 6596 int fflags = 0; 6597 int recall = 0; 6598 int err; 6599 int cmd; 6600 6601 /* get the file struct and hold a lock on it during initial open */ 6602 file = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate); 6603 if (file == NULL) { 6604 NFS4_DEBUG(rfs4_debug, 6605 (CE_NOTE, "rfs4_do_open: can't find file")); 6606 resp->status = NFS4ERR_SERVERFAULT; 6607 return; 6608 } 6609 6610 state = rfs4_findstate_by_owner_file(oo, file, &screate); 6611 if (state == NULL) { 6612 NFS4_DEBUG(rfs4_debug, 6613 (CE_NOTE, "rfs4_do_open: can't find state")); 6614 resp->status = NFS4ERR_RESOURCE; 6615 /* No need to keep any reference */ 6616 rfs4_file_rele_withunlock(file); 6617 return; 6618 } 6619 6620 /* try to get the sysid before continuing */ 6621 if ((status = rfs4_client_sysid(oo->client, &sysid)) != NFS4_OK) { 6622 resp->status = status; 6623 rfs4_file_rele(file); 6624 /* Not a fully formed open; "close" it */ 6625 if (screate == TRUE) 6626 rfs4_state_close(state, FALSE, FALSE, cs->cr); 6627 rfs4_state_rele(state); 6628 return; 6629 } 6630 6631 /* Calculate the fflags for this OPEN. */ 6632 if (access & OPEN4_SHARE_ACCESS_READ) 6633 fflags |= FREAD; 6634 if (access & OPEN4_SHARE_ACCESS_WRITE) 6635 fflags |= FWRITE; 6636 6637 /* 6638 * Calculate the new deny and access mode that this open is adding to 6639 * the file for this open owner; 6640 */ 6641 dmodes = (deny & ~state->share_deny); 6642 amodes = (access & ~state->share_access); 6643 6644 /* 6645 * Check to see the client has already sent an open for this 6646 * open owner on this file with the same share/deny modes. 6647 * If so, we don't need to check for a conflict and we don't 6648 * need to add another shrlock. If not, then we need to 6649 * check for conflicts in deny and access before checking for 6650 * conflicts in delegation. We don't want to recall a 6651 * delegation based on an open that will eventually fail based 6652 * on shares modes. 6653 */ 6654 6655 if (dmodes || amodes) { 6656 shr.s_access = (short)access; 6657 shr.s_deny = (short)deny; 6658 shr.s_pid = rfs4_dbe_getid(oo->dbe); 6659 shr.s_sysid = sysid; 6660 shr_loco.sl_pid = shr.s_pid; 6661 shr_loco.sl_id = shr.s_sysid; 6662 shr.s_owner = (caddr_t)&shr_loco; 6663 shr.s_own_len = sizeof (shr_loco); 6664 6665 cmd = nbl_need_check(cs->vp) ? F_SHARE_NBMAND : F_SHARE; 6666 if ((err = vop_shrlock(cs->vp, cmd, &shr, fflags)) != 0) { 6667 6668 resp->status = err == EAGAIN ? 6669 NFS4ERR_SHARE_DENIED : puterrno4(err); 6670 6671 rfs4_file_rele(file); 6672 /* Not a fully formed open; "close" it */ 6673 if (screate == TRUE) 6674 rfs4_state_close(state, FALSE, FALSE, cs->cr); 6675 rfs4_state_rele(state); 6676 return; 6677 } 6678 } 6679 6680 rfs4_dbe_lock(state->dbe); 6681 rfs4_dbe_lock(file->dbe); 6682 6683 /* 6684 * Check to see if this file is delegated and if so, if a 6685 * recall needs to be done. 6686 */ 6687 if (rfs4_check_recall(state, access)) { 6688 rfs4_dbe_unlock(file->dbe); 6689 rfs4_dbe_unlock(state->dbe); 6690 rfs4_recall_deleg(file, FALSE, state->owner->client); 6691 delay(NFS4_DELEGATION_CONFLICT_DELAY); 6692 rfs4_dbe_lock(state->dbe); 6693 rfs4_dbe_lock(file->dbe); 6694 /* Let's see if the delegation was returned */ 6695 if (rfs4_check_recall(state, access)) { 6696 rfs4_dbe_unlock(file->dbe); 6697 rfs4_dbe_unlock(state->dbe); 6698 rfs4_file_rele(file); 6699 rfs4_update_lease(state->owner->client); 6700 (void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags); 6701 /* Not a fully formed open; "close" it */ 6702 if (screate == TRUE) 6703 rfs4_state_close(state, FALSE, FALSE, cs->cr); 6704 rfs4_state_rele(state); 6705 resp->status = NFS4ERR_DELAY; 6706 return; 6707 } 6708 } 6709 /* 6710 * the share check passed and any delegation conflict has been 6711 * taken care of, now call vop_open. 6712 * if this is the first open then call vop_open with fflags. 6713 * if not, call vn_open_upgrade with just the upgrade flags. 6714 * 6715 * if the file has been opened already, it will have the current 6716 * access mode in the state struct. if it has no share access, then 6717 * this is a new open. 6718 * 6719 * However, if this is open with CLAIM_DLEGATE_CUR, then don't 6720 * call VOP_OPEN(), just do the open upgrade. 6721 */ 6722 if (((state->share_access & OPEN4_SHARE_ACCESS_BOTH) == 0) && 6723 !deleg_cur) { 6724 ct.cc_sysid = sysid; 6725 ct.cc_pid = shr.s_pid; 6726 ct.cc_caller_id = nfs4_srv_caller_id; 6727 ct.cc_flags = CC_DONTBLOCK; 6728 err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct); 6729 if (err) { 6730 rfs4_dbe_unlock(file->dbe); 6731 rfs4_dbe_unlock(state->dbe); 6732 rfs4_file_rele(file); 6733 (void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags); 6734 /* Not a fully formed open; "close" it */ 6735 if (screate == TRUE) 6736 rfs4_state_close(state, FALSE, FALSE, cs->cr); 6737 rfs4_state_rele(state); 6738 /* check if a monitor detected a delegation conflict */ 6739 if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) 6740 resp->status = NFS4ERR_DELAY; 6741 else 6742 resp->status = NFS4ERR_SERVERFAULT; 6743 return; 6744 } 6745 } else { /* open upgrade */ 6746 /* 6747 * calculate the fflags for the new mode that is being added 6748 * by this upgrade. 6749 */ 6750 fflags = 0; 6751 if (amodes & OPEN4_SHARE_ACCESS_READ) 6752 fflags |= FREAD; 6753 if (amodes & OPEN4_SHARE_ACCESS_WRITE) 6754 fflags |= FWRITE; 6755 vn_open_upgrade(cs->vp, fflags); 6756 } 6757 6758 if (dmodes & OPEN4_SHARE_DENY_READ) 6759 file->deny_read++; 6760 if (dmodes & OPEN4_SHARE_DENY_WRITE) 6761 file->deny_write++; 6762 file->share_deny |= deny; 6763 state->share_deny |= deny; 6764 6765 if (amodes & OPEN4_SHARE_ACCESS_READ) 6766 file->access_read++; 6767 if (amodes & OPEN4_SHARE_ACCESS_WRITE) 6768 file->access_write++; 6769 file->share_access |= access; 6770 state->share_access |= access; 6771 6772 /* 6773 * Check for delegation here. if the deleg argument is not 6774 * DELEG_ANY, then this is a reclaim from a client and 6775 * we must honor the delegation requested. If necessary we can 6776 * set the recall flag. 6777 */ 6778 6779 dsp = rfs4_grant_delegation(deleg, state, &recall); 6780 6781 cs->deleg = (file->dinfo->dtype == OPEN_DELEGATE_WRITE); 6782 6783 next_stateid(&state->stateid); 6784 6785 resp->stateid = state->stateid.stateid; 6786 6787 rfs4_dbe_unlock(file->dbe); 6788 rfs4_dbe_unlock(state->dbe); 6789 6790 if (dsp) { 6791 rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall); 6792 rfs4_deleg_state_rele(dsp); 6793 } 6794 6795 rfs4_file_rele(file); 6796 rfs4_state_rele(state); 6797 6798 resp->status = NFS4_OK; 6799 } 6800 6801 /*ARGSUSED*/ 6802 static void 6803 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req, 6804 OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp) 6805 { 6806 change_info4 *cinfo = &resp->cinfo; 6807 bitmap4 *attrset = &resp->attrset; 6808 6809 if (args->opentype == OPEN4_NOCREATE) 6810 resp->status = rfs4_lookupfile(&args->open_claim4_u.file, 6811 req, cs, args->share_access, cinfo); 6812 else { 6813 /* inhibit delegation grants during exclusive create */ 6814 6815 if (args->mode == EXCLUSIVE4) 6816 rfs4_disable_delegation(); 6817 6818 resp->status = rfs4_createfile(args, req, cs, cinfo, attrset, 6819 oo->client->clientid); 6820 } 6821 6822 if (resp->status == NFS4_OK) { 6823 6824 /* cs->vp cs->fh now reference the desired file */ 6825 6826 rfs4_do_open(cs, req, oo, DELEG_ANY, args->share_access, 6827 args->share_deny, resp, 0); 6828 6829 /* 6830 * If rfs4_createfile set attrset, we must 6831 * clear this attrset before the response is copied. 6832 */ 6833 if (resp->status != NFS4_OK && resp->attrset) { 6834 resp->attrset = 0; 6835 } 6836 } 6837 else 6838 *cs->statusp = resp->status; 6839 6840 if (args->mode == EXCLUSIVE4) 6841 rfs4_enable_delegation(); 6842 } 6843 6844 /*ARGSUSED*/ 6845 static void 6846 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req, 6847 OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp) 6848 { 6849 change_info4 *cinfo = &resp->cinfo; 6850 vattr_t va; 6851 vtype_t v_type = cs->vp->v_type; 6852 int error = 0; 6853 6854 /* Verify that we have a regular file */ 6855 if (v_type != VREG) { 6856 if (v_type == VDIR) 6857 resp->status = NFS4ERR_ISDIR; 6858 else if (v_type == VLNK) 6859 resp->status = NFS4ERR_SYMLINK; 6860 else 6861 resp->status = NFS4ERR_INVAL; 6862 return; 6863 } 6864 6865 va.va_mask = AT_MODE|AT_UID; 6866 error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL); 6867 if (error) { 6868 resp->status = puterrno4(error); 6869 return; 6870 } 6871 6872 cs->mandlock = MANDLOCK(cs->vp, va.va_mode); 6873 6874 /* 6875 * Check if we have access to the file, Note the the file 6876 * could have originally been open UNCHECKED or GUARDED 6877 * with mode bits that will now fail, but there is nothing 6878 * we can really do about that except in the case that the 6879 * owner of the file is the one requesting the open. 6880 */ 6881 if (crgetuid(cs->cr) != va.va_uid) { 6882 resp->status = check_open_access(args->share_access, cs, req); 6883 if (resp->status != NFS4_OK) { 6884 return; 6885 } 6886 } 6887 6888 /* 6889 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero 6890 */ 6891 cinfo->before = 0; 6892 cinfo->after = 0; 6893 cinfo->atomic = FALSE; 6894 6895 rfs4_do_open(cs, req, oo, 6896 NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type), 6897 args->share_access, args->share_deny, resp, 0); 6898 } 6899 6900 static void 6901 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req, 6902 OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp) 6903 { 6904 int error; 6905 nfsstat4 status; 6906 stateid4 stateid = 6907 args->open_claim4_u.delegate_cur_info.delegate_stateid; 6908 rfs4_deleg_state_t *dsp; 6909 6910 /* 6911 * Find the state info from the stateid and confirm that the 6912 * file is delegated. If the state openowner is the same as 6913 * the supplied openowner we're done. If not, get the file 6914 * info from the found state info. Use that file info to 6915 * create the state for this lock owner. Note solaris doen't 6916 * really need the pathname to find the file. We may want to 6917 * lookup the pathname and make sure that the vp exist and 6918 * matches the vp in the file structure. However it is 6919 * possible that the pathname nolonger exists (local process 6920 * unlinks the file), so this may not be that useful. 6921 */ 6922 6923 status = rfs4_get_deleg_state(&stateid, &dsp); 6924 if (status != NFS4_OK) { 6925 resp->status = status; 6926 return; 6927 } 6928 6929 ASSERT(dsp->finfo->dinfo->dtype != OPEN_DELEGATE_NONE); 6930 6931 /* 6932 * New lock owner, create state. Since this was probably called 6933 * in response to a CB_RECALL we set deleg to DELEG_NONE 6934 */ 6935 6936 ASSERT(cs->vp != NULL); 6937 VN_RELE(cs->vp); 6938 VN_HOLD(dsp->finfo->vp); 6939 cs->vp = dsp->finfo->vp; 6940 6941 if (error = makefh4(&cs->fh, cs->vp, cs->exi)) { 6942 rfs4_deleg_state_rele(dsp); 6943 *cs->statusp = resp->status = puterrno4(error); 6944 return; 6945 } 6946 6947 /* Mark progress for delegation returns */ 6948 dsp->finfo->dinfo->time_lastwrite = gethrestime_sec(); 6949 rfs4_deleg_state_rele(dsp); 6950 rfs4_do_open(cs, req, oo, DELEG_NONE, 6951 args->share_access, args->share_deny, resp, 1); 6952 } 6953 6954 /*ARGSUSED*/ 6955 static void 6956 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req, 6957 OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp) 6958 { 6959 /* 6960 * Lookup the pathname, it must already exist since this file 6961 * was delegated. 6962 * 6963 * Find the file and state info for this vp and open owner pair. 6964 * check that they are in fact delegated. 6965 * check that the state access and deny modes are the same. 6966 * 6967 * Return the delgation possibly seting the recall flag. 6968 */ 6969 rfs4_file_t *file; 6970 rfs4_state_t *state; 6971 bool_t create = FALSE; 6972 bool_t dcreate = FALSE; 6973 rfs4_deleg_state_t *dsp; 6974 nfsace4 *ace; 6975 6976 6977 /* Note we ignore oflags */ 6978 resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev, 6979 req, cs, args->share_access, &resp->cinfo); 6980 6981 if (resp->status != NFS4_OK) { 6982 return; 6983 } 6984 6985 /* get the file struct and hold a lock on it during initial open */ 6986 file = rfs4_findfile_withlock(cs->vp, NULL, &create); 6987 if (file == NULL) { 6988 NFS4_DEBUG(rfs4_debug, 6989 (CE_NOTE, "rfs4_do_opendelprev: can't find file")); 6990 resp->status = NFS4ERR_SERVERFAULT; 6991 return; 6992 } 6993 6994 state = rfs4_findstate_by_owner_file(oo, file, &create); 6995 if (state == NULL) { 6996 NFS4_DEBUG(rfs4_debug, 6997 (CE_NOTE, "rfs4_do_opendelprev: can't find state")); 6998 resp->status = NFS4ERR_SERVERFAULT; 6999 rfs4_file_rele_withunlock(file); 7000 return; 7001 } 7002 7003 rfs4_dbe_lock(state->dbe); 7004 rfs4_dbe_lock(file->dbe); 7005 if (args->share_access != state->share_access || 7006 args->share_deny != state->share_deny || 7007 state->finfo->dinfo->dtype == OPEN_DELEGATE_NONE) { 7008 NFS4_DEBUG(rfs4_debug, 7009 (CE_NOTE, "rfs4_do_opendelprev: state mixup")); 7010 rfs4_dbe_unlock(file->dbe); 7011 rfs4_dbe_unlock(state->dbe); 7012 rfs4_file_rele(file); 7013 rfs4_state_rele(state); 7014 resp->status = NFS4ERR_SERVERFAULT; 7015 return; 7016 } 7017 rfs4_dbe_unlock(file->dbe); 7018 rfs4_dbe_unlock(state->dbe); 7019 7020 dsp = rfs4_finddeleg(state, &dcreate); 7021 if (dsp == NULL) { 7022 rfs4_state_rele(state); 7023 rfs4_file_rele(file); 7024 resp->status = NFS4ERR_SERVERFAULT; 7025 return; 7026 } 7027 7028 next_stateid(&state->stateid); 7029 7030 resp->stateid = state->stateid.stateid; 7031 7032 resp->delegation.delegation_type = dsp->dtype; 7033 7034 if (dsp->dtype == OPEN_DELEGATE_READ) { 7035 open_read_delegation4 *rv = 7036 &resp->delegation.open_delegation4_u.read; 7037 7038 rv->stateid = dsp->delegid.stateid; 7039 rv->recall = FALSE; /* no policy in place to set to TRUE */ 7040 ace = &rv->permissions; 7041 } else { 7042 open_write_delegation4 *rv = 7043 &resp->delegation.open_delegation4_u.write; 7044 7045 rv->stateid = dsp->delegid.stateid; 7046 rv->recall = FALSE; /* no policy in place to set to TRUE */ 7047 ace = &rv->permissions; 7048 rv->space_limit.limitby = NFS_LIMIT_SIZE; 7049 rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX; 7050 } 7051 7052 /* XXX For now */ 7053 ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE; 7054 ace->flag = 0; 7055 ace->access_mask = 0; 7056 ace->who.utf8string_len = 0; 7057 ace->who.utf8string_val = 0; 7058 7059 rfs4_deleg_state_rele(dsp); 7060 rfs4_state_rele(state); 7061 rfs4_file_rele(file); 7062 } 7063 7064 typedef enum { 7065 NFS4_CHKSEQ_OKAY = 0, 7066 NFS4_CHKSEQ_REPLAY = 1, 7067 NFS4_CHKSEQ_BAD = 2 7068 } rfs4_chkseq_t; 7069 7070 /* 7071 * Generic function for sequence number checks. 7072 */ 7073 static rfs4_chkseq_t 7074 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop, 7075 seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres) 7076 { 7077 /* Same sequence ids and matching operations? */ 7078 if (seqid == rqst_seq && resop->resop == lastop->resop) { 7079 if (copyres == TRUE) { 7080 rfs4_free_reply(resop); 7081 rfs4_copy_reply(resop, lastop); 7082 } 7083 NFS4_DEBUG(rfs4_debug, (CE_NOTE, 7084 "Replayed SEQID %d\n", seqid)); 7085 return (NFS4_CHKSEQ_REPLAY); 7086 } 7087 7088 /* If the incoming sequence is not the next expected then it is bad */ 7089 if (rqst_seq != seqid + 1) { 7090 if (rqst_seq == seqid) { 7091 NFS4_DEBUG(rfs4_debug, 7092 (CE_NOTE, "BAD SEQID: Replayed sequence id " 7093 "but last op was %d current op is %d\n", 7094 lastop->resop, resop->resop)); 7095 return (NFS4_CHKSEQ_BAD); 7096 } 7097 NFS4_DEBUG(rfs4_debug, 7098 (CE_NOTE, "BAD SEQID: got %u expecting %u\n", 7099 rqst_seq, seqid)); 7100 return (NFS4_CHKSEQ_BAD); 7101 } 7102 7103 /* Everything okay -- next expected */ 7104 return (NFS4_CHKSEQ_OKAY); 7105 } 7106 7107 7108 static rfs4_chkseq_t 7109 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop) 7110 { 7111 rfs4_chkseq_t rc; 7112 7113 rfs4_dbe_lock(op->dbe); 7114 rc = rfs4_check_seqid(op->open_seqid, op->reply, seqid, resop, TRUE); 7115 rfs4_dbe_unlock(op->dbe); 7116 7117 if (rc == NFS4_CHKSEQ_OKAY) 7118 rfs4_update_lease(op->client); 7119 7120 return (rc); 7121 } 7122 7123 static rfs4_chkseq_t 7124 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op, 7125 nfs_resop4 *resop) 7126 { 7127 rfs4_chkseq_t rc; 7128 7129 rfs4_dbe_lock(op->dbe); 7130 rc = rfs4_check_seqid(op->open_seqid, op->reply, 7131 olo_seqid, resop, FALSE); 7132 rfs4_dbe_unlock(op->dbe); 7133 7134 return (rc); 7135 } 7136 7137 static rfs4_chkseq_t 7138 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lp, nfs_resop4 *resop) 7139 { 7140 rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY; 7141 7142 rfs4_dbe_lock(lp->dbe); 7143 if (!lp->skip_seqid_check) 7144 rc = rfs4_check_seqid(lp->seqid, lp->reply, seqid, resop, TRUE); 7145 rfs4_dbe_unlock(lp->dbe); 7146 7147 return (rc); 7148 } 7149 7150 static void 7151 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop, 7152 struct svc_req *req, struct compound_state *cs) 7153 { 7154 OPEN4args *args = &argop->nfs_argop4_u.opopen; 7155 OPEN4res *resp = &resop->nfs_resop4_u.opopen; 7156 open_owner4 *owner = &args->owner; 7157 open_claim_type4 claim = args->claim; 7158 rfs4_client_t *cp; 7159 rfs4_openowner_t *oo; 7160 bool_t create; 7161 bool_t replay = FALSE; 7162 int can_reclaim; 7163 7164 DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs, 7165 OPEN4args *, args); 7166 7167 if (cs->vp == NULL) { 7168 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 7169 goto end; 7170 } 7171 7172 /* 7173 * Need to check clientid and lease expiration first based on 7174 * error ordering and incrementing sequence id. 7175 */ 7176 cp = rfs4_findclient_by_id(owner->clientid, FALSE); 7177 if (cp == NULL) { 7178 *cs->statusp = resp->status = 7179 rfs4_check_clientid(&owner->clientid, 0); 7180 goto end; 7181 } 7182 7183 if (rfs4_lease_expired(cp)) { 7184 rfs4_client_close(cp); 7185 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 7186 goto end; 7187 } 7188 can_reclaim = cp->can_reclaim; 7189 7190 /* 7191 * Find the open_owner for use from this point forward. Take 7192 * care in updating the sequence id based on the type of error 7193 * being returned. 7194 */ 7195 retry: 7196 create = TRUE; 7197 oo = rfs4_findopenowner(owner, &create, args->seqid); 7198 if (oo == NULL) { 7199 *cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID; 7200 rfs4_client_rele(cp); 7201 goto end; 7202 } 7203 7204 /* Hold off access to the sequence space while the open is done */ 7205 rfs4_sw_enter(&oo->oo_sw); 7206 7207 /* 7208 * If the open_owner existed before at the server, then check 7209 * the sequence id. 7210 */ 7211 if (!create && !oo->postpone_confirm) { 7212 switch (rfs4_check_open_seqid(args->seqid, oo, resop)) { 7213 case NFS4_CHKSEQ_BAD: 7214 if ((args->seqid > oo->open_seqid) && 7215 oo->need_confirm) { 7216 rfs4_free_opens(oo, TRUE, FALSE); 7217 rfs4_sw_exit(&oo->oo_sw); 7218 rfs4_openowner_rele(oo); 7219 goto retry; 7220 } 7221 resp->status = NFS4ERR_BAD_SEQID; 7222 goto out; 7223 case NFS4_CHKSEQ_REPLAY: /* replay of previous request */ 7224 replay = TRUE; 7225 goto out; 7226 default: 7227 break; 7228 } 7229 7230 /* 7231 * Sequence was ok and open owner exists 7232 * check to see if we have yet to see an 7233 * open_confirm. 7234 */ 7235 if (oo->need_confirm) { 7236 rfs4_free_opens(oo, TRUE, FALSE); 7237 rfs4_sw_exit(&oo->oo_sw); 7238 rfs4_openowner_rele(oo); 7239 goto retry; 7240 } 7241 } 7242 /* Grace only applies to regular-type OPENs */ 7243 if (rfs4_clnt_in_grace(cp) && 7244 (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) { 7245 *cs->statusp = resp->status = NFS4ERR_GRACE; 7246 goto out; 7247 } 7248 7249 /* 7250 * If previous state at the server existed then can_reclaim 7251 * will be set. If not reply NFS4ERR_NO_GRACE to the 7252 * client. 7253 */ 7254 if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) { 7255 *cs->statusp = resp->status = NFS4ERR_NO_GRACE; 7256 goto out; 7257 } 7258 7259 7260 /* 7261 * Reject the open if the client has missed the grace period 7262 */ 7263 if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) { 7264 *cs->statusp = resp->status = NFS4ERR_NO_GRACE; 7265 goto out; 7266 } 7267 7268 /* Couple of up-front bookkeeping items */ 7269 if (oo->need_confirm) { 7270 /* 7271 * If this is a reclaim OPEN then we should not ask 7272 * for a confirmation of the open_owner per the 7273 * protocol specification. 7274 */ 7275 if (claim == CLAIM_PREVIOUS) 7276 oo->need_confirm = FALSE; 7277 else 7278 resp->rflags |= OPEN4_RESULT_CONFIRM; 7279 } 7280 resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX; 7281 7282 /* 7283 * If there is an unshared filesystem mounted on this vnode, 7284 * do not allow to open/create in this directory. 7285 */ 7286 if (vn_ismntpt(cs->vp)) { 7287 *cs->statusp = resp->status = NFS4ERR_ACCESS; 7288 goto out; 7289 } 7290 7291 /* 7292 * access must READ, WRITE, or BOTH. No access is invalid. 7293 * deny can be READ, WRITE, BOTH, or NONE. 7294 * bits not defined for access/deny are invalid. 7295 */ 7296 if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) || 7297 (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) || 7298 (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) { 7299 *cs->statusp = resp->status = NFS4ERR_INVAL; 7300 goto out; 7301 } 7302 7303 7304 /* 7305 * make sure attrset is zero before response is built. 7306 */ 7307 resp->attrset = 0; 7308 7309 switch (claim) { 7310 case CLAIM_NULL: 7311 rfs4_do_opennull(cs, req, args, oo, resp); 7312 break; 7313 case CLAIM_PREVIOUS: 7314 rfs4_do_openprev(cs, req, args, oo, resp); 7315 break; 7316 case CLAIM_DELEGATE_CUR: 7317 rfs4_do_opendelcur(cs, req, args, oo, resp); 7318 break; 7319 case CLAIM_DELEGATE_PREV: 7320 rfs4_do_opendelprev(cs, req, args, oo, resp); 7321 break; 7322 default: 7323 resp->status = NFS4ERR_INVAL; 7324 break; 7325 } 7326 7327 out: 7328 rfs4_client_rele(cp); 7329 7330 /* Catch sequence id handling here to make it a little easier */ 7331 switch (resp->status) { 7332 case NFS4ERR_BADXDR: 7333 case NFS4ERR_BAD_SEQID: 7334 case NFS4ERR_BAD_STATEID: 7335 case NFS4ERR_NOFILEHANDLE: 7336 case NFS4ERR_RESOURCE: 7337 case NFS4ERR_STALE_CLIENTID: 7338 case NFS4ERR_STALE_STATEID: 7339 /* 7340 * The protocol states that if any of these errors are 7341 * being returned, the sequence id should not be 7342 * incremented. Any other return requires an 7343 * increment. 7344 */ 7345 break; 7346 default: 7347 /* Always update the lease in this case */ 7348 rfs4_update_lease(oo->client); 7349 7350 /* Regular response - copy the result */ 7351 if (!replay) 7352 rfs4_update_open_resp(oo, resop, &cs->fh); 7353 7354 /* 7355 * REPLAY case: Only if the previous response was OK 7356 * do we copy the filehandle. If not OK, no 7357 * filehandle to copy. 7358 */ 7359 if (replay == TRUE && 7360 resp->status == NFS4_OK && 7361 oo->reply_fh.nfs_fh4_val) { 7362 /* 7363 * If this is a replay, we must restore the 7364 * current filehandle/vp to that of what was 7365 * returned originally. Try our best to do 7366 * it. 7367 */ 7368 nfs_fh4_fmt_t *fh_fmtp = 7369 (nfs_fh4_fmt_t *)oo->reply_fh.nfs_fh4_val; 7370 7371 cs->exi = checkexport4(&fh_fmtp->fh4_fsid, 7372 (fid_t *)&fh_fmtp->fh4_xlen, NULL); 7373 7374 if (cs->exi == NULL) { 7375 resp->status = NFS4ERR_STALE; 7376 goto finish; 7377 } 7378 7379 VN_RELE(cs->vp); 7380 7381 cs->vp = nfs4_fhtovp(&oo->reply_fh, cs->exi, 7382 &resp->status); 7383 7384 if (cs->vp == NULL) 7385 goto finish; 7386 7387 nfs_fh4_copy(&oo->reply_fh, &cs->fh); 7388 } 7389 7390 /* 7391 * If this was a replay, no need to update the 7392 * sequence id. If the open_owner was not created on 7393 * this pass, then update. The first use of an 7394 * open_owner will not bump the sequence id. 7395 */ 7396 if (replay == FALSE && !create) 7397 rfs4_update_open_sequence(oo); 7398 /* 7399 * If the client is receiving an error and the 7400 * open_owner needs to be confirmed, there is no way 7401 * to notify the client of this fact ignoring the fact 7402 * that the server has no method of returning a 7403 * stateid to confirm. Therefore, the server needs to 7404 * mark this open_owner in a way as to avoid the 7405 * sequence id checking the next time the client uses 7406 * this open_owner. 7407 */ 7408 if (resp->status != NFS4_OK && oo->need_confirm) 7409 oo->postpone_confirm = TRUE; 7410 /* 7411 * If OK response then clear the postpone flag and 7412 * reset the sequence id to keep in sync with the 7413 * client. 7414 */ 7415 if (resp->status == NFS4_OK && oo->postpone_confirm) { 7416 oo->postpone_confirm = FALSE; 7417 oo->open_seqid = args->seqid; 7418 } 7419 break; 7420 } 7421 7422 finish: 7423 *cs->statusp = resp->status; 7424 7425 rfs4_sw_exit(&oo->oo_sw); 7426 rfs4_openowner_rele(oo); 7427 7428 end: 7429 DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs, 7430 OPEN4res *, resp); 7431 } 7432 7433 /*ARGSUSED*/ 7434 void 7435 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop, 7436 struct svc_req *req, struct compound_state *cs) 7437 { 7438 OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm; 7439 OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm; 7440 rfs4_state_t *sp; 7441 nfsstat4 status; 7442 7443 DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs, 7444 OPEN_CONFIRM4args *, args); 7445 7446 if (cs->vp == NULL) { 7447 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 7448 goto out; 7449 } 7450 7451 status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID); 7452 if (status != NFS4_OK) { 7453 *cs->statusp = resp->status = status; 7454 goto out; 7455 } 7456 7457 /* Ensure specified filehandle matches */ 7458 if (cs->vp != sp->finfo->vp) { 7459 rfs4_state_rele(sp); 7460 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 7461 goto out; 7462 } 7463 7464 /* hold off other access to open_owner while we tinker */ 7465 rfs4_sw_enter(&sp->owner->oo_sw); 7466 7467 switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) { 7468 case NFS4_CHECK_STATEID_OKAY: 7469 if (rfs4_check_open_seqid(args->seqid, sp->owner, 7470 resop) != 0) { 7471 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 7472 break; 7473 } 7474 /* 7475 * If it is the appropriate stateid and determined to 7476 * be "OKAY" then this means that the stateid does not 7477 * need to be confirmed and the client is in error for 7478 * sending an OPEN_CONFIRM. 7479 */ 7480 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 7481 break; 7482 case NFS4_CHECK_STATEID_OLD: 7483 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 7484 break; 7485 case NFS4_CHECK_STATEID_BAD: 7486 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 7487 break; 7488 case NFS4_CHECK_STATEID_EXPIRED: 7489 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 7490 break; 7491 case NFS4_CHECK_STATEID_CLOSED: 7492 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 7493 break; 7494 case NFS4_CHECK_STATEID_REPLAY: 7495 switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) { 7496 case NFS4_CHKSEQ_OKAY: 7497 /* 7498 * This is replayed stateid; if seqid matches 7499 * next expected, then client is using wrong seqid. 7500 */ 7501 /* fall through */ 7502 case NFS4_CHKSEQ_BAD: 7503 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 7504 break; 7505 case NFS4_CHKSEQ_REPLAY: 7506 /* 7507 * Note this case is the duplicate case so 7508 * resp->status is already set. 7509 */ 7510 *cs->statusp = resp->status; 7511 rfs4_update_lease(sp->owner->client); 7512 break; 7513 } 7514 break; 7515 case NFS4_CHECK_STATEID_UNCONFIRMED: 7516 if (rfs4_check_open_seqid(args->seqid, sp->owner, 7517 resop) != NFS4_CHKSEQ_OKAY) { 7518 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 7519 break; 7520 } 7521 *cs->statusp = resp->status = NFS4_OK; 7522 7523 next_stateid(&sp->stateid); 7524 resp->open_stateid = sp->stateid.stateid; 7525 sp->owner->need_confirm = FALSE; 7526 rfs4_update_lease(sp->owner->client); 7527 rfs4_update_open_sequence(sp->owner); 7528 rfs4_update_open_resp(sp->owner, resop, NULL); 7529 break; 7530 default: 7531 ASSERT(FALSE); 7532 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT; 7533 break; 7534 } 7535 rfs4_sw_exit(&sp->owner->oo_sw); 7536 rfs4_state_rele(sp); 7537 7538 out: 7539 DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs, 7540 OPEN_CONFIRM4res *, resp); 7541 } 7542 7543 /*ARGSUSED*/ 7544 void 7545 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop, 7546 struct svc_req *req, struct compound_state *cs) 7547 { 7548 OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade; 7549 OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade; 7550 uint32_t access = args->share_access; 7551 uint32_t deny = args->share_deny; 7552 nfsstat4 status; 7553 rfs4_state_t *sp; 7554 rfs4_file_t *fp; 7555 int fflags = 0; 7556 7557 DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs, 7558 OPEN_DOWNGRADE4args *, args); 7559 7560 if (cs->vp == NULL) { 7561 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 7562 goto out; 7563 } 7564 7565 status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID); 7566 if (status != NFS4_OK) { 7567 *cs->statusp = resp->status = status; 7568 goto out; 7569 } 7570 7571 /* Ensure specified filehandle matches */ 7572 if (cs->vp != sp->finfo->vp) { 7573 rfs4_state_rele(sp); 7574 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 7575 goto out; 7576 } 7577 7578 /* hold off other access to open_owner while we tinker */ 7579 rfs4_sw_enter(&sp->owner->oo_sw); 7580 7581 switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) { 7582 case NFS4_CHECK_STATEID_OKAY: 7583 if (rfs4_check_open_seqid(args->seqid, sp->owner, 7584 resop) != NFS4_CHKSEQ_OKAY) { 7585 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 7586 goto end; 7587 } 7588 break; 7589 case NFS4_CHECK_STATEID_OLD: 7590 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 7591 goto end; 7592 case NFS4_CHECK_STATEID_BAD: 7593 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 7594 goto end; 7595 case NFS4_CHECK_STATEID_EXPIRED: 7596 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 7597 goto end; 7598 case NFS4_CHECK_STATEID_CLOSED: 7599 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 7600 goto end; 7601 case NFS4_CHECK_STATEID_UNCONFIRMED: 7602 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 7603 goto end; 7604 case NFS4_CHECK_STATEID_REPLAY: 7605 /* Check the sequence id for the open owner */ 7606 switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) { 7607 case NFS4_CHKSEQ_OKAY: 7608 /* 7609 * This is replayed stateid; if seqid matches 7610 * next expected, then client is using wrong seqid. 7611 */ 7612 /* fall through */ 7613 case NFS4_CHKSEQ_BAD: 7614 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 7615 goto end; 7616 case NFS4_CHKSEQ_REPLAY: 7617 /* 7618 * Note this case is the duplicate case so 7619 * resp->status is already set. 7620 */ 7621 *cs->statusp = resp->status; 7622 rfs4_update_lease(sp->owner->client); 7623 goto end; 7624 } 7625 break; 7626 default: 7627 ASSERT(FALSE); 7628 break; 7629 } 7630 7631 rfs4_dbe_lock(sp->dbe); 7632 /* 7633 * Check that the new access modes and deny modes are valid. 7634 * Check that no invalid bits are set. 7635 */ 7636 if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) || 7637 (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) { 7638 *cs->statusp = resp->status = NFS4ERR_INVAL; 7639 rfs4_update_open_sequence(sp->owner); 7640 rfs4_dbe_unlock(sp->dbe); 7641 goto end; 7642 } 7643 7644 /* 7645 * The new modes must be a subset of the current modes and 7646 * the access must specify at least one mode. To test that 7647 * the new mode is a subset of the current modes we bitwise 7648 * AND them together and check that the result equals the new 7649 * mode. For example: 7650 * New mode, access == R and current mode, sp->share_access == RW 7651 * access & sp->share_access == R == access, so the new access mode 7652 * is valid. Consider access == RW, sp->share_access = R 7653 * access & sp->share_access == R != access, so the new access mode 7654 * is invalid. 7655 */ 7656 if ((access & sp->share_access) != access || 7657 (deny & sp->share_deny) != deny || 7658 (access & 7659 (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) { 7660 *cs->statusp = resp->status = NFS4ERR_INVAL; 7661 rfs4_update_open_sequence(sp->owner); 7662 rfs4_dbe_unlock(sp->dbe); 7663 goto end; 7664 } 7665 7666 /* 7667 * Release any share locks associated with this stateID. 7668 * Strictly speaking, this violates the spec because the 7669 * spec effectively requires that open downgrade be atomic. 7670 * At present, fs_shrlock does not have this capability. 7671 */ 7672 rfs4_dbe_unlock(sp->dbe); 7673 rfs4_unshare(sp); 7674 rfs4_dbe_lock(sp->dbe); 7675 7676 fp = sp->finfo; 7677 rfs4_dbe_lock(fp->dbe); 7678 7679 /* 7680 * If the current mode has deny read and the new mode 7681 * does not, decrement the number of deny read mode bits 7682 * and if it goes to zero turn off the deny read bit 7683 * on the file. 7684 */ 7685 if ((sp->share_deny & OPEN4_SHARE_DENY_READ) && 7686 (deny & OPEN4_SHARE_DENY_READ) == 0) { 7687 fp->deny_read--; 7688 if (fp->deny_read == 0) 7689 fp->share_deny &= ~OPEN4_SHARE_DENY_READ; 7690 } 7691 7692 /* 7693 * If the current mode has deny write and the new mode 7694 * does not, decrement the number of deny write mode bits 7695 * and if it goes to zero turn off the deny write bit 7696 * on the file. 7697 */ 7698 if ((sp->share_deny & OPEN4_SHARE_DENY_WRITE) && 7699 (deny & OPEN4_SHARE_DENY_WRITE) == 0) { 7700 fp->deny_write--; 7701 if (fp->deny_write == 0) 7702 fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE; 7703 } 7704 7705 /* 7706 * If the current mode has access read and the new mode 7707 * does not, decrement the number of access read mode bits 7708 * and if it goes to zero turn off the access read bit 7709 * on the file. set fflags to FREAD for the call to 7710 * vn_open_downgrade(). 7711 */ 7712 if ((sp->share_access & OPEN4_SHARE_ACCESS_READ) && 7713 (access & OPEN4_SHARE_ACCESS_READ) == 0) { 7714 fp->access_read--; 7715 if (fp->access_read == 0) 7716 fp->share_access &= ~OPEN4_SHARE_ACCESS_READ; 7717 fflags |= FREAD; 7718 } 7719 7720 /* 7721 * If the current mode has access write and the new mode 7722 * does not, decrement the number of access write mode bits 7723 * and if it goes to zero turn off the access write bit 7724 * on the file. set fflags to FWRITE for the call to 7725 * vn_open_downgrade(). 7726 */ 7727 if ((sp->share_access & OPEN4_SHARE_ACCESS_WRITE) && 7728 (access & OPEN4_SHARE_ACCESS_WRITE) == 0) { 7729 fp->access_write--; 7730 if (fp->access_write == 0) 7731 fp->share_deny &= ~OPEN4_SHARE_ACCESS_WRITE; 7732 fflags |= FWRITE; 7733 } 7734 7735 /* Set the new access and deny modes */ 7736 sp->share_access = access; 7737 sp->share_deny = deny; 7738 /* Check that the file is still accessible */ 7739 ASSERT(fp->share_access); 7740 7741 rfs4_dbe_unlock(fp->dbe); 7742 7743 rfs4_dbe_unlock(sp->dbe); 7744 7745 if ((status = rfs4_share(sp)) != NFS4_OK) { 7746 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT; 7747 rfs4_update_open_sequence(sp->owner); 7748 goto end; 7749 } 7750 7751 /* 7752 * we successfully downgraded the share lock, now we need to downgrade 7753 * the open. it is possible that the downgrade was only for a deny 7754 * mode and we have nothing else to do. 7755 */ 7756 if ((fflags & (FREAD|FWRITE)) != 0) 7757 vn_open_downgrade(cs->vp, fflags); 7758 7759 rfs4_dbe_lock(sp->dbe); 7760 7761 /* Update the stateid */ 7762 next_stateid(&sp->stateid); 7763 resp->open_stateid = sp->stateid.stateid; 7764 7765 rfs4_dbe_unlock(sp->dbe); 7766 7767 *cs->statusp = resp->status = NFS4_OK; 7768 /* Update the lease */ 7769 rfs4_update_lease(sp->owner->client); 7770 /* And the sequence */ 7771 rfs4_update_open_sequence(sp->owner); 7772 rfs4_update_open_resp(sp->owner, resop, NULL); 7773 7774 end: 7775 rfs4_sw_exit(&sp->owner->oo_sw); 7776 rfs4_state_rele(sp); 7777 out: 7778 DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs, 7779 OPEN_DOWNGRADE4res *, resp); 7780 } 7781 7782 /* 7783 * The logic behind this function is detailed in the NFSv4 RFC in the 7784 * SETCLIENTID operation description under IMPLEMENTATION. Refer to 7785 * that section for explicit guidance to server behavior for 7786 * SETCLIENTID. 7787 */ 7788 void 7789 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop, 7790 struct svc_req *req, struct compound_state *cs) 7791 { 7792 SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid; 7793 SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid; 7794 rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed; 7795 bool_t create = TRUE; 7796 char *addr, *netid; 7797 int len; 7798 7799 DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs, 7800 SETCLIENTID4args *, args); 7801 retry: 7802 newcp = cp_confirmed = cp_unconfirmed = NULL; 7803 7804 /* 7805 * In search of an EXISTING client matching the incoming 7806 * request to establish a new client identifier at the server 7807 */ 7808 create = TRUE; 7809 cp = rfs4_findclient(&args->client, &create, NULL); 7810 7811 /* Should never happen */ 7812 ASSERT(cp != NULL); 7813 7814 if (cp == NULL) { 7815 *cs->statusp = res->status = NFS4ERR_SERVERFAULT; 7816 goto out; 7817 } 7818 7819 /* 7820 * Easiest case. Client identifier is newly created and is 7821 * unconfirmed. Also note that for this case, no other 7822 * entries exist for the client identifier. Nothing else to 7823 * check. Just setup the response and respond. 7824 */ 7825 if (create) { 7826 *cs->statusp = res->status = NFS4_OK; 7827 res->SETCLIENTID4res_u.resok4.clientid = cp->clientid; 7828 res->SETCLIENTID4res_u.resok4.setclientid_confirm = 7829 cp->confirm_verf; 7830 /* Setup callback information; CB_NULL confirmation later */ 7831 rfs4_client_setcb(cp, &args->callback, args->callback_ident); 7832 7833 rfs4_client_rele(cp); 7834 goto out; 7835 } 7836 7837 /* 7838 * An existing, confirmed client may exist but it may not have 7839 * been active for at least one lease period. If so, then 7840 * "close" the client and create a new client identifier 7841 */ 7842 if (rfs4_lease_expired(cp)) { 7843 rfs4_client_close(cp); 7844 goto retry; 7845 } 7846 7847 if (cp->need_confirm == TRUE) 7848 cp_unconfirmed = cp; 7849 else 7850 cp_confirmed = cp; 7851 7852 cp = NULL; 7853 7854 /* 7855 * We have a confirmed client, now check for an 7856 * unconfimred entry 7857 */ 7858 if (cp_confirmed) { 7859 /* If creds don't match then client identifier is inuse */ 7860 if (!creds_ok(cp_confirmed->cr_set, req, cs)) { 7861 rfs4_cbinfo_t *cbp; 7862 /* 7863 * Some one else has established this client 7864 * id. Try and say * who they are. We will use 7865 * the call back address supplied by * the 7866 * first client. 7867 */ 7868 *cs->statusp = res->status = NFS4ERR_CLID_INUSE; 7869 7870 addr = netid = NULL; 7871 7872 cbp = &cp_confirmed->cbinfo; 7873 if (cbp->cb_callback.cb_location.r_addr && 7874 cbp->cb_callback.cb_location.r_netid) { 7875 cb_client4 *cbcp = &cbp->cb_callback; 7876 7877 len = strlen(cbcp->cb_location.r_addr)+1; 7878 addr = kmem_alloc(len, KM_SLEEP); 7879 bcopy(cbcp->cb_location.r_addr, addr, len); 7880 len = strlen(cbcp->cb_location.r_netid)+1; 7881 netid = kmem_alloc(len, KM_SLEEP); 7882 bcopy(cbcp->cb_location.r_netid, netid, len); 7883 } 7884 7885 res->SETCLIENTID4res_u.client_using.r_addr = addr; 7886 res->SETCLIENTID4res_u.client_using.r_netid = netid; 7887 7888 rfs4_client_rele(cp_confirmed); 7889 } 7890 7891 /* 7892 * Confirmed, creds match, and verifier matches; must 7893 * be an update of the callback info 7894 */ 7895 if (cp_confirmed->nfs_client.verifier == 7896 args->client.verifier) { 7897 /* Setup callback information */ 7898 rfs4_client_setcb(cp_confirmed, &args->callback, 7899 args->callback_ident); 7900 7901 /* everything okay -- move ahead */ 7902 *cs->statusp = res->status = NFS4_OK; 7903 res->SETCLIENTID4res_u.resok4.clientid = 7904 cp_confirmed->clientid; 7905 7906 /* update the confirm_verifier and return it */ 7907 rfs4_client_scv_next(cp_confirmed); 7908 res->SETCLIENTID4res_u.resok4.setclientid_confirm = 7909 cp_confirmed->confirm_verf; 7910 7911 rfs4_client_rele(cp_confirmed); 7912 goto out; 7913 } 7914 7915 /* 7916 * Creds match but the verifier doesn't. Must search 7917 * for an unconfirmed client that would be replaced by 7918 * this request. 7919 */ 7920 create = FALSE; 7921 cp_unconfirmed = rfs4_findclient(&args->client, &create, 7922 cp_confirmed); 7923 } 7924 7925 /* 7926 * At this point, we have taken care of the brand new client 7927 * struct, INUSE case, update of an existing, and confirmed 7928 * client struct. 7929 */ 7930 7931 /* 7932 * check to see if things have changed while we originally 7933 * picked up the client struct. If they have, then return and 7934 * retry the processing of this SETCLIENTID request. 7935 */ 7936 if (cp_unconfirmed) { 7937 rfs4_dbe_lock(cp_unconfirmed->dbe); 7938 if (!cp_unconfirmed->need_confirm) { 7939 rfs4_dbe_unlock(cp_unconfirmed->dbe); 7940 rfs4_client_rele(cp_unconfirmed); 7941 if (cp_confirmed) 7942 rfs4_client_rele(cp_confirmed); 7943 goto retry; 7944 } 7945 /* do away with the old unconfirmed one */ 7946 rfs4_dbe_invalidate(cp_unconfirmed->dbe); 7947 rfs4_dbe_unlock(cp_unconfirmed->dbe); 7948 rfs4_client_rele(cp_unconfirmed); 7949 cp_unconfirmed = NULL; 7950 } 7951 7952 /* 7953 * This search will temporarily hide the confirmed client 7954 * struct while a new client struct is created as the 7955 * unconfirmed one. 7956 */ 7957 create = TRUE; 7958 newcp = rfs4_findclient(&args->client, &create, cp_confirmed); 7959 7960 ASSERT(newcp != NULL); 7961 7962 if (newcp == NULL) { 7963 *cs->statusp = res->status = NFS4ERR_SERVERFAULT; 7964 rfs4_client_rele(cp_confirmed); 7965 goto out; 7966 } 7967 7968 /* 7969 * If one was not created, then a similar request must be in 7970 * process so release and start over with this one 7971 */ 7972 if (create != TRUE) { 7973 rfs4_client_rele(newcp); 7974 if (cp_confirmed) 7975 rfs4_client_rele(cp_confirmed); 7976 goto retry; 7977 } 7978 7979 *cs->statusp = res->status = NFS4_OK; 7980 res->SETCLIENTID4res_u.resok4.clientid = newcp->clientid; 7981 res->SETCLIENTID4res_u.resok4.setclientid_confirm = newcp->confirm_verf; 7982 /* Setup callback information; CB_NULL confirmation later */ 7983 rfs4_client_setcb(newcp, &args->callback, args->callback_ident); 7984 7985 newcp->cp_confirmed = cp_confirmed; 7986 7987 rfs4_client_rele(newcp); 7988 7989 out: 7990 DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs, 7991 SETCLIENTID4res *, res); 7992 } 7993 7994 /*ARGSUSED*/ 7995 void 7996 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop, 7997 struct svc_req *req, struct compound_state *cs) 7998 { 7999 SETCLIENTID_CONFIRM4args *args = 8000 &argop->nfs_argop4_u.opsetclientid_confirm; 8001 SETCLIENTID_CONFIRM4res *res = 8002 &resop->nfs_resop4_u.opsetclientid_confirm; 8003 rfs4_client_t *cp, *cptoclose = NULL; 8004 8005 DTRACE_NFSV4_2(op__setclientid__confirm__start, 8006 struct compound_state *, cs, 8007 SETCLIENTID_CONFIRM4args *, args); 8008 8009 *cs->statusp = res->status = NFS4_OK; 8010 8011 cp = rfs4_findclient_by_id(args->clientid, TRUE); 8012 8013 if (cp == NULL) { 8014 *cs->statusp = res->status = 8015 rfs4_check_clientid(&args->clientid, 1); 8016 goto out; 8017 } 8018 8019 if (!creds_ok(cp, req, cs)) { 8020 *cs->statusp = res->status = NFS4ERR_CLID_INUSE; 8021 rfs4_client_rele(cp); 8022 goto out; 8023 } 8024 8025 /* If the verifier doesn't match, the record doesn't match */ 8026 if (cp->confirm_verf != args->setclientid_confirm) { 8027 *cs->statusp = res->status = NFS4ERR_STALE_CLIENTID; 8028 rfs4_client_rele(cp); 8029 goto out; 8030 } 8031 8032 rfs4_dbe_lock(cp->dbe); 8033 cp->need_confirm = FALSE; 8034 if (cp->cp_confirmed) { 8035 cptoclose = cp->cp_confirmed; 8036 cptoclose->ss_remove = 1; 8037 cp->cp_confirmed = NULL; 8038 } 8039 8040 /* 8041 * Update the client's associated server instance, if it's changed 8042 * since the client was created. 8043 */ 8044 if (rfs4_servinst(cp) != rfs4_cur_servinst) 8045 rfs4_servinst_assign(cp, rfs4_cur_servinst); 8046 8047 /* 8048 * Record clientid in stable storage. 8049 * Must be done after server instance has been assigned. 8050 */ 8051 rfs4_ss_clid(cp, req); 8052 8053 rfs4_dbe_unlock(cp->dbe); 8054 8055 if (cptoclose) 8056 /* don't need to rele, client_close does it */ 8057 rfs4_client_close(cptoclose); 8058 8059 /* If needed, initiate CB_NULL call for callback path */ 8060 rfs4_deleg_cb_check(cp); 8061 rfs4_update_lease(cp); 8062 8063 /* 8064 * Check to see if client can perform reclaims 8065 */ 8066 rfs4_ss_chkclid(cp); 8067 8068 rfs4_client_rele(cp); 8069 8070 out: 8071 DTRACE_NFSV4_2(op__setclientid__confirm__done, 8072 struct compound_state *, cs, 8073 SETCLIENTID_CONFIRM4 *, res); 8074 } 8075 8076 8077 /*ARGSUSED*/ 8078 void 8079 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop, 8080 struct svc_req *req, struct compound_state *cs) 8081 { 8082 CLOSE4args *args = &argop->nfs_argop4_u.opclose; 8083 CLOSE4res *resp = &resop->nfs_resop4_u.opclose; 8084 rfs4_state_t *sp; 8085 nfsstat4 status; 8086 8087 DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs, 8088 CLOSE4args *, args); 8089 8090 if (cs->vp == NULL) { 8091 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 8092 goto out; 8093 } 8094 8095 status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID); 8096 if (status != NFS4_OK) { 8097 *cs->statusp = resp->status = status; 8098 goto out; 8099 } 8100 8101 /* Ensure specified filehandle matches */ 8102 if (cs->vp != sp->finfo->vp) { 8103 rfs4_state_rele(sp); 8104 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8105 goto out; 8106 } 8107 8108 /* hold off other access to open_owner while we tinker */ 8109 rfs4_sw_enter(&sp->owner->oo_sw); 8110 8111 switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) { 8112 case NFS4_CHECK_STATEID_OKAY: 8113 if (rfs4_check_open_seqid(args->seqid, sp->owner, 8114 resop) != NFS4_CHKSEQ_OKAY) { 8115 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 8116 goto end; 8117 } 8118 break; 8119 case NFS4_CHECK_STATEID_OLD: 8120 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 8121 goto end; 8122 case NFS4_CHECK_STATEID_BAD: 8123 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8124 goto end; 8125 case NFS4_CHECK_STATEID_EXPIRED: 8126 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 8127 goto end; 8128 case NFS4_CHECK_STATEID_CLOSED: 8129 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 8130 goto end; 8131 case NFS4_CHECK_STATEID_UNCONFIRMED: 8132 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8133 goto end; 8134 case NFS4_CHECK_STATEID_REPLAY: 8135 /* Check the sequence id for the open owner */ 8136 switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) { 8137 case NFS4_CHKSEQ_OKAY: 8138 /* 8139 * This is replayed stateid; if seqid matches 8140 * next expected, then client is using wrong seqid. 8141 */ 8142 /* FALL THROUGH */ 8143 case NFS4_CHKSEQ_BAD: 8144 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 8145 goto end; 8146 case NFS4_CHKSEQ_REPLAY: 8147 /* 8148 * Note this case is the duplicate case so 8149 * resp->status is already set. 8150 */ 8151 *cs->statusp = resp->status; 8152 rfs4_update_lease(sp->owner->client); 8153 goto end; 8154 } 8155 break; 8156 default: 8157 ASSERT(FALSE); 8158 break; 8159 } 8160 8161 rfs4_dbe_lock(sp->dbe); 8162 8163 /* Update the stateid. */ 8164 next_stateid(&sp->stateid); 8165 resp->open_stateid = sp->stateid.stateid; 8166 8167 rfs4_dbe_unlock(sp->dbe); 8168 8169 rfs4_update_lease(sp->owner->client); 8170 rfs4_update_open_sequence(sp->owner); 8171 rfs4_update_open_resp(sp->owner, resop, NULL); 8172 8173 rfs4_state_close(sp, FALSE, FALSE, cs->cr); 8174 8175 *cs->statusp = resp->status = status; 8176 8177 end: 8178 rfs4_sw_exit(&sp->owner->oo_sw); 8179 rfs4_state_rele(sp); 8180 out: 8181 DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs, 8182 CLOSE4res *, resp); 8183 } 8184 8185 /* 8186 * Manage the counts on the file struct and close all file locks 8187 */ 8188 /*ARGSUSED*/ 8189 void 8190 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr, 8191 bool_t close_of_client) 8192 { 8193 rfs4_file_t *fp = sp->finfo; 8194 rfs4_lo_state_t *lsp; 8195 struct shrlock shr; 8196 struct shr_locowner shr_loco; 8197 int fflags, s_access, s_deny; 8198 8199 fflags = s_access = s_deny = 0; 8200 /* 8201 * Decrement the count for each access and deny bit that this 8202 * state has contributed to the file. If the file counts go to zero 8203 * clear the appropriate bit in the appropriate mask. 8204 */ 8205 8206 if (sp->share_access & OPEN4_SHARE_ACCESS_READ) { 8207 fp->access_read--; 8208 fflags |= FREAD; 8209 s_access |= F_RDACC; 8210 if (fp->access_read == 0) 8211 fp->share_access &= ~OPEN4_SHARE_ACCESS_READ; 8212 } 8213 if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) { 8214 fp->access_write--; 8215 fflags |= FWRITE; 8216 s_access |= F_WRACC; 8217 if (fp->access_write == 0) 8218 fp->share_access &= ~OPEN4_SHARE_ACCESS_WRITE; 8219 } 8220 if (sp->share_deny & OPEN4_SHARE_DENY_READ) { 8221 fp->deny_read--; 8222 s_deny |= F_RDDNY; 8223 if (fp->deny_read == 0) 8224 fp->share_deny &= ~OPEN4_SHARE_DENY_READ; 8225 } 8226 if (sp->share_deny & OPEN4_SHARE_DENY_WRITE) { 8227 fp->deny_write--; 8228 s_deny |= F_WRDNY; 8229 if (fp->deny_write == 0) 8230 fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE; 8231 } 8232 8233 /* 8234 * If this call is part of the larger closing down of client 8235 * state then it is just easier to release all locks 8236 * associated with this client instead of going through each 8237 * individual file and cleaning locks there. 8238 */ 8239 if (close_of_client) { 8240 if (sp->owner->client->unlksys_completed == FALSE && 8241 sp->lockownerlist.next->lsp != NULL && 8242 sp->owner->client->sysidt != LM_NOSYSID) { 8243 /* Is the PxFS kernel module loaded? */ 8244 if (lm_remove_file_locks != NULL) { 8245 int new_sysid; 8246 8247 /* Encode the cluster nodeid in new sysid */ 8248 new_sysid = sp->owner->client->sysidt; 8249 lm_set_nlmid_flk(&new_sysid); 8250 8251 /* 8252 * This PxFS routine removes file locks for a 8253 * client over all nodes of a cluster. 8254 */ 8255 NFS4_DEBUG(rfs4_debug, (CE_NOTE, 8256 "lm_remove_file_locks(sysid=0x%x)\n", 8257 new_sysid)); 8258 (*lm_remove_file_locks)(new_sysid); 8259 } else { 8260 struct flock64 flk; 8261 8262 /* Release all locks for this client */ 8263 flk.l_type = F_UNLKSYS; 8264 flk.l_whence = 0; 8265 flk.l_start = 0; 8266 flk.l_len = 0; 8267 flk.l_sysid = sp->owner->client->sysidt; 8268 flk.l_pid = 0; 8269 (void) VOP_FRLOCK(sp->finfo->vp, F_SETLK, &flk, 8270 F_REMOTELOCK | FREAD | FWRITE, 8271 (u_offset_t)0, NULL, CRED(), NULL); 8272 } 8273 8274 sp->owner->client->unlksys_completed = TRUE; 8275 } 8276 } 8277 8278 /* 8279 * Release all locks on this file by this lock owner or at 8280 * least mark the locks as having been released 8281 */ 8282 for (lsp = sp->lockownerlist.next->lsp; lsp != NULL; 8283 lsp = lsp->lockownerlist.next->lsp) { 8284 8285 lsp->locks_cleaned = TRUE; 8286 8287 /* Was this already taken care of above? */ 8288 if (!close_of_client && 8289 sp->owner->client->sysidt != LM_NOSYSID) 8290 (void) cleanlocks(sp->finfo->vp, lsp->locker->pid, 8291 lsp->locker->client->sysidt); 8292 } 8293 8294 /* 8295 * Release any shrlocks associated with this open state ID. 8296 * This must be done before the rfs4_state gets marked closed. 8297 */ 8298 if (sp->owner->client->sysidt != LM_NOSYSID) { 8299 shr.s_access = s_access; 8300 shr.s_deny = s_deny; 8301 shr.s_pid = rfs4_dbe_getid(sp->owner->dbe); 8302 shr.s_sysid = sp->owner->client->sysidt; 8303 shr_loco.sl_pid = shr.s_pid; 8304 shr_loco.sl_id = shr.s_sysid; 8305 shr.s_owner = (caddr_t)&shr_loco; 8306 shr.s_own_len = sizeof (shr_loco); 8307 (void) vop_shrlock(sp->finfo->vp, F_UNSHARE, &shr, fflags); 8308 } 8309 8310 (void) VOP_CLOSE(fp->vp, fflags, 1, (offset_t)0, cr, NULL); 8311 } 8312 8313 /* 8314 * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure. 8315 */ 8316 static nfsstat4 8317 lock_denied(LOCK4denied *dp, struct flock64 *flk) 8318 { 8319 rfs4_lockowner_t *lo; 8320 rfs4_client_t *cp; 8321 uint32_t len; 8322 8323 lo = rfs4_findlockowner_by_pid(flk->l_pid); 8324 if (lo != NULL) { 8325 cp = lo->client; 8326 if (rfs4_lease_expired(cp)) { 8327 rfs4_lockowner_rele(lo); 8328 rfs4_dbe_hold(cp->dbe); 8329 rfs4_client_close(cp); 8330 return (NFS4ERR_EXPIRED); 8331 } 8332 dp->owner.clientid = lo->owner.clientid; 8333 len = lo->owner.owner_len; 8334 dp->owner.owner_val = kmem_alloc(len, KM_SLEEP); 8335 bcopy(lo->owner.owner_val, dp->owner.owner_val, len); 8336 dp->owner.owner_len = len; 8337 rfs4_lockowner_rele(lo); 8338 goto finish; 8339 } 8340 8341 /* 8342 * Its not a NFS4 lock. We take advantage that the upper 32 bits 8343 * of the client id contain the boot time for a NFS4 lock. So we 8344 * fabricate and identity by setting clientid to the sysid, and 8345 * the lock owner to the pid. 8346 */ 8347 dp->owner.clientid = flk->l_sysid; 8348 len = sizeof (pid_t); 8349 dp->owner.owner_len = len; 8350 dp->owner.owner_val = kmem_alloc(len, KM_SLEEP); 8351 bcopy(&flk->l_pid, dp->owner.owner_val, len); 8352 finish: 8353 dp->offset = flk->l_start; 8354 dp->length = flk->l_len; 8355 8356 if (flk->l_type == F_RDLCK) 8357 dp->locktype = READ_LT; 8358 else if (flk->l_type == F_WRLCK) 8359 dp->locktype = WRITE_LT; 8360 else 8361 return (NFS4ERR_INVAL); /* no mapping from POSIX ltype to v4 */ 8362 8363 return (NFS4_OK); 8364 } 8365 8366 static int 8367 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred) 8368 { 8369 int error; 8370 struct flock64 flk; 8371 int i; 8372 clock_t delaytime; 8373 int cmd; 8374 8375 cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK; 8376 retry: 8377 delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay); 8378 8379 for (i = 0; i < rfs4_maxlock_tries; i++) { 8380 LOCK_PRINT(rfs4_debug, "setlock", cmd, flock); 8381 error = VOP_FRLOCK(vp, cmd, 8382 flock, flag, (u_offset_t)0, NULL, cred, NULL); 8383 8384 if (error != EAGAIN && error != EACCES) 8385 break; 8386 8387 if (i < rfs4_maxlock_tries - 1) { 8388 delay(delaytime); 8389 delaytime *= 2; 8390 } 8391 } 8392 8393 if (error == EAGAIN || error == EACCES) { 8394 /* Get the owner of the lock */ 8395 flk = *flock; 8396 LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk); 8397 if (VOP_FRLOCK(vp, F_GETLK, &flk, flag, 8398 (u_offset_t)0, NULL, cred, NULL) == 0) { 8399 if (flk.l_type == F_UNLCK) { 8400 /* No longer locked, retry */ 8401 goto retry; 8402 } 8403 *flock = flk; 8404 LOCK_PRINT(rfs4_debug, "setlock(blocking lock)", 8405 F_GETLK, &flk); 8406 } 8407 } 8408 8409 return (error); 8410 } 8411 8412 /*ARGSUSED*/ 8413 static nfsstat4 8414 rfs4_do_lock(rfs4_lo_state_t *lp, nfs_lock_type4 locktype, 8415 seqid4 seqid, offset4 offset, 8416 length4 length, cred_t *cred, nfs_resop4 *resop) 8417 { 8418 nfsstat4 status; 8419 rfs4_lockowner_t *lo = lp->locker; 8420 rfs4_state_t *sp = lp->state; 8421 struct flock64 flock; 8422 int16_t ltype; 8423 int flag; 8424 int error; 8425 sysid_t sysid; 8426 LOCK4res *lres; 8427 8428 if (rfs4_lease_expired(lo->client)) { 8429 return (NFS4ERR_EXPIRED); 8430 } 8431 8432 if ((status = rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK) 8433 return (status); 8434 8435 /* Check for zero length. To lock to end of file use all ones for V4 */ 8436 if (length == 0) 8437 return (NFS4ERR_INVAL); 8438 else if (length == (length4)(~0)) 8439 length = 0; /* Posix to end of file */ 8440 8441 retry: 8442 rfs4_dbe_lock(sp->dbe); 8443 8444 8445 if (resop->resop != OP_LOCKU) { 8446 switch (locktype) { 8447 case READ_LT: 8448 case READW_LT: 8449 if ((sp->share_access 8450 & OPEN4_SHARE_ACCESS_READ) == 0) { 8451 rfs4_dbe_unlock(sp->dbe); 8452 8453 return (NFS4ERR_OPENMODE); 8454 } 8455 ltype = F_RDLCK; 8456 break; 8457 case WRITE_LT: 8458 case WRITEW_LT: 8459 if ((sp->share_access 8460 & OPEN4_SHARE_ACCESS_WRITE) == 0) { 8461 rfs4_dbe_unlock(sp->dbe); 8462 8463 return (NFS4ERR_OPENMODE); 8464 } 8465 ltype = F_WRLCK; 8466 break; 8467 } 8468 } else 8469 ltype = F_UNLCK; 8470 8471 flock.l_type = ltype; 8472 flock.l_whence = 0; /* SEEK_SET */ 8473 flock.l_start = offset; 8474 flock.l_len = length; 8475 flock.l_sysid = sysid; 8476 flock.l_pid = lp->locker->pid; 8477 8478 /* Note that length4 is uint64_t but l_len and l_start are off64_t */ 8479 if (flock.l_len < 0 || flock.l_start < 0) { 8480 rfs4_dbe_unlock(sp->dbe); 8481 return (NFS4ERR_INVAL); 8482 } 8483 8484 /* 8485 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and 8486 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE. 8487 */ 8488 flag = (int)sp->share_access | F_REMOTELOCK; 8489 8490 error = setlock(sp->finfo->vp, &flock, flag, cred); 8491 if (error == 0) { 8492 rfs4_dbe_lock(lp->dbe); 8493 next_stateid(&lp->lockid); 8494 rfs4_dbe_unlock(lp->dbe); 8495 } 8496 8497 rfs4_dbe_unlock(sp->dbe); 8498 8499 /* 8500 * N.B. We map error values to nfsv4 errors. This is differrent 8501 * than puterrno4 routine. 8502 */ 8503 switch (error) { 8504 case 0: 8505 status = NFS4_OK; 8506 break; 8507 case EAGAIN: 8508 case EACCES: /* Old value */ 8509 /* Can only get here if op is OP_LOCK */ 8510 ASSERT(resop->resop == OP_LOCK); 8511 lres = &resop->nfs_resop4_u.oplock; 8512 status = NFS4ERR_DENIED; 8513 if (lock_denied(&lres->LOCK4res_u.denied, &flock) 8514 == NFS4ERR_EXPIRED) 8515 goto retry; 8516 break; 8517 case ENOLCK: 8518 status = NFS4ERR_DELAY; 8519 break; 8520 case EOVERFLOW: 8521 status = NFS4ERR_INVAL; 8522 break; 8523 case EINVAL: 8524 status = NFS4ERR_NOTSUPP; 8525 break; 8526 default: 8527 status = NFS4ERR_SERVERFAULT; 8528 break; 8529 } 8530 8531 return (status); 8532 } 8533 8534 /*ARGSUSED*/ 8535 void 8536 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop, 8537 struct svc_req *req, struct compound_state *cs) 8538 { 8539 LOCK4args *args = &argop->nfs_argop4_u.oplock; 8540 LOCK4res *resp = &resop->nfs_resop4_u.oplock; 8541 nfsstat4 status; 8542 stateid4 *stateid; 8543 rfs4_lockowner_t *lo; 8544 rfs4_client_t *cp; 8545 rfs4_state_t *sp = NULL; 8546 rfs4_lo_state_t *lsp = NULL; 8547 bool_t ls_sw_held = FALSE; 8548 bool_t create = TRUE; 8549 bool_t lcreate = TRUE; 8550 bool_t dup_lock = FALSE; 8551 int rc; 8552 8553 DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs, 8554 LOCK4args *, args); 8555 8556 if (cs->vp == NULL) { 8557 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 8558 DTRACE_NFSV4_2(op__lock__done, struct compound_state *, 8559 cs, LOCK4res *, resp); 8560 return; 8561 } 8562 8563 if (args->locker.new_lock_owner) { 8564 /* Create a new lockowner for this instance */ 8565 open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner; 8566 8567 NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner")); 8568 8569 stateid = &olo->open_stateid; 8570 status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID); 8571 if (status != NFS4_OK) { 8572 NFS4_DEBUG(rfs4_debug, 8573 (CE_NOTE, "Get state failed in lock %d", status)); 8574 *cs->statusp = resp->status = status; 8575 DTRACE_NFSV4_2(op__lock__done, struct compound_state *, 8576 cs, LOCK4res *, resp); 8577 return; 8578 } 8579 8580 /* Ensure specified filehandle matches */ 8581 if (cs->vp != sp->finfo->vp) { 8582 rfs4_state_rele(sp); 8583 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8584 DTRACE_NFSV4_2(op__lock__done, struct compound_state *, 8585 cs, LOCK4res *, resp); 8586 return; 8587 } 8588 8589 /* hold off other access to open_owner while we tinker */ 8590 rfs4_sw_enter(&sp->owner->oo_sw); 8591 8592 switch (rc = rfs4_check_stateid_seqid(sp, stateid)) { 8593 case NFS4_CHECK_STATEID_OLD: 8594 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 8595 goto end; 8596 case NFS4_CHECK_STATEID_BAD: 8597 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8598 goto end; 8599 case NFS4_CHECK_STATEID_EXPIRED: 8600 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 8601 goto end; 8602 case NFS4_CHECK_STATEID_UNCONFIRMED: 8603 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8604 goto end; 8605 case NFS4_CHECK_STATEID_CLOSED: 8606 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 8607 goto end; 8608 case NFS4_CHECK_STATEID_OKAY: 8609 case NFS4_CHECK_STATEID_REPLAY: 8610 switch (rfs4_check_olo_seqid(olo->open_seqid, 8611 sp->owner, resop)) { 8612 case NFS4_CHKSEQ_OKAY: 8613 if (rc == NFS4_CHECK_STATEID_OKAY) 8614 break; 8615 /* 8616 * This is replayed stateid; if seqid 8617 * matches next expected, then client 8618 * is using wrong seqid. 8619 */ 8620 /* FALLTHROUGH */ 8621 case NFS4_CHKSEQ_BAD: 8622 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 8623 goto end; 8624 case NFS4_CHKSEQ_REPLAY: 8625 /* This is a duplicate LOCK request */ 8626 dup_lock = TRUE; 8627 8628 /* 8629 * For a duplicate we do not want to 8630 * create a new lockowner as it should 8631 * already exist. 8632 * Turn off the lockowner create flag. 8633 */ 8634 lcreate = FALSE; 8635 } 8636 break; 8637 } 8638 8639 lo = rfs4_findlockowner(&olo->lock_owner, &lcreate); 8640 if (lo == NULL) { 8641 NFS4_DEBUG(rfs4_debug, 8642 (CE_NOTE, "rfs4_op_lock: no lock owner")); 8643 *cs->statusp = resp->status = NFS4ERR_RESOURCE; 8644 goto end; 8645 } 8646 8647 lsp = rfs4_findlo_state_by_owner(lo, sp, &create); 8648 if (lsp == NULL) { 8649 rfs4_update_lease(sp->owner->client); 8650 /* 8651 * Only update theh open_seqid if this is not 8652 * a duplicate request 8653 */ 8654 if (dup_lock == FALSE) { 8655 rfs4_update_open_sequence(sp->owner); 8656 } 8657 8658 NFS4_DEBUG(rfs4_debug, 8659 (CE_NOTE, "rfs4_op_lock: no state")); 8660 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT; 8661 rfs4_update_open_resp(sp->owner, resop, NULL); 8662 rfs4_lockowner_rele(lo); 8663 goto end; 8664 } 8665 8666 /* 8667 * This is the new_lock_owner branch and the client is 8668 * supposed to be associating a new lock_owner with 8669 * the open file at this point. If we find that a 8670 * lock_owner/state association already exists and a 8671 * successful LOCK request was returned to the client, 8672 * an error is returned to the client since this is 8673 * not appropriate. The client should be using the 8674 * existing lock_owner branch. 8675 */ 8676 if (dup_lock == FALSE && create == FALSE) { 8677 if (lsp->lock_completed == TRUE) { 8678 *cs->statusp = 8679 resp->status = NFS4ERR_BAD_SEQID; 8680 rfs4_lockowner_rele(lo); 8681 goto end; 8682 } 8683 } 8684 8685 rfs4_update_lease(sp->owner->client); 8686 8687 /* 8688 * Only update theh open_seqid if this is not 8689 * a duplicate request 8690 */ 8691 if (dup_lock == FALSE) { 8692 rfs4_update_open_sequence(sp->owner); 8693 } 8694 8695 /* 8696 * If this is a duplicate lock request, just copy the 8697 * previously saved reply and return. 8698 */ 8699 if (dup_lock == TRUE) { 8700 /* verify that lock_seqid's match */ 8701 if (lsp->seqid != olo->lock_seqid) { 8702 NFS4_DEBUG(rfs4_debug, 8703 (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad" 8704 "lsp->seqid=%d old->seqid=%d", 8705 lsp->seqid, olo->lock_seqid)); 8706 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 8707 } else { 8708 rfs4_copy_reply(resop, lsp->reply); 8709 /* 8710 * Make sure to copy the just 8711 * retrieved reply status into the 8712 * overall compound status 8713 */ 8714 *cs->statusp = resp->status; 8715 } 8716 rfs4_lockowner_rele(lo); 8717 goto end; 8718 } 8719 8720 rfs4_dbe_lock(lsp->dbe); 8721 8722 /* Make sure to update the lock sequence id */ 8723 lsp->seqid = olo->lock_seqid; 8724 8725 NFS4_DEBUG(rfs4_debug, 8726 (CE_NOTE, "Lock seqid established as %d", lsp->seqid)); 8727 8728 /* 8729 * This is used to signify the newly created lockowner 8730 * stateid and its sequence number. The checks for 8731 * sequence number and increment don't occur on the 8732 * very first lock request for a lockowner. 8733 */ 8734 lsp->skip_seqid_check = TRUE; 8735 8736 /* hold off other access to lsp while we tinker */ 8737 rfs4_sw_enter(&lsp->ls_sw); 8738 ls_sw_held = TRUE; 8739 8740 rfs4_dbe_unlock(lsp->dbe); 8741 8742 rfs4_lockowner_rele(lo); 8743 } else { 8744 stateid = &args->locker.locker4_u.lock_owner.lock_stateid; 8745 /* get lsp and hold the lock on the underlying file struct */ 8746 if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) 8747 != NFS4_OK) { 8748 *cs->statusp = resp->status = status; 8749 DTRACE_NFSV4_2(op__lock__done, struct compound_state *, 8750 cs, LOCK4res *, resp); 8751 return; 8752 } 8753 create = FALSE; /* We didn't create lsp */ 8754 8755 /* Ensure specified filehandle matches */ 8756 if (cs->vp != lsp->state->finfo->vp) { 8757 rfs4_lo_state_rele(lsp, TRUE); 8758 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8759 DTRACE_NFSV4_2(op__lock__done, struct compound_state *, 8760 cs, LOCK4res *, resp); 8761 return; 8762 } 8763 8764 /* hold off other access to lsp while we tinker */ 8765 rfs4_sw_enter(&lsp->ls_sw); 8766 ls_sw_held = TRUE; 8767 8768 switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) { 8769 /* 8770 * The stateid looks like it was okay (expected to be 8771 * the next one) 8772 */ 8773 case NFS4_CHECK_STATEID_OKAY: 8774 /* 8775 * The sequence id is now checked. Determine 8776 * if this is a replay or if it is in the 8777 * expected (next) sequence. In the case of a 8778 * replay, there are two replay conditions 8779 * that may occur. The first is the normal 8780 * condition where a LOCK is done with a 8781 * NFS4_OK response and the stateid is 8782 * updated. That case is handled below when 8783 * the stateid is identified as a REPLAY. The 8784 * second is the case where an error is 8785 * returned, like NFS4ERR_DENIED, and the 8786 * sequence number is updated but the stateid 8787 * is not updated. This second case is dealt 8788 * with here. So it may seem odd that the 8789 * stateid is okay but the sequence id is a 8790 * replay but it is okay. 8791 */ 8792 switch (rfs4_check_lock_seqid( 8793 args->locker.locker4_u.lock_owner.lock_seqid, 8794 lsp, resop)) { 8795 case NFS4_CHKSEQ_REPLAY: 8796 if (resp->status != NFS4_OK) { 8797 /* 8798 * Here is our replay and need 8799 * to verify that the last 8800 * response was an error. 8801 */ 8802 *cs->statusp = resp->status; 8803 goto end; 8804 } 8805 /* 8806 * This is done since the sequence id 8807 * looked like a replay but it didn't 8808 * pass our check so a BAD_SEQID is 8809 * returned as a result. 8810 */ 8811 /*FALLTHROUGH*/ 8812 case NFS4_CHKSEQ_BAD: 8813 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 8814 goto end; 8815 case NFS4_CHKSEQ_OKAY: 8816 /* Everything looks okay move ahead */ 8817 break; 8818 } 8819 break; 8820 case NFS4_CHECK_STATEID_OLD: 8821 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 8822 goto end; 8823 case NFS4_CHECK_STATEID_BAD: 8824 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8825 goto end; 8826 case NFS4_CHECK_STATEID_EXPIRED: 8827 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 8828 goto end; 8829 case NFS4_CHECK_STATEID_CLOSED: 8830 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 8831 goto end; 8832 case NFS4_CHECK_STATEID_REPLAY: 8833 switch (rfs4_check_lock_seqid( 8834 args->locker.locker4_u.lock_owner.lock_seqid, 8835 lsp, resop)) { 8836 case NFS4_CHKSEQ_OKAY: 8837 /* 8838 * This is a replayed stateid; if 8839 * seqid matches the next expected, 8840 * then client is using wrong seqid. 8841 */ 8842 case NFS4_CHKSEQ_BAD: 8843 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 8844 goto end; 8845 case NFS4_CHKSEQ_REPLAY: 8846 rfs4_update_lease(lsp->locker->client); 8847 *cs->statusp = status = resp->status; 8848 goto end; 8849 } 8850 break; 8851 default: 8852 ASSERT(FALSE); 8853 break; 8854 } 8855 8856 rfs4_update_lock_sequence(lsp); 8857 rfs4_update_lease(lsp->locker->client); 8858 } 8859 8860 /* 8861 * NFS4 only allows locking on regular files, so 8862 * verify type of object. 8863 */ 8864 if (cs->vp->v_type != VREG) { 8865 if (cs->vp->v_type == VDIR) 8866 status = NFS4ERR_ISDIR; 8867 else 8868 status = NFS4ERR_INVAL; 8869 goto out; 8870 } 8871 8872 cp = lsp->state->owner->client; 8873 8874 if (rfs4_clnt_in_grace(cp) && !args->reclaim) { 8875 status = NFS4ERR_GRACE; 8876 goto out; 8877 } 8878 8879 if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->can_reclaim) { 8880 status = NFS4ERR_NO_GRACE; 8881 goto out; 8882 } 8883 8884 if (!rfs4_clnt_in_grace(cp) && args->reclaim) { 8885 status = NFS4ERR_NO_GRACE; 8886 goto out; 8887 } 8888 8889 if (lsp->state->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE) 8890 cs->deleg = TRUE; 8891 8892 status = rfs4_do_lock(lsp, args->locktype, 8893 args->locker.locker4_u.lock_owner.lock_seqid, 8894 args->offset, args->length, cs->cr, resop); 8895 8896 out: 8897 lsp->skip_seqid_check = FALSE; 8898 8899 *cs->statusp = resp->status = status; 8900 8901 if (status == NFS4_OK) { 8902 resp->LOCK4res_u.lock_stateid = lsp->lockid.stateid; 8903 lsp->lock_completed = TRUE; 8904 } 8905 /* 8906 * Only update the "OPEN" response here if this was a new 8907 * lock_owner 8908 */ 8909 if (sp) 8910 rfs4_update_open_resp(sp->owner, resop, NULL); 8911 8912 rfs4_update_lock_resp(lsp, resop); 8913 8914 end: 8915 if (lsp) { 8916 if (ls_sw_held) 8917 rfs4_sw_exit(&lsp->ls_sw); 8918 /* 8919 * If an sp obtained, then the lsp does not represent 8920 * a lock on the file struct. 8921 */ 8922 if (sp != NULL) 8923 rfs4_lo_state_rele(lsp, FALSE); 8924 else 8925 rfs4_lo_state_rele(lsp, TRUE); 8926 } 8927 if (sp) { 8928 rfs4_sw_exit(&sp->owner->oo_sw); 8929 rfs4_state_rele(sp); 8930 } 8931 8932 DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs, 8933 LOCK4res *, resp); 8934 } 8935 8936 /* free function for LOCK/LOCKT */ 8937 static void 8938 lock_denied_free(nfs_resop4 *resop) 8939 { 8940 LOCK4denied *dp = NULL; 8941 8942 switch (resop->resop) { 8943 case OP_LOCK: 8944 if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED) 8945 dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied; 8946 break; 8947 case OP_LOCKT: 8948 if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED) 8949 dp = &resop->nfs_resop4_u.oplockt.denied; 8950 break; 8951 default: 8952 break; 8953 } 8954 8955 if (dp) 8956 kmem_free(dp->owner.owner_val, dp->owner.owner_len); 8957 } 8958 8959 /*ARGSUSED*/ 8960 void 8961 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop, 8962 struct svc_req *req, struct compound_state *cs) 8963 { 8964 LOCKU4args *args = &argop->nfs_argop4_u.oplocku; 8965 LOCKU4res *resp = &resop->nfs_resop4_u.oplocku; 8966 nfsstat4 status; 8967 stateid4 *stateid = &args->lock_stateid; 8968 rfs4_lo_state_t *lsp; 8969 8970 DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs, 8971 LOCKU4args *, args); 8972 8973 if (cs->vp == NULL) { 8974 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 8975 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs, 8976 LOCKU4res *, resp); 8977 return; 8978 } 8979 8980 if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) { 8981 *cs->statusp = resp->status = status; 8982 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs, 8983 LOCKU4res *, resp); 8984 return; 8985 } 8986 8987 /* Ensure specified filehandle matches */ 8988 if (cs->vp != lsp->state->finfo->vp) { 8989 rfs4_lo_state_rele(lsp, TRUE); 8990 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 8991 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs, 8992 LOCKU4res *, resp); 8993 return; 8994 } 8995 8996 /* hold off other access to lsp while we tinker */ 8997 rfs4_sw_enter(&lsp->ls_sw); 8998 8999 switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) { 9000 case NFS4_CHECK_STATEID_OKAY: 9001 if (rfs4_check_lock_seqid(args->seqid, lsp, resop) 9002 != NFS4_CHKSEQ_OKAY) { 9003 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 9004 goto end; 9005 } 9006 break; 9007 case NFS4_CHECK_STATEID_OLD: 9008 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 9009 goto end; 9010 case NFS4_CHECK_STATEID_BAD: 9011 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID; 9012 goto end; 9013 case NFS4_CHECK_STATEID_EXPIRED: 9014 *cs->statusp = resp->status = NFS4ERR_EXPIRED; 9015 goto end; 9016 case NFS4_CHECK_STATEID_CLOSED: 9017 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID; 9018 goto end; 9019 case NFS4_CHECK_STATEID_REPLAY: 9020 switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) { 9021 case NFS4_CHKSEQ_OKAY: 9022 /* 9023 * This is a replayed stateid; if 9024 * seqid matches the next expected, 9025 * then client is using wrong seqid. 9026 */ 9027 case NFS4_CHKSEQ_BAD: 9028 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID; 9029 goto end; 9030 case NFS4_CHKSEQ_REPLAY: 9031 rfs4_update_lease(lsp->locker->client); 9032 *cs->statusp = status = resp->status; 9033 goto end; 9034 } 9035 break; 9036 default: 9037 ASSERT(FALSE); 9038 break; 9039 } 9040 9041 rfs4_update_lock_sequence(lsp); 9042 rfs4_update_lease(lsp->locker->client); 9043 9044 /* 9045 * NFS4 only allows locking on regular files, so 9046 * verify type of object. 9047 */ 9048 if (cs->vp->v_type != VREG) { 9049 if (cs->vp->v_type == VDIR) 9050 status = NFS4ERR_ISDIR; 9051 else 9052 status = NFS4ERR_INVAL; 9053 goto out; 9054 } 9055 9056 if (rfs4_clnt_in_grace(lsp->state->owner->client)) { 9057 status = NFS4ERR_GRACE; 9058 goto out; 9059 } 9060 9061 status = rfs4_do_lock(lsp, args->locktype, 9062 args->seqid, args->offset, args->length, cs->cr, resop); 9063 9064 out: 9065 *cs->statusp = resp->status = status; 9066 9067 if (status == NFS4_OK) 9068 resp->lock_stateid = lsp->lockid.stateid; 9069 9070 rfs4_update_lock_resp(lsp, resop); 9071 9072 end: 9073 rfs4_sw_exit(&lsp->ls_sw); 9074 rfs4_lo_state_rele(lsp, TRUE); 9075 9076 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs, 9077 LOCKU4res *, resp); 9078 } 9079 9080 /* 9081 * LOCKT is a best effort routine, the client can not be guaranteed that 9082 * the status return is still in effect by the time the reply is received. 9083 * They are numerous race conditions in this routine, but we are not required 9084 * and can not be accurate. 9085 */ 9086 /*ARGSUSED*/ 9087 void 9088 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop, 9089 struct svc_req *req, struct compound_state *cs) 9090 { 9091 LOCKT4args *args = &argop->nfs_argop4_u.oplockt; 9092 LOCKT4res *resp = &resop->nfs_resop4_u.oplockt; 9093 rfs4_lockowner_t *lo; 9094 rfs4_client_t *cp; 9095 bool_t create = FALSE; 9096 struct flock64 flk; 9097 int error; 9098 int flag = FREAD | FWRITE; 9099 int ltype; 9100 length4 posix_length; 9101 sysid_t sysid; 9102 pid_t pid; 9103 9104 DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs, 9105 LOCKT4args *, args); 9106 9107 if (cs->vp == NULL) { 9108 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE; 9109 goto out; 9110 } 9111 9112 /* 9113 * NFS4 only allows locking on regular files, so 9114 * verify type of object. 9115 */ 9116 if (cs->vp->v_type != VREG) { 9117 if (cs->vp->v_type == VDIR) 9118 *cs->statusp = resp->status = NFS4ERR_ISDIR; 9119 else 9120 *cs->statusp = resp->status = NFS4ERR_INVAL; 9121 goto out; 9122 } 9123 9124 /* 9125 * Check out the clientid to ensure the server knows about it 9126 * so that we correctly inform the client of a server reboot. 9127 */ 9128 if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE)) 9129 == NULL) { 9130 *cs->statusp = resp->status = 9131 rfs4_check_clientid(&args->owner.clientid, 0); 9132 goto out; 9133 } 9134 if (rfs4_lease_expired(cp)) { 9135 rfs4_client_close(cp); 9136 /* 9137 * Protocol doesn't allow returning NFS4ERR_STALE as 9138 * other operations do on this check so STALE_CLIENTID 9139 * is returned instead 9140 */ 9141 *cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID; 9142 goto out; 9143 } 9144 9145 if (rfs4_clnt_in_grace(cp) && !(cp->can_reclaim)) { 9146 *cs->statusp = resp->status = NFS4ERR_GRACE; 9147 rfs4_client_rele(cp); 9148 goto out; 9149 } 9150 rfs4_client_rele(cp); 9151 9152 resp->status = NFS4_OK; 9153 9154 switch (args->locktype) { 9155 case READ_LT: 9156 case READW_LT: 9157 ltype = F_RDLCK; 9158 break; 9159 case WRITE_LT: 9160 case WRITEW_LT: 9161 ltype = F_WRLCK; 9162 break; 9163 } 9164 9165 posix_length = args->length; 9166 /* Check for zero length. To lock to end of file use all ones for V4 */ 9167 if (posix_length == 0) { 9168 *cs->statusp = resp->status = NFS4ERR_INVAL; 9169 goto out; 9170 } else if (posix_length == (length4)(~0)) { 9171 posix_length = 0; /* Posix to end of file */ 9172 } 9173 9174 /* Find or create a lockowner */ 9175 lo = rfs4_findlockowner(&args->owner, &create); 9176 9177 if (lo) { 9178 pid = lo->pid; 9179 if ((resp->status = 9180 rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK) 9181 goto err; 9182 } else { 9183 pid = 0; 9184 sysid = lockt_sysid; 9185 } 9186 retry: 9187 flk.l_type = ltype; 9188 flk.l_whence = 0; /* SEEK_SET */ 9189 flk.l_start = args->offset; 9190 flk.l_len = posix_length; 9191 flk.l_sysid = sysid; 9192 flk.l_pid = pid; 9193 flag |= F_REMOTELOCK; 9194 9195 LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk); 9196 9197 /* Note that length4 is uint64_t but l_len and l_start are off64_t */ 9198 if (flk.l_len < 0 || flk.l_start < 0) { 9199 resp->status = NFS4ERR_INVAL; 9200 goto err; 9201 } 9202 error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0, 9203 NULL, cs->cr, NULL); 9204 9205 /* 9206 * N.B. We map error values to nfsv4 errors. This is differrent 9207 * than puterrno4 routine. 9208 */ 9209 switch (error) { 9210 case 0: 9211 if (flk.l_type == F_UNLCK) 9212 resp->status = NFS4_OK; 9213 else { 9214 if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED) 9215 goto retry; 9216 resp->status = NFS4ERR_DENIED; 9217 } 9218 break; 9219 case EOVERFLOW: 9220 resp->status = NFS4ERR_INVAL; 9221 break; 9222 case EINVAL: 9223 resp->status = NFS4ERR_NOTSUPP; 9224 break; 9225 default: 9226 cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)", 9227 error); 9228 resp->status = NFS4ERR_SERVERFAULT; 9229 break; 9230 } 9231 9232 err: 9233 if (lo) 9234 rfs4_lockowner_rele(lo); 9235 *cs->statusp = resp->status; 9236 out: 9237 DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs, 9238 LOCKT4res *, resp); 9239 } 9240 9241 static int 9242 vop_shrlock(vnode_t *vp, int cmd, struct shrlock *sp, int fflags) 9243 { 9244 int err; 9245 9246 if (cmd == F_UNSHARE && sp->s_deny == 0 && sp->s_access == 0) 9247 return (0); 9248 9249 err = VOP_SHRLOCK(vp, cmd, sp, fflags, CRED(), NULL); 9250 9251 NFS4_DEBUG(rfs4_shrlock_debug, 9252 (CE_NOTE, "rfs4_shrlock %s vp=%p acc=%d dny=%d sysid=%d " 9253 "pid=%d err=%d\n", cmd == F_UNSHARE ? "UNSHR" : "SHARE", 9254 (void *) vp, sp->s_access, sp->s_deny, sp->s_sysid, sp->s_pid, 9255 err)); 9256 9257 return (err); 9258 } 9259 9260 static int 9261 rfs4_shrlock(rfs4_state_t *sp, int cmd) 9262 { 9263 struct shrlock shr; 9264 struct shr_locowner shr_loco; 9265 int fflags; 9266 9267 fflags = shr.s_access = shr.s_deny = 0; 9268 9269 if (sp->share_access & OPEN4_SHARE_ACCESS_READ) { 9270 fflags |= FREAD; 9271 shr.s_access |= F_RDACC; 9272 } 9273 if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) { 9274 fflags |= FWRITE; 9275 shr.s_access |= F_WRACC; 9276 } 9277 if (sp->share_deny & OPEN4_SHARE_DENY_READ) 9278 shr.s_deny |= F_RDDNY; 9279 if (sp->share_deny & OPEN4_SHARE_DENY_WRITE) 9280 shr.s_deny |= F_WRDNY; 9281 9282 shr.s_pid = rfs4_dbe_getid(sp->owner->dbe); 9283 shr.s_sysid = sp->owner->client->sysidt; 9284 shr_loco.sl_pid = shr.s_pid; 9285 shr_loco.sl_id = shr.s_sysid; 9286 shr.s_owner = (caddr_t)&shr_loco; 9287 shr.s_own_len = sizeof (shr_loco); 9288 return (vop_shrlock(sp->finfo->vp, cmd, &shr, fflags)); 9289 } 9290 9291 static int 9292 rfs4_share(rfs4_state_t *sp) 9293 { 9294 int cmd; 9295 cmd = nbl_need_check(sp->finfo->vp) ? F_SHARE_NBMAND : F_SHARE; 9296 return (rfs4_shrlock(sp, cmd)); 9297 } 9298 9299 void 9300 rfs4_unshare(rfs4_state_t *sp) 9301 { 9302 (void) rfs4_shrlock(sp, F_UNSHARE); 9303 } 9304 9305 static int 9306 rdma_setup_read_data4(READ4args *args, READ4res *rok) 9307 { 9308 struct clist *wcl; 9309 count4 count = rok->data_len; 9310 int wlist_len; 9311 9312 wcl = args->wlist; 9313 if (rdma_setup_read_chunks(wcl, count, &wlist_len) == FALSE) { 9314 return (FALSE); 9315 } 9316 wcl = args->wlist; 9317 rok->wlist_len = wlist_len; 9318 rok->wlist = wcl; 9319 return (TRUE); 9320 } 9321