]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1989, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software contributed to Berkeley by | |
34 | * Rick Macklem at The University of Guelph. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. All advertising materials mentioning features or use of this software | |
45 | * must display the following acknowledgement: | |
46 | * This product includes software developed by the University of | |
47 | * California, Berkeley and its contributors. | |
48 | * 4. Neither the name of the University nor the names of its contributors | |
49 | * may be used to endorse or promote products derived from this software | |
50 | * without specific prior written permission. | |
51 | * | |
52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
55 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
62 | * SUCH DAMAGE. | |
63 | * | |
64 | * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95 | |
65 | * FreeBSD-Id: nfs_vnops.c,v 1.72 1997/11/07 09:20:48 phk Exp $ | |
66 | */ | |
67 | ||
ea3f0419 A |
68 | #include <nfs/nfs_conf.h> |
69 | #if CONFIG_NFS_CLIENT | |
1c79356b A |
70 | |
71 | /* | |
72 | * vnode op calls for Sun NFS version 2 and 3 | |
73 | */ | |
1c79356b A |
74 | #include <sys/param.h> |
75 | #include <sys/kernel.h> | |
76 | #include <sys/systm.h> | |
77 | #include <sys/resourcevar.h> | |
91447636 A |
78 | #include <sys/proc_internal.h> |
79 | #include <sys/kauth.h> | |
80 | #include <sys/mount_internal.h> | |
1c79356b | 81 | #include <sys/malloc.h> |
91447636 | 82 | #include <sys/kpi_mbuf.h> |
1c79356b | 83 | #include <sys/conf.h> |
91447636 | 84 | #include <sys/vnode_internal.h> |
1c79356b A |
85 | #include <sys/dirent.h> |
86 | #include <sys/fcntl.h> | |
87 | #include <sys/lockf.h> | |
91447636 A |
88 | #include <sys/ubc_internal.h> |
89 | #include <sys/attr.h> | |
90 | #include <sys/signalvar.h> | |
91 | #include <sys/uio_internal.h> | |
cb323159 | 92 | #include <sys/xattr.h> |
1c79356b | 93 | |
1c79356b A |
94 | #include <vfs/vfs_support.h> |
95 | ||
96 | #include <sys/vm.h> | |
1c79356b | 97 | |
2d21ac55 | 98 | #include <sys/time.h> |
1c79356b | 99 | #include <kern/clock.h> |
91447636 | 100 | #include <libkern/OSAtomic.h> |
1c79356b A |
101 | |
102 | #include <miscfs/fifofs/fifo.h> | |
103 | #include <miscfs/specfs/specdev.h> | |
104 | ||
105 | #include <nfs/rpcv2.h> | |
106 | #include <nfs/nfsproto.h> | |
107 | #include <nfs/nfs.h> | |
108 | #include <nfs/nfsnode.h> | |
2d21ac55 | 109 | #include <nfs/nfs_gss.h> |
1c79356b | 110 | #include <nfs/nfsmount.h> |
55e303ae | 111 | #include <nfs/nfs_lock.h> |
1c79356b A |
112 | #include <nfs/xdr_subs.h> |
113 | #include <nfs/nfsm_subs.h> | |
1c79356b A |
114 | |
115 | #include <net/if.h> | |
116 | #include <netinet/in.h> | |
117 | #include <netinet/in_var.h> | |
b0d623f7 | 118 | |
1c79356b | 119 | #include <vm/vm_kern.h> |
b0d623f7 | 120 | #include <vm/vm_pageout.h> |
1c79356b | 121 | |
9bccf70c A |
122 | #include <kern/task.h> |
123 | #include <kern/sched_prim.h> | |
1c79356b | 124 | |
39236c6e | 125 | #define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__) |
fe8ab488 | 126 | #define DEFAULT_READLINK_NOCACHE 0 |
39236c6e | 127 | |
1c79356b | 128 | /* |
2d21ac55 | 129 | * NFS vnode ops |
1c79356b | 130 | */ |
0a7de745 A |
131 | int nfs_vnop_lookup(struct vnop_lookup_args *); |
132 | int nfsspec_vnop_read(struct vnop_read_args *); | |
133 | int nfsspec_vnop_write(struct vnop_write_args *); | |
134 | int nfsspec_vnop_close(struct vnop_close_args *); | |
2d21ac55 | 135 | #if FIFO |
0a7de745 A |
136 | int nfsfifo_vnop_read(struct vnop_read_args *); |
137 | int nfsfifo_vnop_write(struct vnop_write_args *); | |
138 | int nfsfifo_vnop_close(struct vnop_close_args *); | |
2d21ac55 | 139 | #endif |
0a7de745 A |
140 | int nfs_vnop_ioctl(struct vnop_ioctl_args *); |
141 | int nfs_vnop_select(struct vnop_select_args *); | |
142 | int nfs_vnop_setattr(struct vnop_setattr_args *); | |
143 | int nfs_vnop_fsync(struct vnop_fsync_args *); | |
144 | int nfs_vnop_rename(struct vnop_rename_args *); | |
145 | int nfs_vnop_readdir(struct vnop_readdir_args *); | |
146 | int nfs_vnop_readlink(struct vnop_readlink_args *); | |
147 | int nfs_vnop_pathconf(struct vnop_pathconf_args *); | |
148 | int nfs_vnop_pagein(struct vnop_pagein_args *); | |
149 | int nfs_vnop_pageout(struct vnop_pageout_args *); | |
150 | int nfs_vnop_blktooff(struct vnop_blktooff_args *); | |
151 | int nfs_vnop_offtoblk(struct vnop_offtoblk_args *); | |
152 | int nfs_vnop_blockmap(struct vnop_blockmap_args *); | |
153 | int nfs_vnop_monitor(struct vnop_monitor_args *); | |
154 | ||
155 | int nfs3_vnop_create(struct vnop_create_args *); | |
156 | int nfs3_vnop_mknod(struct vnop_mknod_args *); | |
157 | int nfs3_vnop_getattr(struct vnop_getattr_args *); | |
158 | int nfs3_vnop_link(struct vnop_link_args *); | |
159 | int nfs3_vnop_mkdir(struct vnop_mkdir_args *); | |
160 | int nfs3_vnop_rmdir(struct vnop_rmdir_args *); | |
161 | int nfs3_vnop_symlink(struct vnop_symlink_args *); | |
2d21ac55 | 162 | |
cb323159 | 163 | |
91447636 | 164 | vnop_t **nfsv2_vnodeop_p; |
cb323159 A |
165 | static const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = { |
166 | { .opve_op = &vnop_default_desc, .opve_impl = (vnop_t *)vn_default_error }, | |
f427ee49 A |
167 | { .opve_op = &vnop_lookup_desc, .opve_impl = (vnop_t *)nfs_vnop_lookup }, /* lookup */ |
168 | { .opve_op = &vnop_create_desc, .opve_impl = (vnop_t *)nfs3_vnop_create }, /* create */ | |
169 | { .opve_op = &vnop_mknod_desc, .opve_impl = (vnop_t *)nfs3_vnop_mknod }, /* mknod */ | |
170 | { .opve_op = &vnop_open_desc, .opve_impl = (vnop_t *)nfs_vnop_open }, /* open */ | |
171 | { .opve_op = &vnop_close_desc, .opve_impl = (vnop_t *)nfs_vnop_close }, /* close */ | |
172 | { .opve_op = &vnop_access_desc, .opve_impl = (vnop_t *)nfs_vnop_access }, /* access */ | |
173 | { .opve_op = &vnop_getattr_desc, .opve_impl = (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
174 | { .opve_op = &vnop_setattr_desc, .opve_impl = (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
175 | { .opve_op = &vnop_read_desc, .opve_impl = (vnop_t *)nfs_vnop_read }, /* read */ | |
176 | { .opve_op = &vnop_write_desc, .opve_impl = (vnop_t *)nfs_vnop_write }, /* write */ | |
177 | { .opve_op = &vnop_ioctl_desc, .opve_impl = (vnop_t *)nfs_vnop_ioctl }, /* ioctl */ | |
178 | { .opve_op = &vnop_select_desc, .opve_impl = (vnop_t *)nfs_vnop_select }, /* select */ | |
179 | { .opve_op = &vnop_revoke_desc, .opve_impl = (vnop_t *)nfs_vnop_revoke }, /* revoke */ | |
180 | { .opve_op = &vnop_mmap_desc, .opve_impl = (vnop_t *)nfs_vnop_mmap }, /* mmap */ | |
181 | { .opve_op = &vnop_mmap_check_desc, .opve_impl = (vnop_t *)nfs_vnop_mmap_check }, /* mmap_check */ | |
182 | { .opve_op = &vnop_mnomap_desc, .opve_impl = (vnop_t *)nfs_vnop_mnomap }, /* mnomap */ | |
183 | { .opve_op = &vnop_fsync_desc, .opve_impl = (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
184 | { .opve_op = &vnop_remove_desc, .opve_impl = (vnop_t *)nfs_vnop_remove }, /* remove */ | |
185 | { .opve_op = &vnop_link_desc, .opve_impl = (vnop_t *)nfs3_vnop_link }, /* link */ | |
186 | { .opve_op = &vnop_rename_desc, .opve_impl = (vnop_t *)nfs_vnop_rename }, /* rename */ | |
187 | { .opve_op = &vnop_mkdir_desc, .opve_impl = (vnop_t *)nfs3_vnop_mkdir }, /* mkdir */ | |
188 | { .opve_op = &vnop_rmdir_desc, .opve_impl = (vnop_t *)nfs3_vnop_rmdir }, /* rmdir */ | |
189 | { .opve_op = &vnop_symlink_desc, .opve_impl = (vnop_t *)nfs3_vnop_symlink }, /* symlink */ | |
190 | { .opve_op = &vnop_readdir_desc, .opve_impl = (vnop_t *)nfs_vnop_readdir }, /* readdir */ | |
191 | { .opve_op = &vnop_readlink_desc, .opve_impl = (vnop_t *)nfs_vnop_readlink }, /* readlink */ | |
192 | { .opve_op = &vnop_inactive_desc, .opve_impl = (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
193 | { .opve_op = &vnop_reclaim_desc, .opve_impl = (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
194 | { .opve_op = &vnop_strategy_desc, .opve_impl = (vnop_t *)err_strategy }, /* strategy */ | |
195 | { .opve_op = &vnop_pathconf_desc, .opve_impl = (vnop_t *)nfs_vnop_pathconf }, /* pathconf */ | |
196 | { .opve_op = &vnop_advlock_desc, .opve_impl = (vnop_t *)nfs_vnop_advlock }, /* advlock */ | |
197 | { .opve_op = &vnop_bwrite_desc, .opve_impl = (vnop_t *)err_bwrite }, /* bwrite */ | |
198 | { .opve_op = &vnop_pagein_desc, .opve_impl = (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
199 | { .opve_op = &vnop_pageout_desc, .opve_impl = (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
200 | { .opve_op = &vnop_copyfile_desc, .opve_impl = (vnop_t *)err_copyfile }, /* Copyfile */ | |
201 | { .opve_op = &vnop_blktooff_desc, .opve_impl = (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
202 | { .opve_op = &vnop_offtoblk_desc, .opve_impl = (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
203 | { .opve_op = &vnop_blockmap_desc, .opve_impl = (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
204 | { .opve_op = &vnop_monitor_desc, .opve_impl = (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
cb323159 | 205 | { .opve_op = NULL, .opve_impl = NULL } |
1c79356b | 206 | }; |
cb323159 | 207 | const struct vnodeopv_desc nfsv2_vnodeop_opv_desc = |
0a7de745 | 208 | { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries }; |
2d21ac55 | 209 | |
cb323159 A |
210 | |
211 | #if CONFIG_NFS4 | |
2d21ac55 | 212 | vnop_t **nfsv4_vnodeop_p; |
cb323159 | 213 | static const struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = { |
2d21ac55 | 214 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
f427ee49 A |
215 | { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup }, /* lookup */ |
216 | { &vnop_create_desc, (vnop_t *)nfs4_vnop_create }, /* create */ | |
217 | { &vnop_mknod_desc, (vnop_t *)nfs4_vnop_mknod }, /* mknod */ | |
218 | { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */ | |
219 | { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */ | |
220 | { &vnop_access_desc, (vnop_t *)nfs_vnop_access }, /* access */ | |
221 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
222 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
223 | { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */ | |
224 | { &vnop_write_desc, (vnop_t *)nfs_vnop_write }, /* write */ | |
225 | { &vnop_ioctl_desc, (vnop_t *)nfs_vnop_ioctl }, /* ioctl */ | |
226 | { &vnop_select_desc, (vnop_t *)nfs_vnop_select }, /* select */ | |
227 | { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke }, /* revoke */ | |
228 | { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */ | |
229 | { &vnop_mmap_check_desc, (vnop_t *)nfs_vnop_mmap_check }, /* mmap_check */ | |
230 | { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */ | |
231 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
232 | { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove }, /* remove */ | |
233 | { &vnop_link_desc, (vnop_t *)nfs4_vnop_link }, /* link */ | |
234 | { &vnop_rename_desc, (vnop_t *)nfs_vnop_rename }, /* rename */ | |
235 | { &vnop_mkdir_desc, (vnop_t *)nfs4_vnop_mkdir }, /* mkdir */ | |
236 | { &vnop_rmdir_desc, (vnop_t *)nfs4_vnop_rmdir }, /* rmdir */ | |
237 | { &vnop_symlink_desc, (vnop_t *)nfs4_vnop_symlink }, /* symlink */ | |
238 | { &vnop_readdir_desc, (vnop_t *)nfs_vnop_readdir }, /* readdir */ | |
239 | { &vnop_readlink_desc, (vnop_t *)nfs_vnop_readlink }, /* readlink */ | |
240 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
241 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
242 | { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */ | |
243 | { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */ | |
244 | { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */ | |
245 | { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */ | |
246 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
247 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
248 | { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */ | |
249 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
250 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
251 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
252 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ | |
253 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
254 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr }, /* removexattr */ | |
255 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr }, /* listxattr */ | |
6d2010ae | 256 | #if NAMEDSTREAMS |
f427ee49 A |
257 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ |
258 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
259 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream }, /* removenamedstream */ | |
6d2010ae | 260 | #endif |
f427ee49 | 261 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
2d21ac55 A |
262 | { NULL, NULL } |
263 | }; | |
cb323159 | 264 | const struct vnodeopv_desc nfsv4_vnodeop_opv_desc = |
0a7de745 | 265 | { &nfsv4_vnodeop_p, nfsv4_vnodeop_entries }; |
cb323159 | 266 | #endif |
1c79356b A |
267 | |
268 | /* | |
269 | * Special device vnode ops | |
270 | */ | |
91447636 | 271 | vnop_t **spec_nfsv2nodeop_p; |
cb323159 | 272 | static const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = { |
91447636 | 273 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
274 | { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */ |
275 | { &vnop_create_desc, (vnop_t *)spec_create }, /* create */ | |
276 | { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */ | |
277 | { &vnop_open_desc, (vnop_t *)spec_open }, /* open */ | |
278 | { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */ | |
279 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
280 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
281 | { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */ | |
282 | { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */ | |
283 | { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */ | |
284 | { &vnop_select_desc, (vnop_t *)spec_select }, /* select */ | |
285 | { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */ | |
286 | { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */ | |
287 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
288 | { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */ | |
289 | { &vnop_link_desc, (vnop_t *)spec_link }, /* link */ | |
290 | { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */ | |
291 | { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */ | |
292 | { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */ | |
293 | { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */ | |
294 | { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */ | |
295 | { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */ | |
296 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
297 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
298 | { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */ | |
299 | { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */ | |
300 | { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */ | |
301 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
302 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
303 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
304 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
305 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
306 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
307 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
1c79356b A |
308 | { NULL, NULL } |
309 | }; | |
cb323159 | 310 | const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc = |
0a7de745 | 311 | { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries }; |
cb323159 | 312 | #if CONFIG_NFS4 |
2d21ac55 | 313 | vnop_t **spec_nfsv4nodeop_p; |
cb323159 | 314 | static const struct vnodeopv_entry_desc spec_nfsv4nodeop_entries[] = { |
2d21ac55 | 315 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
316 | { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */ |
317 | { &vnop_create_desc, (vnop_t *)spec_create }, /* create */ | |
318 | { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */ | |
319 | { &vnop_open_desc, (vnop_t *)spec_open }, /* open */ | |
320 | { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */ | |
321 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
322 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
323 | { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */ | |
324 | { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */ | |
325 | { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */ | |
326 | { &vnop_select_desc, (vnop_t *)spec_select }, /* select */ | |
327 | { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */ | |
328 | { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */ | |
329 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
330 | { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */ | |
331 | { &vnop_link_desc, (vnop_t *)spec_link }, /* link */ | |
332 | { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */ | |
333 | { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */ | |
334 | { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */ | |
335 | { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */ | |
336 | { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */ | |
337 | { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */ | |
338 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
339 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
340 | { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */ | |
341 | { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */ | |
342 | { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */ | |
343 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
344 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
345 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
346 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
347 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
348 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
349 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ | |
350 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
6d2010ae A |
351 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ |
352 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
353 | #if NAMEDSTREAMS | |
0a7de745 A |
354 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ |
355 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
6d2010ae A |
356 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ |
357 | #endif | |
0a7de745 | 358 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
2d21ac55 A |
359 | { NULL, NULL } |
360 | }; | |
cb323159 | 361 | const struct vnodeopv_desc spec_nfsv4nodeop_opv_desc = |
0a7de745 | 362 | { &spec_nfsv4nodeop_p, spec_nfsv4nodeop_entries }; |
cb323159 | 363 | #endif /* CONFIG_NFS4 */ |
1c79356b | 364 | |
2d21ac55 | 365 | #if FIFO |
91447636 | 366 | vnop_t **fifo_nfsv2nodeop_p; |
cb323159 | 367 | static const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = { |
91447636 | 368 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
369 | { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */ |
370 | { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */ | |
371 | { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */ | |
372 | { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */ | |
373 | { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */ | |
374 | { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */ | |
375 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
376 | { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */ | |
377 | { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */ | |
378 | { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */ | |
379 | { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */ | |
380 | { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */ | |
381 | { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */ | |
382 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
383 | { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */ | |
384 | { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */ | |
385 | { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */ | |
386 | { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */ | |
387 | { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */ | |
388 | { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */ | |
389 | { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */ | |
390 | { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */ | |
391 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
392 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
393 | { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */ | |
394 | { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */ | |
395 | { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */ | |
396 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
397 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
398 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
399 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
400 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
401 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
402 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ | |
1c79356b A |
403 | { NULL, NULL } |
404 | }; | |
cb323159 | 405 | const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc = |
0a7de745 | 406 | { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries }; |
cb323159 | 407 | #endif |
0b4e3aa0 | 408 | |
cb323159 A |
409 | #if CONFIG_NFS4 |
410 | #if FIFO | |
2d21ac55 | 411 | vnop_t **fifo_nfsv4nodeop_p; |
cb323159 | 412 | static const struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries[] = { |
2d21ac55 | 413 | { &vnop_default_desc, (vnop_t *)vn_default_error }, |
0a7de745 A |
414 | { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */ |
415 | { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */ | |
416 | { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */ | |
417 | { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */ | |
418 | { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */ | |
419 | { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */ | |
420 | { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */ | |
421 | { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */ | |
422 | { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */ | |
423 | { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */ | |
424 | { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */ | |
425 | { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */ | |
426 | { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */ | |
427 | { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */ | |
428 | { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */ | |
429 | { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */ | |
430 | { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */ | |
431 | { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */ | |
432 | { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */ | |
433 | { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */ | |
434 | { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */ | |
435 | { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */ | |
436 | { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */ | |
437 | { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */ | |
438 | { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */ | |
439 | { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */ | |
440 | { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */ | |
441 | { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */ | |
442 | { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */ | |
443 | { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */ | |
444 | { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */ | |
445 | { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */ | |
446 | { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */ | |
447 | { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */ | |
448 | { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */ | |
6d2010ae A |
449 | { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */ |
450 | { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */ | |
451 | #if NAMEDSTREAMS | |
0a7de745 A |
452 | { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */ |
453 | { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */ | |
6d2010ae A |
454 | { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */ |
455 | #endif | |
0a7de745 | 456 | { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */ |
2d21ac55 | 457 | { NULL, NULL } |
0b4e3aa0 | 458 | }; |
cb323159 | 459 | const struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc = |
0a7de745 | 460 | { &fifo_nfsv4nodeop_p, fifo_nfsv4nodeop_entries }; |
2d21ac55 | 461 | #endif /* FIFO */ |
cb323159 | 462 | #endif /* CONFIG_NFS4 */ |
0b4e3aa0 | 463 | |
0a7de745 A |
464 | int nfs_sillyrename(nfsnode_t, nfsnode_t, struct componentname *, vfs_context_t); |
465 | int nfs_getattr_internal(nfsnode_t, struct nfs_vattr *, vfs_context_t, int); | |
466 | int nfs_refresh_fh(nfsnode_t, vfs_context_t); | |
2d21ac55 | 467 | |
cb323159 | 468 | |
f427ee49 A |
469 | ZONE_VIEW_DEFINE(ZV_NFSDIROFF, "NFSV3 diroff", |
470 | KHEAP_ID_DATA_BUFFERS, sizeof(struct nfsdmap)); | |
471 | ||
472 | static void | |
473 | nfs_dir_buf_cache_lookup_boundaries(struct nfsbuf *bp, int *sof, int *eof) | |
474 | { | |
475 | if (bp) { | |
476 | struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
477 | if (sof && bp->nb_lblkno == 0) { | |
478 | *sof = 1; | |
479 | } | |
480 | if (eof && ISSET(ndbhp->ndbh_flags, NDB_EOF)) { | |
481 | *eof = 1; | |
482 | } | |
483 | } | |
484 | } | |
485 | ||
ea3f0419 A |
486 | /* |
487 | * Update nfsnode attributes to avoid extra getattr calls for each direntry. | |
488 | * This function should be called only if RDIRPLUS flag is enabled. | |
489 | */ | |
490 | void | |
491 | nfs_rdirplus_update_node_attrs(nfsnode_t dnp, struct direntry *dp, fhandle_t *fhp, struct nfs_vattr *nvattrp, uint64_t *savedxidp) | |
492 | { | |
493 | nfsnode_t np; | |
494 | struct componentname cn; | |
495 | int isdot = (dp->d_namlen == 1) && (dp->d_name[0] == '.'); | |
496 | int isdotdot = (dp->d_namlen == 2) && (dp->d_name[0] == '.') && (dp->d_name[1] == '.'); | |
f427ee49 A |
497 | int should_update_fileid = nvattrp->nva_flags & NFS_FFLAG_FILEID_CONTAINS_XID; |
498 | uint64_t xid = 0; | |
ea3f0419 A |
499 | |
500 | if (isdot || isdotdot) { | |
501 | return; | |
502 | } | |
503 | ||
504 | np = NULL; | |
505 | bzero(&cn, sizeof(cn)); | |
506 | cn.cn_nameptr = dp->d_name; | |
507 | cn.cn_namelen = dp->d_namlen; | |
508 | cn.cn_nameiop = LOOKUP; | |
509 | ||
f427ee49 A |
510 | /* xid might be stashed in nva_fileid is rdirplus is enabled */ |
511 | if (should_update_fileid) { | |
512 | xid = nvattrp->nva_fileid; | |
513 | nvattrp->nva_fileid = dp->d_fileno; | |
514 | } | |
ea3f0419 | 515 | nfs_nget(NFSTOMP(dnp), dnp, &cn, fhp->fh_data, fhp->fh_len, nvattrp, savedxidp, RPCAUTH_UNKNOWN, NG_NOCREATE, &np); |
f427ee49 A |
516 | if (should_update_fileid) { |
517 | nvattrp->nva_fileid = xid; | |
518 | } | |
ea3f0419 A |
519 | if (np) { |
520 | nfs_node_unlock(np); | |
521 | vnode_put(NFSTOV(np)); | |
522 | } | |
523 | } | |
524 | ||
2d21ac55 A |
525 | /* |
526 | * Find the slot in the access cache for this UID. | |
527 | * If adding and no existing slot is found, reuse slots in FIFO order. | |
6d2010ae | 528 | * The index of the next slot to use is kept in the last entry of the n_access array. |
2d21ac55 A |
529 | */ |
530 | int | |
6d2010ae | 531 | nfs_node_access_slot(nfsnode_t np, uid_t uid, int add) |
0b4e3aa0 | 532 | { |
2d21ac55 A |
533 | int slot; |
534 | ||
0a7de745 A |
535 | for (slot = 0; slot < NFS_ACCESS_CACHE_SIZE; slot++) { |
536 | if (np->n_accessuid[slot] == uid) { | |
2d21ac55 | 537 | break; |
0a7de745 A |
538 | } |
539 | } | |
2d21ac55 | 540 | if (slot == NFS_ACCESS_CACHE_SIZE) { |
0a7de745 A |
541 | if (!add) { |
542 | return -1; | |
543 | } | |
6d2010ae A |
544 | slot = np->n_access[NFS_ACCESS_CACHE_SIZE]; |
545 | np->n_access[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE; | |
2d21ac55 | 546 | } |
0a7de745 | 547 | return slot; |
0b4e3aa0 | 548 | } |
1c79356b | 549 | |
2d21ac55 | 550 | int |
fe8ab488 | 551 | nfs3_access_rpc(nfsnode_t np, u_int32_t *access, int rpcflags, vfs_context_t ctx) |
1c79356b | 552 | { |
f427ee49 | 553 | int error = 0, lockerror = ENOENT, status = 0, slot; |
6d2010ae | 554 | uint32_t access_result = 0; |
fa4905b1 | 555 | u_int64_t xid; |
2d21ac55 | 556 | struct nfsm_chain nmreq, nmrep; |
813fb2f6 | 557 | struct nfsmount *nmp; |
55e303ae | 558 | struct timeval now; |
2d21ac55 A |
559 | uid_t uid; |
560 | ||
561 | nfsm_chain_null(&nmreq); | |
562 | nfsm_chain_null(&nmrep); | |
563 | ||
564 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3) + NFSX_UNSIGNED); | |
565 | nfsm_chain_add_fh(error, &nmreq, NFS_VER3, np->n_fhp, np->n_fhsize); | |
6d2010ae | 566 | nfsm_chain_add_32(error, &nmreq, *access); |
2d21ac55 A |
567 | nfsm_chain_build_done(error, &nmreq); |
568 | nfsmout_if(error); | |
fe8ab488 | 569 | error = nfs_request2(np, NULL, &nmreq, NFSPROC_ACCESS, |
0a7de745 A |
570 | vfs_context_thread(ctx), vfs_context_ucred(ctx), |
571 | NULL, rpcflags, &nmrep, &xid, &status); | |
572 | if ((lockerror = nfs_node_lock(np))) { | |
b0d623f7 | 573 | error = lockerror; |
0a7de745 | 574 | } |
2d21ac55 | 575 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 | 576 | if (!error) { |
2d21ac55 | 577 | error = status; |
0a7de745 | 578 | } |
6d2010ae | 579 | nfsm_chain_get_32(error, &nmrep, access_result); |
2d21ac55 | 580 | nfsmout_if(error); |
0b4e3aa0 | 581 | |
813fb2f6 A |
582 | /* XXXab do we really need mount here, also why are we doing access cache management here? */ |
583 | nmp = NFSTONMP(np); | |
584 | if (nfs_mount_gone(nmp)) { | |
585 | error = ENXIO; | |
586 | } | |
587 | nfsmout_if(error); | |
588 | ||
cb323159 | 589 | #if CONFIG_NFS_GSS |
813fb2f6 A |
590 | if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) { |
591 | uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx)); | |
592 | } else { | |
593 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
594 | } | |
cb323159 A |
595 | #else |
596 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
597 | #endif /* CONFIG_NFS_GSS */ | |
6d2010ae A |
598 | slot = nfs_node_access_slot(np, uid, 1); |
599 | np->n_accessuid[slot] = uid; | |
2d21ac55 | 600 | microuptime(&now); |
6d2010ae A |
601 | np->n_accessstamp[slot] = now.tv_sec; |
602 | np->n_access[slot] = access_result; | |
2d21ac55 A |
603 | |
604 | /* | |
605 | * If we asked for DELETE but didn't get it, the server | |
606 | * may simply not support returning that bit (possible | |
607 | * on UNIX systems). So, we'll assume that it is OK, | |
608 | * and just let any subsequent delete action fail if it | |
609 | * really isn't deletable. | |
610 | */ | |
6d2010ae | 611 | if ((*access & NFS_ACCESS_DELETE) && |
0a7de745 | 612 | !(np->n_access[slot] & NFS_ACCESS_DELETE)) { |
6d2010ae | 613 | np->n_access[slot] |= NFS_ACCESS_DELETE; |
0a7de745 | 614 | } |
6d2010ae | 615 | /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */ |
0a7de745 A |
616 | if (nfs_access_dotzfs && (np->n_flag & NISDOTZFSCHILD)) { |
617 | np->n_access[slot] |= (NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND | NFS_ACCESS_DELETE); | |
618 | } | |
6d2010ae A |
619 | /* pass back the access returned with this request */ |
620 | *access = np->n_access[slot]; | |
2d21ac55 | 621 | nfsmout: |
0a7de745 | 622 | if (!lockerror) { |
b0d623f7 | 623 | nfs_node_unlock(np); |
0a7de745 | 624 | } |
2d21ac55 A |
625 | nfsm_chain_cleanup(&nmreq); |
626 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 627 | return error; |
1c79356b A |
628 | } |
629 | ||
cb323159 | 630 | |
1c79356b | 631 | /* |
2d21ac55 A |
632 | * NFS access vnode op. |
633 | * For NFS version 2, just return ok. File accesses may fail later. | |
6d2010ae A |
634 | * For NFS version 3+, use the access RPC to check accessibility. If file |
635 | * permissions are changed on the server, accesses might still fail later. | |
1c79356b | 636 | */ |
2d21ac55 A |
637 | int |
638 | nfs_vnop_access( | |
91447636 | 639 | struct vnop_access_args /* { |
0a7de745 A |
640 | * struct vnodeop_desc *a_desc; |
641 | * vnode_t a_vp; | |
642 | * int a_action; | |
643 | * vfs_context_t a_context; | |
644 | * } */*ap) | |
1c79356b | 645 | { |
2d21ac55 | 646 | vfs_context_t ctx = ap->a_context; |
91447636 | 647 | vnode_t vp = ap->a_vp; |
fe8ab488 | 648 | int error = 0, slot, dorpc, rpcflags = 0; |
6d2010ae | 649 | u_int32_t access, waccess; |
2d21ac55 A |
650 | nfsnode_t np = VTONFS(vp); |
651 | struct nfsmount *nmp; | |
652 | int nfsvers; | |
55e303ae | 653 | struct timeval now; |
2d21ac55 A |
654 | uid_t uid; |
655 | ||
656 | nmp = VTONMP(vp); | |
0a7de745 A |
657 | if (nfs_mount_gone(nmp)) { |
658 | return ENXIO; | |
659 | } | |
2d21ac55 A |
660 | nfsvers = nmp->nm_vers; |
661 | ||
cb323159 A |
662 | |
663 | if (nfsvers == NFS_VER2 || NMFLAG(nmp, NOOPAQUE_AUTH)) { | |
2d21ac55 | 664 | if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) && |
0a7de745 A |
665 | vfs_isrdonly(vnode_mount(vp))) { |
666 | return EROFS; | |
667 | } | |
668 | return 0; | |
2d21ac55 | 669 | } |
1c79356b A |
670 | |
671 | /* | |
2d21ac55 | 672 | * For NFS v3, do an access rpc, otherwise you are stuck emulating |
1c79356b A |
673 | * ufs_access() locally using the vattr. This may not be correct, |
674 | * since the server may apply other access criteria such as | |
675 | * client uid-->server uid mapping that we do not know about, but | |
676 | * this is better than just returning anything that is lying about | |
677 | * in the cache. | |
678 | */ | |
91447636 | 679 | |
2d21ac55 A |
680 | /* |
681 | * Convert KAUTH primitives to NFS access rights. | |
682 | */ | |
6d2010ae | 683 | access = 0; |
2d21ac55 A |
684 | if (vnode_isdir(vp)) { |
685 | /* directory */ | |
686 | if (ap->a_action & | |
687 | (KAUTH_VNODE_LIST_DIRECTORY | | |
0a7de745 | 688 | KAUTH_VNODE_READ_EXTATTRIBUTES)) { |
6d2010ae | 689 | access |= NFS_ACCESS_READ; |
0a7de745 A |
690 | } |
691 | if (ap->a_action & KAUTH_VNODE_SEARCH) { | |
6d2010ae | 692 | access |= NFS_ACCESS_LOOKUP; |
0a7de745 | 693 | } |
2d21ac55 A |
694 | if (ap->a_action & |
695 | (KAUTH_VNODE_ADD_FILE | | |
0a7de745 | 696 | KAUTH_VNODE_ADD_SUBDIRECTORY)) { |
6d2010ae | 697 | access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; |
0a7de745 A |
698 | } |
699 | if (ap->a_action & KAUTH_VNODE_DELETE_CHILD) { | |
6d2010ae | 700 | access |= NFS_ACCESS_MODIFY; |
0a7de745 | 701 | } |
91447636 | 702 | } else { |
2d21ac55 A |
703 | /* file */ |
704 | if (ap->a_action & | |
705 | (KAUTH_VNODE_READ_DATA | | |
0a7de745 | 706 | KAUTH_VNODE_READ_EXTATTRIBUTES)) { |
6d2010ae | 707 | access |= NFS_ACCESS_READ; |
0a7de745 A |
708 | } |
709 | if (ap->a_action & KAUTH_VNODE_WRITE_DATA) { | |
6d2010ae | 710 | access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; |
0a7de745 A |
711 | } |
712 | if (ap->a_action & KAUTH_VNODE_APPEND_DATA) { | |
6d2010ae | 713 | access |= NFS_ACCESS_EXTEND; |
0a7de745 A |
714 | } |
715 | if (ap->a_action & KAUTH_VNODE_EXECUTE) { | |
6d2010ae | 716 | access |= NFS_ACCESS_EXECUTE; |
0a7de745 | 717 | } |
2d21ac55 A |
718 | } |
719 | /* common */ | |
0a7de745 | 720 | if (ap->a_action & KAUTH_VNODE_DELETE) { |
6d2010ae | 721 | access |= NFS_ACCESS_DELETE; |
0a7de745 | 722 | } |
2d21ac55 A |
723 | if (ap->a_action & |
724 | (KAUTH_VNODE_WRITE_ATTRIBUTES | | |
725 | KAUTH_VNODE_WRITE_EXTATTRIBUTES | | |
0a7de745 | 726 | KAUTH_VNODE_WRITE_SECURITY)) { |
6d2010ae | 727 | access |= NFS_ACCESS_MODIFY; |
0a7de745 | 728 | } |
2d21ac55 | 729 | /* XXX this is pretty dubious */ |
0a7de745 | 730 | if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER) { |
6d2010ae | 731 | access |= NFS_ACCESS_MODIFY; |
0a7de745 | 732 | } |
2d21ac55 A |
733 | |
734 | /* if caching, always ask for every right */ | |
735 | if (nfs_access_cache_timeout > 0) { | |
6d2010ae | 736 | waccess = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | |
0a7de745 A |
737 | NFS_ACCESS_EXTEND | NFS_ACCESS_EXECUTE | |
738 | NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; | |
2d21ac55 | 739 | } else { |
6d2010ae | 740 | waccess = access; |
2d21ac55 A |
741 | } |
742 | ||
0a7de745 A |
743 | if ((error = nfs_node_lock(np))) { |
744 | return error; | |
745 | } | |
2d21ac55 A |
746 | |
747 | /* | |
748 | * Does our cached result allow us to give a definite yes to | |
749 | * this request? | |
750 | */ | |
cb323159 | 751 | #if CONFIG_NFS_GSS |
0a7de745 | 752 | if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) { |
3e170ce0 | 753 | uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx)); |
0a7de745 | 754 | } else { |
3e170ce0 | 755 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); |
0a7de745 | 756 | } |
cb323159 A |
757 | #else |
758 | uid = kauth_cred_getuid(vfs_context_ucred(ctx)); | |
759 | #endif /* CONFIG_NFS_GSS */ | |
6d2010ae | 760 | slot = nfs_node_access_slot(np, uid, 0); |
2d21ac55 | 761 | dorpc = 1; |
6d2010ae | 762 | if (access == 0) { |
b0d623f7 A |
763 | /* not asking for any rights understood by NFS, so don't bother doing an RPC */ |
764 | /* OSAddAtomic(1, &nfsstats.accesscache_hits); */ | |
765 | dorpc = 0; | |
6d2010ae A |
766 | waccess = 0; |
767 | } else if (NACCESSVALID(np, slot)) { | |
fe8ab488 A |
768 | microuptime(&now); |
769 | if (((now.tv_sec < (np->n_accessstamp[slot] + nfs_access_cache_timeout)) && | |
770 | ((np->n_access[slot] & access) == access)) || nfs_use_cache(nmp)) { | |
b0d623f7 | 771 | /* OSAddAtomic(1, &nfsstats.accesscache_hits); */ |
fe8ab488 A |
772 | dorpc = 0; |
773 | waccess = np->n_access[slot]; | |
1c79356b | 774 | } |
fa4905b1 | 775 | } |
b0d623f7 | 776 | nfs_node_unlock(np); |
2d21ac55 A |
777 | if (dorpc) { |
778 | /* Either a no, or a don't know. Go to the wire. */ | |
b0d623f7 | 779 | /* OSAddAtomic(1, &nfsstats.accesscache_misses); */ |
fe8ab488 A |
780 | |
781 | /* | |
782 | * Allow an access call to timeout if we have it cached | |
783 | * so we won't hang if the server isn't responding. | |
784 | */ | |
0a7de745 | 785 | if (NACCESSVALID(np, slot)) { |
fe8ab488 | 786 | rpcflags |= R_SOFT; |
0a7de745 | 787 | } |
fe8ab488 A |
788 | |
789 | error = nmp->nm_funcs->nf_access_rpc(np, &waccess, rpcflags, ctx); | |
790 | ||
791 | /* | |
792 | * If the server didn't respond return the cached access. | |
793 | */ | |
794 | if ((error == ETIMEDOUT) && (rpcflags & R_SOFT)) { | |
795 | error = 0; | |
796 | waccess = np->n_access[slot]; | |
797 | } | |
2d21ac55 | 798 | } |
0a7de745 | 799 | if (!error && ((waccess & access) != access)) { |
2d21ac55 | 800 | error = EACCES; |
0a7de745 | 801 | } |
91447636 | 802 | |
0a7de745 | 803 | return error; |
1c79356b A |
804 | } |
805 | ||
6d2010ae | 806 | |
1c79356b | 807 | /* |
2d21ac55 | 808 | * NFS open vnode op |
6d2010ae A |
809 | * |
810 | * Perform various update/invalidation checks and then add the | |
811 | * open to the node. Regular files will have an open file structure | |
812 | * on the node and, for NFSv4, perform an OPEN request on the server. | |
1c79356b | 813 | */ |
2d21ac55 | 814 | int |
6d2010ae | 815 | nfs_vnop_open( |
91447636 | 816 | struct vnop_open_args /* { |
0a7de745 A |
817 | * struct vnodeop_desc *a_desc; |
818 | * vnode_t a_vp; | |
819 | * int a_mode; | |
820 | * vfs_context_t a_context; | |
821 | * } */*ap) | |
1c79356b | 822 | { |
2d21ac55 | 823 | vfs_context_t ctx = ap->a_context; |
91447636 | 824 | vnode_t vp = ap->a_vp; |
2d21ac55 A |
825 | nfsnode_t np = VTONFS(vp); |
826 | struct nfsmount *nmp; | |
6d2010ae A |
827 | int error, accessMode, denyMode, opened = 0; |
828 | struct nfs_open_owner *noop = NULL; | |
829 | struct nfs_open_file *nofp = NULL; | |
91447636 | 830 | enum vtype vtype; |
6d2010ae | 831 | |
0a7de745 A |
832 | if (!(ap->a_mode & (FREAD | FWRITE))) { |
833 | return EINVAL; | |
834 | } | |
2d21ac55 A |
835 | |
836 | nmp = VTONMP(vp); | |
0a7de745 A |
837 | if (nfs_mount_gone(nmp)) { |
838 | return ENXIO; | |
839 | } | |
840 | if (np->n_flag & NREVOKE) { | |
841 | return EIO; | |
842 | } | |
1c79356b | 843 | |
91447636 | 844 | vtype = vnode_vtype(vp); |
0a7de745 A |
845 | if ((vtype != VREG) && (vtype != VDIR) && (vtype != VLNK)) { |
846 | return EACCES; | |
847 | } | |
6d2010ae A |
848 | |
849 | /* First, check if we need to update/invalidate */ | |
0a7de745 | 850 | if (ISSET(np->n_flag, NUPDATESIZE)) { |
2d21ac55 | 851 | nfs_data_update_size(np, 0); |
0a7de745 A |
852 | } |
853 | if ((error = nfs_node_lock(np))) { | |
854 | return error; | |
855 | } | |
91447636 A |
856 | if (np->n_flag & NNEEDINVALIDATE) { |
857 | np->n_flag &= ~NNEEDINVALIDATE; | |
0a7de745 | 858 | if (vtype == VDIR) { |
b0d623f7 | 859 | nfs_invaldir(np); |
0a7de745 | 860 | } |
b0d623f7 | 861 | nfs_node_unlock(np); |
0a7de745 A |
862 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
863 | if ((error = nfs_node_lock(np))) { | |
864 | return error; | |
865 | } | |
91447636 | 866 | } |
0a7de745 | 867 | if (vtype == VREG) { |
b0d623f7 | 868 | np->n_lastrahead = -1; |
0a7de745 | 869 | } |
91447636 | 870 | if (np->n_flag & NMODIFIED) { |
0a7de745 | 871 | if (vtype == VDIR) { |
b0d623f7 | 872 | nfs_invaldir(np); |
0a7de745 | 873 | } |
b0d623f7 | 874 | nfs_node_unlock(np); |
0a7de745 A |
875 | if ((error = nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1))) { |
876 | return error; | |
877 | } | |
1c79356b | 878 | } else { |
b0d623f7 | 879 | nfs_node_unlock(np); |
1c79356b | 880 | } |
6d2010ae | 881 | |
b0d623f7 | 882 | /* nfs_getattr() will check changed and purge caches */ |
0a7de745 A |
883 | if ((error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED))) { |
884 | return error; | |
885 | } | |
6d2010ae A |
886 | |
887 | if (vtype != VREG) { | |
888 | /* Just mark that it was opened */ | |
889 | lck_mtx_lock(&np->n_openlock); | |
890 | np->n_openrefcnt++; | |
891 | lck_mtx_unlock(&np->n_openlock); | |
0a7de745 | 892 | return 0; |
6d2010ae A |
893 | } |
894 | ||
895 | /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */ | |
896 | accessMode = 0; | |
0a7de745 | 897 | if (ap->a_mode & FREAD) { |
6d2010ae | 898 | accessMode |= NFS_OPEN_SHARE_ACCESS_READ; |
0a7de745 A |
899 | } |
900 | if (ap->a_mode & FWRITE) { | |
6d2010ae | 901 | accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE; |
0a7de745 A |
902 | } |
903 | if (ap->a_mode & O_EXLOCK) { | |
6d2010ae | 904 | denyMode = NFS_OPEN_SHARE_DENY_BOTH; |
0a7de745 | 905 | } else if (ap->a_mode & O_SHLOCK) { |
6d2010ae | 906 | denyMode = NFS_OPEN_SHARE_DENY_WRITE; |
0a7de745 | 907 | } else { |
6d2010ae | 908 | denyMode = NFS_OPEN_SHARE_DENY_NONE; |
0a7de745 | 909 | } |
6d2010ae A |
910 | // XXX don't do deny modes just yet (and never do it for !v4) |
911 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
912 | ||
913 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1); | |
0a7de745 A |
914 | if (!noop) { |
915 | return ENOMEM; | |
916 | } | |
6d2010ae A |
917 | |
918 | restart: | |
919 | error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx)); | |
920 | if (error) { | |
921 | nfs_open_owner_rele(noop); | |
0a7de745 | 922 | return error; |
6d2010ae A |
923 | } |
924 | if (np->n_flag & NREVOKE) { | |
925 | error = EIO; | |
926 | nfs_mount_state_in_use_end(nmp, 0); | |
927 | nfs_open_owner_rele(noop); | |
0a7de745 | 928 | return error; |
6d2010ae A |
929 | } |
930 | ||
931 | error = nfs_open_file_find(np, noop, &nofp, accessMode, denyMode, 1); | |
932 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) { | |
933 | NP(np, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
934 | error = EIO; | |
935 | } | |
cb323159 | 936 | #if CONFIG_NFS4 |
6d2010ae | 937 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { |
6d2010ae A |
938 | error = nfs4_reopen(nofp, vfs_context_thread(ctx)); |
939 | nofp = NULL; | |
0a7de745 | 940 | if (!error) { |
f427ee49 | 941 | nfs_mount_state_in_use_end(nmp, 0); |
6d2010ae | 942 | goto restart; |
0a7de745 | 943 | } |
6d2010ae | 944 | } |
cb323159 | 945 | #endif |
0a7de745 | 946 | if (!error) { |
6d2010ae | 947 | error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx)); |
0a7de745 | 948 | } |
6d2010ae A |
949 | if (error) { |
950 | nofp = NULL; | |
951 | goto out; | |
952 | } | |
953 | ||
954 | if (nmp->nm_vers < NFS_VER4) { | |
955 | /* | |
956 | * NFS v2/v3 opens are always allowed - so just add it. | |
957 | */ | |
958 | nfs_open_file_add_open(nofp, accessMode, denyMode, 0); | |
959 | goto out; | |
960 | } | |
961 | ||
962 | /* | |
963 | * If we just created the file and the modes match, then we simply use | |
964 | * the open performed in the create. Otherwise, send the request. | |
965 | */ | |
966 | if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
967 | (nofp->nof_creator == current_thread()) && | |
968 | (accessMode == NFS_OPEN_SHARE_ACCESS_BOTH) && | |
969 | (denyMode == NFS_OPEN_SHARE_DENY_NONE)) { | |
970 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
971 | nofp->nof_creator = NULL; | |
972 | } else { | |
cb323159 | 973 | #if CONFIG_NFS4 |
0a7de745 | 974 | if (!opened) { |
6d2010ae | 975 | error = nfs4_open(np, nofp, accessMode, denyMode, ctx); |
0a7de745 | 976 | } |
cb323159 | 977 | #endif |
6d2010ae A |
978 | if ((error == EACCES) && (nofp->nof_flags & NFS_OPEN_FILE_CREATE) && |
979 | (nofp->nof_creator == current_thread())) { | |
980 | /* | |
981 | * Ugh. This can happen if we just created the file with read-only | |
982 | * perms and we're trying to open it for real with different modes | |
983 | * (e.g. write-only or with a deny mode) and the server decides to | |
984 | * not allow the second open because of the read-only perms. | |
985 | * The best we can do is to just use the create's open. | |
986 | * We may have access we don't need or we may not have a requested | |
987 | * deny mode. We may log complaints later, but we'll try to avoid it. | |
988 | */ | |
0a7de745 | 989 | if (denyMode != NFS_OPEN_SHARE_DENY_NONE) { |
6d2010ae | 990 | NP(np, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp->nof_owner->noo_cred)); |
0a7de745 | 991 | } |
6d2010ae A |
992 | nofp->nof_creator = NULL; |
993 | error = 0; | |
994 | } | |
0a7de745 | 995 | if (error) { |
6d2010ae | 996 | goto out; |
0a7de745 | 997 | } |
6d2010ae A |
998 | opened = 1; |
999 | /* | |
1000 | * If we had just created the file, we already had it open. | |
1001 | * If the actual open mode is less than what we grabbed at | |
1002 | * create time, then we'll downgrade the open here. | |
1003 | */ | |
1004 | if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && | |
1005 | (nofp->nof_creator == current_thread())) { | |
1006 | error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_BOTH, NFS_OPEN_SHARE_DENY_NONE, ctx); | |
0a7de745 | 1007 | if (error) { |
6d2010ae | 1008 | NP(np, "nfs_vnop_open: create close error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred)); |
0a7de745 | 1009 | } |
6d2010ae A |
1010 | if (!nfs_mount_state_error_should_restart(error)) { |
1011 | error = 0; | |
1012 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
1013 | } | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | out: | |
0a7de745 | 1018 | if (nofp) { |
6d2010ae | 1019 | nfs_open_file_clear_busy(nofp); |
0a7de745 | 1020 | } |
6d2010ae A |
1021 | if (nfs_mount_state_in_use_end(nmp, error)) { |
1022 | nofp = NULL; | |
1023 | goto restart; | |
1024 | } | |
0a7de745 | 1025 | if (error) { |
6d2010ae | 1026 | NP(np, "nfs_vnop_open: error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); |
0a7de745 A |
1027 | } |
1028 | if (noop) { | |
6d2010ae | 1029 | nfs_open_owner_rele(noop); |
0a7de745 | 1030 | } |
316670eb A |
1031 | if (!error && vtype == VREG && (ap->a_mode & FWRITE)) { |
1032 | lck_mtx_lock(&nmp->nm_lock); | |
1033 | nmp->nm_state &= ~NFSSTA_SQUISHY; | |
1034 | nmp->nm_curdeadtimeout = nmp->nm_deadtimeout; | |
0a7de745 | 1035 | if (nmp->nm_curdeadtimeout <= 0) { |
316670eb | 1036 | nmp->nm_deadto_start = 0; |
0a7de745 | 1037 | } |
316670eb A |
1038 | nmp->nm_writers++; |
1039 | lck_mtx_unlock(&nmp->nm_lock); | |
1040 | } | |
0a7de745 A |
1041 | |
1042 | return error; | |
1c79356b A |
1043 | } |
1044 | ||
316670eb A |
1045 | static uint32_t |
1046 | nfs_no_of_open_file_writers(nfsnode_t np) | |
1047 | { | |
1048 | uint32_t writers = 0; | |
1049 | struct nfs_open_file *nofp; | |
1050 | ||
0a7de745 | 1051 | TAILQ_FOREACH(nofp, &np->n_opens, nof_link) { |
316670eb | 1052 | writers += nofp->nof_w + nofp->nof_rw + nofp->nof_w_dw + nofp->nof_rw_dw + |
0a7de745 A |
1053 | nofp->nof_w_drw + nofp->nof_rw_drw + nofp->nof_d_w_dw + |
1054 | nofp->nof_d_rw_dw + nofp->nof_d_w_drw + nofp->nof_d_rw_drw + | |
1055 | nofp->nof_d_w + nofp->nof_d_rw; | |
316670eb | 1056 | } |
0a7de745 A |
1057 | |
1058 | return writers; | |
316670eb | 1059 | } |
6d2010ae | 1060 | |
1c79356b | 1061 | /* |
2d21ac55 | 1062 | * NFS close vnode op |
6d2010ae | 1063 | * |
1c79356b A |
1064 | * What an NFS client should do upon close after writing is a debatable issue. |
1065 | * Most NFS clients push delayed writes to the server upon close, basically for | |
1066 | * two reasons: | |
1067 | * 1 - So that any write errors may be reported back to the client process | |
1068 | * doing the close system call. By far the two most likely errors are | |
1069 | * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. | |
1070 | * 2 - To put a worst case upper bound on cache inconsistency between | |
1071 | * multiple clients for the file. | |
1072 | * There is also a consistency problem for Version 2 of the protocol w.r.t. | |
1073 | * not being able to tell if other clients are writing a file concurrently, | |
1074 | * since there is no way of knowing if the changed modify time in the reply | |
1075 | * is only due to the write for this client. | |
1076 | * (NFS Version 3 provides weak cache consistency data in the reply that | |
1077 | * should be sufficient to detect and handle this case.) | |
1078 | * | |
1079 | * The current code does the following: | |
1080 | * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers | |
6d2010ae A |
1081 | * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them. |
1082 | * for NFS Version 4 - basically the same as NFSv3 | |
1c79356b | 1083 | */ |
2d21ac55 | 1084 | int |
6d2010ae | 1085 | nfs_vnop_close( |
91447636 | 1086 | struct vnop_close_args /* { |
0a7de745 A |
1087 | * struct vnodeop_desc *a_desc; |
1088 | * vnode_t a_vp; | |
1089 | * int a_fflag; | |
1090 | * vfs_context_t a_context; | |
1091 | * } */*ap) | |
1c79356b | 1092 | { |
2d21ac55 | 1093 | vfs_context_t ctx = ap->a_context; |
91447636 | 1094 | vnode_t vp = ap->a_vp; |
2d21ac55 | 1095 | nfsnode_t np = VTONFS(vp); |
55e303ae | 1096 | struct nfsmount *nmp; |
6d2010ae A |
1097 | int error = 0, error1, nfsvers; |
1098 | int fflag = ap->a_fflag; | |
1099 | enum vtype vtype; | |
1100 | int accessMode, denyMode; | |
1101 | struct nfs_open_owner *noop = NULL; | |
1102 | struct nfs_open_file *nofp = NULL; | |
1c79356b | 1103 | |
2d21ac55 | 1104 | nmp = VTONMP(vp); |
0a7de745 A |
1105 | if (!nmp) { |
1106 | return ENXIO; | |
1107 | } | |
2d21ac55 | 1108 | nfsvers = nmp->nm_vers; |
6d2010ae | 1109 | vtype = vnode_vtype(vp); |
91447636 | 1110 | |
6d2010ae | 1111 | /* First, check if we need to update/flush/invalidate */ |
0a7de745 | 1112 | if (ISSET(np->n_flag, NUPDATESIZE)) { |
2d21ac55 | 1113 | nfs_data_update_size(np, 0); |
0a7de745 | 1114 | } |
6d2010ae | 1115 | nfs_node_lock_force(np); |
2d21ac55 | 1116 | if (np->n_flag & NNEEDINVALIDATE) { |
91447636 | 1117 | np->n_flag &= ~NNEEDINVALIDATE; |
b0d623f7 | 1118 | nfs_node_unlock(np); |
0a7de745 | 1119 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
6d2010ae | 1120 | nfs_node_lock_force(np); |
2d21ac55 | 1121 | } |
6d2010ae A |
1122 | if ((vtype == VREG) && (np->n_flag & NMODIFIED) && (fflag & FWRITE)) { |
1123 | /* we're closing an open for write and the file is modified, so flush it */ | |
b0d623f7 | 1124 | nfs_node_unlock(np); |
0a7de745 | 1125 | if (nfsvers != NFS_VER2) { |
2d21ac55 | 1126 | error = nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), 0); |
0a7de745 | 1127 | } else { |
2d21ac55 | 1128 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
0a7de745 | 1129 | } |
b0d623f7 | 1130 | nfs_node_lock_force(np); |
91447636 | 1131 | NATTRINVALIDATE(np); |
2d21ac55 A |
1132 | } |
1133 | if (np->n_flag & NWRITEERR) { | |
1c79356b A |
1134 | np->n_flag &= ~NWRITEERR; |
1135 | error = np->n_error; | |
1c79356b | 1136 | } |
b0d623f7 | 1137 | nfs_node_unlock(np); |
6d2010ae A |
1138 | |
1139 | if (vtype != VREG) { | |
1140 | /* Just mark that it was closed */ | |
1141 | lck_mtx_lock(&np->n_openlock); | |
1142 | if (np->n_openrefcnt == 0) { | |
0a7de745 | 1143 | if (fflag & (FREAD | FWRITE)) { |
6d2010ae A |
1144 | NP(np, "nfs_vnop_close: open reference underrun"); |
1145 | error = EINVAL; | |
1146 | } | |
0a7de745 | 1147 | } else if (fflag & (FREAD | FWRITE)) { |
6d2010ae A |
1148 | np->n_openrefcnt--; |
1149 | } else { | |
1150 | /* No FREAD/FWRITE set - probably the final close */ | |
1151 | np->n_openrefcnt = 0; | |
1152 | } | |
1153 | lck_mtx_unlock(&np->n_openlock); | |
0a7de745 | 1154 | return error; |
6d2010ae A |
1155 | } |
1156 | error1 = error; | |
1157 | ||
f427ee49 | 1158 | /* fflag should contain some combination of: FREAD, FWRITE */ |
6d2010ae | 1159 | accessMode = 0; |
0a7de745 | 1160 | if (fflag & FREAD) { |
6d2010ae | 1161 | accessMode |= NFS_OPEN_SHARE_ACCESS_READ; |
0a7de745 A |
1162 | } |
1163 | if (fflag & FWRITE) { | |
6d2010ae | 1164 | accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE; |
0a7de745 | 1165 | } |
6d2010ae A |
1166 | // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open |
1167 | // if (fflag & O_EXLOCK) | |
1168 | // denyMode = NFS_OPEN_SHARE_DENY_BOTH; | |
1169 | // else if (fflag & O_SHLOCK) | |
1170 | // denyMode = NFS_OPEN_SHARE_DENY_WRITE; | |
1171 | // else | |
1172 | // denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
6d2010ae A |
1173 | // XXX don't do deny modes just yet (and never do it for !v4) |
1174 | denyMode = NFS_OPEN_SHARE_DENY_NONE; | |
1175 | ||
1176 | if (!accessMode) { | |
1177 | /* | |
1178 | * No mode given to close? | |
1179 | * Guess this is the final close. | |
1180 | * We should unlock all locks and close all opens. | |
1181 | */ | |
316670eb | 1182 | uint32_t writers; |
6d2010ae | 1183 | mount_t mp = vnode_mount(vp); |
fe8ab488 | 1184 | int force = (!mp || vfs_isforce(mp)); |
316670eb A |
1185 | |
1186 | writers = nfs_no_of_open_file_writers(np); | |
6d2010ae | 1187 | nfs_release_open_state_for_node(np, force); |
316670eb A |
1188 | if (writers) { |
1189 | lck_mtx_lock(&nmp->nm_lock); | |
1190 | if (writers > nmp->nm_writers) { | |
1191 | NP(np, "nfs_vnop_close: number of write opens for mount underrun. Node has %d" | |
0a7de745 A |
1192 | " opens for write. Mount has total of %d opens for write\n", |
1193 | writers, nmp->nm_writers); | |
316670eb A |
1194 | nmp->nm_writers = 0; |
1195 | } else { | |
1196 | nmp->nm_writers -= writers; | |
1197 | } | |
1198 | lck_mtx_unlock(&nmp->nm_lock); | |
1199 | } | |
0a7de745 A |
1200 | |
1201 | return error; | |
316670eb A |
1202 | } else if (fflag & FWRITE) { |
1203 | lck_mtx_lock(&nmp->nm_lock); | |
1204 | if (nmp->nm_writers == 0) { | |
1205 | NP(np, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing"); | |
1206 | } else { | |
1207 | nmp->nm_writers--; | |
1208 | } | |
1209 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae | 1210 | } |
0a7de745 | 1211 | |
6d2010ae A |
1212 | |
1213 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 0); | |
1214 | if (!noop) { | |
1215 | // printf("nfs_vnop_close: can't get open owner!\n"); | |
0a7de745 | 1216 | return EIO; |
6d2010ae A |
1217 | } |
1218 | ||
1219 | restart: | |
1220 | error = nfs_mount_state_in_use_start(nmp, NULL); | |
1221 | if (error) { | |
1222 | nfs_open_owner_rele(noop); | |
0a7de745 | 1223 | return error; |
6d2010ae A |
1224 | } |
1225 | ||
1226 | error = nfs_open_file_find(np, noop, &nofp, 0, 0, 0); | |
cb323159 | 1227 | #if CONFIG_NFS4 |
6d2010ae | 1228 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { |
6d2010ae A |
1229 | error = nfs4_reopen(nofp, NULL); |
1230 | nofp = NULL; | |
0a7de745 | 1231 | if (!error) { |
f427ee49 | 1232 | nfs_mount_state_in_use_end(nmp, 0); |
6d2010ae | 1233 | goto restart; |
0a7de745 | 1234 | } |
6d2010ae | 1235 | } |
cb323159 | 1236 | #endif |
6d2010ae A |
1237 | if (error) { |
1238 | NP(np, "nfs_vnop_close: no open file for owner, error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); | |
1239 | error = EBADF; | |
1240 | goto out; | |
1241 | } | |
1242 | error = nfs_open_file_set_busy(nofp, NULL); | |
1243 | if (error) { | |
1244 | nofp = NULL; | |
1245 | goto out; | |
1246 | } | |
1247 | ||
1248 | error = nfs_close(np, nofp, accessMode, denyMode, ctx); | |
0a7de745 | 1249 | if (error) { |
6d2010ae | 1250 | NP(np, "nfs_vnop_close: close error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); |
0a7de745 | 1251 | } |
6d2010ae A |
1252 | |
1253 | out: | |
0a7de745 | 1254 | if (nofp) { |
6d2010ae | 1255 | nfs_open_file_clear_busy(nofp); |
0a7de745 | 1256 | } |
6d2010ae A |
1257 | if (nfs_mount_state_in_use_end(nmp, error)) { |
1258 | nofp = NULL; | |
1259 | goto restart; | |
1260 | } | |
0a7de745 | 1261 | if (!error) { |
6d2010ae | 1262 | error = error1; |
0a7de745 A |
1263 | } |
1264 | if (error) { | |
6d2010ae | 1265 | NP(np, "nfs_vnop_close: error %d, %d", error, kauth_cred_getuid(noop->noo_cred)); |
0a7de745 A |
1266 | } |
1267 | if (noop) { | |
6d2010ae | 1268 | nfs_open_owner_rele(noop); |
0a7de745 A |
1269 | } |
1270 | return error; | |
1c79356b A |
1271 | } |
1272 | ||
6d2010ae A |
1273 | /* |
1274 | * nfs_close(): common function that does all the heavy lifting of file closure | |
1275 | * | |
1276 | * Takes an open file structure and a set of access/deny modes and figures out how | |
1277 | * to update the open file structure (and the state on the server) appropriately. | |
1278 | */ | |
1279 | int | |
1280 | nfs_close( | |
1281 | nfsnode_t np, | |
1282 | struct nfs_open_file *nofp, | |
1283 | uint32_t accessMode, | |
1284 | uint32_t denyMode, | |
c6bf4f31 | 1285 | __unused vfs_context_t ctx) |
6d2010ae | 1286 | { |
cb323159 | 1287 | #if CONFIG_NFS4 |
6d2010ae | 1288 | struct nfs_lock_owner *nlop; |
cb323159 | 1289 | #endif |
6d2010ae | 1290 | int error = 0, changed = 0, delegated = 0, closed = 0, downgrade = 0; |
f427ee49 | 1291 | uint8_t newAccessMode, newDenyMode; |
0a7de745 | 1292 | |
6d2010ae | 1293 | /* warn if modes don't match current state */ |
0a7de745 | 1294 | if (((accessMode & nofp->nof_access) != accessMode) || ((denyMode & nofp->nof_deny) != denyMode)) { |
6d2010ae | 1295 | NP(np, "nfs_close: mode mismatch %d %d, current %d %d, %d", |
0a7de745 A |
1296 | accessMode, denyMode, nofp->nof_access, nofp->nof_deny, |
1297 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1298 | } | |
6d2010ae A |
1299 | |
1300 | /* | |
1301 | * If we're closing a write-only open, we may not have a write-only count | |
1302 | * if we also grabbed read access. So, check the read-write count. | |
1303 | */ | |
1304 | if (denyMode == NFS_OPEN_SHARE_DENY_NONE) { | |
1305 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1306 | (nofp->nof_w == 0) && (nofp->nof_d_w == 0) && | |
0a7de745 | 1307 | (nofp->nof_rw || nofp->nof_d_rw)) { |
6d2010ae | 1308 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; |
0a7de745 | 1309 | } |
6d2010ae A |
1310 | } else if (denyMode == NFS_OPEN_SHARE_DENY_WRITE) { |
1311 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1312 | (nofp->nof_w_dw == 0) && (nofp->nof_d_w_dw == 0) && | |
0a7de745 | 1313 | (nofp->nof_rw_dw || nofp->nof_d_rw_dw)) { |
6d2010ae | 1314 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; |
0a7de745 | 1315 | } |
6d2010ae A |
1316 | } else { /* NFS_OPEN_SHARE_DENY_BOTH */ |
1317 | if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) && | |
1318 | (nofp->nof_w_drw == 0) && (nofp->nof_d_w_drw == 0) && | |
0a7de745 | 1319 | (nofp->nof_rw_drw || nofp->nof_d_rw_drw)) { |
6d2010ae | 1320 | accessMode = NFS_OPEN_SHARE_ACCESS_BOTH; |
0a7de745 | 1321 | } |
6d2010ae A |
1322 | } |
1323 | ||
1324 | nfs_open_file_remove_open_find(nofp, accessMode, denyMode, &newAccessMode, &newDenyMode, &delegated); | |
0a7de745 | 1325 | if ((newAccessMode != nofp->nof_access) || (newDenyMode != nofp->nof_deny)) { |
6d2010ae | 1326 | changed = 1; |
0a7de745 | 1327 | } else { |
6d2010ae | 1328 | changed = 0; |
0a7de745 | 1329 | } |
6d2010ae | 1330 | |
cb323159 A |
1331 | if (NFSTONMP(np)->nm_vers < NFS_VER4) { |
1332 | /* NFS v2/v3 closes simply need to remove the open. */ | |
6d2010ae | 1333 | goto v3close; |
0a7de745 | 1334 | } |
cb323159 | 1335 | #if CONFIG_NFS4 |
6d2010ae A |
1336 | if ((newAccessMode == 0) || (nofp->nof_opencnt == 1)) { |
1337 | /* | |
1338 | * No more access after this close, so clean up and close it. | |
1339 | * Don't send a close RPC if we're closing a delegated open. | |
1340 | */ | |
1341 | nfs_wait_bufs(np); | |
1342 | closed = 1; | |
0a7de745 | 1343 | if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) { |
6d2010ae | 1344 | error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0); |
0a7de745 | 1345 | } |
6d2010ae A |
1346 | if (error == NFSERR_LOCKS_HELD) { |
1347 | /* | |
1348 | * Hmm... the server says we have locks we need to release first | |
1349 | * Find the lock owner and try to unlock everything. | |
1350 | */ | |
1351 | nlop = nfs_lock_owner_find(np, vfs_context_proc(ctx), 0); | |
1352 | if (nlop) { | |
1353 | nfs4_unlock_rpc(np, nlop, F_WRLCK, 0, UINT64_MAX, | |
0a7de745 | 1354 | 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
6d2010ae A |
1355 | nfs_lock_owner_rele(nlop); |
1356 | } | |
1357 | error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0); | |
1358 | } | |
1359 | } else if (changed) { | |
1360 | /* | |
1361 | * File is still open but with less access, so downgrade the open. | |
1362 | * Don't send a downgrade RPC if we're closing a delegated open. | |
1363 | */ | |
1364 | if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) { | |
1365 | downgrade = 1; | |
1366 | /* | |
1367 | * If we have delegated opens, we should probably claim them before sending | |
1368 | * the downgrade because the server may not know the open we are downgrading to. | |
1369 | */ | |
1370 | if (nofp->nof_d_rw_drw || nofp->nof_d_w_drw || nofp->nof_d_r_drw || | |
1371 | nofp->nof_d_rw_dw || nofp->nof_d_w_dw || nofp->nof_d_r_dw || | |
0a7de745 | 1372 | nofp->nof_d_rw || nofp->nof_d_w || nofp->nof_d_r) { |
6d2010ae | 1373 | nfs4_claim_delegated_state_for_open_file(nofp, 0); |
0a7de745 | 1374 | } |
6d2010ae A |
1375 | /* need to remove the open before sending the downgrade */ |
1376 | nfs_open_file_remove_open(nofp, accessMode, denyMode); | |
1377 | error = nfs4_open_downgrade_rpc(np, nofp, ctx); | |
0a7de745 | 1378 | if (error) { /* Hmm.. that didn't work. Add the open back in. */ |
6d2010ae | 1379 | nfs_open_file_add_open(nofp, accessMode, denyMode, delegated); |
0a7de745 | 1380 | } |
6d2010ae A |
1381 | } |
1382 | } | |
cb323159 A |
1383 | #endif |
1384 | v3close: | |
6d2010ae A |
1385 | if (error) { |
1386 | NP(np, "nfs_close: error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
0a7de745 | 1387 | return error; |
6d2010ae A |
1388 | } |
1389 | ||
0a7de745 | 1390 | if (!downgrade) { |
6d2010ae | 1391 | nfs_open_file_remove_open(nofp, accessMode, denyMode); |
0a7de745 | 1392 | } |
6d2010ae A |
1393 | |
1394 | if (closed) { | |
1395 | lck_mtx_lock(&nofp->nof_lock); | |
1396 | if (nofp->nof_r || nofp->nof_d_r || nofp->nof_w || nofp->nof_d_w || nofp->nof_d_rw || | |
1397 | (nofp->nof_rw && !((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && !nofp->nof_creator && (nofp->nof_rw == 1))) || | |
1398 | nofp->nof_r_dw || nofp->nof_d_r_dw || nofp->nof_w_dw || nofp->nof_d_w_dw || | |
1399 | nofp->nof_rw_dw || nofp->nof_d_rw_dw || nofp->nof_r_drw || nofp->nof_d_r_drw || | |
0a7de745 | 1400 | nofp->nof_w_drw || nofp->nof_d_w_drw || nofp->nof_rw_drw || nofp->nof_d_rw_drw) { |
6d2010ae | 1401 | NP(np, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d", |
0a7de745 A |
1402 | nofp->nof_r, nofp->nof_d_r, nofp->nof_w, nofp->nof_d_w, |
1403 | nofp->nof_rw, nofp->nof_d_rw, nofp->nof_r_dw, nofp->nof_d_r_dw, | |
1404 | nofp->nof_w_dw, nofp->nof_d_w_dw, nofp->nof_rw_dw, nofp->nof_d_rw_dw, | |
1405 | nofp->nof_r_drw, nofp->nof_d_r_drw, nofp->nof_w_drw, nofp->nof_d_w_drw, | |
1406 | nofp->nof_rw_drw, nofp->nof_d_rw_drw, nofp->nof_flags, | |
1407 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); | |
1408 | } | |
6d2010ae A |
1409 | /* clear out all open info, just to be safe */ |
1410 | nofp->nof_access = nofp->nof_deny = 0; | |
1411 | nofp->nof_mmap_access = nofp->nof_mmap_deny = 0; | |
1412 | nofp->nof_r = nofp->nof_d_r = 0; | |
1413 | nofp->nof_w = nofp->nof_d_w = 0; | |
1414 | nofp->nof_rw = nofp->nof_d_rw = 0; | |
1415 | nofp->nof_r_dw = nofp->nof_d_r_dw = 0; | |
1416 | nofp->nof_w_dw = nofp->nof_d_w_dw = 0; | |
1417 | nofp->nof_rw_dw = nofp->nof_d_rw_dw = 0; | |
1418 | nofp->nof_r_drw = nofp->nof_d_r_drw = 0; | |
1419 | nofp->nof_w_drw = nofp->nof_d_w_drw = 0; | |
1420 | nofp->nof_rw_drw = nofp->nof_d_rw_drw = 0; | |
1421 | nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE; | |
1422 | lck_mtx_unlock(&nofp->nof_lock); | |
1423 | /* XXX we may potentially want to clean up idle/unused open file structures */ | |
1424 | } | |
1425 | if (nofp->nof_flags & NFS_OPEN_FILE_LOST) { | |
1426 | error = EIO; | |
1427 | NP(np, "nfs_close: LOST%s, %d", !nofp->nof_opencnt ? " (last)" : "", | |
0a7de745 | 1428 | kauth_cred_getuid(nofp->nof_owner->noo_cred)); |
6d2010ae | 1429 | } |
0a7de745 A |
1430 | |
1431 | return error; | |
6d2010ae A |
1432 | } |
1433 | ||
1434 | ||
91447636 | 1435 | int |
2d21ac55 A |
1436 | nfs3_getattr_rpc( |
1437 | nfsnode_t np, | |
91447636 A |
1438 | mount_t mp, |
1439 | u_char *fhp, | |
2d21ac55 | 1440 | size_t fhsize, |
6d2010ae | 1441 | int flags, |
2d21ac55 | 1442 | vfs_context_t ctx, |
91447636 A |
1443 | struct nfs_vattr *nvap, |
1444 | u_int64_t *xidp) | |
1445 | { | |
2d21ac55 | 1446 | struct nfsmount *nmp = mp ? VFSTONFS(mp) : NFSTONMP(np); |
f427ee49 | 1447 | int error = 0, status = 0, nfsvers, rpcflags = 0; |
2d21ac55 | 1448 | struct nfsm_chain nmreq, nmrep; |
91447636 | 1449 | |
0a7de745 A |
1450 | if (nfs_mount_gone(nmp)) { |
1451 | return ENXIO; | |
1452 | } | |
2d21ac55 A |
1453 | nfsvers = nmp->nm_vers; |
1454 | ||
0a7de745 | 1455 | if (flags & NGA_MONITOR) { /* vnode monitor requests should be soft */ |
6d2010ae | 1456 | rpcflags = R_RECOVER; |
0a7de745 | 1457 | } |
6d2010ae | 1458 | |
0a7de745 | 1459 | if (flags & NGA_SOFT) { /* Return ETIMEDOUT if server not responding */ |
fe8ab488 | 1460 | rpcflags |= R_SOFT; |
0a7de745 | 1461 | } |
fe8ab488 | 1462 | |
2d21ac55 A |
1463 | nfsm_chain_null(&nmreq); |
1464 | nfsm_chain_null(&nmrep); | |
1465 | ||
1466 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers)); | |
0a7de745 | 1467 | if (nfsvers != NFS_VER2) { |
2d21ac55 | 1468 | nfsm_chain_add_32(error, &nmreq, fhsize); |
0a7de745 | 1469 | } |
2d21ac55 A |
1470 | nfsm_chain_add_opaque(error, &nmreq, fhp, fhsize); |
1471 | nfsm_chain_build_done(error, &nmreq); | |
1472 | nfsmout_if(error); | |
0a7de745 A |
1473 | error = nfs_request2(np, mp, &nmreq, NFSPROC_GETATTR, |
1474 | vfs_context_thread(ctx), vfs_context_ucred(ctx), | |
1475 | NULL, rpcflags, &nmrep, xidp, &status); | |
1476 | if (!error) { | |
2d21ac55 | 1477 | error = status; |
0a7de745 | 1478 | } |
2d21ac55 | 1479 | nfsmout_if(error); |
cb323159 | 1480 | error = nfs_parsefattr(nmp, &nmrep, nfsvers, nvap); |
2d21ac55 A |
1481 | nfsmout: |
1482 | nfsm_chain_cleanup(&nmreq); | |
1483 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 1484 | return error; |
91447636 A |
1485 | } |
1486 | ||
fe8ab488 A |
1487 | /* |
1488 | * nfs_refresh_fh will attempt to update the file handle for the node. | |
1489 | * | |
1490 | * It only does this for symbolic links and regular files that are not currently opened. | |
1491 | * | |
1492 | * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure. | |
0a7de745 | 1493 | */ |
fe8ab488 A |
1494 | int |
1495 | nfs_refresh_fh(nfsnode_t np, vfs_context_t ctx) | |
1496 | { | |
1497 | vnode_t dvp, vp = NFSTOV(np); | |
1498 | nfsnode_t dnp; | |
1499 | const char *v_name = vnode_getname(vp); | |
1500 | char *name; | |
f427ee49 A |
1501 | int namelen, refreshed; |
1502 | uint32_t fhsize; | |
fe8ab488 A |
1503 | int error, wanted = 0; |
1504 | uint8_t *fhp; | |
cb323159 | 1505 | struct timespec ts = {.tv_sec = 2, .tv_nsec = 0}; |
fe8ab488 A |
1506 | |
1507 | NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp)); | |
1508 | ||
1509 | dvp = vnode_parent(vp); | |
1510 | if ((vnode_vtype(vp) != VREG && vnode_vtype(vp) != VLNK) || | |
1511 | v_name == NULL || *v_name == '\0' || dvp == NULL) { | |
0a7de745 | 1512 | if (v_name != NULL) { |
fe8ab488 | 1513 | vnode_putname(v_name); |
0a7de745 A |
1514 | } |
1515 | return ESTALE; | |
fe8ab488 A |
1516 | } |
1517 | dnp = VTONFS(dvp); | |
0a7de745 | 1518 | |
f427ee49 | 1519 | namelen = NFS_STRLEN_INT(v_name); |
fe8ab488 A |
1520 | MALLOC(name, char *, namelen + 1, M_TEMP, M_WAITOK); |
1521 | if (name == NULL) { | |
1522 | vnode_putname(v_name); | |
0a7de745 | 1523 | return ESTALE; |
fe8ab488 | 1524 | } |
0a7de745 | 1525 | bcopy(v_name, name, namelen + 1); |
fe8ab488 A |
1526 | NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name, name); |
1527 | vnode_putname(v_name); | |
1528 | ||
1529 | /* Allocate the maximum size file handle */ | |
f427ee49 | 1530 | MALLOC(fhp, uint8_t *, NFS4_FHSIZE, M_FHANDLE, M_WAITOK); |
fe8ab488 A |
1531 | if (fhp == NULL) { |
1532 | FREE(name, M_TEMP); | |
0a7de745 | 1533 | return ESTALE; |
fe8ab488 | 1534 | } |
0a7de745 | 1535 | |
fe8ab488 A |
1536 | if ((error = nfs_node_lock(np))) { |
1537 | FREE(name, M_TEMP); | |
f427ee49 | 1538 | FREE(fhp, M_FHANDLE); |
0a7de745 | 1539 | return ESTALE; |
fe8ab488 | 1540 | } |
0a7de745 | 1541 | |
fe8ab488 A |
1542 | fhsize = np->n_fhsize; |
1543 | bcopy(np->n_fhp, fhp, fhsize); | |
1544 | while (ISSET(np->n_flag, NREFRESH)) { | |
1545 | SET(np->n_flag, NREFRESHWANT); | |
1546 | NFS_VNOP_DBG("Waiting for refresh of %s\n", name); | |
0a7de745 A |
1547 | msleep(np, &np->n_lock, PZERO - 1, "nfsrefreshwant", &ts); |
1548 | if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) { | |
fe8ab488 | 1549 | break; |
0a7de745 | 1550 | } |
fe8ab488 A |
1551 | } |
1552 | refreshed = error ? 0 : !NFS_CMPFH(np, fhp, fhsize); | |
1553 | SET(np->n_flag, NREFRESH); | |
1554 | nfs_node_unlock(np); | |
1555 | ||
1556 | NFS_VNOP_DBG("error = %d, refreshed = %d\n", error, refreshed); | |
0a7de745 | 1557 | if (error || refreshed) { |
fe8ab488 | 1558 | goto nfsmout; |
0a7de745 A |
1559 | } |
1560 | ||
fe8ab488 A |
1561 | /* Check that there are no open references for this file */ |
1562 | lck_mtx_lock(&np->n_openlock); | |
1563 | if (np->n_openrefcnt || !TAILQ_EMPTY(&np->n_opens) || !TAILQ_EMPTY(&np->n_lock_owners)) { | |
1564 | int cnt = 0; | |
1565 | struct nfs_open_file *ofp; | |
0a7de745 | 1566 | |
fe8ab488 A |
1567 | TAILQ_FOREACH(ofp, &np->n_opens, nof_link) { |
1568 | cnt += ofp->nof_opencnt; | |
1569 | } | |
1570 | if (cnt) { | |
1571 | lck_mtx_unlock(&np->n_openlock); | |
1572 | NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name); | |
0a7de745 A |
1573 | NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n", |
1574 | np->n_openrefcnt, cnt, !TAILQ_EMPTY(&np->n_lock_owners)); | |
fe8ab488 A |
1575 | error = ESTALE; |
1576 | goto nfsmout; | |
1577 | } | |
1578 | } | |
1579 | lck_mtx_unlock(&np->n_openlock); | |
0a7de745 | 1580 | /* |
fe8ab488 A |
1581 | * Since the FH is currently stale we should not be able to |
1582 | * establish any open state until the FH is refreshed. | |
1583 | */ | |
1584 | ||
1585 | error = nfs_node_lock(np); | |
1586 | nfsmout_if(error); | |
1587 | /* | |
1588 | * Symlinks should never need invalidations and are holding | |
1589 | * the one and only nfsbuf in an uncached acquired state | |
1590 | * trying to do a readlink. So we will hang if we invalidate | |
1591 | * in that case. Only in in the VREG case do we need to | |
1592 | * invalidate. | |
1593 | */ | |
1594 | if (vnode_vtype(vp) == VREG) { | |
1595 | np->n_flag &= ~NNEEDINVALIDATE; | |
1596 | nfs_node_unlock(np); | |
1597 | error = nfs_vinvalbuf(vp, V_IGNORE_WRITEERR, ctx, 1); | |
0a7de745 | 1598 | if (error) { |
fe8ab488 | 1599 | NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error); |
0a7de745 | 1600 | } |
fe8ab488 A |
1601 | nfsmout_if(error); |
1602 | } else { | |
1603 | nfs_node_unlock(np); | |
1604 | } | |
1605 | ||
1606 | NFS_VNOP_DBG("Looking up %s\n", name); | |
1607 | error = nfs_lookitup(dnp, name, namelen, ctx, &np); | |
0a7de745 | 1608 | if (error) { |
fe8ab488 | 1609 | NFS_VNOP_DBG("nfs_lookitup returned %d\n", error); |
0a7de745 | 1610 | } |
fe8ab488 A |
1611 | |
1612 | nfsmout: | |
1613 | nfs_node_lock_force(np); | |
1614 | wanted = ISSET(np->n_flag, NREFRESHWANT); | |
0a7de745 | 1615 | CLR(np->n_flag, NREFRESH | NREFRESHWANT); |
fe8ab488 | 1616 | nfs_node_unlock(np); |
0a7de745 | 1617 | if (wanted) { |
fe8ab488 | 1618 | wakeup(np); |
0a7de745 A |
1619 | } |
1620 | ||
1621 | if (error == 0) { | |
fe8ab488 | 1622 | NFS_VNOP_DBG("%s refreshed file handle\n", name); |
0a7de745 | 1623 | } |
fe8ab488 A |
1624 | |
1625 | FREE(name, M_TEMP); | |
f427ee49 | 1626 | FREE(fhp, M_FHANDLE); |
0a7de745 A |
1627 | |
1628 | return error ? ESTALE : 0; | |
fe8ab488 | 1629 | } |
2d21ac55 | 1630 | |
91447636 | 1631 | int |
6d2010ae | 1632 | nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags) |
fe8ab488 A |
1633 | { |
1634 | int error; | |
0a7de745 | 1635 | |
fe8ab488 A |
1636 | retry: |
1637 | error = nfs_getattr_internal(np, nvap, ctx, flags); | |
1638 | if (error == ESTALE) { | |
1639 | error = nfs_refresh_fh(np, ctx); | |
0a7de745 | 1640 | if (!error) { |
fe8ab488 | 1641 | goto retry; |
0a7de745 | 1642 | } |
fe8ab488 | 1643 | } |
0a7de745 | 1644 | return error; |
fe8ab488 A |
1645 | } |
1646 | ||
1647 | int | |
1648 | nfs_getattr_internal(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags) | |
1c79356b | 1649 | { |
2d21ac55 | 1650 | struct nfsmount *nmp; |
f427ee49 A |
1651 | int error = 0, nfsvers, inprogset = 0, wanted = 0, avoidfloods = 0; |
1652 | struct nfs_vattr *nvattr = NULL; | |
cb323159 | 1653 | struct timespec ts = { .tv_sec = 2, .tv_nsec = 0 }; |
f427ee49 | 1654 | u_int64_t xid = 0; |
91447636 A |
1655 | |
1656 | FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag); | |
1657 | ||
fe8ab488 | 1658 | nmp = NFSTONMP(np); |
0a7de745 A |
1659 | |
1660 | if (nfs_mount_gone(nmp)) { | |
1661 | return ENXIO; | |
1662 | } | |
b0d623f7 A |
1663 | nfsvers = nmp->nm_vers; |
1664 | ||
0a7de745 | 1665 | if (!nvap) { |
f427ee49 A |
1666 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); |
1667 | nvap = nvattr; | |
0a7de745 | 1668 | } |
6d2010ae A |
1669 | NVATTR_INIT(nvap); |
1670 | ||
2d21ac55 A |
1671 | /* Update local times for special files. */ |
1672 | if (np->n_flag & (NACC | NUPD)) { | |
b0d623f7 | 1673 | nfs_node_lock_force(np); |
1c79356b | 1674 | np->n_flag |= NCHG; |
b0d623f7 | 1675 | nfs_node_unlock(np); |
2d21ac55 A |
1676 | } |
1677 | /* Update size, if necessary */ | |
0a7de745 | 1678 | if (ISSET(np->n_flag, NUPDATESIZE)) { |
2d21ac55 | 1679 | nfs_data_update_size(np, 0); |
0a7de745 | 1680 | } |
2d21ac55 | 1681 | |
b0d623f7 A |
1682 | error = nfs_node_lock(np); |
1683 | nfsmout_if(error); | |
0a7de745 | 1684 | if (!(flags & (NGA_UNCACHED | NGA_MONITOR)) || ((nfsvers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))) { |
6d2010ae A |
1685 | /* |
1686 | * Use the cache or wait for any getattr in progress if: | |
1687 | * - it's a cached request, or | |
fe8ab488 A |
1688 | * - we have a delegation, or |
1689 | * - the server isn't responding | |
6d2010ae | 1690 | */ |
b0d623f7 | 1691 | while (1) { |
6d2010ae | 1692 | error = nfs_getattrcache(np, nvap, flags); |
b0d623f7 A |
1693 | if (!error || (error != ENOENT)) { |
1694 | nfs_node_unlock(np); | |
1695 | goto nfsmout; | |
1696 | } | |
6d2010ae | 1697 | error = 0; |
0a7de745 | 1698 | if (!ISSET(np->n_flag, NGETATTRINPROG)) { |
b0d623f7 | 1699 | break; |
0a7de745 | 1700 | } |
6d2010ae A |
1701 | if (flags & NGA_MONITOR) { |
1702 | /* no need to wait if a request is pending */ | |
1703 | error = EINPROGRESS; | |
1704 | nfs_node_unlock(np); | |
1705 | goto nfsmout; | |
1706 | } | |
b0d623f7 | 1707 | SET(np->n_flag, NGETATTRWANT); |
0a7de745 | 1708 | msleep(np, &np->n_lock, PZERO - 1, "nfsgetattrwant", &ts); |
b0d623f7 A |
1709 | if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) { |
1710 | nfs_node_unlock(np); | |
1711 | goto nfsmout; | |
1712 | } | |
1713 | } | |
1714 | SET(np->n_flag, NGETATTRINPROG); | |
1715 | inprogset = 1; | |
1716 | } else if (!ISSET(np->n_flag, NGETATTRINPROG)) { | |
1717 | SET(np->n_flag, NGETATTRINPROG); | |
1718 | inprogset = 1; | |
6d2010ae A |
1719 | } else if (flags & NGA_MONITOR) { |
1720 | /* no need to make a request if one is pending */ | |
1721 | error = EINPROGRESS; | |
b0d623f7 A |
1722 | } |
1723 | nfs_node_unlock(np); | |
55e303ae | 1724 | |
2d21ac55 | 1725 | nmp = NFSTONMP(np); |
0a7de745 | 1726 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 1727 | error = ENXIO; |
0a7de745 A |
1728 | } |
1729 | if (error) { | |
2d21ac55 | 1730 | goto nfsmout; |
0a7de745 | 1731 | } |
fa4905b1 | 1732 | |
fe8ab488 A |
1733 | /* |
1734 | * Return cached attributes if they are valid, | |
1735 | * if the server doesn't respond, and this is | |
1736 | * some softened up style of mount. | |
1737 | */ | |
0a7de745 | 1738 | if (NATTRVALID(np) && nfs_use_cache(nmp)) { |
fe8ab488 | 1739 | flags |= NGA_SOFT; |
0a7de745 | 1740 | } |
fe8ab488 | 1741 | |
91447636 | 1742 | /* |
6d2010ae A |
1743 | * We might want to try to get both the attributes and access info by |
1744 | * making an ACCESS call and seeing if it returns updated attributes. | |
91447636 A |
1745 | * But don't bother if we aren't caching access info or if the |
1746 | * attributes returned wouldn't be cached. | |
1747 | */ | |
6d2010ae | 1748 | if (!(flags & NGA_ACL) && (nfsvers != NFS_VER2) && nfs_access_for_getattr && (nfs_access_cache_timeout > 0)) { |
2d21ac55 | 1749 | if (nfs_attrcachetimeout(np) > 0) { |
b0d623f7 | 1750 | /* OSAddAtomic(1, &nfsstats.accesscache_misses); */ |
6d2010ae | 1751 | u_int32_t access = NFS_ACCESS_ALL; |
fe8ab488 A |
1752 | int rpcflags = 0; |
1753 | ||
1754 | /* Return cached attrs if server doesn't respond */ | |
0a7de745 | 1755 | if (flags & NGA_SOFT) { |
fe8ab488 | 1756 | rpcflags |= R_SOFT; |
0a7de745 | 1757 | } |
fe8ab488 A |
1758 | |
1759 | error = nmp->nm_funcs->nf_access_rpc(np, &access, rpcflags, ctx); | |
1760 | ||
0a7de745 | 1761 | if (error == ETIMEDOUT) { |
fe8ab488 | 1762 | goto returncached; |
0a7de745 | 1763 | } |
fe8ab488 | 1764 | |
0a7de745 | 1765 | if (error) { |
2d21ac55 | 1766 | goto nfsmout; |
0a7de745 | 1767 | } |
b0d623f7 | 1768 | nfs_node_lock_force(np); |
6d2010ae | 1769 | error = nfs_getattrcache(np, nvap, flags); |
b0d623f7 | 1770 | nfs_node_unlock(np); |
0a7de745 | 1771 | if (!error || (error != ENOENT)) { |
2d21ac55 | 1772 | goto nfsmout; |
0a7de745 | 1773 | } |
b0d623f7 | 1774 | /* Well, that didn't work... just do a getattr... */ |
2d21ac55 A |
1775 | error = 0; |
1776 | } | |
1c79356b | 1777 | } |
b0d623f7 | 1778 | |
fa4905b1 | 1779 | avoidfloods = 0; |
fe8ab488 | 1780 | |
fa4905b1 | 1781 | tryagain: |
6d2010ae | 1782 | error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, flags, ctx, nvap, &xid); |
b0d623f7 A |
1783 | if (!error) { |
1784 | nfs_node_lock_force(np); | |
1785 | error = nfs_loadattrcache(np, nvap, &xid, 0); | |
1786 | nfs_node_unlock(np); | |
1787 | } | |
fe8ab488 A |
1788 | |
1789 | /* | |
1790 | * If the server didn't respond, return cached attributes. | |
1791 | */ | |
1792 | returncached: | |
1793 | if ((flags & NGA_SOFT) && (error == ETIMEDOUT)) { | |
1794 | nfs_node_lock_force(np); | |
1795 | error = nfs_getattrcache(np, nvap, flags); | |
1796 | if (!error || (error != ENOENT)) { | |
1797 | nfs_node_unlock(np); | |
1798 | goto nfsmout; | |
1799 | } | |
1800 | nfs_node_unlock(np); | |
1801 | } | |
2d21ac55 | 1802 | nfsmout_if(error); |
fe8ab488 | 1803 | |
2d21ac55 A |
1804 | if (!xid) { /* out-of-order rpc - attributes were dropped */ |
1805 | FSDBG(513, -1, np, np->n_xid >> 32, np->n_xid); | |
0a7de745 | 1806 | if (avoidfloods++ < 20) { |
2d21ac55 | 1807 | goto tryagain; |
0a7de745 | 1808 | } |
b0d623f7 A |
1809 | /* avoidfloods>1 is bizarre. at 20 pull the plug */ |
1810 | /* just return the last attributes we got */ | |
1811 | } | |
1812 | nfsmout: | |
1813 | nfs_node_lock_force(np); | |
1814 | if (inprogset) { | |
1815 | wanted = ISSET(np->n_flag, NGETATTRWANT); | |
1816 | CLR(np->n_flag, (NGETATTRINPROG | NGETATTRWANT)); | |
91447636 | 1817 | } |
b0d623f7 A |
1818 | if (!error) { |
1819 | /* check if the node changed on us */ | |
2d21ac55 A |
1820 | vnode_t vp = NFSTOV(np); |
1821 | enum vtype vtype = vnode_vtype(vp); | |
b0d623f7 A |
1822 | if ((vtype == VDIR) && NFS_CHANGED_NC(nfsvers, np, nvap)) { |
1823 | FSDBG(513, -1, np, 0, np); | |
1824 | np->n_flag &= ~NNEGNCENTRIES; | |
1825 | cache_purge(vp); | |
1826 | np->n_ncgen++; | |
1827 | NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap); | |
0a7de745 A |
1828 | NFS_VNOP_DBG("Purge directory 0x%llx\n", |
1829 | (uint64_t)VM_KERNEL_ADDRPERM(vp)); | |
2d21ac55 | 1830 | } |
b0d623f7 A |
1831 | if (NFS_CHANGED(nfsvers, np, nvap)) { |
1832 | FSDBG(513, -1, np, -1, np); | |
39236c6e | 1833 | if (vtype == VDIR) { |
0a7de745 A |
1834 | NFS_VNOP_DBG("Invalidate directory 0x%llx\n", |
1835 | (uint64_t)VM_KERNEL_ADDRPERM(vp)); | |
b0d623f7 | 1836 | nfs_invaldir(np); |
39236c6e | 1837 | } |
b0d623f7 | 1838 | nfs_node_unlock(np); |
0a7de745 | 1839 | if (wanted) { |
b0d623f7 | 1840 | wakeup(np); |
0a7de745 | 1841 | } |
2d21ac55 | 1842 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
fa4905b1 | 1843 | FSDBG(513, -1, np, -2, error); |
55e303ae | 1844 | if (!error) { |
b0d623f7 | 1845 | nfs_node_lock_force(np); |
2d21ac55 | 1846 | NFS_CHANGED_UPDATE(nfsvers, np, nvap); |
b0d623f7 | 1847 | nfs_node_unlock(np); |
55e303ae | 1848 | } |
2d21ac55 | 1849 | } else { |
b0d623f7 | 1850 | nfs_node_unlock(np); |
0a7de745 | 1851 | if (wanted) { |
b0d623f7 | 1852 | wakeup(np); |
0a7de745 | 1853 | } |
1c79356b | 1854 | } |
b0d623f7 A |
1855 | } else { |
1856 | nfs_node_unlock(np); | |
0a7de745 | 1857 | if (wanted) { |
b0d623f7 | 1858 | wakeup(np); |
0a7de745 | 1859 | } |
1c79356b | 1860 | } |
6d2010ae | 1861 | |
f427ee49 | 1862 | if (nvattr != NULL) { |
6d2010ae | 1863 | NVATTR_CLEANUP(nvap); |
f427ee49 | 1864 | FREE(nvattr, M_TEMP); |
6d2010ae A |
1865 | } else if (!(flags & NGA_ACL)) { |
1866 | /* make sure we don't return an ACL if it wasn't asked for */ | |
1867 | NFS_BITMAP_CLR(nvap->nva_bitmap, NFS_FATTR_ACL); | |
1868 | if (nvap->nva_acl) { | |
1869 | kauth_acl_free(nvap->nva_acl); | |
1870 | nvap->nva_acl = NULL; | |
1871 | } | |
1872 | } | |
2d21ac55 | 1873 | FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag); |
0a7de745 | 1874 | return error; |
91447636 A |
1875 | } |
1876 | ||
cb323159 | 1877 | |
2d21ac55 A |
1878 | /* |
1879 | * NFS getattr call from vfs. | |
1880 | */ | |
39236c6e A |
1881 | |
1882 | /* | |
1883 | * The attributes we support over the wire. | |
0a7de745 | 1884 | * We also get fsid but the vfs layer gets it out of the mount |
39236c6e A |
1885 | * structure after this calling us so there's no need to return it, |
1886 | * and Finder expects to call getattrlist just looking for the FSID | |
1887 | * with out hanging on a non responsive server. | |
1888 | */ | |
1889 | #define NFS3_SUPPORTED_VATTRS \ | |
0a7de745 A |
1890 | (VNODE_ATTR_va_rdev | \ |
1891 | VNODE_ATTR_va_nlink | \ | |
1892 | VNODE_ATTR_va_data_size | \ | |
1893 | VNODE_ATTR_va_data_alloc | \ | |
1894 | VNODE_ATTR_va_uid | \ | |
1895 | VNODE_ATTR_va_gid | \ | |
1896 | VNODE_ATTR_va_mode | \ | |
1897 | VNODE_ATTR_va_modify_time | \ | |
1898 | VNODE_ATTR_va_change_time | \ | |
1899 | VNODE_ATTR_va_access_time | \ | |
1900 | VNODE_ATTR_va_fileid | \ | |
39236c6e A |
1901 | VNODE_ATTR_va_type) |
1902 | ||
cb323159 | 1903 | |
b0d623f7 | 1904 | int |
2d21ac55 | 1905 | nfs3_vnop_getattr( |
91447636 | 1906 | struct vnop_getattr_args /* { |
0a7de745 A |
1907 | * struct vnodeop_desc *a_desc; |
1908 | * vnode_t a_vp; | |
1909 | * struct vnode_attr *a_vap; | |
1910 | * vfs_context_t a_context; | |
1911 | * } */*ap) | |
91447636 A |
1912 | { |
1913 | int error; | |
ea3f0419 A |
1914 | nfsnode_t np; |
1915 | uint64_t supported_attrs; | |
f427ee49 | 1916 | struct nfs_vattr *nva; |
91447636 | 1917 | struct vnode_attr *vap = ap->a_vap; |
5ba3f43e | 1918 | struct nfsmount *nmp; |
2d21ac55 | 1919 | dev_t rdev; |
91447636 | 1920 | |
cb323159 A |
1921 | nmp = VTONMP(ap->a_vp); |
1922 | ||
39236c6e A |
1923 | /* |
1924 | * Lets don't go over the wire if we don't support any of the attributes. | |
1925 | * Just fall through at the VFS layer and let it cons up what it needs. | |
1926 | */ | |
1927 | /* Return the io size no matter what, since we don't go over the wire for this */ | |
1928 | VATTR_RETURN(vap, va_iosize, nfs_iosize); | |
cb323159 | 1929 | |
ea3f0419 A |
1930 | supported_attrs = NFS3_SUPPORTED_VATTRS; |
1931 | ||
1932 | if ((vap->va_active & supported_attrs) == 0) { | |
0a7de745 A |
1933 | return 0; |
1934 | } | |
39236c6e | 1935 | |
0a7de745 A |
1936 | if (VATTR_IS_ACTIVE(ap->a_vap, va_name)) { |
1937 | NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n", | |
1938 | (uint64_t)VM_KERNEL_ADDRPERM(ap->a_vp), | |
1939 | ap->a_vp->v_name ? ap->a_vp->v_name : "empty"); | |
1940 | } | |
ea3f0419 A |
1941 | |
1942 | /* | |
1943 | * We should not go over the wire if only fileid was requested and has ever been populated. | |
1944 | */ | |
1945 | if ((vap->va_active & supported_attrs) == VNODE_ATTR_va_fileid) { | |
1946 | np = VTONFS(ap->a_vp); | |
1947 | if (np->n_attrstamp) { | |
1948 | VATTR_RETURN(vap, va_fileid, np->n_vattr.nva_fileid); | |
1949 | return 0; | |
1950 | } | |
1951 | } | |
1952 | ||
f427ee49 A |
1953 | MALLOC(nva, struct nfs_vattr *, sizeof(*nva), M_TEMP, M_WAITOK); |
1954 | error = nfs_getattr(VTONFS(ap->a_vp), nva, ap->a_context, NGA_CACHED); | |
0a7de745 | 1955 | if (error) { |
f427ee49 | 1956 | goto out; |
0a7de745 | 1957 | } |
91447636 A |
1958 | |
1959 | /* copy nva to *a_vap */ | |
f427ee49 A |
1960 | VATTR_RETURN(vap, va_type, nva->nva_type); |
1961 | VATTR_RETURN(vap, va_mode, nva->nva_mode); | |
1962 | rdev = makedev(nva->nva_rawdev.specdata1, nva->nva_rawdev.specdata2); | |
2d21ac55 | 1963 | VATTR_RETURN(vap, va_rdev, rdev); |
f427ee49 A |
1964 | VATTR_RETURN(vap, va_uid, nva->nva_uid); |
1965 | VATTR_RETURN(vap, va_gid, nva->nva_gid); | |
1966 | VATTR_RETURN(vap, va_nlink, nva->nva_nlink); | |
1967 | VATTR_RETURN(vap, va_fileid, nva->nva_fileid); | |
1968 | VATTR_RETURN(vap, va_data_size, nva->nva_size); | |
1969 | VATTR_RETURN(vap, va_data_alloc, nva->nva_bytes); | |
1970 | vap->va_access_time.tv_sec = nva->nva_timesec[NFSTIME_ACCESS]; | |
1971 | vap->va_access_time.tv_nsec = nva->nva_timensec[NFSTIME_ACCESS]; | |
2d21ac55 | 1972 | VATTR_SET_SUPPORTED(vap, va_access_time); |
f427ee49 A |
1973 | vap->va_modify_time.tv_sec = nva->nva_timesec[NFSTIME_MODIFY]; |
1974 | vap->va_modify_time.tv_nsec = nva->nva_timensec[NFSTIME_MODIFY]; | |
2d21ac55 | 1975 | VATTR_SET_SUPPORTED(vap, va_modify_time); |
f427ee49 A |
1976 | vap->va_change_time.tv_sec = nva->nva_timesec[NFSTIME_CHANGE]; |
1977 | vap->va_change_time.tv_nsec = nva->nva_timensec[NFSTIME_CHANGE]; | |
2d21ac55 | 1978 | VATTR_SET_SUPPORTED(vap, va_change_time); |
91447636 | 1979 | |
cb323159 | 1980 | |
2d21ac55 | 1981 | // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */); |
f427ee49 A |
1982 | out: |
1983 | FREE(nva, M_TEMP); | |
0a7de745 | 1984 | return error; |
1c79356b A |
1985 | } |
1986 | ||
1987 | /* | |
2d21ac55 | 1988 | * NFS setattr call. |
1c79356b | 1989 | */ |
b0d623f7 | 1990 | int |
2d21ac55 | 1991 | nfs_vnop_setattr( |
91447636 | 1992 | struct vnop_setattr_args /* { |
0a7de745 A |
1993 | * struct vnodeop_desc *a_desc; |
1994 | * vnode_t a_vp; | |
1995 | * struct vnode_attr *a_vap; | |
1996 | * vfs_context_t a_context; | |
1997 | * } */*ap) | |
1c79356b | 1998 | { |
2d21ac55 | 1999 | vfs_context_t ctx = ap->a_context; |
91447636 | 2000 | vnode_t vp = ap->a_vp; |
2d21ac55 | 2001 | nfsnode_t np = VTONFS(vp); |
0c530ab8 | 2002 | struct nfsmount *nmp; |
91447636 | 2003 | struct vnode_attr *vap = ap->a_vap; |
1c79356b | 2004 | int error = 0; |
6d2010ae A |
2005 | int biosize, nfsvers, namedattrs; |
2006 | u_quad_t origsize, vapsize; | |
f427ee49 | 2007 | struct nfs_dulookup *dul; |
2d21ac55 | 2008 | nfsnode_t dnp = NULL; |
813fb2f6 | 2009 | int dul_in_progress = 0; |
2d21ac55 A |
2010 | vnode_t dvp = NULL; |
2011 | const char *vname = NULL; | |
cb323159 | 2012 | #if CONFIG_NFS4 |
b0d623f7 A |
2013 | struct nfs_open_owner *noop = NULL; |
2014 | struct nfs_open_file *nofp = NULL; | |
cb323159 | 2015 | #endif |
2d21ac55 | 2016 | nmp = VTONMP(vp); |
0a7de745 A |
2017 | if (nfs_mount_gone(nmp)) { |
2018 | return ENXIO; | |
2019 | } | |
2d21ac55 | 2020 | nfsvers = nmp->nm_vers; |
6d2010ae | 2021 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
0c530ab8 | 2022 | biosize = nmp->nm_biosize; |
fa4905b1 | 2023 | |
91447636 | 2024 | /* Disallow write attempts if the filesystem is mounted read-only. */ |
0a7de745 A |
2025 | if (vnode_vfsisrdonly(vp)) { |
2026 | return EROFS; | |
2027 | } | |
91447636 | 2028 | |
2d21ac55 | 2029 | origsize = np->n_size; |
91447636 | 2030 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { |
2d21ac55 A |
2031 | switch (vnode_vtype(vp)) { |
2032 | case VDIR: | |
0a7de745 | 2033 | return EISDIR; |
2d21ac55 A |
2034 | case VCHR: |
2035 | case VBLK: | |
2036 | case VSOCK: | |
2037 | case VFIFO: | |
91447636 A |
2038 | if (!VATTR_IS_ACTIVE(vap, va_modify_time) && |
2039 | !VATTR_IS_ACTIVE(vap, va_access_time) && | |
2040 | !VATTR_IS_ACTIVE(vap, va_mode) && | |
2041 | !VATTR_IS_ACTIVE(vap, va_uid) && | |
2d21ac55 | 2042 | !VATTR_IS_ACTIVE(vap, va_gid)) { |
0a7de745 | 2043 | return 0; |
2d21ac55 | 2044 | } |
91447636 | 2045 | VATTR_CLEAR_ACTIVE(vap, va_data_size); |
2d21ac55 A |
2046 | break; |
2047 | default: | |
1c79356b A |
2048 | /* |
2049 | * Disallow write attempts if the filesystem is | |
2050 | * mounted read-only. | |
2051 | */ | |
0a7de745 A |
2052 | if (vnode_vfsisrdonly(vp)) { |
2053 | return EROFS; | |
2054 | } | |
91447636 | 2055 | FSDBG_TOP(512, np->n_size, vap->va_data_size, |
0a7de745 | 2056 | np->n_vattr.nva_size, np->n_flag); |
2d21ac55 | 2057 | /* clear NNEEDINVALIDATE, if set */ |
0a7de745 A |
2058 | if ((error = nfs_node_lock(np))) { |
2059 | return error; | |
2060 | } | |
2061 | if (np->n_flag & NNEEDINVALIDATE) { | |
2d21ac55 | 2062 | np->n_flag &= ~NNEEDINVALIDATE; |
0a7de745 | 2063 | } |
b0d623f7 | 2064 | nfs_node_unlock(np); |
2d21ac55 | 2065 | /* flush everything */ |
0a7de745 | 2066 | error = nfs_vinvalbuf(vp, (vap->va_data_size ? V_SAVE : 0), ctx, 1); |
2d21ac55 | 2067 | if (error) { |
6d2010ae | 2068 | NP(np, "nfs_setattr: nfs_vinvalbuf %d", error); |
2d21ac55 | 2069 | FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, -1); |
0a7de745 | 2070 | return error; |
2d21ac55 | 2071 | } |
cb323159 | 2072 | #if CONFIG_NFS4 |
b0d623f7 A |
2073 | if (nfsvers >= NFS_VER4) { |
2074 | /* setting file size requires having the file open for write access */ | |
0a7de745 A |
2075 | if (np->n_flag & NREVOKE) { |
2076 | return EIO; | |
2077 | } | |
b0d623f7 | 2078 | noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1); |
0a7de745 A |
2079 | if (!noop) { |
2080 | return ENOMEM; | |
2081 | } | |
6d2010ae A |
2082 | restart: |
2083 | error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx)); | |
0a7de745 A |
2084 | if (error) { |
2085 | return error; | |
2086 | } | |
6d2010ae A |
2087 | if (np->n_flag & NREVOKE) { |
2088 | nfs_mount_state_in_use_end(nmp, 0); | |
0a7de745 | 2089 | return EIO; |
6d2010ae | 2090 | } |
b0d623f7 | 2091 | error = nfs_open_file_find(np, noop, &nofp, 0, 0, 1); |
0a7de745 | 2092 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) { |
b0d623f7 | 2093 | error = EIO; |
0a7de745 | 2094 | } |
b0d623f7 | 2095 | if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) { |
6d2010ae | 2096 | error = nfs4_reopen(nofp, vfs_context_thread(ctx)); |
b0d623f7 | 2097 | nofp = NULL; |
0a7de745 | 2098 | if (!error) { |
f427ee49 | 2099 | nfs_mount_state_in_use_end(nmp, 0); |
6d2010ae | 2100 | goto restart; |
0a7de745 | 2101 | } |
b0d623f7 | 2102 | } |
0a7de745 | 2103 | if (!error) { |
6d2010ae | 2104 | error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx)); |
0a7de745 | 2105 | } |
b0d623f7 | 2106 | if (error) { |
f427ee49 | 2107 | nfs_mount_state_in_use_end(nmp, 0); |
b0d623f7 | 2108 | nfs_open_owner_rele(noop); |
0a7de745 | 2109 | return error; |
b0d623f7 A |
2110 | } |
2111 | if (!(nofp->nof_access & NFS_OPEN_SHARE_ACCESS_WRITE)) { | |
2112 | /* we don't have the file open for write access, so open it */ | |
b0d623f7 | 2113 | error = nfs4_open(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx); |
0a7de745 | 2114 | if (!error) { |
b0d623f7 | 2115 | nofp->nof_flags |= NFS_OPEN_FILE_SETATTR; |
0a7de745 | 2116 | } |
b0d623f7 A |
2117 | if (nfs_mount_state_error_should_restart(error)) { |
2118 | nfs_open_file_clear_busy(nofp); | |
2119 | nofp = NULL; | |
f427ee49 A |
2120 | nfs_mount_state_in_use_end(nmp, error); |
2121 | goto restart; | |
b0d623f7 | 2122 | } |
b0d623f7 A |
2123 | } |
2124 | } | |
cb323159 | 2125 | #endif |
b0d623f7 | 2126 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
2d21ac55 | 2127 | if (np->n_size > vap->va_data_size) { /* shrinking? */ |
91447636 | 2128 | daddr64_t obn, bn; |
f427ee49 A |
2129 | int mustwrite; |
2130 | off_t neweofoff; | |
55e303ae | 2131 | struct nfsbuf *bp; |
f427ee49 | 2132 | nfsbufpgs pagemask; |
fa4905b1 | 2133 | |
fa4905b1 | 2134 | obn = (np->n_size - 1) / biosize; |
2d21ac55 | 2135 | bn = vap->va_data_size / biosize; |
0a7de745 A |
2136 | for (; obn >= bn; obn--) { |
2137 | if (!nfs_buf_is_incore(np, obn)) { | |
91447636 | 2138 | continue; |
0a7de745 | 2139 | } |
2d21ac55 | 2140 | error = nfs_buf_get(np, obn, biosize, NULL, NBLK_READ, &bp); |
0a7de745 | 2141 | if (error) { |
55e303ae | 2142 | continue; |
0a7de745 | 2143 | } |
91447636 A |
2144 | if (obn != bn) { |
2145 | FSDBG(512, bp, bp->nb_flags, 0, obn); | |
2146 | SET(bp->nb_flags, NB_INVAL); | |
2147 | nfs_buf_release(bp, 1); | |
2148 | continue; | |
2149 | } | |
2150 | mustwrite = 0; | |
2151 | neweofoff = vap->va_data_size - NBOFF(bp); | |
2152 | /* check for any dirty data before the new EOF */ | |
2d21ac55 | 2153 | if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) { |
55e303ae | 2154 | /* clip dirty range to EOF */ |
2d21ac55 | 2155 | if (bp->nb_dirtyend > neweofoff) { |
91447636 | 2156 | bp->nb_dirtyend = neweofoff; |
0a7de745 | 2157 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) { |
2d21ac55 | 2158 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; |
0a7de745 | 2159 | } |
2d21ac55 | 2160 | } |
0a7de745 | 2161 | if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) { |
2d21ac55 | 2162 | mustwrite++; |
0a7de745 | 2163 | } |
91447636 | 2164 | } |
f427ee49 A |
2165 | nfs_buf_pgs_get_page_mask(&pagemask, round_page_64(neweofoff) / PAGE_SIZE); |
2166 | nfs_buf_pgs_bit_and(&bp->nb_dirty, &pagemask, &bp->nb_dirty); | |
2167 | if (nfs_buf_pgs_is_set(&bp->nb_dirty)) { | |
55e303ae | 2168 | mustwrite++; |
0a7de745 | 2169 | } |
91447636 A |
2170 | if (!mustwrite) { |
2171 | FSDBG(512, bp, bp->nb_flags, 0, obn); | |
2172 | SET(bp->nb_flags, NB_INVAL); | |
2173 | nfs_buf_release(bp, 1); | |
2174 | continue; | |
2175 | } | |
2176 | /* gotta write out dirty data before invalidating */ | |
2177 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
2178 | /* (NB_NOCACHE indicates buffer should be discarded) */ | |
2179 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL | NB_ASYNC | NB_READ)); | |
2180 | SET(bp->nb_flags, NB_STABLE | NB_NOCACHE); | |
0c530ab8 | 2181 | if (!IS_VALID_CRED(bp->nb_wcred)) { |
2d21ac55 | 2182 | kauth_cred_t cred = vfs_context_ucred(ctx); |
91447636 A |
2183 | kauth_cred_ref(cred); |
2184 | bp->nb_wcred = cred; | |
2185 | } | |
2186 | error = nfs_buf_write(bp); | |
2187 | // Note: bp has been released | |
2188 | if (error) { | |
2189 | FSDBG(512, bp, 0xd00dee, 0xbad, error); | |
b0d623f7 | 2190 | nfs_node_lock_force(np); |
91447636 A |
2191 | np->n_error = error; |
2192 | np->n_flag |= NWRITEERR; | |
55e303ae | 2193 | /* |
91447636 A |
2194 | * There was a write error and we need to |
2195 | * invalidate attrs and flush buffers in | |
2196 | * order to sync up with the server. | |
2197 | * (if this write was extending the file, | |
2198 | * we may no longer know the correct size) | |
55e303ae | 2199 | */ |
91447636 | 2200 | NATTRINVALIDATE(np); |
b0d623f7 | 2201 | nfs_node_unlock(np); |
2d21ac55 | 2202 | nfs_data_unlock(np); |
0a7de745 | 2203 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
b0d623f7 | 2204 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
91447636 | 2205 | error = 0; |
fa4905b1 | 2206 | } |
91447636 | 2207 | } |
1c79356b | 2208 | } |
0a7de745 | 2209 | if (vap->va_data_size != np->n_size) { |
2d21ac55 | 2210 | ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */ |
0a7de745 | 2211 | } |
2d21ac55 | 2212 | origsize = np->n_size; |
91447636 | 2213 | np->n_size = np->n_vattr.nva_size = vap->va_data_size; |
b0d623f7 | 2214 | nfs_node_lock_force(np); |
2d21ac55 | 2215 | CLR(np->n_flag, NUPDATESIZE); |
b0d623f7 | 2216 | nfs_node_unlock(np); |
2d21ac55 A |
2217 | FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); |
2218 | } | |
2219 | } else if (VATTR_IS_ACTIVE(vap, va_modify_time) || | |
0a7de745 A |
2220 | VATTR_IS_ACTIVE(vap, va_access_time) || |
2221 | (vap->va_vaflags & VA_UTIMES_NULL)) { | |
2222 | if ((error = nfs_node_lock(np))) { | |
f427ee49 A |
2223 | #if CONFIG_NFS4 |
2224 | if (nfsvers >= NFS_VER4) { | |
2225 | nfs_mount_state_in_use_end(nmp, 0); | |
2226 | } | |
2227 | #endif | |
0a7de745 A |
2228 | return error; |
2229 | } | |
2d21ac55 | 2230 | if ((np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) { |
b0d623f7 | 2231 | nfs_node_unlock(np); |
2d21ac55 | 2232 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
0a7de745 | 2233 | if (error == EINTR) { |
f427ee49 A |
2234 | #if CONFIG_NFS4 |
2235 | if (nfsvers >= NFS_VER4) { | |
2236 | nfs_mount_state_in_use_end(nmp, 0); | |
2237 | } | |
2238 | #endif | |
0a7de745 A |
2239 | return error; |
2240 | } | |
2d21ac55 | 2241 | } else { |
b0d623f7 | 2242 | nfs_node_unlock(np); |
2d21ac55 | 2243 | } |
55e303ae | 2244 | } |
f427ee49 A |
2245 | |
2246 | MALLOC(dul, struct nfs_dulookup *, sizeof(*dul), M_TEMP, M_WAITOK); | |
2247 | ||
6d2010ae | 2248 | if ((VATTR_IS_ACTIVE(vap, va_mode) || VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) || |
0a7de745 | 2249 | VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid)) && |
6d2010ae A |
2250 | !(error = nfs_node_lock(np))) { |
2251 | NACCESSINVALIDATE(np); | |
b0d623f7 | 2252 | nfs_node_unlock(np); |
6d2010ae A |
2253 | if (!namedattrs) { |
2254 | dvp = vnode_getparent(vp); | |
2255 | vname = vnode_getname(vp); | |
2256 | dnp = (dvp && vname) ? VTONFS(dvp) : NULL; | |
2257 | if (dnp) { | |
813fb2f6 A |
2258 | if (nfs_node_set_busy(dnp, vfs_context_thread(ctx))) { |
2259 | vnode_put(dvp); | |
2260 | vnode_putname(vname); | |
2261 | } else { | |
f427ee49 A |
2262 | nfs_dulookup_init(dul, dnp, vname, NFS_STRLEN_INT(vname), ctx); |
2263 | nfs_dulookup_start(dul, dnp, ctx); | |
813fb2f6 | 2264 | dul_in_progress = 1; |
6d2010ae | 2265 | } |
813fb2f6 | 2266 | } else { |
0a7de745 | 2267 | if (dvp) { |
813fb2f6 | 2268 | vnode_put(dvp); |
0a7de745 A |
2269 | } |
2270 | if (vname) { | |
813fb2f6 | 2271 | vnode_putname(vname); |
0a7de745 | 2272 | } |
2d21ac55 | 2273 | } |
2d21ac55 A |
2274 | } |
2275 | } | |
2276 | ||
0a7de745 | 2277 | if (!error) { |
b0d623f7 | 2278 | error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx); |
0a7de745 | 2279 | } |
b0d623f7 | 2280 | |
813fb2f6 | 2281 | if (dul_in_progress) { |
f427ee49 | 2282 | nfs_dulookup_finish(dul, dnp, ctx); |
813fb2f6 A |
2283 | nfs_node_clear_busy(dnp); |
2284 | vnode_put(dvp); | |
2285 | vnode_putname(vname); | |
91447636 | 2286 | } |
1c79356b | 2287 | |
f427ee49 | 2288 | FREE(dul, M_TEMP); |
2d21ac55 A |
2289 | FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error); |
2290 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { | |
6d2010ae A |
2291 | if (error && (origsize != np->n_size) && |
2292 | ((nfsvers < NFS_VER4) || !nfs_mount_state_error_should_restart(error))) { | |
2d21ac55 | 2293 | /* make every effort to resync file size w/ server... */ |
6d2010ae | 2294 | /* (don't bother if we'll be restarting the operation) */ |
2d21ac55 A |
2295 | int err; /* preserve "error" for return */ |
2296 | np->n_size = np->n_vattr.nva_size = origsize; | |
b0d623f7 | 2297 | nfs_node_lock_force(np); |
2d21ac55 | 2298 | CLR(np->n_flag, NUPDATESIZE); |
b0d623f7 | 2299 | nfs_node_unlock(np); |
2d21ac55 A |
2300 | FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002); |
2301 | ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */ | |
6d2010ae | 2302 | vapsize = vap->va_data_size; |
2d21ac55 | 2303 | vap->va_data_size = origsize; |
b0d623f7 | 2304 | err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx); |
0a7de745 | 2305 | if (err) { |
6d2010ae | 2306 | NP(np, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers, error, err); |
0a7de745 | 2307 | } |
6d2010ae | 2308 | vap->va_data_size = vapsize; |
2d21ac55 | 2309 | } |
b0d623f7 A |
2310 | nfs_node_lock_force(np); |
2311 | /* | |
2312 | * The size was just set. If the size is already marked for update, don't | |
2313 | * trust the newsize (it may have been set while the setattr was in progress). | |
2314 | * Clear the update flag and make sure we fetch new attributes so we are sure | |
2315 | * we have the latest size. | |
2316 | */ | |
2317 | if (ISSET(np->n_flag, NUPDATESIZE)) { | |
2318 | CLR(np->n_flag, NUPDATESIZE); | |
2319 | NATTRINVALIDATE(np); | |
2320 | nfs_node_unlock(np); | |
6d2010ae | 2321 | nfs_getattr(np, NULL, ctx, NGA_UNCACHED); |
b0d623f7 A |
2322 | } else { |
2323 | nfs_node_unlock(np); | |
2324 | } | |
2d21ac55 | 2325 | nfs_data_unlock(np); |
cb323159 | 2326 | #if CONFIG_NFS4 |
b0d623f7 | 2327 | if (nfsvers >= NFS_VER4) { |
6d2010ae A |
2328 | if (nofp) { |
2329 | /* don't close our setattr open if we'll be restarting... */ | |
2330 | if (!nfs_mount_state_error_should_restart(error) && | |
2331 | (nofp->nof_flags & NFS_OPEN_FILE_SETATTR)) { | |
2332 | int err = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx); | |
0a7de745 | 2333 | if (err) { |
6d2010ae | 2334 | NP(np, "nfs_vnop_setattr: close error: %d", err); |
0a7de745 | 2335 | } |
6d2010ae | 2336 | nofp->nof_flags &= ~NFS_OPEN_FILE_SETATTR; |
b0d623f7 | 2337 | } |
b0d623f7 | 2338 | nfs_open_file_clear_busy(nofp); |
6d2010ae | 2339 | nofp = NULL; |
b0d623f7 | 2340 | } |
0a7de745 | 2341 | if (nfs_mount_state_in_use_end(nmp, error)) { |
6d2010ae | 2342 | goto restart; |
0a7de745 | 2343 | } |
b0d623f7 A |
2344 | nfs_open_owner_rele(noop); |
2345 | } | |
cb323159 | 2346 | #endif |
1c79356b | 2347 | } |
0a7de745 | 2348 | return error; |
1c79356b A |
2349 | } |
2350 | ||
2351 | /* | |
2d21ac55 | 2352 | * Do an NFS setattr RPC. |
1c79356b | 2353 | */ |
2d21ac55 A |
2354 | int |
2355 | nfs3_setattr_rpc( | |
2356 | nfsnode_t np, | |
2357 | struct vnode_attr *vap, | |
b0d623f7 | 2358 | vfs_context_t ctx) |
1c79356b | 2359 | { |
2d21ac55 | 2360 | struct nfsmount *nmp = NFSTONMP(np); |
f427ee49 | 2361 | int error = 0, lockerror = ENOENT, status = 0, wccpostattr = 0, nfsvers; |
b0d623f7 | 2362 | u_int64_t xid, nextxid; |
2d21ac55 | 2363 | struct nfsm_chain nmreq, nmrep; |
55e303ae | 2364 | |
0a7de745 A |
2365 | if (nfs_mount_gone(nmp)) { |
2366 | return ENXIO; | |
2367 | } | |
2d21ac55 | 2368 | nfsvers = nmp->nm_vers; |
1c79356b | 2369 | |
2d21ac55 A |
2370 | VATTR_SET_SUPPORTED(vap, va_mode); |
2371 | VATTR_SET_SUPPORTED(vap, va_uid); | |
2372 | VATTR_SET_SUPPORTED(vap, va_gid); | |
2373 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
2374 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
2375 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
2376 | ||
cb323159 A |
2377 | |
2378 | if (VATTR_IS_ACTIVE(vap, va_flags) | |
2379 | ) { | |
0a7de745 A |
2380 | if (vap->va_flags) { /* we don't support setting flags */ |
2381 | if (vap->va_active & ~VNODE_ATTR_va_flags) { | |
2382 | return EINVAL; /* return EINVAL if other attributes also set */ | |
2383 | } else { | |
2384 | return ENOTSUP; /* return ENOTSUP for chflags(2) */ | |
2385 | } | |
2d21ac55 A |
2386 | } |
2387 | /* no flags set, so we'll just ignore it */ | |
0a7de745 A |
2388 | if (!(vap->va_active & ~VNODE_ATTR_va_flags)) { |
2389 | return 0; /* no (other) attributes to set, so nothing to do */ | |
2390 | } | |
2d21ac55 A |
2391 | } |
2392 | ||
2393 | nfsm_chain_null(&nmreq); | |
2394 | nfsm_chain_null(&nmrep); | |
2395 | ||
2396 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 2397 | NFSX_FH(nfsvers) + NFSX_SATTR(nfsvers)); |
2d21ac55 A |
2398 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); |
2399 | if (nfsvers == NFS_VER3) { | |
91447636 | 2400 | if (VATTR_IS_ACTIVE(vap, va_mode)) { |
2d21ac55 A |
2401 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2402 | nfsm_chain_add_32(error, &nmreq, vap->va_mode); | |
1c79356b | 2403 | } else { |
2d21ac55 | 2404 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2405 | } |
91447636 | 2406 | if (VATTR_IS_ACTIVE(vap, va_uid)) { |
2d21ac55 A |
2407 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2408 | nfsm_chain_add_32(error, &nmreq, vap->va_uid); | |
1c79356b | 2409 | } else { |
2d21ac55 | 2410 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2411 | } |
91447636 | 2412 | if (VATTR_IS_ACTIVE(vap, va_gid)) { |
2d21ac55 A |
2413 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2414 | nfsm_chain_add_32(error, &nmreq, vap->va_gid); | |
1c79356b | 2415 | } else { |
2d21ac55 | 2416 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2417 | } |
91447636 | 2418 | if (VATTR_IS_ACTIVE(vap, va_data_size)) { |
2d21ac55 A |
2419 | nfsm_chain_add_32(error, &nmreq, TRUE); |
2420 | nfsm_chain_add_64(error, &nmreq, vap->va_data_size); | |
1c79356b | 2421 | } else { |
2d21ac55 | 2422 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2423 | } |
2d21ac55 A |
2424 | if (vap->va_vaflags & VA_UTIMES_NULL) { |
2425 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER); | |
2426 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER); | |
2427 | } else { | |
2428 | if (VATTR_IS_ACTIVE(vap, va_access_time)) { | |
2429 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT); | |
2430 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec); | |
2431 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_nsec); | |
1c79356b | 2432 | } else { |
2d21ac55 | 2433 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE); |
1c79356b | 2434 | } |
2d21ac55 A |
2435 | if (VATTR_IS_ACTIVE(vap, va_modify_time)) { |
2436 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT); | |
2437 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec); | |
2438 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_nsec); | |
1c79356b | 2439 | } else { |
2d21ac55 | 2440 | nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE); |
1c79356b | 2441 | } |
1c79356b | 2442 | } |
2d21ac55 | 2443 | nfsm_chain_add_32(error, &nmreq, FALSE); |
1c79356b | 2444 | } else { |
2d21ac55 | 2445 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_mode) ? |
0a7de745 | 2446 | vtonfsv2_mode(vnode_vtype(NFSTOV(np)), vap->va_mode) : -1); |
2d21ac55 | 2447 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_uid) ? |
0a7de745 | 2448 | vap->va_uid : (uint32_t)-1); |
2d21ac55 | 2449 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_gid) ? |
0a7de745 | 2450 | vap->va_gid : (uint32_t)-1); |
2d21ac55 | 2451 | nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_data_size) ? |
0a7de745 | 2452 | vap->va_data_size : (uint32_t)-1); |
91447636 | 2453 | if (VATTR_IS_ACTIVE(vap, va_access_time)) { |
2d21ac55 A |
2454 | nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec); |
2455 | nfsm_chain_add_32(error, &nmreq, (vap->va_access_time.tv_nsec != -1) ? | |
0a7de745 | 2456 | ((uint32_t)vap->va_access_time.tv_nsec / 1000) : 0xffffffff); |
91447636 | 2457 | } else { |
2d21ac55 A |
2458 | nfsm_chain_add_32(error, &nmreq, -1); |
2459 | nfsm_chain_add_32(error, &nmreq, -1); | |
91447636 A |
2460 | } |
2461 | if (VATTR_IS_ACTIVE(vap, va_modify_time)) { | |
2d21ac55 A |
2462 | nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec); |
2463 | nfsm_chain_add_32(error, &nmreq, (vap->va_modify_time.tv_nsec != -1) ? | |
0a7de745 | 2464 | ((uint32_t)vap->va_modify_time.tv_nsec / 1000) : 0xffffffff); |
91447636 | 2465 | } else { |
2d21ac55 A |
2466 | nfsm_chain_add_32(error, &nmreq, -1); |
2467 | nfsm_chain_add_32(error, &nmreq, -1); | |
91447636 | 2468 | } |
1c79356b | 2469 | } |
2d21ac55 A |
2470 | nfsm_chain_build_done(error, &nmreq); |
2471 | nfsmout_if(error); | |
6d2010ae | 2472 | error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx, NULL, &nmrep, &xid, &status); |
0a7de745 | 2473 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 2474 | error = lockerror; |
0a7de745 | 2475 | } |
2d21ac55 | 2476 | if (nfsvers == NFS_VER3) { |
cb323159 | 2477 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
2478 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); |
2479 | nfsmout_if(error); | |
483a1d10 | 2480 | /* if file hadn't changed, update cached mtime */ |
0a7de745 | 2481 | if (nfstimespeccmp(&np->n_mtime, &premtime, ==)) { |
2d21ac55 | 2482 | NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr); |
0a7de745 | 2483 | } |
483a1d10 | 2484 | /* if directory hadn't changed, update namecache mtime */ |
2d21ac55 | 2485 | if ((vnode_vtype(NFSTOV(np)) == VDIR) && |
0a7de745 | 2486 | nfstimespeccmp(&np->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 2487 | NFS_CHANGED_UPDATE_NC(nfsvers, np, &np->n_vattr); |
0a7de745 A |
2488 | } |
2489 | if (!wccpostattr) { | |
2d21ac55 | 2490 | NATTRINVALIDATE(np); |
0a7de745 | 2491 | } |
2d21ac55 | 2492 | error = status; |
e5568f75 | 2493 | } else { |
0a7de745 | 2494 | if (!error) { |
2d21ac55 | 2495 | error = status; |
0a7de745 | 2496 | } |
6d2010ae | 2497 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
e5568f75 | 2498 | } |
b0d623f7 A |
2499 | /* |
2500 | * We just changed the attributes and we want to make sure that we | |
2501 | * see the latest attributes. Get the next XID. If it's not the | |
2502 | * next XID after the SETATTR XID, then it's possible that another | |
2503 | * RPC was in flight at the same time and it might put stale attributes | |
2504 | * in the cache. In that case, we invalidate the attributes and set | |
2505 | * the attribute cache XID to guarantee that newer attributes will | |
2506 | * get loaded next. | |
2507 | */ | |
2508 | nextxid = 0; | |
2509 | nfs_get_xid(&nextxid); | |
2510 | if (nextxid != (xid + 1)) { | |
2511 | np->n_xid = nextxid; | |
2512 | NATTRINVALIDATE(np); | |
2513 | } | |
2d21ac55 | 2514 | nfsmout: |
0a7de745 | 2515 | if (!lockerror) { |
b0d623f7 | 2516 | nfs_node_unlock(np); |
0a7de745 | 2517 | } |
2d21ac55 A |
2518 | nfsm_chain_cleanup(&nmreq); |
2519 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 2520 | return error; |
1c79356b A |
2521 | } |
2522 | ||
2523 | /* | |
2d21ac55 | 2524 | * NFS lookup call, one step at a time... |
1c79356b | 2525 | * First look in cache |
2d21ac55 | 2526 | * If not found, unlock the directory nfsnode and do the RPC |
1c79356b | 2527 | */ |
b0d623f7 | 2528 | int |
2d21ac55 | 2529 | nfs_vnop_lookup( |
91447636 | 2530 | struct vnop_lookup_args /* { |
0a7de745 A |
2531 | * struct vnodeop_desc *a_desc; |
2532 | * vnode_t a_dvp; | |
2533 | * vnode_t *a_vpp; | |
2534 | * struct componentname *a_cnp; | |
2535 | * vfs_context_t a_context; | |
2536 | * } */*ap) | |
1c79356b | 2537 | { |
2d21ac55 | 2538 | vfs_context_t ctx = ap->a_context; |
91447636 A |
2539 | struct componentname *cnp = ap->a_cnp; |
2540 | vnode_t dvp = ap->a_dvp; | |
2541 | vnode_t *vpp = ap->a_vpp; | |
2542 | int flags = cnp->cn_flags; | |
2543 | vnode_t newvp; | |
2d21ac55 A |
2544 | nfsnode_t dnp, np; |
2545 | struct nfsmount *nmp; | |
2546 | mount_t mp; | |
b0d623f7 | 2547 | int nfsvers, error, busyerror = ENOENT, isdot, isdotdot, negnamecache; |
f427ee49 A |
2548 | u_int64_t xid = 0; |
2549 | struct nfs_vattr *nvattr; | |
2550 | int ngflags, skipdu = 0; | |
2d21ac55 | 2551 | struct vnop_access_args naa; |
f427ee49 A |
2552 | fhandle_t *fh; |
2553 | struct nfsreq *req; | |
1c79356b | 2554 | |
1c79356b | 2555 | *vpp = NULLVP; |
55e303ae | 2556 | |
91447636 | 2557 | dnp = VTONFS(dvp); |
f427ee49 A |
2558 | |
2559 | fh = zalloc(nfs_fhandle_zone); | |
2560 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); | |
2561 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
2562 | NVATTR_INIT(nvattr); | |
fa4905b1 | 2563 | |
2d21ac55 A |
2564 | mp = vnode_mount(dvp); |
2565 | nmp = VFSTONFS(mp); | |
fe8ab488 | 2566 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2567 | error = ENXIO; |
2568 | goto error_return; | |
2569 | } | |
2570 | nfsvers = nmp->nm_vers; | |
6d2010ae | 2571 | negnamecache = !NMFLAG(nmp, NONEGNAMECACHE); |
2d21ac55 | 2572 | |
0a7de745 | 2573 | if ((error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)))) { |
b0d623f7 | 2574 | goto error_return; |
0a7de745 | 2575 | } |
b0d623f7 | 2576 | /* nfs_getattr() will check changed and purge caches */ |
0a7de745 | 2577 | if ((error = nfs_getattr(dnp, NULL, ctx, NGA_CACHED))) { |
91447636 | 2578 | goto error_return; |
0a7de745 | 2579 | } |
1c79356b | 2580 | |
91447636 A |
2581 | error = cache_lookup(dvp, vpp, cnp); |
2582 | switch (error) { | |
2583 | case ENOENT: | |
2d21ac55 A |
2584 | /* negative cache entry */ |
2585 | goto error_return; | |
91447636 A |
2586 | case 0: |
2587 | /* cache miss */ | |
6d2010ae | 2588 | if ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { |
b0d623f7 | 2589 | /* if rdirplus, try dir buf cache lookup */ |
f427ee49 | 2590 | error = nfs_dir_buf_cache_lookup(dnp, &np, cnp, ctx, 0, &skipdu); |
b0d623f7 A |
2591 | if (!error && np) { |
2592 | /* dir buf cache hit */ | |
2593 | *vpp = NFSTOV(np); | |
2594 | error = -1; | |
f427ee49 A |
2595 | } else if (skipdu) { |
2596 | /* Skip lookup for du files */ | |
2597 | error = ENOENT; | |
2598 | goto error_return; | |
b0d623f7 A |
2599 | } |
2600 | } | |
0a7de745 | 2601 | if (error != -1) { /* cache miss */ |
b0d623f7 | 2602 | break; |
0a7de745 | 2603 | } |
f427ee49 | 2604 | OS_FALLTHROUGH; |
91447636 A |
2605 | case -1: |
2606 | /* cache hit, not really an error */ | |
316670eb | 2607 | OSAddAtomic64(1, &nfsstats.lookupcache_hits); |
91447636 | 2608 | |
b0d623f7 | 2609 | nfs_node_clear_busy(dnp); |
6d2010ae | 2610 | busyerror = ENOENT; |
2d21ac55 | 2611 | |
91447636 | 2612 | /* check for directory access */ |
6d2010ae | 2613 | naa.a_desc = &vnop_access_desc; |
91447636 A |
2614 | naa.a_vp = dvp; |
2615 | naa.a_action = KAUTH_VNODE_SEARCH; | |
2d21ac55 | 2616 | naa.a_context = ctx; |
91447636 A |
2617 | |
2618 | /* compute actual success/failure based on accessibility */ | |
2d21ac55 | 2619 | error = nfs_vnop_access(&naa); |
f427ee49 | 2620 | OS_FALLTHROUGH; |
91447636 A |
2621 | default: |
2622 | /* unexpected error from cache_lookup */ | |
2623 | goto error_return; | |
2624 | } | |
2d21ac55 A |
2625 | |
2626 | /* skip lookup, if we know who we are: "." or ".." */ | |
2627 | isdot = isdotdot = 0; | |
2628 | if (cnp->cn_nameptr[0] == '.') { | |
0a7de745 | 2629 | if (cnp->cn_namelen == 1) { |
2d21ac55 | 2630 | isdot = 1; |
0a7de745 A |
2631 | } |
2632 | if ((cnp->cn_namelen == 2) && (cnp->cn_nameptr[1] == '.')) { | |
2d21ac55 | 2633 | isdotdot = 1; |
0a7de745 | 2634 | } |
2d21ac55 A |
2635 | } |
2636 | if (isdotdot || isdot) { | |
f427ee49 | 2637 | fh->fh_len = 0; |
91447636 A |
2638 | goto found; |
2639 | } | |
cb323159 | 2640 | #if CONFIG_NFS4 |
6d2010ae A |
2641 | if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) { |
2642 | /* we should never be looking things up in a trigger directory, return nothing */ | |
2643 | error = ENOENT; | |
2644 | goto error_return; | |
2645 | } | |
cb323159 | 2646 | #endif |
55e303ae | 2647 | |
91447636 | 2648 | /* do we know this name is too long? */ |
2d21ac55 | 2649 | nmp = VTONMP(dvp); |
fe8ab488 | 2650 | if (nfs_mount_gone(nmp)) { |
2d21ac55 A |
2651 | error = ENXIO; |
2652 | goto error_return; | |
2653 | } | |
2654 | if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) && | |
f427ee49 | 2655 | (cnp->cn_namelen > nmp->nm_fsattr.nfsa_maxname)) { |
91447636 A |
2656 | error = ENAMETOOLONG; |
2657 | goto error_return; | |
1c79356b | 2658 | } |
fa4905b1 | 2659 | |
1c79356b A |
2660 | error = 0; |
2661 | newvp = NULLVP; | |
91447636 | 2662 | |
316670eb | 2663 | OSAddAtomic64(1, &nfsstats.lookupcache_misses); |
1c79356b | 2664 | |
2d21ac55 A |
2665 | error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &req); |
2666 | nfsmout_if(error); | |
f427ee49 | 2667 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, req, &xid, fh, nvattr); |
2d21ac55 | 2668 | nfsmout_if(error); |
91447636 | 2669 | |
91447636 | 2670 | /* is the file handle the same as this directory's file handle? */ |
f427ee49 | 2671 | isdot = NFS_CMPFH(dnp, fh->fh_data, fh->fh_len); |
2d21ac55 A |
2672 | |
2673 | found: | |
2d21ac55 A |
2674 | if (flags & ISLASTCN) { |
2675 | switch (cnp->cn_nameiop) { | |
2676 | case DELETE: | |
2677 | cnp->cn_flags &= ~MAKEENTRY; | |
2678 | break; | |
2679 | case RENAME: | |
2680 | cnp->cn_flags &= ~MAKEENTRY; | |
2681 | if (isdot) { | |
2682 | error = EISDIR; | |
91447636 A |
2683 | goto error_return; |
2684 | } | |
2d21ac55 | 2685 | break; |
91447636 | 2686 | } |
91447636 A |
2687 | } |
2688 | ||
2d21ac55 | 2689 | if (isdotdot) { |
2d21ac55 A |
2690 | newvp = vnode_getparent(dvp); |
2691 | if (!newvp) { | |
2692 | error = ENOENT; | |
0b4e3aa0 | 2693 | goto error_return; |
1c79356b | 2694 | } |
2d21ac55 | 2695 | } else if (isdot) { |
91447636 | 2696 | error = vnode_get(dvp); |
0a7de745 | 2697 | if (error) { |
0b4e3aa0 | 2698 | goto error_return; |
0a7de745 | 2699 | } |
91447636 | 2700 | newvp = dvp; |
b0d623f7 | 2701 | nfs_node_lock_force(dnp); |
f427ee49 A |
2702 | if (fh->fh_len && (dnp->n_xid <= xid)) { |
2703 | nfs_loadattrcache(dnp, nvattr, &xid, 0); | |
0a7de745 | 2704 | } |
b0d623f7 | 2705 | nfs_node_unlock(dnp); |
1c79356b | 2706 | } else { |
2d21ac55 | 2707 | ngflags = (cnp->cn_flags & MAKEENTRY) ? NG_MAKEENTRY : 0; |
f427ee49 | 2708 | error = nfs_nget(mp, dnp, cnp, fh->fh_data, fh->fh_len, nvattr, &xid, req->r_auth, ngflags, &np); |
0a7de745 | 2709 | if (error) { |
0b4e3aa0 | 2710 | goto error_return; |
0a7de745 | 2711 | } |
1c79356b | 2712 | newvp = NFSTOV(np); |
b0d623f7 | 2713 | nfs_node_unlock(np); |
1c79356b | 2714 | } |
1c79356b | 2715 | *vpp = newvp; |
91447636 | 2716 | |
2d21ac55 | 2717 | nfsmout: |
1c79356b | 2718 | if (error) { |
2d21ac55 A |
2719 | if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) && |
2720 | (flags & ISLASTCN) && (error == ENOENT)) { | |
0a7de745 | 2721 | if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp)) { |
1c79356b | 2722 | error = EROFS; |
0a7de745 | 2723 | } else { |
1c79356b | 2724 | error = EJUSTRETURN; |
0a7de745 | 2725 | } |
1c79356b | 2726 | } |
1c79356b | 2727 | } |
2d21ac55 A |
2728 | if ((error == ENOENT) && (cnp->cn_flags & MAKEENTRY) && |
2729 | (cnp->cn_nameiop != CREATE) && negnamecache) { | |
2730 | /* add a negative entry in the name cache */ | |
b0d623f7 | 2731 | nfs_node_lock_force(dnp); |
2d21ac55 A |
2732 | cache_enter(dvp, NULL, cnp); |
2733 | dnp->n_flag |= NNEGNCENTRIES; | |
b0d623f7 | 2734 | nfs_node_unlock(dnp); |
2d21ac55 | 2735 | } |
0b4e3aa0 | 2736 | error_return: |
f427ee49 A |
2737 | NVATTR_CLEANUP(nvattr); |
2738 | NFS_ZFREE(nfs_fhandle_zone, fh); | |
2739 | NFS_ZFREE(nfs_req_zone, req); | |
2740 | FREE(nvattr, M_TEMP); | |
0a7de745 | 2741 | if (!busyerror) { |
b0d623f7 | 2742 | nfs_node_clear_busy(dnp); |
0a7de745 | 2743 | } |
91447636 | 2744 | if (error && *vpp) { |
0a7de745 | 2745 | vnode_put(*vpp); |
91447636 A |
2746 | *vpp = NULLVP; |
2747 | } | |
0a7de745 | 2748 | return error; |
1c79356b A |
2749 | } |
2750 | ||
fe8ab488 A |
2751 | int nfs_readlink_nocache = DEFAULT_READLINK_NOCACHE; |
2752 | ||
1c79356b | 2753 | /* |
2d21ac55 | 2754 | * NFS readlink call |
1c79356b | 2755 | */ |
b0d623f7 | 2756 | int |
2d21ac55 | 2757 | nfs_vnop_readlink( |
91447636 | 2758 | struct vnop_readlink_args /* { |
0a7de745 A |
2759 | * struct vnodeop_desc *a_desc; |
2760 | * vnode_t a_vp; | |
2761 | * struct uio *a_uio; | |
2762 | * vfs_context_t a_context; | |
2763 | * } */*ap) | |
1c79356b | 2764 | { |
2d21ac55 A |
2765 | vfs_context_t ctx = ap->a_context; |
2766 | nfsnode_t np = VTONFS(ap->a_vp); | |
2767 | struct nfsmount *nmp; | |
b0d623f7 | 2768 | int error = 0, nfsvers; |
f427ee49 | 2769 | size_t buflen; |
b0d623f7 | 2770 | uio_t uio = ap->a_uio; |
2d21ac55 | 2771 | struct nfsbuf *bp = NULL; |
f427ee49 A |
2772 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; |
2773 | long timeo = 0; | |
2d21ac55 | 2774 | |
0a7de745 A |
2775 | if (vnode_vtype(ap->a_vp) != VLNK) { |
2776 | return EPERM; | |
2777 | } | |
2d21ac55 | 2778 | |
0a7de745 A |
2779 | if (uio_resid(uio) == 0) { |
2780 | return 0; | |
2781 | } | |
2782 | if (uio_offset(uio) < 0) { | |
2783 | return EINVAL; | |
2784 | } | |
2d21ac55 A |
2785 | |
2786 | nmp = VTONMP(ap->a_vp); | |
0a7de745 A |
2787 | if (nfs_mount_gone(nmp)) { |
2788 | return ENXIO; | |
2789 | } | |
2d21ac55 A |
2790 | nfsvers = nmp->nm_vers; |
2791 | ||
0a7de745 | 2792 | |
b0d623f7 | 2793 | /* nfs_getattr() will check changed and purge caches */ |
fe8ab488 | 2794 | if ((error = nfs_getattr(np, NULL, ctx, nfs_readlink_nocache ? NGA_UNCACHED : NGA_CACHED))) { |
2d21ac55 | 2795 | FSDBG(531, np, 0xd1e0001, 0, error); |
0a7de745 | 2796 | return error; |
2d21ac55 | 2797 | } |
2d21ac55 | 2798 | |
fe8ab488 A |
2799 | if (nfs_readlink_nocache) { |
2800 | timeo = nfs_attrcachetimeout(np); | |
2801 | nanouptime(&ts); | |
2802 | } | |
0a7de745 | 2803 | |
fe8ab488 | 2804 | retry: |
316670eb | 2805 | OSAddAtomic64(1, &nfsstats.biocache_readlinks); |
fe8ab488 | 2806 | error = nfs_buf_get(np, 0, NFS_MAXPATHLEN, vfs_context_thread(ctx), NBLK_META, &bp); |
2d21ac55 A |
2807 | if (error) { |
2808 | FSDBG(531, np, 0xd1e0002, 0, error); | |
0a7de745 | 2809 | return error; |
2d21ac55 | 2810 | } |
fe8ab488 A |
2811 | |
2812 | if (nfs_readlink_nocache) { | |
f427ee49 | 2813 | NFS_VNOP_DBG("timeo = %ld ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo, ts.tv_sec, |
0a7de745 A |
2814 | (np->n_rltim.tv_sec + timeo) < ts.tv_sec || nfs_readlink_nocache > 1, |
2815 | ISSET(bp->nb_flags, NB_CACHE) == NB_CACHE); | |
fe8ab488 | 2816 | /* n_rltim is synchronized by the associated nfs buf */ |
0a7de745 | 2817 | if (ISSET(bp->nb_flags, NB_CACHE) && ((nfs_readlink_nocache > 1) || ((np->n_rltim.tv_sec + timeo) < ts.tv_sec))) { |
fe8ab488 A |
2818 | SET(bp->nb_flags, NB_INVAL); |
2819 | nfs_buf_release(bp, 0); | |
2820 | goto retry; | |
2821 | } | |
2822 | } | |
2d21ac55 | 2823 | if (!ISSET(bp->nb_flags, NB_CACHE)) { |
fe8ab488 | 2824 | readagain: |
316670eb | 2825 | OSAddAtomic64(1, &nfsstats.readlink_bios); |
2d21ac55 A |
2826 | buflen = bp->nb_bufsize; |
2827 | error = nmp->nm_funcs->nf_readlink_rpc(np, bp->nb_data, &buflen, ctx); | |
2828 | if (error) { | |
fe8ab488 A |
2829 | if (error == ESTALE) { |
2830 | NFS_VNOP_DBG("Stale FH from readlink rpc\n"); | |
2831 | error = nfs_refresh_fh(np, ctx); | |
0a7de745 | 2832 | if (error == 0) { |
fe8ab488 | 2833 | goto readagain; |
0a7de745 | 2834 | } |
fe8ab488 | 2835 | } |
2d21ac55 A |
2836 | SET(bp->nb_flags, NB_ERROR); |
2837 | bp->nb_error = error; | |
fe8ab488 | 2838 | NFS_VNOP_DBG("readlink failed %d\n", error); |
2d21ac55 A |
2839 | } else { |
2840 | bp->nb_validoff = 0; | |
2841 | bp->nb_validend = buflen; | |
fe8ab488 | 2842 | np->n_rltim = ts; |
f427ee49 | 2843 | NFS_VNOP_DBG("readlink of %.*s\n", (int32_t)bp->nb_validend, (char *)bp->nb_data); |
2d21ac55 | 2844 | } |
fe8ab488 | 2845 | } else { |
f427ee49 | 2846 | NFS_VNOP_DBG("got cached link of %.*s\n", (int32_t)bp->nb_validend, (char *)bp->nb_data); |
2d21ac55 | 2847 | } |
0a7de745 A |
2848 | |
2849 | if (!error && (bp->nb_validend > 0)) { | |
f427ee49 A |
2850 | int validend32 = bp->nb_validend > INT_MAX ? INT_MAX : (int)bp->nb_validend; |
2851 | error = uiomove(bp->nb_data, validend32, uio); | |
2852 | if (!error && bp->nb_validend > validend32) { | |
2853 | error = uiomove(bp->nb_data + validend32, (int)(bp->nb_validend - validend32), uio); | |
2854 | } | |
0a7de745 | 2855 | } |
2d21ac55 A |
2856 | FSDBG(531, np, bp->nb_validend, 0, error); |
2857 | nfs_buf_release(bp, 1); | |
0a7de745 | 2858 | return error; |
1c79356b A |
2859 | } |
2860 | ||
2861 | /* | |
2d21ac55 | 2862 | * Do a readlink RPC. |
1c79356b A |
2863 | */ |
2864 | int | |
f427ee49 | 2865 | nfs3_readlink_rpc(nfsnode_t np, char *buf, size_t *buflenp, vfs_context_t ctx) |
1c79356b | 2866 | { |
2d21ac55 A |
2867 | struct nfsmount *nmp; |
2868 | int error = 0, lockerror = ENOENT, nfsvers, status; | |
f427ee49 | 2869 | size_t len; |
fa4905b1 | 2870 | u_int64_t xid; |
2d21ac55 | 2871 | struct nfsm_chain nmreq, nmrep; |
1c79356b | 2872 | |
2d21ac55 | 2873 | nmp = NFSTONMP(np); |
0a7de745 A |
2874 | if (nfs_mount_gone(nmp)) { |
2875 | return ENXIO; | |
2876 | } | |
2d21ac55 A |
2877 | nfsvers = nmp->nm_vers; |
2878 | nfsm_chain_null(&nmreq); | |
2879 | nfsm_chain_null(&nmrep); | |
2880 | ||
2881 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers)); | |
2882 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
2883 | nfsm_chain_build_done(error, &nmreq); | |
2884 | nfsmout_if(error); | |
6d2010ae | 2885 | error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx, NULL, &nmrep, &xid, &status); |
0a7de745 | 2886 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 2887 | error = lockerror; |
0a7de745 A |
2888 | } |
2889 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 2890 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 A |
2891 | } |
2892 | if (!error) { | |
2d21ac55 | 2893 | error = status; |
0a7de745 | 2894 | } |
2d21ac55 A |
2895 | nfsm_chain_get_32(error, &nmrep, len); |
2896 | nfsmout_if(error); | |
2897 | if ((nfsvers == NFS_VER2) && (len > *buflenp)) { | |
2898 | error = EBADRPC; | |
2899 | goto nfsmout; | |
2900 | } | |
2901 | if (len >= *buflenp) { | |
0a7de745 | 2902 | if (np->n_size && (np->n_size < *buflenp)) { |
f427ee49 | 2903 | len = (size_t)np->n_size; |
0a7de745 | 2904 | } else { |
2d21ac55 | 2905 | len = *buflenp - 1; |
0a7de745 | 2906 | } |
2d21ac55 A |
2907 | } |
2908 | nfsm_chain_get_opaque(error, &nmrep, len, buf); | |
0a7de745 | 2909 | if (!error) { |
2d21ac55 | 2910 | *buflenp = len; |
0a7de745 | 2911 | } |
2d21ac55 | 2912 | nfsmout: |
0a7de745 | 2913 | if (!lockerror) { |
b0d623f7 | 2914 | nfs_node_unlock(np); |
0a7de745 | 2915 | } |
2d21ac55 A |
2916 | nfsm_chain_cleanup(&nmreq); |
2917 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 2918 | return error; |
1c79356b A |
2919 | } |
2920 | ||
2921 | /* | |
2d21ac55 | 2922 | * NFS read RPC call |
1c79356b A |
2923 | * Ditto above |
2924 | */ | |
2925 | int | |
b0d623f7 | 2926 | nfs_read_rpc(nfsnode_t np, uio_t uio, vfs_context_t ctx) |
1c79356b | 2927 | { |
1c79356b | 2928 | struct nfsmount *nmp; |
2d21ac55 | 2929 | int error = 0, nfsvers, eof = 0; |
b0d623f7 A |
2930 | size_t nmrsize, len, retlen; |
2931 | user_ssize_t tsiz; | |
2d21ac55 | 2932 | off_t txoffset; |
f427ee49 | 2933 | struct nfsreq *req; |
cb323159 | 2934 | #if CONFIG_NFS4 |
b0d623f7 | 2935 | uint32_t stategenid = 0, restart = 0; |
cb323159 | 2936 | #endif |
b0d623f7 | 2937 | FSDBG_TOP(536, np, uio_offset(uio), uio_resid(uio), 0); |
2d21ac55 | 2938 | nmp = NFSTONMP(np); |
0a7de745 A |
2939 | if (nfs_mount_gone(nmp)) { |
2940 | return ENXIO; | |
2941 | } | |
2d21ac55 | 2942 | nfsvers = nmp->nm_vers; |
55e303ae A |
2943 | nmrsize = nmp->nm_rsize; |
2944 | ||
b0d623f7 A |
2945 | txoffset = uio_offset(uio); |
2946 | tsiz = uio_resid(uio); | |
2947 | if ((nfsvers == NFS_VER2) && ((uint64_t)(txoffset + tsiz) > 0xffffffffULL)) { | |
2948 | FSDBG_BOT(536, np, uio_offset(uio), uio_resid(uio), EFBIG); | |
0a7de745 | 2949 | return EFBIG; |
55e303ae | 2950 | } |
2d21ac55 | 2951 | |
f427ee49 | 2952 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); |
1c79356b | 2953 | while (tsiz > 0) { |
b0d623f7 | 2954 | len = retlen = (tsiz > (user_ssize_t)nmrsize) ? nmrsize : (size_t)tsiz; |
2d21ac55 | 2955 | FSDBG(536, np, txoffset, len, 0); |
6d2010ae A |
2956 | if (np->n_flag & NREVOKE) { |
2957 | error = EIO; | |
2958 | break; | |
2959 | } | |
cb323159 | 2960 | #if CONFIG_NFS4 |
0a7de745 | 2961 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 2962 | stategenid = nmp->nm_stategenid; |
0a7de745 | 2963 | } |
cb323159 | 2964 | #endif |
2d21ac55 | 2965 | error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, len, |
0a7de745 A |
2966 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req); |
2967 | if (!error) { | |
b0d623f7 | 2968 | error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req, uio, &retlen, &eof); |
0a7de745 | 2969 | } |
cb323159 | 2970 | #if CONFIG_NFS4 |
b0d623f7 A |
2971 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) && |
2972 | (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */ | |
2973 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
2974 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
2975 | NP(np, "nfs_read_rpc: error %d, initiating recovery", error); | |
2976 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
2977 | } |
2978 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae A |
2979 | if (np->n_flag & NREVOKE) { |
2980 | error = EIO; | |
2981 | } else { | |
0a7de745 A |
2982 | if (error == NFSERR_GRACE) { |
2983 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
2984 | } | |
2985 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 2986 | continue; |
0a7de745 | 2987 | } |
6d2010ae | 2988 | } |
b0d623f7 | 2989 | } |
cb323159 | 2990 | #endif |
0a7de745 | 2991 | if (error) { |
91447636 | 2992 | break; |
0a7de745 | 2993 | } |
2d21ac55 A |
2994 | txoffset += retlen; |
2995 | tsiz -= retlen; | |
2996 | if (nfsvers != NFS_VER2) { | |
0a7de745 | 2997 | if (eof || (retlen == 0)) { |
2d21ac55 | 2998 | tsiz = 0; |
0a7de745 A |
2999 | } |
3000 | } else if (retlen < len) { | |
2d21ac55 | 3001 | tsiz = 0; |
0a7de745 | 3002 | } |
2d21ac55 A |
3003 | } |
3004 | ||
f427ee49 | 3005 | NFS_ZFREE(nfs_req_zone, req); |
b0d623f7 | 3006 | FSDBG_BOT(536, np, eof, uio_resid(uio), error); |
0a7de745 | 3007 | return error; |
2d21ac55 A |
3008 | } |
3009 | ||
3010 | int | |
3011 | nfs3_read_rpc_async( | |
3012 | nfsnode_t np, | |
3013 | off_t offset, | |
3014 | size_t len, | |
3015 | thread_t thd, | |
3016 | kauth_cred_t cred, | |
3017 | struct nfsreq_cbinfo *cb, | |
3018 | struct nfsreq **reqp) | |
3019 | { | |
3020 | struct nfsmount *nmp; | |
3021 | int error = 0, nfsvers; | |
3022 | struct nfsm_chain nmreq; | |
3023 | ||
3024 | nmp = NFSTONMP(np); | |
0a7de745 A |
3025 | if (nfs_mount_gone(nmp)) { |
3026 | return ENXIO; | |
3027 | } | |
2d21ac55 A |
3028 | nfsvers = nmp->nm_vers; |
3029 | ||
3030 | nfsm_chain_null(&nmreq); | |
3031 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers) + 3 * NFSX_UNSIGNED); | |
3032 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
3033 | if (nfsvers == NFS_VER3) { | |
3034 | nfsm_chain_add_64(error, &nmreq, offset); | |
3035 | nfsm_chain_add_32(error, &nmreq, len); | |
3036 | } else { | |
3037 | nfsm_chain_add_32(error, &nmreq, offset); | |
3038 | nfsm_chain_add_32(error, &nmreq, len); | |
3039 | nfsm_chain_add_32(error, &nmreq, 0); | |
3040 | } | |
3041 | nfsm_chain_build_done(error, &nmreq); | |
3042 | nfsmout_if(error); | |
6d2010ae | 3043 | error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, NULL, 0, cb, reqp); |
2d21ac55 A |
3044 | nfsmout: |
3045 | nfsm_chain_cleanup(&nmreq); | |
0a7de745 | 3046 | return error; |
2d21ac55 A |
3047 | } |
3048 | ||
3049 | int | |
3050 | nfs3_read_rpc_async_finish( | |
3051 | nfsnode_t np, | |
3052 | struct nfsreq *req, | |
b0d623f7 | 3053 | uio_t uio, |
2d21ac55 A |
3054 | size_t *lenp, |
3055 | int *eofp) | |
3056 | { | |
f427ee49 A |
3057 | int error = 0, lockerror, nfsvers, status = 0, eof = 0; |
3058 | uint32_t retlen = 0; | |
2d21ac55 A |
3059 | uint64_t xid; |
3060 | struct nfsmount *nmp; | |
3061 | struct nfsm_chain nmrep; | |
3062 | ||
3063 | nmp = NFSTONMP(np); | |
fe8ab488 | 3064 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 3065 | nfs_request_async_cancel(req); |
0a7de745 | 3066 | return ENXIO; |
2d21ac55 A |
3067 | } |
3068 | nfsvers = nmp->nm_vers; | |
3069 | ||
3070 | nfsm_chain_null(&nmrep); | |
3071 | ||
3072 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
0a7de745 A |
3073 | if (error == EINPROGRESS) { /* async request restarted */ |
3074 | return error; | |
3075 | } | |
2d21ac55 | 3076 | |
0a7de745 | 3077 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 3078 | error = lockerror; |
0a7de745 A |
3079 | } |
3080 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 3081 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 A |
3082 | } |
3083 | if (!error) { | |
2d21ac55 | 3084 | error = status; |
0a7de745 | 3085 | } |
2d21ac55 A |
3086 | if (nfsvers == NFS_VER3) { |
3087 | nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED); | |
3088 | nfsm_chain_get_32(error, &nmrep, eof); | |
3089 | } else { | |
6d2010ae | 3090 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
2d21ac55 | 3091 | } |
0a7de745 | 3092 | if (!lockerror) { |
b0d623f7 | 3093 | nfs_node_unlock(np); |
0a7de745 | 3094 | } |
2d21ac55 | 3095 | nfsm_chain_get_32(error, &nmrep, retlen); |
0a7de745 | 3096 | if ((nfsvers == NFS_VER2) && (retlen > *lenp)) { |
2d21ac55 | 3097 | error = EBADRPC; |
0a7de745 | 3098 | } |
2d21ac55 | 3099 | nfsmout_if(error); |
b0d623f7 | 3100 | error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uio); |
2d21ac55 A |
3101 | if (eofp) { |
3102 | if (nfsvers == NFS_VER3) { | |
0a7de745 | 3103 | if (!eof && !retlen) { |
2d21ac55 | 3104 | eof = 1; |
0a7de745 | 3105 | } |
2d21ac55 A |
3106 | } else if (retlen < *lenp) { |
3107 | eof = 1; | |
1c79356b | 3108 | } |
2d21ac55 A |
3109 | *eofp = eof; |
3110 | } | |
3111 | *lenp = MIN(retlen, *lenp); | |
3112 | nfsmout: | |
3113 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 3114 | return error; |
2d21ac55 A |
3115 | } |
3116 | ||
3117 | /* | |
3118 | * NFS write call | |
3119 | */ | |
3120 | int | |
3121 | nfs_vnop_write( | |
3122 | struct vnop_write_args /* { | |
0a7de745 A |
3123 | * struct vnodeop_desc *a_desc; |
3124 | * vnode_t a_vp; | |
3125 | * struct uio *a_uio; | |
3126 | * int a_ioflag; | |
3127 | * vfs_context_t a_context; | |
3128 | * } */*ap) | |
2d21ac55 A |
3129 | { |
3130 | vfs_context_t ctx = ap->a_context; | |
b0d623f7 | 3131 | uio_t uio = ap->a_uio; |
2d21ac55 A |
3132 | vnode_t vp = ap->a_vp; |
3133 | nfsnode_t np = VTONFS(vp); | |
3134 | int ioflag = ap->a_ioflag; | |
3135 | struct nfsbuf *bp; | |
2d21ac55 A |
3136 | struct nfsmount *nmp = VTONMP(vp); |
3137 | daddr64_t lbn; | |
f427ee49 A |
3138 | uint32_t biosize; |
3139 | int error = 0; | |
3140 | off_t n, on; | |
3141 | int n32; | |
2d21ac55 | 3142 | off_t boff, start, end; |
b0d623f7 | 3143 | uio_t auio; |
0a7de745 | 3144 | char auio_buf[UIO_SIZEOF(1)]; |
2d21ac55 A |
3145 | thread_t thd; |
3146 | kauth_cred_t cred; | |
3147 | ||
b0d623f7 | 3148 | FSDBG_TOP(515, np, uio_offset(uio), uio_resid(uio), ioflag); |
2d21ac55 A |
3149 | |
3150 | if (vnode_vtype(vp) != VREG) { | |
b0d623f7 | 3151 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), EIO); |
0a7de745 | 3152 | return EIO; |
2d21ac55 A |
3153 | } |
3154 | ||
3155 | thd = vfs_context_thread(ctx); | |
3156 | cred = vfs_context_ucred(ctx); | |
3157 | ||
b0d623f7 | 3158 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
2d21ac55 | 3159 | |
b0d623f7 | 3160 | if ((error = nfs_node_lock(np))) { |
2d21ac55 | 3161 | nfs_data_unlock(np); |
b0d623f7 | 3162 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error); |
0a7de745 | 3163 | return error; |
2d21ac55 A |
3164 | } |
3165 | np->n_wrbusy++; | |
3166 | ||
3167 | if (np->n_flag & NWRITEERR) { | |
3168 | error = np->n_error; | |
3169 | np->n_flag &= ~NWRITEERR; | |
3170 | } | |
3171 | if (np->n_flag & NNEEDINVALIDATE) { | |
3172 | np->n_flag &= ~NNEEDINVALIDATE; | |
b0d623f7 | 3173 | nfs_node_unlock(np); |
2d21ac55 | 3174 | nfs_data_unlock(np); |
0a7de745 | 3175 | nfs_vinvalbuf(vp, V_SAVE | V_IGNORE_WRITEERR, ctx, 1); |
b0d623f7 A |
3176 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
3177 | } else { | |
3178 | nfs_node_unlock(np); | |
2d21ac55 | 3179 | } |
0a7de745 | 3180 | if (error) { |
2d21ac55 | 3181 | goto out; |
0a7de745 | 3182 | } |
2d21ac55 A |
3183 | |
3184 | biosize = nmp->nm_biosize; | |
3185 | ||
3186 | if (ioflag & (IO_APPEND | IO_SYNC)) { | |
b0d623f7 | 3187 | nfs_node_lock_force(np); |
2d21ac55 A |
3188 | if (np->n_flag & NMODIFIED) { |
3189 | NATTRINVALIDATE(np); | |
b0d623f7 | 3190 | nfs_node_unlock(np); |
2d21ac55 A |
3191 | nfs_data_unlock(np); |
3192 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); | |
b0d623f7 A |
3193 | nfs_data_lock(np, NFS_DATA_LOCK_SHARED); |
3194 | if (error) { | |
3195 | FSDBG(515, np, uio_offset(uio), 0x10bad01, error); | |
2d21ac55 | 3196 | goto out; |
e5568f75 | 3197 | } |
b0d623f7 A |
3198 | } else { |
3199 | nfs_node_unlock(np); | |
2d21ac55 A |
3200 | } |
3201 | if (ioflag & IO_APPEND) { | |
2d21ac55 | 3202 | nfs_data_unlock(np); |
b0d623f7 | 3203 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae | 3204 | error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED); |
2d21ac55 | 3205 | /* we'll be extending the file, so take the data lock exclusive */ |
b0d623f7 A |
3206 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
3207 | if (error) { | |
3208 | FSDBG(515, np, uio_offset(uio), 0x10bad02, error); | |
2d21ac55 A |
3209 | goto out; |
3210 | } | |
b0d623f7 | 3211 | uio_setoffset(uio, np->n_size); |
2d21ac55 A |
3212 | } |
3213 | } | |
b0d623f7 | 3214 | if (uio_offset(uio) < 0) { |
2d21ac55 | 3215 | error = EINVAL; |
b0d623f7 | 3216 | FSDBG_BOT(515, np, uio_offset(uio), 0xbad0ff, error); |
2d21ac55 A |
3217 | goto out; |
3218 | } | |
0a7de745 | 3219 | if (uio_resid(uio) == 0) { |
2d21ac55 | 3220 | goto out; |
0a7de745 | 3221 | } |
2d21ac55 | 3222 | |
b0d623f7 | 3223 | if (((uio_offset(uio) + uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) { |
fe8ab488 A |
3224 | /* |
3225 | * It looks like we'll be extending the file, so take the data lock exclusive. | |
3226 | */ | |
2d21ac55 | 3227 | nfs_data_unlock(np); |
b0d623f7 | 3228 | nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE); |
fe8ab488 A |
3229 | |
3230 | /* | |
3231 | * Also, if the write begins after the previous EOF buffer, make sure to zero | |
3232 | * and validate the new bytes in that buffer. | |
3233 | */ | |
3234 | struct nfsbuf *eofbp = NULL; | |
3235 | daddr64_t eofbn = np->n_size / biosize; | |
f427ee49 | 3236 | uint32_t eofoff = np->n_size % biosize; |
fe8ab488 A |
3237 | lbn = uio_offset(uio) / biosize; |
3238 | ||
3239 | if (eofoff && (eofbn < lbn)) { | |
0a7de745 | 3240 | if ((error = nfs_buf_get(np, eofbn, biosize, thd, NBLK_WRITE | NBLK_ONLYVALID, &eofbp))) { |
fe8ab488 | 3241 | goto out; |
0a7de745 | 3242 | } |
fe8ab488 A |
3243 | np->n_size += (biosize - eofoff); |
3244 | nfs_node_lock_force(np); | |
3245 | CLR(np->n_flag, NUPDATESIZE); | |
3246 | np->n_flag |= NMODIFIED; | |
3247 | nfs_node_unlock(np); | |
3248 | FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); | |
3249 | ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */ | |
3250 | if (eofbp) { | |
3251 | /* | |
3252 | * For the old last page, don't zero bytes if there | |
3253 | * are invalid bytes in that page (i.e. the page isn't | |
3254 | * currently valid). | |
3255 | * For pages after the old last page, zero them and | |
3256 | * mark them as valid. | |
3257 | */ | |
3258 | char *d; | |
3259 | int i; | |
0a7de745 | 3260 | if (ioflag & IO_NOCACHE) { |
fe8ab488 | 3261 | SET(eofbp->nb_flags, NB_NOCACHE); |
0a7de745 | 3262 | } |
fe8ab488 A |
3263 | NFS_BUF_MAP(eofbp); |
3264 | FSDBG(516, eofbp, eofoff, biosize - eofoff, 0xe0fff01e); | |
3265 | d = eofbp->nb_data; | |
0a7de745 | 3266 | i = eofoff / PAGE_SIZE; |
fe8ab488 A |
3267 | while (eofoff < biosize) { |
3268 | int poff = eofoff & PAGE_MASK; | |
0a7de745 | 3269 | if (!poff || NBPGVALID(eofbp, i)) { |
fe8ab488 A |
3270 | bzero(d + eofoff, PAGE_SIZE - poff); |
3271 | NBPGVALID_SET(eofbp, i); | |
3272 | } | |
3273 | eofoff += PAGE_SIZE - poff; | |
3274 | i++; | |
3275 | } | |
3276 | nfs_buf_release(eofbp, 1); | |
3277 | } | |
3278 | } | |
2d21ac55 A |
3279 | } |
3280 | ||
3281 | do { | |
316670eb | 3282 | OSAddAtomic64(1, &nfsstats.biocache_writes); |
b0d623f7 A |
3283 | lbn = uio_offset(uio) / biosize; |
3284 | on = uio_offset(uio) % biosize; | |
3285 | n = biosize - on; | |
0a7de745 | 3286 | if (uio_resid(uio) < n) { |
b0d623f7 | 3287 | n = uio_resid(uio); |
0a7de745 | 3288 | } |
2d21ac55 A |
3289 | again: |
3290 | /* | |
3291 | * Get a cache block for writing. The range to be written is | |
3292 | * (off..off+n) within the block. We ensure that the block | |
3293 | * either has no dirty region or that the given range is | |
3294 | * contiguous with the existing dirty region. | |
3295 | */ | |
3296 | error = nfs_buf_get(np, lbn, biosize, thd, NBLK_WRITE, &bp); | |
0a7de745 | 3297 | if (error) { |
2d21ac55 | 3298 | goto out; |
0a7de745 | 3299 | } |
2d21ac55 A |
3300 | /* map the block because we know we're going to write to it */ |
3301 | NFS_BUF_MAP(bp); | |
3302 | ||
0a7de745 | 3303 | if (ioflag & IO_NOCACHE) { |
2d21ac55 | 3304 | SET(bp->nb_flags, NB_NOCACHE); |
0a7de745 | 3305 | } |
2d21ac55 A |
3306 | |
3307 | if (!IS_VALID_CRED(bp->nb_wcred)) { | |
3308 | kauth_cred_ref(cred); | |
3309 | bp->nb_wcred = cred; | |
3310 | } | |
3311 | ||
3312 | /* | |
3313 | * If there's already a dirty range AND dirty pages in this block we | |
3314 | * need to send a commit AND write the dirty pages before continuing. | |
3315 | * | |
3316 | * If there's already a dirty range OR dirty pages in this block | |
3317 | * and the new write range is not contiguous with the existing range, | |
3318 | * then force the buffer to be written out now. | |
3319 | * (We used to just extend the dirty range to cover the valid, | |
3320 | * but unwritten, data in between also. But writing ranges | |
3321 | * of data that weren't actually written by an application | |
3322 | * risks overwriting some other client's data with stale data | |
3323 | * that's just masquerading as new written data.) | |
3324 | */ | |
3325 | if (bp->nb_dirtyend > 0) { | |
f427ee49 | 3326 | if (on > bp->nb_dirtyend || (on + n) < bp->nb_dirtyoff || nfs_buf_pgs_is_set(&bp->nb_dirty)) { |
0a7de745 A |
3327 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c001); |
3328 | /* write/commit buffer "synchronously" */ | |
3329 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
3330 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL)); | |
3331 | SET(bp->nb_flags, (NB_ASYNC | NB_STABLE)); | |
3332 | error = nfs_buf_write(bp); | |
3333 | if (error) { | |
3334 | goto out; | |
3335 | } | |
3336 | goto again; | |
3337 | } | |
f427ee49 A |
3338 | } else if (nfs_buf_pgs_is_set(&bp->nb_dirty)) { |
3339 | off_t firstpg = 0, lastpg = 0; | |
3340 | nfsbufpgs pagemask, pagemaskand; | |
0a7de745 | 3341 | /* calculate write range pagemask */ |
f427ee49 A |
3342 | if (n > 0) { |
3343 | firstpg = on / PAGE_SIZE; | |
3344 | lastpg = (on + n - 1) / PAGE_SIZE; | |
3345 | nfs_buf_pgs_set_pages_between(&pagemask, firstpg, lastpg + 1); | |
3346 | } else { | |
3347 | NBPGS_ERASE(&pagemask); | |
3348 | } | |
0a7de745 | 3349 | /* check if there are dirty pages outside the write range */ |
f427ee49 A |
3350 | nfs_buf_pgs_bit_not(&pagemask); |
3351 | nfs_buf_pgs_bit_and(&bp->nb_dirty, &pagemask, &pagemaskand); | |
3352 | if (nfs_buf_pgs_is_set(&pagemaskand)) { | |
0a7de745 A |
3353 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c002); |
3354 | /* write/commit buffer "synchronously" */ | |
3355 | /* (NB_STABLE indicates that data writes should be FILESYNC) */ | |
3356 | CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL)); | |
3357 | SET(bp->nb_flags, (NB_ASYNC | NB_STABLE)); | |
3358 | error = nfs_buf_write(bp); | |
3359 | if (error) { | |
3360 | goto out; | |
3361 | } | |
3362 | goto again; | |
3363 | } | |
3364 | /* if the first or last pages are already dirty */ | |
3365 | /* make sure that the dirty range encompasses those pages */ | |
3366 | if (NBPGDIRTY(bp, firstpg) || NBPGDIRTY(bp, lastpg)) { | |
3367 | FSDBG(515, np, uio_offset(uio), bp, 0xd15c003); | |
f427ee49 | 3368 | bp->nb_dirtyoff = MIN(on, firstpg * PAGE_SIZE); |
0a7de745 A |
3369 | if (NBPGDIRTY(bp, lastpg)) { |
3370 | bp->nb_dirtyend = (lastpg + 1) * PAGE_SIZE; | |
3371 | /* clip to EOF */ | |
3372 | if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) { | |
3373 | bp->nb_dirtyend = np->n_size - NBOFF(bp); | |
3374 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) { | |
3375 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; | |
3376 | } | |
3377 | } | |
3378 | } else { | |
3379 | bp->nb_dirtyend = on + n; | |
3380 | } | |
3381 | } | |
2d21ac55 A |
3382 | } |
3383 | ||
3384 | /* | |
3385 | * Are we extending the size of the file with this write? | |
3386 | * If so, update file size now that we have the block. | |
3387 | * If there was a partial buf at the old eof, validate | |
3388 | * and zero the new bytes. | |
3389 | */ | |
b0d623f7 | 3390 | if ((uio_offset(uio) + n) > (off_t)np->n_size) { |
2d21ac55 | 3391 | daddr64_t eofbn = np->n_size / biosize; |
b0d623f7 | 3392 | int neweofoff = (uio_offset(uio) + n) % biosize; |
2d21ac55 | 3393 | |
b0d623f7 | 3394 | FSDBG(515, 0xb1ffa000, uio_offset(uio) + n, eofoff, neweofoff); |
2d21ac55 | 3395 | |
2d21ac55 A |
3396 | /* if we're extending within the same last block */ |
3397 | /* and the block is flagged as being cached... */ | |
3398 | if ((lbn == eofbn) && ISSET(bp->nb_flags, NB_CACHE)) { | |
3399 | /* ...check that all pages in buffer are valid */ | |
0a7de745 | 3400 | int endpg = ((neweofoff ? neweofoff : biosize) - 1) / PAGE_SIZE; |
f427ee49 | 3401 | nfsbufpgs pagemask, pagemaskand; |
2d21ac55 | 3402 | /* pagemask only has to extend to last page being written to */ |
f427ee49 | 3403 | nfs_buf_pgs_get_page_mask(&pagemask, endpg + 1); |
2d21ac55 | 3404 | FSDBG(515, 0xb1ffa001, bp->nb_valid, pagemask, 0); |
f427ee49 A |
3405 | nfs_buf_pgs_bit_and(&bp->nb_valid, &pagemask, &pagemaskand); |
3406 | if (!NBPGS_IS_EQUAL(&pagemaskand, &pagemask)) { | |
2d21ac55 A |
3407 | /* zerofill any hole */ |
3408 | if (on > bp->nb_validend) { | |
f427ee49 | 3409 | for (off_t i = bp->nb_validend / PAGE_SIZE; i <= (on - 1) / PAGE_SIZE; i++) { |
2d21ac55 | 3410 | NBPGVALID_SET(bp, i); |
0a7de745 | 3411 | } |
2d21ac55 A |
3412 | NFS_BUF_MAP(bp); |
3413 | FSDBG(516, bp, bp->nb_validend, on - bp->nb_validend, 0xf01e); | |
f427ee49 | 3414 | NFS_BZERO((char *)bp->nb_data + bp->nb_validend, on - bp->nb_validend); |
2d21ac55 A |
3415 | } |
3416 | /* zerofill any trailing data in the last page */ | |
3417 | if (neweofoff) { | |
3418 | NFS_BUF_MAP(bp); | |
3419 | FSDBG(516, bp, neweofoff, PAGE_SIZE - (neweofoff & PAGE_MASK), 0xe0f); | |
3420 | bzero((char *)bp->nb_data + neweofoff, | |
0a7de745 | 3421 | PAGE_SIZE - (neweofoff & PAGE_MASK)); |
2d21ac55 A |
3422 | } |
3423 | } | |
3424 | } | |
b0d623f7 A |
3425 | np->n_size = uio_offset(uio) + n; |
3426 | nfs_node_lock_force(np); | |
2d21ac55 A |
3427 | CLR(np->n_flag, NUPDATESIZE); |
3428 | np->n_flag |= NMODIFIED; | |
b0d623f7 | 3429 | nfs_node_unlock(np); |
2d21ac55 A |
3430 | FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001); |
3431 | ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */ | |
2d21ac55 A |
3432 | } |
3433 | /* | |
3434 | * If dirtyend exceeds file size, chop it down. This should | |
3435 | * not occur unless there is a race. | |
3436 | */ | |
3437 | if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) { | |
3438 | bp->nb_dirtyend = np->n_size - NBOFF(bp); | |
0a7de745 | 3439 | if (bp->nb_dirtyoff >= bp->nb_dirtyend) { |
2d21ac55 | 3440 | bp->nb_dirtyoff = bp->nb_dirtyend = 0; |
0a7de745 | 3441 | } |
2d21ac55 A |
3442 | } |
3443 | /* | |
3444 | * UBC doesn't handle partial pages, so we need to make sure | |
3445 | * that any pages left in the page cache are completely valid. | |
3446 | * | |
3447 | * Writes that are smaller than a block are delayed if they | |
3448 | * don't extend to the end of the block. | |
3449 | * | |
3450 | * If the block isn't (completely) cached, we may need to read | |
3451 | * in some parts of pages that aren't covered by the write. | |
3452 | * If the write offset (on) isn't page aligned, we'll need to | |
3453 | * read the start of the first page being written to. Likewise, | |
3454 | * if the offset of the end of the write (on+n) isn't page aligned, | |
3455 | * we'll need to read the end of the last page being written to. | |
3456 | * | |
3457 | * Notes: | |
3458 | * We don't want to read anything we're just going to write over. | |
6d2010ae A |
3459 | * We don't want to read anything we're just going drop when the |
3460 | * I/O is complete (i.e. don't do reads for NOCACHE requests). | |
2d21ac55 A |
3461 | * We don't want to issue multiple I/Os if we don't have to |
3462 | * (because they're synchronous rpcs). | |
3463 | * We don't want to read anything we already have modified in the | |
3464 | * page cache. | |
3465 | */ | |
6d2010ae | 3466 | if (!ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) { |
f427ee49 | 3467 | off_t firstpgoff, lastpgoff, firstpg, lastpg, dirtypg; |
2d21ac55 | 3468 | start = end = -1; |
0a7de745 | 3469 | firstpg = on / PAGE_SIZE; |
2d21ac55 | 3470 | firstpgoff = on & PAGE_MASK; |
0a7de745 A |
3471 | lastpg = (on + n - 1) / PAGE_SIZE; |
3472 | lastpgoff = (on + n) & PAGE_MASK; | |
3473 | if (firstpgoff && !NBPGVALID(bp, firstpg)) { | |
2d21ac55 A |
3474 | /* need to read start of first page */ |
3475 | start = firstpg * PAGE_SIZE; | |
3476 | end = start + firstpgoff; | |
3477 | } | |
0a7de745 | 3478 | if (lastpgoff && !NBPGVALID(bp, lastpg)) { |
2d21ac55 | 3479 | /* need to read end of last page */ |
0a7de745 | 3480 | if (start < 0) { |
2d21ac55 | 3481 | start = (lastpg * PAGE_SIZE) + lastpgoff; |
0a7de745 | 3482 | } |
2d21ac55 A |
3483 | end = (lastpg + 1) * PAGE_SIZE; |
3484 | } | |
6d2010ae A |
3485 | if (ISSET(bp->nb_flags, NB_NOCACHE)) { |
3486 | /* | |
3487 | * For nocache writes, if there is any partial page at the | |
3488 | * start or end of the write range, then we do the write | |
3489 | * synchronously to make sure that we can drop the data | |
3490 | * from the cache as soon as the WRITE finishes. Normally, | |
3491 | * we would do an unstable write and not drop the data until | |
3492 | * it was committed. But doing that here would risk allowing | |
3493 | * invalid data to be read from the cache between the WRITE | |
3494 | * and the COMMIT. | |
3495 | * (NB_STABLE indicates that data writes should be FILESYNC) | |
3496 | */ | |
0a7de745 | 3497 | if (end > start) { |
6d2010ae | 3498 | SET(bp->nb_flags, NB_STABLE); |
0a7de745 | 3499 | } |
6d2010ae A |
3500 | goto skipread; |
3501 | } | |
2d21ac55 A |
3502 | if (end > start) { |
3503 | /* need to read the data in range: start...end-1 */ | |
3504 | ||
3505 | /* first, check for dirty pages in between */ | |
3506 | /* if there are, we'll have to do two reads because */ | |
3507 | /* we don't want to overwrite the dirty pages. */ | |
0a7de745 A |
3508 | for (dirtypg = start / PAGE_SIZE; dirtypg <= (end - 1) / PAGE_SIZE; dirtypg++) { |
3509 | if (NBPGDIRTY(bp, dirtypg)) { | |
2d21ac55 | 3510 | break; |
0a7de745 A |
3511 | } |
3512 | } | |
2d21ac55 A |
3513 | |
3514 | /* if start is at beginning of page, try */ | |
3515 | /* to get any preceeding pages as well. */ | |
3516 | if (!(start & PAGE_MASK)) { | |
3517 | /* stop at next dirty/valid page or start of block */ | |
0a7de745 A |
3518 | for (; start > 0; start -= PAGE_SIZE) { |
3519 | if (NBPGVALID(bp, ((start - 1) / PAGE_SIZE))) { | |
2d21ac55 | 3520 | break; |
0a7de745 A |
3521 | } |
3522 | } | |
2d21ac55 A |
3523 | } |
3524 | ||
3525 | NFS_BUF_MAP(bp); | |
3526 | /* setup uio for read(s) */ | |
3527 | boff = NBOFF(bp); | |
b0d623f7 | 3528 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, |
0a7de745 | 3529 | &auio_buf, sizeof(auio_buf)); |
2d21ac55 | 3530 | |
0a7de745 | 3531 | if (dirtypg <= (end - 1) / PAGE_SIZE) { |
2d21ac55 A |
3532 | /* there's a dirty page in the way, so just do two reads */ |
3533 | /* we'll read the preceding data here */ | |
b0d623f7 | 3534 | uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ); |
f427ee49 | 3535 | NFS_UIO_ADDIOV(auio, CAST_USER_ADDR_T(bp->nb_data + start), on - start); |
b0d623f7 | 3536 | error = nfs_read_rpc(np, auio, ctx); |
6d2010ae A |
3537 | if (error) { |
3538 | /* couldn't read the data, so treat buffer as synchronous NOCACHE */ | |
0a7de745 | 3539 | SET(bp->nb_flags, (NB_NOCACHE | NB_STABLE)); |
6d2010ae A |
3540 | goto skipread; |
3541 | } | |
b0d623f7 A |
3542 | if (uio_resid(auio) > 0) { |
3543 | FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee01); | |
3544 | bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio)); | |
2d21ac55 A |
3545 | } |
3546 | if (!error) { | |
3547 | /* update validoff/validend if necessary */ | |
0a7de745 | 3548 | if ((bp->nb_validoff < 0) || (bp->nb_validoff > start)) { |
2d21ac55 | 3549 | bp->nb_validoff = start; |
0a7de745 A |
3550 | } |
3551 | if ((bp->nb_validend < 0) || (bp->nb_validend < on)) { | |
2d21ac55 | 3552 | bp->nb_validend = on; |
0a7de745 A |
3553 | } |
3554 | if ((off_t)np->n_size > boff + bp->nb_validend) { | |
f427ee49 | 3555 | bp->nb_validend = MIN(np->n_size - (boff + start), biosize); |
0a7de745 | 3556 | } |
2d21ac55 | 3557 | /* validate any pages before the write offset */ |
0a7de745 A |
3558 | for (; start < on / PAGE_SIZE; start += PAGE_SIZE) { |
3559 | NBPGVALID_SET(bp, start / PAGE_SIZE); | |
3560 | } | |
2d21ac55 A |
3561 | } |
3562 | /* adjust start to read any trailing data */ | |
0a7de745 | 3563 | start = on + n; |
2d21ac55 A |
3564 | } |
3565 | ||
3566 | /* if end is at end of page, try to */ | |
3567 | /* get any following pages as well. */ | |
3568 | if (!(end & PAGE_MASK)) { | |
3569 | /* stop at next valid page or end of block */ | |
0a7de745 A |
3570 | for (; end < biosize; end += PAGE_SIZE) { |
3571 | if (NBPGVALID(bp, end / PAGE_SIZE)) { | |
2d21ac55 | 3572 | break; |
0a7de745 A |
3573 | } |
3574 | } | |
2d21ac55 A |
3575 | } |
3576 | ||
0a7de745 | 3577 | if (((boff + start) >= (off_t)np->n_size) || |
2d21ac55 A |
3578 | ((start >= on) && ((boff + on + n) >= (off_t)np->n_size))) { |
3579 | /* | |
3580 | * Either this entire read is beyond the current EOF | |
3581 | * or the range that we won't be modifying (on+n...end) | |
3582 | * is all beyond the current EOF. | |
3583 | * No need to make a trip across the network to | |
3584 | * read nothing. So, just zero the buffer instead. | |
3585 | */ | |
3586 | FSDBG(516, bp, start, end - start, 0xd00dee00); | |
f427ee49 | 3587 | NFS_BZERO(bp->nb_data + start, end - start); |
2d21ac55 | 3588 | error = 0; |
6d2010ae | 3589 | } else { |
2d21ac55 | 3590 | /* now we'll read the (rest of the) data */ |
b0d623f7 | 3591 | uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ); |
f427ee49 | 3592 | NFS_UIO_ADDIOV(auio, CAST_USER_ADDR_T(bp->nb_data + start), end - start); |
b0d623f7 | 3593 | error = nfs_read_rpc(np, auio, ctx); |
6d2010ae A |
3594 | if (error) { |
3595 | /* couldn't read the data, so treat buffer as synchronous NOCACHE */ | |
0a7de745 | 3596 | SET(bp->nb_flags, (NB_NOCACHE | NB_STABLE)); |
6d2010ae A |
3597 | goto skipread; |
3598 | } | |
b0d623f7 A |
3599 | if (uio_resid(auio) > 0) { |
3600 | FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee02); | |
3601 | bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio)); | |
2d21ac55 A |
3602 | } |
3603 | } | |
3604 | if (!error) { | |
3605 | /* update validoff/validend if necessary */ | |
0a7de745 | 3606 | if ((bp->nb_validoff < 0) || (bp->nb_validoff > start)) { |
2d21ac55 | 3607 | bp->nb_validoff = start; |
0a7de745 A |
3608 | } |
3609 | if ((bp->nb_validend < 0) || (bp->nb_validend < end)) { | |
2d21ac55 | 3610 | bp->nb_validend = end; |
0a7de745 A |
3611 | } |
3612 | if ((off_t)np->n_size > boff + bp->nb_validend) { | |
f427ee49 | 3613 | bp->nb_validend = MIN(np->n_size - (boff + start), biosize); |
0a7de745 | 3614 | } |
2d21ac55 | 3615 | /* validate any pages before the write offset's page */ |
f427ee49 | 3616 | for (; start < (off_t)trunc_page_64(on); start += PAGE_SIZE) { |
0a7de745 A |
3617 | NBPGVALID_SET(bp, start / PAGE_SIZE); |
3618 | } | |
2d21ac55 | 3619 | /* validate any pages after the range of pages being written to */ |
f427ee49 | 3620 | for (; (end - 1) > (off_t)round_page_64(on + n - 1); end -= PAGE_SIZE) { |
0a7de745 A |
3621 | NBPGVALID_SET(bp, (end - 1) / PAGE_SIZE); |
3622 | } | |
2d21ac55 A |
3623 | } |
3624 | /* Note: pages being written to will be validated when written */ | |
3625 | } | |
3626 | } | |
6d2010ae | 3627 | skipread: |
2d21ac55 A |
3628 | |
3629 | if (ISSET(bp->nb_flags, NB_ERROR)) { | |
3630 | error = bp->nb_error; | |
3631 | nfs_buf_release(bp, 1); | |
3632 | goto out; | |
3633 | } | |
3634 | ||
b0d623f7 | 3635 | nfs_node_lock_force(np); |
2d21ac55 | 3636 | np->n_flag |= NMODIFIED; |
b0d623f7 | 3637 | nfs_node_unlock(np); |
2d21ac55 A |
3638 | |
3639 | NFS_BUF_MAP(bp); | |
f427ee49 A |
3640 | if (n < 0) { |
3641 | error = EINVAL; | |
3642 | } else { | |
3643 | n32 = n > INT_MAX ? INT_MAX : (int)n; | |
3644 | error = uiomove(bp->nb_data + on, n32, uio); | |
3645 | if (!error && n > n32) { | |
3646 | error = uiomove(bp->nb_data + on + n32, (int)(n - n32), uio); | |
3647 | } | |
3648 | } | |
2d21ac55 A |
3649 | if (error) { |
3650 | SET(bp->nb_flags, NB_ERROR); | |
3651 | nfs_buf_release(bp, 1); | |
3652 | goto out; | |
3653 | } | |
3654 | ||
3655 | /* validate any pages written to */ | |
3656 | start = on & ~PAGE_MASK; | |
0a7de745 A |
3657 | for (; start < on + n; start += PAGE_SIZE) { |
3658 | NBPGVALID_SET(bp, start / PAGE_SIZE); | |
2d21ac55 A |
3659 | /* |
3660 | * This may seem a little weird, but we don't actually set the | |
3661 | * dirty bits for writes. This is because we keep the dirty range | |
3662 | * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for | |
3663 | * delayed writes, when we give the pages back to the VM we don't | |
3664 | * want to keep them marked dirty, because when we later write the | |
3665 | * buffer we won't be able to tell which pages were written dirty | |
3666 | * and which pages were mmapped and dirtied. | |
3667 | */ | |
3668 | } | |
3669 | if (bp->nb_dirtyend > 0) { | |
f427ee49 A |
3670 | bp->nb_dirtyoff = MIN(on, bp->nb_dirtyoff); |
3671 | bp->nb_dirtyend = MAX((on + n), bp->nb_dirtyend); | |
2d21ac55 A |
3672 | } else { |
3673 | bp->nb_dirtyoff = on; | |
3674 | bp->nb_dirtyend = on + n; | |
3675 | } | |
3676 | if (bp->nb_validend <= 0 || bp->nb_validend < bp->nb_dirtyoff || | |
3677 | bp->nb_validoff > bp->nb_dirtyend) { | |
3678 | bp->nb_validoff = bp->nb_dirtyoff; | |
3679 | bp->nb_validend = bp->nb_dirtyend; | |
e5568f75 | 3680 | } else { |
f427ee49 A |
3681 | bp->nb_validoff = MIN(bp->nb_validoff, bp->nb_dirtyoff); |
3682 | bp->nb_validend = MAX(bp->nb_validend, bp->nb_dirtyend); | |
2d21ac55 | 3683 | } |
0a7de745 | 3684 | if (!ISSET(bp->nb_flags, NB_CACHE)) { |
2d21ac55 | 3685 | nfs_buf_normalize_valid_range(np, bp); |
0a7de745 | 3686 | } |
2d21ac55 A |
3687 | |
3688 | /* | |
3689 | * Since this block is being modified, it must be written | |
3690 | * again and not just committed. | |
3691 | */ | |
3692 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { | |
b0d623f7 | 3693 | nfs_node_lock_force(np); |
2d21ac55 A |
3694 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { |
3695 | np->n_needcommitcnt--; | |
3696 | CHECK_NEEDCOMMITCNT(np); | |
e5568f75 | 3697 | } |
2d21ac55 | 3698 | CLR(bp->nb_flags, NB_NEEDCOMMIT); |
b0d623f7 | 3699 | nfs_node_unlock(np); |
e5568f75 | 3700 | } |
2d21ac55 A |
3701 | |
3702 | if (ioflag & IO_SYNC) { | |
3703 | error = nfs_buf_write(bp); | |
0a7de745 | 3704 | if (error) { |
2d21ac55 | 3705 | goto out; |
0a7de745 | 3706 | } |
f427ee49 A |
3707 | if (np->n_needcommitcnt >= NFS_A_LOT_OF_NEEDCOMMITS) { |
3708 | nfs_flushcommits(np, 1); | |
3709 | } | |
b0d623f7 | 3710 | } else if (((n + on) == biosize) || (ioflag & IO_APPEND) || |
0a7de745 | 3711 | (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) { |
2d21ac55 A |
3712 | SET(bp->nb_flags, NB_ASYNC); |
3713 | error = nfs_buf_write(bp); | |
0a7de745 | 3714 | if (error) { |
2d21ac55 | 3715 | goto out; |
0a7de745 | 3716 | } |
e5568f75 | 3717 | } else { |
2d21ac55 A |
3718 | /* If the block wasn't already delayed: charge for the write */ |
3719 | if (!ISSET(bp->nb_flags, NB_DELWRI)) { | |
3720 | proc_t p = vfs_context_proc(ctx); | |
0a7de745 | 3721 | if (p && p->p_stats) { |
b0d623f7 | 3722 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); |
0a7de745 | 3723 | } |
2d21ac55 A |
3724 | } |
3725 | nfs_buf_write_delayed(bp); | |
e5568f75 | 3726 | } |
cb323159 | 3727 | |
b0d623f7 | 3728 | } while (uio_resid(uio) > 0 && n > 0); |
2d21ac55 A |
3729 | |
3730 | out: | |
b0d623f7 | 3731 | nfs_node_lock_force(np); |
2d21ac55 | 3732 | np->n_wrbusy--; |
ea3f0419 A |
3733 | if ((ioflag & IO_SYNC) && !np->n_wrbusy && !np->n_numoutput) { |
3734 | np->n_flag &= ~NMODIFIED; | |
3735 | } | |
b0d623f7 | 3736 | nfs_node_unlock(np); |
2d21ac55 | 3737 | nfs_data_unlock(np); |
b0d623f7 | 3738 | FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error); |
0a7de745 | 3739 | return error; |
1c79356b A |
3740 | } |
3741 | ||
2d21ac55 | 3742 | |
1c79356b | 3743 | /* |
2d21ac55 | 3744 | * NFS write call |
1c79356b A |
3745 | */ |
3746 | int | |
2d21ac55 A |
3747 | nfs_write_rpc( |
3748 | nfsnode_t np, | |
b0d623f7 | 3749 | uio_t uio, |
2d21ac55 A |
3750 | vfs_context_t ctx, |
3751 | int *iomodep, | |
3752 | uint64_t *wverfp) | |
3753 | { | |
b0d623f7 | 3754 | return nfs_write_rpc2(np, uio, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp); |
2d21ac55 A |
3755 | } |
3756 | ||
3757 | int | |
3758 | nfs_write_rpc2( | |
3759 | nfsnode_t np, | |
b0d623f7 | 3760 | uio_t uio, |
2d21ac55 | 3761 | thread_t thd, |
91447636 | 3762 | kauth_cred_t cred, |
2d21ac55 | 3763 | int *iomodep, |
8f6c56a5 | 3764 | uint64_t *wverfp) |
1c79356b | 3765 | { |
55e303ae | 3766 | struct nfsmount *nmp; |
b0d623f7 | 3767 | int error = 0, nfsvers; |
f427ee49 A |
3768 | int wverfset, commit = 0, committed; |
3769 | uint64_t wverf = 0, wverf2 = 0; | |
3770 | size_t nmwsize, totalsize, tsiz, len, rlen = 0; | |
3771 | struct nfsreq *req; | |
cb323159 A |
3772 | #if CONFIG_NFS4 |
3773 | uint32_t stategenid = 0, restart = 0; | |
3774 | #endif | |
3775 | uint32_t vrestart = 0; | |
39236c6e | 3776 | uio_t uio_save = NULL; |
1c79356b A |
3777 | |
3778 | #if DIAGNOSTIC | |
2d21ac55 | 3779 | /* XXX limitation based on need to back up uio on short write */ |
0a7de745 | 3780 | if (uio_iovcnt(uio) != 1) { |
2d21ac55 | 3781 | panic("nfs3_write_rpc: iovcnt > 1"); |
0a7de745 | 3782 | } |
1c79356b | 3783 | #endif |
b0d623f7 | 3784 | FSDBG_TOP(537, np, uio_offset(uio), uio_resid(uio), *iomodep); |
2d21ac55 | 3785 | nmp = NFSTONMP(np); |
0a7de745 A |
3786 | if (nfs_mount_gone(nmp)) { |
3787 | return ENXIO; | |
3788 | } | |
2d21ac55 A |
3789 | nfsvers = nmp->nm_vers; |
3790 | nmwsize = nmp->nm_wsize; | |
3791 | ||
b0d623f7 | 3792 | wverfset = 0; |
2d21ac55 A |
3793 | committed = NFS_WRITE_FILESYNC; |
3794 | ||
b0d623f7 A |
3795 | totalsize = tsiz = uio_resid(uio); |
3796 | if ((nfsvers == NFS_VER2) && ((uint64_t)(uio_offset(uio) + tsiz) > 0xffffffffULL)) { | |
3797 | FSDBG_BOT(537, np, uio_offset(uio), uio_resid(uio), EFBIG); | |
0a7de745 | 3798 | return EFBIG; |
55e303ae | 3799 | } |
2d21ac55 | 3800 | |
39236c6e A |
3801 | uio_save = uio_duplicate(uio); |
3802 | if (uio_save == NULL) { | |
0a7de745 | 3803 | return EIO; |
39236c6e A |
3804 | } |
3805 | ||
f427ee49 | 3806 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); |
1c79356b | 3807 | while (tsiz > 0) { |
2d21ac55 | 3808 | len = (tsiz > nmwsize) ? nmwsize : tsiz; |
b0d623f7 | 3809 | FSDBG(537, np, uio_offset(uio), len, 0); |
6d2010ae A |
3810 | if (np->n_flag & NREVOKE) { |
3811 | error = EIO; | |
3812 | break; | |
3813 | } | |
cb323159 | 3814 | #if CONFIG_NFS4 |
0a7de745 | 3815 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 3816 | stategenid = nmp->nm_stategenid; |
0a7de745 | 3817 | } |
cb323159 | 3818 | #endif |
b0d623f7 | 3819 | error = nmp->nm_funcs->nf_write_rpc_async(np, uio, len, thd, cred, *iomodep, NULL, &req); |
0a7de745 | 3820 | if (!error) { |
2d21ac55 | 3821 | error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req, &commit, &rlen, &wverf2); |
0a7de745 | 3822 | } |
2d21ac55 | 3823 | nmp = NFSTONMP(np); |
0a7de745 | 3824 | if (nfs_mount_gone(nmp)) { |
55e303ae | 3825 | error = ENXIO; |
0a7de745 | 3826 | } |
cb323159 | 3827 | #if CONFIG_NFS4 |
b0d623f7 A |
3828 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) && |
3829 | (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */ | |
3830 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
3831 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
3832 | NP(np, "nfs_write_rpc: error %d, initiating recovery", error); | |
3833 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
3834 | } |
3835 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae A |
3836 | if (np->n_flag & NREVOKE) { |
3837 | error = EIO; | |
3838 | } else { | |
0a7de745 A |
3839 | if (error == NFSERR_GRACE) { |
3840 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
3841 | } | |
3842 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 3843 | continue; |
0a7de745 | 3844 | } |
6d2010ae | 3845 | } |
b0d623f7 | 3846 | } |
cb323159 | 3847 | #endif |
0a7de745 | 3848 | if (error) { |
91447636 | 3849 | break; |
0a7de745 | 3850 | } |
2d21ac55 A |
3851 | if (nfsvers == NFS_VER2) { |
3852 | tsiz -= len; | |
3853 | continue; | |
1c79356b | 3854 | } |
1c79356b | 3855 | |
2d21ac55 A |
3856 | /* check for a short write */ |
3857 | if (rlen < len) { | |
39236c6e A |
3858 | /* Reset the uio to reflect the actual transfer */ |
3859 | *uio = *uio_save; | |
3860 | uio_update(uio, totalsize - (tsiz - rlen)); | |
2d21ac55 | 3861 | len = rlen; |
e5568f75 | 3862 | } |
fa4905b1 | 3863 | |
2d21ac55 | 3864 | /* return lowest commit level returned */ |
0a7de745 | 3865 | if (commit < committed) { |
2d21ac55 | 3866 | committed = commit; |
0a7de745 | 3867 | } |
2d21ac55 | 3868 | |
fa4905b1 | 3869 | tsiz -= len; |
2d21ac55 A |
3870 | |
3871 | /* check write verifier */ | |
3872 | if (!wverfset) { | |
3873 | wverf = wverf2; | |
3874 | wverfset = 1; | |
3875 | } else if (wverf != wverf2) { | |
3876 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 3877 | if (++vrestart > 100) { |
2d21ac55 A |
3878 | /* give up after too many restarts */ |
3879 | error = EIO; | |
3880 | break; | |
3881 | } | |
0a7de745 | 3882 | *uio = *uio_save; // Reset the uio back to the start |
2d21ac55 A |
3883 | committed = NFS_WRITE_FILESYNC; |
3884 | wverfset = 0; | |
3885 | tsiz = totalsize; | |
3886 | } | |
3887 | } | |
0a7de745 | 3888 | if (uio_save) { |
39236c6e | 3889 | uio_free(uio_save); |
0a7de745 A |
3890 | } |
3891 | if (wverfset && wverfp) { | |
2d21ac55 | 3892 | *wverfp = wverf; |
0a7de745 | 3893 | } |
2d21ac55 | 3894 | *iomodep = committed; |
0a7de745 | 3895 | if (error) { |
b0d623f7 | 3896 | uio_setresid(uio, tsiz); |
0a7de745 | 3897 | } |
f427ee49 | 3898 | NFS_ZFREE(nfs_req_zone, req); |
b0d623f7 | 3899 | FSDBG_BOT(537, np, committed, uio_resid(uio), error); |
0a7de745 | 3900 | return error; |
2d21ac55 A |
3901 | } |
3902 | ||
3903 | int | |
3904 | nfs3_write_rpc_async( | |
3905 | nfsnode_t np, | |
b0d623f7 | 3906 | uio_t uio, |
2d21ac55 A |
3907 | size_t len, |
3908 | thread_t thd, | |
3909 | kauth_cred_t cred, | |
3910 | int iomode, | |
3911 | struct nfsreq_cbinfo *cb, | |
3912 | struct nfsreq **reqp) | |
3913 | { | |
3914 | struct nfsmount *nmp; | |
6d2010ae | 3915 | mount_t mp; |
2d21ac55 | 3916 | int error = 0, nfsvers; |
2d21ac55 A |
3917 | struct nfsm_chain nmreq; |
3918 | ||
3919 | nmp = NFSTONMP(np); | |
0a7de745 A |
3920 | if (nfs_mount_gone(nmp)) { |
3921 | return ENXIO; | |
3922 | } | |
2d21ac55 A |
3923 | nfsvers = nmp->nm_vers; |
3924 | ||
6d2010ae A |
3925 | /* for async mounts, don't bother sending sync write requests */ |
3926 | if ((iomode != NFS_WRITE_UNSTABLE) && nfs_allow_async && | |
0a7de745 | 3927 | ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC)) { |
6d2010ae | 3928 | iomode = NFS_WRITE_UNSTABLE; |
0a7de745 | 3929 | } |
6d2010ae | 3930 | |
2d21ac55 A |
3931 | nfsm_chain_null(&nmreq); |
3932 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 3933 | NFSX_FH(nfsvers) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); |
2d21ac55 A |
3934 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); |
3935 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 3936 | nfsm_chain_add_64(error, &nmreq, uio_offset(uio)); |
2d21ac55 A |
3937 | nfsm_chain_add_32(error, &nmreq, len); |
3938 | nfsm_chain_add_32(error, &nmreq, iomode); | |
3939 | } else { | |
3940 | nfsm_chain_add_32(error, &nmreq, 0); | |
b0d623f7 | 3941 | nfsm_chain_add_32(error, &nmreq, uio_offset(uio)); |
2d21ac55 A |
3942 | nfsm_chain_add_32(error, &nmreq, 0); |
3943 | } | |
3944 | nfsm_chain_add_32(error, &nmreq, len); | |
3945 | nfsmout_if(error); | |
b0d623f7 | 3946 | error = nfsm_chain_add_uio(&nmreq, uio, len); |
2d21ac55 A |
3947 | nfsm_chain_build_done(error, &nmreq); |
3948 | nfsmout_if(error); | |
6d2010ae | 3949 | error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, NULL, 0, cb, reqp); |
2d21ac55 A |
3950 | nfsmout: |
3951 | nfsm_chain_cleanup(&nmreq); | |
0a7de745 | 3952 | return error; |
2d21ac55 A |
3953 | } |
3954 | ||
3955 | int | |
3956 | nfs3_write_rpc_async_finish( | |
3957 | nfsnode_t np, | |
3958 | struct nfsreq *req, | |
3959 | int *iomodep, | |
3960 | size_t *rlenp, | |
3961 | uint64_t *wverfp) | |
3962 | { | |
3963 | struct nfsmount *nmp; | |
3964 | int error = 0, lockerror = ENOENT, nfsvers, status; | |
3965 | int updatemtime = 0, wccpostattr = 0, rlen, committed = NFS_WRITE_FILESYNC; | |
3966 | u_int64_t xid, wverf; | |
3967 | mount_t mp; | |
3968 | struct nfsm_chain nmrep; | |
3969 | ||
3970 | nmp = NFSTONMP(np); | |
fe8ab488 | 3971 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 3972 | nfs_request_async_cancel(req); |
0a7de745 | 3973 | return ENXIO; |
2d21ac55 A |
3974 | } |
3975 | nfsvers = nmp->nm_vers; | |
3976 | ||
3977 | nfsm_chain_null(&nmrep); | |
3978 | ||
3979 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); | |
0a7de745 A |
3980 | if (error == EINPROGRESS) { /* async request restarted */ |
3981 | return error; | |
3982 | } | |
2d21ac55 | 3983 | nmp = NFSTONMP(np); |
0a7de745 | 3984 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 3985 | error = ENXIO; |
0a7de745 A |
3986 | } |
3987 | if (!error && (lockerror = nfs_node_lock(np))) { | |
2d21ac55 | 3988 | error = lockerror; |
0a7de745 | 3989 | } |
2d21ac55 | 3990 | if (nfsvers == NFS_VER3) { |
cb323159 | 3991 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 | 3992 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); |
0a7de745 | 3993 | if (nfstimespeccmp(&np->n_mtime, &premtime, ==)) { |
2d21ac55 | 3994 | updatemtime = 1; |
0a7de745 A |
3995 | } |
3996 | if (!error) { | |
2d21ac55 | 3997 | error = status; |
0a7de745 | 3998 | } |
2d21ac55 A |
3999 | nfsm_chain_get_32(error, &nmrep, rlen); |
4000 | nfsmout_if(error); | |
4001 | *rlenp = rlen; | |
0a7de745 | 4002 | if (rlen <= 0) { |
2d21ac55 | 4003 | error = NFSERR_IO; |
0a7de745 | 4004 | } |
2d21ac55 A |
4005 | nfsm_chain_get_32(error, &nmrep, committed); |
4006 | nfsm_chain_get_64(error, &nmrep, wverf); | |
4007 | nfsmout_if(error); | |
0a7de745 | 4008 | if (wverfp) { |
2d21ac55 | 4009 | *wverfp = wverf; |
0a7de745 | 4010 | } |
2d21ac55 A |
4011 | lck_mtx_lock(&nmp->nm_lock); |
4012 | if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) { | |
4013 | nmp->nm_verf = wverf; | |
4014 | nmp->nm_state |= NFSSTA_HASWRITEVERF; | |
4015 | } else if (nmp->nm_verf != wverf) { | |
4016 | nmp->nm_verf = wverf; | |
4017 | } | |
4018 | lck_mtx_unlock(&nmp->nm_lock); | |
4019 | } else { | |
0a7de745 | 4020 | if (!error) { |
2d21ac55 | 4021 | error = status; |
0a7de745 | 4022 | } |
6d2010ae | 4023 | nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid); |
2d21ac55 | 4024 | nfsmout_if(error); |
1c79356b | 4025 | } |
0a7de745 | 4026 | if (updatemtime) { |
2d21ac55 | 4027 | NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr); |
0a7de745 | 4028 | } |
1c79356b | 4029 | nfsmout: |
0a7de745 | 4030 | if (!lockerror) { |
b0d623f7 | 4031 | nfs_node_unlock(np); |
0a7de745 | 4032 | } |
2d21ac55 A |
4033 | nfsm_chain_cleanup(&nmrep); |
4034 | if ((committed != NFS_WRITE_FILESYNC) && nfs_allow_async && | |
0a7de745 | 4035 | ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC)) { |
2d21ac55 | 4036 | committed = NFS_WRITE_FILESYNC; |
0a7de745 | 4037 | } |
2d21ac55 | 4038 | *iomodep = committed; |
0a7de745 | 4039 | return error; |
1c79356b A |
4040 | } |
4041 | ||
4042 | /* | |
2d21ac55 A |
4043 | * NFS mknod vnode op |
4044 | * | |
4045 | * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the | |
1c79356b A |
4046 | * mode set to specify the file type and the size field for rdev. |
4047 | */ | |
b0d623f7 | 4048 | int |
2d21ac55 A |
4049 | nfs3_vnop_mknod( |
4050 | struct vnop_mknod_args /* { | |
0a7de745 A |
4051 | * struct vnodeop_desc *a_desc; |
4052 | * vnode_t a_dvp; | |
4053 | * vnode_t *a_vpp; | |
4054 | * struct componentname *a_cnp; | |
4055 | * struct vnode_attr *a_vap; | |
4056 | * vfs_context_t a_context; | |
4057 | * } */*ap) | |
1c79356b | 4058 | { |
2d21ac55 A |
4059 | vnode_t dvp = ap->a_dvp; |
4060 | vnode_t *vpp = ap->a_vpp; | |
4061 | struct componentname *cnp = ap->a_cnp; | |
4062 | struct vnode_attr *vap = ap->a_vap; | |
4063 | vfs_context_t ctx = ap->a_context; | |
4064 | vnode_t newvp = NULL; | |
4065 | nfsnode_t np = NULL; | |
4066 | struct nfsmount *nmp; | |
4067 | nfsnode_t dnp = VTONFS(dvp); | |
f427ee49 A |
4068 | struct nfs_vattr *nvattr; |
4069 | fhandle_t *fh; | |
4070 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status = 0, wccpostattr = 0; | |
cb323159 | 4071 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
b0d623f7 | 4072 | u_int32_t rdev; |
39236c6e | 4073 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
4074 | int nfsvers, gotuid, gotgid; |
4075 | struct nfsm_chain nmreq, nmrep; | |
f427ee49 | 4076 | struct nfsreq *req; |
2d21ac55 A |
4077 | |
4078 | nmp = VTONMP(dvp); | |
0a7de745 A |
4079 | if (nfs_mount_gone(nmp)) { |
4080 | return ENXIO; | |
4081 | } | |
2d21ac55 | 4082 | nfsvers = nmp->nm_vers; |
1c79356b | 4083 | |
0a7de745 A |
4084 | if (!VATTR_IS_ACTIVE(vap, va_type)) { |
4085 | return EINVAL; | |
4086 | } | |
91447636 | 4087 | if (vap->va_type == VCHR || vap->va_type == VBLK) { |
0a7de745 A |
4088 | if (!VATTR_IS_ACTIVE(vap, va_rdev)) { |
4089 | return EINVAL; | |
4090 | } | |
2d21ac55 | 4091 | rdev = vap->va_rdev; |
0a7de745 | 4092 | } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) { |
1c79356b | 4093 | rdev = 0xffffffff; |
0a7de745 A |
4094 | } else { |
4095 | return ENOTSUP; | |
4096 | } | |
4097 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { | |
4098 | return ENAMETOOLONG; | |
1c79356b | 4099 | } |
91447636 | 4100 | |
6d2010ae A |
4101 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
4102 | ||
91447636 A |
4103 | VATTR_SET_SUPPORTED(vap, va_mode); |
4104 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4105 | VATTR_SET_SUPPORTED(vap, va_gid); | |
4106 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
4107 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
4108 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
4109 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
4110 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
4111 | ||
2d21ac55 A |
4112 | nfsm_chain_null(&nmreq); |
4113 | nfsm_chain_null(&nmrep); | |
4114 | ||
f427ee49 A |
4115 | fh = zalloc(nfs_fhandle_zone); |
4116 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); | |
4117 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
4118 | ||
2d21ac55 | 4119 | nfsm_chain_build_alloc_init(error, &nmreq, |
0a7de745 A |
4120 | NFSX_FH(nfsvers) + 4 * NFSX_UNSIGNED + |
4121 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 4122 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 4123 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
4124 | if (nfsvers == NFS_VER3) { |
4125 | nfsm_chain_add_32(error, &nmreq, vtonfs_type(vap->va_type, nfsvers)); | |
cb323159 | 4126 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
1c79356b | 4127 | if (vap->va_type == VCHR || vap->va_type == VBLK) { |
2d21ac55 A |
4128 | nfsm_chain_add_32(error, &nmreq, major(vap->va_rdev)); |
4129 | nfsm_chain_add_32(error, &nmreq, minor(vap->va_rdev)); | |
1c79356b A |
4130 | } |
4131 | } else { | |
2d21ac55 | 4132 | nfsm_chain_add_v2sattr(error, &nmreq, vap, rdev); |
91447636 | 4133 | } |
2d21ac55 | 4134 | nfsm_chain_build_done(error, &nmreq); |
0a7de745 | 4135 | if (!error) { |
b0d623f7 | 4136 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
0a7de745 | 4137 | } |
2d21ac55 A |
4138 | nfsmout_if(error); |
4139 | ||
6d2010ae | 4140 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKNOD, |
0a7de745 A |
4141 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
4142 | if (!error) { | |
6d2010ae | 4143 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
0a7de745 | 4144 | } |
2d21ac55 | 4145 | |
0a7de745 | 4146 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 4147 | error = lockerror; |
0a7de745 | 4148 | } |
91447636 | 4149 | /* XXX no EEXIST kludge here? */ |
2d21ac55 A |
4150 | dxid = xid; |
4151 | if (!error && !status) { | |
4152 | if (dnp->n_flag & NNEGNCENTRIES) { | |
4153 | dnp->n_flag &= ~NNEGNCENTRIES; | |
4154 | cache_purge_negatives(dvp); | |
4155 | } | |
f427ee49 | 4156 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, fh, nvattr); |
2d21ac55 | 4157 | } |
0a7de745 | 4158 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 4159 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
4160 | } |
4161 | if (!error) { | |
2d21ac55 | 4162 | error = status; |
0a7de745 | 4163 | } |
2d21ac55 A |
4164 | nfsmout: |
4165 | nfsm_chain_cleanup(&nmreq); | |
4166 | nfsm_chain_cleanup(&nmrep); | |
4167 | ||
4168 | if (!lockerror) { | |
4169 | dnp->n_flag |= NMODIFIED; | |
4170 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4171 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 4172 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 4173 | } |
b0d623f7 A |
4174 | nfs_node_unlock(dnp); |
4175 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4176 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 4177 | } |
2d21ac55 | 4178 | |
f427ee49 A |
4179 | if (!error && fh->fh_len) { |
4180 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh->fh_data, fh->fh_len, nvattr, &xid, req->r_auth, NG_MAKEENTRY, &np); | |
0a7de745 A |
4181 | } |
4182 | if (!error && !np) { | |
2d21ac55 | 4183 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
0a7de745 A |
4184 | } |
4185 | if (!error && np) { | |
2d21ac55 | 4186 | newvp = NFSTOV(np); |
0a7de745 A |
4187 | } |
4188 | if (!busyerror) { | |
b0d623f7 | 4189 | nfs_node_clear_busy(dnp); |
0a7de745 | 4190 | } |
2d21ac55 | 4191 | |
91447636 | 4192 | if (!error && (gotuid || gotgid) && |
f427ee49 A |
4193 | (!newvp || nfs_getattrcache(np, nvattr, 0) || |
4194 | (gotuid && (nvattr->nva_uid != vap->va_uid)) || | |
4195 | (gotgid && (nvattr->nva_gid != vap->va_gid)))) { | |
91447636 A |
4196 | /* clear ID bits if server didn't use them (or we can't tell) */ |
4197 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
4198 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
4199 | } | |
1c79356b | 4200 | if (error) { |
2d21ac55 | 4201 | if (newvp) { |
b0d623f7 | 4202 | nfs_node_unlock(np); |
91447636 | 4203 | vnode_put(newvp); |
2d21ac55 | 4204 | } |
1c79356b | 4205 | } else { |
1c79356b | 4206 | *vpp = newvp; |
b0d623f7 | 4207 | nfs_node_unlock(np); |
1c79356b | 4208 | } |
f427ee49 A |
4209 | NFS_ZFREE(nfs_fhandle_zone, fh); |
4210 | NFS_ZFREE(nfs_req_zone, req); | |
4211 | FREE(nvattr, M_TEMP); | |
0a7de745 | 4212 | return error; |
1c79356b A |
4213 | } |
4214 | ||
b0d623f7 | 4215 | static uint32_t create_verf; |
1c79356b | 4216 | /* |
2d21ac55 | 4217 | * NFS file create call |
1c79356b | 4218 | */ |
b0d623f7 | 4219 | int |
2d21ac55 | 4220 | nfs3_vnop_create( |
91447636 | 4221 | struct vnop_create_args /* { |
0a7de745 A |
4222 | * struct vnodeop_desc *a_desc; |
4223 | * vnode_t a_dvp; | |
4224 | * vnode_t *a_vpp; | |
4225 | * struct componentname *a_cnp; | |
4226 | * struct vnode_attr *a_vap; | |
4227 | * vfs_context_t a_context; | |
4228 | * } */*ap) | |
1c79356b | 4229 | { |
2d21ac55 | 4230 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4231 | vnode_t dvp = ap->a_dvp; |
4232 | struct vnode_attr *vap = ap->a_vap; | |
4233 | struct componentname *cnp = ap->a_cnp; | |
f427ee49 A |
4234 | struct nfs_vattr *nvattr; |
4235 | fhandle_t *fh; | |
2d21ac55 A |
4236 | nfsnode_t np = NULL; |
4237 | struct nfsmount *nmp; | |
4238 | nfsnode_t dnp = VTONFS(dvp); | |
4239 | vnode_t newvp = NULL; | |
f427ee49 | 4240 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status = 0, wccpostattr = 0, fmode = 0; |
cb323159 | 4241 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 | 4242 | int nfsvers, gotuid, gotgid; |
f427ee49 | 4243 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
4244 | uint32_t val; |
4245 | struct nfsm_chain nmreq, nmrep; | |
f427ee49 A |
4246 | struct nfsreq *req; |
4247 | struct nfs_dulookup *dul; | |
cb323159 A |
4248 | int dul_in_progress = 0; |
4249 | int namedattrs; | |
91447636 | 4250 | |
2d21ac55 | 4251 | nmp = VTONMP(dvp); |
0a7de745 A |
4252 | if (nfs_mount_gone(nmp)) { |
4253 | return ENXIO; | |
4254 | } | |
2d21ac55 | 4255 | nfsvers = nmp->nm_vers; |
cb323159 | 4256 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
1c79356b | 4257 | |
0a7de745 A |
4258 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
4259 | return ENAMETOOLONG; | |
4260 | } | |
91447636 | 4261 | |
6d2010ae A |
4262 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
4263 | ||
91447636 A |
4264 | VATTR_SET_SUPPORTED(vap, va_mode); |
4265 | VATTR_SET_SUPPORTED(vap, va_uid); | |
4266 | VATTR_SET_SUPPORTED(vap, va_gid); | |
4267 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
4268 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
4269 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
4270 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
4271 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
1c79356b | 4272 | |
cb323159 A |
4273 | if ((vap->va_vaflags & VA_EXCLUSIVE) |
4274 | ) { | |
1c79356b | 4275 | fmode |= O_EXCL; |
0a7de745 | 4276 | if (!VATTR_IS_ACTIVE(vap, va_access_time) || !VATTR_IS_ACTIVE(vap, va_modify_time)) { |
6d2010ae | 4277 | vap->va_vaflags |= VA_UTIMES_NULL; |
0a7de745 | 4278 | } |
6d2010ae | 4279 | } |
2d21ac55 | 4280 | |
f427ee49 A |
4281 | fh = zalloc(nfs_fhandle_zone); |
4282 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); | |
4283 | MALLOC(dul, struct nfs_dulookup *, sizeof(*dul), M_TEMP, M_WAITOK); | |
4284 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
4285 | ||
1c79356b | 4286 | again: |
b0d623f7 | 4287 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
cb323159 | 4288 | if (!namedattrs) { |
f427ee49 | 4289 | nfs_dulookup_init(dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
cb323159 | 4290 | } |
2d21ac55 A |
4291 | |
4292 | nfsm_chain_null(&nmreq); | |
4293 | nfsm_chain_null(&nmrep); | |
4294 | ||
4295 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
4296 | NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED + |
4297 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 4298 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 4299 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 | 4300 | if (nfsvers == NFS_VER3) { |
1c79356b | 4301 | if (fmode & O_EXCL) { |
2d21ac55 | 4302 | nfsm_chain_add_32(error, &nmreq, NFS_CREATE_EXCLUSIVE); |
b0d623f7 | 4303 | lck_rw_lock_shared(in_ifaddr_rwlock); |
0a7de745 | 4304 | if (!TAILQ_EMPTY(&in_ifaddrhead)) { |
2d21ac55 | 4305 | val = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr; |
0a7de745 | 4306 | } else { |
2d21ac55 | 4307 | val = create_verf; |
0a7de745 | 4308 | } |
b0d623f7 | 4309 | lck_rw_done(in_ifaddr_rwlock); |
2d21ac55 A |
4310 | nfsm_chain_add_32(error, &nmreq, val); |
4311 | ++create_verf; | |
4312 | nfsm_chain_add_32(error, &nmreq, create_verf); | |
1c79356b | 4313 | } else { |
2d21ac55 | 4314 | nfsm_chain_add_32(error, &nmreq, NFS_CREATE_UNCHECKED); |
cb323159 | 4315 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
1c79356b A |
4316 | } |
4317 | } else { | |
2d21ac55 | 4318 | nfsm_chain_add_v2sattr(error, &nmreq, vap, 0); |
1c79356b | 4319 | } |
2d21ac55 A |
4320 | nfsm_chain_build_done(error, &nmreq); |
4321 | nfsmout_if(error); | |
2d21ac55 A |
4322 | |
4323 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_CREATE, | |
0a7de745 | 4324 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
1c79356b | 4325 | if (!error) { |
cb323159 | 4326 | if (!namedattrs) { |
f427ee49 | 4327 | nfs_dulookup_start(dul, dnp, ctx); |
cb323159 A |
4328 | dul_in_progress = 1; |
4329 | } | |
2d21ac55 A |
4330 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
4331 | } | |
4332 | ||
0a7de745 | 4333 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 4334 | error = lockerror; |
0a7de745 | 4335 | } |
2d21ac55 A |
4336 | dxid = xid; |
4337 | if (!error && !status) { | |
4338 | if (dnp->n_flag & NNEGNCENTRIES) { | |
4339 | dnp->n_flag &= ~NNEGNCENTRIES; | |
4340 | cache_purge_negatives(dvp); | |
4341 | } | |
f427ee49 | 4342 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, fh, nvattr); |
2d21ac55 | 4343 | } |
0a7de745 | 4344 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 4345 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
4346 | } |
4347 | if (!error) { | |
2d21ac55 | 4348 | error = status; |
0a7de745 | 4349 | } |
2d21ac55 A |
4350 | nfsmout: |
4351 | nfsm_chain_cleanup(&nmreq); | |
4352 | nfsm_chain_cleanup(&nmrep); | |
4353 | ||
4354 | if (!lockerror) { | |
4355 | dnp->n_flag |= NMODIFIED; | |
4356 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4357 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 4358 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 4359 | } |
b0d623f7 A |
4360 | nfs_node_unlock(dnp); |
4361 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 4362 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 4363 | } |
2d21ac55 | 4364 | |
f427ee49 A |
4365 | if (!error && fh->fh_len) { |
4366 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh->fh_data, fh->fh_len, nvattr, &xid, req->r_auth, NG_MAKEENTRY, &np); | |
0a7de745 A |
4367 | } |
4368 | if (!error && !np) { | |
2d21ac55 | 4369 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
0a7de745 A |
4370 | } |
4371 | if (!error && np) { | |
2d21ac55 | 4372 | newvp = NFSTOV(np); |
0a7de745 | 4373 | } |
2d21ac55 | 4374 | |
cb323159 | 4375 | if (dul_in_progress) { |
f427ee49 | 4376 | nfs_dulookup_finish(dul, dnp, ctx); |
cb323159 | 4377 | } |
0a7de745 | 4378 | if (!busyerror) { |
b0d623f7 | 4379 | nfs_node_clear_busy(dnp); |
0a7de745 | 4380 | } |
2d21ac55 | 4381 | |
1c79356b | 4382 | if (error) { |
2d21ac55 | 4383 | if ((nfsvers == NFS_VER3) && (fmode & O_EXCL) && (error == NFSERR_NOTSUPP)) { |
1c79356b A |
4384 | fmode &= ~O_EXCL; |
4385 | goto again; | |
4386 | } | |
2d21ac55 | 4387 | if (newvp) { |
b0d623f7 | 4388 | nfs_node_unlock(np); |
91447636 | 4389 | vnode_put(newvp); |
2d21ac55 A |
4390 | } |
4391 | } else if ((nfsvers == NFS_VER3) && (fmode & O_EXCL)) { | |
b0d623f7 A |
4392 | nfs_node_unlock(np); |
4393 | error = nfs3_setattr_rpc(np, vap, ctx); | |
91447636 A |
4394 | if (error && (gotuid || gotgid)) { |
4395 | /* it's possible the server didn't like our attempt to set IDs. */ | |
4396 | /* so, let's try it again without those */ | |
4397 | VATTR_CLEAR_ACTIVE(vap, va_uid); | |
4398 | VATTR_CLEAR_ACTIVE(vap, va_gid); | |
b0d623f7 | 4399 | error = nfs3_setattr_rpc(np, vap, ctx); |
91447636 | 4400 | } |
0a7de745 | 4401 | if (error) { |
91447636 | 4402 | vnode_put(newvp); |
0a7de745 | 4403 | } else { |
b0d623f7 | 4404 | nfs_node_lock_force(np); |
0a7de745 | 4405 | } |
ccc36f2f | 4406 | } |
0a7de745 | 4407 | if (!error) { |
1c79356b | 4408 | *ap->a_vpp = newvp; |
0a7de745 | 4409 | } |
91447636 | 4410 | if (!error && (gotuid || gotgid) && |
f427ee49 A |
4411 | (!newvp || nfs_getattrcache(np, nvattr, 0) || |
4412 | (gotuid && (nvattr->nva_uid != vap->va_uid)) || | |
4413 | (gotgid && (nvattr->nva_gid != vap->va_gid)))) { | |
91447636 A |
4414 | /* clear ID bits if server didn't use them (or we can't tell) */ |
4415 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
4416 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
4417 | } | |
0a7de745 | 4418 | if (!error) { |
b0d623f7 | 4419 | nfs_node_unlock(np); |
0a7de745 | 4420 | } |
f427ee49 A |
4421 | NFS_ZFREE(nfs_fhandle_zone, fh); |
4422 | NFS_ZFREE(nfs_req_zone, req); | |
4423 | FREE(dul, M_TEMP); | |
4424 | FREE(nvattr, M_TEMP); | |
0a7de745 | 4425 | return error; |
1c79356b A |
4426 | } |
4427 | ||
4428 | /* | |
2d21ac55 A |
4429 | * NFS file remove call |
4430 | * To try and make NFS semantics closer to UFS semantics, a file that has | |
1c79356b A |
4431 | * other processes using the vnode is renamed instead of removed and then |
4432 | * removed later on the last close. | |
91447636 | 4433 | * - If vnode_isinuse() |
1c79356b A |
4434 | * If a rename is not already in the works |
4435 | * call nfs_sillyrename() to set it up | |
4436 | * else | |
2d21ac55 | 4437 | * do the remove RPC |
1c79356b | 4438 | */ |
b0d623f7 | 4439 | int |
2d21ac55 | 4440 | nfs_vnop_remove( |
91447636 | 4441 | struct vnop_remove_args /* { |
0a7de745 A |
4442 | * struct vnodeop_desc *a_desc; |
4443 | * vnode_t a_dvp; | |
4444 | * vnode_t a_vp; | |
4445 | * struct componentname *a_cnp; | |
4446 | * int a_flags; | |
4447 | * vfs_context_t a_context; | |
4448 | * } */*ap) | |
1c79356b | 4449 | { |
2d21ac55 | 4450 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4451 | vnode_t vp = ap->a_vp; |
4452 | vnode_t dvp = ap->a_dvp; | |
4453 | struct componentname *cnp = ap->a_cnp; | |
2d21ac55 A |
4454 | nfsnode_t dnp = VTONFS(dvp); |
4455 | nfsnode_t np = VTONFS(vp); | |
6d2010ae | 4456 | int error = 0, nfsvers, namedattrs, inuse, gotattr = 0, flushed = 0, setsize = 0; |
f427ee49 | 4457 | struct nfs_vattr *nvattr; |
2d21ac55 | 4458 | struct nfsmount *nmp; |
f427ee49 | 4459 | struct nfs_dulookup *dul; |
2d21ac55 A |
4460 | |
4461 | /* XXX prevent removing a sillyrenamed file? */ | |
4462 | ||
4463 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
4464 | if (nfs_mount_gone(nmp)) { |
4465 | return ENXIO; | |
4466 | } | |
f427ee49 A |
4467 | |
4468 | if (vnode_isdir(vp)) { | |
4469 | return EPERM; | |
4470 | } | |
4471 | ||
2d21ac55 | 4472 | nfsvers = nmp->nm_vers; |
6d2010ae | 4473 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
f427ee49 A |
4474 | MALLOC(dul, struct nfs_dulookup *, sizeof(*dul), M_TEMP, M_WAITOK); |
4475 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
2d21ac55 A |
4476 | |
4477 | again_relock: | |
b0d623f7 | 4478 | error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx)); |
0a7de745 | 4479 | if (error) { |
f427ee49 | 4480 | goto out_free; |
0a7de745 | 4481 | } |
1c79356b | 4482 | |
2d21ac55 | 4483 | /* lock the node while we remove the file */ |
c3c9b80d | 4484 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4485 | while (np->n_hflag & NHLOCKED) { |
4486 | np->n_hflag |= NHLOCKWANT; | |
c3c9b80d | 4487 | msleep(np, &nfs_node_hash_mutex, PINOD, "nfs_remove", NULL); |
2d21ac55 A |
4488 | } |
4489 | np->n_hflag |= NHLOCKED; | |
c3c9b80d | 4490 | lck_mtx_unlock(&nfs_node_hash_mutex); |
9bccf70c | 4491 | |
0a7de745 | 4492 | if (!namedattrs) { |
f427ee49 | 4493 | nfs_dulookup_init(dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
0a7de745 | 4494 | } |
f427ee49 | 4495 | |
2d21ac55 A |
4496 | again: |
4497 | inuse = vnode_isinuse(vp, 0); | |
4498 | if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && inuse) { | |
55e303ae | 4499 | /* Caller requested Carbon delete semantics, but file is busy */ |
2d21ac55 A |
4500 | error = EBUSY; |
4501 | goto out; | |
55e303ae | 4502 | } |
2d21ac55 | 4503 | if (inuse && !gotattr) { |
f427ee49 A |
4504 | if (nfs_getattr(np, nvattr, ctx, NGA_CACHED)) { |
4505 | nvattr->nva_nlink = 1; | |
0a7de745 | 4506 | } |
2d21ac55 A |
4507 | gotattr = 1; |
4508 | goto again; | |
4509 | } | |
f427ee49 | 4510 | if (!inuse || (np->n_sillyrename && (nvattr->nva_nlink > 1))) { |
2d21ac55 A |
4511 | if (!inuse && !flushed) { /* flush all the buffers first */ |
4512 | /* unlock the node */ | |
c3c9b80d | 4513 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4514 | np->n_hflag &= ~NHLOCKED; |
4515 | if (np->n_hflag & NHLOCKWANT) { | |
4516 | np->n_hflag &= ~NHLOCKWANT; | |
4517 | wakeup(np); | |
4518 | } | |
c3c9b80d | 4519 | lck_mtx_unlock(&nfs_node_hash_mutex); |
b0d623f7 | 4520 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 A |
4521 | error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1); |
4522 | FSDBG(260, np, np->n_size, np->n_vattr.nva_size, 0xf00d0011); | |
4523 | flushed = 1; | |
4524 | if (error == EINTR) { | |
b0d623f7 | 4525 | nfs_node_lock_force(np); |
2d21ac55 | 4526 | NATTRINVALIDATE(np); |
b0d623f7 | 4527 | nfs_node_unlock(np); |
f427ee49 | 4528 | goto out_free; |
2d21ac55 | 4529 | } |
0a7de745 | 4530 | if (!namedattrs) { |
f427ee49 | 4531 | nfs_dulookup_finish(dul, dnp, ctx); |
0a7de745 | 4532 | } |
2d21ac55 A |
4533 | goto again_relock; |
4534 | } | |
cb323159 | 4535 | #if CONFIG_NFS4 |
0a7de745 | 4536 | if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK)) { |
6d2010ae | 4537 | nfs4_delegation_return(np, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
0a7de745 | 4538 | } |
cb323159 | 4539 | #endif |
1c79356b A |
4540 | /* |
4541 | * Purge the name cache so that the chance of a lookup for | |
4542 | * the name succeeding while the remove is in progress is | |
483a1d10 | 4543 | * minimized. |
1c79356b | 4544 | */ |
b0d623f7 | 4545 | nfs_name_cache_purge(dnp, np, cnp, ctx); |
2d21ac55 | 4546 | |
0a7de745 | 4547 | if (!namedattrs) { |
f427ee49 | 4548 | nfs_dulookup_start(dul, dnp, ctx); |
0a7de745 | 4549 | } |
2d21ac55 | 4550 | |
1c79356b | 4551 | /* Do the rpc */ |
2d21ac55 | 4552 | error = nmp->nm_funcs->nf_remove_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen, |
0a7de745 | 4553 | vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
2d21ac55 | 4554 | |
1c79356b A |
4555 | /* |
4556 | * Kludge City: If the first reply to the remove rpc is lost.. | |
4557 | * the reply to the retransmitted request will be ENOENT | |
4558 | * since the file was in fact removed | |
4559 | * Therefore, we cheat and return success. | |
4560 | */ | |
0a7de745 | 4561 | if (error == ENOENT) { |
1c79356b | 4562 | error = 0; |
0a7de745 | 4563 | } |
2d21ac55 A |
4564 | |
4565 | if (!error && !inuse && !np->n_sillyrename) { | |
55e303ae | 4566 | /* |
2d21ac55 | 4567 | * removal succeeded, it's not in use, and not silly renamed so |
55e303ae A |
4568 | * remove nfsnode from hash now so we can't accidentally find it |
4569 | * again if another object gets created with the same filehandle | |
4570 | * before this vnode gets reclaimed | |
4571 | */ | |
c3c9b80d | 4572 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4573 | if (np->n_hflag & NHHASHED) { |
4574 | LIST_REMOVE(np, n_hash); | |
4575 | np->n_hflag &= ~NHHASHED; | |
4576 | FSDBG(266, 0, np, np->n_flag, 0xb1eb1e); | |
4577 | } | |
c3c9b80d | 4578 | lck_mtx_unlock(&nfs_node_hash_mutex); |
2d21ac55 | 4579 | /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */ |
91447636 | 4580 | /* clear all flags other than these */ |
b0d623f7 | 4581 | nfs_node_lock_force(np); |
2d21ac55 | 4582 | np->n_flag &= (NMODIFIED); |
2d21ac55 | 4583 | NATTRINVALIDATE(np); |
b0d623f7 A |
4584 | nfs_node_unlock(np); |
4585 | vnode_recycle(vp); | |
2d21ac55 A |
4586 | setsize = 1; |
4587 | } else { | |
b0d623f7 | 4588 | nfs_node_lock_force(np); |
2d21ac55 | 4589 | NATTRINVALIDATE(np); |
b0d623f7 | 4590 | nfs_node_unlock(np); |
55e303ae | 4591 | } |
1c79356b | 4592 | } else if (!np->n_sillyrename) { |
0a7de745 | 4593 | if (!namedattrs) { |
f427ee49 | 4594 | nfs_dulookup_start(dul, dnp, ctx); |
0a7de745 | 4595 | } |
2d21ac55 | 4596 | error = nfs_sillyrename(dnp, np, cnp, ctx); |
b0d623f7 | 4597 | nfs_node_lock_force(np); |
2d21ac55 | 4598 | NATTRINVALIDATE(np); |
b0d623f7 | 4599 | nfs_node_unlock(np); |
2d21ac55 | 4600 | } else { |
b0d623f7 | 4601 | nfs_node_lock_force(np); |
2d21ac55 | 4602 | NATTRINVALIDATE(np); |
b0d623f7 | 4603 | nfs_node_unlock(np); |
0a7de745 | 4604 | if (!namedattrs) { |
f427ee49 | 4605 | nfs_dulookup_start(dul, dnp, ctx); |
0a7de745 | 4606 | } |
1c79356b | 4607 | } |
1c79356b | 4608 | |
b0d623f7 | 4609 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae | 4610 | nfs_getattr(dnp, NULL, ctx, NGA_CACHED); |
0a7de745 | 4611 | if (!namedattrs) { |
f427ee49 | 4612 | nfs_dulookup_finish(dul, dnp, ctx); |
0a7de745 | 4613 | } |
2d21ac55 A |
4614 | out: |
4615 | /* unlock the node */ | |
c3c9b80d | 4616 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4617 | np->n_hflag &= ~NHLOCKED; |
4618 | if (np->n_hflag & NHLOCKWANT) { | |
4619 | np->n_hflag &= ~NHLOCKWANT; | |
4620 | wakeup(np); | |
4621 | } | |
c3c9b80d | 4622 | lck_mtx_unlock(&nfs_node_hash_mutex); |
b0d623f7 | 4623 | nfs_node_clear_busy2(dnp, np); |
0a7de745 | 4624 | if (setsize) { |
2d21ac55 | 4625 | ubc_setsize(vp, 0); |
0a7de745 | 4626 | } |
f427ee49 A |
4627 | out_free: |
4628 | FREE(dul, M_TEMP); | |
4629 | FREE(nvattr, M_TEMP); | |
0a7de745 | 4630 | return error; |
1c79356b A |
4631 | } |
4632 | ||
4633 | /* | |
2d21ac55 | 4634 | * NFS silly-renamed file removal function called from nfs_vnop_inactive |
1c79356b A |
4635 | */ |
4636 | int | |
2d21ac55 | 4637 | nfs_removeit(struct nfs_sillyrename *nsp) |
1c79356b | 4638 | { |
2d21ac55 | 4639 | struct nfsmount *nmp = NFSTONMP(nsp->nsr_dnp); |
0a7de745 A |
4640 | if (nfs_mount_gone(nmp)) { |
4641 | return ENXIO; | |
4642 | } | |
2d21ac55 | 4643 | return nmp->nm_funcs->nf_remove_rpc(nsp->nsr_dnp, nsp->nsr_name, nsp->nsr_namlen, NULL, nsp->nsr_cred); |
1c79356b A |
4644 | } |
4645 | ||
4646 | /* | |
2d21ac55 | 4647 | * NFS remove rpc, called from nfs_remove() and nfs_removeit(). |
1c79356b | 4648 | */ |
2d21ac55 A |
4649 | int |
4650 | nfs3_remove_rpc( | |
4651 | nfsnode_t dnp, | |
4652 | char *name, | |
4653 | int namelen, | |
4654 | thread_t thd, | |
4655 | kauth_cred_t cred) | |
1c79356b | 4656 | { |
f427ee49 | 4657 | int error = 0, lockerror = ENOENT, status = 0, wccpostattr = 0; |
cb323159 | 4658 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4659 | struct nfsmount *nmp; |
4660 | int nfsvers; | |
fa4905b1 | 4661 | u_int64_t xid; |
2d21ac55 | 4662 | struct nfsm_chain nmreq, nmrep; |
1c79356b | 4663 | |
2d21ac55 | 4664 | nmp = NFSTONMP(dnp); |
0a7de745 A |
4665 | if (nfs_mount_gone(nmp)) { |
4666 | return ENXIO; | |
4667 | } | |
2d21ac55 | 4668 | nfsvers = nmp->nm_vers; |
0a7de745 A |
4669 | if ((nfsvers == NFS_VER2) && (namelen > NFS_MAXNAMLEN)) { |
4670 | return ENAMETOOLONG; | |
4671 | } | |
55e303ae | 4672 | |
2d21ac55 A |
4673 | nfsm_chain_null(&nmreq); |
4674 | nfsm_chain_null(&nmrep); | |
4675 | ||
4676 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 4677 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen)); |
2d21ac55 | 4678 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 4679 | nfsm_chain_add_name(error, &nmreq, name, namelen, nmp); |
2d21ac55 A |
4680 | nfsm_chain_build_done(error, &nmreq); |
4681 | nfsmout_if(error); | |
4682 | ||
6d2010ae | 4683 | error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, NULL, 0, &nmrep, &xid, &status); |
2d21ac55 | 4684 | |
0a7de745 | 4685 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 4686 | error = lockerror; |
0a7de745 A |
4687 | } |
4688 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 4689 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid); |
0a7de745 | 4690 | } |
b0d623f7 | 4691 | nfsmout_if(error); |
2d21ac55 | 4692 | dnp->n_flag |= NMODIFIED; |
483a1d10 | 4693 | /* if directory hadn't changed, update namecache mtime */ |
0a7de745 | 4694 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 4695 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 A |
4696 | } |
4697 | if (!wccpostattr) { | |
2d21ac55 | 4698 | NATTRINVALIDATE(dnp); |
0a7de745 A |
4699 | } |
4700 | if (!error) { | |
2d21ac55 | 4701 | error = status; |
0a7de745 | 4702 | } |
2d21ac55 | 4703 | nfsmout: |
0a7de745 | 4704 | if (!lockerror) { |
b0d623f7 | 4705 | nfs_node_unlock(dnp); |
0a7de745 | 4706 | } |
2d21ac55 A |
4707 | nfsm_chain_cleanup(&nmreq); |
4708 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 4709 | return error; |
1c79356b A |
4710 | } |
4711 | ||
4712 | /* | |
2d21ac55 | 4713 | * NFS file rename call |
1c79356b | 4714 | */ |
b0d623f7 | 4715 | int |
2d21ac55 | 4716 | nfs_vnop_rename( |
91447636 | 4717 | struct vnop_rename_args /* { |
0a7de745 A |
4718 | * struct vnodeop_desc *a_desc; |
4719 | * vnode_t a_fdvp; | |
4720 | * vnode_t a_fvp; | |
4721 | * struct componentname *a_fcnp; | |
4722 | * vnode_t a_tdvp; | |
4723 | * vnode_t a_tvp; | |
4724 | * struct componentname *a_tcnp; | |
4725 | * vfs_context_t a_context; | |
4726 | * } */*ap) | |
1c79356b | 4727 | { |
2d21ac55 | 4728 | vfs_context_t ctx = ap->a_context; |
91447636 | 4729 | vnode_t fdvp = ap->a_fdvp; |
2d21ac55 | 4730 | vnode_t fvp = ap->a_fvp; |
91447636 | 4731 | vnode_t tdvp = ap->a_tdvp; |
2d21ac55 A |
4732 | vnode_t tvp = ap->a_tvp; |
4733 | nfsnode_t fdnp, fnp, tdnp, tnp; | |
91447636 A |
4734 | struct componentname *tcnp = ap->a_tcnp; |
4735 | struct componentname *fcnp = ap->a_fcnp; | |
0a7de745 | 4736 | int error, nfsvers, inuse = 0, tvprecycle = 0, locked = 0; |
91447636 | 4737 | mount_t fmp, tdmp, tmp; |
f427ee49 | 4738 | struct nfs_vattr *nvattr; |
2d21ac55 | 4739 | struct nfsmount *nmp; |
91447636 | 4740 | |
2d21ac55 A |
4741 | fdnp = VTONFS(fdvp); |
4742 | fnp = VTONFS(fvp); | |
4743 | tdnp = VTONFS(tdvp); | |
91447636 | 4744 | tnp = tvp ? VTONFS(tvp) : NULL; |
1c79356b | 4745 | |
2d21ac55 | 4746 | nmp = NFSTONMP(fdnp); |
0a7de745 A |
4747 | if (nfs_mount_gone(nmp)) { |
4748 | return ENXIO; | |
4749 | } | |
2d21ac55 A |
4750 | nfsvers = nmp->nm_vers; |
4751 | ||
b0d623f7 | 4752 | error = nfs_node_set_busy4(fdnp, fnp, tdnp, tnp, vfs_context_thread(ctx)); |
0a7de745 A |
4753 | if (error) { |
4754 | return error; | |
4755 | } | |
2d21ac55 | 4756 | |
f427ee49 A |
4757 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); |
4758 | ||
2d21ac55 A |
4759 | if (tvp && (tvp != fvp)) { |
4760 | /* lock the node while we rename over the existing file */ | |
c3c9b80d | 4761 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4762 | while (tnp->n_hflag & NHLOCKED) { |
4763 | tnp->n_hflag |= NHLOCKWANT; | |
c3c9b80d | 4764 | msleep(tnp, &nfs_node_hash_mutex, PINOD, "nfs_rename", NULL); |
2d21ac55 A |
4765 | } |
4766 | tnp->n_hflag |= NHLOCKED; | |
c3c9b80d | 4767 | lck_mtx_unlock(&nfs_node_hash_mutex); |
2d21ac55 A |
4768 | locked = 1; |
4769 | } | |
4770 | ||
1c79356b | 4771 | /* Check for cross-device rename */ |
91447636 A |
4772 | fmp = vnode_mount(fvp); |
4773 | tmp = tvp ? vnode_mount(tvp) : NULL; | |
4774 | tdmp = vnode_mount(tdvp); | |
4775 | if ((fmp != tdmp) || (tvp && (fmp != tmp))) { | |
1c79356b A |
4776 | error = EXDEV; |
4777 | goto out; | |
4778 | } | |
4779 | ||
2d21ac55 A |
4780 | /* XXX prevent renaming from/over a sillyrenamed file? */ |
4781 | ||
1c79356b A |
4782 | /* |
4783 | * If the tvp exists and is in use, sillyrename it before doing the | |
4784 | * rename of the new file over it. | |
4785 | * XXX Can't sillyrename a directory. | |
9bccf70c A |
4786 | * Don't sillyrename if source and target are same vnode (hard |
4787 | * links or case-variants) | |
1c79356b | 4788 | */ |
0a7de745 | 4789 | if (tvp && (tvp != fvp)) { |
91447636 | 4790 | inuse = vnode_isinuse(tvp, 0); |
0a7de745 | 4791 | } |
2d21ac55 A |
4792 | if (inuse && !tnp->n_sillyrename && (vnode_vtype(tvp) != VDIR)) { |
4793 | error = nfs_sillyrename(tdnp, tnp, tcnp, ctx); | |
4794 | if (error) { | |
9bccf70c A |
4795 | /* sillyrename failed. Instead of pressing on, return error */ |
4796 | goto out; /* should not be ENOENT. */ | |
4797 | } else { | |
4798 | /* sillyrename succeeded.*/ | |
9bccf70c A |
4799 | tvp = NULL; |
4800 | } | |
cb323159 A |
4801 | } |
4802 | #if CONFIG_NFS4 | |
4803 | else if (tvp && (nmp->nm_vers >= NFS_VER4) && (tnp->n_openflags & N_DELEG_MASK)) { | |
6d2010ae | 4804 | nfs4_delegation_return(tnp, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx)); |
1c79356b | 4805 | } |
cb323159 | 4806 | #endif |
2d21ac55 | 4807 | error = nmp->nm_funcs->nf_rename_rpc(fdnp, fcnp->cn_nameptr, fcnp->cn_namelen, |
0a7de745 | 4808 | tdnp, tcnp->cn_nameptr, tcnp->cn_namelen, ctx); |
1c79356b | 4809 | |
91447636 A |
4810 | /* |
4811 | * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. | |
4812 | */ | |
0a7de745 | 4813 | if (error == ENOENT) { |
91447636 | 4814 | error = 0; |
0a7de745 | 4815 | } |
91447636 | 4816 | |
2d21ac55 | 4817 | if (tvp && (tvp != fvp) && !tnp->n_sillyrename) { |
b0d623f7 | 4818 | nfs_node_lock_force(tnp); |
2d21ac55 | 4819 | tvprecycle = (!error && !vnode_isinuse(tvp, 0) && |
f427ee49 | 4820 | (nfs_getattrcache(tnp, nvattr, 0) || (nvattr->nva_nlink == 1))); |
b0d623f7 | 4821 | nfs_node_unlock(tnp); |
c3c9b80d | 4822 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4823 | if (tvprecycle && (tnp->n_hflag & NHHASHED)) { |
4824 | /* | |
4825 | * remove nfsnode from hash now so we can't accidentally find it | |
4826 | * again if another object gets created with the same filehandle | |
4827 | * before this vnode gets reclaimed | |
4828 | */ | |
4829 | LIST_REMOVE(tnp, n_hash); | |
4830 | tnp->n_hflag &= ~NHHASHED; | |
4831 | FSDBG(266, 0, tnp, tnp->n_flag, 0xb1eb1e); | |
4832 | } | |
c3c9b80d | 4833 | lck_mtx_unlock(&nfs_node_hash_mutex); |
55e303ae | 4834 | } |
2d21ac55 | 4835 | |
91447636 | 4836 | /* purge the old name cache entries and enter the new one */ |
b0d623f7 | 4837 | nfs_name_cache_purge(fdnp, fnp, fcnp, ctx); |
9bccf70c | 4838 | if (tvp) { |
b0d623f7 | 4839 | nfs_name_cache_purge(tdnp, tnp, tcnp, ctx); |
2d21ac55 A |
4840 | if (tvprecycle) { |
4841 | /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */ | |
91447636 | 4842 | /* clear all flags other than these */ |
b0d623f7 | 4843 | nfs_node_lock_force(tnp); |
2d21ac55 | 4844 | tnp->n_flag &= (NMODIFIED); |
b0d623f7 | 4845 | nfs_node_unlock(tnp); |
91447636 A |
4846 | vnode_recycle(tvp); |
4847 | } | |
9bccf70c | 4848 | } |
2d21ac55 | 4849 | if (!error) { |
b0d623f7 | 4850 | nfs_node_lock_force(tdnp); |
2d21ac55 A |
4851 | if (tdnp->n_flag & NNEGNCENTRIES) { |
4852 | tdnp->n_flag &= ~NNEGNCENTRIES; | |
4853 | cache_purge_negatives(tdvp); | |
4854 | } | |
b0d623f7 A |
4855 | nfs_node_unlock(tdnp); |
4856 | nfs_node_lock_force(fnp); | |
91447636 | 4857 | cache_enter(tdvp, fvp, tcnp); |
0a7de745 | 4858 | if (tdvp != fdvp) { /* update parent pointer */ |
2d21ac55 A |
4859 | if (fnp->n_parent && !vnode_get(fnp->n_parent)) { |
4860 | /* remove ref from old parent */ | |
4861 | vnode_rele(fnp->n_parent); | |
4862 | vnode_put(fnp->n_parent); | |
4863 | } | |
4864 | fnp->n_parent = tdvp; | |
4865 | if (tdvp && !vnode_get(tdvp)) { | |
4866 | /* add ref to new parent */ | |
4867 | vnode_ref(tdvp); | |
4868 | vnode_put(tdvp); | |
4869 | } else { | |
4870 | fnp->n_parent = NULL; | |
4871 | } | |
4872 | } | |
b0d623f7 | 4873 | nfs_node_unlock(fnp); |
2d21ac55 | 4874 | } |
1c79356b | 4875 | out: |
b0d623f7 | 4876 | /* nfs_getattr() will check changed and purge caches */ |
6d2010ae A |
4877 | nfs_getattr(fdnp, NULL, ctx, NGA_CACHED); |
4878 | nfs_getattr(tdnp, NULL, ctx, NGA_CACHED); | |
2d21ac55 A |
4879 | if (locked) { |
4880 | /* unlock node */ | |
c3c9b80d | 4881 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
4882 | tnp->n_hflag &= ~NHLOCKED; |
4883 | if (tnp->n_hflag & NHLOCKWANT) { | |
4884 | tnp->n_hflag &= ~NHLOCKWANT; | |
4885 | wakeup(tnp); | |
4886 | } | |
c3c9b80d | 4887 | lck_mtx_unlock(&nfs_node_hash_mutex); |
2d21ac55 | 4888 | } |
b0d623f7 | 4889 | nfs_node_clear_busy4(fdnp, fnp, tdnp, tnp); |
f427ee49 | 4890 | FREE(nvattr, M_TEMP); |
0a7de745 | 4891 | return error; |
1c79356b A |
4892 | } |
4893 | ||
4894 | /* | |
2d21ac55 | 4895 | * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename(). |
1c79356b | 4896 | */ |
2d21ac55 A |
4897 | int |
4898 | nfs3_rename_rpc( | |
4899 | nfsnode_t fdnp, | |
4900 | char *fnameptr, | |
4901 | int fnamelen, | |
4902 | nfsnode_t tdnp, | |
4903 | char *tnameptr, | |
4904 | int tnamelen, | |
4905 | vfs_context_t ctx) | |
1c79356b | 4906 | { |
f427ee49 | 4907 | int error = 0, lockerror = ENOENT, status = 0, fwccpostattr = 0, twccpostattr = 0; |
cb323159 | 4908 | struct timespec fpremtime = { .tv_sec = 0, .tv_nsec = 0 }, tpremtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4909 | struct nfsmount *nmp; |
4910 | int nfsvers; | |
4911 | u_int64_t xid, txid; | |
4912 | struct nfsm_chain nmreq, nmrep; | |
1c79356b | 4913 | |
2d21ac55 | 4914 | nmp = NFSTONMP(fdnp); |
0a7de745 A |
4915 | if (nfs_mount_gone(nmp)) { |
4916 | return ENXIO; | |
4917 | } | |
2d21ac55 A |
4918 | nfsvers = nmp->nm_vers; |
4919 | if ((nfsvers == NFS_VER2) && | |
0a7de745 A |
4920 | ((fnamelen > NFS_MAXNAMLEN) || (tnamelen > NFS_MAXNAMLEN))) { |
4921 | return ENAMETOOLONG; | |
4922 | } | |
2d21ac55 A |
4923 | |
4924 | nfsm_chain_null(&nmreq); | |
4925 | nfsm_chain_null(&nmrep); | |
4926 | ||
4927 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
4928 | (NFSX_FH(nfsvers) + NFSX_UNSIGNED) * 2 + |
4929 | nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen)); | |
2d21ac55 | 4930 | nfsm_chain_add_fh(error, &nmreq, nfsvers, fdnp->n_fhp, fdnp->n_fhsize); |
6d2010ae | 4931 | nfsm_chain_add_name(error, &nmreq, fnameptr, fnamelen, nmp); |
2d21ac55 | 4932 | nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize); |
6d2010ae | 4933 | nfsm_chain_add_name(error, &nmreq, tnameptr, tnamelen, nmp); |
2d21ac55 A |
4934 | nfsm_chain_build_done(error, &nmreq); |
4935 | nfsmout_if(error); | |
4936 | ||
6d2010ae | 4937 | error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, NULL, &nmrep, &xid, &status); |
2d21ac55 | 4938 | |
0a7de745 | 4939 | if ((lockerror = nfs_node_lock2(fdnp, tdnp))) { |
b0d623f7 | 4940 | error = lockerror; |
0a7de745 | 4941 | } |
2d21ac55 A |
4942 | if (nfsvers == NFS_VER3) { |
4943 | txid = xid; | |
4944 | nfsm_chain_get_wcc_data(error, &nmrep, fdnp, &fpremtime, &fwccpostattr, &xid); | |
4945 | nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &tpremtime, &twccpostattr, &txid); | |
4946 | } | |
0a7de745 | 4947 | if (!error) { |
2d21ac55 | 4948 | error = status; |
0a7de745 | 4949 | } |
2d21ac55 A |
4950 | nfsmout: |
4951 | nfsm_chain_cleanup(&nmreq); | |
4952 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 A |
4953 | if (!lockerror) { |
4954 | fdnp->n_flag |= NMODIFIED; | |
4955 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4956 | if (nfstimespeccmp(&fdnp->n_ncmtime, &fpremtime, ==)) { |
b0d623f7 | 4957 | NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &fdnp->n_vattr); |
0a7de745 A |
4958 | } |
4959 | if (!fwccpostattr) { | |
b0d623f7 | 4960 | NATTRINVALIDATE(fdnp); |
0a7de745 | 4961 | } |
b0d623f7 A |
4962 | tdnp->n_flag |= NMODIFIED; |
4963 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 4964 | if (nfstimespeccmp(&tdnp->n_ncmtime, &tpremtime, ==)) { |
b0d623f7 | 4965 | NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr); |
0a7de745 A |
4966 | } |
4967 | if (!twccpostattr) { | |
b0d623f7 | 4968 | NATTRINVALIDATE(tdnp); |
0a7de745 | 4969 | } |
b0d623f7 A |
4970 | nfs_node_unlock2(fdnp, tdnp); |
4971 | } | |
0a7de745 | 4972 | return error; |
1c79356b A |
4973 | } |
4974 | ||
4975 | /* | |
2d21ac55 | 4976 | * NFS hard link create call |
1c79356b | 4977 | */ |
b0d623f7 | 4978 | int |
2d21ac55 | 4979 | nfs3_vnop_link( |
91447636 | 4980 | struct vnop_link_args /* { |
0a7de745 A |
4981 | * struct vnodeop_desc *a_desc; |
4982 | * vnode_t a_vp; | |
4983 | * vnode_t a_tdvp; | |
4984 | * struct componentname *a_cnp; | |
4985 | * vfs_context_t a_context; | |
4986 | * } */*ap) | |
1c79356b | 4987 | { |
2d21ac55 | 4988 | vfs_context_t ctx = ap->a_context; |
91447636 A |
4989 | vnode_t vp = ap->a_vp; |
4990 | vnode_t tdvp = ap->a_tdvp; | |
4991 | struct componentname *cnp = ap->a_cnp; | |
f427ee49 | 4992 | int error = 0, lockerror = ENOENT, status = 0, wccpostattr = 0, attrflag = 0; |
cb323159 | 4993 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
4994 | struct nfsmount *nmp; |
4995 | nfsnode_t np = VTONFS(vp); | |
4996 | nfsnode_t tdnp = VTONFS(tdvp); | |
4997 | int nfsvers; | |
4998 | u_int64_t xid, txid; | |
4999 | struct nfsm_chain nmreq, nmrep; | |
1c79356b | 5000 | |
0a7de745 A |
5001 | if (vnode_mount(vp) != vnode_mount(tdvp)) { |
5002 | return EXDEV; | |
5003 | } | |
1c79356b | 5004 | |
2d21ac55 | 5005 | nmp = VTONMP(vp); |
0a7de745 A |
5006 | if (nfs_mount_gone(nmp)) { |
5007 | return ENXIO; | |
5008 | } | |
2d21ac55 | 5009 | nfsvers = nmp->nm_vers; |
0a7de745 A |
5010 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
5011 | return ENAMETOOLONG; | |
5012 | } | |
55e303ae | 5013 | |
1c79356b A |
5014 | /* |
5015 | * Push all writes to the server, so that the attribute cache | |
5016 | * doesn't get "out of sync" with the server. | |
5017 | * XXX There should be a better way! | |
5018 | */ | |
2d21ac55 | 5019 | nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), V_IGNORE_WRITEERR); |
1c79356b | 5020 | |
b0d623f7 | 5021 | error = nfs_node_set_busy2(tdnp, np, vfs_context_thread(ctx)); |
0a7de745 A |
5022 | if (error) { |
5023 | return error; | |
5024 | } | |
fa4905b1 | 5025 | |
2d21ac55 A |
5026 | nfsm_chain_null(&nmreq); |
5027 | nfsm_chain_null(&nmrep); | |
5028 | ||
5029 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 5030 | NFSX_FH(nfsvers) * 2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); |
2d21ac55 A |
5031 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); |
5032 | nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize); | |
6d2010ae | 5033 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
5034 | nfsm_chain_build_done(error, &nmreq); |
5035 | nfsmout_if(error); | |
6d2010ae | 5036 | error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx, NULL, &nmrep, &xid, &status); |
b0d623f7 A |
5037 | |
5038 | if ((lockerror = nfs_node_lock2(tdnp, np))) { | |
5039 | error = lockerror; | |
5040 | goto nfsmout; | |
5041 | } | |
2d21ac55 A |
5042 | if (nfsvers == NFS_VER3) { |
5043 | txid = xid; | |
5044 | nfsm_chain_postop_attr_update_flag(error, &nmrep, np, attrflag, &xid); | |
5045 | nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &premtime, &wccpostattr, &txid); | |
1c79356b | 5046 | } |
0a7de745 | 5047 | if (!error) { |
2d21ac55 | 5048 | error = status; |
0a7de745 | 5049 | } |
2d21ac55 A |
5050 | nfsmout: |
5051 | nfsm_chain_cleanup(&nmreq); | |
5052 | nfsm_chain_cleanup(&nmrep); | |
b0d623f7 | 5053 | if (!lockerror) { |
0a7de745 | 5054 | if (!attrflag) { |
b0d623f7 | 5055 | NATTRINVALIDATE(np); |
0a7de745 | 5056 | } |
b0d623f7 A |
5057 | tdnp->n_flag |= NMODIFIED; |
5058 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5059 | if (nfstimespeccmp(&tdnp->n_ncmtime, &premtime, ==)) { |
b0d623f7 | 5060 | NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr); |
0a7de745 A |
5061 | } |
5062 | if (!wccpostattr) { | |
b0d623f7 | 5063 | NATTRINVALIDATE(tdnp); |
0a7de745 | 5064 | } |
b0d623f7 A |
5065 | if (!error && (tdnp->n_flag & NNEGNCENTRIES)) { |
5066 | tdnp->n_flag &= ~NNEGNCENTRIES; | |
5067 | cache_purge_negatives(tdvp); | |
5068 | } | |
5069 | nfs_node_unlock2(tdnp, np); | |
2d21ac55 | 5070 | } |
b0d623f7 | 5071 | nfs_node_clear_busy2(tdnp, np); |
1c79356b A |
5072 | /* |
5073 | * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. | |
5074 | */ | |
0a7de745 | 5075 | if (error == EEXIST) { |
1c79356b | 5076 | error = 0; |
0a7de745 A |
5077 | } |
5078 | return error; | |
1c79356b A |
5079 | } |
5080 | ||
5081 | /* | |
2d21ac55 | 5082 | * NFS symbolic link create call |
1c79356b | 5083 | */ |
b0d623f7 | 5084 | int |
2d21ac55 | 5085 | nfs3_vnop_symlink( |
91447636 | 5086 | struct vnop_symlink_args /* { |
0a7de745 A |
5087 | * struct vnodeop_desc *a_desc; |
5088 | * vnode_t a_dvp; | |
5089 | * vnode_t *a_vpp; | |
5090 | * struct componentname *a_cnp; | |
5091 | * struct vnode_attr *a_vap; | |
5092 | * char *a_target; | |
5093 | * vfs_context_t a_context; | |
5094 | * } */*ap) | |
1c79356b | 5095 | { |
2d21ac55 | 5096 | vfs_context_t ctx = ap->a_context; |
91447636 A |
5097 | vnode_t dvp = ap->a_dvp; |
5098 | struct vnode_attr *vap = ap->a_vap; | |
5099 | struct componentname *cnp = ap->a_cnp; | |
f427ee49 A |
5100 | struct nfs_vattr *nvattr; |
5101 | fhandle_t *fh; | |
5102 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status = 0, wccpostattr = 0; | |
5103 | size_t slen; | |
cb323159 | 5104 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
5105 | vnode_t newvp = NULL; |
5106 | int nfsvers, gotuid, gotgid; | |
39236c6e | 5107 | u_int64_t xid = 0, dxid; |
2d21ac55 A |
5108 | nfsnode_t np = NULL; |
5109 | nfsnode_t dnp = VTONFS(dvp); | |
5110 | struct nfsmount *nmp; | |
5111 | struct nfsm_chain nmreq, nmrep; | |
f427ee49 A |
5112 | struct nfsreq *req; |
5113 | struct nfs_dulookup *dul; | |
cb323159 A |
5114 | int namedattrs; |
5115 | int dul_in_progress = 0; | |
91447636 | 5116 | |
2d21ac55 | 5117 | nmp = VTONMP(dvp); |
0a7de745 A |
5118 | if (nfs_mount_gone(nmp)) { |
5119 | return ENXIO; | |
5120 | } | |
2d21ac55 | 5121 | nfsvers = nmp->nm_vers; |
cb323159 | 5122 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
1c79356b | 5123 | |
1c79356b | 5124 | slen = strlen(ap->a_target); |
2d21ac55 | 5125 | if ((nfsvers == NFS_VER2) && |
0a7de745 A |
5126 | ((cnp->cn_namelen > NFS_MAXNAMLEN) || (slen > NFS_MAXPATHLEN))) { |
5127 | return ENAMETOOLONG; | |
5128 | } | |
91447636 | 5129 | |
6d2010ae A |
5130 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
5131 | ||
91447636 A |
5132 | VATTR_SET_SUPPORTED(vap, va_mode); |
5133 | VATTR_SET_SUPPORTED(vap, va_uid); | |
5134 | VATTR_SET_SUPPORTED(vap, va_gid); | |
5135 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
5136 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
5137 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
5138 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
5139 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
5140 | ||
f427ee49 A |
5141 | fh = zalloc(nfs_fhandle_zone); |
5142 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); | |
5143 | MALLOC(dul, struct nfs_dulookup *, sizeof(*dul), M_TEMP, M_WAITOK); | |
5144 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
5145 | ||
b0d623f7 | 5146 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
cb323159 | 5147 | if (!namedattrs) { |
f427ee49 | 5148 | nfs_dulookup_init(dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
cb323159 | 5149 | } |
2d21ac55 A |
5150 | |
5151 | nfsm_chain_null(&nmreq); | |
5152 | nfsm_chain_null(&nmrep); | |
5153 | ||
5154 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
5155 | NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED + |
5156 | nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 5157 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 5158 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
0a7de745 | 5159 | if (nfsvers == NFS_VER3) { |
cb323159 | 5160 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
0a7de745 | 5161 | } |
6d2010ae | 5162 | nfsm_chain_add_name(error, &nmreq, ap->a_target, slen, nmp); |
0a7de745 | 5163 | if (nfsvers == NFS_VER2) { |
2d21ac55 | 5164 | nfsm_chain_add_v2sattr(error, &nmreq, vap, -1); |
0a7de745 | 5165 | } |
2d21ac55 A |
5166 | nfsm_chain_build_done(error, &nmreq); |
5167 | nfsmout_if(error); | |
2d21ac55 A |
5168 | |
5169 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_SYMLINK, | |
0a7de745 | 5170 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 | 5171 | if (!error) { |
cb323159 | 5172 | if (!namedattrs) { |
f427ee49 | 5173 | nfs_dulookup_start(dul, dnp, ctx); |
cb323159 A |
5174 | dul_in_progress = 1; |
5175 | } | |
2d21ac55 A |
5176 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
5177 | } | |
5178 | ||
0a7de745 | 5179 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 5180 | error = lockerror; |
0a7de745 | 5181 | } |
2d21ac55 A |
5182 | dxid = xid; |
5183 | if (!error && !status) { | |
5184 | if (dnp->n_flag & NNEGNCENTRIES) { | |
5185 | dnp->n_flag &= ~NNEGNCENTRIES; | |
5186 | cache_purge_negatives(dvp); | |
91447636 | 5187 | } |
0a7de745 | 5188 | if (nfsvers == NFS_VER3) { |
f427ee49 | 5189 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, fh, nvattr); |
0a7de745 | 5190 | } else { |
f427ee49 | 5191 | fh->fh_len = 0; |
0a7de745 | 5192 | } |
1c79356b | 5193 | } |
0a7de745 | 5194 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 5195 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
5196 | } |
5197 | if (!error) { | |
2d21ac55 | 5198 | error = status; |
0a7de745 | 5199 | } |
2d21ac55 A |
5200 | nfsmout: |
5201 | nfsm_chain_cleanup(&nmreq); | |
5202 | nfsm_chain_cleanup(&nmrep); | |
fa4905b1 | 5203 | |
2d21ac55 A |
5204 | if (!lockerror) { |
5205 | dnp->n_flag |= NMODIFIED; | |
5206 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5207 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 5208 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 5209 | } |
b0d623f7 A |
5210 | nfs_node_unlock(dnp); |
5211 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5212 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
1c79356b | 5213 | } |
55e303ae | 5214 | |
f427ee49 A |
5215 | if (!error && fh->fh_len) { |
5216 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh->fh_data, fh->fh_len, nvattr, &xid, req->r_auth, NG_MAKEENTRY, &np); | |
0a7de745 A |
5217 | } |
5218 | if (!error && np) { | |
2d21ac55 | 5219 | newvp = NFSTOV(np); |
0a7de745 | 5220 | } |
2d21ac55 | 5221 | |
cb323159 | 5222 | if (dul_in_progress) { |
f427ee49 | 5223 | nfs_dulookup_finish(dul, dnp, ctx); |
cb323159 | 5224 | } |
91447636 | 5225 | |
1c79356b | 5226 | /* |
91447636 A |
5227 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry |
5228 | * if we can succeed in looking up the symlink. | |
1c79356b | 5229 | */ |
2d21ac55 | 5230 | if ((error == EEXIST) || (!error && !newvp)) { |
91447636 | 5231 | if (newvp) { |
b0d623f7 | 5232 | nfs_node_unlock(np); |
91447636 A |
5233 | vnode_put(newvp); |
5234 | newvp = NULL; | |
5235 | } | |
2d21ac55 | 5236 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
91447636 A |
5237 | if (!error) { |
5238 | newvp = NFSTOV(np); | |
0a7de745 | 5239 | if (vnode_vtype(newvp) != VLNK) { |
91447636 | 5240 | error = EEXIST; |
0a7de745 | 5241 | } |
91447636 A |
5242 | } |
5243 | } | |
0a7de745 | 5244 | if (!busyerror) { |
b0d623f7 | 5245 | nfs_node_clear_busy(dnp); |
0a7de745 | 5246 | } |
91447636 | 5247 | if (!error && (gotuid || gotgid) && |
f427ee49 A |
5248 | (!newvp || nfs_getattrcache(np, nvattr, 0) || |
5249 | (gotuid && (nvattr->nva_uid != vap->va_uid)) || | |
5250 | (gotgid && (nvattr->nva_gid != vap->va_gid)))) { | |
91447636 A |
5251 | /* clear ID bits if server didn't use them (or we can't tell) */ |
5252 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
5253 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
5254 | } | |
5255 | if (error) { | |
2d21ac55 | 5256 | if (newvp) { |
b0d623f7 | 5257 | nfs_node_unlock(np); |
91447636 | 5258 | vnode_put(newvp); |
2d21ac55 | 5259 | } |
91447636 | 5260 | } else { |
b0d623f7 | 5261 | nfs_node_unlock(np); |
91447636 A |
5262 | *ap->a_vpp = newvp; |
5263 | } | |
f427ee49 A |
5264 | NFS_ZFREE(nfs_fhandle_zone, fh); |
5265 | NFS_ZFREE(nfs_req_zone, req); | |
5266 | FREE(dul, M_TEMP); | |
5267 | FREE(nvattr, M_TEMP); | |
0a7de745 | 5268 | return error; |
1c79356b A |
5269 | } |
5270 | ||
5271 | /* | |
2d21ac55 | 5272 | * NFS make dir call |
1c79356b | 5273 | */ |
b0d623f7 | 5274 | int |
2d21ac55 | 5275 | nfs3_vnop_mkdir( |
91447636 | 5276 | struct vnop_mkdir_args /* { |
0a7de745 A |
5277 | * struct vnodeop_desc *a_desc; |
5278 | * vnode_t a_dvp; | |
5279 | * vnode_t *a_vpp; | |
5280 | * struct componentname *a_cnp; | |
5281 | * struct vnode_attr *a_vap; | |
5282 | * vfs_context_t a_context; | |
5283 | * } */*ap) | |
1c79356b | 5284 | { |
2d21ac55 | 5285 | vfs_context_t ctx = ap->a_context; |
91447636 A |
5286 | vnode_t dvp = ap->a_dvp; |
5287 | struct vnode_attr *vap = ap->a_vap; | |
5288 | struct componentname *cnp = ap->a_cnp; | |
f427ee49 | 5289 | struct nfs_vattr *nvattr; |
2d21ac55 A |
5290 | nfsnode_t np = NULL; |
5291 | struct nfsmount *nmp; | |
5292 | nfsnode_t dnp = VTONFS(dvp); | |
5293 | vnode_t newvp = NULL; | |
f427ee49 | 5294 | int error = 0, lockerror = ENOENT, busyerror = ENOENT, status = 0, wccpostattr = 0; |
cb323159 | 5295 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 | 5296 | int nfsvers, gotuid, gotgid; |
0a7de745 | 5297 | u_int64_t xid = 0, dxid; |
f427ee49 | 5298 | fhandle_t *fh; |
2d21ac55 | 5299 | struct nfsm_chain nmreq, nmrep; |
f427ee49 A |
5300 | struct nfsreq *req; |
5301 | struct nfs_dulookup *dul; | |
cb323159 A |
5302 | int namedattrs; |
5303 | int dul_in_progress = 0; | |
1c79356b | 5304 | |
2d21ac55 | 5305 | nmp = VTONMP(dvp); |
0a7de745 A |
5306 | if (nfs_mount_gone(nmp)) { |
5307 | return ENXIO; | |
5308 | } | |
2d21ac55 | 5309 | nfsvers = nmp->nm_vers; |
cb323159 A |
5310 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
5311 | ||
0a7de745 A |
5312 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
5313 | return ENAMETOOLONG; | |
5314 | } | |
91447636 | 5315 | |
6d2010ae A |
5316 | nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx); |
5317 | ||
91447636 A |
5318 | VATTR_SET_SUPPORTED(vap, va_mode); |
5319 | VATTR_SET_SUPPORTED(vap, va_uid); | |
5320 | VATTR_SET_SUPPORTED(vap, va_gid); | |
5321 | VATTR_SET_SUPPORTED(vap, va_data_size); | |
5322 | VATTR_SET_SUPPORTED(vap, va_access_time); | |
5323 | VATTR_SET_SUPPORTED(vap, va_modify_time); | |
5324 | gotuid = VATTR_IS_ACTIVE(vap, va_uid); | |
5325 | gotgid = VATTR_IS_ACTIVE(vap, va_gid); | |
5326 | ||
f427ee49 A |
5327 | fh = zalloc(nfs_fhandle_zone); |
5328 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); | |
5329 | MALLOC(dul, struct nfs_dulookup *, sizeof(*dul), M_TEMP, M_WAITOK); | |
5330 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
5331 | ||
b0d623f7 | 5332 | error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx)); |
cb323159 | 5333 | if (!namedattrs) { |
f427ee49 | 5334 | nfs_dulookup_init(dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
cb323159 | 5335 | } |
2d21ac55 A |
5336 | |
5337 | nfsm_chain_null(&nmreq); | |
5338 | nfsm_chain_null(&nmrep); | |
5339 | ||
5340 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 A |
5341 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + |
5342 | nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers)); | |
2d21ac55 | 5343 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 5344 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
0a7de745 | 5345 | if (nfsvers == NFS_VER3) { |
cb323159 | 5346 | nfsm_chain_add_v3sattr(nmp, error, &nmreq, vap); |
0a7de745 | 5347 | } else { |
2d21ac55 | 5348 | nfsm_chain_add_v2sattr(error, &nmreq, vap, -1); |
0a7de745 | 5349 | } |
2d21ac55 A |
5350 | nfsm_chain_build_done(error, &nmreq); |
5351 | nfsmout_if(error); | |
2d21ac55 A |
5352 | |
5353 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKDIR, | |
0a7de745 | 5354 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 | 5355 | if (!error) { |
cb323159 | 5356 | if (!namedattrs) { |
f427ee49 | 5357 | nfs_dulookup_start(dul, dnp, ctx); |
cb323159 A |
5358 | dul_in_progress = 1; |
5359 | } | |
2d21ac55 | 5360 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
1c79356b | 5361 | } |
2d21ac55 | 5362 | |
0a7de745 | 5363 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 5364 | error = lockerror; |
0a7de745 | 5365 | } |
fa4905b1 | 5366 | dxid = xid; |
2d21ac55 A |
5367 | if (!error && !status) { |
5368 | if (dnp->n_flag & NNEGNCENTRIES) { | |
5369 | dnp->n_flag &= ~NNEGNCENTRIES; | |
5370 | cache_purge_negatives(dvp); | |
5371 | } | |
f427ee49 | 5372 | error = nfsm_chain_get_fh_attr(nmp, &nmrep, dnp, ctx, nfsvers, &xid, fh, nvattr); |
2d21ac55 | 5373 | } |
0a7de745 | 5374 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 5375 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid); |
0a7de745 A |
5376 | } |
5377 | if (!error) { | |
2d21ac55 | 5378 | error = status; |
0a7de745 | 5379 | } |
2d21ac55 A |
5380 | nfsmout: |
5381 | nfsm_chain_cleanup(&nmreq); | |
5382 | nfsm_chain_cleanup(&nmrep); | |
5383 | ||
5384 | if (!lockerror) { | |
5385 | dnp->n_flag |= NMODIFIED; | |
5386 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5387 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
2d21ac55 | 5388 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 5389 | } |
b0d623f7 A |
5390 | nfs_node_unlock(dnp); |
5391 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5392 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
2d21ac55 A |
5393 | } |
5394 | ||
f427ee49 A |
5395 | if (!error && fh->fh_len) { |
5396 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh->fh_data, fh->fh_len, nvattr, &xid, req->r_auth, NG_MAKEENTRY, &np); | |
0a7de745 A |
5397 | } |
5398 | if (!error && np) { | |
2d21ac55 | 5399 | newvp = NFSTOV(np); |
0a7de745 | 5400 | } |
2d21ac55 | 5401 | |
cb323159 | 5402 | if (dul_in_progress) { |
f427ee49 | 5403 | nfs_dulookup_finish(dul, dnp, ctx); |
cb323159 | 5404 | } |
2d21ac55 | 5405 | |
1c79356b A |
5406 | /* |
5407 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry | |
5408 | * if we can succeed in looking up the directory. | |
5409 | */ | |
b0d623f7 | 5410 | if ((error == EEXIST) || (!error && !newvp)) { |
1c79356b | 5411 | if (newvp) { |
b0d623f7 | 5412 | nfs_node_unlock(np); |
91447636 A |
5413 | vnode_put(newvp); |
5414 | newvp = NULL; | |
1c79356b | 5415 | } |
2d21ac55 | 5416 | error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np); |
1c79356b A |
5417 | if (!error) { |
5418 | newvp = NFSTOV(np); | |
0a7de745 | 5419 | if (vnode_vtype(newvp) != VDIR) { |
1c79356b | 5420 | error = EEXIST; |
0a7de745 | 5421 | } |
1c79356b A |
5422 | } |
5423 | } | |
0a7de745 | 5424 | if (!busyerror) { |
b0d623f7 | 5425 | nfs_node_clear_busy(dnp); |
0a7de745 | 5426 | } |
91447636 | 5427 | if (!error && (gotuid || gotgid) && |
f427ee49 A |
5428 | (!newvp || nfs_getattrcache(np, nvattr, 0) || |
5429 | (gotuid && (nvattr->nva_uid != vap->va_uid)) || | |
5430 | (gotgid && (nvattr->nva_gid != vap->va_gid)))) { | |
91447636 A |
5431 | /* clear ID bits if server didn't use them (or we can't tell) */ |
5432 | VATTR_CLEAR_SUPPORTED(vap, va_uid); | |
5433 | VATTR_CLEAR_SUPPORTED(vap, va_gid); | |
5434 | } | |
1c79356b | 5435 | if (error) { |
2d21ac55 | 5436 | if (newvp) { |
b0d623f7 | 5437 | nfs_node_unlock(np); |
91447636 | 5438 | vnode_put(newvp); |
2d21ac55 | 5439 | } |
483a1d10 | 5440 | } else { |
b0d623f7 | 5441 | nfs_node_unlock(np); |
1c79356b | 5442 | *ap->a_vpp = newvp; |
483a1d10 | 5443 | } |
f427ee49 A |
5444 | NFS_ZFREE(nfs_fhandle_zone, fh); |
5445 | NFS_ZFREE(nfs_req_zone, req); | |
5446 | FREE(dul, M_TEMP); | |
5447 | FREE(nvattr, M_TEMP); | |
0a7de745 | 5448 | return error; |
1c79356b A |
5449 | } |
5450 | ||
5451 | /* | |
2d21ac55 | 5452 | * NFS remove directory call |
1c79356b | 5453 | */ |
b0d623f7 | 5454 | int |
2d21ac55 | 5455 | nfs3_vnop_rmdir( |
91447636 | 5456 | struct vnop_rmdir_args /* { |
0a7de745 A |
5457 | * struct vnodeop_desc *a_desc; |
5458 | * vnode_t a_dvp; | |
5459 | * vnode_t a_vp; | |
5460 | * struct componentname *a_cnp; | |
5461 | * vfs_context_t a_context; | |
5462 | * } */*ap) | |
1c79356b | 5463 | { |
2d21ac55 | 5464 | vfs_context_t ctx = ap->a_context; |
91447636 A |
5465 | vnode_t vp = ap->a_vp; |
5466 | vnode_t dvp = ap->a_dvp; | |
5467 | struct componentname *cnp = ap->a_cnp; | |
f427ee49 | 5468 | int error = 0, lockerror = ENOENT, status = 0, wccpostattr = 0; |
cb323159 | 5469 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
2d21ac55 A |
5470 | struct nfsmount *nmp; |
5471 | nfsnode_t np = VTONFS(vp); | |
5472 | nfsnode_t dnp = VTONFS(dvp); | |
2d21ac55 | 5473 | int nfsvers; |
fa4905b1 | 5474 | u_int64_t xid; |
2d21ac55 | 5475 | struct nfsm_chain nmreq, nmrep; |
f427ee49 A |
5476 | struct nfsreq *req; |
5477 | struct nfs_dulookup *dul; | |
cb323159 A |
5478 | int namedattrs; |
5479 | int dul_in_progress = 0; | |
1c79356b | 5480 | |
2d21ac55 | 5481 | nmp = VTONMP(vp); |
0a7de745 A |
5482 | if (nfs_mount_gone(nmp)) { |
5483 | return ENXIO; | |
5484 | } | |
2d21ac55 | 5485 | nfsvers = nmp->nm_vers; |
cb323159 A |
5486 | namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR); |
5487 | ||
0a7de745 A |
5488 | if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN)) { |
5489 | return ENAMETOOLONG; | |
5490 | } | |
91447636 | 5491 | |
0a7de745 A |
5492 | if ((error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx)))) { |
5493 | return error; | |
5494 | } | |
2d21ac55 | 5495 | |
f427ee49 A |
5496 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); |
5497 | MALLOC(dul, struct nfs_dulookup *, sizeof(*dul), M_TEMP, M_WAITOK); | |
5498 | ||
cb323159 | 5499 | if (!namedattrs) { |
f427ee49 | 5500 | nfs_dulookup_init(dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx); |
cb323159 | 5501 | } |
b0d623f7 | 5502 | |
2d21ac55 A |
5503 | nfsm_chain_null(&nmreq); |
5504 | nfsm_chain_null(&nmrep); | |
5505 | ||
5506 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 5507 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); |
2d21ac55 | 5508 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 5509 | nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp); |
2d21ac55 A |
5510 | nfsm_chain_build_done(error, &nmreq); |
5511 | nfsmout_if(error); | |
5512 | ||
5513 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_RMDIR, | |
0a7de745 | 5514 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req); |
2d21ac55 | 5515 | if (!error) { |
cb323159 | 5516 | if (!namedattrs) { |
f427ee49 | 5517 | nfs_dulookup_start(dul, dnp, ctx); |
cb323159 A |
5518 | dul_in_progress = 1; |
5519 | } | |
2d21ac55 A |
5520 | error = nfs_request_async_finish(req, &nmrep, &xid, &status); |
5521 | } | |
5522 | ||
0a7de745 | 5523 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 5524 | error = lockerror; |
0a7de745 A |
5525 | } |
5526 | if (nfsvers == NFS_VER3) { | |
2d21ac55 | 5527 | nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid); |
0a7de745 A |
5528 | } |
5529 | if (!error) { | |
2d21ac55 | 5530 | error = status; |
0a7de745 | 5531 | } |
2d21ac55 A |
5532 | nfsmout: |
5533 | nfsm_chain_cleanup(&nmreq); | |
5534 | nfsm_chain_cleanup(&nmrep); | |
5535 | ||
b0d623f7 A |
5536 | if (!lockerror) { |
5537 | dnp->n_flag |= NMODIFIED; | |
5538 | /* if directory hadn't changed, update namecache mtime */ | |
0a7de745 | 5539 | if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==)) { |
b0d623f7 | 5540 | NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr); |
0a7de745 | 5541 | } |
b0d623f7 A |
5542 | nfs_node_unlock(dnp); |
5543 | nfs_name_cache_purge(dnp, np, cnp, ctx); | |
5544 | /* nfs_getattr() will check changed and purge caches */ | |
6d2010ae | 5545 | nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED); |
2d21ac55 | 5546 | } |
cb323159 | 5547 | if (dul_in_progress) { |
f427ee49 | 5548 | nfs_dulookup_finish(dul, dnp, ctx); |
cb323159 | 5549 | } |
b0d623f7 | 5550 | nfs_node_clear_busy2(dnp, np); |
2d21ac55 | 5551 | |
1c79356b A |
5552 | /* |
5553 | * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. | |
5554 | */ | |
0a7de745 | 5555 | if (error == ENOENT) { |
1c79356b | 5556 | error = 0; |
0a7de745 | 5557 | } |
483a1d10 A |
5558 | if (!error) { |
5559 | /* | |
5560 | * remove nfsnode from hash now so we can't accidentally find it | |
5561 | * again if another object gets created with the same filehandle | |
5562 | * before this vnode gets reclaimed | |
5563 | */ | |
c3c9b80d | 5564 | lck_mtx_lock(&nfs_node_hash_mutex); |
2d21ac55 A |
5565 | if (np->n_hflag & NHHASHED) { |
5566 | LIST_REMOVE(np, n_hash); | |
5567 | np->n_hflag &= ~NHHASHED; | |
5568 | FSDBG(266, 0, np, np->n_flag, 0xb1eb1e); | |
5569 | } | |
c3c9b80d | 5570 | lck_mtx_unlock(&nfs_node_hash_mutex); |
483a1d10 | 5571 | } |
f427ee49 A |
5572 | NFS_ZFREE(nfs_req_zone, req); |
5573 | FREE(dul, M_TEMP); | |
0a7de745 | 5574 | return error; |
1c79356b A |
5575 | } |
5576 | ||
5577 | /* | |
2d21ac55 | 5578 | * NFS readdir call |
b0d623f7 A |
5579 | * |
5580 | * The incoming "offset" is a directory cookie indicating where in the | |
5581 | * directory entries should be read from. A zero cookie means start at | |
5582 | * the beginning of the directory. Any other cookie will be a cookie | |
0a7de745 | 5583 | * returned from the server. |
b0d623f7 A |
5584 | * |
5585 | * Using that cookie, determine which buffer (and where in that buffer) | |
5586 | * to start returning entries from. Buffer logical block numbers are | |
5587 | * the cookies they start at. If a buffer is found that is not full, | |
5588 | * call into the bio/RPC code to fill it. The RPC code will probably | |
5589 | * fill several buffers (dropping the first, requiring a re-get). | |
5590 | * | |
5591 | * When done copying entries to the buffer, set the offset to the current | |
5592 | * entry's cookie and enter that cookie in the cookie cache. | |
5593 | * | |
5594 | * Note: because the getdirentries(2) API returns a long-typed offset, | |
5595 | * the incoming offset is a potentially truncated cookie (ptc). | |
5596 | * The cookie matching code is aware of this and will fall back to | |
5597 | * matching only 32 bits of the cookie. | |
1c79356b | 5598 | */ |
b0d623f7 | 5599 | int |
2d21ac55 | 5600 | nfs_vnop_readdir( |
91447636 | 5601 | struct vnop_readdir_args /* { |
0a7de745 A |
5602 | * struct vnodeop_desc *a_desc; |
5603 | * vnode_t a_vp; | |
5604 | * struct uio *a_uio; | |
5605 | * int a_flags; | |
5606 | * int *a_eofflag; | |
5607 | * int *a_numdirent; | |
5608 | * vfs_context_t a_context; | |
5609 | * } */*ap) | |
1c79356b | 5610 | { |
2d21ac55 | 5611 | vfs_context_t ctx = ap->a_context; |
b0d623f7 A |
5612 | vnode_t dvp = ap->a_vp; |
5613 | nfsnode_t dnp = VTONFS(dvp); | |
2d21ac55 | 5614 | struct nfsmount *nmp; |
b0d623f7 | 5615 | uio_t uio = ap->a_uio; |
f427ee49 A |
5616 | int error, nfsvers, extended, numdirent, bigcookies, ptc, done; |
5617 | long attrcachetimeout; | |
b0d623f7 A |
5618 | uint16_t i, iptc, rlen, nlen; |
5619 | uint64_t cookie, nextcookie, lbn = 0; | |
5620 | struct nfsbuf *bp = NULL; | |
5621 | struct nfs_dir_buf_header *ndbhp; | |
5622 | struct direntry *dp, *dpptc; | |
5623 | struct dirent dent; | |
5624 | char *cp = NULL; | |
ea3f0419 | 5625 | struct timeval now; |
b0d623f7 | 5626 | thread_t thd; |
1c79356b | 5627 | |
b0d623f7 | 5628 | nmp = VTONMP(dvp); |
0a7de745 A |
5629 | if (nfs_mount_gone(nmp)) { |
5630 | return ENXIO; | |
5631 | } | |
2d21ac55 | 5632 | nfsvers = nmp->nm_vers; |
b0d623f7 A |
5633 | bigcookies = (nmp->nm_state & NFSSTA_BIGCOOKIES); |
5634 | extended = (ap->a_flags & VNODE_READDIR_EXTENDED); | |
2d21ac55 | 5635 | |
0a7de745 A |
5636 | if (vnode_vtype(dvp) != VDIR) { |
5637 | return EPERM; | |
5638 | } | |
b0d623f7 | 5639 | |
0a7de745 | 5640 | if (ap->a_eofflag) { |
b0d623f7 | 5641 | *ap->a_eofflag = 0; |
0a7de745 | 5642 | } |
b0d623f7 | 5643 | |
0a7de745 A |
5644 | if (uio_resid(uio) == 0) { |
5645 | return 0; | |
5646 | } | |
cb323159 | 5647 | #if CONFIG_NFS4 |
6d2010ae A |
5648 | if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) { |
5649 | /* trigger directories should never be read, return nothing */ | |
0a7de745 | 5650 | return 0; |
6d2010ae | 5651 | } |
cb323159 | 5652 | #endif |
b0d623f7 A |
5653 | thd = vfs_context_thread(ctx); |
5654 | numdirent = done = 0; | |
5655 | nextcookie = uio_offset(uio); | |
5656 | ptc = bigcookies && NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie); | |
5657 | ||
0a7de745 | 5658 | if ((error = nfs_node_lock(dnp))) { |
b0d623f7 | 5659 | goto out; |
0a7de745 | 5660 | } |
b0d623f7 A |
5661 | |
5662 | if (dnp->n_flag & NNEEDINVALIDATE) { | |
5663 | dnp->n_flag &= ~NNEEDINVALIDATE; | |
5664 | nfs_invaldir(dnp); | |
5665 | nfs_node_unlock(dnp); | |
5666 | error = nfs_vinvalbuf(dvp, 0, ctx, 1); | |
0a7de745 | 5667 | if (!error) { |
b0d623f7 | 5668 | error = nfs_node_lock(dnp); |
0a7de745 A |
5669 | } |
5670 | if (error) { | |
b0d623f7 | 5671 | goto out; |
0a7de745 | 5672 | } |
b0d623f7 | 5673 | } |
91447636 | 5674 | |
ea3f0419 A |
5675 | if (dnp->n_rdirplusstamp_eof && dnp->n_rdirplusstamp_sof) { |
5676 | attrcachetimeout = nfs_attrcachetimeout(dnp); | |
5677 | microuptime(&now); | |
5678 | if (attrcachetimeout && (now.tv_sec - dnp->n_rdirplusstamp_sof > attrcachetimeout - 1)) { | |
5679 | dnp->n_rdirplusstamp_eof = dnp->n_rdirplusstamp_sof = 0; | |
5680 | nfs_invaldir(dnp); | |
5681 | nfs_node_unlock(dnp); | |
5682 | error = nfs_vinvalbuf(dvp, 0, ctx, 1); | |
5683 | if (!error) { | |
5684 | error = nfs_node_lock(dnp); | |
5685 | } | |
5686 | if (error) { | |
5687 | goto out; | |
5688 | } | |
5689 | } | |
5690 | } | |
5691 | ||
1c79356b | 5692 | /* |
b0d623f7 | 5693 | * check for need to invalidate when (re)starting at beginning |
1c79356b | 5694 | */ |
b0d623f7 A |
5695 | if (!nextcookie) { |
5696 | if (dnp->n_flag & NMODIFIED) { | |
5697 | nfs_invaldir(dnp); | |
5698 | nfs_node_unlock(dnp); | |
0a7de745 | 5699 | if ((error = nfs_vinvalbuf(dvp, 0, ctx, 1))) { |
b0d623f7 | 5700 | goto out; |
0a7de745 | 5701 | } |
b0d623f7 A |
5702 | } else { |
5703 | nfs_node_unlock(dnp); | |
5704 | } | |
5705 | /* nfs_getattr() will check changed and purge caches */ | |
0a7de745 | 5706 | if ((error = nfs_getattr(dnp, NULL, ctx, NGA_UNCACHED))) { |
b0d623f7 | 5707 | goto out; |
0a7de745 | 5708 | } |
b0d623f7 A |
5709 | } else { |
5710 | nfs_node_unlock(dnp); | |
5711 | } | |
5712 | ||
5713 | error = nfs_dir_cookie_to_lbn(dnp, nextcookie, &ptc, &lbn); | |
5714 | if (error) { | |
5715 | if (error < 0) { /* just hit EOF cookie */ | |
5716 | done = 1; | |
5717 | error = 0; | |
5718 | } | |
0a7de745 | 5719 | if (ap->a_eofflag) { |
b0d623f7 | 5720 | *ap->a_eofflag = 1; |
0a7de745 | 5721 | } |
b0d623f7 A |
5722 | } |
5723 | ||
5724 | while (!error && !done) { | |
316670eb | 5725 | OSAddAtomic64(1, &nfsstats.biocache_readdirs); |
b0d623f7 A |
5726 | cookie = nextcookie; |
5727 | getbuffer: | |
5728 | error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ, &bp); | |
0a7de745 | 5729 | if (error) { |
b0d623f7 | 5730 | goto out; |
0a7de745 | 5731 | } |
b0d623f7 A |
5732 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; |
5733 | if (!ISSET(bp->nb_flags, NB_CACHE) || !ISSET(ndbhp->ndbh_flags, NDB_FULL)) { | |
5734 | if (!ISSET(bp->nb_flags, NB_CACHE)) { /* initialize the buffer */ | |
5735 | ndbhp->ndbh_flags = 0; | |
5736 | ndbhp->ndbh_count = 0; | |
5737 | ndbhp->ndbh_entry_end = sizeof(*ndbhp); | |
5738 | ndbhp->ndbh_ncgen = dnp->n_ncgen; | |
5739 | } | |
5740 | error = nfs_buf_readdir(bp, ctx); | |
0a7de745 | 5741 | if (error == NFSERR_DIRBUFDROPPED) { |
b0d623f7 | 5742 | goto getbuffer; |
0a7de745 A |
5743 | } |
5744 | if (error) { | |
b0d623f7 | 5745 | nfs_buf_release(bp, 1); |
0a7de745 | 5746 | } |
b0d623f7 A |
5747 | if (error && (error != ENXIO) && (error != ETIMEDOUT) && (error != EINTR) && (error != ERESTART)) { |
5748 | if (!nfs_node_lock(dnp)) { | |
5749 | nfs_invaldir(dnp); | |
5750 | nfs_node_unlock(dnp); | |
5751 | } | |
5752 | nfs_vinvalbuf(dvp, 0, ctx, 1); | |
0a7de745 | 5753 | if (error == NFSERR_BAD_COOKIE) { |
b0d623f7 | 5754 | error = ENOENT; |
0a7de745 | 5755 | } |
b0d623f7 | 5756 | } |
0a7de745 | 5757 | if (error) { |
b0d623f7 | 5758 | goto out; |
0a7de745 | 5759 | } |
b0d623f7 A |
5760 | } |
5761 | ||
5762 | /* find next entry to return */ | |
5763 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
5764 | i = 0; | |
5765 | if ((lbn != cookie) && !(ptc && NFS_DIR_COOKIE_SAME32(lbn, cookie))) { | |
5766 | dpptc = NULL; | |
5767 | iptc = 0; | |
5768 | for (; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) { | |
5769 | if (ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) { | |
5770 | iptc = i; | |
5771 | dpptc = dp; | |
5772 | } | |
5773 | nextcookie = dp->d_seekoff; | |
5774 | dp = NFS_DIRENTRY_NEXT(dp); | |
5775 | } | |
5776 | if ((i == ndbhp->ndbh_count) && dpptc) { | |
5777 | i = iptc; | |
5778 | dp = dpptc; | |
5779 | } | |
5780 | if (i < ndbhp->ndbh_count) { | |
5781 | nextcookie = dp->d_seekoff; | |
5782 | dp = NFS_DIRENTRY_NEXT(dp); | |
5783 | i++; | |
5784 | } | |
5785 | } | |
5786 | ptc = 0; /* only have to deal with ptc on first cookie */ | |
5787 | ||
5788 | /* return as many entries as we can */ | |
5789 | for (; i < ndbhp->ndbh_count; i++) { | |
5790 | if (extended) { | |
5791 | rlen = dp->d_reclen; | |
5792 | cp = (char*)dp; | |
5793 | } else { | |
5794 | if (!cp) { | |
5795 | cp = (char*)&dent; | |
5796 | bzero(cp, sizeof(dent)); | |
5797 | } | |
0a7de745 | 5798 | if (dp->d_namlen > (sizeof(dent.d_name) - 1)) { |
b0d623f7 | 5799 | nlen = sizeof(dent.d_name) - 1; |
0a7de745 | 5800 | } else { |
b0d623f7 | 5801 | nlen = dp->d_namlen; |
0a7de745 | 5802 | } |
b0d623f7 A |
5803 | rlen = NFS_DIRENT_LEN(nlen); |
5804 | dent.d_reclen = rlen; | |
f427ee49 | 5805 | dent.d_ino = (ino_t)dp->d_ino; |
b0d623f7 | 5806 | dent.d_type = dp->d_type; |
f427ee49 | 5807 | dent.d_namlen = (uint8_t)nlen; |
b0d623f7 A |
5808 | strlcpy(dent.d_name, dp->d_name, nlen + 1); |
5809 | } | |
5810 | /* check that the record fits */ | |
5811 | if (rlen > uio_resid(uio)) { | |
5812 | done = 1; | |
5813 | break; | |
5814 | } | |
0a7de745 | 5815 | if ((error = uiomove(cp, rlen, uio))) { |
b0d623f7 | 5816 | break; |
0a7de745 | 5817 | } |
b0d623f7 A |
5818 | numdirent++; |
5819 | nextcookie = dp->d_seekoff; | |
5820 | dp = NFS_DIRENTRY_NEXT(dp); | |
5821 | } | |
5822 | ||
5823 | if (i == ndbhp->ndbh_count) { | |
5824 | /* hit end of buffer, move to next buffer */ | |
5825 | lbn = nextcookie; | |
5826 | /* if we also hit EOF, we're done */ | |
5827 | if (ISSET(ndbhp->ndbh_flags, NDB_EOF)) { | |
5828 | done = 1; | |
0a7de745 | 5829 | if (ap->a_eofflag) { |
2d21ac55 | 5830 | *ap->a_eofflag = 1; |
0a7de745 | 5831 | } |
483a1d10 | 5832 | } |
1c79356b | 5833 | } |
0a7de745 | 5834 | if (!error) { |
b0d623f7 | 5835 | uio_setoffset(uio, nextcookie); |
0a7de745 | 5836 | } |
b0d623f7 A |
5837 | if (!error && !done && (nextcookie == cookie)) { |
5838 | printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie, i, ndbhp->ndbh_count); | |
5839 | error = EIO; | |
5840 | } | |
5841 | nfs_buf_release(bp, 1); | |
1c79356b A |
5842 | } |
5843 | ||
0a7de745 | 5844 | if (!error) { |
b0d623f7 | 5845 | nfs_dir_cookie_cache(dnp, nextcookie, lbn); |
0a7de745 | 5846 | } |
1c79356b | 5847 | |
0a7de745 | 5848 | if (ap->a_numdirent) { |
b0d623f7 | 5849 | *ap->a_numdirent = numdirent; |
0a7de745 | 5850 | } |
b0d623f7 | 5851 | out: |
0a7de745 | 5852 | return error; |
1c79356b A |
5853 | } |
5854 | ||
b0d623f7 | 5855 | |
1c79356b | 5856 | /* |
b0d623f7 A |
5857 | * Invalidate cached directory information, except for the actual directory |
5858 | * blocks (which are invalidated separately). | |
1c79356b | 5859 | */ |
c3c9b80d A |
5860 | static void |
5861 | nfs_invaldir_cookies(nfsnode_t dnp) | |
1c79356b | 5862 | { |
0a7de745 | 5863 | if (vnode_vtype(NFSTOV(dnp)) != VDIR) { |
b0d623f7 | 5864 | return; |
0a7de745 | 5865 | } |
b0d623f7 A |
5866 | dnp->n_eofcookie = 0; |
5867 | dnp->n_cookieverf = 0; | |
0a7de745 | 5868 | if (!dnp->n_cookiecache) { |
b0d623f7 | 5869 | return; |
0a7de745 | 5870 | } |
b0d623f7 A |
5871 | dnp->n_cookiecache->free = 0; |
5872 | dnp->n_cookiecache->mru = -1; | |
5873 | memset(dnp->n_cookiecache->next, -1, NFSNUMCOOKIES); | |
5874 | } | |
1c79356b | 5875 | |
c3c9b80d A |
5876 | void |
5877 | nfs_invaldir(nfsnode_t dnp) | |
5878 | { | |
5879 | ||
5880 | nfs_invaldir_cookies(dnp); | |
5881 | } | |
5882 | ||
b0d623f7 A |
5883 | /* |
5884 | * calculate how much space is available for additional directory entries. | |
5885 | */ | |
f427ee49 | 5886 | uint64_t |
b0d623f7 A |
5887 | nfs_dir_buf_freespace(struct nfsbuf *bp, int rdirplus) |
5888 | { | |
5889 | struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
f427ee49 | 5890 | uint64_t space; |
1c79356b | 5891 | |
0a7de745 A |
5892 | if (!ndbhp) { |
5893 | return 0; | |
5894 | } | |
b0d623f7 | 5895 | space = bp->nb_bufsize - ndbhp->ndbh_entry_end; |
0a7de745 | 5896 | if (rdirplus) { |
b0d623f7 | 5897 | space -= ndbhp->ndbh_count * sizeof(struct nfs_vattr); |
0a7de745 A |
5898 | } |
5899 | return space; | |
b0d623f7 | 5900 | } |
2d21ac55 | 5901 | |
b0d623f7 A |
5902 | /* |
5903 | * add/update a cookie->lbn entry in the directory cookie cache | |
5904 | */ | |
5905 | void | |
5906 | nfs_dir_cookie_cache(nfsnode_t dnp, uint64_t cookie, uint64_t lbn) | |
5907 | { | |
5908 | struct nfsdmap *ndcc; | |
5909 | int8_t i, prev; | |
5910 | ||
0a7de745 | 5911 | if (!cookie) { |
b0d623f7 | 5912 | return; |
0a7de745 | 5913 | } |
b0d623f7 | 5914 | |
0a7de745 | 5915 | if (nfs_node_lock(dnp)) { |
b0d623f7 | 5916 | return; |
0a7de745 | 5917 | } |
b0d623f7 A |
5918 | |
5919 | if (cookie == dnp->n_eofcookie) { /* EOF cookie */ | |
5920 | nfs_node_unlock(dnp); | |
5921 | return; | |
5922 | } | |
5923 | ||
5924 | ndcc = dnp->n_cookiecache; | |
5925 | if (!ndcc) { | |
5926 | /* allocate the cookie cache structure */ | |
f427ee49 | 5927 | ndcc = dnp->n_cookiecache = zalloc(ZV_NFSDIROFF); |
b0d623f7 A |
5928 | ndcc->free = 0; |
5929 | ndcc->mru = -1; | |
5930 | memset(ndcc->next, -1, NFSNUMCOOKIES); | |
2d21ac55 A |
5931 | } |
5932 | ||
1c79356b | 5933 | /* |
b0d623f7 A |
5934 | * Search the list for this cookie. |
5935 | * Keep track of previous and last entries. | |
1c79356b | 5936 | */ |
b0d623f7 A |
5937 | prev = -1; |
5938 | i = ndcc->mru; | |
5939 | while ((i != -1) && (cookie != ndcc->cookies[i].key)) { | |
0a7de745 | 5940 | if (ndcc->next[i] == -1) { /* stop on last entry so we can reuse */ |
b0d623f7 | 5941 | break; |
0a7de745 | 5942 | } |
b0d623f7 A |
5943 | prev = i; |
5944 | i = ndcc->next[i]; | |
5945 | } | |
5946 | if ((i != -1) && (cookie == ndcc->cookies[i].key)) { | |
5947 | /* found it, remove from list */ | |
0a7de745 | 5948 | if (prev != -1) { |
b0d623f7 | 5949 | ndcc->next[prev] = ndcc->next[i]; |
0a7de745 | 5950 | } else { |
b0d623f7 | 5951 | ndcc->mru = ndcc->next[i]; |
0a7de745 | 5952 | } |
b0d623f7 A |
5953 | } else { |
5954 | /* not found, use next free entry or reuse last entry */ | |
0a7de745 | 5955 | if (ndcc->free != NFSNUMCOOKIES) { |
b0d623f7 | 5956 | i = ndcc->free++; |
0a7de745 | 5957 | } else { |
b0d623f7 | 5958 | ndcc->next[prev] = -1; |
0a7de745 | 5959 | } |
b0d623f7 A |
5960 | ndcc->cookies[i].key = cookie; |
5961 | ndcc->cookies[i].lbn = lbn; | |
5962 | } | |
5963 | /* insert cookie at head of MRU list */ | |
5964 | ndcc->next[i] = ndcc->mru; | |
5965 | ndcc->mru = i; | |
5966 | nfs_node_unlock(dnp); | |
5967 | } | |
2d21ac55 | 5968 | |
b0d623f7 A |
5969 | /* |
5970 | * Try to map the given directory cookie to a directory buffer (return lbn). | |
5971 | * If we have a possibly truncated cookie (ptc), check for 32-bit matches too. | |
5972 | */ | |
5973 | int | |
5974 | nfs_dir_cookie_to_lbn(nfsnode_t dnp, uint64_t cookie, int *ptc, uint64_t *lbnp) | |
5975 | { | |
5976 | struct nfsdmap *ndcc = dnp->n_cookiecache; | |
6d2010ae A |
5977 | int8_t eofptc, found; |
5978 | int i, iptc; | |
b0d623f7 A |
5979 | struct nfsmount *nmp; |
5980 | struct nfsbuf *bp, *lastbp; | |
5981 | struct nfsbuflists blist; | |
5982 | struct direntry *dp, *dpptc; | |
5983 | struct nfs_dir_buf_header *ndbhp; | |
5984 | ||
5985 | if (!cookie) { /* initial cookie */ | |
5986 | *lbnp = 0; | |
5987 | *ptc = 0; | |
0a7de745 | 5988 | return 0; |
b0d623f7 | 5989 | } |
2d21ac55 | 5990 | |
0a7de745 A |
5991 | if (nfs_node_lock(dnp)) { |
5992 | return ENOENT; | |
5993 | } | |
2d21ac55 | 5994 | |
b0d623f7 A |
5995 | if (cookie == dnp->n_eofcookie) { /* EOF cookie */ |
5996 | nfs_node_unlock(dnp); | |
316670eb | 5997 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 | 5998 | *ptc = 0; |
0a7de745 | 5999 | return -1; |
b0d623f7 A |
6000 | } |
6001 | /* note if cookie is a 32-bit match with the EOF cookie */ | |
6002 | eofptc = *ptc ? NFS_DIR_COOKIE_SAME32(cookie, dnp->n_eofcookie) : 0; | |
6003 | iptc = -1; | |
2d21ac55 | 6004 | |
b0d623f7 A |
6005 | /* search the list for the cookie */ |
6006 | for (i = ndcc ? ndcc->mru : -1; i >= 0; i = ndcc->next[i]) { | |
6007 | if (ndcc->cookies[i].key == cookie) { | |
6008 | /* found a match for this cookie */ | |
6009 | *lbnp = ndcc->cookies[i].lbn; | |
6010 | nfs_node_unlock(dnp); | |
316670eb | 6011 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
b0d623f7 | 6012 | *ptc = 0; |
0a7de745 | 6013 | return 0; |
1c79356b | 6014 | } |
b0d623f7 | 6015 | /* check for 32-bit match */ |
0a7de745 | 6016 | if (*ptc && (iptc == -1) && NFS_DIR_COOKIE_SAME32(ndcc->cookies[i].key, cookie)) { |
b0d623f7 | 6017 | iptc = i; |
0a7de745 | 6018 | } |
b0d623f7 A |
6019 | } |
6020 | /* exact match not found */ | |
6021 | if (eofptc) { | |
6022 | /* but 32-bit match hit the EOF cookie */ | |
6023 | nfs_node_unlock(dnp); | |
316670eb | 6024 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
0a7de745 | 6025 | return -1; |
b0d623f7 A |
6026 | } |
6027 | if (iptc >= 0) { | |
6028 | /* but 32-bit match got a hit */ | |
6029 | *lbnp = ndcc->cookies[iptc].lbn; | |
6030 | nfs_node_unlock(dnp); | |
316670eb | 6031 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
0a7de745 | 6032 | return 0; |
b0d623f7 A |
6033 | } |
6034 | nfs_node_unlock(dnp); | |
2d21ac55 | 6035 | |
b0d623f7 A |
6036 | /* |
6037 | * No match found in the cookie cache... hmm... | |
6038 | * Let's search the directory's buffers for the cookie. | |
6039 | */ | |
6040 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6041 | if (nfs_mount_gone(nmp)) { |
6042 | return ENXIO; | |
6043 | } | |
b0d623f7 A |
6044 | dpptc = NULL; |
6045 | found = 0; | |
6046 | ||
c3c9b80d | 6047 | lck_mtx_lock(&nfs_buf_mutex); |
b0d623f7 A |
6048 | /* |
6049 | * Scan the list of buffers, keeping them in order. | |
6050 | * Note that itercomplete inserts each of the remaining buffers | |
6051 | * into the head of list (thus reversing the elements). So, we | |
6052 | * make sure to iterate through all buffers, inserting them after | |
6053 | * each other, to keep them in order. | |
6054 | * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because | |
6055 | * we don't drop nfs_buf_mutex. | |
6056 | */ | |
6057 | if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) { | |
6058 | lastbp = NULL; | |
6059 | while ((bp = LIST_FIRST(&blist))) { | |
6060 | LIST_REMOVE(bp, nb_vnbufs); | |
0a7de745 | 6061 | if (!lastbp) { |
b0d623f7 | 6062 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs); |
0a7de745 | 6063 | } else { |
b0d623f7 | 6064 | LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs); |
0a7de745 | 6065 | } |
b0d623f7 | 6066 | lastbp = bp; |
0a7de745 | 6067 | if (found) { |
b0d623f7 | 6068 | continue; |
0a7de745 | 6069 | } |
b0d623f7 A |
6070 | nfs_buf_refget(bp); |
6071 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
6072 | /* just skip this buffer */ | |
6073 | nfs_buf_refrele(bp); | |
6074 | continue; | |
1c79356b | 6075 | } |
b0d623f7 A |
6076 | nfs_buf_refrele(bp); |
6077 | ||
6078 | /* scan the buffer for the cookie */ | |
6079 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
6080 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
6081 | dpptc = NULL; | |
0a7de745 | 6082 | for (i = 0; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) { |
b0d623f7 A |
6083 | if (*ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) { |
6084 | dpptc = dp; | |
6085 | iptc = i; | |
6086 | } | |
6087 | dp = NFS_DIRENTRY_NEXT(dp); | |
91447636 | 6088 | } |
b0d623f7 A |
6089 | if ((i == ndbhp->ndbh_count) && dpptc) { |
6090 | /* found only a PTC match */ | |
6091 | dp = dpptc; | |
6092 | i = iptc; | |
6093 | } else if (i < ndbhp->ndbh_count) { | |
6094 | *ptc = 0; | |
1c79356b | 6095 | } |
0a7de745 | 6096 | if (i < (ndbhp->ndbh_count - 1)) { |
b0d623f7 A |
6097 | /* next entry is *in* this buffer: return this block */ |
6098 | *lbnp = bp->nb_lblkno; | |
6099 | found = 1; | |
0a7de745 | 6100 | } else if (i == (ndbhp->ndbh_count - 1)) { |
b0d623f7 A |
6101 | /* next entry refers to *next* buffer: return next block */ |
6102 | *lbnp = dp->d_seekoff; | |
6103 | found = 1; | |
1c79356b | 6104 | } |
b0d623f7 | 6105 | nfs_buf_drop(bp); |
1c79356b | 6106 | } |
b0d623f7 | 6107 | nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN); |
2d21ac55 | 6108 | } |
c3c9b80d | 6109 | lck_mtx_unlock(&nfs_buf_mutex); |
b0d623f7 | 6110 | if (found) { |
316670eb | 6111 | OSAddAtomic64(1, &nfsstats.direofcache_hits); |
0a7de745 | 6112 | return 0; |
1c79356b | 6113 | } |
b0d623f7 A |
6114 | |
6115 | /* still not found... oh well, just start a new block */ | |
6116 | *lbnp = cookie; | |
316670eb | 6117 | OSAddAtomic64(1, &nfsstats.direofcache_misses); |
0a7de745 | 6118 | return 0; |
b0d623f7 A |
6119 | } |
6120 | ||
6121 | /* | |
6122 | * scan a directory buffer for the given name | |
6123 | * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found | |
6124 | * Note: should only be called with RDIRPLUS directory buffers | |
6125 | */ | |
6126 | ||
0a7de745 A |
6127 | #define NDBS_PURGE 1 |
6128 | #define NDBS_UPDATE 2 | |
b0d623f7 A |
6129 | |
6130 | int | |
6131 | nfs_dir_buf_search( | |
6132 | struct nfsbuf *bp, | |
6133 | struct componentname *cnp, | |
6134 | fhandle_t *fhp, | |
6135 | struct nfs_vattr *nvap, | |
6136 | uint64_t *xidp, | |
6137 | time_t *attrstampp, | |
6138 | daddr64_t *nextlbnp, | |
6139 | int flags) | |
6140 | { | |
6141 | struct direntry *dp; | |
6142 | struct nfs_dir_buf_header *ndbhp; | |
6143 | struct nfs_vattr *nvattrp; | |
6144 | daddr64_t nextlbn = 0; | |
d26ffc64 A |
6145 | int i, error = ESRCH; |
6146 | uint32_t fhlen; | |
b0d623f7 A |
6147 | |
6148 | /* scan the buffer for the name */ | |
6149 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
6150 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
0a7de745 | 6151 | for (i = 0; i < ndbhp->ndbh_count; i++) { |
b0d623f7 A |
6152 | nextlbn = dp->d_seekoff; |
6153 | if ((cnp->cn_namelen == dp->d_namlen) && !strcmp(cnp->cn_nameptr, dp->d_name)) { | |
f427ee49 | 6154 | fhlen = (uint8_t)dp->d_name[dp->d_namlen + 1]; |
b0d623f7 | 6155 | nvattrp = NFS_DIR_BUF_NVATTR(bp, i); |
94ff46dc | 6156 | if ((ndbhp->ndbh_ncgen != bp->nb_np->n_ncgen) || (fhlen == 0) || |
b0d623f7 | 6157 | (nvattrp->nva_type == VNON) || (nvattrp->nva_fileid == 0)) { |
6d2010ae | 6158 | /* entry is not valid */ |
b0d623f7 A |
6159 | error = ENOENT; |
6160 | break; | |
6161 | } | |
6162 | if (flags == NDBS_PURGE) { | |
6163 | dp->d_fileno = 0; | |
6164 | bzero(nvattrp, sizeof(*nvattrp)); | |
6165 | error = ENOENT; | |
6166 | break; | |
6167 | } | |
6168 | if (flags == NDBS_UPDATE) { | |
6169 | /* update direntry's attrs if fh matches */ | |
0a7de745 | 6170 | if ((fhp->fh_len == fhlen) && !bcmp(&dp->d_name[dp->d_namlen + 2], fhp->fh_data, fhlen)) { |
b0d623f7 A |
6171 | bcopy(nvap, nvattrp, sizeof(*nvap)); |
6172 | dp->d_fileno = nvattrp->nva_fileid; | |
6173 | nvattrp->nva_fileid = *xidp; | |
f427ee49 | 6174 | nvap->nva_flags |= NFS_FFLAG_FILEID_CONTAINS_XID; |
0a7de745 | 6175 | *(time_t*)(&dp->d_name[dp->d_namlen + 2 + fhp->fh_len]) = *attrstampp; |
b0d623f7 A |
6176 | } |
6177 | error = 0; | |
6178 | break; | |
6179 | } | |
6180 | /* copy out fh, attrs, attrstamp, and xid */ | |
6181 | fhp->fh_len = fhlen; | |
0a7de745 A |
6182 | bcopy(&dp->d_name[dp->d_namlen + 2], fhp->fh_data, MAX(fhp->fh_len, (int)sizeof(fhp->fh_data))); |
6183 | *attrstampp = *(time_t*)(&dp->d_name[dp->d_namlen + 2 + fhp->fh_len]); | |
b0d623f7 A |
6184 | bcopy(nvattrp, nvap, sizeof(*nvap)); |
6185 | *xidp = nvap->nva_fileid; | |
6186 | nvap->nva_fileid = dp->d_fileno; | |
f427ee49 | 6187 | nvap->nva_flags &= ~NFS_FFLAG_FILEID_CONTAINS_XID; |
b0d623f7 A |
6188 | error = 0; |
6189 | break; | |
6190 | } | |
6191 | dp = NFS_DIRENTRY_NEXT(dp); | |
1c79356b | 6192 | } |
0a7de745 | 6193 | if (nextlbnp) { |
b0d623f7 | 6194 | *nextlbnp = nextlbn; |
0a7de745 A |
6195 | } |
6196 | return error; | |
b0d623f7 | 6197 | } |
1c79356b | 6198 | |
b0d623f7 A |
6199 | /* |
6200 | * Look up a name in a directory's buffers. | |
6201 | * Note: should only be called with RDIRPLUS directory buffers | |
6202 | */ | |
6203 | int | |
f427ee49 | 6204 | nfs_dir_buf_cache_lookup(nfsnode_t dnp, nfsnode_t *npp, struct componentname *cnp, vfs_context_t ctx, int purge, int *skipdu) |
b0d623f7 A |
6205 | { |
6206 | nfsnode_t newnp; | |
6207 | struct nfsmount *nmp; | |
6d2010ae | 6208 | int error = 0, i, found = 0, count = 0; |
b0d623f7 | 6209 | u_int64_t xid; |
f427ee49 A |
6210 | struct nfs_vattr *nvattr; |
6211 | fhandle_t *fh; | |
b0d623f7 A |
6212 | time_t attrstamp = 0; |
6213 | thread_t thd = vfs_context_thread(ctx); | |
6214 | struct nfsbuf *bp, *lastbp, *foundbp; | |
6215 | struct nfsbuflists blist; | |
6216 | daddr64_t lbn, nextlbn; | |
6217 | int dotunder = (cnp->cn_namelen > 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '_'); | |
ea3f0419 A |
6218 | int isdot = (cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.'); |
6219 | int isdotdot = (cnp->cn_namelen == 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '.'); | |
f427ee49 | 6220 | int eof = 0, sof = 0, skipped = 0; |
b0d623f7 | 6221 | |
fe8ab488 | 6222 | nmp = NFSTONMP(dnp); |
0a7de745 A |
6223 | if (nfs_mount_gone(nmp)) { |
6224 | return ENXIO; | |
6225 | } | |
6226 | if (!purge) { | |
b0d623f7 | 6227 | *npp = NULL; |
0a7de745 | 6228 | } |
b0d623f7 | 6229 | |
ea3f0419 A |
6230 | if (isdot || isdotdot) { |
6231 | return 0; | |
6232 | } | |
6233 | ||
f427ee49 A |
6234 | fh = zalloc(nfs_fhandle_zone); |
6235 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
6236 | ||
b0d623f7 A |
6237 | /* first check most recent buffer (and next one too) */ |
6238 | lbn = dnp->n_lastdbl; | |
0a7de745 A |
6239 | for (i = 0; i < 2; i++) { |
6240 | if ((error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ | NBLK_ONLYVALID, &bp))) { | |
f427ee49 | 6241 | goto out; |
0a7de745 A |
6242 | } |
6243 | if (!bp) { | |
f427ee49 | 6244 | skipped = 1; |
b0d623f7 | 6245 | break; |
0a7de745 | 6246 | } |
b0d623f7 | 6247 | count++; |
f427ee49 A |
6248 | nfs_dir_buf_cache_lookup_boundaries(bp, &sof, &eof); |
6249 | error = nfs_dir_buf_search(bp, cnp, fh, nvattr, &xid, &attrstamp, &nextlbn, purge ? NDBS_PURGE : 0); | |
b0d623f7 A |
6250 | nfs_buf_release(bp, 0); |
6251 | if (error == ESRCH) { | |
6252 | error = 0; | |
6253 | } else { | |
6254 | found = 1; | |
6255 | break; | |
6256 | } | |
6257 | lbn = nextlbn; | |
6258 | } | |
6259 | ||
c3c9b80d | 6260 | lck_mtx_lock(&nfs_buf_mutex); |
b0d623f7 A |
6261 | if (found) { |
6262 | dnp->n_lastdbl = lbn; | |
6263 | goto done; | |
6264 | } | |
2d21ac55 | 6265 | |
f427ee49 A |
6266 | /* If we detect that we fetched full directory listing we should avoid sending lookups for ._ files */ |
6267 | if (dotunder && !found && !error && eof && sof && !skipped && skipdu) { | |
6268 | *skipdu = 1; | |
6269 | } | |
6270 | ||
1c79356b | 6271 | /* |
b0d623f7 A |
6272 | * Scan the list of buffers, keeping them in order. |
6273 | * Note that itercomplete inserts each of the remaining buffers | |
6274 | * into the head of list (thus reversing the elements). So, we | |
6275 | * make sure to iterate through all buffers, inserting them after | |
6276 | * each other, to keep them in order. | |
6277 | * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because | |
6278 | * we don't drop nfs_buf_mutex. | |
1c79356b | 6279 | */ |
f427ee49 | 6280 | eof = sof = skipped = 0; |
b0d623f7 A |
6281 | if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) { |
6282 | lastbp = foundbp = NULL; | |
6283 | while ((bp = LIST_FIRST(&blist))) { | |
6284 | LIST_REMOVE(bp, nb_vnbufs); | |
0a7de745 | 6285 | if (!lastbp) { |
b0d623f7 | 6286 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs); |
0a7de745 | 6287 | } else { |
b0d623f7 | 6288 | LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs); |
0a7de745 | 6289 | } |
b0d623f7 | 6290 | lastbp = bp; |
0a7de745 | 6291 | if (error || found) { |
f427ee49 | 6292 | skipped = 1; |
b0d623f7 | 6293 | continue; |
0a7de745 A |
6294 | } |
6295 | if (!purge && dotunder && (count > 100)) { /* don't waste too much time looking for ._ files */ | |
f427ee49 | 6296 | skipped = 1; |
b0d623f7 | 6297 | continue; |
0a7de745 | 6298 | } |
b0d623f7 A |
6299 | nfs_buf_refget(bp); |
6300 | lbn = bp->nb_lblkno; | |
6301 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
6302 | /* just skip this buffer */ | |
6303 | nfs_buf_refrele(bp); | |
f427ee49 | 6304 | skipped = 1; |
b0d623f7 A |
6305 | continue; |
6306 | } | |
6307 | nfs_buf_refrele(bp); | |
6308 | count++; | |
f427ee49 A |
6309 | nfs_dir_buf_cache_lookup_boundaries(bp, &sof, &eof); |
6310 | error = nfs_dir_buf_search(bp, cnp, fh, nvattr, &xid, &attrstamp, NULL, purge ? NDBS_PURGE : 0); | |
b0d623f7 A |
6311 | if (error == ESRCH) { |
6312 | error = 0; | |
6313 | } else { | |
6314 | found = 1; | |
6315 | foundbp = bp; | |
6316 | } | |
6317 | nfs_buf_drop(bp); | |
6318 | } | |
6319 | if (found) { | |
6320 | LIST_REMOVE(foundbp, nb_vnbufs); | |
6321 | LIST_INSERT_HEAD(&dnp->n_cleanblkhd, foundbp, nb_vnbufs); | |
6322 | dnp->n_lastdbl = foundbp->nb_lblkno; | |
6323 | } | |
6324 | nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN); | |
6325 | } | |
f427ee49 A |
6326 | |
6327 | /* If we detect that we fetched full directory listing we should avoid sending lookups for ._ files */ | |
6328 | if (dotunder && !found && !error && eof && sof && !skipped && skipdu) { | |
6329 | *skipdu = 1; | |
6330 | } | |
6331 | ||
b0d623f7 | 6332 | done: |
c3c9b80d | 6333 | lck_mtx_unlock(&nfs_buf_mutex); |
b0d623f7 A |
6334 | |
6335 | if (!error && found && !purge) { | |
f427ee49 A |
6336 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh->fh_data, |
6337 | fh->fh_len, nvattr, &xid, dnp->n_auth, NG_MAKEENTRY, | |
6338 | &newnp); | |
0a7de745 | 6339 | if (error) { |
f427ee49 | 6340 | goto out; |
0a7de745 | 6341 | } |
b0d623f7 A |
6342 | newnp->n_attrstamp = attrstamp; |
6343 | *npp = newnp; | |
6344 | nfs_node_unlock(newnp); | |
6345 | /* check if the dir buffer's attrs are out of date */ | |
f427ee49 | 6346 | if (!nfs_getattr(newnp, nvattr, ctx, NGA_CACHED) && |
b0d623f7 A |
6347 | (newnp->n_attrstamp != attrstamp)) { |
6348 | /* they are, so update them */ | |
0a7de745 | 6349 | error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ | NBLK_ONLYVALID, &bp); |
b0d623f7 A |
6350 | if (!error && bp) { |
6351 | attrstamp = newnp->n_attrstamp; | |
6352 | xid = newnp->n_xid; | |
f427ee49 | 6353 | nfs_dir_buf_search(bp, cnp, fh, nvattr, &xid, &attrstamp, NULL, NDBS_UPDATE); |
b0d623f7 A |
6354 | nfs_buf_release(bp, 0); |
6355 | } | |
6356 | error = 0; | |
6357 | } | |
1c79356b | 6358 | } |
2d21ac55 | 6359 | |
f427ee49 A |
6360 | out: |
6361 | NFS_ZFREE(nfs_fhandle_zone, fh); | |
6362 | FREE(nvattr, M_TEMP); | |
0a7de745 | 6363 | return error; |
1c79356b A |
6364 | } |
6365 | ||
6366 | /* | |
b0d623f7 A |
6367 | * Purge name cache entries for the given node. |
6368 | * For RDIRPLUS, also invalidate the entry in the directory's buffers. | |
6369 | */ | |
6370 | void | |
6371 | nfs_name_cache_purge(nfsnode_t dnp, nfsnode_t np, struct componentname *cnp, vfs_context_t ctx) | |
6372 | { | |
6373 | struct nfsmount *nmp = NFSTONMP(dnp); | |
6374 | ||
6375 | cache_purge(NFSTOV(np)); | |
0a7de745 | 6376 | if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { |
f427ee49 | 6377 | nfs_dir_buf_cache_lookup(dnp, NULL, cnp, ctx, 1, NULL); |
0a7de745 | 6378 | } |
b0d623f7 A |
6379 | } |
6380 | ||
6381 | /* | |
6382 | * NFS V3 readdir (plus) RPC. | |
1c79356b A |
6383 | */ |
6384 | int | |
b0d623f7 | 6385 | nfs3_readdir_rpc(nfsnode_t dnp, struct nfsbuf *bp, vfs_context_t ctx) |
1c79356b | 6386 | { |
55e303ae | 6387 | struct nfsmount *nmp; |
b0d623f7 | 6388 | int error = 0, lockerror, nfsvers, rdirplus, bigcookies; |
f427ee49 | 6389 | int i, status = 0, attrflag, fhflag, more_entries = 1, eof, bp_dropped = 0; |
b0d623f7 | 6390 | uint32_t nmreaddirsize, nmrsize; |
f427ee49 A |
6391 | uint32_t namlen, skiplen, fhlen, xlen, attrlen; |
6392 | uint64_t cookie, lastcookie, xid, savedxid, fileno, space_free, space_needed; | |
b0d623f7 | 6393 | struct nfsm_chain nmreq, nmrep, nmrepsave; |
f427ee49 | 6394 | fhandle_t *fh; |
b0d623f7 A |
6395 | struct nfs_vattr *nvattrp; |
6396 | struct nfs_dir_buf_header *ndbhp; | |
6397 | struct direntry *dp; | |
f427ee49 | 6398 | char *padstart; |
b0d623f7 | 6399 | struct timeval now; |
f427ee49 A |
6400 | uint16_t reclen; |
6401 | size_t padlen; | |
1c79356b | 6402 | |
2d21ac55 | 6403 | nmp = NFSTONMP(dnp); |
0a7de745 A |
6404 | if (nfs_mount_gone(nmp)) { |
6405 | return ENXIO; | |
6406 | } | |
2d21ac55 | 6407 | nfsvers = nmp->nm_vers; |
55e303ae A |
6408 | nmreaddirsize = nmp->nm_readdirsize; |
6409 | nmrsize = nmp->nm_rsize; | |
b0d623f7 | 6410 | bigcookies = nmp->nm_state & NFSSTA_BIGCOOKIES; |
f427ee49 | 6411 | fh = zalloc(nfs_fhandle_zone); |
c3c9b80d | 6412 | resend: |
6d2010ae | 6413 | rdirplus = ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) ? 1 : 0; |
55e303ae | 6414 | |
0a7de745 | 6415 | if ((lockerror = nfs_node_lock(dnp))) { |
f427ee49 | 6416 | NFS_ZFREE(nfs_fhandle_zone, fh); |
0a7de745 A |
6417 | return lockerror; |
6418 | } | |
2d21ac55 | 6419 | |
b0d623f7 A |
6420 | /* determine cookie to use, and move dp to the right offset */ |
6421 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
6422 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
6423 | if (ndbhp->ndbh_count) { | |
0a7de745 | 6424 | for (i = 0; i < ndbhp->ndbh_count - 1; i++) { |
b0d623f7 | 6425 | dp = NFS_DIRENTRY_NEXT(dp); |
0a7de745 | 6426 | } |
b0d623f7 A |
6427 | cookie = dp->d_seekoff; |
6428 | dp = NFS_DIRENTRY_NEXT(dp); | |
6429 | } else { | |
6430 | cookie = bp->nb_lblkno; | |
6431 | /* increment with every buffer read */ | |
316670eb | 6432 | OSAddAtomic64(1, &nfsstats.readdir_bios); |
2d21ac55 | 6433 | } |
b0d623f7 | 6434 | lastcookie = cookie; |
2d21ac55 | 6435 | |
1c79356b | 6436 | /* |
b0d623f7 A |
6437 | * Loop around doing readdir(plus) RPCs of size nm_readdirsize until |
6438 | * the buffer is full (or we hit EOF). Then put the remainder of the | |
6439 | * results in the next buffer(s). | |
1c79356b | 6440 | */ |
2d21ac55 A |
6441 | nfsm_chain_null(&nmreq); |
6442 | nfsm_chain_null(&nmrep); | |
b0d623f7 | 6443 | while (nfs_dir_buf_freespace(bp, rdirplus) && !(ndbhp->ndbh_flags & NDB_FULL)) { |
2d21ac55 | 6444 | nfsm_chain_build_alloc_init(error, &nmreq, |
0a7de745 | 6445 | NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers) + NFSX_UNSIGNED); |
2d21ac55 | 6446 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
b0d623f7 A |
6447 | if (nfsvers == NFS_VER3) { |
6448 | /* opaque values don't need swapping, but as long */ | |
6449 | /* as we are consistent about it, it should be ok */ | |
6450 | nfsm_chain_add_64(error, &nmreq, cookie); | |
6451 | nfsm_chain_add_64(error, &nmreq, dnp->n_cookieverf); | |
6452 | } else { | |
6453 | nfsm_chain_add_32(error, &nmreq, cookie); | |
6454 | } | |
2d21ac55 | 6455 | nfsm_chain_add_32(error, &nmreq, nmreaddirsize); |
0a7de745 | 6456 | if (rdirplus) { |
b0d623f7 | 6457 | nfsm_chain_add_32(error, &nmreq, nmrsize); |
0a7de745 | 6458 | } |
2d21ac55 | 6459 | nfsm_chain_build_done(error, &nmreq); |
b0d623f7 | 6460 | nfs_node_unlock(dnp); |
2d21ac55 A |
6461 | lockerror = ENOENT; |
6462 | nfsmout_if(error); | |
6463 | ||
b0d623f7 | 6464 | error = nfs_request(dnp, NULL, &nmreq, |
0a7de745 A |
6465 | rdirplus ? NFSPROC_READDIRPLUS : NFSPROC_READDIR, |
6466 | ctx, NULL, &nmrep, &xid, &status); | |
2d21ac55 | 6467 | |
0a7de745 | 6468 | if ((lockerror = nfs_node_lock(dnp))) { |
2d21ac55 | 6469 | error = lockerror; |
0a7de745 | 6470 | } |
2d21ac55 | 6471 | |
b0d623f7 | 6472 | savedxid = xid; |
0a7de745 | 6473 | if (nfsvers == NFS_VER3) { |
b0d623f7 | 6474 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); |
0a7de745 A |
6475 | } |
6476 | if (!error) { | |
2d21ac55 | 6477 | error = status; |
0a7de745 A |
6478 | } |
6479 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 6480 | nfsm_chain_get_64(error, &nmrep, dnp->n_cookieverf); |
0a7de745 | 6481 | } |
b0d623f7 | 6482 | nfsm_chain_get_32(error, &nmrep, more_entries); |
2d21ac55 A |
6483 | |
6484 | if (!lockerror) { | |
b0d623f7 | 6485 | nfs_node_unlock(dnp); |
2d21ac55 | 6486 | lockerror = ENOENT; |
1c79356b | 6487 | } |
b0d623f7 A |
6488 | if (error == NFSERR_NOTSUPP) { |
6489 | /* oops... it doesn't look like readdirplus is supported */ | |
6490 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae | 6491 | NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS); |
b0d623f7 | 6492 | lck_mtx_unlock(&nmp->nm_lock); |
c3c9b80d A |
6493 | nfsm_chain_cleanup(&nmreq); |
6494 | nfsm_chain_cleanup(&nmrep); | |
6495 | goto resend; | |
b0d623f7 | 6496 | } |
2d21ac55 | 6497 | nfsmout_if(error); |
1c79356b | 6498 | |
0a7de745 | 6499 | if (rdirplus) { |
b0d623f7 | 6500 | microuptime(&now); |
ea3f0419 A |
6501 | if (lastcookie == 0) { |
6502 | dnp->n_rdirplusstamp_sof = now.tv_sec; | |
6503 | dnp->n_rdirplusstamp_eof = 0; | |
6504 | } | |
0a7de745 | 6505 | } |
b0d623f7 A |
6506 | |
6507 | /* loop through the entries packing them into the buffer */ | |
6508 | while (more_entries) { | |
0a7de745 | 6509 | if (nfsvers == NFS_VER3) { |
b0d623f7 | 6510 | nfsm_chain_get_64(error, &nmrep, fileno); |
0a7de745 | 6511 | } else { |
b0d623f7 | 6512 | nfsm_chain_get_32(error, &nmrep, fileno); |
0a7de745 | 6513 | } |
b0d623f7 | 6514 | nfsm_chain_get_32(error, &nmrep, namlen); |
2d21ac55 | 6515 | nfsmout_if(error); |
b0d623f7 A |
6516 | /* just truncate names that don't fit in direntry.d_name */ |
6517 | if (namlen <= 0) { | |
1c79356b | 6518 | error = EBADRPC; |
1c79356b A |
6519 | goto nfsmout; |
6520 | } | |
0a7de745 | 6521 | if (namlen > (sizeof(dp->d_name) - 1)) { |
b0d623f7 A |
6522 | skiplen = namlen - sizeof(dp->d_name) + 1; |
6523 | namlen = sizeof(dp->d_name) - 1; | |
91447636 A |
6524 | } else { |
6525 | skiplen = 0; | |
6526 | } | |
b0d623f7 A |
6527 | /* guess that fh size will be same as parent */ |
6528 | fhlen = rdirplus ? (1 + dnp->n_fhsize) : 0; | |
6529 | xlen = rdirplus ? (fhlen + sizeof(time_t)) : 0; | |
6530 | attrlen = rdirplus ? sizeof(struct nfs_vattr) : 0; | |
f427ee49 | 6531 | reclen = NFS_DIRENTRY_LEN_16(namlen + xlen); |
b0d623f7 A |
6532 | space_needed = reclen + attrlen; |
6533 | space_free = nfs_dir_buf_freespace(bp, rdirplus); | |
6534 | if (space_needed > space_free) { | |
6535 | /* | |
6536 | * We still have entries to pack, but we've | |
6537 | * run out of room in the current buffer. | |
6538 | * So we need to move to the next buffer. | |
6539 | * The block# for the next buffer is the | |
6540 | * last cookie in the current buffer. | |
6541 | */ | |
6542 | nextbuffer: | |
6543 | ndbhp->ndbh_flags |= NDB_FULL; | |
6544 | nfs_buf_release(bp, 0); | |
6545 | bp_dropped = 1; | |
6546 | bp = NULL; | |
6547 | error = nfs_buf_get(dnp, lastcookie, NFS_DIRBLKSIZ, vfs_context_thread(ctx), NBLK_READ, &bp); | |
2d21ac55 | 6548 | nfsmout_if(error); |
b0d623f7 A |
6549 | /* initialize buffer */ |
6550 | ndbhp = (struct nfs_dir_buf_header*)bp->nb_data; | |
6551 | ndbhp->ndbh_flags = 0; | |
6552 | ndbhp->ndbh_count = 0; | |
6553 | ndbhp->ndbh_entry_end = sizeof(*ndbhp); | |
6554 | ndbhp->ndbh_ncgen = dnp->n_ncgen; | |
6555 | space_free = nfs_dir_buf_freespace(bp, rdirplus); | |
6556 | dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp); | |
6557 | /* increment with every buffer read */ | |
316670eb | 6558 | OSAddAtomic64(1, &nfsstats.readdir_bios); |
91447636 | 6559 | } |
b0d623f7 A |
6560 | nmrepsave = nmrep; |
6561 | dp->d_fileno = fileno; | |
f427ee49 | 6562 | dp->d_namlen = (uint16_t)namlen; |
b0d623f7 A |
6563 | dp->d_reclen = reclen; |
6564 | dp->d_type = DT_UNKNOWN; | |
6565 | nfsm_chain_get_opaque(error, &nmrep, namlen, dp->d_name); | |
2d21ac55 | 6566 | nfsmout_if(error); |
b0d623f7 | 6567 | dp->d_name[namlen] = '\0'; |
0a7de745 | 6568 | if (skiplen) { |
b0d623f7 | 6569 | nfsm_chain_adv(error, &nmrep, |
0a7de745 A |
6570 | nfsm_rndup(namlen + skiplen) - nfsm_rndup(namlen)); |
6571 | } | |
6572 | if (nfsvers == NFS_VER3) { | |
b0d623f7 | 6573 | nfsm_chain_get_64(error, &nmrep, cookie); |
0a7de745 | 6574 | } else { |
b0d623f7 | 6575 | nfsm_chain_get_32(error, &nmrep, cookie); |
0a7de745 | 6576 | } |
b0d623f7 A |
6577 | nfsmout_if(error); |
6578 | dp->d_seekoff = cookie; | |
6579 | if (!bigcookies && (cookie >> 32) && (nmp == NFSTONMP(dnp))) { | |
6580 | /* we've got a big cookie, make sure flag is set */ | |
6581 | lck_mtx_lock(&nmp->nm_lock); | |
6582 | nmp->nm_state |= NFSSTA_BIGCOOKIES; | |
6583 | lck_mtx_unlock(&nmp->nm_lock); | |
6584 | bigcookies = 1; | |
6585 | } | |
6586 | if (rdirplus) { | |
6587 | nvattrp = NFS_DIR_BUF_NVATTR(bp, ndbhp->ndbh_count); | |
6588 | /* check for attributes */ | |
6589 | nfsm_chain_get_32(error, &nmrep, attrflag); | |
2d21ac55 | 6590 | nfsmout_if(error); |
b0d623f7 A |
6591 | if (attrflag) { |
6592 | /* grab attributes */ | |
cb323159 | 6593 | error = nfs_parsefattr(nmp, &nmrep, NFS_VER3, nvattrp); |
b0d623f7 A |
6594 | nfsmout_if(error); |
6595 | dp->d_type = IFTODT(VTTOIF(nvattrp->nva_type)); | |
6596 | /* fileid is already in d_fileno, so stash xid in attrs */ | |
6597 | nvattrp->nva_fileid = savedxid; | |
f427ee49 | 6598 | nvattrp->nva_flags |= NFS_FFLAG_FILEID_CONTAINS_XID; |
b0d623f7 A |
6599 | } else { |
6600 | /* mark the attributes invalid */ | |
6601 | bzero(nvattrp, sizeof(struct nfs_vattr)); | |
6602 | } | |
6603 | /* check for file handle */ | |
6604 | nfsm_chain_get_32(error, &nmrep, fhflag); | |
6605 | nfsmout_if(error); | |
6606 | if (fhflag) { | |
f427ee49 | 6607 | nfsm_chain_get_fh(error, &nmrep, NFS_VER3, fh); |
b0d623f7 | 6608 | nfsmout_if(error); |
f427ee49 | 6609 | fhlen = fh->fh_len + 1; |
b0d623f7 | 6610 | xlen = fhlen + sizeof(time_t); |
f427ee49 | 6611 | reclen = NFS_DIRENTRY_LEN_16(namlen + xlen); |
b0d623f7 A |
6612 | space_needed = reclen + attrlen; |
6613 | if (space_needed > space_free) { | |
6614 | /* didn't actually have the room... move on to next buffer */ | |
6615 | nmrep = nmrepsave; | |
6616 | goto nextbuffer; | |
2d21ac55 | 6617 | } |
b0d623f7 | 6618 | /* pack the file handle into the record */ |
f427ee49 A |
6619 | dp->d_name[dp->d_namlen + 1] = (unsigned char)fh->fh_len; /* No truncation because fh_len's value is checked during nfsm_chain_get_fh() */ |
6620 | bcopy(fh->fh_data, &dp->d_name[dp->d_namlen + 2], fh->fh_len); | |
1c79356b | 6621 | } else { |
b0d623f7 | 6622 | /* mark the file handle invalid */ |
f427ee49 A |
6623 | fh->fh_len = 0; |
6624 | fhlen = fh->fh_len + 1; | |
b0d623f7 | 6625 | xlen = fhlen + sizeof(time_t); |
f427ee49 | 6626 | reclen = NFS_DIRENTRY_LEN_16(namlen + xlen); |
0a7de745 | 6627 | bzero(&dp->d_name[dp->d_namlen + 1], fhlen); |
1c79356b | 6628 | } |
0a7de745 | 6629 | *(time_t*)(&dp->d_name[dp->d_namlen + 1 + fhlen]) = now.tv_sec; |
b0d623f7 | 6630 | dp->d_reclen = reclen; |
f427ee49 | 6631 | nfs_rdirplus_update_node_attrs(dnp, dp, fh, nvattrp, &savedxid); |
1c79356b | 6632 | } |
b0d623f7 A |
6633 | padstart = dp->d_name + dp->d_namlen + 1 + xlen; |
6634 | ndbhp->ndbh_count++; | |
6635 | lastcookie = cookie; | |
6636 | /* advance to next direntry in buffer */ | |
6637 | dp = NFS_DIRENTRY_NEXT(dp); | |
6638 | ndbhp->ndbh_entry_end = (char*)dp - bp->nb_data; | |
6639 | /* zero out the pad bytes */ | |
6640 | padlen = (char*)dp - padstart; | |
0a7de745 | 6641 | if (padlen > 0) { |
b0d623f7 | 6642 | bzero(padstart, padlen); |
0a7de745 | 6643 | } |
b0d623f7 A |
6644 | /* check for more entries */ |
6645 | nfsm_chain_get_32(error, &nmrep, more_entries); | |
2d21ac55 | 6646 | nfsmout_if(error); |
1c79356b | 6647 | } |
b0d623f7 A |
6648 | /* Finally, get the eof boolean */ |
6649 | nfsm_chain_get_32(error, &nmrep, eof); | |
6650 | nfsmout_if(error); | |
6651 | if (eof) { | |
0a7de745 | 6652 | ndbhp->ndbh_flags |= (NDB_FULL | NDB_EOF); |
b0d623f7 A |
6653 | nfs_node_lock_force(dnp); |
6654 | dnp->n_eofcookie = lastcookie; | |
ea3f0419 A |
6655 | if (rdirplus) { |
6656 | dnp->n_rdirplusstamp_eof = now.tv_sec; | |
6657 | } | |
b0d623f7 A |
6658 | nfs_node_unlock(dnp); |
6659 | } else { | |
6660 | more_entries = 1; | |
6661 | } | |
6662 | if (bp_dropped) { | |
6663 | nfs_buf_release(bp, 0); | |
6664 | bp = NULL; | |
6665 | break; | |
1c79356b | 6666 | } |
0a7de745 | 6667 | if ((lockerror = nfs_node_lock(dnp))) { |
2d21ac55 | 6668 | error = lockerror; |
0a7de745 | 6669 | } |
2d21ac55 A |
6670 | nfsmout_if(error); |
6671 | nfsm_chain_cleanup(&nmrep); | |
6672 | nfsm_chain_null(&nmreq); | |
6673 | } | |
1c79356b | 6674 | nfsmout: |
0a7de745 | 6675 | if (bp_dropped && bp) { |
b0d623f7 | 6676 | nfs_buf_release(bp, 0); |
0a7de745 A |
6677 | } |
6678 | if (!lockerror) { | |
b0d623f7 | 6679 | nfs_node_unlock(dnp); |
0a7de745 | 6680 | } |
2d21ac55 A |
6681 | nfsm_chain_cleanup(&nmreq); |
6682 | nfsm_chain_cleanup(&nmrep); | |
f427ee49 | 6683 | NFS_ZFREE(nfs_fhandle_zone, fh); |
0a7de745 | 6684 | return bp_dropped ? NFSERR_DIRBUFDROPPED : error; |
1c79356b A |
6685 | } |
6686 | ||
6687 | /* | |
6688 | * Silly rename. To make the NFS filesystem that is stateless look a little | |
6689 | * more like the "ufs" a remove of an active vnode is translated to a rename | |
2d21ac55 | 6690 | * to a funny looking filename that is removed by nfs_vnop_inactive on the |
1c79356b | 6691 | * nfsnode. There is the potential for another process on a different client |
2d21ac55 A |
6692 | * to create the same funny name between when the lookitup() fails and the |
6693 | * rename() completes, but... | |
1c79356b | 6694 | */ |
55e303ae | 6695 | |
2d21ac55 A |
6696 | /* format of "random" silly names - includes a number and pid */ |
6697 | /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */ | |
6698 | #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x" | |
6699 | /* starting from zero isn't silly enough */ | |
6700 | static uint32_t nfs_sillyrename_number = 0x20051025; | |
55e303ae | 6701 | |
b0d623f7 | 6702 | int |
91447636 | 6703 | nfs_sillyrename( |
2d21ac55 A |
6704 | nfsnode_t dnp, |
6705 | nfsnode_t np, | |
91447636 | 6706 | struct componentname *cnp, |
2d21ac55 | 6707 | vfs_context_t ctx) |
1c79356b | 6708 | { |
2d21ac55 | 6709 | struct nfs_sillyrename *nsp; |
1c79356b | 6710 | int error; |
f427ee49 | 6711 | pid_t pid; |
2d21ac55 A |
6712 | kauth_cred_t cred; |
6713 | uint32_t num; | |
6714 | struct nfsmount *nmp; | |
1c79356b | 6715 | |
2d21ac55 | 6716 | nmp = NFSTONMP(dnp); |
0a7de745 A |
6717 | if (nfs_mount_gone(nmp)) { |
6718 | return ENXIO; | |
6719 | } | |
2d21ac55 | 6720 | |
b0d623f7 | 6721 | nfs_name_cache_purge(dnp, np, cnp, ctx); |
2d21ac55 | 6722 | |
f427ee49 A |
6723 | MALLOC(nsp, struct nfs_sillyrename *, |
6724 | sizeof(struct nfs_sillyrename), M_TEMP, M_WAITOK); | |
0a7de745 A |
6725 | if (!nsp) { |
6726 | return ENOMEM; | |
6727 | } | |
2d21ac55 | 6728 | cred = vfs_context_ucred(ctx); |
91447636 | 6729 | kauth_cred_ref(cred); |
2d21ac55 A |
6730 | nsp->nsr_cred = cred; |
6731 | nsp->nsr_dnp = dnp; | |
6732 | error = vnode_ref(NFSTOV(dnp)); | |
0a7de745 | 6733 | if (error) { |
91447636 | 6734 | goto bad_norele; |
0a7de745 | 6735 | } |
1c79356b A |
6736 | |
6737 | /* Fudge together a funny name */ | |
2d21ac55 | 6738 | pid = vfs_context_pid(ctx); |
b0d623f7 | 6739 | num = OSAddAtomic(1, &nfs_sillyrename_number); |
2d21ac55 | 6740 | nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name), |
0a7de745 A |
6741 | NFS_SILLYNAME_FORMAT, num, (pid & 0xffff)); |
6742 | if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name)) { | |
2d21ac55 | 6743 | nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1; |
0a7de745 | 6744 | } |
1c79356b A |
6745 | |
6746 | /* Try lookitups until we get one that isn't there */ | |
2d21ac55 | 6747 | while (nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, NULL) == 0) { |
b0d623f7 | 6748 | num = OSAddAtomic(1, &nfs_sillyrename_number); |
2d21ac55 | 6749 | nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name), |
0a7de745 A |
6750 | NFS_SILLYNAME_FORMAT, num, (pid & 0xffff)); |
6751 | if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name)) { | |
2d21ac55 | 6752 | nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1; |
0a7de745 | 6753 | } |
55e303ae | 6754 | } |
2d21ac55 | 6755 | |
55e303ae | 6756 | /* now, do the rename */ |
2d21ac55 | 6757 | error = nmp->nm_funcs->nf_rename_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen, |
0a7de745 | 6758 | dnp, nsp->nsr_name, nsp->nsr_namlen, ctx); |
6d2010ae A |
6759 | |
6760 | /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */ | |
0a7de745 | 6761 | if (error == ENOENT) { |
6d2010ae | 6762 | error = 0; |
0a7de745 | 6763 | } |
b0d623f7 A |
6764 | if (!error) { |
6765 | nfs_node_lock_force(dnp); | |
6766 | if (dnp->n_flag & NNEGNCENTRIES) { | |
6767 | dnp->n_flag &= ~NNEGNCENTRIES; | |
6768 | cache_purge_negatives(NFSTOV(dnp)); | |
6769 | } | |
6770 | nfs_node_unlock(dnp); | |
2d21ac55 A |
6771 | } |
6772 | FSDBG(267, dnp, np, num, error); | |
0a7de745 | 6773 | if (error) { |
1c79356b | 6774 | goto bad; |
0a7de745 | 6775 | } |
2d21ac55 | 6776 | error = nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, &np); |
b0d623f7 | 6777 | nfs_node_lock_force(np); |
2d21ac55 | 6778 | np->n_sillyrename = nsp; |
b0d623f7 | 6779 | nfs_node_unlock(np); |
0a7de745 | 6780 | return 0; |
1c79356b | 6781 | bad: |
2d21ac55 | 6782 | vnode_rele(NFSTOV(dnp)); |
91447636 | 6783 | bad_norele: |
2d21ac55 A |
6784 | nsp->nsr_cred = NOCRED; |
6785 | kauth_cred_unref(&cred); | |
f427ee49 | 6786 | FREE(nsp, M_TEMP); |
0a7de745 | 6787 | return error; |
2d21ac55 A |
6788 | } |
6789 | ||
6790 | int | |
6791 | nfs3_lookup_rpc_async( | |
6792 | nfsnode_t dnp, | |
6793 | char *name, | |
6794 | int namelen, | |
6795 | vfs_context_t ctx, | |
6796 | struct nfsreq **reqp) | |
6797 | { | |
6798 | struct nfsmount *nmp; | |
6799 | struct nfsm_chain nmreq; | |
6800 | int error = 0, nfsvers; | |
6801 | ||
6802 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6803 | if (nfs_mount_gone(nmp)) { |
6804 | return ENXIO; | |
6805 | } | |
2d21ac55 A |
6806 | nfsvers = nmp->nm_vers; |
6807 | ||
6808 | nfsm_chain_null(&nmreq); | |
6809 | ||
6810 | nfsm_chain_build_alloc_init(error, &nmreq, | |
0a7de745 | 6811 | NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen)); |
2d21ac55 | 6812 | nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize); |
6d2010ae | 6813 | nfsm_chain_add_name(error, &nmreq, name, namelen, nmp); |
2d21ac55 A |
6814 | nfsm_chain_build_done(error, &nmreq); |
6815 | nfsmout_if(error); | |
6816 | error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_LOOKUP, | |
0a7de745 | 6817 | vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, reqp); |
2d21ac55 A |
6818 | nfsmout: |
6819 | nfsm_chain_cleanup(&nmreq); | |
0a7de745 | 6820 | return error; |
2d21ac55 A |
6821 | } |
6822 | ||
6823 | int | |
6824 | nfs3_lookup_rpc_async_finish( | |
6825 | nfsnode_t dnp, | |
6d2010ae A |
6826 | __unused char *name, |
6827 | __unused int namelen, | |
2d21ac55 A |
6828 | vfs_context_t ctx, |
6829 | struct nfsreq *req, | |
6830 | u_int64_t *xidp, | |
6831 | fhandle_t *fhp, | |
6832 | struct nfs_vattr *nvap) | |
6833 | { | |
f427ee49 | 6834 | int error = 0, lockerror = ENOENT, status = 0, nfsvers, attrflag; |
2d21ac55 A |
6835 | u_int64_t xid; |
6836 | struct nfsmount *nmp; | |
6837 | struct nfsm_chain nmrep; | |
6838 | ||
6839 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6840 | if (nmp == NULL) { |
6841 | return ENXIO; | |
6842 | } | |
2d21ac55 A |
6843 | nfsvers = nmp->nm_vers; |
6844 | ||
6845 | nfsm_chain_null(&nmrep); | |
6846 | ||
6847 | error = nfs_request_async_finish(req, &nmrep, xidp, &status); | |
6848 | ||
0a7de745 | 6849 | if ((lockerror = nfs_node_lock(dnp))) { |
b0d623f7 | 6850 | error = lockerror; |
0a7de745 | 6851 | } |
2d21ac55 A |
6852 | xid = *xidp; |
6853 | if (error || status) { | |
0a7de745 | 6854 | if (nfsvers == NFS_VER3) { |
2d21ac55 | 6855 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); |
0a7de745 A |
6856 | } |
6857 | if (!error) { | |
2d21ac55 | 6858 | error = status; |
0a7de745 | 6859 | } |
2d21ac55 A |
6860 | goto nfsmout; |
6861 | } | |
6862 | ||
6863 | nfsmout_if(error || !fhp || !nvap); | |
6864 | ||
6865 | /* get the file handle */ | |
6866 | nfsm_chain_get_fh(error, &nmrep, nfsvers, fhp); | |
6867 | ||
6868 | /* get the attributes */ | |
6869 | if (nfsvers == NFS_VER3) { | |
cb323159 | 6870 | nfsm_chain_postop_attr_get(nmp, error, &nmrep, attrflag, nvap); |
2d21ac55 | 6871 | nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid); |
0a7de745 | 6872 | if (!error && !attrflag) { |
6d2010ae | 6873 | error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp); |
0a7de745 | 6874 | } |
2d21ac55 | 6875 | } else { |
cb323159 | 6876 | error = nfs_parsefattr(nmp, &nmrep, nfsvers, nvap); |
2d21ac55 A |
6877 | } |
6878 | nfsmout: | |
0a7de745 | 6879 | if (!lockerror) { |
b0d623f7 | 6880 | nfs_node_unlock(dnp); |
0a7de745 | 6881 | } |
2d21ac55 | 6882 | nfsm_chain_cleanup(&nmrep); |
0a7de745 | 6883 | return error; |
1c79356b A |
6884 | } |
6885 | ||
6886 | /* | |
6887 | * Look up a file name and optionally either update the file handle or | |
6888 | * allocate an nfsnode, depending on the value of npp. | |
6889 | * npp == NULL --> just do the lookup | |
6890 | * *npp == NULL --> allocate a new nfsnode and make sure attributes are | |
6891 | * handled too | |
6892 | * *npp != NULL --> update the file handle in the vnode | |
6893 | */ | |
2d21ac55 A |
6894 | int |
6895 | nfs_lookitup( | |
6896 | nfsnode_t dnp, | |
6897 | char *name, | |
6898 | int namelen, | |
6899 | vfs_context_t ctx, | |
6900 | nfsnode_t *npp) | |
6901 | { | |
6902 | int error = 0; | |
6903 | nfsnode_t np, newnp = NULL; | |
6904 | u_int64_t xid; | |
f427ee49 | 6905 | fhandle_t *fh; |
2d21ac55 | 6906 | struct nfsmount *nmp; |
f427ee49 A |
6907 | struct nfs_vattr *nvattr; |
6908 | struct nfsreq *req; | |
2d21ac55 A |
6909 | |
6910 | nmp = NFSTONMP(dnp); | |
0a7de745 A |
6911 | if (nfs_mount_gone(nmp)) { |
6912 | return ENXIO; | |
6913 | } | |
2d21ac55 A |
6914 | |
6915 | if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) && | |
f427ee49 | 6916 | (namelen > nmp->nm_fsattr.nfsa_maxname)) { |
0a7de745 A |
6917 | return ENAMETOOLONG; |
6918 | } | |
2d21ac55 | 6919 | |
f427ee49 A |
6920 | fh = zalloc(nfs_fhandle_zone); |
6921 | req = zalloc_flags(nfs_req_zone, Z_WAITOK); | |
6922 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
6923 | NVATTR_INIT(nvattr); | |
6d2010ae | 6924 | |
2d21ac55 A |
6925 | /* check for lookup of "." */ |
6926 | if ((name[0] == '.') && (namelen == 1)) { | |
6927 | /* skip lookup, we know who we are */ | |
f427ee49 | 6928 | fh->fh_len = 0; |
2d21ac55 A |
6929 | newnp = dnp; |
6930 | goto nfsmout; | |
6931 | } | |
6932 | ||
6933 | error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, name, namelen, ctx, &req); | |
6934 | nfsmout_if(error); | |
f427ee49 | 6935 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, name, namelen, ctx, req, &xid, fh, nvattr); |
2d21ac55 A |
6936 | nfsmout_if(!npp || error); |
6937 | ||
6938 | if (*npp) { | |
6939 | np = *npp; | |
f427ee49 | 6940 | if (fh->fh_len != np->n_fhsize) { |
2d21ac55 | 6941 | u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL; |
f427ee49 A |
6942 | if (fh->fh_len > NFS_SMALLFH) { |
6943 | MALLOC(np->n_fhp, u_char *, fh->fh_len, M_NFSBIGFH, M_WAITOK); | |
2d21ac55 | 6944 | if (!np->n_fhp) { |
0a7de745 A |
6945 | np->n_fhp = oldbuf; |
6946 | error = ENOMEM; | |
6947 | goto nfsmout; | |
2d21ac55 A |
6948 | } |
6949 | } else { | |
6950 | np->n_fhp = &np->n_fh[0]; | |
6951 | } | |
0a7de745 | 6952 | if (oldbuf) { |
f427ee49 | 6953 | FREE(oldbuf, M_NFSBIGFH); |
0a7de745 | 6954 | } |
2d21ac55 | 6955 | } |
f427ee49 A |
6956 | bcopy(fh->fh_data, np->n_fhp, fh->fh_len); |
6957 | np->n_fhsize = fh->fh_len; | |
b0d623f7 | 6958 | nfs_node_lock_force(np); |
f427ee49 | 6959 | error = nfs_loadattrcache(np, nvattr, &xid, 0); |
b0d623f7 | 6960 | nfs_node_unlock(np); |
2d21ac55 A |
6961 | nfsmout_if(error); |
6962 | newnp = np; | |
f427ee49 | 6963 | } else if (NFS_CMPFH(dnp, fh->fh_data, fh->fh_len)) { |
b0d623f7 | 6964 | nfs_node_lock_force(dnp); |
0a7de745 | 6965 | if (dnp->n_xid <= xid) { |
f427ee49 | 6966 | error = nfs_loadattrcache(dnp, nvattr, &xid, 0); |
0a7de745 | 6967 | } |
b0d623f7 | 6968 | nfs_node_unlock(dnp); |
2d21ac55 A |
6969 | nfsmout_if(error); |
6970 | newnp = dnp; | |
6971 | } else { | |
6972 | struct componentname cn, *cnp = &cn; | |
6973 | bzero(cnp, sizeof(*cnp)); | |
6974 | cnp->cn_nameptr = name; | |
6975 | cnp->cn_namelen = namelen; | |
f427ee49 A |
6976 | error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh->fh_data, fh->fh_len, |
6977 | nvattr, &xid, req->r_auth, NG_MAKEENTRY, &np); | |
2d21ac55 A |
6978 | nfsmout_if(error); |
6979 | newnp = np; | |
6980 | } | |
6981 | ||
6982 | nfsmout: | |
0a7de745 | 6983 | if (npp && !*npp && !error) { |
2d21ac55 | 6984 | *npp = newnp; |
0a7de745 | 6985 | } |
f427ee49 A |
6986 | NVATTR_CLEANUP(nvattr); |
6987 | NFS_ZFREE(nfs_fhandle_zone, fh); | |
6988 | NFS_ZFREE(nfs_req_zone, req); | |
6989 | FREE(nvattr, M_TEMP); | |
0a7de745 | 6990 | return error; |
2d21ac55 A |
6991 | } |
6992 | ||
6993 | /* | |
6994 | * set up and initialize a "._" file lookup structure used for | |
6995 | * performing async lookups. | |
6996 | */ | |
6997 | void | |
b0d623f7 | 6998 | nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen, vfs_context_t ctx) |
2d21ac55 A |
6999 | { |
7000 | int error, du_namelen; | |
7001 | vnode_t du_vp; | |
6d2010ae | 7002 | struct nfsmount *nmp = NFSTONMP(dnp); |
2d21ac55 A |
7003 | |
7004 | /* check for ._ file in name cache */ | |
7005 | dulp->du_flags = 0; | |
7006 | bzero(&dulp->du_cn, sizeof(dulp->du_cn)); | |
7007 | du_namelen = namelen + 2; | |
0a7de745 | 7008 | if (!nmp || NMFLAG(nmp, NONEGNAMECACHE)) { |
6d2010ae | 7009 | return; |
0a7de745 A |
7010 | } |
7011 | if ((namelen >= 2) && (name[0] == '.') && (name[1] == '_')) { | |
2d21ac55 | 7012 | return; |
0a7de745 A |
7013 | } |
7014 | if (du_namelen >= (int)sizeof(dulp->du_smallname)) { | |
2d21ac55 | 7015 | MALLOC(dulp->du_cn.cn_nameptr, char *, du_namelen + 1, M_TEMP, M_WAITOK); |
0a7de745 | 7016 | } else { |
2d21ac55 | 7017 | dulp->du_cn.cn_nameptr = dulp->du_smallname; |
0a7de745 A |
7018 | } |
7019 | if (!dulp->du_cn.cn_nameptr) { | |
2d21ac55 | 7020 | return; |
0a7de745 | 7021 | } |
2d21ac55 A |
7022 | dulp->du_cn.cn_namelen = du_namelen; |
7023 | snprintf(dulp->du_cn.cn_nameptr, du_namelen + 1, "._%s", name); | |
7024 | dulp->du_cn.cn_nameptr[du_namelen] = '\0'; | |
b0d623f7 A |
7025 | dulp->du_cn.cn_nameiop = LOOKUP; |
7026 | dulp->du_cn.cn_flags = MAKEENTRY; | |
2d21ac55 A |
7027 | |
7028 | error = cache_lookup(NFSTOV(dnp), &du_vp, &dulp->du_cn); | |
b0d623f7 | 7029 | if (error == -1) { |
2d21ac55 | 7030 | vnode_put(du_vp); |
b0d623f7 | 7031 | } else if (!error) { |
6d2010ae A |
7032 | nmp = NFSTONMP(dnp); |
7033 | if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) { | |
b0d623f7 A |
7034 | /* if rdirplus, try dir buf cache lookup */ |
7035 | nfsnode_t du_np = NULL; | |
f427ee49 | 7036 | if (!nfs_dir_buf_cache_lookup(dnp, &du_np, &dulp->du_cn, ctx, 0, NULL) && du_np) { |
b0d623f7 A |
7037 | /* dir buf cache hit */ |
7038 | du_vp = NFSTOV(du_np); | |
7039 | vnode_put(du_vp); | |
7040 | error = -1; | |
7041 | } | |
7042 | } | |
0a7de745 | 7043 | if (!error) { |
b0d623f7 | 7044 | dulp->du_flags |= NFS_DULOOKUP_DOIT; |
0a7de745 | 7045 | } |
b0d623f7 | 7046 | } |
2d21ac55 A |
7047 | } |
7048 | ||
7049 | /* | |
7050 | * start an async "._" file lookup request | |
7051 | */ | |
7052 | void | |
7053 | nfs_dulookup_start(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx) | |
7054 | { | |
7055 | struct nfsmount *nmp = NFSTONMP(dnp); | |
7056 | struct nfsreq *req = &dulp->du_req; | |
7057 | ||
0a7de745 | 7058 | if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT) || (dulp->du_flags & NFS_DULOOKUP_INPROG)) { |
2d21ac55 | 7059 | return; |
0a7de745 | 7060 | } |
2d21ac55 | 7061 | if (!nmp->nm_funcs->nf_lookup_rpc_async(dnp, dulp->du_cn.cn_nameptr, |
0a7de745 | 7062 | dulp->du_cn.cn_namelen, ctx, &req)) { |
2d21ac55 | 7063 | dulp->du_flags |= NFS_DULOOKUP_INPROG; |
0a7de745 | 7064 | } |
2d21ac55 A |
7065 | } |
7066 | ||
7067 | /* | |
7068 | * finish an async "._" file lookup request and clean up the structure | |
7069 | */ | |
7070 | void | |
7071 | nfs_dulookup_finish(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx) | |
1c79356b | 7072 | { |
2d21ac55 A |
7073 | struct nfsmount *nmp = NFSTONMP(dnp); |
7074 | int error; | |
7075 | nfsnode_t du_np; | |
7076 | u_int64_t xid; | |
f427ee49 A |
7077 | fhandle_t *fh; |
7078 | struct nfs_vattr *nvattr; | |
1c79356b | 7079 | |
0a7de745 | 7080 | if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_INPROG)) { |
2d21ac55 | 7081 | goto out; |
0a7de745 | 7082 | } |
55e303ae | 7083 | |
f427ee49 A |
7084 | fh = zalloc(nfs_fhandle_zone); |
7085 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); | |
7086 | NVATTR_INIT(nvattr); | |
6d2010ae | 7087 | error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, dulp->du_cn.cn_nameptr, |
f427ee49 | 7088 | dulp->du_cn.cn_namelen, ctx, &dulp->du_req, &xid, fh, nvattr); |
2d21ac55 A |
7089 | dulp->du_flags &= ~NFS_DULOOKUP_INPROG; |
7090 | if (error == ENOENT) { | |
7091 | /* add a negative entry in the name cache */ | |
b0d623f7 | 7092 | nfs_node_lock_force(dnp); |
2d21ac55 A |
7093 | cache_enter(NFSTOV(dnp), NULL, &dulp->du_cn); |
7094 | dnp->n_flag |= NNEGNCENTRIES; | |
b0d623f7 | 7095 | nfs_node_unlock(dnp); |
2d21ac55 | 7096 | } else if (!error) { |
f427ee49 A |
7097 | error = nfs_nget(NFSTOMP(dnp), dnp, &dulp->du_cn, fh->fh_data, fh->fh_len, |
7098 | nvattr, &xid, dulp->du_req.r_auth, NG_MAKEENTRY, &du_np); | |
2d21ac55 | 7099 | if (!error) { |
b0d623f7 | 7100 | nfs_node_unlock(du_np); |
2d21ac55 | 7101 | vnode_put(NFSTOV(du_np)); |
1c79356b | 7102 | } |
1c79356b | 7103 | } |
f427ee49 A |
7104 | NVATTR_CLEANUP(nvattr); |
7105 | NFS_ZFREE(nfs_fhandle_zone, fh); | |
7106 | FREE(nvattr, M_TEMP); | |
2d21ac55 | 7107 | out: |
0a7de745 | 7108 | if (dulp->du_flags & NFS_DULOOKUP_INPROG) { |
2d21ac55 | 7109 | nfs_request_async_cancel(&dulp->du_req); |
0a7de745 A |
7110 | } |
7111 | if (dulp->du_cn.cn_nameptr && (dulp->du_cn.cn_nameptr != dulp->du_smallname)) { | |
2d21ac55 | 7112 | FREE(dulp->du_cn.cn_nameptr, M_TEMP); |
0a7de745 | 7113 | } |
1c79356b A |
7114 | } |
7115 | ||
2d21ac55 | 7116 | |
1c79356b | 7117 | /* |
2d21ac55 | 7118 | * NFS Version 3 commit RPC |
1c79356b | 7119 | */ |
55e303ae | 7120 | int |
2d21ac55 A |
7121 | nfs3_commit_rpc( |
7122 | nfsnode_t np, | |
6d2010ae A |
7123 | uint64_t offset, |
7124 | uint64_t count, | |
7125 | kauth_cred_t cred, | |
7126 | uint64_t wverf) | |
1c79356b | 7127 | { |
2d21ac55 | 7128 | struct nfsmount *nmp; |
f427ee49 | 7129 | int error = 0, lockerror, status = 0, wccpostattr = 0, nfsvers; |
cb323159 | 7130 | struct timespec premtime = { .tv_sec = 0, .tv_nsec = 0 }; |
6d2010ae | 7131 | u_int64_t xid, newwverf; |
2d21ac55 A |
7132 | uint32_t count32; |
7133 | struct nfsm_chain nmreq, nmrep; | |
8f6c56a5 | 7134 | |
2d21ac55 A |
7135 | nmp = NFSTONMP(np); |
7136 | FSDBG(521, np, offset, count, nmp ? nmp->nm_state : 0); | |
0a7de745 A |
7137 | if (nfs_mount_gone(nmp)) { |
7138 | return ENXIO; | |
7139 | } | |
7140 | if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) { | |
7141 | return 0; | |
7142 | } | |
2d21ac55 | 7143 | nfsvers = nmp->nm_vers; |
f427ee49 | 7144 | count32 = count > UINT32_MAX ? 0 : (uint32_t)count; |
2d21ac55 A |
7145 | |
7146 | nfsm_chain_null(&nmreq); | |
7147 | nfsm_chain_null(&nmrep); | |
7148 | ||
7149 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3)); | |
7150 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
7151 | nfsm_chain_add_64(error, &nmreq, offset); | |
7152 | nfsm_chain_add_32(error, &nmreq, count32); | |
7153 | nfsm_chain_build_done(error, &nmreq); | |
7154 | nfsmout_if(error); | |
7155 | error = nfs_request2(np, NULL, &nmreq, NFSPROC_COMMIT, | |
0a7de745 A |
7156 | current_thread(), cred, NULL, 0, &nmrep, &xid, &status); |
7157 | if ((lockerror = nfs_node_lock(np))) { | |
2d21ac55 | 7158 | error = lockerror; |
0a7de745 | 7159 | } |
2d21ac55 A |
7160 | /* can we do anything useful with the wcc info? */ |
7161 | nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid); | |
0a7de745 | 7162 | if (!lockerror) { |
b0d623f7 | 7163 | nfs_node_unlock(np); |
0a7de745 A |
7164 | } |
7165 | if (!error) { | |
2d21ac55 | 7166 | error = status; |
0a7de745 | 7167 | } |
6d2010ae | 7168 | nfsm_chain_get_64(error, &nmrep, newwverf); |
2d21ac55 A |
7169 | nfsmout_if(error); |
7170 | lck_mtx_lock(&nmp->nm_lock); | |
0a7de745 | 7171 | if (nmp->nm_verf != newwverf) { |
6d2010ae | 7172 | nmp->nm_verf = newwverf; |
0a7de745 A |
7173 | } |
7174 | if (wverf != newwverf) { | |
2d21ac55 | 7175 | error = NFSERR_STALEWRITEVERF; |
0a7de745 | 7176 | } |
2d21ac55 A |
7177 | lck_mtx_unlock(&nmp->nm_lock); |
7178 | nfsmout: | |
7179 | nfsm_chain_cleanup(&nmreq); | |
7180 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 7181 | return error; |
1c79356b A |
7182 | } |
7183 | ||
2d21ac55 | 7184 | |
b0d623f7 | 7185 | int |
2d21ac55 | 7186 | nfs_vnop_blockmap( |
91447636 | 7187 | __unused struct vnop_blockmap_args /* { |
0a7de745 A |
7188 | * struct vnodeop_desc *a_desc; |
7189 | * vnode_t a_vp; | |
7190 | * off_t a_foffset; | |
7191 | * size_t a_size; | |
7192 | * daddr64_t *a_bpn; | |
7193 | * size_t *a_run; | |
7194 | * void *a_poff; | |
7195 | * int a_flags; | |
7196 | * } */*ap) | |
1c79356b | 7197 | { |
0a7de745 | 7198 | return ENOTSUP; |
1c79356b A |
7199 | } |
7200 | ||
1c79356b A |
7201 | |
7202 | /* | |
2d21ac55 | 7203 | * fsync vnode op. Just call nfs_flush(). |
1c79356b A |
7204 | */ |
7205 | /* ARGSUSED */ | |
b0d623f7 | 7206 | int |
2d21ac55 | 7207 | nfs_vnop_fsync( |
91447636 | 7208 | struct vnop_fsync_args /* { |
0a7de745 A |
7209 | * struct vnodeop_desc *a_desc; |
7210 | * vnode_t a_vp; | |
7211 | * int a_waitfor; | |
7212 | * vfs_context_t a_context; | |
7213 | * } */*ap) | |
1c79356b | 7214 | { |
0a7de745 | 7215 | return nfs_flush(VTONFS(ap->a_vp), ap->a_waitfor, vfs_context_thread(ap->a_context), 0); |
1c79356b | 7216 | } |
1c79356b | 7217 | |
1c79356b | 7218 | |
55e303ae | 7219 | /* |
2d21ac55 | 7220 | * Do an NFS pathconf RPC. |
55e303ae | 7221 | */ |
91447636 | 7222 | int |
2d21ac55 A |
7223 | nfs3_pathconf_rpc( |
7224 | nfsnode_t np, | |
7225 | struct nfs_fsattr *nfsap, | |
7226 | vfs_context_t ctx) | |
55e303ae | 7227 | { |
2d21ac55 | 7228 | u_int64_t xid; |
f427ee49 | 7229 | int error = 0, lockerror, status = 0, nfsvers; |
2d21ac55 A |
7230 | struct nfsm_chain nmreq, nmrep; |
7231 | struct nfsmount *nmp = NFSTONMP(np); | |
7232 | uint32_t val = 0; | |
483a1d10 | 7233 | |
0a7de745 A |
7234 | if (nfs_mount_gone(nmp)) { |
7235 | return ENXIO; | |
7236 | } | |
2d21ac55 | 7237 | nfsvers = nmp->nm_vers; |
1c79356b | 7238 | |
2d21ac55 A |
7239 | nfsm_chain_null(&nmreq); |
7240 | nfsm_chain_null(&nmrep); | |
1c79356b | 7241 | |
91447636 | 7242 | /* fetch pathconf info from server */ |
2d21ac55 A |
7243 | nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3)); |
7244 | nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize); | |
7245 | nfsm_chain_build_done(error, &nmreq); | |
7246 | nfsmout_if(error); | |
6d2010ae | 7247 | error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx, NULL, &nmrep, &xid, &status); |
0a7de745 | 7248 | if ((lockerror = nfs_node_lock(np))) { |
2d21ac55 | 7249 | error = lockerror; |
0a7de745 | 7250 | } |
2d21ac55 | 7251 | nfsm_chain_postop_attr_update(error, &nmrep, np, &xid); |
0a7de745 | 7252 | if (!lockerror) { |
b0d623f7 | 7253 | nfs_node_unlock(np); |
0a7de745 A |
7254 | } |
7255 | if (!error) { | |
2d21ac55 | 7256 | error = status; |
0a7de745 | 7257 | } |
2d21ac55 A |
7258 | nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxlink); |
7259 | nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxname); | |
0a7de745 | 7260 | nfsap->nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC | NFS_FSFLAG_CHOWN_RESTRICTED | NFS_FSFLAG_CASE_INSENSITIVE | NFS_FSFLAG_CASE_PRESERVING); |
2d21ac55 | 7261 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7262 | if (val) { |
2d21ac55 | 7263 | nfsap->nfsa_flags |= NFS_FSFLAG_NO_TRUNC; |
0a7de745 | 7264 | } |
2d21ac55 | 7265 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7266 | if (val) { |
2d21ac55 | 7267 | nfsap->nfsa_flags |= NFS_FSFLAG_CHOWN_RESTRICTED; |
0a7de745 | 7268 | } |
2d21ac55 | 7269 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7270 | if (val) { |
2d21ac55 | 7271 | nfsap->nfsa_flags |= NFS_FSFLAG_CASE_INSENSITIVE; |
0a7de745 | 7272 | } |
2d21ac55 | 7273 | nfsm_chain_get_32(error, &nmrep, val); |
0a7de745 | 7274 | if (val) { |
2d21ac55 | 7275 | nfsap->nfsa_flags |= NFS_FSFLAG_CASE_PRESERVING; |
0a7de745 | 7276 | } |
2d21ac55 A |
7277 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK); |
7278 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME); | |
7279 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC); | |
7280 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED); | |
7281 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE); | |
7282 | NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING); | |
7283 | nfsmout: | |
7284 | nfsm_chain_cleanup(&nmreq); | |
7285 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 7286 | return error; |
1c79356b A |
7287 | } |
7288 | ||
2d21ac55 | 7289 | /* save pathconf info for NFSv3 mount */ |
91447636 | 7290 | void |
2d21ac55 | 7291 | nfs3_pathconf_cache(struct nfsmount *nmp, struct nfs_fsattr *nfsap) |
1c79356b | 7292 | { |
2d21ac55 A |
7293 | nmp->nm_fsattr.nfsa_maxlink = nfsap->nfsa_maxlink; |
7294 | nmp->nm_fsattr.nfsa_maxname = nfsap->nfsa_maxname; | |
0a7de745 | 7295 | nmp->nm_fsattr.nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC | NFS_FSFLAG_CHOWN_RESTRICTED | NFS_FSFLAG_CASE_INSENSITIVE | NFS_FSFLAG_CASE_PRESERVING); |
2d21ac55 A |
7296 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC; |
7297 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED; | |
7298 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE; | |
7299 | nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING; | |
7300 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXLINK); | |
7301 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME); | |
7302 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_NO_TRUNC); | |
7303 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED); | |
7304 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE); | |
7305 | NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING); | |
91447636 | 7306 | nmp->nm_state |= NFSSTA_GOTPATHCONF; |
1c79356b A |
7307 | } |
7308 | ||
7309 | /* | |
91447636 A |
7310 | * Return POSIX pathconf information applicable to nfs. |
7311 | * | |
7312 | * The NFS V2 protocol doesn't support this, so just return EINVAL | |
7313 | * for V2. | |
1c79356b | 7314 | */ |
91447636 | 7315 | /* ARGSUSED */ |
b0d623f7 | 7316 | int |
2d21ac55 | 7317 | nfs_vnop_pathconf( |
91447636 | 7318 | struct vnop_pathconf_args /* { |
0a7de745 A |
7319 | * struct vnodeop_desc *a_desc; |
7320 | * vnode_t a_vp; | |
7321 | * int a_name; | |
7322 | * int32_t *a_retval; | |
7323 | * vfs_context_t a_context; | |
7324 | * } */*ap) | |
1c79356b | 7325 | { |
91447636 | 7326 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7327 | nfsnode_t np = VTONFS(vp); |
91447636 | 7328 | struct nfsmount *nmp; |
2d21ac55 A |
7329 | struct nfs_fsattr nfsa, *nfsap; |
7330 | int error = 0; | |
7331 | uint64_t maxFileSize; | |
7332 | uint nbits; | |
1c79356b | 7333 | |
2d21ac55 | 7334 | nmp = VTONMP(vp); |
0a7de745 A |
7335 | if (nfs_mount_gone(nmp)) { |
7336 | return ENXIO; | |
7337 | } | |
1c79356b | 7338 | |
91447636 A |
7339 | switch (ap->a_name) { |
7340 | case _PC_LINK_MAX: | |
7341 | case _PC_NAME_MAX: | |
7342 | case _PC_CHOWN_RESTRICTED: | |
7343 | case _PC_NO_TRUNC: | |
7344 | case _PC_CASE_SENSITIVE: | |
7345 | case _PC_CASE_PRESERVING: | |
7346 | break; | |
2d21ac55 A |
7347 | case _PC_FILESIZEBITS: |
7348 | if (nmp->nm_vers == NFS_VER2) { | |
7349 | *ap->a_retval = 32; | |
0a7de745 | 7350 | return 0; |
2d21ac55 A |
7351 | } |
7352 | break; | |
6d2010ae A |
7353 | case _PC_XATTR_SIZE_BITS: |
7354 | /* Do we support xattrs natively? */ | |
0a7de745 | 7355 | if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR) { |
6d2010ae | 7356 | break; /* Yes */ |
0a7de745 | 7357 | } |
f427ee49 A |
7358 | /* No... so just return an error */ |
7359 | return EINVAL; | |
91447636 A |
7360 | default: |
7361 | /* don't bother contacting the server if we know the answer */ | |
0a7de745 | 7362 | return EINVAL; |
91447636 | 7363 | } |
1c79356b | 7364 | |
0a7de745 A |
7365 | if (nmp->nm_vers == NFS_VER2) { |
7366 | return EINVAL; | |
7367 | } | |
2d21ac55 A |
7368 | |
7369 | lck_mtx_lock(&nmp->nm_lock); | |
7370 | if (nmp->nm_vers == NFS_VER3) { | |
f427ee49 A |
7371 | if (!(nmp->nm_state & NFSSTA_GOTPATHCONF) || (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS) && nmp->nm_dnp != np)) { |
7372 | /* no pathconf info cached OR we were asked for non-root pathconf and filesystem does not support FSF_HOMOGENEOUS */ | |
2d21ac55 A |
7373 | lck_mtx_unlock(&nmp->nm_lock); |
7374 | NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap); | |
7375 | error = nfs3_pathconf_rpc(np, &nfsa, ap->a_context); | |
0a7de745 A |
7376 | if (error) { |
7377 | return error; | |
7378 | } | |
2d21ac55 | 7379 | nmp = VTONMP(vp); |
0a7de745 A |
7380 | if (nfs_mount_gone(nmp)) { |
7381 | return ENXIO; | |
7382 | } | |
2d21ac55 A |
7383 | lck_mtx_lock(&nmp->nm_lock); |
7384 | if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS) { | |
7385 | /* all files have the same pathconf info, */ | |
7386 | /* so cache a copy of the results */ | |
7387 | nfs3_pathconf_cache(nmp, &nfsa); | |
7388 | } | |
7389 | nfsap = &nfsa; | |
7390 | } else { | |
7391 | nfsap = &nmp->nm_fsattr; | |
7392 | } | |
cb323159 A |
7393 | } |
7394 | #if CONFIG_NFS4 | |
7395 | else if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) { | |
91447636 | 7396 | /* no pathconf info cached */ |
2d21ac55 A |
7397 | lck_mtx_unlock(&nmp->nm_lock); |
7398 | NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap); | |
7399 | error = nfs4_pathconf_rpc(np, &nfsa, ap->a_context); | |
0a7de745 A |
7400 | if (error) { |
7401 | return error; | |
7402 | } | |
2d21ac55 | 7403 | nmp = VTONMP(vp); |
0a7de745 A |
7404 | if (nfs_mount_gone(nmp)) { |
7405 | return ENXIO; | |
7406 | } | |
2d21ac55 A |
7407 | lck_mtx_lock(&nmp->nm_lock); |
7408 | nfsap = &nfsa; | |
cb323159 A |
7409 | } |
7410 | #endif | |
7411 | else { | |
2d21ac55 | 7412 | nfsap = &nmp->nm_fsattr; |
91447636 | 7413 | } |
91447636 A |
7414 | switch (ap->a_name) { |
7415 | case _PC_LINK_MAX: | |
0a7de745 | 7416 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK)) { |
2d21ac55 | 7417 | *ap->a_retval = nfsap->nfsa_maxlink; |
cb323159 | 7418 | #if CONFIG_NFS4 |
0a7de745 | 7419 | } else if ((nmp->nm_vers == NFS_VER4) && NFS_BITMAP_ISSET(np->n_vattr.nva_bitmap, NFS_FATTR_MAXLINK)) { |
2d21ac55 | 7420 | *ap->a_retval = np->n_vattr.nva_maxlink; |
cb323159 | 7421 | #endif |
0a7de745 | 7422 | } else { |
2d21ac55 | 7423 | error = EINVAL; |
0a7de745 | 7424 | } |
91447636 | 7425 | break; |
2d21ac55 | 7426 | case _PC_NAME_MAX: |
0a7de745 | 7427 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME)) { |
2d21ac55 | 7428 | *ap->a_retval = nfsap->nfsa_maxname; |
0a7de745 | 7429 | } else { |
2d21ac55 | 7430 | error = EINVAL; |
0a7de745 | 7431 | } |
2d21ac55 A |
7432 | break; |
7433 | case _PC_CHOWN_RESTRICTED: | |
0a7de745 | 7434 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED)) { |
b0d623f7 | 7435 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0; |
0a7de745 | 7436 | } else { |
2d21ac55 | 7437 | error = EINVAL; |
0a7de745 | 7438 | } |
2d21ac55 A |
7439 | break; |
7440 | case _PC_NO_TRUNC: | |
0a7de745 | 7441 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC)) { |
b0d623f7 | 7442 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0; |
0a7de745 | 7443 | } else { |
2d21ac55 | 7444 | error = EINVAL; |
0a7de745 | 7445 | } |
2d21ac55 A |
7446 | break; |
7447 | case _PC_CASE_SENSITIVE: | |
0a7de745 | 7448 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE)) { |
b0d623f7 | 7449 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1; |
0a7de745 | 7450 | } else { |
2d21ac55 | 7451 | error = EINVAL; |
0a7de745 | 7452 | } |
2d21ac55 A |
7453 | break; |
7454 | case _PC_CASE_PRESERVING: | |
0a7de745 | 7455 | if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING)) { |
b0d623f7 | 7456 | *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0; |
0a7de745 | 7457 | } else { |
2d21ac55 | 7458 | error = EINVAL; |
0a7de745 | 7459 | } |
2d21ac55 | 7460 | break; |
6d2010ae | 7461 | case _PC_XATTR_SIZE_BITS: /* same as file size bits if named attrs supported */ |
2d21ac55 | 7462 | case _PC_FILESIZEBITS: |
b0d623f7 | 7463 | if (!NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) { |
2d21ac55 A |
7464 | *ap->a_retval = 64; |
7465 | error = 0; | |
7466 | break; | |
1c79356b | 7467 | } |
b0d623f7 | 7468 | maxFileSize = nfsap->nfsa_maxfilesize; |
2d21ac55 A |
7469 | nbits = 1; |
7470 | if (maxFileSize & 0xffffffff00000000ULL) { | |
7471 | nbits += 32; | |
7472 | maxFileSize >>= 32; | |
91447636 | 7473 | } |
2d21ac55 A |
7474 | if (maxFileSize & 0xffff0000) { |
7475 | nbits += 16; | |
7476 | maxFileSize >>= 16; | |
7477 | } | |
7478 | if (maxFileSize & 0xff00) { | |
7479 | nbits += 8; | |
7480 | maxFileSize >>= 8; | |
7481 | } | |
7482 | if (maxFileSize & 0xf0) { | |
7483 | nbits += 4; | |
7484 | maxFileSize >>= 4; | |
91447636 | 7485 | } |
2d21ac55 A |
7486 | if (maxFileSize & 0xc) { |
7487 | nbits += 2; | |
7488 | maxFileSize >>= 2; | |
7489 | } | |
7490 | if (maxFileSize & 0x2) { | |
7491 | nbits += 1; | |
7492 | } | |
7493 | *ap->a_retval = nbits; | |
7494 | break; | |
7495 | default: | |
7496 | error = EINVAL; | |
7497 | } | |
1c79356b | 7498 | |
2d21ac55 A |
7499 | lck_mtx_unlock(&nmp->nm_lock); |
7500 | ||
0a7de745 | 7501 | return error; |
1c79356b A |
7502 | } |
7503 | ||
1c79356b A |
7504 | /* |
7505 | * Read wrapper for special devices. | |
7506 | */ | |
b0d623f7 | 7507 | int |
2d21ac55 | 7508 | nfsspec_vnop_read( |
91447636 | 7509 | struct vnop_read_args /* { |
0a7de745 A |
7510 | * struct vnodeop_desc *a_desc; |
7511 | * vnode_t a_vp; | |
7512 | * struct uio *a_uio; | |
7513 | * int a_ioflag; | |
7514 | * vfs_context_t a_context; | |
7515 | * } */*ap) | |
1c79356b | 7516 | { |
2d21ac55 | 7517 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7518 | struct timespec now; |
2d21ac55 | 7519 | int error; |
1c79356b A |
7520 | |
7521 | /* | |
7522 | * Set access flag. | |
7523 | */ | |
0a7de745 A |
7524 | if ((error = nfs_node_lock(np))) { |
7525 | return error; | |
7526 | } | |
1c79356b | 7527 | np->n_flag |= NACC; |
5ba3f43e | 7528 | nanotime(&now); |
55e303ae | 7529 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 7530 | np->n_atim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7531 | nfs_node_unlock(np); |
0a7de745 | 7532 | return VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap); |
1c79356b A |
7533 | } |
7534 | ||
7535 | /* | |
7536 | * Write wrapper for special devices. | |
7537 | */ | |
b0d623f7 | 7538 | int |
2d21ac55 | 7539 | nfsspec_vnop_write( |
91447636 | 7540 | struct vnop_write_args /* { |
0a7de745 A |
7541 | * struct vnodeop_desc *a_desc; |
7542 | * vnode_t a_vp; | |
7543 | * struct uio *a_uio; | |
7544 | * int a_ioflag; | |
7545 | * vfs_context_t a_context; | |
7546 | * } */*ap) | |
1c79356b | 7547 | { |
2d21ac55 | 7548 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7549 | struct timespec now; |
2d21ac55 | 7550 | int error; |
1c79356b A |
7551 | |
7552 | /* | |
7553 | * Set update flag. | |
7554 | */ | |
0a7de745 A |
7555 | if ((error = nfs_node_lock(np))) { |
7556 | return error; | |
7557 | } | |
1c79356b | 7558 | np->n_flag |= NUPD; |
5ba3f43e | 7559 | nanotime(&now); |
55e303ae | 7560 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 7561 | np->n_mtim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7562 | nfs_node_unlock(np); |
0a7de745 | 7563 | return VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap); |
1c79356b A |
7564 | } |
7565 | ||
7566 | /* | |
7567 | * Close wrapper for special devices. | |
7568 | * | |
7569 | * Update the times on the nfsnode then do device close. | |
7570 | */ | |
b0d623f7 | 7571 | int |
2d21ac55 | 7572 | nfsspec_vnop_close( |
91447636 | 7573 | struct vnop_close_args /* { |
0a7de745 A |
7574 | * struct vnodeop_desc *a_desc; |
7575 | * vnode_t a_vp; | |
7576 | * int a_fflag; | |
7577 | * vfs_context_t a_context; | |
7578 | * } */*ap) | |
1c79356b | 7579 | { |
91447636 | 7580 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7581 | nfsnode_t np = VTONFS(vp); |
91447636 A |
7582 | struct vnode_attr vattr; |
7583 | mount_t mp; | |
2d21ac55 | 7584 | int error; |
1c79356b | 7585 | |
0a7de745 A |
7586 | if ((error = nfs_node_lock(np))) { |
7587 | return error; | |
7588 | } | |
1c79356b A |
7589 | if (np->n_flag & (NACC | NUPD)) { |
7590 | np->n_flag |= NCHG; | |
b0d623f7 | 7591 | if (!vnode_isinuse(vp, 0) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) { |
91447636 A |
7592 | VATTR_INIT(&vattr); |
7593 | if (np->n_flag & NACC) { | |
7594 | vattr.va_access_time = np->n_atim; | |
7595 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
7596 | } | |
7597 | if (np->n_flag & NUPD) { | |
7598 | vattr.va_modify_time = np->n_mtim; | |
7599 | VATTR_SET_ACTIVE(&vattr, va_modify_time); | |
7600 | } | |
b0d623f7 | 7601 | nfs_node_unlock(np); |
91447636 | 7602 | vnode_setattr(vp, &vattr, ap->a_context); |
2d21ac55 | 7603 | } else { |
b0d623f7 | 7604 | nfs_node_unlock(np); |
1c79356b | 7605 | } |
2d21ac55 | 7606 | } else { |
b0d623f7 | 7607 | nfs_node_unlock(np); |
1c79356b | 7608 | } |
0a7de745 | 7609 | return VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap); |
1c79356b A |
7610 | } |
7611 | ||
2d21ac55 | 7612 | #if FIFO |
91447636 A |
7613 | extern vnop_t **fifo_vnodeop_p; |
7614 | ||
1c79356b A |
7615 | /* |
7616 | * Read wrapper for fifos. | |
7617 | */ | |
b0d623f7 | 7618 | int |
2d21ac55 | 7619 | nfsfifo_vnop_read( |
91447636 | 7620 | struct vnop_read_args /* { |
0a7de745 A |
7621 | * struct vnodeop_desc *a_desc; |
7622 | * vnode_t a_vp; | |
7623 | * struct uio *a_uio; | |
7624 | * int a_ioflag; | |
7625 | * vfs_context_t a_context; | |
7626 | * } */*ap) | |
1c79356b | 7627 | { |
2d21ac55 | 7628 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7629 | struct timespec now; |
2d21ac55 | 7630 | int error; |
1c79356b A |
7631 | |
7632 | /* | |
7633 | * Set access flag. | |
7634 | */ | |
0a7de745 A |
7635 | if ((error = nfs_node_lock(np))) { |
7636 | return error; | |
7637 | } | |
1c79356b | 7638 | np->n_flag |= NACC; |
5ba3f43e | 7639 | nanotime(&now); |
55e303ae | 7640 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 7641 | np->n_atim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7642 | nfs_node_unlock(np); |
0a7de745 | 7643 | return VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap); |
1c79356b A |
7644 | } |
7645 | ||
7646 | /* | |
7647 | * Write wrapper for fifos. | |
7648 | */ | |
b0d623f7 | 7649 | int |
2d21ac55 | 7650 | nfsfifo_vnop_write( |
91447636 | 7651 | struct vnop_write_args /* { |
0a7de745 A |
7652 | * struct vnodeop_desc *a_desc; |
7653 | * vnode_t a_vp; | |
7654 | * struct uio *a_uio; | |
7655 | * int a_ioflag; | |
7656 | * vfs_context_t a_context; | |
7657 | * } */*ap) | |
1c79356b | 7658 | { |
2d21ac55 | 7659 | nfsnode_t np = VTONFS(ap->a_vp); |
5ba3f43e | 7660 | struct timespec now; |
2d21ac55 | 7661 | int error; |
1c79356b A |
7662 | |
7663 | /* | |
7664 | * Set update flag. | |
7665 | */ | |
0a7de745 A |
7666 | if ((error = nfs_node_lock(np))) { |
7667 | return error; | |
7668 | } | |
1c79356b | 7669 | np->n_flag |= NUPD; |
5ba3f43e | 7670 | nanotime(&now); |
55e303ae | 7671 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 7672 | np->n_mtim.tv_nsec = now.tv_nsec; |
b0d623f7 | 7673 | nfs_node_unlock(np); |
0a7de745 | 7674 | return VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap); |
1c79356b A |
7675 | } |
7676 | ||
7677 | /* | |
7678 | * Close wrapper for fifos. | |
7679 | * | |
7680 | * Update the times on the nfsnode then do fifo close. | |
7681 | */ | |
b0d623f7 | 7682 | int |
2d21ac55 | 7683 | nfsfifo_vnop_close( |
91447636 | 7684 | struct vnop_close_args /* { |
0a7de745 A |
7685 | * struct vnodeop_desc *a_desc; |
7686 | * vnode_t a_vp; | |
7687 | * int a_fflag; | |
7688 | * vfs_context_t a_context; | |
7689 | * } */*ap) | |
1c79356b | 7690 | { |
91447636 | 7691 | vnode_t vp = ap->a_vp; |
2d21ac55 | 7692 | nfsnode_t np = VTONFS(vp); |
91447636 | 7693 | struct vnode_attr vattr; |
5ba3f43e | 7694 | struct timespec now; |
91447636 | 7695 | mount_t mp; |
2d21ac55 | 7696 | int error; |
1c79356b | 7697 | |
0a7de745 A |
7698 | if ((error = nfs_node_lock(np))) { |
7699 | return error; | |
7700 | } | |
1c79356b | 7701 | if (np->n_flag & (NACC | NUPD)) { |
5ba3f43e | 7702 | nanotime(&now); |
1c79356b | 7703 | if (np->n_flag & NACC) { |
55e303ae | 7704 | np->n_atim.tv_sec = now.tv_sec; |
5ba3f43e | 7705 | np->n_atim.tv_nsec = now.tv_nsec; |
1c79356b A |
7706 | } |
7707 | if (np->n_flag & NUPD) { | |
55e303ae | 7708 | np->n_mtim.tv_sec = now.tv_sec; |
5ba3f43e | 7709 | np->n_mtim.tv_nsec = now.tv_nsec; |
1c79356b A |
7710 | } |
7711 | np->n_flag |= NCHG; | |
91447636 A |
7712 | if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) { |
7713 | VATTR_INIT(&vattr); | |
7714 | if (np->n_flag & NACC) { | |
7715 | vattr.va_access_time = np->n_atim; | |
7716 | VATTR_SET_ACTIVE(&vattr, va_access_time); | |
7717 | } | |
7718 | if (np->n_flag & NUPD) { | |
7719 | vattr.va_modify_time = np->n_mtim; | |
7720 | VATTR_SET_ACTIVE(&vattr, va_modify_time); | |
7721 | } | |
b0d623f7 | 7722 | nfs_node_unlock(np); |
91447636 | 7723 | vnode_setattr(vp, &vattr, ap->a_context); |
2d21ac55 | 7724 | } else { |
b0d623f7 | 7725 | nfs_node_unlock(np); |
1c79356b | 7726 | } |
2d21ac55 | 7727 | } else { |
b0d623f7 | 7728 | nfs_node_unlock(np); |
1c79356b | 7729 | } |
0a7de745 | 7730 | return VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap); |
1c79356b | 7731 | } |
2d21ac55 | 7732 | #endif /* FIFO */ |
1c79356b | 7733 | |
91447636 | 7734 | /*ARGSUSED*/ |
b0d623f7 | 7735 | int |
2d21ac55 | 7736 | nfs_vnop_ioctl( |
6d2010ae | 7737 | struct vnop_ioctl_args /* { |
0a7de745 A |
7738 | * struct vnodeop_desc *a_desc; |
7739 | * vnode_t a_vp; | |
7740 | * u_int32_t a_command; | |
7741 | * caddr_t a_data; | |
7742 | * int a_fflag; | |
7743 | * vfs_context_t a_context; | |
7744 | * } */*ap) | |
1c79356b | 7745 | { |
6d2010ae A |
7746 | vfs_context_t ctx = ap->a_context; |
7747 | vnode_t vp = ap->a_vp; | |
39236c6e | 7748 | struct nfsmount *mp = VTONMP(vp); |
cb323159 A |
7749 | int error = ENOTTY; |
7750 | #if CONFIG_NFS_GSS | |
d9a64523 | 7751 | struct user_nfs_gss_principal gprinc = {}; |
f427ee49 | 7752 | size_t len; |
cb323159 | 7753 | #endif |
1c79356b | 7754 | |
0a7de745 A |
7755 | if (mp == NULL) { |
7756 | return ENXIO; | |
7757 | } | |
0a7de745 | 7758 | switch (ap->a_command) { |
6d2010ae | 7759 | case F_FULLFSYNC: |
0a7de745 A |
7760 | if (vnode_vfsisrdonly(vp)) { |
7761 | return EROFS; | |
7762 | } | |
6d2010ae A |
7763 | error = nfs_flush(VTONFS(vp), MNT_WAIT, vfs_context_thread(ctx), 0); |
7764 | break; | |
cb323159 | 7765 | #if CONFIG_NFS_GSS |
5ba3f43e | 7766 | case NFS_IOC_DESTROY_CRED: |
0a7de745 A |
7767 | if (!auth_is_kerberized(mp->nm_auth)) { |
7768 | return ENOTSUP; | |
7769 | } | |
c3c9b80d A |
7770 | if ((error = nfs_gss_clnt_ctx_remove(mp, vfs_context_ucred(ctx))) == ENOENT) { |
7771 | error = 0; | |
7772 | } | |
39236c6e | 7773 | break; |
5ba3f43e A |
7774 | case NFS_IOC_SET_CRED: |
7775 | case NFS_IOC_SET_CRED64: | |
0a7de745 A |
7776 | if (!auth_is_kerberized(mp->nm_auth)) { |
7777 | return ENOTSUP; | |
7778 | } | |
5ba3f43e | 7779 | if ((ap->a_command == NFS_IOC_SET_CRED && vfs_context_is64bit(ctx)) || |
0a7de745 A |
7780 | (ap->a_command == NFS_IOC_SET_CRED64 && !vfs_context_is64bit(ctx))) { |
7781 | return EINVAL; | |
7782 | } | |
3e170ce0 A |
7783 | if (vfs_context_is64bit(ctx)) { |
7784 | gprinc = *(struct user_nfs_gss_principal *)ap->a_data; | |
7785 | } else { | |
7786 | struct nfs_gss_principal *tp; | |
7787 | tp = (struct nfs_gss_principal *)ap->a_data; | |
7788 | gprinc.princlen = tp->princlen; | |
7789 | gprinc.nametype = tp->nametype; | |
7790 | gprinc.principal = CAST_USER_ADDR_T(tp->principal); | |
7791 | } | |
f427ee49 | 7792 | NFS_DBG(NFS_FAC_GSS, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %zu name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx), gprinc.princlen, gprinc.nametype, gprinc.principal); |
0a7de745 A |
7793 | if (gprinc.princlen > MAXPATHLEN) { |
7794 | return EINVAL; | |
7795 | } | |
3e170ce0 | 7796 | uint8_t *p; |
0a7de745 A |
7797 | MALLOC(p, uint8_t *, gprinc.princlen + 1, M_TEMP, M_WAITOK | M_ZERO); |
7798 | if (p == NULL) { | |
7799 | return ENOMEM; | |
7800 | } | |
f427ee49 A |
7801 | assert((user_addr_t)gprinc.principal == gprinc.principal); |
7802 | error = copyin((user_addr_t)gprinc.principal, p, gprinc.princlen); | |
3e170ce0 | 7803 | if (error) { |
f427ee49 | 7804 | NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %zu: %d\n", |
0a7de745 | 7805 | gprinc.princlen, error); |
3e170ce0 | 7806 | FREE(p, M_TEMP); |
0a7de745 | 7807 | return error; |
3e170ce0 A |
7808 | } |
7809 | NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s\n", p); | |
7810 | error = nfs_gss_clnt_ctx_set_principal(mp, ctx, p, gprinc.princlen, gprinc.nametype); | |
7811 | NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s returned %d\n", p, error); | |
7812 | FREE(p, M_TEMP); | |
7813 | break; | |
5ba3f43e A |
7814 | case NFS_IOC_GET_CRED: |
7815 | case NFS_IOC_GET_CRED64: | |
0a7de745 A |
7816 | if (!auth_is_kerberized(mp->nm_auth)) { |
7817 | return ENOTSUP; | |
7818 | } | |
5ba3f43e | 7819 | if ((ap->a_command == NFS_IOC_GET_CRED && vfs_context_is64bit(ctx)) || |
0a7de745 A |
7820 | (ap->a_command == NFS_IOC_GET_CRED64 && !vfs_context_is64bit(ctx))) { |
7821 | return EINVAL; | |
7822 | } | |
3e170ce0 | 7823 | error = nfs_gss_clnt_ctx_get_principal(mp, ctx, &gprinc); |
0a7de745 | 7824 | if (error) { |
3e170ce0 | 7825 | break; |
0a7de745 | 7826 | } |
3e170ce0 A |
7827 | if (vfs_context_is64bit(ctx)) { |
7828 | struct user_nfs_gss_principal *upp = (struct user_nfs_gss_principal *)ap->a_data; | |
7829 | len = upp->princlen; | |
0a7de745 | 7830 | if (gprinc.princlen < len) { |
3e170ce0 | 7831 | len = gprinc.princlen; |
0a7de745 | 7832 | } |
3e170ce0 A |
7833 | upp->princlen = gprinc.princlen; |
7834 | upp->nametype = gprinc.nametype; | |
7835 | upp->flags = gprinc.flags; | |
0a7de745 | 7836 | if (gprinc.principal) { |
f427ee49 A |
7837 | assert((user_addr_t)upp->principal == upp->principal); |
7838 | error = copyout((void *)gprinc.principal, (user_addr_t)upp->principal, len); | |
0a7de745 | 7839 | } else { |
3e170ce0 | 7840 | upp->principal = USER_ADDR_NULL; |
0a7de745 | 7841 | } |
3e170ce0 A |
7842 | } else { |
7843 | struct nfs_gss_principal *u32pp = (struct nfs_gss_principal *)ap->a_data; | |
7844 | len = u32pp->princlen; | |
0a7de745 | 7845 | if (gprinc.princlen < len) { |
3e170ce0 | 7846 | len = gprinc.princlen; |
0a7de745 | 7847 | } |
3e170ce0 A |
7848 | u32pp->princlen = gprinc.princlen; |
7849 | u32pp->nametype = gprinc.nametype; | |
7850 | u32pp->flags = gprinc.flags; | |
0a7de745 | 7851 | if (gprinc.principal) { |
3e170ce0 | 7852 | error = copyout((void *)gprinc.principal, u32pp->principal, len); |
0a7de745 | 7853 | } else { |
3e170ce0 | 7854 | u32pp->principal = (user32_addr_t)0; |
0a7de745 | 7855 | } |
3e170ce0 A |
7856 | } |
7857 | if (error) { | |
f427ee49 | 7858 | NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %zu: %d\n", |
0a7de745 | 7859 | gprinc.princlen, error); |
3e170ce0 | 7860 | } |
0a7de745 | 7861 | if (gprinc.principal) { |
f427ee49 A |
7862 | void *ptr = (void *)gprinc.principal; |
7863 | gprinc.principal = 0; | |
7864 | FREE(ptr, M_TEMP); | |
0a7de745 | 7865 | } |
cb323159 | 7866 | #endif /* CONFIG_NFS_GSS */ |
6d2010ae A |
7867 | } |
7868 | ||
0a7de745 | 7869 | return error; |
1c79356b A |
7870 | } |
7871 | ||
91447636 | 7872 | /*ARGSUSED*/ |
b0d623f7 | 7873 | int |
2d21ac55 | 7874 | nfs_vnop_select( |
91447636 | 7875 | __unused struct vnop_select_args /* { |
0a7de745 A |
7876 | * struct vnodeop_desc *a_desc; |
7877 | * vnode_t a_vp; | |
7878 | * int a_which; | |
7879 | * int a_fflags; | |
7880 | * void *a_wql; | |
7881 | * vfs_context_t a_context; | |
7882 | * } */*ap) | |
1c79356b | 7883 | { |
1c79356b A |
7884 | /* |
7885 | * We were once bogusly seltrue() which returns 1. Is this right? | |
7886 | */ | |
0a7de745 | 7887 | return 1; |
1c79356b A |
7888 | } |
7889 | ||
1c79356b | 7890 | /* |
2d21ac55 A |
7891 | * vnode OP for pagein using UPL |
7892 | * | |
7893 | * No buffer I/O, just RPCs straight into the mapped pages. | |
1c79356b | 7894 | */ |
b0d623f7 | 7895 | int |
2d21ac55 | 7896 | nfs_vnop_pagein( |
91447636 | 7897 | struct vnop_pagein_args /* { |
0a7de745 A |
7898 | * struct vnodeop_desc *a_desc; |
7899 | * vnode_t a_vp; | |
7900 | * upl_t a_pl; | |
7901 | * vm_offset_t a_pl_offset; | |
7902 | * off_t a_f_offset; | |
7903 | * size_t a_size; | |
7904 | * int a_flags; | |
7905 | * vfs_context_t a_context; | |
7906 | * } */*ap) | |
1c79356b | 7907 | { |
91447636 | 7908 | vnode_t vp = ap->a_vp; |
1c79356b | 7909 | upl_t pl = ap->a_pl; |
f427ee49 | 7910 | upl_size_t size = (upl_size_t)ap->a_size; |
1c79356b | 7911 | off_t f_offset = ap->a_f_offset; |
f427ee49 | 7912 | upl_offset_t pl_offset = ap->a_pl_offset; |
2d21ac55 A |
7913 | int flags = ap->a_flags; |
7914 | thread_t thd; | |
91447636 | 7915 | kauth_cred_t cred; |
2d21ac55 A |
7916 | nfsnode_t np = VTONFS(vp); |
7917 | size_t nmrsize, iosize, txsize, rxsize, retsize; | |
7918 | off_t txoffset; | |
55e303ae | 7919 | struct nfsmount *nmp; |
1c79356b | 7920 | int error = 0; |
b0d623f7 A |
7921 | vm_offset_t ioaddr, rxaddr; |
7922 | uio_t uio; | |
0a7de745 | 7923 | char uio_buf[UIO_SIZEOF(1)]; |
fa4905b1 | 7924 | int nofreeupl = flags & UPL_NOCOMMIT; |
55e303ae | 7925 | upl_page_info_t *plinfo; |
0a7de745 | 7926 | #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */ |
2d21ac55 A |
7927 | struct nfsreq *req[MAXPAGINGREQS]; |
7928 | int nextsend, nextwait; | |
cb323159 A |
7929 | #if CONFIG_NFS4 |
7930 | uint32_t stategenid = 0; | |
7931 | #endif | |
7932 | uint32_t restart = 0; | |
b0d623f7 | 7933 | kern_return_t kret; |
1c79356b | 7934 | |
2d21ac55 | 7935 | FSDBG(322, np, f_offset, size, flags); |
0a7de745 | 7936 | if (pl == (upl_t)NULL) { |
fa4905b1 | 7937 | panic("nfs_pagein: no upl"); |
0a7de745 | 7938 | } |
1c79356b | 7939 | |
fa4905b1 | 7940 | if (size <= 0) { |
f427ee49 | 7941 | printf("nfs_pagein: invalid size %u", size); |
0a7de745 | 7942 | if (!nofreeupl) { |
316670eb | 7943 | (void) ubc_upl_abort_range(pl, pl_offset, size, 0); |
0a7de745 A |
7944 | } |
7945 | return EINVAL; | |
fa4905b1 | 7946 | } |
91447636 | 7947 | if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) { |
0a7de745 | 7948 | if (!nofreeupl) { |
2d21ac55 | 7949 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
7950 | UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); |
7951 | } | |
7952 | return EINVAL; | |
1c79356b | 7953 | } |
91447636 | 7954 | |
2d21ac55 | 7955 | thd = vfs_context_thread(ap->a_context); |
fa4905b1 | 7956 | cred = ubc_getcred(vp); |
0a7de745 | 7957 | if (!IS_VALID_CRED(cred)) { |
91447636 | 7958 | cred = vfs_context_ucred(ap->a_context); |
0a7de745 | 7959 | } |
1c79356b | 7960 | |
b0d623f7 | 7961 | uio = uio_createwithbuffer(1, f_offset, UIO_SYSSPACE, UIO_READ, |
0a7de745 | 7962 | &uio_buf, sizeof(uio_buf)); |
1c79356b | 7963 | |
2d21ac55 | 7964 | nmp = VTONMP(vp); |
fe8ab488 | 7965 | if (nfs_mount_gone(nmp)) { |
0a7de745 | 7966 | if (!nofreeupl) { |
2d21ac55 | 7967 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
7968 | UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); |
7969 | } | |
7970 | return ENXIO; | |
55e303ae | 7971 | } |
2d21ac55 | 7972 | nmrsize = nmp->nm_rsize; |
1c79356b | 7973 | |
55e303ae | 7974 | plinfo = ubc_upl_pageinfo(pl); |
b0d623f7 | 7975 | kret = ubc_upl_map(pl, &ioaddr); |
0a7de745 | 7976 | if (kret != KERN_SUCCESS) { |
b0d623f7 | 7977 | panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret); |
0a7de745 | 7978 | } |
1c79356b | 7979 | ioaddr += pl_offset; |
b0d623f7 A |
7980 | |
7981 | tryagain: | |
cb323159 | 7982 | #if CONFIG_NFS4 |
0a7de745 | 7983 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 7984 | stategenid = nmp->nm_stategenid; |
0a7de745 | 7985 | } |
cb323159 | 7986 | #endif |
2d21ac55 A |
7987 | txsize = rxsize = size; |
7988 | txoffset = f_offset; | |
b0d623f7 | 7989 | rxaddr = ioaddr; |
1c79356b | 7990 | |
2d21ac55 A |
7991 | bzero(req, sizeof(req)); |
7992 | nextsend = nextwait = 0; | |
1c79356b | 7993 | do { |
6d2010ae A |
7994 | if (np->n_flag & NREVOKE) { |
7995 | error = EIO; | |
7996 | break; | |
7997 | } | |
2d21ac55 A |
7998 | /* send requests while we need to and have available slots */ |
7999 | while ((txsize > 0) && (req[nextsend] == NULL)) { | |
8000 | iosize = MIN(nmrsize, txsize); | |
8001 | if ((error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, iosize, thd, cred, NULL, &req[nextsend]))) { | |
8002 | req[nextsend] = NULL; | |
8003 | break; | |
8004 | } | |
8005 | txoffset += iosize; | |
8006 | txsize -= iosize; | |
8007 | nextsend = (nextsend + 1) % MAXPAGINGREQS; | |
8008 | } | |
8009 | /* wait while we need to and break out if more requests to send */ | |
8010 | while ((rxsize > 0) && req[nextwait]) { | |
8011 | iosize = retsize = MIN(nmrsize, rxsize); | |
b0d623f7 A |
8012 | uio_reset(uio, uio_offset(uio), UIO_SYSSPACE, UIO_READ); |
8013 | uio_addiov(uio, CAST_USER_ADDR_T(rxaddr), iosize); | |
8014 | FSDBG(322, uio_offset(uio), uio_resid(uio), rxaddr, rxsize); | |
8015 | #if UPL_DEBUG | |
8016 | upl_ubc_alias_set(pl, (uintptr_t) current_thread(), (uintptr_t) 2); | |
91447636 | 8017 | #endif /* UPL_DEBUG */ |
316670eb | 8018 | OSAddAtomic64(1, &nfsstats.pageins); |
2d21ac55 A |
8019 | error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req[nextwait], uio, &retsize, NULL); |
8020 | req[nextwait] = NULL; | |
8021 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
cb323159 | 8022 | #if CONFIG_NFS4 |
b0d623f7 A |
8023 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
8024 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
8025 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
8026 | NP(np, "nfs_vnop_pagein: error %d, initiating recovery", error); | |
8027 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
8028 | } |
8029 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
8030 | restart++; |
8031 | goto cancel; | |
8032 | } | |
cb323159 | 8033 | #endif |
2d21ac55 | 8034 | if (error) { |
b0d623f7 | 8035 | FSDBG(322, uio_offset(uio), uio_resid(uio), error, -1); |
2d21ac55 A |
8036 | break; |
8037 | } | |
8038 | if (retsize < iosize) { | |
8039 | /* Just zero fill the rest of the valid area. */ | |
f427ee49 | 8040 | size_t zcnt = iosize - retsize; |
b0d623f7 A |
8041 | bzero((char *)rxaddr + retsize, zcnt); |
8042 | FSDBG(324, uio_offset(uio), retsize, zcnt, rxaddr); | |
8043 | uio_update(uio, zcnt); | |
1c79356b | 8044 | } |
0a7de745 | 8045 | rxaddr += iosize; |
2d21ac55 | 8046 | rxsize -= iosize; |
0a7de745 | 8047 | if (txsize) { |
2d21ac55 | 8048 | break; |
0a7de745 | 8049 | } |
1c79356b | 8050 | } |
2d21ac55 | 8051 | } while (!error && (txsize || rxsize)); |
1c79356b | 8052 | |
b0d623f7 | 8053 | restart = 0; |
1c79356b | 8054 | |
2d21ac55 | 8055 | if (error) { |
cb323159 | 8056 | #if CONFIG_NFS4 |
b0d623f7 | 8057 | cancel: |
cb323159 | 8058 | #endif |
2d21ac55 A |
8059 | /* cancel any outstanding requests */ |
8060 | while (req[nextwait]) { | |
8061 | nfs_request_async_cancel(req[nextwait]); | |
8062 | req[nextwait] = NULL; | |
8063 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
8064 | } | |
6d2010ae A |
8065 | if (np->n_flag & NREVOKE) { |
8066 | error = EIO; | |
8067 | } else if (restart) { | |
8068 | if (restart <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */ | |
0a7de745 A |
8069 | if (error == NFSERR_GRACE) { |
8070 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
8071 | } | |
8072 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 8073 | goto tryagain; |
0a7de745 | 8074 | } |
6d2010ae A |
8075 | } else { |
8076 | NP(np, "nfs_pagein: too many restarts, aborting"); | |
8077 | } | |
b0d623f7 | 8078 | } |
2d21ac55 A |
8079 | } |
8080 | ||
b0d623f7 A |
8081 | ubc_upl_unmap(pl); |
8082 | ||
fa4905b1 | 8083 | if (!nofreeupl) { |
0a7de745 | 8084 | if (error) { |
f427ee49 A |
8085 | /* |
8086 | * See comment in vnode_pagein() on handling EAGAIN, even though UPL_NOCOMMIT flag | |
8087 | * is not set, we will not abort this upl, since VM subsystem will handle it. | |
8088 | */ | |
8089 | if (error != EAGAIN && error != EPERM) { | |
8090 | ubc_upl_abort_range(pl, pl_offset, size, | |
8091 | UPL_ABORT_ERROR | | |
8092 | UPL_ABORT_FREE_ON_EMPTY); | |
8093 | } | |
0a7de745 | 8094 | } else { |
0b4e3aa0 | 8095 | ubc_upl_commit_range(pl, pl_offset, size, |
0a7de745 A |
8096 | UPL_COMMIT_CLEAR_DIRTY | |
8097 | UPL_COMMIT_FREE_ON_EMPTY); | |
8098 | } | |
1c79356b | 8099 | } |
0a7de745 | 8100 | return error; |
1c79356b A |
8101 | } |
8102 | ||
0b4e3aa0 | 8103 | |
1c79356b | 8104 | /* |
2d21ac55 A |
8105 | * the following are needed only by nfs_pageout to know how to handle errors |
8106 | * see nfs_pageout comments on explanation of actions. | |
8107 | * the errors here are copied from errno.h and errors returned by servers | |
8108 | * are expected to match the same numbers here. If not, our actions maybe | |
8109 | * erroneous. | |
8110 | */ | |
b0d623f7 | 8111 | char nfs_pageouterrorhandler(int); |
39236c6e | 8112 | enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, SEVER}; |
2d21ac55 | 8113 | #define NFS_ELAST 88 |
0a7de745 A |
8114 | static u_char errorcount[NFS_ELAST + 1]; /* better be zeros when initialized */ |
8115 | static const char errortooutcome[NFS_ELAST + 1] = { | |
2d21ac55 | 8116 | NOACTION, |
0a7de745 A |
8117 | DUMP, /* EPERM 1 Operation not permitted */ |
8118 | DUMP, /* ENOENT 2 No such file or directory */ | |
8119 | DUMPANDLOG, /* ESRCH 3 No such process */ | |
8120 | RETRY, /* EINTR 4 Interrupted system call */ | |
8121 | DUMP, /* EIO 5 Input/output error */ | |
8122 | DUMP, /* ENXIO 6 Device not configured */ | |
8123 | DUMPANDLOG, /* E2BIG 7 Argument list too long */ | |
8124 | DUMPANDLOG, /* ENOEXEC 8 Exec format error */ | |
8125 | DUMPANDLOG, /* EBADF 9 Bad file descriptor */ | |
8126 | DUMPANDLOG, /* ECHILD 10 No child processes */ | |
8127 | DUMPANDLOG, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */ | |
8128 | RETRY, /* ENOMEM 12 Cannot allocate memory */ | |
8129 | DUMP, /* EACCES 13 Permission denied */ | |
8130 | DUMPANDLOG, /* EFAULT 14 Bad address */ | |
8131 | DUMPANDLOG, /* ENOTBLK 15 POSIX - Block device required */ | |
8132 | RETRY, /* EBUSY 16 Device busy */ | |
8133 | DUMP, /* EEXIST 17 File exists */ | |
8134 | DUMP, /* EXDEV 18 Cross-device link */ | |
8135 | DUMP, /* ENODEV 19 Operation not supported by device */ | |
8136 | DUMP, /* ENOTDIR 20 Not a directory */ | |
8137 | DUMP, /* EISDIR 21 Is a directory */ | |
8138 | DUMP, /* EINVAL 22 Invalid argument */ | |
8139 | DUMPANDLOG, /* ENFILE 23 Too many open files in system */ | |
8140 | DUMPANDLOG, /* EMFILE 24 Too many open files */ | |
8141 | DUMPANDLOG, /* ENOTTY 25 Inappropriate ioctl for device */ | |
8142 | DUMPANDLOG, /* ETXTBSY 26 Text file busy - POSIX */ | |
8143 | DUMP, /* EFBIG 27 File too large */ | |
8144 | DUMP, /* ENOSPC 28 No space left on device */ | |
8145 | DUMPANDLOG, /* ESPIPE 29 Illegal seek */ | |
8146 | DUMP, /* EROFS 30 Read-only file system */ | |
8147 | DUMP, /* EMLINK 31 Too many links */ | |
8148 | RETRY, /* EPIPE 32 Broken pipe */ | |
2d21ac55 | 8149 | /* math software */ |
0a7de745 A |
8150 | DUMPANDLOG, /* EDOM 33 Numerical argument out of domain */ |
8151 | DUMPANDLOG, /* ERANGE 34 Result too large */ | |
8152 | RETRY, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */ | |
8153 | DUMPANDLOG, /* EINPROGRESS 36 Operation now in progress */ | |
8154 | DUMPANDLOG, /* EALREADY 37 Operation already in progress */ | |
2d21ac55 | 8155 | /* ipc/network software -- argument errors */ |
0a7de745 A |
8156 | DUMPANDLOG, /* ENOTSOC 38 Socket operation on non-socket */ |
8157 | DUMPANDLOG, /* EDESTADDRREQ 39 Destination address required */ | |
8158 | DUMPANDLOG, /* EMSGSIZE 40 Message too long */ | |
8159 | DUMPANDLOG, /* EPROTOTYPE 41 Protocol wrong type for socket */ | |
8160 | DUMPANDLOG, /* ENOPROTOOPT 42 Protocol not available */ | |
8161 | DUMPANDLOG, /* EPROTONOSUPPORT 43 Protocol not supported */ | |
8162 | DUMPANDLOG, /* ESOCKTNOSUPPORT 44 Socket type not supported */ | |
8163 | DUMPANDLOG, /* ENOTSUP 45 Operation not supported */ | |
8164 | DUMPANDLOG, /* EPFNOSUPPORT 46 Protocol family not supported */ | |
8165 | DUMPANDLOG, /* EAFNOSUPPORT 47 Address family not supported by protocol family */ | |
8166 | DUMPANDLOG, /* EADDRINUSE 48 Address already in use */ | |
8167 | DUMPANDLOG, /* EADDRNOTAVAIL 49 Can't assign requested address */ | |
2d21ac55 | 8168 | /* ipc/network software -- operational errors */ |
0a7de745 A |
8169 | RETRY, /* ENETDOWN 50 Network is down */ |
8170 | RETRY, /* ENETUNREACH 51 Network is unreachable */ | |
8171 | RETRY, /* ENETRESET 52 Network dropped connection on reset */ | |
8172 | RETRY, /* ECONNABORTED 53 Software caused connection abort */ | |
8173 | RETRY, /* ECONNRESET 54 Connection reset by peer */ | |
8174 | RETRY, /* ENOBUFS 55 No buffer space available */ | |
8175 | RETRY, /* EISCONN 56 Socket is already connected */ | |
8176 | RETRY, /* ENOTCONN 57 Socket is not connected */ | |
8177 | RETRY, /* ESHUTDOWN 58 Can't send after socket shutdown */ | |
8178 | RETRY, /* ETOOMANYREFS 59 Too many references: can't splice */ | |
8179 | RETRY, /* ETIMEDOUT 60 Operation timed out */ | |
8180 | RETRY, /* ECONNREFUSED 61 Connection refused */ | |
8181 | ||
8182 | DUMPANDLOG, /* ELOOP 62 Too many levels of symbolic links */ | |
8183 | DUMP, /* ENAMETOOLONG 63 File name too long */ | |
8184 | RETRY, /* EHOSTDOWN 64 Host is down */ | |
8185 | RETRY, /* EHOSTUNREACH 65 No route to host */ | |
8186 | DUMP, /* ENOTEMPTY 66 Directory not empty */ | |
2d21ac55 | 8187 | /* quotas & mush */ |
0a7de745 A |
8188 | DUMPANDLOG, /* PROCLIM 67 Too many processes */ |
8189 | DUMPANDLOG, /* EUSERS 68 Too many users */ | |
8190 | DUMPANDLOG, /* EDQUOT 69 Disc quota exceeded */ | |
2d21ac55 | 8191 | /* Network File System */ |
0a7de745 A |
8192 | DUMP, /* ESTALE 70 Stale NFS file handle */ |
8193 | DUMP, /* EREMOTE 71 Too many levels of remote in path */ | |
8194 | DUMPANDLOG, /* EBADRPC 72 RPC struct is bad */ | |
8195 | DUMPANDLOG, /* ERPCMISMATCH 73 RPC version wrong */ | |
8196 | DUMPANDLOG, /* EPROGUNAVAIL 74 RPC prog. not avail */ | |
8197 | DUMPANDLOG, /* EPROGMISMATCH 75 Program version wrong */ | |
8198 | DUMPANDLOG, /* EPROCUNAVAIL 76 Bad procedure for program */ | |
8199 | ||
8200 | DUMPANDLOG, /* ENOLCK 77 No locks available */ | |
8201 | DUMPANDLOG, /* ENOSYS 78 Function not implemented */ | |
8202 | DUMPANDLOG, /* EFTYPE 79 Inappropriate file type or format */ | |
8203 | DUMPANDLOG, /* EAUTH 80 Authentication error */ | |
8204 | DUMPANDLOG, /* ENEEDAUTH 81 Need authenticator */ | |
2d21ac55 | 8205 | /* Intelligent device errors */ |
0a7de745 A |
8206 | DUMPANDLOG, /* EPWROFF 82 Device power is off */ |
8207 | DUMPANDLOG, /* EDEVERR 83 Device error, e.g. paper out */ | |
8208 | DUMPANDLOG, /* EOVERFLOW 84 Value too large to be stored in data type */ | |
2d21ac55 | 8209 | /* Program loading errors */ |
0a7de745 A |
8210 | DUMPANDLOG, /* EBADEXEC 85 Bad executable */ |
8211 | DUMPANDLOG, /* EBADARCH 86 Bad CPU type in executable */ | |
8212 | DUMPANDLOG, /* ESHLIBVERS 87 Shared library version mismatch */ | |
8213 | DUMPANDLOG, /* EBADMACHO 88 Malformed Macho file */ | |
2d21ac55 A |
8214 | }; |
8215 | ||
b0d623f7 | 8216 | char |
2d21ac55 A |
8217 | nfs_pageouterrorhandler(int error) |
8218 | { | |
0a7de745 A |
8219 | if (error > NFS_ELAST) { |
8220 | return DUMP; | |
8221 | } else { | |
8222 | return errortooutcome[error]; | |
8223 | } | |
2d21ac55 A |
8224 | } |
8225 | ||
8226 | ||
8227 | /* | |
8228 | * vnode OP for pageout using UPL | |
8229 | * | |
8230 | * No buffer I/O, just RPCs straight from the mapped pages. | |
1c79356b A |
8231 | * File size changes are not permitted in pageout. |
8232 | */ | |
b0d623f7 | 8233 | int |
2d21ac55 | 8234 | nfs_vnop_pageout( |
91447636 | 8235 | struct vnop_pageout_args /* { |
0a7de745 A |
8236 | * struct vnodeop_desc *a_desc; |
8237 | * vnode_t a_vp; | |
8238 | * upl_t a_pl; | |
8239 | * vm_offset_t a_pl_offset; | |
8240 | * off_t a_f_offset; | |
8241 | * size_t a_size; | |
8242 | * int a_flags; | |
8243 | * vfs_context_t a_context; | |
8244 | * } */*ap) | |
1c79356b | 8245 | { |
91447636 | 8246 | vnode_t vp = ap->a_vp; |
1c79356b | 8247 | upl_t pl = ap->a_pl; |
f427ee49 | 8248 | upl_size_t size = (upl_size_t)ap->a_size; |
1c79356b | 8249 | off_t f_offset = ap->a_f_offset; |
f427ee49 A |
8250 | upl_offset_t pl_offset = ap->a_pl_offset; |
8251 | upl_offset_t pgsize; | |
2d21ac55 A |
8252 | int flags = ap->a_flags; |
8253 | nfsnode_t np = VTONFS(vp); | |
8254 | thread_t thd; | |
91447636 | 8255 | kauth_cred_t cred; |
55e303ae | 8256 | struct nfsbuf *bp; |
2d21ac55 | 8257 | struct nfsmount *nmp = VTONMP(vp); |
91447636 | 8258 | daddr64_t lbn; |
8f6c56a5 | 8259 | int error = 0, iomode; |
2d21ac55 A |
8260 | off_t off, txoffset, rxoffset; |
8261 | vm_offset_t ioaddr, txaddr, rxaddr; | |
b0d623f7 | 8262 | uio_t auio; |
0a7de745 | 8263 | char uio_buf[UIO_SIZEOF(1)]; |
fa4905b1 | 8264 | int nofreeupl = flags & UPL_NOCOMMIT; |
f427ee49 | 8265 | size_t nmwsize, biosize, iosize, remsize; |
2d21ac55 | 8266 | struct nfsreq *req[MAXPAGINGREQS]; |
b0d623f7 | 8267 | int nextsend, nextwait, wverfset, commit; |
f427ee49 | 8268 | uint64_t wverf, wverf2, xsize, txsize, rxsize; |
cb323159 A |
8269 | #if CONFIG_NFS4 |
8270 | uint32_t stategenid = 0; | |
8271 | #endif | |
8272 | uint32_t vrestart = 0, restart = 0, vrestarts = 0, restarts = 0; | |
b0d623f7 | 8273 | kern_return_t kret; |
1c79356b | 8274 | |
fa4905b1 A |
8275 | FSDBG(323, f_offset, size, pl, pl_offset); |
8276 | ||
0a7de745 | 8277 | if (pl == (upl_t)NULL) { |
fa4905b1 | 8278 | panic("nfs_pageout: no upl"); |
0a7de745 | 8279 | } |
1c79356b | 8280 | |
fa4905b1 | 8281 | if (size <= 0) { |
f427ee49 | 8282 | printf("nfs_pageout: invalid size %u", size); |
0a7de745 | 8283 | if (!nofreeupl) { |
316670eb | 8284 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
0a7de745 A |
8285 | } |
8286 | return EINVAL; | |
1c79356b A |
8287 | } |
8288 | ||
55e303ae | 8289 | if (!nmp) { |
0a7de745 A |
8290 | if (!nofreeupl) { |
8291 | ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY); | |
8292 | } | |
8293 | return ENXIO; | |
55e303ae | 8294 | } |
0c530ab8 | 8295 | biosize = nmp->nm_biosize; |
2d21ac55 A |
8296 | nmwsize = nmp->nm_wsize; |
8297 | ||
b0d623f7 | 8298 | nfs_data_lock_noupdate(np, NFS_DATA_LOCK_SHARED); |
1c79356b | 8299 | |
1c79356b | 8300 | /* |
55e303ae A |
8301 | * Check to see whether the buffer is incore. |
8302 | * If incore and not busy, invalidate it from the cache. | |
1c79356b | 8303 | */ |
55e303ae A |
8304 | for (iosize = 0; iosize < size; iosize += xsize) { |
8305 | off = f_offset + iosize; | |
8306 | /* need make sure we do things on block boundaries */ | |
8307 | xsize = biosize - (off % biosize); | |
f427ee49 | 8308 | if (off + (off_t)xsize > f_offset + (off_t)size) { |
55e303ae | 8309 | xsize = f_offset + size - off; |
0a7de745 | 8310 | } |
2d21ac55 | 8311 | lbn = (daddr64_t)(off / biosize); |
c3c9b80d | 8312 | lck_mtx_lock(&nfs_buf_mutex); |
2d21ac55 | 8313 | if ((bp = nfs_buf_incore(np, lbn))) { |
91447636 A |
8314 | FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags); |
8315 | if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) { | |
c3c9b80d | 8316 | lck_mtx_unlock(&nfs_buf_mutex); |
b0d623f7 | 8317 | nfs_data_unlock_noupdate(np); |
fa4905b1 | 8318 | /* no panic. just tell vm we are busy */ |
0a7de745 | 8319 | if (!nofreeupl) { |
316670eb | 8320 | ubc_upl_abort_range(pl, pl_offset, size, 0); |
0a7de745 A |
8321 | } |
8322 | return EBUSY; | |
55e303ae A |
8323 | } |
8324 | if (bp->nb_dirtyend > 0) { | |
8325 | /* | |
91447636 A |
8326 | * if there's a dirty range in the buffer, check |
8327 | * to see if it extends beyond the pageout region | |
55e303ae A |
8328 | * |
8329 | * if the dirty region lies completely within the | |
8330 | * pageout region, we just invalidate the buffer | |
8331 | * because it's all being written out now anyway. | |
8332 | * | |
8333 | * if any of the dirty region lies outside the | |
8334 | * pageout region, we'll try to clip the dirty | |
8335 | * region to eliminate the portion that's being | |
8336 | * paged out. If that's not possible, because | |
8337 | * the dirty region extends before and after the | |
8338 | * pageout region, then we'll just return EBUSY. | |
8339 | */ | |
8340 | off_t boff, start, end; | |
8341 | boff = NBOFF(bp); | |
8342 | start = off; | |
8343 | end = off + xsize; | |
8344 | /* clip end to EOF */ | |
0a7de745 | 8345 | if (end > (off_t)np->n_size) { |
55e303ae | 8346 | end = np->n_size; |
0a7de745 | 8347 | } |
55e303ae A |
8348 | start -= boff; |
8349 | end -= boff; | |
8350 | if ((bp->nb_dirtyoff < start) && | |
8351 | (bp->nb_dirtyend > end)) { | |
0a7de745 A |
8352 | /* |
8353 | * not gonna be able to clip the dirty region | |
8354 | * | |
8355 | * But before returning the bad news, move the | |
8356 | * buffer to the start of the delwri list and | |
8357 | * give the list a push to try to flush the | |
8358 | * buffer out. | |
8359 | */ | |
8360 | FSDBG(323, np, bp, 0xd00deebc, EBUSY); | |
8361 | nfs_buf_remfree(bp); | |
8362 | TAILQ_INSERT_HEAD(&nfsbufdelwri, bp, nb_free); | |
8363 | nfsbufdelwricnt++; | |
8364 | nfs_buf_drop(bp); | |
8365 | nfs_buf_delwri_push(1); | |
c3c9b80d | 8366 | lck_mtx_unlock(&nfs_buf_mutex); |
0a7de745 A |
8367 | nfs_data_unlock_noupdate(np); |
8368 | if (!nofreeupl) { | |
8369 | ubc_upl_abort_range(pl, pl_offset, size, 0); | |
8370 | } | |
8371 | return EBUSY; | |
55e303ae A |
8372 | } |
8373 | if ((bp->nb_dirtyoff < start) || | |
8374 | (bp->nb_dirtyend > end)) { | |
0a7de745 A |
8375 | /* clip dirty region, if necessary */ |
8376 | if (bp->nb_dirtyoff < start) { | |
f427ee49 | 8377 | bp->nb_dirtyend = MIN(bp->nb_dirtyend, start); |
0a7de745 A |
8378 | } |
8379 | if (bp->nb_dirtyend > end) { | |
f427ee49 | 8380 | bp->nb_dirtyoff = MAX(bp->nb_dirtyoff, end); |
0a7de745 A |
8381 | } |
8382 | FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00); | |
8383 | /* we're leaving this block dirty */ | |
8384 | nfs_buf_drop(bp); | |
c3c9b80d | 8385 | lck_mtx_unlock(&nfs_buf_mutex); |
0a7de745 | 8386 | continue; |
55e303ae A |
8387 | } |
8388 | } | |
8389 | nfs_buf_remfree(bp); | |
c3c9b80d | 8390 | lck_mtx_unlock(&nfs_buf_mutex); |
91447636 | 8391 | SET(bp->nb_flags, NB_INVAL); |
b0d623f7 | 8392 | nfs_node_lock_force(np); |
55e303ae A |
8393 | if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) { |
8394 | CLR(bp->nb_flags, NB_NEEDCOMMIT); | |
8395 | np->n_needcommitcnt--; | |
8396 | CHECK_NEEDCOMMITCNT(np); | |
fa4905b1 | 8397 | } |
b0d623f7 | 8398 | nfs_node_unlock(np); |
483a1d10 | 8399 | nfs_buf_release(bp, 1); |
91447636 | 8400 | } else { |
c3c9b80d | 8401 | lck_mtx_unlock(&nfs_buf_mutex); |
1c79356b | 8402 | } |
1c79356b A |
8403 | } |
8404 | ||
2d21ac55 | 8405 | thd = vfs_context_thread(ap->a_context); |
1c79356b | 8406 | cred = ubc_getcred(vp); |
0a7de745 | 8407 | if (!IS_VALID_CRED(cred)) { |
91447636 | 8408 | cred = vfs_context_ucred(ap->a_context); |
0a7de745 | 8409 | } |
1c79356b | 8410 | |
b0d623f7 | 8411 | nfs_node_lock_force(np); |
1c79356b | 8412 | if (np->n_flag & NWRITEERR) { |
2d21ac55 | 8413 | error = np->n_error; |
b0d623f7 A |
8414 | nfs_node_unlock(np); |
8415 | nfs_data_unlock_noupdate(np); | |
0a7de745 | 8416 | if (!nofreeupl) { |
fa4905b1 | 8417 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
8418 | UPL_ABORT_FREE_ON_EMPTY); |
8419 | } | |
8420 | return error; | |
1c79356b | 8421 | } |
b0d623f7 | 8422 | nfs_node_unlock(np); |
1c79356b | 8423 | |
91447636 | 8424 | if (f_offset < 0 || f_offset >= (off_t)np->n_size || |
55e303ae | 8425 | f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) { |
b0d623f7 | 8426 | nfs_data_unlock_noupdate(np); |
0a7de745 | 8427 | if (!nofreeupl) { |
fa4905b1 | 8428 | ubc_upl_abort_range(pl, pl_offset, size, |
0a7de745 A |
8429 | UPL_ABORT_FREE_ON_EMPTY); |
8430 | } | |
8431 | return EINVAL; | |
1c79356b A |
8432 | } |
8433 | ||
b0d623f7 | 8434 | kret = ubc_upl_map(pl, &ioaddr); |
0a7de745 | 8435 | if (kret != KERN_SUCCESS) { |
b0d623f7 | 8436 | panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret); |
0a7de745 | 8437 | } |
55e303ae | 8438 | ioaddr += pl_offset; |
1c79356b | 8439 | |
0a7de745 | 8440 | if ((u_quad_t)f_offset + size > np->n_size) { |
55e303ae | 8441 | xsize = np->n_size - f_offset; |
0a7de745 | 8442 | } else { |
55e303ae | 8443 | xsize = size; |
0a7de745 | 8444 | } |
1c79356b | 8445 | |
f427ee49 | 8446 | pgsize = (upl_offset_t)round_page_64(xsize); |
0a7de745 | 8447 | if ((size > pgsize) && !nofreeupl) { |
2d21ac55 | 8448 | ubc_upl_abort_range(pl, pl_offset + pgsize, size - pgsize, |
0a7de745 A |
8449 | UPL_ABORT_FREE_ON_EMPTY); |
8450 | } | |
1c79356b | 8451 | |
2d21ac55 | 8452 | /* |
1c79356b A |
8453 | * check for partial page and clear the |
8454 | * contents past end of the file before | |
8455 | * releasing it in the VM page cache | |
8456 | */ | |
91447636 | 8457 | if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) { |
f427ee49 A |
8458 | uint64_t io = np->n_size - f_offset; |
8459 | NFS_BZERO((caddr_t)(ioaddr + io), size - io); | |
fa4905b1 | 8460 | FSDBG(321, np->n_size, f_offset, f_offset + io, size - io); |
1c79356b | 8461 | } |
b0d623f7 | 8462 | nfs_data_unlock_noupdate(np); |
1c79356b | 8463 | |
b0d623f7 | 8464 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE, |
0a7de745 | 8465 | &uio_buf, sizeof(uio_buf)); |
55e303ae | 8466 | |
2d21ac55 | 8467 | tryagain: |
cb323159 | 8468 | #if CONFIG_NFS4 |
0a7de745 | 8469 | if (nmp->nm_vers >= NFS_VER4) { |
b0d623f7 | 8470 | stategenid = nmp->nm_stategenid; |
0a7de745 | 8471 | } |
cb323159 | 8472 | #endif |
2d21ac55 A |
8473 | wverf = wverf2 = wverfset = 0; |
8474 | txsize = rxsize = xsize; | |
8475 | txoffset = rxoffset = f_offset; | |
8476 | txaddr = rxaddr = ioaddr; | |
8477 | commit = NFS_WRITE_FILESYNC; | |
8478 | ||
8479 | bzero(req, sizeof(req)); | |
8480 | nextsend = nextwait = 0; | |
1c79356b | 8481 | do { |
6d2010ae A |
8482 | if (np->n_flag & NREVOKE) { |
8483 | error = EIO; | |
8484 | break; | |
8485 | } | |
2d21ac55 A |
8486 | /* send requests while we need to and have available slots */ |
8487 | while ((txsize > 0) && (req[nextsend] == NULL)) { | |
f427ee49 | 8488 | iosize = (size_t)MIN(nmwsize, txsize); |
b0d623f7 A |
8489 | uio_reset(auio, txoffset, UIO_SYSSPACE, UIO_WRITE); |
8490 | uio_addiov(auio, CAST_USER_ADDR_T(txaddr), iosize); | |
8491 | FSDBG(323, uio_offset(auio), iosize, txaddr, txsize); | |
316670eb | 8492 | OSAddAtomic64(1, &nfsstats.pageouts); |
b0d623f7 A |
8493 | nfs_node_lock_force(np); |
8494 | np->n_numoutput++; | |
8495 | nfs_node_unlock(np); | |
2d21ac55 A |
8496 | vnode_startwrite(vp); |
8497 | iomode = NFS_WRITE_UNSTABLE; | |
b0d623f7 | 8498 | if ((error = nmp->nm_funcs->nf_write_rpc_async(np, auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) { |
2d21ac55 A |
8499 | req[nextsend] = NULL; |
8500 | vnode_writedone(vp); | |
b0d623f7 A |
8501 | nfs_node_lock_force(np); |
8502 | np->n_numoutput--; | |
8503 | nfs_node_unlock(np); | |
2d21ac55 A |
8504 | break; |
8505 | } | |
8506 | txaddr += iosize; | |
8507 | txoffset += iosize; | |
8508 | txsize -= iosize; | |
8509 | nextsend = (nextsend + 1) % MAXPAGINGREQS; | |
8510 | } | |
8511 | /* wait while we need to and break out if more requests to send */ | |
8512 | while ((rxsize > 0) && req[nextwait]) { | |
f427ee49 | 8513 | iosize = remsize = (size_t)MIN(nmwsize, rxsize); |
2d21ac55 A |
8514 | error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req[nextwait], &iomode, &iosize, &wverf2); |
8515 | req[nextwait] = NULL; | |
8516 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
8517 | vnode_writedone(vp); | |
b0d623f7 A |
8518 | nfs_node_lock_force(np); |
8519 | np->n_numoutput--; | |
8520 | nfs_node_unlock(np); | |
cb323159 | 8521 | #if CONFIG_NFS4 |
b0d623f7 A |
8522 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
8523 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
8524 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
8525 | NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error); | |
8526 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
8527 | } |
8528 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
8529 | restart = 1; |
8530 | goto cancel; | |
8531 | } | |
cb323159 | 8532 | #endif |
2d21ac55 A |
8533 | if (error) { |
8534 | FSDBG(323, rxoffset, rxsize, error, -1); | |
8535 | break; | |
8536 | } | |
8537 | if (!wverfset) { | |
8538 | wverf = wverf2; | |
8539 | wverfset = 1; | |
8540 | } else if (wverf != wverf2) { | |
8541 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 8542 | vrestart = 1; |
2d21ac55 A |
8543 | goto cancel; |
8544 | } | |
8545 | /* Retain the lowest commitment level returned. */ | |
0a7de745 | 8546 | if (iomode < commit) { |
2d21ac55 | 8547 | commit = iomode; |
0a7de745 A |
8548 | } |
8549 | rxaddr += iosize; | |
8550 | rxoffset += iosize; | |
2d21ac55 A |
8551 | rxsize -= iosize; |
8552 | remsize -= iosize; | |
8553 | if (remsize > 0) { | |
8554 | /* need to try sending the remainder */ | |
8555 | iosize = remsize; | |
b0d623f7 A |
8556 | uio_reset(auio, rxoffset, UIO_SYSSPACE, UIO_WRITE); |
8557 | uio_addiov(auio, CAST_USER_ADDR_T(rxaddr), remsize); | |
2d21ac55 | 8558 | iomode = NFS_WRITE_UNSTABLE; |
b0d623f7 | 8559 | error = nfs_write_rpc2(np, auio, thd, cred, &iomode, &wverf2); |
cb323159 | 8560 | #if CONFIG_NFS4 |
b0d623f7 | 8561 | if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) { |
6d2010ae | 8562 | NP(np, "nfs_vnop_pageout: restart: error %d", error); |
b0d623f7 | 8563 | lck_mtx_lock(&nmp->nm_lock); |
6d2010ae A |
8564 | if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) { |
8565 | NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error); | |
8566 | nfs_need_recover(nmp, error); | |
b0d623f7 A |
8567 | } |
8568 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
8569 | restart = 1; |
8570 | goto cancel; | |
8571 | } | |
cb323159 | 8572 | #endif |
2d21ac55 A |
8573 | if (error) { |
8574 | FSDBG(323, rxoffset, rxsize, error, -1); | |
8575 | break; | |
8576 | } | |
8577 | if (wverf != wverf2) { | |
8578 | /* verifier changed, so we need to restart all the writes */ | |
b0d623f7 | 8579 | vrestart = 1; |
2d21ac55 A |
8580 | goto cancel; |
8581 | } | |
0a7de745 | 8582 | if (iomode < commit) { |
2d21ac55 | 8583 | commit = iomode; |
0a7de745 A |
8584 | } |
8585 | rxaddr += iosize; | |
8586 | rxoffset += iosize; | |
2d21ac55 A |
8587 | rxsize -= iosize; |
8588 | } | |
0a7de745 | 8589 | if (txsize) { |
2d21ac55 | 8590 | break; |
0a7de745 | 8591 | } |
2d21ac55 A |
8592 | } |
8593 | } while (!error && (txsize || rxsize)); | |
8594 | ||
b0d623f7 | 8595 | vrestart = 0; |
2d21ac55 A |
8596 | |
8597 | if (!error && (commit != NFS_WRITE_FILESYNC)) { | |
6d2010ae | 8598 | error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred, wverf); |
2d21ac55 | 8599 | if (error == NFSERR_STALEWRITEVERF) { |
b0d623f7 | 8600 | vrestart = 1; |
2d21ac55 A |
8601 | error = EIO; |
8602 | } | |
8603 | } | |
8604 | ||
8605 | if (error) { | |
8606 | cancel: | |
8607 | /* cancel any outstanding requests */ | |
8608 | while (req[nextwait]) { | |
8609 | nfs_request_async_cancel(req[nextwait]); | |
8610 | req[nextwait] = NULL; | |
8611 | nextwait = (nextwait + 1) % MAXPAGINGREQS; | |
8612 | vnode_writedone(vp); | |
b0d623f7 A |
8613 | nfs_node_lock_force(np); |
8614 | np->n_numoutput--; | |
8615 | nfs_node_unlock(np); | |
8616 | } | |
6d2010ae A |
8617 | if (np->n_flag & NREVOKE) { |
8618 | error = EIO; | |
8619 | } else { | |
8620 | if (vrestart) { | |
0a7de745 | 8621 | if (++vrestarts <= 100) { /* guard against no progress */ |
6d2010ae | 8622 | goto tryagain; |
0a7de745 | 8623 | } |
6d2010ae A |
8624 | NP(np, "nfs_pageout: too many restarts, aborting"); |
8625 | FSDBG(323, f_offset, xsize, ERESTART, -1); | |
8626 | } | |
8627 | if (restart) { | |
8628 | if (restarts <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */ | |
0a7de745 A |
8629 | if (error == NFSERR_GRACE) { |
8630 | tsleep(&nmp->nm_state, (PZERO - 1), "nfsgrace", 2 * hz); | |
8631 | } | |
8632 | if (!(error = nfs_mount_state_wait_for_recovery(nmp))) { | |
6d2010ae | 8633 | goto tryagain; |
0a7de745 | 8634 | } |
6d2010ae A |
8635 | } else { |
8636 | NP(np, "nfs_pageout: too many restarts, aborting"); | |
8637 | FSDBG(323, f_offset, xsize, ERESTART, -1); | |
8638 | } | |
8639 | } | |
2d21ac55 A |
8640 | } |
8641 | } | |
8642 | ||
0b4e3aa0 | 8643 | ubc_upl_unmap(pl); |
2d21ac55 | 8644 | |
0b4e3aa0 A |
8645 | /* |
8646 | * We've had several different solutions on what to do when the pageout | |
2d21ac55 A |
8647 | * gets an error. If we don't handle it, and return an error to the |
8648 | * caller, vm, it will retry . This can end in endless looping | |
0b4e3aa0 A |
8649 | * between vm and here doing retries of the same page. Doing a dump |
8650 | * back to vm, will get it out of vm's knowledge and we lose whatever | |
8651 | * data existed. This is risky, but in some cases necessary. For | |
8652 | * example, the initial fix here was to do that for ESTALE. In that case | |
2d21ac55 A |
8653 | * the server is telling us that the file is no longer the same. We |
8654 | * would not want to keep paging out to that. We also saw some 151 | |
0b4e3aa0 | 8655 | * errors from Auspex server and NFSv3 can return errors higher than |
fa4905b1 A |
8656 | * ELAST. Those along with NFS known server errors we will "dump" from |
8657 | * vm. Errors we don't expect to occur, we dump and log for further | |
0b4e3aa0 A |
8658 | * analysis. Errors that could be transient, networking ones, |
8659 | * we let vm "retry". Lastly, errors that we retry, but may have potential | |
8660 | * to storm the network, we "retrywithsleep". "sever" will be used in | |
8661 | * in the future to dump all pages of object for cases like ESTALE. | |
8662 | * All this is the basis for the states returned and first guesses on | |
8663 | * error handling. Tweaking expected as more statistics are gathered. | |
8664 | * Note, in the long run we may need another more robust solution to | |
8665 | * have some kind of persistant store when the vm cannot dump nor keep | |
fa4905b1 | 8666 | * retrying as a solution, but this would be a file architectural change |
0b4e3aa0 | 8667 | */ |
fa4905b1 | 8668 | if (!nofreeupl) { /* otherwise stacked file system has to handle this */ |
0b4e3aa0 | 8669 | if (error) { |
2d21ac55 A |
8670 | int abortflags = 0; |
8671 | char action = nfs_pageouterrorhandler(error); | |
0a7de745 | 8672 | |
0b4e3aa0 | 8673 | switch (action) { |
0a7de745 A |
8674 | case DUMP: |
8675 | abortflags = UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY; | |
8676 | break; | |
8677 | case DUMPANDLOG: | |
8678 | abortflags = UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY; | |
8679 | if (error <= NFS_ELAST) { | |
8680 | if ((errorcount[error] % 100) == 0) { | |
8681 | NP(np, "nfs_pageout: unexpected error %d. dumping vm page", error); | |
2d21ac55 | 8682 | } |
0a7de745 A |
8683 | errorcount[error]++; |
8684 | } | |
8685 | break; | |
8686 | case RETRY: | |
8687 | abortflags = UPL_ABORT_FREE_ON_EMPTY; | |
8688 | break; | |
8689 | case SEVER: /* not implemented */ | |
8690 | default: | |
8691 | NP(np, "nfs_pageout: action %d not expected", action); | |
8692 | break; | |
0b4e3aa0 | 8693 | } |
2d21ac55 A |
8694 | |
8695 | ubc_upl_abort_range(pl, pl_offset, pgsize, abortflags); | |
0b4e3aa0 | 8696 | /* return error in all cases above */ |
2d21ac55 | 8697 | } else { |
0b4e3aa0 | 8698 | ubc_upl_commit_range(pl, pl_offset, pgsize, |
0a7de745 A |
8699 | UPL_COMMIT_CLEAR_DIRTY | |
8700 | UPL_COMMIT_FREE_ON_EMPTY); | |
2d21ac55 | 8701 | } |
1c79356b | 8702 | } |
0a7de745 | 8703 | return error; |
1c79356b A |
8704 | } |
8705 | ||
8706 | /* Blktooff derives file offset given a logical block number */ | |
b0d623f7 | 8707 | int |
2d21ac55 | 8708 | nfs_vnop_blktooff( |
91447636 | 8709 | struct vnop_blktooff_args /* { |
0a7de745 A |
8710 | * struct vnodeop_desc *a_desc; |
8711 | * vnode_t a_vp; | |
8712 | * daddr64_t a_lblkno; | |
8713 | * off_t *a_offset; | |
8714 | * } */*ap) | |
1c79356b A |
8715 | { |
8716 | int biosize; | |
91447636 | 8717 | vnode_t vp = ap->a_vp; |
2d21ac55 | 8718 | struct nfsmount *nmp = VTONMP(vp); |
1c79356b | 8719 | |
0a7de745 A |
8720 | if (nfs_mount_gone(nmp)) { |
8721 | return ENXIO; | |
8722 | } | |
0c530ab8 | 8723 | biosize = nmp->nm_biosize; |
1c79356b | 8724 | |
91447636 | 8725 | *ap->a_offset = (off_t)(ap->a_lblkno * biosize); |
1c79356b | 8726 | |
0a7de745 | 8727 | return 0; |
1c79356b A |
8728 | } |
8729 | ||
b0d623f7 | 8730 | int |
2d21ac55 | 8731 | nfs_vnop_offtoblk( |
91447636 | 8732 | struct vnop_offtoblk_args /* { |
0a7de745 A |
8733 | * struct vnodeop_desc *a_desc; |
8734 | * vnode_t a_vp; | |
8735 | * off_t a_offset; | |
8736 | * daddr64_t *a_lblkno; | |
8737 | * } */*ap) | |
1c79356b A |
8738 | { |
8739 | int biosize; | |
91447636 | 8740 | vnode_t vp = ap->a_vp; |
2d21ac55 | 8741 | struct nfsmount *nmp = VTONMP(vp); |
1c79356b | 8742 | |
0a7de745 A |
8743 | if (nfs_mount_gone(nmp)) { |
8744 | return ENXIO; | |
8745 | } | |
0c530ab8 | 8746 | biosize = nmp->nm_biosize; |
1c79356b | 8747 | |
91447636 | 8748 | *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize); |
1c79356b | 8749 | |
0a7de745 | 8750 | return 0; |
1c79356b | 8751 | } |
91447636 | 8752 | |
6d2010ae A |
8753 | /* |
8754 | * vnode change monitoring | |
8755 | */ | |
8756 | int | |
8757 | nfs_vnop_monitor( | |
8758 | struct vnop_monitor_args /* { | |
0a7de745 A |
8759 | * struct vnodeop_desc *a_desc; |
8760 | * vnode_t a_vp; | |
8761 | * uint32_t a_events; | |
8762 | * uint32_t a_flags; | |
8763 | * void *a_handle; | |
8764 | * vfs_context_t a_context; | |
8765 | * } */*ap) | |
6d2010ae A |
8766 | { |
8767 | nfsnode_t np = VTONFS(ap->a_vp); | |
8768 | struct nfsmount *nmp = VTONMP(ap->a_vp); | |
8769 | int error = 0; | |
8770 | ||
0a7de745 A |
8771 | if (nfs_mount_gone(nmp)) { |
8772 | return ENXIO; | |
8773 | } | |
6d2010ae A |
8774 | |
8775 | /* make sure that the vnode's monitoring status is up to date */ | |
8776 | lck_mtx_lock(&nmp->nm_lock); | |
8777 | if (vnode_ismonitored(ap->a_vp)) { | |
8778 | /* This vnode is currently being monitored, make sure we're tracking it. */ | |
8779 | if (np->n_monlink.le_next == NFSNOLIST) { | |
8780 | LIST_INSERT_HEAD(&nmp->nm_monlist, np, n_monlink); | |
8781 | nfs_mount_sock_thread_wake(nmp); | |
8782 | } | |
8783 | } else { | |
8784 | /* This vnode is no longer being monitored, make sure we're not tracking it. */ | |
8785 | /* Wait for any in-progress getattr to complete first. */ | |
8786 | while (np->n_mflag & NMMONSCANINPROG) { | |
cb323159 | 8787 | struct timespec ts = { .tv_sec = 1, .tv_nsec = 0 }; |
6d2010ae | 8788 | np->n_mflag |= NMMONSCANWANT; |
0a7de745 | 8789 | msleep(&np->n_mflag, &nmp->nm_lock, PZERO - 1, "nfswaitmonscan", &ts); |
6d2010ae A |
8790 | } |
8791 | if (np->n_monlink.le_next != NFSNOLIST) { | |
8792 | LIST_REMOVE(np, n_monlink); | |
8793 | np->n_monlink.le_next = NFSNOLIST; | |
8794 | } | |
8795 | } | |
8796 | lck_mtx_unlock(&nmp->nm_lock); | |
8797 | ||
0a7de745 | 8798 | return error; |
6d2010ae A |
8799 | } |
8800 | ||
8801 | /* | |
8802 | * Send a vnode notification for the given events. | |
8803 | */ | |
8804 | void | |
8805 | nfs_vnode_notify(nfsnode_t np, uint32_t events) | |
8806 | { | |
8807 | struct nfsmount *nmp = NFSTONMP(np); | |
f427ee49 | 8808 | struct nfs_vattr *nvattr; |
6d2010ae A |
8809 | struct vnode_attr vattr, *vap = NULL; |
8810 | struct timeval now; | |
8811 | ||
8812 | microuptime(&now); | |
8813 | if ((np->n_evtstamp == now.tv_sec) || !nmp) { | |
8814 | /* delay sending this notify */ | |
8815 | np->n_events |= events; | |
8816 | return; | |
8817 | } | |
8818 | events |= np->n_events; | |
8819 | np->n_events = 0; | |
8820 | np->n_evtstamp = now.tv_sec; | |
f427ee49 | 8821 | MALLOC(nvattr, struct nfs_vattr *, sizeof(*nvattr), M_TEMP, M_WAITOK); |
6d2010ae A |
8822 | |
8823 | vfs_get_notify_attributes(&vattr); | |
f427ee49 | 8824 | if (!nfs_getattrcache(np, nvattr, 0)) { |
6d2010ae A |
8825 | vap = &vattr; |
8826 | VATTR_INIT(vap); | |
5ba3f43e | 8827 | |
6d2010ae | 8828 | VATTR_RETURN(vap, va_fsid, vfs_statfs(nmp->nm_mountp)->f_fsid.val[0]); |
f427ee49 A |
8829 | VATTR_RETURN(vap, va_fileid, nvattr->nva_fileid); |
8830 | VATTR_RETURN(vap, va_mode, nvattr->nva_mode); | |
8831 | VATTR_RETURN(vap, va_uid, nvattr->nva_uid); | |
8832 | VATTR_RETURN(vap, va_gid, nvattr->nva_gid); | |
8833 | VATTR_RETURN(vap, va_nlink, nvattr->nva_nlink); | |
6d2010ae A |
8834 | } |
8835 | vnode_notify(NFSTOV(np), events, vap); | |
f427ee49 | 8836 | FREE(nvattr, M_TEMP); |
6d2010ae | 8837 | } |
cb323159 | 8838 | |
ea3f0419 | 8839 | #endif /* CONFIG_NFS_CLIENT */ |