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