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