lfs_vnops.c revision 1.74 1 /* $NetBSD: lfs_vnops.c,v 1.74 2002/12/28 20:08:36 christos 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.74 2002/12/28 20:08:36 christos 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 struct vop_inactive_args /* {
337 struct vnode *a_vp;
338 struct proc *a_p;
339 } */ *ap = v;
340
341 KASSERT(!(ap->a_vp->v_flag & VDIROP));
342 KASSERT(!(VTOI(ap->a_vp)->i_flag & IN_ADIROP));
343
344 return ufs_inactive(ap);
345 }
346
347 /*
348 * These macros are used to bracket UFS directory ops, so that we can
349 * identify all the pages touched during directory ops which need to
350 * be ordered and flushed atomically, so that they may be recovered.
351 */
352 /*
353 * XXX KS - Because we have to mark nodes VDIROP in order to prevent
354 * the cache from reclaiming them while a dirop is in progress, we must
355 * also manage the number of nodes so marked (otherwise we can run out).
356 * We do this by setting lfs_dirvcount to the number of marked vnodes; it
357 * is decremented during segment write, when VDIROP is taken off.
358 */
359 #define SET_DIROP(vp) SET_DIROP2((vp), NULL)
360 #define SET_DIROP2(vp, vp2) lfs_set_dirop((vp), (vp2))
361 static int lfs_set_dirop(struct vnode *, struct vnode *);
362 extern int lfs_dirvcount;
363
364 #define NRESERVE(fs) (btofsb(fs, (NIADDR + 3 + (2 * NIADDR + 3)) << fs->lfs_bshift))
365
366 static int
367 lfs_set_dirop(struct vnode *vp, struct vnode *vp2)
368 {
369 struct lfs *fs;
370 int error;
371
372 KASSERT(VOP_ISLOCKED(vp));
373 KASSERT(vp2 == NULL || VOP_ISLOCKED(vp2));
374
375 fs = VTOI(vp)->i_lfs;
376 /*
377 * We might need one directory block plus supporting indirect blocks,
378 * plus an inode block and ifile page for the new vnode.
379 */
380 if ((error = lfs_reserve(fs, vp, vp2, NRESERVE(fs))) != 0)
381 return (error);
382
383 if (fs->lfs_dirops == 0)
384 lfs_check(vp, LFS_UNUSED_LBN, 0);
385 while (fs->lfs_writer || lfs_dirvcount > LFS_MAXDIROP) {
386 if (fs->lfs_writer)
387 tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_sdirop", 0);
388 if (lfs_dirvcount > LFS_MAXDIROP && fs->lfs_dirops == 0) {
389 ++fs->lfs_writer;
390 lfs_flush(fs, 0);
391 if (--fs->lfs_writer == 0)
392 wakeup(&fs->lfs_dirops);
393 }
394
395 if (lfs_dirvcount > LFS_MAXDIROP) {
396 #ifdef DEBUG_LFS
397 printf("lfs_set_dirop: sleeping with dirops=%d, "
398 "dirvcount=%d\n", fs->lfs_dirops,
399 lfs_dirvcount);
400 #endif
401 if ((error = tsleep(&lfs_dirvcount, PCATCH|PUSER,
402 "lfs_maxdirop", 0)) != 0) {
403 goto unreserve;
404 }
405 }
406 }
407 ++fs->lfs_dirops;
408 fs->lfs_doifile = 1;
409
410 /* Hold a reference so SET_ENDOP will be happy */
411 lfs_vref(vp);
412 if (vp2 != NULL)
413 lfs_vref(vp2);
414
415 return 0;
416
417 unreserve:
418 lfs_reserve(fs, vp, vp2, -NRESERVE(fs));
419 return error;
420 }
421
422 #define SET_ENDOP(fs, vp, str) SET_ENDOP2((fs), (vp), NULL, (str))
423 #define SET_ENDOP2(fs, vp, vp2, str) { \
424 --(fs)->lfs_dirops; \
425 if (!(fs)->lfs_dirops) { \
426 if ((fs)->lfs_nadirop) { \
427 panic("SET_ENDOP: %s: no dirops but nadirop=%d", \
428 (str), (fs)->lfs_nadirop); \
429 } \
430 wakeup(&(fs)->lfs_writer); \
431 lfs_check((vp),LFS_UNUSED_LBN,0); \
432 } \
433 lfs_reserve((fs), vp, vp2, -NRESERVE(fs)); /* XXX */ \
434 lfs_vunref(vp); \
435 if (vp2 != NULL) \
436 lfs_vunref(vp2); \
437 }
438
439 #define MARK_VNODE(dvp) do { \
440 if (!((dvp)->v_flag & VDIROP)) { \
441 (void)lfs_vref(dvp); \
442 ++lfs_dirvcount; \
443 } \
444 (dvp)->v_flag |= VDIROP; \
445 if (!(VTOI(dvp)->i_flag & IN_ADIROP)) { \
446 ++VTOI(dvp)->i_lfs->lfs_nadirop; \
447 } \
448 VTOI(dvp)->i_flag |= IN_ADIROP; \
449 } while (0)
450
451 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
452
453 void lfs_unmark_vnode(struct vnode *vp)
454 {
455 struct inode *ip;
456
457 ip = VTOI(vp);
458
459 if (ip->i_flag & IN_ADIROP)
460 --ip->i_lfs->lfs_nadirop;
461 ip->i_flag &= ~IN_ADIROP;
462 }
463
464 int
465 lfs_symlink(void *v)
466 {
467 struct vop_symlink_args /* {
468 struct vnode *a_dvp;
469 struct vnode **a_vpp;
470 struct componentname *a_cnp;
471 struct vattr *a_vap;
472 char *a_target;
473 } */ *ap = v;
474 int error;
475
476 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
477 vput(ap->a_dvp);
478 return error;
479 }
480 MARK_VNODE(ap->a_dvp);
481 error = ufs_symlink(ap);
482 UNMARK_VNODE(ap->a_dvp);
483 if (*(ap->a_vpp))
484 UNMARK_VNODE(*(ap->a_vpp));
485 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"symlink");
486 return (error);
487 }
488
489 int
490 lfs_mknod(void *v)
491 {
492 struct vop_mknod_args /* {
493 struct vnode *a_dvp;
494 struct vnode **a_vpp;
495 struct componentname *a_cnp;
496 struct vattr *a_vap;
497 } */ *ap = v;
498 struct vattr *vap = ap->a_vap;
499 struct vnode **vpp = ap->a_vpp;
500 struct inode *ip;
501 int error;
502 struct mount *mp;
503 ino_t ino;
504
505 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
506 vput(ap->a_dvp);
507 return error;
508 }
509 MARK_VNODE(ap->a_dvp);
510 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
511 ap->a_dvp, vpp, ap->a_cnp);
512 UNMARK_VNODE(ap->a_dvp);
513 if (*(ap->a_vpp))
514 UNMARK_VNODE(*(ap->a_vpp));
515
516 /* Either way we're done with the dirop at this point */
517 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mknod");
518
519 if (error)
520 return (error);
521
522 ip = VTOI(*vpp);
523 mp = (*vpp)->v_mount;
524 ino = ip->i_number;
525 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
526 if (vap->va_rdev != VNOVAL) {
527 /*
528 * Want to be able to use this to make badblock
529 * inodes, so don't truncate the dev number.
530 */
531 #if 0
532 ip->i_ffs_rdev = ufs_rw32(vap->va_rdev,
533 UFS_MPNEEDSWAP((*vpp)->v_mount));
534 #else
535 ip->i_ffs_rdev = vap->va_rdev;
536 #endif
537 }
538 /*
539 * Call fsync to write the vnode so that we don't have to deal with
540 * flushing it when it's marked VDIROP|VXLOCK.
541 *
542 * XXX KS - If we can't flush we also can't call vgone(), so must
543 * return. But, that leaves this vnode in limbo, also not good.
544 * Can this ever happen (barring hardware failure)?
545 */
546 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0, curproc)) != 0) {
547 printf("Couldn't fsync in mknod (ino %d)---what do I do?\n",
548 VTOI(*vpp)->i_number);
549 return (error);
550 }
551 /*
552 * Remove vnode so that it will be reloaded by VFS_VGET and
553 * checked to see if it is an alias of an existing entry in
554 * the inode cache.
555 */
556 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
557 VOP_UNLOCK(*vpp, 0);
558 lfs_vunref(*vpp);
559 (*vpp)->v_type = VNON;
560 vgone(*vpp);
561 error = VFS_VGET(mp, ino, vpp);
562 if (error != 0) {
563 *vpp = NULL;
564 return (error);
565 }
566 return (0);
567 }
568
569 int
570 lfs_create(void *v)
571 {
572 struct vop_create_args /* {
573 struct vnode *a_dvp;
574 struct vnode **a_vpp;
575 struct componentname *a_cnp;
576 struct vattr *a_vap;
577 } */ *ap = v;
578 int error;
579
580 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
581 vput(ap->a_dvp);
582 return error;
583 }
584 MARK_VNODE(ap->a_dvp);
585 error = ufs_create(ap);
586 UNMARK_VNODE(ap->a_dvp);
587 if (*(ap->a_vpp))
588 UNMARK_VNODE(*(ap->a_vpp));
589 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"create");
590 return (error);
591 }
592
593 int
594 lfs_whiteout(void *v)
595 {
596 struct vop_whiteout_args /* {
597 struct vnode *a_dvp;
598 struct componentname *a_cnp;
599 int a_flags;
600 } */ *ap = v;
601 int error;
602
603 if ((error = SET_DIROP(ap->a_dvp)) != 0)
604 /* XXX no unlock here? */
605 return error;
606 MARK_VNODE(ap->a_dvp);
607 error = ufs_whiteout(ap);
608 UNMARK_VNODE(ap->a_dvp);
609 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"whiteout");
610 return (error);
611 }
612
613 int
614 lfs_mkdir(void *v)
615 {
616 struct vop_mkdir_args /* {
617 struct vnode *a_dvp;
618 struct vnode **a_vpp;
619 struct componentname *a_cnp;
620 struct vattr *a_vap;
621 } */ *ap = v;
622 int error;
623
624 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
625 vput(ap->a_dvp);
626 return error;
627 }
628 MARK_VNODE(ap->a_dvp);
629 error = ufs_mkdir(ap);
630 UNMARK_VNODE(ap->a_dvp);
631 if (*(ap->a_vpp))
632 UNMARK_VNODE(*(ap->a_vpp));
633 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"mkdir");
634 return (error);
635 }
636
637 int
638 lfs_remove(void *v)
639 {
640 struct vop_remove_args /* {
641 struct vnode *a_dvp;
642 struct vnode *a_vp;
643 struct componentname *a_cnp;
644 } */ *ap = v;
645 struct vnode *dvp, *vp;
646 int error;
647
648 dvp = ap->a_dvp;
649 vp = ap->a_vp;
650 if ((error = SET_DIROP2(dvp, vp)) != 0) {
651 if (dvp == vp)
652 vrele(vp);
653 else
654 vput(vp);
655 vput(dvp);
656 return error;
657 }
658 MARK_VNODE(dvp);
659 MARK_VNODE(vp);
660 error = ufs_remove(ap);
661 UNMARK_VNODE(dvp);
662 UNMARK_VNODE(vp);
663
664 /*
665 * If ufs_remove failed, vp doesn't need to be VDIROP any more.
666 * If it succeeded, we can go ahead and wipe out vp, since
667 * its loss won't appear on disk until checkpoint, and by then
668 * dvp will have been written, completing the dirop.
669 */
670 --lfs_dirvcount;
671 vp->v_flag &= ~VDIROP;
672 wakeup(&lfs_dirvcount);
673 vrele(vp);
674
675 SET_ENDOP2(VTOI(dvp)->i_lfs, dvp, vp, "remove");
676 return (error);
677 }
678
679 int
680 lfs_rmdir(void *v)
681 {
682 struct vop_rmdir_args /* {
683 struct vnodeop_desc *a_desc;
684 struct vnode *a_dvp;
685 struct vnode *a_vp;
686 struct componentname *a_cnp;
687 } */ *ap = v;
688 int error;
689
690 if ((error = SET_DIROP2(ap->a_dvp, ap->a_vp)) != 0) {
691 vrele(ap->a_dvp);
692 if (ap->a_vp != ap->a_dvp)
693 VOP_UNLOCK(ap->a_dvp, 0);
694 vput(ap->a_vp);
695 return error;
696 }
697 MARK_VNODE(ap->a_dvp);
698 MARK_VNODE(ap->a_vp);
699 error = ufs_rmdir(ap);
700 UNMARK_VNODE(ap->a_dvp);
701 UNMARK_VNODE(ap->a_vp);
702
703 /*
704 * If ufs_rmdir failed, vp doesn't need to be VDIROP any more.
705 * If it succeeded, we can go ahead and wipe out vp, since
706 * its loss won't appear on disk until checkpoint, and by then
707 * dvp will have been written, completing the dirop.
708 */
709 --lfs_dirvcount;
710 ap->a_vp->v_flag &= ~VDIROP;
711 wakeup(&lfs_dirvcount);
712 vrele(ap->a_vp);
713
714 SET_ENDOP2(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
715 return (error);
716 }
717
718 int
719 lfs_link(void *v)
720 {
721 struct vop_link_args /* {
722 struct vnode *a_dvp;
723 struct vnode *a_vp;
724 struct componentname *a_cnp;
725 } */ *ap = v;
726 int error;
727
728 if ((error = SET_DIROP(ap->a_dvp)) != 0) {
729 vput(ap->a_dvp);
730 return error;
731 }
732 MARK_VNODE(ap->a_dvp);
733 error = ufs_link(ap);
734 UNMARK_VNODE(ap->a_dvp);
735 SET_ENDOP(VTOI(ap->a_dvp)->i_lfs,ap->a_dvp,"link");
736 return (error);
737 }
738
739 int
740 lfs_rename(void *v)
741 {
742 struct vop_rename_args /* {
743 struct vnode *a_fdvp;
744 struct vnode *a_fvp;
745 struct componentname *a_fcnp;
746 struct vnode *a_tdvp;
747 struct vnode *a_tvp;
748 struct componentname *a_tcnp;
749 } */ *ap = v;
750 struct vnode *tvp, *fvp, *tdvp, *fdvp;
751 int error;
752 struct lfs *fs;
753
754 fs = VTOI(ap->a_fdvp)->i_lfs;
755 tvp = ap->a_tvp;
756 tdvp = ap->a_tdvp;
757 fvp = ap->a_fvp;
758 fdvp = ap->a_fdvp;
759
760 /*
761 * Check for cross-device rename.
762 * If it is, we don't want to set dirops, just error out.
763 * (In particular note that MARK_VNODE(tdvp) will DTWT on
764 * a cross-device rename.)
765 *
766 * Copied from ufs_rename.
767 */
768 if ((fvp->v_mount != tdvp->v_mount) ||
769 (tvp && (fvp->v_mount != tvp->v_mount))) {
770 error = EXDEV;
771 goto errout;
772 }
773 if ((error = SET_DIROP2(tdvp, tvp)) != 0)
774 goto errout;
775 MARK_VNODE(fdvp);
776 MARK_VNODE(tdvp);
777 MARK_VNODE(fvp);
778 if (tvp) {
779 MARK_VNODE(tvp);
780 }
781 error = ufs_rename(ap);
782 UNMARK_VNODE(fdvp);
783 UNMARK_VNODE(tdvp);
784 UNMARK_VNODE(fvp);
785 if (tvp) {
786 UNMARK_VNODE(tvp);
787 }
788 SET_ENDOP2(fs, tdvp, tvp, "rename");
789 return (error);
790
791 errout:
792 VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
793 if (tdvp == tvp)
794 vrele(tdvp);
795 else
796 vput(tdvp);
797 if (tvp)
798 vput(tvp);
799 VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
800 vrele(fdvp);
801 vrele(fvp);
802 return (error);
803 }
804
805 /* XXX hack to avoid calling ITIMES in getattr */
806 int
807 lfs_getattr(void *v)
808 {
809 struct vop_getattr_args /* {
810 struct vnode *a_vp;
811 struct vattr *a_vap;
812 struct ucred *a_cred;
813 struct proc *a_p;
814 } */ *ap = v;
815 struct vnode *vp = ap->a_vp;
816 struct inode *ip = VTOI(vp);
817 struct vattr *vap = ap->a_vap;
818 struct lfs *fs = ip->i_lfs;
819 /*
820 * Copy from inode table
821 */
822 vap->va_fsid = ip->i_dev;
823 vap->va_fileid = ip->i_number;
824 vap->va_mode = ip->i_ffs_mode & ~IFMT;
825 vap->va_nlink = ip->i_ffs_nlink;
826 vap->va_uid = ip->i_ffs_uid;
827 vap->va_gid = ip->i_ffs_gid;
828 vap->va_rdev = (dev_t)ip->i_ffs_rdev;
829 vap->va_size = vp->v_size;
830 vap->va_atime.tv_sec = ip->i_ffs_atime;
831 vap->va_atime.tv_nsec = ip->i_ffs_atimensec;
832 vap->va_mtime.tv_sec = ip->i_ffs_mtime;
833 vap->va_mtime.tv_nsec = ip->i_ffs_mtimensec;
834 vap->va_ctime.tv_sec = ip->i_ffs_ctime;
835 vap->va_ctime.tv_nsec = ip->i_ffs_ctimensec;
836 vap->va_flags = ip->i_ffs_flags;
837 vap->va_gen = ip->i_ffs_gen;
838 /* this doesn't belong here */
839 if (vp->v_type == VBLK)
840 vap->va_blocksize = BLKDEV_IOSIZE;
841 else if (vp->v_type == VCHR)
842 vap->va_blocksize = MAXBSIZE;
843 else
844 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
845 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_ffs_blocks);
846 vap->va_type = vp->v_type;
847 vap->va_filerev = ip->i_modrev;
848 return (0);
849 }
850
851 /*
852 * Check to make sure the inode blocks won't choke the buffer
853 * cache, then call ufs_setattr as usual.
854 */
855 int
856 lfs_setattr(void *v)
857 {
858 struct vop_getattr_args /* {
859 struct vnode *a_vp;
860 struct vattr *a_vap;
861 struct ucred *a_cred;
862 struct proc *a_p;
863 } */ *ap = v;
864 struct vnode *vp = ap->a_vp;
865
866 lfs_check(vp, LFS_UNUSED_LBN, 0);
867 return ufs_setattr(v);
868 }
869
870 /*
871 * Close called
872 *
873 * XXX -- we were using ufs_close, but since it updates the
874 * times on the inode, we might need to bump the uinodes
875 * count.
876 */
877 /* ARGSUSED */
878 int
879 lfs_close(void *v)
880 {
881 struct vop_close_args /* {
882 struct vnode *a_vp;
883 int a_fflag;
884 struct ucred *a_cred;
885 struct proc *a_p;
886 } */ *ap = v;
887 struct vnode *vp = ap->a_vp;
888 struct inode *ip = VTOI(vp);
889 struct timespec ts;
890
891 if (vp->v_usecount > 1) {
892 TIMEVAL_TO_TIMESPEC(&time, &ts);
893 LFS_ITIMES(ip, &ts, &ts, &ts);
894 }
895 return (0);
896 }
897
898 /*
899 * Close wrapper for special devices.
900 *
901 * Update the times on the inode then do device close.
902 */
903 int
904 lfsspec_close(void *v)
905 {
906 struct vop_close_args /* {
907 struct vnode *a_vp;
908 int a_fflag;
909 struct ucred *a_cred;
910 struct proc *a_p;
911 } */ *ap = v;
912 struct vnode *vp;
913 struct inode *ip;
914 struct timespec ts;
915
916 vp = ap->a_vp;
917 ip = VTOI(vp);
918 if (vp->v_usecount > 1) {
919 TIMEVAL_TO_TIMESPEC(&time, &ts);
920 LFS_ITIMES(ip, &ts, &ts, &ts);
921 }
922 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
923 }
924
925 /*
926 * Close wrapper for fifo's.
927 *
928 * Update the times on the inode then do device close.
929 */
930 int
931 lfsfifo_close(void *v)
932 {
933 struct vop_close_args /* {
934 struct vnode *a_vp;
935 int a_fflag;
936 struct ucred *a_cred;
937 struct proc *a_p;
938 } */ *ap = v;
939 struct vnode *vp;
940 struct inode *ip;
941 struct timespec ts;
942
943 vp = ap->a_vp;
944 ip = VTOI(vp);
945 if (ap->a_vp->v_usecount > 1) {
946 TIMEVAL_TO_TIMESPEC(&time, &ts);
947 LFS_ITIMES(ip, &ts, &ts, &ts);
948 }
949 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
950 }
951
952 /*
953 * Reclaim an inode so that it can be used for other purposes.
954 */
955 int lfs_no_inactive = 0;
956
957 int
958 lfs_reclaim(void *v)
959 {
960 struct vop_reclaim_args /* {
961 struct vnode *a_vp;
962 struct proc *a_p;
963 } */ *ap = v;
964 struct vnode *vp = ap->a_vp;
965 int error;
966
967 LFS_CLR_UINO(VTOI(vp), IN_ALLMOD);
968 if ((error = ufs_reclaim(vp, ap->a_p)))
969 return (error);
970 pool_put(&lfs_inode_pool, vp->v_data);
971 vp->v_data = NULL;
972 return (0);
973 }
974
975 int
976 lfs_getpages(void *v)
977 {
978 struct vop_getpages_args /* {
979 struct vnode *a_vp;
980 voff_t a_offset;
981 struct vm_page **a_m;
982 int *a_count;
983 int a_centeridx;
984 vm_prot_t a_access_type;
985 int a_advice;
986 int a_flags;
987 } */ *ap = v;
988
989 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
990 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
991 }
992 return genfs_compat_getpages(v);
993 }
994
995 int
996 lfs_putpages(void *v)
997 {
998 int error;
999
1000 error = genfs_putpages(v);
1001 return error;
1002 }
1003