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