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