Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/nfsclient/nfs_clcomsubs.c
39483 views
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/cdefs.h>
37
/*
38
* These functions support the macros and help fiddle mbuf chains for
39
* the nfs op functions. They do things like create the rpc header and
40
* copy data between mbuf chains and uio lists.
41
*/
42
#include <fs/nfs/nfsport.h>
43
44
extern struct nfsstatsv1 nfsstatsv1;
45
extern int ncl_mbuf_mlen;
46
extern __enum_uint8(vtype) newnv2tov_type[8];
47
extern __enum_uint8(vtype) nv34tov_type[8];
48
NFSCLSTATEMUTEX;
49
50
/*
51
* copies a uio scatter/gather list to an mbuf chain.
52
* NOTE: can only handle iovcnt == 1
53
*/
54
int
55
nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
56
{
57
char *uiocp;
58
struct mbuf *mp, *mp2;
59
int error, xfer, left, mlen;
60
int uiosiz, clflg, rem;
61
char *mcp, *tcp;
62
63
KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
64
65
if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
66
clflg = 1;
67
else
68
clflg = 0;
69
rem = NFSM_RNDUP(siz) - siz;
70
mp = mp2 = nd->nd_mb;
71
mcp = nd->nd_bpos;
72
while (siz > 0) {
73
KASSERT((nd->nd_flag & ND_EXTPG) != 0 || mcp ==
74
mtod(mp, char *) + mp->m_len, ("nfsm_uiombuf: mcp wrong"));
75
left = uiop->uio_iov->iov_len;
76
uiocp = uiop->uio_iov->iov_base;
77
if (left > siz)
78
left = siz;
79
uiosiz = left;
80
while (left > 0) {
81
if ((nd->nd_flag & ND_EXTPG) != 0)
82
mlen = nd->nd_bextpgsiz;
83
else
84
mlen = M_TRAILINGSPACE(mp);
85
if (mlen == 0) {
86
if ((nd->nd_flag & ND_EXTPG) != 0) {
87
mp = nfsm_add_ext_pgs(mp,
88
nd->nd_maxextsiz, &nd->nd_bextpg);
89
mcp = (char *)(void *)PHYS_TO_DMAP(
90
mp->m_epg_pa[nd->nd_bextpg]);
91
nd->nd_bextpgsiz = mlen = PAGE_SIZE;
92
} else {
93
if (clflg)
94
NFSMCLGET(mp, M_WAITOK);
95
else
96
NFSMGET(mp);
97
mp->m_len = 0;
98
mlen = M_TRAILINGSPACE(mp);
99
mcp = mtod(mp, char *);
100
mp2->m_next = mp;
101
mp2 = mp;
102
}
103
}
104
xfer = (left > mlen) ? mlen : left;
105
if (uiop->uio_segflg == UIO_SYSSPACE)
106
NFSBCOPY(uiocp, mcp, xfer);
107
else {
108
error = copyin(uiocp, mcp, xfer);
109
if (error != 0)
110
return (error);
111
}
112
mp->m_len += xfer;
113
left -= xfer;
114
uiocp += xfer;
115
mcp += xfer;
116
if ((nd->nd_flag & ND_EXTPG) != 0) {
117
nd->nd_bextpgsiz -= xfer;
118
mp->m_epg_last_len += xfer;
119
}
120
uiop->uio_offset += xfer;
121
uiop->uio_resid -= xfer;
122
}
123
tcp = (char *)uiop->uio_iov->iov_base;
124
tcp += uiosiz;
125
uiop->uio_iov->iov_base = (void *)tcp;
126
uiop->uio_iov->iov_len -= uiosiz;
127
siz -= uiosiz;
128
}
129
if (rem > 0) {
130
if ((nd->nd_flag & ND_EXTPG) == 0 && rem >
131
M_TRAILINGSPACE(mp)) {
132
NFSMGET(mp);
133
mp->m_len = 0;
134
mp2->m_next = mp;
135
mcp = mtod(mp, char *);
136
} else if ((nd->nd_flag & ND_EXTPG) != 0 && rem >
137
nd->nd_bextpgsiz) {
138
mp = nfsm_add_ext_pgs(mp, nd->nd_maxextsiz,
139
&nd->nd_bextpg);
140
mcp = (char *)(void *)
141
PHYS_TO_DMAP(mp->m_epg_pa[nd->nd_bextpg]);
142
nd->nd_bextpgsiz = PAGE_SIZE;
143
}
144
for (left = 0; left < rem; left++)
145
*mcp++ = '\0';
146
mp->m_len += rem;
147
if ((nd->nd_flag & ND_EXTPG) != 0) {
148
nd->nd_bextpgsiz -= rem;
149
mp->m_epg_last_len += rem;
150
}
151
}
152
nd->nd_bpos = mcp;
153
nd->nd_mb = mp;
154
return (0);
155
}
156
157
/*
158
* copies a uio scatter/gather list to an mbuf chain.
159
* This version returns the mbuf list and does not use "nd".
160
* NOTE: can only handle iovcnt == 1
161
*/
162
struct mbuf *
163
nfsm_uiombuflist(struct uio *uiop, int siz, u_int maxext)
164
{
165
char *uiocp;
166
struct mbuf *mp, *mp2, *firstmp;
167
int error, extpg, extpgsiz = 0, i, left, mlen, rem, xfer;
168
int uiosiz, clflg;
169
char *mcp, *tcp;
170
171
KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
172
173
if (maxext > 0) {
174
mp = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);
175
mcp = (char *)(void *)PHYS_TO_DMAP(mp->m_epg_pa[0]);
176
extpg = 0;
177
extpgsiz = PAGE_SIZE;
178
} else {
179
if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
180
clflg = 1;
181
else
182
clflg = 0;
183
if (clflg != 0)
184
NFSMCLGET(mp, M_WAITOK);
185
else
186
NFSMGET(mp);
187
mcp = mtod(mp, char *);
188
}
189
mp->m_len = 0;
190
firstmp = mp2 = mp;
191
rem = NFSM_RNDUP(siz) - siz;
192
while (siz > 0) {
193
left = uiop->uio_iov->iov_len;
194
uiocp = uiop->uio_iov->iov_base;
195
if (left > siz)
196
left = siz;
197
uiosiz = left;
198
while (left > 0) {
199
if (maxext > 0)
200
mlen = extpgsiz;
201
else
202
mlen = M_TRAILINGSPACE(mp);
203
if (mlen == 0) {
204
if (maxext > 0) {
205
mp = nfsm_add_ext_pgs(mp, maxext,
206
&extpg);
207
mlen = extpgsiz = PAGE_SIZE;
208
mcp = (char *)(void *)PHYS_TO_DMAP(
209
mp->m_epg_pa[extpg]);
210
} else {
211
if (clflg)
212
NFSMCLGET(mp, M_WAITOK);
213
else
214
NFSMGET(mp);
215
mcp = mtod(mp, char *);
216
mlen = M_TRAILINGSPACE(mp);
217
mp->m_len = 0;
218
mp2->m_next = mp;
219
mp2 = mp;
220
}
221
}
222
xfer = (left > mlen) ? mlen : left;
223
if (uiop->uio_segflg == UIO_SYSSPACE)
224
NFSBCOPY(uiocp, mcp, xfer);
225
else {
226
error = copyin(uiocp, mcp, xfer);
227
if (error != 0) {
228
m_freem(firstmp);
229
return (NULL);
230
}
231
}
232
mp->m_len += xfer;
233
mcp += xfer;
234
if (maxext > 0) {
235
extpgsiz -= xfer;
236
mp->m_epg_last_len += xfer;
237
}
238
left -= xfer;
239
uiocp += xfer;
240
uiop->uio_offset += xfer;
241
uiop->uio_resid -= xfer;
242
}
243
tcp = (char *)uiop->uio_iov->iov_base;
244
tcp += uiosiz;
245
uiop->uio_iov->iov_base = (void *)tcp;
246
uiop->uio_iov->iov_len -= uiosiz;
247
siz -= uiosiz;
248
}
249
if (rem > 0) {
250
KASSERT((mp->m_flags & M_EXTPG) != 0 ||
251
rem <= M_TRAILINGSPACE(mp),
252
("nfsm_uiombuflist: no space for padding"));
253
for (i = 0; i < rem; i++)
254
*mcp++ = '\0';
255
mp->m_len += rem;
256
if (maxext > 0)
257
mp->m_epg_last_len += rem;
258
}
259
return (firstmp);
260
}
261
262
/*
263
* Load vnode attributes from the xdr file attributes.
264
* Returns EBADRPC if they can't be parsed, 0 otherwise.
265
*/
266
int
267
nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap)
268
{
269
struct nfs_fattr *fp;
270
int error = 0;
271
272
if (nd->nd_flag & ND_NFSV4) {
273
error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL,
274
NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL,
275
NULL, NULL);
276
} else if (nd->nd_flag & ND_NFSV3) {
277
NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR);
278
nap->na_type = nfsv34tov_type(fp->fa_type);
279
nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
280
nap->na_rdev = NFSMAKEDEV(
281
fxdr_unsigned(int, fp->fa3_rdev.specdata1),
282
fxdr_unsigned(int, fp->fa3_rdev.specdata2));
283
nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink);
284
nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
285
nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
286
nap->na_size = fxdr_hyper(&fp->fa3_size);
287
nap->na_blocksize = NFS_FABLKSIZE;
288
nap->na_bytes = fxdr_hyper(&fp->fa3_used);
289
nap->na_fileid = fxdr_hyper(&fp->fa3_fileid);
290
fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime);
291
fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime);
292
fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime);
293
nap->na_btime.tv_sec = -1;
294
nap->na_btime.tv_nsec = 0;
295
nap->na_flags = 0;
296
nap->na_gen = 0;
297
nap->na_filerev = 0;
298
} else {
299
NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR);
300
nap->na_type = nfsv2tov_type(fp->fa_type);
301
nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
302
if (nap->na_type == VNON || nap->na_type == VREG)
303
nap->na_type = IFTOVT(nap->na_mode);
304
nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev);
305
306
/*
307
* Really ugly NFSv2 kludge.
308
*/
309
if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1))
310
nap->na_type = VFIFO;
311
nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
312
nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
313
nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
314
nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
315
nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
316
nap->na_bytes =
317
(u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
318
NFS_FABLKSIZE;
319
nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid);
320
fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime);
321
fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime);
322
nap->na_flags = 0;
323
nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t,
324
fp->fa2_ctime.nfsv2_sec);
325
nap->na_ctime.tv_nsec = 0;
326
nap->na_btime.tv_sec = -1;
327
nap->na_btime.tv_nsec = 0;
328
nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
329
nap->na_filerev = 0;
330
}
331
nfsmout:
332
return (error);
333
}
334
335
/*
336
* Gets a file handle out of an nfs reply sent to the client and returns
337
* the file handle and the file's attributes.
338
* For V4, it assumes that Getfh and Getattr Op's results are here.
339
*/
340
int
341
nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp,
342
struct nfsvattr *nap, int *attrflagp)
343
{
344
u_int32_t *tl;
345
int error = 0, flag = 1;
346
347
*nfhpp = NULL;
348
*attrflagp = 0;
349
/*
350
* First get the file handle and vnode.
351
*/
352
if (nd->nd_flag & ND_NFSV3) {
353
NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
354
flag = fxdr_unsigned(int, *tl);
355
} else if (nd->nd_flag & ND_NFSV4) {
356
NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
357
/* If the GetFH failed, clear flag. */
358
if (*++tl != 0) {
359
nd->nd_flag |= ND_NOMOREDATA;
360
flag = 0;
361
error = ENXIO; /* Return ENXIO so *nfhpp isn't used. */
362
}
363
}
364
if (flag) {
365
error = nfsm_getfh(nd, nfhpp);
366
if (error)
367
return (error);
368
}
369
370
/*
371
* Now, get the attributes.
372
*/
373
if (flag != 0 && (nd->nd_flag & ND_NFSV4) != 0) {
374
NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
375
if (*++tl != 0) {
376
nd->nd_flag |= ND_NOMOREDATA;
377
flag = 0;
378
}
379
} else if (nd->nd_flag & ND_NFSV3) {
380
NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
381
if (flag) {
382
flag = fxdr_unsigned(int, *tl);
383
} else if (fxdr_unsigned(int, *tl)) {
384
error = nfsm_advance(nd, NFSX_V3FATTR, -1);
385
if (error)
386
return (error);
387
}
388
}
389
if (flag) {
390
error = nfsm_loadattr(nd, nap);
391
if (!error)
392
*attrflagp = 1;
393
}
394
nfsmout:
395
return (error);
396
}
397
398
/*
399
* Initialize the owner/delegation sleep lock.
400
*/
401
void
402
nfscl_lockinit(struct nfsv4lock *lckp)
403
{
404
405
lckp->nfslock_usecnt = 0;
406
lckp->nfslock_lock = 0;
407
}
408
409
/*
410
* Get an exclusive lock. (Not needed for OpenBSD4, since there is only one
411
* thread for each posix process in the kernel.)
412
*/
413
void
414
nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex)
415
{
416
int igotlock;
417
418
do {
419
igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL);
420
} while (!igotlock);
421
}
422
423
/*
424
* Release an exclusive lock.
425
*/
426
void
427
nfscl_lockunlock(struct nfsv4lock *lckp)
428
{
429
430
nfsv4_unlock(lckp, 0);
431
}
432
433
/*
434
* Called to dereference a lock on a stateid (delegation or open owner).
435
*/
436
void
437
nfscl_lockderef(struct nfsv4lock *lckp)
438
{
439
440
NFSLOCKCLSTATE();
441
lckp->nfslock_usecnt--;
442
if (lckp->nfslock_usecnt == 0 && (lckp->nfslock_lock & NFSV4LOCK_WANTED)) {
443
lckp->nfslock_lock &= ~NFSV4LOCK_WANTED;
444
wakeup((caddr_t)lckp);
445
}
446
NFSUNLOCKCLSTATE();
447
}
448
449