1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #include <sys/cdefs.h>
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39
40 #include <sys/capsicum.h>
41
42 /*
43 * generally, I don't like #includes inside .h files, but it seems to
44 * be the easiest way to handle the port.
45 */
46 #include <sys/fail.h>
47 #include <sys/hash.h>
48 #include <sys/sysctl.h>
49 #include <fs/nfs/nfsport.h>
50 #include <netinet/in_fib.h>
51 #include <netinet/if_ether.h>
52 #include <netinet6/ip6_var.h>
53 #include <net/if_types.h>
54 #include <net/route/nhop.h>
55
56 #include <fs/nfsclient/nfs_kdtrace.h>
57
58 #ifdef KDTRACE_HOOKS
59 dtrace_nfsclient_attrcache_flush_probe_func_t
60 dtrace_nfscl_attrcache_flush_done_probe;
61 uint32_t nfscl_attrcache_flush_done_id;
62
63 dtrace_nfsclient_attrcache_get_hit_probe_func_t
64 dtrace_nfscl_attrcache_get_hit_probe;
65 uint32_t nfscl_attrcache_get_hit_id;
66
67 dtrace_nfsclient_attrcache_get_miss_probe_func_t
68 dtrace_nfscl_attrcache_get_miss_probe;
69 uint32_t nfscl_attrcache_get_miss_id;
70
71 dtrace_nfsclient_attrcache_load_probe_func_t
72 dtrace_nfscl_attrcache_load_done_probe;
73 uint32_t nfscl_attrcache_load_done_id;
74 #endif /* !KDTRACE_HOOKS */
75
76 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
77 extern struct vop_vector newnfs_vnodeops;
78 extern struct vop_vector newnfs_fifoops;
79 extern uma_zone_t newnfsnode_zone;
80 extern uma_zone_t ncl_pbuf_zone;
81 extern short nfsv4_cbport;
82 extern int nfscl_enablecallb;
83 extern int nfs_numnfscbd;
84 extern int nfscl_inited;
85 struct mtx ncl_iod_mutex;
86 NFSDLOCKMUTEX;
87 extern struct mtx nfsrv_dslock_mtx;
88
89 extern void (*ncl_call_invalcaches)(struct vnode *);
90
91 SYSCTL_DECL(_vfs_nfs);
92 static int ncl_fileid_maxwarnings = 10;
93 SYSCTL_INT(_vfs_nfs, OID_AUTO, fileid_maxwarnings, CTLFLAG_RWTUN,
94 &ncl_fileid_maxwarnings, 0,
95 "Limit fileid corruption warnings; 0 is off; -1 is unlimited");
96 static volatile int ncl_fileid_nwarnings;
97
98 static void nfscl_warn_fileid(struct nfsmount *, struct nfsvattr *,
99 struct nfsvattr *);
100
101 /*
102 * Comparison function for vfs_hash functions.
103 */
104 int
newnfs_vncmpf(struct vnode * vp,void * arg)105 newnfs_vncmpf(struct vnode *vp, void *arg)
106 {
107 struct nfsfh *nfhp = (struct nfsfh *)arg;
108 struct nfsnode *np = VTONFS(vp);
109
110 if (np->n_fhp->nfh_len != nfhp->nfh_len ||
111 NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
112 return (1);
113 return (0);
114 }
115
116 /*
117 * Look up a vnode/nfsnode by file handle.
118 * Callers must check for mount points!!
119 * In all cases, a pointer to a
120 * nfsnode structure is returned.
121 * This variant takes a "struct nfsfh *" as second argument and uses
122 * that structure up, either by hanging off the nfsnode or FREEing it.
123 */
124 int
nfscl_nget(struct mount * mntp,struct vnode * dvp,struct nfsfh * nfhp,struct componentname * cnp,struct thread * td,struct nfsnode ** npp,int lkflags)125 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
126 struct componentname *cnp, struct thread *td, struct nfsnode **npp,
127 int lkflags)
128 {
129 struct nfsnode *np, *dnp;
130 struct vnode *vp, *nvp;
131 struct nfsv4node *newd, *oldd;
132 int error;
133 u_int hash;
134 struct nfsmount *nmp;
135
136 nmp = VFSTONFS(mntp);
137 dnp = VTONFS(dvp);
138 *npp = NULL;
139
140 /*
141 * If this is the mount point fh and NFSMNTP_FAKEROOT is set, replace
142 * it with the fake fh.
143 */
144 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
145 nmp->nm_fhsize > 0 && nmp->nm_fhsize == nfhp->nfh_len &&
146 !NFSBCMP(nmp->nm_fh, nfhp->nfh_fh, nmp->nm_fhsize)) {
147 free(nfhp, M_NFSFH);
148 nfhp = malloc(sizeof(struct nfsfh) + NFSX_FHMAX + 1,
149 M_NFSFH, M_WAITOK | M_ZERO);
150 nfhp->nfh_len = NFSX_FHMAX + 1;
151 }
152
153 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
154
155 error = vfs_hash_get(mntp, hash, lkflags,
156 td, &nvp, newnfs_vncmpf, nfhp);
157 if (error == 0 && nvp != NULL) {
158 /*
159 * I believe there is a slight chance that vgonel() could
160 * get called on this vnode between when NFSVOPLOCK() drops
161 * the VI_LOCK() and vget() acquires it again, so that it
162 * hasn't yet had v_usecount incremented. If this were to
163 * happen, the VIRF_DOOMED flag would be set, so check for
164 * that here. Since we now have the v_usecount incremented,
165 * we should be ok until we vrele() it, if the VIRF_DOOMED
166 * flag isn't set now.
167 */
168 VI_LOCK(nvp);
169 if (VN_IS_DOOMED(nvp)) {
170 VI_UNLOCK(nvp);
171 vrele(nvp);
172 error = ENOENT;
173 } else {
174 VI_UNLOCK(nvp);
175 }
176 }
177 if (error) {
178 free(nfhp, M_NFSFH);
179 return (error);
180 }
181 if (nvp != NULL) {
182 np = VTONFS(nvp);
183 /*
184 * For NFSv4, check to see if it is the same name and
185 * replace the name, if it is different.
186 */
187 oldd = newd = NULL;
188 if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
189 nvp->v_type == VREG &&
190 (np->n_v4->n4_namelen != cnp->cn_namelen ||
191 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
192 cnp->cn_namelen) ||
193 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
194 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
195 dnp->n_fhp->nfh_len))) {
196 newd = malloc(
197 sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
198 + cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
199 NFSLOCKNODE(np);
200 if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
201 && (np->n_v4->n4_namelen != cnp->cn_namelen ||
202 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
203 cnp->cn_namelen) ||
204 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
205 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
206 dnp->n_fhp->nfh_len))) {
207 oldd = np->n_v4;
208 np->n_v4 = newd;
209 newd = NULL;
210 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
211 np->n_v4->n4_namelen = cnp->cn_namelen;
212 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
213 dnp->n_fhp->nfh_len);
214 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
215 cnp->cn_namelen);
216 }
217 NFSUNLOCKNODE(np);
218 }
219 if (newd != NULL)
220 free(newd, M_NFSV4NODE);
221 if (oldd != NULL)
222 free(oldd, M_NFSV4NODE);
223 *npp = np;
224 free(nfhp, M_NFSFH);
225 return (0);
226 }
227 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
228
229 error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp);
230 if (error) {
231 uma_zfree(newnfsnode_zone, np);
232 free(nfhp, M_NFSFH);
233 return (error);
234 }
235 vp = nvp;
236 KASSERT(vp->v_bufobj.bo_bsize != 0, ("nfscl_nget: bo_bsize == 0"));
237 vp->v_data = np;
238 np->n_vnode = vp;
239 /*
240 * Initialize the mutex even if the vnode is going to be a loser.
241 * This simplifies the logic in reclaim, which can then unconditionally
242 * destroy the mutex (in the case of the loser, or if hash_insert
243 * happened to return an error no special casing is needed).
244 */
245 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
246 lockinit(&np->n_excl, PVFS, "nfsupg", VLKTIMEOUT, LK_NOSHARE |
247 LK_CANRECURSE);
248
249 /*
250 * Are we getting the root? If so, make sure the vnode flags
251 * are correct
252 */
253 if (nfhp->nfh_len == NFSX_FHMAX + 1 ||
254 (nfhp->nfh_len == nmp->nm_fhsize &&
255 !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len))) {
256 if (vp->v_type == VNON)
257 vp->v_type = VDIR;
258 vp->v_vflag |= VV_ROOT;
259 }
260
261 vp->v_vflag |= VV_VMSIZEVNLOCK;
262
263 np->n_fhp = nfhp;
264 /*
265 * For NFSv4.0, we have to attach the directory file handle and
266 * file name, so that Open Ops can be done later.
267 */
268 if (NFSHASNFSV4(nmp) && !NFSHASNFSV4N(nmp)) {
269 np->n_v4 = malloc(sizeof (struct nfsv4node)
270 + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
271 M_WAITOK);
272 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
273 np->n_v4->n4_namelen = cnp->cn_namelen;
274 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
275 dnp->n_fhp->nfh_len);
276 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
277 cnp->cn_namelen);
278 } else {
279 np->n_v4 = NULL;
280 }
281
282 /*
283 * NFS supports recursive and shared locking.
284 */
285 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
286 VN_LOCK_AREC(vp);
287 VN_LOCK_ASHARE(vp);
288 error = insmntque(vp, mntp);
289 if (error != 0) {
290 *npp = NULL;
291 mtx_destroy(&np->n_mtx);
292 lockdestroy(&np->n_excl);
293 free(nfhp, M_NFSFH);
294 if (np->n_v4 != NULL)
295 free(np->n_v4, M_NFSV4NODE);
296 uma_zfree(newnfsnode_zone, np);
297 return (error);
298 }
299 vn_set_state(vp, VSTATE_CONSTRUCTED);
300 error = vfs_hash_insert(vp, hash, lkflags,
301 td, &nvp, newnfs_vncmpf, nfhp);
302 if (error)
303 return (error);
304 if (nvp != NULL) {
305 *npp = VTONFS(nvp);
306 /* vfs_hash_insert() vput()'s the losing vnode */
307 return (0);
308 }
309 *npp = np;
310
311 return (0);
312 }
313
314 /*
315 * Another variant of nfs_nget(). This one is only used by reopen. It
316 * takes almost the same args as nfs_nget(), but only succeeds if an entry
317 * exists in the cache. (Since files should already be "open" with a
318 * vnode ref cnt on the node when reopen calls this, it should always
319 * succeed.)
320 * Also, don't get a vnode lock, since it may already be locked by some
321 * other process that is handling it. This is ok, since all other threads
322 * on the client are blocked by the nfsc_lock being exclusively held by the
323 * caller of this function.
324 */
325 int
nfscl_ngetreopen(struct mount * mntp,u_int8_t * fhp,int fhsize,struct thread * td,struct nfsnode ** npp)326 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
327 struct thread *td, struct nfsnode **npp)
328 {
329 struct vnode *nvp;
330 u_int hash;
331 struct nfsfh *nfhp;
332 int error;
333
334 *npp = NULL;
335 /* For forced dismounts, just return error. */
336 if (NFSCL_FORCEDISM(mntp))
337 return (EINTR);
338 nfhp = malloc(sizeof (struct nfsfh) + fhsize,
339 M_NFSFH, M_WAITOK);
340 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
341 nfhp->nfh_len = fhsize;
342
343 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
344
345 /*
346 * First, try to get the vnode locked, but don't block for the lock.
347 */
348 error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
349 newnfs_vncmpf, nfhp);
350 if (error == 0 && nvp != NULL) {
351 NFSVOPUNLOCK(nvp);
352 } else if (error == EBUSY) {
353 /*
354 * It is safe so long as a vflush() with
355 * FORCECLOSE has not been done. Since the Renew thread is
356 * stopped and the MNTK_UNMOUNTF flag is set before doing
357 * a vflush() with FORCECLOSE, we should be ok here.
358 */
359 if (NFSCL_FORCEDISM(mntp))
360 error = EINTR;
361 else {
362 vfs_hash_ref(mntp, hash, td, &nvp, newnfs_vncmpf, nfhp);
363 if (nvp == NULL) {
364 error = ENOENT;
365 } else if (VN_IS_DOOMED(nvp)) {
366 error = ENOENT;
367 vrele(nvp);
368 } else {
369 error = 0;
370 }
371 }
372 }
373 free(nfhp, M_NFSFH);
374 if (error)
375 return (error);
376 if (nvp != NULL) {
377 *npp = VTONFS(nvp);
378 return (0);
379 }
380 return (EINVAL);
381 }
382
383 static void
nfscl_warn_fileid(struct nfsmount * nmp,struct nfsvattr * oldnap,struct nfsvattr * newnap)384 nfscl_warn_fileid(struct nfsmount *nmp, struct nfsvattr *oldnap,
385 struct nfsvattr *newnap)
386 {
387 int off;
388
389 if (ncl_fileid_maxwarnings >= 0 &&
390 ncl_fileid_nwarnings >= ncl_fileid_maxwarnings)
391 return;
392 off = 0;
393 if (ncl_fileid_maxwarnings >= 0) {
394 if (++ncl_fileid_nwarnings >= ncl_fileid_maxwarnings)
395 off = 1;
396 }
397
398 printf("newnfs: server '%s' error: fileid changed. "
399 "fsid %jx:%jx: expected fileid %#jx, got %#jx. "
400 "(BROKEN NFS SERVER OR MIDDLEWARE)\n",
401 nmp->nm_com.nmcom_hostname,
402 (uintmax_t)nmp->nm_fsid[0],
403 (uintmax_t)nmp->nm_fsid[1],
404 (uintmax_t)oldnap->na_fileid,
405 (uintmax_t)newnap->na_fileid);
406
407 if (off)
408 printf("newnfs: Logged %d times about fileid corruption; "
409 "going quiet to avoid spamming logs excessively. (Limit "
410 "is: %d).\n", ncl_fileid_nwarnings,
411 ncl_fileid_maxwarnings);
412 }
413
414 void
ncl_copy_vattr(struct vnode * vp,struct vattr * dst,struct vattr * src)415 ncl_copy_vattr(struct vnode *vp, struct vattr *dst, struct vattr *src)
416 {
417 dst->va_type = src->va_type;
418 dst->va_mode = src->va_mode;
419 dst->va_nlink = src->va_nlink;
420 dst->va_uid = src->va_uid;
421 dst->va_gid = src->va_gid;
422 dst->va_fsid = src->va_fsid;
423 dst->va_fileid = src->va_fileid;
424 dst->va_size = src->va_size;
425 dst->va_blocksize = src->va_blocksize;
426 dst->va_atime = src->va_atime;
427 dst->va_mtime = src->va_mtime;
428 dst->va_ctime = src->va_ctime;
429 dst->va_birthtime = src->va_birthtime;
430 dst->va_gen = src->va_gen;
431 dst->va_flags = src->va_flags;
432 dst->va_rdev = VN_ISDEV(vp) ? src->va_rdev : NODEV;
433 dst->va_bytes = src->va_bytes;
434 dst->va_filerev = src->va_filerev;
435 }
436
437 /*
438 * Load the attribute cache (that lives in the nfsnode entry) with
439 * the attributes of the second argument and
440 * Iff vaper not NULL
441 * copy the attributes to *vaper
442 * Similar to nfs_loadattrcache(), except the attributes are passed in
443 * instead of being parsed out of the mbuf list.
444 */
445 int
nfscl_loadattrcache(struct vnode ** vpp,struct nfsvattr * nap,void * nvaper,int writeattr,int dontshrink)446 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
447 int writeattr, int dontshrink)
448 {
449 struct vnode *vp = *vpp;
450 struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
451 struct nfsnode *np;
452 struct nfsmount *nmp;
453 struct timespec mtime_save;
454 int error, force_fid_err;
455 dev_t topfsid;
456
457 error = 0;
458
459 /*
460 * If v_type == VNON it is a new node, so fill in the v_type,
461 * n_mtime fields. Check to see if it represents a special
462 * device, and if so, check for a possible alias. Once the
463 * correct vnode has been obtained, fill in the rest of the
464 * information.
465 */
466 np = VTONFS(vp);
467 NFSLOCKNODE(np);
468 if (vp->v_type != nvap->va_type) {
469 vp->v_type = nvap->va_type;
470 if (vp->v_type == VFIFO)
471 vp->v_op = &newnfs_fifoops;
472 np->n_mtime = nvap->va_mtime;
473 }
474 nmp = VFSTONFS(vp->v_mount);
475 vap = &np->n_vattr.na_vattr;
476 mtime_save = vap->va_mtime;
477 if (writeattr) {
478 np->n_vattr.na_filerev = nap->na_filerev;
479 np->n_vattr.na_size = nap->na_size;
480 np->n_vattr.na_mtime = nap->na_mtime;
481 np->n_vattr.na_ctime = nap->na_ctime;
482 np->n_vattr.na_btime = nap->na_btime;
483 np->n_vattr.na_fsid = nap->na_fsid;
484 np->n_vattr.na_mode = nap->na_mode;
485 } else {
486 force_fid_err = 0;
487 KFAIL_POINT_ERROR(DEBUG_FP, nfscl_force_fileid_warning,
488 force_fid_err);
489 /*
490 * BROKEN NFS SERVER OR MIDDLEWARE
491 *
492 * Certain NFS servers (certain old proprietary filers ca.
493 * 2006) or broken middleboxes (e.g. WAN accelerator products)
494 * will respond to GETATTR requests with results for a
495 * different fileid.
496 *
497 * The WAN accelerator we've observed not only serves stale
498 * cache results for a given file, it also occasionally serves
499 * results for wholly different files. This causes surprising
500 * problems; for example the cached size attribute of a file
501 * may truncate down and then back up, resulting in zero
502 * regions in file contents read by applications. We observed
503 * this reliably with Clang and .c files during parallel build.
504 * A pcap revealed packet fragmentation and GETATTR RPC
505 * responses with wholly wrong fileids.
506 * For the case where the file handle is a fake one
507 * generated via the "syskrb5" mount option and
508 * the old fileid is 2, ignore the test, since this might
509 * be replacing the fake attributes with correct ones.
510 */
511 if ((np->n_vattr.na_fileid != 0 &&
512 np->n_vattr.na_fileid != nap->na_fileid &&
513 (np->n_vattr.na_fileid != 2 || !NFSHASSYSKRB5(nmp) ||
514 np->n_fhp->nfh_len != NFSX_FHMAX + 1)) ||
515 force_fid_err) {
516 nfscl_warn_fileid(nmp, &np->n_vattr, nap);
517 error = EIDRM;
518 goto out;
519 }
520 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
521 sizeof (struct nfsvattr));
522 }
523
524 /*
525 * For NFSv4, the server's export may be a tree of file systems
526 * where a fileno is a unique value within each file system.
527 * na_filesid[0,1] uniquely identify the server file system
528 * and nm_fsid[0,1] is the value for the root file system mounted.
529 * As such, the value of va_fsid generated by vn_fsid() represents
530 * the root file system on the server and a different value for
531 * va_fsid is needed for the other server file systems. This
532 * va_fsid is ideally unique for all of the server file systems,
533 * so a 64bit hash on na_filesid[0,1] is calculated.
534 * Although highly unlikely that the fnv_64_hash() will be
535 * the same as the root, test for this case and recalculate the hash.
536 */
537 vn_fsid(vp, vap);
538 if (NFSHASNFSV4(nmp) && NFSHASHASSETFSID(nmp) &&
539 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
540 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1])) {
541 topfsid = vap->va_fsid;
542 vap->va_fsid = FNV1_64_INIT;
543 do {
544 vap->va_fsid = fnv_64_buf(np->n_vattr.na_filesid,
545 sizeof(np->n_vattr.na_filesid), vap->va_fsid);
546 } while (vap->va_fsid == topfsid);
547 }
548
549 np->n_attrstamp = time_second;
550 if (vap->va_size != np->n_size) {
551 if (vap->va_type == VREG) {
552 if (dontshrink && vap->va_size < np->n_size) {
553 /*
554 * We've been told not to shrink the file;
555 * zero np->n_attrstamp to indicate that
556 * the attributes are stale.
557 */
558 vap->va_size = np->n_size;
559 np->n_attrstamp = 0;
560 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
561 } else if (np->n_flag & NMODIFIED) {
562 /*
563 * We've modified the file: Use the larger
564 * of our size, and the server's size.
565 */
566 if (vap->va_size < np->n_size) {
567 vap->va_size = np->n_size;
568 } else {
569 np->n_size = vap->va_size;
570 np->n_flag |= NSIZECHANGED;
571 }
572 } else {
573 np->n_size = vap->va_size;
574 np->n_flag |= NSIZECHANGED;
575 }
576 } else {
577 np->n_size = vap->va_size;
578 }
579 }
580 /*
581 * The following checks are added to prevent a race between (say)
582 * a READDIR+ and a WRITE.
583 * READDIR+, WRITE requests sent out.
584 * READDIR+ resp, WRITE resp received on client.
585 * However, the WRITE resp was handled before the READDIR+ resp
586 * causing the post op attrs from the write to be loaded first
587 * and the attrs from the READDIR+ to be loaded later. If this
588 * happens, we have stale attrs loaded into the attrcache.
589 * We detect this by for the mtime moving back. We invalidate the
590 * attrcache when this happens.
591 */
592 if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
593 /* Size changed or mtime went backwards */
594 np->n_attrstamp = 0;
595 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
596 }
597 if (vaper != NULL) {
598 ncl_copy_vattr(vp, vaper, vap);
599 if (np->n_flag & NCHG) {
600 if (np->n_flag & NACC)
601 vaper->va_atime = np->n_atim;
602 if (np->n_flag & NUPD)
603 vaper->va_mtime = np->n_mtim;
604 }
605 }
606
607 out:
608 #ifdef KDTRACE_HOOKS
609 if (np->n_attrstamp != 0)
610 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, vap, error);
611 #endif
612 (void)ncl_pager_setsize(vp, NULL);
613 return (error);
614 }
615
616 /*
617 * Call vnode_pager_setsize() if the size of the node changed, as
618 * recorded in nfsnode vs. v_object, or delay the call if notifying
619 * the pager is not possible at the moment.
620 *
621 * If nsizep is non-NULL, the call is delayed and the new node size is
622 * provided. Caller should itself call vnode_pager_setsize() if
623 * function returned true. If nsizep is NULL, function tries to call
624 * vnode_pager_setsize() itself if needed and possible, and the nfs
625 * node is unlocked unconditionally, the return value is not useful.
626 */
627 bool
ncl_pager_setsize(struct vnode * vp,u_quad_t * nsizep)628 ncl_pager_setsize(struct vnode *vp, u_quad_t *nsizep)
629 {
630 struct nfsnode *np;
631 vm_object_t object;
632 struct vattr *vap;
633 u_quad_t nsize;
634 bool setnsize;
635
636 np = VTONFS(vp);
637 NFSASSERTNODE(np);
638
639 vap = &np->n_vattr.na_vattr;
640 nsize = vap->va_size;
641 object = vp->v_object;
642 setnsize = false;
643
644 if (object != NULL && nsize != object->un_pager.vnp.vnp_size) {
645 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
646 (curthread->td_pflags2 & TDP2_SBPAGES) == 0)
647 setnsize = true;
648 else
649 vn_delayed_setsize(vp);
650 }
651 if (nsizep == NULL) {
652 NFSUNLOCKNODE(np);
653 if (setnsize)
654 vnode_pager_setsize(vp, nsize);
655 setnsize = false;
656 } else {
657 *nsizep = nsize;
658 }
659 return (setnsize);
660 }
661
662 /*
663 * If the uuid passed in is the DEFAULT_UUID, try and find an
664 * alternate to replace it with.
665 * If no alternate is available, set uuid to "" so that nfscl_fillclid()
666 * will use random bytes.
667 */
668 void
nfscl_uuidcheck(char * uuid)669 nfscl_uuidcheck(char *uuid)
670 {
671 int ucplen, uuidlen;
672 char *ucp;
673
674 /*
675 * If the uuid is the DEFAULT_UUID, try and get an alternative.
676 */
677 uuidlen = strlen(uuid);
678 ucp = NULL;
679 if (uuidlen == strlen(DEFAULT_HOSTUUID) &&
680 NFSBCMP(uuid, DEFAULT_HOSTUUID, uuidlen) == 0) {
681 *uuid = '\0';
682 /* Use smbios.system.uuid if it exists. */
683 if ((ucp = kern_getenv("smbios.system.uuid")) != NULL) {
684 ucplen = strlen(ucp);
685 if (ucplen < HOSTUUIDLEN && ucplen > 0)
686 strlcpy(uuid, ucp, HOSTUUIDLEN);
687 }
688 }
689 if (ucp != NULL)
690 freeenv(ucp);
691 }
692
693 /*
694 * Fill in the client id name. For these bytes:
695 * 1 - they must be unique
696 * 2 - they should be persistent across client reboots
697 * 1 is more critical than 2
698 * Use the mount point's unique id plus either the uuid or, if that
699 * isn't set, random junk.
700 */
701 void
nfscl_fillclid(u_int64_t clval,char * uuid,u_int8_t * cp,u_int16_t idlen)702 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
703 {
704 int uuidlen;
705
706 /*
707 * First, put in the 64bit mount point identifier.
708 */
709 if (idlen >= sizeof (u_int64_t)) {
710 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
711 cp += sizeof (u_int64_t);
712 idlen -= sizeof (u_int64_t);
713 }
714
715 /*
716 * If uuid is non-zero length, use it.
717 */
718 uuidlen = strlen(uuid);
719 if (uuidlen > 0 && idlen >= uuidlen) {
720 NFSBCOPY(uuid, cp, uuidlen);
721 cp += uuidlen;
722 idlen -= uuidlen;
723 }
724
725 /*
726 * This only normally happens if the uuid isn't set.
727 */
728 while (idlen > 0) {
729 *cp++ = (u_int8_t)(arc4random() % 256);
730 idlen--;
731 }
732 }
733
734 /*
735 * Fill in a lock owner name. For now, pid + the process's creation time.
736 */
737 void
nfscl_filllockowner(void * id,u_int8_t * cp,int flags)738 nfscl_filllockowner(void *id, u_int8_t *cp, int flags)
739 {
740 union {
741 u_int32_t lval;
742 u_int8_t cval[4];
743 } tl;
744 struct proc *p;
745
746 if (id == NULL) {
747 /* Return the single open_owner of all 0 bytes. */
748 bzero(cp, NFSV4CL_LOCKNAMELEN);
749 return;
750 }
751 if ((flags & F_POSIX) != 0) {
752 p = (struct proc *)id;
753 tl.lval = p->p_pid;
754 *cp++ = tl.cval[0];
755 *cp++ = tl.cval[1];
756 *cp++ = tl.cval[2];
757 *cp++ = tl.cval[3];
758 tl.lval = p->p_stats->p_start.tv_sec;
759 *cp++ = tl.cval[0];
760 *cp++ = tl.cval[1];
761 *cp++ = tl.cval[2];
762 *cp++ = tl.cval[3];
763 tl.lval = p->p_stats->p_start.tv_usec;
764 *cp++ = tl.cval[0];
765 *cp++ = tl.cval[1];
766 *cp++ = tl.cval[2];
767 *cp = tl.cval[3];
768 } else if ((flags & F_FLOCK) != 0) {
769 bcopy(&id, cp, sizeof(id));
770 bzero(&cp[sizeof(id)], NFSV4CL_LOCKNAMELEN - sizeof(id));
771 } else {
772 printf("nfscl_filllockowner: not F_POSIX or F_FLOCK\n");
773 bzero(cp, NFSV4CL_LOCKNAMELEN);
774 }
775 }
776
777 /*
778 * Find the parent process for the thread passed in as an argument.
779 * If none exists, return NULL, otherwise return a thread for the parent.
780 * (Can be any of the threads, since it is only used for td->td_proc.)
781 */
782 NFSPROC_T *
nfscl_getparent(struct thread * td)783 nfscl_getparent(struct thread *td)
784 {
785 struct proc *p;
786 struct thread *ptd;
787
788 if (td == NULL)
789 return (NULL);
790 p = td->td_proc;
791 if (p->p_pid == 0)
792 return (NULL);
793 p = p->p_pptr;
794 if (p == NULL)
795 return (NULL);
796 ptd = TAILQ_FIRST(&p->p_threads);
797 return (ptd);
798 }
799
800 /*
801 * Start up the renew kernel thread.
802 */
803 static void
start_nfscl(void * arg)804 start_nfscl(void *arg)
805 {
806 struct nfsclclient *clp;
807 struct thread *td;
808
809 clp = (struct nfsclclient *)arg;
810 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
811 nfscl_renewthread(clp, td);
812 kproc_exit(0);
813 }
814
815 void
nfscl_start_renewthread(struct nfsclclient * clp)816 nfscl_start_renewthread(struct nfsclclient *clp)
817 {
818
819 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
820 "nfscl");
821 }
822
823 /*
824 * Handle wcc_data.
825 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
826 * as the first Op after PutFH.
827 * (For NFSv4, the postop attributes are after the Op, so they can't be
828 * parsed here. A separate call to nfscl_postop_attr() is required.)
829 */
830 int
nfscl_wcc_data(struct nfsrv_descript * nd,struct vnode * vp,struct nfsvattr * nap,int * flagp,int * wccflagp,uint64_t * repsizep)831 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
832 struct nfsvattr *nap, int *flagp, int *wccflagp, uint64_t *repsizep)
833 {
834 u_int32_t *tl;
835 struct nfsnode *np = VTONFS(vp);
836 struct nfsvattr nfsva;
837 int error = 0;
838
839 if (wccflagp != NULL)
840 *wccflagp = 0;
841 if (nd->nd_flag & ND_NFSV3) {
842 *flagp = 0;
843 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
844 if (*tl == newnfs_true) {
845 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
846 if (wccflagp != NULL) {
847 NFSLOCKNODE(np);
848 *wccflagp = (np->n_mtime.tv_sec ==
849 fxdr_unsigned(u_int32_t, *(tl + 2)) &&
850 np->n_mtime.tv_nsec ==
851 fxdr_unsigned(u_int32_t, *(tl + 3)));
852 NFSUNLOCKNODE(np);
853 }
854 }
855 error = nfscl_postop_attr(nd, nap, flagp);
856 if (wccflagp != NULL && *flagp == 0)
857 *wccflagp = 0;
858 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
859 == (ND_NFSV4 | ND_V4WCCATTR)) {
860 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
861 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
862 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
863 if (error)
864 return (error);
865 /*
866 * Get rid of Op# and status for next op.
867 */
868 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
869 if (*++tl)
870 nd->nd_flag |= ND_NOMOREDATA;
871 if (repsizep != NULL)
872 *repsizep = nfsva.na_size;
873 if (wccflagp != NULL &&
874 nfsva.na_vattr.va_mtime.tv_sec != 0) {
875 NFSLOCKNODE(np);
876 *wccflagp = (np->n_mtime.tv_sec ==
877 nfsva.na_vattr.va_mtime.tv_sec &&
878 np->n_mtime.tv_nsec ==
879 nfsva.na_vattr.va_mtime.tv_sec);
880 NFSUNLOCKNODE(np);
881 }
882 }
883 nfsmout:
884 return (error);
885 }
886
887 /*
888 * Get postop attributes.
889 */
890 int
nfscl_postop_attr(struct nfsrv_descript * nd,struct nfsvattr * nap,int * retp)891 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp)
892 {
893 u_int32_t *tl;
894 int error = 0;
895
896 *retp = 0;
897 if (nd->nd_flag & ND_NOMOREDATA)
898 return (error);
899 if (nd->nd_flag & ND_NFSV3) {
900 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
901 *retp = fxdr_unsigned(int, *tl);
902 } else if (nd->nd_flag & ND_NFSV4) {
903 /*
904 * For NFSv4, the postop attr are at the end, so no point
905 * in looking if nd_repstat != 0.
906 */
907 if (!nd->nd_repstat) {
908 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
909 if (*(tl + 1))
910 /* should never happen since nd_repstat != 0 */
911 nd->nd_flag |= ND_NOMOREDATA;
912 else
913 *retp = 1;
914 }
915 } else if (!nd->nd_repstat) {
916 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
917 *retp = 1;
918 }
919 if (*retp) {
920 error = nfsm_loadattr(nd, nap);
921 if (error)
922 *retp = 0;
923 }
924 nfsmout:
925 return (error);
926 }
927
928 /*
929 * nfscl_request() - mostly a wrapper for newnfs_request().
930 */
931 int
nfscl_request(struct nfsrv_descript * nd,struct vnode * vp,NFSPROC_T * p,struct ucred * cred)932 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
933 struct ucred *cred)
934 {
935 int ret, vers;
936 struct nfsmount *nmp;
937
938 nmp = VFSTONFS(vp->v_mount);
939 if (nd->nd_flag & ND_NFSV4)
940 vers = NFS_VER4;
941 else if (nd->nd_flag & ND_NFSV3)
942 vers = NFS_VER3;
943 else
944 vers = NFS_VER2;
945 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
946 NFS_PROG, vers, NULL, 1, NULL, NULL);
947 return (ret);
948 }
949
950 /*
951 * fill in this bsden's variant of statfs using nfsstatfs.
952 */
953 void
nfscl_loadsbinfo(struct nfsmount * nmp,struct nfsstatfs * sfp,void * statfs)954 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
955 {
956 struct statfs *sbp = (struct statfs *)statfs;
957
958 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
959 sbp->f_bsize = NFS_FABLKSIZE;
960 sbp->f_blocks = sfp->sf_tbytes / NFS_FABLKSIZE;
961 sbp->f_bfree = sfp->sf_fbytes / NFS_FABLKSIZE;
962 /*
963 * Although sf_abytes is uint64_t and f_bavail is int64_t,
964 * the value after dividing by NFS_FABLKSIZE is small
965 * enough that it will fit in 63bits, so it is ok to
966 * assign it to f_bavail without fear that it will become
967 * negative.
968 */
969 sbp->f_bavail = sfp->sf_abytes / NFS_FABLKSIZE;
970 sbp->f_files = sfp->sf_tfiles;
971 /* Since f_ffree is int64_t, clip it to 63bits. */
972 if (sfp->sf_ffiles > INT64_MAX)
973 sbp->f_ffree = INT64_MAX;
974 else
975 sbp->f_ffree = sfp->sf_ffiles;
976 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
977 /*
978 * The type casts to (int32_t) ensure that this code is
979 * compatible with the old NFS client, in that it will
980 * propagate bit31 to the high order bits. This may or may
981 * not be correct for NFSv2, but since it is a legacy
982 * environment, I'd rather retain backwards compatibility.
983 */
984 sbp->f_bsize = (int32_t)sfp->sf_bsize;
985 sbp->f_blocks = (int32_t)sfp->sf_blocks;
986 sbp->f_bfree = (int32_t)sfp->sf_bfree;
987 sbp->f_bavail = (int32_t)sfp->sf_bavail;
988 sbp->f_files = 0;
989 sbp->f_ffree = 0;
990 }
991 }
992
993 /*
994 * Use the fsinfo stuff to update the mount point.
995 */
996 void
nfscl_loadfsinfo(struct nfsmount * nmp,struct nfsfsinfo * fsp,uint32_t clone_blksize)997 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp,
998 uint32_t clone_blksize)
999 {
1000
1001 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
1002 fsp->fs_wtpref >= NFS_FABLKSIZE)
1003 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
1004 ~(NFS_FABLKSIZE - 1);
1005 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
1006 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
1007 if (nmp->nm_wsize == 0)
1008 nmp->nm_wsize = fsp->fs_wtmax;
1009 }
1010 if (nmp->nm_wsize < NFS_FABLKSIZE)
1011 nmp->nm_wsize = NFS_FABLKSIZE;
1012 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
1013 fsp->fs_rtpref >= NFS_FABLKSIZE)
1014 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
1015 ~(NFS_FABLKSIZE - 1);
1016 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
1017 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
1018 if (nmp->nm_rsize == 0)
1019 nmp->nm_rsize = fsp->fs_rtmax;
1020 }
1021 if (nmp->nm_rsize < NFS_FABLKSIZE)
1022 nmp->nm_rsize = NFS_FABLKSIZE;
1023 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
1024 && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
1025 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
1026 ~(NFS_DIRBLKSIZ - 1);
1027 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
1028 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
1029 if (nmp->nm_readdirsize == 0)
1030 nmp->nm_readdirsize = fsp->fs_rtmax;
1031 }
1032 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
1033 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
1034 if (fsp->fs_maxfilesize > 0 &&
1035 fsp->fs_maxfilesize < nmp->nm_maxfilesize)
1036 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
1037 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
1038
1039 /*
1040 * Although ZFS reports a clone_blksize of 16Mbytes,
1041 * 128Kbytes usually works, so set it to that.
1042 */
1043 if (clone_blksize > 128 * 1024)
1044 clone_blksize = 128 * 1024;
1045 nmp->nm_cloneblksize = clone_blksize;
1046 nmp->nm_state |= NFSSTA_GOTFSINFO;
1047 }
1048
1049 /*
1050 * Lookups source address which should be used to communicate with
1051 * @nmp and stores it inside @pdst.
1052 *
1053 * Returns 0 on success.
1054 */
1055 u_int8_t *
nfscl_getmyip(struct nfsmount * nmp,struct in6_addr * paddr,int * isinet6p)1056 nfscl_getmyip(struct nfsmount *nmp, struct in6_addr *paddr, int *isinet6p)
1057 {
1058 #if defined(INET6) || defined(INET)
1059 int fibnum;
1060
1061 fibnum = curthread->td_proc->p_fibnum;
1062 #endif
1063 #ifdef INET
1064 if (nmp->nm_nam->sa_family == AF_INET) {
1065 struct epoch_tracker et;
1066 struct nhop_object *nh;
1067 struct sockaddr_in *sin;
1068 struct in_addr addr = {};
1069
1070 sin = (struct sockaddr_in *)nmp->nm_nam;
1071 NET_EPOCH_ENTER(et);
1072 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
1073 nh = fib4_lookup(fibnum, sin->sin_addr, 0, NHR_NONE, 0);
1074 if (nh != NULL) {
1075 addr = IA_SIN(ifatoia(nh->nh_ifa))->sin_addr;
1076 if (IN_LOOPBACK(ntohl(addr.s_addr))) {
1077 /* Ignore loopback addresses */
1078 nh = NULL;
1079 }
1080 }
1081 CURVNET_RESTORE();
1082 NET_EPOCH_EXIT(et);
1083
1084 if (nh == NULL)
1085 return (NULL);
1086 *isinet6p = 0;
1087 *((struct in_addr *)paddr) = addr;
1088
1089 return (u_int8_t *)paddr;
1090 }
1091 #endif
1092 #ifdef INET6
1093 if (nmp->nm_nam->sa_family == AF_INET6) {
1094 struct epoch_tracker et;
1095 struct sockaddr_in6 *sin6;
1096 int error;
1097
1098 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
1099
1100 NET_EPOCH_ENTER(et);
1101 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
1102 error = in6_selectsrc_addr(fibnum, &sin6->sin6_addr,
1103 sin6->sin6_scope_id, NULL, paddr, NULL);
1104 CURVNET_RESTORE();
1105 NET_EPOCH_EXIT(et);
1106 if (error != 0)
1107 return (NULL);
1108
1109 if (IN6_IS_ADDR_LOOPBACK(paddr))
1110 return (NULL);
1111
1112 /* Scope is embedded in */
1113 *isinet6p = 1;
1114
1115 return (u_int8_t *)paddr;
1116 }
1117 #endif
1118 return (NULL);
1119 }
1120
1121 /*
1122 * Copy NFS uid, gids from the cred structure.
1123 */
1124 void
newnfs_copyincred(struct ucred * cr,struct nfscred * nfscr)1125 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
1126 {
1127 int i;
1128
1129 KASSERT(cr->cr_ngroups >= 0,
1130 ("newnfs_copyincred: negative cr_ngroups"));
1131 nfscr->nfsc_uid = cr->cr_uid;
1132 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups + 1, NFS_MAXGRPS + 1);
1133 nfscr->nfsc_groups[0] = cr->cr_gid;
1134 for (i = 1; i < nfscr->nfsc_ngroups; i++)
1135 nfscr->nfsc_groups[i] = cr->cr_groups[i - 1];
1136 }
1137
1138 /*
1139 * Do any client specific initialization.
1140 */
1141 void
nfscl_init(void)1142 nfscl_init(void)
1143 {
1144 static int inited = 0;
1145
1146 if (inited)
1147 return;
1148 inited = 1;
1149 nfscl_inited = 1;
1150 ncl_pbuf_zone = pbuf_zsecond_create("nfspbuf", nswbuf / 2);
1151 }
1152
1153 /*
1154 * Check each of the attributes to be set, to ensure they aren't already
1155 * the correct value. Disable setting ones already correct.
1156 */
1157 int
nfscl_checksattr(struct vattr * vap,struct nfsvattr * nvap)1158 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1159 {
1160
1161 if (vap->va_mode != (mode_t)VNOVAL) {
1162 if (vap->va_mode == nvap->na_mode)
1163 vap->va_mode = (mode_t)VNOVAL;
1164 }
1165 if (vap->va_uid != (uid_t)VNOVAL) {
1166 if (vap->va_uid == nvap->na_uid)
1167 vap->va_uid = (uid_t)VNOVAL;
1168 }
1169 if (vap->va_gid != (gid_t)VNOVAL) {
1170 if (vap->va_gid == nvap->na_gid)
1171 vap->va_gid = (gid_t)VNOVAL;
1172 }
1173 if (vap->va_size != VNOVAL) {
1174 if (vap->va_size == nvap->na_size)
1175 vap->va_size = VNOVAL;
1176 }
1177
1178 /*
1179 * We are normally called with only a partially initialized
1180 * VAP. Since the NFSv3 spec says that server may use the
1181 * file attributes to store the verifier, the spec requires
1182 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1183 * in atime, but we can't really assume that all servers will
1184 * so we ensure that our SETATTR sets both atime and mtime.
1185 * Set the VA_UTIMES_NULL flag for this case, so that
1186 * the server's time will be used. This is needed to
1187 * work around a bug in some Solaris servers, where
1188 * setting the time TOCLIENT causes the Setattr RPC
1189 * to return NFS_OK, but not set va_mode.
1190 */
1191 if (vap->va_mtime.tv_sec == VNOVAL) {
1192 vfs_timestamp(&vap->va_mtime);
1193 vap->va_vaflags |= VA_UTIMES_NULL;
1194 }
1195 if (vap->va_atime.tv_sec == VNOVAL)
1196 vap->va_atime = vap->va_mtime;
1197 return (1);
1198 }
1199
1200 /*
1201 * Map nfsv4 errors to errno.h errors.
1202 * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1203 * error should only be returned for the Open, Create and Setattr Ops.
1204 * As such, most calls can just pass in 0 for those arguments.
1205 */
1206 int
nfscl_maperr(struct thread * td,int error,uid_t uid,gid_t gid)1207 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1208 {
1209 struct proc *p;
1210
1211 if (error < 10000 || error >= NFSERR_STALEWRITEVERF)
1212 return (error);
1213 if (td != NULL)
1214 p = td->td_proc;
1215 else
1216 p = NULL;
1217 switch (error) {
1218 case NFSERR_BADOWNER:
1219 tprintf(p, LOG_INFO,
1220 "No name and/or group mapping for uid,gid:(%d,%d)\n",
1221 uid, gid);
1222 return (EPERM);
1223 case NFSERR_BADNAME:
1224 case NFSERR_BADCHAR:
1225 printf("nfsv4 char/name not handled by server\n");
1226 return (ENOENT);
1227 case NFSERR_STALECLIENTID:
1228 case NFSERR_STALESTATEID:
1229 case NFSERR_EXPIRED:
1230 case NFSERR_BADSTATEID:
1231 case NFSERR_BADSESSION:
1232 printf("nfsv4 recover err returned %d\n", error);
1233 return (EIO);
1234 case NFSERR_BADHANDLE:
1235 case NFSERR_SERVERFAULT:
1236 case NFSERR_BADTYPE:
1237 case NFSERR_FHEXPIRED:
1238 case NFSERR_RESOURCE:
1239 case NFSERR_MOVED:
1240 case NFSERR_MINORVERMISMATCH:
1241 case NFSERR_OLDSTATEID:
1242 case NFSERR_BADSEQID:
1243 case NFSERR_LEASEMOVED:
1244 case NFSERR_RECLAIMBAD:
1245 case NFSERR_BADXDR:
1246 case NFSERR_OPILLEGAL:
1247 printf("nfsv4 client/server protocol prob err=%d\n",
1248 error);
1249 return (EIO);
1250 case NFSERR_NOFILEHANDLE:
1251 printf("nfsv4 no file handle: usually means the file "
1252 "system is not exported on the NFSv4 server\n");
1253 return (EIO);
1254 case NFSERR_WRONGSEC:
1255 tprintf(p, LOG_INFO, "NFSv4 error WrongSec: You probably need a"
1256 " Kerberos TGT\n");
1257 return (EIO);
1258 default:
1259 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1260 return (EIO);
1261 };
1262 }
1263
1264 /*
1265 * Check to see if the process for this owner exists. Return 1 if it doesn't
1266 * and 0 otherwise.
1267 */
1268 int
nfscl_procdoesntexist(u_int8_t * own)1269 nfscl_procdoesntexist(u_int8_t *own)
1270 {
1271 union {
1272 u_int32_t lval;
1273 u_int8_t cval[4];
1274 } tl;
1275 struct proc *p;
1276 pid_t pid;
1277 int i, ret = 0;
1278
1279 /* For the single open_owner of all 0 bytes, just return 0. */
1280 for (i = 0; i < NFSV4CL_LOCKNAMELEN; i++)
1281 if (own[i] != 0)
1282 break;
1283 if (i == NFSV4CL_LOCKNAMELEN)
1284 return (0);
1285
1286 tl.cval[0] = *own++;
1287 tl.cval[1] = *own++;
1288 tl.cval[2] = *own++;
1289 tl.cval[3] = *own++;
1290 pid = tl.lval;
1291 p = pfind_any_locked(pid);
1292 if (p == NULL)
1293 return (1);
1294 if (p->p_stats == NULL) {
1295 PROC_UNLOCK(p);
1296 return (0);
1297 }
1298 tl.cval[0] = *own++;
1299 tl.cval[1] = *own++;
1300 tl.cval[2] = *own++;
1301 tl.cval[3] = *own++;
1302 if (tl.lval != p->p_stats->p_start.tv_sec) {
1303 ret = 1;
1304 } else {
1305 tl.cval[0] = *own++;
1306 tl.cval[1] = *own++;
1307 tl.cval[2] = *own++;
1308 tl.cval[3] = *own;
1309 if (tl.lval != p->p_stats->p_start.tv_usec)
1310 ret = 1;
1311 }
1312 PROC_UNLOCK(p);
1313 return (ret);
1314 }
1315
1316 /*
1317 * - nfs pseudo system call for the client
1318 */
1319 /*
1320 * MPSAFE
1321 */
1322 static int
nfssvc_nfscl(struct thread * td,struct nfssvc_args * uap)1323 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1324 {
1325 struct file *fp;
1326 struct nfscbd_args nfscbdarg;
1327 struct nfsd_nfscbd_args nfscbdarg2;
1328 struct nameidata nd;
1329 struct nfscl_dumpmntopts dumpmntopts;
1330 cap_rights_t rights;
1331 char *buf;
1332 int error;
1333 struct mount *mp;
1334 struct nfsmount *nmp;
1335
1336 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
1337 if (uap->flag & NFSSVC_CBADDSOCK) {
1338 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1339 if (error)
1340 goto out;
1341 /*
1342 * Since we don't know what rights might be required,
1343 * pretend that we need them all. It is better to be too
1344 * careful than too reckless.
1345 */
1346 error = fget(td, nfscbdarg.sock,
1347 cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp);
1348 if (error)
1349 goto out;
1350 if (fp->f_type != DTYPE_SOCKET) {
1351 fdrop(fp, td);
1352 error = EPERM;
1353 goto out;
1354 }
1355 error = nfscbd_addsock(fp);
1356 fdrop(fp, td);
1357 if (!error && nfscl_enablecallb == 0) {
1358 nfsv4_cbport = nfscbdarg.port;
1359 nfscl_enablecallb = 1;
1360 }
1361 } else if (uap->flag & NFSSVC_NFSCBD) {
1362 if (uap->argp == NULL) {
1363 error = EINVAL;
1364 goto out;
1365 }
1366 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1367 sizeof(nfscbdarg2));
1368 if (error)
1369 goto out;
1370 error = nfscbd_nfsd(td, &nfscbdarg2);
1371 } else if (uap->flag & NFSSVC_DUMPMNTOPTS) {
1372 error = copyin(uap->argp, &dumpmntopts, sizeof(dumpmntopts));
1373 if (error == 0 && (dumpmntopts.ndmnt_blen < 256 ||
1374 dumpmntopts.ndmnt_blen > 1024))
1375 error = EINVAL;
1376 if (error == 0)
1377 error = nfsrv_lookupfilename(&nd,
1378 dumpmntopts.ndmnt_fname, td);
1379 if (error == 0 && strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name,
1380 "nfs") != 0) {
1381 vput(nd.ni_vp);
1382 error = EINVAL;
1383 }
1384 if (error == 0) {
1385 buf = malloc(dumpmntopts.ndmnt_blen, M_TEMP, M_WAITOK |
1386 M_ZERO);
1387 nfscl_retopts(VFSTONFS(nd.ni_vp->v_mount), buf,
1388 dumpmntopts.ndmnt_blen);
1389 vput(nd.ni_vp);
1390 error = copyout(buf, dumpmntopts.ndmnt_buf,
1391 dumpmntopts.ndmnt_blen);
1392 free(buf, M_TEMP);
1393 }
1394 } else if (uap->flag & NFSSVC_FORCEDISM) {
1395 buf = malloc(MNAMELEN + 1, M_TEMP, M_WAITOK);
1396 error = copyinstr(uap->argp, buf, MNAMELEN + 1, NULL);
1397 if (error == 0) {
1398 nmp = NULL;
1399 mtx_lock(&mountlist_mtx);
1400 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1401 if (strcmp(mp->mnt_stat.f_mntonname, buf) ==
1402 0 && strcmp(mp->mnt_stat.f_fstypename,
1403 "nfs") == 0 && mp->mnt_data != NULL) {
1404 nmp = VFSTONFS(mp);
1405 NFSDDSLOCK();
1406 if (nfsv4_findmirror(nmp) != NULL) {
1407 NFSDDSUNLOCK();
1408 error = ENXIO;
1409 nmp = NULL;
1410 break;
1411 }
1412 mtx_lock(&nmp->nm_mtx);
1413 if ((nmp->nm_privflag &
1414 NFSMNTP_FORCEDISM) == 0) {
1415 nmp->nm_privflag |=
1416 (NFSMNTP_FORCEDISM |
1417 NFSMNTP_CANCELRPCS);
1418 mtx_unlock(&nmp->nm_mtx);
1419 } else {
1420 mtx_unlock(&nmp->nm_mtx);
1421 nmp = NULL;
1422 }
1423 NFSDDSUNLOCK();
1424 break;
1425 }
1426 }
1427 mtx_unlock(&mountlist_mtx);
1428
1429 if (nmp != NULL) {
1430 /*
1431 * Call newnfs_nmcancelreqs() to cause
1432 * any RPCs in progress on the mount point to
1433 * fail.
1434 * This will cause any process waiting for an
1435 * RPC to complete while holding a vnode lock
1436 * on the mounted-on vnode (such as "df" or
1437 * a non-forced "umount") to fail.
1438 * This will unlock the mounted-on vnode so
1439 * a forced dismount can succeed.
1440 * Then clear NFSMNTP_CANCELRPCS and wakeup(),
1441 * so that nfs_unmount() can complete.
1442 */
1443 newnfs_nmcancelreqs(nmp);
1444 mtx_lock(&nmp->nm_mtx);
1445 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
1446 wakeup(nmp);
1447 mtx_unlock(&nmp->nm_mtx);
1448 } else if (error == 0)
1449 error = EINVAL;
1450 }
1451 free(buf, M_TEMP);
1452 } else {
1453 error = EINVAL;
1454 }
1455 out:
1456 NFSD_CURVNET_RESTORE();
1457 return (error);
1458 }
1459
1460 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1461
1462 /*
1463 * Called once to initialize data structures...
1464 */
1465 static int
nfscl_modevent(module_t mod,int type,void * data)1466 nfscl_modevent(module_t mod, int type, void *data)
1467 {
1468 int error = 0;
1469 static int loaded = 0;
1470
1471 switch (type) {
1472 case MOD_LOAD:
1473 if (loaded)
1474 return (0);
1475 newnfs_portinit();
1476 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1477 nfscl_init();
1478 NFSD_LOCK();
1479 nfsrvd_cbinit(0);
1480 NFSD_UNLOCK();
1481 ncl_call_invalcaches = ncl_invalcaches;
1482 nfsd_call_nfscl = nfssvc_nfscl;
1483 loaded = 1;
1484 break;
1485
1486 case MOD_UNLOAD:
1487 if (nfs_numnfscbd != 0) {
1488 error = EBUSY;
1489 break;
1490 }
1491
1492 /*
1493 * XXX: Unloading of nfscl module is unsupported.
1494 */
1495 #if 0
1496 ncl_call_invalcaches = NULL;
1497 nfsd_call_nfscl = NULL;
1498 uma_zdestroy(ncl_pbuf_zone);
1499 /* and get rid of the mutexes */
1500 mtx_destroy(&ncl_iod_mutex);
1501 loaded = 0;
1502 break;
1503 #else
1504 /* FALLTHROUGH */
1505 #endif
1506 default:
1507 error = EOPNOTSUPP;
1508 break;
1509 }
1510 return error;
1511 }
1512 static moduledata_t nfscl_mod = {
1513 "nfscl",
1514 nfscl_modevent,
1515 NULL,
1516 };
1517 /*
1518 * This is the main module declaration for the NFS client. The
1519 * nfscl_modevent() function is needed to ensure that the module
1520 * cannot be unloaded, among other things.
1521 * There is also a module declaration in sys/fs/nfsclient/nfs_clvfsops.c
1522 * for the name "nfs" within the VFS_SET() macro that defines the "nfs"
1523 * file system type.
1524 */
1525 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1526
1527 /* So that loader and kldload(2) can find us, wherever we are.. */
1528 MODULE_VERSION(nfscl, 1);
1529 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1530 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1531 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);
1532 MODULE_DEPEND(nfscl, xdr, 1, 1, 1);
1533 MODULE_DEPEND(nfscl, acl_nfs4, 1, 1, 1);
1534