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