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