]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 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> | |
cb323159 | 90 | #include <sys/xattr.h> |
1c79356b | 91 | |
1c79356b A |
92 | #include <vfs/vfs_support.h> |
93 | ||
94 | #include <sys/vm.h> | |
1c79356b | 95 | |
2d21ac55 | 96 | #include <sys/time.h> |
1c79356b | 97 | #include <kern/clock.h> |
91447636 | 98 | #include <libkern/OSAtomic.h> |
1c79356b A |
99 | |
100 | #include <miscfs/fifofs/fifo.h> | |
101 | #include <miscfs/specfs/specdev.h> | |
102 | ||
103 | #include <nfs/rpcv2.h> | |
104 | #include <nfs/nfsproto.h> | |
105 | #include <nfs/nfs.h> | |
106 | #include <nfs/nfsnode.h> | |
2d21ac55 | 107 | #include <nfs/nfs_gss.h> |
1c79356b | 108 | #include <nfs/nfsmount.h> |
55e303ae | 109 | #include <nfs/nfs_lock.h> |
1c79356b A |
110 | #include <nfs/xdr_subs.h> |
111 | #include <nfs/nfsm_subs.h> | |
1c79356b A |
112 | |
113 | #include <net/if.h> | |
114 | #include <netinet/in.h> | |
115 | #include <netinet/in_var.h> | |
b0d623f7 | 116 | |
1c79356b | 117 | #include <vm/vm_kern.h> |
b0d623f7 | 118 | #include <vm/vm_pageout.h> |
1c79356b | 119 | |
9bccf70c A |
120 | #include <kern/task.h> |
121 | #include <kern/sched_prim.h> | |
1c79356b | 122 | |
39236c6e | 123 | #define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__) |
fe8ab488 | 124 | #define DEFAULT_READLINK_NOCACHE 0 |
39236c6e | 125 | |
1c79356b | 126 | /* |
2d21ac55 | 127 | * NFS vnode ops |
1c79356b | 128 | */ |
0a7de745 A |
129 | int nfs_vnop_lookup(struct vnop_lookup_args *); |
130 | int nfsspec_vnop_read(struct vnop_read_args *); | |
131 | int nfsspec_vnop_write(struct vnop_write_args *); | |
132 | int nfsspec_vnop_close(struct vnop_close_args *); | |
2d21ac55 | 133 | #if FIFO |
0a7de745 A |
134 | int nfsfifo_vnop_read(struct vnop_read_args *); |
135 | int nfsfifo_vnop_write(struct vnop_write_args *); | |
136 | int nfsfifo_vnop_close(struct vnop_close_args *); | |
2d21ac55 | 137 | #endif |
0a7de745 A |
138 | int nfs_vnop_ioctl(struct vnop_ioctl_args *); |
139 | int nfs_vnop_select(struct vnop_select_args *); | |
140 | int nfs_vnop_setattr(struct vnop_setattr_args *); | |
141 | int nfs_vnop_fsync(struct vnop_fsync_args *); | |
142 | int nfs_vnop_rename(struct vnop_rename_args *); | |
143 | int nfs_vnop_readdir(struct vnop_readdir_args *); | |
144 | int nfs_vnop_readlink(struct vnop_readlink_args *); | |
145 | int nfs_vnop_pathconf(struct vnop_pathconf_args *); | |
146 | int nfs_vnop_pagein(struct vnop_pagein_args *); | |
147 | int nfs_vnop_pageout(struct vnop_pageout_args *); | |
148 | int nfs_vnop_blktooff(struct vnop_blktooff_args *); | |
149 | int nfs_vnop_offtoblk(struct vnop_offtoblk_args *); | |
150 | int nfs_vnop_blockmap(struct vnop_blockmap_args *); | |
151 | int nfs_vnop_monitor(struct vnop_monitor_args *); | |
152 | ||
153 | int nfs3_vnop_create(struct vnop_create_args *); | |
154 | int nfs3_vnop_mknod(struct vnop_mknod_args *); | |
155 | int nfs3_vnop_getattr(struct vnop_getattr_args *); | |
156 | int nfs3_vnop_link(struct vnop_link_args *); | |
157 | int nfs3_vnop_mkdir(struct vnop_mkdir_args *); | |
158 | int nfs3_vnop_rmdir(struct vnop_rmdir_args *); | |
159 | int nfs3_vnop_symlink(struct vnop_symlink_args *); | |
2d21ac55 | 160 | |
cb323159 | 161 | |
91447636 | 162 | vnop_t **nfsv2_vnodeop_p; |
cb323159 A |
163 | static const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = { |
164 | { .opve_op = &vnop_default_desc, .opve_impl = (vnop_t *)vn_default_error }, | |
165 | { .opve_op = &vnop_lookup_desc, .opve_impl = (vnop_t *)nfs_vnop_lookup }, /* lookup */ | |
166 | { .opve_op = &vnop_create_desc, .opve_impl = (vnop_t *)nfs3_vnop_create }, /* create */ | |
167 | { .opve_op = &vnop_mknod_desc, .opve_impl = (vnop_t *)nfs3_vnop_mknod }, /* mknod */ | |
168 | { .opve_op = &vnop_open_desc, .opve_impl = (vnop_t *)nfs_vnop_open }, /* open */ | |
169 | { .opve_op = &vnop_close_desc, .opve_impl = (vnop_t *)nfs_vnop_close }, /* close */ | |
170 | { .opve_op = &vnop_access_desc, .opve_impl = (vnop_t *)nfs_vnop_access }, /* access */ | |
171 | { .opve_op = &vnop_getattr_desc, .opve_impl = (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
172 | { .opve_op = &vnop_setattr_desc, .opve_impl = (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
173 | { .opve_op = &vnop_read_desc, .opve_impl = (vnop_t *)nfs_vnop_read }, /* read */ | |
174 | { .opve_op = &vnop_write_desc, .opve_impl = (vnop_t *)nfs_vnop_write }, /* write */ | |
175 | { .opve_op = &vnop_ioctl_desc, .opve_impl = (vnop_t *)nfs_vnop_ioctl }, /* ioctl */ | |
176 | { .opve_op = &vnop_select_desc, .opve_impl = (vnop_t *)nfs_vnop_select }, /* select */ | |
177 | { .opve_op = &vnop_revoke_desc, .opve_impl = (vnop_t *)nfs_vnop_revoke }, /* revoke */ | |
178 | { .opve_op = &vnop_mmap_desc, .opve_impl = (vnop_t *)nfs_vnop_mmap }, /* mmap */ | |
179 | { .opve_op = &vnop_mnomap_desc, .opve_impl = (vnop_t *)nfs_vnop_mnomap }, /* mnomap */ | |
180 | { .opve_op = &vnop_fsync_desc, .opve_impl = (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
181 | { .opve_op = &vnop_remove_desc, .opve_impl = (vnop_t *)nfs_vnop_remove }, /* remove */ | |
182 | { .opve_op = &vnop_link_desc, .opve_impl = (vnop_t *)nfs3_vnop_link }, /* link */ | |
183 | { .opve_op = &vnop_rename_desc, .opve_impl = (vnop_t *)nfs_vnop_rename }, /* rename */ | |
184 | { .opve_op = &vnop_mkdir_desc, .opve_impl = (vnop_t *)nfs3_vnop_mkdir }, /* mkdir */ | |
185 | { .opve_op = &vnop_rmdir_desc, .opve_impl = (vnop_t *)nfs3_vnop_rmdir }, /* rmdir */ | |
186 | { .opve_op = &vnop_symlink_desc, .opve_impl = (vnop_t *)nfs3_vnop_symlink }, /* symlink */ | |
187 | { .opve_op = &vnop_readdir_desc, .opve_impl = (vnop_t *)nfs_vnop_readdir }, /* readdir */ | |
188 | { .opve_op = &vnop_readlink_desc, .opve_impl = (vnop_t *)nfs_vnop_readlink }, /* readlink */ | |
189 | { .opve_op = &vnop_inactive_desc, .opve_impl = (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
190 | { .opve_op = &vnop_reclaim_desc, .opve_impl = (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
191 | { .opve_op = &vnop_strategy_desc, .opve_impl = (vnop_t *)err_strategy }, /* strategy */ | |
192 | { .opve_op = &vnop_pathconf_desc, .opve_impl = (vnop_t *)nfs_vnop_pathconf }, /* pathconf */ | |
193 | { .opve_op = &vnop_advlock_desc, .opve_impl = (vnop_t *)nfs_vnop_advlock }, /* advlock */ | |
194 | { .opve_op = &vnop_bwrite_desc, .opve_impl = (vnop_t *)err_bwrite }, /* bwrite */ | |
195 | { .opve_op = &vnop_pagein_desc, .opve_impl = (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
196 | { .opve_op = &vnop_pageout_desc, .opve_impl = (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
197 | { .opve_op = &vnop_copyfile_desc, .opve_impl = (vnop_t *)err_copyfile }, /* Copyfile */ | |
198 | { .opve_op = &vnop_blktooff_desc, .opve_impl = (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
199 | { .opve_op = &vnop_offtoblk_desc, .opve_impl = (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
200 | { .opve_op = &vnop_blockmap_desc, .opve_impl = (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
201 | { .opve_op = &vnop_monitor_desc, .opve_impl = (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
202 | { .opve_op = NULL, .opve_impl = NULL } | |
1c79356b | 203 | }; |
cb323159 | 204 | const struct vnodeopv_desc nfsv2_vnodeop_opv_desc = |
0a7de745 | 205 | { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries }; |
2d21ac55 | 206 | |
cb323159 A |
207 | |
208 | #if CONFIG_NFS4 | |
2d21ac55 | 209 | vnop_t **nfsv4_vnodeop_p; |
cb323159 | 210 | static const struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = { |
2d21ac55 | 211 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
212 | { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup }, /* lookup */ |
213 | { &vnop_create_desc, (vnop_t *)nfs4_vnop_create }, /* create */ | |
214 | { &vnop_mknod_desc, (vnop_t *)nfs4_vnop_mknod }, /* mknod */ | |
215 | { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */ | |
216 | { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */ | |
217 | { &vnop_access_desc, (vnop_t *)nfs_vnop_access }, /* access */ | |
218 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
219 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
220 | { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */ | |
221 | { &vnop_write_desc, (vnop_t *)nfs_vnop_write }, /* write */ | |
222 | { &vnop_ioctl_desc, (vnop_t *)nfs_vnop_ioctl }, /* ioctl */ | |
223 | { &vnop_select_desc, (vnop_t *)nfs_vnop_select }, /* select */ | |
224 | { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke }, /* revoke */ | |
225 | { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */ | |
226 | { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */ | |
227 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
228 | { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove }, /* remove */ | |
229 | { &vnop_link_desc, (vnop_t *)nfs4_vnop_link }, /* link */ | |
230 | { &vnop_rename_desc, (vnop_t *)nfs_vnop_rename }, /* rename */ | |
231 | { &vnop_mkdir_desc, (vnop_t *)nfs4_vnop_mkdir }, /* mkdir */ | |
232 | { &vnop_rmdir_desc, (vnop_t *)nfs4_vnop_rmdir }, /* rmdir */ | |
233 | { &vnop_symlink_desc, (vnop_t *)nfs4_vnop_symlink }, /* symlink */ | |
234 | { &vnop_readdir_desc, (vnop_t *)nfs_vnop_readdir }, /* readdir */ | |
235 | { &vnop_readlink_desc, (vnop_t *)nfs_vnop_readlink }, /* readlink */ | |
236 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
237 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
238 | { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */ | |
239 | { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */ | |
240 | { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */ | |
241 | { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */ | |
242 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
243 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
244 | { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */ | |
245 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
246 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
247 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
248 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ | |
249 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
6d2010ae A |
250 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ |
251 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
252 | #if NAMEDSTREAMS | |
0a7de745 A |
253 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ |
254 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
6d2010ae A |
255 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ |
256 | #endif | |
0a7de745 | 257 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
2d21ac55 A |
258 | { NULL, NULL } |
259 | }; | |
cb323159 | 260 | const struct vnodeopv_desc nfsv4_vnodeop_opv_desc = |
0a7de745 | 261 | { &nfsv4_vnodeop_p, nfsv4_vnodeop_entries }; |
cb323159 | 262 | #endif |
1c79356b A |
263 | |
264 | /* | |
265 | * Special device vnode ops | |
266 | */ | |
91447636 | 267 | vnop_t **spec_nfsv2nodeop_p; |
cb323159 | 268 | static const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = { |
91447636 | 269 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
270 | { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */ |
271 | { &vnop_create_desc, (vnop_t *)spec_create }, /* create */ | |
272 | { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */ | |
273 | { &vnop_open_desc, (vnop_t *)spec_open }, /* open */ | |
274 | { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */ | |
275 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
276 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
277 | { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */ | |
278 | { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */ | |
279 | { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */ | |
280 | { &vnop_select_desc, (vnop_t *)spec_select }, /* select */ | |
281 | { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */ | |
282 | { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */ | |
283 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
284 | { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */ | |
285 | { &vnop_link_desc, (vnop_t *)spec_link }, /* link */ | |
286 | { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */ | |
287 | { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */ | |
288 | { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */ | |
289 | { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */ | |
290 | { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */ | |
291 | { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */ | |
292 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
293 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
294 | { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */ | |
295 | { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */ | |
296 | { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */ | |
297 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
298 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
299 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
300 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
301 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
302 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
303 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
1c79356b A |
304 | { NULL, NULL } |
305 | }; | |
cb323159 | 306 | const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc = |
0a7de745 | 307 | { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries }; |
cb323159 | 308 | #if CONFIG_NFS4 |
2d21ac55 | 309 | vnop_t **spec_nfsv4nodeop_p; |
cb323159 | 310 | static const struct vnodeopv_entry_desc spec_nfsv4nodeop_entries[] = { |
2d21ac55 | 311 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
312 | { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */ |
313 | { &vnop_create_desc, (vnop_t *)spec_create }, /* create */ | |
314 | { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */ | |
315 | { &vnop_open_desc, (vnop_t *)spec_open }, /* open */ | |
316 | { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */ | |
317 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
318 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
319 | { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */ | |
320 | { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */ | |
321 | { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */ | |
322 | { &vnop_select_desc, (vnop_t *)spec_select }, /* select */ | |
323 | { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */ | |
324 | { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */ | |
325 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
326 | { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */ | |
327 | { &vnop_link_desc, (vnop_t *)spec_link }, /* link */ | |
328 | { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */ | |
329 | { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */ | |
330 | { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */ | |
331 | { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */ | |
332 | { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */ | |
333 | { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */ | |
334 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
335 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
336 | { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */ | |
337 | { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */ | |
338 | { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */ | |
339 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
340 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
341 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
342 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
343 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
344 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
345 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ | |
346 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
6d2010ae A |
347 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ |
348 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
349 | #if NAMEDSTREAMS | |
0a7de745 A |
350 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ |
351 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
6d2010ae A |
352 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ |
353 | #endif | |
0a7de745 | 354 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
2d21ac55 A |
355 | { NULL, NULL } |
356 | }; | |
cb323159 | 357 | const struct vnodeopv_desc spec_nfsv4nodeop_opv_desc = |
0a7de745 | 358 | { &spec_nfsv4nodeop_p, spec_nfsv4nodeop_entries }; |
cb323159 | 359 | #endif /* CONFIG_NFS4 */ |
1c79356b | 360 | |
2d21ac55 | 361 | #if FIFO |
91447636 | 362 | vnop_t **fifo_nfsv2nodeop_p; |
cb323159 | 363 | static const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = { |
91447636 | 364 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
365 | { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */ |
366 | { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */ | |
367 | { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */ | |
368 | { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */ | |
369 | { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */ | |
370 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
371 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
372 | { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */ | |
373 | { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */ | |
374 | { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */ | |
375 | { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */ | |
376 | { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */ | |
377 | { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */ | |
378 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
379 | { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */ | |
380 | { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */ | |
381 | { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */ | |
382 | { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */ | |
383 | { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */ | |
384 | { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */ | |
385 | { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */ | |
386 | { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */ | |
387 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
388 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
389 | { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */ | |
390 | { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */ | |
391 | { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */ | |
392 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
393 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
394 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
395 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
396 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
397 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
398 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
1c79356b A |
399 | { NULL, NULL } |
400 | }; | |
cb323159 | 401 | const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc = |
0a7de745 | 402 | { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries }; |
cb323159 | 403 | #endif |
0b4e3aa0 | 404 | |
cb323159 A |
405 | #if CONFIG_NFS4 |
406 | #if FIFO | |
2d21ac55 | 407 | vnop_t **fifo_nfsv4nodeop_p; |
cb323159 | 408 | static const struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries[] = { |
2d21ac55 | 409 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
410 | { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */ |
411 | { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */ | |
412 | { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */ | |
413 | { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */ | |
414 | { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */ | |
415 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
416 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
417 | { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */ | |
418 | { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */ | |
419 | { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */ | |
420 | { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */ | |
421 | { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */ | |
422 | { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */ | |
423 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
424 | { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */ | |
425 | { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */ | |
426 | { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */ | |
427 | { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */ | |
428 | { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */ | |
429 | { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */ | |
430 | { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */ | |
431 | { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */ | |
432 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
433 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
434 | { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */ | |
435 | { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */ | |
436 | { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */ | |
437 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
438 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
439 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
440 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
441 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
442 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
443 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ | |
444 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
6d2010ae A |
445 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ |
446 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
447 | #if NAMEDSTREAMS | |
0a7de745 A |
448 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ |
449 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
6d2010ae A |
450 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ |
451 | #endif | |
0a7de745 | 452 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
2d21ac55 | 453 | { NULL, NULL } |
0b4e3aa0 | 454 | }; |
cb323159 | 455 | const struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc = |
0a7de745 | 456 | { &fifo_nfsv4nodeop_p, fifo_nfsv4nodeop_entries }; |
2d21ac55 | 457 | #endif /* FIFO */ |
cb323159 | 458 | #endif /* CONFIG_NFS4 */ |
0b4e3aa0 | 459 | |
0a7de745 A |
460 | int nfs_sillyrename(nfsnode_t, nfsnode_t, struct componentname *, vfs_context_t); |
461 | int nfs_getattr_internal(nfsnode_t, struct nfs_vattr *, vfs_context_t, int); | |
462 | int nfs_refresh_fh(nfsnode_t, vfs_context_t); | |
2d21ac55 | 463 | |
cb323159 | 464 | |
2d21ac55 A |
465 | /* |
466 | * Find the slot in the access cache for this UID. | |
467 | * If adding and no existing slot is found, reuse slots in FIFO order. | |
6d2010ae | 468 | * The index of the next slot to use is kept in the last entry of the n_access array. |
2d21ac55 A |
469 | */ |
470 | int | |
6d2010ae | 471 | nfs_node_access_slot(nfsnode_t np, uid_t uid, int add) |
0b4e3aa0 | 472 | { |
2d21ac55 A |
473 | int slot; |
474 | ||
0a7de745 A |
475 | for (slot = 0; slot < NFS_ACCESS_CACHE_SIZE; slot++) { |
476 | if (np->n_accessuid[slot] == uid) { | |
2d21ac55 | 477 | break; |
0a7de745 A |
478 | } |
479 | } | |
2d21ac55 | 480 | if (slot == NFS_ACCESS_CACHE_SIZE) { |
0a7de745 A |
481 | if (!add) { |
482 | return -1; | |
483 | } | |
6d2010ae A |
484 | slot = np->n_access[NFS_ACCESS_CACHE_SIZE]; |
485 | np->n_access[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE; | |
2d21ac55 | 486 | } |
0a7de745 | 487 | return slot; |
0b4e3aa0 | 488 | } |
1c79356b | 489 | |
2d21ac55 | 490 | int |
fe8ab488 | 491 | nfs3_access_rpc(nfsnode_t np, u_int32_t *access, int rpcflags, vfs_context_t ctx) |
1c79356b | 492 | { |
b0d623f7 | 493 | int error = 0, lockerror = ENOENT, status, slot; |
6d2010ae | 494 | uint32_t access_result = 0; |
fa4905b1 | 495 | u_int64_t xid; |
2d21ac55 | 496 | struct nfsm_chain nmreq, nmrep; |
813fb2f6 | 497 | struct nfsmount *nmp; |
55e303ae | 498 | struct timeval now; |
2d21ac55 A |
499 | uid_t uid; |
500 | ||
501 | nfsm_chain_null(&nmreq); | |
502 | nfsm_chain_null(&nmrep); | |
503 | ||
504 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3) + NFSX_UNSIGNED); | |
505 | nfsm_chain_add_fh(error, &nmreq, NFS_VER3, np->n_fhp, np->n_fhsize); | |
6d2010ae | 506 | nfsm_chain_add_32(error, &nmreq, *access); |
2d21ac55 A |
507 | nfsm_chain_build_done(error, &nmreq); |
508 | nfsmout_if(error); | |
fe8ab488 | 509 | error = nfs_request2(np, NULL, &nmreq, NFSPROC_ACCESS, |
0a7de745 A |
510 | vfs_context_thread(ctx), vfs_context_ucred(ctx), |
511 | NULL, rpcflags, &nmrep, &xid, &status); | |
512 | if ((lockerror = nfs_node_lock(np))) { | |
b0d623f7 | 513 | error = lockerror; |
0a7de745 | 514 | } |
2d21ac55 | 515 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 | 516 | if (!error) { |
2d21ac55 | 517 | error = status; |
0a7de745 | 518 | } |
6d2010ae | 519 | nfsm_chain_get_32(error, &nmrep, access_result); |
2d21ac55 | 520 | nfsmout_if(error); |
0b4e3aa0 | 521 | |
813fb2f6 A |
522 | /* XXXab do we really need mount here, also why are we doing access cache management here? */ |
523 | nmp = NFSTONMP(np); | |
524 | if (nfs_mount_gone(nmp)) { | |
525 | error = ENXIO; | |
526 | } | |
527 | nfsmout_if(error); | |
528 | ||
cb323159 | 529 | #if CONFIG_NFS_GSS |
813fb2f6 A |
530 | if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) { |
531 | uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx)); | |
532 | } else { | |
533 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
534 | } | |
cb323159 A |
535 | #else |
536 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
537 | #endif /* CONFIG_NFS_GSS */ | |
6d2010ae A |
538 | slot = nfs_node_access_slot(np, uid, 1); |
539 | np->n_accessuid[slot] = uid; | |
2d21ac55 | 540 | microuptime(&now); |
6d2010ae A |
541 | np->n_accessstamp[slot] = now.tv_sec; |
542 | np->n_access[slot] = access_result; | |
2d21ac55 A |
543 | |
544 | /* | |
545 | * If we asked for DELETE but didn't get it, the server | |
546 | * may simply not support returning that bit (possible | |
547 | * on UNIX systems). So, we'll assume that it is OK, | |
548 | * and just let any subsequent delete action fail if it | |
549 | * really isn't deletable. | |
550 | */ | |
6d2010ae | 551 | if ((*access & NFS_ACCESS_DELETE) && |
0a7de745 | 552 | !(np->n_access[slot] & NFS_ACCESS_DELETE)) { |
6d2010ae | 553 | np->n_access[slot] |= NFS_ACCESS_DELETE; |
0a7de745 | 554 | } |
6d2010ae | 555 | /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */ |
0a7de745 A |
556 | if (nfs_access_dotzfs && (np->n_flag & NISDOTZFSCHILD)) { |
557 | np->n_access[slot] |= (NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND | NFS_ACCESS_DELETE); | |
558 | } | |
6d2010ae A |
559 | /* pass back the access returned with this request */ |
560 | *access = np->n_access[slot]; | |
2d21ac55 | 561 | nfsmout: |
0a7de745 | 562 | if (!lockerror) { |
b0d623f7 | 563 | nfs_node_unlock(np); |
0a7de745 | 564 | } |
2d21ac55 A |
565 | nfsm_chain_cleanup(&nmreq); |
566 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 567 | return error; |
1c79356b A |
568 | } |
569 | ||
cb323159 | 570 | |
1c79356b | 571 | /* |
2d21ac55 A |
572 | * NFS access vnode op. |
573 | * For NFS version 2, just return ok. File accesses may fail later. | |
6d2010ae A |
574 | * For NFS version 3+, use the access RPC to check accessibility. If file |
575 | * permissions are changed on the server, accesses might still fail later. | |
1c79356b | 576 | */ |
2d21ac55 A |
577 | int |
578 | nfs_vnop_access( | |
91447636 | 579 | struct vnop_access_args /* { |
0a7de745 A |
580 | * struct vnodeop_desc *a_desc; |
581 | * vnode_t a_vp; | |
582 | * int a_action; | |
583 | * vfs_context_t a_context; | |
584 | * } */*ap) | |
1c79356b | 585 | { |
2d21ac55 | 586 | vfs_context_t ctx = ap->a_context; |
91447636 | 587 | vnode_t vp = ap->a_vp; |
fe8ab488 | 588 | int error = 0, slot, dorpc, rpcflags = 0; |
6d2010ae | 589 | u_int32_t access, waccess; |
2d21ac55 A |
590 | nfsnode_t np = VTONFS(vp); |
591 | struct nfsmount *nmp; | |
592 | int nfsvers; | |
55e303ae | 593 | struct timeval now; |
2d21ac55 A |
594 | uid_t uid; |
595 | ||
596 | nmp = VTONMP(vp); | |
0a7de745 A |
597 | if (nfs_mount_gone(nmp)) { |
598 | return ENXIO; | |
599 | } | |
2d21ac55 A |
600 | nfsvers = nmp->nm_vers; |
601 | ||
cb323159 A |
602 | |
603 | if (nfsvers == NFS_VER2 || NMFLAG(nmp, NOOPAQUE_AUTH)) { | |
2d21ac55 | 604 | if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) && |
0a7de745 A |
605 | vfs_isrdonly(vnode_mount(vp))) { |
606 | return EROFS; | |
607 | } | |
608 | return 0; | |
2d21ac55 | 609 | } |
1c79356b A |
610 | |
611 | /* | |
2d21ac55 | 612 | * For NFS v3, do an access rpc, otherwise you are stuck emulating |
1c79356b A |
613 | * ufs_access() locally using the vattr. This may not be correct, |
614 | * since the server may apply other access criteria such as | |
615 | * client uid-->server uid mapping that we do not know about, but | |
616 | * this is better than just returning anything that is lying about | |
617 | * in the cache. | |
618 | */ | |
91447636 | 619 | |
2d21ac55 A |
620 | /* |
621 | * Convert KAUTH primitives to NFS access rights. | |
622 | */ | |
6d2010ae | 623 | access = 0; |
2d21ac55 A |
624 | if (vnode_isdir(vp)) { |
625 | /* directory */ | |
626 | if (ap->a_action & | |
627 | (KAUTH_VNODE_LIST_DIRECTORY | | |
0a7de745 | 628 | KAUTH_VNODE_READ_EXTATTRIBUTES)) { |
6d2010ae | 629 | access |= NFS_ACCESS_READ; |
0a7de745 A |
630 | } |
631 | if (ap->a_action & KAUTH_VNODE_SEARCH) { | |
6d2010ae | 632 | access |= NFS_ACCESS_LOOKUP; |
0a7de745 | 633 | } |
2d21ac55 A |
634 | if (ap->a_action & |
635 | (KAUTH_VNODE_ADD_FILE | | |
0a7de745 | 636 | KAUTH_VNODE_ADD_SUBDIRECTORY)) { |
6d2010ae | 637 | access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; |
0a7de745 A |
638 | } |
639 | if (ap->a_action & KAUTH_VNODE_DELETE_CHILD) { | |
6d2010ae | 640 | access |= NFS_ACCESS_MODIFY; |
0a7de745 | 641 | } |
91447636 | 642 | } else { |
2d21ac55 A |
643 | /* file */ |
644 | if (ap->a_action & | |
645 | (KAUTH_VNODE_READ_DATA | | |
0a7de745 | 646 | KAUTH_VNODE_READ_EXTATTRIBUTES)) { |
6d2010ae | 647 | access |= NFS_ACCESS_READ; |
0a7de745 A |
648 | } |
649 | if (ap->a_action & KAUTH_VNODE_WRITE_DATA) { | |
6d2010ae | 650 | access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; |
0a7de745 A |
651 | } |
652 | if (ap->a_action & KAUTH_VNODE_APPEND_DATA) { | |
6d2010ae | 653 | access |= NFS_ACCESS_EXTEND; |
0a7de745 A |
654 | } |
655 | if (ap->a_action & KAUTH_VNODE_EXECUTE) { | |
6d2010ae | 656 | access |= NFS_ACCESS_EXECUTE; |
0a7de745 | 657 | } |
2d21ac55 A |
658 | } |
659 | /* common */ | |
0a7de745 | 660 | if (ap->a_action & KAUTH_VNODE_DELETE) { |
6d2010ae | 661 | access |= NFS_ACCESS_DELETE; |
0a7de745 | 662 | } |
2d21ac55 A |
663 | if (ap->a_action & |
664 | (KAUTH_VNODE_WRITE_ATTRIBUTES | | |
665 | KAUTH_VNODE_WRITE_EXTATTRIBUTES | | |
0a7de745 | 666 | KAUTH_VNODE_WRITE_SECURITY)) { |
6d2010ae | 667 | access |= NFS_ACCESS_MODIFY; |
0a7de745 | 668 | } |
2d21ac55 | 669 | /* XXX this is pretty dubious */ |
0a7de745 | 670 | if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER) { |
6d2010ae | 671 | access |= NFS_ACCESS_MODIFY; |
0a7de745 | 672 | } |
2d21ac55 A |
673 | |
674 | /* if caching, always ask for every right */ | |
675 | if (nfs_access_cache_timeout > 0) { | |
6d2010ae | 676 | waccess = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | |
0a7de745 A |
677 | NFS_ACCESS_EXTEND | NFS_ACCESS_EXECUTE | |
678 | NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; | |
2d21ac55 | 679 | } else { |
6d2010ae | 680 | waccess = access; |
2d21ac55 A |
681 | } |
682 | ||
0a7de745 A |
683 | if ((error = nfs_node_lock(np))) { |
684 | return error; | |
685 | } | |
2d21ac55 A |
686 | |
687 | /* | |
688 | * Does our cached result allow us to give a definite yes to | |
689 | * this request? | |
690 | */ | |
cb323159 | 691 | #if CONFIG_NFS_GSS |
0a7de745 | 692 | if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) { |
3e170ce0 | 693 | uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx)); |
0a7de745 | 694 | } else { |
3e170ce0 | 695 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); |
0a7de745 | 696 | } |
cb323159 A |
697 | #else |
698 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
699 | #endif /* CONFIG_NFS_GSS */ | |
6d2010ae | 700 | slot = nfs_node_access_slot(np, uid, 0); |
2d21ac55 | 701 | dorpc = 1; |
6d2010ae | 702 | if (access == 0) { |
b0d623f7 A |
703 | /* not asking for any rights understood by NFS, so don't bother doing an RPC */ |
704 | /* OSAddAtomic(1, &nfsstats.accesscache_hits); */ | |
705 | dorpc = 0; | |
6d2010ae A |
706 | waccess = 0; |
707 | } else if (NACCESSVALID(np, slot)) { | |
fe8ab488 A |
708 | microuptime(&now); |
709 | if (((now.tv_sec < (np->n_accessstamp[slot] + nfs_access_cache_timeout)) && | |
710 | ((np->n_access[slot] & access) == access)) || nfs_use_cache(nmp)) { | |
b0d623f7 | 711 | /* OSAddAtomic(1, &nfsstats.accesscache_hits); */ |
fe8ab488 A |
712 | dorpc = 0; |
713 | waccess = np->n_access[slot]; | |
1c79356b | 714 | } |
fa4905b1 | 715 | } |
b0d623f7 | 716 | nfs_node_unlock(np); |
2d21ac55 A |
717 | if (dorpc) { |
718 | /* Either a no, or a don't know. Go to the wire. */ | |
b0d623f7 | 719 | /* OSAddAtomic(1, &nfsstats.accesscache_misses); */ |
fe8ab488 A |
720 | |
721 | /* | |
722 | * Allow an access call to timeout if we have it cached | |
723 | * so we won't hang if the server isn't responding. | |
724 | */ | |
0a7de745 | 725 | if (NACCESSVALID(np, slot)) { |
fe8ab488 | 726 | rpcflags |= R_SOFT; |
0a7de745 | 727 | } |
fe8ab488 A |
728 | |
729 | error = nmp->nm_funcs->nf_access_rpc(np, &waccess, rpcflags, ctx); | |
730 | ||
731 | /* | |
732 | * If the server didn't respond return the cached access. | |
733 | */ | |
734 | if ((error == ETIMEDOUT) && (rpcflags & R_SOFT)) { | |
735 | error = 0; | |
736 | waccess = np->n_access[slot]; | |
737 | } | |
2d21ac55 | 738 | } |
0a7de745 | 739 | if (!error && ((waccess & access) != access)) { |
2d21ac55 | 740 | error = EACCES; |
0a7de745 | 741 | } |
91447636 | 742 | |
0a7de745 | 743 | return error; |
1c79356b A |
744 | } |
745 | ||
6d2010ae | 746 | |
1c79356b | 747 | /* |
2d21ac55 | 748 | * NFS open vnode op |
6d2010ae A |
749 | * |
750 | * Perform various update/invalidation checks and then add the | |
751 | * open to the node. Regular files will have an open file structure | |
752 | * on the node and, for NFSv4, perform an OPEN request on the server. | |
1c79356b | 753 | */ |
2d21ac55 | 754 | int |
6d2010ae | 755 | nfs_vnop_open( |
91447636 | 756 | struct vnop_open_args /* { |
0a7de745 A |
757 | * struct vnodeop_desc *a_desc; |
758 | * vnode_t a_vp; | |
759 | * int a_mode; | |
760 | * vfs_context_t a_context; | |
761 | * } */*ap) | |
1c79356b | 762 | { |
2d21ac55 | 763 | vfs_context_t ctx = ap->a_context; |
91447636 | 764 | vnode_t vp = ap->a_vp; |
2d21ac55 A |
765 | nfsnode_t np = VTONFS(vp); |
766 | struct nfsmount *nmp; | |
6d2010ae A |
767 | int error, accessMode, denyMode, opened = 0; |
768 | struct nfs_open_owner *noop = NULL; | |
769 | struct nfs_open_file *nofp = NULL; | |
91447636 | 770 | enum vtype vtype; |
6d2010ae | 771 | |
0a7de745 A |
772 | if (!(ap->a_mode & (FREAD | FWRITE))) { |
773 | return EINVAL; | |
774 | } | |
2d21ac55 A |
775 | |
776 | nmp = VTONMP(vp); | |
0a7de745 A |
777 | if (nfs_mount_gone(nmp)) { |
778 | return ENXIO; | |
779 | } | |
780 | if (np->n_flag & NREVOKE) { | |
781 | return EIO; | |
782 | } | |
1c79356b | 783 | |
91447636 | 784 | vtype = vnode_vtype(vp); |
0a7de745 A |
785 | if ((vtype != VREG) && (vtype != VDIR) && (vtype != VLNK)) { |
786 | return EACCES; | |
787 | } | |
6d2010ae A |
788 | |
789 | /* First, check if we need to update/invalidate */ | |
0a7de745 | 790 | if (ISSET(np->n_flag, NUPDATESIZE)) { |
2d21ac55 | 791 | nfs_data_update_size(np, 0); |
0a7de745 A |
792 | } |
793 | if ((error = nfs_node_lock(np))) { | |
794 | return error; | |
795 | } | |
91447636 A |
796 | if (np->n_flag & NNEEDINVALIDATE) { |
797 | np->n_flag &= ~NNEEDINVALIDATE; | |
0a7de745 | 798 | if (vtype == VDIR) { |
b0d623f7 | 799 | nfs_invaldir(np); |
0a7de745 | 800 | } |
b0d623f7 | 801 | nfs_node_unlock(np); |
0a7de745 A |
802 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
803 | if ((error = nfs_node_lock(np))) { | |
804 | return error; | |
805 | } | |
91447636 | 806 | } |
0a7de745 | 807 | if (vtype == VREG) { |
b0d623f7 | 808 | np->n_lastrahead = -1; |
0a7de745 | 809 | } |
91447636 | 810 | if (np->n_flag & NMODIFIED) { |
0a7de745 | 811 | if (vtype == VDIR) { |
b0d623f7 | 812 | nfs_invaldir(np); |
0a7de745 | 813 | } |
b0d623f7 | 814 | nfs_node_unlock(np); |
0a7de745 A |
815 | if ((error = nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1))) { |
816 | return error; | |
817 | } | |
1c79356b | 818 | } else { |
b0d623f7 | 819 | nfs_node_unlock(np); |
1c79356b | 820 | } |
6d2010ae | 821 | |
b0d623f7 | 822 | /* nfs_getattr() will check changed and purge caches */ |
0a7de745 A |
823 | if ((error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED))) { |
824 | return error; | |
825 | } | |
6d2010ae A |
826 | |
827 | if (vtype != VREG) { | |
828 | /* Just mark that it was opened */ | |
829 | lck_mtx_lock(&np->n_openlock); | |
830 | np->n_openrefcnt++; | |
831 | lck_mtx_unlock(&np->n_openlock); | |
0a7de745 | 832 | return 0; |
6d2010ae A |
833 | } |
834 | ||
835 | /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */ | |
836 | accessMode = 0; | |
0a7de745 | 837 | if (ap->a_mode & FREAD) { |
6d2010ae | 838 | accessMode |= NFS_OPEN_SHARE_ACCESS_READ; |
0a7de745 A |
839 | } |
840 | if (ap->a_mode & FWRITE) { | |
6d2010ae | 841 | accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE; |
0a7de745 A |
842 | } |
843 | if (ap->a_mode & O_EXLOCK) { | |
6d2010ae | 844 | denyMode = NFS_OPEN_SHARE_DENY_BOTH; |
0a7de745 | 845 | } else if (ap->a_mode & O_SHLOCK) { |
6d2010ae | 846 | denyMode = NFS_OPEN_SHARE_DENY_WRITE; |
0a7de745 | 847 | } else { |
6d2010ae | 848 | denyMode = NFS_OPEN_SHARE_DENY_NONE; |
0a7de745 | 849 | } |
6d2010ae A |
850 | // XXX don't do deny modes just yet (and never do it for !v4) |
851 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
852 | ||
853 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1); | |
0a7de745 A |
854 | if (!noop) { |
855 | return ENOMEM; | |
856 | } | |
6d2010ae A |
857 | |
858 | restart: | |
859 | error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx)); | |
860 | if (error) { | |
861 | nfs_open_owner_rele(noop); | |
0a7de745 | 862 | return error; |
6d2010ae A |
863 | } |
864 | if (np->n_flag & NREVOKE) { | |
865 | error = EIO; | |
866 | nfs_mount_state_in_use_end(nmp, 0); | |
867 | nfs_open_owner_rele(noop); | |
0a7de745 | 868 | return error; |
6d2010ae A |
869 | } |
870 | ||
871 | error = nfs_open_file_find(np, noop, &nofp, accessMode, denyMode, 1); | |
872 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) { | |
873 | NP(np, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
874 | error = EIO; | |
875 | } | |
cb323159 | 876 | #if CONFIG_NFS4 |
6d2010ae A |
877 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { |
878 | nfs_mount_state_in_use_end(nmp, 0); | |
879 | error = nfs4_reopen(nofp, vfs_context_thread(ctx)); | |
880 | nofp = NULL; | |
0a7de745 | 881 | if (!error) { |
6d2010ae | 882 | goto restart; |
0a7de745 | 883 | } |
6d2010ae | 884 | } |
cb323159 | 885 | #endif |
0a7de745 | 886 | if (!error) { |
6d2010ae | 887 | error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx)); |
0a7de745 | 888 | } |
6d2010ae A |
889 | if (error) { |
890 | nofp = NULL; | |
891 | goto out; | |
892 | } | |
893 | ||
894 | if (nmp->nm_vers < NFS_VER4) { | |
895 | /* | |
896 | * NFS v2/v3 opens are always allowed - so just add it. | |
897 | */ | |
898 | nfs_open_file_add_open(nofp, accessMode, denyMode, 0); | |
899 | goto out; | |
900 | } | |
901 | ||
902 | /* | |
903 | * If we just created the file and the modes match, then we simply use | |
904 | * the open performed in the create. Otherwise, send the request. | |
905 | */ | |
906 | if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
907 | (nofp->nof_creator == current_thread()) && | |
908 | (accessMode == NFS_OPEN_SHARE_ACCESS_BOTH) && | |
909 | (denyMode == NFS_OPEN_SHARE_DENY_NONE)) { | |
910 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
911 | nofp->nof_creator = NULL; | |
912 | } else { | |
cb323159 | 913 | #if CONFIG_NFS4 |
0a7de745 | 914 | if (!opened) { |
6d2010ae | 915 | error = nfs4_open(np, nofp, accessMode, denyMode, ctx); |
0a7de745 | 916 | } |
cb323159 | 917 | #endif |
6d2010ae A |
918 | if ((error == EACCES) && (nofp->nof_flags & NFS_OPEN_FILE_CREATE) && |
919 | (nofp->nof_creator == current_thread())) { | |
920 | /* | |
921 | * Ugh. This can happen if we just created the file with read-only | |
922 | * perms and we're trying to open it for real with different modes | |
923 | * (e.g. write-only or with a deny mode) and the server decides to | |
924 | * not allow the second open because of the read-only perms. | |
925 | * The best we can do is to just use the create's open. | |
926 | * We may have access we don't need or we may not have a requested | |
927 | * deny mode. We may log complaints later, but we'll try to avoid it. | |
928 | */ | |
0a7de745 | 929 | if (denyMode != NFS_OPEN_SHARE_DENY_NONE) { |
6d2010ae | 930 | NP(np, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp->nof_owner->noo_cred)); |
0a7de745 | 931 | } |
6d2010ae A |
932 | nofp->nof_creator = NULL; |
933 | error = 0; | |
934 | } | |
0a7de745 | 935 | if (error) { |
6d2010ae | 936 | goto out; |
0a7de745 | 937 | } |
6d2010ae A |
938 | opened = 1; |
939 | /* | |
940 | * If we had just created the file, we already had it open. | |
941 | * If the actual open mode is less than what we grabbed at | |
942 | * create time, then we'll downgrade the open here. | |
943 | */ | |
944 | if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
945 | (nofp->nof_creator == current_thread())) { | |
946 | error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_BOTH, NFS_OPEN_SHARE_DENY_NONE, ctx); | |
0a7de745 | 947 | if (error) { |
6d2010ae | 948 | NP(np, "nfs_vnop_open: create close error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred)); |
0a7de745 | 949 | } |
6d2010ae A |
950 | if (!nfs_mount_state_error_should_restart(error)) { |
951 | error = 0; | |
952 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
953 | } | |
954 | } | |
955 | } | |
956 | ||
957 | out: | |
0a7de745 | 958 | if (nofp) { |
6d2010ae | 959 | nfs_open_file_clear_busy(nofp); |
0a7de745 | 960 | } |
6d2010ae A |
961 | if (nfs_mount_state_in_use_end(nmp, error)) { |
962 | nofp = NULL; | |
963 | goto restart; | |
964 | } | |
0a7de745 | 965 | if (error) { |
6d2010ae | 966 | NP(np, "nfs_vnop_open: error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); |
0a7de745 A |
967 | } |
968 | if (noop) { | |
6d2010ae | 969 | nfs_open_owner_rele(noop); |
0a7de745 | 970 | } |
316670eb A |
971 | if (!error && vtype == VREG && (ap->a_mode & FWRITE)) { |
972 | lck_mtx_lock(&nmp->nm_lock); | |
973 | nmp->nm_state &= ~NFSSTA_SQUISHY; | |
974 | nmp->nm_curdeadtimeout = nmp->nm_deadtimeout; | |
0a7de745 | 975 | if (nmp->nm_curdeadtimeout <= 0) { |
316670eb | 976 | nmp->nm_deadto_start = 0; |
0a7de745 | 977 | } |
316670eb A |
978 | nmp->nm_writers++; |
979 | lck_mtx_unlock(&nmp->nm_lock); | |
980 | } | |
0a7de745 A |
981 | |
982 | return error; | |
1c79356b A |
983 | } |
984 | ||
316670eb A |
985 | static uint32_t |
986 | nfs_no_of_open_file_writers(nfsnode_t np) | |
987 | { | |
988 | uint32_t writers = 0; | |
989 | struct nfs_open_file *nofp; | |
990 | ||
0a7de745 | 991 | TAILQ_FOREACH(nofp, &np->n_opens, nof_link) { |
316670eb | 992 | writers += nofp->nof_w + nofp->nof_rw + nofp->nof_w_dw + nofp->nof_rw_dw + |
0a7de745 A |
993 | nofp->nof_w_drw + nofp->nof_rw_drw + nofp->nof_d_w_dw + |
994 | nofp->nof_d_rw_dw + nofp->nof_d_w_drw + nofp->nof_d_rw_drw + | |
995 | nofp->nof_d_w + nofp->nof_d_rw; | |
316670eb | 996 | } |
0a7de745 A |
997 | |
998 | return writers; | |
316670eb | 999 | } |
6d2010ae | 1000 | |
1c79356b | 1001 | /* |
2d21ac55 | 1002 | * NFS close vnode op |
6d2010ae | 1003 | * |
1c79356b A |
1004 | * What an NFS client should do upon close after writing is a debatable issue. |
1005 | * Most NFS clients push delayed writes to the server upon close, basically for | |
1006 | * two reasons: | |
1007 | * 1 - So that any write errors may be reported back to the client process | |
1008 | * doing the close system call. By far the two most likely errors are | |
1009 | * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. | |
1010 | * 2 - To put a worst case upper bound on cache inconsistency between | |
1011 | * multiple clients for the file. | |
1012 | * There is also a consistency problem for Version 2 of the protocol w.r.t. | |
1013 | * not being able to tell if other clients are writing a file concurrently, | |
1014 | * since there is no way of knowing if the changed modify time in the reply | |
1015 | * is only due to the write for this client. | |
1016 | * (NFS Version 3 provides weak cache consistency data in the reply that | |
1017 | * should be sufficient to detect and handle this case.) | |
1018 | * | |
1019 | * The current code does the following: | |
1020 | * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers | |
6d2010ae A |
1021 | * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them. |
1022 | * for NFS Version 4 - basically the same as NFSv3 | |
1c79356b | 1023 | */ |
2d21ac55 | 1024 | int |
6d2010ae | 1025 | nfs_vnop_close( |
91447636 | 1026 | struct vnop_close_args /* { |
0a7de745 A |
1027 | * struct vnodeop_desc *a_desc; |
1028 | * vnode_t a_vp; | |
1029 | * int a_fflag; | |
1030 | * vfs_context_t a_context; | |
1031 | * } */*ap) | |
1c79356b | 1032 | { |
2d21ac55 | 1033 | vfs_context_t ctx = ap->a_context; |
91447636 | 1034 | vnode_t vp = ap->a_vp; |
2d21ac55 | 1035 | nfsnode_t np = VTONFS(vp); |
55e303ae | 1036 | struct nfsmount *nmp; |
6d2010ae A |
1037 | int error = 0, error1, nfsvers; |
1038 | int fflag = ap->a_fflag; | |
1039 | enum vtype vtype; | |
1040 | int accessMode, denyMode; | |
1041 | struct nfs_open_owner *noop = NULL; | |
1042 | struct nfs_open_file *nofp = NULL; | |
1c79356b | 1043 | |
2d21ac55 | 1044 | nmp = VTONMP(vp); |
0a7de745 A |
1045 | if (!nmp) { |
1046 | return ENXIO; | |
1047 | } | |
2d21ac55 | 1048 | nfsvers = nmp->nm_vers; |
6d2010ae | 1049 | vtype = vnode_vtype(vp); |
91447636 | 1050 | |
6d2010ae | 1051 | /* First, check if we need to update/flush/invalidate */ |
0a7de745 | 1052 | if (ISSET(np->n_flag, NUPDATESIZE)) { |
2d21ac55 | 1053 | nfs_data_update_size(np, 0); |
0a7de745 | 1054 | } |
6d2010ae | 1055 | nfs_node_lock_force(np); |
2d21ac55 | 1056 | if (np->n_flag & NNEEDINVALIDATE) { |
91447636 | 1057 | np->n_flag &= ~NNEEDINVALIDATE; |
b0d623f7 | 1058 | nfs_node_unlock(np); |
0a7de745 | 1059 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
6d2010ae | 1060 | nfs_node_lock_force(np); |
2d21ac55 | 1061 | } |
6d2010ae A |
1062 | if ((vtype == VREG) && (np->n_flag & NMODIFIED) && (fflag & FWRITE)) { |
1063 | /* we're closing an open for write and the file is modified, so flush it */ | |
b0d623f7 | 1064 | nfs_node_unlock(np); |
0a7de745 | 1065 | if (nfsvers != NFS_VER2) { |
2d21ac55 | 1066 | error = nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), 0); |
0a7de745 | 1067 | } else { |
2d21ac55 | 1068 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
0a7de745 | 1069 | } |
b0d623f7 | 1070 | nfs_node_lock_force(np); |
91447636 | 1071 | NATTRINVALIDATE(np); |
2d21ac55 A |
1072 | } |
1073 | if (np->n_flag & NWRITEERR) { | |
1c79356b A |
1074 | np->n_flag &= ~NWRITEERR; |
1075 | error = np->n_error; | |
1c79356b | 1076 | } |
b0d623f7 | 1077 | nfs_node_unlock(np); |
6d2010ae A |
1078 | |
1079 | if (vtype != VREG) { | |
1080 | /* Just mark that it was closed */ | |
1081 | lck_mtx_lock(&np->n_openlock); | |
1082 | if (np->n_openrefcnt == 0) { | |
0a7de745 | 1083 | if (fflag & (FREAD | FWRITE)) { |
6d2010ae A |
1084 | NP(np, "nfs_vnop_close: open reference underrun"); |
1085 | error = EINVAL; | |
1086 | } | |
0a7de745 | 1087 | } else if (fflag & (FREAD | FWRITE)) { |
6d2010ae A |
1088 | np->n_openrefcnt--; |
1089 | } else { | |
1090 | /* No FREAD/FWRITE set - probably the final close */ | |
1091 | np->n_openrefcnt = 0; | |
1092 | } | |
1093 | lck_mtx_unlock(&np->n_openlock); | |
0a7de745 | 1094 | return error; |
6d2010ae A |
1095 | } |
1096 | error1 = error; | |
1097 | ||
1098 | /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */ | |
1099 | accessMode = 0; | |
0a7de745 | 1100 | if (fflag & FREAD) { |
6d2010ae | 1101 | accessMode |= NFS_OPEN_SHARE_ACCESS_READ; |
0a7de745 A |
1102 | } |
1103 | if (fflag & FWRITE) { | |
6d2010ae | 1104 | accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE; |
0a7de745 | 1105 | } |
6d2010ae A |
1106 | // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open |
1107 | // if (fflag & O_EXLOCK) | |
1108 | // denyMode = NFS_OPEN_SHARE_DENY_BOTH; | |
1109 | // else if (fflag & O_SHLOCK) | |
1110 | // denyMode = NFS_OPEN_SHARE_DENY_WRITE; | |
1111 | // else | |
1112 | // denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
ebb1b9f4 | 1113 | #if 0 // Not yet |
6d2010ae A |
1114 | if (fflag & FHASLOCK) { |
1115 | /* XXX assume FHASLOCK is for the deny mode and not flock */ | |
1116 | /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */ | |
0a7de745 | 1117 | if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_READ) { |
6d2010ae | 1118 | denyMode = NFS_OPEN_SHARE_DENY_BOTH; |
0a7de745 | 1119 | } else if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_WRITE) { |
6d2010ae | 1120 | denyMode = NFS_OPEN_SHARE_DENY_WRITE; |
0a7de745 | 1121 | } else { |
6d2010ae | 1122 | denyMode = NFS_OPEN_SHARE_DENY_NONE; |
0a7de745 | 1123 | } |
6d2010ae | 1124 | } else { |
0a7de745 | 1125 | denyMode = NFS_OPEN_SHARE_DENY_NONE; |
6d2010ae | 1126 | } |
ebb1b9f4 | 1127 | #else |
6d2010ae A |
1128 | // XXX don't do deny modes just yet (and never do it for !v4) |
1129 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
ebb1b9f4 | 1130 | #endif |
6d2010ae A |
1131 | |
1132 | if (!accessMode) { | |
1133 | /* | |
1134 | * No mode given to close? | |
1135 | * Guess this is the final close. | |
1136 | * We should unlock all locks and close all opens. | |
1137 | */ | |
316670eb | 1138 | uint32_t writers; |
6d2010ae | 1139 | mount_t mp = vnode_mount(vp); |
fe8ab488 | 1140 | int force = (!mp || vfs_isforce(mp)); |
316670eb A |
1141 | |
1142 | writers = nfs_no_of_open_file_writers(np); | |
6d2010ae | 1143 | nfs_release_open_state_for_node(np, force); |
316670eb A |
1144 | if (writers) { |
1145 | lck_mtx_lock(&nmp->nm_lock); | |
1146 | if (writers > nmp->nm_writers) { | |
1147 | NP(np, "nfs_vnop_close: number of write opens for mount underrun. Node has %d" | |
0a7de745 A |
1148 | " opens for write. Mount has total of %d opens for write\n", |
1149 | writers, nmp->nm_writers); | |
316670eb A |
1150 | nmp->nm_writers = 0; |
1151 | } else { | |
1152 | nmp->nm_writers -= writers; | |
1153 | } | |
1154 | lck_mtx_unlock(&nmp->nm_lock); | |
1155 | } | |
0a7de745 A |
1156 | |
1157 | return error; | |
316670eb A |
1158 | } else if (fflag & FWRITE) { |
1159 | lck_mtx_lock(&nmp->nm_lock); | |
1160 | if (nmp->nm_writers == 0) { | |
1161 | NP(np, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing"); | |
1162 | } else { | |
1163 | nmp->nm_writers--; | |
1164 | } | |
1165 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae | 1166 | } |
0a7de745 | 1167 | |
6d2010ae A |
1168 | |
1169 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 0); | |
1170 | if (!noop) { | |
1171 | // printf("nfs_vnop_close: can't get open owner!\n"); | |
0a7de745 | 1172 | return EIO; |
6d2010ae A |
1173 | } |
1174 | ||
1175 | restart: | |
1176 | error = nfs_mount_state_in_use_start(nmp, NULL); | |
1177 | if (error) { | |
1178 | nfs_open_owner_rele(noop); | |
0a7de745 | 1179 | return error; |
6d2010ae A |
1180 | } |
1181 | ||
1182 | error = nfs_open_file_find(np, noop, &nofp, 0, 0, 0); | |
cb323159 | 1183 | #if CONFIG_NFS4 |
6d2010ae A |
1184 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { |
1185 | nfs_mount_state_in_use_end(nmp, 0); | |
1186 | error = nfs4_reopen(nofp, NULL); | |
1187 | nofp = NULL; | |
0a7de745 | 1188 | if (!error) { |
6d2010ae | 1189 | goto restart; |
0a7de745 | 1190 | } |
6d2010ae | 1191 | } |
cb323159 | 1192 | #endif |
6d2010ae A |
1193 | if (error) { |
1194 | NP(np, "nfs_vnop_close: no open file for owner, error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); | |
1195 | error = EBADF; | |
1196 | goto out; | |
1197 | } | |
1198 | error = nfs_open_file_set_busy(nofp, NULL); | |
1199 | if (error) { | |
1200 | nofp = NULL; | |
1201 | goto out; | |
1202 | } | |
1203 | ||
1204 | error = nfs_close(np, nofp, accessMode, denyMode, ctx); | |
0a7de745 | 1205 | if (error) { |
6d2010ae | 1206 | NP(np, "nfs_vnop_close: close error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); |
0a7de745 | 1207 | } |
6d2010ae A |
1208 | |
1209 | out: | |
0a7de745 | 1210 | if (nofp) { |
6d2010ae | 1211 | nfs_open_file_clear_busy(nofp); |
0a7de745 | 1212 | } |
6d2010ae A |
1213 | if (nfs_mount_state_in_use_end(nmp, error)) { |
1214 | nofp = NULL; | |
1215 | goto restart; | |
1216 | } | |
0a7de745 | 1217 | if (!error) { |
6d2010ae | 1218 | error = error1; |
0a7de745 A |
1219 | } |
1220 | if (error) { | |
6d2010ae | 1221 | NP(np, "nfs_vnop_close: error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); |
0a7de745 A |
1222 | } |
1223 | if (noop) { | |
6d2010ae | 1224 | nfs_open_owner_rele(noop); |
0a7de745 A |
1225 | } |
1226 | return error; | |
1c79356b A |
1227 | } |
1228 | ||
6d2010ae A |
1229 | /* |
1230 | * nfs_close(): common function that does all the heavy lifting of file closure | |
1231 | * | |
1232 | * Takes an open file structure and a set of access/deny modes and figures out how | |
1233 | * to update the open file structure (and the state on the server) appropriately. | |
1234 | */ | |
1235 | int | |
1236 | nfs_close( | |
1237 | nfsnode_t np, | |
1238 | struct nfs_open_file *nofp, | |
1239 | uint32_t accessMode, | |
1240 | uint32_t denyMode, | |
1241 | vfs_context_t ctx) | |
1242 | { | |
cb323159 | 1243 | #if CONFIG_NFS4 |
6d2010ae | 1244 | struct nfs_lock_owner *nlop; |
cb323159 | 1245 | #endif |
6d2010ae A |
1246 | int error = 0, changed = 0, delegated = 0, closed = 0, downgrade = 0; |
1247 | uint32_t newAccessMode, newDenyMode; | |
0a7de745 | 1248 | |
6d2010ae | 1249 | /* warn if modes don't match current state */ |
0a7de745 | 1250 | if (((accessMode & nofp->nof_access) != accessMode) || ((denyMode & nofp->nof_deny) != denyMode)) { |
6d2010ae | 1251 | NP(np, "nfs_close: mode mismatch %d %d, current %d %d, %d", |
0a7de745 A |
1252 | accessMode, denyMode, nofp->nof_access, nofp->nof_deny, |
1253 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1254 | } | |
6d2010ae A |
1255 | |
1256 | /* | |
1257 | * If we're closing a write-only open, we may not have a write-only count | |
1258 | * if we also grabbed read access. So, check the read-write count. | |
1259 | */ | |
1260 | if (denyMode == NFS_OPEN_SHARE_DENY_NONE) { | |
1261 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1262 | (nofp->nof_w == 0) && (nofp->nof_d_w == 0) && | |
0a7de745 | 1263 | (nofp->nof_rw || nofp->nof_d_rw)) { |
6d2010ae | 1264 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; |
0a7de745 | 1265 | } |
6d2010ae A |
1266 | } else if (denyMode == NFS_OPEN_SHARE_DENY_WRITE) { |
1267 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1268 | (nofp->nof_w_dw == 0) && (nofp->nof_d_w_dw == 0) && | |
0a7de745 | 1269 | (nofp->nof_rw_dw || nofp->nof_d_rw_dw)) { |
6d2010ae | 1270 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; |
0a7de745 | 1271 | } |
6d2010ae A |
1272 | } else { /* NFS_OPEN_SHARE_DENY_BOTH */ |
1273 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1274 | (nofp->nof_w_drw == 0) && (nofp->nof_d_w_drw == 0) && | |
0a7de745 | 1275 | (nofp->nof_rw_drw || nofp->nof_d_rw_drw)) { |
6d2010ae | 1276 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; |
0a7de745 | 1277 | } |
6d2010ae A |
1278 | } |
1279 | ||
1280 | nfs_open_file_remove_open_find(nofp, accessMode, denyMode, &newAccessMode, &newDenyMode, &delegated); | |
0a7de745 | 1281 | if ((newAccessMode != nofp->nof_access) || (newDenyMode != nofp->nof_deny)) { |
6d2010ae | 1282 | changed = 1; |
0a7de745 | 1283 | } else { |
6d2010ae | 1284 | changed = 0; |
0a7de745 | 1285 | } |
6d2010ae | 1286 | |
cb323159 A |
1287 | if (NFSTONMP(np)->nm_vers < NFS_VER4) { |
1288 | /* NFS v2/v3 closes simply need to remove the open. */ | |
6d2010ae | 1289 | goto v3close; |
0a7de745 | 1290 | } |
cb323159 | 1291 | #if CONFIG_NFS4 |
6d2010ae A |
1292 | if ((newAccessMode == 0) || (nofp->nof_opencnt == 1)) { |
1293 | /* | |
1294 | * No more access after this close, so clean up and close it. | |
1295 | * Don't send a close RPC if we're closing a delegated open. | |
1296 | */ | |
1297 | nfs_wait_bufs(np); | |
1298 | closed = 1; | |
0a7de745 | 1299 | if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) { |
6d2010ae | 1300 | error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0); |
0a7de745 | 1301 | } |
6d2010ae A |
1302 | if (error == NFSERR_LOCKS_HELD) { |
1303 | /* | |
1304 | * Hmm... the server says we have locks we need to release first | |
1305 | * Find the lock owner and try to unlock everything. | |
1306 | */ | |
1307 | nlop = nfs_lock_owner_find(np, vfs_context_proc(ctx), 0); | |
1308 | if (nlop) { | |
1309 | nfs4_unlock_rpc(np, nlop, F_WRLCK, 0, UINT64_MAX, | |
0a7de745 | 1310 | 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
6d2010ae A |
1311 | nfs_lock_owner_rele(nlop); |
1312 | } | |
1313 | error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0); | |
1314 | } | |
1315 | } else if (changed) { | |
1316 | /* | |
1317 | * File is still open but with less access, so downgrade the open. | |
1318 | * Don't send a downgrade RPC if we're closing a delegated open. | |
1319 | */ | |
1320 | if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) { | |
1321 | downgrade = 1; | |
1322 | /* | |
1323 | * If we have delegated opens, we should probably claim them before sending | |
1324 | * the downgrade because the server may not know the open we are downgrading to. | |
1325 | */ | |
1326 | if (nofp->nof_d_rw_drw || nofp->nof_d_w_drw || nofp->nof_d_r_drw || | |
1327 | nofp->nof_d_rw_dw || nofp->nof_d_w_dw || nofp->nof_d_r_dw || | |
0a7de745 | 1328 | nofp->nof_d_rw || nofp->nof_d_w || nofp->nof_d_r) { |
6d2010ae | 1329 | nfs4_claim_delegated_state_for_open_file(nofp, 0); |
0a7de745 | 1330 | } |
6d2010ae A |
1331 | /* need to remove the open before sending the downgrade */ |
1332 | nfs_open_file_remove_open(nofp, accessMode, denyMode); | |
1333 | error = nfs4_open_downgrade_rpc(np, nofp, ctx); | |
0a7de745 | 1334 | if (error) { /* Hmm.. that didn't work. Add the open back in. */ |
6d2010ae | 1335 | nfs_open_file_add_open(nofp, accessMode, denyMode, delegated); |
0a7de745 | 1336 | } |
6d2010ae A |
1337 | } |
1338 | } | |
cb323159 A |
1339 | #endif |
1340 | v3close: | |
6d2010ae A |
1341 | if (error) { |
1342 | NP(np, "nfs_close: error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
0a7de745 | 1343 | return error; |
6d2010ae A |
1344 | } |
1345 | ||
0a7de745 | 1346 | if (!downgrade) { |
6d2010ae | 1347 | nfs_open_file_remove_open(nofp, accessMode, denyMode); |
0a7de745 | 1348 | } |
6d2010ae A |
1349 | |
1350 | if (closed) { | |
1351 | lck_mtx_lock(&nofp->nof_lock); | |
1352 | if (nofp->nof_r || nofp->nof_d_r || nofp->nof_w || nofp->nof_d_w || nofp->nof_d_rw || | |
1353 | (nofp->nof_rw && !((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && !nofp->nof_creator && (nofp->nof_rw == 1))) || | |
1354 | nofp->nof_r_dw || nofp->nof_d_r_dw || nofp->nof_w_dw || nofp->nof_d_w_dw || | |
1355 | nofp->nof_rw_dw || nofp->nof_d_rw_dw || nofp->nof_r_drw || nofp->nof_d_r_drw || | |
0a7de745 | 1356 | nofp->nof_w_drw || nofp->nof_d_w_drw || nofp->nof_rw_drw || nofp->nof_d_rw_drw) { |
6d2010ae | 1357 | 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", |
0a7de745 A |
1358 | nofp->nof_r, nofp->nof_d_r, nofp->nof_w, nofp->nof_d_w, |
1359 | nofp->nof_rw, nofp->nof_d_rw, nofp->nof_r_dw, nofp->nof_d_r_dw, | |
1360 | nofp->nof_w_dw, nofp->nof_d_w_dw, nofp->nof_rw_dw, nofp->nof_d_rw_dw, | |
1361 | nofp->nof_r_drw, nofp->nof_d_r_drw, nofp->nof_w_drw, nofp->nof_d_w_drw, | |
1362 | nofp->nof_rw_drw, nofp->nof_d_rw_drw, nofp->nof_flags, | |
1363 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1364 | } | |
6d2010ae A |
1365 | /* clear out all open info, just to be safe */ |
1366 | nofp->nof_access = nofp->nof_deny = 0; | |
1367 | nofp->nof_mmap_access = nofp->nof_mmap_deny = 0; | |
1368 | nofp->nof_r = nofp->nof_d_r = 0; | |
1369 | nofp->nof_w = nofp->nof_d_w = 0; | |
1370 | nofp->nof_rw = nofp->nof_d_rw = 0; | |
1371 | nofp->nof_r_dw = nofp->nof_d_r_dw = 0; | |
1372 | nofp->nof_w_dw = nofp->nof_d_w_dw = 0; | |
1373 | nofp->nof_rw_dw = nofp->nof_d_rw_dw = 0; | |
1374 | nofp->nof_r_drw = nofp->nof_d_r_drw = 0; | |
1375 | nofp->nof_w_drw = nofp->nof_d_w_drw = 0; | |
1376 | nofp->nof_rw_drw = nofp->nof_d_rw_drw = 0; | |
1377 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
1378 | lck_mtx_unlock(&nofp->nof_lock); | |
1379 | /* XXX we may potentially want to clean up idle/unused open file structures */ | |
1380 | } | |
1381 | if (nofp->nof_flags & NFS_OPEN_FILE_LOST) { | |
1382 | error = EIO; | |
1383 | NP(np, "nfs_close: LOST%s, %d", !nofp->nof_opencnt ? " (last)" : "", | |
0a7de745 | 1384 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); |
6d2010ae | 1385 | } |
0a7de745 A |
1386 | |
1387 | return error; | |
6d2010ae A |
1388 | } |
1389 | ||
1390 | ||
91447636 | 1391 | int |
2d21ac55 A |
1392 | nfs3_getattr_rpc( |
1393 | nfsnode_t np, | |
91447636 A |
1394 | mount_t mp, |
1395 | u_char *fhp, | |
2d21ac55 | 1396 | size_t fhsize, |
6d2010ae | 1397 | int flags, |
2d21ac55 | 1398 | vfs_context_t ctx, |
91447636 A |
1399 | struct nfs_vattr *nvap, |
1400 | u_int64_t *xidp) | |
1401 | { | |
2d21ac55 | 1402 | struct nfsmount *nmp = mp ? VFSTONFS(mp) : NFSTONMP(np); |
6d2010ae | 1403 | int error = 0, status, nfsvers, rpcflags = 0; |
2d21ac55 | 1404 | struct nfsm_chain nmreq, nmrep; |
91447636 | 1405 | |
0a7de745 A |
1406 | if (nfs_mount_gone(nmp)) { |
1407 | return ENXIO; | |
1408 | } | |
2d21ac55 A |
1409 | nfsvers = nmp->nm_vers; |
1410 | ||
0a7de745 | 1411 | if (flags & NGA_MONITOR) { /* vnode monitor requests should be soft */ |
6d2010ae | 1412 | rpcflags = R_RECOVER; |
0a7de745 | 1413 | } |
6d2010ae | 1414 | |
0a7de745 | 1415 | if (flags & NGA_SOFT) { /* Return ETIMEDOUT if server not responding */ |
fe8ab488 | 1416 | rpcflags |= R_SOFT; |
0a7de745 | 1417 | } |
fe8ab488 | 1418 | |
2d21ac55 A |
1419 | nfsm_chain_null(&nmreq); |
1420 | nfsm_chain_null(&nmrep); | |
1421 | ||
1422 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers)); | |
0a7de745 | 1423 | if (nfsvers != NFS_VER2) { |
2d21ac55 | 1424 | nfsm_chain_add_32(error, &nmreq, fhsize); |
0a7de745 | 1425 | } |
2d21ac55 A |
1426 | nfsm_chain_add_opaque(error, &nmreq, fhp, fhsize); |
1427 | nfsm_chain_build_done(error, &nmreq); | |
1428 | nfsmout_if(error); | |
0a7de745 A |
1429 | error = nfs_request2(np, mp, &nmreq, NFSPROC_GETATTR, |
1430 | vfs_context_thread(ctx), vfs_context_ucred(ctx), | |
1431 | NULL, rpcflags, &nmrep, xidp, &status); | |
1432 | if (!error) { | |
2d21ac55 | 1433 | error = status; |
0a7de745 | 1434 | } |
2d21ac55 | 1435 | nfsmout_if(error); |
cb323159 | 1436 | error = nfs_parsefattr(nmp, &nmrep, nfsvers, nvap); |
2d21ac55 A |
1437 | nfsmout: |
1438 | nfsm_chain_cleanup(&nmreq); | |
1439 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 1440 | return error; |
91447636 A |
1441 | } |
1442 | ||
fe8ab488 A |
1443 | /* |
1444 | * nfs_refresh_fh will attempt to update the file handle for the node. | |
1445 | * | |
1446 | * It only does this for symbolic links and regular files that are not currently opened. | |
1447 | * | |
1448 | * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure. | |
0a7de745 | 1449 | */ |
fe8ab488 A |
1450 | int |
1451 | nfs_refresh_fh(nfsnode_t np, vfs_context_t ctx) | |
1452 | { | |
1453 | vnode_t dvp, vp = NFSTOV(np); | |
1454 | nfsnode_t dnp; | |
1455 | const char *v_name = vnode_getname(vp); | |
1456 | char *name; | |
1457 | int namelen, fhsize, refreshed; | |
1458 | int error, wanted = 0; | |
1459 | uint8_t *fhp; | |
cb323159 | 1460 | struct timespec ts = {.tv_sec = 2, .tv_nsec = 0}; |
fe8ab488 A |
1461 | |
1462 | NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp)); | |
1463 | ||
1464 | dvp = vnode_parent(vp); | |
1465 | if ((vnode_vtype(vp) != VREG && vnode_vtype(vp) != VLNK) || | |
1466 | v_name == NULL || *v_name == '\0' || dvp == NULL) { | |
0a7de745 | 1467 | if (v_name != NULL) { |
fe8ab488 | 1468 | vnode_putname(v_name); |
0a7de745 A |
1469 | } |
1470 | return ESTALE; | |
fe8ab488 A |
1471 | } |
1472 | dnp = VTONFS(dvp); | |
0a7de745 | 1473 | |
fe8ab488 A |
1474 | namelen = strlen(v_name); |
1475 | MALLOC(name, char *, namelen + 1, M_TEMP, M_WAITOK); | |
1476 | if (name == NULL) { | |
1477 | vnode_putname(v_name); | |
0a7de745 | 1478 | return ESTALE; |
fe8ab488 | 1479 | } |
0a7de745 | 1480 | bcopy(v_name, name, namelen + 1); |
fe8ab488 A |
1481 | NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name, name); |
1482 | vnode_putname(v_name); | |
1483 | ||
1484 | /* Allocate the maximum size file handle */ | |
1485 | MALLOC(fhp, uint8_t *, NFS4_FHSIZE, M_TEMP, M_WAITOK); | |
1486 | if (fhp == NULL) { | |
1487 | FREE(name, M_TEMP); | |
0a7de745 | 1488 | return ESTALE; |
fe8ab488 | 1489 | } |
0a7de745 | 1490 | |
fe8ab488 A |
1491 | if ((error = nfs_node_lock(np))) { |
1492 | FREE(name, M_TEMP); | |
1493 | FREE(fhp, M_TEMP); | |
0a7de745 | 1494 | return ESTALE; |
fe8ab488 | 1495 | } |
0a7de745 | 1496 | |
fe8ab488 A |
1497 | fhsize = np->n_fhsize; |
1498 | bcopy(np->n_fhp, fhp, fhsize); | |
1499 | while (ISSET(np->n_flag, NREFRESH)) { | |
1500 | SET(np->n_flag, NREFRESHWANT); | |
1501 | NFS_VNOP_DBG("Waiting for refresh of %s\n", name); | |
0a7de745 A |
1502 | msleep(np, &np->n_lock, PZERO - 1, "nfsrefreshwant", &ts); |
1503 | if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) { | |
fe8ab488 | 1504 | break; |
0a7de745 | 1505 | } |
fe8ab488 A |
1506 | } |
1507 | refreshed = error ? 0 : !NFS_CMPFH(np, fhp, fhsize); | |
1508 | SET(np->n_flag, NREFRESH); | |
1509 | nfs_node_unlock(np); | |
1510 | ||
1511 | NFS_VNOP_DBG("error = %d, refreshed = %d\n", error, refreshed); | |
0a7de745 | 1512 | if (error || refreshed) { |
fe8ab488 | 1513 | goto nfsmout; |
0a7de745 A |
1514 | } |
1515 | ||
fe8ab488 A |
1516 | /* Check that there are no open references for this file */ |
1517 | lck_mtx_lock(&np->n_openlock); | |
1518 | if (np->n_openrefcnt || !TAILQ_EMPTY(&np->n_opens) || !TAILQ_EMPTY(&np->n_lock_owners)) { | |
1519 | int cnt = 0; | |
1520 | struct nfs_open_file *ofp; | |
0a7de745 | 1521 | |
fe8ab488 A |
1522 | TAILQ_FOREACH(ofp, &np->n_opens, nof_link) { |
1523 | cnt += ofp->nof_opencnt; | |
1524 | } | |
1525 | if (cnt) { | |
1526 | lck_mtx_unlock(&np->n_openlock); | |
1527 | NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name); | |
0a7de745 A |
1528 | NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n", |
1529 | np->n_openrefcnt, cnt, !TAILQ_EMPTY(&np->n_lock_owners)); | |
fe8ab488 A |
1530 | error = ESTALE; |
1531 | goto nfsmout; | |
1532 | } | |
1533 | } | |
1534 | lck_mtx_unlock(&np->n_openlock); | |
0a7de745 | 1535 | /* |
fe8ab488 A |
1536 | * Since the FH is currently stale we should not be able to |
1537 | * establish any open state until the FH is refreshed. | |
1538 | */ | |
1539 | ||
1540 | error = nfs_node_lock(np); | |
1541 | nfsmout_if(error); | |
1542 | /* | |
1543 | * Symlinks should never need invalidations and are holding | |
1544 | * the one and only nfsbuf in an uncached acquired state | |
1545 | * trying to do a readlink. So we will hang if we invalidate | |
1546 | * in that case. Only in in the VREG case do we need to | |
1547 | * invalidate. | |
1548 | */ | |
1549 | if (vnode_vtype(vp) == VREG) { | |
1550 | np->n_flag &= ~NNEEDINVALIDATE; | |
1551 | nfs_node_unlock(np); | |
1552 | error = nfs_vinvalbuf(vp, V_IGNORE_WRITEERR, ctx, 1); | |
0a7de745 | 1553 | if (error) { |
fe8ab488 | 1554 | NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error); |
0a7de745 | 1555 | } |
fe8ab488 A |
1556 | nfsmout_if(error); |
1557 | } else { | |
1558 | nfs_node_unlock(np); | |
1559 | } | |
1560 | ||
1561 | NFS_VNOP_DBG("Looking up %s\n", name); | |
1562 | error = nfs_lookitup(dnp, name, namelen, ctx, &np); | |
0a7de745 | 1563 | if (error) { |
fe8ab488 | 1564 | NFS_VNOP_DBG("nfs_lookitup returned %d\n", error); |
0a7de745 | 1565 | } |
fe8ab488 A |
1566 | |
1567 | nfsmout: | |
1568 | nfs_node_lock_force(np); | |
1569 | wanted = ISSET(np->n_flag, NREFRESHWANT); | |
0a7de745 | 1570 | CLR(np->n_flag, NREFRESH | NREFRESHWANT); |
fe8ab488 | 1571 | nfs_node_unlock(np); |
0a7de745 | 1572 | if (wanted) { |
fe8ab488 | 1573 | wakeup(np); |
0a7de745 A |
1574 | } |
1575 | ||
1576 | if (error == 0) { | |
fe8ab488 | 1577 | NFS_VNOP_DBG("%s refreshed file handle\n", name); |
0a7de745 | 1578 | } |
fe8ab488 A |
1579 | |
1580 | FREE(name, M_TEMP); | |
1581 | FREE(fhp, M_TEMP); | |
0a7de745 A |
1582 | |
1583 | return error ? ESTALE : 0; | |
fe8ab488 | 1584 | } |
2d21ac55 | 1585 | |
91447636 | 1586 | int |
6d2010ae | 1587 | nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags) |
fe8ab488 A |
1588 | { |
1589 | int error; | |
0a7de745 | 1590 | |
fe8ab488 A |
1591 | retry: |
1592 | error = nfs_getattr_internal(np, nvap, ctx, flags); | |
1593 | if (error == ESTALE) { | |
1594 | error = nfs_refresh_fh(np, ctx); | |
0a7de745 | 1595 | if (!error) { |
fe8ab488 | 1596 | goto retry; |
0a7de745 | 1597 | } |
fe8ab488 | 1598 | } |
0a7de745 | 1599 | return error; |
fe8ab488 A |
1600 | } |
1601 | ||
1602 | int | |
1603 | nfs_getattr_internal(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags) | |
1c79356b | 1604 | { |
2d21ac55 | 1605 | struct nfsmount *nmp; |
b0d623f7 | 1606 | int error = 0, nfsvers, inprogset = 0, wanted = 0, avoidfloods; |
6d2010ae | 1607 | struct nfs_vattr nvattr; |
cb323159 | 1608 | struct timespec ts = { .tv_sec = 2, .tv_nsec = 0 }; |
fa4905b1 | 1609 | u_int64_t xid; |
91447636 A |
1610 | |
1611 | FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag); | |
1612 | ||
fe8ab488 | 1613 | nmp = NFSTONMP(np); |
0a7de745 A |
1614 | |
1615 | if (nfs_mount_gone(nmp)) { | |
1616 | return ENXIO; | |
1617 | } | |
b0d623f7 A |
1618 | nfsvers = nmp->nm_vers; |
1619 | ||
0a7de745 | 1620 | if (!nvap) { |
6d2010ae | 1621 | nvap = &nvattr; |
0a7de745 | 1622 | } |
6d2010ae A |
1623 | NVATTR_INIT(nvap); |
1624 | ||
2d21ac55 A |
1625 | /* Update local times for special files. */ |
1626 | if (np->n_flag & (NACC | NUPD)) { | |
b0d623f7 | 1627 | nfs_node_lock_force(np); |
1c79356b | 1628 | np->n_flag |= NCHG; |
b0d623f7 | 1629 | nfs_node_unlock(np); |
2d21ac55 A |
1630 | } |
1631 | /* Update size, if necessary */ | |
0a7de745 | 1632 | if (ISSET(np->n_flag, NUPDATESIZE)) { |
2d21ac55 | 1633 | nfs_data_update_size(np, 0); |
0a7de745 | 1634 | } |
2d21ac55 | 1635 | |
b0d623f7 A |
1636 | error = nfs_node_lock(np); |
1637 | nfsmout_if(error); | |
0a7de745 | 1638 | if (!(flags & (NGA_UNCACHED | NGA_MONITOR)) || ((nfsvers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))) { |
6d2010ae A |
1639 | /* |
1640 | * Use the cache or wait for any getattr in progress if: | |
1641 | * - it's a cached request, or | |
fe8ab488 A |
1642 | * - we have a delegation, or |
1643 | * - the server isn't responding | |
6d2010ae | 1644 | */ |
b0d623f7 | 1645 | while (1) { |
6d2010ae | 1646 | error = nfs_getattrcache(np, nvap, flags); |
b0d623f7 A |
1647 | if (!error || (error != ENOENT)) { |
1648 | nfs_node_unlock(np); | |
1649 | goto nfsmout; | |
1650 | } | |
6d2010ae | 1651 | error = 0; |
0a7de745 | 1652 | if (!ISSET(np->n_flag, NGETATTRINPROG)) { |
b0d623f7 | 1653 | break; |
0a7de745 | 1654 | } |
6d2010ae A |
1655 | if (flags & NGA_MONITOR) { |
1656 | /* no need to wait if a request is pending */ | |
1657 | error = EINPROGRESS; | |
1658 | nfs_node_unlock(np); | |
1659 | goto nfsmout; | |
1660 | } | |
b0d623f7 | 1661 | SET(np->n_flag, NGETATTRWANT); |
0a7de745 | 1662 | msleep(np, &np->n_lock, PZERO - 1, "nfsgetattrwant", &ts); |
b0d623f7 A |
1663 | if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) { |
1664 | nfs_node_unlock(np); | |
1665 | goto nfsmout; | |
1666 | } | |
1667 | } | |
1668 | SET(np->n_flag, NGETATTRINPROG); | |
1669 | inprogset = 1; | |
1670 | } else if (!ISSET(np->n_flag, NGETATTRINPROG)) { | |
1671 | SET(np->n_flag, NGETATTRINPROG); | |
1672 | inprogset = 1; | |
6d2010ae A |
1673 | } else if (flags & NGA_MONITOR) { |
1674 | /* no need to make a request if one is pending */ | |
1675 | error = EINPROGRESS; | |
b0d623f7 A |
1676 | } |
1677 | nfs_node_unlock(np); | |
55e303ae | 1678 | |
2d21ac55 | 1679 | nmp = NFSTONMP(np); |
0a7de745 | 1680 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 1681 | error = ENXIO; |
0a7de745 A |
1682 | } |
1683 | if (error) { | |
2d21ac55 | 1684 | goto nfsmout; |
0a7de745 | 1685 | } |
fa4905b1 | 1686 | |
fe8ab488 A |
1687 | /* |
1688 | * Return cached attributes if they are valid, | |
1689 | * if the server doesn't respond, and this is | |
1690 | * some softened up style of mount. | |
1691 | */ | |
0a7de745 | 1692 | if (NATTRVALID(np) && nfs_use_cache(nmp)) { |
fe8ab488 | 1693 | flags |= NGA_SOFT; |
0a7de745 | 1694 | } |
fe8ab488 | 1695 | |
91447636 | 1696 | /* |
6d2010ae A |
1697 | * We might want to try to get both the attributes and access info by |
1698 | * making an ACCESS call and seeing if it returns updated attributes. | |
91447636 A |
1699 | * But don't bother if we aren't caching access info or if the |
1700 | * attributes returned wouldn't be cached. | |
1701 | */ | |
6d2010ae | 1702 | if (!(flags & NGA_ACL) && (nfsvers != NFS_VER2) && nfs_access_for_getattr && (nfs_access_cache_timeout > 0)) { |
2d21ac55 | 1703 | if (nfs_attrcachetimeout(np) > 0) { |
b0d623f7 | 1704 | /* OSAddAtomic(1, &nfsstats.accesscache_misses); */ |
6d2010ae | 1705 | u_int32_t access = NFS_ACCESS_ALL; |
fe8ab488 A |
1706 | int rpcflags = 0; |
1707 | ||
1708 | /* Return cached attrs if server doesn't respond */ | |
0a7de745 | 1709 | if (flags & NGA_SOFT) { |
fe8ab488 | 1710 | rpcflags |= R_SOFT; |
0a7de745 | 1711 | } |
fe8ab488 A |
1712 | |
1713 | error = nmp->nm_funcs->nf_access_rpc(np, &access, rpcflags, ctx); | |
1714 | ||
0a7de745 | 1715 | if (error == ETIMEDOUT) { |
fe8ab488 | 1716 | goto returncached; |
0a7de745 | 1717 | } |
fe8ab488 | 1718 | |
0a7de745 | 1719 | if (error) { |
2d21ac55 | 1720 | goto nfsmout; |
0a7de745 | 1721 | } |
b0d623f7 | 1722 | nfs_node_lock_force(np); |
6d2010ae | 1723 | error = nfs_getattrcache(np, nvap, flags); |
b0d623f7 | 1724 | nfs_node_unlock(np); |
0a7de745 | 1725 | if (!error || (error != ENOENT)) { |
2d21ac55 | 1726 | goto nfsmout; |
0a7de745 | 1727 | } |
b0d623f7 | 1728 | /* Well, that didn't work... just do a getattr... */ |
2d21ac55 A |
1729 | error = 0; |
1730 | } | |
1c79356b | 1731 | } |
b0d623f7 | 1732 | |
fa4905b1 | 1733 | avoidfloods = 0; |
fe8ab488 | 1734 | |
fa4905b1 | 1735 | tryagain: |
6d2010ae | 1736 | error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, flags, ctx, nvap, &xid); |
b0d623f7 A |
1737 | if (!error) { |
1738 | nfs_node_lock_force(np); | |
1739 | error = nfs_loadattrcache(np, nvap, &xid, 0); | |
1740 | nfs_node_unlock(np); | |
1741 | } | |
fe8ab488 A |
1742 | |
1743 | /* | |
1744 | * If the server didn't respond, return cached attributes. | |
1745 | */ | |
1746 | returncached: | |
1747 | if ((flags & NGA_SOFT) && (error == ETIMEDOUT)) { | |
1748 | nfs_node_lock_force(np); | |
1749 | error = nfs_getattrcache(np, nvap, flags); | |
1750 | if (!error || (error != ENOENT)) { | |
1751 | nfs_node_unlock(np); | |
1752 | goto nfsmout; | |
1753 | } | |
1754 | nfs_node_unlock(np); | |
1755 | } | |
2d21ac55 | 1756 | nfsmout_if(error); |
fe8ab488 | 1757 | |
2d21ac55 A |
1758 | if (!xid) { /* out-of-order rpc - attributes were dropped */ |
1759 | FSDBG(513, -1, np, np->n_xid >> 32, np->n_xid); | |
0a7de745 | 1760 | if (avoidfloods++ < 20) { |
2d21ac55 | 1761 | goto tryagain; |
0a7de745 | 1762 | } |
b0d623f7 A |
1763 | /* avoidfloods>1 is bizarre. at 20 pull the plug */ |
1764 | /* just return the last attributes we got */ | |
1765 | } | |
1766 | nfsmout: | |
1767 | nfs_node_lock_force(np); | |
1768 | if (inprogset) { | |
1769 | wanted = ISSET(np->n_flag, NGETATTRWANT); | |
1770 | CLR(np->n_flag, (NGETATTRINPROG | NGETATTRWANT)); | |
91447636 | 1771 | } |
b0d623f7 A |
1772 | if (!error) { |
1773 | /* check if the node changed on us */ | |
2d21ac55 A |
1774 | vnode_t vp = NFSTOV(np); |
1775 | enum vtype vtype = vnode_vtype(vp); | |
b0d623f7 A |
1776 | if ((vtype == VDIR) && NFS_CHANGED_NC(nfsvers, np, nvap)) { |
1777 | FSDBG(513, -1, np, 0, np); | |
1778 | np->n_flag &= ~NNEGNCENTRIES; | |
1779 | cache_purge(vp); | |
1780 | np->n_ncgen++; | |
1781 | NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap); | |
0a7de745 A |
1782 | NFS_VNOP_DBG("Purge directory 0x%llx\n", |
1783 | (uint64_t)VM_KERNEL_ADDRPERM(vp)); | |
2d21ac55 | 1784 | } |
b0d623f7 A |
1785 | if (NFS_CHANGED(nfsvers, np, nvap)) { |
1786 | FSDBG(513, -1, np, -1, np); | |
39236c6e | 1787 | if (vtype == VDIR) { |
0a7de745 A |
1788 | NFS_VNOP_DBG("Invalidate directory 0x%llx\n", |
1789 | (uint64_t)VM_KERNEL_ADDRPERM(vp)); | |
b0d623f7 | 1790 | nfs_invaldir(np); |
39236c6e | 1791 | } |
b0d623f7 | 1792 | nfs_node_unlock(np); |
0a7de745 | 1793 | if (wanted) { |
b0d623f7 | 1794 | wakeup(np); |
0a7de745 | 1795 | } |
2d21ac55 | 1796 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
fa4905b1 | 1797 | FSDBG(513, -1, np, -2, error); |
55e303ae | 1798 | if (!error) { |
b0d623f7 | 1799 | nfs_node_lock_force(np); |
2d21ac55 | 1800 | NFS_CHANGED_UPDATE(nfsvers, np, nvap); |
b0d623f7 | 1801 | nfs_node_unlock(np); |
55e303ae | 1802 | } |
2d21ac55 | 1803 | } else { |
b0d623f7 | 1804 | nfs_node_unlock(np); |
0a7de745 | 1805 | if (wanted) { |
b0d623f7 | 1806 | wakeup(np); |
0a7de745 | 1807 | } |
1c79356b | 1808 | } |
b0d623f7 A |
1809 | } else { |
1810 | nfs_node_unlock(np); | |
0a7de745 | 1811 | if (wanted) { |
b0d623f7 | 1812 | wakeup(np); |
0a7de745 | 1813 | } |
1c79356b | 1814 | } |
6d2010ae A |
1815 | |
1816 | if (nvap == &nvattr) { | |
1817 | NVATTR_CLEANUP(nvap); | |
1818 | } else if (!(flags & NGA_ACL)) { | |
1819 | /* make sure we don't return an ACL if it wasn't asked for */ | |
1820 | NFS_BITMAP_CLR(nvap->nva_bitmap, NFS_FATTR_ACL); | |
1821 | if (nvap->nva_acl) { | |
1822 | kauth_acl_free(nvap->nva_acl); | |
1823 | nvap->nva_acl = NULL; | |
1824 | } | |
1825 | } | |
2d21ac55 | 1826 | FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag); |
0a7de745 | 1827 | return error; |
91447636 A |
1828 | } |
1829 | ||
cb323159 A |
1830 | static int |
1831 | nfs_parse_user_access( | |
1832 | mount_t mp, | |
1833 | enum vtype type) | |
1834 | { | |
1835 | int user_access = R_OK; | |
1836 | if ((vfs_flags(mp) & MNT_RDONLY) == 0) { | |
1837 | user_access |= W_OK; | |
1838 | } | |
1839 | if (type == VDIR) { | |
1840 | user_access |= X_OK; | |
1841 | } | |
1842 | return user_access; | |
1843 | } | |
1844 | ||
2d21ac55 A |
1845 | /* |
1846 | * NFS getattr call from vfs. | |
1847 | */ | |
39236c6e A |
1848 | |
1849 | /* | |
1850 | * The attributes we support over the wire. | |
0a7de745 | 1851 | * We also get fsid but the vfs layer gets it out of the mount |
39236c6e A |
1852 | * structure after this calling us so there's no need to return it, |
1853 | * and Finder expects to call getattrlist just looking for the FSID | |
1854 | * with out hanging on a non responsive server. | |
1855 | */ | |
1856 | #define NFS3_SUPPORTED_VATTRS \ | |
0a7de745 A |
1857 | (VNODE_ATTR_va_rdev | \ |
1858 | VNODE_ATTR_va_nlink | \ | |
1859 | VNODE_ATTR_va_data_size | \ | |
1860 | VNODE_ATTR_va_data_alloc | \ | |
1861 | VNODE_ATTR_va_uid | \ | |
1862 | VNODE_ATTR_va_gid | \ | |
1863 | VNODE_ATTR_va_mode | \ | |
1864 | VNODE_ATTR_va_modify_time | \ | |
1865 | VNODE_ATTR_va_change_time | \ | |
1866 | VNODE_ATTR_va_access_time | \ | |
1867 | VNODE_ATTR_va_fileid | \ | |
39236c6e A |
1868 | VNODE_ATTR_va_type) |
1869 | ||
cb323159 | 1870 | |
b0d623f7 | 1871 | int |
2d21ac55 | 1872 | nfs3_vnop_getattr( |
91447636 | 1873 | struct vnop_getattr_args /* { |
0a7de745 A |
1874 | * struct vnodeop_desc *a_desc; |
1875 | * vnode_t a_vp; | |
1876 | * struct vnode_attr *a_vap; | |
1877 | * vfs_context_t a_context; | |
1878 | * } */*ap) | |
91447636 A |
1879 | { |
1880 | int error; | |
1881 | struct nfs_vattr nva; | |
1882 | struct vnode_attr *vap = ap->a_vap; | |
5ba3f43e | 1883 | struct nfsmount *nmp; |
2d21ac55 | 1884 | dev_t rdev; |
91447636 | 1885 | |
cb323159 A |
1886 | nmp = VTONMP(ap->a_vp); |
1887 | ||
39236c6e A |
1888 | /* |
1889 | * Lets don't go over the wire if we don't support any of the attributes. | |
1890 | * Just fall through at the VFS layer and let it cons up what it needs. | |
1891 | */ | |
1892 | /* Return the io size no matter what, since we don't go over the wire for this */ | |
1893 | VATTR_RETURN(vap, va_iosize, nfs_iosize); | |
cb323159 | 1894 | |
0a7de745 A |
1895 | if ((vap->va_active & NFS3_SUPPORTED_VATTRS) == 0) { |
1896 | return 0; | |
1897 | } | |
39236c6e | 1898 | |
0a7de745 A |
1899 | if (VATTR_IS_ACTIVE(ap->a_vap, va_name)) { |
1900 | NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n", | |
1901 | (uint64_t)VM_KERNEL_ADDRPERM(ap->a_vp), | |
1902 | ap->a_vp->v_name ? ap->a_vp->v_name : "empty"); | |
1903 | } | |
b0d623f7 | 1904 | error = nfs_getattr(VTONFS(ap->a_vp), &nva, ap->a_context, NGA_CACHED); |
0a7de745 A |
1905 | if (error) { |
1906 | return error; | |
1907 | } | |
91447636 A |
1908 | |
1909 | /* copy nva to *a_vap */ | |
2d21ac55 A |
1910 | VATTR_RETURN(vap, va_type, nva.nva_type); |
1911 | VATTR_RETURN(vap, va_mode, nva.nva_mode); | |
1912 | rdev = makedev(nva.nva_rawdev.specdata1, nva.nva_rawdev.specdata2); | |
1913 | VATTR_RETURN(vap, va_rdev, rdev); | |
1914 | VATTR_RETURN(vap, va_uid, nva.nva_uid); | |
1915 | VATTR_RETURN(vap, va_gid, nva.nva_gid); | |
1916 | VATTR_RETURN(vap, va_nlink, nva.nva_nlink); | |
1917 | VATTR_RETURN(vap, va_fileid, nva.nva_fileid); | |
1918 | VATTR_RETURN(vap, va_data_size, nva.nva_size); | |
1919 | VATTR_RETURN(vap, va_data_alloc, nva.nva_bytes); | |
2d21ac55 A |
1920 | vap->va_access_time.tv_sec = nva.nva_timesec[NFSTIME_ACCESS]; |
1921 | vap->va_access_time.tv_nsec = nva.nva_timensec[NFSTIME_ACCESS]; | |
1922 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
1923 | vap->va_modify_time.tv_sec = nva.nva_timesec[NFSTIME_MODIFY]; | |
1924 | vap->va_modify_time.tv_nsec = nva.nva_timensec[NFSTIME_MODIFY]; | |
1925 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
1926 | vap->va_change_time.tv_sec = nva.nva_timesec[NFSTIME_CHANGE]; | |
1927 | vap->va_change_time.tv_nsec = nva.nva_timensec[NFSTIME_CHANGE]; | |
1928 | VATTR_SET_SUPPORTED(vap, va_change_time); | |
91447636 | 1929 | |
cb323159 | 1930 | |
2d21ac55 | 1931 | // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */); |
0a7de745 | 1932 | return error; |
1c79356b A |
1933 | } |
1934 | ||
1935 | /* | |
2d21ac55 | 1936 | * NFS setattr call. |
1c79356b | 1937 | */ |
b0d623f7 | 1938 | int |
2d21ac55 | 1939 | nfs_vnop_setattr( |
91447636 | 1940 | struct vnop_setattr_args /* { |
0a7de745 A |
1941 | * struct vnodeop_desc *a_desc; |
1942 | * vnode_t a_vp; | |
1943 | * struct vnode_attr *a_vap; | |
1944 | * vfs_context_t a_context; | |
1945 | * } */*ap) | |
1c79356b | 1946 | { |
2d21ac55 | 1947 | vfs_context_t ctx = ap->a_context; |
91447636 | 1948 | vnode_t vp = ap->a_vp; |
2d21ac55 | 1949 | nfsnode_t np = VTONFS(vp); |
0c530ab8 | 1950 | struct nfsmount *nmp; |
91447636 | 1951 | struct vnode_attr *vap = ap->a_vap; |
1c79356b | 1952 | int error = 0; |
6d2010ae A |
1953 | int biosize, nfsvers, namedattrs; |
1954 | u_quad_t origsize, vapsize; | |
2d21ac55 A |
1955 | struct nfs_dulookup dul; |
1956 | nfsnode_t dnp = NULL; | |
813fb2f6 | 1957 | int dul_in_progress = 0; |
2d21ac55 A |
1958 | vnode_t dvp = NULL; |
1959 | const char *vname = NULL; | |
cb323159 | 1960 | #if CONFIG_NFS4 |
b0d623f7 A |
1961 | struct nfs_open_owner *noop = NULL; |
1962 | struct nfs_open_file *nofp = NULL; | |
cb323159 | 1963 | #endif |
2d21ac55 | 1964 | nmp = VTONMP(vp); |
0a7de745 A |
1965 | if (nfs_mount_gone(nmp)) { |
1966 | return ENXIO; | |
1967 | } | |
2d21ac55 | 1968 | nfsvers = nmp->nm_vers; |
6d2010ae | 1969 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
0c530ab8 | 1970 | biosize = nmp->nm_biosize; |
fa4905b1 | 1971 | |
91447636 | 1972 | /* Disallow write attempts if the filesystem is mounted read-only. */ |
0a7de745 A |
1973 | if (vnode_vfsisrdonly(vp)) { |
1974 | return EROFS; | |
1975 | } | |
91447636 | 1976 | |
2d21ac55 | 1977 | origsize = np->n_size; |
91447636 | 1978 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { |
2d21ac55 A |
1979 | switch (vnode_vtype(vp)) { |
1980 | case VDIR: | |
0a7de745 | 1981 | return EISDIR; |
2d21ac55 A |
1982 | case VCHR: |
1983 | case VBLK: | |
1984 | case VSOCK: | |
1985 | case VFIFO: | |
91447636 A |
1986 | if (!VATTR_IS_ACTIVE(vap, va_modify_time) && |
1987 | !VATTR_IS_ACTIVE(vap, va_access_time) && | |
1988 | !VATTR_IS_ACTIVE(vap, va_mode) && | |
1989 | !VATTR_IS_ACTIVE(vap, va_uid) && | |
2d21ac55 | 1990 | !VATTR_IS_ACTIVE(vap, va_gid)) { |
0a7de745 | 1991 | return 0; |
2d21ac55 | 1992 | } |
91447636 | 1993 | VATTR_CLEAR_ACTIVE(vap, va_data_size); |
2d21ac55 A |
1994 | break; |
1995 | default: | |
1c79356b A |
1996 | /* |
1997 | * Disallow write attempts if the filesystem is | |
1998 | * mounted read-only. | |
1999 | */ | |
0a7de745 A |
2000 | if (vnode_vfsisrdonly(vp)) { |
2001 | return EROFS; | |
2002 | } | |
91447636 | 2003 | FSDBG_TOP(512, np->n_size, vap->va_data_size, |
0a7de745 | 2004 | np->n_vattr.nva_size, np->n_flag); |
2d21ac55 | 2005 | /* clear NNEEDINVALIDATE, if set */ |
0a7de745 A |
2006 | if ((error = nfs_node_lock(np))) { |
2007 | return error; | |
2008 | } | |
2009 | if (np->n_flag & NNEEDINVALIDATE) { | |
2d21ac55 | 2010 | np->n_flag &= ~NNEEDINVALIDATE; |
0a7de745 | 2011 | } |
b0d623f7 | 2012 | nfs_node_unlock(np); |
2d21ac55 | 2013 | /* flush everything */ |
0a7de745 | 2014 | error = nfs_vinvalbuf(vp, (vap->va_data_size ? V_SAVE : 0), ctx, 1); |
2d21ac55 | 2015 | if (error) { |
6d2010ae | 2016 | NP(np, "nfs_setattr: nfs_vinvalbuf %d", error); |
2d21ac55 | 2017 | FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, -1); |
0a7de745 | 2018 | return error; |
2d21ac55 | 2019 | } |
cb323159 | 2020 | #if CONFIG_NFS4 |
b0d623f7 A |
2021 | if (nfsvers >= NFS_VER4) { |
2022 | /* setting file size requires having the file open for write access */ | |
0a7de745 A |
2023 | if (np->n_flag & NREVOKE) { |
2024 | return EIO; | |
2025 | } | |
b0d623f7 | 2026 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1); |
0a7de745 A |
2027 | if (!noop) { |
2028 | return ENOMEM; | |
2029 | } | |
6d2010ae A |
2030 | restart: |
2031 | error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx)); | |
0a7de745 A |
2032 | if (error) { |
2033 | return error; | |
2034 | } | |
6d2010ae A |
2035 | if (np->n_flag & NREVOKE) { |
2036 | nfs_mount_state_in_use_end(nmp, 0); | |
0a7de745 | 2037 | return EIO; |
6d2010ae | 2038 | } |
b0d623f7 | 2039 | error = nfs_open_file_find(np, noop, &nofp, 0, 0, 1); |
0a7de745 | 2040 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) { |
b0d623f7 | 2041 | error = EIO; |
0a7de745 | 2042 | } |
b0d623f7 | 2043 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { |
6d2010ae A |
2044 | nfs_mount_state_in_use_end(nmp, 0); |
2045 | error = nfs4_reopen(nofp, vfs_context_thread(ctx)); | |
b0d623f7 | 2046 | nofp = NULL; |
0a7de745 | 2047 | if (!error) { |
6d2010ae | 2048 | goto restart; |
0a7de745 | 2049 | } |
b0d623f7 | 2050 | } |
0a7de745 | 2051 | if (!error) { |
6d2010ae | 2052 | error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx)); |
0a7de745 | 2053 | } |
b0d623f7 A |
2054 | if (error) { |
2055 | nfs_open_owner_rele(noop); | |
0a7de745 | 2056 | return error; |
b0d623f7 A |
2057 | } |
2058 | if (!(nofp->nof_access & NFS_OPEN_SHARE_ACCESS_WRITE)) { | |
2059 | /* we don't have the file open for write access, so open it */ | |
b0d623f7 | 2060 | error = nfs4_open(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx); |
0a7de745 | 2061 | if (!error) { |
b0d623f7 | 2062 | nofp->nof_flags |= NFS_OPEN_FILE_SETATTR; |
0a7de745 | 2063 | } |
b0d623f7 A |
2064 | if (nfs_mount_state_error_should_restart(error)) { |
2065 | nfs_open_file_clear_busy(nofp); | |
2066 | nofp = NULL; | |
0a7de745 | 2067 | if (nfs_mount_state_in_use_end(nmp, error)) { |
6d2010ae | 2068 | goto restart; |
0a7de745 | 2069 | } |
b0d623f7 | 2070 | } |
b0d623f7 A |
2071 | } |
2072 | } | |
cb323159 | 2073 | #endif |
b0d623f7 | 2074 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
2d21ac55 | 2075 | if (np->n_size > vap->va_data_size) { /* shrinking? */ |
91447636 | 2076 | daddr64_t obn, bn; |
0c530ab8 | 2077 | int neweofoff, mustwrite; |
55e303ae | 2078 | struct nfsbuf *bp; |
fa4905b1 | 2079 | |
fa4905b1 | 2080 | obn = (np->n_size - 1) / biosize; |
2d21ac55 | 2081 | bn = vap->va_data_size / biosize; |
0a7de745 A |
2082 | for (; obn >= bn; obn--) { |
2083 | if (!nfs_buf_is_incore(np, obn)) { | |
91447636 | 2084 | continue; |
0a7de745 | 2085 | } |
2d21ac55 | 2086 | error = nfs_buf_get(np, obn, biosize, NULL, NBLK_READ, &bp); |
0a7de745 | 2087 | if (error) { |
55e303ae | 2088 | continue; |
0a7de745 | 2089 | } |
91447636 A |
2090 | if (obn != bn) { |
2091 | FSDBG(512, bp, bp->nb_flags, 0, obn); | |
2092 | SET(bp->nb_flags, NB_INVAL); | |
2093 | nfs_buf_release(bp, 1); | |
2094 | continue; | |
2095 | } | |
2096 | mustwrite = 0; | |
2097 | neweofoff = vap->va_data_size - NBOFF(bp); | |
2098 | /* check for any dirty data before the new EOF */ | |
2d21ac55 | 2099 | if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) { |
55e303ae | 2100 | /* clip dirty range to EOF */ |
2d21ac55 | 2101 | if (bp->nb_dirtyend > neweofoff) { |
91447636 | 2102 | bp->nb_dirtyend = neweofoff; |
0a7de745 | 2103 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) { |
2d21ac55 | 2104 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; |
0a7de745 | 2105 | } |
2d21ac55 | 2106 | } |
0a7de745 | 2107 | if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) { |
2d21ac55 | 2108 | mustwrite++; |
0a7de745 | 2109 | } |
91447636 | 2110 | } |
0a7de745 A |
2111 | bp->nb_dirty &= (1 << round_page_32(neweofoff) / PAGE_SIZE) - 1; |
2112 | if (bp->nb_dirty) { | |
55e303ae | 2113 | mustwrite++; |
0a7de745 | 2114 | } |
91447636 A |
2115 | if (!mustwrite) { |
2116 | FSDBG(512, bp, bp->nb_flags, 0, obn); | |
2117 | SET(bp->nb_flags, NB_INVAL); | |
2118 | nfs_buf_release(bp, 1); | |
2119 | continue; | |
2120 | } | |
2121 | /* gotta write out dirty data before invalidating */ | |
2122 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
2123 | /* (NB_NOCACHE indicates buffer should be discarded) */ | |
2124 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL | NB_ASYNC | NB_READ)); | |
2125 | SET(bp->nb_flags, NB_STABLE | NB_NOCACHE); | |
0c530ab8 | 2126 | if (!IS_VALID_CRED(bp->nb_wcred)) { |
2d21ac55 | 2127 | kauth_cred_t cred = vfs_context_ucred(ctx); |
91447636 A |
2128 | kauth_cred_ref(cred); |
2129 | bp->nb_wcred = cred; | |
2130 | } | |
2131 | error = nfs_buf_write(bp); | |
2132 | // Note: bp has been released | |
2133 | if (error) { | |
2134 | FSDBG(512, bp, 0xd00dee, 0xbad, error); | |
b0d623f7 | 2135 | nfs_node_lock_force(np); |
91447636 A |
2136 | np->n_error = error; |
2137 | np->n_flag |= NWRITEERR; | |
55e303ae | 2138 | /* |
91447636 A |
2139 | * There was a write error and we need to |
2140 | * invalidate attrs and flush buffers in | |
2141 | * order to sync up with the server. | |
2142 | * (if this write was extending the file, | |
2143 | * we may no longer know the correct size) | |
55e303ae | 2144 | */ |
91447636 | 2145 | NATTRINVALIDATE(np); |
b0d623f7 | 2146 | nfs_node_unlock(np); |
2d21ac55 | 2147 | nfs_data_unlock(np); |
0a7de745 | 2148 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
b0d623f7 | 2149 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
91447636 | 2150 | error = 0; |
fa4905b1 | 2151 | } |
91447636 | 2152 | } |
1c79356b | 2153 | } |
0a7de745 | 2154 | if (vap->va_data_size != np->n_size) { |
2d21ac55 | 2155 | ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */ |
0a7de745 | 2156 | } |
2d21ac55 | 2157 | origsize = np->n_size; |
91447636 | 2158 | np->n_size = np->n_vattr.nva_size = vap->va_data_size; |
b0d623f7 | 2159 | nfs_node_lock_force(np); |
2d21ac55 | 2160 | CLR(np->n_flag, NUPDATESIZE); |
b0d623f7 | 2161 | nfs_node_unlock(np); |
2d21ac55 A |
2162 | FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); |
2163 | } | |
2164 | } else if (VATTR_IS_ACTIVE(vap, va_modify_time) || | |
0a7de745 A |
2165 | VATTR_IS_ACTIVE(vap, va_access_time) || |
2166 | (vap->va_vaflags & VA_UTIMES_NULL)) { | |
2167 | if ((error = nfs_node_lock(np))) { | |
2168 | return error; | |
2169 | } | |
2d21ac55 | 2170 | if ((np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) { |
b0d623f7 | 2171 | nfs_node_unlock(np); |
2d21ac55 | 2172 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
0a7de745 A |
2173 | if (error == EINTR) { |
2174 | return error; | |
2175 | } | |
2d21ac55 | 2176 | } else { |
b0d623f7 | 2177 | nfs_node_unlock(np); |
2d21ac55 | 2178 | } |
55e303ae | 2179 | } |
6d2010ae | 2180 | if ((VATTR_IS_ACTIVE(vap, va_mode) || VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) || |
0a7de745 | 2181 | VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid)) && |
6d2010ae A |
2182 | !(error = nfs_node_lock(np))) { |
2183 | NACCESSINVALIDATE(np); | |
b0d623f7 | 2184 | nfs_node_unlock(np); |
6d2010ae A |
2185 | if (!namedattrs) { |
2186 | dvp = vnode_getparent(vp); | |
2187 | vname = vnode_getname(vp); | |
2188 | dnp = (dvp && vname) ? VTONFS(dvp) : NULL; | |
2189 | if (dnp) { | |
813fb2f6 A |
2190 | if (nfs_node_set_busy(dnp, vfs_context_thread(ctx))) { |
2191 | vnode_put(dvp); | |
2192 | vnode_putname(vname); | |
2193 | } else { | |
2194 | nfs_dulookup_init(&dul, dnp, vname, strlen(vname), ctx); | |
2195 | nfs_dulookup_start(&dul, dnp, ctx); | |
2196 | dul_in_progress = 1; | |
6d2010ae | 2197 | } |
813fb2f6 | 2198 | } else { |
0a7de745 | 2199 | if (dvp) { |
813fb2f6 | 2200 | vnode_put(dvp); |
0a7de745 A |
2201 | } |
2202 | if (vname) { | |
813fb2f6 | 2203 | vnode_putname(vname); |
0a7de745 | 2204 | } |
2d21ac55 | 2205 | } |
2d21ac55 A |
2206 | } |
2207 | } | |
2208 | ||
0a7de745 | 2209 | if (!error) { |
b0d623f7 | 2210 | error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx); |
0a7de745 | 2211 | } |
b0d623f7 | 2212 | |
813fb2f6 A |
2213 | if (dul_in_progress) { |
2214 | nfs_dulookup_finish(&dul, dnp, ctx); | |
2215 | nfs_node_clear_busy(dnp); | |
2216 | vnode_put(dvp); | |
2217 | vnode_putname(vname); | |
91447636 | 2218 | } |
1c79356b | 2219 | |
2d21ac55 A |
2220 | FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error); |
2221 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { | |
6d2010ae A |
2222 | if (error && (origsize != np->n_size) && |
2223 | ((nfsvers < NFS_VER4) || !nfs_mount_state_error_should_restart(error))) { | |
2d21ac55 | 2224 | /* make every effort to resync file size w/ server... */ |
6d2010ae | 2225 | /* (don't bother if we'll be restarting the operation) */ |
2d21ac55 A |
2226 | int err; /* preserve "error" for return */ |
2227 | np->n_size = np->n_vattr.nva_size = origsize; | |
b0d623f7 | 2228 | nfs_node_lock_force(np); |
2d21ac55 | 2229 | CLR(np->n_flag, NUPDATESIZE); |
b0d623f7 | 2230 | nfs_node_unlock(np); |
2d21ac55 A |
2231 | FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002); |
2232 | ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */ | |
6d2010ae | 2233 | vapsize = vap->va_data_size; |
2d21ac55 | 2234 | vap->va_data_size = origsize; |
b0d623f7 | 2235 | err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx); |
0a7de745 | 2236 | if (err) { |
6d2010ae | 2237 | NP(np, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers, error, err); |
0a7de745 | 2238 | } |
6d2010ae | 2239 | vap->va_data_size = vapsize; |
2d21ac55 | 2240 | } |
b0d623f7 A |
2241 | nfs_node_lock_force(np); |
2242 | /* | |
2243 | * The size was just set. If the size is already marked for update, don't | |
2244 | * trust the newsize (it may have been set while the setattr was in progress). | |
2245 | * Clear the update flag and make sure we fetch new attributes so we are sure | |
2246 | * we have the latest size. | |
2247 | */ | |
2248 | if (ISSET(np->n_flag, NUPDATESIZE)) { | |
2249 | CLR(np->n_flag, NUPDATESIZE); | |
2250 | NATTRINVALIDATE(np); | |
2251 | nfs_node_unlock(np); | |
6d2010ae | 2252 | nfs_getattr(np, NULL, ctx, NGA_UNCACHED); |
b0d623f7 A |
2253 | } else { |
2254 | nfs_node_unlock(np); | |
2255 | } | |
2d21ac55 | 2256 | nfs_data_unlock(np); |
cb323159 | 2257 | #if CONFIG_NFS4 |
b0d623f7 | 2258 | if (nfsvers >= NFS_VER4) { |
6d2010ae A |
2259 | if (nofp) { |
2260 | /* don't close our setattr open if we'll be restarting... */ | |
2261 | if (!nfs_mount_state_error_should_restart(error) && | |
2262 | (nofp->nof_flags & NFS_OPEN_FILE_SETATTR)) { | |
2263 | int err = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx); | |
0a7de745 | 2264 | if (err) { |
6d2010ae | 2265 | NP(np, "nfs_vnop_setattr: close error: %d", err); |
0a7de745 | 2266 | } |
6d2010ae | 2267 | nofp->nof_flags &= ~NFS_OPEN_FILE_SETATTR; |
b0d623f7 | 2268 | } |
b0d623f7 | 2269 | nfs_open_file_clear_busy(nofp); |
6d2010ae | 2270 | nofp = NULL; |
b0d623f7 | 2271 | } |
0a7de745 | 2272 | if (nfs_mount_state_in_use_end(nmp, error)) { |
6d2010ae | 2273 | goto restart; |
0a7de745 | 2274 | } |
b0d623f7 A |
2275 | nfs_open_owner_rele(noop); |
2276 | } | |
cb323159 | 2277 | #endif |
1c79356b | 2278 | } |
0a7de745 | 2279 | return error; |
1c79356b A |
2280 | } |
2281 | ||
2282 | /* | |
2d21ac55 | 2283 | * Do an NFS setattr RPC. |
1c79356b | 2284 | */ |
2d21ac55 A |
2285 | int |
2286 | nfs3_setattr_rpc( | |
2287 | nfsnode_t np, | |
2288 | struct vnode_attr *vap, | |
b0d623f7 | 2289 | vfs_context_t ctx) |
1c79356b | 2290 | { |
2d21ac55 A |
2291 | struct nfsmount *nmp = NFSTONMP(np); |
2292 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0, nfsvers; | |
b0d623f7 | 2293 | u_int64_t xid, nextxid; |
2d21ac55 | 2294 | struct nfsm_chain nmreq, nmrep; |
55e303ae | 2295 | |
0a7de745 A |
2296 | if (nfs_mount_gone(nmp)) { |
2297 | return ENXIO; | |
2298 | } | |
2d21ac55 | 2299 | nfsvers = nmp->nm_vers; |
1c79356b | 2300 | |
2d21ac55 A |
2301 | VATTR_SET_SUPPORTED(vap, va_mode); |
2302 | VATTR_SET_SUPPORTED(vap, va_uid); | |
2303 | VATTR_SET_SUPPORTED(vap, va_gid); | |
2304 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
2305 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
2306 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
2307 | ||
cb323159 A |
2308 | |
2309 | if (VATTR_IS_ACTIVE(vap, va_flags) | |
2310 | ) { | |
0a7de745 A |
2311 | if (vap->va_flags) { /* we don't support setting flags */ |
2312 | if (vap->va_active & ~VNODE_ATTR_va_flags) { | |
2313 | return EINVAL; /* return EINVAL if other attributes also set */ | |
2314 | } else { | |
2315 | return ENOTSUP; /* return ENOTSUP for chflags(2) */ | |
2316 | } | |
2d21ac55 A |
2317 | } |
2318 | /* no flags set, so we'll just ignore it */ | |
0a7de745 A |
2319 | if (!(vap->va_active & ~VNODE_ATTR_va_flags)) { |
2320 | return 0; /* no (other) attributes to set, so nothing to do */ | |
2321 | } | |
2d21ac55 A |
2322 | } |
2323 | ||
2324 | nfsm_chain_null(&nmreq); | |
2325 | nfsm_chain_null(&nmrep); | |
2326 | ||
2327 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 2328 | NFSX_FH(nfsvers) + NFSX_SATTR(nfsvers)); |
2d21ac55 A |
2329 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); |
2330 | if (nfsvers == NFS_VER3) { | |
91447636 | 2331 | if (VATTR_IS_ACTIVE(vap, va_mode)) { |
2d21ac55 A |
2332 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2333 | nfsm_chain_add_32(error, &nmreq, vap->va_mode); | |
1c79356b | 2334 | } else { |
2d21ac55 | 2335 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2336 | } |
91447636 | 2337 | if (VATTR_IS_ACTIVE(vap, va_uid)) { |
2d21ac55 A |
2338 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2339 | nfsm_chain_add_32(error, &nmreq, vap->va_uid); | |
1c79356b | 2340 | } else { |
2d21ac55 | 2341 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2342 | } |
91447636 | 2343 | if (VATTR_IS_ACTIVE(vap, va_gid)) { |
2d21ac55 A |
2344 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2345 | nfsm_chain_add_32(error, &nmreq, vap->va_gid); | |
1c79356b | 2346 | } else { |
2d21ac55 | 2347 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2348 | } |
91447636 | 2349 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { |
2d21ac55 A |
2350 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2351 | nfsm_chain_add_64(error, &nmreq, vap->va_data_size); | |
1c79356b | 2352 | } else { |
2d21ac55 | 2353 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2354 | } |
2d21ac55 A |
2355 | if (vap->va_vaflags & VA_UTIMES_NULL) { |
2356 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER); | |
2357 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER); | |
2358 | } else { | |
2359 | if (VATTR_IS_ACTIVE(vap, va_access_time)) { | |
2360 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT); | |
2361 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec); | |
2362 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_nsec); | |
1c79356b | 2363 | } else { |
2d21ac55 | 2364 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE); |
1c79356b | 2365 | } |
2d21ac55 A |
2366 | if (VATTR_IS_ACTIVE(vap, va_modify_time)) { |
2367 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT); | |
2368 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec); | |
2369 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_nsec); | |
1c79356b | 2370 | } else { |
2d21ac55 | 2371 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE); |
1c79356b | 2372 | } |
1c79356b | 2373 | } |
2d21ac55 | 2374 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2375 | } else { |
2d21ac55 | 2376 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_mode) ? |
0a7de745 | 2377 | vtonfsv2_mode(vnode_vtype(NFSTOV(np)), vap->va_mode) : -1); |
2d21ac55 | 2378 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_uid) ? |
0a7de745 | 2379 | vap->va_uid : (uint32_t)-1); |
2d21ac55 | 2380 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_gid) ? |
0a7de745 | 2381 | vap->va_gid : (uint32_t)-1); |
2d21ac55 | 2382 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_data_size) ? |
0a7de745 | 2383 | vap->va_data_size : (uint32_t)-1); |
91447636 | 2384 | if (VATTR_IS_ACTIVE(vap, va_access_time)) { |
2d21ac55 A |
2385 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec); |
2386 | nfsm_chain_add_32(error, &nmreq, (vap->va_access_time.tv_nsec != -1) ? | |
0a7de745 | 2387 | ((uint32_t)vap->va_access_time.tv_nsec / 1000) : 0xffffffff); |
91447636 | 2388 | } else { |
2d21ac55 A |
2389 | nfsm_chain_add_32(error, &nmreq, -1); |
2390 | nfsm_chain_add_32(error, &nmreq, -1); | |
91447636 A |
2391 | } |
2392 | if (VATTR_IS_ACTIVE(vap, va_modify_time)) { | |
2d21ac55 A |
2393 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec); |
2394 | nfsm_chain_add_32(error, &nmreq, (vap->va_modify_time.tv_nsec != -1) ? | |
0a7de745 | 2395 | ((uint32_t)vap->va_modify_time.tv_nsec / 1000) : 0xffffffff); |
91447636 | 2396 | } else { |
2d21ac55 A |
2397 | nfsm_chain_add_32(error, &nmreq, -1); |
2398 | nfsm_chain_add_32(error, &nmreq, -1); | |
91447636 | 2399 | } |
1c79356b | 2400 | } |
2d21ac55 A |
2401 | nfsm_chain_build_done(error, &nmreq); |
2402 | nfsmout_if(error); | |
6d2010ae | 2403 | error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx, NULL, &nmrep, &xid, &status); |
0a7de745 | 2404 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 2405 | error = lockerror; |
0a7de745 | 2406 | } |
2d21ac55 | 2407 | if (nfsvers == NFS_VER3) { |
cb323159 | 2408 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
2409 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); |
2410 | nfsmout_if(error); | |
483a1d10 | 2411 | /* if file hadn't changed, update cached mtime */ |
0a7de745 | 2412 | if (nfstimespeccmp(&np->n_mtime, &premtime, ==)) { |
2d21ac55 | 2413 | NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr); |
0a7de745 | 2414 | } |
483a1d10 | 2415 | /* if directory hadn't changed, update namecache mtime */ |
2d21ac55 | 2416 | if ((vnode_vtype(NFSTOV(np)) == VDIR) && |
0a7de745 | 2417 | nfstimespeccmp(&np->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 2418 | NFS_CHANGED_UPDATE_NC(nfsvers, np, &np->n_vattr); |
0a7de745 A |
2419 | } |
2420 | if (!wccpostattr) { | |
2d21ac55 | 2421 | NATTRINVALIDATE(np); |
0a7de745 | 2422 | } |
2d21ac55 | 2423 | error = status; |
e5568f75 | 2424 | } else { |
0a7de745 | 2425 | if (!error) { |
2d21ac55 | 2426 | error = status; |
0a7de745 | 2427 | } |
6d2010ae | 2428 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
e5568f75 | 2429 | } |
b0d623f7 A |
2430 | /* |
2431 | * We just changed the attributes and we want to make sure that we | |
2432 | * see the latest attributes. Get the next XID. If it's not the | |
2433 | * next XID after the SETATTR XID, then it's possible that another | |
2434 | * RPC was in flight at the same time and it might put stale attributes | |
2435 | * in the cache. In that case, we invalidate the attributes and set | |
2436 | * the attribute cache XID to guarantee that newer attributes will | |
2437 | * get loaded next. | |
2438 | */ | |
2439 | nextxid = 0; | |
2440 | nfs_get_xid(&nextxid); | |
2441 | if (nextxid != (xid + 1)) { | |
2442 | np->n_xid = nextxid; | |
2443 | NATTRINVALIDATE(np); | |
2444 | } | |
2d21ac55 | 2445 | nfsmout: |
0a7de745 | 2446 | if (!lockerror) { |
b0d623f7 | 2447 | nfs_node_unlock(np); |
0a7de745 | 2448 | } |
2d21ac55 A |
2449 | nfsm_chain_cleanup(&nmreq); |
2450 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 2451 | return error; |
1c79356b A |
2452 | } |
2453 | ||
2454 | /* | |
2d21ac55 | 2455 | * NFS lookup call, one step at a time... |
1c79356b | 2456 | * First look in cache |
2d21ac55 | 2457 | * If not found, unlock the directory nfsnode and do the RPC |
1c79356b | 2458 | */ |
b0d623f7 | 2459 | int |
2d21ac55 | 2460 | nfs_vnop_lookup( |
91447636 | 2461 | struct vnop_lookup_args /* { |
0a7de745 A |
2462 | * struct vnodeop_desc *a_desc; |
2463 | * vnode_t a_dvp; | |
2464 | * vnode_t *a_vpp; | |
2465 | * struct componentname *a_cnp; | |
2466 | * vfs_context_t a_context; | |
2467 | * } */*ap) | |
1c79356b | 2468 | { |
2d21ac55 | 2469 | vfs_context_t ctx = ap->a_context; |
91447636 A |
2470 | struct componentname *cnp = ap->a_cnp; |
2471 | vnode_t dvp = ap->a_dvp; | |
2472 | vnode_t *vpp = ap->a_vpp; | |
2473 | int flags = cnp->cn_flags; | |
2474 | vnode_t newvp; | |
2d21ac55 A |
2475 | nfsnode_t dnp, np; |
2476 | struct nfsmount *nmp; | |
2477 | mount_t mp; | |
b0d623f7 | 2478 | int nfsvers, error, busyerror = ENOENT, isdot, isdotdot, negnamecache; |
2d21ac55 | 2479 | u_int64_t xid; |
91447636 | 2480 | struct nfs_vattr nvattr; |
91447636 | 2481 | int ngflags; |
2d21ac55 A |
2482 | struct vnop_access_args naa; |
2483 | fhandle_t fh; | |
2484 | struct nfsreq rq, *req = &rq; | |
1c79356b | 2485 | |
1c79356b | 2486 | *vpp = NULLVP; |
55e303ae | 2487 | |
91447636 | 2488 | dnp = VTONFS(dvp); |
6d2010ae | 2489 | NVATTR_INIT(&nvattr); |
fa4905b1 | 2490 | |
2d21ac55 A |
2491 | mp = vnode_mount(dvp); |
2492 | nmp = VFSTONFS(mp); | |
fe8ab488 | 2493 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2494 | error = ENXIO; |
2495 | goto error_return; | |
2496 | } | |
2497 | nfsvers = nmp->nm_vers; | |
6d2010ae | 2498 | negnamecache = !NMFLAG(nmp, NONEGNAMECACHE); |
2d21ac55 | 2499 | |
0a7de745 | 2500 | if ((error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)))) { |
b0d623f7 | 2501 | goto error_return; |
0a7de745 | 2502 | } |
b0d623f7 | 2503 | /* nfs_getattr() will check changed and purge caches */ |
0a7de745 | 2504 | if ((error = nfs_getattr(dnp, NULL, ctx, NGA_CACHED))) { |
91447636 | 2505 | goto error_return; |
0a7de745 | 2506 | } |
1c79356b | 2507 | |
91447636 A |
2508 | error = cache_lookup(dvp, vpp, cnp); |
2509 | switch (error) { | |
2510 | case ENOENT: | |
2d21ac55 A |
2511 | /* negative cache entry */ |
2512 | goto error_return; | |
91447636 A |
2513 | case 0: |
2514 | /* cache miss */ | |
6d2010ae | 2515 | if ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { |
b0d623f7 A |
2516 | /* if rdirplus, try dir buf cache lookup */ |
2517 | error = nfs_dir_buf_cache_lookup(dnp, &np, cnp, ctx, 0); | |
2518 | if (!error && np) { | |
2519 | /* dir buf cache hit */ | |
2520 | *vpp = NFSTOV(np); | |
2521 | error = -1; | |
2522 | } | |
2523 | } | |
0a7de745 | 2524 | if (error != -1) { /* cache miss */ |
b0d623f7 | 2525 | break; |
0a7de745 A |
2526 | } |
2527 | /* FALLTHROUGH */ | |
91447636 A |
2528 | case -1: |
2529 | /* cache hit, not really an error */ | |
316670eb | 2530 | OSAddAtomic64(1, &nfsstats.lookupcache_hits); |
91447636 | 2531 | |
b0d623f7 | 2532 | nfs_node_clear_busy(dnp); |
6d2010ae | 2533 | busyerror = ENOENT; |
2d21ac55 | 2534 | |
91447636 | 2535 | /* check for directory access */ |
6d2010ae | 2536 | naa.a_desc = &vnop_access_desc; |
91447636 A |
2537 | naa.a_vp = dvp; |
2538 | naa.a_action = KAUTH_VNODE_SEARCH; | |
2d21ac55 | 2539 | naa.a_context = ctx; |
91447636 A |
2540 | |
2541 | /* compute actual success/failure based on accessibility */ | |
2d21ac55 | 2542 | error = nfs_vnop_access(&naa); |
0a7de745 | 2543 | /* FALLTHROUGH */ |
91447636 A |
2544 | default: |
2545 | /* unexpected error from cache_lookup */ | |
2546 | goto error_return; | |
2547 | } | |
2d21ac55 A |
2548 | |
2549 | /* skip lookup, if we know who we are: "." or ".." */ | |
2550 | isdot = isdotdot = 0; | |
2551 | if (cnp->cn_nameptr[0] == '.') { | |
0a7de745 | 2552 | if (cnp->cn_namelen == 1) { |
2d21ac55 | 2553 | isdot = 1; |
0a7de745 A |
2554 | } |
2555 | if ((cnp->cn_namelen == 2) && (cnp->cn_nameptr[1] == '.')) { | |
2d21ac55 | 2556 | isdotdot = 1; |
0a7de745 | 2557 | } |
2d21ac55 A |
2558 | } |
2559 | if (isdotdot || isdot) { | |
2560 | fh.fh_len = 0; | |
91447636 A |
2561 | goto found; |
2562 | } | |
cb323159 | 2563 | #if CONFIG_NFS4 |
6d2010ae A |
2564 | if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) { |
2565 | /* we should never be looking things up in a trigger directory, return nothing */ | |
2566 | error = ENOENT; | |
2567 | goto error_return; | |
2568 | } | |
cb323159 | 2569 | #endif |
55e303ae | 2570 | |
91447636 | 2571 | /* do we know this name is too long? */ |
2d21ac55 | 2572 | nmp = VTONMP(dvp); |
fe8ab488 | 2573 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2574 | error = ENXIO; |
2575 | goto error_return; | |
2576 | } | |
2577 | if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) && | |
0a7de745 | 2578 | (cnp->cn_namelen > (int)nmp->nm_fsattr.nfsa_maxname)) { |
91447636 A |
2579 | error = ENAMETOOLONG; |
2580 | goto error_return; | |
1c79356b | 2581 | } |
fa4905b1 | 2582 | |
1c79356b A |
2583 | error = 0; |
2584 | newvp = NULLVP; | |
91447636 | 2585 | |
316670eb | 2586 | OSAddAtomic64(1, &nfsstats.lookupcache_misses); |
1c79356b | 2587 | |
2d21ac55 A |
2588 | error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &req); |
2589 | nfsmout_if(error); | |
6d2010ae | 2590 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, req, &xid, &fh, &nvattr); |
2d21ac55 | 2591 | nfsmout_if(error); |
91447636 | 2592 | |
91447636 | 2593 | /* is the file handle the same as this directory's file handle? */ |
2d21ac55 A |
2594 | isdot = NFS_CMPFH(dnp, fh.fh_data, fh.fh_len); |
2595 | ||
2596 | found: | |
2d21ac55 A |
2597 | if (flags & ISLASTCN) { |
2598 | switch (cnp->cn_nameiop) { | |
2599 | case DELETE: | |
2600 | cnp->cn_flags &= ~MAKEENTRY; | |
2601 | break; | |
2602 | case RENAME: | |
2603 | cnp->cn_flags &= ~MAKEENTRY; | |
2604 | if (isdot) { | |
2605 | error = EISDIR; | |
91447636 A |
2606 | goto error_return; |
2607 | } | |
2d21ac55 | 2608 | break; |
91447636 | 2609 | } |
91447636 A |
2610 | } |
2611 | ||
2d21ac55 | 2612 | if (isdotdot) { |
2d21ac55 A |
2613 | newvp = vnode_getparent(dvp); |
2614 | if (!newvp) { | |
2615 | error = ENOENT; | |
0b4e3aa0 | 2616 | goto error_return; |
1c79356b | 2617 | } |
2d21ac55 | 2618 | } else if (isdot) { |
91447636 | 2619 | error = vnode_get(dvp); |
0a7de745 | 2620 | if (error) { |
0b4e3aa0 | 2621 | goto error_return; |
0a7de745 | 2622 | } |
91447636 | 2623 | newvp = dvp; |
b0d623f7 | 2624 | nfs_node_lock_force(dnp); |
0a7de745 | 2625 | if (fh.fh_len && (dnp->n_xid <= xid)) { |
2d21ac55 | 2626 | nfs_loadattrcache(dnp, &nvattr, &xid, 0); |
0a7de745 | 2627 | } |
b0d623f7 | 2628 | nfs_node_unlock(dnp); |
1c79356b | 2629 | } else { |
2d21ac55 | 2630 | ngflags = (cnp->cn_flags & MAKEENTRY) ? NG_MAKEENTRY : 0; |
6d2010ae | 2631 | error = nfs_nget(mp, dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, ngflags, &np); |
0a7de745 | 2632 | if (error) { |
0b4e3aa0 | 2633 | goto error_return; |
0a7de745 | 2634 | } |
1c79356b | 2635 | newvp = NFSTOV(np); |
b0d623f7 | 2636 | nfs_node_unlock(np); |
1c79356b | 2637 | } |
1c79356b | 2638 | *vpp = newvp; |
91447636 | 2639 | |
2d21ac55 | 2640 | nfsmout: |
1c79356b | 2641 | if (error) { |
2d21ac55 A |
2642 | if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) && |
2643 | (flags & ISLASTCN) && (error == ENOENT)) { | |
0a7de745 | 2644 | if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp)) { |
1c79356b | 2645 | error = EROFS; |
0a7de745 | 2646 | } else { |
1c79356b | 2647 | error = EJUSTRETURN; |
0a7de745 | 2648 | } |
1c79356b | 2649 | } |
1c79356b | 2650 | } |
2d21ac55 A |
2651 | if ((error == ENOENT) && (cnp->cn_flags & MAKEENTRY) && |
2652 | (cnp->cn_nameiop != CREATE) && negnamecache) { | |
2653 | /* add a negative entry in the name cache */ | |
b0d623f7 | 2654 | nfs_node_lock_force(dnp); |
2d21ac55 A |
2655 | cache_enter(dvp, NULL, cnp); |
2656 | dnp->n_flag |= NNEGNCENTRIES; | |
b0d623f7 | 2657 | nfs_node_unlock(dnp); |
2d21ac55 | 2658 | } |
0b4e3aa0 | 2659 | error_return: |
6d2010ae | 2660 | NVATTR_CLEANUP(&nvattr); |
0a7de745 | 2661 | if (!busyerror) { |
b0d623f7 | 2662 | nfs_node_clear_busy(dnp); |
0a7de745 | 2663 | } |
91447636 | 2664 | if (error && *vpp) { |
0a7de745 | 2665 | vnode_put(*vpp); |
91447636 A |
2666 | *vpp = NULLVP; |
2667 | } | |
0a7de745 | 2668 | return error; |
1c79356b A |
2669 | } |
2670 | ||
fe8ab488 A |
2671 | int nfs_readlink_nocache = DEFAULT_READLINK_NOCACHE; |
2672 | ||
1c79356b | 2673 | /* |
2d21ac55 | 2674 | * NFS readlink call |
1c79356b | 2675 | */ |
b0d623f7 | 2676 | int |
2d21ac55 | 2677 | nfs_vnop_readlink( |
91447636 | 2678 | struct vnop_readlink_args /* { |
0a7de745 A |
2679 | * struct vnodeop_desc *a_desc; |
2680 | * vnode_t a_vp; | |
2681 | * struct uio *a_uio; | |
2682 | * vfs_context_t a_context; | |
2683 | * } */*ap) | |
1c79356b | 2684 | { |
2d21ac55 A |
2685 | vfs_context_t ctx = ap->a_context; |
2686 | nfsnode_t np = VTONFS(ap->a_vp); | |
2687 | struct nfsmount *nmp; | |
b0d623f7 | 2688 | int error = 0, nfsvers; |
2d21ac55 | 2689 | uint32_t buflen; |
b0d623f7 | 2690 | uio_t uio = ap->a_uio; |
2d21ac55 | 2691 | struct nfsbuf *bp = NULL; |
fe8ab488 A |
2692 | struct timespec ts; |
2693 | int timeo; | |
2d21ac55 | 2694 | |
0a7de745 A |
2695 | if (vnode_vtype(ap->a_vp) != VLNK) { |
2696 | return EPERM; | |
2697 | } | |
2d21ac55 | 2698 | |
0a7de745 A |
2699 | if (uio_resid(uio) == 0) { |
2700 | return 0; | |
2701 | } | |
2702 | if (uio_offset(uio) < 0) { | |
2703 | return EINVAL; | |
2704 | } | |
2d21ac55 A |
2705 | |
2706 | nmp = VTONMP(ap->a_vp); | |
0a7de745 A |
2707 | if (nfs_mount_gone(nmp)) { |
2708 | return ENXIO; | |
2709 | } | |
2d21ac55 A |
2710 | nfsvers = nmp->nm_vers; |
2711 | ||
0a7de745 | 2712 | |
b0d623f7 | 2713 | /* nfs_getattr() will check changed and purge caches */ |
fe8ab488 | 2714 | if ((error = nfs_getattr(np, NULL, ctx, nfs_readlink_nocache ? NGA_UNCACHED : NGA_CACHED))) { |
2d21ac55 | 2715 | FSDBG(531, np, 0xd1e0001, 0, error); |
0a7de745 | 2716 | return error; |
2d21ac55 | 2717 | } |
2d21ac55 | 2718 | |
fe8ab488 A |
2719 | if (nfs_readlink_nocache) { |
2720 | timeo = nfs_attrcachetimeout(np); | |
2721 | nanouptime(&ts); | |
2722 | } | |
0a7de745 | 2723 | |
fe8ab488 | 2724 | retry: |
316670eb | 2725 | OSAddAtomic64(1, &nfsstats.biocache_readlinks); |
fe8ab488 | 2726 | error = nfs_buf_get(np, 0, NFS_MAXPATHLEN, vfs_context_thread(ctx), NBLK_META, &bp); |
2d21ac55 A |
2727 | if (error) { |
2728 | FSDBG(531, np, 0xd1e0002, 0, error); | |
0a7de745 | 2729 | return error; |
2d21ac55 | 2730 | } |
fe8ab488 A |
2731 | |
2732 | if (nfs_readlink_nocache) { | |
2733 | NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo, ts.tv_sec, | |
0a7de745 A |
2734 | (np->n_rltim.tv_sec + timeo) < ts.tv_sec || nfs_readlink_nocache > 1, |
2735 | ISSET(bp->nb_flags, NB_CACHE) == NB_CACHE); | |
fe8ab488 | 2736 | /* n_rltim is synchronized by the associated nfs buf */ |
0a7de745 | 2737 | if (ISSET(bp->nb_flags, NB_CACHE) && ((nfs_readlink_nocache > 1) || ((np->n_rltim.tv_sec + timeo) < ts.tv_sec))) { |
fe8ab488 A |
2738 | SET(bp->nb_flags, NB_INVAL); |
2739 | nfs_buf_release(bp, 0); | |
2740 | goto retry; | |
2741 | } | |
2742 | } | |
2d21ac55 | 2743 | if (!ISSET(bp->nb_flags, NB_CACHE)) { |
fe8ab488 | 2744 | readagain: |
316670eb | 2745 | OSAddAtomic64(1, &nfsstats.readlink_bios); |
2d21ac55 A |
2746 | buflen = bp->nb_bufsize; |
2747 | error = nmp->nm_funcs->nf_readlink_rpc(np, bp->nb_data, &buflen, ctx); | |
2748 | if (error) { | |
fe8ab488 A |
2749 | if (error == ESTALE) { |
2750 | NFS_VNOP_DBG("Stale FH from readlink rpc\n"); | |
2751 | error = nfs_refresh_fh(np, ctx); | |
0a7de745 | 2752 | if (error == 0) { |
fe8ab488 | 2753 | goto readagain; |
0a7de745 | 2754 | } |
fe8ab488 | 2755 | } |
2d21ac55 A |
2756 | SET(bp->nb_flags, NB_ERROR); |
2757 | bp->nb_error = error; | |
fe8ab488 | 2758 | NFS_VNOP_DBG("readlink failed %d\n", error); |
2d21ac55 A |
2759 | } else { |
2760 | bp->nb_validoff = 0; | |
2761 | bp->nb_validend = buflen; | |
fe8ab488 A |
2762 | np->n_rltim = ts; |
2763 | NFS_VNOP_DBG("readlink of %.*s\n", bp->nb_validend, (char *)bp->nb_data); | |
2d21ac55 | 2764 | } |
fe8ab488 A |
2765 | } else { |
2766 | NFS_VNOP_DBG("got cached link of %.*s\n", bp->nb_validend, (char *)bp->nb_data); | |
2d21ac55 | 2767 | } |
0a7de745 A |
2768 | |
2769 | if (!error && (bp->nb_validend > 0)) { | |
b0d623f7 | 2770 | error = uiomove(bp->nb_data, bp->nb_validend, uio); |
0a7de745 | 2771 | } |
2d21ac55 A |
2772 | FSDBG(531, np, bp->nb_validend, 0, error); |
2773 | nfs_buf_release(bp, 1); | |
0a7de745 | 2774 | return error; |
1c79356b A |
2775 | } |
2776 | ||
2777 | /* | |
2d21ac55 | 2778 | * Do a readlink RPC. |
1c79356b A |
2779 | */ |
2780 | int | |
2d21ac55 | 2781 | nfs3_readlink_rpc(nfsnode_t np, char *buf, uint32_t *buflenp, vfs_context_t ctx) |
1c79356b | 2782 | { |
2d21ac55 A |
2783 | struct nfsmount *nmp; |
2784 | int error = 0, lockerror = ENOENT, nfsvers, status; | |
2785 | uint32_t len; | |
fa4905b1 | 2786 | u_int64_t xid; |
2d21ac55 | 2787 | struct nfsm_chain nmreq, nmrep; |
1c79356b | 2788 | |
2d21ac55 | 2789 | nmp = NFSTONMP(np); |
0a7de745 A |
2790 | if (nfs_mount_gone(nmp)) { |
2791 | return ENXIO; | |
2792 | } | |
2d21ac55 A |
2793 | nfsvers = nmp->nm_vers; |
2794 | nfsm_chain_null(&nmreq); | |
2795 | nfsm_chain_null(&nmrep); | |
2796 | ||
2797 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers)); | |
2798 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
2799 | nfsm_chain_build_done(error, &nmreq); | |
2800 | nfsmout_if(error); | |
6d2010ae | 2801 | error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx, NULL, &nmrep, &xid, &status); |
0a7de745 | 2802 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 2803 | error = lockerror; |
0a7de745 A |
2804 | } |
2805 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 2806 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 A |
2807 | } |
2808 | if (!error) { | |
2d21ac55 | 2809 | error = status; |
0a7de745 | 2810 | } |
2d21ac55 A |
2811 | nfsm_chain_get_32(error, &nmrep, len); |
2812 | nfsmout_if(error); | |
2813 | if ((nfsvers == NFS_VER2) && (len > *buflenp)) { | |
2814 | error = EBADRPC; | |
2815 | goto nfsmout; | |
2816 | } | |
2817 | if (len >= *buflenp) { | |
0a7de745 | 2818 | if (np->n_size && (np->n_size < *buflenp)) { |
2d21ac55 | 2819 | len = np->n_size; |
0a7de745 | 2820 | } else { |
2d21ac55 | 2821 | len = *buflenp - 1; |
0a7de745 | 2822 | } |
2d21ac55 A |
2823 | } |
2824 | nfsm_chain_get_opaque(error, &nmrep, len, buf); | |
0a7de745 | 2825 | if (!error) { |
2d21ac55 | 2826 | *buflenp = len; |
0a7de745 | 2827 | } |
2d21ac55 | 2828 | nfsmout: |
0a7de745 | 2829 | if (!lockerror) { |
b0d623f7 | 2830 | nfs_node_unlock(np); |
0a7de745 | 2831 | } |
2d21ac55 A |
2832 | nfsm_chain_cleanup(&nmreq); |
2833 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 2834 | return error; |
1c79356b A |
2835 | } |
2836 | ||
2837 | /* | |
2d21ac55 | 2838 | * NFS read RPC call |
1c79356b A |
2839 | * Ditto above |
2840 | */ | |
2841 | int | |
b0d623f7 | 2842 | nfs_read_rpc(nfsnode_t np, uio_t uio, vfs_context_t ctx) |
1c79356b | 2843 | { |
1c79356b | 2844 | struct nfsmount *nmp; |
2d21ac55 | 2845 | int error = 0, nfsvers, eof = 0; |
b0d623f7 A |
2846 | size_t nmrsize, len, retlen; |
2847 | user_ssize_t tsiz; | |
2d21ac55 A |
2848 | off_t txoffset; |
2849 | struct nfsreq rq, *req = &rq; | |
cb323159 | 2850 | #if CONFIG_NFS4 |
b0d623f7 | 2851 | uint32_t stategenid = 0, restart = 0; |
cb323159 | 2852 | #endif |
b0d623f7 | 2853 | FSDBG_TOP(536, np, uio_offset(uio), uio_resid(uio), 0); |
2d21ac55 | 2854 | nmp = NFSTONMP(np); |
0a7de745 A |
2855 | if (nfs_mount_gone(nmp)) { |
2856 | return ENXIO; | |
2857 | } | |
2d21ac55 | 2858 | nfsvers = nmp->nm_vers; |
55e303ae A |
2859 | nmrsize = nmp->nm_rsize; |
2860 | ||
b0d623f7 A |
2861 | txoffset = uio_offset(uio); |
2862 | tsiz = uio_resid(uio); | |
2863 | if ((nfsvers == NFS_VER2) && ((uint64_t)(txoffset + tsiz) > 0xffffffffULL)) { | |
2864 | FSDBG_BOT(536, np, uio_offset(uio), uio_resid(uio), EFBIG); | |
0a7de745 | 2865 | return EFBIG; |
55e303ae | 2866 | } |
2d21ac55 | 2867 | |
1c79356b | 2868 | while (tsiz > 0) { |
b0d623f7 | 2869 | len = retlen = (tsiz > (user_ssize_t)nmrsize) ? nmrsize : (size_t)tsiz; |
2d21ac55 | 2870 | FSDBG(536, np, txoffset, len, 0); |
6d2010ae A |
2871 | if (np->n_flag & NREVOKE) { |
2872 | error = EIO; | |
2873 | break; | |
2874 | } | |
cb323159 | 2875 | #if CONFIG_NFS4 |
0a7de745 | 2876 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 2877 | stategenid = nmp->nm_stategenid; |
0a7de745 | 2878 | } |
cb323159 | 2879 | #endif |
2d21ac55 | 2880 | error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, len, |
0a7de745 A |
2881 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req); |
2882 | if (!error) { | |
b0d623f7 | 2883 | error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req, uio, &retlen, &eof); |
0a7de745 | 2884 | } |
cb323159 | 2885 | #if CONFIG_NFS4 |
b0d623f7 A |
2886 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) && |
2887 | (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */ | |
2888 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
2889 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
2890 | NP(np, "nfs_read_rpc: error %d, initiating recovery", error); | |
2891 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
2892 | } |
2893 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae A |
2894 | if (np->n_flag & NREVOKE) { |
2895 | error = EIO; | |
2896 | } else { | |
0a7de745 A |
2897 | if (error == NFSERR_GRACE) { |
2898 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
2899 | } | |
2900 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 2901 | continue; |
0a7de745 | 2902 | } |
6d2010ae | 2903 | } |
b0d623f7 | 2904 | } |
cb323159 | 2905 | #endif |
0a7de745 | 2906 | if (error) { |
91447636 | 2907 | break; |
0a7de745 | 2908 | } |
2d21ac55 A |
2909 | txoffset += retlen; |
2910 | tsiz -= retlen; | |
2911 | if (nfsvers != NFS_VER2) { | |
0a7de745 | 2912 | if (eof || (retlen == 0)) { |
2d21ac55 | 2913 | tsiz = 0; |
0a7de745 A |
2914 | } |
2915 | } else if (retlen < len) { | |
2d21ac55 | 2916 | tsiz = 0; |
0a7de745 | 2917 | } |
2d21ac55 A |
2918 | } |
2919 | ||
b0d623f7 | 2920 | FSDBG_BOT(536, np, eof, uio_resid(uio), error); |
0a7de745 | 2921 | return error; |
2d21ac55 A |
2922 | } |
2923 | ||
2924 | int | |
2925 | nfs3_read_rpc_async( | |
2926 | nfsnode_t np, | |
2927 | off_t offset, | |
2928 | size_t len, | |
2929 | thread_t thd, | |
2930 | kauth_cred_t cred, | |
2931 | struct nfsreq_cbinfo *cb, | |
2932 | struct nfsreq **reqp) | |
2933 | { | |
2934 | struct nfsmount *nmp; | |
2935 | int error = 0, nfsvers; | |
2936 | struct nfsm_chain nmreq; | |
2937 | ||
2938 | nmp = NFSTONMP(np); | |
0a7de745 A |
2939 | if (nfs_mount_gone(nmp)) { |
2940 | return ENXIO; | |
2941 | } | |
2d21ac55 A |
2942 | nfsvers = nmp->nm_vers; |
2943 | ||
2944 | nfsm_chain_null(&nmreq); | |
2945 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers) + 3 * NFSX_UNSIGNED); | |
2946 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
2947 | if (nfsvers == NFS_VER3) { | |
2948 | nfsm_chain_add_64(error, &nmreq, offset); | |
2949 | nfsm_chain_add_32(error, &nmreq, len); | |
2950 | } else { | |
2951 | nfsm_chain_add_32(error, &nmreq, offset); | |
2952 | nfsm_chain_add_32(error, &nmreq, len); | |
2953 | nfsm_chain_add_32(error, &nmreq, 0); | |
2954 | } | |
2955 | nfsm_chain_build_done(error, &nmreq); | |
2956 | nfsmout_if(error); | |
6d2010ae | 2957 | error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, NULL, 0, cb, reqp); |
2d21ac55 A |
2958 | nfsmout: |
2959 | nfsm_chain_cleanup(&nmreq); | |
0a7de745 | 2960 | return error; |
2d21ac55 A |
2961 | } |
2962 | ||
2963 | int | |
2964 | nfs3_read_rpc_async_finish( | |
2965 | nfsnode_t np, | |
2966 | struct nfsreq *req, | |
b0d623f7 | 2967 | uio_t uio, |
2d21ac55 A |
2968 | size_t *lenp, |
2969 | int *eofp) | |
2970 | { | |
2971 | int error = 0, lockerror, nfsvers, status, eof = 0; | |
2972 | size_t retlen = 0; | |
2973 | uint64_t xid; | |
2974 | struct nfsmount *nmp; | |
2975 | struct nfsm_chain nmrep; | |
2976 | ||
2977 | nmp = NFSTONMP(np); | |
fe8ab488 | 2978 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 2979 | nfs_request_async_cancel(req); |
0a7de745 | 2980 | return ENXIO; |
2d21ac55 A |
2981 | } |
2982 | nfsvers = nmp->nm_vers; | |
2983 | ||
2984 | nfsm_chain_null(&nmrep); | |
2985 | ||
2986 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
0a7de745 A |
2987 | if (error == EINPROGRESS) { /* async request restarted */ |
2988 | return error; | |
2989 | } | |
2d21ac55 | 2990 | |
0a7de745 | 2991 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 2992 | error = lockerror; |
0a7de745 A |
2993 | } |
2994 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 2995 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 A |
2996 | } |
2997 | if (!error) { | |
2d21ac55 | 2998 | error = status; |
0a7de745 | 2999 | } |
2d21ac55 A |
3000 | if (nfsvers == NFS_VER3) { |
3001 | nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); | |
3002 | nfsm_chain_get_32(error, &nmrep, eof); | |
3003 | } else { | |
6d2010ae | 3004 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
2d21ac55 | 3005 | } |
0a7de745 | 3006 | if (!lockerror) { |
b0d623f7 | 3007 | nfs_node_unlock(np); |
0a7de745 | 3008 | } |
2d21ac55 | 3009 | nfsm_chain_get_32(error, &nmrep, retlen); |
0a7de745 | 3010 | if ((nfsvers == NFS_VER2) && (retlen > *lenp)) { |
2d21ac55 | 3011 | error = EBADRPC; |
0a7de745 | 3012 | } |
2d21ac55 | 3013 | nfsmout_if(error); |
b0d623f7 | 3014 | error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uio); |
2d21ac55 A |
3015 | if (eofp) { |
3016 | if (nfsvers == NFS_VER3) { | |
0a7de745 | 3017 | if (!eof && !retlen) { |
2d21ac55 | 3018 | eof = 1; |
0a7de745 | 3019 | } |
2d21ac55 A |
3020 | } else if (retlen < *lenp) { |
3021 | eof = 1; | |
1c79356b | 3022 | } |
2d21ac55 A |
3023 | *eofp = eof; |
3024 | } | |
3025 | *lenp = MIN(retlen, *lenp); | |
3026 | nfsmout: | |
3027 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 3028 | return error; |
2d21ac55 A |
3029 | } |
3030 | ||
3031 | /* | |
3032 | * NFS write call | |
3033 | */ | |
3034 | int | |
3035 | nfs_vnop_write( | |
3036 | struct vnop_write_args /* { | |
0a7de745 A |
3037 | * struct vnodeop_desc *a_desc; |
3038 | * vnode_t a_vp; | |
3039 | * struct uio *a_uio; | |
3040 | * int a_ioflag; | |
3041 | * vfs_context_t a_context; | |
3042 | * } */*ap) | |
2d21ac55 A |
3043 | { |
3044 | vfs_context_t ctx = ap->a_context; | |
b0d623f7 | 3045 | uio_t uio = ap->a_uio; |
2d21ac55 A |
3046 | vnode_t vp = ap->a_vp; |
3047 | nfsnode_t np = VTONFS(vp); | |
3048 | int ioflag = ap->a_ioflag; | |
3049 | struct nfsbuf *bp; | |
2d21ac55 A |
3050 | struct nfsmount *nmp = VTONMP(vp); |
3051 | daddr64_t lbn; | |
3052 | int biosize; | |
3053 | int n, on, error = 0; | |
3054 | off_t boff, start, end; | |
b0d623f7 | 3055 | uio_t auio; |
0a7de745 | 3056 | char auio_buf[UIO_SIZEOF(1)]; |
2d21ac55 A |
3057 | thread_t thd; |
3058 | kauth_cred_t cred; | |
3059 | ||
b0d623f7 | 3060 | FSDBG_TOP(515, np, uio_offset(uio), uio_resid(uio), ioflag); |
2d21ac55 A |
3061 | |
3062 | if (vnode_vtype(vp) != VREG) { | |
b0d623f7 | 3063 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), EIO); |
0a7de745 | 3064 | return EIO; |
2d21ac55 A |
3065 | } |
3066 | ||
3067 | thd = vfs_context_thread(ctx); | |
3068 | cred = vfs_context_ucred(ctx); | |
3069 | ||
b0d623f7 | 3070 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
2d21ac55 | 3071 | |
b0d623f7 | 3072 | if ((error = nfs_node_lock(np))) { |
2d21ac55 | 3073 | nfs_data_unlock(np); |
b0d623f7 | 3074 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error); |
0a7de745 | 3075 | return error; |
2d21ac55 A |
3076 | } |
3077 | np->n_wrbusy++; | |
3078 | ||
3079 | if (np->n_flag & NWRITEERR) { | |
3080 | error = np->n_error; | |
3081 | np->n_flag &= ~NWRITEERR; | |
3082 | } | |
3083 | if (np->n_flag & NNEEDINVALIDATE) { | |
3084 | np->n_flag &= ~NNEEDINVALIDATE; | |
b0d623f7 | 3085 | nfs_node_unlock(np); |
2d21ac55 | 3086 | nfs_data_unlock(np); |
0a7de745 | 3087 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
b0d623f7 A |
3088 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
3089 | } else { | |
3090 | nfs_node_unlock(np); | |
2d21ac55 | 3091 | } |
0a7de745 | 3092 | if (error) { |
2d21ac55 | 3093 | goto out; |
0a7de745 | 3094 | } |
2d21ac55 A |
3095 | |
3096 | biosize = nmp->nm_biosize; | |
3097 | ||
3098 | if (ioflag & (IO_APPEND | IO_SYNC)) { | |
b0d623f7 | 3099 | nfs_node_lock_force(np); |
2d21ac55 A |
3100 | if (np->n_flag & NMODIFIED) { |
3101 | NATTRINVALIDATE(np); | |
b0d623f7 | 3102 | nfs_node_unlock(np); |
2d21ac55 A |
3103 | nfs_data_unlock(np); |
3104 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); | |
b0d623f7 A |
3105 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
3106 | if (error) { | |
3107 | FSDBG(515, np, uio_offset(uio), 0x10bad01, error); | |
2d21ac55 | 3108 | goto out; |
e5568f75 | 3109 | } |
b0d623f7 A |
3110 | } else { |
3111 | nfs_node_unlock(np); | |
2d21ac55 A |
3112 | } |
3113 | if (ioflag & IO_APPEND) { | |
2d21ac55 | 3114 | nfs_data_unlock(np); |
b0d623f7 | 3115 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae | 3116 | error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED); |
2d21ac55 | 3117 | /* we'll be extending the file, so take the data lock exclusive */ |
b0d623f7 A |
3118 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
3119 | if (error) { | |
3120 | FSDBG(515, np, uio_offset(uio), 0x10bad02, error); | |
2d21ac55 A |
3121 | goto out; |
3122 | } | |
b0d623f7 | 3123 | uio_setoffset(uio, np->n_size); |
2d21ac55 A |
3124 | } |
3125 | } | |
b0d623f7 | 3126 | if (uio_offset(uio) < 0) { |
2d21ac55 | 3127 | error = EINVAL; |
b0d623f7 | 3128 | FSDBG_BOT(515, np, uio_offset(uio), 0xbad0ff, error); |
2d21ac55 A |
3129 | goto out; |
3130 | } | |
0a7de745 | 3131 | if (uio_resid(uio) == 0) { |
2d21ac55 | 3132 | goto out; |
0a7de745 | 3133 | } |
2d21ac55 | 3134 | |
b0d623f7 | 3135 | if (((uio_offset(uio) + uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) { |
fe8ab488 A |
3136 | /* |
3137 | * It looks like we'll be extending the file, so take the data lock exclusive. | |
3138 | */ | |
2d21ac55 | 3139 | nfs_data_unlock(np); |
b0d623f7 | 3140 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
fe8ab488 A |
3141 | |
3142 | /* | |
3143 | * Also, if the write begins after the previous EOF buffer, make sure to zero | |
3144 | * and validate the new bytes in that buffer. | |
3145 | */ | |
3146 | struct nfsbuf *eofbp = NULL; | |
3147 | daddr64_t eofbn = np->n_size / biosize; | |
3148 | int eofoff = np->n_size % biosize; | |
3149 | lbn = uio_offset(uio) / biosize; | |
3150 | ||
3151 | if (eofoff && (eofbn < lbn)) { | |
0a7de745 | 3152 | if ((error = nfs_buf_get(np, eofbn, biosize, thd, NBLK_WRITE | NBLK_ONLYVALID, &eofbp))) { |
fe8ab488 | 3153 | goto out; |
0a7de745 | 3154 | } |
fe8ab488 A |
3155 | np->n_size += (biosize - eofoff); |
3156 | nfs_node_lock_force(np); | |
3157 | CLR(np->n_flag, NUPDATESIZE); | |
3158 | np->n_flag |= NMODIFIED; | |
3159 | nfs_node_unlock(np); | |
3160 | FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); | |
3161 | ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */ | |
3162 | if (eofbp) { | |
3163 | /* | |
3164 | * For the old last page, don't zero bytes if there | |
3165 | * are invalid bytes in that page (i.e. the page isn't | |
3166 | * currently valid). | |
3167 | * For pages after the old last page, zero them and | |
3168 | * mark them as valid. | |
3169 | */ | |
3170 | char *d; | |
3171 | int i; | |
0a7de745 | 3172 | if (ioflag & IO_NOCACHE) { |
fe8ab488 | 3173 | SET(eofbp->nb_flags, NB_NOCACHE); |
0a7de745 | 3174 | } |
fe8ab488 A |
3175 | NFS_BUF_MAP(eofbp); |
3176 | FSDBG(516, eofbp, eofoff, biosize - eofoff, 0xe0fff01e); | |
3177 | d = eofbp->nb_data; | |
0a7de745 | 3178 | i = eofoff / PAGE_SIZE; |
fe8ab488 A |
3179 | while (eofoff < biosize) { |
3180 | int poff = eofoff & PAGE_MASK; | |
0a7de745 | 3181 | if (!poff || NBPGVALID(eofbp, i)) { |
fe8ab488 A |
3182 | bzero(d + eofoff, PAGE_SIZE - poff); |
3183 | NBPGVALID_SET(eofbp, i); | |
3184 | } | |
3185 | eofoff += PAGE_SIZE - poff; | |
3186 | i++; | |
3187 | } | |
3188 | nfs_buf_release(eofbp, 1); | |
3189 | } | |
3190 | } | |
2d21ac55 A |
3191 | } |
3192 | ||
3193 | do { | |
316670eb | 3194 | OSAddAtomic64(1, &nfsstats.biocache_writes); |
b0d623f7 A |
3195 | lbn = uio_offset(uio) / biosize; |
3196 | on = uio_offset(uio) % biosize; | |
3197 | n = biosize - on; | |
0a7de745 | 3198 | if (uio_resid(uio) < n) { |
b0d623f7 | 3199 | n = uio_resid(uio); |
0a7de745 | 3200 | } |
2d21ac55 A |
3201 | again: |
3202 | /* | |
3203 | * Get a cache block for writing. The range to be written is | |
3204 | * (off..off+n) within the block. We ensure that the block | |
3205 | * either has no dirty region or that the given range is | |
3206 | * contiguous with the existing dirty region. | |
3207 | */ | |
3208 | error = nfs_buf_get(np, lbn, biosize, thd, NBLK_WRITE, &bp); | |
0a7de745 | 3209 | if (error) { |
2d21ac55 | 3210 | goto out; |
0a7de745 | 3211 | } |
2d21ac55 A |
3212 | /* map the block because we know we're going to write to it */ |
3213 | NFS_BUF_MAP(bp); | |
3214 | ||
0a7de745 | 3215 | if (ioflag & IO_NOCACHE) { |
2d21ac55 | 3216 | SET(bp->nb_flags, NB_NOCACHE); |
0a7de745 | 3217 | } |
2d21ac55 A |
3218 | |
3219 | if (!IS_VALID_CRED(bp->nb_wcred)) { | |
3220 | kauth_cred_ref(cred); | |
3221 | bp->nb_wcred = cred; | |
3222 | } | |
3223 | ||
3224 | /* | |
3225 | * If there's already a dirty range AND dirty pages in this block we | |
3226 | * need to send a commit AND write the dirty pages before continuing. | |
3227 | * | |
3228 | * If there's already a dirty range OR dirty pages in this block | |
3229 | * and the new write range is not contiguous with the existing range, | |
3230 | * then force the buffer to be written out now. | |
3231 | * (We used to just extend the dirty range to cover the valid, | |
3232 | * but unwritten, data in between also. But writing ranges | |
3233 | * of data that weren't actually written by an application | |
3234 | * risks overwriting some other client's data with stale data | |
3235 | * that's just masquerading as new written data.) | |
3236 | */ | |
3237 | if (bp->nb_dirtyend > 0) { | |
0a7de745 A |
3238 | if (on > bp->nb_dirtyend || (on + n) < bp->nb_dirtyoff || bp->nb_dirty) { |
3239 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c001); | |
3240 | /* write/commit buffer "synchronously" */ | |
3241 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
3242 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL)); | |
3243 | SET(bp->nb_flags, (NB_ASYNC | NB_STABLE)); | |
3244 | error = nfs_buf_write(bp); | |
3245 | if (error) { | |
3246 | goto out; | |
3247 | } | |
3248 | goto again; | |
3249 | } | |
2d21ac55 | 3250 | } else if (bp->nb_dirty) { |
0a7de745 A |
3251 | int firstpg, lastpg; |
3252 | u_int32_t pagemask; | |
3253 | /* calculate write range pagemask */ | |
3254 | firstpg = on / PAGE_SIZE; | |
3255 | lastpg = (on + n - 1) / PAGE_SIZE; | |
3256 | pagemask = ((1 << (lastpg + 1)) - 1) & ~((1 << firstpg) - 1); | |
3257 | /* check if there are dirty pages outside the write range */ | |
3258 | if (bp->nb_dirty & ~pagemask) { | |
3259 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c002); | |
3260 | /* write/commit buffer "synchronously" */ | |
3261 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
3262 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL)); | |
3263 | SET(bp->nb_flags, (NB_ASYNC | NB_STABLE)); | |
3264 | error = nfs_buf_write(bp); | |
3265 | if (error) { | |
3266 | goto out; | |
3267 | } | |
3268 | goto again; | |
3269 | } | |
3270 | /* if the first or last pages are already dirty */ | |
3271 | /* make sure that the dirty range encompasses those pages */ | |
3272 | if (NBPGDIRTY(bp, firstpg) || NBPGDIRTY(bp, lastpg)) { | |
3273 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c003); | |
3274 | bp->nb_dirtyoff = min(on, firstpg * PAGE_SIZE); | |
3275 | if (NBPGDIRTY(bp, lastpg)) { | |
3276 | bp->nb_dirtyend = (lastpg + 1) * PAGE_SIZE; | |
3277 | /* clip to EOF */ | |
3278 | if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) { | |
3279 | bp->nb_dirtyend = np->n_size - NBOFF(bp); | |
3280 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) { | |
3281 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; | |
3282 | } | |
3283 | } | |
3284 | } else { | |
3285 | bp->nb_dirtyend = on + n; | |
3286 | } | |
3287 | } | |
2d21ac55 A |
3288 | } |
3289 | ||
3290 | /* | |
3291 | * Are we extending the size of the file with this write? | |
3292 | * If so, update file size now that we have the block. | |
3293 | * If there was a partial buf at the old eof, validate | |
3294 | * and zero the new bytes. | |
3295 | */ | |
b0d623f7 | 3296 | if ((uio_offset(uio) + n) > (off_t)np->n_size) { |
2d21ac55 | 3297 | daddr64_t eofbn = np->n_size / biosize; |
b0d623f7 | 3298 | int neweofoff = (uio_offset(uio) + n) % biosize; |
2d21ac55 | 3299 | |
b0d623f7 | 3300 | FSDBG(515, 0xb1ffa000, uio_offset(uio) + n, eofoff, neweofoff); |
2d21ac55 | 3301 | |
2d21ac55 A |
3302 | /* if we're extending within the same last block */ |
3303 | /* and the block is flagged as being cached... */ | |
3304 | if ((lbn == eofbn) && ISSET(bp->nb_flags, NB_CACHE)) { | |
3305 | /* ...check that all pages in buffer are valid */ | |
0a7de745 | 3306 | int endpg = ((neweofoff ? neweofoff : biosize) - 1) / PAGE_SIZE; |
2d21ac55 A |
3307 | u_int32_t pagemask; |
3308 | /* pagemask only has to extend to last page being written to */ | |
0a7de745 | 3309 | pagemask = (1 << (endpg + 1)) - 1; |
2d21ac55 A |
3310 | FSDBG(515, 0xb1ffa001, bp->nb_valid, pagemask, 0); |
3311 | if ((bp->nb_valid & pagemask) != pagemask) { | |
3312 | /* zerofill any hole */ | |
3313 | if (on > bp->nb_validend) { | |
3314 | int i; | |
0a7de745 | 3315 | for (i = bp->nb_validend / PAGE_SIZE; i <= (on - 1) / PAGE_SIZE; i++) { |
2d21ac55 | 3316 | NBPGVALID_SET(bp, i); |
0a7de745 | 3317 | } |
2d21ac55 A |
3318 | NFS_BUF_MAP(bp); |
3319 | FSDBG(516, bp, bp->nb_validend, on - bp->nb_validend, 0xf01e); | |
3320 | bzero((char *)bp->nb_data + bp->nb_validend, | |
0a7de745 | 3321 | on - bp->nb_validend); |
2d21ac55 A |
3322 | } |
3323 | /* zerofill any trailing data in the last page */ | |
3324 | if (neweofoff) { | |
3325 | NFS_BUF_MAP(bp); | |
3326 | FSDBG(516, bp, neweofoff, PAGE_SIZE - (neweofoff & PAGE_MASK), 0xe0f); | |
3327 | bzero((char *)bp->nb_data + neweofoff, | |
0a7de745 | 3328 | PAGE_SIZE - (neweofoff & PAGE_MASK)); |
2d21ac55 A |
3329 | } |
3330 | } | |
3331 | } | |
b0d623f7 A |
3332 | np->n_size = uio_offset(uio) + n; |
3333 | nfs_node_lock_force(np); | |
2d21ac55 A |
3334 | CLR(np->n_flag, NUPDATESIZE); |
3335 | np->n_flag |= NMODIFIED; | |
b0d623f7 | 3336 | nfs_node_unlock(np); |
2d21ac55 A |
3337 | FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); |
3338 | ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */ | |
2d21ac55 A |
3339 | } |
3340 | /* | |
3341 | * If dirtyend exceeds file size, chop it down. This should | |
3342 | * not occur unless there is a race. | |
3343 | */ | |
3344 | if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) { | |
3345 | bp->nb_dirtyend = np->n_size - NBOFF(bp); | |
0a7de745 | 3346 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) { |
2d21ac55 | 3347 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; |
0a7de745 | 3348 | } |
2d21ac55 A |
3349 | } |
3350 | /* | |
3351 | * UBC doesn't handle partial pages, so we need to make sure | |
3352 | * that any pages left in the page cache are completely valid. | |
3353 | * | |
3354 | * Writes that are smaller than a block are delayed if they | |
3355 | * don't extend to the end of the block. | |
3356 | * | |
3357 | * If the block isn't (completely) cached, we may need to read | |
3358 | * in some parts of pages that aren't covered by the write. | |
3359 | * If the write offset (on) isn't page aligned, we'll need to | |
3360 | * read the start of the first page being written to. Likewise, | |
3361 | * if the offset of the end of the write (on+n) isn't page aligned, | |
3362 | * we'll need to read the end of the last page being written to. | |
3363 | * | |
3364 | * Notes: | |
3365 | * We don't want to read anything we're just going to write over. | |
6d2010ae A |
3366 | * We don't want to read anything we're just going drop when the |
3367 | * I/O is complete (i.e. don't do reads for NOCACHE requests). | |
2d21ac55 A |
3368 | * We don't want to issue multiple I/Os if we don't have to |
3369 | * (because they're synchronous rpcs). | |
3370 | * We don't want to read anything we already have modified in the | |
3371 | * page cache. | |
3372 | */ | |
6d2010ae | 3373 | if (!ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) { |
2d21ac55 A |
3374 | int firstpg, lastpg, dirtypg; |
3375 | int firstpgoff, lastpgoff; | |
3376 | start = end = -1; | |
0a7de745 | 3377 | firstpg = on / PAGE_SIZE; |
2d21ac55 | 3378 | firstpgoff = on & PAGE_MASK; |
0a7de745 A |
3379 | lastpg = (on + n - 1) / PAGE_SIZE; |
3380 | lastpgoff = (on + n) & PAGE_MASK; | |
3381 | if (firstpgoff && !NBPGVALID(bp, firstpg)) { | |
2d21ac55 A |
3382 | /* need to read start of first page */ |
3383 | start = firstpg * PAGE_SIZE; | |
3384 | end = start + firstpgoff; | |
3385 | } | |
0a7de745 | 3386 | if (lastpgoff && !NBPGVALID(bp, lastpg)) { |
2d21ac55 | 3387 | /* need to read end of last page */ |
0a7de745 | 3388 | if (start < 0) { |
2d21ac55 | 3389 | start = (lastpg * PAGE_SIZE) + lastpgoff; |
0a7de745 | 3390 | } |
2d21ac55 A |
3391 | end = (lastpg + 1) * PAGE_SIZE; |
3392 | } | |
6d2010ae A |
3393 | if (ISSET(bp->nb_flags, NB_NOCACHE)) { |
3394 | /* | |
3395 | * For nocache writes, if there is any partial page at the | |
3396 | * start or end of the write range, then we do the write | |
3397 | * synchronously to make sure that we can drop the data | |
3398 | * from the cache as soon as the WRITE finishes. Normally, | |
3399 | * we would do an unstable write and not drop the data until | |
3400 | * it was committed. But doing that here would risk allowing | |
3401 | * invalid data to be read from the cache between the WRITE | |
3402 | * and the COMMIT. | |
3403 | * (NB_STABLE indicates that data writes should be FILESYNC) | |
3404 | */ | |
0a7de745 | 3405 | if (end > start) { |
6d2010ae | 3406 | SET(bp->nb_flags, NB_STABLE); |
0a7de745 | 3407 | } |
6d2010ae A |
3408 | goto skipread; |
3409 | } | |
2d21ac55 A |
3410 | if (end > start) { |
3411 | /* need to read the data in range: start...end-1 */ | |
3412 | ||
3413 | /* first, check for dirty pages in between */ | |
3414 | /* if there are, we'll have to do two reads because */ | |
3415 | /* we don't want to overwrite the dirty pages. */ | |
0a7de745 A |
3416 | for (dirtypg = start / PAGE_SIZE; dirtypg <= (end - 1) / PAGE_SIZE; dirtypg++) { |
3417 | if (NBPGDIRTY(bp, dirtypg)) { | |
2d21ac55 | 3418 | break; |
0a7de745 A |
3419 | } |
3420 | } | |
2d21ac55 A |
3421 | |
3422 | /* if start is at beginning of page, try */ | |
3423 | /* to get any preceeding pages as well. */ | |
3424 | if (!(start & PAGE_MASK)) { | |
3425 | /* stop at next dirty/valid page or start of block */ | |
0a7de745 A |
3426 | for (; start > 0; start -= PAGE_SIZE) { |
3427 | if (NBPGVALID(bp, ((start - 1) / PAGE_SIZE))) { | |
2d21ac55 | 3428 | break; |
0a7de745 A |
3429 | } |
3430 | } | |
2d21ac55 A |
3431 | } |
3432 | ||
3433 | NFS_BUF_MAP(bp); | |
3434 | /* setup uio for read(s) */ | |
3435 | boff = NBOFF(bp); | |
b0d623f7 | 3436 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, |
0a7de745 | 3437 | &auio_buf, sizeof(auio_buf)); |
2d21ac55 | 3438 | |
0a7de745 | 3439 | if (dirtypg <= (end - 1) / PAGE_SIZE) { |
2d21ac55 A |
3440 | /* there's a dirty page in the way, so just do two reads */ |
3441 | /* we'll read the preceding data here */ | |
b0d623f7 A |
3442 | uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ); |
3443 | uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), on - start); | |
3444 | error = nfs_read_rpc(np, auio, ctx); | |
6d2010ae A |
3445 | if (error) { |
3446 | /* couldn't read the data, so treat buffer as synchronous NOCACHE */ | |
0a7de745 | 3447 | SET(bp->nb_flags, (NB_NOCACHE | NB_STABLE)); |
6d2010ae A |
3448 | goto skipread; |
3449 | } | |
b0d623f7 A |
3450 | if (uio_resid(auio) > 0) { |
3451 | FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee01); | |
3452 | bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio)); | |
2d21ac55 A |
3453 | } |
3454 | if (!error) { | |
3455 | /* update validoff/validend if necessary */ | |
0a7de745 | 3456 | if ((bp->nb_validoff < 0) || (bp->nb_validoff > start)) { |
2d21ac55 | 3457 | bp->nb_validoff = start; |
0a7de745 A |
3458 | } |
3459 | if ((bp->nb_validend < 0) || (bp->nb_validend < on)) { | |
2d21ac55 | 3460 | bp->nb_validend = on; |
0a7de745 A |
3461 | } |
3462 | if ((off_t)np->n_size > boff + bp->nb_validend) { | |
2d21ac55 | 3463 | bp->nb_validend = min(np->n_size - (boff + start), biosize); |
0a7de745 | 3464 | } |
2d21ac55 | 3465 | /* validate any pages before the write offset */ |
0a7de745 A |
3466 | for (; start < on / PAGE_SIZE; start += PAGE_SIZE) { |
3467 | NBPGVALID_SET(bp, start / PAGE_SIZE); | |
3468 | } | |
2d21ac55 A |
3469 | } |
3470 | /* adjust start to read any trailing data */ | |
0a7de745 | 3471 | start = on + n; |
2d21ac55 A |
3472 | } |
3473 | ||
3474 | /* if end is at end of page, try to */ | |
3475 | /* get any following pages as well. */ | |
3476 | if (!(end & PAGE_MASK)) { | |
3477 | /* stop at next valid page or end of block */ | |
0a7de745 A |
3478 | for (; end < biosize; end += PAGE_SIZE) { |
3479 | if (NBPGVALID(bp, end / PAGE_SIZE)) { | |
2d21ac55 | 3480 | break; |
0a7de745 A |
3481 | } |
3482 | } | |
2d21ac55 A |
3483 | } |
3484 | ||
0a7de745 | 3485 | if (((boff + start) >= (off_t)np->n_size) || |
2d21ac55 A |
3486 | ((start >= on) && ((boff + on + n) >= (off_t)np->n_size))) { |
3487 | /* | |
3488 | * Either this entire read is beyond the current EOF | |
3489 | * or the range that we won't be modifying (on+n...end) | |
3490 | * is all beyond the current EOF. | |
3491 | * No need to make a trip across the network to | |
3492 | * read nothing. So, just zero the buffer instead. | |
3493 | */ | |
3494 | FSDBG(516, bp, start, end - start, 0xd00dee00); | |
3495 | bzero(bp->nb_data + start, end - start); | |
3496 | error = 0; | |
6d2010ae | 3497 | } else { |
2d21ac55 | 3498 | /* now we'll read the (rest of the) data */ |
b0d623f7 A |
3499 | uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ); |
3500 | uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), end - start); | |
3501 | error = nfs_read_rpc(np, auio, ctx); | |
6d2010ae A |
3502 | if (error) { |
3503 | /* couldn't read the data, so treat buffer as synchronous NOCACHE */ | |
0a7de745 | 3504 | SET(bp->nb_flags, (NB_NOCACHE | NB_STABLE)); |
6d2010ae A |
3505 | goto skipread; |
3506 | } | |
b0d623f7 A |
3507 | if (uio_resid(auio) > 0) { |
3508 | FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee02); | |
3509 | bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio)); | |
2d21ac55 A |
3510 | } |
3511 | } | |
3512 | if (!error) { | |
3513 | /* update validoff/validend if necessary */ | |
0a7de745 | 3514 | if ((bp->nb_validoff < 0) || (bp->nb_validoff > start)) { |
2d21ac55 | 3515 | bp->nb_validoff = start; |
0a7de745 A |
3516 | } |
3517 | if ((bp->nb_validend < 0) || (bp->nb_validend < end)) { | |
2d21ac55 | 3518 | bp->nb_validend = end; |
0a7de745 A |
3519 | } |
3520 | if ((off_t)np->n_size > boff + bp->nb_validend) { | |
2d21ac55 | 3521 | bp->nb_validend = min(np->n_size - (boff + start), biosize); |
0a7de745 | 3522 | } |
2d21ac55 | 3523 | /* validate any pages before the write offset's page */ |
0a7de745 A |
3524 | for (; start < (off_t)trunc_page_32(on); start += PAGE_SIZE) { |
3525 | NBPGVALID_SET(bp, start / PAGE_SIZE); | |
3526 | } | |
2d21ac55 | 3527 | /* validate any pages after the range of pages being written to */ |
0a7de745 A |
3528 | for (; (end - 1) > (off_t)round_page_32(on + n - 1); end -= PAGE_SIZE) { |
3529 | NBPGVALID_SET(bp, (end - 1) / PAGE_SIZE); | |
3530 | } | |
2d21ac55 A |
3531 | } |
3532 | /* Note: pages being written to will be validated when written */ | |
3533 | } | |
3534 | } | |
6d2010ae | 3535 | skipread: |
2d21ac55 A |
3536 | |
3537 | if (ISSET(bp->nb_flags, NB_ERROR)) { | |
3538 | error = bp->nb_error; | |
3539 | nfs_buf_release(bp, 1); | |
3540 | goto out; | |
3541 | } | |
3542 | ||
b0d623f7 | 3543 | nfs_node_lock_force(np); |
2d21ac55 | 3544 | np->n_flag |= NMODIFIED; |
b0d623f7 | 3545 | nfs_node_unlock(np); |
2d21ac55 A |
3546 | |
3547 | NFS_BUF_MAP(bp); | |
3548 | error = uiomove((char *)bp->nb_data + on, n, uio); | |
3549 | if (error) { | |
3550 | SET(bp->nb_flags, NB_ERROR); | |
3551 | nfs_buf_release(bp, 1); | |
3552 | goto out; | |
3553 | } | |
3554 | ||
3555 | /* validate any pages written to */ | |
3556 | start = on & ~PAGE_MASK; | |
0a7de745 A |
3557 | for (; start < on + n; start += PAGE_SIZE) { |
3558 | NBPGVALID_SET(bp, start / PAGE_SIZE); | |
2d21ac55 A |
3559 | /* |
3560 | * This may seem a little weird, but we don't actually set the | |
3561 | * dirty bits for writes. This is because we keep the dirty range | |
3562 | * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for | |
3563 | * delayed writes, when we give the pages back to the VM we don't | |
3564 | * want to keep them marked dirty, because when we later write the | |
3565 | * buffer we won't be able to tell which pages were written dirty | |
3566 | * and which pages were mmapped and dirtied. | |
3567 | */ | |
3568 | } | |
3569 | if (bp->nb_dirtyend > 0) { | |
3570 | bp->nb_dirtyoff = min(on, bp->nb_dirtyoff); | |
3571 | bp->nb_dirtyend = max((on + n), bp->nb_dirtyend); | |
3572 | } else { | |
3573 | bp->nb_dirtyoff = on; | |
3574 | bp->nb_dirtyend = on + n; | |
3575 | } | |
3576 | if (bp->nb_validend <= 0 || bp->nb_validend < bp->nb_dirtyoff || | |
3577 | bp->nb_validoff > bp->nb_dirtyend) { | |
3578 | bp->nb_validoff = bp->nb_dirtyoff; | |
3579 | bp->nb_validend = bp->nb_dirtyend; | |
e5568f75 | 3580 | } else { |
2d21ac55 A |
3581 | bp->nb_validoff = min(bp->nb_validoff, bp->nb_dirtyoff); |
3582 | bp->nb_validend = max(bp->nb_validend, bp->nb_dirtyend); | |
3583 | } | |
0a7de745 | 3584 | if (!ISSET(bp->nb_flags, NB_CACHE)) { |
2d21ac55 | 3585 | nfs_buf_normalize_valid_range(np, bp); |
0a7de745 | 3586 | } |
2d21ac55 A |
3587 | |
3588 | /* | |
3589 | * Since this block is being modified, it must be written | |
3590 | * again and not just committed. | |
3591 | */ | |
3592 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { | |
b0d623f7 | 3593 | nfs_node_lock_force(np); |
2d21ac55 A |
3594 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { |
3595 | np->n_needcommitcnt--; | |
3596 | CHECK_NEEDCOMMITCNT(np); | |
e5568f75 | 3597 | } |
2d21ac55 | 3598 | CLR(bp->nb_flags, NB_NEEDCOMMIT); |
b0d623f7 | 3599 | nfs_node_unlock(np); |
e5568f75 | 3600 | } |
2d21ac55 A |
3601 | |
3602 | if (ioflag & IO_SYNC) { | |
3603 | error = nfs_buf_write(bp); | |
0a7de745 | 3604 | if (error) { |
2d21ac55 | 3605 | goto out; |
0a7de745 | 3606 | } |
b0d623f7 | 3607 | } else if (((n + on) == biosize) || (ioflag & IO_APPEND) || |
0a7de745 | 3608 | (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) { |
2d21ac55 A |
3609 | SET(bp->nb_flags, NB_ASYNC); |
3610 | error = nfs_buf_write(bp); | |
0a7de745 | 3611 | if (error) { |
2d21ac55 | 3612 | goto out; |
0a7de745 | 3613 | } |
e5568f75 | 3614 | } else { |
2d21ac55 A |
3615 | /* If the block wasn't already delayed: charge for the write */ |
3616 | if (!ISSET(bp->nb_flags, NB_DELWRI)) { | |
3617 | proc_t p = vfs_context_proc(ctx); | |
0a7de745 | 3618 | if (p && p->p_stats) { |
b0d623f7 | 3619 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); |
0a7de745 | 3620 | } |
2d21ac55 A |
3621 | } |
3622 | nfs_buf_write_delayed(bp); | |
e5568f75 | 3623 | } |
cb323159 A |
3624 | |
3625 | ||
0a7de745 A |
3626 | if (np->n_needcommitcnt >= NFS_A_LOT_OF_NEEDCOMMITS) { |
3627 | nfs_flushcommits(np, 1); | |
3628 | } | |
b0d623f7 | 3629 | } while (uio_resid(uio) > 0 && n > 0); |
2d21ac55 A |
3630 | |
3631 | out: | |
b0d623f7 | 3632 | nfs_node_lock_force(np); |
2d21ac55 | 3633 | np->n_wrbusy--; |
b0d623f7 | 3634 | nfs_node_unlock(np); |
2d21ac55 | 3635 | nfs_data_unlock(np); |
b0d623f7 | 3636 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error); |
0a7de745 | 3637 | return error; |
1c79356b A |
3638 | } |
3639 | ||
2d21ac55 | 3640 | |
1c79356b | 3641 | /* |
2d21ac55 | 3642 | * NFS write call |
1c79356b A |
3643 | */ |
3644 | int | |
2d21ac55 A |
3645 | nfs_write_rpc( |
3646 | nfsnode_t np, | |
b0d623f7 | 3647 | uio_t uio, |
2d21ac55 A |
3648 | vfs_context_t ctx, |
3649 | int *iomodep, | |
3650 | uint64_t *wverfp) | |
3651 | { | |
b0d623f7 | 3652 | return nfs_write_rpc2(np, uio, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp); |
2d21ac55 A |
3653 | } |
3654 | ||
3655 | int | |
3656 | nfs_write_rpc2( | |
3657 | nfsnode_t np, | |
b0d623f7 | 3658 | uio_t uio, |
2d21ac55 | 3659 | thread_t thd, |
91447636 | 3660 | kauth_cred_t cred, |
2d21ac55 | 3661 | int *iomodep, |
8f6c56a5 | 3662 | uint64_t *wverfp) |
1c79356b | 3663 | { |
55e303ae | 3664 | struct nfsmount *nmp; |
b0d623f7 | 3665 | int error = 0, nfsvers; |
39236c6e | 3666 | int wverfset, commit, committed; |
2d21ac55 A |
3667 | uint64_t wverf = 0, wverf2; |
3668 | size_t nmwsize, totalsize, tsiz, len, rlen; | |
3669 | struct nfsreq rq, *req = &rq; | |
cb323159 A |
3670 | #if CONFIG_NFS4 |
3671 | uint32_t stategenid = 0, restart = 0; | |
3672 | #endif | |
3673 | uint32_t vrestart = 0; | |
39236c6e | 3674 | uio_t uio_save = NULL; |
1c79356b A |
3675 | |
3676 | #if DIAGNOSTIC | |
2d21ac55 | 3677 | /* XXX limitation based on need to back up uio on short write */ |
0a7de745 | 3678 | if (uio_iovcnt(uio) != 1) { |
2d21ac55 | 3679 | panic("nfs3_write_rpc: iovcnt > 1"); |
0a7de745 | 3680 | } |
1c79356b | 3681 | #endif |
b0d623f7 | 3682 | FSDBG_TOP(537, np, uio_offset(uio), uio_resid(uio), *iomodep); |
2d21ac55 | 3683 | nmp = NFSTONMP(np); |
0a7de745 A |
3684 | if (nfs_mount_gone(nmp)) { |
3685 | return ENXIO; | |
3686 | } | |
2d21ac55 A |
3687 | nfsvers = nmp->nm_vers; |
3688 | nmwsize = nmp->nm_wsize; | |
3689 | ||
b0d623f7 | 3690 | wverfset = 0; |
2d21ac55 A |
3691 | committed = NFS_WRITE_FILESYNC; |
3692 | ||
b0d623f7 A |
3693 | totalsize = tsiz = uio_resid(uio); |
3694 | if ((nfsvers == NFS_VER2) && ((uint64_t)(uio_offset(uio) + tsiz) > 0xffffffffULL)) { | |
3695 | FSDBG_BOT(537, np, uio_offset(uio), uio_resid(uio), EFBIG); | |
0a7de745 | 3696 | return EFBIG; |
55e303ae | 3697 | } |
2d21ac55 | 3698 | |
39236c6e A |
3699 | uio_save = uio_duplicate(uio); |
3700 | if (uio_save == NULL) { | |
0a7de745 | 3701 | return EIO; |
39236c6e A |
3702 | } |
3703 | ||
1c79356b | 3704 | while (tsiz > 0) { |
2d21ac55 | 3705 | len = (tsiz > nmwsize) ? nmwsize : tsiz; |
b0d623f7 | 3706 | FSDBG(537, np, uio_offset(uio), len, 0); |
6d2010ae A |
3707 | if (np->n_flag & NREVOKE) { |
3708 | error = EIO; | |
3709 | break; | |
3710 | } | |
cb323159 | 3711 | #if CONFIG_NFS4 |
0a7de745 | 3712 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 3713 | stategenid = nmp->nm_stategenid; |
0a7de745 | 3714 | } |
cb323159 | 3715 | #endif |
b0d623f7 | 3716 | error = nmp->nm_funcs->nf_write_rpc_async(np, uio, len, thd, cred, *iomodep, NULL, &req); |
0a7de745 | 3717 | if (!error) { |
2d21ac55 | 3718 | error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req, &commit, &rlen, &wverf2); |
0a7de745 | 3719 | } |
2d21ac55 | 3720 | nmp = NFSTONMP(np); |
0a7de745 | 3721 | if (nfs_mount_gone(nmp)) { |
55e303ae | 3722 | error = ENXIO; |
0a7de745 | 3723 | } |
cb323159 | 3724 | #if CONFIG_NFS4 |
b0d623f7 A |
3725 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) && |
3726 | (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */ | |
3727 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
3728 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
3729 | NP(np, "nfs_write_rpc: error %d, initiating recovery", error); | |
3730 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
3731 | } |
3732 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae A |
3733 | if (np->n_flag & NREVOKE) { |
3734 | error = EIO; | |
3735 | } else { | |
0a7de745 A |
3736 | if (error == NFSERR_GRACE) { |
3737 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
3738 | } | |
3739 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 3740 | continue; |
0a7de745 | 3741 | } |
6d2010ae | 3742 | } |
b0d623f7 | 3743 | } |
cb323159 | 3744 | #endif |
0a7de745 | 3745 | if (error) { |
91447636 | 3746 | break; |
0a7de745 | 3747 | } |
2d21ac55 A |
3748 | if (nfsvers == NFS_VER2) { |
3749 | tsiz -= len; | |
3750 | continue; | |
1c79356b | 3751 | } |
1c79356b | 3752 | |
2d21ac55 A |
3753 | /* check for a short write */ |
3754 | if (rlen < len) { | |
39236c6e A |
3755 | /* Reset the uio to reflect the actual transfer */ |
3756 | *uio = *uio_save; | |
3757 | uio_update(uio, totalsize - (tsiz - rlen)); | |
2d21ac55 | 3758 | len = rlen; |
e5568f75 | 3759 | } |
fa4905b1 | 3760 | |
2d21ac55 | 3761 | /* return lowest commit level returned */ |
0a7de745 | 3762 | if (commit < committed) { |
2d21ac55 | 3763 | committed = commit; |
0a7de745 | 3764 | } |
2d21ac55 | 3765 | |
fa4905b1 | 3766 | tsiz -= len; |
2d21ac55 A |
3767 | |
3768 | /* check write verifier */ | |
3769 | if (!wverfset) { | |
3770 | wverf = wverf2; | |
3771 | wverfset = 1; | |
3772 | } else if (wverf != wverf2) { | |
3773 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 3774 | if (++vrestart > 100) { |
2d21ac55 A |
3775 | /* give up after too many restarts */ |
3776 | error = EIO; | |
3777 | break; | |
3778 | } | |
0a7de745 | 3779 | *uio = *uio_save; // Reset the uio back to the start |
2d21ac55 A |
3780 | committed = NFS_WRITE_FILESYNC; |
3781 | wverfset = 0; | |
3782 | tsiz = totalsize; | |
3783 | } | |
3784 | } | |
0a7de745 | 3785 | if (uio_save) { |
39236c6e | 3786 | uio_free(uio_save); |
0a7de745 A |
3787 | } |
3788 | if (wverfset && wverfp) { | |
2d21ac55 | 3789 | *wverfp = wverf; |
0a7de745 | 3790 | } |
2d21ac55 | 3791 | *iomodep = committed; |
0a7de745 | 3792 | if (error) { |
b0d623f7 | 3793 | uio_setresid(uio, tsiz); |
0a7de745 | 3794 | } |
b0d623f7 | 3795 | FSDBG_BOT(537, np, committed, uio_resid(uio), error); |
0a7de745 | 3796 | return error; |
2d21ac55 A |
3797 | } |
3798 | ||
3799 | int | |
3800 | nfs3_write_rpc_async( | |
3801 | nfsnode_t np, | |
b0d623f7 | 3802 | uio_t uio, |
2d21ac55 A |
3803 | size_t len, |
3804 | thread_t thd, | |
3805 | kauth_cred_t cred, | |
3806 | int iomode, | |
3807 | struct nfsreq_cbinfo *cb, | |
3808 | struct nfsreq **reqp) | |
3809 | { | |
3810 | struct nfsmount *nmp; | |
6d2010ae | 3811 | mount_t mp; |
2d21ac55 | 3812 | int error = 0, nfsvers; |
2d21ac55 A |
3813 | struct nfsm_chain nmreq; |
3814 | ||
3815 | nmp = NFSTONMP(np); | |
0a7de745 A |
3816 | if (nfs_mount_gone(nmp)) { |
3817 | return ENXIO; | |
3818 | } | |
2d21ac55 A |
3819 | nfsvers = nmp->nm_vers; |
3820 | ||
6d2010ae A |
3821 | /* for async mounts, don't bother sending sync write requests */ |
3822 | if ((iomode != NFS_WRITE_UNSTABLE) && nfs_allow_async && | |
0a7de745 | 3823 | ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC)) { |
6d2010ae | 3824 | iomode = NFS_WRITE_UNSTABLE; |
0a7de745 | 3825 | } |
6d2010ae | 3826 | |
2d21ac55 A |
3827 | nfsm_chain_null(&nmreq); |
3828 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 3829 | NFSX_FH(nfsvers) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); |
2d21ac55 A |
3830 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); |
3831 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 3832 | nfsm_chain_add_64(error, &nmreq, uio_offset(uio)); |
2d21ac55 A |
3833 | nfsm_chain_add_32(error, &nmreq, len); |
3834 | nfsm_chain_add_32(error, &nmreq, iomode); | |
3835 | } else { | |
3836 | nfsm_chain_add_32(error, &nmreq, 0); | |
b0d623f7 | 3837 | nfsm_chain_add_32(error, &nmreq, uio_offset(uio)); |
2d21ac55 A |
3838 | nfsm_chain_add_32(error, &nmreq, 0); |
3839 | } | |
3840 | nfsm_chain_add_32(error, &nmreq, len); | |
3841 | nfsmout_if(error); | |
b0d623f7 | 3842 | error = nfsm_chain_add_uio(&nmreq, uio, len); |
2d21ac55 A |
3843 | nfsm_chain_build_done(error, &nmreq); |
3844 | nfsmout_if(error); | |
6d2010ae | 3845 | error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, NULL, 0, cb, reqp); |
2d21ac55 A |
3846 | nfsmout: |
3847 | nfsm_chain_cleanup(&nmreq); | |
0a7de745 | 3848 | return error; |
2d21ac55 A |
3849 | } |
3850 | ||
3851 | int | |
3852 | nfs3_write_rpc_async_finish( | |
3853 | nfsnode_t np, | |
3854 | struct nfsreq *req, | |
3855 | int *iomodep, | |
3856 | size_t *rlenp, | |
3857 | uint64_t *wverfp) | |
3858 | { | |
3859 | struct nfsmount *nmp; | |
3860 | int error = 0, lockerror = ENOENT, nfsvers, status; | |
3861 | int updatemtime = 0, wccpostattr = 0, rlen, committed = NFS_WRITE_FILESYNC; | |
3862 | u_int64_t xid, wverf; | |
3863 | mount_t mp; | |
3864 | struct nfsm_chain nmrep; | |
3865 | ||
3866 | nmp = NFSTONMP(np); | |
fe8ab488 | 3867 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 3868 | nfs_request_async_cancel(req); |
0a7de745 | 3869 | return ENXIO; |
2d21ac55 A |
3870 | } |
3871 | nfsvers = nmp->nm_vers; | |
3872 | ||
3873 | nfsm_chain_null(&nmrep); | |
3874 | ||
3875 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
0a7de745 A |
3876 | if (error == EINPROGRESS) { /* async request restarted */ |
3877 | return error; | |
3878 | } | |
2d21ac55 | 3879 | nmp = NFSTONMP(np); |
0a7de745 | 3880 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 3881 | error = ENXIO; |
0a7de745 A |
3882 | } |
3883 | if (!error && (lockerror = nfs_node_lock(np))) { | |
2d21ac55 | 3884 | error = lockerror; |
0a7de745 | 3885 | } |
2d21ac55 | 3886 | if (nfsvers == NFS_VER3) { |
cb323159 | 3887 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 | 3888 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); |
0a7de745 | 3889 | if (nfstimespeccmp(&np->n_mtime, &premtime, ==)) { |
2d21ac55 | 3890 | updatemtime = 1; |
0a7de745 A |
3891 | } |
3892 | if (!error) { | |
2d21ac55 | 3893 | error = status; |
0a7de745 | 3894 | } |
2d21ac55 A |
3895 | nfsm_chain_get_32(error, &nmrep, rlen); |
3896 | nfsmout_if(error); | |
3897 | *rlenp = rlen; | |
0a7de745 | 3898 | if (rlen <= 0) { |
2d21ac55 | 3899 | error = NFSERR_IO; |
0a7de745 | 3900 | } |
2d21ac55 A |
3901 | nfsm_chain_get_32(error, &nmrep, committed); |
3902 | nfsm_chain_get_64(error, &nmrep, wverf); | |
3903 | nfsmout_if(error); | |
0a7de745 | 3904 | if (wverfp) { |
2d21ac55 | 3905 | *wverfp = wverf; |
0a7de745 | 3906 | } |
2d21ac55 A |
3907 | lck_mtx_lock(&nmp->nm_lock); |
3908 | if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) { | |
3909 | nmp->nm_verf = wverf; | |
3910 | nmp->nm_state |= NFSSTA_HASWRITEVERF; | |
3911 | } else if (nmp->nm_verf != wverf) { | |
3912 | nmp->nm_verf = wverf; | |
3913 | } | |
3914 | lck_mtx_unlock(&nmp->nm_lock); | |
3915 | } else { | |
0a7de745 | 3916 | if (!error) { |
2d21ac55 | 3917 | error = status; |
0a7de745 | 3918 | } |
6d2010ae | 3919 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
2d21ac55 | 3920 | nfsmout_if(error); |
1c79356b | 3921 | } |
0a7de745 | 3922 | if (updatemtime) { |
2d21ac55 | 3923 | NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr); |
0a7de745 | 3924 | } |
1c79356b | 3925 | nfsmout: |
0a7de745 | 3926 | if (!lockerror) { |
b0d623f7 | 3927 | nfs_node_unlock(np); |
0a7de745 | 3928 | } |
2d21ac55 A |
3929 | nfsm_chain_cleanup(&nmrep); |
3930 | if ((committed != NFS_WRITE_FILESYNC) && nfs_allow_async && | |
0a7de745 | 3931 | ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC)) { |
2d21ac55 | 3932 | committed = NFS_WRITE_FILESYNC; |
0a7de745 | 3933 | } |
2d21ac55 | 3934 | *iomodep = committed; |
0a7de745 | 3935 | return error; |
1c79356b A |
3936 | } |
3937 | ||
3938 | /* | |
2d21ac55 A |
3939 | * NFS mknod vnode op |
3940 | * | |
3941 | * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the | |
1c79356b A |
3942 | * mode set to specify the file type and the size field for rdev. |
3943 | */ | |
b0d623f7 | 3944 | int |
2d21ac55 A |
3945 | nfs3_vnop_mknod( |
3946 | struct vnop_mknod_args /* { | |
0a7de745 A |
3947 | * struct vnodeop_desc *a_desc; |
3948 | * vnode_t a_dvp; | |
3949 | * vnode_t *a_vpp; | |
3950 | * struct componentname *a_cnp; | |
3951 | * struct vnode_attr *a_vap; | |
3952 | * vfs_context_t a_context; | |
3953 | * } */*ap) | |
1c79356b | 3954 | { |
2d21ac55 A |
3955 | vnode_t dvp = ap->a_dvp; |
3956 | vnode_t *vpp = ap->a_vpp; | |
3957 | struct componentname *cnp = ap->a_cnp; | |
3958 | struct vnode_attr *vap = ap->a_vap; | |
3959 | vfs_context_t ctx = ap->a_context; | |
3960 | vnode_t newvp = NULL; | |
3961 | nfsnode_t np = NULL; | |
3962 | struct nfsmount *nmp; | |
3963 | nfsnode_t dnp = VTONFS(dvp); | |
6d2010ae | 3964 | struct nfs_vattr nvattr; |
2d21ac55 | 3965 | fhandle_t fh; |
b0d623f7 | 3966 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0; |
cb323159 | 3967 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
b0d623f7 | 3968 | u_int32_t rdev; |
39236c6e | 3969 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
3970 | int nfsvers, gotuid, gotgid; |
3971 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 3972 | struct nfsreq rq, *req = &rq; |
2d21ac55 A |
3973 | |
3974 | nmp = VTONMP(dvp); | |
0a7de745 A |
3975 | if (nfs_mount_gone(nmp)) { |
3976 | return ENXIO; | |
3977 | } | |
2d21ac55 | 3978 | nfsvers = nmp->nm_vers; |
1c79356b | 3979 | |
0a7de745 A |
3980 | if (!VATTR_IS_ACTIVE(vap, va_type)) { |
3981 | return EINVAL; | |
3982 | } | |
91447636 | 3983 | if (vap->va_type == VCHR || vap->va_type == VBLK) { |
0a7de745 A |
3984 | if (!VATTR_IS_ACTIVE(vap, va_rdev)) { |
3985 | return EINVAL; | |
3986 | } | |
2d21ac55 | 3987 | rdev = vap->va_rdev; |
0a7de745 | 3988 | } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) { |
1c79356b | 3989 | rdev = 0xffffffff; |
0a7de745 A |
3990 | } else { |
3991 | return ENOTSUP; | |
3992 | } | |
3993 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { | |
3994 | return ENAMETOOLONG; | |
1c79356b | 3995 | } |
91447636 | 3996 | |
6d2010ae A |
3997 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
3998 | ||
91447636 A |
3999 | VATTR_SET_SUPPORTED(vap, va_mode); |
4000 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4001 | VATTR_SET_SUPPORTED(vap, va_gid); | |
4002 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
4003 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
4004 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
4005 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
4006 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
4007 | ||
2d21ac55 A |
4008 | nfsm_chain_null(&nmreq); |
4009 | nfsm_chain_null(&nmrep); | |
4010 | ||
4011 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
4012 | NFSX_FH(nfsvers) + 4 * NFSX_UNSIGNED + |
4013 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 4014 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 4015 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4016 | if (nfsvers == NFS_VER3) { |
4017 | nfsm_chain_add_32(error, &nmreq, vtonfs_type(vap->va_type, nfsvers)); | |
cb323159 | 4018 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
1c79356b | 4019 | if (vap->va_type == VCHR || vap->va_type == VBLK) { |
2d21ac55 A |
4020 | nfsm_chain_add_32(error, &nmreq, major(vap->va_rdev)); |
4021 | nfsm_chain_add_32(error, &nmreq, minor(vap->va_rdev)); | |
1c79356b A |
4022 | } |
4023 | } else { | |
2d21ac55 | 4024 | nfsm_chain_add_v2sattr(error, &nmreq, vap, rdev); |
91447636 | 4025 | } |
2d21ac55 | 4026 | nfsm_chain_build_done(error, &nmreq); |
0a7de745 | 4027 | if (!error) { |
b0d623f7 | 4028 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
0a7de745 | 4029 | } |
2d21ac55 A |
4030 | nfsmout_if(error); |
4031 | ||
6d2010ae | 4032 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKNOD, |
0a7de745 A |
4033 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
4034 | if (!error) { | |
6d2010ae | 4035 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
0a7de745 | 4036 | } |
2d21ac55 | 4037 | |
0a7de745 | 4038 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 4039 | error = lockerror; |
0a7de745 | 4040 | } |
91447636 | 4041 | /* XXX no EEXIST kludge here? */ |
2d21ac55 A |
4042 | dxid = xid; |
4043 | if (!error && !status) { | |
4044 | if (dnp->n_flag & NNEGNCENTRIES) { | |
4045 | dnp->n_flag &= ~NNEGNCENTRIES; | |
4046 | cache_purge_negatives(dvp); | |
4047 | } | |
cb323159 | 4048 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); |
2d21ac55 | 4049 | } |
0a7de745 | 4050 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 4051 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
4052 | } |
4053 | if (!error) { | |
2d21ac55 | 4054 | error = status; |
0a7de745 | 4055 | } |
2d21ac55 A |
4056 | nfsmout: |
4057 | nfsm_chain_cleanup(&nmreq); | |
4058 | nfsm_chain_cleanup(&nmrep); | |
4059 | ||
4060 | if (!lockerror) { | |
4061 | dnp->n_flag |= NMODIFIED; | |
4062 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4063 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 4064 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 4065 | } |
b0d623f7 A |
4066 | nfs_node_unlock(dnp); |
4067 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4068 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 4069 | } |
2d21ac55 | 4070 | |
0a7de745 | 4071 | if (!error && fh.fh_len) { |
6d2010ae | 4072 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
0a7de745 A |
4073 | } |
4074 | if (!error && !np) { | |
2d21ac55 | 4075 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
0a7de745 A |
4076 | } |
4077 | if (!error && np) { | |
2d21ac55 | 4078 | newvp = NFSTOV(np); |
0a7de745 A |
4079 | } |
4080 | if (!busyerror) { | |
b0d623f7 | 4081 | nfs_node_clear_busy(dnp); |
0a7de745 | 4082 | } |
2d21ac55 | 4083 | |
91447636 | 4084 | if (!error && (gotuid || gotgid) && |
6d2010ae | 4085 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
0a7de745 A |
4086 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
4087 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
91447636 A |
4088 | /* clear ID bits if server didn't use them (or we can't tell) */ |
4089 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
4090 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
4091 | } | |
1c79356b | 4092 | if (error) { |
2d21ac55 | 4093 | if (newvp) { |
b0d623f7 | 4094 | nfs_node_unlock(np); |
91447636 | 4095 | vnode_put(newvp); |
2d21ac55 | 4096 | } |
1c79356b | 4097 | } else { |
1c79356b | 4098 | *vpp = newvp; |
b0d623f7 | 4099 | nfs_node_unlock(np); |
1c79356b | 4100 | } |
0a7de745 | 4101 | return error; |
1c79356b A |
4102 | } |
4103 | ||
b0d623f7 | 4104 | static uint32_t create_verf; |
1c79356b | 4105 | /* |
2d21ac55 | 4106 | * NFS file create call |
1c79356b | 4107 | */ |
b0d623f7 | 4108 | int |
2d21ac55 | 4109 | nfs3_vnop_create( |
91447636 | 4110 | struct vnop_create_args /* { |
0a7de745 A |
4111 | * struct vnodeop_desc *a_desc; |
4112 | * vnode_t a_dvp; | |
4113 | * vnode_t *a_vpp; | |
4114 | * struct componentname *a_cnp; | |
4115 | * struct vnode_attr *a_vap; | |
4116 | * vfs_context_t a_context; | |
4117 | * } */*ap) | |
1c79356b | 4118 | { |
2d21ac55 | 4119 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4120 | vnode_t dvp = ap->a_dvp; |
4121 | struct vnode_attr *vap = ap->a_vap; | |
4122 | struct componentname *cnp = ap->a_cnp; | |
6d2010ae | 4123 | struct nfs_vattr nvattr; |
2d21ac55 A |
4124 | fhandle_t fh; |
4125 | nfsnode_t np = NULL; | |
4126 | struct nfsmount *nmp; | |
4127 | nfsnode_t dnp = VTONFS(dvp); | |
4128 | vnode_t newvp = NULL; | |
b0d623f7 | 4129 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0, fmode = 0; |
cb323159 | 4130 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4131 | int nfsvers, gotuid, gotgid; |
4132 | u_int64_t xid, dxid; | |
4133 | uint32_t val; | |
4134 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 4135 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 4136 | struct nfs_dulookup dul; |
cb323159 A |
4137 | int dul_in_progress = 0; |
4138 | int namedattrs; | |
91447636 | 4139 | |
2d21ac55 | 4140 | nmp = VTONMP(dvp); |
0a7de745 A |
4141 | if (nfs_mount_gone(nmp)) { |
4142 | return ENXIO; | |
4143 | } | |
2d21ac55 | 4144 | nfsvers = nmp->nm_vers; |
cb323159 | 4145 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
1c79356b | 4146 | |
0a7de745 A |
4147 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
4148 | return ENAMETOOLONG; | |
4149 | } | |
91447636 | 4150 | |
6d2010ae A |
4151 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
4152 | ||
91447636 A |
4153 | VATTR_SET_SUPPORTED(vap, va_mode); |
4154 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4155 | VATTR_SET_SUPPORTED(vap, va_gid); | |
4156 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
4157 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
4158 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
4159 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
4160 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
1c79356b | 4161 | |
cb323159 A |
4162 | if ((vap->va_vaflags & VA_EXCLUSIVE) |
4163 | ) { | |
1c79356b | 4164 | fmode |= O_EXCL; |
0a7de745 | 4165 | if (!VATTR_IS_ACTIVE(vap, va_access_time) || !VATTR_IS_ACTIVE(vap, va_modify_time)) { |
6d2010ae | 4166 | vap->va_vaflags |= VA_UTIMES_NULL; |
0a7de745 | 4167 | } |
6d2010ae | 4168 | } |
2d21ac55 | 4169 | |
1c79356b | 4170 | again: |
b0d623f7 | 4171 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
cb323159 A |
4172 | if (!namedattrs) { |
4173 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
4174 | } | |
2d21ac55 A |
4175 | |
4176 | nfsm_chain_null(&nmreq); | |
4177 | nfsm_chain_null(&nmrep); | |
4178 | ||
4179 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
4180 | NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED + |
4181 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 4182 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 4183 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 | 4184 | if (nfsvers == NFS_VER3) { |
1c79356b | 4185 | if (fmode & O_EXCL) { |
2d21ac55 | 4186 | nfsm_chain_add_32(error, &nmreq, NFS_CREATE_EXCLUSIVE); |
b0d623f7 | 4187 | lck_rw_lock_shared(in_ifaddr_rwlock); |
0a7de745 | 4188 | if (!TAILQ_EMPTY(&in_ifaddrhead)) { |
2d21ac55 | 4189 | val = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr; |
0a7de745 | 4190 | } else { |
2d21ac55 | 4191 | val = create_verf; |
0a7de745 | 4192 | } |
b0d623f7 | 4193 | lck_rw_done(in_ifaddr_rwlock); |
2d21ac55 A |
4194 | nfsm_chain_add_32(error, &nmreq, val); |
4195 | ++create_verf; | |
4196 | nfsm_chain_add_32(error, &nmreq, create_verf); | |
1c79356b | 4197 | } else { |
2d21ac55 | 4198 | nfsm_chain_add_32(error, &nmreq, NFS_CREATE_UNCHECKED); |
cb323159 | 4199 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
1c79356b A |
4200 | } |
4201 | } else { | |
2d21ac55 | 4202 | nfsm_chain_add_v2sattr(error, &nmreq, vap, 0); |
1c79356b | 4203 | } |
2d21ac55 A |
4204 | nfsm_chain_build_done(error, &nmreq); |
4205 | nfsmout_if(error); | |
2d21ac55 A |
4206 | |
4207 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_CREATE, | |
0a7de745 | 4208 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
1c79356b | 4209 | if (!error) { |
cb323159 A |
4210 | if (!namedattrs) { |
4211 | nfs_dulookup_start(&dul, dnp, ctx); | |
4212 | dul_in_progress = 1; | |
4213 | } | |
2d21ac55 A |
4214 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
4215 | } | |
4216 | ||
0a7de745 | 4217 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 4218 | error = lockerror; |
0a7de745 | 4219 | } |
2d21ac55 A |
4220 | dxid = xid; |
4221 | if (!error && !status) { | |
4222 | if (dnp->n_flag & NNEGNCENTRIES) { | |
4223 | dnp->n_flag &= ~NNEGNCENTRIES; | |
4224 | cache_purge_negatives(dvp); | |
4225 | } | |
cb323159 | 4226 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); |
2d21ac55 | 4227 | } |
0a7de745 | 4228 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 4229 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
4230 | } |
4231 | if (!error) { | |
2d21ac55 | 4232 | error = status; |
0a7de745 | 4233 | } |
2d21ac55 A |
4234 | nfsmout: |
4235 | nfsm_chain_cleanup(&nmreq); | |
4236 | nfsm_chain_cleanup(&nmrep); | |
4237 | ||
4238 | if (!lockerror) { | |
4239 | dnp->n_flag |= NMODIFIED; | |
4240 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4241 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 4242 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 4243 | } |
b0d623f7 A |
4244 | nfs_node_unlock(dnp); |
4245 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4246 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 4247 | } |
2d21ac55 | 4248 | |
0a7de745 | 4249 | if (!error && fh.fh_len) { |
6d2010ae | 4250 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
0a7de745 A |
4251 | } |
4252 | if (!error && !np) { | |
2d21ac55 | 4253 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
0a7de745 A |
4254 | } |
4255 | if (!error && np) { | |
2d21ac55 | 4256 | newvp = NFSTOV(np); |
0a7de745 | 4257 | } |
2d21ac55 | 4258 | |
cb323159 A |
4259 | if (dul_in_progress) { |
4260 | nfs_dulookup_finish(&dul, dnp, ctx); | |
4261 | } | |
0a7de745 | 4262 | if (!busyerror) { |
b0d623f7 | 4263 | nfs_node_clear_busy(dnp); |
0a7de745 | 4264 | } |
2d21ac55 | 4265 | |
1c79356b | 4266 | if (error) { |
2d21ac55 | 4267 | if ((nfsvers == NFS_VER3) && (fmode & O_EXCL) && (error == NFSERR_NOTSUPP)) { |
1c79356b A |
4268 | fmode &= ~O_EXCL; |
4269 | goto again; | |
4270 | } | |
2d21ac55 | 4271 | if (newvp) { |
b0d623f7 | 4272 | nfs_node_unlock(np); |
91447636 | 4273 | vnode_put(newvp); |
2d21ac55 A |
4274 | } |
4275 | } else if ((nfsvers == NFS_VER3) && (fmode & O_EXCL)) { | |
b0d623f7 A |
4276 | nfs_node_unlock(np); |
4277 | error = nfs3_setattr_rpc(np, vap, ctx); | |
91447636 A |
4278 | if (error && (gotuid || gotgid)) { |
4279 | /* it's possible the server didn't like our attempt to set IDs. */ | |
4280 | /* so, let's try it again without those */ | |
4281 | VATTR_CLEAR_ACTIVE(vap, va_uid); | |
4282 | VATTR_CLEAR_ACTIVE(vap, va_gid); | |
b0d623f7 | 4283 | error = nfs3_setattr_rpc(np, vap, ctx); |
91447636 | 4284 | } |
0a7de745 | 4285 | if (error) { |
91447636 | 4286 | vnode_put(newvp); |
0a7de745 | 4287 | } else { |
b0d623f7 | 4288 | nfs_node_lock_force(np); |
0a7de745 | 4289 | } |
ccc36f2f | 4290 | } |
0a7de745 | 4291 | if (!error) { |
1c79356b | 4292 | *ap->a_vpp = newvp; |
0a7de745 | 4293 | } |
91447636 | 4294 | if (!error && (gotuid || gotgid) && |
6d2010ae | 4295 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
0a7de745 A |
4296 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
4297 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
91447636 A |
4298 | /* clear ID bits if server didn't use them (or we can't tell) */ |
4299 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
4300 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
4301 | } | |
0a7de745 | 4302 | if (!error) { |
b0d623f7 | 4303 | nfs_node_unlock(np); |
0a7de745 A |
4304 | } |
4305 | return error; | |
1c79356b A |
4306 | } |
4307 | ||
4308 | /* | |
2d21ac55 A |
4309 | * NFS file remove call |
4310 | * To try and make NFS semantics closer to UFS semantics, a file that has | |
1c79356b A |
4311 | * other processes using the vnode is renamed instead of removed and then |
4312 | * removed later on the last close. | |
91447636 | 4313 | * - If vnode_isinuse() |
1c79356b A |
4314 | * If a rename is not already in the works |
4315 | * call nfs_sillyrename() to set it up | |
4316 | * else | |
2d21ac55 | 4317 | * do the remove RPC |
1c79356b | 4318 | */ |
b0d623f7 | 4319 | int |
2d21ac55 | 4320 | nfs_vnop_remove( |
91447636 | 4321 | struct vnop_remove_args /* { |
0a7de745 A |
4322 | * struct vnodeop_desc *a_desc; |
4323 | * vnode_t a_dvp; | |
4324 | * vnode_t a_vp; | |
4325 | * struct componentname *a_cnp; | |
4326 | * int a_flags; | |
4327 | * vfs_context_t a_context; | |
4328 | * } */*ap) | |
1c79356b | 4329 | { |
2d21ac55 | 4330 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4331 | vnode_t vp = ap->a_vp; |
4332 | vnode_t dvp = ap->a_dvp; | |
4333 | struct componentname *cnp = ap->a_cnp; | |
2d21ac55 A |
4334 | nfsnode_t dnp = VTONFS(dvp); |
4335 | nfsnode_t np = VTONFS(vp); | |
6d2010ae | 4336 | int error = 0, nfsvers, namedattrs, inuse, gotattr = 0, flushed = 0, setsize = 0; |
91447636 | 4337 | struct nfs_vattr nvattr; |
2d21ac55 A |
4338 | struct nfsmount *nmp; |
4339 | struct nfs_dulookup dul; | |
4340 | ||
4341 | /* XXX prevent removing a sillyrenamed file? */ | |
4342 | ||
4343 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
4344 | if (nfs_mount_gone(nmp)) { |
4345 | return ENXIO; | |
4346 | } | |
2d21ac55 | 4347 | nfsvers = nmp->nm_vers; |
6d2010ae | 4348 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
2d21ac55 A |
4349 | |
4350 | again_relock: | |
b0d623f7 | 4351 | error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx)); |
0a7de745 A |
4352 | if (error) { |
4353 | return error; | |
4354 | } | |
1c79356b | 4355 | |
2d21ac55 A |
4356 | /* lock the node while we remove the file */ |
4357 | lck_mtx_lock(nfs_node_hash_mutex); | |
4358 | while (np->n_hflag & NHLOCKED) { | |
4359 | np->n_hflag |= NHLOCKWANT; | |
4360 | msleep(np, nfs_node_hash_mutex, PINOD, "nfs_remove", NULL); | |
4361 | } | |
4362 | np->n_hflag |= NHLOCKED; | |
4363 | lck_mtx_unlock(nfs_node_hash_mutex); | |
9bccf70c | 4364 | |
0a7de745 | 4365 | if (!namedattrs) { |
6d2010ae | 4366 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
0a7de745 | 4367 | } |
2d21ac55 A |
4368 | again: |
4369 | inuse = vnode_isinuse(vp, 0); | |
4370 | if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && inuse) { | |
55e303ae | 4371 | /* Caller requested Carbon delete semantics, but file is busy */ |
2d21ac55 A |
4372 | error = EBUSY; |
4373 | goto out; | |
55e303ae | 4374 | } |
2d21ac55 | 4375 | if (inuse && !gotattr) { |
0a7de745 | 4376 | if (nfs_getattr(np, &nvattr, ctx, NGA_CACHED)) { |
2d21ac55 | 4377 | nvattr.nva_nlink = 1; |
0a7de745 | 4378 | } |
2d21ac55 A |
4379 | gotattr = 1; |
4380 | goto again; | |
4381 | } | |
4382 | if (!inuse || (np->n_sillyrename && (nvattr.nva_nlink > 1))) { | |
2d21ac55 A |
4383 | if (!inuse && !flushed) { /* flush all the buffers first */ |
4384 | /* unlock the node */ | |
4385 | lck_mtx_lock(nfs_node_hash_mutex); | |
4386 | np->n_hflag &= ~NHLOCKED; | |
4387 | if (np->n_hflag & NHLOCKWANT) { | |
4388 | np->n_hflag &= ~NHLOCKWANT; | |
4389 | wakeup(np); | |
4390 | } | |
4391 | lck_mtx_unlock(nfs_node_hash_mutex); | |
b0d623f7 | 4392 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 A |
4393 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
4394 | FSDBG(260, np, np->n_size, np->n_vattr.nva_size, 0xf00d0011); | |
4395 | flushed = 1; | |
4396 | if (error == EINTR) { | |
b0d623f7 | 4397 | nfs_node_lock_force(np); |
2d21ac55 | 4398 | NATTRINVALIDATE(np); |
b0d623f7 | 4399 | nfs_node_unlock(np); |
0a7de745 | 4400 | return error; |
2d21ac55 | 4401 | } |
0a7de745 | 4402 | if (!namedattrs) { |
6d2010ae | 4403 | nfs_dulookup_finish(&dul, dnp, ctx); |
0a7de745 | 4404 | } |
2d21ac55 A |
4405 | goto again_relock; |
4406 | } | |
cb323159 | 4407 | #if CONFIG_NFS4 |
0a7de745 | 4408 | if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK)) { |
6d2010ae | 4409 | nfs4_delegation_return(np, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
0a7de745 | 4410 | } |
cb323159 | 4411 | #endif |
1c79356b A |
4412 | /* |
4413 | * Purge the name cache so that the chance of a lookup for | |
4414 | * the name succeeding while the remove is in progress is | |
483a1d10 | 4415 | * minimized. |
1c79356b | 4416 | */ |
b0d623f7 | 4417 | nfs_name_cache_purge(dnp, np, cnp, ctx); |
2d21ac55 | 4418 | |
0a7de745 | 4419 | if (!namedattrs) { |
6d2010ae | 4420 | nfs_dulookup_start(&dul, dnp, ctx); |
0a7de745 | 4421 | } |
2d21ac55 | 4422 | |
1c79356b | 4423 | /* Do the rpc */ |
2d21ac55 | 4424 | error = nmp->nm_funcs->nf_remove_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen, |
0a7de745 | 4425 | vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
2d21ac55 | 4426 | |
1c79356b A |
4427 | /* |
4428 | * Kludge City: If the first reply to the remove rpc is lost.. | |
4429 | * the reply to the retransmitted request will be ENOENT | |
4430 | * since the file was in fact removed | |
4431 | * Therefore, we cheat and return success. | |
4432 | */ | |
0a7de745 | 4433 | if (error == ENOENT) { |
1c79356b | 4434 | error = 0; |
0a7de745 | 4435 | } |
2d21ac55 A |
4436 | |
4437 | if (!error && !inuse && !np->n_sillyrename) { | |
55e303ae | 4438 | /* |
2d21ac55 | 4439 | * removal succeeded, it's not in use, and not silly renamed so |
55e303ae A |
4440 | * remove nfsnode from hash now so we can't accidentally find it |
4441 | * again if another object gets created with the same filehandle | |
4442 | * before this vnode gets reclaimed | |
4443 | */ | |
91447636 | 4444 | lck_mtx_lock(nfs_node_hash_mutex); |
2d21ac55 A |
4445 | if (np->n_hflag & NHHASHED) { |
4446 | LIST_REMOVE(np, n_hash); | |
4447 | np->n_hflag &= ~NHHASHED; | |
4448 | FSDBG(266, 0, np, np->n_flag, 0xb1eb1e); | |
4449 | } | |
91447636 | 4450 | lck_mtx_unlock(nfs_node_hash_mutex); |
2d21ac55 | 4451 | /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */ |
91447636 | 4452 | /* clear all flags other than these */ |
b0d623f7 | 4453 | nfs_node_lock_force(np); |
2d21ac55 | 4454 | np->n_flag &= (NMODIFIED); |
2d21ac55 | 4455 | NATTRINVALIDATE(np); |
b0d623f7 A |
4456 | nfs_node_unlock(np); |
4457 | vnode_recycle(vp); | |
2d21ac55 A |
4458 | setsize = 1; |
4459 | } else { | |
b0d623f7 | 4460 | nfs_node_lock_force(np); |
2d21ac55 | 4461 | NATTRINVALIDATE(np); |
b0d623f7 | 4462 | nfs_node_unlock(np); |
55e303ae | 4463 | } |
1c79356b | 4464 | } else if (!np->n_sillyrename) { |
0a7de745 | 4465 | if (!namedattrs) { |
6d2010ae | 4466 | nfs_dulookup_start(&dul, dnp, ctx); |
0a7de745 | 4467 | } |
2d21ac55 | 4468 | error = nfs_sillyrename(dnp, np, cnp, ctx); |
b0d623f7 | 4469 | nfs_node_lock_force(np); |
2d21ac55 | 4470 | NATTRINVALIDATE(np); |
b0d623f7 | 4471 | nfs_node_unlock(np); |
2d21ac55 | 4472 | } else { |
b0d623f7 | 4473 | nfs_node_lock_force(np); |
2d21ac55 | 4474 | NATTRINVALIDATE(np); |
b0d623f7 | 4475 | nfs_node_unlock(np); |
0a7de745 | 4476 | if (!namedattrs) { |
6d2010ae | 4477 | nfs_dulookup_start(&dul, dnp, ctx); |
0a7de745 | 4478 | } |
1c79356b | 4479 | } |
1c79356b | 4480 | |
b0d623f7 | 4481 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae | 4482 | nfs_getattr(dnp, NULL, ctx, NGA_CACHED); |
0a7de745 | 4483 | if (!namedattrs) { |
6d2010ae | 4484 | nfs_dulookup_finish(&dul, dnp, ctx); |
0a7de745 | 4485 | } |
2d21ac55 A |
4486 | out: |
4487 | /* unlock the node */ | |
4488 | lck_mtx_lock(nfs_node_hash_mutex); | |
4489 | np->n_hflag &= ~NHLOCKED; | |
4490 | if (np->n_hflag & NHLOCKWANT) { | |
4491 | np->n_hflag &= ~NHLOCKWANT; | |
4492 | wakeup(np); | |
4493 | } | |
4494 | lck_mtx_unlock(nfs_node_hash_mutex); | |
b0d623f7 | 4495 | nfs_node_clear_busy2(dnp, np); |
0a7de745 | 4496 | if (setsize) { |
2d21ac55 | 4497 | ubc_setsize(vp, 0); |
0a7de745 A |
4498 | } |
4499 | return error; | |
1c79356b A |
4500 | } |
4501 | ||
4502 | /* | |
2d21ac55 | 4503 | * NFS silly-renamed file removal function called from nfs_vnop_inactive |
1c79356b A |
4504 | */ |
4505 | int | |
2d21ac55 | 4506 | nfs_removeit(struct nfs_sillyrename *nsp) |
1c79356b | 4507 | { |
2d21ac55 | 4508 | struct nfsmount *nmp = NFSTONMP(nsp->nsr_dnp); |
0a7de745 A |
4509 | if (nfs_mount_gone(nmp)) { |
4510 | return ENXIO; | |
4511 | } | |
2d21ac55 | 4512 | return nmp->nm_funcs->nf_remove_rpc(nsp->nsr_dnp, nsp->nsr_name, nsp->nsr_namlen, NULL, nsp->nsr_cred); |
1c79356b A |
4513 | } |
4514 | ||
4515 | /* | |
2d21ac55 | 4516 | * NFS remove rpc, called from nfs_remove() and nfs_removeit(). |
1c79356b | 4517 | */ |
2d21ac55 A |
4518 | int |
4519 | nfs3_remove_rpc( | |
4520 | nfsnode_t dnp, | |
4521 | char *name, | |
4522 | int namelen, | |
4523 | thread_t thd, | |
4524 | kauth_cred_t cred) | |
1c79356b | 4525 | { |
b0d623f7 | 4526 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0; |
cb323159 | 4527 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4528 | struct nfsmount *nmp; |
4529 | int nfsvers; | |
fa4905b1 | 4530 | u_int64_t xid; |
2d21ac55 | 4531 | struct nfsm_chain nmreq, nmrep; |
1c79356b | 4532 | |
2d21ac55 | 4533 | nmp = NFSTONMP(dnp); |
0a7de745 A |
4534 | if (nfs_mount_gone(nmp)) { |
4535 | return ENXIO; | |
4536 | } | |
2d21ac55 | 4537 | nfsvers = nmp->nm_vers; |
0a7de745 A |
4538 | if ((nfsvers == NFS_VER2) && (namelen > NFS_MAXNAMLEN)) { |
4539 | return ENAMETOOLONG; | |
4540 | } | |
55e303ae | 4541 | |
2d21ac55 A |
4542 | nfsm_chain_null(&nmreq); |
4543 | nfsm_chain_null(&nmrep); | |
4544 | ||
4545 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 4546 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen)); |
2d21ac55 | 4547 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 4548 | nfsm_chain_add_name(error, &nmreq, name, namelen, nmp); |
2d21ac55 A |
4549 | nfsm_chain_build_done(error, &nmreq); |
4550 | nfsmout_if(error); | |
4551 | ||
6d2010ae | 4552 | error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, NULL, 0, &nmrep, &xid, &status); |
2d21ac55 | 4553 | |
0a7de745 | 4554 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 4555 | error = lockerror; |
0a7de745 A |
4556 | } |
4557 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 4558 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid); |
0a7de745 | 4559 | } |
b0d623f7 | 4560 | nfsmout_if(error); |
2d21ac55 | 4561 | dnp->n_flag |= NMODIFIED; |
483a1d10 | 4562 | /* if directory hadn't changed, update namecache mtime */ |
0a7de745 | 4563 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 4564 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 A |
4565 | } |
4566 | if (!wccpostattr) { | |
2d21ac55 | 4567 | NATTRINVALIDATE(dnp); |
0a7de745 A |
4568 | } |
4569 | if (!error) { | |
2d21ac55 | 4570 | error = status; |
0a7de745 | 4571 | } |
2d21ac55 | 4572 | nfsmout: |
0a7de745 | 4573 | if (!lockerror) { |
b0d623f7 | 4574 | nfs_node_unlock(dnp); |
0a7de745 | 4575 | } |
2d21ac55 A |
4576 | nfsm_chain_cleanup(&nmreq); |
4577 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 4578 | return error; |
1c79356b A |
4579 | } |
4580 | ||
4581 | /* | |
2d21ac55 | 4582 | * NFS file rename call |
1c79356b | 4583 | */ |
b0d623f7 | 4584 | int |
2d21ac55 | 4585 | nfs_vnop_rename( |
91447636 | 4586 | struct vnop_rename_args /* { |
0a7de745 A |
4587 | * struct vnodeop_desc *a_desc; |
4588 | * vnode_t a_fdvp; | |
4589 | * vnode_t a_fvp; | |
4590 | * struct componentname *a_fcnp; | |
4591 | * vnode_t a_tdvp; | |
4592 | * vnode_t a_tvp; | |
4593 | * struct componentname *a_tcnp; | |
4594 | * vfs_context_t a_context; | |
4595 | * } */*ap) | |
1c79356b | 4596 | { |
2d21ac55 | 4597 | vfs_context_t ctx = ap->a_context; |
91447636 | 4598 | vnode_t fdvp = ap->a_fdvp; |
2d21ac55 | 4599 | vnode_t fvp = ap->a_fvp; |
91447636 | 4600 | vnode_t tdvp = ap->a_tdvp; |
2d21ac55 A |
4601 | vnode_t tvp = ap->a_tvp; |
4602 | nfsnode_t fdnp, fnp, tdnp, tnp; | |
91447636 A |
4603 | struct componentname *tcnp = ap->a_tcnp; |
4604 | struct componentname *fcnp = ap->a_fcnp; | |
0a7de745 | 4605 | int error, nfsvers, inuse = 0, tvprecycle = 0, locked = 0; |
91447636 | 4606 | mount_t fmp, tdmp, tmp; |
2d21ac55 A |
4607 | struct nfs_vattr nvattr; |
4608 | struct nfsmount *nmp; | |
91447636 | 4609 | |
2d21ac55 A |
4610 | fdnp = VTONFS(fdvp); |
4611 | fnp = VTONFS(fvp); | |
4612 | tdnp = VTONFS(tdvp); | |
91447636 | 4613 | tnp = tvp ? VTONFS(tvp) : NULL; |
1c79356b | 4614 | |
2d21ac55 | 4615 | nmp = NFSTONMP(fdnp); |
0a7de745 A |
4616 | if (nfs_mount_gone(nmp)) { |
4617 | return ENXIO; | |
4618 | } | |
2d21ac55 A |
4619 | nfsvers = nmp->nm_vers; |
4620 | ||
b0d623f7 | 4621 | error = nfs_node_set_busy4(fdnp, fnp, tdnp, tnp, vfs_context_thread(ctx)); |
0a7de745 A |
4622 | if (error) { |
4623 | return error; | |
4624 | } | |
2d21ac55 A |
4625 | |
4626 | if (tvp && (tvp != fvp)) { | |
4627 | /* lock the node while we rename over the existing file */ | |
4628 | lck_mtx_lock(nfs_node_hash_mutex); | |
4629 | while (tnp->n_hflag & NHLOCKED) { | |
4630 | tnp->n_hflag |= NHLOCKWANT; | |
4631 | msleep(tnp, nfs_node_hash_mutex, PINOD, "nfs_rename", NULL); | |
4632 | } | |
4633 | tnp->n_hflag |= NHLOCKED; | |
4634 | lck_mtx_unlock(nfs_node_hash_mutex); | |
4635 | locked = 1; | |
4636 | } | |
4637 | ||
1c79356b | 4638 | /* Check for cross-device rename */ |
91447636 A |
4639 | fmp = vnode_mount(fvp); |
4640 | tmp = tvp ? vnode_mount(tvp) : NULL; | |
4641 | tdmp = vnode_mount(tdvp); | |
4642 | if ((fmp != tdmp) || (tvp && (fmp != tmp))) { | |
1c79356b A |
4643 | error = EXDEV; |
4644 | goto out; | |
4645 | } | |
4646 | ||
2d21ac55 A |
4647 | /* XXX prevent renaming from/over a sillyrenamed file? */ |
4648 | ||
1c79356b A |
4649 | /* |
4650 | * If the tvp exists and is in use, sillyrename it before doing the | |
4651 | * rename of the new file over it. | |
4652 | * XXX Can't sillyrename a directory. | |
9bccf70c A |
4653 | * Don't sillyrename if source and target are same vnode (hard |
4654 | * links or case-variants) | |
1c79356b | 4655 | */ |
0a7de745 | 4656 | if (tvp && (tvp != fvp)) { |
91447636 | 4657 | inuse = vnode_isinuse(tvp, 0); |
0a7de745 | 4658 | } |
2d21ac55 A |
4659 | if (inuse && !tnp->n_sillyrename && (vnode_vtype(tvp) != VDIR)) { |
4660 | error = nfs_sillyrename(tdnp, tnp, tcnp, ctx); | |
4661 | if (error) { | |
9bccf70c A |
4662 | /* sillyrename failed. Instead of pressing on, return error */ |
4663 | goto out; /* should not be ENOENT. */ | |
4664 | } else { | |
4665 | /* sillyrename succeeded.*/ | |
9bccf70c A |
4666 | tvp = NULL; |
4667 | } | |
cb323159 A |
4668 | } |
4669 | #if CONFIG_NFS4 | |
4670 | else if (tvp && (nmp->nm_vers >= NFS_VER4) && (tnp->n_openflags & N_DELEG_MASK)) { | |
6d2010ae | 4671 | nfs4_delegation_return(tnp, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
1c79356b | 4672 | } |
cb323159 | 4673 | #endif |
2d21ac55 | 4674 | error = nmp->nm_funcs->nf_rename_rpc(fdnp, fcnp->cn_nameptr, fcnp->cn_namelen, |
0a7de745 | 4675 | tdnp, tcnp->cn_nameptr, tcnp->cn_namelen, ctx); |
1c79356b | 4676 | |
91447636 A |
4677 | /* |
4678 | * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. | |
4679 | */ | |
0a7de745 | 4680 | if (error == ENOENT) { |
91447636 | 4681 | error = 0; |
0a7de745 | 4682 | } |
91447636 | 4683 | |
2d21ac55 | 4684 | if (tvp && (tvp != fvp) && !tnp->n_sillyrename) { |
b0d623f7 | 4685 | nfs_node_lock_force(tnp); |
2d21ac55 | 4686 | tvprecycle = (!error && !vnode_isinuse(tvp, 0) && |
6d2010ae | 4687 | (nfs_getattrcache(tnp, &nvattr, 0) || (nvattr.nva_nlink == 1))); |
b0d623f7 | 4688 | nfs_node_unlock(tnp); |
91447636 | 4689 | lck_mtx_lock(nfs_node_hash_mutex); |
2d21ac55 A |
4690 | if (tvprecycle && (tnp->n_hflag & NHHASHED)) { |
4691 | /* | |
4692 | * remove nfsnode from hash now so we can't accidentally find it | |
4693 | * again if another object gets created with the same filehandle | |
4694 | * before this vnode gets reclaimed | |
4695 | */ | |
4696 | LIST_REMOVE(tnp, n_hash); | |
4697 | tnp->n_hflag &= ~NHHASHED; | |
4698 | FSDBG(266, 0, tnp, tnp->n_flag, 0xb1eb1e); | |
4699 | } | |
91447636 | 4700 | lck_mtx_unlock(nfs_node_hash_mutex); |
55e303ae | 4701 | } |
2d21ac55 | 4702 | |
91447636 | 4703 | /* purge the old name cache entries and enter the new one */ |
b0d623f7 | 4704 | nfs_name_cache_purge(fdnp, fnp, fcnp, ctx); |
9bccf70c | 4705 | if (tvp) { |
b0d623f7 | 4706 | nfs_name_cache_purge(tdnp, tnp, tcnp, ctx); |
2d21ac55 A |
4707 | if (tvprecycle) { |
4708 | /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */ | |
91447636 | 4709 | /* clear all flags other than these */ |
b0d623f7 | 4710 | nfs_node_lock_force(tnp); |
2d21ac55 | 4711 | tnp->n_flag &= (NMODIFIED); |
b0d623f7 | 4712 | nfs_node_unlock(tnp); |
91447636 A |
4713 | vnode_recycle(tvp); |
4714 | } | |
9bccf70c | 4715 | } |
2d21ac55 | 4716 | if (!error) { |
b0d623f7 | 4717 | nfs_node_lock_force(tdnp); |
2d21ac55 A |
4718 | if (tdnp->n_flag & NNEGNCENTRIES) { |
4719 | tdnp->n_flag &= ~NNEGNCENTRIES; | |
4720 | cache_purge_negatives(tdvp); | |
4721 | } | |
b0d623f7 A |
4722 | nfs_node_unlock(tdnp); |
4723 | nfs_node_lock_force(fnp); | |
91447636 | 4724 | cache_enter(tdvp, fvp, tcnp); |
0a7de745 | 4725 | if (tdvp != fdvp) { /* update parent pointer */ |
2d21ac55 A |
4726 | if (fnp->n_parent && !vnode_get(fnp->n_parent)) { |
4727 | /* remove ref from old parent */ | |
4728 | vnode_rele(fnp->n_parent); | |
4729 | vnode_put(fnp->n_parent); | |
4730 | } | |
4731 | fnp->n_parent = tdvp; | |
4732 | if (tdvp && !vnode_get(tdvp)) { | |
4733 | /* add ref to new parent */ | |
4734 | vnode_ref(tdvp); | |
4735 | vnode_put(tdvp); | |
4736 | } else { | |
4737 | fnp->n_parent = NULL; | |
4738 | } | |
4739 | } | |
b0d623f7 | 4740 | nfs_node_unlock(fnp); |
2d21ac55 | 4741 | } |
1c79356b | 4742 | out: |
b0d623f7 | 4743 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae A |
4744 | nfs_getattr(fdnp, NULL, ctx, NGA_CACHED); |
4745 | nfs_getattr(tdnp, NULL, ctx, NGA_CACHED); | |
2d21ac55 A |
4746 | if (locked) { |
4747 | /* unlock node */ | |
4748 | lck_mtx_lock(nfs_node_hash_mutex); | |
4749 | tnp->n_hflag &= ~NHLOCKED; | |
4750 | if (tnp->n_hflag & NHLOCKWANT) { | |
4751 | tnp->n_hflag &= ~NHLOCKWANT; | |
4752 | wakeup(tnp); | |
4753 | } | |
4754 | lck_mtx_unlock(nfs_node_hash_mutex); | |
4755 | } | |
b0d623f7 | 4756 | nfs_node_clear_busy4(fdnp, fnp, tdnp, tnp); |
0a7de745 | 4757 | return error; |
1c79356b A |
4758 | } |
4759 | ||
4760 | /* | |
2d21ac55 | 4761 | * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename(). |
1c79356b | 4762 | */ |
2d21ac55 A |
4763 | int |
4764 | nfs3_rename_rpc( | |
4765 | nfsnode_t fdnp, | |
4766 | char *fnameptr, | |
4767 | int fnamelen, | |
4768 | nfsnode_t tdnp, | |
4769 | char *tnameptr, | |
4770 | int tnamelen, | |
4771 | vfs_context_t ctx) | |
1c79356b | 4772 | { |
b0d623f7 | 4773 | int error = 0, lockerror = ENOENT, status, fwccpostattr = 0, twccpostattr = 0; |
cb323159 | 4774 | struct timespec fpremtime = { .tv_sec = 0, .tv_nsec = 0 }, tpremtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4775 | struct nfsmount *nmp; |
4776 | int nfsvers; | |
4777 | u_int64_t xid, txid; | |
4778 | struct nfsm_chain nmreq, nmrep; | |
1c79356b | 4779 | |
2d21ac55 | 4780 | nmp = NFSTONMP(fdnp); |
0a7de745 A |
4781 | if (nfs_mount_gone(nmp)) { |
4782 | return ENXIO; | |
4783 | } | |
2d21ac55 A |
4784 | nfsvers = nmp->nm_vers; |
4785 | if ((nfsvers == NFS_VER2) && | |
0a7de745 A |
4786 | ((fnamelen > NFS_MAXNAMLEN) || (tnamelen > NFS_MAXNAMLEN))) { |
4787 | return ENAMETOOLONG; | |
4788 | } | |
2d21ac55 A |
4789 | |
4790 | nfsm_chain_null(&nmreq); | |
4791 | nfsm_chain_null(&nmrep); | |
4792 | ||
4793 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
4794 | (NFSX_FH(nfsvers) + NFSX_UNSIGNED) * 2 + |
4795 | nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen)); | |
2d21ac55 | 4796 | nfsm_chain_add_fh(error, &nmreq, nfsvers, fdnp->n_fhp, fdnp->n_fhsize); |
6d2010ae | 4797 | nfsm_chain_add_name(error, &nmreq, fnameptr, fnamelen, nmp); |
2d21ac55 | 4798 | nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize); |
6d2010ae | 4799 | nfsm_chain_add_name(error, &nmreq, tnameptr, tnamelen, nmp); |
2d21ac55 A |
4800 | nfsm_chain_build_done(error, &nmreq); |
4801 | nfsmout_if(error); | |
4802 | ||
6d2010ae | 4803 | error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, NULL, &nmrep, &xid, &status); |
2d21ac55 | 4804 | |
0a7de745 | 4805 | if ((lockerror = nfs_node_lock2(fdnp, tdnp))) { |
b0d623f7 | 4806 | error = lockerror; |
0a7de745 | 4807 | } |
2d21ac55 A |
4808 | if (nfsvers == NFS_VER3) { |
4809 | txid = xid; | |
4810 | nfsm_chain_get_wcc_data(error, &nmrep, fdnp, &fpremtime, &fwccpostattr, &xid); | |
4811 | nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &tpremtime, &twccpostattr, &txid); | |
4812 | } | |
0a7de745 | 4813 | if (!error) { |
2d21ac55 | 4814 | error = status; |
0a7de745 | 4815 | } |
2d21ac55 A |
4816 | nfsmout: |
4817 | nfsm_chain_cleanup(&nmreq); | |
4818 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 A |
4819 | if (!lockerror) { |
4820 | fdnp->n_flag |= NMODIFIED; | |
4821 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4822 | if (nfstimespeccmp(&fdnp->n_ncmtime, &fpremtime, ==)) { |
b0d623f7 | 4823 | NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &fdnp->n_vattr); |
0a7de745 A |
4824 | } |
4825 | if (!fwccpostattr) { | |
b0d623f7 | 4826 | NATTRINVALIDATE(fdnp); |
0a7de745 | 4827 | } |
b0d623f7 A |
4828 | tdnp->n_flag |= NMODIFIED; |
4829 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4830 | if (nfstimespeccmp(&tdnp->n_ncmtime, &tpremtime, ==)) { |
b0d623f7 | 4831 | NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr); |
0a7de745 A |
4832 | } |
4833 | if (!twccpostattr) { | |
b0d623f7 | 4834 | NATTRINVALIDATE(tdnp); |
0a7de745 | 4835 | } |
b0d623f7 A |
4836 | nfs_node_unlock2(fdnp, tdnp); |
4837 | } | |
0a7de745 | 4838 | return error; |
1c79356b A |
4839 | } |
4840 | ||
4841 | /* | |
2d21ac55 | 4842 | * NFS hard link create call |
1c79356b | 4843 | */ |
b0d623f7 | 4844 | int |
2d21ac55 | 4845 | nfs3_vnop_link( |
91447636 | 4846 | struct vnop_link_args /* { |
0a7de745 A |
4847 | * struct vnodeop_desc *a_desc; |
4848 | * vnode_t a_vp; | |
4849 | * vnode_t a_tdvp; | |
4850 | * struct componentname *a_cnp; | |
4851 | * vfs_context_t a_context; | |
4852 | * } */*ap) | |
1c79356b | 4853 | { |
2d21ac55 | 4854 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4855 | vnode_t vp = ap->a_vp; |
4856 | vnode_t tdvp = ap->a_tdvp; | |
4857 | struct componentname *cnp = ap->a_cnp; | |
b0d623f7 | 4858 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0, attrflag = 0; |
cb323159 | 4859 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4860 | struct nfsmount *nmp; |
4861 | nfsnode_t np = VTONFS(vp); | |
4862 | nfsnode_t tdnp = VTONFS(tdvp); | |
4863 | int nfsvers; | |
4864 | u_int64_t xid, txid; | |
4865 | struct nfsm_chain nmreq, nmrep; | |
1c79356b | 4866 | |
0a7de745 A |
4867 | if (vnode_mount(vp) != vnode_mount(tdvp)) { |
4868 | return EXDEV; | |
4869 | } | |
1c79356b | 4870 | |
2d21ac55 | 4871 | nmp = VTONMP(vp); |
0a7de745 A |
4872 | if (nfs_mount_gone(nmp)) { |
4873 | return ENXIO; | |
4874 | } | |
2d21ac55 | 4875 | nfsvers = nmp->nm_vers; |
0a7de745 A |
4876 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
4877 | return ENAMETOOLONG; | |
4878 | } | |
55e303ae | 4879 | |
1c79356b A |
4880 | /* |
4881 | * Push all writes to the server, so that the attribute cache | |
4882 | * doesn't get "out of sync" with the server. | |
4883 | * XXX There should be a better way! | |
4884 | */ | |
2d21ac55 | 4885 | nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), V_IGNORE_WRITEERR); |
1c79356b | 4886 | |
b0d623f7 | 4887 | error = nfs_node_set_busy2(tdnp, np, vfs_context_thread(ctx)); |
0a7de745 A |
4888 | if (error) { |
4889 | return error; | |
4890 | } | |
fa4905b1 | 4891 | |
2d21ac55 A |
4892 | nfsm_chain_null(&nmreq); |
4893 | nfsm_chain_null(&nmrep); | |
4894 | ||
4895 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 4896 | NFSX_FH(nfsvers) * 2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); |
2d21ac55 A |
4897 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); |
4898 | nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize); | |
6d2010ae | 4899 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4900 | nfsm_chain_build_done(error, &nmreq); |
4901 | nfsmout_if(error); | |
6d2010ae | 4902 | error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx, NULL, &nmrep, &xid, &status); |
b0d623f7 A |
4903 | |
4904 | if ((lockerror = nfs_node_lock2(tdnp, np))) { | |
4905 | error = lockerror; | |
4906 | goto nfsmout; | |
4907 | } | |
2d21ac55 A |
4908 | if (nfsvers == NFS_VER3) { |
4909 | txid = xid; | |
4910 | nfsm_chain_postop_attr_update_flag(error, &nmrep, np, attrflag, &xid); | |
4911 | nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &premtime, &wccpostattr, &txid); | |
1c79356b | 4912 | } |
0a7de745 | 4913 | if (!error) { |
2d21ac55 | 4914 | error = status; |
0a7de745 | 4915 | } |
2d21ac55 A |
4916 | nfsmout: |
4917 | nfsm_chain_cleanup(&nmreq); | |
4918 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 | 4919 | if (!lockerror) { |
0a7de745 | 4920 | if (!attrflag) { |
b0d623f7 | 4921 | NATTRINVALIDATE(np); |
0a7de745 | 4922 | } |
b0d623f7 A |
4923 | tdnp->n_flag |= NMODIFIED; |
4924 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4925 | if (nfstimespeccmp(&tdnp->n_ncmtime, &premtime, ==)) { |
b0d623f7 | 4926 | NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr); |
0a7de745 A |
4927 | } |
4928 | if (!wccpostattr) { | |
b0d623f7 | 4929 | NATTRINVALIDATE(tdnp); |
0a7de745 | 4930 | } |
b0d623f7 A |
4931 | if (!error && (tdnp->n_flag & NNEGNCENTRIES)) { |
4932 | tdnp->n_flag &= ~NNEGNCENTRIES; | |
4933 | cache_purge_negatives(tdvp); | |
4934 | } | |
4935 | nfs_node_unlock2(tdnp, np); | |
2d21ac55 | 4936 | } |
b0d623f7 | 4937 | nfs_node_clear_busy2(tdnp, np); |
1c79356b A |
4938 | /* |
4939 | * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. | |
4940 | */ | |
0a7de745 | 4941 | if (error == EEXIST) { |
1c79356b | 4942 | error = 0; |
0a7de745 A |
4943 | } |
4944 | return error; | |
1c79356b A |
4945 | } |
4946 | ||
4947 | /* | |
2d21ac55 | 4948 | * NFS symbolic link create call |
1c79356b | 4949 | */ |
b0d623f7 | 4950 | int |
2d21ac55 | 4951 | nfs3_vnop_symlink( |
91447636 | 4952 | struct vnop_symlink_args /* { |
0a7de745 A |
4953 | * struct vnodeop_desc *a_desc; |
4954 | * vnode_t a_dvp; | |
4955 | * vnode_t *a_vpp; | |
4956 | * struct componentname *a_cnp; | |
4957 | * struct vnode_attr *a_vap; | |
4958 | * char *a_target; | |
4959 | * vfs_context_t a_context; | |
4960 | * } */*ap) | |
1c79356b | 4961 | { |
2d21ac55 | 4962 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4963 | vnode_t dvp = ap->a_dvp; |
4964 | struct vnode_attr *vap = ap->a_vap; | |
4965 | struct componentname *cnp = ap->a_cnp; | |
6d2010ae | 4966 | struct nfs_vattr nvattr; |
2d21ac55 | 4967 | fhandle_t fh; |
b0d623f7 | 4968 | int slen, error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0; |
cb323159 | 4969 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4970 | vnode_t newvp = NULL; |
4971 | int nfsvers, gotuid, gotgid; | |
39236c6e | 4972 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
4973 | nfsnode_t np = NULL; |
4974 | nfsnode_t dnp = VTONFS(dvp); | |
4975 | struct nfsmount *nmp; | |
4976 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 4977 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 4978 | struct nfs_dulookup dul; |
cb323159 A |
4979 | int namedattrs; |
4980 | int dul_in_progress = 0; | |
91447636 | 4981 | |
2d21ac55 | 4982 | nmp = VTONMP(dvp); |
0a7de745 A |
4983 | if (nfs_mount_gone(nmp)) { |
4984 | return ENXIO; | |
4985 | } | |
2d21ac55 | 4986 | nfsvers = nmp->nm_vers; |
cb323159 | 4987 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
1c79356b | 4988 | |
1c79356b | 4989 | slen = strlen(ap->a_target); |
2d21ac55 | 4990 | if ((nfsvers == NFS_VER2) && |
0a7de745 A |
4991 | ((cnp->cn_namelen > NFS_MAXNAMLEN) || (slen > NFS_MAXPATHLEN))) { |
4992 | return ENAMETOOLONG; | |
4993 | } | |
91447636 | 4994 | |
6d2010ae A |
4995 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
4996 | ||
91447636 A |
4997 | VATTR_SET_SUPPORTED(vap, va_mode); |
4998 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4999 | VATTR_SET_SUPPORTED(vap, va_gid); | |
5000 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
5001 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
5002 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
5003 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
5004 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
5005 | ||
b0d623f7 | 5006 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
cb323159 A |
5007 | if (!namedattrs) { |
5008 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
5009 | } | |
2d21ac55 A |
5010 | |
5011 | nfsm_chain_null(&nmreq); | |
5012 | nfsm_chain_null(&nmrep); | |
5013 | ||
5014 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
5015 | NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED + |
5016 | nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 5017 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 5018 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
0a7de745 | 5019 | if (nfsvers == NFS_VER3) { |
cb323159 | 5020 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
0a7de745 | 5021 | } |
6d2010ae | 5022 | nfsm_chain_add_name(error, &nmreq, ap->a_target, slen, nmp); |
0a7de745 | 5023 | if (nfsvers == NFS_VER2) { |
2d21ac55 | 5024 | nfsm_chain_add_v2sattr(error, &nmreq, vap, -1); |
0a7de745 | 5025 | } |
2d21ac55 A |
5026 | nfsm_chain_build_done(error, &nmreq); |
5027 | nfsmout_if(error); | |
2d21ac55 A |
5028 | |
5029 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_SYMLINK, | |
0a7de745 | 5030 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 | 5031 | if (!error) { |
cb323159 A |
5032 | if (!namedattrs) { |
5033 | nfs_dulookup_start(&dul, dnp, ctx); | |
5034 | dul_in_progress = 1; | |
5035 | } | |
2d21ac55 A |
5036 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
5037 | } | |
5038 | ||
0a7de745 | 5039 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 5040 | error = lockerror; |
0a7de745 | 5041 | } |
2d21ac55 A |
5042 | dxid = xid; |
5043 | if (!error && !status) { | |
5044 | if (dnp->n_flag & NNEGNCENTRIES) { | |
5045 | dnp->n_flag &= ~NNEGNCENTRIES; | |
5046 | cache_purge_negatives(dvp); | |
91447636 | 5047 | } |
0a7de745 | 5048 | if (nfsvers == NFS_VER3) { |
cb323159 | 5049 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); |
0a7de745 | 5050 | } else { |
2d21ac55 | 5051 | fh.fh_len = 0; |
0a7de745 | 5052 | } |
1c79356b | 5053 | } |
0a7de745 | 5054 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 5055 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
5056 | } |
5057 | if (!error) { | |
2d21ac55 | 5058 | error = status; |
0a7de745 | 5059 | } |
2d21ac55 A |
5060 | nfsmout: |
5061 | nfsm_chain_cleanup(&nmreq); | |
5062 | nfsm_chain_cleanup(&nmrep); | |
fa4905b1 | 5063 | |
2d21ac55 A |
5064 | if (!lockerror) { |
5065 | dnp->n_flag |= NMODIFIED; | |
5066 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5067 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 5068 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 5069 | } |
b0d623f7 A |
5070 | nfs_node_unlock(dnp); |
5071 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5072 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 5073 | } |
55e303ae | 5074 | |
0a7de745 | 5075 | if (!error && fh.fh_len) { |
6d2010ae | 5076 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
0a7de745 A |
5077 | } |
5078 | if (!error && np) { | |
2d21ac55 | 5079 | newvp = NFSTOV(np); |
0a7de745 | 5080 | } |
2d21ac55 | 5081 | |
cb323159 A |
5082 | if (dul_in_progress) { |
5083 | nfs_dulookup_finish(&dul, dnp, ctx); | |
5084 | } | |
91447636 | 5085 | |
1c79356b | 5086 | /* |
91447636 A |
5087 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry |
5088 | * if we can succeed in looking up the symlink. | |
1c79356b | 5089 | */ |
2d21ac55 | 5090 | if ((error == EEXIST) || (!error && !newvp)) { |
91447636 | 5091 | if (newvp) { |
b0d623f7 | 5092 | nfs_node_unlock(np); |
91447636 A |
5093 | vnode_put(newvp); |
5094 | newvp = NULL; | |
5095 | } | |
2d21ac55 | 5096 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
91447636 A |
5097 | if (!error) { |
5098 | newvp = NFSTOV(np); | |
0a7de745 | 5099 | if (vnode_vtype(newvp) != VLNK) { |
91447636 | 5100 | error = EEXIST; |
0a7de745 | 5101 | } |
91447636 A |
5102 | } |
5103 | } | |
0a7de745 | 5104 | if (!busyerror) { |
b0d623f7 | 5105 | nfs_node_clear_busy(dnp); |
0a7de745 | 5106 | } |
91447636 | 5107 | if (!error && (gotuid || gotgid) && |
6d2010ae | 5108 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
0a7de745 A |
5109 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
5110 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
91447636 A |
5111 | /* clear ID bits if server didn't use them (or we can't tell) */ |
5112 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
5113 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
5114 | } | |
5115 | if (error) { | |
2d21ac55 | 5116 | if (newvp) { |
b0d623f7 | 5117 | nfs_node_unlock(np); |
91447636 | 5118 | vnode_put(newvp); |
2d21ac55 | 5119 | } |
91447636 | 5120 | } else { |
b0d623f7 | 5121 | nfs_node_unlock(np); |
91447636 A |
5122 | *ap->a_vpp = newvp; |
5123 | } | |
0a7de745 | 5124 | return error; |
1c79356b A |
5125 | } |
5126 | ||
5127 | /* | |
2d21ac55 | 5128 | * NFS make dir call |
1c79356b | 5129 | */ |
b0d623f7 | 5130 | int |
2d21ac55 | 5131 | nfs3_vnop_mkdir( |
91447636 | 5132 | struct vnop_mkdir_args /* { |
0a7de745 A |
5133 | * struct vnodeop_desc *a_desc; |
5134 | * vnode_t a_dvp; | |
5135 | * vnode_t *a_vpp; | |
5136 | * struct componentname *a_cnp; | |
5137 | * struct vnode_attr *a_vap; | |
5138 | * vfs_context_t a_context; | |
5139 | * } */*ap) | |
1c79356b | 5140 | { |
2d21ac55 | 5141 | vfs_context_t ctx = ap->a_context; |
91447636 A |
5142 | vnode_t dvp = ap->a_dvp; |
5143 | struct vnode_attr *vap = ap->a_vap; | |
5144 | struct componentname *cnp = ap->a_cnp; | |
6d2010ae | 5145 | struct nfs_vattr nvattr; |
2d21ac55 A |
5146 | nfsnode_t np = NULL; |
5147 | struct nfsmount *nmp; | |
5148 | nfsnode_t dnp = VTONFS(dvp); | |
5149 | vnode_t newvp = NULL; | |
b0d623f7 | 5150 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0; |
cb323159 | 5151 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 | 5152 | int nfsvers, gotuid, gotgid; |
0a7de745 | 5153 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
5154 | fhandle_t fh; |
5155 | struct nfsm_chain nmreq, nmrep; | |
6d2010ae | 5156 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 5157 | struct nfs_dulookup dul; |
cb323159 A |
5158 | int namedattrs; |
5159 | int dul_in_progress = 0; | |
1c79356b | 5160 | |
2d21ac55 | 5161 | nmp = VTONMP(dvp); |
0a7de745 A |
5162 | if (nfs_mount_gone(nmp)) { |
5163 | return ENXIO; | |
5164 | } | |
2d21ac55 | 5165 | nfsvers = nmp->nm_vers; |
cb323159 A |
5166 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
5167 | ||
0a7de745 A |
5168 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
5169 | return ENAMETOOLONG; | |
5170 | } | |
91447636 | 5171 | |
6d2010ae A |
5172 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
5173 | ||
91447636 A |
5174 | VATTR_SET_SUPPORTED(vap, va_mode); |
5175 | VATTR_SET_SUPPORTED(vap, va_uid); | |
5176 | VATTR_SET_SUPPORTED(vap, va_gid); | |
5177 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
5178 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
5179 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
5180 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
5181 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
5182 | ||
b0d623f7 | 5183 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
cb323159 A |
5184 | if (!namedattrs) { |
5185 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
5186 | } | |
2d21ac55 A |
5187 | |
5188 | nfsm_chain_null(&nmreq); | |
5189 | nfsm_chain_null(&nmrep); | |
5190 | ||
5191 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
5192 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + |
5193 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 5194 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 5195 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
0a7de745 | 5196 | if (nfsvers == NFS_VER3) { |
cb323159 | 5197 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
0a7de745 | 5198 | } else { |
2d21ac55 | 5199 | nfsm_chain_add_v2sattr(error, &nmreq, vap, -1); |
0a7de745 | 5200 | } |
2d21ac55 A |
5201 | nfsm_chain_build_done(error, &nmreq); |
5202 | nfsmout_if(error); | |
2d21ac55 A |
5203 | |
5204 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKDIR, | |
0a7de745 | 5205 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 | 5206 | if (!error) { |
cb323159 A |
5207 | if (!namedattrs) { |
5208 | nfs_dulookup_start(&dul, dnp, ctx); | |
5209 | dul_in_progress = 1; | |
5210 | } | |
2d21ac55 | 5211 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
1c79356b | 5212 | } |
2d21ac55 | 5213 | |
0a7de745 | 5214 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 5215 | error = lockerror; |
0a7de745 | 5216 | } |
fa4905b1 | 5217 | dxid = xid; |
2d21ac55 A |
5218 | if (!error && !status) { |
5219 | if (dnp->n_flag & NNEGNCENTRIES) { | |
5220 | dnp->n_flag &= ~NNEGNCENTRIES; | |
5221 | cache_purge_negatives(dvp); | |
5222 | } | |
cb323159 | 5223 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr); |
2d21ac55 | 5224 | } |
0a7de745 | 5225 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 5226 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
5227 | } |
5228 | if (!error) { | |
2d21ac55 | 5229 | error = status; |
0a7de745 | 5230 | } |
2d21ac55 A |
5231 | nfsmout: |
5232 | nfsm_chain_cleanup(&nmreq); | |
5233 | nfsm_chain_cleanup(&nmrep); | |
5234 | ||
5235 | if (!lockerror) { | |
5236 | dnp->n_flag |= NMODIFIED; | |
5237 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5238 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 5239 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 5240 | } |
b0d623f7 A |
5241 | nfs_node_unlock(dnp); |
5242 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5243 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
2d21ac55 A |
5244 | } |
5245 | ||
0a7de745 | 5246 | if (!error && fh.fh_len) { |
6d2010ae | 5247 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
0a7de745 A |
5248 | } |
5249 | if (!error && np) { | |
2d21ac55 | 5250 | newvp = NFSTOV(np); |
0a7de745 | 5251 | } |
2d21ac55 | 5252 | |
cb323159 A |
5253 | if (dul_in_progress) { |
5254 | nfs_dulookup_finish(&dul, dnp, ctx); | |
5255 | } | |
2d21ac55 | 5256 | |
1c79356b A |
5257 | /* |
5258 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry | |
5259 | * if we can succeed in looking up the directory. | |
5260 | */ | |
b0d623f7 | 5261 | if ((error == EEXIST) || (!error && !newvp)) { |
1c79356b | 5262 | if (newvp) { |
b0d623f7 | 5263 | nfs_node_unlock(np); |
91447636 A |
5264 | vnode_put(newvp); |
5265 | newvp = NULL; | |
1c79356b | 5266 | } |
2d21ac55 | 5267 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
1c79356b A |
5268 | if (!error) { |
5269 | newvp = NFSTOV(np); | |
0a7de745 | 5270 | if (vnode_vtype(newvp) != VDIR) { |
1c79356b | 5271 | error = EEXIST; |
0a7de745 | 5272 | } |
1c79356b A |
5273 | } |
5274 | } | |
0a7de745 | 5275 | if (!busyerror) { |
b0d623f7 | 5276 | nfs_node_clear_busy(dnp); |
0a7de745 | 5277 | } |
91447636 | 5278 | if (!error && (gotuid || gotgid) && |
6d2010ae | 5279 | (!newvp || nfs_getattrcache(np, &nvattr, 0) || |
0a7de745 A |
5280 | (gotuid && (nvattr.nva_uid != vap->va_uid)) || |
5281 | (gotgid && (nvattr.nva_gid != vap->va_gid)))) { | |
91447636 A |
5282 | /* clear ID bits if server didn't use them (or we can't tell) */ |
5283 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
5284 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
5285 | } | |
1c79356b | 5286 | if (error) { |
2d21ac55 | 5287 | if (newvp) { |
b0d623f7 | 5288 | nfs_node_unlock(np); |
91447636 | 5289 | vnode_put(newvp); |
2d21ac55 | 5290 | } |
483a1d10 | 5291 | } else { |
b0d623f7 | 5292 | nfs_node_unlock(np); |
1c79356b | 5293 | *ap->a_vpp = newvp; |
483a1d10 | 5294 | } |
0a7de745 | 5295 | return error; |
1c79356b A |
5296 | } |
5297 | ||
5298 | /* | |
2d21ac55 | 5299 | * NFS remove directory call |
1c79356b | 5300 | */ |
b0d623f7 | 5301 | int |
2d21ac55 | 5302 | nfs3_vnop_rmdir( |
91447636 | 5303 | struct vnop_rmdir_args /* { |
0a7de745 A |
5304 | * struct vnodeop_desc *a_desc; |
5305 | * vnode_t a_dvp; | |
5306 | * vnode_t a_vp; | |
5307 | * struct componentname *a_cnp; | |
5308 | * vfs_context_t a_context; | |
5309 | * } */*ap) | |
1c79356b | 5310 | { |
2d21ac55 | 5311 | vfs_context_t ctx = ap->a_context; |
91447636 A |
5312 | vnode_t vp = ap->a_vp; |
5313 | vnode_t dvp = ap->a_dvp; | |
5314 | struct componentname *cnp = ap->a_cnp; | |
b0d623f7 | 5315 | int error = 0, lockerror = ENOENT, status, wccpostattr = 0; |
cb323159 | 5316 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
5317 | struct nfsmount *nmp; |
5318 | nfsnode_t np = VTONFS(vp); | |
5319 | nfsnode_t dnp = VTONFS(dvp); | |
2d21ac55 | 5320 | int nfsvers; |
fa4905b1 | 5321 | u_int64_t xid; |
2d21ac55 | 5322 | struct nfsm_chain nmreq, nmrep; |
6d2010ae | 5323 | struct nfsreq rq, *req = &rq; |
2d21ac55 | 5324 | struct nfs_dulookup dul; |
cb323159 A |
5325 | int namedattrs; |
5326 | int dul_in_progress = 0; | |
1c79356b | 5327 | |
2d21ac55 | 5328 | nmp = VTONMP(vp); |
0a7de745 A |
5329 | if (nfs_mount_gone(nmp)) { |
5330 | return ENXIO; | |
5331 | } | |
2d21ac55 | 5332 | nfsvers = nmp->nm_vers; |
cb323159 A |
5333 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
5334 | ||
0a7de745 A |
5335 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
5336 | return ENAMETOOLONG; | |
5337 | } | |
91447636 | 5338 | |
0a7de745 A |
5339 | if ((error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx)))) { |
5340 | return error; | |
5341 | } | |
2d21ac55 | 5342 | |
cb323159 A |
5343 | if (!namedattrs) { |
5344 | nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); | |
5345 | } | |
b0d623f7 | 5346 | |
2d21ac55 A |
5347 | nfsm_chain_null(&nmreq); |
5348 | nfsm_chain_null(&nmrep); | |
5349 | ||
5350 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 5351 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); |
2d21ac55 | 5352 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 5353 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
5354 | nfsm_chain_build_done(error, &nmreq); |
5355 | nfsmout_if(error); | |
5356 | ||
5357 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_RMDIR, | |
0a7de745 | 5358 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 | 5359 | if (!error) { |
cb323159 A |
5360 | if (!namedattrs) { |
5361 | nfs_dulookup_start(&dul, dnp, ctx); | |
5362 | dul_in_progress = 1; | |
5363 | } | |
2d21ac55 A |
5364 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
5365 | } | |
5366 | ||
0a7de745 | 5367 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 5368 | error = lockerror; |
0a7de745 A |
5369 | } |
5370 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 5371 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid); |
0a7de745 A |
5372 | } |
5373 | if (!error) { | |
2d21ac55 | 5374 | error = status; |
0a7de745 | 5375 | } |
2d21ac55 A |
5376 | nfsmout: |
5377 | nfsm_chain_cleanup(&nmreq); | |
5378 | nfsm_chain_cleanup(&nmrep); | |
5379 | ||
b0d623f7 A |
5380 | if (!lockerror) { |
5381 | dnp->n_flag |= NMODIFIED; | |
5382 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5383 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
b0d623f7 | 5384 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 5385 | } |
b0d623f7 A |
5386 | nfs_node_unlock(dnp); |
5387 | nfs_name_cache_purge(dnp, np, cnp, ctx); | |
5388 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5389 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
2d21ac55 | 5390 | } |
cb323159 A |
5391 | if (dul_in_progress) { |
5392 | nfs_dulookup_finish(&dul, dnp, ctx); | |
5393 | } | |
b0d623f7 | 5394 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 | 5395 | |
1c79356b A |
5396 | /* |
5397 | * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. | |
5398 | */ | |
0a7de745 | 5399 | if (error == ENOENT) { |
1c79356b | 5400 | error = 0; |
0a7de745 | 5401 | } |
483a1d10 A |
5402 | if (!error) { |
5403 | /* | |
5404 | * remove nfsnode from hash now so we can't accidentally find it | |
5405 | * again if another object gets created with the same filehandle | |
5406 | * before this vnode gets reclaimed | |
5407 | */ | |
91447636 | 5408 | lck_mtx_lock(nfs_node_hash_mutex); |
2d21ac55 A |
5409 | if (np->n_hflag & NHHASHED) { |
5410 | LIST_REMOVE(np, n_hash); | |
5411 | np->n_hflag &= ~NHHASHED; | |
5412 | FSDBG(266, 0, np, np->n_flag, 0xb1eb1e); | |
5413 | } | |
91447636 | 5414 | lck_mtx_unlock(nfs_node_hash_mutex); |
483a1d10 | 5415 | } |
0a7de745 | 5416 | return error; |
1c79356b A |
5417 | } |
5418 | ||
5419 | /* | |
2d21ac55 | 5420 | * NFS readdir call |
b0d623f7 A |
5421 | * |
5422 | * The incoming "offset" is a directory cookie indicating where in the | |
5423 | * directory entries should be read from. A zero cookie means start at | |
5424 | * the beginning of the directory. Any other cookie will be a cookie | |
0a7de745 | 5425 | * returned from the server. |
b0d623f7 A |
5426 | * |
5427 | * Using that cookie, determine which buffer (and where in that buffer) | |
5428 | * to start returning entries from. Buffer logical block numbers are | |
5429 | * the cookies they start at. If a buffer is found that is not full, | |
5430 | * call into the bio/RPC code to fill it. The RPC code will probably | |
5431 | * fill several buffers (dropping the first, requiring a re-get). | |
5432 | * | |
5433 | * When done copying entries to the buffer, set the offset to the current | |
5434 | * entry's cookie and enter that cookie in the cookie cache. | |
5435 | * | |
5436 | * Note: because the getdirentries(2) API returns a long-typed offset, | |
5437 | * the incoming offset is a potentially truncated cookie (ptc). | |
5438 | * The cookie matching code is aware of this and will fall back to | |
5439 | * matching only 32 bits of the cookie. | |
1c79356b | 5440 | */ |
b0d623f7 | 5441 | int |
2d21ac55 | 5442 | nfs_vnop_readdir( |
91447636 | 5443 | struct vnop_readdir_args /* { |
0a7de745 A |
5444 | * struct vnodeop_desc *a_desc; |
5445 | * vnode_t a_vp; | |
5446 | * struct uio *a_uio; | |
5447 | * int a_flags; | |
5448 | * int *a_eofflag; | |
5449 | * int *a_numdirent; | |
5450 | * vfs_context_t a_context; | |
5451 | * } */*ap) | |
1c79356b | 5452 | { |
2d21ac55 | 5453 | vfs_context_t ctx = ap->a_context; |
b0d623f7 A |
5454 | vnode_t dvp = ap->a_vp; |
5455 | nfsnode_t dnp = VTONFS(dvp); | |
2d21ac55 | 5456 | struct nfsmount *nmp; |
b0d623f7 A |
5457 | uio_t uio = ap->a_uio; |
5458 | int error, nfsvers, extended, numdirent, bigcookies, ptc, done; | |
b0d623f7 A |
5459 | uint16_t i, iptc, rlen, nlen; |
5460 | uint64_t cookie, nextcookie, lbn = 0; | |
5461 | struct nfsbuf *bp = NULL; | |
5462 | struct nfs_dir_buf_header *ndbhp; | |
5463 | struct direntry *dp, *dpptc; | |
5464 | struct dirent dent; | |
5465 | char *cp = NULL; | |
5466 | thread_t thd; | |
1c79356b | 5467 | |
b0d623f7 | 5468 | nmp = VTONMP(dvp); |
0a7de745 A |
5469 | if (nfs_mount_gone(nmp)) { |
5470 | return ENXIO; | |
5471 | } | |
2d21ac55 | 5472 | nfsvers = nmp->nm_vers; |
b0d623f7 A |
5473 | bigcookies = (nmp->nm_state & NFSSTA_BIGCOOKIES); |
5474 | extended = (ap->a_flags & VNODE_READDIR_EXTENDED); | |
2d21ac55 | 5475 | |
0a7de745 A |
5476 | if (vnode_vtype(dvp) != VDIR) { |
5477 | return EPERM; | |
5478 | } | |
b0d623f7 | 5479 | |
0a7de745 | 5480 | if (ap->a_eofflag) { |
b0d623f7 | 5481 | *ap->a_eofflag = 0; |
0a7de745 | 5482 | } |
b0d623f7 | 5483 | |
0a7de745 A |
5484 | if (uio_resid(uio) == 0) { |
5485 | return 0; | |
5486 | } | |
cb323159 | 5487 | #if CONFIG_NFS4 |
6d2010ae A |
5488 | if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) { |
5489 | /* trigger directories should never be read, return nothing */ | |
0a7de745 | 5490 | return 0; |
6d2010ae | 5491 | } |
cb323159 | 5492 | #endif |
b0d623f7 A |
5493 | thd = vfs_context_thread(ctx); |
5494 | numdirent = done = 0; | |
5495 | nextcookie = uio_offset(uio); | |
5496 | ptc = bigcookies && NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie); | |
5497 | ||
0a7de745 | 5498 | if ((error = nfs_node_lock(dnp))) { |
b0d623f7 | 5499 | goto out; |
0a7de745 | 5500 | } |
b0d623f7 A |
5501 | |
5502 | if (dnp->n_flag & NNEEDINVALIDATE) { | |
5503 | dnp->n_flag &= ~NNEEDINVALIDATE; | |
5504 | nfs_invaldir(dnp); | |
5505 | nfs_node_unlock(dnp); | |
5506 | error = nfs_vinvalbuf(dvp, 0, ctx, 1); | |
0a7de745 | 5507 | if (!error) { |
b0d623f7 | 5508 | error = nfs_node_lock(dnp); |
0a7de745 A |
5509 | } |
5510 | if (error) { | |
b0d623f7 | 5511 | goto out; |
0a7de745 | 5512 | } |
b0d623f7 | 5513 | } |
91447636 | 5514 | |
1c79356b | 5515 | /* |
b0d623f7 | 5516 | * check for need to invalidate when (re)starting at beginning |
1c79356b | 5517 | */ |
b0d623f7 A |
5518 | if (!nextcookie) { |
5519 | if (dnp->n_flag & NMODIFIED) { | |
5520 | nfs_invaldir(dnp); | |
5521 | nfs_node_unlock(dnp); | |
0a7de745 | 5522 | if ((error = nfs_vinvalbuf(dvp, 0, ctx, 1))) { |
b0d623f7 | 5523 | goto out; |
0a7de745 | 5524 | } |
b0d623f7 A |
5525 | } else { |
5526 | nfs_node_unlock(dnp); | |
5527 | } | |
5528 | /* nfs_getattr() will check changed and purge caches */ | |
0a7de745 | 5529 | if ((error = nfs_getattr(dnp, NULL, ctx, NGA_UNCACHED))) { |
b0d623f7 | 5530 | goto out; |
0a7de745 | 5531 | } |
b0d623f7 A |
5532 | } else { |
5533 | nfs_node_unlock(dnp); | |
5534 | } | |
5535 | ||
5536 | error = nfs_dir_cookie_to_lbn(dnp, nextcookie, &ptc, &lbn); | |
5537 | if (error) { | |
5538 | if (error < 0) { /* just hit EOF cookie */ | |
5539 | done = 1; | |
5540 | error = 0; | |
5541 | } | |
0a7de745 | 5542 | if (ap->a_eofflag) { |
b0d623f7 | 5543 | *ap->a_eofflag = 1; |
0a7de745 | 5544 | } |
b0d623f7 A |
5545 | } |
5546 | ||
5547 | while (!error && !done) { | |
316670eb | 5548 | OSAddAtomic64(1, &nfsstats.biocache_readdirs); |
b0d623f7 A |
5549 | cookie = nextcookie; |
5550 | getbuffer: | |
5551 | error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ, &bp); | |
0a7de745 | 5552 | if (error) { |
b0d623f7 | 5553 | goto out; |
0a7de745 | 5554 | } |
b0d623f7 A |
5555 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; |
5556 | if (!ISSET(bp->nb_flags, NB_CACHE) || !ISSET(ndbhp->ndbh_flags, NDB_FULL)) { | |
5557 | if (!ISSET(bp->nb_flags, NB_CACHE)) { /* initialize the buffer */ | |
5558 | ndbhp->ndbh_flags = 0; | |
5559 | ndbhp->ndbh_count = 0; | |
5560 | ndbhp->ndbh_entry_end = sizeof(*ndbhp); | |
5561 | ndbhp->ndbh_ncgen = dnp->n_ncgen; | |
5562 | } | |
5563 | error = nfs_buf_readdir(bp, ctx); | |
0a7de745 | 5564 | if (error == NFSERR_DIRBUFDROPPED) { |
b0d623f7 | 5565 | goto getbuffer; |
0a7de745 A |
5566 | } |
5567 | if (error) { | |
b0d623f7 | 5568 | nfs_buf_release(bp, 1); |
0a7de745 | 5569 | } |
b0d623f7 A |
5570 | if (error && (error != ENXIO) && (error != ETIMEDOUT) && (error != EINTR) && (error != ERESTART)) { |
5571 | if (!nfs_node_lock(dnp)) { | |
5572 | nfs_invaldir(dnp); | |
5573 | nfs_node_unlock(dnp); | |
5574 | } | |
5575 | nfs_vinvalbuf(dvp, 0, ctx, 1); | |
0a7de745 | 5576 | if (error == NFSERR_BAD_COOKIE) { |
b0d623f7 | 5577 | error = ENOENT; |
0a7de745 | 5578 | } |
b0d623f7 | 5579 | } |
0a7de745 | 5580 | if (error) { |
b0d623f7 | 5581 | goto out; |
0a7de745 | 5582 | } |
b0d623f7 A |
5583 | } |
5584 | ||
5585 | /* find next entry to return */ | |
5586 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5587 | i = 0; | |
5588 | if ((lbn != cookie) && !(ptc && NFS_DIR_COOKIE_SAME32(lbn, cookie))) { | |
5589 | dpptc = NULL; | |
5590 | iptc = 0; | |
5591 | for (; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) { | |
5592 | if (ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) { | |
5593 | iptc = i; | |
5594 | dpptc = dp; | |
5595 | } | |
5596 | nextcookie = dp->d_seekoff; | |
5597 | dp = NFS_DIRENTRY_NEXT(dp); | |
5598 | } | |
5599 | if ((i == ndbhp->ndbh_count) && dpptc) { | |
5600 | i = iptc; | |
5601 | dp = dpptc; | |
5602 | } | |
5603 | if (i < ndbhp->ndbh_count) { | |
5604 | nextcookie = dp->d_seekoff; | |
5605 | dp = NFS_DIRENTRY_NEXT(dp); | |
5606 | i++; | |
5607 | } | |
5608 | } | |
5609 | ptc = 0; /* only have to deal with ptc on first cookie */ | |
5610 | ||
5611 | /* return as many entries as we can */ | |
5612 | for (; i < ndbhp->ndbh_count; i++) { | |
5613 | if (extended) { | |
5614 | rlen = dp->d_reclen; | |
5615 | cp = (char*)dp; | |
5616 | } else { | |
5617 | if (!cp) { | |
5618 | cp = (char*)&dent; | |
5619 | bzero(cp, sizeof(dent)); | |
5620 | } | |
0a7de745 | 5621 | if (dp->d_namlen > (sizeof(dent.d_name) - 1)) { |
b0d623f7 | 5622 | nlen = sizeof(dent.d_name) - 1; |
0a7de745 | 5623 | } else { |
b0d623f7 | 5624 | nlen = dp->d_namlen; |
0a7de745 | 5625 | } |
b0d623f7 A |
5626 | rlen = NFS_DIRENT_LEN(nlen); |
5627 | dent.d_reclen = rlen; | |
5628 | dent.d_ino = dp->d_ino; | |
5629 | dent.d_type = dp->d_type; | |
5630 | dent.d_namlen = nlen; | |
5631 | strlcpy(dent.d_name, dp->d_name, nlen + 1); | |
5632 | } | |
5633 | /* check that the record fits */ | |
5634 | if (rlen > uio_resid(uio)) { | |
5635 | done = 1; | |
5636 | break; | |
5637 | } | |
0a7de745 | 5638 | if ((error = uiomove(cp, rlen, uio))) { |
b0d623f7 | 5639 | break; |
0a7de745 | 5640 | } |
b0d623f7 A |
5641 | numdirent++; |
5642 | nextcookie = dp->d_seekoff; | |
5643 | dp = NFS_DIRENTRY_NEXT(dp); | |
5644 | } | |
5645 | ||
5646 | if (i == ndbhp->ndbh_count) { | |
5647 | /* hit end of buffer, move to next buffer */ | |
5648 | lbn = nextcookie; | |
5649 | /* if we also hit EOF, we're done */ | |
5650 | if (ISSET(ndbhp->ndbh_flags, NDB_EOF)) { | |
5651 | done = 1; | |
0a7de745 | 5652 | if (ap->a_eofflag) { |
2d21ac55 | 5653 | *ap->a_eofflag = 1; |
0a7de745 | 5654 | } |
483a1d10 | 5655 | } |
1c79356b | 5656 | } |
0a7de745 | 5657 | if (!error) { |
b0d623f7 | 5658 | uio_setoffset(uio, nextcookie); |
0a7de745 | 5659 | } |
b0d623f7 A |
5660 | if (!error && !done && (nextcookie == cookie)) { |
5661 | printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie, i, ndbhp->ndbh_count); | |
5662 | error = EIO; | |
5663 | } | |
5664 | nfs_buf_release(bp, 1); | |
1c79356b A |
5665 | } |
5666 | ||
0a7de745 | 5667 | if (!error) { |
b0d623f7 | 5668 | nfs_dir_cookie_cache(dnp, nextcookie, lbn); |
0a7de745 | 5669 | } |
1c79356b | 5670 | |
0a7de745 | 5671 | if (ap->a_numdirent) { |
b0d623f7 | 5672 | *ap->a_numdirent = numdirent; |
0a7de745 | 5673 | } |
b0d623f7 | 5674 | out: |
0a7de745 | 5675 | return error; |
1c79356b A |
5676 | } |
5677 | ||
b0d623f7 | 5678 | |
1c79356b | 5679 | /* |
b0d623f7 A |
5680 | * Invalidate cached directory information, except for the actual directory |
5681 | * blocks (which are invalidated separately). | |
1c79356b | 5682 | */ |
b0d623f7 A |
5683 | void |
5684 | nfs_invaldir(nfsnode_t dnp) | |
1c79356b | 5685 | { |
0a7de745 | 5686 | if (vnode_vtype(NFSTOV(dnp)) != VDIR) { |
b0d623f7 | 5687 | return; |
0a7de745 | 5688 | } |
b0d623f7 A |
5689 | dnp->n_eofcookie = 0; |
5690 | dnp->n_cookieverf = 0; | |
0a7de745 | 5691 | if (!dnp->n_cookiecache) { |
b0d623f7 | 5692 | return; |
0a7de745 | 5693 | } |
b0d623f7 A |
5694 | dnp->n_cookiecache->free = 0; |
5695 | dnp->n_cookiecache->mru = -1; | |
5696 | memset(dnp->n_cookiecache->next, -1, NFSNUMCOOKIES); | |
5697 | } | |
1c79356b | 5698 | |
b0d623f7 A |
5699 | /* |
5700 | * calculate how much space is available for additional directory entries. | |
5701 | */ | |
5702 | uint32_t | |
5703 | nfs_dir_buf_freespace(struct nfsbuf *bp, int rdirplus) | |
5704 | { | |
5705 | struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5706 | uint32_t space; | |
1c79356b | 5707 | |
0a7de745 A |
5708 | if (!ndbhp) { |
5709 | return 0; | |
5710 | } | |
b0d623f7 | 5711 | space = bp->nb_bufsize - ndbhp->ndbh_entry_end; |
0a7de745 | 5712 | if (rdirplus) { |
b0d623f7 | 5713 | space -= ndbhp->ndbh_count * sizeof(struct nfs_vattr); |
0a7de745 A |
5714 | } |
5715 | return space; | |
b0d623f7 | 5716 | } |
2d21ac55 | 5717 | |
b0d623f7 A |
5718 | /* |
5719 | * add/update a cookie->lbn entry in the directory cookie cache | |
5720 | */ | |
5721 | void | |
5722 | nfs_dir_cookie_cache(nfsnode_t dnp, uint64_t cookie, uint64_t lbn) | |
5723 | { | |
5724 | struct nfsdmap *ndcc; | |
5725 | int8_t i, prev; | |
5726 | ||
0a7de745 | 5727 | if (!cookie) { |
b0d623f7 | 5728 | return; |
0a7de745 | 5729 | } |
b0d623f7 | 5730 | |
0a7de745 | 5731 | if (nfs_node_lock(dnp)) { |
b0d623f7 | 5732 | return; |
0a7de745 | 5733 | } |
b0d623f7 A |
5734 | |
5735 | if (cookie == dnp->n_eofcookie) { /* EOF cookie */ | |
5736 | nfs_node_unlock(dnp); | |
5737 | return; | |
5738 | } | |
5739 | ||
5740 | ndcc = dnp->n_cookiecache; | |
5741 | if (!ndcc) { | |
5742 | /* allocate the cookie cache structure */ | |
5743 | MALLOC_ZONE(dnp->n_cookiecache, struct nfsdmap *, | |
0a7de745 | 5744 | sizeof(struct nfsdmap), M_NFSDIROFF, M_WAITOK); |
b0d623f7 A |
5745 | if (!dnp->n_cookiecache) { |
5746 | nfs_node_unlock(dnp); | |
5747 | return; | |
5748 | } | |
5749 | ndcc = dnp->n_cookiecache; | |
5750 | ndcc->free = 0; | |
5751 | ndcc->mru = -1; | |
5752 | memset(ndcc->next, -1, NFSNUMCOOKIES); | |
2d21ac55 A |
5753 | } |
5754 | ||
1c79356b | 5755 | /* |
b0d623f7 A |
5756 | * Search the list for this cookie. |
5757 | * Keep track of previous and last entries. | |
1c79356b | 5758 | */ |
b0d623f7 A |
5759 | prev = -1; |
5760 | i = ndcc->mru; | |
5761 | while ((i != -1) && (cookie != ndcc->cookies[i].key)) { | |
0a7de745 | 5762 | if (ndcc->next[i] == -1) { /* stop on last entry so we can reuse */ |
b0d623f7 | 5763 | break; |
0a7de745 | 5764 | } |
b0d623f7 A |
5765 | prev = i; |
5766 | i = ndcc->next[i]; | |
5767 | } | |
5768 | if ((i != -1) && (cookie == ndcc->cookies[i].key)) { | |
5769 | /* found it, remove from list */ | |
0a7de745 | 5770 | if (prev != -1) { |
b0d623f7 | 5771 | ndcc->next[prev] = ndcc->next[i]; |
0a7de745 | 5772 | } else { |
b0d623f7 | 5773 | ndcc->mru = ndcc->next[i]; |
0a7de745 | 5774 | } |
b0d623f7 A |
5775 | } else { |
5776 | /* not found, use next free entry or reuse last entry */ | |
0a7de745 | 5777 | if (ndcc->free != NFSNUMCOOKIES) { |
b0d623f7 | 5778 | i = ndcc->free++; |
0a7de745 | 5779 | } else { |
b0d623f7 | 5780 | ndcc->next[prev] = -1; |
0a7de745 | 5781 | } |
b0d623f7 A |
5782 | ndcc->cookies[i].key = cookie; |
5783 | ndcc->cookies[i].lbn = lbn; | |
5784 | } | |
5785 | /* insert cookie at head of MRU list */ | |
5786 | ndcc->next[i] = ndcc->mru; | |
5787 | ndcc->mru = i; | |
5788 | nfs_node_unlock(dnp); | |
5789 | } | |
2d21ac55 | 5790 | |
b0d623f7 A |
5791 | /* |
5792 | * Try to map the given directory cookie to a directory buffer (return lbn). | |
5793 | * If we have a possibly truncated cookie (ptc), check for 32-bit matches too. | |
5794 | */ | |
5795 | int | |
5796 | nfs_dir_cookie_to_lbn(nfsnode_t dnp, uint64_t cookie, int *ptc, uint64_t *lbnp) | |
5797 | { | |
5798 | struct nfsdmap *ndcc = dnp->n_cookiecache; | |
6d2010ae A |
5799 | int8_t eofptc, found; |
5800 | int i, iptc; | |
b0d623f7 A |
5801 | struct nfsmount *nmp; |
5802 | struct nfsbuf *bp, *lastbp; | |
5803 | struct nfsbuflists blist; | |
5804 | struct direntry *dp, *dpptc; | |
5805 | struct nfs_dir_buf_header *ndbhp; | |
5806 | ||
5807 | if (!cookie) { /* initial cookie */ | |
5808 | *lbnp = 0; | |
5809 | *ptc = 0; | |
0a7de745 | 5810 | return 0; |
b0d623f7 | 5811 | } |
2d21ac55 | 5812 | |
0a7de745 A |
5813 | if (nfs_node_lock(dnp)) { |
5814 | return ENOENT; | |
5815 | } | |
2d21ac55 | 5816 | |
b0d623f7 A |
5817 | if (cookie == dnp->n_eofcookie) { /* EOF cookie */ |
5818 | nfs_node_unlock(dnp); | |
316670eb | 5819 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 | 5820 | *ptc = 0; |
0a7de745 | 5821 | return -1; |
b0d623f7 A |
5822 | } |
5823 | /* note if cookie is a 32-bit match with the EOF cookie */ | |
5824 | eofptc = *ptc ? NFS_DIR_COOKIE_SAME32(cookie, dnp->n_eofcookie) : 0; | |
5825 | iptc = -1; | |
2d21ac55 | 5826 | |
b0d623f7 A |
5827 | /* search the list for the cookie */ |
5828 | for (i = ndcc ? ndcc->mru : -1; i >= 0; i = ndcc->next[i]) { | |
5829 | if (ndcc->cookies[i].key == cookie) { | |
5830 | /* found a match for this cookie */ | |
5831 | *lbnp = ndcc->cookies[i].lbn; | |
5832 | nfs_node_unlock(dnp); | |
316670eb | 5833 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 | 5834 | *ptc = 0; |
0a7de745 | 5835 | return 0; |
1c79356b | 5836 | } |
b0d623f7 | 5837 | /* check for 32-bit match */ |
0a7de745 | 5838 | if (*ptc && (iptc == -1) && NFS_DIR_COOKIE_SAME32(ndcc->cookies[i].key, cookie)) { |
b0d623f7 | 5839 | iptc = i; |
0a7de745 | 5840 | } |
b0d623f7 A |
5841 | } |
5842 | /* exact match not found */ | |
5843 | if (eofptc) { | |
5844 | /* but 32-bit match hit the EOF cookie */ | |
5845 | nfs_node_unlock(dnp); | |
316670eb | 5846 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
0a7de745 | 5847 | return -1; |
b0d623f7 A |
5848 | } |
5849 | if (iptc >= 0) { | |
5850 | /* but 32-bit match got a hit */ | |
5851 | *lbnp = ndcc->cookies[iptc].lbn; | |
5852 | nfs_node_unlock(dnp); | |
316670eb | 5853 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
0a7de745 | 5854 | return 0; |
b0d623f7 A |
5855 | } |
5856 | nfs_node_unlock(dnp); | |
2d21ac55 | 5857 | |
b0d623f7 A |
5858 | /* |
5859 | * No match found in the cookie cache... hmm... | |
5860 | * Let's search the directory's buffers for the cookie. | |
5861 | */ | |
5862 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
5863 | if (nfs_mount_gone(nmp)) { |
5864 | return ENXIO; | |
5865 | } | |
b0d623f7 A |
5866 | dpptc = NULL; |
5867 | found = 0; | |
5868 | ||
5869 | lck_mtx_lock(nfs_buf_mutex); | |
5870 | /* | |
5871 | * Scan the list of buffers, keeping them in order. | |
5872 | * Note that itercomplete inserts each of the remaining buffers | |
5873 | * into the head of list (thus reversing the elements). So, we | |
5874 | * make sure to iterate through all buffers, inserting them after | |
5875 | * each other, to keep them in order. | |
5876 | * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because | |
5877 | * we don't drop nfs_buf_mutex. | |
5878 | */ | |
5879 | if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) { | |
5880 | lastbp = NULL; | |
5881 | while ((bp = LIST_FIRST(&blist))) { | |
5882 | LIST_REMOVE(bp, nb_vnbufs); | |
0a7de745 | 5883 | if (!lastbp) { |
b0d623f7 | 5884 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs); |
0a7de745 | 5885 | } else { |
b0d623f7 | 5886 | LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs); |
0a7de745 | 5887 | } |
b0d623f7 | 5888 | lastbp = bp; |
0a7de745 | 5889 | if (found) { |
b0d623f7 | 5890 | continue; |
0a7de745 | 5891 | } |
b0d623f7 A |
5892 | nfs_buf_refget(bp); |
5893 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
5894 | /* just skip this buffer */ | |
5895 | nfs_buf_refrele(bp); | |
5896 | continue; | |
1c79356b | 5897 | } |
b0d623f7 A |
5898 | nfs_buf_refrele(bp); |
5899 | ||
5900 | /* scan the buffer for the cookie */ | |
5901 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5902 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5903 | dpptc = NULL; | |
0a7de745 | 5904 | for (i = 0; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) { |
b0d623f7 A |
5905 | if (*ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) { |
5906 | dpptc = dp; | |
5907 | iptc = i; | |
5908 | } | |
5909 | dp = NFS_DIRENTRY_NEXT(dp); | |
91447636 | 5910 | } |
b0d623f7 A |
5911 | if ((i == ndbhp->ndbh_count) && dpptc) { |
5912 | /* found only a PTC match */ | |
5913 | dp = dpptc; | |
5914 | i = iptc; | |
5915 | } else if (i < ndbhp->ndbh_count) { | |
5916 | *ptc = 0; | |
1c79356b | 5917 | } |
0a7de745 | 5918 | if (i < (ndbhp->ndbh_count - 1)) { |
b0d623f7 A |
5919 | /* next entry is *in* this buffer: return this block */ |
5920 | *lbnp = bp->nb_lblkno; | |
5921 | found = 1; | |
0a7de745 | 5922 | } else if (i == (ndbhp->ndbh_count - 1)) { |
b0d623f7 A |
5923 | /* next entry refers to *next* buffer: return next block */ |
5924 | *lbnp = dp->d_seekoff; | |
5925 | found = 1; | |
1c79356b | 5926 | } |
b0d623f7 | 5927 | nfs_buf_drop(bp); |
1c79356b | 5928 | } |
b0d623f7 | 5929 | nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN); |
2d21ac55 | 5930 | } |
b0d623f7 A |
5931 | lck_mtx_unlock(nfs_buf_mutex); |
5932 | if (found) { | |
316670eb | 5933 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
0a7de745 | 5934 | return 0; |
1c79356b | 5935 | } |
b0d623f7 A |
5936 | |
5937 | /* still not found... oh well, just start a new block */ | |
5938 | *lbnp = cookie; | |
316670eb | 5939 | OSAddAtomic64(1, &nfsstats.direofcache_misses); |
0a7de745 | 5940 | return 0; |
b0d623f7 A |
5941 | } |
5942 | ||
5943 | /* | |
5944 | * scan a directory buffer for the given name | |
5945 | * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found | |
5946 | * Note: should only be called with RDIRPLUS directory buffers | |
5947 | */ | |
5948 | ||
0a7de745 A |
5949 | #define NDBS_PURGE 1 |
5950 | #define NDBS_UPDATE 2 | |
b0d623f7 A |
5951 | |
5952 | int | |
5953 | nfs_dir_buf_search( | |
5954 | struct nfsbuf *bp, | |
5955 | struct componentname *cnp, | |
5956 | fhandle_t *fhp, | |
5957 | struct nfs_vattr *nvap, | |
5958 | uint64_t *xidp, | |
5959 | time_t *attrstampp, | |
5960 | daddr64_t *nextlbnp, | |
5961 | int flags) | |
5962 | { | |
5963 | struct direntry *dp; | |
5964 | struct nfs_dir_buf_header *ndbhp; | |
5965 | struct nfs_vattr *nvattrp; | |
5966 | daddr64_t nextlbn = 0; | |
d26ffc64 A |
5967 | int i, error = ESRCH; |
5968 | uint32_t fhlen; | |
b0d623f7 A |
5969 | |
5970 | /* scan the buffer for the name */ | |
5971 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
5972 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
0a7de745 | 5973 | for (i = 0; i < ndbhp->ndbh_count; i++) { |
b0d623f7 A |
5974 | nextlbn = dp->d_seekoff; |
5975 | if ((cnp->cn_namelen == dp->d_namlen) && !strcmp(cnp->cn_nameptr, dp->d_name)) { | |
0a7de745 | 5976 | fhlen = dp->d_name[dp->d_namlen + 1]; |
b0d623f7 | 5977 | nvattrp = NFS_DIR_BUF_NVATTR(bp, i); |
94ff46dc | 5978 | if ((ndbhp->ndbh_ncgen != bp->nb_np->n_ncgen) || (fhlen == 0) || |
b0d623f7 | 5979 | (nvattrp->nva_type == VNON) || (nvattrp->nva_fileid == 0)) { |
6d2010ae | 5980 | /* entry is not valid */ |
b0d623f7 A |
5981 | error = ENOENT; |
5982 | break; | |
5983 | } | |
5984 | if (flags == NDBS_PURGE) { | |
5985 | dp->d_fileno = 0; | |
5986 | bzero(nvattrp, sizeof(*nvattrp)); | |
5987 | error = ENOENT; | |
5988 | break; | |
5989 | } | |
5990 | if (flags == NDBS_UPDATE) { | |
5991 | /* update direntry's attrs if fh matches */ | |
0a7de745 | 5992 | if ((fhp->fh_len == fhlen) && !bcmp(&dp->d_name[dp->d_namlen + 2], fhp->fh_data, fhlen)) { |
b0d623f7 A |
5993 | bcopy(nvap, nvattrp, sizeof(*nvap)); |
5994 | dp->d_fileno = nvattrp->nva_fileid; | |
5995 | nvattrp->nva_fileid = *xidp; | |
0a7de745 | 5996 | *(time_t*)(&dp->d_name[dp->d_namlen + 2 + fhp->fh_len]) = *attrstampp; |
b0d623f7 A |
5997 | } |
5998 | error = 0; | |
5999 | break; | |
6000 | } | |
6001 | /* copy out fh, attrs, attrstamp, and xid */ | |
6002 | fhp->fh_len = fhlen; | |
0a7de745 A |
6003 | bcopy(&dp->d_name[dp->d_namlen + 2], fhp->fh_data, MAX(fhp->fh_len, (int)sizeof(fhp->fh_data))); |
6004 | *attrstampp = *(time_t*)(&dp->d_name[dp->d_namlen + 2 + fhp->fh_len]); | |
b0d623f7 A |
6005 | bcopy(nvattrp, nvap, sizeof(*nvap)); |
6006 | *xidp = nvap->nva_fileid; | |
6007 | nvap->nva_fileid = dp->d_fileno; | |
6008 | error = 0; | |
6009 | break; | |
6010 | } | |
6011 | dp = NFS_DIRENTRY_NEXT(dp); | |
1c79356b | 6012 | } |
0a7de745 | 6013 | if (nextlbnp) { |
b0d623f7 | 6014 | *nextlbnp = nextlbn; |
0a7de745 A |
6015 | } |
6016 | return error; | |
b0d623f7 | 6017 | } |
1c79356b | 6018 | |
b0d623f7 A |
6019 | /* |
6020 | * Look up a name in a directory's buffers. | |
6021 | * Note: should only be called with RDIRPLUS directory buffers | |
6022 | */ | |
6023 | int | |
6024 | nfs_dir_buf_cache_lookup(nfsnode_t dnp, nfsnode_t *npp, struct componentname *cnp, vfs_context_t ctx, int purge) | |
6025 | { | |
6026 | nfsnode_t newnp; | |
6027 | struct nfsmount *nmp; | |
6d2010ae | 6028 | int error = 0, i, found = 0, count = 0; |
b0d623f7 A |
6029 | u_int64_t xid; |
6030 | struct nfs_vattr nvattr; | |
6031 | fhandle_t fh; | |
6032 | time_t attrstamp = 0; | |
6033 | thread_t thd = vfs_context_thread(ctx); | |
6034 | struct nfsbuf *bp, *lastbp, *foundbp; | |
6035 | struct nfsbuflists blist; | |
6036 | daddr64_t lbn, nextlbn; | |
6037 | int dotunder = (cnp->cn_namelen > 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '_'); | |
6038 | ||
fe8ab488 | 6039 | nmp = NFSTONMP(dnp); |
0a7de745 A |
6040 | if (nfs_mount_gone(nmp)) { |
6041 | return ENXIO; | |
6042 | } | |
6043 | if (!purge) { | |
b0d623f7 | 6044 | *npp = NULL; |
0a7de745 | 6045 | } |
b0d623f7 A |
6046 | |
6047 | /* first check most recent buffer (and next one too) */ | |
6048 | lbn = dnp->n_lastdbl; | |
0a7de745 A |
6049 | for (i = 0; i < 2; i++) { |
6050 | if ((error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ | NBLK_ONLYVALID, &bp))) { | |
6051 | return error; | |
6052 | } | |
6053 | if (!bp) { | |
b0d623f7 | 6054 | break; |
0a7de745 | 6055 | } |
b0d623f7 A |
6056 | count++; |
6057 | error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, &nextlbn, purge ? NDBS_PURGE : 0); | |
6058 | nfs_buf_release(bp, 0); | |
6059 | if (error == ESRCH) { | |
6060 | error = 0; | |
6061 | } else { | |
6062 | found = 1; | |
6063 | break; | |
6064 | } | |
6065 | lbn = nextlbn; | |
6066 | } | |
6067 | ||
6068 | lck_mtx_lock(nfs_buf_mutex); | |
6069 | if (found) { | |
6070 | dnp->n_lastdbl = lbn; | |
6071 | goto done; | |
6072 | } | |
2d21ac55 | 6073 | |
1c79356b | 6074 | /* |
b0d623f7 A |
6075 | * Scan the list of buffers, keeping them in order. |
6076 | * Note that itercomplete inserts each of the remaining buffers | |
6077 | * into the head of list (thus reversing the elements). So, we | |
6078 | * make sure to iterate through all buffers, inserting them after | |
6079 | * each other, to keep them in order. | |
6080 | * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because | |
6081 | * we don't drop nfs_buf_mutex. | |
1c79356b | 6082 | */ |
b0d623f7 A |
6083 | if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) { |
6084 | lastbp = foundbp = NULL; | |
6085 | while ((bp = LIST_FIRST(&blist))) { | |
6086 | LIST_REMOVE(bp, nb_vnbufs); | |
0a7de745 | 6087 | if (!lastbp) { |
b0d623f7 | 6088 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs); |
0a7de745 | 6089 | } else { |
b0d623f7 | 6090 | LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs); |
0a7de745 | 6091 | } |
b0d623f7 | 6092 | lastbp = bp; |
0a7de745 | 6093 | if (error || found) { |
b0d623f7 | 6094 | continue; |
0a7de745 A |
6095 | } |
6096 | if (!purge && dotunder && (count > 100)) { /* don't waste too much time looking for ._ files */ | |
b0d623f7 | 6097 | continue; |
0a7de745 | 6098 | } |
b0d623f7 A |
6099 | nfs_buf_refget(bp); |
6100 | lbn = bp->nb_lblkno; | |
6101 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
6102 | /* just skip this buffer */ | |
6103 | nfs_buf_refrele(bp); | |
6104 | continue; | |
6105 | } | |
6106 | nfs_buf_refrele(bp); | |
6107 | count++; | |
6108 | error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, purge ? NDBS_PURGE : 0); | |
6109 | if (error == ESRCH) { | |
6110 | error = 0; | |
6111 | } else { | |
6112 | found = 1; | |
6113 | foundbp = bp; | |
6114 | } | |
6115 | nfs_buf_drop(bp); | |
6116 | } | |
6117 | if (found) { | |
6118 | LIST_REMOVE(foundbp, nb_vnbufs); | |
6119 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, foundbp, nb_vnbufs); | |
6120 | dnp->n_lastdbl = foundbp->nb_lblkno; | |
6121 | } | |
6122 | nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN); | |
6123 | } | |
6124 | done: | |
6125 | lck_mtx_unlock(nfs_buf_mutex); | |
6126 | ||
6127 | if (!error && found && !purge) { | |
6128 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, | |
0a7de745 A |
6129 | &nvattr, &xid, dnp->n_auth, NG_MAKEENTRY, &newnp); |
6130 | if (error) { | |
6131 | return error; | |
6132 | } | |
b0d623f7 A |
6133 | newnp->n_attrstamp = attrstamp; |
6134 | *npp = newnp; | |
6135 | nfs_node_unlock(newnp); | |
6136 | /* check if the dir buffer's attrs are out of date */ | |
6137 | if (!nfs_getattr(newnp, &nvattr, ctx, NGA_CACHED) && | |
6138 | (newnp->n_attrstamp != attrstamp)) { | |
6139 | /* they are, so update them */ | |
0a7de745 | 6140 | error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ | NBLK_ONLYVALID, &bp); |
b0d623f7 A |
6141 | if (!error && bp) { |
6142 | attrstamp = newnp->n_attrstamp; | |
6143 | xid = newnp->n_xid; | |
6144 | nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, NDBS_UPDATE); | |
6145 | nfs_buf_release(bp, 0); | |
6146 | } | |
6147 | error = 0; | |
6148 | } | |
1c79356b | 6149 | } |
2d21ac55 | 6150 | |
0a7de745 | 6151 | return error; |
1c79356b A |
6152 | } |
6153 | ||
6154 | /* | |
b0d623f7 A |
6155 | * Purge name cache entries for the given node. |
6156 | * For RDIRPLUS, also invalidate the entry in the directory's buffers. | |
6157 | */ | |
6158 | void | |
6159 | nfs_name_cache_purge(nfsnode_t dnp, nfsnode_t np, struct componentname *cnp, vfs_context_t ctx) | |
6160 | { | |
6161 | struct nfsmount *nmp = NFSTONMP(dnp); | |
6162 | ||
6163 | cache_purge(NFSTOV(np)); | |
0a7de745 | 6164 | if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { |
b0d623f7 | 6165 | nfs_dir_buf_cache_lookup(dnp, NULL, cnp, ctx, 1); |
0a7de745 | 6166 | } |
b0d623f7 A |
6167 | } |
6168 | ||
6169 | /* | |
6170 | * NFS V3 readdir (plus) RPC. | |
1c79356b A |
6171 | */ |
6172 | int | |
b0d623f7 | 6173 | nfs3_readdir_rpc(nfsnode_t dnp, struct nfsbuf *bp, vfs_context_t ctx) |
1c79356b | 6174 | { |
55e303ae | 6175 | struct nfsmount *nmp; |
b0d623f7 A |
6176 | int error = 0, lockerror, nfsvers, rdirplus, bigcookies; |
6177 | int i, status, attrflag, fhflag, more_entries = 1, eof, bp_dropped = 0; | |
6178 | uint32_t nmreaddirsize, nmrsize; | |
6179 | uint32_t namlen, skiplen, fhlen, xlen, attrlen, reclen, space_free, space_needed; | |
6180 | uint64_t cookie, lastcookie, xid, savedxid, fileno; | |
6181 | struct nfsm_chain nmreq, nmrep, nmrepsave; | |
6182 | fhandle_t fh; | |
6183 | struct nfs_vattr *nvattrp; | |
6184 | struct nfs_dir_buf_header *ndbhp; | |
6185 | struct direntry *dp; | |
6186 | char *padstart, padlen; | |
6187 | struct timeval now; | |
1c79356b | 6188 | |
2d21ac55 | 6189 | nmp = NFSTONMP(dnp); |
0a7de745 A |
6190 | if (nfs_mount_gone(nmp)) { |
6191 | return ENXIO; | |
6192 | } | |
2d21ac55 | 6193 | nfsvers = nmp->nm_vers; |
55e303ae A |
6194 | nmreaddirsize = nmp->nm_readdirsize; |
6195 | nmrsize = nmp->nm_rsize; | |
b0d623f7 A |
6196 | bigcookies = nmp->nm_state & NFSSTA_BIGCOOKIES; |
6197 | noplus: | |
6d2010ae | 6198 | rdirplus = ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) ? 1 : 0; |
55e303ae | 6199 | |
0a7de745 A |
6200 | if ((lockerror = nfs_node_lock(dnp))) { |
6201 | return lockerror; | |
6202 | } | |
2d21ac55 | 6203 | |
b0d623f7 A |
6204 | /* determine cookie to use, and move dp to the right offset */ |
6205 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
6206 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
6207 | if (ndbhp->ndbh_count) { | |
0a7de745 | 6208 | for (i = 0; i < ndbhp->ndbh_count - 1; i++) { |
b0d623f7 | 6209 | dp = NFS_DIRENTRY_NEXT(dp); |
0a7de745 | 6210 | } |
b0d623f7 A |
6211 | cookie = dp->d_seekoff; |
6212 | dp = NFS_DIRENTRY_NEXT(dp); | |
6213 | } else { | |
6214 | cookie = bp->nb_lblkno; | |
6215 | /* increment with every buffer read */ | |
316670eb | 6216 | OSAddAtomic64(1, &nfsstats.readdir_bios); |
2d21ac55 | 6217 | } |
b0d623f7 | 6218 | lastcookie = cookie; |
2d21ac55 | 6219 | |
1c79356b | 6220 | /* |
b0d623f7 A |
6221 | * Loop around doing readdir(plus) RPCs of size nm_readdirsize until |
6222 | * the buffer is full (or we hit EOF). Then put the remainder of the | |
6223 | * results in the next buffer(s). | |
1c79356b | 6224 | */ |
2d21ac55 A |
6225 | nfsm_chain_null(&nmreq); |
6226 | nfsm_chain_null(&nmrep); | |
b0d623f7 | 6227 | while (nfs_dir_buf_freespace(bp, rdirplus) && !(ndbhp->ndbh_flags & NDB_FULL)) { |
2d21ac55 | 6228 | nfsm_chain_build_alloc_init(error, &nmreq, |
0a7de745 | 6229 | NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers) + NFSX_UNSIGNED); |
2d21ac55 | 6230 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
b0d623f7 A |
6231 | if (nfsvers == NFS_VER3) { |
6232 | /* opaque values don't need swapping, but as long */ | |
6233 | /* as we are consistent about it, it should be ok */ | |
6234 | nfsm_chain_add_64(error, &nmreq, cookie); | |
6235 | nfsm_chain_add_64(error, &nmreq, dnp->n_cookieverf); | |
6236 | } else { | |
6237 | nfsm_chain_add_32(error, &nmreq, cookie); | |
6238 | } | |
2d21ac55 | 6239 | nfsm_chain_add_32(error, &nmreq, nmreaddirsize); |
0a7de745 | 6240 | if (rdirplus) { |
b0d623f7 | 6241 | nfsm_chain_add_32(error, &nmreq, nmrsize); |
0a7de745 | 6242 | } |
2d21ac55 | 6243 | nfsm_chain_build_done(error, &nmreq); |
b0d623f7 | 6244 | nfs_node_unlock(dnp); |
2d21ac55 A |
6245 | lockerror = ENOENT; |
6246 | nfsmout_if(error); | |
6247 | ||
b0d623f7 | 6248 | error = nfs_request(dnp, NULL, &nmreq, |
0a7de745 A |
6249 | rdirplus ? NFSPROC_READDIRPLUS : NFSPROC_READDIR, |
6250 | ctx, NULL, &nmrep, &xid, &status); | |
2d21ac55 | 6251 | |
0a7de745 | 6252 | if ((lockerror = nfs_node_lock(dnp))) { |
2d21ac55 | 6253 | error = lockerror; |
0a7de745 | 6254 | } |
2d21ac55 | 6255 | |
b0d623f7 | 6256 | savedxid = xid; |
0a7de745 | 6257 | if (nfsvers == NFS_VER3) { |
b0d623f7 | 6258 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); |
0a7de745 A |
6259 | } |
6260 | if (!error) { | |
2d21ac55 | 6261 | error = status; |
0a7de745 A |
6262 | } |
6263 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 6264 | nfsm_chain_get_64(error, &nmrep, dnp->n_cookieverf); |
0a7de745 | 6265 | } |
b0d623f7 | 6266 | nfsm_chain_get_32(error, &nmrep, more_entries); |
2d21ac55 A |
6267 | |
6268 | if (!lockerror) { | |
b0d623f7 | 6269 | nfs_node_unlock(dnp); |
2d21ac55 | 6270 | lockerror = ENOENT; |
1c79356b | 6271 | } |
b0d623f7 A |
6272 | if (error == NFSERR_NOTSUPP) { |
6273 | /* oops... it doesn't look like readdirplus is supported */ | |
6274 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae | 6275 | NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS); |
b0d623f7 A |
6276 | lck_mtx_unlock(&nmp->nm_lock); |
6277 | goto noplus; | |
6278 | } | |
2d21ac55 | 6279 | nfsmout_if(error); |
1c79356b | 6280 | |
0a7de745 | 6281 | if (rdirplus) { |
b0d623f7 | 6282 | microuptime(&now); |
0a7de745 | 6283 | } |
b0d623f7 A |
6284 | |
6285 | /* loop through the entries packing them into the buffer */ | |
6286 | while (more_entries) { | |
0a7de745 | 6287 | if (nfsvers == NFS_VER3) { |
b0d623f7 | 6288 | nfsm_chain_get_64(error, &nmrep, fileno); |
0a7de745 | 6289 | } else { |
b0d623f7 | 6290 | nfsm_chain_get_32(error, &nmrep, fileno); |
0a7de745 | 6291 | } |
b0d623f7 | 6292 | nfsm_chain_get_32(error, &nmrep, namlen); |
2d21ac55 | 6293 | nfsmout_if(error); |
b0d623f7 A |
6294 | /* just truncate names that don't fit in direntry.d_name */ |
6295 | if (namlen <= 0) { | |
1c79356b | 6296 | error = EBADRPC; |
1c79356b A |
6297 | goto nfsmout; |
6298 | } | |
0a7de745 | 6299 | if (namlen > (sizeof(dp->d_name) - 1)) { |
b0d623f7 A |
6300 | skiplen = namlen - sizeof(dp->d_name) + 1; |
6301 | namlen = sizeof(dp->d_name) - 1; | |
91447636 A |
6302 | } else { |
6303 | skiplen = 0; | |
6304 | } | |
b0d623f7 A |
6305 | /* guess that fh size will be same as parent */ |
6306 | fhlen = rdirplus ? (1 + dnp->n_fhsize) : 0; | |
6307 | xlen = rdirplus ? (fhlen + sizeof(time_t)) : 0; | |
6308 | attrlen = rdirplus ? sizeof(struct nfs_vattr) : 0; | |
6309 | reclen = NFS_DIRENTRY_LEN(namlen + xlen); | |
6310 | space_needed = reclen + attrlen; | |
6311 | space_free = nfs_dir_buf_freespace(bp, rdirplus); | |
6312 | if (space_needed > space_free) { | |
6313 | /* | |
6314 | * We still have entries to pack, but we've | |
6315 | * run out of room in the current buffer. | |
6316 | * So we need to move to the next buffer. | |
6317 | * The block# for the next buffer is the | |
6318 | * last cookie in the current buffer. | |
6319 | */ | |
6320 | nextbuffer: | |
6321 | ndbhp->ndbh_flags |= NDB_FULL; | |
6322 | nfs_buf_release(bp, 0); | |
6323 | bp_dropped = 1; | |
6324 | bp = NULL; | |
6325 | error = nfs_buf_get(dnp, lastcookie, NFS_DIRBLKSIZ, vfs_context_thread(ctx), NBLK_READ, &bp); | |
2d21ac55 | 6326 | nfsmout_if(error); |
b0d623f7 A |
6327 | /* initialize buffer */ |
6328 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
6329 | ndbhp->ndbh_flags = 0; | |
6330 | ndbhp->ndbh_count = 0; | |
6331 | ndbhp->ndbh_entry_end = sizeof(*ndbhp); | |
6332 | ndbhp->ndbh_ncgen = dnp->n_ncgen; | |
6333 | space_free = nfs_dir_buf_freespace(bp, rdirplus); | |
6334 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
6335 | /* increment with every buffer read */ | |
316670eb | 6336 | OSAddAtomic64(1, &nfsstats.readdir_bios); |
91447636 | 6337 | } |
b0d623f7 A |
6338 | nmrepsave = nmrep; |
6339 | dp->d_fileno = fileno; | |
6340 | dp->d_namlen = namlen; | |
6341 | dp->d_reclen = reclen; | |
6342 | dp->d_type = DT_UNKNOWN; | |
6343 | nfsm_chain_get_opaque(error, &nmrep, namlen, dp->d_name); | |
2d21ac55 | 6344 | nfsmout_if(error); |
b0d623f7 | 6345 | dp->d_name[namlen] = '\0'; |
0a7de745 | 6346 | if (skiplen) { |
b0d623f7 | 6347 | nfsm_chain_adv(error, &nmrep, |
0a7de745 A |
6348 | nfsm_rndup(namlen + skiplen) - nfsm_rndup(namlen)); |
6349 | } | |
6350 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 6351 | nfsm_chain_get_64(error, &nmrep, cookie); |
0a7de745 | 6352 | } else { |
b0d623f7 | 6353 | nfsm_chain_get_32(error, &nmrep, cookie); |
0a7de745 | 6354 | } |
b0d623f7 A |
6355 | nfsmout_if(error); |
6356 | dp->d_seekoff = cookie; | |
6357 | if (!bigcookies && (cookie >> 32) && (nmp == NFSTONMP(dnp))) { | |
6358 | /* we've got a big cookie, make sure flag is set */ | |
6359 | lck_mtx_lock(&nmp->nm_lock); | |
6360 | nmp->nm_state |= NFSSTA_BIGCOOKIES; | |
6361 | lck_mtx_unlock(&nmp->nm_lock); | |
6362 | bigcookies = 1; | |
6363 | } | |
6364 | if (rdirplus) { | |
6365 | nvattrp = NFS_DIR_BUF_NVATTR(bp, ndbhp->ndbh_count); | |
6366 | /* check for attributes */ | |
6367 | nfsm_chain_get_32(error, &nmrep, attrflag); | |
2d21ac55 | 6368 | nfsmout_if(error); |
b0d623f7 A |
6369 | if (attrflag) { |
6370 | /* grab attributes */ | |
cb323159 | 6371 | error = nfs_parsefattr(nmp, &nmrep, NFS_VER3, nvattrp); |
b0d623f7 A |
6372 | nfsmout_if(error); |
6373 | dp->d_type = IFTODT(VTTOIF(nvattrp->nva_type)); | |
6374 | /* fileid is already in d_fileno, so stash xid in attrs */ | |
6375 | nvattrp->nva_fileid = savedxid; | |
6376 | } else { | |
6377 | /* mark the attributes invalid */ | |
6378 | bzero(nvattrp, sizeof(struct nfs_vattr)); | |
6379 | } | |
6380 | /* check for file handle */ | |
6381 | nfsm_chain_get_32(error, &nmrep, fhflag); | |
6382 | nfsmout_if(error); | |
6383 | if (fhflag) { | |
6384 | nfsm_chain_get_fh(error, &nmrep, NFS_VER3, &fh); | |
6385 | nfsmout_if(error); | |
6386 | fhlen = fh.fh_len + 1; | |
6387 | xlen = fhlen + sizeof(time_t); | |
6388 | reclen = NFS_DIRENTRY_LEN(namlen + xlen); | |
6389 | space_needed = reclen + attrlen; | |
6390 | if (space_needed > space_free) { | |
6391 | /* didn't actually have the room... move on to next buffer */ | |
6392 | nmrep = nmrepsave; | |
6393 | goto nextbuffer; | |
2d21ac55 | 6394 | } |
b0d623f7 | 6395 | /* pack the file handle into the record */ |
0a7de745 A |
6396 | dp->d_name[dp->d_namlen + 1] = fh.fh_len; |
6397 | bcopy(fh.fh_data, &dp->d_name[dp->d_namlen + 2], fh.fh_len); | |
1c79356b | 6398 | } else { |
b0d623f7 A |
6399 | /* mark the file handle invalid */ |
6400 | fh.fh_len = 0; | |
6401 | fhlen = fh.fh_len + 1; | |
6402 | xlen = fhlen + sizeof(time_t); | |
6403 | reclen = NFS_DIRENTRY_LEN(namlen + xlen); | |
0a7de745 | 6404 | bzero(&dp->d_name[dp->d_namlen + 1], fhlen); |
1c79356b | 6405 | } |
0a7de745 | 6406 | *(time_t*)(&dp->d_name[dp->d_namlen + 1 + fhlen]) = now.tv_sec; |
b0d623f7 | 6407 | dp->d_reclen = reclen; |
1c79356b | 6408 | } |
b0d623f7 A |
6409 | padstart = dp->d_name + dp->d_namlen + 1 + xlen; |
6410 | ndbhp->ndbh_count++; | |
6411 | lastcookie = cookie; | |
6412 | /* advance to next direntry in buffer */ | |
6413 | dp = NFS_DIRENTRY_NEXT(dp); | |
6414 | ndbhp->ndbh_entry_end = (char*)dp - bp->nb_data; | |
6415 | /* zero out the pad bytes */ | |
6416 | padlen = (char*)dp - padstart; | |
0a7de745 | 6417 | if (padlen > 0) { |
b0d623f7 | 6418 | bzero(padstart, padlen); |
0a7de745 | 6419 | } |
b0d623f7 A |
6420 | /* check for more entries */ |
6421 | nfsm_chain_get_32(error, &nmrep, more_entries); | |
2d21ac55 | 6422 | nfsmout_if(error); |
1c79356b | 6423 | } |
b0d623f7 A |
6424 | /* Finally, get the eof boolean */ |
6425 | nfsm_chain_get_32(error, &nmrep, eof); | |
6426 | nfsmout_if(error); | |
6427 | if (eof) { | |
0a7de745 | 6428 | ndbhp->ndbh_flags |= (NDB_FULL | NDB_EOF); |
b0d623f7 A |
6429 | nfs_node_lock_force(dnp); |
6430 | dnp->n_eofcookie = lastcookie; | |
6431 | nfs_node_unlock(dnp); | |
6432 | } else { | |
6433 | more_entries = 1; | |
6434 | } | |
6435 | if (bp_dropped) { | |
6436 | nfs_buf_release(bp, 0); | |
6437 | bp = NULL; | |
6438 | break; | |
1c79356b | 6439 | } |
0a7de745 | 6440 | if ((lockerror = nfs_node_lock(dnp))) { |
2d21ac55 | 6441 | error = lockerror; |
0a7de745 | 6442 | } |
2d21ac55 A |
6443 | nfsmout_if(error); |
6444 | nfsm_chain_cleanup(&nmrep); | |
6445 | nfsm_chain_null(&nmreq); | |
6446 | } | |
1c79356b | 6447 | nfsmout: |
0a7de745 | 6448 | if (bp_dropped && bp) { |
b0d623f7 | 6449 | nfs_buf_release(bp, 0); |
0a7de745 A |
6450 | } |
6451 | if (!lockerror) { | |
b0d623f7 | 6452 | nfs_node_unlock(dnp); |
0a7de745 | 6453 | } |
2d21ac55 A |
6454 | nfsm_chain_cleanup(&nmreq); |
6455 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 6456 | return bp_dropped ? NFSERR_DIRBUFDROPPED : error; |
1c79356b A |
6457 | } |
6458 | ||
6459 | /* | |
6460 | * Silly rename. To make the NFS filesystem that is stateless look a little | |
6461 | * more like the "ufs" a remove of an active vnode is translated to a rename | |
2d21ac55 | 6462 | * to a funny looking filename that is removed by nfs_vnop_inactive on the |
1c79356b | 6463 | * nfsnode. There is the potential for another process on a different client |
2d21ac55 A |
6464 | * to create the same funny name between when the lookitup() fails and the |
6465 | * rename() completes, but... | |
1c79356b | 6466 | */ |
55e303ae | 6467 | |
2d21ac55 A |
6468 | /* format of "random" silly names - includes a number and pid */ |
6469 | /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */ | |
6470 | #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x" | |
6471 | /* starting from zero isn't silly enough */ | |
6472 | static uint32_t nfs_sillyrename_number = 0x20051025; | |
55e303ae | 6473 | |
b0d623f7 | 6474 | int |
91447636 | 6475 | nfs_sillyrename( |
2d21ac55 A |
6476 | nfsnode_t dnp, |
6477 | nfsnode_t np, | |
91447636 | 6478 | struct componentname *cnp, |
2d21ac55 | 6479 | vfs_context_t ctx) |
1c79356b | 6480 | { |
2d21ac55 | 6481 | struct nfs_sillyrename *nsp; |
1c79356b A |
6482 | int error; |
6483 | short pid; | |
2d21ac55 A |
6484 | kauth_cred_t cred; |
6485 | uint32_t num; | |
6486 | struct nfsmount *nmp; | |
1c79356b | 6487 | |
2d21ac55 | 6488 | nmp = NFSTONMP(dnp); |
0a7de745 A |
6489 | if (nfs_mount_gone(nmp)) { |
6490 | return ENXIO; | |
6491 | } | |
2d21ac55 | 6492 | |
b0d623f7 | 6493 | nfs_name_cache_purge(dnp, np, cnp, ctx); |
2d21ac55 A |
6494 | |
6495 | MALLOC_ZONE(nsp, struct nfs_sillyrename *, | |
0a7de745 A |
6496 | sizeof(struct nfs_sillyrename), M_NFSREQ, M_WAITOK); |
6497 | if (!nsp) { | |
6498 | return ENOMEM; | |
6499 | } | |
2d21ac55 | 6500 | cred = vfs_context_ucred(ctx); |
91447636 | 6501 | kauth_cred_ref(cred); |
2d21ac55 A |
6502 | nsp->nsr_cred = cred; |
6503 | nsp->nsr_dnp = dnp; | |
6504 | error = vnode_ref(NFSTOV(dnp)); | |
0a7de745 | 6505 | if (error) { |
91447636 | 6506 | goto bad_norele; |
0a7de745 | 6507 | } |
1c79356b A |
6508 | |
6509 | /* Fudge together a funny name */ | |
2d21ac55 | 6510 | pid = vfs_context_pid(ctx); |
b0d623f7 | 6511 | num = OSAddAtomic(1, &nfs_sillyrename_number); |
2d21ac55 | 6512 | nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name), |
0a7de745 A |
6513 | NFS_SILLYNAME_FORMAT, num, (pid & 0xffff)); |
6514 | if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name)) { | |
2d21ac55 | 6515 | nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1; |
0a7de745 | 6516 | } |
1c79356b A |
6517 | |
6518 | /* Try lookitups until we get one that isn't there */ | |
2d21ac55 | 6519 | while (nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, NULL) == 0) { |
b0d623f7 | 6520 | num = OSAddAtomic(1, &nfs_sillyrename_number); |
2d21ac55 | 6521 | nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name), |
0a7de745 A |
6522 | NFS_SILLYNAME_FORMAT, num, (pid & 0xffff)); |
6523 | if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name)) { | |
2d21ac55 | 6524 | nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1; |
0a7de745 | 6525 | } |
55e303ae | 6526 | } |
2d21ac55 | 6527 | |
55e303ae | 6528 | /* now, do the rename */ |
2d21ac55 | 6529 | error = nmp->nm_funcs->nf_rename_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen, |
0a7de745 | 6530 | dnp, nsp->nsr_name, nsp->nsr_namlen, ctx); |
6d2010ae A |
6531 | |
6532 | /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */ | |
0a7de745 | 6533 | if (error == ENOENT) { |
6d2010ae | 6534 | error = 0; |
0a7de745 | 6535 | } |
b0d623f7 A |
6536 | if (!error) { |
6537 | nfs_node_lock_force(dnp); | |
6538 | if (dnp->n_flag & NNEGNCENTRIES) { | |
6539 | dnp->n_flag &= ~NNEGNCENTRIES; | |
6540 | cache_purge_negatives(NFSTOV(dnp)); | |
6541 | } | |
6542 | nfs_node_unlock(dnp); | |
2d21ac55 A |
6543 | } |
6544 | FSDBG(267, dnp, np, num, error); | |
0a7de745 | 6545 | if (error) { |
1c79356b | 6546 | goto bad; |
0a7de745 | 6547 | } |
2d21ac55 | 6548 | error = nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, &np); |
b0d623f7 | 6549 | nfs_node_lock_force(np); |
2d21ac55 | 6550 | np->n_sillyrename = nsp; |
b0d623f7 | 6551 | nfs_node_unlock(np); |
0a7de745 | 6552 | return 0; |
1c79356b | 6553 | bad: |
2d21ac55 | 6554 | vnode_rele(NFSTOV(dnp)); |
91447636 | 6555 | bad_norele: |
2d21ac55 A |
6556 | nsp->nsr_cred = NOCRED; |
6557 | kauth_cred_unref(&cred); | |
6558 | FREE_ZONE(nsp, sizeof(*nsp), M_NFSREQ); | |
0a7de745 | 6559 | return error; |
2d21ac55 A |
6560 | } |
6561 | ||
6562 | int | |
6563 | nfs3_lookup_rpc_async( | |
6564 | nfsnode_t dnp, | |
6565 | char *name, | |
6566 | int namelen, | |
6567 | vfs_context_t ctx, | |
6568 | struct nfsreq **reqp) | |
6569 | { | |
6570 | struct nfsmount *nmp; | |
6571 | struct nfsm_chain nmreq; | |
6572 | int error = 0, nfsvers; | |
6573 | ||
6574 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6575 | if (nfs_mount_gone(nmp)) { |
6576 | return ENXIO; | |
6577 | } | |
2d21ac55 A |
6578 | nfsvers = nmp->nm_vers; |
6579 | ||
6580 | nfsm_chain_null(&nmreq); | |
6581 | ||
6582 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 6583 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen)); |
2d21ac55 | 6584 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 6585 | nfsm_chain_add_name(error, &nmreq, name, namelen, nmp); |
2d21ac55 A |
6586 | nfsm_chain_build_done(error, &nmreq); |
6587 | nfsmout_if(error); | |
6588 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_LOOKUP, | |
0a7de745 | 6589 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, reqp); |
2d21ac55 A |
6590 | nfsmout: |
6591 | nfsm_chain_cleanup(&nmreq); | |
0a7de745 | 6592 | return error; |
2d21ac55 A |
6593 | } |
6594 | ||
6595 | int | |
6596 | nfs3_lookup_rpc_async_finish( | |
6597 | nfsnode_t dnp, | |
6d2010ae A |
6598 | __unused char *name, |
6599 | __unused int namelen, | |
2d21ac55 A |
6600 | vfs_context_t ctx, |
6601 | struct nfsreq *req, | |
6602 | u_int64_t *xidp, | |
6603 | fhandle_t *fhp, | |
6604 | struct nfs_vattr *nvap) | |
6605 | { | |
b0d623f7 | 6606 | int error = 0, lockerror = ENOENT, status, nfsvers, attrflag; |
2d21ac55 A |
6607 | u_int64_t xid; |
6608 | struct nfsmount *nmp; | |
6609 | struct nfsm_chain nmrep; | |
6610 | ||
6611 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6612 | if (nmp == NULL) { |
6613 | return ENXIO; | |
6614 | } | |
2d21ac55 A |
6615 | nfsvers = nmp->nm_vers; |
6616 | ||
6617 | nfsm_chain_null(&nmrep); | |
6618 | ||
6619 | error = nfs_request_async_finish(req, &nmrep, xidp, &status); | |
6620 | ||
0a7de745 | 6621 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 6622 | error = lockerror; |
0a7de745 | 6623 | } |
2d21ac55 A |
6624 | xid = *xidp; |
6625 | if (error || status) { | |
0a7de745 | 6626 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 6627 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); |
0a7de745 A |
6628 | } |
6629 | if (!error) { | |
2d21ac55 | 6630 | error = status; |
0a7de745 | 6631 | } |
2d21ac55 A |
6632 | goto nfsmout; |
6633 | } | |
6634 | ||
6635 | nfsmout_if(error || !fhp || !nvap); | |
6636 | ||
6637 | /* get the file handle */ | |
6638 | nfsm_chain_get_fh(error, &nmrep, nfsvers, fhp); | |
6639 | ||
6640 | /* get the attributes */ | |
6641 | if (nfsvers == NFS_VER3) { | |
cb323159 | 6642 | nfsm_chain_postop_attr_get(nmp, error, &nmrep, attrflag, nvap); |
2d21ac55 | 6643 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); |
0a7de745 | 6644 | if (!error && !attrflag) { |
6d2010ae | 6645 | error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp); |
0a7de745 | 6646 | } |
2d21ac55 | 6647 | } else { |
cb323159 | 6648 | error = nfs_parsefattr(nmp, &nmrep, nfsvers, nvap); |
2d21ac55 A |
6649 | } |
6650 | nfsmout: | |
0a7de745 | 6651 | if (!lockerror) { |
b0d623f7 | 6652 | nfs_node_unlock(dnp); |
0a7de745 | 6653 | } |
2d21ac55 | 6654 | nfsm_chain_cleanup(&nmrep); |
0a7de745 | 6655 | return error; |
1c79356b A |
6656 | } |
6657 | ||
6658 | /* | |
6659 | * Look up a file name and optionally either update the file handle or | |
6660 | * allocate an nfsnode, depending on the value of npp. | |
6661 | * npp == NULL --> just do the lookup | |
6662 | * *npp == NULL --> allocate a new nfsnode and make sure attributes are | |
6663 | * handled too | |
6664 | * *npp != NULL --> update the file handle in the vnode | |
6665 | */ | |
2d21ac55 A |
6666 | int |
6667 | nfs_lookitup( | |
6668 | nfsnode_t dnp, | |
6669 | char *name, | |
6670 | int namelen, | |
6671 | vfs_context_t ctx, | |
6672 | nfsnode_t *npp) | |
6673 | { | |
6674 | int error = 0; | |
6675 | nfsnode_t np, newnp = NULL; | |
6676 | u_int64_t xid; | |
6677 | fhandle_t fh; | |
6678 | struct nfsmount *nmp; | |
6679 | struct nfs_vattr nvattr; | |
6680 | struct nfsreq rq, *req = &rq; | |
6681 | ||
6682 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6683 | if (nfs_mount_gone(nmp)) { |
6684 | return ENXIO; | |
6685 | } | |
2d21ac55 A |
6686 | |
6687 | if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) && | |
0a7de745 A |
6688 | (namelen > (int)nmp->nm_fsattr.nfsa_maxname)) { |
6689 | return ENAMETOOLONG; | |
6690 | } | |
2d21ac55 | 6691 | |
6d2010ae A |
6692 | NVATTR_INIT(&nvattr); |
6693 | ||
2d21ac55 A |
6694 | /* check for lookup of "." */ |
6695 | if ((name[0] == '.') && (namelen == 1)) { | |
6696 | /* skip lookup, we know who we are */ | |
6697 | fh.fh_len = 0; | |
6698 | newnp = dnp; | |
6699 | goto nfsmout; | |
6700 | } | |
6701 | ||
6702 | error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, name, namelen, ctx, &req); | |
6703 | nfsmout_if(error); | |
6d2010ae | 6704 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, name, namelen, ctx, req, &xid, &fh, &nvattr); |
2d21ac55 A |
6705 | nfsmout_if(!npp || error); |
6706 | ||
6707 | if (*npp) { | |
6708 | np = *npp; | |
6709 | if (fh.fh_len != np->n_fhsize) { | |
6710 | u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL; | |
6711 | if (fh.fh_len > NFS_SMALLFH) { | |
6712 | MALLOC_ZONE(np->n_fhp, u_char *, fh.fh_len, M_NFSBIGFH, M_WAITOK); | |
6713 | if (!np->n_fhp) { | |
0a7de745 A |
6714 | np->n_fhp = oldbuf; |
6715 | error = ENOMEM; | |
6716 | goto nfsmout; | |
2d21ac55 A |
6717 | } |
6718 | } else { | |
6719 | np->n_fhp = &np->n_fh[0]; | |
6720 | } | |
0a7de745 | 6721 | if (oldbuf) { |
2d21ac55 | 6722 | FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH); |
0a7de745 | 6723 | } |
2d21ac55 A |
6724 | } |
6725 | bcopy(fh.fh_data, np->n_fhp, fh.fh_len); | |
6726 | np->n_fhsize = fh.fh_len; | |
b0d623f7 | 6727 | nfs_node_lock_force(np); |
2d21ac55 | 6728 | error = nfs_loadattrcache(np, &nvattr, &xid, 0); |
b0d623f7 | 6729 | nfs_node_unlock(np); |
2d21ac55 A |
6730 | nfsmout_if(error); |
6731 | newnp = np; | |
6732 | } else if (NFS_CMPFH(dnp, fh.fh_data, fh.fh_len)) { | |
b0d623f7 | 6733 | nfs_node_lock_force(dnp); |
0a7de745 | 6734 | if (dnp->n_xid <= xid) { |
2d21ac55 | 6735 | error = nfs_loadattrcache(dnp, &nvattr, &xid, 0); |
0a7de745 | 6736 | } |
b0d623f7 | 6737 | nfs_node_unlock(dnp); |
2d21ac55 A |
6738 | nfsmout_if(error); |
6739 | newnp = dnp; | |
6740 | } else { | |
6741 | struct componentname cn, *cnp = &cn; | |
6742 | bzero(cnp, sizeof(*cnp)); | |
6743 | cnp->cn_nameptr = name; | |
6744 | cnp->cn_namelen = namelen; | |
6745 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, | |
0a7de745 | 6746 | &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np); |
2d21ac55 A |
6747 | nfsmout_if(error); |
6748 | newnp = np; | |
6749 | } | |
6750 | ||
6751 | nfsmout: | |
0a7de745 | 6752 | if (npp && !*npp && !error) { |
2d21ac55 | 6753 | *npp = newnp; |
0a7de745 | 6754 | } |
6d2010ae | 6755 | NVATTR_CLEANUP(&nvattr); |
0a7de745 | 6756 | return error; |
2d21ac55 A |
6757 | } |
6758 | ||
6759 | /* | |
6760 | * set up and initialize a "._" file lookup structure used for | |
6761 | * performing async lookups. | |
6762 | */ | |
6763 | void | |
b0d623f7 | 6764 | nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen, vfs_context_t ctx) |
2d21ac55 A |
6765 | { |
6766 | int error, du_namelen; | |
6767 | vnode_t du_vp; | |
6d2010ae | 6768 | struct nfsmount *nmp = NFSTONMP(dnp); |
2d21ac55 A |
6769 | |
6770 | /* check for ._ file in name cache */ | |
6771 | dulp->du_flags = 0; | |
6772 | bzero(&dulp->du_cn, sizeof(dulp->du_cn)); | |
6773 | du_namelen = namelen + 2; | |
0a7de745 | 6774 | if (!nmp || NMFLAG(nmp, NONEGNAMECACHE)) { |
6d2010ae | 6775 | return; |
0a7de745 A |
6776 | } |
6777 | if ((namelen >= 2) && (name[0] == '.') && (name[1] == '_')) { | |
2d21ac55 | 6778 | return; |
0a7de745 A |
6779 | } |
6780 | if (du_namelen >= (int)sizeof(dulp->du_smallname)) { | |
2d21ac55 | 6781 | MALLOC(dulp->du_cn.cn_nameptr, char *, du_namelen + 1, M_TEMP, M_WAITOK); |
0a7de745 | 6782 | } else { |
2d21ac55 | 6783 | dulp->du_cn.cn_nameptr = dulp->du_smallname; |
0a7de745 A |
6784 | } |
6785 | if (!dulp->du_cn.cn_nameptr) { | |
2d21ac55 | 6786 | return; |
0a7de745 | 6787 | } |
2d21ac55 A |
6788 | dulp->du_cn.cn_namelen = du_namelen; |
6789 | snprintf(dulp->du_cn.cn_nameptr, du_namelen + 1, "._%s", name); | |
6790 | dulp->du_cn.cn_nameptr[du_namelen] = '\0'; | |
b0d623f7 A |
6791 | dulp->du_cn.cn_nameiop = LOOKUP; |
6792 | dulp->du_cn.cn_flags = MAKEENTRY; | |
2d21ac55 A |
6793 | |
6794 | error = cache_lookup(NFSTOV(dnp), &du_vp, &dulp->du_cn); | |
b0d623f7 | 6795 | if (error == -1) { |
2d21ac55 | 6796 | vnode_put(du_vp); |
b0d623f7 | 6797 | } else if (!error) { |
6d2010ae A |
6798 | nmp = NFSTONMP(dnp); |
6799 | if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { | |
b0d623f7 A |
6800 | /* if rdirplus, try dir buf cache lookup */ |
6801 | nfsnode_t du_np = NULL; | |
6802 | if (!nfs_dir_buf_cache_lookup(dnp, &du_np, &dulp->du_cn, ctx, 0) && du_np) { | |
6803 | /* dir buf cache hit */ | |
6804 | du_vp = NFSTOV(du_np); | |
6805 | vnode_put(du_vp); | |
6806 | error = -1; | |
6807 | } | |
6808 | } | |
0a7de745 | 6809 | if (!error) { |
b0d623f7 | 6810 | dulp->du_flags |= NFS_DULOOKUP_DOIT; |
0a7de745 | 6811 | } |
b0d623f7 | 6812 | } |
2d21ac55 A |
6813 | } |
6814 | ||
6815 | /* | |
6816 | * start an async "._" file lookup request | |
6817 | */ | |
6818 | void | |
6819 | nfs_dulookup_start(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx) | |
6820 | { | |
6821 | struct nfsmount *nmp = NFSTONMP(dnp); | |
6822 | struct nfsreq *req = &dulp->du_req; | |
6823 | ||
0a7de745 | 6824 | if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT) || (dulp->du_flags & NFS_DULOOKUP_INPROG)) { |
2d21ac55 | 6825 | return; |
0a7de745 | 6826 | } |
2d21ac55 | 6827 | if (!nmp->nm_funcs->nf_lookup_rpc_async(dnp, dulp->du_cn.cn_nameptr, |
0a7de745 | 6828 | dulp->du_cn.cn_namelen, ctx, &req)) { |
2d21ac55 | 6829 | dulp->du_flags |= NFS_DULOOKUP_INPROG; |
0a7de745 | 6830 | } |
2d21ac55 A |
6831 | } |
6832 | ||
6833 | /* | |
6834 | * finish an async "._" file lookup request and clean up the structure | |
6835 | */ | |
6836 | void | |
6837 | nfs_dulookup_finish(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx) | |
1c79356b | 6838 | { |
2d21ac55 A |
6839 | struct nfsmount *nmp = NFSTONMP(dnp); |
6840 | int error; | |
6841 | nfsnode_t du_np; | |
6842 | u_int64_t xid; | |
6843 | fhandle_t fh; | |
91447636 | 6844 | struct nfs_vattr nvattr; |
1c79356b | 6845 | |
0a7de745 | 6846 | if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_INPROG)) { |
2d21ac55 | 6847 | goto out; |
0a7de745 | 6848 | } |
55e303ae | 6849 | |
6d2010ae A |
6850 | NVATTR_INIT(&nvattr); |
6851 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, dulp->du_cn.cn_nameptr, | |
0a7de745 | 6852 | dulp->du_cn.cn_namelen, ctx, &dulp->du_req, &xid, &fh, &nvattr); |
2d21ac55 A |
6853 | dulp->du_flags &= ~NFS_DULOOKUP_INPROG; |
6854 | if (error == ENOENT) { | |
6855 | /* add a negative entry in the name cache */ | |
b0d623f7 | 6856 | nfs_node_lock_force(dnp); |
2d21ac55 A |
6857 | cache_enter(NFSTOV(dnp), NULL, &dulp->du_cn); |
6858 | dnp->n_flag |= NNEGNCENTRIES; | |
b0d623f7 | 6859 | nfs_node_unlock(dnp); |
2d21ac55 A |
6860 | } else if (!error) { |
6861 | error = nfs_nget(NFSTOMP(dnp), dnp, &dulp->du_cn, fh.fh_data, fh.fh_len, | |
0a7de745 | 6862 | &nvattr, &xid, dulp->du_req.r_auth, NG_MAKEENTRY, &du_np); |
2d21ac55 | 6863 | if (!error) { |
b0d623f7 | 6864 | nfs_node_unlock(du_np); |
2d21ac55 | 6865 | vnode_put(NFSTOV(du_np)); |
1c79356b | 6866 | } |
1c79356b | 6867 | } |
6d2010ae | 6868 | NVATTR_CLEANUP(&nvattr); |
2d21ac55 | 6869 | out: |
0a7de745 | 6870 | if (dulp->du_flags & NFS_DULOOKUP_INPROG) { |
2d21ac55 | 6871 | nfs_request_async_cancel(&dulp->du_req); |
0a7de745 A |
6872 | } |
6873 | if (dulp->du_cn.cn_nameptr && (dulp->du_cn.cn_nameptr != dulp->du_smallname)) { | |
2d21ac55 | 6874 | FREE(dulp->du_cn.cn_nameptr, M_TEMP); |
0a7de745 | 6875 | } |
1c79356b A |
6876 | } |
6877 | ||
2d21ac55 | 6878 | |
1c79356b | 6879 | /* |
2d21ac55 | 6880 | * NFS Version 3 commit RPC |
1c79356b | 6881 | */ |
55e303ae | 6882 | int |
2d21ac55 A |
6883 | nfs3_commit_rpc( |
6884 | nfsnode_t np, | |
6d2010ae A |
6885 | uint64_t offset, |
6886 | uint64_t count, | |
6887 | kauth_cred_t cred, | |
6888 | uint64_t wverf) | |
1c79356b | 6889 | { |
2d21ac55 A |
6890 | struct nfsmount *nmp; |
6891 | int error = 0, lockerror, status, wccpostattr = 0, nfsvers; | |
cb323159 | 6892 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
6d2010ae | 6893 | u_int64_t xid, newwverf; |
2d21ac55 A |
6894 | uint32_t count32; |
6895 | struct nfsm_chain nmreq, nmrep; | |
8f6c56a5 | 6896 | |
2d21ac55 A |
6897 | nmp = NFSTONMP(np); |
6898 | FSDBG(521, np, offset, count, nmp ? nmp->nm_state : 0); | |
0a7de745 A |
6899 | if (nfs_mount_gone(nmp)) { |
6900 | return ENXIO; | |
6901 | } | |
6902 | if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) { | |
6903 | return 0; | |
6904 | } | |
2d21ac55 A |
6905 | nfsvers = nmp->nm_vers; |
6906 | ||
0a7de745 | 6907 | if (count > UINT32_MAX) { |
2d21ac55 | 6908 | count32 = 0; |
0a7de745 | 6909 | } else { |
2d21ac55 | 6910 | count32 = count; |
0a7de745 | 6911 | } |
2d21ac55 A |
6912 | |
6913 | nfsm_chain_null(&nmreq); | |
6914 | nfsm_chain_null(&nmrep); | |
6915 | ||
6916 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3)); | |
6917 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
6918 | nfsm_chain_add_64(error, &nmreq, offset); | |
6919 | nfsm_chain_add_32(error, &nmreq, count32); | |
6920 | nfsm_chain_build_done(error, &nmreq); | |
6921 | nfsmout_if(error); | |
6922 | error = nfs_request2(np, NULL, &nmreq, NFSPROC_COMMIT, | |
0a7de745 A |
6923 | current_thread(), cred, NULL, 0, &nmrep, &xid, &status); |
6924 | if ((lockerror = nfs_node_lock(np))) { | |
2d21ac55 | 6925 | error = lockerror; |
0a7de745 | 6926 | } |
2d21ac55 A |
6927 | /* can we do anything useful with the wcc info? */ |
6928 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); | |
0a7de745 | 6929 | if (!lockerror) { |
b0d623f7 | 6930 | nfs_node_unlock(np); |
0a7de745 A |
6931 | } |
6932 | if (!error) { | |
2d21ac55 | 6933 | error = status; |
0a7de745 | 6934 | } |
6d2010ae | 6935 | nfsm_chain_get_64(error, &nmrep, newwverf); |
2d21ac55 A |
6936 | nfsmout_if(error); |
6937 | lck_mtx_lock(&nmp->nm_lock); | |
0a7de745 | 6938 | if (nmp->nm_verf != newwverf) { |
6d2010ae | 6939 | nmp->nm_verf = newwverf; |
0a7de745 A |
6940 | } |
6941 | if (wverf != newwverf) { | |
2d21ac55 | 6942 | error = NFSERR_STALEWRITEVERF; |
0a7de745 | 6943 | } |
2d21ac55 A |
6944 | lck_mtx_unlock(&nmp->nm_lock); |
6945 | nfsmout: | |
6946 | nfsm_chain_cleanup(&nmreq); | |
6947 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 6948 | return error; |
1c79356b A |
6949 | } |
6950 | ||
2d21ac55 | 6951 | |
b0d623f7 | 6952 | int |
2d21ac55 | 6953 | nfs_vnop_blockmap( |
91447636 | 6954 | __unused struct vnop_blockmap_args /* { |
0a7de745 A |
6955 | * struct vnodeop_desc *a_desc; |
6956 | * vnode_t a_vp; | |
6957 | * off_t a_foffset; | |
6958 | * size_t a_size; | |
6959 | * daddr64_t *a_bpn; | |
6960 | * size_t *a_run; | |
6961 | * void *a_poff; | |
6962 | * int a_flags; | |
6963 | * } */*ap) | |
1c79356b | 6964 | { |
0a7de745 | 6965 | return ENOTSUP; |
1c79356b A |
6966 | } |
6967 | ||
1c79356b A |
6968 | |
6969 | /* | |
2d21ac55 | 6970 | * fsync vnode op. Just call nfs_flush(). |
1c79356b A |
6971 | */ |
6972 | /* ARGSUSED */ | |
b0d623f7 | 6973 | int |
2d21ac55 | 6974 | nfs_vnop_fsync( |
91447636 | 6975 | struct vnop_fsync_args /* { |
0a7de745 A |
6976 | * struct vnodeop_desc *a_desc; |
6977 | * vnode_t a_vp; | |
6978 | * int a_waitfor; | |
6979 | * vfs_context_t a_context; | |
6980 | * } */*ap) | |
1c79356b | 6981 | { |
0a7de745 | 6982 | return nfs_flush(VTONFS(ap->a_vp), ap->a_waitfor, vfs_context_thread(ap->a_context), 0); |
1c79356b | 6983 | } |
1c79356b | 6984 | |
1c79356b | 6985 | |
55e303ae | 6986 | /* |
2d21ac55 | 6987 | * Do an NFS pathconf RPC. |
55e303ae | 6988 | */ |
91447636 | 6989 | int |
2d21ac55 A |
6990 | nfs3_pathconf_rpc( |
6991 | nfsnode_t np, | |
6992 | struct nfs_fsattr *nfsap, | |
6993 | vfs_context_t ctx) | |
55e303ae | 6994 | { |
2d21ac55 A |
6995 | u_int64_t xid; |
6996 | int error = 0, lockerror, status, nfsvers; | |
6997 | struct nfsm_chain nmreq, nmrep; | |
6998 | struct nfsmount *nmp = NFSTONMP(np); | |
6999 | uint32_t val = 0; | |
483a1d10 | 7000 | |
0a7de745 A |
7001 | if (nfs_mount_gone(nmp)) { |
7002 | return ENXIO; | |
7003 | } | |
2d21ac55 | 7004 | nfsvers = nmp->nm_vers; |
1c79356b | 7005 | |
2d21ac55 A |
7006 | nfsm_chain_null(&nmreq); |
7007 | nfsm_chain_null(&nmrep); | |
1c79356b | 7008 | |
91447636 | 7009 | /* fetch pathconf info from server */ |
2d21ac55 A |
7010 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3)); |
7011 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
7012 | nfsm_chain_build_done(error, &nmreq); | |
7013 | nfsmout_if(error); | |
6d2010ae | 7014 | error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx, NULL, &nmrep, &xid, &status); |
0a7de745 | 7015 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 7016 | error = lockerror; |
0a7de745 | 7017 | } |
2d21ac55 | 7018 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 | 7019 | if (!lockerror) { |
b0d623f7 | 7020 | nfs_node_unlock(np); |
0a7de745 A |
7021 | } |
7022 | if (!error) { | |
2d21ac55 | 7023 | error = status; |
0a7de745 | 7024 | } |
2d21ac55 A |
7025 | nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxlink); |
7026 | nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxname); | |
0a7de745 | 7027 | nfsap->nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC | NFS_FSFLAG_CHOWN_RESTRICTED | NFS_FSFLAG_CASE_INSENSITIVE | NFS_FSFLAG_CASE_PRESERVING); |
2d21ac55 | 7028 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7029 | if (val) { |
2d21ac55 | 7030 | nfsap->nfsa_flags |= NFS_FSFLAG_NO_TRUNC; |
0a7de745 | 7031 | } |
2d21ac55 | 7032 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7033 | if (val) { |
2d21ac55 | 7034 | nfsap->nfsa_flags |= NFS_FSFLAG_CHOWN_RESTRICTED; |
0a7de745 | 7035 | } |
2d21ac55 | 7036 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7037 | if (val) { |
2d21ac55 | 7038 | nfsap->nfsa_flags |= NFS_FSFLAG_CASE_INSENSITIVE; |
0a7de745 | 7039 | } |
2d21ac55 | 7040 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7041 | if (val) { |
2d21ac55 | 7042 | nfsap->nfsa_flags |= NFS_FSFLAG_CASE_PRESERVING; |
0a7de745 | 7043 | } |
2d21ac55 A |
7044 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK); |
7045 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME); | |
7046 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC); | |
7047 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED); | |
7048 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE); | |
7049 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING); | |
7050 | nfsmout: | |
7051 | nfsm_chain_cleanup(&nmreq); | |
7052 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 7053 | return error; |
1c79356b A |
7054 | } |
7055 | ||
2d21ac55 | 7056 | /* save pathconf info for NFSv3 mount */ |
91447636 | 7057 | void |
2d21ac55 | 7058 | nfs3_pathconf_cache(struct nfsmount *nmp, struct nfs_fsattr *nfsap) |
1c79356b | 7059 | { |
2d21ac55 A |
7060 | nmp->nm_fsattr.nfsa_maxlink = nfsap->nfsa_maxlink; |
7061 | nmp->nm_fsattr.nfsa_maxname = nfsap->nfsa_maxname; | |
0a7de745 | 7062 | nmp->nm_fsattr.nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC | NFS_FSFLAG_CHOWN_RESTRICTED | NFS_FSFLAG_CASE_INSENSITIVE | NFS_FSFLAG_CASE_PRESERVING); |
2d21ac55 A |
7063 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC; |
7064 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED; | |
7065 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE; | |
7066 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING; | |
7067 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXLINK); | |
7068 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME); | |
7069 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_NO_TRUNC); | |
7070 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED); | |
7071 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE); | |
7072 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING); | |
91447636 | 7073 | nmp->nm_state |= NFSSTA_GOTPATHCONF; |
1c79356b A |
7074 | } |
7075 | ||
7076 | /* | |
91447636 A |
7077 | * Return POSIX pathconf information applicable to nfs. |
7078 | * | |
7079 | * The NFS V2 protocol doesn't support this, so just return EINVAL | |
7080 | * for V2. | |
1c79356b | 7081 | */ |
91447636 | 7082 | /* ARGSUSED */ |
b0d623f7 | 7083 | int |
2d21ac55 | 7084 | nfs_vnop_pathconf( |
91447636 | 7085 | struct vnop_pathconf_args /* { |
0a7de745 A |
7086 | * struct vnodeop_desc *a_desc; |
7087 | * vnode_t a_vp; | |
7088 | * int a_name; | |
7089 | * int32_t *a_retval; | |
7090 | * vfs_context_t a_context; | |
7091 | * } */*ap) | |
1c79356b | 7092 | { |
91447636 | 7093 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7094 | nfsnode_t np = VTONFS(vp); |
91447636 | 7095 | struct nfsmount *nmp; |
2d21ac55 A |
7096 | struct nfs_fsattr nfsa, *nfsap; |
7097 | int error = 0; | |
7098 | uint64_t maxFileSize; | |
7099 | uint nbits; | |
1c79356b | 7100 | |
2d21ac55 | 7101 | nmp = VTONMP(vp); |
0a7de745 A |
7102 | if (nfs_mount_gone(nmp)) { |
7103 | return ENXIO; | |
7104 | } | |
1c79356b | 7105 | |
91447636 A |
7106 | switch (ap->a_name) { |
7107 | case _PC_LINK_MAX: | |
7108 | case _PC_NAME_MAX: | |
7109 | case _PC_CHOWN_RESTRICTED: | |
7110 | case _PC_NO_TRUNC: | |
7111 | case _PC_CASE_SENSITIVE: | |
7112 | case _PC_CASE_PRESERVING: | |
7113 | break; | |
2d21ac55 A |
7114 | case _PC_FILESIZEBITS: |
7115 | if (nmp->nm_vers == NFS_VER2) { | |
7116 | *ap->a_retval = 32; | |
0a7de745 | 7117 | return 0; |
2d21ac55 A |
7118 | } |
7119 | break; | |
6d2010ae A |
7120 | case _PC_XATTR_SIZE_BITS: |
7121 | /* Do we support xattrs natively? */ | |
0a7de745 | 7122 | if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) { |
6d2010ae | 7123 | break; /* Yes */ |
0a7de745 A |
7124 | } |
7125 | /* No... so just return an error */ | |
7126 | /* FALLTHROUGH */ | |
91447636 A |
7127 | default: |
7128 | /* don't bother contacting the server if we know the answer */ | |
0a7de745 | 7129 | return EINVAL; |
91447636 | 7130 | } |
1c79356b | 7131 | |
0a7de745 A |
7132 | if (nmp->nm_vers == NFS_VER2) { |
7133 | return EINVAL; | |
7134 | } | |
2d21ac55 A |
7135 | |
7136 | lck_mtx_lock(&nmp->nm_lock); | |
7137 | if (nmp->nm_vers == NFS_VER3) { | |
7138 | if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) { | |
7139 | /* no pathconf info cached */ | |
7140 | lck_mtx_unlock(&nmp->nm_lock); | |
7141 | NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap); | |
7142 | error = nfs3_pathconf_rpc(np, &nfsa, ap->a_context); | |
0a7de745 A |
7143 | if (error) { |
7144 | return error; | |
7145 | } | |
2d21ac55 | 7146 | nmp = VTONMP(vp); |
0a7de745 A |
7147 | if (nfs_mount_gone(nmp)) { |
7148 | return ENXIO; | |
7149 | } | |
2d21ac55 A |
7150 | lck_mtx_lock(&nmp->nm_lock); |
7151 | if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS) { | |
7152 | /* all files have the same pathconf info, */ | |
7153 | /* so cache a copy of the results */ | |
7154 | nfs3_pathconf_cache(nmp, &nfsa); | |
7155 | } | |
7156 | nfsap = &nfsa; | |
7157 | } else { | |
7158 | nfsap = &nmp->nm_fsattr; | |
7159 | } | |
cb323159 A |
7160 | } |
7161 | #if CONFIG_NFS4 | |
7162 | else if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) { | |
91447636 | 7163 | /* no pathconf info cached */ |
2d21ac55 A |
7164 | lck_mtx_unlock(&nmp->nm_lock); |
7165 | NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap); | |
7166 | error = nfs4_pathconf_rpc(np, &nfsa, ap->a_context); | |
0a7de745 A |
7167 | if (error) { |
7168 | return error; | |
7169 | } | |
2d21ac55 | 7170 | nmp = VTONMP(vp); |
0a7de745 A |
7171 | if (nfs_mount_gone(nmp)) { |
7172 | return ENXIO; | |
7173 | } | |
2d21ac55 A |
7174 | lck_mtx_lock(&nmp->nm_lock); |
7175 | nfsap = &nfsa; | |
cb323159 A |
7176 | } |
7177 | #endif | |
7178 | else { | |
2d21ac55 | 7179 | nfsap = &nmp->nm_fsattr; |
91447636 | 7180 | } |
91447636 A |
7181 | switch (ap->a_name) { |
7182 | case _PC_LINK_MAX: | |
0a7de745 | 7183 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK)) { |
2d21ac55 | 7184 | *ap->a_retval = nfsap->nfsa_maxlink; |
cb323159 | 7185 | #if CONFIG_NFS4 |
0a7de745 | 7186 | } else if ((nmp->nm_vers == NFS_VER4) && NFS_BITMAP_ISSET(np->n_vattr.nva_bitmap, NFS_FATTR_MAXLINK)) { |
2d21ac55 | 7187 | *ap->a_retval = np->n_vattr.nva_maxlink; |
cb323159 | 7188 | #endif |
0a7de745 | 7189 | } else { |
2d21ac55 | 7190 | error = EINVAL; |
0a7de745 | 7191 | } |
91447636 | 7192 | break; |
2d21ac55 | 7193 | case _PC_NAME_MAX: |
0a7de745 | 7194 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME)) { |
2d21ac55 | 7195 | *ap->a_retval = nfsap->nfsa_maxname; |
0a7de745 | 7196 | } else { |
2d21ac55 | 7197 | error = EINVAL; |
0a7de745 | 7198 | } |
2d21ac55 A |
7199 | break; |
7200 | case _PC_CHOWN_RESTRICTED: | |
0a7de745 | 7201 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED)) { |
b0d623f7 | 7202 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0; |
0a7de745 | 7203 | } else { |
2d21ac55 | 7204 | error = EINVAL; |
0a7de745 | 7205 | } |
2d21ac55 A |
7206 | break; |
7207 | case _PC_NO_TRUNC: | |
0a7de745 | 7208 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC)) { |
b0d623f7 | 7209 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0; |
0a7de745 | 7210 | } else { |
2d21ac55 | 7211 | error = EINVAL; |
0a7de745 | 7212 | } |
2d21ac55 A |
7213 | break; |
7214 | case _PC_CASE_SENSITIVE: | |
0a7de745 | 7215 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE)) { |
b0d623f7 | 7216 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1; |
0a7de745 | 7217 | } else { |
2d21ac55 | 7218 | error = EINVAL; |
0a7de745 | 7219 | } |
2d21ac55 A |
7220 | break; |
7221 | case _PC_CASE_PRESERVING: | |
0a7de745 | 7222 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING)) { |
b0d623f7 | 7223 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0; |
0a7de745 | 7224 | } else { |
2d21ac55 | 7225 | error = EINVAL; |
0a7de745 | 7226 | } |
2d21ac55 | 7227 | break; |
6d2010ae | 7228 | case _PC_XATTR_SIZE_BITS: /* same as file size bits if named attrs supported */ |
2d21ac55 | 7229 | case _PC_FILESIZEBITS: |
b0d623f7 | 7230 | if (!NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) { |
2d21ac55 A |
7231 | *ap->a_retval = 64; |
7232 | error = 0; | |
7233 | break; | |
1c79356b | 7234 | } |
b0d623f7 | 7235 | maxFileSize = nfsap->nfsa_maxfilesize; |
2d21ac55 A |
7236 | nbits = 1; |
7237 | if (maxFileSize & 0xffffffff00000000ULL) { | |
7238 | nbits += 32; | |
7239 | maxFileSize >>= 32; | |
91447636 | 7240 | } |
2d21ac55 A |
7241 | if (maxFileSize & 0xffff0000) { |
7242 | nbits += 16; | |
7243 | maxFileSize >>= 16; | |
7244 | } | |
7245 | if (maxFileSize & 0xff00) { | |
7246 | nbits += 8; | |
7247 | maxFileSize >>= 8; | |
7248 | } | |
7249 | if (maxFileSize & 0xf0) { | |
7250 | nbits += 4; | |
7251 | maxFileSize >>= 4; | |
91447636 | 7252 | } |
2d21ac55 A |
7253 | if (maxFileSize & 0xc) { |
7254 | nbits += 2; | |
7255 | maxFileSize >>= 2; | |
7256 | } | |
7257 | if (maxFileSize & 0x2) { | |
7258 | nbits += 1; | |
7259 | } | |
7260 | *ap->a_retval = nbits; | |
7261 | break; | |
7262 | default: | |
7263 | error = EINVAL; | |
7264 | } | |
1c79356b | 7265 | |
2d21ac55 A |
7266 | lck_mtx_unlock(&nmp->nm_lock); |
7267 | ||
0a7de745 | 7268 | return error; |
1c79356b A |
7269 | } |
7270 | ||
1c79356b A |
7271 | /* |
7272 | * Read wrapper for special devices. | |
7273 | */ | |
b0d623f7 | 7274 | int |
2d21ac55 | 7275 | nfsspec_vnop_read( |
91447636 | 7276 | struct vnop_read_args /* { |
0a7de745 A |
7277 | * struct vnodeop_desc *a_desc; |
7278 | * vnode_t a_vp; | |
7279 | * struct uio *a_uio; | |
7280 | * int a_ioflag; | |
7281 | * vfs_context_t a_context; | |
7282 | * } */*ap) | |
1c79356b | 7283 | { |
2d21ac55 | 7284 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7285 | struct timespec now; |
2d21ac55 | 7286 | int error; |
1c79356b A |
7287 | |
7288 | /* | |
7289 | * Set access flag. | |
7290 | */ | |
0a7de745 A |
7291 | if ((error = nfs_node_lock(np))) { |
7292 | return error; | |
7293 | } | |
1c79356b | 7294 | np->n_flag |= NACC; |
5ba3f43e | 7295 | nanotime(&now); |
55e303ae | 7296 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 7297 | np->n_atim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7298 | nfs_node_unlock(np); |
0a7de745 | 7299 | return VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap); |
1c79356b A |
7300 | } |
7301 | ||
7302 | /* | |
7303 | * Write wrapper for special devices. | |
7304 | */ | |
b0d623f7 | 7305 | int |
2d21ac55 | 7306 | nfsspec_vnop_write( |
91447636 | 7307 | struct vnop_write_args /* { |
0a7de745 A |
7308 | * struct vnodeop_desc *a_desc; |
7309 | * vnode_t a_vp; | |
7310 | * struct uio *a_uio; | |
7311 | * int a_ioflag; | |
7312 | * vfs_context_t a_context; | |
7313 | * } */*ap) | |
1c79356b | 7314 | { |
2d21ac55 | 7315 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7316 | struct timespec now; |
2d21ac55 | 7317 | int error; |
1c79356b A |
7318 | |
7319 | /* | |
7320 | * Set update flag. | |
7321 | */ | |
0a7de745 A |
7322 | if ((error = nfs_node_lock(np))) { |
7323 | return error; | |
7324 | } | |
1c79356b | 7325 | np->n_flag |= NUPD; |
5ba3f43e | 7326 | nanotime(&now); |
55e303ae | 7327 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 7328 | np->n_mtim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7329 | nfs_node_unlock(np); |
0a7de745 | 7330 | return VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap); |
1c79356b A |
7331 | } |
7332 | ||
7333 | /* | |
7334 | * Close wrapper for special devices. | |
7335 | * | |
7336 | * Update the times on the nfsnode then do device close. | |
7337 | */ | |
b0d623f7 | 7338 | int |
2d21ac55 | 7339 | nfsspec_vnop_close( |
91447636 | 7340 | struct vnop_close_args /* { |
0a7de745 A |
7341 | * struct vnodeop_desc *a_desc; |
7342 | * vnode_t a_vp; | |
7343 | * int a_fflag; | |
7344 | * vfs_context_t a_context; | |
7345 | * } */*ap) | |
1c79356b | 7346 | { |
91447636 | 7347 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7348 | nfsnode_t np = VTONFS(vp); |
91447636 A |
7349 | struct vnode_attr vattr; |
7350 | mount_t mp; | |
2d21ac55 | 7351 | int error; |
1c79356b | 7352 | |
0a7de745 A |
7353 | if ((error = nfs_node_lock(np))) { |
7354 | return error; | |
7355 | } | |
1c79356b A |
7356 | if (np->n_flag & (NACC | NUPD)) { |
7357 | np->n_flag |= NCHG; | |
b0d623f7 | 7358 | if (!vnode_isinuse(vp, 0) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) { |
91447636 A |
7359 | VATTR_INIT(&vattr); |
7360 | if (np->n_flag & NACC) { | |
7361 | vattr.va_access_time = np->n_atim; | |
7362 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
7363 | } | |
7364 | if (np->n_flag & NUPD) { | |
7365 | vattr.va_modify_time = np->n_mtim; | |
7366 | VATTR_SET_ACTIVE(&vattr, va_modify_time); | |
7367 | } | |
b0d623f7 | 7368 | nfs_node_unlock(np); |
91447636 | 7369 | vnode_setattr(vp, &vattr, ap->a_context); |
2d21ac55 | 7370 | } else { |
b0d623f7 | 7371 | nfs_node_unlock(np); |
1c79356b | 7372 | } |
2d21ac55 | 7373 | } else { |
b0d623f7 | 7374 | nfs_node_unlock(np); |
1c79356b | 7375 | } |
0a7de745 | 7376 | return VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap); |
1c79356b A |
7377 | } |
7378 | ||
2d21ac55 | 7379 | #if FIFO |
91447636 A |
7380 | extern vnop_t **fifo_vnodeop_p; |
7381 | ||
1c79356b A |
7382 | /* |
7383 | * Read wrapper for fifos. | |
7384 | */ | |
b0d623f7 | 7385 | int |
2d21ac55 | 7386 | nfsfifo_vnop_read( |
91447636 | 7387 | struct vnop_read_args /* { |
0a7de745 A |
7388 | * struct vnodeop_desc *a_desc; |
7389 | * vnode_t a_vp; | |
7390 | * struct uio *a_uio; | |
7391 | * int a_ioflag; | |
7392 | * vfs_context_t a_context; | |
7393 | * } */*ap) | |
1c79356b | 7394 | { |
2d21ac55 | 7395 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7396 | struct timespec now; |
2d21ac55 | 7397 | int error; |
1c79356b A |
7398 | |
7399 | /* | |
7400 | * Set access flag. | |
7401 | */ | |
0a7de745 A |
7402 | if ((error = nfs_node_lock(np))) { |
7403 | return error; | |
7404 | } | |
1c79356b | 7405 | np->n_flag |= NACC; |
5ba3f43e | 7406 | nanotime(&now); |
55e303ae | 7407 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 7408 | np->n_atim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7409 | nfs_node_unlock(np); |
0a7de745 | 7410 | return VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap); |
1c79356b A |
7411 | } |
7412 | ||
7413 | /* | |
7414 | * Write wrapper for fifos. | |
7415 | */ | |
b0d623f7 | 7416 | int |
2d21ac55 | 7417 | nfsfifo_vnop_write( |
91447636 | 7418 | struct vnop_write_args /* { |
0a7de745 A |
7419 | * struct vnodeop_desc *a_desc; |
7420 | * vnode_t a_vp; | |
7421 | * struct uio *a_uio; | |
7422 | * int a_ioflag; | |
7423 | * vfs_context_t a_context; | |
7424 | * } */*ap) | |
1c79356b | 7425 | { |
2d21ac55 | 7426 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7427 | struct timespec now; |
2d21ac55 | 7428 | int error; |
1c79356b A |
7429 | |
7430 | /* | |
7431 | * Set update flag. | |
7432 | */ | |
0a7de745 A |
7433 | if ((error = nfs_node_lock(np))) { |
7434 | return error; | |
7435 | } | |
1c79356b | 7436 | np->n_flag |= NUPD; |
5ba3f43e | 7437 | nanotime(&now); |
55e303ae | 7438 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 7439 | np->n_mtim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7440 | nfs_node_unlock(np); |
0a7de745 | 7441 | return VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap); |
1c79356b A |
7442 | } |
7443 | ||
7444 | /* | |
7445 | * Close wrapper for fifos. | |
7446 | * | |
7447 | * Update the times on the nfsnode then do fifo close. | |
7448 | */ | |
b0d623f7 | 7449 | int |
2d21ac55 | 7450 | nfsfifo_vnop_close( |
91447636 | 7451 | struct vnop_close_args /* { |
0a7de745 A |
7452 | * struct vnodeop_desc *a_desc; |
7453 | * vnode_t a_vp; | |
7454 | * int a_fflag; | |
7455 | * vfs_context_t a_context; | |
7456 | * } */*ap) | |
1c79356b | 7457 | { |
91447636 | 7458 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7459 | nfsnode_t np = VTONFS(vp); |
91447636 | 7460 | struct vnode_attr vattr; |
5ba3f43e | 7461 | struct timespec now; |
91447636 | 7462 | mount_t mp; |
2d21ac55 | 7463 | int error; |
1c79356b | 7464 | |
0a7de745 A |
7465 | if ((error = nfs_node_lock(np))) { |
7466 | return error; | |
7467 | } | |
1c79356b | 7468 | if (np->n_flag & (NACC | NUPD)) { |
5ba3f43e | 7469 | nanotime(&now); |
1c79356b | 7470 | if (np->n_flag & NACC) { |
55e303ae | 7471 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 7472 | np->n_atim.tv_nsec = now.tv_nsec; |
1c79356b A |
7473 | } |
7474 | if (np->n_flag & NUPD) { | |
55e303ae | 7475 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 7476 | np->n_mtim.tv_nsec = now.tv_nsec; |
1c79356b A |
7477 | } |
7478 | np->n_flag |= NCHG; | |
91447636 A |
7479 | if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) { |
7480 | VATTR_INIT(&vattr); | |
7481 | if (np->n_flag & NACC) { | |
7482 | vattr.va_access_time = np->n_atim; | |
7483 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
7484 | } | |
7485 | if (np->n_flag & NUPD) { | |
7486 | vattr.va_modify_time = np->n_mtim; | |
7487 | VATTR_SET_ACTIVE(&vattr, va_modify_time); | |
7488 | } | |
b0d623f7 | 7489 | nfs_node_unlock(np); |
91447636 | 7490 | vnode_setattr(vp, &vattr, ap->a_context); |
2d21ac55 | 7491 | } else { |
b0d623f7 | 7492 | nfs_node_unlock(np); |
1c79356b | 7493 | } |
2d21ac55 | 7494 | } else { |
b0d623f7 | 7495 | nfs_node_unlock(np); |
1c79356b | 7496 | } |
0a7de745 | 7497 | return VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap); |
1c79356b | 7498 | } |
2d21ac55 | 7499 | #endif /* FIFO */ |
1c79356b | 7500 | |
91447636 | 7501 | /*ARGSUSED*/ |
b0d623f7 | 7502 | int |
2d21ac55 | 7503 | nfs_vnop_ioctl( |
6d2010ae | 7504 | struct vnop_ioctl_args /* { |
0a7de745 A |
7505 | * struct vnodeop_desc *a_desc; |
7506 | * vnode_t a_vp; | |
7507 | * u_int32_t a_command; | |
7508 | * caddr_t a_data; | |
7509 | * int a_fflag; | |
7510 | * vfs_context_t a_context; | |
7511 | * } */*ap) | |
1c79356b | 7512 | { |
6d2010ae A |
7513 | vfs_context_t ctx = ap->a_context; |
7514 | vnode_t vp = ap->a_vp; | |
39236c6e | 7515 | struct nfsmount *mp = VTONMP(vp); |
cb323159 A |
7516 | int error = ENOTTY; |
7517 | #if CONFIG_NFS_GSS | |
d9a64523 | 7518 | struct user_nfs_gss_principal gprinc = {}; |
3e170ce0 | 7519 | uint32_t len; |
cb323159 | 7520 | #endif |
1c79356b | 7521 | |
0a7de745 A |
7522 | if (mp == NULL) { |
7523 | return ENXIO; | |
7524 | } | |
0a7de745 | 7525 | switch (ap->a_command) { |
6d2010ae | 7526 | case F_FULLFSYNC: |
0a7de745 A |
7527 | if (vnode_vfsisrdonly(vp)) { |
7528 | return EROFS; | |
7529 | } | |
6d2010ae A |
7530 | error = nfs_flush(VTONFS(vp), MNT_WAIT, vfs_context_thread(ctx), 0); |
7531 | break; | |
cb323159 | 7532 | #if CONFIG_NFS_GSS |
5ba3f43e | 7533 | case NFS_IOC_DESTROY_CRED: |
0a7de745 A |
7534 | if (!auth_is_kerberized(mp->nm_auth)) { |
7535 | return ENOTSUP; | |
7536 | } | |
fe8ab488 | 7537 | error = nfs_gss_clnt_ctx_remove(mp, vfs_context_ucred(ctx)); |
39236c6e | 7538 | break; |
5ba3f43e A |
7539 | case NFS_IOC_SET_CRED: |
7540 | case NFS_IOC_SET_CRED64: | |
0a7de745 A |
7541 | if (!auth_is_kerberized(mp->nm_auth)) { |
7542 | return ENOTSUP; | |
7543 | } | |
5ba3f43e | 7544 | if ((ap->a_command == NFS_IOC_SET_CRED && vfs_context_is64bit(ctx)) || |
0a7de745 A |
7545 | (ap->a_command == NFS_IOC_SET_CRED64 && !vfs_context_is64bit(ctx))) { |
7546 | return EINVAL; | |
7547 | } | |
3e170ce0 A |
7548 | if (vfs_context_is64bit(ctx)) { |
7549 | gprinc = *(struct user_nfs_gss_principal *)ap->a_data; | |
7550 | } else { | |
7551 | struct nfs_gss_principal *tp; | |
7552 | tp = (struct nfs_gss_principal *)ap->a_data; | |
7553 | gprinc.princlen = tp->princlen; | |
7554 | gprinc.nametype = tp->nametype; | |
7555 | gprinc.principal = CAST_USER_ADDR_T(tp->principal); | |
7556 | } | |
39037602 | 7557 | NFS_DBG(NFS_FAC_GSS, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %d name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx), gprinc.princlen, gprinc.nametype, (unsigned long long)gprinc.principal); |
0a7de745 A |
7558 | if (gprinc.princlen > MAXPATHLEN) { |
7559 | return EINVAL; | |
7560 | } | |
3e170ce0 | 7561 | uint8_t *p; |
0a7de745 A |
7562 | MALLOC(p, uint8_t *, gprinc.princlen + 1, M_TEMP, M_WAITOK | M_ZERO); |
7563 | if (p == NULL) { | |
7564 | return ENOMEM; | |
7565 | } | |
3e170ce0 A |
7566 | error = copyin(gprinc.principal, p, gprinc.princlen); |
7567 | if (error) { | |
7568 | NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n", | |
0a7de745 | 7569 | gprinc.princlen, error); |
3e170ce0 | 7570 | FREE(p, M_TEMP); |
0a7de745 | 7571 | return error; |
3e170ce0 A |
7572 | } |
7573 | NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s\n", p); | |
7574 | error = nfs_gss_clnt_ctx_set_principal(mp, ctx, p, gprinc.princlen, gprinc.nametype); | |
7575 | NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s returned %d\n", p, error); | |
7576 | FREE(p, M_TEMP); | |
7577 | break; | |
5ba3f43e A |
7578 | case NFS_IOC_GET_CRED: |
7579 | case NFS_IOC_GET_CRED64: | |
0a7de745 A |
7580 | if (!auth_is_kerberized(mp->nm_auth)) { |
7581 | return ENOTSUP; | |
7582 | } | |
5ba3f43e | 7583 | if ((ap->a_command == NFS_IOC_GET_CRED && vfs_context_is64bit(ctx)) || |
0a7de745 A |
7584 | (ap->a_command == NFS_IOC_GET_CRED64 && !vfs_context_is64bit(ctx))) { |
7585 | return EINVAL; | |
7586 | } | |
3e170ce0 | 7587 | error = nfs_gss_clnt_ctx_get_principal(mp, ctx, &gprinc); |
0a7de745 | 7588 | if (error) { |
3e170ce0 | 7589 | break; |
0a7de745 | 7590 | } |
3e170ce0 A |
7591 | if (vfs_context_is64bit(ctx)) { |
7592 | struct user_nfs_gss_principal *upp = (struct user_nfs_gss_principal *)ap->a_data; | |
7593 | len = upp->princlen; | |
0a7de745 | 7594 | if (gprinc.princlen < len) { |
3e170ce0 | 7595 | len = gprinc.princlen; |
0a7de745 | 7596 | } |
3e170ce0 A |
7597 | upp->princlen = gprinc.princlen; |
7598 | upp->nametype = gprinc.nametype; | |
7599 | upp->flags = gprinc.flags; | |
0a7de745 | 7600 | if (gprinc.principal) { |
3e170ce0 | 7601 | error = copyout((void *)gprinc.principal, upp->principal, len); |
0a7de745 | 7602 | } else { |
3e170ce0 | 7603 | upp->principal = USER_ADDR_NULL; |
0a7de745 | 7604 | } |
3e170ce0 A |
7605 | } else { |
7606 | struct nfs_gss_principal *u32pp = (struct nfs_gss_principal *)ap->a_data; | |
7607 | len = u32pp->princlen; | |
0a7de745 | 7608 | if (gprinc.princlen < len) { |
3e170ce0 | 7609 | len = gprinc.princlen; |
0a7de745 | 7610 | } |
3e170ce0 A |
7611 | u32pp->princlen = gprinc.princlen; |
7612 | u32pp->nametype = gprinc.nametype; | |
7613 | u32pp->flags = gprinc.flags; | |
0a7de745 | 7614 | if (gprinc.principal) { |
3e170ce0 | 7615 | error = copyout((void *)gprinc.principal, u32pp->principal, len); |
0a7de745 | 7616 | } else { |
3e170ce0 | 7617 | u32pp->principal = (user32_addr_t)0; |
0a7de745 | 7618 | } |
3e170ce0 A |
7619 | } |
7620 | if (error) { | |
7621 | NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n", | |
0a7de745 | 7622 | gprinc.princlen, error); |
3e170ce0 | 7623 | } |
0a7de745 | 7624 | if (gprinc.principal) { |
39037602 | 7625 | FREE(gprinc.principal, M_TEMP); |
0a7de745 | 7626 | } |
cb323159 | 7627 | #endif /* CONFIG_NFS_GSS */ |
6d2010ae A |
7628 | } |
7629 | ||
0a7de745 | 7630 | return error; |
1c79356b A |
7631 | } |
7632 | ||
91447636 | 7633 | /*ARGSUSED*/ |
b0d623f7 | 7634 | int |
2d21ac55 | 7635 | nfs_vnop_select( |
91447636 | 7636 | __unused struct vnop_select_args /* { |
0a7de745 A |
7637 | * struct vnodeop_desc *a_desc; |
7638 | * vnode_t a_vp; | |
7639 | * int a_which; | |
7640 | * int a_fflags; | |
7641 | * void *a_wql; | |
7642 | * vfs_context_t a_context; | |
7643 | * } */*ap) | |
1c79356b | 7644 | { |
1c79356b A |
7645 | /* |
7646 | * We were once bogusly seltrue() which returns 1. Is this right? | |
7647 | */ | |
0a7de745 | 7648 | return 1; |
1c79356b A |
7649 | } |
7650 | ||
1c79356b | 7651 | /* |
2d21ac55 A |
7652 | * vnode OP for pagein using UPL |
7653 | * | |
7654 | * No buffer I/O, just RPCs straight into the mapped pages. | |
1c79356b | 7655 | */ |
b0d623f7 | 7656 | int |
2d21ac55 | 7657 | nfs_vnop_pagein( |
91447636 | 7658 | struct vnop_pagein_args /* { |
0a7de745 A |
7659 | * struct vnodeop_desc *a_desc; |
7660 | * vnode_t a_vp; | |
7661 | * upl_t a_pl; | |
7662 | * vm_offset_t a_pl_offset; | |
7663 | * off_t a_f_offset; | |
7664 | * size_t a_size; | |
7665 | * int a_flags; | |
7666 | * vfs_context_t a_context; | |
7667 | * } */*ap) | |
1c79356b | 7668 | { |
91447636 | 7669 | vnode_t vp = ap->a_vp; |
1c79356b | 7670 | upl_t pl = ap->a_pl; |
2d21ac55 | 7671 | size_t size = ap->a_size; |
1c79356b A |
7672 | off_t f_offset = ap->a_f_offset; |
7673 | vm_offset_t pl_offset = ap->a_pl_offset; | |
2d21ac55 A |
7674 | int flags = ap->a_flags; |
7675 | thread_t thd; | |
91447636 | 7676 | kauth_cred_t cred; |
2d21ac55 A |
7677 | nfsnode_t np = VTONFS(vp); |
7678 | size_t nmrsize, iosize, txsize, rxsize, retsize; | |
7679 | off_t txoffset; | |
55e303ae | 7680 | struct nfsmount *nmp; |
1c79356b | 7681 | int error = 0; |
b0d623f7 A |
7682 | vm_offset_t ioaddr, rxaddr; |
7683 | uio_t uio; | |
0a7de745 | 7684 | char uio_buf[UIO_SIZEOF(1)]; |
fa4905b1 | 7685 | int nofreeupl = flags & UPL_NOCOMMIT; |
55e303ae | 7686 | upl_page_info_t *plinfo; |
0a7de745 | 7687 | #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */ |
2d21ac55 A |
7688 | struct nfsreq *req[MAXPAGINGREQS]; |
7689 | int nextsend, nextwait; | |
cb323159 A |
7690 | #if CONFIG_NFS4 |
7691 | uint32_t stategenid = 0; | |
7692 | #endif | |
7693 | uint32_t restart = 0; | |
b0d623f7 | 7694 | kern_return_t kret; |
1c79356b | 7695 | |
2d21ac55 | 7696 | FSDBG(322, np, f_offset, size, flags); |
0a7de745 | 7697 | if (pl == (upl_t)NULL) { |
fa4905b1 | 7698 | panic("nfs_pagein: no upl"); |
0a7de745 | 7699 | } |
1c79356b | 7700 | |
fa4905b1 | 7701 | if (size <= 0) { |
2d21ac55 | 7702 | printf("nfs_pagein: invalid size %ld", size); |
0a7de745 | 7703 | if (!nofreeupl) { |
316670eb | 7704 | (void) ubc_upl_abort_range(pl, pl_offset, size, 0); |
0a7de745 A |
7705 | } |
7706 | return EINVAL; | |
fa4905b1 | 7707 | } |
91447636 | 7708 | if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) { |
0a7de745 | 7709 | if (!nofreeupl) { |
2d21ac55 | 7710 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
7711 | UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); |
7712 | } | |
7713 | return EINVAL; | |
1c79356b | 7714 | } |
91447636 | 7715 | |
2d21ac55 | 7716 | thd = vfs_context_thread(ap->a_context); |
fa4905b1 | 7717 | cred = ubc_getcred(vp); |
0a7de745 | 7718 | if (!IS_VALID_CRED(cred)) { |
91447636 | 7719 | cred = vfs_context_ucred(ap->a_context); |
0a7de745 | 7720 | } |
1c79356b | 7721 | |
b0d623f7 | 7722 | uio = uio_createwithbuffer(1, f_offset, UIO_SYSSPACE, UIO_READ, |
0a7de745 | 7723 | &uio_buf, sizeof(uio_buf)); |
1c79356b | 7724 | |
2d21ac55 | 7725 | nmp = VTONMP(vp); |
fe8ab488 | 7726 | if (nfs_mount_gone(nmp)) { |
0a7de745 | 7727 | if (!nofreeupl) { |
2d21ac55 | 7728 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
7729 | UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); |
7730 | } | |
7731 | return ENXIO; | |
55e303ae | 7732 | } |
2d21ac55 | 7733 | nmrsize = nmp->nm_rsize; |
1c79356b | 7734 | |
55e303ae | 7735 | plinfo = ubc_upl_pageinfo(pl); |
b0d623f7 | 7736 | kret = ubc_upl_map(pl, &ioaddr); |
0a7de745 | 7737 | if (kret != KERN_SUCCESS) { |
b0d623f7 | 7738 | panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret); |
0a7de745 | 7739 | } |
1c79356b | 7740 | ioaddr += pl_offset; |
b0d623f7 A |
7741 | |
7742 | tryagain: | |
cb323159 | 7743 | #if CONFIG_NFS4 |
0a7de745 | 7744 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 7745 | stategenid = nmp->nm_stategenid; |
0a7de745 | 7746 | } |
cb323159 | 7747 | #endif |
2d21ac55 A |
7748 | txsize = rxsize = size; |
7749 | txoffset = f_offset; | |
b0d623f7 | 7750 | rxaddr = ioaddr; |
1c79356b | 7751 | |
2d21ac55 A |
7752 | bzero(req, sizeof(req)); |
7753 | nextsend = nextwait = 0; | |
1c79356b | 7754 | do { |
6d2010ae A |
7755 | if (np->n_flag & NREVOKE) { |
7756 | error = EIO; | |
7757 | break; | |
7758 | } | |
2d21ac55 A |
7759 | /* send requests while we need to and have available slots */ |
7760 | while ((txsize > 0) && (req[nextsend] == NULL)) { | |
7761 | iosize = MIN(nmrsize, txsize); | |
7762 | if ((error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, iosize, thd, cred, NULL, &req[nextsend]))) { | |
7763 | req[nextsend] = NULL; | |
7764 | break; | |
7765 | } | |
7766 | txoffset += iosize; | |
7767 | txsize -= iosize; | |
7768 | nextsend = (nextsend + 1) % MAXPAGINGREQS; | |
7769 | } | |
7770 | /* wait while we need to and break out if more requests to send */ | |
7771 | while ((rxsize > 0) && req[nextwait]) { | |
7772 | iosize = retsize = MIN(nmrsize, rxsize); | |
b0d623f7 A |
7773 | uio_reset(uio, uio_offset(uio), UIO_SYSSPACE, UIO_READ); |
7774 | uio_addiov(uio, CAST_USER_ADDR_T(rxaddr), iosize); | |
7775 | FSDBG(322, uio_offset(uio), uio_resid(uio), rxaddr, rxsize); | |
7776 | #if UPL_DEBUG | |
7777 | upl_ubc_alias_set(pl, (uintptr_t) current_thread(), (uintptr_t) 2); | |
91447636 | 7778 | #endif /* UPL_DEBUG */ |
316670eb | 7779 | OSAddAtomic64(1, &nfsstats.pageins); |
2d21ac55 A |
7780 | error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req[nextwait], uio, &retsize, NULL); |
7781 | req[nextwait] = NULL; | |
7782 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
cb323159 | 7783 | #if CONFIG_NFS4 |
b0d623f7 A |
7784 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
7785 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
7786 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
7787 | NP(np, "nfs_vnop_pagein: error %d, initiating recovery", error); | |
7788 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
7789 | } |
7790 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
7791 | restart++; |
7792 | goto cancel; | |
7793 | } | |
cb323159 | 7794 | #endif |
2d21ac55 | 7795 | if (error) { |
b0d623f7 | 7796 | FSDBG(322, uio_offset(uio), uio_resid(uio), error, -1); |
2d21ac55 A |
7797 | break; |
7798 | } | |
7799 | if (retsize < iosize) { | |
7800 | /* Just zero fill the rest of the valid area. */ | |
2d21ac55 | 7801 | int zcnt = iosize - retsize; |
b0d623f7 A |
7802 | bzero((char *)rxaddr + retsize, zcnt); |
7803 | FSDBG(324, uio_offset(uio), retsize, zcnt, rxaddr); | |
7804 | uio_update(uio, zcnt); | |
1c79356b | 7805 | } |
0a7de745 | 7806 | rxaddr += iosize; |
2d21ac55 | 7807 | rxsize -= iosize; |
0a7de745 | 7808 | if (txsize) { |
2d21ac55 | 7809 | break; |
0a7de745 | 7810 | } |
1c79356b | 7811 | } |
2d21ac55 | 7812 | } while (!error && (txsize || rxsize)); |
1c79356b | 7813 | |
b0d623f7 | 7814 | restart = 0; |
1c79356b | 7815 | |
2d21ac55 | 7816 | if (error) { |
cb323159 | 7817 | #if CONFIG_NFS4 |
b0d623f7 | 7818 | cancel: |
cb323159 | 7819 | #endif |
2d21ac55 A |
7820 | /* cancel any outstanding requests */ |
7821 | while (req[nextwait]) { | |
7822 | nfs_request_async_cancel(req[nextwait]); | |
7823 | req[nextwait] = NULL; | |
7824 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
7825 | } | |
6d2010ae A |
7826 | if (np->n_flag & NREVOKE) { |
7827 | error = EIO; | |
7828 | } else if (restart) { | |
7829 | if (restart <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */ | |
0a7de745 A |
7830 | if (error == NFSERR_GRACE) { |
7831 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
7832 | } | |
7833 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 7834 | goto tryagain; |
0a7de745 | 7835 | } |
6d2010ae A |
7836 | } else { |
7837 | NP(np, "nfs_pagein: too many restarts, aborting"); | |
7838 | } | |
b0d623f7 | 7839 | } |
2d21ac55 A |
7840 | } |
7841 | ||
b0d623f7 A |
7842 | ubc_upl_unmap(pl); |
7843 | ||
fa4905b1 | 7844 | if (!nofreeupl) { |
0a7de745 | 7845 | if (error) { |
2d21ac55 | 7846 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
7847 | UPL_ABORT_ERROR | |
7848 | UPL_ABORT_FREE_ON_EMPTY); | |
7849 | } else { | |
0b4e3aa0 | 7850 | ubc_upl_commit_range(pl, pl_offset, size, |
0a7de745 A |
7851 | UPL_COMMIT_CLEAR_DIRTY | |
7852 | UPL_COMMIT_FREE_ON_EMPTY); | |
7853 | } | |
1c79356b | 7854 | } |
0a7de745 | 7855 | return error; |
1c79356b A |
7856 | } |
7857 | ||
0b4e3aa0 | 7858 | |
1c79356b | 7859 | /* |
2d21ac55 A |
7860 | * the following are needed only by nfs_pageout to know how to handle errors |
7861 | * see nfs_pageout comments on explanation of actions. | |
7862 | * the errors here are copied from errno.h and errors returned by servers | |
7863 | * are expected to match the same numbers here. If not, our actions maybe | |
7864 | * erroneous. | |
7865 | */ | |
b0d623f7 | 7866 | char nfs_pageouterrorhandler(int); |
39236c6e | 7867 | enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, SEVER}; |
2d21ac55 | 7868 | #define NFS_ELAST 88 |
0a7de745 A |
7869 | static u_char errorcount[NFS_ELAST + 1]; /* better be zeros when initialized */ |
7870 | static const char errortooutcome[NFS_ELAST + 1] = { | |
2d21ac55 | 7871 | NOACTION, |
0a7de745 A |
7872 | DUMP, /* EPERM 1 Operation not permitted */ |
7873 | DUMP, /* ENOENT 2 No such file or directory */ | |
7874 | DUMPANDLOG, /* ESRCH 3 No such process */ | |
7875 | RETRY, /* EINTR 4 Interrupted system call */ | |
7876 | DUMP, /* EIO 5 Input/output error */ | |
7877 | DUMP, /* ENXIO 6 Device not configured */ | |
7878 | DUMPANDLOG, /* E2BIG 7 Argument list too long */ | |
7879 | DUMPANDLOG, /* ENOEXEC 8 Exec format error */ | |
7880 | DUMPANDLOG, /* EBADF 9 Bad file descriptor */ | |
7881 | DUMPANDLOG, /* ECHILD 10 No child processes */ | |
7882 | DUMPANDLOG, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */ | |
7883 | RETRY, /* ENOMEM 12 Cannot allocate memory */ | |
7884 | DUMP, /* EACCES 13 Permission denied */ | |
7885 | DUMPANDLOG, /* EFAULT 14 Bad address */ | |
7886 | DUMPANDLOG, /* ENOTBLK 15 POSIX - Block device required */ | |
7887 | RETRY, /* EBUSY 16 Device busy */ | |
7888 | DUMP, /* EEXIST 17 File exists */ | |
7889 | DUMP, /* EXDEV 18 Cross-device link */ | |
7890 | DUMP, /* ENODEV 19 Operation not supported by device */ | |
7891 | DUMP, /* ENOTDIR 20 Not a directory */ | |
7892 | DUMP, /* EISDIR 21 Is a directory */ | |
7893 | DUMP, /* EINVAL 22 Invalid argument */ | |
7894 | DUMPANDLOG, /* ENFILE 23 Too many open files in system */ | |
7895 | DUMPANDLOG, /* EMFILE 24 Too many open files */ | |
7896 | DUMPANDLOG, /* ENOTTY 25 Inappropriate ioctl for device */ | |
7897 | DUMPANDLOG, /* ETXTBSY 26 Text file busy - POSIX */ | |
7898 | DUMP, /* EFBIG 27 File too large */ | |
7899 | DUMP, /* ENOSPC 28 No space left on device */ | |
7900 | DUMPANDLOG, /* ESPIPE 29 Illegal seek */ | |
7901 | DUMP, /* EROFS 30 Read-only file system */ | |
7902 | DUMP, /* EMLINK 31 Too many links */ | |
7903 | RETRY, /* EPIPE 32 Broken pipe */ | |
2d21ac55 | 7904 | /* math software */ |
0a7de745 A |
7905 | DUMPANDLOG, /* EDOM 33 Numerical argument out of domain */ |
7906 | DUMPANDLOG, /* ERANGE 34 Result too large */ | |
7907 | RETRY, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */ | |
7908 | DUMPANDLOG, /* EINPROGRESS 36 Operation now in progress */ | |
7909 | DUMPANDLOG, /* EALREADY 37 Operation already in progress */ | |
2d21ac55 | 7910 | /* ipc/network software -- argument errors */ |
0a7de745 A |
7911 | DUMPANDLOG, /* ENOTSOC 38 Socket operation on non-socket */ |
7912 | DUMPANDLOG, /* EDESTADDRREQ 39 Destination address required */ | |
7913 | DUMPANDLOG, /* EMSGSIZE 40 Message too long */ | |
7914 | DUMPANDLOG, /* EPROTOTYPE 41 Protocol wrong type for socket */ | |
7915 | DUMPANDLOG, /* ENOPROTOOPT 42 Protocol not available */ | |
7916 | DUMPANDLOG, /* EPROTONOSUPPORT 43 Protocol not supported */ | |
7917 | DUMPANDLOG, /* ESOCKTNOSUPPORT 44 Socket type not supported */ | |
7918 | DUMPANDLOG, /* ENOTSUP 45 Operation not supported */ | |
7919 | DUMPANDLOG, /* EPFNOSUPPORT 46 Protocol family not supported */ | |
7920 | DUMPANDLOG, /* EAFNOSUPPORT 47 Address family not supported by protocol family */ | |
7921 | DUMPANDLOG, /* EADDRINUSE 48 Address already in use */ | |
7922 | DUMPANDLOG, /* EADDRNOTAVAIL 49 Can't assign requested address */ | |
2d21ac55 | 7923 | /* ipc/network software -- operational errors */ |
0a7de745 A |
7924 | RETRY, /* ENETDOWN 50 Network is down */ |
7925 | RETRY, /* ENETUNREACH 51 Network is unreachable */ | |
7926 | RETRY, /* ENETRESET 52 Network dropped connection on reset */ | |
7927 | RETRY, /* ECONNABORTED 53 Software caused connection abort */ | |
7928 | RETRY, /* ECONNRESET 54 Connection reset by peer */ | |
7929 | RETRY, /* ENOBUFS 55 No buffer space available */ | |
7930 | RETRY, /* EISCONN 56 Socket is already connected */ | |
7931 | RETRY, /* ENOTCONN 57 Socket is not connected */ | |
7932 | RETRY, /* ESHUTDOWN 58 Can't send after socket shutdown */ | |
7933 | RETRY, /* ETOOMANYREFS 59 Too many references: can't splice */ | |
7934 | RETRY, /* ETIMEDOUT 60 Operation timed out */ | |
7935 | RETRY, /* ECONNREFUSED 61 Connection refused */ | |
7936 | ||
7937 | DUMPANDLOG, /* ELOOP 62 Too many levels of symbolic links */ | |
7938 | DUMP, /* ENAMETOOLONG 63 File name too long */ | |
7939 | RETRY, /* EHOSTDOWN 64 Host is down */ | |
7940 | RETRY, /* EHOSTUNREACH 65 No route to host */ | |
7941 | DUMP, /* ENOTEMPTY 66 Directory not empty */ | |
2d21ac55 | 7942 | /* quotas & mush */ |
0a7de745 A |
7943 | DUMPANDLOG, /* PROCLIM 67 Too many processes */ |
7944 | DUMPANDLOG, /* EUSERS 68 Too many users */ | |
7945 | DUMPANDLOG, /* EDQUOT 69 Disc quota exceeded */ | |
2d21ac55 | 7946 | /* Network File System */ |
0a7de745 A |
7947 | DUMP, /* ESTALE 70 Stale NFS file handle */ |
7948 | DUMP, /* EREMOTE 71 Too many levels of remote in path */ | |
7949 | DUMPANDLOG, /* EBADRPC 72 RPC struct is bad */ | |
7950 | DUMPANDLOG, /* ERPCMISMATCH 73 RPC version wrong */ | |
7951 | DUMPANDLOG, /* EPROGUNAVAIL 74 RPC prog. not avail */ | |
7952 | DUMPANDLOG, /* EPROGMISMATCH 75 Program version wrong */ | |
7953 | DUMPANDLOG, /* EPROCUNAVAIL 76 Bad procedure for program */ | |
7954 | ||
7955 | DUMPANDLOG, /* ENOLCK 77 No locks available */ | |
7956 | DUMPANDLOG, /* ENOSYS 78 Function not implemented */ | |
7957 | DUMPANDLOG, /* EFTYPE 79 Inappropriate file type or format */ | |
7958 | DUMPANDLOG, /* EAUTH 80 Authentication error */ | |
7959 | DUMPANDLOG, /* ENEEDAUTH 81 Need authenticator */ | |
2d21ac55 | 7960 | /* Intelligent device errors */ |
0a7de745 A |
7961 | DUMPANDLOG, /* EPWROFF 82 Device power is off */ |
7962 | DUMPANDLOG, /* EDEVERR 83 Device error, e.g. paper out */ | |
7963 | DUMPANDLOG, /* EOVERFLOW 84 Value too large to be stored in data type */ | |
2d21ac55 | 7964 | /* Program loading errors */ |
0a7de745 A |
7965 | DUMPANDLOG, /* EBADEXEC 85 Bad executable */ |
7966 | DUMPANDLOG, /* EBADARCH 86 Bad CPU type in executable */ | |
7967 | DUMPANDLOG, /* ESHLIBVERS 87 Shared library version mismatch */ | |
7968 | DUMPANDLOG, /* EBADMACHO 88 Malformed Macho file */ | |
2d21ac55 A |
7969 | }; |
7970 | ||
b0d623f7 | 7971 | char |
2d21ac55 A |
7972 | nfs_pageouterrorhandler(int error) |
7973 | { | |
0a7de745 A |
7974 | if (error > NFS_ELAST) { |
7975 | return DUMP; | |
7976 | } else { | |
7977 | return errortooutcome[error]; | |
7978 | } | |
2d21ac55 A |
7979 | } |
7980 | ||
7981 | ||
7982 | /* | |
7983 | * vnode OP for pageout using UPL | |
7984 | * | |
7985 | * No buffer I/O, just RPCs straight from the mapped pages. | |
1c79356b A |
7986 | * File size changes are not permitted in pageout. |
7987 | */ | |
b0d623f7 | 7988 | int |
2d21ac55 | 7989 | nfs_vnop_pageout( |
91447636 | 7990 | struct vnop_pageout_args /* { |
0a7de745 A |
7991 | * struct vnodeop_desc *a_desc; |
7992 | * vnode_t a_vp; | |
7993 | * upl_t a_pl; | |
7994 | * vm_offset_t a_pl_offset; | |
7995 | * off_t a_f_offset; | |
7996 | * size_t a_size; | |
7997 | * int a_flags; | |
7998 | * vfs_context_t a_context; | |
7999 | * } */*ap) | |
1c79356b | 8000 | { |
91447636 | 8001 | vnode_t vp = ap->a_vp; |
1c79356b | 8002 | upl_t pl = ap->a_pl; |
2d21ac55 | 8003 | size_t size = ap->a_size; |
1c79356b A |
8004 | off_t f_offset = ap->a_f_offset; |
8005 | vm_offset_t pl_offset = ap->a_pl_offset; | |
2d21ac55 A |
8006 | int flags = ap->a_flags; |
8007 | nfsnode_t np = VTONFS(vp); | |
8008 | thread_t thd; | |
91447636 | 8009 | kauth_cred_t cred; |
55e303ae | 8010 | struct nfsbuf *bp; |
2d21ac55 | 8011 | struct nfsmount *nmp = VTONMP(vp); |
91447636 | 8012 | daddr64_t lbn; |
8f6c56a5 | 8013 | int error = 0, iomode; |
2d21ac55 A |
8014 | off_t off, txoffset, rxoffset; |
8015 | vm_offset_t ioaddr, txaddr, rxaddr; | |
b0d623f7 | 8016 | uio_t auio; |
0a7de745 | 8017 | char uio_buf[UIO_SIZEOF(1)]; |
fa4905b1 | 8018 | int nofreeupl = flags & UPL_NOCOMMIT; |
2d21ac55 A |
8019 | size_t nmwsize, biosize, iosize, pgsize, txsize, rxsize, xsize, remsize; |
8020 | struct nfsreq *req[MAXPAGINGREQS]; | |
b0d623f7 | 8021 | int nextsend, nextwait, wverfset, commit; |
2d21ac55 | 8022 | uint64_t wverf, wverf2; |
cb323159 A |
8023 | #if CONFIG_NFS4 |
8024 | uint32_t stategenid = 0; | |
8025 | #endif | |
8026 | uint32_t vrestart = 0, restart = 0, vrestarts = 0, restarts = 0; | |
b0d623f7 | 8027 | kern_return_t kret; |
1c79356b | 8028 | |
fa4905b1 A |
8029 | FSDBG(323, f_offset, size, pl, pl_offset); |
8030 | ||
0a7de745 | 8031 | if (pl == (upl_t)NULL) { |
fa4905b1 | 8032 | panic("nfs_pageout: no upl"); |
0a7de745 | 8033 | } |
1c79356b | 8034 | |
fa4905b1 | 8035 | if (size <= 0) { |
2d21ac55 | 8036 | printf("nfs_pageout: invalid size %ld", size); |
0a7de745 | 8037 | if (!nofreeupl) { |
316670eb | 8038 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
0a7de745 A |
8039 | } |
8040 | return EINVAL; | |
1c79356b A |
8041 | } |
8042 | ||
55e303ae | 8043 | if (!nmp) { |
0a7de745 A |
8044 | if (!nofreeupl) { |
8045 | ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY); | |
8046 | } | |
8047 | return ENXIO; | |
55e303ae | 8048 | } |
0c530ab8 | 8049 | biosize = nmp->nm_biosize; |
2d21ac55 A |
8050 | nmwsize = nmp->nm_wsize; |
8051 | ||
b0d623f7 | 8052 | nfs_data_lock_noupdate(np, NFS_DATA_LOCK_SHARED); |
1c79356b | 8053 | |
1c79356b | 8054 | /* |
55e303ae A |
8055 | * Check to see whether the buffer is incore. |
8056 | * If incore and not busy, invalidate it from the cache. | |
1c79356b | 8057 | */ |
55e303ae A |
8058 | for (iosize = 0; iosize < size; iosize += xsize) { |
8059 | off = f_offset + iosize; | |
8060 | /* need make sure we do things on block boundaries */ | |
8061 | xsize = biosize - (off % biosize); | |
0a7de745 | 8062 | if (off + xsize > f_offset + size) { |
55e303ae | 8063 | xsize = f_offset + size - off; |
0a7de745 | 8064 | } |
2d21ac55 | 8065 | lbn = (daddr64_t)(off / biosize); |
91447636 | 8066 | lck_mtx_lock(nfs_buf_mutex); |
2d21ac55 | 8067 | if ((bp = nfs_buf_incore(np, lbn))) { |
91447636 A |
8068 | FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags); |
8069 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
8070 | lck_mtx_unlock(nfs_buf_mutex); | |
b0d623f7 | 8071 | nfs_data_unlock_noupdate(np); |
fa4905b1 | 8072 | /* no panic. just tell vm we are busy */ |
0a7de745 | 8073 | if (!nofreeupl) { |
316670eb | 8074 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
0a7de745 A |
8075 | } |
8076 | return EBUSY; | |
55e303ae A |
8077 | } |
8078 | if (bp->nb_dirtyend > 0) { | |
8079 | /* | |
91447636 A |
8080 | * if there's a dirty range in the buffer, check |
8081 | * to see if it extends beyond the pageout region | |
55e303ae A |
8082 | * |
8083 | * if the dirty region lies completely within the | |
8084 | * pageout region, we just invalidate the buffer | |
8085 | * because it's all being written out now anyway. | |
8086 | * | |
8087 | * if any of the dirty region lies outside the | |
8088 | * pageout region, we'll try to clip the dirty | |
8089 | * region to eliminate the portion that's being | |
8090 | * paged out. If that's not possible, because | |
8091 | * the dirty region extends before and after the | |
8092 | * pageout region, then we'll just return EBUSY. | |
8093 | */ | |
8094 | off_t boff, start, end; | |
8095 | boff = NBOFF(bp); | |
8096 | start = off; | |
8097 | end = off + xsize; | |
8098 | /* clip end to EOF */ | |
0a7de745 | 8099 | if (end > (off_t)np->n_size) { |
55e303ae | 8100 | end = np->n_size; |
0a7de745 | 8101 | } |
55e303ae A |
8102 | start -= boff; |
8103 | end -= boff; | |
8104 | if ((bp->nb_dirtyoff < start) && | |
8105 | (bp->nb_dirtyend > end)) { | |
0a7de745 A |
8106 | /* |
8107 | * not gonna be able to clip the dirty region | |
8108 | * | |
8109 | * But before returning the bad news, move the | |
8110 | * buffer to the start of the delwri list and | |
8111 | * give the list a push to try to flush the | |
8112 | * buffer out. | |
8113 | */ | |
8114 | FSDBG(323, np, bp, 0xd00deebc, EBUSY); | |
8115 | nfs_buf_remfree(bp); | |
8116 | TAILQ_INSERT_HEAD(&nfsbufdelwri, bp, nb_free); | |
8117 | nfsbufdelwricnt++; | |
8118 | nfs_buf_drop(bp); | |
8119 | nfs_buf_delwri_push(1); | |
8120 | lck_mtx_unlock(nfs_buf_mutex); | |
8121 | nfs_data_unlock_noupdate(np); | |
8122 | if (!nofreeupl) { | |
8123 | ubc_upl_abort_range(pl, pl_offset, size, 0); | |
8124 | } | |
8125 | return EBUSY; | |
55e303ae A |
8126 | } |
8127 | if ((bp->nb_dirtyoff < start) || | |
8128 | (bp->nb_dirtyend > end)) { | |
0a7de745 A |
8129 | /* clip dirty region, if necessary */ |
8130 | if (bp->nb_dirtyoff < start) { | |
8131 | bp->nb_dirtyend = min(bp->nb_dirtyend, start); | |
8132 | } | |
8133 | if (bp->nb_dirtyend > end) { | |
8134 | bp->nb_dirtyoff = max(bp->nb_dirtyoff, end); | |
8135 | } | |
8136 | FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00); | |
8137 | /* we're leaving this block dirty */ | |
8138 | nfs_buf_drop(bp); | |
8139 | lck_mtx_unlock(nfs_buf_mutex); | |
8140 | continue; | |
55e303ae A |
8141 | } |
8142 | } | |
8143 | nfs_buf_remfree(bp); | |
91447636 A |
8144 | lck_mtx_unlock(nfs_buf_mutex); |
8145 | SET(bp->nb_flags, NB_INVAL); | |
b0d623f7 | 8146 | nfs_node_lock_force(np); |
55e303ae A |
8147 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { |
8148 | CLR(bp->nb_flags, NB_NEEDCOMMIT); | |
8149 | np->n_needcommitcnt--; | |
8150 | CHECK_NEEDCOMMITCNT(np); | |
fa4905b1 | 8151 | } |
b0d623f7 | 8152 | nfs_node_unlock(np); |
483a1d10 | 8153 | nfs_buf_release(bp, 1); |
91447636 A |
8154 | } else { |
8155 | lck_mtx_unlock(nfs_buf_mutex); | |
1c79356b | 8156 | } |
1c79356b A |
8157 | } |
8158 | ||
2d21ac55 | 8159 | thd = vfs_context_thread(ap->a_context); |
1c79356b | 8160 | cred = ubc_getcred(vp); |
0a7de745 | 8161 | if (!IS_VALID_CRED(cred)) { |
91447636 | 8162 | cred = vfs_context_ucred(ap->a_context); |
0a7de745 | 8163 | } |
1c79356b | 8164 | |
b0d623f7 | 8165 | nfs_node_lock_force(np); |
1c79356b | 8166 | if (np->n_flag & NWRITEERR) { |
2d21ac55 | 8167 | error = np->n_error; |
b0d623f7 A |
8168 | nfs_node_unlock(np); |
8169 | nfs_data_unlock_noupdate(np); | |
0a7de745 | 8170 | if (!nofreeupl) { |
fa4905b1 | 8171 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
8172 | UPL_ABORT_FREE_ON_EMPTY); |
8173 | } | |
8174 | return error; | |
1c79356b | 8175 | } |
b0d623f7 | 8176 | nfs_node_unlock(np); |
1c79356b | 8177 | |
91447636 | 8178 | if (f_offset < 0 || f_offset >= (off_t)np->n_size || |
55e303ae | 8179 | f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) { |
b0d623f7 | 8180 | nfs_data_unlock_noupdate(np); |
0a7de745 | 8181 | if (!nofreeupl) { |
fa4905b1 | 8182 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
8183 | UPL_ABORT_FREE_ON_EMPTY); |
8184 | } | |
8185 | return EINVAL; | |
1c79356b A |
8186 | } |
8187 | ||
b0d623f7 | 8188 | kret = ubc_upl_map(pl, &ioaddr); |
0a7de745 | 8189 | if (kret != KERN_SUCCESS) { |
b0d623f7 | 8190 | panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret); |
0a7de745 | 8191 | } |
55e303ae | 8192 | ioaddr += pl_offset; |
1c79356b | 8193 | |
0a7de745 | 8194 | if ((u_quad_t)f_offset + size > np->n_size) { |
55e303ae | 8195 | xsize = np->n_size - f_offset; |
0a7de745 | 8196 | } else { |
55e303ae | 8197 | xsize = size; |
0a7de745 | 8198 | } |
1c79356b | 8199 | |
55e303ae | 8200 | pgsize = round_page_64(xsize); |
0a7de745 | 8201 | if ((size > pgsize) && !nofreeupl) { |
2d21ac55 | 8202 | ubc_upl_abort_range(pl, pl_offset + pgsize, size - pgsize, |
0a7de745 A |
8203 | UPL_ABORT_FREE_ON_EMPTY); |
8204 | } | |
1c79356b | 8205 | |
2d21ac55 | 8206 | /* |
1c79356b A |
8207 | * check for partial page and clear the |
8208 | * contents past end of the file before | |
8209 | * releasing it in the VM page cache | |
8210 | */ | |
91447636 | 8211 | if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) { |
1c79356b | 8212 | size_t io = np->n_size - f_offset; |
55e303ae | 8213 | bzero((caddr_t)(ioaddr + io), size - io); |
fa4905b1 | 8214 | FSDBG(321, np->n_size, f_offset, f_offset + io, size - io); |
1c79356b | 8215 | } |
b0d623f7 | 8216 | nfs_data_unlock_noupdate(np); |
1c79356b | 8217 | |
b0d623f7 | 8218 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE, |
0a7de745 | 8219 | &uio_buf, sizeof(uio_buf)); |
55e303ae | 8220 | |
2d21ac55 | 8221 | tryagain: |
cb323159 | 8222 | #if CONFIG_NFS4 |
0a7de745 | 8223 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 8224 | stategenid = nmp->nm_stategenid; |
0a7de745 | 8225 | } |
cb323159 | 8226 | #endif |
2d21ac55 A |
8227 | wverf = wverf2 = wverfset = 0; |
8228 | txsize = rxsize = xsize; | |
8229 | txoffset = rxoffset = f_offset; | |
8230 | txaddr = rxaddr = ioaddr; | |
8231 | commit = NFS_WRITE_FILESYNC; | |
8232 | ||
8233 | bzero(req, sizeof(req)); | |
8234 | nextsend = nextwait = 0; | |
1c79356b | 8235 | do { |
6d2010ae A |
8236 | if (np->n_flag & NREVOKE) { |
8237 | error = EIO; | |
8238 | break; | |
8239 | } | |
2d21ac55 A |
8240 | /* send requests while we need to and have available slots */ |
8241 | while ((txsize > 0) && (req[nextsend] == NULL)) { | |
8242 | iosize = MIN(nmwsize, txsize); | |
b0d623f7 A |
8243 | uio_reset(auio, txoffset, UIO_SYSSPACE, UIO_WRITE); |
8244 | uio_addiov(auio, CAST_USER_ADDR_T(txaddr), iosize); | |
8245 | FSDBG(323, uio_offset(auio), iosize, txaddr, txsize); | |
316670eb | 8246 | OSAddAtomic64(1, &nfsstats.pageouts); |
b0d623f7 A |
8247 | nfs_node_lock_force(np); |
8248 | np->n_numoutput++; | |
8249 | nfs_node_unlock(np); | |
2d21ac55 A |
8250 | vnode_startwrite(vp); |
8251 | iomode = NFS_WRITE_UNSTABLE; | |
b0d623f7 | 8252 | if ((error = nmp->nm_funcs->nf_write_rpc_async(np, auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) { |
2d21ac55 A |
8253 | req[nextsend] = NULL; |
8254 | vnode_writedone(vp); | |
b0d623f7 A |
8255 | nfs_node_lock_force(np); |
8256 | np->n_numoutput--; | |
8257 | nfs_node_unlock(np); | |
2d21ac55 A |
8258 | break; |
8259 | } | |
8260 | txaddr += iosize; | |
8261 | txoffset += iosize; | |
8262 | txsize -= iosize; | |
8263 | nextsend = (nextsend + 1) % MAXPAGINGREQS; | |
8264 | } | |
8265 | /* wait while we need to and break out if more requests to send */ | |
8266 | while ((rxsize > 0) && req[nextwait]) { | |
8267 | iosize = remsize = MIN(nmwsize, rxsize); | |
8268 | error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req[nextwait], &iomode, &iosize, &wverf2); | |
8269 | req[nextwait] = NULL; | |
8270 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
8271 | vnode_writedone(vp); | |
b0d623f7 A |
8272 | nfs_node_lock_force(np); |
8273 | np->n_numoutput--; | |
8274 | nfs_node_unlock(np); | |
cb323159 | 8275 | #if CONFIG_NFS4 |
b0d623f7 A |
8276 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
8277 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
8278 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
8279 | NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error); | |
8280 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
8281 | } |
8282 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
8283 | restart = 1; |
8284 | goto cancel; | |
8285 | } | |
cb323159 | 8286 | #endif |
2d21ac55 A |
8287 | if (error) { |
8288 | FSDBG(323, rxoffset, rxsize, error, -1); | |
8289 | break; | |
8290 | } | |
8291 | if (!wverfset) { | |
8292 | wverf = wverf2; | |
8293 | wverfset = 1; | |
8294 | } else if (wverf != wverf2) { | |
8295 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 8296 | vrestart = 1; |
2d21ac55 A |
8297 | goto cancel; |
8298 | } | |
8299 | /* Retain the lowest commitment level returned. */ | |
0a7de745 | 8300 | if (iomode < commit) { |
2d21ac55 | 8301 | commit = iomode; |
0a7de745 A |
8302 | } |
8303 | rxaddr += iosize; | |
8304 | rxoffset += iosize; | |
2d21ac55 A |
8305 | rxsize -= iosize; |
8306 | remsize -= iosize; | |
8307 | if (remsize > 0) { | |
8308 | /* need to try sending the remainder */ | |
8309 | iosize = remsize; | |
b0d623f7 A |
8310 | uio_reset(auio, rxoffset, UIO_SYSSPACE, UIO_WRITE); |
8311 | uio_addiov(auio, CAST_USER_ADDR_T(rxaddr), remsize); | |
2d21ac55 | 8312 | iomode = NFS_WRITE_UNSTABLE; |
b0d623f7 | 8313 | error = nfs_write_rpc2(np, auio, thd, cred, &iomode, &wverf2); |
cb323159 | 8314 | #if CONFIG_NFS4 |
b0d623f7 | 8315 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
6d2010ae | 8316 | NP(np, "nfs_vnop_pageout: restart: error %d", error); |
b0d623f7 | 8317 | lck_mtx_lock(&nmp->nm_lock); |
6d2010ae A |
8318 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
8319 | NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error); | |
8320 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
8321 | } |
8322 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
8323 | restart = 1; |
8324 | goto cancel; | |
8325 | } | |
cb323159 | 8326 | #endif |
2d21ac55 A |
8327 | if (error) { |
8328 | FSDBG(323, rxoffset, rxsize, error, -1); | |
8329 | break; | |
8330 | } | |
8331 | if (wverf != wverf2) { | |
8332 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 8333 | vrestart = 1; |
2d21ac55 A |
8334 | goto cancel; |
8335 | } | |
0a7de745 | 8336 | if (iomode < commit) { |
2d21ac55 | 8337 | commit = iomode; |
0a7de745 A |
8338 | } |
8339 | rxaddr += iosize; | |
8340 | rxoffset += iosize; | |
2d21ac55 A |
8341 | rxsize -= iosize; |
8342 | } | |
0a7de745 | 8343 | if (txsize) { |
2d21ac55 | 8344 | break; |
0a7de745 | 8345 | } |
2d21ac55 A |
8346 | } |
8347 | } while (!error && (txsize || rxsize)); | |
8348 | ||
b0d623f7 | 8349 | vrestart = 0; |
2d21ac55 A |
8350 | |
8351 | if (!error && (commit != NFS_WRITE_FILESYNC)) { | |
6d2010ae | 8352 | error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred, wverf); |
2d21ac55 | 8353 | if (error == NFSERR_STALEWRITEVERF) { |
b0d623f7 | 8354 | vrestart = 1; |
2d21ac55 A |
8355 | error = EIO; |
8356 | } | |
8357 | } | |
8358 | ||
8359 | if (error) { | |
8360 | cancel: | |
8361 | /* cancel any outstanding requests */ | |
8362 | while (req[nextwait]) { | |
8363 | nfs_request_async_cancel(req[nextwait]); | |
8364 | req[nextwait] = NULL; | |
8365 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
8366 | vnode_writedone(vp); | |
b0d623f7 A |
8367 | nfs_node_lock_force(np); |
8368 | np->n_numoutput--; | |
8369 | nfs_node_unlock(np); | |
8370 | } | |
6d2010ae A |
8371 | if (np->n_flag & NREVOKE) { |
8372 | error = EIO; | |
8373 | } else { | |
8374 | if (vrestart) { | |
0a7de745 | 8375 | if (++vrestarts <= 100) { /* guard against no progress */ |
6d2010ae | 8376 | goto tryagain; |
0a7de745 | 8377 | } |
6d2010ae A |
8378 | NP(np, "nfs_pageout: too many restarts, aborting"); |
8379 | FSDBG(323, f_offset, xsize, ERESTART, -1); | |
8380 | } | |
8381 | if (restart) { | |
8382 | if (restarts <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */ | |
0a7de745 A |
8383 | if (error == NFSERR_GRACE) { |
8384 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
8385 | } | |
8386 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 8387 | goto tryagain; |
0a7de745 | 8388 | } |
6d2010ae A |
8389 | } else { |
8390 | NP(np, "nfs_pageout: too many restarts, aborting"); | |
8391 | FSDBG(323, f_offset, xsize, ERESTART, -1); | |
8392 | } | |
8393 | } | |
2d21ac55 A |
8394 | } |
8395 | } | |
8396 | ||
0b4e3aa0 | 8397 | ubc_upl_unmap(pl); |
2d21ac55 | 8398 | |
0b4e3aa0 A |
8399 | /* |
8400 | * We've had several different solutions on what to do when the pageout | |
2d21ac55 A |
8401 | * gets an error. If we don't handle it, and return an error to the |
8402 | * caller, vm, it will retry . This can end in endless looping | |
0b4e3aa0 A |
8403 | * between vm and here doing retries of the same page. Doing a dump |
8404 | * back to vm, will get it out of vm's knowledge and we lose whatever | |
8405 | * data existed. This is risky, but in some cases necessary. For | |
8406 | * example, the initial fix here was to do that for ESTALE. In that case | |
2d21ac55 A |
8407 | * the server is telling us that the file is no longer the same. We |
8408 | * would not want to keep paging out to that. We also saw some 151 | |
0b4e3aa0 | 8409 | * errors from Auspex server and NFSv3 can return errors higher than |
fa4905b1 A |
8410 | * ELAST. Those along with NFS known server errors we will "dump" from |
8411 | * vm. Errors we don't expect to occur, we dump and log for further | |
0b4e3aa0 A |
8412 | * analysis. Errors that could be transient, networking ones, |
8413 | * we let vm "retry". Lastly, errors that we retry, but may have potential | |
8414 | * to storm the network, we "retrywithsleep". "sever" will be used in | |
8415 | * in the future to dump all pages of object for cases like ESTALE. | |
8416 | * All this is the basis for the states returned and first guesses on | |
8417 | * error handling. Tweaking expected as more statistics are gathered. | |
8418 | * Note, in the long run we may need another more robust solution to | |
8419 | * have some kind of persistant store when the vm cannot dump nor keep | |
fa4905b1 | 8420 | * retrying as a solution, but this would be a file architectural change |
0b4e3aa0 | 8421 | */ |
fa4905b1 | 8422 | if (!nofreeupl) { /* otherwise stacked file system has to handle this */ |
0b4e3aa0 | 8423 | if (error) { |
2d21ac55 A |
8424 | int abortflags = 0; |
8425 | char action = nfs_pageouterrorhandler(error); | |
0a7de745 | 8426 | |
0b4e3aa0 | 8427 | switch (action) { |
0a7de745 A |
8428 | case DUMP: |
8429 | abortflags = UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY; | |
8430 | break; | |
8431 | case DUMPANDLOG: | |
8432 | abortflags = UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY; | |
8433 | if (error <= NFS_ELAST) { | |
8434 | if ((errorcount[error] % 100) == 0) { | |
8435 | NP(np, "nfs_pageout: unexpected error %d. dumping vm page", error); | |
2d21ac55 | 8436 | } |
0a7de745 A |
8437 | errorcount[error]++; |
8438 | } | |
8439 | break; | |
8440 | case RETRY: | |
8441 | abortflags = UPL_ABORT_FREE_ON_EMPTY; | |
8442 | break; | |
8443 | case SEVER: /* not implemented */ | |
8444 | default: | |
8445 | NP(np, "nfs_pageout: action %d not expected", action); | |
8446 | break; | |
0b4e3aa0 | 8447 | } |
2d21ac55 A |
8448 | |
8449 | ubc_upl_abort_range(pl, pl_offset, pgsize, abortflags); | |
0b4e3aa0 | 8450 | /* return error in all cases above */ |
2d21ac55 | 8451 | } else { |
0b4e3aa0 | 8452 | ubc_upl_commit_range(pl, pl_offset, pgsize, |
0a7de745 A |
8453 | UPL_COMMIT_CLEAR_DIRTY | |
8454 | UPL_COMMIT_FREE_ON_EMPTY); | |
2d21ac55 | 8455 | } |
1c79356b | 8456 | } |
0a7de745 | 8457 | return error; |
1c79356b A |
8458 | } |
8459 | ||
8460 | /* Blktooff derives file offset given a logical block number */ | |
b0d623f7 | 8461 | int |
2d21ac55 | 8462 | nfs_vnop_blktooff( |
91447636 | 8463 | struct vnop_blktooff_args /* { |
0a7de745 A |
8464 | * struct vnodeop_desc *a_desc; |
8465 | * vnode_t a_vp; | |
8466 | * daddr64_t a_lblkno; | |
8467 | * off_t *a_offset; | |
8468 | * } */*ap) | |
1c79356b A |
8469 | { |
8470 | int biosize; | |
91447636 | 8471 | vnode_t vp = ap->a_vp; |
2d21ac55 | 8472 | struct nfsmount *nmp = VTONMP(vp); |
1c79356b | 8473 | |
0a7de745 A |
8474 | if (nfs_mount_gone(nmp)) { |
8475 | return ENXIO; | |
8476 | } | |
0c530ab8 | 8477 | biosize = nmp->nm_biosize; |
1c79356b | 8478 | |
91447636 | 8479 | *ap->a_offset = (off_t)(ap->a_lblkno * biosize); |
1c79356b | 8480 | |
0a7de745 | 8481 | return 0; |
1c79356b A |
8482 | } |
8483 | ||
b0d623f7 | 8484 | int |
2d21ac55 | 8485 | nfs_vnop_offtoblk( |
91447636 | 8486 | struct vnop_offtoblk_args /* { |
0a7de745 A |
8487 | * struct vnodeop_desc *a_desc; |
8488 | * vnode_t a_vp; | |
8489 | * off_t a_offset; | |
8490 | * daddr64_t *a_lblkno; | |
8491 | * } */*ap) | |
1c79356b A |
8492 | { |
8493 | int biosize; | |
91447636 | 8494 | vnode_t vp = ap->a_vp; |
2d21ac55 | 8495 | struct nfsmount *nmp = VTONMP(vp); |
1c79356b | 8496 | |
0a7de745 A |
8497 | if (nfs_mount_gone(nmp)) { |
8498 | return ENXIO; | |
8499 | } | |
0c530ab8 | 8500 | biosize = nmp->nm_biosize; |
1c79356b | 8501 | |
91447636 | 8502 | *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize); |
1c79356b | 8503 | |
0a7de745 | 8504 | return 0; |
1c79356b | 8505 | } |
91447636 | 8506 | |
6d2010ae A |
8507 | /* |
8508 | * vnode change monitoring | |
8509 | */ | |
8510 | int | |
8511 | nfs_vnop_monitor( | |
8512 | struct vnop_monitor_args /* { | |
0a7de745 A |
8513 | * struct vnodeop_desc *a_desc; |
8514 | * vnode_t a_vp; | |
8515 | * uint32_t a_events; | |
8516 | * uint32_t a_flags; | |
8517 | * void *a_handle; | |
8518 | * vfs_context_t a_context; | |
8519 | * } */*ap) | |
6d2010ae A |
8520 | { |
8521 | nfsnode_t np = VTONFS(ap->a_vp); | |
8522 | struct nfsmount *nmp = VTONMP(ap->a_vp); | |
8523 | int error = 0; | |
8524 | ||
0a7de745 A |
8525 | if (nfs_mount_gone(nmp)) { |
8526 | return ENXIO; | |
8527 | } | |
6d2010ae A |
8528 | |
8529 | /* make sure that the vnode's monitoring status is up to date */ | |
8530 | lck_mtx_lock(&nmp->nm_lock); | |
8531 | if (vnode_ismonitored(ap->a_vp)) { | |
8532 | /* This vnode is currently being monitored, make sure we're tracking it. */ | |
8533 | if (np->n_monlink.le_next == NFSNOLIST) { | |
8534 | LIST_INSERT_HEAD(&nmp->nm_monlist, np, n_monlink); | |
8535 | nfs_mount_sock_thread_wake(nmp); | |
8536 | } | |
8537 | } else { | |
8538 | /* This vnode is no longer being monitored, make sure we're not tracking it. */ | |
8539 | /* Wait for any in-progress getattr to complete first. */ | |
8540 | while (np->n_mflag & NMMONSCANINPROG) { | |
cb323159 | 8541 | struct timespec ts = { .tv_sec = 1, .tv_nsec = 0 }; |
6d2010ae | 8542 | np->n_mflag |= NMMONSCANWANT; |
0a7de745 | 8543 | msleep(&np->n_mflag, &nmp->nm_lock, PZERO - 1, "nfswaitmonscan", &ts); |
6d2010ae A |
8544 | } |
8545 | if (np->n_monlink.le_next != NFSNOLIST) { | |
8546 | LIST_REMOVE(np, n_monlink); | |
8547 | np->n_monlink.le_next = NFSNOLIST; | |
8548 | } | |
8549 | } | |
8550 | lck_mtx_unlock(&nmp->nm_lock); | |
8551 | ||
0a7de745 | 8552 | return error; |
6d2010ae A |
8553 | } |
8554 | ||
8555 | /* | |
8556 | * Send a vnode notification for the given events. | |
8557 | */ | |
8558 | void | |
8559 | nfs_vnode_notify(nfsnode_t np, uint32_t events) | |
8560 | { | |
8561 | struct nfsmount *nmp = NFSTONMP(np); | |
8562 | struct nfs_vattr nvattr; | |
8563 | struct vnode_attr vattr, *vap = NULL; | |
8564 | struct timeval now; | |
8565 | ||
8566 | microuptime(&now); | |
8567 | if ((np->n_evtstamp == now.tv_sec) || !nmp) { | |
8568 | /* delay sending this notify */ | |
8569 | np->n_events |= events; | |
8570 | return; | |
8571 | } | |
8572 | events |= np->n_events; | |
8573 | np->n_events = 0; | |
8574 | np->n_evtstamp = now.tv_sec; | |
8575 | ||
8576 | vfs_get_notify_attributes(&vattr); | |
8577 | if (!nfs_getattrcache(np, &nvattr, 0)) { | |
8578 | vap = &vattr; | |
8579 | VATTR_INIT(vap); | |
5ba3f43e | 8580 | |
6d2010ae A |
8581 | VATTR_RETURN(vap, va_fsid, vfs_statfs(nmp->nm_mountp)->f_fsid.val[0]); |
8582 | VATTR_RETURN(vap, va_fileid, nvattr.nva_fileid); | |
8583 | VATTR_RETURN(vap, va_mode, nvattr.nva_mode); | |
8584 | VATTR_RETURN(vap, va_uid, nvattr.nva_uid); | |
8585 | VATTR_RETURN(vap, va_gid, nvattr.nva_gid); | |
8586 | VATTR_RETURN(vap, va_nlink, nvattr.nva_nlink); | |
8587 | } | |
8588 | vnode_notify(NFSTOV(np), events, vap); | |
8589 | } | |
cb323159 | 8590 |