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