2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
59 * FreeBSD-Id: nfs_vnops.c,v 1.72 1997/11/07 09:20:48 phk Exp $
64 * vnode op calls for Sun NFS version 2 and 3
66 #include <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/systm.h>
69 #include <sys/resourcevar.h>
70 #include <sys/proc_internal.h>
71 #include <sys/kauth.h>
72 #include <sys/mount_internal.h>
73 #include <sys/malloc.h>
74 #include <sys/kpi_mbuf.h>
76 #include <sys/vnode_internal.h>
77 #include <sys/dirent.h>
78 #include <sys/fcntl.h>
79 #include <sys/lockf.h>
80 #include <sys/ubc_internal.h>
82 #include <sys/signalvar.h>
83 #include <sys/uio_internal.h>
85 #include <vfs/vfs_support.h>
90 #include <kern/clock.h>
91 #include <libkern/OSAtomic.h>
93 #include <miscfs/fifofs/fifo.h>
94 #include <miscfs/specfs/specdev.h>
96 #include <nfs/rpcv2.h>
97 #include <nfs/nfsproto.h>
99 #include <nfs/nfsnode.h>
100 #include <nfs/nfsmount.h>
101 #include <nfs/nfs_lock.h>
102 #include <nfs/xdr_subs.h>
103 #include <nfs/nfsm_subs.h>
106 #include <netinet/in.h>
107 #include <netinet/in_var.h>
108 #include <vm/vm_kern.h>
110 #include <kern/task.h>
111 #include <kern/sched_prim.h>
113 #include <sys/kdebug.h>
115 #define FSDBG(A, B, C, D, E) \
116 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
117 (int)(B), (int)(C), (int)(D), (int)(E), 0)
118 #define FSDBG_TOP(A, B, C, D, E) \
119 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
120 (int)(B), (int)(C), (int)(D), (int)(E), 0)
121 #define FSDBG_BOT(A, B, C, D, E) \
122 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
123 (int)(B), (int)(C), (int)(D), (int)(E), 0)
125 static int nfsspec_read(struct vnop_read_args
*);
126 static int nfsspec_write(struct vnop_write_args
*);
127 static int nfsfifo_read(struct vnop_read_args
*);
128 static int nfsfifo_write(struct vnop_write_args
*);
129 static int nfsspec_close(struct vnop_close_args
*);
130 static int nfsfifo_close(struct vnop_close_args
*);
131 static int nfs_ioctl(struct vnop_ioctl_args
*);
132 static int nfs_select(struct vnop_select_args
*);
133 static int nfs_setattrrpc(vnode_t
,struct vnode_attr
*,kauth_cred_t
,proc_t
);
134 static int nfs_lookup(struct vnop_lookup_args
*);
135 static int nfs_create(struct vnop_create_args
*);
136 static int nfs_mknod(struct vnop_mknod_args
*);
137 static int nfs_open(struct vnop_open_args
*);
138 static int nfs_close(struct vnop_close_args
*);
139 static int nfs_access(struct vnop_access_args
*);
140 static int nfs_vnop_getattr(struct vnop_getattr_args
*);
141 static int nfs_setattr(struct vnop_setattr_args
*);
142 static int nfs_read(struct vnop_read_args
*);
143 static int nfs_mmap(struct vnop_mmap_args
*);
144 static int nfs_fsync(struct vnop_fsync_args
*);
145 static int nfs_remove(struct vnop_remove_args
*);
146 static int nfs_link(struct vnop_link_args
*);
147 static int nfs_rename(struct vnop_rename_args
*);
148 static int nfs_mkdir(struct vnop_mkdir_args
*);
149 static int nfs_rmdir(struct vnop_rmdir_args
*);
150 static int nfs_symlink(struct vnop_symlink_args
*);
151 static int nfs_readdir(struct vnop_readdir_args
*);
152 static int nfs_lookitup(vnode_t
,char *,int,kauth_cred_t
,proc_t
,struct nfsnode
**);
153 static int nfs_sillyrename(vnode_t
,vnode_t
,struct componentname
*,kauth_cred_t
,proc_t
);
154 static int nfs_readlink(struct vnop_readlink_args
*);
155 static int nfs_pathconf(struct vnop_pathconf_args
*);
156 static int nfs_advlock(struct vnop_advlock_args
*);
157 static int nfs_pagein(struct vnop_pagein_args
*);
158 static int nfs_pageout(struct vnop_pageout_args
*);
159 static int nfs_blktooff(struct vnop_blktooff_args
*);
160 static int nfs_offtoblk(struct vnop_offtoblk_args
*);
161 static int nfs_blockmap(struct vnop_blockmap_args
*);
164 * Global vfs data structures for nfs
166 vnop_t
**nfsv2_vnodeop_p
;
167 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries
[] = {
168 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
169 { &vnop_lookup_desc
, (vnop_t
*)nfs_lookup
}, /* lookup */
170 { &vnop_create_desc
, (vnop_t
*)nfs_create
}, /* create */
171 { &vnop_mknod_desc
, (vnop_t
*)nfs_mknod
}, /* mknod */
172 { &vnop_open_desc
, (vnop_t
*)nfs_open
}, /* open */
173 { &vnop_close_desc
, (vnop_t
*)nfs_close
}, /* close */
174 { &vnop_access_desc
, (vnop_t
*)nfs_access
}, /* access */
175 { &vnop_getattr_desc
, (vnop_t
*)nfs_vnop_getattr
}, /* getattr */
176 { &vnop_setattr_desc
, (vnop_t
*)nfs_setattr
}, /* setattr */
177 { &vnop_read_desc
, (vnop_t
*)nfs_read
}, /* read */
178 { &vnop_write_desc
, (vnop_t
*)nfs_write
}, /* write */
179 { &vnop_ioctl_desc
, (vnop_t
*)nfs_ioctl
}, /* ioctl */
180 { &vnop_select_desc
, (vnop_t
*)nfs_select
}, /* select */
181 { &vnop_revoke_desc
, (vnop_t
*)nfs_revoke
}, /* revoke */
182 { &vnop_mmap_desc
, (vnop_t
*)nfs_mmap
}, /* mmap */
183 { &vnop_fsync_desc
, (vnop_t
*)nfs_fsync
}, /* fsync */
184 { &vnop_remove_desc
, (vnop_t
*)nfs_remove
}, /* remove */
185 { &vnop_link_desc
, (vnop_t
*)nfs_link
}, /* link */
186 { &vnop_rename_desc
, (vnop_t
*)nfs_rename
}, /* rename */
187 { &vnop_mkdir_desc
, (vnop_t
*)nfs_mkdir
}, /* mkdir */
188 { &vnop_rmdir_desc
, (vnop_t
*)nfs_rmdir
}, /* rmdir */
189 { &vnop_symlink_desc
, (vnop_t
*)nfs_symlink
}, /* symlink */
190 { &vnop_readdir_desc
, (vnop_t
*)nfs_readdir
}, /* readdir */
191 { &vnop_readlink_desc
, (vnop_t
*)nfs_readlink
}, /* readlink */
192 { &vnop_inactive_desc
, (vnop_t
*)nfs_inactive
}, /* inactive */
193 { &vnop_reclaim_desc
, (vnop_t
*)nfs_reclaim
}, /* reclaim */
194 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
195 { &vnop_pathconf_desc
, (vnop_t
*)nfs_pathconf
}, /* pathconf */
196 { &vnop_advlock_desc
, (vnop_t
*)nfs_advlock
}, /* advlock */
197 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
198 { &vnop_pagein_desc
, (vnop_t
*)nfs_pagein
}, /* Pagein */
199 { &vnop_pageout_desc
, (vnop_t
*)nfs_pageout
}, /* Pageout */
200 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
201 { &vnop_blktooff_desc
, (vnop_t
*)nfs_blktooff
}, /* blktooff */
202 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_offtoblk
}, /* offtoblk */
203 { &vnop_blockmap_desc
, (vnop_t
*)nfs_blockmap
}, /* blockmap */
206 struct vnodeopv_desc nfsv2_vnodeop_opv_desc
=
207 { &nfsv2_vnodeop_p
, nfsv2_vnodeop_entries
};
209 VNODEOP_SET(nfsv2_vnodeop_opv_desc
);
213 * Special device vnode ops
215 vnop_t
**spec_nfsv2nodeop_p
;
216 static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries
[] = {
217 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
218 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
219 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
220 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
221 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
222 { &vnop_close_desc
, (vnop_t
*)nfsspec_close
}, /* close */
223 { &vnop_getattr_desc
, (vnop_t
*)nfs_vnop_getattr
}, /* getattr */
224 { &vnop_setattr_desc
, (vnop_t
*)nfs_setattr
}, /* setattr */
225 { &vnop_read_desc
, (vnop_t
*)nfsspec_read
}, /* read */
226 { &vnop_write_desc
, (vnop_t
*)nfsspec_write
}, /* write */
227 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
228 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
229 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
230 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
231 { &vnop_fsync_desc
, (vnop_t
*)nfs_fsync
}, /* fsync */
232 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
233 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
234 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
235 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
236 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
237 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
238 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
239 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
240 { &vnop_inactive_desc
, (vnop_t
*)nfs_inactive
}, /* inactive */
241 { &vnop_reclaim_desc
, (vnop_t
*)nfs_reclaim
}, /* reclaim */
242 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
243 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
244 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
245 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
246 { &vnop_pagein_desc
, (vnop_t
*)nfs_pagein
}, /* Pagein */
247 { &vnop_pageout_desc
, (vnop_t
*)nfs_pageout
}, /* Pageout */
248 { &vnop_blktooff_desc
, (vnop_t
*)nfs_blktooff
}, /* blktooff */
249 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_offtoblk
}, /* offtoblk */
250 { &vnop_blockmap_desc
, (vnop_t
*)nfs_blockmap
}, /* blockmap */
253 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc
=
254 { &spec_nfsv2nodeop_p
, spec_nfsv2nodeop_entries
};
256 VNODEOP_SET(spec_nfsv2nodeop_opv_desc
);
259 vnop_t
**fifo_nfsv2nodeop_p
;
260 static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries
[] = {
261 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
262 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
263 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
264 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
265 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
266 { &vnop_close_desc
, (vnop_t
*)nfsfifo_close
}, /* close */
267 { &vnop_getattr_desc
, (vnop_t
*)nfs_vnop_getattr
}, /* getattr */
268 { &vnop_setattr_desc
, (vnop_t
*)nfs_setattr
}, /* setattr */
269 { &vnop_read_desc
, (vnop_t
*)nfsfifo_read
}, /* read */
270 { &vnop_write_desc
, (vnop_t
*)nfsfifo_write
}, /* write */
271 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
272 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
273 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
274 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
275 { &vnop_fsync_desc
, (vnop_t
*)nfs_fsync
}, /* fsync */
276 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
277 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
278 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
279 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
280 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
281 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
282 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
283 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
284 { &vnop_inactive_desc
, (vnop_t
*)nfs_inactive
}, /* inactive */
285 { &vnop_reclaim_desc
, (vnop_t
*)nfs_reclaim
}, /* reclaim */
286 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
287 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
288 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
289 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
290 { &vnop_pagein_desc
, (vnop_t
*)nfs_pagein
}, /* Pagein */
291 { &vnop_pageout_desc
, (vnop_t
*)nfs_pageout
}, /* Pageout */
292 { &vnop_blktooff_desc
, (vnop_t
*)nfs_blktooff
}, /* blktooff */
293 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_offtoblk
}, /* offtoblk */
294 { &vnop_blockmap_desc
, (vnop_t
*)nfs_blockmap
}, /* blockmap */
297 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc
=
298 { &fifo_nfsv2nodeop_p
, fifo_nfsv2nodeop_entries
};
300 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc
);
303 static int nfs_mknodrpc(vnode_t dvp
, vnode_t
*vpp
,
304 struct componentname
*cnp
,
305 struct vnode_attr
*vap
,
306 kauth_cred_t cred
, proc_t p
);
307 static int nfs_removerpc(vnode_t dvp
, char *name
, int namelen
,
308 kauth_cred_t cred
, proc_t proc
);
309 static int nfs_renamerpc(vnode_t fdvp
, char *fnameptr
,
310 int fnamelen
, vnode_t tdvp
,
311 char *tnameptr
, int tnamelen
,
312 kauth_cred_t cred
, proc_t proc
);
317 extern u_long nfs_xdrneg1
;
318 extern u_long nfs_true
, nfs_false
;
319 extern struct nfsstats nfsstats
;
320 extern nfstype nfsv3_type
[9];
321 proc_t nfs_iodwant
[NFS_MAXASYNCDAEMON
];
322 struct nfsmount
*nfs_iodmount
[NFS_MAXASYNCDAEMON
];
324 lck_grp_t
*nfs_iod_lck_grp
;
325 lck_grp_attr_t
*nfs_iod_lck_grp_attr
;
326 lck_attr_t
*nfs_iod_lck_attr
;
327 lck_mtx_t
*nfs_iod_mutex
;
329 int nfs_numasync
= 0;
330 int nfs_ioddelwri
= 0;
332 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
334 static int nfsaccess_cache_timeout
= NFS_MAXATTRTIMO
;
335 /* SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
336 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
338 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
339 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
340 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
344 * the following are needed only by nfs_pageout to know how to handle errors
345 * see nfs_pageout comments on explanation of actions.
346 * the errors here are copied from errno.h and errors returned by servers
347 * are expected to match the same numbers here. If not, our actions maybe
350 enum actiontype
{NOACTION
, DUMP
, DUMPANDLOG
, RETRY
, RETRYWITHSLEEP
, SEVER
};
352 static int errorcount
[ELAST
+1]; /* better be zeros when initialized */
354 static const short errortooutcome
[ELAST
+1] = {
356 DUMP
, /* EPERM 1 Operation not permitted */
357 DUMP
, /* ENOENT 2 No such file or directory */
358 DUMPANDLOG
, /* ESRCH 3 No such process */
359 RETRY
, /* EINTR 4 Interrupted system call */
360 DUMP
, /* EIO 5 Input/output error */
361 DUMP
, /* ENXIO 6 Device not configured */
362 DUMPANDLOG
, /* E2BIG 7 Argument list too long */
363 DUMPANDLOG
, /* ENOEXEC 8 Exec format error */
364 DUMPANDLOG
, /* EBADF 9 Bad file descriptor */
365 DUMPANDLOG
, /* ECHILD 10 No child processes */
366 DUMPANDLOG
, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
367 RETRY
, /* ENOMEM 12 Cannot allocate memory */
368 DUMP
, /* EACCES 13 Permission denied */
369 DUMPANDLOG
, /* EFAULT 14 Bad address */
370 DUMPANDLOG
, /* ENOTBLK 15 POSIX - Block device required */
371 RETRY
, /* EBUSY 16 Device busy */
372 DUMP
, /* EEXIST 17 File exists */
373 DUMP
, /* EXDEV 18 Cross-device link */
374 DUMP
, /* ENODEV 19 Operation not supported by device */
375 DUMP
, /* ENOTDIR 20 Not a directory */
376 DUMP
, /* EISDIR 21 Is a directory */
377 DUMP
, /* EINVAL 22 Invalid argument */
378 DUMPANDLOG
, /* ENFILE 23 Too many open files in system */
379 DUMPANDLOG
, /* EMFILE 24 Too many open files */
380 DUMPANDLOG
, /* ENOTTY 25 Inappropriate ioctl for device */
381 DUMPANDLOG
, /* ETXTBSY 26 Text file busy - POSIX */
382 DUMP
, /* EFBIG 27 File too large */
383 DUMP
, /* ENOSPC 28 No space left on device */
384 DUMPANDLOG
, /* ESPIPE 29 Illegal seek */
385 DUMP
, /* EROFS 30 Read-only file system */
386 DUMP
, /* EMLINK 31 Too many links */
387 RETRY
, /* EPIPE 32 Broken pipe */
389 DUMPANDLOG
, /* EDOM 33 Numerical argument out of domain */
390 DUMPANDLOG
, /* ERANGE 34 Result too large */
391 RETRY
, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
392 DUMPANDLOG
, /* EINPROGRESS 36 Operation now in progress */
393 DUMPANDLOG
, /* EALREADY 37 Operation already in progress */
394 /* ipc/network software -- argument errors */
395 DUMPANDLOG
, /* ENOTSOC 38 Socket operation on non-socket */
396 DUMPANDLOG
, /* EDESTADDRREQ 39 Destination address required */
397 DUMPANDLOG
, /* EMSGSIZE 40 Message too long */
398 DUMPANDLOG
, /* EPROTOTYPE 41 Protocol wrong type for socket */
399 DUMPANDLOG
, /* ENOPROTOOPT 42 Protocol not available */
400 DUMPANDLOG
, /* EPROTONOSUPPORT 43 Protocol not supported */
401 DUMPANDLOG
, /* ESOCKTNOSUPPORT 44 Socket type not supported */
402 DUMPANDLOG
, /* ENOTSUP 45 Operation not supported */
403 DUMPANDLOG
, /* EPFNOSUPPORT 46 Protocol family not supported */
404 DUMPANDLOG
, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
405 DUMPANDLOG
, /* EADDRINUSE 48 Address already in use */
406 DUMPANDLOG
, /* EADDRNOTAVAIL 49 Can't assign requested address */
407 /* ipc/network software -- operational errors */
408 RETRY
, /* ENETDOWN 50 Network is down */
409 RETRY
, /* ENETUNREACH 51 Network is unreachable */
410 RETRY
, /* ENETRESET 52 Network dropped connection on reset */
411 RETRY
, /* ECONNABORTED 53 Software caused connection abort */
412 RETRY
, /* ECONNRESET 54 Connection reset by peer */
413 RETRY
, /* ENOBUFS 55 No buffer space available */
414 RETRY
, /* EISCONN 56 Socket is already connected */
415 RETRY
, /* ENOTCONN 57 Socket is not connected */
416 RETRY
, /* ESHUTDOWN 58 Can't send after socket shutdown */
417 RETRY
, /* ETOOMANYREFS 59 Too many references: can't splice */
418 RETRY
, /* ETIMEDOUT 60 Operation timed out */
419 RETRY
, /* ECONNREFUSED 61 Connection refused */
421 DUMPANDLOG
, /* ELOOP 62 Too many levels of symbolic links */
422 DUMP
, /* ENAMETOOLONG 63 File name too long */
423 RETRY
, /* EHOSTDOWN 64 Host is down */
424 RETRY
, /* EHOSTUNREACH 65 No route to host */
425 DUMP
, /* ENOTEMPTY 66 Directory not empty */
427 DUMPANDLOG
, /* PROCLIM 67 Too many processes */
428 DUMPANDLOG
, /* EUSERS 68 Too many users */
429 DUMPANDLOG
, /* EDQUOT 69 Disc quota exceeded */
430 /* Network File System */
431 DUMP
, /* ESTALE 70 Stale NFS file handle */
432 DUMP
, /* EREMOTE 71 Too many levels of remote in path */
433 DUMPANDLOG
, /* EBADRPC 72 RPC struct is bad */
434 DUMPANDLOG
, /* ERPCMISMATCH 73 RPC version wrong */
435 DUMPANDLOG
, /* EPROGUNAVAIL 74 RPC prog. not avail */
436 DUMPANDLOG
, /* EPROGMISMATCH 75 Program version wrong */
437 DUMPANDLOG
, /* EPROCUNAVAIL 76 Bad procedure for program */
439 DUMPANDLOG
, /* ENOLCK 77 No locks available */
440 DUMPANDLOG
, /* ENOSYS 78 Function not implemented */
441 DUMPANDLOG
, /* EFTYPE 79 Inappropriate file type or format */
442 DUMPANDLOG
, /* EAUTH 80 Authentication error */
443 DUMPANDLOG
, /* ENEEDAUTH 81 Need authenticator */
444 /* Intelligent device errors */
445 DUMPANDLOG
, /* EPWROFF 82 Device power is off */
446 DUMPANDLOG
, /* EDEVERR 83 Device error, e.g. paper out */
447 DUMPANDLOG
, /* EOVERFLOW 84 Value too large to be stored in data type */
448 /* Program loading errors */
449 DUMPANDLOG
, /* EBADEXEC 85 Bad executable */
450 DUMPANDLOG
, /* EBADARCH 86 Bad CPU type in executable */
451 DUMPANDLOG
, /* ESHLIBVERS 87 Shared library version mismatch */
452 DUMPANDLOG
, /* EBADMACHO 88 Malformed Macho file */
457 nfs_pageouterrorhandler(int error
)
462 return(errortooutcome
[error
]);
466 nfs3_access_otw(vnode_t vp
,
473 int error
= 0, attrflag
;
475 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
476 caddr_t bpos
, dpos
, cp2
;
477 register long t1
, t2
;
480 struct nfsnode
*np
= VTONFS(vp
);
484 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
);
487 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_ACCESS
]);
489 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
490 *tl
= txdr_unsigned(wmode
);
491 nfsm_request(vp
, NFSPROC_ACCESS
, p
, cred
, &xid
);
493 nfsm_postop_attr_update(vp
, 1, attrflag
, &xid
);
496 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
497 rmode
= fxdr_unsigned(u_int32_t
, *tl
);
499 np
->n_modeuid
= kauth_cred_getuid(cred
);
501 np
->n_modestamp
= now
.tv_sec
;
508 * nfs access vnode op.
509 * For nfs version 2, just return ok. File accesses may fail later.
510 * For nfs version 3, use the access rpc to check accessibility. If file modes
511 * are changed on the server, accesses might still fail later.
515 struct vnop_access_args
/* {
516 struct vnodeop_desc *a_desc;
519 vfs_context_t a_context;
522 vnode_t vp
= ap
->a_vp
;
523 int error
= 0, dorpc
;
525 int v3
= NFS_ISV3(vp
);
526 struct nfsnode
*np
= VTONFS(vp
);
531 * For nfs v3, do an access rpc, otherwise you are stuck emulating
532 * ufs_access() locally using the vattr. This may not be correct,
533 * since the server may apply other access criteria such as
534 * client uid-->server uid mapping that we do not know about, but
535 * this is better than just returning anything that is lying about
540 * Convert KAUTH primitives to NFS access rights.
543 if (vnode_isdir(vp
)) {
546 (KAUTH_VNODE_LIST_DIRECTORY
|
547 KAUTH_VNODE_READ_EXTATTRIBUTES
))
548 mode
|= NFSV3ACCESS_READ
;
549 if (ap
->a_action
& KAUTH_VNODE_SEARCH
)
550 mode
|= NFSV3ACCESS_LOOKUP
;
552 (KAUTH_VNODE_ADD_FILE
|
553 KAUTH_VNODE_ADD_SUBDIRECTORY
))
554 mode
|= NFSV3ACCESS_MODIFY
| NFSV3ACCESS_EXTEND
;
555 if (ap
->a_action
& KAUTH_VNODE_DELETE_CHILD
)
556 mode
|= NFSV3ACCESS_MODIFY
;
560 (KAUTH_VNODE_READ_DATA
|
561 KAUTH_VNODE_READ_EXTATTRIBUTES
))
562 mode
|= NFSV3ACCESS_READ
;
563 if (ap
->a_action
& KAUTH_VNODE_WRITE_DATA
)
564 mode
|= NFSV3ACCESS_MODIFY
| NFSV3ACCESS_EXTEND
;
565 if (ap
->a_action
& KAUTH_VNODE_APPEND_DATA
)
566 mode
|= NFSV3ACCESS_EXTEND
;
567 if (ap
->a_action
& KAUTH_VNODE_EXECUTE
)
568 mode
|= NFSV3ACCESS_EXECUTE
;
571 if (ap
->a_action
& KAUTH_VNODE_DELETE
)
572 mode
|= NFSV3ACCESS_DELETE
;
574 (KAUTH_VNODE_WRITE_ATTRIBUTES
|
575 KAUTH_VNODE_WRITE_EXTATTRIBUTES
|
576 KAUTH_VNODE_WRITE_SECURITY
))
577 mode
|= NFSV3ACCESS_MODIFY
;
578 /* XXX this is pretty dubious */
579 if (ap
->a_action
& KAUTH_VNODE_CHANGE_OWNER
)
580 mode
|= NFSV3ACCESS_MODIFY
;
582 /* if caching, always ask for every right */
583 if (nfsaccess_cache_timeout
> 0) {
584 wmode
= NFSV3ACCESS_READ
| NFSV3ACCESS_MODIFY
|
585 NFSV3ACCESS_EXTEND
| NFSV3ACCESS_EXECUTE
|
586 NFSV3ACCESS_DELETE
| NFSV3ACCESS_LOOKUP
;
590 cred
= vfs_context_ucred(ap
->a_context
);
593 * Does our cached result allow us to give a definite yes to
597 if (NMODEVALID(np
)) {
599 if ((now
.tv_sec
< (np
->n_modestamp
+ nfsaccess_cache_timeout
)) &&
600 (kauth_cred_getuid(cred
) == np
->n_modeuid
) &&
601 ((np
->n_mode
& mode
) == mode
)) {
602 /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_hits); */
607 /* Either a no, or a don't know. Go to the wire. */
608 /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_misses); */
609 error
= nfs3_access_otw(vp
, wmode
, vfs_context_proc(ap
->a_context
), cred
);
613 * If we asked for DELETE but didn't get it, the server
614 * may simply not support returning that bit (possible
615 * on UNIX systems). So, we'll assume that it is OK,
616 * and just let any subsequent delete action fail if it
617 * really isn't deletable.
619 if ((mode
& NFSV3ACCESS_DELETE
) &&
620 !(np
->n_mode
& NFSV3ACCESS_DELETE
))
621 np
->n_mode
|= NFSV3ACCESS_DELETE
;
622 if ((np
->n_mode
& mode
) != mode
)
627 if ((ap
->a_action
& KAUTH_VNODE_WRITE_RIGHTS
) && vfs_isrdonly(vnode_mount(vp
))) {
639 * Check to see if the type is ok
640 * and that deletion is not in progress.
641 * For paged in text files, you will need to flush the page cache
642 * if consistency is lost.
648 struct vnop_open_args
/* {
649 struct vnodeop_desc *a_desc;
652 vfs_context_t a_context;
655 vnode_t vp
= ap
->a_vp
;
656 struct nfsnode
*np
= VTONFS(vp
);
657 struct nfs_vattr nvattr
;
663 vtype
= vnode_vtype(vp
);
664 if (vtype
!= VREG
&& vtype
!= VDIR
&& vtype
!= VLNK
) {
668 cred
= vfs_context_ucred(ap
->a_context
);
669 p
= vfs_context_proc(ap
->a_context
);
671 if (np
->n_flag
& NNEEDINVALIDATE
) {
672 np
->n_flag
&= ~NNEEDINVALIDATE
;
673 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
675 if (np
->n_flag
& NMODIFIED
) {
676 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1)) == EINTR
)
680 np
->n_direofoffset
= 0;
681 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
685 /* if directory changed, purge any name cache entries */
686 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=))
688 np
->n_ncmtime
= nvattr
.nva_mtime
;
690 np
->n_mtime
= nvattr
.nva_mtime
;
692 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
695 if (nfstimespeccmp(&np
->n_mtime
, &nvattr
.nva_mtime
, !=)) {
697 np
->n_direofoffset
= 0;
699 /* purge name cache entries */
700 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=))
703 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1)) == EINTR
)
706 np
->n_ncmtime
= nvattr
.nva_mtime
;
707 np
->n_mtime
= nvattr
.nva_mtime
;
710 NATTRINVALIDATE(np
); /* For Open/Close consistency */
716 * What an NFS client should do upon close after writing is a debatable issue.
717 * Most NFS clients push delayed writes to the server upon close, basically for
719 * 1 - So that any write errors may be reported back to the client process
720 * doing the close system call. By far the two most likely errors are
721 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
722 * 2 - To put a worst case upper bound on cache inconsistency between
723 * multiple clients for the file.
724 * There is also a consistency problem for Version 2 of the protocol w.r.t.
725 * not being able to tell if other clients are writing a file concurrently,
726 * since there is no way of knowing if the changed modify time in the reply
727 * is only due to the write for this client.
728 * (NFS Version 3 provides weak cache consistency data in the reply that
729 * should be sufficient to detect and handle this case.)
731 * The current code does the following:
732 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
733 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
739 struct vnop_close_args
/* {
740 struct vnodeop_desc *a_desc;
743 vfs_context_t a_context;
746 vnode_t vp
= ap
->a_vp
;
747 struct nfsnode
*np
= VTONFS(vp
);
748 struct nfsmount
*nmp
;
753 cred
= vfs_context_ucred(ap
->a_context
);
754 p
= vfs_context_proc(ap
->a_context
);
756 if (vnode_vtype(vp
) == VREG
) {
758 register struct sillyrename
*sp
= np
->n_sillyrename
;
760 kprintf("nfs_close: %s, dvp=%x, vp=%x, ap=%x, np=%x, sp=%x\n",
761 &sp
->s_name
[0], (unsigned)(sp
->s_dvp
), (unsigned)vp
,
762 (unsigned)ap
, (unsigned)np
, (unsigned)sp
);
764 nmp
= VFSTONFS(vnode_mount(vp
));
767 if (np
->n_flag
& NNEEDINVALIDATE
) {
768 np
->n_flag
&= ~NNEEDINVALIDATE
;
769 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
771 if (np
->n_flag
& NMODIFIED
) {
773 error
= nfs_flush(vp
, MNT_WAIT
, cred
, p
, 0);
775 * We cannot clear the NMODIFIED bit in np->n_flag due to
776 * potential races with other processes
777 * NMODIFIED is a hint
779 /* np->n_flag &= ~NMODIFIED; */
781 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
785 if (np
->n_flag
& NWRITEERR
) {
786 np
->n_flag
&= ~NWRITEERR
;
795 nfs_getattr_no_vnode(
801 struct nfs_vattr
*nvap
,
804 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
809 struct nfsmount
*nmp
= VFSTONFS(mp
);
810 int v3
= (nmp
->nm_flag
& NFSMNT_NFSV3
);
814 // XXX fix this to use macros once the macros get cleaned up
815 //nfsm_reqhead(NFSX_FH(v3));
818 if (hsiz
>= nfs_mbuf_minclsize
)
819 error
= mbuf_mclget(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mb
);
821 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mb
);
824 bpos
= mbuf_data(mb
);
826 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_GETATTR
]);
827 //nfsm_fhtom(vp, v3);
829 t2
= nfsm_rndup(fhsize
) + NFSX_UNSIGNED
;
830 if (t2
<= mbuf_trailingspace(mb
)) {
831 nfsm_build(tl
, u_long
*, t2
);
832 *tl
++ = txdr_unsigned(fhsize
);
833 *(tl
+ ((t2
>>2) - 2)) = 0;
834 bcopy((caddr_t
)fhp
,(caddr_t
)tl
, fhsize
);
835 } else if ((t2
= nfsm_strtmbuf(&mb
, &bpos
, (caddr_t
)fhp
, fhsize
))) {
841 nfsm_build(cp
, caddr_t
, NFSX_V2FH
);
842 bcopy((caddr_t
)fhp
, cp
, NFSX_V2FH
);
844 //nfsm_request(vp, NFSPROC_GETATTR, p, cred, xidp);
845 if ((error
= nfs_request(NULL
, mp
, mreq
, NFSPROC_GETATTR
, p
, cred
, &mrep
, &md
, &dpos
, xidp
))) {
846 if (error
& NFSERR_RETERR
)
847 error
&= ~NFSERR_RETERR
;
852 //nfsm_loadattr(vp, nvap, xidp);
853 error
= nfs_parsefattr(&md
, &dpos
, v3
, nvap
);
864 * nfs getattr call from vfs.
869 struct nfs_vattr
*nvap
,
873 struct nfsnode
*np
= VTONFS(vp
);
879 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
884 FSDBG_TOP(513, np
->n_size
, np
, np
->n_vattr
.nva_size
, np
->n_flag
);
887 * Update local times for special files.
889 if (np
->n_flag
& (NACC
| NUPD
))
892 * First look in the cache.
894 if ((error
= nfs_getattrcache(vp
, nvap
)) == 0) {
895 FSDBG_BOT(513, np
->n_size
, 0, np
->n_vattr
.nva_size
, np
->n_flag
);
898 if (error
!= ENOENT
) {
899 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
,
904 if (!VFSTONFS(vnode_mount(vp
))) {
905 FSDBG_BOT(513, np
->n_size
, ENXIO
, np
->n_vattr
.nva_size
, np
->n_flag
);
912 * Try to get both the attributes and access info by making an
913 * ACCESS call and seeing if it returns updated attributes.
914 * But don't bother if we aren't caching access info or if the
915 * attributes returned wouldn't be cached.
917 if (v3
&& (nfsaccess_cache_timeout
> 0) &&
918 (nfs_attrcachetimeout(vp
) > 0)) {
919 /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_misses); */
920 if ((error
= nfs3_access_otw(vp
, NFSV3ACCESS_ALL
, p
, cred
)))
922 if ((error
= nfs_getattrcache(vp
, nvap
)) == 0)
930 nfsm_reqhead(NFSX_FH(v3
));
932 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
, np
->n_flag
);
935 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_GETATTR
]);
937 nfsm_request(vp
, NFSPROC_GETATTR
, p
, cred
, &xid
);
939 nfsm_loadattr(vp
, v3
, nvap
, &xid
);
940 if (!xid
) { /* out-of-order rpc - attributes were dropped */
943 FSDBG(513, -1, np
, np
->n_xid
<< 32, np
->n_xid
);
944 if (avoidfloods
++ < 100)
947 * avoidfloods>1 is bizarre. at 100 pull the plug
949 panic("nfs_getattr: getattr flood\n");
951 if (nfstimespeccmp(&np
->n_mtime
, &nvap
->nva_mtime
, !=)) {
952 enum vtype vtype
= vnode_vtype(vp
);
953 FSDBG(513, -1, np
, -1, vp
);
956 /* purge name cache entries */
957 if (nfstimespeccmp(&np
->n_ncmtime
, &nvap
->nva_mtime
, !=))
960 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
961 FSDBG(513, -1, np
, -2, error
);
964 np
->n_ncmtime
= nvap
->nva_mtime
;
965 np
->n_mtime
= nvap
->nva_mtime
;
971 FSDBG_BOT(513, np
->n_size
, -1, np
->n_vattr
.nva_size
, error
);
978 struct vnop_getattr_args
/* {
979 struct vnodeop_desc *a_desc;
981 struct vnode_attr *a_vap;
982 vfs_context_t a_context;
986 struct nfs_vattr nva
;
987 struct vnode_attr
*vap
= ap
->a_vap
;
989 error
= nfs_getattr(ap
->a_vp
, &nva
,
990 vfs_context_ucred(ap
->a_context
),
991 vfs_context_proc(ap
->a_context
));
995 /* copy nva to *a_vap */
996 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
997 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
998 VATTR_RETURN(vap
, va_rdev
, nva
.nva_rdev
);
999 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1000 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1001 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1002 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1003 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1004 VATTR_RETURN(vap
, va_data_alloc
, nva
.nva_bytes
);
1005 VATTR_RETURN(vap
, va_iosize
, nva
.nva_blocksize
); /* should this just be f_iosize? */
1006 VATTR_RETURN(vap
, va_fsid
, nva
.nva_fsid
);
1007 vap
->va_access_time
.tv_sec
= nva
.nva_atime
.tv_sec
;
1008 vap
->va_access_time
.tv_nsec
= nva
.nva_atime
.tv_nsec
;
1009 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1010 vap
->va_modify_time
.tv_sec
= nva
.nva_mtime
.tv_sec
;
1011 vap
->va_modify_time
.tv_nsec
= nva
.nva_mtime
.tv_nsec
;
1012 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1013 vap
->va_change_time
.tv_sec
= nva
.nva_ctime
.tv_sec
;
1014 vap
->va_change_time
.tv_nsec
= nva
.nva_ctime
.tv_nsec
;
1015 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1025 struct vnop_setattr_args
/* {
1026 struct vnodeop_desc *a_desc;
1028 struct vnode_attr *a_vap;
1029 vfs_context_t a_context;
1032 vnode_t vp
= ap
->a_vp
;
1033 struct nfsnode
*np
= VTONFS(vp
);
1034 struct vnode_attr
*vap
= ap
->a_vap
;
1041 tsize
= (u_quad_t
)0;
1044 /* Setting of flags is not supported. */
1045 if (VATTR_IS_ACTIVE(vap
, va_flags
))
1048 cred
= vfs_context_ucred(ap
->a_context
);
1049 p
= vfs_context_proc(ap
->a_context
);
1051 VATTR_SET_SUPPORTED(vap
, va_mode
);
1052 VATTR_SET_SUPPORTED(vap
, va_uid
);
1053 VATTR_SET_SUPPORTED(vap
, va_gid
);
1054 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1055 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1056 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1058 /* Disallow write attempts if the filesystem is mounted read-only. */
1059 if ((VATTR_IS_ACTIVE(vap
, va_flags
) || VATTR_IS_ACTIVE(vap
, va_mode
) ||
1060 VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
1061 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1062 VATTR_IS_ACTIVE(vap
, va_modify_time
)) &&
1063 vnode_vfsisrdonly(vp
))
1066 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1067 switch (vnode_vtype(vp
)) {
1074 if (!VATTR_IS_ACTIVE(vap
, va_modify_time
) &&
1075 !VATTR_IS_ACTIVE(vap
, va_access_time
) &&
1076 !VATTR_IS_ACTIVE(vap
, va_mode
) &&
1077 !VATTR_IS_ACTIVE(vap
, va_uid
) &&
1078 !VATTR_IS_ACTIVE(vap
, va_gid
))
1080 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1084 * Disallow write attempts if the filesystem is
1085 * mounted read-only.
1087 if (vnode_vfsisrdonly(vp
))
1089 FSDBG_TOP(512, np
->n_size
, vap
->va_data_size
,
1090 np
->n_vattr
.nva_size
, np
->n_flag
);
1091 if (np
->n_flag
& NMODIFIED
) {
1092 if (vap
->va_data_size
== 0)
1093 error
= nfs_vinvalbuf(vp
, 0, cred
, p
, 1);
1095 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1097 printf("nfs_setattr: nfs_vinvalbuf %d\n", error
);
1098 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
,
1099 np
->n_vattr
.nva_size
, -1);
1102 } else if (np
->n_size
> vap
->va_data_size
) { /* shrinking? */
1104 int biosize
, neweofoff
, mustwrite
;
1107 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
1108 obn
= (np
->n_size
- 1) / biosize
;
1109 bn
= vap
->va_data_size
/ biosize
;
1110 for ( ; obn
>= bn
; obn
--) {
1111 if (!nfs_buf_is_incore(vp
, obn
))
1113 error
= nfs_buf_get(vp
, obn
, biosize
, 0, NBLK_READ
, &bp
);
1117 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1118 SET(bp
->nb_flags
, NB_INVAL
);
1119 nfs_buf_release(bp
, 1);
1123 neweofoff
= vap
->va_data_size
- NBOFF(bp
);
1124 /* check for any dirty data before the new EOF */
1125 if (bp
->nb_dirtyend
&& bp
->nb_dirtyoff
< neweofoff
) {
1126 /* clip dirty range to EOF */
1127 if (bp
->nb_dirtyend
> neweofoff
)
1128 bp
->nb_dirtyend
= neweofoff
;
1131 bp
->nb_dirty
&= (1 << round_page_32(neweofoff
)/PAGE_SIZE
) - 1;
1135 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1136 SET(bp
->nb_flags
, NB_INVAL
);
1137 nfs_buf_release(bp
, 1);
1140 /* gotta write out dirty data before invalidating */
1141 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1142 /* (NB_NOCACHE indicates buffer should be discarded) */
1143 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
| NB_READ
));
1144 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
1145 if (bp
->nb_wcred
== NOCRED
) {
1146 kauth_cred_ref(cred
);
1147 bp
->nb_wcred
= cred
;
1149 error
= nfs_buf_write(bp
);
1150 // Note: bp has been released
1152 FSDBG(512, bp
, 0xd00dee, 0xbad, error
);
1153 np
->n_error
= error
;
1154 np
->n_flag
|= NWRITEERR
;
1156 * There was a write error and we need to
1157 * invalidate attrs and flush buffers in
1158 * order to sync up with the server.
1159 * (if this write was extending the file,
1160 * we may no longer know the correct size)
1162 NATTRINVALIDATE(np
);
1163 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
1169 np
->n_size
= np
->n_vattr
.nva_size
= vap
->va_data_size
;
1170 ubc_setsize(vp
, (off_t
)vap
->va_data_size
); /* XXX error? */
1172 } else if ((VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1173 VATTR_IS_ACTIVE(vap
, va_access_time
)) &&
1174 (np
->n_flag
& NMODIFIED
) && (vnode_vtype(vp
) == VREG
)) {
1175 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1179 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
1180 NMODEINVALIDATE(np
);
1182 error
= nfs_setattrrpc(vp
, vap
, cred
, p
);
1183 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, error
);
1184 if (error
&& VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1185 /* make every effort to resync file size w/ server... */
1186 int err
; /* preserve "error" for return */
1188 np
->n_size
= np
->n_vattr
.nva_size
= tsize
;
1189 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX check error */
1190 vap
->va_data_size
= tsize
;
1191 err
= nfs_setattrrpc(vp
, vap
, cred
, p
);
1192 printf("nfs_setattr: nfs_setattrrpc %d %d\n", error
, err
);
1198 * Do an nfs setattr rpc.
1201 nfs_setattrrpc(vp
, vap
, cred
, procp
)
1203 struct vnode_attr
*vap
;
1207 register struct nfsv2_sattr
*sp
;
1208 register caddr_t cp
;
1209 register long t1
, t2
;
1210 caddr_t bpos
, dpos
, cp2
;
1212 int error
= 0, wccpostattr
= 0;
1213 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
1218 if (!VFSTONFS(vnode_mount(vp
)))
1222 nfsm_reqhead(NFSX_FH(v3
) + NFSX_SATTR(v3
));
1225 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_SETATTR
]);
1228 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
1229 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1231 *tl
= txdr_unsigned(vap
->va_mode
);
1233 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1236 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
1237 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1239 *tl
= txdr_unsigned(vap
->va_uid
);
1241 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1244 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
1245 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1247 *tl
= txdr_unsigned(vap
->va_gid
);
1249 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1252 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1253 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1255 txdr_hyper(&vap
->va_data_size
, tl
);
1257 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1261 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1262 if (vap
->va_access_time
.tv_sec
!= now
.tv_sec
) {
1263 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1264 *tl
++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT
);
1265 txdr_nfsv3time(&vap
->va_access_time
, tl
);
1267 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1268 *tl
= txdr_unsigned(NFSV3SATTRTIME_TOSERVER
);
1271 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1272 *tl
= txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE
);
1274 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1275 if (vap
->va_modify_time
.tv_sec
!= now
.tv_sec
) {
1276 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1277 *tl
++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT
);
1278 txdr_nfsv3time(&vap
->va_modify_time
, tl
);
1280 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1281 *tl
= txdr_unsigned(NFSV3SATTRTIME_TOSERVER
);
1284 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1285 *tl
= txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE
);
1287 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1290 struct timespec neg1time
= { -1, -1 };
1291 nfsm_build(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
1292 if (VATTR_IS_ACTIVE(vap
, va_mode
))
1293 sp
->sa_mode
= vtonfsv2_mode(vnode_vtype(vp
), vap
->va_mode
);
1295 sp
->sa_mode
= nfs_xdrneg1
;
1296 if (VATTR_IS_ACTIVE(vap
, va_uid
))
1297 sp
->sa_uid
= txdr_unsigned(vap
->va_uid
);
1299 sp
->sa_uid
= nfs_xdrneg1
;
1300 if (VATTR_IS_ACTIVE(vap
, va_gid
))
1301 sp
->sa_gid
= txdr_unsigned(vap
->va_gid
);
1303 sp
->sa_gid
= nfs_xdrneg1
;
1304 if (VATTR_IS_ACTIVE(vap
, va_data_size
))
1305 sp
->sa_size
= txdr_unsigned(vap
->va_data_size
);
1307 sp
->sa_size
= nfs_xdrneg1
;
1308 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1309 txdr_nfsv2time(&vap
->va_access_time
, &sp
->sa_atime
);
1311 txdr_nfsv2time(&neg1time
, &sp
->sa_atime
);
1313 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1314 txdr_nfsv2time(&vap
->va_modify_time
, &sp
->sa_mtime
);
1316 txdr_nfsv2time(&neg1time
, &sp
->sa_mtime
);
1319 nfsm_request(vp
, NFSPROC_SETATTR
, procp
, cred
, &xid
);
1321 struct timespec premtime
= { 0, 0 };
1323 nfsm_wcc_data(vp
, &premtime
, wccpostattr
, &xid
);
1325 /* if file hadn't changed, update cached mtime */
1326 if (nfstimespeccmp(&VTONFS(vp
)->n_mtime
, &premtime
, ==)) {
1327 VTONFS(vp
)->n_mtime
= VTONFS(vp
)->n_vattr
.nva_mtime
;
1329 /* if directory hadn't changed, update namecache mtime */
1330 if ((vnode_vtype(vp
) == VDIR
) &&
1331 nfstimespeccmp(&VTONFS(vp
)->n_ncmtime
, &premtime
, ==)) {
1332 VTONFS(vp
)->n_ncmtime
= VTONFS(vp
)->n_vattr
.nva_mtime
;
1335 NATTRINVALIDATE(VTONFS(vp
));
1338 nfsm_loadattr(vp
, v3
, NULL
, &xid
);
1346 * nfs lookup call, one step at a time...
1347 * First look in cache
1348 * If not found, unlock the directory nfsnode and do the rpc
1352 struct vnop_lookup_args
/* {
1353 struct vnodeop_desc *a_desc;
1356 struct componentname *a_cnp;
1357 vfs_context_t a_context;
1360 struct componentname
*cnp
= ap
->a_cnp
;
1361 vnode_t dvp
= ap
->a_dvp
;
1362 vnode_t
*vpp
= ap
->a_vpp
;
1363 int flags
= cnp
->cn_flags
;
1368 caddr_t bpos
, dpos
, cp2
;
1369 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
1372 struct nfsnode
*dnp
, *np
;
1373 int wantparent
, error
, attrflag
, dattrflag
, fhsize
, fhisdvp
;
1374 int v3
= NFS_ISV3(dvp
);
1375 u_int64_t xid
, dxid
;
1376 struct nfs_vattr nvattr
;
1383 cred
= vfs_context_ucred(ap
->a_context
);
1384 p
= vfs_context_proc(ap
->a_context
);
1386 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
1389 error
= nfs_getattr(dvp
, &nvattr
, cred
, p
);
1392 if (nfstimespeccmp(&dnp
->n_ncmtime
, &nvattr
.nva_mtime
, !=)) {
1394 * This directory has changed on us.
1395 * Purge any name cache entries.
1398 dnp
->n_ncmtime
= nvattr
.nva_mtime
;
1401 error
= cache_lookup(dvp
, vpp
, cnp
);
1404 /* negative cache entry same as cache miss */
1411 /* cache hit, not really an error */
1413 struct vnop_access_args naa
;
1415 OSAddAtomic(1, (SInt32
*)&nfsstats
.lookupcache_hits
);
1417 /* check for directory access */
1419 naa
.a_action
= KAUTH_VNODE_SEARCH
;
1420 naa
.a_context
= ap
->a_context
;
1422 /* compute actual success/failure based on accessibility */
1423 error
= nfs_access(&naa
);
1427 /* unexpected error from cache_lookup */
1431 /* check for lookup of "." */
1432 if ((cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_namelen
== 1)) {
1433 /* skip lookup, we know who we are */
1441 /* do we know this name is too long? */
1443 /* For NFSv3: need uniform pathconf info to test pc_namemax */
1444 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(dvp
));
1449 if (((nmp
->nm_state
& (NFSSTA_GOTFSINFO
|NFSSTA_GOTPATHCONF
)) ==
1450 (NFSSTA_GOTFSINFO
|NFSSTA_GOTPATHCONF
)) &&
1451 (nmp
->nm_fsinfo
.fsproperties
& NFSV3FSINFO_HOMOGENEOUS
) &&
1452 (cnp
->cn_namelen
> (long)nmp
->nm_fsinfo
.namemax
)) {
1453 error
= ENAMETOOLONG
;
1456 } else if (cnp
->cn_namelen
> NFS_MAXNAMLEN
) {
1457 error
= ENAMETOOLONG
;
1464 OSAddAtomic(1, (SInt32
*)&nfsstats
.lookupcache_misses
);
1465 len
= cnp
->cn_namelen
;
1466 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
+ nfsm_rndup(len
));
1469 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_LOOKUP
]);
1470 nfsm_fhtom(dvp
, v3
);
1471 nfsm_strtom(cnp
->cn_nameptr
, len
, NFS_MAXNAMLEN
, v3
);
1472 /* nfsm_request for NFSv2 causes you to goto to nfsmout upon errors */
1473 nfsm_request(dvp
, NFSPROC_LOOKUP
, p
, cred
, &xid
);
1477 nfsm_postop_attr_update(dvp
, v3
, dattrflag
, &xid
);
1483 /* get the filehandle */
1484 nfsm_getfh(fhp
, fhsize
, v3
);
1485 /* is the file handle the same as this directory's file handle? */
1486 fhisdvp
= NFS_CMPFH(dnp
, fhp
, fhsize
);
1488 /* get attributes */
1491 nfsm_postop_attr_get(v3
, attrflag
, &nvattr
);
1492 nfsm_postop_attr_update(dvp
, v3
, dattrflag
, &dxid
);
1493 if (!attrflag
&& (!fhisdvp
|| !dattrflag
)) {
1494 /* We need valid attributes in order */
1495 /* to call nfs_nget/vnode_create(). */
1496 error
= nfs_getattr_no_vnode(vnode_mount(dvp
),
1497 fhp
, fhsize
, cred
, p
, &nvattr
, &xid
);
1504 nfsm_attr_get(v3
, &nvattr
);
1510 * Handle RENAME case...
1512 if (cnp
->cn_nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
1518 error
= nfs_nget(vnode_mount(dvp
), dvp
, cnp
, fhp
, fhsize
,
1519 &nvattr
, &xid
, 0, &np
);
1530 if ((cnp
->cn_flags
& MAKEENTRY
) &&
1531 (cnp
->cn_nameiop
!= DELETE
|| !(flags
& ISLASTCN
)))
1532 ngflags
= NG_MAKEENTRY
;
1537 error
= vnode_get(dvp
);
1543 /* test fhp to see if we have valid attributes in nvattr */
1544 if (fhp
&& (dnp
->n_xid
<= xid
)) {
1545 error
= nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
1553 error
= nfs_nget(vnode_mount(dvp
), dvp
, cnp
, fhp
, fhsize
,
1554 &nvattr
, &xid
, ngflags
, &np
);
1562 // if (error == 0 && *vpp != NULL && *vpp != dvp)
1563 // nfs_unlock(VTONFS(*vpp));
1567 if ((cnp
->cn_nameiop
== CREATE
|| cnp
->cn_nameiop
== RENAME
) &&
1568 (flags
& ISLASTCN
) && error
== ENOENT
) {
1569 if (vnode_mount(dvp
) && vnode_vfsisrdonly(dvp
))
1572 error
= EJUSTRETURN
;
1576 if (error
&& *vpp
) {
1585 * Just call nfs_bioread() to do the work.
1589 struct vnop_read_args
/* {
1590 struct vnodeop_desc *a_desc;
1594 vfs_context_t a_context;
1597 if (vnode_vtype(ap
->a_vp
) != VREG
)
1599 return (nfs_bioread(ap
->a_vp
, ap
->a_uio
, ap
->a_ioflag
,
1600 vfs_context_ucred(ap
->a_context
),
1601 vfs_context_proc(ap
->a_context
)));
1610 struct vnop_readlink_args
/* {
1611 struct vnodeop_desc *a_desc;
1614 vfs_context_t a_context;
1617 if (vnode_vtype(ap
->a_vp
) != VLNK
)
1619 return (nfs_bioread(ap
->a_vp
, ap
->a_uio
, 0,
1620 vfs_context_ucred(ap
->a_context
),
1621 vfs_context_proc(ap
->a_context
)));
1625 * Do a readlink rpc.
1626 * Called by nfs_doio() from below the buffer cache.
1635 register u_long
*tl
;
1636 register caddr_t cp
;
1637 register long t1
, t2
;
1638 caddr_t bpos
, dpos
, cp2
;
1639 int error
= 0, len
, attrflag
;
1640 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
1644 if (!VFSTONFS(vnode_mount(vp
)))
1648 nfsm_reqhead(NFSX_FH(v3
));
1651 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_READLINK
]);
1653 nfsm_request(vp
, NFSPROC_READLINK
, p
, cred
, &xid
);
1655 nfsm_postop_attr_update(vp
, v3
, attrflag
, &xid
);
1657 nfsm_strsiz(len
, NFS_MAXPATHLEN
, v3
);
1658 if (len
>= NFS_MAXPATHLEN
) {
1659 struct nfsnode
*np
= VTONFS(vp
);
1662 panic("nfs_readlinkrpc: null np");
1664 if (np
->n_size
&& np
->n_size
< NFS_MAXPATHLEN
)
1667 nfsm_mtouio(uiop
, len
);
1684 register u_long
*tl
;
1685 register caddr_t cp
;
1686 register long t1
, t2
;
1687 caddr_t bpos
, dpos
, cp2
;
1688 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
1689 struct nfsmount
*nmp
;
1690 int error
= 0, len
, retlen
, tsiz
, eof
= 0, attrflag
;
1694 FSDBG_TOP(536, vp
, uiop
->uio_offset
, uio_uio_resid(uiop
), 0);
1695 nmp
= VFSTONFS(vnode_mount(vp
));
1699 nmrsize
= nmp
->nm_rsize
;
1701 // LP64todo - fix this
1702 tsiz
= uio_uio_resid(uiop
);
1703 if (((u_int64_t
)uiop
->uio_offset
+ (unsigned int)tsiz
> 0xffffffff) && !v3
) {
1704 FSDBG_BOT(536, vp
, uiop
->uio_offset
, uio_uio_resid(uiop
), EFBIG
);
1708 len
= (tsiz
> nmrsize
) ? nmrsize
: tsiz
;
1709 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
* 3);
1712 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_READ
]);
1714 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
* 3);
1716 txdr_hyper(&uiop
->uio_offset
, tl
);
1717 *(tl
+ 2) = txdr_unsigned(len
);
1719 *tl
++ = txdr_unsigned(uiop
->uio_offset
);
1720 *tl
++ = txdr_unsigned(len
);
1723 FSDBG(536, vp
, uiop
->uio_offset
, len
, 0);
1724 nfsm_request(vp
, NFSPROC_READ
, p
, cred
, &xid
);
1727 nfsm_postop_attr_update(vp
, v3
, attrflag
, &xid
);
1733 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1734 eof
= fxdr_unsigned(int, *(tl
+ 1));
1737 nfsm_loadattr(vp
, v3
, NULL
, &xid
);
1741 nfsm_strsiz(retlen
, nmrsize
, 0);
1742 nfsm_mtouio(uiop
, retlen
);
1749 if (eof
|| retlen
== 0)
1751 } else if (retlen
< len
)
1755 FSDBG_BOT(536, vp
, eof
, uio_uio_resid(uiop
), error
);
1771 register u_long
*tl
;
1772 register caddr_t cp
;
1773 register int t1
, t2
, backup
;
1774 caddr_t bpos
, dpos
, cp2
;
1775 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
1776 struct nfsmount
*nmp
;
1777 int error
= 0, len
, tsiz
, updatemtime
= 0, wccpostattr
= 0, rlen
, commit
;
1778 int v3
, committed
= NFSV3WRITE_FILESYNC
;
1783 if (uiop
->uio_iovcnt
!= 1)
1784 panic("nfs_writerpc: iovcnt > 1");
1786 FSDBG_TOP(537, vp
, uiop
->uio_offset
, uio_uio_resid(uiop
), *iomode
);
1787 nmp
= VFSTONFS(vnode_mount(vp
));
1792 // LP64todo - fix this
1793 tsiz
= uio_uio_resid(uiop
);
1794 if (((u_int64_t
)uiop
->uio_offset
+ (unsigned int)tsiz
> 0xffffffff) && !v3
) {
1795 FSDBG_BOT(537, vp
, uiop
->uio_offset
, uio_uio_resid(uiop
), EFBIG
);
1799 nmp
= VFSTONFS(vnode_mount(vp
));
1804 len
= (tsiz
> nmp
->nm_wsize
) ? nmp
->nm_wsize
: tsiz
;
1805 nfsm_reqhead(NFSX_FH(v3
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
1808 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_WRITE
]);
1811 nfsm_build(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1812 txdr_hyper(&uiop
->uio_offset
, tl
);
1814 *tl
++ = txdr_unsigned(len
);
1815 *tl
++ = txdr_unsigned(*iomode
);
1817 nfsm_build(tl
, u_long
*, 4 * NFSX_UNSIGNED
);
1818 *++tl
= txdr_unsigned(uiop
->uio_offset
);
1821 *tl
= txdr_unsigned(len
);
1822 FSDBG(537, vp
, uiop
->uio_offset
, len
, 0);
1823 nfsm_uiotom(uiop
, len
);
1824 nfsm_request(vp
, NFSPROC_WRITE
, p
, cred
, &xid
);
1825 nmp
= VFSTONFS(vnode_mount(vp
));
1830 struct timespec premtime
;
1831 nfsm_wcc_data(vp
, &premtime
, wccpostattr
, &xid
);
1832 if (nfstimespeccmp(&VTONFS(vp
)->n_mtime
, &premtime
, ==))
1836 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
+
1838 rlen
= fxdr_unsigned(int, *tl
++);
1842 } else if (rlen
< len
) {
1843 backup
= len
- rlen
;
1844 uio_iov_base_add(uiop
, -backup
);
1845 uio_iov_len_add(uiop
, backup
);
1846 uiop
->uio_offset
-= backup
;
1847 uio_uio_resid_add(uiop
, backup
);
1850 commit
= fxdr_unsigned(int, *tl
++);
1853 * Return the lowest committment level
1854 * obtained by any of the RPCs.
1856 if (committed
== NFSV3WRITE_FILESYNC
)
1858 else if (committed
== NFSV3WRITE_DATASYNC
&&
1859 commit
== NFSV3WRITE_UNSTABLE
)
1861 if ((nmp
->nm_state
& NFSSTA_HASWRITEVERF
) == 0) {
1862 bcopy((caddr_t
)tl
, (caddr_t
)nmp
->nm_verf
,
1864 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
1865 } else if (bcmp((caddr_t
)tl
,
1866 (caddr_t
)nmp
->nm_verf
, NFSX_V3WRITEVERF
)) {
1868 bcopy((caddr_t
)tl
, (caddr_t
)nmp
->nm_verf
,
1874 nfsm_loadattr(vp
, v3
, NULL
, &xid
);
1879 VTONFS(vp
)->n_mtime
= VTONFS(vp
)->n_vattr
.nva_mtime
;
1882 * we seem to have a case where we end up looping on shutdown
1883 * and taking down nfs servers. For V3, error cases, there is
1884 * no way to terminate loop, if the len was 0, meaning,
1885 * nmp->nm_wsize was trashed. FreeBSD has this fix in it.
1893 if ((mp
= vnode_mount(vp
)) && (vfs_flags(mp
) & MNT_ASYNC
))
1894 committed
= NFSV3WRITE_FILESYNC
;
1895 *iomode
= committed
;
1897 uio_uio_resid_set(uiop
, tsiz
);
1898 FSDBG_BOT(537, vp
, committed
, uio_uio_resid(uiop
), error
);
1904 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1905 * mode set to specify the file type and the size field for rdev.
1911 struct componentname
*cnp
,
1912 struct vnode_attr
*vap
,
1916 register struct nfsv2_sattr
*sp
;
1917 register u_long
*tl
;
1918 register caddr_t cp
;
1919 register long t1
, t2
;
1920 vnode_t newvp
= (vnode_t
)0;
1921 struct nfsnode
*np
= (struct nfsnode
*)0;
1922 struct nfs_vattr nvattr
;
1925 int error
= 0, wccpostattr
= 0, gotvp
= 0;
1926 struct timespec premtime
= { 0, 0 };
1927 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
1930 int v3
= NFS_ISV3(dvp
);
1933 if (!VATTR_IS_ACTIVE(vap
, va_type
))
1935 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
1936 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
1938 rdev
= txdr_unsigned(vap
->va_rdev
);
1939 } else if (vap
->va_type
== VFIFO
|| vap
->va_type
== VSOCK
)
1944 nfsm_reqhead(NFSX_FH(v3
) + 4 * NFSX_UNSIGNED
+
1945 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(v3
));
1949 VATTR_SET_SUPPORTED(vap
, va_mode
);
1950 VATTR_SET_SUPPORTED(vap
, va_uid
);
1951 VATTR_SET_SUPPORTED(vap
, va_gid
);
1952 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1953 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1954 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1955 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
1956 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
1958 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_MKNOD
]);
1959 nfsm_fhtom(dvp
, v3
);
1960 nfsm_strtom(cnp
->cn_nameptr
, cnp
->cn_namelen
, NFS_MAXNAMLEN
, v3
);
1962 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1963 *tl
++ = vtonfsv3_type(vap
->va_type
);
1965 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
1966 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1967 *tl
++ = txdr_unsigned(major(vap
->va_rdev
));
1968 *tl
= txdr_unsigned(minor(vap
->va_rdev
));
1971 struct timespec neg1time
= { -1, -1 };
1972 nfsm_build(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
1973 sp
->sa_mode
= vtonfsv2_mode(vap
->va_type
,
1974 (VATTR_IS_ACTIVE(vap
, va_mode
) ? vap
->va_mode
: 0600));
1975 sp
->sa_uid
= gotuid
? (u_long
)txdr_unsigned(vap
->va_uid
) : nfs_xdrneg1
;
1976 sp
->sa_gid
= gotgid
? (u_long
)txdr_unsigned(vap
->va_gid
) : nfs_xdrneg1
;
1978 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1979 txdr_nfsv2time(&vap
->va_access_time
, &sp
->sa_atime
);
1981 txdr_nfsv2time(&neg1time
, &sp
->sa_atime
);
1983 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1984 txdr_nfsv2time(&vap
->va_modify_time
, &sp
->sa_mtime
);
1986 txdr_nfsv2time(&neg1time
, &sp
->sa_mtime
);
1989 nfsm_request(dvp
, NFSPROC_MKNOD
, p
, cred
, &xid
);
1990 /* XXX no EEXIST kludge here? */
1992 nfsm_mtofh(dvp
, cnp
, newvp
, v3
, &xid
, gotvp
);
1994 error
= nfs_lookitup(dvp
, cnp
->cn_nameptr
,
1995 cnp
->cn_namelen
, cred
, p
, &np
);
2001 nfsm_wcc_data(dvp
, &premtime
, wccpostattr
, &xid
);
2002 if (!error
&& (gotuid
|| gotgid
) &&
2003 (!newvp
|| nfs_getattrcache(newvp
, &nvattr
) ||
2004 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
2005 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
2006 /* clear ID bits if server didn't use them (or we can't tell) */
2007 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
2008 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
2017 VTONFS(dvp
)->n_flag
|= NMODIFIED
;
2018 /* if directory hadn't changed, update namecache mtime */
2019 if (nfstimespeccmp(&VTONFS(dvp
)->n_ncmtime
, &premtime
, ==))
2020 VTONFS(dvp
)->n_ncmtime
= VTONFS(dvp
)->n_vattr
.nva_mtime
;
2022 NATTRINVALIDATE(VTONFS(dvp
));
2028 * just call nfs_mknodrpc() to do the work.
2033 struct vnop_mknod_args
/* {
2034 struct vnodeop_desc *a_desc;
2037 struct componentname *a_cnp;
2038 struct vnode_attr *a_vap;
2039 vfs_context_t a_context;
2044 error
= nfs_mknodrpc(ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
, ap
->a_vap
,
2045 vfs_context_ucred(ap
->a_context
),
2046 vfs_context_proc(ap
->a_context
));
2051 static u_long create_verf
;
2053 * nfs file create call
2057 struct vnop_create_args
/* {
2058 struct vnodeop_desc *a_desc;
2061 struct componentname *a_cnp;
2062 struct vnode_attr *a_vap;
2063 vfs_context_t a_context;
2066 vnode_t dvp
= ap
->a_dvp
;
2067 struct vnode_attr
*vap
= ap
->a_vap
;
2068 struct componentname
*cnp
= ap
->a_cnp
;
2069 struct nfs_vattr nvattr
;
2070 struct nfsv2_sattr
*sp
;
2074 struct nfsnode
*np
= (struct nfsnode
*)0;
2075 vnode_t newvp
= (vnode_t
)0;
2076 caddr_t bpos
, dpos
, cp2
;
2077 int error
= 0, wccpostattr
= 0, gotvp
= 0, fmode
= 0;
2078 struct timespec premtime
= { 0, 0 };
2079 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2080 int v3
= NFS_ISV3(dvp
);
2086 cred
= vfs_context_ucred(ap
->a_context
);
2087 p
= vfs_context_proc(ap
->a_context
);
2089 if (!VATTR_IS_ACTIVE(vap
, va_type
))
2093 * Oops, not for me..
2095 if (vap
->va_type
== VSOCK
)
2096 return (nfs_mknodrpc(dvp
, ap
->a_vpp
, cnp
, vap
, cred
, p
));
2098 VATTR_SET_SUPPORTED(vap
, va_mode
);
2099 VATTR_SET_SUPPORTED(vap
, va_uid
);
2100 VATTR_SET_SUPPORTED(vap
, va_gid
);
2101 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2102 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2103 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2104 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
2105 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
2107 if (vap
->va_vaflags
& VA_EXCLUSIVE
)
2110 nfsm_reqhead(NFSX_FH(v3
) + 2 * NFSX_UNSIGNED
+
2111 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(v3
));
2114 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_CREATE
]);
2115 nfsm_fhtom(dvp
, v3
);
2116 nfsm_strtom(cnp
->cn_nameptr
, cnp
->cn_namelen
, NFS_MAXNAMLEN
, v3
);
2118 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
2119 if (fmode
& O_EXCL
) {
2120 *tl
= txdr_unsigned(NFSV3CREATE_EXCLUSIVE
);
2121 nfsm_build(tl
, u_long
*, NFSX_V3CREATEVERF
);
2122 if (!TAILQ_EMPTY(&in_ifaddrhead
))
2123 *tl
++ = IA_SIN(in_ifaddrhead
.tqh_first
)->sin_addr
.s_addr
;
2125 *tl
++ = create_verf
;
2126 *tl
= ++create_verf
;
2128 *tl
= txdr_unsigned(NFSV3CREATE_UNCHECKED
);
2132 struct timespec neg1time
= { -1, -1 };
2133 nfsm_build(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
2134 sp
->sa_mode
= vtonfsv2_mode(vap
->va_type
,
2135 (VATTR_IS_ACTIVE(vap
, va_mode
) ? vap
->va_mode
: 0600));
2136 sp
->sa_uid
= gotuid
? (u_long
)txdr_unsigned(vap
->va_uid
) : nfs_xdrneg1
;
2137 sp
->sa_gid
= gotgid
? (u_long
)txdr_unsigned(vap
->va_gid
) : nfs_xdrneg1
;
2139 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2140 txdr_nfsv2time(&vap
->va_access_time
, &sp
->sa_atime
);
2142 txdr_nfsv2time(&neg1time
, &sp
->sa_atime
);
2144 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2145 txdr_nfsv2time(&vap
->va_modify_time
, &sp
->sa_mtime
);
2147 txdr_nfsv2time(&neg1time
, &sp
->sa_mtime
);
2150 nfsm_request(dvp
, NFSPROC_CREATE
, p
, cred
, &xid
);
2152 nfsm_mtofh(dvp
, cnp
, newvp
, v3
, &xid
, gotvp
);
2154 error
= nfs_lookitup(dvp
, cnp
->cn_nameptr
,
2155 cnp
->cn_namelen
, cred
, p
, &np
);
2161 nfsm_wcc_data(dvp
, &premtime
, wccpostattr
, &xid
);
2164 if (v3
&& (fmode
& O_EXCL
) && error
== NFSERR_NOTSUPP
) {
2170 } else if (v3
&& (fmode
& O_EXCL
)) {
2171 error
= nfs_setattrrpc(newvp
, vap
, cred
, p
);
2172 if (error
&& (gotuid
|| gotgid
)) {
2173 /* it's possible the server didn't like our attempt to set IDs. */
2174 /* so, let's try it again without those */
2175 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
2176 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
2177 error
= nfs_setattrrpc(newvp
, vap
, cred
, p
);
2185 VTONFS(dvp
)->n_flag
|= NMODIFIED
;
2186 /* if directory hadn't changed, update namecache mtime */
2187 if (nfstimespeccmp(&VTONFS(dvp
)->n_ncmtime
, &premtime
, ==))
2188 VTONFS(dvp
)->n_ncmtime
= VTONFS(dvp
)->n_vattr
.nva_mtime
;
2190 NATTRINVALIDATE(VTONFS(dvp
));
2191 if (!error
&& (gotuid
|| gotgid
) &&
2192 (!newvp
|| nfs_getattrcache(newvp
, &nvattr
) ||
2193 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
2194 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
2195 /* clear ID bits if server didn't use them (or we can't tell) */
2196 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
2197 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
2203 * nfs file remove call
2204 * To try and make nfs semantics closer to ufs semantics, a file that has
2205 * other processes using the vnode is renamed instead of removed and then
2206 * removed later on the last close.
2207 * - If vnode_isinuse()
2208 * If a rename is not already in the works
2209 * call nfs_sillyrename() to set it up
2215 struct vnop_remove_args
/* {
2216 struct vnodeop_desc *a_desc;
2219 struct componentname *a_cnp;
2221 vfs_context_t a_context;
2224 vnode_t vp
= ap
->a_vp
;
2225 vnode_t dvp
= ap
->a_dvp
;
2226 struct componentname
*cnp
= ap
->a_cnp
;
2227 struct nfsnode
*np
= VTONFS(vp
);
2228 int error
= 0, gofree
= 0;
2229 struct nfs_vattr nvattr
;
2233 cred
= vfs_context_ucred(ap
->a_context
);
2234 p
= vfs_context_proc(ap
->a_context
);
2236 gofree
= vnode_isinuse(vp
, 0) ? 0 : 1;
2237 if ((ap
->a_flags
& VNODE_REMOVE_NODELETEBUSY
) && !gofree
) {
2238 /* Caller requested Carbon delete semantics, but file is busy */
2241 if (gofree
|| (np
->n_sillyrename
&&
2242 nfs_getattr(vp
, &nvattr
, cred
, p
) == 0 &&
2243 nvattr
.nva_nlink
> 1)) {
2245 * Purge the name cache so that the chance of a lookup for
2246 * the name succeeding while the remove is in progress is
2251 * throw away biocache buffers, mainly to avoid
2252 * unnecessary delayed writes later.
2254 error
= nfs_vinvalbuf(vp
, 0, cred
, p
, 1);
2256 ubc_setsize(vp
, (off_t
)0); /* XXX check error */
2259 error
= nfs_removerpc(dvp
, cnp
->cn_nameptr
,
2260 cnp
->cn_namelen
, cred
, p
);
2262 * Kludge City: If the first reply to the remove rpc is lost..
2263 * the reply to the retransmitted request will be ENOENT
2264 * since the file was in fact removed
2265 * Therefore, we cheat and return success.
2267 if (error
== ENOENT
)
2271 * remove nfsnode from hash now so we can't accidentally find it
2272 * again if another object gets created with the same filehandle
2273 * before this vnode gets reclaimed
2275 lck_mtx_lock(nfs_node_hash_mutex
);
2276 LIST_REMOVE(np
, n_hash
);
2277 np
->n_flag
&= ~NHASHED
;
2278 lck_mtx_unlock(nfs_node_hash_mutex
);
2280 if (!error
&& !np
->n_sillyrename
) {
2281 /* clear flags now: won't get nfs_inactive for recycled vnode */
2282 /* clear all flags other than these */
2283 np
->n_flag
&= (NMODIFIED
| NFLUSHINPROG
| NFLUSHWANT
| NHASHED
);
2286 } else if (!np
->n_sillyrename
) {
2287 error
= nfs_sillyrename(dvp
, vp
, cnp
, cred
, p
);
2289 NATTRINVALIDATE(np
);
2295 * nfs file remove rpc called from nfs_inactive
2298 nfs_removeit(struct sillyrename
*sp
)
2300 return (nfs_removerpc(sp
->s_dvp
, sp
->s_name
, sp
->s_namlen
, sp
->s_cred
, NULL
));
2304 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
2307 nfs_removerpc(dvp
, name
, namelen
, cred
, proc
)
2314 register u_long
*tl
;
2315 register caddr_t cp
;
2316 register long t1
, t2
;
2317 caddr_t bpos
, dpos
, cp2
;
2318 int error
= 0, wccpostattr
= 0;
2319 struct timespec premtime
= { 0, 0 };
2320 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2324 if (!VFSTONFS(vnode_mount(dvp
)))
2328 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
2331 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_REMOVE
]);
2332 nfsm_fhtom(dvp
, v3
);
2333 nfsm_strtom(name
, namelen
, NFS_MAXNAMLEN
, v3
);
2334 nfsm_request(dvp
, NFSPROC_REMOVE
, proc
, cred
, &xid
);
2336 nfsm_wcc_data(dvp
, &premtime
, wccpostattr
, &xid
);
2338 VTONFS(dvp
)->n_flag
|= NMODIFIED
;
2339 /* if directory hadn't changed, update namecache mtime */
2340 if (nfstimespeccmp(&VTONFS(dvp
)->n_ncmtime
, &premtime
, ==))
2341 VTONFS(dvp
)->n_ncmtime
= VTONFS(dvp
)->n_vattr
.nva_mtime
;
2343 NATTRINVALIDATE(VTONFS(dvp
));
2348 * nfs file rename call
2352 struct vnop_rename_args
/* {
2353 struct vnodeop_desc *a_desc;
2356 struct componentname *a_fcnp;
2359 struct componentname *a_tcnp;
2360 vfs_context_t a_context;
2363 vnode_t fvp
= ap
->a_fvp
;
2364 vnode_t tvp
= ap
->a_tvp
;
2365 vnode_t fdvp
= ap
->a_fdvp
;
2366 vnode_t tdvp
= ap
->a_tdvp
;
2367 struct componentname
*tcnp
= ap
->a_tcnp
;
2368 struct componentname
*fcnp
= ap
->a_fcnp
;
2370 mount_t fmp
, tdmp
, tmp
;
2371 struct nfsnode
*tnp
;
2375 cred
= vfs_context_ucred(ap
->a_context
);
2376 p
= vfs_context_proc(ap
->a_context
);
2378 tnp
= tvp
? VTONFS(tvp
) : NULL
;
2380 /* Check for cross-device rename */
2381 fmp
= vnode_mount(fvp
);
2382 tmp
= tvp
? vnode_mount(tvp
) : NULL
;
2383 tdmp
= vnode_mount(tdvp
);
2384 if ((fmp
!= tdmp
) || (tvp
&& (fmp
!= tmp
))) {
2390 * If the tvp exists and is in use, sillyrename it before doing the
2391 * rename of the new file over it.
2392 * XXX Can't sillyrename a directory.
2393 * Don't sillyrename if source and target are same vnode (hard
2394 * links or case-variants)
2396 if (tvp
&& tvp
!= fvp
) {
2397 inuse
= vnode_isinuse(tvp
, 0);
2399 if (inuse
&& !tnp
->n_sillyrename
&& vnode_vtype(tvp
) != VDIR
) {
2400 if ((error
= nfs_sillyrename(tdvp
, tvp
, tcnp
, cred
, p
))) {
2401 /* sillyrename failed. Instead of pressing on, return error */
2402 goto out
; /* should not be ENOENT. */
2404 /* sillyrename succeeded.*/
2409 error
= nfs_renamerpc(fdvp
, fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
2410 tdvp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
, cred
, p
);
2413 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2415 if (error
== ENOENT
)
2418 if (!error
&& tvp
&& tvp
!= fvp
&& !tnp
->n_sillyrename
) {
2420 * remove nfsnode from hash now so we can't accidentally find it
2421 * again if another object gets created with the same filehandle
2422 * before this vnode gets reclaimed
2424 lck_mtx_lock(nfs_node_hash_mutex
);
2425 LIST_REMOVE(tnp
, n_hash
);
2426 tnp
->n_flag
&= ~NHASHED
;
2427 lck_mtx_unlock(nfs_node_hash_mutex
);
2430 /* purge the old name cache entries and enter the new one */
2434 if (!error
&& !tnp
->n_sillyrename
) {
2435 /* clear flags now: won't get nfs_inactive for recycled vnode */
2436 /* clear all flags other than these */
2437 tnp
->n_flag
&= (NMODIFIED
| NFLUSHINPROG
| NFLUSHWANT
| NHASHED
);
2442 cache_enter(tdvp
, fvp
, tcnp
);
2446 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2448 if (error
== ENOENT
)
2454 * Do an nfs rename rpc. Called from nfs_rename() and nfs_sillyrename().
2457 nfs_renamerpc(fdvp
, fnameptr
, fnamelen
, tdvp
, tnameptr
, tnamelen
, cred
, proc
)
2467 register u_long
*tl
;
2468 register caddr_t cp
;
2469 register long t1
, t2
;
2470 caddr_t bpos
, dpos
, cp2
;
2471 int error
= 0, fwccpostattr
= 0, twccpostattr
= 0;
2472 struct timespec fpremtime
= { 0, 0 }, tpremtime
= { 0, 0 };
2473 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2477 if (!VFSTONFS(vnode_mount(fdvp
)))
2479 v3
= NFS_ISV3(fdvp
);
2481 nfsm_reqhead((NFSX_FH(v3
) + NFSX_UNSIGNED
)*2 + nfsm_rndup(fnamelen
) +
2482 nfsm_rndup(tnamelen
));
2485 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_RENAME
]);
2486 nfsm_fhtom(fdvp
, v3
);
2487 nfsm_strtom(fnameptr
, fnamelen
, NFS_MAXNAMLEN
, v3
);
2488 nfsm_fhtom(tdvp
, v3
);
2489 nfsm_strtom(tnameptr
, tnamelen
, NFS_MAXNAMLEN
, v3
);
2490 nfsm_request(fdvp
, NFSPROC_RENAME
, proc
, cred
, &xid
);
2492 u_int64_t txid
= xid
;
2494 nfsm_wcc_data(fdvp
, &fpremtime
, fwccpostattr
, &xid
);
2495 nfsm_wcc_data(tdvp
, &tpremtime
, twccpostattr
, &txid
);
2498 VTONFS(fdvp
)->n_flag
|= NMODIFIED
;
2499 /* if directory hadn't changed, update namecache mtime */
2500 if (nfstimespeccmp(&VTONFS(fdvp
)->n_ncmtime
, &fpremtime
, ==))
2501 VTONFS(fdvp
)->n_ncmtime
= VTONFS(fdvp
)->n_vattr
.nva_mtime
;
2503 NATTRINVALIDATE(VTONFS(fdvp
));
2504 VTONFS(tdvp
)->n_flag
|= NMODIFIED
;
2505 /* if directory hadn't changed, update namecache mtime */
2506 if (nfstimespeccmp(&VTONFS(tdvp
)->n_ncmtime
, &tpremtime
, ==))
2507 VTONFS(tdvp
)->n_ncmtime
= VTONFS(tdvp
)->n_vattr
.nva_mtime
;
2509 NATTRINVALIDATE(VTONFS(tdvp
));
2514 * nfs hard link create call
2518 struct vnop_link_args
/* {
2519 struct vnodeop_desc *a_desc;
2522 struct componentname *a_cnp;
2523 vfs_context_t a_context;
2526 vnode_t vp
= ap
->a_vp
;
2527 vnode_t tdvp
= ap
->a_tdvp
;
2528 struct componentname
*cnp
= ap
->a_cnp
;
2532 caddr_t bpos
, dpos
, cp2
;
2533 int error
= 0, wccpostattr
= 0, attrflag
= 0;
2534 struct timespec premtime
= { 0, 0 };
2535 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2541 if (vnode_mount(vp
) != vnode_mount(tdvp
)) {
2545 cred
= vfs_context_ucred(ap
->a_context
);
2546 p
= vfs_context_proc(ap
->a_context
);
2551 * Push all writes to the server, so that the attribute cache
2552 * doesn't get "out of sync" with the server.
2553 * XXX There should be a better way!
2555 nfs_flush(vp
, MNT_WAIT
, cred
, p
, 0);
2557 nfsm_reqhead(NFSX_FH(v3
)*2 + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
2560 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_LINK
]);
2562 nfsm_fhtom(tdvp
, v3
);
2563 nfsm_strtom(cnp
->cn_nameptr
, cnp
->cn_namelen
, NFS_MAXNAMLEN
, v3
);
2564 nfsm_request(vp
, NFSPROC_LINK
, p
, cred
, &xid
);
2566 u_int64_t txid
= xid
;
2568 nfsm_postop_attr_update(vp
, v3
, attrflag
, &xid
);
2569 nfsm_wcc_data(tdvp
, &premtime
, wccpostattr
, &txid
);
2573 VTONFS(tdvp
)->n_flag
|= NMODIFIED
;
2575 NATTRINVALIDATE(VTONFS(vp
));
2576 /* if directory hadn't changed, update namecache mtime */
2577 if (nfstimespeccmp(&VTONFS(tdvp
)->n_ncmtime
, &premtime
, ==))
2578 VTONFS(tdvp
)->n_ncmtime
= VTONFS(tdvp
)->n_vattr
.nva_mtime
;
2580 NATTRINVALIDATE(VTONFS(tdvp
));
2582 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2584 if (error
== EEXIST
)
2590 * nfs symbolic link create call
2594 struct vnop_symlink_args
/* {
2595 struct vnodeop_desc *a_desc;
2598 struct componentname *a_cnp;
2599 struct vnode_attr *a_vap;
2601 vfs_context_t a_context;
2604 vnode_t dvp
= ap
->a_dvp
;
2605 struct vnode_attr
*vap
= ap
->a_vap
;
2606 struct componentname
*cnp
= ap
->a_cnp
;
2607 struct nfs_vattr nvattr
;
2608 struct nfsv2_sattr
*sp
;
2612 caddr_t bpos
, dpos
, cp2
;
2613 int slen
, error
= 0, wccpostattr
= 0, gotvp
= 0;
2614 struct timespec premtime
= { 0, 0 };
2615 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2616 vnode_t newvp
= (vnode_t
)0;
2617 int v3
= NFS_ISV3(dvp
);
2622 struct nfsnode
*np
= NULL
;
2624 cred
= vfs_context_ucred(ap
->a_context
);
2625 p
= vfs_context_proc(ap
->a_context
);
2627 slen
= strlen(ap
->a_target
);
2628 nfsm_reqhead(NFSX_FH(v3
) + 2*NFSX_UNSIGNED
+
2629 nfsm_rndup(cnp
->cn_namelen
) + nfsm_rndup(slen
) + NFSX_SATTR(v3
));
2633 VATTR_SET_SUPPORTED(vap
, va_mode
);
2634 VATTR_SET_SUPPORTED(vap
, va_uid
);
2635 VATTR_SET_SUPPORTED(vap
, va_gid
);
2636 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2637 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2638 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2639 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
2640 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
2642 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_SYMLINK
]);
2643 nfsm_fhtom(dvp
, v3
);
2644 nfsm_strtom(cnp
->cn_nameptr
, cnp
->cn_namelen
, NFS_MAXNAMLEN
, v3
);
2648 nfsm_strtom(ap
->a_target
, slen
, NFS_MAXPATHLEN
, v3
);
2650 struct timespec neg1time
= { -1, -1 };
2651 nfsm_build(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
2652 sp
->sa_mode
= vtonfsv2_mode(VLNK
,
2653 (VATTR_IS_ACTIVE(vap
, va_mode
) ? vap
->va_mode
: 0600));
2654 sp
->sa_uid
= gotuid
? (u_long
)txdr_unsigned(vap
->va_uid
) : nfs_xdrneg1
;
2655 sp
->sa_gid
= gotgid
? (u_long
)txdr_unsigned(vap
->va_gid
) : nfs_xdrneg1
;
2656 sp
->sa_size
= nfs_xdrneg1
;
2657 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2658 txdr_nfsv2time(&vap
->va_access_time
, &sp
->sa_atime
);
2660 txdr_nfsv2time(&neg1time
, &sp
->sa_atime
);
2662 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2663 txdr_nfsv2time(&vap
->va_modify_time
, &sp
->sa_mtime
);
2665 txdr_nfsv2time(&neg1time
, &sp
->sa_mtime
);
2668 nfsm_request(dvp
, NFSPROC_SYMLINK
, p
, cred
, &xid
);
2670 u_int64_t dxid
= xid
;
2673 nfsm_mtofh(dvp
, cnp
, newvp
, v3
, &xid
, gotvp
);
2674 nfsm_wcc_data(dvp
, &premtime
, wccpostattr
, &dxid
);
2678 VTONFS(dvp
)->n_flag
|= NMODIFIED
;
2679 /* if directory hadn't changed, update namecache mtime */
2680 if (nfstimespeccmp(&VTONFS(dvp
)->n_ncmtime
, &premtime
, ==))
2681 VTONFS(dvp
)->n_ncmtime
= VTONFS(dvp
)->n_vattr
.nva_mtime
;
2683 NATTRINVALIDATE(VTONFS(dvp
));
2686 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2687 * if we can succeed in looking up the symlink.
2689 if ((error
== EEXIST
) || (!error
&& !gotvp
)) {
2694 error
= nfs_lookitup(dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cred
, p
, &np
);
2697 if (vnode_vtype(newvp
) != VLNK
)
2701 if (!error
&& (gotuid
|| gotgid
) &&
2702 (!newvp
|| nfs_getattrcache(newvp
, &nvattr
) ||
2703 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
2704 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
2705 /* clear ID bits if server didn't use them (or we can't tell) */
2706 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
2707 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
2723 struct vnop_mkdir_args
/* {
2724 struct vnodeop_desc *a_desc;
2727 struct componentname *a_cnp;
2728 struct vnode_attr *a_vap;
2729 vfs_context_t a_context;
2732 vnode_t dvp
= ap
->a_dvp
;
2733 struct vnode_attr
*vap
= ap
->a_vap
;
2734 struct componentname
*cnp
= ap
->a_cnp
;
2735 struct nfs_vattr nvattr
;
2736 struct nfsv2_sattr
*sp
;
2741 struct nfsnode
*np
= (struct nfsnode
*)0;
2742 vnode_t newvp
= (vnode_t
)0;
2743 caddr_t bpos
, dpos
, cp2
;
2744 int error
= 0, wccpostattr
= 0;
2745 struct timespec premtime
= { 0, 0 };
2747 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2748 int v3
= NFS_ISV3(dvp
);
2750 u_int64_t xid
, dxid
;
2754 cred
= vfs_context_ucred(ap
->a_context
);
2755 p
= vfs_context_proc(ap
->a_context
);
2757 len
= cnp
->cn_namelen
;
2758 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
+ nfsm_rndup(len
) + NFSX_SATTR(v3
));
2762 VATTR_SET_SUPPORTED(vap
, va_mode
);
2763 VATTR_SET_SUPPORTED(vap
, va_uid
);
2764 VATTR_SET_SUPPORTED(vap
, va_gid
);
2765 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2766 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2767 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2768 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
2769 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
2771 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_MKDIR
]);
2772 nfsm_fhtom(dvp
, v3
);
2773 nfsm_strtom(cnp
->cn_nameptr
, len
, NFS_MAXNAMLEN
, v3
);
2777 struct timespec neg1time
= { -1, -1 };
2778 nfsm_build(sp
, struct nfsv2_sattr
*, NFSX_V2SATTR
);
2779 sp
->sa_mode
= vtonfsv2_mode(VDIR
,
2780 (VATTR_IS_ACTIVE(vap
, va_mode
) ? vap
->va_mode
: 0600));
2781 sp
->sa_uid
= gotuid
? (u_long
)txdr_unsigned(vap
->va_uid
) : nfs_xdrneg1
;
2782 sp
->sa_gid
= gotgid
? (u_long
)txdr_unsigned(vap
->va_gid
) : nfs_xdrneg1
;
2783 sp
->sa_size
= nfs_xdrneg1
;
2784 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2785 txdr_nfsv2time(&vap
->va_access_time
, &sp
->sa_atime
);
2787 txdr_nfsv2time(&neg1time
, &sp
->sa_atime
);
2789 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2790 txdr_nfsv2time(&vap
->va_modify_time
, &sp
->sa_mtime
);
2792 txdr_nfsv2time(&neg1time
, &sp
->sa_mtime
);
2795 nfsm_request(dvp
, NFSPROC_MKDIR
, p
, cred
, &xid
);
2798 nfsm_mtofh(dvp
, cnp
, newvp
, v3
, &xid
, gotvp
);
2800 nfsm_wcc_data(dvp
, &premtime
, wccpostattr
, &dxid
);
2802 VTONFS(dvp
)->n_flag
|= NMODIFIED
;
2803 /* if directory hadn't changed, update namecache mtime */
2804 if (nfstimespeccmp(&VTONFS(dvp
)->n_ncmtime
, &premtime
, ==))
2805 VTONFS(dvp
)->n_ncmtime
= VTONFS(dvp
)->n_vattr
.nva_mtime
;
2807 NATTRINVALIDATE(VTONFS(dvp
));
2809 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2810 * if we can succeed in looking up the directory.
2812 if (error
== EEXIST
|| (!error
&& !gotvp
)) {
2817 error
= nfs_lookitup(dvp
, cnp
->cn_nameptr
, len
, cred
, p
, &np
);
2820 if (vnode_vtype(newvp
) != VDIR
)
2824 if (!error
&& (gotuid
|| gotgid
) &&
2825 (!newvp
|| nfs_getattrcache(newvp
, &nvattr
) ||
2826 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
2827 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
2828 /* clear ID bits if server didn't use them (or we can't tell) */
2829 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
2830 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
2842 * nfs remove directory call
2846 struct vnop_rmdir_args
/* {
2847 struct vnodeop_desc *a_desc;
2850 struct componentname *a_cnp;
2851 vfs_context_t a_context;
2854 vnode_t vp
= ap
->a_vp
;
2855 vnode_t dvp
= ap
->a_dvp
;
2856 struct componentname
*cnp
= ap
->a_cnp
;
2860 caddr_t bpos
, dpos
, cp2
;
2861 int error
= 0, wccpostattr
= 0;
2862 struct timespec premtime
= { 0, 0 };
2863 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2864 int v3
= NFS_ISV3(dvp
);
2869 cred
= vfs_context_ucred(ap
->a_context
);
2870 p
= vfs_context_proc(ap
->a_context
);
2872 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
2875 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_RMDIR
]);
2876 nfsm_fhtom(dvp
, v3
);
2877 nfsm_strtom(cnp
->cn_nameptr
, cnp
->cn_namelen
, NFS_MAXNAMLEN
, v3
);
2878 nfsm_request(dvp
, NFSPROC_RMDIR
, p
, cred
, &xid
);
2880 nfsm_wcc_data(dvp
, &premtime
, wccpostattr
, &xid
);
2882 VTONFS(dvp
)->n_flag
|= NMODIFIED
;
2883 /* if directory hadn't changed, update namecache mtime */
2884 if (nfstimespeccmp(&VTONFS(dvp
)->n_ncmtime
, &premtime
, ==))
2885 VTONFS(dvp
)->n_ncmtime
= VTONFS(dvp
)->n_vattr
.nva_mtime
;
2887 NATTRINVALIDATE(VTONFS(dvp
));
2890 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2892 if (error
== ENOENT
)
2896 * remove nfsnode from hash now so we can't accidentally find it
2897 * again if another object gets created with the same filehandle
2898 * before this vnode gets reclaimed
2900 lck_mtx_lock(nfs_node_hash_mutex
);
2901 LIST_REMOVE(VTONFS(vp
), n_hash
);
2902 VTONFS(vp
)->n_flag
&= ~NHASHED
;
2903 lck_mtx_unlock(nfs_node_hash_mutex
);
2913 struct vnop_readdir_args
/* {
2914 struct vnodeop_desc *a_desc;
2920 vfs_context_t a_context;
2923 vnode_t vp
= ap
->a_vp
;
2924 struct nfsnode
*np
= VTONFS(vp
);
2925 struct uio
*uio
= ap
->a_uio
;
2927 struct nfs_vattr nvattr
;
2931 if (vnode_vtype(vp
) != VDIR
)
2934 cred
= vfs_context_ucred(ap
->a_context
);
2935 p
= vfs_context_proc(ap
->a_context
);
2938 * First, check for hit on the EOF offset cache
2940 if (np
->n_direofoffset
> 0 && uio
->uio_offset
>= np
->n_direofoffset
&&
2941 (np
->n_flag
& NMODIFIED
) == 0) {
2942 if (!nfs_getattr(vp
, &nvattr
, cred
, p
)) {
2943 if (nfstimespeccmp(&np
->n_mtime
, &nvattr
.nva_mtime
, ==)) {
2944 OSAddAtomic(1, (SInt32
*)&nfsstats
.direofcache_hits
);
2947 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=)) {
2948 /* directory changed, purge any name cache entries */
2955 * Call nfs_bioread() to do the real work.
2957 // LP64todo - fix this
2958 tresid
= uio_uio_resid(uio
);
2959 error
= nfs_bioread(vp
, uio
, 0, cred
, p
);
2961 if (!error
&& uio_uio_resid(uio
) == tresid
)
2962 OSAddAtomic(1, (SInt32
*)&nfsstats
.direofcache_misses
);
2968 * Called from below the buffer cache by nfs_doio().
2977 register int len
, skiplen
, left
;
2978 register struct dirent
*dp
;
2979 register u_long
*tl
;
2980 register caddr_t cp
;
2981 register long t1
, t2
;
2982 register nfsuint64
*cookiep
;
2983 caddr_t bpos
, dpos
, cp2
;
2984 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
2986 struct nfsmount
*nmp
;
2987 struct nfsnode
*dnp
= VTONFS(vp
);
2989 int error
= 0, tlen
, more_dirs
= 1, blksiz
= 0, bigenough
= 1;
2991 int v3
, nmreaddirsize
;
2995 dp
= (struct dirent
*)0;
2998 if (uiop
->uio_iovcnt
!= 1 || (uiop
->uio_offset
& (NFS_DIRBLKSIZ
- 1)) ||
2999 (uio_uio_resid(uiop
) & (NFS_DIRBLKSIZ
- 1)))
3000 panic("nfs_readdirrpc: bad uio");
3002 nmp
= VFSTONFS(vnode_mount(vp
));
3006 nmreaddirsize
= nmp
->nm_readdirsize
;
3009 * If there is no cookie, assume directory was stale.
3011 cookiep
= nfs_getcookie(dnp
, uiop
->uio_offset
, 0);
3015 return (NFSERR_BAD_COOKIE
);
3017 * Loop around doing readdir rpc's of size nm_readdirsize
3018 * truncated to a multiple of DIRBLKSIZ.
3019 * The stopping criteria is EOF or buffer full.
3021 while (more_dirs
&& bigenough
) {
3022 nfsm_reqhead(NFSX_FH(v3
) + NFSX_READDIR(v3
));
3025 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_READDIR
]);
3028 nfsm_build(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
3029 *tl
++ = cookie
.nfsuquad
[0];
3030 *tl
++ = cookie
.nfsuquad
[1];
3031 *tl
++ = dnp
->n_cookieverf
.nfsuquad
[0];
3032 *tl
++ = dnp
->n_cookieverf
.nfsuquad
[1];
3034 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3035 *tl
++ = cookie
.nfsuquad
[0];
3037 *tl
= txdr_unsigned(nmreaddirsize
);
3038 nfsm_request(vp
, NFSPROC_READDIR
, p
, cred
, &xid
);
3041 nfsm_postop_attr_update(vp
, v3
, attrflag
, &xid
);
3044 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3045 dnp
->n_cookieverf
.nfsuquad
[0] = *tl
++;
3046 dnp
->n_cookieverf
.nfsuquad
[1] = *tl
;
3052 // XXX assert error?
3055 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3056 more_dirs
= fxdr_unsigned(int, *tl
);
3058 /* loop thru the dir entries, doctoring them to 4bsd form */
3059 while (more_dirs
&& bigenough
) {
3061 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
3062 fxdr_hyper(tl
, &fileno
);
3063 len
= fxdr_unsigned(int, *(tl
+ 2));
3065 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3066 fileno
= fxdr_unsigned(u_quad_t
, *tl
++);
3067 len
= fxdr_unsigned(int, *tl
);
3069 /* Note: v3 supports longer names, but struct dirent doesn't */
3070 /* so we just truncate the names to fit */
3076 if (len
> MAXNAMLEN
) {
3077 skiplen
= len
- MAXNAMLEN
;
3082 tlen
= nfsm_rndup(len
);
3084 tlen
+= 4; /* To ensure null termination */
3085 left
= DIRBLKSIZ
- blksiz
;
3086 if ((tlen
+ (int)DIRHDSIZ
) > left
) {
3087 dp
->d_reclen
+= left
;
3088 uio_iov_base_add(uiop
, left
);
3089 uio_iov_len_add(uiop
, -left
);
3090 uiop
->uio_offset
+= left
;
3091 uio_uio_resid_add(uiop
, -left
);
3094 if ((tlen
+ (int)DIRHDSIZ
) > uio_uio_resid(uiop
))
3097 // LP64todo - fix this!
3098 dp
= (struct dirent
*) CAST_DOWN(caddr_t
, uio_iov_base(uiop
));
3099 dp
->d_fileno
= (int)fileno
;
3101 dp
->d_reclen
= tlen
+ DIRHDSIZ
;
3102 dp
->d_type
= DT_UNKNOWN
;
3103 blksiz
+= dp
->d_reclen
;
3104 if (blksiz
== DIRBLKSIZ
)
3106 uiop
->uio_offset
+= DIRHDSIZ
;
3108 uio_uio_resid_add(uiop
, -((int64_t)DIRHDSIZ
));
3109 uio_iov_len_add(uiop
, -((int64_t)DIRHDSIZ
));
3111 uio_uio_resid_add(uiop
, -((int)DIRHDSIZ
));
3112 uio_iov_len_add(uiop
, -((int)DIRHDSIZ
));
3114 uio_iov_base_add(uiop
, DIRHDSIZ
);
3115 nfsm_mtouio(uiop
, len
);
3116 // LP64todo - fix this!
3117 cp
= CAST_DOWN(caddr_t
, uio_iov_base(uiop
));
3119 *cp
= '\0'; /* null terminate */
3120 uio_iov_base_add(uiop
, tlen
);
3121 uio_iov_len_add(uiop
, -tlen
);
3122 uiop
->uio_offset
+= tlen
;
3123 uio_uio_resid_add(uiop
, -tlen
);
3125 nfsm_adv(nfsm_rndup(len
));
3128 nfsm_adv(nfsm_rndup(skiplen
));
3130 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
3132 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
3135 cookie
.nfsuquad
[0] = *tl
++;
3137 cookie
.nfsuquad
[1] = *tl
++;
3142 more_dirs
= fxdr_unsigned(int, *tl
);
3145 * If at end of rpc data, get the eof boolean
3148 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3149 more_dirs
= (fxdr_unsigned(int, *tl
) == 0);
3154 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3155 * by increasing d_reclen for the last record.
3158 left
= DIRBLKSIZ
- blksiz
;
3159 dp
->d_reclen
+= left
;
3160 uio_iov_base_add(uiop
, left
);
3161 uio_iov_len_add(uiop
, -left
);
3162 uiop
->uio_offset
+= left
;
3163 uio_uio_resid_add(uiop
, -left
);
3167 * We are now either at the end of the directory or have filled the
3171 dnp
->n_direofoffset
= uiop
->uio_offset
;
3173 if (uio_uio_resid(uiop
) > 0)
3174 printf("EEK! readdirrpc resid > 0\n");
3175 cookiep
= nfs_getcookie(dnp
, uiop
->uio_offset
, 1);
3184 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
3193 int len
, skiplen
, left
;
3200 caddr_t bpos
, dpos
, cp2
;
3201 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
3202 struct componentname cn
, *cnp
= &cn
;
3204 struct nfsmount
*nmp
;
3205 struct nfsnode
*dnp
= VTONFS(vp
), *np
;
3208 int error
= 0, tlen
, more_dirs
= 1, blksiz
= 0, doit
, bigenough
= 1, i
;
3209 int attrflag
, fhsize
, nmreaddirsize
, nmrsize
;
3210 u_int64_t xid
, savexid
;
3211 struct nfs_vattr nvattr
;
3214 dp
= (struct dirent
*)0;
3217 if (uiop
->uio_iovcnt
!= 1 || (uiop
->uio_offset
& (DIRBLKSIZ
- 1)) ||
3218 (uio_uio_resid(uiop
) & (DIRBLKSIZ
- 1)))
3219 panic("nfs_readdirplusrpc: bad uio");
3221 nmp
= VFSTONFS(vnode_mount(vp
));
3224 nmreaddirsize
= nmp
->nm_readdirsize
;
3225 nmrsize
= nmp
->nm_rsize
;
3227 bzero(cnp
, sizeof(*cnp
));
3231 * If there is no cookie, assume directory was stale.
3233 cookiep
= nfs_getcookie(dnp
, uiop
->uio_offset
, 0);
3237 return (NFSERR_BAD_COOKIE
);
3239 * Loop around doing readdir rpc's of size nm_readdirsize
3240 * truncated to a multiple of DIRBLKSIZ.
3241 * The stopping criteria is EOF or buffer full.
3243 while (more_dirs
&& bigenough
) {
3244 nfsm_reqhead(NFSX_FH(1) + 6 * NFSX_UNSIGNED
);
3247 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_READDIRPLUS
]);
3249 nfsm_build(tl
, u_long
*, 6 * NFSX_UNSIGNED
);
3250 *tl
++ = cookie
.nfsuquad
[0];
3251 *tl
++ = cookie
.nfsuquad
[1];
3252 *tl
++ = dnp
->n_cookieverf
.nfsuquad
[0];
3253 *tl
++ = dnp
->n_cookieverf
.nfsuquad
[1];
3254 *tl
++ = txdr_unsigned(nmreaddirsize
);
3255 *tl
= txdr_unsigned(nmrsize
);
3256 nfsm_request(vp
, NFSPROC_READDIRPLUS
, p
, cred
, &xid
);
3259 nfsm_postop_attr_update(vp
, 1, attrflag
, &xid
);
3265 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
3266 dnp
->n_cookieverf
.nfsuquad
[0] = *tl
++;
3267 dnp
->n_cookieverf
.nfsuquad
[1] = *tl
++;
3268 more_dirs
= fxdr_unsigned(int, *tl
);
3270 /* loop thru the dir entries, doctoring them to 4bsd form */
3271 while (more_dirs
&& bigenough
) {
3272 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
3273 fxdr_hyper(tl
, &fileno
);
3274 len
= fxdr_unsigned(int, *(tl
+ 2));
3275 /* Note: v3 supports longer names, but struct dirent doesn't */
3276 /* so we just truncate the names to fit */
3282 if (len
> MAXNAMLEN
) {
3283 skiplen
= len
- MAXNAMLEN
;
3288 tlen
= nfsm_rndup(len
);
3290 tlen
+= 4; /* To ensure null termination*/
3291 left
= DIRBLKSIZ
- blksiz
;
3292 if ((tlen
+ (int)DIRHDSIZ
) > left
) {
3293 dp
->d_reclen
+= left
;
3294 uio_iov_base_add(uiop
, left
);
3295 uio_iov_len_add(uiop
, -left
);
3296 uiop
->uio_offset
+= left
;
3297 uio_uio_resid_add(uiop
, -left
);
3300 if ((tlen
+ (int)DIRHDSIZ
) > uio_uio_resid(uiop
))
3303 // LP64todo - fix this!
3304 dp
= (struct dirent
*) CAST_DOWN(caddr_t
, uio_iov_base(uiop
));
3305 dp
->d_fileno
= (int)fileno
;
3307 dp
->d_reclen
= tlen
+ DIRHDSIZ
;
3308 dp
->d_type
= DT_UNKNOWN
;
3309 blksiz
+= dp
->d_reclen
;
3310 if (blksiz
== DIRBLKSIZ
)
3312 uiop
->uio_offset
+= DIRHDSIZ
;
3314 uio_uio_resid_add(uiop
, -((int64_t)DIRHDSIZ
));
3315 uio_iov_len_add(uiop
, -((int64_t)DIRHDSIZ
));
3317 uio_uio_resid_add(uiop
, -((int)DIRHDSIZ
));
3318 uio_iov_len_add(uiop
, -((int)DIRHDSIZ
));
3320 uio_iov_base_add(uiop
, DIRHDSIZ
);
3321 // LP64todo - fix this!
3322 cnp
->cn_nameptr
= CAST_DOWN(caddr_t
, uio_iov_base(uiop
));
3323 cnp
->cn_namelen
= len
;
3324 nfsm_mtouio(uiop
, len
);
3325 cp
= CAST_DOWN(caddr_t
, uio_iov_base(uiop
));
3328 uio_iov_base_add(uiop
, tlen
);
3329 uio_iov_len_add(uiop
, -tlen
);
3330 uiop
->uio_offset
+= tlen
;
3331 uio_uio_resid_add(uiop
, -tlen
);
3333 nfsm_adv(nfsm_rndup(len
));
3336 nfsm_adv(nfsm_rndup(skiplen
));
3337 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
3339 cookie
.nfsuquad
[0] = *tl
++;
3340 cookie
.nfsuquad
[1] = *tl
++;
3345 * Since the attributes are before the file handle
3346 * (sigh), we must skip over the attributes and then
3347 * come back and get them.
3349 attrflag
= fxdr_unsigned(int, *tl
);
3351 /* grab attributes */
3352 nfsm_attr_get(1, &nvattr
);
3353 dp
->d_type
= IFTODT(VTTOIF(nvattr
.nva_type
));
3354 /* check for file handle */
3355 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3356 doit
= fxdr_unsigned(int, *tl
);
3358 nfsm_getfh(fhp
, fhsize
, 1);
3359 if (NFS_CMPFH(dnp
, fhp
, fhsize
)) {
3360 error
= vnode_ref(vp
);
3367 } else if (!bigenough
||
3368 (cnp
->cn_namelen
== 2 &&
3369 cnp
->cn_nameptr
[1] == '.' &&
3370 cnp
->cn_nameptr
[0] == '.')) {
3372 * XXXmacko I don't think this ".." thing is a problem anymore.
3373 * don't doit if we can't guarantee
3374 * that this entry is NOT ".." because
3375 * we would have to drop the lock on
3376 * the directory before getting the
3377 * lock on the ".." vnode... and we
3378 * don't want to drop the dvp lock in
3379 * the middle of a readdirplus.
3385 error
= nfs_nget(vnode_mount(vp
), vp
, cnp
,
3386 fhp
, fhsize
, &nvattr
, &xid
,
3394 /* update attributes if not already updated */
3395 if (doit
&& bigenough
&& (np
->n_xid
<= savexid
)) {
3397 nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
3398 /* any error can be ignored */
3401 /* Just skip over the file handle */
3402 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3403 i
= fxdr_unsigned(int, *tl
);
3404 nfsm_adv(nfsm_rndup(i
));
3406 if (newvp
!= NULLVP
) {
3413 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3414 more_dirs
= fxdr_unsigned(int, *tl
);
3417 * If at end of rpc data, get the eof boolean
3420 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
3421 more_dirs
= (fxdr_unsigned(int, *tl
) == 0);
3426 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
3427 * by increasing d_reclen for the last record.
3430 left
= DIRBLKSIZ
- blksiz
;
3431 dp
->d_reclen
+= left
;
3432 uio_iov_base_add(uiop
, left
);
3433 uio_iov_len_add(uiop
, -left
);
3434 uiop
->uio_offset
+= left
;
3435 uio_uio_resid_add(uiop
, -left
);
3439 * We are now either at the end of the directory or have filled the
3443 dnp
->n_direofoffset
= uiop
->uio_offset
;
3445 if (uio_uio_resid(uiop
) > 0)
3446 printf("EEK! readdirplusrpc resid > 0\n");
3447 cookiep
= nfs_getcookie(dnp
, uiop
->uio_offset
, 1);
3456 * Silly rename. To make the NFS filesystem that is stateless look a little
3457 * more like the "ufs" a remove of an active vnode is translated to a rename
3458 * to a funny looking filename that is removed by nfs_inactive on the
3459 * nfsnode. There is the potential for another process on a different client
3460 * to create the same funny name between the nfs_lookitup() fails and the
3461 * nfs_rename() completes, but...
3464 /* format of "random" names and next name to try */
3465 /* (note: shouldn't exceed size of sillyrename.s_name) */
3466 static char sillyrename_name
[] = ".nfsAAA%04x4.4";
3472 struct componentname
*cnp
,
3476 register struct sillyrename
*sp
;
3480 kauth_cred_t tmpcred
;
3486 if (vnode_vtype(vp
) == VDIR
)
3487 panic("nfs_sillyrename: dir");
3489 MALLOC_ZONE(sp
, struct sillyrename
*,
3490 sizeof (struct sillyrename
), M_NFSREQ
, M_WAITOK
);
3493 kauth_cred_ref(cred
);
3496 error
= vnode_ref(dvp
);
3500 /* Fudge together a funny name */
3502 sp
->s_namlen
= sprintf(sp
->s_name
, sillyrename_name
, pid
);
3504 /* Try lookitups until we get one that isn't there */
3506 while (nfs_lookitup(dvp
, sp
->s_name
, sp
->s_namlen
, sp
->s_cred
, p
, NULL
) == 0) {
3507 if (sp
->s_name
[4]++ >= 'z')
3508 sp
->s_name
[4] = 'A';
3509 if (++i
> ('z' - 'A' + 1)) {
3511 if (sp
->s_name
[5]++ >= 'z')
3512 sp
->s_name
[5] = 'A';
3513 if (++j
> ('z' - 'A' + 1)) {
3515 if (sp
->s_name
[6]++ >= 'z')
3516 sp
->s_name
[6] = 'A';
3517 if (++k
> ('z' - 'A' + 1)) {
3524 /* make note of next "random" name to try */
3525 if ((sillyrename_name
[4] = (sp
->s_name
[4] + 1)) > 'z') {
3526 sillyrename_name
[4] = 'A';
3527 if ((sillyrename_name
[5] = (sp
->s_name
[5] + 1)) > 'z') {
3528 sillyrename_name
[5] = 'A';
3529 if ((sillyrename_name
[6] = (sp
->s_name
[6] + 1)) > 'z')
3530 sillyrename_name
[6] = 'A';
3533 /* now, do the rename */
3534 error
= nfs_renamerpc(dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
3535 dvp
, sp
->s_name
, sp
->s_namlen
, sp
->s_cred
, p
);
3538 error
= nfs_lookitup(dvp
, sp
->s_name
, sp
->s_namlen
, sp
->s_cred
, p
, &np
);
3540 kprintf("sillyrename: %s, vp=%x, np=%x, dvp=%x\n",
3541 &sp
->s_name
[0], (unsigned)vp
, (unsigned)np
, (unsigned)dvp
);
3543 np
->n_sillyrename
= sp
;
3546 vnode_rele(sp
->s_dvp
);
3548 tmpcred
= sp
->s_cred
;
3549 sp
->s_cred
= NOCRED
;
3550 kauth_cred_rele(tmpcred
);
3551 FREE_ZONE((caddr_t
)sp
, sizeof (struct sillyrename
), M_NFSREQ
);
3556 * Look up a file name and optionally either update the file handle or
3557 * allocate an nfsnode, depending on the value of npp.
3558 * npp == NULL --> just do the lookup
3559 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3561 * *npp != NULL --> update the file handle in the vnode
3564 nfs_lookitup(dvp
, name
, len
, cred
, procp
, npp
)
3570 struct nfsnode
**npp
;
3575 vnode_t newvp
= (vnode_t
)0;
3576 struct nfsnode
*np
, *dnp
= VTONFS(dvp
);
3577 caddr_t bpos
, dpos
, cp2
;
3578 int error
= 0, fhlen
, attrflag
;
3579 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
3582 u_int64_t xid
, dxid
, savedxid
;
3583 struct nfs_vattr nvattr
;
3585 if (!VFSTONFS(vnode_mount(dvp
)))
3589 nfsm_reqhead(NFSX_FH(v3
) + NFSX_UNSIGNED
+ nfsm_rndup(len
));
3592 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_LOOKUP
]);
3593 nfsm_fhtom(dvp
, v3
);
3594 nfsm_strtom(name
, len
, NFS_MAXNAMLEN
, v3
);
3595 nfsm_request(dvp
, NFSPROC_LOOKUP
, procp
, cred
, &xid
);
3596 if (npp
&& !error
) {
3598 nfsm_getfh(nfhp
, fhlen
, v3
);
3599 /* get attributes */
3601 nfsm_postop_attr_get(v3
, attrflag
, &nvattr
);
3603 /* We need valid attributes in order */
3604 /* to call nfs_nget/vnode_create(). */
3605 error
= nfs_getattr_no_vnode(vnode_mount(dvp
),
3606 nfhp
, fhlen
, cred
, procp
, &nvattr
, &xid
);
3613 nfsm_postop_attr_update(dvp
, v3
, attrflag
, &dxid
);
3615 nfsm_attr_get(v3
, &nvattr
);
3619 if (fhlen
!= np
->n_fhsize
) {
3620 u_char
*oldbuf
= (np
->n_fhsize
> NFS_SMALLFH
) ? np
->n_fhp
: NULL
;
3621 if (fhlen
> NFS_SMALLFH
) {
3622 MALLOC_ZONE(np
->n_fhp
, u_char
*, fhlen
, M_NFSBIGFH
, M_WAITOK
);
3630 np
->n_fhp
= &np
->n_fh
[0];
3633 FREE_ZONE(oldbuf
, np
->n_fhsize
, M_NFSBIGFH
);
3636 bcopy(nfhp
, np
->n_fhp
, fhlen
);
3637 np
->n_fhsize
= fhlen
;
3639 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
3644 } else if (NFS_CMPFH(dnp
, nfhp
, fhlen
)) {
3646 if (dnp
->n_xid
<= savedxid
) {
3648 error
= nfs_loadattrcache(dnp
, &nvattr
, &dxid
, 0);
3655 struct componentname cn
, *cnp
= &cn
;
3656 bzero(cnp
, sizeof(*cnp
));
3657 cnp
->cn_nameptr
= name
;
3658 cnp
->cn_namelen
= len
;
3660 error
= nfs_nget(vnode_mount(dvp
), dvp
, cnp
, nfhp
, fhlen
,
3661 &nvattr
, &xid
, NG_MAKEENTRY
, &np
);
3670 if (npp
&& *npp
== NULL
) {
3685 * Nfs Version 3 commit rpc
3688 nfs_commit(vp
, offset
, count
, cred
, procp
)
3698 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
3699 caddr_t bpos
, dpos
, cp2
;
3700 int error
= 0, wccpostattr
= 0;
3701 struct timespec premtime
= { 0, 0 };
3702 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
3705 FSDBG(521, vp
, offset
, count
, nmp
->nm_state
);
3708 if ((nmp
->nm_state
& NFSSTA_HASWRITEVERF
) == 0)
3710 nfsm_reqhead(NFSX_FH(1));
3713 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpccnt
[NFSPROC_COMMIT
]);
3715 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
3716 txdr_hyper(&offset
, tl
);
3718 *tl
= txdr_unsigned(count
);
3719 nfsm_request(vp
, NFSPROC_COMMIT
, procp
, cred
, &xid
);
3721 nfsm_wcc_data(vp
, &premtime
, wccpostattr
, &xid
);
3722 /* XXX can we do anything useful with the wcc info? */
3725 nfsm_dissect(tl
, u_long
*, NFSX_V3WRITEVERF
);
3726 if (bcmp((caddr_t
)nmp
->nm_verf
, (caddr_t
)tl
,
3727 NFSX_V3WRITEVERF
)) {
3728 bcopy((caddr_t
)tl
, (caddr_t
)nmp
->nm_verf
,
3730 error
= NFSERR_STALEWRITEVERF
;
3739 __unused
struct vnop_blockmap_args
/* {
3740 struct vnodeop_desc *a_desc;
3756 * NB Currently unsupported.
3761 __unused
struct vnop_mmap_args
/* {
3762 struct vnodeop_desc *a_desc;
3765 kauth_cred_t a_cred;
3774 * fsync vnode op. Just call nfs_flush() with commit == 1.
3779 struct vnop_fsync_args
/* {
3780 struct vnodeop_desc *a_desc;
3783 vfs_context_t a_context;
3786 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
3787 proc_t p
= vfs_context_proc(ap
->a_context
);
3788 struct nfsnode
*np
= VTONFS(ap
->a_vp
);
3791 np
->n_flag
|= NWRBUSY
;
3792 error
= nfs_flush(ap
->a_vp
, ap
->a_waitfor
, cred
, p
, 0);
3793 np
->n_flag
&= ~NWRBUSY
;
3798 nfs_flushcommits(vnode_t vp
, proc_t p
, int nowait
)
3800 struct nfsnode
*np
= VTONFS(vp
);
3802 struct nfsbuflists blist
, commitlist
;
3803 int error
= 0, retv
, wcred_set
, flags
;
3804 u_quad_t off
, endoff
, toff
;
3806 kauth_cred_t wcred
= NULL
;
3808 FSDBG_TOP(557, vp
, np
, 0, 0);
3811 * A nb_flags == (NB_DELWRI | NB_NEEDCOMMIT) block has been written to the
3812 * server, but nas not been committed to stable storage on the server
3813 * yet. The byte range is worked out for as many nfsbufs as we can handle
3814 * and the commit rpc is done.
3816 if (!LIST_EMPTY(&np
->n_dirtyblkhd
))
3817 np
->n_flag
|= NMODIFIED
;
3822 LIST_INIT(&commitlist
);
3824 if (!VFSTONFS(vnode_mount(vp
))) {
3828 if (!NFS_ISV3(vp
)) {
3835 flags
|= NBI_NOWAIT
;
3836 lck_mtx_lock(nfs_buf_mutex
);
3837 if (!nfs_buf_iterprepare(np
, &blist
, flags
)) {
3838 while ((bp
= LIST_FIRST(&blist
))) {
3839 LIST_REMOVE(bp
, nb_vnbufs
);
3840 LIST_INSERT_HEAD(&np
->n_dirtyblkhd
, bp
, nb_vnbufs
);
3841 error
= nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0);
3844 if (((bp
->nb_flags
& (NB_DELWRI
| NB_NEEDCOMMIT
))
3845 != (NB_DELWRI
| NB_NEEDCOMMIT
))) {
3849 nfs_buf_remfree(bp
);
3850 lck_mtx_unlock(nfs_buf_mutex
);
3852 * we need a upl to see if the page has been
3853 * dirtied (think mmap) since the unstable write, and
3854 * also to prevent vm from paging it during our commit rpc
3856 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
)) {
3857 retv
= nfs_buf_upl_setup(bp
);
3859 /* unable to create upl */
3860 /* vm object must no longer exist */
3861 /* this could be fatal if we need */
3862 /* to write the data again, we'll see... */
3863 printf("nfs_flushcommits: upl create failed %d\n", retv
);
3864 bp
->nb_valid
= bp
->nb_dirty
= 0;
3867 nfs_buf_upl_check(bp
);
3868 lck_mtx_lock(nfs_buf_mutex
);
3870 FSDBG(557, bp
, bp
->nb_flags
, bp
->nb_valid
, bp
->nb_dirty
);
3871 FSDBG(557, bp
->nb_validoff
, bp
->nb_validend
,
3872 bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
3875 * We used to check for dirty pages here; if there were any
3876 * we'd abort the commit and force the entire buffer to be
3879 * Instead of doing that, we now go ahead and commit the dirty
3880 * range, and then leave the buffer around with dirty pages
3881 * that will be written out later.
3885 * Work out if all buffers are using the same cred
3886 * so we can deal with them all with one commit.
3888 * XXX creds in bp's must be obtained by kauth_cred_ref on
3889 * the same original cred in order for them to be equal.
3891 if (wcred_set
== 0) {
3892 wcred
= bp
->nb_wcred
;
3893 if (wcred
== NOCRED
)
3894 panic("nfs: needcommit w/out wcred");
3896 } else if ((wcred_set
== 1) && wcred
!= bp
->nb_wcred
) {
3899 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3902 * A list of these buffers is kept so that the
3903 * second loop knows which buffers have actually
3904 * been committed. This is necessary, since there
3905 * may be a race between the commit rpc and new
3906 * uncommitted writes on the file.
3908 LIST_REMOVE(bp
, nb_vnbufs
);
3909 LIST_INSERT_HEAD(&commitlist
, bp
, nb_vnbufs
);
3910 toff
= NBOFF(bp
) + bp
->nb_dirtyoff
;
3913 toff
+= (u_quad_t
)(bp
->nb_dirtyend
- bp
->nb_dirtyoff
);
3917 nfs_buf_itercomplete(np
, &blist
, NBI_DIRTY
);
3919 lck_mtx_unlock(nfs_buf_mutex
);
3921 if (LIST_EMPTY(&commitlist
)) {
3927 * Commit data on the server, as required.
3928 * If all bufs are using the same wcred, then use that with
3929 * one call for all of them, otherwise commit each one
3932 if (wcred_set
== 1) {
3934 * Note, it's possible the commit range could be >2^32-1.
3935 * If it is, we'll send one commit that covers the whole file.
3937 if ((endoff
- off
) > 0xffffffff)
3940 count
= (endoff
- off
);
3941 retv
= nfs_commit(vp
, off
, count
, wcred
, p
);
3944 LIST_FOREACH(bp
, &commitlist
, nb_vnbufs
) {
3945 toff
= NBOFF(bp
) + bp
->nb_dirtyoff
;
3946 count
= bp
->nb_dirtyend
- bp
->nb_dirtyoff
;
3947 retv
= nfs_commit(vp
, toff
, count
, bp
->nb_wcred
, p
);
3952 if (retv
== NFSERR_STALEWRITEVERF
)
3953 nfs_clearcommit(vnode_mount(vp
));
3956 * Now, either mark the blocks I/O done or mark the
3957 * blocks dirty, depending on whether the commit
3960 while ((bp
= LIST_FIRST(&commitlist
))) {
3961 LIST_REMOVE(bp
, nb_vnbufs
);
3962 FSDBG(557, bp
, retv
, bp
->nb_flags
, bp
->nb_dirty
);
3963 CLR(bp
->nb_flags
, (NB_NEEDCOMMIT
| NB_WRITEINPROG
));
3964 np
->n_needcommitcnt
--;
3965 CHECK_NEEDCOMMITCNT(np
);
3968 /* move back to dirty list */
3969 lck_mtx_lock(nfs_buf_mutex
);
3970 LIST_INSERT_HEAD(&VTONFS(vp
)->n_dirtyblkhd
, bp
, nb_vnbufs
);
3971 lck_mtx_unlock(nfs_buf_mutex
);
3972 nfs_buf_release(bp
, 1);
3976 vnode_startwrite(vp
);
3977 if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3978 OSAddAtomic(-1, (SInt32
*)&nfs_nbdwrite
);
3980 wakeup(&nfs_nbdwrite
);
3982 CLR(bp
->nb_flags
, (NB_READ
|NB_DONE
|NB_ERROR
|NB_DELWRI
));
3983 /* if block still has dirty pages, we don't want it to */
3984 /* be released in nfs_buf_iodone(). So, don't set NB_ASYNC. */
3986 SET(bp
->nb_flags
, NB_ASYNC
);
3988 /* move to clean list */
3989 lck_mtx_lock(nfs_buf_mutex
);
3990 LIST_INSERT_HEAD(&VTONFS(vp
)->n_cleanblkhd
, bp
, nb_vnbufs
);
3991 lck_mtx_unlock(nfs_buf_mutex
);
3993 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3997 /* throw it back in as a delayed write buffer */
3998 CLR(bp
->nb_flags
, NB_DONE
);
3999 nfs_buf_write_delayed(bp
, p
);
4004 FSDBG_BOT(557, vp
, np
, 0, error
);
4009 * Flush all the blocks associated with a vnode.
4010 * Walk through the buffer pool and push any dirty pages
4011 * associated with the vnode.
4017 __unused kauth_cred_t cred
,
4019 int ignore_writeerr
)
4021 struct nfsnode
*np
= VTONFS(vp
);
4023 struct nfsbuflists blist
;
4024 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
4025 int error
= 0, error2
, slptimeo
= 0, slpflag
= 0;
4026 int flags
, passone
= 1;
4028 FSDBG_TOP(517, vp
, np
, waitfor
, 0);
4034 if (nmp
->nm_flag
& NFSMNT_INT
)
4038 * On the first pass, start async/unstable writes on all
4039 * delayed write buffers. Then wait for all writes to complete
4040 * and call nfs_flushcommits() to commit any uncommitted buffers.
4041 * On all subsequent passes, start STABLE writes on any remaining
4042 * dirty buffers. Then wait for all writes to complete.
4045 lck_mtx_lock(nfs_buf_mutex
);
4046 FSDBG(518, LIST_FIRST(&np
->n_dirtyblkhd
), np
->n_flag
, 0, 0);
4047 if (!LIST_EMPTY(&np
->n_dirtyblkhd
))
4048 np
->n_flag
|= NMODIFIED
;
4049 if (!VFSTONFS(vnode_mount(vp
))) {
4050 lck_mtx_unlock(nfs_buf_mutex
);
4055 /* Start/do any write(s) that are required. */
4056 if (!nfs_buf_iterprepare(np
, &blist
, NBI_DIRTY
)) {
4057 while ((bp
= LIST_FIRST(&blist
))) {
4058 LIST_REMOVE(bp
, nb_vnbufs
);
4059 LIST_INSERT_HEAD(&np
->n_dirtyblkhd
, bp
, nb_vnbufs
);
4060 flags
= (passone
|| (waitfor
!= MNT_WAIT
)) ? NBAC_NOWAIT
: 0;
4061 if (flags
!= NBAC_NOWAIT
)
4063 while ((error
= nfs_buf_acquire(bp
, flags
, slpflag
, slptimeo
))) {
4064 FSDBG(524, bp
, flags
, bp
->nb_lflags
, bp
->nb_flags
);
4068 error2
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
);
4070 if (flags
!= NBAC_NOWAIT
)
4071 nfs_buf_refrele(bp
);
4072 nfs_buf_itercomplete(np
, &blist
, NBI_DIRTY
);
4073 lck_mtx_unlock(nfs_buf_mutex
);
4077 if (slpflag
== PCATCH
) {
4083 if (flags
!= NBAC_NOWAIT
)
4084 nfs_buf_refrele(bp
);
4088 /* buffer is no longer valid */
4092 if (!ISSET(bp
->nb_flags
, NB_DELWRI
))
4093 panic("nfs_flush: not dirty");
4094 FSDBG(525, bp
, passone
, bp
->nb_lflags
, bp
->nb_flags
);
4095 if ((passone
|| (waitfor
!= MNT_WAIT
)) &&
4096 ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
4100 nfs_buf_remfree(bp
);
4101 lck_mtx_unlock(nfs_buf_mutex
);
4102 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
4103 np
->n_error
= bp
->nb_error
? bp
->nb_error
: EIO
;
4104 np
->n_flag
|= NWRITEERR
;
4105 nfs_buf_release(bp
, 1);
4106 lck_mtx_lock(nfs_buf_mutex
);
4109 SET(bp
->nb_flags
, NB_ASYNC
);
4111 /* NB_STABLE forces this to be written FILESYNC */
4112 SET(bp
->nb_flags
, NB_STABLE
);
4115 lck_mtx_lock(nfs_buf_mutex
);
4117 nfs_buf_itercomplete(np
, &blist
, NBI_DIRTY
);
4119 lck_mtx_unlock(nfs_buf_mutex
);
4121 if (waitfor
== MNT_WAIT
) {
4122 while ((error
= vnode_waitforwrites(vp
, 0, slpflag
, slptimeo
, "nfsflush"))) {
4123 error2
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
);
4128 if (slpflag
== PCATCH
) {
4136 /* loop while it looks like there are still buffers to be */
4137 /* commited and nfs_flushcommits() seems to be handling them. */
4138 while (np
->n_needcommitcnt
)
4139 if (nfs_flushcommits(vp
, p
, 0))
4148 if ((waitfor
== MNT_WAIT
) && !LIST_EMPTY(&np
->n_dirtyblkhd
)) {
4152 FSDBG(526, np
->n_flag
, np
->n_error
, 0, 0);
4153 if (!ignore_writeerr
&& (np
->n_flag
& NWRITEERR
)) {
4154 error
= np
->n_error
;
4155 np
->n_flag
&= ~NWRITEERR
;
4158 FSDBG_BOT(517, vp
, np
, error
, 0);
4163 * Do an nfs pathconf rpc.
4168 struct nfsv3_pathconf
*pc
,
4172 mbuf_t mreq
, mrep
, md
, mb
, mb2
;
4173 caddr_t bpos
, dpos
, cp
, cp2
;
4177 int attrflag
, error
= 0;
4178 struct nfsv3_pathconf
*mpc
;
4180 /* fetch pathconf info from server */
4181 nfsm_reqhead(NFSX_FH(1));
4185 nfsm_request(vp
, NFSPROC_PATHCONF
, procp
, cred
, &xid
);
4186 nfsm_postop_attr_update(vp
, 1, attrflag
, &xid
);
4188 nfsm_dissect(mpc
, struct nfsv3_pathconf
*, NFSX_V3PATHCONF
);
4189 pc
->pc_linkmax
= fxdr_unsigned(long, mpc
->pc_linkmax
);
4190 pc
->pc_namemax
= fxdr_unsigned(long, mpc
->pc_namemax
);
4191 pc
->pc_chownrestricted
= fxdr_unsigned(long, mpc
->pc_chownrestricted
);
4192 pc
->pc_notrunc
= fxdr_unsigned(long, mpc
->pc_notrunc
);
4193 pc
->pc_caseinsensitive
= fxdr_unsigned(long, mpc
->pc_caseinsensitive
);
4194 pc
->pc_casepreserving
= fxdr_unsigned(long, mpc
->pc_casepreserving
);
4202 nfs_pathconf_cache(struct nfsmount
*nmp
, struct nfsv3_pathconf
*pc
)
4204 nmp
->nm_state
|= NFSSTA_GOTPATHCONF
;
4205 nmp
->nm_fsinfo
.linkmax
= pc
->pc_linkmax
;
4206 nmp
->nm_fsinfo
.namemax
= pc
->pc_namemax
;
4207 nmp
->nm_fsinfo
.pcflags
= 0;
4209 nmp
->nm_fsinfo
.pcflags
|= NFSPCINFO_NOTRUNC
;
4210 if (pc
->pc_chownrestricted
)
4211 nmp
->nm_fsinfo
.pcflags
|= NFSPCINFO_CHOWN_RESTRICTED
;
4212 if (pc
->pc_caseinsensitive
)
4213 nmp
->nm_fsinfo
.pcflags
|= NFSPCINFO_CASE_INSENSITIVE
;
4214 if (pc
->pc_casepreserving
)
4215 nmp
->nm_fsinfo
.pcflags
|= NFSPCINFO_CASE_PRESERVING
;
4219 * Return POSIX pathconf information applicable to nfs.
4221 * The NFS V2 protocol doesn't support this, so just return EINVAL
4227 struct vnop_pathconf_args
/* {
4228 struct vnodeop_desc *a_desc;
4231 register_t *a_retval;
4232 vfs_context_t a_context;
4235 vnode_t vp
= ap
->a_vp
;
4236 struct nfsmount
*nmp
;
4237 struct nfsv3_pathconf pc
;
4238 int error
= 0, cached
;
4240 nmp
= VFSTONFS(vnode_mount(vp
));
4246 switch (ap
->a_name
) {
4249 case _PC_CHOWN_RESTRICTED
:
4251 case _PC_CASE_SENSITIVE
:
4252 case _PC_CASE_PRESERVING
:
4255 /* don't bother contacting the server if we know the answer */
4259 if (!(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
4260 /* no pathconf info cached */
4261 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
4262 proc_t p
= vfs_context_proc(ap
->a_context
);
4263 error
= nfs_pathconfrpc(vp
, &pc
, cred
, p
);
4266 nmp
= VFSTONFS(vnode_mount(vp
));
4269 if (!(nmp
->nm_state
& NFSSTA_GOTFSINFO
)) {
4270 nfs_fsinfo(nmp
, vp
, cred
, p
);
4271 nmp
= VFSTONFS(vnode_mount(vp
));
4275 if ((nmp
->nm_state
& NFSSTA_GOTFSINFO
) &&
4276 (nmp
->nm_fsinfo
.fsproperties
& NFSV3FSINFO_HOMOGENEOUS
)) {
4277 /* all files have the same pathconf info, */
4278 /* so cache a copy of the results */
4279 nfs_pathconf_cache(nmp
, &pc
);
4283 cached
= (nmp
->nm_state
& NFSSTA_GOTPATHCONF
);
4285 switch (ap
->a_name
) {
4287 *ap
->a_retval
= cached
? nmp
->nm_fsinfo
.linkmax
: pc
.pc_linkmax
;
4290 *ap
->a_retval
= cached
? nmp
->nm_fsinfo
.namemax
: pc
.pc_namemax
;
4292 case _PC_CHOWN_RESTRICTED
:
4294 *ap
->a_retval
= (nmp
->nm_fsinfo
.pcflags
& NFSPCINFO_CHOWN_RESTRICTED
) ? 1 : 0;
4296 *ap
->a_retval
= pc
.pc_chownrestricted
;
4300 *ap
->a_retval
= (nmp
->nm_fsinfo
.pcflags
& NFSPCINFO_NOTRUNC
) ? 1 : 0;
4302 *ap
->a_retval
= pc
.pc_notrunc
;
4304 case _PC_CASE_SENSITIVE
:
4306 *ap
->a_retval
= (nmp
->nm_fsinfo
.pcflags
& NFSPCINFO_CASE_INSENSITIVE
) ? 0 : 1;
4308 *ap
->a_retval
= !pc
.pc_caseinsensitive
;
4310 case _PC_CASE_PRESERVING
:
4312 *ap
->a_retval
= (nmp
->nm_fsinfo
.pcflags
& NFSPCINFO_CASE_PRESERVING
) ? 1 : 0;
4314 *ap
->a_retval
= pc
.pc_casepreserving
;
4324 * NFS advisory byte-level locks (client)
4328 struct vnop_advlock_args
/* {
4329 struct vnodeop_desc *a_desc;
4335 vfs_context_t a_context;
4338 return (nfs_dolock(ap
));
4342 * write (or commit) the given NFS buffer
4345 nfs_buf_write(struct nfsbuf
*bp
)
4347 int oldflags
= bp
->nb_flags
, rv
= 0;
4348 vnode_t vp
= bp
->nb_vp
;
4349 struct nfsnode
*np
= VTONFS(vp
);
4351 proc_t p
= current_proc(); // XXX
4353 FSDBG_TOP(553, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
4355 if (!ISSET(bp
->nb_lflags
, NBL_BUSY
))
4356 panic("nfs_buf_write: buffer is not busy???");
4358 CLR(bp
->nb_flags
, (NB_READ
|NB_DONE
|NB_ERROR
|NB_DELWRI
));
4359 if (ISSET(oldflags
, NB_DELWRI
)) {
4360 OSAddAtomic(-1, (SInt32
*)&nfs_nbdwrite
);
4362 wakeup(&nfs_nbdwrite
);
4365 /* move to clean list */
4366 if (ISSET(oldflags
, (NB_ASYNC
|NB_DELWRI
))) {
4367 lck_mtx_lock(nfs_buf_mutex
);
4368 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
)
4369 LIST_REMOVE(bp
, nb_vnbufs
);
4370 LIST_INSERT_HEAD(&VTONFS(vp
)->n_cleanblkhd
, bp
, nb_vnbufs
);
4371 lck_mtx_unlock(nfs_buf_mutex
);
4373 vnode_startwrite(vp
);
4375 if (p
&& p
->p_stats
)
4376 p
->p_stats
->p_ru
.ru_oublock
++;
4379 * For async requests when nfsiod(s) are running, queue the request by
4380 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the request.
4382 if (ISSET(bp
->nb_flags
, NB_ASYNC
))
4384 if (ISSET(bp
->nb_flags
, NB_READ
))
4388 if (!ISSET(bp
->nb_flags
, NB_ASYNC
) || nfs_asyncio(bp
, NOCRED
))
4389 rv
= nfs_doio(bp
, cr
, p
);
4391 if ((oldflags
& NB_ASYNC
) == 0) {
4392 rv
= nfs_buf_iowait(bp
);
4393 /* move to clean list */
4394 if (oldflags
& NB_DELWRI
) {
4395 lck_mtx_lock(nfs_buf_mutex
);
4396 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
)
4397 LIST_REMOVE(bp
, nb_vnbufs
);
4398 LIST_INSERT_HEAD(&VTONFS(vp
)->n_cleanblkhd
, bp
, nb_vnbufs
);
4399 lck_mtx_unlock(nfs_buf_mutex
);
4401 oldflags
= bp
->nb_flags
;
4402 FSDBG_BOT(553, bp
, NBOFF(bp
), bp
->nb_flags
, rv
);
4406 nfs_buf_release(bp
, 1);
4407 if (ISSET(oldflags
, NB_ERROR
) && !(np
->n_flag
& NFLUSHINPROG
)) {
4409 * There was a write error and we need to
4410 * invalidate attrs and flush buffers in
4411 * order to sync up with the server.
4412 * (if this write was extending the file,
4413 * we may no longer know the correct size)
4415 * But we couldn't call vinvalbuf while holding
4416 * the buffer busy. So we call vinvalbuf() after
4417 * releasing the buffer.
4419 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cr
, p
, 1);
4422 kauth_cred_rele(cr
);
4426 FSDBG_BOT(553, bp
, NBOFF(bp
), bp
->nb_flags
, rv
);
4431 * Read wrapper for special devices.
4435 struct vnop_read_args
/* {
4436 struct vnodeop_desc *a_desc;
4440 vfs_context_t a_context;
4443 register struct nfsnode
*np
= VTONFS(ap
->a_vp
);
4451 np
->n_atim
.tv_sec
= now
.tv_sec
;
4452 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
4453 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
4457 * Write wrapper for special devices.
4461 struct vnop_write_args
/* {
4462 struct vnodeop_desc *a_desc;
4466 vfs_context_t a_context;
4469 register struct nfsnode
*np
= VTONFS(ap
->a_vp
);
4477 np
->n_mtim
.tv_sec
= now
.tv_sec
;
4478 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
4479 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
4483 * Close wrapper for special devices.
4485 * Update the times on the nfsnode then do device close.
4489 struct vnop_close_args
/* {
4490 struct vnodeop_desc *a_desc;
4493 vfs_context_t a_context;
4496 vnode_t vp
= ap
->a_vp
;
4497 struct nfsnode
*np
= VTONFS(vp
);
4498 struct vnode_attr vattr
;
4501 if (np
->n_flag
& (NACC
| NUPD
)) {
4503 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
4505 if (np
->n_flag
& NACC
) {
4506 vattr
.va_access_time
= np
->n_atim
;
4507 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
4509 if (np
->n_flag
& NUPD
) {
4510 vattr
.va_modify_time
= np
->n_mtim
;
4511 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
4513 vnode_setattr(vp
, &vattr
, ap
->a_context
);
4516 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
4519 extern vnop_t
**fifo_vnodeop_p
;
4522 * Read wrapper for fifos.
4526 struct vnop_read_args
/* {
4527 struct vnodeop_desc *a_desc;
4531 vfs_context_t a_context;
4534 register struct nfsnode
*np
= VTONFS(ap
->a_vp
);
4542 np
->n_atim
.tv_sec
= now
.tv_sec
;
4543 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
4544 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
4548 * Write wrapper for fifos.
4552 struct vnop_write_args
/* {
4553 struct vnodeop_desc *a_desc;
4557 vfs_context_t a_context;
4560 register struct nfsnode
*np
= VTONFS(ap
->a_vp
);
4568 np
->n_mtim
.tv_sec
= now
.tv_sec
;
4569 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
4570 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
4574 * Close wrapper for fifos.
4576 * Update the times on the nfsnode then do fifo close.
4580 struct vnop_close_args
/* {
4581 struct vnodeop_desc *a_desc;
4584 vfs_context_t a_context;
4587 vnode_t vp
= ap
->a_vp
;
4588 struct nfsnode
*np
= VTONFS(vp
);
4589 struct vnode_attr vattr
;
4593 if (np
->n_flag
& (NACC
| NUPD
)) {
4595 if (np
->n_flag
& NACC
) {
4596 np
->n_atim
.tv_sec
= now
.tv_sec
;
4597 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
4599 if (np
->n_flag
& NUPD
) {
4600 np
->n_mtim
.tv_sec
= now
.tv_sec
;
4601 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
4604 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
4606 if (np
->n_flag
& NACC
) {
4607 vattr
.va_access_time
= np
->n_atim
;
4608 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
4610 if (np
->n_flag
& NUPD
) {
4611 vattr
.va_modify_time
= np
->n_mtim
;
4612 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
4614 vnode_setattr(vp
, &vattr
, ap
->a_context
);
4617 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
4623 __unused
struct vnop_ioctl_args
/* {
4624 struct vnodeop_desc *a_desc;
4629 kauth_cred_t a_cred;
4635 * XXX we were once bogusly enoictl() which returned this (ENOTTY).
4636 * Probably we should return ENODEV.
4644 __unused
struct vnop_select_args
/* {
4645 struct vnodeop_desc *a_desc;
4649 kauth_cred_t a_cred;
4656 * We were once bogusly seltrue() which returns 1. Is this right?
4662 * Vnode op for pagein using getblk_pages
4663 * derived from nfs_bioread()
4664 * No read aheads are started from pagein operation
4668 struct vnop_pagein_args
/* {
4669 struct vnodeop_desc *a_desc;
4672 vm_offset_t a_pl_offset;
4676 vfs_context_t a_context;
4679 vnode_t vp
= ap
->a_vp
;
4680 upl_t pl
= ap
->a_pl
;
4681 size_t size
= ap
->a_size
;
4682 off_t f_offset
= ap
->a_f_offset
;
4683 vm_offset_t pl_offset
= ap
->a_pl_offset
;
4684 int flags
= ap
->a_flags
;
4687 struct nfsnode
*np
= VTONFS(vp
);
4688 int biosize
, xsize
, iosize
;
4689 struct nfsmount
*nmp
;
4693 struct iovec_32 aiov
;
4694 struct uio
* uio
= &auio
;
4695 int nofreeupl
= flags
& UPL_NOCOMMIT
;
4696 upl_page_info_t
*plinfo
;
4698 FSDBG(322, vp
, f_offset
, size
, flags
);
4699 if (pl
== (upl_t
)NULL
)
4700 panic("nfs_pagein: no upl");
4702 if (UBCINVALID(vp
)) {
4703 printf("nfs_pagein: invalid vnode 0x%x", (int)vp
);
4705 (void) ubc_upl_abort(pl
, 0);
4708 UBCINFOCHECK("nfs_pagein", vp
);
4711 printf("nfs_pagein: invalid size %d", size
);
4713 (void) ubc_upl_abort(pl
, 0);
4716 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
|| (f_offset
& PAGE_MASK_64
)) {
4718 ubc_upl_abort_range(pl
, pl_offset
, size
,
4719 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
4723 cred
= ubc_getcred(vp
);
4725 cred
= vfs_context_ucred(ap
->a_context
);
4726 p
= vfs_context_proc(ap
->a_context
);
4728 auio
.uio_offset
= f_offset
;
4729 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
4730 auio
.uio_segflg
= UIO_SYSSPACE
;
4732 auio
.uio_segflg
= UIO_SYSSPACE32
;
4734 auio
.uio_rw
= UIO_READ
;
4737 nmp
= VFSTONFS(vnode_mount(vp
));
4740 ubc_upl_abort_range(pl
, pl_offset
, size
,
4741 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
4744 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) && !(nmp
->nm_state
& NFSSTA_GOTFSINFO
))
4745 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
4746 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
4748 plinfo
= ubc_upl_pageinfo(pl
);
4749 ubc_upl_map(pl
, &ioaddr
);
4750 ioaddr
+= pl_offset
;
4755 * It would be nice to be able to issue all these requests
4756 * in parallel instead of waiting for each one to complete
4757 * before sending the next one.
4758 * XXX Should we align these requests to block boundaries?
4760 iosize
= min(biosize
, xsize
);
4761 aiov
.iov_len
= iosize
;
4762 aiov
.iov_base
= (uintptr_t)ioaddr
;
4763 auio
.uio_iovs
.iov32p
= &aiov
;
4764 auio
.uio_iovcnt
= 1;
4765 uio_uio_resid_set(&auio
, iosize
);
4767 FSDBG(322, uio
->uio_offset
, uio_uio_resid(uio
), ioaddr
, xsize
);
4769 * With UBC we get here only when the file data is not in the VM
4770 * page cache, so go ahead and read in.
4773 upl_ubc_alias_set(pl
, current_thread(), 2);
4774 #endif /* UPL_DEBUG */
4775 OSAddAtomic(1, (SInt32
*)&nfsstats
.pageins
);
4777 error
= nfs_readrpc(vp
, uio
, cred
, p
);
4780 if (uio_uio_resid(uio
)) {
4782 * If uio_resid > 0, there is a hole in the file
4783 * and no writes after the hole have been pushed
4784 * to the server yet... or we're at the EOF
4785 * Just zero fill the rest of the valid area.
4787 // LP64todo - fix this
4788 int zcnt
= uio_uio_resid(uio
);
4789 int zoff
= iosize
- zcnt
;
4790 bzero((char *)ioaddr
+ zoff
, zcnt
);
4792 FSDBG(324, uio
->uio_offset
, zoff
, zcnt
, ioaddr
);
4793 uio
->uio_offset
+= zcnt
;
4798 FSDBG(322, uio
->uio_offset
, uio_uio_resid(uio
), error
, -1);
4801 nmp
= VFSTONFS(vnode_mount(vp
));
4802 } while (error
== 0 && xsize
> 0);
4808 ubc_upl_abort_range(pl
, pl_offset
, size
,
4810 UPL_ABORT_FREE_ON_EMPTY
);
4812 ubc_upl_commit_range(pl
, pl_offset
, size
,
4813 UPL_COMMIT_CLEAR_DIRTY
|
4814 UPL_COMMIT_FREE_ON_EMPTY
);
4821 * Vnode op for pageout using UPL
4822 * Derived from nfs_write()
4823 * File size changes are not permitted in pageout.
4827 struct vnop_pageout_args
/* {
4828 struct vnodeop_desc *a_desc;
4831 vm_offset_t a_pl_offset;
4835 vfs_context_t a_context;
4838 vnode_t vp
= ap
->a_vp
;
4839 upl_t pl
= ap
->a_pl
;
4840 size_t size
= ap
->a_size
;
4841 off_t f_offset
= ap
->a_f_offset
;
4842 vm_offset_t pl_offset
= ap
->a_pl_offset
;
4843 int flags
= ap
->a_flags
;
4844 struct nfsnode
*np
= VTONFS(vp
);
4848 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
4850 int error
= 0, iomode
, must_commit
;
4854 struct iovec_32 aiov
;
4855 int nofreeupl
= flags
& UPL_NOCOMMIT
;
4856 size_t biosize
, iosize
, pgsize
, xsize
;
4858 FSDBG(323, f_offset
, size
, pl
, pl_offset
);
4860 if (pl
== (upl_t
)NULL
)
4861 panic("nfs_pageout: no upl");
4863 if (UBCINVALID(vp
)) {
4864 printf("nfs_pageout: invalid vnode 0x%x", (int)vp
);
4866 ubc_upl_abort(pl
, 0);
4869 UBCINFOCHECK("nfs_pageout", vp
);
4872 printf("nfs_pageout: invalid size %d", size
);
4874 ubc_upl_abort(pl
, 0);
4880 ubc_upl_abort(pl
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
4883 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
4886 * Check to see whether the buffer is incore.
4887 * If incore and not busy, invalidate it from the cache.
4889 for (iosize
= 0; iosize
< size
; iosize
+= xsize
) {
4890 off
= f_offset
+ iosize
;
4891 /* need make sure we do things on block boundaries */
4892 xsize
= biosize
- (off
% biosize
);
4893 if (off
+ xsize
> f_offset
+ size
)
4894 xsize
= f_offset
+ size
- off
;
4895 lbn
= ubc_offtoblk(vp
, off
);
4896 lck_mtx_lock(nfs_buf_mutex
);
4897 if ((bp
= nfs_buf_incore(vp
, lbn
))) {
4898 FSDBG(323, off
, bp
, bp
->nb_lflags
, bp
->nb_flags
);
4899 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
4900 lck_mtx_unlock(nfs_buf_mutex
);
4901 /* no panic. just tell vm we are busy */
4903 ubc_upl_abort(pl
, 0);
4906 if (bp
->nb_dirtyend
> 0) {
4908 * if there's a dirty range in the buffer, check
4909 * to see if it extends beyond the pageout region
4911 * if the dirty region lies completely within the
4912 * pageout region, we just invalidate the buffer
4913 * because it's all being written out now anyway.
4915 * if any of the dirty region lies outside the
4916 * pageout region, we'll try to clip the dirty
4917 * region to eliminate the portion that's being
4918 * paged out. If that's not possible, because
4919 * the dirty region extends before and after the
4920 * pageout region, then we'll just return EBUSY.
4922 off_t boff
, start
, end
;
4926 /* clip end to EOF */
4927 if (end
> (off_t
)np
->n_size
)
4931 if ((bp
->nb_dirtyoff
< start
) &&
4932 (bp
->nb_dirtyend
> end
)) {
4933 /* not gonna be able to clip the dirty region */
4934 FSDBG(323, vp
, bp
, 0xd00deebc, EBUSY
);
4936 lck_mtx_unlock(nfs_buf_mutex
);
4938 ubc_upl_abort(pl
, 0);
4941 if ((bp
->nb_dirtyoff
< start
) ||
4942 (bp
->nb_dirtyend
> end
)) {
4943 /* clip dirty region, if necessary */
4944 if (bp
->nb_dirtyoff
< start
)
4945 bp
->nb_dirtyend
= min(bp
->nb_dirtyend
, start
);
4946 if (bp
->nb_dirtyend
> end
)
4947 bp
->nb_dirtyoff
= max(bp
->nb_dirtyoff
, end
);
4948 FSDBG(323, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, 0xd00dee00);
4949 /* we're leaving this block dirty */
4951 lck_mtx_unlock(nfs_buf_mutex
);
4955 nfs_buf_remfree(bp
);
4956 lck_mtx_unlock(nfs_buf_mutex
);
4957 SET(bp
->nb_flags
, NB_INVAL
);
4958 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
4959 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
4960 np
->n_needcommitcnt
--;
4961 CHECK_NEEDCOMMITCNT(np
);
4963 nfs_buf_release(bp
, 1);
4965 lck_mtx_unlock(nfs_buf_mutex
);
4969 cred
= ubc_getcred(vp
);
4971 cred
= vfs_context_ucred(ap
->a_context
);
4972 p
= vfs_context_proc(ap
->a_context
);
4974 if (np
->n_flag
& NWRITEERR
) {
4975 np
->n_flag
&= ~NWRITEERR
;
4977 ubc_upl_abort_range(pl
, pl_offset
, size
,
4978 UPL_ABORT_FREE_ON_EMPTY
);
4979 return (np
->n_error
);
4981 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) && !(nmp
->nm_state
& NFSSTA_GOTFSINFO
))
4982 nfs_fsinfo(nmp
, vp
, cred
, p
);
4984 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
||
4985 f_offset
& PAGE_MASK_64
|| size
& PAGE_MASK_64
) {
4987 ubc_upl_abort_range(pl
, pl_offset
, size
,
4988 UPL_ABORT_FREE_ON_EMPTY
);
4992 ubc_upl_map(pl
, &ioaddr
);
4993 ioaddr
+= pl_offset
;
4995 if ((u_quad_t
)f_offset
+ size
> np
->n_size
)
4996 xsize
= np
->n_size
- f_offset
;
5000 pgsize
= round_page_64(xsize
);
5001 if (size
> pgsize
) {
5003 ubc_upl_abort_range(pl
, pl_offset
+ pgsize
,
5005 UPL_ABORT_FREE_ON_EMPTY
);
5009 * check for partial page and clear the
5010 * contents past end of the file before
5011 * releasing it in the VM page cache
5013 if ((u_quad_t
)f_offset
< np
->n_size
&& (u_quad_t
)f_offset
+ size
> np
->n_size
) {
5014 size_t io
= np
->n_size
- f_offset
;
5015 bzero((caddr_t
)(ioaddr
+ io
), size
- io
);
5016 FSDBG(321, np
->n_size
, f_offset
, f_offset
+ io
, size
- io
);
5019 auio
.uio_offset
= f_offset
;
5020 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
5021 auio
.uio_segflg
= UIO_SYSSPACE
;
5023 auio
.uio_segflg
= UIO_SYSSPACE32
;
5025 auio
.uio_rw
= UIO_READ
;
5030 * It would be nice to be able to issue all these requests
5031 * in parallel instead of waiting for each one to complete
5032 * before sending the next one.
5033 * XXX Should we align these requests to block boundaries?
5035 iosize
= min(biosize
, xsize
);
5036 uio_uio_resid_set(&auio
, iosize
);
5037 aiov
.iov_len
= iosize
;
5038 aiov
.iov_base
= (uintptr_t)ioaddr
;
5039 auio
.uio_iovs
.iov32p
= &aiov
;
5040 auio
.uio_iovcnt
= 1;
5042 FSDBG(323, auio
.uio_offset
, uio_uio_resid(&auio
), ioaddr
, xsize
);
5043 OSAddAtomic(1, (SInt32
*)&nfsstats
.pageouts
);
5045 vnode_startwrite(vp
);
5047 /* NMODIFIED would be set here if doing unstable writes */
5048 iomode
= NFSV3WRITE_FILESYNC
;
5049 error
= nfs_writerpc(vp
, &auio
, cred
, p
, &iomode
, &must_commit
);
5051 nfs_clearcommit(vnode_mount(vp
));
5052 vnode_writedone(vp
);
5055 /* Note: no need to check uio_resid, because */
5056 /* it'll only be set if there was an error. */
5059 } while (xsize
> 0);
5064 * We've had several different solutions on what to do when the pageout
5065 * gets an error. If we don't handle it, and return an error to the
5066 * caller, vm, it will retry . This can end in endless looping
5067 * between vm and here doing retries of the same page. Doing a dump
5068 * back to vm, will get it out of vm's knowledge and we lose whatever
5069 * data existed. This is risky, but in some cases necessary. For
5070 * example, the initial fix here was to do that for ESTALE. In that case
5071 * the server is telling us that the file is no longer the same. We
5072 * would not want to keep paging out to that. We also saw some 151
5073 * errors from Auspex server and NFSv3 can return errors higher than
5074 * ELAST. Those along with NFS known server errors we will "dump" from
5075 * vm. Errors we don't expect to occur, we dump and log for further
5076 * analysis. Errors that could be transient, networking ones,
5077 * we let vm "retry". Lastly, errors that we retry, but may have potential
5078 * to storm the network, we "retrywithsleep". "sever" will be used in
5079 * in the future to dump all pages of object for cases like ESTALE.
5080 * All this is the basis for the states returned and first guesses on
5081 * error handling. Tweaking expected as more statistics are gathered.
5082 * Note, in the long run we may need another more robust solution to
5083 * have some kind of persistant store when the vm cannot dump nor keep
5084 * retrying as a solution, but this would be a file architectural change
5087 if (!nofreeupl
) { /* otherwise stacked file system has to handle this */
5090 short action
= nfs_pageouterrorhandler(error
);
5094 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
5097 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
5098 if (error
<= ELAST
&&
5099 (errorcount
[error
] % 100 == 0))
5100 printf("nfs_pageout: unexpected error %d. dumping vm page\n", error
);
5101 errorcount
[error
]++;
5104 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
5106 case RETRYWITHSLEEP
:
5107 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
5108 /* pri unused. PSOCK for placeholder. */
5109 tsleep(&lbolt
, PSOCK
, "nfspageout", 0);
5111 case SEVER
: /* not implemented */
5113 printf("nfs_pageout: action %d not expected\n", action
);
5117 ubc_upl_abort_range(pl
, pl_offset
, size
, abortflags
);
5118 /* return error in all cases above */
5121 ubc_upl_commit_range(pl
, pl_offset
, pgsize
,
5122 UPL_COMMIT_CLEAR_DIRTY
|
5123 UPL_COMMIT_FREE_ON_EMPTY
);
5128 /* Blktooff derives file offset given a logical block number */
5131 struct vnop_blktooff_args
/* {
5132 struct vnodeop_desc *a_desc;
5139 vnode_t vp
= ap
->a_vp
;
5140 mount_t mp
= vnode_mount(vp
);
5145 biosize
= vfs_statfs(mp
)->f_iosize
;
5147 *ap
->a_offset
= (off_t
)(ap
->a_lblkno
* biosize
);
5154 struct vnop_offtoblk_args
/* {
5155 struct vnodeop_desc *a_desc;
5158 daddr64_t *a_lblkno;
5162 vnode_t vp
= ap
->a_vp
;
5163 mount_t mp
= vnode_mount(vp
);
5168 biosize
= vfs_statfs(mp
)->f_iosize
;
5170 *ap
->a_lblkno
= (daddr64_t
)(ap
->a_offset
/ biosize
);