]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vnops.c
xnu-792.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vnops.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
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.
45 *
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
56 * SUCH DAMAGE.
57 *
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 $
60 */
61
62
63 /*
64 * vnode op calls for Sun NFS version 2 and 3
65 */
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>
75 #include <sys/conf.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>
81 #include <sys/attr.h>
82 #include <sys/signalvar.h>
83 #include <sys/uio_internal.h>
84
85 #include <vfs/vfs_support.h>
86
87 #include <sys/vm.h>
88
89 #include <sys/time.h>
90 #include <kern/clock.h>
91 #include <libkern/OSAtomic.h>
92
93 #include <miscfs/fifofs/fifo.h>
94 #include <miscfs/specfs/specdev.h>
95
96 #include <nfs/rpcv2.h>
97 #include <nfs/nfsproto.h>
98 #include <nfs/nfs.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>
104
105 #include <net/if.h>
106 #include <netinet/in.h>
107 #include <netinet/in_var.h>
108 #include <vm/vm_kern.h>
109
110 #include <kern/task.h>
111 #include <kern/sched_prim.h>
112
113 #include <sys/kdebug.h>
114
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)
124
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 *);
162
163 /*
164 * Global vfs data structures for nfs
165 */
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 */
204 { NULL, NULL }
205 };
206 struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
207 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
208 #ifdef __FreeBSD__
209 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
210 #endif
211
212 /*
213 * Special device vnode ops
214 */
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 */
251 { NULL, NULL }
252 };
253 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
254 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
255 #ifdef __FreeBSD__
256 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
257 #endif
258
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 */
295 { NULL, NULL }
296 };
297 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
298 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
299 #ifdef __FreeBSD__
300 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
301 #endif
302
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);
313
314 /*
315 * Global variables
316 */
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];
323
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;
328
329 int nfs_numasync = 0;
330 int nfs_ioddelwri = 0;
331
332 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
333
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");
337 */
338 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
339 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
340 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
341
342
343 /*
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
348 * erroneous.
349 */
350 enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, RETRYWITHSLEEP, SEVER};
351
352 static int errorcount[ELAST+1]; /* better be zeros when initialized */
353
354 static const short errortooutcome[ELAST+1] = {
355 NOACTION,
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 */
388 /* math software */
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 */
420
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 */
426 /* quotas & mush */
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 */
438
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 */
453 };
454
455
456 static short
457 nfs_pageouterrorhandler(int error)
458 {
459 if (error > ELAST)
460 return(DUMP);
461 else
462 return(errortooutcome[error]);
463 }
464
465 static int
466 nfs3_access_otw(vnode_t vp,
467 int wmode,
468 proc_t p,
469 kauth_cred_t cred)
470 {
471 const int v3 = 1;
472 u_long *tl;
473 int error = 0, attrflag;
474
475 mbuf_t mreq, mrep, md, mb, mb2;
476 caddr_t bpos, dpos, cp2;
477 register long t1, t2;
478 register caddr_t cp;
479 u_int32_t rmode;
480 struct nfsnode *np = VTONFS(vp);
481 u_int64_t xid;
482 struct timeval now;
483
484 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED);
485 if (error)
486 return (error);
487 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_ACCESS]);
488 nfsm_fhtom(vp, v3);
489 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
490 *tl = txdr_unsigned(wmode);
491 nfsm_request(vp, NFSPROC_ACCESS, p, cred, &xid);
492 if (mrep) {
493 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
494 }
495 if (!error) {
496 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
497 rmode = fxdr_unsigned(u_int32_t, *tl);
498 np->n_mode = rmode;
499 np->n_modeuid = kauth_cred_getuid(cred);
500 microuptime(&now);
501 np->n_modestamp = now.tv_sec;
502 }
503 nfsm_reqdone;
504 return error;
505 }
506
507 /*
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.
512 */
513 static int
514 nfs_access(ap)
515 struct vnop_access_args /* {
516 struct vnodeop_desc *a_desc;
517 vnode_t a_vp;
518 int a_mode;
519 vfs_context_t a_context;
520 } */ *ap;
521 {
522 vnode_t vp = ap->a_vp;
523 int error = 0, dorpc;
524 u_long mode, wmode;
525 int v3 = NFS_ISV3(vp);
526 struct nfsnode *np = VTONFS(vp);
527 struct timeval now;
528 kauth_cred_t cred;
529
530 /*
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
536 * in the cache.
537 */
538 if (v3) {
539 /*
540 * Convert KAUTH primitives to NFS access rights.
541 */
542 mode = 0;
543 if (vnode_isdir(vp)) {
544 /* directory */
545 if (ap->a_action &
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;
551 if (ap->a_action &
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;
557 } else {
558 /* file */
559 if (ap->a_action &
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;
569 }
570 /* common */
571 if (ap->a_action & KAUTH_VNODE_DELETE)
572 mode |= NFSV3ACCESS_DELETE;
573 if (ap->a_action &
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;
581
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;
587 } else
588 wmode = mode;
589
590 cred = vfs_context_ucred(ap->a_context);
591
592 /*
593 * Does our cached result allow us to give a definite yes to
594 * this request?
595 */
596 dorpc = 1;
597 if (NMODEVALID(np)) {
598 microuptime(&now);
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); */
603 dorpc = 0;
604 }
605 }
606 if (dorpc) {
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);
610 }
611 if (!error) {
612 /*
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.
618 */
619 if ((mode & NFSV3ACCESS_DELETE) &&
620 !(np->n_mode & NFSV3ACCESS_DELETE))
621 np->n_mode |= NFSV3ACCESS_DELETE;
622 if ((np->n_mode & mode) != mode)
623 error = EACCES;
624 }
625 } else {
626 /* v2 */
627 if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) && vfs_isrdonly(vnode_mount(vp))) {
628 error = EROFS;
629 } else {
630 error = 0;
631 }
632 }
633
634 return (error);
635 }
636
637 /*
638 * nfs open vnode op
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.
643 */
644 /* ARGSUSED */
645
646 static int
647 nfs_open(ap)
648 struct vnop_open_args /* {
649 struct vnodeop_desc *a_desc;
650 vnode_t a_vp;
651 int a_mode;
652 vfs_context_t a_context;
653 } */ *ap;
654 {
655 vnode_t vp = ap->a_vp;
656 struct nfsnode *np = VTONFS(vp);
657 struct nfs_vattr nvattr;
658 kauth_cred_t cred;
659 proc_t p;
660 enum vtype vtype;
661 int error;
662
663 vtype = vnode_vtype(vp);
664 if (vtype != VREG && vtype != VDIR && vtype != VLNK) {
665 return (EACCES);
666 }
667
668 cred = vfs_context_ucred(ap->a_context);
669 p = vfs_context_proc(ap->a_context);
670
671 if (np->n_flag & NNEEDINVALIDATE) {
672 np->n_flag &= ~NNEEDINVALIDATE;
673 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
674 }
675 if (np->n_flag & NMODIFIED) {
676 if ((error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1)) == EINTR)
677 return (error);
678 NATTRINVALIDATE(np);
679 if (vtype == VDIR)
680 np->n_direofoffset = 0;
681 error = nfs_getattr(vp, &nvattr, cred, p);
682 if (error)
683 return (error);
684 if (vtype == VDIR) {
685 /* if directory changed, purge any name cache entries */
686 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=))
687 cache_purge(vp);
688 np->n_ncmtime = nvattr.nva_mtime;
689 }
690 np->n_mtime = nvattr.nva_mtime;
691 } else {
692 error = nfs_getattr(vp, &nvattr, cred, p);
693 if (error)
694 return (error);
695 if (nfstimespeccmp(&np->n_mtime, &nvattr.nva_mtime, !=)) {
696 if (vtype == VDIR) {
697 np->n_direofoffset = 0;
698 nfs_invaldir(vp);
699 /* purge name cache entries */
700 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=))
701 cache_purge(vp);
702 }
703 if ((error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1)) == EINTR)
704 return (error);
705 if (vtype == VDIR)
706 np->n_ncmtime = nvattr.nva_mtime;
707 np->n_mtime = nvattr.nva_mtime;
708 }
709 }
710 NATTRINVALIDATE(np); /* For Open/Close consistency */
711 return (0);
712 }
713
714 /*
715 * nfs close vnode op
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
718 * two reasons:
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.)
730 *
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
734 * them.
735 */
736 /* ARGSUSED */
737 static int
738 nfs_close(ap)
739 struct vnop_close_args /* {
740 struct vnodeop_desc *a_desc;
741 vnode_t a_vp;
742 int a_fflag;
743 vfs_context_t a_context;
744 } */ *ap;
745 {
746 vnode_t vp = ap->a_vp;
747 struct nfsnode *np = VTONFS(vp);
748 struct nfsmount *nmp;
749 kauth_cred_t cred;
750 proc_t p;
751 int error = 0;
752
753 cred = vfs_context_ucred(ap->a_context);
754 p = vfs_context_proc(ap->a_context);
755
756 if (vnode_vtype(vp) == VREG) {
757 #if DIAGNOSTIC
758 register struct sillyrename *sp = np->n_sillyrename;
759 if (sp)
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);
763 #endif
764 nmp = VFSTONFS(vnode_mount(vp));
765 if (!nmp)
766 return (ENXIO);
767 if (np->n_flag & NNEEDINVALIDATE) {
768 np->n_flag &= ~NNEEDINVALIDATE;
769 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
770 }
771 if (np->n_flag & NMODIFIED) {
772 if (NFS_ISV3(vp)) {
773 error = nfs_flush(vp, MNT_WAIT, cred, p, 0);
774 /*
775 * We cannot clear the NMODIFIED bit in np->n_flag due to
776 * potential races with other processes
777 * NMODIFIED is a hint
778 */
779 /* np->n_flag &= ~NMODIFIED; */
780 } else {
781 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
782 }
783 NATTRINVALIDATE(np);
784 }
785 if (np->n_flag & NWRITEERR) {
786 np->n_flag &= ~NWRITEERR;
787 error = np->n_error;
788 }
789 }
790 return (error);
791 }
792
793
794 int
795 nfs_getattr_no_vnode(
796 mount_t mp,
797 u_char *fhp,
798 int fhsize,
799 kauth_cred_t cred,
800 proc_t p,
801 struct nfs_vattr *nvap,
802 u_int64_t *xidp)
803 {
804 mbuf_t mreq, mrep, md, mb, mb2;
805 caddr_t bpos, dpos;
806 int t2;
807 u_long *tl;
808 caddr_t cp;
809 struct nfsmount *nmp = VFSTONFS(mp);
810 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
811 int hsiz;
812 int error = 0;
813
814 // XXX fix this to use macros once the macros get cleaned up
815 //nfsm_reqhead(NFSX_FH(v3));
816 hsiz = NFSX_FH(v3);
817 mb = NULL;
818 if (hsiz >= nfs_mbuf_minclsize)
819 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mb);
820 else
821 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb);
822 if (error)
823 return (error);
824 bpos = mbuf_data(mb);
825 mreq = mb;
826 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_GETATTR]);
827 //nfsm_fhtom(vp, v3);
828 if (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))) {
836 error = t2;
837 mbuf_freem(mreq);
838 goto nfsmout;
839 }
840 } else {
841 nfsm_build(cp, caddr_t, NFSX_V2FH);
842 bcopy((caddr_t)fhp, cp, NFSX_V2FH);
843 }
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;
848 else
849 goto nfsmout;
850 }
851 if (!error) {
852 //nfsm_loadattr(vp, nvap, xidp);
853 error = nfs_parsefattr(&md, &dpos, v3, nvap);
854 if (error) {
855 mbuf_freem(mrep);
856 goto nfsmout;
857 }
858 }
859 nfsm_reqdone;
860 return (error);
861 }
862
863 /*
864 * nfs getattr call from vfs.
865 */
866 int
867 nfs_getattr(
868 vnode_t vp,
869 struct nfs_vattr *nvap,
870 kauth_cred_t cred,
871 proc_t p)
872 {
873 struct nfsnode *np = VTONFS(vp);
874 caddr_t cp;
875 u_long *tl;
876 int t1, t2;
877 caddr_t bpos, dpos;
878 int error = 0;
879 mbuf_t mreq, mrep, md, mb, mb2;
880 int v3;
881 u_int64_t xid;
882 int avoidfloods;
883
884 FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag);
885
886 /*
887 * Update local times for special files.
888 */
889 if (np->n_flag & (NACC | NUPD))
890 np->n_flag |= NCHG;
891 /*
892 * First look in the cache.
893 */
894 if ((error = nfs_getattrcache(vp, nvap)) == 0) {
895 FSDBG_BOT(513, np->n_size, 0, np->n_vattr.nva_size, np->n_flag);
896 return (0);
897 }
898 if (error != ENOENT) {
899 FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size,
900 np->n_flag);
901 return (error);
902 }
903
904 if (!VFSTONFS(vnode_mount(vp))) {
905 FSDBG_BOT(513, np->n_size, ENXIO, np->n_vattr.nva_size, np->n_flag);
906 return (ENXIO);
907 }
908 v3 = NFS_ISV3(vp);
909 error = 0;
910
911 /*
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.
916 */
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)))
921 return (error);
922 if ((error = nfs_getattrcache(vp, nvap)) == 0)
923 return (0);
924 if (error != ENOENT)
925 return (error);
926 error = 0;
927 }
928 avoidfloods = 0;
929 tryagain:
930 nfsm_reqhead(NFSX_FH(v3));
931 if (error) {
932 FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag);
933 return (error);
934 }
935 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_GETATTR]);
936 nfsm_fhtom(vp, v3);
937 nfsm_request(vp, NFSPROC_GETATTR, p, cred, &xid);
938 if (!error) {
939 nfsm_loadattr(vp, v3, nvap, &xid);
940 if (!xid) { /* out-of-order rpc - attributes were dropped */
941 mbuf_freem(mrep);
942 mrep = NULL;
943 FSDBG(513, -1, np, np->n_xid << 32, np->n_xid);
944 if (avoidfloods++ < 100)
945 goto tryagain;
946 /*
947 * avoidfloods>1 is bizarre. at 100 pull the plug
948 */
949 panic("nfs_getattr: getattr flood\n");
950 }
951 if (nfstimespeccmp(&np->n_mtime, &nvap->nva_mtime, !=)) {
952 enum vtype vtype = vnode_vtype(vp);
953 FSDBG(513, -1, np, -1, vp);
954 if (vtype == VDIR) {
955 nfs_invaldir(vp);
956 /* purge name cache entries */
957 if (nfstimespeccmp(&np->n_ncmtime, &nvap->nva_mtime, !=))
958 cache_purge(vp);
959 }
960 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
961 FSDBG(513, -1, np, -2, error);
962 if (!error) {
963 if (vtype == VDIR)
964 np->n_ncmtime = nvap->nva_mtime;
965 np->n_mtime = nvap->nva_mtime;
966 }
967 }
968 }
969 nfsm_reqdone;
970
971 FSDBG_BOT(513, np->n_size, -1, np->n_vattr.nva_size, error);
972 return (error);
973 }
974
975
976 static int
977 nfs_vnop_getattr(
978 struct vnop_getattr_args /* {
979 struct vnodeop_desc *a_desc;
980 vnode_t a_vp;
981 struct vnode_attr *a_vap;
982 vfs_context_t a_context;
983 } */ *ap)
984 {
985 int error;
986 struct nfs_vattr nva;
987 struct vnode_attr *vap = ap->a_vap;
988
989 error = nfs_getattr(ap->a_vp, &nva,
990 vfs_context_ucred(ap->a_context),
991 vfs_context_proc(ap->a_context));
992 if (error)
993 return (error);
994
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);
1016
1017 return (error);
1018 }
1019
1020 /*
1021 * nfs setattr call.
1022 */
1023 static int
1024 nfs_setattr(ap)
1025 struct vnop_setattr_args /* {
1026 struct vnodeop_desc *a_desc;
1027 vnode_t a_vp;
1028 struct vnode_attr *a_vap;
1029 vfs_context_t a_context;
1030 } */ *ap;
1031 {
1032 vnode_t vp = ap->a_vp;
1033 struct nfsnode *np = VTONFS(vp);
1034 struct vnode_attr *vap = ap->a_vap;
1035 int error = 0;
1036 u_quad_t tsize;
1037 kauth_cred_t cred;
1038 proc_t p;
1039
1040 #ifndef nolint
1041 tsize = (u_quad_t)0;
1042 #endif
1043
1044 /* Setting of flags is not supported. */
1045 if (VATTR_IS_ACTIVE(vap, va_flags))
1046 return (ENOTSUP);
1047
1048 cred = vfs_context_ucred(ap->a_context);
1049 p = vfs_context_proc(ap->a_context);
1050
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);
1057
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))
1064 return (EROFS);
1065
1066 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
1067 switch (vnode_vtype(vp)) {
1068 case VDIR:
1069 return (EISDIR);
1070 case VCHR:
1071 case VBLK:
1072 case VSOCK:
1073 case VFIFO:
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))
1079 return (0);
1080 VATTR_CLEAR_ACTIVE(vap, va_data_size);
1081 break;
1082 default:
1083 /*
1084 * Disallow write attempts if the filesystem is
1085 * mounted read-only.
1086 */
1087 if (vnode_vfsisrdonly(vp))
1088 return (EROFS);
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);
1094 else
1095 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1096 if (error) {
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);
1100 return (error);
1101 }
1102 } else if (np->n_size > vap->va_data_size) { /* shrinking? */
1103 daddr64_t obn, bn;
1104 int biosize, neweofoff, mustwrite;
1105 struct nfsbuf *bp;
1106
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))
1112 continue;
1113 error = nfs_buf_get(vp, obn, biosize, 0, NBLK_READ, &bp);
1114 if (error)
1115 continue;
1116 if (obn != bn) {
1117 FSDBG(512, bp, bp->nb_flags, 0, obn);
1118 SET(bp->nb_flags, NB_INVAL);
1119 nfs_buf_release(bp, 1);
1120 continue;
1121 }
1122 mustwrite = 0;
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;
1129 mustwrite++;
1130 }
1131 bp->nb_dirty &= (1 << round_page_32(neweofoff)/PAGE_SIZE) - 1;
1132 if (bp->nb_dirty)
1133 mustwrite++;
1134 if (!mustwrite) {
1135 FSDBG(512, bp, bp->nb_flags, 0, obn);
1136 SET(bp->nb_flags, NB_INVAL);
1137 nfs_buf_release(bp, 1);
1138 continue;
1139 }
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;
1148 }
1149 error = nfs_buf_write(bp);
1150 // Note: bp has been released
1151 if (error) {
1152 FSDBG(512, bp, 0xd00dee, 0xbad, error);
1153 np->n_error = error;
1154 np->n_flag |= NWRITEERR;
1155 /*
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)
1161 */
1162 NATTRINVALIDATE(np);
1163 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
1164 error = 0;
1165 }
1166 }
1167 }
1168 tsize = np->n_size;
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? */
1171 }
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);
1176 if (error == EINTR)
1177 return (error);
1178 }
1179 if (VATTR_IS_ACTIVE(vap, va_mode)) {
1180 NMODEINVALIDATE(np);
1181 }
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 */
1187
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);
1193 }
1194 return (error);
1195 }
1196
1197 /*
1198 * Do an nfs setattr rpc.
1199 */
1200 static int
1201 nfs_setattrrpc(vp, vap, cred, procp)
1202 vnode_t vp;
1203 struct vnode_attr *vap;
1204 kauth_cred_t cred;
1205 proc_t procp;
1206 {
1207 register struct nfsv2_sattr *sp;
1208 register caddr_t cp;
1209 register long t1, t2;
1210 caddr_t bpos, dpos, cp2;
1211 u_long *tl;
1212 int error = 0, wccpostattr = 0;
1213 mbuf_t mreq, mrep, md, mb, mb2;
1214 int v3;
1215 u_int64_t xid;
1216 struct timeval now;
1217
1218 if (!VFSTONFS(vnode_mount(vp)))
1219 return (ENXIO);
1220 v3 = NFS_ISV3(vp);
1221
1222 nfsm_reqhead(NFSX_FH(v3) + NFSX_SATTR(v3));
1223 if (error)
1224 return (error);
1225 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_SETATTR]);
1226 nfsm_fhtom(vp, v3);
1227 if (v3) {
1228 if (VATTR_IS_ACTIVE(vap, va_mode)) {
1229 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1230 *tl++ = nfs_true;
1231 *tl = txdr_unsigned(vap->va_mode);
1232 } else {
1233 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1234 *tl = nfs_false;
1235 }
1236 if (VATTR_IS_ACTIVE(vap, va_uid)) {
1237 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1238 *tl++ = nfs_true;
1239 *tl = txdr_unsigned(vap->va_uid);
1240 } else {
1241 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1242 *tl = nfs_false;
1243 }
1244 if (VATTR_IS_ACTIVE(vap, va_gid)) {
1245 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1246 *tl++ = nfs_true;
1247 *tl = txdr_unsigned(vap->va_gid);
1248 } else {
1249 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1250 *tl = nfs_false;
1251 }
1252 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
1253 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1254 *tl++ = nfs_true;
1255 txdr_hyper(&vap->va_data_size, tl);
1256 } else {
1257 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1258 *tl = nfs_false;
1259 }
1260 microtime(&now);
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);
1266 } else {
1267 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1268 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1269 }
1270 } else {
1271 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1272 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1273 }
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);
1279 } else {
1280 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1281 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1282 }
1283 } else {
1284 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1285 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1286 }
1287 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1288 *tl = nfs_false;
1289 } else {
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);
1294 else
1295 sp->sa_mode = nfs_xdrneg1;
1296 if (VATTR_IS_ACTIVE(vap, va_uid))
1297 sp->sa_uid = txdr_unsigned(vap->va_uid);
1298 else
1299 sp->sa_uid = nfs_xdrneg1;
1300 if (VATTR_IS_ACTIVE(vap, va_gid))
1301 sp->sa_gid = txdr_unsigned(vap->va_gid);
1302 else
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);
1306 else
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);
1310 } else {
1311 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1312 }
1313 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1314 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1315 } else {
1316 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1317 }
1318 }
1319 nfsm_request(vp, NFSPROC_SETATTR, procp, cred, &xid);
1320 if (v3) {
1321 struct timespec premtime = { 0, 0 };
1322 if (mrep) {
1323 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
1324 }
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;
1328 }
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;
1333 }
1334 if (!wccpostattr)
1335 NATTRINVALIDATE(VTONFS(vp));
1336 } else {
1337 if (mrep) {
1338 nfsm_loadattr(vp, v3, NULL, &xid);
1339 }
1340 }
1341 nfsm_reqdone;
1342 return (error);
1343 }
1344
1345 /*
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
1349 */
1350 static int
1351 nfs_lookup(ap)
1352 struct vnop_lookup_args /* {
1353 struct vnodeop_desc *a_desc;
1354 vnode_t a_dvp;
1355 vnode_t *a_vpp;
1356 struct componentname *a_cnp;
1357 vfs_context_t a_context;
1358 } */ *ap;
1359 {
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;
1364 vnode_t newvp;
1365 u_long *tl;
1366 caddr_t cp;
1367 long t1, t2;
1368 caddr_t bpos, dpos, cp2;
1369 mbuf_t mreq, mrep, md, mb, mb2;
1370 long len;
1371 u_char *fhp;
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;
1377 kauth_cred_t cred;
1378 proc_t p;
1379 int ngflags;
1380
1381 *vpp = NULLVP;
1382
1383 cred = vfs_context_ucred(ap->a_context);
1384 p = vfs_context_proc(ap->a_context);
1385
1386 wantparent = flags & (LOCKPARENT|WANTPARENT);
1387 dnp = VTONFS(dvp);
1388
1389 error = nfs_getattr(dvp, &nvattr, cred, p);
1390 if (error)
1391 goto error_return;
1392 if (nfstimespeccmp(&dnp->n_ncmtime, &nvattr.nva_mtime, !=)) {
1393 /*
1394 * This directory has changed on us.
1395 * Purge any name cache entries.
1396 */
1397 cache_purge(dvp);
1398 dnp->n_ncmtime = nvattr.nva_mtime;
1399 }
1400
1401 error = cache_lookup(dvp, vpp, cnp);
1402 switch (error) {
1403 case ENOENT:
1404 /* negative cache entry same as cache miss */
1405 error = 0;
1406 /* FALLTHROUGH */
1407 case 0:
1408 /* cache miss */
1409 break;
1410 case -1:
1411 /* cache hit, not really an error */
1412 {
1413 struct vnop_access_args naa;
1414
1415 OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_hits);
1416
1417 /* check for directory access */
1418 naa.a_vp = dvp;
1419 naa.a_action = KAUTH_VNODE_SEARCH;
1420 naa.a_context = ap->a_context;
1421
1422 /* compute actual success/failure based on accessibility */
1423 error = nfs_access(&naa);
1424 }
1425 /* FALLTHROUGH */
1426 default:
1427 /* unexpected error from cache_lookup */
1428 goto error_return;
1429 }
1430
1431 /* check for lookup of "." */
1432 if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
1433 /* skip lookup, we know who we are */
1434 fhisdvp = 1;
1435 fhp = NULL;
1436 fhsize = 0;
1437 mrep = NULL;
1438 goto found;
1439 }
1440
1441 /* do we know this name is too long? */
1442 if (v3) {
1443 /* For NFSv3: need uniform pathconf info to test pc_namemax */
1444 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1445 if (!nmp) {
1446 error = ENXIO;
1447 goto error_return;
1448 }
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;
1454 goto error_return;
1455 }
1456 } else if (cnp->cn_namelen > NFS_MAXNAMLEN) {
1457 error = ENAMETOOLONG;
1458 goto error_return;
1459 }
1460
1461 error = 0;
1462 newvp = NULLVP;
1463
1464 OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_misses);
1465 len = cnp->cn_namelen;
1466 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1467 if (error)
1468 goto error_return;
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);
1474
1475 if (error) {
1476 if (mrep) {
1477 nfsm_postop_attr_update(dvp, v3, dattrflag, &xid);
1478 mbuf_freem(mrep);
1479 }
1480 goto nfsmout;
1481 }
1482
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);
1487
1488 /* get attributes */
1489 if (v3) {
1490 dxid = xid;
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);
1498 if (error) {
1499 mbuf_freem(mrep);
1500 goto error_return;
1501 }
1502 }
1503 } else {
1504 nfsm_attr_get(v3, &nvattr);
1505 }
1506
1507 found:
1508
1509 /*
1510 * Handle RENAME case...
1511 */
1512 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
1513 if (fhisdvp) {
1514 mbuf_freem(mrep);
1515 error = EISDIR;
1516 goto error_return;
1517 }
1518 error = nfs_nget(vnode_mount(dvp), dvp, cnp, fhp, fhsize,
1519 &nvattr, &xid, 0, &np);
1520 if (error) {
1521 mbuf_freem(mrep);
1522 goto error_return;
1523 }
1524 *vpp = NFSTOV(np);
1525 mbuf_freem(mrep);
1526
1527 goto error_return;
1528 }
1529
1530 if ((cnp->cn_flags & MAKEENTRY) &&
1531 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)))
1532 ngflags = NG_MAKEENTRY;
1533 else
1534 ngflags = 0;
1535
1536 if (fhisdvp) {
1537 error = vnode_get(dvp);
1538 if (error) {
1539 mbuf_freem(mrep);
1540 goto error_return;
1541 }
1542 newvp = 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);
1546 if (error) {
1547 vnode_put(dvp);
1548 mbuf_freem(mrep);
1549 goto error_return;
1550 }
1551 }
1552 } else {
1553 error = nfs_nget(vnode_mount(dvp), dvp, cnp, fhp, fhsize,
1554 &nvattr, &xid, ngflags, &np);
1555 if (error) {
1556 mbuf_freem(mrep);
1557 goto error_return;
1558 }
1559 newvp = NFSTOV(np);
1560 }
1561 *vpp = newvp;
1562 // if (error == 0 && *vpp != NULL && *vpp != dvp)
1563 // nfs_unlock(VTONFS(*vpp));
1564
1565 nfsm_reqdone;
1566 if (error) {
1567 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1568 (flags & ISLASTCN) && error == ENOENT) {
1569 if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp))
1570 error = EROFS;
1571 else
1572 error = EJUSTRETURN;
1573 }
1574 }
1575 error_return:
1576 if (error && *vpp) {
1577 vnode_put(*vpp);
1578 *vpp = NULLVP;
1579 }
1580 return (error);
1581 }
1582
1583 /*
1584 * nfs read call.
1585 * Just call nfs_bioread() to do the work.
1586 */
1587 static int
1588 nfs_read(ap)
1589 struct vnop_read_args /* {
1590 struct vnodeop_desc *a_desc;
1591 vnode_t a_vp;
1592 struct uio *a_uio;
1593 int a_ioflag;
1594 vfs_context_t a_context;
1595 } */ *ap;
1596 {
1597 if (vnode_vtype(ap->a_vp) != VREG)
1598 return (EPERM);
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)));
1602 }
1603
1604
1605 /*
1606 * nfs readlink call
1607 */
1608 static int
1609 nfs_readlink(ap)
1610 struct vnop_readlink_args /* {
1611 struct vnodeop_desc *a_desc;
1612 vnode_t a_vp;
1613 struct uio *a_uio;
1614 vfs_context_t a_context;
1615 } */ *ap;
1616 {
1617 if (vnode_vtype(ap->a_vp) != VLNK)
1618 return (EPERM);
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)));
1622 }
1623
1624 /*
1625 * Do a readlink rpc.
1626 * Called by nfs_doio() from below the buffer cache.
1627 */
1628 int
1629 nfs_readlinkrpc(
1630 vnode_t vp,
1631 struct uio *uiop,
1632 kauth_cred_t cred,
1633 proc_t p)
1634 {
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;
1641 int v3;
1642 u_int64_t xid;
1643
1644 if (!VFSTONFS(vnode_mount(vp)))
1645 return (ENXIO);
1646 v3 = NFS_ISV3(vp);
1647
1648 nfsm_reqhead(NFSX_FH(v3));
1649 if (error)
1650 return (error);
1651 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READLINK]);
1652 nfsm_fhtom(vp, v3);
1653 nfsm_request(vp, NFSPROC_READLINK, p, cred, &xid);
1654 if (v3 && mrep)
1655 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
1656 if (!error) {
1657 nfsm_strsiz(len, NFS_MAXPATHLEN, v3);
1658 if (len >= NFS_MAXPATHLEN) {
1659 struct nfsnode *np = VTONFS(vp);
1660 #if DIAGNOSTIC
1661 if (!np)
1662 panic("nfs_readlinkrpc: null np");
1663 #endif
1664 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1665 len = np->n_size;
1666 }
1667 nfsm_mtouio(uiop, len);
1668 }
1669 nfsm_reqdone;
1670 return (error);
1671 }
1672
1673 /*
1674 * nfs read rpc call
1675 * Ditto above
1676 */
1677 int
1678 nfs_readrpc(
1679 vnode_t vp,
1680 struct uio *uiop,
1681 kauth_cred_t cred,
1682 proc_t p)
1683 {
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;
1691 int v3, nmrsize;
1692 u_int64_t xid;
1693
1694 FSDBG_TOP(536, vp, uiop->uio_offset, uio_uio_resid(uiop), 0);
1695 nmp = VFSTONFS(vnode_mount(vp));
1696 if (!nmp)
1697 return (ENXIO);
1698 v3 = NFS_ISV3(vp);
1699 nmrsize = nmp->nm_rsize;
1700
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);
1705 return (EFBIG);
1706 }
1707 while (tsiz > 0) {
1708 len = (tsiz > nmrsize) ? nmrsize : tsiz;
1709 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1710 if (error)
1711 break;
1712 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READ]);
1713 nfsm_fhtom(vp, v3);
1714 nfsm_build(tl, u_long *, NFSX_UNSIGNED * 3);
1715 if (v3) {
1716 txdr_hyper(&uiop->uio_offset, tl);
1717 *(tl + 2) = txdr_unsigned(len);
1718 } else {
1719 *tl++ = txdr_unsigned(uiop->uio_offset);
1720 *tl++ = txdr_unsigned(len);
1721 *tl = 0;
1722 }
1723 FSDBG(536, vp, uiop->uio_offset, len, 0);
1724 nfsm_request(vp, NFSPROC_READ, p, cred, &xid);
1725 if (v3) {
1726 if (mrep) {
1727 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
1728 }
1729 if (error) {
1730 mbuf_freem(mrep);
1731 goto nfsmout;
1732 }
1733 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
1734 eof = fxdr_unsigned(int, *(tl + 1));
1735 } else {
1736 if (mrep) {
1737 nfsm_loadattr(vp, v3, NULL, &xid);
1738 }
1739 }
1740 if (mrep) {
1741 nfsm_strsiz(retlen, nmrsize, 0);
1742 nfsm_mtouio(uiop, retlen);
1743 mbuf_freem(mrep);
1744 } else {
1745 retlen = 0;
1746 }
1747 tsiz -= retlen;
1748 if (v3) {
1749 if (eof || retlen == 0)
1750 tsiz = 0;
1751 } else if (retlen < len)
1752 tsiz = 0;
1753 }
1754 nfsmout:
1755 FSDBG_BOT(536, vp, eof, uio_uio_resid(uiop), error);
1756 return (error);
1757 }
1758
1759 /*
1760 * nfs write call
1761 */
1762 int
1763 nfs_writerpc(
1764 vnode_t vp,
1765 struct uio *uiop,
1766 kauth_cred_t cred,
1767 proc_t p,
1768 int *iomode,
1769 int *must_commit)
1770 {
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;
1779 u_int64_t xid;
1780 mount_t mp;
1781
1782 #if DIAGNOSTIC
1783 if (uiop->uio_iovcnt != 1)
1784 panic("nfs_writerpc: iovcnt > 1");
1785 #endif
1786 FSDBG_TOP(537, vp, uiop->uio_offset, uio_uio_resid(uiop), *iomode);
1787 nmp = VFSTONFS(vnode_mount(vp));
1788 if (!nmp)
1789 return (ENXIO);
1790 v3 = NFS_ISV3(vp);
1791 *must_commit = 0;
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);
1796 return (EFBIG);
1797 }
1798 while (tsiz > 0) {
1799 nmp = VFSTONFS(vnode_mount(vp));
1800 if (!nmp) {
1801 error = ENXIO;
1802 break;
1803 }
1804 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1805 nfsm_reqhead(NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1806 if (error)
1807 break;
1808 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_WRITE]);
1809 nfsm_fhtom(vp, v3);
1810 if (v3) {
1811 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1812 txdr_hyper(&uiop->uio_offset, tl);
1813 tl += 2;
1814 *tl++ = txdr_unsigned(len);
1815 *tl++ = txdr_unsigned(*iomode);
1816 } else {
1817 nfsm_build(tl, u_long *, 4 * NFSX_UNSIGNED);
1818 *++tl = txdr_unsigned(uiop->uio_offset);
1819 tl += 2;
1820 }
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));
1826 if (!nmp)
1827 error = ENXIO;
1828 if (v3) {
1829 if (mrep) {
1830 struct timespec premtime;
1831 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
1832 if (nfstimespeccmp(&VTONFS(vp)->n_mtime, &premtime, ==))
1833 updatemtime = 1;
1834 }
1835 if (!error) {
1836 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED +
1837 NFSX_V3WRITEVERF);
1838 rlen = fxdr_unsigned(int, *tl++);
1839 if (rlen <= 0) {
1840 error = NFSERR_IO;
1841 break;
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);
1848 len = rlen;
1849 }
1850 commit = fxdr_unsigned(int, *tl++);
1851
1852 /*
1853 * Return the lowest committment level
1854 * obtained by any of the RPCs.
1855 */
1856 if (committed == NFSV3WRITE_FILESYNC)
1857 committed = commit;
1858 else if (committed == NFSV3WRITE_DATASYNC &&
1859 commit == NFSV3WRITE_UNSTABLE)
1860 committed = commit;
1861 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1862 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1863 NFSX_V3WRITEVERF);
1864 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1865 } else if (bcmp((caddr_t)tl,
1866 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1867 *must_commit = 1;
1868 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1869 NFSX_V3WRITEVERF);
1870 }
1871 }
1872 } else {
1873 if (mrep) {
1874 nfsm_loadattr(vp, v3, NULL, &xid);
1875 }
1876 }
1877
1878 if (updatemtime)
1879 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.nva_mtime;
1880 mbuf_freem(mrep);
1881 /*
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.
1886 * Let's try it.
1887 */
1888 if (error)
1889 break;
1890 tsiz -= len;
1891 }
1892 nfsmout:
1893 if ((mp = vnode_mount(vp)) && (vfs_flags(mp) & MNT_ASYNC))
1894 committed = NFSV3WRITE_FILESYNC;
1895 *iomode = committed;
1896 if (error)
1897 uio_uio_resid_set(uiop, tsiz);
1898 FSDBG_BOT(537, vp, committed, uio_uio_resid(uiop), error);
1899 return (error);
1900 }
1901
1902 /*
1903 * nfs mknod rpc
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.
1906 */
1907 static int
1908 nfs_mknodrpc(
1909 vnode_t dvp,
1910 vnode_t *vpp,
1911 struct componentname *cnp,
1912 struct vnode_attr *vap,
1913 kauth_cred_t cred,
1914 proc_t p)
1915 {
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;
1923 char *cp2;
1924 caddr_t bpos, dpos;
1925 int error = 0, wccpostattr = 0, gotvp = 0;
1926 struct timespec premtime = { 0, 0 };
1927 mbuf_t mreq, mrep, md, mb, mb2;
1928 u_long rdev;
1929 u_int64_t xid;
1930 int v3 = NFS_ISV3(dvp);
1931 int gotuid, gotgid;
1932
1933 if (!VATTR_IS_ACTIVE(vap, va_type))
1934 return (EINVAL);
1935 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1936 if (!VATTR_IS_ACTIVE(vap, va_rdev))
1937 return (EINVAL);
1938 rdev = txdr_unsigned(vap->va_rdev);
1939 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1940 rdev = 0xffffffff;
1941 else {
1942 return (ENOTSUP);
1943 }
1944 nfsm_reqhead(NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1945 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1946 if (error)
1947 return (error);
1948
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);
1957
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);
1961 if (v3) {
1962 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1963 *tl++ = vtonfsv3_type(vap->va_type);
1964 nfsm_v3sattr(vap);
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));
1969 }
1970 } else {
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;
1977 sp->sa_size = rdev;
1978 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1979 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
1980 } else {
1981 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1982 }
1983 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1984 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1985 } else {
1986 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1987 }
1988 }
1989 nfsm_request(dvp, NFSPROC_MKNOD, p, cred, &xid);
1990 /* XXX no EEXIST kludge here? */
1991 if (!error) {
1992 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
1993 if (!gotvp) {
1994 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1995 cnp->cn_namelen, cred, p, &np);
1996 if (!error)
1997 newvp = NFSTOV(np);
1998 }
1999 }
2000 if (v3 && mrep)
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);
2009 }
2010 nfsm_reqdone;
2011 if (error) {
2012 if (newvp)
2013 vnode_put(newvp);
2014 } else {
2015 *vpp = newvp;
2016 }
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;
2021 if (!wccpostattr)
2022 NATTRINVALIDATE(VTONFS(dvp));
2023 return (error);
2024 }
2025
2026 /*
2027 * nfs mknod vop
2028 * just call nfs_mknodrpc() to do the work.
2029 */
2030 /* ARGSUSED */
2031 static int
2032 nfs_mknod(ap)
2033 struct vnop_mknod_args /* {
2034 struct vnodeop_desc *a_desc;
2035 vnode_t a_dvp;
2036 vnode_t *a_vpp;
2037 struct componentname *a_cnp;
2038 struct vnode_attr *a_vap;
2039 vfs_context_t a_context;
2040 } */ *ap;
2041 {
2042 int error;
2043
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));
2047
2048 return (error);
2049 }
2050
2051 static u_long create_verf;
2052 /*
2053 * nfs file create call
2054 */
2055 static int
2056 nfs_create(ap)
2057 struct vnop_create_args /* {
2058 struct vnodeop_desc *a_desc;
2059 vnode_t a_dvp;
2060 vnode_t *a_vpp;
2061 struct componentname *a_cnp;
2062 struct vnode_attr *a_vap;
2063 vfs_context_t a_context;
2064 } */ *ap;
2065 {
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;
2071 u_long *tl;
2072 caddr_t cp;
2073 long t1, t2;
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);
2081 int gotuid, gotgid;
2082 u_int64_t xid;
2083 kauth_cred_t cred;
2084 proc_t p;
2085
2086 cred = vfs_context_ucred(ap->a_context);
2087 p = vfs_context_proc(ap->a_context);
2088
2089 if (!VATTR_IS_ACTIVE(vap, va_type))
2090 return (EINVAL);
2091
2092 /*
2093 * Oops, not for me..
2094 */
2095 if (vap->va_type == VSOCK)
2096 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap, cred, p));
2097
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);
2106
2107 if (vap->va_vaflags & VA_EXCLUSIVE)
2108 fmode |= O_EXCL;
2109 again:
2110 nfsm_reqhead(NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
2111 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
2112 if (error)
2113 return (error);
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);
2117 if (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;
2124 else
2125 *tl++ = create_verf;
2126 *tl = ++create_verf;
2127 } else {
2128 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
2129 nfsm_v3sattr(vap);
2130 }
2131 } else {
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;
2138 sp->sa_size = 0;
2139 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2140 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2141 } else {
2142 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2143 }
2144 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2145 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2146 } else {
2147 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2148 }
2149 }
2150 nfsm_request(dvp, NFSPROC_CREATE, p, cred, &xid);
2151 if (!error) {
2152 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2153 if (!gotvp) {
2154 error = nfs_lookitup(dvp, cnp->cn_nameptr,
2155 cnp->cn_namelen, cred, p, &np);
2156 if (!error)
2157 newvp = NFSTOV(np);
2158 }
2159 }
2160 if (v3 && mrep)
2161 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2162 nfsm_reqdone;
2163 if (error) {
2164 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
2165 fmode &= ~O_EXCL;
2166 goto again;
2167 }
2168 if (newvp)
2169 vnode_put(newvp);
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);
2178 }
2179 if (error)
2180 vnode_put(newvp);
2181 }
2182 if (!error) {
2183 *ap->a_vpp = newvp;
2184 }
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;
2189 if (!wccpostattr)
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);
2198 }
2199 return (error);
2200 }
2201
2202 /*
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
2210 * else
2211 * do the remove rpc
2212 */
2213 static int
2214 nfs_remove(ap)
2215 struct vnop_remove_args /* {
2216 struct vnodeop_desc *a_desc;
2217 vnode_t a_dvp;
2218 vnode_t a_vp;
2219 struct componentname *a_cnp;
2220 int a_flags;
2221 vfs_context_t a_context;
2222 } */ *ap;
2223 {
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;
2230 kauth_cred_t cred;
2231 proc_t p;
2232
2233 cred = vfs_context_ucred(ap->a_context);
2234 p = vfs_context_proc(ap->a_context);
2235
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 */
2239 return (EBUSY);
2240 }
2241 if (gofree || (np->n_sillyrename &&
2242 nfs_getattr(vp, &nvattr, cred, p) == 0 &&
2243 nvattr.nva_nlink > 1)) {
2244 /*
2245 * Purge the name cache so that the chance of a lookup for
2246 * the name succeeding while the remove is in progress is
2247 * minimized.
2248 */
2249 cache_purge(vp);
2250 /*
2251 * throw away biocache buffers, mainly to avoid
2252 * unnecessary delayed writes later.
2253 */
2254 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
2255 np->n_size = 0;
2256 ubc_setsize(vp, (off_t)0); /* XXX check error */
2257 /* Do the rpc */
2258 if (error != EINTR)
2259 error = nfs_removerpc(dvp, cnp->cn_nameptr,
2260 cnp->cn_namelen, cred, p);
2261 /*
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.
2266 */
2267 if (error == ENOENT)
2268 error = 0;
2269 if (!error) {
2270 /*
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
2274 */
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);
2279 }
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);
2284 vnode_recycle(vp);
2285 }
2286 } else if (!np->n_sillyrename) {
2287 error = nfs_sillyrename(dvp, vp, cnp, cred, p);
2288 }
2289 NATTRINVALIDATE(np);
2290
2291 return (error);
2292 }
2293
2294 /*
2295 * nfs file remove rpc called from nfs_inactive
2296 */
2297 int
2298 nfs_removeit(struct sillyrename *sp)
2299 {
2300 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, NULL));
2301 }
2302
2303 /*
2304 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
2305 */
2306 static int
2307 nfs_removerpc(dvp, name, namelen, cred, proc)
2308 vnode_t dvp;
2309 char *name;
2310 int namelen;
2311 kauth_cred_t cred;
2312 proc_t proc;
2313 {
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;
2321 int v3;
2322 u_int64_t xid;
2323
2324 if (!VFSTONFS(vnode_mount(dvp)))
2325 return (ENXIO);
2326 v3 = NFS_ISV3(dvp);
2327
2328 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
2329 if (error)
2330 return (error);
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);
2335 if (v3 && mrep)
2336 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2337 nfsm_reqdone;
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;
2342 if (!wccpostattr)
2343 NATTRINVALIDATE(VTONFS(dvp));
2344 return (error);
2345 }
2346
2347 /*
2348 * nfs file rename call
2349 */
2350 static int
2351 nfs_rename(ap)
2352 struct vnop_rename_args /* {
2353 struct vnodeop_desc *a_desc;
2354 vnode_t a_fdvp;
2355 vnode_t a_fvp;
2356 struct componentname *a_fcnp;
2357 vnode_t a_tdvp;
2358 vnode_t a_tvp;
2359 struct componentname *a_tcnp;
2360 vfs_context_t a_context;
2361 } */ *ap;
2362 {
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;
2369 int error, inuse=0;
2370 mount_t fmp, tdmp, tmp;
2371 struct nfsnode *tnp;
2372 kauth_cred_t cred;
2373 proc_t p;
2374
2375 cred = vfs_context_ucred(ap->a_context);
2376 p = vfs_context_proc(ap->a_context);
2377
2378 tnp = tvp ? VTONFS(tvp) : NULL;
2379
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))) {
2385 error = EXDEV;
2386 goto out;
2387 }
2388
2389 /*
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)
2395 */
2396 if (tvp && tvp != fvp) {
2397 inuse = vnode_isinuse(tvp, 0);
2398 }
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. */
2403 } else {
2404 /* sillyrename succeeded.*/
2405 tvp = NULL;
2406 }
2407 }
2408
2409 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2410 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, cred, p);
2411
2412 /*
2413 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2414 */
2415 if (error == ENOENT)
2416 error = 0;
2417
2418 if (!error && tvp && tvp != fvp && !tnp->n_sillyrename) {
2419 /*
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
2423 */
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);
2428 }
2429
2430 /* purge the old name cache entries and enter the new one */
2431 cache_purge(fvp);
2432 if (tvp) {
2433 cache_purge(tvp);
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);
2438 vnode_recycle(tvp);
2439 }
2440 }
2441 if (!error)
2442 cache_enter(tdvp, fvp, tcnp);
2443
2444 out:
2445 /*
2446 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2447 */
2448 if (error == ENOENT)
2449 error = 0;
2450 return (error);
2451 }
2452
2453 /*
2454 * Do an nfs rename rpc. Called from nfs_rename() and nfs_sillyrename().
2455 */
2456 static int
2457 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
2458 vnode_t fdvp;
2459 char *fnameptr;
2460 int fnamelen;
2461 vnode_t tdvp;
2462 char *tnameptr;
2463 int tnamelen;
2464 kauth_cred_t cred;
2465 proc_t proc;
2466 {
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;
2474 int v3;
2475 u_int64_t xid;
2476
2477 if (!VFSTONFS(vnode_mount(fdvp)))
2478 return (ENXIO);
2479 v3 = NFS_ISV3(fdvp);
2480
2481 nfsm_reqhead((NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
2482 nfsm_rndup(tnamelen));
2483 if (error)
2484 return (error);
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);
2491 if (v3 && mrep) {
2492 u_int64_t txid = xid;
2493
2494 nfsm_wcc_data(fdvp, &fpremtime, fwccpostattr, &xid);
2495 nfsm_wcc_data(tdvp, &tpremtime, twccpostattr, &txid);
2496 }
2497 nfsm_reqdone;
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;
2502 if (!fwccpostattr)
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;
2508 if (!twccpostattr)
2509 NATTRINVALIDATE(VTONFS(tdvp));
2510 return (error);
2511 }
2512
2513 /*
2514 * nfs hard link create call
2515 */
2516 static int
2517 nfs_link(ap)
2518 struct vnop_link_args /* {
2519 struct vnodeop_desc *a_desc;
2520 vnode_t a_vp;
2521 vnode_t a_tdvp;
2522 struct componentname *a_cnp;
2523 vfs_context_t a_context;
2524 } */ *ap;
2525 {
2526 vnode_t vp = ap->a_vp;
2527 vnode_t tdvp = ap->a_tdvp;
2528 struct componentname *cnp = ap->a_cnp;
2529 u_long *tl;
2530 caddr_t cp;
2531 long t1, t2;
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;
2536 int v3;
2537 u_int64_t xid;
2538 kauth_cred_t cred;
2539 proc_t p;
2540
2541 if (vnode_mount(vp) != vnode_mount(tdvp)) {
2542 return (EXDEV);
2543 }
2544
2545 cred = vfs_context_ucred(ap->a_context);
2546 p = vfs_context_proc(ap->a_context);
2547
2548 v3 = NFS_ISV3(vp);
2549
2550 /*
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!
2554 */
2555 nfs_flush(vp, MNT_WAIT, cred, p, 0);
2556
2557 nfsm_reqhead(NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2558 if (error)
2559 return (error);
2560 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LINK]);
2561 nfsm_fhtom(vp, v3);
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);
2565 if (v3 && mrep) {
2566 u_int64_t txid = xid;
2567
2568 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
2569 nfsm_wcc_data(tdvp, &premtime, wccpostattr, &txid);
2570 }
2571 nfsm_reqdone;
2572
2573 VTONFS(tdvp)->n_flag |= NMODIFIED;
2574 if (!attrflag)
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;
2579 if (!wccpostattr)
2580 NATTRINVALIDATE(VTONFS(tdvp));
2581 /*
2582 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2583 */
2584 if (error == EEXIST)
2585 error = 0;
2586 return (error);
2587 }
2588
2589 /*
2590 * nfs symbolic link create call
2591 */
2592 static int
2593 nfs_symlink(ap)
2594 struct vnop_symlink_args /* {
2595 struct vnodeop_desc *a_desc;
2596 vnode_t a_dvp;
2597 vnode_t *a_vpp;
2598 struct componentname *a_cnp;
2599 struct vnode_attr *a_vap;
2600 char *a_target;
2601 vfs_context_t a_context;
2602 } */ *ap;
2603 {
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;
2609 u_long *tl;
2610 caddr_t cp;
2611 long t1, t2;
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);
2618 int gotuid, gotgid;
2619 u_int64_t xid;
2620 kauth_cred_t cred;
2621 proc_t p;
2622 struct nfsnode *np = NULL;
2623
2624 cred = vfs_context_ucred(ap->a_context);
2625 p = vfs_context_proc(ap->a_context);
2626
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));
2630 if (error)
2631 return (error);
2632
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);
2641
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);
2645 if (v3) {
2646 nfsm_v3sattr(vap);
2647 }
2648 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN, v3);
2649 if (!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);
2659 } else {
2660 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2661 }
2662 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2663 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2664 } else {
2665 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2666 }
2667 }
2668 nfsm_request(dvp, NFSPROC_SYMLINK, p, cred, &xid);
2669 if (v3 && mrep) {
2670 u_int64_t dxid = xid;
2671
2672 if (!error)
2673 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2674 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
2675 }
2676 nfsm_reqdone;
2677
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;
2682 if (!wccpostattr)
2683 NATTRINVALIDATE(VTONFS(dvp));
2684
2685 /*
2686 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2687 * if we can succeed in looking up the symlink.
2688 */
2689 if ((error == EEXIST) || (!error && !gotvp)) {
2690 if (newvp) {
2691 vnode_put(newvp);
2692 newvp = NULL;
2693 }
2694 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, cred, p, &np);
2695 if (!error) {
2696 newvp = NFSTOV(np);
2697 if (vnode_vtype(newvp) != VLNK)
2698 error = EEXIST;
2699 }
2700 }
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);
2708 }
2709 if (error) {
2710 if (newvp)
2711 vnode_put(newvp);
2712 } else {
2713 *ap->a_vpp = newvp;
2714 }
2715 return (error);
2716 }
2717
2718 /*
2719 * nfs make dir call
2720 */
2721 static int
2722 nfs_mkdir(ap)
2723 struct vnop_mkdir_args /* {
2724 struct vnodeop_desc *a_desc;
2725 vnode_t a_dvp;
2726 vnode_t *a_vpp;
2727 struct componentname *a_cnp;
2728 struct vnode_attr *a_vap;
2729 vfs_context_t a_context;
2730 } */ *ap;
2731 {
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;
2737 u_long *tl;
2738 caddr_t cp;
2739 long t1, t2;
2740 int len;
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 };
2746 int gotvp = 0;
2747 mbuf_t mreq, mrep, md, mb, mb2;
2748 int v3 = NFS_ISV3(dvp);
2749 int gotuid, gotgid;
2750 u_int64_t xid, dxid;
2751 kauth_cred_t cred;
2752 proc_t p;
2753
2754 cred = vfs_context_ucred(ap->a_context);
2755 p = vfs_context_proc(ap->a_context);
2756
2757 len = cnp->cn_namelen;
2758 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2759 if (error)
2760 return (error);
2761
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);
2770
2771 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKDIR]);
2772 nfsm_fhtom(dvp, v3);
2773 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN, v3);
2774 if (v3) {
2775 nfsm_v3sattr(vap);
2776 } else {
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);
2786 } else {
2787 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2788 }
2789 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2790 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2791 } else {
2792 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2793 }
2794 }
2795 nfsm_request(dvp, NFSPROC_MKDIR, p, cred, &xid);
2796 dxid = xid;
2797 if (!error)
2798 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2799 if (v3 && mrep)
2800 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
2801 nfsm_reqdone;
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;
2806 if (!wccpostattr)
2807 NATTRINVALIDATE(VTONFS(dvp));
2808 /*
2809 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2810 * if we can succeed in looking up the directory.
2811 */
2812 if (error == EEXIST || (!error && !gotvp)) {
2813 if (newvp) {
2814 vnode_put(newvp);
2815 newvp = NULL;
2816 }
2817 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cred, p, &np);
2818 if (!error) {
2819 newvp = NFSTOV(np);
2820 if (vnode_vtype(newvp) != VDIR)
2821 error = EEXIST;
2822 }
2823 }
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);
2831 }
2832 if (error) {
2833 if (newvp)
2834 vnode_put(newvp);
2835 } else {
2836 *ap->a_vpp = newvp;
2837 }
2838 return (error);
2839 }
2840
2841 /*
2842 * nfs remove directory call
2843 */
2844 static int
2845 nfs_rmdir(ap)
2846 struct vnop_rmdir_args /* {
2847 struct vnodeop_desc *a_desc;
2848 vnode_t a_dvp;
2849 vnode_t a_vp;
2850 struct componentname *a_cnp;
2851 vfs_context_t a_context;
2852 } */ *ap;
2853 {
2854 vnode_t vp = ap->a_vp;
2855 vnode_t dvp = ap->a_dvp;
2856 struct componentname *cnp = ap->a_cnp;
2857 u_long *tl;
2858 caddr_t cp;
2859 long t1, t2;
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);
2865 u_int64_t xid;
2866 kauth_cred_t cred;
2867 proc_t p;
2868
2869 cred = vfs_context_ucred(ap->a_context);
2870 p = vfs_context_proc(ap->a_context);
2871
2872 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2873 if (error)
2874 return (error);
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);
2879 if (v3 && mrep)
2880 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2881 nfsm_reqdone;
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;
2886 if (!wccpostattr)
2887 NATTRINVALIDATE(VTONFS(dvp));
2888 cache_purge(vp);
2889 /*
2890 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2891 */
2892 if (error == ENOENT)
2893 error = 0;
2894 if (!error) {
2895 /*
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
2899 */
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);
2904 }
2905 return (error);
2906 }
2907
2908 /*
2909 * nfs readdir call
2910 */
2911 static int
2912 nfs_readdir(ap)
2913 struct vnop_readdir_args /* {
2914 struct vnodeop_desc *a_desc;
2915 vnode_t a_vp;
2916 struct uio *a_uio;
2917 int *a_eofflag;
2918 int *a_ncookies;
2919 u_long **a_cookies;
2920 vfs_context_t a_context;
2921 } */ *ap;
2922 {
2923 vnode_t vp = ap->a_vp;
2924 struct nfsnode *np = VTONFS(vp);
2925 struct uio *uio = ap->a_uio;
2926 int tresid, error;
2927 struct nfs_vattr nvattr;
2928 kauth_cred_t cred;
2929 proc_t p;
2930
2931 if (vnode_vtype(vp) != VDIR)
2932 return (EPERM);
2933
2934 cred = vfs_context_ucred(ap->a_context);
2935 p = vfs_context_proc(ap->a_context);
2936
2937 /*
2938 * First, check for hit on the EOF offset cache
2939 */
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);
2945 return (0);
2946 }
2947 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=)) {
2948 /* directory changed, purge any name cache entries */
2949 cache_purge(vp);
2950 }
2951 }
2952 }
2953
2954 /*
2955 * Call nfs_bioread() to do the real work.
2956 */
2957 // LP64todo - fix this
2958 tresid = uio_uio_resid(uio);
2959 error = nfs_bioread(vp, uio, 0, cred, p);
2960
2961 if (!error && uio_uio_resid(uio) == tresid)
2962 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_misses);
2963 return (error);
2964 }
2965
2966 /*
2967 * Readdir rpc call.
2968 * Called from below the buffer cache by nfs_doio().
2969 */
2970 int
2971 nfs_readdirrpc(
2972 vnode_t vp,
2973 struct uio *uiop,
2974 kauth_cred_t cred,
2975 proc_t p)
2976 {
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;
2985 nfsuint64 cookie;
2986 struct nfsmount *nmp;
2987 struct nfsnode *dnp = VTONFS(vp);
2988 u_quad_t fileno;
2989 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2990 int attrflag;
2991 int v3, nmreaddirsize;
2992 u_int64_t xid;
2993
2994 #ifndef nolint
2995 dp = (struct dirent *)0;
2996 #endif
2997 #if DIAGNOSTIC
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");
3001 #endif
3002 nmp = VFSTONFS(vnode_mount(vp));
3003 if (!nmp)
3004 return (ENXIO);
3005 v3 = NFS_ISV3(vp);
3006 nmreaddirsize = nmp->nm_readdirsize;
3007
3008 /*
3009 * If there is no cookie, assume directory was stale.
3010 */
3011 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3012 if (cookiep)
3013 cookie = *cookiep;
3014 else
3015 return (NFSERR_BAD_COOKIE);
3016 /*
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.
3020 */
3021 while (more_dirs && bigenough) {
3022 nfsm_reqhead(NFSX_FH(v3) + NFSX_READDIR(v3));
3023 if (error)
3024 goto nfsmout;
3025 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIR]);
3026 nfsm_fhtom(vp, v3);
3027 if (v3) {
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];
3033 } else {
3034 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
3035 *tl++ = cookie.nfsuquad[0];
3036 }
3037 *tl = txdr_unsigned(nmreaddirsize);
3038 nfsm_request(vp, NFSPROC_READDIR, p, cred, &xid);
3039 if (v3) {
3040 if (mrep) {
3041 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
3042 }
3043 if (!error) {
3044 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3045 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3046 dnp->n_cookieverf.nfsuquad[1] = *tl;
3047 } else {
3048 mbuf_freem(mrep);
3049 goto nfsmout;
3050 }
3051 } else if (!mrep) {
3052 // XXX assert error?
3053 goto nfsmout;
3054 }
3055 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3056 more_dirs = fxdr_unsigned(int, *tl);
3057
3058 /* loop thru the dir entries, doctoring them to 4bsd form */
3059 while (more_dirs && bigenough) {
3060 if (v3) {
3061 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3062 fxdr_hyper(tl, &fileno);
3063 len = fxdr_unsigned(int, *(tl + 2));
3064 } else {
3065 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3066 fileno = fxdr_unsigned(u_quad_t, *tl++);
3067 len = fxdr_unsigned(int, *tl);
3068 }
3069 /* Note: v3 supports longer names, but struct dirent doesn't */
3070 /* so we just truncate the names to fit */
3071 if (len <= 0) {
3072 error = EBADRPC;
3073 mbuf_freem(mrep);
3074 goto nfsmout;
3075 }
3076 if (len > MAXNAMLEN) {
3077 skiplen = len - MAXNAMLEN;
3078 len = MAXNAMLEN;
3079 } else {
3080 skiplen = 0;
3081 }
3082 tlen = nfsm_rndup(len);
3083 if (tlen == 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);
3092 blksiz = 0;
3093 }
3094 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
3095 bigenough = 0;
3096 if (bigenough) {
3097 // LP64todo - fix this!
3098 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3099 dp->d_fileno = (int)fileno;
3100 dp->d_namlen = len;
3101 dp->d_reclen = tlen + DIRHDSIZ;
3102 dp->d_type = DT_UNKNOWN;
3103 blksiz += dp->d_reclen;
3104 if (blksiz == DIRBLKSIZ)
3105 blksiz = 0;
3106 uiop->uio_offset += DIRHDSIZ;
3107 #if LP64KERN
3108 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3109 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3110 #else
3111 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3112 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3113 #endif
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));
3118 tlen -= len;
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);
3124 } else {
3125 nfsm_adv(nfsm_rndup(len));
3126 }
3127 if (skiplen)
3128 nfsm_adv(nfsm_rndup(skiplen));
3129 if (v3) {
3130 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3131 } else {
3132 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3133 }
3134 if (bigenough) {
3135 cookie.nfsuquad[0] = *tl++;
3136 if (v3)
3137 cookie.nfsuquad[1] = *tl++;
3138 } else if (v3)
3139 tl += 2;
3140 else
3141 tl++;
3142 more_dirs = fxdr_unsigned(int, *tl);
3143 }
3144 /*
3145 * If at end of rpc data, get the eof boolean
3146 */
3147 if (!more_dirs) {
3148 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3149 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3150 }
3151 mbuf_freem(mrep);
3152 }
3153 /*
3154 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3155 * by increasing d_reclen for the last record.
3156 */
3157 if (blksiz > 0) {
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);
3164 }
3165
3166 /*
3167 * We are now either at the end of the directory or have filled the
3168 * block.
3169 */
3170 if (bigenough)
3171 dnp->n_direofoffset = uiop->uio_offset;
3172 else {
3173 if (uio_uio_resid(uiop) > 0)
3174 printf("EEK! readdirrpc resid > 0\n");
3175 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
3176 if (cookiep)
3177 *cookiep = cookie;
3178 }
3179 nfsmout:
3180 return (error);
3181 }
3182
3183 /*
3184 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
3185 */
3186 int
3187 nfs_readdirplusrpc(
3188 vnode_t vp,
3189 struct uio *uiop,
3190 kauth_cred_t cred,
3191 proc_t p)
3192 {
3193 int len, skiplen, left;
3194 struct dirent *dp;
3195 u_long *tl;
3196 caddr_t cp;
3197 long t1, t2;
3198 vnode_t newvp;
3199 nfsuint64 *cookiep;
3200 caddr_t bpos, dpos, cp2;
3201 mbuf_t mreq, mrep, md, mb, mb2;
3202 struct componentname cn, *cnp = &cn;
3203 nfsuint64 cookie;
3204 struct nfsmount *nmp;
3205 struct nfsnode *dnp = VTONFS(vp), *np;
3206 u_char *fhp;
3207 u_quad_t fileno;
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;
3212
3213 #ifndef nolint
3214 dp = (struct dirent *)0;
3215 #endif
3216 #if DIAGNOSTIC
3217 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
3218 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
3219 panic("nfs_readdirplusrpc: bad uio");
3220 #endif
3221 nmp = VFSTONFS(vnode_mount(vp));
3222 if (!nmp)
3223 return (ENXIO);
3224 nmreaddirsize = nmp->nm_readdirsize;
3225 nmrsize = nmp->nm_rsize;
3226
3227 bzero(cnp, sizeof(*cnp));
3228 newvp = NULLVP;
3229
3230 /*
3231 * If there is no cookie, assume directory was stale.
3232 */
3233 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3234 if (cookiep)
3235 cookie = *cookiep;
3236 else
3237 return (NFSERR_BAD_COOKIE);
3238 /*
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.
3242 */
3243 while (more_dirs && bigenough) {
3244 nfsm_reqhead(NFSX_FH(1) + 6 * NFSX_UNSIGNED);
3245 if (error)
3246 goto nfsmout;
3247 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIRPLUS]);
3248 nfsm_fhtom(vp, 1);
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);
3257 savexid = xid;
3258 if (mrep) {
3259 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
3260 }
3261 if (error) {
3262 mbuf_freem(mrep);
3263 goto nfsmout;
3264 }
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);
3269
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 */
3277 if (len <= 0) {
3278 error = EBADRPC;
3279 mbuf_freem(mrep);
3280 goto nfsmout;
3281 }
3282 if (len > MAXNAMLEN) {
3283 skiplen = len - MAXNAMLEN;
3284 len = MAXNAMLEN;
3285 } else {
3286 skiplen = 0;
3287 }
3288 tlen = nfsm_rndup(len);
3289 if (tlen == 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);
3298 blksiz = 0;
3299 }
3300 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
3301 bigenough = 0;
3302 if (bigenough) {
3303 // LP64todo - fix this!
3304 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3305 dp->d_fileno = (int)fileno;
3306 dp->d_namlen = len;
3307 dp->d_reclen = tlen + DIRHDSIZ;
3308 dp->d_type = DT_UNKNOWN;
3309 blksiz += dp->d_reclen;
3310 if (blksiz == DIRBLKSIZ)
3311 blksiz = 0;
3312 uiop->uio_offset += DIRHDSIZ;
3313 #if LP64KERN
3314 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3315 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3316 #else
3317 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3318 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3319 #endif
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));
3326 tlen -= len;
3327 *cp = '\0';
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);
3332 } else {
3333 nfsm_adv(nfsm_rndup(len));
3334 }
3335 if (skiplen)
3336 nfsm_adv(nfsm_rndup(skiplen));
3337 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3338 if (bigenough) {
3339 cookie.nfsuquad[0] = *tl++;
3340 cookie.nfsuquad[1] = *tl++;
3341 } else
3342 tl += 2;
3343
3344 /*
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.
3348 */
3349 attrflag = fxdr_unsigned(int, *tl);
3350 if (attrflag) {
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);
3357 if (doit) {
3358 nfsm_getfh(fhp, fhsize, 1);
3359 if (NFS_CMPFH(dnp, fhp, fhsize)) {
3360 error = vnode_ref(vp);
3361 if (error) {
3362 doit = 0;
3363 } else {
3364 newvp = vp;
3365 np = dnp;
3366 }
3367 } else if (!bigenough ||
3368 (cnp->cn_namelen == 2 &&
3369 cnp->cn_nameptr[1] == '.' &&
3370 cnp->cn_nameptr[0] == '.')) {
3371 /*
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.
3380 */
3381 doit = 0;
3382 } else {
3383 cnp->cn_hash = 0;
3384
3385 error = nfs_nget(vnode_mount(vp), vp, cnp,
3386 fhp, fhsize, &nvattr, &xid,
3387 NG_MAKEENTRY, &np);
3388 if (error)
3389 doit = 0;
3390 else
3391 newvp = NFSTOV(np);
3392 }
3393 }
3394 /* update attributes if not already updated */
3395 if (doit && bigenough && (np->n_xid <= savexid)) {
3396 xid = savexid;
3397 nfs_loadattrcache(np, &nvattr, &xid, 0);
3398 /* any error can be ignored */
3399 }
3400 } else {
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));
3405 }
3406 if (newvp != NULLVP) {
3407 if (newvp == vp)
3408 vnode_rele(newvp);
3409 else
3410 vnode_put(newvp);
3411 newvp = NULLVP;
3412 }
3413 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3414 more_dirs = fxdr_unsigned(int, *tl);
3415 }
3416 /*
3417 * If at end of rpc data, get the eof boolean
3418 */
3419 if (!more_dirs) {
3420 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3421 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3422 }
3423 mbuf_freem(mrep);
3424 }
3425 /*
3426 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
3427 * by increasing d_reclen for the last record.
3428 */
3429 if (blksiz > 0) {
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);
3436 }
3437
3438 /*
3439 * We are now either at the end of the directory or have filled the
3440 * block.
3441 */
3442 if (bigenough)
3443 dnp->n_direofoffset = uiop->uio_offset;
3444 else {
3445 if (uio_uio_resid(uiop) > 0)
3446 printf("EEK! readdirplusrpc resid > 0\n");
3447 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
3448 if (cookiep)
3449 *cookiep = cookie;
3450 }
3451 nfsmout:
3452 return (error);
3453 }
3454
3455 /*
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...
3462 */
3463
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";
3467
3468 static int
3469 nfs_sillyrename(
3470 vnode_t dvp,
3471 vnode_t vp,
3472 struct componentname *cnp,
3473 kauth_cred_t cred,
3474 proc_t p)
3475 {
3476 register struct sillyrename *sp;
3477 struct nfsnode *np;
3478 int error;
3479 short pid;
3480 kauth_cred_t tmpcred;
3481 int i, j, k;
3482
3483 cache_purge(vp);
3484 np = VTONFS(vp);
3485 #if DIAGNOSTIC
3486 if (vnode_vtype(vp) == VDIR)
3487 panic("nfs_sillyrename: dir");
3488 #endif
3489 MALLOC_ZONE(sp, struct sillyrename *,
3490 sizeof (struct sillyrename), M_NFSREQ, M_WAITOK);
3491 if (!sp)
3492 return (ENOMEM);
3493 kauth_cred_ref(cred);
3494 sp->s_cred = cred;
3495 sp->s_dvp = dvp;
3496 error = vnode_ref(dvp);
3497 if (error)
3498 goto bad_norele;
3499
3500 /* Fudge together a funny name */
3501 pid = proc_pid(p);
3502 sp->s_namlen = sprintf(sp->s_name, sillyrename_name, pid);
3503
3504 /* Try lookitups until we get one that isn't there */
3505 i = j = k = 0;
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)) {
3510 i = 0;
3511 if (sp->s_name[5]++ >= 'z')
3512 sp->s_name[5] = 'A';
3513 if (++j > ('z' - 'A' + 1)) {
3514 j = 0;
3515 if (sp->s_name[6]++ >= 'z')
3516 sp->s_name[6] = 'A';
3517 if (++k > ('z' - 'A' + 1)) {
3518 error = EINVAL;
3519 goto bad;
3520 }
3521 }
3522 }
3523 }
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';
3531 }
3532 }
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);
3536 if (error)
3537 goto bad;
3538 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, &np);
3539 #if DIAGNOSTIC
3540 kprintf("sillyrename: %s, vp=%x, np=%x, dvp=%x\n",
3541 &sp->s_name[0], (unsigned)vp, (unsigned)np, (unsigned)dvp);
3542 #endif
3543 np->n_sillyrename = sp;
3544 return (0);
3545 bad:
3546 vnode_rele(sp->s_dvp);
3547 bad_norele:
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);
3552 return (error);
3553 }
3554
3555 /*
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
3560 * handled too
3561 * *npp != NULL --> update the file handle in the vnode
3562 */
3563 static int
3564 nfs_lookitup(dvp, name, len, cred, procp, npp)
3565 vnode_t dvp;
3566 char *name;
3567 int len;
3568 kauth_cred_t cred;
3569 proc_t procp;
3570 struct nfsnode **npp;
3571 {
3572 u_long *tl;
3573 caddr_t cp;
3574 long t1, t2;
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;
3580 u_char *nfhp;
3581 int v3;
3582 u_int64_t xid, dxid, savedxid;
3583 struct nfs_vattr nvattr;
3584
3585 if (!VFSTONFS(vnode_mount(dvp)))
3586 return (ENXIO);
3587 v3 = NFS_ISV3(dvp);
3588
3589 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3590 if (error)
3591 return (error);
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) {
3597 savedxid = xid;
3598 nfsm_getfh(nfhp, fhlen, v3);
3599 /* get attributes */
3600 if (v3) {
3601 nfsm_postop_attr_get(v3, attrflag, &nvattr);
3602 if (!attrflag) {
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);
3607 if (error) {
3608 mbuf_freem(mrep);
3609 goto nfsmout;
3610 }
3611 }
3612 dxid = savedxid;
3613 nfsm_postop_attr_update(dvp, v3, attrflag, &dxid);
3614 } else {
3615 nfsm_attr_get(v3, &nvattr);
3616 }
3617 if (*npp) {
3618 np = *npp;
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);
3623 if (!np->n_fhp) {
3624 np->n_fhp = oldbuf;
3625 error = ENOMEM;
3626 mbuf_freem(mrep);
3627 goto nfsmout;
3628 }
3629 } else {
3630 np->n_fhp = &np->n_fh[0];
3631 }
3632 if (oldbuf) {
3633 FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH);
3634 }
3635 }
3636 bcopy(nfhp, np->n_fhp, fhlen);
3637 np->n_fhsize = fhlen;
3638 newvp = NFSTOV(np);
3639 error = nfs_loadattrcache(np, &nvattr, &xid, 0);
3640 if (error) {
3641 mbuf_freem(mrep);
3642 goto nfsmout;
3643 }
3644 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3645 newvp = dvp;
3646 if (dnp->n_xid <= savedxid) {
3647 dxid = savedxid;
3648 error = nfs_loadattrcache(dnp, &nvattr, &dxid, 0);
3649 if (error) {
3650 mbuf_freem(mrep);
3651 goto nfsmout;
3652 }
3653 }
3654 } else {
3655 struct componentname cn, *cnp = &cn;
3656 bzero(cnp, sizeof(*cnp));
3657 cnp->cn_nameptr = name;
3658 cnp->cn_namelen = len;
3659
3660 error = nfs_nget(vnode_mount(dvp), dvp, cnp, nfhp, fhlen,
3661 &nvattr, &xid, NG_MAKEENTRY, &np);
3662 if (error) {
3663 mbuf_freem(mrep);
3664 return (error);
3665 }
3666 newvp = NFSTOV(np);
3667 }
3668 }
3669 nfsm_reqdone;
3670 if (npp && *npp == NULL) {
3671 if (error) {
3672 if (newvp) {
3673 if (newvp == dvp)
3674 vnode_rele(newvp);
3675 else
3676 vnode_put(newvp);
3677 }
3678 } else
3679 *npp = np;
3680 }
3681 return (error);
3682 }
3683
3684 /*
3685 * Nfs Version 3 commit rpc
3686 */
3687 int
3688 nfs_commit(vp, offset, count, cred, procp)
3689 vnode_t vp;
3690 u_quad_t offset;
3691 u_int32_t count;
3692 kauth_cred_t cred;
3693 proc_t procp;
3694 {
3695 caddr_t cp;
3696 u_long *tl;
3697 int t1, t2;
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;
3703 u_int64_t xid;
3704
3705 FSDBG(521, vp, offset, count, nmp->nm_state);
3706 if (!nmp)
3707 return (ENXIO);
3708 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3709 return (0);
3710 nfsm_reqhead(NFSX_FH(1));
3711 if (error)
3712 return (error);
3713 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_COMMIT]);
3714 nfsm_fhtom(vp, 1);
3715 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
3716 txdr_hyper(&offset, tl);
3717 tl += 2;
3718 *tl = txdr_unsigned(count);
3719 nfsm_request(vp, NFSPROC_COMMIT, procp, cred, &xid);
3720 if (mrep) {
3721 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
3722 /* XXX can we do anything useful with the wcc info? */
3723 }
3724 if (!error) {
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,
3729 NFSX_V3WRITEVERF);
3730 error = NFSERR_STALEWRITEVERF;
3731 }
3732 }
3733 nfsm_reqdone;
3734 return (error);
3735 }
3736
3737 static int
3738 nfs_blockmap(
3739 __unused struct vnop_blockmap_args /* {
3740 struct vnodeop_desc *a_desc;
3741 vnode_t a_vp;
3742 off_t a_foffset;
3743 size_t a_size;
3744 daddr64_t *a_bpn;
3745 size_t *a_run;
3746 void *a_poff;
3747 int a_flags;
3748 } */ *ap)
3749 {
3750 return (ENOTSUP);
3751 }
3752
3753 /*
3754 * Mmap a file
3755 *
3756 * NB Currently unsupported.
3757 */
3758 /*ARGSUSED*/
3759 static int
3760 nfs_mmap(
3761 __unused struct vnop_mmap_args /* {
3762 struct vnodeop_desc *a_desc;
3763 vnode_t a_vp;
3764 int a_fflags;
3765 kauth_cred_t a_cred;
3766 proc_t a_p;
3767 } */ *ap)
3768 {
3769
3770 return (EINVAL);
3771 }
3772
3773 /*
3774 * fsync vnode op. Just call nfs_flush() with commit == 1.
3775 */
3776 /* ARGSUSED */
3777 static int
3778 nfs_fsync(ap)
3779 struct vnop_fsync_args /* {
3780 struct vnodeop_desc *a_desc;
3781 vnode_t a_vp;
3782 int a_waitfor;
3783 vfs_context_t a_context;
3784 } */ *ap;
3785 {
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);
3789 int error;
3790
3791 np->n_flag |= NWRBUSY;
3792 error = nfs_flush(ap->a_vp, ap->a_waitfor, cred, p, 0);
3793 np->n_flag &= ~NWRBUSY;
3794 return (error);
3795 }
3796
3797 int
3798 nfs_flushcommits(vnode_t vp, proc_t p, int nowait)
3799 {
3800 struct nfsnode *np = VTONFS(vp);
3801 struct nfsbuf *bp;
3802 struct nfsbuflists blist, commitlist;
3803 int error = 0, retv, wcred_set, flags;
3804 u_quad_t off, endoff, toff;
3805 u_int32_t count;
3806 kauth_cred_t wcred = NULL;
3807
3808 FSDBG_TOP(557, vp, np, 0, 0);
3809
3810 /*
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.
3815 */
3816 if (!LIST_EMPTY(&np->n_dirtyblkhd))
3817 np->n_flag |= NMODIFIED;
3818
3819 off = (u_quad_t)-1;
3820 endoff = 0;
3821 wcred_set = 0;
3822 LIST_INIT(&commitlist);
3823
3824 if (!VFSTONFS(vnode_mount(vp))) {
3825 error = ENXIO;
3826 goto done;
3827 }
3828 if (!NFS_ISV3(vp)) {
3829 error = EINVAL;
3830 goto done;
3831 }
3832
3833 flags = NBI_DIRTY;
3834 if (nowait)
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);
3842 if (error)
3843 continue;
3844 if (((bp->nb_flags & (NB_DELWRI | NB_NEEDCOMMIT))
3845 != (NB_DELWRI | NB_NEEDCOMMIT))) {
3846 nfs_buf_drop(bp);
3847 continue;
3848 }
3849 nfs_buf_remfree(bp);
3850 lck_mtx_unlock(nfs_buf_mutex);
3851 /*
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
3855 */
3856 if (!ISSET(bp->nb_flags, NB_PAGELIST)) {
3857 retv = nfs_buf_upl_setup(bp);
3858 if (retv) {
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;
3865 }
3866 }
3867 nfs_buf_upl_check(bp);
3868 lck_mtx_lock(nfs_buf_mutex);
3869
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);
3873
3874 /*
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
3877 * written again.
3878 *
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.
3882 */
3883
3884 /*
3885 * Work out if all buffers are using the same cred
3886 * so we can deal with them all with one commit.
3887 *
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.
3890 */
3891 if (wcred_set == 0) {
3892 wcred = bp->nb_wcred;
3893 if (wcred == NOCRED)
3894 panic("nfs: needcommit w/out wcred");
3895 wcred_set = 1;
3896 } else if ((wcred_set == 1) && wcred != bp->nb_wcred) {
3897 wcred_set = -1;
3898 }
3899 SET(bp->nb_flags, NB_WRITEINPROG);
3900
3901 /*
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.
3907 */
3908 LIST_REMOVE(bp, nb_vnbufs);
3909 LIST_INSERT_HEAD(&commitlist, bp, nb_vnbufs);
3910 toff = NBOFF(bp) + bp->nb_dirtyoff;
3911 if (toff < off)
3912 off = toff;
3913 toff += (u_quad_t)(bp->nb_dirtyend - bp->nb_dirtyoff);
3914 if (toff > endoff)
3915 endoff = toff;
3916 }
3917 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
3918 }
3919 lck_mtx_unlock(nfs_buf_mutex);
3920
3921 if (LIST_EMPTY(&commitlist)) {
3922 error = ENOBUFS;
3923 goto done;
3924 }
3925
3926 /*
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
3930 * separately.
3931 */
3932 if (wcred_set == 1) {
3933 /*
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.
3936 */
3937 if ((endoff - off) > 0xffffffff)
3938 count = 0;
3939 else
3940 count = (endoff - off);
3941 retv = nfs_commit(vp, off, count, wcred, p);
3942 } else {
3943 retv = 0;
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);
3948 if (retv)
3949 break;
3950 }
3951 }
3952 if (retv == NFSERR_STALEWRITEVERF)
3953 nfs_clearcommit(vnode_mount(vp));
3954
3955 /*
3956 * Now, either mark the blocks I/O done or mark the
3957 * blocks dirty, depending on whether the commit
3958 * succeeded.
3959 */
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);
3966
3967 if (retv) {
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);
3973 continue;
3974 }
3975
3976 vnode_startwrite(vp);
3977 if (ISSET(bp->nb_flags, NB_DELWRI)) {
3978 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
3979 NFSBUFCNTCHK(0);
3980 wakeup(&nfs_nbdwrite);
3981 }
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. */
3985 if (!bp->nb_dirty)
3986 SET(bp->nb_flags, NB_ASYNC);
3987
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);
3992
3993 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
3994
3995 nfs_buf_iodone(bp);
3996 if (bp->nb_dirty) {
3997 /* throw it back in as a delayed write buffer */
3998 CLR(bp->nb_flags, NB_DONE);
3999 nfs_buf_write_delayed(bp, p);
4000 }
4001 }
4002
4003 done:
4004 FSDBG_BOT(557, vp, np, 0, error);
4005 return (error);
4006 }
4007
4008 /*
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.
4012 */
4013 int
4014 nfs_flush(
4015 vnode_t vp,
4016 int waitfor,
4017 __unused kauth_cred_t cred,
4018 proc_t p,
4019 int ignore_writeerr)
4020 {
4021 struct nfsnode *np = VTONFS(vp);
4022 struct nfsbuf *bp;
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;
4027
4028 FSDBG_TOP(517, vp, np, waitfor, 0);
4029
4030 if (!nmp) {
4031 error = ENXIO;
4032 goto done;
4033 }
4034 if (nmp->nm_flag & NFSMNT_INT)
4035 slpflag = PCATCH;
4036
4037 /*
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.
4043 */
4044 again:
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);
4051 error = ENXIO;
4052 goto done;
4053 }
4054
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)
4062 nfs_buf_refget(bp);
4063 while ((error = nfs_buf_acquire(bp, flags, slpflag, slptimeo))) {
4064 FSDBG(524, bp, flags, bp->nb_lflags, bp->nb_flags);
4065 if (error == EBUSY)
4066 break;
4067 if (error) {
4068 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4069 if (error2) {
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);
4074 error = error2;
4075 goto done;
4076 }
4077 if (slpflag == PCATCH) {
4078 slpflag = 0;
4079 slptimeo = 2 * hz;
4080 }
4081 }
4082 }
4083 if (flags != NBAC_NOWAIT)
4084 nfs_buf_refrele(bp);
4085 if (error == EBUSY)
4086 continue;
4087 if (!bp->nb_vp) {
4088 /* buffer is no longer valid */
4089 nfs_buf_drop(bp);
4090 continue;
4091 }
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)) {
4097 nfs_buf_drop(bp);
4098 continue;
4099 }
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);
4107 continue;
4108 }
4109 SET(bp->nb_flags, NB_ASYNC);
4110 if (!passone) {
4111 /* NB_STABLE forces this to be written FILESYNC */
4112 SET(bp->nb_flags, NB_STABLE);
4113 }
4114 nfs_buf_write(bp);
4115 lck_mtx_lock(nfs_buf_mutex);
4116 }
4117 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
4118 }
4119 lck_mtx_unlock(nfs_buf_mutex);
4120
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);
4124 if (error2) {
4125 error = error2;
4126 goto done;
4127 }
4128 if (slpflag == PCATCH) {
4129 slpflag = 0;
4130 slptimeo = 2 * hz;
4131 }
4132 }
4133 }
4134
4135 if (NFS_ISV3(vp)) {
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))
4140 break;
4141 }
4142
4143 if (passone) {
4144 passone = 0;
4145 goto again;
4146 }
4147
4148 if ((waitfor == MNT_WAIT) && !LIST_EMPTY(&np->n_dirtyblkhd)) {
4149 goto again;
4150 }
4151
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;
4156 }
4157 done:
4158 FSDBG_BOT(517, vp, np, error, 0);
4159 return (error);
4160 }
4161
4162 /*
4163 * Do an nfs pathconf rpc.
4164 */
4165 int
4166 nfs_pathconfrpc(
4167 vnode_t vp,
4168 struct nfsv3_pathconf *pc,
4169 kauth_cred_t cred,
4170 proc_t procp)
4171 {
4172 mbuf_t mreq, mrep, md, mb, mb2;
4173 caddr_t bpos, dpos, cp, cp2;
4174 int32_t t1, t2;
4175 u_long *tl;
4176 u_int64_t xid;
4177 int attrflag, error = 0;
4178 struct nfsv3_pathconf *mpc;
4179
4180 /* fetch pathconf info from server */
4181 nfsm_reqhead(NFSX_FH(1));
4182 if (error)
4183 return (error);
4184 nfsm_fhtom(vp, 1);
4185 nfsm_request(vp, NFSPROC_PATHCONF, procp, cred, &xid);
4186 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
4187 if (!error) {
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);
4195 }
4196 nfsm_reqdone;
4197
4198 return (error);
4199 }
4200
4201 void
4202 nfs_pathconf_cache(struct nfsmount *nmp, struct nfsv3_pathconf *pc)
4203 {
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;
4208 if (pc->pc_notrunc)
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;
4216 }
4217
4218 /*
4219 * Return POSIX pathconf information applicable to nfs.
4220 *
4221 * The NFS V2 protocol doesn't support this, so just return EINVAL
4222 * for V2.
4223 */
4224 /* ARGSUSED */
4225 static int
4226 nfs_pathconf(ap)
4227 struct vnop_pathconf_args /* {
4228 struct vnodeop_desc *a_desc;
4229 vnode_t a_vp;
4230 int a_name;
4231 register_t *a_retval;
4232 vfs_context_t a_context;
4233 } */ *ap;
4234 {
4235 vnode_t vp = ap->a_vp;
4236 struct nfsmount *nmp;
4237 struct nfsv3_pathconf pc;
4238 int error = 0, cached;
4239
4240 nmp = VFSTONFS(vnode_mount(vp));
4241 if (!nmp)
4242 return (ENXIO);
4243 if (!NFS_ISV3(vp))
4244 return (EINVAL);
4245
4246 switch (ap->a_name) {
4247 case _PC_LINK_MAX:
4248 case _PC_NAME_MAX:
4249 case _PC_CHOWN_RESTRICTED:
4250 case _PC_NO_TRUNC:
4251 case _PC_CASE_SENSITIVE:
4252 case _PC_CASE_PRESERVING:
4253 break;
4254 default:
4255 /* don't bother contacting the server if we know the answer */
4256 return (EINVAL);
4257 }
4258
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);
4264 if (error)
4265 return (error);
4266 nmp = VFSTONFS(vnode_mount(vp));
4267 if (!nmp)
4268 return (ENXIO);
4269 if (!(nmp->nm_state & NFSSTA_GOTFSINFO)) {
4270 nfs_fsinfo(nmp, vp, cred, p);
4271 nmp = VFSTONFS(vnode_mount(vp));
4272 if (!nmp)
4273 return (ENXIO);
4274 }
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);
4280 }
4281 }
4282
4283 cached = (nmp->nm_state & NFSSTA_GOTPATHCONF);
4284
4285 switch (ap->a_name) {
4286 case _PC_LINK_MAX:
4287 *ap->a_retval = cached ? nmp->nm_fsinfo.linkmax : pc.pc_linkmax;
4288 break;
4289 case _PC_NAME_MAX:
4290 *ap->a_retval = cached ? nmp->nm_fsinfo.namemax : pc.pc_namemax;
4291 break;
4292 case _PC_CHOWN_RESTRICTED:
4293 if (cached)
4294 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CHOWN_RESTRICTED) ? 1 : 0;
4295 else
4296 *ap->a_retval = pc.pc_chownrestricted;
4297 break;
4298 case _PC_NO_TRUNC:
4299 if (cached)
4300 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_NOTRUNC) ? 1 : 0;
4301 else
4302 *ap->a_retval = pc.pc_notrunc;
4303 break;
4304 case _PC_CASE_SENSITIVE:
4305 if (cached)
4306 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_INSENSITIVE) ? 0 : 1;
4307 else
4308 *ap->a_retval = !pc.pc_caseinsensitive;
4309 break;
4310 case _PC_CASE_PRESERVING:
4311 if (cached)
4312 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_PRESERVING) ? 1 : 0;
4313 else
4314 *ap->a_retval = pc.pc_casepreserving;
4315 break;
4316 default:
4317 error = EINVAL;
4318 }
4319
4320 return (error);
4321 }
4322
4323 /*
4324 * NFS advisory byte-level locks (client)
4325 */
4326 static int
4327 nfs_advlock(ap)
4328 struct vnop_advlock_args /* {
4329 struct vnodeop_desc *a_desc;
4330 vnode_t a_vp;
4331 caddr_t a_id;
4332 int a_op;
4333 struct flock *a_fl;
4334 int a_flags;
4335 vfs_context_t a_context;
4336 } */ *ap;
4337 {
4338 return (nfs_dolock(ap));
4339 }
4340
4341 /*
4342 * write (or commit) the given NFS buffer
4343 */
4344 int
4345 nfs_buf_write(struct nfsbuf *bp)
4346 {
4347 int oldflags = bp->nb_flags, rv = 0;
4348 vnode_t vp = bp->nb_vp;
4349 struct nfsnode *np = VTONFS(vp);
4350 kauth_cred_t cr;
4351 proc_t p = current_proc(); // XXX
4352
4353 FSDBG_TOP(553, bp, NBOFF(bp), bp->nb_flags, 0);
4354
4355 if (!ISSET(bp->nb_lflags, NBL_BUSY))
4356 panic("nfs_buf_write: buffer is not busy???");
4357
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);
4361 NFSBUFCNTCHK(0);
4362 wakeup(&nfs_nbdwrite);
4363 }
4364
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);
4372 }
4373 vnode_startwrite(vp);
4374
4375 if (p && p->p_stats)
4376 p->p_stats->p_ru.ru_oublock++;
4377
4378 /*
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.
4381 */
4382 if (ISSET(bp->nb_flags, NB_ASYNC))
4383 p = NULL;
4384 if (ISSET(bp->nb_flags, NB_READ))
4385 cr = bp->nb_rcred;
4386 else
4387 cr = bp->nb_wcred;
4388 if (!ISSET(bp->nb_flags, NB_ASYNC) || nfs_asyncio(bp, NOCRED))
4389 rv = nfs_doio(bp, cr, p);
4390
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);
4400 }
4401 oldflags = bp->nb_flags;
4402 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
4403 if (cr) {
4404 kauth_cred_ref(cr);
4405 }
4406 nfs_buf_release(bp, 1);
4407 if (ISSET(oldflags, NB_ERROR) && !(np->n_flag & NFLUSHINPROG)) {
4408 /*
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)
4414 *
4415 * But we couldn't call vinvalbuf while holding
4416 * the buffer busy. So we call vinvalbuf() after
4417 * releasing the buffer.
4418 */
4419 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cr, p, 1);
4420 }
4421 if (cr)
4422 kauth_cred_rele(cr);
4423 return (rv);
4424 }
4425
4426 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
4427 return (rv);
4428 }
4429
4430 /*
4431 * Read wrapper for special devices.
4432 */
4433 static int
4434 nfsspec_read(ap)
4435 struct vnop_read_args /* {
4436 struct vnodeop_desc *a_desc;
4437 vnode_t a_vp;
4438 struct uio *a_uio;
4439 int a_ioflag;
4440 vfs_context_t a_context;
4441 } */ *ap;
4442 {
4443 register struct nfsnode *np = VTONFS(ap->a_vp);
4444 struct timeval now;
4445
4446 /*
4447 * Set access flag.
4448 */
4449 np->n_flag |= NACC;
4450 microtime(&now);
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));
4454 }
4455
4456 /*
4457 * Write wrapper for special devices.
4458 */
4459 static int
4460 nfsspec_write(ap)
4461 struct vnop_write_args /* {
4462 struct vnodeop_desc *a_desc;
4463 vnode_t a_vp;
4464 struct uio *a_uio;
4465 int a_ioflag;
4466 vfs_context_t a_context;
4467 } */ *ap;
4468 {
4469 register struct nfsnode *np = VTONFS(ap->a_vp);
4470 struct timeval now;
4471
4472 /*
4473 * Set update flag.
4474 */
4475 np->n_flag |= NUPD;
4476 microtime(&now);
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));
4480 }
4481
4482 /*
4483 * Close wrapper for special devices.
4484 *
4485 * Update the times on the nfsnode then do device close.
4486 */
4487 static int
4488 nfsspec_close(ap)
4489 struct vnop_close_args /* {
4490 struct vnodeop_desc *a_desc;
4491 vnode_t a_vp;
4492 int a_fflag;
4493 vfs_context_t a_context;
4494 } */ *ap;
4495 {
4496 vnode_t vp = ap->a_vp;
4497 struct nfsnode *np = VTONFS(vp);
4498 struct vnode_attr vattr;
4499 mount_t mp;
4500
4501 if (np->n_flag & (NACC | NUPD)) {
4502 np->n_flag |= NCHG;
4503 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4504 VATTR_INIT(&vattr);
4505 if (np->n_flag & NACC) {
4506 vattr.va_access_time = np->n_atim;
4507 VATTR_SET_ACTIVE(&vattr, va_access_time);
4508 }
4509 if (np->n_flag & NUPD) {
4510 vattr.va_modify_time = np->n_mtim;
4511 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4512 }
4513 vnode_setattr(vp, &vattr, ap->a_context);
4514 }
4515 }
4516 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
4517 }
4518
4519 extern vnop_t **fifo_vnodeop_p;
4520
4521 /*
4522 * Read wrapper for fifos.
4523 */
4524 static int
4525 nfsfifo_read(ap)
4526 struct vnop_read_args /* {
4527 struct vnodeop_desc *a_desc;
4528 vnode_t a_vp;
4529 struct uio *a_uio;
4530 int a_ioflag;
4531 vfs_context_t a_context;
4532 } */ *ap;
4533 {
4534 register struct nfsnode *np = VTONFS(ap->a_vp);
4535 struct timeval now;
4536
4537 /*
4538 * Set access flag.
4539 */
4540 np->n_flag |= NACC;
4541 microtime(&now);
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));
4545 }
4546
4547 /*
4548 * Write wrapper for fifos.
4549 */
4550 static int
4551 nfsfifo_write(ap)
4552 struct vnop_write_args /* {
4553 struct vnodeop_desc *a_desc;
4554 vnode_t a_vp;
4555 struct uio *a_uio;
4556 int a_ioflag;
4557 vfs_context_t a_context;
4558 } */ *ap;
4559 {
4560 register struct nfsnode *np = VTONFS(ap->a_vp);
4561 struct timeval now;
4562
4563 /*
4564 * Set update flag.
4565 */
4566 np->n_flag |= NUPD;
4567 microtime(&now);
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));
4571 }
4572
4573 /*
4574 * Close wrapper for fifos.
4575 *
4576 * Update the times on the nfsnode then do fifo close.
4577 */
4578 static int
4579 nfsfifo_close(ap)
4580 struct vnop_close_args /* {
4581 struct vnodeop_desc *a_desc;
4582 vnode_t a_vp;
4583 int a_fflag;
4584 vfs_context_t a_context;
4585 } */ *ap;
4586 {
4587 vnode_t vp = ap->a_vp;
4588 struct nfsnode *np = VTONFS(vp);
4589 struct vnode_attr vattr;
4590 struct timeval now;
4591 mount_t mp;
4592
4593 if (np->n_flag & (NACC | NUPD)) {
4594 microtime(&now);
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;
4598 }
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;
4602 }
4603 np->n_flag |= NCHG;
4604 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4605 VATTR_INIT(&vattr);
4606 if (np->n_flag & NACC) {
4607 vattr.va_access_time = np->n_atim;
4608 VATTR_SET_ACTIVE(&vattr, va_access_time);
4609 }
4610 if (np->n_flag & NUPD) {
4611 vattr.va_modify_time = np->n_mtim;
4612 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4613 }
4614 vnode_setattr(vp, &vattr, ap->a_context);
4615 }
4616 }
4617 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
4618 }
4619
4620 /*ARGSUSED*/
4621 static int
4622 nfs_ioctl(
4623 __unused struct vnop_ioctl_args /* {
4624 struct vnodeop_desc *a_desc;
4625 vnode_t a_vp;
4626 u_long a_command;
4627 caddr_t a_data;
4628 int a_fflag;
4629 kauth_cred_t a_cred;
4630 proc_t a_p;
4631 } */ *ap)
4632 {
4633
4634 /*
4635 * XXX we were once bogusly enoictl() which returned this (ENOTTY).
4636 * Probably we should return ENODEV.
4637 */
4638 return (ENOTTY);
4639 }
4640
4641 /*ARGSUSED*/
4642 static int
4643 nfs_select(
4644 __unused struct vnop_select_args /* {
4645 struct vnodeop_desc *a_desc;
4646 vnode_t a_vp;
4647 int a_which;
4648 int a_fflags;
4649 kauth_cred_t a_cred;
4650 void *a_wql;
4651 proc_t a_p;
4652 } */ *ap)
4653 {
4654
4655 /*
4656 * We were once bogusly seltrue() which returns 1. Is this right?
4657 */
4658 return (1);
4659 }
4660
4661 /*
4662 * Vnode op for pagein using getblk_pages
4663 * derived from nfs_bioread()
4664 * No read aheads are started from pagein operation
4665 */
4666 static int
4667 nfs_pagein(ap)
4668 struct vnop_pagein_args /* {
4669 struct vnodeop_desc *a_desc;
4670 vnode_t a_vp;
4671 upl_t a_pl;
4672 vm_offset_t a_pl_offset;
4673 off_t a_f_offset;
4674 size_t a_size;
4675 int a_flags;
4676 vfs_context_t a_context;
4677 } */ *ap;
4678 {
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;
4685 kauth_cred_t cred;
4686 proc_t p;
4687 struct nfsnode *np = VTONFS(vp);
4688 int biosize, xsize, iosize;
4689 struct nfsmount *nmp;
4690 int error = 0;
4691 vm_offset_t ioaddr;
4692 struct uio auio;
4693 struct iovec_32 aiov;
4694 struct uio * uio = &auio;
4695 int nofreeupl = flags & UPL_NOCOMMIT;
4696 upl_page_info_t *plinfo;
4697
4698 FSDBG(322, vp, f_offset, size, flags);
4699 if (pl == (upl_t)NULL)
4700 panic("nfs_pagein: no upl");
4701
4702 if (UBCINVALID(vp)) {
4703 printf("nfs_pagein: invalid vnode 0x%x", (int)vp);
4704 if (!nofreeupl)
4705 (void) ubc_upl_abort(pl, 0);
4706 return (EPERM);
4707 }
4708 UBCINFOCHECK("nfs_pagein", vp);
4709
4710 if (size <= 0) {
4711 printf("nfs_pagein: invalid size %d", size);
4712 if (!nofreeupl)
4713 (void) ubc_upl_abort(pl, 0);
4714 return (EINVAL);
4715 }
4716 if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
4717 if (!nofreeupl)
4718 ubc_upl_abort_range(pl, pl_offset, size,
4719 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4720 return (EINVAL);
4721 }
4722
4723 cred = ubc_getcred(vp);
4724 if (cred == NOCRED)
4725 cred = vfs_context_ucred(ap->a_context);
4726 p = vfs_context_proc(ap->a_context);
4727
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;
4731 #else
4732 auio.uio_segflg = UIO_SYSSPACE32;
4733 #endif
4734 auio.uio_rw = UIO_READ;
4735 auio.uio_procp = p;
4736
4737 nmp = VFSTONFS(vnode_mount(vp));
4738 if (!nmp) {
4739 if (!nofreeupl)
4740 ubc_upl_abort_range(pl, pl_offset, size,
4741 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4742 return (ENXIO);
4743 }
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;
4747
4748 plinfo = ubc_upl_pageinfo(pl);
4749 ubc_upl_map(pl, &ioaddr);
4750 ioaddr += pl_offset;
4751 xsize = size;
4752
4753 do {
4754 /*
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?
4759 */
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);
4766
4767 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), ioaddr, xsize);
4768 /*
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.
4771 */
4772 #ifdef UPL_DEBUG
4773 upl_ubc_alias_set(pl, current_thread(), 2);
4774 #endif /* UPL_DEBUG */
4775 OSAddAtomic(1, (SInt32*)&nfsstats.pageins);
4776
4777 error = nfs_readrpc(vp, uio, cred, p);
4778
4779 if (!error) {
4780 if (uio_uio_resid(uio)) {
4781 /*
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.
4786 */
4787 // LP64todo - fix this
4788 int zcnt = uio_uio_resid(uio);
4789 int zoff = iosize - zcnt;
4790 bzero((char *)ioaddr + zoff, zcnt);
4791
4792 FSDBG(324, uio->uio_offset, zoff, zcnt, ioaddr);
4793 uio->uio_offset += zcnt;
4794 }
4795 ioaddr += iosize;
4796 xsize -= iosize;
4797 } else {
4798 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), error, -1);
4799 }
4800
4801 nmp = VFSTONFS(vnode_mount(vp));
4802 } while (error == 0 && xsize > 0);
4803
4804 ubc_upl_unmap(pl);
4805
4806 if (!nofreeupl) {
4807 if (error)
4808 ubc_upl_abort_range(pl, pl_offset, size,
4809 UPL_ABORT_ERROR |
4810 UPL_ABORT_FREE_ON_EMPTY);
4811 else
4812 ubc_upl_commit_range(pl, pl_offset, size,
4813 UPL_COMMIT_CLEAR_DIRTY |
4814 UPL_COMMIT_FREE_ON_EMPTY);
4815 }
4816 return (error);
4817 }
4818
4819
4820 /*
4821 * Vnode op for pageout using UPL
4822 * Derived from nfs_write()
4823 * File size changes are not permitted in pageout.
4824 */
4825 static int
4826 nfs_pageout(ap)
4827 struct vnop_pageout_args /* {
4828 struct vnodeop_desc *a_desc;
4829 vnode_t a_vp;
4830 upl_t a_pl;
4831 vm_offset_t a_pl_offset;
4832 off_t a_f_offset;
4833 size_t a_size;
4834 int a_flags;
4835 vfs_context_t a_context;
4836 } */ *ap;
4837 {
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);
4845 kauth_cred_t cred;
4846 proc_t p;
4847 struct nfsbuf *bp;
4848 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4849 daddr64_t lbn;
4850 int error = 0, iomode, must_commit;
4851 off_t off;
4852 vm_offset_t ioaddr;
4853 struct uio auio;
4854 struct iovec_32 aiov;
4855 int nofreeupl = flags & UPL_NOCOMMIT;
4856 size_t biosize, iosize, pgsize, xsize;
4857
4858 FSDBG(323, f_offset, size, pl, pl_offset);
4859
4860 if (pl == (upl_t)NULL)
4861 panic("nfs_pageout: no upl");
4862
4863 if (UBCINVALID(vp)) {
4864 printf("nfs_pageout: invalid vnode 0x%x", (int)vp);
4865 if (!nofreeupl)
4866 ubc_upl_abort(pl, 0);
4867 return (EIO);
4868 }
4869 UBCINFOCHECK("nfs_pageout", vp);
4870
4871 if (size <= 0) {
4872 printf("nfs_pageout: invalid size %d", size);
4873 if (!nofreeupl)
4874 ubc_upl_abort(pl, 0);
4875 return (EINVAL);
4876 }
4877
4878 if (!nmp) {
4879 if (!nofreeupl)
4880 ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
4881 return (ENXIO);
4882 }
4883 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
4884
4885 /*
4886 * Check to see whether the buffer is incore.
4887 * If incore and not busy, invalidate it from the cache.
4888 */
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 */
4902 if (!nofreeupl)
4903 ubc_upl_abort(pl, 0);
4904 return (EBUSY);
4905 }
4906 if (bp->nb_dirtyend > 0) {
4907 /*
4908 * if there's a dirty range in the buffer, check
4909 * to see if it extends beyond the pageout region
4910 *
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.
4914 *
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.
4921 */
4922 off_t boff, start, end;
4923 boff = NBOFF(bp);
4924 start = off;
4925 end = off + xsize;
4926 /* clip end to EOF */
4927 if (end > (off_t)np->n_size)
4928 end = np->n_size;
4929 start -= boff;
4930 end -= boff;
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);
4935 nfs_buf_drop(bp);
4936 lck_mtx_unlock(nfs_buf_mutex);
4937 if (!nofreeupl)
4938 ubc_upl_abort(pl, 0);
4939 return (EBUSY);
4940 }
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 */
4950 nfs_buf_drop(bp);
4951 lck_mtx_unlock(nfs_buf_mutex);
4952 continue;
4953 }
4954 }
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);
4962 }
4963 nfs_buf_release(bp, 1);
4964 } else {
4965 lck_mtx_unlock(nfs_buf_mutex);
4966 }
4967 }
4968
4969 cred = ubc_getcred(vp);
4970 if (cred == NOCRED)
4971 cred = vfs_context_ucred(ap->a_context);
4972 p = vfs_context_proc(ap->a_context);
4973
4974 if (np->n_flag & NWRITEERR) {
4975 np->n_flag &= ~NWRITEERR;
4976 if (!nofreeupl)
4977 ubc_upl_abort_range(pl, pl_offset, size,
4978 UPL_ABORT_FREE_ON_EMPTY);
4979 return (np->n_error);
4980 }
4981 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
4982 nfs_fsinfo(nmp, vp, cred, p);
4983
4984 if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
4985 f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
4986 if (!nofreeupl)
4987 ubc_upl_abort_range(pl, pl_offset, size,
4988 UPL_ABORT_FREE_ON_EMPTY);
4989 return (EINVAL);
4990 }
4991
4992 ubc_upl_map(pl, &ioaddr);
4993 ioaddr += pl_offset;
4994
4995 if ((u_quad_t)f_offset + size > np->n_size)
4996 xsize = np->n_size - f_offset;
4997 else
4998 xsize = size;
4999
5000 pgsize = round_page_64(xsize);
5001 if (size > pgsize) {
5002 if (!nofreeupl)
5003 ubc_upl_abort_range(pl, pl_offset + pgsize,
5004 size - pgsize,
5005 UPL_ABORT_FREE_ON_EMPTY);
5006 }
5007
5008 /*
5009 * check for partial page and clear the
5010 * contents past end of the file before
5011 * releasing it in the VM page cache
5012 */
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);
5017 }
5018
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;
5022 #else
5023 auio.uio_segflg = UIO_SYSSPACE32;
5024 #endif
5025 auio.uio_rw = UIO_READ;
5026 auio.uio_procp = p;
5027
5028 do {
5029 /*
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?
5034 */
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;
5041
5042 FSDBG(323, auio.uio_offset, uio_uio_resid(&auio), ioaddr, xsize);
5043 OSAddAtomic(1, (SInt32*)&nfsstats.pageouts);
5044
5045 vnode_startwrite(vp);
5046
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);
5050 if (must_commit)
5051 nfs_clearcommit(vnode_mount(vp));
5052 vnode_writedone(vp);
5053 if (error)
5054 goto cleanup;
5055 /* Note: no need to check uio_resid, because */
5056 /* it'll only be set if there was an error. */
5057 ioaddr += iosize;
5058 xsize -= iosize;
5059 } while (xsize > 0);
5060
5061 cleanup:
5062 ubc_upl_unmap(pl);
5063 /*
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
5085 */
5086
5087 if (!nofreeupl) { /* otherwise stacked file system has to handle this */
5088 if (error) {
5089 int abortflags = 0;
5090 short action = nfs_pageouterrorhandler(error);
5091
5092 switch (action) {
5093 case DUMP:
5094 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
5095 break;
5096 case DUMPANDLOG:
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]++;
5102 break;
5103 case RETRY:
5104 abortflags = UPL_ABORT_FREE_ON_EMPTY;
5105 break;
5106 case RETRYWITHSLEEP:
5107 abortflags = UPL_ABORT_FREE_ON_EMPTY;
5108 /* pri unused. PSOCK for placeholder. */
5109 tsleep(&lbolt, PSOCK, "nfspageout", 0);
5110 break;
5111 case SEVER: /* not implemented */
5112 default:
5113 printf("nfs_pageout: action %d not expected\n", action);
5114 break;
5115 }
5116
5117 ubc_upl_abort_range(pl, pl_offset, size, abortflags);
5118 /* return error in all cases above */
5119
5120 } else
5121 ubc_upl_commit_range(pl, pl_offset, pgsize,
5122 UPL_COMMIT_CLEAR_DIRTY |
5123 UPL_COMMIT_FREE_ON_EMPTY);
5124 }
5125 return (error);
5126 }
5127
5128 /* Blktooff derives file offset given a logical block number */
5129 static int
5130 nfs_blktooff(ap)
5131 struct vnop_blktooff_args /* {
5132 struct vnodeop_desc *a_desc;
5133 vnode_t a_vp;
5134 daddr64_t a_lblkno;
5135 off_t *a_offset;
5136 } */ *ap;
5137 {
5138 int biosize;
5139 vnode_t vp = ap->a_vp;
5140 mount_t mp = vnode_mount(vp);
5141
5142 if (!mp)
5143 return (ENXIO);
5144
5145 biosize = vfs_statfs(mp)->f_iosize;
5146
5147 *ap->a_offset = (off_t)(ap->a_lblkno * biosize);
5148
5149 return (0);
5150 }
5151
5152 static int
5153 nfs_offtoblk(ap)
5154 struct vnop_offtoblk_args /* {
5155 struct vnodeop_desc *a_desc;
5156 vnode_t a_vp;
5157 off_t a_offset;
5158 daddr64_t *a_lblkno;
5159 } */ *ap;
5160 {
5161 int biosize;
5162 vnode_t vp = ap->a_vp;
5163 mount_t mp = vnode_mount(vp);
5164
5165 if (!mp)
5166 return (ENXIO);
5167
5168 biosize = vfs_statfs(mp)->f_iosize;
5169
5170 *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize);
5171
5172 return (0);
5173 }
5174