]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vnops.c
b44eb45f24b9d5643c0fa661e3a51eff22386089
[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 nfsmount *nmp;
1035 struct vnode_attr *vap = ap->a_vap;
1036 int error = 0;
1037 int biosize;
1038 u_quad_t tsize;
1039 kauth_cred_t cred;
1040 proc_t p;
1041
1042 #ifndef nolint
1043 tsize = (u_quad_t)0;
1044 #endif
1045 nmp = VFSTONFS(vnode_mount(vp));
1046 if (!nmp)
1047 return (ENXIO);
1048 biosize = nmp->nm_biosize;
1049
1050 /* Setting of flags is not supported. */
1051 if (VATTR_IS_ACTIVE(vap, va_flags))
1052 return (ENOTSUP);
1053
1054 cred = vfs_context_ucred(ap->a_context);
1055 p = vfs_context_proc(ap->a_context);
1056
1057 VATTR_SET_SUPPORTED(vap, va_mode);
1058 VATTR_SET_SUPPORTED(vap, va_uid);
1059 VATTR_SET_SUPPORTED(vap, va_gid);
1060 VATTR_SET_SUPPORTED(vap, va_data_size);
1061 VATTR_SET_SUPPORTED(vap, va_access_time);
1062 VATTR_SET_SUPPORTED(vap, va_modify_time);
1063
1064 /* Disallow write attempts if the filesystem is mounted read-only. */
1065 if ((VATTR_IS_ACTIVE(vap, va_flags) || VATTR_IS_ACTIVE(vap, va_mode) ||
1066 VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) ||
1067 VATTR_IS_ACTIVE(vap, va_access_time) ||
1068 VATTR_IS_ACTIVE(vap, va_modify_time)) &&
1069 vnode_vfsisrdonly(vp))
1070 return (EROFS);
1071
1072 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
1073 switch (vnode_vtype(vp)) {
1074 case VDIR:
1075 return (EISDIR);
1076 case VCHR:
1077 case VBLK:
1078 case VSOCK:
1079 case VFIFO:
1080 if (!VATTR_IS_ACTIVE(vap, va_modify_time) &&
1081 !VATTR_IS_ACTIVE(vap, va_access_time) &&
1082 !VATTR_IS_ACTIVE(vap, va_mode) &&
1083 !VATTR_IS_ACTIVE(vap, va_uid) &&
1084 !VATTR_IS_ACTIVE(vap, va_gid))
1085 return (0);
1086 VATTR_CLEAR_ACTIVE(vap, va_data_size);
1087 break;
1088 default:
1089 /*
1090 * Disallow write attempts if the filesystem is
1091 * mounted read-only.
1092 */
1093 if (vnode_vfsisrdonly(vp))
1094 return (EROFS);
1095 FSDBG_TOP(512, np->n_size, vap->va_data_size,
1096 np->n_vattr.nva_size, np->n_flag);
1097 if (np->n_flag & NMODIFIED) {
1098 if (vap->va_data_size == 0)
1099 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
1100 else
1101 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1102 if (error) {
1103 printf("nfs_setattr: nfs_vinvalbuf %d\n", error);
1104 FSDBG_BOT(512, np->n_size, vap->va_data_size,
1105 np->n_vattr.nva_size, -1);
1106 return (error);
1107 }
1108 } else if (np->n_size > vap->va_data_size) { /* shrinking? */
1109 daddr64_t obn, bn;
1110 int neweofoff, mustwrite;
1111 struct nfsbuf *bp;
1112
1113 obn = (np->n_size - 1) / biosize;
1114 bn = vap->va_data_size / biosize;
1115 for ( ; obn >= bn; obn--) {
1116 if (!nfs_buf_is_incore(vp, obn))
1117 continue;
1118 error = nfs_buf_get(vp, obn, biosize, 0, NBLK_READ, &bp);
1119 if (error)
1120 continue;
1121 if (obn != bn) {
1122 FSDBG(512, bp, bp->nb_flags, 0, obn);
1123 SET(bp->nb_flags, NB_INVAL);
1124 nfs_buf_release(bp, 1);
1125 continue;
1126 }
1127 mustwrite = 0;
1128 neweofoff = vap->va_data_size - NBOFF(bp);
1129 /* check for any dirty data before the new EOF */
1130 if (bp->nb_dirtyend && bp->nb_dirtyoff < neweofoff) {
1131 /* clip dirty range to EOF */
1132 if (bp->nb_dirtyend > neweofoff)
1133 bp->nb_dirtyend = neweofoff;
1134 mustwrite++;
1135 }
1136 bp->nb_dirty &= (1 << round_page_32(neweofoff)/PAGE_SIZE) - 1;
1137 if (bp->nb_dirty)
1138 mustwrite++;
1139 if (!mustwrite) {
1140 FSDBG(512, bp, bp->nb_flags, 0, obn);
1141 SET(bp->nb_flags, NB_INVAL);
1142 nfs_buf_release(bp, 1);
1143 continue;
1144 }
1145 /* gotta write out dirty data before invalidating */
1146 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1147 /* (NB_NOCACHE indicates buffer should be discarded) */
1148 CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL | NB_ASYNC | NB_READ));
1149 SET(bp->nb_flags, NB_STABLE | NB_NOCACHE);
1150 if (bp->nb_wcred == NOCRED) {
1151 kauth_cred_ref(cred);
1152 bp->nb_wcred = cred;
1153 }
1154 error = nfs_buf_write(bp);
1155 // Note: bp has been released
1156 if (error) {
1157 FSDBG(512, bp, 0xd00dee, 0xbad, error);
1158 np->n_error = error;
1159 np->n_flag |= NWRITEERR;
1160 /*
1161 * There was a write error and we need to
1162 * invalidate attrs and flush buffers in
1163 * order to sync up with the server.
1164 * (if this write was extending the file,
1165 * we may no longer know the correct size)
1166 */
1167 NATTRINVALIDATE(np);
1168 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
1169 error = 0;
1170 }
1171 }
1172 }
1173 tsize = np->n_size;
1174 np->n_size = np->n_vattr.nva_size = vap->va_data_size;
1175 ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */
1176 }
1177 } else if ((VATTR_IS_ACTIVE(vap, va_modify_time) ||
1178 VATTR_IS_ACTIVE(vap, va_access_time)) &&
1179 (np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) {
1180 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1181 if (error == EINTR)
1182 return (error);
1183 }
1184 if (VATTR_IS_ACTIVE(vap, va_mode)) {
1185 NMODEINVALIDATE(np);
1186 }
1187 error = nfs_setattrrpc(vp, vap, cred, p);
1188 FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error);
1189 if (error && VATTR_IS_ACTIVE(vap, va_data_size)) {
1190 /* make every effort to resync file size w/ server... */
1191 int err; /* preserve "error" for return */
1192
1193 np->n_size = np->n_vattr.nva_size = tsize;
1194 ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */
1195 vap->va_data_size = tsize;
1196 err = nfs_setattrrpc(vp, vap, cred, p);
1197 printf("nfs_setattr: nfs_setattrrpc %d %d\n", error, err);
1198 }
1199 return (error);
1200 }
1201
1202 /*
1203 * Do an nfs setattr rpc.
1204 */
1205 static int
1206 nfs_setattrrpc(vp, vap, cred, procp)
1207 vnode_t vp;
1208 struct vnode_attr *vap;
1209 kauth_cred_t cred;
1210 proc_t procp;
1211 {
1212 register struct nfsv2_sattr *sp;
1213 register caddr_t cp;
1214 register long t1, t2;
1215 caddr_t bpos, dpos, cp2;
1216 u_long *tl;
1217 int error = 0, wccpostattr = 0;
1218 mbuf_t mreq, mrep, md, mb, mb2;
1219 int v3;
1220 u_int64_t xid;
1221 struct timeval now;
1222
1223 if (!VFSTONFS(vnode_mount(vp)))
1224 return (ENXIO);
1225 v3 = NFS_ISV3(vp);
1226
1227 nfsm_reqhead(NFSX_FH(v3) + NFSX_SATTR(v3));
1228 if (error)
1229 return (error);
1230 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_SETATTR]);
1231 nfsm_fhtom(vp, v3);
1232 if (v3) {
1233 if (VATTR_IS_ACTIVE(vap, va_mode)) {
1234 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1235 *tl++ = nfs_true;
1236 *tl = txdr_unsigned(vap->va_mode);
1237 } else {
1238 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1239 *tl = nfs_false;
1240 }
1241 if (VATTR_IS_ACTIVE(vap, va_uid)) {
1242 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1243 *tl++ = nfs_true;
1244 *tl = txdr_unsigned(vap->va_uid);
1245 } else {
1246 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1247 *tl = nfs_false;
1248 }
1249 if (VATTR_IS_ACTIVE(vap, va_gid)) {
1250 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1251 *tl++ = nfs_true;
1252 *tl = txdr_unsigned(vap->va_gid);
1253 } else {
1254 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1255 *tl = nfs_false;
1256 }
1257 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
1258 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1259 *tl++ = nfs_true;
1260 txdr_hyper(&vap->va_data_size, tl);
1261 } else {
1262 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1263 *tl = nfs_false;
1264 }
1265 microtime(&now);
1266 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1267 if (vap->va_access_time.tv_sec != now.tv_sec) {
1268 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1269 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1270 txdr_nfsv3time(&vap->va_access_time, tl);
1271 } else {
1272 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1273 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1274 }
1275 } else {
1276 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1277 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1278 }
1279 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1280 if (vap->va_modify_time.tv_sec != now.tv_sec) {
1281 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1282 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1283 txdr_nfsv3time(&vap->va_modify_time, tl);
1284 } else {
1285 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1286 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1287 }
1288 } else {
1289 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1290 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1291 }
1292 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1293 *tl = nfs_false;
1294 } else {
1295 struct timespec neg1time = { -1, -1 };
1296 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1297 if (VATTR_IS_ACTIVE(vap, va_mode))
1298 sp->sa_mode = vtonfsv2_mode(vnode_vtype(vp), vap->va_mode);
1299 else
1300 sp->sa_mode = nfs_xdrneg1;
1301 if (VATTR_IS_ACTIVE(vap, va_uid))
1302 sp->sa_uid = txdr_unsigned(vap->va_uid);
1303 else
1304 sp->sa_uid = nfs_xdrneg1;
1305 if (VATTR_IS_ACTIVE(vap, va_gid))
1306 sp->sa_gid = txdr_unsigned(vap->va_gid);
1307 else
1308 sp->sa_gid = nfs_xdrneg1;
1309 if (VATTR_IS_ACTIVE(vap, va_data_size))
1310 sp->sa_size = txdr_unsigned(vap->va_data_size);
1311 else
1312 sp->sa_size = nfs_xdrneg1;
1313 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1314 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
1315 } else {
1316 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1317 }
1318 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1319 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1320 } else {
1321 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1322 }
1323 }
1324 nfsm_request(vp, NFSPROC_SETATTR, procp, cred, &xid);
1325 if (v3) {
1326 struct timespec premtime = { 0, 0 };
1327 if (mrep) {
1328 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
1329 }
1330 /* if file hadn't changed, update cached mtime */
1331 if (nfstimespeccmp(&VTONFS(vp)->n_mtime, &premtime, ==)) {
1332 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.nva_mtime;
1333 }
1334 /* if directory hadn't changed, update namecache mtime */
1335 if ((vnode_vtype(vp) == VDIR) &&
1336 nfstimespeccmp(&VTONFS(vp)->n_ncmtime, &premtime, ==)) {
1337 VTONFS(vp)->n_ncmtime = VTONFS(vp)->n_vattr.nva_mtime;
1338 }
1339 if (!wccpostattr)
1340 NATTRINVALIDATE(VTONFS(vp));
1341 } else {
1342 if (mrep) {
1343 nfsm_loadattr(vp, v3, NULL, &xid);
1344 }
1345 }
1346 nfsm_reqdone;
1347 return (error);
1348 }
1349
1350 /*
1351 * nfs lookup call, one step at a time...
1352 * First look in cache
1353 * If not found, unlock the directory nfsnode and do the rpc
1354 */
1355 static int
1356 nfs_lookup(ap)
1357 struct vnop_lookup_args /* {
1358 struct vnodeop_desc *a_desc;
1359 vnode_t a_dvp;
1360 vnode_t *a_vpp;
1361 struct componentname *a_cnp;
1362 vfs_context_t a_context;
1363 } */ *ap;
1364 {
1365 struct componentname *cnp = ap->a_cnp;
1366 vnode_t dvp = ap->a_dvp;
1367 vnode_t *vpp = ap->a_vpp;
1368 int flags = cnp->cn_flags;
1369 vnode_t newvp;
1370 u_long *tl;
1371 caddr_t cp;
1372 long t1, t2;
1373 caddr_t bpos, dpos, cp2;
1374 mbuf_t mreq, mrep, md, mb, mb2;
1375 long len;
1376 u_char *fhp;
1377 struct nfsnode *dnp, *np;
1378 int wantparent, error, attrflag, dattrflag, fhsize, fhisdvp;
1379 int v3 = NFS_ISV3(dvp);
1380 u_int64_t xid, dxid;
1381 struct nfs_vattr nvattr;
1382 kauth_cred_t cred;
1383 proc_t p;
1384 int ngflags;
1385
1386 *vpp = NULLVP;
1387
1388 cred = vfs_context_ucred(ap->a_context);
1389 p = vfs_context_proc(ap->a_context);
1390
1391 wantparent = flags & (LOCKPARENT|WANTPARENT);
1392 dnp = VTONFS(dvp);
1393
1394 error = nfs_getattr(dvp, &nvattr, cred, p);
1395 if (error)
1396 goto error_return;
1397 if (nfstimespeccmp(&dnp->n_ncmtime, &nvattr.nva_mtime, !=)) {
1398 /*
1399 * This directory has changed on us.
1400 * Purge any name cache entries.
1401 */
1402 cache_purge(dvp);
1403 dnp->n_ncmtime = nvattr.nva_mtime;
1404 }
1405
1406 error = cache_lookup(dvp, vpp, cnp);
1407 switch (error) {
1408 case ENOENT:
1409 /* negative cache entry same as cache miss */
1410 error = 0;
1411 /* FALLTHROUGH */
1412 case 0:
1413 /* cache miss */
1414 break;
1415 case -1:
1416 /* cache hit, not really an error */
1417 {
1418 struct vnop_access_args naa;
1419
1420 OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_hits);
1421
1422 /* check for directory access */
1423 naa.a_vp = dvp;
1424 naa.a_action = KAUTH_VNODE_SEARCH;
1425 naa.a_context = ap->a_context;
1426
1427 /* compute actual success/failure based on accessibility */
1428 error = nfs_access(&naa);
1429 }
1430 /* FALLTHROUGH */
1431 default:
1432 /* unexpected error from cache_lookup */
1433 goto error_return;
1434 }
1435
1436 /* check for lookup of "." */
1437 if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
1438 /* skip lookup, we know who we are */
1439 fhisdvp = 1;
1440 fhp = NULL;
1441 fhsize = 0;
1442 mrep = NULL;
1443 goto found;
1444 }
1445
1446 /* do we know this name is too long? */
1447 if (v3) {
1448 /* For NFSv3: need uniform pathconf info to test pc_namemax */
1449 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1450 if (!nmp) {
1451 error = ENXIO;
1452 goto error_return;
1453 }
1454 if (((nmp->nm_state & (NFSSTA_GOTFSINFO|NFSSTA_GOTPATHCONF)) ==
1455 (NFSSTA_GOTFSINFO|NFSSTA_GOTPATHCONF)) &&
1456 (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS) &&
1457 (cnp->cn_namelen > (long)nmp->nm_fsinfo.namemax)) {
1458 error = ENAMETOOLONG;
1459 goto error_return;
1460 }
1461 } else if (cnp->cn_namelen > NFS_MAXNAMLEN) {
1462 error = ENAMETOOLONG;
1463 goto error_return;
1464 }
1465
1466 error = 0;
1467 newvp = NULLVP;
1468
1469 OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_misses);
1470 len = cnp->cn_namelen;
1471 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1472 if (error)
1473 goto error_return;
1474 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LOOKUP]);
1475 nfsm_fhtom(dvp, v3);
1476 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN, v3);
1477 /* nfsm_request for NFSv2 causes you to goto to nfsmout upon errors */
1478 nfsm_request(dvp, NFSPROC_LOOKUP, p, cred, &xid);
1479
1480 if (error) {
1481 if (mrep) {
1482 nfsm_postop_attr_update(dvp, v3, dattrflag, &xid);
1483 mbuf_freem(mrep);
1484 }
1485 goto nfsmout;
1486 }
1487
1488 /* get the filehandle */
1489 nfsm_getfh(fhp, fhsize, v3);
1490 /* is the file handle the same as this directory's file handle? */
1491 fhisdvp = NFS_CMPFH(dnp, fhp, fhsize);
1492
1493 /* get attributes */
1494 if (v3) {
1495 dxid = xid;
1496 nfsm_postop_attr_get(v3, attrflag, &nvattr);
1497 nfsm_postop_attr_update(dvp, v3, dattrflag, &dxid);
1498 if (!attrflag && (!fhisdvp || !dattrflag)) {
1499 /* We need valid attributes in order */
1500 /* to call nfs_nget/vnode_create(). */
1501 error = nfs_getattr_no_vnode(vnode_mount(dvp),
1502 fhp, fhsize, cred, p, &nvattr, &xid);
1503 if (error) {
1504 mbuf_freem(mrep);
1505 goto error_return;
1506 }
1507 }
1508 } else {
1509 nfsm_attr_get(v3, &nvattr);
1510 }
1511
1512 found:
1513
1514 /*
1515 * Handle RENAME case...
1516 */
1517 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
1518 if (fhisdvp) {
1519 mbuf_freem(mrep);
1520 error = EISDIR;
1521 goto error_return;
1522 }
1523 error = nfs_nget(vnode_mount(dvp), dvp, cnp, fhp, fhsize,
1524 &nvattr, &xid, 0, &np);
1525 if (error) {
1526 mbuf_freem(mrep);
1527 goto error_return;
1528 }
1529 *vpp = NFSTOV(np);
1530 mbuf_freem(mrep);
1531
1532 goto error_return;
1533 }
1534
1535 if ((cnp->cn_flags & MAKEENTRY) &&
1536 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)))
1537 ngflags = NG_MAKEENTRY;
1538 else
1539 ngflags = 0;
1540
1541 if (fhisdvp) {
1542 error = vnode_get(dvp);
1543 if (error) {
1544 mbuf_freem(mrep);
1545 goto error_return;
1546 }
1547 newvp = dvp;
1548 /* test fhp to see if we have valid attributes in nvattr */
1549 if (fhp && (dnp->n_xid <= xid)) {
1550 error = nfs_loadattrcache(dnp, &nvattr, &xid, 0);
1551 if (error) {
1552 vnode_put(dvp);
1553 mbuf_freem(mrep);
1554 goto error_return;
1555 }
1556 }
1557 } else {
1558 error = nfs_nget(vnode_mount(dvp), dvp, cnp, fhp, fhsize,
1559 &nvattr, &xid, ngflags, &np);
1560 if (error) {
1561 mbuf_freem(mrep);
1562 goto error_return;
1563 }
1564 newvp = NFSTOV(np);
1565 }
1566 *vpp = newvp;
1567 // if (error == 0 && *vpp != NULL && *vpp != dvp)
1568 // nfs_unlock(VTONFS(*vpp));
1569
1570 nfsm_reqdone;
1571 if (error) {
1572 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1573 (flags & ISLASTCN) && error == ENOENT) {
1574 if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp))
1575 error = EROFS;
1576 else
1577 error = EJUSTRETURN;
1578 }
1579 }
1580 error_return:
1581 if (error && *vpp) {
1582 vnode_put(*vpp);
1583 *vpp = NULLVP;
1584 }
1585 return (error);
1586 }
1587
1588 /*
1589 * nfs read call.
1590 * Just call nfs_bioread() to do the work.
1591 */
1592 static int
1593 nfs_read(ap)
1594 struct vnop_read_args /* {
1595 struct vnodeop_desc *a_desc;
1596 vnode_t a_vp;
1597 struct uio *a_uio;
1598 int a_ioflag;
1599 vfs_context_t a_context;
1600 } */ *ap;
1601 {
1602 if (vnode_vtype(ap->a_vp) != VREG)
1603 return (EPERM);
1604 return (nfs_bioread(ap->a_vp, ap->a_uio, ap->a_ioflag,
1605 vfs_context_ucred(ap->a_context),
1606 vfs_context_proc(ap->a_context)));
1607 }
1608
1609
1610 /*
1611 * nfs readlink call
1612 */
1613 static int
1614 nfs_readlink(ap)
1615 struct vnop_readlink_args /* {
1616 struct vnodeop_desc *a_desc;
1617 vnode_t a_vp;
1618 struct uio *a_uio;
1619 vfs_context_t a_context;
1620 } */ *ap;
1621 {
1622 if (vnode_vtype(ap->a_vp) != VLNK)
1623 return (EPERM);
1624 return (nfs_bioread(ap->a_vp, ap->a_uio, 0,
1625 vfs_context_ucred(ap->a_context),
1626 vfs_context_proc(ap->a_context)));
1627 }
1628
1629 /*
1630 * Do a readlink rpc.
1631 * Called by nfs_doio() from below the buffer cache.
1632 */
1633 int
1634 nfs_readlinkrpc(
1635 vnode_t vp,
1636 struct uio *uiop,
1637 kauth_cred_t cred,
1638 proc_t p)
1639 {
1640 register u_long *tl;
1641 register caddr_t cp;
1642 register long t1, t2;
1643 caddr_t bpos, dpos, cp2;
1644 int error = 0, len, attrflag;
1645 mbuf_t mreq, mrep, md, mb, mb2;
1646 int v3;
1647 u_int64_t xid;
1648
1649 if (!VFSTONFS(vnode_mount(vp)))
1650 return (ENXIO);
1651 v3 = NFS_ISV3(vp);
1652
1653 nfsm_reqhead(NFSX_FH(v3));
1654 if (error)
1655 return (error);
1656 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READLINK]);
1657 nfsm_fhtom(vp, v3);
1658 nfsm_request(vp, NFSPROC_READLINK, p, cred, &xid);
1659 if (v3 && mrep)
1660 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
1661 if (!error) {
1662 nfsm_strsiz(len, NFS_MAXPATHLEN, v3);
1663 if (len >= NFS_MAXPATHLEN) {
1664 struct nfsnode *np = VTONFS(vp);
1665 #if DIAGNOSTIC
1666 if (!np)
1667 panic("nfs_readlinkrpc: null np");
1668 #endif
1669 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1670 len = np->n_size;
1671 }
1672 nfsm_mtouio(uiop, len);
1673 }
1674 nfsm_reqdone;
1675 return (error);
1676 }
1677
1678 /*
1679 * nfs read rpc call
1680 * Ditto above
1681 */
1682 int
1683 nfs_readrpc(
1684 vnode_t vp,
1685 struct uio *uiop,
1686 kauth_cred_t cred,
1687 proc_t p)
1688 {
1689 register u_long *tl;
1690 register caddr_t cp;
1691 register long t1, t2;
1692 caddr_t bpos, dpos, cp2;
1693 mbuf_t mreq, mrep, md, mb, mb2;
1694 struct nfsmount *nmp;
1695 int error = 0, len, retlen, tsiz, eof = 0, attrflag;
1696 int v3, nmrsize;
1697 u_int64_t xid;
1698
1699 FSDBG_TOP(536, vp, uiop->uio_offset, uio_uio_resid(uiop), 0);
1700 nmp = VFSTONFS(vnode_mount(vp));
1701 if (!nmp)
1702 return (ENXIO);
1703 v3 = NFS_ISV3(vp);
1704 nmrsize = nmp->nm_rsize;
1705
1706 // LP64todo - fix this
1707 tsiz = uio_uio_resid(uiop);
1708 if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && !v3) {
1709 FSDBG_BOT(536, vp, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
1710 return (EFBIG);
1711 }
1712 while (tsiz > 0) {
1713 len = (tsiz > nmrsize) ? nmrsize : tsiz;
1714 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1715 if (error)
1716 break;
1717 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READ]);
1718 nfsm_fhtom(vp, v3);
1719 nfsm_build(tl, u_long *, NFSX_UNSIGNED * 3);
1720 if (v3) {
1721 txdr_hyper(&uiop->uio_offset, tl);
1722 *(tl + 2) = txdr_unsigned(len);
1723 } else {
1724 *tl++ = txdr_unsigned(uiop->uio_offset);
1725 *tl++ = txdr_unsigned(len);
1726 *tl = 0;
1727 }
1728 FSDBG(536, vp, uiop->uio_offset, len, 0);
1729 nfsm_request(vp, NFSPROC_READ, p, cred, &xid);
1730 if (v3) {
1731 if (mrep) {
1732 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
1733 }
1734 if (error) {
1735 mbuf_freem(mrep);
1736 goto nfsmout;
1737 }
1738 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
1739 eof = fxdr_unsigned(int, *(tl + 1));
1740 } else {
1741 if (mrep) {
1742 nfsm_loadattr(vp, v3, NULL, &xid);
1743 }
1744 }
1745 if (mrep) {
1746 nfsm_strsiz(retlen, nmrsize, 0);
1747 nfsm_mtouio(uiop, retlen);
1748 mbuf_freem(mrep);
1749 } else {
1750 retlen = 0;
1751 }
1752 tsiz -= retlen;
1753 if (v3) {
1754 if (eof || retlen == 0)
1755 tsiz = 0;
1756 } else if (retlen < len)
1757 tsiz = 0;
1758 }
1759 nfsmout:
1760 FSDBG_BOT(536, vp, eof, uio_uio_resid(uiop), error);
1761 return (error);
1762 }
1763
1764 /*
1765 * nfs write call
1766 */
1767 int
1768 nfs_writerpc(
1769 vnode_t vp,
1770 struct uio *uiop,
1771 kauth_cred_t cred,
1772 proc_t p,
1773 int *iomode,
1774 int *must_commit)
1775 {
1776 register u_long *tl;
1777 register caddr_t cp;
1778 register int t1, t2, backup;
1779 caddr_t bpos, dpos, cp2;
1780 mbuf_t mreq, mrep, md, mb, mb2;
1781 struct nfsmount *nmp;
1782 int error = 0, len, tsiz, updatemtime = 0, wccpostattr = 0, rlen, commit;
1783 int v3, committed = NFSV3WRITE_FILESYNC;
1784 u_int64_t xid;
1785 mount_t mp;
1786
1787 #if DIAGNOSTIC
1788 if (uiop->uio_iovcnt != 1)
1789 panic("nfs_writerpc: iovcnt > 1");
1790 #endif
1791 FSDBG_TOP(537, vp, uiop->uio_offset, uio_uio_resid(uiop), *iomode);
1792 nmp = VFSTONFS(vnode_mount(vp));
1793 if (!nmp)
1794 return (ENXIO);
1795 v3 = NFS_ISV3(vp);
1796 *must_commit = 0;
1797 // LP64todo - fix this
1798 tsiz = uio_uio_resid(uiop);
1799 if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && !v3) {
1800 FSDBG_BOT(537, vp, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
1801 return (EFBIG);
1802 }
1803 while (tsiz > 0) {
1804 nmp = VFSTONFS(vnode_mount(vp));
1805 if (!nmp) {
1806 error = ENXIO;
1807 break;
1808 }
1809 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1810 nfsm_reqhead(NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1811 if (error)
1812 break;
1813 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_WRITE]);
1814 nfsm_fhtom(vp, v3);
1815 if (v3) {
1816 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1817 txdr_hyper(&uiop->uio_offset, tl);
1818 tl += 2;
1819 *tl++ = txdr_unsigned(len);
1820 *tl++ = txdr_unsigned(*iomode);
1821 } else {
1822 nfsm_build(tl, u_long *, 4 * NFSX_UNSIGNED);
1823 *++tl = txdr_unsigned(uiop->uio_offset);
1824 tl += 2;
1825 }
1826 *tl = txdr_unsigned(len);
1827 FSDBG(537, vp, uiop->uio_offset, len, 0);
1828 nfsm_uiotom(uiop, len);
1829 nfsm_request(vp, NFSPROC_WRITE, p, cred, &xid);
1830 nmp = VFSTONFS(vnode_mount(vp));
1831 if (!nmp)
1832 error = ENXIO;
1833 if (v3) {
1834 if (mrep) {
1835 struct timespec premtime;
1836 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
1837 if (nfstimespeccmp(&VTONFS(vp)->n_mtime, &premtime, ==))
1838 updatemtime = 1;
1839 }
1840 if (!error) {
1841 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED +
1842 NFSX_V3WRITEVERF);
1843 rlen = fxdr_unsigned(int, *tl++);
1844 if (rlen <= 0) {
1845 error = NFSERR_IO;
1846 break;
1847 } else if (rlen < len) {
1848 backup = len - rlen;
1849 uio_iov_base_add(uiop, -backup);
1850 uio_iov_len_add(uiop, backup);
1851 uiop->uio_offset -= backup;
1852 uio_uio_resid_add(uiop, backup);
1853 len = rlen;
1854 }
1855 commit = fxdr_unsigned(int, *tl++);
1856
1857 /*
1858 * Return the lowest committment level
1859 * obtained by any of the RPCs.
1860 */
1861 if (committed == NFSV3WRITE_FILESYNC)
1862 committed = commit;
1863 else if (committed == NFSV3WRITE_DATASYNC &&
1864 commit == NFSV3WRITE_UNSTABLE)
1865 committed = commit;
1866 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1867 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1868 NFSX_V3WRITEVERF);
1869 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1870 } else if (bcmp((caddr_t)tl,
1871 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1872 *must_commit = 1;
1873 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1874 NFSX_V3WRITEVERF);
1875 }
1876 }
1877 } else {
1878 if (mrep) {
1879 nfsm_loadattr(vp, v3, NULL, &xid);
1880 }
1881 }
1882
1883 if (updatemtime)
1884 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.nva_mtime;
1885 mbuf_freem(mrep);
1886 /*
1887 * we seem to have a case where we end up looping on shutdown
1888 * and taking down nfs servers. For V3, error cases, there is
1889 * no way to terminate loop, if the len was 0, meaning,
1890 * nmp->nm_wsize was trashed. FreeBSD has this fix in it.
1891 * Let's try it.
1892 */
1893 if (error)
1894 break;
1895 tsiz -= len;
1896 }
1897 nfsmout:
1898 if ((mp = vnode_mount(vp)) && (vfs_flags(mp) & MNT_ASYNC))
1899 committed = NFSV3WRITE_FILESYNC;
1900 *iomode = committed;
1901 if (error)
1902 uio_uio_resid_set(uiop, tsiz);
1903 FSDBG_BOT(537, vp, committed, uio_uio_resid(uiop), error);
1904 return (error);
1905 }
1906
1907 /*
1908 * nfs mknod rpc
1909 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1910 * mode set to specify the file type and the size field for rdev.
1911 */
1912 static int
1913 nfs_mknodrpc(
1914 vnode_t dvp,
1915 vnode_t *vpp,
1916 struct componentname *cnp,
1917 struct vnode_attr *vap,
1918 kauth_cred_t cred,
1919 proc_t p)
1920 {
1921 register struct nfsv2_sattr *sp;
1922 register u_long *tl;
1923 register caddr_t cp;
1924 register long t1, t2;
1925 vnode_t newvp = (vnode_t)0;
1926 struct nfsnode *np = (struct nfsnode *)0;
1927 struct nfs_vattr nvattr;
1928 char *cp2;
1929 caddr_t bpos, dpos;
1930 int error = 0, wccpostattr = 0, gotvp = 0;
1931 struct timespec premtime = { 0, 0 };
1932 mbuf_t mreq, mrep, md, mb, mb2;
1933 u_long rdev;
1934 u_int64_t xid;
1935 int v3 = NFS_ISV3(dvp);
1936 int gotuid, gotgid;
1937
1938 if (!VATTR_IS_ACTIVE(vap, va_type))
1939 return (EINVAL);
1940 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1941 if (!VATTR_IS_ACTIVE(vap, va_rdev))
1942 return (EINVAL);
1943 rdev = txdr_unsigned(vap->va_rdev);
1944 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1945 rdev = 0xffffffff;
1946 else {
1947 return (ENOTSUP);
1948 }
1949 nfsm_reqhead(NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1950 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1951 if (error)
1952 return (error);
1953
1954 VATTR_SET_SUPPORTED(vap, va_mode);
1955 VATTR_SET_SUPPORTED(vap, va_uid);
1956 VATTR_SET_SUPPORTED(vap, va_gid);
1957 VATTR_SET_SUPPORTED(vap, va_data_size);
1958 VATTR_SET_SUPPORTED(vap, va_access_time);
1959 VATTR_SET_SUPPORTED(vap, va_modify_time);
1960 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
1961 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
1962
1963 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKNOD]);
1964 nfsm_fhtom(dvp, v3);
1965 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1966 if (v3) {
1967 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1968 *tl++ = vtonfsv3_type(vap->va_type);
1969 nfsm_v3sattr(vap);
1970 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1971 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1972 *tl++ = txdr_unsigned(major(vap->va_rdev));
1973 *tl = txdr_unsigned(minor(vap->va_rdev));
1974 }
1975 } else {
1976 struct timespec neg1time = { -1, -1 };
1977 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1978 sp->sa_mode = vtonfsv2_mode(vap->va_type,
1979 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
1980 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
1981 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
1982 sp->sa_size = rdev;
1983 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1984 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
1985 } else {
1986 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1987 }
1988 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1989 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1990 } else {
1991 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1992 }
1993 }
1994 nfsm_request(dvp, NFSPROC_MKNOD, p, cred, &xid);
1995 /* XXX no EEXIST kludge here? */
1996 if (!error) {
1997 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
1998 if (!gotvp) {
1999 error = nfs_lookitup(dvp, cnp->cn_nameptr,
2000 cnp->cn_namelen, cred, p, &np);
2001 if (!error)
2002 newvp = NFSTOV(np);
2003 }
2004 }
2005 if (v3 && mrep)
2006 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2007 if (!error && (gotuid || gotgid) &&
2008 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2009 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2010 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2011 /* clear ID bits if server didn't use them (or we can't tell) */
2012 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2013 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2014 }
2015 nfsm_reqdone;
2016 if (error) {
2017 if (newvp)
2018 vnode_put(newvp);
2019 } else {
2020 *vpp = newvp;
2021 }
2022 VTONFS(dvp)->n_flag |= NMODIFIED;
2023 /* if directory hadn't changed, update namecache mtime */
2024 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2025 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
2026 if (!wccpostattr)
2027 NATTRINVALIDATE(VTONFS(dvp));
2028 return (error);
2029 }
2030
2031 /*
2032 * nfs mknod vop
2033 * just call nfs_mknodrpc() to do the work.
2034 */
2035 /* ARGSUSED */
2036 static int
2037 nfs_mknod(ap)
2038 struct vnop_mknod_args /* {
2039 struct vnodeop_desc *a_desc;
2040 vnode_t a_dvp;
2041 vnode_t *a_vpp;
2042 struct componentname *a_cnp;
2043 struct vnode_attr *a_vap;
2044 vfs_context_t a_context;
2045 } */ *ap;
2046 {
2047 int error;
2048
2049 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap,
2050 vfs_context_ucred(ap->a_context),
2051 vfs_context_proc(ap->a_context));
2052
2053 return (error);
2054 }
2055
2056 static u_long create_verf;
2057 /*
2058 * nfs file create call
2059 */
2060 static int
2061 nfs_create(ap)
2062 struct vnop_create_args /* {
2063 struct vnodeop_desc *a_desc;
2064 vnode_t a_dvp;
2065 vnode_t *a_vpp;
2066 struct componentname *a_cnp;
2067 struct vnode_attr *a_vap;
2068 vfs_context_t a_context;
2069 } */ *ap;
2070 {
2071 vnode_t dvp = ap->a_dvp;
2072 struct vnode_attr *vap = ap->a_vap;
2073 struct componentname *cnp = ap->a_cnp;
2074 struct nfs_vattr nvattr;
2075 struct nfsv2_sattr *sp;
2076 u_long *tl;
2077 caddr_t cp;
2078 long t1, t2;
2079 struct nfsnode *np = (struct nfsnode *)0;
2080 vnode_t newvp = (vnode_t)0;
2081 caddr_t bpos, dpos, cp2;
2082 int error = 0, wccpostattr = 0, gotvp = 0, fmode = 0;
2083 struct timespec premtime = { 0, 0 };
2084 mbuf_t mreq, mrep, md, mb, mb2;
2085 int v3 = NFS_ISV3(dvp);
2086 int gotuid, gotgid;
2087 u_int64_t xid;
2088 kauth_cred_t cred;
2089 proc_t p;
2090
2091 cred = vfs_context_ucred(ap->a_context);
2092 p = vfs_context_proc(ap->a_context);
2093
2094 if (!VATTR_IS_ACTIVE(vap, va_type))
2095 return (EINVAL);
2096
2097 /*
2098 * Oops, not for me..
2099 */
2100 if (vap->va_type == VSOCK)
2101 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap, cred, p));
2102
2103 VATTR_SET_SUPPORTED(vap, va_mode);
2104 VATTR_SET_SUPPORTED(vap, va_uid);
2105 VATTR_SET_SUPPORTED(vap, va_gid);
2106 VATTR_SET_SUPPORTED(vap, va_data_size);
2107 VATTR_SET_SUPPORTED(vap, va_access_time);
2108 VATTR_SET_SUPPORTED(vap, va_modify_time);
2109 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2110 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2111
2112 if (vap->va_vaflags & VA_EXCLUSIVE)
2113 fmode |= O_EXCL;
2114 again:
2115 nfsm_reqhead(NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
2116 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
2117 if (error)
2118 return (error);
2119 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_CREATE]);
2120 nfsm_fhtom(dvp, v3);
2121 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2122 if (v3) {
2123 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
2124 if (fmode & O_EXCL) {
2125 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
2126 nfsm_build(tl, u_long *, NFSX_V3CREATEVERF);
2127 if (!TAILQ_EMPTY(&in_ifaddrhead))
2128 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
2129 else
2130 *tl++ = create_verf;
2131 *tl = ++create_verf;
2132 } else {
2133 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
2134 nfsm_v3sattr(vap);
2135 }
2136 } else {
2137 struct timespec neg1time = { -1, -1 };
2138 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2139 sp->sa_mode = vtonfsv2_mode(vap->va_type,
2140 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2141 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2142 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2143 sp->sa_size = 0;
2144 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2145 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2146 } else {
2147 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2148 }
2149 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2150 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2151 } else {
2152 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2153 }
2154 }
2155 nfsm_request(dvp, NFSPROC_CREATE, p, cred, &xid);
2156 if (!error) {
2157 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2158 if (!gotvp) {
2159 error = nfs_lookitup(dvp, cnp->cn_nameptr,
2160 cnp->cn_namelen, cred, p, &np);
2161 if (!error)
2162 newvp = NFSTOV(np);
2163 }
2164 }
2165 if (v3 && mrep)
2166 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2167 nfsm_reqdone;
2168 if (error) {
2169 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
2170 fmode &= ~O_EXCL;
2171 goto again;
2172 }
2173 if (newvp)
2174 vnode_put(newvp);
2175 } else if (v3 && (fmode & O_EXCL)) {
2176 error = nfs_setattrrpc(newvp, vap, cred, p);
2177 if (error && (gotuid || gotgid)) {
2178 /* it's possible the server didn't like our attempt to set IDs. */
2179 /* so, let's try it again without those */
2180 VATTR_CLEAR_ACTIVE(vap, va_uid);
2181 VATTR_CLEAR_ACTIVE(vap, va_gid);
2182 error = nfs_setattrrpc(newvp, vap, cred, p);
2183 }
2184 if (error)
2185 vnode_put(newvp);
2186 }
2187 if (!error) {
2188 *ap->a_vpp = newvp;
2189 }
2190 VTONFS(dvp)->n_flag |= NMODIFIED;
2191 /* if directory hadn't changed, update namecache mtime */
2192 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2193 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
2194 if (!wccpostattr)
2195 NATTRINVALIDATE(VTONFS(dvp));
2196 if (!error && (gotuid || gotgid) &&
2197 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2198 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2199 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2200 /* clear ID bits if server didn't use them (or we can't tell) */
2201 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2202 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2203 }
2204 return (error);
2205 }
2206
2207 /*
2208 * nfs file remove call
2209 * To try and make nfs semantics closer to ufs semantics, a file that has
2210 * other processes using the vnode is renamed instead of removed and then
2211 * removed later on the last close.
2212 * - If vnode_isinuse()
2213 * If a rename is not already in the works
2214 * call nfs_sillyrename() to set it up
2215 * else
2216 * do the remove rpc
2217 */
2218 static int
2219 nfs_remove(ap)
2220 struct vnop_remove_args /* {
2221 struct vnodeop_desc *a_desc;
2222 vnode_t a_dvp;
2223 vnode_t a_vp;
2224 struct componentname *a_cnp;
2225 int a_flags;
2226 vfs_context_t a_context;
2227 } */ *ap;
2228 {
2229 vnode_t vp = ap->a_vp;
2230 vnode_t dvp = ap->a_dvp;
2231 struct componentname *cnp = ap->a_cnp;
2232 struct nfsnode *np = VTONFS(vp);
2233 int error = 0, gofree = 0;
2234 struct nfs_vattr nvattr;
2235 kauth_cred_t cred;
2236 proc_t p;
2237
2238 cred = vfs_context_ucred(ap->a_context);
2239 p = vfs_context_proc(ap->a_context);
2240
2241 gofree = vnode_isinuse(vp, 0) ? 0 : 1;
2242 if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && !gofree) {
2243 /* Caller requested Carbon delete semantics, but file is busy */
2244 return (EBUSY);
2245 }
2246 if (gofree || (np->n_sillyrename &&
2247 nfs_getattr(vp, &nvattr, cred, p) == 0 &&
2248 nvattr.nva_nlink > 1)) {
2249 /*
2250 * Purge the name cache so that the chance of a lookup for
2251 * the name succeeding while the remove is in progress is
2252 * minimized.
2253 */
2254 cache_purge(vp);
2255 /*
2256 * throw away biocache buffers, mainly to avoid
2257 * unnecessary delayed writes later.
2258 */
2259 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
2260 np->n_size = 0;
2261 ubc_setsize(vp, (off_t)0); /* XXX check error */
2262 /* Do the rpc */
2263 if (error != EINTR)
2264 error = nfs_removerpc(dvp, cnp->cn_nameptr,
2265 cnp->cn_namelen, cred, p);
2266 /*
2267 * Kludge City: If the first reply to the remove rpc is lost..
2268 * the reply to the retransmitted request will be ENOENT
2269 * since the file was in fact removed
2270 * Therefore, we cheat and return success.
2271 */
2272 if (error == ENOENT)
2273 error = 0;
2274 if (!error) {
2275 /*
2276 * remove nfsnode from hash now so we can't accidentally find it
2277 * again if another object gets created with the same filehandle
2278 * before this vnode gets reclaimed
2279 */
2280 lck_mtx_lock(nfs_node_hash_mutex);
2281 LIST_REMOVE(np, n_hash);
2282 np->n_flag &= ~NHASHED;
2283 lck_mtx_unlock(nfs_node_hash_mutex);
2284 }
2285 if (!error && !np->n_sillyrename) {
2286 /* clear flags now: won't get nfs_inactive for recycled vnode */
2287 /* clear all flags other than these */
2288 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
2289 vnode_recycle(vp);
2290 }
2291 } else if (!np->n_sillyrename) {
2292 error = nfs_sillyrename(dvp, vp, cnp, cred, p);
2293 }
2294 NATTRINVALIDATE(np);
2295
2296 return (error);
2297 }
2298
2299 /*
2300 * nfs file remove rpc called from nfs_inactive
2301 */
2302 int
2303 nfs_removeit(struct sillyrename *sp)
2304 {
2305 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, NULL));
2306 }
2307
2308 /*
2309 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
2310 */
2311 static int
2312 nfs_removerpc(dvp, name, namelen, cred, proc)
2313 vnode_t dvp;
2314 char *name;
2315 int namelen;
2316 kauth_cred_t cred;
2317 proc_t proc;
2318 {
2319 register u_long *tl;
2320 register caddr_t cp;
2321 register long t1, t2;
2322 caddr_t bpos, dpos, cp2;
2323 int error = 0, wccpostattr = 0;
2324 struct timespec premtime = { 0, 0 };
2325 mbuf_t mreq, mrep, md, mb, mb2;
2326 int v3;
2327 u_int64_t xid;
2328
2329 if (!VFSTONFS(vnode_mount(dvp)))
2330 return (ENXIO);
2331 v3 = NFS_ISV3(dvp);
2332
2333 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
2334 if (error)
2335 return (error);
2336 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_REMOVE]);
2337 nfsm_fhtom(dvp, v3);
2338 nfsm_strtom(name, namelen, NFS_MAXNAMLEN, v3);
2339 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred, &xid);
2340 if (v3 && mrep)
2341 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2342 nfsm_reqdone;
2343 VTONFS(dvp)->n_flag |= NMODIFIED;
2344 /* if directory hadn't changed, update namecache mtime */
2345 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2346 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
2347 if (!wccpostattr)
2348 NATTRINVALIDATE(VTONFS(dvp));
2349 return (error);
2350 }
2351
2352 /*
2353 * nfs file rename call
2354 */
2355 static int
2356 nfs_rename(ap)
2357 struct vnop_rename_args /* {
2358 struct vnodeop_desc *a_desc;
2359 vnode_t a_fdvp;
2360 vnode_t a_fvp;
2361 struct componentname *a_fcnp;
2362 vnode_t a_tdvp;
2363 vnode_t a_tvp;
2364 struct componentname *a_tcnp;
2365 vfs_context_t a_context;
2366 } */ *ap;
2367 {
2368 vnode_t fvp = ap->a_fvp;
2369 vnode_t tvp = ap->a_tvp;
2370 vnode_t fdvp = ap->a_fdvp;
2371 vnode_t tdvp = ap->a_tdvp;
2372 struct componentname *tcnp = ap->a_tcnp;
2373 struct componentname *fcnp = ap->a_fcnp;
2374 int error, inuse=0;
2375 mount_t fmp, tdmp, tmp;
2376 struct nfsnode *tnp;
2377 kauth_cred_t cred;
2378 proc_t p;
2379
2380 cred = vfs_context_ucred(ap->a_context);
2381 p = vfs_context_proc(ap->a_context);
2382
2383 tnp = tvp ? VTONFS(tvp) : NULL;
2384
2385 /* Check for cross-device rename */
2386 fmp = vnode_mount(fvp);
2387 tmp = tvp ? vnode_mount(tvp) : NULL;
2388 tdmp = vnode_mount(tdvp);
2389 if ((fmp != tdmp) || (tvp && (fmp != tmp))) {
2390 error = EXDEV;
2391 goto out;
2392 }
2393
2394 /*
2395 * If the tvp exists and is in use, sillyrename it before doing the
2396 * rename of the new file over it.
2397 * XXX Can't sillyrename a directory.
2398 * Don't sillyrename if source and target are same vnode (hard
2399 * links or case-variants)
2400 */
2401 if (tvp && tvp != fvp) {
2402 inuse = vnode_isinuse(tvp, 0);
2403 }
2404 if (inuse && !tnp->n_sillyrename && vnode_vtype(tvp) != VDIR) {
2405 if ((error = nfs_sillyrename(tdvp, tvp, tcnp, cred, p))) {
2406 /* sillyrename failed. Instead of pressing on, return error */
2407 goto out; /* should not be ENOENT. */
2408 } else {
2409 /* sillyrename succeeded.*/
2410 tvp = NULL;
2411 }
2412 }
2413
2414 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2415 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, cred, p);
2416
2417 /*
2418 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2419 */
2420 if (error == ENOENT)
2421 error = 0;
2422
2423 if (!error && tvp && tvp != fvp && !tnp->n_sillyrename) {
2424 /*
2425 * remove nfsnode from hash now so we can't accidentally find it
2426 * again if another object gets created with the same filehandle
2427 * before this vnode gets reclaimed
2428 */
2429 lck_mtx_lock(nfs_node_hash_mutex);
2430 LIST_REMOVE(tnp, n_hash);
2431 tnp->n_flag &= ~NHASHED;
2432 lck_mtx_unlock(nfs_node_hash_mutex);
2433 }
2434
2435 /* purge the old name cache entries and enter the new one */
2436 cache_purge(fvp);
2437 if (tvp) {
2438 cache_purge(tvp);
2439 if (!error && !tnp->n_sillyrename) {
2440 /* clear flags now: won't get nfs_inactive for recycled vnode */
2441 /* clear all flags other than these */
2442 tnp->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
2443 vnode_recycle(tvp);
2444 }
2445 }
2446 if (!error)
2447 cache_enter(tdvp, fvp, tcnp);
2448
2449 out:
2450 /*
2451 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2452 */
2453 if (error == ENOENT)
2454 error = 0;
2455 return (error);
2456 }
2457
2458 /*
2459 * Do an nfs rename rpc. Called from nfs_rename() and nfs_sillyrename().
2460 */
2461 static int
2462 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
2463 vnode_t fdvp;
2464 char *fnameptr;
2465 int fnamelen;
2466 vnode_t tdvp;
2467 char *tnameptr;
2468 int tnamelen;
2469 kauth_cred_t cred;
2470 proc_t proc;
2471 {
2472 register u_long *tl;
2473 register caddr_t cp;
2474 register long t1, t2;
2475 caddr_t bpos, dpos, cp2;
2476 int error = 0, fwccpostattr = 0, twccpostattr = 0;
2477 struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 };
2478 mbuf_t mreq, mrep, md, mb, mb2;
2479 int v3;
2480 u_int64_t xid;
2481
2482 if (!VFSTONFS(vnode_mount(fdvp)))
2483 return (ENXIO);
2484 v3 = NFS_ISV3(fdvp);
2485
2486 nfsm_reqhead((NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
2487 nfsm_rndup(tnamelen));
2488 if (error)
2489 return (error);
2490 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_RENAME]);
2491 nfsm_fhtom(fdvp, v3);
2492 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN, v3);
2493 nfsm_fhtom(tdvp, v3);
2494 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN, v3);
2495 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred, &xid);
2496 if (v3 && mrep) {
2497 u_int64_t txid = xid;
2498
2499 nfsm_wcc_data(fdvp, &fpremtime, fwccpostattr, &xid);
2500 nfsm_wcc_data(tdvp, &tpremtime, twccpostattr, &txid);
2501 }
2502 nfsm_reqdone;
2503 VTONFS(fdvp)->n_flag |= NMODIFIED;
2504 /* if directory hadn't changed, update namecache mtime */
2505 if (nfstimespeccmp(&VTONFS(fdvp)->n_ncmtime, &fpremtime, ==))
2506 VTONFS(fdvp)->n_ncmtime = VTONFS(fdvp)->n_vattr.nva_mtime;
2507 if (!fwccpostattr)
2508 NATTRINVALIDATE(VTONFS(fdvp));
2509 VTONFS(tdvp)->n_flag |= NMODIFIED;
2510 /* if directory hadn't changed, update namecache mtime */
2511 if (nfstimespeccmp(&VTONFS(tdvp)->n_ncmtime, &tpremtime, ==))
2512 VTONFS(tdvp)->n_ncmtime = VTONFS(tdvp)->n_vattr.nva_mtime;
2513 if (!twccpostattr)
2514 NATTRINVALIDATE(VTONFS(tdvp));
2515 return (error);
2516 }
2517
2518 /*
2519 * nfs hard link create call
2520 */
2521 static int
2522 nfs_link(ap)
2523 struct vnop_link_args /* {
2524 struct vnodeop_desc *a_desc;
2525 vnode_t a_vp;
2526 vnode_t a_tdvp;
2527 struct componentname *a_cnp;
2528 vfs_context_t a_context;
2529 } */ *ap;
2530 {
2531 vnode_t vp = ap->a_vp;
2532 vnode_t tdvp = ap->a_tdvp;
2533 struct componentname *cnp = ap->a_cnp;
2534 u_long *tl;
2535 caddr_t cp;
2536 long t1, t2;
2537 caddr_t bpos, dpos, cp2;
2538 int error = 0, wccpostattr = 0, attrflag = 0;
2539 struct timespec premtime = { 0, 0 };
2540 mbuf_t mreq, mrep, md, mb, mb2;
2541 int v3;
2542 u_int64_t xid;
2543 kauth_cred_t cred;
2544 proc_t p;
2545
2546 if (vnode_mount(vp) != vnode_mount(tdvp)) {
2547 return (EXDEV);
2548 }
2549
2550 cred = vfs_context_ucred(ap->a_context);
2551 p = vfs_context_proc(ap->a_context);
2552
2553 v3 = NFS_ISV3(vp);
2554
2555 /*
2556 * Push all writes to the server, so that the attribute cache
2557 * doesn't get "out of sync" with the server.
2558 * XXX There should be a better way!
2559 */
2560 nfs_flush(vp, MNT_WAIT, cred, p, 0);
2561
2562 nfsm_reqhead(NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2563 if (error)
2564 return (error);
2565 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LINK]);
2566 nfsm_fhtom(vp, v3);
2567 nfsm_fhtom(tdvp, v3);
2568 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2569 nfsm_request(vp, NFSPROC_LINK, p, cred, &xid);
2570 if (v3 && mrep) {
2571 u_int64_t txid = xid;
2572
2573 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
2574 nfsm_wcc_data(tdvp, &premtime, wccpostattr, &txid);
2575 }
2576 nfsm_reqdone;
2577
2578 VTONFS(tdvp)->n_flag |= NMODIFIED;
2579 if (!attrflag)
2580 NATTRINVALIDATE(VTONFS(vp));
2581 /* if directory hadn't changed, update namecache mtime */
2582 if (nfstimespeccmp(&VTONFS(tdvp)->n_ncmtime, &premtime, ==))
2583 VTONFS(tdvp)->n_ncmtime = VTONFS(tdvp)->n_vattr.nva_mtime;
2584 if (!wccpostattr)
2585 NATTRINVALIDATE(VTONFS(tdvp));
2586 /*
2587 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2588 */
2589 if (error == EEXIST)
2590 error = 0;
2591 return (error);
2592 }
2593
2594 /*
2595 * nfs symbolic link create call
2596 */
2597 static int
2598 nfs_symlink(ap)
2599 struct vnop_symlink_args /* {
2600 struct vnodeop_desc *a_desc;
2601 vnode_t a_dvp;
2602 vnode_t *a_vpp;
2603 struct componentname *a_cnp;
2604 struct vnode_attr *a_vap;
2605 char *a_target;
2606 vfs_context_t a_context;
2607 } */ *ap;
2608 {
2609 vnode_t dvp = ap->a_dvp;
2610 struct vnode_attr *vap = ap->a_vap;
2611 struct componentname *cnp = ap->a_cnp;
2612 struct nfs_vattr nvattr;
2613 struct nfsv2_sattr *sp;
2614 u_long *tl;
2615 caddr_t cp;
2616 long t1, t2;
2617 caddr_t bpos, dpos, cp2;
2618 int slen, error = 0, wccpostattr = 0, gotvp = 0;
2619 struct timespec premtime = { 0, 0 };
2620 mbuf_t mreq, mrep, md, mb, mb2;
2621 vnode_t newvp = (vnode_t)0;
2622 int v3 = NFS_ISV3(dvp);
2623 int gotuid, gotgid;
2624 u_int64_t xid;
2625 kauth_cred_t cred;
2626 proc_t p;
2627 struct nfsnode *np = NULL;
2628
2629 cred = vfs_context_ucred(ap->a_context);
2630 p = vfs_context_proc(ap->a_context);
2631
2632 slen = strlen(ap->a_target);
2633 nfsm_reqhead(NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2634 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
2635 if (error)
2636 return (error);
2637
2638 VATTR_SET_SUPPORTED(vap, va_mode);
2639 VATTR_SET_SUPPORTED(vap, va_uid);
2640 VATTR_SET_SUPPORTED(vap, va_gid);
2641 VATTR_SET_SUPPORTED(vap, va_data_size);
2642 VATTR_SET_SUPPORTED(vap, va_access_time);
2643 VATTR_SET_SUPPORTED(vap, va_modify_time);
2644 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2645 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2646
2647 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_SYMLINK]);
2648 nfsm_fhtom(dvp, v3);
2649 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2650 if (v3) {
2651 nfsm_v3sattr(vap);
2652 }
2653 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN, v3);
2654 if (!v3) {
2655 struct timespec neg1time = { -1, -1 };
2656 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2657 sp->sa_mode = vtonfsv2_mode(VLNK,
2658 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2659 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2660 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2661 sp->sa_size = nfs_xdrneg1;
2662 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2663 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2664 } else {
2665 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2666 }
2667 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2668 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2669 } else {
2670 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2671 }
2672 }
2673 nfsm_request(dvp, NFSPROC_SYMLINK, p, cred, &xid);
2674 if (v3 && mrep) {
2675 u_int64_t dxid = xid;
2676
2677 if (!error)
2678 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2679 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
2680 }
2681 nfsm_reqdone;
2682
2683 VTONFS(dvp)->n_flag |= NMODIFIED;
2684 /* if directory hadn't changed, update namecache mtime */
2685 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2686 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
2687 if (!wccpostattr)
2688 NATTRINVALIDATE(VTONFS(dvp));
2689
2690 /*
2691 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2692 * if we can succeed in looking up the symlink.
2693 */
2694 if ((error == EEXIST) || (!error && !gotvp)) {
2695 if (newvp) {
2696 vnode_put(newvp);
2697 newvp = NULL;
2698 }
2699 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, cred, p, &np);
2700 if (!error) {
2701 newvp = NFSTOV(np);
2702 if (vnode_vtype(newvp) != VLNK)
2703 error = EEXIST;
2704 }
2705 }
2706 if (!error && (gotuid || gotgid) &&
2707 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2708 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2709 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2710 /* clear ID bits if server didn't use them (or we can't tell) */
2711 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2712 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2713 }
2714 if (error) {
2715 if (newvp)
2716 vnode_put(newvp);
2717 } else {
2718 *ap->a_vpp = newvp;
2719 }
2720 return (error);
2721 }
2722
2723 /*
2724 * nfs make dir call
2725 */
2726 static int
2727 nfs_mkdir(ap)
2728 struct vnop_mkdir_args /* {
2729 struct vnodeop_desc *a_desc;
2730 vnode_t a_dvp;
2731 vnode_t *a_vpp;
2732 struct componentname *a_cnp;
2733 struct vnode_attr *a_vap;
2734 vfs_context_t a_context;
2735 } */ *ap;
2736 {
2737 vnode_t dvp = ap->a_dvp;
2738 struct vnode_attr *vap = ap->a_vap;
2739 struct componentname *cnp = ap->a_cnp;
2740 struct nfs_vattr nvattr;
2741 struct nfsv2_sattr *sp;
2742 u_long *tl;
2743 caddr_t cp;
2744 long t1, t2;
2745 int len;
2746 struct nfsnode *np = (struct nfsnode *)0;
2747 vnode_t newvp = (vnode_t)0;
2748 caddr_t bpos, dpos, cp2;
2749 int error = 0, wccpostattr = 0;
2750 struct timespec premtime = { 0, 0 };
2751 int gotvp = 0;
2752 mbuf_t mreq, mrep, md, mb, mb2;
2753 int v3 = NFS_ISV3(dvp);
2754 int gotuid, gotgid;
2755 u_int64_t xid, dxid;
2756 kauth_cred_t cred;
2757 proc_t p;
2758
2759 cred = vfs_context_ucred(ap->a_context);
2760 p = vfs_context_proc(ap->a_context);
2761
2762 len = cnp->cn_namelen;
2763 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2764 if (error)
2765 return (error);
2766
2767 VATTR_SET_SUPPORTED(vap, va_mode);
2768 VATTR_SET_SUPPORTED(vap, va_uid);
2769 VATTR_SET_SUPPORTED(vap, va_gid);
2770 VATTR_SET_SUPPORTED(vap, va_data_size);
2771 VATTR_SET_SUPPORTED(vap, va_access_time);
2772 VATTR_SET_SUPPORTED(vap, va_modify_time);
2773 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2774 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2775
2776 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKDIR]);
2777 nfsm_fhtom(dvp, v3);
2778 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN, v3);
2779 if (v3) {
2780 nfsm_v3sattr(vap);
2781 } else {
2782 struct timespec neg1time = { -1, -1 };
2783 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2784 sp->sa_mode = vtonfsv2_mode(VDIR,
2785 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2786 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2787 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2788 sp->sa_size = nfs_xdrneg1;
2789 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2790 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2791 } else {
2792 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2793 }
2794 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2795 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2796 } else {
2797 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2798 }
2799 }
2800 nfsm_request(dvp, NFSPROC_MKDIR, p, cred, &xid);
2801 dxid = xid;
2802 if (!error)
2803 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2804 if (v3 && mrep)
2805 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
2806 nfsm_reqdone;
2807 VTONFS(dvp)->n_flag |= NMODIFIED;
2808 /* if directory hadn't changed, update namecache mtime */
2809 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2810 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
2811 if (!wccpostattr)
2812 NATTRINVALIDATE(VTONFS(dvp));
2813 /*
2814 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2815 * if we can succeed in looking up the directory.
2816 */
2817 if (error == EEXIST || (!error && !gotvp)) {
2818 if (newvp) {
2819 vnode_put(newvp);
2820 newvp = NULL;
2821 }
2822 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cred, p, &np);
2823 if (!error) {
2824 newvp = NFSTOV(np);
2825 if (vnode_vtype(newvp) != VDIR)
2826 error = EEXIST;
2827 }
2828 }
2829 if (!error && (gotuid || gotgid) &&
2830 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2831 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2832 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2833 /* clear ID bits if server didn't use them (or we can't tell) */
2834 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2835 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2836 }
2837 if (error) {
2838 if (newvp)
2839 vnode_put(newvp);
2840 } else {
2841 *ap->a_vpp = newvp;
2842 }
2843 return (error);
2844 }
2845
2846 /*
2847 * nfs remove directory call
2848 */
2849 static int
2850 nfs_rmdir(ap)
2851 struct vnop_rmdir_args /* {
2852 struct vnodeop_desc *a_desc;
2853 vnode_t a_dvp;
2854 vnode_t a_vp;
2855 struct componentname *a_cnp;
2856 vfs_context_t a_context;
2857 } */ *ap;
2858 {
2859 vnode_t vp = ap->a_vp;
2860 vnode_t dvp = ap->a_dvp;
2861 struct componentname *cnp = ap->a_cnp;
2862 u_long *tl;
2863 caddr_t cp;
2864 long t1, t2;
2865 caddr_t bpos, dpos, cp2;
2866 int error = 0, wccpostattr = 0;
2867 struct timespec premtime = { 0, 0 };
2868 mbuf_t mreq, mrep, md, mb, mb2;
2869 int v3 = NFS_ISV3(dvp);
2870 u_int64_t xid;
2871 kauth_cred_t cred;
2872 proc_t p;
2873
2874 cred = vfs_context_ucred(ap->a_context);
2875 p = vfs_context_proc(ap->a_context);
2876
2877 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2878 if (error)
2879 return (error);
2880 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_RMDIR]);
2881 nfsm_fhtom(dvp, v3);
2882 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2883 nfsm_request(dvp, NFSPROC_RMDIR, p, cred, &xid);
2884 if (v3 && mrep)
2885 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2886 nfsm_reqdone;
2887 VTONFS(dvp)->n_flag |= NMODIFIED;
2888 /* if directory hadn't changed, update namecache mtime */
2889 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2890 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
2891 if (!wccpostattr)
2892 NATTRINVALIDATE(VTONFS(dvp));
2893 cache_purge(vp);
2894 /*
2895 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2896 */
2897 if (error == ENOENT)
2898 error = 0;
2899 if (!error) {
2900 /*
2901 * remove nfsnode from hash now so we can't accidentally find it
2902 * again if another object gets created with the same filehandle
2903 * before this vnode gets reclaimed
2904 */
2905 lck_mtx_lock(nfs_node_hash_mutex);
2906 LIST_REMOVE(VTONFS(vp), n_hash);
2907 VTONFS(vp)->n_flag &= ~NHASHED;
2908 lck_mtx_unlock(nfs_node_hash_mutex);
2909 }
2910 return (error);
2911 }
2912
2913 /*
2914 * nfs readdir call
2915 */
2916 static int
2917 nfs_readdir(ap)
2918 struct vnop_readdir_args /* {
2919 struct vnodeop_desc *a_desc;
2920 vnode_t a_vp;
2921 struct uio *a_uio;
2922 int *a_eofflag;
2923 int *a_ncookies;
2924 u_long **a_cookies;
2925 vfs_context_t a_context;
2926 } */ *ap;
2927 {
2928 vnode_t vp = ap->a_vp;
2929 struct nfsnode *np = VTONFS(vp);
2930 struct uio *uio = ap->a_uio;
2931 int tresid, error;
2932 struct nfs_vattr nvattr;
2933 kauth_cred_t cred;
2934 proc_t p;
2935
2936 if (vnode_vtype(vp) != VDIR)
2937 return (EPERM);
2938
2939 cred = vfs_context_ucred(ap->a_context);
2940 p = vfs_context_proc(ap->a_context);
2941
2942 /*
2943 * First, check for hit on the EOF offset cache
2944 */
2945 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2946 (np->n_flag & NMODIFIED) == 0) {
2947 if (!nfs_getattr(vp, &nvattr, cred, p)) {
2948 if (nfstimespeccmp(&np->n_mtime, &nvattr.nva_mtime, ==)) {
2949 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_hits);
2950 return (0);
2951 }
2952 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=)) {
2953 /* directory changed, purge any name cache entries */
2954 cache_purge(vp);
2955 }
2956 }
2957 }
2958
2959 /*
2960 * Call nfs_bioread() to do the real work.
2961 */
2962 // LP64todo - fix this
2963 tresid = uio_uio_resid(uio);
2964 error = nfs_bioread(vp, uio, 0, cred, p);
2965
2966 if (!error && uio_uio_resid(uio) == tresid)
2967 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_misses);
2968 return (error);
2969 }
2970
2971 /*
2972 * Readdir rpc call.
2973 * Called from below the buffer cache by nfs_doio().
2974 */
2975 int
2976 nfs_readdirrpc(
2977 vnode_t vp,
2978 struct uio *uiop,
2979 kauth_cred_t cred,
2980 proc_t p)
2981 {
2982 register int len, skiplen, left;
2983 register struct dirent *dp;
2984 register u_long *tl;
2985 register caddr_t cp;
2986 register long t1, t2;
2987 register nfsuint64 *cookiep;
2988 caddr_t bpos, dpos, cp2;
2989 mbuf_t mreq, mrep, md, mb, mb2;
2990 nfsuint64 cookie;
2991 struct nfsmount *nmp;
2992 struct nfsnode *dnp = VTONFS(vp);
2993 u_quad_t fileno;
2994 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2995 int attrflag;
2996 int v3, nmreaddirsize;
2997 u_int64_t xid;
2998
2999 #ifndef nolint
3000 dp = (struct dirent *)0;
3001 #endif
3002 #if DIAGNOSTIC
3003 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (NFS_DIRBLKSIZ - 1)) ||
3004 (uio_uio_resid(uiop) & (NFS_DIRBLKSIZ - 1)))
3005 panic("nfs_readdirrpc: bad uio");
3006 #endif
3007 nmp = VFSTONFS(vnode_mount(vp));
3008 if (!nmp)
3009 return (ENXIO);
3010 v3 = NFS_ISV3(vp);
3011 nmreaddirsize = nmp->nm_readdirsize;
3012
3013 /*
3014 * If there is no cookie, assume directory was stale.
3015 */
3016 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3017 if (cookiep)
3018 cookie = *cookiep;
3019 else
3020 return (NFSERR_BAD_COOKIE);
3021 /*
3022 * Loop around doing readdir rpc's of size nm_readdirsize
3023 * truncated to a multiple of DIRBLKSIZ.
3024 * The stopping criteria is EOF or buffer full.
3025 */
3026 while (more_dirs && bigenough) {
3027 nfsm_reqhead(NFSX_FH(v3) + NFSX_READDIR(v3));
3028 if (error)
3029 goto nfsmout;
3030 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIR]);
3031 nfsm_fhtom(vp, v3);
3032 if (v3) {
3033 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
3034 *tl++ = cookie.nfsuquad[0];
3035 *tl++ = cookie.nfsuquad[1];
3036 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3037 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3038 } else {
3039 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
3040 *tl++ = cookie.nfsuquad[0];
3041 }
3042 *tl = txdr_unsigned(nmreaddirsize);
3043 nfsm_request(vp, NFSPROC_READDIR, p, cred, &xid);
3044 if (v3) {
3045 if (mrep) {
3046 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
3047 }
3048 if (!error) {
3049 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3050 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3051 dnp->n_cookieverf.nfsuquad[1] = *tl;
3052 } else {
3053 mbuf_freem(mrep);
3054 goto nfsmout;
3055 }
3056 } else if (!mrep) {
3057 // XXX assert error?
3058 goto nfsmout;
3059 }
3060 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3061 more_dirs = fxdr_unsigned(int, *tl);
3062
3063 /* loop thru the dir entries, doctoring them to 4bsd form */
3064 while (more_dirs && bigenough) {
3065 if (v3) {
3066 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3067 fxdr_hyper(tl, &fileno);
3068 len = fxdr_unsigned(int, *(tl + 2));
3069 } else {
3070 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3071 fileno = fxdr_unsigned(u_quad_t, *tl++);
3072 len = fxdr_unsigned(int, *tl);
3073 }
3074 /* Note: v3 supports longer names, but struct dirent doesn't */
3075 /* so we just truncate the names to fit */
3076 if (len <= 0) {
3077 error = EBADRPC;
3078 mbuf_freem(mrep);
3079 goto nfsmout;
3080 }
3081 if (len > MAXNAMLEN) {
3082 skiplen = len - MAXNAMLEN;
3083 len = MAXNAMLEN;
3084 } else {
3085 skiplen = 0;
3086 }
3087 tlen = nfsm_rndup(len);
3088 if (tlen == len)
3089 tlen += 4; /* To ensure null termination */
3090 left = DIRBLKSIZ - blksiz;
3091 if ((tlen + (int)DIRHDSIZ) > left) {
3092 dp->d_reclen += left;
3093 uio_iov_base_add(uiop, left);
3094 uio_iov_len_add(uiop, -left);
3095 uiop->uio_offset += left;
3096 uio_uio_resid_add(uiop, -left);
3097 blksiz = 0;
3098 }
3099 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
3100 bigenough = 0;
3101 if (bigenough) {
3102 // LP64todo - fix this!
3103 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3104 dp->d_fileno = (int)fileno;
3105 dp->d_namlen = len;
3106 dp->d_reclen = tlen + DIRHDSIZ;
3107 dp->d_type = DT_UNKNOWN;
3108 blksiz += dp->d_reclen;
3109 if (blksiz == DIRBLKSIZ)
3110 blksiz = 0;
3111 uiop->uio_offset += DIRHDSIZ;
3112 #if LP64KERN
3113 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3114 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3115 #else
3116 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3117 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3118 #endif
3119 uio_iov_base_add(uiop, DIRHDSIZ);
3120 nfsm_mtouio(uiop, len);
3121 // LP64todo - fix this!
3122 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
3123 tlen -= len;
3124 *cp = '\0'; /* null terminate */
3125 uio_iov_base_add(uiop, tlen);
3126 uio_iov_len_add(uiop, -tlen);
3127 uiop->uio_offset += tlen;
3128 uio_uio_resid_add(uiop, -tlen);
3129 } else {
3130 nfsm_adv(nfsm_rndup(len));
3131 }
3132 if (skiplen)
3133 nfsm_adv(nfsm_rndup(skiplen));
3134 if (v3) {
3135 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3136 } else {
3137 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3138 }
3139 if (bigenough) {
3140 cookie.nfsuquad[0] = *tl++;
3141 if (v3)
3142 cookie.nfsuquad[1] = *tl++;
3143 } else if (v3)
3144 tl += 2;
3145 else
3146 tl++;
3147 more_dirs = fxdr_unsigned(int, *tl);
3148 }
3149 /*
3150 * If at end of rpc data, get the eof boolean
3151 */
3152 if (!more_dirs) {
3153 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3154 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3155 }
3156 mbuf_freem(mrep);
3157 }
3158 /*
3159 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3160 * by increasing d_reclen for the last record.
3161 */
3162 if (blksiz > 0) {
3163 left = DIRBLKSIZ - blksiz;
3164 dp->d_reclen += left;
3165 uio_iov_base_add(uiop, left);
3166 uio_iov_len_add(uiop, -left);
3167 uiop->uio_offset += left;
3168 uio_uio_resid_add(uiop, -left);
3169 }
3170
3171 /*
3172 * We are now either at the end of the directory or have filled the
3173 * block.
3174 */
3175 if (bigenough)
3176 dnp->n_direofoffset = uiop->uio_offset;
3177 else {
3178 if (uio_uio_resid(uiop) > 0)
3179 printf("EEK! readdirrpc resid > 0\n");
3180 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
3181 if (cookiep)
3182 *cookiep = cookie;
3183 }
3184 nfsmout:
3185 return (error);
3186 }
3187
3188 /*
3189 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
3190 */
3191 int
3192 nfs_readdirplusrpc(
3193 vnode_t vp,
3194 struct uio *uiop,
3195 kauth_cred_t cred,
3196 proc_t p)
3197 {
3198 int len, skiplen, left;
3199 struct dirent *dp;
3200 u_long *tl;
3201 caddr_t cp;
3202 long t1, t2;
3203 vnode_t newvp;
3204 nfsuint64 *cookiep;
3205 caddr_t bpos, dpos, cp2;
3206 mbuf_t mreq, mrep, md, mb, mb2;
3207 struct componentname cn, *cnp = &cn;
3208 nfsuint64 cookie;
3209 struct nfsmount *nmp;
3210 struct nfsnode *dnp = VTONFS(vp), *np;
3211 u_char *fhp;
3212 u_quad_t fileno;
3213 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
3214 int attrflag, fhsize, nmreaddirsize, nmrsize;
3215 u_int64_t xid, savexid;
3216 struct nfs_vattr nvattr;
3217
3218 #ifndef nolint
3219 dp = (struct dirent *)0;
3220 #endif
3221 #if DIAGNOSTIC
3222 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
3223 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
3224 panic("nfs_readdirplusrpc: bad uio");
3225 #endif
3226 nmp = VFSTONFS(vnode_mount(vp));
3227 if (!nmp)
3228 return (ENXIO);
3229 nmreaddirsize = nmp->nm_readdirsize;
3230 nmrsize = nmp->nm_rsize;
3231
3232 bzero(cnp, sizeof(*cnp));
3233 newvp = NULLVP;
3234
3235 /*
3236 * If there is no cookie, assume directory was stale.
3237 */
3238 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3239 if (cookiep)
3240 cookie = *cookiep;
3241 else
3242 return (NFSERR_BAD_COOKIE);
3243 /*
3244 * Loop around doing readdir rpc's of size nm_readdirsize
3245 * truncated to a multiple of DIRBLKSIZ.
3246 * The stopping criteria is EOF or buffer full.
3247 */
3248 while (more_dirs && bigenough) {
3249 nfsm_reqhead(NFSX_FH(1) + 6 * NFSX_UNSIGNED);
3250 if (error)
3251 goto nfsmout;
3252 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIRPLUS]);
3253 nfsm_fhtom(vp, 1);
3254 nfsm_build(tl, u_long *, 6 * NFSX_UNSIGNED);
3255 *tl++ = cookie.nfsuquad[0];
3256 *tl++ = cookie.nfsuquad[1];
3257 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3258 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3259 *tl++ = txdr_unsigned(nmreaddirsize);
3260 *tl = txdr_unsigned(nmrsize);
3261 nfsm_request(vp, NFSPROC_READDIRPLUS, p, cred, &xid);
3262 savexid = xid;
3263 if (mrep) {
3264 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
3265 }
3266 if (error) {
3267 mbuf_freem(mrep);
3268 goto nfsmout;
3269 }
3270 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3271 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3272 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3273 more_dirs = fxdr_unsigned(int, *tl);
3274
3275 /* loop thru the dir entries, doctoring them to 4bsd form */
3276 while (more_dirs && bigenough) {
3277 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3278 fxdr_hyper(tl, &fileno);
3279 len = fxdr_unsigned(int, *(tl + 2));
3280 /* Note: v3 supports longer names, but struct dirent doesn't */
3281 /* so we just truncate the names to fit */
3282 if (len <= 0) {
3283 error = EBADRPC;
3284 mbuf_freem(mrep);
3285 goto nfsmout;
3286 }
3287 if (len > MAXNAMLEN) {
3288 skiplen = len - MAXNAMLEN;
3289 len = MAXNAMLEN;
3290 } else {
3291 skiplen = 0;
3292 }
3293 tlen = nfsm_rndup(len);
3294 if (tlen == len)
3295 tlen += 4; /* To ensure null termination*/
3296 left = DIRBLKSIZ - blksiz;
3297 if ((tlen + (int)DIRHDSIZ) > left) {
3298 dp->d_reclen += left;
3299 uio_iov_base_add(uiop, left);
3300 uio_iov_len_add(uiop, -left);
3301 uiop->uio_offset += left;
3302 uio_uio_resid_add(uiop, -left);
3303 blksiz = 0;
3304 }
3305 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
3306 bigenough = 0;
3307 if (bigenough) {
3308 // LP64todo - fix this!
3309 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
3310 dp->d_fileno = (int)fileno;
3311 dp->d_namlen = len;
3312 dp->d_reclen = tlen + DIRHDSIZ;
3313 dp->d_type = DT_UNKNOWN;
3314 blksiz += dp->d_reclen;
3315 if (blksiz == DIRBLKSIZ)
3316 blksiz = 0;
3317 uiop->uio_offset += DIRHDSIZ;
3318 #if LP64KERN
3319 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3320 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3321 #else
3322 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3323 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3324 #endif
3325 uio_iov_base_add(uiop, DIRHDSIZ);
3326 // LP64todo - fix this!
3327 cnp->cn_nameptr = CAST_DOWN(caddr_t, uio_iov_base(uiop));
3328 cnp->cn_namelen = len;
3329 nfsm_mtouio(uiop, len);
3330 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
3331 tlen -= len;
3332 *cp = '\0';
3333 uio_iov_base_add(uiop, tlen);
3334 uio_iov_len_add(uiop, -tlen);
3335 uiop->uio_offset += tlen;
3336 uio_uio_resid_add(uiop, -tlen);
3337 } else {
3338 nfsm_adv(nfsm_rndup(len));
3339 }
3340 if (skiplen)
3341 nfsm_adv(nfsm_rndup(skiplen));
3342 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3343 if (bigenough) {
3344 cookie.nfsuquad[0] = *tl++;
3345 cookie.nfsuquad[1] = *tl++;
3346 } else
3347 tl += 2;
3348
3349 /*
3350 * Since the attributes are before the file handle
3351 * (sigh), we must skip over the attributes and then
3352 * come back and get them.
3353 */
3354 attrflag = fxdr_unsigned(int, *tl);
3355 if (attrflag) {
3356 /* grab attributes */
3357 nfsm_attr_get(1, &nvattr);
3358 dp->d_type = IFTODT(VTTOIF(nvattr.nva_type));
3359 /* check for file handle */
3360 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3361 doit = fxdr_unsigned(int, *tl);
3362 if (doit) {
3363 nfsm_getfh(fhp, fhsize, 1);
3364 if (NFS_CMPFH(dnp, fhp, fhsize)) {
3365 error = vnode_ref(vp);
3366 if (error) {
3367 doit = 0;
3368 } else {
3369 newvp = vp;
3370 np = dnp;
3371 }
3372 } else if (!bigenough ||
3373 (cnp->cn_namelen == 2 &&
3374 cnp->cn_nameptr[1] == '.' &&
3375 cnp->cn_nameptr[0] == '.')) {
3376 /*
3377 * XXXmacko I don't think this ".." thing is a problem anymore.
3378 * don't doit if we can't guarantee
3379 * that this entry is NOT ".." because
3380 * we would have to drop the lock on
3381 * the directory before getting the
3382 * lock on the ".." vnode... and we
3383 * don't want to drop the dvp lock in
3384 * the middle of a readdirplus.
3385 */
3386 doit = 0;
3387 } else {
3388 cnp->cn_hash = 0;
3389
3390 error = nfs_nget(vnode_mount(vp), vp, cnp,
3391 fhp, fhsize, &nvattr, &xid,
3392 NG_MAKEENTRY, &np);
3393 if (error)
3394 doit = 0;
3395 else
3396 newvp = NFSTOV(np);
3397 }
3398 }
3399 /* update attributes if not already updated */
3400 if (doit && bigenough && (np->n_xid <= savexid)) {
3401 xid = savexid;
3402 nfs_loadattrcache(np, &nvattr, &xid, 0);
3403 /* any error can be ignored */
3404 }
3405 } else {
3406 /* Just skip over the file handle */
3407 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3408 i = fxdr_unsigned(int, *tl);
3409 nfsm_adv(nfsm_rndup(i));
3410 }
3411 if (newvp != NULLVP) {
3412 if (newvp == vp)
3413 vnode_rele(newvp);
3414 else
3415 vnode_put(newvp);
3416 newvp = NULLVP;
3417 }
3418 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3419 more_dirs = fxdr_unsigned(int, *tl);
3420 }
3421 /*
3422 * If at end of rpc data, get the eof boolean
3423 */
3424 if (!more_dirs) {
3425 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3426 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3427 }
3428 mbuf_freem(mrep);
3429 }
3430 /*
3431 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
3432 * by increasing d_reclen for the last record.
3433 */
3434 if (blksiz > 0) {
3435 left = DIRBLKSIZ - blksiz;
3436 dp->d_reclen += left;
3437 uio_iov_base_add(uiop, left);
3438 uio_iov_len_add(uiop, -left);
3439 uiop->uio_offset += left;
3440 uio_uio_resid_add(uiop, -left);
3441 }
3442
3443 /*
3444 * We are now either at the end of the directory or have filled the
3445 * block.
3446 */
3447 if (bigenough)
3448 dnp->n_direofoffset = uiop->uio_offset;
3449 else {
3450 if (uio_uio_resid(uiop) > 0)
3451 printf("EEK! readdirplusrpc resid > 0\n");
3452 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
3453 if (cookiep)
3454 *cookiep = cookie;
3455 }
3456 nfsmout:
3457 return (error);
3458 }
3459
3460 /*
3461 * Silly rename. To make the NFS filesystem that is stateless look a little
3462 * more like the "ufs" a remove of an active vnode is translated to a rename
3463 * to a funny looking filename that is removed by nfs_inactive on the
3464 * nfsnode. There is the potential for another process on a different client
3465 * to create the same funny name between the nfs_lookitup() fails and the
3466 * nfs_rename() completes, but...
3467 */
3468
3469 /* format of "random" names and next name to try */
3470 /* (note: shouldn't exceed size of sillyrename.s_name) */
3471 static char sillyrename_name[] = ".nfsAAA%04x4.4";
3472
3473 static int
3474 nfs_sillyrename(
3475 vnode_t dvp,
3476 vnode_t vp,
3477 struct componentname *cnp,
3478 kauth_cred_t cred,
3479 proc_t p)
3480 {
3481 register struct sillyrename *sp;
3482 struct nfsnode *np;
3483 int error;
3484 short pid;
3485 kauth_cred_t tmpcred;
3486 int i, j, k;
3487
3488 cache_purge(vp);
3489 np = VTONFS(vp);
3490 #if DIAGNOSTIC
3491 if (vnode_vtype(vp) == VDIR)
3492 panic("nfs_sillyrename: dir");
3493 #endif
3494 MALLOC_ZONE(sp, struct sillyrename *,
3495 sizeof (struct sillyrename), M_NFSREQ, M_WAITOK);
3496 if (!sp)
3497 return (ENOMEM);
3498 kauth_cred_ref(cred);
3499 sp->s_cred = cred;
3500 sp->s_dvp = dvp;
3501 error = vnode_ref(dvp);
3502 if (error)
3503 goto bad_norele;
3504
3505 /* Fudge together a funny name */
3506 pid = proc_pid(p);
3507 sp->s_namlen = sprintf(sp->s_name, sillyrename_name, pid);
3508
3509 /* Try lookitups until we get one that isn't there */
3510 i = j = k = 0;
3511 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, NULL) == 0) {
3512 if (sp->s_name[4]++ >= 'z')
3513 sp->s_name[4] = 'A';
3514 if (++i > ('z' - 'A' + 1)) {
3515 i = 0;
3516 if (sp->s_name[5]++ >= 'z')
3517 sp->s_name[5] = 'A';
3518 if (++j > ('z' - 'A' + 1)) {
3519 j = 0;
3520 if (sp->s_name[6]++ >= 'z')
3521 sp->s_name[6] = 'A';
3522 if (++k > ('z' - 'A' + 1)) {
3523 error = EINVAL;
3524 goto bad;
3525 }
3526 }
3527 }
3528 }
3529 /* make note of next "random" name to try */
3530 if ((sillyrename_name[4] = (sp->s_name[4] + 1)) > 'z') {
3531 sillyrename_name[4] = 'A';
3532 if ((sillyrename_name[5] = (sp->s_name[5] + 1)) > 'z') {
3533 sillyrename_name[5] = 'A';
3534 if ((sillyrename_name[6] = (sp->s_name[6] + 1)) > 'z')
3535 sillyrename_name[6] = 'A';
3536 }
3537 }
3538 /* now, do the rename */
3539 error = nfs_renamerpc(dvp, cnp->cn_nameptr, cnp->cn_namelen,
3540 dvp, sp->s_name, sp->s_namlen, sp->s_cred, p);
3541 if (error)
3542 goto bad;
3543 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, &np);
3544 #if DIAGNOSTIC
3545 kprintf("sillyrename: %s, vp=%x, np=%x, dvp=%x\n",
3546 &sp->s_name[0], (unsigned)vp, (unsigned)np, (unsigned)dvp);
3547 #endif
3548 np->n_sillyrename = sp;
3549 return (0);
3550 bad:
3551 vnode_rele(sp->s_dvp);
3552 bad_norele:
3553 tmpcred = sp->s_cred;
3554 sp->s_cred = NOCRED;
3555 kauth_cred_rele(tmpcred);
3556 FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ);
3557 return (error);
3558 }
3559
3560 /*
3561 * Look up a file name and optionally either update the file handle or
3562 * allocate an nfsnode, depending on the value of npp.
3563 * npp == NULL --> just do the lookup
3564 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3565 * handled too
3566 * *npp != NULL --> update the file handle in the vnode
3567 */
3568 static int
3569 nfs_lookitup(dvp, name, len, cred, procp, npp)
3570 vnode_t dvp;
3571 char *name;
3572 int len;
3573 kauth_cred_t cred;
3574 proc_t procp;
3575 struct nfsnode **npp;
3576 {
3577 u_long *tl;
3578 caddr_t cp;
3579 long t1, t2;
3580 vnode_t newvp = (vnode_t)0;
3581 struct nfsnode *np, *dnp = VTONFS(dvp);
3582 caddr_t bpos, dpos, cp2;
3583 int error = 0, fhlen, attrflag;
3584 mbuf_t mreq, mrep, md, mb, mb2;
3585 u_char *nfhp;
3586 int v3;
3587 u_int64_t xid, dxid, savedxid;
3588 struct nfs_vattr nvattr;
3589
3590 if (!VFSTONFS(vnode_mount(dvp)))
3591 return (ENXIO);
3592 v3 = NFS_ISV3(dvp);
3593
3594 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3595 if (error)
3596 return (error);
3597 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LOOKUP]);
3598 nfsm_fhtom(dvp, v3);
3599 nfsm_strtom(name, len, NFS_MAXNAMLEN, v3);
3600 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred, &xid);
3601 if (npp && !error) {
3602 savedxid = xid;
3603 nfsm_getfh(nfhp, fhlen, v3);
3604 /* get attributes */
3605 if (v3) {
3606 nfsm_postop_attr_get(v3, attrflag, &nvattr);
3607 if (!attrflag) {
3608 /* We need valid attributes in order */
3609 /* to call nfs_nget/vnode_create(). */
3610 error = nfs_getattr_no_vnode(vnode_mount(dvp),
3611 nfhp, fhlen, cred, procp, &nvattr, &xid);
3612 if (error) {
3613 mbuf_freem(mrep);
3614 goto nfsmout;
3615 }
3616 }
3617 dxid = savedxid;
3618 nfsm_postop_attr_update(dvp, v3, attrflag, &dxid);
3619 } else {
3620 nfsm_attr_get(v3, &nvattr);
3621 }
3622 if (*npp) {
3623 np = *npp;
3624 if (fhlen != np->n_fhsize) {
3625 u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL;
3626 if (fhlen > NFS_SMALLFH) {
3627 MALLOC_ZONE(np->n_fhp, u_char *, fhlen, M_NFSBIGFH, M_WAITOK);
3628 if (!np->n_fhp) {
3629 np->n_fhp = oldbuf;
3630 error = ENOMEM;
3631 mbuf_freem(mrep);
3632 goto nfsmout;
3633 }
3634 } else {
3635 np->n_fhp = &np->n_fh[0];
3636 }
3637 if (oldbuf) {
3638 FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH);
3639 }
3640 }
3641 bcopy(nfhp, np->n_fhp, fhlen);
3642 np->n_fhsize = fhlen;
3643 newvp = NFSTOV(np);
3644 error = nfs_loadattrcache(np, &nvattr, &xid, 0);
3645 if (error) {
3646 mbuf_freem(mrep);
3647 goto nfsmout;
3648 }
3649 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3650 newvp = dvp;
3651 if (dnp->n_xid <= savedxid) {
3652 dxid = savedxid;
3653 error = nfs_loadattrcache(dnp, &nvattr, &dxid, 0);
3654 if (error) {
3655 mbuf_freem(mrep);
3656 goto nfsmout;
3657 }
3658 }
3659 } else {
3660 struct componentname cn, *cnp = &cn;
3661 bzero(cnp, sizeof(*cnp));
3662 cnp->cn_nameptr = name;
3663 cnp->cn_namelen = len;
3664
3665 error = nfs_nget(vnode_mount(dvp), dvp, cnp, nfhp, fhlen,
3666 &nvattr, &xid, NG_MAKEENTRY, &np);
3667 if (error) {
3668 mbuf_freem(mrep);
3669 return (error);
3670 }
3671 newvp = NFSTOV(np);
3672 }
3673 }
3674 nfsm_reqdone;
3675 if (npp && *npp == NULL) {
3676 if (error) {
3677 if (newvp) {
3678 if (newvp == dvp)
3679 vnode_rele(newvp);
3680 else
3681 vnode_put(newvp);
3682 }
3683 } else
3684 *npp = np;
3685 }
3686 return (error);
3687 }
3688
3689 /*
3690 * Nfs Version 3 commit rpc
3691 */
3692 int
3693 nfs_commit(vp, offset, count, cred, procp)
3694 vnode_t vp;
3695 u_quad_t offset;
3696 u_int32_t count;
3697 kauth_cred_t cred;
3698 proc_t procp;
3699 {
3700 caddr_t cp;
3701 u_long *tl;
3702 int t1, t2;
3703 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
3704 caddr_t bpos, dpos, cp2;
3705 int error = 0, wccpostattr = 0;
3706 struct timespec premtime = { 0, 0 };
3707 mbuf_t mreq, mrep, md, mb, mb2;
3708 u_int64_t xid;
3709
3710 FSDBG(521, vp, offset, count, nmp->nm_state);
3711 if (!nmp)
3712 return (ENXIO);
3713 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3714 return (0);
3715 nfsm_reqhead(NFSX_FH(1));
3716 if (error)
3717 return (error);
3718 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_COMMIT]);
3719 nfsm_fhtom(vp, 1);
3720 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
3721 txdr_hyper(&offset, tl);
3722 tl += 2;
3723 *tl = txdr_unsigned(count);
3724 nfsm_request(vp, NFSPROC_COMMIT, procp, cred, &xid);
3725 if (mrep) {
3726 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
3727 /* XXX can we do anything useful with the wcc info? */
3728 }
3729 if (!error) {
3730 nfsm_dissect(tl, u_long *, NFSX_V3WRITEVERF);
3731 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3732 NFSX_V3WRITEVERF)) {
3733 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3734 NFSX_V3WRITEVERF);
3735 error = NFSERR_STALEWRITEVERF;
3736 }
3737 }
3738 nfsm_reqdone;
3739 return (error);
3740 }
3741
3742 static int
3743 nfs_blockmap(
3744 __unused struct vnop_blockmap_args /* {
3745 struct vnodeop_desc *a_desc;
3746 vnode_t a_vp;
3747 off_t a_foffset;
3748 size_t a_size;
3749 daddr64_t *a_bpn;
3750 size_t *a_run;
3751 void *a_poff;
3752 int a_flags;
3753 } */ *ap)
3754 {
3755 return (ENOTSUP);
3756 }
3757
3758 /*
3759 * Mmap a file
3760 *
3761 * NB Currently unsupported.
3762 */
3763 /*ARGSUSED*/
3764 static int
3765 nfs_mmap(
3766 __unused struct vnop_mmap_args /* {
3767 struct vnodeop_desc *a_desc;
3768 vnode_t a_vp;
3769 int a_fflags;
3770 kauth_cred_t a_cred;
3771 proc_t a_p;
3772 } */ *ap)
3773 {
3774
3775 return (EINVAL);
3776 }
3777
3778 /*
3779 * fsync vnode op. Just call nfs_flush() with commit == 1.
3780 */
3781 /* ARGSUSED */
3782 static int
3783 nfs_fsync(ap)
3784 struct vnop_fsync_args /* {
3785 struct vnodeop_desc *a_desc;
3786 vnode_t a_vp;
3787 int a_waitfor;
3788 vfs_context_t a_context;
3789 } */ *ap;
3790 {
3791 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
3792 proc_t p = vfs_context_proc(ap->a_context);
3793 struct nfsnode *np = VTONFS(ap->a_vp);
3794 int error;
3795
3796 np->n_flag |= NWRBUSY;
3797 error = nfs_flush(ap->a_vp, ap->a_waitfor, cred, p, 0);
3798 np->n_flag &= ~NWRBUSY;
3799 return (error);
3800 }
3801
3802 int
3803 nfs_flushcommits(vnode_t vp, proc_t p, int nowait)
3804 {
3805 struct nfsnode *np = VTONFS(vp);
3806 struct nfsbuf *bp;
3807 struct nfsbuflists blist, commitlist;
3808 int error = 0, retv, wcred_set, flags;
3809 u_quad_t off, endoff, toff;
3810 u_int32_t count;
3811 kauth_cred_t wcred = NULL;
3812
3813 FSDBG_TOP(557, vp, np, 0, 0);
3814
3815 /*
3816 * A nb_flags == (NB_DELWRI | NB_NEEDCOMMIT) block has been written to the
3817 * server, but nas not been committed to stable storage on the server
3818 * yet. The byte range is worked out for as many nfsbufs as we can handle
3819 * and the commit rpc is done.
3820 */
3821 if (!LIST_EMPTY(&np->n_dirtyblkhd))
3822 np->n_flag |= NMODIFIED;
3823
3824 off = (u_quad_t)-1;
3825 endoff = 0;
3826 wcred_set = 0;
3827 LIST_INIT(&commitlist);
3828
3829 if (!VFSTONFS(vnode_mount(vp))) {
3830 error = ENXIO;
3831 goto done;
3832 }
3833 if (!NFS_ISV3(vp)) {
3834 error = EINVAL;
3835 goto done;
3836 }
3837
3838 flags = NBI_DIRTY;
3839 if (nowait)
3840 flags |= NBI_NOWAIT;
3841 lck_mtx_lock(nfs_buf_mutex);
3842 if (!nfs_buf_iterprepare(np, &blist, flags)) {
3843 while ((bp = LIST_FIRST(&blist))) {
3844 LIST_REMOVE(bp, nb_vnbufs);
3845 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
3846 error = nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0);
3847 if (error)
3848 continue;
3849 if (((bp->nb_flags & (NB_DELWRI | NB_NEEDCOMMIT))
3850 != (NB_DELWRI | NB_NEEDCOMMIT))) {
3851 nfs_buf_drop(bp);
3852 continue;
3853 }
3854 nfs_buf_remfree(bp);
3855 lck_mtx_unlock(nfs_buf_mutex);
3856 /*
3857 * we need a upl to see if the page has been
3858 * dirtied (think mmap) since the unstable write, and
3859 * also to prevent vm from paging it during our commit rpc
3860 */
3861 if (!ISSET(bp->nb_flags, NB_PAGELIST)) {
3862 retv = nfs_buf_upl_setup(bp);
3863 if (retv) {
3864 /* unable to create upl */
3865 /* vm object must no longer exist */
3866 /* this could be fatal if we need */
3867 /* to write the data again, we'll see... */
3868 printf("nfs_flushcommits: upl create failed %d\n", retv);
3869 bp->nb_valid = bp->nb_dirty = 0;
3870 }
3871 }
3872 nfs_buf_upl_check(bp);
3873 lck_mtx_lock(nfs_buf_mutex);
3874
3875 FSDBG(557, bp, bp->nb_flags, bp->nb_valid, bp->nb_dirty);
3876 FSDBG(557, bp->nb_validoff, bp->nb_validend,
3877 bp->nb_dirtyoff, bp->nb_dirtyend);
3878
3879 /*
3880 * We used to check for dirty pages here; if there were any
3881 * we'd abort the commit and force the entire buffer to be
3882 * written again.
3883 *
3884 * Instead of doing that, we now go ahead and commit the dirty
3885 * range, and then leave the buffer around with dirty pages
3886 * that will be written out later.
3887 */
3888
3889 /*
3890 * Work out if all buffers are using the same cred
3891 * so we can deal with them all with one commit.
3892 *
3893 * XXX creds in bp's must be obtained by kauth_cred_ref on
3894 * the same original cred in order for them to be equal.
3895 */
3896 if (wcred_set == 0) {
3897 wcred = bp->nb_wcred;
3898 if (wcred == NOCRED)
3899 panic("nfs: needcommit w/out wcred");
3900 wcred_set = 1;
3901 } else if ((wcred_set == 1) && wcred != bp->nb_wcred) {
3902 wcred_set = -1;
3903 }
3904 SET(bp->nb_flags, NB_WRITEINPROG);
3905
3906 /*
3907 * A list of these buffers is kept so that the
3908 * second loop knows which buffers have actually
3909 * been committed. This is necessary, since there
3910 * may be a race between the commit rpc and new
3911 * uncommitted writes on the file.
3912 */
3913 LIST_REMOVE(bp, nb_vnbufs);
3914 LIST_INSERT_HEAD(&commitlist, bp, nb_vnbufs);
3915 toff = NBOFF(bp) + bp->nb_dirtyoff;
3916 if (toff < off)
3917 off = toff;
3918 toff += (u_quad_t)(bp->nb_dirtyend - bp->nb_dirtyoff);
3919 if (toff > endoff)
3920 endoff = toff;
3921 }
3922 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
3923 }
3924 lck_mtx_unlock(nfs_buf_mutex);
3925
3926 if (LIST_EMPTY(&commitlist)) {
3927 error = ENOBUFS;
3928 goto done;
3929 }
3930
3931 /*
3932 * Commit data on the server, as required.
3933 * If all bufs are using the same wcred, then use that with
3934 * one call for all of them, otherwise commit each one
3935 * separately.
3936 */
3937 if (wcred_set == 1) {
3938 /*
3939 * Note, it's possible the commit range could be >2^32-1.
3940 * If it is, we'll send one commit that covers the whole file.
3941 */
3942 if ((endoff - off) > 0xffffffff)
3943 count = 0;
3944 else
3945 count = (endoff - off);
3946 retv = nfs_commit(vp, off, count, wcred, p);
3947 } else {
3948 retv = 0;
3949 LIST_FOREACH(bp, &commitlist, nb_vnbufs) {
3950 toff = NBOFF(bp) + bp->nb_dirtyoff;
3951 count = bp->nb_dirtyend - bp->nb_dirtyoff;
3952 retv = nfs_commit(vp, toff, count, bp->nb_wcred, p);
3953 if (retv)
3954 break;
3955 }
3956 }
3957 if (retv == NFSERR_STALEWRITEVERF)
3958 nfs_clearcommit(vnode_mount(vp));
3959
3960 /*
3961 * Now, either mark the blocks I/O done or mark the
3962 * blocks dirty, depending on whether the commit
3963 * succeeded.
3964 */
3965 while ((bp = LIST_FIRST(&commitlist))) {
3966 LIST_REMOVE(bp, nb_vnbufs);
3967 FSDBG(557, bp, retv, bp->nb_flags, bp->nb_dirty);
3968 CLR(bp->nb_flags, (NB_NEEDCOMMIT | NB_WRITEINPROG));
3969 np->n_needcommitcnt--;
3970 CHECK_NEEDCOMMITCNT(np);
3971
3972 if (retv) {
3973 /* move back to dirty list */
3974 lck_mtx_lock(nfs_buf_mutex);
3975 LIST_INSERT_HEAD(&VTONFS(vp)->n_dirtyblkhd, bp, nb_vnbufs);
3976 lck_mtx_unlock(nfs_buf_mutex);
3977 nfs_buf_release(bp, 1);
3978 continue;
3979 }
3980
3981 vnode_startwrite(vp);
3982 if (ISSET(bp->nb_flags, NB_DELWRI)) {
3983 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
3984 NFSBUFCNTCHK(0);
3985 wakeup(&nfs_nbdwrite);
3986 }
3987 CLR(bp->nb_flags, (NB_READ|NB_DONE|NB_ERROR|NB_DELWRI));
3988 /* if block still has dirty pages, we don't want it to */
3989 /* be released in nfs_buf_iodone(). So, don't set NB_ASYNC. */
3990 if (!bp->nb_dirty)
3991 SET(bp->nb_flags, NB_ASYNC);
3992
3993 /* move to clean list */
3994 lck_mtx_lock(nfs_buf_mutex);
3995 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
3996 lck_mtx_unlock(nfs_buf_mutex);
3997
3998 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
3999
4000 nfs_buf_iodone(bp);
4001 if (bp->nb_dirty) {
4002 /* throw it back in as a delayed write buffer */
4003 CLR(bp->nb_flags, NB_DONE);
4004 nfs_buf_write_delayed(bp, p);
4005 }
4006 }
4007
4008 done:
4009 FSDBG_BOT(557, vp, np, 0, error);
4010 return (error);
4011 }
4012
4013 /*
4014 * Flush all the blocks associated with a vnode.
4015 * Walk through the buffer pool and push any dirty pages
4016 * associated with the vnode.
4017 */
4018 int
4019 nfs_flush(
4020 vnode_t vp,
4021 int waitfor,
4022 __unused kauth_cred_t cred,
4023 proc_t p,
4024 int ignore_writeerr)
4025 {
4026 struct nfsnode *np = VTONFS(vp);
4027 struct nfsbuf *bp;
4028 struct nfsbuflists blist;
4029 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4030 int error = 0, error2, slptimeo = 0, slpflag = 0;
4031 int flags, passone = 1;
4032
4033 FSDBG_TOP(517, vp, np, waitfor, 0);
4034
4035 if (!nmp) {
4036 error = ENXIO;
4037 goto done;
4038 }
4039 if (nmp->nm_flag & NFSMNT_INT)
4040 slpflag = PCATCH;
4041
4042 /*
4043 * On the first pass, start async/unstable writes on all
4044 * delayed write buffers. Then wait for all writes to complete
4045 * and call nfs_flushcommits() to commit any uncommitted buffers.
4046 * On all subsequent passes, start STABLE writes on any remaining
4047 * dirty buffers. Then wait for all writes to complete.
4048 */
4049 again:
4050 lck_mtx_lock(nfs_buf_mutex);
4051 FSDBG(518, LIST_FIRST(&np->n_dirtyblkhd), np->n_flag, 0, 0);
4052 if (!LIST_EMPTY(&np->n_dirtyblkhd))
4053 np->n_flag |= NMODIFIED;
4054 if (!VFSTONFS(vnode_mount(vp))) {
4055 lck_mtx_unlock(nfs_buf_mutex);
4056 error = ENXIO;
4057 goto done;
4058 }
4059
4060 /* Start/do any write(s) that are required. */
4061 if (!nfs_buf_iterprepare(np, &blist, NBI_DIRTY)) {
4062 while ((bp = LIST_FIRST(&blist))) {
4063 LIST_REMOVE(bp, nb_vnbufs);
4064 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
4065 flags = (passone || (waitfor != MNT_WAIT)) ? NBAC_NOWAIT : 0;
4066 if (flags != NBAC_NOWAIT)
4067 nfs_buf_refget(bp);
4068 while ((error = nfs_buf_acquire(bp, flags, slpflag, slptimeo))) {
4069 FSDBG(524, bp, flags, bp->nb_lflags, bp->nb_flags);
4070 if (error == EBUSY)
4071 break;
4072 if (error) {
4073 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4074 if (error2) {
4075 if (flags != NBAC_NOWAIT)
4076 nfs_buf_refrele(bp);
4077 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
4078 lck_mtx_unlock(nfs_buf_mutex);
4079 error = error2;
4080 goto done;
4081 }
4082 if (slpflag == PCATCH) {
4083 slpflag = 0;
4084 slptimeo = 2 * hz;
4085 }
4086 }
4087 }
4088 if (flags != NBAC_NOWAIT)
4089 nfs_buf_refrele(bp);
4090 if (error == EBUSY)
4091 continue;
4092 if (!bp->nb_vp) {
4093 /* buffer is no longer valid */
4094 nfs_buf_drop(bp);
4095 continue;
4096 }
4097 if (!ISSET(bp->nb_flags, NB_DELWRI))
4098 panic("nfs_flush: not dirty");
4099 FSDBG(525, bp, passone, bp->nb_lflags, bp->nb_flags);
4100 if ((passone || (waitfor != MNT_WAIT)) &&
4101 ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
4102 nfs_buf_drop(bp);
4103 continue;
4104 }
4105 nfs_buf_remfree(bp);
4106 lck_mtx_unlock(nfs_buf_mutex);
4107 if (ISSET(bp->nb_flags, NB_ERROR)) {
4108 np->n_error = bp->nb_error ? bp->nb_error : EIO;
4109 np->n_flag |= NWRITEERR;
4110 nfs_buf_release(bp, 1);
4111 lck_mtx_lock(nfs_buf_mutex);
4112 continue;
4113 }
4114 SET(bp->nb_flags, NB_ASYNC);
4115 if (!passone) {
4116 /* NB_STABLE forces this to be written FILESYNC */
4117 SET(bp->nb_flags, NB_STABLE);
4118 }
4119 nfs_buf_write(bp);
4120 lck_mtx_lock(nfs_buf_mutex);
4121 }
4122 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
4123 }
4124 lck_mtx_unlock(nfs_buf_mutex);
4125
4126 if (waitfor == MNT_WAIT) {
4127 while ((error = vnode_waitforwrites(vp, 0, slpflag, slptimeo, "nfsflush"))) {
4128 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4129 if (error2) {
4130 error = error2;
4131 goto done;
4132 }
4133 if (slpflag == PCATCH) {
4134 slpflag = 0;
4135 slptimeo = 2 * hz;
4136 }
4137 }
4138 }
4139
4140 if (NFS_ISV3(vp)) {
4141 /* loop while it looks like there are still buffers to be */
4142 /* commited and nfs_flushcommits() seems to be handling them. */
4143 while (np->n_needcommitcnt)
4144 if (nfs_flushcommits(vp, p, 0))
4145 break;
4146 }
4147
4148 if (passone) {
4149 passone = 0;
4150 goto again;
4151 }
4152
4153 if (waitfor == MNT_WAIT) {
4154 if (!LIST_EMPTY(&np->n_dirtyblkhd))
4155 goto again;
4156 /* if we have no dirty blocks, we can clear the modified flag */
4157 np->n_flag &= ~NMODIFIED;
4158 }
4159
4160 FSDBG(526, np->n_flag, np->n_error, 0, 0);
4161 if (!ignore_writeerr && (np->n_flag & NWRITEERR)) {
4162 error = np->n_error;
4163 np->n_flag &= ~NWRITEERR;
4164 }
4165 done:
4166 FSDBG_BOT(517, vp, np, error, 0);
4167 return (error);
4168 }
4169
4170 /*
4171 * Do an nfs pathconf rpc.
4172 */
4173 int
4174 nfs_pathconfrpc(
4175 vnode_t vp,
4176 struct nfsv3_pathconf *pc,
4177 kauth_cred_t cred,
4178 proc_t procp)
4179 {
4180 mbuf_t mreq, mrep, md, mb, mb2;
4181 caddr_t bpos, dpos, cp, cp2;
4182 int32_t t1, t2;
4183 u_long *tl;
4184 u_int64_t xid;
4185 int attrflag, error = 0;
4186 struct nfsv3_pathconf *mpc;
4187
4188 /* fetch pathconf info from server */
4189 nfsm_reqhead(NFSX_FH(1));
4190 if (error)
4191 return (error);
4192 nfsm_fhtom(vp, 1);
4193 nfsm_request(vp, NFSPROC_PATHCONF, procp, cred, &xid);
4194 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
4195 if (!error) {
4196 nfsm_dissect(mpc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
4197 pc->pc_linkmax = fxdr_unsigned(long, mpc->pc_linkmax);
4198 pc->pc_namemax = fxdr_unsigned(long, mpc->pc_namemax);
4199 pc->pc_chownrestricted = fxdr_unsigned(long, mpc->pc_chownrestricted);
4200 pc->pc_notrunc = fxdr_unsigned(long, mpc->pc_notrunc);
4201 pc->pc_caseinsensitive = fxdr_unsigned(long, mpc->pc_caseinsensitive);
4202 pc->pc_casepreserving = fxdr_unsigned(long, mpc->pc_casepreserving);
4203 }
4204 nfsm_reqdone;
4205
4206 return (error);
4207 }
4208
4209 void
4210 nfs_pathconf_cache(struct nfsmount *nmp, struct nfsv3_pathconf *pc)
4211 {
4212 nmp->nm_state |= NFSSTA_GOTPATHCONF;
4213 nmp->nm_fsinfo.linkmax = pc->pc_linkmax;
4214 nmp->nm_fsinfo.namemax = pc->pc_namemax;
4215 nmp->nm_fsinfo.pcflags = 0;
4216 if (pc->pc_notrunc)
4217 nmp->nm_fsinfo.pcflags |= NFSPCINFO_NOTRUNC;
4218 if (pc->pc_chownrestricted)
4219 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CHOWN_RESTRICTED;
4220 if (pc->pc_caseinsensitive)
4221 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CASE_INSENSITIVE;
4222 if (pc->pc_casepreserving)
4223 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CASE_PRESERVING;
4224 }
4225
4226 /*
4227 * Return POSIX pathconf information applicable to nfs.
4228 *
4229 * The NFS V2 protocol doesn't support this, so just return EINVAL
4230 * for V2.
4231 */
4232 /* ARGSUSED */
4233 static int
4234 nfs_pathconf(ap)
4235 struct vnop_pathconf_args /* {
4236 struct vnodeop_desc *a_desc;
4237 vnode_t a_vp;
4238 int a_name;
4239 register_t *a_retval;
4240 vfs_context_t a_context;
4241 } */ *ap;
4242 {
4243 vnode_t vp = ap->a_vp;
4244 struct nfsmount *nmp;
4245 struct nfsv3_pathconf pc;
4246 int error = 0, cached;
4247
4248 nmp = VFSTONFS(vnode_mount(vp));
4249 if (!nmp)
4250 return (ENXIO);
4251 if (!NFS_ISV3(vp))
4252 return (EINVAL);
4253
4254 switch (ap->a_name) {
4255 case _PC_LINK_MAX:
4256 case _PC_NAME_MAX:
4257 case _PC_CHOWN_RESTRICTED:
4258 case _PC_NO_TRUNC:
4259 case _PC_CASE_SENSITIVE:
4260 case _PC_CASE_PRESERVING:
4261 break;
4262 default:
4263 /* don't bother contacting the server if we know the answer */
4264 return (EINVAL);
4265 }
4266
4267 if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
4268 /* no pathconf info cached */
4269 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
4270 proc_t p = vfs_context_proc(ap->a_context);
4271 error = nfs_pathconfrpc(vp, &pc, cred, p);
4272 if (error)
4273 return (error);
4274 nmp = VFSTONFS(vnode_mount(vp));
4275 if (!nmp)
4276 return (ENXIO);
4277 if (!(nmp->nm_state & NFSSTA_GOTFSINFO)) {
4278 nfs_fsinfo(nmp, vp, cred, p);
4279 nmp = VFSTONFS(vnode_mount(vp));
4280 if (!nmp)
4281 return (ENXIO);
4282 }
4283 if ((nmp->nm_state & NFSSTA_GOTFSINFO) &&
4284 (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS)) {
4285 /* all files have the same pathconf info, */
4286 /* so cache a copy of the results */
4287 nfs_pathconf_cache(nmp, &pc);
4288 }
4289 }
4290
4291 cached = (nmp->nm_state & NFSSTA_GOTPATHCONF);
4292
4293 switch (ap->a_name) {
4294 case _PC_LINK_MAX:
4295 *ap->a_retval = cached ? nmp->nm_fsinfo.linkmax : pc.pc_linkmax;
4296 break;
4297 case _PC_NAME_MAX:
4298 *ap->a_retval = cached ? nmp->nm_fsinfo.namemax : pc.pc_namemax;
4299 break;
4300 case _PC_CHOWN_RESTRICTED:
4301 if (cached)
4302 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CHOWN_RESTRICTED) ? 1 : 0;
4303 else
4304 *ap->a_retval = pc.pc_chownrestricted;
4305 break;
4306 case _PC_NO_TRUNC:
4307 if (cached)
4308 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_NOTRUNC) ? 1 : 0;
4309 else
4310 *ap->a_retval = pc.pc_notrunc;
4311 break;
4312 case _PC_CASE_SENSITIVE:
4313 if (cached)
4314 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_INSENSITIVE) ? 0 : 1;
4315 else
4316 *ap->a_retval = !pc.pc_caseinsensitive;
4317 break;
4318 case _PC_CASE_PRESERVING:
4319 if (cached)
4320 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_PRESERVING) ? 1 : 0;
4321 else
4322 *ap->a_retval = pc.pc_casepreserving;
4323 break;
4324 default:
4325 error = EINVAL;
4326 }
4327
4328 return (error);
4329 }
4330
4331 /*
4332 * NFS advisory byte-level locks (client)
4333 */
4334 static int
4335 nfs_advlock(ap)
4336 struct vnop_advlock_args /* {
4337 struct vnodeop_desc *a_desc;
4338 vnode_t a_vp;
4339 caddr_t a_id;
4340 int a_op;
4341 struct flock *a_fl;
4342 int a_flags;
4343 vfs_context_t a_context;
4344 } */ *ap;
4345 {
4346 return (nfs_dolock(ap));
4347 }
4348
4349 /*
4350 * write (or commit) the given NFS buffer
4351 */
4352 int
4353 nfs_buf_write(struct nfsbuf *bp)
4354 {
4355 int oldflags = bp->nb_flags, rv = 0;
4356 vnode_t vp = bp->nb_vp;
4357 struct nfsnode *np = VTONFS(vp);
4358 kauth_cred_t cr;
4359 proc_t p = current_proc(); // XXX
4360
4361 FSDBG_TOP(553, bp, NBOFF(bp), bp->nb_flags, 0);
4362
4363 if (!ISSET(bp->nb_lflags, NBL_BUSY))
4364 panic("nfs_buf_write: buffer is not busy???");
4365
4366 CLR(bp->nb_flags, (NB_READ|NB_DONE|NB_ERROR|NB_DELWRI));
4367 if (ISSET(oldflags, NB_DELWRI)) {
4368 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
4369 NFSBUFCNTCHK(0);
4370 wakeup(&nfs_nbdwrite);
4371 }
4372
4373 /* move to clean list */
4374 if (ISSET(oldflags, (NB_ASYNC|NB_DELWRI))) {
4375 lck_mtx_lock(nfs_buf_mutex);
4376 if (bp->nb_vnbufs.le_next != NFSNOLIST)
4377 LIST_REMOVE(bp, nb_vnbufs);
4378 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
4379 lck_mtx_unlock(nfs_buf_mutex);
4380 }
4381 vnode_startwrite(vp);
4382
4383 if (p && p->p_stats)
4384 p->p_stats->p_ru.ru_oublock++;
4385
4386 /*
4387 * For async requests when nfsiod(s) are running, queue the request by
4388 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the request.
4389 */
4390 if (ISSET(bp->nb_flags, NB_ASYNC))
4391 p = NULL;
4392 if (ISSET(bp->nb_flags, NB_READ))
4393 cr = bp->nb_rcred;
4394 else
4395 cr = bp->nb_wcred;
4396 if (!ISSET(bp->nb_flags, NB_ASYNC) || nfs_asyncio(bp, NOCRED))
4397 rv = nfs_doio(bp, cr, p);
4398
4399 if ((oldflags & NB_ASYNC) == 0) {
4400 rv = nfs_buf_iowait(bp);
4401 /* move to clean list */
4402 if (oldflags & NB_DELWRI) {
4403 lck_mtx_lock(nfs_buf_mutex);
4404 if (bp->nb_vnbufs.le_next != NFSNOLIST)
4405 LIST_REMOVE(bp, nb_vnbufs);
4406 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
4407 lck_mtx_unlock(nfs_buf_mutex);
4408 }
4409 oldflags = bp->nb_flags;
4410 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
4411 if (cr) {
4412 kauth_cred_ref(cr);
4413 }
4414 nfs_buf_release(bp, 1);
4415 if (ISSET(oldflags, NB_ERROR) && !(np->n_flag & NFLUSHINPROG)) {
4416 /*
4417 * There was a write error and we need to
4418 * invalidate attrs and flush buffers in
4419 * order to sync up with the server.
4420 * (if this write was extending the file,
4421 * we may no longer know the correct size)
4422 *
4423 * But we couldn't call vinvalbuf while holding
4424 * the buffer busy. So we call vinvalbuf() after
4425 * releasing the buffer.
4426 */
4427 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cr, p, 1);
4428 }
4429 if (cr)
4430 kauth_cred_rele(cr);
4431 return (rv);
4432 }
4433
4434 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
4435 return (rv);
4436 }
4437
4438 /*
4439 * Read wrapper for special devices.
4440 */
4441 static int
4442 nfsspec_read(ap)
4443 struct vnop_read_args /* {
4444 struct vnodeop_desc *a_desc;
4445 vnode_t a_vp;
4446 struct uio *a_uio;
4447 int a_ioflag;
4448 vfs_context_t a_context;
4449 } */ *ap;
4450 {
4451 register struct nfsnode *np = VTONFS(ap->a_vp);
4452 struct timeval now;
4453
4454 /*
4455 * Set access flag.
4456 */
4457 np->n_flag |= NACC;
4458 microtime(&now);
4459 np->n_atim.tv_sec = now.tv_sec;
4460 np->n_atim.tv_nsec = now.tv_usec * 1000;
4461 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap));
4462 }
4463
4464 /*
4465 * Write wrapper for special devices.
4466 */
4467 static int
4468 nfsspec_write(ap)
4469 struct vnop_write_args /* {
4470 struct vnodeop_desc *a_desc;
4471 vnode_t a_vp;
4472 struct uio *a_uio;
4473 int a_ioflag;
4474 vfs_context_t a_context;
4475 } */ *ap;
4476 {
4477 register struct nfsnode *np = VTONFS(ap->a_vp);
4478 struct timeval now;
4479
4480 /*
4481 * Set update flag.
4482 */
4483 np->n_flag |= NUPD;
4484 microtime(&now);
4485 np->n_mtim.tv_sec = now.tv_sec;
4486 np->n_mtim.tv_nsec = now.tv_usec * 1000;
4487 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap));
4488 }
4489
4490 /*
4491 * Close wrapper for special devices.
4492 *
4493 * Update the times on the nfsnode then do device close.
4494 */
4495 static int
4496 nfsspec_close(ap)
4497 struct vnop_close_args /* {
4498 struct vnodeop_desc *a_desc;
4499 vnode_t a_vp;
4500 int a_fflag;
4501 vfs_context_t a_context;
4502 } */ *ap;
4503 {
4504 vnode_t vp = ap->a_vp;
4505 struct nfsnode *np = VTONFS(vp);
4506 struct vnode_attr vattr;
4507 mount_t mp;
4508
4509 if (np->n_flag & (NACC | NUPD)) {
4510 np->n_flag |= NCHG;
4511 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4512 VATTR_INIT(&vattr);
4513 if (np->n_flag & NACC) {
4514 vattr.va_access_time = np->n_atim;
4515 VATTR_SET_ACTIVE(&vattr, va_access_time);
4516 }
4517 if (np->n_flag & NUPD) {
4518 vattr.va_modify_time = np->n_mtim;
4519 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4520 }
4521 vnode_setattr(vp, &vattr, ap->a_context);
4522 }
4523 }
4524 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
4525 }
4526
4527 extern vnop_t **fifo_vnodeop_p;
4528
4529 /*
4530 * Read wrapper for fifos.
4531 */
4532 static int
4533 nfsfifo_read(ap)
4534 struct vnop_read_args /* {
4535 struct vnodeop_desc *a_desc;
4536 vnode_t a_vp;
4537 struct uio *a_uio;
4538 int a_ioflag;
4539 vfs_context_t a_context;
4540 } */ *ap;
4541 {
4542 register struct nfsnode *np = VTONFS(ap->a_vp);
4543 struct timeval now;
4544
4545 /*
4546 * Set access flag.
4547 */
4548 np->n_flag |= NACC;
4549 microtime(&now);
4550 np->n_atim.tv_sec = now.tv_sec;
4551 np->n_atim.tv_nsec = now.tv_usec * 1000;
4552 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap));
4553 }
4554
4555 /*
4556 * Write wrapper for fifos.
4557 */
4558 static int
4559 nfsfifo_write(ap)
4560 struct vnop_write_args /* {
4561 struct vnodeop_desc *a_desc;
4562 vnode_t a_vp;
4563 struct uio *a_uio;
4564 int a_ioflag;
4565 vfs_context_t a_context;
4566 } */ *ap;
4567 {
4568 register struct nfsnode *np = VTONFS(ap->a_vp);
4569 struct timeval now;
4570
4571 /*
4572 * Set update flag.
4573 */
4574 np->n_flag |= NUPD;
4575 microtime(&now);
4576 np->n_mtim.tv_sec = now.tv_sec;
4577 np->n_mtim.tv_nsec = now.tv_usec * 1000;
4578 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap));
4579 }
4580
4581 /*
4582 * Close wrapper for fifos.
4583 *
4584 * Update the times on the nfsnode then do fifo close.
4585 */
4586 static int
4587 nfsfifo_close(ap)
4588 struct vnop_close_args /* {
4589 struct vnodeop_desc *a_desc;
4590 vnode_t a_vp;
4591 int a_fflag;
4592 vfs_context_t a_context;
4593 } */ *ap;
4594 {
4595 vnode_t vp = ap->a_vp;
4596 struct nfsnode *np = VTONFS(vp);
4597 struct vnode_attr vattr;
4598 struct timeval now;
4599 mount_t mp;
4600
4601 if (np->n_flag & (NACC | NUPD)) {
4602 microtime(&now);
4603 if (np->n_flag & NACC) {
4604 np->n_atim.tv_sec = now.tv_sec;
4605 np->n_atim.tv_nsec = now.tv_usec * 1000;
4606 }
4607 if (np->n_flag & NUPD) {
4608 np->n_mtim.tv_sec = now.tv_sec;
4609 np->n_mtim.tv_nsec = now.tv_usec * 1000;
4610 }
4611 np->n_flag |= NCHG;
4612 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4613 VATTR_INIT(&vattr);
4614 if (np->n_flag & NACC) {
4615 vattr.va_access_time = np->n_atim;
4616 VATTR_SET_ACTIVE(&vattr, va_access_time);
4617 }
4618 if (np->n_flag & NUPD) {
4619 vattr.va_modify_time = np->n_mtim;
4620 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4621 }
4622 vnode_setattr(vp, &vattr, ap->a_context);
4623 }
4624 }
4625 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
4626 }
4627
4628 /*ARGSUSED*/
4629 static int
4630 nfs_ioctl(
4631 __unused struct vnop_ioctl_args /* {
4632 struct vnodeop_desc *a_desc;
4633 vnode_t a_vp;
4634 u_long a_command;
4635 caddr_t a_data;
4636 int a_fflag;
4637 kauth_cred_t a_cred;
4638 proc_t a_p;
4639 } */ *ap)
4640 {
4641
4642 /*
4643 * XXX we were once bogusly enoictl() which returned this (ENOTTY).
4644 * Probably we should return ENODEV.
4645 */
4646 return (ENOTTY);
4647 }
4648
4649 /*ARGSUSED*/
4650 static int
4651 nfs_select(
4652 __unused struct vnop_select_args /* {
4653 struct vnodeop_desc *a_desc;
4654 vnode_t a_vp;
4655 int a_which;
4656 int a_fflags;
4657 kauth_cred_t a_cred;
4658 void *a_wql;
4659 proc_t a_p;
4660 } */ *ap)
4661 {
4662
4663 /*
4664 * We were once bogusly seltrue() which returns 1. Is this right?
4665 */
4666 return (1);
4667 }
4668
4669 /*
4670 * Vnode op for pagein using getblk_pages
4671 * derived from nfs_bioread()
4672 * No read aheads are started from pagein operation
4673 */
4674 static int
4675 nfs_pagein(ap)
4676 struct vnop_pagein_args /* {
4677 struct vnodeop_desc *a_desc;
4678 vnode_t a_vp;
4679 upl_t a_pl;
4680 vm_offset_t a_pl_offset;
4681 off_t a_f_offset;
4682 size_t a_size;
4683 int a_flags;
4684 vfs_context_t a_context;
4685 } */ *ap;
4686 {
4687 vnode_t vp = ap->a_vp;
4688 upl_t pl = ap->a_pl;
4689 size_t size= ap->a_size;
4690 off_t f_offset = ap->a_f_offset;
4691 vm_offset_t pl_offset = ap->a_pl_offset;
4692 int flags = ap->a_flags;
4693 kauth_cred_t cred;
4694 proc_t p;
4695 struct nfsnode *np = VTONFS(vp);
4696 int biosize, xsize, iosize;
4697 struct nfsmount *nmp;
4698 int error = 0;
4699 vm_offset_t ioaddr;
4700 struct uio auio;
4701 struct iovec_32 aiov;
4702 struct uio * uio = &auio;
4703 int nofreeupl = flags & UPL_NOCOMMIT;
4704 upl_page_info_t *plinfo;
4705
4706 FSDBG(322, vp, f_offset, size, flags);
4707 if (pl == (upl_t)NULL)
4708 panic("nfs_pagein: no upl");
4709
4710 if (UBCINVALID(vp)) {
4711 printf("nfs_pagein: invalid vnode 0x%x", (int)vp);
4712 if (!nofreeupl)
4713 (void) ubc_upl_abort(pl, 0);
4714 return (EPERM);
4715 }
4716 UBCINFOCHECK("nfs_pagein", vp);
4717
4718 if (size <= 0) {
4719 printf("nfs_pagein: invalid size %d", size);
4720 if (!nofreeupl)
4721 (void) ubc_upl_abort(pl, 0);
4722 return (EINVAL);
4723 }
4724 if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
4725 if (!nofreeupl)
4726 ubc_upl_abort_range(pl, pl_offset, size,
4727 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4728 return (EINVAL);
4729 }
4730
4731 cred = ubc_getcred(vp);
4732 if (cred == NOCRED)
4733 cred = vfs_context_ucred(ap->a_context);
4734 p = vfs_context_proc(ap->a_context);
4735
4736 auio.uio_offset = f_offset;
4737 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
4738 auio.uio_segflg = UIO_SYSSPACE;
4739 #else
4740 auio.uio_segflg = UIO_SYSSPACE32;
4741 #endif
4742 auio.uio_rw = UIO_READ;
4743 auio.uio_procp = p;
4744
4745 nmp = VFSTONFS(vnode_mount(vp));
4746 if (!nmp) {
4747 if (!nofreeupl)
4748 ubc_upl_abort_range(pl, pl_offset, size,
4749 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4750 return (ENXIO);
4751 }
4752 biosize = nmp->nm_biosize;
4753 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
4754 nfs_fsinfo(nmp, vp, cred, p);
4755
4756 plinfo = ubc_upl_pageinfo(pl);
4757 ubc_upl_map(pl, &ioaddr);
4758 ioaddr += pl_offset;
4759 xsize = size;
4760
4761 do {
4762 /*
4763 * It would be nice to be able to issue all these requests
4764 * in parallel instead of waiting for each one to complete
4765 * before sending the next one.
4766 * XXX Should we align these requests to block boundaries?
4767 */
4768 iosize = min(biosize, xsize);
4769 aiov.iov_len = iosize;
4770 aiov.iov_base = (uintptr_t)ioaddr;
4771 auio.uio_iovs.iov32p = &aiov;
4772 auio.uio_iovcnt = 1;
4773 uio_uio_resid_set(&auio, iosize);
4774
4775 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), ioaddr, xsize);
4776 /*
4777 * With UBC we get here only when the file data is not in the VM
4778 * page cache, so go ahead and read in.
4779 */
4780 #ifdef UPL_DEBUG
4781 upl_ubc_alias_set(pl, current_thread(), 2);
4782 #endif /* UPL_DEBUG */
4783 OSAddAtomic(1, (SInt32*)&nfsstats.pageins);
4784
4785 error = nfs_readrpc(vp, uio, cred, p);
4786
4787 if (!error) {
4788 if (uio_uio_resid(uio)) {
4789 /*
4790 * If uio_resid > 0, there is a hole in the file
4791 * and no writes after the hole have been pushed
4792 * to the server yet... or we're at the EOF
4793 * Just zero fill the rest of the valid area.
4794 */
4795 // LP64todo - fix this
4796 int zcnt = uio_uio_resid(uio);
4797 int zoff = iosize - zcnt;
4798 bzero((char *)ioaddr + zoff, zcnt);
4799
4800 FSDBG(324, uio->uio_offset, zoff, zcnt, ioaddr);
4801 uio->uio_offset += zcnt;
4802 }
4803 ioaddr += iosize;
4804 xsize -= iosize;
4805 } else {
4806 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), error, -1);
4807 }
4808
4809 nmp = VFSTONFS(vnode_mount(vp));
4810 } while (error == 0 && xsize > 0);
4811
4812 ubc_upl_unmap(pl);
4813
4814 if (!nofreeupl) {
4815 if (error)
4816 ubc_upl_abort_range(pl, pl_offset, size,
4817 UPL_ABORT_ERROR |
4818 UPL_ABORT_FREE_ON_EMPTY);
4819 else
4820 ubc_upl_commit_range(pl, pl_offset, size,
4821 UPL_COMMIT_CLEAR_DIRTY |
4822 UPL_COMMIT_FREE_ON_EMPTY);
4823 }
4824 return (error);
4825 }
4826
4827
4828 /*
4829 * Vnode op for pageout using UPL
4830 * Derived from nfs_write()
4831 * File size changes are not permitted in pageout.
4832 */
4833 static int
4834 nfs_pageout(ap)
4835 struct vnop_pageout_args /* {
4836 struct vnodeop_desc *a_desc;
4837 vnode_t a_vp;
4838 upl_t a_pl;
4839 vm_offset_t a_pl_offset;
4840 off_t a_f_offset;
4841 size_t a_size;
4842 int a_flags;
4843 vfs_context_t a_context;
4844 } */ *ap;
4845 {
4846 vnode_t vp = ap->a_vp;
4847 upl_t pl = ap->a_pl;
4848 size_t size= ap->a_size;
4849 off_t f_offset = ap->a_f_offset;
4850 vm_offset_t pl_offset = ap->a_pl_offset;
4851 int flags = ap->a_flags;
4852 struct nfsnode *np = VTONFS(vp);
4853 kauth_cred_t cred;
4854 proc_t p;
4855 struct nfsbuf *bp;
4856 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4857 daddr64_t lbn;
4858 int error = 0, iomode, must_commit;
4859 off_t off;
4860 vm_offset_t ioaddr;
4861 struct uio auio;
4862 struct iovec_32 aiov;
4863 int nofreeupl = flags & UPL_NOCOMMIT;
4864 size_t biosize, iosize, pgsize, xsize;
4865
4866 FSDBG(323, f_offset, size, pl, pl_offset);
4867
4868 if (pl == (upl_t)NULL)
4869 panic("nfs_pageout: no upl");
4870
4871 if (UBCINVALID(vp)) {
4872 printf("nfs_pageout: invalid vnode 0x%x", (int)vp);
4873 if (!nofreeupl)
4874 ubc_upl_abort(pl, 0);
4875 return (EIO);
4876 }
4877 UBCINFOCHECK("nfs_pageout", vp);
4878
4879 if (size <= 0) {
4880 printf("nfs_pageout: invalid size %d", size);
4881 if (!nofreeupl)
4882 ubc_upl_abort(pl, 0);
4883 return (EINVAL);
4884 }
4885
4886 if (!nmp) {
4887 if (!nofreeupl)
4888 ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
4889 return (ENXIO);
4890 }
4891 biosize = nmp->nm_biosize;
4892
4893 /*
4894 * Check to see whether the buffer is incore.
4895 * If incore and not busy, invalidate it from the cache.
4896 */
4897 for (iosize = 0; iosize < size; iosize += xsize) {
4898 off = f_offset + iosize;
4899 /* need make sure we do things on block boundaries */
4900 xsize = biosize - (off % biosize);
4901 if (off + xsize > f_offset + size)
4902 xsize = f_offset + size - off;
4903 lbn = ubc_offtoblk(vp, off);
4904 lck_mtx_lock(nfs_buf_mutex);
4905 if ((bp = nfs_buf_incore(vp, lbn))) {
4906 FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags);
4907 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
4908 lck_mtx_unlock(nfs_buf_mutex);
4909 /* no panic. just tell vm we are busy */
4910 if (!nofreeupl)
4911 ubc_upl_abort(pl, 0);
4912 return (EBUSY);
4913 }
4914 if (bp->nb_dirtyend > 0) {
4915 /*
4916 * if there's a dirty range in the buffer, check
4917 * to see if it extends beyond the pageout region
4918 *
4919 * if the dirty region lies completely within the
4920 * pageout region, we just invalidate the buffer
4921 * because it's all being written out now anyway.
4922 *
4923 * if any of the dirty region lies outside the
4924 * pageout region, we'll try to clip the dirty
4925 * region to eliminate the portion that's being
4926 * paged out. If that's not possible, because
4927 * the dirty region extends before and after the
4928 * pageout region, then we'll just return EBUSY.
4929 */
4930 off_t boff, start, end;
4931 boff = NBOFF(bp);
4932 start = off;
4933 end = off + xsize;
4934 /* clip end to EOF */
4935 if (end > (off_t)np->n_size)
4936 end = np->n_size;
4937 start -= boff;
4938 end -= boff;
4939 if ((bp->nb_dirtyoff < start) &&
4940 (bp->nb_dirtyend > end)) {
4941 /* not gonna be able to clip the dirty region */
4942 FSDBG(323, vp, bp, 0xd00deebc, EBUSY);
4943 nfs_buf_drop(bp);
4944 lck_mtx_unlock(nfs_buf_mutex);
4945 if (!nofreeupl)
4946 ubc_upl_abort(pl, 0);
4947 return (EBUSY);
4948 }
4949 if ((bp->nb_dirtyoff < start) ||
4950 (bp->nb_dirtyend > end)) {
4951 /* clip dirty region, if necessary */
4952 if (bp->nb_dirtyoff < start)
4953 bp->nb_dirtyend = min(bp->nb_dirtyend, start);
4954 if (bp->nb_dirtyend > end)
4955 bp->nb_dirtyoff = max(bp->nb_dirtyoff, end);
4956 FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00);
4957 /* we're leaving this block dirty */
4958 nfs_buf_drop(bp);
4959 lck_mtx_unlock(nfs_buf_mutex);
4960 continue;
4961 }
4962 }
4963 nfs_buf_remfree(bp);
4964 lck_mtx_unlock(nfs_buf_mutex);
4965 SET(bp->nb_flags, NB_INVAL);
4966 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
4967 CLR(bp->nb_flags, NB_NEEDCOMMIT);
4968 np->n_needcommitcnt--;
4969 CHECK_NEEDCOMMITCNT(np);
4970 }
4971 nfs_buf_release(bp, 1);
4972 } else {
4973 lck_mtx_unlock(nfs_buf_mutex);
4974 }
4975 }
4976
4977 cred = ubc_getcred(vp);
4978 if (cred == NOCRED)
4979 cred = vfs_context_ucred(ap->a_context);
4980 p = vfs_context_proc(ap->a_context);
4981
4982 if (np->n_flag & NWRITEERR) {
4983 np->n_flag &= ~NWRITEERR;
4984 if (!nofreeupl)
4985 ubc_upl_abort_range(pl, pl_offset, size,
4986 UPL_ABORT_FREE_ON_EMPTY);
4987 return (np->n_error);
4988 }
4989 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
4990 nfs_fsinfo(nmp, vp, cred, p);
4991
4992 if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
4993 f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
4994 if (!nofreeupl)
4995 ubc_upl_abort_range(pl, pl_offset, size,
4996 UPL_ABORT_FREE_ON_EMPTY);
4997 return (EINVAL);
4998 }
4999
5000 ubc_upl_map(pl, &ioaddr);
5001 ioaddr += pl_offset;
5002
5003 if ((u_quad_t)f_offset + size > np->n_size)
5004 xsize = np->n_size - f_offset;
5005 else
5006 xsize = size;
5007
5008 pgsize = round_page_64(xsize);
5009 if (size > pgsize) {
5010 if (!nofreeupl)
5011 ubc_upl_abort_range(pl, pl_offset + pgsize,
5012 size - pgsize,
5013 UPL_ABORT_FREE_ON_EMPTY);
5014 }
5015
5016 /*
5017 * check for partial page and clear the
5018 * contents past end of the file before
5019 * releasing it in the VM page cache
5020 */
5021 if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) {
5022 size_t io = np->n_size - f_offset;
5023 bzero((caddr_t)(ioaddr + io), size - io);
5024 FSDBG(321, np->n_size, f_offset, f_offset + io, size - io);
5025 }
5026
5027 auio.uio_offset = f_offset;
5028 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
5029 auio.uio_segflg = UIO_SYSSPACE;
5030 #else
5031 auio.uio_segflg = UIO_SYSSPACE32;
5032 #endif
5033 auio.uio_rw = UIO_READ;
5034 auio.uio_procp = p;
5035
5036 do {
5037 /*
5038 * It would be nice to be able to issue all these requests
5039 * in parallel instead of waiting for each one to complete
5040 * before sending the next one.
5041 * XXX Should we align these requests to block boundaries?
5042 */
5043 iosize = min(biosize, xsize);
5044 uio_uio_resid_set(&auio, iosize);
5045 aiov.iov_len = iosize;
5046 aiov.iov_base = (uintptr_t)ioaddr;
5047 auio.uio_iovs.iov32p = &aiov;
5048 auio.uio_iovcnt = 1;
5049
5050 FSDBG(323, auio.uio_offset, uio_uio_resid(&auio), ioaddr, xsize);
5051 OSAddAtomic(1, (SInt32*)&nfsstats.pageouts);
5052
5053 vnode_startwrite(vp);
5054
5055 /* NMODIFIED would be set here if doing unstable writes */
5056 iomode = NFSV3WRITE_FILESYNC;
5057 error = nfs_writerpc(vp, &auio, cred, p, &iomode, &must_commit);
5058 if (must_commit)
5059 nfs_clearcommit(vnode_mount(vp));
5060 vnode_writedone(vp);
5061 if (error)
5062 goto cleanup;
5063 /* Note: no need to check uio_resid, because */
5064 /* it'll only be set if there was an error. */
5065 ioaddr += iosize;
5066 xsize -= iosize;
5067 } while (xsize > 0);
5068
5069 cleanup:
5070 ubc_upl_unmap(pl);
5071 /*
5072 * We've had several different solutions on what to do when the pageout
5073 * gets an error. If we don't handle it, and return an error to the
5074 * caller, vm, it will retry . This can end in endless looping
5075 * between vm and here doing retries of the same page. Doing a dump
5076 * back to vm, will get it out of vm's knowledge and we lose whatever
5077 * data existed. This is risky, but in some cases necessary. For
5078 * example, the initial fix here was to do that for ESTALE. In that case
5079 * the server is telling us that the file is no longer the same. We
5080 * would not want to keep paging out to that. We also saw some 151
5081 * errors from Auspex server and NFSv3 can return errors higher than
5082 * ELAST. Those along with NFS known server errors we will "dump" from
5083 * vm. Errors we don't expect to occur, we dump and log for further
5084 * analysis. Errors that could be transient, networking ones,
5085 * we let vm "retry". Lastly, errors that we retry, but may have potential
5086 * to storm the network, we "retrywithsleep". "sever" will be used in
5087 * in the future to dump all pages of object for cases like ESTALE.
5088 * All this is the basis for the states returned and first guesses on
5089 * error handling. Tweaking expected as more statistics are gathered.
5090 * Note, in the long run we may need another more robust solution to
5091 * have some kind of persistant store when the vm cannot dump nor keep
5092 * retrying as a solution, but this would be a file architectural change
5093 */
5094
5095 if (!nofreeupl) { /* otherwise stacked file system has to handle this */
5096 if (error) {
5097 int abortflags = 0;
5098 short action = nfs_pageouterrorhandler(error);
5099
5100 switch (action) {
5101 case DUMP:
5102 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
5103 break;
5104 case DUMPANDLOG:
5105 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
5106 if (error <= ELAST &&
5107 (errorcount[error] % 100 == 0))
5108 printf("nfs_pageout: unexpected error %d. dumping vm page\n", error);
5109 errorcount[error]++;
5110 break;
5111 case RETRY:
5112 abortflags = UPL_ABORT_FREE_ON_EMPTY;
5113 break;
5114 case RETRYWITHSLEEP:
5115 abortflags = UPL_ABORT_FREE_ON_EMPTY;
5116 /* pri unused. PSOCK for placeholder. */
5117 tsleep(&lbolt, PSOCK, "nfspageout", 0);
5118 break;
5119 case SEVER: /* not implemented */
5120 default:
5121 printf("nfs_pageout: action %d not expected\n", action);
5122 break;
5123 }
5124
5125 ubc_upl_abort_range(pl, pl_offset, size, abortflags);
5126 /* return error in all cases above */
5127
5128 } else
5129 ubc_upl_commit_range(pl, pl_offset, pgsize,
5130 UPL_COMMIT_CLEAR_DIRTY |
5131 UPL_COMMIT_FREE_ON_EMPTY);
5132 }
5133 return (error);
5134 }
5135
5136 /* Blktooff derives file offset given a logical block number */
5137 static int
5138 nfs_blktooff(ap)
5139 struct vnop_blktooff_args /* {
5140 struct vnodeop_desc *a_desc;
5141 vnode_t a_vp;
5142 daddr64_t a_lblkno;
5143 off_t *a_offset;
5144 } */ *ap;
5145 {
5146 int biosize;
5147 vnode_t vp = ap->a_vp;
5148 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5149
5150 if (!nmp)
5151 return (ENXIO);
5152 biosize = nmp->nm_biosize;
5153
5154 *ap->a_offset = (off_t)(ap->a_lblkno * biosize);
5155
5156 return (0);
5157 }
5158
5159 static int
5160 nfs_offtoblk(ap)
5161 struct vnop_offtoblk_args /* {
5162 struct vnodeop_desc *a_desc;
5163 vnode_t a_vp;
5164 off_t a_offset;
5165 daddr64_t *a_lblkno;
5166 } */ *ap;
5167 {
5168 int biosize;
5169 vnode_t vp = ap->a_vp;
5170 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
5171
5172 if (!nmp)
5173 return (ENXIO);
5174 biosize = nmp->nm_biosize;
5175
5176 *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize);
5177
5178 return (0);
5179 }
5180