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)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 {
2117 struct statfs *sf;
2118 int error;
2119
2120 sf = NULL;
2121 if (nfsrv_devidcnt > 0 &&
2122 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
2123 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
2124 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
2125 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
2126 error = nfsrv_pnfsstatfs(sf, mp);
2127 if (error != 0) {
2128 free(sf, M_TEMP);
2129 sf = NULL;
2130 }
2131 }
2132 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
2133 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
2134 mounted_on_fileno, sf);
2135 free(sf, M_TEMP);
2136 NFSEXITCODE2(0, nd);
2137 return (error);
2138 }
2139
2140 /*
2141 * Convert a dirent d_type to a vnode type.
2142 */
nfs_dtypetovtype(struct nfsvattr * nvap,struct vnode * vp,uint8_t dtype)2143 static void nfs_dtypetovtype(struct nfsvattr *nvap, struct vnode *vp,
2144 uint8_t dtype)
2145 {
2146
2147 if ((vn_irflag_read(vp) & VIRF_NAMEDDIR) != 0) {
2148 nvap->na_type = VREG;
2149 nvap->na_bsdflags |= SFBSD_NAMEDATTR;
2150 } else if (dtype <= DT_WHT) {
2151 nvap->na_type = dtype_to_vnode[dtype];
2152 } else {
2153 nvap->na_type = VNON;
2154 }
2155 }
2156
2157 /* Since the Readdir vnode ops vary, put the entire functions in here. */
2158 /*
2159 * nfs readdir service
2160 * - mallocs what it thinks is enough to read
2161 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
2162 * - calls VOP_READDIR()
2163 * - loops around building the reply
2164 * if the output generated exceeds count break out of loop
2165 * The NFSM_CLGET macro is used here so that the reply will be packed
2166 * tightly in mbuf clusters.
2167 * - it trims out records with d_fileno == 0
2168 * this doesn't matter for Unix clients, but they might confuse clients
2169 * for other os'.
2170 * - it trims out records with d_type == DT_WHT
2171 * these cannot be seen through NFS (unless we extend the protocol)
2172 * The alternate call nfsrvd_readdirplus() does lookups as well.
2173 * PS: The NFS protocol spec. does not clarify what the "count" byte
2174 * argument is a count of.. just name strings and file id's or the
2175 * entire reply rpc or ...
2176 * I tried just file name and id sizes and it confused the Sun client,
2177 * so I am using the full rpc size now. The "paranoia.." comment refers
2178 * to including the status longwords that are not a part of the dir.
2179 * "entry" structures, but are in the rpc.
2180 */
2181 int
nfsrvd_readdir(struct nfsrv_descript * nd,int isdgram,struct vnode * vp,struct nfsexstuff * exp)2182 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2183 struct vnode *vp, struct nfsexstuff *exp)
2184 {
2185 struct dirent *dp;
2186 u_int32_t *tl;
2187 int dirlen;
2188 char *cpos, *cend, *rbuf;
2189 struct nfsvattr at;
2190 int nlen, error = 0, getret = 1;
2191 int siz, cnt, fullsiz, eofflag, ncookies;
2192 u_int64_t off, toff, verf __unused;
2193 uint64_t *cookies = NULL, *cookiep;
2194 struct uio io;
2195 struct iovec iv;
2196 int is_ufs;
2197 struct thread *p = curthread;
2198
2199 if (nd->nd_repstat) {
2200 nfsrv_postopattr(nd, getret, &at);
2201 goto out;
2202 }
2203 if (nd->nd_flag & ND_NFSV2) {
2204 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2205 off = fxdr_unsigned(u_quad_t, *tl++);
2206 } else {
2207 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2208 off = fxdr_hyper(tl);
2209 tl += 2;
2210 verf = fxdr_hyper(tl);
2211 tl += 2;
2212 }
2213 toff = off;
2214 cnt = fxdr_unsigned(int, *tl);
2215 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2216 cnt = NFS_SRVMAXDATA(nd);
2217 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2218 fullsiz = siz;
2219 if (nd->nd_flag & ND_NFSV3) {
2220 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2221 NULL);
2222 #if 0
2223 /*
2224 * va_filerev is not sufficient as a cookie verifier,
2225 * since it is not supposed to change when entries are
2226 * removed/added unless that offset cookies returned to
2227 * the client are no longer valid.
2228 */
2229 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2230 nd->nd_repstat = NFSERR_BAD_COOKIE;
2231 #endif
2232 }
2233 if (!nd->nd_repstat && vp->v_type != VDIR)
2234 nd->nd_repstat = NFSERR_NOTDIR;
2235 if (nd->nd_repstat == 0 && cnt == 0) {
2236 if (nd->nd_flag & ND_NFSV2)
2237 /* NFSv2 does not have NFSERR_TOOSMALL */
2238 nd->nd_repstat = EPERM;
2239 else
2240 nd->nd_repstat = NFSERR_TOOSMALL;
2241 }
2242 if (!nd->nd_repstat)
2243 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2244 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2245 NFSACCCHK_VPISLOCKED, NULL);
2246 if (nd->nd_repstat) {
2247 vput(vp);
2248 if (nd->nd_flag & ND_NFSV3)
2249 nfsrv_postopattr(nd, getret, &at);
2250 goto out;
2251 }
2252 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2253 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2254 again:
2255 eofflag = 0;
2256 if (cookies) {
2257 free(cookies, M_TEMP);
2258 cookies = NULL;
2259 }
2260
2261 iv.iov_base = rbuf;
2262 iv.iov_len = siz;
2263 io.uio_iov = &iv;
2264 io.uio_iovcnt = 1;
2265 io.uio_offset = (off_t)off;
2266 io.uio_resid = siz;
2267 io.uio_segflg = UIO_SYSSPACE;
2268 io.uio_rw = UIO_READ;
2269 io.uio_td = NULL;
2270 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2271 &cookies);
2272 off = (u_int64_t)io.uio_offset;
2273 if (io.uio_resid)
2274 siz -= io.uio_resid;
2275
2276 if (!cookies && !nd->nd_repstat)
2277 nd->nd_repstat = NFSERR_PERM;
2278 if (nd->nd_flag & ND_NFSV3) {
2279 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2280 if (!nd->nd_repstat)
2281 nd->nd_repstat = getret;
2282 }
2283
2284 /*
2285 * Handles the failed cases. nd->nd_repstat == 0 past here.
2286 */
2287 if (nd->nd_repstat) {
2288 vput(vp);
2289 free(rbuf, M_TEMP);
2290 if (cookies)
2291 free(cookies, M_TEMP);
2292 if (nd->nd_flag & ND_NFSV3)
2293 nfsrv_postopattr(nd, getret, &at);
2294 goto out;
2295 }
2296 /*
2297 * If nothing read, return eof
2298 * rpc reply
2299 */
2300 if (siz == 0) {
2301 vput(vp);
2302 if (nd->nd_flag & ND_NFSV2) {
2303 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2304 } else {
2305 nfsrv_postopattr(nd, getret, &at);
2306 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2307 txdr_hyper(at.na_filerev, tl);
2308 tl += 2;
2309 }
2310 *tl++ = newnfs_false;
2311 *tl = newnfs_true;
2312 free(rbuf, M_TEMP);
2313 free(cookies, M_TEMP);
2314 goto out;
2315 }
2316
2317 /*
2318 * Check for degenerate cases of nothing useful read.
2319 * If so go try again
2320 */
2321 cpos = rbuf;
2322 cend = rbuf + siz;
2323 dp = (struct dirent *)cpos;
2324 cookiep = cookies;
2325
2326 /*
2327 * For some reason FreeBSD's ufs_readdir() chooses to back the
2328 * directory offset up to a block boundary, so it is necessary to
2329 * skip over the records that precede the requested offset. This
2330 * requires the assumption that file offset cookies monotonically
2331 * increase.
2332 */
2333 while (cpos < cend && ncookies > 0 &&
2334 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2335 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2336 cpos += dp->d_reclen;
2337 dp = (struct dirent *)cpos;
2338 cookiep++;
2339 ncookies--;
2340 }
2341 if (cpos >= cend || ncookies == 0) {
2342 siz = fullsiz;
2343 toff = off;
2344 goto again;
2345 }
2346 vput(vp);
2347
2348 /*
2349 * If cnt > MCLBYTES and the reply will not be saved, use
2350 * ext_pgs mbufs for TLS.
2351 * For NFSv4.0, we do not know for sure if the reply will
2352 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2353 */
2354 if (cnt > MCLBYTES && siz > MCLBYTES &&
2355 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2356 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2357 nd->nd_flag |= ND_EXTPG;
2358
2359 /*
2360 * dirlen is the size of the reply, including all XDR and must
2361 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2362 * if the XDR should be included in "count", but to be safe, we do.
2363 * (Include the two booleans at the end of the reply in dirlen now.)
2364 */
2365 if (nd->nd_flag & ND_NFSV3) {
2366 nfsrv_postopattr(nd, getret, &at);
2367 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2368 txdr_hyper(at.na_filerev, tl);
2369 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2370 } else {
2371 dirlen = 2 * NFSX_UNSIGNED;
2372 }
2373
2374 /* Loop through the records and build reply */
2375 while (cpos < cend && ncookies > 0) {
2376 nlen = dp->d_namlen;
2377 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2378 nlen <= NFS_MAXNAMLEN) {
2379 if (nd->nd_flag & ND_NFSV3)
2380 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2381 else
2382 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2383 if (dirlen > cnt) {
2384 eofflag = 0;
2385 break;
2386 }
2387
2388 /*
2389 * Build the directory record xdr from
2390 * the dirent entry.
2391 */
2392 if (nd->nd_flag & ND_NFSV3) {
2393 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2394 *tl++ = newnfs_true;
2395 txdr_hyper(dp->d_fileno, tl);
2396 } else {
2397 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2398 *tl++ = newnfs_true;
2399 *tl = txdr_unsigned(dp->d_fileno);
2400 }
2401 (void) nfsm_strtom(nd, dp->d_name, nlen);
2402 if (nd->nd_flag & ND_NFSV3) {
2403 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2404 txdr_hyper(*cookiep, tl);
2405 } else {
2406 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2407 *tl = txdr_unsigned(*cookiep);
2408 }
2409 }
2410 cpos += dp->d_reclen;
2411 dp = (struct dirent *)cpos;
2412 cookiep++;
2413 ncookies--;
2414 }
2415 if (cpos < cend)
2416 eofflag = 0;
2417 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2418 *tl++ = newnfs_false;
2419 if (eofflag)
2420 *tl = newnfs_true;
2421 else
2422 *tl = newnfs_false;
2423 free(rbuf, M_TEMP);
2424 free(cookies, M_TEMP);
2425
2426 out:
2427 NFSEXITCODE2(0, nd);
2428 return (0);
2429 nfsmout:
2430 vput(vp);
2431 NFSEXITCODE2(error, nd);
2432 return (error);
2433 }
2434
2435 /*
2436 * Readdirplus for V3 and Readdir for V4.
2437 */
2438 int
nfsrvd_readdirplus(struct nfsrv_descript * nd,int isdgram,struct vnode * vp,struct nfsexstuff * exp)2439 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2440 struct vnode *vp, struct nfsexstuff *exp)
2441 {
2442 struct dirent *dp;
2443 u_int32_t *tl;
2444 int dirlen;
2445 char *cpos, *cend, *rbuf;
2446 struct vnode *nvp;
2447 fhandle_t nfh;
2448 struct nfsvattr nva, at, *nvap = &nva;
2449 struct mbuf *mb0, *mb1;
2450 struct nfsreferral *refp;
2451 int nlen, r, error = 0, getret = 1, usevget = 1;
2452 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2453 caddr_t bpos0, bpos1;
2454 u_int64_t off, toff, verf __unused;
2455 uint64_t *cookies = NULL, *cookiep;
2456 nfsattrbit_t attrbits, rderrbits, savbits, refbits;
2457 struct uio io;
2458 struct iovec iv;
2459 struct componentname cn;
2460 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2461 struct mount *mp, *new_mp;
2462 uint64_t mounted_on_fileno;
2463 struct thread *p = curthread;
2464 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2465
2466 if (nd->nd_repstat) {
2467 nfsrv_postopattr(nd, getret, &at);
2468 goto out;
2469 }
2470 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2471 off = fxdr_hyper(tl);
2472 toff = off;
2473 tl += 2;
2474 verf = fxdr_hyper(tl);
2475 tl += 2;
2476 siz = fxdr_unsigned(int, *tl++);
2477 cnt = fxdr_unsigned(int, *tl);
2478
2479 /*
2480 * Use the server's maximum data transfer size as the upper bound
2481 * on reply datalen.
2482 */
2483 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2484 cnt = NFS_SRVMAXDATA(nd);
2485
2486 /*
2487 * siz is a "hint" of how much directory information (name, fileid,
2488 * cookie) should be in the reply. At least one client "hints" 0,
2489 * so I set it to cnt for that case. I also round it up to the
2490 * next multiple of DIRBLKSIZ.
2491 * Since the size of a Readdirplus directory entry reply will always
2492 * be greater than a directory entry returned by VOP_READDIR(), it
2493 * does not make sense to read more than NFS_SRVMAXDATA() via
2494 * VOP_READDIR().
2495 */
2496 if (siz <= 0)
2497 siz = cnt;
2498 else if (siz > NFS_SRVMAXDATA(nd))
2499 siz = NFS_SRVMAXDATA(nd);
2500 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2501
2502 if (nd->nd_flag & ND_NFSV4) {
2503 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2504 if (error)
2505 goto nfsmout;
2506 NFSSET_ATTRBIT(&savbits, &attrbits);
2507 NFSSET_ATTRBIT(&refbits, &attrbits);
2508 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2509 NFSZERO_ATTRBIT(&rderrbits);
2510 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2511 /*
2512 * If these 4 bits are the only attributes requested by the
2513 * client, they can be satisfied without acquiring the vnode
2514 * for the file object unless it is a directory.
2515 * This will be indicated by savbits being all 0s.
2516 */
2517 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_TYPE);
2518 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_FILEID);
2519 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_MOUNTEDONFILEID);
2520 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_RDATTRERROR);
2521 } else {
2522 NFSZERO_ATTRBIT(&attrbits);
2523 }
2524 fullsiz = siz;
2525 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2526 #if 0
2527 if (!nd->nd_repstat) {
2528 if (off && verf != at.na_filerev) {
2529 /*
2530 * va_filerev is not sufficient as a cookie verifier,
2531 * since it is not supposed to change when entries are
2532 * removed/added unless that offset cookies returned to
2533 * the client are no longer valid.
2534 */
2535 if (nd->nd_flag & ND_NFSV4) {
2536 nd->nd_repstat = NFSERR_NOTSAME;
2537 } else {
2538 nd->nd_repstat = NFSERR_BAD_COOKIE;
2539 }
2540 }
2541 }
2542 #endif
2543 if (!nd->nd_repstat && vp->v_type != VDIR)
2544 nd->nd_repstat = NFSERR_NOTDIR;
2545 if (!nd->nd_repstat && cnt == 0)
2546 nd->nd_repstat = NFSERR_TOOSMALL;
2547 if (!nd->nd_repstat)
2548 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2549 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2550 NFSACCCHK_VPISLOCKED, NULL);
2551 if (nd->nd_repstat) {
2552 vput(vp);
2553 if (nd->nd_flag & ND_NFSV3)
2554 nfsrv_postopattr(nd, getret, &at);
2555 goto out;
2556 }
2557 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2558 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2559
2560 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2561 again:
2562 eofflag = 0;
2563 if (cookies) {
2564 free(cookies, M_TEMP);
2565 cookies = NULL;
2566 }
2567
2568 iv.iov_base = rbuf;
2569 iv.iov_len = siz;
2570 io.uio_iov = &iv;
2571 io.uio_iovcnt = 1;
2572 io.uio_offset = (off_t)off;
2573 io.uio_resid = siz;
2574 io.uio_segflg = UIO_SYSSPACE;
2575 io.uio_rw = UIO_READ;
2576 io.uio_td = NULL;
2577 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2578 &cookies);
2579 off = (u_int64_t)io.uio_offset;
2580 if (io.uio_resid)
2581 siz -= io.uio_resid;
2582
2583 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2584
2585 if (!cookies && !nd->nd_repstat)
2586 nd->nd_repstat = NFSERR_PERM;
2587 if (!nd->nd_repstat)
2588 nd->nd_repstat = getret;
2589 if (nd->nd_repstat) {
2590 vput(vp);
2591 if (cookies)
2592 free(cookies, M_TEMP);
2593 free(rbuf, M_TEMP);
2594 if (nd->nd_flag & ND_NFSV3)
2595 nfsrv_postopattr(nd, getret, &at);
2596 goto out;
2597 }
2598 /*
2599 * If nothing read, return eof
2600 * rpc reply
2601 */
2602 if (siz == 0) {
2603 vput(vp);
2604 if (nd->nd_flag & ND_NFSV3)
2605 nfsrv_postopattr(nd, getret, &at);
2606 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2607 txdr_hyper(at.na_filerev, tl);
2608 tl += 2;
2609 *tl++ = newnfs_false;
2610 *tl = newnfs_true;
2611 free(cookies, M_TEMP);
2612 free(rbuf, M_TEMP);
2613 goto out;
2614 }
2615
2616 /*
2617 * Check for degenerate cases of nothing useful read.
2618 * If so go try again
2619 */
2620 cpos = rbuf;
2621 cend = rbuf + siz;
2622 dp = (struct dirent *)cpos;
2623 cookiep = cookies;
2624
2625 /*
2626 * For some reason FreeBSD's ufs_readdir() chooses to back the
2627 * directory offset up to a block boundary, so it is necessary to
2628 * skip over the records that precede the requested offset. This
2629 * requires the assumption that file offset cookies monotonically
2630 * increase.
2631 */
2632 while (cpos < cend && ncookies > 0 &&
2633 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2634 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2635 ((nd->nd_flag & ND_NFSV4) &&
2636 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2637 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2638 cpos += dp->d_reclen;
2639 dp = (struct dirent *)cpos;
2640 cookiep++;
2641 ncookies--;
2642 }
2643 if (cpos >= cend || ncookies == 0) {
2644 siz = fullsiz;
2645 toff = off;
2646 goto again;
2647 }
2648
2649 /*
2650 * Busy the file system so that the mount point won't go away
2651 * and, as such, VFS_VGET() can be used safely.
2652 */
2653 mp = vp->v_mount;
2654 vfs_ref(mp);
2655 NFSVOPUNLOCK(vp);
2656 nd->nd_repstat = vfs_busy(mp, 0);
2657 vfs_rel(mp);
2658 if (nd->nd_repstat != 0) {
2659 vrele(vp);
2660 free(cookies, M_TEMP);
2661 free(rbuf, M_TEMP);
2662 if (nd->nd_flag & ND_NFSV3)
2663 nfsrv_postopattr(nd, getret, &at);
2664 goto out;
2665 }
2666
2667 /*
2668 * Check to see if entries in this directory can be safely acquired
2669 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2670 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2671 * automount of the snapshot directory that is required will
2672 * be done.
2673 * This needs to be done here for NFSv4, since NFSv4 never does
2674 * a VFS_VGET() for "." or "..".
2675 */
2676 if (is_zfs == 1) {
2677 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2678 if (r == EOPNOTSUPP) {
2679 usevget = 0;
2680 cn.cn_nameiop = LOOKUP;
2681 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2682 cn.cn_cred = nd->nd_cred;
2683 } else if (r == 0)
2684 vput(nvp);
2685 }
2686
2687 /*
2688 * If the reply is likely to exceed MCLBYTES and the reply will
2689 * not be saved, use ext_pgs mbufs for TLS.
2690 * It is difficult to predict how large each entry will be and
2691 * how many entries have been read, so just assume the directory
2692 * entries grow by a factor of 4 when attributes are included.
2693 * For NFSv4.0, we do not know for sure if the reply will
2694 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2695 */
2696 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2697 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2698 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2699 nd->nd_flag |= ND_EXTPG;
2700
2701 /*
2702 * Save this position, in case there is an error before one entry
2703 * is created.
2704 */
2705 mb0 = nd->nd_mb;
2706 bpos0 = nd->nd_bpos;
2707 bextpg0 = nd->nd_bextpg;
2708 bextpgsiz0 = nd->nd_bextpgsiz;
2709
2710 /*
2711 * Fill in the first part of the reply.
2712 * dirlen is the reply length in bytes and cannot exceed cnt.
2713 * (Include the two booleans at the end of the reply in dirlen now,
2714 * so we recognize when we have exceeded cnt.)
2715 */
2716 if (nd->nd_flag & ND_NFSV3) {
2717 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2718 nfsrv_postopattr(nd, getret, &at);
2719 } else {
2720 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2721 }
2722 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2723 txdr_hyper(at.na_filerev, tl);
2724
2725 /*
2726 * Save this position, in case there is an empty reply needed.
2727 */
2728 mb1 = nd->nd_mb;
2729 bpos1 = nd->nd_bpos;
2730 bextpg1 = nd->nd_bextpg;
2731 bextpgsiz1 = nd->nd_bextpgsiz;
2732
2733 /* Loop through the records and build reply */
2734 entrycnt = 0;
2735 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2736 nlen = dp->d_namlen;
2737 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2738 nlen <= NFS_MAXNAMLEN &&
2739 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2740 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2741 || (nlen == 1 && dp->d_name[0] != '.'))) {
2742 /*
2743 * Save the current position in the reply, in case
2744 * this entry exceeds cnt.
2745 */
2746 mb1 = nd->nd_mb;
2747 bpos1 = nd->nd_bpos;
2748 bextpg1 = nd->nd_bextpg;
2749 bextpgsiz1 = nd->nd_bextpgsiz;
2750
2751 /*
2752 * For readdir_and_lookup get the vnode using
2753 * the file number.
2754 */
2755 nvp = NULL;
2756 refp = NULL;
2757 r = 0;
2758 at_root = 0;
2759 needs_unbusy = 0;
2760 new_mp = mp;
2761 mounted_on_fileno = (uint64_t)dp->d_fileno;
2762 if ((nd->nd_flag & ND_NFSV3) ||
2763 NFSNONZERO_ATTRBIT(&savbits) ||
2764 dp->d_type == DT_UNKNOWN ||
2765 (dp->d_type == DT_DIR &&
2766 nfsrv_enable_crossmntpt != 0)) {
2767 if (nd->nd_flag & ND_NFSV4)
2768 refp = nfsv4root_getreferral(NULL,
2769 vp, dp->d_fileno);
2770 if (refp == NULL) {
2771 if (usevget)
2772 r = VFS_VGET(mp, dp->d_fileno,
2773 LK_SHARED, &nvp);
2774 else
2775 r = EOPNOTSUPP;
2776 if (r == 0 && (vn_irflag_read(vp) &
2777 VIRF_NAMEDDIR) != 0)
2778 vn_irflag_set_cond(nvp,
2779 VIRF_NAMEDATTR);
2780 if (r == EOPNOTSUPP) {
2781 if (usevget) {
2782 usevget = 0;
2783 cn.cn_nameiop = LOOKUP;
2784 cn.cn_lkflags =
2785 LK_SHARED |
2786 LK_RETRY;
2787 cn.cn_cred =
2788 nd->nd_cred;
2789 }
2790 cn.cn_nameptr = dp->d_name;
2791 cn.cn_namelen = nlen;
2792 cn.cn_flags = ISLASTCN |
2793 NOFOLLOW | LOCKLEAF;
2794 if ((vn_irflag_read(vp) &
2795 VIRF_NAMEDDIR) != 0)
2796 cn.cn_flags |=
2797 OPENNAMED;
2798 if (nlen == 2 &&
2799 dp->d_name[0] == '.' &&
2800 dp->d_name[1] == '.')
2801 cn.cn_flags |=
2802 ISDOTDOT;
2803 if (NFSVOPLOCK(vp, LK_SHARED)
2804 != 0) {
2805 nd->nd_repstat = EPERM;
2806 break;
2807 }
2808 if ((vp->v_vflag & VV_ROOT) != 0
2809 && (cn.cn_flags & ISDOTDOT)
2810 != 0) {
2811 vref(vp);
2812 nvp = vp;
2813 r = 0;
2814 } else {
2815 r = VOP_LOOKUP(vp, &nvp,
2816 &cn);
2817 if (vp != nvp)
2818 NFSVOPUNLOCK(vp);
2819 }
2820 }
2821
2822 /*
2823 * For NFSv4, check to see if nvp is
2824 * a mount point and get the mount
2825 * point vnode, as required.
2826 */
2827 if (r == 0 &&
2828 nfsrv_enable_crossmntpt != 0 &&
2829 (nd->nd_flag & ND_NFSV4) != 0 &&
2830 nvp->v_type == VDIR &&
2831 nvp->v_mountedhere != NULL) {
2832 new_mp = nvp->v_mountedhere;
2833 r = vfs_busy(new_mp, 0);
2834 vput(nvp);
2835 nvp = NULL;
2836 if (r == 0) {
2837 r = VFS_ROOT(new_mp,
2838 LK_SHARED, &nvp);
2839 needs_unbusy = 1;
2840 if (r == 0)
2841 at_root = 1;
2842 }
2843 }
2844 }
2845
2846 /*
2847 * If we failed to look up the entry, then it
2848 * has become invalid, most likely removed.
2849 */
2850 if (r != 0) {
2851 if (needs_unbusy)
2852 vfs_unbusy(new_mp);
2853 goto invalid;
2854 }
2855 KASSERT(refp != NULL || nvp != NULL,
2856 ("%s: undetected lookup error", __func__));
2857
2858 if (refp == NULL &&
2859 ((nd->nd_flag & ND_NFSV3) ||
2860 NFSNONZERO_ATTRBIT(&attrbits))) {
2861 r = nfsvno_getfh(nvp, &nfh, p);
2862 if (!r)
2863 r = nfsvno_getattr(nvp, nvap, nd, p,
2864 1, &attrbits);
2865 if (r == 0 && is_zfs == 1 &&
2866 nfsrv_enable_crossmntpt != 0 &&
2867 (nd->nd_flag & ND_NFSV4) != 0 &&
2868 nvp->v_type == VDIR &&
2869 vp->v_mount != nvp->v_mount) {
2870 /*
2871 * For a ZFS snapshot, there is a
2872 * pseudo mount that does not set
2873 * v_mountedhere, so it needs to
2874 * be detected via a different
2875 * mount structure.
2876 */
2877 at_root = 1;
2878 if (new_mp == mp)
2879 new_mp = nvp->v_mount;
2880 }
2881 }
2882
2883 /*
2884 * If we failed to get attributes of the entry,
2885 * then just skip it for NFSv3 (the traditional
2886 * behavior in the old NFS server).
2887 * For NFSv4 the behavior is controlled by
2888 * RDATTRERROR: we either ignore the error or
2889 * fail the request.
2890 * The exception is EOPNOTSUPP, which can be
2891 * returned by nfsvno_getfh() for certain
2892 * file systems, such as devfs. This indicates
2893 * that the file system cannot be exported,
2894 * so just skip over the entry.
2895 * Note that RDATTRERROR is never set for NFSv3.
2896 */
2897 if (r != 0) {
2898 if (!NFSISSET_ATTRBIT(&attrbits,
2899 NFSATTRBIT_RDATTRERROR) ||
2900 r == EOPNOTSUPP) {
2901 vput(nvp);
2902 if (needs_unbusy != 0)
2903 vfs_unbusy(new_mp);
2904 if ((nd->nd_flag & ND_NFSV3) ||
2905 r == EOPNOTSUPP)
2906 goto invalid;
2907 nd->nd_repstat = r;
2908 break;
2909 }
2910 }
2911 } else if (NFSNONZERO_ATTRBIT(&attrbits)) {
2912 /* Only need Type and/or Fileid. */
2913 VATTR_NULL(&nvap->na_vattr);
2914 nvap->na_fileid = dp->d_fileno;
2915 nfs_dtypetovtype(nvap, vp, dp->d_type);
2916 }
2917
2918 /*
2919 * Build the directory record xdr
2920 */
2921 if (nd->nd_flag & ND_NFSV3) {
2922 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2923 *tl++ = newnfs_true;
2924 txdr_hyper(dp->d_fileno, tl);
2925 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2926 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2927 txdr_hyper(*cookiep, tl);
2928 nfsrv_postopattr(nd, 0, nvap);
2929 dirlen += nfsm_fhtom(NULL, nd, (u_int8_t *)&nfh,
2930 0, 1);
2931 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2932 if (nvp != NULL)
2933 vput(nvp);
2934 } else {
2935 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2936 *tl++ = newnfs_true;
2937 txdr_hyper(*cookiep, tl);
2938 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2939 if (nvp != NULL) {
2940 supports_nfsv4acls =
2941 nfs_supportsnfsv4acls(nvp);
2942 NFSVOPUNLOCK(nvp);
2943 } else
2944 supports_nfsv4acls = 0;
2945 if (refp != NULL) {
2946 dirlen += nfsrv_putreferralattr(nd,
2947 &refbits, refp, 0,
2948 &nd->nd_repstat);
2949 if (nd->nd_repstat) {
2950 if (nvp != NULL)
2951 vrele(nvp);
2952 if (needs_unbusy != 0)
2953 vfs_unbusy(new_mp);
2954 break;
2955 }
2956 } else if (r) {
2957 dirlen += nfsvno_fillattr(nd, new_mp,
2958 nvp, nvap, &nfh, r, &rderrbits,
2959 nd->nd_cred, p, isdgram, 0,
2960 supports_nfsv4acls, at_root,
2961 mounted_on_fileno);
2962 } else {
2963 dirlen += nfsvno_fillattr(nd, new_mp,
2964 nvp, nvap, &nfh, r, &attrbits,
2965 nd->nd_cred, p, isdgram, 0,
2966 supports_nfsv4acls, at_root,
2967 mounted_on_fileno);
2968 }
2969 if (nvp != NULL)
2970 vrele(nvp);
2971 dirlen += (3 * NFSX_UNSIGNED);
2972 }
2973 if (needs_unbusy != 0)
2974 vfs_unbusy(new_mp);
2975 if (dirlen <= cnt)
2976 entrycnt++;
2977 }
2978 invalid:
2979 cpos += dp->d_reclen;
2980 dp = (struct dirent *)cpos;
2981 cookiep++;
2982 ncookies--;
2983 }
2984 vrele(vp);
2985 vfs_unbusy(mp);
2986
2987 /*
2988 * If dirlen > cnt, we must strip off the last entry. If that
2989 * results in an empty reply, report NFSERR_TOOSMALL.
2990 */
2991 if (dirlen > cnt || nd->nd_repstat) {
2992 if (!nd->nd_repstat && entrycnt == 0)
2993 nd->nd_repstat = NFSERR_TOOSMALL;
2994 if (nd->nd_repstat) {
2995 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2996 if (nd->nd_flag & ND_NFSV3)
2997 nfsrv_postopattr(nd, getret, &at);
2998 } else
2999 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
3000 eofflag = 0;
3001 } else if (cpos < cend)
3002 eofflag = 0;
3003 if (!nd->nd_repstat) {
3004 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3005 *tl++ = newnfs_false;
3006 if (eofflag)
3007 *tl = newnfs_true;
3008 else
3009 *tl = newnfs_false;
3010 }
3011 free(cookies, M_TEMP);
3012 free(rbuf, M_TEMP);
3013
3014 out:
3015 NFSEXITCODE2(0, nd);
3016 return (0);
3017 nfsmout:
3018 vput(vp);
3019 NFSEXITCODE2(error, nd);
3020 return (error);
3021 }
3022
3023 /*
3024 * Get the settable attributes out of the mbuf list.
3025 * (Return 0 or EBADRPC)
3026 */
3027 int
nfsrv_sattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSACL_T * aclp,struct thread * p)3028 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
3029 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
3030 {
3031 u_int32_t *tl;
3032 struct nfsv2_sattr *sp;
3033 int error = 0, toclient = 0;
3034
3035 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
3036 case ND_NFSV2:
3037 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
3038 /*
3039 * Some old clients didn't fill in the high order 16bits.
3040 * --> check the low order 2 bytes for 0xffff
3041 */
3042 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
3043 nvap->na_mode = nfstov_mode(sp->sa_mode);
3044 if (sp->sa_uid != newnfs_xdrneg1)
3045 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
3046 if (sp->sa_gid != newnfs_xdrneg1)
3047 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
3048 if (sp->sa_size != newnfs_xdrneg1)
3049 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
3050 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
3051 #ifdef notyet
3052 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
3053 #else
3054 nvap->na_atime.tv_sec =
3055 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
3056 nvap->na_atime.tv_nsec = 0;
3057 #endif
3058 }
3059 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
3060 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
3061 break;
3062 case ND_NFSV3:
3063 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3064 if (*tl == newnfs_true) {
3065 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3066 nvap->na_mode = nfstov_mode(*tl);
3067 }
3068 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3069 if (*tl == newnfs_true) {
3070 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3071 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
3072 }
3073 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3074 if (*tl == newnfs_true) {
3075 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3076 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
3077 }
3078 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3079 if (*tl == newnfs_true) {
3080 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3081 nvap->na_size = fxdr_hyper(tl);
3082 }
3083 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3084 switch (fxdr_unsigned(int, *tl)) {
3085 case NFSV3SATTRTIME_TOCLIENT:
3086 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3087 fxdr_nfsv3time(tl, &nvap->na_atime);
3088 toclient = 1;
3089 break;
3090 case NFSV3SATTRTIME_TOSERVER:
3091 vfs_timestamp(&nvap->na_atime);
3092 nvap->na_vaflags |= VA_UTIMES_NULL;
3093 break;
3094 }
3095 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3096 switch (fxdr_unsigned(int, *tl)) {
3097 case NFSV3SATTRTIME_TOCLIENT:
3098 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3099 fxdr_nfsv3time(tl, &nvap->na_mtime);
3100 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3101 break;
3102 case NFSV3SATTRTIME_TOSERVER:
3103 vfs_timestamp(&nvap->na_mtime);
3104 if (!toclient)
3105 nvap->na_vaflags |= VA_UTIMES_NULL;
3106 break;
3107 }
3108 break;
3109 case ND_NFSV4:
3110 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
3111 }
3112 nfsmout:
3113 NFSEXITCODE2(error, nd);
3114 return (error);
3115 }
3116
3117 /*
3118 * Handle the setable attributes for V4.
3119 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
3120 */
3121 int
nfsv4_sattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSACL_T * aclp,struct thread * p)3122 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
3123 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
3124 {
3125 u_int32_t *tl;
3126 int attrsum = 0;
3127 int i, j;
3128 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
3129 int moderet, toclient = 0;
3130 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
3131 uid_t uid;
3132 gid_t gid;
3133 u_short mode, mask; /* Same type as va_mode. */
3134 struct vattr va;
3135
3136 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
3137 if (error)
3138 goto nfsmout;
3139 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3140 attrsize = fxdr_unsigned(int, *tl);
3141
3142 /*
3143 * Loop around getting the setable attributes. If an unsupported
3144 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
3145 * Once nd_repstat != 0, do not set the attribute value, but keep
3146 * parsing the attribute(s).
3147 */
3148 if (retnotsup) {
3149 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3150 bitpos = NFSATTRBIT_MAX;
3151 } else {
3152 bitpos = 0;
3153 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_HIDDEN) ||
3154 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SYSTEM))
3155 nvap->na_flags = 0;
3156 }
3157 moderet = 0;
3158 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
3159 if (attrsum > attrsize) {
3160 error = NFSERR_BADXDR;
3161 goto nfsmout;
3162 }
3163 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
3164 switch (bitpos) {
3165 case NFSATTRBIT_SIZE:
3166 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3167 if (!nd->nd_repstat) {
3168 if (vp != NULL && vp->v_type != VREG)
3169 nd->nd_repstat = (vp->v_type == VDIR) ?
3170 NFSERR_ISDIR : NFSERR_INVAL;
3171 else
3172 nvap->na_size = fxdr_hyper(tl);
3173 }
3174 attrsum += NFSX_HYPER;
3175 break;
3176 case NFSATTRBIT_ACL:
3177 error = nfsrv_dissectacl(nd, aclp, true, &aceerr,
3178 &aclsize, p);
3179 if (error)
3180 goto nfsmout;
3181 if (aceerr && !nd->nd_repstat)
3182 nd->nd_repstat = aceerr;
3183 attrsum += aclsize;
3184 break;
3185 case NFSATTRBIT_ARCHIVE:
3186 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3187 if (!nd->nd_repstat)
3188 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3189 attrsum += NFSX_UNSIGNED;
3190 break;
3191 case NFSATTRBIT_HIDDEN:
3192 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3193 if (nd->nd_repstat == 0) {
3194 if (*tl == newnfs_true)
3195 nvap->na_flags |= UF_HIDDEN;
3196 }
3197 attrsum += NFSX_UNSIGNED;
3198 break;
3199 case NFSATTRBIT_MIMETYPE:
3200 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3201 i = fxdr_unsigned(int, *tl);
3202 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3203 if (error)
3204 goto nfsmout;
3205 if (!nd->nd_repstat)
3206 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3207 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3208 break;
3209 case NFSATTRBIT_MODE:
3210 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
3211 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3212 if (!nd->nd_repstat)
3213 nvap->na_mode = nfstov_mode(*tl);
3214 attrsum += NFSX_UNSIGNED;
3215 break;
3216 case NFSATTRBIT_OWNER:
3217 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3218 j = fxdr_unsigned(int, *tl);
3219 if (j < 0) {
3220 error = NFSERR_BADXDR;
3221 goto nfsmout;
3222 }
3223 if (j > NFSV4_SMALLSTR)
3224 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3225 else
3226 cp = namestr;
3227 error = nfsrv_mtostr(nd, cp, j);
3228 if (error) {
3229 if (j > NFSV4_SMALLSTR)
3230 free(cp, M_NFSSTRING);
3231 goto nfsmout;
3232 }
3233 if (!nd->nd_repstat) {
3234 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3235 &uid);
3236 if (!nd->nd_repstat)
3237 nvap->na_uid = uid;
3238 }
3239 if (j > NFSV4_SMALLSTR)
3240 free(cp, M_NFSSTRING);
3241 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3242 break;
3243 case NFSATTRBIT_OWNERGROUP:
3244 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3245 j = fxdr_unsigned(int, *tl);
3246 if (j < 0) {
3247 error = NFSERR_BADXDR;
3248 goto nfsmout;
3249 }
3250 if (j > NFSV4_SMALLSTR)
3251 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3252 else
3253 cp = namestr;
3254 error = nfsrv_mtostr(nd, cp, j);
3255 if (error) {
3256 if (j > NFSV4_SMALLSTR)
3257 free(cp, M_NFSSTRING);
3258 goto nfsmout;
3259 }
3260 if (!nd->nd_repstat) {
3261 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3262 &gid);
3263 if (!nd->nd_repstat)
3264 nvap->na_gid = gid;
3265 }
3266 if (j > NFSV4_SMALLSTR)
3267 free(cp, M_NFSSTRING);
3268 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3269 break;
3270 case NFSATTRBIT_SYSTEM:
3271 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
3272 if (nd->nd_repstat == 0) {
3273 if (*tl == newnfs_true)
3274 nvap->na_flags |= UF_SYSTEM;
3275 }
3276 attrsum += NFSX_UNSIGNED;
3277 break;
3278 case NFSATTRBIT_TIMEACCESSSET:
3279 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3280 attrsum += NFSX_UNSIGNED;
3281 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3282 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3283 if (!nd->nd_repstat)
3284 fxdr_nfsv4time(tl, &nvap->na_atime);
3285 toclient = 1;
3286 attrsum += NFSX_V4TIME;
3287 } else if (!nd->nd_repstat) {
3288 vfs_timestamp(&nvap->na_atime);
3289 nvap->na_vaflags |= VA_UTIMES_NULL;
3290 }
3291 break;
3292 case NFSATTRBIT_TIMEBACKUP:
3293 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3294 if (!nd->nd_repstat)
3295 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3296 attrsum += NFSX_V4TIME;
3297 break;
3298 case NFSATTRBIT_TIMECREATE:
3299 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3300 if (!nd->nd_repstat)
3301 fxdr_nfsv4time(tl, &nvap->na_btime);
3302 attrsum += NFSX_V4TIME;
3303 break;
3304 case NFSATTRBIT_TIMEMODIFYSET:
3305 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3306 attrsum += NFSX_UNSIGNED;
3307 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3308 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3309 if (!nd->nd_repstat)
3310 fxdr_nfsv4time(tl, &nvap->na_mtime);
3311 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3312 attrsum += NFSX_V4TIME;
3313 } else if (!nd->nd_repstat) {
3314 vfs_timestamp(&nvap->na_mtime);
3315 if (!toclient)
3316 nvap->na_vaflags |= VA_UTIMES_NULL;
3317 }
3318 break;
3319 case NFSATTRBIT_MODESETMASKED:
3320 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3321 mode = fxdr_unsigned(u_short, *tl++);
3322 mask = fxdr_unsigned(u_short, *tl);
3323 /*
3324 * vp == NULL implies an Open/Create operation.
3325 * This attribute can only be used for Setattr and
3326 * only for NFSv4.1 or higher.
3327 * If moderet != 0, a mode attribute has also been
3328 * specified and this attribute cannot be done in the
3329 * same Setattr operation.
3330 */
3331 if (!nd->nd_repstat) {
3332 if ((nd->nd_flag & ND_NFSV41) == 0)
3333 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3334 else if ((mode & ~07777) != 0 ||
3335 (mask & ~07777) != 0 || vp == NULL)
3336 nd->nd_repstat = NFSERR_INVAL;
3337 else if (moderet == 0)
3338 moderet = VOP_GETATTR(vp, &va,
3339 nd->nd_cred);
3340 if (moderet == 0)
3341 nvap->na_mode = (mode & mask) |
3342 (va.va_mode & ~mask);
3343 else
3344 nd->nd_repstat = moderet;
3345 }
3346 attrsum += 2 * NFSX_UNSIGNED;
3347 break;
3348 case NFSATTRBIT_MODEUMASK:
3349 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3350 mode = fxdr_unsigned(u_short, *tl++);
3351 mask = fxdr_unsigned(u_short, *tl);
3352 /*
3353 * If moderet != 0, mode has already been done.
3354 * If vp != NULL, this is not a file object creation.
3355 */
3356 if (!nd->nd_repstat) {
3357 if ((nd->nd_flag & ND_NFSV42) == 0)
3358 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3359 else if ((mask & ~0777) != 0 || vp != NULL ||
3360 moderet != 0)
3361 nd->nd_repstat = NFSERR_INVAL;
3362 else
3363 nvap->na_mode = (mode & ~mask);
3364 }
3365 attrsum += 2 * NFSX_UNSIGNED;
3366 break;
3367 default:
3368 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3369 /*
3370 * set bitpos so we drop out of the loop.
3371 */
3372 bitpos = NFSATTRBIT_MAX;
3373 break;
3374 }
3375 }
3376
3377 /*
3378 * some clients pad the attrlist, so we need to skip over the
3379 * padding. This also skips over unparsed non-supported attributes.
3380 */
3381 if (attrsum > attrsize) {
3382 error = NFSERR_BADXDR;
3383 } else {
3384 attrsize = NFSM_RNDUP(attrsize);
3385 if (attrsum < attrsize)
3386 error = nfsm_advance(nd, attrsize - attrsum, -1);
3387 }
3388 nfsmout:
3389 NFSEXITCODE2(error, nd);
3390 return (error);
3391 }
3392
3393 /*
3394 * Check/setup export credentials.
3395 */
3396 int
nfsd_excred(struct nfsrv_descript * nd,struct nfsexstuff * exp,struct ucred * credanon,bool testsec)3397 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3398 struct ucred *credanon, bool testsec)
3399 {
3400 int error;
3401
3402 /*
3403 * Check/setup credentials.
3404 */
3405 if (nd->nd_flag & ND_GSS)
3406 exp->nes_exflag &= ~MNT_EXPORTANON;
3407
3408 /*
3409 * Check to see if the operation is allowed for this security flavor.
3410 */
3411 error = 0;
3412 if (testsec) {
3413 error = nfsvno_testexp(nd, exp);
3414 if (error != 0)
3415 goto out;
3416 }
3417
3418 /*
3419 * Check to see if the file system is exported V4 only.
3420 */
3421 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3422 error = NFSERR_PROGNOTV4;
3423 goto out;
3424 }
3425
3426 /*
3427 * Now, map the user credentials.
3428 * (Note that ND_AUTHNONE will only be set for an NFSv3
3429 * Fsinfo RPC. If set for anything else, this code might need
3430 * to change.)
3431 */
3432 if (NFSVNO_EXPORTED(exp)) {
3433 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3434 NFSVNO_EXPORTANON(exp) ||
3435 (nd->nd_flag & ND_AUTHNONE) != 0) {
3436 nd->nd_cred->cr_uid = credanon->cr_uid;
3437 /*
3438 * 'credanon' is already a 'struct ucred' that was built
3439 * internally with calls to crsetgroups_fallback(), so
3440 * we don't need a fallback here.
3441 */
3442 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3443 credanon->cr_groups);
3444 } else if ((nd->nd_flag & ND_GSS) == 0) {
3445 /*
3446 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3447 * if there is a replacement credential with a group
3448 * list set up by "nfsuserd -manage-gids".
3449 * If there is no replacement, nfsrv_getgrpscred()
3450 * simply returns its argument.
3451 */
3452 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3453 }
3454 }
3455
3456 out:
3457 NFSEXITCODE2(error, nd);
3458 return (error);
3459 }
3460
3461 /*
3462 * Check exports.
3463 */
3464 int
nfsvno_checkexp(struct mount * mp,struct sockaddr * nam,struct nfsexstuff * exp,struct ucred ** credp)3465 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3466 struct ucred **credp)
3467 {
3468 int error;
3469
3470 error = 0;
3471 *credp = NULL;
3472 MNT_ILOCK(mp);
3473 if (mp->mnt_exjail == NULL ||
3474 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3475 error = EACCES;
3476 MNT_IUNLOCK(mp);
3477 if (error == 0)
3478 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3479 &exp->nes_numsecflavor, exp->nes_secflavors);
3480 if (error) {
3481 if (NFSD_VNET(nfs_rootfhset)) {
3482 exp->nes_exflag = 0;
3483 exp->nes_numsecflavor = 0;
3484 error = 0;
3485 }
3486 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3487 MAXSECFLAVORS) {
3488 printf("nfsvno_checkexp: numsecflavors out of range\n");
3489 exp->nes_numsecflavor = 0;
3490 error = EACCES;
3491 }
3492 NFSEXITCODE(error);
3493 return (error);
3494 }
3495
3496 /*
3497 * Get a vnode for a file handle and export stuff.
3498 */
3499 int
nfsvno_fhtovp(struct mount * mp,fhandle_t * fhp,struct sockaddr * nam,int lktype,struct vnode ** vpp,struct nfsexstuff * exp,struct ucred ** credp)3500 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3501 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3502 struct ucred **credp)
3503 {
3504 int error;
3505
3506 *credp = NULL;
3507 exp->nes_numsecflavor = 0;
3508 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3509 if (error != 0)
3510 /* Make sure the server replies ESTALE to the client. */
3511 error = ESTALE;
3512 if (nam && !error) {
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 } else {
3527 vput(*vpp);
3528 }
3529 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3530 MAXSECFLAVORS) {
3531 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3532 exp->nes_numsecflavor = 0;
3533 error = EACCES;
3534 vput(*vpp);
3535 }
3536 }
3537 NFSEXITCODE(error);
3538 return (error);
3539 }
3540
3541 /*
3542 * nfsd_fhtovp() - convert a fh to a vnode ptr
3543 * - look up fsid in mount list (if not found ret error)
3544 * - get vp and export rights by calling nfsvno_fhtovp()
3545 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3546 * for AUTH_SYS
3547 * - if mpp != NULL, return the mount point so that it can
3548 * be used for vn_finished_write() by the caller
3549 */
3550 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)3551 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3552 struct vnode **vpp, struct nfsexstuff *exp,
3553 struct mount **mpp, int startwrite, int nextop)
3554 {
3555 struct mount *mp, *mpw;
3556 struct ucred *credanon;
3557 fhandle_t *fhp;
3558 int error;
3559
3560 if (mpp != NULL)
3561 *mpp = NULL;
3562 *vpp = NULL;
3563 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3564 mp = vfs_busyfs(&fhp->fh_fsid);
3565 if (mp == NULL) {
3566 nd->nd_repstat = ESTALE;
3567 goto out;
3568 }
3569
3570 if (startwrite) {
3571 mpw = mp;
3572 error = vn_start_write(NULL, &mpw, V_WAIT);
3573 if (error != 0) {
3574 mpw = NULL;
3575 vfs_unbusy(mp);
3576 nd->nd_repstat = ESTALE;
3577 goto out;
3578 }
3579 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3580 lktype = LK_EXCLUSIVE;
3581 } else
3582 mpw = NULL;
3583
3584 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3585 &credanon);
3586 vfs_unbusy(mp);
3587
3588 if (nd->nd_repstat == 0 &&
3589 nfp->nfsrvfh_len >= NFSX_MYFH + NFSX_V4NAMEDDIRFH &&
3590 nfp->nfsrvfh_len <= NFSX_MYFH + NFSX_V4NAMEDATTRFH) {
3591 if (nfp->nfsrvfh_len == NFSX_MYFH + NFSX_V4NAMEDDIRFH)
3592 vn_irflag_set_cond(*vpp, VIRF_NAMEDDIR);
3593 else
3594 vn_irflag_set_cond(*vpp, VIRF_NAMEDATTR);
3595 }
3596
3597 /*
3598 * For NFSv4 without a pseudo root fs, unexported file handles
3599 * can be returned, so that Lookup works everywhere.
3600 */
3601 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3602 !(nd->nd_flag & ND_NFSV4)) {
3603 vput(*vpp);
3604 *vpp = NULL;
3605 nd->nd_repstat = EACCES;
3606 }
3607
3608 /*
3609 * Personally, I've never seen any point in requiring a
3610 * reserved port#, since only in the rare case where the
3611 * clients are all boxes with secure system privileges,
3612 * does it provide any enhanced security, but... some people
3613 * believe it to be useful and keep putting this code back in.
3614 * (There is also some "security checker" out there that
3615 * complains if the nfs server doesn't enforce this.)
3616 * However, note the following:
3617 * RFC3530 (NFSv4) specifies that a reserved port# not be
3618 * required.
3619 * RFC2623 recommends that, if a reserved port# is checked for,
3620 * that there be a way to turn that off--> ifdef'd.
3621 */
3622 #ifdef NFS_REQRSVPORT
3623 if (!nd->nd_repstat) {
3624 struct sockaddr_in *saddr;
3625 struct sockaddr_in6 *saddr6;
3626
3627 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3628 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3629 if (!(nd->nd_flag & ND_NFSV4) &&
3630 ((saddr->sin_family == AF_INET &&
3631 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3632 (saddr6->sin6_family == AF_INET6 &&
3633 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3634 vput(*vpp);
3635 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3636 }
3637 }
3638 #endif /* NFS_REQRSVPORT */
3639
3640 /*
3641 * Check/setup credentials.
3642 */
3643 if (!nd->nd_repstat) {
3644 nd->nd_saveduid = nd->nd_cred->cr_uid;
3645 nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3646 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3647 if (nd->nd_repstat)
3648 vput(*vpp);
3649 }
3650 if (credanon != NULL)
3651 crfree(credanon);
3652 if (nd->nd_repstat) {
3653 vn_finished_write(mpw);
3654 *vpp = NULL;
3655 } else if (mpp != NULL) {
3656 *mpp = mpw;
3657 }
3658
3659 out:
3660 NFSEXITCODE2(0, nd);
3661 }
3662
3663 /*
3664 * glue for fp.
3665 */
3666 static int
fp_getfvp(struct thread * p,int fd,struct file ** fpp,struct vnode ** vpp)3667 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3668 {
3669 struct filedesc *fdp;
3670 struct file *fp;
3671 int error = 0;
3672
3673 fdp = p->td_proc->p_fd;
3674 if (fd < 0 || fd >= fdp->fd_nfiles ||
3675 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3676 error = EBADF;
3677 goto out;
3678 }
3679 *fpp = fp;
3680
3681 out:
3682 NFSEXITCODE(error);
3683 return (error);
3684 }
3685
3686 /*
3687 * Called from nfssvc() to update the exports list. Just call
3688 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3689 * in the mount list.
3690 */
3691 int
nfsrv_v4rootexport(void * argp,struct ucred * cred,struct thread * p)3692 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3693 {
3694 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3695 int error = 0;
3696 struct nameidata nd;
3697 fhandle_t fh;
3698
3699 error = vfs_export(NFSD_VNET(nfsv4root_mnt), &nfsexargp->export, false);
3700 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3701 NFSD_VNET(nfs_rootfhset) = 0;
3702 else if (error == 0) {
3703 if (nfsexargp->fspec == NULL) {
3704 error = EPERM;
3705 goto out;
3706 }
3707 /*
3708 * If fspec != NULL, this is the v4root path.
3709 */
3710 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec);
3711 if ((error = namei(&nd)) != 0)
3712 goto out;
3713 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3714 vrele(nd.ni_vp);
3715 if (!error) {
3716 NFSD_VNET(nfs_rootfh).nfsrvfh_len = NFSX_MYFH;
3717 NFSBCOPY((caddr_t)&fh,
3718 NFSD_VNET(nfs_rootfh).nfsrvfh_data,
3719 sizeof (fhandle_t));
3720 NFSD_VNET(nfs_rootfhset) = 1;
3721 }
3722 }
3723
3724 out:
3725 NFSEXITCODE(error);
3726 return (error);
3727 }
3728
3729 /*
3730 * This function needs to test to see if the system is near its limit
3731 * for memory allocation via malloc() or mget() and return True iff
3732 * either of these resources are near their limit.
3733 * XXX (For now, this is just a stub.)
3734 */
3735 int nfsrv_testmalloclimit = 0;
3736 int
nfsrv_mallocmget_limit(void)3737 nfsrv_mallocmget_limit(void)
3738 {
3739 static int printmesg = 0;
3740 static int testval = 1;
3741
3742 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3743 if ((printmesg++ % 100) == 0)
3744 printf("nfsd: malloc/mget near limit\n");
3745 return (1);
3746 }
3747 return (0);
3748 }
3749
3750 /*
3751 * BSD specific initialization of a mount point.
3752 */
3753 void
nfsd_mntinit(void)3754 nfsd_mntinit(void)
3755 {
3756
3757 NFSD_LOCK();
3758 if (NFSD_VNET(nfsrv_mntinited)) {
3759 NFSD_UNLOCK();
3760 return;
3761 }
3762 NFSD_VNET(nfsrv_mntinited) = true;
3763 nfsrvd_init(0);
3764 NFSD_UNLOCK();
3765
3766 NFSD_VNET(nfsv4root_mnt) = malloc(sizeof(struct mount), M_TEMP,
3767 M_WAITOK | M_ZERO);
3768 NFSD_VNET(nfsv4root_mnt)->mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3769 mtx_init(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3770 lockinit(&NFSD_VNET(nfsv4root_mnt)->mnt_explock, PVFS, "explock", 0, 0);
3771 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelist);
3772 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelist);
3773 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
3774 TAILQ_INIT(&NFSD_VNET(nfsv4root_opt));
3775 TAILQ_INIT(&NFSD_VNET(nfsv4root_newopt));
3776 NFSD_VNET(nfsv4root_mnt)->mnt_opt = &NFSD_VNET(nfsv4root_opt);
3777 NFSD_VNET(nfsv4root_mnt)->mnt_optnew = &NFSD_VNET(nfsv4root_newopt);
3778 NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelistsize = 0;
3779 NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelistsize = 0;
3780 callout_init(&NFSD_VNET(nfsd_callout), 1);
3781
3782 nfsrvd_initcache();
3783 nfsd_init();
3784 }
3785
3786 static void
nfsd_timer(void * arg)3787 nfsd_timer(void *arg)
3788 {
3789 struct vnet *vnetp;
3790
3791 vnetp = (struct vnet *)arg;
3792 NFSD_CURVNET_SET_QUIET(vnetp);
3793 nfsrv_servertimer(vnetp);
3794 callout_reset_sbt(&NFSD_VNET(nfsd_callout), SBT_1S, SBT_1S, nfsd_timer,
3795 arg, 0);
3796 NFSD_CURVNET_RESTORE();
3797 }
3798
3799 /*
3800 * Get a vnode for a file handle, without checking exports, etc.
3801 */
3802 struct vnode *
nfsvno_getvp(fhandle_t * fhp)3803 nfsvno_getvp(fhandle_t *fhp)
3804 {
3805 struct mount *mp;
3806 struct vnode *vp;
3807 int error;
3808
3809 mp = vfs_busyfs(&fhp->fh_fsid);
3810 if (mp == NULL)
3811 return (NULL);
3812 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3813 vfs_unbusy(mp);
3814 if (error)
3815 return (NULL);
3816 return (vp);
3817 }
3818
3819 /*
3820 * Do a local VOP_ADVLOCK().
3821 */
3822 int
nfsvno_advlock(struct vnode * vp,int ftype,u_int64_t first,u_int64_t end,struct thread * td)3823 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3824 u_int64_t end, struct thread *td)
3825 {
3826 int error = 0;
3827 struct flock fl;
3828 u_int64_t tlen;
3829
3830 if (nfsrv_dolocallocks == 0)
3831 goto out;
3832 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3833
3834 fl.l_whence = SEEK_SET;
3835 fl.l_type = ftype;
3836 fl.l_start = (off_t)first;
3837 if (end == NFS64BITSSET) {
3838 fl.l_len = 0;
3839 } else {
3840 tlen = end - first;
3841 fl.l_len = (off_t)tlen;
3842 }
3843 /*
3844 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3845 * values for all calls, so that all locks will be held by the
3846 * nfsd server. (The nfsd server handles conflicts between the
3847 * various clients.)
3848 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3849 * bytes, so it can't be put in l_sysid.
3850 */
3851 if (nfsv4_sysid == 0)
3852 nfsv4_sysid = nlm_acquire_next_sysid();
3853 fl.l_pid = (pid_t)0;
3854 fl.l_sysid = (int)nfsv4_sysid;
3855
3856 if (ftype == F_UNLCK)
3857 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3858 (F_POSIX | F_REMOTE));
3859 else
3860 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3861 (F_POSIX | F_REMOTE));
3862
3863 out:
3864 NFSEXITCODE(error);
3865 return (error);
3866 }
3867
3868 /*
3869 * Check the nfsv4 root exports.
3870 */
3871 int
nfsvno_v4rootexport(struct nfsrv_descript * nd)3872 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3873 {
3874 struct ucred *credanon;
3875 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3876 uint64_t exflags;
3877
3878 error = vfs_stdcheckexp(NFSD_VNET(nfsv4root_mnt), nd->nd_nam, &exflags,
3879 &credanon, &numsecflavor, secflavors);
3880 if (error) {
3881 error = NFSERR_PROGUNAVAIL;
3882 goto out;
3883 }
3884 if (credanon != NULL)
3885 crfree(credanon);
3886 for (i = 0; i < numsecflavor; i++) {
3887 if (secflavors[i] == AUTH_SYS)
3888 nd->nd_flag |= ND_EXAUTHSYS;
3889 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3890 nd->nd_flag |= ND_EXGSS;
3891 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3892 nd->nd_flag |= ND_EXGSSINTEGRITY;
3893 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3894 nd->nd_flag |= ND_EXGSSPRIVACY;
3895 }
3896
3897 /* And set ND_EXxx flags for TLS. */
3898 if ((exflags & MNT_EXTLS) != 0) {
3899 nd->nd_flag |= ND_EXTLS;
3900 if ((exflags & MNT_EXTLSCERT) != 0)
3901 nd->nd_flag |= ND_EXTLSCERT;
3902 if ((exflags & MNT_EXTLSCERTUSER) != 0)
3903 nd->nd_flag |= ND_EXTLSCERTUSER;
3904 }
3905
3906 out:
3907 NFSEXITCODE(error);
3908 return (error);
3909 }
3910
3911 /*
3912 * Nfs server pseudo system call for the nfsd's
3913 */
3914 /*
3915 * MPSAFE
3916 */
3917 static int
nfssvc_nfsd(struct thread * td,struct nfssvc_args * uap)3918 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3919 {
3920 struct file *fp;
3921 struct nfsd_addsock_args sockarg;
3922 struct nfsd_nfsd_args nfsdarg;
3923 struct nfsd_nfsd_oargs onfsdarg;
3924 struct nfsd_pnfsd_args pnfsdarg;
3925 struct vnode *vp, *nvp, *curdvp;
3926 struct pnfsdsfile *pf;
3927 struct nfsdevice *ds, *fds;
3928 cap_rights_t rights;
3929 int buflen, error, ret;
3930 char *buf, *cp, *cp2, *cp3;
3931 char fname[PNFS_FILENAME_LEN + 1];
3932
3933 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
3934 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3935 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3936 if (error)
3937 goto out;
3938 /*
3939 * Since we don't know what rights might be required,
3940 * pretend that we need them all. It is better to be too
3941 * careful than too reckless.
3942 */
3943 error = fget(td, sockarg.sock,
3944 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
3945 if (error != 0)
3946 goto out;
3947 if (fp->f_type != DTYPE_SOCKET) {
3948 fdrop(fp, td);
3949 error = EPERM;
3950 goto out;
3951 }
3952 error = nfsrvd_addsock(fp);
3953 fdrop(fp, td);
3954 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3955 if (uap->argp == NULL) {
3956 error = EINVAL;
3957 goto out;
3958 }
3959 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3960 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3961 if (error == 0) {
3962 nfsdarg.principal = onfsdarg.principal;
3963 nfsdarg.minthreads = onfsdarg.minthreads;
3964 nfsdarg.maxthreads = onfsdarg.maxthreads;
3965 nfsdarg.version = 1;
3966 nfsdarg.addr = NULL;
3967 nfsdarg.addrlen = 0;
3968 nfsdarg.dnshost = NULL;
3969 nfsdarg.dnshostlen = 0;
3970 nfsdarg.dspath = NULL;
3971 nfsdarg.dspathlen = 0;
3972 nfsdarg.mdspath = NULL;
3973 nfsdarg.mdspathlen = 0;
3974 nfsdarg.mirrorcnt = 1;
3975 }
3976 } else
3977 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3978 if (error)
3979 goto out;
3980 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3981 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3982 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3983 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3984 nfsdarg.mirrorcnt >= 1 &&
3985 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3986 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3987 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3988 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3989 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3990 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3991 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3992 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3993 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3994 if (error != 0) {
3995 free(cp, M_TEMP);
3996 goto out;
3997 }
3998 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3999 nfsdarg.addr = cp;
4000 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
4001 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
4002 if (error != 0) {
4003 free(nfsdarg.addr, M_TEMP);
4004 free(cp, M_TEMP);
4005 goto out;
4006 }
4007 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
4008 nfsdarg.dnshost = cp;
4009 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
4010 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
4011 if (error != 0) {
4012 free(nfsdarg.addr, M_TEMP);
4013 free(nfsdarg.dnshost, M_TEMP);
4014 free(cp, M_TEMP);
4015 goto out;
4016 }
4017 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
4018 nfsdarg.dspath = cp;
4019 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
4020 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
4021 if (error != 0) {
4022 free(nfsdarg.addr, M_TEMP);
4023 free(nfsdarg.dnshost, M_TEMP);
4024 free(nfsdarg.dspath, M_TEMP);
4025 free(cp, M_TEMP);
4026 goto out;
4027 }
4028 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
4029 nfsdarg.mdspath = cp;
4030 } else {
4031 nfsdarg.addr = NULL;
4032 nfsdarg.addrlen = 0;
4033 nfsdarg.dnshost = NULL;
4034 nfsdarg.dnshostlen = 0;
4035 nfsdarg.dspath = NULL;
4036 nfsdarg.dspathlen = 0;
4037 nfsdarg.mdspath = NULL;
4038 nfsdarg.mdspathlen = 0;
4039 nfsdarg.mirrorcnt = 1;
4040 }
4041 nfsd_timer(NFSD_TD_TO_VNET(td));
4042 error = nfsrvd_nfsd(td, &nfsdarg);
4043 callout_drain(&NFSD_VNET(nfsd_callout));
4044 free(nfsdarg.addr, M_TEMP);
4045 free(nfsdarg.dnshost, M_TEMP);
4046 free(nfsdarg.dspath, M_TEMP);
4047 free(nfsdarg.mdspath, M_TEMP);
4048 } else if (uap->flag & NFSSVC_PNFSDS) {
4049 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
4050 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
4051 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
4052 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4053 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
4054 NULL);
4055 if (error == 0)
4056 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
4057 free(cp, M_TEMP);
4058 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
4059 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4060 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
4061 buf = malloc(buflen, M_TEMP, M_WAITOK);
4062 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
4063 NULL);
4064 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
4065 if (error == 0 && pnfsdarg.dspath != NULL) {
4066 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4067 error = copyinstr(pnfsdarg.dspath, cp2,
4068 PATH_MAX + 1, NULL);
4069 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
4070 error);
4071 } else
4072 cp2 = NULL;
4073 if (error == 0 && pnfsdarg.curdspath != NULL) {
4074 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
4075 error = copyinstr(pnfsdarg.curdspath, cp3,
4076 PATH_MAX + 1, NULL);
4077 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
4078 error);
4079 } else
4080 cp3 = NULL;
4081 curdvp = NULL;
4082 fds = NULL;
4083 if (error == 0)
4084 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
4085 &buflen, fname, td, &vp, &nvp, &pf, &ds,
4086 &fds);
4087 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
4088 if (error == 0) {
4089 if (pf->dsf_dir >= nfsrv_dsdirsize) {
4090 printf("copymr: dsdir out of range\n");
4091 pf->dsf_dir = 0;
4092 }
4093 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
4094 error = nfsrv_copymr(vp, nvp,
4095 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
4096 (struct pnfsdsfile *)buf,
4097 buflen / sizeof(*pf), td->td_ucred, td);
4098 vput(vp);
4099 vput(nvp);
4100 if (fds != NULL && error == 0) {
4101 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
4102 ret = vn_lock(curdvp, LK_EXCLUSIVE);
4103 if (ret == 0) {
4104 nfsrv_dsremove(curdvp, fname,
4105 td->td_ucred, td);
4106 NFSVOPUNLOCK(curdvp);
4107 }
4108 }
4109 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
4110 }
4111 free(cp, M_TEMP);
4112 free(cp2, M_TEMP);
4113 free(cp3, M_TEMP);
4114 free(buf, M_TEMP);
4115 }
4116 } else {
4117 error = nfssvc_srvcall(td, uap, td->td_ucred);
4118 }
4119
4120 out:
4121 NFSD_CURVNET_RESTORE();
4122 NFSEXITCODE(error);
4123 return (error);
4124 }
4125
4126 static int
nfssvc_srvcall(struct thread * p,struct nfssvc_args * uap,struct ucred * cred)4127 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
4128 {
4129 struct nfsex_args export;
4130 struct nfsex_oldargs oexp;
4131 struct file *fp = NULL;
4132 int stablefd, i, len;
4133 struct nfsd_clid adminrevoke;
4134 struct nfsd_dumplist dumplist;
4135 struct nfsd_dumpclients *dumpclients;
4136 struct nfsd_dumplocklist dumplocklist;
4137 struct nfsd_dumplocks *dumplocks;
4138 struct nameidata nd;
4139 vnode_t vp;
4140 int error = EINVAL, igotlock;
4141 struct proc *procp;
4142 gid_t *grps;
4143
4144 if (uap->flag & NFSSVC_PUBLICFH) {
4145 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
4146 sizeof (fhandle_t));
4147 error = copyin(uap->argp,
4148 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
4149 if (!error)
4150 nfs_pubfhset = 1;
4151 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
4152 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
4153 error = copyin(uap->argp,(caddr_t)&export,
4154 sizeof (struct nfsex_args));
4155 if (!error) {
4156 grps = NULL;
4157 if (export.export.ex_ngroups > NGROUPS_MAX ||
4158 export.export.ex_ngroups < 0)
4159 error = EINVAL;
4160 else if (export.export.ex_ngroups > 0) {
4161 grps = malloc(export.export.ex_ngroups *
4162 sizeof(gid_t), M_TEMP, M_WAITOK);
4163 error = copyin(export.export.ex_groups, grps,
4164 export.export.ex_ngroups * sizeof(gid_t));
4165 export.export.ex_groups = grps;
4166 } else
4167 export.export.ex_groups = NULL;
4168 if (!error)
4169 error = nfsrv_v4rootexport(&export, cred, p);
4170 free(grps, M_TEMP);
4171 }
4172 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
4173 NFSSVC_V4ROOTEXPORT) {
4174 error = copyin(uap->argp,(caddr_t)&oexp,
4175 sizeof (struct nfsex_oldargs));
4176 if (!error) {
4177 memset(&export.export, 0, sizeof(export.export));
4178 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
4179 export.export.ex_root = oexp.export.ex_root;
4180 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
4181 export.export.ex_ngroups =
4182 oexp.export.ex_anon.cr_ngroups;
4183 export.export.ex_groups = NULL;
4184 if (export.export.ex_ngroups > XU_NGROUPS ||
4185 export.export.ex_ngroups < 0)
4186 error = EINVAL;
4187 else if (export.export.ex_ngroups > 0) {
4188 export.export.ex_groups = malloc(
4189 export.export.ex_ngroups * sizeof(gid_t),
4190 M_TEMP, M_WAITOK);
4191 for (i = 0; i < export.export.ex_ngroups; i++)
4192 export.export.ex_groups[i] =
4193 oexp.export.ex_anon.cr_groups[i];
4194 }
4195 export.export.ex_addr = oexp.export.ex_addr;
4196 export.export.ex_addrlen = oexp.export.ex_addrlen;
4197 export.export.ex_mask = oexp.export.ex_mask;
4198 export.export.ex_masklen = oexp.export.ex_masklen;
4199 export.export.ex_indexfile = oexp.export.ex_indexfile;
4200 export.export.ex_numsecflavors =
4201 oexp.export.ex_numsecflavors;
4202 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
4203 export.export.ex_numsecflavors < 0)
4204 error = EINVAL;
4205 else {
4206 for (i = 0; i < export.export.ex_numsecflavors;
4207 i++)
4208 export.export.ex_secflavors[i] =
4209 oexp.export.ex_secflavors[i];
4210 }
4211 export.fspec = oexp.fspec;
4212 if (error == 0)
4213 error = nfsrv_v4rootexport(&export, cred, p);
4214 free(export.export.ex_groups, M_TEMP);
4215 }
4216 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
4217 nfs_pubfhset = 0;
4218 error = 0;
4219 } else if (uap->flag & NFSSVC_STABLERESTART) {
4220 error = copyin(uap->argp, (caddr_t)&stablefd,
4221 sizeof (int));
4222 if (!error)
4223 error = fp_getfvp(p, stablefd, &fp, &vp);
4224 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
4225 error = EBADF;
4226 if (!error && NFSD_VNET(nfsrv_numnfsd) != 0)
4227 error = ENXIO;
4228 if (!error) {
4229 NFSD_VNET(nfsrv_stablefirst).nsf_fp = fp;
4230 nfsrv_setupstable(p);
4231 }
4232 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
4233 error = copyin(uap->argp, (caddr_t)&adminrevoke,
4234 sizeof (struct nfsd_clid));
4235 if (!error)
4236 error = nfsrv_adminrevoke(&adminrevoke, p);
4237 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
4238 error = copyin(uap->argp, (caddr_t)&dumplist,
4239 sizeof (struct nfsd_dumplist));
4240 if (!error && (dumplist.ndl_size < 1 ||
4241 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
4242 error = EPERM;
4243 if (!error) {
4244 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
4245 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4246 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
4247 error = copyout(dumpclients, dumplist.ndl_list, len);
4248 free(dumpclients, M_TEMP);
4249 }
4250 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
4251 error = copyin(uap->argp, (caddr_t)&dumplocklist,
4252 sizeof (struct nfsd_dumplocklist));
4253 if (!error && (dumplocklist.ndllck_size < 1 ||
4254 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
4255 error = EPERM;
4256 if (!error)
4257 error = nfsrv_lookupfilename(&nd,
4258 dumplocklist.ndllck_fname, p);
4259 if (!error) {
4260 len = sizeof (struct nfsd_dumplocks) *
4261 dumplocklist.ndllck_size;
4262 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4263 nfsrv_dumplocks(nd.ni_vp, dumplocks,
4264 dumplocklist.ndllck_size, p);
4265 vput(nd.ni_vp);
4266 error = copyout(dumplocks, dumplocklist.ndllck_list,
4267 len);
4268 free(dumplocks, M_TEMP);
4269 }
4270 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
4271 procp = p->td_proc;
4272 PROC_LOCK(procp);
4273 nfsd_master_pid = procp->p_pid;
4274 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
4275 nfsd_master_start = procp->p_stats->p_start;
4276 NFSD_VNET(nfsd_master_proc) = procp;
4277 PROC_UNLOCK(procp);
4278 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4279 NFSLOCKV4ROOTMUTEX();
4280 if (!NFSD_VNET(nfsrv_suspend_nfsd)) {
4281 /* Lock out all nfsd threads */
4282 do {
4283 igotlock = nfsv4_lock(
4284 &NFSD_VNET(nfsd_suspend_lock), 1, NULL,
4285 NFSV4ROOTLOCKMUTEXPTR, NULL);
4286 } while (igotlock == 0 &&
4287 !NFSD_VNET(nfsrv_suspend_nfsd));
4288 NFSD_VNET(nfsrv_suspend_nfsd) = true;
4289 }
4290 NFSUNLOCKV4ROOTMUTEX();
4291 error = 0;
4292 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4293 NFSLOCKV4ROOTMUTEX();
4294 if (NFSD_VNET(nfsrv_suspend_nfsd)) {
4295 nfsv4_unlock(&NFSD_VNET(nfsd_suspend_lock), 0);
4296 NFSD_VNET(nfsrv_suspend_nfsd) = false;
4297 }
4298 NFSUNLOCKV4ROOTMUTEX();
4299 error = 0;
4300 }
4301
4302 NFSEXITCODE(error);
4303 return (error);
4304 }
4305
4306 /*
4307 * Check exports.
4308 * Returns 0 if ok, 1 otherwise.
4309 */
4310 int
nfsvno_testexp(struct nfsrv_descript * nd,struct nfsexstuff * exp)4311 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4312 {
4313 int i;
4314
4315 if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4316 (NFSVNO_EXTLSCERT(exp) &&
4317 (nd->nd_flag & ND_TLSCERT) == 0) ||
4318 (NFSVNO_EXTLSCERTUSER(exp) &&
4319 (nd->nd_flag & ND_TLSCERTUSER) == 0)) {
4320 if ((nd->nd_flag & ND_NFSV4) != 0)
4321 return (NFSERR_WRONGSEC);
4322 #ifdef notnow
4323 /* There is currently no auth_stat for this. */
4324 else if ((nd->nd_flag & ND_TLS) == 0)
4325 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4326 else
4327 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4328 #endif
4329 else
4330 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4331 }
4332
4333 /*
4334 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use
4335 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
4336 */
4337 if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO)
4338 return (0);
4339
4340 /*
4341 * This seems odd, but allow the case where the security flavor
4342 * list is empty. This happens when NFSv4 is traversing non-exported
4343 * file systems. Exported file systems should always have a non-empty
4344 * security flavor list.
4345 */
4346 if (exp->nes_numsecflavor == 0)
4347 return (0);
4348
4349 for (i = 0; i < exp->nes_numsecflavor; i++) {
4350 /*
4351 * The tests for privacy and integrity must be first,
4352 * since ND_GSS is set for everything but AUTH_SYS.
4353 */
4354 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4355 (nd->nd_flag & ND_GSSPRIVACY))
4356 return (0);
4357 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4358 (nd->nd_flag & ND_GSSINTEGRITY))
4359 return (0);
4360 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4361 (nd->nd_flag & ND_GSS))
4362 return (0);
4363 if (exp->nes_secflavors[i] == AUTH_SYS &&
4364 (nd->nd_flag & ND_GSS) == 0)
4365 return (0);
4366 }
4367 if ((nd->nd_flag & ND_NFSV4) != 0)
4368 return (NFSERR_WRONGSEC);
4369 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4370 }
4371
4372 /*
4373 * Calculate a hash value for the fid in a file handle.
4374 */
4375 uint32_t
nfsrv_hashfh(fhandle_t * fhp)4376 nfsrv_hashfh(fhandle_t *fhp)
4377 {
4378 uint32_t hashval;
4379
4380 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4381 return (hashval);
4382 }
4383
4384 /*
4385 * Calculate a hash value for the sessionid.
4386 */
4387 uint32_t
nfsrv_hashsessionid(uint8_t * sessionid)4388 nfsrv_hashsessionid(uint8_t *sessionid)
4389 {
4390 uint32_t hashval;
4391
4392 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4393 return (hashval);
4394 }
4395
4396 /*
4397 * Signal the userland master nfsd to backup the stable restart file.
4398 */
4399 void
nfsrv_backupstable(void)4400 nfsrv_backupstable(void)
4401 {
4402 struct proc *procp;
4403
4404 if (NFSD_VNET(nfsd_master_proc) != NULL) {
4405 procp = pfind(nfsd_master_pid);
4406 /* Try to make sure it is the correct process. */
4407 if (procp == NFSD_VNET(nfsd_master_proc) &&
4408 procp->p_stats->p_start.tv_sec ==
4409 nfsd_master_start.tv_sec &&
4410 procp->p_stats->p_start.tv_usec ==
4411 nfsd_master_start.tv_usec &&
4412 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4413 kern_psignal(procp, SIGUSR2);
4414 else
4415 NFSD_VNET(nfsd_master_proc) = NULL;
4416
4417 if (procp != NULL)
4418 PROC_UNLOCK(procp);
4419 }
4420 }
4421
4422 /*
4423 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4424 * The arguments are in a structure, so that they can be passed through
4425 * taskqueue for a kernel process to execute this function.
4426 */
4427 struct nfsrvdscreate {
4428 int done;
4429 int inprog;
4430 struct task tsk;
4431 struct ucred *tcred;
4432 struct vnode *dvp;
4433 NFSPROC_T *p;
4434 struct pnfsdsfile *pf;
4435 int err;
4436 fhandle_t fh;
4437 struct vattr va;
4438 struct vattr createva;
4439 };
4440
4441 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)4442 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4443 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4444 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4445 {
4446 struct vnode *nvp;
4447 struct nameidata named;
4448 struct vattr va;
4449 char *bufp;
4450 u_long *hashp;
4451 struct nfsnode *np;
4452 struct nfsmount *nmp;
4453 int error;
4454
4455 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4456 LOCKPARENT | LOCKLEAF | NOCACHE);
4457 nfsvno_setpathbuf(&named, &bufp, &hashp);
4458 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4459 named.ni_cnd.cn_nameptr = bufp;
4460 if (fnamep != NULL) {
4461 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4462 named.ni_cnd.cn_namelen = strlen(bufp);
4463 } else
4464 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4465 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4466
4467 /* Create the date file in the DS mount. */
4468 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4469 if (error == 0) {
4470 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4471 vref(dvp);
4472 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4473 if (error == 0) {
4474 /* Set the ownership of the file. */
4475 error = VOP_SETATTR(nvp, nvap, tcred);
4476 NFSD_DEBUG(4, "nfsrv_dscreate:"
4477 " setattr-uid=%d\n", error);
4478 if (error != 0)
4479 vput(nvp);
4480 }
4481 if (error != 0)
4482 printf("pNFS: pnfscreate failed=%d\n", error);
4483 } else
4484 printf("pNFS: pnfscreate vnlock=%d\n", error);
4485 if (error == 0) {
4486 np = VTONFS(nvp);
4487 nmp = VFSTONFS(nvp->v_mount);
4488 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4489 != 0 || nmp->nm_nam->sa_len > sizeof(
4490 struct sockaddr_in6) ||
4491 np->n_fhp->nfh_len != NFSX_MYFH) {
4492 printf("Bad DS file: fstype=%s salen=%d"
4493 " fhlen=%d\n",
4494 nvp->v_mount->mnt_vfc->vfc_name,
4495 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4496 error = ENOENT;
4497 }
4498
4499 /* Set extattrs for the DS on the MDS file. */
4500 if (error == 0) {
4501 if (dsa != NULL) {
4502 error = VOP_GETATTR(nvp, &va, tcred);
4503 if (error == 0) {
4504 dsa->dsa_filerev = va.va_filerev;
4505 dsa->dsa_size = va.va_size;
4506 dsa->dsa_atime = va.va_atime;
4507 dsa->dsa_mtime = va.va_mtime;
4508 dsa->dsa_bytes = va.va_bytes;
4509 }
4510 }
4511 if (error == 0) {
4512 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4513 NFSX_MYFH);
4514 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4515 nmp->nm_nam->sa_len);
4516 NFSBCOPY(named.ni_cnd.cn_nameptr,
4517 pf->dsf_filename,
4518 sizeof(pf->dsf_filename));
4519 }
4520 } else
4521 printf("pNFS: pnfscreate can't get DS"
4522 " attr=%d\n", error);
4523 if (nvpp != NULL && error == 0)
4524 *nvpp = nvp;
4525 else
4526 vput(nvp);
4527 }
4528 nfsvno_relpathbuf(&named);
4529 return (error);
4530 }
4531
4532 /*
4533 * Start up the thread that will execute nfsrv_dscreate().
4534 */
4535 static void
start_dscreate(void * arg,int pending)4536 start_dscreate(void *arg, int pending)
4537 {
4538 struct nfsrvdscreate *dsc;
4539
4540 dsc = (struct nfsrvdscreate *)arg;
4541 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4542 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4543 dsc->done = 1;
4544 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4545 }
4546
4547 /*
4548 * Create a pNFS data file on the Data Server(s).
4549 */
4550 static void
nfsrv_pnfscreate(struct vnode * vp,struct vattr * vap,struct ucred * cred,NFSPROC_T * p)4551 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4552 NFSPROC_T *p)
4553 {
4554 struct nfsrvdscreate *dsc, *tdsc = NULL;
4555 struct nfsdevice *ds, *tds, *fds;
4556 struct mount *mp;
4557 struct pnfsdsfile *pf, *tpf;
4558 struct pnfsdsattr dsattr;
4559 struct vattr va;
4560 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4561 struct nfsmount *nmp;
4562 fhandle_t fh;
4563 uid_t vauid;
4564 gid_t vagid;
4565 u_short vamode;
4566 struct ucred *tcred;
4567 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4568 int failpos, timo;
4569
4570 /* Get a DS server directory in a round-robin order. */
4571 mirrorcnt = 1;
4572 mp = vp->v_mount;
4573 ds = fds = NULL;
4574 NFSDDSLOCK();
4575 /*
4576 * Search for the first entry that handles this MDS fs, but use the
4577 * first entry for all MDS fs's otherwise.
4578 */
4579 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4580 if (tds->nfsdev_nmp != NULL) {
4581 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4582 ds = tds;
4583 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4584 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4585 ds = fds = tds;
4586 break;
4587 }
4588 }
4589 }
4590 if (ds == NULL) {
4591 NFSDDSUNLOCK();
4592 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4593 return;
4594 }
4595 i = dsdir[0] = ds->nfsdev_nextdir;
4596 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4597 dvp[0] = ds->nfsdev_dsdir[i];
4598 tds = TAILQ_NEXT(ds, nfsdev_list);
4599 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4600 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4601 if (tds->nfsdev_nmp != NULL &&
4602 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4603 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4604 fsidcmp(&mp->mnt_stat.f_fsid,
4605 &tds->nfsdev_mdsfsid) == 0))) {
4606 dsdir[mirrorcnt] = i;
4607 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4608 mirrorcnt++;
4609 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4610 break;
4611 }
4612 }
4613 }
4614 /* Put at end of list to implement round-robin usage. */
4615 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4616 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4617 NFSDDSUNLOCK();
4618 dsc = NULL;
4619 if (mirrorcnt > 1)
4620 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4621 M_WAITOK | M_ZERO);
4622 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4623 M_ZERO);
4624
4625 error = nfsvno_getfh(vp, &fh, p);
4626 if (error == 0)
4627 error = VOP_GETATTR(vp, &va, cred);
4628 if (error == 0) {
4629 /* Set the attributes for "vp" to Setattr the DS vp. */
4630 vauid = va.va_uid;
4631 vagid = va.va_gid;
4632 vamode = va.va_mode;
4633 VATTR_NULL(&va);
4634 va.va_uid = vauid;
4635 va.va_gid = vagid;
4636 va.va_mode = vamode;
4637 va.va_size = 0;
4638 } else
4639 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4640
4641 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4642 cred->cr_gid);
4643 /* Make data file name based on FH. */
4644 tcred = newnfs_getcred();
4645
4646 /*
4647 * Create the file on each DS mirror, using kernel process(es) for the
4648 * additional mirrors.
4649 */
4650 failpos = -1;
4651 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4652 tpf->dsf_dir = dsdir[i];
4653 tdsc->tcred = tcred;
4654 tdsc->p = p;
4655 tdsc->pf = tpf;
4656 tdsc->createva = *vap;
4657 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4658 tdsc->va = va;
4659 tdsc->dvp = dvp[i];
4660 tdsc->done = 0;
4661 tdsc->inprog = 0;
4662 tdsc->err = 0;
4663 ret = EIO;
4664 if (nfs_pnfsiothreads != 0) {
4665 ret = nfs_pnfsio(start_dscreate, tdsc);
4666 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4667 }
4668 if (ret != 0) {
4669 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4670 NULL, tcred, p, NULL);
4671 if (ret != 0) {
4672 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4673 error));
4674 if (failpos == -1 && nfsds_failerr(ret))
4675 failpos = i;
4676 else
4677 error = ret;
4678 }
4679 }
4680 }
4681 if (error == 0) {
4682 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4683 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4684 &dsattr, NULL, tcred, p, NULL);
4685 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4686 failpos = mirrorcnt - 1;
4687 error = 0;
4688 }
4689 }
4690 timo = hz / 50; /* Wait for 20msec. */
4691 if (timo < 1)
4692 timo = 1;
4693 /* Wait for kernel task(s) to complete. */
4694 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4695 while (tdsc->inprog != 0 && tdsc->done == 0)
4696 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4697 if (tdsc->err != 0) {
4698 if (failpos == -1 && nfsds_failerr(tdsc->err))
4699 failpos = i;
4700 else if (error == 0)
4701 error = tdsc->err;
4702 }
4703 }
4704
4705 /*
4706 * If failpos has been set, that mirror has failed, so it needs
4707 * to be disabled.
4708 */
4709 if (failpos >= 0) {
4710 nmp = VFSTONFS(dvp[failpos]->v_mount);
4711 NFSLOCKMNT(nmp);
4712 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4713 NFSMNTP_CANCELRPCS)) == 0) {
4714 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4715 NFSUNLOCKMNT(nmp);
4716 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4717 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4718 ds);
4719 if (ds != NULL)
4720 nfsrv_killrpcs(nmp);
4721 NFSLOCKMNT(nmp);
4722 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4723 wakeup(nmp);
4724 }
4725 NFSUNLOCKMNT(nmp);
4726 }
4727
4728 NFSFREECRED(tcred);
4729 if (error == 0) {
4730 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4731
4732 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4733 mirrorcnt, nfsrv_maxpnfsmirror);
4734 /*
4735 * For all mirrors that couldn't be created, fill in the
4736 * *pf structure, but with an IP address == 0.0.0.0.
4737 */
4738 tpf = pf + mirrorcnt;
4739 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4740 *tpf = *pf;
4741 tpf->dsf_sin.sin_family = AF_INET;
4742 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4743 tpf->dsf_sin.sin_addr.s_addr = 0;
4744 tpf->dsf_sin.sin_port = 0;
4745 }
4746
4747 error = vn_extattr_set(vp, IO_NODELOCKED,
4748 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4749 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4750 if (error == 0)
4751 error = vn_extattr_set(vp, IO_NODELOCKED,
4752 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4753 sizeof(dsattr), (char *)&dsattr, p);
4754 if (error != 0)
4755 printf("pNFS: pnfscreate setextattr=%d\n",
4756 error);
4757 } else
4758 printf("pNFS: pnfscreate=%d\n", error);
4759 free(pf, M_TEMP);
4760 free(dsc, M_TEMP);
4761 }
4762
4763 /*
4764 * Get the information needed to remove the pNFS Data Server file from the
4765 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4766 * DS directory vnode. The caller must unlock *ddvp when done with it.
4767 */
4768 static void
nfsrv_pnfsremovesetup(struct vnode * vp,NFSPROC_T * p,struct vnode ** dvpp,int * mirrorcntp,char * fname,fhandle_t * fhp)4769 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4770 int *mirrorcntp, char *fname, fhandle_t *fhp)
4771 {
4772 struct vattr va;
4773 struct ucred *tcred;
4774 char *buf;
4775 int buflen, error;
4776
4777 dvpp[0] = NULL;
4778 /* If not an exported regular file or not a pNFS server, just return. */
4779 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4780 nfsrv_devidcnt == 0)
4781 return;
4782
4783 /* Check to see if this is the last hard link. */
4784 tcred = newnfs_getcred();
4785 error = VOP_GETATTR(vp, &va, tcred);
4786 NFSFREECRED(tcred);
4787 if (error != 0) {
4788 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4789 return;
4790 }
4791 if (va.va_nlink > 1)
4792 return;
4793
4794 error = nfsvno_getfh(vp, fhp, p);
4795 if (error != 0) {
4796 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4797 return;
4798 }
4799
4800 buflen = 1024;
4801 buf = malloc(buflen, M_TEMP, M_WAITOK);
4802 /* Get the directory vnode for the DS mount and the file handle. */
4803 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4804 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4805 free(buf, M_TEMP);
4806 if (error != 0)
4807 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4808 }
4809
4810 /*
4811 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4812 * The arguments are in a structure, so that they can be passed through
4813 * taskqueue for a kernel process to execute this function.
4814 */
4815 struct nfsrvdsremove {
4816 int done;
4817 int inprog;
4818 struct task tsk;
4819 struct ucred *tcred;
4820 struct vnode *dvp;
4821 NFSPROC_T *p;
4822 int err;
4823 char fname[PNFS_FILENAME_LEN + 1];
4824 };
4825
4826 static int
nfsrv_dsremove(struct vnode * dvp,char * fname,struct ucred * tcred,NFSPROC_T * p)4827 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4828 NFSPROC_T *p)
4829 {
4830 struct nameidata named;
4831 struct vnode *nvp;
4832 char *bufp;
4833 u_long *hashp;
4834 int error;
4835
4836 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4837 if (error != 0)
4838 return (error);
4839 named.ni_cnd.cn_nameiop = DELETE;
4840 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4841 named.ni_cnd.cn_cred = tcred;
4842 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
4843 nfsvno_setpathbuf(&named, &bufp, &hashp);
4844 named.ni_cnd.cn_nameptr = bufp;
4845 named.ni_cnd.cn_namelen = strlen(fname);
4846 strlcpy(bufp, fname, NAME_MAX);
4847 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4848 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4849 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4850 if (error == 0) {
4851 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4852 vput(nvp);
4853 }
4854 NFSVOPUNLOCK(dvp);
4855 nfsvno_relpathbuf(&named);
4856 if (error != 0)
4857 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4858 return (error);
4859 }
4860
4861 /*
4862 * Start up the thread that will execute nfsrv_dsremove().
4863 */
4864 static void
start_dsremove(void * arg,int pending)4865 start_dsremove(void *arg, int pending)
4866 {
4867 struct nfsrvdsremove *dsrm;
4868
4869 dsrm = (struct nfsrvdsremove *)arg;
4870 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4871 dsrm->p);
4872 dsrm->done = 1;
4873 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4874 }
4875
4876 /*
4877 * Remove a pNFS data file from a Data Server.
4878 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4879 * removed to set up the dvp and fill in the FH.
4880 */
4881 static void
nfsrv_pnfsremove(struct vnode ** dvp,int mirrorcnt,char * fname,fhandle_t * fhp,NFSPROC_T * p)4882 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4883 NFSPROC_T *p)
4884 {
4885 struct ucred *tcred;
4886 struct nfsrvdsremove *dsrm, *tdsrm;
4887 struct nfsdevice *ds;
4888 struct nfsmount *nmp;
4889 int failpos, i, ret, timo;
4890
4891 tcred = newnfs_getcred();
4892 dsrm = NULL;
4893 if (mirrorcnt > 1)
4894 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4895 /*
4896 * Remove the file on each DS mirror, using kernel process(es) for the
4897 * additional mirrors.
4898 */
4899 failpos = -1;
4900 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4901 tdsrm->tcred = tcred;
4902 tdsrm->p = p;
4903 tdsrm->dvp = dvp[i];
4904 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4905 tdsrm->inprog = 0;
4906 tdsrm->done = 0;
4907 tdsrm->err = 0;
4908 ret = EIO;
4909 if (nfs_pnfsiothreads != 0) {
4910 ret = nfs_pnfsio(start_dsremove, tdsrm);
4911 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4912 }
4913 if (ret != 0) {
4914 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4915 if (failpos == -1 && nfsds_failerr(ret))
4916 failpos = i;
4917 }
4918 }
4919 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4920 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4921 failpos = mirrorcnt - 1;
4922 timo = hz / 50; /* Wait for 20msec. */
4923 if (timo < 1)
4924 timo = 1;
4925 /* Wait for kernel task(s) to complete. */
4926 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4927 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4928 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4929 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4930 failpos = i;
4931 }
4932
4933 /*
4934 * If failpos has been set, that mirror has failed, so it needs
4935 * to be disabled.
4936 */
4937 if (failpos >= 0) {
4938 nmp = VFSTONFS(dvp[failpos]->v_mount);
4939 NFSLOCKMNT(nmp);
4940 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4941 NFSMNTP_CANCELRPCS)) == 0) {
4942 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4943 NFSUNLOCKMNT(nmp);
4944 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4945 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4946 ds);
4947 if (ds != NULL)
4948 nfsrv_killrpcs(nmp);
4949 NFSLOCKMNT(nmp);
4950 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4951 wakeup(nmp);
4952 }
4953 NFSUNLOCKMNT(nmp);
4954 }
4955
4956 /* Get rid all layouts for the file. */
4957 nfsrv_freefilelayouts(fhp);
4958
4959 NFSFREECRED(tcred);
4960 free(dsrm, M_TEMP);
4961 }
4962
4963 /*
4964 * Generate a file name based on the file handle and put it in *bufp.
4965 * Return the number of bytes generated.
4966 */
4967 static int
nfsrv_putfhname(fhandle_t * fhp,char * bufp)4968 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4969 {
4970 int i;
4971 uint8_t *cp;
4972 const uint8_t *hexdigits = "0123456789abcdef";
4973
4974 cp = (uint8_t *)fhp;
4975 for (i = 0; i < sizeof(*fhp); i++) {
4976 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4977 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4978 }
4979 bufp[2 * i] = '\0';
4980 return (2 * i);
4981 }
4982
4983 /*
4984 * Update the Metadata file's attributes from the DS file when a Read/Write
4985 * layout is returned.
4986 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4987 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4988 */
4989 int
nfsrv_updatemdsattr(struct vnode * vp,struct nfsvattr * nap,NFSPROC_T * p)4990 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4991 {
4992 struct ucred *tcred;
4993 int error;
4994
4995 /* Do this as root so that it won't fail with EACCES. */
4996 tcred = newnfs_getcred();
4997 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4998 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4999 NFSFREECRED(tcred);
5000 return (error);
5001 }
5002
5003 /*
5004 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
5005 */
5006 static int
nfsrv_dssetacl(struct vnode * vp,struct acl * aclp,struct ucred * cred,NFSPROC_T * p)5007 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
5008 NFSPROC_T *p)
5009 {
5010 int error;
5011
5012 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
5013 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
5014 return (error);
5015 }
5016
5017 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)5018 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
5019 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
5020 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
5021 off_t *offp, int content, bool *eofp)
5022 {
5023 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
5024 fhandle_t fh[NFSDEV_MAXMIRRORS];
5025 struct vnode *dvp[NFSDEV_MAXMIRRORS];
5026 struct nfsdevice *ds;
5027 struct pnfsdsattr dsattr;
5028 struct opnfsdsattr odsattr;
5029 char *buf;
5030 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
5031
5032 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
5033 /*
5034 * If not a regular file, not exported or not a pNFS server,
5035 * just return ENOENT.
5036 */
5037 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
5038 nfsrv_devidcnt == 0)
5039 return (ENOENT);
5040
5041 buflen = 1024;
5042 buf = malloc(buflen, M_TEMP, M_WAITOK);
5043 error = 0;
5044
5045 /*
5046 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
5047 * from the MetaData file's extended attribute.
5048 */
5049 if (ioproc == NFSPROC_GETATTR) {
5050 error = vn_extattr_get(vp, IO_NODELOCKED,
5051 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
5052 p);
5053 if (error == 0) {
5054 if (buflen == sizeof(odsattr)) {
5055 NFSBCOPY(buf, &odsattr, buflen);
5056 nap->na_filerev = odsattr.dsa_filerev;
5057 nap->na_size = odsattr.dsa_size;
5058 nap->na_atime = odsattr.dsa_atime;
5059 nap->na_mtime = odsattr.dsa_mtime;
5060 /*
5061 * Fake na_bytes by rounding up na_size.
5062 * Since we don't know the block size, just
5063 * use BLKDEV_IOSIZE.
5064 */
5065 nap->na_bytes = (odsattr.dsa_size +
5066 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
5067 } else if (buflen == sizeof(dsattr)) {
5068 NFSBCOPY(buf, &dsattr, buflen);
5069 nap->na_filerev = dsattr.dsa_filerev;
5070 nap->na_size = dsattr.dsa_size;
5071 nap->na_atime = dsattr.dsa_atime;
5072 nap->na_mtime = dsattr.dsa_mtime;
5073 nap->na_bytes = dsattr.dsa_bytes;
5074 } else
5075 error = ENXIO;
5076 }
5077 if (error == 0) {
5078 /*
5079 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
5080 * returns 0, just return now. nfsrv_checkdsattr()
5081 * returns 0 if there is no Read/Write layout
5082 * plus either an Open/Write_access or Write
5083 * delegation issued to a client for the file.
5084 */
5085 if (nfsrv_pnfsgetdsattr == 0 ||
5086 nfsrv_checkdsattr(vp, p) == 0) {
5087 free(buf, M_TEMP);
5088 return (error);
5089 }
5090 }
5091
5092 /*
5093 * Clear ENOATTR so the code below will attempt to do a
5094 * nfsrv_getattrdsrpc() to get the attributes and (re)create
5095 * the extended attribute.
5096 */
5097 if (error == ENOATTR)
5098 error = 0;
5099 }
5100
5101 origmircnt = -1;
5102 trycnt = 0;
5103 tryagain:
5104 if (error == 0) {
5105 buflen = 1024;
5106 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
5107 LK_EXCLUSIVE)
5108 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
5109 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
5110 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
5111 NULL, NULL);
5112 if (error == 0) {
5113 for (i = 0; i < mirrorcnt; i++)
5114 nmp[i] = VFSTONFS(dvp[i]->v_mount);
5115 } else
5116 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
5117 } else
5118 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
5119 if (error == 0) {
5120 failpos = -1;
5121 if (origmircnt == -1)
5122 origmircnt = mirrorcnt;
5123 /*
5124 * If failpos is set to a mirror#, then that mirror has
5125 * failed and will be disabled. For Read, Getattr and Seek, the
5126 * function only tries one mirror, so if that mirror has
5127 * failed, it will need to be retried. As such, increment
5128 * tryitagain for these cases.
5129 * For Write, Setattr and Setacl, the function tries all
5130 * mirrors and will not return an error for the case where
5131 * one mirror has failed. For these cases, the functioning
5132 * mirror(s) will have been modified, so a retry isn't
5133 * necessary. These functions will set failpos for the
5134 * failed mirror#.
5135 */
5136 if (ioproc == NFSPROC_READDS) {
5137 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
5138 mpp, mpp2);
5139 if (nfsds_failerr(error) && mirrorcnt > 1) {
5140 /*
5141 * Setting failpos will cause the mirror
5142 * to be disabled and then a retry of this
5143 * read is required.
5144 */
5145 failpos = 0;
5146 error = 0;
5147 trycnt++;
5148 }
5149 } else if (ioproc == NFSPROC_WRITEDS)
5150 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
5151 &nmp[0], mirrorcnt, mpp, cp, &failpos);
5152 else if (ioproc == NFSPROC_SETATTR)
5153 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
5154 mirrorcnt, nap, &failpos);
5155 else if (ioproc == NFSPROC_SETACL)
5156 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
5157 mirrorcnt, aclp, &failpos);
5158 else if (ioproc == NFSPROC_SEEKDS) {
5159 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
5160 p, nmp[0]);
5161 if (nfsds_failerr(error) && mirrorcnt > 1) {
5162 /*
5163 * Setting failpos will cause the mirror
5164 * to be disabled and then a retry of this
5165 * read is required.
5166 */
5167 failpos = 0;
5168 error = 0;
5169 trycnt++;
5170 }
5171 } else if (ioproc == NFSPROC_ALLOCATE)
5172 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
5173 &nmp[0], mirrorcnt, &failpos);
5174 else if (ioproc == NFSPROC_DEALLOCATE)
5175 error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p,
5176 vp, &nmp[0], mirrorcnt, &failpos);
5177 else {
5178 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
5179 vp, nmp[mirrorcnt - 1], nap);
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 * getattr is required.
5185 */
5186 failpos = mirrorcnt - 1;
5187 error = 0;
5188 trycnt++;
5189 }
5190 }
5191 ds = NULL;
5192 if (failpos >= 0) {
5193 failnmp = nmp[failpos];
5194 NFSLOCKMNT(failnmp);
5195 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
5196 NFSMNTP_CANCELRPCS)) == 0) {
5197 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
5198 NFSUNLOCKMNT(failnmp);
5199 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
5200 failnmp, p);
5201 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
5202 failpos, ds);
5203 if (ds != NULL)
5204 nfsrv_killrpcs(failnmp);
5205 NFSLOCKMNT(failnmp);
5206 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
5207 wakeup(failnmp);
5208 }
5209 NFSUNLOCKMNT(failnmp);
5210 }
5211 for (i = 0; i < mirrorcnt; i++)
5212 NFSVOPUNLOCK(dvp[i]);
5213 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
5214 trycnt);
5215 /* Try the Read/Getattr again if a mirror was deleted. */
5216 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
5217 goto tryagain;
5218 } else {
5219 /* Return ENOENT for any Extended Attribute error. */
5220 error = ENOENT;
5221 }
5222 free(buf, M_TEMP);
5223 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
5224 return (error);
5225 }
5226
5227 /*
5228 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
5229 * attribute.
5230 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
5231 * to be checked. If it points to a NULL nmp, then it returns
5232 * a suitable destination.
5233 * curnmp - If non-NULL, it is the source mount for the copy.
5234 */
5235 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)5236 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
5237 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
5238 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
5239 struct nfsmount *curnmp, int *ippos, int *dsdirp)
5240 {
5241 struct vnode *dvp, *nvp = NULL, **tdvpp;
5242 struct mount *mp;
5243 struct nfsmount *nmp, *newnmp;
5244 struct sockaddr *sad;
5245 struct sockaddr_in *sin;
5246 struct nfsdevice *ds, *tds, *fndds;
5247 struct pnfsdsfile *pf;
5248 uint32_t dsdir;
5249 int error, fhiszero, fnd, gotone, i, mirrorcnt;
5250
5251 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
5252 *mirrorcntp = 1;
5253 tdvpp = dvpp;
5254 if (nvpp != NULL)
5255 *nvpp = NULL;
5256 if (dvpp != NULL)
5257 *dvpp = NULL;
5258 if (ippos != NULL)
5259 *ippos = -1;
5260 if (newnmpp != NULL)
5261 newnmp = *newnmpp;
5262 else
5263 newnmp = NULL;
5264 mp = vp->v_mount;
5265 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5266 "pnfsd.dsfile", buflenp, buf, p);
5267 mirrorcnt = *buflenp / sizeof(*pf);
5268 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
5269 *buflenp != sizeof(*pf) * mirrorcnt))
5270 error = ENOATTR;
5271
5272 pf = (struct pnfsdsfile *)buf;
5273 /* If curnmp != NULL, check for a match in the mirror list. */
5274 if (curnmp != NULL && error == 0) {
5275 fnd = 0;
5276 for (i = 0; i < mirrorcnt; i++, pf++) {
5277 sad = (struct sockaddr *)&pf->dsf_sin;
5278 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5279 if (ippos != NULL)
5280 *ippos = i;
5281 fnd = 1;
5282 break;
5283 }
5284 }
5285 if (fnd == 0)
5286 error = ENXIO;
5287 }
5288
5289 gotone = 0;
5290 pf = (struct pnfsdsfile *)buf;
5291 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5292 error);
5293 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5294 fhiszero = 0;
5295 sad = (struct sockaddr *)&pf->dsf_sin;
5296 sin = &pf->dsf_sin;
5297 dsdir = pf->dsf_dir;
5298 if (dsdir >= nfsrv_dsdirsize) {
5299 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5300 error = ENOATTR;
5301 } else if (nvpp != NULL && newnmp != NULL &&
5302 nfsaddr2_match(sad, newnmp->nm_nam))
5303 error = EEXIST;
5304 if (error == 0) {
5305 if (ippos != NULL && curnmp == NULL &&
5306 sad->sa_family == AF_INET &&
5307 sin->sin_addr.s_addr == 0)
5308 *ippos = i;
5309 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5310 fhiszero = 1;
5311 /* Use the socket address to find the mount point. */
5312 fndds = NULL;
5313 NFSDDSLOCK();
5314 /* Find a match for the IP address. */
5315 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5316 if (ds->nfsdev_nmp != NULL) {
5317 dvp = ds->nfsdev_dvp;
5318 nmp = VFSTONFS(dvp->v_mount);
5319 if (nmp != ds->nfsdev_nmp)
5320 printf("different2 nmp %p %p\n",
5321 nmp, ds->nfsdev_nmp);
5322 if (nfsaddr2_match(sad, nmp->nm_nam)) {
5323 fndds = ds;
5324 break;
5325 }
5326 }
5327 }
5328 if (fndds != NULL && newnmpp != NULL &&
5329 newnmp == NULL) {
5330 /* Search for a place to make a mirror copy. */
5331 TAILQ_FOREACH(tds, &nfsrv_devidhead,
5332 nfsdev_list) {
5333 if (tds->nfsdev_nmp != NULL &&
5334 fndds != tds &&
5335 ((tds->nfsdev_mdsisset == 0 &&
5336 fndds->nfsdev_mdsisset == 0) ||
5337 (tds->nfsdev_mdsisset != 0 &&
5338 fndds->nfsdev_mdsisset != 0 &&
5339 fsidcmp(&tds->nfsdev_mdsfsid,
5340 &mp->mnt_stat.f_fsid) == 0))) {
5341 *newnmpp = tds->nfsdev_nmp;
5342 break;
5343 }
5344 }
5345 if (tds != NULL) {
5346 /*
5347 * Move this entry to the end of the
5348 * list, so it won't be selected as
5349 * easily the next time.
5350 */
5351 TAILQ_REMOVE(&nfsrv_devidhead, tds,
5352 nfsdev_list);
5353 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5354 nfsdev_list);
5355 }
5356 }
5357 NFSDDSUNLOCK();
5358 if (fndds != NULL) {
5359 dvp = fndds->nfsdev_dsdir[dsdir];
5360 if (lktype != 0 || fhiszero != 0 ||
5361 (nvpp != NULL && *nvpp == NULL)) {
5362 if (fhiszero != 0)
5363 error = vn_lock(dvp,
5364 LK_EXCLUSIVE);
5365 else if (lktype != 0)
5366 error = vn_lock(dvp, lktype);
5367 else
5368 error = vn_lock(dvp, LK_SHARED);
5369 /*
5370 * If the file handle is all 0's, try to
5371 * do a Lookup against the DS to acquire
5372 * it.
5373 * If dvpp == NULL or the Lookup fails,
5374 * unlock dvp after the call.
5375 */
5376 if (error == 0 && (fhiszero != 0 ||
5377 (nvpp != NULL && *nvpp == NULL))) {
5378 error = nfsrv_pnfslookupds(vp,
5379 dvp, pf, &nvp, p);
5380 if (error == 0) {
5381 if (fhiszero != 0)
5382 nfsrv_pnfssetfh(
5383 vp, pf,
5384 devid,
5385 fnamep,
5386 nvp, p);
5387 if (nvpp != NULL &&
5388 *nvpp == NULL) {
5389 *nvpp = nvp;
5390 *dsdirp = dsdir;
5391 } else
5392 vput(nvp);
5393 }
5394 if (error != 0 || lktype == 0)
5395 NFSVOPUNLOCK(dvp);
5396 }
5397 }
5398 if (error == 0) {
5399 gotone++;
5400 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5401 if (devid != NULL) {
5402 NFSBCOPY(fndds->nfsdev_deviceid,
5403 devid, NFSX_V4DEVICEID);
5404 devid += NFSX_V4DEVICEID;
5405 }
5406 if (dvpp != NULL)
5407 *tdvpp++ = dvp;
5408 if (fhp != NULL)
5409 NFSBCOPY(&pf->dsf_fh, fhp++,
5410 NFSX_MYFH);
5411 if (fnamep != NULL && gotone == 1)
5412 strlcpy(fnamep,
5413 pf->dsf_filename,
5414 sizeof(pf->dsf_filename));
5415 } else
5416 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5417 "err=%d\n", error);
5418 }
5419 }
5420 }
5421 if (error == 0 && gotone == 0)
5422 error = ENOENT;
5423
5424 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5425 error);
5426 if (error == 0)
5427 *mirrorcntp = gotone;
5428 else {
5429 if (gotone > 0 && dvpp != NULL) {
5430 /*
5431 * If the error didn't occur on the first one and
5432 * dvpp != NULL, the one(s) prior to the failure will
5433 * have locked dvp's that need to be unlocked.
5434 */
5435 for (i = 0; i < gotone; i++) {
5436 NFSVOPUNLOCK(*dvpp);
5437 *dvpp++ = NULL;
5438 }
5439 }
5440 /*
5441 * If it found the vnode to be copied from before a failure,
5442 * it needs to be vput()'d.
5443 */
5444 if (nvpp != NULL && *nvpp != NULL) {
5445 vput(*nvpp);
5446 *nvpp = NULL;
5447 }
5448 }
5449 return (error);
5450 }
5451
5452 /*
5453 * Set the extended attribute for the Change attribute.
5454 */
5455 static int
nfsrv_setextattr(struct vnode * vp,struct nfsvattr * nap,NFSPROC_T * p)5456 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5457 {
5458 struct pnfsdsattr dsattr;
5459 int error;
5460
5461 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5462 dsattr.dsa_filerev = nap->na_filerev;
5463 dsattr.dsa_size = nap->na_size;
5464 dsattr.dsa_atime = nap->na_atime;
5465 dsattr.dsa_mtime = nap->na_mtime;
5466 dsattr.dsa_bytes = nap->na_bytes;
5467 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5468 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5469 if (error != 0)
5470 printf("pNFS: setextattr=%d\n", error);
5471 return (error);
5472 }
5473
5474 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)5475 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5476 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5477 {
5478 uint32_t *tl;
5479 struct nfsrv_descript *nd;
5480 nfsv4stateid_t st;
5481 struct mbuf *m, *m2;
5482 int error = 0, retlen, tlen, trimlen;
5483
5484 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5485 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5486 *mpp = NULL;
5487 /*
5488 * Use a stateid where other is an alternating 01010 pattern and
5489 * seqid is 0xffffffff. This value is not defined as special by
5490 * the RFC and is used by the FreeBSD NFS server to indicate an
5491 * MDS->DS proxy operation.
5492 */
5493 st.other[0] = 0x55555555;
5494 st.other[1] = 0x55555555;
5495 st.other[2] = 0x55555555;
5496 st.seqid = 0xffffffff;
5497 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5498 NULL, NULL, 0, 0, cred);
5499 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5500 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5501 txdr_hyper(off, tl);
5502 *(tl + 2) = txdr_unsigned(len);
5503 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5504 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5505 if (error != 0) {
5506 free(nd, M_TEMP);
5507 return (error);
5508 }
5509 if (nd->nd_repstat == 0) {
5510 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5511 NFSM_STRSIZ(retlen, len);
5512 if (retlen > 0) {
5513 /* Trim off the pre-data XDR from the mbuf chain. */
5514 m = nd->nd_mrep;
5515 while (m != NULL && m != nd->nd_md) {
5516 if (m->m_next == nd->nd_md) {
5517 m->m_next = NULL;
5518 m_freem(nd->nd_mrep);
5519 nd->nd_mrep = m = nd->nd_md;
5520 } else
5521 m = m->m_next;
5522 }
5523 if (m == NULL) {
5524 printf("nfsrv_readdsrpc: busted mbuf list\n");
5525 error = ENOENT;
5526 goto nfsmout;
5527 }
5528
5529 /*
5530 * Now, adjust first mbuf so that any XDR before the
5531 * read data is skipped over.
5532 */
5533 trimlen = nd->nd_dpos - mtod(m, char *);
5534 if (trimlen > 0) {
5535 m->m_len -= trimlen;
5536 NFSM_DATAP(m, trimlen);
5537 }
5538
5539 /*
5540 * Truncate the mbuf chain at retlen bytes of data,
5541 * plus XDR padding that brings the length up to a
5542 * multiple of 4.
5543 */
5544 tlen = NFSM_RNDUP(retlen);
5545 do {
5546 if (m->m_len >= tlen) {
5547 m->m_len = tlen;
5548 tlen = 0;
5549 m2 = m->m_next;
5550 m->m_next = NULL;
5551 m_freem(m2);
5552 break;
5553 }
5554 tlen -= m->m_len;
5555 m = m->m_next;
5556 } while (m != NULL);
5557 if (tlen > 0) {
5558 printf("nfsrv_readdsrpc: busted mbuf list\n");
5559 error = ENOENT;
5560 goto nfsmout;
5561 }
5562 *mpp = nd->nd_mrep;
5563 *mpendp = m;
5564 nd->nd_mrep = NULL;
5565 }
5566 } else
5567 error = nd->nd_repstat;
5568 nfsmout:
5569 /* If nd->nd_mrep is already NULL, this is a no-op. */
5570 m_freem(nd->nd_mrep);
5571 free(nd, M_TEMP);
5572 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5573 return (error);
5574 }
5575
5576 /*
5577 * Do a write RPC on a DS data file, using this structure for the arguments,
5578 * so that this function can be executed by a separate kernel process.
5579 */
5580 struct nfsrvwritedsdorpc {
5581 int done;
5582 int inprog;
5583 struct task tsk;
5584 fhandle_t fh;
5585 off_t off;
5586 int len;
5587 struct nfsmount *nmp;
5588 struct ucred *cred;
5589 NFSPROC_T *p;
5590 struct mbuf *m;
5591 int err;
5592 };
5593
5594 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)5595 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5596 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5597 {
5598 uint32_t *tl;
5599 struct nfsrv_descript *nd;
5600 nfsattrbit_t attrbits;
5601 nfsv4stateid_t st;
5602 int commit, error, retlen;
5603
5604 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5605 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5606 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5607
5608 /*
5609 * Use a stateid where other is an alternating 01010 pattern and
5610 * seqid is 0xffffffff. This value is not defined as special by
5611 * the RFC and is used by the FreeBSD NFS server to indicate an
5612 * MDS->DS proxy operation.
5613 */
5614 st.other[0] = 0x55555555;
5615 st.other[1] = 0x55555555;
5616 st.other[2] = 0x55555555;
5617 st.seqid = 0xffffffff;
5618 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5619 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5620 txdr_hyper(off, tl);
5621 tl += 2;
5622 /*
5623 * Do all writes FileSync, since the server doesn't hold onto dirty
5624 * buffers. Since clients should be accessing the DS servers directly
5625 * using the pNFS layouts, this just needs to work correctly as a
5626 * fallback.
5627 */
5628 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5629 *tl = txdr_unsigned(len);
5630 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5631
5632 /* Put data in mbuf chain. */
5633 nd->nd_mb->m_next = m;
5634
5635 /* Set nd_mb and nd_bpos to end of data. */
5636 while (m->m_next != NULL)
5637 m = m->m_next;
5638 nd->nd_mb = m;
5639 nfsm_set(nd, m->m_len);
5640 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5641
5642 /* Do a Getattr for the attributes that change upon writing. */
5643 NFSZERO_ATTRBIT(&attrbits);
5644 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5645 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5646 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5647 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5648 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5649 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5650 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5651 (void) nfsrv_putattrbit(nd, &attrbits);
5652 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5653 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5654 if (error != 0) {
5655 free(nd, M_TEMP);
5656 return (error);
5657 }
5658 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5659 /* Get rid of weak cache consistency data for now. */
5660 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5661 (ND_NFSV4 | ND_V4WCCATTR)) {
5662 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5663 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
5664 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5665 if (error != 0)
5666 goto nfsmout;
5667 /*
5668 * Get rid of Op# and status for next op.
5669 */
5670 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5671 if (*++tl != 0)
5672 nd->nd_flag |= ND_NOMOREDATA;
5673 }
5674 if (nd->nd_repstat == 0) {
5675 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5676 retlen = fxdr_unsigned(int, *tl++);
5677 commit = fxdr_unsigned(int, *tl);
5678 if (commit != NFSWRITE_FILESYNC)
5679 error = NFSERR_IO;
5680 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5681 retlen, commit, error);
5682 } else
5683 error = nd->nd_repstat;
5684 /* We have no use for the Write Verifier since we use FileSync. */
5685
5686 /*
5687 * Get the Change, Size, Access Time and Modify Time attributes and set
5688 * on the Metadata file, so its attributes will be what the file's
5689 * would be if it had been written.
5690 */
5691 if (error == 0) {
5692 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5693 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5694 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
5695 }
5696 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5697 nfsmout:
5698 m_freem(nd->nd_mrep);
5699 free(nd, M_TEMP);
5700 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5701 return (error);
5702 }
5703
5704 /*
5705 * Start up the thread that will execute nfsrv_writedsdorpc().
5706 */
5707 static void
start_writedsdorpc(void * arg,int pending)5708 start_writedsdorpc(void *arg, int pending)
5709 {
5710 struct nfsrvwritedsdorpc *drpc;
5711
5712 drpc = (struct nfsrvwritedsdorpc *)arg;
5713 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5714 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5715 drpc->done = 1;
5716 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5717 }
5718
5719 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)5720 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5721 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5722 struct mbuf **mpp, char *cp, int *failposp)
5723 {
5724 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5725 struct nfsvattr na;
5726 struct mbuf *m;
5727 int error, i, offs, ret, timo;
5728
5729 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5730 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5731 drpc = NULL;
5732 if (mirrorcnt > 1)
5733 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5734 M_WAITOK);
5735
5736 /* Calculate offset in mbuf chain that data starts. */
5737 offs = cp - mtod(*mpp, char *);
5738 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5739
5740 /*
5741 * Do the write RPC for every DS, using a separate kernel process
5742 * for every DS except the last one.
5743 */
5744 error = 0;
5745 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5746 tdrpc->done = 0;
5747 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5748 tdrpc->off = off;
5749 tdrpc->len = len;
5750 tdrpc->nmp = *nmpp;
5751 tdrpc->cred = cred;
5752 tdrpc->p = p;
5753 tdrpc->inprog = 0;
5754 tdrpc->err = 0;
5755 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5756 ret = EIO;
5757 if (nfs_pnfsiothreads != 0) {
5758 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5759 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5760 ret);
5761 }
5762 if (ret != 0) {
5763 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5764 tdrpc->m, cred, p);
5765 if (nfsds_failerr(ret) && *failposp == -1)
5766 *failposp = i;
5767 else if (error == 0 && ret != 0)
5768 error = ret;
5769 }
5770 nmpp++;
5771 fhp++;
5772 }
5773 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5774 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5775 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5776 *failposp = mirrorcnt - 1;
5777 else if (error == 0 && ret != 0)
5778 error = ret;
5779 if (error == 0)
5780 error = nfsrv_setextattr(vp, &na, p);
5781 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5782 tdrpc = drpc;
5783 timo = hz / 50; /* Wait for 20msec. */
5784 if (timo < 1)
5785 timo = 1;
5786 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5787 /* Wait for RPCs on separate threads to complete. */
5788 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5789 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5790 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5791 *failposp = i;
5792 else if (error == 0 && tdrpc->err != 0)
5793 error = tdrpc->err;
5794 }
5795 free(drpc, M_TEMP);
5796 return (error);
5797 }
5798
5799 /*
5800 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5801 * so that this function can be executed by a separate kernel process.
5802 */
5803 struct nfsrvallocatedsdorpc {
5804 int done;
5805 int inprog;
5806 struct task tsk;
5807 fhandle_t fh;
5808 off_t off;
5809 off_t len;
5810 struct nfsmount *nmp;
5811 struct ucred *cred;
5812 NFSPROC_T *p;
5813 int err;
5814 };
5815
5816 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)5817 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5818 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5819 {
5820 uint32_t *tl;
5821 struct nfsrv_descript *nd;
5822 nfsattrbit_t attrbits;
5823 nfsv4stateid_t st;
5824 int error;
5825
5826 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5827 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5828 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5829
5830 /*
5831 * Use a stateid where other is an alternating 01010 pattern and
5832 * seqid is 0xffffffff. This value is not defined as special by
5833 * the RFC and is used by the FreeBSD NFS server to indicate an
5834 * MDS->DS proxy operation.
5835 */
5836 st.other[0] = 0x55555555;
5837 st.other[1] = 0x55555555;
5838 st.other[2] = 0x55555555;
5839 st.seqid = 0xffffffff;
5840 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5841 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5842 txdr_hyper(off, tl); tl += 2;
5843 txdr_hyper(len, tl); tl += 2;
5844 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5845
5846 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5847 NFSGETATTR_ATTRBIT(&attrbits);
5848 nfsrv_putattrbit(nd, &attrbits);
5849 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5850 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5851 if (error != 0) {
5852 free(nd, M_TEMP);
5853 return (error);
5854 }
5855 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5856 nd->nd_repstat);
5857 if (nd->nd_repstat == 0) {
5858 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5859 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5860 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
5861 } else
5862 error = nd->nd_repstat;
5863 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5864 nfsmout:
5865 m_freem(nd->nd_mrep);
5866 free(nd, M_TEMP);
5867 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5868 return (error);
5869 }
5870
5871 /*
5872 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5873 */
5874 static void
start_allocatedsdorpc(void * arg,int pending)5875 start_allocatedsdorpc(void *arg, int pending)
5876 {
5877 struct nfsrvallocatedsdorpc *drpc;
5878
5879 drpc = (struct nfsrvallocatedsdorpc *)arg;
5880 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5881 drpc->len, NULL, drpc->cred, drpc->p);
5882 drpc->done = 1;
5883 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5884 }
5885
5886 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)5887 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5888 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5889 int *failposp)
5890 {
5891 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5892 struct nfsvattr na;
5893 int error, i, ret, timo;
5894
5895 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5896 drpc = NULL;
5897 if (mirrorcnt > 1)
5898 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5899 M_WAITOK);
5900
5901 /*
5902 * Do the allocate RPC for every DS, using a separate kernel process
5903 * for every DS except the last one.
5904 */
5905 error = 0;
5906 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5907 tdrpc->done = 0;
5908 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5909 tdrpc->off = off;
5910 tdrpc->len = len;
5911 tdrpc->nmp = *nmpp;
5912 tdrpc->cred = cred;
5913 tdrpc->p = p;
5914 tdrpc->inprog = 0;
5915 tdrpc->err = 0;
5916 ret = EIO;
5917 if (nfs_pnfsiothreads != 0) {
5918 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5919 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5920 ret);
5921 }
5922 if (ret != 0) {
5923 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5924 cred, p);
5925 if (nfsds_failerr(ret) && *failposp == -1)
5926 *failposp = i;
5927 else if (error == 0 && ret != 0)
5928 error = ret;
5929 }
5930 nmpp++;
5931 fhp++;
5932 }
5933 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5934 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5935 *failposp = mirrorcnt - 1;
5936 else if (error == 0 && ret != 0)
5937 error = ret;
5938 if (error == 0)
5939 error = nfsrv_setextattr(vp, &na, p);
5940 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5941 tdrpc = drpc;
5942 timo = hz / 50; /* Wait for 20msec. */
5943 if (timo < 1)
5944 timo = 1;
5945 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5946 /* Wait for RPCs on separate threads to complete. */
5947 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5948 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5949 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5950 *failposp = i;
5951 else if (error == 0 && tdrpc->err != 0)
5952 error = tdrpc->err;
5953 }
5954 free(drpc, M_TEMP);
5955 return (error);
5956 }
5957
5958 /*
5959 * Do a deallocate RPC on a DS data file, using this structure for the
5960 * arguments, so that this function can be executed by a separate kernel
5961 * process.
5962 */
5963 struct nfsrvdeallocatedsdorpc {
5964 int done;
5965 int inprog;
5966 struct task tsk;
5967 fhandle_t fh;
5968 off_t off;
5969 off_t len;
5970 struct nfsmount *nmp;
5971 struct ucred *cred;
5972 NFSPROC_T *p;
5973 int err;
5974 };
5975
5976 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)5977 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5978 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5979 {
5980 uint32_t *tl;
5981 struct nfsrv_descript *nd;
5982 nfsattrbit_t attrbits;
5983 nfsv4stateid_t st;
5984 int error;
5985
5986 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5987 nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp,
5988 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5989
5990 /*
5991 * Use a stateid where other is an alternating 01010 pattern and
5992 * seqid is 0xffffffff. This value is not defined as special by
5993 * the RFC and is used by the FreeBSD NFS server to indicate an
5994 * MDS->DS proxy operation.
5995 */
5996 st.other[0] = 0x55555555;
5997 st.other[1] = 0x55555555;
5998 st.other[2] = 0x55555555;
5999 st.seqid = 0xffffffff;
6000 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6001 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
6002 txdr_hyper(off, tl); tl += 2;
6003 txdr_hyper(len, tl); tl += 2;
6004 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len);
6005
6006 /* Do a Getattr for the attributes that change upon writing. */
6007 NFSZERO_ATTRBIT(&attrbits);
6008 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6009 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6010 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6011 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6012 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6013 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6014 nfsrv_putattrbit(nd, &attrbits);
6015 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
6016 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6017 if (error != 0) {
6018 free(nd, M_TEMP);
6019 return (error);
6020 }
6021 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n",
6022 nd->nd_repstat);
6023 /* Get rid of weak cache consistency data for now. */
6024 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
6025 (ND_NFSV4 | ND_V4WCCATTR)) {
6026 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
6027 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
6028 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error);
6029 if (error != 0)
6030 goto nfsmout;
6031 /*
6032 * Get rid of Op# and status for next op.
6033 */
6034 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6035 if (*++tl != 0)
6036 nd->nd_flag |= ND_NOMOREDATA;
6037 }
6038 if (nd->nd_repstat == 0) {
6039 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6040 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
6041 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
6042 } else
6043 error = nd->nd_repstat;
6044 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error);
6045 nfsmout:
6046 m_freem(nd->nd_mrep);
6047 free(nd, M_TEMP);
6048 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error);
6049 return (error);
6050 }
6051
6052 /*
6053 * Start up the thread that will execute nfsrv_deallocatedsdorpc().
6054 */
6055 static void
start_deallocatedsdorpc(void * arg,int pending)6056 start_deallocatedsdorpc(void *arg, int pending)
6057 {
6058 struct nfsrvdeallocatedsdorpc *drpc;
6059
6060 drpc = (struct nfsrvdeallocatedsdorpc *)arg;
6061 drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
6062 drpc->len, NULL, drpc->cred, drpc->p);
6063 drpc->done = 1;
6064 NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err);
6065 }
6066
6067 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)6068 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
6069 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6070 int *failposp)
6071 {
6072 struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL;
6073 struct nfsvattr na;
6074 int error, i, ret, timo;
6075
6076 NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n");
6077 drpc = NULL;
6078 if (mirrorcnt > 1)
6079 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6080 M_WAITOK);
6081
6082 /*
6083 * Do the deallocate RPC for every DS, using a separate kernel process
6084 * for every DS except the last one.
6085 */
6086 error = 0;
6087 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6088 tdrpc->done = 0;
6089 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6090 tdrpc->off = off;
6091 tdrpc->len = len;
6092 tdrpc->nmp = *nmpp;
6093 tdrpc->cred = cred;
6094 tdrpc->p = p;
6095 tdrpc->inprog = 0;
6096 tdrpc->err = 0;
6097 ret = EIO;
6098 if (nfs_pnfsiothreads != 0) {
6099 ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc);
6100 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n",
6101 ret);
6102 }
6103 if (ret != 0) {
6104 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len,
6105 NULL, cred, p);
6106 if (nfsds_failerr(ret) && *failposp == -1)
6107 *failposp = i;
6108 else if (error == 0 && ret != 0)
6109 error = ret;
6110 }
6111 nmpp++;
6112 fhp++;
6113 }
6114 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
6115 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6116 *failposp = mirrorcnt - 1;
6117 else if (error == 0 && ret != 0)
6118 error = ret;
6119 if (error == 0)
6120 error = nfsrv_setextattr(vp, &na, p);
6121 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error);
6122 tdrpc = drpc;
6123 timo = hz / 50; /* Wait for 20msec. */
6124 if (timo < 1)
6125 timo = 1;
6126 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6127 /* Wait for RPCs on separate threads to complete. */
6128 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6129 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
6130 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6131 *failposp = i;
6132 else if (error == 0 && tdrpc->err != 0)
6133 error = tdrpc->err;
6134 }
6135 free(drpc, M_TEMP);
6136 return (error);
6137 }
6138
6139 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)6140 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6141 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
6142 struct nfsvattr *dsnap)
6143 {
6144 uint32_t *tl;
6145 struct nfsrv_descript *nd;
6146 nfsv4stateid_t st;
6147 nfsattrbit_t attrbits;
6148 int error;
6149
6150 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
6151 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6152 /*
6153 * Use a stateid where other is an alternating 01010 pattern and
6154 * seqid is 0xffffffff. This value is not defined as special by
6155 * the RFC and is used by the FreeBSD NFS server to indicate an
6156 * MDS->DS proxy operation.
6157 */
6158 st.other[0] = 0x55555555;
6159 st.other[1] = 0x55555555;
6160 st.other[2] = 0x55555555;
6161 st.seqid = 0xffffffff;
6162 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6163 NULL, NULL, 0, 0, cred);
6164 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6165 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
6166
6167 /* Do a Getattr for the attributes that change due to writing. */
6168 NFSZERO_ATTRBIT(&attrbits);
6169 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6170 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6171 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6172 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6173 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6174 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
6175 *tl = txdr_unsigned(NFSV4OP_GETATTR);
6176 (void) nfsrv_putattrbit(nd, &attrbits);
6177 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6178 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6179 if (error != 0) {
6180 free(nd, M_TEMP);
6181 return (error);
6182 }
6183 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
6184 nd->nd_repstat);
6185 /* Get rid of weak cache consistency data for now. */
6186 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
6187 (ND_NFSV4 | ND_V4WCCATTR)) {
6188 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
6189 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
6190 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
6191 if (error != 0)
6192 goto nfsmout;
6193 /*
6194 * Get rid of Op# and status for next op.
6195 */
6196 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6197 if (*++tl != 0)
6198 nd->nd_flag |= ND_NOMOREDATA;
6199 }
6200 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
6201 if (error != 0)
6202 goto nfsmout;
6203 if (nd->nd_repstat != 0)
6204 error = nd->nd_repstat;
6205 /*
6206 * Get the Change, Size, Access Time and Modify Time attributes and set
6207 * on the Metadata file, so its attributes will be what the file's
6208 * would be if it had been written.
6209 */
6210 if (error == 0) {
6211 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6212 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
6213 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL,
6214 NULL);
6215 }
6216 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
6217 nfsmout:
6218 m_freem(nd->nd_mrep);
6219 free(nd, M_TEMP);
6220 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
6221 return (error);
6222 }
6223
6224 struct nfsrvsetattrdsdorpc {
6225 int done;
6226 int inprog;
6227 struct task tsk;
6228 fhandle_t fh;
6229 struct nfsmount *nmp;
6230 struct vnode *vp;
6231 struct ucred *cred;
6232 NFSPROC_T *p;
6233 struct nfsvattr na;
6234 struct nfsvattr dsna;
6235 int err;
6236 };
6237
6238 /*
6239 * Start up the thread that will execute nfsrv_setattrdsdorpc().
6240 */
6241 static void
start_setattrdsdorpc(void * arg,int pending)6242 start_setattrdsdorpc(void *arg, int pending)
6243 {
6244 struct nfsrvsetattrdsdorpc *drpc;
6245
6246 drpc = (struct nfsrvsetattrdsdorpc *)arg;
6247 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
6248 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
6249 drpc->done = 1;
6250 }
6251
6252 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)6253 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6254 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6255 struct nfsvattr *nap, int *failposp)
6256 {
6257 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
6258 struct nfsvattr na;
6259 int error, i, ret, timo;
6260
6261 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
6262 drpc = NULL;
6263 if (mirrorcnt > 1)
6264 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6265 M_WAITOK);
6266
6267 /*
6268 * Do the setattr RPC for every DS, using a separate kernel process
6269 * for every DS except the last one.
6270 */
6271 error = 0;
6272 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6273 tdrpc->done = 0;
6274 tdrpc->inprog = 0;
6275 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6276 tdrpc->nmp = *nmpp;
6277 tdrpc->vp = vp;
6278 tdrpc->cred = cred;
6279 tdrpc->p = p;
6280 tdrpc->na = *nap;
6281 tdrpc->err = 0;
6282 ret = EIO;
6283 if (nfs_pnfsiothreads != 0) {
6284 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
6285 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
6286 ret);
6287 }
6288 if (ret != 0) {
6289 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
6290 &na);
6291 if (nfsds_failerr(ret) && *failposp == -1)
6292 *failposp = i;
6293 else if (error == 0 && ret != 0)
6294 error = ret;
6295 }
6296 nmpp++;
6297 fhp++;
6298 }
6299 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
6300 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6301 *failposp = mirrorcnt - 1;
6302 else if (error == 0 && ret != 0)
6303 error = ret;
6304 if (error == 0)
6305 error = nfsrv_setextattr(vp, &na, p);
6306 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
6307 tdrpc = drpc;
6308 timo = hz / 50; /* Wait for 20msec. */
6309 if (timo < 1)
6310 timo = 1;
6311 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6312 /* Wait for RPCs on separate threads to complete. */
6313 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6314 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
6315 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6316 *failposp = i;
6317 else if (error == 0 && tdrpc->err != 0)
6318 error = tdrpc->err;
6319 }
6320 free(drpc, M_TEMP);
6321 return (error);
6322 }
6323
6324 /*
6325 * Do a Setattr of an NFSv4 ACL on the DS file.
6326 */
6327 static int
nfsrv_setacldsdorpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct acl * aclp)6328 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6329 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
6330 {
6331 struct nfsrv_descript *nd;
6332 nfsv4stateid_t st;
6333 nfsattrbit_t attrbits;
6334 int error;
6335
6336 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
6337 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6338 /*
6339 * Use a stateid where other is an alternating 01010 pattern and
6340 * seqid is 0xffffffff. This value is not defined as special by
6341 * the RFC and is used by the FreeBSD NFS server to indicate an
6342 * MDS->DS proxy operation.
6343 */
6344 st.other[0] = 0x55555555;
6345 st.other[1] = 0x55555555;
6346 st.other[2] = 0x55555555;
6347 st.seqid = 0xffffffff;
6348 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6349 NULL, NULL, 0, 0, cred);
6350 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6351 NFSZERO_ATTRBIT(&attrbits);
6352 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
6353 /*
6354 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
6355 * so passing in the metadata "vp" will be ok, since it is of
6356 * the same type (VREG).
6357 */
6358 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
6359 NULL, 0, 0, 0, 0, 0, NULL);
6360 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6361 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6362 if (error != 0) {
6363 free(nd, M_TEMP);
6364 return (error);
6365 }
6366 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
6367 nd->nd_repstat);
6368 error = nd->nd_repstat;
6369 m_freem(nd->nd_mrep);
6370 free(nd, M_TEMP);
6371 return (error);
6372 }
6373
6374 struct nfsrvsetacldsdorpc {
6375 int done;
6376 int inprog;
6377 struct task tsk;
6378 fhandle_t fh;
6379 struct nfsmount *nmp;
6380 struct vnode *vp;
6381 struct ucred *cred;
6382 NFSPROC_T *p;
6383 struct acl *aclp;
6384 int err;
6385 };
6386
6387 /*
6388 * Start up the thread that will execute nfsrv_setacldsdorpc().
6389 */
6390 static void
start_setacldsdorpc(void * arg,int pending)6391 start_setacldsdorpc(void *arg, int pending)
6392 {
6393 struct nfsrvsetacldsdorpc *drpc;
6394
6395 drpc = (struct nfsrvsetacldsdorpc *)arg;
6396 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
6397 drpc->vp, drpc->nmp, drpc->aclp);
6398 drpc->done = 1;
6399 }
6400
6401 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)6402 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6403 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
6404 int *failposp)
6405 {
6406 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
6407 int error, i, ret, timo;
6408
6409 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
6410 drpc = NULL;
6411 if (mirrorcnt > 1)
6412 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6413 M_WAITOK);
6414
6415 /*
6416 * Do the setattr RPC for every DS, using a separate kernel process
6417 * for every DS except the last one.
6418 */
6419 error = 0;
6420 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6421 tdrpc->done = 0;
6422 tdrpc->inprog = 0;
6423 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6424 tdrpc->nmp = *nmpp;
6425 tdrpc->vp = vp;
6426 tdrpc->cred = cred;
6427 tdrpc->p = p;
6428 tdrpc->aclp = aclp;
6429 tdrpc->err = 0;
6430 ret = EIO;
6431 if (nfs_pnfsiothreads != 0) {
6432 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
6433 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
6434 ret);
6435 }
6436 if (ret != 0) {
6437 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
6438 aclp);
6439 if (nfsds_failerr(ret) && *failposp == -1)
6440 *failposp = i;
6441 else if (error == 0 && ret != 0)
6442 error = ret;
6443 }
6444 nmpp++;
6445 fhp++;
6446 }
6447 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
6448 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6449 *failposp = mirrorcnt - 1;
6450 else if (error == 0 && ret != 0)
6451 error = ret;
6452 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
6453 tdrpc = drpc;
6454 timo = hz / 50; /* Wait for 20msec. */
6455 if (timo < 1)
6456 timo = 1;
6457 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6458 /* Wait for RPCs on separate threads to complete. */
6459 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6460 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6461 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6462 *failposp = i;
6463 else if (error == 0 && tdrpc->err != 0)
6464 error = tdrpc->err;
6465 }
6466 free(drpc, M_TEMP);
6467 return (error);
6468 }
6469
6470 /*
6471 * Getattr call to the DS for the attributes that change due to writing.
6472 */
6473 static int
nfsrv_getattrdsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct nfsvattr * nap)6474 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6475 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6476 {
6477 struct nfsrv_descript *nd;
6478 int error;
6479 nfsattrbit_t attrbits;
6480
6481 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6482 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6483 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6484 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6485 NFSZERO_ATTRBIT(&attrbits);
6486 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6487 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6488 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6489 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6490 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6491 (void) nfsrv_putattrbit(nd, &attrbits);
6492 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6493 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6494 if (error != 0) {
6495 free(nd, M_TEMP);
6496 return (error);
6497 }
6498 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6499 nd->nd_repstat);
6500 if (nd->nd_repstat == 0) {
6501 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6502 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6503 NULL, NULL, NULL);
6504 /*
6505 * We can only save the updated values in the extended
6506 * attribute if the vp is exclusively locked.
6507 * This should happen when any of the following operations
6508 * occur on the vnode:
6509 * Close, Delegreturn, LayoutCommit, LayoutReturn
6510 * As such, the updated extended attribute should get saved
6511 * before nfsrv_checkdsattr() returns 0 and allows the cached
6512 * attributes to be returned without calling this function.
6513 */
6514 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6515 error = nfsrv_setextattr(vp, nap, p);
6516 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6517 error);
6518 }
6519 } else
6520 error = nd->nd_repstat;
6521 m_freem(nd->nd_mrep);
6522 free(nd, M_TEMP);
6523 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6524 return (error);
6525 }
6526
6527 /*
6528 * Seek call to a DS.
6529 */
6530 static int
nfsrv_seekdsrpc(fhandle_t * fhp,off_t * offp,int content,bool * eofp,struct ucred * cred,NFSPROC_T * p,struct nfsmount * nmp)6531 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6532 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6533 {
6534 uint32_t *tl;
6535 struct nfsrv_descript *nd;
6536 nfsv4stateid_t st;
6537 int error;
6538
6539 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6540 /*
6541 * Use a stateid where other is an alternating 01010 pattern and
6542 * seqid is 0xffffffff. This value is not defined as special by
6543 * the RFC and is used by the FreeBSD NFS server to indicate an
6544 * MDS->DS proxy operation.
6545 */
6546 st.other[0] = 0x55555555;
6547 st.other[1] = 0x55555555;
6548 st.other[2] = 0x55555555;
6549 st.seqid = 0xffffffff;
6550 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6551 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6552 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6553 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6554 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6555 txdr_hyper(*offp, tl); tl += 2;
6556 *tl = txdr_unsigned(content);
6557 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6558 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6559 if (error != 0) {
6560 free(nd, M_TEMP);
6561 return (error);
6562 }
6563 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6564 if (nd->nd_repstat == 0) {
6565 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6566 if (*tl++ == newnfs_true)
6567 *eofp = true;
6568 else
6569 *eofp = false;
6570 *offp = fxdr_hyper(tl);
6571 } else
6572 error = nd->nd_repstat;
6573 nfsmout:
6574 m_freem(nd->nd_mrep);
6575 free(nd, M_TEMP);
6576 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6577 return (error);
6578 }
6579
6580 /*
6581 * Get the device id and file handle for a DS file.
6582 */
6583 int
nfsrv_dsgetdevandfh(struct vnode * vp,NFSPROC_T * p,int * mirrorcntp,fhandle_t * fhp,char * devid)6584 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6585 fhandle_t *fhp, char *devid)
6586 {
6587 int buflen, error;
6588 char *buf;
6589
6590 buflen = 1024;
6591 buf = malloc(buflen, M_TEMP, M_WAITOK);
6592 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6593 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6594 free(buf, M_TEMP);
6595 return (error);
6596 }
6597
6598 /*
6599 * Do a Lookup against the DS for the filename.
6600 */
6601 static int
nfsrv_pnfslookupds(struct vnode * vp,struct vnode * dvp,struct pnfsdsfile * pf,struct vnode ** nvpp,NFSPROC_T * p)6602 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6603 struct vnode **nvpp, NFSPROC_T *p)
6604 {
6605 struct nameidata named;
6606 struct ucred *tcred;
6607 char *bufp;
6608 u_long *hashp;
6609 struct vnode *nvp;
6610 int error;
6611
6612 tcred = newnfs_getcred();
6613 named.ni_cnd.cn_nameiop = LOOKUP;
6614 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6615 named.ni_cnd.cn_cred = tcred;
6616 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
6617 nfsvno_setpathbuf(&named, &bufp, &hashp);
6618 named.ni_cnd.cn_nameptr = bufp;
6619 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6620 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6621 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6622 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6623 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6624 NFSFREECRED(tcred);
6625 nfsvno_relpathbuf(&named);
6626 if (error == 0)
6627 *nvpp = nvp;
6628 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6629 return (error);
6630 }
6631
6632 /*
6633 * Set the file handle to the correct one.
6634 */
6635 static void
nfsrv_pnfssetfh(struct vnode * vp,struct pnfsdsfile * pf,char * devid,char * fnamep,struct vnode * nvp,NFSPROC_T * p)6636 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6637 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6638 {
6639 struct nfsnode *np;
6640 int ret = 0;
6641
6642 np = VTONFS(nvp);
6643 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6644 /*
6645 * We can only do a vn_set_extattr() if the vnode is exclusively
6646 * locked and vn_start_write() has been done. If devid != NULL or
6647 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6648 * may not have been done.
6649 * If not done now, it will be done on a future call.
6650 */
6651 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6652 LK_EXCLUSIVE)
6653 ret = vn_extattr_set(vp, IO_NODELOCKED,
6654 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6655 (char *)pf, p);
6656 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6657 }
6658
6659 /*
6660 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6661 * when the DS has failed.
6662 */
6663 void
nfsrv_killrpcs(struct nfsmount * nmp)6664 nfsrv_killrpcs(struct nfsmount *nmp)
6665 {
6666
6667 /*
6668 * Call newnfs_nmcancelreqs() to cause
6669 * any RPCs in progress on the mount point to
6670 * fail.
6671 * This will cause any process waiting for an
6672 * RPC to complete while holding a vnode lock
6673 * on the mounted-on vnode (such as "df" or
6674 * a non-forced "umount") to fail.
6675 * This will unlock the mounted-on vnode so
6676 * a forced dismount can succeed.
6677 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6678 * called.
6679 */
6680 newnfs_nmcancelreqs(nmp);
6681 }
6682
6683 /*
6684 * Sum up the statfs info for each of the DSs, so that the client will
6685 * receive the total for all DSs.
6686 */
6687 static int
nfsrv_pnfsstatfs(struct statfs * sf,struct mount * mp)6688 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6689 {
6690 struct statfs *tsf;
6691 struct nfsdevice *ds;
6692 struct vnode **dvpp, **tdvpp, *dvp;
6693 uint64_t tot;
6694 int cnt, error = 0, i;
6695
6696 if (nfsrv_devidcnt <= 0)
6697 return (ENXIO);
6698 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6699 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6700
6701 /* Get an array of the dvps for the DSs. */
6702 tdvpp = dvpp;
6703 i = 0;
6704 NFSDDSLOCK();
6705 /* First, search for matches for same file system. */
6706 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6707 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6708 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6709 if (++i > nfsrv_devidcnt)
6710 break;
6711 *tdvpp++ = ds->nfsdev_dvp;
6712 }
6713 }
6714 /*
6715 * If no matches for same file system, total all servers not assigned
6716 * to a file system.
6717 */
6718 if (i == 0) {
6719 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6720 if (ds->nfsdev_nmp != NULL &&
6721 ds->nfsdev_mdsisset == 0) {
6722 if (++i > nfsrv_devidcnt)
6723 break;
6724 *tdvpp++ = ds->nfsdev_dvp;
6725 }
6726 }
6727 }
6728 NFSDDSUNLOCK();
6729 cnt = i;
6730
6731 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6732 tdvpp = dvpp;
6733 for (i = 0; i < cnt && error == 0; i++) {
6734 dvp = *tdvpp++;
6735 error = VFS_STATFS(dvp->v_mount, tsf);
6736 if (error == 0) {
6737 if (sf->f_bsize == 0) {
6738 if (tsf->f_bsize > 0)
6739 sf->f_bsize = tsf->f_bsize;
6740 else
6741 sf->f_bsize = 8192;
6742 }
6743 if (tsf->f_blocks > 0) {
6744 if (sf->f_bsize != tsf->f_bsize) {
6745 tot = tsf->f_blocks * tsf->f_bsize;
6746 sf->f_blocks += (tot / sf->f_bsize);
6747 } else
6748 sf->f_blocks += tsf->f_blocks;
6749 }
6750 if (tsf->f_bfree > 0) {
6751 if (sf->f_bsize != tsf->f_bsize) {
6752 tot = tsf->f_bfree * tsf->f_bsize;
6753 sf->f_bfree += (tot / sf->f_bsize);
6754 } else
6755 sf->f_bfree += tsf->f_bfree;
6756 }
6757 if (tsf->f_bavail > 0) {
6758 if (sf->f_bsize != tsf->f_bsize) {
6759 tot = tsf->f_bavail * tsf->f_bsize;
6760 sf->f_bavail += (tot / sf->f_bsize);
6761 } else
6762 sf->f_bavail += tsf->f_bavail;
6763 }
6764 }
6765 }
6766 free(tsf, M_TEMP);
6767 free(dvpp, M_TEMP);
6768 return (error);
6769 }
6770
6771 /*
6772 * Set an NFSv4 acl.
6773 */
6774 int
nfsrv_setacl(struct vnode * vp,NFSACL_T * aclp,struct ucred * cred,NFSPROC_T * p)6775 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6776 {
6777 int error;
6778
6779 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6780 error = NFSERR_ATTRNOTSUPP;
6781 goto out;
6782 }
6783 /*
6784 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6785 * Make sure it has enough room for that - splitting every entry
6786 * into two and appending "canonical six" entries at the end.
6787 * Cribbed out of kern/vfs_acl.c - Rick M.
6788 */
6789 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6790 error = NFSERR_ATTRNOTSUPP;
6791 goto out;
6792 }
6793 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6794 if (error == 0) {
6795 error = nfsrv_dssetacl(vp, aclp, cred, p);
6796 if (error == ENOENT)
6797 error = 0;
6798 }
6799
6800 out:
6801 NFSEXITCODE(error);
6802 return (error);
6803 }
6804
6805 /*
6806 * Seek vnode op call (actually it is a VOP_IOCTL()).
6807 * This function is called with the vnode locked, but unlocks and vrele()s
6808 * the vp before returning.
6809 */
6810 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)6811 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6812 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6813 {
6814 struct nfsvattr at;
6815 int error, ret;
6816
6817 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6818 /*
6819 * Attempt to seek on a DS file. A return of ENOENT implies
6820 * there is no DS file to seek on.
6821 */
6822 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6823 NULL, NULL, NULL, NULL, offp, content, eofp);
6824 if (error != ENOENT) {
6825 vput(vp);
6826 return (error);
6827 }
6828
6829 /*
6830 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6831 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6832 * NFSv4.2 is *eofp == true and error == 0 for this case.
6833 */
6834 NFSVOPUNLOCK(vp);
6835 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6836 *eofp = false;
6837 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6838 /* Handle the cases where we might be at EOF. */
6839 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6840 if (ret == 0 && *offp == at.na_size) {
6841 *eofp = true;
6842 error = 0;
6843 }
6844 if (ret != 0 && error == 0)
6845 error = ret;
6846 }
6847 vrele(vp);
6848 NFSEXITCODE(error);
6849 return (error);
6850 }
6851
6852 /*
6853 * Allocate vnode op call.
6854 */
6855 int
nfsvno_allocate(struct vnode * vp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p)6856 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6857 NFSPROC_T *p)
6858 {
6859 int error;
6860 off_t olen;
6861
6862 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6863 /*
6864 * Attempt to allocate on a DS file. A return of ENOENT implies
6865 * there is no DS file to allocate on.
6866 */
6867 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6868 NULL, NULL, NULL, NULL, &len, 0, NULL);
6869 if (error != ENOENT)
6870 return (error);
6871
6872 /*
6873 * Do the actual VOP_ALLOCATE(), looping so long as
6874 * progress is being made, to achieve completion.
6875 */
6876 do {
6877 olen = len;
6878 error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred);
6879 if (error == 0 && len > 0 && olen > len)
6880 maybe_yield();
6881 } while (error == 0 && len > 0 && olen > len);
6882 if (error == 0 && len > 0)
6883 error = NFSERR_IO;
6884 NFSEXITCODE(error);
6885 return (error);
6886 }
6887
6888 /*
6889 * Deallocate vnode op call.
6890 */
6891 int
nfsvno_deallocate(struct vnode * vp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p)6892 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6893 NFSPROC_T *p)
6894 {
6895 int error;
6896 off_t olen;
6897
6898 ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp");
6899 /*
6900 * Attempt to deallocate on a DS file. A return of ENOENT implies
6901 * there is no DS file to deallocate on.
6902 */
6903 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL,
6904 NULL, NULL, NULL, NULL, &len, 0, NULL);
6905 if (error != ENOENT)
6906 return (error);
6907
6908 /*
6909 * Do the actual VOP_DEALLOCATE(), looping so long as
6910 * progress is being made, to achieve completion.
6911 */
6912 do {
6913 olen = len;
6914 error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred);
6915 if (error == 0 && len > 0 && olen > len)
6916 maybe_yield();
6917 } while (error == 0 && len > 0 && olen > len);
6918 if (error == 0 && len > 0)
6919 error = NFSERR_IO;
6920 NFSEXITCODE(error);
6921 return (error);
6922 }
6923
6924 /*
6925 * Get Extended Atribute vnode op into an mbuf list.
6926 */
6927 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)6928 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6929 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6930 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6931 {
6932 struct iovec *iv;
6933 struct uio io, *uiop = &io;
6934 struct mbuf *m, *m2;
6935 int alen, error, len, tlen;
6936 size_t siz;
6937
6938 /* First, find out the size of the extended attribute. */
6939 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6940 &siz, cred, p);
6941 if (error != 0)
6942 return (NFSERR_NOXATTR);
6943 if (siz > maxresp - NFS_MAXXDR)
6944 return (NFSERR_XATTR2BIG);
6945 len = siz;
6946 tlen = NFSM_RNDUP(len);
6947 if (tlen > 0) {
6948 /*
6949 * If cnt > MCLBYTES and the reply will not be saved, use
6950 * ext_pgs mbufs for TLS.
6951 * For NFSv4.0, we do not know for sure if the reply will
6952 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6953 * Always use ext_pgs mbufs if ND_EXTPG is set.
6954 */
6955 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6956 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6957 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6958 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6959 maxextsiz, &m, &m2, &iv);
6960 else
6961 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6962 &iv);
6963 uiop->uio_iov = iv;
6964 } else {
6965 uiop->uio_iovcnt = 0;
6966 uiop->uio_iov = iv = NULL;
6967 m = m2 = NULL;
6968 }
6969 uiop->uio_offset = 0;
6970 uiop->uio_resid = tlen;
6971 uiop->uio_rw = UIO_READ;
6972 uiop->uio_segflg = UIO_SYSSPACE;
6973 uiop->uio_td = p;
6974 #ifdef MAC
6975 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6976 name);
6977 if (error != 0)
6978 goto out;
6979 #endif
6980
6981 if (tlen > 0)
6982 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6983 NULL, cred, p);
6984 if (error != 0)
6985 goto out;
6986 if (uiop->uio_resid > 0) {
6987 alen = tlen;
6988 len = tlen - uiop->uio_resid;
6989 tlen = NFSM_RNDUP(len);
6990 if (alen != tlen)
6991 printf("nfsvno_getxattr: weird size read\n");
6992 if (tlen == 0) {
6993 m_freem(m);
6994 m = m2 = NULL;
6995 } else if (alen != tlen || tlen != len)
6996 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6997 }
6998 *lenp = len;
6999 *mpp = m;
7000 *mpendp = m2;
7001
7002 out:
7003 if (error != 0) {
7004 if (m != NULL)
7005 m_freem(m);
7006 *lenp = 0;
7007 }
7008 free(iv, M_TEMP);
7009 NFSEXITCODE(error);
7010 return (error);
7011 }
7012
7013 /*
7014 * Set Extended attribute vnode op from an mbuf list.
7015 */
7016 int
nfsvno_setxattr(struct vnode * vp,char * name,int len,struct mbuf * m,char * cp,struct ucred * cred,struct thread * p)7017 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
7018 char *cp, struct ucred *cred, struct thread *p)
7019 {
7020 struct iovec *iv;
7021 struct uio uio, *uiop = &uio;
7022 int cnt, error;
7023
7024 error = 0;
7025 #ifdef MAC
7026 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
7027 name);
7028 #endif
7029 if (error != 0)
7030 goto out;
7031
7032 uiop->uio_rw = UIO_WRITE;
7033 uiop->uio_segflg = UIO_SYSSPACE;
7034 uiop->uio_td = p;
7035 uiop->uio_offset = 0;
7036 uiop->uio_resid = len;
7037 if (len > 0) {
7038 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
7039 uiop->uio_iov = iv;
7040 uiop->uio_iovcnt = cnt;
7041 } else {
7042 uiop->uio_iov = iv = NULL;
7043 uiop->uio_iovcnt = 0;
7044 }
7045 if (error == 0) {
7046 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
7047 cred, p);
7048 if (error == 0) {
7049 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
7050 nfsvno_updateds(vp, cred, p);
7051 error = VOP_FSYNC(vp, MNT_WAIT, p);
7052 }
7053 free(iv, M_TEMP);
7054 }
7055
7056 out:
7057 NFSEXITCODE(error);
7058 return (error);
7059 }
7060
7061 /*
7062 * For a pNFS server, the DS file's ctime and
7063 * va_filerev (TimeMetadata and Change) needs to
7064 * be updated. This is a hack, but works by
7065 * flipping the S_ISGID bit in va_mode and then
7066 * flipping it back.
7067 * It does result in two MDS->DS RPCs, but creating
7068 * a custom RPC just to do this seems overkill, since
7069 * Setxattr/Rmxattr will not be done that frequently.
7070 * If it fails part way through, that is not too
7071 * serious, since the DS file is never executed.
7072 */
7073 static void
nfsvno_updateds(struct vnode * vp,struct ucred * cred,NFSPROC_T * p)7074 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p)
7075 {
7076 struct nfsvattr nva;
7077 int ret;
7078 u_short tmode;
7079
7080 ret = VOP_GETATTR(vp, &nva.na_vattr, cred);
7081 if (ret == 0) {
7082 tmode = nva.na_mode;
7083 NFSVNO_ATTRINIT(&nva);
7084 tmode ^= S_ISGID;
7085 NFSVNO_SETATTRVAL(&nva, mode, tmode);
7086 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
7087 NFSPROC_SETATTR, NULL, NULL, NULL, &nva,
7088 NULL, NULL, 0, NULL);
7089 if (ret == 0) {
7090 tmode ^= S_ISGID;
7091 NFSVNO_SETATTRVAL(&nva, mode, tmode);
7092 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
7093 NFSPROC_SETATTR, NULL, NULL, NULL,
7094 &nva, NULL, NULL, 0, NULL);
7095 }
7096 }
7097 }
7098
7099 /*
7100 * Remove Extended attribute vnode op.
7101 */
7102 int
nfsvno_rmxattr(struct nfsrv_descript * nd,struct vnode * vp,char * name,struct ucred * cred,struct thread * p)7103 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
7104 struct ucred *cred, struct thread *p)
7105 {
7106 int error;
7107
7108 /*
7109 * Get rid of any delegations. I am not sure why this is required,
7110 * but RFC-8276 says so.
7111 */
7112 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
7113 if (error != 0)
7114 goto out;
7115 #ifdef MAC
7116 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
7117 name);
7118 if (error != 0)
7119 goto out;
7120 #endif
7121
7122 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
7123 if (error == EOPNOTSUPP)
7124 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
7125 cred, p);
7126 if (error == 0) {
7127 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
7128 nfsvno_updateds(vp, cred, p);
7129 error = VOP_FSYNC(vp, MNT_WAIT, p);
7130 }
7131 out:
7132 NFSEXITCODE(error);
7133 return (error);
7134 }
7135
7136 /*
7137 * List Extended Atribute vnode op into an mbuf list.
7138 */
7139 int
nfsvno_listxattr(struct vnode * vp,uint64_t cookie,struct ucred * cred,struct thread * p,u_char ** bufp,uint32_t * lenp,bool * eofp)7140 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
7141 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
7142 {
7143 struct iovec iv;
7144 struct uio io;
7145 int error;
7146 size_t siz;
7147
7148 *bufp = NULL;
7149 /* First, find out the size of the extended attribute. */
7150 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
7151 p);
7152 if (error != 0)
7153 return (NFSERR_NOXATTR);
7154 if (siz <= cookie) {
7155 *lenp = 0;
7156 *eofp = true;
7157 goto out;
7158 }
7159 if (siz > cookie + *lenp) {
7160 siz = cookie + *lenp;
7161 *eofp = false;
7162 } else
7163 *eofp = true;
7164 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
7165 if (siz > 10 * 1024 * 1024) {
7166 error = NFSERR_XATTR2BIG;
7167 goto out;
7168 }
7169 *bufp = malloc(siz, M_TEMP, M_WAITOK);
7170 iv.iov_base = *bufp;
7171 iv.iov_len = siz;
7172 io.uio_iovcnt = 1;
7173 io.uio_iov = &iv;
7174 io.uio_offset = 0;
7175 io.uio_resid = siz;
7176 io.uio_rw = UIO_READ;
7177 io.uio_segflg = UIO_SYSSPACE;
7178 io.uio_td = p;
7179 #ifdef MAC
7180 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
7181 if (error != 0)
7182 goto out;
7183 #endif
7184
7185 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
7186 p);
7187 if (error != 0)
7188 goto out;
7189 if (io.uio_resid > 0)
7190 siz -= io.uio_resid;
7191 *lenp = siz;
7192
7193 out:
7194 if (error != 0) {
7195 free(*bufp, M_TEMP);
7196 *bufp = NULL;
7197 }
7198 NFSEXITCODE(error);
7199 return (error);
7200 }
7201
7202 /*
7203 * Trim trailing data off the mbuf list being built.
7204 */
7205 void
nfsm_trimtrailing(struct nfsrv_descript * nd,struct mbuf * mb,char * bpos,int bextpg,int bextpgsiz)7206 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
7207 int bextpg, int bextpgsiz)
7208 {
7209 vm_page_t pg;
7210 int fullpgsiz, i;
7211
7212 if (mb->m_next != NULL) {
7213 m_freem(mb->m_next);
7214 mb->m_next = NULL;
7215 }
7216 if ((mb->m_flags & M_EXTPG) != 0) {
7217 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
7218 ("nfsm_trimtrailing: bextpg out of range"));
7219 KASSERT(bpos == (char *)(void *)
7220 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
7221 ("nfsm_trimtrailing: bextpgsiz bad!"));
7222
7223 /* First, get rid of any pages after this position. */
7224 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
7225 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
7226 vm_page_unwire_noq(pg);
7227 vm_page_free(pg);
7228 }
7229 mb->m_epg_npgs = bextpg + 1;
7230 if (bextpg == 0)
7231 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
7232 else
7233 fullpgsiz = PAGE_SIZE;
7234 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
7235 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
7236 for (i = 1; i < mb->m_epg_npgs; i++)
7237 mb->m_len += m_epg_pagelen(mb, i, 0);
7238 nd->nd_bextpgsiz = bextpgsiz;
7239 nd->nd_bextpg = bextpg;
7240 } else
7241 mb->m_len = bpos - mtod(mb, char *);
7242 nd->nd_mb = mb;
7243 nd->nd_bpos = bpos;
7244 }
7245
7246
7247 /*
7248 * Check to see if a put file handle operation should test for
7249 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
7250 * When Open is the next operation, NFSERR_WRONGSEC cannot be
7251 * replied for the Open cases that use a component. This can
7252 * be identified by the fact that the file handle's type is VDIR.
7253 */
7254 bool
nfsrv_checkwrongsec(struct nfsrv_descript * nd,int nextop,__enum_uint8 (vtype)vtyp)7255 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, __enum_uint8(vtype) vtyp)
7256 {
7257
7258 if ((nd->nd_flag & ND_NFSV4) == 0)
7259 return (true);
7260
7261 if ((nd->nd_flag & ND_LASTOP) != 0)
7262 return (false);
7263
7264 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
7265 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
7266 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
7267 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
7268 return (false);
7269 if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
7270 return (false);
7271 return (true);
7272 }
7273
7274 /*
7275 * Check DSs marked no space.
7276 */
7277 void
nfsrv_checknospc(void)7278 nfsrv_checknospc(void)
7279 {
7280 struct statfs *tsf;
7281 struct nfsdevice *ds;
7282 struct vnode **dvpp, **tdvpp, *dvp;
7283 char *devid, *tdevid;
7284 int cnt, error = 0, i;
7285
7286 if (nfsrv_devidcnt <= 0)
7287 return;
7288 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
7289 devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK);
7290 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
7291
7292 /* Get an array of the dvps for the DSs. */
7293 tdvpp = dvpp;
7294 tdevid = devid;
7295 i = 0;
7296 NFSDDSLOCK();
7297 /* First, search for matches for same file system. */
7298 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7299 if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) {
7300 if (++i > nfsrv_devidcnt)
7301 break;
7302 *tdvpp++ = ds->nfsdev_dvp;
7303 NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID);
7304 tdevid += NFSX_V4DEVICEID;
7305 }
7306 }
7307 NFSDDSUNLOCK();
7308
7309 /* Do a VFS_STATFS() for each of the DSs and clear no space. */
7310 cnt = i;
7311 tdvpp = dvpp;
7312 tdevid = devid;
7313 for (i = 0; i < cnt && error == 0; i++) {
7314 dvp = *tdvpp++;
7315 error = VFS_STATFS(dvp->v_mount, tsf);
7316 if (error == 0 && tsf->f_bavail > 0) {
7317 NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n");
7318 nfsrv_marknospc(tdevid, false);
7319 }
7320 tdevid += NFSX_V4DEVICEID;
7321 }
7322 free(tsf, M_TEMP);
7323 free(dvpp, M_TEMP);
7324 free(devid, M_TEMP);
7325 }
7326
7327 /*
7328 * Initialize everything that needs to be initialized for a vnet.
7329 */
7330 static void
nfsrv_vnetinit(const void * unused __unused)7331 nfsrv_vnetinit(const void *unused __unused)
7332 {
7333
7334 nfsd_mntinit();
7335 }
7336 VNET_SYSINIT(nfsrv_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7337 nfsrv_vnetinit, NULL);
7338
7339 /*
7340 * Clean up everything that is in a vnet and needs to be
7341 * done when the jail is destroyed or the module unloaded.
7342 */
7343 static void
nfsrv_cleanup(const void * unused __unused)7344 nfsrv_cleanup(const void *unused __unused)
7345 {
7346 int i;
7347
7348 NFSD_LOCK();
7349 if (!NFSD_VNET(nfsrv_mntinited)) {
7350 NFSD_UNLOCK();
7351 return;
7352 }
7353 NFSD_VNET(nfsrv_mntinited) = false;
7354 NFSD_UNLOCK();
7355
7356 /* Clean out all NFSv4 state. */
7357 nfsrv_throwawayallstate(curthread);
7358
7359 /* Clean the NFS server reply cache */
7360 nfsrvd_cleancache();
7361
7362 /* Clean out v4root exports. */
7363 if (NFSD_VNET(nfsv4root_mnt)->mnt_export != NULL) {
7364 vfs_free_addrlist(NFSD_VNET(nfsv4root_mnt)->mnt_export);
7365 free(NFSD_VNET(nfsv4root_mnt)->mnt_export, M_MOUNT);
7366 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
7367 }
7368
7369 /* Free up the krpc server pool. */
7370 if (NFSD_VNET(nfsrvd_pool) != NULL)
7371 svcpool_destroy(NFSD_VNET(nfsrvd_pool));
7372
7373 /* and get rid of the locks */
7374 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7375 mtx_destroy(&NFSD_VNET(nfsrchash_table)[i].mtx);
7376 mtx_destroy(&NFSD_VNET(nfsrcahash_table)[i].mtx);
7377 }
7378 mtx_destroy(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx);
7379 for (i = 0; i < nfsrv_sessionhashsize; i++)
7380 mtx_destroy(&NFSD_VNET(nfssessionhash)[i].mtx);
7381 lockdestroy(&NFSD_VNET(nfsv4root_mnt)->mnt_explock);
7382 free(NFSD_VNET(nfsrvudphashtbl), M_NFSRVCACHE);
7383 free(NFSD_VNET(nfsrchash_table), M_NFSRVCACHE);
7384 free(NFSD_VNET(nfsrcahash_table), M_NFSRVCACHE);
7385 free(NFSD_VNET(nfsclienthash), M_NFSDCLIENT);
7386 free(NFSD_VNET(nfslockhash), M_NFSDLOCKFILE);
7387 free(NFSD_VNET(nfssessionhash), M_NFSDSESSION);
7388 free(NFSD_VNET(nfsv4root_mnt), M_TEMP);
7389 NFSD_VNET(nfsv4root_mnt) = NULL;
7390 }
7391 VNET_SYSUNINIT(nfsrv_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7392 nfsrv_cleanup, NULL);
7393
7394 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
7395
7396 /*
7397 * Called once to initialize data structures...
7398 */
7399 static int
nfsd_modevent(module_t mod,int type,void * data)7400 nfsd_modevent(module_t mod, int type, void *data)
7401 {
7402 int error = 0, i;
7403 static int loaded = 0;
7404
7405 switch (type) {
7406 case MOD_LOAD:
7407 if (loaded)
7408 goto out;
7409 newnfs_portinit();
7410 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
7411 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
7412 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
7413 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
7414 #ifdef VV_DISABLEDELEG
7415 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
7416 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
7417 #endif
7418 nfsd_call_nfsd = nfssvc_nfsd;
7419 loaded = 1;
7420 break;
7421
7422 case MOD_UNLOAD:
7423 if (newnfs_numnfsd != 0) {
7424 error = EBUSY;
7425 break;
7426 }
7427
7428 #ifdef VV_DISABLEDELEG
7429 vn_deleg_ops.vndeleg_recall = NULL;
7430 vn_deleg_ops.vndeleg_disable = NULL;
7431 #endif
7432 nfsd_call_nfsd = NULL;
7433 mtx_destroy(&nfsrc_udpmtx);
7434 mtx_destroy(&nfs_v4root_mutex);
7435 mtx_destroy(&nfsrv_dontlistlock_mtx);
7436 mtx_destroy(&nfsrv_recalllock_mtx);
7437 if (nfslayouthash != NULL) {
7438 for (i = 0; i < nfsrv_layouthashsize; i++)
7439 mtx_destroy(&nfslayouthash[i].mtx);
7440 free(nfslayouthash, M_NFSDSESSION);
7441 }
7442 loaded = 0;
7443 break;
7444 default:
7445 error = EOPNOTSUPP;
7446 break;
7447 }
7448
7449 out:
7450 NFSEXITCODE(error);
7451 return (error);
7452 }
7453 static moduledata_t nfsd_mod = {
7454 "nfsd",
7455 nfsd_modevent,
7456 NULL,
7457 };
7458 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
7459
7460 /* So that loader and kldload(2) can find us, wherever we are.. */
7461 MODULE_VERSION(nfsd, 1);
7462 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
7463 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
7464 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
7465 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
7466