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