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