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