Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/nfsclient/nfs_clvnops.c
107265 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_ARCHIVE, 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_ARCHIVE | UF_HIDDEN | UF_SYSTEM)) != 0 ||
1090
((vap->va_flags & UF_ARCHIVE) != 0 &&
1091
!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_ARCHIVE)) ||
1092
((vap->va_flags & UF_HIDDEN) != 0 &&
1093
!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_HIDDEN)) ||
1094
((vap->va_flags & UF_SYSTEM) != 0 &&
1095
!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr, NFSATTRBIT_SYSTEM))))
1096
return (EOPNOTSUPP);
1097
1098
/*
1099
* Disallow write attempts if the filesystem is mounted read-only.
1100
*/
1101
if ((vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1102
vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1103
vap->va_mtime.tv_sec != VNOVAL ||
1104
vap->va_birthtime.tv_sec != VNOVAL ||
1105
vap->va_mode != (mode_t)VNOVAL) &&
1106
(vp->v_mount->mnt_flag & MNT_RDONLY))
1107
return (EROFS);
1108
if (vap->va_size != VNOVAL) {
1109
switch (vp->v_type) {
1110
case VDIR:
1111
return (EISDIR);
1112
case VCHR:
1113
case VBLK:
1114
case VSOCK:
1115
case VFIFO:
1116
if (vap->va_mtime.tv_sec == VNOVAL &&
1117
vap->va_atime.tv_sec == VNOVAL &&
1118
vap->va_birthtime.tv_sec == VNOVAL &&
1119
vap->va_mode == (mode_t)VNOVAL &&
1120
vap->va_uid == (uid_t)VNOVAL &&
1121
vap->va_gid == (gid_t)VNOVAL)
1122
return (0);
1123
vap->va_size = VNOVAL;
1124
break;
1125
default:
1126
/*
1127
* Disallow write attempts if the filesystem is
1128
* mounted read-only.
1129
*/
1130
if (vp->v_mount->mnt_flag & MNT_RDONLY)
1131
return (EROFS);
1132
/*
1133
* We run vnode_pager_setsize() early (why?),
1134
* we must set np->n_size now to avoid vinvalbuf
1135
* V_SAVE races that might setsize a lower
1136
* value.
1137
*/
1138
NFSLOCKNODE(np);
1139
tsize = np->n_size;
1140
NFSUNLOCKNODE(np);
1141
error = ncl_meta_setsize(vp, td, vap->va_size);
1142
NFSLOCKNODE(np);
1143
if (np->n_flag & NMODIFIED) {
1144
tsize = np->n_size;
1145
NFSUNLOCKNODE(np);
1146
error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1147
0 : V_SAVE, td, 1);
1148
if (error != 0) {
1149
vnode_pager_setsize(vp, tsize);
1150
return (error);
1151
}
1152
/*
1153
* Call nfscl_delegmodtime() to set the modify time
1154
* locally, as required.
1155
*/
1156
nfscl_delegmodtime(vp, NULL);
1157
} else
1158
NFSUNLOCKNODE(np);
1159
/*
1160
* np->n_size has already been set to vap->va_size
1161
* in ncl_meta_setsize(). We must set it again since
1162
* nfs_loadattrcache() could be called through
1163
* ncl_meta_setsize() and could modify np->n_size.
1164
*/
1165
NFSLOCKNODE(np);
1166
np->n_vattr.na_size = np->n_size = vap->va_size;
1167
NFSUNLOCKNODE(np);
1168
}
1169
} else {
1170
NFSLOCKNODE(np);
1171
if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1172
(np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1173
NFSUNLOCKNODE(np);
1174
error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1175
if (error == EINTR || error == EIO)
1176
return (error);
1177
} else
1178
NFSUNLOCKNODE(np);
1179
}
1180
error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1181
if (vap->va_size != VNOVAL) {
1182
if (error == 0) {
1183
nanouptime(&ts);
1184
NFSLOCKNODE(np);
1185
np->n_localmodtime = ts;
1186
NFSUNLOCKNODE(np);
1187
} else {
1188
NFSLOCKNODE(np);
1189
np->n_size = np->n_vattr.na_size = tsize;
1190
vnode_pager_setsize(vp, tsize);
1191
NFSUNLOCKNODE(np);
1192
}
1193
}
1194
if (vap->va_mtime.tv_sec != VNOVAL && error == 0)
1195
nfscl_delegmodtime(vp, &vap->va_mtime);
1196
return (error);
1197
}
1198
1199
/*
1200
* Do an nfs setattr rpc.
1201
*/
1202
static int
1203
nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1204
struct thread *td)
1205
{
1206
struct nfsnode *np = VTONFS(vp);
1207
int error, ret, attrflag, i;
1208
struct nfsvattr nfsva;
1209
1210
if (NFS_ISV34(vp)) {
1211
NFSLOCKNODE(np);
1212
for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1213
np->n_accesscache[i].stamp = 0;
1214
np->n_flag |= NDELEGMOD;
1215
NFSUNLOCKNODE(np);
1216
KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1217
}
1218
error = nfsrpc_setattr(vp, vap, NULL, 0, cred, td, &nfsva, &attrflag);
1219
if (attrflag) {
1220
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1221
if (ret && !error)
1222
error = ret;
1223
}
1224
if (error && NFS_ISV4(vp))
1225
error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1226
return (error);
1227
}
1228
1229
/*
1230
* Get a named attribute directory for the vnode.
1231
*/
1232
static int
1233
nfs_get_namedattrdir(struct vnode *vp, struct componentname *cnp,
1234
struct vnode **vpp)
1235
{
1236
struct nfsfh *nfhp;
1237
struct nfsnode *np;
1238
struct vnode *newvp;
1239
struct nfsvattr nfsva;
1240
int attrflag, error;
1241
1242
attrflag = 0;
1243
*vpp = NULL;
1244
np = VTONFS(vp);
1245
error = nfsrpc_openattr(VFSTONFS(vp->v_mount), vp, np->n_fhp->nfh_fh,
1246
np->n_fhp->nfh_len, (cnp->cn_flags & CREATENAMED),
1247
cnp->cn_cred, curthread, &nfsva, &nfhp, &attrflag);
1248
if (error == NFSERR_NOTSUPP)
1249
error = ENOATTR;
1250
if (error == 0)
1251
error = nfscl_nget(vp->v_mount, vp, nfhp, cnp, curthread, &np,
1252
cnp->cn_lkflags);
1253
if (error != 0)
1254
return (error);
1255
newvp = NFSTOV(np);
1256
vn_irflag_set_cond(newvp, VIRF_NAMEDDIR);
1257
if (attrflag != 0)
1258
(void)nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1259
*vpp = newvp;
1260
return (0);
1261
}
1262
1263
/*
1264
* nfs lookup call, one step at a time...
1265
* First look in cache
1266
* If not found, unlock the directory nfsnode and do the rpc
1267
*/
1268
static int
1269
nfs_lookup(struct vop_lookup_args *ap)
1270
{
1271
struct componentname *cnp = ap->a_cnp;
1272
struct vnode *dvp = ap->a_dvp;
1273
struct vnode **vpp = ap->a_vpp;
1274
struct mount *mp = dvp->v_mount;
1275
uint64_t flags = cnp->cn_flags;
1276
struct vnode *newvp;
1277
struct nfsmount *nmp;
1278
struct nfsnode *np, *newnp;
1279
int error = 0, attrflag, dattrflag, ltype, ncticks;
1280
struct thread *td = curthread;
1281
struct nfsfh *nfhp;
1282
struct nfsvattr dnfsva, nfsva;
1283
struct vattr vattr;
1284
struct timespec nctime, ts;
1285
uint32_t openmode;
1286
bool is_nameddir, needs_nameddir, opennamed;
1287
1288
dattrflag = 0;
1289
*vpp = NULL;
1290
nmp = VFSTONFS(mp);
1291
opennamed = (flags & (OPENNAMED | ISLASTCN)) == (OPENNAMED | ISLASTCN);
1292
if (opennamed && (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp)))
1293
return (ENOATTR);
1294
is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1295
if ((is_nameddir && (flags & ISLASTCN) == 0 && (cnp->cn_namelen > 1 ||
1296
*cnp->cn_nameptr != '.')) ||
1297
(opennamed && !is_nameddir && (flags & ISDOTDOT) != 0))
1298
return (ENOATTR);
1299
if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1300
(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1301
return (EROFS);
1302
np = VTONFS(dvp);
1303
1304
needs_nameddir = false;
1305
if (opennamed || is_nameddir) {
1306
cnp->cn_flags &= ~MAKEENTRY;
1307
if (!is_nameddir)
1308
needs_nameddir = true;
1309
}
1310
1311
/*
1312
* If the named attribute directory is needed, acquire it now.
1313
*/
1314
newvp = NULL;
1315
if (needs_nameddir) {
1316
KASSERT(np->n_v4 == NULL, ("nfs_lookup: O_NAMEDATTR when"
1317
" n_v4 not NULL"));
1318
error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1319
if (error != 0)
1320
goto handle_error;
1321
if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
1322
*vpp = newvp;
1323
return (0);
1324
}
1325
dvp = newvp;
1326
np = VTONFS(dvp);
1327
newvp = NULL;
1328
} else if (opennamed && cnp->cn_namelen == 1 &&
1329
*cnp->cn_nameptr == '.') {
1330
vref(dvp);
1331
*vpp = dvp;
1332
return (0);
1333
}
1334
1335
if (dvp->v_type != VDIR)
1336
return (ENOTDIR);
1337
1338
/* For NFSv4, wait until any remove is done. */
1339
NFSLOCKNODE(np);
1340
while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1341
np->n_flag |= NREMOVEWANT;
1342
(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1343
}
1344
NFSUNLOCKNODE(np);
1345
1346
error = vn_dir_check_exec(dvp, cnp);
1347
if (error != 0)
1348
return (error);
1349
1350
if (!opennamed && !is_nameddir) {
1351
error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1352
if (error > 0 && error != ENOENT)
1353
return (error);
1354
if (error == -1) {
1355
/*
1356
* Lookups of "." are special and always return the
1357
* current directory. cache_lookup() already handles
1358
* associated locking bookkeeping, etc.
1359
*/
1360
if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1361
return (0);
1362
}
1363
1364
/*
1365
* We only accept a positive hit in the cache if the
1366
* change time of the file matches our cached copy.
1367
* Otherwise, we discard the cache entry and fallback
1368
* to doing a lookup RPC. We also only trust cache
1369
* entries for less than nm_nametimeo seconds.
1370
*
1371
* To better handle stale file handles and attributes,
1372
* clear the attribute cache of this node if it is a
1373
* leaf component, part of an open() call, and not
1374
* locally modified before fetching the attributes.
1375
* This should allow stale file handles to be detected
1376
* here where we can fall back to a LOOKUP RPC to
1377
* recover rather than having nfs_open() detect the
1378
* stale file handle and failing open(2) with ESTALE.
1379
*/
1380
newvp = *vpp;
1381
newnp = VTONFS(newvp);
1382
if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1383
(flags & (ISLASTCN | ISOPEN)) ==
1384
(ISLASTCN | ISOPEN) &&
1385
!(newnp->n_flag & NMODIFIED)) {
1386
NFSLOCKNODE(newnp);
1387
newnp->n_attrstamp = 0;
1388
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1389
NFSUNLOCKNODE(newnp);
1390
}
1391
if (nfscl_nodeleg(newvp, 0) == 0 ||
1392
((u_int)(ticks - ncticks) <
1393
(nmp->nm_nametimeo * hz) &&
1394
VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1395
timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1396
NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1397
return (0);
1398
}
1399
cache_purge(newvp);
1400
if (dvp != newvp)
1401
vput(newvp);
1402
else
1403
vrele(newvp);
1404
*vpp = NULL;
1405
} else if (error == ENOENT) {
1406
if (VN_IS_DOOMED(dvp))
1407
return (ENOENT);
1408
/*
1409
* We only accept a negative hit in the cache if the
1410
* modification time of the parent directory matches
1411
* the cached copy in the name cache entry.
1412
* Otherwise, we discard all of the negative cache
1413
* entries for this directory. We also only trust
1414
* negative cache entries for up to nm_negnametimeo
1415
* seconds.
1416
*/
1417
if ((u_int)(ticks - ncticks) <
1418
(nmp->nm_negnametimeo * hz) &&
1419
VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1420
timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1421
NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1422
return (ENOENT);
1423
}
1424
cache_purge_negative(dvp);
1425
}
1426
}
1427
1428
openmode = 0;
1429
#if 0
1430
/*
1431
* The use of LookupOpen breaks some builds. It is disabled
1432
* until that is fixed.
1433
*/
1434
/*
1435
* If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1436
* option, it is possible to do the Open operation in the same
1437
* compound as Lookup, so long as delegations are not being
1438
* issued. This saves doing a separate RPC for Open.
1439
* For pnfs, do not do this, since the Open+LayoutGet will
1440
* be needed as a separate RPC.
1441
*/
1442
NFSLOCKMNT(nmp);
1443
if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && !NFSHASPNFS(nmp) &&
1444
(nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1445
(!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1446
(flags & (ISLASTCN | ISOPEN | OPENNAMED))) == (ISLASTCN | ISOPEN)) {
1447
if ((flags & OPENREAD) != 0)
1448
openmode |= NFSV4OPEN_ACCESSREAD;
1449
if ((flags & OPENWRITE) != 0)
1450
openmode |= NFSV4OPEN_ACCESSWRITE;
1451
}
1452
NFSUNLOCKMNT(nmp);
1453
#endif
1454
1455
newvp = NULL;
1456
NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1457
nanouptime(&ts);
1458
error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1459
cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1460
openmode);
1461
if (dattrflag)
1462
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1463
if (needs_nameddir) {
1464
vput(dvp);
1465
dvp = ap->a_dvp;
1466
}
1467
handle_error:
1468
if (error) {
1469
if (newvp != NULL) {
1470
vput(newvp);
1471
*vpp = NULL;
1472
}
1473
1474
if (error != ENOENT) {
1475
if (NFS_ISV4(dvp))
1476
error = nfscl_maperr(td, error,
1477
(uid_t)0, (gid_t)0);
1478
return (error);
1479
}
1480
1481
/* The requested file was not found. */
1482
if ((cnp->cn_nameiop == CREATE ||
1483
cnp->cn_nameiop == RENAME) &&
1484
(flags & ISLASTCN)) {
1485
/*
1486
* XXX: UFS does a full VOP_ACCESS(dvp,
1487
* VWRITE) here instead of just checking
1488
* MNT_RDONLY.
1489
*/
1490
if (mp->mnt_flag & MNT_RDONLY)
1491
return (EROFS);
1492
return (EJUSTRETURN);
1493
}
1494
1495
if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag &&
1496
!NFSHASCASEINSENSITIVE(nmp)) {
1497
/*
1498
* Cache the modification time of the parent
1499
* directory from the post-op attributes in
1500
* the name cache entry. The negative cache
1501
* entry will be ignored once the directory
1502
* has changed. Don't bother adding the entry
1503
* if the directory has already changed.
1504
*/
1505
NFSLOCKNODE(np);
1506
if (timespeccmp(&np->n_vattr.na_mtime,
1507
&dnfsva.na_mtime, ==)) {
1508
NFSUNLOCKNODE(np);
1509
cache_enter_time(dvp, NULL, cnp,
1510
&dnfsva.na_mtime, NULL);
1511
} else
1512
NFSUNLOCKNODE(np);
1513
}
1514
return (ENOENT);
1515
}
1516
1517
/*
1518
* Handle RENAME case...
1519
*/
1520
if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1521
if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1522
free(nfhp, M_NFSFH);
1523
return (EISDIR);
1524
}
1525
error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1526
LK_EXCLUSIVE);
1527
if (error)
1528
return (error);
1529
newvp = NFSTOV(np);
1530
/*
1531
* If n_localmodtime >= time before RPC, then
1532
* a file modification operation, such as
1533
* VOP_SETATTR() of size, has occurred while
1534
* the Lookup RPC and acquisition of the vnode
1535
* happened. As such, the attributes might
1536
* be stale, with possibly an incorrect size.
1537
*/
1538
NFSLOCKNODE(np);
1539
if (timespecisset(&np->n_localmodtime) &&
1540
timespeccmp(&np->n_localmodtime, &ts, >=)) {
1541
NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1542
"stale attributes\n");
1543
attrflag = 0;
1544
}
1545
NFSUNLOCKNODE(np);
1546
if (attrflag)
1547
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1548
0, 1);
1549
*vpp = newvp;
1550
return (0);
1551
}
1552
1553
if (flags & ISDOTDOT) {
1554
ltype = NFSVOPISLOCKED(dvp);
1555
error = vfs_busy(mp, MBF_NOWAIT);
1556
if (error != 0) {
1557
vfs_ref(mp);
1558
NFSVOPUNLOCK(dvp);
1559
error = vfs_busy(mp, 0);
1560
NFSVOPLOCK(dvp, ltype | LK_RETRY);
1561
vfs_rel(mp);
1562
if (error == 0 && VN_IS_DOOMED(dvp)) {
1563
vfs_unbusy(mp);
1564
error = ENOENT;
1565
}
1566
if (error != 0)
1567
return (error);
1568
}
1569
NFSVOPUNLOCK(dvp);
1570
error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1571
cnp->cn_lkflags);
1572
if (error == 0)
1573
newvp = NFSTOV(np);
1574
vfs_unbusy(mp);
1575
if (newvp != dvp)
1576
NFSVOPLOCK(dvp, ltype | LK_RETRY);
1577
if (VN_IS_DOOMED(dvp)) {
1578
if (error == 0) {
1579
if (newvp == dvp)
1580
vrele(newvp);
1581
else
1582
vput(newvp);
1583
}
1584
error = ENOENT;
1585
}
1586
if (error != 0)
1587
return (error);
1588
if (attrflag)
1589
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1590
0, 1);
1591
} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1592
free(nfhp, M_NFSFH);
1593
vref(dvp);
1594
newvp = dvp;
1595
if (attrflag)
1596
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1597
0, 1);
1598
} else {
1599
error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1600
cnp->cn_lkflags);
1601
if (error)
1602
return (error);
1603
newvp = NFSTOV(np);
1604
if (opennamed)
1605
vn_irflag_set_cond(newvp, VIRF_NAMEDATTR);
1606
/*
1607
* If n_localmodtime >= time before RPC, then
1608
* a file modification operation, such as
1609
* VOP_SETATTR() of size, has occurred while
1610
* the Lookup RPC and acquisition of the vnode
1611
* happened. As such, the attributes might
1612
* be stale, with possibly an incorrect size.
1613
*/
1614
NFSLOCKNODE(np);
1615
if (timespecisset(&np->n_localmodtime) &&
1616
timespeccmp(&np->n_localmodtime, &ts, >=)) {
1617
NFSCL_DEBUG(4, "nfs_lookup: localmod "
1618
"stale attributes\n");
1619
attrflag = 0;
1620
}
1621
NFSUNLOCKNODE(np);
1622
if (attrflag)
1623
(void)nfscl_loadattrcache(&newvp, &nfsva, NULL,
1624
0, 1);
1625
else if ((flags & (ISLASTCN | ISOPEN)) ==
1626
(ISLASTCN | ISOPEN) &&
1627
!(np->n_flag & NMODIFIED)) {
1628
/*
1629
* Flush the attribute cache when opening a
1630
* leaf node to ensure that fresh attributes
1631
* are fetched in nfs_open() since we did not
1632
* fetch attributes from the LOOKUP reply.
1633
*/
1634
NFSLOCKNODE(np);
1635
np->n_attrstamp = 0;
1636
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1637
NFSUNLOCKNODE(np);
1638
}
1639
}
1640
if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1641
(cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1642
attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1643
cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1644
newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1645
*vpp = newvp;
1646
return (0);
1647
}
1648
1649
/*
1650
* nfs read call.
1651
* Just call ncl_bioread() to do the work.
1652
*/
1653
static int
1654
nfs_read(struct vop_read_args *ap)
1655
{
1656
struct vnode *vp = ap->a_vp;
1657
1658
switch (vp->v_type) {
1659
case VREG:
1660
return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1661
case VDIR:
1662
return (EISDIR);
1663
default:
1664
return (EOPNOTSUPP);
1665
}
1666
}
1667
1668
/*
1669
* nfs readlink call
1670
*/
1671
static int
1672
nfs_readlink(struct vop_readlink_args *ap)
1673
{
1674
struct vnode *vp = ap->a_vp;
1675
1676
if (vp->v_type != VLNK)
1677
return (EINVAL);
1678
return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1679
}
1680
1681
/*
1682
* Do a readlink rpc.
1683
* Called by ncl_doio() from below the buffer cache.
1684
*/
1685
int
1686
ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1687
{
1688
int error, ret, attrflag;
1689
struct nfsvattr nfsva;
1690
1691
error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1692
&attrflag);
1693
if (attrflag) {
1694
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1695
if (ret && !error)
1696
error = ret;
1697
}
1698
if (error && NFS_ISV4(vp))
1699
error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1700
return (error);
1701
}
1702
1703
/*
1704
* nfs read rpc call
1705
* Ditto above
1706
*/
1707
int
1708
ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1709
{
1710
int error, ret, attrflag;
1711
struct nfsvattr nfsva;
1712
struct nfsmount *nmp;
1713
1714
nmp = VFSTONFS(vp->v_mount);
1715
error = EIO;
1716
attrflag = 0;
1717
if (NFSHASPNFS(nmp))
1718
error = nfscl_doiods(vp, uiop, NULL, NULL,
1719
NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1720
NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1721
if (error != 0 && error != EFAULT)
1722
error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1723
&attrflag);
1724
if (attrflag) {
1725
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1726
if (ret && !error)
1727
error = ret;
1728
}
1729
if (error && NFS_ISV4(vp))
1730
error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1731
return (error);
1732
}
1733
1734
/*
1735
* nfs write call
1736
*/
1737
int
1738
ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1739
int *iomode, int *must_commit, int called_from_strategy, int ioflag)
1740
{
1741
struct nfsvattr nfsva;
1742
int error, attrflag, ret;
1743
struct nfsmount *nmp;
1744
1745
nmp = VFSTONFS(vp->v_mount);
1746
error = EIO;
1747
attrflag = 0;
1748
if (NFSHASPNFS(nmp))
1749
error = nfscl_doiods(vp, uiop, iomode, must_commit,
1750
NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1751
NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1752
if (error != 0 && error != EFAULT)
1753
error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1754
uiop->uio_td, &nfsva, &attrflag, called_from_strategy,
1755
ioflag);
1756
if (attrflag) {
1757
if (VTONFS(vp)->n_flag & ND_NFSV4)
1758
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 1, 1);
1759
else
1760
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1761
if (ret && !error)
1762
error = ret;
1763
}
1764
if (DOINGASYNC(vp))
1765
*iomode = NFSWRITE_FILESYNC;
1766
if (error && NFS_ISV4(vp))
1767
error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1768
return (error);
1769
}
1770
1771
/*
1772
* nfs mknod rpc
1773
* For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1774
* mode set to specify the file type and the size field for rdev.
1775
*/
1776
static int
1777
nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1778
struct vattr *vap)
1779
{
1780
struct nfsvattr nfsva, dnfsva;
1781
struct vnode *newvp = NULL;
1782
struct nfsnode *np = NULL, *dnp;
1783
struct nfsfh *nfhp;
1784
struct vattr vattr;
1785
int error = 0, attrflag, dattrflag;
1786
u_int32_t rdev;
1787
1788
if (VATTR_ISDEV(vap))
1789
rdev = vap->va_rdev;
1790
else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1791
rdev = 0xffffffff;
1792
else
1793
return (EOPNOTSUPP);
1794
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1795
return (error);
1796
error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1797
rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1798
&nfsva, &nfhp, &attrflag, &dattrflag);
1799
if (!error) {
1800
if (!nfhp)
1801
(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1802
cnp->cn_namelen, cnp->cn_cred, curthread,
1803
&dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1804
if (nfhp)
1805
error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1806
curthread, &np, LK_EXCLUSIVE);
1807
}
1808
if (dattrflag)
1809
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1810
if (!error) {
1811
newvp = NFSTOV(np);
1812
if (attrflag != 0) {
1813
error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1814
if (error != 0)
1815
vput(newvp);
1816
}
1817
}
1818
if (!error) {
1819
*vpp = newvp;
1820
} else if (NFS_ISV4(dvp)) {
1821
error = nfscl_maperr(curthread, error, vap->va_uid,
1822
vap->va_gid);
1823
}
1824
dnp = VTONFS(dvp);
1825
NFSLOCKNODE(dnp);
1826
dnp->n_flag |= NMODIFIED;
1827
if (!dattrflag) {
1828
dnp->n_attrstamp = 0;
1829
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1830
}
1831
NFSUNLOCKNODE(dnp);
1832
return (error);
1833
}
1834
1835
/*
1836
* nfs mknod vop
1837
* just call nfs_mknodrpc() to do the work.
1838
*/
1839
/* ARGSUSED */
1840
static int
1841
nfs_mknod(struct vop_mknod_args *ap)
1842
{
1843
return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1844
}
1845
1846
static struct mtx nfs_cverf_mtx;
1847
MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1848
MTX_DEF);
1849
1850
static nfsquad_t
1851
nfs_get_cverf(void)
1852
{
1853
static nfsquad_t cverf;
1854
nfsquad_t ret;
1855
static int cverf_initialized = 0;
1856
1857
mtx_lock(&nfs_cverf_mtx);
1858
if (cverf_initialized == 0) {
1859
cverf.lval[0] = arc4random();
1860
cverf.lval[1] = arc4random();
1861
cverf_initialized = 1;
1862
} else
1863
cverf.qval++;
1864
ret = cverf;
1865
mtx_unlock(&nfs_cverf_mtx);
1866
1867
return (ret);
1868
}
1869
1870
/*
1871
* nfs file create call
1872
*/
1873
static int
1874
nfs_create(struct vop_create_args *ap)
1875
{
1876
struct vnode *dvp = ap->a_dvp;
1877
struct vattr *vap = ap->a_vap;
1878
struct componentname *cnp = ap->a_cnp;
1879
struct nfsnode *np = NULL, *dnp;
1880
struct vnode *newvp = NULL;
1881
struct nfsmount *nmp;
1882
struct nfsvattr dnfsva, nfsva;
1883
struct nfsfh *nfhp;
1884
nfsquad_t cverf;
1885
int error = 0, attrflag, dattrflag, fmode = 0;
1886
struct vattr vattr;
1887
bool is_nameddir, needs_nameddir, opennamed;
1888
1889
/*
1890
* Oops, not for me..
1891
*/
1892
if (vap->va_type == VSOCK)
1893
return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1894
1895
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1896
return (error);
1897
if (vap->va_vaflags & VA_EXCLUSIVE)
1898
fmode |= O_EXCL;
1899
dnp = VTONFS(dvp);
1900
nmp = VFSTONFS(dvp->v_mount);
1901
needs_nameddir = false;
1902
if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
1903
opennamed = (cnp->cn_flags & (OPENNAMED | ISLASTCN)) ==
1904
(OPENNAMED | ISLASTCN);
1905
is_nameddir = (vn_irflag_read(dvp) & VIRF_NAMEDDIR) != 0;
1906
if (opennamed || is_nameddir) {
1907
cnp->cn_flags &= ~MAKEENTRY;
1908
if (!is_nameddir)
1909
needs_nameddir = true;
1910
}
1911
}
1912
1913
/*
1914
* If the named attribute directory is needed, acquire it now.
1915
*/
1916
if (needs_nameddir) {
1917
KASSERT(dnp->n_v4 == NULL, ("nfs_create: O_NAMEDATTR when"
1918
" n_v4 not NULL"));
1919
error = nfs_get_namedattrdir(dvp, cnp, &newvp);
1920
if (error != 0)
1921
return (error);
1922
dvp = newvp;
1923
dnp = VTONFS(dvp);
1924
newvp = NULL;
1925
}
1926
1927
again:
1928
/* For NFSv4, wait until any remove is done. */
1929
NFSLOCKNODE(dnp);
1930
while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1931
dnp->n_flag |= NREMOVEWANT;
1932
(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1933
}
1934
NFSUNLOCKNODE(dnp);
1935
1936
cverf = nfs_get_cverf();
1937
error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1938
vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1939
&nfhp, &attrflag, &dattrflag);
1940
if (!error) {
1941
if (nfhp == NULL)
1942
(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1943
cnp->cn_namelen, cnp->cn_cred, curthread,
1944
&dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1945
if (nfhp != NULL)
1946
error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1947
curthread, &np, LK_EXCLUSIVE);
1948
}
1949
if (dattrflag)
1950
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1951
if (!error) {
1952
newvp = NFSTOV(np);
1953
if (attrflag == 0)
1954
error = nfsrpc_getattr(newvp, cnp->cn_cred, curthread,
1955
&nfsva);
1956
if (error == 0)
1957
error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1958
}
1959
if (error) {
1960
if (newvp != NULL) {
1961
vput(newvp);
1962
newvp = NULL;
1963
}
1964
if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1965
error == NFSERR_NOTSUPP) {
1966
fmode &= ~O_EXCL;
1967
goto again;
1968
}
1969
} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1970
if (nfscl_checksattr(vap, &nfsva)) {
1971
error = nfsrpc_setattr(newvp, vap, NULL, 0,
1972
cnp->cn_cred, curthread, &nfsva, &attrflag);
1973
if (error && (vap->va_uid != (uid_t)VNOVAL ||
1974
vap->va_gid != (gid_t)VNOVAL)) {
1975
/* try again without setting uid/gid */
1976
vap->va_uid = (uid_t)VNOVAL;
1977
vap->va_gid = (uid_t)VNOVAL;
1978
error = nfsrpc_setattr(newvp, vap, NULL, 0,
1979
cnp->cn_cred, curthread, &nfsva, &attrflag);
1980
}
1981
if (attrflag)
1982
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1983
0, 1);
1984
if (error != 0)
1985
vput(newvp);
1986
}
1987
}
1988
if (!error) {
1989
if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1990
if (dvp != newvp)
1991
cache_enter_time(dvp, newvp, cnp,
1992
&nfsva.na_ctime, NULL);
1993
else
1994
printf("nfs_create: bogus NFS server returned "
1995
"the directory as the new file object\n");
1996
}
1997
*ap->a_vpp = newvp;
1998
} else if (NFS_ISV4(dvp)) {
1999
error = nfscl_maperr(curthread, error, vap->va_uid,
2000
vap->va_gid);
2001
}
2002
NFSLOCKNODE(dnp);
2003
dnp->n_flag |= NMODIFIED;
2004
if (!dattrflag) {
2005
dnp->n_attrstamp = 0;
2006
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2007
}
2008
NFSUNLOCKNODE(dnp);
2009
if (needs_nameddir)
2010
vput(dvp);
2011
return (error);
2012
}
2013
2014
/*
2015
* nfs file remove call
2016
* To try and make nfs semantics closer to ufs semantics, a file that has
2017
* other processes using the vnode is renamed instead of removed and then
2018
* removed later on the last close.
2019
* - If v_usecount > 1
2020
* If a rename is not already in the works
2021
* call nfs_sillyrename() to set it up
2022
* else
2023
* do the remove rpc
2024
*/
2025
static int
2026
nfs_remove(struct vop_remove_args *ap)
2027
{
2028
struct vnode *vp = ap->a_vp;
2029
struct vnode *dvp = ap->a_dvp;
2030
struct componentname *cnp = ap->a_cnp;
2031
struct nfsnode *np = VTONFS(vp);
2032
int error = 0;
2033
struct vattr vattr;
2034
struct nfsmount *nmp;
2035
2036
KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
2037
if (vp->v_type == VDIR)
2038
error = EPERM;
2039
else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
2040
VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
2041
vattr.va_nlink > 1)) {
2042
nmp = VFSTONFS(vp->v_mount);
2043
/*
2044
* Purge the name cache so that the chance of a lookup for
2045
* the name succeeding while the remove is in progress is
2046
* minimized. Without node locking it can still happen, such
2047
* that an I/O op returns ESTALE, but since you get this if
2048
* another host removes the file..
2049
*/
2050
cache_purge(vp);
2051
/*
2052
* throw away biocache buffers, mainly to avoid
2053
* unnecessary delayed writes later.
2054
* Flushing here would be more correct for the case
2055
* where nfs_close() did not do a flush. However, it
2056
* could be a large performance hit for some servers
2057
* and only matters when the file name being removed is
2058
* one of multiple hard links.
2059
*/
2060
if (!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2061
(nmp->nm_flag & NFSMNT_NOCTO) == 0)
2062
error = ncl_vinvalbuf(vp, 0, curthread, 1);
2063
if (error != EINTR && error != EIO)
2064
/* Do the rpc */
2065
error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
2066
cnp->cn_namelen, cnp->cn_cred, curthread, false);
2067
/*
2068
* Kludge City: If the first reply to the remove rpc is lost..
2069
* the reply to the retransmitted request will be ENOENT
2070
* since the file was in fact removed
2071
* Therefore, we cheat and return success.
2072
*/
2073
if (error == ENOENT)
2074
error = 0;
2075
} else if (!np->n_sillyrename)
2076
error = nfs_sillyrename(dvp, vp, cnp);
2077
NFSLOCKNODE(np);
2078
np->n_attrstamp = 0;
2079
NFSUNLOCKNODE(np);
2080
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2081
return (error);
2082
}
2083
2084
/*
2085
* nfs file remove rpc called from nfs_inactive
2086
*/
2087
int
2088
ncl_removeit(struct sillyrename *sp, struct vnode *vp)
2089
{
2090
/*
2091
* Make sure that the directory vnode is still valid.
2092
* XXX we should lock sp->s_dvp here.
2093
*/
2094
if (sp->s_dvp->v_type == VBAD)
2095
return (0);
2096
return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
2097
sp->s_cred, NULL, true));
2098
}
2099
2100
/*
2101
* Handle the nfsremove_status reply from the RPC function.
2102
*/
2103
static void
2104
nfs_removestatus(struct vnode *vp, nfsremove_status file_status,
2105
bool silly, struct thread *td)
2106
{
2107
2108
switch (file_status) {
2109
case NLINK_ZERO:
2110
/* Get rid of any delegation. */
2111
nfscl_delegreturnvp(vp, false, td);
2112
/* FALLTHROUGH */
2113
case DELETED:
2114
/* Throw away buffer cache blocks. */
2115
(void)ncl_vinvalbuf(vp, 0, td, 1);
2116
break;
2117
case VALID:
2118
/* Nothing to do, delegation is still ok. */
2119
break;
2120
default:
2121
break;
2122
}
2123
}
2124
2125
/*
2126
* Nfs remove rpc, called from nfs_remove() and ncl_removeit().
2127
*/
2128
static int
2129
nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
2130
int namelen, struct ucred *cred, struct thread *td, bool silly)
2131
{
2132
struct nfsvattr dnfsva, nfsva;
2133
struct nfsnode *dnp = VTONFS(dvp);
2134
struct nfsmount *nmp;
2135
int attrflag, error = 0, dattrflag;
2136
nfsremove_status file_status;
2137
2138
nmp = VFSTONFS(dvp->v_mount);
2139
NFSLOCKNODE(dnp);
2140
dnp->n_flag |= NREMOVEINPROG;
2141
NFSUNLOCKNODE(dnp);
2142
error = nfsrpc_remove(dvp, name, namelen, vp, &nfsva, &attrflag,
2143
&file_status, &dnfsva, &dattrflag, cred, td);
2144
NFSLOCKNODE(dnp);
2145
if ((dnp->n_flag & NREMOVEWANT)) {
2146
dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
2147
NFSUNLOCKNODE(dnp);
2148
wakeup((caddr_t)dnp);
2149
} else {
2150
dnp->n_flag &= ~NREMOVEINPROG;
2151
NFSUNLOCKNODE(dnp);
2152
}
2153
2154
if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp)) {
2155
if (file_status != DELETED && attrflag != 0)
2156
(void)nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2157
if ((nmp->nm_flag & NFSMNT_NOCTO) != 0)
2158
nfs_removestatus(vp, file_status, silly, td);
2159
}
2160
2161
if (dattrflag != 0)
2162
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2163
NFSLOCKNODE(dnp);
2164
dnp->n_flag |= NMODIFIED;
2165
if (dattrflag == 0) {
2166
dnp->n_attrstamp = 0;
2167
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2168
}
2169
NFSUNLOCKNODE(dnp);
2170
if (error && NFS_ISV4(dvp))
2171
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2172
return (error);
2173
}
2174
2175
/*
2176
* nfs file rename call
2177
*/
2178
static int
2179
nfs_rename(struct vop_rename_args *ap)
2180
{
2181
struct vnode *fvp = ap->a_fvp;
2182
struct vnode *tvp = ap->a_tvp;
2183
struct vnode *fdvp = ap->a_fdvp;
2184
struct vnode *tdvp = ap->a_tdvp;
2185
struct componentname *tcnp = ap->a_tcnp;
2186
struct componentname *fcnp = ap->a_fcnp;
2187
struct nfsnode *fnp = VTONFS(ap->a_fvp);
2188
struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
2189
struct nfsv4node *newv4 = NULL;
2190
struct nfsmount *nmp;
2191
int error;
2192
2193
/* Check for cross-device rename */
2194
if ((fvp->v_mount != tdvp->v_mount) ||
2195
(tvp && (fvp->v_mount != tvp->v_mount))) {
2196
error = EXDEV;
2197
goto out;
2198
}
2199
nmp = VFSTONFS(fvp->v_mount);
2200
2201
if (fvp == tvp) {
2202
printf("nfs_rename: fvp == tvp (can't happen)\n");
2203
error = 0;
2204
goto out;
2205
}
2206
if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2207
goto out;
2208
2209
/*
2210
* For case insensitive file systems, there may be multiple
2211
* names cached for the one name being rename'd, so purge
2212
* all names from the cache.
2213
*/
2214
if (NFSHASCASEINSENSITIVE(nmp))
2215
cache_purge(fvp);
2216
2217
/*
2218
* We have to flush B_DELWRI data prior to renaming
2219
* the file. If we don't, the delayed-write buffers
2220
* can be flushed out later after the file has gone stale
2221
* under NFSV3. NFSV2 does not have this problem because
2222
* ( as far as I can tell ) it flushes dirty buffers more
2223
* often.
2224
*
2225
* Skip the rename operation if the fsync fails, this can happen
2226
* due to the server's volume being full, when we pushed out data
2227
* that was written back to our cache earlier. Not checking for
2228
* this condition can result in potential (silent) data loss.
2229
*/
2230
if ((nmp->nm_flag & NFSMNT_NOCTO) == 0 || !NFSHASNFSV4(nmp) ||
2231
!NFSHASNFSV4N(nmp) || nfscl_mustflush(fvp) != 0)
2232
error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2233
2234
NFSVOPUNLOCK(fvp);
2235
if (error == 0 && tvp != NULL && ((nmp->nm_flag & NFSMNT_NOCTO) == 0 ||
2236
!NFSHASNFSV4(nmp) || !NFSHASNFSV4N(nmp) ||
2237
nfscl_mustflush(tvp) != 0))
2238
error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2239
if (error != 0)
2240
goto out;
2241
2242
/*
2243
* If the tvp exists and is in use, sillyrename it before doing the
2244
* rename of the new file over it.
2245
* XXX Can't sillyrename a directory.
2246
*/
2247
if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2248
tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2249
vput(tvp);
2250
tvp = NULL;
2251
}
2252
2253
error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2254
tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, false, tcnp->cn_cred,
2255
curthread);
2256
2257
if (error == 0 && NFS_ISV4(tdvp)) {
2258
/*
2259
* For NFSv4, check to see if it is the same name and
2260
* replace the name, if it is different.
2261
*/
2262
newv4 = malloc(
2263
sizeof (struct nfsv4node) +
2264
tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2265
M_NFSV4NODE, M_WAITOK);
2266
NFSLOCKNODE(tdnp);
2267
NFSLOCKNODE(fnp);
2268
if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2269
(fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2270
NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2271
tcnp->cn_namelen) ||
2272
tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2273
NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2274
tdnp->n_fhp->nfh_len))) {
2275
free(fnp->n_v4, M_NFSV4NODE);
2276
fnp->n_v4 = newv4;
2277
newv4 = NULL;
2278
fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2279
fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2280
NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2281
tdnp->n_fhp->nfh_len);
2282
NFSBCOPY(tcnp->cn_nameptr,
2283
NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2284
}
2285
NFSUNLOCKNODE(tdnp);
2286
NFSUNLOCKNODE(fnp);
2287
if (newv4 != NULL)
2288
free(newv4, M_NFSV4NODE);
2289
}
2290
2291
if (fvp->v_type == VDIR) {
2292
if (tvp != NULL && tvp->v_type == VDIR)
2293
cache_purge(tdvp);
2294
cache_purge(fdvp);
2295
}
2296
2297
out:
2298
if (tdvp == tvp)
2299
vrele(tdvp);
2300
else
2301
vput(tdvp);
2302
if (tvp)
2303
vput(tvp);
2304
vrele(fdvp);
2305
vrele(fvp);
2306
/*
2307
* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2308
*/
2309
if (error == ENOENT)
2310
error = 0;
2311
return (error);
2312
}
2313
2314
/*
2315
* nfs file rename rpc called from nfs_remove() above
2316
*/
2317
static int
2318
nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2319
struct sillyrename *sp)
2320
{
2321
2322
return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2323
sdvp, NULL, sp->s_name, sp->s_namlen, true, scnp->cn_cred,
2324
curthread));
2325
}
2326
2327
/*
2328
* Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2329
*/
2330
static int
2331
nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2332
int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2333
int tnamelen, bool silly, struct ucred *cred, struct thread *td)
2334
{
2335
struct nfsvattr fnfsva, tnfsva, tvpnfsva;
2336
struct nfsnode *fdnp = VTONFS(fdvp);
2337
struct nfsnode *tdnp = VTONFS(tdvp);
2338
struct nfsmount *nmp;
2339
int error = 0, fattrflag, tattrflag, tvpattrflag;
2340
nfsremove_status tvp_status;
2341
2342
nmp = VFSTONFS(fdvp->v_mount);
2343
error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2344
tnameptr, tnamelen, &tvp_status, &fnfsva, &tnfsva, &fattrflag,
2345
&tattrflag, &tvpnfsva, &tvpattrflag, cred, td);
2346
NFSLOCKNODE(fdnp);
2347
fdnp->n_flag |= NMODIFIED;
2348
if (fattrflag != 0) {
2349
NFSUNLOCKNODE(fdnp);
2350
(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, 0, 1);
2351
} else {
2352
fdnp->n_attrstamp = 0;
2353
NFSUNLOCKNODE(fdnp);
2354
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2355
}
2356
NFSLOCKNODE(tdnp);
2357
tdnp->n_flag |= NMODIFIED;
2358
if (tattrflag != 0) {
2359
NFSUNLOCKNODE(tdnp);
2360
(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, 0, 1);
2361
} else {
2362
tdnp->n_attrstamp = 0;
2363
NFSUNLOCKNODE(tdnp);
2364
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2365
}
2366
2367
if (tvp != NULL) {
2368
if (NFSHASNFSV4(nmp) && NFSHASNFSV4N(nmp) &&
2369
(nmp->nm_flag & NFSMNT_NOCTO) != 0)
2370
nfs_removestatus(tvp, tvp_status, silly, td);
2371
if (!silly && tvpattrflag != 0)
2372
(void)nfscl_loadattrcache(&tvp, &tvpnfsva, NULL, 0, 1);
2373
}
2374
2375
if (error && NFS_ISV4(fdvp))
2376
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2377
return (error);
2378
}
2379
2380
/*
2381
* nfs hard link create call
2382
*/
2383
static int
2384
nfs_link(struct vop_link_args *ap)
2385
{
2386
struct vnode *vp = ap->a_vp;
2387
struct vnode *tdvp = ap->a_tdvp;
2388
struct componentname *cnp = ap->a_cnp;
2389
struct nfsnode *np, *tdnp;
2390
struct nfsvattr nfsva, dnfsva;
2391
int error = 0, attrflag, dattrflag;
2392
2393
/*
2394
* Push all writes to the server, so that the attribute cache
2395
* doesn't get "out of sync" with the server.
2396
* XXX There should be a better way!
2397
*/
2398
#ifdef notnow
2399
VOP_FSYNC(vp, MNT_WAIT, curthread);
2400
#endif
2401
2402
error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2403
cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag, &dattrflag);
2404
tdnp = VTONFS(tdvp);
2405
NFSLOCKNODE(tdnp);
2406
tdnp->n_flag |= NMODIFIED;
2407
if (dattrflag != 0) {
2408
NFSUNLOCKNODE(tdnp);
2409
(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, 0, 1);
2410
} else {
2411
tdnp->n_attrstamp = 0;
2412
NFSUNLOCKNODE(tdnp);
2413
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2414
}
2415
if (attrflag)
2416
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2417
else {
2418
np = VTONFS(vp);
2419
NFSLOCKNODE(np);
2420
np->n_attrstamp = 0;
2421
NFSUNLOCKNODE(np);
2422
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2423
}
2424
/*
2425
* If negative lookup caching is enabled, I might as well
2426
* add an entry for this node. Not necessary for correctness,
2427
* but if negative caching is enabled, then the system
2428
* must care about lookup caching hit rate, so...
2429
*/
2430
if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2431
(cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2432
if (tdvp != vp)
2433
cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2434
else
2435
printf("nfs_link: bogus NFS server returned "
2436
"the directory as the new link\n");
2437
}
2438
if (error && NFS_ISV4(vp))
2439
error = nfscl_maperr(curthread, error, (uid_t)0,
2440
(gid_t)0);
2441
return (error);
2442
}
2443
2444
/*
2445
* nfs symbolic link create call
2446
*/
2447
static int
2448
nfs_symlink(struct vop_symlink_args *ap)
2449
{
2450
struct vnode *dvp = ap->a_dvp;
2451
struct vattr *vap = ap->a_vap;
2452
struct componentname *cnp = ap->a_cnp;
2453
struct nfsvattr nfsva, dnfsva;
2454
struct nfsfh *nfhp;
2455
struct nfsnode *np = NULL, *dnp;
2456
struct vnode *newvp = NULL;
2457
int error = 0, attrflag, dattrflag, ret;
2458
2459
vap->va_type = VLNK;
2460
error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2461
ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2462
&nfsva, &nfhp, &attrflag, &dattrflag);
2463
if (nfhp) {
2464
ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2465
&np, LK_EXCLUSIVE);
2466
if (!ret)
2467
newvp = NFSTOV(np);
2468
else if (!error)
2469
error = ret;
2470
}
2471
if (newvp != NULL) {
2472
if (attrflag)
2473
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2474
} else if (!error) {
2475
/*
2476
* If we do not have an error and we could not extract the
2477
* newvp from the response due to the request being NFSv2, we
2478
* have to do a lookup in order to obtain a newvp to return.
2479
*/
2480
error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2481
cnp->cn_cred, curthread, &np);
2482
if (!error)
2483
newvp = NFSTOV(np);
2484
}
2485
if (error) {
2486
if (newvp)
2487
vput(newvp);
2488
if (NFS_ISV4(dvp))
2489
error = nfscl_maperr(curthread, error,
2490
vap->va_uid, vap->va_gid);
2491
} else {
2492
*ap->a_vpp = newvp;
2493
}
2494
2495
dnp = VTONFS(dvp);
2496
NFSLOCKNODE(dnp);
2497
dnp->n_flag |= NMODIFIED;
2498
if (dattrflag != 0) {
2499
NFSUNLOCKNODE(dnp);
2500
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2501
} else {
2502
dnp->n_attrstamp = 0;
2503
NFSUNLOCKNODE(dnp);
2504
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2505
}
2506
/*
2507
* If negative lookup caching is enabled, I might as well
2508
* add an entry for this node. Not necessary for correctness,
2509
* but if negative caching is enabled, then the system
2510
* must care about lookup caching hit rate, so...
2511
*/
2512
if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2513
(cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2514
if (dvp != newvp)
2515
cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2516
NULL);
2517
else
2518
printf("nfs_symlink: bogus NFS server returned "
2519
"the directory as the new file object\n");
2520
}
2521
return (error);
2522
}
2523
2524
/*
2525
* nfs make dir call
2526
*/
2527
static int
2528
nfs_mkdir(struct vop_mkdir_args *ap)
2529
{
2530
struct vnode *dvp = ap->a_dvp;
2531
struct vattr *vap = ap->a_vap;
2532
struct componentname *cnp = ap->a_cnp;
2533
struct nfsnode *np = NULL, *dnp;
2534
struct vnode *newvp = NULL;
2535
struct vattr vattr;
2536
struct nfsfh *nfhp;
2537
struct nfsvattr nfsva, dnfsva;
2538
int error = 0, attrflag, dattrflag, ret;
2539
2540
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2541
return (error);
2542
vap->va_type = VDIR;
2543
error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2544
vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2545
&attrflag, &dattrflag);
2546
dnp = VTONFS(dvp);
2547
NFSLOCKNODE(dnp);
2548
dnp->n_flag |= NMODIFIED;
2549
if (dattrflag != 0) {
2550
NFSUNLOCKNODE(dnp);
2551
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2552
} else {
2553
dnp->n_attrstamp = 0;
2554
NFSUNLOCKNODE(dnp);
2555
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2556
}
2557
if (nfhp) {
2558
ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2559
&np, LK_EXCLUSIVE);
2560
if (!ret) {
2561
newvp = NFSTOV(np);
2562
if (attrflag)
2563
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2564
0, 1);
2565
} else if (!error)
2566
error = ret;
2567
}
2568
if (!error && newvp == NULL) {
2569
error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2570
cnp->cn_cred, curthread, &np);
2571
if (!error) {
2572
newvp = NFSTOV(np);
2573
if (newvp->v_type != VDIR)
2574
error = EEXIST;
2575
}
2576
}
2577
if (error) {
2578
if (newvp)
2579
vput(newvp);
2580
if (NFS_ISV4(dvp))
2581
error = nfscl_maperr(curthread, error,
2582
vap->va_uid, vap->va_gid);
2583
} else {
2584
/*
2585
* If negative lookup caching is enabled, I might as well
2586
* add an entry for this node. Not necessary for correctness,
2587
* but if negative caching is enabled, then the system
2588
* must care about lookup caching hit rate, so...
2589
*/
2590
if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2591
(cnp->cn_flags & MAKEENTRY) &&
2592
attrflag != 0 && dattrflag != 0) {
2593
if (dvp != newvp)
2594
cache_enter_time(dvp, newvp, cnp,
2595
&nfsva.na_ctime, &dnfsva.na_ctime);
2596
else
2597
printf("nfs_mkdir: bogus NFS server returned "
2598
"the directory that the directory was "
2599
"created in as the new file object\n");
2600
}
2601
*ap->a_vpp = newvp;
2602
}
2603
return (error);
2604
}
2605
2606
/*
2607
* nfs remove directory call
2608
*/
2609
static int
2610
nfs_rmdir(struct vop_rmdir_args *ap)
2611
{
2612
struct vnode *vp = ap->a_vp;
2613
struct vnode *dvp = ap->a_dvp;
2614
struct componentname *cnp = ap->a_cnp;
2615
struct nfsnode *dnp;
2616
struct nfsvattr dnfsva;
2617
int error, dattrflag;
2618
2619
if (dvp == vp)
2620
return (EINVAL);
2621
error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2622
cnp->cn_cred, curthread, &dnfsva, &dattrflag);
2623
dnp = VTONFS(dvp);
2624
NFSLOCKNODE(dnp);
2625
dnp->n_flag |= NMODIFIED;
2626
if (dattrflag != 0) {
2627
NFSUNLOCKNODE(dnp);
2628
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2629
} else {
2630
dnp->n_attrstamp = 0;
2631
NFSUNLOCKNODE(dnp);
2632
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2633
}
2634
2635
cache_purge(dvp);
2636
cache_purge(vp);
2637
if (error && NFS_ISV4(dvp))
2638
error = nfscl_maperr(curthread, error, (uid_t)0,
2639
(gid_t)0);
2640
/*
2641
* Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2642
*/
2643
if (error == ENOENT)
2644
error = 0;
2645
return (error);
2646
}
2647
2648
/*
2649
* nfs readdir call
2650
*/
2651
static int
2652
nfs_readdir(struct vop_readdir_args *ap)
2653
{
2654
struct vnode *vp = ap->a_vp;
2655
struct nfsnode *np = VTONFS(vp);
2656
struct uio *uio = ap->a_uio;
2657
ssize_t tresid, left;
2658
int error = 0;
2659
struct vattr vattr;
2660
2661
if (ap->a_eofflag != NULL)
2662
*ap->a_eofflag = 0;
2663
if (vp->v_type != VDIR)
2664
return(EPERM);
2665
2666
/*
2667
* First, check for hit on the EOF offset cache
2668
*/
2669
NFSLOCKNODE(np);
2670
if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2671
(np->n_flag & NMODIFIED) == 0) {
2672
NFSUNLOCKNODE(np);
2673
if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2674
NFSLOCKNODE(np);
2675
if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2676
!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2677
NFSUNLOCKNODE(np);
2678
NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2679
if (ap->a_eofflag != NULL)
2680
*ap->a_eofflag = 1;
2681
return (0);
2682
} else
2683
NFSUNLOCKNODE(np);
2684
}
2685
} else
2686
NFSUNLOCKNODE(np);
2687
2688
/*
2689
* NFS always guarantees that directory entries don't straddle
2690
* DIRBLKSIZ boundaries. As such, we need to limit the size
2691
* to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2692
* directory entry.
2693
*/
2694
left = uio->uio_resid % DIRBLKSIZ;
2695
if (left == uio->uio_resid)
2696
return (EINVAL);
2697
uio->uio_resid -= left;
2698
2699
/*
2700
* For readdirplus, if starting to read the directory,
2701
* purge the name cache, since it will be reloaded by
2702
* this directory read.
2703
* This removes potentially stale name cache entries.
2704
*/
2705
if (uio->uio_offset == 0 &&
2706
(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_RDIRPLUS) != 0)
2707
cache_purge(vp);
2708
2709
/*
2710
* Call ncl_bioread() to do the real work.
2711
*/
2712
tresid = uio->uio_resid;
2713
error = ncl_bioread(vp, uio, 0, ap->a_cred);
2714
2715
if (!error && uio->uio_resid == tresid) {
2716
NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2717
if (ap->a_eofflag != NULL)
2718
*ap->a_eofflag = 1;
2719
}
2720
2721
/* Add the partial DIRBLKSIZ (left) back in. */
2722
uio->uio_resid += left;
2723
return (error);
2724
}
2725
2726
/*
2727
* Readdir rpc call.
2728
* Called from below the buffer cache by ncl_doio().
2729
*/
2730
int
2731
ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2732
struct thread *td)
2733
{
2734
struct nfsvattr nfsva;
2735
nfsuint64 *cookiep, cookie;
2736
struct nfsnode *dnp = VTONFS(vp);
2737
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2738
int error = 0, eof, attrflag;
2739
2740
KASSERT(uiop->uio_iovcnt == 1 &&
2741
(uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2742
(uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2743
("nfs readdirrpc bad uio"));
2744
2745
/*
2746
* If there is no cookie, assume directory was stale.
2747
*/
2748
ncl_dircookie_lock(dnp);
2749
NFSUNLOCKNODE(dnp);
2750
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2751
if (cookiep) {
2752
cookie = *cookiep;
2753
ncl_dircookie_unlock(dnp);
2754
} else {
2755
ncl_dircookie_unlock(dnp);
2756
return (NFSERR_BAD_COOKIE);
2757
}
2758
2759
if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2760
(void)ncl_fsinfo(nmp, vp, cred, td);
2761
2762
error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2763
&attrflag, &eof);
2764
if (attrflag)
2765
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2766
2767
if (!error) {
2768
/*
2769
* We are now either at the end of the directory or have filled
2770
* the block.
2771
*/
2772
if (eof) {
2773
NFSLOCKNODE(dnp);
2774
dnp->n_direofoffset = uiop->uio_offset;
2775
NFSUNLOCKNODE(dnp);
2776
} else {
2777
if (uiop->uio_resid > 0)
2778
printf("EEK! readdirrpc resid > 0\n");
2779
ncl_dircookie_lock(dnp);
2780
NFSUNLOCKNODE(dnp);
2781
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2782
*cookiep = cookie;
2783
ncl_dircookie_unlock(dnp);
2784
}
2785
} else if (NFS_ISV4(vp)) {
2786
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2787
}
2788
return (error);
2789
}
2790
2791
/*
2792
* NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2793
*/
2794
int
2795
ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2796
struct thread *td)
2797
{
2798
struct nfsvattr nfsva;
2799
nfsuint64 *cookiep, cookie;
2800
struct nfsnode *dnp = VTONFS(vp);
2801
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2802
int error = 0, attrflag, eof;
2803
2804
KASSERT(uiop->uio_iovcnt == 1 &&
2805
(uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2806
(uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2807
("nfs readdirplusrpc bad uio"));
2808
2809
/*
2810
* If there is no cookie, assume directory was stale.
2811
*/
2812
ncl_dircookie_lock(dnp);
2813
NFSUNLOCKNODE(dnp);
2814
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2815
if (cookiep) {
2816
cookie = *cookiep;
2817
ncl_dircookie_unlock(dnp);
2818
} else {
2819
ncl_dircookie_unlock(dnp);
2820
return (NFSERR_BAD_COOKIE);
2821
}
2822
2823
if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2824
(void)ncl_fsinfo(nmp, vp, cred, td);
2825
error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2826
&attrflag, &eof);
2827
if (attrflag)
2828
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2829
2830
if (!error) {
2831
/*
2832
* We are now either at end of the directory or have filled the
2833
* the block.
2834
*/
2835
if (eof) {
2836
NFSLOCKNODE(dnp);
2837
dnp->n_direofoffset = uiop->uio_offset;
2838
NFSUNLOCKNODE(dnp);
2839
} else {
2840
if (uiop->uio_resid > 0)
2841
printf("EEK! readdirplusrpc resid > 0\n");
2842
ncl_dircookie_lock(dnp);
2843
NFSUNLOCKNODE(dnp);
2844
cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2845
*cookiep = cookie;
2846
ncl_dircookie_unlock(dnp);
2847
}
2848
} else if (NFS_ISV4(vp)) {
2849
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2850
}
2851
return (error);
2852
}
2853
2854
/*
2855
* Silly rename. To make the NFS filesystem that is stateless look a little
2856
* more like the "ufs" a remove of an active vnode is translated to a rename
2857
* to a funny looking filename that is removed by nfs_inactive on the
2858
* nfsnode. There is the potential for another process on a different client
2859
* to create the same funny name between the nfs_lookitup() fails and the
2860
* nfs_rename() completes, but...
2861
*/
2862
static int
2863
nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2864
{
2865
struct sillyrename *sp;
2866
struct nfsnode *np;
2867
int error;
2868
short pid;
2869
unsigned int lticks;
2870
2871
cache_purge(dvp);
2872
np = VTONFS(vp);
2873
KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2874
sp = malloc(sizeof (struct sillyrename),
2875
M_NEWNFSREQ, M_WAITOK);
2876
sp->s_cred = crhold(cnp->cn_cred);
2877
sp->s_dvp = dvp;
2878
vref(dvp);
2879
2880
/*
2881
* Fudge together a funny name.
2882
* Changing the format of the funny name to accommodate more
2883
* sillynames per directory.
2884
* The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2885
* CPU ticks since boot.
2886
*/
2887
pid = curthread->td_proc->p_pid;
2888
lticks = (unsigned int)ticks;
2889
for ( ; ; ) {
2890
sp->s_namlen = sprintf(sp->s_name,
2891
".nfs.%08x.%04x4.4", lticks,
2892
pid);
2893
if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2894
curthread, NULL))
2895
break;
2896
lticks++;
2897
}
2898
error = nfs_renameit(dvp, vp, cnp, sp);
2899
if (error)
2900
goto bad;
2901
error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2902
curthread, &np);
2903
np->n_sillyrename = sp;
2904
return (0);
2905
bad:
2906
vrele(sp->s_dvp);
2907
crfree(sp->s_cred);
2908
free(sp, M_NEWNFSREQ);
2909
return (error);
2910
}
2911
2912
/*
2913
* Look up a file name and optionally either update the file handle or
2914
* allocate an nfsnode, depending on the value of npp.
2915
* npp == NULL --> just do the lookup
2916
* *npp == NULL --> allocate a new nfsnode and make sure attributes are
2917
* handled too
2918
* *npp != NULL --> update the file handle in the vnode
2919
*/
2920
static int
2921
nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2922
struct thread *td, struct nfsnode **npp)
2923
{
2924
struct vnode *newvp = NULL, *vp;
2925
struct nfsnode *np, *dnp = VTONFS(dvp);
2926
struct nfsfh *nfhp, *onfhp;
2927
struct nfsvattr nfsva, dnfsva;
2928
struct componentname cn;
2929
int error = 0, attrflag, dattrflag;
2930
u_int hash;
2931
struct timespec ts;
2932
2933
nanouptime(&ts);
2934
error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2935
&nfhp, &attrflag, &dattrflag, 0);
2936
if (dattrflag)
2937
(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2938
if (npp && !error) {
2939
if (*npp != NULL) {
2940
np = *npp;
2941
vp = NFSTOV(np);
2942
/*
2943
* For NFSv4, check to see if it is the same name and
2944
* replace the name, if it is different.
2945
*/
2946
if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2947
(np->n_v4->n4_namelen != len ||
2948
NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2949
dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2950
NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2951
dnp->n_fhp->nfh_len))) {
2952
free(np->n_v4, M_NFSV4NODE);
2953
np->n_v4 = malloc(
2954
sizeof (struct nfsv4node) +
2955
dnp->n_fhp->nfh_len + len - 1,
2956
M_NFSV4NODE, M_WAITOK);
2957
np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2958
np->n_v4->n4_namelen = len;
2959
NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2960
dnp->n_fhp->nfh_len);
2961
NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2962
}
2963
hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2964
FNV1_32_INIT);
2965
onfhp = np->n_fhp;
2966
/*
2967
* Rehash node for new file handle.
2968
*/
2969
vfs_hash_rehash(vp, hash);
2970
np->n_fhp = nfhp;
2971
if (onfhp != NULL)
2972
free(onfhp, M_NFSFH);
2973
newvp = NFSTOV(np);
2974
} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2975
free(nfhp, M_NFSFH);
2976
vref(dvp);
2977
newvp = dvp;
2978
} else {
2979
cn.cn_nameptr = name;
2980
cn.cn_namelen = len;
2981
error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2982
&np, LK_EXCLUSIVE);
2983
if (error)
2984
return (error);
2985
newvp = NFSTOV(np);
2986
/*
2987
* If n_localmodtime >= time before RPC, then
2988
* a file modification operation, such as
2989
* VOP_SETATTR() of size, has occurred while
2990
* the Lookup RPC and acquisition of the vnode
2991
* happened. As such, the attributes might
2992
* be stale, with possibly an incorrect size.
2993
*/
2994
NFSLOCKNODE(np);
2995
if (timespecisset(&np->n_localmodtime) &&
2996
timespeccmp(&np->n_localmodtime, &ts, >=)) {
2997
NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2998
"stale attributes\n");
2999
attrflag = 0;
3000
}
3001
NFSUNLOCKNODE(np);
3002
}
3003
if (!attrflag && *npp == NULL) {
3004
if (newvp == dvp)
3005
vrele(newvp);
3006
else
3007
vput(newvp);
3008
return (ENOENT);
3009
}
3010
if (attrflag)
3011
(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
3012
}
3013
if (npp && *npp == NULL) {
3014
if (error) {
3015
if (newvp) {
3016
if (newvp == dvp)
3017
vrele(newvp);
3018
else
3019
vput(newvp);
3020
}
3021
} else
3022
*npp = np;
3023
}
3024
if (error && NFS_ISV4(dvp))
3025
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3026
return (error);
3027
}
3028
3029
/*
3030
* Nfs Version 3 and 4 commit rpc
3031
*/
3032
int
3033
ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
3034
struct thread *td)
3035
{
3036
struct nfsvattr nfsva;
3037
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3038
struct nfsnode *np;
3039
struct uio uio;
3040
int error, attrflag;
3041
3042
np = VTONFS(vp);
3043
error = EIO;
3044
attrflag = 0;
3045
if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
3046
uio.uio_offset = offset;
3047
uio.uio_resid = cnt;
3048
error = nfscl_doiods(vp, &uio, NULL, NULL,
3049
NFSV4OPEN_ACCESSWRITE, 1, cred, td);
3050
if (error != 0) {
3051
NFSLOCKNODE(np);
3052
np->n_flag &= ~NDSCOMMIT;
3053
NFSUNLOCKNODE(np);
3054
}
3055
}
3056
if (error != 0) {
3057
mtx_lock(&nmp->nm_mtx);
3058
if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
3059
mtx_unlock(&nmp->nm_mtx);
3060
return (0);
3061
}
3062
mtx_unlock(&nmp->nm_mtx);
3063
error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
3064
&attrflag);
3065
}
3066
if (attrflag != 0)
3067
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3068
if (error != 0 && NFS_ISV4(vp))
3069
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3070
return (error);
3071
}
3072
3073
/*
3074
* Strategy routine.
3075
* For async requests when nfsiod(s) are running, queue the request by
3076
* calling ncl_asyncio(), otherwise just all ncl_doio() to do the
3077
* request.
3078
*/
3079
static int
3080
nfs_strategy(struct vop_strategy_args *ap)
3081
{
3082
struct buf *bp;
3083
struct vnode *vp;
3084
struct ucred *cr;
3085
3086
bp = ap->a_bp;
3087
vp = ap->a_vp;
3088
KASSERT(bp->b_vp == vp, ("missing b_getvp"));
3089
KASSERT(!(bp->b_flags & B_DONE),
3090
("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
3091
3092
if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
3093
bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
3094
DEV_BSIZE);
3095
if (bp->b_iocmd == BIO_READ)
3096
cr = bp->b_rcred;
3097
else
3098
cr = bp->b_wcred;
3099
3100
/*
3101
* If the op is asynchronous and an i/o daemon is waiting
3102
* queue the request, wake it up and wait for completion
3103
* otherwise just do it ourselves.
3104
*/
3105
if ((bp->b_flags & B_ASYNC) == 0 ||
3106
ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
3107
(void) ncl_doio(vp, bp, cr, curthread, 1);
3108
return (0);
3109
}
3110
3111
/*
3112
* fsync vnode op. Just call ncl_flush() with commit == 1.
3113
*/
3114
/* ARGSUSED */
3115
static int
3116
nfs_fsync(struct vop_fsync_args *ap)
3117
{
3118
3119
if (ap->a_vp->v_type != VREG) {
3120
/*
3121
* For NFS, metadata is changed synchronously on the server,
3122
* so there is nothing to flush. Also, ncl_flush() clears
3123
* the NMODIFIED flag and that shouldn't be done here for
3124
* directories.
3125
*/
3126
return (0);
3127
}
3128
return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
3129
}
3130
3131
/*
3132
* Flush all the blocks associated with a vnode.
3133
* Walk through the buffer pool and push any dirty pages
3134
* associated with the vnode.
3135
* If the called_from_renewthread argument is TRUE, it has been called
3136
* from the NFSv4 renew thread and, as such, cannot block indefinitely
3137
* waiting for a buffer write to complete.
3138
*/
3139
int
3140
ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
3141
int commit, int called_from_renewthread)
3142
{
3143
struct nfsnode *np = VTONFS(vp);
3144
struct buf *bp;
3145
int i;
3146
struct buf *nbp;
3147
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3148
int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
3149
int passone = 1, trycnt = 0;
3150
u_quad_t off, endoff, toff;
3151
struct ucred* wcred = NULL;
3152
struct buf **bvec = NULL;
3153
struct bufobj *bo;
3154
#ifndef NFS_COMMITBVECSIZ
3155
#define NFS_COMMITBVECSIZ 20
3156
#endif
3157
struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
3158
u_int bvecsize = 0, bveccount;
3159
struct timespec ts;
3160
3161
if (called_from_renewthread != 0)
3162
slptimeo = hz;
3163
if (nmp->nm_flag & NFSMNT_INT)
3164
slpflag = PCATCH;
3165
if (!commit)
3166
passone = 0;
3167
bo = &vp->v_bufobj;
3168
/*
3169
* A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
3170
* server, but has not been committed to stable storage on the server
3171
* yet. On the first pass, the byte range is worked out and the commit
3172
* rpc is done. On the second pass, bwrite() is called to do the
3173
* job.
3174
*/
3175
again:
3176
off = (u_quad_t)-1;
3177
endoff = 0;
3178
bvecpos = 0;
3179
if (NFS_ISV34(vp) && commit) {
3180
if (bvec != NULL && bvec != bvec_on_stack)
3181
free(bvec, M_TEMP);
3182
/*
3183
* Count up how many buffers waiting for a commit.
3184
*/
3185
bveccount = 0;
3186
BO_LOCK(bo);
3187
TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3188
if (!BUF_ISLOCKED(bp) &&
3189
(bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
3190
== (B_DELWRI | B_NEEDCOMMIT))
3191
bveccount++;
3192
}
3193
/*
3194
* Allocate space to remember the list of bufs to commit. It is
3195
* important to use M_NOWAIT here to avoid a race with nfs_write.
3196
* If we can't get memory (for whatever reason), we will end up
3197
* committing the buffers one-by-one in the loop below.
3198
*/
3199
if (bveccount > NFS_COMMITBVECSIZ) {
3200
/*
3201
* Release the vnode interlock to avoid a lock
3202
* order reversal.
3203
*/
3204
BO_UNLOCK(bo);
3205
bvec = (struct buf **)
3206
malloc(bveccount * sizeof(struct buf *),
3207
M_TEMP, M_NOWAIT);
3208
BO_LOCK(bo);
3209
if (bvec == NULL) {
3210
bvec = bvec_on_stack;
3211
bvecsize = NFS_COMMITBVECSIZ;
3212
} else
3213
bvecsize = bveccount;
3214
} else {
3215
bvec = bvec_on_stack;
3216
bvecsize = NFS_COMMITBVECSIZ;
3217
}
3218
TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3219
if (bvecpos >= bvecsize)
3220
break;
3221
if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3222
nbp = TAILQ_NEXT(bp, b_bobufs);
3223
continue;
3224
}
3225
if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3226
(B_DELWRI | B_NEEDCOMMIT)) {
3227
BUF_UNLOCK(bp);
3228
nbp = TAILQ_NEXT(bp, b_bobufs);
3229
continue;
3230
}
3231
BO_UNLOCK(bo);
3232
bremfree(bp);
3233
/*
3234
* Work out if all buffers are using the same cred
3235
* so we can deal with them all with one commit.
3236
*
3237
* NOTE: we are not clearing B_DONE here, so we have
3238
* to do it later on in this routine if we intend to
3239
* initiate I/O on the bp.
3240
*
3241
* Note: to avoid loopback deadlocks, we do not
3242
* assign b_runningbufspace.
3243
*/
3244
if (wcred == NULL)
3245
wcred = bp->b_wcred;
3246
else if (wcred != bp->b_wcred)
3247
wcred = NOCRED;
3248
vfs_busy_pages(bp, 0);
3249
3250
BO_LOCK(bo);
3251
/*
3252
* bp is protected by being locked, but nbp is not
3253
* and vfs_busy_pages() may sleep. We have to
3254
* recalculate nbp.
3255
*/
3256
nbp = TAILQ_NEXT(bp, b_bobufs);
3257
3258
/*
3259
* A list of these buffers is kept so that the
3260
* second loop knows which buffers have actually
3261
* been committed. This is necessary, since there
3262
* may be a race between the commit rpc and new
3263
* uncommitted writes on the file.
3264
*/
3265
bvec[bvecpos++] = bp;
3266
toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3267
bp->b_dirtyoff;
3268
if (toff < off)
3269
off = toff;
3270
toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3271
if (toff > endoff)
3272
endoff = toff;
3273
}
3274
BO_UNLOCK(bo);
3275
}
3276
if (bvecpos > 0) {
3277
/*
3278
* Commit data on the server, as required.
3279
* If all bufs are using the same wcred, then use that with
3280
* one call for all of them, otherwise commit each one
3281
* separately.
3282
*/
3283
if (wcred != NOCRED)
3284
retv = ncl_commit(vp, off, (int)(endoff - off),
3285
wcred, td);
3286
else {
3287
retv = 0;
3288
for (i = 0; i < bvecpos; i++) {
3289
off_t off, size;
3290
bp = bvec[i];
3291
off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3292
bp->b_dirtyoff;
3293
size = (u_quad_t)(bp->b_dirtyend
3294
- bp->b_dirtyoff);
3295
retv = ncl_commit(vp, off, (int)size,
3296
bp->b_wcred, td);
3297
if (retv) break;
3298
}
3299
}
3300
3301
if (retv == NFSERR_STALEWRITEVERF)
3302
ncl_clearcommit(vp->v_mount);
3303
3304
/*
3305
* Now, either mark the blocks I/O done or mark the
3306
* blocks dirty, depending on whether the commit
3307
* succeeded.
3308
*/
3309
for (i = 0; i < bvecpos; i++) {
3310
bp = bvec[i];
3311
bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3312
if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3313
/*
3314
* Error, leave B_DELWRI intact
3315
*/
3316
vfs_unbusy_pages(bp);
3317
brelse(bp);
3318
} else {
3319
/*
3320
* Success, remove B_DELWRI ( bundirty() ).
3321
*
3322
* b_dirtyoff/b_dirtyend seem to be NFS
3323
* specific. We should probably move that
3324
* into bundirty(). XXX
3325
*/
3326
bufobj_wref(bo);
3327
bp->b_flags |= B_ASYNC;
3328
bundirty(bp);
3329
bp->b_flags &= ~B_DONE;
3330
bp->b_ioflags &= ~BIO_ERROR;
3331
bp->b_dirtyoff = bp->b_dirtyend = 0;
3332
bufdone(bp);
3333
}
3334
}
3335
}
3336
3337
/*
3338
* Start/do any write(s) that are required.
3339
*/
3340
loop:
3341
BO_LOCK(bo);
3342
TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3343
if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3344
if (waitfor != MNT_WAIT || passone)
3345
continue;
3346
3347
error = BUF_TIMELOCK(bp,
3348
LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3349
BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3350
if (error == 0) {
3351
BUF_UNLOCK(bp);
3352
goto loop;
3353
}
3354
if (error == ENOLCK) {
3355
error = 0;
3356
goto loop;
3357
}
3358
if (called_from_renewthread != 0) {
3359
/*
3360
* Return EIO so the flush will be retried
3361
* later.
3362
*/
3363
error = EIO;
3364
goto done;
3365
}
3366
if (newnfs_sigintr(nmp, td)) {
3367
error = EINTR;
3368
goto done;
3369
}
3370
if (slpflag == PCATCH) {
3371
slpflag = 0;
3372
slptimeo = 2 * hz;
3373
}
3374
goto loop;
3375
}
3376
if ((bp->b_flags & B_DELWRI) == 0)
3377
panic("nfs_fsync: not dirty");
3378
if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3379
BUF_UNLOCK(bp);
3380
continue;
3381
}
3382
BO_UNLOCK(bo);
3383
bremfree(bp);
3384
bp->b_flags |= B_ASYNC;
3385
bwrite(bp);
3386
if (newnfs_sigintr(nmp, td)) {
3387
error = EINTR;
3388
goto done;
3389
}
3390
goto loop;
3391
}
3392
if (passone) {
3393
passone = 0;
3394
BO_UNLOCK(bo);
3395
goto again;
3396
}
3397
if (waitfor == MNT_WAIT) {
3398
while (bo->bo_numoutput) {
3399
error = bufobj_wwait(bo, slpflag, slptimeo);
3400
if (error) {
3401
BO_UNLOCK(bo);
3402
if (called_from_renewthread != 0) {
3403
/*
3404
* Return EIO so that the flush will be
3405
* retried later.
3406
*/
3407
error = EIO;
3408
goto done;
3409
}
3410
error = newnfs_sigintr(nmp, td);
3411
if (error)
3412
goto done;
3413
if (slpflag == PCATCH) {
3414
slpflag = 0;
3415
slptimeo = 2 * hz;
3416
}
3417
BO_LOCK(bo);
3418
}
3419
}
3420
if (bo->bo_dirty.bv_cnt != 0 && commit) {
3421
BO_UNLOCK(bo);
3422
goto loop;
3423
}
3424
/*
3425
* Wait for all the async IO requests to drain
3426
*/
3427
BO_UNLOCK(bo);
3428
} else
3429
BO_UNLOCK(bo);
3430
if (NFSHASPNFS(nmp)) {
3431
nfscl_layoutcommit(vp, td);
3432
/*
3433
* Invalidate the attribute cache, since writes to a DS
3434
* won't update the size attribute.
3435
*/
3436
NFSLOCKNODE(np);
3437
np->n_attrstamp = 0;
3438
} else
3439
NFSLOCKNODE(np);
3440
if (np->n_flag & NWRITEERR) {
3441
error = np->n_error;
3442
np->n_flag &= ~NWRITEERR;
3443
}
3444
if (commit && bo->bo_dirty.bv_cnt == 0 &&
3445
bo->bo_numoutput == 0)
3446
np->n_flag &= ~NMODIFIED;
3447
NFSUNLOCKNODE(np);
3448
done:
3449
if (bvec != NULL && bvec != bvec_on_stack)
3450
free(bvec, M_TEMP);
3451
if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3452
(bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0)) {
3453
if (trycnt++ < 5) {
3454
/* try, try again... */
3455
passone = 1;
3456
wcred = NULL;
3457
bvec = NULL;
3458
bvecsize = 0;
3459
goto again;
3460
}
3461
vn_printf(vp, "ncl_flush failed");
3462
error = called_from_renewthread != 0 ? EIO : EBUSY;
3463
}
3464
if (error == 0) {
3465
nanouptime(&ts);
3466
NFSLOCKNODE(np);
3467
np->n_localmodtime = ts;
3468
NFSUNLOCKNODE(np);
3469
}
3470
return (error);
3471
}
3472
3473
/*
3474
* NFS advisory byte-level locks.
3475
*/
3476
static int
3477
nfs_advlock(struct vop_advlock_args *ap)
3478
{
3479
struct vnode *vp = ap->a_vp;
3480
struct ucred *cred;
3481
struct nfsnode *np = VTONFS(ap->a_vp);
3482
struct proc *p = (struct proc *)ap->a_id;
3483
struct thread *td = curthread; /* XXX */
3484
struct vattr va;
3485
int ret, error;
3486
u_quad_t size;
3487
struct nfsmount *nmp;
3488
3489
error = NFSVOPLOCK(vp, LK_EXCLUSIVE);
3490
if (error != 0)
3491
return (EBADF);
3492
nmp = VFSTONFS(vp->v_mount);
3493
if (!NFS_ISV4(vp) || (nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3494
if ((nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3495
size = np->n_size;
3496
NFSVOPUNLOCK(vp);
3497
error = lf_advlock(ap, &(vp->v_lockf), size);
3498
} else {
3499
if (nfs_advlock_p != NULL)
3500
error = nfs_advlock_p(ap);
3501
else {
3502
NFSVOPUNLOCK(vp);
3503
error = ENOLCK;
3504
}
3505
}
3506
if (error == 0 && ap->a_op == F_SETLK) {
3507
error = NFSVOPLOCK(vp, LK_SHARED);
3508
if (error == 0) {
3509
/* Mark that a file lock has been acquired. */
3510
NFSLOCKNODE(np);
3511
np->n_flag |= NHASBEENLOCKED;
3512
NFSUNLOCKNODE(np);
3513
NFSVOPUNLOCK(vp);
3514
}
3515
}
3516
return (error);
3517
} else if ((ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3518
if (vp->v_type != VREG) {
3519
error = EINVAL;
3520
goto out;
3521
}
3522
if ((ap->a_flags & F_POSIX) != 0)
3523
cred = p->p_ucred;
3524
else
3525
cred = td->td_ucred;
3526
3527
/*
3528
* If this is unlocking a write locked region, flush and
3529
* commit them before unlocking. This is required by
3530
* RFC3530 Sec. 9.3.2.
3531
*/
3532
if (ap->a_op == F_UNLCK &&
3533
nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3534
ap->a_flags))
3535
(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3536
3537
/*
3538
* Mark NFS node as might have acquired a lock.
3539
* This is separate from NHASBEENLOCKED, because it must
3540
* be done before the nfsrpc_advlock() call, which might
3541
* add a nfscllock structure to the client state.
3542
* It is used to check for the case where a nfscllock
3543
* state structure cannot exist for the file.
3544
* Only done for "oneopenown" NFSv4.1/4.2 mounts.
3545
*/
3546
if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3547
NFSLOCKNODE(np);
3548
np->n_flag |= NMIGHTBELOCKED;
3549
NFSUNLOCKNODE(np);
3550
}
3551
3552
/*
3553
* Loop around doing the lock op, while a blocking lock
3554
* must wait for the lock op to succeed.
3555
*/
3556
do {
3557
ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3558
ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3559
if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3560
ap->a_op == F_SETLK) {
3561
NFSVOPUNLOCK(vp);
3562
error = nfs_catnap(PZERO | PCATCH, ret,
3563
"ncladvl");
3564
if (error)
3565
return (EINTR);
3566
NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3567
if (VN_IS_DOOMED(vp)) {
3568
error = EBADF;
3569
goto out;
3570
}
3571
}
3572
} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3573
ap->a_op == F_SETLK);
3574
if (ret == NFSERR_DENIED) {
3575
error = EAGAIN;
3576
goto out;
3577
} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3578
error = ret;
3579
goto out;
3580
} else if (ret != 0) {
3581
error = EACCES;
3582
goto out;
3583
}
3584
3585
/*
3586
* Now, if we just got a lock, invalidate data in the buffer
3587
* cache, as required, so that the coherency conforms with
3588
* RFC3530 Sec. 9.3.2.
3589
*/
3590
if (ap->a_op == F_SETLK) {
3591
if ((np->n_flag & NMODIFIED) == 0) {
3592
np->n_attrstamp = 0;
3593
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3594
ret = VOP_GETATTR(vp, &va, cred);
3595
}
3596
if ((np->n_flag & NMODIFIED) || ret ||
3597
np->n_change != va.va_filerev) {
3598
(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3599
np->n_attrstamp = 0;
3600
KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3601
ret = VOP_GETATTR(vp, &va, cred);
3602
if (!ret) {
3603
np->n_mtime = va.va_mtime;
3604
np->n_change = va.va_filerev;
3605
}
3606
}
3607
/* Mark that a file lock has been acquired. */
3608
NFSLOCKNODE(np);
3609
np->n_flag |= NHASBEENLOCKED;
3610
NFSUNLOCKNODE(np);
3611
}
3612
} else
3613
error = EOPNOTSUPP;
3614
out:
3615
NFSVOPUNLOCK(vp);
3616
return (error);
3617
}
3618
3619
/*
3620
* NFS advisory byte-level locks.
3621
*/
3622
static int
3623
nfs_advlockasync(struct vop_advlockasync_args *ap)
3624
{
3625
struct vnode *vp = ap->a_vp;
3626
u_quad_t size;
3627
int error;
3628
3629
error = NFSVOPLOCK(vp, LK_SHARED);
3630
if (error)
3631
return (error);
3632
if (NFS_ISV4(vp)) {
3633
NFSVOPUNLOCK(vp);
3634
return (EOPNOTSUPP);
3635
}
3636
if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3637
size = VTONFS(vp)->n_size;
3638
NFSVOPUNLOCK(vp);
3639
error = lf_advlockasync(ap, &(vp->v_lockf), size);
3640
} else {
3641
NFSVOPUNLOCK(vp);
3642
error = EOPNOTSUPP;
3643
}
3644
return (error);
3645
}
3646
3647
/*
3648
* Print out the contents of an nfsnode.
3649
*/
3650
static int
3651
nfs_print(struct vop_print_args *ap)
3652
{
3653
struct vnode *vp = ap->a_vp;
3654
struct nfsnode *np = VTONFS(vp);
3655
3656
printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3657
(uintmax_t)np->n_vattr.na_fsid);
3658
if (vp->v_type == VFIFO)
3659
fifo_printinfo(vp);
3660
printf("\n");
3661
return (0);
3662
}
3663
3664
/*
3665
* nfs special file access vnode op.
3666
* Essentially just get vattr and then imitate iaccess() since the device is
3667
* local to the client.
3668
*/
3669
static int
3670
nfsspec_access(struct vop_access_args *ap)
3671
{
3672
struct vattr *vap;
3673
struct ucred *cred = ap->a_cred;
3674
struct vnode *vp = ap->a_vp;
3675
accmode_t accmode = ap->a_accmode;
3676
struct vattr vattr;
3677
int error;
3678
3679
/*
3680
* Disallow write attempts on filesystems mounted read-only;
3681
* unless the file is a socket, fifo, or a block or character
3682
* device resident on the filesystem.
3683
*/
3684
if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3685
switch (vp->v_type) {
3686
case VREG:
3687
case VDIR:
3688
case VLNK:
3689
return (EROFS);
3690
default:
3691
break;
3692
}
3693
}
3694
vap = &vattr;
3695
error = VOP_GETATTR(vp, vap, cred);
3696
if (error)
3697
goto out;
3698
error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3699
accmode, cred);
3700
out:
3701
return error;
3702
}
3703
3704
/*
3705
* Read wrapper for fifos.
3706
*/
3707
static int
3708
nfsfifo_read(struct vop_read_args *ap)
3709
{
3710
struct nfsnode *np = VTONFS(ap->a_vp);
3711
int error;
3712
3713
/*
3714
* Set access flag.
3715
*/
3716
NFSLOCKNODE(np);
3717
np->n_flag |= NACC;
3718
vfs_timestamp(&np->n_atim);
3719
NFSUNLOCKNODE(np);
3720
error = fifo_specops.vop_read(ap);
3721
return error;
3722
}
3723
3724
/*
3725
* Write wrapper for fifos.
3726
*/
3727
static int
3728
nfsfifo_write(struct vop_write_args *ap)
3729
{
3730
struct nfsnode *np = VTONFS(ap->a_vp);
3731
3732
/*
3733
* Set update flag.
3734
*/
3735
NFSLOCKNODE(np);
3736
np->n_flag |= NUPD;
3737
vfs_timestamp(&np->n_mtim);
3738
NFSUNLOCKNODE(np);
3739
return(fifo_specops.vop_write(ap));
3740
}
3741
3742
/*
3743
* Close wrapper for fifos.
3744
*
3745
* Update the times on the nfsnode then do fifo close.
3746
*/
3747
static int
3748
nfsfifo_close(struct vop_close_args *ap)
3749
{
3750
struct vnode *vp = ap->a_vp;
3751
struct nfsnode *np = VTONFS(vp);
3752
struct vattr vattr;
3753
struct timespec ts;
3754
3755
NFSLOCKNODE(np);
3756
if (np->n_flag & (NACC | NUPD)) {
3757
vfs_timestamp(&ts);
3758
if (np->n_flag & NACC)
3759
np->n_atim = ts;
3760
if (np->n_flag & NUPD)
3761
np->n_mtim = ts;
3762
np->n_flag |= NCHG;
3763
if (vrefcnt(vp) == 1 &&
3764
(vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3765
VATTR_NULL(&vattr);
3766
if (np->n_flag & NACC)
3767
vattr.va_atime = np->n_atim;
3768
if (np->n_flag & NUPD)
3769
vattr.va_mtime = np->n_mtim;
3770
NFSUNLOCKNODE(np);
3771
(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3772
goto out;
3773
}
3774
}
3775
NFSUNLOCKNODE(np);
3776
out:
3777
return (fifo_specops.vop_close(ap));
3778
}
3779
3780
static int
3781
nfs_getacl(struct vop_getacl_args *ap)
3782
{
3783
int error;
3784
3785
if (ap->a_type != ACL_TYPE_NFS4 && ap->a_type != ACL_TYPE_ACCESS &&
3786
ap->a_type != ACL_TYPE_DEFAULT)
3787
return (EOPNOTSUPP);
3788
if (ap->a_type == ACL_TYPE_DEFAULT && ap->a_vp->v_type != VDIR)
3789
return (EINVAL);
3790
error = nfsrpc_getacl(ap->a_vp, ap->a_type, ap->a_cred, ap->a_td,
3791
ap->a_aclp);
3792
if (error == 0 && ap->a_aclp->acl_cnt == 0 &&
3793
ap->a_type != ACL_TYPE_DEFAULT)
3794
return (EOPNOTSUPP);
3795
if (error > NFSERR_STALE) {
3796
(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3797
error = EPERM;
3798
}
3799
return (error);
3800
}
3801
3802
static int
3803
nfs_setacl(struct vop_setacl_args *ap)
3804
{
3805
int error;
3806
3807
if (ap->a_type != ACL_TYPE_NFS4 && ap->a_type != ACL_TYPE_ACCESS &&
3808
ap->a_type != ACL_TYPE_DEFAULT)
3809
return (EOPNOTSUPP);
3810
if (ap->a_aclp == NULL) {
3811
if (ap->a_type != ACL_TYPE_DEFAULT)
3812
return (EINVAL);
3813
if (ap->a_vp->v_type != VDIR)
3814
return (ENOTDIR);
3815
}
3816
error = nfsrpc_setacl(ap->a_vp, ap->a_type, ap->a_cred, ap->a_td,
3817
ap->a_aclp);
3818
if (error > NFSERR_STALE) {
3819
(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3820
error = EPERM;
3821
}
3822
return (error);
3823
}
3824
3825
/*
3826
* VOP_ADVISE for NFS.
3827
* Just return 0 for any errors, since it is just a hint.
3828
*/
3829
static int
3830
nfs_advise(struct vop_advise_args *ap)
3831
{
3832
struct thread *td = curthread;
3833
struct nfsmount *nmp;
3834
uint64_t len;
3835
int error;
3836
3837
/*
3838
* First do vop_stdadvise() to handle the buffer cache.
3839
*/
3840
error = vop_stdadvise(ap);
3841
if (error != 0)
3842
return (error);
3843
if (ap->a_start < 0 || ap->a_end < 0)
3844
return (0);
3845
if (ap->a_end == OFF_MAX)
3846
len = 0;
3847
else if (ap->a_end < ap->a_start)
3848
return (0);
3849
else
3850
len = ap->a_end - ap->a_start + 1;
3851
nmp = VFSTONFS(ap->a_vp->v_mount);
3852
mtx_lock(&nmp->nm_mtx);
3853
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3854
(NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3855
0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3856
mtx_unlock(&nmp->nm_mtx);
3857
return (0);
3858
}
3859
mtx_unlock(&nmp->nm_mtx);
3860
error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3861
td->td_ucred, td);
3862
if (error == NFSERR_NOTSUPP) {
3863
mtx_lock(&nmp->nm_mtx);
3864
nmp->nm_privflag |= NFSMNTP_NOADVISE;
3865
mtx_unlock(&nmp->nm_mtx);
3866
}
3867
return (0);
3868
}
3869
3870
/*
3871
* nfs allocate call
3872
*/
3873
static int
3874
nfs_allocate(struct vop_allocate_args *ap)
3875
{
3876
struct vnode *vp = ap->a_vp;
3877
struct thread *td = curthread;
3878
struct nfsvattr nfsva;
3879
struct nfsmount *nmp;
3880
struct nfsnode *np;
3881
off_t alen;
3882
int attrflag, error, ret;
3883
struct timespec ts;
3884
struct uio io;
3885
3886
attrflag = 0;
3887
nmp = VFSTONFS(vp->v_mount);
3888
np = VTONFS(vp);
3889
mtx_lock(&nmp->nm_mtx);
3890
if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3891
(nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3892
mtx_unlock(&nmp->nm_mtx);
3893
alen = *ap->a_len;
3894
if ((uint64_t)alen > nfs_maxalloclen)
3895
alen = nfs_maxalloclen;
3896
3897
/* Check the file size limit. */
3898
io.uio_offset = *ap->a_offset;
3899
io.uio_resid = alen;
3900
error = vn_rlimit_fsize(vp, &io, td);
3901
3902
/*
3903
* Flush first to ensure that the allocate adds to the
3904
* file's allocation on the server.
3905
*/
3906
if (error == 0) {
3907
vnode_pager_clean_sync(vp);
3908
error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3909
}
3910
if (error == 0)
3911
error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3912
&nfsva, &attrflag, ap->a_cred, td);
3913
if (error == 0) {
3914
*ap->a_offset += alen;
3915
*ap->a_len -= alen;
3916
nanouptime(&ts);
3917
NFSLOCKNODE(np);
3918
np->n_localmodtime = ts;
3919
NFSUNLOCKNODE(np);
3920
} else if (error == NFSERR_NOTSUPP) {
3921
mtx_lock(&nmp->nm_mtx);
3922
nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3923
mtx_unlock(&nmp->nm_mtx);
3924
error = EOPNOTSUPP;
3925
}
3926
} else {
3927
/*
3928
* Pre-v4.2 NFS server that doesn't support it, or a newer
3929
* NFS server that has indicated that it doesn't support it.
3930
*/
3931
mtx_unlock(&nmp->nm_mtx);
3932
error = EOPNOTSUPP;
3933
}
3934
if (attrflag != 0) {
3935
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3936
if (error == 0 && ret != 0)
3937
error = ret;
3938
}
3939
if (error != 0)
3940
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3941
return (error);
3942
}
3943
3944
/*
3945
* nfs deallocate call
3946
*/
3947
static int
3948
nfs_deallocate(struct vop_deallocate_args *ap)
3949
{
3950
struct vnode *vp = ap->a_vp;
3951
struct thread *td = curthread;
3952
struct nfsvattr nfsva;
3953
struct nfsmount *nmp;
3954
struct nfsnode *np;
3955
off_t tlen, mlen;
3956
int attrflag, error, ret;
3957
bool clipped;
3958
struct timespec ts;
3959
3960
error = 0;
3961
attrflag = 0;
3962
nmp = VFSTONFS(vp->v_mount);
3963
np = VTONFS(vp);
3964
mtx_lock(&nmp->nm_mtx);
3965
if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3966
(nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3967
mtx_unlock(&nmp->nm_mtx);
3968
tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3969
NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3970
(intmax_t)*ap->a_offset, (intmax_t)tlen,
3971
(uintmax_t)nmp->nm_maxfilesize);
3972
if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3973
/* Avoid EFBIG error return from the NFSv4.2 server. */
3974
*ap->a_len = 0;
3975
return (0);
3976
}
3977
clipped = false;
3978
if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3979
tlen = nmp->nm_maxfilesize - *ap->a_offset;
3980
if ((uint64_t)*ap->a_offset < np->n_size) {
3981
/* Limit the len to nfs_maxalloclen before EOF. */
3982
mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3983
if ((uint64_t)mlen > nfs_maxalloclen) {
3984
NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3985
tlen = nfs_maxalloclen;
3986
clipped = true;
3987
}
3988
}
3989
if (error == 0)
3990
error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3991
if (error == 0) {
3992
vnode_pager_purge_range(vp, *ap->a_offset,
3993
*ap->a_offset + tlen);
3994
error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3995
&nfsva, &attrflag, ap->a_cred, td);
3996
NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3997
}
3998
if (error == 0) {
3999
NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
4000
attrflag, (uintmax_t)nfsva.na_size);
4001
nanouptime(&ts);
4002
NFSLOCKNODE(np);
4003
np->n_localmodtime = ts;
4004
NFSUNLOCKNODE(np);
4005
if (attrflag != 0) {
4006
if ((uint64_t)*ap->a_offset < nfsva.na_size)
4007
*ap->a_offset += omin((off_t)
4008
nfsva.na_size - *ap->a_offset,
4009
tlen);
4010
}
4011
if (clipped && tlen < *ap->a_len)
4012
*ap->a_len -= tlen;
4013
else
4014
*ap->a_len = 0;
4015
} else if (error == NFSERR_NOTSUPP) {
4016
mtx_lock(&nmp->nm_mtx);
4017
nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
4018
mtx_unlock(&nmp->nm_mtx);
4019
}
4020
} else {
4021
mtx_unlock(&nmp->nm_mtx);
4022
error = EIO;
4023
}
4024
/*
4025
* If the NFS server cannot perform the Deallocate operation, just call
4026
* vop_stddeallocate() to perform it.
4027
*/
4028
if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
4029
error = vop_stddeallocate(ap);
4030
NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
4031
}
4032
if (attrflag != 0) {
4033
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4034
if (error == 0 && ret != 0)
4035
error = ret;
4036
}
4037
if (error != 0)
4038
error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
4039
return (error);
4040
}
4041
4042
/*
4043
* nfs copy_file_range call
4044
*/
4045
static int
4046
nfs_copy_file_range(struct vop_copy_file_range_args *ap)
4047
{
4048
struct vnode *invp = ap->a_invp;
4049
struct vnode *outvp = ap->a_outvp;
4050
struct mount *mp;
4051
vm_object_t invp_obj;
4052
struct nfsvattr innfsva, outnfsva;
4053
struct vattr va, *vap;
4054
struct uio io;
4055
struct nfsmount *nmp;
4056
struct nfsnode *np;
4057
size_t len, len2;
4058
ssize_t r;
4059
int error, inattrflag, outattrflag, ret, ret2, invp_lock;
4060
off_t inoff, outoff;
4061
bool consecutive, must_commit, onevp, toeof, tryclone, tryoutcred;
4062
bool mustclone;
4063
4064
/*
4065
* NFSv4.2 Copy is not permitted for infile == outfile.
4066
* The NFSv4.2 Clone operation does work on non-overlapping
4067
* byte ranges in the same file, but only if offsets
4068
* (and len if not to EOF) are aligned properly.
4069
* TODO: copy_file_range() between multiple NFS mountpoints
4070
* --> This is not possible now, since each mount appears to
4071
* the NFSv4.n server as a separate client.
4072
*/
4073
if ((invp == outvp && (ap->a_flags & COPY_FILE_RANGE_CLONE) == 0) ||
4074
(invp != outvp && invp->v_mount != outvp->v_mount)) {
4075
generic_copy:
4076
return (ENOSYS);
4077
}
4078
if (invp == outvp) {
4079
onevp = true;
4080
invp_lock = LK_EXCLUSIVE;
4081
} else {
4082
onevp = false;
4083
invp_lock = LK_SHARED;
4084
}
4085
mustclone = false;
4086
if (onevp || (ap->a_flags & COPY_FILE_RANGE_CLONE) != 0)
4087
mustclone = true;
4088
relock:
4089
inoff = *ap->a_inoffp;
4090
outoff = *ap->a_outoffp;
4091
4092
/* Lock vnode(s), avoiding risk of deadlock. */
4093
do {
4094
mp = NULL;
4095
error = vn_start_write(outvp, &mp, V_WAIT);
4096
if (error == 0) {
4097
error = vn_lock(outvp, LK_EXCLUSIVE);
4098
if (error == 0) {
4099
if (onevp)
4100
break;
4101
error = vn_lock(invp, invp_lock | LK_NOWAIT);
4102
if (error == 0)
4103
break;
4104
VOP_UNLOCK(outvp);
4105
if (mp != NULL)
4106
vn_finished_write(mp);
4107
mp = NULL;
4108
error = vn_lock(invp, invp_lock);
4109
if (error == 0)
4110
VOP_UNLOCK(invp);
4111
}
4112
}
4113
if (mp != NULL)
4114
vn_finished_write(mp);
4115
} while (error == 0);
4116
if (error != 0)
4117
return (error);
4118
4119
/*
4120
* More reasons to avoid nfs copy/clone: not NFSv4.2, explicitly
4121
* disabled or requires cloning and unable to clone.
4122
* Only clone if the clone_blksize attribute is supported
4123
* and the clone_blksize is greater than 0.
4124
* Alignment of offsets and length will be checked later.
4125
*/
4126
nmp = VFSTONFS(invp->v_mount);
4127
np = VTONFS(invp);
4128
mtx_lock(&nmp->nm_mtx);
4129
if ((nmp->nm_privflag & NFSMNTP_NOCOPY) != 0)
4130
mustclone = true;
4131
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4132
(mustclone && (!NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4133
NFSATTRBIT_CLONEBLKSIZE) || nmp->nm_cloneblksize == 0))) {
4134
mtx_unlock(&nmp->nm_mtx);
4135
VOP_UNLOCK(invp);
4136
if (!onevp)
4137
VOP_UNLOCK(outvp); /* For onevp, same as invp. */
4138
if (mp != NULL)
4139
vn_finished_write(mp);
4140
goto generic_copy;
4141
}
4142
mtx_unlock(&nmp->nm_mtx);
4143
4144
/*
4145
* Do the vn_rlimit_fsize() check. Should this be above the VOP layer?
4146
*/
4147
io.uio_offset = *ap->a_outoffp;
4148
io.uio_resid = *ap->a_lenp;
4149
error = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
4150
*ap->a_lenp = io.uio_resid;
4151
/*
4152
* No need to call vn_rlimit_fsizex_res before return, since the uio is
4153
* local.
4154
*/
4155
4156
/*
4157
* Flush the input file so that the data is up to date before
4158
* the copy. Flush writes for the output file so that they
4159
* do not overwrite the data copied to the output file by the Copy.
4160
* Set the commit argument for both flushes so that the data is on
4161
* stable storage before the Copy RPC. This is done in case the
4162
* server reboots during the Copy and needs to be redone.
4163
*/
4164
if (error == 0) {
4165
invp_obj = invp->v_object;
4166
if (invp_obj != NULL && vm_object_mightbedirty(invp_obj)) {
4167
if (invp_lock != LK_EXCLUSIVE) {
4168
KASSERT(!onevp, ("nfs_copy_file_range: "
4169
"invp_lock LK_SHARED for onevp"));
4170
invp_lock = LK_EXCLUSIVE;
4171
VOP_UNLOCK(invp);
4172
VOP_UNLOCK(outvp);
4173
if (mp != NULL)
4174
vn_finished_write(mp);
4175
goto relock;
4176
}
4177
vnode_pager_clean_sync(invp);
4178
}
4179
error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
4180
}
4181
if (error == 0)
4182
error = ncl_vinvalbuf(outvp, V_SAVE, curthread, 0);
4183
4184
/* Do the actual NFSv4.2 RPC. */
4185
ret = ret2 = 0;
4186
len = *ap->a_lenp;
4187
mtx_lock(&nmp->nm_mtx);
4188
if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
4189
consecutive = true;
4190
else
4191
consecutive = false;
4192
mtx_unlock(&nmp->nm_mtx);
4193
tryoutcred = true;
4194
must_commit = false;
4195
toeof = false;
4196
4197
if (error == 0) {
4198
vap = &VTONFS(invp)->n_vattr.na_vattr;
4199
error = VOP_GETATTR(invp, vap, ap->a_incred);
4200
if (error == 0) {
4201
/*
4202
* Clip "len" at va_size so that RFC compliant servers
4203
* will not reply NFSERR_INVAL.
4204
* Setting "len == 0" for the RPC would be preferred,
4205
* but some Linux servers do not support that.
4206
* If the len is being set to 0, do a Setattr RPC to
4207
* set the server's atime. This behaviour was the
4208
* preferred one for the FreeBSD "collective".
4209
*/
4210
if (inoff >= vap->va_size) {
4211
*ap->a_lenp = len = 0;
4212
if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) ==
4213
0) {
4214
VATTR_NULL(&va);
4215
va.va_atime.tv_sec = 0;
4216
va.va_atime.tv_nsec = 0;
4217
va.va_vaflags = VA_UTIMES_NULL;
4218
inattrflag = 0;
4219
error = nfsrpc_setattr(invp, &va, NULL,
4220
0, ap->a_incred, curthread,
4221
&innfsva, &inattrflag);
4222
if (inattrflag != 0)
4223
ret = nfscl_loadattrcache(&invp,
4224
&innfsva, NULL, 0, 1);
4225
if (error == 0 && ret != 0)
4226
error = ret;
4227
}
4228
} else if (inoff + len >= vap->va_size) {
4229
toeof = true;
4230
*ap->a_lenp = len = vap->va_size - inoff;
4231
}
4232
} else
4233
error = 0;
4234
}
4235
4236
/*
4237
* For cloning, the offsets must be clone blksize aligned and
4238
* the len must be blksize aligned unless it goes to EOF on
4239
* the input file.
4240
*/
4241
tryclone = false;
4242
if (len > 0) {
4243
if (error == 0 && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4244
NFSATTRBIT_CLONEBLKSIZE) && nmp->nm_cloneblksize != 0 &&
4245
(inoff % nmp->nm_cloneblksize) == 0 &&
4246
(outoff % nmp->nm_cloneblksize) == 0 &&
4247
(toeof || (len % nmp->nm_cloneblksize) == 0))
4248
tryclone = true;
4249
else if (mustclone)
4250
error = ENOSYS;
4251
}
4252
4253
/*
4254
* len will be set to 0 upon a successful Copy RPC.
4255
* As such, this only loops when the Copy/Clone RPC needs to be retried.
4256
*/
4257
while (len > 0 && error == 0) {
4258
inattrflag = outattrflag = 0;
4259
len2 = len;
4260
if (tryclone) {
4261
if (tryoutcred)
4262
error = nfsrpc_clone(invp, ap->a_inoffp, outvp,
4263
ap->a_outoffp, &len2, toeof, &inattrflag,
4264
&innfsva, &outattrflag, &outnfsva,
4265
ap->a_outcred);
4266
else
4267
error = nfsrpc_clone(invp, ap->a_inoffp, outvp,
4268
ap->a_outoffp, &len2, toeof, &inattrflag,
4269
&innfsva, &outattrflag, &outnfsva,
4270
ap->a_incred);
4271
} else {
4272
if (tryoutcred)
4273
error = nfsrpc_copy_file_range(invp,
4274
ap->a_inoffp, outvp, ap->a_outoffp, &len2,
4275
ap->a_flags, &inattrflag, &innfsva,
4276
&outattrflag, &outnfsva,
4277
ap->a_outcred, consecutive, &must_commit);
4278
else
4279
error = nfsrpc_copy_file_range(invp,
4280
ap->a_inoffp, outvp, ap->a_outoffp, &len2,
4281
ap->a_flags, &inattrflag, &innfsva,
4282
&outattrflag, &outnfsva,
4283
ap->a_incred, consecutive, &must_commit);
4284
}
4285
if (inattrflag != 0)
4286
ret = nfscl_loadattrcache(&invp, &innfsva, NULL, 0, 1);
4287
if (outattrflag != 0)
4288
ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
4289
1, 1);
4290
if (error == 0) {
4291
if (consecutive == false) {
4292
if (len2 == len) {
4293
mtx_lock(&nmp->nm_mtx);
4294
nmp->nm_privflag |=
4295
NFSMNTP_NOCONSECUTIVE;
4296
mtx_unlock(&nmp->nm_mtx);
4297
} else
4298
error = NFSERR_OFFLOADNOREQS;
4299
}
4300
*ap->a_lenp = len2;
4301
len = 0;
4302
if (len2 > 0 && must_commit && error == 0)
4303
error = ncl_commit(outvp, outoff, *ap->a_lenp,
4304
ap->a_outcred, curthread);
4305
if (error == 0 && ret != 0)
4306
error = ret;
4307
if (error == 0 && ret2 != 0)
4308
error = ret2;
4309
} else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4310
/*
4311
* Try consecutive == false, which is ok only if all
4312
* bytes are copied.
4313
* If only some bytes were copied when consecutive
4314
* is false, there is no way to know which bytes
4315
* still need to be written.
4316
*/
4317
consecutive = false;
4318
error = 0;
4319
} else if (error == NFSERR_ACCES && tryoutcred) {
4320
/* Try again with incred. */
4321
tryoutcred = false;
4322
error = 0;
4323
} else if (tryclone && error != 0) {
4324
if (mustclone) {
4325
error = ENOSYS;
4326
} else {
4327
tryclone = false;
4328
error = 0;
4329
}
4330
}
4331
if (error == NFSERR_STALEWRITEVERF) {
4332
/*
4333
* Server rebooted, so do it all again.
4334
*/
4335
*ap->a_inoffp = inoff;
4336
*ap->a_outoffp = outoff;
4337
len = *ap->a_lenp;
4338
must_commit = false;
4339
error = 0;
4340
}
4341
}
4342
VOP_UNLOCK(invp);
4343
if (!onevp)
4344
VOP_UNLOCK(outvp); /* For onevp, same as invp. */
4345
if (mp != NULL)
4346
vn_finished_write(mp);
4347
if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4348
error == NFSERR_ACCES || error == ENOSYS) {
4349
/*
4350
* Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4351
* use a_incred for the read and a_outcred for the write, so
4352
* try this for NFSERR_ACCES failures for the Copy.
4353
* For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4354
* never succeed, so disable it.
4355
*/
4356
if (error != NFSERR_ACCES && error != ENOSYS) {
4357
/* Can never do Copy on this mount. */
4358
mtx_lock(&nmp->nm_mtx);
4359
nmp->nm_privflag |= NFSMNTP_NOCOPY;
4360
mtx_unlock(&nmp->nm_mtx);
4361
}
4362
*ap->a_inoffp = inoff;
4363
*ap->a_outoffp = outoff;
4364
error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4365
ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4366
ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4367
} else if (error != 0)
4368
*ap->a_lenp = 0;
4369
4370
if (error != 0)
4371
error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4372
return (error);
4373
}
4374
4375
/*
4376
* nfs ioctl call
4377
*/
4378
static int
4379
nfs_ioctl(struct vop_ioctl_args *ap)
4380
{
4381
struct vnode *vp = ap->a_vp;
4382
struct nfsvattr nfsva;
4383
struct nfsmount *nmp;
4384
int attrflag, content, error, ret;
4385
bool eof = false; /* shut up compiler. */
4386
4387
/* Do the actual NFSv4.2 RPC. */
4388
switch (ap->a_command) {
4389
case FIOSEEKDATA:
4390
content = NFSV4CONTENT_DATA;
4391
break;
4392
case FIOSEEKHOLE:
4393
content = NFSV4CONTENT_HOLE;
4394
break;
4395
default:
4396
return (ENOTTY);
4397
}
4398
4399
error = vn_lock(vp, LK_EXCLUSIVE);
4400
if (error != 0)
4401
return (EBADF);
4402
4403
if (vp->v_type != VREG) {
4404
VOP_UNLOCK(vp);
4405
return (ENOTTY);
4406
}
4407
nmp = VFSTONFS(vp->v_mount);
4408
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4409
VOP_UNLOCK(vp);
4410
error = vop_stdioctl(ap);
4411
return (error);
4412
}
4413
4414
attrflag = 0;
4415
if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4416
error = ENXIO;
4417
else {
4418
/*
4419
* Flush all writes, so that the server is up to date.
4420
* Although a Commit is not required, the commit argument
4421
* is set so that, for a pNFS File/Flexible File Layout
4422
* server, the LayoutCommit will be done to ensure the file
4423
* size is up to date on the Metadata Server.
4424
*/
4425
4426
vnode_pager_clean_sync(vp);
4427
error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4428
if (error == 0)
4429
error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4430
content, ap->a_cred, &nfsva, &attrflag);
4431
/* If at eof for FIOSEEKDATA, return ENXIO. */
4432
if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4433
error = ENXIO;
4434
}
4435
if (attrflag != 0) {
4436
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4437
if (error == 0 && ret != 0)
4438
error = ret;
4439
}
4440
NFSVOPUNLOCK(vp);
4441
4442
if (error != 0)
4443
error = ENXIO;
4444
return (error);
4445
}
4446
4447
/*
4448
* nfs getextattr call
4449
*/
4450
static int
4451
nfs_getextattr(struct vop_getextattr_args *ap)
4452
{
4453
struct vnode *vp = ap->a_vp;
4454
struct nfsmount *nmp;
4455
struct ucred *cred;
4456
struct thread *td = ap->a_td;
4457
struct nfsvattr nfsva;
4458
ssize_t len;
4459
int attrflag, error, ret;
4460
4461
nmp = VFSTONFS(vp->v_mount);
4462
mtx_lock(&nmp->nm_mtx);
4463
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4464
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4465
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4466
mtx_unlock(&nmp->nm_mtx);
4467
return (EOPNOTSUPP);
4468
}
4469
mtx_unlock(&nmp->nm_mtx);
4470
4471
cred = ap->a_cred;
4472
if (cred == NULL)
4473
cred = td->td_ucred;
4474
/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4475
attrflag = 0;
4476
error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4477
&attrflag, cred, td);
4478
if (attrflag != 0) {
4479
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4480
if (error == 0 && ret != 0)
4481
error = ret;
4482
}
4483
if (error == 0 && ap->a_size != NULL)
4484
*ap->a_size = len;
4485
4486
switch (error) {
4487
case NFSERR_NOTSUPP:
4488
case NFSERR_OPILLEGAL:
4489
mtx_lock(&nmp->nm_mtx);
4490
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4491
mtx_unlock(&nmp->nm_mtx);
4492
error = EOPNOTSUPP;
4493
break;
4494
case NFSERR_NOXATTR:
4495
case NFSERR_XATTR2BIG:
4496
error = ENOATTR;
4497
break;
4498
default:
4499
error = nfscl_maperr(td, error, 0, 0);
4500
break;
4501
}
4502
return (error);
4503
}
4504
4505
/*
4506
* nfs setextattr call
4507
*/
4508
static int
4509
nfs_setextattr(struct vop_setextattr_args *ap)
4510
{
4511
struct vnode *vp = ap->a_vp;
4512
struct nfsmount *nmp;
4513
struct ucred *cred;
4514
struct thread *td = ap->a_td;
4515
struct nfsvattr nfsva;
4516
int attrflag, error, ret;
4517
4518
nmp = VFSTONFS(vp->v_mount);
4519
mtx_lock(&nmp->nm_mtx);
4520
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4521
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4522
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4523
mtx_unlock(&nmp->nm_mtx);
4524
return (EOPNOTSUPP);
4525
}
4526
mtx_unlock(&nmp->nm_mtx);
4527
4528
if (ap->a_uio->uio_resid < 0)
4529
return (EINVAL);
4530
cred = ap->a_cred;
4531
if (cred == NULL)
4532
cred = td->td_ucred;
4533
/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4534
attrflag = 0;
4535
error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4536
&attrflag, cred, td);
4537
if (attrflag != 0) {
4538
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4539
if (error == 0 && ret != 0)
4540
error = ret;
4541
}
4542
4543
switch (error) {
4544
case NFSERR_NOTSUPP:
4545
case NFSERR_OPILLEGAL:
4546
mtx_lock(&nmp->nm_mtx);
4547
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4548
mtx_unlock(&nmp->nm_mtx);
4549
error = EOPNOTSUPP;
4550
break;
4551
case NFSERR_NOXATTR:
4552
case NFSERR_XATTR2BIG:
4553
error = ENOATTR;
4554
break;
4555
default:
4556
error = nfscl_maperr(td, error, 0, 0);
4557
break;
4558
}
4559
return (error);
4560
}
4561
4562
/*
4563
* nfs listextattr call
4564
*/
4565
static int
4566
nfs_listextattr(struct vop_listextattr_args *ap)
4567
{
4568
struct vnode *vp = ap->a_vp;
4569
struct nfsmount *nmp;
4570
struct ucred *cred;
4571
struct thread *td = ap->a_td;
4572
struct nfsvattr nfsva;
4573
size_t len, len2;
4574
uint64_t cookie;
4575
int attrflag, error, ret;
4576
bool eof;
4577
4578
nmp = VFSTONFS(vp->v_mount);
4579
mtx_lock(&nmp->nm_mtx);
4580
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4581
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4582
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4583
mtx_unlock(&nmp->nm_mtx);
4584
return (EOPNOTSUPP);
4585
}
4586
mtx_unlock(&nmp->nm_mtx);
4587
4588
cred = ap->a_cred;
4589
if (cred == NULL)
4590
cred = td->td_ucred;
4591
4592
/* Loop around doing List Extended Attribute RPCs. */
4593
eof = false;
4594
cookie = 0;
4595
len2 = 0;
4596
error = 0;
4597
while (!eof && error == 0) {
4598
len = nmp->nm_rsize;
4599
attrflag = 0;
4600
error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4601
&nfsva, &attrflag, cred, td);
4602
if (attrflag != 0) {
4603
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4604
if (error == 0 && ret != 0)
4605
error = ret;
4606
}
4607
if (error == 0) {
4608
len2 += len;
4609
if (len2 > SSIZE_MAX)
4610
error = ENOATTR;
4611
}
4612
}
4613
if (error == 0 && ap->a_size != NULL)
4614
*ap->a_size = len2;
4615
4616
switch (error) {
4617
case NFSERR_NOTSUPP:
4618
case NFSERR_OPILLEGAL:
4619
mtx_lock(&nmp->nm_mtx);
4620
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4621
mtx_unlock(&nmp->nm_mtx);
4622
error = EOPNOTSUPP;
4623
break;
4624
case NFSERR_NOXATTR:
4625
case NFSERR_XATTR2BIG:
4626
error = ENOATTR;
4627
break;
4628
default:
4629
error = nfscl_maperr(td, error, 0, 0);
4630
break;
4631
}
4632
return (error);
4633
}
4634
4635
/*
4636
* nfs setextattr call
4637
*/
4638
static int
4639
nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4640
{
4641
struct vnode *vp = ap->a_vp;
4642
struct nfsmount *nmp;
4643
struct nfsvattr nfsva;
4644
int attrflag, error, ret;
4645
4646
nmp = VFSTONFS(vp->v_mount);
4647
mtx_lock(&nmp->nm_mtx);
4648
if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4649
(nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4650
ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4651
mtx_unlock(&nmp->nm_mtx);
4652
return (EOPNOTSUPP);
4653
}
4654
mtx_unlock(&nmp->nm_mtx);
4655
4656
/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4657
attrflag = 0;
4658
error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4659
ap->a_td);
4660
if (attrflag != 0) {
4661
ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4662
if (error == 0 && ret != 0)
4663
error = ret;
4664
}
4665
4666
switch (error) {
4667
case NFSERR_NOTSUPP:
4668
case NFSERR_OPILLEGAL:
4669
mtx_lock(&nmp->nm_mtx);
4670
nmp->nm_privflag |= NFSMNTP_NOXATTR;
4671
mtx_unlock(&nmp->nm_mtx);
4672
error = EOPNOTSUPP;
4673
break;
4674
case NFSERR_NOXATTR:
4675
case NFSERR_XATTR2BIG:
4676
error = ENOATTR;
4677
break;
4678
default:
4679
error = nfscl_maperr(ap->a_td, error, 0, 0);
4680
break;
4681
}
4682
return (error);
4683
}
4684
4685
/*
4686
* Return POSIX pathconf information applicable to nfs filesystems.
4687
*/
4688
static int
4689
nfs_pathconf(struct vop_pathconf_args *ap)
4690
{
4691
struct nfsv3_pathconf pc;
4692
struct nfsvattr nfsva;
4693
struct vnode *vp = ap->a_vp;
4694
struct nfsmount *nmp;
4695
struct thread *td = curthread;
4696
off_t off;
4697
uint32_t clone_blksize;
4698
bool eof, has_namedattr, named_enabled;
4699
int attrflag, error;
4700
struct nfsnode *np;
4701
uint32_t trueform;
4702
4703
nmp = VFSTONFS(vp->v_mount);
4704
np = VTONFS(vp);
4705
named_enabled = false;
4706
has_namedattr = false;
4707
clone_blksize = 0;
4708
if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4709
ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4710
ap->a_name == _PC_NO_TRUNC ||
4711
ap->a_name == _PC_CASE_INSENSITIVE)) ||
4712
(NFS_ISV4(vp) && (ap->a_name == _PC_ACL_NFS4 ||
4713
ap->a_name == _PC_HAS_NAMEDATTR ||
4714
ap->a_name == _PC_CLONE_BLKSIZE ||
4715
ap->a_name == _PC_ACL_EXTENDED))) {
4716
/*
4717
* Since only the above 5 a_names are returned by the NFSv3
4718
* Pathconf RPC, there is no point in doing it for others.
4719
* For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4720
* be used for _PC_ACL_NFS4, _PC_HAS_NAMEDATTR,
4721
* and _PC_ACL_EXTENDED as well.
4722
*/
4723
trueform = UINT32_MAX;
4724
error = nfsrpc_pathconf(vp, &pc, &has_namedattr, &clone_blksize,
4725
td->td_ucred, td, &nfsva, &attrflag, &trueform);
4726
if (attrflag != 0)
4727
(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4728
if (error != 0)
4729
return (error);
4730
} else if (NFS_ISV4(vp) && ap->a_name == _PC_NAMEDATTR_ENABLED &&
4731
(np->n_flag & NNAMEDNOTSUPP) == 0) {
4732
struct nfsfh *nfhp;
4733
4734
error = nfsrpc_openattr(nmp, vp, np->n_fhp->nfh_fh,
4735
np->n_fhp->nfh_len, false, td->td_ucred, td, &nfsva, &nfhp,
4736
&attrflag);
4737
named_enabled = true;
4738
if (error == 0) {
4739
free(nfhp, M_NFSFH);
4740
} else if (error == NFSERR_NOTSUPP) {
4741
named_enabled = false;
4742
NFSLOCKNODE(np);
4743
np->n_flag |= NNAMEDNOTSUPP;
4744
NFSUNLOCKNODE(np);
4745
}
4746
error = 0;
4747
} else {
4748
/*
4749
* For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4750
* just fake them.
4751
*/
4752
pc.pc_linkmax = NFS_LINK_MAX;
4753
pc.pc_namemax = NFS_MAXNAMLEN;
4754
pc.pc_notrunc = 1;
4755
pc.pc_chownrestricted = 1;
4756
pc.pc_caseinsensitive = 0;
4757
pc.pc_casepreserving = 1;
4758
error = 0;
4759
}
4760
switch (ap->a_name) {
4761
case _PC_LINK_MAX:
4762
#ifdef _LP64
4763
*ap->a_retval = pc.pc_linkmax;
4764
#else
4765
*ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4766
#endif
4767
break;
4768
case _PC_NAME_MAX:
4769
*ap->a_retval = pc.pc_namemax;
4770
break;
4771
case _PC_PIPE_BUF:
4772
if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4773
*ap->a_retval = PIPE_BUF;
4774
else
4775
error = EINVAL;
4776
break;
4777
case _PC_CHOWN_RESTRICTED:
4778
*ap->a_retval = pc.pc_chownrestricted;
4779
break;
4780
case _PC_NO_TRUNC:
4781
*ap->a_retval = pc.pc_notrunc;
4782
break;
4783
case _PC_ACL_NFS4:
4784
if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4785
NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL) &&
4786
(trueform == NFSV4_ACL_MODEL_NFS4 ||
4787
trueform == UINT32_MAX))
4788
*ap->a_retval = 1;
4789
else
4790
*ap->a_retval = 0;
4791
break;
4792
case _PC_ACL_EXTENDED:
4793
if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4794
NFSISSET_ATTRBIT(&nfsva.na_suppattr,
4795
NFSATTRBIT_POSIXACCESSACL) &&
4796
NFSISSET_ATTRBIT(&nfsva.na_suppattr,
4797
NFSATTRBIT_POSIXDEFAULTACL) &&
4798
trueform == NFSV4_ACL_MODEL_POSIX_DRAFT)
4799
*ap->a_retval = 1;
4800
else
4801
*ap->a_retval = 0;
4802
break;
4803
case _PC_ACL_PATH_MAX:
4804
if (NFS_ISV4(vp))
4805
*ap->a_retval = ACL_MAX_ENTRIES;
4806
else
4807
*ap->a_retval = 3;
4808
break;
4809
case _PC_PRIO_IO:
4810
*ap->a_retval = 0;
4811
break;
4812
case _PC_SYNC_IO:
4813
*ap->a_retval = 0;
4814
break;
4815
case _PC_ALLOC_SIZE_MIN:
4816
*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4817
break;
4818
case _PC_FILESIZEBITS:
4819
if (NFS_ISV34(vp))
4820
*ap->a_retval = 64;
4821
else
4822
*ap->a_retval = 32;
4823
break;
4824
case _PC_REC_INCR_XFER_SIZE:
4825
*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4826
break;
4827
case _PC_REC_MAX_XFER_SIZE:
4828
*ap->a_retval = -1; /* means ``unlimited'' */
4829
break;
4830
case _PC_REC_MIN_XFER_SIZE:
4831
*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4832
break;
4833
case _PC_REC_XFER_ALIGN:
4834
*ap->a_retval = PAGE_SIZE;
4835
break;
4836
case _PC_SYMLINK_MAX:
4837
*ap->a_retval = NFS_MAXPATHLEN;
4838
break;
4839
case _PC_MIN_HOLE_SIZE:
4840
/* Only some NFSv4.2 servers support Seek for Holes. */
4841
*ap->a_retval = 0;
4842
if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4843
/*
4844
* NFSv4.2 doesn't have an attribute for hole size,
4845
* so all we can do is see if the Seek operation is
4846
* supported and then use f_iosize as a "best guess".
4847
*/
4848
mtx_lock(&nmp->nm_mtx);
4849
if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4850
mtx_unlock(&nmp->nm_mtx);
4851
off = 0;
4852
attrflag = 0;
4853
error = nfsrpc_seek(vp, &off, &eof,
4854
NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4855
&attrflag);
4856
if (attrflag != 0)
4857
(void) nfscl_loadattrcache(&vp, &nfsva,
4858
NULL, 0, 1);
4859
mtx_lock(&nmp->nm_mtx);
4860
if (error == NFSERR_NOTSUPP)
4861
nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4862
else
4863
nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4864
NFSMNTP_SEEK;
4865
error = 0;
4866
}
4867
if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4868
*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4869
mtx_unlock(&nmp->nm_mtx);
4870
}
4871
break;
4872
case _PC_NAMEDATTR_ENABLED:
4873
if (named_enabled)
4874
*ap->a_retval = 1;
4875
else
4876
*ap->a_retval = 0;
4877
break;
4878
case _PC_HAS_NAMEDATTR:
4879
if (has_namedattr)
4880
*ap->a_retval = 1;
4881
else
4882
*ap->a_retval = 0;
4883
break;
4884
case _PC_HAS_HIDDENSYSTEM:
4885
if (NFS_ISV4(vp) && NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4886
NFSATTRBIT_ARCHIVE) &&
4887
NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4888
NFSATTRBIT_HIDDEN) &&
4889
NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
4890
NFSATTRBIT_SYSTEM))
4891
*ap->a_retval = 1;
4892
else
4893
*ap->a_retval = 0;
4894
break;
4895
case _PC_CLONE_BLKSIZE:
4896
*ap->a_retval = clone_blksize;
4897
break;
4898
case _PC_CASE_INSENSITIVE:
4899
*ap->a_retval = pc.pc_caseinsensitive;
4900
break;
4901
4902
default:
4903
error = vop_stdpathconf(ap);
4904
break;
4905
}
4906
return (error);
4907
}
4908
4909