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