lfs_vnops.c revision 1.73 1 /* $NetBSD: lfs_vnops.c,v 1.73 2002/12/28 17:22:47 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1986, 1989, 1991, 1993, 1995
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.73 2002/12/28 17:22:47 yamt Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/namei.h>
79 #include <sys/resourcevar.h>
80 #include <sys/kernel.h>
81 #include <sys/file.h>
82 #include <sys/stat.h>
83 #include <sys/buf.h>
84 #include <sys/proc.h>
85 #include <sys/mount.h>
86 #include <sys/vnode.h>
87 #include <sys/malloc.h>
88 #include <sys/pool.h>
89 #include <sys/signalvar.h>
90
91 #include <miscfs/fifofs/fifo.h>
92 #include <miscfs/genfs/genfs.h>
93 #include <miscfs/specfs/specdev.h>
94
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/dir.h>
97 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ufs/ufs_extern.h>
99
100 #include <ufs/lfs/lfs.h>
101 #include <ufs/lfs/lfs_extern.h>
102
103 /* Global vfs data structures for lfs. */
104 int (**lfs_vnodeop_p)(void *);
105 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
106 { &vop_default_desc, vn_default_error },
107 { &vop_lookup_desc, ufs_lookup }, /* lookup */
108 { &vop_create_desc, lfs_create }, /* create */
109 { &vop_whiteout_desc, lfs_whiteout }, /* whiteout */
110 { &vop_mknod_desc, lfs_mknod }, /* mknod */
111 { &vop_open_desc, ufs_open }, /* open */
112 { &vop_close_desc, lfs_close }, /* close */
113 { &vop_access_desc, ufs_access }, /* access */
114 { &vop_getattr_desc, lfs_getattr }, /* getattr */
115 { &vop_setattr_desc, lfs_setattr }, /* setattr */
116 { &vop_read_desc, lfs_read }, /* read */
117 { &vop_write_desc, lfs_write }, /* write */
118 { &vop_lease_desc, ufs_lease_check }, /* lease */
119 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
120 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
121 { &vop_poll_desc, ufs_poll }, /* poll */
122 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
123 { &vop_revoke_desc, ufs_revoke }, /* revoke */
124 { &vop_mmap_desc, ufs_mmap }, /* mmap */
125 { &vop_fsync_desc, lfs_fsync }, /* fsync */
126 { &vop_seek_desc, ufs_seek }, /* seek */
127 { &vop_remove_desc, lfs_remove }, /* remove */
128 { &vop_link_desc, lfs_link }, /* link */
129 { &vop_rename_desc, lfs_rename }, /* rename */
130 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
131 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
132 { &vop_symlink_desc, lfs_symlink }, /* symlink */
133 { &vop_readdir_desc, ufs_readdir }, /* readdir */
134 { &vop_readlink_desc, ufs_readlink }, /* readlink */
135 { &vop_abortop_desc, ufs_abortop }, /* abortop */
136 { &vop_inactive_desc, lfs_inactive }, /* inactive */
137 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
138 { &vop_lock_desc, ufs_lock }, /* lock */
139 { &vop_unlock_desc, ufs_unlock }, /* unlock */
140 { &vop_bmap_desc, ufs_bmap }, /* bmap */
141 { &vop_strategy_desc, ufs_strategy }, /* strategy */
142 { &vop_print_desc, ufs_print }, /* print */
143 { &vop_islocked_desc, ufs_islocked }, /* islocked */
144 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
145 { &vop_advlock_desc, ufs_advlock }, /* advlock */
146 { &vop_blkatoff_desc, lfs_blkatoff }, /* blkatoff */
147 { &vop_valloc_desc, lfs_valloc }, /* valloc */
148 { &vop_balloc_desc, lfs_balloc }, /* balloc */
149 { &vop_vfree_desc, lfs_vfree }, /* vfree */
150 { &vop_truncate_desc, lfs_truncate }, /* truncate */
151 { &vop_update_desc, lfs_update }, /* update */
152 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
153 { &vop_getpages_desc, lfs_getpages }, /* getpages */
154 { &vop_putpages_desc, lfs_putpages }, /* putpages */
155 { NULL, NULL }
156 };
157 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
158 { &lfs_vnodeop_p, lfs_vnodeop_entries };
159
160 int (**lfs_specop_p)(void *);
161 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
162 { &vop_default_desc, vn_default_error },
163 { &vop_lookup_desc, spec_lookup }, /* lookup */
164 { &vop_create_desc, spec_create }, /* create */
165 { &vop_mknod_desc, spec_mknod }, /* mknod */
166 { &vop_open_desc, spec_open }, /* open */
167 { &vop_close_desc, lfsspec_close }, /* close */
168 { &vop_access_desc, ufs_access }, /* access */
169 { &vop_getattr_desc, lfs_getattr }, /* getattr */
170 { &vop_setattr_desc, lfs_setattr }, /* setattr */
171 { &vop_read_desc, ufsspec_read }, /* read */
172 { &vop_write_desc, ufsspec_write }, /* write */
173 { &vop_lease_desc, spec_lease_check }, /* lease */
174 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
175 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
176 { &vop_poll_desc, spec_poll }, /* poll */
177 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
178 { &vop_revoke_desc, spec_revoke }, /* revoke */
179 { &vop_mmap_desc, spec_mmap }, /* mmap */
180 { &vop_fsync_desc, spec_fsync }, /* fsync */
181 { &vop_seek_desc, spec_seek }, /* seek */
182 { &vop_remove_desc, spec_remove }, /* remove */
183 { &vop_link_desc, spec_link }, /* link */
184 { &vop_rename_desc, spec_rename }, /* rename */
185 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
186 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
187 { &vop_symlink_desc, spec_symlink }, /* symlink */
188 { &vop_readdir_desc, spec_readdir }, /* readdir */
189 { &vop_readlink_desc, spec_readlink }, /* readlink */
190 { &vop_abortop_desc, spec_abortop }, /* abortop */
191 { &vop_inactive_desc, lfs_inactive }, /* inactive */
192 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
193 { &vop_lock_desc, ufs_lock }, /* lock */
194 { &vop_unlock_desc, ufs_unlock }, /* unlock */
195 { &vop_bmap_desc, spec_bmap }, /* bmap */
196 { &vop_strategy_desc, spec_strategy }, /* strategy */
197 { &vop_print_desc, ufs_print }, /* print */
198 { &vop_islocked_desc, ufs_islocked }, /* islocked */
199 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
200 { &vop_advlock_desc, spec_advlock }, /* advlock */
201 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
202 { &vop_valloc_desc, spec_valloc }, /* valloc */
203 { &vop_vfree_desc, lfs_vfree }, /* vfree */
204 { &vop_truncate_desc, spec_truncate }, /* truncate */
205 { &vop_update_desc, lfs_update }, /* update */
206 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
207 { &vop_getpages_desc, spec_getpages }, /* getpages */
208 { &vop_putpages_desc, spec_putpages }, /* putpages */
209 { NULL, NULL }
210 };
211 const struct vnodeopv_desc lfs_specop_opv_desc =
212 { &lfs_specop_p, lfs_specop_entries };
213
214 int (**lfs_fifoop_p)(void *);
215 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
216 { &vop_default_desc, vn_default_error },
217 { &vop_lookup_desc, fifo_lookup }, /* lookup */
218 { &vop_create_desc, fifo_create }, /* create */
219 { &vop_mknod_desc, fifo_mknod }, /* mknod */
220 { &vop_open_desc, fifo_open }, /* open */
221 { &vop_close_desc, lfsfifo_close }, /* close */
222 { &vop_access_desc, ufs_access }, /* access */
223 { &vop_getattr_desc, lfs_getattr }, /* getattr */
224 { &vop_setattr_desc, lfs_setattr }, /* setattr */
225 { &vop_read_desc, ufsfifo_read }, /* read */
226 { &vop_write_desc, ufsfifo_write }, /* write */
227 { &vop_lease_desc, fifo_lease_check }, /* lease */
228 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
229 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
230 { &vop_poll_desc, fifo_poll }, /* poll */
231 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
232 { &vop_revoke_desc, fifo_revoke }, /* revoke */
233 { &vop_mmap_desc, fifo_mmap }, /* mmap */
234 { &vop_fsync_desc, fifo_fsync }, /* fsync */
235 { &vop_seek_desc, fifo_seek }, /* seek */
236 { &vop_remove_desc, fifo_remove }, /* remove */
237 { &vop_link_desc, fifo_link }, /* link */
238 { &vop_rename_desc, fifo_rename }, /* rename */
239 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
240 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
241 { &vop_symlink_desc, fifo_symlink }, /* symlink */
242 { &vop_readdir_desc, fifo_readdir }, /* readdir */
243 { &vop_readlink_desc, fifo_readlink }, /* readlink */
244 { &vop_abortop_desc, fifo_abortop }, /* abortop */
245 { &vop_inactive_desc, lfs_inactive }, /* inactive */
246 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
247 { &vop_lock_desc, ufs_lock }, /* lock */
248 { &vop_unlock_desc, ufs_unlock }, /* unlock */
249 { &vop_bmap_desc, fifo_bmap }, /* bmap */
250 { &vop_strategy_desc, fifo_strategy }, /* strategy */
251 { &vop_print_desc, ufs_print }, /* print */
252 { &vop_islocked_desc, ufs_islocked }, /* islocked */
253 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
254 { &vop_advlock_desc, fifo_advlock }, /* advlock */
255 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
256 { &vop_valloc_desc, fifo_valloc }, /* valloc */
257 { &vop_vfree_desc, lfs_vfree }, /* vfree */
258 { &vop_truncate_desc, fifo_truncate }, /* truncate */
259 { &vop_update_desc, lfs_update }, /* update */
260 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
261 { &vop_putpages_desc, fifo_putpages }, /* putpages */
262 { NULL, NULL }
263 };
264 const struct vnodeopv_desc lfs_fifoop_opv_desc =
265 { &lfs_fifoop_p, lfs_fifoop_entries };
266
267 /*
268 * A function version of LFS_ITIMES, for the UFS functions which call ITIMES
269 */
270 void
271 lfs_itimes(struct inode *ip, struct timespec *acc, struct timespec *mod, struct timespec *cre)
272 {
273 LFS_ITIMES(ip, acc, mod, cre);
274 }
275
276 #define LFS_READWRITE
277 #include <ufs/ufs/ufs_readwrite.c>
278 #undef LFS_READWRITE
279
280 /*
281 * Synch an open file.
282 */
283 /* ARGSUSED */
284 int
285 lfs_fsync(void *v)
286 {
287 struct vop_fsync_args /* {
288 struct vnode *a_vp;
289 struct ucred *a_cred;
290 int a_flags;
291 off_t offlo;
292 off_t offhi;
293 struct proc *a_p;
294 } */ *ap = v;
295 struct vnode *vp = ap->a_vp;
296 int error;
297
298 /* Ignore the trickle syncer */
299 if (ap->a_flags & FSYNC_LAZY)
300 return 0;
301
302 simple_lock(&vp->v_interlock);
303 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
304 round_page(ap->a_offhi), PGO_CLEANIT | PGO_SYNCIO);
305 if (error)
306 return error;
307 error = VOP_UPDATE(vp, NULL, NULL,
308 (ap->a_flags & FSYNC_WAIT) != 0 ? UPDATE_WAIT : 0);
309 #ifdef DEBUG
310 /*
311 * If we were called from vinvalbuf and lfs_update
312 * didn't flush all our buffers, we're in trouble.
313 */
314 if ((ap->a_flags & FSYNC_WAIT) && LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
315 struct buf *bp;
316
317 bp = LIST_FIRST(&vp->v_dirtyblkhd);
318 printf("lfs_fsync: ino %d failed to sync", VTOI(vp)->i_number);
319 printf("lfs_fsync: iocount = %d\n", VTOI(vp)->i_lfs->lfs_iocount);
320 printf("lfs_fsync: flags are 0x%x, numoutput=%d\n",
321 VTOI(vp)->i_flag, vp->v_numoutput);
322 printf("lfs_fsync: writecount=%ld\n", vp->v_writecount);
323 printf("lfs_fsync: first bp: %p, flags=0x%lx, lbn=%d\n",
324 bp, bp->b_flags, bp->b_lblkno);
325 }
326 #endif
327 return error;
328 }
329
330 /*
331 * Take IN_ADIROP off, then call ufs_inactive.
332 */
333 int
334 lfs_inactive(void *v)
335 {
336 #ifdef DIAGNOSTIC
337 struct vop_inactive_args /* {
338 struct vnode *a_vp;
339 struct proc *a_p;
340 } */ *ap = v;
341 struct vnode *vp = ap->a_vp;
342 struct inode *ip = VTOI(vp);
343
344 KASSERT(!(vp->v_flag & VDIROP));
345 KASSERT(!(ip->i_flag & IN_ADIROP));
346 #endif
347
348 return ufs_inactive(v);
349 }
350
351 /*
352 * These macros are used to bracket UFS directory ops, so that we can
353 * identify all the pages touched during directory ops which need to
354 * be ordered and flushed atomically, so that they may be recovered.
355 */
356 /*
357 * XXX KS - Because we have to mark nodes VDIROP in order to prevent
358 * the cache from reclaiming them while a dirop is in progress, we must
359 * also manage the number of nodes so marked (otherwise we can run out).
360 * We do this by setting lfs_dirvcount to the number of marked vnodes; it
361 * is decremented during segment write, when VDIROP is taken off.
362 */
363 #define SET_DIROP(vp) SET_DIROP2((vp), NULL)
364 #define SET_DIROP2(vp, vp2) lfs_set_dirop((vp), (vp2))
365 static int lfs_set_dirop(struct vnode *, struct vnode *);
366 extern int lfs_dirvcount;
367
368 #define NRESERVE(fs) (btofsb(fs, (NIADDR + 3 + (2 * NIADDR + 3)) << fs->lfs_bshift))
369
370 static int
371 lfs_set_dirop(struct vnode *vp, struct vnode *vp2)
372 {
373 struct lfs *fs;
374 int error;
375
376 KASSERT(VOP_ISLOCKED(vp));
377 KASSERT(vp2 == NULL || VOP_ISLOCKED(vp2));
378
379 fs = VTOI(vp)->i_lfs;
380 /*
381 * We might need one directory block plus supporting indirect blocks,
382 * plus an inode block and ifile page for the new vnode.
383 */
384 if ((error = lfs_reserve(fs, vp, vp2, NRESERVE(fs))) != 0)
385 return (error);
386
387 if (fs->lfs_dirops == 0)
388 lfs_check(vp, LFS_UNUSED_LBN, 0);
389 while (fs->lfs_writer || lfs_dirvcount > LFS_MAXDIROP) {
390 if (fs->lfs_writer)
391 tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_sdirop", 0);
392 if (lfs_dirvcount > LFS_MAXDIROP && fs->lfs_dirops == 0) {
393 ++fs->lfs_writer;
394 lfs_flush(fs, 0);
395 if (--fs->lfs_writer == 0)
396 wakeup(&fs->lfs_dirops);
397 }
398
399 if (lfs_dirvcount > LFS_MAXDIROP) {
400 #ifdef DEBUG_LFS
401 printf("lfs_set_dirop: sleeping with dirops=%d, "
402 "dirvcount=%d\n", fs->lfs_dirops,
403 lfs_dirvcount);
404 #endif
405 if ((error = tsleep(&lfs_dirvcount, PCATCH|PUSER,
406 "lfs_maxdirop", 0)) != 0) {
407 goto unreserve;
408 }
409 }
410 }
411 ++fs->lfs_dirops;
412 fs->lfs_doifile = 1;
413
414 /* Hold a reference so SET_ENDOP will be happy */
415 lfs_vref(vp);
416 if (vp2 != NULL)
417 lfs_vref(vp2);
418
419 return 0;
420
421 unreserve:
422 lfs_reserve(fs, vp, vp2, -NRESERVE(fs));
423 return error;
424 }
425
426 #define SET_ENDOP(fs, vp, str) SET_ENDOP2((fs), (vp), NULL, (str))
427 #define SET_ENDOP2(fs, vp, vp2, str) { \
428 --(fs)->lfs_dirops; \
429 if (!(fs)->lfs_dirops) { \
430 if ((fs)->lfs_nadirop) { \
431 panic("SET_ENDOP: %s: no dirops but nadirop=%d", \
432 (str), (fs)->lfs_nadirop); \
433 } \
434 wakeup(&(fs)->lfs_writer); \
435 lfs_check((vp),LFS_UNUSED_LBN,0); \
436 } \
437 lfs_reserve((fs), vp, vp2, -NRESERVE(fs)); /* XXX */ \
438 lfs_vunref(vp); \
439 if (vp2 != NULL) \
440 lfs_vunref(vp2); \
441 }
442
443 #define MARK_VNODE(dvp) do { \
444 if (!((dvp)->v_flag & VDIROP)) { \
445 (void)lfs_vref(dvp); \
446 ++lfs_dirvcount; \
447 } \
448 (dvp)->v_flag |= VDIROP; \
449 if (!(VTOI(dvp)->i_flag & IN_ADIROP)) { \
450 ++VTOI(dvp)->i_lfs->lfs_nadirop; \
451 } \
452 VTOI(dvp)->i_flag |= IN_ADIROP; \
453 } while (0)
454
455 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
456
457 void lfs_unmark_vnode(struct vnode *vp)
458 {
459 struct inode *ip;
460
461 ip = VTOI(vp);
462
463 if (ip->i_flag & IN_ADIROP)
464 --ip->i_lfs->lfs_nadirop;
465 ip->i_flag &= ~IN_ADIROP;
466 }
467
468 int
469 lfs_symlink(void *v)
470 {
471 struct vop_symlink_args /* {
472 struct vnode *a_dvp;
473 struct vnode **a_vpp;
474 struct componentname *a_cnp;
475 struct vattr *a_vap;
476 char *a_target;
477 } */ *ap = v;
478 int error;
479
480 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
481 vput(ap->a_dvp);
482 return error;
483 }
484 MARK_VNODE(ap->a_dvp);
485 error = ufs_symlink(ap);
486 UNMARK_VNODE(ap->a_dvp);
487 if (*(ap->a_vpp))
488 UNMARK_VNODE(*(ap->a_vpp));
489 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"symlink");
490 return (error);
491 }
492
493 int
494 lfs_mknod(void *v)
495 {
496 struct vop_mknod_args /* {
497 struct vnode *a_dvp;
498 struct vnode **a_vpp;
499 struct componentname *a_cnp;
500 struct vattr *a_vap;
501 } */ *ap = v;
502 struct vattr *vap = ap->a_vap;
503 struct vnode **vpp = ap->a_vpp;
504 struct inode *ip;
505 int error;
506 struct mount *mp;
507 ino_t ino;
508
509 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
510 vput(ap->a_dvp);
511 return error;
512 }
513 MARK_VNODE(ap->a_dvp);
514 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
515 ap->a_dvp, vpp, ap->a_cnp);
516 UNMARK_VNODE(ap->a_dvp);
517 if (*(ap->a_vpp))
518 UNMARK_VNODE(*(ap->a_vpp));
519
520 /* Either way we're done with the dirop at this point */
521 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mknod");
522
523 if (error)
524 return (error);
525
526 ip = VTOI(*vpp);
527 mp = (*vpp)->v_mount;
528 ino = ip->i_number;
529 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
530 if (vap->va_rdev != VNOVAL) {
531 /*
532 * Want to be able to use this to make badblock
533 * inodes, so don't truncate the dev number.
534 */
535 #if 0
536 ip->i_ffs_rdev = ufs_rw32(vap->va_rdev,
537 UFS_MPNEEDSWAP((*vpp)->v_mount));
538 #else
539 ip->i_ffs_rdev = vap->va_rdev;
540 #endif
541 }
542 /*
543 * Call fsync to write the vnode so that we don't have to deal with
544 * flushing it when it's marked VDIROP|VXLOCK.
545 *
546 * XXX KS - If we can't flush we also can't call vgone(), so must
547 * return. But, that leaves this vnode in limbo, also not good.
548 * Can this ever happen (barring hardware failure)?
549 */
550 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0, curproc)) != 0) {
551 printf("Couldn't fsync in mknod (ino %d)---what do I do?\n",
552 VTOI(*vpp)->i_number);
553 return (error);
554 }
555 /*
556 * Remove vnode so that it will be reloaded by VFS_VGET and
557 * checked to see if it is an alias of an existing entry in
558 * the inode cache.
559 */
560 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
561 VOP_UNLOCK(*vpp, 0);
562 lfs_vunref(*vpp);
563 (*vpp)->v_type = VNON;
564 vgone(*vpp);
565 error = VFS_VGET(mp, ino, vpp);
566 if (error != 0) {
567 *vpp = NULL;
568 return (error);
569 }
570 return (0);
571 }
572
573 int
574 lfs_create(void *v)
575 {
576 struct vop_create_args /* {
577 struct vnode *a_dvp;
578 struct vnode **a_vpp;
579 struct componentname *a_cnp;
580 struct vattr *a_vap;
581 } */ *ap = v;
582 int error;
583
584 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
585 vput(ap->a_dvp);
586 return error;
587 }
588 MARK_VNODE(ap->a_dvp);
589 error = ufs_create(ap);
590 UNMARK_VNODE(ap->a_dvp);
591 if (*(ap->a_vpp))
592 UNMARK_VNODE(*(ap->a_vpp));
593 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"create");
594 return (error);
595 }
596
597 int
598 lfs_whiteout(void *v)
599 {
600 struct vop_whiteout_args /* {
601 struct vnode *a_dvp;
602 struct componentname *a_cnp;
603 int a_flags;
604 } */ *ap = v;
605 int error;
606
607 if ((error = SET_DIROP(ap->a_dvp)) != 0)
608 /* XXX no unlock here? */
609 return error;
610 MARK_VNODE(ap->a_dvp);
611 error = ufs_whiteout(ap);
612 UNMARK_VNODE(ap->a_dvp);
613 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"whiteout");
614 return (error);
615 }
616
617 int
618 lfs_mkdir(void *v)
619 {
620 struct vop_mkdir_args /* {
621 struct vnode *a_dvp;
622 struct vnode **a_vpp;
623 struct componentname *a_cnp;
624 struct vattr *a_vap;
625 } */ *ap = v;
626 int error;
627
628 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
629 vput(ap->a_dvp);
630 return error;
631 }
632 MARK_VNODE(ap->a_dvp);
633 error = ufs_mkdir(ap);
634 UNMARK_VNODE(ap->a_dvp);
635 if (*(ap->a_vpp))
636 UNMARK_VNODE(*(ap->a_vpp));
637 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mkdir");
638 return (error);
639 }
640
641 int
642 lfs_remove(void *v)
643 {
644 struct vop_remove_args /* {
645 struct vnode *a_dvp;
646 struct vnode *a_vp;
647 struct componentname *a_cnp;
648 } */ *ap = v;
649 struct vnode *dvp, *vp;
650 int error;
651
652 dvp = ap->a_dvp;
653 vp = ap->a_vp;
654 if ((error = SET_DIROP2(dvp, vp)) != 0) {
655 if (dvp == vp)
656 vrele(vp);
657 else
658 vput(vp);
659 vput(dvp);
660 return error;
661 }
662 MARK_VNODE(dvp);
663 MARK_VNODE(vp);
664 error = ufs_remove(ap);
665 UNMARK_VNODE(dvp);
666 UNMARK_VNODE(vp);
667
668 /*
669 * If ufs_remove failed, vp doesn't need to be VDIROP any more.
670 * If it succeeded, we can go ahead and wipe out vp, since
671 * its loss won't appear on disk until checkpoint, and by then
672 * dvp will have been written, completing the dirop.
673 */
674 --lfs_dirvcount;
675 vp->v_flag &= ~VDIROP;
676 wakeup(&lfs_dirvcount);
677 vrele(vp);
678
679 SET_ENDOP2(VTOI(dvp)->i_lfs, dvp, vp, "remove");
680 return (error);
681 }
682
683 int
684 lfs_rmdir(void *v)
685 {
686 struct vop_rmdir_args /* {
687 struct vnodeop_desc *a_desc;
688 struct vnode *a_dvp;
689 struct vnode *a_vp;
690 struct componentname *a_cnp;
691 } */ *ap = v;
692 int error;
693
694 if ((error = SET_DIROP2(ap->a_dvp, ap->a_vp)) != 0) {
695 vrele(ap->a_dvp);
696 if (ap->a_vp != ap->a_dvp)
697 VOP_UNLOCK(ap->a_dvp, 0);
698 vput(ap->a_vp);
699 return error;
700 }
701 MARK_VNODE(ap->a_dvp);
702 MARK_VNODE(ap->a_vp);
703 error = ufs_rmdir(ap);
704 UNMARK_VNODE(ap->a_dvp);
705 UNMARK_VNODE(ap->a_vp);
706
707 /*
708 * If ufs_rmdir failed, vp doesn't need to be VDIROP any more.
709 * If it succeeded, we can go ahead and wipe out vp, since
710 * its loss won't appear on disk until checkpoint, and by then
711 * dvp will have been written, completing the dirop.
712 */
713 --lfs_dirvcount;
714 ap->a_vp->v_flag &= ~VDIROP;
715 wakeup(&lfs_dirvcount);
716 vrele(ap->a_vp);
717
718 SET_ENDOP2(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
719 return (error);
720 }
721
722 int
723 lfs_link(void *v)
724 {
725 struct vop_link_args /* {
726 struct vnode *a_dvp;
727 struct vnode *a_vp;
728 struct componentname *a_cnp;
729 } */ *ap = v;
730 int error;
731
732 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
733 vput(ap->a_dvp);
734 return error;
735 }
736 MARK_VNODE(ap->a_dvp);
737 error = ufs_link(ap);
738 UNMARK_VNODE(ap->a_dvp);
739 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"link");
740 return (error);
741 }
742
743 int
744 lfs_rename(void *v)
745 {
746 struct vop_rename_args /* {
747 struct vnode *a_fdvp;
748 struct vnode *a_fvp;
749 struct componentname *a_fcnp;
750 struct vnode *a_tdvp;
751 struct vnode *a_tvp;
752 struct componentname *a_tcnp;
753 } */ *ap = v;
754 struct vnode *tvp, *fvp, *tdvp, *fdvp;
755 int error;
756 struct lfs *fs;
757
758 fs = VTOI(ap->a_fdvp)->i_lfs;
759 tvp = ap->a_tvp;
760 tdvp = ap->a_tdvp;
761 fvp = ap->a_fvp;
762 fdvp = ap->a_fdvp;
763
764 /*
765 * Check for cross-device rename.
766 * If it is, we don't want to set dirops, just error out.
767 * (In particular note that MARK_VNODE(tdvp) will DTWT on
768 * a cross-device rename.)
769 *
770 * Copied from ufs_rename.
771 */
772 if ((fvp->v_mount != tdvp->v_mount) ||
773 (tvp && (fvp->v_mount != tvp->v_mount))) {
774 error = EXDEV;
775 goto errout;
776 }
777 if ((error = SET_DIROP2(tdvp, tvp)) != 0)
778 goto errout;
779 MARK_VNODE(fdvp);
780 MARK_VNODE(tdvp);
781 MARK_VNODE(fvp);
782 if (tvp) {
783 MARK_VNODE(tvp);
784 }
785 error = ufs_rename(ap);
786 UNMARK_VNODE(fdvp);
787 UNMARK_VNODE(tdvp);
788 UNMARK_VNODE(fvp);
789 if (tvp) {
790 UNMARK_VNODE(tvp);
791 }
792 SET_ENDOP2(fs, tdvp, tvp, "rename");
793 return (error);
794
795 errout:
796 VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
797 if (tdvp == tvp)
798 vrele(tdvp);
799 else
800 vput(tdvp);
801 if (tvp)
802 vput(tvp);
803 VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
804 vrele(fdvp);
805 vrele(fvp);
806 return (error);
807 }
808
809 /* XXX hack to avoid calling ITIMES in getattr */
810 int
811 lfs_getattr(void *v)
812 {
813 struct vop_getattr_args /* {
814 struct vnode *a_vp;
815 struct vattr *a_vap;
816 struct ucred *a_cred;
817 struct proc *a_p;
818 } */ *ap = v;
819 struct vnode *vp = ap->a_vp;
820 struct inode *ip = VTOI(vp);
821 struct vattr *vap = ap->a_vap;
822 struct lfs *fs = ip->i_lfs;
823 /*
824 * Copy from inode table
825 */
826 vap->va_fsid = ip->i_dev;
827 vap->va_fileid = ip->i_number;
828 vap->va_mode = ip->i_ffs_mode & ~IFMT;
829 vap->va_nlink = ip->i_ffs_nlink;
830 vap->va_uid = ip->i_ffs_uid;
831 vap->va_gid = ip->i_ffs_gid;
832 vap->va_rdev = (dev_t)ip->i_ffs_rdev;
833 vap->va_size = vp->v_size;
834 vap->va_atime.tv_sec = ip->i_ffs_atime;
835 vap->va_atime.tv_nsec = ip->i_ffs_atimensec;
836 vap->va_mtime.tv_sec = ip->i_ffs_mtime;
837 vap->va_mtime.tv_nsec = ip->i_ffs_mtimensec;
838 vap->va_ctime.tv_sec = ip->i_ffs_ctime;
839 vap->va_ctime.tv_nsec = ip->i_ffs_ctimensec;
840 vap->va_flags = ip->i_ffs_flags;
841 vap->va_gen = ip->i_ffs_gen;
842 /* this doesn't belong here */
843 if (vp->v_type == VBLK)
844 vap->va_blocksize = BLKDEV_IOSIZE;
845 else if (vp->v_type == VCHR)
846 vap->va_blocksize = MAXBSIZE;
847 else
848 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
849 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_ffs_blocks);
850 vap->va_type = vp->v_type;
851 vap->va_filerev = ip->i_modrev;
852 return (0);
853 }
854
855 /*
856 * Check to make sure the inode blocks won't choke the buffer
857 * cache, then call ufs_setattr as usual.
858 */
859 int
860 lfs_setattr(void *v)
861 {
862 struct vop_getattr_args /* {
863 struct vnode *a_vp;
864 struct vattr *a_vap;
865 struct ucred *a_cred;
866 struct proc *a_p;
867 } */ *ap = v;
868 struct vnode *vp = ap->a_vp;
869
870 lfs_check(vp, LFS_UNUSED_LBN, 0);
871 return ufs_setattr(v);
872 }
873
874 /*
875 * Close called
876 *
877 * XXX -- we were using ufs_close, but since it updates the
878 * times on the inode, we might need to bump the uinodes
879 * count.
880 */
881 /* ARGSUSED */
882 int
883 lfs_close(void *v)
884 {
885 struct vop_close_args /* {
886 struct vnode *a_vp;
887 int a_fflag;
888 struct ucred *a_cred;
889 struct proc *a_p;
890 } */ *ap = v;
891 struct vnode *vp = ap->a_vp;
892 struct inode *ip = VTOI(vp);
893 struct timespec ts;
894
895 if (vp->v_usecount > 1) {
896 TIMEVAL_TO_TIMESPEC(&time, &ts);
897 LFS_ITIMES(ip, &ts, &ts, &ts);
898 }
899 return (0);
900 }
901
902 /*
903 * Close wrapper for special devices.
904 *
905 * Update the times on the inode then do device close.
906 */
907 int
908 lfsspec_close(void *v)
909 {
910 struct vop_close_args /* {
911 struct vnode *a_vp;
912 int a_fflag;
913 struct ucred *a_cred;
914 struct proc *a_p;
915 } */ *ap = v;
916 struct vnode *vp;
917 struct inode *ip;
918 struct timespec ts;
919
920 vp = ap->a_vp;
921 ip = VTOI(vp);
922 if (vp->v_usecount > 1) {
923 TIMEVAL_TO_TIMESPEC(&time, &ts);
924 LFS_ITIMES(ip, &ts, &ts, &ts);
925 }
926 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
927 }
928
929 /*
930 * Close wrapper for fifo's.
931 *
932 * Update the times on the inode then do device close.
933 */
934 int
935 lfsfifo_close(void *v)
936 {
937 struct vop_close_args /* {
938 struct vnode *a_vp;
939 int a_fflag;
940 struct ucred *a_cred;
941 struct proc *a_p;
942 } */ *ap = v;
943 struct vnode *vp;
944 struct inode *ip;
945 struct timespec ts;
946
947 vp = ap->a_vp;
948 ip = VTOI(vp);
949 if (ap->a_vp->v_usecount > 1) {
950 TIMEVAL_TO_TIMESPEC(&time, &ts);
951 LFS_ITIMES(ip, &ts, &ts, &ts);
952 }
953 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
954 }
955
956 /*
957 * Reclaim an inode so that it can be used for other purposes.
958 */
959 int lfs_no_inactive = 0;
960
961 int
962 lfs_reclaim(void *v)
963 {
964 struct vop_reclaim_args /* {
965 struct vnode *a_vp;
966 struct proc *a_p;
967 } */ *ap = v;
968 struct vnode *vp = ap->a_vp;
969 int error;
970
971 LFS_CLR_UINO(VTOI(vp), IN_ALLMOD);
972 if ((error = ufs_reclaim(vp, ap->a_p)))
973 return (error);
974 pool_put(&lfs_inode_pool, vp->v_data);
975 vp->v_data = NULL;
976 return (0);
977 }
978
979 int
980 lfs_getpages(void *v)
981 {
982 struct vop_getpages_args /* {
983 struct vnode *a_vp;
984 voff_t a_offset;
985 struct vm_page **a_m;
986 int *a_count;
987 int a_centeridx;
988 vm_prot_t a_access_type;
989 int a_advice;
990 int a_flags;
991 } */ *ap = v;
992
993 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
994 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
995 }
996 return genfs_compat_getpages(v);
997 }
998
999 int
1000 lfs_putpages(void *v)
1001 {
1002 int error;
1003
1004 error = genfs_putpages(v);
1005 return error;
1006 }
1007