Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/nfsclient/nfs_clvnops.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
* from nfs_vnops.c 8.16 (Berkeley) 5/27/95
35
*/
36
37
#include <sys/cdefs.h>
38
/*
39
* vnode op calls for Sun NFS version 2, 3 and 4
40
*/
41
42
#include "opt_inet.h"
43
44
#include <sys/param.h>
45
#include <sys/kernel.h>
46
#include <sys/systm.h>
47
#include <sys/resourcevar.h>
48
#include <sys/proc.h>
49
#include <sys/mount.h>
50
#include <sys/bio.h>
51
#include <sys/buf.h>
52
#include <sys/extattr.h>
53
#include <sys/filio.h>
54
#include <sys/jail.h>
55
#include <sys/malloc.h>
56
#include <sys/mbuf.h>
57
#include <sys/namei.h>
58
#include <sys/socket.h>
59
#include <sys/vnode.h>
60
#include <sys/dirent.h>
61
#include <sys/fcntl.h>
62
#include <sys/lockf.h>
63
#include <sys/stat.h>
64
#include <sys/sysctl.h>
65
#include <sys/signalvar.h>
66
67
#include <vm/vm.h>
68
#include <vm/vm_extern.h>
69
#include <vm/vm_object.h>
70
#include <vm/vnode_pager.h>
71
72
#include <fs/nfs/nfsport.h>
73
#include <fs/nfsclient/nfsnode.h>
74
#include <fs/nfsclient/nfsmount.h>
75
#include <fs/nfsclient/nfs.h>
76
#include <fs/nfsclient/nfs_kdtrace.h>
77
78
#include <net/if.h>
79
#include <netinet/in.h>
80
#include <netinet/in_var.h>
81
82
#include <nfs/nfs_lock.h>
83
84
#ifdef KDTRACE_HOOKS
85
#include <sys/dtrace_bsd.h>
86
87
dtrace_nfsclient_accesscache_flush_probe_func_t
88
dtrace_nfscl_accesscache_flush_done_probe;
89
uint32_t nfscl_accesscache_flush_done_id;
90
91
dtrace_nfsclient_accesscache_get_probe_func_t
92
dtrace_nfscl_accesscache_get_hit_probe,
93
dtrace_nfscl_accesscache_get_miss_probe;
94
uint32_t nfscl_accesscache_get_hit_id;
95
uint32_t nfscl_accesscache_get_miss_id;
96
97
dtrace_nfsclient_accesscache_load_probe_func_t
98
dtrace_nfscl_accesscache_load_done_probe;
99
uint32_t nfscl_accesscache_load_done_id;
100
#endif /* !KDTRACE_HOOKS */
101
102
/* Defs */
103
#define TRUE 1
104
#define FALSE 0
105
106
extern struct nfsstatsv1 nfsstatsv1;
107
extern int nfsrv_useacl;
108
extern int nfscl_debuglevel;
109
NFSCLSTATEMUTEX;
110
MALLOC_DECLARE(M_NEWNFSREQ);
111
112
static vop_read_t nfsfifo_read;
113
static vop_write_t nfsfifo_write;
114
static vop_close_t nfsfifo_close;
115
static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
116
struct thread *);
117
static int nfs_get_namedattrdir(struct vnode *, struct componentname *,
118
struct vnode **);
119
static vop_lookup_t nfs_lookup;
120
static vop_create_t nfs_create;
121
static vop_mknod_t nfs_mknod;
122
static vop_open_t nfs_open;
123
static vop_pathconf_t nfs_pathconf;
124
static vop_close_t nfs_close;
125
static vop_access_t nfs_access;
126
static vop_getattr_t nfs_getattr;
127
static vop_setattr_t nfs_setattr;
128
static vop_read_t nfs_read;
129
static vop_fsync_t nfs_fsync;
130
static vop_remove_t nfs_remove;
131
static vop_link_t nfs_link;
132
static vop_rename_t nfs_rename;
133
static vop_mkdir_t nfs_mkdir;
134
static vop_rmdir_t nfs_rmdir;
135
static vop_symlink_t nfs_symlink;
136
static vop_readdir_t nfs_readdir;
137
static vop_strategy_t nfs_strategy;
138
static int nfs_lookitup(struct vnode *, char *, int,
139
struct ucred *, struct thread *, struct nfsnode **);
140
static int nfs_sillyrename(struct vnode *, struct vnode *,
141
struct componentname *);
142
static vop_access_t nfsspec_access;
143
static vop_readlink_t nfs_readlink;
144
static vop_print_t nfs_print;
145
static vop_advlock_t nfs_advlock;
146
static vop_advlockasync_t nfs_advlockasync;
147
static vop_getacl_t nfs_getacl;
148
static vop_setacl_t nfs_setacl;
149
static vop_advise_t nfs_advise;
150
static vop_allocate_t nfs_allocate;
151
static vop_deallocate_t nfs_deallocate;
152
static vop_copy_file_range_t nfs_copy_file_range;
153
static vop_ioctl_t nfs_ioctl;
154
static vop_getextattr_t nfs_getextattr;
155
static vop_setextattr_t nfs_setextattr;
156
static vop_listextattr_t nfs_listextattr;
157
static vop_deleteextattr_t nfs_deleteextattr;
158
static vop_lock1_t nfs_lock;
159
160
/*
161
* Global vfs data structures for nfs
162
*/
163
164
static struct vop_vector newnfs_vnodeops_nosig = {
165
.vop_default = &default_vnodeops,
166
.vop_access = nfs_access,
167
.vop_advlock = nfs_advlock,
168
.vop_advlockasync = nfs_advlockasync,
169
.vop_close = nfs_close,
170
.vop_create = nfs_create,
171
.vop_fsync = nfs_fsync,
172
.vop_getattr = nfs_getattr,
173
.vop_getpages = ncl_getpages,
174
.vop_putpages = ncl_putpages,
175
.vop_inactive = ncl_inactive,
176
.vop_link = nfs_link,
177
.vop_lock1 = nfs_lock,
178
.vop_lookup = nfs_lookup,
179
.vop_mkdir = nfs_mkdir,
180
.vop_mknod = nfs_mknod,
181
.vop_open = nfs_open,
182
.vop_pathconf = nfs_pathconf,
183
.vop_print = nfs_print,
184
.vop_read = nfs_read,
185
.vop_readdir = nfs_readdir,
186
.vop_readlink = nfs_readlink,
187
.vop_reclaim = ncl_reclaim,
188
.vop_remove = nfs_remove,
189
.vop_rename = nfs_rename,
190
.vop_rmdir = nfs_rmdir,
191
.vop_setattr = nfs_setattr,
192
.vop_strategy = nfs_strategy,
193
.vop_symlink = nfs_symlink,
194
.vop_write = ncl_write,
195
.vop_getacl = nfs_getacl,
196
.vop_setacl = nfs_setacl,
197
.vop_advise = nfs_advise,
198
.vop_allocate = nfs_allocate,
199
.vop_deallocate = nfs_deallocate,
200
.vop_copy_file_range = nfs_copy_file_range,
201
.vop_ioctl = nfs_ioctl,
202
.vop_getextattr = nfs_getextattr,
203
.vop_setextattr = nfs_setextattr,
204
.vop_listextattr = nfs_listextattr,
205
.vop_deleteextattr = nfs_deleteextattr,
206
};
207
VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops_nosig);
208
209
static int
210
nfs_vnodeops_bypass(struct vop_generic_args *a)
211
{
212
213
return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
214
}
215
216
struct vop_vector newnfs_vnodeops = {
217
.vop_default = &default_vnodeops,
218
.vop_bypass = nfs_vnodeops_bypass,
219
};
220
VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops);
221
222
static struct vop_vector newnfs_fifoops_nosig = {
223
.vop_default = &fifo_specops,
224
.vop_access = nfsspec_access,
225
.vop_close = nfsfifo_close,
226
.vop_fsync = nfs_fsync,
227
.vop_getattr = nfs_getattr,
228
.vop_inactive = ncl_inactive,
229
.vop_pathconf = nfs_pathconf,
230
.vop_print = nfs_print,
231
.vop_read = nfsfifo_read,
232
.vop_reclaim = ncl_reclaim,
233
.vop_setattr = nfs_setattr,
234
.vop_write = nfsfifo_write,
235
};
236
VFS_VOP_VECTOR_REGISTER(newnfs_fifoops_nosig);
237
238
static int
239
nfs_fifoops_bypass(struct vop_generic_args *a)
240
{
241
242
return (vop_sigdefer(&newnfs_fifoops_nosig, a));
243
}
244
245
struct vop_vector newnfs_fifoops = {
246
.vop_default = &default_vnodeops,
247
.vop_bypass = nfs_fifoops_bypass,
248
};
249
VFS_VOP_VECTOR_REGISTER(newnfs_fifoops);
250
251
static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
252
struct componentname *cnp, struct vattr *vap);
253
static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
254
int namelen, struct ucred *cred, struct thread *td, bool silly);
255
static void nfs_removestatus(struct vnode *vp, nfsremove_status file_status,
256
bool silly, struct thread *td);
257
static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
258
char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
259
char *tnameptr, int tnamelen, bool silly, struct ucred *cred,
260
struct thread *td);
261
static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
262
struct componentname *scnp, struct sillyrename *sp);
263
264
/*
265
* Global variables
266
*/
267
SYSCTL_DECL(_vfs_nfs);
268
269
static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
270
SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
271
&nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
272
273
static int nfs_prime_access_cache = 0;
274
SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
275
&nfs_prime_access_cache, 0,
276
"Prime NFS ACCESS cache when fetching attributes");
277
278
static int newnfs_commit_on_close = 0;
279
SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
280
&newnfs_commit_on_close, 0, "write+commit on close, else only write");
281
282
static int nfs_clean_pages_on_close = 1;
283
SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
284
&nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
285
286
int newnfs_directio_enable = 0;
287
SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
288
&newnfs_directio_enable, 0, "Enable NFS directio");
289
290
int nfs_keep_dirty_on_error;
291
SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
292
&nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
293
294
/*
295
* This sysctl allows other processes to mmap a file that has been opened
296
* O_DIRECT by a process. In general, having processes mmap the file while
297
* Direct IO is in progress can lead to Data Inconsistencies. But, we allow
298
* this by default to prevent DoS attacks - to prevent a malicious user from
299
* opening up files O_DIRECT preventing other users from mmap'ing these
300
* files. "Protected" environments where stricter consistency guarantees are
301
* required can disable this knob. The process that opened the file O_DIRECT
302
* cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
303
* meaningful.
304
*/
305
int newnfs_directio_allow_mmap = 1;
306
SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
307
&newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
308
309
static uint64_t nfs_maxalloclen = 64 * 1024 * 1024;
310
SYSCTL_U64(_vfs_nfs, OID_AUTO, maxalloclen, CTLFLAG_RW,
311
&nfs_maxalloclen, 0, "NFS max allocate/deallocate length");
312
313
#define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
314
| NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
315
| NFSACCESS_DELETE | NFSACCESS_LOOKUP)
316
317
/*
318
* SMP Locking Note :
319
* The list of locks after the description of the lock is the ordering
320
* of other locks acquired with the lock held.
321
* np->n_mtx : Protects the fields in the nfsnode.
322
VM Object Lock
323
VI_MTX (acquired indirectly)
324
* nmp->nm_mtx : Protects the fields in the nfsmount.
325
rep->r_mtx
326
* ncl_iod_mutex : Global lock, protects shared nfsiod state.
327
* nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
328
nmp->nm_mtx
329
rep->r_mtx
330
* rep->r_mtx : Protects the fields in an nfsreq.
331
*/
332
333
static int
334
nfs_lock(struct vop_lock1_args *ap)
335
{
336
struct vnode *vp;
337
struct nfsnode *np;
338
u_quad_t nsize;
339
int error, lktype;
340
bool onfault;
341
342
vp = ap->a_vp;
343
lktype = ap->a_flags & LK_TYPE_MASK;
344
error = VOP_LOCK1_APV(&default_vnodeops, ap);
345
if (error != 0 || vp->v_op != &newnfs_vnodeops)
346
return (error);
347
np = VTONFS(vp);
348
if (np == NULL)
349
return (0);
350
NFSLOCKNODE(np);
351
if ((np->n_flag & NVNSETSZSKIP) == 0 || (lktype != LK_SHARED &&
352
lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE &&
353
lktype != LK_TRYUPGRADE)) {
354
NFSUNLOCKNODE(np);
355
return (0);
356
}
357
onfault = (ap->a_flags & LK_EATTR_MASK) == LK_NOWAIT &&
358
(ap->a_flags & LK_INIT_MASK) == LK_CANRECURSE &&
359
(lktype == LK_SHARED || lktype == LK_EXCLUSIVE);
360
if (onfault && vp->v_vnlock->lk_recurse == 0) {
361
/*
362
* Force retry in vm_fault(), to make the lock request
363
* sleepable, which allows us to piggy-back the
364
* sleepable call to vnode_pager_setsize().
365
*/
366
NFSUNLOCKNODE(np);
367
VOP_UNLOCK(vp);
368
return (EBUSY);
369
}
370
if ((ap->a_flags & LK_NOWAIT) != 0 ||
371
(lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) {
372
NFSUNLOCKNODE(np);
373
return (0);
374
}
375
if (lktype == LK_SHARED) {
376
NFSUNLOCKNODE(np);
377
VOP_UNLOCK(vp);
378
ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
379
ap->a_flags |= LK_EXCLUSIVE;
380
error = VOP_LOCK1_APV(&default_vnodeops, ap);
381
if (error != 0 || vp->v_op != &newnfs_vnodeops)
382
return (error);
383
if (vp->v_data == NULL)
384
goto downgrade;
385
MPASS(vp->v_data == np);
386
NFSLOCKNODE(np);
387
if ((np->n_flag & NVNSETSZSKIP) == 0) {
388
NFSUNLOCKNODE(np);
389
goto downgrade;
390
}
391
}
392
np->n_flag &= ~NVNSETSZSKIP;
393
nsize = np->n_size;
394
NFSUNLOCKNODE(np);
395
vnode_pager_setsize(vp, nsize);
396
downgrade:
397
if (lktype == LK_SHARED) {
398
ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
399
ap->a_flags |= LK_DOWNGRADE;
400
(void)VOP_LOCK1_APV(&default_vnodeops, ap);
401
}
402
return (0);
403
}
404
405
static int
406
nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
407
struct ucred *cred, u_int32_t *retmode)
408
{
409
int error = 0, attrflag, i, lrupos;
410
u_int32_t rmode;
411
struct nfsnode *np = VTONFS(vp);
412
struct nfsvattr nfsva;
413
414
error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
415
&rmode);
416
if (attrflag)
417
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
418
if (!error) {
419
lrupos = 0;
420
NFSLOCKNODE(np);
421
for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
422
if (np->n_accesscache[i].uid == cred->cr_uid) {
423
np->n_accesscache[i].mode = rmode;
424
np->n_accesscache[i].stamp = time_second;
425
break;
426
}
427
if (i > 0 && np->n_accesscache[i].stamp <
428
np->n_accesscache[lrupos].stamp)
429
lrupos = i;
430
}
431
if (i == NFS_ACCESSCACHESIZE) {
432
np->n_accesscache[lrupos].uid = cred->cr_uid;
433
np->n_accesscache[lrupos].mode = rmode;
434
np->n_accesscache[lrupos].stamp = time_second;
435
}
436
NFSUNLOCKNODE(np);
437
if (retmode != NULL)
438
*retmode = rmode;
439
KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
440
} else if (NFS_ISV4(vp)) {
441
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
442
}
443
#ifdef KDTRACE_HOOKS
444
if (error != 0)
445
KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
446
error);
447
#endif
448
return (error);
449
}
450
451
/*
452
* nfs access vnode op.
453
* For nfs version 2, just return ok. File accesses may fail later.
454
* For nfs version 3, use the access rpc to check accessibility. If file modes
455
* are changed on the server, accesses might still fail later.
456
*/
457
static int
458
nfs_access(struct vop_access_args *ap)
459
{
460
struct vnode *vp = ap->a_vp;
461
int error = 0, i, gotahit;
462
u_int32_t mode, wmode, rmode;
463
int v34 = NFS_ISV34(vp);
464
struct nfsnode *np = VTONFS(vp);
465
466
/*
467
* Disallow write attempts on filesystems mounted read-only;
468
* unless the file is a socket, fifo, or a block or character
469
* device resident on the filesystem.
470
*/
471
if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
472
VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
473
VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
474
switch (vp->v_type) {
475
case VREG:
476
case VDIR:
477
case VLNK:
478
return (EROFS);
479
default:
480
break;
481
}
482
}
483
484
/*
485
* For NFSv4, check for a delegation with an Allow ACE, to see
486
* if that permits access.
487
*/
488
if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) != 0) {
489
error = nfscl_delegacecheck(vp, ap->a_accmode, ap->a_cred);
490
if (error == 0)
491
return (error);
492
error = 0;
493
}
494
495
/*
496
* For nfs v3 or v4, check to see if we have done this recently, and if
497
* so return our cached result instead of making an ACCESS call.
498
* If not, do an access rpc, otherwise you are stuck emulating
499
* ufs_access() locally using the vattr. This may not be correct,
500
* since the server may apply other access criteria such as
501
* client uid-->server uid mapping that we do not know about.
502
*/
503
if (v34) {
504
if (ap->a_accmode & VREAD)
505
mode = NFSACCESS_READ;
506
else
507
mode = 0;
508
if (vp->v_type != VDIR) {
509
if (ap->a_accmode & VWRITE)
510
mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
511
if (ap->a_accmode & VAPPEND)
512
mode |= NFSACCESS_EXTEND;
513
if (ap->a_accmode & VEXEC)
514
mode |= NFSACCESS_EXECUTE;
515
if (ap->a_accmode & VDELETE)
516
mode |= NFSACCESS_DELETE;
517
} else {
518
if (ap->a_accmode & VWRITE)
519
mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
520
if (ap->a_accmode & VAPPEND)
521
mode |= NFSACCESS_EXTEND;
522
if (ap->a_accmode & VEXEC)
523
mode |= NFSACCESS_LOOKUP;
524
if (ap->a_accmode & VDELETE)
525
mode |= NFSACCESS_DELETE;
526
if (ap->a_accmode & VDELETE_CHILD)
527
mode |= NFSACCESS_MODIFY;
528
}
529
/* XXX safety belt, only make blanket request if caching */
530
if (nfsaccess_cache_timeout > 0) {
531
wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
532
NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
533
NFSACCESS_DELETE | NFSACCESS_LOOKUP;
534
} else {
535
wmode = mode;
536
}
537
538
/*
539
* Does our cached result allow us to give a definite yes to
540
* this request?
541
*/
542
gotahit = 0;
543
NFSLOCKNODE(np);
544
for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
545
if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
546
if (time_second < (np->n_accesscache[i].stamp
547
+ nfsaccess_cache_timeout) &&
548
(np->n_accesscache[i].mode & mode) == mode) {
549
NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
550
gotahit = 1;
551
}
552
break;
553
}
554
}
555
NFSUNLOCKNODE(np);
556
#ifdef KDTRACE_HOOKS
557
if (gotahit != 0)
558
KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
559
ap->a_cred->cr_uid, mode);
560
else
561
KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
562
ap->a_cred->cr_uid, mode);
563
#endif
564
if (gotahit == 0) {
565
/*
566
* Either a no, or a don't know. Go to the wire.
567
*/
568
NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
569
error = nfs34_access_otw(vp, wmode, ap->a_td,
570
ap->a_cred, &rmode);
571
if (!error &&
572
(rmode & mode) != mode)
573
error = EACCES;
574
}
575
return (error);
576
} else {
577
if ((error = nfsspec_access(ap)) != 0) {
578
return (error);
579
}
580
/*
581
* Attempt to prevent a mapped root from accessing a file
582
* which it shouldn't. We try to read a byte from the file
583
* if the user is root and the file is not zero length.
584
* After calling nfsspec_access, we should have the correct
585
* file size cached.
586
*/
587
NFSLOCKNODE(np);
588
if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
589
&& VTONFS(vp)->n_size > 0) {
590
struct iovec aiov;
591
struct uio auio;
592
char buf[1];
593
594
NFSUNLOCKNODE(np);
595
aiov.iov_base = buf;
596
aiov.iov_len = 1;
597
auio.uio_iov = &aiov;
598
auio.uio_iovcnt = 1;
599
auio.uio_offset = 0;
600
auio.uio_resid = 1;
601
auio.uio_segflg = UIO_SYSSPACE;
602
auio.uio_rw = UIO_READ;
603
auio.uio_td = ap->a_td;
604
605
if (vp->v_type == VREG)
606
error = ncl_readrpc(vp, &auio, ap->a_cred);
607
else if (vp->v_type == VDIR) {
608
char* bp;
609
bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
610
aiov.iov_base = bp;
611
aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
612
error = ncl_readdirrpc(vp, &auio, ap->a_cred,
613
ap->a_td);
614
free(bp, M_TEMP);
615
} else if (vp->v_type == VLNK)
616
error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
617
else
618
error = EACCES;
619
} else
620
NFSUNLOCKNODE(np);
621
return (error);
622
}
623
}
624
625
/*
626
* nfs open vnode op
627
* Check to see if the type is ok
628
* and that deletion is not in progress.
629
* For paged in text files, you will need to flush the page cache
630
* if consistency is lost.
631
*/
632
/* ARGSUSED */
633
static int
634
nfs_open(struct vop_open_args *ap)
635
{
636
struct vnode *vp = ap->a_vp;
637
struct nfsnode *np = VTONFS(vp);
638
struct vattr vattr;
639
int error;
640
int fmode = ap->a_mode;
641
struct ucred *cred;
642
vm_object_t obj;
643
644
if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
645
return (EOPNOTSUPP);
646
647
/*
648
* For NFSv4, we need to do the Open Op before cache validation,
649
* so that we conform to RFC3530 Sec. 9.3.1.
650
*/
651
if (NFS_ISV4(vp)) {
652
error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
653
if (error) {
654
error = nfscl_maperr(ap->a_td, error, (uid_t)0,
655
(gid_t)0);
656
return (error);
657
}
658
}
659
660
/*
661
* Now, if this Open will be doing reading, re-validate/flush the
662
* cache, so that Close/Open coherency is maintained.
663
*/
664
NFSLOCKNODE(np);
665
if (np->n_flag & NMODIFIED) {
666
NFSUNLOCKNODE(np);
667
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
668
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
669
if (VN_IS_DOOMED(vp))
670
return (EBADF);
671
}
672
error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
673
if (error == EINTR || error == EIO) {
674
if (NFS_ISV4(vp))
675
(void) nfsrpc_close(vp, 0, ap->a_td);
676
return (error);
677
}
678
NFSLOCKNODE(np);
679
np->n_attrstamp = 0;
680
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
681
if (vp->v_type == VDIR)
682
np->n_direofoffset = 0;
683
NFSUNLOCKNODE(np);
684
error = VOP_GETATTR(vp, &vattr, ap->a_cred);
685
if (error) {
686
if (NFS_ISV4(vp))
687
(void) nfsrpc_close(vp, 0, ap->a_td);
688
return (error);
689
}
690
NFSLOCKNODE(np);
691
np->n_mtime = vattr.va_mtime;
692
if (NFS_ISV4(vp))
693
np->n_change = vattr.va_filerev;
694
} else {
695
NFSUNLOCKNODE(np);
696
error = VOP_GETATTR(vp, &vattr, ap->a_cred);
697
if (error) {
698
if (NFS_ISV4(vp))
699
(void) nfsrpc_close(vp, 0, ap->a_td);
700
return (error);
701
}
702
NFSLOCKNODE(np);
703
if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
704
NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
705
if (vp->v_type == VDIR)
706
np->n_direofoffset = 0;
707
NFSUNLOCKNODE(np);
708
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
709
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
710
if (VN_IS_DOOMED(vp))
711
return (EBADF);
712
}
713
error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
714
if (error == EINTR || error == EIO) {
715
if (NFS_ISV4(vp))
716
(void) nfsrpc_close(vp, 0, ap->a_td);
717
return (error);
718
}
719
NFSLOCKNODE(np);
720
np->n_mtime = vattr.va_mtime;
721
if (NFS_ISV4(vp))
722
np->n_change = vattr.va_filerev;
723
}
724
}
725
726
/*
727
* If the object has >= 1 O_DIRECT active opens, we disable caching.
728
*/
729
if (newnfs_directio_enable && (fmode & O_DIRECT) &&
730
(vp->v_type == VREG)) {
731
if (np->n_directio_opens == 0) {
732
NFSUNLOCKNODE(np);
733
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
734
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
735
if (VN_IS_DOOMED(vp))
736
return (EBADF);
737
}
738
error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
739
if (error) {
740
if (NFS_ISV4(vp))
741
(void) nfsrpc_close(vp, 0, ap->a_td);
742
return (error);
743
}
744
NFSLOCKNODE(np);
745
np->n_flag |= NNONCACHE;
746
}
747
np->n_directio_opens++;
748
}
749
750
/* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
751
if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
752
np->n_flag |= NWRITEOPENED;
753
754
/*
755
* If this is an open for writing, capture a reference to the
756
* credentials, so they can be used by ncl_putpages(). Using
757
* these write credentials is preferable to the credentials of
758
* whatever thread happens to be doing the VOP_PUTPAGES() since
759
* the write RPCs are less likely to fail with EACCES.
760
*/
761
if ((fmode & FWRITE) != 0) {
762
cred = np->n_writecred;
763
np->n_writecred = crhold(ap->a_cred);
764
} else
765
cred = NULL;
766
NFSUNLOCKNODE(np);
767
768
if (cred != NULL)
769
crfree(cred);
770
vnode_create_vobject(vp, vattr.va_size, ap->a_td);
771
772
/*
773
* If the text file has been mmap'd, flush any dirty pages to the
774
* buffer cache and then...
775
* Make sure all writes are pushed to the NFS server. If this is not
776
* done, the modify time of the file can change while the text
777
* file is being executed. This will cause the process that is
778
* executing the text file to be terminated.
779
*/
780
if (vp->v_writecount <= -1) {
781
if ((obj = vp->v_object) != NULL &&
782
vm_object_mightbedirty(obj)) {
783
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
784
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
785
if (VN_IS_DOOMED(vp))
786
return (EBADF);
787
}
788
vnode_pager_clean_sync(vp);
789
}
790
791
/* Now, flush the buffer cache. */
792
ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
793
794
/* And, finally, make sure that n_mtime is up to date. */
795
np = VTONFS(vp);
796
NFSLOCKNODE(np);
797
np->n_mtime = np->n_vattr.na_mtime;
798
NFSUNLOCKNODE(np);
799
}
800
return (0);
801
}
802
803
/*
804
* nfs close vnode op
805
* What an NFS client should do upon close after writing is a debatable issue.
806
* Most NFS clients push delayed writes to the server upon close, basically for
807
* two reasons:
808
* 1 - So that any write errors may be reported back to the client process
809
* doing the close system call. By far the two most likely errors are
810
* NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
811
* 2 - To put a worst case upper bound on cache inconsistency between
812
* multiple clients for the file.
813
* There is also a consistency problem for Version 2 of the protocol w.r.t.
814
* not being able to tell if other clients are writing a file concurrently,
815
* since there is no way of knowing if the changed modify time in the reply
816
* is only due to the write for this client.
817
* (NFS Version 3 provides weak cache consistency data in the reply that
818
* should be sufficient to detect and handle this case.)
819
*
820
* The current code does the following:
821
* for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
822
* for NFS Version 3 - flush dirty buffers to the server but don't invalidate
823
* or commit them (this satisfies 1 and 2 except for the
824
* case where the server crashes after this close but
825
* before the commit RPC, which is felt to be "good
826
* enough". Changing the last argument to ncl_flush() to
827
* a 1 would force a commit operation, if it is felt a
828
* commit is necessary now.
829
* for NFS Version 4 - flush the dirty buffers and commit them, if
830
* nfscl_mustflush() says this is necessary.
831
* It is necessary if there is no write delegation held,
832
* in order to satisfy open/close coherency.
833
* If the file isn't cached on local stable storage,
834
* it may be necessary in order to detect "out of space"
835
* errors from the server, if the write delegation
836
* issued by the server doesn't allow the file to grow.
837
*/
838
/* ARGSUSED */
839
static int
840
nfs_close(struct vop_close_args *ap)
841
{
842
struct vnode *vp = ap->a_vp;
843
struct nfsnode *np = VTONFS(vp);
844
struct nfsvattr nfsva;
845
struct ucred *cred;
846
int error = 0, ret, localcred = 0;
847
int fmode = ap->a_fflag;
848
struct nfsmount *nmp;
849
850
if (NFSCL_FORCEDISM(vp->v_mount))
851
return (0);
852
nmp = VFSTONFS(vp->v_mount);
853
/*
854
* During shutdown, a_cred isn't valid, so just use root.
855
*/
856
if (ap->a_cred == NOCRED) {
857
cred = newnfs_getcred();
858
localcred = 1;
859
} else {
860
cred = ap->a_cred;
861
}
862
if (vp->v_type == VREG) {
863
/*
864
* Examine and clean dirty pages, regardless of NMODIFIED.
865
* This closes a major hole in close-to-open consistency.
866
* We want to push out all dirty pages (and buffers) on
867
* close, regardless of whether they were dirtied by
868
* mmap'ed writes or via write().
869
*/
870
if (nfs_clean_pages_on_close && vp->v_object) {
871
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
872
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
873
if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
874
return (EBADF);
875
}
876
vnode_pager_clean_async(vp);
877
}
878
NFSLOCKNODE(np);
879
if (np->n_flag & NMODIFIED) {
880
NFSUNLOCKNODE(np);
881
if (NFS_ISV3(vp)) {
882
/*
883
* Under NFSv3 we have dirty buffers to dispose of. We
884
* must flush them to the NFS server. We have the option
885
* of waiting all the way through the commit rpc or just
886
* waiting for the initial write. The default is to only
887
* wait through the initial write so the data is in the
888
* server's cache, which is roughly similar to the state
889
* a standard disk subsystem leaves the file in on close().
890
*
891
* We cannot clear the NMODIFIED bit in np->n_flag due to
892
* potential races with other processes, and certainly
893
* cannot clear it if we don't commit.
894
* These races occur when there is no longer the old
895
* traditional vnode locking implemented for Vnode Ops.
896
*/
897
int cm = newnfs_commit_on_close ? 1 : 0;
898
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
899
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
900
if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
901
return (EBADF);
902
}
903
error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
904
/* np->n_flag &= ~NMODIFIED; */
905
} else if (NFS_ISV4(vp)) {
906
if (!NFSHASNFSV4N(nmp) ||
907
(nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
908
nfscl_mustflush(vp) != 0) {
909
int cm = newnfs_commit_on_close ? 1 : 0;
910
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
911
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
912
if (VN_IS_DOOMED(vp) && ap->a_fflag !=
913
FNONBLOCK)
914
return (EBADF);
915
}
916
error = ncl_flush(vp, MNT_WAIT, ap->a_td,
917
cm, 0);
918
/*
919
* as above w.r.t races when clearing
920
* NMODIFIED.
921
* np->n_flag &= ~NMODIFIED;
922
*/
923
}
924
} else {
925
if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
926
NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
927
if (VN_IS_DOOMED(vp) && ap->a_fflag !=
928
FNONBLOCK)
929
return (EBADF);
930
}
931
error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
932
}
933
NFSLOCKNODE(np);
934
}
935
/*
936
* Invalidate the attribute cache in all cases.
937
* An open is going to fetch fresh attrs any way, other procs
938
* on this node that have file open will be forced to do an
939
* otw attr fetch, but this is safe.
940
* --> A user found that their RPC count dropped by 20% when
941
* this was commented out and I can't see any requirement
942
* for it, so I've disabled it when negative lookups are
943
* enabled. (What does this have to do with negative lookup
944
* caching? Well nothing, except it was reported by the
945
* same user that needed negative lookup caching and I wanted
946
* there to be a way to disable it to see if it
947
* is the cause of some caching/coherency issue that might
948
* crop up.)
949
*/
950
if (nmp->nm_negnametimeo == 0) {
951
np->n_attrstamp = 0;
952
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
953
}
954
if (np->n_flag & NWRITEERR) {
955
np->n_flag &= ~NWRITEERR;
956
error = np->n_error;
957
}
958
NFSUNLOCKNODE(np);
959
}
960
961
if (NFS_ISV4(vp)) {
962
/*
963
* Get attributes so "change" is up to date.
964
*/
965
if (error == 0 && nfscl_nodeleg(vp, 0) != 0 &&
966
vp->v_type == VREG &&
967
(nmp->nm_flag & NFSMNT_NOCTO) == 0) {
968
ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva);
969
if (!ret) {
970
np->n_change = nfsva.na_filerev;
971
(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
972
0, 0);
973
}
974
}
975
976
/*
977
* and do the close.
978
*/
979
ret = nfsrpc_close(vp, 0, ap->a_td);
980
if (!error && ret)
981
error = ret;
982
if (error)
983
error = nfscl_maperr(ap->a_td, error, (uid_t)0,
984
(gid_t)0);
985
}
986
if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
987
NFSLOCKNODE(np);
988
KASSERT((np->n_directio_opens > 0),
989
("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
990
np->n_directio_opens--;
991
if (np->n_directio_opens == 0)
992
np->n_flag &= ~NNONCACHE;
993
NFSUNLOCKNODE(np);
994
}
995
if (localcred)
996
NFSFREECRED(cred);
997
return (error);
998
}
999
1000
/*
1001
* nfs getattr call from vfs.
1002
*/
1003
static int
1004
nfs_getattr(struct vop_getattr_args *ap)
1005
{
1006
struct vnode *vp = ap->a_vp;
1007
struct thread *td = curthread; /* XXX */
1008
struct nfsnode *np = VTONFS(vp);
1009
int error = 0;
1010
struct nfsvattr nfsva;
1011
struct vattr *vap = ap->a_vap;
1012
struct vattr vattr;
1013
struct nfsmount *nmp;
1014
1015
nmp = VFSTONFS(vp->v_mount);
1016
/*
1017
* Update local times for special files.
1018
*/
1019
NFSLOCKNODE(np);
1020
if (np->n_flag & (NACC | NUPD))
1021
np->n_flag |= NCHG;
1022
NFSUNLOCKNODE(np);
1023
/*
1024
* First look in the cache.
1025
* For "syskrb5" mounts, nm_fhsize might still be zero and
1026
* cached attributes should be ignored.
1027
*/
1028
if (nmp->nm_fhsize > 0 && ncl_getattrcache(vp, &vattr) == 0) {
1029
ncl_copy_vattr(vp, vap, &vattr);
1030
1031
/*
1032
* Get the local modify time for the case of a write
1033
* delegation.
1034
*/
1035
nfscl_deleggetmodtime(vp, &vap->va_mtime);
1036
return (0);
1037
}
1038
1039
if (NFS_ISV34(vp) && nfs_prime_access_cache &&
1040
nfsaccess_cache_timeout > 0) {
1041
NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
1042
nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
1043
if (ncl_getattrcache(vp, ap->a_vap) == 0) {
1044
nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
1045
return (0);
1046
}
1047
}
1048
1049
error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva);
1050
if (error == 0)
1051
error = nfscl_loadattrcache(&vp, &nfsva, vap, 0, 0);
1052
if (!error) {
1053
/*
1054
* Get the local modify time for the case of a write
1055
* delegation.
1056
*/
1057
nfscl_deleggetmodtime(vp, &vap->va_mtime);
1058
} else if (NFS_ISV4(vp)) {
1059
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1060
}
1061
return (error);
1062
}
1063
1064
/*
1065
* nfs setattr call.
1066
*/
1067
static int
1068
nfs_setattr(struct vop_setattr_args *ap)
1069
{
1070
struct vnode *vp = ap->a_vp;
1071
struct nfsnode *np = VTONFS(vp);
1072
struct thread *td = curthread; /* XXX */
1073
struct vattr *vap = ap->a_vap;
1074
int error = 0;
1075
u_quad_t tsize;
1076
struct timespec ts;
1077
struct nfsmount *nmp;
1078
1079
#ifndef nolint
1080
tsize = (u_quad_t)0;
1081
#endif
1082
1083
/*
1084
* Only setting of UF_HIDDEN and UF_SYSTEM are supported and
1085
* only for NFSv4 servers that support them.
1086
*/
1087
nmp = VFSTONFS(vp->v_mount);
1088
if (vap->va_flags != VNOVAL && (!NFSHASNFSV4(nmp) ||
1089
(vap->va_flags & ~(UF_HIDDEN | UF_SYSTEM)) != 0 ||
1090
((vap->va_flags & UF_HIDDEN) != 0 &&
1091
!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_HIDDEN)) ||
1092
((vap->va_flags & UF_SYSTEM) != 0 &&
1093
!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_SYSTEM))))
1094
return (EOPNOTSUPP);
1095
1096
/*
1097
* Disallow write attempts if the filesystem is mounted read-only.
1098
*/
1099
if ((vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1100
vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1101
vap->va_mtime.tv_sec != VNOVAL ||
1102
vap->va_birthtime.tv_sec != VNOVAL ||
1103
vap->va_mode != (mode_t)VNOVAL) &&
1104
(vp->v_mount->mnt_flag & MNT_RDONLY))
1105
return (EROFS);
1106
if (vap->va_size != VNOVAL) {
1107
switch (vp->v_type) {
1108
case VDIR:
1109
return (EISDIR);
1110
case VCHR:
1111
case VBLK:
1112
case VSOCK:
1113
case VFIFO:
1114
if (vap->va_mtime.tv_sec == VNOVAL &&
1115
vap->va_atime.tv_sec == VNOVAL &&
1116
vap->va_birthtime.tv_sec == VNOVAL &&
1117
vap->va_mode == (mode_t)VNOVAL &&
1118
vap->va_uid == (uid_t)VNOVAL &&
1119
vap->va_gid == (gid_t)VNOVAL)
1120
return (0);
1121
vap->va_size = VNOVAL;
1122
break;
1123
default:
1124
/*
1125
* Disallow write attempts if the filesystem is
1126
* mounted read-only.
1127
*/
1128
if (vp->v_mount->mnt_flag & MNT_RDONLY)
1129
return (EROFS);
1130
/*
1131
* We run vnode_pager_setsize() early (why?),
1132
* we must set np->n_size now to avoid vinvalbuf
1133
* V_SAVE races that might setsize a lower
1134
* value.
1135
*/
1136
NFSLOCKNODE(np);
1137
tsize = np->n_size;
1138
NFSUNLOCKNODE(np);
1139
error = ncl_meta_setsize(vp, td, vap->va_size);
1140
NFSLOCKNODE(np);
1141
if (np->n_flag & NMODIFIED) {
1142
tsize = np->n_size;
1143
NFSUNLOCKNODE(np);
1144
error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1145
0 : V_SAVE, td, 1);
1146
if (error != 0) {
1147
vnode_pager_setsize(vp, tsize);
1148
return (error);
1149
}
1150
/*
1151
* Call nfscl_delegmodtime() to set the modify time
1152
* locally, as required.
1153
*/
1154
nfscl_delegmodtime(vp, NULL);
1155
} else
1156
NFSUNLOCKNODE(np);
1157
/*
1158
* np->n_size has already been set to vap->va_size
1159
* in ncl_meta_setsize(). We must set it again since
1160
* nfs_loadattrcache() could be called through
1161
* ncl_meta_setsize() and could modify np->n_size.
1162
*/
1163
NFSLOCKNODE(np);
1164
np->n_vattr.na_size = np->n_size = vap->va_size;
1165
NFSUNLOCKNODE(np);
1166
}
1167
} else {
1168
NFSLOCKNODE(np);
1169
if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1170
(np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1171
NFSUNLOCKNODE(np);
1172
error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1173
if (error == EINTR || error == EIO)
1174
return (error);
1175
} else
1176
NFSUNLOCKNODE(np);
1177
}
1178
error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1179
if (vap->va_size != VNOVAL) {
1180
if (error == 0) {
1181
nanouptime(&ts);
1182
NFSLOCKNODE(np);
1183
np->n_localmodtime = ts;
1184
NFSUNLOCKNODE(np);
1185
} else {
1186
NFSLOCKNODE(np);
1187
np->n_size = np->n_vattr.na_size = tsize;
1188
vnode_pager_setsize(vp, tsize);
1189
NFSUNLOCKNODE(np);
1190
}
1191
}
1192
if (vap->va_mtime.tv_sec != VNOVAL && error == 0)
1193
nfscl_delegmodtime(vp, &vap->va_mtime);
1194
return (error);
1195
}
1196
1197
/*
1198
* Do an nfs setattr rpc.
1199
*/
1200
static int
1201
nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1202
struct thread *td)
1203
{
1204
struct nfsnode *np = VTONFS(vp);
1205
int error, ret, attrflag, i;
1206
struct nfsvattr nfsva;
1207
1208
if (NFS_ISV34(vp)) {
1209
NFSLOCKNODE(np);
1210
for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1211
np->n_accesscache[i].stamp = 0;
1212
np->n_flag |= NDELEGMOD;
1213
NFSUNLOCKNODE(np);
1214
KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1215
}
1216
error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag);
1217
if (attrflag) {
1218
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1219
if (ret && !error)
1220
error = ret;
1221
}
1222
if (error && NFS_ISV4(vp))
1223
error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1224
return (error);
1225
}
1226
1227
/*
1228
* Get a named attribute directory for the vnode.
1229
*/
1230
static int
1231
nfs_get_namedattrdir(struct vnode *vp, struct componentname *cnp,
1232
struct vnode **vpp)
1233
{
1234
struct nfsfh *nfhp;
1235
struct nfsnode *np;
1236
struct vnode *newvp;
1237
struct nfsvattr nfsva;
1238
int attrflag, error;
1239
1240
attrflag = 0;
1241
*vpp = NULL;
1242
np = VTONFS(vp);
1243
error = nfsrpc_openattr(VFSTONFS(vp->v_mount), vp, np->n_fhp->nfh_fh,
1244
np->n_fhp->nfh_len, (cnp->cn_flags & CREATENAMED),
1245
cnp->cn_cred, curthread, &nfsva, &nfhp, &attrflag);
1246
if (error == NFSERR_NOTSUPP)
1247
error = ENOATTR;
1248
if (error == 0)
1249
error = nfscl_nget(vp->v_mount, vp, nfhp, cnp, curthread, &np,
1250
cnp->cn_lkflags);
1251
if (error != 0)
1252
return (error);
1253
newvp = NFSTOV(np);
1254
vn_irflag_set_cond(newvp, VIRF_NAMEDDIR);
1255
if (attrflag != 0)
1256
(void)nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1257
*vpp = newvp;
1258
return (0);
1259
}
1260
1261
/*
1262
* nfs lookup call, one step at a time...
1263
* First look in cache
1264
* If not found, unlock the directory nfsnode and do the rpc
1265
*/
1266
static int
1267
nfs_lookup(struct vop_lookup_args *ap)
1268
{
1269
struct componentname *cnp = ap->a_cnp;
1270
struct vnode *dvp = ap->a_dvp;
1271
struct vnode **vpp = ap->a_vpp;
1272
struct mount *mp = dvp->v_mount;
1273
uint64_t flags = cnp->cn_flags;
1274
struct vnode *newvp;
1275
struct nfsmount *nmp;
1276
struct nfsnode *np, *newnp;
1277
int error = 0, attrflag, dattrflag, ltype, ncticks;
1278
struct thread *td = curthread;
1279
struct nfsfh *nfhp;
1280
struct nfsvattr dnfsva, nfsva;
1281
struct vattr vattr;
1282
struct timespec nctime, ts;
1283
uint32_t openmode;
1284
bool is_nameddir, needs_nameddir, opennamed;
1285
1286
dattrflag = 0;
1287
*vpp = NULL;
1288
nmp = VFSTONFS(mp);
1289
opennamed = (flags & (OPENNAMED | ISLASTCN)) == (OPENNAMED | ISLASTCN);
1290
if (opennamed && (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp)))
1291
return (ENOATTR);
1292
is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1293
if ((is_nameddir && (flags & ISLASTCN) == 0 && (cnp->cn_namelen > 1 ||
1294
*cnp->cn_nameptr != '.')) ||
1295
(opennamed && !is_nameddir && (flags & ISDOTDOT) != 0))
1296
return (ENOATTR);
1297
if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1298
(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1299
return (EROFS);
1300
np = VTONFS(dvp);
1301
1302
needs_nameddir = false;
1303
if (opennamed || is_nameddir) {
1304
cnp->cn_flags &= ~MAKEENTRY;
1305
if (!is_nameddir)
1306
needs_nameddir = true;
1307
}
1308
1309
/*
1310
* If the named attribute directory is needed, acquire it now.
1311
*/
1312
newvp = NULL;
1313
if (needs_nameddir) {
1314
KASSERT(np->n_v4 == NULL, ("nfs_lookup: O_NAMEDATTR when"
1315
" n_v4 not NULL"));
1316
error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1317
if (error != 0)
1318
goto handle_error;
1319
if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
1320
*vpp = newvp;
1321
return (0);
1322
}
1323
dvp = newvp;
1324
np = VTONFS(dvp);
1325
newvp = NULL;
1326
} else if (opennamed && cnp->cn_namelen == 1 &&
1327
*cnp->cn_nameptr == '.') {
1328
vref(dvp);
1329
*vpp = dvp;
1330
return (0);
1331
}
1332
1333
if (dvp->v_type != VDIR)
1334
return (ENOTDIR);
1335
1336
/* For NFSv4, wait until any remove is done. */
1337
NFSLOCKNODE(np);
1338
while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1339
np->n_flag |= NREMOVEWANT;
1340
(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1341
}
1342
NFSUNLOCKNODE(np);
1343
1344
error = vn_dir_check_exec(dvp, cnp);
1345
if (error != 0)
1346
return (error);
1347
1348
if (!opennamed && !is_nameddir) {
1349
error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1350
if (error > 0 && error != ENOENT)
1351
return (error);
1352
if (error == -1) {
1353
/*
1354
* Lookups of "." are special and always return the
1355
* current directory. cache_lookup() already handles
1356
* associated locking bookkeeping, etc.
1357
*/
1358
if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1359
return (0);
1360
}
1361
1362
/*
1363
* We only accept a positive hit in the cache if the
1364
* change time of the file matches our cached copy.
1365
* Otherwise, we discard the cache entry and fallback
1366
* to doing a lookup RPC. We also only trust cache
1367
* entries for less than nm_nametimeo seconds.
1368
*
1369
* To better handle stale file handles and attributes,
1370
* clear the attribute cache of this node if it is a
1371
* leaf component, part of an open() call, and not
1372
* locally modified before fetching the attributes.
1373
* This should allow stale file handles to be detected
1374
* here where we can fall back to a LOOKUP RPC to
1375
* recover rather than having nfs_open() detect the
1376
* stale file handle and failing open(2) with ESTALE.
1377
*/
1378
newvp = *vpp;
1379
newnp = VTONFS(newvp);
1380
if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1381
(flags & (ISLASTCN | ISOPEN)) ==
1382
(ISLASTCN | ISOPEN) &&
1383
!(newnp->n_flag & NMODIFIED)) {
1384
NFSLOCKNODE(newnp);
1385
newnp->n_attrstamp = 0;
1386
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1387
NFSUNLOCKNODE(newnp);
1388
}
1389
if (nfscl_nodeleg(newvp, 0) == 0 ||
1390
((u_int)(ticks - ncticks) <
1391
(nmp->nm_nametimeo * hz) &&
1392
VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1393
timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1394
NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1395
return (0);
1396
}
1397
cache_purge(newvp);
1398
if (dvp != newvp)
1399
vput(newvp);
1400
else
1401
vrele(newvp);
1402
*vpp = NULL;
1403
} else if (error == ENOENT) {
1404
if (VN_IS_DOOMED(dvp))
1405
return (ENOENT);
1406
/*
1407
* We only accept a negative hit in the cache if the
1408
* modification time of the parent directory matches
1409
* the cached copy in the name cache entry.
1410
* Otherwise, we discard all of the negative cache
1411
* entries for this directory. We also only trust
1412
* negative cache entries for up to nm_negnametimeo
1413
* seconds.
1414
*/
1415
if ((u_int)(ticks - ncticks) <
1416
(nmp->nm_negnametimeo * hz) &&
1417
VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1418
timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1419
NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1420
return (ENOENT);
1421
}
1422
cache_purge_negative(dvp);
1423
}
1424
}
1425
1426
openmode = 0;
1427
#if 0
1428
/*
1429
* The use of LookupOpen breaks some builds. It is disabled
1430
* until that is fixed.
1431
*/
1432
/*
1433
* If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1434
* option, it is possible to do the Open operation in the same
1435
* compound as Lookup, so long as delegations are not being
1436
* issued. This saves doing a separate RPC for Open.
1437
* For pnfs, do not do this, since the Open+LayoutGet will
1438
* be needed as a separate RPC.
1439
*/
1440
NFSLOCKMNT(nmp);
1441
if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && !NFSHASPNFS(nmp) &&
1442
(nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1443
(!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1444
(flags & (ISLASTCN | ISOPEN | OPENNAMED))) == (ISLASTCN | ISOPEN)) {
1445
if ((flags & OPENREAD) != 0)
1446
openmode |= NFSV4OPEN_ACCESSREAD;
1447
if ((flags & OPENWRITE) != 0)
1448
openmode |= NFSV4OPEN_ACCESSWRITE;
1449
}
1450
NFSUNLOCKMNT(nmp);
1451
#endif
1452
1453
newvp = NULL;
1454
NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1455
nanouptime(&ts);
1456
error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1457
cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1458
openmode);
1459
if (dattrflag)
1460
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1461
if (needs_nameddir) {
1462
vput(dvp);
1463
dvp = ap->a_dvp;
1464
}
1465
handle_error:
1466
if (error) {
1467
if (newvp != NULL) {
1468
vput(newvp);
1469
*vpp = NULL;
1470
}
1471
1472
if (error != ENOENT) {
1473
if (NFS_ISV4(dvp))
1474
error = nfscl_maperr(td, error,
1475
(uid_t)0, (gid_t)0);
1476
return (error);
1477
}
1478
1479
/* The requested file was not found. */
1480
if ((cnp->cn_nameiop == CREATE ||
1481
cnp->cn_nameiop == RENAME) &&
1482
(flags & ISLASTCN)) {
1483
/*
1484
* XXX: UFS does a full VOP_ACCESS(dvp,
1485
* VWRITE) here instead of just checking
1486
* MNT_RDONLY.
1487
*/
1488
if (mp->mnt_flag & MNT_RDONLY)
1489
return (EROFS);
1490
return (EJUSTRETURN);
1491
}
1492
1493
if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1494
/*
1495
* Cache the modification time of the parent
1496
* directory from the post-op attributes in
1497
* the name cache entry. The negative cache
1498
* entry will be ignored once the directory
1499
* has changed. Don't bother adding the entry
1500
* if the directory has already changed.
1501
*/
1502
NFSLOCKNODE(np);
1503
if (timespeccmp(&np->n_vattr.na_mtime,
1504
&dnfsva.na_mtime, ==)) {
1505
NFSUNLOCKNODE(np);
1506
cache_enter_time(dvp, NULL, cnp,
1507
&dnfsva.na_mtime, NULL);
1508
} else
1509
NFSUNLOCKNODE(np);
1510
}
1511
return (ENOENT);
1512
}
1513
1514
/*
1515
* Handle RENAME case...
1516
*/
1517
if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1518
if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1519
free(nfhp, M_NFSFH);
1520
return (EISDIR);
1521
}
1522
error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1523
LK_EXCLUSIVE);
1524
if (error)
1525
return (error);
1526
newvp = NFSTOV(np);
1527
/*
1528
* If n_localmodtime >= time before RPC, then
1529
* a file modification operation, such as
1530
* VOP_SETATTR() of size, has occurred while
1531
* the Lookup RPC and acquisition of the vnode
1532
* happened. As such, the attributes might
1533
* be stale, with possibly an incorrect size.
1534
*/
1535
NFSLOCKNODE(np);
1536
if (timespecisset(&np->n_localmodtime) &&
1537
timespeccmp(&np->n_localmodtime, &ts, >=)) {
1538
NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1539
"stale attributes\n");
1540
attrflag = 0;
1541
}
1542
NFSUNLOCKNODE(np);
1543
if (attrflag)
1544
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1545
0, 1);
1546
*vpp = newvp;
1547
return (0);
1548
}
1549
1550
if (flags & ISDOTDOT) {
1551
ltype = NFSVOPISLOCKED(dvp);
1552
error = vfs_busy(mp, MBF_NOWAIT);
1553
if (error != 0) {
1554
vfs_ref(mp);
1555
NFSVOPUNLOCK(dvp);
1556
error = vfs_busy(mp, 0);
1557
NFSVOPLOCK(dvp, ltype | LK_RETRY);
1558
vfs_rel(mp);
1559
if (error == 0 && VN_IS_DOOMED(dvp)) {
1560
vfs_unbusy(mp);
1561
error = ENOENT;
1562
}
1563
if (error != 0)
1564
return (error);
1565
}
1566
NFSVOPUNLOCK(dvp);
1567
error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1568
cnp->cn_lkflags);
1569
if (error == 0)
1570
newvp = NFSTOV(np);
1571
vfs_unbusy(mp);
1572
if (newvp != dvp)
1573
NFSVOPLOCK(dvp, ltype | LK_RETRY);
1574
if (VN_IS_DOOMED(dvp)) {
1575
if (error == 0) {
1576
if (newvp == dvp)
1577
vrele(newvp);
1578
else
1579
vput(newvp);
1580
}
1581
error = ENOENT;
1582
}
1583
if (error != 0)
1584
return (error);
1585
if (attrflag)
1586
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1587
0, 1);
1588
} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1589
free(nfhp, M_NFSFH);
1590
vref(dvp);
1591
newvp = dvp;
1592
if (attrflag)
1593
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1594
0, 1);
1595
} else {
1596
error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1597
cnp->cn_lkflags);
1598
if (error)
1599
return (error);
1600
newvp = NFSTOV(np);
1601
if (opennamed)
1602
vn_irflag_set_cond(newvp, VIRF_NAMEDATTR);
1603
/*
1604
* If n_localmodtime >= time before RPC, then
1605
* a file modification operation, such as
1606
* VOP_SETATTR() of size, has occurred while
1607
* the Lookup RPC and acquisition of the vnode
1608
* happened. As such, the attributes might
1609
* be stale, with possibly an incorrect size.
1610
*/
1611
NFSLOCKNODE(np);
1612
if (timespecisset(&np->n_localmodtime) &&
1613
timespeccmp(&np->n_localmodtime, &ts, >=)) {
1614
NFSCL_DEBUG(4, "nfs_lookup: localmod "
1615
"stale attributes\n");
1616
attrflag = 0;
1617
}
1618
NFSUNLOCKNODE(np);
1619
if (attrflag)
1620
(void)nfscl_loadattrcache(&newvp, &nfsva, NULL,
1621
0, 1);
1622
else if ((flags & (ISLASTCN | ISOPEN)) ==
1623
(ISLASTCN | ISOPEN) &&
1624
!(np->n_flag & NMODIFIED)) {
1625
/*
1626
* Flush the attribute cache when opening a
1627
* leaf node to ensure that fresh attributes
1628
* are fetched in nfs_open() since we did not
1629
* fetch attributes from the LOOKUP reply.
1630
*/
1631
NFSLOCKNODE(np);
1632
np->n_attrstamp = 0;
1633
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1634
NFSUNLOCKNODE(np);
1635
}
1636
}
1637
if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1638
(cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1639
attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1640
cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1641
newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1642
*vpp = newvp;
1643
return (0);
1644
}
1645
1646
/*
1647
* nfs read call.
1648
* Just call ncl_bioread() to do the work.
1649
*/
1650
static int
1651
nfs_read(struct vop_read_args *ap)
1652
{
1653
struct vnode *vp = ap->a_vp;
1654
1655
switch (vp->v_type) {
1656
case VREG:
1657
return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1658
case VDIR:
1659
return (EISDIR);
1660
default:
1661
return (EOPNOTSUPP);
1662
}
1663
}
1664
1665
/*
1666
* nfs readlink call
1667
*/
1668
static int
1669
nfs_readlink(struct vop_readlink_args *ap)
1670
{
1671
struct vnode *vp = ap->a_vp;
1672
1673
if (vp->v_type != VLNK)
1674
return (EINVAL);
1675
return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1676
}
1677
1678
/*
1679
* Do a readlink rpc.
1680
* Called by ncl_doio() from below the buffer cache.
1681
*/
1682
int
1683
ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1684
{
1685
int error, ret, attrflag;
1686
struct nfsvattr nfsva;
1687
1688
error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1689
&attrflag);
1690
if (attrflag) {
1691
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1692
if (ret && !error)
1693
error = ret;
1694
}
1695
if (error && NFS_ISV4(vp))
1696
error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1697
return (error);
1698
}
1699
1700
/*
1701
* nfs read rpc call
1702
* Ditto above
1703
*/
1704
int
1705
ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1706
{
1707
int error, ret, attrflag;
1708
struct nfsvattr nfsva;
1709
struct nfsmount *nmp;
1710
1711
nmp = VFSTONFS(vp->v_mount);
1712
error = EIO;
1713
attrflag = 0;
1714
if (NFSHASPNFS(nmp))
1715
error = nfscl_doiods(vp, uiop, NULL, NULL,
1716
NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1717
NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1718
if (error != 0 && error != EFAULT)
1719
error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1720
&attrflag);
1721
if (attrflag) {
1722
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1723
if (ret && !error)
1724
error = ret;
1725
}
1726
if (error && NFS_ISV4(vp))
1727
error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1728
return (error);
1729
}
1730
1731
/*
1732
* nfs write call
1733
*/
1734
int
1735
ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1736
int *iomode, int *must_commit, int called_from_strategy, int ioflag)
1737
{
1738
struct nfsvattr nfsva;
1739
int error, attrflag, ret;
1740
struct nfsmount *nmp;
1741
1742
nmp = VFSTONFS(vp->v_mount);
1743
error = EIO;
1744
attrflag = 0;
1745
if (NFSHASPNFS(nmp))
1746
error = nfscl_doiods(vp, uiop, iomode, must_commit,
1747
NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1748
NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1749
if (error != 0 && error != EFAULT)
1750
error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1751
uiop->uio_td, &nfsva, &attrflag, called_from_strategy,
1752
ioflag);
1753
if (attrflag) {
1754
if (VTONFS(vp)->n_flag & ND_NFSV4)
1755
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 1, 1);
1756
else
1757
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1758
if (ret && !error)
1759
error = ret;
1760
}
1761
if (DOINGASYNC(vp))
1762
*iomode = NFSWRITE_FILESYNC;
1763
if (error && NFS_ISV4(vp))
1764
error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1765
return (error);
1766
}
1767
1768
/*
1769
* nfs mknod rpc
1770
* For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1771
* mode set to specify the file type and the size field for rdev.
1772
*/
1773
static int
1774
nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1775
struct vattr *vap)
1776
{
1777
struct nfsvattr nfsva, dnfsva;
1778
struct vnode *newvp = NULL;
1779
struct nfsnode *np = NULL, *dnp;
1780
struct nfsfh *nfhp;
1781
struct vattr vattr;
1782
int error = 0, attrflag, dattrflag;
1783
u_int32_t rdev;
1784
1785
if (VATTR_ISDEV(vap))
1786
rdev = vap->va_rdev;
1787
else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1788
rdev = 0xffffffff;
1789
else
1790
return (EOPNOTSUPP);
1791
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1792
return (error);
1793
error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1794
rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1795
&nfsva, &nfhp, &attrflag, &dattrflag);
1796
if (!error) {
1797
if (!nfhp)
1798
(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1799
cnp->cn_namelen, cnp->cn_cred, curthread,
1800
&dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1801
if (nfhp)
1802
error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1803
curthread, &np, LK_EXCLUSIVE);
1804
}
1805
if (dattrflag)
1806
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1807
if (!error) {
1808
newvp = NFSTOV(np);
1809
if (attrflag != 0) {
1810
error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1811
if (error != 0)
1812
vput(newvp);
1813
}
1814
}
1815
if (!error) {
1816
*vpp = newvp;
1817
} else if (NFS_ISV4(dvp)) {
1818
error = nfscl_maperr(curthread, error, vap->va_uid,
1819
vap->va_gid);
1820
}
1821
dnp = VTONFS(dvp);
1822
NFSLOCKNODE(dnp);
1823
dnp->n_flag |= NMODIFIED;
1824
if (!dattrflag) {
1825
dnp->n_attrstamp = 0;
1826
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1827
}
1828
NFSUNLOCKNODE(dnp);
1829
return (error);
1830
}
1831
1832
/*
1833
* nfs mknod vop
1834
* just call nfs_mknodrpc() to do the work.
1835
*/
1836
/* ARGSUSED */
1837
static int
1838
nfs_mknod(struct vop_mknod_args *ap)
1839
{
1840
return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1841
}
1842
1843
static struct mtx nfs_cverf_mtx;
1844
MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1845
MTX_DEF);
1846
1847
static nfsquad_t
1848
nfs_get_cverf(void)
1849
{
1850
static nfsquad_t cverf;
1851
nfsquad_t ret;
1852
static int cverf_initialized = 0;
1853
1854
mtx_lock(&nfs_cverf_mtx);
1855
if (cverf_initialized == 0) {
1856
cverf.lval[0] = arc4random();
1857
cverf.lval[1] = arc4random();
1858
cverf_initialized = 1;
1859
} else
1860
cverf.qval++;
1861
ret = cverf;
1862
mtx_unlock(&nfs_cverf_mtx);
1863
1864
return (ret);
1865
}
1866
1867
/*
1868
* nfs file create call
1869
*/
1870
static int
1871
nfs_create(struct vop_create_args *ap)
1872
{
1873
struct vnode *dvp = ap->a_dvp;
1874
struct vattr *vap = ap->a_vap;
1875
struct componentname *cnp = ap->a_cnp;
1876
struct nfsnode *np = NULL, *dnp;
1877
struct vnode *newvp = NULL;
1878
struct nfsmount *nmp;
1879
struct nfsvattr dnfsva, nfsva;
1880
struct nfsfh *nfhp;
1881
nfsquad_t cverf;
1882
int error = 0, attrflag, dattrflag, fmode = 0;
1883
struct vattr vattr;
1884
bool is_nameddir, needs_nameddir, opennamed;
1885
1886
/*
1887
* Oops, not for me..
1888
*/
1889
if (vap->va_type == VSOCK)
1890
return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1891
1892
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1893
return (error);
1894
if (vap->va_vaflags & VA_EXCLUSIVE)
1895
fmode |= O_EXCL;
1896
dnp = VTONFS(dvp);
1897
nmp = VFSTONFS(dvp->v_mount);
1898
needs_nameddir = false;
1899
if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
1900
opennamed = (cnp->cn_flags & (OPENNAMED | ISLASTCN)) ==
1901
(OPENNAMED | ISLASTCN);
1902
is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1903
if (opennamed || is_nameddir) {
1904
cnp->cn_flags &= ~MAKEENTRY;
1905
if (!is_nameddir)
1906
needs_nameddir = true;
1907
}
1908
}
1909
1910
/*
1911
* If the named attribute directory is needed, acquire it now.
1912
*/
1913
if (needs_nameddir) {
1914
KASSERT(dnp->n_v4 == NULL, ("nfs_create: O_NAMEDATTR when"
1915
" n_v4 not NULL"));
1916
error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1917
if (error != 0)
1918
return (error);
1919
dvp = newvp;
1920
dnp = VTONFS(dvp);
1921
newvp = NULL;
1922
}
1923
1924
again:
1925
/* For NFSv4, wait until any remove is done. */
1926
NFSLOCKNODE(dnp);
1927
while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1928
dnp->n_flag |= NREMOVEWANT;
1929
(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1930
}
1931
NFSUNLOCKNODE(dnp);
1932
1933
cverf = nfs_get_cverf();
1934
error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1935
vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1936
&nfhp, &attrflag, &dattrflag);
1937
if (!error) {
1938
if (nfhp == NULL)
1939
(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1940
cnp->cn_namelen, cnp->cn_cred, curthread,
1941
&dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1942
if (nfhp != NULL)
1943
error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1944
curthread, &np, LK_EXCLUSIVE);
1945
}
1946
if (dattrflag)
1947
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1948
if (!error) {
1949
newvp = NFSTOV(np);
1950
if (attrflag == 0)
1951
error = nfsrpc_getattr(newvp, cnp->cn_cred, curthread,
1952
&nfsva);
1953
if (error == 0)
1954
error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1955
}
1956
if (error) {
1957
if (newvp != NULL) {
1958
vput(newvp);
1959
newvp = NULL;
1960
}
1961
if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1962
error == NFSERR_NOTSUPP) {
1963
fmode &= ~O_EXCL;
1964
goto again;
1965
}
1966
} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1967
if (nfscl_checksattr(vap, &nfsva)) {
1968
error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1969
curthread, &nfsva, &attrflag);
1970
if (error && (vap->va_uid != (uid_t)VNOVAL ||
1971
vap->va_gid != (gid_t)VNOVAL)) {
1972
/* try again without setting uid/gid */
1973
vap->va_uid = (uid_t)VNOVAL;
1974
vap->va_gid = (uid_t)VNOVAL;
1975
error = nfsrpc_setattr(newvp, vap, NULL,
1976
cnp->cn_cred, curthread, &nfsva, &attrflag);
1977
}
1978
if (attrflag)
1979
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1980
0, 1);
1981
if (error != 0)
1982
vput(newvp);
1983
}
1984
}
1985
if (!error) {
1986
if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1987
if (dvp != newvp)
1988
cache_enter_time(dvp, newvp, cnp,
1989
&nfsva.na_ctime, NULL);
1990
else
1991
printf("nfs_create: bogus NFS server returned "
1992
"the directory as the new file object\n");
1993
}
1994
*ap->a_vpp = newvp;
1995
} else if (NFS_ISV4(dvp)) {
1996
error = nfscl_maperr(curthread, error, vap->va_uid,
1997
vap->va_gid);
1998
}
1999
NFSLOCKNODE(dnp);
2000
dnp->n_flag |= NMODIFIED;
2001
if (!dattrflag) {
2002
dnp->n_attrstamp = 0;
2003
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2004
}
2005
NFSUNLOCKNODE(dnp);
2006
if (needs_nameddir)
2007
vput(dvp);
2008
return (error);
2009
}
2010
2011
/*
2012
* nfs file remove call
2013
* To try and make nfs semantics closer to ufs semantics, a file that has
2014
* other processes using the vnode is renamed instead of removed and then
2015
* removed later on the last close.
2016
* - If v_usecount > 1
2017
* If a rename is not already in the works
2018
* call nfs_sillyrename() to set it up
2019
* else
2020
* do the remove rpc
2021
*/
2022
static int
2023
nfs_remove(struct vop_remove_args *ap)
2024
{
2025
struct vnode *vp = ap->a_vp;
2026
struct vnode *dvp = ap->a_dvp;
2027
struct componentname *cnp = ap->a_cnp;
2028
struct nfsnode *np = VTONFS(vp);
2029
int error = 0;
2030
struct vattr vattr;
2031
struct nfsmount *nmp;
2032
2033
KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
2034
if (vp->v_type == VDIR)
2035
error = EPERM;
2036
else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
2037
VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
2038
vattr.va_nlink > 1)) {
2039
nmp = VFSTONFS(vp->v_mount);
2040
/*
2041
* Purge the name cache so that the chance of a lookup for
2042
* the name succeeding while the remove is in progress is
2043
* minimized. Without node locking it can still happen, such
2044
* that an I/O op returns ESTALE, but since you get this if
2045
* another host removes the file..
2046
*/
2047
cache_purge(vp);
2048
/*
2049
* throw away biocache buffers, mainly to avoid
2050
* unnecessary delayed writes later.
2051
* Flushing here would be more correct for the case
2052
* where nfs_close() did not do a flush. However, it
2053
* could be a large performance hit for some servers
2054
* and only matters when the file name being removed is
2055
* one of multiple hard links.
2056
*/
2057
if (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2058
(nmp->nm_flag & NFSMNT_NOCTO) == 0)
2059
error = ncl_vinvalbuf(vp, 0, curthread, 1);
2060
if (error != EINTR && error != EIO)
2061
/* Do the rpc */
2062
error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
2063
cnp->cn_namelen, cnp->cn_cred, curthread, false);
2064
/*
2065
* Kludge City: If the first reply to the remove rpc is lost..
2066
* the reply to the retransmitted request will be ENOENT
2067
* since the file was in fact removed
2068
* Therefore, we cheat and return success.
2069
*/
2070
if (error == ENOENT)
2071
error = 0;
2072
} else if (!np->n_sillyrename)
2073
error = nfs_sillyrename(dvp, vp, cnp);
2074
NFSLOCKNODE(np);
2075
np->n_attrstamp = 0;
2076
NFSUNLOCKNODE(np);
2077
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2078
return (error);
2079
}
2080
2081
/*
2082
* nfs file remove rpc called from nfs_inactive
2083
*/
2084
int
2085
ncl_removeit(struct sillyrename *sp, struct vnode *vp)
2086
{
2087
/*
2088
* Make sure that the directory vnode is still valid.
2089
* XXX we should lock sp->s_dvp here.
2090
*/
2091
if (sp->s_dvp->v_type == VBAD)
2092
return (0);
2093
return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
2094
sp->s_cred, NULL, true));
2095
}
2096
2097
/*
2098
* Handle the nfsremove_status reply from the RPC function.
2099
*/
2100
static void
2101
nfs_removestatus(struct vnode *vp, nfsremove_status file_status,
2102
bool silly, struct thread *td)
2103
{
2104
2105
switch (file_status) {
2106
case NLINK_ZERO:
2107
/* Get rid of any delegation. */
2108
nfscl_delegreturnvp(vp, false, td);
2109
/* FALLTHROUGH */
2110
case DELETED:
2111
/* Throw away buffer cache blocks. */
2112
(void)ncl_vinvalbuf(vp, 0, td, 1);
2113
break;
2114
case VALID:
2115
/* Nothing to do, delegation is still ok. */
2116
break;
2117
default:
2118
break;
2119
}
2120
}
2121
2122
/*
2123
* Nfs remove rpc, called from nfs_remove() and ncl_removeit().
2124
*/
2125
static int
2126
nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
2127
int namelen, struct ucred *cred, struct thread *td, bool silly)
2128
{
2129
struct nfsvattr dnfsva, nfsva;
2130
struct nfsnode *dnp = VTONFS(dvp);
2131
struct nfsmount *nmp;
2132
int attrflag, error = 0, dattrflag;
2133
nfsremove_status file_status;
2134
2135
nmp = VFSTONFS(dvp->v_mount);
2136
NFSLOCKNODE(dnp);
2137
dnp->n_flag |= NREMOVEINPROG;
2138
NFSUNLOCKNODE(dnp);
2139
error = nfsrpc_remove(dvp, name, namelen, vp, &nfsva, &attrflag,
2140
&file_status, &dnfsva, &dattrflag, cred, td);
2141
NFSLOCKNODE(dnp);
2142
if ((dnp->n_flag & NREMOVEWANT)) {
2143
dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
2144
NFSUNLOCKNODE(dnp);
2145
wakeup((caddr_t)dnp);
2146
} else {
2147
dnp->n_flag &= ~NREMOVEINPROG;
2148
NFSUNLOCKNODE(dnp);
2149
}
2150
2151
if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
2152
if (file_status != DELETED && attrflag != 0)
2153
(void)nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2154
if ((nmp->nm_flag & NFSMNT_NOCTO) != 0)
2155
nfs_removestatus(vp, file_status, silly, td);
2156
}
2157
2158
if (dattrflag != 0)
2159
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2160
NFSLOCKNODE(dnp);
2161
dnp->n_flag |= NMODIFIED;
2162
if (dattrflag == 0) {
2163
dnp->n_attrstamp = 0;
2164
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2165
}
2166
NFSUNLOCKNODE(dnp);
2167
if (error && NFS_ISV4(dvp))
2168
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2169
return (error);
2170
}
2171
2172
/*
2173
* nfs file rename call
2174
*/
2175
static int
2176
nfs_rename(struct vop_rename_args *ap)
2177
{
2178
struct vnode *fvp = ap->a_fvp;
2179
struct vnode *tvp = ap->a_tvp;
2180
struct vnode *fdvp = ap->a_fdvp;
2181
struct vnode *tdvp = ap->a_tdvp;
2182
struct componentname *tcnp = ap->a_tcnp;
2183
struct componentname *fcnp = ap->a_fcnp;
2184
struct nfsnode *fnp = VTONFS(ap->a_fvp);
2185
struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
2186
struct nfsv4node *newv4 = NULL;
2187
struct nfsmount *nmp;
2188
int error;
2189
2190
/* Check for cross-device rename */
2191
if ((fvp->v_mount != tdvp->v_mount) ||
2192
(tvp && (fvp->v_mount != tvp->v_mount))) {
2193
error = EXDEV;
2194
goto out;
2195
}
2196
nmp = VFSTONFS(fvp->v_mount);
2197
2198
if (fvp == tvp) {
2199
printf("nfs_rename: fvp == tvp (can't happen)\n");
2200
error = 0;
2201
goto out;
2202
}
2203
if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2204
goto out;
2205
2206
/*
2207
* We have to flush B_DELWRI data prior to renaming
2208
* the file. If we don't, the delayed-write buffers
2209
* can be flushed out later after the file has gone stale
2210
* under NFSV3. NFSV2 does not have this problem because
2211
* ( as far as I can tell ) it flushes dirty buffers more
2212
* often.
2213
*
2214
* Skip the rename operation if the fsync fails, this can happen
2215
* due to the server's volume being full, when we pushed out data
2216
* that was written back to our cache earlier. Not checking for
2217
* this condition can result in potential (silent) data loss.
2218
*/
2219
if ((nmp->nm_flag & NFSMNT_NOCTO) == 0 || !NFSHASNFSV4(nmp) ||
2220
!NFSHASNFSV4N(nmp) || nfscl_mustflush(fvp) != 0)
2221
error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2222
NFSVOPUNLOCK(fvp);
2223
if (error == 0 && tvp != NULL && ((nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
2224
!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2225
nfscl_mustflush(tvp) != 0))
2226
error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2227
if (error != 0)
2228
goto out;
2229
2230
/*
2231
* If the tvp exists and is in use, sillyrename it before doing the
2232
* rename of the new file over it.
2233
* XXX Can't sillyrename a directory.
2234
*/
2235
if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2236
tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2237
vput(tvp);
2238
tvp = NULL;
2239
}
2240
2241
error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2242
tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, false, tcnp->cn_cred,
2243
curthread);
2244
2245
if (error == 0 && NFS_ISV4(tdvp)) {
2246
/*
2247
* For NFSv4, check to see if it is the same name and
2248
* replace the name, if it is different.
2249
*/
2250
newv4 = malloc(
2251
sizeof (struct nfsv4node) +
2252
tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2253
M_NFSV4NODE, M_WAITOK);
2254
NFSLOCKNODE(tdnp);
2255
NFSLOCKNODE(fnp);
2256
if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2257
(fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2258
NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2259
tcnp->cn_namelen) ||
2260
tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2261
NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2262
tdnp->n_fhp->nfh_len))) {
2263
free(fnp->n_v4, M_NFSV4NODE);
2264
fnp->n_v4 = newv4;
2265
newv4 = NULL;
2266
fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2267
fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2268
NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2269
tdnp->n_fhp->nfh_len);
2270
NFSBCOPY(tcnp->cn_nameptr,
2271
NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2272
}
2273
NFSUNLOCKNODE(tdnp);
2274
NFSUNLOCKNODE(fnp);
2275
if (newv4 != NULL)
2276
free(newv4, M_NFSV4NODE);
2277
}
2278
2279
if (fvp->v_type == VDIR) {
2280
if (tvp != NULL && tvp->v_type == VDIR)
2281
cache_purge(tdvp);
2282
cache_purge(fdvp);
2283
}
2284
2285
out:
2286
if (tdvp == tvp)
2287
vrele(tdvp);
2288
else
2289
vput(tdvp);
2290
if (tvp)
2291
vput(tvp);
2292
vrele(fdvp);
2293
vrele(fvp);
2294
/*
2295
* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2296
*/
2297
if (error == ENOENT)
2298
error = 0;
2299
return (error);
2300
}
2301
2302
/*
2303
* nfs file rename rpc called from nfs_remove() above
2304
*/
2305
static int
2306
nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2307
struct sillyrename *sp)
2308
{
2309
2310
return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2311
sdvp, NULL, sp->s_name, sp->s_namlen, true, scnp->cn_cred,
2312
curthread));
2313
}
2314
2315
/*
2316
* Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2317
*/
2318
static int
2319
nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2320
int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2321
int tnamelen, bool silly, struct ucred *cred, struct thread *td)
2322
{
2323
struct nfsvattr fnfsva, tnfsva, tvpnfsva;
2324
struct nfsnode *fdnp = VTONFS(fdvp);
2325
struct nfsnode *tdnp = VTONFS(tdvp);
2326
struct nfsmount *nmp;
2327
int error = 0, fattrflag, tattrflag, tvpattrflag;
2328
nfsremove_status tvp_status;
2329
2330
nmp = VFSTONFS(fdvp->v_mount);
2331
error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2332
tnameptr, tnamelen, &tvp_status, &fnfsva, &tnfsva, &fattrflag,
2333
&tattrflag, &tvpnfsva, &tvpattrflag, cred, td);
2334
NFSLOCKNODE(fdnp);
2335
fdnp->n_flag |= NMODIFIED;
2336
if (fattrflag != 0) {
2337
NFSUNLOCKNODE(fdnp);
2338
(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, 0, 1);
2339
} else {
2340
fdnp->n_attrstamp = 0;
2341
NFSUNLOCKNODE(fdnp);
2342
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2343
}
2344
NFSLOCKNODE(tdnp);
2345
tdnp->n_flag |= NMODIFIED;
2346
if (tattrflag != 0) {
2347
NFSUNLOCKNODE(tdnp);
2348
(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, 0, 1);
2349
} else {
2350
tdnp->n_attrstamp = 0;
2351
NFSUNLOCKNODE(tdnp);
2352
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2353
}
2354
2355
if (tvp != NULL) {
2356
if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp) &&
2357
(nmp->nm_flag & NFSMNT_NOCTO) != 0)
2358
nfs_removestatus(tvp, tvp_status, silly, td);
2359
if (!silly && tvpattrflag != 0)
2360
(void)nfscl_loadattrcache(&tvp, &tvpnfsva, NULL, 0, 1);
2361
}
2362
2363
if (error && NFS_ISV4(fdvp))
2364
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2365
return (error);
2366
}
2367
2368
/*
2369
* nfs hard link create call
2370
*/
2371
static int
2372
nfs_link(struct vop_link_args *ap)
2373
{
2374
struct vnode *vp = ap->a_vp;
2375
struct vnode *tdvp = ap->a_tdvp;
2376
struct componentname *cnp = ap->a_cnp;
2377
struct nfsnode *np, *tdnp;
2378
struct nfsvattr nfsva, dnfsva;
2379
int error = 0, attrflag, dattrflag;
2380
2381
/*
2382
* Push all writes to the server, so that the attribute cache
2383
* doesn't get "out of sync" with the server.
2384
* XXX There should be a better way!
2385
*/
2386
#ifdef notnow
2387
VOP_FSYNC(vp, MNT_WAIT, curthread);
2388
#endif
2389
2390
error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2391
cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag, &dattrflag);
2392
tdnp = VTONFS(tdvp);
2393
NFSLOCKNODE(tdnp);
2394
tdnp->n_flag |= NMODIFIED;
2395
if (dattrflag != 0) {
2396
NFSUNLOCKNODE(tdnp);
2397
(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, 0, 1);
2398
} else {
2399
tdnp->n_attrstamp = 0;
2400
NFSUNLOCKNODE(tdnp);
2401
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2402
}
2403
if (attrflag)
2404
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2405
else {
2406
np = VTONFS(vp);
2407
NFSLOCKNODE(np);
2408
np->n_attrstamp = 0;
2409
NFSUNLOCKNODE(np);
2410
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2411
}
2412
/*
2413
* If negative lookup caching is enabled, I might as well
2414
* add an entry for this node. Not necessary for correctness,
2415
* but if negative caching is enabled, then the system
2416
* must care about lookup caching hit rate, so...
2417
*/
2418
if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2419
(cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2420
if (tdvp != vp)
2421
cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2422
else
2423
printf("nfs_link: bogus NFS server returned "
2424
"the directory as the new link\n");
2425
}
2426
if (error && NFS_ISV4(vp))
2427
error = nfscl_maperr(curthread, error, (uid_t)0,
2428
(gid_t)0);
2429
return (error);
2430
}
2431
2432
/*
2433
* nfs symbolic link create call
2434
*/
2435
static int
2436
nfs_symlink(struct vop_symlink_args *ap)
2437
{
2438
struct vnode *dvp = ap->a_dvp;
2439
struct vattr *vap = ap->a_vap;
2440
struct componentname *cnp = ap->a_cnp;
2441
struct nfsvattr nfsva, dnfsva;
2442
struct nfsfh *nfhp;
2443
struct nfsnode *np = NULL, *dnp;
2444
struct vnode *newvp = NULL;
2445
int error = 0, attrflag, dattrflag, ret;
2446
2447
vap->va_type = VLNK;
2448
error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2449
ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2450
&nfsva, &nfhp, &attrflag, &dattrflag);
2451
if (nfhp) {
2452
ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2453
&np, LK_EXCLUSIVE);
2454
if (!ret)
2455
newvp = NFSTOV(np);
2456
else if (!error)
2457
error = ret;
2458
}
2459
if (newvp != NULL) {
2460
if (attrflag)
2461
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2462
} else if (!error) {
2463
/*
2464
* If we do not have an error and we could not extract the
2465
* newvp from the response due to the request being NFSv2, we
2466
* have to do a lookup in order to obtain a newvp to return.
2467
*/
2468
error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2469
cnp->cn_cred, curthread, &np);
2470
if (!error)
2471
newvp = NFSTOV(np);
2472
}
2473
if (error) {
2474
if (newvp)
2475
vput(newvp);
2476
if (NFS_ISV4(dvp))
2477
error = nfscl_maperr(curthread, error,
2478
vap->va_uid, vap->va_gid);
2479
} else {
2480
*ap->a_vpp = newvp;
2481
}
2482
2483
dnp = VTONFS(dvp);
2484
NFSLOCKNODE(dnp);
2485
dnp->n_flag |= NMODIFIED;
2486
if (dattrflag != 0) {
2487
NFSUNLOCKNODE(dnp);
2488
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2489
} else {
2490
dnp->n_attrstamp = 0;
2491
NFSUNLOCKNODE(dnp);
2492
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2493
}
2494
/*
2495
* If negative lookup caching is enabled, I might as well
2496
* add an entry for this node. Not necessary for correctness,
2497
* but if negative caching is enabled, then the system
2498
* must care about lookup caching hit rate, so...
2499
*/
2500
if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2501
(cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2502
if (dvp != newvp)
2503
cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2504
NULL);
2505
else
2506
printf("nfs_symlink: bogus NFS server returned "
2507
"the directory as the new file object\n");
2508
}
2509
return (error);
2510
}
2511
2512
/*
2513
* nfs make dir call
2514
*/
2515
static int
2516
nfs_mkdir(struct vop_mkdir_args *ap)
2517
{
2518
struct vnode *dvp = ap->a_dvp;
2519
struct vattr *vap = ap->a_vap;
2520
struct componentname *cnp = ap->a_cnp;
2521
struct nfsnode *np = NULL, *dnp;
2522
struct vnode *newvp = NULL;
2523
struct vattr vattr;
2524
struct nfsfh *nfhp;
2525
struct nfsvattr nfsva, dnfsva;
2526
int error = 0, attrflag, dattrflag, ret;
2527
2528
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2529
return (error);
2530
vap->va_type = VDIR;
2531
error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2532
vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2533
&attrflag, &dattrflag);
2534
dnp = VTONFS(dvp);
2535
NFSLOCKNODE(dnp);
2536
dnp->n_flag |= NMODIFIED;
2537
if (dattrflag != 0) {
2538
NFSUNLOCKNODE(dnp);
2539
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2540
} else {
2541
dnp->n_attrstamp = 0;
2542
NFSUNLOCKNODE(dnp);
2543
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2544
}
2545
if (nfhp) {
2546
ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2547
&np, LK_EXCLUSIVE);
2548
if (!ret) {
2549
newvp = NFSTOV(np);
2550
if (attrflag)
2551
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2552
0, 1);
2553
} else if (!error)
2554
error = ret;
2555
}
2556
if (!error && newvp == NULL) {
2557
error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2558
cnp->cn_cred, curthread, &np);
2559
if (!error) {
2560
newvp = NFSTOV(np);
2561
if (newvp->v_type != VDIR)
2562
error = EEXIST;
2563
}
2564
}
2565
if (error) {
2566
if (newvp)
2567
vput(newvp);
2568
if (NFS_ISV4(dvp))
2569
error = nfscl_maperr(curthread, error,
2570
vap->va_uid, vap->va_gid);
2571
} else {
2572
/*
2573
* If negative lookup caching is enabled, I might as well
2574
* add an entry for this node. Not necessary for correctness,
2575
* but if negative caching is enabled, then the system
2576
* must care about lookup caching hit rate, so...
2577
*/
2578
if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2579
(cnp->cn_flags & MAKEENTRY) &&
2580
attrflag != 0 && dattrflag != 0) {
2581
if (dvp != newvp)
2582
cache_enter_time(dvp, newvp, cnp,
2583
&nfsva.na_ctime, &dnfsva.na_ctime);
2584
else
2585
printf("nfs_mkdir: bogus NFS server returned "
2586
"the directory that the directory was "
2587
"created in as the new file object\n");
2588
}
2589
*ap->a_vpp = newvp;
2590
}
2591
return (error);
2592
}
2593
2594
/*
2595
* nfs remove directory call
2596
*/
2597
static int
2598
nfs_rmdir(struct vop_rmdir_args *ap)
2599
{
2600
struct vnode *vp = ap->a_vp;
2601
struct vnode *dvp = ap->a_dvp;
2602
struct componentname *cnp = ap->a_cnp;
2603
struct nfsnode *dnp;
2604
struct nfsvattr dnfsva;
2605
int error, dattrflag;
2606
2607
if (dvp == vp)
2608
return (EINVAL);
2609
error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2610
cnp->cn_cred, curthread, &dnfsva, &dattrflag);
2611
dnp = VTONFS(dvp);
2612
NFSLOCKNODE(dnp);
2613
dnp->n_flag |= NMODIFIED;
2614
if (dattrflag != 0) {
2615
NFSUNLOCKNODE(dnp);
2616
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2617
} else {
2618
dnp->n_attrstamp = 0;
2619
NFSUNLOCKNODE(dnp);
2620
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2621
}
2622
2623
cache_purge(dvp);
2624
cache_purge(vp);
2625
if (error && NFS_ISV4(dvp))
2626
error = nfscl_maperr(curthread, error, (uid_t)0,
2627
(gid_t)0);
2628
/*
2629
* Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2630
*/
2631
if (error == ENOENT)
2632
error = 0;
2633
return (error);
2634
}
2635
2636
/*
2637
* nfs readdir call
2638
*/
2639
static int
2640
nfs_readdir(struct vop_readdir_args *ap)
2641
{
2642
struct vnode *vp = ap->a_vp;
2643
struct nfsnode *np = VTONFS(vp);
2644
struct uio *uio = ap->a_uio;
2645
ssize_t tresid, left;
2646
int error = 0;
2647
struct vattr vattr;
2648
2649
if (ap->a_eofflag != NULL)
2650
*ap->a_eofflag = 0;
2651
if (vp->v_type != VDIR)
2652
return(EPERM);
2653
2654
/*
2655
* First, check for hit on the EOF offset cache
2656
*/
2657
NFSLOCKNODE(np);
2658
if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2659
(np->n_flag & NMODIFIED) == 0) {
2660
NFSUNLOCKNODE(np);
2661
if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2662
NFSLOCKNODE(np);
2663
if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2664
!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2665
NFSUNLOCKNODE(np);
2666
NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2667
if (ap->a_eofflag != NULL)
2668
*ap->a_eofflag = 1;
2669
return (0);
2670
} else
2671
NFSUNLOCKNODE(np);
2672
}
2673
} else
2674
NFSUNLOCKNODE(np);
2675
2676
/*
2677
* NFS always guarantees that directory entries don't straddle
2678
* DIRBLKSIZ boundaries. As such, we need to limit the size
2679
* to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2680
* directory entry.
2681
*/
2682
left = uio->uio_resid % DIRBLKSIZ;
2683
if (left == uio->uio_resid)
2684
return (EINVAL);
2685
uio->uio_resid -= left;
2686
2687
/*
2688
* For readdirplus, if starting to read the directory,
2689
* purge the name cache, since it will be reloaded by
2690
* this directory read.
2691
* This removes potentially stale name cache entries.
2692
*/
2693
if (uio->uio_offset == 0 &&
2694
(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_RDIRPLUS) != 0)
2695
cache_purge(vp);
2696
2697
/*
2698
* Call ncl_bioread() to do the real work.
2699
*/
2700
tresid = uio->uio_resid;
2701
error = ncl_bioread(vp, uio, 0, ap->a_cred);
2702
2703
if (!error && uio->uio_resid == tresid) {
2704
NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2705
if (ap->a_eofflag != NULL)
2706
*ap->a_eofflag = 1;
2707
}
2708
2709
/* Add the partial DIRBLKSIZ (left) back in. */
2710
uio->uio_resid += left;
2711
return (error);
2712
}
2713
2714
/*
2715
* Readdir rpc call.
2716
* Called from below the buffer cache by ncl_doio().
2717
*/
2718
int
2719
ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2720
struct thread *td)
2721
{
2722
struct nfsvattr nfsva;
2723
nfsuint64 *cookiep, cookie;
2724
struct nfsnode *dnp = VTONFS(vp);
2725
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2726
int error = 0, eof, attrflag;
2727
2728
KASSERT(uiop->uio_iovcnt == 1 &&
2729
(uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2730
(uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2731
("nfs readdirrpc bad uio"));
2732
2733
/*
2734
* If there is no cookie, assume directory was stale.
2735
*/
2736
ncl_dircookie_lock(dnp);
2737
NFSUNLOCKNODE(dnp);
2738
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2739
if (cookiep) {
2740
cookie = *cookiep;
2741
ncl_dircookie_unlock(dnp);
2742
} else {
2743
ncl_dircookie_unlock(dnp);
2744
return (NFSERR_BAD_COOKIE);
2745
}
2746
2747
if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2748
(void)ncl_fsinfo(nmp, vp, cred, td);
2749
2750
error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2751
&attrflag, &eof);
2752
if (attrflag)
2753
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2754
2755
if (!error) {
2756
/*
2757
* We are now either at the end of the directory or have filled
2758
* the block.
2759
*/
2760
if (eof) {
2761
NFSLOCKNODE(dnp);
2762
dnp->n_direofoffset = uiop->uio_offset;
2763
NFSUNLOCKNODE(dnp);
2764
} else {
2765
if (uiop->uio_resid > 0)
2766
printf("EEK! readdirrpc resid > 0\n");
2767
ncl_dircookie_lock(dnp);
2768
NFSUNLOCKNODE(dnp);
2769
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2770
*cookiep = cookie;
2771
ncl_dircookie_unlock(dnp);
2772
}
2773
} else if (NFS_ISV4(vp)) {
2774
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2775
}
2776
return (error);
2777
}
2778
2779
/*
2780
* NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2781
*/
2782
int
2783
ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2784
struct thread *td)
2785
{
2786
struct nfsvattr nfsva;
2787
nfsuint64 *cookiep, cookie;
2788
struct nfsnode *dnp = VTONFS(vp);
2789
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2790
int error = 0, attrflag, eof;
2791
2792
KASSERT(uiop->uio_iovcnt == 1 &&
2793
(uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2794
(uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2795
("nfs readdirplusrpc bad uio"));
2796
2797
/*
2798
* If there is no cookie, assume directory was stale.
2799
*/
2800
ncl_dircookie_lock(dnp);
2801
NFSUNLOCKNODE(dnp);
2802
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2803
if (cookiep) {
2804
cookie = *cookiep;
2805
ncl_dircookie_unlock(dnp);
2806
} else {
2807
ncl_dircookie_unlock(dnp);
2808
return (NFSERR_BAD_COOKIE);
2809
}
2810
2811
if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2812
(void)ncl_fsinfo(nmp, vp, cred, td);
2813
error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2814
&attrflag, &eof);
2815
if (attrflag)
2816
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2817
2818
if (!error) {
2819
/*
2820
* We are now either at end of the directory or have filled the
2821
* the block.
2822
*/
2823
if (eof) {
2824
NFSLOCKNODE(dnp);
2825
dnp->n_direofoffset = uiop->uio_offset;
2826
NFSUNLOCKNODE(dnp);
2827
} else {
2828
if (uiop->uio_resid > 0)
2829
printf("EEK! readdirplusrpc resid > 0\n");
2830
ncl_dircookie_lock(dnp);
2831
NFSUNLOCKNODE(dnp);
2832
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2833
*cookiep = cookie;
2834
ncl_dircookie_unlock(dnp);
2835
}
2836
} else if (NFS_ISV4(vp)) {
2837
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2838
}
2839
return (error);
2840
}
2841
2842
/*
2843
* Silly rename. To make the NFS filesystem that is stateless look a little
2844
* more like the "ufs" a remove of an active vnode is translated to a rename
2845
* to a funny looking filename that is removed by nfs_inactive on the
2846
* nfsnode. There is the potential for another process on a different client
2847
* to create the same funny name between the nfs_lookitup() fails and the
2848
* nfs_rename() completes, but...
2849
*/
2850
static int
2851
nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2852
{
2853
struct sillyrename *sp;
2854
struct nfsnode *np;
2855
int error;
2856
short pid;
2857
unsigned int lticks;
2858
2859
cache_purge(dvp);
2860
np = VTONFS(vp);
2861
KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2862
sp = malloc(sizeof (struct sillyrename),
2863
M_NEWNFSREQ, M_WAITOK);
2864
sp->s_cred = crhold(cnp->cn_cred);
2865
sp->s_dvp = dvp;
2866
vref(dvp);
2867
2868
/*
2869
* Fudge together a funny name.
2870
* Changing the format of the funny name to accommodate more
2871
* sillynames per directory.
2872
* The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2873
* CPU ticks since boot.
2874
*/
2875
pid = curthread->td_proc->p_pid;
2876
lticks = (unsigned int)ticks;
2877
for ( ; ; ) {
2878
sp->s_namlen = sprintf(sp->s_name,
2879
".nfs.%08x.%04x4.4", lticks,
2880
pid);
2881
if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2882
curthread, NULL))
2883
break;
2884
lticks++;
2885
}
2886
error = nfs_renameit(dvp, vp, cnp, sp);
2887
if (error)
2888
goto bad;
2889
error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2890
curthread, &np);
2891
np->n_sillyrename = sp;
2892
return (0);
2893
bad:
2894
vrele(sp->s_dvp);
2895
crfree(sp->s_cred);
2896
free(sp, M_NEWNFSREQ);
2897
return (error);
2898
}
2899
2900
/*
2901
* Look up a file name and optionally either update the file handle or
2902
* allocate an nfsnode, depending on the value of npp.
2903
* npp == NULL --> just do the lookup
2904
* *npp == NULL --> allocate a new nfsnode and make sure attributes are
2905
* handled too
2906
* *npp != NULL --> update the file handle in the vnode
2907
*/
2908
static int
2909
nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2910
struct thread *td, struct nfsnode **npp)
2911
{
2912
struct vnode *newvp = NULL, *vp;
2913
struct nfsnode *np, *dnp = VTONFS(dvp);
2914
struct nfsfh *nfhp, *onfhp;
2915
struct nfsvattr nfsva, dnfsva;
2916
struct componentname cn;
2917
int error = 0, attrflag, dattrflag;
2918
u_int hash;
2919
struct timespec ts;
2920
2921
nanouptime(&ts);
2922
error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2923
&nfhp, &attrflag, &dattrflag, 0);
2924
if (dattrflag)
2925
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2926
if (npp && !error) {
2927
if (*npp != NULL) {
2928
np = *npp;
2929
vp = NFSTOV(np);
2930
/*
2931
* For NFSv4, check to see if it is the same name and
2932
* replace the name, if it is different.
2933
*/
2934
if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2935
(np->n_v4->n4_namelen != len ||
2936
NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2937
dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2938
NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2939
dnp->n_fhp->nfh_len))) {
2940
free(np->n_v4, M_NFSV4NODE);
2941
np->n_v4 = malloc(
2942
sizeof (struct nfsv4node) +
2943
dnp->n_fhp->nfh_len + len - 1,
2944
M_NFSV4NODE, M_WAITOK);
2945
np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2946
np->n_v4->n4_namelen = len;
2947
NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2948
dnp->n_fhp->nfh_len);
2949
NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2950
}
2951
hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2952
FNV1_32_INIT);
2953
onfhp = np->n_fhp;
2954
/*
2955
* Rehash node for new file handle.
2956
*/
2957
vfs_hash_rehash(vp, hash);
2958
np->n_fhp = nfhp;
2959
if (onfhp != NULL)
2960
free(onfhp, M_NFSFH);
2961
newvp = NFSTOV(np);
2962
} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2963
free(nfhp, M_NFSFH);
2964
vref(dvp);
2965
newvp = dvp;
2966
} else {
2967
cn.cn_nameptr = name;
2968
cn.cn_namelen = len;
2969
error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2970
&np, LK_EXCLUSIVE);
2971
if (error)
2972
return (error);
2973
newvp = NFSTOV(np);
2974
/*
2975
* If n_localmodtime >= time before RPC, then
2976
* a file modification operation, such as
2977
* VOP_SETATTR() of size, has occurred while
2978
* the Lookup RPC and acquisition of the vnode
2979
* happened. As such, the attributes might
2980
* be stale, with possibly an incorrect size.
2981
*/
2982
NFSLOCKNODE(np);
2983
if (timespecisset(&np->n_localmodtime) &&
2984
timespeccmp(&np->n_localmodtime, &ts, >=)) {
2985
NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2986
"stale attributes\n");
2987
attrflag = 0;
2988
}
2989
NFSUNLOCKNODE(np);
2990
}
2991
if (!attrflag && *npp == NULL) {
2992
if (newvp == dvp)
2993
vrele(newvp);
2994
else
2995
vput(newvp);
2996
return (ENOENT);
2997
}
2998
if (attrflag)
2999
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
3000
}
3001
if (npp && *npp == NULL) {
3002
if (error) {
3003
if (newvp) {
3004
if (newvp == dvp)
3005
vrele(newvp);
3006
else
3007
vput(newvp);
3008
}
3009
} else
3010
*npp = np;
3011
}
3012
if (error && NFS_ISV4(dvp))
3013
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3014
return (error);
3015
}
3016
3017
/*
3018
* Nfs Version 3 and 4 commit rpc
3019
*/
3020
int
3021
ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
3022
struct thread *td)
3023
{
3024
struct nfsvattr nfsva;
3025
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3026
struct nfsnode *np;
3027
struct uio uio;
3028
int error, attrflag;
3029
3030
np = VTONFS(vp);
3031
error = EIO;
3032
attrflag = 0;
3033
if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
3034
uio.uio_offset = offset;
3035
uio.uio_resid = cnt;
3036
error = nfscl_doiods(vp, &uio, NULL, NULL,
3037
NFSV4OPEN_ACCESSWRITE, 1, cred, td);
3038
if (error != 0) {
3039
NFSLOCKNODE(np);
3040
np->n_flag &= ~NDSCOMMIT;
3041
NFSUNLOCKNODE(np);
3042
}
3043
}
3044
if (error != 0) {
3045
mtx_lock(&nmp->nm_mtx);
3046
if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
3047
mtx_unlock(&nmp->nm_mtx);
3048
return (0);
3049
}
3050
mtx_unlock(&nmp->nm_mtx);
3051
error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
3052
&attrflag);
3053
}
3054
if (attrflag != 0)
3055
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3056
if (error != 0 && NFS_ISV4(vp))
3057
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3058
return (error);
3059
}
3060
3061
/*
3062
* Strategy routine.
3063
* For async requests when nfsiod(s) are running, queue the request by
3064
* calling ncl_asyncio(), otherwise just all ncl_doio() to do the
3065
* request.
3066
*/
3067
static int
3068
nfs_strategy(struct vop_strategy_args *ap)
3069
{
3070
struct buf *bp;
3071
struct vnode *vp;
3072
struct ucred *cr;
3073
3074
bp = ap->a_bp;
3075
vp = ap->a_vp;
3076
KASSERT(bp->b_vp == vp, ("missing b_getvp"));
3077
KASSERT(!(bp->b_flags & B_DONE),
3078
("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
3079
3080
if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
3081
bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
3082
DEV_BSIZE);
3083
if (bp->b_iocmd == BIO_READ)
3084
cr = bp->b_rcred;
3085
else
3086
cr = bp->b_wcred;
3087
3088
/*
3089
* If the op is asynchronous and an i/o daemon is waiting
3090
* queue the request, wake it up and wait for completion
3091
* otherwise just do it ourselves.
3092
*/
3093
if ((bp->b_flags & B_ASYNC) == 0 ||
3094
ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
3095
(void) ncl_doio(vp, bp, cr, curthread, 1);
3096
return (0);
3097
}
3098
3099
/*
3100
* fsync vnode op. Just call ncl_flush() with commit == 1.
3101
*/
3102
/* ARGSUSED */
3103
static int
3104
nfs_fsync(struct vop_fsync_args *ap)
3105
{
3106
3107
if (ap->a_vp->v_type != VREG) {
3108
/*
3109
* For NFS, metadata is changed synchronously on the server,
3110
* so there is nothing to flush. Also, ncl_flush() clears
3111
* the NMODIFIED flag and that shouldn't be done here for
3112
* directories.
3113
*/
3114
return (0);
3115
}
3116
return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
3117
}
3118
3119
/*
3120
* Flush all the blocks associated with a vnode.
3121
* Walk through the buffer pool and push any dirty pages
3122
* associated with the vnode.
3123
* If the called_from_renewthread argument is TRUE, it has been called
3124
* from the NFSv4 renew thread and, as such, cannot block indefinitely
3125
* waiting for a buffer write to complete.
3126
*/
3127
int
3128
ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
3129
int commit, int called_from_renewthread)
3130
{
3131
struct nfsnode *np = VTONFS(vp);
3132
struct buf *bp;
3133
int i;
3134
struct buf *nbp;
3135
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3136
int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
3137
int passone = 1, trycnt = 0;
3138
u_quad_t off, endoff, toff;
3139
struct ucred* wcred = NULL;
3140
struct buf **bvec = NULL;
3141
struct bufobj *bo;
3142
#ifndef NFS_COMMITBVECSIZ
3143
#define NFS_COMMITBVECSIZ 20
3144
#endif
3145
struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
3146
u_int bvecsize = 0, bveccount;
3147
struct timespec ts;
3148
3149
if (called_from_renewthread != 0)
3150
slptimeo = hz;
3151
if (nmp->nm_flag & NFSMNT_INT)
3152
slpflag = PCATCH;
3153
if (!commit)
3154
passone = 0;
3155
bo = &vp->v_bufobj;
3156
/*
3157
* A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
3158
* server, but has not been committed to stable storage on the server
3159
* yet. On the first pass, the byte range is worked out and the commit
3160
* rpc is done. On the second pass, bwrite() is called to do the
3161
* job.
3162
*/
3163
again:
3164
off = (u_quad_t)-1;
3165
endoff = 0;
3166
bvecpos = 0;
3167
if (NFS_ISV34(vp) && commit) {
3168
if (bvec != NULL && bvec != bvec_on_stack)
3169
free(bvec, M_TEMP);
3170
/*
3171
* Count up how many buffers waiting for a commit.
3172
*/
3173
bveccount = 0;
3174
BO_LOCK(bo);
3175
TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3176
if (!BUF_ISLOCKED(bp) &&
3177
(bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
3178
== (B_DELWRI | B_NEEDCOMMIT))
3179
bveccount++;
3180
}
3181
/*
3182
* Allocate space to remember the list of bufs to commit. It is
3183
* important to use M_NOWAIT here to avoid a race with nfs_write.
3184
* If we can't get memory (for whatever reason), we will end up
3185
* committing the buffers one-by-one in the loop below.
3186
*/
3187
if (bveccount > NFS_COMMITBVECSIZ) {
3188
/*
3189
* Release the vnode interlock to avoid a lock
3190
* order reversal.
3191
*/
3192
BO_UNLOCK(bo);
3193
bvec = (struct buf **)
3194
malloc(bveccount * sizeof(struct buf *),
3195
M_TEMP, M_NOWAIT);
3196
BO_LOCK(bo);
3197
if (bvec == NULL) {
3198
bvec = bvec_on_stack;
3199
bvecsize = NFS_COMMITBVECSIZ;
3200
} else
3201
bvecsize = bveccount;
3202
} else {
3203
bvec = bvec_on_stack;
3204
bvecsize = NFS_COMMITBVECSIZ;
3205
}
3206
TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3207
if (bvecpos >= bvecsize)
3208
break;
3209
if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3210
nbp = TAILQ_NEXT(bp, b_bobufs);
3211
continue;
3212
}
3213
if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3214
(B_DELWRI | B_NEEDCOMMIT)) {
3215
BUF_UNLOCK(bp);
3216
nbp = TAILQ_NEXT(bp, b_bobufs);
3217
continue;
3218
}
3219
BO_UNLOCK(bo);
3220
bremfree(bp);
3221
/*
3222
* Work out if all buffers are using the same cred
3223
* so we can deal with them all with one commit.
3224
*
3225
* NOTE: we are not clearing B_DONE here, so we have
3226
* to do it later on in this routine if we intend to
3227
* initiate I/O on the bp.
3228
*
3229
* Note: to avoid loopback deadlocks, we do not
3230
* assign b_runningbufspace.
3231
*/
3232
if (wcred == NULL)
3233
wcred = bp->b_wcred;
3234
else if (wcred != bp->b_wcred)
3235
wcred = NOCRED;
3236
vfs_busy_pages(bp, 0);
3237
3238
BO_LOCK(bo);
3239
/*
3240
* bp is protected by being locked, but nbp is not
3241
* and vfs_busy_pages() may sleep. We have to
3242
* recalculate nbp.
3243
*/
3244
nbp = TAILQ_NEXT(bp, b_bobufs);
3245
3246
/*
3247
* A list of these buffers is kept so that the
3248
* second loop knows which buffers have actually
3249
* been committed. This is necessary, since there
3250
* may be a race between the commit rpc and new
3251
* uncommitted writes on the file.
3252
*/
3253
bvec[bvecpos++] = bp;
3254
toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3255
bp->b_dirtyoff;
3256
if (toff < off)
3257
off = toff;
3258
toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3259
if (toff > endoff)
3260
endoff = toff;
3261
}
3262
BO_UNLOCK(bo);
3263
}
3264
if (bvecpos > 0) {
3265
/*
3266
* Commit data on the server, as required.
3267
* If all bufs are using the same wcred, then use that with
3268
* one call for all of them, otherwise commit each one
3269
* separately.
3270
*/
3271
if (wcred != NOCRED)
3272
retv = ncl_commit(vp, off, (int)(endoff - off),
3273
wcred, td);
3274
else {
3275
retv = 0;
3276
for (i = 0; i < bvecpos; i++) {
3277
off_t off, size;
3278
bp = bvec[i];
3279
off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3280
bp->b_dirtyoff;
3281
size = (u_quad_t)(bp->b_dirtyend
3282
- bp->b_dirtyoff);
3283
retv = ncl_commit(vp, off, (int)size,
3284
bp->b_wcred, td);
3285
if (retv) break;
3286
}
3287
}
3288
3289
if (retv == NFSERR_STALEWRITEVERF)
3290
ncl_clearcommit(vp->v_mount);
3291
3292
/*
3293
* Now, either mark the blocks I/O done or mark the
3294
* blocks dirty, depending on whether the commit
3295
* succeeded.
3296
*/
3297
for (i = 0; i < bvecpos; i++) {
3298
bp = bvec[i];
3299
bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3300
if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3301
/*
3302
* Error, leave B_DELWRI intact
3303
*/
3304
vfs_unbusy_pages(bp);
3305
brelse(bp);
3306
} else {
3307
/*
3308
* Success, remove B_DELWRI ( bundirty() ).
3309
*
3310
* b_dirtyoff/b_dirtyend seem to be NFS
3311
* specific. We should probably move that
3312
* into bundirty(). XXX
3313
*/
3314
bufobj_wref(bo);
3315
bp->b_flags |= B_ASYNC;
3316
bundirty(bp);
3317
bp->b_flags &= ~B_DONE;
3318
bp->b_ioflags &= ~BIO_ERROR;
3319
bp->b_dirtyoff = bp->b_dirtyend = 0;
3320
bufdone(bp);
3321
}
3322
}
3323
}
3324
3325
/*
3326
* Start/do any write(s) that are required.
3327
*/
3328
loop:
3329
BO_LOCK(bo);
3330
TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3331
if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3332
if (waitfor != MNT_WAIT || passone)
3333
continue;
3334
3335
error = BUF_TIMELOCK(bp,
3336
LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3337
BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3338
if (error == 0) {
3339
BUF_UNLOCK(bp);
3340
goto loop;
3341
}
3342
if (error == ENOLCK) {
3343
error = 0;
3344
goto loop;
3345
}
3346
if (called_from_renewthread != 0) {
3347
/*
3348
* Return EIO so the flush will be retried
3349
* later.
3350
*/
3351
error = EIO;
3352
goto done;
3353
}
3354
if (newnfs_sigintr(nmp, td)) {
3355
error = EINTR;
3356
goto done;
3357
}
3358
if (slpflag == PCATCH) {
3359
slpflag = 0;
3360
slptimeo = 2 * hz;
3361
}
3362
goto loop;
3363
}
3364
if ((bp->b_flags & B_DELWRI) == 0)
3365
panic("nfs_fsync: not dirty");
3366
if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3367
BUF_UNLOCK(bp);
3368
continue;
3369
}
3370
BO_UNLOCK(bo);
3371
bremfree(bp);
3372
bp->b_flags |= B_ASYNC;
3373
bwrite(bp);
3374
if (newnfs_sigintr(nmp, td)) {
3375
error = EINTR;
3376
goto done;
3377
}
3378
goto loop;
3379
}
3380
if (passone) {
3381
passone = 0;
3382
BO_UNLOCK(bo);
3383
goto again;
3384
}
3385
if (waitfor == MNT_WAIT) {
3386
while (bo->bo_numoutput) {
3387
error = bufobj_wwait(bo, slpflag, slptimeo);
3388
if (error) {
3389
BO_UNLOCK(bo);
3390
if (called_from_renewthread != 0) {
3391
/*
3392
* Return EIO so that the flush will be
3393
* retried later.
3394
*/
3395
error = EIO;
3396
goto done;
3397
}
3398
error = newnfs_sigintr(nmp, td);
3399
if (error)
3400
goto done;
3401
if (slpflag == PCATCH) {
3402
slpflag = 0;
3403
slptimeo = 2 * hz;
3404
}
3405
BO_LOCK(bo);
3406
}
3407
}
3408
if (bo->bo_dirty.bv_cnt != 0 && commit) {
3409
BO_UNLOCK(bo);
3410
goto loop;
3411
}
3412
/*
3413
* Wait for all the async IO requests to drain
3414
*/
3415
BO_UNLOCK(bo);
3416
} else
3417
BO_UNLOCK(bo);
3418
if (NFSHASPNFS(nmp)) {
3419
nfscl_layoutcommit(vp, td);
3420
/*
3421
* Invalidate the attribute cache, since writes to a DS
3422
* won't update the size attribute.
3423
*/
3424
NFSLOCKNODE(np);
3425
np->n_attrstamp = 0;
3426
} else
3427
NFSLOCKNODE(np);
3428
if (np->n_flag & NWRITEERR) {
3429
error = np->n_error;
3430
np->n_flag &= ~NWRITEERR;
3431
}
3432
if (commit && bo->bo_dirty.bv_cnt == 0 &&
3433
bo->bo_numoutput == 0)
3434
np->n_flag &= ~NMODIFIED;
3435
NFSUNLOCKNODE(np);
3436
done:
3437
if (bvec != NULL && bvec != bvec_on_stack)
3438
free(bvec, M_TEMP);
3439
if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3440
(bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0)) {
3441
if (trycnt++ < 5) {
3442
/* try, try again... */
3443
passone = 1;
3444
wcred = NULL;
3445
bvec = NULL;
3446
bvecsize = 0;
3447
goto again;
3448
}
3449
vn_printf(vp, "ncl_flush failed");
3450
error = called_from_renewthread != 0 ? EIO : EBUSY;
3451
}
3452
if (error == 0) {
3453
nanouptime(&ts);
3454
NFSLOCKNODE(np);
3455
np->n_localmodtime = ts;
3456
NFSUNLOCKNODE(np);
3457
}
3458
return (error);
3459
}
3460
3461
/*
3462
* NFS advisory byte-level locks.
3463
*/
3464
static int
3465
nfs_advlock(struct vop_advlock_args *ap)
3466
{
3467
struct vnode *vp = ap->a_vp;
3468
struct ucred *cred;
3469
struct nfsnode *np = VTONFS(ap->a_vp);
3470
struct proc *p = (struct proc *)ap->a_id;
3471
struct thread *td = curthread; /* XXX */
3472
struct vattr va;
3473
int ret, error;
3474
u_quad_t size;
3475
struct nfsmount *nmp;
3476
3477
error = NFSVOPLOCK(vp, LK_EXCLUSIVE);
3478
if (error != 0)
3479
return (EBADF);
3480
nmp = VFSTONFS(vp->v_mount);
3481
if (!NFS_ISV4(vp) || (nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3482
if ((nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3483
size = np->n_size;
3484
NFSVOPUNLOCK(vp);
3485
error = lf_advlock(ap, &(vp->v_lockf), size);
3486
} else {
3487
if (nfs_advlock_p != NULL)
3488
error = nfs_advlock_p(ap);
3489
else {
3490
NFSVOPUNLOCK(vp);
3491
error = ENOLCK;
3492
}
3493
}
3494
if (error == 0 && ap->a_op == F_SETLK) {
3495
error = NFSVOPLOCK(vp, LK_SHARED);
3496
if (error == 0) {
3497
/* Mark that a file lock has been acquired. */
3498
NFSLOCKNODE(np);
3499
np->n_flag |= NHASBEENLOCKED;
3500
NFSUNLOCKNODE(np);
3501
NFSVOPUNLOCK(vp);
3502
}
3503
}
3504
return (error);
3505
} else if ((ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3506
if (vp->v_type != VREG) {
3507
error = EINVAL;
3508
goto out;
3509
}
3510
if ((ap->a_flags & F_POSIX) != 0)
3511
cred = p->p_ucred;
3512
else
3513
cred = td->td_ucred;
3514
3515
/*
3516
* If this is unlocking a write locked region, flush and
3517
* commit them before unlocking. This is required by
3518
* RFC3530 Sec. 9.3.2.
3519
*/
3520
if (ap->a_op == F_UNLCK &&
3521
nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3522
ap->a_flags))
3523
(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3524
3525
/*
3526
* Mark NFS node as might have acquired a lock.
3527
* This is separate from NHASBEENLOCKED, because it must
3528
* be done before the nfsrpc_advlock() call, which might
3529
* add a nfscllock structure to the client state.
3530
* It is used to check for the case where a nfscllock
3531
* state structure cannot exist for the file.
3532
* Only done for "oneopenown" NFSv4.1/4.2 mounts.
3533
*/
3534
if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3535
NFSLOCKNODE(np);
3536
np->n_flag |= NMIGHTBELOCKED;
3537
NFSUNLOCKNODE(np);
3538
}
3539
3540
/*
3541
* Loop around doing the lock op, while a blocking lock
3542
* must wait for the lock op to succeed.
3543
*/
3544
do {
3545
ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3546
ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3547
if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3548
ap->a_op == F_SETLK) {
3549
NFSVOPUNLOCK(vp);
3550
error = nfs_catnap(PZERO | PCATCH, ret,
3551
"ncladvl");
3552
if (error)
3553
return (EINTR);
3554
NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3555
if (VN_IS_DOOMED(vp)) {
3556
error = EBADF;
3557
goto out;
3558
}
3559
}
3560
} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3561
ap->a_op == F_SETLK);
3562
if (ret == NFSERR_DENIED) {
3563
error = EAGAIN;
3564
goto out;
3565
} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3566
error = ret;
3567
goto out;
3568
} else if (ret != 0) {
3569
error = EACCES;
3570
goto out;
3571
}
3572
3573
/*
3574
* Now, if we just got a lock, invalidate data in the buffer
3575
* cache, as required, so that the coherency conforms with
3576
* RFC3530 Sec. 9.3.2.
3577
*/
3578
if (ap->a_op == F_SETLK) {
3579
if ((np->n_flag & NMODIFIED) == 0) {
3580
np->n_attrstamp = 0;
3581
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3582
ret = VOP_GETATTR(vp, &va, cred);
3583
}
3584
if ((np->n_flag & NMODIFIED) || ret ||
3585
np->n_change != va.va_filerev) {
3586
(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3587
np->n_attrstamp = 0;
3588
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3589
ret = VOP_GETATTR(vp, &va, cred);
3590
if (!ret) {
3591
np->n_mtime = va.va_mtime;
3592
np->n_change = va.va_filerev;
3593
}
3594
}
3595
/* Mark that a file lock has been acquired. */
3596
NFSLOCKNODE(np);
3597
np->n_flag |= NHASBEENLOCKED;
3598
NFSUNLOCKNODE(np);
3599
}
3600
} else
3601
error = EOPNOTSUPP;
3602
out:
3603
NFSVOPUNLOCK(vp);
3604
return (error);
3605
}
3606
3607
/*
3608
* NFS advisory byte-level locks.
3609
*/
3610
static int
3611
nfs_advlockasync(struct vop_advlockasync_args *ap)
3612
{
3613
struct vnode *vp = ap->a_vp;
3614
u_quad_t size;
3615
int error;
3616
3617
error = NFSVOPLOCK(vp, LK_SHARED);
3618
if (error)
3619
return (error);
3620
if (NFS_ISV4(vp)) {
3621
NFSVOPUNLOCK(vp);
3622
return (EOPNOTSUPP);
3623
}
3624
if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3625
size = VTONFS(vp)->n_size;
3626
NFSVOPUNLOCK(vp);
3627
error = lf_advlockasync(ap, &(vp->v_lockf), size);
3628
} else {
3629
NFSVOPUNLOCK(vp);
3630
error = EOPNOTSUPP;
3631
}
3632
return (error);
3633
}
3634
3635
/*
3636
* Print out the contents of an nfsnode.
3637
*/
3638
static int
3639
nfs_print(struct vop_print_args *ap)
3640
{
3641
struct vnode *vp = ap->a_vp;
3642
struct nfsnode *np = VTONFS(vp);
3643
3644
printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3645
(uintmax_t)np->n_vattr.na_fsid);
3646
if (vp->v_type == VFIFO)
3647
fifo_printinfo(vp);
3648
printf("\n");
3649
return (0);
3650
}
3651
3652
/*
3653
* nfs special file access vnode op.
3654
* Essentially just get vattr and then imitate iaccess() since the device is
3655
* local to the client.
3656
*/
3657
static int
3658
nfsspec_access(struct vop_access_args *ap)
3659
{
3660
struct vattr *vap;
3661
struct ucred *cred = ap->a_cred;
3662
struct vnode *vp = ap->a_vp;
3663
accmode_t accmode = ap->a_accmode;
3664
struct vattr vattr;
3665
int error;
3666
3667
/*
3668
* Disallow write attempts on filesystems mounted read-only;
3669
* unless the file is a socket, fifo, or a block or character
3670
* device resident on the filesystem.
3671
*/
3672
if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3673
switch (vp->v_type) {
3674
case VREG:
3675
case VDIR:
3676
case VLNK:
3677
return (EROFS);
3678
default:
3679
break;
3680
}
3681
}
3682
vap = &vattr;
3683
error = VOP_GETATTR(vp, vap, cred);
3684
if (error)
3685
goto out;
3686
error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3687
accmode, cred);
3688
out:
3689
return error;
3690
}
3691
3692
/*
3693
* Read wrapper for fifos.
3694
*/
3695
static int
3696
nfsfifo_read(struct vop_read_args *ap)
3697
{
3698
struct nfsnode *np = VTONFS(ap->a_vp);
3699
int error;
3700
3701
/*
3702
* Set access flag.
3703
*/
3704
NFSLOCKNODE(np);
3705
np->n_flag |= NACC;
3706
vfs_timestamp(&np->n_atim);
3707
NFSUNLOCKNODE(np);
3708
error = fifo_specops.vop_read(ap);
3709
return error;
3710
}
3711
3712
/*
3713
* Write wrapper for fifos.
3714
*/
3715
static int
3716
nfsfifo_write(struct vop_write_args *ap)
3717
{
3718
struct nfsnode *np = VTONFS(ap->a_vp);
3719
3720
/*
3721
* Set update flag.
3722
*/
3723
NFSLOCKNODE(np);
3724
np->n_flag |= NUPD;
3725
vfs_timestamp(&np->n_mtim);
3726
NFSUNLOCKNODE(np);
3727
return(fifo_specops.vop_write(ap));
3728
}
3729
3730
/*
3731
* Close wrapper for fifos.
3732
*
3733
* Update the times on the nfsnode then do fifo close.
3734
*/
3735
static int
3736
nfsfifo_close(struct vop_close_args *ap)
3737
{
3738
struct vnode *vp = ap->a_vp;
3739
struct nfsnode *np = VTONFS(vp);
3740
struct vattr vattr;
3741
struct timespec ts;
3742
3743
NFSLOCKNODE(np);
3744
if (np->n_flag & (NACC | NUPD)) {
3745
vfs_timestamp(&ts);
3746
if (np->n_flag & NACC)
3747
np->n_atim = ts;
3748
if (np->n_flag & NUPD)
3749
np->n_mtim = ts;
3750
np->n_flag |= NCHG;
3751
if (vrefcnt(vp) == 1 &&
3752
(vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3753
VATTR_NULL(&vattr);
3754
if (np->n_flag & NACC)
3755
vattr.va_atime = np->n_atim;
3756
if (np->n_flag & NUPD)
3757
vattr.va_mtime = np->n_mtim;
3758
NFSUNLOCKNODE(np);
3759
(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3760
goto out;
3761
}
3762
}
3763
NFSUNLOCKNODE(np);
3764
out:
3765
return (fifo_specops.vop_close(ap));
3766
}
3767
3768
static int
3769
nfs_getacl(struct vop_getacl_args *ap)
3770
{
3771
int error;
3772
3773
if (ap->a_type != ACL_TYPE_NFS4)
3774
return (EOPNOTSUPP);
3775
error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3776
if (error > NFSERR_STALE) {
3777
(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3778
error = EPERM;
3779
}
3780
return (error);
3781
}
3782
3783
static int
3784
nfs_setacl(struct vop_setacl_args *ap)
3785
{
3786
int error;
3787
3788
if (ap->a_type != ACL_TYPE_NFS4)
3789
return (EOPNOTSUPP);
3790
error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3791
if (error > NFSERR_STALE) {
3792
(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3793
error = EPERM;
3794
}
3795
return (error);
3796
}
3797
3798
/*
3799
* VOP_ADVISE for NFS.
3800
* Just return 0 for any errors, since it is just a hint.
3801
*/
3802
static int
3803
nfs_advise(struct vop_advise_args *ap)
3804
{
3805
struct thread *td = curthread;
3806
struct nfsmount *nmp;
3807
uint64_t len;
3808
int error;
3809
3810
/*
3811
* First do vop_stdadvise() to handle the buffer cache.
3812
*/
3813
error = vop_stdadvise(ap);
3814
if (error != 0)
3815
return (error);
3816
if (ap->a_start < 0 || ap->a_end < 0)
3817
return (0);
3818
if (ap->a_end == OFF_MAX)
3819
len = 0;
3820
else if (ap->a_end < ap->a_start)
3821
return (0);
3822
else
3823
len = ap->a_end - ap->a_start + 1;
3824
nmp = VFSTONFS(ap->a_vp->v_mount);
3825
mtx_lock(&nmp->nm_mtx);
3826
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3827
(NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3828
0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3829
mtx_unlock(&nmp->nm_mtx);
3830
return (0);
3831
}
3832
mtx_unlock(&nmp->nm_mtx);
3833
error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3834
td->td_ucred, td);
3835
if (error == NFSERR_NOTSUPP) {
3836
mtx_lock(&nmp->nm_mtx);
3837
nmp->nm_privflag |= NFSMNTP_NOADVISE;
3838
mtx_unlock(&nmp->nm_mtx);
3839
}
3840
return (0);
3841
}
3842
3843
/*
3844
* nfs allocate call
3845
*/
3846
static int
3847
nfs_allocate(struct vop_allocate_args *ap)
3848
{
3849
struct vnode *vp = ap->a_vp;
3850
struct thread *td = curthread;
3851
struct nfsvattr nfsva;
3852
struct nfsmount *nmp;
3853
struct nfsnode *np;
3854
off_t alen;
3855
int attrflag, error, ret;
3856
struct timespec ts;
3857
struct uio io;
3858
3859
attrflag = 0;
3860
nmp = VFSTONFS(vp->v_mount);
3861
np = VTONFS(vp);
3862
mtx_lock(&nmp->nm_mtx);
3863
if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3864
(nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3865
mtx_unlock(&nmp->nm_mtx);
3866
alen = *ap->a_len;
3867
if ((uint64_t)alen > nfs_maxalloclen)
3868
alen = nfs_maxalloclen;
3869
3870
/* Check the file size limit. */
3871
io.uio_offset = *ap->a_offset;
3872
io.uio_resid = alen;
3873
error = vn_rlimit_fsize(vp, &io, td);
3874
3875
/*
3876
* Flush first to ensure that the allocate adds to the
3877
* file's allocation on the server.
3878
*/
3879
if (error == 0) {
3880
vnode_pager_clean_sync(vp);
3881
error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3882
}
3883
if (error == 0)
3884
error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3885
&nfsva, &attrflag, ap->a_cred, td);
3886
if (error == 0) {
3887
*ap->a_offset += alen;
3888
*ap->a_len -= alen;
3889
nanouptime(&ts);
3890
NFSLOCKNODE(np);
3891
np->n_localmodtime = ts;
3892
NFSUNLOCKNODE(np);
3893
} else if (error == NFSERR_NOTSUPP) {
3894
mtx_lock(&nmp->nm_mtx);
3895
nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3896
mtx_unlock(&nmp->nm_mtx);
3897
error = EINVAL;
3898
}
3899
} else {
3900
mtx_unlock(&nmp->nm_mtx);
3901
error = EINVAL;
3902
}
3903
if (attrflag != 0) {
3904
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3905
if (error == 0 && ret != 0)
3906
error = ret;
3907
}
3908
if (error != 0)
3909
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3910
return (error);
3911
}
3912
3913
/*
3914
* nfs deallocate call
3915
*/
3916
static int
3917
nfs_deallocate(struct vop_deallocate_args *ap)
3918
{
3919
struct vnode *vp = ap->a_vp;
3920
struct thread *td = curthread;
3921
struct nfsvattr nfsva;
3922
struct nfsmount *nmp;
3923
struct nfsnode *np;
3924
off_t tlen, mlen;
3925
int attrflag, error, ret;
3926
bool clipped;
3927
struct timespec ts;
3928
3929
error = 0;
3930
attrflag = 0;
3931
nmp = VFSTONFS(vp->v_mount);
3932
np = VTONFS(vp);
3933
mtx_lock(&nmp->nm_mtx);
3934
if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3935
(nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3936
mtx_unlock(&nmp->nm_mtx);
3937
tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3938
NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3939
(intmax_t)*ap->a_offset, (intmax_t)tlen,
3940
(uintmax_t)nmp->nm_maxfilesize);
3941
if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3942
/* Avoid EFBIG error return from the NFSv4.2 server. */
3943
*ap->a_len = 0;
3944
return (0);
3945
}
3946
clipped = false;
3947
if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3948
tlen = nmp->nm_maxfilesize - *ap->a_offset;
3949
if ((uint64_t)*ap->a_offset < np->n_size) {
3950
/* Limit the len to nfs_maxalloclen before EOF. */
3951
mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3952
if ((uint64_t)mlen > nfs_maxalloclen) {
3953
NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3954
tlen = nfs_maxalloclen;
3955
clipped = true;
3956
}
3957
}
3958
if (error == 0)
3959
error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3960
if (error == 0) {
3961
vnode_pager_purge_range(vp, *ap->a_offset,
3962
*ap->a_offset + tlen);
3963
error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3964
&nfsva, &attrflag, ap->a_cred, td);
3965
NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3966
}
3967
if (error == 0) {
3968
NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3969
attrflag, (uintmax_t)nfsva.na_size);
3970
nanouptime(&ts);
3971
NFSLOCKNODE(np);
3972
np->n_localmodtime = ts;
3973
NFSUNLOCKNODE(np);
3974
if (attrflag != 0) {
3975
if ((uint64_t)*ap->a_offset < nfsva.na_size)
3976
*ap->a_offset += omin((off_t)
3977
nfsva.na_size - *ap->a_offset,
3978
tlen);
3979
}
3980
if (clipped && tlen < *ap->a_len)
3981
*ap->a_len -= tlen;
3982
else
3983
*ap->a_len = 0;
3984
} else if (error == NFSERR_NOTSUPP) {
3985
mtx_lock(&nmp->nm_mtx);
3986
nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3987
mtx_unlock(&nmp->nm_mtx);
3988
}
3989
} else {
3990
mtx_unlock(&nmp->nm_mtx);
3991
error = EIO;
3992
}
3993
/*
3994
* If the NFS server cannot perform the Deallocate operation, just call
3995
* vop_stddeallocate() to perform it.
3996
*/
3997
if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
3998
error = vop_stddeallocate(ap);
3999
NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
4000
}
4001
if (attrflag != 0) {
4002
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4003
if (error == 0 && ret != 0)
4004
error = ret;
4005
}
4006
if (error != 0)
4007
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
4008
return (error);
4009
}
4010
4011
/*
4012
* nfs copy_file_range call
4013
*/
4014
static int
4015
nfs_copy_file_range(struct vop_copy_file_range_args *ap)
4016
{
4017
struct vnode *invp = ap->a_invp;
4018
struct vnode *outvp = ap->a_outvp;
4019
struct mount *mp;
4020
vm_object_t invp_obj;
4021
struct nfsvattr innfsva, outnfsva;
4022
struct vattr va, *vap;
4023
struct uio io;
4024
struct nfsmount *nmp;
4025
struct nfsnode *np;
4026
size_t len, len2;
4027
ssize_t r;
4028
int error, inattrflag, outattrflag, ret, ret2, invp_lock;
4029
off_t inoff, outoff;
4030
bool consecutive, must_commit, onevp, toeof, tryclone, tryoutcred;
4031
bool mustclone;
4032
4033
/*
4034
* NFSv4.2 Copy is not permitted for infile == outfile.
4035
* The NFSv4.2 Clone operation does work on non-overlapping
4036
* byte ranges in the same file, but only if offsets
4037
* (and len if not to EOF) are aligned properly.
4038
* TODO: copy_file_range() between multiple NFS mountpoints
4039
* --> This is not possible now, since each mount appears to
4040
* the NFSv4.n server as a separate client.
4041
*/
4042
if ((invp == outvp && (ap->a_flags & COPY_FILE_RANGE_CLONE) == 0) ||
4043
(invp != outvp && invp->v_mount != outvp->v_mount)) {
4044
generic_copy:
4045
return (ENOSYS);
4046
}
4047
if (invp == outvp) {
4048
onevp = true;
4049
invp_lock = LK_EXCLUSIVE;
4050
} else {
4051
onevp = false;
4052
invp_lock = LK_SHARED;
4053
}
4054
mustclone = false;
4055
if (onevp || (ap->a_flags & COPY_FILE_RANGE_CLONE) != 0)
4056
mustclone = true;
4057
relock:
4058
inoff = *ap->a_inoffp;
4059
outoff = *ap->a_outoffp;
4060
4061
/* Lock vnode(s), avoiding risk of deadlock. */
4062
do {
4063
mp = NULL;
4064
error = vn_start_write(outvp, &mp, V_WAIT);
4065
if (error == 0) {
4066
error = vn_lock(outvp, LK_EXCLUSIVE);
4067
if (error == 0) {
4068
if (onevp)
4069
break;
4070
error = vn_lock(invp, invp_lock | LK_NOWAIT);
4071
if (error == 0)
4072
break;
4073
VOP_UNLOCK(outvp);
4074
if (mp != NULL)
4075
vn_finished_write(mp);
4076
mp = NULL;
4077
error = vn_lock(invp, invp_lock);
4078
if (error == 0)
4079
VOP_UNLOCK(invp);
4080
}
4081
}
4082
if (mp != NULL)
4083
vn_finished_write(mp);
4084
} while (error == 0);
4085
if (error != 0)
4086
return (error);
4087
4088
/*
4089
* More reasons to avoid nfs copy/clone: not NFSv4.2, explicitly
4090
* disabled or requires cloning and unable to clone.
4091
* Only clone if the clone_blksize attribute is supported
4092
* and the clone_blksize is greater than 0.
4093
* Alignment of offsets and length will be checked later.
4094
*/
4095
nmp = VFSTONFS(invp->v_mount);
4096
np = VTONFS(invp);
4097
mtx_lock(&nmp->nm_mtx);
4098
if ((nmp->nm_privflag & NFSMNTP_NOCOPY) != 0)
4099
mustclone = true;
4100
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4101
(mustclone && (!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4102
NFSATTRBIT_CLONEBLKSIZE) || nmp->nm_cloneblksize == 0))) {
4103
mtx_unlock(&nmp->nm_mtx);
4104
VOP_UNLOCK(invp);
4105
if (!onevp)
4106
VOP_UNLOCK(outvp); /* For onevp, same as invp. */
4107
if (mp != NULL)
4108
vn_finished_write(mp);
4109
goto generic_copy;
4110
}
4111
mtx_unlock(&nmp->nm_mtx);
4112
4113
/*
4114
* Do the vn_rlimit_fsize() check. Should this be above the VOP layer?
4115
*/
4116
io.uio_offset = *ap->a_outoffp;
4117
io.uio_resid = *ap->a_lenp;
4118
error = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
4119
*ap->a_lenp = io.uio_resid;
4120
/*
4121
* No need to call vn_rlimit_fsizex_res before return, since the uio is
4122
* local.
4123
*/
4124
4125
/*
4126
* Flush the input file so that the data is up to date before
4127
* the copy. Flush writes for the output file so that they
4128
* do not overwrite the data copied to the output file by the Copy.
4129
* Set the commit argument for both flushes so that the data is on
4130
* stable storage before the Copy RPC. This is done in case the
4131
* server reboots during the Copy and needs to be redone.
4132
*/
4133
if (error == 0) {
4134
invp_obj = invp->v_object;
4135
if (invp_obj != NULL && vm_object_mightbedirty(invp_obj)) {
4136
if (invp_lock != LK_EXCLUSIVE) {
4137
KASSERT(!onevp, ("nfs_copy_file_range: "
4138
"invp_lock LK_SHARED for onevp"));
4139
invp_lock = LK_EXCLUSIVE;
4140
VOP_UNLOCK(invp);
4141
VOP_UNLOCK(outvp);
4142
if (mp != NULL)
4143
vn_finished_write(mp);
4144
goto relock;
4145
}
4146
vnode_pager_clean_sync(invp);
4147
}
4148
error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
4149
}
4150
if (error == 0)
4151
error = ncl_vinvalbuf(outvp, V_SAVE, curthread, 0);
4152
4153
/* Do the actual NFSv4.2 RPC. */
4154
ret = ret2 = 0;
4155
len = *ap->a_lenp;
4156
mtx_lock(&nmp->nm_mtx);
4157
if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
4158
consecutive = true;
4159
else
4160
consecutive = false;
4161
mtx_unlock(&nmp->nm_mtx);
4162
tryoutcred = true;
4163
must_commit = false;
4164
toeof = false;
4165
4166
if (error == 0) {
4167
vap = &VTONFS(invp)->n_vattr.na_vattr;
4168
error = VOP_GETATTR(invp, vap, ap->a_incred);
4169
if (error == 0) {
4170
/*
4171
* Clip "len" at va_size so that RFC compliant servers
4172
* will not reply NFSERR_INVAL.
4173
* Setting "len == 0" for the RPC would be preferred,
4174
* but some Linux servers do not support that.
4175
* If the len is being set to 0, do a Setattr RPC to
4176
* set the server's atime. This behaviour was the
4177
* preferred one for the FreeBSD "collective".
4178
*/
4179
if (inoff >= vap->va_size) {
4180
*ap->a_lenp = len = 0;
4181
if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) ==
4182
0) {
4183
VATTR_NULL(&va);
4184
va.va_atime.tv_sec = 0;
4185
va.va_atime.tv_nsec = 0;
4186
va.va_vaflags = VA_UTIMES_NULL;
4187
inattrflag = 0;
4188
error = nfsrpc_setattr(invp, &va, NULL,
4189
ap->a_incred, curthread, &innfsva,
4190
&inattrflag);
4191
if (inattrflag != 0)
4192
ret = nfscl_loadattrcache(&invp,
4193
&innfsva, NULL, 0, 1);
4194
if (error == 0 && ret != 0)
4195
error = ret;
4196
}
4197
} else if (inoff + len >= vap->va_size) {
4198
toeof = true;
4199
*ap->a_lenp = len = vap->va_size - inoff;
4200
}
4201
} else
4202
error = 0;
4203
}
4204
4205
/*
4206
* For cloning, the offsets must be clone blksize aligned and
4207
* the len must be blksize aligned unless it goes to EOF on
4208
* the input file.
4209
*/
4210
tryclone = false;
4211
if (len > 0) {
4212
if (error == 0 && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4213
NFSATTRBIT_CLONEBLKSIZE) && nmp->nm_cloneblksize != 0 &&
4214
(inoff % nmp->nm_cloneblksize) == 0 &&
4215
(outoff % nmp->nm_cloneblksize) == 0 &&
4216
(toeof || (len % nmp->nm_cloneblksize) == 0))
4217
tryclone = true;
4218
else if (mustclone)
4219
error = ENOSYS;
4220
}
4221
4222
/*
4223
* len will be set to 0 upon a successful Copy RPC.
4224
* As such, this only loops when the Copy/Clone RPC needs to be retried.
4225
*/
4226
while (len > 0 && error == 0) {
4227
inattrflag = outattrflag = 0;
4228
len2 = len;
4229
if (tryclone) {
4230
if (tryoutcred)
4231
error = nfsrpc_clone(invp, ap->a_inoffp, outvp,
4232
ap->a_outoffp, &len2, toeof, &inattrflag,
4233
&innfsva, &outattrflag, &outnfsva,
4234
ap->a_outcred);
4235
else
4236
error = nfsrpc_clone(invp, ap->a_inoffp, outvp,
4237
ap->a_outoffp, &len2, toeof, &inattrflag,
4238
&innfsva, &outattrflag, &outnfsva,
4239
ap->a_incred);
4240
} else {
4241
if (tryoutcred)
4242
error = nfsrpc_copy_file_range(invp,
4243
ap->a_inoffp, outvp, ap->a_outoffp, &len2,
4244
ap->a_flags, &inattrflag, &innfsva,
4245
&outattrflag, &outnfsva,
4246
ap->a_outcred, consecutive, &must_commit);
4247
else
4248
error = nfsrpc_copy_file_range(invp,
4249
ap->a_inoffp, outvp, ap->a_outoffp, &len2,
4250
ap->a_flags, &inattrflag, &innfsva,
4251
&outattrflag, &outnfsva,
4252
ap->a_incred, consecutive, &must_commit);
4253
}
4254
if (inattrflag != 0)
4255
ret = nfscl_loadattrcache(&invp, &innfsva, NULL, 0, 1);
4256
if (outattrflag != 0)
4257
ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
4258
1, 1);
4259
if (error == 0) {
4260
if (consecutive == false) {
4261
if (len2 == len) {
4262
mtx_lock(&nmp->nm_mtx);
4263
nmp->nm_privflag |=
4264
NFSMNTP_NOCONSECUTIVE;
4265
mtx_unlock(&nmp->nm_mtx);
4266
} else
4267
error = NFSERR_OFFLOADNOREQS;
4268
}
4269
*ap->a_lenp = len2;
4270
len = 0;
4271
if (len2 > 0 && must_commit && error == 0)
4272
error = ncl_commit(outvp, outoff, *ap->a_lenp,
4273
ap->a_outcred, curthread);
4274
if (error == 0 && ret != 0)
4275
error = ret;
4276
if (error == 0 && ret2 != 0)
4277
error = ret2;
4278
} else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4279
/*
4280
* Try consecutive == false, which is ok only if all
4281
* bytes are copied.
4282
* If only some bytes were copied when consecutive
4283
* is false, there is no way to know which bytes
4284
* still need to be written.
4285
*/
4286
consecutive = false;
4287
error = 0;
4288
} else if (error == NFSERR_ACCES && tryoutcred) {
4289
/* Try again with incred. */
4290
tryoutcred = false;
4291
error = 0;
4292
} else if (tryclone && error != 0) {
4293
if (mustclone) {
4294
error = ENOSYS;
4295
} else {
4296
tryclone = false;
4297
error = 0;
4298
}
4299
}
4300
if (error == NFSERR_STALEWRITEVERF) {
4301
/*
4302
* Server rebooted, so do it all again.
4303
*/
4304
*ap->a_inoffp = inoff;
4305
*ap->a_outoffp = outoff;
4306
len = *ap->a_lenp;
4307
must_commit = false;
4308
error = 0;
4309
}
4310
}
4311
VOP_UNLOCK(invp);
4312
if (!onevp)
4313
VOP_UNLOCK(outvp); /* For onevp, same as invp. */
4314
if (mp != NULL)
4315
vn_finished_write(mp);
4316
if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4317
error == NFSERR_ACCES || error == ENOSYS) {
4318
/*
4319
* Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4320
* use a_incred for the read and a_outcred for the write, so
4321
* try this for NFSERR_ACCES failures for the Copy.
4322
* For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4323
* never succeed, so disable it.
4324
*/
4325
if (error != NFSERR_ACCES && error != ENOSYS) {
4326
/* Can never do Copy on this mount. */
4327
mtx_lock(&nmp->nm_mtx);
4328
nmp->nm_privflag |= NFSMNTP_NOCOPY;
4329
mtx_unlock(&nmp->nm_mtx);
4330
}
4331
*ap->a_inoffp = inoff;
4332
*ap->a_outoffp = outoff;
4333
error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4334
ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4335
ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4336
} else if (error != 0)
4337
*ap->a_lenp = 0;
4338
4339
if (error != 0)
4340
error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4341
return (error);
4342
}
4343
4344
/*
4345
* nfs ioctl call
4346
*/
4347
static int
4348
nfs_ioctl(struct vop_ioctl_args *ap)
4349
{
4350
struct vnode *vp = ap->a_vp;
4351
struct nfsvattr nfsva;
4352
struct nfsmount *nmp;
4353
int attrflag, content, error, ret;
4354
bool eof = false; /* shut up compiler. */
4355
4356
/* Do the actual NFSv4.2 RPC. */
4357
switch (ap->a_command) {
4358
case FIOSEEKDATA:
4359
content = NFSV4CONTENT_DATA;
4360
break;
4361
case FIOSEEKHOLE:
4362
content = NFSV4CONTENT_HOLE;
4363
break;
4364
default:
4365
return (ENOTTY);
4366
}
4367
4368
error = vn_lock(vp, LK_EXCLUSIVE);
4369
if (error != 0)
4370
return (EBADF);
4371
4372
if (vp->v_type != VREG) {
4373
VOP_UNLOCK(vp);
4374
return (ENOTTY);
4375
}
4376
nmp = VFSTONFS(vp->v_mount);
4377
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4378
VOP_UNLOCK(vp);
4379
error = vop_stdioctl(ap);
4380
return (error);
4381
}
4382
4383
attrflag = 0;
4384
if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4385
error = ENXIO;
4386
else {
4387
/*
4388
* Flush all writes, so that the server is up to date.
4389
* Although a Commit is not required, the commit argument
4390
* is set so that, for a pNFS File/Flexible File Layout
4391
* server, the LayoutCommit will be done to ensure the file
4392
* size is up to date on the Metadata Server.
4393
*/
4394
4395
vnode_pager_clean_sync(vp);
4396
error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4397
if (error == 0)
4398
error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4399
content, ap->a_cred, &nfsva, &attrflag);
4400
/* If at eof for FIOSEEKDATA, return ENXIO. */
4401
if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4402
error = ENXIO;
4403
}
4404
if (attrflag != 0) {
4405
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4406
if (error == 0 && ret != 0)
4407
error = ret;
4408
}
4409
NFSVOPUNLOCK(vp);
4410
4411
if (error != 0)
4412
error = ENXIO;
4413
return (error);
4414
}
4415
4416
/*
4417
* nfs getextattr call
4418
*/
4419
static int
4420
nfs_getextattr(struct vop_getextattr_args *ap)
4421
{
4422
struct vnode *vp = ap->a_vp;
4423
struct nfsmount *nmp;
4424
struct ucred *cred;
4425
struct thread *td = ap->a_td;
4426
struct nfsvattr nfsva;
4427
ssize_t len;
4428
int attrflag, error, ret;
4429
4430
nmp = VFSTONFS(vp->v_mount);
4431
mtx_lock(&nmp->nm_mtx);
4432
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4433
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4434
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4435
mtx_unlock(&nmp->nm_mtx);
4436
return (EOPNOTSUPP);
4437
}
4438
mtx_unlock(&nmp->nm_mtx);
4439
4440
cred = ap->a_cred;
4441
if (cred == NULL)
4442
cred = td->td_ucred;
4443
/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4444
attrflag = 0;
4445
error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4446
&attrflag, cred, td);
4447
if (attrflag != 0) {
4448
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4449
if (error == 0 && ret != 0)
4450
error = ret;
4451
}
4452
if (error == 0 && ap->a_size != NULL)
4453
*ap->a_size = len;
4454
4455
switch (error) {
4456
case NFSERR_NOTSUPP:
4457
case NFSERR_OPILLEGAL:
4458
mtx_lock(&nmp->nm_mtx);
4459
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4460
mtx_unlock(&nmp->nm_mtx);
4461
error = EOPNOTSUPP;
4462
break;
4463
case NFSERR_NOXATTR:
4464
case NFSERR_XATTR2BIG:
4465
error = ENOATTR;
4466
break;
4467
default:
4468
error = nfscl_maperr(td, error, 0, 0);
4469
break;
4470
}
4471
return (error);
4472
}
4473
4474
/*
4475
* nfs setextattr call
4476
*/
4477
static int
4478
nfs_setextattr(struct vop_setextattr_args *ap)
4479
{
4480
struct vnode *vp = ap->a_vp;
4481
struct nfsmount *nmp;
4482
struct ucred *cred;
4483
struct thread *td = ap->a_td;
4484
struct nfsvattr nfsva;
4485
int attrflag, error, ret;
4486
4487
nmp = VFSTONFS(vp->v_mount);
4488
mtx_lock(&nmp->nm_mtx);
4489
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4490
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4491
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4492
mtx_unlock(&nmp->nm_mtx);
4493
return (EOPNOTSUPP);
4494
}
4495
mtx_unlock(&nmp->nm_mtx);
4496
4497
if (ap->a_uio->uio_resid < 0)
4498
return (EINVAL);
4499
cred = ap->a_cred;
4500
if (cred == NULL)
4501
cred = td->td_ucred;
4502
/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4503
attrflag = 0;
4504
error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4505
&attrflag, cred, td);
4506
if (attrflag != 0) {
4507
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4508
if (error == 0 && ret != 0)
4509
error = ret;
4510
}
4511
4512
switch (error) {
4513
case NFSERR_NOTSUPP:
4514
case NFSERR_OPILLEGAL:
4515
mtx_lock(&nmp->nm_mtx);
4516
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4517
mtx_unlock(&nmp->nm_mtx);
4518
error = EOPNOTSUPP;
4519
break;
4520
case NFSERR_NOXATTR:
4521
case NFSERR_XATTR2BIG:
4522
error = ENOATTR;
4523
break;
4524
default:
4525
error = nfscl_maperr(td, error, 0, 0);
4526
break;
4527
}
4528
return (error);
4529
}
4530
4531
/*
4532
* nfs listextattr call
4533
*/
4534
static int
4535
nfs_listextattr(struct vop_listextattr_args *ap)
4536
{
4537
struct vnode *vp = ap->a_vp;
4538
struct nfsmount *nmp;
4539
struct ucred *cred;
4540
struct thread *td = ap->a_td;
4541
struct nfsvattr nfsva;
4542
size_t len, len2;
4543
uint64_t cookie;
4544
int attrflag, error, ret;
4545
bool eof;
4546
4547
nmp = VFSTONFS(vp->v_mount);
4548
mtx_lock(&nmp->nm_mtx);
4549
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4550
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4551
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4552
mtx_unlock(&nmp->nm_mtx);
4553
return (EOPNOTSUPP);
4554
}
4555
mtx_unlock(&nmp->nm_mtx);
4556
4557
cred = ap->a_cred;
4558
if (cred == NULL)
4559
cred = td->td_ucred;
4560
4561
/* Loop around doing List Extended Attribute RPCs. */
4562
eof = false;
4563
cookie = 0;
4564
len2 = 0;
4565
error = 0;
4566
while (!eof && error == 0) {
4567
len = nmp->nm_rsize;
4568
attrflag = 0;
4569
error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4570
&nfsva, &attrflag, cred, td);
4571
if (attrflag != 0) {
4572
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4573
if (error == 0 && ret != 0)
4574
error = ret;
4575
}
4576
if (error == 0) {
4577
len2 += len;
4578
if (len2 > SSIZE_MAX)
4579
error = ENOATTR;
4580
}
4581
}
4582
if (error == 0 && ap->a_size != NULL)
4583
*ap->a_size = len2;
4584
4585
switch (error) {
4586
case NFSERR_NOTSUPP:
4587
case NFSERR_OPILLEGAL:
4588
mtx_lock(&nmp->nm_mtx);
4589
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4590
mtx_unlock(&nmp->nm_mtx);
4591
error = EOPNOTSUPP;
4592
break;
4593
case NFSERR_NOXATTR:
4594
case NFSERR_XATTR2BIG:
4595
error = ENOATTR;
4596
break;
4597
default:
4598
error = nfscl_maperr(td, error, 0, 0);
4599
break;
4600
}
4601
return (error);
4602
}
4603
4604
/*
4605
* nfs setextattr call
4606
*/
4607
static int
4608
nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4609
{
4610
struct vnode *vp = ap->a_vp;
4611
struct nfsmount *nmp;
4612
struct nfsvattr nfsva;
4613
int attrflag, error, ret;
4614
4615
nmp = VFSTONFS(vp->v_mount);
4616
mtx_lock(&nmp->nm_mtx);
4617
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4618
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4619
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4620
mtx_unlock(&nmp->nm_mtx);
4621
return (EOPNOTSUPP);
4622
}
4623
mtx_unlock(&nmp->nm_mtx);
4624
4625
/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4626
attrflag = 0;
4627
error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4628
ap->a_td);
4629
if (attrflag != 0) {
4630
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4631
if (error == 0 && ret != 0)
4632
error = ret;
4633
}
4634
4635
switch (error) {
4636
case NFSERR_NOTSUPP:
4637
case NFSERR_OPILLEGAL:
4638
mtx_lock(&nmp->nm_mtx);
4639
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4640
mtx_unlock(&nmp->nm_mtx);
4641
error = EOPNOTSUPP;
4642
break;
4643
case NFSERR_NOXATTR:
4644
case NFSERR_XATTR2BIG:
4645
error = ENOATTR;
4646
break;
4647
default:
4648
error = nfscl_maperr(ap->a_td, error, 0, 0);
4649
break;
4650
}
4651
return (error);
4652
}
4653
4654
/*
4655
* Return POSIX pathconf information applicable to nfs filesystems.
4656
*/
4657
static int
4658
nfs_pathconf(struct vop_pathconf_args *ap)
4659
{
4660
struct nfsv3_pathconf pc;
4661
struct nfsvattr nfsva;
4662
struct vnode *vp = ap->a_vp;
4663
struct nfsmount *nmp;
4664
struct thread *td = curthread;
4665
off_t off;
4666
uint32_t clone_blksize;
4667
bool eof, has_namedattr, named_enabled;
4668
int attrflag, error;
4669
struct nfsnode *np;
4670
4671
nmp = VFSTONFS(vp->v_mount);
4672
np = VTONFS(vp);
4673
named_enabled = false;
4674
has_namedattr = false;
4675
clone_blksize = 0;
4676
if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4677
ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4678
ap->a_name == _PC_NO_TRUNC)) ||
4679
(NFS_ISV4(vp) && (ap->a_name == _PC_ACL_NFS4 ||
4680
ap->a_name == _PC_HAS_NAMEDATTR ||
4681
ap->a_name == _PC_CLONE_BLKSIZE))) {
4682
/*
4683
* Since only the above 4 a_names are returned by the NFSv3
4684
* Pathconf RPC, there is no point in doing it for others.
4685
* For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4686
* be used for _PC_ACL_NFS4, _PC_HAS_NAMEDATTR and
4687
* _PC_CLONE_BLKSIZE as well.
4688
*/
4689
error = nfsrpc_pathconf(vp, &pc, &has_namedattr, &clone_blksize,
4690
td->td_ucred, td, &nfsva, &attrflag);
4691
if (attrflag != 0)
4692
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4693
if (error != 0)
4694
return (error);
4695
} else if (NFS_ISV4(vp) && ap->a_name == _PC_NAMEDATTR_ENABLED &&
4696
(np->n_flag & NNAMEDNOTSUPP) == 0) {
4697
struct nfsfh *nfhp;
4698
4699
error = nfsrpc_openattr(nmp, vp, np->n_fhp->nfh_fh,
4700
np->n_fhp->nfh_len, false, td->td_ucred, td, &nfsva, &nfhp,
4701
&attrflag);
4702
named_enabled = true;
4703
if (error == 0) {
4704
free(nfhp, M_NFSFH);
4705
} else if (error == NFSERR_NOTSUPP) {
4706
named_enabled = false;
4707
NFSLOCKNODE(np);
4708
np->n_flag |= NNAMEDNOTSUPP;
4709
NFSUNLOCKNODE(np);
4710
}
4711
error = 0;
4712
} else {
4713
/*
4714
* For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4715
* just fake them.
4716
*/
4717
pc.pc_linkmax = NFS_LINK_MAX;
4718
pc.pc_namemax = NFS_MAXNAMLEN;
4719
pc.pc_notrunc = 1;
4720
pc.pc_chownrestricted = 1;
4721
pc.pc_caseinsensitive = 0;
4722
pc.pc_casepreserving = 1;
4723
error = 0;
4724
}
4725
switch (ap->a_name) {
4726
case _PC_LINK_MAX:
4727
#ifdef _LP64
4728
*ap->a_retval = pc.pc_linkmax;
4729
#else
4730
*ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4731
#endif
4732
break;
4733
case _PC_NAME_MAX:
4734
*ap->a_retval = pc.pc_namemax;
4735
break;
4736
case _PC_PIPE_BUF:
4737
if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4738
*ap->a_retval = PIPE_BUF;
4739
else
4740
error = EINVAL;
4741
break;
4742
case _PC_CHOWN_RESTRICTED:
4743
*ap->a_retval = pc.pc_chownrestricted;
4744
break;
4745
case _PC_NO_TRUNC:
4746
*ap->a_retval = pc.pc_notrunc;
4747
break;
4748
case _PC_ACL_NFS4:
4749
if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4750
NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4751
*ap->a_retval = 1;
4752
else
4753
*ap->a_retval = 0;
4754
break;
4755
case _PC_ACL_PATH_MAX:
4756
if (NFS_ISV4(vp))
4757
*ap->a_retval = ACL_MAX_ENTRIES;
4758
else
4759
*ap->a_retval = 3;
4760
break;
4761
case _PC_PRIO_IO:
4762
*ap->a_retval = 0;
4763
break;
4764
case _PC_SYNC_IO:
4765
*ap->a_retval = 0;
4766
break;
4767
case _PC_ALLOC_SIZE_MIN:
4768
*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4769
break;
4770
case _PC_FILESIZEBITS:
4771
if (NFS_ISV34(vp))
4772
*ap->a_retval = 64;
4773
else
4774
*ap->a_retval = 32;
4775
break;
4776
case _PC_REC_INCR_XFER_SIZE:
4777
*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4778
break;
4779
case _PC_REC_MAX_XFER_SIZE:
4780
*ap->a_retval = -1; /* means ``unlimited'' */
4781
break;
4782
case _PC_REC_MIN_XFER_SIZE:
4783
*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4784
break;
4785
case _PC_REC_XFER_ALIGN:
4786
*ap->a_retval = PAGE_SIZE;
4787
break;
4788
case _PC_SYMLINK_MAX:
4789
*ap->a_retval = NFS_MAXPATHLEN;
4790
break;
4791
case _PC_MIN_HOLE_SIZE:
4792
/* Only some NFSv4.2 servers support Seek for Holes. */
4793
*ap->a_retval = 0;
4794
if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4795
/*
4796
* NFSv4.2 doesn't have an attribute for hole size,
4797
* so all we can do is see if the Seek operation is
4798
* supported and then use f_iosize as a "best guess".
4799
*/
4800
mtx_lock(&nmp->nm_mtx);
4801
if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4802
mtx_unlock(&nmp->nm_mtx);
4803
off = 0;
4804
attrflag = 0;
4805
error = nfsrpc_seek(vp, &off, &eof,
4806
NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4807
&attrflag);
4808
if (attrflag != 0)
4809
(void) nfscl_loadattrcache(&vp, &nfsva,
4810
NULL, 0, 1);
4811
mtx_lock(&nmp->nm_mtx);
4812
if (error == NFSERR_NOTSUPP)
4813
nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4814
else
4815
nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4816
NFSMNTP_SEEK;
4817
error = 0;
4818
}
4819
if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4820
*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4821
mtx_unlock(&nmp->nm_mtx);
4822
}
4823
break;
4824
case _PC_NAMEDATTR_ENABLED:
4825
if (named_enabled)
4826
*ap->a_retval = 1;
4827
else
4828
*ap->a_retval = 0;
4829
break;
4830
case _PC_HAS_NAMEDATTR:
4831
if (has_namedattr)
4832
*ap->a_retval = 1;
4833
else
4834
*ap->a_retval = 0;
4835
break;
4836
case _PC_HAS_HIDDENSYSTEM:
4837
if (NFS_ISV4(vp) && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4838
NFSATTRBIT_HIDDEN) &&
4839
NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4840
NFSATTRBIT_SYSTEM))
4841
*ap->a_retval = 1;
4842
else
4843
*ap->a_retval = 0;
4844
break;
4845
case _PC_CLONE_BLKSIZE:
4846
*ap->a_retval = clone_blksize;
4847
break;
4848
4849
default:
4850
error = vop_stdpathconf(ap);
4851
break;
4852
}
4853
return (error);
4854
}
4855
4856