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