1/*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)dead_vnops.c 8.3 (Berkeley) 5/14/95
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/time.h>
67#include <sys/vnode_internal.h>
68#include <sys/buf_internal.h>
69#include <sys/errno.h>
70#include <sys/namei.h>
71#include <sys/buf.h>
72#include <vfs/vfs_support.h>
73
74int chkvnlock(vnode_t vp);
75
76/*
77 * Prototypes for dead operations on vnodes.
78 */
79int dead_badop(void *);
80int dead_ebadf(void *);
81int dead_lookup(struct vnop_lookup_args *);
82int dead_open(struct vnop_open_args *);
83#define dead_access (int (*)(struct vnop_access_args *))dead_ebadf
84#define dead_getattr (int (*)(struct vnop_getattr_args *))dead_ebadf
85#define dead_setattr (int (*)(struct vnop_setattr_args *))dead_ebadf
86int dead_read(struct vnop_read_args *);
87int dead_write(struct vnop_write_args *);
88int dead_ioctl(struct vnop_ioctl_args *);
89int dead_select(struct vnop_select_args *);
90#define dead_readdir (int (*)(struct vnop_readdir_args *))dead_ebadf
91#define dead_readlink (int (*)(struct vnop_readlink_args *))dead_ebadf
92int dead_strategy(struct vnop_strategy_args *);
93int dead_bwrite(struct vnop_bwrite_args *);
94#define dead_pathconf (int (*)(struct vnop_pathconf_args *))dead_ebadf
95#define dead_advlock (int (*)(struct vnop_advlock_args *))dead_ebadf
96int dead_pagein(struct vnop_pagein_args *);
97int dead_pageout(struct vnop_pageout_args *);
98int dead_blktooff(struct vnop_blktooff_args *);
99int dead_offtoblk(struct vnop_offtoblk_args *);
100int dead_blockmap(struct vnop_blockmap_args *);
101
102#define dead_nullop (void (*)(void))nullop
103
104#define VOPFUNC int (*)(void *)
105int(**dead_vnodeop_p)(void *);
106const struct vnodeopv_entry_desc dead_vnodeop_entries[] = {
107 { .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)(void (*)(void ))vn_default_error },
108 { .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)dead_lookup }, /* lookup */
109 { .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)dead_badop }, /* create */
110 { .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)dead_open }, /* open */
111 { .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)dead_badop }, /* mknod */
112 { .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)dead_nullop }, /* close */
113 { .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)dead_access }, /* access */
114 { .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)dead_getattr }, /* getattr */
115 { .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)dead_setattr }, /* setattr */
116 { .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)dead_read }, /* read */
117 { .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)dead_write }, /* write */
118 { .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)dead_ioctl }, /* ioctl */
119 { .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)dead_select }, /* select */
120 { .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)dead_badop }, /* mmap */
121 { .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)dead_nullop}, /* fsync */
122 { .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)dead_badop }, /* remove */
123 { .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)dead_badop }, /* link */
124 { .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)dead_badop }, /* rename */
125 { .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)dead_badop }, /* mkdir */
126 { .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)dead_badop }, /* rmdir */
127 { .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)dead_badop }, /* symlink */
128 { .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)dead_readdir }, /* readdir */
129 { .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)dead_readlink }, /* readlink */
130 { .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)dead_nullop }, /* inactive */
131 { .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)dead_nullop }, /* reclaim */
132 { .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)dead_strategy }, /* strategy */
133 { .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)dead_pathconf }, /* pathconf */
134 { .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)dead_advlock }, /* advlock */
135 { .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)dead_bwrite }, /* bwrite */
136 { .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein }, /* Pagein */
137 { .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout }, /* Pageout */
138 { .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */
139 { .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)dead_blktooff }, /* blktooff */
140 { .opve_op = &vnop_offtoblk_desc, .opve_impl = (VOPFUNC)dead_offtoblk }, /* offtoblk */
141 { .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)dead_blockmap }, /* blockmap */
142 { .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (VOPFUNC)NULL }
143};
144const struct vnodeopv_desc dead_vnodeop_opv_desc =
145{ .opv_desc_vector_p = &dead_vnodeop_p, .opv_desc_ops = dead_vnodeop_entries };
146
147/*
148 * Trivial lookup routine that always fails.
149 */
150/* ARGSUSED */
151int
152dead_lookup(struct vnop_lookup_args *ap)
153{
154 *ap->a_vpp = NULL;
155 return ENOTDIR;
156}
157
158/*
159 * Open always fails as if device did not exist.
160 */
161/* ARGSUSED */
162int
163dead_open(__unused struct vnop_open_args *ap)
164{
165 return ENXIO;
166}
167
168/*
169 * Vnode op for read
170 */
171/* ARGSUSED */
172int
173dead_read(struct vnop_read_args *ap)
174{
175 if (chkvnlock(vp: ap->a_vp)) {
176 panic("dead_read: lock");
177 }
178 /*
179 * Return EOF for character devices, EIO for others
180 */
181 if (ap->a_vp->v_type != VCHR) {
182 return EIO;
183 }
184 return 0;
185}
186
187/*
188 * Vnode op for write
189 */
190/* ARGSUSED */
191int
192dead_write(struct vnop_write_args *ap)
193{
194 if (chkvnlock(vp: ap->a_vp)) {
195 panic("dead_write: lock");
196 }
197 return EIO;
198}
199
200/*
201 * Device ioctl operation.
202 */
203/* ARGSUSED */
204int
205dead_ioctl(struct vnop_ioctl_args *ap)
206{
207 if (!chkvnlock(vp: ap->a_vp)) {
208 return EBADF;
209 }
210 return VCALL(ap->a_vp, VOFFSET(vnop_ioctl), ap);
211}
212
213/* ARGSUSED */
214int
215dead_select(__unused struct vnop_select_args *ap)
216{
217 /*
218 * Let the user find out that the descriptor is gone.
219 */
220 return 1;
221}
222
223/*
224 * Just call the device strategy routine
225 */
226int
227dead_strategy(struct vnop_strategy_args *ap)
228{
229 vnode_t vp = buf_vnop_vnode(ap->a_bp);
230
231 if (!vp || !chkvnlock(vp)) {
232 if (vp) {
233 vnode_lock(vp);
234 if (!(vp->v_lflag & VL_DEAD) || vp->v_op != dead_vnodeop_p) {
235#if DEVELOPMENT || DEBUG
236 panic("%s: vnode %p (v_lflag 0x%x) is NOT in dead state",
237 __func__, vp, vp->v_lflag);
238#else
239 printf("%s: vnode %p (v_lflag 0x%x) is NOT in dead state",
240 __func__, vp, vp->v_lflag);
241#endif
242 }
243 vnode_unlock(vp);
244 printf("%s: returning EIO for vnode v_flags = 0x%x v_numoutput = %d\n",
245 __func__, vp->v_flag, vp->v_numoutput);
246 }
247 buf_seterror(bp: ap->a_bp, EIO);
248 buf_biodone(bp: ap->a_bp);
249 return EIO;
250 }
251
252 printf("%s: vnode is going to call the original bwrite routine v_lflag 0x%x v_flags = 0x%x v_numoutput = %d\n",
253 __func__, vp->v_lflag, vp->v_flag, vp->v_numoutput);
254
255 return VOCALL(vp->v_op, VOFFSET(vnop_strategy), ap);
256}
257
258/*
259 * Return an error and complete the IO if the vnode is not changing state.
260 */
261int
262dead_bwrite(struct vnop_bwrite_args *ap)
263{
264 buf_t bp = ap->a_bp;
265 vnode_t vp = buf_vnop_vnode(bp);
266
267 if (!vp || !chkvnlock(vp)) {
268 vnode_t buf_vp = buf_vnode(bp);
269
270 if (vp) {
271 vnode_lock(vp);
272 if (!(vp->v_lflag & VL_DEAD) || vp->v_op != dead_vnodeop_p) {
273#if DEVELOPMENT || DEBUG
274 panic("%s: vnode %p (v_lflag 0x%x) is NOT in dead state",
275 __func__, vp, vp->v_lflag);
276#else
277 printf("%s: vnode %p (v_lflag 0x%x) is NOT in dead state",
278 __func__, vp, vp->v_lflag);
279#endif
280 }
281 vnode_unlock(vp);
282 }
283
284 buf_seterror(bp, EIO);
285
286 if (buf_vp) {
287 /*
288 * buf_biodone is expecting this to be incremented (see buf_bwrite)
289 */
290 OSAddAtomic(1, &buf_vp->v_numoutput);
291 printf("%s: returning EIO for vnode v_flags = 0x%x v_numoutput = %d\n",
292 __func__, buf_vp->v_flag, buf_vp->v_numoutput);
293 }
294 buf_biodone(bp);
295 return EIO;
296 }
297
298 printf("%s: vnode is going to call the original bwrite routine v_lflag 0x%x v_flags = 0x%x v_numoutput = %d\n",
299 __func__, vp->v_lflag, vp->v_flag, vp->v_numoutput);
300
301 return VOCALL(vp->v_op, VOFFSET(vnop_bwrite), ap);
302}
303
304/*
305 * Wait until the vnode has finished changing state.
306 */
307int
308dead_blockmap(struct vnop_blockmap_args *ap)
309{
310 if (!chkvnlock(vp: ap->a_vp)) {
311 return EIO;
312 }
313 return VNOP_BLOCKMAP(ap->a_vp, ap->a_foffset, ap->a_size, ap->a_bpn,
314 ap->a_run, ap->a_poff, ap->a_flags, ap->a_context);
315}
316
317/*
318 * Empty vnode failed operation
319 */
320/* ARGSUSED */
321int
322dead_ebadf(__unused void *dummy)
323{
324 return EBADF;
325}
326
327/*
328 * Empty vnode bad operation
329 */
330/* ARGSUSED */
331int
332dead_badop(__unused void *dummy)
333{
334 panic("dead_badop called");
335 /* NOTREACHED */
336 return -1;
337}
338
339/*
340 * We have to wait during times when the vnode is
341 * in a state of change.
342 */
343int
344chkvnlock(vnode_t vp)
345{
346 vnode_lock(vp);
347 if (!(vp->v_lflag & VL_OPSCHANGE)) {
348 vnode_unlock(vp);
349 return 0;
350 }
351 while (vp->v_lflag & VL_DEAD) {
352 msleep(chan: &vp->v_lflag, mtx: &vp->v_lock, PVFS, wmesg: "chkvnlock", NULL);
353 }
354 vnode_unlock(vp);
355 return 1;
356}
357
358
359/* Blktooff */
360int
361dead_blktooff(struct vnop_blktooff_args *ap)
362{
363 if (!chkvnlock(vp: ap->a_vp)) {
364 return EIO;
365 }
366
367 *ap->a_offset = (off_t)-1; /* failure */
368 return 0;
369}
370/* Blktooff */
371int
372dead_offtoblk(struct vnop_offtoblk_args *ap)
373{
374 if (!chkvnlock(vp: ap->a_vp)) {
375 return EIO;
376 }
377
378 *ap->a_lblkno = (daddr64_t)-1; /* failure */
379 return 0;
380}
381