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/capsicum.h>
37 #include <sys/extattr.h>
38
39 /*
40 * Functions that perform the vfs operations required by the routines in
41 * nfsd_serv.c. It is hoped that this change will make the server more
42 * portable.
43 */
44
45 #include <fs/nfs/nfsport.h>
46 #include <security/mac/mac_framework.h>
47 #include <sys/callout.h>
48 #include <sys/filio.h>
49 #include <sys/hash.h>
50 #include <sys/osd.h>
51 #include <sys/sysctl.h>
52 #include <nlm/nlm_prot.h>
53 #include <nlm/nlm.h>
54 #include <vm/vm_param.h>
55 #include <vm/vnode_pager.h>
56
57 FEATURE(nfsd, "NFSv4 server");
58
59 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
60 extern int nfsrv_useacl;
61 extern int newnfs_numnfsd;
62 extern int nfsrv_sessionhashsize;
63 extern struct nfslayouthash *nfslayouthash;
64 extern int nfsrv_layouthashsize;
65 extern struct mtx nfsrv_dslock_mtx;
66 extern int nfs_pnfsiothreads;
67 extern volatile int nfsrv_devidcnt;
68 extern int nfsrv_maxpnfsmirror;
69 extern uint32_t nfs_srvmaxio;
70 extern int nfs_bufpackets;
71 extern u_long sb_max_adj;
72 extern struct nfsv4lock nfsv4rootfs_lock;
73
74 NFSD_VNET_DECLARE(int, nfsrv_numnfsd);
75 NFSD_VNET_DECLARE(struct nfsrv_stablefirst, nfsrv_stablefirst);
76 NFSD_VNET_DECLARE(SVCPOOL *, nfsrvd_pool);
77 NFSD_VNET_DECLARE(struct nfsclienthashhead *, nfsclienthash);
78 NFSD_VNET_DECLARE(struct nfslockhashhead *, nfslockhash);
79 NFSD_VNET_DECLARE(struct nfssessionhash *, nfssessionhash);
80 NFSD_VNET_DECLARE(struct nfsv4lock, nfsd_suspend_lock);
81 NFSD_VNET_DECLARE(struct nfsstatsv1 *, nfsstatsv1_p);
82
83 NFSDLOCKMUTEX;
84 NFSSTATESPINLOCK;
85 struct mtx nfsrc_udpmtx;
86 struct mtx nfs_v4root_mutex;
87 struct mtx nfsrv_dontlistlock_mtx;
88 struct mtx nfsrv_recalllock_mtx;
89 struct nfsrvfh nfs_pubfh;
90 int nfs_pubfhset = 0;
91 int nfsd_debuglevel = 0;
92 static pid_t nfsd_master_pid = (pid_t)-1;
93 static char nfsd_master_comm[MAXCOMLEN + 1];
94 static struct timeval nfsd_master_start;
95 static uint32_t nfsv4_sysid = 0;
96 static fhandle_t zerofh;
97
98 NFSD_VNET_DEFINE(struct proc *, nfsd_master_proc) = NULL;
99 NFSD_VNET_DEFINE(struct nfsrvhashhead *, nfsrvudphashtbl);
100 NFSD_VNET_DEFINE(struct nfsrchash_bucket *, nfsrchash_table);
101 NFSD_VNET_DEFINE(struct nfsrchash_bucket *, nfsrcahash_table);
102 NFSD_VNET_DEFINE(struct nfsrvfh, nfs_rootfh);
103 NFSD_VNET_DEFINE(int, nfs_rootfhset) = 0;
104 NFSD_VNET_DEFINE(struct callout, nfsd_callout);
105 NFSD_VNET_DEFINE_STATIC(struct mount *, nfsv4root_mnt);
106 NFSD_VNET_DEFINE_STATIC(struct vfsoptlist, nfsv4root_opt);
107 NFSD_VNET_DEFINE_STATIC(struct vfsoptlist, nfsv4root_newopt);
108 NFSD_VNET_DEFINE_STATIC(bool, nfsrv_suspend_nfsd) = false;
109 NFSD_VNET_DEFINE_STATIC(bool, nfsrv_mntinited) = false;
110
111 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
112 struct ucred *);
113 static void nfsvno_updateds(struct vnode *, struct ucred *, struct thread *);
114
115 int nfsrv_enable_crossmntpt = 1;
116 static int nfs_commit_blks;
117 static int nfs_commit_miss;
118 extern int nfsrv_issuedelegs;
119 extern int nfsrv_dolocallocks;
120 extern struct nfsdevicehead nfsrv_devidhead;
121
122 /* Map d_type to vnode type. */
123 static uint8_t dtype_to_vnode[DT_WHT + 1] = { VNON, VFIFO, VCHR, VNON, VDIR,
124 VNON, VBLK, VNON, VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON };
125
126 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
127 struct iovec **);
128 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
129 struct mbuf **, struct iovec **);
130 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
131 int *);
132 static void nfs_dtypetovtype(struct nfsvattr *, struct vnode *, uint8_t);
133 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
134 NFSPROC_T *);
135 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
136 int *, char *, fhandle_t *);
137 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
138 NFSPROC_T *);
139 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
140 struct thread *, int, struct mbuf **, char *, struct mbuf **,
141 struct nfsvattr *, struct acl *, off_t *, int, bool *);
142 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
143 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
144 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
145 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
146 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
147 char *, int *);
148 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
149 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
150 static int nfsrv_deallocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
151 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
152 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
153 struct vnode *, struct nfsmount **, int, struct acl *, int *);
154 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
155 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
156 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
157 struct vnode *, struct nfsmount *, struct nfsvattr *);
158 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
159 NFSPROC_T *, struct nfsmount *);
160 static int nfsrv_putfhname(fhandle_t *, char *);
161 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
162 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
163 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
164 struct vnode *, NFSPROC_T *);
165 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
166 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
167 NFSPROC_T *);
168 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
169
170 int nfs_pnfsio(task_fn_t *, void *);
171
172 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
173 "NFS server");
174 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
175 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
176 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
177 0, "");
178 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
179 0, "");
180 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
181 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
182 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
183 0, "Debug level for NFS server");
184 NFSD_VNET_DECLARE(int, nfsd_enable_stringtouid);
185 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid,
186 CTLFLAG_NFSD_VNET | CTLFLAG_RW, &NFSD_VNET_NAME(nfsd_enable_stringtouid),
187 0, "Enable nfsd to accept numeric owner_names");
188 static int nfsrv_pnfsgetdsattr = 1;
189 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
190 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
191 static bool nfsrv_recalldeleg = false;
192 SYSCTL_BOOL(_vfs_nfsd, OID_AUTO, recalldeleg, CTLFLAG_RW,
193 &nfsrv_recalldeleg, 0,
194 "When set remove/rename recalls delegations for same client");
195
196 /*
197 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
198 * not running.
199 * The dsN subdirectories for the increased values must have been created
200 * on all DS servers before this increase is done.
201 */
202 u_int nfsrv_dsdirsize = 20;
203 static int
sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)204 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
205 {
206 int error, newdsdirsize;
207
208 newdsdirsize = nfsrv_dsdirsize;
209 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
210 if (error != 0 || req->newptr == NULL)
211 return (error);
212 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
213 newnfs_numnfsd != 0)
214 return (EINVAL);
215 nfsrv_dsdirsize = newdsdirsize;
216 return (0);
217 }
218 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
219 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
220 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
221
222 /*
223 * nfs_srvmaxio can only be increased and only when the nfsd threads are
224 * not running. The setting must be a power of 2, with the current limit of
225 * 1Mbyte.
226 */
227 static int
sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)228 sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)
229 {
230 int error;
231 u_int newsrvmaxio;
232 uint64_t tval;
233
234 newsrvmaxio = nfs_srvmaxio;
235 error = sysctl_handle_int(oidp, &newsrvmaxio, 0, req);
236 if (error != 0 || req->newptr == NULL)
237 return (error);
238 if (newsrvmaxio == nfs_srvmaxio)
239 return (0);
240 if (newsrvmaxio < nfs_srvmaxio) {
241 printf("nfsd: vfs.nfsd.srvmaxio can only be increased\n");
242 return (EINVAL);
243 }
244 if (newsrvmaxio > 1048576) {
245 printf("nfsd: vfs.nfsd.srvmaxio cannot be > 1Mbyte\n");
246 return (EINVAL);
247 }
248 if ((newsrvmaxio & (newsrvmaxio - 1)) != 0) {
249 printf("nfsd: vfs.nfsd.srvmaxio must be a power of 2\n");
250 return (EINVAL);
251 }
252
253 /*
254 * Check that kern.ipc.maxsockbuf is large enough for
255 * newsrviomax, given the setting of vfs.nfs.bufpackets.
256 */
257 if ((newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets >
258 sb_max_adj) {
259 /*
260 * Suggest vfs.nfs.bufpackets * maximum RPC message for
261 * sb_max_adj.
262 */
263 tval = (newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets;
264
265 /*
266 * Convert suggested sb_max_adj value to a suggested
267 * sb_max value, which is what is set via kern.ipc.maxsockbuf.
268 * Perform the inverse calculation of (from uipc_sockbuf.c):
269 * sb_max_adj = (u_quad_t)sb_max * MCLBYTES /
270 * (MSIZE + MCLBYTES);
271 * XXX If the calculation of sb_max_adj from sb_max changes,
272 * this calculation must be changed as well.
273 */
274 tval *= (MSIZE + MCLBYTES); /* Brackets for readability. */
275 tval += MCLBYTES - 1; /* Round up divide. */
276 tval /= MCLBYTES;
277 printf("nfsd: set kern.ipc.maxsockbuf to a minimum of "
278 "%ju to support %ubyte NFS I/O\n", (uintmax_t)tval,
279 newsrvmaxio);
280 return (EINVAL);
281 }
282
283 NFSD_LOCK();
284 if (newnfs_numnfsd != 0) {
285 NFSD_UNLOCK();
286 printf("nfsd: cannot set vfs.nfsd.srvmaxio when nfsd "
287 "threads are running\n");
288 return (EINVAL);
289 }
290
291
292 nfs_srvmaxio = newsrvmaxio;
293 NFSD_UNLOCK();
294 return (0);
295 }
296 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, srvmaxio,
297 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
298 sysctl_srvmaxio, "IU", "Maximum I/O size in bytes");
299
300 static int
sysctl_dolocallocks(SYSCTL_HANDLER_ARGS)301 sysctl_dolocallocks(SYSCTL_HANDLER_ARGS)
302 {
303 int error, igotlock, newdolocallocks;
304
305 newdolocallocks = nfsrv_dolocallocks;
306 error = sysctl_handle_int(oidp, &newdolocallocks, 0, req);
307 if (error != 0 || req->newptr == NULL)
308 return (error);
309 if (newdolocallocks == nfsrv_dolocallocks)
310 return (0);
311 if (jailed(curthread->td_ucred))
312 return (EINVAL);
313
314 NFSLOCKV4ROOTMUTEX();
315 do {
316 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
317 NFSV4ROOTLOCKMUTEXPTR, NULL);
318 } while (!igotlock);
319 NFSUNLOCKV4ROOTMUTEX();
320
321 nfsrv_dolocallocks = newdolocallocks;
322
323 NFSLOCKV4ROOTMUTEX();
324 nfsv4_unlock(&nfsv4rootfs_lock, 0);
325 NFSUNLOCKV4ROOTMUTEX();
326 return (0);
327 }
328 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, enable_locallocks,
329 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
330 sysctl_dolocallocks, "IU", "Enable nfsd to acquire local locks on files");
331
332 #define MAX_REORDERED_RPC 16
333 #define NUM_HEURISTIC 1031
334 #define NHUSE_INIT 64
335 #define NHUSE_INC 16
336 #define NHUSE_MAX 2048
337
338 static struct nfsheur {
339 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
340 off_t nh_nextoff; /* next offset for sequential detection */
341 int nh_use; /* use count for selection */
342 int nh_seqcount; /* heuristic */
343 } nfsheur[NUM_HEURISTIC];
344
345 /*
346 * Heuristic to detect sequential operation.
347 */
348 static struct nfsheur *
nfsrv_sequential_heuristic(struct uio * uio,struct vnode * vp)349 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
350 {
351 struct nfsheur *nh;
352 int hi, try;
353
354 /* Locate best candidate. */
355 try = 32;
356 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
357 nh = &nfsheur[hi];
358 while (try--) {
359 if (nfsheur[hi].nh_vp == vp) {
360 nh = &nfsheur[hi];
361 break;
362 }
363 if (nfsheur[hi].nh_use > 0)
364 --nfsheur[hi].nh_use;
365 hi = (hi + 1) % NUM_HEURISTIC;
366 if (nfsheur[hi].nh_use < nh->nh_use)
367 nh = &nfsheur[hi];
368 }
369
370 /* Initialize hint if this is a new file. */
371 if (nh->nh_vp != vp) {
372 nh->nh_vp = vp;
373 nh->nh_nextoff = uio->uio_offset;
374 nh->nh_use = NHUSE_INIT;
375 if (uio->uio_offset == 0)
376 nh->nh_seqcount = 4;
377 else
378 nh->nh_seqcount = 1;
379 }
380
381 /* Calculate heuristic. */
382 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
383 uio->uio_offset == nh->nh_nextoff) {
384 /* See comments in vfs_vnops.c:sequential_heuristic(). */
385 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
386 if (nh->nh_seqcount > IO_SEQMAX)
387 nh->nh_seqcount = IO_SEQMAX;
388 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
389 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
390 /* Probably a reordered RPC, leave seqcount alone. */
391 } else if (nh->nh_seqcount > 1) {
392 nh->nh_seqcount /= 2;
393 } else {
394 nh->nh_seqcount = 0;
395 }
396 nh->nh_use += NHUSE_INC;
397 if (nh->nh_use > NHUSE_MAX)
398 nh->nh_use = NHUSE_MAX;
399 return (nh);
400 }
401
402 /*
403 * Get attributes into nfsvattr structure.
404 */
405 int
nfsvno_getattr(struct vnode * vp,struct nfsvattr * nvap,struct nfsrv_descript * nd,struct thread * p,int vpislocked,nfsattrbit_t * attrbitp)406 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
407 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
408 nfsattrbit_t *attrbitp)
409 {
410 int error, gotattr, lockedit = 0;
411 struct nfsvattr na;
412
413 if (vpislocked == 0) {
414 /*
415 * When vpislocked == 0, the vnode is either exclusively
416 * locked by this thread or not locked by this thread.
417 * As such, shared lock it, if not exclusively locked.
418 */
419 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
420 lockedit = 1;
421 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
422 }
423 }
424
425 /*
426 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
427 * attributes, as required.
428 * This needs to be done for regular files if:
429 * - non-NFSv4 RPCs or
430 * - when attrbitp == NULL or
431 * - an NFSv4 RPC with any of the above attributes in attrbitp.
432 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
433 * these attributes. nfsrv_proxyds() will return an error if the
434 * server is not a pNFS one.
435 */
436 gotattr = 0;
437 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
438 (nd->nd_flag & ND_NFSV4) == 0 ||
439 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
440 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
441 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
442 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
443 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
444 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
445 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
446 NULL);
447 if (error == 0)
448 gotattr = 1;
449 }
450
451 nvap->na_bsdflags = 0;
452 nvap->na_flags = 0;
453 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
454 if (lockedit != 0)
455 NFSVOPUNLOCK(vp);
456
457 /*
458 * If we got the Change, Size and Modify Time from the DS,
459 * replace them.
460 */
461 if (gotattr != 0) {
462 nvap->na_atime = na.na_atime;
463 nvap->na_mtime = na.na_mtime;
464 nvap->na_filerev = na.na_filerev;
465 nvap->na_size = na.na_size;
466 nvap->na_bytes = na.na_bytes;
467 }
468 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
469 error, (uintmax_t)na.na_filerev);
470
471 NFSEXITCODE(error);
472 return (error);
473 }
474
475 /*
476 * Get a file handle for a vnode.
477 */
478 int
nfsvno_getfh(struct vnode * vp,fhandle_t * fhp,struct thread * p)479 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
480 {
481 int error;
482
483 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
484 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
485 error = VOP_VPTOFH(vp, &fhp->fh_fid);
486
487 NFSEXITCODE(error);
488 return (error);
489 }
490
491 /*
492 * Perform access checking for vnodes obtained from file handles that would
493 * refer to files already opened by a Unix client. You cannot just use
494 * vn_writechk() and VOP_ACCESSX() for two reasons.
495 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
496 * case.
497 * 2 - The owner is to be given access irrespective of mode bits for some
498 * operations, so that processes that chmod after opening a file don't
499 * break.
500 */
501 int
nfsvno_accchk(struct vnode * vp,accmode_t accmode,struct ucred * cred,struct nfsexstuff * exp,struct thread * p,int override,int vpislocked,u_int32_t * supportedtypep)502 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
503 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
504 u_int32_t *supportedtypep)
505 {
506 struct vattr vattr;
507 int error = 0, getret = 0;
508
509 if (vpislocked == 0) {
510 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
511 error = EPERM;
512 goto out;
513 }
514 }
515 if (accmode & VWRITE) {
516 /* Just vn_writechk() changed to check rdonly */
517 /*
518 * Disallow write attempts on read-only file systems;
519 * unless the file is a socket or a block or character
520 * device resident on the file system.
521 */
522 if (NFSVNO_EXRDONLY(exp) ||
523 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
524 switch (vp->v_type) {
525 case VREG:
526 case VDIR:
527 case VLNK:
528 error = EROFS;
529 default:
530 break;
531 }
532 }
533 /*
534 * If there's shared text associated with
535 * the inode, try to free it up once. If
536 * we fail, we can't allow writing.
537 */
538 if (VOP_IS_TEXT(vp) && error == 0)
539 error = ETXTBSY;
540 }
541 if (error != 0) {
542 if (vpislocked == 0)
543 NFSVOPUNLOCK(vp);
544 goto out;
545 }
546
547 /*
548 * Should the override still be applied when ACLs are enabled?
549 */
550 error = VOP_ACCESSX(vp, accmode, cred, p);
551 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
552 /*
553 * Try again with VEXPLICIT_DENY, to see if the test for
554 * deletion is supported.
555 */
556 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
557 if (error == 0) {
558 if (vp->v_type == VDIR) {
559 accmode &= ~(VDELETE | VDELETE_CHILD);
560 accmode |= VWRITE;
561 error = VOP_ACCESSX(vp, accmode, cred, p);
562 } else if (supportedtypep != NULL) {
563 *supportedtypep &= ~NFSACCESS_DELETE;
564 }
565 }
566 }
567
568 /*
569 * Allow certain operations for the owner (reads and writes
570 * on files that are already open).
571 */
572 if (override != NFSACCCHK_NOOVERRIDE &&
573 (error == EPERM || error == EACCES)) {
574 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
575 error = 0;
576 else if (override & NFSACCCHK_ALLOWOWNER) {
577 getret = VOP_GETATTR(vp, &vattr, cred);
578 if (getret == 0 && cred->cr_uid == vattr.va_uid)
579 error = 0;
580 }
581 }
582 if (vpislocked == 0)
583 NFSVOPUNLOCK(vp);
584
585 out:
586 NFSEXITCODE(error);
587 return (error);
588 }
589
590 /*
591 * Set attribute(s) vnop.
592 */
593 int
nfsvno_setattr(struct vnode * vp,struct nfsvattr * nvap,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)594 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
595 struct thread *p, struct nfsexstuff *exp)
596 {
597 u_quad_t savsize = 0;
598 int error, savedit;
599 time_t savbtime;
600
601 /*
602 * If this is an exported file system and a pNFS service is running,
603 * don't VOP_SETATTR() of size for the MDS file system.
604 */
605 savedit = 0;
606 error = 0;
607 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
608 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
609 nvap->na_vattr.va_size > 0) {
610 savsize = nvap->na_vattr.va_size;
611 nvap->na_vattr.va_size = VNOVAL;
612 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
613 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
614 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
615 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
616 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
617 savedit = 1;
618 else
619 savedit = 2;
620 }
621 if (savedit != 2)
622 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
623 if (savedit != 0)
624 nvap->na_vattr.va_size = savsize;
625 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
626 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
627 nvap->na_vattr.va_size != VNOVAL ||
628 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
629 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
630 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
631 /* Never modify birthtime on a DS file. */
632 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
633 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
634 /* For a pNFS server, set the attributes on the DS file. */
635 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
636 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
637 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
638 if (error == ENOENT)
639 error = 0;
640 }
641 NFSEXITCODE(error);
642 return (error);
643 }
644
645 /*
646 * Set up nameidata for a lookup() call and do it.
647 */
648 int
nfsvno_namei(struct nfsrv_descript * nd,struct nameidata * ndp,struct vnode * dp,int islocked,struct nfsexstuff * exp,struct vnode ** retdirp)649 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
650 struct vnode *dp, int islocked, struct nfsexstuff *exp,
651 struct vnode **retdirp)
652 {
653 struct componentname *cnp = &ndp->ni_cnd;
654 int i;
655 struct iovec aiov;
656 struct uio auio;
657 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
658 int error = 0;
659 char *cp;
660
661 *retdirp = NULL;
662 cnp->cn_nameptr = cnp->cn_pnbuf;
663 ndp->ni_lcf = 0;
664 /*
665 * Extract and set starting directory.
666 */
667 if (dp->v_type != VDIR) {
668 if (islocked)
669 vput(dp);
670 else
671 vrele(dp);
672 nfsvno_relpathbuf(ndp);
673 error = ENOTDIR;
674 goto out1;
675 }
676 if (islocked)
677 NFSVOPUNLOCK(dp);
678 vref(dp);
679 *retdirp = dp;
680 if (NFSVNO_EXRDONLY(exp))
681 cnp->cn_flags |= RDONLY;
682 ndp->ni_segflg = UIO_SYSSPACE;
683
684 if (nd->nd_flag & ND_PUBLOOKUP) {
685 ndp->ni_loopcnt = 0;
686 if (cnp->cn_pnbuf[0] == '/') {
687 vrele(dp);
688 /*
689 * Check for degenerate pathnames here, since lookup()
690 * panics on them.
691 */
692 for (i = 1; i < ndp->ni_pathlen; i++)
693 if (cnp->cn_pnbuf[i] != '/')
694 break;
695 if (i == ndp->ni_pathlen) {
696 error = NFSERR_ACCES;
697 goto out;
698 }
699 dp = rootvnode;
700 vref(dp);
701 }
702 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
703 (nd->nd_flag & ND_NFSV4) == 0) {
704 /*
705 * Only cross mount points for NFSv4 when doing a
706 * mount while traversing the file system above
707 * the mount point, unless nfsrv_enable_crossmntpt is set.
708 */
709 cnp->cn_flags |= NOCROSSMOUNT;
710 }
711
712 /*
713 * Initialize for scan, set ni_startdir and bump ref on dp again
714 * because lookup() will dereference ni_startdir.
715 */
716
717 ndp->ni_startdir = dp;
718 ndp->ni_rootdir = rootvnode;
719 ndp->ni_topdir = NULL;
720
721 if (!lockleaf)
722 cnp->cn_flags |= LOCKLEAF;
723 for (;;) {
724 cnp->cn_nameptr = cnp->cn_pnbuf;
725 /*
726 * Call lookup() to do the real work. If an error occurs,
727 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
728 * we do not have to dereference anything before returning.
729 * In either case ni_startdir will be dereferenced and NULLed
730 * out.
731 */
732 error = vfs_lookup(ndp);
733 if (error)
734 break;
735
736 /*
737 * Check for encountering a symbolic link. Trivial
738 * termination occurs if no symlink encountered.
739 */
740 if ((cnp->cn_flags & ISSYMLINK) == 0) {
741 if (ndp->ni_vp && !lockleaf)
742 NFSVOPUNLOCK(ndp->ni_vp);
743 break;
744 }
745
746 /*
747 * Validate symlink
748 */
749 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
750 NFSVOPUNLOCK(ndp->ni_dvp);
751 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
752 error = EINVAL;
753 goto badlink2;
754 }
755
756 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
757 error = ELOOP;
758 goto badlink2;
759 }
760 if (ndp->ni_pathlen > 1)
761 cp = uma_zalloc(namei_zone, M_WAITOK);
762 else
763 cp = cnp->cn_pnbuf;
764 aiov.iov_base = cp;
765 aiov.iov_len = MAXPATHLEN;
766 auio.uio_iov = &aiov;
767 auio.uio_iovcnt = 1;
768 auio.uio_offset = 0;
769 auio.uio_rw = UIO_READ;
770 auio.uio_segflg = UIO_SYSSPACE;
771 auio.uio_td = NULL;
772 auio.uio_resid = MAXPATHLEN;
773 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
774 if (error) {
775 badlink1:
776 if (ndp->ni_pathlen > 1)
777 uma_zfree(namei_zone, cp);
778 badlink2:
779 vrele(ndp->ni_dvp);
780 vput(ndp->ni_vp);
781 break;
782 }
783 linklen = MAXPATHLEN - auio.uio_resid;
784 if (linklen == 0) {
785 error = ENOENT;
786 goto badlink1;
787 }
788 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
789 error = ENAMETOOLONG;
790 goto badlink1;
791 }
792
793 /*
794 * Adjust or replace path
795 */
796 if (ndp->ni_pathlen > 1) {
797 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
798 uma_zfree(namei_zone, cnp->cn_pnbuf);
799 cnp->cn_pnbuf = cp;
800 } else
801 cnp->cn_pnbuf[linklen] = '\0';
802 ndp->ni_pathlen += linklen;
803
804 /*
805 * Cleanup refs for next loop and check if root directory
806 * should replace current directory. Normally ni_dvp
807 * becomes the new base directory and is cleaned up when
808 * we loop. Explicitly null pointers after invalidation
809 * to clarify operation.
810 */
811 vput(ndp->ni_vp);
812 ndp->ni_vp = NULL;
813
814 if (cnp->cn_pnbuf[0] == '/') {
815 vrele(ndp->ni_dvp);
816 ndp->ni_dvp = ndp->ni_rootdir;
817 vref(ndp->ni_dvp);
818 }
819 ndp->ni_startdir = ndp->ni_dvp;
820 ndp->ni_dvp = NULL;
821 }
822 if (!lockleaf)
823 cnp->cn_flags &= ~LOCKLEAF;
824
825 out:
826 if (error) {
827 nfsvno_relpathbuf(ndp);
828 ndp->ni_vp = NULL;
829 ndp->ni_dvp = NULL;
830 ndp->ni_startdir = NULL;
831 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
832 ndp->ni_dvp = NULL;
833 }
834
835 out1:
836 NFSEXITCODE2(error, nd);
837 return (error);
838 }
839
840 /*
841 * Set up a pathname buffer and return a pointer to it and, optionally
842 * set a hash pointer.
843 */
844 void
nfsvno_setpathbuf(struct nameidata * ndp,char ** bufpp,u_long ** hashpp)845 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
846 {
847 struct componentname *cnp = &ndp->ni_cnd;
848
849 cnp->cn_flags |= (NOMACCHECK);
850 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
851 if (hashpp != NULL)
852 *hashpp = NULL;
853 *bufpp = cnp->cn_pnbuf;
854 }
855
856 /*
857 * Release the above path buffer, if not released by nfsvno_namei().
858 */
859 void
nfsvno_relpathbuf(struct nameidata * ndp)860 nfsvno_relpathbuf(struct nameidata *ndp)
861 {
862
863 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
864 ndp->ni_cnd.cn_pnbuf = NULL;
865 }
866
867 /*
868 * Readlink vnode op into an mbuf list.
869 */
870 int
nfsvno_readlink(struct vnode * vp,struct ucred * cred,int maxextsiz,struct thread * p,struct mbuf ** mpp,struct mbuf ** mpendp,int * lenp)871 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
872 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
873 {
874 struct iovec *iv;
875 struct uio io, *uiop = &io;
876 struct mbuf *mp, *mp3;
877 int len, tlen, error = 0;
878
879 len = NFS_MAXPATHLEN;
880 if (maxextsiz > 0)
881 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
882 &mp3, &mp, &iv);
883 else
884 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
885 uiop->uio_iov = iv;
886 uiop->uio_offset = 0;
887 uiop->uio_resid = len;
888 uiop->uio_rw = UIO_READ;
889 uiop->uio_segflg = UIO_SYSSPACE;
890 uiop->uio_td = NULL;
891 error = VOP_READLINK(vp, uiop, cred);
892 free(iv, M_TEMP);
893 if (error) {
894 m_freem(mp3);
895 *lenp = 0;
896 goto out;
897 }
898 if (uiop->uio_resid > 0) {
899 len -= uiop->uio_resid;
900 tlen = NFSM_RNDUP(len);
901 if (tlen == 0) {
902 m_freem(mp3);
903 mp3 = mp = NULL;
904 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
905 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
906 tlen - len);
907 }
908 *lenp = len;
909 *mpp = mp3;
910 *mpendp = mp;
911
912 out:
913 NFSEXITCODE(error);
914 return (error);
915 }
916
917 /*
918 * Create an mbuf chain and an associated iovec that can be used to Read
919 * or Getextattr of data.
920 * Upon success, return pointers to the first and last mbufs in the chain
921 * plus the malloc'd iovec and its iovlen.
922 */
923 static int
nfsrv_createiovec(int len,struct mbuf ** mpp,struct mbuf ** mpendp,struct iovec ** ivp)924 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
925 struct iovec **ivp)
926 {
927 struct mbuf *m, *m2 = NULL, *m3;
928 struct iovec *iv;
929 int i, left, siz;
930
931 left = len;
932 m3 = NULL;
933 /*
934 * Generate the mbuf list with the uio_iov ref. to it.
935 */
936 i = 0;
937 while (left > 0) {
938 NFSMGET(m);
939 MCLGET(m, M_WAITOK);
940 m->m_len = 0;
941 siz = min(M_TRAILINGSPACE(m), left);
942 left -= siz;
943 i++;
944 if (m3)
945 m2->m_next = m;
946 else
947 m3 = m;
948 m2 = m;
949 }
950 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
951 m = m3;
952 left = len;
953 i = 0;
954 while (left > 0) {
955 if (m == NULL)
956 panic("nfsrv_createiovec iov");
957 siz = min(M_TRAILINGSPACE(m), left);
958 if (siz > 0) {
959 iv->iov_base = mtod(m, caddr_t) + m->m_len;
960 iv->iov_len = siz;
961 m->m_len += siz;
962 left -= siz;
963 iv++;
964 i++;
965 }
966 m = m->m_next;
967 }
968 *mpp = m3;
969 *mpendp = m2;
970 return (i);
971 }
972
973 /*
974 * Create an mbuf chain and an associated iovec that can be used to Read
975 * or Getextattr of data.
976 * Upon success, return pointers to the first and last mbufs in the chain
977 * plus the malloc'd iovec and its iovlen.
978 * Same as above, but creates ext_pgs mbuf(s).
979 */
980 static int
nfsrv_createiovec_extpgs(int len,int maxextsiz,struct mbuf ** mpp,struct mbuf ** mpendp,struct iovec ** ivp)981 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
982 struct mbuf **mpendp, struct iovec **ivp)
983 {
984 struct mbuf *m, *m2 = NULL, *m3;
985 struct iovec *iv;
986 int i, left, pgno, siz;
987
988 left = len;
989 m3 = NULL;
990 /*
991 * Generate the mbuf list with the uio_iov ref. to it.
992 */
993 i = 0;
994 while (left > 0) {
995 siz = min(left, maxextsiz);
996 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
997 left -= siz;
998 i += m->m_epg_npgs;
999 if (m3 != NULL)
1000 m2->m_next = m;
1001 else
1002 m3 = m;
1003 m2 = m;
1004 }
1005 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
1006 m = m3;
1007 left = len;
1008 i = 0;
1009 pgno = 0;
1010 while (left > 0) {
1011 if (m == NULL)
1012 panic("nfsvno_createiovec_extpgs iov");
1013 siz = min(PAGE_SIZE, left);
1014 if (siz > 0) {
1015 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
1016 iv->iov_len = siz;
1017 m->m_len += siz;
1018 if (pgno == m->m_epg_npgs - 1)
1019 m->m_epg_last_len = siz;
1020 left -= siz;
1021 iv++;
1022 i++;
1023 pgno++;
1024 }
1025 if (pgno == m->m_epg_npgs && left > 0) {
1026 m = m->m_next;
1027 if (m == NULL)
1028 panic("nfsvno_createiovec_extpgs iov");
1029 pgno = 0;
1030 }
1031 }
1032 *mpp = m3;
1033 *mpendp = m2;
1034 return (i);
1035 }
1036
1037 /*
1038 * Read vnode op call into mbuf list.
1039 */
1040 int
nfsvno_read(struct vnode * vp,off_t off,int cnt,struct ucred * cred,int maxextsiz,struct thread * p,struct mbuf ** mpp,struct mbuf ** mpendp)1041 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
1042 int maxextsiz, struct thread *p, struct mbuf **mpp,
1043 struct mbuf **mpendp)
1044 {
1045 struct mbuf *m;
1046 struct iovec *iv;
1047 int error = 0, len, tlen, ioflag = 0;
1048 struct mbuf *m3;
1049 struct uio io, *uiop = &io;
1050 struct nfsheur *nh;
1051
1052 /*
1053 * Attempt to read from a DS file. A return of ENOENT implies
1054 * there is no DS file to read.
1055 */
1056 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
1057 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
1058 if (error != ENOENT)
1059 return (error);
1060
1061 len = NFSM_RNDUP(cnt);
1062 if (maxextsiz > 0)
1063 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
1064 &m3, &m, &iv);
1065 else
1066 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
1067 uiop->uio_iov = iv;
1068 uiop->uio_offset = off;
1069 uiop->uio_resid = len;
1070 uiop->uio_rw = UIO_READ;
1071 uiop->uio_segflg = UIO_SYSSPACE;
1072 uiop->uio_td = NULL;
1073 nh = nfsrv_sequential_heuristic(uiop, vp);
1074 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
1075 /* XXX KDM make this more systematic? */
1076 NFSD_VNET(nfsstatsv1_p)->srvbytes[NFSV4OP_READ] += uiop->uio_resid;
1077 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
1078 free(iv, M_TEMP);
1079 if (error) {
1080 m_freem(m3);
1081 *mpp = NULL;
1082 goto out;
1083 }
1084 nh->nh_nextoff = uiop->uio_offset;
1085 tlen = len - uiop->uio_resid;
1086 cnt = cnt < tlen ? cnt : tlen;
1087 tlen = NFSM_RNDUP(cnt);
1088 if (tlen == 0) {
1089 m_freem(m3);
1090 m3 = m = NULL;
1091 } else if (len != tlen || tlen != cnt)
1092 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
1093 *mpp = m3;
1094 *mpendp = m;
1095
1096 out:
1097 NFSEXITCODE(error);
1098 return (error);
1099 }
1100
1101 /*
1102 * Create the iovec for the mbuf chain passed in as an argument.
1103 * The "cp" argument is where the data starts within the first mbuf in
1104 * the chain. It returns the iovec and the iovcnt.
1105 */
1106 static int
nfsrv_createiovecw(int retlen,struct mbuf * m,char * cp,struct iovec ** ivpp,int * iovcntp)1107 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
1108 int *iovcntp)
1109 {
1110 struct mbuf *mp;
1111 struct iovec *ivp;
1112 int cnt, i, len;
1113
1114 /*
1115 * Loop through the mbuf chain, counting how many mbufs are a
1116 * part of this write operation, so the iovec size is known.
1117 */
1118 cnt = 0;
1119 len = retlen;
1120 mp = m;
1121 i = mtod(mp, caddr_t) + mp->m_len - cp;
1122 while (len > 0) {
1123 if (i > 0) {
1124 len -= i;
1125 cnt++;
1126 }
1127 mp = mp->m_next;
1128 if (!mp) {
1129 if (len > 0)
1130 return (EBADRPC);
1131 } else
1132 i = mp->m_len;
1133 }
1134
1135 /* Now, create the iovec. */
1136 mp = m;
1137 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1138 M_WAITOK);
1139 *iovcntp = cnt;
1140 i = mtod(mp, caddr_t) + mp->m_len - cp;
1141 len = retlen;
1142 while (len > 0) {
1143 if (mp == NULL)
1144 panic("nfsrv_createiovecw");
1145 if (i > 0) {
1146 i = min(i, len);
1147 ivp->iov_base = cp;
1148 ivp->iov_len = i;
1149 ivp++;
1150 len -= i;
1151 }
1152 mp = mp->m_next;
1153 if (mp) {
1154 i = mp->m_len;
1155 cp = mtod(mp, caddr_t);
1156 }
1157 }
1158 return (0);
1159 }
1160
1161 /*
1162 * Write vnode op from an mbuf list.
1163 */
1164 int
nfsvno_write(struct vnode * vp,off_t off,int retlen,int * stable,struct mbuf * mp,char * cp,struct ucred * cred,struct thread * p)1165 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1166 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1167 {
1168 struct iovec *iv;
1169 int cnt, ioflags, error;
1170 struct uio io, *uiop = &io;
1171 struct nfsheur *nh;
1172
1173 /*
1174 * Attempt to write to a DS file. A return of ENOENT implies
1175 * there is no DS file to write.
1176 */
1177 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1178 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1179 if (error != ENOENT) {
1180 *stable = NFSWRITE_FILESYNC;
1181 return (error);
1182 }
1183
1184 if (*stable == NFSWRITE_UNSTABLE)
1185 ioflags = IO_NODELOCKED;
1186 else
1187 ioflags = (IO_SYNC | IO_NODELOCKED);
1188 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1189 if (error != 0)
1190 return (error);
1191 uiop->uio_iov = iv;
1192 uiop->uio_iovcnt = cnt;
1193 uiop->uio_resid = retlen;
1194 uiop->uio_rw = UIO_WRITE;
1195 uiop->uio_segflg = UIO_SYSSPACE;
1196 NFSUIOPROC(uiop, p);
1197 uiop->uio_offset = off;
1198 nh = nfsrv_sequential_heuristic(uiop, vp);
1199 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1200 /* XXX KDM make this more systematic? */
1201 NFSD_VNET(nfsstatsv1_p)->srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1202 error = VOP_WRITE(vp, uiop, ioflags, cred);
1203 if (error == 0)
1204 nh->nh_nextoff = uiop->uio_offset;
1205 free(iv, M_TEMP);
1206
1207 NFSEXITCODE(error);
1208 return (error);
1209 }
1210
1211 /*
1212 * Common code for creating a regular file (plus special files for V2).
1213 */
1214 int
nfsvno_createsub(struct nfsrv_descript * nd,struct nameidata * ndp,struct vnode ** vpp,struct nfsvattr * nvap,int * exclusive_flagp,int32_t * cverf,NFSDEV_T rdev,struct nfsexstuff * exp)1215 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1216 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1217 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1218 {
1219 u_quad_t tempsize;
1220 int error;
1221 struct thread *p = curthread;
1222
1223 error = nd->nd_repstat;
1224 if (!error && ndp->ni_vp == NULL) {
1225 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1226 error = VOP_CREATE(ndp->ni_dvp,
1227 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1228 /* For a pNFS server, create the data file on a DS. */
1229 if (error == 0 && nvap->na_type == VREG) {
1230 /*
1231 * Create a data file on a DS for a pNFS server.
1232 * This function just returns if not
1233 * running a pNFS DS or the creation fails.
1234 */
1235 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1236 nd->nd_cred, p);
1237 }
1238 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1239 NULL, false);
1240 nfsvno_relpathbuf(ndp);
1241 if (!error) {
1242 if (*exclusive_flagp) {
1243 *exclusive_flagp = 0;
1244 NFSVNO_ATTRINIT(nvap);
1245 nvap->na_atime.tv_sec = cverf[0];
1246 nvap->na_atime.tv_nsec = cverf[1];
1247 error = VOP_SETATTR(ndp->ni_vp,
1248 &nvap->na_vattr, nd->nd_cred);
1249 if (error != 0) {
1250 vput(ndp->ni_vp);
1251 ndp->ni_vp = NULL;
1252 error = NFSERR_NOTSUPP;
1253 }
1254 }
1255 }
1256 /*
1257 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1258 * (This implies, just get out on an error.)
1259 */
1260 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1261 nvap->na_type == VFIFO) {
1262 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1263 nvap->na_type = VFIFO;
1264 if (nvap->na_type != VFIFO &&
1265 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1266 nfsvno_relpathbuf(ndp);
1267 vput(ndp->ni_dvp);
1268 goto out;
1269 }
1270 nvap->na_rdev = rdev;
1271 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1272 &ndp->ni_cnd, &nvap->na_vattr);
1273 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1274 NULL, false);
1275 nfsvno_relpathbuf(ndp);
1276 if (error)
1277 goto out;
1278 } else {
1279 nfsvno_relpathbuf(ndp);
1280 vput(ndp->ni_dvp);
1281 error = ENXIO;
1282 goto out;
1283 }
1284 *vpp = ndp->ni_vp;
1285 } else {
1286 /*
1287 * Handle cases where error is already set and/or
1288 * the file exists.
1289 * 1 - clean up the lookup
1290 * 2 - iff !error and na_size set, truncate it
1291 */
1292 nfsvno_relpathbuf(ndp);
1293 *vpp = ndp->ni_vp;
1294 if (ndp->ni_dvp == *vpp)
1295 vrele(ndp->ni_dvp);
1296 else
1297 vput(ndp->ni_dvp);
1298 if (!error && nvap->na_size != VNOVAL) {
1299 error = nfsvno_accchk(*vpp, VWRITE,
1300 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1301 NFSACCCHK_VPISLOCKED, NULL);
1302 if (!error) {
1303 tempsize = nvap->na_size;
1304 NFSVNO_ATTRINIT(nvap);
1305 nvap->na_size = tempsize;
1306 error = nfsvno_setattr(*vpp, nvap,
1307 nd->nd_cred, p, exp);
1308 }
1309 }
1310 if (error)
1311 vput(*vpp);
1312 }
1313
1314 out:
1315 NFSEXITCODE(error);
1316 return (error);
1317 }
1318
1319 /*
1320 * Do a mknod vnode op.
1321 */
1322 int
nfsvno_mknod(struct nameidata * ndp,struct nfsvattr * nvap,struct ucred * cred,struct thread * p)1323 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1324 struct thread *p)
1325 {
1326 int error = 0;
1327 __enum_uint8(vtype) vtyp;
1328
1329 vtyp = nvap->na_type;
1330 /*
1331 * Iff doesn't exist, create it.
1332 */
1333 if (ndp->ni_vp) {
1334 nfsvno_relpathbuf(ndp);
1335 vput(ndp->ni_dvp);
1336 vrele(ndp->ni_vp);
1337 error = EEXIST;
1338 goto out;
1339 }
1340 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1341 nfsvno_relpathbuf(ndp);
1342 vput(ndp->ni_dvp);
1343 error = NFSERR_BADTYPE;
1344 goto out;
1345 }
1346 if (vtyp == VSOCK) {
1347 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1348 &ndp->ni_cnd, &nvap->na_vattr);
1349 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1350 false);
1351 nfsvno_relpathbuf(ndp);
1352 } else {
1353 if (nvap->na_type != VFIFO &&
1354 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1355 nfsvno_relpathbuf(ndp);
1356 vput(ndp->ni_dvp);
1357 goto out;
1358 }
1359 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1360 &ndp->ni_cnd, &nvap->na_vattr);
1361 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1362 false);
1363 nfsvno_relpathbuf(ndp);
1364 /*
1365 * Since VOP_MKNOD returns the ni_vp, I can't
1366 * see any reason to do the lookup.
1367 */
1368 }
1369
1370 out:
1371 NFSEXITCODE(error);
1372 return (error);
1373 }
1374
1375 /*
1376 * Mkdir vnode op.
1377 */
1378 int
nfsvno_mkdir(struct nameidata * ndp,struct nfsvattr * nvap,uid_t saved_uid,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1379 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1380 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1381 {
1382 int error = 0;
1383
1384 if (ndp->ni_vp != NULL) {
1385 if (ndp->ni_dvp == ndp->ni_vp)
1386 vrele(ndp->ni_dvp);
1387 else
1388 vput(ndp->ni_dvp);
1389 vrele(ndp->ni_vp);
1390 nfsvno_relpathbuf(ndp);
1391 error = EEXIST;
1392 goto out;
1393 }
1394 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1395 &nvap->na_vattr);
1396 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL, false);
1397 nfsvno_relpathbuf(ndp);
1398
1399 out:
1400 NFSEXITCODE(error);
1401 return (error);
1402 }
1403
1404 /*
1405 * symlink vnode op.
1406 */
1407 int
nfsvno_symlink(struct nameidata * ndp,struct nfsvattr * nvap,char * pathcp,int pathlen,int not_v2,uid_t saved_uid,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1408 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1409 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1410 struct nfsexstuff *exp)
1411 {
1412 int error = 0;
1413
1414 if (ndp->ni_vp) {
1415 nfsvno_relpathbuf(ndp);
1416 if (ndp->ni_dvp == ndp->ni_vp)
1417 vrele(ndp->ni_dvp);
1418 else
1419 vput(ndp->ni_dvp);
1420 vrele(ndp->ni_vp);
1421 error = EEXIST;
1422 goto out;
1423 }
1424
1425 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1426 &nvap->na_vattr, pathcp);
1427 /*
1428 * Although FreeBSD still had the lookup code in
1429 * it for 7/current, there doesn't seem to be any
1430 * point, since VOP_SYMLINK() returns the ni_vp.
1431 * Just vput it for v2.
1432 */
1433 VOP_VPUT_PAIR(ndp->ni_dvp, &ndp->ni_vp, !not_v2 && error == 0);
1434 nfsvno_relpathbuf(ndp);
1435
1436 out:
1437 NFSEXITCODE(error);
1438 return (error);
1439 }
1440
1441 /*
1442 * Parse symbolic link arguments.
1443 * This function has an ugly side effect. It will malloc() an area for
1444 * the symlink and set iov_base to point to it, only if it succeeds.
1445 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1446 * be FREE'd later.
1447 */
1448 int
nfsvno_getsymlink(struct nfsrv_descript * nd,struct nfsvattr * nvap,struct thread * p,char ** pathcpp,int * lenp)1449 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1450 struct thread *p, char **pathcpp, int *lenp)
1451 {
1452 u_int32_t *tl;
1453 char *pathcp = NULL;
1454 int error = 0, len;
1455 struct nfsv2_sattr *sp;
1456
1457 *pathcpp = NULL;
1458 *lenp = 0;
1459 if ((nd->nd_flag & ND_NFSV3) &&
1460 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, NULL, p)))
1461 goto nfsmout;
1462 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1463 len = fxdr_unsigned(int, *tl);
1464 if (len > NFS_MAXPATHLEN || len <= 0) {
1465 error = EBADRPC;
1466 goto nfsmout;
1467 }
1468 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1469 error = nfsrv_mtostr(nd, pathcp, len);
1470 if (error)
1471 goto nfsmout;
1472 if (nd->nd_flag & ND_NFSV2) {
1473 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1474 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1475 }
1476 *pathcpp = pathcp;
1477 *lenp = len;
1478 NFSEXITCODE2(0, nd);
1479 return (0);
1480 nfsmout:
1481 if (pathcp)
1482 free(pathcp, M_TEMP);
1483 NFSEXITCODE2(error, nd);
1484 return (error);
1485 }
1486
1487 /*
1488 * Remove a non-directory object.
1489 */
1490 int
nfsvno_removesub(struct nameidata * ndp,bool is_v4,struct nfsrv_descript * nd,struct thread * p,struct nfsexstuff * exp)1491 nfsvno_removesub(struct nameidata *ndp, bool is_v4, struct nfsrv_descript *nd,
1492 struct thread *p, struct nfsexstuff *exp)
1493 {
1494 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS], *newvp;
1495 struct mount *mp;
1496 int error = 0, mirrorcnt, ret;
1497 char fname[PNFS_FILENAME_LEN + 1];
1498 fhandle_t fh;
1499
1500 vp = ndp->ni_vp;
1501 dsdvp[0] = NULL;
1502 if (vp->v_type == VDIR) {
1503 error = NFSERR_ISDIR;
1504 } else if (is_v4) {
1505 if (nfsrv_recalldeleg || (nd->nd_flag & ND_NFSV41) == 0)
1506 error = nfsrv_checkremove(vp, 1, NULL,
1507 (nfsquad_t)((u_quad_t)0), p);
1508 else
1509 error = nfsrv_checkremove(vp, 1, NULL, nd->nd_clientid,
1510 p);
1511 }
1512 if (error == 0)
1513 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1514 if (!error)
1515 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1516 if (error == 0 && dsdvp[0] != NULL)
1517 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1518 if (is_v4 && (nd->nd_flag & ND_NFSV41) != 0 && error == 0)
1519 error = nfsvno_getfh(vp, &fh, p);
1520 if (ndp->ni_dvp == vp)
1521 vrele(ndp->ni_dvp);
1522 else
1523 vput(ndp->ni_dvp);
1524 vput(vp);
1525
1526 /* Use ret to determine if the file still exists. */
1527 if (is_v4 && (nd->nd_flag & ND_NFSV41) != 0 && error == 0) {
1528 mp = vfs_busyfs(&fh.fh_fsid);
1529 if (mp != NULL) {
1530 /* Find out if the file still exists. */
1531 ret = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &newvp);
1532 if (ret == 0)
1533 vput(newvp);
1534 else
1535 ret = ESTALE;
1536 vfs_unbusy(mp);
1537 } else {
1538 ret = ESTALE;
1539 }
1540 if (ret == ESTALE) {
1541 /* Get rid of any delegation. */
1542 nfsrv_removedeleg(&fh, nd, p);
1543 }
1544 }
1545
1546 nfsvno_relpathbuf(ndp);
1547 NFSEXITCODE(error);
1548 return (error);
1549 }
1550
1551 /*
1552 * Remove a directory.
1553 */
1554 int
nfsvno_rmdirsub(struct nameidata * ndp,int is_v4,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1555 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1556 struct thread *p, struct nfsexstuff *exp)
1557 {
1558 struct vnode *vp;
1559 int error = 0;
1560
1561 vp = ndp->ni_vp;
1562 if (vp->v_type != VDIR) {
1563 error = ENOTDIR;
1564 goto out;
1565 }
1566 /*
1567 * No rmdir "." please.
1568 */
1569 if (ndp->ni_dvp == vp) {
1570 error = EINVAL;
1571 goto out;
1572 }
1573 /*
1574 * The root of a mounted filesystem cannot be deleted.
1575 */
1576 if (vp->v_vflag & VV_ROOT)
1577 error = EBUSY;
1578 out:
1579 if (!error)
1580 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1581 if (ndp->ni_dvp == vp)
1582 vrele(ndp->ni_dvp);
1583 else
1584 vput(ndp->ni_dvp);
1585 vput(vp);
1586 nfsvno_relpathbuf(ndp);
1587 NFSEXITCODE(error);
1588 return (error);
1589 }
1590
1591 /*
1592 * Rename vnode op.
1593 */
1594 int
nfsvno_rename(struct nameidata * fromndp,struct nameidata * tondp,struct nfsrv_descript * nd,struct thread * p)1595 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1596 struct nfsrv_descript *nd, struct thread *p)
1597 {
1598 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS], *newvp;
1599 struct mount *mp;
1600 int error = 0, mirrorcnt, ret;
1601 char fname[PNFS_FILENAME_LEN + 1];
1602 fhandle_t fh, fh2;
1603
1604 dsdvp[0] = NULL;
1605 fvp = fromndp->ni_vp;
1606 if (nd->nd_repstat != 0) {
1607 vrele(fromndp->ni_dvp);
1608 vrele(fvp);
1609 error = nd->nd_repstat;
1610 goto out1;
1611 }
1612 tdvp = tondp->ni_dvp;
1613 tvp = tondp->ni_vp;
1614 if (tvp != NULL) {
1615 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1616 error = (nd->nd_flag & ND_NFSV2) ? EISDIR : EEXIST;
1617 goto out;
1618 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1619 error = (nd->nd_flag & ND_NFSV2) ? ENOTDIR : EEXIST;
1620 goto out;
1621 }
1622 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1623 error = (nd->nd_flag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1624 goto out;
1625 }
1626
1627 /*
1628 * A rename to '.' or '..' results in a prematurely
1629 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1630 * here.
1631 */
1632 if ((tondp->ni_cnd.cn_namelen == 1 &&
1633 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1634 (tondp->ni_cnd.cn_namelen == 2 &&
1635 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1636 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1637 error = EINVAL;
1638 goto out;
1639 }
1640 }
1641 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1642 error = (nd->nd_flag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1643 goto out;
1644 }
1645 if (fvp->v_mount != tdvp->v_mount) {
1646 error = (nd->nd_flag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1647 goto out;
1648 }
1649 if (fvp == tdvp) {
1650 error = (nd->nd_flag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1651 goto out;
1652 }
1653 if (fvp == tvp) {
1654 /*
1655 * If source and destination are the same, there is
1656 * nothing to do. Set error to EJUSTRETURN to indicate
1657 * this.
1658 */
1659 error = EJUSTRETURN;
1660 goto out;
1661 }
1662 if (nd->nd_flag & ND_NFSV4) {
1663 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1664 if (nfsrv_recalldeleg || (nd->nd_flag & ND_NFSV41) == 0)
1665 error = nfsrv_checkremove(fvp, 0, NULL,
1666 (nfsquad_t)((u_quad_t)0), p);
1667 else
1668 error = nfsrv_checkremove(fvp, 0, NULL,
1669 nd->nd_clientid, p);
1670 NFSVOPUNLOCK(fvp);
1671 } else
1672 error = EPERM;
1673 if (tvp && !error) {
1674 if (nfsrv_recalldeleg || (nd->nd_flag & ND_NFSV41) == 0)
1675 error = nfsrv_checkremove(tvp, 1, NULL,
1676 (nfsquad_t)((u_quad_t)0), p);
1677 else
1678 error = nfsrv_checkremove(tvp, 1, NULL,
1679 nd->nd_clientid, p);
1680 }
1681 } else {
1682 /*
1683 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1684 * that the NFSv4 client won't be confused by the rename.
1685 * Since nfsd_recalldelegation() can only be called on an
1686 * unlocked vnode at this point and fvp is the file that will
1687 * still exist after the rename, just do fvp.
1688 */
1689 nfsd_recalldelegation(fvp, p);
1690 }
1691 if (error == 0 && tvp != NULL) {
1692 if ((nd->nd_flag & ND_NFSV41) != 0)
1693 error = nfsvno_getfh(tvp, &fh2, p);
1694 if (error == 0)
1695 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname,
1696 &fh);
1697 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1698 " dsdvp=%p\n", dsdvp[0]);
1699 }
1700 out:
1701 mp = NULL;
1702 if (error == 0) {
1703 error = VOP_GETWRITEMOUNT(tondp->ni_dvp, &mp);
1704 if (error == 0) {
1705 if (mp == NULL) {
1706 error = ENOENT;
1707 } else {
1708 error = lockmgr(&mp->mnt_renamelock,
1709 LK_EXCLUSIVE | LK_NOWAIT, NULL);
1710 if (error != 0)
1711 error = ERELOOKUP;
1712 }
1713 }
1714 }
1715 if (error == 0) {
1716 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1717 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1718 &tondp->ni_cnd);
1719 lockmgr(&mp->mnt_renamelock, LK_RELEASE, 0);
1720 vfs_rel(mp);
1721 } else {
1722 if (tdvp == tvp)
1723 vrele(tdvp);
1724 else
1725 vput(tdvp);
1726 if (tvp)
1727 vput(tvp);
1728 vrele(fromndp->ni_dvp);
1729 vrele(fvp);
1730 if (error == EJUSTRETURN) {
1731 error = 0;
1732 } else if (error == ERELOOKUP && mp != NULL) {
1733 lockmgr(&mp->mnt_renamelock, LK_EXCLUSIVE, 0);
1734 lockmgr(&mp->mnt_renamelock, LK_RELEASE, 0);
1735 vfs_rel(mp);
1736 }
1737 }
1738
1739 /*
1740 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1741 * if the rename succeeded, the DS file for the tvp needs to be
1742 * removed.
1743 */
1744 if (error == 0 && dsdvp[0] != NULL) {
1745 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1746 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1747 }
1748
1749 /* Use ret to determine if the file still exists. */
1750 if ((nd->nd_flag & ND_NFSV41) != 0 && error == 0) {
1751 mp = vfs_busyfs(&fh2.fh_fsid);
1752 if (mp != NULL) {
1753 /* Find out if the file still exists. */
1754 ret = VFS_FHTOVP(mp, &fh2.fh_fid, LK_SHARED, &newvp);
1755 if (ret == 0)
1756 vput(newvp);
1757 else
1758 ret = ESTALE;
1759 vfs_unbusy(mp);
1760 } else {
1761 ret = ESTALE;
1762 }
1763 if (ret == ESTALE) {
1764 /* Get rid of any delegation. */
1765 nfsrv_removedeleg(&fh2, nd, p);
1766 }
1767 }
1768
1769 nfsvno_relpathbuf(tondp);
1770 out1:
1771 nfsvno_relpathbuf(fromndp);
1772 NFSEXITCODE(error);
1773 return (error);
1774 }
1775
1776 /*
1777 * Link vnode op.
1778 */
1779 int
nfsvno_link(struct nameidata * ndp,struct vnode * vp,nfsquad_t clientid,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1780 nfsvno_link(struct nameidata *ndp, struct vnode *vp, nfsquad_t clientid,
1781 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1782 {
1783 struct vnode *xp;
1784 int error = 0;
1785
1786 xp = ndp->ni_vp;
1787 if (xp != NULL) {
1788 error = EEXIST;
1789 } else {
1790 xp = ndp->ni_dvp;
1791 if (vp->v_mount != xp->v_mount)
1792 error = EXDEV;
1793 }
1794 if (!error) {
1795 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1796 if (!VN_IS_DOOMED(vp)) {
1797 error = nfsrv_checkremove(vp, 0, NULL, clientid, p);
1798 if (error == 0)
1799 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1800 } else
1801 error = EPERM;
1802 if (ndp->ni_dvp == vp) {
1803 vrele(ndp->ni_dvp);
1804 NFSVOPUNLOCK(vp);
1805 } else {
1806 vref(vp);
1807 VOP_VPUT_PAIR(ndp->ni_dvp, &vp, true);
1808 }
1809 } else {
1810 if (ndp->ni_dvp == ndp->ni_vp)
1811 vrele(ndp->ni_dvp);
1812 else
1813 vput(ndp->ni_dvp);
1814 if (ndp->ni_vp)
1815 vrele(ndp->ni_vp);
1816 }
1817 nfsvno_relpathbuf(ndp);
1818 NFSEXITCODE(error);
1819 return (error);
1820 }
1821
1822 /*
1823 * Do the fsync() appropriate for the commit.
1824 */
1825 int
nfsvno_fsync(struct vnode * vp,u_int64_t off,int cnt,struct ucred * cred,struct thread * td)1826 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1827 struct thread *td)
1828 {
1829 int error = 0;
1830
1831 /*
1832 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1833 * file is done. At this time VOP_FSYNC does not accept offset and
1834 * byte count parameters so call VOP_FSYNC the whole file for now.
1835 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1836 * File systems that do not use the buffer cache (as indicated
1837 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1838 */
1839 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1840 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1841 /*
1842 * Give up and do the whole thing
1843 */
1844 vnode_pager_clean_sync(vp);
1845 error = VOP_FSYNC(vp, MNT_WAIT, td);
1846 } else {
1847 /*
1848 * Locate and synchronously write any buffers that fall
1849 * into the requested range. Note: we are assuming that
1850 * f_iosize is a power of 2.
1851 */
1852 int iosize = vp->v_mount->mnt_stat.f_iosize;
1853 int iomask = iosize - 1;
1854 struct bufobj *bo;
1855 daddr_t lblkno;
1856
1857 /*
1858 * Align to iosize boundary, super-align to page boundary.
1859 */
1860 if (off & iomask) {
1861 cnt += off & iomask;
1862 off &= ~(u_quad_t)iomask;
1863 }
1864 if (off & PAGE_MASK) {
1865 cnt += off & PAGE_MASK;
1866 off &= ~(u_quad_t)PAGE_MASK;
1867 }
1868 lblkno = off / iosize;
1869
1870 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1871 VM_OBJECT_WLOCK(vp->v_object);
1872 vm_object_page_clean(vp->v_object, off, off + cnt,
1873 OBJPC_SYNC);
1874 VM_OBJECT_WUNLOCK(vp->v_object);
1875 }
1876
1877 bo = &vp->v_bufobj;
1878 BO_LOCK(bo);
1879 while (cnt > 0) {
1880 struct buf *bp;
1881
1882 /*
1883 * If we have a buffer and it is marked B_DELWRI we
1884 * have to lock and write it. Otherwise the prior
1885 * write is assumed to have already been committed.
1886 *
1887 * gbincore() can return invalid buffers now so we
1888 * have to check that bit as well (though B_DELWRI
1889 * should not be set if B_INVAL is set there could be
1890 * a race here since we haven't locked the buffer).
1891 */
1892 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1893 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1894 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1895 BO_LOCK(bo);
1896 continue; /* retry */
1897 }
1898 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1899 B_DELWRI) {
1900 bremfree(bp);
1901 bp->b_flags &= ~B_ASYNC;
1902 bwrite(bp);
1903 ++nfs_commit_miss;
1904 } else
1905 BUF_UNLOCK(bp);
1906 BO_LOCK(bo);
1907 }
1908 ++nfs_commit_blks;
1909 if (cnt < iosize)
1910 break;
1911 cnt -= iosize;
1912 ++lblkno;
1913 }
1914 BO_UNLOCK(bo);
1915 }
1916 NFSEXITCODE(error);
1917 return (error);
1918 }
1919
1920 /*
1921 * Statfs vnode op.
1922 */
1923 int
nfsvno_statfs(struct vnode * vp,struct statfs * sf)1924 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1925 {
1926 struct statfs *tsf;
1927 int error;
1928
1929 tsf = NULL;
1930 if (nfsrv_devidcnt > 0) {
1931 /* For a pNFS service, get the DS numbers. */
1932 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1933 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1934 if (error != 0) {
1935 free(tsf, M_TEMP);
1936 tsf = NULL;
1937 }
1938 }
1939 error = VFS_STATFS(vp->v_mount, sf);
1940 if (error == 0) {
1941 if (tsf != NULL) {
1942 sf->f_blocks = tsf->f_blocks;
1943 sf->f_bavail = tsf->f_bavail;
1944 sf->f_bfree = tsf->f_bfree;
1945 sf->f_bsize = tsf->f_bsize;
1946 }
1947 /*
1948 * Since NFS handles these values as unsigned on the
1949 * wire, there is no way to represent negative values,
1950 * so set them to 0. Without this, they will appear
1951 * to be very large positive values for clients like
1952 * Solaris10.
1953 */
1954 if (sf->f_bavail < 0)
1955 sf->f_bavail = 0;
1956 if (sf->f_ffree < 0)
1957 sf->f_ffree = 0;
1958 }
1959 free(tsf, M_TEMP);
1960 NFSEXITCODE(error);
1961 return (error);
1962 }
1963
1964 /*
1965 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1966 * must handle nfsrv_opencheck() calls after any other access checks.
1967 */
1968 void
nfsvno_open(struct nfsrv_descript * nd,struct nameidata * ndp,nfsquad_t clientid,nfsv4stateid_t * stateidp,struct nfsstate * stp,int * exclusive_flagp,struct nfsvattr * nvap,int32_t * cverf,int create,NFSACL_T * aclp,NFSACL_T * daclp,nfsattrbit_t * attrbitp,struct ucred * cred,bool done_namei,struct nfsexstuff * exp,struct vnode ** vpp)1969 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1970 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1971 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1972 NFSACL_T *aclp, NFSACL_T *daclp, nfsattrbit_t *attrbitp, struct ucred *cred,
1973 bool done_namei, struct nfsexstuff *exp, struct vnode **vpp)
1974 {
1975 struct vattr va;
1976 struct vnode *vp = NULL;
1977 u_quad_t tempsize;
1978 struct nfsexstuff nes;
1979 struct thread *p = curthread;
1980 uint32_t oldrepstat;
1981 u_long savflags;
1982
1983 if (ndp->ni_vp == NULL) {
1984 /*
1985 * If nfsrv_opencheck() sets nd_repstat, done_namei needs to be
1986 * set true, since cleanup after nfsvno_namei() is needed.
1987 */
1988 oldrepstat = nd->nd_repstat;
1989 nd->nd_repstat = nfsrv_opencheck(clientid,
1990 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1991 if (nd->nd_repstat != 0 && oldrepstat == 0)
1992 done_namei = true;
1993 }
1994 if (!nd->nd_repstat) {
1995 if (ndp->ni_vp == NULL) {
1996 /*
1997 * Most file systems ignore va_flags for
1998 * VOP_CREATE(), however setting va_flags
1999 * for VOP_CREATE() causes problems for ZFS.
2000 * So disable them and let nfsrv_fixattr()
2001 * do them, as required.
2002 */
2003 savflags = nvap->na_flags;
2004 nvap->na_flags = VNOVAL;
2005 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
2006 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
2007 /* For a pNFS server, create the data file on a DS. */
2008 if (nd->nd_repstat == 0) {
2009 /*
2010 * Create a data file on a DS for a pNFS server.
2011 * This function just returns if not
2012 * running a pNFS DS or the creation fails.
2013 */
2014 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
2015 cred, p);
2016 }
2017 nvap->na_flags = savflags;
2018 VOP_VPUT_PAIR(ndp->ni_dvp, nd->nd_repstat == 0 ?
2019 &ndp->ni_vp : NULL, false);
2020 nfsvno_relpathbuf(ndp);
2021 if (!nd->nd_repstat) {
2022 if (*exclusive_flagp != NFSV4_EXCLUSIVE_NONE) {
2023 VATTR_NULL(&va);
2024 va.va_atime.tv_sec = cverf[0];
2025 va.va_atime.tv_nsec = cverf[1];
2026 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
2027 &va, cred);
2028 if (nd->nd_repstat != 0) {
2029 vput(ndp->ni_vp);
2030 ndp->ni_vp = NULL;
2031 nd->nd_repstat = NFSERR_NOTSUPP;
2032 } else {
2033 /*
2034 * Few clients set these
2035 * attributes in Open/Create
2036 * Exclusive_41. If this
2037 * changes, this should include
2038 * setting atime, instead of
2039 * the above.
2040 */
2041 if (*exclusive_flagp ==
2042 NFSV4_EXCLUSIVE_41 &&
2043 (NFSISSET_ATTRBIT(attrbitp,
2044 NFSATTRBIT_OWNER) ||
2045 NFSISSET_ATTRBIT(attrbitp,
2046 NFSATTRBIT_OWNERGROUP) ||
2047 NFSISSET_ATTRBIT(attrbitp,
2048 NFSATTRBIT_TIMEMODIFYSET)||
2049 NFSISSET_ATTRBIT(attrbitp,
2050 NFSATTRBIT_ARCHIVE) ||
2051 NFSISSET_ATTRBIT(attrbitp,
2052 NFSATTRBIT_HIDDEN) ||
2053 NFSISSET_ATTRBIT(attrbitp,
2054 NFSATTRBIT_SYSTEM) ||
2055 aclp != NULL ||
2056 daclp != NULL))
2057 nfsrv_fixattr(nd,
2058 ndp->ni_vp, nvap,
2059 aclp, daclp, p,
2060 attrbitp, true);
2061 NFSSETBIT_ATTRBIT(attrbitp,
2062 NFSATTRBIT_TIMEACCESS);
2063 }
2064 *exclusive_flagp = NFSV4_EXCLUSIVE_NONE;
2065 } else {
2066 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
2067 aclp, daclp, p, attrbitp, false);
2068 }
2069 }
2070 vp = ndp->ni_vp;
2071 } else {
2072 nfsvno_relpathbuf(ndp);
2073 vp = ndp->ni_vp;
2074 if (create == NFSV4OPEN_CREATE) {
2075 if (ndp->ni_dvp == vp)
2076 vrele(ndp->ni_dvp);
2077 else
2078 vput(ndp->ni_dvp);
2079 }
2080 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
2081 if (ndp->ni_cnd.cn_flags & RDONLY)
2082 NFSVNO_SETEXRDONLY(&nes);
2083 else
2084 NFSVNO_EXINIT(&nes);
2085 nd->nd_repstat = nfsvno_accchk(vp,
2086 VWRITE, cred, &nes, p,
2087 NFSACCCHK_NOOVERRIDE,
2088 NFSACCCHK_VPISLOCKED, NULL);
2089 nd->nd_repstat = nfsrv_opencheck(clientid,
2090 stateidp, stp, vp, nd, p, nd->nd_repstat);
2091 if (!nd->nd_repstat) {
2092 tempsize = nvap->na_size;
2093 NFSVNO_ATTRINIT(nvap);
2094 nvap->na_size = tempsize;
2095 nd->nd_repstat = nfsvno_setattr(vp,
2096 nvap, cred, p, exp);
2097 }
2098 } else if (vp->v_type == VREG) {
2099 nd->nd_repstat = nfsrv_opencheck(clientid,
2100 stateidp, stp, vp, nd, p, nd->nd_repstat);
2101 }
2102 }
2103 } else if (done_namei) {
2104 KASSERT(create == NFSV4OPEN_CREATE,
2105 ("nfsvno_open: not create"));
2106 /*
2107 * done_namei is set when nfsvno_namei() has completed
2108 * successfully, but a subsequent error was set in
2109 * nd_repstat. As such, cleanup of the nfsvno_namei()
2110 * results is required.
2111 */
2112 nfsvno_relpathbuf(ndp);
2113 if (ndp->ni_dvp == ndp->ni_vp)
2114 vrele(ndp->ni_dvp);
2115 else
2116 vput(ndp->ni_dvp);
2117 if (ndp->ni_vp)
2118 vput(ndp->ni_vp);
2119 }
2120 *vpp = vp;
2121
2122 NFSEXITCODE2(0, nd);
2123 }
2124
2125 /*
2126 * Updates the file rev and sets the mtime and ctime
2127 * to the current clock time, returning the va_filerev and va_Xtime
2128 * values.
2129 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
2130 */
2131 int
nfsvno_updfilerev(struct vnode * vp,struct nfsvattr * nvap,struct nfsrv_descript * nd,struct thread * p)2132 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
2133 struct nfsrv_descript *nd, struct thread *p)
2134 {
2135 struct vattr va;
2136
2137 VATTR_NULL(&va);
2138 vfs_timestamp(&va.va_mtime);
2139 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
2140 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
2141 if (VN_IS_DOOMED(vp))
2142 return (ESTALE);
2143 }
2144 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
2145 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
2146 return (0);
2147 }
2148
2149 /*
2150 * Glue routine to nfsv4_fillattr().
2151 */
2152 int
nfsvno_fillattr(struct nfsrv_descript * nd,struct mount * mp,struct vnode * vp,struct nfsvattr * nvap,fhandle_t * fhp,int rderror,nfsattrbit_t * attrbitp,struct ucred * cred,struct thread * p,int isdgram,int reterr,int supports_nfsv4acls,int at_root,uint64_t mounted_on_fileno,bool xattrsupp,bool has_hiddensystem,bool has_namedattr,uint32_t clone_blksize,bool has_caseinsensitive)2153 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
2154 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
2155 struct ucred *cred, struct thread *p, int isdgram, int reterr,
2156 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno,
2157 bool xattrsupp, bool has_hiddensystem, bool has_namedattr,
2158 uint32_t clone_blksize, bool has_caseinsensitive)
2159 {
2160 struct statfs *sf;
2161 int error;
2162
2163 sf = NULL;
2164 if (nfsrv_devidcnt > 0 &&
2165 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
2166 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
2167 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
2168 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
2169 error = nfsrv_pnfsstatfs(sf, mp);
2170 if (error != 0) {
2171 free(sf, M_TEMP);
2172 sf = NULL;
2173 }
2174 }
2175
2176 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
2177 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
2178 mounted_on_fileno, sf, xattrsupp, has_hiddensystem, has_namedattr,
2179 clone_blksize, NULL, has_caseinsensitive);
2180 free(sf, M_TEMP);
2181 NFSEXITCODE2(0, nd);
2182 return (error);
2183 }
2184
2185 /*
2186 * Convert a dirent d_type to a vnode type.
2187 */
nfs_dtypetovtype(struct nfsvattr * nvap,struct vnode * vp,uint8_t dtype)2188 static void nfs_dtypetovtype(struct nfsvattr *nvap, struct vnode *vp,
2189 uint8_t dtype)
2190 {
2191
2192 if ((vn_irflag_read(vp) & VIRF_NAMEDDIR) != 0) {
2193 nvap->na_type = VREG;
2194 nvap->na_bsdflags |= SFBSD_NAMEDATTR;
2195 } else if (dtype <= DT_WHT) {
2196 nvap->na_type = dtype_to_vnode[dtype];
2197 } else {
2198 nvap->na_type = VNON;
2199 }
2200 }
2201
2202 /* Since the Readdir vnode ops vary, put the entire functions in here. */
2203 /*
2204 * nfs readdir service
2205 * - mallocs what it thinks is enough to read
2206 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
2207 * - calls VOP_READDIR()
2208 * - loops around building the reply
2209 * if the output generated exceeds count break out of loop
2210 * The NFSM_CLGET macro is used here so that the reply will be packed
2211 * tightly in mbuf clusters.
2212 * - it trims out records with d_fileno == 0
2213 * this doesn't matter for Unix clients, but they might confuse clients
2214 * for other os'.
2215 * - it trims out records with d_type == DT_WHT
2216 * these cannot be seen through NFS (unless we extend the protocol)
2217 * The alternate call nfsrvd_readdirplus() does lookups as well.
2218 * PS: The NFS protocol spec. does not clarify what the "count" byte
2219 * argument is a count of.. just name strings and file id's or the
2220 * entire reply rpc or ...
2221 * I tried just file name and id sizes and it confused the Sun client,
2222 * so I am using the full rpc size now. The "paranoia.." comment refers
2223 * to including the status longwords that are not a part of the dir.
2224 * "entry" structures, but are in the rpc.
2225 */
2226 int
nfsrvd_readdir(struct nfsrv_descript * nd,int isdgram,struct vnode * vp,struct nfsexstuff * exp)2227 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2228 struct vnode *vp, struct nfsexstuff *exp)
2229 {
2230 struct dirent *dp;
2231 u_int32_t *tl;
2232 int dirlen;
2233 char *cpos, *cend, *rbuf;
2234 struct nfsvattr at;
2235 int nlen, error = 0, getret = 1;
2236 int siz, cnt, fullsiz, eofflag, ncookies;
2237 u_int64_t off, toff, verf __unused;
2238 uint64_t *cookies = NULL, *cookiep;
2239 struct uio io;
2240 struct iovec iv;
2241 int is_ufs;
2242 struct thread *p = curthread;
2243
2244 if (nd->nd_repstat) {
2245 nfsrv_postopattr(nd, getret, &at);
2246 goto out;
2247 }
2248 if (nd->nd_flag & ND_NFSV2) {
2249 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2250 off = fxdr_unsigned(u_quad_t, *tl++);
2251 } else {
2252 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2253 off = fxdr_hyper(tl);
2254 tl += 2;
2255 verf = fxdr_hyper(tl);
2256 tl += 2;
2257 }
2258 toff = off;
2259 cnt = fxdr_unsigned(int, *tl);
2260 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2261 cnt = NFS_SRVMAXDATA(nd);
2262 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2263 fullsiz = siz;
2264 if (nd->nd_flag & ND_NFSV3) {
2265 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2266 NULL);
2267 #if 0
2268 /*
2269 * va_filerev is not sufficient as a cookie verifier,
2270 * since it is not supposed to change when entries are
2271 * removed/added unless that offset cookies returned to
2272 * the client are no longer valid.
2273 */
2274 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2275 nd->nd_repstat = NFSERR_BAD_COOKIE;
2276 #endif
2277 }
2278 if (!nd->nd_repstat && vp->v_type != VDIR)
2279 nd->nd_repstat = NFSERR_NOTDIR;
2280 if (nd->nd_repstat == 0 && cnt == 0) {
2281 if (nd->nd_flag & ND_NFSV2)
2282 /* NFSv2 does not have NFSERR_TOOSMALL */
2283 nd->nd_repstat = EPERM;
2284 else
2285 nd->nd_repstat = NFSERR_TOOSMALL;
2286 }
2287 if (!nd->nd_repstat)
2288 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2289 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2290 NFSACCCHK_VPISLOCKED, NULL);
2291 if (nd->nd_repstat) {
2292 vput(vp);
2293 if (nd->nd_flag & ND_NFSV3)
2294 nfsrv_postopattr(nd, getret, &at);
2295 goto out;
2296 }
2297 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2298 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2299 again:
2300 eofflag = 0;
2301 if (cookies) {
2302 free(cookies, M_TEMP);
2303 cookies = NULL;
2304 }
2305
2306 iv.iov_base = rbuf;
2307 iv.iov_len = siz;
2308 io.uio_iov = &iv;
2309 io.uio_iovcnt = 1;
2310 io.uio_offset = (off_t)off;
2311 io.uio_resid = siz;
2312 io.uio_segflg = UIO_SYSSPACE;
2313 io.uio_rw = UIO_READ;
2314 io.uio_td = NULL;
2315 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2316 &cookies);
2317 off = (u_int64_t)io.uio_offset;
2318 if (io.uio_resid)
2319 siz -= io.uio_resid;
2320
2321 if (!cookies && !nd->nd_repstat)
2322 nd->nd_repstat = NFSERR_PERM;
2323 if (nd->nd_flag & ND_NFSV3) {
2324 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2325 if (!nd->nd_repstat)
2326 nd->nd_repstat = getret;
2327 }
2328
2329 /*
2330 * Handles the failed cases. nd->nd_repstat == 0 past here.
2331 */
2332 if (nd->nd_repstat) {
2333 vput(vp);
2334 free(rbuf, M_TEMP);
2335 if (cookies)
2336 free(cookies, M_TEMP);
2337 if (nd->nd_flag & ND_NFSV3)
2338 nfsrv_postopattr(nd, getret, &at);
2339 goto out;
2340 }
2341 /*
2342 * If nothing read, return eof
2343 * rpc reply
2344 */
2345 if (siz == 0) {
2346 vput(vp);
2347 if (nd->nd_flag & ND_NFSV2) {
2348 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2349 } else {
2350 nfsrv_postopattr(nd, getret, &at);
2351 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2352 txdr_hyper(at.na_filerev, tl);
2353 tl += 2;
2354 }
2355 *tl++ = newnfs_false;
2356 *tl = newnfs_true;
2357 free(rbuf, M_TEMP);
2358 free(cookies, M_TEMP);
2359 goto out;
2360 }
2361
2362 /*
2363 * Check for degenerate cases of nothing useful read.
2364 * If so go try again
2365 */
2366 cpos = rbuf;
2367 cend = rbuf + siz;
2368 dp = (struct dirent *)cpos;
2369 cookiep = cookies;
2370
2371 /*
2372 * For some reason FreeBSD's ufs_readdir() chooses to back the
2373 * directory offset up to a block boundary, so it is necessary to
2374 * skip over the records that precede the requested offset. This
2375 * requires the assumption that file offset cookies monotonically
2376 * increase.
2377 */
2378 while (cpos < cend && ncookies > 0 &&
2379 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2380 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2381 cpos += dp->d_reclen;
2382 dp = (struct dirent *)cpos;
2383 cookiep++;
2384 ncookies--;
2385 }
2386 if (cpos >= cend || ncookies == 0) {
2387 siz = fullsiz;
2388 toff = off;
2389 goto again;
2390 }
2391 vput(vp);
2392
2393 /*
2394 * If cnt > MCLBYTES and the reply will not be saved, use
2395 * ext_pgs mbufs for TLS.
2396 * For NFSv4.0, we do not know for sure if the reply will
2397 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2398 */
2399 if (cnt > MCLBYTES && siz > MCLBYTES &&
2400 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2401 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2402 nd->nd_flag |= ND_EXTPG;
2403
2404 /*
2405 * dirlen is the size of the reply, including all XDR and must
2406 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2407 * if the XDR should be included in "count", but to be safe, we do.
2408 * (Include the two booleans at the end of the reply in dirlen now.)
2409 */
2410 if (nd->nd_flag & ND_NFSV3) {
2411 nfsrv_postopattr(nd, getret, &at);
2412 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2413 txdr_hyper(at.na_filerev, tl);
2414 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2415 } else {
2416 dirlen = 2 * NFSX_UNSIGNED;
2417 }
2418
2419 /* Loop through the records and build reply */
2420 while (cpos < cend && ncookies > 0) {
2421 nlen = dp->d_namlen;
2422 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2423 nlen <= NFS_MAXNAMLEN) {
2424 if (nd->nd_flag & ND_NFSV3)
2425 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2426 else
2427 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2428 if (dirlen > cnt) {
2429 eofflag = 0;
2430 break;
2431 }
2432
2433 /*
2434 * Build the directory record xdr from
2435 * the dirent entry.
2436 */
2437 if (nd->nd_flag & ND_NFSV3) {
2438 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2439 *tl++ = newnfs_true;
2440 txdr_hyper(dp->d_fileno, tl);
2441 } else {
2442 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2443 *tl++ = newnfs_true;
2444 *tl = txdr_unsigned(dp->d_fileno);
2445 }
2446 (void) nfsm_strtom(nd, dp->d_name, nlen);
2447 if (nd->nd_flag & ND_NFSV3) {
2448 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2449 txdr_hyper(*cookiep, tl);
2450 } else {
2451 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2452 *tl = txdr_unsigned(*cookiep);
2453 }
2454 }
2455 cpos += dp->d_reclen;
2456 dp = (struct dirent *)cpos;
2457 cookiep++;
2458 ncookies--;
2459 }
2460 if (cpos < cend)
2461 eofflag = 0;
2462 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2463 *tl++ = newnfs_false;
2464 if (eofflag)
2465 *tl = newnfs_true;
2466 else
2467 *tl = newnfs_false;
2468 free(rbuf, M_TEMP);
2469 free(cookies, M_TEMP);
2470
2471 out:
2472 NFSEXITCODE2(0, nd);
2473 return (0);
2474 nfsmout:
2475 vput(vp);
2476 NFSEXITCODE2(error, nd);
2477 return (error);
2478 }
2479
2480 /*
2481 * Readdirplus for V3 and Readdir for V4.
2482 */
2483 int
nfsrvd_readdirplus(struct nfsrv_descript * nd,int isdgram,struct vnode * vp,struct nfsexstuff * exp)2484 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2485 struct vnode *vp, struct nfsexstuff *exp)
2486 {
2487 struct dirent *dp;
2488 uint32_t clone_blksize, *tl;
2489 int dirlen;
2490 char *cpos, *cend, *rbuf;
2491 struct vnode *nvp;
2492 fhandle_t nfh;
2493 struct nfsvattr nva, at, *nvap = &nva;
2494 struct mbuf *mb0, *mb1;
2495 struct nfsreferral *refp;
2496 int nlen, r, error = 0, getret = 1, ret, usevget = 1;
2497 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2498 caddr_t bpos0, bpos1;
2499 u_int64_t off, toff, verf __unused;
2500 uint64_t *cookies = NULL, *cookiep;
2501 nfsattrbit_t attrbits, rderrbits, savbits, refbits;
2502 struct uio io;
2503 struct iovec iv;
2504 struct componentname cn;
2505 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2506 struct mount *mp, *new_mp;
2507 uint64_t mounted_on_fileno;
2508 struct thread *p = curthread;
2509 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2510 size_t atsiz;
2511 long pathval;
2512 bool has_caseinsensitive, has_hiddensystem, has_namedattr, xattrsupp;
2513
2514 if (nd->nd_repstat) {
2515 nfsrv_postopattr(nd, getret, &at);
2516 goto out;
2517 }
2518 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2519 off = fxdr_hyper(tl);
2520 toff = off;
2521 tl += 2;
2522 verf = fxdr_hyper(tl);
2523 tl += 2;
2524 siz = fxdr_unsigned(int, *tl++);
2525 cnt = fxdr_unsigned(int, *tl);
2526
2527 /*
2528 * Use the server's maximum data transfer size as the upper bound
2529 * on reply datalen.
2530 */
2531 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2532 cnt = NFS_SRVMAXDATA(nd);
2533
2534 /*
2535 * siz is a "hint" of how much directory information (name, fileid,
2536 * cookie) should be in the reply. At least one client "hints" 0,
2537 * so I set it to cnt for that case. I also round it up to the
2538 * next multiple of DIRBLKSIZ.
2539 * Since the size of a Readdirplus directory entry reply will always
2540 * be greater than a directory entry returned by VOP_READDIR(), it
2541 * does not make sense to read more than NFS_SRVMAXDATA() via
2542 * VOP_READDIR().
2543 */
2544 if (siz <= 0)
2545 siz = cnt;
2546 else if (siz > NFS_SRVMAXDATA(nd))
2547 siz = NFS_SRVMAXDATA(nd);
2548 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2549
2550 if (nd->nd_flag & ND_NFSV4) {
2551 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2552 if (error)
2553 goto nfsmout;
2554 NFSSET_ATTRBIT(&savbits, &attrbits);
2555 NFSSET_ATTRBIT(&refbits, &attrbits);
2556 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2557 NFSZERO_ATTRBIT(&rderrbits);
2558 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2559 /*
2560 * If these 4 bits are the only attributes requested by the
2561 * client, they can be satisfied without acquiring the vnode
2562 * for the file object unless it is a directory.
2563 * This will be indicated by savbits being all 0s.
2564 */
2565 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_TYPE);
2566 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_FILEID);
2567 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_MOUNTEDONFILEID);
2568 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_RDATTRERROR);
2569 } else {
2570 NFSZERO_ATTRBIT(&attrbits);
2571 }
2572 fullsiz = siz;
2573 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2574 #if 0
2575 if (!nd->nd_repstat) {
2576 if (off && verf != at.na_filerev) {
2577 /*
2578 * va_filerev is not sufficient as a cookie verifier,
2579 * since it is not supposed to change when entries are
2580 * removed/added unless that offset cookies returned to
2581 * the client are no longer valid.
2582 */
2583 if (nd->nd_flag & ND_NFSV4) {
2584 nd->nd_repstat = NFSERR_NOTSAME;
2585 } else {
2586 nd->nd_repstat = NFSERR_BAD_COOKIE;
2587 }
2588 }
2589 }
2590 #endif
2591 if (!nd->nd_repstat && vp->v_type != VDIR)
2592 nd->nd_repstat = NFSERR_NOTDIR;
2593 if (!nd->nd_repstat && cnt == 0)
2594 nd->nd_repstat = NFSERR_TOOSMALL;
2595 if (!nd->nd_repstat)
2596 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2597 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2598 NFSACCCHK_VPISLOCKED, NULL);
2599 if (nd->nd_repstat) {
2600 vput(vp);
2601 if (nd->nd_flag & ND_NFSV3)
2602 nfsrv_postopattr(nd, getret, &at);
2603 goto out;
2604 }
2605 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2606 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2607
2608 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2609 again:
2610 eofflag = 0;
2611 if (cookies) {
2612 free(cookies, M_TEMP);
2613 cookies = NULL;
2614 }
2615
2616 iv.iov_base = rbuf;
2617 iv.iov_len = siz;
2618 io.uio_iov = &iv;
2619 io.uio_iovcnt = 1;
2620 io.uio_offset = (off_t)off;
2621 io.uio_resid = siz;
2622 io.uio_segflg = UIO_SYSSPACE;
2623 io.uio_rw = UIO_READ;
2624 io.uio_td = NULL;
2625 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2626 &cookies);
2627 off = (u_int64_t)io.uio_offset;
2628 if (io.uio_resid)
2629 siz -= io.uio_resid;
2630
2631 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2632
2633 if (!cookies && !nd->nd_repstat)
2634 nd->nd_repstat = NFSERR_PERM;
2635 if (!nd->nd_repstat)
2636 nd->nd_repstat = getret;
2637 if (nd->nd_repstat) {
2638 vput(vp);
2639 if (cookies)
2640 free(cookies, M_TEMP);
2641 free(rbuf, M_TEMP);
2642 if (nd->nd_flag & ND_NFSV3)
2643 nfsrv_postopattr(nd, getret, &at);
2644 goto out;
2645 }
2646 /*
2647 * If nothing read, return eof
2648 * rpc reply
2649 */
2650 if (siz == 0) {
2651 ateof:
2652 vput(vp);
2653 if (nd->nd_flag & ND_NFSV3)
2654 nfsrv_postopattr(nd, getret, &at);
2655 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2656 txdr_hyper(at.na_filerev, tl);
2657 tl += 2;
2658 *tl++ = newnfs_false;
2659 *tl = newnfs_true;
2660 free(cookies, M_TEMP);
2661 free(rbuf, M_TEMP);
2662 goto out;
2663 }
2664
2665 /*
2666 * Check for degenerate cases of nothing useful read.
2667 * If so go try again
2668 */
2669 cpos = rbuf;
2670 cend = rbuf + siz;
2671 dp = (struct dirent *)cpos;
2672 cookiep = cookies;
2673
2674 /*
2675 * For some reason FreeBSD's ufs_readdir() chooses to back the
2676 * directory offset up to a block boundary, so it is necessary to
2677 * skip over the records that precede the requested offset. This
2678 * requires the assumption that file offset cookies monotonically
2679 * increase.
2680 */
2681 while (cpos < cend && ncookies > 0 &&
2682 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2683 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2684 ((nd->nd_flag & ND_NFSV4) &&
2685 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2686 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2687 cpos += dp->d_reclen;
2688 dp = (struct dirent *)cpos;
2689 cookiep++;
2690 ncookies--;
2691 }
2692 if (cpos >= cend || ncookies == 0) {
2693 if (eofflag != 0)
2694 goto ateof;
2695 siz = fullsiz;
2696 toff = off;
2697 goto again;
2698 }
2699
2700 /*
2701 * Busy the file system so that the mount point won't go away
2702 * and, as such, VFS_VGET() can be used safely.
2703 */
2704 mp = vp->v_mount;
2705 vfs_ref(mp);
2706 NFSVOPUNLOCK(vp);
2707 nd->nd_repstat = vfs_busy(mp, 0);
2708 vfs_rel(mp);
2709 if (nd->nd_repstat != 0) {
2710 vrele(vp);
2711 free(cookies, M_TEMP);
2712 free(rbuf, M_TEMP);
2713 if (nd->nd_flag & ND_NFSV3)
2714 nfsrv_postopattr(nd, getret, &at);
2715 goto out;
2716 }
2717
2718 /*
2719 * Check to see if entries in this directory can be safely acquired
2720 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2721 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2722 * automount of the snapshot directory that is required will
2723 * be done.
2724 * This needs to be done here for NFSv4, since NFSv4 never does
2725 * a VFS_VGET() for "." or "..".
2726 */
2727 if (is_zfs == 1) {
2728 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2729 if (r == EOPNOTSUPP) {
2730 usevget = 0;
2731 cn.cn_nameiop = LOOKUP;
2732 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2733 cn.cn_cred = nd->nd_cred;
2734 } else if (r == 0)
2735 vput(nvp);
2736 }
2737
2738 /*
2739 * If the reply is likely to exceed MCLBYTES and the reply will
2740 * not be saved, use ext_pgs mbufs for TLS.
2741 * It is difficult to predict how large each entry will be and
2742 * how many entries have been read, so just assume the directory
2743 * entries grow by a factor of 4 when attributes are included.
2744 * For NFSv4.0, we do not know for sure if the reply will
2745 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2746 */
2747 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2748 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2749 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2750 nd->nd_flag |= ND_EXTPG;
2751
2752 /*
2753 * Save this position, in case there is an error before one entry
2754 * is created.
2755 */
2756 mb0 = nd->nd_mb;
2757 bpos0 = nd->nd_bpos;
2758 bextpg0 = nd->nd_bextpg;
2759 bextpgsiz0 = nd->nd_bextpgsiz;
2760
2761 /*
2762 * Fill in the first part of the reply.
2763 * dirlen is the reply length in bytes and cannot exceed cnt.
2764 * (Include the two booleans at the end of the reply in dirlen now,
2765 * so we recognize when we have exceeded cnt.)
2766 */
2767 if (nd->nd_flag & ND_NFSV3) {
2768 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2769 nfsrv_postopattr(nd, getret, &at);
2770 } else {
2771 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2772 }
2773 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2774 txdr_hyper(at.na_filerev, tl);
2775
2776 /*
2777 * Save this position, in case there is an empty reply needed.
2778 */
2779 mb1 = nd->nd_mb;
2780 bpos1 = nd->nd_bpos;
2781 bextpg1 = nd->nd_bextpg;
2782 bextpgsiz1 = nd->nd_bextpgsiz;
2783
2784 /* Loop through the records and build reply */
2785 entrycnt = 0;
2786 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2787 nlen = dp->d_namlen;
2788 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2789 nlen <= NFS_MAXNAMLEN &&
2790 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2791 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2792 || (nlen == 1 && dp->d_name[0] != '.'))) {
2793 /*
2794 * Save the current position in the reply, in case
2795 * this entry exceeds cnt.
2796 */
2797 mb1 = nd->nd_mb;
2798 bpos1 = nd->nd_bpos;
2799 bextpg1 = nd->nd_bextpg;
2800 bextpgsiz1 = nd->nd_bextpgsiz;
2801
2802 /*
2803 * For readdir_and_lookup get the vnode using
2804 * the file number.
2805 */
2806 nvp = NULL;
2807 refp = NULL;
2808 r = 0;
2809 at_root = 0;
2810 needs_unbusy = 0;
2811 new_mp = mp;
2812 mounted_on_fileno = (uint64_t)dp->d_fileno;
2813 if ((nd->nd_flag & ND_NFSV3) ||
2814 NFSNONZERO_ATTRBIT(&savbits) ||
2815 dp->d_type == DT_UNKNOWN ||
2816 (dp->d_type == DT_DIR &&
2817 nfsrv_enable_crossmntpt != 0)) {
2818 if (nd->nd_flag & ND_NFSV4)
2819 refp = nfsv4root_getreferral(NULL,
2820 vp, dp->d_fileno);
2821 if (refp == NULL) {
2822 if (usevget)
2823 r = VFS_VGET(mp, dp->d_fileno,
2824 LK_SHARED, &nvp);
2825 else
2826 r = EOPNOTSUPP;
2827 if (r == 0 && (vn_irflag_read(vp) &
2828 VIRF_NAMEDDIR) != 0)
2829 vn_irflag_set_cond(nvp,
2830 VIRF_NAMEDATTR);
2831 if (r == EOPNOTSUPP) {
2832 if (usevget) {
2833 usevget = 0;
2834 cn.cn_nameiop = LOOKUP;
2835 cn.cn_lkflags =
2836 LK_SHARED |
2837 LK_RETRY;
2838 cn.cn_cred =
2839 nd->nd_cred;
2840 }
2841 cn.cn_nameptr = dp->d_name;
2842 cn.cn_namelen = nlen;
2843 cn.cn_flags = ISLASTCN |
2844 NOFOLLOW | LOCKLEAF;
2845 if ((vn_irflag_read(vp) &
2846 VIRF_NAMEDDIR) != 0)
2847 cn.cn_flags |=
2848 OPENNAMED;
2849 if (nlen == 2 &&
2850 dp->d_name[0] == '.' &&
2851 dp->d_name[1] == '.')
2852 cn.cn_flags |=
2853 ISDOTDOT;
2854 if (NFSVOPLOCK(vp, LK_SHARED)
2855 != 0) {
2856 nd->nd_repstat = EPERM;
2857 break;
2858 }
2859 if ((vp->v_vflag & VV_ROOT) != 0
2860 && (cn.cn_flags & ISDOTDOT)
2861 != 0) {
2862 vref(vp);
2863 nvp = vp;
2864 r = 0;
2865 } else {
2866 r = VOP_LOOKUP(vp, &nvp,
2867 &cn);
2868 if (vp != nvp)
2869 NFSVOPUNLOCK(vp);
2870 }
2871 }
2872
2873 /*
2874 * For NFSv4, check to see if nvp is
2875 * a mount point and get the mount
2876 * point vnode, as required.
2877 */
2878 if (r == 0 &&
2879 nfsrv_enable_crossmntpt != 0 &&
2880 (nd->nd_flag & ND_NFSV4) != 0 &&
2881 nvp->v_type == VDIR &&
2882 nvp->v_mountedhere != NULL) {
2883 new_mp = nvp->v_mountedhere;
2884 r = vfs_busy(new_mp, 0);
2885 vput(nvp);
2886 nvp = NULL;
2887 if (r == 0) {
2888 r = VFS_ROOT(new_mp,
2889 LK_SHARED, &nvp);
2890 needs_unbusy = 1;
2891 if (r == 0)
2892 at_root = 1;
2893 }
2894 }
2895 }
2896
2897 /*
2898 * If we failed to look up the entry, then it
2899 * has become invalid, most likely removed.
2900 */
2901 if (r != 0) {
2902 if (needs_unbusy)
2903 vfs_unbusy(new_mp);
2904 goto invalid;
2905 }
2906 KASSERT(refp != NULL || nvp != NULL,
2907 ("%s: undetected lookup error", __func__));
2908
2909 if (refp == NULL &&
2910 ((nd->nd_flag & ND_NFSV3) ||
2911 NFSNONZERO_ATTRBIT(&attrbits))) {
2912 r = nfsvno_getfh(nvp, &nfh, p);
2913 if (!r)
2914 r = nfsvno_getattr(nvp, nvap, nd, p,
2915 1, &attrbits);
2916 if (r == 0 && is_zfs == 1 &&
2917 nfsrv_enable_crossmntpt != 0 &&
2918 (nd->nd_flag & ND_NFSV4) != 0 &&
2919 nvp->v_type == VDIR &&
2920 vp->v_mount != nvp->v_mount) {
2921 /*
2922 * For a ZFS snapshot, there is a
2923 * pseudo mount that does not set
2924 * v_mountedhere, so it needs to
2925 * be detected via a different
2926 * mount structure.
2927 */
2928 at_root = 1;
2929 if (new_mp == mp)
2930 new_mp = nvp->v_mount;
2931 }
2932 }
2933
2934 /*
2935 * If we failed to get attributes of the entry,
2936 * then just skip it for NFSv3 (the traditional
2937 * behavior in the old NFS server).
2938 * For NFSv4 the behavior is controlled by
2939 * RDATTRERROR: we either ignore the error or
2940 * fail the request.
2941 * The exception is EOPNOTSUPP, which can be
2942 * returned by nfsvno_getfh() for certain
2943 * file systems, such as devfs. This indicates
2944 * that the file system cannot be exported,
2945 * so just skip over the entry.
2946 * Note that RDATTRERROR is never set for NFSv3.
2947 */
2948 if (r != 0) {
2949 if (!NFSISSET_ATTRBIT(&attrbits,
2950 NFSATTRBIT_RDATTRERROR) ||
2951 r == EOPNOTSUPP) {
2952 vput(nvp);
2953 if (needs_unbusy != 0)
2954 vfs_unbusy(new_mp);
2955 if ((nd->nd_flag & ND_NFSV3) ||
2956 r == EOPNOTSUPP)
2957 goto invalid;
2958 nd->nd_repstat = r;
2959 break;
2960 }
2961 }
2962 } else if (NFSNONZERO_ATTRBIT(&attrbits)) {
2963 /* Only need Type and/or Fileid. */
2964 VATTR_NULL(&nvap->na_vattr);
2965 nvap->na_fileid = dp->d_fileno;
2966 nfs_dtypetovtype(nvap, vp, dp->d_type);
2967 }
2968
2969 /*
2970 * Build the directory record xdr
2971 */
2972 if (nd->nd_flag & ND_NFSV3) {
2973 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2974 *tl++ = newnfs_true;
2975 txdr_hyper(dp->d_fileno, tl);
2976 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2977 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2978 txdr_hyper(*cookiep, tl);
2979 nfsrv_postopattr(nd, 0, nvap);
2980 dirlen += nfsm_fhtom(NULL, nd, (u_int8_t *)&nfh,
2981 0, 1);
2982 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2983 if (nvp != NULL)
2984 vput(nvp);
2985 } else {
2986 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2987 *tl++ = newnfs_true;
2988 txdr_hyper(*cookiep, tl);
2989 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2990 xattrsupp = false;
2991 has_hiddensystem = false;
2992 has_namedattr = false;
2993 has_caseinsensitive = false;
2994 clone_blksize = 0;
2995 if (nvp != NULL) {
2996 supports_nfsv4acls =
2997 nfs_supportsacls(nvp);
2998 if (NFSISSET_ATTRBIT(&attrbits,
2999 NFSATTRBIT_XATTRSUPPORT)) {
3000 ret = VOP_GETEXTATTR(nvp,
3001 EXTATTR_NAMESPACE_USER,
3002 "xxx", NULL, &atsiz,
3003 nd->nd_cred, p);
3004 xattrsupp = ret != EOPNOTSUPP;
3005 }
3006 if (VOP_PATHCONF(nvp,
3007 _PC_HAS_HIDDENSYSTEM, &pathval) !=
3008 0)
3009 pathval = 0;
3010 has_hiddensystem = pathval > 0;
3011 pathval = 0;
3012 if (NFSISSET_ATTRBIT(&attrbits,
3013 NFSATTRBIT_NAMEDATTR) &&
3014 VOP_PATHCONF(nvp, _PC_HAS_NAMEDATTR,
3015 &pathval) != 0)
3016 pathval = 0;
3017 has_namedattr = pathval > 0;
3018 pathval = 0;
3019 if (VOP_PATHCONF(nvp, _PC_CLONE_BLKSIZE,
3020 &pathval) != 0)
3021 pathval = 0;
3022 clone_blksize = pathval;
3023 if (VOP_PATHCONF(nvp,
3024 _PC_CASE_INSENSITIVE,
3025 &pathval) != 0)
3026 pathval = 0;
3027 has_caseinsensitive = pathval > 0;
3028 NFSVOPUNLOCK(nvp);
3029 } else
3030 supports_nfsv4acls = 0;
3031 if (refp != NULL) {
3032 dirlen += nfsrv_putreferralattr(nd,
3033 &refbits, refp, 0,
3034 &nd->nd_repstat);
3035 if (nd->nd_repstat) {
3036 if (nvp != NULL)
3037 vrele(nvp);
3038 if (needs_unbusy != 0)
3039 vfs_unbusy(new_mp);
3040 break;
3041 }
3042 } else if (r) {
3043 dirlen += nfsvno_fillattr(nd, new_mp,
3044 nvp, nvap, &nfh, r, &rderrbits,
3045 nd->nd_cred, p, isdgram, 0,
3046 supports_nfsv4acls, at_root,
3047 mounted_on_fileno, xattrsupp,
3048 has_hiddensystem, has_namedattr,
3049 clone_blksize, has_caseinsensitive);
3050 } else {
3051 dirlen += nfsvno_fillattr(nd, new_mp,
3052 nvp, nvap, &nfh, r, &attrbits,
3053 nd->nd_cred, p, isdgram, 0,
3054 supports_nfsv4acls, at_root,
3055 mounted_on_fileno, xattrsupp,
3056 has_hiddensystem, has_namedattr,
3057 clone_blksize, has_caseinsensitive);
3058 }
3059 if (nvp != NULL)
3060 vrele(nvp);
3061 dirlen += (3 * NFSX_UNSIGNED);
3062 }
3063 if (needs_unbusy != 0)
3064 vfs_unbusy(new_mp);
3065 if (dirlen <= cnt)
3066 entrycnt++;
3067 }
3068 invalid:
3069 cpos += dp->d_reclen;
3070 dp = (struct dirent *)cpos;
3071 cookiep++;
3072 ncookies--;
3073 }
3074 vrele(vp);
3075 vfs_unbusy(mp);
3076
3077 /*
3078 * If dirlen > cnt, we must strip off the last entry. If that
3079 * results in an empty reply, report NFSERR_TOOSMALL.
3080 */
3081 if (dirlen > cnt || nd->nd_repstat) {
3082 if (!nd->nd_repstat && entrycnt == 0)
3083 nd->nd_repstat = NFSERR_TOOSMALL;
3084 if (nd->nd_repstat) {
3085 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
3086 if (nd->nd_flag & ND_NFSV3)
3087 nfsrv_postopattr(nd, getret, &at);
3088 } else
3089 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
3090 eofflag = 0;
3091 } else if (cpos < cend)
3092 eofflag = 0;
3093 if (!nd->nd_repstat) {
3094 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3095 *tl++ = newnfs_false;
3096 if (eofflag)
3097 *tl = newnfs_true;
3098 else
3099 *tl = newnfs_false;
3100 }
3101 free(cookies, M_TEMP);
3102 free(rbuf, M_TEMP);
3103
3104 out:
3105 NFSEXITCODE2(0, nd);
3106 return (0);
3107 nfsmout:
3108 vput(vp);
3109 NFSEXITCODE2(error, nd);
3110 return (error);
3111 }
3112
3113 /*
3114 * Get the settable attributes out of the mbuf list.
3115 * (Return 0 or EBADRPC)
3116 */
3117 int
nfsrv_sattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSACL_T * aclp,NFSACL_T * daclp,struct thread * p)3118 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
3119 nfsattrbit_t *attrbitp, NFSACL_T *aclp, NFSACL_T *daclp, struct thread *p)
3120 {
3121 u_int32_t *tl;
3122 struct nfsv2_sattr *sp;
3123 int error = 0, toclient = 0;
3124
3125 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
3126 case ND_NFSV2:
3127 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
3128 /*
3129 * Some old clients didn't fill in the high order 16bits.
3130 * --> check the low order 2 bytes for 0xffff
3131 */
3132 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
3133 nvap->na_mode = nfstov_mode(sp->sa_mode);
3134 if (sp->sa_uid != newnfs_xdrneg1)
3135 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
3136 if (sp->sa_gid != newnfs_xdrneg1)
3137 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
3138 if (sp->sa_size != newnfs_xdrneg1)
3139 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
3140 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
3141 #ifdef notyet
3142 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
3143 #else
3144 nvap->na_atime.tv_sec =
3145 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
3146 nvap->na_atime.tv_nsec = 0;
3147 #endif
3148 }
3149 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
3150 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
3151 break;
3152 case ND_NFSV3:
3153 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3154 if (*tl == newnfs_true) {
3155 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3156 nvap->na_mode = nfstov_mode(*tl);
3157 }
3158 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3159 if (*tl == newnfs_true) {
3160 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3161 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
3162 }
3163 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3164 if (*tl == newnfs_true) {
3165 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3166 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
3167 }
3168 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3169 if (*tl == newnfs_true) {
3170 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3171 nvap->na_size = fxdr_hyper(tl);
3172 }
3173 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3174 switch (fxdr_unsigned(int, *tl)) {
3175 case NFSV3SATTRTIME_TOCLIENT:
3176 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3177 fxdr_nfsv3time(tl, &nvap->na_atime);
3178 toclient = 1;
3179 break;
3180 case NFSV3SATTRTIME_TOSERVER:
3181 vfs_timestamp(&nvap->na_atime);
3182 nvap->na_vaflags |= VA_UTIMES_NULL;
3183 break;
3184 }
3185 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3186 switch (fxdr_unsigned(int, *tl)) {
3187 case NFSV3SATTRTIME_TOCLIENT:
3188 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3189 fxdr_nfsv3time(tl, &nvap->na_mtime);
3190 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3191 break;
3192 case NFSV3SATTRTIME_TOSERVER:
3193 vfs_timestamp(&nvap->na_mtime);
3194 if (!toclient)
3195 nvap->na_vaflags |= VA_UTIMES_NULL;
3196 break;
3197 }
3198 break;
3199 case ND_NFSV4:
3200 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, daclp, p);
3201 }
3202 nfsmout:
3203 NFSEXITCODE2(error, nd);
3204 return (error);
3205 }
3206
3207 /*
3208 * Handle the setable attributes for V4.
3209 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
3210 */
3211 int
nfsv4_sattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSACL_T * aclp,NFSACL_T * daclp,struct thread * p)3212 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
3213 nfsattrbit_t *attrbitp, NFSACL_T *aclp, NFSACL_T *daclp, struct thread *p)
3214 {
3215 u_int32_t *tl;
3216 int attrsum = 0;
3217 int i, j;
3218 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
3219 int moderet, toclient = 0;
3220 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
3221 uid_t uid;
3222 gid_t gid;
3223 u_short mode, mask; /* Same type as va_mode. */
3224 struct vattr va;
3225
3226 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
3227 if (error)
3228 goto nfsmout;
3229 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3230 attrsize = fxdr_unsigned(int, *tl);
3231
3232 /*
3233 * Loop around getting the setable attributes. If an unsupported
3234 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
3235 * Once nd_repstat != 0, do not set the attribute value, but keep
3236 * parsing the attribute(s).
3237 */
3238 if (retnotsup) {
3239 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3240 bitpos = NFSATTRBIT_MAX;
3241 } else {
3242 bitpos = 0;
3243 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ARCHIVE) ||
3244 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_HIDDEN) ||
3245 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SYSTEM))
3246 nvap->na_flags = 0;
3247 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
3248 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_POSIXDEFAULTACL) ||
3249 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_POSIXACCESSACL)))
3250 nd->nd_repstat = NFSERR_INVAL;
3251 }
3252 moderet = 0;
3253 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
3254 if (attrsum > attrsize) {
3255 error = NFSERR_BADXDR;
3256 goto nfsmout;
3257 }
3258 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
3259 switch (bitpos) {
3260 case NFSATTRBIT_SIZE:
3261 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3262 if (!nd->nd_repstat) {
3263 if (vp != NULL && vp->v_type != VREG)
3264 nd->nd_repstat = (vp->v_type == VDIR) ?
3265 NFSERR_ISDIR : NFSERR_INVAL;
3266 else
3267 nvap->na_size = fxdr_hyper(tl);
3268 }
3269 attrsum += NFSX_HYPER;
3270 break;
3271 case NFSATTRBIT_ACL:
3272 error = nfsrv_dissectacl(nd, aclp, true, false, &aceerr,
3273 &aclsize);
3274 if (error)
3275 goto nfsmout;
3276 if (aceerr && !nd->nd_repstat)
3277 nd->nd_repstat = aceerr;
3278 attrsum += aclsize;
3279 break;
3280 case NFSATTRBIT_ARCHIVE:
3281 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3282 if (nd->nd_repstat == 0) {
3283 if (*tl == newnfs_true)
3284 nvap->na_flags |= UF_ARCHIVE;
3285 }
3286 attrsum += NFSX_UNSIGNED;
3287 break;
3288 case NFSATTRBIT_HIDDEN:
3289 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3290 if (nd->nd_repstat == 0) {
3291 if (*tl == newnfs_true)
3292 nvap->na_flags |= UF_HIDDEN;
3293 }
3294 attrsum += NFSX_UNSIGNED;
3295 break;
3296 case NFSATTRBIT_MIMETYPE:
3297 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3298 i = fxdr_unsigned(int, *tl);
3299 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3300 if (error)
3301 goto nfsmout;
3302 if (!nd->nd_repstat)
3303 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3304 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3305 break;
3306 case NFSATTRBIT_MODE:
3307 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
3308 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3309 if (!nd->nd_repstat)
3310 nvap->na_mode = nfstov_mode(*tl);
3311 attrsum += NFSX_UNSIGNED;
3312 break;
3313 case NFSATTRBIT_OWNER:
3314 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3315 j = fxdr_unsigned(int, *tl);
3316 if (j < 0) {
3317 error = NFSERR_BADXDR;
3318 goto nfsmout;
3319 }
3320 if (j > NFSV4_SMALLSTR)
3321 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3322 else
3323 cp = namestr;
3324 error = nfsrv_mtostr(nd, cp, j);
3325 if (error) {
3326 if (j > NFSV4_SMALLSTR)
3327 free(cp, M_NFSSTRING);
3328 goto nfsmout;
3329 }
3330 if (!nd->nd_repstat) {
3331 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3332 &uid);
3333 if (!nd->nd_repstat)
3334 nvap->na_uid = uid;
3335 }
3336 if (j > NFSV4_SMALLSTR)
3337 free(cp, M_NFSSTRING);
3338 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3339 break;
3340 case NFSATTRBIT_OWNERGROUP:
3341 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3342 j = fxdr_unsigned(int, *tl);
3343 if (j < 0) {
3344 error = NFSERR_BADXDR;
3345 goto nfsmout;
3346 }
3347 if (j > NFSV4_SMALLSTR)
3348 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3349 else
3350 cp = namestr;
3351 error = nfsrv_mtostr(nd, cp, j);
3352 if (error) {
3353 if (j > NFSV4_SMALLSTR)
3354 free(cp, M_NFSSTRING);
3355 goto nfsmout;
3356 }
3357 if (!nd->nd_repstat) {
3358 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3359 &gid);
3360 if (!nd->nd_repstat)
3361 nvap->na_gid = gid;
3362 }
3363 if (j > NFSV4_SMALLSTR)
3364 free(cp, M_NFSSTRING);
3365 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3366 break;
3367 case NFSATTRBIT_SYSTEM:
3368 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3369 if (nd->nd_repstat == 0) {
3370 if (*tl == newnfs_true)
3371 nvap->na_flags |= UF_SYSTEM;
3372 }
3373 attrsum += NFSX_UNSIGNED;
3374 break;
3375 case NFSATTRBIT_TIMEACCESSSET:
3376 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3377 attrsum += NFSX_UNSIGNED;
3378 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3379 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3380 if (!nd->nd_repstat)
3381 fxdr_nfsv4time(tl, &nvap->na_atime);
3382 toclient = 1;
3383 attrsum += NFSX_V4TIME;
3384 } else if (!nd->nd_repstat) {
3385 vfs_timestamp(&nvap->na_atime);
3386 nvap->na_vaflags |= VA_UTIMES_NULL;
3387 }
3388 break;
3389 case NFSATTRBIT_TIMEBACKUP:
3390 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3391 if (!nd->nd_repstat)
3392 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3393 attrsum += NFSX_V4TIME;
3394 break;
3395 case NFSATTRBIT_TIMECREATE:
3396 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3397 if (!nd->nd_repstat)
3398 fxdr_nfsv4time(tl, &nvap->na_btime);
3399 attrsum += NFSX_V4TIME;
3400 break;
3401 case NFSATTRBIT_TIMEMODIFYSET:
3402 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3403 attrsum += NFSX_UNSIGNED;
3404 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3405 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3406 if (!nd->nd_repstat)
3407 fxdr_nfsv4time(tl, &nvap->na_mtime);
3408 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3409 attrsum += NFSX_V4TIME;
3410 } else if (!nd->nd_repstat) {
3411 vfs_timestamp(&nvap->na_mtime);
3412 if (!toclient)
3413 nvap->na_vaflags |= VA_UTIMES_NULL;
3414 }
3415 break;
3416 case NFSATTRBIT_MODESETMASKED:
3417 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3418 mode = fxdr_unsigned(u_short, *tl++);
3419 mask = fxdr_unsigned(u_short, *tl);
3420 /*
3421 * vp == NULL implies an Open/Create operation.
3422 * This attribute can only be used for Setattr and
3423 * only for NFSv4.1 or higher.
3424 * If moderet != 0, a mode attribute has also been
3425 * specified and this attribute cannot be done in the
3426 * same Setattr operation.
3427 */
3428 if (!nd->nd_repstat) {
3429 if ((nd->nd_flag & ND_NFSV41) == 0)
3430 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3431 else if ((mode & ~07777) != 0 ||
3432 (mask & ~07777) != 0 || vp == NULL)
3433 nd->nd_repstat = NFSERR_INVAL;
3434 else if (moderet == 0)
3435 moderet = VOP_GETATTR(vp, &va,
3436 nd->nd_cred);
3437 if (moderet == 0)
3438 nvap->na_mode = (mode & mask) |
3439 (va.va_mode & ~mask);
3440 else
3441 nd->nd_repstat = moderet;
3442 }
3443 attrsum += 2 * NFSX_UNSIGNED;
3444 break;
3445 case NFSATTRBIT_MODEUMASK:
3446 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3447 mode = fxdr_unsigned(u_short, *tl++);
3448 mask = fxdr_unsigned(u_short, *tl);
3449 /*
3450 * If moderet != 0, mode has already been done.
3451 * If vp != NULL, this is not a file object creation.
3452 */
3453 if (!nd->nd_repstat) {
3454 if ((nd->nd_flag & ND_NFSV42) == 0)
3455 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3456 else if ((mask & ~0777) != 0 || vp != NULL ||
3457 moderet != 0)
3458 nd->nd_repstat = NFSERR_INVAL;
3459 else
3460 nvap->na_mode = (mode & ~mask);
3461 }
3462 attrsum += 2 * NFSX_UNSIGNED;
3463 break;
3464 case NFSATTRBIT_POSIXACCESSACL:
3465 error = nfsrv_dissectacl(nd, aclp, true, true, &aceerr,
3466 &aclsize);
3467 if (error != 0)
3468 goto nfsmout;
3469 if (!nd->nd_repstat) {
3470 if ((nd->nd_flag & ND_NFSV42) == 0)
3471 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3472 else if (aclp != NULL && aclp->acl_cnt == 0)
3473 nd->nd_repstat = NFSERR_INVAL;
3474 else if (aceerr != 0)
3475 nd->nd_repstat = aceerr;
3476 }
3477 attrsum += aclsize;
3478 break;
3479 case NFSATTRBIT_POSIXDEFAULTACL:
3480 error = nfsrv_dissectacl(nd, daclp, true, true, &aceerr,
3481 &aclsize);
3482 if (error != 0)
3483 goto nfsmout;
3484 if (!nd->nd_repstat) {
3485 if ((nd->nd_flag & ND_NFSV42) == 0)
3486 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3487 else if (aclp != NULL && aclp->acl_cnt == 0)
3488 nd->nd_repstat = NFSERR_INVAL;
3489 else if (aceerr != 0)
3490 nd->nd_repstat = aceerr;
3491 else if (vp != NULL && vp->v_type != VDIR)
3492 nd->nd_repstat = NFSERR_INVAL;
3493 }
3494 attrsum += aclsize;
3495 break;
3496 default:
3497 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3498 /*
3499 * set bitpos so we drop out of the loop.
3500 */
3501 bitpos = NFSATTRBIT_MAX;
3502 break;
3503 }
3504 }
3505
3506 /*
3507 * some clients pad the attrlist, so we need to skip over the
3508 * padding. This also skips over unparsed non-supported attributes.
3509 */
3510 if (attrsum > attrsize) {
3511 error = NFSERR_BADXDR;
3512 } else {
3513 attrsize = NFSM_RNDUP(attrsize);
3514 if (attrsum < attrsize)
3515 error = nfsm_advance(nd, attrsize - attrsum, -1);
3516 }
3517 nfsmout:
3518 NFSEXITCODE2(error, nd);
3519 return (error);
3520 }
3521
3522 /*
3523 * Check/setup export credentials.
3524 */
3525 int
nfsd_excred(struct nfsrv_descript * nd,struct nfsexstuff * exp,struct ucred * credanon,bool testsec)3526 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3527 struct ucred *credanon, bool testsec)
3528 {
3529 int error;
3530
3531 /*
3532 * Check/setup credentials.
3533 */
3534 if (nd->nd_flag & ND_GSS)
3535 exp->nes_exflag &= ~MNT_EXPORTANON;
3536
3537 /*
3538 * Check to see if the operation is allowed for this security flavor.
3539 */
3540 error = 0;
3541 if (testsec) {
3542 error = nfsvno_testexp(nd, exp);
3543 if (error != 0)
3544 goto out;
3545 }
3546
3547 /*
3548 * Check to see if the file system is exported V4 only.
3549 */
3550 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3551 error = NFSERR_PROGNOTV4;
3552 goto out;
3553 }
3554
3555 /*
3556 * Now, map the user credentials.
3557 * (Note that ND_AUTHNONE will only be set for an NFSv3
3558 * Fsinfo RPC. If set for anything else, this code might need
3559 * to change.)
3560 */
3561 if (NFSVNO_EXPORTED(exp)) {
3562 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3563 NFSVNO_EXPORTANON(exp) ||
3564 (nd->nd_flag & ND_AUTHNONE) != 0) {
3565 nd->nd_cred->cr_uid = credanon->cr_uid;
3566 nd->nd_cred->cr_gid = credanon->cr_gid;
3567 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3568 credanon->cr_groups);
3569 } else if ((nd->nd_flag & ND_GSS) == 0) {
3570 /*
3571 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3572 * if there is a replacement credential with a group
3573 * list set up by "nfsuserd -manage-gids".
3574 * If there is no replacement, nfsrv_getgrpscred()
3575 * simply returns its argument.
3576 */
3577 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3578 }
3579 }
3580
3581 out:
3582 NFSEXITCODE2(error, nd);
3583 return (error);
3584 }
3585
3586 /*
3587 * Check exports.
3588 */
3589 int
nfsvno_checkexp(struct mount * mp,struct sockaddr * nam,struct nfsexstuff * exp,struct ucred ** credp)3590 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3591 struct ucred **credp)
3592 {
3593 int error;
3594
3595 error = 0;
3596 *credp = NULL;
3597 MNT_ILOCK(mp);
3598 if (mp->mnt_exjail == NULL ||
3599 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3600 error = EACCES;
3601 MNT_IUNLOCK(mp);
3602 if (error == 0)
3603 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3604 &exp->nes_numsecflavor, exp->nes_secflavors);
3605 if (error) {
3606 if (NFSD_VNET(nfs_rootfhset)) {
3607 exp->nes_exflag = 0;
3608 exp->nes_numsecflavor = 0;
3609 error = 0;
3610 }
3611 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3612 MAXSECFLAVORS) {
3613 printf("nfsvno_checkexp: numsecflavors out of range\n");
3614 exp->nes_numsecflavor = 0;
3615 error = EACCES;
3616 }
3617 NFSEXITCODE(error);
3618 return (error);
3619 }
3620
3621 /*
3622 * Get a vnode for a file handle and export stuff.
3623 */
3624 int
nfsvno_fhtovp(struct mount * mp,fhandle_t * fhp,struct sockaddr * nam,int lktype,struct vnode ** vpp,struct nfsexstuff * exp,struct ucred ** credp)3625 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3626 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3627 struct ucred **credp)
3628 {
3629 int error;
3630
3631 *credp = NULL;
3632 exp->nes_numsecflavor = 0;
3633 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3634 if (error != 0)
3635 /* Make sure the server replies ESTALE to the client. */
3636 error = ESTALE;
3637 if (nam && !error) {
3638 MNT_ILOCK(mp);
3639 if (mp->mnt_exjail == NULL ||
3640 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3641 error = EACCES;
3642 MNT_IUNLOCK(mp);
3643 if (error == 0)
3644 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3645 &exp->nes_numsecflavor, exp->nes_secflavors);
3646 if (error) {
3647 if (NFSD_VNET(nfs_rootfhset)) {
3648 exp->nes_exflag = 0;
3649 exp->nes_numsecflavor = 0;
3650 error = 0;
3651 } else {
3652 vput(*vpp);
3653 }
3654 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3655 MAXSECFLAVORS) {
3656 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3657 exp->nes_numsecflavor = 0;
3658 error = EACCES;
3659 vput(*vpp);
3660 }
3661 }
3662 NFSEXITCODE(error);
3663 return (error);
3664 }
3665
3666 /*
3667 * nfsd_fhtovp() - convert a fh to a vnode ptr
3668 * - look up fsid in mount list (if not found ret error)
3669 * - get vp and export rights by calling nfsvno_fhtovp()
3670 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3671 * for AUTH_SYS
3672 * - if mpp != NULL, return the mount point so that it can
3673 * be used for vn_finished_write() by the caller
3674 */
3675 void
nfsd_fhtovp(struct nfsrv_descript * nd,struct nfsrvfh * nfp,int lktype,struct vnode ** vpp,struct nfsexstuff * exp,struct mount ** mpp,int startwrite,int nextop)3676 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3677 struct vnode **vpp, struct nfsexstuff *exp,
3678 struct mount **mpp, int startwrite, int nextop)
3679 {
3680 struct mount *mp, *mpw;
3681 struct ucred *credanon;
3682 fhandle_t *fhp;
3683 int error;
3684
3685 if (mpp != NULL)
3686 *mpp = NULL;
3687 *vpp = NULL;
3688 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3689 mp = vfs_busyfs(&fhp->fh_fsid);
3690 if (mp == NULL) {
3691 nd->nd_repstat = ESTALE;
3692 goto out;
3693 }
3694
3695 if (startwrite) {
3696 mpw = mp;
3697 error = vn_start_write(NULL, &mpw, V_WAIT);
3698 if (error != 0) {
3699 mpw = NULL;
3700 vfs_unbusy(mp);
3701 nd->nd_repstat = ESTALE;
3702 goto out;
3703 }
3704 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3705 lktype = LK_EXCLUSIVE;
3706 } else
3707 mpw = NULL;
3708
3709 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3710 &credanon);
3711 vfs_unbusy(mp);
3712
3713 if (nd->nd_repstat == 0 &&
3714 nfp->nfsrvfh_len >= NFSX_MYFH + NFSX_V4NAMEDDIRFH &&
3715 nfp->nfsrvfh_len <= NFSX_MYFH + NFSX_V4NAMEDATTRFH) {
3716 if (nfp->nfsrvfh_len == NFSX_MYFH + NFSX_V4NAMEDDIRFH)
3717 vn_irflag_set_cond(*vpp, VIRF_NAMEDDIR);
3718 else
3719 vn_irflag_set_cond(*vpp, VIRF_NAMEDATTR);
3720 }
3721
3722 /*
3723 * For NFSv4 without a pseudo root fs, unexported file handles
3724 * can be returned, so that Lookup works everywhere.
3725 */
3726 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3727 !(nd->nd_flag & ND_NFSV4)) {
3728 vput(*vpp);
3729 *vpp = NULL;
3730 nd->nd_repstat = EACCES;
3731 }
3732
3733 /*
3734 * Personally, I've never seen any point in requiring a
3735 * reserved port#, since only in the rare case where the
3736 * clients are all boxes with secure system privileges,
3737 * does it provide any enhanced security, but... some people
3738 * believe it to be useful and keep putting this code back in.
3739 * (There is also some "security checker" out there that
3740 * complains if the nfs server doesn't enforce this.)
3741 * However, note the following:
3742 * RFC3530 (NFSv4) specifies that a reserved port# not be
3743 * required.
3744 * RFC2623 recommends that, if a reserved port# is checked for,
3745 * that there be a way to turn that off--> ifdef'd.
3746 */
3747 #ifdef NFS_REQRSVPORT
3748 if (!nd->nd_repstat) {
3749 struct sockaddr_in *saddr;
3750 struct sockaddr_in6 *saddr6;
3751
3752 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3753 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3754 if (!(nd->nd_flag & ND_NFSV4) &&
3755 ((saddr->sin_family == AF_INET &&
3756 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3757 (saddr6->sin6_family == AF_INET6 &&
3758 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3759 vput(*vpp);
3760 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3761 }
3762 }
3763 #endif /* NFS_REQRSVPORT */
3764
3765 /*
3766 * Check/setup credentials.
3767 */
3768 if (!nd->nd_repstat) {
3769 nd->nd_saveduid = nd->nd_cred->cr_uid;
3770 nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3771 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3772 if (nd->nd_repstat)
3773 vput(*vpp);
3774 }
3775 if (credanon != NULL)
3776 crfree(credanon);
3777 if (nd->nd_repstat) {
3778 vn_finished_write(mpw);
3779 *vpp = NULL;
3780 } else if (mpp != NULL) {
3781 *mpp = mpw;
3782 }
3783
3784 out:
3785 NFSEXITCODE2(0, nd);
3786 }
3787
3788 /*
3789 * glue for fp.
3790 */
3791 static int
fp_getfvp(struct thread * p,int fd,struct file ** fpp,struct vnode ** vpp)3792 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3793 {
3794 struct filedesc *fdp;
3795 struct file *fp;
3796 int error = 0;
3797
3798 fdp = p->td_proc->p_fd;
3799 if (fd < 0 || fd >= fdp->fd_nfiles ||
3800 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3801 error = EBADF;
3802 goto out;
3803 }
3804 *fpp = fp;
3805
3806 out:
3807 NFSEXITCODE(error);
3808 return (error);
3809 }
3810
3811 /*
3812 * Called from nfssvc() to update the exports list. Just call
3813 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3814 * in the mount list.
3815 */
3816 int
nfsrv_v4rootexport(void * argp,struct ucred * cred,struct thread * p)3817 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3818 {
3819 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3820 int error = 0;
3821 struct nameidata nd;
3822 fhandle_t fh;
3823
3824 error = vfs_export(NFSD_VNET(nfsv4root_mnt), &nfsexargp->export, false);
3825 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3826 NFSD_VNET(nfs_rootfhset) = 0;
3827 else if (error == 0) {
3828 if (nfsexargp->fspec == NULL) {
3829 error = EPERM;
3830 goto out;
3831 }
3832 /*
3833 * If fspec != NULL, this is the v4root path.
3834 */
3835 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec);
3836 if ((error = namei(&nd)) != 0)
3837 goto out;
3838 NDFREE_PNBUF(&nd);
3839 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3840 vrele(nd.ni_vp);
3841 if (!error) {
3842 NFSD_VNET(nfs_rootfh).nfsrvfh_len = NFSX_MYFH;
3843 NFSBCOPY((caddr_t)&fh,
3844 NFSD_VNET(nfs_rootfh).nfsrvfh_data,
3845 sizeof (fhandle_t));
3846 NFSD_VNET(nfs_rootfhset) = 1;
3847 }
3848 }
3849
3850 out:
3851 NFSEXITCODE(error);
3852 return (error);
3853 }
3854
3855 /*
3856 * This function needs to test to see if the system is near its limit
3857 * for memory allocation via malloc() or mget() and return True iff
3858 * either of these resources are near their limit.
3859 * XXX (For now, this is just a stub.)
3860 */
3861 int nfsrv_testmalloclimit = 0;
3862 int
nfsrv_mallocmget_limit(void)3863 nfsrv_mallocmget_limit(void)
3864 {
3865 static int printmesg = 0;
3866 static int testval = 1;
3867
3868 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3869 if ((printmesg++ % 100) == 0)
3870 printf("nfsd: malloc/mget near limit\n");
3871 return (1);
3872 }
3873 return (0);
3874 }
3875
3876 /*
3877 * BSD specific initialization of a mount point.
3878 */
3879 void
nfsd_mntinit(void)3880 nfsd_mntinit(void)
3881 {
3882
3883 NFSD_LOCK();
3884 if (NFSD_VNET(nfsrv_mntinited)) {
3885 NFSD_UNLOCK();
3886 return;
3887 }
3888 NFSD_VNET(nfsrv_mntinited) = true;
3889 nfsrvd_init(0);
3890 NFSD_UNLOCK();
3891
3892 NFSD_VNET(nfsv4root_mnt) = malloc(sizeof(struct mount), M_TEMP,
3893 M_WAITOK | M_ZERO);
3894 NFSD_VNET(nfsv4root_mnt)->mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3895 mtx_init(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3896 lockinit(&NFSD_VNET(nfsv4root_mnt)->mnt_explock, PVFS, "explock", 0, 0);
3897 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelist);
3898 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelist);
3899 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
3900 TAILQ_INIT(&NFSD_VNET(nfsv4root_opt));
3901 TAILQ_INIT(&NFSD_VNET(nfsv4root_newopt));
3902 NFSD_VNET(nfsv4root_mnt)->mnt_opt = &NFSD_VNET(nfsv4root_opt);
3903 NFSD_VNET(nfsv4root_mnt)->mnt_optnew = &NFSD_VNET(nfsv4root_newopt);
3904 NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelistsize = 0;
3905 NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelistsize = 0;
3906 callout_init(&NFSD_VNET(nfsd_callout), 1);
3907
3908 nfsrvd_initcache();
3909 nfsd_init();
3910 }
3911
3912 static void
nfsd_timer(void * arg)3913 nfsd_timer(void *arg)
3914 {
3915 struct vnet *vnetp;
3916
3917 vnetp = (struct vnet *)arg;
3918 NFSD_CURVNET_SET_QUIET(vnetp);
3919 nfsrv_servertimer(vnetp);
3920 callout_reset_sbt(&NFSD_VNET(nfsd_callout), SBT_1S, SBT_1S, nfsd_timer,
3921 arg, 0);
3922 NFSD_CURVNET_RESTORE();
3923 }
3924
3925 /*
3926 * Get a vnode for a file handle, without checking exports, etc.
3927 */
3928 struct vnode *
nfsvno_getvp(fhandle_t * fhp)3929 nfsvno_getvp(fhandle_t *fhp)
3930 {
3931 struct mount *mp;
3932 struct vnode *vp;
3933 int error;
3934
3935 mp = vfs_busyfs(&fhp->fh_fsid);
3936 if (mp == NULL)
3937 return (NULL);
3938 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3939 vfs_unbusy(mp);
3940 if (error)
3941 return (NULL);
3942 return (vp);
3943 }
3944
3945 /*
3946 * Do a local VOP_ADVLOCK().
3947 */
3948 int
nfsvno_advlock(struct vnode * vp,int ftype,u_int64_t first,u_int64_t end,struct thread * td)3949 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3950 u_int64_t end, struct thread *td)
3951 {
3952 int error = 0;
3953 struct flock fl;
3954 u_int64_t tlen;
3955
3956 if (nfsrv_dolocallocks == 0)
3957 goto out;
3958 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3959
3960 fl.l_whence = SEEK_SET;
3961 fl.l_type = ftype;
3962 fl.l_start = (off_t)first;
3963 if (end == NFS64BITSSET) {
3964 fl.l_len = 0;
3965 } else {
3966 tlen = end - first;
3967 fl.l_len = (off_t)tlen;
3968 }
3969 /*
3970 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3971 * values for all calls, so that all locks will be held by the
3972 * nfsd server. (The nfsd server handles conflicts between the
3973 * various clients.)
3974 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3975 * bytes, so it can't be put in l_sysid.
3976 */
3977 if (nfsv4_sysid == 0)
3978 nfsv4_sysid = nlm_acquire_next_sysid();
3979 fl.l_pid = (pid_t)0;
3980 fl.l_sysid = (int)nfsv4_sysid;
3981
3982 if (ftype == F_UNLCK)
3983 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3984 (F_POSIX | F_REMOTE));
3985 else
3986 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3987 (F_POSIX | F_REMOTE));
3988
3989 out:
3990 NFSEXITCODE(error);
3991 return (error);
3992 }
3993
3994 /*
3995 * Check the nfsv4 root exports.
3996 */
3997 int
nfsvno_v4rootexport(struct nfsrv_descript * nd)3998 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3999 {
4000 struct ucred *credanon;
4001 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
4002 uint64_t exflags;
4003
4004 error = vfs_stdcheckexp(NFSD_VNET(nfsv4root_mnt), nd->nd_nam, &exflags,
4005 &credanon, &numsecflavor, secflavors);
4006 if (error) {
4007 error = NFSERR_PROGUNAVAIL;
4008 goto out;
4009 }
4010 if (credanon != NULL)
4011 crfree(credanon);
4012 for (i = 0; i < numsecflavor; i++) {
4013 if (secflavors[i] == AUTH_SYS)
4014 nd->nd_flag |= ND_EXAUTHSYS;
4015 else if (secflavors[i] == RPCSEC_GSS_KRB5)
4016 nd->nd_flag |= ND_EXGSS;
4017 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
4018 nd->nd_flag |= ND_EXGSSINTEGRITY;
4019 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
4020 nd->nd_flag |= ND_EXGSSPRIVACY;
4021 }
4022
4023 /* And set ND_EXxx flags for TLS. */
4024 if ((exflags & MNT_EXTLS) != 0) {
4025 nd->nd_flag |= ND_EXTLS;
4026 if ((exflags & MNT_EXTLSCERT) != 0)
4027 nd->nd_flag |= ND_EXTLSCERT;
4028 if ((exflags & MNT_EXTLSCERTUSER) != 0)
4029 nd->nd_flag |= ND_EXTLSCERTUSER;
4030 }
4031
4032 out:
4033 NFSEXITCODE(error);
4034 return (error);
4035 }
4036
4037 /*
4038 * Nfs server pseudo system call for the nfsd's
4039 */
4040 /*
4041 * MPSAFE
4042 */
4043 static int
nfssvc_nfsd(struct thread * td,struct nfssvc_args * uap)4044 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
4045 {
4046 struct file *fp;
4047 struct nfsd_addsock_args sockarg;
4048 struct nfsd_nfsd_args nfsdarg;
4049 struct nfsd_nfsd_oargs onfsdarg;
4050 struct nfsd_pnfsd_args pnfsdarg;
4051 struct vnode *vp, *nvp, *curdvp;
4052 struct pnfsdsfile *pf;
4053 struct nfsdevice *ds, *fds;
4054 cap_rights_t rights;
4055 int buflen, error, ret;
4056 char *buf, *cp, *cp2, *cp3;
4057 char fname[PNFS_FILENAME_LEN + 1];
4058
4059 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
4060 if (uap->flag & NFSSVC_NFSDADDSOCK) {
4061 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
4062 if (error)
4063 goto out;
4064 /*
4065 * Since we don't know what rights might be required,
4066 * pretend that we need them all. It is better to be too
4067 * careful than too reckless.
4068 */
4069 error = fget(td, sockarg.sock,
4070 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
4071 if (error != 0)
4072 goto out;
4073 if (fp->f_type != DTYPE_SOCKET) {
4074 fdrop(fp, td);
4075 error = EPERM;
4076 goto out;
4077 }
4078 error = nfsrvd_addsock(fp);
4079 fdrop(fp, td);
4080 } else if (uap->flag & NFSSVC_NFSDNFSD) {
4081 if (uap->argp == NULL) {
4082 error = EINVAL;
4083 goto out;
4084 }
4085 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
4086 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
4087 if (error == 0) {
4088 nfsdarg.principal = onfsdarg.principal;
4089 nfsdarg.minthreads = onfsdarg.minthreads;
4090 nfsdarg.maxthreads = onfsdarg.maxthreads;
4091 nfsdarg.version = 1;
4092 nfsdarg.addr = NULL;
4093 nfsdarg.addrlen = 0;
4094 nfsdarg.dnshost = NULL;
4095 nfsdarg.dnshostlen = 0;
4096 nfsdarg.dspath = NULL;
4097 nfsdarg.dspathlen = 0;
4098 nfsdarg.mdspath = NULL;
4099 nfsdarg.mdspathlen = 0;
4100 nfsdarg.mirrorcnt = 1;
4101 }
4102 } else
4103 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
4104 if (error)
4105 goto out;
4106 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
4107 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
4108 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
4109 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
4110 nfsdarg.mirrorcnt >= 1 &&
4111 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
4112 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
4113 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
4114 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
4115 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
4116 nfsdarg.dspathlen, nfsdarg.dnshostlen,
4117 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
4118 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
4119 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
4120 if (error != 0) {
4121 free(cp, M_TEMP);
4122 goto out;
4123 }
4124 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
4125 nfsdarg.addr = cp;
4126 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
4127 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
4128 if (error != 0) {
4129 free(nfsdarg.addr, M_TEMP);
4130 free(cp, M_TEMP);
4131 goto out;
4132 }
4133 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
4134 nfsdarg.dnshost = cp;
4135 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
4136 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
4137 if (error != 0) {
4138 free(nfsdarg.addr, M_TEMP);
4139 free(nfsdarg.dnshost, M_TEMP);
4140 free(cp, M_TEMP);
4141 goto out;
4142 }
4143 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
4144 nfsdarg.dspath = cp;
4145 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
4146 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
4147 if (error != 0) {
4148 free(nfsdarg.addr, M_TEMP);
4149 free(nfsdarg.dnshost, M_TEMP);
4150 free(nfsdarg.dspath, M_TEMP);
4151 free(cp, M_TEMP);
4152 goto out;
4153 }
4154 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
4155 nfsdarg.mdspath = cp;
4156 } else {
4157 nfsdarg.addr = NULL;
4158 nfsdarg.addrlen = 0;
4159 nfsdarg.dnshost = NULL;
4160 nfsdarg.dnshostlen = 0;
4161 nfsdarg.dspath = NULL;
4162 nfsdarg.dspathlen = 0;
4163 nfsdarg.mdspath = NULL;
4164 nfsdarg.mdspathlen = 0;
4165 nfsdarg.mirrorcnt = 1;
4166 }
4167 nfsd_timer(NFSD_TD_TO_VNET(td));
4168 error = nfsrvd_nfsd(td, &nfsdarg);
4169 callout_drain(&NFSD_VNET(nfsd_callout));
4170 free(nfsdarg.addr, M_TEMP);
4171 free(nfsdarg.dnshost, M_TEMP);
4172 free(nfsdarg.dspath, M_TEMP);
4173 free(nfsdarg.mdspath, M_TEMP);
4174 } else if (uap->flag & NFSSVC_PNFSDS) {
4175 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
4176 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
4177 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
4178 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4179 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
4180 NULL);
4181 if (error == 0)
4182 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
4183 free(cp, M_TEMP);
4184 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
4185 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4186 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
4187 buf = malloc(buflen, M_TEMP, M_WAITOK);
4188 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
4189 NULL);
4190 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
4191 if (error == 0 && pnfsdarg.dspath != NULL) {
4192 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4193 error = copyinstr(pnfsdarg.dspath, cp2,
4194 PATH_MAX + 1, NULL);
4195 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
4196 error);
4197 } else
4198 cp2 = NULL;
4199 if (error == 0 && pnfsdarg.curdspath != NULL) {
4200 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4201 error = copyinstr(pnfsdarg.curdspath, cp3,
4202 PATH_MAX + 1, NULL);
4203 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
4204 error);
4205 } else
4206 cp3 = NULL;
4207 curdvp = NULL;
4208 fds = NULL;
4209 if (error == 0)
4210 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
4211 &buflen, fname, td, &vp, &nvp, &pf, &ds,
4212 &fds);
4213 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
4214 if (error == 0) {
4215 if (pf->dsf_dir >= nfsrv_dsdirsize) {
4216 printf("copymr: dsdir out of range\n");
4217 pf->dsf_dir = 0;
4218 }
4219 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
4220 error = nfsrv_copymr(vp, nvp,
4221 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
4222 (struct pnfsdsfile *)buf,
4223 buflen / sizeof(*pf), td->td_ucred, td);
4224 vput(vp);
4225 vput(nvp);
4226 if (fds != NULL && error == 0) {
4227 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
4228 ret = vn_lock(curdvp, LK_EXCLUSIVE);
4229 if (ret == 0) {
4230 nfsrv_dsremove(curdvp, fname,
4231 td->td_ucred, td);
4232 NFSVOPUNLOCK(curdvp);
4233 }
4234 }
4235 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
4236 }
4237 free(cp, M_TEMP);
4238 free(cp2, M_TEMP);
4239 free(cp3, M_TEMP);
4240 free(buf, M_TEMP);
4241 }
4242 } else {
4243 error = nfssvc_srvcall(td, uap, td->td_ucred);
4244 }
4245
4246 out:
4247 NFSD_CURVNET_RESTORE();
4248 NFSEXITCODE(error);
4249 return (error);
4250 }
4251
4252 static int
nfssvc_srvcall(struct thread * p,struct nfssvc_args * uap,struct ucred * cred)4253 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
4254 {
4255 struct nfsex_args export;
4256 struct nfsex_oldargs oexp;
4257 struct file *fp = NULL;
4258 int stablefd, i, len;
4259 struct nfsd_clid adminrevoke;
4260 struct nfsd_dumplist dumplist;
4261 struct nfsd_dumpclients *dumpclients;
4262 struct nfsd_dumplocklist dumplocklist;
4263 struct nfsd_dumplocks *dumplocks;
4264 struct nameidata nd;
4265 vnode_t vp;
4266 int error = EINVAL, igotlock;
4267 struct proc *procp;
4268 gid_t *grps;
4269
4270 if (uap->flag & NFSSVC_PUBLICFH) {
4271 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
4272 sizeof (fhandle_t));
4273 error = copyin(uap->argp,
4274 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
4275 if (!error)
4276 nfs_pubfhset = 1;
4277 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
4278 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
4279 error = copyin(uap->argp,(caddr_t)&export,
4280 sizeof (struct nfsex_args));
4281 if (!error) {
4282 grps = NULL;
4283 if (export.export.ex_ngroups > NGROUPS_MAX ||
4284 export.export.ex_ngroups < 0)
4285 error = EINVAL;
4286 else if (export.export.ex_ngroups > 0) {
4287 grps = malloc(export.export.ex_ngroups *
4288 sizeof(gid_t), M_TEMP, M_WAITOK);
4289 error = copyin(export.export.ex_groups, grps,
4290 export.export.ex_ngroups * sizeof(gid_t));
4291 export.export.ex_groups = grps;
4292 } else
4293 export.export.ex_groups = NULL;
4294 if (!error)
4295 error = nfsrv_v4rootexport(&export, cred, p);
4296 free(grps, M_TEMP);
4297 }
4298 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
4299 NFSSVC_V4ROOTEXPORT) {
4300 error = copyin(uap->argp,(caddr_t)&oexp,
4301 sizeof (struct nfsex_oldargs));
4302 if (!error) {
4303 memset(&export.export, 0, sizeof(export.export));
4304 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
4305 export.export.ex_root = oexp.export.ex_root;
4306 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
4307 export.export.ex_ngroups =
4308 oexp.export.ex_anon.cr_ngroups;
4309 export.export.ex_groups = NULL;
4310 if (export.export.ex_ngroups > XU_NGROUPS ||
4311 export.export.ex_ngroups < 0)
4312 error = EINVAL;
4313 else if (export.export.ex_ngroups > 0) {
4314 export.export.ex_groups = malloc(
4315 export.export.ex_ngroups * sizeof(gid_t),
4316 M_TEMP, M_WAITOK);
4317 for (i = 0; i < export.export.ex_ngroups; i++)
4318 export.export.ex_groups[i] =
4319 oexp.export.ex_anon.cr_groups[i];
4320 }
4321 export.export.ex_addr = oexp.export.ex_addr;
4322 export.export.ex_addrlen = oexp.export.ex_addrlen;
4323 export.export.ex_mask = oexp.export.ex_mask;
4324 export.export.ex_masklen = oexp.export.ex_masklen;
4325 export.export.ex_indexfile = oexp.export.ex_indexfile;
4326 export.export.ex_numsecflavors =
4327 oexp.export.ex_numsecflavors;
4328 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
4329 export.export.ex_numsecflavors < 0)
4330 error = EINVAL;
4331 else {
4332 for (i = 0; i < export.export.ex_numsecflavors;
4333 i++)
4334 export.export.ex_secflavors[i] =
4335 oexp.export.ex_secflavors[i];
4336 }
4337 export.fspec = oexp.fspec;
4338 if (error == 0)
4339 error = nfsrv_v4rootexport(&export, cred, p);
4340 free(export.export.ex_groups, M_TEMP);
4341 }
4342 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
4343 nfs_pubfhset = 0;
4344 error = 0;
4345 } else if (uap->flag & NFSSVC_STABLERESTART) {
4346 error = copyin(uap->argp, (caddr_t)&stablefd,
4347 sizeof (int));
4348 if (!error)
4349 error = fp_getfvp(p, stablefd, &fp, &vp);
4350 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
4351 error = EBADF;
4352 if (!error && NFSD_VNET(nfsrv_numnfsd) != 0)
4353 error = ENXIO;
4354 if (!error) {
4355 NFSD_VNET(nfsrv_stablefirst).nsf_fp = fp;
4356 nfsrv_setupstable(p);
4357 }
4358 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
4359 error = copyin(uap->argp, (caddr_t)&adminrevoke,
4360 sizeof (struct nfsd_clid));
4361 if (!error)
4362 error = nfsrv_adminrevoke(&adminrevoke, p);
4363 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
4364 error = copyin(uap->argp, (caddr_t)&dumplist,
4365 sizeof (struct nfsd_dumplist));
4366 if (!error && (dumplist.ndl_size < 1 ||
4367 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
4368 error = EPERM;
4369 if (!error) {
4370 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
4371 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4372 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
4373 error = copyout(dumpclients, dumplist.ndl_list, len);
4374 free(dumpclients, M_TEMP);
4375 }
4376 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
4377 error = copyin(uap->argp, (caddr_t)&dumplocklist,
4378 sizeof (struct nfsd_dumplocklist));
4379 if (!error && (dumplocklist.ndllck_size < 1 ||
4380 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
4381 error = EPERM;
4382 if (!error)
4383 error = nfsrv_lookupfilename(&nd,
4384 dumplocklist.ndllck_fname, p);
4385 if (!error) {
4386 len = sizeof (struct nfsd_dumplocks) *
4387 dumplocklist.ndllck_size;
4388 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4389 nfsrv_dumplocks(nd.ni_vp, dumplocks,
4390 dumplocklist.ndllck_size, p);
4391 vput(nd.ni_vp);
4392 error = copyout(dumplocks, dumplocklist.ndllck_list,
4393 len);
4394 free(dumplocks, M_TEMP);
4395 }
4396 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
4397 procp = p->td_proc;
4398 PROC_LOCK(procp);
4399 nfsd_master_pid = procp->p_pid;
4400 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
4401 nfsd_master_start = procp->p_stats->p_start;
4402 NFSD_VNET(nfsd_master_proc) = procp;
4403 PROC_UNLOCK(procp);
4404 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4405 NFSLOCKV4ROOTMUTEX();
4406 if (!NFSD_VNET(nfsrv_suspend_nfsd)) {
4407 /* Lock out all nfsd threads */
4408 do {
4409 igotlock = nfsv4_lock(
4410 &NFSD_VNET(nfsd_suspend_lock), 1, NULL,
4411 NFSV4ROOTLOCKMUTEXPTR, NULL);
4412 } while (igotlock == 0 &&
4413 !NFSD_VNET(nfsrv_suspend_nfsd));
4414 NFSD_VNET(nfsrv_suspend_nfsd) = true;
4415 }
4416 NFSUNLOCKV4ROOTMUTEX();
4417 error = 0;
4418 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4419 NFSLOCKV4ROOTMUTEX();
4420 if (NFSD_VNET(nfsrv_suspend_nfsd)) {
4421 nfsv4_unlock(&NFSD_VNET(nfsd_suspend_lock), 0);
4422 NFSD_VNET(nfsrv_suspend_nfsd) = false;
4423 }
4424 NFSUNLOCKV4ROOTMUTEX();
4425 error = 0;
4426 }
4427
4428 NFSEXITCODE(error);
4429 return (error);
4430 }
4431
4432 /*
4433 * Check exports.
4434 * Returns 0 if ok, 1 otherwise.
4435 */
4436 int
nfsvno_testexp(struct nfsrv_descript * nd,struct nfsexstuff * exp)4437 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4438 {
4439 int i;
4440
4441 if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4442 (NFSVNO_EXTLSCERT(exp) &&
4443 (nd->nd_flag & ND_TLSCERT) == 0) ||
4444 (NFSVNO_EXTLSCERTUSER(exp) &&
4445 (nd->nd_flag & ND_TLSCERTUSER) == 0)) {
4446 if ((nd->nd_flag & ND_NFSV4) != 0)
4447 return (NFSERR_WRONGSEC);
4448 #ifdef notnow
4449 /* There is currently no auth_stat for this. */
4450 else if ((nd->nd_flag & ND_TLS) == 0)
4451 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4452 else
4453 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4454 #endif
4455 else
4456 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4457 }
4458
4459 /*
4460 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use
4461 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
4462 */
4463 if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO)
4464 return (0);
4465
4466 /*
4467 * This seems odd, but allow the case where the security flavor
4468 * list is empty. This happens when NFSv4 is traversing non-exported
4469 * file systems. Exported file systems should always have a non-empty
4470 * security flavor list.
4471 */
4472 if (exp->nes_numsecflavor == 0)
4473 return (0);
4474
4475 for (i = 0; i < exp->nes_numsecflavor; i++) {
4476 /*
4477 * The tests for privacy and integrity must be first,
4478 * since ND_GSS is set for everything but AUTH_SYS.
4479 */
4480 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4481 (nd->nd_flag & ND_GSSPRIVACY))
4482 return (0);
4483 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4484 (nd->nd_flag & ND_GSSINTEGRITY))
4485 return (0);
4486 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4487 (nd->nd_flag & ND_GSS))
4488 return (0);
4489 if (exp->nes_secflavors[i] == AUTH_SYS &&
4490 (nd->nd_flag & ND_GSS) == 0)
4491 return (0);
4492 }
4493 if ((nd->nd_flag & ND_NFSV4) != 0)
4494 return (NFSERR_WRONGSEC);
4495 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4496 }
4497
4498 /*
4499 * Calculate a hash value for the fid in a file handle.
4500 */
4501 uint32_t
nfsrv_hashfh(fhandle_t * fhp)4502 nfsrv_hashfh(fhandle_t *fhp)
4503 {
4504 uint32_t hashval;
4505
4506 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4507 return (hashval);
4508 }
4509
4510 /*
4511 * Calculate a hash value for the sessionid.
4512 */
4513 uint32_t
nfsrv_hashsessionid(uint8_t * sessionid)4514 nfsrv_hashsessionid(uint8_t *sessionid)
4515 {
4516 uint32_t hashval;
4517
4518 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4519 return (hashval);
4520 }
4521
4522 /*
4523 * Signal the userland master nfsd to backup the stable restart file.
4524 */
4525 void
nfsrv_backupstable(void)4526 nfsrv_backupstable(void)
4527 {
4528 struct proc *procp;
4529
4530 if (NFSD_VNET(nfsd_master_proc) != NULL) {
4531 procp = pfind(nfsd_master_pid);
4532 /* Try to make sure it is the correct process. */
4533 if (procp == NFSD_VNET(nfsd_master_proc) &&
4534 procp->p_stats->p_start.tv_sec ==
4535 nfsd_master_start.tv_sec &&
4536 procp->p_stats->p_start.tv_usec ==
4537 nfsd_master_start.tv_usec &&
4538 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4539 kern_psignal(procp, SIGUSR2);
4540 else
4541 NFSD_VNET(nfsd_master_proc) = NULL;
4542
4543 if (procp != NULL)
4544 PROC_UNLOCK(procp);
4545 }
4546 }
4547
4548 /*
4549 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4550 * The arguments are in a structure, so that they can be passed through
4551 * taskqueue for a kernel process to execute this function.
4552 */
4553 struct nfsrvdscreate {
4554 int done;
4555 int inprog;
4556 struct task tsk;
4557 struct ucred *tcred;
4558 struct vnode *dvp;
4559 NFSPROC_T *p;
4560 struct pnfsdsfile *pf;
4561 int err;
4562 fhandle_t fh;
4563 struct vattr va;
4564 struct vattr createva;
4565 };
4566
4567 int
nfsrv_dscreate(struct vnode * dvp,struct vattr * vap,struct vattr * nvap,fhandle_t * fhp,struct pnfsdsfile * pf,struct pnfsdsattr * dsa,char * fnamep,struct ucred * tcred,NFSPROC_T * p,struct vnode ** nvpp)4568 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4569 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4570 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4571 {
4572 struct vnode *nvp;
4573 struct nameidata named;
4574 struct vattr va;
4575 char *bufp;
4576 u_long *hashp;
4577 struct nfsnode *np;
4578 struct nfsmount *nmp;
4579 int error;
4580
4581 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4582 LOCKPARENT | LOCKLEAF | NOCACHE);
4583 nfsvno_setpathbuf(&named, &bufp, &hashp);
4584 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4585 named.ni_cnd.cn_nameptr = bufp;
4586 if (fnamep != NULL) {
4587 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4588 named.ni_cnd.cn_namelen = strlen(bufp);
4589 } else
4590 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4591 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4592
4593 /* Create the date file in the DS mount. */
4594 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4595 if (error == 0) {
4596 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4597 vref(dvp);
4598 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4599 if (error == 0) {
4600 /* Set the ownership of the file. */
4601 error = VOP_SETATTR(nvp, nvap, tcred);
4602 NFSD_DEBUG(4, "nfsrv_dscreate:"
4603 " setattr-uid=%d\n", error);
4604 if (error != 0)
4605 vput(nvp);
4606 }
4607 if (error != 0)
4608 printf("pNFS: pnfscreate failed=%d\n", error);
4609 } else
4610 printf("pNFS: pnfscreate vnlock=%d\n", error);
4611 if (error == 0) {
4612 np = VTONFS(nvp);
4613 nmp = VFSTONFS(nvp->v_mount);
4614 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4615 != 0 || nmp->nm_nam->sa_len > sizeof(
4616 struct sockaddr_in6) ||
4617 np->n_fhp->nfh_len != NFSX_MYFH) {
4618 printf("Bad DS file: fstype=%s salen=%d"
4619 " fhlen=%d\n",
4620 nvp->v_mount->mnt_vfc->vfc_name,
4621 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4622 error = ENOENT;
4623 }
4624
4625 /* Set extattrs for the DS on the MDS file. */
4626 if (error == 0) {
4627 if (dsa != NULL) {
4628 error = VOP_GETATTR(nvp, &va, tcred);
4629 if (error == 0) {
4630 dsa->dsa_filerev = va.va_filerev;
4631 dsa->dsa_size = va.va_size;
4632 dsa->dsa_atime = va.va_atime;
4633 dsa->dsa_mtime = va.va_mtime;
4634 dsa->dsa_bytes = va.va_bytes;
4635 }
4636 }
4637 if (error == 0) {
4638 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4639 NFSX_MYFH);
4640 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4641 nmp->nm_nam->sa_len);
4642 NFSBCOPY(named.ni_cnd.cn_nameptr,
4643 pf->dsf_filename,
4644 sizeof(pf->dsf_filename));
4645 }
4646 } else
4647 printf("pNFS: pnfscreate can't get DS"
4648 " attr=%d\n", error);
4649 if (nvpp != NULL && error == 0)
4650 *nvpp = nvp;
4651 else
4652 vput(nvp);
4653 }
4654 nfsvno_relpathbuf(&named);
4655 return (error);
4656 }
4657
4658 /*
4659 * Start up the thread that will execute nfsrv_dscreate().
4660 */
4661 static void
start_dscreate(void * arg,int pending)4662 start_dscreate(void *arg, int pending)
4663 {
4664 struct nfsrvdscreate *dsc;
4665
4666 dsc = (struct nfsrvdscreate *)arg;
4667 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4668 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4669 dsc->done = 1;
4670 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4671 }
4672
4673 /*
4674 * Create a pNFS data file on the Data Server(s).
4675 */
4676 static void
nfsrv_pnfscreate(struct vnode * vp,struct vattr * vap,struct ucred * cred,NFSPROC_T * p)4677 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4678 NFSPROC_T *p)
4679 {
4680 struct nfsrvdscreate *dsc, *tdsc = NULL;
4681 struct nfsdevice *ds, *tds, *fds;
4682 struct mount *mp;
4683 struct pnfsdsfile *pf, *tpf;
4684 struct pnfsdsattr dsattr;
4685 struct vattr va;
4686 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4687 struct nfsmount *nmp;
4688 fhandle_t fh;
4689 uid_t vauid;
4690 gid_t vagid;
4691 u_short vamode;
4692 struct ucred *tcred;
4693 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4694 int failpos, timo;
4695
4696 /* Get a DS server directory in a round-robin order. */
4697 mirrorcnt = 1;
4698 mp = vp->v_mount;
4699 ds = fds = NULL;
4700 NFSDDSLOCK();
4701 /*
4702 * Search for the first entry that handles this MDS fs, but use the
4703 * first entry for all MDS fs's otherwise.
4704 */
4705 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4706 if (tds->nfsdev_nmp != NULL) {
4707 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4708 ds = tds;
4709 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4710 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4711 ds = fds = tds;
4712 break;
4713 }
4714 }
4715 }
4716 if (ds == NULL) {
4717 NFSDDSUNLOCK();
4718 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4719 return;
4720 }
4721 i = dsdir[0] = ds->nfsdev_nextdir;
4722 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4723 dvp[0] = ds->nfsdev_dsdir[i];
4724 tds = TAILQ_NEXT(ds, nfsdev_list);
4725 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4726 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4727 if (tds->nfsdev_nmp != NULL &&
4728 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4729 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4730 fsidcmp(&mp->mnt_stat.f_fsid,
4731 &tds->nfsdev_mdsfsid) == 0))) {
4732 dsdir[mirrorcnt] = i;
4733 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4734 mirrorcnt++;
4735 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4736 break;
4737 }
4738 }
4739 }
4740 /* Put at end of list to implement round-robin usage. */
4741 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4742 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4743 NFSDDSUNLOCK();
4744 dsc = NULL;
4745 if (mirrorcnt > 1)
4746 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4747 M_WAITOK | M_ZERO);
4748 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4749 M_ZERO);
4750
4751 error = nfsvno_getfh(vp, &fh, p);
4752 if (error == 0)
4753 error = VOP_GETATTR(vp, &va, cred);
4754 if (error == 0) {
4755 /* Set the attributes for "vp" to Setattr the DS vp. */
4756 vauid = va.va_uid;
4757 vagid = va.va_gid;
4758 vamode = va.va_mode;
4759 VATTR_NULL(&va);
4760 va.va_uid = vauid;
4761 va.va_gid = vagid;
4762 va.va_mode = vamode;
4763 va.va_size = 0;
4764 } else
4765 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4766
4767 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4768 cred->cr_gid);
4769 /* Make data file name based on FH. */
4770 tcred = newnfs_getcred();
4771
4772 /*
4773 * Create the file on each DS mirror, using kernel process(es) for the
4774 * additional mirrors.
4775 */
4776 failpos = -1;
4777 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4778 tpf->dsf_dir = dsdir[i];
4779 tdsc->tcred = tcred;
4780 tdsc->p = p;
4781 tdsc->pf = tpf;
4782 tdsc->createva = *vap;
4783 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4784 tdsc->va = va;
4785 tdsc->dvp = dvp[i];
4786 tdsc->done = 0;
4787 tdsc->inprog = 0;
4788 tdsc->err = 0;
4789 ret = EIO;
4790 if (nfs_pnfsiothreads != 0) {
4791 ret = nfs_pnfsio(start_dscreate, tdsc);
4792 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4793 }
4794 if (ret != 0) {
4795 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4796 NULL, tcred, p, NULL);
4797 if (ret != 0) {
4798 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4799 error));
4800 if (failpos == -1 && nfsds_failerr(ret))
4801 failpos = i;
4802 else
4803 error = ret;
4804 }
4805 }
4806 }
4807 if (error == 0) {
4808 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4809 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4810 &dsattr, NULL, tcred, p, NULL);
4811 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4812 failpos = mirrorcnt - 1;
4813 error = 0;
4814 }
4815 }
4816 timo = hz / 50; /* Wait for 20msec. */
4817 if (timo < 1)
4818 timo = 1;
4819 /* Wait for kernel task(s) to complete. */
4820 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4821 while (tdsc->inprog != 0 && tdsc->done == 0)
4822 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4823 if (tdsc->err != 0) {
4824 if (failpos == -1 && nfsds_failerr(tdsc->err))
4825 failpos = i;
4826 else if (error == 0)
4827 error = tdsc->err;
4828 }
4829 }
4830
4831 /*
4832 * If failpos has been set, that mirror has failed, so it needs
4833 * to be disabled.
4834 */
4835 if (failpos >= 0) {
4836 nmp = VFSTONFS(dvp[failpos]->v_mount);
4837 NFSLOCKMNT(nmp);
4838 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4839 NFSMNTP_CANCELRPCS)) == 0) {
4840 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4841 NFSUNLOCKMNT(nmp);
4842 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4843 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4844 ds);
4845 if (ds != NULL)
4846 nfsrv_killrpcs(nmp);
4847 NFSLOCKMNT(nmp);
4848 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4849 wakeup(nmp);
4850 }
4851 NFSUNLOCKMNT(nmp);
4852 }
4853
4854 NFSFREECRED(tcred);
4855 if (error == 0) {
4856 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4857
4858 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4859 mirrorcnt, nfsrv_maxpnfsmirror);
4860 /*
4861 * For all mirrors that couldn't be created, fill in the
4862 * *pf structure, but with an IP address == 0.0.0.0.
4863 */
4864 tpf = pf + mirrorcnt;
4865 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4866 *tpf = *pf;
4867 tpf->dsf_sin.sin_family = AF_INET;
4868 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4869 tpf->dsf_sin.sin_addr.s_addr = 0;
4870 tpf->dsf_sin.sin_port = 0;
4871 }
4872
4873 error = vn_extattr_set(vp, IO_NODELOCKED,
4874 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4875 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4876 if (error == 0)
4877 error = vn_extattr_set(vp, IO_NODELOCKED,
4878 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4879 sizeof(dsattr), (char *)&dsattr, p);
4880 if (error != 0)
4881 printf("pNFS: pnfscreate setextattr=%d\n",
4882 error);
4883 } else
4884 printf("pNFS: pnfscreate=%d\n", error);
4885 free(pf, M_TEMP);
4886 free(dsc, M_TEMP);
4887 }
4888
4889 /*
4890 * Get the information needed to remove the pNFS Data Server file from the
4891 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4892 * DS directory vnode. The caller must unlock *ddvp when done with it.
4893 */
4894 static void
nfsrv_pnfsremovesetup(struct vnode * vp,NFSPROC_T * p,struct vnode ** dvpp,int * mirrorcntp,char * fname,fhandle_t * fhp)4895 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4896 int *mirrorcntp, char *fname, fhandle_t *fhp)
4897 {
4898 struct vattr va;
4899 struct ucred *tcred;
4900 char *buf;
4901 int buflen, error;
4902
4903 dvpp[0] = NULL;
4904 /* If not an exported regular file or not a pNFS server, just return. */
4905 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4906 nfsrv_devidcnt == 0)
4907 return;
4908
4909 /* Check to see if this is the last hard link. */
4910 tcred = newnfs_getcred();
4911 error = VOP_GETATTR(vp, &va, tcred);
4912 NFSFREECRED(tcred);
4913 if (error != 0) {
4914 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4915 return;
4916 }
4917 if (va.va_nlink > 1)
4918 return;
4919
4920 error = nfsvno_getfh(vp, fhp, p);
4921 if (error != 0) {
4922 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4923 return;
4924 }
4925
4926 buflen = 1024;
4927 buf = malloc(buflen, M_TEMP, M_WAITOK);
4928 /* Get the directory vnode for the DS mount and the file handle. */
4929 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4930 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4931 free(buf, M_TEMP);
4932 if (error != 0)
4933 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4934 }
4935
4936 /*
4937 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4938 * The arguments are in a structure, so that they can be passed through
4939 * taskqueue for a kernel process to execute this function.
4940 */
4941 struct nfsrvdsremove {
4942 int done;
4943 int inprog;
4944 struct task tsk;
4945 struct ucred *tcred;
4946 struct vnode *dvp;
4947 NFSPROC_T *p;
4948 int err;
4949 char fname[PNFS_FILENAME_LEN + 1];
4950 };
4951
4952 static int
nfsrv_dsremove(struct vnode * dvp,char * fname,struct ucred * tcred,NFSPROC_T * p)4953 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4954 NFSPROC_T *p)
4955 {
4956 struct nameidata named;
4957 struct vnode *nvp;
4958 char *bufp;
4959 u_long *hashp;
4960 int error;
4961
4962 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4963 if (error != 0)
4964 return (error);
4965 named.ni_cnd.cn_nameiop = DELETE;
4966 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4967 named.ni_cnd.cn_cred = tcred;
4968 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
4969 nfsvno_setpathbuf(&named, &bufp, &hashp);
4970 named.ni_cnd.cn_nameptr = bufp;
4971 named.ni_cnd.cn_namelen = strlen(fname);
4972 strlcpy(bufp, fname, NAME_MAX);
4973 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4974 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4975 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4976 if (error == 0) {
4977 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4978 vput(nvp);
4979 }
4980 NFSVOPUNLOCK(dvp);
4981 nfsvno_relpathbuf(&named);
4982 if (error != 0)
4983 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4984 return (error);
4985 }
4986
4987 /*
4988 * Start up the thread that will execute nfsrv_dsremove().
4989 */
4990 static void
start_dsremove(void * arg,int pending)4991 start_dsremove(void *arg, int pending)
4992 {
4993 struct nfsrvdsremove *dsrm;
4994
4995 dsrm = (struct nfsrvdsremove *)arg;
4996 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4997 dsrm->p);
4998 dsrm->done = 1;
4999 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
5000 }
5001
5002 /*
5003 * Remove a pNFS data file from a Data Server.
5004 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
5005 * removed to set up the dvp and fill in the FH.
5006 */
5007 static void
nfsrv_pnfsremove(struct vnode ** dvp,int mirrorcnt,char * fname,fhandle_t * fhp,NFSPROC_T * p)5008 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
5009 NFSPROC_T *p)
5010 {
5011 struct ucred *tcred;
5012 struct nfsrvdsremove *dsrm, *tdsrm;
5013 struct nfsdevice *ds;
5014 struct nfsmount *nmp;
5015 int failpos, i, ret, timo;
5016
5017 tcred = newnfs_getcred();
5018 dsrm = NULL;
5019 if (mirrorcnt > 1)
5020 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
5021 /*
5022 * Remove the file on each DS mirror, using kernel process(es) for the
5023 * additional mirrors.
5024 */
5025 failpos = -1;
5026 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
5027 tdsrm->tcred = tcred;
5028 tdsrm->p = p;
5029 tdsrm->dvp = dvp[i];
5030 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
5031 tdsrm->inprog = 0;
5032 tdsrm->done = 0;
5033 tdsrm->err = 0;
5034 ret = EIO;
5035 if (nfs_pnfsiothreads != 0) {
5036 ret = nfs_pnfsio(start_dsremove, tdsrm);
5037 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
5038 }
5039 if (ret != 0) {
5040 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
5041 if (failpos == -1 && nfsds_failerr(ret))
5042 failpos = i;
5043 }
5044 }
5045 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
5046 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
5047 failpos = mirrorcnt - 1;
5048 timo = hz / 50; /* Wait for 20msec. */
5049 if (timo < 1)
5050 timo = 1;
5051 /* Wait for kernel task(s) to complete. */
5052 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
5053 while (tdsrm->inprog != 0 && tdsrm->done == 0)
5054 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
5055 if (failpos == -1 && nfsds_failerr(tdsrm->err))
5056 failpos = i;
5057 }
5058
5059 /*
5060 * If failpos has been set, that mirror has failed, so it needs
5061 * to be disabled.
5062 */
5063 if (failpos >= 0) {
5064 nmp = VFSTONFS(dvp[failpos]->v_mount);
5065 NFSLOCKMNT(nmp);
5066 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
5067 NFSMNTP_CANCELRPCS)) == 0) {
5068 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
5069 NFSUNLOCKMNT(nmp);
5070 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
5071 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
5072 ds);
5073 if (ds != NULL)
5074 nfsrv_killrpcs(nmp);
5075 NFSLOCKMNT(nmp);
5076 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
5077 wakeup(nmp);
5078 }
5079 NFSUNLOCKMNT(nmp);
5080 }
5081
5082 /* Get rid all layouts for the file. */
5083 nfsrv_freefilelayouts(fhp);
5084
5085 NFSFREECRED(tcred);
5086 free(dsrm, M_TEMP);
5087 }
5088
5089 /*
5090 * Generate a file name based on the file handle and put it in *bufp.
5091 * Return the number of bytes generated.
5092 */
5093 static int
nfsrv_putfhname(fhandle_t * fhp,char * bufp)5094 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
5095 {
5096 int i;
5097 uint8_t *cp;
5098 const uint8_t *hexdigits = "0123456789abcdef";
5099
5100 cp = (uint8_t *)fhp;
5101 for (i = 0; i < sizeof(*fhp); i++) {
5102 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
5103 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
5104 }
5105 bufp[2 * i] = '\0';
5106 return (2 * i);
5107 }
5108
5109 /*
5110 * Update the Metadata file's attributes from the DS file when a Read/Write
5111 * layout is returned.
5112 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
5113 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
5114 */
5115 int
nfsrv_updatemdsattr(struct vnode * vp,struct nfsvattr * nap,NFSPROC_T * p)5116 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5117 {
5118 struct ucred *tcred;
5119 int error;
5120
5121 /* Do this as root so that it won't fail with EACCES. */
5122 tcred = newnfs_getcred();
5123 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
5124 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
5125 NFSFREECRED(tcred);
5126 return (error);
5127 }
5128
5129 /*
5130 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
5131 */
5132 static int
nfsrv_dssetacl(struct vnode * vp,struct acl * aclp,struct ucred * cred,NFSPROC_T * p)5133 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
5134 NFSPROC_T *p)
5135 {
5136 int error;
5137
5138 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
5139 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
5140 return (error);
5141 }
5142
5143 static int
nfsrv_proxyds(struct vnode * vp,off_t off,int cnt,struct ucred * cred,struct thread * p,int ioproc,struct mbuf ** mpp,char * cp,struct mbuf ** mpp2,struct nfsvattr * nap,struct acl * aclp,off_t * offp,int content,bool * eofp)5144 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
5145 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
5146 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
5147 off_t *offp, int content, bool *eofp)
5148 {
5149 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
5150 fhandle_t fh[NFSDEV_MAXMIRRORS];
5151 struct vnode *dvp[NFSDEV_MAXMIRRORS];
5152 struct nfsdevice *ds;
5153 struct pnfsdsattr dsattr;
5154 struct opnfsdsattr odsattr;
5155 char *buf;
5156 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
5157
5158 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
5159 /*
5160 * If not a regular file, not exported or not a pNFS server,
5161 * just return ENOENT.
5162 */
5163 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
5164 nfsrv_devidcnt == 0)
5165 return (ENOENT);
5166
5167 buflen = 1024;
5168 buf = malloc(buflen, M_TEMP, M_WAITOK);
5169 error = 0;
5170
5171 /*
5172 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
5173 * from the MetaData file's extended attribute.
5174 */
5175 if (ioproc == NFSPROC_GETATTR) {
5176 error = vn_extattr_get(vp, IO_NODELOCKED,
5177 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
5178 p);
5179 if (error == 0) {
5180 if (buflen == sizeof(odsattr)) {
5181 NFSBCOPY(buf, &odsattr, buflen);
5182 nap->na_filerev = odsattr.dsa_filerev;
5183 nap->na_size = odsattr.dsa_size;
5184 nap->na_atime = odsattr.dsa_atime;
5185 nap->na_mtime = odsattr.dsa_mtime;
5186 /*
5187 * Fake na_bytes by rounding up na_size.
5188 * Since we don't know the block size, just
5189 * use BLKDEV_IOSIZE.
5190 */
5191 nap->na_bytes = (odsattr.dsa_size +
5192 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
5193 } else if (buflen == sizeof(dsattr)) {
5194 NFSBCOPY(buf, &dsattr, buflen);
5195 nap->na_filerev = dsattr.dsa_filerev;
5196 nap->na_size = dsattr.dsa_size;
5197 nap->na_atime = dsattr.dsa_atime;
5198 nap->na_mtime = dsattr.dsa_mtime;
5199 nap->na_bytes = dsattr.dsa_bytes;
5200 } else
5201 error = ENXIO;
5202 }
5203 if (error == 0) {
5204 /*
5205 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
5206 * returns 0, just return now. nfsrv_checkdsattr()
5207 * returns 0 if there is no Read/Write layout
5208 * plus either an Open/Write_access or Write
5209 * delegation issued to a client for the file.
5210 */
5211 if (nfsrv_pnfsgetdsattr == 0 ||
5212 nfsrv_checkdsattr(vp, p) == 0) {
5213 free(buf, M_TEMP);
5214 return (error);
5215 }
5216 }
5217
5218 /*
5219 * Clear ENOATTR so the code below will attempt to do a
5220 * nfsrv_getattrdsrpc() to get the attributes and (re)create
5221 * the extended attribute.
5222 */
5223 if (error == ENOATTR)
5224 error = 0;
5225 }
5226
5227 origmircnt = -1;
5228 trycnt = 0;
5229 tryagain:
5230 if (error == 0) {
5231 buflen = 1024;
5232 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
5233 LK_EXCLUSIVE)
5234 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
5235 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
5236 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
5237 NULL, NULL);
5238 if (error == 0) {
5239 for (i = 0; i < mirrorcnt; i++)
5240 nmp[i] = VFSTONFS(dvp[i]->v_mount);
5241 } else
5242 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
5243 } else
5244 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
5245 if (error == 0) {
5246 failpos = -1;
5247 if (origmircnt == -1)
5248 origmircnt = mirrorcnt;
5249 /*
5250 * If failpos is set to a mirror#, then that mirror has
5251 * failed and will be disabled. For Read, Getattr and Seek, the
5252 * function only tries one mirror, so if that mirror has
5253 * failed, it will need to be retried. As such, increment
5254 * tryitagain for these cases.
5255 * For Write, Setattr and Setacl, the function tries all
5256 * mirrors and will not return an error for the case where
5257 * one mirror has failed. For these cases, the functioning
5258 * mirror(s) will have been modified, so a retry isn't
5259 * necessary. These functions will set failpos for the
5260 * failed mirror#.
5261 */
5262 if (ioproc == NFSPROC_READDS) {
5263 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
5264 mpp, mpp2);
5265 if (nfsds_failerr(error) && mirrorcnt > 1) {
5266 /*
5267 * Setting failpos will cause the mirror
5268 * to be disabled and then a retry of this
5269 * read is required.
5270 */
5271 failpos = 0;
5272 error = 0;
5273 trycnt++;
5274 }
5275 } else if (ioproc == NFSPROC_WRITEDS)
5276 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
5277 &nmp[0], mirrorcnt, mpp, cp, &failpos);
5278 else if (ioproc == NFSPROC_SETATTR)
5279 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
5280 mirrorcnt, nap, &failpos);
5281 else if (ioproc == NFSPROC_SETACL)
5282 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
5283 mirrorcnt, aclp, &failpos);
5284 else if (ioproc == NFSPROC_SEEKDS) {
5285 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
5286 p, nmp[0]);
5287 if (nfsds_failerr(error) && mirrorcnt > 1) {
5288 /*
5289 * Setting failpos will cause the mirror
5290 * to be disabled and then a retry of this
5291 * read is required.
5292 */
5293 failpos = 0;
5294 error = 0;
5295 trycnt++;
5296 }
5297 } else if (ioproc == NFSPROC_ALLOCATE)
5298 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
5299 &nmp[0], mirrorcnt, &failpos);
5300 else if (ioproc == NFSPROC_DEALLOCATE)
5301 error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p,
5302 vp, &nmp[0], mirrorcnt, &failpos);
5303 else {
5304 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
5305 vp, nmp[mirrorcnt - 1], nap);
5306 if (nfsds_failerr(error) && mirrorcnt > 1) {
5307 /*
5308 * Setting failpos will cause the mirror
5309 * to be disabled and then a retry of this
5310 * getattr is required.
5311 */
5312 failpos = mirrorcnt - 1;
5313 error = 0;
5314 trycnt++;
5315 }
5316 }
5317 ds = NULL;
5318 if (failpos >= 0) {
5319 failnmp = nmp[failpos];
5320 NFSLOCKMNT(failnmp);
5321 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
5322 NFSMNTP_CANCELRPCS)) == 0) {
5323 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
5324 NFSUNLOCKMNT(failnmp);
5325 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
5326 failnmp, p);
5327 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
5328 failpos, ds);
5329 if (ds != NULL)
5330 nfsrv_killrpcs(failnmp);
5331 NFSLOCKMNT(failnmp);
5332 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
5333 wakeup(failnmp);
5334 }
5335 NFSUNLOCKMNT(failnmp);
5336 }
5337 for (i = 0; i < mirrorcnt; i++)
5338 NFSVOPUNLOCK(dvp[i]);
5339 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
5340 trycnt);
5341 /* Try the Read/Getattr again if a mirror was deleted. */
5342 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
5343 goto tryagain;
5344 } else {
5345 /* Return ENOENT for any Extended Attribute error. */
5346 error = ENOENT;
5347 }
5348 free(buf, M_TEMP);
5349 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
5350 return (error);
5351 }
5352
5353 /*
5354 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
5355 * attribute.
5356 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
5357 * to be checked. If it points to a NULL nmp, then it returns
5358 * a suitable destination.
5359 * curnmp - If non-NULL, it is the source mount for the copy.
5360 */
5361 int
nfsrv_dsgetsockmnt(struct vnode * vp,int lktype,char * buf,int * buflenp,int * mirrorcntp,NFSPROC_T * p,struct vnode ** dvpp,fhandle_t * fhp,char * devid,char * fnamep,struct vnode ** nvpp,struct nfsmount ** newnmpp,struct nfsmount * curnmp,int * ippos,int * dsdirp)5362 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
5363 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
5364 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
5365 struct nfsmount *curnmp, int *ippos, int *dsdirp)
5366 {
5367 struct vnode *dvp, *nvp = NULL, **tdvpp;
5368 struct mount *mp;
5369 struct nfsmount *nmp, *newnmp;
5370 struct sockaddr *sad;
5371 struct sockaddr_in *sin;
5372 struct nfsdevice *ds, *tds, *fndds;
5373 struct pnfsdsfile *pf;
5374 uint32_t dsdir;
5375 int error, fhiszero, fnd, gotone, i, mirrorcnt;
5376
5377 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
5378 *mirrorcntp = 1;
5379 tdvpp = dvpp;
5380 if (nvpp != NULL)
5381 *nvpp = NULL;
5382 if (dvpp != NULL)
5383 *dvpp = NULL;
5384 if (ippos != NULL)
5385 *ippos = -1;
5386 if (newnmpp != NULL)
5387 newnmp = *newnmpp;
5388 else
5389 newnmp = NULL;
5390 mp = vp->v_mount;
5391 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5392 "pnfsd.dsfile", buflenp, buf, p);
5393 mirrorcnt = *buflenp / sizeof(*pf);
5394 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
5395 *buflenp != sizeof(*pf) * mirrorcnt))
5396 error = ENOATTR;
5397
5398 pf = (struct pnfsdsfile *)buf;
5399 /* If curnmp != NULL, check for a match in the mirror list. */
5400 if (curnmp != NULL && error == 0) {
5401 fnd = 0;
5402 for (i = 0; i < mirrorcnt; i++, pf++) {
5403 sad = (struct sockaddr *)&pf->dsf_sin;
5404 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5405 if (ippos != NULL)
5406 *ippos = i;
5407 fnd = 1;
5408 break;
5409 }
5410 }
5411 if (fnd == 0)
5412 error = ENXIO;
5413 }
5414
5415 gotone = 0;
5416 pf = (struct pnfsdsfile *)buf;
5417 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5418 error);
5419 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5420 fhiszero = 0;
5421 sad = (struct sockaddr *)&pf->dsf_sin;
5422 sin = &pf->dsf_sin;
5423 dsdir = pf->dsf_dir;
5424 if (dsdir >= nfsrv_dsdirsize) {
5425 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5426 error = ENOATTR;
5427 } else if (nvpp != NULL && newnmp != NULL &&
5428 nfsaddr2_match(sad, newnmp->nm_nam))
5429 error = EEXIST;
5430 if (error == 0) {
5431 if (ippos != NULL && curnmp == NULL &&
5432 sad->sa_family == AF_INET &&
5433 sin->sin_addr.s_addr == 0)
5434 *ippos = i;
5435 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5436 fhiszero = 1;
5437 /* Use the socket address to find the mount point. */
5438 fndds = NULL;
5439 NFSDDSLOCK();
5440 /* Find a match for the IP address. */
5441 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5442 if (ds->nfsdev_nmp != NULL) {
5443 dvp = ds->nfsdev_dvp;
5444 nmp = VFSTONFS(dvp->v_mount);
5445 if (nmp != ds->nfsdev_nmp)
5446 printf("different2 nmp %p %p\n",
5447 nmp, ds->nfsdev_nmp);
5448 if (nfsaddr2_match(sad, nmp->nm_nam)) {
5449 fndds = ds;
5450 break;
5451 }
5452 }
5453 }
5454 if (fndds != NULL && newnmpp != NULL &&
5455 newnmp == NULL) {
5456 /* Search for a place to make a mirror copy. */
5457 TAILQ_FOREACH(tds, &nfsrv_devidhead,
5458 nfsdev_list) {
5459 if (tds->nfsdev_nmp != NULL &&
5460 fndds != tds &&
5461 ((tds->nfsdev_mdsisset == 0 &&
5462 fndds->nfsdev_mdsisset == 0) ||
5463 (tds->nfsdev_mdsisset != 0 &&
5464 fndds->nfsdev_mdsisset != 0 &&
5465 fsidcmp(&tds->nfsdev_mdsfsid,
5466 &mp->mnt_stat.f_fsid) == 0))) {
5467 *newnmpp = tds->nfsdev_nmp;
5468 break;
5469 }
5470 }
5471 if (tds != NULL) {
5472 /*
5473 * Move this entry to the end of the
5474 * list, so it won't be selected as
5475 * easily the next time.
5476 */
5477 TAILQ_REMOVE(&nfsrv_devidhead, tds,
5478 nfsdev_list);
5479 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5480 nfsdev_list);
5481 }
5482 }
5483 NFSDDSUNLOCK();
5484 if (fndds != NULL) {
5485 dvp = fndds->nfsdev_dsdir[dsdir];
5486 if (lktype != 0 || fhiszero != 0 ||
5487 (nvpp != NULL && *nvpp == NULL)) {
5488 if (fhiszero != 0)
5489 error = vn_lock(dvp,
5490 LK_EXCLUSIVE);
5491 else if (lktype != 0)
5492 error = vn_lock(dvp, lktype);
5493 else
5494 error = vn_lock(dvp, LK_SHARED);
5495 /*
5496 * If the file handle is all 0's, try to
5497 * do a Lookup against the DS to acquire
5498 * it.
5499 * If dvpp == NULL or the Lookup fails,
5500 * unlock dvp after the call.
5501 */
5502 if (error == 0 && (fhiszero != 0 ||
5503 (nvpp != NULL && *nvpp == NULL))) {
5504 error = nfsrv_pnfslookupds(vp,
5505 dvp, pf, &nvp, p);
5506 if (error == 0) {
5507 if (fhiszero != 0)
5508 nfsrv_pnfssetfh(
5509 vp, pf,
5510 devid,
5511 fnamep,
5512 nvp, p);
5513 if (nvpp != NULL &&
5514 *nvpp == NULL) {
5515 *nvpp = nvp;
5516 *dsdirp = dsdir;
5517 } else
5518 vput(nvp);
5519 }
5520 if (error != 0 || lktype == 0)
5521 NFSVOPUNLOCK(dvp);
5522 }
5523 }
5524 if (error == 0) {
5525 gotone++;
5526 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5527 if (devid != NULL) {
5528 NFSBCOPY(fndds->nfsdev_deviceid,
5529 devid, NFSX_V4DEVICEID);
5530 devid += NFSX_V4DEVICEID;
5531 }
5532 if (dvpp != NULL)
5533 *tdvpp++ = dvp;
5534 if (fhp != NULL)
5535 NFSBCOPY(&pf->dsf_fh, fhp++,
5536 NFSX_MYFH);
5537 if (fnamep != NULL && gotone == 1)
5538 strlcpy(fnamep,
5539 pf->dsf_filename,
5540 sizeof(pf->dsf_filename));
5541 } else
5542 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5543 "err=%d\n", error);
5544 }
5545 }
5546 }
5547 if (error == 0 && gotone == 0)
5548 error = ENOENT;
5549
5550 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5551 error);
5552 if (error == 0)
5553 *mirrorcntp = gotone;
5554 else {
5555 if (gotone > 0 && dvpp != NULL) {
5556 /*
5557 * If the error didn't occur on the first one and
5558 * dvpp != NULL, the one(s) prior to the failure will
5559 * have locked dvp's that need to be unlocked.
5560 */
5561 for (i = 0; i < gotone; i++) {
5562 NFSVOPUNLOCK(*dvpp);
5563 *dvpp++ = NULL;
5564 }
5565 }
5566 /*
5567 * If it found the vnode to be copied from before a failure,
5568 * it needs to be vput()'d.
5569 */
5570 if (nvpp != NULL && *nvpp != NULL) {
5571 vput(*nvpp);
5572 *nvpp = NULL;
5573 }
5574 }
5575 return (error);
5576 }
5577
5578 /*
5579 * Set the extended attribute for the Change attribute.
5580 */
5581 static int
nfsrv_setextattr(struct vnode * vp,struct nfsvattr * nap,NFSPROC_T * p)5582 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5583 {
5584 struct pnfsdsattr dsattr;
5585 int error;
5586
5587 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5588 dsattr.dsa_filerev = nap->na_filerev;
5589 dsattr.dsa_size = nap->na_size;
5590 dsattr.dsa_atime = nap->na_atime;
5591 dsattr.dsa_mtime = nap->na_mtime;
5592 dsattr.dsa_bytes = nap->na_bytes;
5593 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5594 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5595 if (error != 0)
5596 printf("pNFS: setextattr=%d\n", error);
5597 return (error);
5598 }
5599
5600 static int
nfsrv_readdsrpc(fhandle_t * fhp,off_t off,int len,struct ucred * cred,NFSPROC_T * p,struct nfsmount * nmp,struct mbuf ** mpp,struct mbuf ** mpendp)5601 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5602 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5603 {
5604 uint32_t *tl;
5605 struct nfsrv_descript *nd;
5606 nfsv4stateid_t st;
5607 struct mbuf *m, *m2;
5608 int error = 0, retlen, tlen, trimlen;
5609
5610 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5611 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5612 *mpp = NULL;
5613 /*
5614 * Use a stateid where other is an alternating 01010 pattern and
5615 * seqid is 0xffffffff. This value is not defined as special by
5616 * the RFC and is used by the FreeBSD NFS server to indicate an
5617 * MDS->DS proxy operation.
5618 */
5619 st.other[0] = 0x55555555;
5620 st.other[1] = 0x55555555;
5621 st.other[2] = 0x55555555;
5622 st.seqid = 0xffffffff;
5623 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5624 NULL, NULL, 0, 0, cred);
5625 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5626 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5627 txdr_hyper(off, tl);
5628 *(tl + 2) = txdr_unsigned(len);
5629 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5630 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5631 if (error != 0) {
5632 free(nd, M_TEMP);
5633 return (error);
5634 }
5635 if (nd->nd_repstat == 0) {
5636 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5637 NFSM_STRSIZ(retlen, len);
5638 if (retlen > 0) {
5639 /* Trim off the pre-data XDR from the mbuf chain. */
5640 m = nd->nd_mrep;
5641 while (m != NULL && m != nd->nd_md) {
5642 if (m->m_next == nd->nd_md) {
5643 m->m_next = NULL;
5644 m_freem(nd->nd_mrep);
5645 nd->nd_mrep = m = nd->nd_md;
5646 } else
5647 m = m->m_next;
5648 }
5649 if (m == NULL) {
5650 printf("nfsrv_readdsrpc: busted mbuf list\n");
5651 error = ENOENT;
5652 goto nfsmout;
5653 }
5654
5655 /*
5656 * Now, adjust first mbuf so that any XDR before the
5657 * read data is skipped over.
5658 */
5659 trimlen = nd->nd_dpos - mtod(m, char *);
5660 if (trimlen > 0) {
5661 m->m_len -= trimlen;
5662 NFSM_DATAP(m, trimlen);
5663 }
5664
5665 /*
5666 * Truncate the mbuf chain at retlen bytes of data,
5667 * plus XDR padding that brings the length up to a
5668 * multiple of 4.
5669 */
5670 tlen = NFSM_RNDUP(retlen);
5671 do {
5672 if (m->m_len >= tlen) {
5673 m->m_len = tlen;
5674 tlen = 0;
5675 m2 = m->m_next;
5676 m->m_next = NULL;
5677 m_freem(m2);
5678 break;
5679 }
5680 tlen -= m->m_len;
5681 m = m->m_next;
5682 } while (m != NULL);
5683 if (tlen > 0) {
5684 printf("nfsrv_readdsrpc: busted mbuf list\n");
5685 error = ENOENT;
5686 goto nfsmout;
5687 }
5688 *mpp = nd->nd_mrep;
5689 *mpendp = m;
5690 nd->nd_mrep = NULL;
5691 }
5692 } else
5693 error = nd->nd_repstat;
5694 nfsmout:
5695 /* If nd->nd_mrep is already NULL, this is a no-op. */
5696 m_freem(nd->nd_mrep);
5697 free(nd, M_TEMP);
5698 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5699 return (error);
5700 }
5701
5702 /*
5703 * Do a write RPC on a DS data file, using this structure for the arguments,
5704 * so that this function can be executed by a separate kernel process.
5705 */
5706 struct nfsrvwritedsdorpc {
5707 int done;
5708 int inprog;
5709 struct task tsk;
5710 fhandle_t fh;
5711 off_t off;
5712 int len;
5713 struct nfsmount *nmp;
5714 struct ucred *cred;
5715 NFSPROC_T *p;
5716 struct mbuf *m;
5717 int err;
5718 };
5719
5720 static int
nfsrv_writedsdorpc(struct nfsmount * nmp,fhandle_t * fhp,off_t off,int len,struct nfsvattr * nap,struct mbuf * m,struct ucred * cred,NFSPROC_T * p)5721 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5722 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5723 {
5724 uint32_t *tl;
5725 struct nfsrv_descript *nd;
5726 nfsattrbit_t attrbits;
5727 nfsv4stateid_t st;
5728 int commit, error, retlen;
5729
5730 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5731 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5732 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5733
5734 /*
5735 * Use a stateid where other is an alternating 01010 pattern and
5736 * seqid is 0xffffffff. This value is not defined as special by
5737 * the RFC and is used by the FreeBSD NFS server to indicate an
5738 * MDS->DS proxy operation.
5739 */
5740 st.other[0] = 0x55555555;
5741 st.other[1] = 0x55555555;
5742 st.other[2] = 0x55555555;
5743 st.seqid = 0xffffffff;
5744 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5745 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5746 txdr_hyper(off, tl);
5747 tl += 2;
5748 /*
5749 * Do all writes FileSync, since the server doesn't hold onto dirty
5750 * buffers. Since clients should be accessing the DS servers directly
5751 * using the pNFS layouts, this just needs to work correctly as a
5752 * fallback.
5753 */
5754 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5755 *tl = txdr_unsigned(len);
5756 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5757
5758 /* Put data in mbuf chain. */
5759 nd->nd_mb->m_next = m;
5760
5761 /* Set nd_mb and nd_bpos to end of data. */
5762 while (m->m_next != NULL)
5763 m = m->m_next;
5764 nd->nd_mb = m;
5765 nfsm_set(nd, m->m_len);
5766 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5767
5768 /* Do a Getattr for the attributes that change upon writing. */
5769 NFSZERO_ATTRBIT(&attrbits);
5770 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5771 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5772 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5773 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5774 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5775 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5776 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5777 (void) nfsrv_putattrbit(nd, &attrbits);
5778 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5779 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5780 if (error != 0) {
5781 free(nd, M_TEMP);
5782 return (error);
5783 }
5784 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5785 /* Get rid of weak cache consistency data for now. */
5786 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5787 (ND_NFSV4 | ND_V4WCCATTR)) {
5788 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5789 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL,
5790 NULL, NULL);
5791 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5792 if (error != 0)
5793 goto nfsmout;
5794 /*
5795 * Get rid of Op# and status for next op.
5796 */
5797 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5798 if (*++tl != 0)
5799 nd->nd_flag |= ND_NOMOREDATA;
5800 }
5801 if (nd->nd_repstat == 0) {
5802 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5803 retlen = fxdr_unsigned(int, *tl++);
5804 commit = fxdr_unsigned(int, *tl);
5805 if (commit != NFSWRITE_FILESYNC)
5806 error = NFSERR_IO;
5807 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5808 retlen, commit, error);
5809 } else
5810 error = nd->nd_repstat;
5811 /* We have no use for the Write Verifier since we use FileSync. */
5812
5813 /*
5814 * Get the Change, Size, Access Time and Modify Time attributes and set
5815 * on the Metadata file, so its attributes will be what the file's
5816 * would be if it had been written.
5817 */
5818 if (error == 0) {
5819 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5820 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5821 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL,
5822 NULL, NULL);
5823 }
5824 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5825 nfsmout:
5826 m_freem(nd->nd_mrep);
5827 free(nd, M_TEMP);
5828 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5829 return (error);
5830 }
5831
5832 /*
5833 * Start up the thread that will execute nfsrv_writedsdorpc().
5834 */
5835 static void
start_writedsdorpc(void * arg,int pending)5836 start_writedsdorpc(void *arg, int pending)
5837 {
5838 struct nfsrvwritedsdorpc *drpc;
5839
5840 drpc = (struct nfsrvwritedsdorpc *)arg;
5841 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5842 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5843 drpc->done = 1;
5844 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5845 }
5846
5847 static int
nfsrv_writedsrpc(fhandle_t * fhp,off_t off,int len,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,struct mbuf ** mpp,char * cp,int * failposp)5848 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5849 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5850 struct mbuf **mpp, char *cp, int *failposp)
5851 {
5852 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5853 struct nfsvattr na;
5854 struct mbuf *m;
5855 int error, i, offs, ret, timo;
5856
5857 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5858 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5859 drpc = NULL;
5860 if (mirrorcnt > 1)
5861 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5862 M_WAITOK);
5863
5864 /* Calculate offset in mbuf chain that data starts. */
5865 offs = cp - mtod(*mpp, char *);
5866 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5867
5868 /*
5869 * Do the write RPC for every DS, using a separate kernel process
5870 * for every DS except the last one.
5871 */
5872 error = 0;
5873 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5874 tdrpc->done = 0;
5875 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5876 tdrpc->off = off;
5877 tdrpc->len = len;
5878 tdrpc->nmp = *nmpp;
5879 tdrpc->cred = cred;
5880 tdrpc->p = p;
5881 tdrpc->inprog = 0;
5882 tdrpc->err = 0;
5883 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5884 ret = EIO;
5885 if (nfs_pnfsiothreads != 0) {
5886 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5887 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5888 ret);
5889 }
5890 if (ret != 0) {
5891 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5892 tdrpc->m, cred, p);
5893 if (nfsds_failerr(ret) && *failposp == -1)
5894 *failposp = i;
5895 else if (error == 0 && ret != 0)
5896 error = ret;
5897 }
5898 nmpp++;
5899 fhp++;
5900 }
5901 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5902 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5903 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5904 *failposp = mirrorcnt - 1;
5905 else if (error == 0 && ret != 0)
5906 error = ret;
5907 if (error == 0)
5908 error = nfsrv_setextattr(vp, &na, p);
5909 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5910 tdrpc = drpc;
5911 timo = hz / 50; /* Wait for 20msec. */
5912 if (timo < 1)
5913 timo = 1;
5914 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5915 /* Wait for RPCs on separate threads to complete. */
5916 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5917 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5918 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5919 *failposp = i;
5920 else if (error == 0 && tdrpc->err != 0)
5921 error = tdrpc->err;
5922 }
5923 free(drpc, M_TEMP);
5924 return (error);
5925 }
5926
5927 /*
5928 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5929 * so that this function can be executed by a separate kernel process.
5930 */
5931 struct nfsrvallocatedsdorpc {
5932 int done;
5933 int inprog;
5934 struct task tsk;
5935 fhandle_t fh;
5936 off_t off;
5937 off_t len;
5938 struct nfsmount *nmp;
5939 struct ucred *cred;
5940 NFSPROC_T *p;
5941 int err;
5942 };
5943
5944 static int
nfsrv_allocatedsdorpc(struct nfsmount * nmp,fhandle_t * fhp,off_t off,off_t len,struct nfsvattr * nap,struct ucred * cred,NFSPROC_T * p)5945 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5946 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5947 {
5948 uint32_t *tl;
5949 struct nfsrv_descript *nd;
5950 nfsattrbit_t attrbits;
5951 nfsv4stateid_t st;
5952 int error;
5953
5954 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5955 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5956 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5957
5958 /*
5959 * Use a stateid where other is an alternating 01010 pattern and
5960 * seqid is 0xffffffff. This value is not defined as special by
5961 * the RFC and is used by the FreeBSD NFS server to indicate an
5962 * MDS->DS proxy operation.
5963 */
5964 st.other[0] = 0x55555555;
5965 st.other[1] = 0x55555555;
5966 st.other[2] = 0x55555555;
5967 st.seqid = 0xffffffff;
5968 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5969 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5970 txdr_hyper(off, tl); tl += 2;
5971 txdr_hyper(len, tl); tl += 2;
5972 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5973
5974 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5975 NFSGETATTR_ATTRBIT(&attrbits);
5976 nfsrv_putattrbit(nd, &attrbits);
5977 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5978 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5979 if (error != 0) {
5980 free(nd, M_TEMP);
5981 return (error);
5982 }
5983 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5984 nd->nd_repstat);
5985 if (nd->nd_repstat == 0) {
5986 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5987 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5988 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL,
5989 NULL, NULL);
5990 } else
5991 error = nd->nd_repstat;
5992 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5993 nfsmout:
5994 m_freem(nd->nd_mrep);
5995 free(nd, M_TEMP);
5996 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5997 return (error);
5998 }
5999
6000 /*
6001 * Start up the thread that will execute nfsrv_allocatedsdorpc().
6002 */
6003 static void
start_allocatedsdorpc(void * arg,int pending)6004 start_allocatedsdorpc(void *arg, int pending)
6005 {
6006 struct nfsrvallocatedsdorpc *drpc;
6007
6008 drpc = (struct nfsrvallocatedsdorpc *)arg;
6009 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
6010 drpc->len, NULL, drpc->cred, drpc->p);
6011 drpc->done = 1;
6012 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
6013 }
6014
6015 static int
nfsrv_allocatedsrpc(fhandle_t * fhp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,int * failposp)6016 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
6017 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6018 int *failposp)
6019 {
6020 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
6021 struct nfsvattr na;
6022 int error, i, ret, timo;
6023
6024 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
6025 drpc = NULL;
6026 if (mirrorcnt > 1)
6027 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6028 M_WAITOK);
6029
6030 /*
6031 * Do the allocate RPC for every DS, using a separate kernel process
6032 * for every DS except the last one.
6033 */
6034 error = 0;
6035 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6036 tdrpc->done = 0;
6037 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6038 tdrpc->off = off;
6039 tdrpc->len = len;
6040 tdrpc->nmp = *nmpp;
6041 tdrpc->cred = cred;
6042 tdrpc->p = p;
6043 tdrpc->inprog = 0;
6044 tdrpc->err = 0;
6045 ret = EIO;
6046 if (nfs_pnfsiothreads != 0) {
6047 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
6048 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
6049 ret);
6050 }
6051 if (ret != 0) {
6052 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
6053 cred, p);
6054 if (nfsds_failerr(ret) && *failposp == -1)
6055 *failposp = i;
6056 else if (error == 0 && ret != 0)
6057 error = ret;
6058 }
6059 nmpp++;
6060 fhp++;
6061 }
6062 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
6063 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6064 *failposp = mirrorcnt - 1;
6065 else if (error == 0 && ret != 0)
6066 error = ret;
6067 if (error == 0)
6068 error = nfsrv_setextattr(vp, &na, p);
6069 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
6070 tdrpc = drpc;
6071 timo = hz / 50; /* Wait for 20msec. */
6072 if (timo < 1)
6073 timo = 1;
6074 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6075 /* Wait for RPCs on separate threads to complete. */
6076 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6077 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
6078 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6079 *failposp = i;
6080 else if (error == 0 && tdrpc->err != 0)
6081 error = tdrpc->err;
6082 }
6083 free(drpc, M_TEMP);
6084 return (error);
6085 }
6086
6087 /*
6088 * Do a deallocate RPC on a DS data file, using this structure for the
6089 * arguments, so that this function can be executed by a separate kernel
6090 * process.
6091 */
6092 struct nfsrvdeallocatedsdorpc {
6093 int done;
6094 int inprog;
6095 struct task tsk;
6096 fhandle_t fh;
6097 off_t off;
6098 off_t len;
6099 struct nfsmount *nmp;
6100 struct ucred *cred;
6101 NFSPROC_T *p;
6102 int err;
6103 };
6104
6105 static int
nfsrv_deallocatedsdorpc(struct nfsmount * nmp,fhandle_t * fhp,off_t off,off_t len,struct nfsvattr * nap,struct ucred * cred,NFSPROC_T * p)6106 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
6107 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
6108 {
6109 uint32_t *tl;
6110 struct nfsrv_descript *nd;
6111 nfsattrbit_t attrbits;
6112 nfsv4stateid_t st;
6113 int error;
6114
6115 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6116 nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp,
6117 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6118
6119 /*
6120 * Use a stateid where other is an alternating 01010 pattern and
6121 * seqid is 0xffffffff. This value is not defined as special by
6122 * the RFC and is used by the FreeBSD NFS server to indicate an
6123 * MDS->DS proxy operation.
6124 */
6125 st.other[0] = 0x55555555;
6126 st.other[1] = 0x55555555;
6127 st.other[2] = 0x55555555;
6128 st.seqid = 0xffffffff;
6129 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6130 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
6131 txdr_hyper(off, tl); tl += 2;
6132 txdr_hyper(len, tl); tl += 2;
6133 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len);
6134
6135 /* Do a Getattr for the attributes that change upon writing. */
6136 NFSZERO_ATTRBIT(&attrbits);
6137 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6138 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6139 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6140 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6141 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6142 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6143 nfsrv_putattrbit(nd, &attrbits);
6144 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
6145 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6146 if (error != 0) {
6147 free(nd, M_TEMP);
6148 return (error);
6149 }
6150 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n",
6151 nd->nd_repstat);
6152 /* Get rid of weak cache consistency data for now. */
6153 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
6154 (ND_NFSV4 | ND_V4WCCATTR)) {
6155 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
6156 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL,
6157 NULL, NULL);
6158 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error);
6159 if (error != 0)
6160 goto nfsmout;
6161 /*
6162 * Get rid of Op# and status for next op.
6163 */
6164 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6165 if (*++tl != 0)
6166 nd->nd_flag |= ND_NOMOREDATA;
6167 }
6168 if (nd->nd_repstat == 0) {
6169 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6170 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
6171 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL,
6172 NULL, NULL);
6173 } else
6174 error = nd->nd_repstat;
6175 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error);
6176 nfsmout:
6177 m_freem(nd->nd_mrep);
6178 free(nd, M_TEMP);
6179 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error);
6180 return (error);
6181 }
6182
6183 /*
6184 * Start up the thread that will execute nfsrv_deallocatedsdorpc().
6185 */
6186 static void
start_deallocatedsdorpc(void * arg,int pending)6187 start_deallocatedsdorpc(void *arg, int pending)
6188 {
6189 struct nfsrvdeallocatedsdorpc *drpc;
6190
6191 drpc = (struct nfsrvdeallocatedsdorpc *)arg;
6192 drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
6193 drpc->len, NULL, drpc->cred, drpc->p);
6194 drpc->done = 1;
6195 NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err);
6196 }
6197
6198 static int
nfsrv_deallocatedsrpc(fhandle_t * fhp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,int * failposp)6199 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
6200 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6201 int *failposp)
6202 {
6203 struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL;
6204 struct nfsvattr na;
6205 int error, i, ret, timo;
6206
6207 NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n");
6208 drpc = NULL;
6209 if (mirrorcnt > 1)
6210 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6211 M_WAITOK);
6212
6213 /*
6214 * Do the deallocate RPC for every DS, using a separate kernel process
6215 * for every DS except the last one.
6216 */
6217 error = 0;
6218 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6219 tdrpc->done = 0;
6220 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6221 tdrpc->off = off;
6222 tdrpc->len = len;
6223 tdrpc->nmp = *nmpp;
6224 tdrpc->cred = cred;
6225 tdrpc->p = p;
6226 tdrpc->inprog = 0;
6227 tdrpc->err = 0;
6228 ret = EIO;
6229 if (nfs_pnfsiothreads != 0) {
6230 ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc);
6231 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n",
6232 ret);
6233 }
6234 if (ret != 0) {
6235 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len,
6236 NULL, cred, p);
6237 if (nfsds_failerr(ret) && *failposp == -1)
6238 *failposp = i;
6239 else if (error == 0 && ret != 0)
6240 error = ret;
6241 }
6242 nmpp++;
6243 fhp++;
6244 }
6245 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
6246 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6247 *failposp = mirrorcnt - 1;
6248 else if (error == 0 && ret != 0)
6249 error = ret;
6250 if (error == 0)
6251 error = nfsrv_setextattr(vp, &na, p);
6252 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error);
6253 tdrpc = drpc;
6254 timo = hz / 50; /* Wait for 20msec. */
6255 if (timo < 1)
6256 timo = 1;
6257 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6258 /* Wait for RPCs on separate threads to complete. */
6259 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6260 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
6261 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6262 *failposp = i;
6263 else if (error == 0 && tdrpc->err != 0)
6264 error = tdrpc->err;
6265 }
6266 free(drpc, M_TEMP);
6267 return (error);
6268 }
6269
6270 static int
nfsrv_setattrdsdorpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct nfsvattr * nap,struct nfsvattr * dsnap)6271 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6272 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
6273 struct nfsvattr *dsnap)
6274 {
6275 uint32_t *tl;
6276 struct nfsrv_descript *nd;
6277 nfsv4stateid_t st;
6278 nfsattrbit_t attrbits;
6279 int error;
6280
6281 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
6282 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6283 /*
6284 * Use a stateid where other is an alternating 01010 pattern and
6285 * seqid is 0xffffffff. This value is not defined as special by
6286 * the RFC and is used by the FreeBSD NFS server to indicate an
6287 * MDS->DS proxy operation.
6288 */
6289 st.other[0] = 0x55555555;
6290 st.other[1] = 0x55555555;
6291 st.other[2] = 0x55555555;
6292 st.seqid = 0xffffffff;
6293 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6294 NULL, NULL, 0, 0, cred);
6295 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6296 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
6297
6298 /* Do a Getattr for the attributes that change due to writing. */
6299 NFSZERO_ATTRBIT(&attrbits);
6300 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6301 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6302 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6303 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6304 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6305 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
6306 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6307 (void) nfsrv_putattrbit(nd, &attrbits);
6308 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6309 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6310 if (error != 0) {
6311 free(nd, M_TEMP);
6312 return (error);
6313 }
6314 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
6315 nd->nd_repstat);
6316 /* Get rid of weak cache consistency data for now. */
6317 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
6318 (ND_NFSV4 | ND_V4WCCATTR)) {
6319 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
6320 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL,
6321 NULL, NULL, NULL);
6322 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
6323 if (error != 0)
6324 goto nfsmout;
6325 /*
6326 * Get rid of Op# and status for next op.
6327 */
6328 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6329 if (*++tl != 0)
6330 nd->nd_flag |= ND_NOMOREDATA;
6331 }
6332 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
6333 if (error != 0)
6334 goto nfsmout;
6335 if (nd->nd_repstat != 0)
6336 error = nd->nd_repstat;
6337 /*
6338 * Get the Change, Size, Access Time and Modify Time attributes and set
6339 * on the Metadata file, so its attributes will be what the file's
6340 * would be if it had been written.
6341 */
6342 if (error == 0) {
6343 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6344 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
6345 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL,
6346 NULL, NULL, NULL);
6347 }
6348 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
6349 nfsmout:
6350 m_freem(nd->nd_mrep);
6351 free(nd, M_TEMP);
6352 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
6353 return (error);
6354 }
6355
6356 struct nfsrvsetattrdsdorpc {
6357 int done;
6358 int inprog;
6359 struct task tsk;
6360 fhandle_t fh;
6361 struct nfsmount *nmp;
6362 struct vnode *vp;
6363 struct ucred *cred;
6364 NFSPROC_T *p;
6365 struct nfsvattr na;
6366 struct nfsvattr dsna;
6367 int err;
6368 };
6369
6370 /*
6371 * Start up the thread that will execute nfsrv_setattrdsdorpc().
6372 */
6373 static void
start_setattrdsdorpc(void * arg,int pending)6374 start_setattrdsdorpc(void *arg, int pending)
6375 {
6376 struct nfsrvsetattrdsdorpc *drpc;
6377
6378 drpc = (struct nfsrvsetattrdsdorpc *)arg;
6379 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
6380 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
6381 drpc->done = 1;
6382 }
6383
6384 static int
nfsrv_setattrdsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,struct nfsvattr * nap,int * failposp)6385 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6386 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6387 struct nfsvattr *nap, int *failposp)
6388 {
6389 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
6390 struct nfsvattr na;
6391 int error, i, ret, timo;
6392
6393 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
6394 drpc = NULL;
6395 if (mirrorcnt > 1)
6396 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6397 M_WAITOK);
6398
6399 /*
6400 * Do the setattr RPC for every DS, using a separate kernel process
6401 * for every DS except the last one.
6402 */
6403 error = 0;
6404 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6405 tdrpc->done = 0;
6406 tdrpc->inprog = 0;
6407 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6408 tdrpc->nmp = *nmpp;
6409 tdrpc->vp = vp;
6410 tdrpc->cred = cred;
6411 tdrpc->p = p;
6412 tdrpc->na = *nap;
6413 tdrpc->err = 0;
6414 ret = EIO;
6415 if (nfs_pnfsiothreads != 0) {
6416 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
6417 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
6418 ret);
6419 }
6420 if (ret != 0) {
6421 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
6422 &na);
6423 if (nfsds_failerr(ret) && *failposp == -1)
6424 *failposp = i;
6425 else if (error == 0 && ret != 0)
6426 error = ret;
6427 }
6428 nmpp++;
6429 fhp++;
6430 }
6431 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
6432 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6433 *failposp = mirrorcnt - 1;
6434 else if (error == 0 && ret != 0)
6435 error = ret;
6436 if (error == 0)
6437 error = nfsrv_setextattr(vp, &na, p);
6438 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
6439 tdrpc = drpc;
6440 timo = hz / 50; /* Wait for 20msec. */
6441 if (timo < 1)
6442 timo = 1;
6443 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6444 /* Wait for RPCs on separate threads to complete. */
6445 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6446 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
6447 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6448 *failposp = i;
6449 else if (error == 0 && tdrpc->err != 0)
6450 error = tdrpc->err;
6451 }
6452 free(drpc, M_TEMP);
6453 return (error);
6454 }
6455
6456 /*
6457 * Do a Setattr of an NFSv4 ACL on the DS file.
6458 */
6459 static int
nfsrv_setacldsdorpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct acl * aclp)6460 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6461 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
6462 {
6463 struct nfsrv_descript *nd;
6464 nfsv4stateid_t st;
6465 nfsattrbit_t attrbits;
6466 int error;
6467
6468 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
6469 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6470 /*
6471 * Use a stateid where other is an alternating 01010 pattern and
6472 * seqid is 0xffffffff. This value is not defined as special by
6473 * the RFC and is used by the FreeBSD NFS server to indicate an
6474 * MDS->DS proxy operation.
6475 */
6476 st.other[0] = 0x55555555;
6477 st.other[1] = 0x55555555;
6478 st.other[2] = 0x55555555;
6479 st.seqid = 0xffffffff;
6480 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6481 NULL, NULL, 0, 0, cred);
6482 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6483 NFSZERO_ATTRBIT(&attrbits);
6484 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
6485 /*
6486 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
6487 * so passing in the metadata "vp" will be ok, since it is of
6488 * the same type (VREG).
6489 */
6490 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
6491 NULL, 0, 0, 0, 0, 0, NULL, false, false, false, 0, NULL, false);
6492 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6493 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6494 if (error != 0) {
6495 free(nd, M_TEMP);
6496 return (error);
6497 }
6498 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
6499 nd->nd_repstat);
6500 error = nd->nd_repstat;
6501 m_freem(nd->nd_mrep);
6502 free(nd, M_TEMP);
6503 return (error);
6504 }
6505
6506 struct nfsrvsetacldsdorpc {
6507 int done;
6508 int inprog;
6509 struct task tsk;
6510 fhandle_t fh;
6511 struct nfsmount *nmp;
6512 struct vnode *vp;
6513 struct ucred *cred;
6514 NFSPROC_T *p;
6515 struct acl *aclp;
6516 int err;
6517 };
6518
6519 /*
6520 * Start up the thread that will execute nfsrv_setacldsdorpc().
6521 */
6522 static void
start_setacldsdorpc(void * arg,int pending)6523 start_setacldsdorpc(void *arg, int pending)
6524 {
6525 struct nfsrvsetacldsdorpc *drpc;
6526
6527 drpc = (struct nfsrvsetacldsdorpc *)arg;
6528 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
6529 drpc->vp, drpc->nmp, drpc->aclp);
6530 drpc->done = 1;
6531 }
6532
6533 static int
nfsrv_setacldsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,struct acl * aclp,int * failposp)6534 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6535 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
6536 int *failposp)
6537 {
6538 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
6539 int error, i, ret, timo;
6540
6541 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
6542 drpc = NULL;
6543 if (mirrorcnt > 1)
6544 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6545 M_WAITOK);
6546
6547 /*
6548 * Do the setattr RPC for every DS, using a separate kernel process
6549 * for every DS except the last one.
6550 */
6551 error = 0;
6552 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6553 tdrpc->done = 0;
6554 tdrpc->inprog = 0;
6555 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6556 tdrpc->nmp = *nmpp;
6557 tdrpc->vp = vp;
6558 tdrpc->cred = cred;
6559 tdrpc->p = p;
6560 tdrpc->aclp = aclp;
6561 tdrpc->err = 0;
6562 ret = EIO;
6563 if (nfs_pnfsiothreads != 0) {
6564 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
6565 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
6566 ret);
6567 }
6568 if (ret != 0) {
6569 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
6570 aclp);
6571 if (nfsds_failerr(ret) && *failposp == -1)
6572 *failposp = i;
6573 else if (error == 0 && ret != 0)
6574 error = ret;
6575 }
6576 nmpp++;
6577 fhp++;
6578 }
6579 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
6580 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6581 *failposp = mirrorcnt - 1;
6582 else if (error == 0 && ret != 0)
6583 error = ret;
6584 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
6585 tdrpc = drpc;
6586 timo = hz / 50; /* Wait for 20msec. */
6587 if (timo < 1)
6588 timo = 1;
6589 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6590 /* Wait for RPCs on separate threads to complete. */
6591 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6592 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6593 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6594 *failposp = i;
6595 else if (error == 0 && tdrpc->err != 0)
6596 error = tdrpc->err;
6597 }
6598 free(drpc, M_TEMP);
6599 return (error);
6600 }
6601
6602 /*
6603 * Getattr call to the DS for the attributes that change due to writing.
6604 */
6605 static int
nfsrv_getattrdsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct nfsvattr * nap)6606 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6607 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6608 {
6609 struct nfsrv_descript *nd;
6610 int error;
6611 nfsattrbit_t attrbits;
6612
6613 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6614 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6615 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6616 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6617 NFSZERO_ATTRBIT(&attrbits);
6618 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6619 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6620 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6621 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6622 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6623 (void) nfsrv_putattrbit(nd, &attrbits);
6624 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6625 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6626 if (error != 0) {
6627 free(nd, M_TEMP);
6628 return (error);
6629 }
6630 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6631 nd->nd_repstat);
6632 if (nd->nd_repstat == 0) {
6633 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6634 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6635 NULL, NULL, NULL, NULL, NULL);
6636 /*
6637 * We can only save the updated values in the extended
6638 * attribute if the vp is exclusively locked.
6639 * This should happen when any of the following operations
6640 * occur on the vnode:
6641 * Close, Delegreturn, LayoutCommit, LayoutReturn
6642 * As such, the updated extended attribute should get saved
6643 * before nfsrv_checkdsattr() returns 0 and allows the cached
6644 * attributes to be returned without calling this function.
6645 */
6646 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6647 error = nfsrv_setextattr(vp, nap, p);
6648 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6649 error);
6650 }
6651 } else
6652 error = nd->nd_repstat;
6653 m_freem(nd->nd_mrep);
6654 free(nd, M_TEMP);
6655 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6656 return (error);
6657 }
6658
6659 /*
6660 * Seek call to a DS.
6661 */
6662 static int
nfsrv_seekdsrpc(fhandle_t * fhp,off_t * offp,int content,bool * eofp,struct ucred * cred,NFSPROC_T * p,struct nfsmount * nmp)6663 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6664 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6665 {
6666 uint32_t *tl;
6667 struct nfsrv_descript *nd;
6668 nfsv4stateid_t st;
6669 int error;
6670
6671 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6672 /*
6673 * Use a stateid where other is an alternating 01010 pattern and
6674 * seqid is 0xffffffff. This value is not defined as special by
6675 * the RFC and is used by the FreeBSD NFS server to indicate an
6676 * MDS->DS proxy operation.
6677 */
6678 st.other[0] = 0x55555555;
6679 st.other[1] = 0x55555555;
6680 st.other[2] = 0x55555555;
6681 st.seqid = 0xffffffff;
6682 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6683 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6684 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6685 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6686 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6687 txdr_hyper(*offp, tl); tl += 2;
6688 *tl = txdr_unsigned(content);
6689 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6690 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6691 if (error != 0) {
6692 free(nd, M_TEMP);
6693 return (error);
6694 }
6695 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6696 if (nd->nd_repstat == 0) {
6697 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6698 if (*tl++ == newnfs_true)
6699 *eofp = true;
6700 else
6701 *eofp = false;
6702 *offp = fxdr_hyper(tl);
6703 } else
6704 error = nd->nd_repstat;
6705 nfsmout:
6706 m_freem(nd->nd_mrep);
6707 free(nd, M_TEMP);
6708 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6709 return (error);
6710 }
6711
6712 /*
6713 * Get the device id and file handle for a DS file.
6714 */
6715 int
nfsrv_dsgetdevandfh(struct vnode * vp,NFSPROC_T * p,int * mirrorcntp,fhandle_t * fhp,char * devid)6716 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6717 fhandle_t *fhp, char *devid)
6718 {
6719 int buflen, error;
6720 char *buf;
6721
6722 buflen = 1024;
6723 buf = malloc(buflen, M_TEMP, M_WAITOK);
6724 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6725 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6726 free(buf, M_TEMP);
6727 return (error);
6728 }
6729
6730 /*
6731 * Do a Lookup against the DS for the filename.
6732 */
6733 static int
nfsrv_pnfslookupds(struct vnode * vp,struct vnode * dvp,struct pnfsdsfile * pf,struct vnode ** nvpp,NFSPROC_T * p)6734 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6735 struct vnode **nvpp, NFSPROC_T *p)
6736 {
6737 struct nameidata named;
6738 struct ucred *tcred;
6739 char *bufp;
6740 u_long *hashp;
6741 struct vnode *nvp;
6742 int error;
6743
6744 tcred = newnfs_getcred();
6745 named.ni_cnd.cn_nameiop = LOOKUP;
6746 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6747 named.ni_cnd.cn_cred = tcred;
6748 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
6749 nfsvno_setpathbuf(&named, &bufp, &hashp);
6750 named.ni_cnd.cn_nameptr = bufp;
6751 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6752 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6753 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6754 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6755 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6756 NFSFREECRED(tcred);
6757 nfsvno_relpathbuf(&named);
6758 if (error == 0)
6759 *nvpp = nvp;
6760 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6761 return (error);
6762 }
6763
6764 /*
6765 * Set the file handle to the correct one.
6766 */
6767 static void
nfsrv_pnfssetfh(struct vnode * vp,struct pnfsdsfile * pf,char * devid,char * fnamep,struct vnode * nvp,NFSPROC_T * p)6768 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6769 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6770 {
6771 struct nfsnode *np;
6772 int ret = 0;
6773
6774 np = VTONFS(nvp);
6775 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6776 /*
6777 * We can only do a vn_set_extattr() if the vnode is exclusively
6778 * locked and vn_start_write() has been done. If devid != NULL or
6779 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6780 * may not have been done.
6781 * If not done now, it will be done on a future call.
6782 */
6783 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6784 LK_EXCLUSIVE)
6785 ret = vn_extattr_set(vp, IO_NODELOCKED,
6786 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6787 (char *)pf, p);
6788 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6789 }
6790
6791 /*
6792 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6793 * when the DS has failed.
6794 */
6795 void
nfsrv_killrpcs(struct nfsmount * nmp)6796 nfsrv_killrpcs(struct nfsmount *nmp)
6797 {
6798
6799 /*
6800 * Call newnfs_nmcancelreqs() to cause
6801 * any RPCs in progress on the mount point to
6802 * fail.
6803 * This will cause any process waiting for an
6804 * RPC to complete while holding a vnode lock
6805 * on the mounted-on vnode (such as "df" or
6806 * a non-forced "umount") to fail.
6807 * This will unlock the mounted-on vnode so
6808 * a forced dismount can succeed.
6809 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6810 * called.
6811 */
6812 newnfs_nmcancelreqs(nmp);
6813 }
6814
6815 /*
6816 * Sum up the statfs info for each of the DSs, so that the client will
6817 * receive the total for all DSs.
6818 */
6819 static int
nfsrv_pnfsstatfs(struct statfs * sf,struct mount * mp)6820 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6821 {
6822 struct statfs *tsf;
6823 struct nfsdevice *ds;
6824 struct vnode **dvpp, **tdvpp, *dvp;
6825 uint64_t tot;
6826 int cnt, error = 0, i;
6827
6828 if (nfsrv_devidcnt <= 0)
6829 return (ENXIO);
6830 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6831 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6832
6833 /* Get an array of the dvps for the DSs. */
6834 tdvpp = dvpp;
6835 i = 0;
6836 NFSDDSLOCK();
6837 /* First, search for matches for same file system. */
6838 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6839 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6840 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6841 if (++i > nfsrv_devidcnt)
6842 break;
6843 *tdvpp++ = ds->nfsdev_dvp;
6844 }
6845 }
6846 /*
6847 * If no matches for same file system, total all servers not assigned
6848 * to a file system.
6849 */
6850 if (i == 0) {
6851 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6852 if (ds->nfsdev_nmp != NULL &&
6853 ds->nfsdev_mdsisset == 0) {
6854 if (++i > nfsrv_devidcnt)
6855 break;
6856 *tdvpp++ = ds->nfsdev_dvp;
6857 }
6858 }
6859 }
6860 NFSDDSUNLOCK();
6861 cnt = i;
6862
6863 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6864 tdvpp = dvpp;
6865 for (i = 0; i < cnt && error == 0; i++) {
6866 dvp = *tdvpp++;
6867 error = VFS_STATFS(dvp->v_mount, tsf);
6868 if (error == 0) {
6869 if (sf->f_bsize == 0) {
6870 if (tsf->f_bsize > 0)
6871 sf->f_bsize = tsf->f_bsize;
6872 else
6873 sf->f_bsize = 8192;
6874 }
6875 if (tsf->f_blocks > 0) {
6876 if (sf->f_bsize != tsf->f_bsize) {
6877 tot = tsf->f_blocks * tsf->f_bsize;
6878 sf->f_blocks += (tot / sf->f_bsize);
6879 } else
6880 sf->f_blocks += tsf->f_blocks;
6881 }
6882 if (tsf->f_bfree > 0) {
6883 if (sf->f_bsize != tsf->f_bsize) {
6884 tot = tsf->f_bfree * tsf->f_bsize;
6885 sf->f_bfree += (tot / sf->f_bsize);
6886 } else
6887 sf->f_bfree += tsf->f_bfree;
6888 }
6889 if (tsf->f_bavail > 0) {
6890 if (sf->f_bsize != tsf->f_bsize) {
6891 tot = tsf->f_bavail * tsf->f_bsize;
6892 sf->f_bavail += (tot / sf->f_bsize);
6893 } else
6894 sf->f_bavail += tsf->f_bavail;
6895 }
6896 }
6897 }
6898 free(tsf, M_TEMP);
6899 free(dvpp, M_TEMP);
6900 return (error);
6901 }
6902
6903 /*
6904 * Set an acl.
6905 */
6906 int
nfsrv_setacl(struct vnode * vp,NFSACL_T * aclp,acl_type_t atype,struct ucred * cred,NFSPROC_T * p)6907 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, acl_type_t atype,
6908 struct ucred *cred, NFSPROC_T *p)
6909 {
6910 int error;
6911
6912 if (nfsrv_useacl == 0 || (atype == ACL_TYPE_NFS4 &&
6913 nfs_supportsnfsv4acls(vp) == 0) || (atype != ACL_TYPE_NFS4 &&
6914 nfs_supportsposixacls(vp) == 0)) {
6915 error = NFSERR_ATTRNOTSUPP;
6916 goto out;
6917 }
6918 /*
6919 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6920 * Make sure it has enough room for that - splitting every entry
6921 * into two and appending "canonical six" entries at the end.
6922 * Cribbed out of kern/vfs_acl.c - Rick M.
6923 */
6924 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6925 error = NFSERR_ATTRNOTSUPP;
6926 goto out;
6927 }
6928 if (aclp->acl_cnt == 0) {
6929 if (atype != ACL_TYPE_DEFAULT || vp->v_type != VDIR) {
6930 error = NFSERR_INVAL;
6931 goto out;
6932 }
6933 error = VOP_SETACL(vp, atype, NULL, cred, p);
6934 } else
6935 error = VOP_SETACL(vp, atype, aclp, cred, p);
6936 if (error == 0) {
6937 error = nfsrv_dssetacl(vp, aclp, cred, p);
6938 if (error == ENOENT)
6939 error = 0;
6940 }
6941
6942 out:
6943 NFSEXITCODE(error);
6944 return (error);
6945 }
6946
6947 /*
6948 * Seek vnode op call (actually it is a VOP_IOCTL()).
6949 * This function is called with the vnode locked, but unlocks and vrele()s
6950 * the vp before returning.
6951 */
6952 int
nfsvno_seek(struct nfsrv_descript * nd,struct vnode * vp,u_long cmd,off_t * offp,int content,bool * eofp,struct ucred * cred,NFSPROC_T * p)6953 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6954 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6955 {
6956 struct nfsvattr at;
6957 int error, ret;
6958
6959 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6960 /*
6961 * Attempt to seek on a DS file. A return of ENOENT implies
6962 * there is no DS file to seek on.
6963 */
6964 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6965 NULL, NULL, NULL, NULL, offp, content, eofp);
6966 if (error != ENOENT) {
6967 vput(vp);
6968 return (error);
6969 }
6970
6971 /*
6972 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6973 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6974 * NFSv4.2 is *eofp == true and error == 0 for this case.
6975 */
6976 NFSVOPUNLOCK(vp);
6977 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6978 *eofp = false;
6979 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6980 /* Handle the cases where we might be at EOF. */
6981 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6982 if (ret == 0 && *offp == at.na_size) {
6983 *eofp = true;
6984 error = 0;
6985 }
6986 if (ret != 0 && error == 0)
6987 error = ret;
6988 }
6989 vrele(vp);
6990 NFSEXITCODE(error);
6991 return (error);
6992 }
6993
6994 /*
6995 * Allocate vnode op call.
6996 */
6997 int
nfsvno_allocate(struct vnode * vp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p)6998 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6999 NFSPROC_T *p)
7000 {
7001 int error;
7002 off_t olen;
7003
7004 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
7005 /*
7006 * Attempt to allocate on a DS file. A return of ENOENT implies
7007 * there is no DS file to allocate on.
7008 */
7009 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
7010 NULL, NULL, NULL, NULL, &len, 0, NULL);
7011 if (error != ENOENT)
7012 return (error);
7013
7014 /*
7015 * Do the actual VOP_ALLOCATE(), looping so long as
7016 * progress is being made, to achieve completion.
7017 */
7018 do {
7019 olen = len;
7020 error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred);
7021 if (error == 0 && len > 0 && olen > len)
7022 maybe_yield();
7023 } while (error == 0 && len > 0 && olen > len);
7024 if (error == 0 && len > 0)
7025 error = NFSERR_IO;
7026 NFSEXITCODE(error);
7027 return (error);
7028 }
7029
7030 /*
7031 * Deallocate vnode op call.
7032 */
7033 int
nfsvno_deallocate(struct vnode * vp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p)7034 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
7035 NFSPROC_T *p)
7036 {
7037 int error;
7038 off_t olen;
7039
7040 ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp");
7041 /*
7042 * Attempt to deallocate on a DS file. A return of ENOENT implies
7043 * there is no DS file to deallocate on.
7044 */
7045 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL,
7046 NULL, NULL, NULL, NULL, &len, 0, NULL);
7047 if (error != ENOENT)
7048 return (error);
7049
7050 /*
7051 * Do the actual VOP_DEALLOCATE(), looping so long as
7052 * progress is being made, to achieve completion.
7053 */
7054 do {
7055 olen = len;
7056 error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred);
7057 if (error == 0 && len > 0 && olen > len)
7058 maybe_yield();
7059 } while (error == 0 && len > 0 && olen > len);
7060 if (error == 0 && len > 0)
7061 error = NFSERR_IO;
7062 NFSEXITCODE(error);
7063 return (error);
7064 }
7065
7066 /*
7067 * Get Extended Atribute vnode op into an mbuf list.
7068 */
7069 int
nfsvno_getxattr(struct vnode * vp,char * name,uint32_t maxresp,struct ucred * cred,uint64_t flag,int maxextsiz,struct thread * p,struct mbuf ** mpp,struct mbuf ** mpendp,int * lenp)7070 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
7071 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
7072 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
7073 {
7074 struct iovec *iv;
7075 struct uio io, *uiop = &io;
7076 struct mbuf *m, *m2;
7077 int alen, error, len, tlen;
7078 size_t siz;
7079
7080 /* First, find out the size of the extended attribute. */
7081 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
7082 &siz, cred, p);
7083 if (error != 0)
7084 return (NFSERR_NOXATTR);
7085 if (siz > maxresp - NFS_MAXXDR)
7086 return (NFSERR_XATTR2BIG);
7087 len = siz;
7088 tlen = NFSM_RNDUP(len);
7089 if (tlen > 0) {
7090 /*
7091 * If cnt > MCLBYTES and the reply will not be saved, use
7092 * ext_pgs mbufs for TLS.
7093 * For NFSv4.0, we do not know for sure if the reply will
7094 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
7095 * Always use ext_pgs mbufs if ND_EXTPG is set.
7096 */
7097 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
7098 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
7099 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
7100 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
7101 maxextsiz, &m, &m2, &iv);
7102 else
7103 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
7104 &iv);
7105 uiop->uio_iov = iv;
7106 } else {
7107 uiop->uio_iovcnt = 0;
7108 uiop->uio_iov = iv = NULL;
7109 m = m2 = NULL;
7110 }
7111 uiop->uio_offset = 0;
7112 uiop->uio_resid = tlen;
7113 uiop->uio_rw = UIO_READ;
7114 uiop->uio_segflg = UIO_SYSSPACE;
7115 uiop->uio_td = p;
7116 #ifdef MAC
7117 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
7118 name);
7119 if (error != 0)
7120 goto out;
7121 #endif
7122
7123 if (tlen > 0)
7124 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
7125 NULL, cred, p);
7126 if (error != 0)
7127 goto out;
7128 if (uiop->uio_resid > 0) {
7129 alen = tlen;
7130 len = tlen - uiop->uio_resid;
7131 tlen = NFSM_RNDUP(len);
7132 if (alen != tlen)
7133 printf("nfsvno_getxattr: weird size read\n");
7134 if (tlen == 0) {
7135 m_freem(m);
7136 m = m2 = NULL;
7137 } else if (alen != tlen || tlen != len)
7138 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
7139 }
7140 *lenp = len;
7141 *mpp = m;
7142 *mpendp = m2;
7143
7144 out:
7145 if (error != 0) {
7146 if (m != NULL)
7147 m_freem(m);
7148 *lenp = 0;
7149 }
7150 free(iv, M_TEMP);
7151 NFSEXITCODE(error);
7152 return (error);
7153 }
7154
7155 /*
7156 * Set Extended attribute vnode op from an mbuf list.
7157 */
7158 int
nfsvno_setxattr(struct vnode * vp,char * name,int len,struct mbuf * m,char * cp,struct ucred * cred,struct thread * p)7159 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
7160 char *cp, struct ucred *cred, struct thread *p)
7161 {
7162 struct iovec *iv;
7163 struct uio uio, *uiop = &uio;
7164 int cnt, error;
7165
7166 error = 0;
7167 #ifdef MAC
7168 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
7169 name);
7170 #endif
7171 if (error != 0)
7172 goto out;
7173
7174 uiop->uio_rw = UIO_WRITE;
7175 uiop->uio_segflg = UIO_SYSSPACE;
7176 uiop->uio_td = p;
7177 uiop->uio_offset = 0;
7178 uiop->uio_resid = len;
7179 if (len > 0) {
7180 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
7181 uiop->uio_iov = iv;
7182 uiop->uio_iovcnt = cnt;
7183 } else {
7184 uiop->uio_iov = iv = NULL;
7185 uiop->uio_iovcnt = 0;
7186 }
7187 if (error == 0) {
7188 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
7189 cred, p);
7190 if (error == 0) {
7191 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
7192 nfsvno_updateds(vp, cred, p);
7193 error = VOP_FSYNC(vp, MNT_WAIT, p);
7194 }
7195 free(iv, M_TEMP);
7196 }
7197
7198 out:
7199 NFSEXITCODE(error);
7200 return (error);
7201 }
7202
7203 /*
7204 * For a pNFS server, the DS file's ctime and
7205 * va_filerev (TimeMetadata and Change) needs to
7206 * be updated. This is a hack, but works by
7207 * flipping the S_ISGID bit in va_mode and then
7208 * flipping it back.
7209 * It does result in two MDS->DS RPCs, but creating
7210 * a custom RPC just to do this seems overkill, since
7211 * Setxattr/Rmxattr will not be done that frequently.
7212 * If it fails part way through, that is not too
7213 * serious, since the DS file is never executed.
7214 */
7215 static void
nfsvno_updateds(struct vnode * vp,struct ucred * cred,NFSPROC_T * p)7216 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p)
7217 {
7218 struct nfsvattr nva;
7219 int ret;
7220 u_short tmode;
7221
7222 ret = VOP_GETATTR(vp, &nva.na_vattr, cred);
7223 if (ret == 0) {
7224 tmode = nva.na_mode;
7225 NFSVNO_ATTRINIT(&nva);
7226 tmode ^= S_ISGID;
7227 NFSVNO_SETATTRVAL(&nva, mode, tmode);
7228 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
7229 NFSPROC_SETATTR, NULL, NULL, NULL, &nva,
7230 NULL, NULL, 0, NULL);
7231 if (ret == 0) {
7232 tmode ^= S_ISGID;
7233 NFSVNO_SETATTRVAL(&nva, mode, tmode);
7234 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
7235 NFSPROC_SETATTR, NULL, NULL, NULL,
7236 &nva, NULL, NULL, 0, NULL);
7237 }
7238 }
7239 }
7240
7241 /*
7242 * Remove Extended attribute vnode op.
7243 */
7244 int
nfsvno_rmxattr(struct nfsrv_descript * nd,struct vnode * vp,char * name,struct ucred * cred,struct thread * p)7245 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
7246 struct ucred *cred, struct thread *p)
7247 {
7248 int error;
7249
7250 /*
7251 * Get rid of any delegations. I am not sure why this is required,
7252 * but RFC-8276 says so.
7253 */
7254 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
7255 if (error != 0)
7256 goto out;
7257 #ifdef MAC
7258 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
7259 name);
7260 if (error != 0)
7261 goto out;
7262 #endif
7263
7264 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
7265 if (error == EOPNOTSUPP)
7266 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
7267 cred, p);
7268 if (error == 0) {
7269 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
7270 nfsvno_updateds(vp, cred, p);
7271 error = VOP_FSYNC(vp, MNT_WAIT, p);
7272 }
7273 out:
7274 NFSEXITCODE(error);
7275 return (error);
7276 }
7277
7278 /*
7279 * List Extended Atribute vnode op into an mbuf list.
7280 */
7281 int
nfsvno_listxattr(struct vnode * vp,uint64_t cookie,struct ucred * cred,struct thread * p,u_char ** bufp,uint32_t * lenp,bool * eofp)7282 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
7283 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
7284 {
7285 struct iovec iv;
7286 struct uio io;
7287 int error;
7288 size_t siz;
7289
7290 *bufp = NULL;
7291 /* First, find out the size of the extended attribute. */
7292 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
7293 p);
7294 if (error != 0)
7295 return (NFSERR_NOXATTR);
7296 if (siz <= cookie) {
7297 *lenp = 0;
7298 *eofp = true;
7299 goto out;
7300 }
7301 if (siz > cookie + *lenp) {
7302 siz = cookie + *lenp;
7303 *eofp = false;
7304 } else
7305 *eofp = true;
7306 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
7307 if (siz > 10 * 1024 * 1024) {
7308 error = NFSERR_XATTR2BIG;
7309 goto out;
7310 }
7311 *bufp = malloc(siz, M_TEMP, M_WAITOK);
7312 iv.iov_base = *bufp;
7313 iv.iov_len = siz;
7314 io.uio_iovcnt = 1;
7315 io.uio_iov = &iv;
7316 io.uio_offset = 0;
7317 io.uio_resid = siz;
7318 io.uio_rw = UIO_READ;
7319 io.uio_segflg = UIO_SYSSPACE;
7320 io.uio_td = p;
7321 #ifdef MAC
7322 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
7323 if (error != 0)
7324 goto out;
7325 #endif
7326
7327 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
7328 p);
7329 if (error != 0)
7330 goto out;
7331 if (io.uio_resid > 0)
7332 siz -= io.uio_resid;
7333 *lenp = siz;
7334
7335 out:
7336 if (error != 0) {
7337 free(*bufp, M_TEMP);
7338 *bufp = NULL;
7339 }
7340 NFSEXITCODE(error);
7341 return (error);
7342 }
7343
7344 /*
7345 * Trim trailing data off the mbuf list being built.
7346 */
7347 void
nfsm_trimtrailing(struct nfsrv_descript * nd,struct mbuf * mb,char * bpos,int bextpg,int bextpgsiz)7348 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
7349 int bextpg, int bextpgsiz)
7350 {
7351 vm_page_t pg;
7352 int fullpgsiz, i;
7353
7354 if (mb->m_next != NULL) {
7355 m_freem(mb->m_next);
7356 mb->m_next = NULL;
7357 }
7358 if ((mb->m_flags & M_EXTPG) != 0) {
7359 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
7360 ("nfsm_trimtrailing: bextpg out of range"));
7361 KASSERT(bpos == (char *)(void *)
7362 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
7363 ("nfsm_trimtrailing: bextpgsiz bad!"));
7364
7365 /* First, get rid of any pages after this position. */
7366 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
7367 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
7368 vm_page_unwire_noq(pg);
7369 vm_page_free(pg);
7370 }
7371 mb->m_epg_npgs = bextpg + 1;
7372 if (bextpg == 0)
7373 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
7374 else
7375 fullpgsiz = PAGE_SIZE;
7376 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
7377 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
7378 for (i = 1; i < mb->m_epg_npgs; i++)
7379 mb->m_len += m_epg_pagelen(mb, i, 0);
7380 nd->nd_bextpgsiz = bextpgsiz;
7381 nd->nd_bextpg = bextpg;
7382 } else
7383 mb->m_len = bpos - mtod(mb, char *);
7384 nd->nd_mb = mb;
7385 nd->nd_bpos = bpos;
7386 }
7387
7388
7389 /*
7390 * Check to see if a put file handle operation should test for
7391 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
7392 * When Open is the next operation, NFSERR_WRONGSEC cannot be
7393 * replied for the Open cases that use a component. This can
7394 * be identified by the fact that the file handle's type is VDIR.
7395 */
7396 bool
nfsrv_checkwrongsec(struct nfsrv_descript * nd,int nextop,__enum_uint8 (vtype)vtyp)7397 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, __enum_uint8(vtype) vtyp)
7398 {
7399
7400 if ((nd->nd_flag & ND_NFSV4) == 0)
7401 return (true);
7402
7403 if ((nd->nd_flag & ND_LASTOP) != 0)
7404 return (false);
7405
7406 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
7407 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
7408 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
7409 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
7410 return (false);
7411 if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
7412 return (false);
7413 return (true);
7414 }
7415
7416 /*
7417 * Check DSs marked no space.
7418 */
7419 void
nfsrv_checknospc(void)7420 nfsrv_checknospc(void)
7421 {
7422 struct statfs *tsf;
7423 struct nfsdevice *ds;
7424 struct vnode **dvpp, **tdvpp, *dvp;
7425 char *devid, *tdevid;
7426 int cnt, error = 0, i;
7427
7428 if (nfsrv_devidcnt <= 0)
7429 return;
7430 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
7431 devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK);
7432 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
7433
7434 /* Get an array of the dvps for the DSs. */
7435 tdvpp = dvpp;
7436 tdevid = devid;
7437 i = 0;
7438 NFSDDSLOCK();
7439 /* First, search for matches for same file system. */
7440 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7441 if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) {
7442 if (++i > nfsrv_devidcnt)
7443 break;
7444 *tdvpp++ = ds->nfsdev_dvp;
7445 NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID);
7446 tdevid += NFSX_V4DEVICEID;
7447 }
7448 }
7449 NFSDDSUNLOCK();
7450
7451 /* Do a VFS_STATFS() for each of the DSs and clear no space. */
7452 cnt = i;
7453 tdvpp = dvpp;
7454 tdevid = devid;
7455 for (i = 0; i < cnt && error == 0; i++) {
7456 dvp = *tdvpp++;
7457 error = VFS_STATFS(dvp->v_mount, tsf);
7458 if (error == 0 && tsf->f_bavail > 0) {
7459 NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n");
7460 nfsrv_marknospc(tdevid, false);
7461 }
7462 tdevid += NFSX_V4DEVICEID;
7463 }
7464 free(tsf, M_TEMP);
7465 free(dvpp, M_TEMP);
7466 free(devid, M_TEMP);
7467 }
7468
7469 /*
7470 * Return the correct ACL support value for a vnode.
7471 */
7472 int
nfs_supportsacls(struct vnode * vp)7473 nfs_supportsacls(struct vnode *vp)
7474 {
7475
7476 if (nfs_supportsnfsv4acls(vp) != 0)
7477 return (SUPPACL_NFSV4);
7478 else if (nfs_supportsposixacls(vp) != 0)
7479 return (SUPPACL_POSIX);
7480 return (SUPPACL_NONE);
7481 }
7482
7483 /*
7484 * Initialize everything that needs to be initialized for a vnet.
7485 */
7486 static void
nfsrv_vnetinit(const void * unused __unused)7487 nfsrv_vnetinit(const void *unused __unused)
7488 {
7489
7490 nfsd_mntinit();
7491 }
7492 VNET_SYSINIT(nfsrv_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7493 nfsrv_vnetinit, NULL);
7494
7495 /*
7496 * Clean up everything that is in a vnet and needs to be
7497 * done when the jail is destroyed or the module unloaded.
7498 */
7499 static void
nfsrv_cleanup(const void * unused __unused)7500 nfsrv_cleanup(const void *unused __unused)
7501 {
7502 int i;
7503
7504 NFSD_LOCK();
7505 if (!NFSD_VNET(nfsrv_mntinited)) {
7506 NFSD_UNLOCK();
7507 return;
7508 }
7509 NFSD_VNET(nfsrv_mntinited) = false;
7510 NFSD_UNLOCK();
7511
7512 /* Clean out all NFSv4 state. */
7513 nfsrv_throwawayallstate(curthread);
7514
7515 /* Clean the NFS server reply cache */
7516 nfsrvd_cleancache();
7517
7518 /* Clean out v4root exports. */
7519 if (NFSD_VNET(nfsv4root_mnt)->mnt_export != NULL) {
7520 vfs_free_addrlist(NFSD_VNET(nfsv4root_mnt)->mnt_export);
7521 free(NFSD_VNET(nfsv4root_mnt)->mnt_export, M_MOUNT);
7522 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
7523 }
7524
7525 /* Free up the krpc server pool. */
7526 if (NFSD_VNET(nfsrvd_pool) != NULL)
7527 svcpool_destroy(NFSD_VNET(nfsrvd_pool));
7528
7529 /* and get rid of the locks */
7530 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7531 mtx_destroy(&NFSD_VNET(nfsrchash_table)[i].mtx);
7532 mtx_destroy(&NFSD_VNET(nfsrcahash_table)[i].mtx);
7533 }
7534 mtx_destroy(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx);
7535 for (i = 0; i < nfsrv_sessionhashsize; i++)
7536 mtx_destroy(&NFSD_VNET(nfssessionhash)[i].mtx);
7537 lockdestroy(&NFSD_VNET(nfsv4root_mnt)->mnt_explock);
7538 free(NFSD_VNET(nfsrvudphashtbl), M_NFSRVCACHE);
7539 free(NFSD_VNET(nfsrchash_table), M_NFSRVCACHE);
7540 free(NFSD_VNET(nfsrcahash_table), M_NFSRVCACHE);
7541 free(NFSD_VNET(nfsclienthash), M_NFSDCLIENT);
7542 free(NFSD_VNET(nfslockhash), M_NFSDLOCKFILE);
7543 free(NFSD_VNET(nfssessionhash), M_NFSDSESSION);
7544 free(NFSD_VNET(nfsv4root_mnt), M_TEMP);
7545 NFSD_VNET(nfsv4root_mnt) = NULL;
7546 }
7547 VNET_SYSUNINIT(nfsrv_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7548 nfsrv_cleanup, NULL);
7549
7550 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
7551
7552 /*
7553 * Called once to initialize data structures...
7554 */
7555 static int
nfsd_modevent(module_t mod,int type,void * data)7556 nfsd_modevent(module_t mod, int type, void *data)
7557 {
7558 int error = 0, i;
7559 static int loaded = 0;
7560
7561 switch (type) {
7562 case MOD_LOAD:
7563 if (loaded)
7564 goto out;
7565 newnfs_portinit();
7566 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
7567 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
7568 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
7569 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
7570 #ifdef VV_DISABLEDELEG
7571 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
7572 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
7573 #endif
7574 nfsd_call_nfsd = nfssvc_nfsd;
7575 loaded = 1;
7576 break;
7577
7578 case MOD_UNLOAD:
7579 if (newnfs_numnfsd != 0) {
7580 error = EBUSY;
7581 break;
7582 }
7583
7584 #ifdef VV_DISABLEDELEG
7585 vn_deleg_ops.vndeleg_recall = NULL;
7586 vn_deleg_ops.vndeleg_disable = NULL;
7587 #endif
7588 nfsd_call_nfsd = NULL;
7589 mtx_destroy(&nfsrc_udpmtx);
7590 mtx_destroy(&nfs_v4root_mutex);
7591 mtx_destroy(&nfsrv_dontlistlock_mtx);
7592 mtx_destroy(&nfsrv_recalllock_mtx);
7593 if (nfslayouthash != NULL) {
7594 for (i = 0; i < nfsrv_layouthashsize; i++)
7595 mtx_destroy(&nfslayouthash[i].mtx);
7596 free(nfslayouthash, M_NFSDSESSION);
7597 }
7598 loaded = 0;
7599 break;
7600 default:
7601 error = EOPNOTSUPP;
7602 break;
7603 }
7604
7605 out:
7606 NFSEXITCODE(error);
7607 return (error);
7608 }
7609 static moduledata_t nfsd_mod = {
7610 "nfsd",
7611 nfsd_modevent,
7612 NULL,
7613 };
7614 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
7615
7616 /* So that loader and kldload(2) can find us, wherever we are.. */
7617 MODULE_VERSION(nfsd, 1);
7618 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
7619 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
7620 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
7621 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
7622