]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5ba3f43e | 2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1989, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software contributed to Berkeley by | |
34 | * Rick Macklem at The University of Guelph. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. All advertising materials mentioning features or use of this software | |
45 | * must display the following acknowledgement: | |
46 | * This product includes software developed by the University of | |
47 | * California, Berkeley and its contributors. | |
48 | * 4. Neither the name of the University nor the names of its contributors | |
49 | * may be used to endorse or promote products derived from this software | |
50 | * without specific prior written permission. | |
51 | * | |
52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
55 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
62 | * SUCH DAMAGE. | |
63 | * | |
64 | * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95 | |
65 | * FreeBSD-Id: nfs_vnops.c,v 1.72 1997/11/07 09:20:48 phk Exp $ | |
66 | */ | |
67 | ||
68 | ||
69 | /* | |
70 | * vnode op calls for Sun NFS version 2 and 3 | |
71 | */ | |
1c79356b A |
72 | #include <sys/param.h> |
73 | #include <sys/kernel.h> | |
74 | #include <sys/systm.h> | |
75 | #include <sys/resourcevar.h> | |
91447636 A |
76 | #include <sys/proc_internal.h> |
77 | #include <sys/kauth.h> | |
78 | #include <sys/mount_internal.h> | |
1c79356b | 79 | #include <sys/malloc.h> |
91447636 | 80 | #include <sys/kpi_mbuf.h> |
1c79356b | 81 | #include <sys/conf.h> |
91447636 | 82 | #include <sys/vnode_internal.h> |
1c79356b A |
83 | #include <sys/dirent.h> |
84 | #include <sys/fcntl.h> | |
85 | #include <sys/lockf.h> | |
91447636 A |
86 | #include <sys/ubc_internal.h> |
87 | #include <sys/attr.h> | |
88 | #include <sys/signalvar.h> | |
89 | #include <sys/uio_internal.h> | |
1c79356b | 90 | |
1c79356b A |
91 | #include <vfs/vfs_support.h> |
92 | ||
93 | #include <sys/vm.h> | |
1c79356b | 94 | |
2d21ac55 | 95 | #include <sys/time.h> |
1c79356b | 96 | #include <kern/clock.h> |
91447636 | 97 | #include <libkern/OSAtomic.h> |
1c79356b A |
98 | |
99 | #include <miscfs/fifofs/fifo.h> | |
100 | #include <miscfs/specfs/specdev.h> | |
101 | ||
102 | #include <nfs/rpcv2.h> | |
103 | #include <nfs/nfsproto.h> | |
104 | #include <nfs/nfs.h> | |
105 | #include <nfs/nfsnode.h> | |
2d21ac55 | 106 | #include <nfs/nfs_gss.h> |
1c79356b | 107 | #include <nfs/nfsmount.h> |
55e303ae | 108 | #include <nfs/nfs_lock.h> |
1c79356b A |
109 | #include <nfs/xdr_subs.h> |
110 | #include <nfs/nfsm_subs.h> | |
1c79356b A |
111 | |
112 | #include <net/if.h> | |
113 | #include <netinet/in.h> | |
114 | #include <netinet/in_var.h> | |
b0d623f7 | 115 | |
1c79356b | 116 | #include <vm/vm_kern.h> |
b0d623f7 | 117 | #include <vm/vm_pageout.h> |
1c79356b | 118 | |
9bccf70c A |
119 | #include <kern/task.h> |
120 | #include <kern/sched_prim.h> | |
1c79356b | 121 | |
39236c6e | 122 | #define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__) |
fe8ab488 | 123 | #define DEFAULT_READLINK_NOCACHE 0 |
39236c6e | 124 | |
1c79356b | 125 | /* |
2d21ac55 | 126 | * NFS vnode ops |
1c79356b | 127 | */ |
b0d623f7 A |
128 | int nfs_vnop_lookup(struct vnop_lookup_args *); |
129 | int nfsspec_vnop_read(struct vnop_read_args *); | |
130 | int nfsspec_vnop_write(struct vnop_write_args *); | |
131 | int nfsspec_vnop_close(struct vnop_close_args *); | |
2d21ac55 | 132 | #if FIFO |
b0d623f7 A |
133 | int nfsfifo_vnop_read(struct vnop_read_args *); |
134 | int nfsfifo_vnop_write(struct vnop_write_args *); | |
135 | int nfsfifo_vnop_close(struct vnop_close_args *); | |
2d21ac55 | 136 | #endif |
b0d623f7 A |
137 | int nfs_vnop_ioctl(struct vnop_ioctl_args *); |
138 | int nfs_vnop_select(struct vnop_select_args *); | |
139 | int nfs_vnop_setattr(struct vnop_setattr_args *); | |
b0d623f7 | 140 | int nfs_vnop_fsync(struct vnop_fsync_args *); |
b0d623f7 A |
141 | int nfs_vnop_rename(struct vnop_rename_args *); |
142 | int nfs_vnop_readdir(struct vnop_readdir_args *); | |
143 | int nfs_vnop_readlink(struct vnop_readlink_args *); | |
144 | int nfs_vnop_pathconf(struct vnop_pathconf_args *); | |
145 | int nfs_vnop_pagein(struct vnop_pagein_args *); | |
146 | int nfs_vnop_pageout(struct vnop_pageout_args *); | |
147 | int nfs_vnop_blktooff(struct vnop_blktooff_args *); | |
148 | int nfs_vnop_offtoblk(struct vnop_offtoblk_args *); | |
149 | int nfs_vnop_blockmap(struct vnop_blockmap_args *); | |
6d2010ae | 150 | int nfs_vnop_monitor(struct vnop_monitor_args *); |
b0d623f7 A |
151 | |
152 | int nfs3_vnop_create(struct vnop_create_args *); | |
153 | int nfs3_vnop_mknod(struct vnop_mknod_args *); | |
154 | int nfs3_vnop_getattr(struct vnop_getattr_args *); | |
155 | int nfs3_vnop_link(struct vnop_link_args *); | |
156 | int nfs3_vnop_mkdir(struct vnop_mkdir_args *); | |
157 | int nfs3_vnop_rmdir(struct vnop_rmdir_args *); | |
158 | int nfs3_vnop_symlink(struct vnop_symlink_args *); | |
2d21ac55 | 159 | |
91447636 | 160 | vnop_t **nfsv2_vnodeop_p; |
1c79356b | 161 | static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = { |
91447636 | 162 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
2d21ac55 A |
163 | { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup }, /* lookup */ |
164 | { &vnop_create_desc, (vnop_t *)nfs3_vnop_create }, /* create */ | |
165 | { &vnop_mknod_desc, (vnop_t *)nfs3_vnop_mknod }, /* mknod */ | |
6d2010ae A |
166 | { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */ |
167 | { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */ | |
2d21ac55 A |
168 | { &vnop_access_desc, (vnop_t *)nfs_vnop_access }, /* access */ |
169 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
170 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
171 | { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */ | |
172 | { &vnop_write_desc, (vnop_t *)nfs_vnop_write }, /* write */ | |
173 | { &vnop_ioctl_desc, (vnop_t *)nfs_vnop_ioctl }, /* ioctl */ | |
174 | { &vnop_select_desc, (vnop_t *)nfs_vnop_select }, /* select */ | |
175 | { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke }, /* revoke */ | |
176 | { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */ | |
6d2010ae | 177 | { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */ |
2d21ac55 A |
178 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ |
179 | { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove }, /* remove */ | |
180 | { &vnop_link_desc, (vnop_t *)nfs3_vnop_link }, /* link */ | |
181 | { &vnop_rename_desc, (vnop_t *)nfs_vnop_rename }, /* rename */ | |
182 | { &vnop_mkdir_desc, (vnop_t *)nfs3_vnop_mkdir }, /* mkdir */ | |
183 | { &vnop_rmdir_desc, (vnop_t *)nfs3_vnop_rmdir }, /* rmdir */ | |
184 | { &vnop_symlink_desc, (vnop_t *)nfs3_vnop_symlink }, /* symlink */ | |
185 | { &vnop_readdir_desc, (vnop_t *)nfs_vnop_readdir }, /* readdir */ | |
186 | { &vnop_readlink_desc, (vnop_t *)nfs_vnop_readlink }, /* readlink */ | |
187 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
188 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
91447636 | 189 | { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */ |
2d21ac55 | 190 | { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */ |
6d2010ae | 191 | { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */ |
91447636 | 192 | { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */ |
2d21ac55 A |
193 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ |
194 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
91447636 | 195 | { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */ |
2d21ac55 A |
196 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ |
197 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
198 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
6d2010ae | 199 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
1c79356b A |
200 | { NULL, NULL } |
201 | }; | |
202 | struct vnodeopv_desc nfsv2_vnodeop_opv_desc = | |
203 | { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries }; | |
2d21ac55 A |
204 | |
205 | vnop_t **nfsv4_vnodeop_p; | |
206 | static struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = { | |
207 | { &vnop_default_desc, (vnop_t *)vn_default_error }, | |
208 | { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup }, /* lookup */ | |
209 | { &vnop_create_desc, (vnop_t *)nfs4_vnop_create }, /* create */ | |
210 | { &vnop_mknod_desc, (vnop_t *)nfs4_vnop_mknod }, /* mknod */ | |
6d2010ae A |
211 | { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */ |
212 | { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */ | |
2d21ac55 A |
213 | { &vnop_access_desc, (vnop_t *)nfs_vnop_access }, /* access */ |
214 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
215 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
6d2010ae | 216 | { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */ |
2d21ac55 A |
217 | { &vnop_write_desc, (vnop_t *)nfs_vnop_write }, /* write */ |
218 | { &vnop_ioctl_desc, (vnop_t *)nfs_vnop_ioctl }, /* ioctl */ | |
219 | { &vnop_select_desc, (vnop_t *)nfs_vnop_select }, /* select */ | |
220 | { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke }, /* revoke */ | |
6d2010ae A |
221 | { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */ |
222 | { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */ | |
2d21ac55 A |
223 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ |
224 | { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove }, /* remove */ | |
225 | { &vnop_link_desc, (vnop_t *)nfs4_vnop_link }, /* link */ | |
226 | { &vnop_rename_desc, (vnop_t *)nfs_vnop_rename }, /* rename */ | |
227 | { &vnop_mkdir_desc, (vnop_t *)nfs4_vnop_mkdir }, /* mkdir */ | |
228 | { &vnop_rmdir_desc, (vnop_t *)nfs4_vnop_rmdir }, /* rmdir */ | |
229 | { &vnop_symlink_desc, (vnop_t *)nfs4_vnop_symlink }, /* symlink */ | |
230 | { &vnop_readdir_desc, (vnop_t *)nfs_vnop_readdir }, /* readdir */ | |
231 | { &vnop_readlink_desc, (vnop_t *)nfs_vnop_readlink }, /* readlink */ | |
232 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
233 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
234 | { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */ | |
235 | { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */ | |
6d2010ae | 236 | { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */ |
2d21ac55 A |
237 | { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */ |
238 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
239 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
240 | { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */ | |
241 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
242 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
243 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
6d2010ae A |
244 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ |
245 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
246 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ | |
247 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
248 | #if NAMEDSTREAMS | |
249 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ | |
250 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
251 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ | |
252 | #endif | |
253 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
2d21ac55 A |
254 | { NULL, NULL } |
255 | }; | |
256 | struct vnodeopv_desc nfsv4_vnodeop_opv_desc = | |
257 | { &nfsv4_vnodeop_p, nfsv4_vnodeop_entries }; | |
1c79356b A |
258 | |
259 | /* | |
260 | * Special device vnode ops | |
261 | */ | |
91447636 | 262 | vnop_t **spec_nfsv2nodeop_p; |
1c79356b | 263 | static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = { |
91447636 A |
264 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
265 | { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */ | |
266 | { &vnop_create_desc, (vnop_t *)spec_create }, /* create */ | |
267 | { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */ | |
268 | { &vnop_open_desc, (vnop_t *)spec_open }, /* open */ | |
2d21ac55 A |
269 | { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */ |
270 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
271 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
272 | { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */ | |
273 | { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */ | |
91447636 A |
274 | { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */ |
275 | { &vnop_select_desc, (vnop_t *)spec_select }, /* select */ | |
276 | { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */ | |
277 | { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */ | |
2d21ac55 | 278 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ |
91447636 A |
279 | { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */ |
280 | { &vnop_link_desc, (vnop_t *)spec_link }, /* link */ | |
281 | { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */ | |
282 | { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */ | |
283 | { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */ | |
284 | { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */ | |
285 | { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */ | |
286 | { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */ | |
2d21ac55 A |
287 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ |
288 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
91447636 A |
289 | { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */ |
290 | { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */ | |
291 | { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */ | |
292 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
2d21ac55 A |
293 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ |
294 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
295 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
296 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
297 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
6d2010ae | 298 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
1c79356b A |
299 | { NULL, NULL } |
300 | }; | |
301 | struct vnodeopv_desc spec_nfsv2nodeop_opv_desc = | |
302 | { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries }; | |
2d21ac55 A |
303 | vnop_t **spec_nfsv4nodeop_p; |
304 | static struct vnodeopv_entry_desc spec_nfsv4nodeop_entries[] = { | |
305 | { &vnop_default_desc, (vnop_t *)vn_default_error }, | |
306 | { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */ | |
307 | { &vnop_create_desc, (vnop_t *)spec_create }, /* create */ | |
308 | { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */ | |
309 | { &vnop_open_desc, (vnop_t *)spec_open }, /* open */ | |
310 | { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */ | |
311 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
312 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
313 | { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */ | |
314 | { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */ | |
315 | { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */ | |
316 | { &vnop_select_desc, (vnop_t *)spec_select }, /* select */ | |
317 | { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */ | |
318 | { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */ | |
319 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
320 | { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */ | |
321 | { &vnop_link_desc, (vnop_t *)spec_link }, /* link */ | |
322 | { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */ | |
323 | { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */ | |
324 | { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */ | |
325 | { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */ | |
326 | { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */ | |
327 | { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */ | |
328 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
329 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
330 | { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */ | |
331 | { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */ | |
332 | { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */ | |
333 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
334 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
335 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
336 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
337 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
338 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
6d2010ae A |
339 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ |
340 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
341 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ | |
342 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
343 | #if NAMEDSTREAMS | |
344 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ | |
345 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
346 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ | |
347 | #endif | |
348 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
2d21ac55 A |
349 | { NULL, NULL } |
350 | }; | |
351 | struct vnodeopv_desc spec_nfsv4nodeop_opv_desc = | |
352 | { &spec_nfsv4nodeop_p, spec_nfsv4nodeop_entries }; | |
1c79356b | 353 | |
2d21ac55 | 354 | #if FIFO |
91447636 | 355 | vnop_t **fifo_nfsv2nodeop_p; |
1c79356b | 356 | static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = { |
91447636 A |
357 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
358 | { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */ | |
359 | { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */ | |
360 | { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */ | |
361 | { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */ | |
2d21ac55 A |
362 | { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */ |
363 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
364 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
365 | { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */ | |
366 | { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */ | |
91447636 A |
367 | { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */ |
368 | { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */ | |
369 | { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */ | |
370 | { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */ | |
2d21ac55 | 371 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ |
91447636 A |
372 | { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */ |
373 | { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */ | |
374 | { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */ | |
375 | { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */ | |
376 | { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */ | |
377 | { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */ | |
378 | { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */ | |
379 | { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */ | |
2d21ac55 A |
380 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ |
381 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
91447636 A |
382 | { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */ |
383 | { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */ | |
384 | { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */ | |
385 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
2d21ac55 A |
386 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ |
387 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
388 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
389 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
390 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
6d2010ae | 391 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
1c79356b A |
392 | { NULL, NULL } |
393 | }; | |
394 | struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc = | |
395 | { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries }; | |
0b4e3aa0 | 396 | |
2d21ac55 A |
397 | vnop_t **fifo_nfsv4nodeop_p; |
398 | static struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries[] = { | |
399 | { &vnop_default_desc, (vnop_t *)vn_default_error }, | |
400 | { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */ | |
401 | { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */ | |
402 | { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */ | |
403 | { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */ | |
404 | { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */ | |
405 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
406 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
407 | { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */ | |
408 | { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */ | |
409 | { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */ | |
410 | { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */ | |
411 | { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */ | |
412 | { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */ | |
413 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
414 | { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */ | |
415 | { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */ | |
416 | { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */ | |
417 | { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */ | |
418 | { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */ | |
419 | { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */ | |
420 | { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */ | |
421 | { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */ | |
422 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
423 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
424 | { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */ | |
425 | { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */ | |
426 | { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */ | |
427 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
428 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
429 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
430 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
431 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
432 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
6d2010ae A |
433 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ |
434 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
435 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ | |
436 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
437 | #if NAMEDSTREAMS | |
438 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ | |
439 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
440 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ | |
441 | #endif | |
442 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
2d21ac55 | 443 | { NULL, NULL } |
0b4e3aa0 | 444 | }; |
2d21ac55 A |
445 | struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc = |
446 | { &fifo_nfsv4nodeop_p, fifo_nfsv4nodeop_entries }; | |
447 | #endif /* FIFO */ | |
0b4e3aa0 | 448 | |
b0d623f7 | 449 | int nfs_sillyrename(nfsnode_t,nfsnode_t,struct componentname *,vfs_context_t); |
fe8ab488 A |
450 | int nfs_getattr_internal(nfsnode_t, struct nfs_vattr *, vfs_context_t, int); |
451 | int nfs_refresh_fh(nfsnode_t, vfs_context_t); | |
2d21ac55 A |
452 | |
453 | /* | |
454 | * Find the slot in the access cache for this UID. | |
455 | * If adding and no existing slot is found, reuse slots in FIFO order. | |
6d2010ae | 456 | * The index of the next slot to use is kept in the last entry of the n_access array. |
2d21ac55 A |
457 | */ |
458 | int | |
6d2010ae | 459 | nfs_node_access_slot(nfsnode_t np, uid_t uid, int add) |
0b4e3aa0 | 460 | { |
2d21ac55 A |
461 | int slot; |
462 | ||
463 | for (slot=0; slot < NFS_ACCESS_CACHE_SIZE; slot++) | |
6d2010ae | 464 | if (np->n_accessuid[slot] == uid) |
2d21ac55 A |
465 | break; |
466 | if (slot == NFS_ACCESS_CACHE_SIZE) { | |
467 | if (!add) | |
468 | return (-1); | |
6d2010ae A |
469 | slot = np->n_access[NFS_ACCESS_CACHE_SIZE]; |
470 | np->n_access[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE; | |
2d21ac55 A |
471 | } |
472 | return (slot); | |
0b4e3aa0 | 473 | } |
1c79356b | 474 | |
2d21ac55 | 475 | int |
fe8ab488 | 476 | nfs3_access_rpc(nfsnode_t np, u_int32_t *access, int rpcflags, vfs_context_t ctx) |
1c79356b | 477 | { |
b0d623f7 | 478 | int error = 0, lockerror = ENOENT, status, slot; |
6d2010ae | 479 | uint32_t access_result = 0; |
fa4905b1 | 480 | u_int64_t xid; |
2d21ac55 | 481 | struct nfsm_chain nmreq, nmrep; |
813fb2f6 | 482 | struct nfsmount *nmp; |
55e303ae | 483 | struct timeval now; |
2d21ac55 A |
484 | uid_t uid; |
485 | ||
486 | nfsm_chain_null(&nmreq); | |
487 | nfsm_chain_null(&nmrep); | |
488 | ||
489 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3) + NFSX_UNSIGNED); | |
490 | nfsm_chain_add_fh(error, &nmreq, NFS_VER3, np->n_fhp, np->n_fhsize); | |
6d2010ae | 491 | nfsm_chain_add_32(error, &nmreq, *access); |
2d21ac55 A |
492 | nfsm_chain_build_done(error, &nmreq); |
493 | nfsmout_if(error); | |
fe8ab488 A |
494 | error = nfs_request2(np, NULL, &nmreq, NFSPROC_ACCESS, |
495 | vfs_context_thread(ctx), vfs_context_ucred(ctx), | |
496 | NULL, rpcflags, &nmrep, &xid, &status); | |
b0d623f7 A |
497 | if ((lockerror = nfs_node_lock(np))) |
498 | error = lockerror; | |
2d21ac55 A |
499 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
500 | if (!error) | |
501 | error = status; | |
6d2010ae | 502 | nfsm_chain_get_32(error, &nmrep, access_result); |
2d21ac55 | 503 | nfsmout_if(error); |
0b4e3aa0 | 504 | |
813fb2f6 A |
505 | /* XXXab do we really need mount here, also why are we doing access cache management here? */ |
506 | nmp = NFSTONMP(np); | |
507 | if (nfs_mount_gone(nmp)) { | |
508 | error = ENXIO; | |
509 | } | |
510 | nfsmout_if(error); | |
511 | ||
512 | if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) { | |
513 | uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx)); | |
514 | } else { | |
515 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
516 | } | |
6d2010ae A |
517 | slot = nfs_node_access_slot(np, uid, 1); |
518 | np->n_accessuid[slot] = uid; | |
2d21ac55 | 519 | microuptime(&now); |
6d2010ae A |
520 | np->n_accessstamp[slot] = now.tv_sec; |
521 | np->n_access[slot] = access_result; | |
2d21ac55 A |
522 | |
523 | /* | |
524 | * If we asked for DELETE but didn't get it, the server | |
525 | * may simply not support returning that bit (possible | |
526 | * on UNIX systems). So, we'll assume that it is OK, | |
527 | * and just let any subsequent delete action fail if it | |
528 | * really isn't deletable. | |
529 | */ | |
6d2010ae A |
530 | if ((*access & NFS_ACCESS_DELETE) && |
531 | !(np->n_access[slot] & NFS_ACCESS_DELETE)) | |
532 | np->n_access[slot] |= NFS_ACCESS_DELETE; | |
533 | /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */ | |
534 | if (nfs_access_dotzfs && (np->n_flag & NISDOTZFSCHILD)) | |
535 | np->n_access[slot] |= (NFS_ACCESS_MODIFY|NFS_ACCESS_EXTEND|NFS_ACCESS_DELETE); | |
536 | /* pass back the access returned with this request */ | |
537 | *access = np->n_access[slot]; | |
2d21ac55 | 538 | nfsmout: |
b0d623f7 A |
539 | if (!lockerror) |
540 | nfs_node_unlock(np); | |
2d21ac55 A |
541 | nfsm_chain_cleanup(&nmreq); |
542 | nfsm_chain_cleanup(&nmrep); | |
543 | return (error); | |
1c79356b A |
544 | } |
545 | ||
546 | /* | |
2d21ac55 A |
547 | * NFS access vnode op. |
548 | * For NFS version 2, just return ok. File accesses may fail later. | |
6d2010ae A |
549 | * For NFS version 3+, use the access RPC to check accessibility. If file |
550 | * permissions are changed on the server, accesses might still fail later. | |
1c79356b | 551 | */ |
2d21ac55 A |
552 | int |
553 | nfs_vnop_access( | |
91447636 A |
554 | struct vnop_access_args /* { |
555 | struct vnodeop_desc *a_desc; | |
556 | vnode_t a_vp; | |
2d21ac55 | 557 | int a_action; |
91447636 | 558 | vfs_context_t a_context; |
2d21ac55 | 559 | } */ *ap) |
1c79356b | 560 | { |
2d21ac55 | 561 | vfs_context_t ctx = ap->a_context; |
91447636 | 562 | vnode_t vp = ap->a_vp; |
fe8ab488 | 563 | int error = 0, slot, dorpc, rpcflags = 0; |
6d2010ae | 564 | u_int32_t access, waccess; |
2d21ac55 A |
565 | nfsnode_t np = VTONFS(vp); |
566 | struct nfsmount *nmp; | |
567 | int nfsvers; | |
55e303ae | 568 | struct timeval now; |
2d21ac55 A |
569 | uid_t uid; |
570 | ||
571 | nmp = VTONMP(vp); | |
fe8ab488 | 572 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
573 | return (ENXIO); |
574 | nfsvers = nmp->nm_vers; | |
575 | ||
576 | if (nfsvers == NFS_VER2) { | |
577 | if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) && | |
578 | vfs_isrdonly(vnode_mount(vp))) | |
579 | return (EROFS); | |
580 | return (0); | |
581 | } | |
1c79356b A |
582 | |
583 | /* | |
2d21ac55 | 584 | * For NFS v3, do an access rpc, otherwise you are stuck emulating |
1c79356b A |
585 | * ufs_access() locally using the vattr. This may not be correct, |
586 | * since the server may apply other access criteria such as | |
587 | * client uid-->server uid mapping that we do not know about, but | |
588 | * this is better than just returning anything that is lying about | |
589 | * in the cache. | |
590 | */ | |
91447636 | 591 | |
2d21ac55 A |
592 | /* |
593 | * Convert KAUTH primitives to NFS access rights. | |
594 | */ | |
6d2010ae | 595 | access = 0; |
2d21ac55 A |
596 | if (vnode_isdir(vp)) { |
597 | /* directory */ | |
598 | if (ap->a_action & | |
599 | (KAUTH_VNODE_LIST_DIRECTORY | | |
600 | KAUTH_VNODE_READ_EXTATTRIBUTES)) | |
6d2010ae | 601 | access |= NFS_ACCESS_READ; |
2d21ac55 | 602 | if (ap->a_action & KAUTH_VNODE_SEARCH) |
6d2010ae | 603 | access |= NFS_ACCESS_LOOKUP; |
2d21ac55 A |
604 | if (ap->a_action & |
605 | (KAUTH_VNODE_ADD_FILE | | |
606 | KAUTH_VNODE_ADD_SUBDIRECTORY)) | |
6d2010ae | 607 | access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; |
2d21ac55 | 608 | if (ap->a_action & KAUTH_VNODE_DELETE_CHILD) |
6d2010ae | 609 | access |= NFS_ACCESS_MODIFY; |
91447636 | 610 | } else { |
2d21ac55 A |
611 | /* file */ |
612 | if (ap->a_action & | |
613 | (KAUTH_VNODE_READ_DATA | | |
614 | KAUTH_VNODE_READ_EXTATTRIBUTES)) | |
6d2010ae | 615 | access |= NFS_ACCESS_READ; |
2d21ac55 | 616 | if (ap->a_action & KAUTH_VNODE_WRITE_DATA) |
6d2010ae | 617 | access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; |
2d21ac55 | 618 | if (ap->a_action & KAUTH_VNODE_APPEND_DATA) |
6d2010ae | 619 | access |= NFS_ACCESS_EXTEND; |
2d21ac55 | 620 | if (ap->a_action & KAUTH_VNODE_EXECUTE) |
6d2010ae | 621 | access |= NFS_ACCESS_EXECUTE; |
2d21ac55 A |
622 | } |
623 | /* common */ | |
624 | if (ap->a_action & KAUTH_VNODE_DELETE) | |
6d2010ae | 625 | access |= NFS_ACCESS_DELETE; |
2d21ac55 A |
626 | if (ap->a_action & |
627 | (KAUTH_VNODE_WRITE_ATTRIBUTES | | |
628 | KAUTH_VNODE_WRITE_EXTATTRIBUTES | | |
629 | KAUTH_VNODE_WRITE_SECURITY)) | |
6d2010ae | 630 | access |= NFS_ACCESS_MODIFY; |
2d21ac55 A |
631 | /* XXX this is pretty dubious */ |
632 | if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER) | |
6d2010ae | 633 | access |= NFS_ACCESS_MODIFY; |
2d21ac55 A |
634 | |
635 | /* if caching, always ask for every right */ | |
636 | if (nfs_access_cache_timeout > 0) { | |
6d2010ae | 637 | waccess = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | |
2d21ac55 A |
638 | NFS_ACCESS_EXTEND | NFS_ACCESS_EXECUTE | |
639 | NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; | |
640 | } else { | |
6d2010ae | 641 | waccess = access; |
2d21ac55 A |
642 | } |
643 | ||
b0d623f7 | 644 | if ((error = nfs_node_lock(np))) |
2d21ac55 A |
645 | return (error); |
646 | ||
647 | /* | |
648 | * Does our cached result allow us to give a definite yes to | |
649 | * this request? | |
650 | */ | |
3e170ce0 A |
651 | if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) |
652 | uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx)); | |
653 | else | |
654 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
6d2010ae | 655 | slot = nfs_node_access_slot(np, uid, 0); |
2d21ac55 | 656 | dorpc = 1; |
6d2010ae | 657 | if (access == 0) { |
b0d623f7 A |
658 | /* not asking for any rights understood by NFS, so don't bother doing an RPC */ |
659 | /* OSAddAtomic(1, &nfsstats.accesscache_hits); */ | |
660 | dorpc = 0; | |
6d2010ae A |
661 | waccess = 0; |
662 | } else if (NACCESSVALID(np, slot)) { | |
fe8ab488 A |
663 | microuptime(&now); |
664 | if (((now.tv_sec < (np->n_accessstamp[slot] + nfs_access_cache_timeout)) && | |
665 | ((np->n_access[slot] & access) == access)) || nfs_use_cache(nmp)) { | |
b0d623f7 | 666 | /* OSAddAtomic(1, &nfsstats.accesscache_hits); */ |
fe8ab488 A |
667 | dorpc = 0; |
668 | waccess = np->n_access[slot]; | |
1c79356b | 669 | } |
fa4905b1 | 670 | } |
b0d623f7 | 671 | nfs_node_unlock(np); |
2d21ac55 A |
672 | if (dorpc) { |
673 | /* Either a no, or a don't know. Go to the wire. */ | |
b0d623f7 | 674 | /* OSAddAtomic(1, &nfsstats.accesscache_misses); */ |
fe8ab488 A |
675 | |
676 | /* | |
677 | * Allow an access call to timeout if we have it cached | |
678 | * so we won't hang if the server isn't responding. | |
679 | */ | |
680 | if (NACCESSVALID(np, slot)) | |
681 | rpcflags |= R_SOFT; | |
682 | ||
683 | error = nmp->nm_funcs->nf_access_rpc(np, &waccess, rpcflags, ctx); | |
684 | ||
685 | /* | |
686 | * If the server didn't respond return the cached access. | |
687 | */ | |
688 | if ((error == ETIMEDOUT) && (rpcflags & R_SOFT)) { | |
689 | error = 0; | |
690 | waccess = np->n_access[slot]; | |
691 | } | |
2d21ac55 | 692 | } |
6d2010ae | 693 | if (!error && ((waccess & access) != access)) |
2d21ac55 | 694 | error = EACCES; |
91447636 | 695 | |
0b4e3aa0 | 696 | return (error); |
1c79356b A |
697 | } |
698 | ||
6d2010ae | 699 | |
1c79356b | 700 | /* |
2d21ac55 | 701 | * NFS open vnode op |
6d2010ae A |
702 | * |
703 | * Perform various update/invalidation checks and then add the | |
704 | * open to the node. Regular files will have an open file structure | |
705 | * on the node and, for NFSv4, perform an OPEN request on the server. | |
1c79356b | 706 | */ |
2d21ac55 | 707 | int |
6d2010ae | 708 | nfs_vnop_open( |
91447636 A |
709 | struct vnop_open_args /* { |
710 | struct vnodeop_desc *a_desc; | |
711 | vnode_t a_vp; | |
712 | int a_mode; | |
713 | vfs_context_t a_context; | |
2d21ac55 | 714 | } */ *ap) |
1c79356b | 715 | { |
2d21ac55 | 716 | vfs_context_t ctx = ap->a_context; |
91447636 | 717 | vnode_t vp = ap->a_vp; |
2d21ac55 A |
718 | nfsnode_t np = VTONFS(vp); |
719 | struct nfsmount *nmp; | |
6d2010ae A |
720 | int error, accessMode, denyMode, opened = 0; |
721 | struct nfs_open_owner *noop = NULL; | |
722 | struct nfs_open_file *nofp = NULL; | |
91447636 | 723 | enum vtype vtype; |
6d2010ae A |
724 | |
725 | if (!(ap->a_mode & (FREAD|FWRITE))) | |
726 | return (EINVAL); | |
2d21ac55 A |
727 | |
728 | nmp = VTONMP(vp); | |
fe8ab488 | 729 | if (nfs_mount_gone(nmp)) |
2d21ac55 | 730 | return (ENXIO); |
6d2010ae A |
731 | if (np->n_flag & NREVOKE) |
732 | return (EIO); | |
1c79356b | 733 | |
91447636 | 734 | vtype = vnode_vtype(vp); |
2d21ac55 | 735 | if ((vtype != VREG) && (vtype != VDIR) && (vtype != VLNK)) |
1c79356b | 736 | return (EACCES); |
6d2010ae A |
737 | |
738 | /* First, check if we need to update/invalidate */ | |
2d21ac55 A |
739 | if (ISSET(np->n_flag, NUPDATESIZE)) |
740 | nfs_data_update_size(np, 0); | |
b0d623f7 | 741 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 742 | return (error); |
91447636 A |
743 | if (np->n_flag & NNEEDINVALIDATE) { |
744 | np->n_flag &= ~NNEEDINVALIDATE; | |
b0d623f7 A |
745 | if (vtype == VDIR) |
746 | nfs_invaldir(np); | |
747 | nfs_node_unlock(np); | |
2d21ac55 | 748 | nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1); |
b0d623f7 | 749 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 750 | return (error); |
91447636 | 751 | } |
6d2010ae | 752 | if (vtype == VREG) |
b0d623f7 | 753 | np->n_lastrahead = -1; |
91447636 | 754 | if (np->n_flag & NMODIFIED) { |
91447636 | 755 | if (vtype == VDIR) |
b0d623f7 A |
756 | nfs_invaldir(np); |
757 | nfs_node_unlock(np); | |
758 | if ((error = nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1))) | |
91447636 | 759 | return (error); |
1c79356b | 760 | } else { |
b0d623f7 | 761 | nfs_node_unlock(np); |
1c79356b | 762 | } |
6d2010ae | 763 | |
b0d623f7 | 764 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae A |
765 | if ((error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED))) |
766 | return (error); | |
767 | ||
768 | if (vtype != VREG) { | |
769 | /* Just mark that it was opened */ | |
770 | lck_mtx_lock(&np->n_openlock); | |
771 | np->n_openrefcnt++; | |
772 | lck_mtx_unlock(&np->n_openlock); | |
773 | return (0); | |
774 | } | |
775 | ||
776 | /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */ | |
777 | accessMode = 0; | |
778 | if (ap->a_mode & FREAD) | |
779 | accessMode |= NFS_OPEN_SHARE_ACCESS_READ; | |
780 | if (ap->a_mode & FWRITE) | |
781 | accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE; | |
782 | if (ap->a_mode & O_EXLOCK) | |
783 | denyMode = NFS_OPEN_SHARE_DENY_BOTH; | |
784 | else if (ap->a_mode & O_SHLOCK) | |
785 | denyMode = NFS_OPEN_SHARE_DENY_WRITE; | |
786 | else | |
787 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
788 | // XXX don't do deny modes just yet (and never do it for !v4) | |
789 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
790 | ||
791 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1); | |
792 | if (!noop) | |
793 | return (ENOMEM); | |
794 | ||
795 | restart: | |
796 | error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx)); | |
797 | if (error) { | |
798 | nfs_open_owner_rele(noop); | |
799 | return (error); | |
800 | } | |
801 | if (np->n_flag & NREVOKE) { | |
802 | error = EIO; | |
803 | nfs_mount_state_in_use_end(nmp, 0); | |
804 | nfs_open_owner_rele(noop); | |
805 | return (error); | |
806 | } | |
807 | ||
808 | error = nfs_open_file_find(np, noop, &nofp, accessMode, denyMode, 1); | |
809 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) { | |
810 | NP(np, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
811 | error = EIO; | |
812 | } | |
813 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { | |
814 | nfs_mount_state_in_use_end(nmp, 0); | |
815 | error = nfs4_reopen(nofp, vfs_context_thread(ctx)); | |
816 | nofp = NULL; | |
817 | if (!error) | |
818 | goto restart; | |
819 | } | |
820 | if (!error) | |
821 | error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx)); | |
822 | if (error) { | |
823 | nofp = NULL; | |
824 | goto out; | |
825 | } | |
826 | ||
827 | if (nmp->nm_vers < NFS_VER4) { | |
828 | /* | |
829 | * NFS v2/v3 opens are always allowed - so just add it. | |
830 | */ | |
831 | nfs_open_file_add_open(nofp, accessMode, denyMode, 0); | |
832 | goto out; | |
833 | } | |
834 | ||
835 | /* | |
836 | * If we just created the file and the modes match, then we simply use | |
837 | * the open performed in the create. Otherwise, send the request. | |
838 | */ | |
839 | if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
840 | (nofp->nof_creator == current_thread()) && | |
841 | (accessMode == NFS_OPEN_SHARE_ACCESS_BOTH) && | |
842 | (denyMode == NFS_OPEN_SHARE_DENY_NONE)) { | |
843 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
844 | nofp->nof_creator = NULL; | |
845 | } else { | |
846 | if (!opened) | |
847 | error = nfs4_open(np, nofp, accessMode, denyMode, ctx); | |
848 | if ((error == EACCES) && (nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
849 | (nofp->nof_creator == current_thread())) { | |
850 | /* | |
851 | * Ugh. This can happen if we just created the file with read-only | |
852 | * perms and we're trying to open it for real with different modes | |
853 | * (e.g. write-only or with a deny mode) and the server decides to | |
854 | * not allow the second open because of the read-only perms. | |
855 | * The best we can do is to just use the create's open. | |
856 | * We may have access we don't need or we may not have a requested | |
857 | * deny mode. We may log complaints later, but we'll try to avoid it. | |
858 | */ | |
859 | if (denyMode != NFS_OPEN_SHARE_DENY_NONE) | |
860 | NP(np, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
861 | nofp->nof_creator = NULL; | |
862 | error = 0; | |
863 | } | |
864 | if (error) | |
865 | goto out; | |
866 | opened = 1; | |
867 | /* | |
868 | * If we had just created the file, we already had it open. | |
869 | * If the actual open mode is less than what we grabbed at | |
870 | * create time, then we'll downgrade the open here. | |
871 | */ | |
872 | if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
873 | (nofp->nof_creator == current_thread())) { | |
874 | error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_BOTH, NFS_OPEN_SHARE_DENY_NONE, ctx); | |
875 | if (error) | |
876 | NP(np, "nfs_vnop_open: create close error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
877 | if (!nfs_mount_state_error_should_restart(error)) { | |
878 | error = 0; | |
879 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
880 | } | |
881 | } | |
882 | } | |
883 | ||
884 | out: | |
885 | if (nofp) | |
886 | nfs_open_file_clear_busy(nofp); | |
887 | if (nfs_mount_state_in_use_end(nmp, error)) { | |
888 | nofp = NULL; | |
889 | goto restart; | |
890 | } | |
891 | if (error) | |
892 | NP(np, "nfs_vnop_open: error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); | |
893 | if (noop) | |
894 | nfs_open_owner_rele(noop); | |
316670eb A |
895 | if (!error && vtype == VREG && (ap->a_mode & FWRITE)) { |
896 | lck_mtx_lock(&nmp->nm_lock); | |
897 | nmp->nm_state &= ~NFSSTA_SQUISHY; | |
898 | nmp->nm_curdeadtimeout = nmp->nm_deadtimeout; | |
899 | if (nmp->nm_curdeadtimeout <= 0) | |
900 | nmp->nm_deadto_start = 0; | |
901 | nmp->nm_writers++; | |
902 | lck_mtx_unlock(&nmp->nm_lock); | |
903 | } | |
904 | ||
6d2010ae | 905 | return (error); |
1c79356b A |
906 | } |
907 | ||
316670eb A |
908 | static uint32_t |
909 | nfs_no_of_open_file_writers(nfsnode_t np) | |
910 | { | |
911 | uint32_t writers = 0; | |
912 | struct nfs_open_file *nofp; | |
913 | ||
914 | TAILQ_FOREACH(nofp, &np->n_opens, nof_link) { | |
915 | writers += nofp->nof_w + nofp->nof_rw + nofp->nof_w_dw + nofp->nof_rw_dw + | |
916 | nofp->nof_w_drw + nofp->nof_rw_drw + nofp->nof_d_w_dw + | |
917 | nofp->nof_d_rw_dw + nofp->nof_d_w_drw + nofp->nof_d_rw_drw + | |
918 | nofp->nof_d_w + nofp->nof_d_rw; | |
919 | } | |
920 | ||
921 | return (writers); | |
922 | } | |
6d2010ae | 923 | |
1c79356b | 924 | /* |
2d21ac55 | 925 | * NFS close vnode op |
6d2010ae | 926 | * |
1c79356b A |
927 | * What an NFS client should do upon close after writing is a debatable issue. |
928 | * Most NFS clients push delayed writes to the server upon close, basically for | |
929 | * two reasons: | |
930 | * 1 - So that any write errors may be reported back to the client process | |
931 | * doing the close system call. By far the two most likely errors are | |
932 | * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. | |
933 | * 2 - To put a worst case upper bound on cache inconsistency between | |
934 | * multiple clients for the file. | |
935 | * There is also a consistency problem for Version 2 of the protocol w.r.t. | |
936 | * not being able to tell if other clients are writing a file concurrently, | |
937 | * since there is no way of knowing if the changed modify time in the reply | |
938 | * is only due to the write for this client. | |
939 | * (NFS Version 3 provides weak cache consistency data in the reply that | |
940 | * should be sufficient to detect and handle this case.) | |
941 | * | |
942 | * The current code does the following: | |
943 | * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers | |
6d2010ae A |
944 | * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them. |
945 | * for NFS Version 4 - basically the same as NFSv3 | |
1c79356b | 946 | */ |
2d21ac55 | 947 | int |
6d2010ae | 948 | nfs_vnop_close( |
91447636 | 949 | struct vnop_close_args /* { |
1c79356b | 950 | struct vnodeop_desc *a_desc; |
91447636 A |
951 | vnode_t a_vp; |
952 | int a_fflag; | |
953 | vfs_context_t a_context; | |
2d21ac55 | 954 | } */ *ap) |
1c79356b | 955 | { |
2d21ac55 | 956 | vfs_context_t ctx = ap->a_context; |
91447636 | 957 | vnode_t vp = ap->a_vp; |
2d21ac55 | 958 | nfsnode_t np = VTONFS(vp); |
55e303ae | 959 | struct nfsmount *nmp; |
6d2010ae A |
960 | int error = 0, error1, nfsvers; |
961 | int fflag = ap->a_fflag; | |
962 | enum vtype vtype; | |
963 | int accessMode, denyMode; | |
964 | struct nfs_open_owner *noop = NULL; | |
965 | struct nfs_open_file *nofp = NULL; | |
1c79356b | 966 | |
2d21ac55 A |
967 | nmp = VTONMP(vp); |
968 | if (!nmp) | |
969 | return (ENXIO); | |
970 | nfsvers = nmp->nm_vers; | |
6d2010ae | 971 | vtype = vnode_vtype(vp); |
91447636 | 972 | |
6d2010ae | 973 | /* First, check if we need to update/flush/invalidate */ |
2d21ac55 A |
974 | if (ISSET(np->n_flag, NUPDATESIZE)) |
975 | nfs_data_update_size(np, 0); | |
6d2010ae | 976 | nfs_node_lock_force(np); |
2d21ac55 | 977 | if (np->n_flag & NNEEDINVALIDATE) { |
91447636 | 978 | np->n_flag &= ~NNEEDINVALIDATE; |
b0d623f7 | 979 | nfs_node_unlock(np); |
2d21ac55 | 980 | nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1); |
6d2010ae | 981 | nfs_node_lock_force(np); |
2d21ac55 | 982 | } |
6d2010ae A |
983 | if ((vtype == VREG) && (np->n_flag & NMODIFIED) && (fflag & FWRITE)) { |
984 | /* we're closing an open for write and the file is modified, so flush it */ | |
b0d623f7 | 985 | nfs_node_unlock(np); |
2d21ac55 A |
986 | if (nfsvers != NFS_VER2) |
987 | error = nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), 0); | |
988 | else | |
989 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); | |
b0d623f7 | 990 | nfs_node_lock_force(np); |
91447636 | 991 | NATTRINVALIDATE(np); |
2d21ac55 A |
992 | } |
993 | if (np->n_flag & NWRITEERR) { | |
1c79356b A |
994 | np->n_flag &= ~NWRITEERR; |
995 | error = np->n_error; | |
1c79356b | 996 | } |
b0d623f7 | 997 | nfs_node_unlock(np); |
6d2010ae A |
998 | |
999 | if (vtype != VREG) { | |
1000 | /* Just mark that it was closed */ | |
1001 | lck_mtx_lock(&np->n_openlock); | |
1002 | if (np->n_openrefcnt == 0) { | |
1003 | if (fflag & (FREAD|FWRITE)) { | |
1004 | NP(np, "nfs_vnop_close: open reference underrun"); | |
1005 | error = EINVAL; | |
1006 | } | |
1007 | } else if (fflag & (FREAD|FWRITE)) { | |
1008 | np->n_openrefcnt--; | |
1009 | } else { | |
1010 | /* No FREAD/FWRITE set - probably the final close */ | |
1011 | np->n_openrefcnt = 0; | |
1012 | } | |
1013 | lck_mtx_unlock(&np->n_openlock); | |
1014 | return (error); | |
1015 | } | |
1016 | error1 = error; | |
1017 | ||
1018 | /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */ | |
1019 | accessMode = 0; | |
1020 | if (fflag & FREAD) | |
1021 | accessMode |= NFS_OPEN_SHARE_ACCESS_READ; | |
1022 | if (fflag & FWRITE) | |
1023 | accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE; | |
1024 | // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open | |
1025 | // if (fflag & O_EXLOCK) | |
1026 | // denyMode = NFS_OPEN_SHARE_DENY_BOTH; | |
1027 | // else if (fflag & O_SHLOCK) | |
1028 | // denyMode = NFS_OPEN_SHARE_DENY_WRITE; | |
1029 | // else | |
1030 | // denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
ebb1b9f4 | 1031 | #if 0 // Not yet |
6d2010ae A |
1032 | if (fflag & FHASLOCK) { |
1033 | /* XXX assume FHASLOCK is for the deny mode and not flock */ | |
1034 | /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */ | |
1035 | if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_READ) | |
1036 | denyMode = NFS_OPEN_SHARE_DENY_BOTH; | |
1037 | else if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_WRITE) | |
1038 | denyMode = NFS_OPEN_SHARE_DENY_WRITE; | |
1039 | else | |
1040 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
1041 | } else { | |
1042 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
1043 | } | |
ebb1b9f4 | 1044 | #else |
6d2010ae A |
1045 | // XXX don't do deny modes just yet (and never do it for !v4) |
1046 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
ebb1b9f4 | 1047 | #endif |
6d2010ae A |
1048 | |
1049 | if (!accessMode) { | |
1050 | /* | |
1051 | * No mode given to close? | |
1052 | * Guess this is the final close. | |
1053 | * We should unlock all locks and close all opens. | |
1054 | */ | |
316670eb | 1055 | uint32_t writers; |
6d2010ae | 1056 | mount_t mp = vnode_mount(vp); |
fe8ab488 | 1057 | int force = (!mp || vfs_isforce(mp)); |
316670eb A |
1058 | |
1059 | writers = nfs_no_of_open_file_writers(np); | |
6d2010ae | 1060 | nfs_release_open_state_for_node(np, force); |
316670eb A |
1061 | if (writers) { |
1062 | lck_mtx_lock(&nmp->nm_lock); | |
1063 | if (writers > nmp->nm_writers) { | |
1064 | NP(np, "nfs_vnop_close: number of write opens for mount underrun. Node has %d" | |
1065 | " opens for write. Mount has total of %d opens for write\n", | |
1066 | writers, nmp->nm_writers); | |
1067 | nmp->nm_writers = 0; | |
1068 | } else { | |
1069 | nmp->nm_writers -= writers; | |
1070 | } | |
1071 | lck_mtx_unlock(&nmp->nm_lock); | |
1072 | } | |
1073 | ||
6d2010ae | 1074 | return (error); |
316670eb A |
1075 | } else if (fflag & FWRITE) { |
1076 | lck_mtx_lock(&nmp->nm_lock); | |
1077 | if (nmp->nm_writers == 0) { | |
1078 | NP(np, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing"); | |
1079 | } else { | |
1080 | nmp->nm_writers--; | |
1081 | } | |
1082 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae | 1083 | } |
316670eb | 1084 | |
6d2010ae A |
1085 | |
1086 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 0); | |
1087 | if (!noop) { | |
1088 | // printf("nfs_vnop_close: can't get open owner!\n"); | |
1089 | return (EIO); | |
1090 | } | |
1091 | ||
1092 | restart: | |
1093 | error = nfs_mount_state_in_use_start(nmp, NULL); | |
1094 | if (error) { | |
1095 | nfs_open_owner_rele(noop); | |
1096 | return (error); | |
1097 | } | |
1098 | ||
1099 | error = nfs_open_file_find(np, noop, &nofp, 0, 0, 0); | |
1100 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { | |
1101 | nfs_mount_state_in_use_end(nmp, 0); | |
1102 | error = nfs4_reopen(nofp, NULL); | |
1103 | nofp = NULL; | |
1104 | if (!error) | |
1105 | goto restart; | |
1106 | } | |
1107 | if (error) { | |
1108 | NP(np, "nfs_vnop_close: no open file for owner, error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); | |
1109 | error = EBADF; | |
1110 | goto out; | |
1111 | } | |
1112 | error = nfs_open_file_set_busy(nofp, NULL); | |
1113 | if (error) { | |
1114 | nofp = NULL; | |
1115 | goto out; | |
1116 | } | |
1117 | ||
1118 | error = nfs_close(np, nofp, accessMode, denyMode, ctx); | |
1119 | if (error) | |
1120 | NP(np, "nfs_vnop_close: close error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); | |
1121 | ||
1122 | out: | |
1123 | if (nofp) | |
1124 | nfs_open_file_clear_busy(nofp); | |
1125 | if (nfs_mount_state_in_use_end(nmp, error)) { | |
1126 | nofp = NULL; | |
1127 | goto restart; | |
1128 | } | |
1129 | if (!error) | |
1130 | error = error1; | |
1131 | if (error) | |
1132 | NP(np, "nfs_vnop_close: error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); | |
1133 | if (noop) | |
1134 | nfs_open_owner_rele(noop); | |
1c79356b A |
1135 | return (error); |
1136 | } | |
1137 | ||
6d2010ae A |
1138 | /* |
1139 | * nfs_close(): common function that does all the heavy lifting of file closure | |
1140 | * | |
1141 | * Takes an open file structure and a set of access/deny modes and figures out how | |
1142 | * to update the open file structure (and the state on the server) appropriately. | |
1143 | */ | |
1144 | int | |
1145 | nfs_close( | |
1146 | nfsnode_t np, | |
1147 | struct nfs_open_file *nofp, | |
1148 | uint32_t accessMode, | |
1149 | uint32_t denyMode, | |
1150 | vfs_context_t ctx) | |
1151 | { | |
1152 | struct nfs_lock_owner *nlop; | |
1153 | int error = 0, changed = 0, delegated = 0, closed = 0, downgrade = 0; | |
1154 | uint32_t newAccessMode, newDenyMode; | |
316670eb | 1155 | |
6d2010ae A |
1156 | /* warn if modes don't match current state */ |
1157 | if (((accessMode & nofp->nof_access) != accessMode) || ((denyMode & nofp->nof_deny) != denyMode)) | |
1158 | NP(np, "nfs_close: mode mismatch %d %d, current %d %d, %d", | |
1159 | accessMode, denyMode, nofp->nof_access, nofp->nof_deny, | |
1160 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1161 | ||
1162 | /* | |
1163 | * If we're closing a write-only open, we may not have a write-only count | |
1164 | * if we also grabbed read access. So, check the read-write count. | |
1165 | */ | |
1166 | if (denyMode == NFS_OPEN_SHARE_DENY_NONE) { | |
1167 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1168 | (nofp->nof_w == 0) && (nofp->nof_d_w == 0) && | |
1169 | (nofp->nof_rw || nofp->nof_d_rw)) | |
1170 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; | |
1171 | } else if (denyMode == NFS_OPEN_SHARE_DENY_WRITE) { | |
1172 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1173 | (nofp->nof_w_dw == 0) && (nofp->nof_d_w_dw == 0) && | |
1174 | (nofp->nof_rw_dw || nofp->nof_d_rw_dw)) | |
1175 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; | |
1176 | } else { /* NFS_OPEN_SHARE_DENY_BOTH */ | |
1177 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1178 | (nofp->nof_w_drw == 0) && (nofp->nof_d_w_drw == 0) && | |
1179 | (nofp->nof_rw_drw || nofp->nof_d_rw_drw)) | |
1180 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; | |
1181 | } | |
1182 | ||
1183 | nfs_open_file_remove_open_find(nofp, accessMode, denyMode, &newAccessMode, &newDenyMode, &delegated); | |
1184 | if ((newAccessMode != nofp->nof_access) || (newDenyMode != nofp->nof_deny)) | |
1185 | changed = 1; | |
1186 | else | |
1187 | changed = 0; | |
1188 | ||
1189 | if (NFSTONMP(np)->nm_vers < NFS_VER4) /* NFS v2/v3 closes simply need to remove the open. */ | |
1190 | goto v3close; | |
1191 | ||
1192 | if ((newAccessMode == 0) || (nofp->nof_opencnt == 1)) { | |
1193 | /* | |
1194 | * No more access after this close, so clean up and close it. | |
1195 | * Don't send a close RPC if we're closing a delegated open. | |
1196 | */ | |
1197 | nfs_wait_bufs(np); | |
1198 | closed = 1; | |
1199 | if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) | |
1200 | error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0); | |
1201 | if (error == NFSERR_LOCKS_HELD) { | |
1202 | /* | |
1203 | * Hmm... the server says we have locks we need to release first | |
1204 | * Find the lock owner and try to unlock everything. | |
1205 | */ | |
1206 | nlop = nfs_lock_owner_find(np, vfs_context_proc(ctx), 0); | |
1207 | if (nlop) { | |
1208 | nfs4_unlock_rpc(np, nlop, F_WRLCK, 0, UINT64_MAX, | |
1209 | 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); | |
1210 | nfs_lock_owner_rele(nlop); | |
1211 | } | |
1212 | error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0); | |
1213 | } | |
1214 | } else if (changed) { | |
1215 | /* | |
1216 | * File is still open but with less access, so downgrade the open. | |
1217 | * Don't send a downgrade RPC if we're closing a delegated open. | |
1218 | */ | |
1219 | if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) { | |
1220 | downgrade = 1; | |
1221 | /* | |
1222 | * If we have delegated opens, we should probably claim them before sending | |
1223 | * the downgrade because the server may not know the open we are downgrading to. | |
1224 | */ | |
1225 | if (nofp->nof_d_rw_drw || nofp->nof_d_w_drw || nofp->nof_d_r_drw || | |
1226 | nofp->nof_d_rw_dw || nofp->nof_d_w_dw || nofp->nof_d_r_dw || | |
1227 | nofp->nof_d_rw || nofp->nof_d_w || nofp->nof_d_r) | |
1228 | nfs4_claim_delegated_state_for_open_file(nofp, 0); | |
1229 | /* need to remove the open before sending the downgrade */ | |
1230 | nfs_open_file_remove_open(nofp, accessMode, denyMode); | |
1231 | error = nfs4_open_downgrade_rpc(np, nofp, ctx); | |
1232 | if (error) /* Hmm.. that didn't work. Add the open back in. */ | |
1233 | nfs_open_file_add_open(nofp, accessMode, denyMode, delegated); | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | if (error) { | |
1238 | NP(np, "nfs_close: error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1239 | return (error); | |
1240 | } | |
1241 | ||
1242 | v3close: | |
1243 | if (!downgrade) | |
1244 | nfs_open_file_remove_open(nofp, accessMode, denyMode); | |
1245 | ||
1246 | if (closed) { | |
1247 | lck_mtx_lock(&nofp->nof_lock); | |
1248 | if (nofp->nof_r || nofp->nof_d_r || nofp->nof_w || nofp->nof_d_w || nofp->nof_d_rw || | |
1249 | (nofp->nof_rw && !((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && !nofp->nof_creator && (nofp->nof_rw == 1))) || | |
1250 | nofp->nof_r_dw || nofp->nof_d_r_dw || nofp->nof_w_dw || nofp->nof_d_w_dw || | |
1251 | nofp->nof_rw_dw || nofp->nof_d_rw_dw || nofp->nof_r_drw || nofp->nof_d_r_drw || | |
1252 | nofp->nof_w_drw || nofp->nof_d_w_drw || nofp->nof_rw_drw || nofp->nof_d_rw_drw) | |
1253 | NP(np, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d", | |
1254 | nofp->nof_r, nofp->nof_d_r, nofp->nof_w, nofp->nof_d_w, | |
1255 | nofp->nof_rw, nofp->nof_d_rw, nofp->nof_r_dw, nofp->nof_d_r_dw, | |
1256 | nofp->nof_w_dw, nofp->nof_d_w_dw, nofp->nof_rw_dw, nofp->nof_d_rw_dw, | |
1257 | nofp->nof_r_drw, nofp->nof_d_r_drw, nofp->nof_w_drw, nofp->nof_d_w_drw, | |
1258 | nofp->nof_rw_drw, nofp->nof_d_rw_drw, nofp->nof_flags, | |
1259 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1260 | /* clear out all open info, just to be safe */ | |
1261 | nofp->nof_access = nofp->nof_deny = 0; | |
1262 | nofp->nof_mmap_access = nofp->nof_mmap_deny = 0; | |
1263 | nofp->nof_r = nofp->nof_d_r = 0; | |
1264 | nofp->nof_w = nofp->nof_d_w = 0; | |
1265 | nofp->nof_rw = nofp->nof_d_rw = 0; | |
1266 | nofp->nof_r_dw = nofp->nof_d_r_dw = 0; | |
1267 | nofp->nof_w_dw = nofp->nof_d_w_dw = 0; | |
1268 | nofp->nof_rw_dw = nofp->nof_d_rw_dw = 0; | |
1269 | nofp->nof_r_drw = nofp->nof_d_r_drw = 0; | |
1270 | nofp->nof_w_drw = nofp->nof_d_w_drw = 0; | |
1271 | nofp->nof_rw_drw = nofp->nof_d_rw_drw = 0; | |
1272 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
1273 | lck_mtx_unlock(&nofp->nof_lock); | |
1274 | /* XXX we may potentially want to clean up idle/unused open file structures */ | |
1275 | } | |
1276 | if (nofp->nof_flags & NFS_OPEN_FILE_LOST) { | |
1277 | error = EIO; | |
1278 | NP(np, "nfs_close: LOST%s, %d", !nofp->nof_opencnt ? " (last)" : "", | |
1279 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1280 | } | |
316670eb | 1281 | |
6d2010ae A |
1282 | return (error); |
1283 | } | |
1284 | ||
1285 | ||
91447636 | 1286 | int |
2d21ac55 A |
1287 | nfs3_getattr_rpc( |
1288 | nfsnode_t np, | |
91447636 A |
1289 | mount_t mp, |
1290 | u_char *fhp, | |
2d21ac55 | 1291 | size_t fhsize, |
6d2010ae | 1292 | int flags, |
2d21ac55 | 1293 | vfs_context_t ctx, |
91447636 A |
1294 | struct nfs_vattr *nvap, |
1295 | u_int64_t *xidp) | |
1296 | { | |
2d21ac55 | 1297 | struct nfsmount *nmp = mp ? VFSTONFS(mp) : NFSTONMP(np); |
6d2010ae | 1298 | int error = 0, status, nfsvers, rpcflags = 0; |
2d21ac55 | 1299 | struct nfsm_chain nmreq, nmrep; |
91447636 | 1300 | |
fe8ab488 | 1301 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
1302 | return (ENXIO); |
1303 | nfsvers = nmp->nm_vers; | |
1304 | ||
6d2010ae A |
1305 | if (flags & NGA_MONITOR) /* vnode monitor requests should be soft */ |
1306 | rpcflags = R_RECOVER; | |
1307 | ||
fe8ab488 A |
1308 | if (flags & NGA_SOFT) /* Return ETIMEDOUT if server not responding */ |
1309 | rpcflags |= R_SOFT; | |
1310 | ||
2d21ac55 A |
1311 | nfsm_chain_null(&nmreq); |
1312 | nfsm_chain_null(&nmrep); | |
1313 | ||
1314 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers)); | |
1315 | if (nfsvers != NFS_VER2) | |
1316 | nfsm_chain_add_32(error, &nmreq, fhsize); | |
1317 | nfsm_chain_add_opaque(error, &nmreq, fhp, fhsize); | |
1318 | nfsm_chain_build_done(error, &nmreq); | |
1319 | nfsmout_if(error); | |
6d2010ae A |
1320 | error = nfs_request2(np, mp, &nmreq, NFSPROC_GETATTR, |
1321 | vfs_context_thread(ctx), vfs_context_ucred(ctx), | |
1322 | NULL, rpcflags, &nmrep, xidp, &status); | |
2d21ac55 A |
1323 | if (!error) |
1324 | error = status; | |
1325 | nfsmout_if(error); | |
1326 | error = nfs_parsefattr(&nmrep, nfsvers, nvap); | |
1327 | nfsmout: | |
1328 | nfsm_chain_cleanup(&nmreq); | |
1329 | nfsm_chain_cleanup(&nmrep); | |
91447636 A |
1330 | return (error); |
1331 | } | |
1332 | ||
fe8ab488 A |
1333 | /* |
1334 | * nfs_refresh_fh will attempt to update the file handle for the node. | |
1335 | * | |
1336 | * It only does this for symbolic links and regular files that are not currently opened. | |
1337 | * | |
1338 | * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure. | |
1339 | */ | |
1340 | int | |
1341 | nfs_refresh_fh(nfsnode_t np, vfs_context_t ctx) | |
1342 | { | |
1343 | vnode_t dvp, vp = NFSTOV(np); | |
1344 | nfsnode_t dnp; | |
1345 | const char *v_name = vnode_getname(vp); | |
1346 | char *name; | |
1347 | int namelen, fhsize, refreshed; | |
1348 | int error, wanted = 0; | |
1349 | uint8_t *fhp; | |
1350 | struct timespec ts = {2, 0}; | |
1351 | ||
1352 | NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp)); | |
1353 | ||
1354 | dvp = vnode_parent(vp); | |
1355 | if ((vnode_vtype(vp) != VREG && vnode_vtype(vp) != VLNK) || | |
1356 | v_name == NULL || *v_name == '\0' || dvp == NULL) { | |
1357 | if (v_name != NULL) | |
1358 | vnode_putname(v_name); | |
1359 | return (ESTALE); | |
1360 | } | |
1361 | dnp = VTONFS(dvp); | |
1362 | ||
1363 | namelen = strlen(v_name); | |
1364 | MALLOC(name, char *, namelen + 1, M_TEMP, M_WAITOK); | |
1365 | if (name == NULL) { | |
1366 | vnode_putname(v_name); | |
1367 | return (ESTALE); | |
1368 | } | |
1369 | bcopy(v_name, name, namelen+1); | |
1370 | NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name, name); | |
1371 | vnode_putname(v_name); | |
1372 | ||
1373 | /* Allocate the maximum size file handle */ | |
1374 | MALLOC(fhp, uint8_t *, NFS4_FHSIZE, M_TEMP, M_WAITOK); | |
1375 | if (fhp == NULL) { | |
1376 | FREE(name, M_TEMP); | |
1377 | return (ESTALE); | |
1378 | } | |
1379 | ||
1380 | if ((error = nfs_node_lock(np))) { | |
1381 | FREE(name, M_TEMP); | |
1382 | FREE(fhp, M_TEMP); | |
1383 | return (ESTALE); | |
1384 | } | |
1385 | ||
1386 | fhsize = np->n_fhsize; | |
1387 | bcopy(np->n_fhp, fhp, fhsize); | |
1388 | while (ISSET(np->n_flag, NREFRESH)) { | |
1389 | SET(np->n_flag, NREFRESHWANT); | |
1390 | NFS_VNOP_DBG("Waiting for refresh of %s\n", name); | |
1391 | msleep(np, &np->n_lock, PZERO-1, "nfsrefreshwant", &ts); | |
1392 | if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) | |
1393 | break; | |
1394 | } | |
1395 | refreshed = error ? 0 : !NFS_CMPFH(np, fhp, fhsize); | |
1396 | SET(np->n_flag, NREFRESH); | |
1397 | nfs_node_unlock(np); | |
1398 | ||
1399 | NFS_VNOP_DBG("error = %d, refreshed = %d\n", error, refreshed); | |
1400 | if (error || refreshed) | |
1401 | goto nfsmout; | |
1402 | ||
1403 | /* Check that there are no open references for this file */ | |
1404 | lck_mtx_lock(&np->n_openlock); | |
1405 | if (np->n_openrefcnt || !TAILQ_EMPTY(&np->n_opens) || !TAILQ_EMPTY(&np->n_lock_owners)) { | |
1406 | int cnt = 0; | |
1407 | struct nfs_open_file *ofp; | |
1408 | ||
1409 | TAILQ_FOREACH(ofp, &np->n_opens, nof_link) { | |
1410 | cnt += ofp->nof_opencnt; | |
1411 | } | |
1412 | if (cnt) { | |
1413 | lck_mtx_unlock(&np->n_openlock); | |
1414 | NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name); | |
1415 | NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n", | |
1416 | np->n_openrefcnt, cnt, !TAILQ_EMPTY(&np->n_lock_owners)); | |
1417 | error = ESTALE; | |
1418 | goto nfsmout; | |
1419 | } | |
1420 | } | |
1421 | lck_mtx_unlock(&np->n_openlock); | |
1422 | /* | |
1423 | * Since the FH is currently stale we should not be able to | |
1424 | * establish any open state until the FH is refreshed. | |
1425 | */ | |
1426 | ||
1427 | error = nfs_node_lock(np); | |
1428 | nfsmout_if(error); | |
1429 | /* | |
1430 | * Symlinks should never need invalidations and are holding | |
1431 | * the one and only nfsbuf in an uncached acquired state | |
1432 | * trying to do a readlink. So we will hang if we invalidate | |
1433 | * in that case. Only in in the VREG case do we need to | |
1434 | * invalidate. | |
1435 | */ | |
1436 | if (vnode_vtype(vp) == VREG) { | |
1437 | np->n_flag &= ~NNEEDINVALIDATE; | |
1438 | nfs_node_unlock(np); | |
1439 | error = nfs_vinvalbuf(vp, V_IGNORE_WRITEERR, ctx, 1); | |
1440 | if (error) | |
1441 | NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error); | |
1442 | nfsmout_if(error); | |
1443 | } else { | |
1444 | nfs_node_unlock(np); | |
1445 | } | |
1446 | ||
1447 | NFS_VNOP_DBG("Looking up %s\n", name); | |
1448 | error = nfs_lookitup(dnp, name, namelen, ctx, &np); | |
1449 | if (error) | |
1450 | NFS_VNOP_DBG("nfs_lookitup returned %d\n", error); | |
1451 | ||
1452 | nfsmout: | |
1453 | nfs_node_lock_force(np); | |
1454 | wanted = ISSET(np->n_flag, NREFRESHWANT); | |
1455 | CLR(np->n_flag, NREFRESH|NREFRESHWANT); | |
1456 | nfs_node_unlock(np); | |
1457 | if (wanted) | |
1458 | wakeup(np); | |
1459 | ||
1460 | if (error == 0) | |
1461 | NFS_VNOP_DBG("%s refreshed file handle\n", name); | |
1462 | ||
1463 | FREE(name, M_TEMP); | |
1464 | FREE(fhp, M_TEMP); | |
1465 | ||
1466 | return (error ? ESTALE : 0); | |
1467 | } | |
2d21ac55 | 1468 | |
91447636 | 1469 | int |
6d2010ae | 1470 | nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags) |
fe8ab488 A |
1471 | { |
1472 | int error; | |
1473 | ||
1474 | retry: | |
1475 | error = nfs_getattr_internal(np, nvap, ctx, flags); | |
1476 | if (error == ESTALE) { | |
1477 | error = nfs_refresh_fh(np, ctx); | |
1478 | if (!error) | |
1479 | goto retry; | |
1480 | } | |
1481 | return (error); | |
1482 | } | |
1483 | ||
1484 | int | |
1485 | nfs_getattr_internal(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags) | |
1c79356b | 1486 | { |
2d21ac55 | 1487 | struct nfsmount *nmp; |
b0d623f7 | 1488 | int error = 0, nfsvers, inprogset = 0, wanted = 0, avoidfloods; |
6d2010ae | 1489 | struct nfs_vattr nvattr; |
b0d623f7 | 1490 | struct timespec ts = { 2, 0 }; |
fa4905b1 | 1491 | u_int64_t xid; |
91447636 A |
1492 | |
1493 | FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag); | |
1494 | ||
fe8ab488 A |
1495 | nmp = NFSTONMP(np); |
1496 | ||
1497 | if (nfs_mount_gone(nmp)) | |
b0d623f7 A |
1498 | return (ENXIO); |
1499 | nfsvers = nmp->nm_vers; | |
1500 | ||
6d2010ae A |
1501 | if (!nvap) |
1502 | nvap = &nvattr; | |
1503 | NVATTR_INIT(nvap); | |
1504 | ||
2d21ac55 A |
1505 | /* Update local times for special files. */ |
1506 | if (np->n_flag & (NACC | NUPD)) { | |
b0d623f7 | 1507 | nfs_node_lock_force(np); |
1c79356b | 1508 | np->n_flag |= NCHG; |
b0d623f7 | 1509 | nfs_node_unlock(np); |
2d21ac55 A |
1510 | } |
1511 | /* Update size, if necessary */ | |
b0d623f7 | 1512 | if (ISSET(np->n_flag, NUPDATESIZE)) |
2d21ac55 A |
1513 | nfs_data_update_size(np, 0); |
1514 | ||
b0d623f7 A |
1515 | error = nfs_node_lock(np); |
1516 | nfsmout_if(error); | |
6d2010ae A |
1517 | if (!(flags & (NGA_UNCACHED|NGA_MONITOR)) || ((nfsvers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))) { |
1518 | /* | |
1519 | * Use the cache or wait for any getattr in progress if: | |
1520 | * - it's a cached request, or | |
fe8ab488 A |
1521 | * - we have a delegation, or |
1522 | * - the server isn't responding | |
6d2010ae | 1523 | */ |
b0d623f7 | 1524 | while (1) { |
6d2010ae | 1525 | error = nfs_getattrcache(np, nvap, flags); |
b0d623f7 A |
1526 | if (!error || (error != ENOENT)) { |
1527 | nfs_node_unlock(np); | |
1528 | goto nfsmout; | |
1529 | } | |
6d2010ae | 1530 | error = 0; |
b0d623f7 A |
1531 | if (!ISSET(np->n_flag, NGETATTRINPROG)) |
1532 | break; | |
6d2010ae A |
1533 | if (flags & NGA_MONITOR) { |
1534 | /* no need to wait if a request is pending */ | |
1535 | error = EINPROGRESS; | |
1536 | nfs_node_unlock(np); | |
1537 | goto nfsmout; | |
1538 | } | |
b0d623f7 A |
1539 | SET(np->n_flag, NGETATTRWANT); |
1540 | msleep(np, &np->n_lock, PZERO-1, "nfsgetattrwant", &ts); | |
1541 | if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) { | |
1542 | nfs_node_unlock(np); | |
1543 | goto nfsmout; | |
1544 | } | |
1545 | } | |
1546 | SET(np->n_flag, NGETATTRINPROG); | |
1547 | inprogset = 1; | |
1548 | } else if (!ISSET(np->n_flag, NGETATTRINPROG)) { | |
1549 | SET(np->n_flag, NGETATTRINPROG); | |
1550 | inprogset = 1; | |
6d2010ae A |
1551 | } else if (flags & NGA_MONITOR) { |
1552 | /* no need to make a request if one is pending */ | |
1553 | error = EINPROGRESS; | |
b0d623f7 A |
1554 | } |
1555 | nfs_node_unlock(np); | |
55e303ae | 1556 | |
2d21ac55 | 1557 | nmp = NFSTONMP(np); |
fe8ab488 | 1558 | if (nfs_mount_gone(nmp)) |
2d21ac55 | 1559 | error = ENXIO; |
6d2010ae | 1560 | if (error) |
2d21ac55 | 1561 | goto nfsmout; |
fa4905b1 | 1562 | |
fe8ab488 A |
1563 | /* |
1564 | * Return cached attributes if they are valid, | |
1565 | * if the server doesn't respond, and this is | |
1566 | * some softened up style of mount. | |
1567 | */ | |
1568 | if (NATTRVALID(np) && nfs_use_cache(nmp)) | |
1569 | flags |= NGA_SOFT; | |
1570 | ||
91447636 | 1571 | /* |
6d2010ae A |
1572 | * We might want to try to get both the attributes and access info by |
1573 | * making an ACCESS call and seeing if it returns updated attributes. | |
91447636 A |
1574 | * But don't bother if we aren't caching access info or if the |
1575 | * attributes returned wouldn't be cached. | |
1576 | */ | |
6d2010ae | 1577 | if (!(flags & NGA_ACL) && (nfsvers != NFS_VER2) && nfs_access_for_getattr && (nfs_access_cache_timeout > 0)) { |
2d21ac55 | 1578 | if (nfs_attrcachetimeout(np) > 0) { |
b0d623f7 | 1579 | /* OSAddAtomic(1, &nfsstats.accesscache_misses); */ |
6d2010ae | 1580 | u_int32_t access = NFS_ACCESS_ALL; |
fe8ab488 A |
1581 | int rpcflags = 0; |
1582 | ||
1583 | /* Return cached attrs if server doesn't respond */ | |
1584 | if (flags & NGA_SOFT) | |
1585 | rpcflags |= R_SOFT; | |
1586 | ||
1587 | error = nmp->nm_funcs->nf_access_rpc(np, &access, rpcflags, ctx); | |
1588 | ||
1589 | if (error == ETIMEDOUT) | |
1590 | goto returncached; | |
1591 | ||
2d21ac55 A |
1592 | if (error) |
1593 | goto nfsmout; | |
b0d623f7 | 1594 | nfs_node_lock_force(np); |
6d2010ae | 1595 | error = nfs_getattrcache(np, nvap, flags); |
b0d623f7 A |
1596 | nfs_node_unlock(np); |
1597 | if (!error || (error != ENOENT)) | |
2d21ac55 | 1598 | goto nfsmout; |
b0d623f7 | 1599 | /* Well, that didn't work... just do a getattr... */ |
2d21ac55 A |
1600 | error = 0; |
1601 | } | |
1c79356b | 1602 | } |
b0d623f7 | 1603 | |
fa4905b1 | 1604 | avoidfloods = 0; |
fe8ab488 | 1605 | |
fa4905b1 | 1606 | tryagain: |
6d2010ae | 1607 | error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, flags, ctx, nvap, &xid); |
b0d623f7 A |
1608 | if (!error) { |
1609 | nfs_node_lock_force(np); | |
1610 | error = nfs_loadattrcache(np, nvap, &xid, 0); | |
1611 | nfs_node_unlock(np); | |
1612 | } | |
fe8ab488 A |
1613 | |
1614 | /* | |
1615 | * If the server didn't respond, return cached attributes. | |
1616 | */ | |
1617 | returncached: | |
1618 | if ((flags & NGA_SOFT) && (error == ETIMEDOUT)) { | |
1619 | nfs_node_lock_force(np); | |
1620 | error = nfs_getattrcache(np, nvap, flags); | |
1621 | if (!error || (error != ENOENT)) { | |
1622 | nfs_node_unlock(np); | |
1623 | goto nfsmout; | |
1624 | } | |
1625 | nfs_node_unlock(np); | |
1626 | } | |
2d21ac55 | 1627 | nfsmout_if(error); |
fe8ab488 | 1628 | |
2d21ac55 A |
1629 | if (!xid) { /* out-of-order rpc - attributes were dropped */ |
1630 | FSDBG(513, -1, np, np->n_xid >> 32, np->n_xid); | |
b0d623f7 | 1631 | if (avoidfloods++ < 20) |
2d21ac55 | 1632 | goto tryagain; |
b0d623f7 A |
1633 | /* avoidfloods>1 is bizarre. at 20 pull the plug */ |
1634 | /* just return the last attributes we got */ | |
1635 | } | |
1636 | nfsmout: | |
1637 | nfs_node_lock_force(np); | |
1638 | if (inprogset) { | |
1639 | wanted = ISSET(np->n_flag, NGETATTRWANT); | |
1640 | CLR(np->n_flag, (NGETATTRINPROG | NGETATTRWANT)); | |
91447636 | 1641 | } |
b0d623f7 A |
1642 | if (!error) { |
1643 | /* check if the node changed on us */ | |
2d21ac55 A |
1644 | vnode_t vp = NFSTOV(np); |
1645 | enum vtype vtype = vnode_vtype(vp); | |
b0d623f7 A |
1646 | if ((vtype == VDIR) && NFS_CHANGED_NC(nfsvers, np, nvap)) { |
1647 | FSDBG(513, -1, np, 0, np); | |
1648 | np->n_flag &= ~NNEGNCENTRIES; | |
1649 | cache_purge(vp); | |
1650 | np->n_ncgen++; | |
1651 | NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap); | |
39236c6e A |
1652 | NFS_VNOP_DBG("Purge directory 0x%llx\n", |
1653 | (uint64_t)VM_KERNEL_ADDRPERM(vp)); | |
2d21ac55 | 1654 | } |
b0d623f7 A |
1655 | if (NFS_CHANGED(nfsvers, np, nvap)) { |
1656 | FSDBG(513, -1, np, -1, np); | |
39236c6e A |
1657 | if (vtype == VDIR) { |
1658 | NFS_VNOP_DBG("Invalidate directory 0x%llx\n", | |
1659 | (uint64_t)VM_KERNEL_ADDRPERM(vp)); | |
b0d623f7 | 1660 | nfs_invaldir(np); |
39236c6e | 1661 | } |
b0d623f7 A |
1662 | nfs_node_unlock(np); |
1663 | if (wanted) | |
1664 | wakeup(np); | |
2d21ac55 | 1665 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
fa4905b1 | 1666 | FSDBG(513, -1, np, -2, error); |
55e303ae | 1667 | if (!error) { |
b0d623f7 | 1668 | nfs_node_lock_force(np); |
2d21ac55 | 1669 | NFS_CHANGED_UPDATE(nfsvers, np, nvap); |
b0d623f7 | 1670 | nfs_node_unlock(np); |
55e303ae | 1671 | } |
2d21ac55 | 1672 | } else { |
b0d623f7 A |
1673 | nfs_node_unlock(np); |
1674 | if (wanted) | |
1675 | wakeup(np); | |
1c79356b | 1676 | } |
b0d623f7 A |
1677 | } else { |
1678 | nfs_node_unlock(np); | |
1679 | if (wanted) | |
1680 | wakeup(np); | |
1c79356b | 1681 | } |
6d2010ae A |
1682 | |
1683 | if (nvap == &nvattr) { | |
1684 | NVATTR_CLEANUP(nvap); | |
1685 | } else if (!(flags & NGA_ACL)) { | |
1686 | /* make sure we don't return an ACL if it wasn't asked for */ | |
1687 | NFS_BITMAP_CLR(nvap->nva_bitmap, NFS_FATTR_ACL); | |
1688 | if (nvap->nva_acl) { | |
1689 | kauth_acl_free(nvap->nva_acl); | |
1690 | nvap->nva_acl = NULL; | |
1691 | } | |
1692 | } | |
2d21ac55 | 1693 | FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag); |
91447636 A |
1694 | return (error); |
1695 | } | |
1696 | ||
2d21ac55 A |
1697 | /* |
1698 | * NFS getattr call from vfs. | |
1699 | */ | |
39236c6e A |
1700 | |
1701 | /* | |
1702 | * The attributes we support over the wire. | |
1703 | * We also get fsid but the vfs layer gets it out of the mount | |
1704 | * structure after this calling us so there's no need to return it, | |
1705 | * and Finder expects to call getattrlist just looking for the FSID | |
1706 | * with out hanging on a non responsive server. | |
1707 | */ | |
1708 | #define NFS3_SUPPORTED_VATTRS \ | |
1709 | (VNODE_ATTR_va_rdev | \ | |
1710 | VNODE_ATTR_va_nlink | \ | |
1711 | VNODE_ATTR_va_data_size | \ | |
1712 | VNODE_ATTR_va_data_alloc | \ | |
1713 | VNODE_ATTR_va_uid | \ | |
1714 | VNODE_ATTR_va_gid | \ | |
1715 | VNODE_ATTR_va_mode | \ | |
1716 | VNODE_ATTR_va_modify_time | \ | |
1717 | VNODE_ATTR_va_change_time | \ | |
1718 | VNODE_ATTR_va_access_time | \ | |
1719 | VNODE_ATTR_va_fileid | \ | |
1720 | VNODE_ATTR_va_type) | |
1721 | ||
b0d623f7 | 1722 | int |
2d21ac55 | 1723 | nfs3_vnop_getattr( |
91447636 A |
1724 | struct vnop_getattr_args /* { |
1725 | struct vnodeop_desc *a_desc; | |
1726 | vnode_t a_vp; | |
1727 | struct vnode_attr *a_vap; | |
1728 | vfs_context_t a_context; | |
1729 | } */ *ap) | |
1730 | { | |
1731 | int error; | |
1732 | struct nfs_vattr nva; | |
1733 | struct vnode_attr *vap = ap->a_vap; | |
5ba3f43e | 1734 | struct nfsmount *nmp; |
2d21ac55 | 1735 | dev_t rdev; |
91447636 | 1736 | |
39236c6e A |
1737 | /* |
1738 | * Lets don't go over the wire if we don't support any of the attributes. | |
1739 | * Just fall through at the VFS layer and let it cons up what it needs. | |
1740 | */ | |
1741 | /* Return the io size no matter what, since we don't go over the wire for this */ | |
1742 | VATTR_RETURN(vap, va_iosize, nfs_iosize); | |
1743 | if ((vap->va_active & NFS3_SUPPORTED_VATTRS) == 0) | |
1744 | return (0); | |
1745 | ||
1746 | if (VATTR_IS_ACTIVE(ap->a_vap, va_name)) | |
1747 | NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n", | |
1748 | (uint64_t)VM_KERNEL_ADDRPERM(ap->a_vp), | |
1749 | ap->a_vp->v_name ? ap->a_vp->v_name : "empty"); | |
b0d623f7 | 1750 | error = nfs_getattr(VTONFS(ap->a_vp), &nva, ap->a_context, NGA_CACHED); |
91447636 A |
1751 | if (error) |
1752 | return (error); | |
1753 | ||
1754 | /* copy nva to *a_vap */ | |
5ba3f43e A |
1755 | nmp = VTONMP(ap->a_vp); |
1756 | vap->va_flags |= nmp ? (nmp->nm_vers > 2 ? VA_64BITOBJIDS : 0) : 0; | |
2d21ac55 A |
1757 | VATTR_RETURN(vap, va_type, nva.nva_type); |
1758 | VATTR_RETURN(vap, va_mode, nva.nva_mode); | |
1759 | rdev = makedev(nva.nva_rawdev.specdata1, nva.nva_rawdev.specdata2); | |
1760 | VATTR_RETURN(vap, va_rdev, rdev); | |
1761 | VATTR_RETURN(vap, va_uid, nva.nva_uid); | |
1762 | VATTR_RETURN(vap, va_gid, nva.nva_gid); | |
1763 | VATTR_RETURN(vap, va_nlink, nva.nva_nlink); | |
1764 | VATTR_RETURN(vap, va_fileid, nva.nva_fileid); | |
1765 | VATTR_RETURN(vap, va_data_size, nva.nva_size); | |
1766 | VATTR_RETURN(vap, va_data_alloc, nva.nva_bytes); | |
2d21ac55 A |
1767 | vap->va_access_time.tv_sec = nva.nva_timesec[NFSTIME_ACCESS]; |
1768 | vap->va_access_time.tv_nsec = nva.nva_timensec[NFSTIME_ACCESS]; | |
1769 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
1770 | vap->va_modify_time.tv_sec = nva.nva_timesec[NFSTIME_MODIFY]; | |
1771 | vap->va_modify_time.tv_nsec = nva.nva_timensec[NFSTIME_MODIFY]; | |
1772 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
1773 | vap->va_change_time.tv_sec = nva.nva_timesec[NFSTIME_CHANGE]; | |
1774 | vap->va_change_time.tv_nsec = nva.nva_timensec[NFSTIME_CHANGE]; | |
1775 | VATTR_SET_SUPPORTED(vap, va_change_time); | |
91447636 | 1776 | |
2d21ac55 | 1777 | // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */); |
1c79356b A |
1778 | return (error); |
1779 | } | |
1780 | ||
1781 | /* | |
2d21ac55 | 1782 | * NFS setattr call. |
1c79356b | 1783 | */ |
b0d623f7 | 1784 | int |
2d21ac55 | 1785 | nfs_vnop_setattr( |
91447636 | 1786 | struct vnop_setattr_args /* { |
1c79356b | 1787 | struct vnodeop_desc *a_desc; |
91447636 A |
1788 | vnode_t a_vp; |
1789 | struct vnode_attr *a_vap; | |
1790 | vfs_context_t a_context; | |
2d21ac55 | 1791 | } */ *ap) |
1c79356b | 1792 | { |
2d21ac55 | 1793 | vfs_context_t ctx = ap->a_context; |
91447636 | 1794 | vnode_t vp = ap->a_vp; |
2d21ac55 | 1795 | nfsnode_t np = VTONFS(vp); |
0c530ab8 | 1796 | struct nfsmount *nmp; |
91447636 | 1797 | struct vnode_attr *vap = ap->a_vap; |
1c79356b | 1798 | int error = 0; |
6d2010ae A |
1799 | int biosize, nfsvers, namedattrs; |
1800 | u_quad_t origsize, vapsize; | |
2d21ac55 A |
1801 | struct nfs_dulookup dul; |
1802 | nfsnode_t dnp = NULL; | |
813fb2f6 | 1803 | int dul_in_progress = 0; |
2d21ac55 A |
1804 | vnode_t dvp = NULL; |
1805 | const char *vname = NULL; | |
b0d623f7 A |
1806 | struct nfs_open_owner *noop = NULL; |
1807 | struct nfs_open_file *nofp = NULL; | |
2d21ac55 A |
1808 | |
1809 | nmp = VTONMP(vp); | |
fe8ab488 | 1810 | if (nfs_mount_gone(nmp)) |
0c530ab8 | 1811 | return (ENXIO); |
2d21ac55 | 1812 | nfsvers = nmp->nm_vers; |
6d2010ae | 1813 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
0c530ab8 | 1814 | biosize = nmp->nm_biosize; |
fa4905b1 | 1815 | |
91447636 | 1816 | /* Disallow write attempts if the filesystem is mounted read-only. */ |
2d21ac55 | 1817 | if (vnode_vfsisrdonly(vp)) |
1c79356b | 1818 | return (EROFS); |
91447636 | 1819 | |
2d21ac55 | 1820 | origsize = np->n_size; |
91447636 | 1821 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { |
2d21ac55 A |
1822 | switch (vnode_vtype(vp)) { |
1823 | case VDIR: | |
1824 | return (EISDIR); | |
1825 | case VCHR: | |
1826 | case VBLK: | |
1827 | case VSOCK: | |
1828 | case VFIFO: | |
91447636 A |
1829 | if (!VATTR_IS_ACTIVE(vap, va_modify_time) && |
1830 | !VATTR_IS_ACTIVE(vap, va_access_time) && | |
1831 | !VATTR_IS_ACTIVE(vap, va_mode) && | |
1832 | !VATTR_IS_ACTIVE(vap, va_uid) && | |
2d21ac55 | 1833 | !VATTR_IS_ACTIVE(vap, va_gid)) { |
1c79356b | 1834 | return (0); |
2d21ac55 | 1835 | } |
91447636 | 1836 | VATTR_CLEAR_ACTIVE(vap, va_data_size); |
2d21ac55 A |
1837 | break; |
1838 | default: | |
1c79356b A |
1839 | /* |
1840 | * Disallow write attempts if the filesystem is | |
1841 | * mounted read-only. | |
1842 | */ | |
91447636 | 1843 | if (vnode_vfsisrdonly(vp)) |
1c79356b | 1844 | return (EROFS); |
91447636 A |
1845 | FSDBG_TOP(512, np->n_size, vap->va_data_size, |
1846 | np->n_vattr.nva_size, np->n_flag); | |
2d21ac55 | 1847 | /* clear NNEEDINVALIDATE, if set */ |
b0d623f7 | 1848 | if ((error = nfs_node_lock(np))) |
2d21ac55 A |
1849 | return (error); |
1850 | if (np->n_flag & NNEEDINVALIDATE) | |
1851 | np->n_flag &= ~NNEEDINVALIDATE; | |
b0d623f7 | 1852 | nfs_node_unlock(np); |
2d21ac55 A |
1853 | /* flush everything */ |
1854 | error = nfs_vinvalbuf(vp, (vap->va_data_size ? V_SAVE : 0) , ctx, 1); | |
1855 | if (error) { | |
6d2010ae | 1856 | NP(np, "nfs_setattr: nfs_vinvalbuf %d", error); |
2d21ac55 A |
1857 | FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, -1); |
1858 | return (error); | |
1859 | } | |
b0d623f7 A |
1860 | if (nfsvers >= NFS_VER4) { |
1861 | /* setting file size requires having the file open for write access */ | |
6d2010ae A |
1862 | if (np->n_flag & NREVOKE) |
1863 | return (EIO); | |
b0d623f7 A |
1864 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1); |
1865 | if (!noop) | |
1866 | return (ENOMEM); | |
6d2010ae A |
1867 | restart: |
1868 | error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx)); | |
1869 | if (error) | |
1870 | return (error); | |
1871 | if (np->n_flag & NREVOKE) { | |
1872 | nfs_mount_state_in_use_end(nmp, 0); | |
1873 | return (EIO); | |
1874 | } | |
b0d623f7 A |
1875 | error = nfs_open_file_find(np, noop, &nofp, 0, 0, 1); |
1876 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) | |
1877 | error = EIO; | |
1878 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { | |
6d2010ae A |
1879 | nfs_mount_state_in_use_end(nmp, 0); |
1880 | error = nfs4_reopen(nofp, vfs_context_thread(ctx)); | |
b0d623f7 | 1881 | nofp = NULL; |
6d2010ae A |
1882 | if (!error) |
1883 | goto restart; | |
b0d623f7 | 1884 | } |
6d2010ae A |
1885 | if (!error) |
1886 | error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx)); | |
b0d623f7 A |
1887 | if (error) { |
1888 | nfs_open_owner_rele(noop); | |
1889 | return (error); | |
1890 | } | |
1891 | if (!(nofp->nof_access & NFS_OPEN_SHARE_ACCESS_WRITE)) { | |
1892 | /* we don't have the file open for write access, so open it */ | |
b0d623f7 A |
1893 | error = nfs4_open(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx); |
1894 | if (!error) | |
1895 | nofp->nof_flags |= NFS_OPEN_FILE_SETATTR; | |
1896 | if (nfs_mount_state_error_should_restart(error)) { | |
1897 | nfs_open_file_clear_busy(nofp); | |
1898 | nofp = NULL; | |
6d2010ae A |
1899 | if (nfs_mount_state_in_use_end(nmp, error)) |
1900 | goto restart; | |
b0d623f7 | 1901 | } |
b0d623f7 A |
1902 | } |
1903 | } | |
1904 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); | |
2d21ac55 | 1905 | if (np->n_size > vap->va_data_size) { /* shrinking? */ |
91447636 | 1906 | daddr64_t obn, bn; |
0c530ab8 | 1907 | int neweofoff, mustwrite; |
55e303ae | 1908 | struct nfsbuf *bp; |
fa4905b1 | 1909 | |
fa4905b1 | 1910 | obn = (np->n_size - 1) / biosize; |
2d21ac55 | 1911 | bn = vap->va_data_size / biosize; |
91447636 | 1912 | for ( ; obn >= bn; obn--) { |
2d21ac55 | 1913 | if (!nfs_buf_is_incore(np, obn)) |
91447636 | 1914 | continue; |
2d21ac55 | 1915 | error = nfs_buf_get(np, obn, biosize, NULL, NBLK_READ, &bp); |
91447636 | 1916 | if (error) |
55e303ae | 1917 | continue; |
91447636 A |
1918 | if (obn != bn) { |
1919 | FSDBG(512, bp, bp->nb_flags, 0, obn); | |
1920 | SET(bp->nb_flags, NB_INVAL); | |
1921 | nfs_buf_release(bp, 1); | |
1922 | continue; | |
1923 | } | |
1924 | mustwrite = 0; | |
1925 | neweofoff = vap->va_data_size - NBOFF(bp); | |
1926 | /* check for any dirty data before the new EOF */ | |
2d21ac55 | 1927 | if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) { |
55e303ae | 1928 | /* clip dirty range to EOF */ |
2d21ac55 | 1929 | if (bp->nb_dirtyend > neweofoff) { |
91447636 | 1930 | bp->nb_dirtyend = neweofoff; |
2d21ac55 A |
1931 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) |
1932 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; | |
1933 | } | |
1934 | if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) | |
1935 | mustwrite++; | |
91447636 A |
1936 | } |
1937 | bp->nb_dirty &= (1 << round_page_32(neweofoff)/PAGE_SIZE) - 1; | |
1938 | if (bp->nb_dirty) | |
55e303ae | 1939 | mustwrite++; |
91447636 A |
1940 | if (!mustwrite) { |
1941 | FSDBG(512, bp, bp->nb_flags, 0, obn); | |
1942 | SET(bp->nb_flags, NB_INVAL); | |
1943 | nfs_buf_release(bp, 1); | |
1944 | continue; | |
1945 | } | |
1946 | /* gotta write out dirty data before invalidating */ | |
1947 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
1948 | /* (NB_NOCACHE indicates buffer should be discarded) */ | |
1949 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL | NB_ASYNC | NB_READ)); | |
1950 | SET(bp->nb_flags, NB_STABLE | NB_NOCACHE); | |
0c530ab8 | 1951 | if (!IS_VALID_CRED(bp->nb_wcred)) { |
2d21ac55 | 1952 | kauth_cred_t cred = vfs_context_ucred(ctx); |
91447636 A |
1953 | kauth_cred_ref(cred); |
1954 | bp->nb_wcred = cred; | |
1955 | } | |
1956 | error = nfs_buf_write(bp); | |
1957 | // Note: bp has been released | |
1958 | if (error) { | |
1959 | FSDBG(512, bp, 0xd00dee, 0xbad, error); | |
b0d623f7 | 1960 | nfs_node_lock_force(np); |
91447636 A |
1961 | np->n_error = error; |
1962 | np->n_flag |= NWRITEERR; | |
55e303ae | 1963 | /* |
91447636 A |
1964 | * There was a write error and we need to |
1965 | * invalidate attrs and flush buffers in | |
1966 | * order to sync up with the server. | |
1967 | * (if this write was extending the file, | |
1968 | * we may no longer know the correct size) | |
55e303ae | 1969 | */ |
91447636 | 1970 | NATTRINVALIDATE(np); |
b0d623f7 | 1971 | nfs_node_unlock(np); |
2d21ac55 A |
1972 | nfs_data_unlock(np); |
1973 | nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1); | |
b0d623f7 | 1974 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
91447636 | 1975 | error = 0; |
fa4905b1 | 1976 | } |
91447636 | 1977 | } |
1c79356b | 1978 | } |
2d21ac55 A |
1979 | if (vap->va_data_size != np->n_size) |
1980 | ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */ | |
1981 | origsize = np->n_size; | |
91447636 | 1982 | np->n_size = np->n_vattr.nva_size = vap->va_data_size; |
b0d623f7 | 1983 | nfs_node_lock_force(np); |
2d21ac55 | 1984 | CLR(np->n_flag, NUPDATESIZE); |
b0d623f7 | 1985 | nfs_node_unlock(np); |
2d21ac55 A |
1986 | FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); |
1987 | } | |
1988 | } else if (VATTR_IS_ACTIVE(vap, va_modify_time) || | |
1989 | VATTR_IS_ACTIVE(vap, va_access_time) || | |
1990 | (vap->va_vaflags & VA_UTIMES_NULL)) { | |
b0d623f7 | 1991 | if ((error = nfs_node_lock(np))) |
55e303ae | 1992 | return (error); |
2d21ac55 | 1993 | if ((np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) { |
b0d623f7 | 1994 | nfs_node_unlock(np); |
2d21ac55 A |
1995 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
1996 | if (error == EINTR) | |
1997 | return (error); | |
1998 | } else { | |
b0d623f7 | 1999 | nfs_node_unlock(np); |
2d21ac55 | 2000 | } |
55e303ae | 2001 | } |
6d2010ae A |
2002 | if ((VATTR_IS_ACTIVE(vap, va_mode) || VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) || |
2003 | VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid)) && | |
2004 | !(error = nfs_node_lock(np))) { | |
2005 | NACCESSINVALIDATE(np); | |
b0d623f7 | 2006 | nfs_node_unlock(np); |
6d2010ae A |
2007 | if (!namedattrs) { |
2008 | dvp = vnode_getparent(vp); | |
2009 | vname = vnode_getname(vp); | |
2010 | dnp = (dvp && vname) ? VTONFS(dvp) : NULL; | |
2011 | if (dnp) { | |
813fb2f6 A |
2012 | if (nfs_node_set_busy(dnp, vfs_context_thread(ctx))) { |
2013 | vnode_put(dvp); | |
2014 | vnode_putname(vname); | |
2015 | } else { | |
2016 | nfs_dulookup_init(&dul, dnp, vname, strlen(vname), ctx); | |
2017 | nfs_dulookup_start(&dul, dnp, ctx); | |
2018 | dul_in_progress = 1; | |
6d2010ae | 2019 | } |
813fb2f6 A |
2020 | } else { |
2021 | if (dvp) | |
2022 | vnode_put(dvp); | |
2023 | if (vname) | |
2024 | vnode_putname(vname); | |
2d21ac55 | 2025 | } |
2d21ac55 A |
2026 | } |
2027 | } | |
2028 | ||
6d2010ae | 2029 | if (!error) |
b0d623f7 A |
2030 | error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx); |
2031 | ||
813fb2f6 A |
2032 | if (dul_in_progress) { |
2033 | nfs_dulookup_finish(&dul, dnp, ctx); | |
2034 | nfs_node_clear_busy(dnp); | |
2035 | vnode_put(dvp); | |
2036 | vnode_putname(vname); | |
91447636 | 2037 | } |
1c79356b | 2038 | |
2d21ac55 A |
2039 | FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error); |
2040 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { | |
6d2010ae A |
2041 | if (error && (origsize != np->n_size) && |
2042 | ((nfsvers < NFS_VER4) || !nfs_mount_state_error_should_restart(error))) { | |
2d21ac55 | 2043 | /* make every effort to resync file size w/ server... */ |
6d2010ae | 2044 | /* (don't bother if we'll be restarting the operation) */ |
2d21ac55 A |
2045 | int err; /* preserve "error" for return */ |
2046 | np->n_size = np->n_vattr.nva_size = origsize; | |
b0d623f7 | 2047 | nfs_node_lock_force(np); |
2d21ac55 | 2048 | CLR(np->n_flag, NUPDATESIZE); |
b0d623f7 | 2049 | nfs_node_unlock(np); |
2d21ac55 A |
2050 | FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002); |
2051 | ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */ | |
6d2010ae | 2052 | vapsize = vap->va_data_size; |
2d21ac55 | 2053 | vap->va_data_size = origsize; |
b0d623f7 | 2054 | err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx); |
2d21ac55 | 2055 | if (err) |
6d2010ae A |
2056 | NP(np, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers, error, err); |
2057 | vap->va_data_size = vapsize; | |
2d21ac55 | 2058 | } |
b0d623f7 A |
2059 | nfs_node_lock_force(np); |
2060 | /* | |
2061 | * The size was just set. If the size is already marked for update, don't | |
2062 | * trust the newsize (it may have been set while the setattr was in progress). | |
2063 | * Clear the update flag and make sure we fetch new attributes so we are sure | |
2064 | * we have the latest size. | |
2065 | */ | |
2066 | if (ISSET(np->n_flag, NUPDATESIZE)) { | |
2067 | CLR(np->n_flag, NUPDATESIZE); | |
2068 | NATTRINVALIDATE(np); | |
2069 | nfs_node_unlock(np); | |
6d2010ae | 2070 | nfs_getattr(np, NULL, ctx, NGA_UNCACHED); |
b0d623f7 A |
2071 | } else { |
2072 | nfs_node_unlock(np); | |
2073 | } | |
2d21ac55 | 2074 | nfs_data_unlock(np); |
b0d623f7 | 2075 | if (nfsvers >= NFS_VER4) { |
6d2010ae A |
2076 | if (nofp) { |
2077 | /* don't close our setattr open if we'll be restarting... */ | |
2078 | if (!nfs_mount_state_error_should_restart(error) && | |
2079 | (nofp->nof_flags & NFS_OPEN_FILE_SETATTR)) { | |
2080 | int err = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx); | |
2081 | if (err) | |
2082 | NP(np, "nfs_vnop_setattr: close error: %d", err); | |
2083 | nofp->nof_flags &= ~NFS_OPEN_FILE_SETATTR; | |
b0d623f7 | 2084 | } |
b0d623f7 | 2085 | nfs_open_file_clear_busy(nofp); |
6d2010ae | 2086 | nofp = NULL; |
b0d623f7 | 2087 | } |
6d2010ae A |
2088 | if (nfs_mount_state_in_use_end(nmp, error)) |
2089 | goto restart; | |
b0d623f7 A |
2090 | nfs_open_owner_rele(noop); |
2091 | } | |
1c79356b A |
2092 | } |
2093 | return (error); | |
2094 | } | |
2095 | ||
2096 | /* | |
2d21ac55 | 2097 | * Do an NFS setattr RPC. |
1c79356b | 2098 | */ |
2d21ac55 A |
2099 | int |
2100 | nfs3_setattr_rpc( | |
2101 | nfsnode_t np, | |
2102 | struct vnode_attr *vap, | |
b0d623f7 | 2103 | vfs_context_t ctx) |
1c79356b | 2104 | { |
2d21ac55 A |
2105 | struct nfsmount *nmp = NFSTONMP(np); |
2106 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0, nfsvers; | |
b0d623f7 | 2107 | u_int64_t xid, nextxid; |
2d21ac55 | 2108 | struct nfsm_chain nmreq, nmrep; |
55e303ae | 2109 | |
fe8ab488 | 2110 | if (nfs_mount_gone(nmp)) |
55e303ae | 2111 | return (ENXIO); |
2d21ac55 | 2112 | nfsvers = nmp->nm_vers; |
1c79356b | 2113 | |
2d21ac55 A |
2114 | VATTR_SET_SUPPORTED(vap, va_mode); |
2115 | VATTR_SET_SUPPORTED(vap, va_uid); | |
2116 | VATTR_SET_SUPPORTED(vap, va_gid); | |
2117 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
2118 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
2119 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
2120 | ||
2121 | if (VATTR_IS_ACTIVE(vap, va_flags)) { | |
2122 | if (vap->va_flags) { /* we don't support setting flags */ | |
2123 | if (vap->va_active & ~VNODE_ATTR_va_flags) | |
2124 | return (EINVAL); /* return EINVAL if other attributes also set */ | |
2125 | else | |
2126 | return (ENOTSUP); /* return ENOTSUP for chflags(2) */ | |
2127 | } | |
2128 | /* no flags set, so we'll just ignore it */ | |
2129 | if (!(vap->va_active & ~VNODE_ATTR_va_flags)) | |
2130 | return (0); /* no (other) attributes to set, so nothing to do */ | |
2131 | } | |
2132 | ||
2133 | nfsm_chain_null(&nmreq); | |
2134 | nfsm_chain_null(&nmrep); | |
2135 | ||
2136 | nfsm_chain_build_alloc_init(error, &nmreq, | |
2137 | NFSX_FH(nfsvers) + NFSX_SATTR(nfsvers)); | |
2138 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
2139 | if (nfsvers == NFS_VER3) { | |
91447636 | 2140 | if (VATTR_IS_ACTIVE(vap, va_mode)) { |
2d21ac55 A |
2141 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2142 | nfsm_chain_add_32(error, &nmreq, vap->va_mode); | |
1c79356b | 2143 | } else { |
2d21ac55 | 2144 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2145 | } |
91447636 | 2146 | if (VATTR_IS_ACTIVE(vap, va_uid)) { |
2d21ac55 A |
2147 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2148 | nfsm_chain_add_32(error, &nmreq, vap->va_uid); | |
1c79356b | 2149 | } else { |
2d21ac55 | 2150 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2151 | } |
91447636 | 2152 | if (VATTR_IS_ACTIVE(vap, va_gid)) { |
2d21ac55 A |
2153 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2154 | nfsm_chain_add_32(error, &nmreq, vap->va_gid); | |
1c79356b | 2155 | } else { |
2d21ac55 | 2156 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2157 | } |
91447636 | 2158 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { |
2d21ac55 A |
2159 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2160 | nfsm_chain_add_64(error, &nmreq, vap->va_data_size); | |
1c79356b | 2161 | } else { |
2d21ac55 | 2162 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2163 | } |
2d21ac55 A |
2164 | if (vap->va_vaflags & VA_UTIMES_NULL) { |
2165 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER); | |
2166 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER); | |
2167 | } else { | |
2168 | if (VATTR_IS_ACTIVE(vap, va_access_time)) { | |
2169 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT); | |
2170 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec); | |
2171 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_nsec); | |
1c79356b | 2172 | } else { |
2d21ac55 | 2173 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE); |
1c79356b | 2174 | } |
2d21ac55 A |
2175 | if (VATTR_IS_ACTIVE(vap, va_modify_time)) { |
2176 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT); | |
2177 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec); | |
2178 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_nsec); | |
1c79356b | 2179 | } else { |
2d21ac55 | 2180 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE); |
1c79356b | 2181 | } |
1c79356b | 2182 | } |
2d21ac55 | 2183 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2184 | } else { |
2d21ac55 A |
2185 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_mode) ? |
2186 | vtonfsv2_mode(vnode_vtype(NFSTOV(np)), vap->va_mode) : -1); | |
2187 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_uid) ? | |
2188 | vap->va_uid : (uint32_t)-1); | |
2189 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_gid) ? | |
2190 | vap->va_gid : (uint32_t)-1); | |
2191 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_data_size) ? | |
2192 | vap->va_data_size : (uint32_t)-1); | |
91447636 | 2193 | if (VATTR_IS_ACTIVE(vap, va_access_time)) { |
2d21ac55 A |
2194 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec); |
2195 | nfsm_chain_add_32(error, &nmreq, (vap->va_access_time.tv_nsec != -1) ? | |
2196 | ((uint32_t)vap->va_access_time.tv_nsec / 1000) : 0xffffffff); | |
91447636 | 2197 | } else { |
2d21ac55 A |
2198 | nfsm_chain_add_32(error, &nmreq, -1); |
2199 | nfsm_chain_add_32(error, &nmreq, -1); | |
91447636 A |
2200 | } |
2201 | if (VATTR_IS_ACTIVE(vap, va_modify_time)) { | |
2d21ac55 A |
2202 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec); |
2203 | nfsm_chain_add_32(error, &nmreq, (vap->va_modify_time.tv_nsec != -1) ? | |
2204 | ((uint32_t)vap->va_modify_time.tv_nsec / 1000) : 0xffffffff); | |
91447636 | 2205 | } else { |
2d21ac55 A |
2206 | nfsm_chain_add_32(error, &nmreq, -1); |
2207 | nfsm_chain_add_32(error, &nmreq, -1); | |
91447636 | 2208 | } |
1c79356b | 2209 | } |
2d21ac55 A |
2210 | nfsm_chain_build_done(error, &nmreq); |
2211 | nfsmout_if(error); | |
6d2010ae | 2212 | error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx, NULL, &nmrep, &xid, &status); |
b0d623f7 | 2213 | if ((lockerror = nfs_node_lock(np))) |
2d21ac55 A |
2214 | error = lockerror; |
2215 | if (nfsvers == NFS_VER3) { | |
91447636 | 2216 | struct timespec premtime = { 0, 0 }; |
2d21ac55 A |
2217 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); |
2218 | nfsmout_if(error); | |
483a1d10 | 2219 | /* if file hadn't changed, update cached mtime */ |
2d21ac55 A |
2220 | if (nfstimespeccmp(&np->n_mtime, &premtime, ==)) |
2221 | NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr); | |
483a1d10 | 2222 | /* if directory hadn't changed, update namecache mtime */ |
2d21ac55 A |
2223 | if ((vnode_vtype(NFSTOV(np)) == VDIR) && |
2224 | nfstimespeccmp(&np->n_ncmtime, &premtime, ==)) | |
2225 | NFS_CHANGED_UPDATE_NC(nfsvers, np, &np->n_vattr); | |
483a1d10 | 2226 | if (!wccpostattr) |
2d21ac55 A |
2227 | NATTRINVALIDATE(np); |
2228 | error = status; | |
e5568f75 | 2229 | } else { |
2d21ac55 A |
2230 | if (!error) |
2231 | error = status; | |
6d2010ae | 2232 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
e5568f75 | 2233 | } |
b0d623f7 A |
2234 | /* |
2235 | * We just changed the attributes and we want to make sure that we | |
2236 | * see the latest attributes. Get the next XID. If it's not the | |
2237 | * next XID after the SETATTR XID, then it's possible that another | |
2238 | * RPC was in flight at the same time and it might put stale attributes | |
2239 | * in the cache. In that case, we invalidate the attributes and set | |
2240 | * the attribute cache XID to guarantee that newer attributes will | |
2241 | * get loaded next. | |
2242 | */ | |
2243 | nextxid = 0; | |
2244 | nfs_get_xid(&nextxid); | |
2245 | if (nextxid != (xid + 1)) { | |
2246 | np->n_xid = nextxid; | |
2247 | NATTRINVALIDATE(np); | |
2248 | } | |
2d21ac55 | 2249 | nfsmout: |
b0d623f7 A |
2250 | if (!lockerror) |
2251 | nfs_node_unlock(np); | |
2d21ac55 A |
2252 | nfsm_chain_cleanup(&nmreq); |
2253 | nfsm_chain_cleanup(&nmrep); | |
1c79356b A |
2254 | return (error); |
2255 | } | |
2256 | ||
2257 | /* | |
2d21ac55 | 2258 | * NFS lookup call, one step at a time... |
1c79356b | 2259 | * First look in cache |
2d21ac55 | 2260 | * If not found, unlock the directory nfsnode and do the RPC |
1c79356b | 2261 | */ |
b0d623f7 | 2262 | int |
2d21ac55 | 2263 | nfs_vnop_lookup( |
91447636 | 2264 | struct vnop_lookup_args /* { |
1c79356b | 2265 | struct vnodeop_desc *a_desc; |
91447636 A |
2266 | vnode_t a_dvp; |
2267 | vnode_t *a_vpp; | |
1c79356b | 2268 | struct componentname *a_cnp; |
91447636 | 2269 | vfs_context_t a_context; |
2d21ac55 | 2270 | } */ *ap) |
1c79356b | 2271 | { |
2d21ac55 | 2272 | vfs_context_t ctx = ap->a_context; |
91447636 A |
2273 | struct componentname *cnp = ap->a_cnp; |
2274 | vnode_t dvp = ap->a_dvp; | |
2275 | vnode_t *vpp = ap->a_vpp; | |
2276 | int flags = cnp->cn_flags; | |
2277 | vnode_t newvp; | |
2d21ac55 A |
2278 | nfsnode_t dnp, np; |
2279 | struct nfsmount *nmp; | |
2280 | mount_t mp; | |
b0d623f7 | 2281 | int nfsvers, error, busyerror = ENOENT, isdot, isdotdot, negnamecache; |
2d21ac55 | 2282 | u_int64_t xid; |
91447636 | 2283 | struct nfs_vattr nvattr; |
91447636 | 2284 | int ngflags; |
2d21ac55 A |
2285 | struct vnop_access_args naa; |
2286 | fhandle_t fh; | |
2287 | struct nfsreq rq, *req = &rq; | |
1c79356b | 2288 | |
1c79356b | 2289 | *vpp = NULLVP; |
55e303ae | 2290 | |
91447636 | 2291 | dnp = VTONFS(dvp); |
6d2010ae | 2292 | NVATTR_INIT(&nvattr); |
fa4905b1 | 2293 | |
2d21ac55 A |
2294 | mp = vnode_mount(dvp); |
2295 | nmp = VFSTONFS(mp); | |
fe8ab488 | 2296 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2297 | error = ENXIO; |
2298 | goto error_return; | |
2299 | } | |
2300 | nfsvers = nmp->nm_vers; | |
6d2010ae | 2301 | negnamecache = !NMFLAG(nmp, NONEGNAMECACHE); |
2d21ac55 | 2302 | |
b0d623f7 A |
2303 | if ((error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)))) |
2304 | goto error_return; | |
2305 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 2306 | if ((error = nfs_getattr(dnp, NULL, ctx, NGA_CACHED))) |
91447636 | 2307 | goto error_return; |
1c79356b | 2308 | |
91447636 A |
2309 | error = cache_lookup(dvp, vpp, cnp); |
2310 | switch (error) { | |
2311 | case ENOENT: | |
2d21ac55 A |
2312 | /* negative cache entry */ |
2313 | goto error_return; | |
91447636 A |
2314 | case 0: |
2315 | /* cache miss */ | |
6d2010ae | 2316 | if ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { |
b0d623f7 A |
2317 | /* if rdirplus, try dir buf cache lookup */ |
2318 | error = nfs_dir_buf_cache_lookup(dnp, &np, cnp, ctx, 0); | |
2319 | if (!error && np) { | |
2320 | /* dir buf cache hit */ | |
2321 | *vpp = NFSTOV(np); | |
2322 | error = -1; | |
2323 | } | |
2324 | } | |
2325 | if (error != -1) /* cache miss */ | |
2326 | break; | |
2327 | /* FALLTHROUGH */ | |
91447636 A |
2328 | case -1: |
2329 | /* cache hit, not really an error */ | |
316670eb | 2330 | OSAddAtomic64(1, &nfsstats.lookupcache_hits); |
91447636 | 2331 | |
b0d623f7 | 2332 | nfs_node_clear_busy(dnp); |
6d2010ae | 2333 | busyerror = ENOENT; |
2d21ac55 | 2334 | |
91447636 | 2335 | /* check for directory access */ |
6d2010ae | 2336 | naa.a_desc = &vnop_access_desc; |
91447636 A |
2337 | naa.a_vp = dvp; |
2338 | naa.a_action = KAUTH_VNODE_SEARCH; | |
2d21ac55 | 2339 | naa.a_context = ctx; |
91447636 A |
2340 | |
2341 | /* compute actual success/failure based on accessibility */ | |
2d21ac55 | 2342 | error = nfs_vnop_access(&naa); |
91447636 A |
2343 | /* FALLTHROUGH */ |
2344 | default: | |
2345 | /* unexpected error from cache_lookup */ | |
2346 | goto error_return; | |
2347 | } | |
2d21ac55 A |
2348 | |
2349 | /* skip lookup, if we know who we are: "." or ".." */ | |
2350 | isdot = isdotdot = 0; | |
2351 | if (cnp->cn_nameptr[0] == '.') { | |
2352 | if (cnp->cn_namelen == 1) | |
2353 | isdot = 1; | |
2354 | if ((cnp->cn_namelen == 2) && (cnp->cn_nameptr[1] == '.')) | |
2355 | isdotdot = 1; | |
2356 | } | |
2357 | if (isdotdot || isdot) { | |
2358 | fh.fh_len = 0; | |
91447636 A |
2359 | goto found; |
2360 | } | |
6d2010ae A |
2361 | if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) { |
2362 | /* we should never be looking things up in a trigger directory, return nothing */ | |
2363 | error = ENOENT; | |
2364 | goto error_return; | |
2365 | } | |
55e303ae | 2366 | |
91447636 | 2367 | /* do we know this name is too long? */ |
2d21ac55 | 2368 | nmp = VTONMP(dvp); |
fe8ab488 | 2369 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2370 | error = ENXIO; |
2371 | goto error_return; | |
2372 | } | |
2373 | if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) && | |
b0d623f7 | 2374 | (cnp->cn_namelen > (int)nmp->nm_fsattr.nfsa_maxname)) { |
91447636 A |
2375 | error = ENAMETOOLONG; |
2376 | goto error_return; | |
1c79356b | 2377 | } |
fa4905b1 | 2378 | |
1c79356b A |
2379 | error = 0; |
2380 | newvp = NULLVP; | |
91447636 | 2381 | |
316670eb | 2382 | OSAddAtomic64(1, &nfsstats.lookupcache_misses); |
1c79356b | 2383 | |
2d21ac55 A |
2384 | error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &req); |
2385 | nfsmout_if(error); | |
6d2010ae | 2386 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, req, &xid, &fh, &nvattr); |
2d21ac55 | 2387 | nfsmout_if(error); |
91447636 | 2388 | |
91447636 | 2389 | /* is the file handle the same as this directory's file handle? */ |
2d21ac55 A |
2390 | isdot = NFS_CMPFH(dnp, fh.fh_data, fh.fh_len); |
2391 | ||
2392 | found: | |
2d21ac55 A |
2393 | if (flags & ISLASTCN) { |
2394 | switch (cnp->cn_nameiop) { | |
2395 | case DELETE: | |
2396 | cnp->cn_flags &= ~MAKEENTRY; | |
2397 | break; | |
2398 | case RENAME: | |
2399 | cnp->cn_flags &= ~MAKEENTRY; | |
2400 | if (isdot) { | |
2401 | error = EISDIR; | |
91447636 A |
2402 | goto error_return; |
2403 | } | |
2d21ac55 | 2404 | break; |
91447636 | 2405 | } |
91447636 A |
2406 | } |
2407 | ||
2d21ac55 | 2408 | if (isdotdot) { |
2d21ac55 A |
2409 | newvp = vnode_getparent(dvp); |
2410 | if (!newvp) { | |
2411 | error = ENOENT; | |
0b4e3aa0 | 2412 | goto error_return; |
1c79356b | 2413 | } |
2d21ac55 | 2414 | } else if (isdot) { |
91447636 | 2415 | error = vnode_get(dvp); |
2d21ac55 | 2416 | if (error) |
0b4e3aa0 | 2417 | goto error_return; |
91447636 | 2418 | newvp = dvp; |
b0d623f7 | 2419 | nfs_node_lock_force(dnp); |
2d21ac55 A |
2420 | if (fh.fh_len && (dnp->n_xid <= xid)) |
2421 | nfs_loadattrcache(dnp, &nvattr, &xid, 0); | |
b0d623f7 | 2422 | nfs_node_unlock(dnp); |
1c79356b | 2423 | } else { |
2d21ac55 | 2424 | ngflags = (cnp->cn_flags & MAKEENTRY) ? NG_MAKEENTRY : 0; |
6d2010ae | 2425 | error = nfs_nget(mp, dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, ngflags, &np); |
2d21ac55 | 2426 | if (error) |
0b4e3aa0 | 2427 | goto error_return; |
1c79356b | 2428 | newvp = NFSTOV(np); |
b0d623f7 | 2429 | nfs_node_unlock(np); |
1c79356b | 2430 | } |
1c79356b | 2431 | *vpp = newvp; |
91447636 | 2432 | |
2d21ac55 | 2433 | nfsmout: |
1c79356b | 2434 | if (error) { |
2d21ac55 A |
2435 | if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) && |
2436 | (flags & ISLASTCN) && (error == ENOENT)) { | |
91447636 | 2437 | if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp)) |
1c79356b A |
2438 | error = EROFS; |
2439 | else | |
2440 | error = EJUSTRETURN; | |
2441 | } | |
1c79356b | 2442 | } |
2d21ac55 A |
2443 | if ((error == ENOENT) && (cnp->cn_flags & MAKEENTRY) && |
2444 | (cnp->cn_nameiop != CREATE) && negnamecache) { | |
2445 | /* add a negative entry in the name cache */ | |
b0d623f7 | 2446 | nfs_node_lock_force(dnp); |
2d21ac55 A |
2447 | cache_enter(dvp, NULL, cnp); |
2448 | dnp->n_flag |= NNEGNCENTRIES; | |
b0d623f7 | 2449 | nfs_node_unlock(dnp); |
2d21ac55 | 2450 | } |
0b4e3aa0 | 2451 | error_return: |
6d2010ae | 2452 | NVATTR_CLEANUP(&nvattr); |
b0d623f7 A |
2453 | if (!busyerror) |
2454 | nfs_node_clear_busy(dnp); | |
91447636 A |
2455 | if (error && *vpp) { |
2456 | vnode_put(*vpp); | |
2457 | *vpp = NULLVP; | |
2458 | } | |
1c79356b A |
2459 | return (error); |
2460 | } | |
2461 | ||
fe8ab488 A |
2462 | int nfs_readlink_nocache = DEFAULT_READLINK_NOCACHE; |
2463 | ||
1c79356b | 2464 | /* |
2d21ac55 | 2465 | * NFS readlink call |
1c79356b | 2466 | */ |
b0d623f7 | 2467 | int |
2d21ac55 | 2468 | nfs_vnop_readlink( |
91447636 A |
2469 | struct vnop_readlink_args /* { |
2470 | struct vnodeop_desc *a_desc; | |
2471 | vnode_t a_vp; | |
1c79356b | 2472 | struct uio *a_uio; |
91447636 | 2473 | vfs_context_t a_context; |
2d21ac55 | 2474 | } */ *ap) |
1c79356b | 2475 | { |
2d21ac55 A |
2476 | vfs_context_t ctx = ap->a_context; |
2477 | nfsnode_t np = VTONFS(ap->a_vp); | |
2478 | struct nfsmount *nmp; | |
b0d623f7 | 2479 | int error = 0, nfsvers; |
2d21ac55 | 2480 | uint32_t buflen; |
b0d623f7 | 2481 | uio_t uio = ap->a_uio; |
2d21ac55 | 2482 | struct nfsbuf *bp = NULL; |
fe8ab488 A |
2483 | struct timespec ts; |
2484 | int timeo; | |
2d21ac55 A |
2485 | |
2486 | if (vnode_vtype(ap->a_vp) != VLNK) | |
1c79356b | 2487 | return (EPERM); |
2d21ac55 | 2488 | |
b0d623f7 | 2489 | if (uio_resid(uio) == 0) |
2d21ac55 | 2490 | return (0); |
b0d623f7 | 2491 | if (uio_offset(uio) < 0) |
2d21ac55 A |
2492 | return (EINVAL); |
2493 | ||
2494 | nmp = VTONMP(ap->a_vp); | |
fe8ab488 | 2495 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
2496 | return (ENXIO); |
2497 | nfsvers = nmp->nm_vers; | |
2498 | ||
fe8ab488 | 2499 | |
b0d623f7 | 2500 | /* nfs_getattr() will check changed and purge caches */ |
fe8ab488 | 2501 | if ((error = nfs_getattr(np, NULL, ctx, nfs_readlink_nocache ? NGA_UNCACHED : NGA_CACHED))) { |
2d21ac55 A |
2502 | FSDBG(531, np, 0xd1e0001, 0, error); |
2503 | return (error); | |
2504 | } | |
2d21ac55 | 2505 | |
fe8ab488 A |
2506 | if (nfs_readlink_nocache) { |
2507 | timeo = nfs_attrcachetimeout(np); | |
2508 | nanouptime(&ts); | |
2509 | } | |
2510 | ||
2511 | retry: | |
316670eb | 2512 | OSAddAtomic64(1, &nfsstats.biocache_readlinks); |
fe8ab488 | 2513 | error = nfs_buf_get(np, 0, NFS_MAXPATHLEN, vfs_context_thread(ctx), NBLK_META, &bp); |
2d21ac55 A |
2514 | if (error) { |
2515 | FSDBG(531, np, 0xd1e0002, 0, error); | |
2516 | return (error); | |
2517 | } | |
fe8ab488 A |
2518 | |
2519 | if (nfs_readlink_nocache) { | |
2520 | NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo, ts.tv_sec, | |
2521 | (np->n_rltim.tv_sec + timeo) < ts.tv_sec || nfs_readlink_nocache > 1, | |
2522 | ISSET(bp->nb_flags, NB_CACHE) == NB_CACHE); | |
2523 | /* n_rltim is synchronized by the associated nfs buf */ | |
2524 | if (ISSET(bp->nb_flags, NB_CACHE) && ((nfs_readlink_nocache > 1) || ((np->n_rltim.tv_sec + timeo) < ts.tv_sec))) { | |
2525 | SET(bp->nb_flags, NB_INVAL); | |
2526 | nfs_buf_release(bp, 0); | |
2527 | goto retry; | |
2528 | } | |
2529 | } | |
2d21ac55 | 2530 | if (!ISSET(bp->nb_flags, NB_CACHE)) { |
fe8ab488 | 2531 | readagain: |
316670eb | 2532 | OSAddAtomic64(1, &nfsstats.readlink_bios); |
2d21ac55 A |
2533 | buflen = bp->nb_bufsize; |
2534 | error = nmp->nm_funcs->nf_readlink_rpc(np, bp->nb_data, &buflen, ctx); | |
2535 | if (error) { | |
fe8ab488 A |
2536 | if (error == ESTALE) { |
2537 | NFS_VNOP_DBG("Stale FH from readlink rpc\n"); | |
2538 | error = nfs_refresh_fh(np, ctx); | |
2539 | if (error == 0) | |
2540 | goto readagain; | |
2541 | } | |
2d21ac55 A |
2542 | SET(bp->nb_flags, NB_ERROR); |
2543 | bp->nb_error = error; | |
fe8ab488 | 2544 | NFS_VNOP_DBG("readlink failed %d\n", error); |
2d21ac55 A |
2545 | } else { |
2546 | bp->nb_validoff = 0; | |
2547 | bp->nb_validend = buflen; | |
fe8ab488 A |
2548 | np->n_rltim = ts; |
2549 | NFS_VNOP_DBG("readlink of %.*s\n", bp->nb_validend, (char *)bp->nb_data); | |
2d21ac55 | 2550 | } |
fe8ab488 A |
2551 | } else { |
2552 | NFS_VNOP_DBG("got cached link of %.*s\n", bp->nb_validend, (char *)bp->nb_data); | |
2d21ac55 | 2553 | } |
fe8ab488 | 2554 | |
b0d623f7 A |
2555 | if (!error && (bp->nb_validend > 0)) |
2556 | error = uiomove(bp->nb_data, bp->nb_validend, uio); | |
2d21ac55 A |
2557 | FSDBG(531, np, bp->nb_validend, 0, error); |
2558 | nfs_buf_release(bp, 1); | |
2559 | return (error); | |
1c79356b A |
2560 | } |
2561 | ||
2562 | /* | |
2d21ac55 | 2563 | * Do a readlink RPC. |
1c79356b A |
2564 | */ |
2565 | int | |
2d21ac55 | 2566 | nfs3_readlink_rpc(nfsnode_t np, char *buf, uint32_t *buflenp, vfs_context_t ctx) |
1c79356b | 2567 | { |
2d21ac55 A |
2568 | struct nfsmount *nmp; |
2569 | int error = 0, lockerror = ENOENT, nfsvers, status; | |
2570 | uint32_t len; | |
fa4905b1 | 2571 | u_int64_t xid; |
2d21ac55 | 2572 | struct nfsm_chain nmreq, nmrep; |
1c79356b | 2573 | |
2d21ac55 | 2574 | nmp = NFSTONMP(np); |
fe8ab488 | 2575 | if (nfs_mount_gone(nmp)) |
55e303ae | 2576 | return (ENXIO); |
2d21ac55 A |
2577 | nfsvers = nmp->nm_vers; |
2578 | nfsm_chain_null(&nmreq); | |
2579 | nfsm_chain_null(&nmrep); | |
2580 | ||
2581 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers)); | |
2582 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
2583 | nfsm_chain_build_done(error, &nmreq); | |
2584 | nfsmout_if(error); | |
6d2010ae | 2585 | error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx, NULL, &nmrep, &xid, &status); |
b0d623f7 | 2586 | if ((lockerror = nfs_node_lock(np))) |
2d21ac55 A |
2587 | error = lockerror; |
2588 | if (nfsvers == NFS_VER3) | |
2589 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); | |
2590 | if (!error) | |
2591 | error = status; | |
2592 | nfsm_chain_get_32(error, &nmrep, len); | |
2593 | nfsmout_if(error); | |
2594 | if ((nfsvers == NFS_VER2) && (len > *buflenp)) { | |
2595 | error = EBADRPC; | |
2596 | goto nfsmout; | |
2597 | } | |
2598 | if (len >= *buflenp) { | |
2599 | if (np->n_size && (np->n_size < *buflenp)) | |
2600 | len = np->n_size; | |
2601 | else | |
2602 | len = *buflenp - 1; | |
2603 | } | |
2604 | nfsm_chain_get_opaque(error, &nmrep, len, buf); | |
2605 | if (!error) | |
2606 | *buflenp = len; | |
2607 | nfsmout: | |
2608 | if (!lockerror) | |
b0d623f7 | 2609 | nfs_node_unlock(np); |
2d21ac55 A |
2610 | nfsm_chain_cleanup(&nmreq); |
2611 | nfsm_chain_cleanup(&nmrep); | |
1c79356b A |
2612 | return (error); |
2613 | } | |
2614 | ||
2615 | /* | |
2d21ac55 | 2616 | * NFS read RPC call |
1c79356b A |
2617 | * Ditto above |
2618 | */ | |
2619 | int | |
b0d623f7 | 2620 | nfs_read_rpc(nfsnode_t np, uio_t uio, vfs_context_t ctx) |
1c79356b | 2621 | { |
1c79356b | 2622 | struct nfsmount *nmp; |
2d21ac55 | 2623 | int error = 0, nfsvers, eof = 0; |
b0d623f7 A |
2624 | size_t nmrsize, len, retlen; |
2625 | user_ssize_t tsiz; | |
2d21ac55 A |
2626 | off_t txoffset; |
2627 | struct nfsreq rq, *req = &rq; | |
b0d623f7 | 2628 | uint32_t stategenid = 0, restart = 0; |
1c79356b | 2629 | |
b0d623f7 | 2630 | FSDBG_TOP(536, np, uio_offset(uio), uio_resid(uio), 0); |
2d21ac55 | 2631 | nmp = NFSTONMP(np); |
fe8ab488 | 2632 | if (nfs_mount_gone(nmp)) |
55e303ae | 2633 | return (ENXIO); |
2d21ac55 | 2634 | nfsvers = nmp->nm_vers; |
55e303ae A |
2635 | nmrsize = nmp->nm_rsize; |
2636 | ||
b0d623f7 A |
2637 | txoffset = uio_offset(uio); |
2638 | tsiz = uio_resid(uio); | |
2639 | if ((nfsvers == NFS_VER2) && ((uint64_t)(txoffset + tsiz) > 0xffffffffULL)) { | |
2640 | FSDBG_BOT(536, np, uio_offset(uio), uio_resid(uio), EFBIG); | |
1c79356b | 2641 | return (EFBIG); |
55e303ae | 2642 | } |
2d21ac55 | 2643 | |
1c79356b | 2644 | while (tsiz > 0) { |
b0d623f7 | 2645 | len = retlen = (tsiz > (user_ssize_t)nmrsize) ? nmrsize : (size_t)tsiz; |
2d21ac55 | 2646 | FSDBG(536, np, txoffset, len, 0); |
6d2010ae A |
2647 | if (np->n_flag & NREVOKE) { |
2648 | error = EIO; | |
2649 | break; | |
2650 | } | |
b0d623f7 A |
2651 | if (nmp->nm_vers >= NFS_VER4) |
2652 | stategenid = nmp->nm_stategenid; | |
2d21ac55 A |
2653 | error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, len, |
2654 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req); | |
2655 | if (!error) | |
b0d623f7 A |
2656 | error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req, uio, &retlen, &eof); |
2657 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) && | |
2658 | (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */ | |
2659 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
2660 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
2661 | NP(np, "nfs_read_rpc: error %d, initiating recovery", error); | |
2662 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
2663 | } |
2664 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae A |
2665 | if (np->n_flag & NREVOKE) { |
2666 | error = EIO; | |
2667 | } else { | |
2668 | if (error == NFSERR_GRACE) | |
2669 | tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz); | |
2670 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) | |
2671 | continue; | |
2672 | } | |
b0d623f7 | 2673 | } |
91447636 A |
2674 | if (error) |
2675 | break; | |
2d21ac55 A |
2676 | txoffset += retlen; |
2677 | tsiz -= retlen; | |
2678 | if (nfsvers != NFS_VER2) { | |
2679 | if (eof || (retlen == 0)) | |
2680 | tsiz = 0; | |
2681 | } else if (retlen < len) | |
2682 | tsiz = 0; | |
2683 | } | |
2684 | ||
b0d623f7 | 2685 | FSDBG_BOT(536, np, eof, uio_resid(uio), error); |
2d21ac55 A |
2686 | return (error); |
2687 | } | |
2688 | ||
2689 | int | |
2690 | nfs3_read_rpc_async( | |
2691 | nfsnode_t np, | |
2692 | off_t offset, | |
2693 | size_t len, | |
2694 | thread_t thd, | |
2695 | kauth_cred_t cred, | |
2696 | struct nfsreq_cbinfo *cb, | |
2697 | struct nfsreq **reqp) | |
2698 | { | |
2699 | struct nfsmount *nmp; | |
2700 | int error = 0, nfsvers; | |
2701 | struct nfsm_chain nmreq; | |
2702 | ||
2703 | nmp = NFSTONMP(np); | |
fe8ab488 | 2704 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
2705 | return (ENXIO); |
2706 | nfsvers = nmp->nm_vers; | |
2707 | ||
2708 | nfsm_chain_null(&nmreq); | |
2709 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers) + 3 * NFSX_UNSIGNED); | |
2710 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
2711 | if (nfsvers == NFS_VER3) { | |
2712 | nfsm_chain_add_64(error, &nmreq, offset); | |
2713 | nfsm_chain_add_32(error, &nmreq, len); | |
2714 | } else { | |
2715 | nfsm_chain_add_32(error, &nmreq, offset); | |
2716 | nfsm_chain_add_32(error, &nmreq, len); | |
2717 | nfsm_chain_add_32(error, &nmreq, 0); | |
2718 | } | |
2719 | nfsm_chain_build_done(error, &nmreq); | |
2720 | nfsmout_if(error); | |
6d2010ae | 2721 | error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, NULL, 0, cb, reqp); |
2d21ac55 A |
2722 | nfsmout: |
2723 | nfsm_chain_cleanup(&nmreq); | |
2724 | return (error); | |
2725 | } | |
2726 | ||
2727 | int | |
2728 | nfs3_read_rpc_async_finish( | |
2729 | nfsnode_t np, | |
2730 | struct nfsreq *req, | |
b0d623f7 | 2731 | uio_t uio, |
2d21ac55 A |
2732 | size_t *lenp, |
2733 | int *eofp) | |
2734 | { | |
2735 | int error = 0, lockerror, nfsvers, status, eof = 0; | |
2736 | size_t retlen = 0; | |
2737 | uint64_t xid; | |
2738 | struct nfsmount *nmp; | |
2739 | struct nfsm_chain nmrep; | |
2740 | ||
2741 | nmp = NFSTONMP(np); | |
fe8ab488 | 2742 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2743 | nfs_request_async_cancel(req); |
2744 | return (ENXIO); | |
2745 | } | |
2746 | nfsvers = nmp->nm_vers; | |
2747 | ||
2748 | nfsm_chain_null(&nmrep); | |
2749 | ||
2750 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
2751 | if (error == EINPROGRESS) /* async request restarted */ | |
2752 | return (error); | |
2753 | ||
b0d623f7 | 2754 | if ((lockerror = nfs_node_lock(np))) |
2d21ac55 A |
2755 | error = lockerror; |
2756 | if (nfsvers == NFS_VER3) | |
2757 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); | |
2758 | if (!error) | |
2759 | error = status; | |
2760 | if (nfsvers == NFS_VER3) { | |
2761 | nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); | |
2762 | nfsm_chain_get_32(error, &nmrep, eof); | |
2763 | } else { | |
6d2010ae | 2764 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
2d21ac55 A |
2765 | } |
2766 | if (!lockerror) | |
b0d623f7 | 2767 | nfs_node_unlock(np); |
2d21ac55 A |
2768 | nfsm_chain_get_32(error, &nmrep, retlen); |
2769 | if ((nfsvers == NFS_VER2) && (retlen > *lenp)) | |
2770 | error = EBADRPC; | |
2771 | nfsmout_if(error); | |
b0d623f7 | 2772 | error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uio); |
2d21ac55 A |
2773 | if (eofp) { |
2774 | if (nfsvers == NFS_VER3) { | |
2775 | if (!eof && !retlen) | |
2776 | eof = 1; | |
2777 | } else if (retlen < *lenp) { | |
2778 | eof = 1; | |
1c79356b | 2779 | } |
2d21ac55 A |
2780 | *eofp = eof; |
2781 | } | |
2782 | *lenp = MIN(retlen, *lenp); | |
2783 | nfsmout: | |
2784 | nfsm_chain_cleanup(&nmrep); | |
2785 | return (error); | |
2786 | } | |
2787 | ||
2788 | /* | |
2789 | * NFS write call | |
2790 | */ | |
2791 | int | |
2792 | nfs_vnop_write( | |
2793 | struct vnop_write_args /* { | |
2794 | struct vnodeop_desc *a_desc; | |
2795 | vnode_t a_vp; | |
2796 | struct uio *a_uio; | |
2797 | int a_ioflag; | |
2798 | vfs_context_t a_context; | |
2799 | } */ *ap) | |
2800 | { | |
2801 | vfs_context_t ctx = ap->a_context; | |
b0d623f7 | 2802 | uio_t uio = ap->a_uio; |
2d21ac55 A |
2803 | vnode_t vp = ap->a_vp; |
2804 | nfsnode_t np = VTONFS(vp); | |
2805 | int ioflag = ap->a_ioflag; | |
2806 | struct nfsbuf *bp; | |
2d21ac55 A |
2807 | struct nfsmount *nmp = VTONMP(vp); |
2808 | daddr64_t lbn; | |
2809 | int biosize; | |
2810 | int n, on, error = 0; | |
2811 | off_t boff, start, end; | |
b0d623f7 A |
2812 | uio_t auio; |
2813 | char auio_buf [ UIO_SIZEOF(1) ]; | |
2d21ac55 A |
2814 | thread_t thd; |
2815 | kauth_cred_t cred; | |
2816 | ||
b0d623f7 | 2817 | FSDBG_TOP(515, np, uio_offset(uio), uio_resid(uio), ioflag); |
2d21ac55 A |
2818 | |
2819 | if (vnode_vtype(vp) != VREG) { | |
b0d623f7 | 2820 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), EIO); |
2d21ac55 A |
2821 | return (EIO); |
2822 | } | |
2823 | ||
2824 | thd = vfs_context_thread(ctx); | |
2825 | cred = vfs_context_ucred(ctx); | |
2826 | ||
b0d623f7 | 2827 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
2d21ac55 | 2828 | |
b0d623f7 | 2829 | if ((error = nfs_node_lock(np))) { |
2d21ac55 | 2830 | nfs_data_unlock(np); |
b0d623f7 | 2831 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error); |
2d21ac55 A |
2832 | return (error); |
2833 | } | |
2834 | np->n_wrbusy++; | |
2835 | ||
2836 | if (np->n_flag & NWRITEERR) { | |
2837 | error = np->n_error; | |
2838 | np->n_flag &= ~NWRITEERR; | |
2839 | } | |
2840 | if (np->n_flag & NNEEDINVALIDATE) { | |
2841 | np->n_flag &= ~NNEEDINVALIDATE; | |
b0d623f7 | 2842 | nfs_node_unlock(np); |
2d21ac55 A |
2843 | nfs_data_unlock(np); |
2844 | nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1); | |
b0d623f7 A |
2845 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
2846 | } else { | |
2847 | nfs_node_unlock(np); | |
2d21ac55 | 2848 | } |
b0d623f7 | 2849 | if (error) |
2d21ac55 | 2850 | goto out; |
2d21ac55 A |
2851 | |
2852 | biosize = nmp->nm_biosize; | |
2853 | ||
2854 | if (ioflag & (IO_APPEND | IO_SYNC)) { | |
b0d623f7 | 2855 | nfs_node_lock_force(np); |
2d21ac55 A |
2856 | if (np->n_flag & NMODIFIED) { |
2857 | NATTRINVALIDATE(np); | |
b0d623f7 | 2858 | nfs_node_unlock(np); |
2d21ac55 A |
2859 | nfs_data_unlock(np); |
2860 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); | |
b0d623f7 A |
2861 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
2862 | if (error) { | |
2863 | FSDBG(515, np, uio_offset(uio), 0x10bad01, error); | |
2d21ac55 | 2864 | goto out; |
e5568f75 | 2865 | } |
b0d623f7 A |
2866 | } else { |
2867 | nfs_node_unlock(np); | |
2d21ac55 A |
2868 | } |
2869 | if (ioflag & IO_APPEND) { | |
2d21ac55 | 2870 | nfs_data_unlock(np); |
b0d623f7 | 2871 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae | 2872 | error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED); |
2d21ac55 | 2873 | /* we'll be extending the file, so take the data lock exclusive */ |
b0d623f7 A |
2874 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
2875 | if (error) { | |
2876 | FSDBG(515, np, uio_offset(uio), 0x10bad02, error); | |
2d21ac55 A |
2877 | goto out; |
2878 | } | |
b0d623f7 | 2879 | uio_setoffset(uio, np->n_size); |
2d21ac55 A |
2880 | } |
2881 | } | |
b0d623f7 | 2882 | if (uio_offset(uio) < 0) { |
2d21ac55 | 2883 | error = EINVAL; |
b0d623f7 | 2884 | FSDBG_BOT(515, np, uio_offset(uio), 0xbad0ff, error); |
2d21ac55 A |
2885 | goto out; |
2886 | } | |
b0d623f7 | 2887 | if (uio_resid(uio) == 0) |
2d21ac55 | 2888 | goto out; |
2d21ac55 | 2889 | |
b0d623f7 | 2890 | if (((uio_offset(uio) + uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) { |
fe8ab488 A |
2891 | /* |
2892 | * It looks like we'll be extending the file, so take the data lock exclusive. | |
2893 | */ | |
2d21ac55 | 2894 | nfs_data_unlock(np); |
b0d623f7 | 2895 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
fe8ab488 A |
2896 | |
2897 | /* | |
2898 | * Also, if the write begins after the previous EOF buffer, make sure to zero | |
2899 | * and validate the new bytes in that buffer. | |
2900 | */ | |
2901 | struct nfsbuf *eofbp = NULL; | |
2902 | daddr64_t eofbn = np->n_size / biosize; | |
2903 | int eofoff = np->n_size % biosize; | |
2904 | lbn = uio_offset(uio) / biosize; | |
2905 | ||
2906 | if (eofoff && (eofbn < lbn)) { | |
2907 | if ((error = nfs_buf_get(np, eofbn, biosize, thd, NBLK_WRITE|NBLK_ONLYVALID, &eofbp))) | |
2908 | goto out; | |
2909 | np->n_size += (biosize - eofoff); | |
2910 | nfs_node_lock_force(np); | |
2911 | CLR(np->n_flag, NUPDATESIZE); | |
2912 | np->n_flag |= NMODIFIED; | |
2913 | nfs_node_unlock(np); | |
2914 | FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); | |
2915 | ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */ | |
2916 | if (eofbp) { | |
2917 | /* | |
2918 | * For the old last page, don't zero bytes if there | |
2919 | * are invalid bytes in that page (i.e. the page isn't | |
2920 | * currently valid). | |
2921 | * For pages after the old last page, zero them and | |
2922 | * mark them as valid. | |
2923 | */ | |
2924 | char *d; | |
2925 | int i; | |
2926 | if (ioflag & IO_NOCACHE) | |
2927 | SET(eofbp->nb_flags, NB_NOCACHE); | |
2928 | NFS_BUF_MAP(eofbp); | |
2929 | FSDBG(516, eofbp, eofoff, biosize - eofoff, 0xe0fff01e); | |
2930 | d = eofbp->nb_data; | |
2931 | i = eofoff/PAGE_SIZE; | |
2932 | while (eofoff < biosize) { | |
2933 | int poff = eofoff & PAGE_MASK; | |
2934 | if (!poff || NBPGVALID(eofbp,i)) { | |
2935 | bzero(d + eofoff, PAGE_SIZE - poff); | |
2936 | NBPGVALID_SET(eofbp, i); | |
2937 | } | |
2938 | eofoff += PAGE_SIZE - poff; | |
2939 | i++; | |
2940 | } | |
2941 | nfs_buf_release(eofbp, 1); | |
2942 | } | |
2943 | } | |
2d21ac55 A |
2944 | } |
2945 | ||
2946 | do { | |
316670eb | 2947 | OSAddAtomic64(1, &nfsstats.biocache_writes); |
b0d623f7 A |
2948 | lbn = uio_offset(uio) / biosize; |
2949 | on = uio_offset(uio) % biosize; | |
2950 | n = biosize - on; | |
2951 | if (uio_resid(uio) < n) | |
2952 | n = uio_resid(uio); | |
2d21ac55 A |
2953 | again: |
2954 | /* | |
2955 | * Get a cache block for writing. The range to be written is | |
2956 | * (off..off+n) within the block. We ensure that the block | |
2957 | * either has no dirty region or that the given range is | |
2958 | * contiguous with the existing dirty region. | |
2959 | */ | |
2960 | error = nfs_buf_get(np, lbn, biosize, thd, NBLK_WRITE, &bp); | |
2961 | if (error) | |
2962 | goto out; | |
2963 | /* map the block because we know we're going to write to it */ | |
2964 | NFS_BUF_MAP(bp); | |
2965 | ||
2966 | if (ioflag & IO_NOCACHE) | |
2967 | SET(bp->nb_flags, NB_NOCACHE); | |
2968 | ||
2969 | if (!IS_VALID_CRED(bp->nb_wcred)) { | |
2970 | kauth_cred_ref(cred); | |
2971 | bp->nb_wcred = cred; | |
2972 | } | |
2973 | ||
2974 | /* | |
2975 | * If there's already a dirty range AND dirty pages in this block we | |
2976 | * need to send a commit AND write the dirty pages before continuing. | |
2977 | * | |
2978 | * If there's already a dirty range OR dirty pages in this block | |
2979 | * and the new write range is not contiguous with the existing range, | |
2980 | * then force the buffer to be written out now. | |
2981 | * (We used to just extend the dirty range to cover the valid, | |
2982 | * but unwritten, data in between also. But writing ranges | |
2983 | * of data that weren't actually written by an application | |
2984 | * risks overwriting some other client's data with stale data | |
2985 | * that's just masquerading as new written data.) | |
2986 | */ | |
2987 | if (bp->nb_dirtyend > 0) { | |
2988 | if (on > bp->nb_dirtyend || (on + n) < bp->nb_dirtyoff || bp->nb_dirty) { | |
b0d623f7 | 2989 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c001); |
2d21ac55 A |
2990 | /* write/commit buffer "synchronously" */ |
2991 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
2992 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL)); | |
2993 | SET(bp->nb_flags, (NB_ASYNC | NB_STABLE)); | |
2994 | error = nfs_buf_write(bp); | |
2995 | if (error) | |
2996 | goto out; | |
2997 | goto again; | |
2998 | } | |
2999 | } else if (bp->nb_dirty) { | |
3000 | int firstpg, lastpg; | |
3001 | u_int32_t pagemask; | |
3002 | /* calculate write range pagemask */ | |
3003 | firstpg = on/PAGE_SIZE; | |
3004 | lastpg = (on+n-1)/PAGE_SIZE; | |
3005 | pagemask = ((1 << (lastpg+1)) - 1) & ~((1 << firstpg) - 1); | |
3006 | /* check if there are dirty pages outside the write range */ | |
3007 | if (bp->nb_dirty & ~pagemask) { | |
b0d623f7 | 3008 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c002); |
2d21ac55 A |
3009 | /* write/commit buffer "synchronously" */ |
3010 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
3011 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL)); | |
3012 | SET(bp->nb_flags, (NB_ASYNC | NB_STABLE)); | |
3013 | error = nfs_buf_write(bp); | |
3014 | if (error) | |
3015 | goto out; | |
3016 | goto again; | |
3017 | } | |
3018 | /* if the first or last pages are already dirty */ | |
3019 | /* make sure that the dirty range encompasses those pages */ | |
3020 | if (NBPGDIRTY(bp,firstpg) || NBPGDIRTY(bp,lastpg)) { | |
b0d623f7 | 3021 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c003); |
2d21ac55 A |
3022 | bp->nb_dirtyoff = min(on, firstpg * PAGE_SIZE); |
3023 | if (NBPGDIRTY(bp,lastpg)) { | |
3024 | bp->nb_dirtyend = (lastpg+1) * PAGE_SIZE; | |
3025 | /* clip to EOF */ | |
3026 | if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) { | |
3027 | bp->nb_dirtyend = np->n_size - NBOFF(bp); | |
3028 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) | |
3029 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; | |
3030 | } | |
3031 | } else | |
3032 | bp->nb_dirtyend = on+n; | |
3033 | } | |
3034 | } | |
3035 | ||
3036 | /* | |
3037 | * Are we extending the size of the file with this write? | |
3038 | * If so, update file size now that we have the block. | |
3039 | * If there was a partial buf at the old eof, validate | |
3040 | * and zero the new bytes. | |
3041 | */ | |
b0d623f7 | 3042 | if ((uio_offset(uio) + n) > (off_t)np->n_size) { |
2d21ac55 | 3043 | daddr64_t eofbn = np->n_size / biosize; |
b0d623f7 | 3044 | int neweofoff = (uio_offset(uio) + n) % biosize; |
2d21ac55 | 3045 | |
b0d623f7 | 3046 | FSDBG(515, 0xb1ffa000, uio_offset(uio) + n, eofoff, neweofoff); |
2d21ac55 | 3047 | |
2d21ac55 A |
3048 | /* if we're extending within the same last block */ |
3049 | /* and the block is flagged as being cached... */ | |
3050 | if ((lbn == eofbn) && ISSET(bp->nb_flags, NB_CACHE)) { | |
3051 | /* ...check that all pages in buffer are valid */ | |
3052 | int endpg = ((neweofoff ? neweofoff : biosize) - 1)/PAGE_SIZE; | |
3053 | u_int32_t pagemask; | |
3054 | /* pagemask only has to extend to last page being written to */ | |
3055 | pagemask = (1 << (endpg+1)) - 1; | |
3056 | FSDBG(515, 0xb1ffa001, bp->nb_valid, pagemask, 0); | |
3057 | if ((bp->nb_valid & pagemask) != pagemask) { | |
3058 | /* zerofill any hole */ | |
3059 | if (on > bp->nb_validend) { | |
3060 | int i; | |
3061 | for (i=bp->nb_validend/PAGE_SIZE; i <= (on - 1)/PAGE_SIZE; i++) | |
3062 | NBPGVALID_SET(bp, i); | |
3063 | NFS_BUF_MAP(bp); | |
3064 | FSDBG(516, bp, bp->nb_validend, on - bp->nb_validend, 0xf01e); | |
3065 | bzero((char *)bp->nb_data + bp->nb_validend, | |
3066 | on - bp->nb_validend); | |
3067 | } | |
3068 | /* zerofill any trailing data in the last page */ | |
3069 | if (neweofoff) { | |
3070 | NFS_BUF_MAP(bp); | |
3071 | FSDBG(516, bp, neweofoff, PAGE_SIZE - (neweofoff & PAGE_MASK), 0xe0f); | |
3072 | bzero((char *)bp->nb_data + neweofoff, | |
3073 | PAGE_SIZE - (neweofoff & PAGE_MASK)); | |
3074 | } | |
3075 | } | |
3076 | } | |
b0d623f7 A |
3077 | np->n_size = uio_offset(uio) + n; |
3078 | nfs_node_lock_force(np); | |
2d21ac55 A |
3079 | CLR(np->n_flag, NUPDATESIZE); |
3080 | np->n_flag |= NMODIFIED; | |
b0d623f7 | 3081 | nfs_node_unlock(np); |
2d21ac55 A |
3082 | FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); |
3083 | ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */ | |
2d21ac55 A |
3084 | } |
3085 | /* | |
3086 | * If dirtyend exceeds file size, chop it down. This should | |
3087 | * not occur unless there is a race. | |
3088 | */ | |
3089 | if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) { | |
3090 | bp->nb_dirtyend = np->n_size - NBOFF(bp); | |
3091 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) | |
3092 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; | |
3093 | } | |
3094 | /* | |
3095 | * UBC doesn't handle partial pages, so we need to make sure | |
3096 | * that any pages left in the page cache are completely valid. | |
3097 | * | |
3098 | * Writes that are smaller than a block are delayed if they | |
3099 | * don't extend to the end of the block. | |
3100 | * | |
3101 | * If the block isn't (completely) cached, we may need to read | |
3102 | * in some parts of pages that aren't covered by the write. | |
3103 | * If the write offset (on) isn't page aligned, we'll need to | |
3104 | * read the start of the first page being written to. Likewise, | |
3105 | * if the offset of the end of the write (on+n) isn't page aligned, | |
3106 | * we'll need to read the end of the last page being written to. | |
3107 | * | |
3108 | * Notes: | |
3109 | * We don't want to read anything we're just going to write over. | |
6d2010ae A |
3110 | * We don't want to read anything we're just going drop when the |
3111 | * I/O is complete (i.e. don't do reads for NOCACHE requests). | |
2d21ac55 A |
3112 | * We don't want to issue multiple I/Os if we don't have to |
3113 | * (because they're synchronous rpcs). | |
3114 | * We don't want to read anything we already have modified in the | |
3115 | * page cache. | |
3116 | */ | |
6d2010ae | 3117 | if (!ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) { |
2d21ac55 A |
3118 | int firstpg, lastpg, dirtypg; |
3119 | int firstpgoff, lastpgoff; | |
3120 | start = end = -1; | |
3121 | firstpg = on/PAGE_SIZE; | |
3122 | firstpgoff = on & PAGE_MASK; | |
3123 | lastpg = (on+n-1)/PAGE_SIZE; | |
3124 | lastpgoff = (on+n) & PAGE_MASK; | |
3125 | if (firstpgoff && !NBPGVALID(bp,firstpg)) { | |
3126 | /* need to read start of first page */ | |
3127 | start = firstpg * PAGE_SIZE; | |
3128 | end = start + firstpgoff; | |
3129 | } | |
3130 | if (lastpgoff && !NBPGVALID(bp,lastpg)) { | |
3131 | /* need to read end of last page */ | |
3132 | if (start < 0) | |
3133 | start = (lastpg * PAGE_SIZE) + lastpgoff; | |
3134 | end = (lastpg + 1) * PAGE_SIZE; | |
3135 | } | |
6d2010ae A |
3136 | if (ISSET(bp->nb_flags, NB_NOCACHE)) { |
3137 | /* | |
3138 | * For nocache writes, if there is any partial page at the | |
3139 | * start or end of the write range, then we do the write | |
3140 | * synchronously to make sure that we can drop the data | |
3141 | * from the cache as soon as the WRITE finishes. Normally, | |
3142 | * we would do an unstable write and not drop the data until | |
3143 | * it was committed. But doing that here would risk allowing | |
3144 | * invalid data to be read from the cache between the WRITE | |
3145 | * and the COMMIT. | |
3146 | * (NB_STABLE indicates that data writes should be FILESYNC) | |
3147 | */ | |
3148 | if (end > start) | |
3149 | SET(bp->nb_flags, NB_STABLE); | |
3150 | goto skipread; | |
3151 | } | |
2d21ac55 A |
3152 | if (end > start) { |
3153 | /* need to read the data in range: start...end-1 */ | |
3154 | ||
3155 | /* first, check for dirty pages in between */ | |
3156 | /* if there are, we'll have to do two reads because */ | |
3157 | /* we don't want to overwrite the dirty pages. */ | |
3158 | for (dirtypg=start/PAGE_SIZE; dirtypg <= (end-1)/PAGE_SIZE; dirtypg++) | |
3159 | if (NBPGDIRTY(bp,dirtypg)) | |
3160 | break; | |
3161 | ||
3162 | /* if start is at beginning of page, try */ | |
3163 | /* to get any preceeding pages as well. */ | |
3164 | if (!(start & PAGE_MASK)) { | |
3165 | /* stop at next dirty/valid page or start of block */ | |
3166 | for (; start > 0; start-=PAGE_SIZE) | |
3167 | if (NBPGVALID(bp,((start-1)/PAGE_SIZE))) | |
3168 | break; | |
3169 | } | |
3170 | ||
3171 | NFS_BUF_MAP(bp); | |
3172 | /* setup uio for read(s) */ | |
3173 | boff = NBOFF(bp); | |
b0d623f7 A |
3174 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, |
3175 | &auio_buf, sizeof(auio_buf)); | |
2d21ac55 A |
3176 | |
3177 | if (dirtypg <= (end-1)/PAGE_SIZE) { | |
3178 | /* there's a dirty page in the way, so just do two reads */ | |
3179 | /* we'll read the preceding data here */ | |
b0d623f7 A |
3180 | uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ); |
3181 | uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), on - start); | |
3182 | error = nfs_read_rpc(np, auio, ctx); | |
6d2010ae A |
3183 | if (error) { |
3184 | /* couldn't read the data, so treat buffer as synchronous NOCACHE */ | |
2d21ac55 | 3185 | SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE)); |
6d2010ae A |
3186 | goto skipread; |
3187 | } | |
b0d623f7 A |
3188 | if (uio_resid(auio) > 0) { |
3189 | FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee01); | |
3190 | bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio)); | |
2d21ac55 A |
3191 | } |
3192 | if (!error) { | |
3193 | /* update validoff/validend if necessary */ | |
3194 | if ((bp->nb_validoff < 0) || (bp->nb_validoff > start)) | |
3195 | bp->nb_validoff = start; | |
3196 | if ((bp->nb_validend < 0) || (bp->nb_validend < on)) | |
3197 | bp->nb_validend = on; | |
3198 | if ((off_t)np->n_size > boff + bp->nb_validend) | |
3199 | bp->nb_validend = min(np->n_size - (boff + start), biosize); | |
3200 | /* validate any pages before the write offset */ | |
3201 | for (; start < on/PAGE_SIZE; start+=PAGE_SIZE) | |
3202 | NBPGVALID_SET(bp, start/PAGE_SIZE); | |
3203 | } | |
3204 | /* adjust start to read any trailing data */ | |
3205 | start = on+n; | |
3206 | } | |
3207 | ||
3208 | /* if end is at end of page, try to */ | |
3209 | /* get any following pages as well. */ | |
3210 | if (!(end & PAGE_MASK)) { | |
3211 | /* stop at next valid page or end of block */ | |
3212 | for (; end < biosize; end+=PAGE_SIZE) | |
3213 | if (NBPGVALID(bp,end/PAGE_SIZE)) | |
3214 | break; | |
3215 | } | |
3216 | ||
3217 | if (((boff+start) >= (off_t)np->n_size) || | |
3218 | ((start >= on) && ((boff + on + n) >= (off_t)np->n_size))) { | |
3219 | /* | |
3220 | * Either this entire read is beyond the current EOF | |
3221 | * or the range that we won't be modifying (on+n...end) | |
3222 | * is all beyond the current EOF. | |
3223 | * No need to make a trip across the network to | |
3224 | * read nothing. So, just zero the buffer instead. | |
3225 | */ | |
3226 | FSDBG(516, bp, start, end - start, 0xd00dee00); | |
3227 | bzero(bp->nb_data + start, end - start); | |
3228 | error = 0; | |
6d2010ae | 3229 | } else { |
2d21ac55 | 3230 | /* now we'll read the (rest of the) data */ |
b0d623f7 A |
3231 | uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ); |
3232 | uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), end - start); | |
3233 | error = nfs_read_rpc(np, auio, ctx); | |
6d2010ae A |
3234 | if (error) { |
3235 | /* couldn't read the data, so treat buffer as synchronous NOCACHE */ | |
2d21ac55 | 3236 | SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE)); |
6d2010ae A |
3237 | goto skipread; |
3238 | } | |
b0d623f7 A |
3239 | if (uio_resid(auio) > 0) { |
3240 | FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee02); | |
3241 | bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio)); | |
2d21ac55 A |
3242 | } |
3243 | } | |
3244 | if (!error) { | |
3245 | /* update validoff/validend if necessary */ | |
3246 | if ((bp->nb_validoff < 0) || (bp->nb_validoff > start)) | |
3247 | bp->nb_validoff = start; | |
3248 | if ((bp->nb_validend < 0) || (bp->nb_validend < end)) | |
3249 | bp->nb_validend = end; | |
3250 | if ((off_t)np->n_size > boff + bp->nb_validend) | |
3251 | bp->nb_validend = min(np->n_size - (boff + start), biosize); | |
3252 | /* validate any pages before the write offset's page */ | |
b0d623f7 | 3253 | for (; start < (off_t)trunc_page_32(on); start+=PAGE_SIZE) |
2d21ac55 A |
3254 | NBPGVALID_SET(bp, start/PAGE_SIZE); |
3255 | /* validate any pages after the range of pages being written to */ | |
b0d623f7 | 3256 | for (; (end - 1) > (off_t)round_page_32(on+n-1); end-=PAGE_SIZE) |
2d21ac55 A |
3257 | NBPGVALID_SET(bp, (end-1)/PAGE_SIZE); |
3258 | } | |
3259 | /* Note: pages being written to will be validated when written */ | |
3260 | } | |
3261 | } | |
6d2010ae | 3262 | skipread: |
2d21ac55 A |
3263 | |
3264 | if (ISSET(bp->nb_flags, NB_ERROR)) { | |
3265 | error = bp->nb_error; | |
3266 | nfs_buf_release(bp, 1); | |
3267 | goto out; | |
3268 | } | |
3269 | ||
b0d623f7 | 3270 | nfs_node_lock_force(np); |
2d21ac55 | 3271 | np->n_flag |= NMODIFIED; |
b0d623f7 | 3272 | nfs_node_unlock(np); |
2d21ac55 A |
3273 | |
3274 | NFS_BUF_MAP(bp); | |
3275 | error = uiomove((char *)bp->nb_data + on, n, uio); | |
3276 | if (error) { | |
3277 | SET(bp->nb_flags, NB_ERROR); | |
3278 | nfs_buf_release(bp, 1); | |
3279 | goto out; | |
3280 | } | |
3281 | ||
3282 | /* validate any pages written to */ | |
3283 | start = on & ~PAGE_MASK; | |
3284 | for (; start < on+n; start += PAGE_SIZE) { | |
3285 | NBPGVALID_SET(bp, start/PAGE_SIZE); | |
3286 | /* | |
3287 | * This may seem a little weird, but we don't actually set the | |
3288 | * dirty bits for writes. This is because we keep the dirty range | |
3289 | * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for | |
3290 | * delayed writes, when we give the pages back to the VM we don't | |
3291 | * want to keep them marked dirty, because when we later write the | |
3292 | * buffer we won't be able to tell which pages were written dirty | |
3293 | * and which pages were mmapped and dirtied. | |
3294 | */ | |
3295 | } | |
3296 | if (bp->nb_dirtyend > 0) { | |
3297 | bp->nb_dirtyoff = min(on, bp->nb_dirtyoff); | |
3298 | bp->nb_dirtyend = max((on + n), bp->nb_dirtyend); | |
3299 | } else { | |
3300 | bp->nb_dirtyoff = on; | |
3301 | bp->nb_dirtyend = on + n; | |
3302 | } | |
3303 | if (bp->nb_validend <= 0 || bp->nb_validend < bp->nb_dirtyoff || | |
3304 | bp->nb_validoff > bp->nb_dirtyend) { | |
3305 | bp->nb_validoff = bp->nb_dirtyoff; | |
3306 | bp->nb_validend = bp->nb_dirtyend; | |
e5568f75 | 3307 | } else { |
2d21ac55 A |
3308 | bp->nb_validoff = min(bp->nb_validoff, bp->nb_dirtyoff); |
3309 | bp->nb_validend = max(bp->nb_validend, bp->nb_dirtyend); | |
3310 | } | |
3311 | if (!ISSET(bp->nb_flags, NB_CACHE)) | |
3312 | nfs_buf_normalize_valid_range(np, bp); | |
3313 | ||
3314 | /* | |
3315 | * Since this block is being modified, it must be written | |
3316 | * again and not just committed. | |
3317 | */ | |
3318 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { | |
b0d623f7 | 3319 | nfs_node_lock_force(np); |
2d21ac55 A |
3320 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { |
3321 | np->n_needcommitcnt--; | |
3322 | CHECK_NEEDCOMMITCNT(np); | |
e5568f75 | 3323 | } |
2d21ac55 | 3324 | CLR(bp->nb_flags, NB_NEEDCOMMIT); |
b0d623f7 | 3325 | nfs_node_unlock(np); |
e5568f75 | 3326 | } |
2d21ac55 A |
3327 | |
3328 | if (ioflag & IO_SYNC) { | |
3329 | error = nfs_buf_write(bp); | |
3330 | if (error) | |
3331 | goto out; | |
b0d623f7 A |
3332 | } else if (((n + on) == biosize) || (ioflag & IO_APPEND) || |
3333 | (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) { | |
2d21ac55 A |
3334 | SET(bp->nb_flags, NB_ASYNC); |
3335 | error = nfs_buf_write(bp); | |
3336 | if (error) | |
3337 | goto out; | |
e5568f75 | 3338 | } else { |
2d21ac55 A |
3339 | /* If the block wasn't already delayed: charge for the write */ |
3340 | if (!ISSET(bp->nb_flags, NB_DELWRI)) { | |
3341 | proc_t p = vfs_context_proc(ctx); | |
3342 | if (p && p->p_stats) | |
b0d623f7 | 3343 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); |
2d21ac55 A |
3344 | } |
3345 | nfs_buf_write_delayed(bp); | |
e5568f75 | 3346 | } |
2d21ac55 A |
3347 | if (np->n_needcommitcnt >= NFS_A_LOT_OF_NEEDCOMMITS) |
3348 | nfs_flushcommits(np, 1); | |
3349 | ||
b0d623f7 | 3350 | } while (uio_resid(uio) > 0 && n > 0); |
2d21ac55 A |
3351 | |
3352 | out: | |
b0d623f7 | 3353 | nfs_node_lock_force(np); |
2d21ac55 | 3354 | np->n_wrbusy--; |
b0d623f7 | 3355 | nfs_node_unlock(np); |
2d21ac55 | 3356 | nfs_data_unlock(np); |
b0d623f7 | 3357 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error); |
1c79356b A |
3358 | return (error); |
3359 | } | |
3360 | ||
2d21ac55 | 3361 | |
1c79356b | 3362 | /* |
2d21ac55 | 3363 | * NFS write call |
1c79356b A |
3364 | */ |
3365 | int | |
2d21ac55 A |
3366 | nfs_write_rpc( |
3367 | nfsnode_t np, | |
b0d623f7 | 3368 | uio_t uio, |
2d21ac55 A |
3369 | vfs_context_t ctx, |
3370 | int *iomodep, | |
3371 | uint64_t *wverfp) | |
3372 | { | |
b0d623f7 | 3373 | return nfs_write_rpc2(np, uio, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp); |
2d21ac55 A |
3374 | } |
3375 | ||
3376 | int | |
3377 | nfs_write_rpc2( | |
3378 | nfsnode_t np, | |
b0d623f7 | 3379 | uio_t uio, |
2d21ac55 | 3380 | thread_t thd, |
91447636 | 3381 | kauth_cred_t cred, |
2d21ac55 | 3382 | int *iomodep, |
8f6c56a5 | 3383 | uint64_t *wverfp) |
1c79356b | 3384 | { |
55e303ae | 3385 | struct nfsmount *nmp; |
b0d623f7 | 3386 | int error = 0, nfsvers; |
39236c6e | 3387 | int wverfset, commit, committed; |
2d21ac55 A |
3388 | uint64_t wverf = 0, wverf2; |
3389 | size_t nmwsize, totalsize, tsiz, len, rlen; | |
3390 | struct nfsreq rq, *req = &rq; | |
b0d623f7 | 3391 | uint32_t stategenid = 0, vrestart = 0, restart = 0; |
39236c6e | 3392 | uio_t uio_save = NULL; |
1c79356b A |
3393 | |
3394 | #if DIAGNOSTIC | |
2d21ac55 | 3395 | /* XXX limitation based on need to back up uio on short write */ |
b0d623f7 | 3396 | if (uio_iovcnt(uio) != 1) |
2d21ac55 | 3397 | panic("nfs3_write_rpc: iovcnt > 1"); |
1c79356b | 3398 | #endif |
b0d623f7 | 3399 | FSDBG_TOP(537, np, uio_offset(uio), uio_resid(uio), *iomodep); |
2d21ac55 | 3400 | nmp = NFSTONMP(np); |
fe8ab488 | 3401 | if (nfs_mount_gone(nmp)) |
55e303ae | 3402 | return (ENXIO); |
2d21ac55 A |
3403 | nfsvers = nmp->nm_vers; |
3404 | nmwsize = nmp->nm_wsize; | |
3405 | ||
b0d623f7 | 3406 | wverfset = 0; |
2d21ac55 A |
3407 | committed = NFS_WRITE_FILESYNC; |
3408 | ||
b0d623f7 A |
3409 | totalsize = tsiz = uio_resid(uio); |
3410 | if ((nfsvers == NFS_VER2) && ((uint64_t)(uio_offset(uio) + tsiz) > 0xffffffffULL)) { | |
3411 | FSDBG_BOT(537, np, uio_offset(uio), uio_resid(uio), EFBIG); | |
1c79356b | 3412 | return (EFBIG); |
55e303ae | 3413 | } |
2d21ac55 | 3414 | |
39236c6e A |
3415 | uio_save = uio_duplicate(uio); |
3416 | if (uio_save == NULL) { | |
3417 | return (EIO); | |
3418 | } | |
3419 | ||
1c79356b | 3420 | while (tsiz > 0) { |
2d21ac55 | 3421 | len = (tsiz > nmwsize) ? nmwsize : tsiz; |
b0d623f7 | 3422 | FSDBG(537, np, uio_offset(uio), len, 0); |
6d2010ae A |
3423 | if (np->n_flag & NREVOKE) { |
3424 | error = EIO; | |
3425 | break; | |
3426 | } | |
b0d623f7 A |
3427 | if (nmp->nm_vers >= NFS_VER4) |
3428 | stategenid = nmp->nm_stategenid; | |
3429 | error = nmp->nm_funcs->nf_write_rpc_async(np, uio, len, thd, cred, *iomodep, NULL, &req); | |
2d21ac55 A |
3430 | if (!error) |
3431 | error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req, &commit, &rlen, &wverf2); | |
3432 | nmp = NFSTONMP(np); | |
fe8ab488 | 3433 | if (nfs_mount_gone(nmp)) |
55e303ae | 3434 | error = ENXIO; |
b0d623f7 A |
3435 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) && |
3436 | (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */ | |
3437 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
3438 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
3439 | NP(np, "nfs_write_rpc: error %d, initiating recovery", error); | |
3440 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
3441 | } |
3442 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae A |
3443 | if (np->n_flag & NREVOKE) { |
3444 | error = EIO; | |
3445 | } else { | |
3446 | if (error == NFSERR_GRACE) | |
3447 | tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz); | |
3448 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) | |
3449 | continue; | |
3450 | } | |
b0d623f7 | 3451 | } |
91447636 A |
3452 | if (error) |
3453 | break; | |
2d21ac55 A |
3454 | if (nfsvers == NFS_VER2) { |
3455 | tsiz -= len; | |
3456 | continue; | |
1c79356b | 3457 | } |
1c79356b | 3458 | |
2d21ac55 A |
3459 | /* check for a short write */ |
3460 | if (rlen < len) { | |
39236c6e A |
3461 | /* Reset the uio to reflect the actual transfer */ |
3462 | *uio = *uio_save; | |
3463 | uio_update(uio, totalsize - (tsiz - rlen)); | |
2d21ac55 | 3464 | len = rlen; |
e5568f75 | 3465 | } |
fa4905b1 | 3466 | |
2d21ac55 A |
3467 | /* return lowest commit level returned */ |
3468 | if (commit < committed) | |
3469 | committed = commit; | |
3470 | ||
fa4905b1 | 3471 | tsiz -= len; |
2d21ac55 A |
3472 | |
3473 | /* check write verifier */ | |
3474 | if (!wverfset) { | |
3475 | wverf = wverf2; | |
3476 | wverfset = 1; | |
3477 | } else if (wverf != wverf2) { | |
3478 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 3479 | if (++vrestart > 100) { |
2d21ac55 A |
3480 | /* give up after too many restarts */ |
3481 | error = EIO; | |
3482 | break; | |
3483 | } | |
39236c6e | 3484 | *uio = *uio_save; // Reset the uio back to the start |
2d21ac55 A |
3485 | committed = NFS_WRITE_FILESYNC; |
3486 | wverfset = 0; | |
3487 | tsiz = totalsize; | |
3488 | } | |
3489 | } | |
39236c6e A |
3490 | if (uio_save) |
3491 | uio_free(uio_save); | |
2d21ac55 A |
3492 | if (wverfset && wverfp) |
3493 | *wverfp = wverf; | |
3494 | *iomodep = committed; | |
3495 | if (error) | |
b0d623f7 A |
3496 | uio_setresid(uio, tsiz); |
3497 | FSDBG_BOT(537, np, committed, uio_resid(uio), error); | |
2d21ac55 A |
3498 | return (error); |
3499 | } | |
3500 | ||
3501 | int | |
3502 | nfs3_write_rpc_async( | |
3503 | nfsnode_t np, | |
b0d623f7 | 3504 | uio_t uio, |
2d21ac55 A |
3505 | size_t len, |
3506 | thread_t thd, | |
3507 | kauth_cred_t cred, | |
3508 | int iomode, | |
3509 | struct nfsreq_cbinfo *cb, | |
3510 | struct nfsreq **reqp) | |
3511 | { | |
3512 | struct nfsmount *nmp; | |
6d2010ae | 3513 | mount_t mp; |
2d21ac55 | 3514 | int error = 0, nfsvers; |
2d21ac55 A |
3515 | struct nfsm_chain nmreq; |
3516 | ||
3517 | nmp = NFSTONMP(np); | |
fe8ab488 | 3518 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
3519 | return (ENXIO); |
3520 | nfsvers = nmp->nm_vers; | |
3521 | ||
6d2010ae A |
3522 | /* for async mounts, don't bother sending sync write requests */ |
3523 | if ((iomode != NFS_WRITE_UNSTABLE) && nfs_allow_async && | |
3524 | ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC)) | |
3525 | iomode = NFS_WRITE_UNSTABLE; | |
3526 | ||
2d21ac55 A |
3527 | nfsm_chain_null(&nmreq); |
3528 | nfsm_chain_build_alloc_init(error, &nmreq, | |
3529 | NFSX_FH(nfsvers) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); | |
3530 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
3531 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 3532 | nfsm_chain_add_64(error, &nmreq, uio_offset(uio)); |
2d21ac55 A |
3533 | nfsm_chain_add_32(error, &nmreq, len); |
3534 | nfsm_chain_add_32(error, &nmreq, iomode); | |
3535 | } else { | |
3536 | nfsm_chain_add_32(error, &nmreq, 0); | |
b0d623f7 | 3537 | nfsm_chain_add_32(error, &nmreq, uio_offset(uio)); |
2d21ac55 A |
3538 | nfsm_chain_add_32(error, &nmreq, 0); |
3539 | } | |
3540 | nfsm_chain_add_32(error, &nmreq, len); | |
3541 | nfsmout_if(error); | |
b0d623f7 | 3542 | error = nfsm_chain_add_uio(&nmreq, uio, len); |
2d21ac55 A |
3543 | nfsm_chain_build_done(error, &nmreq); |
3544 | nfsmout_if(error); | |
6d2010ae | 3545 | error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, NULL, 0, cb, reqp); |
2d21ac55 A |
3546 | nfsmout: |
3547 | nfsm_chain_cleanup(&nmreq); | |
3548 | return (error); | |
3549 | } | |
3550 | ||
3551 | int | |
3552 | nfs3_write_rpc_async_finish( | |
3553 | nfsnode_t np, | |
3554 | struct nfsreq *req, | |
3555 | int *iomodep, | |
3556 | size_t *rlenp, | |
3557 | uint64_t *wverfp) | |
3558 | { | |
3559 | struct nfsmount *nmp; | |
3560 | int error = 0, lockerror = ENOENT, nfsvers, status; | |
3561 | int updatemtime = 0, wccpostattr = 0, rlen, committed = NFS_WRITE_FILESYNC; | |
3562 | u_int64_t xid, wverf; | |
3563 | mount_t mp; | |
3564 | struct nfsm_chain nmrep; | |
3565 | ||
3566 | nmp = NFSTONMP(np); | |
fe8ab488 | 3567 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
3568 | nfs_request_async_cancel(req); |
3569 | return (ENXIO); | |
3570 | } | |
3571 | nfsvers = nmp->nm_vers; | |
3572 | ||
3573 | nfsm_chain_null(&nmrep); | |
3574 | ||
3575 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
3576 | if (error == EINPROGRESS) /* async request restarted */ | |
3577 | return (error); | |
3578 | nmp = NFSTONMP(np); | |
fe8ab488 | 3579 | if (nfs_mount_gone(nmp)) |
2d21ac55 | 3580 | error = ENXIO; |
b0d623f7 | 3581 | if (!error && (lockerror = nfs_node_lock(np))) |
2d21ac55 A |
3582 | error = lockerror; |
3583 | if (nfsvers == NFS_VER3) { | |
3584 | struct timespec premtime = { 0, 0 }; | |
3585 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); | |
3586 | if (nfstimespeccmp(&np->n_mtime, &premtime, ==)) | |
3587 | updatemtime = 1; | |
3588 | if (!error) | |
3589 | error = status; | |
3590 | nfsm_chain_get_32(error, &nmrep, rlen); | |
3591 | nfsmout_if(error); | |
3592 | *rlenp = rlen; | |
3593 | if (rlen <= 0) | |
3594 | error = NFSERR_IO; | |
3595 | nfsm_chain_get_32(error, &nmrep, committed); | |
3596 | nfsm_chain_get_64(error, &nmrep, wverf); | |
3597 | nfsmout_if(error); | |
3598 | if (wverfp) | |
3599 | *wverfp = wverf; | |
3600 | lck_mtx_lock(&nmp->nm_lock); | |
3601 | if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) { | |
3602 | nmp->nm_verf = wverf; | |
3603 | nmp->nm_state |= NFSSTA_HASWRITEVERF; | |
3604 | } else if (nmp->nm_verf != wverf) { | |
3605 | nmp->nm_verf = wverf; | |
3606 | } | |
3607 | lck_mtx_unlock(&nmp->nm_lock); | |
3608 | } else { | |
3609 | if (!error) | |
3610 | error = status; | |
6d2010ae | 3611 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
2d21ac55 | 3612 | nfsmout_if(error); |
1c79356b | 3613 | } |
2d21ac55 A |
3614 | if (updatemtime) |
3615 | NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr); | |
1c79356b | 3616 | nfsmout: |
2d21ac55 | 3617 | if (!lockerror) |
b0d623f7 | 3618 | nfs_node_unlock(np); |
2d21ac55 A |
3619 | nfsm_chain_cleanup(&nmrep); |
3620 | if ((committed != NFS_WRITE_FILESYNC) && nfs_allow_async && | |
3621 | ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC)) | |
3622 | committed = NFS_WRITE_FILESYNC; | |
3623 | *iomodep = committed; | |
1c79356b A |
3624 | return (error); |
3625 | } | |
3626 | ||
3627 | /* | |
2d21ac55 A |
3628 | * NFS mknod vnode op |
3629 | * | |
3630 | * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the | |
1c79356b A |
3631 | * mode set to specify the file type and the size field for rdev. |
3632 | */ | |
b0d623f7 | 3633 | int |
2d21ac55 A |
3634 | nfs3_vnop_mknod( |
3635 | struct vnop_mknod_args /* { | |
3636 | struct vnodeop_desc *a_desc; | |
3637 | vnode_t a_dvp; | |
3638 | vnode_t *a_vpp; | |
3639 | struct componentname *a_cnp; | |
3640 | struct vnode_attr *a_vap; | |
3641 | vfs_context_t a_context; | |
3642 | } */ *ap) | |
1c79356b | 3643 | { |
2d21ac55 A |
3644 | vnode_t dvp = ap->a_dvp; |
3645 | vnode_t *vpp = ap->a_vpp; | |
3646 | struct componentname *cnp = ap->a_cnp; | |
3647 | struct vnode_attr *vap = ap->a_vap; | |
3648 | vfs_context_t ctx = ap->a_context; | |
3649 | vnode_t newvp = NULL; | |
3650 | nfsnode_t np = NULL; | |
3651 | struct nfsmount *nmp; | |
3652 | nfsnode_t dnp = VTONFS(dvp); | |
6d2010ae | 3653 | struct nfs_vattr nvattr; |
2d21ac55 | 3654 | fhandle_t fh; |
b0d623f7 | 3655 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0; |
91447636 | 3656 | struct timespec premtime = { 0, 0 }; |
b0d623f7 | 3657 | u_int32_t rdev; |
39236c6e | 3658 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
3659 | int nfsvers, gotuid, gotgid; |
3660 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 3661 | struct nfsreq rq, *req = &rq; |
2d21ac55 A |
3662 | |
3663 | nmp = VTONMP(dvp); | |
fe8ab488 | 3664 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
3665 | return (ENXIO); |
3666 | nfsvers = nmp->nm_vers; | |
1c79356b | 3667 | |
91447636 A |
3668 | if (!VATTR_IS_ACTIVE(vap, va_type)) |
3669 | return (EINVAL); | |
3670 | if (vap->va_type == VCHR || vap->va_type == VBLK) { | |
3671 | if (!VATTR_IS_ACTIVE(vap, va_rdev)) | |
3672 | return (EINVAL); | |
2d21ac55 | 3673 | rdev = vap->va_rdev; |
91447636 | 3674 | } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) |
1c79356b A |
3675 | rdev = 0xffffffff; |
3676 | else { | |
91447636 | 3677 | return (ENOTSUP); |
1c79356b | 3678 | } |
2d21ac55 A |
3679 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) |
3680 | return (ENAMETOOLONG); | |
91447636 | 3681 | |
6d2010ae A |
3682 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
3683 | ||
91447636 A |
3684 | VATTR_SET_SUPPORTED(vap, va_mode); |
3685 | VATTR_SET_SUPPORTED(vap, va_uid); | |
3686 | VATTR_SET_SUPPORTED(vap, va_gid); | |
3687 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
3688 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
3689 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
3690 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
3691 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
3692 | ||
2d21ac55 A |
3693 | nfsm_chain_null(&nmreq); |
3694 | nfsm_chain_null(&nmrep); | |
3695 | ||
3696 | nfsm_chain_build_alloc_init(error, &nmreq, | |
3697 | NFSX_FH(nfsvers) + 4 * NFSX_UNSIGNED + | |
3698 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
3699 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 3700 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
3701 | if (nfsvers == NFS_VER3) { |
3702 | nfsm_chain_add_32(error, &nmreq, vtonfs_type(vap->va_type, nfsvers)); | |
3703 | nfsm_chain_add_v3sattr(error, &nmreq, vap); | |
1c79356b | 3704 | if (vap->va_type == VCHR || vap->va_type == VBLK) { |
2d21ac55 A |
3705 | nfsm_chain_add_32(error, &nmreq, major(vap->va_rdev)); |
3706 | nfsm_chain_add_32(error, &nmreq, minor(vap->va_rdev)); | |
1c79356b A |
3707 | } |
3708 | } else { | |
2d21ac55 | 3709 | nfsm_chain_add_v2sattr(error, &nmreq, vap, rdev); |
91447636 | 3710 | } |
2d21ac55 | 3711 | nfsm_chain_build_done(error, &nmreq); |
b0d623f7 A |
3712 | if (!error) |
3713 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); | |
2d21ac55 A |
3714 | nfsmout_if(error); |
3715 | ||
6d2010ae A |
3716 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKNOD, |
3717 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); | |
3718 | if (!error) | |
3719 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
2d21ac55 | 3720 | |
b0d623f7 A |
3721 | if ((lockerror = nfs_node_lock(dnp))) |
3722 | error = lockerror; | |
91447636 | 3723 | /* XXX no EEXIST kludge here? */ |
2d21ac55 A |
3724 | dxid = xid; |
3725 | if (!error && !status) { | |
3726 | if (dnp->n_flag & NNEGNCENTRIES) { | |
3727 | dnp->n_flag &= ~NNEGNCENTRIES; | |
3728 | cache_purge_negatives(dvp); | |
3729 | } | |
3730 | error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); | |
3731 | } | |
3732 | if (nfsvers == NFS_VER3) | |
3733 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); | |
3734 | if (!error) | |
3735 | error = status; | |
3736 | nfsmout: | |
3737 | nfsm_chain_cleanup(&nmreq); | |
3738 | nfsm_chain_cleanup(&nmrep); | |
3739 | ||
3740 | if (!lockerror) { | |
3741 | dnp->n_flag |= NMODIFIED; | |
3742 | /* if directory hadn't changed, update namecache mtime */ | |
3743 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) | |
3744 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); | |
b0d623f7 A |
3745 | nfs_node_unlock(dnp); |
3746 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 3747 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 3748 | } |
2d21ac55 A |
3749 | |
3750 | if (!error && fh.fh_len) | |
6d2010ae | 3751 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
2d21ac55 A |
3752 | if (!error && !np) |
3753 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); | |
3754 | if (!error && np) | |
3755 | newvp = NFSTOV(np); | |
b0d623f7 A |
3756 | if (!busyerror) |
3757 | nfs_node_clear_busy(dnp); | |
2d21ac55 | 3758 | |
91447636 | 3759 | if (!error && (gotuid || gotgid) && |
6d2010ae | 3760 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
91447636 A |
3761 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
3762 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
3763 | /* clear ID bits if server didn't use them (or we can't tell) */ | |
3764 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
3765 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
3766 | } | |
1c79356b | 3767 | if (error) { |
2d21ac55 | 3768 | if (newvp) { |
b0d623f7 | 3769 | nfs_node_unlock(np); |
91447636 | 3770 | vnode_put(newvp); |
2d21ac55 | 3771 | } |
1c79356b | 3772 | } else { |
1c79356b | 3773 | *vpp = newvp; |
b0d623f7 | 3774 | nfs_node_unlock(np); |
1c79356b | 3775 | } |
1c79356b A |
3776 | return (error); |
3777 | } | |
3778 | ||
b0d623f7 | 3779 | static uint32_t create_verf; |
1c79356b | 3780 | /* |
2d21ac55 | 3781 | * NFS file create call |
1c79356b | 3782 | */ |
b0d623f7 | 3783 | int |
2d21ac55 | 3784 | nfs3_vnop_create( |
91447636 A |
3785 | struct vnop_create_args /* { |
3786 | struct vnodeop_desc *a_desc; | |
3787 | vnode_t a_dvp; | |
3788 | vnode_t *a_vpp; | |
1c79356b | 3789 | struct componentname *a_cnp; |
91447636 A |
3790 | struct vnode_attr *a_vap; |
3791 | vfs_context_t a_context; | |
2d21ac55 | 3792 | } */ *ap) |
1c79356b | 3793 | { |
2d21ac55 | 3794 | vfs_context_t ctx = ap->a_context; |
91447636 A |
3795 | vnode_t dvp = ap->a_dvp; |
3796 | struct vnode_attr *vap = ap->a_vap; | |
3797 | struct componentname *cnp = ap->a_cnp; | |
6d2010ae | 3798 | struct nfs_vattr nvattr; |
2d21ac55 A |
3799 | fhandle_t fh; |
3800 | nfsnode_t np = NULL; | |
3801 | struct nfsmount *nmp; | |
3802 | nfsnode_t dnp = VTONFS(dvp); | |
3803 | vnode_t newvp = NULL; | |
b0d623f7 | 3804 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0, fmode = 0; |
91447636 | 3805 | struct timespec premtime = { 0, 0 }; |
2d21ac55 A |
3806 | int nfsvers, gotuid, gotgid; |
3807 | u_int64_t xid, dxid; | |
3808 | uint32_t val; | |
3809 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 3810 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 3811 | struct nfs_dulookup dul; |
91447636 | 3812 | |
2d21ac55 | 3813 | nmp = VTONMP(dvp); |
fe8ab488 | 3814 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
3815 | return (ENXIO); |
3816 | nfsvers = nmp->nm_vers; | |
1c79356b | 3817 | |
2d21ac55 A |
3818 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) |
3819 | return (ENAMETOOLONG); | |
91447636 | 3820 | |
6d2010ae A |
3821 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
3822 | ||
91447636 A |
3823 | VATTR_SET_SUPPORTED(vap, va_mode); |
3824 | VATTR_SET_SUPPORTED(vap, va_uid); | |
3825 | VATTR_SET_SUPPORTED(vap, va_gid); | |
3826 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
3827 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
3828 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
3829 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
3830 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
1c79356b | 3831 | |
6d2010ae | 3832 | if (vap->va_vaflags & VA_EXCLUSIVE) { |
1c79356b | 3833 | fmode |= O_EXCL; |
6d2010ae A |
3834 | if (!VATTR_IS_ACTIVE(vap, va_access_time) || !VATTR_IS_ACTIVE(vap, va_modify_time)) |
3835 | vap->va_vaflags |= VA_UTIMES_NULL; | |
3836 | } | |
2d21ac55 | 3837 | |
1c79356b | 3838 | again: |
b0d623f7 A |
3839 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
3840 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
2d21ac55 A |
3841 | |
3842 | nfsm_chain_null(&nmreq); | |
3843 | nfsm_chain_null(&nmrep); | |
3844 | ||
3845 | nfsm_chain_build_alloc_init(error, &nmreq, | |
3846 | NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED + | |
3847 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
3848 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 3849 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 | 3850 | if (nfsvers == NFS_VER3) { |
1c79356b | 3851 | if (fmode & O_EXCL) { |
2d21ac55 | 3852 | nfsm_chain_add_32(error, &nmreq, NFS_CREATE_EXCLUSIVE); |
b0d623f7 | 3853 | lck_rw_lock_shared(in_ifaddr_rwlock); |
2d21ac55 A |
3854 | if (!TAILQ_EMPTY(&in_ifaddrhead)) |
3855 | val = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr; | |
3856 | else | |
3857 | val = create_verf; | |
b0d623f7 | 3858 | lck_rw_done(in_ifaddr_rwlock); |
2d21ac55 A |
3859 | nfsm_chain_add_32(error, &nmreq, val); |
3860 | ++create_verf; | |
3861 | nfsm_chain_add_32(error, &nmreq, create_verf); | |
1c79356b | 3862 | } else { |
2d21ac55 A |
3863 | nfsm_chain_add_32(error, &nmreq, NFS_CREATE_UNCHECKED); |
3864 | nfsm_chain_add_v3sattr(error, &nmreq, vap); | |
1c79356b A |
3865 | } |
3866 | } else { | |
2d21ac55 | 3867 | nfsm_chain_add_v2sattr(error, &nmreq, vap, 0); |
1c79356b | 3868 | } |
2d21ac55 A |
3869 | nfsm_chain_build_done(error, &nmreq); |
3870 | nfsmout_if(error); | |
2d21ac55 A |
3871 | |
3872 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_CREATE, | |
6d2010ae | 3873 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
1c79356b | 3874 | if (!error) { |
2d21ac55 A |
3875 | nfs_dulookup_start(&dul, dnp, ctx); |
3876 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
3877 | } | |
3878 | ||
b0d623f7 A |
3879 | if ((lockerror = nfs_node_lock(dnp))) |
3880 | error = lockerror; | |
2d21ac55 A |
3881 | dxid = xid; |
3882 | if (!error && !status) { | |
3883 | if (dnp->n_flag & NNEGNCENTRIES) { | |
3884 | dnp->n_flag &= ~NNEGNCENTRIES; | |
3885 | cache_purge_negatives(dvp); | |
3886 | } | |
3887 | error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); | |
3888 | } | |
3889 | if (nfsvers == NFS_VER3) | |
3890 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); | |
3891 | if (!error) | |
3892 | error = status; | |
3893 | nfsmout: | |
3894 | nfsm_chain_cleanup(&nmreq); | |
3895 | nfsm_chain_cleanup(&nmrep); | |
3896 | ||
3897 | if (!lockerror) { | |
3898 | dnp->n_flag |= NMODIFIED; | |
3899 | /* if directory hadn't changed, update namecache mtime */ | |
3900 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) | |
3901 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); | |
b0d623f7 A |
3902 | nfs_node_unlock(dnp); |
3903 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 3904 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 3905 | } |
2d21ac55 A |
3906 | |
3907 | if (!error && fh.fh_len) | |
6d2010ae | 3908 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
2d21ac55 A |
3909 | if (!error && !np) |
3910 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); | |
3911 | if (!error && np) | |
3912 | newvp = NFSTOV(np); | |
3913 | ||
3914 | nfs_dulookup_finish(&dul, dnp, ctx); | |
b0d623f7 A |
3915 | if (!busyerror) |
3916 | nfs_node_clear_busy(dnp); | |
2d21ac55 | 3917 | |
1c79356b | 3918 | if (error) { |
2d21ac55 | 3919 | if ((nfsvers == NFS_VER3) && (fmode & O_EXCL) && (error == NFSERR_NOTSUPP)) { |
1c79356b A |
3920 | fmode &= ~O_EXCL; |
3921 | goto again; | |
3922 | } | |
2d21ac55 | 3923 | if (newvp) { |
b0d623f7 | 3924 | nfs_node_unlock(np); |
91447636 | 3925 | vnode_put(newvp); |
2d21ac55 A |
3926 | } |
3927 | } else if ((nfsvers == NFS_VER3) && (fmode & O_EXCL)) { | |
b0d623f7 A |
3928 | nfs_node_unlock(np); |
3929 | error = nfs3_setattr_rpc(np, vap, ctx); | |
91447636 A |
3930 | if (error && (gotuid || gotgid)) { |
3931 | /* it's possible the server didn't like our attempt to set IDs. */ | |
3932 | /* so, let's try it again without those */ | |
3933 | VATTR_CLEAR_ACTIVE(vap, va_uid); | |
3934 | VATTR_CLEAR_ACTIVE(vap, va_gid); | |
b0d623f7 | 3935 | error = nfs3_setattr_rpc(np, vap, ctx); |
91447636 | 3936 | } |
b0d623f7 | 3937 | if (error) |
91447636 | 3938 | vnode_put(newvp); |
b0d623f7 A |
3939 | else |
3940 | nfs_node_lock_force(np); | |
ccc36f2f | 3941 | } |
2d21ac55 | 3942 | if (!error) |
1c79356b | 3943 | *ap->a_vpp = newvp; |
91447636 | 3944 | if (!error && (gotuid || gotgid) && |
6d2010ae | 3945 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
91447636 A |
3946 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
3947 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
3948 | /* clear ID bits if server didn't use them (or we can't tell) */ | |
3949 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
3950 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
3951 | } | |
2d21ac55 | 3952 | if (!error) |
b0d623f7 | 3953 | nfs_node_unlock(np); |
1c79356b A |
3954 | return (error); |
3955 | } | |
3956 | ||
3957 | /* | |
2d21ac55 A |
3958 | * NFS file remove call |
3959 | * To try and make NFS semantics closer to UFS semantics, a file that has | |
1c79356b A |
3960 | * other processes using the vnode is renamed instead of removed and then |
3961 | * removed later on the last close. | |
91447636 | 3962 | * - If vnode_isinuse() |
1c79356b A |
3963 | * If a rename is not already in the works |
3964 | * call nfs_sillyrename() to set it up | |
3965 | * else | |
2d21ac55 | 3966 | * do the remove RPC |
1c79356b | 3967 | */ |
b0d623f7 | 3968 | int |
2d21ac55 | 3969 | nfs_vnop_remove( |
91447636 | 3970 | struct vnop_remove_args /* { |
1c79356b | 3971 | struct vnodeop_desc *a_desc; |
91447636 A |
3972 | vnode_t a_dvp; |
3973 | vnode_t a_vp; | |
3974 | struct componentname *a_cnp; | |
3975 | int a_flags; | |
3976 | vfs_context_t a_context; | |
2d21ac55 | 3977 | } */ *ap) |
1c79356b | 3978 | { |
2d21ac55 | 3979 | vfs_context_t ctx = ap->a_context; |
91447636 A |
3980 | vnode_t vp = ap->a_vp; |
3981 | vnode_t dvp = ap->a_dvp; | |
3982 | struct componentname *cnp = ap->a_cnp; | |
2d21ac55 A |
3983 | nfsnode_t dnp = VTONFS(dvp); |
3984 | nfsnode_t np = VTONFS(vp); | |
6d2010ae | 3985 | int error = 0, nfsvers, namedattrs, inuse, gotattr = 0, flushed = 0, setsize = 0; |
91447636 | 3986 | struct nfs_vattr nvattr; |
2d21ac55 A |
3987 | struct nfsmount *nmp; |
3988 | struct nfs_dulookup dul; | |
3989 | ||
3990 | /* XXX prevent removing a sillyrenamed file? */ | |
3991 | ||
3992 | nmp = NFSTONMP(dnp); | |
fe8ab488 | 3993 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
3994 | return (ENXIO); |
3995 | nfsvers = nmp->nm_vers; | |
6d2010ae | 3996 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
2d21ac55 A |
3997 | |
3998 | again_relock: | |
b0d623f7 | 3999 | error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx)); |
2d21ac55 A |
4000 | if (error) |
4001 | return (error); | |
1c79356b | 4002 | |
2d21ac55 A |
4003 | /* lock the node while we remove the file */ |
4004 | lck_mtx_lock(nfs_node_hash_mutex); | |
4005 | while (np->n_hflag & NHLOCKED) { | |
4006 | np->n_hflag |= NHLOCKWANT; | |
4007 | msleep(np, nfs_node_hash_mutex, PINOD, "nfs_remove", NULL); | |
4008 | } | |
4009 | np->n_hflag |= NHLOCKED; | |
4010 | lck_mtx_unlock(nfs_node_hash_mutex); | |
9bccf70c | 4011 | |
6d2010ae A |
4012 | if (!namedattrs) |
4013 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
2d21ac55 A |
4014 | again: |
4015 | inuse = vnode_isinuse(vp, 0); | |
4016 | if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && inuse) { | |
55e303ae | 4017 | /* Caller requested Carbon delete semantics, but file is busy */ |
2d21ac55 A |
4018 | error = EBUSY; |
4019 | goto out; | |
55e303ae | 4020 | } |
2d21ac55 | 4021 | if (inuse && !gotattr) { |
b0d623f7 | 4022 | if (nfs_getattr(np, &nvattr, ctx, NGA_CACHED)) |
2d21ac55 A |
4023 | nvattr.nva_nlink = 1; |
4024 | gotattr = 1; | |
4025 | goto again; | |
4026 | } | |
4027 | if (!inuse || (np->n_sillyrename && (nvattr.nva_nlink > 1))) { | |
4028 | ||
4029 | if (!inuse && !flushed) { /* flush all the buffers first */ | |
4030 | /* unlock the node */ | |
4031 | lck_mtx_lock(nfs_node_hash_mutex); | |
4032 | np->n_hflag &= ~NHLOCKED; | |
4033 | if (np->n_hflag & NHLOCKWANT) { | |
4034 | np->n_hflag &= ~NHLOCKWANT; | |
4035 | wakeup(np); | |
4036 | } | |
4037 | lck_mtx_unlock(nfs_node_hash_mutex); | |
b0d623f7 | 4038 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 A |
4039 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
4040 | FSDBG(260, np, np->n_size, np->n_vattr.nva_size, 0xf00d0011); | |
4041 | flushed = 1; | |
4042 | if (error == EINTR) { | |
b0d623f7 | 4043 | nfs_node_lock_force(np); |
2d21ac55 | 4044 | NATTRINVALIDATE(np); |
b0d623f7 | 4045 | nfs_node_unlock(np); |
2d21ac55 A |
4046 | return (error); |
4047 | } | |
6d2010ae A |
4048 | if (!namedattrs) |
4049 | nfs_dulookup_finish(&dul, dnp, ctx); | |
2d21ac55 A |
4050 | goto again_relock; |
4051 | } | |
4052 | ||
6d2010ae A |
4053 | if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK)) |
4054 | nfs4_delegation_return(np, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); | |
b0d623f7 | 4055 | |
1c79356b A |
4056 | /* |
4057 | * Purge the name cache so that the chance of a lookup for | |
4058 | * the name succeeding while the remove is in progress is | |
483a1d10 | 4059 | * minimized. |
1c79356b | 4060 | */ |
b0d623f7 | 4061 | nfs_name_cache_purge(dnp, np, cnp, ctx); |
2d21ac55 | 4062 | |
6d2010ae A |
4063 | if (!namedattrs) |
4064 | nfs_dulookup_start(&dul, dnp, ctx); | |
2d21ac55 | 4065 | |
1c79356b | 4066 | /* Do the rpc */ |
2d21ac55 A |
4067 | error = nmp->nm_funcs->nf_remove_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen, |
4068 | vfs_context_thread(ctx), vfs_context_ucred(ctx)); | |
4069 | ||
1c79356b A |
4070 | /* |
4071 | * Kludge City: If the first reply to the remove rpc is lost.. | |
4072 | * the reply to the retransmitted request will be ENOENT | |
4073 | * since the file was in fact removed | |
4074 | * Therefore, we cheat and return success. | |
4075 | */ | |
4076 | if (error == ENOENT) | |
4077 | error = 0; | |
2d21ac55 A |
4078 | |
4079 | if (!error && !inuse && !np->n_sillyrename) { | |
55e303ae | 4080 | /* |
2d21ac55 | 4081 | * removal succeeded, it's not in use, and not silly renamed so |
55e303ae A |
4082 | * remove nfsnode from hash now so we can't accidentally find it |
4083 | * again if another object gets created with the same filehandle | |
4084 | * before this vnode gets reclaimed | |
4085 | */ | |
91447636 | 4086 | lck_mtx_lock(nfs_node_hash_mutex); |
2d21ac55 A |
4087 | if (np->n_hflag & NHHASHED) { |
4088 | LIST_REMOVE(np, n_hash); | |
4089 | np->n_hflag &= ~NHHASHED; | |
4090 | FSDBG(266, 0, np, np->n_flag, 0xb1eb1e); | |
4091 | } | |
91447636 | 4092 | lck_mtx_unlock(nfs_node_hash_mutex); |
2d21ac55 | 4093 | /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */ |
91447636 | 4094 | /* clear all flags other than these */ |
b0d623f7 | 4095 | nfs_node_lock_force(np); |
2d21ac55 | 4096 | np->n_flag &= (NMODIFIED); |
2d21ac55 | 4097 | NATTRINVALIDATE(np); |
b0d623f7 A |
4098 | nfs_node_unlock(np); |
4099 | vnode_recycle(vp); | |
2d21ac55 A |
4100 | setsize = 1; |
4101 | } else { | |
b0d623f7 | 4102 | nfs_node_lock_force(np); |
2d21ac55 | 4103 | NATTRINVALIDATE(np); |
b0d623f7 | 4104 | nfs_node_unlock(np); |
55e303ae | 4105 | } |
1c79356b | 4106 | } else if (!np->n_sillyrename) { |
6d2010ae A |
4107 | if (!namedattrs) |
4108 | nfs_dulookup_start(&dul, dnp, ctx); | |
2d21ac55 | 4109 | error = nfs_sillyrename(dnp, np, cnp, ctx); |
b0d623f7 | 4110 | nfs_node_lock_force(np); |
2d21ac55 | 4111 | NATTRINVALIDATE(np); |
b0d623f7 | 4112 | nfs_node_unlock(np); |
2d21ac55 | 4113 | } else { |
b0d623f7 | 4114 | nfs_node_lock_force(np); |
2d21ac55 | 4115 | NATTRINVALIDATE(np); |
b0d623f7 | 4116 | nfs_node_unlock(np); |
6d2010ae A |
4117 | if (!namedattrs) |
4118 | nfs_dulookup_start(&dul, dnp, ctx); | |
1c79356b | 4119 | } |
1c79356b | 4120 | |
b0d623f7 | 4121 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae A |
4122 | nfs_getattr(dnp, NULL, ctx, NGA_CACHED); |
4123 | if (!namedattrs) | |
4124 | nfs_dulookup_finish(&dul, dnp, ctx); | |
2d21ac55 A |
4125 | out: |
4126 | /* unlock the node */ | |
4127 | lck_mtx_lock(nfs_node_hash_mutex); | |
4128 | np->n_hflag &= ~NHLOCKED; | |
4129 | if (np->n_hflag & NHLOCKWANT) { | |
4130 | np->n_hflag &= ~NHLOCKWANT; | |
4131 | wakeup(np); | |
4132 | } | |
4133 | lck_mtx_unlock(nfs_node_hash_mutex); | |
b0d623f7 | 4134 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 A |
4135 | if (setsize) |
4136 | ubc_setsize(vp, 0); | |
1c79356b A |
4137 | return (error); |
4138 | } | |
4139 | ||
4140 | /* | |
2d21ac55 | 4141 | * NFS silly-renamed file removal function called from nfs_vnop_inactive |
1c79356b A |
4142 | */ |
4143 | int | |
2d21ac55 | 4144 | nfs_removeit(struct nfs_sillyrename *nsp) |
1c79356b | 4145 | { |
2d21ac55 | 4146 | struct nfsmount *nmp = NFSTONMP(nsp->nsr_dnp); |
fe8ab488 | 4147 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
4148 | return (ENXIO); |
4149 | return nmp->nm_funcs->nf_remove_rpc(nsp->nsr_dnp, nsp->nsr_name, nsp->nsr_namlen, NULL, nsp->nsr_cred); | |
1c79356b A |
4150 | } |
4151 | ||
4152 | /* | |
2d21ac55 | 4153 | * NFS remove rpc, called from nfs_remove() and nfs_removeit(). |
1c79356b | 4154 | */ |
2d21ac55 A |
4155 | int |
4156 | nfs3_remove_rpc( | |
4157 | nfsnode_t dnp, | |
4158 | char *name, | |
4159 | int namelen, | |
4160 | thread_t thd, | |
4161 | kauth_cred_t cred) | |
1c79356b | 4162 | { |
b0d623f7 | 4163 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0; |
91447636 | 4164 | struct timespec premtime = { 0, 0 }; |
2d21ac55 A |
4165 | struct nfsmount *nmp; |
4166 | int nfsvers; | |
fa4905b1 | 4167 | u_int64_t xid; |
2d21ac55 | 4168 | struct nfsm_chain nmreq, nmrep; |
1c79356b | 4169 | |
2d21ac55 | 4170 | nmp = NFSTONMP(dnp); |
fe8ab488 | 4171 | if (nfs_mount_gone(nmp)) |
55e303ae | 4172 | return (ENXIO); |
2d21ac55 A |
4173 | nfsvers = nmp->nm_vers; |
4174 | if ((nfsvers == NFS_VER2) && (namelen > NFS_MAXNAMLEN)) | |
4175 | return (ENAMETOOLONG); | |
55e303ae | 4176 | |
2d21ac55 A |
4177 | nfsm_chain_null(&nmreq); |
4178 | nfsm_chain_null(&nmrep); | |
4179 | ||
4180 | nfsm_chain_build_alloc_init(error, &nmreq, | |
4181 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen)); | |
4182 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 4183 | nfsm_chain_add_name(error, &nmreq, name, namelen, nmp); |
2d21ac55 A |
4184 | nfsm_chain_build_done(error, &nmreq); |
4185 | nfsmout_if(error); | |
4186 | ||
6d2010ae | 4187 | error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, NULL, 0, &nmrep, &xid, &status); |
2d21ac55 | 4188 | |
b0d623f7 A |
4189 | if ((lockerror = nfs_node_lock(dnp))) |
4190 | error = lockerror; | |
2d21ac55 A |
4191 | if (nfsvers == NFS_VER3) |
4192 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid); | |
b0d623f7 | 4193 | nfsmout_if(error); |
2d21ac55 | 4194 | dnp->n_flag |= NMODIFIED; |
483a1d10 | 4195 | /* if directory hadn't changed, update namecache mtime */ |
2d21ac55 A |
4196 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) |
4197 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); | |
483a1d10 | 4198 | if (!wccpostattr) |
2d21ac55 A |
4199 | NATTRINVALIDATE(dnp); |
4200 | if (!error) | |
4201 | error = status; | |
4202 | nfsmout: | |
b0d623f7 A |
4203 | if (!lockerror) |
4204 | nfs_node_unlock(dnp); | |
2d21ac55 A |
4205 | nfsm_chain_cleanup(&nmreq); |
4206 | nfsm_chain_cleanup(&nmrep); | |
1c79356b A |
4207 | return (error); |
4208 | } | |
4209 | ||
4210 | /* | |
2d21ac55 | 4211 | * NFS file rename call |
1c79356b | 4212 | */ |
b0d623f7 | 4213 | int |
2d21ac55 | 4214 | nfs_vnop_rename( |
91447636 A |
4215 | struct vnop_rename_args /* { |
4216 | struct vnodeop_desc *a_desc; | |
4217 | vnode_t a_fdvp; | |
4218 | vnode_t a_fvp; | |
1c79356b | 4219 | struct componentname *a_fcnp; |
91447636 A |
4220 | vnode_t a_tdvp; |
4221 | vnode_t a_tvp; | |
1c79356b | 4222 | struct componentname *a_tcnp; |
91447636 | 4223 | vfs_context_t a_context; |
2d21ac55 | 4224 | } */ *ap) |
1c79356b | 4225 | { |
2d21ac55 | 4226 | vfs_context_t ctx = ap->a_context; |
91447636 | 4227 | vnode_t fdvp = ap->a_fdvp; |
2d21ac55 | 4228 | vnode_t fvp = ap->a_fvp; |
91447636 | 4229 | vnode_t tdvp = ap->a_tdvp; |
2d21ac55 A |
4230 | vnode_t tvp = ap->a_tvp; |
4231 | nfsnode_t fdnp, fnp, tdnp, tnp; | |
91447636 A |
4232 | struct componentname *tcnp = ap->a_tcnp; |
4233 | struct componentname *fcnp = ap->a_fcnp; | |
2d21ac55 | 4234 | int error, nfsvers, inuse=0, tvprecycle=0, locked=0; |
91447636 | 4235 | mount_t fmp, tdmp, tmp; |
2d21ac55 A |
4236 | struct nfs_vattr nvattr; |
4237 | struct nfsmount *nmp; | |
91447636 | 4238 | |
2d21ac55 A |
4239 | fdnp = VTONFS(fdvp); |
4240 | fnp = VTONFS(fvp); | |
4241 | tdnp = VTONFS(tdvp); | |
91447636 | 4242 | tnp = tvp ? VTONFS(tvp) : NULL; |
1c79356b | 4243 | |
2d21ac55 | 4244 | nmp = NFSTONMP(fdnp); |
fe8ab488 | 4245 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
4246 | return (ENXIO); |
4247 | nfsvers = nmp->nm_vers; | |
4248 | ||
b0d623f7 | 4249 | error = nfs_node_set_busy4(fdnp, fnp, tdnp, tnp, vfs_context_thread(ctx)); |
2d21ac55 A |
4250 | if (error) |
4251 | return (error); | |
4252 | ||
4253 | if (tvp && (tvp != fvp)) { | |
4254 | /* lock the node while we rename over the existing file */ | |
4255 | lck_mtx_lock(nfs_node_hash_mutex); | |
4256 | while (tnp->n_hflag & NHLOCKED) { | |
4257 | tnp->n_hflag |= NHLOCKWANT; | |
4258 | msleep(tnp, nfs_node_hash_mutex, PINOD, "nfs_rename", NULL); | |
4259 | } | |
4260 | tnp->n_hflag |= NHLOCKED; | |
4261 | lck_mtx_unlock(nfs_node_hash_mutex); | |
4262 | locked = 1; | |
4263 | } | |
4264 | ||
1c79356b | 4265 | /* Check for cross-device rename */ |
91447636 A |
4266 | fmp = vnode_mount(fvp); |
4267 | tmp = tvp ? vnode_mount(tvp) : NULL; | |
4268 | tdmp = vnode_mount(tdvp); | |
4269 | if ((fmp != tdmp) || (tvp && (fmp != tmp))) { | |
1c79356b A |
4270 | error = EXDEV; |
4271 | goto out; | |
4272 | } | |
4273 | ||
2d21ac55 A |
4274 | /* XXX prevent renaming from/over a sillyrenamed file? */ |
4275 | ||
1c79356b A |
4276 | /* |
4277 | * If the tvp exists and is in use, sillyrename it before doing the | |
4278 | * rename of the new file over it. | |
4279 | * XXX Can't sillyrename a directory. | |
9bccf70c A |
4280 | * Don't sillyrename if source and target are same vnode (hard |
4281 | * links or case-variants) | |
1c79356b | 4282 | */ |
2d21ac55 | 4283 | if (tvp && (tvp != fvp)) |
91447636 | 4284 | inuse = vnode_isinuse(tvp, 0); |
2d21ac55 A |
4285 | if (inuse && !tnp->n_sillyrename && (vnode_vtype(tvp) != VDIR)) { |
4286 | error = nfs_sillyrename(tdnp, tnp, tcnp, ctx); | |
4287 | if (error) { | |
9bccf70c A |
4288 | /* sillyrename failed. Instead of pressing on, return error */ |
4289 | goto out; /* should not be ENOENT. */ | |
4290 | } else { | |
4291 | /* sillyrename succeeded.*/ | |
9bccf70c A |
4292 | tvp = NULL; |
4293 | } | |
b0d623f7 | 4294 | } else if (tvp && (nmp->nm_vers >= NFS_VER4) && (tnp->n_openflags & N_DELEG_MASK)) { |
6d2010ae | 4295 | nfs4_delegation_return(tnp, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
1c79356b A |
4296 | } |
4297 | ||
2d21ac55 A |
4298 | error = nmp->nm_funcs->nf_rename_rpc(fdnp, fcnp->cn_nameptr, fcnp->cn_namelen, |
4299 | tdnp, tcnp->cn_nameptr, tcnp->cn_namelen, ctx); | |
1c79356b | 4300 | |
91447636 A |
4301 | /* |
4302 | * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. | |
4303 | */ | |
4304 | if (error == ENOENT) | |
4305 | error = 0; | |
4306 | ||
2d21ac55 | 4307 | if (tvp && (tvp != fvp) && !tnp->n_sillyrename) { |
b0d623f7 | 4308 | nfs_node_lock_force(tnp); |
2d21ac55 | 4309 | tvprecycle = (!error && !vnode_isinuse(tvp, 0) && |
6d2010ae | 4310 | (nfs_getattrcache(tnp, &nvattr, 0) || (nvattr.nva_nlink == 1))); |
b0d623f7 | 4311 | nfs_node_unlock(tnp); |
91447636 | 4312 | lck_mtx_lock(nfs_node_hash_mutex); |
2d21ac55 A |
4313 | if (tvprecycle && (tnp->n_hflag & NHHASHED)) { |
4314 | /* | |
4315 | * remove nfsnode from hash now so we can't accidentally find it | |
4316 | * again if another object gets created with the same filehandle | |
4317 | * before this vnode gets reclaimed | |
4318 | */ | |
4319 | LIST_REMOVE(tnp, n_hash); | |
4320 | tnp->n_hflag &= ~NHHASHED; | |
4321 | FSDBG(266, 0, tnp, tnp->n_flag, 0xb1eb1e); | |
4322 | } | |
91447636 | 4323 | lck_mtx_unlock(nfs_node_hash_mutex); |
55e303ae | 4324 | } |
2d21ac55 | 4325 | |
91447636 | 4326 | /* purge the old name cache entries and enter the new one */ |
b0d623f7 | 4327 | nfs_name_cache_purge(fdnp, fnp, fcnp, ctx); |
9bccf70c | 4328 | if (tvp) { |
b0d623f7 | 4329 | nfs_name_cache_purge(tdnp, tnp, tcnp, ctx); |
2d21ac55 A |
4330 | if (tvprecycle) { |
4331 | /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */ | |
91447636 | 4332 | /* clear all flags other than these */ |
b0d623f7 | 4333 | nfs_node_lock_force(tnp); |
2d21ac55 | 4334 | tnp->n_flag &= (NMODIFIED); |
b0d623f7 | 4335 | nfs_node_unlock(tnp); |
91447636 A |
4336 | vnode_recycle(tvp); |
4337 | } | |
9bccf70c | 4338 | } |
2d21ac55 | 4339 | if (!error) { |
b0d623f7 | 4340 | nfs_node_lock_force(tdnp); |
2d21ac55 A |
4341 | if (tdnp->n_flag & NNEGNCENTRIES) { |
4342 | tdnp->n_flag &= ~NNEGNCENTRIES; | |
4343 | cache_purge_negatives(tdvp); | |
4344 | } | |
b0d623f7 A |
4345 | nfs_node_unlock(tdnp); |
4346 | nfs_node_lock_force(fnp); | |
91447636 | 4347 | cache_enter(tdvp, fvp, tcnp); |
2d21ac55 A |
4348 | if (tdvp != fdvp) { /* update parent pointer */ |
4349 | if (fnp->n_parent && !vnode_get(fnp->n_parent)) { | |
4350 | /* remove ref from old parent */ | |
4351 | vnode_rele(fnp->n_parent); | |
4352 | vnode_put(fnp->n_parent); | |
4353 | } | |
4354 | fnp->n_parent = tdvp; | |
4355 | if (tdvp && !vnode_get(tdvp)) { | |
4356 | /* add ref to new parent */ | |
4357 | vnode_ref(tdvp); | |
4358 | vnode_put(tdvp); | |
4359 | } else { | |
4360 | fnp->n_parent = NULL; | |
4361 | } | |
4362 | } | |
b0d623f7 | 4363 | nfs_node_unlock(fnp); |
2d21ac55 | 4364 | } |
1c79356b | 4365 | out: |
b0d623f7 | 4366 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae A |
4367 | nfs_getattr(fdnp, NULL, ctx, NGA_CACHED); |
4368 | nfs_getattr(tdnp, NULL, ctx, NGA_CACHED); | |
2d21ac55 A |
4369 | if (locked) { |
4370 | /* unlock node */ | |
4371 | lck_mtx_lock(nfs_node_hash_mutex); | |
4372 | tnp->n_hflag &= ~NHLOCKED; | |
4373 | if (tnp->n_hflag & NHLOCKWANT) { | |
4374 | tnp->n_hflag &= ~NHLOCKWANT; | |
4375 | wakeup(tnp); | |
4376 | } | |
4377 | lck_mtx_unlock(nfs_node_hash_mutex); | |
4378 | } | |
b0d623f7 | 4379 | nfs_node_clear_busy4(fdnp, fnp, tdnp, tnp); |
1c79356b A |
4380 | return (error); |
4381 | } | |
4382 | ||
4383 | /* | |
2d21ac55 | 4384 | * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename(). |
1c79356b | 4385 | */ |
2d21ac55 A |
4386 | int |
4387 | nfs3_rename_rpc( | |
4388 | nfsnode_t fdnp, | |
4389 | char *fnameptr, | |
4390 | int fnamelen, | |
4391 | nfsnode_t tdnp, | |
4392 | char *tnameptr, | |
4393 | int tnamelen, | |
4394 | vfs_context_t ctx) | |
1c79356b | 4395 | { |
b0d623f7 | 4396 | int error = 0, lockerror = ENOENT, status, fwccpostattr = 0, twccpostattr = 0; |
91447636 | 4397 | struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 }; |
2d21ac55 A |
4398 | struct nfsmount *nmp; |
4399 | int nfsvers; | |
4400 | u_int64_t xid, txid; | |
4401 | struct nfsm_chain nmreq, nmrep; | |
1c79356b | 4402 | |
2d21ac55 | 4403 | nmp = NFSTONMP(fdnp); |
fe8ab488 | 4404 | if (nfs_mount_gone(nmp)) |
55e303ae | 4405 | return (ENXIO); |
2d21ac55 A |
4406 | nfsvers = nmp->nm_vers; |
4407 | if ((nfsvers == NFS_VER2) && | |
4408 | ((fnamelen > NFS_MAXNAMLEN) || (tnamelen > NFS_MAXNAMLEN))) | |
4409 | return (ENAMETOOLONG); | |
4410 | ||
4411 | nfsm_chain_null(&nmreq); | |
4412 | nfsm_chain_null(&nmrep); | |
4413 | ||
4414 | nfsm_chain_build_alloc_init(error, &nmreq, | |
4415 | (NFSX_FH(nfsvers) + NFSX_UNSIGNED) * 2 + | |
4416 | nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen)); | |
4417 | nfsm_chain_add_fh(error, &nmreq, nfsvers, fdnp->n_fhp, fdnp->n_fhsize); | |
6d2010ae | 4418 | nfsm_chain_add_name(error, &nmreq, fnameptr, fnamelen, nmp); |
2d21ac55 | 4419 | nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize); |
6d2010ae | 4420 | nfsm_chain_add_name(error, &nmreq, tnameptr, tnamelen, nmp); |
2d21ac55 A |
4421 | nfsm_chain_build_done(error, &nmreq); |
4422 | nfsmout_if(error); | |
4423 | ||
6d2010ae | 4424 | error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, NULL, &nmrep, &xid, &status); |
2d21ac55 | 4425 | |
b0d623f7 A |
4426 | if ((lockerror = nfs_node_lock2(fdnp, tdnp))) |
4427 | error = lockerror; | |
2d21ac55 A |
4428 | if (nfsvers == NFS_VER3) { |
4429 | txid = xid; | |
4430 | nfsm_chain_get_wcc_data(error, &nmrep, fdnp, &fpremtime, &fwccpostattr, &xid); | |
4431 | nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &tpremtime, &twccpostattr, &txid); | |
4432 | } | |
4433 | if (!error) | |
4434 | error = status; | |
4435 | nfsmout: | |
4436 | nfsm_chain_cleanup(&nmreq); | |
4437 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 A |
4438 | if (!lockerror) { |
4439 | fdnp->n_flag |= NMODIFIED; | |
4440 | /* if directory hadn't changed, update namecache mtime */ | |
4441 | if (nfstimespeccmp(&fdnp->n_ncmtime, &fpremtime, ==)) | |
4442 | NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &fdnp->n_vattr); | |
4443 | if (!fwccpostattr) | |
4444 | NATTRINVALIDATE(fdnp); | |
4445 | tdnp->n_flag |= NMODIFIED; | |
4446 | /* if directory hadn't changed, update namecache mtime */ | |
4447 | if (nfstimespeccmp(&tdnp->n_ncmtime, &tpremtime, ==)) | |
4448 | NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr); | |
4449 | if (!twccpostattr) | |
4450 | NATTRINVALIDATE(tdnp); | |
4451 | nfs_node_unlock2(fdnp, tdnp); | |
4452 | } | |
1c79356b A |
4453 | return (error); |
4454 | } | |
4455 | ||
4456 | /* | |
2d21ac55 | 4457 | * NFS hard link create call |
1c79356b | 4458 | */ |
b0d623f7 | 4459 | int |
2d21ac55 | 4460 | nfs3_vnop_link( |
91447636 A |
4461 | struct vnop_link_args /* { |
4462 | struct vnodeop_desc *a_desc; | |
4463 | vnode_t a_vp; | |
4464 | vnode_t a_tdvp; | |
1c79356b | 4465 | struct componentname *a_cnp; |
91447636 | 4466 | vfs_context_t a_context; |
2d21ac55 | 4467 | } */ *ap) |
1c79356b | 4468 | { |
2d21ac55 | 4469 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4470 | vnode_t vp = ap->a_vp; |
4471 | vnode_t tdvp = ap->a_tdvp; | |
4472 | struct componentname *cnp = ap->a_cnp; | |
b0d623f7 | 4473 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0, attrflag = 0; |
91447636 | 4474 | struct timespec premtime = { 0, 0 }; |
2d21ac55 A |
4475 | struct nfsmount *nmp; |
4476 | nfsnode_t np = VTONFS(vp); | |
4477 | nfsnode_t tdnp = VTONFS(tdvp); | |
4478 | int nfsvers; | |
4479 | u_int64_t xid, txid; | |
4480 | struct nfsm_chain nmreq, nmrep; | |
1c79356b | 4481 | |
2d21ac55 | 4482 | if (vnode_mount(vp) != vnode_mount(tdvp)) |
1c79356b | 4483 | return (EXDEV); |
1c79356b | 4484 | |
2d21ac55 | 4485 | nmp = VTONMP(vp); |
fe8ab488 | 4486 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
4487 | return (ENXIO); |
4488 | nfsvers = nmp->nm_vers; | |
4489 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) | |
4490 | return (ENAMETOOLONG); | |
55e303ae | 4491 | |
1c79356b A |
4492 | /* |
4493 | * Push all writes to the server, so that the attribute cache | |
4494 | * doesn't get "out of sync" with the server. | |
4495 | * XXX There should be a better way! | |
4496 | */ | |
2d21ac55 | 4497 | nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), V_IGNORE_WRITEERR); |
1c79356b | 4498 | |
b0d623f7 | 4499 | error = nfs_node_set_busy2(tdnp, np, vfs_context_thread(ctx)); |
91447636 A |
4500 | if (error) |
4501 | return (error); | |
fa4905b1 | 4502 | |
2d21ac55 A |
4503 | nfsm_chain_null(&nmreq); |
4504 | nfsm_chain_null(&nmrep); | |
4505 | ||
4506 | nfsm_chain_build_alloc_init(error, &nmreq, | |
4507 | NFSX_FH(nfsvers)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); | |
4508 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
4509 | nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize); | |
6d2010ae | 4510 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4511 | nfsm_chain_build_done(error, &nmreq); |
4512 | nfsmout_if(error); | |
6d2010ae | 4513 | error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx, NULL, &nmrep, &xid, &status); |
b0d623f7 A |
4514 | |
4515 | if ((lockerror = nfs_node_lock2(tdnp, np))) { | |
4516 | error = lockerror; | |
4517 | goto nfsmout; | |
4518 | } | |
2d21ac55 A |
4519 | if (nfsvers == NFS_VER3) { |
4520 | txid = xid; | |
4521 | nfsm_chain_postop_attr_update_flag(error, &nmrep, np, attrflag, &xid); | |
4522 | nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &premtime, &wccpostattr, &txid); | |
1c79356b | 4523 | } |
2d21ac55 A |
4524 | if (!error) |
4525 | error = status; | |
4526 | nfsmout: | |
4527 | nfsm_chain_cleanup(&nmreq); | |
4528 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 A |
4529 | if (!lockerror) { |
4530 | if (!attrflag) | |
4531 | NATTRINVALIDATE(np); | |
4532 | tdnp->n_flag |= NMODIFIED; | |
4533 | /* if directory hadn't changed, update namecache mtime */ | |
4534 | if (nfstimespeccmp(&tdnp->n_ncmtime, &premtime, ==)) | |
4535 | NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr); | |
4536 | if (!wccpostattr) | |
4537 | NATTRINVALIDATE(tdnp); | |
4538 | if (!error && (tdnp->n_flag & NNEGNCENTRIES)) { | |
4539 | tdnp->n_flag &= ~NNEGNCENTRIES; | |
4540 | cache_purge_negatives(tdvp); | |
4541 | } | |
4542 | nfs_node_unlock2(tdnp, np); | |
2d21ac55 | 4543 | } |
b0d623f7 | 4544 | nfs_node_clear_busy2(tdnp, np); |
1c79356b A |
4545 | /* |
4546 | * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. | |
4547 | */ | |
4548 | if (error == EEXIST) | |
4549 | error = 0; | |
4550 | return (error); | |
4551 | } | |
4552 | ||
4553 | /* | |
2d21ac55 | 4554 | * NFS symbolic link create call |
1c79356b | 4555 | */ |
b0d623f7 | 4556 | int |
2d21ac55 | 4557 | nfs3_vnop_symlink( |
91447636 A |
4558 | struct vnop_symlink_args /* { |
4559 | struct vnodeop_desc *a_desc; | |
4560 | vnode_t a_dvp; | |
4561 | vnode_t *a_vpp; | |
1c79356b | 4562 | struct componentname *a_cnp; |
91447636 | 4563 | struct vnode_attr *a_vap; |
1c79356b | 4564 | char *a_target; |
91447636 | 4565 | vfs_context_t a_context; |
2d21ac55 | 4566 | } */ *ap) |
1c79356b | 4567 | { |
2d21ac55 | 4568 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4569 | vnode_t dvp = ap->a_dvp; |
4570 | struct vnode_attr *vap = ap->a_vap; | |
4571 | struct componentname *cnp = ap->a_cnp; | |
6d2010ae | 4572 | struct nfs_vattr nvattr; |
2d21ac55 | 4573 | fhandle_t fh; |
b0d623f7 | 4574 | int slen, error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0; |
91447636 | 4575 | struct timespec premtime = { 0, 0 }; |
2d21ac55 A |
4576 | vnode_t newvp = NULL; |
4577 | int nfsvers, gotuid, gotgid; | |
39236c6e | 4578 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
4579 | nfsnode_t np = NULL; |
4580 | nfsnode_t dnp = VTONFS(dvp); | |
4581 | struct nfsmount *nmp; | |
4582 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 4583 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 4584 | struct nfs_dulookup dul; |
91447636 | 4585 | |
2d21ac55 | 4586 | nmp = VTONMP(dvp); |
fe8ab488 | 4587 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
4588 | return (ENXIO); |
4589 | nfsvers = nmp->nm_vers; | |
1c79356b | 4590 | |
1c79356b | 4591 | slen = strlen(ap->a_target); |
2d21ac55 A |
4592 | if ((nfsvers == NFS_VER2) && |
4593 | ((cnp->cn_namelen > NFS_MAXNAMLEN) || (slen > NFS_MAXPATHLEN))) | |
4594 | return (ENAMETOOLONG); | |
91447636 | 4595 | |
6d2010ae A |
4596 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
4597 | ||
91447636 A |
4598 | VATTR_SET_SUPPORTED(vap, va_mode); |
4599 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4600 | VATTR_SET_SUPPORTED(vap, va_gid); | |
4601 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
4602 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
4603 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
4604 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
4605 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
4606 | ||
b0d623f7 A |
4607 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
4608 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
2d21ac55 A |
4609 | |
4610 | nfsm_chain_null(&nmreq); | |
4611 | nfsm_chain_null(&nmrep); | |
4612 | ||
4613 | nfsm_chain_build_alloc_init(error, &nmreq, | |
4614 | NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED + | |
4615 | nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(nfsvers)); | |
4616 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 4617 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4618 | if (nfsvers == NFS_VER3) |
4619 | nfsm_chain_add_v3sattr(error, &nmreq, vap); | |
6d2010ae | 4620 | nfsm_chain_add_name(error, &nmreq, ap->a_target, slen, nmp); |
2d21ac55 A |
4621 | if (nfsvers == NFS_VER2) |
4622 | nfsm_chain_add_v2sattr(error, &nmreq, vap, -1); | |
4623 | nfsm_chain_build_done(error, &nmreq); | |
4624 | nfsmout_if(error); | |
2d21ac55 A |
4625 | |
4626 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_SYMLINK, | |
6d2010ae | 4627 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 A |
4628 | if (!error) { |
4629 | nfs_dulookup_start(&dul, dnp, ctx); | |
4630 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
4631 | } | |
4632 | ||
b0d623f7 A |
4633 | if ((lockerror = nfs_node_lock(dnp))) |
4634 | error = lockerror; | |
2d21ac55 A |
4635 | dxid = xid; |
4636 | if (!error && !status) { | |
4637 | if (dnp->n_flag & NNEGNCENTRIES) { | |
4638 | dnp->n_flag &= ~NNEGNCENTRIES; | |
4639 | cache_purge_negatives(dvp); | |
91447636 | 4640 | } |
2d21ac55 A |
4641 | if (nfsvers == NFS_VER3) |
4642 | error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); | |
4643 | else | |
4644 | fh.fh_len = 0; | |
1c79356b | 4645 | } |
2d21ac55 A |
4646 | if (nfsvers == NFS_VER3) |
4647 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); | |
4648 | if (!error) | |
4649 | error = status; | |
4650 | nfsmout: | |
4651 | nfsm_chain_cleanup(&nmreq); | |
4652 | nfsm_chain_cleanup(&nmrep); | |
fa4905b1 | 4653 | |
2d21ac55 A |
4654 | if (!lockerror) { |
4655 | dnp->n_flag |= NMODIFIED; | |
4656 | /* if directory hadn't changed, update namecache mtime */ | |
4657 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) | |
4658 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); | |
b0d623f7 A |
4659 | nfs_node_unlock(dnp); |
4660 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4661 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 4662 | } |
55e303ae | 4663 | |
2d21ac55 | 4664 | if (!error && fh.fh_len) |
6d2010ae | 4665 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
2d21ac55 A |
4666 | if (!error && np) |
4667 | newvp = NFSTOV(np); | |
4668 | ||
4669 | nfs_dulookup_finish(&dul, dnp, ctx); | |
91447636 | 4670 | |
1c79356b | 4671 | /* |
91447636 A |
4672 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry |
4673 | * if we can succeed in looking up the symlink. | |
1c79356b | 4674 | */ |
2d21ac55 | 4675 | if ((error == EEXIST) || (!error && !newvp)) { |
91447636 | 4676 | if (newvp) { |
b0d623f7 | 4677 | nfs_node_unlock(np); |
91447636 A |
4678 | vnode_put(newvp); |
4679 | newvp = NULL; | |
4680 | } | |
2d21ac55 | 4681 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
91447636 A |
4682 | if (!error) { |
4683 | newvp = NFSTOV(np); | |
4684 | if (vnode_vtype(newvp) != VLNK) | |
4685 | error = EEXIST; | |
4686 | } | |
4687 | } | |
b0d623f7 A |
4688 | if (!busyerror) |
4689 | nfs_node_clear_busy(dnp); | |
91447636 | 4690 | if (!error && (gotuid || gotgid) && |
6d2010ae | 4691 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
91447636 A |
4692 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
4693 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
4694 | /* clear ID bits if server didn't use them (or we can't tell) */ | |
4695 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
4696 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
4697 | } | |
4698 | if (error) { | |
2d21ac55 | 4699 | if (newvp) { |
b0d623f7 | 4700 | nfs_node_unlock(np); |
91447636 | 4701 | vnode_put(newvp); |
2d21ac55 | 4702 | } |
91447636 | 4703 | } else { |
b0d623f7 | 4704 | nfs_node_unlock(np); |
91447636 A |
4705 | *ap->a_vpp = newvp; |
4706 | } | |
1c79356b A |
4707 | return (error); |
4708 | } | |
4709 | ||
4710 | /* | |
2d21ac55 | 4711 | * NFS make dir call |
1c79356b | 4712 | */ |
b0d623f7 | 4713 | int |
2d21ac55 | 4714 | nfs3_vnop_mkdir( |
91447636 A |
4715 | struct vnop_mkdir_args /* { |
4716 | struct vnodeop_desc *a_desc; | |
4717 | vnode_t a_dvp; | |
4718 | vnode_t *a_vpp; | |
1c79356b | 4719 | struct componentname *a_cnp; |
91447636 A |
4720 | struct vnode_attr *a_vap; |
4721 | vfs_context_t a_context; | |
2d21ac55 | 4722 | } */ *ap) |
1c79356b | 4723 | { |
2d21ac55 | 4724 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4725 | vnode_t dvp = ap->a_dvp; |
4726 | struct vnode_attr *vap = ap->a_vap; | |
4727 | struct componentname *cnp = ap->a_cnp; | |
6d2010ae | 4728 | struct nfs_vattr nvattr; |
2d21ac55 A |
4729 | nfsnode_t np = NULL; |
4730 | struct nfsmount *nmp; | |
4731 | nfsnode_t dnp = VTONFS(dvp); | |
4732 | vnode_t newvp = NULL; | |
b0d623f7 | 4733 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0; |
91447636 | 4734 | struct timespec premtime = { 0, 0 }; |
2d21ac55 | 4735 | int nfsvers, gotuid, gotgid; |
39236c6e | 4736 | u_int64_t xid= 0, dxid; |
2d21ac55 A |
4737 | fhandle_t fh; |
4738 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 4739 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 4740 | struct nfs_dulookup dul; |
1c79356b | 4741 | |
2d21ac55 | 4742 | nmp = VTONMP(dvp); |
fe8ab488 | 4743 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
4744 | return (ENXIO); |
4745 | nfsvers = nmp->nm_vers; | |
4746 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) | |
4747 | return (ENAMETOOLONG); | |
91447636 | 4748 | |
6d2010ae A |
4749 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
4750 | ||
91447636 A |
4751 | VATTR_SET_SUPPORTED(vap, va_mode); |
4752 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4753 | VATTR_SET_SUPPORTED(vap, va_gid); | |
4754 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
4755 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
4756 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
4757 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
4758 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
4759 | ||
b0d623f7 A |
4760 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
4761 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
2d21ac55 A |
4762 | |
4763 | nfsm_chain_null(&nmreq); | |
4764 | nfsm_chain_null(&nmrep); | |
4765 | ||
4766 | nfsm_chain_build_alloc_init(error, &nmreq, | |
4767 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + | |
4768 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
4769 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 4770 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4771 | if (nfsvers == NFS_VER3) |
4772 | nfsm_chain_add_v3sattr(error, &nmreq, vap); | |
4773 | else | |
4774 | nfsm_chain_add_v2sattr(error, &nmreq, vap, -1); | |
4775 | nfsm_chain_build_done(error, &nmreq); | |
4776 | nfsmout_if(error); | |
2d21ac55 A |
4777 | |
4778 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKDIR, | |
6d2010ae | 4779 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 A |
4780 | if (!error) { |
4781 | nfs_dulookup_start(&dul, dnp, ctx); | |
4782 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
1c79356b | 4783 | } |
2d21ac55 | 4784 | |
b0d623f7 A |
4785 | if ((lockerror = nfs_node_lock(dnp))) |
4786 | error = lockerror; | |
fa4905b1 | 4787 | dxid = xid; |
2d21ac55 A |
4788 | if (!error && !status) { |
4789 | if (dnp->n_flag & NNEGNCENTRIES) { | |
4790 | dnp->n_flag &= ~NNEGNCENTRIES; | |
4791 | cache_purge_negatives(dvp); | |
4792 | } | |
4793 | error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); | |
4794 | } | |
4795 | if (nfsvers == NFS_VER3) | |
4796 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); | |
1c79356b | 4797 | if (!error) |
2d21ac55 A |
4798 | error = status; |
4799 | nfsmout: | |
4800 | nfsm_chain_cleanup(&nmreq); | |
4801 | nfsm_chain_cleanup(&nmrep); | |
4802 | ||
4803 | if (!lockerror) { | |
4804 | dnp->n_flag |= NMODIFIED; | |
4805 | /* if directory hadn't changed, update namecache mtime */ | |
4806 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) | |
4807 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); | |
b0d623f7 A |
4808 | nfs_node_unlock(dnp); |
4809 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4810 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
2d21ac55 A |
4811 | } |
4812 | ||
4813 | if (!error && fh.fh_len) | |
6d2010ae | 4814 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
2d21ac55 A |
4815 | if (!error && np) |
4816 | newvp = NFSTOV(np); | |
4817 | ||
4818 | nfs_dulookup_finish(&dul, dnp, ctx); | |
4819 | ||
1c79356b A |
4820 | /* |
4821 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry | |
4822 | * if we can succeed in looking up the directory. | |
4823 | */ | |
b0d623f7 | 4824 | if ((error == EEXIST) || (!error && !newvp)) { |
1c79356b | 4825 | if (newvp) { |
b0d623f7 | 4826 | nfs_node_unlock(np); |
91447636 A |
4827 | vnode_put(newvp); |
4828 | newvp = NULL; | |
1c79356b | 4829 | } |
2d21ac55 | 4830 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
1c79356b A |
4831 | if (!error) { |
4832 | newvp = NFSTOV(np); | |
91447636 | 4833 | if (vnode_vtype(newvp) != VDIR) |
1c79356b A |
4834 | error = EEXIST; |
4835 | } | |
4836 | } | |
b0d623f7 A |
4837 | if (!busyerror) |
4838 | nfs_node_clear_busy(dnp); | |
91447636 | 4839 | if (!error && (gotuid || gotgid) && |
6d2010ae | 4840 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
91447636 A |
4841 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
4842 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
4843 | /* clear ID bits if server didn't use them (or we can't tell) */ | |
4844 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
4845 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
4846 | } | |
1c79356b | 4847 | if (error) { |
2d21ac55 | 4848 | if (newvp) { |
b0d623f7 | 4849 | nfs_node_unlock(np); |
91447636 | 4850 | vnode_put(newvp); |
2d21ac55 | 4851 | } |
483a1d10 | 4852 | } else { |
b0d623f7 | 4853 | nfs_node_unlock(np); |
1c79356b | 4854 | *ap->a_vpp = newvp; |
483a1d10 | 4855 | } |
1c79356b A |
4856 | return (error); |
4857 | } | |
4858 | ||
4859 | /* | |
2d21ac55 | 4860 | * NFS remove directory call |
1c79356b | 4861 | */ |
b0d623f7 | 4862 | int |
2d21ac55 | 4863 | nfs3_vnop_rmdir( |
91447636 A |
4864 | struct vnop_rmdir_args /* { |
4865 | struct vnodeop_desc *a_desc; | |
4866 | vnode_t a_dvp; | |
4867 | vnode_t a_vp; | |
1c79356b | 4868 | struct componentname *a_cnp; |
91447636 | 4869 | vfs_context_t a_context; |
2d21ac55 | 4870 | } */ *ap) |
1c79356b | 4871 | { |
2d21ac55 | 4872 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4873 | vnode_t vp = ap->a_vp; |
4874 | vnode_t dvp = ap->a_dvp; | |
4875 | struct componentname *cnp = ap->a_cnp; | |
b0d623f7 | 4876 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0; |
91447636 | 4877 | struct timespec premtime = { 0, 0 }; |
2d21ac55 A |
4878 | struct nfsmount *nmp; |
4879 | nfsnode_t np = VTONFS(vp); | |
4880 | nfsnode_t dnp = VTONFS(dvp); | |
2d21ac55 | 4881 | int nfsvers; |
fa4905b1 | 4882 | u_int64_t xid; |
2d21ac55 | 4883 | struct nfsm_chain nmreq, nmrep; |
6d2010ae | 4884 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 4885 | struct nfs_dulookup dul; |
1c79356b | 4886 | |
2d21ac55 | 4887 | nmp = VTONMP(vp); |
fe8ab488 | 4888 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
4889 | return (ENXIO); |
4890 | nfsvers = nmp->nm_vers; | |
4891 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) | |
4892 | return (ENAMETOOLONG); | |
91447636 | 4893 | |
b0d623f7 | 4894 | if ((error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx)))) |
91447636 | 4895 | return (error); |
2d21ac55 | 4896 | |
b0d623f7 A |
4897 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
4898 | ||
2d21ac55 A |
4899 | nfsm_chain_null(&nmreq); |
4900 | nfsm_chain_null(&nmrep); | |
4901 | ||
4902 | nfsm_chain_build_alloc_init(error, &nmreq, | |
4903 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); | |
4904 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 4905 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4906 | nfsm_chain_build_done(error, &nmreq); |
4907 | nfsmout_if(error); | |
4908 | ||
4909 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_RMDIR, | |
6d2010ae | 4910 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 A |
4911 | if (!error) { |
4912 | nfs_dulookup_start(&dul, dnp, ctx); | |
4913 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
4914 | } | |
4915 | ||
b0d623f7 A |
4916 | if ((lockerror = nfs_node_lock(dnp))) |
4917 | error = lockerror; | |
2d21ac55 A |
4918 | if (nfsvers == NFS_VER3) |
4919 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid); | |
4920 | if (!error) | |
4921 | error = status; | |
4922 | nfsmout: | |
4923 | nfsm_chain_cleanup(&nmreq); | |
4924 | nfsm_chain_cleanup(&nmrep); | |
4925 | ||
b0d623f7 A |
4926 | if (!lockerror) { |
4927 | dnp->n_flag |= NMODIFIED; | |
4928 | /* if directory hadn't changed, update namecache mtime */ | |
4929 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) | |
4930 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); | |
4931 | nfs_node_unlock(dnp); | |
4932 | nfs_name_cache_purge(dnp, np, cnp, ctx); | |
4933 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4934 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
2d21ac55 A |
4935 | } |
4936 | nfs_dulookup_finish(&dul, dnp, ctx); | |
b0d623f7 | 4937 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 | 4938 | |
1c79356b A |
4939 | /* |
4940 | * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. | |
4941 | */ | |
4942 | if (error == ENOENT) | |
4943 | error = 0; | |
483a1d10 A |
4944 | if (!error) { |
4945 | /* | |
4946 | * remove nfsnode from hash now so we can't accidentally find it | |
4947 | * again if another object gets created with the same filehandle | |
4948 | * before this vnode gets reclaimed | |
4949 | */ | |
91447636 | 4950 | lck_mtx_lock(nfs_node_hash_mutex); |
2d21ac55 A |
4951 | if (np->n_hflag & NHHASHED) { |
4952 | LIST_REMOVE(np, n_hash); | |
4953 | np->n_hflag &= ~NHHASHED; | |
4954 | FSDBG(266, 0, np, np->n_flag, 0xb1eb1e); | |
4955 | } | |
91447636 | 4956 | lck_mtx_unlock(nfs_node_hash_mutex); |
483a1d10 | 4957 | } |
1c79356b A |
4958 | return (error); |
4959 | } | |
4960 | ||
4961 | /* | |
2d21ac55 | 4962 | * NFS readdir call |
b0d623f7 A |
4963 | * |
4964 | * The incoming "offset" is a directory cookie indicating where in the | |
4965 | * directory entries should be read from. A zero cookie means start at | |
4966 | * the beginning of the directory. Any other cookie will be a cookie | |
4967 | * returned from the server. | |
4968 | * | |
4969 | * Using that cookie, determine which buffer (and where in that buffer) | |
4970 | * to start returning entries from. Buffer logical block numbers are | |
4971 | * the cookies they start at. If a buffer is found that is not full, | |
4972 | * call into the bio/RPC code to fill it. The RPC code will probably | |
4973 | * fill several buffers (dropping the first, requiring a re-get). | |
4974 | * | |
4975 | * When done copying entries to the buffer, set the offset to the current | |
4976 | * entry's cookie and enter that cookie in the cookie cache. | |
4977 | * | |
4978 | * Note: because the getdirentries(2) API returns a long-typed offset, | |
4979 | * the incoming offset is a potentially truncated cookie (ptc). | |
4980 | * The cookie matching code is aware of this and will fall back to | |
4981 | * matching only 32 bits of the cookie. | |
1c79356b | 4982 | */ |
b0d623f7 | 4983 | int |
2d21ac55 | 4984 | nfs_vnop_readdir( |
91447636 A |
4985 | struct vnop_readdir_args /* { |
4986 | struct vnodeop_desc *a_desc; | |
4987 | vnode_t a_vp; | |
1c79356b | 4988 | struct uio *a_uio; |
b0d623f7 | 4989 | int a_flags; |
91447636 | 4990 | int *a_eofflag; |
b0d623f7 | 4991 | int *a_numdirent; |
91447636 | 4992 | vfs_context_t a_context; |
2d21ac55 | 4993 | } */ *ap) |
1c79356b | 4994 | { |
2d21ac55 | 4995 | vfs_context_t ctx = ap->a_context; |
b0d623f7 A |
4996 | vnode_t dvp = ap->a_vp; |
4997 | nfsnode_t dnp = VTONFS(dvp); | |
2d21ac55 | 4998 | struct nfsmount *nmp; |
b0d623f7 A |
4999 | uio_t uio = ap->a_uio; |
5000 | int error, nfsvers, extended, numdirent, bigcookies, ptc, done; | |
b0d623f7 A |
5001 | uint16_t i, iptc, rlen, nlen; |
5002 | uint64_t cookie, nextcookie, lbn = 0; | |
5003 | struct nfsbuf *bp = NULL; | |
5004 | struct nfs_dir_buf_header *ndbhp; | |
5005 | struct direntry *dp, *dpptc; | |
5006 | struct dirent dent; | |
5007 | char *cp = NULL; | |
5008 | thread_t thd; | |
1c79356b | 5009 | |
b0d623f7 | 5010 | nmp = VTONMP(dvp); |
fe8ab488 | 5011 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
5012 | return (ENXIO); |
5013 | nfsvers = nmp->nm_vers; | |
b0d623f7 A |
5014 | bigcookies = (nmp->nm_state & NFSSTA_BIGCOOKIES); |
5015 | extended = (ap->a_flags & VNODE_READDIR_EXTENDED); | |
2d21ac55 | 5016 | |
b0d623f7 A |
5017 | if (vnode_vtype(dvp) != VDIR) |
5018 | return (EPERM); | |
5019 | ||
5020 | if (ap->a_eofflag) | |
5021 | *ap->a_eofflag = 0; | |
5022 | ||
5023 | if (uio_resid(uio) == 0) | |
5024 | return (0); | |
5025 | ||
6d2010ae A |
5026 | if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) { |
5027 | /* trigger directories should never be read, return nothing */ | |
5028 | return (0); | |
5029 | } | |
5030 | ||
b0d623f7 A |
5031 | thd = vfs_context_thread(ctx); |
5032 | numdirent = done = 0; | |
5033 | nextcookie = uio_offset(uio); | |
5034 | ptc = bigcookies && NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie); | |
5035 | ||
5036 | if ((error = nfs_node_lock(dnp))) | |
5037 | goto out; | |
5038 | ||
5039 | if (dnp->n_flag & NNEEDINVALIDATE) { | |
5040 | dnp->n_flag &= ~NNEEDINVALIDATE; | |
5041 | nfs_invaldir(dnp); | |
5042 | nfs_node_unlock(dnp); | |
5043 | error = nfs_vinvalbuf(dvp, 0, ctx, 1); | |
5044 | if (!error) | |
5045 | error = nfs_node_lock(dnp); | |
5046 | if (error) | |
5047 | goto out; | |
5048 | } | |
91447636 | 5049 | |
1c79356b | 5050 | /* |
b0d623f7 | 5051 | * check for need to invalidate when (re)starting at beginning |
1c79356b | 5052 | */ |
b0d623f7 A |
5053 | if (!nextcookie) { |
5054 | if (dnp->n_flag & NMODIFIED) { | |
5055 | nfs_invaldir(dnp); | |
5056 | nfs_node_unlock(dnp); | |
5057 | if ((error = nfs_vinvalbuf(dvp, 0, ctx, 1))) | |
5058 | goto out; | |
5059 | } else { | |
5060 | nfs_node_unlock(dnp); | |
5061 | } | |
5062 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5063 | if ((error = nfs_getattr(dnp, NULL, ctx, NGA_UNCACHED))) |
b0d623f7 A |
5064 | goto out; |
5065 | } else { | |
5066 | nfs_node_unlock(dnp); | |
5067 | } | |
5068 | ||
5069 | error = nfs_dir_cookie_to_lbn(dnp, nextcookie, &ptc, &lbn); | |
5070 | if (error) { | |
5071 | if (error < 0) { /* just hit EOF cookie */ | |
5072 | done = 1; | |
5073 | error = 0; | |
5074 | } | |
5075 | if (ap->a_eofflag) | |
5076 | *ap->a_eofflag = 1; | |
5077 | } | |
5078 | ||
5079 | while (!error && !done) { | |
316670eb | 5080 | OSAddAtomic64(1, &nfsstats.biocache_readdirs); |
b0d623f7 A |
5081 | cookie = nextcookie; |
5082 | getbuffer: | |
5083 | error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ, &bp); | |
5084 | if (error) | |
5085 | goto out; | |
5086 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5087 | if (!ISSET(bp->nb_flags, NB_CACHE) || !ISSET(ndbhp->ndbh_flags, NDB_FULL)) { | |
5088 | if (!ISSET(bp->nb_flags, NB_CACHE)) { /* initialize the buffer */ | |
5089 | ndbhp->ndbh_flags = 0; | |
5090 | ndbhp->ndbh_count = 0; | |
5091 | ndbhp->ndbh_entry_end = sizeof(*ndbhp); | |
5092 | ndbhp->ndbh_ncgen = dnp->n_ncgen; | |
5093 | } | |
5094 | error = nfs_buf_readdir(bp, ctx); | |
5095 | if (error == NFSERR_DIRBUFDROPPED) | |
5096 | goto getbuffer; | |
5097 | if (error) | |
5098 | nfs_buf_release(bp, 1); | |
5099 | if (error && (error != ENXIO) && (error != ETIMEDOUT) && (error != EINTR) && (error != ERESTART)) { | |
5100 | if (!nfs_node_lock(dnp)) { | |
5101 | nfs_invaldir(dnp); | |
5102 | nfs_node_unlock(dnp); | |
5103 | } | |
5104 | nfs_vinvalbuf(dvp, 0, ctx, 1); | |
5105 | if (error == NFSERR_BAD_COOKIE) | |
5106 | error = ENOENT; | |
5107 | } | |
5108 | if (error) | |
5109 | goto out; | |
5110 | } | |
5111 | ||
5112 | /* find next entry to return */ | |
5113 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5114 | i = 0; | |
5115 | if ((lbn != cookie) && !(ptc && NFS_DIR_COOKIE_SAME32(lbn, cookie))) { | |
5116 | dpptc = NULL; | |
5117 | iptc = 0; | |
5118 | for (; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) { | |
5119 | if (ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) { | |
5120 | iptc = i; | |
5121 | dpptc = dp; | |
5122 | } | |
5123 | nextcookie = dp->d_seekoff; | |
5124 | dp = NFS_DIRENTRY_NEXT(dp); | |
5125 | } | |
5126 | if ((i == ndbhp->ndbh_count) && dpptc) { | |
5127 | i = iptc; | |
5128 | dp = dpptc; | |
5129 | } | |
5130 | if (i < ndbhp->ndbh_count) { | |
5131 | nextcookie = dp->d_seekoff; | |
5132 | dp = NFS_DIRENTRY_NEXT(dp); | |
5133 | i++; | |
5134 | } | |
5135 | } | |
5136 | ptc = 0; /* only have to deal with ptc on first cookie */ | |
5137 | ||
5138 | /* return as many entries as we can */ | |
5139 | for (; i < ndbhp->ndbh_count; i++) { | |
5140 | if (extended) { | |
5141 | rlen = dp->d_reclen; | |
5142 | cp = (char*)dp; | |
5143 | } else { | |
5144 | if (!cp) { | |
5145 | cp = (char*)&dent; | |
5146 | bzero(cp, sizeof(dent)); | |
5147 | } | |
5148 | if (dp->d_namlen > (sizeof(dent.d_name) - 1)) | |
5149 | nlen = sizeof(dent.d_name) - 1; | |
5150 | else | |
5151 | nlen = dp->d_namlen; | |
5152 | rlen = NFS_DIRENT_LEN(nlen); | |
5153 | dent.d_reclen = rlen; | |
5154 | dent.d_ino = dp->d_ino; | |
5155 | dent.d_type = dp->d_type; | |
5156 | dent.d_namlen = nlen; | |
5157 | strlcpy(dent.d_name, dp->d_name, nlen + 1); | |
5158 | } | |
5159 | /* check that the record fits */ | |
5160 | if (rlen > uio_resid(uio)) { | |
5161 | done = 1; | |
5162 | break; | |
5163 | } | |
5164 | if ((error = uiomove(cp, rlen, uio))) | |
5165 | break; | |
5166 | numdirent++; | |
5167 | nextcookie = dp->d_seekoff; | |
5168 | dp = NFS_DIRENTRY_NEXT(dp); | |
5169 | } | |
5170 | ||
5171 | if (i == ndbhp->ndbh_count) { | |
5172 | /* hit end of buffer, move to next buffer */ | |
5173 | lbn = nextcookie; | |
5174 | /* if we also hit EOF, we're done */ | |
5175 | if (ISSET(ndbhp->ndbh_flags, NDB_EOF)) { | |
5176 | done = 1; | |
2d21ac55 A |
5177 | if (ap->a_eofflag) |
5178 | *ap->a_eofflag = 1; | |
483a1d10 | 5179 | } |
1c79356b | 5180 | } |
b0d623f7 A |
5181 | if (!error) |
5182 | uio_setoffset(uio, nextcookie); | |
5183 | if (!error && !done && (nextcookie == cookie)) { | |
5184 | printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie, i, ndbhp->ndbh_count); | |
5185 | error = EIO; | |
5186 | } | |
5187 | nfs_buf_release(bp, 1); | |
1c79356b A |
5188 | } |
5189 | ||
b0d623f7 A |
5190 | if (!error) |
5191 | nfs_dir_cookie_cache(dnp, nextcookie, lbn); | |
1c79356b | 5192 | |
b0d623f7 A |
5193 | if (ap->a_numdirent) |
5194 | *ap->a_numdirent = numdirent; | |
5195 | out: | |
1c79356b A |
5196 | return (error); |
5197 | } | |
5198 | ||
b0d623f7 | 5199 | |
1c79356b | 5200 | /* |
b0d623f7 A |
5201 | * Invalidate cached directory information, except for the actual directory |
5202 | * blocks (which are invalidated separately). | |
1c79356b | 5203 | */ |
b0d623f7 A |
5204 | void |
5205 | nfs_invaldir(nfsnode_t dnp) | |
1c79356b | 5206 | { |
b0d623f7 A |
5207 | if (vnode_vtype(NFSTOV(dnp)) != VDIR) |
5208 | return; | |
5209 | dnp->n_eofcookie = 0; | |
5210 | dnp->n_cookieverf = 0; | |
5211 | if (!dnp->n_cookiecache) | |
5212 | return; | |
5213 | dnp->n_cookiecache->free = 0; | |
5214 | dnp->n_cookiecache->mru = -1; | |
5215 | memset(dnp->n_cookiecache->next, -1, NFSNUMCOOKIES); | |
5216 | } | |
1c79356b | 5217 | |
b0d623f7 A |
5218 | /* |
5219 | * calculate how much space is available for additional directory entries. | |
5220 | */ | |
5221 | uint32_t | |
5222 | nfs_dir_buf_freespace(struct nfsbuf *bp, int rdirplus) | |
5223 | { | |
5224 | struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5225 | uint32_t space; | |
1c79356b | 5226 | |
b0d623f7 A |
5227 | if (!ndbhp) |
5228 | return (0); | |
5229 | space = bp->nb_bufsize - ndbhp->ndbh_entry_end; | |
5230 | if (rdirplus) | |
5231 | space -= ndbhp->ndbh_count * sizeof(struct nfs_vattr); | |
5232 | return (space); | |
5233 | } | |
2d21ac55 | 5234 | |
b0d623f7 A |
5235 | /* |
5236 | * add/update a cookie->lbn entry in the directory cookie cache | |
5237 | */ | |
5238 | void | |
5239 | nfs_dir_cookie_cache(nfsnode_t dnp, uint64_t cookie, uint64_t lbn) | |
5240 | { | |
5241 | struct nfsdmap *ndcc; | |
5242 | int8_t i, prev; | |
5243 | ||
5244 | if (!cookie) | |
5245 | return; | |
5246 | ||
5247 | if (nfs_node_lock(dnp)) | |
5248 | return; | |
5249 | ||
5250 | if (cookie == dnp->n_eofcookie) { /* EOF cookie */ | |
5251 | nfs_node_unlock(dnp); | |
5252 | return; | |
5253 | } | |
5254 | ||
5255 | ndcc = dnp->n_cookiecache; | |
5256 | if (!ndcc) { | |
5257 | /* allocate the cookie cache structure */ | |
5258 | MALLOC_ZONE(dnp->n_cookiecache, struct nfsdmap *, | |
5259 | sizeof(struct nfsdmap), M_NFSDIROFF, M_WAITOK); | |
5260 | if (!dnp->n_cookiecache) { | |
5261 | nfs_node_unlock(dnp); | |
5262 | return; | |
5263 | } | |
5264 | ndcc = dnp->n_cookiecache; | |
5265 | ndcc->free = 0; | |
5266 | ndcc->mru = -1; | |
5267 | memset(ndcc->next, -1, NFSNUMCOOKIES); | |
2d21ac55 A |
5268 | } |
5269 | ||
1c79356b | 5270 | /* |
b0d623f7 A |
5271 | * Search the list for this cookie. |
5272 | * Keep track of previous and last entries. | |
1c79356b | 5273 | */ |
b0d623f7 A |
5274 | prev = -1; |
5275 | i = ndcc->mru; | |
5276 | while ((i != -1) && (cookie != ndcc->cookies[i].key)) { | |
5277 | if (ndcc->next[i] == -1) /* stop on last entry so we can reuse */ | |
5278 | break; | |
5279 | prev = i; | |
5280 | i = ndcc->next[i]; | |
5281 | } | |
5282 | if ((i != -1) && (cookie == ndcc->cookies[i].key)) { | |
5283 | /* found it, remove from list */ | |
5284 | if (prev != -1) | |
5285 | ndcc->next[prev] = ndcc->next[i]; | |
5286 | else | |
5287 | ndcc->mru = ndcc->next[i]; | |
5288 | } else { | |
5289 | /* not found, use next free entry or reuse last entry */ | |
5290 | if (ndcc->free != NFSNUMCOOKIES) | |
5291 | i = ndcc->free++; | |
5292 | else | |
5293 | ndcc->next[prev] = -1; | |
5294 | ndcc->cookies[i].key = cookie; | |
5295 | ndcc->cookies[i].lbn = lbn; | |
5296 | } | |
5297 | /* insert cookie at head of MRU list */ | |
5298 | ndcc->next[i] = ndcc->mru; | |
5299 | ndcc->mru = i; | |
5300 | nfs_node_unlock(dnp); | |
5301 | } | |
2d21ac55 | 5302 | |
b0d623f7 A |
5303 | /* |
5304 | * Try to map the given directory cookie to a directory buffer (return lbn). | |
5305 | * If we have a possibly truncated cookie (ptc), check for 32-bit matches too. | |
5306 | */ | |
5307 | int | |
5308 | nfs_dir_cookie_to_lbn(nfsnode_t dnp, uint64_t cookie, int *ptc, uint64_t *lbnp) | |
5309 | { | |
5310 | struct nfsdmap *ndcc = dnp->n_cookiecache; | |
6d2010ae A |
5311 | int8_t eofptc, found; |
5312 | int i, iptc; | |
b0d623f7 A |
5313 | struct nfsmount *nmp; |
5314 | struct nfsbuf *bp, *lastbp; | |
5315 | struct nfsbuflists blist; | |
5316 | struct direntry *dp, *dpptc; | |
5317 | struct nfs_dir_buf_header *ndbhp; | |
5318 | ||
5319 | if (!cookie) { /* initial cookie */ | |
5320 | *lbnp = 0; | |
5321 | *ptc = 0; | |
5322 | return (0); | |
5323 | } | |
2d21ac55 | 5324 | |
b0d623f7 A |
5325 | if (nfs_node_lock(dnp)) |
5326 | return (ENOENT); | |
2d21ac55 | 5327 | |
b0d623f7 A |
5328 | if (cookie == dnp->n_eofcookie) { /* EOF cookie */ |
5329 | nfs_node_unlock(dnp); | |
316670eb | 5330 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 A |
5331 | *ptc = 0; |
5332 | return (-1); | |
5333 | } | |
5334 | /* note if cookie is a 32-bit match with the EOF cookie */ | |
5335 | eofptc = *ptc ? NFS_DIR_COOKIE_SAME32(cookie, dnp->n_eofcookie) : 0; | |
5336 | iptc = -1; | |
2d21ac55 | 5337 | |
b0d623f7 A |
5338 | /* search the list for the cookie */ |
5339 | for (i = ndcc ? ndcc->mru : -1; i >= 0; i = ndcc->next[i]) { | |
5340 | if (ndcc->cookies[i].key == cookie) { | |
5341 | /* found a match for this cookie */ | |
5342 | *lbnp = ndcc->cookies[i].lbn; | |
5343 | nfs_node_unlock(dnp); | |
316670eb | 5344 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 A |
5345 | *ptc = 0; |
5346 | return (0); | |
1c79356b | 5347 | } |
b0d623f7 A |
5348 | /* check for 32-bit match */ |
5349 | if (*ptc && (iptc == -1) && NFS_DIR_COOKIE_SAME32(ndcc->cookies[i].key, cookie)) | |
5350 | iptc = i; | |
5351 | } | |
5352 | /* exact match not found */ | |
5353 | if (eofptc) { | |
5354 | /* but 32-bit match hit the EOF cookie */ | |
5355 | nfs_node_unlock(dnp); | |
316670eb | 5356 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 A |
5357 | return (-1); |
5358 | } | |
5359 | if (iptc >= 0) { | |
5360 | /* but 32-bit match got a hit */ | |
5361 | *lbnp = ndcc->cookies[iptc].lbn; | |
5362 | nfs_node_unlock(dnp); | |
316670eb | 5363 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 A |
5364 | return (0); |
5365 | } | |
5366 | nfs_node_unlock(dnp); | |
2d21ac55 | 5367 | |
b0d623f7 A |
5368 | /* |
5369 | * No match found in the cookie cache... hmm... | |
5370 | * Let's search the directory's buffers for the cookie. | |
5371 | */ | |
5372 | nmp = NFSTONMP(dnp); | |
fe8ab488 | 5373 | if (nfs_mount_gone(nmp)) |
b0d623f7 A |
5374 | return (ENXIO); |
5375 | dpptc = NULL; | |
5376 | found = 0; | |
5377 | ||
5378 | lck_mtx_lock(nfs_buf_mutex); | |
5379 | /* | |
5380 | * Scan the list of buffers, keeping them in order. | |
5381 | * Note that itercomplete inserts each of the remaining buffers | |
5382 | * into the head of list (thus reversing the elements). So, we | |
5383 | * make sure to iterate through all buffers, inserting them after | |
5384 | * each other, to keep them in order. | |
5385 | * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because | |
5386 | * we don't drop nfs_buf_mutex. | |
5387 | */ | |
5388 | if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) { | |
5389 | lastbp = NULL; | |
5390 | while ((bp = LIST_FIRST(&blist))) { | |
5391 | LIST_REMOVE(bp, nb_vnbufs); | |
5392 | if (!lastbp) | |
5393 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs); | |
2d21ac55 | 5394 | else |
b0d623f7 A |
5395 | LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs); |
5396 | lastbp = bp; | |
5397 | if (found) | |
5398 | continue; | |
5399 | nfs_buf_refget(bp); | |
5400 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
5401 | /* just skip this buffer */ | |
5402 | nfs_buf_refrele(bp); | |
5403 | continue; | |
1c79356b | 5404 | } |
b0d623f7 A |
5405 | nfs_buf_refrele(bp); |
5406 | ||
5407 | /* scan the buffer for the cookie */ | |
5408 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5409 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5410 | dpptc = NULL; | |
5411 | for (i=0; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) { | |
5412 | if (*ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) { | |
5413 | dpptc = dp; | |
5414 | iptc = i; | |
5415 | } | |
5416 | dp = NFS_DIRENTRY_NEXT(dp); | |
91447636 | 5417 | } |
b0d623f7 A |
5418 | if ((i == ndbhp->ndbh_count) && dpptc) { |
5419 | /* found only a PTC match */ | |
5420 | dp = dpptc; | |
5421 | i = iptc; | |
5422 | } else if (i < ndbhp->ndbh_count) { | |
5423 | *ptc = 0; | |
1c79356b | 5424 | } |
b0d623f7 A |
5425 | if (i < (ndbhp->ndbh_count-1)) { |
5426 | /* next entry is *in* this buffer: return this block */ | |
5427 | *lbnp = bp->nb_lblkno; | |
5428 | found = 1; | |
5429 | } else if (i == (ndbhp->ndbh_count-1)) { | |
5430 | /* next entry refers to *next* buffer: return next block */ | |
5431 | *lbnp = dp->d_seekoff; | |
5432 | found = 1; | |
1c79356b | 5433 | } |
b0d623f7 | 5434 | nfs_buf_drop(bp); |
1c79356b | 5435 | } |
b0d623f7 | 5436 | nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN); |
2d21ac55 | 5437 | } |
b0d623f7 A |
5438 | lck_mtx_unlock(nfs_buf_mutex); |
5439 | if (found) { | |
316670eb | 5440 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 | 5441 | return (0); |
1c79356b | 5442 | } |
b0d623f7 A |
5443 | |
5444 | /* still not found... oh well, just start a new block */ | |
5445 | *lbnp = cookie; | |
316670eb | 5446 | OSAddAtomic64(1, &nfsstats.direofcache_misses); |
b0d623f7 A |
5447 | return (0); |
5448 | } | |
5449 | ||
5450 | /* | |
5451 | * scan a directory buffer for the given name | |
5452 | * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found | |
5453 | * Note: should only be called with RDIRPLUS directory buffers | |
5454 | */ | |
5455 | ||
5456 | #define NDBS_PURGE 1 | |
5457 | #define NDBS_UPDATE 2 | |
5458 | ||
5459 | int | |
5460 | nfs_dir_buf_search( | |
5461 | struct nfsbuf *bp, | |
5462 | struct componentname *cnp, | |
5463 | fhandle_t *fhp, | |
5464 | struct nfs_vattr *nvap, | |
5465 | uint64_t *xidp, | |
5466 | time_t *attrstampp, | |
5467 | daddr64_t *nextlbnp, | |
5468 | int flags) | |
5469 | { | |
5470 | struct direntry *dp; | |
5471 | struct nfs_dir_buf_header *ndbhp; | |
5472 | struct nfs_vattr *nvattrp; | |
5473 | daddr64_t nextlbn = 0; | |
d26ffc64 A |
5474 | int i, error = ESRCH; |
5475 | uint32_t fhlen; | |
b0d623f7 A |
5476 | |
5477 | /* scan the buffer for the name */ | |
5478 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5479 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5480 | for (i=0; i < ndbhp->ndbh_count; i++) { | |
5481 | nextlbn = dp->d_seekoff; | |
5482 | if ((cnp->cn_namelen == dp->d_namlen) && !strcmp(cnp->cn_nameptr, dp->d_name)) { | |
5483 | fhlen = dp->d_name[dp->d_namlen+1]; | |
5484 | nvattrp = NFS_DIR_BUF_NVATTR(bp, i); | |
5485 | if ((ndbhp->ndbh_ncgen != bp->nb_np->n_ncgen) || (fhp->fh_len == 0) || | |
5486 | (nvattrp->nva_type == VNON) || (nvattrp->nva_fileid == 0)) { | |
6d2010ae | 5487 | /* entry is not valid */ |
b0d623f7 A |
5488 | error = ENOENT; |
5489 | break; | |
5490 | } | |
5491 | if (flags == NDBS_PURGE) { | |
5492 | dp->d_fileno = 0; | |
5493 | bzero(nvattrp, sizeof(*nvattrp)); | |
5494 | error = ENOENT; | |
5495 | break; | |
5496 | } | |
5497 | if (flags == NDBS_UPDATE) { | |
5498 | /* update direntry's attrs if fh matches */ | |
5499 | if ((fhp->fh_len == fhlen) && !bcmp(&dp->d_name[dp->d_namlen+2], fhp->fh_data, fhlen)) { | |
5500 | bcopy(nvap, nvattrp, sizeof(*nvap)); | |
5501 | dp->d_fileno = nvattrp->nva_fileid; | |
5502 | nvattrp->nva_fileid = *xidp; | |
5503 | *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]) = *attrstampp; | |
5504 | } | |
5505 | error = 0; | |
5506 | break; | |
5507 | } | |
5508 | /* copy out fh, attrs, attrstamp, and xid */ | |
5509 | fhp->fh_len = fhlen; | |
5510 | bcopy(&dp->d_name[dp->d_namlen+2], fhp->fh_data, MAX(fhp->fh_len, (int)sizeof(fhp->fh_data))); | |
5511 | *attrstampp = *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]); | |
5512 | bcopy(nvattrp, nvap, sizeof(*nvap)); | |
5513 | *xidp = nvap->nva_fileid; | |
5514 | nvap->nva_fileid = dp->d_fileno; | |
5515 | error = 0; | |
5516 | break; | |
5517 | } | |
5518 | dp = NFS_DIRENTRY_NEXT(dp); | |
1c79356b | 5519 | } |
b0d623f7 A |
5520 | if (nextlbnp) |
5521 | *nextlbnp = nextlbn; | |
5522 | return (error); | |
5523 | } | |
1c79356b | 5524 | |
b0d623f7 A |
5525 | /* |
5526 | * Look up a name in a directory's buffers. | |
5527 | * Note: should only be called with RDIRPLUS directory buffers | |
5528 | */ | |
5529 | int | |
5530 | nfs_dir_buf_cache_lookup(nfsnode_t dnp, nfsnode_t *npp, struct componentname *cnp, vfs_context_t ctx, int purge) | |
5531 | { | |
5532 | nfsnode_t newnp; | |
5533 | struct nfsmount *nmp; | |
6d2010ae | 5534 | int error = 0, i, found = 0, count = 0; |
b0d623f7 A |
5535 | u_int64_t xid; |
5536 | struct nfs_vattr nvattr; | |
5537 | fhandle_t fh; | |
5538 | time_t attrstamp = 0; | |
5539 | thread_t thd = vfs_context_thread(ctx); | |
5540 | struct nfsbuf *bp, *lastbp, *foundbp; | |
5541 | struct nfsbuflists blist; | |
5542 | daddr64_t lbn, nextlbn; | |
5543 | int dotunder = (cnp->cn_namelen > 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '_'); | |
5544 | ||
fe8ab488 A |
5545 | nmp = NFSTONMP(dnp); |
5546 | if (nfs_mount_gone(nmp)) | |
b0d623f7 | 5547 | return (ENXIO); |
b0d623f7 A |
5548 | if (!purge) |
5549 | *npp = NULL; | |
5550 | ||
5551 | /* first check most recent buffer (and next one too) */ | |
5552 | lbn = dnp->n_lastdbl; | |
5553 | for (i=0; i < 2; i++) { | |
5554 | if ((error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp))) | |
5555 | return (error); | |
5556 | if (!bp) | |
5557 | break; | |
5558 | count++; | |
5559 | error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, &nextlbn, purge ? NDBS_PURGE : 0); | |
5560 | nfs_buf_release(bp, 0); | |
5561 | if (error == ESRCH) { | |
5562 | error = 0; | |
5563 | } else { | |
5564 | found = 1; | |
5565 | break; | |
5566 | } | |
5567 | lbn = nextlbn; | |
5568 | } | |
5569 | ||
5570 | lck_mtx_lock(nfs_buf_mutex); | |
5571 | if (found) { | |
5572 | dnp->n_lastdbl = lbn; | |
5573 | goto done; | |
5574 | } | |
2d21ac55 | 5575 | |
1c79356b | 5576 | /* |
b0d623f7 A |
5577 | * Scan the list of buffers, keeping them in order. |
5578 | * Note that itercomplete inserts each of the remaining buffers | |
5579 | * into the head of list (thus reversing the elements). So, we | |
5580 | * make sure to iterate through all buffers, inserting them after | |
5581 | * each other, to keep them in order. | |
5582 | * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because | |
5583 | * we don't drop nfs_buf_mutex. | |
1c79356b | 5584 | */ |
b0d623f7 A |
5585 | if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) { |
5586 | lastbp = foundbp = NULL; | |
5587 | while ((bp = LIST_FIRST(&blist))) { | |
5588 | LIST_REMOVE(bp, nb_vnbufs); | |
5589 | if (!lastbp) | |
5590 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs); | |
5591 | else | |
5592 | LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs); | |
5593 | lastbp = bp; | |
5594 | if (error || found) | |
5595 | continue; | |
5596 | if (!purge && dotunder && (count > 100)) /* don't waste too much time looking for ._ files */ | |
5597 | continue; | |
5598 | nfs_buf_refget(bp); | |
5599 | lbn = bp->nb_lblkno; | |
5600 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
5601 | /* just skip this buffer */ | |
5602 | nfs_buf_refrele(bp); | |
5603 | continue; | |
5604 | } | |
5605 | nfs_buf_refrele(bp); | |
5606 | count++; | |
5607 | error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, purge ? NDBS_PURGE : 0); | |
5608 | if (error == ESRCH) { | |
5609 | error = 0; | |
5610 | } else { | |
5611 | found = 1; | |
5612 | foundbp = bp; | |
5613 | } | |
5614 | nfs_buf_drop(bp); | |
5615 | } | |
5616 | if (found) { | |
5617 | LIST_REMOVE(foundbp, nb_vnbufs); | |
5618 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, foundbp, nb_vnbufs); | |
5619 | dnp->n_lastdbl = foundbp->nb_lblkno; | |
5620 | } | |
5621 | nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN); | |
5622 | } | |
5623 | done: | |
5624 | lck_mtx_unlock(nfs_buf_mutex); | |
5625 | ||
5626 | if (!error && found && !purge) { | |
5627 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, | |
6d2010ae | 5628 | &nvattr, &xid, dnp->n_auth, NG_MAKEENTRY, &newnp); |
b0d623f7 A |
5629 | if (error) |
5630 | return (error); | |
5631 | newnp->n_attrstamp = attrstamp; | |
5632 | *npp = newnp; | |
5633 | nfs_node_unlock(newnp); | |
5634 | /* check if the dir buffer's attrs are out of date */ | |
5635 | if (!nfs_getattr(newnp, &nvattr, ctx, NGA_CACHED) && | |
5636 | (newnp->n_attrstamp != attrstamp)) { | |
5637 | /* they are, so update them */ | |
5638 | error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp); | |
5639 | if (!error && bp) { | |
5640 | attrstamp = newnp->n_attrstamp; | |
5641 | xid = newnp->n_xid; | |
5642 | nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, NDBS_UPDATE); | |
5643 | nfs_buf_release(bp, 0); | |
5644 | } | |
5645 | error = 0; | |
5646 | } | |
1c79356b | 5647 | } |
2d21ac55 | 5648 | |
1c79356b A |
5649 | return (error); |
5650 | } | |
5651 | ||
5652 | /* | |
b0d623f7 A |
5653 | * Purge name cache entries for the given node. |
5654 | * For RDIRPLUS, also invalidate the entry in the directory's buffers. | |
5655 | */ | |
5656 | void | |
5657 | nfs_name_cache_purge(nfsnode_t dnp, nfsnode_t np, struct componentname *cnp, vfs_context_t ctx) | |
5658 | { | |
5659 | struct nfsmount *nmp = NFSTONMP(dnp); | |
5660 | ||
5661 | cache_purge(NFSTOV(np)); | |
6d2010ae | 5662 | if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) |
b0d623f7 A |
5663 | nfs_dir_buf_cache_lookup(dnp, NULL, cnp, ctx, 1); |
5664 | } | |
5665 | ||
5666 | /* | |
5667 | * NFS V3 readdir (plus) RPC. | |
1c79356b A |
5668 | */ |
5669 | int | |
b0d623f7 | 5670 | nfs3_readdir_rpc(nfsnode_t dnp, struct nfsbuf *bp, vfs_context_t ctx) |
1c79356b | 5671 | { |
55e303ae | 5672 | struct nfsmount *nmp; |
b0d623f7 A |
5673 | int error = 0, lockerror, nfsvers, rdirplus, bigcookies; |
5674 | int i, status, attrflag, fhflag, more_entries = 1, eof, bp_dropped = 0; | |
5675 | uint32_t nmreaddirsize, nmrsize; | |
5676 | uint32_t namlen, skiplen, fhlen, xlen, attrlen, reclen, space_free, space_needed; | |
5677 | uint64_t cookie, lastcookie, xid, savedxid, fileno; | |
5678 | struct nfsm_chain nmreq, nmrep, nmrepsave; | |
5679 | fhandle_t fh; | |
5680 | struct nfs_vattr *nvattrp; | |
5681 | struct nfs_dir_buf_header *ndbhp; | |
5682 | struct direntry *dp; | |
5683 | char *padstart, padlen; | |
5684 | struct timeval now; | |
1c79356b | 5685 | |
2d21ac55 | 5686 | nmp = NFSTONMP(dnp); |
fe8ab488 | 5687 | if (nfs_mount_gone(nmp)) |
55e303ae | 5688 | return (ENXIO); |
2d21ac55 | 5689 | nfsvers = nmp->nm_vers; |
55e303ae A |
5690 | nmreaddirsize = nmp->nm_readdirsize; |
5691 | nmrsize = nmp->nm_rsize; | |
b0d623f7 A |
5692 | bigcookies = nmp->nm_state & NFSSTA_BIGCOOKIES; |
5693 | noplus: | |
6d2010ae | 5694 | rdirplus = ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) ? 1 : 0; |
55e303ae | 5695 | |
b0d623f7 | 5696 | if ((lockerror = nfs_node_lock(dnp))) |
2d21ac55 A |
5697 | return (lockerror); |
5698 | ||
b0d623f7 A |
5699 | /* determine cookie to use, and move dp to the right offset */ |
5700 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5701 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5702 | if (ndbhp->ndbh_count) { | |
5703 | for (i=0; i < ndbhp->ndbh_count-1; i++) | |
5704 | dp = NFS_DIRENTRY_NEXT(dp); | |
5705 | cookie = dp->d_seekoff; | |
5706 | dp = NFS_DIRENTRY_NEXT(dp); | |
5707 | } else { | |
5708 | cookie = bp->nb_lblkno; | |
5709 | /* increment with every buffer read */ | |
316670eb | 5710 | OSAddAtomic64(1, &nfsstats.readdir_bios); |
2d21ac55 | 5711 | } |
b0d623f7 | 5712 | lastcookie = cookie; |
2d21ac55 | 5713 | |
1c79356b | 5714 | /* |
b0d623f7 A |
5715 | * Loop around doing readdir(plus) RPCs of size nm_readdirsize until |
5716 | * the buffer is full (or we hit EOF). Then put the remainder of the | |
5717 | * results in the next buffer(s). | |
1c79356b | 5718 | */ |
2d21ac55 A |
5719 | nfsm_chain_null(&nmreq); |
5720 | nfsm_chain_null(&nmrep); | |
b0d623f7 | 5721 | while (nfs_dir_buf_freespace(bp, rdirplus) && !(ndbhp->ndbh_flags & NDB_FULL)) { |
2d21ac55 | 5722 | nfsm_chain_build_alloc_init(error, &nmreq, |
b0d623f7 | 5723 | NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers) + NFSX_UNSIGNED); |
2d21ac55 | 5724 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
b0d623f7 A |
5725 | if (nfsvers == NFS_VER3) { |
5726 | /* opaque values don't need swapping, but as long */ | |
5727 | /* as we are consistent about it, it should be ok */ | |
5728 | nfsm_chain_add_64(error, &nmreq, cookie); | |
5729 | nfsm_chain_add_64(error, &nmreq, dnp->n_cookieverf); | |
5730 | } else { | |
5731 | nfsm_chain_add_32(error, &nmreq, cookie); | |
5732 | } | |
2d21ac55 | 5733 | nfsm_chain_add_32(error, &nmreq, nmreaddirsize); |
b0d623f7 A |
5734 | if (rdirplus) |
5735 | nfsm_chain_add_32(error, &nmreq, nmrsize); | |
2d21ac55 | 5736 | nfsm_chain_build_done(error, &nmreq); |
b0d623f7 | 5737 | nfs_node_unlock(dnp); |
2d21ac55 A |
5738 | lockerror = ENOENT; |
5739 | nfsmout_if(error); | |
5740 | ||
b0d623f7 A |
5741 | error = nfs_request(dnp, NULL, &nmreq, |
5742 | rdirplus ? NFSPROC_READDIRPLUS : NFSPROC_READDIR, | |
6d2010ae | 5743 | ctx, NULL, &nmrep, &xid, &status); |
2d21ac55 | 5744 | |
b0d623f7 | 5745 | if ((lockerror = nfs_node_lock(dnp))) |
2d21ac55 A |
5746 | error = lockerror; |
5747 | ||
b0d623f7 A |
5748 | savedxid = xid; |
5749 | if (nfsvers == NFS_VER3) | |
5750 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); | |
2d21ac55 A |
5751 | if (!error) |
5752 | error = status; | |
b0d623f7 A |
5753 | if (nfsvers == NFS_VER3) |
5754 | nfsm_chain_get_64(error, &nmrep, dnp->n_cookieverf); | |
5755 | nfsm_chain_get_32(error, &nmrep, more_entries); | |
2d21ac55 A |
5756 | |
5757 | if (!lockerror) { | |
b0d623f7 | 5758 | nfs_node_unlock(dnp); |
2d21ac55 | 5759 | lockerror = ENOENT; |
1c79356b | 5760 | } |
b0d623f7 A |
5761 | if (error == NFSERR_NOTSUPP) { |
5762 | /* oops... it doesn't look like readdirplus is supported */ | |
5763 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae | 5764 | NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS); |
b0d623f7 A |
5765 | lck_mtx_unlock(&nmp->nm_lock); |
5766 | goto noplus; | |
5767 | } | |
2d21ac55 | 5768 | nfsmout_if(error); |
1c79356b | 5769 | |
b0d623f7 A |
5770 | if (rdirplus) |
5771 | microuptime(&now); | |
5772 | ||
5773 | /* loop through the entries packing them into the buffer */ | |
5774 | while (more_entries) { | |
5775 | if (nfsvers == NFS_VER3) | |
5776 | nfsm_chain_get_64(error, &nmrep, fileno); | |
5777 | else | |
5778 | nfsm_chain_get_32(error, &nmrep, fileno); | |
5779 | nfsm_chain_get_32(error, &nmrep, namlen); | |
2d21ac55 | 5780 | nfsmout_if(error); |
b0d623f7 A |
5781 | /* just truncate names that don't fit in direntry.d_name */ |
5782 | if (namlen <= 0) { | |
1c79356b | 5783 | error = EBADRPC; |
1c79356b A |
5784 | goto nfsmout; |
5785 | } | |
b0d623f7 A |
5786 | if (namlen > (sizeof(dp->d_name)-1)) { |
5787 | skiplen = namlen - sizeof(dp->d_name) + 1; | |
5788 | namlen = sizeof(dp->d_name) - 1; | |
91447636 A |
5789 | } else { |
5790 | skiplen = 0; | |
5791 | } | |
b0d623f7 A |
5792 | /* guess that fh size will be same as parent */ |
5793 | fhlen = rdirplus ? (1 + dnp->n_fhsize) : 0; | |
5794 | xlen = rdirplus ? (fhlen + sizeof(time_t)) : 0; | |
5795 | attrlen = rdirplus ? sizeof(struct nfs_vattr) : 0; | |
5796 | reclen = NFS_DIRENTRY_LEN(namlen + xlen); | |
5797 | space_needed = reclen + attrlen; | |
5798 | space_free = nfs_dir_buf_freespace(bp, rdirplus); | |
5799 | if (space_needed > space_free) { | |
5800 | /* | |
5801 | * We still have entries to pack, but we've | |
5802 | * run out of room in the current buffer. | |
5803 | * So we need to move to the next buffer. | |
5804 | * The block# for the next buffer is the | |
5805 | * last cookie in the current buffer. | |
5806 | */ | |
5807 | nextbuffer: | |
5808 | ndbhp->ndbh_flags |= NDB_FULL; | |
5809 | nfs_buf_release(bp, 0); | |
5810 | bp_dropped = 1; | |
5811 | bp = NULL; | |
5812 | error = nfs_buf_get(dnp, lastcookie, NFS_DIRBLKSIZ, vfs_context_thread(ctx), NBLK_READ, &bp); | |
2d21ac55 | 5813 | nfsmout_if(error); |
b0d623f7 A |
5814 | /* initialize buffer */ |
5815 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5816 | ndbhp->ndbh_flags = 0; | |
5817 | ndbhp->ndbh_count = 0; | |
5818 | ndbhp->ndbh_entry_end = sizeof(*ndbhp); | |
5819 | ndbhp->ndbh_ncgen = dnp->n_ncgen; | |
5820 | space_free = nfs_dir_buf_freespace(bp, rdirplus); | |
5821 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5822 | /* increment with every buffer read */ | |
316670eb | 5823 | OSAddAtomic64(1, &nfsstats.readdir_bios); |
91447636 | 5824 | } |
b0d623f7 A |
5825 | nmrepsave = nmrep; |
5826 | dp->d_fileno = fileno; | |
5827 | dp->d_namlen = namlen; | |
5828 | dp->d_reclen = reclen; | |
5829 | dp->d_type = DT_UNKNOWN; | |
5830 | nfsm_chain_get_opaque(error, &nmrep, namlen, dp->d_name); | |
2d21ac55 | 5831 | nfsmout_if(error); |
b0d623f7 A |
5832 | dp->d_name[namlen] = '\0'; |
5833 | if (skiplen) | |
5834 | nfsm_chain_adv(error, &nmrep, | |
5835 | nfsm_rndup(namlen + skiplen) - nfsm_rndup(namlen)); | |
5836 | if (nfsvers == NFS_VER3) | |
5837 | nfsm_chain_get_64(error, &nmrep, cookie); | |
5838 | else | |
5839 | nfsm_chain_get_32(error, &nmrep, cookie); | |
5840 | nfsmout_if(error); | |
5841 | dp->d_seekoff = cookie; | |
5842 | if (!bigcookies && (cookie >> 32) && (nmp == NFSTONMP(dnp))) { | |
5843 | /* we've got a big cookie, make sure flag is set */ | |
5844 | lck_mtx_lock(&nmp->nm_lock); | |
5845 | nmp->nm_state |= NFSSTA_BIGCOOKIES; | |
5846 | lck_mtx_unlock(&nmp->nm_lock); | |
5847 | bigcookies = 1; | |
5848 | } | |
5849 | if (rdirplus) { | |
5850 | nvattrp = NFS_DIR_BUF_NVATTR(bp, ndbhp->ndbh_count); | |
5851 | /* check for attributes */ | |
5852 | nfsm_chain_get_32(error, &nmrep, attrflag); | |
2d21ac55 | 5853 | nfsmout_if(error); |
b0d623f7 A |
5854 | if (attrflag) { |
5855 | /* grab attributes */ | |
5856 | error = nfs_parsefattr(&nmrep, NFS_VER3, nvattrp); | |
5857 | nfsmout_if(error); | |
5858 | dp->d_type = IFTODT(VTTOIF(nvattrp->nva_type)); | |
5859 | /* fileid is already in d_fileno, so stash xid in attrs */ | |
5860 | nvattrp->nva_fileid = savedxid; | |
5861 | } else { | |
5862 | /* mark the attributes invalid */ | |
5863 | bzero(nvattrp, sizeof(struct nfs_vattr)); | |
5864 | } | |
5865 | /* check for file handle */ | |
5866 | nfsm_chain_get_32(error, &nmrep, fhflag); | |
5867 | nfsmout_if(error); | |
5868 | if (fhflag) { | |
5869 | nfsm_chain_get_fh(error, &nmrep, NFS_VER3, &fh); | |
5870 | nfsmout_if(error); | |
5871 | fhlen = fh.fh_len + 1; | |
5872 | xlen = fhlen + sizeof(time_t); | |
5873 | reclen = NFS_DIRENTRY_LEN(namlen + xlen); | |
5874 | space_needed = reclen + attrlen; | |
5875 | if (space_needed > space_free) { | |
5876 | /* didn't actually have the room... move on to next buffer */ | |
5877 | nmrep = nmrepsave; | |
5878 | goto nextbuffer; | |
2d21ac55 | 5879 | } |
b0d623f7 A |
5880 | /* pack the file handle into the record */ |
5881 | dp->d_name[dp->d_namlen+1] = fh.fh_len; | |
5882 | bcopy(fh.fh_data, &dp->d_name[dp->d_namlen+2], fh.fh_len); | |
1c79356b | 5883 | } else { |
b0d623f7 A |
5884 | /* mark the file handle invalid */ |
5885 | fh.fh_len = 0; | |
5886 | fhlen = fh.fh_len + 1; | |
5887 | xlen = fhlen + sizeof(time_t); | |
5888 | reclen = NFS_DIRENTRY_LEN(namlen + xlen); | |
5889 | bzero(&dp->d_name[dp->d_namlen+1], fhlen); | |
1c79356b | 5890 | } |
b0d623f7 A |
5891 | *(time_t*)(&dp->d_name[dp->d_namlen+1+fhlen]) = now.tv_sec; |
5892 | dp->d_reclen = reclen; | |
1c79356b | 5893 | } |
b0d623f7 A |
5894 | padstart = dp->d_name + dp->d_namlen + 1 + xlen; |
5895 | ndbhp->ndbh_count++; | |
5896 | lastcookie = cookie; | |
5897 | /* advance to next direntry in buffer */ | |
5898 | dp = NFS_DIRENTRY_NEXT(dp); | |
5899 | ndbhp->ndbh_entry_end = (char*)dp - bp->nb_data; | |
5900 | /* zero out the pad bytes */ | |
5901 | padlen = (char*)dp - padstart; | |
5902 | if (padlen > 0) | |
5903 | bzero(padstart, padlen); | |
5904 | /* check for more entries */ | |
5905 | nfsm_chain_get_32(error, &nmrep, more_entries); | |
2d21ac55 | 5906 | nfsmout_if(error); |
1c79356b | 5907 | } |
b0d623f7 A |
5908 | /* Finally, get the eof boolean */ |
5909 | nfsm_chain_get_32(error, &nmrep, eof); | |
5910 | nfsmout_if(error); | |
5911 | if (eof) { | |
5912 | ndbhp->ndbh_flags |= (NDB_FULL|NDB_EOF); | |
5913 | nfs_node_lock_force(dnp); | |
5914 | dnp->n_eofcookie = lastcookie; | |
5915 | nfs_node_unlock(dnp); | |
5916 | } else { | |
5917 | more_entries = 1; | |
5918 | } | |
5919 | if (bp_dropped) { | |
5920 | nfs_buf_release(bp, 0); | |
5921 | bp = NULL; | |
5922 | break; | |
1c79356b | 5923 | } |
b0d623f7 | 5924 | if ((lockerror = nfs_node_lock(dnp))) |
2d21ac55 A |
5925 | error = lockerror; |
5926 | nfsmout_if(error); | |
5927 | nfsm_chain_cleanup(&nmrep); | |
5928 | nfsm_chain_null(&nmreq); | |
5929 | } | |
1c79356b | 5930 | nfsmout: |
b0d623f7 A |
5931 | if (bp_dropped && bp) |
5932 | nfs_buf_release(bp, 0); | |
2d21ac55 | 5933 | if (!lockerror) |
b0d623f7 | 5934 | nfs_node_unlock(dnp); |
2d21ac55 A |
5935 | nfsm_chain_cleanup(&nmreq); |
5936 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 | 5937 | return (bp_dropped ? NFSERR_DIRBUFDROPPED : error); |
1c79356b A |
5938 | } |
5939 | ||
5940 | /* | |
5941 | * Silly rename. To make the NFS filesystem that is stateless look a little | |
5942 | * more like the "ufs" a remove of an active vnode is translated to a rename | |
2d21ac55 | 5943 | * to a funny looking filename that is removed by nfs_vnop_inactive on the |
1c79356b | 5944 | * nfsnode. There is the potential for another process on a different client |
2d21ac55 A |
5945 | * to create the same funny name between when the lookitup() fails and the |
5946 | * rename() completes, but... | |
1c79356b | 5947 | */ |
55e303ae | 5948 | |
2d21ac55 A |
5949 | /* format of "random" silly names - includes a number and pid */ |
5950 | /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */ | |
5951 | #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x" | |
5952 | /* starting from zero isn't silly enough */ | |
5953 | static uint32_t nfs_sillyrename_number = 0x20051025; | |
55e303ae | 5954 | |
b0d623f7 | 5955 | int |
91447636 | 5956 | nfs_sillyrename( |
2d21ac55 A |
5957 | nfsnode_t dnp, |
5958 | nfsnode_t np, | |
91447636 | 5959 | struct componentname *cnp, |
2d21ac55 | 5960 | vfs_context_t ctx) |
1c79356b | 5961 | { |
2d21ac55 | 5962 | struct nfs_sillyrename *nsp; |
1c79356b A |
5963 | int error; |
5964 | short pid; | |
2d21ac55 A |
5965 | kauth_cred_t cred; |
5966 | uint32_t num; | |
5967 | struct nfsmount *nmp; | |
1c79356b | 5968 | |
2d21ac55 | 5969 | nmp = NFSTONMP(dnp); |
fe8ab488 | 5970 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
5971 | return (ENXIO); |
5972 | ||
b0d623f7 | 5973 | nfs_name_cache_purge(dnp, np, cnp, ctx); |
2d21ac55 A |
5974 | |
5975 | MALLOC_ZONE(nsp, struct nfs_sillyrename *, | |
5976 | sizeof (struct nfs_sillyrename), M_NFSREQ, M_WAITOK); | |
5977 | if (!nsp) | |
91447636 | 5978 | return (ENOMEM); |
2d21ac55 | 5979 | cred = vfs_context_ucred(ctx); |
91447636 | 5980 | kauth_cred_ref(cred); |
2d21ac55 A |
5981 | nsp->nsr_cred = cred; |
5982 | nsp->nsr_dnp = dnp; | |
5983 | error = vnode_ref(NFSTOV(dnp)); | |
91447636 A |
5984 | if (error) |
5985 | goto bad_norele; | |
1c79356b A |
5986 | |
5987 | /* Fudge together a funny name */ | |
2d21ac55 | 5988 | pid = vfs_context_pid(ctx); |
b0d623f7 | 5989 | num = OSAddAtomic(1, &nfs_sillyrename_number); |
2d21ac55 A |
5990 | nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name), |
5991 | NFS_SILLYNAME_FORMAT, num, (pid & 0xffff)); | |
5992 | if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name)) | |
5993 | nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1; | |
1c79356b A |
5994 | |
5995 | /* Try lookitups until we get one that isn't there */ | |
2d21ac55 | 5996 | while (nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, NULL) == 0) { |
b0d623f7 | 5997 | num = OSAddAtomic(1, &nfs_sillyrename_number); |
2d21ac55 A |
5998 | nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name), |
5999 | NFS_SILLYNAME_FORMAT, num, (pid & 0xffff)); | |
6000 | if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name)) | |
6001 | nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1; | |
55e303ae | 6002 | } |
2d21ac55 | 6003 | |
55e303ae | 6004 | /* now, do the rename */ |
2d21ac55 A |
6005 | error = nmp->nm_funcs->nf_rename_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen, |
6006 | dnp, nsp->nsr_name, nsp->nsr_namlen, ctx); | |
6d2010ae A |
6007 | |
6008 | /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */ | |
6009 | if (error == ENOENT) | |
6010 | error = 0; | |
b0d623f7 A |
6011 | if (!error) { |
6012 | nfs_node_lock_force(dnp); | |
6013 | if (dnp->n_flag & NNEGNCENTRIES) { | |
6014 | dnp->n_flag &= ~NNEGNCENTRIES; | |
6015 | cache_purge_negatives(NFSTOV(dnp)); | |
6016 | } | |
6017 | nfs_node_unlock(dnp); | |
2d21ac55 A |
6018 | } |
6019 | FSDBG(267, dnp, np, num, error); | |
91447636 | 6020 | if (error) |
1c79356b | 6021 | goto bad; |
2d21ac55 | 6022 | error = nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, &np); |
b0d623f7 | 6023 | nfs_node_lock_force(np); |
2d21ac55 | 6024 | np->n_sillyrename = nsp; |
b0d623f7 | 6025 | nfs_node_unlock(np); |
1c79356b A |
6026 | return (0); |
6027 | bad: | |
2d21ac55 | 6028 | vnode_rele(NFSTOV(dnp)); |
91447636 | 6029 | bad_norele: |
2d21ac55 A |
6030 | nsp->nsr_cred = NOCRED; |
6031 | kauth_cred_unref(&cred); | |
6032 | FREE_ZONE(nsp, sizeof(*nsp), M_NFSREQ); | |
6033 | return (error); | |
6034 | } | |
6035 | ||
6036 | int | |
6037 | nfs3_lookup_rpc_async( | |
6038 | nfsnode_t dnp, | |
6039 | char *name, | |
6040 | int namelen, | |
6041 | vfs_context_t ctx, | |
6042 | struct nfsreq **reqp) | |
6043 | { | |
6044 | struct nfsmount *nmp; | |
6045 | struct nfsm_chain nmreq; | |
6046 | int error = 0, nfsvers; | |
6047 | ||
6048 | nmp = NFSTONMP(dnp); | |
fe8ab488 | 6049 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
6050 | return (ENXIO); |
6051 | nfsvers = nmp->nm_vers; | |
6052 | ||
6053 | nfsm_chain_null(&nmreq); | |
6054 | ||
6055 | nfsm_chain_build_alloc_init(error, &nmreq, | |
6056 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen)); | |
6057 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); | |
6d2010ae | 6058 | nfsm_chain_add_name(error, &nmreq, name, namelen, nmp); |
2d21ac55 A |
6059 | nfsm_chain_build_done(error, &nmreq); |
6060 | nfsmout_if(error); | |
6061 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_LOOKUP, | |
6d2010ae | 6062 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, reqp); |
2d21ac55 A |
6063 | nfsmout: |
6064 | nfsm_chain_cleanup(&nmreq); | |
6065 | return (error); | |
6066 | } | |
6067 | ||
6068 | int | |
6069 | nfs3_lookup_rpc_async_finish( | |
6070 | nfsnode_t dnp, | |
6d2010ae A |
6071 | __unused char *name, |
6072 | __unused int namelen, | |
2d21ac55 A |
6073 | vfs_context_t ctx, |
6074 | struct nfsreq *req, | |
6075 | u_int64_t *xidp, | |
6076 | fhandle_t *fhp, | |
6077 | struct nfs_vattr *nvap) | |
6078 | { | |
b0d623f7 | 6079 | int error = 0, lockerror = ENOENT, status, nfsvers, attrflag; |
2d21ac55 A |
6080 | u_int64_t xid; |
6081 | struct nfsmount *nmp; | |
6082 | struct nfsm_chain nmrep; | |
6083 | ||
6084 | nmp = NFSTONMP(dnp); | |
3e170ce0 A |
6085 | if (nmp == NULL) |
6086 | return (ENXIO); | |
2d21ac55 A |
6087 | nfsvers = nmp->nm_vers; |
6088 | ||
6089 | nfsm_chain_null(&nmrep); | |
6090 | ||
6091 | error = nfs_request_async_finish(req, &nmrep, xidp, &status); | |
6092 | ||
b0d623f7 A |
6093 | if ((lockerror = nfs_node_lock(dnp))) |
6094 | error = lockerror; | |
2d21ac55 A |
6095 | xid = *xidp; |
6096 | if (error || status) { | |
6097 | if (nfsvers == NFS_VER3) | |
6098 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); | |
6099 | if (!error) | |
6100 | error = status; | |
6101 | goto nfsmout; | |
6102 | } | |
6103 | ||
6104 | nfsmout_if(error || !fhp || !nvap); | |
6105 | ||
6106 | /* get the file handle */ | |
6107 | nfsm_chain_get_fh(error, &nmrep, nfsvers, fhp); | |
6108 | ||
6109 | /* get the attributes */ | |
6110 | if (nfsvers == NFS_VER3) { | |
6111 | nfsm_chain_postop_attr_get(error, &nmrep, attrflag, nvap); | |
6112 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); | |
6113 | if (!error && !attrflag) | |
6d2010ae | 6114 | error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp); |
2d21ac55 A |
6115 | } else { |
6116 | error = nfs_parsefattr(&nmrep, nfsvers, nvap); | |
6117 | } | |
6118 | nfsmout: | |
b0d623f7 A |
6119 | if (!lockerror) |
6120 | nfs_node_unlock(dnp); | |
2d21ac55 | 6121 | nfsm_chain_cleanup(&nmrep); |
1c79356b A |
6122 | return (error); |
6123 | } | |
6124 | ||
6125 | /* | |
6126 | * Look up a file name and optionally either update the file handle or | |
6127 | * allocate an nfsnode, depending on the value of npp. | |
6128 | * npp == NULL --> just do the lookup | |
6129 | * *npp == NULL --> allocate a new nfsnode and make sure attributes are | |
6130 | * handled too | |
6131 | * *npp != NULL --> update the file handle in the vnode | |
6132 | */ | |
2d21ac55 A |
6133 | int |
6134 | nfs_lookitup( | |
6135 | nfsnode_t dnp, | |
6136 | char *name, | |
6137 | int namelen, | |
6138 | vfs_context_t ctx, | |
6139 | nfsnode_t *npp) | |
6140 | { | |
6141 | int error = 0; | |
6142 | nfsnode_t np, newnp = NULL; | |
6143 | u_int64_t xid; | |
6144 | fhandle_t fh; | |
6145 | struct nfsmount *nmp; | |
6146 | struct nfs_vattr nvattr; | |
6147 | struct nfsreq rq, *req = &rq; | |
6148 | ||
6149 | nmp = NFSTONMP(dnp); | |
fe8ab488 | 6150 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
6151 | return (ENXIO); |
6152 | ||
6153 | if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) && | |
b0d623f7 | 6154 | (namelen > (int)nmp->nm_fsattr.nfsa_maxname)) |
2d21ac55 A |
6155 | return (ENAMETOOLONG); |
6156 | ||
6d2010ae A |
6157 | NVATTR_INIT(&nvattr); |
6158 | ||
2d21ac55 A |
6159 | /* check for lookup of "." */ |
6160 | if ((name[0] == '.') && (namelen == 1)) { | |
6161 | /* skip lookup, we know who we are */ | |
6162 | fh.fh_len = 0; | |
6163 | newnp = dnp; | |
6164 | goto nfsmout; | |
6165 | } | |
6166 | ||
6167 | error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, name, namelen, ctx, &req); | |
6168 | nfsmout_if(error); | |
6d2010ae | 6169 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, name, namelen, ctx, req, &xid, &fh, &nvattr); |
2d21ac55 A |
6170 | nfsmout_if(!npp || error); |
6171 | ||
6172 | if (*npp) { | |
6173 | np = *npp; | |
6174 | if (fh.fh_len != np->n_fhsize) { | |
6175 | u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL; | |
6176 | if (fh.fh_len > NFS_SMALLFH) { | |
6177 | MALLOC_ZONE(np->n_fhp, u_char *, fh.fh_len, M_NFSBIGFH, M_WAITOK); | |
6178 | if (!np->n_fhp) { | |
6179 | np->n_fhp = oldbuf; | |
6180 | error = ENOMEM; | |
6181 | goto nfsmout; | |
6182 | } | |
6183 | } else { | |
6184 | np->n_fhp = &np->n_fh[0]; | |
6185 | } | |
6186 | if (oldbuf) | |
6187 | FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH); | |
6188 | } | |
6189 | bcopy(fh.fh_data, np->n_fhp, fh.fh_len); | |
6190 | np->n_fhsize = fh.fh_len; | |
b0d623f7 | 6191 | nfs_node_lock_force(np); |
2d21ac55 | 6192 | error = nfs_loadattrcache(np, &nvattr, &xid, 0); |
b0d623f7 | 6193 | nfs_node_unlock(np); |
2d21ac55 A |
6194 | nfsmout_if(error); |
6195 | newnp = np; | |
6196 | } else if (NFS_CMPFH(dnp, fh.fh_data, fh.fh_len)) { | |
b0d623f7 | 6197 | nfs_node_lock_force(dnp); |
2d21ac55 A |
6198 | if (dnp->n_xid <= xid) |
6199 | error = nfs_loadattrcache(dnp, &nvattr, &xid, 0); | |
b0d623f7 | 6200 | nfs_node_unlock(dnp); |
2d21ac55 A |
6201 | nfsmout_if(error); |
6202 | newnp = dnp; | |
6203 | } else { | |
6204 | struct componentname cn, *cnp = &cn; | |
6205 | bzero(cnp, sizeof(*cnp)); | |
6206 | cnp->cn_nameptr = name; | |
6207 | cnp->cn_namelen = namelen; | |
6208 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, | |
6d2010ae | 6209 | &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
2d21ac55 A |
6210 | nfsmout_if(error); |
6211 | newnp = np; | |
6212 | } | |
6213 | ||
6214 | nfsmout: | |
6215 | if (npp && !*npp && !error) | |
6216 | *npp = newnp; | |
6d2010ae | 6217 | NVATTR_CLEANUP(&nvattr); |
2d21ac55 A |
6218 | return (error); |
6219 | } | |
6220 | ||
6221 | /* | |
6222 | * set up and initialize a "._" file lookup structure used for | |
6223 | * performing async lookups. | |
6224 | */ | |
6225 | void | |
b0d623f7 | 6226 | nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen, vfs_context_t ctx) |
2d21ac55 A |
6227 | { |
6228 | int error, du_namelen; | |
6229 | vnode_t du_vp; | |
6d2010ae | 6230 | struct nfsmount *nmp = NFSTONMP(dnp); |
2d21ac55 A |
6231 | |
6232 | /* check for ._ file in name cache */ | |
6233 | dulp->du_flags = 0; | |
6234 | bzero(&dulp->du_cn, sizeof(dulp->du_cn)); | |
6235 | du_namelen = namelen + 2; | |
6d2010ae A |
6236 | if (!nmp || NMFLAG(nmp, NONEGNAMECACHE)) |
6237 | return; | |
2d21ac55 A |
6238 | if ((namelen >= 2) && (name[0] == '.') && (name[1] == '_')) |
6239 | return; | |
6240 | if (du_namelen >= (int)sizeof(dulp->du_smallname)) | |
6241 | MALLOC(dulp->du_cn.cn_nameptr, char *, du_namelen + 1, M_TEMP, M_WAITOK); | |
6242 | else | |
6243 | dulp->du_cn.cn_nameptr = dulp->du_smallname; | |
6244 | if (!dulp->du_cn.cn_nameptr) | |
6245 | return; | |
6246 | dulp->du_cn.cn_namelen = du_namelen; | |
6247 | snprintf(dulp->du_cn.cn_nameptr, du_namelen + 1, "._%s", name); | |
6248 | dulp->du_cn.cn_nameptr[du_namelen] = '\0'; | |
b0d623f7 A |
6249 | dulp->du_cn.cn_nameiop = LOOKUP; |
6250 | dulp->du_cn.cn_flags = MAKEENTRY; | |
2d21ac55 A |
6251 | |
6252 | error = cache_lookup(NFSTOV(dnp), &du_vp, &dulp->du_cn); | |
b0d623f7 | 6253 | if (error == -1) { |
2d21ac55 | 6254 | vnode_put(du_vp); |
b0d623f7 | 6255 | } else if (!error) { |
6d2010ae A |
6256 | nmp = NFSTONMP(dnp); |
6257 | if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { | |
b0d623f7 A |
6258 | /* if rdirplus, try dir buf cache lookup */ |
6259 | nfsnode_t du_np = NULL; | |
6260 | if (!nfs_dir_buf_cache_lookup(dnp, &du_np, &dulp->du_cn, ctx, 0) && du_np) { | |
6261 | /* dir buf cache hit */ | |
6262 | du_vp = NFSTOV(du_np); | |
6263 | vnode_put(du_vp); | |
6264 | error = -1; | |
6265 | } | |
6266 | } | |
6267 | if (!error) | |
6268 | dulp->du_flags |= NFS_DULOOKUP_DOIT; | |
6269 | } | |
2d21ac55 A |
6270 | } |
6271 | ||
6272 | /* | |
6273 | * start an async "._" file lookup request | |
6274 | */ | |
6275 | void | |
6276 | nfs_dulookup_start(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx) | |
6277 | { | |
6278 | struct nfsmount *nmp = NFSTONMP(dnp); | |
6279 | struct nfsreq *req = &dulp->du_req; | |
6280 | ||
6d2010ae | 6281 | if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT) || (dulp->du_flags & NFS_DULOOKUP_INPROG)) |
2d21ac55 A |
6282 | return; |
6283 | if (!nmp->nm_funcs->nf_lookup_rpc_async(dnp, dulp->du_cn.cn_nameptr, | |
6284 | dulp->du_cn.cn_namelen, ctx, &req)) | |
6285 | dulp->du_flags |= NFS_DULOOKUP_INPROG; | |
6286 | } | |
6287 | ||
6288 | /* | |
6289 | * finish an async "._" file lookup request and clean up the structure | |
6290 | */ | |
6291 | void | |
6292 | nfs_dulookup_finish(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx) | |
1c79356b | 6293 | { |
2d21ac55 A |
6294 | struct nfsmount *nmp = NFSTONMP(dnp); |
6295 | int error; | |
6296 | nfsnode_t du_np; | |
6297 | u_int64_t xid; | |
6298 | fhandle_t fh; | |
91447636 | 6299 | struct nfs_vattr nvattr; |
1c79356b | 6300 | |
2d21ac55 A |
6301 | if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_INPROG)) |
6302 | goto out; | |
55e303ae | 6303 | |
6d2010ae A |
6304 | NVATTR_INIT(&nvattr); |
6305 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, dulp->du_cn.cn_nameptr, | |
6306 | dulp->du_cn.cn_namelen, ctx, &dulp->du_req, &xid, &fh, &nvattr); | |
2d21ac55 A |
6307 | dulp->du_flags &= ~NFS_DULOOKUP_INPROG; |
6308 | if (error == ENOENT) { | |
6309 | /* add a negative entry in the name cache */ | |
b0d623f7 | 6310 | nfs_node_lock_force(dnp); |
2d21ac55 A |
6311 | cache_enter(NFSTOV(dnp), NULL, &dulp->du_cn); |
6312 | dnp->n_flag |= NNEGNCENTRIES; | |
b0d623f7 | 6313 | nfs_node_unlock(dnp); |
2d21ac55 A |
6314 | } else if (!error) { |
6315 | error = nfs_nget(NFSTOMP(dnp), dnp, &dulp->du_cn, fh.fh_data, fh.fh_len, | |
6d2010ae | 6316 | &nvattr, &xid, dulp->du_req.r_auth, NG_MAKEENTRY, &du_np); |
2d21ac55 | 6317 | if (!error) { |
b0d623f7 | 6318 | nfs_node_unlock(du_np); |
2d21ac55 | 6319 | vnode_put(NFSTOV(du_np)); |
1c79356b | 6320 | } |
1c79356b | 6321 | } |
6d2010ae | 6322 | NVATTR_CLEANUP(&nvattr); |
2d21ac55 A |
6323 | out: |
6324 | if (dulp->du_flags & NFS_DULOOKUP_INPROG) | |
6325 | nfs_request_async_cancel(&dulp->du_req); | |
6326 | if (dulp->du_cn.cn_nameptr && (dulp->du_cn.cn_nameptr != dulp->du_smallname)) | |
6327 | FREE(dulp->du_cn.cn_nameptr, M_TEMP); | |
1c79356b A |
6328 | } |
6329 | ||
2d21ac55 | 6330 | |
1c79356b | 6331 | /* |
2d21ac55 | 6332 | * NFS Version 3 commit RPC |
1c79356b | 6333 | */ |
55e303ae | 6334 | int |
2d21ac55 A |
6335 | nfs3_commit_rpc( |
6336 | nfsnode_t np, | |
6d2010ae A |
6337 | uint64_t offset, |
6338 | uint64_t count, | |
6339 | kauth_cred_t cred, | |
6340 | uint64_t wverf) | |
1c79356b | 6341 | { |
2d21ac55 A |
6342 | struct nfsmount *nmp; |
6343 | int error = 0, lockerror, status, wccpostattr = 0, nfsvers; | |
91447636 | 6344 | struct timespec premtime = { 0, 0 }; |
6d2010ae | 6345 | u_int64_t xid, newwverf; |
2d21ac55 A |
6346 | uint32_t count32; |
6347 | struct nfsm_chain nmreq, nmrep; | |
8f6c56a5 | 6348 | |
2d21ac55 A |
6349 | nmp = NFSTONMP(np); |
6350 | FSDBG(521, np, offset, count, nmp ? nmp->nm_state : 0); | |
fe8ab488 | 6351 | if (nfs_mount_gone(nmp)) |
55e303ae | 6352 | return (ENXIO); |
2d21ac55 | 6353 | if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) |
1c79356b | 6354 | return (0); |
2d21ac55 A |
6355 | nfsvers = nmp->nm_vers; |
6356 | ||
6357 | if (count > UINT32_MAX) | |
6358 | count32 = 0; | |
6359 | else | |
6360 | count32 = count; | |
6361 | ||
6362 | nfsm_chain_null(&nmreq); | |
6363 | nfsm_chain_null(&nmrep); | |
6364 | ||
6365 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3)); | |
6366 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
6367 | nfsm_chain_add_64(error, &nmreq, offset); | |
6368 | nfsm_chain_add_32(error, &nmreq, count32); | |
6369 | nfsm_chain_build_done(error, &nmreq); | |
6370 | nfsmout_if(error); | |
6371 | error = nfs_request2(np, NULL, &nmreq, NFSPROC_COMMIT, | |
6d2010ae | 6372 | current_thread(), cred, NULL, 0, &nmrep, &xid, &status); |
b0d623f7 | 6373 | if ((lockerror = nfs_node_lock(np))) |
2d21ac55 A |
6374 | error = lockerror; |
6375 | /* can we do anything useful with the wcc info? */ | |
6376 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); | |
6377 | if (!lockerror) | |
b0d623f7 | 6378 | nfs_node_unlock(np); |
2d21ac55 A |
6379 | if (!error) |
6380 | error = status; | |
6d2010ae | 6381 | nfsm_chain_get_64(error, &nmrep, newwverf); |
2d21ac55 A |
6382 | nfsmout_if(error); |
6383 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
6384 | if (nmp->nm_verf != newwverf) |
6385 | nmp->nm_verf = newwverf; | |
6386 | if (wverf != newwverf) | |
2d21ac55 | 6387 | error = NFSERR_STALEWRITEVERF; |
2d21ac55 A |
6388 | lck_mtx_unlock(&nmp->nm_lock); |
6389 | nfsmout: | |
6390 | nfsm_chain_cleanup(&nmreq); | |
6391 | nfsm_chain_cleanup(&nmrep); | |
1c79356b A |
6392 | return (error); |
6393 | } | |
6394 | ||
2d21ac55 | 6395 | |
b0d623f7 | 6396 | int |
2d21ac55 | 6397 | nfs_vnop_blockmap( |
91447636 A |
6398 | __unused struct vnop_blockmap_args /* { |
6399 | struct vnodeop_desc *a_desc; | |
6400 | vnode_t a_vp; | |
6401 | off_t a_foffset; | |
6402 | size_t a_size; | |
6403 | daddr64_t *a_bpn; | |
6404 | size_t *a_run; | |
6405 | void *a_poff; | |
6406 | int a_flags; | |
6407 | } */ *ap) | |
1c79356b | 6408 | { |
91447636 | 6409 | return (ENOTSUP); |
1c79356b A |
6410 | } |
6411 | ||
1c79356b A |
6412 | |
6413 | /* | |
2d21ac55 | 6414 | * fsync vnode op. Just call nfs_flush(). |
1c79356b A |
6415 | */ |
6416 | /* ARGSUSED */ | |
b0d623f7 | 6417 | int |
2d21ac55 | 6418 | nfs_vnop_fsync( |
91447636 | 6419 | struct vnop_fsync_args /* { |
1c79356b | 6420 | struct vnodeop_desc *a_desc; |
91447636 A |
6421 | vnode_t a_vp; |
6422 | int a_waitfor; | |
6423 | vfs_context_t a_context; | |
2d21ac55 | 6424 | } */ *ap) |
1c79356b | 6425 | { |
2d21ac55 | 6426 | return (nfs_flush(VTONFS(ap->a_vp), ap->a_waitfor, vfs_context_thread(ap->a_context), 0)); |
1c79356b | 6427 | } |
1c79356b | 6428 | |
1c79356b | 6429 | |
55e303ae | 6430 | /* |
2d21ac55 | 6431 | * Do an NFS pathconf RPC. |
55e303ae | 6432 | */ |
91447636 | 6433 | int |
2d21ac55 A |
6434 | nfs3_pathconf_rpc( |
6435 | nfsnode_t np, | |
6436 | struct nfs_fsattr *nfsap, | |
6437 | vfs_context_t ctx) | |
55e303ae | 6438 | { |
2d21ac55 A |
6439 | u_int64_t xid; |
6440 | int error = 0, lockerror, status, nfsvers; | |
6441 | struct nfsm_chain nmreq, nmrep; | |
6442 | struct nfsmount *nmp = NFSTONMP(np); | |
6443 | uint32_t val = 0; | |
483a1d10 | 6444 | |
fe8ab488 | 6445 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
6446 | return (ENXIO); |
6447 | nfsvers = nmp->nm_vers; | |
1c79356b | 6448 | |
2d21ac55 A |
6449 | nfsm_chain_null(&nmreq); |
6450 | nfsm_chain_null(&nmrep); | |
1c79356b | 6451 | |
91447636 | 6452 | /* fetch pathconf info from server */ |
2d21ac55 A |
6453 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3)); |
6454 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
6455 | nfsm_chain_build_done(error, &nmreq); | |
6456 | nfsmout_if(error); | |
6d2010ae | 6457 | error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx, NULL, &nmrep, &xid, &status); |
b0d623f7 | 6458 | if ((lockerror = nfs_node_lock(np))) |
2d21ac55 A |
6459 | error = lockerror; |
6460 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); | |
6461 | if (!lockerror) | |
b0d623f7 | 6462 | nfs_node_unlock(np); |
2d21ac55 A |
6463 | if (!error) |
6464 | error = status; | |
6465 | nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxlink); | |
6466 | nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxname); | |
fe8ab488 | 6467 | nfsap->nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC|NFS_FSFLAG_CHOWN_RESTRICTED|NFS_FSFLAG_CASE_INSENSITIVE|NFS_FSFLAG_CASE_PRESERVING); |
2d21ac55 A |
6468 | nfsm_chain_get_32(error, &nmrep, val); |
6469 | if (val) | |
6470 | nfsap->nfsa_flags |= NFS_FSFLAG_NO_TRUNC; | |
6471 | nfsm_chain_get_32(error, &nmrep, val); | |
6472 | if (val) | |
6473 | nfsap->nfsa_flags |= NFS_FSFLAG_CHOWN_RESTRICTED; | |
6474 | nfsm_chain_get_32(error, &nmrep, val); | |
6475 | if (val) | |
6476 | nfsap->nfsa_flags |= NFS_FSFLAG_CASE_INSENSITIVE; | |
6477 | nfsm_chain_get_32(error, &nmrep, val); | |
6478 | if (val) | |
6479 | nfsap->nfsa_flags |= NFS_FSFLAG_CASE_PRESERVING; | |
6480 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK); | |
6481 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME); | |
6482 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC); | |
6483 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED); | |
6484 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE); | |
6485 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING); | |
6486 | nfsmout: | |
6487 | nfsm_chain_cleanup(&nmreq); | |
6488 | nfsm_chain_cleanup(&nmrep); | |
91447636 | 6489 | return (error); |
1c79356b A |
6490 | } |
6491 | ||
2d21ac55 | 6492 | /* save pathconf info for NFSv3 mount */ |
91447636 | 6493 | void |
2d21ac55 | 6494 | nfs3_pathconf_cache(struct nfsmount *nmp, struct nfs_fsattr *nfsap) |
1c79356b | 6495 | { |
2d21ac55 A |
6496 | nmp->nm_fsattr.nfsa_maxlink = nfsap->nfsa_maxlink; |
6497 | nmp->nm_fsattr.nfsa_maxname = nfsap->nfsa_maxname; | |
fe8ab488 | 6498 | nmp->nm_fsattr.nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC|NFS_FSFLAG_CHOWN_RESTRICTED|NFS_FSFLAG_CASE_INSENSITIVE|NFS_FSFLAG_CASE_PRESERVING); |
2d21ac55 A |
6499 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC; |
6500 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED; | |
6501 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE; | |
6502 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING; | |
6503 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXLINK); | |
6504 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME); | |
6505 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_NO_TRUNC); | |
6506 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED); | |
6507 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE); | |
6508 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING); | |
91447636 | 6509 | nmp->nm_state |= NFSSTA_GOTPATHCONF; |
1c79356b A |
6510 | } |
6511 | ||
6512 | /* | |
91447636 A |
6513 | * Return POSIX pathconf information applicable to nfs. |
6514 | * | |
6515 | * The NFS V2 protocol doesn't support this, so just return EINVAL | |
6516 | * for V2. | |
1c79356b | 6517 | */ |
91447636 | 6518 | /* ARGSUSED */ |
b0d623f7 | 6519 | int |
2d21ac55 | 6520 | nfs_vnop_pathconf( |
91447636 A |
6521 | struct vnop_pathconf_args /* { |
6522 | struct vnodeop_desc *a_desc; | |
6523 | vnode_t a_vp; | |
6524 | int a_name; | |
b0d623f7 | 6525 | int32_t *a_retval; |
91447636 | 6526 | vfs_context_t a_context; |
2d21ac55 | 6527 | } */ *ap) |
1c79356b | 6528 | { |
91447636 | 6529 | vnode_t vp = ap->a_vp; |
2d21ac55 | 6530 | nfsnode_t np = VTONFS(vp); |
91447636 | 6531 | struct nfsmount *nmp; |
2d21ac55 A |
6532 | struct nfs_fsattr nfsa, *nfsap; |
6533 | int error = 0; | |
6534 | uint64_t maxFileSize; | |
6535 | uint nbits; | |
1c79356b | 6536 | |
2d21ac55 | 6537 | nmp = VTONMP(vp); |
fe8ab488 | 6538 | if (nfs_mount_gone(nmp)) |
91447636 | 6539 | return (ENXIO); |
1c79356b | 6540 | |
91447636 A |
6541 | switch (ap->a_name) { |
6542 | case _PC_LINK_MAX: | |
6543 | case _PC_NAME_MAX: | |
6544 | case _PC_CHOWN_RESTRICTED: | |
6545 | case _PC_NO_TRUNC: | |
6546 | case _PC_CASE_SENSITIVE: | |
6547 | case _PC_CASE_PRESERVING: | |
6548 | break; | |
2d21ac55 A |
6549 | case _PC_FILESIZEBITS: |
6550 | if (nmp->nm_vers == NFS_VER2) { | |
6551 | *ap->a_retval = 32; | |
6552 | return (0); | |
6553 | } | |
6554 | break; | |
6d2010ae A |
6555 | case _PC_XATTR_SIZE_BITS: |
6556 | /* Do we support xattrs natively? */ | |
6557 | if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) | |
6558 | break; /* Yes */ | |
6559 | /* No... so just return an error */ | |
6560 | /* FALLTHROUGH */ | |
91447636 A |
6561 | default: |
6562 | /* don't bother contacting the server if we know the answer */ | |
6563 | return (EINVAL); | |
6564 | } | |
1c79356b | 6565 | |
2d21ac55 A |
6566 | if (nmp->nm_vers == NFS_VER2) |
6567 | return (EINVAL); | |
6568 | ||
6569 | lck_mtx_lock(&nmp->nm_lock); | |
6570 | if (nmp->nm_vers == NFS_VER3) { | |
6571 | if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) { | |
6572 | /* no pathconf info cached */ | |
6573 | lck_mtx_unlock(&nmp->nm_lock); | |
6574 | NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap); | |
6575 | error = nfs3_pathconf_rpc(np, &nfsa, ap->a_context); | |
6576 | if (error) | |
6577 | return (error); | |
6578 | nmp = VTONMP(vp); | |
fe8ab488 | 6579 | if (nfs_mount_gone(nmp)) |
2d21ac55 A |
6580 | return (ENXIO); |
6581 | lck_mtx_lock(&nmp->nm_lock); | |
6582 | if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS) { | |
6583 | /* all files have the same pathconf info, */ | |
6584 | /* so cache a copy of the results */ | |
6585 | nfs3_pathconf_cache(nmp, &nfsa); | |
6586 | } | |
6587 | nfsap = &nfsa; | |
6588 | } else { | |
6589 | nfsap = &nmp->nm_fsattr; | |
6590 | } | |
6591 | } else if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) { | |
91447636 | 6592 | /* no pathconf info cached */ |
2d21ac55 A |
6593 | lck_mtx_unlock(&nmp->nm_lock); |
6594 | NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap); | |
6595 | error = nfs4_pathconf_rpc(np, &nfsa, ap->a_context); | |
91447636 A |
6596 | if (error) |
6597 | return (error); | |
2d21ac55 | 6598 | nmp = VTONMP(vp); |
fe8ab488 | 6599 | if (nfs_mount_gone(nmp)) |
91447636 | 6600 | return (ENXIO); |
2d21ac55 A |
6601 | lck_mtx_lock(&nmp->nm_lock); |
6602 | nfsap = &nfsa; | |
6603 | } else { | |
6604 | nfsap = &nmp->nm_fsattr; | |
91447636 | 6605 | } |
1c79356b | 6606 | |
91447636 A |
6607 | switch (ap->a_name) { |
6608 | case _PC_LINK_MAX: | |
2d21ac55 A |
6609 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK)) |
6610 | *ap->a_retval = nfsap->nfsa_maxlink; | |
6611 | else if ((nmp->nm_vers == NFS_VER4) && NFS_BITMAP_ISSET(np->n_vattr.nva_bitmap, NFS_FATTR_MAXLINK)) | |
6612 | *ap->a_retval = np->n_vattr.nva_maxlink; | |
91447636 | 6613 | else |
2d21ac55 | 6614 | error = EINVAL; |
91447636 | 6615 | break; |
2d21ac55 A |
6616 | case _PC_NAME_MAX: |
6617 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME)) | |
6618 | *ap->a_retval = nfsap->nfsa_maxname; | |
6619 | else | |
6620 | error = EINVAL; | |
6621 | break; | |
6622 | case _PC_CHOWN_RESTRICTED: | |
6623 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED)) | |
b0d623f7 | 6624 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0; |
2d21ac55 A |
6625 | else |
6626 | error = EINVAL; | |
6627 | break; | |
6628 | case _PC_NO_TRUNC: | |
6629 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC)) | |
b0d623f7 | 6630 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0; |
2d21ac55 A |
6631 | else |
6632 | error = EINVAL; | |
6633 | break; | |
6634 | case _PC_CASE_SENSITIVE: | |
6635 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE)) | |
b0d623f7 | 6636 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1; |
2d21ac55 A |
6637 | else |
6638 | error = EINVAL; | |
6639 | break; | |
6640 | case _PC_CASE_PRESERVING: | |
6641 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING)) | |
b0d623f7 | 6642 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0; |
2d21ac55 A |
6643 | else |
6644 | error = EINVAL; | |
6645 | break; | |
6d2010ae | 6646 | case _PC_XATTR_SIZE_BITS: /* same as file size bits if named attrs supported */ |
2d21ac55 | 6647 | case _PC_FILESIZEBITS: |
b0d623f7 | 6648 | if (!NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) { |
2d21ac55 A |
6649 | *ap->a_retval = 64; |
6650 | error = 0; | |
6651 | break; | |
1c79356b | 6652 | } |
b0d623f7 | 6653 | maxFileSize = nfsap->nfsa_maxfilesize; |
2d21ac55 A |
6654 | nbits = 1; |
6655 | if (maxFileSize & 0xffffffff00000000ULL) { | |
6656 | nbits += 32; | |
6657 | maxFileSize >>= 32; | |
91447636 | 6658 | } |
2d21ac55 A |
6659 | if (maxFileSize & 0xffff0000) { |
6660 | nbits += 16; | |
6661 | maxFileSize >>= 16; | |
6662 | } | |
6663 | if (maxFileSize & 0xff00) { | |
6664 | nbits += 8; | |
6665 | maxFileSize >>= 8; | |
6666 | } | |
6667 | if (maxFileSize & 0xf0) { | |
6668 | nbits += 4; | |
6669 | maxFileSize >>= 4; | |
91447636 | 6670 | } |
2d21ac55 A |
6671 | if (maxFileSize & 0xc) { |
6672 | nbits += 2; | |
6673 | maxFileSize >>= 2; | |
6674 | } | |
6675 | if (maxFileSize & 0x2) { | |
6676 | nbits += 1; | |
6677 | } | |
6678 | *ap->a_retval = nbits; | |
6679 | break; | |
6680 | default: | |
6681 | error = EINVAL; | |
6682 | } | |
1c79356b | 6683 | |
2d21ac55 A |
6684 | lck_mtx_unlock(&nmp->nm_lock); |
6685 | ||
6686 | return (error); | |
1c79356b A |
6687 | } |
6688 | ||
1c79356b A |
6689 | /* |
6690 | * Read wrapper for special devices. | |
6691 | */ | |
b0d623f7 | 6692 | int |
2d21ac55 | 6693 | nfsspec_vnop_read( |
91447636 A |
6694 | struct vnop_read_args /* { |
6695 | struct vnodeop_desc *a_desc; | |
6696 | vnode_t a_vp; | |
1c79356b | 6697 | struct uio *a_uio; |
91447636 A |
6698 | int a_ioflag; |
6699 | vfs_context_t a_context; | |
2d21ac55 | 6700 | } */ *ap) |
1c79356b | 6701 | { |
2d21ac55 | 6702 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 6703 | struct timespec now; |
2d21ac55 | 6704 | int error; |
1c79356b A |
6705 | |
6706 | /* | |
6707 | * Set access flag. | |
6708 | */ | |
b0d623f7 | 6709 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 6710 | return (error); |
1c79356b | 6711 | np->n_flag |= NACC; |
5ba3f43e | 6712 | nanotime(&now); |
55e303ae | 6713 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 6714 | np->n_atim.tv_nsec = now.tv_nsec; |
b0d623f7 | 6715 | nfs_node_unlock(np); |
91447636 | 6716 | return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap)); |
1c79356b A |
6717 | } |
6718 | ||
6719 | /* | |
6720 | * Write wrapper for special devices. | |
6721 | */ | |
b0d623f7 | 6722 | int |
2d21ac55 | 6723 | nfsspec_vnop_write( |
91447636 A |
6724 | struct vnop_write_args /* { |
6725 | struct vnodeop_desc *a_desc; | |
6726 | vnode_t a_vp; | |
1c79356b | 6727 | struct uio *a_uio; |
91447636 A |
6728 | int a_ioflag; |
6729 | vfs_context_t a_context; | |
2d21ac55 | 6730 | } */ *ap) |
1c79356b | 6731 | { |
2d21ac55 | 6732 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 6733 | struct timespec now; |
2d21ac55 | 6734 | int error; |
1c79356b A |
6735 | |
6736 | /* | |
6737 | * Set update flag. | |
6738 | */ | |
b0d623f7 | 6739 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 6740 | return (error); |
1c79356b | 6741 | np->n_flag |= NUPD; |
5ba3f43e | 6742 | nanotime(&now); |
55e303ae | 6743 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 6744 | np->n_mtim.tv_nsec = now.tv_nsec; |
b0d623f7 | 6745 | nfs_node_unlock(np); |
91447636 | 6746 | return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap)); |
1c79356b A |
6747 | } |
6748 | ||
6749 | /* | |
6750 | * Close wrapper for special devices. | |
6751 | * | |
6752 | * Update the times on the nfsnode then do device close. | |
6753 | */ | |
b0d623f7 | 6754 | int |
2d21ac55 | 6755 | nfsspec_vnop_close( |
91447636 A |
6756 | struct vnop_close_args /* { |
6757 | struct vnodeop_desc *a_desc; | |
6758 | vnode_t a_vp; | |
6759 | int a_fflag; | |
6760 | vfs_context_t a_context; | |
2d21ac55 | 6761 | } */ *ap) |
1c79356b | 6762 | { |
91447636 | 6763 | vnode_t vp = ap->a_vp; |
2d21ac55 | 6764 | nfsnode_t np = VTONFS(vp); |
91447636 A |
6765 | struct vnode_attr vattr; |
6766 | mount_t mp; | |
2d21ac55 | 6767 | int error; |
1c79356b | 6768 | |
b0d623f7 | 6769 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 6770 | return (error); |
1c79356b A |
6771 | if (np->n_flag & (NACC | NUPD)) { |
6772 | np->n_flag |= NCHG; | |
b0d623f7 | 6773 | if (!vnode_isinuse(vp, 0) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) { |
91447636 A |
6774 | VATTR_INIT(&vattr); |
6775 | if (np->n_flag & NACC) { | |
6776 | vattr.va_access_time = np->n_atim; | |
6777 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
6778 | } | |
6779 | if (np->n_flag & NUPD) { | |
6780 | vattr.va_modify_time = np->n_mtim; | |
6781 | VATTR_SET_ACTIVE(&vattr, va_modify_time); | |
6782 | } | |
b0d623f7 | 6783 | nfs_node_unlock(np); |
91447636 | 6784 | vnode_setattr(vp, &vattr, ap->a_context); |
2d21ac55 | 6785 | } else { |
b0d623f7 | 6786 | nfs_node_unlock(np); |
1c79356b | 6787 | } |
2d21ac55 | 6788 | } else { |
b0d623f7 | 6789 | nfs_node_unlock(np); |
1c79356b | 6790 | } |
91447636 | 6791 | return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap)); |
1c79356b A |
6792 | } |
6793 | ||
2d21ac55 | 6794 | #if FIFO |
91447636 A |
6795 | extern vnop_t **fifo_vnodeop_p; |
6796 | ||
1c79356b A |
6797 | /* |
6798 | * Read wrapper for fifos. | |
6799 | */ | |
b0d623f7 | 6800 | int |
2d21ac55 | 6801 | nfsfifo_vnop_read( |
91447636 A |
6802 | struct vnop_read_args /* { |
6803 | struct vnodeop_desc *a_desc; | |
6804 | vnode_t a_vp; | |
1c79356b | 6805 | struct uio *a_uio; |
91447636 A |
6806 | int a_ioflag; |
6807 | vfs_context_t a_context; | |
2d21ac55 | 6808 | } */ *ap) |
1c79356b | 6809 | { |
2d21ac55 | 6810 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 6811 | struct timespec now; |
2d21ac55 | 6812 | int error; |
1c79356b A |
6813 | |
6814 | /* | |
6815 | * Set access flag. | |
6816 | */ | |
b0d623f7 | 6817 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 6818 | return (error); |
1c79356b | 6819 | np->n_flag |= NACC; |
5ba3f43e | 6820 | nanotime(&now); |
55e303ae | 6821 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 6822 | np->n_atim.tv_nsec = now.tv_nsec; |
b0d623f7 | 6823 | nfs_node_unlock(np); |
91447636 | 6824 | return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap)); |
1c79356b A |
6825 | } |
6826 | ||
6827 | /* | |
6828 | * Write wrapper for fifos. | |
6829 | */ | |
b0d623f7 | 6830 | int |
2d21ac55 | 6831 | nfsfifo_vnop_write( |
91447636 A |
6832 | struct vnop_write_args /* { |
6833 | struct vnodeop_desc *a_desc; | |
6834 | vnode_t a_vp; | |
1c79356b | 6835 | struct uio *a_uio; |
91447636 A |
6836 | int a_ioflag; |
6837 | vfs_context_t a_context; | |
2d21ac55 | 6838 | } */ *ap) |
1c79356b | 6839 | { |
2d21ac55 | 6840 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 6841 | struct timespec now; |
2d21ac55 | 6842 | int error; |
1c79356b A |
6843 | |
6844 | /* | |
6845 | * Set update flag. | |
6846 | */ | |
b0d623f7 | 6847 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 6848 | return (error); |
1c79356b | 6849 | np->n_flag |= NUPD; |
5ba3f43e | 6850 | nanotime(&now); |
55e303ae | 6851 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 6852 | np->n_mtim.tv_nsec = now.tv_nsec; |
b0d623f7 | 6853 | nfs_node_unlock(np); |
91447636 | 6854 | return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap)); |
1c79356b A |
6855 | } |
6856 | ||
6857 | /* | |
6858 | * Close wrapper for fifos. | |
6859 | * | |
6860 | * Update the times on the nfsnode then do fifo close. | |
6861 | */ | |
b0d623f7 | 6862 | int |
2d21ac55 | 6863 | nfsfifo_vnop_close( |
91447636 A |
6864 | struct vnop_close_args /* { |
6865 | struct vnodeop_desc *a_desc; | |
6866 | vnode_t a_vp; | |
6867 | int a_fflag; | |
6868 | vfs_context_t a_context; | |
2d21ac55 | 6869 | } */ *ap) |
1c79356b | 6870 | { |
91447636 | 6871 | vnode_t vp = ap->a_vp; |
2d21ac55 | 6872 | nfsnode_t np = VTONFS(vp); |
91447636 | 6873 | struct vnode_attr vattr; |
5ba3f43e | 6874 | struct timespec now; |
91447636 | 6875 | mount_t mp; |
2d21ac55 | 6876 | int error; |
1c79356b | 6877 | |
b0d623f7 | 6878 | if ((error = nfs_node_lock(np))) |
2d21ac55 | 6879 | return (error); |
1c79356b | 6880 | if (np->n_flag & (NACC | NUPD)) { |
5ba3f43e | 6881 | nanotime(&now); |
1c79356b | 6882 | if (np->n_flag & NACC) { |
55e303ae | 6883 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 6884 | np->n_atim.tv_nsec = now.tv_nsec; |
1c79356b A |
6885 | } |
6886 | if (np->n_flag & NUPD) { | |
55e303ae | 6887 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 6888 | np->n_mtim.tv_nsec = now.tv_nsec; |
1c79356b A |
6889 | } |
6890 | np->n_flag |= NCHG; | |
91447636 A |
6891 | if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) { |
6892 | VATTR_INIT(&vattr); | |
6893 | if (np->n_flag & NACC) { | |
6894 | vattr.va_access_time = np->n_atim; | |
6895 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
6896 | } | |
6897 | if (np->n_flag & NUPD) { | |
6898 | vattr.va_modify_time = np->n_mtim; | |
6899 | VATTR_SET_ACTIVE(&vattr, va_modify_time); | |
6900 | } | |
b0d623f7 | 6901 | nfs_node_unlock(np); |
91447636 | 6902 | vnode_setattr(vp, &vattr, ap->a_context); |
2d21ac55 | 6903 | } else { |
b0d623f7 | 6904 | nfs_node_unlock(np); |
1c79356b | 6905 | } |
2d21ac55 | 6906 | } else { |
b0d623f7 | 6907 | nfs_node_unlock(np); |
1c79356b | 6908 | } |
91447636 | 6909 | return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap)); |
1c79356b | 6910 | } |
2d21ac55 | 6911 | #endif /* FIFO */ |
1c79356b | 6912 | |
91447636 | 6913 | /*ARGSUSED*/ |
b0d623f7 | 6914 | int |
2d21ac55 | 6915 | nfs_vnop_ioctl( |
6d2010ae | 6916 | struct vnop_ioctl_args /* { |
91447636 A |
6917 | struct vnodeop_desc *a_desc; |
6918 | vnode_t a_vp; | |
b0d623f7 | 6919 | u_int32_t a_command; |
91447636 A |
6920 | caddr_t a_data; |
6921 | int a_fflag; | |
2d21ac55 | 6922 | vfs_context_t a_context; |
91447636 | 6923 | } */ *ap) |
1c79356b | 6924 | { |
6d2010ae A |
6925 | vfs_context_t ctx = ap->a_context; |
6926 | vnode_t vp = ap->a_vp; | |
39236c6e | 6927 | struct nfsmount *mp = VTONMP(vp); |
3e170ce0 A |
6928 | struct user_nfs_gss_principal gprinc; |
6929 | uint32_t len; | |
6d2010ae | 6930 | int error = ENOTTY; |
1c79356b | 6931 | |
39236c6e A |
6932 | if (mp == NULL) |
6933 | return (ENXIO); | |
6934 | ||
6d2010ae A |
6935 | switch (ap->a_command) { |
6936 | ||
6937 | case F_FULLFSYNC: | |
6938 | if (vnode_vfsisrdonly(vp)) | |
6939 | return (EROFS); | |
6d2010ae A |
6940 | error = nfs_flush(VTONFS(vp), MNT_WAIT, vfs_context_thread(ctx), 0); |
6941 | break; | |
5ba3f43e | 6942 | case NFS_IOC_DESTROY_CRED: |
3e170ce0 A |
6943 | if (!auth_is_kerberized(mp->nm_auth)) |
6944 | return (ENOTSUP); | |
fe8ab488 | 6945 | error = nfs_gss_clnt_ctx_remove(mp, vfs_context_ucred(ctx)); |
39236c6e | 6946 | break; |
5ba3f43e A |
6947 | case NFS_IOC_SET_CRED: |
6948 | case NFS_IOC_SET_CRED64: | |
3e170ce0 A |
6949 | if (!auth_is_kerberized(mp->nm_auth)) |
6950 | return (ENOTSUP); | |
5ba3f43e A |
6951 | if ((ap->a_command == NFS_IOC_SET_CRED && vfs_context_is64bit(ctx)) || |
6952 | (ap->a_command == NFS_IOC_SET_CRED64 && !vfs_context_is64bit(ctx))) | |
6953 | return (EINVAL); | |
3e170ce0 A |
6954 | if (vfs_context_is64bit(ctx)) { |
6955 | gprinc = *(struct user_nfs_gss_principal *)ap->a_data; | |
6956 | } else { | |
6957 | struct nfs_gss_principal *tp; | |
6958 | tp = (struct nfs_gss_principal *)ap->a_data; | |
6959 | gprinc.princlen = tp->princlen; | |
6960 | gprinc.nametype = tp->nametype; | |
6961 | gprinc.principal = CAST_USER_ADDR_T(tp->principal); | |
6962 | } | |
39037602 | 6963 | NFS_DBG(NFS_FAC_GSS, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %d name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx), gprinc.princlen, gprinc.nametype, (unsigned long long)gprinc.principal); |
3e170ce0 A |
6964 | if (gprinc.princlen > MAXPATHLEN) |
6965 | return (EINVAL); | |
3e170ce0 A |
6966 | uint8_t *p; |
6967 | MALLOC(p, uint8_t *, gprinc.princlen+1, M_TEMP, M_WAITOK|M_ZERO); | |
6968 | if (p == NULL) | |
6969 | return (ENOMEM); | |
6970 | error = copyin(gprinc.principal, p, gprinc.princlen); | |
6971 | if (error) { | |
6972 | NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n", | |
6973 | gprinc.princlen, error); | |
6974 | FREE(p, M_TEMP); | |
6975 | return (error); | |
6976 | } | |
6977 | NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s\n", p); | |
6978 | error = nfs_gss_clnt_ctx_set_principal(mp, ctx, p, gprinc.princlen, gprinc.nametype); | |
6979 | NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s returned %d\n", p, error); | |
6980 | FREE(p, M_TEMP); | |
6981 | break; | |
5ba3f43e A |
6982 | case NFS_IOC_GET_CRED: |
6983 | case NFS_IOC_GET_CRED64: | |
3e170ce0 A |
6984 | if (!auth_is_kerberized(mp->nm_auth)) |
6985 | return (ENOTSUP); | |
5ba3f43e A |
6986 | if ((ap->a_command == NFS_IOC_GET_CRED && vfs_context_is64bit(ctx)) || |
6987 | (ap->a_command == NFS_IOC_GET_CRED64 && !vfs_context_is64bit(ctx))) | |
6988 | return (EINVAL); | |
3e170ce0 A |
6989 | error = nfs_gss_clnt_ctx_get_principal(mp, ctx, &gprinc); |
6990 | if (error) | |
6991 | break; | |
6992 | if (vfs_context_is64bit(ctx)) { | |
6993 | struct user_nfs_gss_principal *upp = (struct user_nfs_gss_principal *)ap->a_data; | |
6994 | len = upp->princlen; | |
6995 | if (gprinc.princlen < len) | |
6996 | len = gprinc.princlen; | |
6997 | upp->princlen = gprinc.princlen; | |
6998 | upp->nametype = gprinc.nametype; | |
6999 | upp->flags = gprinc.flags; | |
7000 | if (gprinc.principal) | |
7001 | error = copyout((void *)gprinc.principal, upp->principal, len); | |
7002 | else | |
7003 | upp->principal = USER_ADDR_NULL; | |
7004 | } else { | |
7005 | struct nfs_gss_principal *u32pp = (struct nfs_gss_principal *)ap->a_data; | |
7006 | len = u32pp->princlen; | |
7007 | if (gprinc.princlen < len) | |
7008 | len = gprinc.princlen; | |
7009 | u32pp->princlen = gprinc.princlen; | |
7010 | u32pp->nametype = gprinc.nametype; | |
7011 | u32pp->flags = gprinc.flags; | |
7012 | if (gprinc.principal) | |
7013 | error = copyout((void *)gprinc.principal, u32pp->principal, len); | |
7014 | else | |
7015 | u32pp->principal = (user32_addr_t)0; | |
7016 | } | |
7017 | if (error) { | |
7018 | NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n", | |
7019 | gprinc.princlen, error); | |
7020 | } | |
39037602 A |
7021 | if (gprinc.principal) |
7022 | FREE(gprinc.principal, M_TEMP); | |
6d2010ae A |
7023 | } |
7024 | ||
7025 | return (error); | |
1c79356b A |
7026 | } |
7027 | ||
91447636 | 7028 | /*ARGSUSED*/ |
b0d623f7 | 7029 | int |
2d21ac55 | 7030 | nfs_vnop_select( |
91447636 A |
7031 | __unused struct vnop_select_args /* { |
7032 | struct vnodeop_desc *a_desc; | |
7033 | vnode_t a_vp; | |
7034 | int a_which; | |
7035 | int a_fflags; | |
91447636 | 7036 | void *a_wql; |
2d21ac55 | 7037 | vfs_context_t a_context; |
91447636 | 7038 | } */ *ap) |
1c79356b A |
7039 | { |
7040 | ||
7041 | /* | |
7042 | * We were once bogusly seltrue() which returns 1. Is this right? | |
7043 | */ | |
7044 | return (1); | |
7045 | } | |
7046 | ||
1c79356b | 7047 | /* |
2d21ac55 A |
7048 | * vnode OP for pagein using UPL |
7049 | * | |
7050 | * No buffer I/O, just RPCs straight into the mapped pages. | |
1c79356b | 7051 | */ |
b0d623f7 | 7052 | int |
2d21ac55 | 7053 | nfs_vnop_pagein( |
91447636 A |
7054 | struct vnop_pagein_args /* { |
7055 | struct vnodeop_desc *a_desc; | |
7056 | vnode_t a_vp; | |
7057 | upl_t a_pl; | |
7058 | vm_offset_t a_pl_offset; | |
7059 | off_t a_f_offset; | |
7060 | size_t a_size; | |
7061 | int a_flags; | |
7062 | vfs_context_t a_context; | |
2d21ac55 | 7063 | } */ *ap) |
1c79356b | 7064 | { |
91447636 | 7065 | vnode_t vp = ap->a_vp; |
1c79356b | 7066 | upl_t pl = ap->a_pl; |
2d21ac55 | 7067 | size_t size = ap->a_size; |
1c79356b A |
7068 | off_t f_offset = ap->a_f_offset; |
7069 | vm_offset_t pl_offset = ap->a_pl_offset; | |
2d21ac55 A |
7070 | int flags = ap->a_flags; |
7071 | thread_t thd; | |
91447636 | 7072 | kauth_cred_t cred; |
2d21ac55 A |
7073 | nfsnode_t np = VTONFS(vp); |
7074 | size_t nmrsize, iosize, txsize, rxsize, retsize; | |
7075 | off_t txoffset; | |
55e303ae | 7076 | struct nfsmount *nmp; |
1c79356b | 7077 | int error = 0; |
b0d623f7 A |
7078 | vm_offset_t ioaddr, rxaddr; |
7079 | uio_t uio; | |
7080 | char uio_buf [ UIO_SIZEOF(1) ]; | |
fa4905b1 | 7081 | int nofreeupl = flags & UPL_NOCOMMIT; |
55e303ae | 7082 | upl_page_info_t *plinfo; |
2d21ac55 A |
7083 | #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */ |
7084 | struct nfsreq *req[MAXPAGINGREQS]; | |
7085 | int nextsend, nextwait; | |
b0d623f7 A |
7086 | uint32_t stategenid = 0, restart = 0; |
7087 | kern_return_t kret; | |
1c79356b | 7088 | |
2d21ac55 | 7089 | FSDBG(322, np, f_offset, size, flags); |
fa4905b1 A |
7090 | if (pl == (upl_t)NULL) |
7091 | panic("nfs_pagein: no upl"); | |
1c79356b | 7092 | |
fa4905b1 | 7093 | if (size <= 0) { |
2d21ac55 | 7094 | printf("nfs_pagein: invalid size %ld", size); |
fa4905b1 | 7095 | if (!nofreeupl) |
316670eb | 7096 | (void) ubc_upl_abort_range(pl, pl_offset, size, 0); |
1c79356b | 7097 | return (EINVAL); |
fa4905b1 | 7098 | } |
91447636 | 7099 | if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) { |
fa4905b1 | 7100 | if (!nofreeupl) |
2d21ac55 | 7101 | ubc_upl_abort_range(pl, pl_offset, size, |
1c79356b A |
7102 | UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); |
7103 | return (EINVAL); | |
7104 | } | |
91447636 | 7105 | |
2d21ac55 | 7106 | thd = vfs_context_thread(ap->a_context); |
fa4905b1 | 7107 | cred = ubc_getcred(vp); |
0c530ab8 | 7108 | if (!IS_VALID_CRED(cred)) |
91447636 | 7109 | cred = vfs_context_ucred(ap->a_context); |
1c79356b | 7110 | |
b0d623f7 A |
7111 | uio = uio_createwithbuffer(1, f_offset, UIO_SYSSPACE, UIO_READ, |
7112 | &uio_buf, sizeof(uio_buf)); | |
1c79356b | 7113 | |
2d21ac55 | 7114 | nmp = VTONMP(vp); |
fe8ab488 | 7115 | if (nfs_mount_gone(nmp)) { |
55e303ae | 7116 | if (!nofreeupl) |
2d21ac55 | 7117 | ubc_upl_abort_range(pl, pl_offset, size, |
55e303ae A |
7118 | UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); |
7119 | return (ENXIO); | |
7120 | } | |
2d21ac55 | 7121 | nmrsize = nmp->nm_rsize; |
1c79356b | 7122 | |
55e303ae | 7123 | plinfo = ubc_upl_pageinfo(pl); |
b0d623f7 A |
7124 | kret = ubc_upl_map(pl, &ioaddr); |
7125 | if (kret != KERN_SUCCESS) | |
7126 | panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret); | |
1c79356b | 7127 | ioaddr += pl_offset; |
b0d623f7 A |
7128 | |
7129 | tryagain: | |
7130 | if (nmp->nm_vers >= NFS_VER4) | |
7131 | stategenid = nmp->nm_stategenid; | |
2d21ac55 A |
7132 | txsize = rxsize = size; |
7133 | txoffset = f_offset; | |
b0d623f7 | 7134 | rxaddr = ioaddr; |
1c79356b | 7135 | |
2d21ac55 A |
7136 | bzero(req, sizeof(req)); |
7137 | nextsend = nextwait = 0; | |
1c79356b | 7138 | do { |
6d2010ae A |
7139 | if (np->n_flag & NREVOKE) { |
7140 | error = EIO; | |
7141 | break; | |
7142 | } | |
2d21ac55 A |
7143 | /* send requests while we need to and have available slots */ |
7144 | while ((txsize > 0) && (req[nextsend] == NULL)) { | |
7145 | iosize = MIN(nmrsize, txsize); | |
7146 | if ((error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, iosize, thd, cred, NULL, &req[nextsend]))) { | |
7147 | req[nextsend] = NULL; | |
7148 | break; | |
7149 | } | |
7150 | txoffset += iosize; | |
7151 | txsize -= iosize; | |
7152 | nextsend = (nextsend + 1) % MAXPAGINGREQS; | |
7153 | } | |
7154 | /* wait while we need to and break out if more requests to send */ | |
7155 | while ((rxsize > 0) && req[nextwait]) { | |
7156 | iosize = retsize = MIN(nmrsize, rxsize); | |
b0d623f7 A |
7157 | uio_reset(uio, uio_offset(uio), UIO_SYSSPACE, UIO_READ); |
7158 | uio_addiov(uio, CAST_USER_ADDR_T(rxaddr), iosize); | |
7159 | FSDBG(322, uio_offset(uio), uio_resid(uio), rxaddr, rxsize); | |
7160 | #if UPL_DEBUG | |
7161 | upl_ubc_alias_set(pl, (uintptr_t) current_thread(), (uintptr_t) 2); | |
91447636 | 7162 | #endif /* UPL_DEBUG */ |
316670eb | 7163 | OSAddAtomic64(1, &nfsstats.pageins); |
2d21ac55 A |
7164 | error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req[nextwait], uio, &retsize, NULL); |
7165 | req[nextwait] = NULL; | |
7166 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
b0d623f7 A |
7167 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
7168 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
7169 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
7170 | NP(np, "nfs_vnop_pagein: error %d, initiating recovery", error); | |
7171 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
7172 | } |
7173 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
7174 | restart++; |
7175 | goto cancel; | |
7176 | } | |
2d21ac55 | 7177 | if (error) { |
b0d623f7 | 7178 | FSDBG(322, uio_offset(uio), uio_resid(uio), error, -1); |
2d21ac55 A |
7179 | break; |
7180 | } | |
7181 | if (retsize < iosize) { | |
7182 | /* Just zero fill the rest of the valid area. */ | |
2d21ac55 | 7183 | int zcnt = iosize - retsize; |
b0d623f7 A |
7184 | bzero((char *)rxaddr + retsize, zcnt); |
7185 | FSDBG(324, uio_offset(uio), retsize, zcnt, rxaddr); | |
7186 | uio_update(uio, zcnt); | |
1c79356b | 7187 | } |
b0d623f7 | 7188 | rxaddr += iosize; |
2d21ac55 A |
7189 | rxsize -= iosize; |
7190 | if (txsize) | |
7191 | break; | |
1c79356b | 7192 | } |
2d21ac55 | 7193 | } while (!error && (txsize || rxsize)); |
1c79356b | 7194 | |
b0d623f7 | 7195 | restart = 0; |
1c79356b | 7196 | |
2d21ac55 | 7197 | if (error) { |
b0d623f7 | 7198 | cancel: |
2d21ac55 A |
7199 | /* cancel any outstanding requests */ |
7200 | while (req[nextwait]) { | |
7201 | nfs_request_async_cancel(req[nextwait]); | |
7202 | req[nextwait] = NULL; | |
7203 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
7204 | } | |
6d2010ae A |
7205 | if (np->n_flag & NREVOKE) { |
7206 | error = EIO; | |
7207 | } else if (restart) { | |
7208 | if (restart <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */ | |
7209 | if (error == NFSERR_GRACE) | |
7210 | tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz); | |
7211 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) | |
7212 | goto tryagain; | |
7213 | } else { | |
7214 | NP(np, "nfs_pagein: too many restarts, aborting"); | |
7215 | } | |
b0d623f7 | 7216 | } |
2d21ac55 A |
7217 | } |
7218 | ||
b0d623f7 A |
7219 | ubc_upl_unmap(pl); |
7220 | ||
fa4905b1 | 7221 | if (!nofreeupl) { |
2d21ac55 A |
7222 | if (error) |
7223 | ubc_upl_abort_range(pl, pl_offset, size, | |
fa4905b1 A |
7224 | UPL_ABORT_ERROR | |
7225 | UPL_ABORT_FREE_ON_EMPTY); | |
1c79356b | 7226 | else |
0b4e3aa0 | 7227 | ubc_upl_commit_range(pl, pl_offset, size, |
fa4905b1 A |
7228 | UPL_COMMIT_CLEAR_DIRTY | |
7229 | UPL_COMMIT_FREE_ON_EMPTY); | |
1c79356b | 7230 | } |
1c79356b A |
7231 | return (error); |
7232 | } | |
7233 | ||
0b4e3aa0 | 7234 | |
1c79356b | 7235 | /* |
2d21ac55 A |
7236 | * the following are needed only by nfs_pageout to know how to handle errors |
7237 | * see nfs_pageout comments on explanation of actions. | |
7238 | * the errors here are copied from errno.h and errors returned by servers | |
7239 | * are expected to match the same numbers here. If not, our actions maybe | |
7240 | * erroneous. | |
7241 | */ | |
b0d623f7 | 7242 | char nfs_pageouterrorhandler(int); |
39236c6e | 7243 | enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, SEVER}; |
2d21ac55 A |
7244 | #define NFS_ELAST 88 |
7245 | static u_char errorcount[NFS_ELAST+1]; /* better be zeros when initialized */ | |
7246 | static const char errortooutcome[NFS_ELAST+1] = { | |
7247 | NOACTION, | |
7248 | DUMP, /* EPERM 1 Operation not permitted */ | |
7249 | DUMP, /* ENOENT 2 No such file or directory */ | |
7250 | DUMPANDLOG, /* ESRCH 3 No such process */ | |
7251 | RETRY, /* EINTR 4 Interrupted system call */ | |
7252 | DUMP, /* EIO 5 Input/output error */ | |
7253 | DUMP, /* ENXIO 6 Device not configured */ | |
7254 | DUMPANDLOG, /* E2BIG 7 Argument list too long */ | |
7255 | DUMPANDLOG, /* ENOEXEC 8 Exec format error */ | |
7256 | DUMPANDLOG, /* EBADF 9 Bad file descriptor */ | |
7257 | DUMPANDLOG, /* ECHILD 10 No child processes */ | |
7258 | DUMPANDLOG, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */ | |
7259 | RETRY, /* ENOMEM 12 Cannot allocate memory */ | |
7260 | DUMP, /* EACCES 13 Permission denied */ | |
7261 | DUMPANDLOG, /* EFAULT 14 Bad address */ | |
7262 | DUMPANDLOG, /* ENOTBLK 15 POSIX - Block device required */ | |
7263 | RETRY, /* EBUSY 16 Device busy */ | |
7264 | DUMP, /* EEXIST 17 File exists */ | |
7265 | DUMP, /* EXDEV 18 Cross-device link */ | |
7266 | DUMP, /* ENODEV 19 Operation not supported by device */ | |
7267 | DUMP, /* ENOTDIR 20 Not a directory */ | |
7268 | DUMP, /* EISDIR 21 Is a directory */ | |
7269 | DUMP, /* EINVAL 22 Invalid argument */ | |
7270 | DUMPANDLOG, /* ENFILE 23 Too many open files in system */ | |
7271 | DUMPANDLOG, /* EMFILE 24 Too many open files */ | |
7272 | DUMPANDLOG, /* ENOTTY 25 Inappropriate ioctl for device */ | |
7273 | DUMPANDLOG, /* ETXTBSY 26 Text file busy - POSIX */ | |
7274 | DUMP, /* EFBIG 27 File too large */ | |
7275 | DUMP, /* ENOSPC 28 No space left on device */ | |
7276 | DUMPANDLOG, /* ESPIPE 29 Illegal seek */ | |
7277 | DUMP, /* EROFS 30 Read-only file system */ | |
7278 | DUMP, /* EMLINK 31 Too many links */ | |
7279 | RETRY, /* EPIPE 32 Broken pipe */ | |
7280 | /* math software */ | |
7281 | DUMPANDLOG, /* EDOM 33 Numerical argument out of domain */ | |
7282 | DUMPANDLOG, /* ERANGE 34 Result too large */ | |
7283 | RETRY, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */ | |
7284 | DUMPANDLOG, /* EINPROGRESS 36 Operation now in progress */ | |
7285 | DUMPANDLOG, /* EALREADY 37 Operation already in progress */ | |
7286 | /* ipc/network software -- argument errors */ | |
7287 | DUMPANDLOG, /* ENOTSOC 38 Socket operation on non-socket */ | |
7288 | DUMPANDLOG, /* EDESTADDRREQ 39 Destination address required */ | |
7289 | DUMPANDLOG, /* EMSGSIZE 40 Message too long */ | |
7290 | DUMPANDLOG, /* EPROTOTYPE 41 Protocol wrong type for socket */ | |
7291 | DUMPANDLOG, /* ENOPROTOOPT 42 Protocol not available */ | |
7292 | DUMPANDLOG, /* EPROTONOSUPPORT 43 Protocol not supported */ | |
7293 | DUMPANDLOG, /* ESOCKTNOSUPPORT 44 Socket type not supported */ | |
7294 | DUMPANDLOG, /* ENOTSUP 45 Operation not supported */ | |
7295 | DUMPANDLOG, /* EPFNOSUPPORT 46 Protocol family not supported */ | |
7296 | DUMPANDLOG, /* EAFNOSUPPORT 47 Address family not supported by protocol family */ | |
7297 | DUMPANDLOG, /* EADDRINUSE 48 Address already in use */ | |
7298 | DUMPANDLOG, /* EADDRNOTAVAIL 49 Can't assign requested address */ | |
7299 | /* ipc/network software -- operational errors */ | |
7300 | RETRY, /* ENETDOWN 50 Network is down */ | |
7301 | RETRY, /* ENETUNREACH 51 Network is unreachable */ | |
7302 | RETRY, /* ENETRESET 52 Network dropped connection on reset */ | |
7303 | RETRY, /* ECONNABORTED 53 Software caused connection abort */ | |
7304 | RETRY, /* ECONNRESET 54 Connection reset by peer */ | |
7305 | RETRY, /* ENOBUFS 55 No buffer space available */ | |
7306 | RETRY, /* EISCONN 56 Socket is already connected */ | |
7307 | RETRY, /* ENOTCONN 57 Socket is not connected */ | |
7308 | RETRY, /* ESHUTDOWN 58 Can't send after socket shutdown */ | |
7309 | RETRY, /* ETOOMANYREFS 59 Too many references: can't splice */ | |
7310 | RETRY, /* ETIMEDOUT 60 Operation timed out */ | |
7311 | RETRY, /* ECONNREFUSED 61 Connection refused */ | |
7312 | ||
7313 | DUMPANDLOG, /* ELOOP 62 Too many levels of symbolic links */ | |
7314 | DUMP, /* ENAMETOOLONG 63 File name too long */ | |
7315 | RETRY, /* EHOSTDOWN 64 Host is down */ | |
7316 | RETRY, /* EHOSTUNREACH 65 No route to host */ | |
7317 | DUMP, /* ENOTEMPTY 66 Directory not empty */ | |
7318 | /* quotas & mush */ | |
7319 | DUMPANDLOG, /* PROCLIM 67 Too many processes */ | |
7320 | DUMPANDLOG, /* EUSERS 68 Too many users */ | |
7321 | DUMPANDLOG, /* EDQUOT 69 Disc quota exceeded */ | |
7322 | /* Network File System */ | |
7323 | DUMP, /* ESTALE 70 Stale NFS file handle */ | |
7324 | DUMP, /* EREMOTE 71 Too many levels of remote in path */ | |
7325 | DUMPANDLOG, /* EBADRPC 72 RPC struct is bad */ | |
7326 | DUMPANDLOG, /* ERPCMISMATCH 73 RPC version wrong */ | |
7327 | DUMPANDLOG, /* EPROGUNAVAIL 74 RPC prog. not avail */ | |
7328 | DUMPANDLOG, /* EPROGMISMATCH 75 Program version wrong */ | |
7329 | DUMPANDLOG, /* EPROCUNAVAIL 76 Bad procedure for program */ | |
7330 | ||
7331 | DUMPANDLOG, /* ENOLCK 77 No locks available */ | |
7332 | DUMPANDLOG, /* ENOSYS 78 Function not implemented */ | |
7333 | DUMPANDLOG, /* EFTYPE 79 Inappropriate file type or format */ | |
7334 | DUMPANDLOG, /* EAUTH 80 Authentication error */ | |
7335 | DUMPANDLOG, /* ENEEDAUTH 81 Need authenticator */ | |
7336 | /* Intelligent device errors */ | |
7337 | DUMPANDLOG, /* EPWROFF 82 Device power is off */ | |
7338 | DUMPANDLOG, /* EDEVERR 83 Device error, e.g. paper out */ | |
7339 | DUMPANDLOG, /* EOVERFLOW 84 Value too large to be stored in data type */ | |
7340 | /* Program loading errors */ | |
7341 | DUMPANDLOG, /* EBADEXEC 85 Bad executable */ | |
7342 | DUMPANDLOG, /* EBADARCH 86 Bad CPU type in executable */ | |
7343 | DUMPANDLOG, /* ESHLIBVERS 87 Shared library version mismatch */ | |
7344 | DUMPANDLOG, /* EBADMACHO 88 Malformed Macho file */ | |
7345 | }; | |
7346 | ||
b0d623f7 | 7347 | char |
2d21ac55 A |
7348 | nfs_pageouterrorhandler(int error) |
7349 | { | |
7350 | if (error > NFS_ELAST) | |
7351 | return(DUMP); | |
7352 | else | |
7353 | return(errortooutcome[error]); | |
7354 | } | |
7355 | ||
7356 | ||
7357 | /* | |
7358 | * vnode OP for pageout using UPL | |
7359 | * | |
7360 | * No buffer I/O, just RPCs straight from the mapped pages. | |
1c79356b A |
7361 | * File size changes are not permitted in pageout. |
7362 | */ | |
b0d623f7 | 7363 | int |
2d21ac55 | 7364 | nfs_vnop_pageout( |
91447636 A |
7365 | struct vnop_pageout_args /* { |
7366 | struct vnodeop_desc *a_desc; | |
7367 | vnode_t a_vp; | |
7368 | upl_t a_pl; | |
7369 | vm_offset_t a_pl_offset; | |
7370 | off_t a_f_offset; | |
7371 | size_t a_size; | |
7372 | int a_flags; | |
7373 | vfs_context_t a_context; | |
2d21ac55 | 7374 | } */ *ap) |
1c79356b | 7375 | { |
91447636 | 7376 | vnode_t vp = ap->a_vp; |
1c79356b | 7377 | upl_t pl = ap->a_pl; |
2d21ac55 | 7378 | size_t size = ap->a_size; |
1c79356b A |
7379 | off_t f_offset = ap->a_f_offset; |
7380 | vm_offset_t pl_offset = ap->a_pl_offset; | |
2d21ac55 A |
7381 | int flags = ap->a_flags; |
7382 | nfsnode_t np = VTONFS(vp); | |
7383 | thread_t thd; | |
91447636 | 7384 | kauth_cred_t cred; |
55e303ae | 7385 | struct nfsbuf *bp; |
2d21ac55 | 7386 | struct nfsmount *nmp = VTONMP(vp); |
91447636 | 7387 | daddr64_t lbn; |
8f6c56a5 | 7388 | int error = 0, iomode; |
2d21ac55 A |
7389 | off_t off, txoffset, rxoffset; |
7390 | vm_offset_t ioaddr, txaddr, rxaddr; | |
b0d623f7 A |
7391 | uio_t auio; |
7392 | char uio_buf [ UIO_SIZEOF(1) ]; | |
fa4905b1 | 7393 | int nofreeupl = flags & UPL_NOCOMMIT; |
2d21ac55 A |
7394 | size_t nmwsize, biosize, iosize, pgsize, txsize, rxsize, xsize, remsize; |
7395 | struct nfsreq *req[MAXPAGINGREQS]; | |
b0d623f7 | 7396 | int nextsend, nextwait, wverfset, commit; |
2d21ac55 | 7397 | uint64_t wverf, wverf2; |
b0d623f7 A |
7398 | uint32_t stategenid = 0, vrestart = 0, restart = 0, vrestarts = 0, restarts = 0; |
7399 | kern_return_t kret; | |
1c79356b | 7400 | |
fa4905b1 A |
7401 | FSDBG(323, f_offset, size, pl, pl_offset); |
7402 | ||
7403 | if (pl == (upl_t)NULL) | |
7404 | panic("nfs_pageout: no upl"); | |
1c79356b | 7405 | |
fa4905b1 | 7406 | if (size <= 0) { |
2d21ac55 | 7407 | printf("nfs_pageout: invalid size %ld", size); |
fa4905b1 | 7408 | if (!nofreeupl) |
316670eb | 7409 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
1c79356b | 7410 | return (EINVAL); |
1c79356b A |
7411 | } |
7412 | ||
55e303ae A |
7413 | if (!nmp) { |
7414 | if (!nofreeupl) | |
7415 | ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY); | |
7416 | return (ENXIO); | |
7417 | } | |
0c530ab8 | 7418 | biosize = nmp->nm_biosize; |
2d21ac55 A |
7419 | nmwsize = nmp->nm_wsize; |
7420 | ||
b0d623f7 | 7421 | nfs_data_lock_noupdate(np, NFS_DATA_LOCK_SHARED); |
1c79356b | 7422 | |
1c79356b | 7423 | /* |
55e303ae A |
7424 | * Check to see whether the buffer is incore. |
7425 | * If incore and not busy, invalidate it from the cache. | |
1c79356b | 7426 | */ |
55e303ae A |
7427 | for (iosize = 0; iosize < size; iosize += xsize) { |
7428 | off = f_offset + iosize; | |
7429 | /* need make sure we do things on block boundaries */ | |
7430 | xsize = biosize - (off % biosize); | |
7431 | if (off + xsize > f_offset + size) | |
7432 | xsize = f_offset + size - off; | |
2d21ac55 | 7433 | lbn = (daddr64_t)(off / biosize); |
91447636 | 7434 | lck_mtx_lock(nfs_buf_mutex); |
2d21ac55 | 7435 | if ((bp = nfs_buf_incore(np, lbn))) { |
91447636 A |
7436 | FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags); |
7437 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
7438 | lck_mtx_unlock(nfs_buf_mutex); | |
b0d623f7 | 7439 | nfs_data_unlock_noupdate(np); |
fa4905b1 A |
7440 | /* no panic. just tell vm we are busy */ |
7441 | if (!nofreeupl) | |
316670eb | 7442 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
55e303ae A |
7443 | return (EBUSY); |
7444 | } | |
7445 | if (bp->nb_dirtyend > 0) { | |
7446 | /* | |
91447636 A |
7447 | * if there's a dirty range in the buffer, check |
7448 | * to see if it extends beyond the pageout region | |
55e303ae A |
7449 | * |
7450 | * if the dirty region lies completely within the | |
7451 | * pageout region, we just invalidate the buffer | |
7452 | * because it's all being written out now anyway. | |
7453 | * | |
7454 | * if any of the dirty region lies outside the | |
7455 | * pageout region, we'll try to clip the dirty | |
7456 | * region to eliminate the portion that's being | |
7457 | * paged out. If that's not possible, because | |
7458 | * the dirty region extends before and after the | |
7459 | * pageout region, then we'll just return EBUSY. | |
7460 | */ | |
7461 | off_t boff, start, end; | |
7462 | boff = NBOFF(bp); | |
7463 | start = off; | |
7464 | end = off + xsize; | |
7465 | /* clip end to EOF */ | |
91447636 | 7466 | if (end > (off_t)np->n_size) |
55e303ae A |
7467 | end = np->n_size; |
7468 | start -= boff; | |
7469 | end -= boff; | |
7470 | if ((bp->nb_dirtyoff < start) && | |
7471 | (bp->nb_dirtyend > end)) { | |
b0d623f7 A |
7472 | /* |
7473 | * not gonna be able to clip the dirty region | |
7474 | * | |
7475 | * But before returning the bad news, move the | |
7476 | * buffer to the start of the delwri list and | |
7477 | * give the list a push to try to flush the | |
7478 | * buffer out. | |
7479 | */ | |
2d21ac55 | 7480 | FSDBG(323, np, bp, 0xd00deebc, EBUSY); |
b0d623f7 A |
7481 | nfs_buf_remfree(bp); |
7482 | TAILQ_INSERT_HEAD(&nfsbufdelwri, bp, nb_free); | |
7483 | nfsbufdelwricnt++; | |
91447636 | 7484 | nfs_buf_drop(bp); |
b0d623f7 | 7485 | nfs_buf_delwri_push(1); |
91447636 | 7486 | lck_mtx_unlock(nfs_buf_mutex); |
b0d623f7 | 7487 | nfs_data_unlock_noupdate(np); |
55e303ae | 7488 | if (!nofreeupl) |
316670eb | 7489 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
55e303ae A |
7490 | return (EBUSY); |
7491 | } | |
7492 | if ((bp->nb_dirtyoff < start) || | |
7493 | (bp->nb_dirtyend > end)) { | |
7494 | /* clip dirty region, if necessary */ | |
7495 | if (bp->nb_dirtyoff < start) | |
7496 | bp->nb_dirtyend = min(bp->nb_dirtyend, start); | |
7497 | if (bp->nb_dirtyend > end) | |
7498 | bp->nb_dirtyoff = max(bp->nb_dirtyoff, end); | |
7499 | FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00); | |
7500 | /* we're leaving this block dirty */ | |
91447636 A |
7501 | nfs_buf_drop(bp); |
7502 | lck_mtx_unlock(nfs_buf_mutex); | |
55e303ae A |
7503 | continue; |
7504 | } | |
7505 | } | |
7506 | nfs_buf_remfree(bp); | |
91447636 A |
7507 | lck_mtx_unlock(nfs_buf_mutex); |
7508 | SET(bp->nb_flags, NB_INVAL); | |
b0d623f7 | 7509 | nfs_node_lock_force(np); |
55e303ae A |
7510 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { |
7511 | CLR(bp->nb_flags, NB_NEEDCOMMIT); | |
7512 | np->n_needcommitcnt--; | |
7513 | CHECK_NEEDCOMMITCNT(np); | |
fa4905b1 | 7514 | } |
b0d623f7 | 7515 | nfs_node_unlock(np); |
483a1d10 | 7516 | nfs_buf_release(bp, 1); |
91447636 A |
7517 | } else { |
7518 | lck_mtx_unlock(nfs_buf_mutex); | |
1c79356b | 7519 | } |
1c79356b A |
7520 | } |
7521 | ||
2d21ac55 | 7522 | thd = vfs_context_thread(ap->a_context); |
1c79356b | 7523 | cred = ubc_getcred(vp); |
0c530ab8 | 7524 | if (!IS_VALID_CRED(cred)) |
91447636 | 7525 | cred = vfs_context_ucred(ap->a_context); |
1c79356b | 7526 | |
b0d623f7 | 7527 | nfs_node_lock_force(np); |
1c79356b | 7528 | if (np->n_flag & NWRITEERR) { |
2d21ac55 | 7529 | error = np->n_error; |
b0d623f7 A |
7530 | nfs_node_unlock(np); |
7531 | nfs_data_unlock_noupdate(np); | |
fa4905b1 A |
7532 | if (!nofreeupl) |
7533 | ubc_upl_abort_range(pl, pl_offset, size, | |
7534 | UPL_ABORT_FREE_ON_EMPTY); | |
2d21ac55 | 7535 | return (error); |
1c79356b | 7536 | } |
b0d623f7 | 7537 | nfs_node_unlock(np); |
1c79356b | 7538 | |
91447636 | 7539 | if (f_offset < 0 || f_offset >= (off_t)np->n_size || |
55e303ae | 7540 | f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) { |
b0d623f7 | 7541 | nfs_data_unlock_noupdate(np); |
fa4905b1 A |
7542 | if (!nofreeupl) |
7543 | ubc_upl_abort_range(pl, pl_offset, size, | |
7544 | UPL_ABORT_FREE_ON_EMPTY); | |
1c79356b A |
7545 | return (EINVAL); |
7546 | } | |
7547 | ||
b0d623f7 A |
7548 | kret = ubc_upl_map(pl, &ioaddr); |
7549 | if (kret != KERN_SUCCESS) | |
7550 | panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret); | |
55e303ae | 7551 | ioaddr += pl_offset; |
1c79356b | 7552 | |
91447636 | 7553 | if ((u_quad_t)f_offset + size > np->n_size) |
55e303ae | 7554 | xsize = np->n_size - f_offset; |
1c79356b | 7555 | else |
55e303ae | 7556 | xsize = size; |
1c79356b | 7557 | |
55e303ae | 7558 | pgsize = round_page_64(xsize); |
2d21ac55 A |
7559 | if ((size > pgsize) && !nofreeupl) |
7560 | ubc_upl_abort_range(pl, pl_offset + pgsize, size - pgsize, | |
7561 | UPL_ABORT_FREE_ON_EMPTY); | |
1c79356b | 7562 | |
2d21ac55 | 7563 | /* |
1c79356b A |
7564 | * check for partial page and clear the |
7565 | * contents past end of the file before | |
7566 | * releasing it in the VM page cache | |
7567 | */ | |
91447636 | 7568 | if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) { |
1c79356b | 7569 | size_t io = np->n_size - f_offset; |
55e303ae | 7570 | bzero((caddr_t)(ioaddr + io), size - io); |
fa4905b1 | 7571 | FSDBG(321, np->n_size, f_offset, f_offset + io, size - io); |
1c79356b | 7572 | } |
b0d623f7 | 7573 | nfs_data_unlock_noupdate(np); |
1c79356b | 7574 | |
b0d623f7 A |
7575 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE, |
7576 | &uio_buf, sizeof(uio_buf)); | |
55e303ae | 7577 | |
2d21ac55 | 7578 | tryagain: |
b0d623f7 A |
7579 | if (nmp->nm_vers >= NFS_VER4) |
7580 | stategenid = nmp->nm_stategenid; | |
2d21ac55 A |
7581 | wverf = wverf2 = wverfset = 0; |
7582 | txsize = rxsize = xsize; | |
7583 | txoffset = rxoffset = f_offset; | |
7584 | txaddr = rxaddr = ioaddr; | |
7585 | commit = NFS_WRITE_FILESYNC; | |
7586 | ||
7587 | bzero(req, sizeof(req)); | |
7588 | nextsend = nextwait = 0; | |
1c79356b | 7589 | do { |
6d2010ae A |
7590 | if (np->n_flag & NREVOKE) { |
7591 | error = EIO; | |
7592 | break; | |
7593 | } | |
2d21ac55 A |
7594 | /* send requests while we need to and have available slots */ |
7595 | while ((txsize > 0) && (req[nextsend] == NULL)) { | |
7596 | iosize = MIN(nmwsize, txsize); | |
b0d623f7 A |
7597 | uio_reset(auio, txoffset, UIO_SYSSPACE, UIO_WRITE); |
7598 | uio_addiov(auio, CAST_USER_ADDR_T(txaddr), iosize); | |
7599 | FSDBG(323, uio_offset(auio), iosize, txaddr, txsize); | |
316670eb | 7600 | OSAddAtomic64(1, &nfsstats.pageouts); |
b0d623f7 A |
7601 | nfs_node_lock_force(np); |
7602 | np->n_numoutput++; | |
7603 | nfs_node_unlock(np); | |
2d21ac55 A |
7604 | vnode_startwrite(vp); |
7605 | iomode = NFS_WRITE_UNSTABLE; | |
b0d623f7 | 7606 | if ((error = nmp->nm_funcs->nf_write_rpc_async(np, auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) { |
2d21ac55 A |
7607 | req[nextsend] = NULL; |
7608 | vnode_writedone(vp); | |
b0d623f7 A |
7609 | nfs_node_lock_force(np); |
7610 | np->n_numoutput--; | |
7611 | nfs_node_unlock(np); | |
2d21ac55 A |
7612 | break; |
7613 | } | |
7614 | txaddr += iosize; | |
7615 | txoffset += iosize; | |
7616 | txsize -= iosize; | |
7617 | nextsend = (nextsend + 1) % MAXPAGINGREQS; | |
7618 | } | |
7619 | /* wait while we need to and break out if more requests to send */ | |
7620 | while ((rxsize > 0) && req[nextwait]) { | |
7621 | iosize = remsize = MIN(nmwsize, rxsize); | |
7622 | error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req[nextwait], &iomode, &iosize, &wverf2); | |
7623 | req[nextwait] = NULL; | |
7624 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
7625 | vnode_writedone(vp); | |
b0d623f7 A |
7626 | nfs_node_lock_force(np); |
7627 | np->n_numoutput--; | |
7628 | nfs_node_unlock(np); | |
7629 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { | |
7630 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
7631 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
7632 | NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error); | |
7633 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
7634 | } |
7635 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
7636 | restart = 1; |
7637 | goto cancel; | |
7638 | } | |
2d21ac55 A |
7639 | if (error) { |
7640 | FSDBG(323, rxoffset, rxsize, error, -1); | |
7641 | break; | |
7642 | } | |
7643 | if (!wverfset) { | |
7644 | wverf = wverf2; | |
7645 | wverfset = 1; | |
7646 | } else if (wverf != wverf2) { | |
7647 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 7648 | vrestart = 1; |
2d21ac55 A |
7649 | goto cancel; |
7650 | } | |
7651 | /* Retain the lowest commitment level returned. */ | |
7652 | if (iomode < commit) | |
7653 | commit = iomode; | |
7654 | rxaddr += iosize; | |
7655 | rxoffset += iosize; | |
7656 | rxsize -= iosize; | |
7657 | remsize -= iosize; | |
7658 | if (remsize > 0) { | |
7659 | /* need to try sending the remainder */ | |
7660 | iosize = remsize; | |
b0d623f7 A |
7661 | uio_reset(auio, rxoffset, UIO_SYSSPACE, UIO_WRITE); |
7662 | uio_addiov(auio, CAST_USER_ADDR_T(rxaddr), remsize); | |
2d21ac55 | 7663 | iomode = NFS_WRITE_UNSTABLE; |
b0d623f7 A |
7664 | error = nfs_write_rpc2(np, auio, thd, cred, &iomode, &wverf2); |
7665 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { | |
6d2010ae | 7666 | NP(np, "nfs_vnop_pageout: restart: error %d", error); |
b0d623f7 | 7667 | lck_mtx_lock(&nmp->nm_lock); |
6d2010ae A |
7668 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
7669 | NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error); | |
7670 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
7671 | } |
7672 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
7673 | restart = 1; |
7674 | goto cancel; | |
7675 | } | |
2d21ac55 A |
7676 | if (error) { |
7677 | FSDBG(323, rxoffset, rxsize, error, -1); | |
7678 | break; | |
7679 | } | |
7680 | if (wverf != wverf2) { | |
7681 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 7682 | vrestart = 1; |
2d21ac55 A |
7683 | goto cancel; |
7684 | } | |
7685 | if (iomode < commit) | |
7686 | commit = iomode; | |
7687 | rxaddr += iosize; | |
7688 | rxoffset += iosize; | |
7689 | rxsize -= iosize; | |
7690 | } | |
7691 | if (txsize) | |
7692 | break; | |
7693 | } | |
7694 | } while (!error && (txsize || rxsize)); | |
7695 | ||
b0d623f7 | 7696 | vrestart = 0; |
2d21ac55 A |
7697 | |
7698 | if (!error && (commit != NFS_WRITE_FILESYNC)) { | |
6d2010ae | 7699 | error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred, wverf); |
2d21ac55 | 7700 | if (error == NFSERR_STALEWRITEVERF) { |
b0d623f7 | 7701 | vrestart = 1; |
2d21ac55 A |
7702 | error = EIO; |
7703 | } | |
7704 | } | |
7705 | ||
7706 | if (error) { | |
7707 | cancel: | |
7708 | /* cancel any outstanding requests */ | |
7709 | while (req[nextwait]) { | |
7710 | nfs_request_async_cancel(req[nextwait]); | |
7711 | req[nextwait] = NULL; | |
7712 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
7713 | vnode_writedone(vp); | |
b0d623f7 A |
7714 | nfs_node_lock_force(np); |
7715 | np->n_numoutput--; | |
7716 | nfs_node_unlock(np); | |
7717 | } | |
6d2010ae A |
7718 | if (np->n_flag & NREVOKE) { |
7719 | error = EIO; | |
7720 | } else { | |
7721 | if (vrestart) { | |
7722 | if (++vrestarts <= 100) /* guard against no progress */ | |
7723 | goto tryagain; | |
7724 | NP(np, "nfs_pageout: too many restarts, aborting"); | |
7725 | FSDBG(323, f_offset, xsize, ERESTART, -1); | |
7726 | } | |
7727 | if (restart) { | |
7728 | if (restarts <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */ | |
7729 | if (error == NFSERR_GRACE) | |
7730 | tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz); | |
7731 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) | |
7732 | goto tryagain; | |
7733 | } else { | |
7734 | NP(np, "nfs_pageout: too many restarts, aborting"); | |
7735 | FSDBG(323, f_offset, xsize, ERESTART, -1); | |
7736 | } | |
7737 | } | |
2d21ac55 A |
7738 | } |
7739 | } | |
7740 | ||
0b4e3aa0 | 7741 | ubc_upl_unmap(pl); |
2d21ac55 | 7742 | |
0b4e3aa0 A |
7743 | /* |
7744 | * We've had several different solutions on what to do when the pageout | |
2d21ac55 A |
7745 | * gets an error. If we don't handle it, and return an error to the |
7746 | * caller, vm, it will retry . This can end in endless looping | |
0b4e3aa0 A |
7747 | * between vm and here doing retries of the same page. Doing a dump |
7748 | * back to vm, will get it out of vm's knowledge and we lose whatever | |
7749 | * data existed. This is risky, but in some cases necessary. For | |
7750 | * example, the initial fix here was to do that for ESTALE. In that case | |
2d21ac55 A |
7751 | * the server is telling us that the file is no longer the same. We |
7752 | * would not want to keep paging out to that. We also saw some 151 | |
0b4e3aa0 | 7753 | * errors from Auspex server and NFSv3 can return errors higher than |
fa4905b1 A |
7754 | * ELAST. Those along with NFS known server errors we will "dump" from |
7755 | * vm. Errors we don't expect to occur, we dump and log for further | |
0b4e3aa0 A |
7756 | * analysis. Errors that could be transient, networking ones, |
7757 | * we let vm "retry". Lastly, errors that we retry, but may have potential | |
7758 | * to storm the network, we "retrywithsleep". "sever" will be used in | |
7759 | * in the future to dump all pages of object for cases like ESTALE. | |
7760 | * All this is the basis for the states returned and first guesses on | |
7761 | * error handling. Tweaking expected as more statistics are gathered. | |
7762 | * Note, in the long run we may need another more robust solution to | |
7763 | * have some kind of persistant store when the vm cannot dump nor keep | |
fa4905b1 | 7764 | * retrying as a solution, but this would be a file architectural change |
0b4e3aa0 | 7765 | */ |
fa4905b1 | 7766 | if (!nofreeupl) { /* otherwise stacked file system has to handle this */ |
0b4e3aa0 | 7767 | if (error) { |
2d21ac55 A |
7768 | int abortflags = 0; |
7769 | char action = nfs_pageouterrorhandler(error); | |
0b4e3aa0 A |
7770 | |
7771 | switch (action) { | |
7772 | case DUMP: | |
7773 | abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY; | |
7774 | break; | |
7775 | case DUMPANDLOG: | |
7776 | abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY; | |
2d21ac55 A |
7777 | if (error <= NFS_ELAST) { |
7778 | if ((errorcount[error] % 100) == 0) | |
6d2010ae | 7779 | NP(np, "nfs_pageout: unexpected error %d. dumping vm page", error); |
2d21ac55 A |
7780 | errorcount[error]++; |
7781 | } | |
0b4e3aa0 A |
7782 | break; |
7783 | case RETRY: | |
7784 | abortflags = UPL_ABORT_FREE_ON_EMPTY; | |
7785 | break; | |
0b4e3aa0 A |
7786 | case SEVER: /* not implemented */ |
7787 | default: | |
6d2010ae | 7788 | NP(np, "nfs_pageout: action %d not expected", action); |
0b4e3aa0 A |
7789 | break; |
7790 | } | |
2d21ac55 A |
7791 | |
7792 | ubc_upl_abort_range(pl, pl_offset, pgsize, abortflags); | |
0b4e3aa0 | 7793 | /* return error in all cases above */ |
2d21ac55 A |
7794 | |
7795 | } else { | |
0b4e3aa0 | 7796 | ubc_upl_commit_range(pl, pl_offset, pgsize, |
fa4905b1 A |
7797 | UPL_COMMIT_CLEAR_DIRTY | |
7798 | UPL_COMMIT_FREE_ON_EMPTY); | |
2d21ac55 | 7799 | } |
1c79356b | 7800 | } |
1c79356b A |
7801 | return (error); |
7802 | } | |
7803 | ||
7804 | /* Blktooff derives file offset given a logical block number */ | |
b0d623f7 | 7805 | int |
2d21ac55 | 7806 | nfs_vnop_blktooff( |
91447636 A |
7807 | struct vnop_blktooff_args /* { |
7808 | struct vnodeop_desc *a_desc; | |
7809 | vnode_t a_vp; | |
7810 | daddr64_t a_lblkno; | |
7811 | off_t *a_offset; | |
2d21ac55 | 7812 | } */ *ap) |
1c79356b A |
7813 | { |
7814 | int biosize; | |
91447636 | 7815 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7816 | struct nfsmount *nmp = VTONMP(vp); |
1c79356b | 7817 | |
fe8ab488 | 7818 | if (nfs_mount_gone(nmp)) |
55e303ae | 7819 | return (ENXIO); |
0c530ab8 | 7820 | biosize = nmp->nm_biosize; |
1c79356b | 7821 | |
91447636 | 7822 | *ap->a_offset = (off_t)(ap->a_lblkno * biosize); |
1c79356b A |
7823 | |
7824 | return (0); | |
7825 | } | |
7826 | ||
b0d623f7 | 7827 | int |
2d21ac55 | 7828 | nfs_vnop_offtoblk( |
91447636 A |
7829 | struct vnop_offtoblk_args /* { |
7830 | struct vnodeop_desc *a_desc; | |
7831 | vnode_t a_vp; | |
7832 | off_t a_offset; | |
7833 | daddr64_t *a_lblkno; | |
2d21ac55 | 7834 | } */ *ap) |
1c79356b A |
7835 | { |
7836 | int biosize; | |
91447636 | 7837 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7838 | struct nfsmount *nmp = VTONMP(vp); |
1c79356b | 7839 | |
fe8ab488 | 7840 | if (nfs_mount_gone(nmp)) |
55e303ae | 7841 | return (ENXIO); |
0c530ab8 | 7842 | biosize = nmp->nm_biosize; |
1c79356b | 7843 | |
91447636 | 7844 | *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize); |
1c79356b A |
7845 | |
7846 | return (0); | |
7847 | } | |
91447636 | 7848 | |
6d2010ae A |
7849 | /* |
7850 | * vnode change monitoring | |
7851 | */ | |
7852 | int | |
7853 | nfs_vnop_monitor( | |
7854 | struct vnop_monitor_args /* { | |
7855 | struct vnodeop_desc *a_desc; | |
7856 | vnode_t a_vp; | |
7857 | uint32_t a_events; | |
7858 | uint32_t a_flags; | |
7859 | void *a_handle; | |
7860 | vfs_context_t a_context; | |
7861 | } */ *ap) | |
7862 | { | |
7863 | nfsnode_t np = VTONFS(ap->a_vp); | |
7864 | struct nfsmount *nmp = VTONMP(ap->a_vp); | |
7865 | int error = 0; | |
7866 | ||
fe8ab488 | 7867 | if (nfs_mount_gone(nmp)) |
6d2010ae A |
7868 | return (ENXIO); |
7869 | ||
7870 | /* make sure that the vnode's monitoring status is up to date */ | |
7871 | lck_mtx_lock(&nmp->nm_lock); | |
7872 | if (vnode_ismonitored(ap->a_vp)) { | |
7873 | /* This vnode is currently being monitored, make sure we're tracking it. */ | |
7874 | if (np->n_monlink.le_next == NFSNOLIST) { | |
7875 | LIST_INSERT_HEAD(&nmp->nm_monlist, np, n_monlink); | |
7876 | nfs_mount_sock_thread_wake(nmp); | |
7877 | } | |
7878 | } else { | |
7879 | /* This vnode is no longer being monitored, make sure we're not tracking it. */ | |
7880 | /* Wait for any in-progress getattr to complete first. */ | |
7881 | while (np->n_mflag & NMMONSCANINPROG) { | |
7882 | struct timespec ts = { 1, 0 }; | |
7883 | np->n_mflag |= NMMONSCANWANT; | |
7884 | msleep(&np->n_mflag, &nmp->nm_lock, PZERO-1, "nfswaitmonscan", &ts); | |
7885 | } | |
7886 | if (np->n_monlink.le_next != NFSNOLIST) { | |
7887 | LIST_REMOVE(np, n_monlink); | |
7888 | np->n_monlink.le_next = NFSNOLIST; | |
7889 | } | |
7890 | } | |
7891 | lck_mtx_unlock(&nmp->nm_lock); | |
7892 | ||
7893 | return (error); | |
7894 | } | |
7895 | ||
7896 | /* | |
7897 | * Send a vnode notification for the given events. | |
7898 | */ | |
7899 | void | |
7900 | nfs_vnode_notify(nfsnode_t np, uint32_t events) | |
7901 | { | |
7902 | struct nfsmount *nmp = NFSTONMP(np); | |
7903 | struct nfs_vattr nvattr; | |
7904 | struct vnode_attr vattr, *vap = NULL; | |
7905 | struct timeval now; | |
7906 | ||
7907 | microuptime(&now); | |
7908 | if ((np->n_evtstamp == now.tv_sec) || !nmp) { | |
7909 | /* delay sending this notify */ | |
7910 | np->n_events |= events; | |
7911 | return; | |
7912 | } | |
7913 | events |= np->n_events; | |
7914 | np->n_events = 0; | |
7915 | np->n_evtstamp = now.tv_sec; | |
7916 | ||
7917 | vfs_get_notify_attributes(&vattr); | |
7918 | if (!nfs_getattrcache(np, &nvattr, 0)) { | |
7919 | vap = &vattr; | |
7920 | VATTR_INIT(vap); | |
5ba3f43e A |
7921 | |
7922 | vap->va_flags |= nmp->nm_vers > 2 ? VA_64BITOBJIDS : 0; | |
6d2010ae A |
7923 | VATTR_RETURN(vap, va_fsid, vfs_statfs(nmp->nm_mountp)->f_fsid.val[0]); |
7924 | VATTR_RETURN(vap, va_fileid, nvattr.nva_fileid); | |
7925 | VATTR_RETURN(vap, va_mode, nvattr.nva_mode); | |
7926 | VATTR_RETURN(vap, va_uid, nvattr.nva_uid); | |
7927 | VATTR_RETURN(vap, va_gid, nvattr.nva_gid); | |
7928 | VATTR_RETURN(vap, va_nlink, nvattr.nva_nlink); | |
7929 | } | |
7930 | vnode_notify(NFSTOV(np), events, vap); | |
7931 | } |