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