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