1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright 1998, 2000 Marshall Kirk McKusick.
5 * Copyright 2009, 2010 Jeffrey W. Roberson <[email protected]>
6 * All rights reserved.
7 *
8 * The soft updates code is derived from the appendix of a University
9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
10 * "Soft Updates: A Solution to the Metadata Update Problem in File
11 * Systems", CSE-TR-254-95, August 1995).
12 *
13 * Further information about soft updates can be obtained from:
14 *
15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/
16 * 1614 Oxford Street [email protected]
17 * Berkeley, CA 94709-1608 +1-510-843-9542
18 * USA
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00
42 */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include "opt_ffs.h"
48 #include "opt_quota.h"
49 #include "opt_ddb.h"
50
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/kdb.h>
57 #include <sys/kthread.h>
58 #include <sys/ktr.h>
59 #include <sys/limits.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
65 #include <sys/priv.h>
66 #include <sys/proc.h>
67 #include <sys/racct.h>
68 #include <sys/rwlock.h>
69 #include <sys/stat.h>
70 #include <sys/sysctl.h>
71 #include <sys/syslog.h>
72 #include <sys/vnode.h>
73 #include <sys/conf.h>
74
75 #include <ufs/ufs/dir.h>
76 #include <ufs/ufs/extattr.h>
77 #include <ufs/ufs/quota.h>
78 #include <ufs/ufs/inode.h>
79 #include <ufs/ufs/ufsmount.h>
80 #include <ufs/ffs/fs.h>
81 #include <ufs/ffs/softdep.h>
82 #include <ufs/ffs/ffs_extern.h>
83 #include <ufs/ufs/ufs_extern.h>
84
85 #include <vm/vm.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_object.h>
88
89 #include <geom/geom.h>
90 #include <geom/geom_vfs.h>
91
92 #include <ddb/ddb.h>
93
94 #define KTR_SUJ 0 /* Define to KTR_SPARE. */
95
96 #ifndef SOFTUPDATES
97
98 int
softdep_flushfiles(oldmnt,flags,td)99 softdep_flushfiles(oldmnt, flags, td)
100 struct mount *oldmnt;
101 int flags;
102 struct thread *td;
103 {
104
105 panic("softdep_flushfiles called");
106 }
107
108 int
softdep_mount(devvp,mp,fs,cred)109 softdep_mount(devvp, mp, fs, cred)
110 struct vnode *devvp;
111 struct mount *mp;
112 struct fs *fs;
113 struct ucred *cred;
114 {
115
116 return (0);
117 }
118
119 void
softdep_initialize()120 softdep_initialize()
121 {
122
123 return;
124 }
125
126 void
softdep_uninitialize()127 softdep_uninitialize()
128 {
129
130 return;
131 }
132
133 void
softdep_unmount(mp)134 softdep_unmount(mp)
135 struct mount *mp;
136 {
137
138 panic("softdep_unmount called");
139 }
140
141 void
softdep_setup_sbupdate(ump,fs,bp)142 softdep_setup_sbupdate(ump, fs, bp)
143 struct ufsmount *ump;
144 struct fs *fs;
145 struct buf *bp;
146 {
147
148 panic("softdep_setup_sbupdate called");
149 }
150
151 void
softdep_setup_inomapdep(bp,ip,newinum,mode)152 softdep_setup_inomapdep(bp, ip, newinum, mode)
153 struct buf *bp;
154 struct inode *ip;
155 ino_t newinum;
156 int mode;
157 {
158
159 panic("softdep_setup_inomapdep called");
160 }
161
162 void
softdep_setup_blkmapdep(bp,mp,newblkno,frags,oldfrags)163 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
164 struct buf *bp;
165 struct mount *mp;
166 ufs2_daddr_t newblkno;
167 int frags;
168 int oldfrags;
169 {
170
171 panic("softdep_setup_blkmapdep called");
172 }
173
174 void
softdep_setup_allocdirect(ip,lbn,newblkno,oldblkno,newsize,oldsize,bp)175 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
176 struct inode *ip;
177 ufs_lbn_t lbn;
178 ufs2_daddr_t newblkno;
179 ufs2_daddr_t oldblkno;
180 long newsize;
181 long oldsize;
182 struct buf *bp;
183 {
184
185 panic("softdep_setup_allocdirect called");
186 }
187
188 void
softdep_setup_allocext(ip,lbn,newblkno,oldblkno,newsize,oldsize,bp)189 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
190 struct inode *ip;
191 ufs_lbn_t lbn;
192 ufs2_daddr_t newblkno;
193 ufs2_daddr_t oldblkno;
194 long newsize;
195 long oldsize;
196 struct buf *bp;
197 {
198
199 panic("softdep_setup_allocext called");
200 }
201
202 void
softdep_setup_allocindir_page(ip,lbn,bp,ptrno,newblkno,oldblkno,nbp)203 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
204 struct inode *ip;
205 ufs_lbn_t lbn;
206 struct buf *bp;
207 int ptrno;
208 ufs2_daddr_t newblkno;
209 ufs2_daddr_t oldblkno;
210 struct buf *nbp;
211 {
212
213 panic("softdep_setup_allocindir_page called");
214 }
215
216 void
softdep_setup_allocindir_meta(nbp,ip,bp,ptrno,newblkno)217 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
218 struct buf *nbp;
219 struct inode *ip;
220 struct buf *bp;
221 int ptrno;
222 ufs2_daddr_t newblkno;
223 {
224
225 panic("softdep_setup_allocindir_meta called");
226 }
227
228 void
softdep_journal_freeblocks(ip,cred,length,flags)229 softdep_journal_freeblocks(ip, cred, length, flags)
230 struct inode *ip;
231 struct ucred *cred;
232 off_t length;
233 int flags;
234 {
235
236 panic("softdep_journal_freeblocks called");
237 }
238
239 void
softdep_journal_fsync(ip)240 softdep_journal_fsync(ip)
241 struct inode *ip;
242 {
243
244 panic("softdep_journal_fsync called");
245 }
246
247 void
softdep_setup_freeblocks(ip,length,flags)248 softdep_setup_freeblocks(ip, length, flags)
249 struct inode *ip;
250 off_t length;
251 int flags;
252 {
253
254 panic("softdep_setup_freeblocks called");
255 }
256
257 void
softdep_freefile(pvp,ino,mode)258 softdep_freefile(pvp, ino, mode)
259 struct vnode *pvp;
260 ino_t ino;
261 int mode;
262 {
263
264 panic("softdep_freefile called");
265 }
266
267 int
softdep_setup_directory_add(bp,dp,diroffset,newinum,newdirbp,isnewblk)268 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
269 struct buf *bp;
270 struct inode *dp;
271 off_t diroffset;
272 ino_t newinum;
273 struct buf *newdirbp;
274 int isnewblk;
275 {
276
277 panic("softdep_setup_directory_add called");
278 }
279
280 void
softdep_change_directoryentry_offset(bp,dp,base,oldloc,newloc,entrysize)281 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
282 struct buf *bp;
283 struct inode *dp;
284 caddr_t base;
285 caddr_t oldloc;
286 caddr_t newloc;
287 int entrysize;
288 {
289
290 panic("softdep_change_directoryentry_offset called");
291 }
292
293 void
softdep_setup_remove(bp,dp,ip,isrmdir)294 softdep_setup_remove(bp, dp, ip, isrmdir)
295 struct buf *bp;
296 struct inode *dp;
297 struct inode *ip;
298 int isrmdir;
299 {
300
301 panic("softdep_setup_remove called");
302 }
303
304 void
softdep_setup_directory_change(bp,dp,ip,newinum,isrmdir)305 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
306 struct buf *bp;
307 struct inode *dp;
308 struct inode *ip;
309 ino_t newinum;
310 int isrmdir;
311 {
312
313 panic("softdep_setup_directory_change called");
314 }
315
316 void
softdep_setup_blkfree(mp,bp,blkno,frags,wkhd)317 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
318 struct mount *mp;
319 struct buf *bp;
320 ufs2_daddr_t blkno;
321 int frags;
322 struct workhead *wkhd;
323 {
324
325 panic("%s called", __FUNCTION__);
326 }
327
328 void
softdep_setup_inofree(mp,bp,ino,wkhd)329 softdep_setup_inofree(mp, bp, ino, wkhd)
330 struct mount *mp;
331 struct buf *bp;
332 ino_t ino;
333 struct workhead *wkhd;
334 {
335
336 panic("%s called", __FUNCTION__);
337 }
338
339 void
softdep_setup_unlink(dp,ip)340 softdep_setup_unlink(dp, ip)
341 struct inode *dp;
342 struct inode *ip;
343 {
344
345 panic("%s called", __FUNCTION__);
346 }
347
348 void
softdep_setup_link(dp,ip)349 softdep_setup_link(dp, ip)
350 struct inode *dp;
351 struct inode *ip;
352 {
353
354 panic("%s called", __FUNCTION__);
355 }
356
357 void
softdep_revert_link(dp,ip)358 softdep_revert_link(dp, ip)
359 struct inode *dp;
360 struct inode *ip;
361 {
362
363 panic("%s called", __FUNCTION__);
364 }
365
366 void
softdep_setup_rmdir(dp,ip)367 softdep_setup_rmdir(dp, ip)
368 struct inode *dp;
369 struct inode *ip;
370 {
371
372 panic("%s called", __FUNCTION__);
373 }
374
375 void
softdep_revert_rmdir(dp,ip)376 softdep_revert_rmdir(dp, ip)
377 struct inode *dp;
378 struct inode *ip;
379 {
380
381 panic("%s called", __FUNCTION__);
382 }
383
384 void
softdep_setup_create(dp,ip)385 softdep_setup_create(dp, ip)
386 struct inode *dp;
387 struct inode *ip;
388 {
389
390 panic("%s called", __FUNCTION__);
391 }
392
393 void
softdep_revert_create(dp,ip)394 softdep_revert_create(dp, ip)
395 struct inode *dp;
396 struct inode *ip;
397 {
398
399 panic("%s called", __FUNCTION__);
400 }
401
402 void
softdep_setup_mkdir(dp,ip)403 softdep_setup_mkdir(dp, ip)
404 struct inode *dp;
405 struct inode *ip;
406 {
407
408 panic("%s called", __FUNCTION__);
409 }
410
411 void
softdep_revert_mkdir(dp,ip)412 softdep_revert_mkdir(dp, ip)
413 struct inode *dp;
414 struct inode *ip;
415 {
416
417 panic("%s called", __FUNCTION__);
418 }
419
420 void
softdep_setup_dotdot_link(dp,ip)421 softdep_setup_dotdot_link(dp, ip)
422 struct inode *dp;
423 struct inode *ip;
424 {
425
426 panic("%s called", __FUNCTION__);
427 }
428
429 int
softdep_prealloc(vp,waitok)430 softdep_prealloc(vp, waitok)
431 struct vnode *vp;
432 int waitok;
433 {
434
435 panic("%s called", __FUNCTION__);
436 }
437
438 int
softdep_journal_lookup(mp,vpp)439 softdep_journal_lookup(mp, vpp)
440 struct mount *mp;
441 struct vnode **vpp;
442 {
443
444 return (ENOENT);
445 }
446
447 void
softdep_change_linkcnt(ip)448 softdep_change_linkcnt(ip)
449 struct inode *ip;
450 {
451
452 panic("softdep_change_linkcnt called");
453 }
454
455 void
softdep_load_inodeblock(ip)456 softdep_load_inodeblock(ip)
457 struct inode *ip;
458 {
459
460 panic("softdep_load_inodeblock called");
461 }
462
463 void
softdep_update_inodeblock(ip,bp,waitfor)464 softdep_update_inodeblock(ip, bp, waitfor)
465 struct inode *ip;
466 struct buf *bp;
467 int waitfor;
468 {
469
470 panic("softdep_update_inodeblock called");
471 }
472
473 int
softdep_fsync(vp)474 softdep_fsync(vp)
475 struct vnode *vp; /* the "in_core" copy of the inode */
476 {
477
478 return (0);
479 }
480
481 void
softdep_fsync_mountdev(vp)482 softdep_fsync_mountdev(vp)
483 struct vnode *vp;
484 {
485
486 return;
487 }
488
489 int
softdep_flushworklist(oldmnt,countp,td)490 softdep_flushworklist(oldmnt, countp, td)
491 struct mount *oldmnt;
492 int *countp;
493 struct thread *td;
494 {
495
496 *countp = 0;
497 return (0);
498 }
499
500 int
softdep_sync_metadata(struct vnode * vp)501 softdep_sync_metadata(struct vnode *vp)
502 {
503
504 panic("softdep_sync_metadata called");
505 }
506
507 int
softdep_sync_buf(struct vnode * vp,struct buf * bp,int waitfor)508 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
509 {
510
511 panic("softdep_sync_buf called");
512 }
513
514 int
softdep_slowdown(vp)515 softdep_slowdown(vp)
516 struct vnode *vp;
517 {
518
519 panic("softdep_slowdown called");
520 }
521
522 int
softdep_request_cleanup(fs,vp,cred,resource)523 softdep_request_cleanup(fs, vp, cred, resource)
524 struct fs *fs;
525 struct vnode *vp;
526 struct ucred *cred;
527 int resource;
528 {
529
530 return (0);
531 }
532
533 int
softdep_check_suspend(struct mount * mp,struct vnode * devvp,int softdep_depcnt,int softdep_accdepcnt,int secondary_writes,int secondary_accwrites)534 softdep_check_suspend(struct mount *mp,
535 struct vnode *devvp,
536 int softdep_depcnt,
537 int softdep_accdepcnt,
538 int secondary_writes,
539 int secondary_accwrites)
540 {
541 struct bufobj *bo;
542 int error;
543
544 (void) softdep_depcnt,
545 (void) softdep_accdepcnt;
546
547 bo = &devvp->v_bufobj;
548 ASSERT_BO_WLOCKED(bo);
549
550 MNT_ILOCK(mp);
551 while (mp->mnt_secondary_writes != 0) {
552 BO_UNLOCK(bo);
553 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
554 (PUSER - 1) | PDROP, "secwr", 0);
555 BO_LOCK(bo);
556 MNT_ILOCK(mp);
557 }
558
559 /*
560 * Reasons for needing more work before suspend:
561 * - Dirty buffers on devvp.
562 * - Secondary writes occurred after start of vnode sync loop
563 */
564 error = 0;
565 if (bo->bo_numoutput > 0 ||
566 bo->bo_dirty.bv_cnt > 0 ||
567 secondary_writes != 0 ||
568 mp->mnt_secondary_writes != 0 ||
569 secondary_accwrites != mp->mnt_secondary_accwrites)
570 error = EAGAIN;
571 BO_UNLOCK(bo);
572 return (error);
573 }
574
575 void
softdep_get_depcounts(struct mount * mp,int * softdepactivep,int * softdepactiveaccp)576 softdep_get_depcounts(struct mount *mp,
577 int *softdepactivep,
578 int *softdepactiveaccp)
579 {
580 (void) mp;
581 *softdepactivep = 0;
582 *softdepactiveaccp = 0;
583 }
584
585 void
softdep_buf_append(bp,wkhd)586 softdep_buf_append(bp, wkhd)
587 struct buf *bp;
588 struct workhead *wkhd;
589 {
590
591 panic("softdep_buf_appendwork called");
592 }
593
594 void
softdep_inode_append(ip,cred,wkhd)595 softdep_inode_append(ip, cred, wkhd)
596 struct inode *ip;
597 struct ucred *cred;
598 struct workhead *wkhd;
599 {
600
601 panic("softdep_inode_appendwork called");
602 }
603
604 void
softdep_freework(wkhd)605 softdep_freework(wkhd)
606 struct workhead *wkhd;
607 {
608
609 panic("softdep_freework called");
610 }
611
612 int
softdep_prerename(fdvp,fvp,tdvp,tvp)613 softdep_prerename(fdvp, fvp, tdvp, tvp)
614 struct vnode *fdvp;
615 struct vnode *fvp;
616 struct vnode *tdvp;
617 struct vnode *tvp;
618 {
619
620 panic("softdep_prerename called");
621 }
622
623 int
softdep_prelink(dvp,vp,cnp)624 softdep_prelink(dvp, vp, cnp)
625 struct vnode *dvp;
626 struct vnode *vp;
627 struct componentname *cnp;
628 {
629
630 panic("softdep_prelink called");
631 }
632
633 #else
634
635 FEATURE(softupdates, "FFS soft-updates support");
636
637 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
638 "soft updates stats");
639 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total,
640 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
641 "total dependencies allocated");
642 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse,
643 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
644 "high use dependencies allocated");
645 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current,
646 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
647 "current dependencies allocated");
648 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write,
649 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
650 "current dependencies written");
651
652 unsigned long dep_current[D_LAST + 1];
653 unsigned long dep_highuse[D_LAST + 1];
654 unsigned long dep_total[D_LAST + 1];
655 unsigned long dep_write[D_LAST + 1];
656
657 #define SOFTDEP_TYPE(type, str, long) \
658 static MALLOC_DEFINE(M_ ## type, #str, long); \
659 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \
660 &dep_total[D_ ## type], 0, ""); \
661 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \
662 &dep_current[D_ ## type], 0, ""); \
663 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \
664 &dep_highuse[D_ ## type], 0, ""); \
665 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \
666 &dep_write[D_ ## type], 0, "");
667
668 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
669 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
670 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
671 "Block or frag allocated from cyl group map");
672 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
673 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
674 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
675 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
676 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
677 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
678 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
679 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
680 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
681 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
682 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
683 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
684 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
685 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
686 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
687 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
688 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
689 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
690 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
691 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
692 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
693 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
694 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
695 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
696
697 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
698
699 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
700 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
701 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
702
703 #define M_SOFTDEP_FLAGS (M_WAITOK)
704
705 /*
706 * translate from workitem type to memory type
707 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
708 */
709 static struct malloc_type *memtype[] = {
710 NULL,
711 M_PAGEDEP,
712 M_INODEDEP,
713 M_BMSAFEMAP,
714 M_NEWBLK,
715 M_ALLOCDIRECT,
716 M_INDIRDEP,
717 M_ALLOCINDIR,
718 M_FREEFRAG,
719 M_FREEBLKS,
720 M_FREEFILE,
721 M_DIRADD,
722 M_MKDIR,
723 M_DIRREM,
724 M_NEWDIRBLK,
725 M_FREEWORK,
726 M_FREEDEP,
727 M_JADDREF,
728 M_JREMREF,
729 M_JMVREF,
730 M_JNEWBLK,
731 M_JFREEBLK,
732 M_JFREEFRAG,
733 M_JSEG,
734 M_JSEGDEP,
735 M_SBDEP,
736 M_JTRUNC,
737 M_JFSYNC,
738 M_SENTINEL
739 };
740
741 #define DtoM(type) (memtype[type])
742
743 /*
744 * Names of malloc types.
745 */
746 #define TYPENAME(type) \
747 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
748 memtype[type]->ks_shortdesc : "???")
749 /*
750 * End system adaptation definitions.
751 */
752
753 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
754 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
755
756 /*
757 * Internal function prototypes.
758 */
759 static void check_clear_deps(struct mount *);
760 static void softdep_error(char *, int);
761 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *);
762 static int softdep_process_worklist(struct mount *, int);
763 static int softdep_waitidle(struct mount *, int);
764 static void drain_output(struct vnode *);
765 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
766 static int check_inodedep_free(struct inodedep *);
767 static void clear_remove(struct mount *);
768 static void clear_inodedeps(struct mount *);
769 static void unlinked_inodedep(struct mount *, struct inodedep *);
770 static void clear_unlinked_inodedep(struct inodedep *);
771 static struct inodedep *first_unlinked_inodedep(struct ufsmount *);
772 static int flush_pagedep_deps(struct vnode *, struct mount *,
773 struct diraddhd *, struct buf *);
774 static int free_pagedep(struct pagedep *);
775 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
776 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
777 static int flush_deplist(struct allocdirectlst *, int, int *);
778 static int sync_cgs(struct mount *, int);
779 static int handle_written_filepage(struct pagedep *, struct buf *, int);
780 static int handle_written_sbdep(struct sbdep *, struct buf *);
781 static void initiate_write_sbdep(struct sbdep *);
782 static void diradd_inode_written(struct diradd *, struct inodedep *);
783 static int handle_written_indirdep(struct indirdep *, struct buf *,
784 struct buf**, int);
785 static int handle_written_inodeblock(struct inodedep *, struct buf *, int);
786 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
787 uint8_t *);
788 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
789 static void handle_written_jaddref(struct jaddref *);
790 static void handle_written_jremref(struct jremref *);
791 static void handle_written_jseg(struct jseg *, struct buf *);
792 static void handle_written_jnewblk(struct jnewblk *);
793 static void handle_written_jblkdep(struct jblkdep *);
794 static void handle_written_jfreefrag(struct jfreefrag *);
795 static void complete_jseg(struct jseg *);
796 static void complete_jsegs(struct jseg *);
797 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
798 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
799 static void jremref_write(struct jremref *, struct jseg *, uint8_t *);
800 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
801 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
802 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
803 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
804 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
805 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
806 static inline void inoref_write(struct inoref *, struct jseg *,
807 struct jrefrec *);
808 static void handle_allocdirect_partdone(struct allocdirect *,
809 struct workhead *);
810 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
811 struct workhead *);
812 static void indirdep_complete(struct indirdep *);
813 static int indirblk_lookup(struct mount *, ufs2_daddr_t);
814 static void indirblk_insert(struct freework *);
815 static void indirblk_remove(struct freework *);
816 static void handle_allocindir_partdone(struct allocindir *);
817 static void initiate_write_filepage(struct pagedep *, struct buf *);
818 static void initiate_write_indirdep(struct indirdep*, struct buf *);
819 static void handle_written_mkdir(struct mkdir *, int);
820 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
821 uint8_t *);
822 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
823 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
824 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
825 static void handle_workitem_freefile(struct freefile *);
826 static int handle_workitem_remove(struct dirrem *, int);
827 static struct dirrem *newdirrem(struct buf *, struct inode *,
828 struct inode *, int, struct dirrem **);
829 static struct indirdep *indirdep_lookup(struct mount *, struct inode *,
830 struct buf *);
831 static void cancel_indirdep(struct indirdep *, struct buf *,
832 struct freeblks *);
833 static void free_indirdep(struct indirdep *);
834 static void free_diradd(struct diradd *, struct workhead *);
835 static void merge_diradd(struct inodedep *, struct diradd *);
836 static void complete_diradd(struct diradd *);
837 static struct diradd *diradd_lookup(struct pagedep *, int);
838 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
839 struct jremref *);
840 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
841 struct jremref *);
842 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
843 struct jremref *, struct jremref *);
844 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
845 struct jremref *);
846 static void cancel_allocindir(struct allocindir *, struct buf *bp,
847 struct freeblks *, int);
848 static int setup_trunc_indir(struct freeblks *, struct inode *,
849 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
850 static void complete_trunc_indir(struct freework *);
851 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
852 int);
853 static void complete_mkdir(struct mkdir *);
854 static void free_newdirblk(struct newdirblk *);
855 static void free_jremref(struct jremref *);
856 static void free_jaddref(struct jaddref *);
857 static void free_jsegdep(struct jsegdep *);
858 static void free_jsegs(struct jblocks *);
859 static void rele_jseg(struct jseg *);
860 static void free_jseg(struct jseg *, struct jblocks *);
861 static void free_jnewblk(struct jnewblk *);
862 static void free_jblkdep(struct jblkdep *);
863 static void free_jfreefrag(struct jfreefrag *);
864 static void free_freedep(struct freedep *);
865 static void journal_jremref(struct dirrem *, struct jremref *,
866 struct inodedep *);
867 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
868 static int cancel_jaddref(struct jaddref *, struct inodedep *,
869 struct workhead *);
870 static void cancel_jfreefrag(struct jfreefrag *);
871 static inline void setup_freedirect(struct freeblks *, struct inode *,
872 int, int);
873 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
874 static inline void setup_freeindir(struct freeblks *, struct inode *, int,
875 ufs_lbn_t, int);
876 static inline struct freeblks *newfreeblks(struct mount *, struct inode *);
877 static void freeblks_free(struct ufsmount *, struct freeblks *, int);
878 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
879 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
880 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
881 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
882 int, int);
883 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
884 static int cancel_pagedep(struct pagedep *, struct freeblks *, int);
885 static int deallocate_dependencies(struct buf *, struct freeblks *, int);
886 static void newblk_freefrag(struct newblk*);
887 static void free_newblk(struct newblk *);
888 static void cancel_allocdirect(struct allocdirectlst *,
889 struct allocdirect *, struct freeblks *);
890 static int check_inode_unwritten(struct inodedep *);
891 static int free_inodedep(struct inodedep *);
892 static void freework_freeblock(struct freework *, u_long);
893 static void freework_enqueue(struct freework *);
894 static int handle_workitem_freeblocks(struct freeblks *, int);
895 static int handle_complete_freeblocks(struct freeblks *, int);
896 static void handle_workitem_indirblk(struct freework *);
897 static void handle_written_freework(struct freework *);
898 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
899 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
900 struct workhead *);
901 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
902 struct inodedep *, struct allocindir *, ufs_lbn_t);
903 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
904 ufs2_daddr_t, ufs_lbn_t);
905 static void handle_workitem_freefrag(struct freefrag *);
906 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
907 ufs_lbn_t, u_long);
908 static void allocdirect_merge(struct allocdirectlst *,
909 struct allocdirect *, struct allocdirect *);
910 static struct freefrag *allocindir_merge(struct allocindir *,
911 struct allocindir *);
912 static int bmsafemap_find(struct bmsafemap_hashhead *, int,
913 struct bmsafemap **);
914 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
915 int cg, struct bmsafemap *);
916 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
917 struct newblk **);
918 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
919 static int inodedep_find(struct inodedep_hashhead *, ino_t,
920 struct inodedep **);
921 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
922 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
923 int, struct pagedep **);
924 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
925 struct pagedep **);
926 static void pause_timer(void *);
927 static int request_cleanup(struct mount *, int);
928 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
929 static void schedule_cleanup(struct mount *);
930 static void softdep_ast_cleanup_proc(struct thread *);
931 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
932 static int process_worklist_item(struct mount *, int, int);
933 static void process_removes(struct vnode *);
934 static void process_truncates(struct vnode *);
935 static void jwork_move(struct workhead *, struct workhead *);
936 static void jwork_insert(struct workhead *, struct jsegdep *);
937 static void add_to_worklist(struct worklist *, int);
938 static void wake_worklist(struct worklist *);
939 static void wait_worklist(struct worklist *, char *);
940 static void remove_from_worklist(struct worklist *);
941 static void softdep_flush(void *);
942 static void softdep_flushjournal(struct mount *);
943 static int softdep_speedup(struct ufsmount *);
944 static void worklist_speedup(struct mount *);
945 static int journal_mount(struct mount *, struct fs *, struct ucred *);
946 static void journal_unmount(struct ufsmount *);
947 static int journal_space(struct ufsmount *, int);
948 static void journal_suspend(struct ufsmount *);
949 static int journal_unsuspend(struct ufsmount *ump);
950 static void add_to_journal(struct worklist *);
951 static void remove_from_journal(struct worklist *);
952 static bool softdep_excess_items(struct ufsmount *, int);
953 static void softdep_process_journal(struct mount *, struct worklist *, int);
954 static struct jremref *newjremref(struct dirrem *, struct inode *,
955 struct inode *ip, off_t, nlink_t);
956 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
957 uint16_t);
958 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
959 uint16_t);
960 static inline struct jsegdep *inoref_jseg(struct inoref *);
961 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
962 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
963 ufs2_daddr_t, int);
964 static void adjust_newfreework(struct freeblks *, int);
965 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
966 static void move_newblock_dep(struct jaddref *, struct inodedep *);
967 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
968 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
969 ufs2_daddr_t, long, ufs_lbn_t);
970 static struct freework *newfreework(struct ufsmount *, struct freeblks *,
971 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
972 static int jwait(struct worklist *, int);
973 static struct inodedep *inodedep_lookup_ip(struct inode *);
974 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
975 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
976 static void handle_jwork(struct workhead *);
977 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
978 struct mkdir **);
979 static struct jblocks *jblocks_create(void);
980 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
981 static void jblocks_free(struct jblocks *, struct mount *, int);
982 static void jblocks_destroy(struct jblocks *);
983 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
984
985 /*
986 * Exported softdep operations.
987 */
988 static void softdep_disk_io_initiation(struct buf *);
989 static void softdep_disk_write_complete(struct buf *);
990 static void softdep_deallocate_dependencies(struct buf *);
991 static int softdep_count_dependencies(struct buf *bp, int);
992
993 /*
994 * Global lock over all of soft updates.
995 */
996 static struct mtx lk;
997 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF);
998
999 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk)
1000 #define FREE_GBLLOCK(lk) mtx_unlock(lk)
1001 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED)
1002
1003 /*
1004 * Per-filesystem soft-updates locking.
1005 */
1006 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock)
1007 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock)
1008 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock)
1009 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock)
1010 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \
1011 RA_WLOCKED)
1012
1013 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock)
1014 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock)
1015
1016 /*
1017 * Worklist queue management.
1018 * These routines require that the lock be held.
1019 */
1020 #ifndef /* NOT */ INVARIANTS
1021 #define WORKLIST_INSERT(head, item) do { \
1022 (item)->wk_state |= ONWORKLIST; \
1023 LIST_INSERT_HEAD(head, item, wk_list); \
1024 } while (0)
1025 #define WORKLIST_REMOVE(item) do { \
1026 (item)->wk_state &= ~ONWORKLIST; \
1027 LIST_REMOVE(item, wk_list); \
1028 } while (0)
1029 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT
1030 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE
1031
1032 #else /* INVARIANTS */
1033 static void worklist_insert(struct workhead *, struct worklist *, int,
1034 const char *, int);
1035 static void worklist_remove(struct worklist *, int, const char *, int);
1036
1037 #define WORKLIST_INSERT(head, item) \
1038 worklist_insert(head, item, 1, __func__, __LINE__)
1039 #define WORKLIST_INSERT_UNLOCKED(head, item)\
1040 worklist_insert(head, item, 0, __func__, __LINE__)
1041 #define WORKLIST_REMOVE(item)\
1042 worklist_remove(item, 1, __func__, __LINE__)
1043 #define WORKLIST_REMOVE_UNLOCKED(item)\
1044 worklist_remove(item, 0, __func__, __LINE__)
1045
1046 static void
worklist_insert(head,item,locked,func,line)1047 worklist_insert(head, item, locked, func, line)
1048 struct workhead *head;
1049 struct worklist *item;
1050 int locked;
1051 const char *func;
1052 int line;
1053 {
1054
1055 if (locked)
1056 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1057 if (item->wk_state & ONWORKLIST)
1058 panic("worklist_insert: %p %s(0x%X) already on list, "
1059 "added in function %s at line %d",
1060 item, TYPENAME(item->wk_type), item->wk_state,
1061 item->wk_func, item->wk_line);
1062 item->wk_state |= ONWORKLIST;
1063 item->wk_func = func;
1064 item->wk_line = line;
1065 LIST_INSERT_HEAD(head, item, wk_list);
1066 }
1067
1068 static void
worklist_remove(item,locked,func,line)1069 worklist_remove(item, locked, func, line)
1070 struct worklist *item;
1071 int locked;
1072 const char *func;
1073 int line;
1074 {
1075
1076 if (locked)
1077 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1078 if ((item->wk_state & ONWORKLIST) == 0)
1079 panic("worklist_remove: %p %s(0x%X) not on list, "
1080 "removed in function %s at line %d",
1081 item, TYPENAME(item->wk_type), item->wk_state,
1082 item->wk_func, item->wk_line);
1083 item->wk_state &= ~ONWORKLIST;
1084 item->wk_func = func;
1085 item->wk_line = line;
1086 LIST_REMOVE(item, wk_list);
1087 }
1088 #endif /* INVARIANTS */
1089
1090 /*
1091 * Merge two jsegdeps keeping only the oldest one as newer references
1092 * can't be discarded until after older references.
1093 */
1094 static inline struct jsegdep *
jsegdep_merge(struct jsegdep * one,struct jsegdep * two)1095 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1096 {
1097 struct jsegdep *swp;
1098
1099 if (two == NULL)
1100 return (one);
1101
1102 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1103 swp = one;
1104 one = two;
1105 two = swp;
1106 }
1107 WORKLIST_REMOVE(&two->jd_list);
1108 free_jsegdep(two);
1109
1110 return (one);
1111 }
1112
1113 /*
1114 * If two freedeps are compatible free one to reduce list size.
1115 */
1116 static inline struct freedep *
freedep_merge(struct freedep * one,struct freedep * two)1117 freedep_merge(struct freedep *one, struct freedep *two)
1118 {
1119 if (two == NULL)
1120 return (one);
1121
1122 if (one->fd_freework == two->fd_freework) {
1123 WORKLIST_REMOVE(&two->fd_list);
1124 free_freedep(two);
1125 }
1126 return (one);
1127 }
1128
1129 /*
1130 * Move journal work from one list to another. Duplicate freedeps and
1131 * jsegdeps are coalesced to keep the lists as small as possible.
1132 */
1133 static void
jwork_move(dst,src)1134 jwork_move(dst, src)
1135 struct workhead *dst;
1136 struct workhead *src;
1137 {
1138 struct freedep *freedep;
1139 struct jsegdep *jsegdep;
1140 struct worklist *wkn;
1141 struct worklist *wk;
1142
1143 KASSERT(dst != src,
1144 ("jwork_move: dst == src"));
1145 freedep = NULL;
1146 jsegdep = NULL;
1147 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1148 if (wk->wk_type == D_JSEGDEP)
1149 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1150 else if (wk->wk_type == D_FREEDEP)
1151 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1152 }
1153
1154 while ((wk = LIST_FIRST(src)) != NULL) {
1155 WORKLIST_REMOVE(wk);
1156 WORKLIST_INSERT(dst, wk);
1157 if (wk->wk_type == D_JSEGDEP) {
1158 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1159 continue;
1160 }
1161 if (wk->wk_type == D_FREEDEP)
1162 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1163 }
1164 }
1165
1166 static void
jwork_insert(dst,jsegdep)1167 jwork_insert(dst, jsegdep)
1168 struct workhead *dst;
1169 struct jsegdep *jsegdep;
1170 {
1171 struct jsegdep *jsegdepn;
1172 struct worklist *wk;
1173
1174 LIST_FOREACH(wk, dst, wk_list)
1175 if (wk->wk_type == D_JSEGDEP)
1176 break;
1177 if (wk == NULL) {
1178 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1179 return;
1180 }
1181 jsegdepn = WK_JSEGDEP(wk);
1182 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1183 WORKLIST_REMOVE(wk);
1184 free_jsegdep(jsegdepn);
1185 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1186 } else
1187 free_jsegdep(jsegdep);
1188 }
1189
1190 /*
1191 * Routines for tracking and managing workitems.
1192 */
1193 static void workitem_free(struct worklist *, int);
1194 static void workitem_alloc(struct worklist *, int, struct mount *);
1195 static void workitem_reassign(struct worklist *, int);
1196
1197 #define WORKITEM_FREE(item, type) \
1198 workitem_free((struct worklist *)(item), (type))
1199 #define WORKITEM_REASSIGN(item, type) \
1200 workitem_reassign((struct worklist *)(item), (type))
1201
1202 static void
workitem_free(item,type)1203 workitem_free(item, type)
1204 struct worklist *item;
1205 int type;
1206 {
1207 struct ufsmount *ump;
1208
1209 #ifdef INVARIANTS
1210 if (item->wk_state & ONWORKLIST)
1211 panic("workitem_free: %s(0x%X) still on list, "
1212 "added in function %s at line %d",
1213 TYPENAME(item->wk_type), item->wk_state,
1214 item->wk_func, item->wk_line);
1215 if (item->wk_type != type && type != D_NEWBLK)
1216 panic("workitem_free: type mismatch %s != %s",
1217 TYPENAME(item->wk_type), TYPENAME(type));
1218 #endif
1219 if (item->wk_state & IOWAITING)
1220 wakeup(item);
1221 ump = VFSTOUFS(item->wk_mp);
1222 LOCK_OWNED(ump);
1223 KASSERT(ump->softdep_deps > 0,
1224 ("workitem_free: %s: softdep_deps going negative",
1225 ump->um_fs->fs_fsmnt));
1226 if (--ump->softdep_deps == 0 && ump->softdep_req)
1227 wakeup(&ump->softdep_deps);
1228 KASSERT(dep_current[item->wk_type] > 0,
1229 ("workitem_free: %s: dep_current[%s] going negative",
1230 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1231 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1232 ("workitem_free: %s: softdep_curdeps[%s] going negative",
1233 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1234 atomic_subtract_long(&dep_current[item->wk_type], 1);
1235 ump->softdep_curdeps[item->wk_type] -= 1;
1236 LIST_REMOVE(item, wk_all);
1237 free(item, DtoM(type));
1238 }
1239
1240 static void
workitem_alloc(item,type,mp)1241 workitem_alloc(item, type, mp)
1242 struct worklist *item;
1243 int type;
1244 struct mount *mp;
1245 {
1246 struct ufsmount *ump;
1247
1248 item->wk_type = type;
1249 item->wk_mp = mp;
1250 item->wk_state = 0;
1251
1252 ump = VFSTOUFS(mp);
1253 ACQUIRE_GBLLOCK(&lk);
1254 dep_current[type]++;
1255 if (dep_current[type] > dep_highuse[type])
1256 dep_highuse[type] = dep_current[type];
1257 dep_total[type]++;
1258 FREE_GBLLOCK(&lk);
1259 ACQUIRE_LOCK(ump);
1260 ump->softdep_curdeps[type] += 1;
1261 ump->softdep_deps++;
1262 ump->softdep_accdeps++;
1263 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all);
1264 FREE_LOCK(ump);
1265 }
1266
1267 static void
workitem_reassign(item,newtype)1268 workitem_reassign(item, newtype)
1269 struct worklist *item;
1270 int newtype;
1271 {
1272 struct ufsmount *ump;
1273
1274 ump = VFSTOUFS(item->wk_mp);
1275 LOCK_OWNED(ump);
1276 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1277 ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1278 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1279 ump->softdep_curdeps[item->wk_type] -= 1;
1280 ump->softdep_curdeps[newtype] += 1;
1281 KASSERT(dep_current[item->wk_type] > 0,
1282 ("workitem_reassign: %s: dep_current[%s] going negative",
1283 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1284 ACQUIRE_GBLLOCK(&lk);
1285 dep_current[newtype]++;
1286 dep_current[item->wk_type]--;
1287 if (dep_current[newtype] > dep_highuse[newtype])
1288 dep_highuse[newtype] = dep_current[newtype];
1289 dep_total[newtype]++;
1290 FREE_GBLLOCK(&lk);
1291 item->wk_type = newtype;
1292 LIST_REMOVE(item, wk_all);
1293 LIST_INSERT_HEAD(&ump->softdep_alldeps[newtype], item, wk_all);
1294 }
1295
1296 /*
1297 * Workitem queue management
1298 */
1299 static int max_softdeps; /* maximum number of structs before slowdown */
1300 static int tickdelay = 2; /* number of ticks to pause during slowdown */
1301 static int proc_waiting; /* tracks whether we have a timeout posted */
1302 static int *stat_countp; /* statistic to count in proc_waiting timeout */
1303 static struct callout softdep_callout;
1304 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
1305 static int req_clear_remove; /* syncer process flush some freeblks */
1306 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1307
1308 /*
1309 * runtime statistics
1310 */
1311 static int stat_flush_threads; /* number of softdep flushing threads */
1312 static int stat_worklist_push; /* number of worklist cleanups */
1313 static int stat_delayed_inact; /* number of delayed inactivation cleanups */
1314 static int stat_blk_limit_push; /* number of times block limit neared */
1315 static int stat_ino_limit_push; /* number of times inode limit neared */
1316 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
1317 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
1318 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
1319 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
1320 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
1321 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1322 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
1323 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */
1324 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */
1325 static int stat_journal_min; /* Times hit journal min threshold */
1326 static int stat_journal_low; /* Times hit journal low threshold */
1327 static int stat_journal_wait; /* Times blocked in jwait(). */
1328 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */
1329 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */
1330 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */
1331 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */
1332 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1333 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1334 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1335 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1336 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1337 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1338
1339 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1340 &max_softdeps, 0, "");
1341 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1342 &tickdelay, 0, "");
1343 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1344 &stat_flush_threads, 0, "");
1345 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push,
1346 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,"");
1347 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD,
1348 &stat_delayed_inact, 0, "");
1349 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push,
1350 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,"");
1351 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push,
1352 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,"");
1353 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit,
1354 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, "");
1355 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit,
1356 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, "");
1357 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit,
1358 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, "");
1359 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs,
1360 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, "");
1361 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap,
1362 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, "");
1363 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs,
1364 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, "");
1365 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry,
1366 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, "");
1367 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback,
1368 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, "");
1369 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback,
1370 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, "");
1371 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low,
1372 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, "");
1373 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min,
1374 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, "");
1375 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait,
1376 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, "");
1377 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage,
1378 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, "");
1379 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks,
1380 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, "");
1381 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode,
1382 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, "");
1383 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk,
1384 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, "");
1385 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests,
1386 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, "");
1387 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests,
1388 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, "");
1389 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay,
1390 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, "");
1391 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries,
1392 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, "");
1393 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures,
1394 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, "");
1395
1396 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1397 &softdep_flushcache, 0, "");
1398 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1399 &stat_emptyjblocks, 0, "");
1400
1401 SYSCTL_DECL(_vfs_ffs);
1402
1403 /* Whether to recompute the summary at mount time */
1404 static int compute_summary_at_mount = 0;
1405 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1406 &compute_summary_at_mount, 0, "Recompute summary at mount");
1407 static int print_threads = 0;
1408 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1409 &print_threads, 0, "Notify flusher thread start/stop");
1410
1411 /* List of all filesystems mounted with soft updates */
1412 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1413
1414 static void
get_parent_vp_unlock_bp(struct mount * mp,struct buf * bp,struct diraddhd * diraddhdp,struct diraddhd * unfinishedp)1415 get_parent_vp_unlock_bp(struct mount *mp, struct buf *bp,
1416 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp)
1417 {
1418 struct diradd *dap;
1419
1420 /*
1421 * Requeue unfinished dependencies before
1422 * unlocking buffer, which could make
1423 * diraddhdp invalid.
1424 */
1425 ACQUIRE_LOCK(VFSTOUFS(mp));
1426 while ((dap = LIST_FIRST(unfinishedp)) != NULL) {
1427 LIST_REMOVE(dap, da_pdlist);
1428 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
1429 }
1430 FREE_LOCK(VFSTOUFS(mp));
1431
1432 bp->b_vflags &= ~BV_SCANNED;
1433 BUF_NOREC(bp);
1434 BUF_UNLOCK(bp);
1435 }
1436
1437 /*
1438 * This function fetches inode inum on mount point mp. We already
1439 * hold a locked vnode vp, and might have a locked buffer bp belonging
1440 * to vp.
1441
1442 * We must not block on acquiring the new inode lock as we will get
1443 * into a lock-order reversal with the buffer lock and possibly get a
1444 * deadlock. Thus if we cannot instantiate the requested vnode
1445 * without sleeping on its lock, we must unlock the vnode and the
1446 * buffer before doing a blocking on the vnode lock. We return
1447 * ERELOOKUP if we have had to unlock either the vnode or the buffer so
1448 * that the caller can reassess its state.
1449 *
1450 * Top-level VFS code (for syscalls and other consumers, e.g. callers
1451 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe
1452 * point.
1453 *
1454 * Since callers expect to operate on fully constructed vnode, we also
1455 * recheck v_data after relock, and return ENOENT if NULL.
1456 *
1457 * If unlocking bp, we must unroll dequeueing its unfinished
1458 * dependencies, and clear scan flag, before unlocking. If unlocking
1459 * vp while it is under deactivation, we re-queue deactivation.
1460 */
1461 static int
get_parent_vp(struct vnode * vp,struct mount * mp,ino_t inum,struct buf * bp,struct diraddhd * diraddhdp,struct diraddhd * unfinishedp,struct vnode ** rvp)1462 get_parent_vp(struct vnode *vp, struct mount *mp, ino_t inum, struct buf *bp,
1463 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp,
1464 struct vnode **rvp)
1465 {
1466 struct vnode *pvp;
1467 int error;
1468 bool bplocked;
1469
1470 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked");
1471 for (bplocked = true, pvp = NULL;;) {
1472 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp,
1473 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
1474 if (error == 0) {
1475 /*
1476 * Since we could have unlocked vp, the inode
1477 * number could no longer indicate a
1478 * constructed node. In this case, we must
1479 * restart the syscall.
1480 */
1481 if (VTOI(pvp)->i_mode == 0 || !bplocked) {
1482 if (bp != NULL && bplocked)
1483 get_parent_vp_unlock_bp(mp, bp,
1484 diraddhdp, unfinishedp);
1485 if (VTOI(pvp)->i_mode == 0)
1486 vgone(pvp);
1487 error = ERELOOKUP;
1488 goto out2;
1489 }
1490 goto out1;
1491 }
1492 if (bp != NULL && bplocked) {
1493 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp);
1494 bplocked = false;
1495 }
1496
1497 /*
1498 * Do not drop vnode lock while inactivating during
1499 * vunref. This would result in leaks of the VI flags
1500 * and reclaiming of non-truncated vnode. Instead,
1501 * re-schedule inactivation hoping that we would be
1502 * able to sync inode later.
1503 */
1504 if ((vp->v_iflag & VI_DOINGINACT) != 0 &&
1505 (vp->v_vflag & VV_UNREF) != 0) {
1506 VI_LOCK(vp);
1507 vp->v_iflag |= VI_OWEINACT;
1508 VI_UNLOCK(vp);
1509 return (ERELOOKUP);
1510 }
1511
1512 VOP_UNLOCK(vp);
1513 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp,
1514 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
1515 if (error != 0) {
1516 MPASS(error != ERELOOKUP);
1517 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1518 break;
1519 }
1520 if (VTOI(pvp)->i_mode == 0) {
1521 vgone(pvp);
1522 vput(pvp);
1523 pvp = NULL;
1524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1525 error = ERELOOKUP;
1526 break;
1527 }
1528 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
1529 if (error == 0)
1530 break;
1531 vput(pvp);
1532 pvp = NULL;
1533 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1534 if (vp->v_data == NULL) {
1535 error = ENOENT;
1536 break;
1537 }
1538 }
1539 if (bp != NULL) {
1540 MPASS(!bplocked);
1541 error = ERELOOKUP;
1542 }
1543 out2:
1544 if (error != 0 && pvp != NULL) {
1545 vput(pvp);
1546 pvp = NULL;
1547 }
1548 out1:
1549 *rvp = pvp;
1550 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return");
1551 return (error);
1552 }
1553
1554 /*
1555 * This function cleans the worklist for a filesystem.
1556 * Each filesystem running with soft dependencies gets its own
1557 * thread to run in this function. The thread is started up in
1558 * softdep_mount and shutdown in softdep_unmount. They show up
1559 * as part of the kernel "bufdaemon" process whose process
1560 * entry is available in bufdaemonproc.
1561 */
1562 static int searchfailed;
1563 extern struct proc *bufdaemonproc;
1564 static void
softdep_flush(addr)1565 softdep_flush(addr)
1566 void *addr;
1567 {
1568 struct mount *mp;
1569 struct thread *td;
1570 struct ufsmount *ump;
1571 int cleanups;
1572
1573 td = curthread;
1574 td->td_pflags |= TDP_NORUNNINGBUF;
1575 mp = (struct mount *)addr;
1576 ump = VFSTOUFS(mp);
1577 atomic_add_int(&stat_flush_threads, 1);
1578 ACQUIRE_LOCK(ump);
1579 ump->softdep_flags &= ~FLUSH_STARTING;
1580 wakeup(&ump->softdep_flushtd);
1581 FREE_LOCK(ump);
1582 if (print_threads) {
1583 if (stat_flush_threads == 1)
1584 printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1585 bufdaemonproc->p_pid);
1586 printf("Start thread %s\n", td->td_name);
1587 }
1588 for (;;) {
1589 while (softdep_process_worklist(mp, 0) > 0 ||
1590 (MOUNTEDSUJ(mp) &&
1591 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1592 kthread_suspend_check();
1593 ACQUIRE_LOCK(ump);
1594 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1595 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1596 "sdflush", hz / 2);
1597 ump->softdep_flags &= ~FLUSH_CLEANUP;
1598 /*
1599 * Check to see if we are done and need to exit.
1600 */
1601 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1602 FREE_LOCK(ump);
1603 continue;
1604 }
1605 ump->softdep_flags &= ~FLUSH_EXIT;
1606 cleanups = ump->um_softdep->sd_cleanups;
1607 FREE_LOCK(ump);
1608 wakeup(&ump->softdep_flags);
1609 if (print_threads) {
1610 printf("Stop thread %s: searchfailed %d, "
1611 "did cleanups %d\n",
1612 td->td_name, searchfailed, cleanups);
1613 }
1614 atomic_subtract_int(&stat_flush_threads, 1);
1615 kthread_exit();
1616 panic("kthread_exit failed\n");
1617 }
1618 }
1619
1620 static void
worklist_speedup(mp)1621 worklist_speedup(mp)
1622 struct mount *mp;
1623 {
1624 struct ufsmount *ump;
1625
1626 ump = VFSTOUFS(mp);
1627 LOCK_OWNED(ump);
1628 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1629 ump->softdep_flags |= FLUSH_CLEANUP;
1630 wakeup(&ump->softdep_flushtd);
1631 }
1632
1633 static void
softdep_send_speedup(struct ufsmount * ump,off_t shortage,u_int flags)1634 softdep_send_speedup(struct ufsmount *ump, off_t shortage, u_int flags)
1635 {
1636 struct buf *bp;
1637
1638 if ((ump->um_flags & UM_CANSPEEDUP) == 0)
1639 return;
1640
1641 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO);
1642 bp->b_iocmd = BIO_SPEEDUP;
1643 bp->b_ioflags = flags;
1644 bp->b_bcount = omin(shortage, LONG_MAX);
1645 g_vfs_strategy(ump->um_bo, bp);
1646 bufwait(bp);
1647 free(bp, M_TRIM);
1648 }
1649
1650 static int
softdep_speedup(ump)1651 softdep_speedup(ump)
1652 struct ufsmount *ump;
1653 {
1654 struct ufsmount *altump;
1655 struct mount_softdeps *sdp;
1656
1657 LOCK_OWNED(ump);
1658 worklist_speedup(ump->um_mountp);
1659 bd_speedup();
1660 /*
1661 * If we have global shortages, then we need other
1662 * filesystems to help with the cleanup. Here we wakeup a
1663 * flusher thread for a filesystem that is over its fair
1664 * share of resources.
1665 */
1666 if (req_clear_inodedeps || req_clear_remove) {
1667 ACQUIRE_GBLLOCK(&lk);
1668 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1669 if ((altump = sdp->sd_ump) == ump)
1670 continue;
1671 if (((req_clear_inodedeps &&
1672 altump->softdep_curdeps[D_INODEDEP] >
1673 max_softdeps / stat_flush_threads) ||
1674 (req_clear_remove &&
1675 altump->softdep_curdeps[D_DIRREM] >
1676 (max_softdeps / 2) / stat_flush_threads)) &&
1677 TRY_ACQUIRE_LOCK(altump))
1678 break;
1679 }
1680 if (sdp == NULL) {
1681 searchfailed++;
1682 FREE_GBLLOCK(&lk);
1683 } else {
1684 /*
1685 * Move to the end of the list so we pick a
1686 * different one on out next try.
1687 */
1688 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1689 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1690 FREE_GBLLOCK(&lk);
1691 if ((altump->softdep_flags &
1692 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1693 altump->softdep_flags |= FLUSH_CLEANUP;
1694 altump->um_softdep->sd_cleanups++;
1695 wakeup(&altump->softdep_flushtd);
1696 FREE_LOCK(altump);
1697 }
1698 }
1699 return (speedup_syncer());
1700 }
1701
1702 /*
1703 * Add an item to the end of the work queue.
1704 * This routine requires that the lock be held.
1705 * This is the only routine that adds items to the list.
1706 * The following routine is the only one that removes items
1707 * and does so in order from first to last.
1708 */
1709
1710 #define WK_HEAD 0x0001 /* Add to HEAD. */
1711 #define WK_NODELAY 0x0002 /* Process immediately. */
1712
1713 static void
add_to_worklist(wk,flags)1714 add_to_worklist(wk, flags)
1715 struct worklist *wk;
1716 int flags;
1717 {
1718 struct ufsmount *ump;
1719
1720 ump = VFSTOUFS(wk->wk_mp);
1721 LOCK_OWNED(ump);
1722 if (wk->wk_state & ONWORKLIST)
1723 panic("add_to_worklist: %s(0x%X) already on list",
1724 TYPENAME(wk->wk_type), wk->wk_state);
1725 wk->wk_state |= ONWORKLIST;
1726 if (ump->softdep_on_worklist == 0) {
1727 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1728 ump->softdep_worklist_tail = wk;
1729 } else if (flags & WK_HEAD) {
1730 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1731 } else {
1732 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1733 ump->softdep_worklist_tail = wk;
1734 }
1735 ump->softdep_on_worklist += 1;
1736 if (flags & WK_NODELAY)
1737 worklist_speedup(wk->wk_mp);
1738 }
1739
1740 /*
1741 * Remove the item to be processed. If we are removing the last
1742 * item on the list, we need to recalculate the tail pointer.
1743 */
1744 static void
remove_from_worklist(wk)1745 remove_from_worklist(wk)
1746 struct worklist *wk;
1747 {
1748 struct ufsmount *ump;
1749
1750 ump = VFSTOUFS(wk->wk_mp);
1751 if (ump->softdep_worklist_tail == wk)
1752 ump->softdep_worklist_tail =
1753 (struct worklist *)wk->wk_list.le_prev;
1754 WORKLIST_REMOVE(wk);
1755 ump->softdep_on_worklist -= 1;
1756 }
1757
1758 static void
wake_worklist(wk)1759 wake_worklist(wk)
1760 struct worklist *wk;
1761 {
1762 if (wk->wk_state & IOWAITING) {
1763 wk->wk_state &= ~IOWAITING;
1764 wakeup(wk);
1765 }
1766 }
1767
1768 static void
wait_worklist(wk,wmesg)1769 wait_worklist(wk, wmesg)
1770 struct worklist *wk;
1771 char *wmesg;
1772 {
1773 struct ufsmount *ump;
1774
1775 ump = VFSTOUFS(wk->wk_mp);
1776 wk->wk_state |= IOWAITING;
1777 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1778 }
1779
1780 /*
1781 * Process that runs once per second to handle items in the background queue.
1782 *
1783 * Note that we ensure that everything is done in the order in which they
1784 * appear in the queue. The code below depends on this property to ensure
1785 * that blocks of a file are freed before the inode itself is freed. This
1786 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1787 * until all the old ones have been purged from the dependency lists.
1788 */
1789 static int
softdep_process_worklist(mp,full)1790 softdep_process_worklist(mp, full)
1791 struct mount *mp;
1792 int full;
1793 {
1794 int cnt, matchcnt;
1795 struct ufsmount *ump;
1796 long starttime;
1797
1798 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1799 ump = VFSTOUFS(mp);
1800 if (ump->um_softdep == NULL)
1801 return (0);
1802 matchcnt = 0;
1803 ACQUIRE_LOCK(ump);
1804 starttime = time_second;
1805 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1806 check_clear_deps(mp);
1807 while (ump->softdep_on_worklist > 0) {
1808 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1809 break;
1810 else
1811 matchcnt += cnt;
1812 check_clear_deps(mp);
1813 /*
1814 * We do not generally want to stop for buffer space, but if
1815 * we are really being a buffer hog, we will stop and wait.
1816 */
1817 if (should_yield()) {
1818 FREE_LOCK(ump);
1819 kern_yield(PRI_USER);
1820 bwillwrite();
1821 ACQUIRE_LOCK(ump);
1822 }
1823 /*
1824 * Never allow processing to run for more than one
1825 * second. This gives the syncer thread the opportunity
1826 * to pause if appropriate.
1827 */
1828 if (!full && starttime != time_second)
1829 break;
1830 }
1831 if (full == 0)
1832 journal_unsuspend(ump);
1833 FREE_LOCK(ump);
1834 return (matchcnt);
1835 }
1836
1837 /*
1838 * Process all removes associated with a vnode if we are running out of
1839 * journal space. Any other process which attempts to flush these will
1840 * be unable as we have the vnodes locked.
1841 */
1842 static void
process_removes(vp)1843 process_removes(vp)
1844 struct vnode *vp;
1845 {
1846 struct inodedep *inodedep;
1847 struct dirrem *dirrem;
1848 struct ufsmount *ump;
1849 struct mount *mp;
1850 ino_t inum;
1851
1852 mp = vp->v_mount;
1853 ump = VFSTOUFS(mp);
1854 LOCK_OWNED(ump);
1855 inum = VTOI(vp)->i_number;
1856 for (;;) {
1857 top:
1858 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1859 return;
1860 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1861 /*
1862 * If another thread is trying to lock this vnode
1863 * it will fail but we must wait for it to do so
1864 * before we can proceed.
1865 */
1866 if (dirrem->dm_state & INPROGRESS) {
1867 wait_worklist(&dirrem->dm_list, "pwrwait");
1868 goto top;
1869 }
1870 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1871 (COMPLETE | ONWORKLIST))
1872 break;
1873 }
1874 if (dirrem == NULL)
1875 return;
1876 remove_from_worklist(&dirrem->dm_list);
1877 FREE_LOCK(ump);
1878 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1879 panic("process_removes: suspended filesystem");
1880 handle_workitem_remove(dirrem, 0);
1881 vn_finished_secondary_write(mp);
1882 ACQUIRE_LOCK(ump);
1883 }
1884 }
1885
1886 /*
1887 * Process all truncations associated with a vnode if we are running out
1888 * of journal space. This is called when the vnode lock is already held
1889 * and no other process can clear the truncation. This function returns
1890 * a value greater than zero if it did any work.
1891 */
1892 static void
process_truncates(vp)1893 process_truncates(vp)
1894 struct vnode *vp;
1895 {
1896 struct inodedep *inodedep;
1897 struct freeblks *freeblks;
1898 struct ufsmount *ump;
1899 struct mount *mp;
1900 ino_t inum;
1901 int cgwait;
1902
1903 mp = vp->v_mount;
1904 ump = VFSTOUFS(mp);
1905 LOCK_OWNED(ump);
1906 inum = VTOI(vp)->i_number;
1907 for (;;) {
1908 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1909 return;
1910 cgwait = 0;
1911 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1912 /* Journal entries not yet written. */
1913 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1914 jwait(&LIST_FIRST(
1915 &freeblks->fb_jblkdephd)->jb_list,
1916 MNT_WAIT);
1917 break;
1918 }
1919 /* Another thread is executing this item. */
1920 if (freeblks->fb_state & INPROGRESS) {
1921 wait_worklist(&freeblks->fb_list, "ptrwait");
1922 break;
1923 }
1924 /* Freeblks is waiting on a inode write. */
1925 if ((freeblks->fb_state & COMPLETE) == 0) {
1926 FREE_LOCK(ump);
1927 ffs_update(vp, 1);
1928 ACQUIRE_LOCK(ump);
1929 break;
1930 }
1931 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1932 (ALLCOMPLETE | ONWORKLIST)) {
1933 remove_from_worklist(&freeblks->fb_list);
1934 freeblks->fb_state |= INPROGRESS;
1935 FREE_LOCK(ump);
1936 if (vn_start_secondary_write(NULL, &mp,
1937 V_NOWAIT))
1938 panic("process_truncates: "
1939 "suspended filesystem");
1940 handle_workitem_freeblocks(freeblks, 0);
1941 vn_finished_secondary_write(mp);
1942 ACQUIRE_LOCK(ump);
1943 break;
1944 }
1945 if (freeblks->fb_cgwait)
1946 cgwait++;
1947 }
1948 if (cgwait) {
1949 FREE_LOCK(ump);
1950 sync_cgs(mp, MNT_WAIT);
1951 ffs_sync_snap(mp, MNT_WAIT);
1952 ACQUIRE_LOCK(ump);
1953 continue;
1954 }
1955 if (freeblks == NULL)
1956 break;
1957 }
1958 return;
1959 }
1960
1961 /*
1962 * Process one item on the worklist.
1963 */
1964 static int
process_worklist_item(mp,target,flags)1965 process_worklist_item(mp, target, flags)
1966 struct mount *mp;
1967 int target;
1968 int flags;
1969 {
1970 struct worklist sentinel;
1971 struct worklist *wk;
1972 struct ufsmount *ump;
1973 int matchcnt;
1974 int error;
1975
1976 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1977 /*
1978 * If we are being called because of a process doing a
1979 * copy-on-write, then it is not safe to write as we may
1980 * recurse into the copy-on-write routine.
1981 */
1982 if (curthread->td_pflags & TDP_COWINPROGRESS)
1983 return (-1);
1984 PHOLD(curproc); /* Don't let the stack go away. */
1985 ump = VFSTOUFS(mp);
1986 LOCK_OWNED(ump);
1987 matchcnt = 0;
1988 sentinel.wk_mp = NULL;
1989 sentinel.wk_type = D_SENTINEL;
1990 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1991 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1992 wk = LIST_NEXT(&sentinel, wk_list)) {
1993 if (wk->wk_type == D_SENTINEL) {
1994 LIST_REMOVE(&sentinel, wk_list);
1995 LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1996 continue;
1997 }
1998 if (wk->wk_state & INPROGRESS)
1999 panic("process_worklist_item: %p already in progress.",
2000 wk);
2001 wk->wk_state |= INPROGRESS;
2002 remove_from_worklist(wk);
2003 FREE_LOCK(ump);
2004 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
2005 panic("process_worklist_item: suspended filesystem");
2006 switch (wk->wk_type) {
2007 case D_DIRREM:
2008 /* removal of a directory entry */
2009 error = handle_workitem_remove(WK_DIRREM(wk), flags);
2010 break;
2011
2012 case D_FREEBLKS:
2013 /* releasing blocks and/or fragments from a file */
2014 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
2015 flags);
2016 break;
2017
2018 case D_FREEFRAG:
2019 /* releasing a fragment when replaced as a file grows */
2020 handle_workitem_freefrag(WK_FREEFRAG(wk));
2021 error = 0;
2022 break;
2023
2024 case D_FREEFILE:
2025 /* releasing an inode when its link count drops to 0 */
2026 handle_workitem_freefile(WK_FREEFILE(wk));
2027 error = 0;
2028 break;
2029
2030 default:
2031 panic("%s_process_worklist: Unknown type %s",
2032 "softdep", TYPENAME(wk->wk_type));
2033 /* NOTREACHED */
2034 }
2035 vn_finished_secondary_write(mp);
2036 ACQUIRE_LOCK(ump);
2037 if (error == 0) {
2038 if (++matchcnt == target)
2039 break;
2040 continue;
2041 }
2042 /*
2043 * We have to retry the worklist item later. Wake up any
2044 * waiters who may be able to complete it immediately and
2045 * add the item back to the head so we don't try to execute
2046 * it again.
2047 */
2048 wk->wk_state &= ~INPROGRESS;
2049 wake_worklist(wk);
2050 add_to_worklist(wk, WK_HEAD);
2051 }
2052 /* Sentinal could've become the tail from remove_from_worklist. */
2053 if (ump->softdep_worklist_tail == &sentinel)
2054 ump->softdep_worklist_tail =
2055 (struct worklist *)sentinel.wk_list.le_prev;
2056 LIST_REMOVE(&sentinel, wk_list);
2057 PRELE(curproc);
2058 return (matchcnt);
2059 }
2060
2061 /*
2062 * Move dependencies from one buffer to another.
2063 */
2064 int
softdep_move_dependencies(oldbp,newbp)2065 softdep_move_dependencies(oldbp, newbp)
2066 struct buf *oldbp;
2067 struct buf *newbp;
2068 {
2069 struct worklist *wk, *wktail;
2070 struct ufsmount *ump;
2071 int dirty;
2072
2073 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
2074 return (0);
2075 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
2076 ("softdep_move_dependencies called on non-softdep filesystem"));
2077 dirty = 0;
2078 wktail = NULL;
2079 ump = VFSTOUFS(wk->wk_mp);
2080 ACQUIRE_LOCK(ump);
2081 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
2082 LIST_REMOVE(wk, wk_list);
2083 if (wk->wk_type == D_BMSAFEMAP &&
2084 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
2085 dirty = 1;
2086 if (wktail == NULL)
2087 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
2088 else
2089 LIST_INSERT_AFTER(wktail, wk, wk_list);
2090 wktail = wk;
2091 }
2092 FREE_LOCK(ump);
2093
2094 return (dirty);
2095 }
2096
2097 /*
2098 * Purge the work list of all items associated with a particular mount point.
2099 */
2100 int
softdep_flushworklist(oldmnt,countp,td)2101 softdep_flushworklist(oldmnt, countp, td)
2102 struct mount *oldmnt;
2103 int *countp;
2104 struct thread *td;
2105 {
2106 struct vnode *devvp;
2107 struct ufsmount *ump;
2108 int count, error;
2109
2110 /*
2111 * Alternately flush the block device associated with the mount
2112 * point and process any dependencies that the flushing
2113 * creates. We continue until no more worklist dependencies
2114 * are found.
2115 */
2116 *countp = 0;
2117 error = 0;
2118 ump = VFSTOUFS(oldmnt);
2119 devvp = ump->um_devvp;
2120 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
2121 *countp += count;
2122 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2123 error = VOP_FSYNC(devvp, MNT_WAIT, td);
2124 VOP_UNLOCK(devvp);
2125 if (error != 0)
2126 break;
2127 }
2128 return (error);
2129 }
2130
2131 #define SU_WAITIDLE_RETRIES 20
2132 static int
softdep_waitidle(struct mount * mp,int flags __unused)2133 softdep_waitidle(struct mount *mp, int flags __unused)
2134 {
2135 struct ufsmount *ump;
2136 struct vnode *devvp;
2137 struct thread *td;
2138 int error, i;
2139
2140 ump = VFSTOUFS(mp);
2141 KASSERT(ump->um_softdep != NULL,
2142 ("softdep_waitidle called on non-softdep filesystem"));
2143 devvp = ump->um_devvp;
2144 td = curthread;
2145 error = 0;
2146 ACQUIRE_LOCK(ump);
2147 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
2148 ump->softdep_req = 1;
2149 KASSERT((flags & FORCECLOSE) == 0 ||
2150 ump->softdep_on_worklist == 0,
2151 ("softdep_waitidle: work added after flush"));
2152 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
2153 "softdeps", 10 * hz);
2154 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2155 error = VOP_FSYNC(devvp, MNT_WAIT, td);
2156 VOP_UNLOCK(devvp);
2157 ACQUIRE_LOCK(ump);
2158 if (error != 0)
2159 break;
2160 }
2161 ump->softdep_req = 0;
2162 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
2163 error = EBUSY;
2164 printf("softdep_waitidle: Failed to flush worklist for %p\n",
2165 mp);
2166 }
2167 FREE_LOCK(ump);
2168 return (error);
2169 }
2170
2171 /*
2172 * Flush all vnodes and worklist items associated with a specified mount point.
2173 */
2174 int
softdep_flushfiles(oldmnt,flags,td)2175 softdep_flushfiles(oldmnt, flags, td)
2176 struct mount *oldmnt;
2177 int flags;
2178 struct thread *td;
2179 {
2180 struct ufsmount *ump;
2181 #ifdef QUOTA
2182 int i;
2183 #endif
2184 int error, early, depcount, loopcnt, retry_flush_count, retry;
2185 int morework;
2186
2187 ump = VFSTOUFS(oldmnt);
2188 KASSERT(ump->um_softdep != NULL,
2189 ("softdep_flushfiles called on non-softdep filesystem"));
2190 loopcnt = 10;
2191 retry_flush_count = 3;
2192 retry_flush:
2193 error = 0;
2194
2195 /*
2196 * Alternately flush the vnodes associated with the mount
2197 * point and process any dependencies that the flushing
2198 * creates. In theory, this loop can happen at most twice,
2199 * but we give it a few extra just to be sure.
2200 */
2201 for (; loopcnt > 0; loopcnt--) {
2202 /*
2203 * Do another flush in case any vnodes were brought in
2204 * as part of the cleanup operations.
2205 */
2206 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2207 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2208 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2209 break;
2210 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2211 depcount == 0)
2212 break;
2213 }
2214 /*
2215 * If we are unmounting then it is an error to fail. If we
2216 * are simply trying to downgrade to read-only, then filesystem
2217 * activity can keep us busy forever, so we just fail with EBUSY.
2218 */
2219 if (loopcnt == 0) {
2220 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2221 panic("softdep_flushfiles: looping");
2222 error = EBUSY;
2223 }
2224 if (!error)
2225 error = softdep_waitidle(oldmnt, flags);
2226 if (!error) {
2227 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2228 retry = 0;
2229 MNT_ILOCK(oldmnt);
2230 morework = oldmnt->mnt_nvnodelistsize > 0;
2231 #ifdef QUOTA
2232 UFS_LOCK(ump);
2233 for (i = 0; i < MAXQUOTAS; i++) {
2234 if (ump->um_quotas[i] != NULLVP)
2235 morework = 1;
2236 }
2237 UFS_UNLOCK(ump);
2238 #endif
2239 if (morework) {
2240 if (--retry_flush_count > 0) {
2241 retry = 1;
2242 loopcnt = 3;
2243 } else
2244 error = EBUSY;
2245 }
2246 MNT_IUNLOCK(oldmnt);
2247 if (retry)
2248 goto retry_flush;
2249 }
2250 }
2251 return (error);
2252 }
2253
2254 /*
2255 * Structure hashing.
2256 *
2257 * There are four types of structures that can be looked up:
2258 * 1) pagedep structures identified by mount point, inode number,
2259 * and logical block.
2260 * 2) inodedep structures identified by mount point and inode number.
2261 * 3) newblk structures identified by mount point and
2262 * physical block number.
2263 * 4) bmsafemap structures identified by mount point and
2264 * cylinder group number.
2265 *
2266 * The "pagedep" and "inodedep" dependency structures are hashed
2267 * separately from the file blocks and inodes to which they correspond.
2268 * This separation helps when the in-memory copy of an inode or
2269 * file block must be replaced. It also obviates the need to access
2270 * an inode or file page when simply updating (or de-allocating)
2271 * dependency structures. Lookup of newblk structures is needed to
2272 * find newly allocated blocks when trying to associate them with
2273 * their allocdirect or allocindir structure.
2274 *
2275 * The lookup routines optionally create and hash a new instance when
2276 * an existing entry is not found. The bmsafemap lookup routine always
2277 * allocates a new structure if an existing one is not found.
2278 */
2279 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2280
2281 /*
2282 * Structures and routines associated with pagedep caching.
2283 */
2284 #define PAGEDEP_HASH(ump, inum, lbn) \
2285 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2286
2287 static int
pagedep_find(pagedephd,ino,lbn,pagedeppp)2288 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2289 struct pagedep_hashhead *pagedephd;
2290 ino_t ino;
2291 ufs_lbn_t lbn;
2292 struct pagedep **pagedeppp;
2293 {
2294 struct pagedep *pagedep;
2295
2296 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2297 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2298 *pagedeppp = pagedep;
2299 return (1);
2300 }
2301 }
2302 *pagedeppp = NULL;
2303 return (0);
2304 }
2305 /*
2306 * Look up a pagedep. Return 1 if found, 0 otherwise.
2307 * If not found, allocate if DEPALLOC flag is passed.
2308 * Found or allocated entry is returned in pagedeppp.
2309 */
2310 static int
pagedep_lookup(mp,bp,ino,lbn,flags,pagedeppp)2311 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2312 struct mount *mp;
2313 struct buf *bp;
2314 ino_t ino;
2315 ufs_lbn_t lbn;
2316 int flags;
2317 struct pagedep **pagedeppp;
2318 {
2319 struct pagedep *pagedep;
2320 struct pagedep_hashhead *pagedephd;
2321 struct worklist *wk;
2322 struct ufsmount *ump;
2323 int ret;
2324 int i;
2325
2326 ump = VFSTOUFS(mp);
2327 LOCK_OWNED(ump);
2328 if (bp) {
2329 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2330 if (wk->wk_type == D_PAGEDEP) {
2331 *pagedeppp = WK_PAGEDEP(wk);
2332 return (1);
2333 }
2334 }
2335 }
2336 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2337 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2338 if (ret) {
2339 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2340 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2341 return (1);
2342 }
2343 if ((flags & DEPALLOC) == 0)
2344 return (0);
2345 FREE_LOCK(ump);
2346 pagedep = malloc(sizeof(struct pagedep),
2347 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2348 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2349 ACQUIRE_LOCK(ump);
2350 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2351 if (*pagedeppp) {
2352 /*
2353 * This should never happen since we only create pagedeps
2354 * with the vnode lock held. Could be an assert.
2355 */
2356 WORKITEM_FREE(pagedep, D_PAGEDEP);
2357 return (ret);
2358 }
2359 pagedep->pd_ino = ino;
2360 pagedep->pd_lbn = lbn;
2361 LIST_INIT(&pagedep->pd_dirremhd);
2362 LIST_INIT(&pagedep->pd_pendinghd);
2363 for (i = 0; i < DAHASHSZ; i++)
2364 LIST_INIT(&pagedep->pd_diraddhd[i]);
2365 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2366 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2367 *pagedeppp = pagedep;
2368 return (0);
2369 }
2370
2371 /*
2372 * Structures and routines associated with inodedep caching.
2373 */
2374 #define INODEDEP_HASH(ump, inum) \
2375 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2376
2377 static int
inodedep_find(inodedephd,inum,inodedeppp)2378 inodedep_find(inodedephd, inum, inodedeppp)
2379 struct inodedep_hashhead *inodedephd;
2380 ino_t inum;
2381 struct inodedep **inodedeppp;
2382 {
2383 struct inodedep *inodedep;
2384
2385 LIST_FOREACH(inodedep, inodedephd, id_hash)
2386 if (inum == inodedep->id_ino)
2387 break;
2388 if (inodedep) {
2389 *inodedeppp = inodedep;
2390 return (1);
2391 }
2392 *inodedeppp = NULL;
2393
2394 return (0);
2395 }
2396 /*
2397 * Look up an inodedep. Return 1 if found, 0 if not found.
2398 * If not found, allocate if DEPALLOC flag is passed.
2399 * Found or allocated entry is returned in inodedeppp.
2400 */
2401 static int
inodedep_lookup(mp,inum,flags,inodedeppp)2402 inodedep_lookup(mp, inum, flags, inodedeppp)
2403 struct mount *mp;
2404 ino_t inum;
2405 int flags;
2406 struct inodedep **inodedeppp;
2407 {
2408 struct inodedep *inodedep;
2409 struct inodedep_hashhead *inodedephd;
2410 struct ufsmount *ump;
2411 struct fs *fs;
2412
2413 ump = VFSTOUFS(mp);
2414 LOCK_OWNED(ump);
2415 fs = ump->um_fs;
2416 inodedephd = INODEDEP_HASH(ump, inum);
2417
2418 if (inodedep_find(inodedephd, inum, inodedeppp))
2419 return (1);
2420 if ((flags & DEPALLOC) == 0)
2421 return (0);
2422 /*
2423 * If the system is over its limit and our filesystem is
2424 * responsible for more than our share of that usage and
2425 * we are not in a rush, request some inodedep cleanup.
2426 */
2427 if (softdep_excess_items(ump, D_INODEDEP))
2428 schedule_cleanup(mp);
2429 else
2430 FREE_LOCK(ump);
2431 inodedep = malloc(sizeof(struct inodedep),
2432 M_INODEDEP, M_SOFTDEP_FLAGS);
2433 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2434 ACQUIRE_LOCK(ump);
2435 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2436 WORKITEM_FREE(inodedep, D_INODEDEP);
2437 return (1);
2438 }
2439 inodedep->id_fs = fs;
2440 inodedep->id_ino = inum;
2441 inodedep->id_state = ALLCOMPLETE;
2442 inodedep->id_nlinkdelta = 0;
2443 inodedep->id_nlinkwrote = -1;
2444 inodedep->id_savedino1 = NULL;
2445 inodedep->id_savedsize = -1;
2446 inodedep->id_savedextsize = -1;
2447 inodedep->id_savednlink = -1;
2448 inodedep->id_bmsafemap = NULL;
2449 inodedep->id_mkdiradd = NULL;
2450 LIST_INIT(&inodedep->id_dirremhd);
2451 LIST_INIT(&inodedep->id_pendinghd);
2452 LIST_INIT(&inodedep->id_inowait);
2453 LIST_INIT(&inodedep->id_bufwait);
2454 TAILQ_INIT(&inodedep->id_inoreflst);
2455 TAILQ_INIT(&inodedep->id_inoupdt);
2456 TAILQ_INIT(&inodedep->id_newinoupdt);
2457 TAILQ_INIT(&inodedep->id_extupdt);
2458 TAILQ_INIT(&inodedep->id_newextupdt);
2459 TAILQ_INIT(&inodedep->id_freeblklst);
2460 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2461 *inodedeppp = inodedep;
2462 return (0);
2463 }
2464
2465 /*
2466 * Structures and routines associated with newblk caching.
2467 */
2468 #define NEWBLK_HASH(ump, inum) \
2469 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2470
2471 static int
newblk_find(newblkhd,newblkno,flags,newblkpp)2472 newblk_find(newblkhd, newblkno, flags, newblkpp)
2473 struct newblk_hashhead *newblkhd;
2474 ufs2_daddr_t newblkno;
2475 int flags;
2476 struct newblk **newblkpp;
2477 {
2478 struct newblk *newblk;
2479
2480 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2481 if (newblkno != newblk->nb_newblkno)
2482 continue;
2483 /*
2484 * If we're creating a new dependency don't match those that
2485 * have already been converted to allocdirects. This is for
2486 * a frag extend.
2487 */
2488 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2489 continue;
2490 break;
2491 }
2492 if (newblk) {
2493 *newblkpp = newblk;
2494 return (1);
2495 }
2496 *newblkpp = NULL;
2497 return (0);
2498 }
2499
2500 /*
2501 * Look up a newblk. Return 1 if found, 0 if not found.
2502 * If not found, allocate if DEPALLOC flag is passed.
2503 * Found or allocated entry is returned in newblkpp.
2504 */
2505 static int
newblk_lookup(mp,newblkno,flags,newblkpp)2506 newblk_lookup(mp, newblkno, flags, newblkpp)
2507 struct mount *mp;
2508 ufs2_daddr_t newblkno;
2509 int flags;
2510 struct newblk **newblkpp;
2511 {
2512 struct newblk *newblk;
2513 struct newblk_hashhead *newblkhd;
2514 struct ufsmount *ump;
2515
2516 ump = VFSTOUFS(mp);
2517 LOCK_OWNED(ump);
2518 newblkhd = NEWBLK_HASH(ump, newblkno);
2519 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2520 return (1);
2521 if ((flags & DEPALLOC) == 0)
2522 return (0);
2523 if (softdep_excess_items(ump, D_NEWBLK) ||
2524 softdep_excess_items(ump, D_ALLOCDIRECT) ||
2525 softdep_excess_items(ump, D_ALLOCINDIR))
2526 schedule_cleanup(mp);
2527 else
2528 FREE_LOCK(ump);
2529 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2530 M_SOFTDEP_FLAGS | M_ZERO);
2531 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2532 ACQUIRE_LOCK(ump);
2533 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2534 WORKITEM_FREE(newblk, D_NEWBLK);
2535 return (1);
2536 }
2537 newblk->nb_freefrag = NULL;
2538 LIST_INIT(&newblk->nb_indirdeps);
2539 LIST_INIT(&newblk->nb_newdirblk);
2540 LIST_INIT(&newblk->nb_jwork);
2541 newblk->nb_state = ATTACHED;
2542 newblk->nb_newblkno = newblkno;
2543 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2544 *newblkpp = newblk;
2545 return (0);
2546 }
2547
2548 /*
2549 * Structures and routines associated with freed indirect block caching.
2550 */
2551 #define INDIR_HASH(ump, blkno) \
2552 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2553
2554 /*
2555 * Lookup an indirect block in the indir hash table. The freework is
2556 * removed and potentially freed. The caller must do a blocking journal
2557 * write before writing to the blkno.
2558 */
2559 static int
indirblk_lookup(mp,blkno)2560 indirblk_lookup(mp, blkno)
2561 struct mount *mp;
2562 ufs2_daddr_t blkno;
2563 {
2564 struct freework *freework;
2565 struct indir_hashhead *wkhd;
2566 struct ufsmount *ump;
2567
2568 ump = VFSTOUFS(mp);
2569 wkhd = INDIR_HASH(ump, blkno);
2570 TAILQ_FOREACH(freework, wkhd, fw_next) {
2571 if (freework->fw_blkno != blkno)
2572 continue;
2573 indirblk_remove(freework);
2574 return (1);
2575 }
2576 return (0);
2577 }
2578
2579 /*
2580 * Insert an indirect block represented by freework into the indirblk
2581 * hash table so that it may prevent the block from being re-used prior
2582 * to the journal being written.
2583 */
2584 static void
indirblk_insert(freework)2585 indirblk_insert(freework)
2586 struct freework *freework;
2587 {
2588 struct jblocks *jblocks;
2589 struct jseg *jseg;
2590 struct ufsmount *ump;
2591
2592 ump = VFSTOUFS(freework->fw_list.wk_mp);
2593 jblocks = ump->softdep_jblocks;
2594 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2595 if (jseg == NULL)
2596 return;
2597
2598 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2599 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2600 fw_next);
2601 freework->fw_state &= ~DEPCOMPLETE;
2602 }
2603
2604 static void
indirblk_remove(freework)2605 indirblk_remove(freework)
2606 struct freework *freework;
2607 {
2608 struct ufsmount *ump;
2609
2610 ump = VFSTOUFS(freework->fw_list.wk_mp);
2611 LIST_REMOVE(freework, fw_segs);
2612 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2613 freework->fw_state |= DEPCOMPLETE;
2614 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2615 WORKITEM_FREE(freework, D_FREEWORK);
2616 }
2617
2618 /*
2619 * Executed during filesystem system initialization before
2620 * mounting any filesystems.
2621 */
2622 void
softdep_initialize()2623 softdep_initialize()
2624 {
2625
2626 TAILQ_INIT(&softdepmounts);
2627 #ifdef __LP64__
2628 max_softdeps = desiredvnodes * 4;
2629 #else
2630 max_softdeps = desiredvnodes * 2;
2631 #endif
2632
2633 /* initialise bioops hack */
2634 bioops.io_start = softdep_disk_io_initiation;
2635 bioops.io_complete = softdep_disk_write_complete;
2636 bioops.io_deallocate = softdep_deallocate_dependencies;
2637 bioops.io_countdeps = softdep_count_dependencies;
2638 softdep_ast_cleanup = softdep_ast_cleanup_proc;
2639
2640 /* Initialize the callout with an mtx. */
2641 callout_init_mtx(&softdep_callout, &lk, 0);
2642 }
2643
2644 /*
2645 * Executed after all filesystems have been unmounted during
2646 * filesystem module unload.
2647 */
2648 void
softdep_uninitialize()2649 softdep_uninitialize()
2650 {
2651
2652 /* clear bioops hack */
2653 bioops.io_start = NULL;
2654 bioops.io_complete = NULL;
2655 bioops.io_deallocate = NULL;
2656 bioops.io_countdeps = NULL;
2657 softdep_ast_cleanup = NULL;
2658
2659 callout_drain(&softdep_callout);
2660 }
2661
2662 /*
2663 * Called at mount time to notify the dependency code that a
2664 * filesystem wishes to use it.
2665 */
2666 int
softdep_mount(devvp,mp,fs,cred)2667 softdep_mount(devvp, mp, fs, cred)
2668 struct vnode *devvp;
2669 struct mount *mp;
2670 struct fs *fs;
2671 struct ucred *cred;
2672 {
2673 struct csum_total cstotal;
2674 struct mount_softdeps *sdp;
2675 struct ufsmount *ump;
2676 struct cg *cgp;
2677 struct buf *bp;
2678 u_int cyl, i;
2679 int error;
2680
2681 ump = VFSTOUFS(mp);
2682
2683 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2684 M_WAITOK | M_ZERO);
2685 rw_init(&sdp->sd_fslock, "SUrw");
2686 sdp->sd_ump = ump;
2687 LIST_INIT(&sdp->sd_workitem_pending);
2688 LIST_INIT(&sdp->sd_journal_pending);
2689 TAILQ_INIT(&sdp->sd_unlinked);
2690 LIST_INIT(&sdp->sd_dirtycg);
2691 sdp->sd_worklist_tail = NULL;
2692 sdp->sd_on_worklist = 0;
2693 sdp->sd_deps = 0;
2694 LIST_INIT(&sdp->sd_mkdirlisthd);
2695 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP,
2696 &sdp->sd_pdhashsize);
2697 sdp->sd_pdnextclean = 0;
2698 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP,
2699 &sdp->sd_idhashsize);
2700 sdp->sd_idnextclean = 0;
2701 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK,
2702 &sdp->sd_newblkhashsize);
2703 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize);
2704 i = 1 << (ffs(desiredvnodes / 10) - 1);
2705 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead),
2706 M_FREEWORK, M_WAITOK);
2707 sdp->sd_indirhashsize = i - 1;
2708 for (i = 0; i <= sdp->sd_indirhashsize; i++)
2709 TAILQ_INIT(&sdp->sd_indirhash[i]);
2710 for (i = 0; i <= D_LAST; i++)
2711 LIST_INIT(&sdp->sd_alldeps[i]);
2712 ACQUIRE_GBLLOCK(&lk);
2713 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2714 FREE_GBLLOCK(&lk);
2715
2716 ump->um_softdep = sdp;
2717 MNT_ILOCK(mp);
2718 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2719 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2720 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2721 MNTK_SOFTDEP | MNTK_NOASYNC;
2722 }
2723 MNT_IUNLOCK(mp);
2724
2725 if ((fs->fs_flags & FS_SUJ) &&
2726 (error = journal_mount(mp, fs, cred)) != 0) {
2727 printf("Failed to start journal: %d\n", error);
2728 softdep_unmount(mp);
2729 return (error);
2730 }
2731 /*
2732 * Start our flushing thread in the bufdaemon process.
2733 */
2734 ACQUIRE_LOCK(ump);
2735 ump->softdep_flags |= FLUSH_STARTING;
2736 FREE_LOCK(ump);
2737 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2738 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2739 mp->mnt_stat.f_mntonname);
2740 ACQUIRE_LOCK(ump);
2741 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2742 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2743 hz / 2);
2744 }
2745 FREE_LOCK(ump);
2746 /*
2747 * When doing soft updates, the counters in the
2748 * superblock may have gotten out of sync. Recomputation
2749 * can take a long time and can be deferred for background
2750 * fsck. However, the old behavior of scanning the cylinder
2751 * groups and recalculating them at mount time is available
2752 * by setting vfs.ffs.compute_summary_at_mount to one.
2753 */
2754 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2755 return (0);
2756 bzero(&cstotal, sizeof cstotal);
2757 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2758 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2759 fs->fs_cgsize, cred, &bp)) != 0) {
2760 brelse(bp);
2761 softdep_unmount(mp);
2762 return (error);
2763 }
2764 cgp = (struct cg *)bp->b_data;
2765 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2766 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2767 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2768 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2769 fs->fs_cs(fs, cyl) = cgp->cg_cs;
2770 brelse(bp);
2771 }
2772 #ifdef INVARIANTS
2773 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2774 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2775 #endif
2776 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2777 return (0);
2778 }
2779
2780 void
softdep_unmount(mp)2781 softdep_unmount(mp)
2782 struct mount *mp;
2783 {
2784 struct ufsmount *ump;
2785 struct mount_softdeps *ums;
2786
2787 ump = VFSTOUFS(mp);
2788 KASSERT(ump->um_softdep != NULL,
2789 ("softdep_unmount called on non-softdep filesystem"));
2790 MNT_ILOCK(mp);
2791 mp->mnt_flag &= ~MNT_SOFTDEP;
2792 if ((mp->mnt_flag & MNT_SUJ) == 0) {
2793 MNT_IUNLOCK(mp);
2794 } else {
2795 mp->mnt_flag &= ~MNT_SUJ;
2796 MNT_IUNLOCK(mp);
2797 journal_unmount(ump);
2798 }
2799 /*
2800 * Shut down our flushing thread. Check for NULL is if
2801 * softdep_mount errors out before the thread has been created.
2802 */
2803 if (ump->softdep_flushtd != NULL) {
2804 ACQUIRE_LOCK(ump);
2805 ump->softdep_flags |= FLUSH_EXIT;
2806 wakeup(&ump->softdep_flushtd);
2807 while ((ump->softdep_flags & FLUSH_EXIT) != 0) {
2808 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM,
2809 "sdwait", 0);
2810 }
2811 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2812 ("Thread shutdown failed"));
2813 FREE_LOCK(ump);
2814 }
2815
2816 /*
2817 * We are no longer have softdep structure attached to ump.
2818 */
2819 ums = ump->um_softdep;
2820 ACQUIRE_GBLLOCK(&lk);
2821 TAILQ_REMOVE(&softdepmounts, ums, sd_next);
2822 FREE_GBLLOCK(&lk);
2823 ump->um_softdep = NULL;
2824
2825 KASSERT(ums->sd_on_journal == 0,
2826 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal));
2827 KASSERT(ums->sd_on_worklist == 0,
2828 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist));
2829 KASSERT(ums->sd_deps == 0,
2830 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps));
2831
2832 /*
2833 * Free up our resources.
2834 */
2835 rw_destroy(&ums->sd_fslock);
2836 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize);
2837 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize);
2838 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize);
2839 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize);
2840 free(ums->sd_indirhash, M_FREEWORK);
2841 #ifdef INVARIANTS
2842 for (int i = 0; i <= D_LAST; i++) {
2843 KASSERT(ums->sd_curdeps[i] == 0,
2844 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2845 TYPENAME(i), ums->sd_curdeps[i]));
2846 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]),
2847 ("Unmount %s: Dep type %s not empty (%p)",
2848 ump->um_fs->fs_fsmnt,
2849 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i])));
2850 }
2851 #endif
2852 free(ums, M_MOUNTDATA);
2853 }
2854
2855 static struct jblocks *
jblocks_create(void)2856 jblocks_create(void)
2857 {
2858 struct jblocks *jblocks;
2859
2860 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2861 TAILQ_INIT(&jblocks->jb_segs);
2862 jblocks->jb_avail = 10;
2863 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2864 M_JBLOCKS, M_WAITOK | M_ZERO);
2865
2866 return (jblocks);
2867 }
2868
2869 static ufs2_daddr_t
jblocks_alloc(jblocks,bytes,actual)2870 jblocks_alloc(jblocks, bytes, actual)
2871 struct jblocks *jblocks;
2872 int bytes;
2873 int *actual;
2874 {
2875 ufs2_daddr_t daddr;
2876 struct jextent *jext;
2877 int freecnt;
2878 int blocks;
2879
2880 blocks = bytes / DEV_BSIZE;
2881 jext = &jblocks->jb_extent[jblocks->jb_head];
2882 freecnt = jext->je_blocks - jblocks->jb_off;
2883 if (freecnt == 0) {
2884 jblocks->jb_off = 0;
2885 if (++jblocks->jb_head > jblocks->jb_used)
2886 jblocks->jb_head = 0;
2887 jext = &jblocks->jb_extent[jblocks->jb_head];
2888 freecnt = jext->je_blocks;
2889 }
2890 if (freecnt > blocks)
2891 freecnt = blocks;
2892 *actual = freecnt * DEV_BSIZE;
2893 daddr = jext->je_daddr + jblocks->jb_off;
2894 jblocks->jb_off += freecnt;
2895 jblocks->jb_free -= freecnt;
2896
2897 return (daddr);
2898 }
2899
2900 static void
jblocks_free(jblocks,mp,bytes)2901 jblocks_free(jblocks, mp, bytes)
2902 struct jblocks *jblocks;
2903 struct mount *mp;
2904 int bytes;
2905 {
2906
2907 LOCK_OWNED(VFSTOUFS(mp));
2908 jblocks->jb_free += bytes / DEV_BSIZE;
2909 if (jblocks->jb_suspended)
2910 worklist_speedup(mp);
2911 wakeup(jblocks);
2912 }
2913
2914 static void
jblocks_destroy(jblocks)2915 jblocks_destroy(jblocks)
2916 struct jblocks *jblocks;
2917 {
2918
2919 if (jblocks->jb_extent)
2920 free(jblocks->jb_extent, M_JBLOCKS);
2921 free(jblocks, M_JBLOCKS);
2922 }
2923
2924 static void
jblocks_add(jblocks,daddr,blocks)2925 jblocks_add(jblocks, daddr, blocks)
2926 struct jblocks *jblocks;
2927 ufs2_daddr_t daddr;
2928 int blocks;
2929 {
2930 struct jextent *jext;
2931
2932 jblocks->jb_blocks += blocks;
2933 jblocks->jb_free += blocks;
2934 jext = &jblocks->jb_extent[jblocks->jb_used];
2935 /* Adding the first block. */
2936 if (jext->je_daddr == 0) {
2937 jext->je_daddr = daddr;
2938 jext->je_blocks = blocks;
2939 return;
2940 }
2941 /* Extending the last extent. */
2942 if (jext->je_daddr + jext->je_blocks == daddr) {
2943 jext->je_blocks += blocks;
2944 return;
2945 }
2946 /* Adding a new extent. */
2947 if (++jblocks->jb_used == jblocks->jb_avail) {
2948 jblocks->jb_avail *= 2;
2949 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2950 M_JBLOCKS, M_WAITOK | M_ZERO);
2951 memcpy(jext, jblocks->jb_extent,
2952 sizeof(struct jextent) * jblocks->jb_used);
2953 free(jblocks->jb_extent, M_JBLOCKS);
2954 jblocks->jb_extent = jext;
2955 }
2956 jext = &jblocks->jb_extent[jblocks->jb_used];
2957 jext->je_daddr = daddr;
2958 jext->je_blocks = blocks;
2959 return;
2960 }
2961
2962 int
softdep_journal_lookup(mp,vpp)2963 softdep_journal_lookup(mp, vpp)
2964 struct mount *mp;
2965 struct vnode **vpp;
2966 {
2967 struct componentname cnp;
2968 struct vnode *dvp;
2969 ino_t sujournal;
2970 int error;
2971
2972 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2973 if (error)
2974 return (error);
2975 bzero(&cnp, sizeof(cnp));
2976 cnp.cn_nameiop = LOOKUP;
2977 cnp.cn_flags = ISLASTCN;
2978 cnp.cn_thread = curthread;
2979 cnp.cn_cred = curthread->td_ucred;
2980 cnp.cn_pnbuf = SUJ_FILE;
2981 cnp.cn_nameptr = SUJ_FILE;
2982 cnp.cn_namelen = strlen(SUJ_FILE);
2983 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2984 vput(dvp);
2985 if (error != 0)
2986 return (error);
2987 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2988 return (error);
2989 }
2990
2991 /*
2992 * Open and verify the journal file.
2993 */
2994 static int
journal_mount(mp,fs,cred)2995 journal_mount(mp, fs, cred)
2996 struct mount *mp;
2997 struct fs *fs;
2998 struct ucred *cred;
2999 {
3000 struct jblocks *jblocks;
3001 struct ufsmount *ump;
3002 struct vnode *vp;
3003 struct inode *ip;
3004 ufs2_daddr_t blkno;
3005 int bcount;
3006 int error;
3007 int i;
3008
3009 ump = VFSTOUFS(mp);
3010 ump->softdep_journal_tail = NULL;
3011 ump->softdep_on_journal = 0;
3012 ump->softdep_accdeps = 0;
3013 ump->softdep_req = 0;
3014 ump->softdep_jblocks = NULL;
3015 error = softdep_journal_lookup(mp, &vp);
3016 if (error != 0) {
3017 printf("Failed to find journal. Use tunefs to create one\n");
3018 return (error);
3019 }
3020 ip = VTOI(vp);
3021 if (ip->i_size < SUJ_MIN) {
3022 error = ENOSPC;
3023 goto out;
3024 }
3025 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */
3026 jblocks = jblocks_create();
3027 for (i = 0; i < bcount; i++) {
3028 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
3029 if (error)
3030 break;
3031 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
3032 }
3033 if (error) {
3034 jblocks_destroy(jblocks);
3035 goto out;
3036 }
3037 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */
3038 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
3039 ump->softdep_jblocks = jblocks;
3040
3041 MNT_ILOCK(mp);
3042 mp->mnt_flag |= MNT_SUJ;
3043 MNT_IUNLOCK(mp);
3044
3045 /*
3046 * Only validate the journal contents if the
3047 * filesystem is clean, otherwise we write the logs
3048 * but they'll never be used. If the filesystem was
3049 * still dirty when we mounted it the journal is
3050 * invalid and a new journal can only be valid if it
3051 * starts from a clean mount.
3052 */
3053 if (fs->fs_clean) {
3054 DIP_SET(ip, i_modrev, fs->fs_mtime);
3055 ip->i_flags |= IN_MODIFIED;
3056 ffs_update(vp, 1);
3057 }
3058 out:
3059 vput(vp);
3060 return (error);
3061 }
3062
3063 static void
journal_unmount(ump)3064 journal_unmount(ump)
3065 struct ufsmount *ump;
3066 {
3067
3068 if (ump->softdep_jblocks)
3069 jblocks_destroy(ump->softdep_jblocks);
3070 ump->softdep_jblocks = NULL;
3071 }
3072
3073 /*
3074 * Called when a journal record is ready to be written. Space is allocated
3075 * and the journal entry is created when the journal is flushed to stable
3076 * store.
3077 */
3078 static void
add_to_journal(wk)3079 add_to_journal(wk)
3080 struct worklist *wk;
3081 {
3082 struct ufsmount *ump;
3083
3084 ump = VFSTOUFS(wk->wk_mp);
3085 LOCK_OWNED(ump);
3086 if (wk->wk_state & ONWORKLIST)
3087 panic("add_to_journal: %s(0x%X) already on list",
3088 TYPENAME(wk->wk_type), wk->wk_state);
3089 wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
3090 if (LIST_EMPTY(&ump->softdep_journal_pending)) {
3091 ump->softdep_jblocks->jb_age = ticks;
3092 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
3093 } else
3094 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
3095 ump->softdep_journal_tail = wk;
3096 ump->softdep_on_journal += 1;
3097 }
3098
3099 /*
3100 * Remove an arbitrary item for the journal worklist maintain the tail
3101 * pointer. This happens when a new operation obviates the need to
3102 * journal an old operation.
3103 */
3104 static void
remove_from_journal(wk)3105 remove_from_journal(wk)
3106 struct worklist *wk;
3107 {
3108 struct ufsmount *ump;
3109
3110 ump = VFSTOUFS(wk->wk_mp);
3111 LOCK_OWNED(ump);
3112 #ifdef INVARIANTS
3113 {
3114 struct worklist *wkn;
3115
3116 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
3117 if (wkn == wk)
3118 break;
3119 if (wkn == NULL)
3120 panic("remove_from_journal: %p is not in journal", wk);
3121 }
3122 #endif
3123 /*
3124 * We emulate a TAILQ to save space in most structures which do not
3125 * require TAILQ semantics. Here we must update the tail position
3126 * when removing the tail which is not the final entry. This works
3127 * only if the worklist linkage are at the beginning of the structure.
3128 */
3129 if (ump->softdep_journal_tail == wk)
3130 ump->softdep_journal_tail =
3131 (struct worklist *)wk->wk_list.le_prev;
3132 WORKLIST_REMOVE(wk);
3133 ump->softdep_on_journal -= 1;
3134 }
3135
3136 /*
3137 * Check for journal space as well as dependency limits so the prelink
3138 * code can throttle both journaled and non-journaled filesystems.
3139 * Threshold is 0 for low and 1 for min.
3140 */
3141 static int
journal_space(ump,thresh)3142 journal_space(ump, thresh)
3143 struct ufsmount *ump;
3144 int thresh;
3145 {
3146 struct jblocks *jblocks;
3147 int limit, avail;
3148
3149 jblocks = ump->softdep_jblocks;
3150 if (jblocks == NULL)
3151 return (1);
3152 /*
3153 * We use a tighter restriction here to prevent request_cleanup()
3154 * running in threads from running into locks we currently hold.
3155 * We have to be over the limit and our filesystem has to be
3156 * responsible for more than our share of that usage.
3157 */
3158 limit = (max_softdeps / 10) * 9;
3159 if (dep_current[D_INODEDEP] > limit &&
3160 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
3161 return (0);
3162 if (thresh)
3163 thresh = jblocks->jb_min;
3164 else
3165 thresh = jblocks->jb_low;
3166 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
3167 avail = jblocks->jb_free - avail;
3168
3169 return (avail > thresh);
3170 }
3171
3172 static void
journal_suspend(ump)3173 journal_suspend(ump)
3174 struct ufsmount *ump;
3175 {
3176 struct jblocks *jblocks;
3177 struct mount *mp;
3178 bool set;
3179
3180 mp = UFSTOVFS(ump);
3181 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0)
3182 return;
3183
3184 jblocks = ump->softdep_jblocks;
3185 vfs_op_enter(mp);
3186 set = false;
3187 MNT_ILOCK(mp);
3188 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
3189 stat_journal_min++;
3190 mp->mnt_kern_flag |= MNTK_SUSPEND;
3191 mp->mnt_susp_owner = ump->softdep_flushtd;
3192 set = true;
3193 }
3194 jblocks->jb_suspended = 1;
3195 MNT_IUNLOCK(mp);
3196 if (!set)
3197 vfs_op_exit(mp);
3198 }
3199
3200 static int
journal_unsuspend(struct ufsmount * ump)3201 journal_unsuspend(struct ufsmount *ump)
3202 {
3203 struct jblocks *jblocks;
3204 struct mount *mp;
3205
3206 mp = UFSTOVFS(ump);
3207 jblocks = ump->softdep_jblocks;
3208
3209 if (jblocks != NULL && jblocks->jb_suspended &&
3210 journal_space(ump, jblocks->jb_min)) {
3211 jblocks->jb_suspended = 0;
3212 FREE_LOCK(ump);
3213 mp->mnt_susp_owner = curthread;
3214 vfs_write_resume(mp, 0);
3215 ACQUIRE_LOCK(ump);
3216 return (1);
3217 }
3218 return (0);
3219 }
3220
3221 static void
journal_check_space(ump)3222 journal_check_space(ump)
3223 struct ufsmount *ump;
3224 {
3225 struct mount *mp;
3226
3227 LOCK_OWNED(ump);
3228
3229 if (journal_space(ump, 0) == 0) {
3230 softdep_speedup(ump);
3231 mp = UFSTOVFS(ump);
3232 FREE_LOCK(ump);
3233 VFS_SYNC(mp, MNT_NOWAIT);
3234 ffs_sbupdate(ump, MNT_WAIT, 0);
3235 ACQUIRE_LOCK(ump);
3236 if (journal_space(ump, 1) == 0)
3237 journal_suspend(ump);
3238 }
3239 }
3240
3241 /*
3242 * Called before any allocation function to be certain that there is
3243 * sufficient space in the journal prior to creating any new records.
3244 * Since in the case of block allocation we may have multiple locked
3245 * buffers at the time of the actual allocation we can not block
3246 * when the journal records are created. Doing so would create a deadlock
3247 * if any of these buffers needed to be flushed to reclaim space. Instead
3248 * we require a sufficiently large amount of available space such that
3249 * each thread in the system could have passed this allocation check and
3250 * still have sufficient free space. With 20% of a minimum journal size
3251 * of 1MB we have 6553 records available.
3252 */
3253 int
softdep_prealloc(vp,waitok)3254 softdep_prealloc(vp, waitok)
3255 struct vnode *vp;
3256 int waitok;
3257 {
3258 struct ufsmount *ump;
3259
3260 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3261 ("softdep_prealloc called on non-softdep filesystem"));
3262 /*
3263 * Nothing to do if we are not running journaled soft updates.
3264 * If we currently hold the snapshot lock, we must avoid
3265 * handling other resources that could cause deadlock. Do not
3266 * touch quotas vnode since it is typically recursed with
3267 * other vnode locks held.
3268 */
3269 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3270 (vp->v_vflag & VV_SYSTEM) != 0)
3271 return (0);
3272 ump = VFSTOUFS(vp->v_mount);
3273 ACQUIRE_LOCK(ump);
3274 if (journal_space(ump, 0)) {
3275 FREE_LOCK(ump);
3276 return (0);
3277 }
3278 stat_journal_low++;
3279 FREE_LOCK(ump);
3280 if (waitok == MNT_NOWAIT)
3281 return (ENOSPC);
3282 /*
3283 * Attempt to sync this vnode once to flush any journal
3284 * work attached to it.
3285 */
3286 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3287 ffs_syncvnode(vp, waitok, 0);
3288 ACQUIRE_LOCK(ump);
3289 process_removes(vp);
3290 process_truncates(vp);
3291 journal_check_space(ump);
3292 FREE_LOCK(ump);
3293
3294 return (0);
3295 }
3296
3297 /*
3298 * Try hard to sync all data and metadata for the vnode, and workitems
3299 * flushing which might conflict with the vnode lock. This is a
3300 * helper for softdep_prerename().
3301 */
3302 static int
softdep_prerename_vnode(ump,vp)3303 softdep_prerename_vnode(ump, vp)
3304 struct ufsmount *ump;
3305 struct vnode *vp;
3306 {
3307 int error;
3308
3309 ASSERT_VOP_ELOCKED(vp, "prehandle");
3310 if (vp->v_data == NULL)
3311 return (0);
3312 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3313 if (error != 0)
3314 return (error);
3315 ACQUIRE_LOCK(ump);
3316 process_removes(vp);
3317 process_truncates(vp);
3318 FREE_LOCK(ump);
3319 return (0);
3320 }
3321
3322 /*
3323 * Must be called from VOP_RENAME() after all vnodes are locked.
3324 * Ensures that there is enough journal space for rename. It is
3325 * sufficiently different from softdep_prelink() by having to handle
3326 * four vnodes.
3327 */
3328 int
softdep_prerename(fdvp,fvp,tdvp,tvp)3329 softdep_prerename(fdvp, fvp, tdvp, tvp)
3330 struct vnode *fdvp;
3331 struct vnode *fvp;
3332 struct vnode *tdvp;
3333 struct vnode *tvp;
3334 {
3335 struct ufsmount *ump;
3336 int error;
3337
3338 ump = VFSTOUFS(fdvp->v_mount);
3339
3340 if (journal_space(ump, 0))
3341 return (0);
3342
3343 VOP_UNLOCK(tdvp);
3344 VOP_UNLOCK(fvp);
3345 if (tvp != NULL && tvp != tdvp)
3346 VOP_UNLOCK(tvp);
3347
3348 error = softdep_prerename_vnode(ump, fdvp);
3349 VOP_UNLOCK(fdvp);
3350 if (error != 0)
3351 return (error);
3352
3353 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY);
3354 error = softdep_prerename_vnode(ump, fvp);
3355 VOP_UNLOCK(fvp);
3356 if (error != 0)
3357 return (error);
3358
3359 if (tdvp != fdvp) {
3360 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY);
3361 error = softdep_prerename_vnode(ump, tdvp);
3362 VOP_UNLOCK(tdvp);
3363 if (error != 0)
3364 return (error);
3365 }
3366
3367 if (tvp != fvp && tvp != NULL) {
3368 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY);
3369 error = softdep_prerename_vnode(ump, tvp);
3370 VOP_UNLOCK(tvp);
3371 if (error != 0)
3372 return (error);
3373 }
3374
3375 ACQUIRE_LOCK(ump);
3376 softdep_speedup(ump);
3377 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3378 journal_check_space(ump);
3379 FREE_LOCK(ump);
3380 return (ERELOOKUP);
3381 }
3382
3383 /*
3384 * Before adjusting a link count on a vnode verify that we have sufficient
3385 * journal space. If not, process operations that depend on the currently
3386 * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3387 * and softdep flush threads can not acquire these locks to reclaim space.
3388 *
3389 * Returns 0 if all owned locks are still valid and were not dropped
3390 * in the process, in other case it returns either an error from sync,
3391 * or ERELOOKUP if any of the locks were re-acquired. In the later
3392 * case, the state of the vnodes cannot be relied upon and our VFS
3393 * syscall must be restarted at top level from the lookup.
3394 */
3395 int
softdep_prelink(dvp,vp,cnp)3396 softdep_prelink(dvp, vp, cnp)
3397 struct vnode *dvp;
3398 struct vnode *vp;
3399 struct componentname *cnp;
3400 {
3401 struct ufsmount *ump;
3402 struct nameidata *ndp;
3403
3404 ASSERT_VOP_ELOCKED(dvp, "prelink dvp");
3405 if (vp != NULL)
3406 ASSERT_VOP_ELOCKED(vp, "prelink vp");
3407 ump = VFSTOUFS(dvp->v_mount);
3408
3409 /*
3410 * Nothing to do if we have sufficient journal space. We skip
3411 * flushing when vp is a snapshot to avoid deadlock where
3412 * another thread is trying to update the inodeblock for dvp
3413 * and is waiting on snaplk that vp holds.
3414 */
3415 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp))))
3416 return (0);
3417
3418 /*
3419 * Check if the journal space consumption can in theory be
3420 * accounted on dvp and vp. If the vnodes metadata was not
3421 * changed comparing with the previous round-trip into
3422 * softdep_prelink(), as indicated by the seqc generation
3423 * recorded in the nameidata, then there is no point in
3424 * starting the sync.
3425 */
3426 ndp = __containerof(cnp, struct nameidata, ni_cnd);
3427 if (!seqc_in_modify(ndp->ni_dvp_seqc) &&
3428 vn_seqc_consistent(dvp, ndp->ni_dvp_seqc) &&
3429 (vp == NULL || (!seqc_in_modify(ndp->ni_vp_seqc) &&
3430 vn_seqc_consistent(vp, ndp->ni_vp_seqc))))
3431 return (0);
3432
3433 stat_journal_low++;
3434 if (vp != NULL) {
3435 VOP_UNLOCK(dvp);
3436 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3437 vn_lock_pair(dvp, false, vp, true);
3438 if (dvp->v_data == NULL)
3439 goto out;
3440 }
3441 if (vp != NULL)
3442 VOP_UNLOCK(vp);
3443 ffs_syncvnode(dvp, MNT_WAIT, 0);
3444 /* Process vp before dvp as it may create .. removes. */
3445 if (vp != NULL) {
3446 VOP_UNLOCK(dvp);
3447 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3448 if (vp->v_data == NULL) {
3449 vn_lock_pair(dvp, false, vp, true);
3450 goto out;
3451 }
3452 ACQUIRE_LOCK(ump);
3453 process_removes(vp);
3454 process_truncates(vp);
3455 FREE_LOCK(ump);
3456 VOP_UNLOCK(vp);
3457 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
3458 if (dvp->v_data == NULL) {
3459 vn_lock_pair(dvp, true, vp, false);
3460 goto out;
3461 }
3462 }
3463
3464 ACQUIRE_LOCK(ump);
3465 process_removes(dvp);
3466 process_truncates(dvp);
3467 VOP_UNLOCK(dvp);
3468 softdep_speedup(ump);
3469
3470 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3471 journal_check_space(ump);
3472 FREE_LOCK(ump);
3473
3474 vn_lock_pair(dvp, false, vp, false);
3475 out:
3476 ndp->ni_dvp_seqc = vn_seqc_read_any(dvp);
3477 if (vp != NULL)
3478 ndp->ni_vp_seqc = vn_seqc_read_any(vp);
3479 return (ERELOOKUP);
3480 }
3481
3482 static void
jseg_write(ump,jseg,data)3483 jseg_write(ump, jseg, data)
3484 struct ufsmount *ump;
3485 struct jseg *jseg;
3486 uint8_t *data;
3487 {
3488 struct jsegrec *rec;
3489
3490 rec = (struct jsegrec *)data;
3491 rec->jsr_seq = jseg->js_seq;
3492 rec->jsr_oldest = jseg->js_oldseq;
3493 rec->jsr_cnt = jseg->js_cnt;
3494 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3495 rec->jsr_crc = 0;
3496 rec->jsr_time = ump->um_fs->fs_mtime;
3497 }
3498
3499 static inline void
inoref_write(inoref,jseg,rec)3500 inoref_write(inoref, jseg, rec)
3501 struct inoref *inoref;
3502 struct jseg *jseg;
3503 struct jrefrec *rec;
3504 {
3505
3506 inoref->if_jsegdep->jd_seg = jseg;
3507 rec->jr_ino = inoref->if_ino;
3508 rec->jr_parent = inoref->if_parent;
3509 rec->jr_nlink = inoref->if_nlink;
3510 rec->jr_mode = inoref->if_mode;
3511 rec->jr_diroff = inoref->if_diroff;
3512 }
3513
3514 static void
jaddref_write(jaddref,jseg,data)3515 jaddref_write(jaddref, jseg, data)
3516 struct jaddref *jaddref;
3517 struct jseg *jseg;
3518 uint8_t *data;
3519 {
3520 struct jrefrec *rec;
3521
3522 rec = (struct jrefrec *)data;
3523 rec->jr_op = JOP_ADDREF;
3524 inoref_write(&jaddref->ja_ref, jseg, rec);
3525 }
3526
3527 static void
jremref_write(jremref,jseg,data)3528 jremref_write(jremref, jseg, data)
3529 struct jremref *jremref;
3530 struct jseg *jseg;
3531 uint8_t *data;
3532 {
3533 struct jrefrec *rec;
3534
3535 rec = (struct jrefrec *)data;
3536 rec->jr_op = JOP_REMREF;
3537 inoref_write(&jremref->jr_ref, jseg, rec);
3538 }
3539
3540 static void
jmvref_write(jmvref,jseg,data)3541 jmvref_write(jmvref, jseg, data)
3542 struct jmvref *jmvref;
3543 struct jseg *jseg;
3544 uint8_t *data;
3545 {
3546 struct jmvrec *rec;
3547
3548 rec = (struct jmvrec *)data;
3549 rec->jm_op = JOP_MVREF;
3550 rec->jm_ino = jmvref->jm_ino;
3551 rec->jm_parent = jmvref->jm_parent;
3552 rec->jm_oldoff = jmvref->jm_oldoff;
3553 rec->jm_newoff = jmvref->jm_newoff;
3554 }
3555
3556 static void
jnewblk_write(jnewblk,jseg,data)3557 jnewblk_write(jnewblk, jseg, data)
3558 struct jnewblk *jnewblk;
3559 struct jseg *jseg;
3560 uint8_t *data;
3561 {
3562 struct jblkrec *rec;
3563
3564 jnewblk->jn_jsegdep->jd_seg = jseg;
3565 rec = (struct jblkrec *)data;
3566 rec->jb_op = JOP_NEWBLK;
3567 rec->jb_ino = jnewblk->jn_ino;
3568 rec->jb_blkno = jnewblk->jn_blkno;
3569 rec->jb_lbn = jnewblk->jn_lbn;
3570 rec->jb_frags = jnewblk->jn_frags;
3571 rec->jb_oldfrags = jnewblk->jn_oldfrags;
3572 }
3573
3574 static void
jfreeblk_write(jfreeblk,jseg,data)3575 jfreeblk_write(jfreeblk, jseg, data)
3576 struct jfreeblk *jfreeblk;
3577 struct jseg *jseg;
3578 uint8_t *data;
3579 {
3580 struct jblkrec *rec;
3581
3582 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3583 rec = (struct jblkrec *)data;
3584 rec->jb_op = JOP_FREEBLK;
3585 rec->jb_ino = jfreeblk->jf_ino;
3586 rec->jb_blkno = jfreeblk->jf_blkno;
3587 rec->jb_lbn = jfreeblk->jf_lbn;
3588 rec->jb_frags = jfreeblk->jf_frags;
3589 rec->jb_oldfrags = 0;
3590 }
3591
3592 static void
jfreefrag_write(jfreefrag,jseg,data)3593 jfreefrag_write(jfreefrag, jseg, data)
3594 struct jfreefrag *jfreefrag;
3595 struct jseg *jseg;
3596 uint8_t *data;
3597 {
3598 struct jblkrec *rec;
3599
3600 jfreefrag->fr_jsegdep->jd_seg = jseg;
3601 rec = (struct jblkrec *)data;
3602 rec->jb_op = JOP_FREEBLK;
3603 rec->jb_ino = jfreefrag->fr_ino;
3604 rec->jb_blkno = jfreefrag->fr_blkno;
3605 rec->jb_lbn = jfreefrag->fr_lbn;
3606 rec->jb_frags = jfreefrag->fr_frags;
3607 rec->jb_oldfrags = 0;
3608 }
3609
3610 static void
jtrunc_write(jtrunc,jseg,data)3611 jtrunc_write(jtrunc, jseg, data)
3612 struct jtrunc *jtrunc;
3613 struct jseg *jseg;
3614 uint8_t *data;
3615 {
3616 struct jtrncrec *rec;
3617
3618 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3619 rec = (struct jtrncrec *)data;
3620 rec->jt_op = JOP_TRUNC;
3621 rec->jt_ino = jtrunc->jt_ino;
3622 rec->jt_size = jtrunc->jt_size;
3623 rec->jt_extsize = jtrunc->jt_extsize;
3624 }
3625
3626 static void
jfsync_write(jfsync,jseg,data)3627 jfsync_write(jfsync, jseg, data)
3628 struct jfsync *jfsync;
3629 struct jseg *jseg;
3630 uint8_t *data;
3631 {
3632 struct jtrncrec *rec;
3633
3634 rec = (struct jtrncrec *)data;
3635 rec->jt_op = JOP_SYNC;
3636 rec->jt_ino = jfsync->jfs_ino;
3637 rec->jt_size = jfsync->jfs_size;
3638 rec->jt_extsize = jfsync->jfs_extsize;
3639 }
3640
3641 static void
softdep_flushjournal(mp)3642 softdep_flushjournal(mp)
3643 struct mount *mp;
3644 {
3645 struct jblocks *jblocks;
3646 struct ufsmount *ump;
3647
3648 if (MOUNTEDSUJ(mp) == 0)
3649 return;
3650 ump = VFSTOUFS(mp);
3651 jblocks = ump->softdep_jblocks;
3652 ACQUIRE_LOCK(ump);
3653 while (ump->softdep_on_journal) {
3654 jblocks->jb_needseg = 1;
3655 softdep_process_journal(mp, NULL, MNT_WAIT);
3656 }
3657 FREE_LOCK(ump);
3658 }
3659
3660 static void softdep_synchronize_completed(struct bio *);
3661 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3662
3663 static void
softdep_synchronize_completed(bp)3664 softdep_synchronize_completed(bp)
3665 struct bio *bp;
3666 {
3667 struct jseg *oldest;
3668 struct jseg *jseg;
3669 struct ufsmount *ump;
3670
3671 /*
3672 * caller1 marks the last segment written before we issued the
3673 * synchronize cache.
3674 */
3675 jseg = bp->bio_caller1;
3676 if (jseg == NULL) {
3677 g_destroy_bio(bp);
3678 return;
3679 }
3680 ump = VFSTOUFS(jseg->js_list.wk_mp);
3681 ACQUIRE_LOCK(ump);
3682 oldest = NULL;
3683 /*
3684 * Mark all the journal entries waiting on the synchronize cache
3685 * as completed so they may continue on.
3686 */
3687 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3688 jseg->js_state |= COMPLETE;
3689 oldest = jseg;
3690 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3691 }
3692 /*
3693 * Restart deferred journal entry processing from the oldest
3694 * completed jseg.
3695 */
3696 if (oldest)
3697 complete_jsegs(oldest);
3698
3699 FREE_LOCK(ump);
3700 g_destroy_bio(bp);
3701 }
3702
3703 /*
3704 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3705 * barriers. The journal must be written prior to any blocks that depend
3706 * on it and the journal can not be released until the blocks have be
3707 * written. This code handles both barriers simultaneously.
3708 */
3709 static void
softdep_synchronize(bp,ump,caller1)3710 softdep_synchronize(bp, ump, caller1)
3711 struct bio *bp;
3712 struct ufsmount *ump;
3713 void *caller1;
3714 {
3715
3716 bp->bio_cmd = BIO_FLUSH;
3717 bp->bio_flags |= BIO_ORDERED;
3718 bp->bio_data = NULL;
3719 bp->bio_offset = ump->um_cp->provider->mediasize;
3720 bp->bio_length = 0;
3721 bp->bio_done = softdep_synchronize_completed;
3722 bp->bio_caller1 = caller1;
3723 g_io_request(bp, ump->um_cp);
3724 }
3725
3726 /*
3727 * Flush some journal records to disk.
3728 */
3729 static void
softdep_process_journal(mp,needwk,flags)3730 softdep_process_journal(mp, needwk, flags)
3731 struct mount *mp;
3732 struct worklist *needwk;
3733 int flags;
3734 {
3735 struct jblocks *jblocks;
3736 struct ufsmount *ump;
3737 struct worklist *wk;
3738 struct jseg *jseg;
3739 struct buf *bp;
3740 struct bio *bio;
3741 uint8_t *data;
3742 struct fs *fs;
3743 int shouldflush;
3744 int segwritten;
3745 int jrecmin; /* Minimum records per block. */
3746 int jrecmax; /* Maximum records per block. */
3747 int size;
3748 int cnt;
3749 int off;
3750 int devbsize;
3751
3752 ump = VFSTOUFS(mp);
3753 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL)
3754 return;
3755 shouldflush = softdep_flushcache;
3756 bio = NULL;
3757 jseg = NULL;
3758 LOCK_OWNED(ump);
3759 fs = ump->um_fs;
3760 jblocks = ump->softdep_jblocks;
3761 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3762 /*
3763 * We write anywhere between a disk block and fs block. The upper
3764 * bound is picked to prevent buffer cache fragmentation and limit
3765 * processing time per I/O.
3766 */
3767 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3768 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3769 segwritten = 0;
3770 for (;;) {
3771 cnt = ump->softdep_on_journal;
3772 /*
3773 * Criteria for writing a segment:
3774 * 1) We have a full block.
3775 * 2) We're called from jwait() and haven't found the
3776 * journal item yet.
3777 * 3) Always write if needseg is set.
3778 * 4) If we are called from process_worklist and have
3779 * not yet written anything we write a partial block
3780 * to enforce a 1 second maximum latency on journal
3781 * entries.
3782 */
3783 if (cnt < (jrecmax - 1) && needwk == NULL &&
3784 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3785 break;
3786 cnt++;
3787 /*
3788 * Verify some free journal space. softdep_prealloc() should
3789 * guarantee that we don't run out so this is indicative of
3790 * a problem with the flow control. Try to recover
3791 * gracefully in any event.
3792 */
3793 while (jblocks->jb_free == 0) {
3794 if (flags != MNT_WAIT)
3795 break;
3796 printf("softdep: Out of journal space!\n");
3797 softdep_speedup(ump);
3798 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3799 }
3800 FREE_LOCK(ump);
3801 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3802 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3803 LIST_INIT(&jseg->js_entries);
3804 LIST_INIT(&jseg->js_indirs);
3805 jseg->js_state = ATTACHED;
3806 if (shouldflush == 0)
3807 jseg->js_state |= COMPLETE;
3808 else if (bio == NULL)
3809 bio = g_alloc_bio();
3810 jseg->js_jblocks = jblocks;
3811 bp = geteblk(fs->fs_bsize, 0);
3812 ACQUIRE_LOCK(ump);
3813 /*
3814 * If there was a race while we were allocating the block
3815 * and jseg the entry we care about was likely written.
3816 * We bail out in both the WAIT and NOWAIT case and assume
3817 * the caller will loop if the entry it cares about is
3818 * not written.
3819 */
3820 cnt = ump->softdep_on_journal;
3821 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3822 bp->b_flags |= B_INVAL | B_NOCACHE;
3823 WORKITEM_FREE(jseg, D_JSEG);
3824 FREE_LOCK(ump);
3825 brelse(bp);
3826 ACQUIRE_LOCK(ump);
3827 break;
3828 }
3829 /*
3830 * Calculate the disk block size required for the available
3831 * records rounded to the min size.
3832 */
3833 if (cnt == 0)
3834 size = devbsize;
3835 else if (cnt < jrecmax)
3836 size = howmany(cnt, jrecmin) * devbsize;
3837 else
3838 size = fs->fs_bsize;
3839 /*
3840 * Allocate a disk block for this journal data and account
3841 * for truncation of the requested size if enough contiguous
3842 * space was not available.
3843 */
3844 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3845 bp->b_lblkno = bp->b_blkno;
3846 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3847 bp->b_bcount = size;
3848 bp->b_flags &= ~B_INVAL;
3849 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3850 /*
3851 * Initialize our jseg with cnt records. Assign the next
3852 * sequence number to it and link it in-order.
3853 */
3854 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3855 jseg->js_buf = bp;
3856 jseg->js_cnt = cnt;
3857 jseg->js_refs = cnt + 1; /* Self ref. */
3858 jseg->js_size = size;
3859 jseg->js_seq = jblocks->jb_nextseq++;
3860 if (jblocks->jb_oldestseg == NULL)
3861 jblocks->jb_oldestseg = jseg;
3862 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3863 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3864 if (jblocks->jb_writeseg == NULL)
3865 jblocks->jb_writeseg = jseg;
3866 /*
3867 * Start filling in records from the pending list.
3868 */
3869 data = bp->b_data;
3870 off = 0;
3871
3872 /*
3873 * Always put a header on the first block.
3874 * XXX As with below, there might not be a chance to get
3875 * into the loop. Ensure that something valid is written.
3876 */
3877 jseg_write(ump, jseg, data);
3878 off += JREC_SIZE;
3879 data = bp->b_data + off;
3880
3881 /*
3882 * XXX Something is wrong here. There's no work to do,
3883 * but we need to perform and I/O and allow it to complete
3884 * anyways.
3885 */
3886 if (LIST_EMPTY(&ump->softdep_journal_pending))
3887 stat_emptyjblocks++;
3888
3889 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3890 != NULL) {
3891 if (cnt == 0)
3892 break;
3893 /* Place a segment header on every device block. */
3894 if ((off % devbsize) == 0) {
3895 jseg_write(ump, jseg, data);
3896 off += JREC_SIZE;
3897 data = bp->b_data + off;
3898 }
3899 if (wk == needwk)
3900 needwk = NULL;
3901 remove_from_journal(wk);
3902 wk->wk_state |= INPROGRESS;
3903 WORKLIST_INSERT(&jseg->js_entries, wk);
3904 switch (wk->wk_type) {
3905 case D_JADDREF:
3906 jaddref_write(WK_JADDREF(wk), jseg, data);
3907 break;
3908 case D_JREMREF:
3909 jremref_write(WK_JREMREF(wk), jseg, data);
3910 break;
3911 case D_JMVREF:
3912 jmvref_write(WK_JMVREF(wk), jseg, data);
3913 break;
3914 case D_JNEWBLK:
3915 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3916 break;
3917 case D_JFREEBLK:
3918 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3919 break;
3920 case D_JFREEFRAG:
3921 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3922 break;
3923 case D_JTRUNC:
3924 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3925 break;
3926 case D_JFSYNC:
3927 jfsync_write(WK_JFSYNC(wk), jseg, data);
3928 break;
3929 default:
3930 panic("process_journal: Unknown type %s",
3931 TYPENAME(wk->wk_type));
3932 /* NOTREACHED */
3933 }
3934 off += JREC_SIZE;
3935 data = bp->b_data + off;
3936 cnt--;
3937 }
3938
3939 /* Clear any remaining space so we don't leak kernel data */
3940 if (size > off)
3941 bzero(data, size - off);
3942
3943 /*
3944 * Write this one buffer and continue.
3945 */
3946 segwritten = 1;
3947 jblocks->jb_needseg = 0;
3948 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3949 FREE_LOCK(ump);
3950 bp->b_xflags |= BX_CVTENXIO;
3951 pbgetvp(ump->um_devvp, bp);
3952 /*
3953 * We only do the blocking wait once we find the journal
3954 * entry we're looking for.
3955 */
3956 if (needwk == NULL && flags == MNT_WAIT)
3957 bwrite(bp);
3958 else
3959 bawrite(bp);
3960 ACQUIRE_LOCK(ump);
3961 }
3962 /*
3963 * If we wrote a segment issue a synchronize cache so the journal
3964 * is reflected on disk before the data is written. Since reclaiming
3965 * journal space also requires writing a journal record this
3966 * process also enforces a barrier before reclamation.
3967 */
3968 if (segwritten && shouldflush) {
3969 softdep_synchronize(bio, ump,
3970 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3971 } else if (bio)
3972 g_destroy_bio(bio);
3973 /*
3974 * If we've suspended the filesystem because we ran out of journal
3975 * space either try to sync it here to make some progress or
3976 * unsuspend it if we already have.
3977 */
3978 if (flags == 0 && jblocks->jb_suspended) {
3979 if (journal_unsuspend(ump))
3980 return;
3981 FREE_LOCK(ump);
3982 VFS_SYNC(mp, MNT_NOWAIT);
3983 ffs_sbupdate(ump, MNT_WAIT, 0);
3984 ACQUIRE_LOCK(ump);
3985 }
3986 }
3987
3988 /*
3989 * Complete a jseg, allowing all dependencies awaiting journal writes
3990 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3991 * structures so that the journal segment can be freed to reclaim space.
3992 */
3993 static void
complete_jseg(jseg)3994 complete_jseg(jseg)
3995 struct jseg *jseg;
3996 {
3997 struct worklist *wk;
3998 struct jmvref *jmvref;
3999 #ifdef INVARIANTS
4000 int i = 0;
4001 #endif
4002
4003 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
4004 WORKLIST_REMOVE(wk);
4005 wk->wk_state &= ~INPROGRESS;
4006 wk->wk_state |= COMPLETE;
4007 KASSERT(i++ < jseg->js_cnt,
4008 ("handle_written_jseg: overflow %d >= %d",
4009 i - 1, jseg->js_cnt));
4010 switch (wk->wk_type) {
4011 case D_JADDREF:
4012 handle_written_jaddref(WK_JADDREF(wk));
4013 break;
4014 case D_JREMREF:
4015 handle_written_jremref(WK_JREMREF(wk));
4016 break;
4017 case D_JMVREF:
4018 rele_jseg(jseg); /* No jsegdep. */
4019 jmvref = WK_JMVREF(wk);
4020 LIST_REMOVE(jmvref, jm_deps);
4021 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
4022 free_pagedep(jmvref->jm_pagedep);
4023 WORKITEM_FREE(jmvref, D_JMVREF);
4024 break;
4025 case D_JNEWBLK:
4026 handle_written_jnewblk(WK_JNEWBLK(wk));
4027 break;
4028 case D_JFREEBLK:
4029 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
4030 break;
4031 case D_JTRUNC:
4032 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
4033 break;
4034 case D_JFSYNC:
4035 rele_jseg(jseg); /* No jsegdep. */
4036 WORKITEM_FREE(wk, D_JFSYNC);
4037 break;
4038 case D_JFREEFRAG:
4039 handle_written_jfreefrag(WK_JFREEFRAG(wk));
4040 break;
4041 default:
4042 panic("handle_written_jseg: Unknown type %s",
4043 TYPENAME(wk->wk_type));
4044 /* NOTREACHED */
4045 }
4046 }
4047 /* Release the self reference so the structure may be freed. */
4048 rele_jseg(jseg);
4049 }
4050
4051 /*
4052 * Determine which jsegs are ready for completion processing. Waits for
4053 * synchronize cache to complete as well as forcing in-order completion
4054 * of journal entries.
4055 */
4056 static void
complete_jsegs(jseg)4057 complete_jsegs(jseg)
4058 struct jseg *jseg;
4059 {
4060 struct jblocks *jblocks;
4061 struct jseg *jsegn;
4062
4063 jblocks = jseg->js_jblocks;
4064 /*
4065 * Don't allow out of order completions. If this isn't the first
4066 * block wait for it to write before we're done.
4067 */
4068 if (jseg != jblocks->jb_writeseg)
4069 return;
4070 /* Iterate through available jsegs processing their entries. */
4071 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
4072 jblocks->jb_oldestwrseq = jseg->js_oldseq;
4073 jsegn = TAILQ_NEXT(jseg, js_next);
4074 complete_jseg(jseg);
4075 jseg = jsegn;
4076 }
4077 jblocks->jb_writeseg = jseg;
4078 /*
4079 * Attempt to free jsegs now that oldestwrseq may have advanced.
4080 */
4081 free_jsegs(jblocks);
4082 }
4083
4084 /*
4085 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
4086 * the final completions.
4087 */
4088 static void
handle_written_jseg(jseg,bp)4089 handle_written_jseg(jseg, bp)
4090 struct jseg *jseg;
4091 struct buf *bp;
4092 {
4093
4094 if (jseg->js_refs == 0)
4095 panic("handle_written_jseg: No self-reference on %p", jseg);
4096 jseg->js_state |= DEPCOMPLETE;
4097 /*
4098 * We'll never need this buffer again, set flags so it will be
4099 * discarded.
4100 */
4101 bp->b_flags |= B_INVAL | B_NOCACHE;
4102 pbrelvp(bp);
4103 complete_jsegs(jseg);
4104 }
4105
4106 static inline struct jsegdep *
inoref_jseg(inoref)4107 inoref_jseg(inoref)
4108 struct inoref *inoref;
4109 {
4110 struct jsegdep *jsegdep;
4111
4112 jsegdep = inoref->if_jsegdep;
4113 inoref->if_jsegdep = NULL;
4114
4115 return (jsegdep);
4116 }
4117
4118 /*
4119 * Called once a jremref has made it to stable store. The jremref is marked
4120 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
4121 * for the jremref to complete will be awoken by free_jremref.
4122 */
4123 static void
handle_written_jremref(jremref)4124 handle_written_jremref(jremref)
4125 struct jremref *jremref;
4126 {
4127 struct inodedep *inodedep;
4128 struct jsegdep *jsegdep;
4129 struct dirrem *dirrem;
4130
4131 /* Grab the jsegdep. */
4132 jsegdep = inoref_jseg(&jremref->jr_ref);
4133 /*
4134 * Remove us from the inoref list.
4135 */
4136 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
4137 0, &inodedep) == 0)
4138 panic("handle_written_jremref: Lost inodedep");
4139 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
4140 /*
4141 * Complete the dirrem.
4142 */
4143 dirrem = jremref->jr_dirrem;
4144 jremref->jr_dirrem = NULL;
4145 LIST_REMOVE(jremref, jr_deps);
4146 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
4147 jwork_insert(&dirrem->dm_jwork, jsegdep);
4148 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
4149 (dirrem->dm_state & COMPLETE) != 0)
4150 add_to_worklist(&dirrem->dm_list, 0);
4151 free_jremref(jremref);
4152 }
4153
4154 /*
4155 * Called once a jaddref has made it to stable store. The dependency is
4156 * marked complete and any dependent structures are added to the inode
4157 * bufwait list to be completed as soon as it is written. If a bitmap write
4158 * depends on this entry we move the inode into the inodedephd of the
4159 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
4160 */
4161 static void
handle_written_jaddref(jaddref)4162 handle_written_jaddref(jaddref)
4163 struct jaddref *jaddref;
4164 {
4165 struct jsegdep *jsegdep;
4166 struct inodedep *inodedep;
4167 struct diradd *diradd;
4168 struct mkdir *mkdir;
4169
4170 /* Grab the jsegdep. */
4171 jsegdep = inoref_jseg(&jaddref->ja_ref);
4172 mkdir = NULL;
4173 diradd = NULL;
4174 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4175 0, &inodedep) == 0)
4176 panic("handle_written_jaddref: Lost inodedep.");
4177 if (jaddref->ja_diradd == NULL)
4178 panic("handle_written_jaddref: No dependency");
4179 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
4180 diradd = jaddref->ja_diradd;
4181 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
4182 } else if (jaddref->ja_state & MKDIR_PARENT) {
4183 mkdir = jaddref->ja_mkdir;
4184 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
4185 } else if (jaddref->ja_state & MKDIR_BODY)
4186 mkdir = jaddref->ja_mkdir;
4187 else
4188 panic("handle_written_jaddref: Unknown dependency %p",
4189 jaddref->ja_diradd);
4190 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
4191 /*
4192 * Remove us from the inode list.
4193 */
4194 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
4195 /*
4196 * The mkdir may be waiting on the jaddref to clear before freeing.
4197 */
4198 if (mkdir) {
4199 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
4200 ("handle_written_jaddref: Incorrect type for mkdir %s",
4201 TYPENAME(mkdir->md_list.wk_type)));
4202 mkdir->md_jaddref = NULL;
4203 diradd = mkdir->md_diradd;
4204 mkdir->md_state |= DEPCOMPLETE;
4205 complete_mkdir(mkdir);
4206 }
4207 jwork_insert(&diradd->da_jwork, jsegdep);
4208 if (jaddref->ja_state & NEWBLOCK) {
4209 inodedep->id_state |= ONDEPLIST;
4210 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
4211 inodedep, id_deps);
4212 }
4213 free_jaddref(jaddref);
4214 }
4215
4216 /*
4217 * Called once a jnewblk journal is written. The allocdirect or allocindir
4218 * is placed in the bmsafemap to await notification of a written bitmap. If
4219 * the operation was canceled we add the segdep to the appropriate
4220 * dependency to free the journal space once the canceling operation
4221 * completes.
4222 */
4223 static void
handle_written_jnewblk(jnewblk)4224 handle_written_jnewblk(jnewblk)
4225 struct jnewblk *jnewblk;
4226 {
4227 struct bmsafemap *bmsafemap;
4228 struct freefrag *freefrag;
4229 struct freework *freework;
4230 struct jsegdep *jsegdep;
4231 struct newblk *newblk;
4232
4233 /* Grab the jsegdep. */
4234 jsegdep = jnewblk->jn_jsegdep;
4235 jnewblk->jn_jsegdep = NULL;
4236 if (jnewblk->jn_dep == NULL)
4237 panic("handle_written_jnewblk: No dependency for the segdep.");
4238 switch (jnewblk->jn_dep->wk_type) {
4239 case D_NEWBLK:
4240 case D_ALLOCDIRECT:
4241 case D_ALLOCINDIR:
4242 /*
4243 * Add the written block to the bmsafemap so it can
4244 * be notified when the bitmap is on disk.
4245 */
4246 newblk = WK_NEWBLK(jnewblk->jn_dep);
4247 newblk->nb_jnewblk = NULL;
4248 if ((newblk->nb_state & GOINGAWAY) == 0) {
4249 bmsafemap = newblk->nb_bmsafemap;
4250 newblk->nb_state |= ONDEPLIST;
4251 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
4252 nb_deps);
4253 }
4254 jwork_insert(&newblk->nb_jwork, jsegdep);
4255 break;
4256 case D_FREEFRAG:
4257 /*
4258 * A newblock being removed by a freefrag when replaced by
4259 * frag extension.
4260 */
4261 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
4262 freefrag->ff_jdep = NULL;
4263 jwork_insert(&freefrag->ff_jwork, jsegdep);
4264 break;
4265 case D_FREEWORK:
4266 /*
4267 * A direct block was removed by truncate.
4268 */
4269 freework = WK_FREEWORK(jnewblk->jn_dep);
4270 freework->fw_jnewblk = NULL;
4271 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
4272 break;
4273 default:
4274 panic("handle_written_jnewblk: Unknown type %d.",
4275 jnewblk->jn_dep->wk_type);
4276 }
4277 jnewblk->jn_dep = NULL;
4278 free_jnewblk(jnewblk);
4279 }
4280
4281 /*
4282 * Cancel a jfreefrag that won't be needed, probably due to colliding with
4283 * an in-flight allocation that has not yet been committed. Divorce us
4284 * from the freefrag and mark it DEPCOMPLETE so that it may be added
4285 * to the worklist.
4286 */
4287 static void
cancel_jfreefrag(jfreefrag)4288 cancel_jfreefrag(jfreefrag)
4289 struct jfreefrag *jfreefrag;
4290 {
4291 struct freefrag *freefrag;
4292
4293 if (jfreefrag->fr_jsegdep) {
4294 free_jsegdep(jfreefrag->fr_jsegdep);
4295 jfreefrag->fr_jsegdep = NULL;
4296 }
4297 freefrag = jfreefrag->fr_freefrag;
4298 jfreefrag->fr_freefrag = NULL;
4299 free_jfreefrag(jfreefrag);
4300 freefrag->ff_state |= DEPCOMPLETE;
4301 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
4302 }
4303
4304 /*
4305 * Free a jfreefrag when the parent freefrag is rendered obsolete.
4306 */
4307 static void
free_jfreefrag(jfreefrag)4308 free_jfreefrag(jfreefrag)
4309 struct jfreefrag *jfreefrag;
4310 {
4311
4312 if (jfreefrag->fr_state & INPROGRESS)
4313 WORKLIST_REMOVE(&jfreefrag->fr_list);
4314 else if (jfreefrag->fr_state & ONWORKLIST)
4315 remove_from_journal(&jfreefrag->fr_list);
4316 if (jfreefrag->fr_freefrag != NULL)
4317 panic("free_jfreefrag: Still attached to a freefrag.");
4318 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
4319 }
4320
4321 /*
4322 * Called when the journal write for a jfreefrag completes. The parent
4323 * freefrag is added to the worklist if this completes its dependencies.
4324 */
4325 static void
handle_written_jfreefrag(jfreefrag)4326 handle_written_jfreefrag(jfreefrag)
4327 struct jfreefrag *jfreefrag;
4328 {
4329 struct jsegdep *jsegdep;
4330 struct freefrag *freefrag;
4331
4332 /* Grab the jsegdep. */
4333 jsegdep = jfreefrag->fr_jsegdep;
4334 jfreefrag->fr_jsegdep = NULL;
4335 freefrag = jfreefrag->fr_freefrag;
4336 if (freefrag == NULL)
4337 panic("handle_written_jfreefrag: No freefrag.");
4338 freefrag->ff_state |= DEPCOMPLETE;
4339 freefrag->ff_jdep = NULL;
4340 jwork_insert(&freefrag->ff_jwork, jsegdep);
4341 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
4342 add_to_worklist(&freefrag->ff_list, 0);
4343 jfreefrag->fr_freefrag = NULL;
4344 free_jfreefrag(jfreefrag);
4345 }
4346
4347 /*
4348 * Called when the journal write for a jfreeblk completes. The jfreeblk
4349 * is removed from the freeblks list of pending journal writes and the
4350 * jsegdep is moved to the freeblks jwork to be completed when all blocks
4351 * have been reclaimed.
4352 */
4353 static void
handle_written_jblkdep(jblkdep)4354 handle_written_jblkdep(jblkdep)
4355 struct jblkdep *jblkdep;
4356 {
4357 struct freeblks *freeblks;
4358 struct jsegdep *jsegdep;
4359
4360 /* Grab the jsegdep. */
4361 jsegdep = jblkdep->jb_jsegdep;
4362 jblkdep->jb_jsegdep = NULL;
4363 freeblks = jblkdep->jb_freeblks;
4364 LIST_REMOVE(jblkdep, jb_deps);
4365 jwork_insert(&freeblks->fb_jwork, jsegdep);
4366 /*
4367 * If the freeblks is all journaled, we can add it to the worklist.
4368 */
4369 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4370 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4371 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4372
4373 free_jblkdep(jblkdep);
4374 }
4375
4376 static struct jsegdep *
newjsegdep(struct worklist * wk)4377 newjsegdep(struct worklist *wk)
4378 {
4379 struct jsegdep *jsegdep;
4380
4381 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4382 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4383 jsegdep->jd_seg = NULL;
4384
4385 return (jsegdep);
4386 }
4387
4388 static struct jmvref *
newjmvref(dp,ino,oldoff,newoff)4389 newjmvref(dp, ino, oldoff, newoff)
4390 struct inode *dp;
4391 ino_t ino;
4392 off_t oldoff;
4393 off_t newoff;
4394 {
4395 struct jmvref *jmvref;
4396
4397 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4398 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4399 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4400 jmvref->jm_parent = dp->i_number;
4401 jmvref->jm_ino = ino;
4402 jmvref->jm_oldoff = oldoff;
4403 jmvref->jm_newoff = newoff;
4404
4405 return (jmvref);
4406 }
4407
4408 /*
4409 * Allocate a new jremref that tracks the removal of ip from dp with the
4410 * directory entry offset of diroff. Mark the entry as ATTACHED and
4411 * DEPCOMPLETE as we have all the information required for the journal write
4412 * and the directory has already been removed from the buffer. The caller
4413 * is responsible for linking the jremref into the pagedep and adding it
4414 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4415 * a DOTDOT addition so handle_workitem_remove() can properly assign
4416 * the jsegdep when we're done.
4417 */
4418 static struct jremref *
newjremref(struct dirrem * dirrem,struct inode * dp,struct inode * ip,off_t diroff,nlink_t nlink)4419 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4420 off_t diroff, nlink_t nlink)
4421 {
4422 struct jremref *jremref;
4423
4424 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4425 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4426 jremref->jr_state = ATTACHED;
4427 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4428 nlink, ip->i_mode);
4429 jremref->jr_dirrem = dirrem;
4430
4431 return (jremref);
4432 }
4433
4434 static inline void
newinoref(struct inoref * inoref,ino_t ino,ino_t parent,off_t diroff,nlink_t nlink,uint16_t mode)4435 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4436 nlink_t nlink, uint16_t mode)
4437 {
4438
4439 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4440 inoref->if_diroff = diroff;
4441 inoref->if_ino = ino;
4442 inoref->if_parent = parent;
4443 inoref->if_nlink = nlink;
4444 inoref->if_mode = mode;
4445 }
4446
4447 /*
4448 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4449 * directory offset may not be known until later. The caller is responsible
4450 * adding the entry to the journal when this information is available. nlink
4451 * should be the link count prior to the addition and mode is only required
4452 * to have the correct FMT.
4453 */
4454 static struct jaddref *
newjaddref(struct inode * dp,ino_t ino,off_t diroff,int16_t nlink,uint16_t mode)4455 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4456 uint16_t mode)
4457 {
4458 struct jaddref *jaddref;
4459
4460 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4461 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4462 jaddref->ja_state = ATTACHED;
4463 jaddref->ja_mkdir = NULL;
4464 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4465
4466 return (jaddref);
4467 }
4468
4469 /*
4470 * Create a new free dependency for a freework. The caller is responsible
4471 * for adjusting the reference count when it has the lock held. The freedep
4472 * will track an outstanding bitmap write that will ultimately clear the
4473 * freework to continue.
4474 */
4475 static struct freedep *
newfreedep(struct freework * freework)4476 newfreedep(struct freework *freework)
4477 {
4478 struct freedep *freedep;
4479
4480 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4481 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4482 freedep->fd_freework = freework;
4483
4484 return (freedep);
4485 }
4486
4487 /*
4488 * Free a freedep structure once the buffer it is linked to is written. If
4489 * this is the last reference to the freework schedule it for completion.
4490 */
4491 static void
free_freedep(freedep)4492 free_freedep(freedep)
4493 struct freedep *freedep;
4494 {
4495 struct freework *freework;
4496
4497 freework = freedep->fd_freework;
4498 freework->fw_freeblks->fb_cgwait--;
4499 if (--freework->fw_ref == 0)
4500 freework_enqueue(freework);
4501 WORKITEM_FREE(freedep, D_FREEDEP);
4502 }
4503
4504 /*
4505 * Allocate a new freework structure that may be a level in an indirect
4506 * when parent is not NULL or a top level block when it is. The top level
4507 * freework structures are allocated without the per-filesystem lock held
4508 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4509 */
4510 static struct freework *
newfreework(ump,freeblks,parent,lbn,nb,frags,off,journal)4511 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4512 struct ufsmount *ump;
4513 struct freeblks *freeblks;
4514 struct freework *parent;
4515 ufs_lbn_t lbn;
4516 ufs2_daddr_t nb;
4517 int frags;
4518 int off;
4519 int journal;
4520 {
4521 struct freework *freework;
4522
4523 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4524 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4525 freework->fw_state = ATTACHED;
4526 freework->fw_jnewblk = NULL;
4527 freework->fw_freeblks = freeblks;
4528 freework->fw_parent = parent;
4529 freework->fw_lbn = lbn;
4530 freework->fw_blkno = nb;
4531 freework->fw_frags = frags;
4532 freework->fw_indir = NULL;
4533 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4534 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4535 freework->fw_start = freework->fw_off = off;
4536 if (journal)
4537 newjfreeblk(freeblks, lbn, nb, frags);
4538 if (parent == NULL) {
4539 ACQUIRE_LOCK(ump);
4540 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4541 freeblks->fb_ref++;
4542 FREE_LOCK(ump);
4543 }
4544
4545 return (freework);
4546 }
4547
4548 /*
4549 * Eliminate a jfreeblk for a block that does not need journaling.
4550 */
4551 static void
cancel_jfreeblk(freeblks,blkno)4552 cancel_jfreeblk(freeblks, blkno)
4553 struct freeblks *freeblks;
4554 ufs2_daddr_t blkno;
4555 {
4556 struct jfreeblk *jfreeblk;
4557 struct jblkdep *jblkdep;
4558
4559 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4560 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4561 continue;
4562 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4563 if (jfreeblk->jf_blkno == blkno)
4564 break;
4565 }
4566 if (jblkdep == NULL)
4567 return;
4568 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4569 free_jsegdep(jblkdep->jb_jsegdep);
4570 LIST_REMOVE(jblkdep, jb_deps);
4571 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4572 }
4573
4574 /*
4575 * Allocate a new jfreeblk to journal top level block pointer when truncating
4576 * a file. The caller must add this to the worklist when the per-filesystem
4577 * lock is held.
4578 */
4579 static struct jfreeblk *
newjfreeblk(freeblks,lbn,blkno,frags)4580 newjfreeblk(freeblks, lbn, blkno, frags)
4581 struct freeblks *freeblks;
4582 ufs_lbn_t lbn;
4583 ufs2_daddr_t blkno;
4584 int frags;
4585 {
4586 struct jfreeblk *jfreeblk;
4587
4588 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4589 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4590 freeblks->fb_list.wk_mp);
4591 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4592 jfreeblk->jf_dep.jb_freeblks = freeblks;
4593 jfreeblk->jf_ino = freeblks->fb_inum;
4594 jfreeblk->jf_lbn = lbn;
4595 jfreeblk->jf_blkno = blkno;
4596 jfreeblk->jf_frags = frags;
4597 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4598
4599 return (jfreeblk);
4600 }
4601
4602 /*
4603 * The journal is only prepared to handle full-size block numbers, so we
4604 * have to adjust the record to reflect the change to a full-size block.
4605 * For example, suppose we have a block made up of fragments 8-15 and
4606 * want to free its last two fragments. We are given a request that says:
4607 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4608 * where frags are the number of fragments to free and oldfrags are the
4609 * number of fragments to keep. To block align it, we have to change it to
4610 * have a valid full-size blkno, so it becomes:
4611 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4612 */
4613 static void
adjust_newfreework(freeblks,frag_offset)4614 adjust_newfreework(freeblks, frag_offset)
4615 struct freeblks *freeblks;
4616 int frag_offset;
4617 {
4618 struct jfreeblk *jfreeblk;
4619
4620 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4621 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4622 ("adjust_newfreework: Missing freeblks dependency"));
4623
4624 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4625 jfreeblk->jf_blkno -= frag_offset;
4626 jfreeblk->jf_frags += frag_offset;
4627 }
4628
4629 /*
4630 * Allocate a new jtrunc to track a partial truncation.
4631 */
4632 static struct jtrunc *
newjtrunc(freeblks,size,extsize)4633 newjtrunc(freeblks, size, extsize)
4634 struct freeblks *freeblks;
4635 off_t size;
4636 int extsize;
4637 {
4638 struct jtrunc *jtrunc;
4639
4640 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4641 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4642 freeblks->fb_list.wk_mp);
4643 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4644 jtrunc->jt_dep.jb_freeblks = freeblks;
4645 jtrunc->jt_ino = freeblks->fb_inum;
4646 jtrunc->jt_size = size;
4647 jtrunc->jt_extsize = extsize;
4648 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4649
4650 return (jtrunc);
4651 }
4652
4653 /*
4654 * If we're canceling a new bitmap we have to search for another ref
4655 * to move into the bmsafemap dep. This might be better expressed
4656 * with another structure.
4657 */
4658 static void
move_newblock_dep(jaddref,inodedep)4659 move_newblock_dep(jaddref, inodedep)
4660 struct jaddref *jaddref;
4661 struct inodedep *inodedep;
4662 {
4663 struct inoref *inoref;
4664 struct jaddref *jaddrefn;
4665
4666 jaddrefn = NULL;
4667 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4668 inoref = TAILQ_NEXT(inoref, if_deps)) {
4669 if ((jaddref->ja_state & NEWBLOCK) &&
4670 inoref->if_list.wk_type == D_JADDREF) {
4671 jaddrefn = (struct jaddref *)inoref;
4672 break;
4673 }
4674 }
4675 if (jaddrefn == NULL)
4676 return;
4677 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4678 jaddrefn->ja_state |= jaddref->ja_state &
4679 (ATTACHED | UNDONE | NEWBLOCK);
4680 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4681 jaddref->ja_state |= ATTACHED;
4682 LIST_REMOVE(jaddref, ja_bmdeps);
4683 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4684 ja_bmdeps);
4685 }
4686
4687 /*
4688 * Cancel a jaddref either before it has been written or while it is being
4689 * written. This happens when a link is removed before the add reaches
4690 * the disk. The jaddref dependency is kept linked into the bmsafemap
4691 * and inode to prevent the link count or bitmap from reaching the disk
4692 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4693 * required.
4694 *
4695 * Returns 1 if the canceled addref requires journaling of the remove and
4696 * 0 otherwise.
4697 */
4698 static int
cancel_jaddref(jaddref,inodedep,wkhd)4699 cancel_jaddref(jaddref, inodedep, wkhd)
4700 struct jaddref *jaddref;
4701 struct inodedep *inodedep;
4702 struct workhead *wkhd;
4703 {
4704 struct inoref *inoref;
4705 struct jsegdep *jsegdep;
4706 int needsj;
4707
4708 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4709 ("cancel_jaddref: Canceling complete jaddref"));
4710 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4711 needsj = 1;
4712 else
4713 needsj = 0;
4714 if (inodedep == NULL)
4715 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4716 0, &inodedep) == 0)
4717 panic("cancel_jaddref: Lost inodedep");
4718 /*
4719 * We must adjust the nlink of any reference operation that follows
4720 * us so that it is consistent with the in-memory reference. This
4721 * ensures that inode nlink rollbacks always have the correct link.
4722 */
4723 if (needsj == 0) {
4724 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4725 inoref = TAILQ_NEXT(inoref, if_deps)) {
4726 if (inoref->if_state & GOINGAWAY)
4727 break;
4728 inoref->if_nlink--;
4729 }
4730 }
4731 jsegdep = inoref_jseg(&jaddref->ja_ref);
4732 if (jaddref->ja_state & NEWBLOCK)
4733 move_newblock_dep(jaddref, inodedep);
4734 wake_worklist(&jaddref->ja_list);
4735 jaddref->ja_mkdir = NULL;
4736 if (jaddref->ja_state & INPROGRESS) {
4737 jaddref->ja_state &= ~INPROGRESS;
4738 WORKLIST_REMOVE(&jaddref->ja_list);
4739 jwork_insert(wkhd, jsegdep);
4740 } else {
4741 free_jsegdep(jsegdep);
4742 if (jaddref->ja_state & DEPCOMPLETE)
4743 remove_from_journal(&jaddref->ja_list);
4744 }
4745 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4746 /*
4747 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4748 * can arrange for them to be freed with the bitmap. Otherwise we
4749 * no longer need this addref attached to the inoreflst and it
4750 * will incorrectly adjust nlink if we leave it.
4751 */
4752 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4753 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4754 if_deps);
4755 jaddref->ja_state |= COMPLETE;
4756 free_jaddref(jaddref);
4757 return (needsj);
4758 }
4759 /*
4760 * Leave the head of the list for jsegdeps for fast merging.
4761 */
4762 if (LIST_FIRST(wkhd) != NULL) {
4763 jaddref->ja_state |= ONWORKLIST;
4764 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4765 } else
4766 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4767
4768 return (needsj);
4769 }
4770
4771 /*
4772 * Attempt to free a jaddref structure when some work completes. This
4773 * should only succeed once the entry is written and all dependencies have
4774 * been notified.
4775 */
4776 static void
free_jaddref(jaddref)4777 free_jaddref(jaddref)
4778 struct jaddref *jaddref;
4779 {
4780
4781 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4782 return;
4783 if (jaddref->ja_ref.if_jsegdep)
4784 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4785 jaddref, jaddref->ja_state);
4786 if (jaddref->ja_state & NEWBLOCK)
4787 LIST_REMOVE(jaddref, ja_bmdeps);
4788 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4789 panic("free_jaddref: Bad state %p(0x%X)",
4790 jaddref, jaddref->ja_state);
4791 if (jaddref->ja_mkdir != NULL)
4792 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4793 WORKITEM_FREE(jaddref, D_JADDREF);
4794 }
4795
4796 /*
4797 * Free a jremref structure once it has been written or discarded.
4798 */
4799 static void
free_jremref(jremref)4800 free_jremref(jremref)
4801 struct jremref *jremref;
4802 {
4803
4804 if (jremref->jr_ref.if_jsegdep)
4805 free_jsegdep(jremref->jr_ref.if_jsegdep);
4806 if (jremref->jr_state & INPROGRESS)
4807 panic("free_jremref: IO still pending");
4808 WORKITEM_FREE(jremref, D_JREMREF);
4809 }
4810
4811 /*
4812 * Free a jnewblk structure.
4813 */
4814 static void
free_jnewblk(jnewblk)4815 free_jnewblk(jnewblk)
4816 struct jnewblk *jnewblk;
4817 {
4818
4819 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4820 return;
4821 LIST_REMOVE(jnewblk, jn_deps);
4822 if (jnewblk->jn_dep != NULL)
4823 panic("free_jnewblk: Dependency still attached.");
4824 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4825 }
4826
4827 /*
4828 * Cancel a jnewblk which has been been made redundant by frag extension.
4829 */
4830 static void
cancel_jnewblk(jnewblk,wkhd)4831 cancel_jnewblk(jnewblk, wkhd)
4832 struct jnewblk *jnewblk;
4833 struct workhead *wkhd;
4834 {
4835 struct jsegdep *jsegdep;
4836
4837 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4838 jsegdep = jnewblk->jn_jsegdep;
4839 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4840 panic("cancel_jnewblk: Invalid state");
4841 jnewblk->jn_jsegdep = NULL;
4842 jnewblk->jn_dep = NULL;
4843 jnewblk->jn_state |= GOINGAWAY;
4844 if (jnewblk->jn_state & INPROGRESS) {
4845 jnewblk->jn_state &= ~INPROGRESS;
4846 WORKLIST_REMOVE(&jnewblk->jn_list);
4847 jwork_insert(wkhd, jsegdep);
4848 } else {
4849 free_jsegdep(jsegdep);
4850 remove_from_journal(&jnewblk->jn_list);
4851 }
4852 wake_worklist(&jnewblk->jn_list);
4853 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4854 }
4855
4856 static void
free_jblkdep(jblkdep)4857 free_jblkdep(jblkdep)
4858 struct jblkdep *jblkdep;
4859 {
4860
4861 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4862 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4863 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4864 WORKITEM_FREE(jblkdep, D_JTRUNC);
4865 else
4866 panic("free_jblkdep: Unexpected type %s",
4867 TYPENAME(jblkdep->jb_list.wk_type));
4868 }
4869
4870 /*
4871 * Free a single jseg once it is no longer referenced in memory or on
4872 * disk. Reclaim journal blocks and dependencies waiting for the segment
4873 * to disappear.
4874 */
4875 static void
free_jseg(jseg,jblocks)4876 free_jseg(jseg, jblocks)
4877 struct jseg *jseg;
4878 struct jblocks *jblocks;
4879 {
4880 struct freework *freework;
4881
4882 /*
4883 * Free freework structures that were lingering to indicate freed
4884 * indirect blocks that forced journal write ordering on reallocate.
4885 */
4886 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4887 indirblk_remove(freework);
4888 if (jblocks->jb_oldestseg == jseg)
4889 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4890 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4891 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4892 KASSERT(LIST_EMPTY(&jseg->js_entries),
4893 ("free_jseg: Freed jseg has valid entries."));
4894 WORKITEM_FREE(jseg, D_JSEG);
4895 }
4896
4897 /*
4898 * Free all jsegs that meet the criteria for being reclaimed and update
4899 * oldestseg.
4900 */
4901 static void
free_jsegs(jblocks)4902 free_jsegs(jblocks)
4903 struct jblocks *jblocks;
4904 {
4905 struct jseg *jseg;
4906
4907 /*
4908 * Free only those jsegs which have none allocated before them to
4909 * preserve the journal space ordering.
4910 */
4911 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4912 /*
4913 * Only reclaim space when nothing depends on this journal
4914 * set and another set has written that it is no longer
4915 * valid.
4916 */
4917 if (jseg->js_refs != 0) {
4918 jblocks->jb_oldestseg = jseg;
4919 return;
4920 }
4921 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4922 break;
4923 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4924 break;
4925 /*
4926 * We can free jsegs that didn't write entries when
4927 * oldestwrseq == js_seq.
4928 */
4929 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4930 jseg->js_cnt != 0)
4931 break;
4932 free_jseg(jseg, jblocks);
4933 }
4934 /*
4935 * If we exited the loop above we still must discover the
4936 * oldest valid segment.
4937 */
4938 if (jseg)
4939 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4940 jseg = TAILQ_NEXT(jseg, js_next))
4941 if (jseg->js_refs != 0)
4942 break;
4943 jblocks->jb_oldestseg = jseg;
4944 /*
4945 * The journal has no valid records but some jsegs may still be
4946 * waiting on oldestwrseq to advance. We force a small record
4947 * out to permit these lingering records to be reclaimed.
4948 */
4949 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4950 jblocks->jb_needseg = 1;
4951 }
4952
4953 /*
4954 * Release one reference to a jseg and free it if the count reaches 0. This
4955 * should eventually reclaim journal space as well.
4956 */
4957 static void
rele_jseg(jseg)4958 rele_jseg(jseg)
4959 struct jseg *jseg;
4960 {
4961
4962 KASSERT(jseg->js_refs > 0,
4963 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4964 if (--jseg->js_refs != 0)
4965 return;
4966 free_jsegs(jseg->js_jblocks);
4967 }
4968
4969 /*
4970 * Release a jsegdep and decrement the jseg count.
4971 */
4972 static void
free_jsegdep(jsegdep)4973 free_jsegdep(jsegdep)
4974 struct jsegdep *jsegdep;
4975 {
4976
4977 if (jsegdep->jd_seg)
4978 rele_jseg(jsegdep->jd_seg);
4979 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4980 }
4981
4982 /*
4983 * Wait for a journal item to make it to disk. Initiate journal processing
4984 * if required.
4985 */
4986 static int
jwait(wk,waitfor)4987 jwait(wk, waitfor)
4988 struct worklist *wk;
4989 int waitfor;
4990 {
4991
4992 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4993 /*
4994 * Blocking journal waits cause slow synchronous behavior. Record
4995 * stats on the frequency of these blocking operations.
4996 */
4997 if (waitfor == MNT_WAIT) {
4998 stat_journal_wait++;
4999 switch (wk->wk_type) {
5000 case D_JREMREF:
5001 case D_JMVREF:
5002 stat_jwait_filepage++;
5003 break;
5004 case D_JTRUNC:
5005 case D_JFREEBLK:
5006 stat_jwait_freeblks++;
5007 break;
5008 case D_JNEWBLK:
5009 stat_jwait_newblk++;
5010 break;
5011 case D_JADDREF:
5012 stat_jwait_inode++;
5013 break;
5014 default:
5015 break;
5016 }
5017 }
5018 /*
5019 * If IO has not started we process the journal. We can't mark the
5020 * worklist item as IOWAITING because we drop the lock while
5021 * processing the journal and the worklist entry may be freed after
5022 * this point. The caller may call back in and re-issue the request.
5023 */
5024 if ((wk->wk_state & INPROGRESS) == 0) {
5025 softdep_process_journal(wk->wk_mp, wk, waitfor);
5026 if (waitfor != MNT_WAIT)
5027 return (EBUSY);
5028 return (0);
5029 }
5030 if (waitfor != MNT_WAIT)
5031 return (EBUSY);
5032 wait_worklist(wk, "jwait");
5033 return (0);
5034 }
5035
5036 /*
5037 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
5038 * appropriate. This is a convenience function to reduce duplicate code
5039 * for the setup and revert functions below.
5040 */
5041 static struct inodedep *
inodedep_lookup_ip(ip)5042 inodedep_lookup_ip(ip)
5043 struct inode *ip;
5044 {
5045 struct inodedep *inodedep;
5046
5047 KASSERT(ip->i_nlink >= ip->i_effnlink,
5048 ("inodedep_lookup_ip: bad delta"));
5049 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
5050 &inodedep);
5051 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
5052 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
5053
5054 return (inodedep);
5055 }
5056
5057 /*
5058 * Called prior to creating a new inode and linking it to a directory. The
5059 * jaddref structure must already be allocated by softdep_setup_inomapdep
5060 * and it is discovered here so we can initialize the mode and update
5061 * nlinkdelta.
5062 */
5063 void
softdep_setup_create(dp,ip)5064 softdep_setup_create(dp, ip)
5065 struct inode *dp;
5066 struct inode *ip;
5067 {
5068 struct inodedep *inodedep;
5069 struct jaddref *jaddref __diagused;
5070 struct vnode *dvp;
5071
5072 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5073 ("softdep_setup_create called on non-softdep filesystem"));
5074 KASSERT(ip->i_nlink == 1,
5075 ("softdep_setup_create: Invalid link count."));
5076 dvp = ITOV(dp);
5077 ACQUIRE_LOCK(ITOUMP(dp));
5078 inodedep = inodedep_lookup_ip(ip);
5079 if (DOINGSUJ(dvp)) {
5080 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5081 inoreflst);
5082 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
5083 ("softdep_setup_create: No addref structure present."));
5084 }
5085 FREE_LOCK(ITOUMP(dp));
5086 }
5087
5088 /*
5089 * Create a jaddref structure to track the addition of a DOTDOT link when
5090 * we are reparenting an inode as part of a rename. This jaddref will be
5091 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
5092 * non-journaling softdep.
5093 */
5094 void
softdep_setup_dotdot_link(dp,ip)5095 softdep_setup_dotdot_link(dp, ip)
5096 struct inode *dp;
5097 struct inode *ip;
5098 {
5099 struct inodedep *inodedep;
5100 struct jaddref *jaddref;
5101 struct vnode *dvp;
5102
5103 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5104 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
5105 dvp = ITOV(dp);
5106 jaddref = NULL;
5107 /*
5108 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
5109 * is used as a normal link would be.
5110 */
5111 if (DOINGSUJ(dvp))
5112 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5113 dp->i_effnlink - 1, dp->i_mode);
5114 ACQUIRE_LOCK(ITOUMP(dp));
5115 inodedep = inodedep_lookup_ip(dp);
5116 if (jaddref)
5117 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5118 if_deps);
5119 FREE_LOCK(ITOUMP(dp));
5120 }
5121
5122 /*
5123 * Create a jaddref structure to track a new link to an inode. The directory
5124 * offset is not known until softdep_setup_directory_add or
5125 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
5126 * softdep.
5127 */
5128 void
softdep_setup_link(dp,ip)5129 softdep_setup_link(dp, ip)
5130 struct inode *dp;
5131 struct inode *ip;
5132 {
5133 struct inodedep *inodedep;
5134 struct jaddref *jaddref;
5135 struct vnode *dvp;
5136
5137 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5138 ("softdep_setup_link called on non-softdep filesystem"));
5139 dvp = ITOV(dp);
5140 jaddref = NULL;
5141 if (DOINGSUJ(dvp))
5142 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
5143 ip->i_mode);
5144 ACQUIRE_LOCK(ITOUMP(dp));
5145 inodedep = inodedep_lookup_ip(ip);
5146 if (jaddref)
5147 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5148 if_deps);
5149 FREE_LOCK(ITOUMP(dp));
5150 }
5151
5152 /*
5153 * Called to create the jaddref structures to track . and .. references as
5154 * well as lookup and further initialize the incomplete jaddref created
5155 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
5156 * nlinkdelta for non-journaling softdep.
5157 */
5158 void
softdep_setup_mkdir(dp,ip)5159 softdep_setup_mkdir(dp, ip)
5160 struct inode *dp;
5161 struct inode *ip;
5162 {
5163 struct inodedep *inodedep;
5164 struct jaddref *dotdotaddref;
5165 struct jaddref *dotaddref;
5166 struct jaddref *jaddref;
5167 struct vnode *dvp;
5168
5169 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5170 ("softdep_setup_mkdir called on non-softdep filesystem"));
5171 dvp = ITOV(dp);
5172 dotaddref = dotdotaddref = NULL;
5173 if (DOINGSUJ(dvp)) {
5174 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
5175 ip->i_mode);
5176 dotaddref->ja_state |= MKDIR_BODY;
5177 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5178 dp->i_effnlink - 1, dp->i_mode);
5179 dotdotaddref->ja_state |= MKDIR_PARENT;
5180 }
5181 ACQUIRE_LOCK(ITOUMP(dp));
5182 inodedep = inodedep_lookup_ip(ip);
5183 if (DOINGSUJ(dvp)) {
5184 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5185 inoreflst);
5186 KASSERT(jaddref != NULL,
5187 ("softdep_setup_mkdir: No addref structure present."));
5188 KASSERT(jaddref->ja_parent == dp->i_number,
5189 ("softdep_setup_mkdir: bad parent %ju",
5190 (uintmax_t)jaddref->ja_parent));
5191 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
5192 if_deps);
5193 }
5194 inodedep = inodedep_lookup_ip(dp);
5195 if (DOINGSUJ(dvp))
5196 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
5197 &dotdotaddref->ja_ref, if_deps);
5198 FREE_LOCK(ITOUMP(dp));
5199 }
5200
5201 /*
5202 * Called to track nlinkdelta of the inode and parent directories prior to
5203 * unlinking a directory.
5204 */
5205 void
softdep_setup_rmdir(dp,ip)5206 softdep_setup_rmdir(dp, ip)
5207 struct inode *dp;
5208 struct inode *ip;
5209 {
5210
5211 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5212 ("softdep_setup_rmdir called on non-softdep filesystem"));
5213 ACQUIRE_LOCK(ITOUMP(dp));
5214 (void) inodedep_lookup_ip(ip);
5215 (void) inodedep_lookup_ip(dp);
5216 FREE_LOCK(ITOUMP(dp));
5217 }
5218
5219 /*
5220 * Called to track nlinkdelta of the inode and parent directories prior to
5221 * unlink.
5222 */
5223 void
softdep_setup_unlink(dp,ip)5224 softdep_setup_unlink(dp, ip)
5225 struct inode *dp;
5226 struct inode *ip;
5227 {
5228
5229 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5230 ("softdep_setup_unlink called on non-softdep filesystem"));
5231 ACQUIRE_LOCK(ITOUMP(dp));
5232 (void) inodedep_lookup_ip(ip);
5233 (void) inodedep_lookup_ip(dp);
5234 FREE_LOCK(ITOUMP(dp));
5235 }
5236
5237 /*
5238 * Called to release the journal structures created by a failed non-directory
5239 * creation. Adjusts nlinkdelta for non-journaling softdep.
5240 */
5241 void
softdep_revert_create(dp,ip)5242 softdep_revert_create(dp, ip)
5243 struct inode *dp;
5244 struct inode *ip;
5245 {
5246 struct inodedep *inodedep;
5247 struct jaddref *jaddref;
5248 struct vnode *dvp;
5249
5250 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
5251 ("softdep_revert_create called on non-softdep filesystem"));
5252 dvp = ITOV(dp);
5253 ACQUIRE_LOCK(ITOUMP(dp));
5254 inodedep = inodedep_lookup_ip(ip);
5255 if (DOINGSUJ(dvp)) {
5256 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5257 inoreflst);
5258 KASSERT(jaddref->ja_parent == dp->i_number,
5259 ("softdep_revert_create: addref parent mismatch"));
5260 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5261 }
5262 FREE_LOCK(ITOUMP(dp));
5263 }
5264
5265 /*
5266 * Called to release the journal structures created by a failed link
5267 * addition. Adjusts nlinkdelta for non-journaling softdep.
5268 */
5269 void
softdep_revert_link(dp,ip)5270 softdep_revert_link(dp, ip)
5271 struct inode *dp;
5272 struct inode *ip;
5273 {
5274 struct inodedep *inodedep;
5275 struct jaddref *jaddref;
5276 struct vnode *dvp;
5277
5278 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5279 ("softdep_revert_link called on non-softdep filesystem"));
5280 dvp = ITOV(dp);
5281 ACQUIRE_LOCK(ITOUMP(dp));
5282 inodedep = inodedep_lookup_ip(ip);
5283 if (DOINGSUJ(dvp)) {
5284 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5285 inoreflst);
5286 KASSERT(jaddref->ja_parent == dp->i_number,
5287 ("softdep_revert_link: addref parent mismatch"));
5288 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5289 }
5290 FREE_LOCK(ITOUMP(dp));
5291 }
5292
5293 /*
5294 * Called to release the journal structures created by a failed mkdir
5295 * attempt. Adjusts nlinkdelta for non-journaling softdep.
5296 */
5297 void
softdep_revert_mkdir(dp,ip)5298 softdep_revert_mkdir(dp, ip)
5299 struct inode *dp;
5300 struct inode *ip;
5301 {
5302 struct inodedep *inodedep;
5303 struct jaddref *jaddref;
5304 struct jaddref *dotaddref;
5305 struct vnode *dvp;
5306
5307 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5308 ("softdep_revert_mkdir called on non-softdep filesystem"));
5309 dvp = ITOV(dp);
5310
5311 ACQUIRE_LOCK(ITOUMP(dp));
5312 inodedep = inodedep_lookup_ip(dp);
5313 if (DOINGSUJ(dvp)) {
5314 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5315 inoreflst);
5316 KASSERT(jaddref->ja_parent == ip->i_number,
5317 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
5318 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5319 }
5320 inodedep = inodedep_lookup_ip(ip);
5321 if (DOINGSUJ(dvp)) {
5322 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5323 inoreflst);
5324 KASSERT(jaddref->ja_parent == dp->i_number,
5325 ("softdep_revert_mkdir: addref parent mismatch"));
5326 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
5327 inoreflst, if_deps);
5328 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5329 KASSERT(dotaddref->ja_parent == ip->i_number,
5330 ("softdep_revert_mkdir: dot addref parent mismatch"));
5331 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
5332 }
5333 FREE_LOCK(ITOUMP(dp));
5334 }
5335
5336 /*
5337 * Called to correct nlinkdelta after a failed rmdir.
5338 */
5339 void
softdep_revert_rmdir(dp,ip)5340 softdep_revert_rmdir(dp, ip)
5341 struct inode *dp;
5342 struct inode *ip;
5343 {
5344
5345 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5346 ("softdep_revert_rmdir called on non-softdep filesystem"));
5347 ACQUIRE_LOCK(ITOUMP(dp));
5348 (void) inodedep_lookup_ip(ip);
5349 (void) inodedep_lookup_ip(dp);
5350 FREE_LOCK(ITOUMP(dp));
5351 }
5352
5353 /*
5354 * Protecting the freemaps (or bitmaps).
5355 *
5356 * To eliminate the need to execute fsck before mounting a filesystem
5357 * after a power failure, one must (conservatively) guarantee that the
5358 * on-disk copy of the bitmaps never indicate that a live inode or block is
5359 * free. So, when a block or inode is allocated, the bitmap should be
5360 * updated (on disk) before any new pointers. When a block or inode is
5361 * freed, the bitmap should not be updated until all pointers have been
5362 * reset. The latter dependency is handled by the delayed de-allocation
5363 * approach described below for block and inode de-allocation. The former
5364 * dependency is handled by calling the following procedure when a block or
5365 * inode is allocated. When an inode is allocated an "inodedep" is created
5366 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5367 * Each "inodedep" is also inserted into the hash indexing structure so
5368 * that any additional link additions can be made dependent on the inode
5369 * allocation.
5370 *
5371 * The ufs filesystem maintains a number of free block counts (e.g., per
5372 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5373 * in addition to the bitmaps. These counts are used to improve efficiency
5374 * during allocation and therefore must be consistent with the bitmaps.
5375 * There is no convenient way to guarantee post-crash consistency of these
5376 * counts with simple update ordering, for two main reasons: (1) The counts
5377 * and bitmaps for a single cylinder group block are not in the same disk
5378 * sector. If a disk write is interrupted (e.g., by power failure), one may
5379 * be written and the other not. (2) Some of the counts are located in the
5380 * superblock rather than the cylinder group block. So, we focus our soft
5381 * updates implementation on protecting the bitmaps. When mounting a
5382 * filesystem, we recompute the auxiliary counts from the bitmaps.
5383 */
5384
5385 /*
5386 * Called just after updating the cylinder group block to allocate an inode.
5387 */
5388 void
softdep_setup_inomapdep(bp,ip,newinum,mode)5389 softdep_setup_inomapdep(bp, ip, newinum, mode)
5390 struct buf *bp; /* buffer for cylgroup block with inode map */
5391 struct inode *ip; /* inode related to allocation */
5392 ino_t newinum; /* new inode number being allocated */
5393 int mode;
5394 {
5395 struct inodedep *inodedep;
5396 struct bmsafemap *bmsafemap;
5397 struct jaddref *jaddref;
5398 struct mount *mp;
5399 struct fs *fs;
5400
5401 mp = ITOVFS(ip);
5402 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5403 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5404 fs = VFSTOUFS(mp)->um_fs;
5405 jaddref = NULL;
5406
5407 /*
5408 * Allocate the journal reference add structure so that the bitmap
5409 * can be dependent on it.
5410 */
5411 if (MOUNTEDSUJ(mp)) {
5412 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5413 jaddref->ja_state |= NEWBLOCK;
5414 }
5415
5416 /*
5417 * Create a dependency for the newly allocated inode.
5418 * Panic if it already exists as something is seriously wrong.
5419 * Otherwise add it to the dependency list for the buffer holding
5420 * the cylinder group map from which it was allocated.
5421 *
5422 * We have to preallocate a bmsafemap entry in case it is needed
5423 * in bmsafemap_lookup since once we allocate the inodedep, we
5424 * have to finish initializing it before we can FREE_LOCK().
5425 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5426 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5427 * creating the inodedep as it can be freed during the time
5428 * that we FREE_LOCK() while allocating the inodedep. We must
5429 * call workitem_alloc() before entering the locked section as
5430 * it also acquires the lock and we must avoid trying doing so
5431 * recursively.
5432 */
5433 bmsafemap = malloc(sizeof(struct bmsafemap),
5434 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5435 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5436 ACQUIRE_LOCK(ITOUMP(ip));
5437 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5438 panic("softdep_setup_inomapdep: dependency %p for new"
5439 "inode already exists", inodedep);
5440 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5441 if (jaddref) {
5442 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5443 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5444 if_deps);
5445 } else {
5446 inodedep->id_state |= ONDEPLIST;
5447 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5448 }
5449 inodedep->id_bmsafemap = bmsafemap;
5450 inodedep->id_state &= ~DEPCOMPLETE;
5451 FREE_LOCK(ITOUMP(ip));
5452 }
5453
5454 /*
5455 * Called just after updating the cylinder group block to
5456 * allocate block or fragment.
5457 */
5458 void
softdep_setup_blkmapdep(bp,mp,newblkno,frags,oldfrags)5459 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5460 struct buf *bp; /* buffer for cylgroup block with block map */
5461 struct mount *mp; /* filesystem doing allocation */
5462 ufs2_daddr_t newblkno; /* number of newly allocated block */
5463 int frags; /* Number of fragments. */
5464 int oldfrags; /* Previous number of fragments for extend. */
5465 {
5466 struct newblk *newblk;
5467 struct bmsafemap *bmsafemap;
5468 struct jnewblk *jnewblk;
5469 struct ufsmount *ump;
5470 struct fs *fs;
5471
5472 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5473 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5474 ump = VFSTOUFS(mp);
5475 fs = ump->um_fs;
5476 jnewblk = NULL;
5477 /*
5478 * Create a dependency for the newly allocated block.
5479 * Add it to the dependency list for the buffer holding
5480 * the cylinder group map from which it was allocated.
5481 */
5482 if (MOUNTEDSUJ(mp)) {
5483 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5484 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5485 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5486 jnewblk->jn_state = ATTACHED;
5487 jnewblk->jn_blkno = newblkno;
5488 jnewblk->jn_frags = frags;
5489 jnewblk->jn_oldfrags = oldfrags;
5490 #ifdef INVARIANTS
5491 {
5492 struct cg *cgp;
5493 uint8_t *blksfree;
5494 long bno;
5495 int i;
5496
5497 cgp = (struct cg *)bp->b_data;
5498 blksfree = cg_blksfree(cgp);
5499 bno = dtogd(fs, jnewblk->jn_blkno);
5500 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5501 i++) {
5502 if (isset(blksfree, bno + i))
5503 panic("softdep_setup_blkmapdep: "
5504 "free fragment %d from %d-%d "
5505 "state 0x%X dep %p", i,
5506 jnewblk->jn_oldfrags,
5507 jnewblk->jn_frags,
5508 jnewblk->jn_state,
5509 jnewblk->jn_dep);
5510 }
5511 }
5512 #endif
5513 }
5514
5515 CTR3(KTR_SUJ,
5516 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5517 newblkno, frags, oldfrags);
5518 ACQUIRE_LOCK(ump);
5519 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5520 panic("softdep_setup_blkmapdep: found block");
5521 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5522 dtog(fs, newblkno), NULL);
5523 if (jnewblk) {
5524 jnewblk->jn_dep = (struct worklist *)newblk;
5525 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5526 } else {
5527 newblk->nb_state |= ONDEPLIST;
5528 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5529 }
5530 newblk->nb_bmsafemap = bmsafemap;
5531 newblk->nb_jnewblk = jnewblk;
5532 FREE_LOCK(ump);
5533 }
5534
5535 #define BMSAFEMAP_HASH(ump, cg) \
5536 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5537
5538 static int
bmsafemap_find(bmsafemaphd,cg,bmsafemapp)5539 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5540 struct bmsafemap_hashhead *bmsafemaphd;
5541 int cg;
5542 struct bmsafemap **bmsafemapp;
5543 {
5544 struct bmsafemap *bmsafemap;
5545
5546 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5547 if (bmsafemap->sm_cg == cg)
5548 break;
5549 if (bmsafemap) {
5550 *bmsafemapp = bmsafemap;
5551 return (1);
5552 }
5553 *bmsafemapp = NULL;
5554
5555 return (0);
5556 }
5557
5558 /*
5559 * Find the bmsafemap associated with a cylinder group buffer.
5560 * If none exists, create one. The buffer must be locked when
5561 * this routine is called and this routine must be called with
5562 * the softdep lock held. To avoid giving up the lock while
5563 * allocating a new bmsafemap, a preallocated bmsafemap may be
5564 * provided. If it is provided but not needed, it is freed.
5565 */
5566 static struct bmsafemap *
bmsafemap_lookup(mp,bp,cg,newbmsafemap)5567 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5568 struct mount *mp;
5569 struct buf *bp;
5570 int cg;
5571 struct bmsafemap *newbmsafemap;
5572 {
5573 struct bmsafemap_hashhead *bmsafemaphd;
5574 struct bmsafemap *bmsafemap, *collision;
5575 struct worklist *wk;
5576 struct ufsmount *ump;
5577
5578 ump = VFSTOUFS(mp);
5579 LOCK_OWNED(ump);
5580 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5581 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5582 if (wk->wk_type == D_BMSAFEMAP) {
5583 if (newbmsafemap)
5584 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5585 return (WK_BMSAFEMAP(wk));
5586 }
5587 }
5588 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5589 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5590 if (newbmsafemap)
5591 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5592 return (bmsafemap);
5593 }
5594 if (newbmsafemap) {
5595 bmsafemap = newbmsafemap;
5596 } else {
5597 FREE_LOCK(ump);
5598 bmsafemap = malloc(sizeof(struct bmsafemap),
5599 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5600 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5601 ACQUIRE_LOCK(ump);
5602 }
5603 bmsafemap->sm_buf = bp;
5604 LIST_INIT(&bmsafemap->sm_inodedephd);
5605 LIST_INIT(&bmsafemap->sm_inodedepwr);
5606 LIST_INIT(&bmsafemap->sm_newblkhd);
5607 LIST_INIT(&bmsafemap->sm_newblkwr);
5608 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5609 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5610 LIST_INIT(&bmsafemap->sm_freehd);
5611 LIST_INIT(&bmsafemap->sm_freewr);
5612 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5613 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5614 return (collision);
5615 }
5616 bmsafemap->sm_cg = cg;
5617 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5618 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5619 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5620 return (bmsafemap);
5621 }
5622
5623 /*
5624 * Direct block allocation dependencies.
5625 *
5626 * When a new block is allocated, the corresponding disk locations must be
5627 * initialized (with zeros or new data) before the on-disk inode points to
5628 * them. Also, the freemap from which the block was allocated must be
5629 * updated (on disk) before the inode's pointer. These two dependencies are
5630 * independent of each other and are needed for all file blocks and indirect
5631 * blocks that are pointed to directly by the inode. Just before the
5632 * "in-core" version of the inode is updated with a newly allocated block
5633 * number, a procedure (below) is called to setup allocation dependency
5634 * structures. These structures are removed when the corresponding
5635 * dependencies are satisfied or when the block allocation becomes obsolete
5636 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5637 * fragment that gets upgraded). All of these cases are handled in
5638 * procedures described later.
5639 *
5640 * When a file extension causes a fragment to be upgraded, either to a larger
5641 * fragment or to a full block, the on-disk location may change (if the
5642 * previous fragment could not simply be extended). In this case, the old
5643 * fragment must be de-allocated, but not until after the inode's pointer has
5644 * been updated. In most cases, this is handled by later procedures, which
5645 * will construct a "freefrag" structure to be added to the workitem queue
5646 * when the inode update is complete (or obsolete). The main exception to
5647 * this is when an allocation occurs while a pending allocation dependency
5648 * (for the same block pointer) remains. This case is handled in the main
5649 * allocation dependency setup procedure by immediately freeing the
5650 * unreferenced fragments.
5651 */
5652 void
softdep_setup_allocdirect(ip,off,newblkno,oldblkno,newsize,oldsize,bp)5653 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5654 struct inode *ip; /* inode to which block is being added */
5655 ufs_lbn_t off; /* block pointer within inode */
5656 ufs2_daddr_t newblkno; /* disk block number being added */
5657 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
5658 long newsize; /* size of new block */
5659 long oldsize; /* size of new block */
5660 struct buf *bp; /* bp for allocated block */
5661 {
5662 struct allocdirect *adp, *oldadp;
5663 struct allocdirectlst *adphead;
5664 struct freefrag *freefrag;
5665 struct inodedep *inodedep;
5666 struct pagedep *pagedep;
5667 struct jnewblk *jnewblk;
5668 struct newblk *newblk;
5669 struct mount *mp;
5670 ufs_lbn_t lbn;
5671
5672 lbn = bp->b_lblkno;
5673 mp = ITOVFS(ip);
5674 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5675 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5676 if (oldblkno && oldblkno != newblkno)
5677 /*
5678 * The usual case is that a smaller fragment that
5679 * was just allocated has been replaced with a bigger
5680 * fragment or a full-size block. If it is marked as
5681 * B_DELWRI, the current contents have not been written
5682 * to disk. It is possible that the block was written
5683 * earlier, but very uncommon. If the block has never
5684 * been written, there is no need to send a BIO_DELETE
5685 * for it when it is freed. The gain from avoiding the
5686 * TRIMs for the common case of unwritten blocks far
5687 * exceeds the cost of the write amplification for the
5688 * uncommon case of failing to send a TRIM for a block
5689 * that had been written.
5690 */
5691 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5692 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5693 else
5694 freefrag = NULL;
5695
5696 CTR6(KTR_SUJ,
5697 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5698 "off %jd newsize %ld oldsize %d",
5699 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5700 ACQUIRE_LOCK(ITOUMP(ip));
5701 if (off >= UFS_NDADDR) {
5702 if (lbn > 0)
5703 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5704 lbn, off);
5705 /* allocating an indirect block */
5706 if (oldblkno != 0)
5707 panic("softdep_setup_allocdirect: non-zero indir");
5708 } else {
5709 if (off != lbn)
5710 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5711 lbn, off);
5712 /*
5713 * Allocating a direct block.
5714 *
5715 * If we are allocating a directory block, then we must
5716 * allocate an associated pagedep to track additions and
5717 * deletions.
5718 */
5719 if ((ip->i_mode & IFMT) == IFDIR)
5720 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5721 &pagedep);
5722 }
5723 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5724 panic("softdep_setup_allocdirect: lost block");
5725 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5726 ("softdep_setup_allocdirect: newblk already initialized"));
5727 /*
5728 * Convert the newblk to an allocdirect.
5729 */
5730 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5731 adp = (struct allocdirect *)newblk;
5732 newblk->nb_freefrag = freefrag;
5733 adp->ad_offset = off;
5734 adp->ad_oldblkno = oldblkno;
5735 adp->ad_newsize = newsize;
5736 adp->ad_oldsize = oldsize;
5737
5738 /*
5739 * Finish initializing the journal.
5740 */
5741 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5742 jnewblk->jn_ino = ip->i_number;
5743 jnewblk->jn_lbn = lbn;
5744 add_to_journal(&jnewblk->jn_list);
5745 }
5746 if (freefrag && freefrag->ff_jdep != NULL &&
5747 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5748 add_to_journal(freefrag->ff_jdep);
5749 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5750 adp->ad_inodedep = inodedep;
5751
5752 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5753 /*
5754 * The list of allocdirects must be kept in sorted and ascending
5755 * order so that the rollback routines can quickly determine the
5756 * first uncommitted block (the size of the file stored on disk
5757 * ends at the end of the lowest committed fragment, or if there
5758 * are no fragments, at the end of the highest committed block).
5759 * Since files generally grow, the typical case is that the new
5760 * block is to be added at the end of the list. We speed this
5761 * special case by checking against the last allocdirect in the
5762 * list before laboriously traversing the list looking for the
5763 * insertion point.
5764 */
5765 adphead = &inodedep->id_newinoupdt;
5766 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5767 if (oldadp == NULL || oldadp->ad_offset <= off) {
5768 /* insert at end of list */
5769 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5770 if (oldadp != NULL && oldadp->ad_offset == off)
5771 allocdirect_merge(adphead, adp, oldadp);
5772 FREE_LOCK(ITOUMP(ip));
5773 return;
5774 }
5775 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5776 if (oldadp->ad_offset >= off)
5777 break;
5778 }
5779 if (oldadp == NULL)
5780 panic("softdep_setup_allocdirect: lost entry");
5781 /* insert in middle of list */
5782 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5783 if (oldadp->ad_offset == off)
5784 allocdirect_merge(adphead, adp, oldadp);
5785
5786 FREE_LOCK(ITOUMP(ip));
5787 }
5788
5789 /*
5790 * Merge a newer and older journal record to be stored either in a
5791 * newblock or freefrag. This handles aggregating journal records for
5792 * fragment allocation into a second record as well as replacing a
5793 * journal free with an aborted journal allocation. A segment for the
5794 * oldest record will be placed on wkhd if it has been written. If not
5795 * the segment for the newer record will suffice.
5796 */
5797 static struct worklist *
jnewblk_merge(new,old,wkhd)5798 jnewblk_merge(new, old, wkhd)
5799 struct worklist *new;
5800 struct worklist *old;
5801 struct workhead *wkhd;
5802 {
5803 struct jnewblk *njnewblk;
5804 struct jnewblk *jnewblk;
5805
5806 /* Handle NULLs to simplify callers. */
5807 if (new == NULL)
5808 return (old);
5809 if (old == NULL)
5810 return (new);
5811 /* Replace a jfreefrag with a jnewblk. */
5812 if (new->wk_type == D_JFREEFRAG) {
5813 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5814 panic("jnewblk_merge: blkno mismatch: %p, %p",
5815 old, new);
5816 cancel_jfreefrag(WK_JFREEFRAG(new));
5817 return (old);
5818 }
5819 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5820 panic("jnewblk_merge: Bad type: old %d new %d\n",
5821 old->wk_type, new->wk_type);
5822 /*
5823 * Handle merging of two jnewblk records that describe
5824 * different sets of fragments in the same block.
5825 */
5826 jnewblk = WK_JNEWBLK(old);
5827 njnewblk = WK_JNEWBLK(new);
5828 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5829 panic("jnewblk_merge: Merging disparate blocks.");
5830 /*
5831 * The record may be rolled back in the cg.
5832 */
5833 if (jnewblk->jn_state & UNDONE) {
5834 jnewblk->jn_state &= ~UNDONE;
5835 njnewblk->jn_state |= UNDONE;
5836 njnewblk->jn_state &= ~ATTACHED;
5837 }
5838 /*
5839 * We modify the newer addref and free the older so that if neither
5840 * has been written the most up-to-date copy will be on disk. If
5841 * both have been written but rolled back we only temporarily need
5842 * one of them to fix the bits when the cg write completes.
5843 */
5844 jnewblk->jn_state |= ATTACHED | COMPLETE;
5845 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5846 cancel_jnewblk(jnewblk, wkhd);
5847 WORKLIST_REMOVE(&jnewblk->jn_list);
5848 free_jnewblk(jnewblk);
5849 return (new);
5850 }
5851
5852 /*
5853 * Replace an old allocdirect dependency with a newer one.
5854 */
5855 static void
allocdirect_merge(adphead,newadp,oldadp)5856 allocdirect_merge(adphead, newadp, oldadp)
5857 struct allocdirectlst *adphead; /* head of list holding allocdirects */
5858 struct allocdirect *newadp; /* allocdirect being added */
5859 struct allocdirect *oldadp; /* existing allocdirect being checked */
5860 {
5861 struct worklist *wk;
5862 struct freefrag *freefrag;
5863
5864 freefrag = NULL;
5865 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5866 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5867 newadp->ad_oldsize != oldadp->ad_newsize ||
5868 newadp->ad_offset >= UFS_NDADDR)
5869 panic("%s %jd != new %jd || old size %ld != new %ld",
5870 "allocdirect_merge: old blkno",
5871 (intmax_t)newadp->ad_oldblkno,
5872 (intmax_t)oldadp->ad_newblkno,
5873 newadp->ad_oldsize, oldadp->ad_newsize);
5874 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5875 newadp->ad_oldsize = oldadp->ad_oldsize;
5876 /*
5877 * If the old dependency had a fragment to free or had never
5878 * previously had a block allocated, then the new dependency
5879 * can immediately post its freefrag and adopt the old freefrag.
5880 * This action is done by swapping the freefrag dependencies.
5881 * The new dependency gains the old one's freefrag, and the
5882 * old one gets the new one and then immediately puts it on
5883 * the worklist when it is freed by free_newblk. It is
5884 * not possible to do this swap when the old dependency had a
5885 * non-zero size but no previous fragment to free. This condition
5886 * arises when the new block is an extension of the old block.
5887 * Here, the first part of the fragment allocated to the new
5888 * dependency is part of the block currently claimed on disk by
5889 * the old dependency, so cannot legitimately be freed until the
5890 * conditions for the new dependency are fulfilled.
5891 */
5892 freefrag = newadp->ad_freefrag;
5893 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5894 newadp->ad_freefrag = oldadp->ad_freefrag;
5895 oldadp->ad_freefrag = freefrag;
5896 }
5897 /*
5898 * If we are tracking a new directory-block allocation,
5899 * move it from the old allocdirect to the new allocdirect.
5900 */
5901 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5902 WORKLIST_REMOVE(wk);
5903 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5904 panic("allocdirect_merge: extra newdirblk");
5905 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5906 }
5907 TAILQ_REMOVE(adphead, oldadp, ad_next);
5908 /*
5909 * We need to move any journal dependencies over to the freefrag
5910 * that releases this block if it exists. Otherwise we are
5911 * extending an existing block and we'll wait until that is
5912 * complete to release the journal space and extend the
5913 * new journal to cover this old space as well.
5914 */
5915 if (freefrag == NULL) {
5916 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5917 panic("allocdirect_merge: %jd != %jd",
5918 oldadp->ad_newblkno, newadp->ad_newblkno);
5919 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5920 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5921 &oldadp->ad_block.nb_jnewblk->jn_list,
5922 &newadp->ad_block.nb_jwork);
5923 oldadp->ad_block.nb_jnewblk = NULL;
5924 cancel_newblk(&oldadp->ad_block, NULL,
5925 &newadp->ad_block.nb_jwork);
5926 } else {
5927 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5928 &freefrag->ff_list, &freefrag->ff_jwork);
5929 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5930 &freefrag->ff_jwork);
5931 }
5932 free_newblk(&oldadp->ad_block);
5933 }
5934
5935 /*
5936 * Allocate a jfreefrag structure to journal a single block free.
5937 */
5938 static struct jfreefrag *
newjfreefrag(freefrag,ip,blkno,size,lbn)5939 newjfreefrag(freefrag, ip, blkno, size, lbn)
5940 struct freefrag *freefrag;
5941 struct inode *ip;
5942 ufs2_daddr_t blkno;
5943 long size;
5944 ufs_lbn_t lbn;
5945 {
5946 struct jfreefrag *jfreefrag;
5947 struct fs *fs;
5948
5949 fs = ITOFS(ip);
5950 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5951 M_SOFTDEP_FLAGS);
5952 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5953 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5954 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5955 jfreefrag->fr_ino = ip->i_number;
5956 jfreefrag->fr_lbn = lbn;
5957 jfreefrag->fr_blkno = blkno;
5958 jfreefrag->fr_frags = numfrags(fs, size);
5959 jfreefrag->fr_freefrag = freefrag;
5960
5961 return (jfreefrag);
5962 }
5963
5964 /*
5965 * Allocate a new freefrag structure.
5966 */
5967 static struct freefrag *
newfreefrag(ip,blkno,size,lbn,key)5968 newfreefrag(ip, blkno, size, lbn, key)
5969 struct inode *ip;
5970 ufs2_daddr_t blkno;
5971 long size;
5972 ufs_lbn_t lbn;
5973 u_long key;
5974 {
5975 struct freefrag *freefrag;
5976 struct ufsmount *ump;
5977 struct fs *fs;
5978
5979 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5980 ip->i_number, blkno, size, lbn);
5981 ump = ITOUMP(ip);
5982 fs = ump->um_fs;
5983 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5984 panic("newfreefrag: frag size");
5985 freefrag = malloc(sizeof(struct freefrag),
5986 M_FREEFRAG, M_SOFTDEP_FLAGS);
5987 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5988 freefrag->ff_state = ATTACHED;
5989 LIST_INIT(&freefrag->ff_jwork);
5990 freefrag->ff_inum = ip->i_number;
5991 freefrag->ff_vtype = ITOV(ip)->v_type;
5992 freefrag->ff_blkno = blkno;
5993 freefrag->ff_fragsize = size;
5994 freefrag->ff_key = key;
5995
5996 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5997 freefrag->ff_jdep = (struct worklist *)
5998 newjfreefrag(freefrag, ip, blkno, size, lbn);
5999 } else {
6000 freefrag->ff_state |= DEPCOMPLETE;
6001 freefrag->ff_jdep = NULL;
6002 }
6003
6004 return (freefrag);
6005 }
6006
6007 /*
6008 * This workitem de-allocates fragments that were replaced during
6009 * file block allocation.
6010 */
6011 static void
handle_workitem_freefrag(freefrag)6012 handle_workitem_freefrag(freefrag)
6013 struct freefrag *freefrag;
6014 {
6015 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
6016 struct workhead wkhd;
6017
6018 CTR3(KTR_SUJ,
6019 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
6020 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
6021 /*
6022 * It would be illegal to add new completion items to the
6023 * freefrag after it was schedule to be done so it must be
6024 * safe to modify the list head here.
6025 */
6026 LIST_INIT(&wkhd);
6027 ACQUIRE_LOCK(ump);
6028 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
6029 /*
6030 * If the journal has not been written we must cancel it here.
6031 */
6032 if (freefrag->ff_jdep) {
6033 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
6034 panic("handle_workitem_freefrag: Unexpected type %d\n",
6035 freefrag->ff_jdep->wk_type);
6036 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
6037 }
6038 FREE_LOCK(ump);
6039 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
6040 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
6041 &wkhd, freefrag->ff_key);
6042 ACQUIRE_LOCK(ump);
6043 WORKITEM_FREE(freefrag, D_FREEFRAG);
6044 FREE_LOCK(ump);
6045 }
6046
6047 /*
6048 * Set up a dependency structure for an external attributes data block.
6049 * This routine follows much of the structure of softdep_setup_allocdirect.
6050 * See the description of softdep_setup_allocdirect above for details.
6051 */
6052 void
softdep_setup_allocext(ip,off,newblkno,oldblkno,newsize,oldsize,bp)6053 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
6054 struct inode *ip;
6055 ufs_lbn_t off;
6056 ufs2_daddr_t newblkno;
6057 ufs2_daddr_t oldblkno;
6058 long newsize;
6059 long oldsize;
6060 struct buf *bp;
6061 {
6062 struct allocdirect *adp, *oldadp;
6063 struct allocdirectlst *adphead;
6064 struct freefrag *freefrag;
6065 struct inodedep *inodedep;
6066 struct jnewblk *jnewblk;
6067 struct newblk *newblk;
6068 struct mount *mp;
6069 struct ufsmount *ump;
6070 ufs_lbn_t lbn;
6071
6072 mp = ITOVFS(ip);
6073 ump = VFSTOUFS(mp);
6074 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6075 ("softdep_setup_allocext called on non-softdep filesystem"));
6076 KASSERT(off < UFS_NXADDR,
6077 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
6078
6079 lbn = bp->b_lblkno;
6080 if (oldblkno && oldblkno != newblkno)
6081 /*
6082 * The usual case is that a smaller fragment that
6083 * was just allocated has been replaced with a bigger
6084 * fragment or a full-size block. If it is marked as
6085 * B_DELWRI, the current contents have not been written
6086 * to disk. It is possible that the block was written
6087 * earlier, but very uncommon. If the block has never
6088 * been written, there is no need to send a BIO_DELETE
6089 * for it when it is freed. The gain from avoiding the
6090 * TRIMs for the common case of unwritten blocks far
6091 * exceeds the cost of the write amplification for the
6092 * uncommon case of failing to send a TRIM for a block
6093 * that had been written.
6094 */
6095 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
6096 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
6097 else
6098 freefrag = NULL;
6099
6100 ACQUIRE_LOCK(ump);
6101 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
6102 panic("softdep_setup_allocext: lost block");
6103 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6104 ("softdep_setup_allocext: newblk already initialized"));
6105 /*
6106 * Convert the newblk to an allocdirect.
6107 */
6108 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
6109 adp = (struct allocdirect *)newblk;
6110 newblk->nb_freefrag = freefrag;
6111 adp->ad_offset = off;
6112 adp->ad_oldblkno = oldblkno;
6113 adp->ad_newsize = newsize;
6114 adp->ad_oldsize = oldsize;
6115 adp->ad_state |= EXTDATA;
6116
6117 /*
6118 * Finish initializing the journal.
6119 */
6120 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6121 jnewblk->jn_ino = ip->i_number;
6122 jnewblk->jn_lbn = lbn;
6123 add_to_journal(&jnewblk->jn_list);
6124 }
6125 if (freefrag && freefrag->ff_jdep != NULL &&
6126 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6127 add_to_journal(freefrag->ff_jdep);
6128 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6129 adp->ad_inodedep = inodedep;
6130
6131 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
6132 /*
6133 * The list of allocdirects must be kept in sorted and ascending
6134 * order so that the rollback routines can quickly determine the
6135 * first uncommitted block (the size of the file stored on disk
6136 * ends at the end of the lowest committed fragment, or if there
6137 * are no fragments, at the end of the highest committed block).
6138 * Since files generally grow, the typical case is that the new
6139 * block is to be added at the end of the list. We speed this
6140 * special case by checking against the last allocdirect in the
6141 * list before laboriously traversing the list looking for the
6142 * insertion point.
6143 */
6144 adphead = &inodedep->id_newextupdt;
6145 oldadp = TAILQ_LAST(adphead, allocdirectlst);
6146 if (oldadp == NULL || oldadp->ad_offset <= off) {
6147 /* insert at end of list */
6148 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
6149 if (oldadp != NULL && oldadp->ad_offset == off)
6150 allocdirect_merge(adphead, adp, oldadp);
6151 FREE_LOCK(ump);
6152 return;
6153 }
6154 TAILQ_FOREACH(oldadp, adphead, ad_next) {
6155 if (oldadp->ad_offset >= off)
6156 break;
6157 }
6158 if (oldadp == NULL)
6159 panic("softdep_setup_allocext: lost entry");
6160 /* insert in middle of list */
6161 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
6162 if (oldadp->ad_offset == off)
6163 allocdirect_merge(adphead, adp, oldadp);
6164 FREE_LOCK(ump);
6165 }
6166
6167 /*
6168 * Indirect block allocation dependencies.
6169 *
6170 * The same dependencies that exist for a direct block also exist when
6171 * a new block is allocated and pointed to by an entry in a block of
6172 * indirect pointers. The undo/redo states described above are also
6173 * used here. Because an indirect block contains many pointers that
6174 * may have dependencies, a second copy of the entire in-memory indirect
6175 * block is kept. The buffer cache copy is always completely up-to-date.
6176 * The second copy, which is used only as a source for disk writes,
6177 * contains only the safe pointers (i.e., those that have no remaining
6178 * update dependencies). The second copy is freed when all pointers
6179 * are safe. The cache is not allowed to replace indirect blocks with
6180 * pending update dependencies. If a buffer containing an indirect
6181 * block with dependencies is written, these routines will mark it
6182 * dirty again. It can only be successfully written once all the
6183 * dependencies are removed. The ffs_fsync routine in conjunction with
6184 * softdep_sync_metadata work together to get all the dependencies
6185 * removed so that a file can be successfully written to disk. Three
6186 * procedures are used when setting up indirect block pointer
6187 * dependencies. The division is necessary because of the organization
6188 * of the "balloc" routine and because of the distinction between file
6189 * pages and file metadata blocks.
6190 */
6191
6192 /*
6193 * Allocate a new allocindir structure.
6194 */
6195 static struct allocindir *
newallocindir(ip,ptrno,newblkno,oldblkno,lbn)6196 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
6197 struct inode *ip; /* inode for file being extended */
6198 int ptrno; /* offset of pointer in indirect block */
6199 ufs2_daddr_t newblkno; /* disk block number being added */
6200 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
6201 ufs_lbn_t lbn;
6202 {
6203 struct newblk *newblk;
6204 struct allocindir *aip;
6205 struct freefrag *freefrag;
6206 struct jnewblk *jnewblk;
6207
6208 if (oldblkno)
6209 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
6210 SINGLETON_KEY);
6211 else
6212 freefrag = NULL;
6213 ACQUIRE_LOCK(ITOUMP(ip));
6214 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
6215 panic("new_allocindir: lost block");
6216 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6217 ("newallocindir: newblk already initialized"));
6218 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
6219 newblk->nb_freefrag = freefrag;
6220 aip = (struct allocindir *)newblk;
6221 aip->ai_offset = ptrno;
6222 aip->ai_oldblkno = oldblkno;
6223 aip->ai_lbn = lbn;
6224 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6225 jnewblk->jn_ino = ip->i_number;
6226 jnewblk->jn_lbn = lbn;
6227 add_to_journal(&jnewblk->jn_list);
6228 }
6229 if (freefrag && freefrag->ff_jdep != NULL &&
6230 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6231 add_to_journal(freefrag->ff_jdep);
6232 return (aip);
6233 }
6234
6235 /*
6236 * Called just before setting an indirect block pointer
6237 * to a newly allocated file page.
6238 */
6239 void
softdep_setup_allocindir_page(ip,lbn,bp,ptrno,newblkno,oldblkno,nbp)6240 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
6241 struct inode *ip; /* inode for file being extended */
6242 ufs_lbn_t lbn; /* allocated block number within file */
6243 struct buf *bp; /* buffer with indirect blk referencing page */
6244 int ptrno; /* offset of pointer in indirect block */
6245 ufs2_daddr_t newblkno; /* disk block number being added */
6246 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
6247 struct buf *nbp; /* buffer holding allocated page */
6248 {
6249 struct inodedep *inodedep;
6250 struct freefrag *freefrag;
6251 struct allocindir *aip;
6252 struct pagedep *pagedep;
6253 struct mount *mp;
6254 struct ufsmount *ump;
6255
6256 mp = ITOVFS(ip);
6257 ump = VFSTOUFS(mp);
6258 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6259 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
6260 KASSERT(lbn == nbp->b_lblkno,
6261 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
6262 lbn, bp->b_lblkno));
6263 CTR4(KTR_SUJ,
6264 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
6265 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
6266 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
6267 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
6268 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6269 /*
6270 * If we are allocating a directory page, then we must
6271 * allocate an associated pagedep to track additions and
6272 * deletions.
6273 */
6274 if ((ip->i_mode & IFMT) == IFDIR)
6275 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
6276 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6277 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
6278 FREE_LOCK(ump);
6279 if (freefrag)
6280 handle_workitem_freefrag(freefrag);
6281 }
6282
6283 /*
6284 * Called just before setting an indirect block pointer to a
6285 * newly allocated indirect block.
6286 */
6287 void
softdep_setup_allocindir_meta(nbp,ip,bp,ptrno,newblkno)6288 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
6289 struct buf *nbp; /* newly allocated indirect block */
6290 struct inode *ip; /* inode for file being extended */
6291 struct buf *bp; /* indirect block referencing allocated block */
6292 int ptrno; /* offset of pointer in indirect block */
6293 ufs2_daddr_t newblkno; /* disk block number being added */
6294 {
6295 struct inodedep *inodedep;
6296 struct allocindir *aip;
6297 struct ufsmount *ump;
6298 ufs_lbn_t lbn;
6299
6300 ump = ITOUMP(ip);
6301 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6302 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
6303 CTR3(KTR_SUJ,
6304 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
6305 ip->i_number, newblkno, ptrno);
6306 lbn = nbp->b_lblkno;
6307 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
6308 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
6309 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
6310 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6311 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
6312 panic("softdep_setup_allocindir_meta: Block already existed");
6313 FREE_LOCK(ump);
6314 }
6315
6316 static void
indirdep_complete(indirdep)6317 indirdep_complete(indirdep)
6318 struct indirdep *indirdep;
6319 {
6320 struct allocindir *aip;
6321
6322 LIST_REMOVE(indirdep, ir_next);
6323 indirdep->ir_state |= DEPCOMPLETE;
6324
6325 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
6326 LIST_REMOVE(aip, ai_next);
6327 free_newblk(&aip->ai_block);
6328 }
6329 /*
6330 * If this indirdep is not attached to a buf it was simply waiting
6331 * on completion to clear completehd. free_indirdep() asserts
6332 * that nothing is dangling.
6333 */
6334 if ((indirdep->ir_state & ONWORKLIST) == 0)
6335 free_indirdep(indirdep);
6336 }
6337
6338 static struct indirdep *
indirdep_lookup(mp,ip,bp)6339 indirdep_lookup(mp, ip, bp)
6340 struct mount *mp;
6341 struct inode *ip;
6342 struct buf *bp;
6343 {
6344 struct indirdep *indirdep, *newindirdep;
6345 struct newblk *newblk;
6346 struct ufsmount *ump;
6347 struct worklist *wk;
6348 struct fs *fs;
6349 ufs2_daddr_t blkno;
6350
6351 ump = VFSTOUFS(mp);
6352 LOCK_OWNED(ump);
6353 indirdep = NULL;
6354 newindirdep = NULL;
6355 fs = ump->um_fs;
6356 for (;;) {
6357 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6358 if (wk->wk_type != D_INDIRDEP)
6359 continue;
6360 indirdep = WK_INDIRDEP(wk);
6361 break;
6362 }
6363 /* Found on the buffer worklist, no new structure to free. */
6364 if (indirdep != NULL && newindirdep == NULL)
6365 return (indirdep);
6366 if (indirdep != NULL && newindirdep != NULL)
6367 panic("indirdep_lookup: simultaneous create");
6368 /* None found on the buffer and a new structure is ready. */
6369 if (indirdep == NULL && newindirdep != NULL)
6370 break;
6371 /* None found and no new structure available. */
6372 FREE_LOCK(ump);
6373 newindirdep = malloc(sizeof(struct indirdep),
6374 M_INDIRDEP, M_SOFTDEP_FLAGS);
6375 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6376 newindirdep->ir_state = ATTACHED;
6377 if (I_IS_UFS1(ip))
6378 newindirdep->ir_state |= UFS1FMT;
6379 TAILQ_INIT(&newindirdep->ir_trunc);
6380 newindirdep->ir_saveddata = NULL;
6381 LIST_INIT(&newindirdep->ir_deplisthd);
6382 LIST_INIT(&newindirdep->ir_donehd);
6383 LIST_INIT(&newindirdep->ir_writehd);
6384 LIST_INIT(&newindirdep->ir_completehd);
6385 if (bp->b_blkno == bp->b_lblkno) {
6386 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6387 NULL, NULL);
6388 bp->b_blkno = blkno;
6389 }
6390 newindirdep->ir_freeblks = NULL;
6391 newindirdep->ir_savebp =
6392 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6393 newindirdep->ir_bp = bp;
6394 BUF_KERNPROC(newindirdep->ir_savebp);
6395 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6396 ACQUIRE_LOCK(ump);
6397 }
6398 indirdep = newindirdep;
6399 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6400 /*
6401 * If the block is not yet allocated we don't set DEPCOMPLETE so
6402 * that we don't free dependencies until the pointers are valid.
6403 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6404 * than using the hash.
6405 */
6406 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6407 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6408 else
6409 indirdep->ir_state |= DEPCOMPLETE;
6410 return (indirdep);
6411 }
6412
6413 /*
6414 * Called to finish the allocation of the "aip" allocated
6415 * by one of the two routines above.
6416 */
6417 static struct freefrag *
setup_allocindir_phase2(bp,ip,inodedep,aip,lbn)6418 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
6419 struct buf *bp; /* in-memory copy of the indirect block */
6420 struct inode *ip; /* inode for file being extended */
6421 struct inodedep *inodedep; /* Inodedep for ip */
6422 struct allocindir *aip; /* allocindir allocated by the above routines */
6423 ufs_lbn_t lbn; /* Logical block number for this block. */
6424 {
6425 struct fs *fs __diagused;
6426 struct indirdep *indirdep;
6427 struct allocindir *oldaip;
6428 struct freefrag *freefrag;
6429 struct mount *mp;
6430 struct ufsmount *ump;
6431
6432 mp = ITOVFS(ip);
6433 ump = VFSTOUFS(mp);
6434 LOCK_OWNED(ump);
6435 fs = ump->um_fs;
6436 if (bp->b_lblkno >= 0)
6437 panic("setup_allocindir_phase2: not indir blk");
6438 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6439 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6440 indirdep = indirdep_lookup(mp, ip, bp);
6441 KASSERT(indirdep->ir_savebp != NULL,
6442 ("setup_allocindir_phase2 NULL ir_savebp"));
6443 aip->ai_indirdep = indirdep;
6444 /*
6445 * Check for an unwritten dependency for this indirect offset. If
6446 * there is, merge the old dependency into the new one. This happens
6447 * as a result of reallocblk only.
6448 */
6449 freefrag = NULL;
6450 if (aip->ai_oldblkno != 0) {
6451 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6452 if (oldaip->ai_offset == aip->ai_offset) {
6453 freefrag = allocindir_merge(aip, oldaip);
6454 goto done;
6455 }
6456 }
6457 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6458 if (oldaip->ai_offset == aip->ai_offset) {
6459 freefrag = allocindir_merge(aip, oldaip);
6460 goto done;
6461 }
6462 }
6463 }
6464 done:
6465 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6466 return (freefrag);
6467 }
6468
6469 /*
6470 * Merge two allocindirs which refer to the same block. Move newblock
6471 * dependencies and setup the freefrags appropriately.
6472 */
6473 static struct freefrag *
allocindir_merge(aip,oldaip)6474 allocindir_merge(aip, oldaip)
6475 struct allocindir *aip;
6476 struct allocindir *oldaip;
6477 {
6478 struct freefrag *freefrag;
6479 struct worklist *wk;
6480
6481 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6482 panic("allocindir_merge: blkno");
6483 aip->ai_oldblkno = oldaip->ai_oldblkno;
6484 freefrag = aip->ai_freefrag;
6485 aip->ai_freefrag = oldaip->ai_freefrag;
6486 oldaip->ai_freefrag = NULL;
6487 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6488 /*
6489 * If we are tracking a new directory-block allocation,
6490 * move it from the old allocindir to the new allocindir.
6491 */
6492 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6493 WORKLIST_REMOVE(wk);
6494 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6495 panic("allocindir_merge: extra newdirblk");
6496 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6497 }
6498 /*
6499 * We can skip journaling for this freefrag and just complete
6500 * any pending journal work for the allocindir that is being
6501 * removed after the freefrag completes.
6502 */
6503 if (freefrag->ff_jdep)
6504 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6505 LIST_REMOVE(oldaip, ai_next);
6506 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6507 &freefrag->ff_list, &freefrag->ff_jwork);
6508 free_newblk(&oldaip->ai_block);
6509
6510 return (freefrag);
6511 }
6512
6513 static inline void
setup_freedirect(freeblks,ip,i,needj)6514 setup_freedirect(freeblks, ip, i, needj)
6515 struct freeblks *freeblks;
6516 struct inode *ip;
6517 int i;
6518 int needj;
6519 {
6520 struct ufsmount *ump;
6521 ufs2_daddr_t blkno;
6522 int frags;
6523
6524 blkno = DIP(ip, i_db[i]);
6525 if (blkno == 0)
6526 return;
6527 DIP_SET(ip, i_db[i], 0);
6528 ump = ITOUMP(ip);
6529 frags = sblksize(ump->um_fs, ip->i_size, i);
6530 frags = numfrags(ump->um_fs, frags);
6531 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6532 }
6533
6534 static inline void
setup_freeext(freeblks,ip,i,needj)6535 setup_freeext(freeblks, ip, i, needj)
6536 struct freeblks *freeblks;
6537 struct inode *ip;
6538 int i;
6539 int needj;
6540 {
6541 struct ufsmount *ump;
6542 ufs2_daddr_t blkno;
6543 int frags;
6544
6545 blkno = ip->i_din2->di_extb[i];
6546 if (blkno == 0)
6547 return;
6548 ip->i_din2->di_extb[i] = 0;
6549 ump = ITOUMP(ip);
6550 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6551 frags = numfrags(ump->um_fs, frags);
6552 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6553 }
6554
6555 static inline void
setup_freeindir(freeblks,ip,i,lbn,needj)6556 setup_freeindir(freeblks, ip, i, lbn, needj)
6557 struct freeblks *freeblks;
6558 struct inode *ip;
6559 int i;
6560 ufs_lbn_t lbn;
6561 int needj;
6562 {
6563 struct ufsmount *ump;
6564 ufs2_daddr_t blkno;
6565
6566 blkno = DIP(ip, i_ib[i]);
6567 if (blkno == 0)
6568 return;
6569 DIP_SET(ip, i_ib[i], 0);
6570 ump = ITOUMP(ip);
6571 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6572 0, needj);
6573 }
6574
6575 static inline struct freeblks *
newfreeblks(mp,ip)6576 newfreeblks(mp, ip)
6577 struct mount *mp;
6578 struct inode *ip;
6579 {
6580 struct freeblks *freeblks;
6581
6582 freeblks = malloc(sizeof(struct freeblks),
6583 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6584 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6585 LIST_INIT(&freeblks->fb_jblkdephd);
6586 LIST_INIT(&freeblks->fb_jwork);
6587 freeblks->fb_ref = 0;
6588 freeblks->fb_cgwait = 0;
6589 freeblks->fb_state = ATTACHED;
6590 freeblks->fb_uid = ip->i_uid;
6591 freeblks->fb_inum = ip->i_number;
6592 freeblks->fb_vtype = ITOV(ip)->v_type;
6593 freeblks->fb_modrev = DIP(ip, i_modrev);
6594 freeblks->fb_devvp = ITODEVVP(ip);
6595 freeblks->fb_chkcnt = 0;
6596 freeblks->fb_len = 0;
6597
6598 return (freeblks);
6599 }
6600
6601 static void
trunc_indirdep(indirdep,freeblks,bp,off)6602 trunc_indirdep(indirdep, freeblks, bp, off)
6603 struct indirdep *indirdep;
6604 struct freeblks *freeblks;
6605 struct buf *bp;
6606 int off;
6607 {
6608 struct allocindir *aip, *aipn;
6609
6610 /*
6611 * The first set of allocindirs won't be in savedbp.
6612 */
6613 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6614 if (aip->ai_offset > off)
6615 cancel_allocindir(aip, bp, freeblks, 1);
6616 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6617 if (aip->ai_offset > off)
6618 cancel_allocindir(aip, bp, freeblks, 1);
6619 /*
6620 * These will exist in savedbp.
6621 */
6622 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6623 if (aip->ai_offset > off)
6624 cancel_allocindir(aip, NULL, freeblks, 0);
6625 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6626 if (aip->ai_offset > off)
6627 cancel_allocindir(aip, NULL, freeblks, 0);
6628 }
6629
6630 /*
6631 * Follow the chain of indirects down to lastlbn creating a freework
6632 * structure for each. This will be used to start indir_trunc() at
6633 * the right offset and create the journal records for the parrtial
6634 * truncation. A second step will handle the truncated dependencies.
6635 */
6636 static int
setup_trunc_indir(freeblks,ip,lbn,lastlbn,blkno)6637 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6638 struct freeblks *freeblks;
6639 struct inode *ip;
6640 ufs_lbn_t lbn;
6641 ufs_lbn_t lastlbn;
6642 ufs2_daddr_t blkno;
6643 {
6644 struct indirdep *indirdep;
6645 struct indirdep *indirn;
6646 struct freework *freework;
6647 struct newblk *newblk;
6648 struct mount *mp;
6649 struct ufsmount *ump;
6650 struct buf *bp;
6651 uint8_t *start;
6652 uint8_t *end;
6653 ufs_lbn_t lbnadd;
6654 int level;
6655 int error;
6656 int off;
6657
6658 freework = NULL;
6659 if (blkno == 0)
6660 return (0);
6661 mp = freeblks->fb_list.wk_mp;
6662 ump = VFSTOUFS(mp);
6663 /*
6664 * Here, calls to VOP_BMAP() will fail. However, we already have
6665 * the on-disk address, so we just pass it to bread() instead of
6666 * having bread() attempt to calculate it using VOP_BMAP().
6667 */
6668 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno),
6669 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6670 if (error)
6671 return (error);
6672 level = lbn_level(lbn);
6673 lbnadd = lbn_offset(ump->um_fs, level);
6674 /*
6675 * Compute the offset of the last block we want to keep. Store
6676 * in the freework the first block we want to completely free.
6677 */
6678 off = (lastlbn - -(lbn + level)) / lbnadd;
6679 if (off + 1 == NINDIR(ump->um_fs))
6680 goto nowork;
6681 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6682 /*
6683 * Link the freework into the indirdep. This will prevent any new
6684 * allocations from proceeding until we are finished with the
6685 * truncate and the block is written.
6686 */
6687 ACQUIRE_LOCK(ump);
6688 indirdep = indirdep_lookup(mp, ip, bp);
6689 if (indirdep->ir_freeblks)
6690 panic("setup_trunc_indir: indirdep already truncated.");
6691 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6692 freework->fw_indir = indirdep;
6693 /*
6694 * Cancel any allocindirs that will not make it to disk.
6695 * We have to do this for all copies of the indirdep that
6696 * live on this newblk.
6697 */
6698 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6699 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6700 &newblk) == 0)
6701 panic("setup_trunc_indir: lost block");
6702 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6703 trunc_indirdep(indirn, freeblks, bp, off);
6704 } else
6705 trunc_indirdep(indirdep, freeblks, bp, off);
6706 FREE_LOCK(ump);
6707 /*
6708 * Creation is protected by the buf lock. The saveddata is only
6709 * needed if a full truncation follows a partial truncation but it
6710 * is difficult to allocate in that case so we fetch it anyway.
6711 */
6712 if (indirdep->ir_saveddata == NULL)
6713 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6714 M_SOFTDEP_FLAGS);
6715 nowork:
6716 /* Fetch the blkno of the child and the zero start offset. */
6717 if (I_IS_UFS1(ip)) {
6718 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6719 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6720 } else {
6721 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6722 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6723 }
6724 if (freework) {
6725 /* Zero the truncated pointers. */
6726 end = bp->b_data + bp->b_bcount;
6727 bzero(start, end - start);
6728 bdwrite(bp);
6729 } else
6730 bqrelse(bp);
6731 if (level == 0)
6732 return (0);
6733 lbn++; /* adjust level */
6734 lbn -= (off * lbnadd);
6735 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6736 }
6737
6738 /*
6739 * Complete the partial truncation of an indirect block setup by
6740 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6741 * copy and writes them to disk before the freeblks is allowed to complete.
6742 */
6743 static void
complete_trunc_indir(freework)6744 complete_trunc_indir(freework)
6745 struct freework *freework;
6746 {
6747 struct freework *fwn;
6748 struct indirdep *indirdep;
6749 struct ufsmount *ump;
6750 struct buf *bp;
6751 uintptr_t start;
6752 int count;
6753
6754 ump = VFSTOUFS(freework->fw_list.wk_mp);
6755 LOCK_OWNED(ump);
6756 indirdep = freework->fw_indir;
6757 for (;;) {
6758 bp = indirdep->ir_bp;
6759 /* See if the block was discarded. */
6760 if (bp == NULL)
6761 break;
6762 /* Inline part of getdirtybuf(). We dont want bremfree. */
6763 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6764 break;
6765 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6766 LOCK_PTR(ump)) == 0)
6767 BUF_UNLOCK(bp);
6768 ACQUIRE_LOCK(ump);
6769 }
6770 freework->fw_state |= DEPCOMPLETE;
6771 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6772 /*
6773 * Zero the pointers in the saved copy.
6774 */
6775 if (indirdep->ir_state & UFS1FMT)
6776 start = sizeof(ufs1_daddr_t);
6777 else
6778 start = sizeof(ufs2_daddr_t);
6779 start *= freework->fw_start;
6780 count = indirdep->ir_savebp->b_bcount - start;
6781 start += (uintptr_t)indirdep->ir_savebp->b_data;
6782 bzero((char *)start, count);
6783 /*
6784 * We need to start the next truncation in the list if it has not
6785 * been started yet.
6786 */
6787 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6788 if (fwn != NULL) {
6789 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6790 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6791 if ((fwn->fw_state & ONWORKLIST) == 0)
6792 freework_enqueue(fwn);
6793 }
6794 /*
6795 * If bp is NULL the block was fully truncated, restore
6796 * the saved block list otherwise free it if it is no
6797 * longer needed.
6798 */
6799 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6800 if (bp == NULL)
6801 bcopy(indirdep->ir_saveddata,
6802 indirdep->ir_savebp->b_data,
6803 indirdep->ir_savebp->b_bcount);
6804 free(indirdep->ir_saveddata, M_INDIRDEP);
6805 indirdep->ir_saveddata = NULL;
6806 }
6807 /*
6808 * When bp is NULL there is a full truncation pending. We
6809 * must wait for this full truncation to be journaled before
6810 * we can release this freework because the disk pointers will
6811 * never be written as zero.
6812 */
6813 if (bp == NULL) {
6814 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6815 handle_written_freework(freework);
6816 else
6817 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6818 &freework->fw_list);
6819 if (fwn == NULL) {
6820 freework->fw_indir = (void *)0x0000deadbeef0000;
6821 bp = indirdep->ir_savebp;
6822 indirdep->ir_savebp = NULL;
6823 free_indirdep(indirdep);
6824 FREE_LOCK(ump);
6825 brelse(bp);
6826 ACQUIRE_LOCK(ump);
6827 }
6828 } else {
6829 /* Complete when the real copy is written. */
6830 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6831 BUF_UNLOCK(bp);
6832 }
6833 }
6834
6835 /*
6836 * Calculate the number of blocks we are going to release where datablocks
6837 * is the current total and length is the new file size.
6838 */
6839 static ufs2_daddr_t
blkcount(fs,datablocks,length)6840 blkcount(fs, datablocks, length)
6841 struct fs *fs;
6842 ufs2_daddr_t datablocks;
6843 off_t length;
6844 {
6845 off_t totblks, numblks;
6846
6847 totblks = 0;
6848 numblks = howmany(length, fs->fs_bsize);
6849 if (numblks <= UFS_NDADDR) {
6850 totblks = howmany(length, fs->fs_fsize);
6851 goto out;
6852 }
6853 totblks = blkstofrags(fs, numblks);
6854 numblks -= UFS_NDADDR;
6855 /*
6856 * Count all single, then double, then triple indirects required.
6857 * Subtracting one indirects worth of blocks for each pass
6858 * acknowledges one of each pointed to by the inode.
6859 */
6860 for (;;) {
6861 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6862 numblks -= NINDIR(fs);
6863 if (numblks <= 0)
6864 break;
6865 numblks = howmany(numblks, NINDIR(fs));
6866 }
6867 out:
6868 totblks = fsbtodb(fs, totblks);
6869 /*
6870 * Handle sparse files. We can't reclaim more blocks than the inode
6871 * references. We will correct it later in handle_complete_freeblks()
6872 * when we know the real count.
6873 */
6874 if (totblks > datablocks)
6875 return (0);
6876 return (datablocks - totblks);
6877 }
6878
6879 /*
6880 * Handle freeblocks for journaled softupdate filesystems.
6881 *
6882 * Contrary to normal softupdates, we must preserve the block pointers in
6883 * indirects until their subordinates are free. This is to avoid journaling
6884 * every block that is freed which may consume more space than the journal
6885 * itself. The recovery program will see the free block journals at the
6886 * base of the truncated area and traverse them to reclaim space. The
6887 * pointers in the inode may be cleared immediately after the journal
6888 * records are written because each direct and indirect pointer in the
6889 * inode is recorded in a journal. This permits full truncation to proceed
6890 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6891 *
6892 * The algorithm is as follows:
6893 * 1) Traverse the in-memory state and create journal entries to release
6894 * the relevant blocks and full indirect trees.
6895 * 2) Traverse the indirect block chain adding partial truncation freework
6896 * records to indirects in the path to lastlbn. The freework will
6897 * prevent new allocation dependencies from being satisfied in this
6898 * indirect until the truncation completes.
6899 * 3) Read and lock the inode block, performing an update with the new size
6900 * and pointers. This prevents truncated data from becoming valid on
6901 * disk through step 4.
6902 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6903 * eliminate journal work for those records that do not require it.
6904 * 5) Schedule the journal records to be written followed by the inode block.
6905 * 6) Allocate any necessary frags for the end of file.
6906 * 7) Zero any partially truncated blocks.
6907 *
6908 * From this truncation proceeds asynchronously using the freework and
6909 * indir_trunc machinery. The file will not be extended again into a
6910 * partially truncated indirect block until all work is completed but
6911 * the normal dependency mechanism ensures that it is rolled back/forward
6912 * as appropriate. Further truncation may occur without delay and is
6913 * serialized in indir_trunc().
6914 */
6915 void
softdep_journal_freeblocks(ip,cred,length,flags)6916 softdep_journal_freeblocks(ip, cred, length, flags)
6917 struct inode *ip; /* The inode whose length is to be reduced */
6918 struct ucred *cred;
6919 off_t length; /* The new length for the file */
6920 int flags; /* IO_EXT and/or IO_NORMAL */
6921 {
6922 struct freeblks *freeblks, *fbn;
6923 struct worklist *wk, *wkn;
6924 struct inodedep *inodedep;
6925 struct jblkdep *jblkdep;
6926 struct allocdirect *adp, *adpn;
6927 struct ufsmount *ump;
6928 struct fs *fs;
6929 struct buf *bp;
6930 struct vnode *vp;
6931 struct mount *mp;
6932 daddr_t dbn;
6933 ufs2_daddr_t extblocks, datablocks;
6934 ufs_lbn_t tmpval, lbn, lastlbn;
6935 int frags, lastoff, iboff, allocblock, needj, error, i;
6936
6937 ump = ITOUMP(ip);
6938 mp = UFSTOVFS(ump);
6939 fs = ump->um_fs;
6940 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6941 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6942 vp = ITOV(ip);
6943 needj = 1;
6944 iboff = -1;
6945 allocblock = 0;
6946 extblocks = 0;
6947 datablocks = 0;
6948 frags = 0;
6949 freeblks = newfreeblks(mp, ip);
6950 ACQUIRE_LOCK(ump);
6951 /*
6952 * If we're truncating a removed file that will never be written
6953 * we don't need to journal the block frees. The canceled journals
6954 * for the allocations will suffice.
6955 */
6956 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6957 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6958 length == 0)
6959 needj = 0;
6960 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6961 ip->i_number, length, needj);
6962 FREE_LOCK(ump);
6963 /*
6964 * Calculate the lbn that we are truncating to. This results in -1
6965 * if we're truncating the 0 bytes. So it is the last lbn we want
6966 * to keep, not the first lbn we want to truncate.
6967 */
6968 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6969 lastoff = blkoff(fs, length);
6970 /*
6971 * Compute frags we are keeping in lastlbn. 0 means all.
6972 */
6973 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6974 frags = fragroundup(fs, lastoff);
6975 /* adp offset of last valid allocdirect. */
6976 iboff = lastlbn;
6977 } else if (lastlbn > 0)
6978 iboff = UFS_NDADDR;
6979 if (fs->fs_magic == FS_UFS2_MAGIC)
6980 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6981 /*
6982 * Handle normal data blocks and indirects. This section saves
6983 * values used after the inode update to complete frag and indirect
6984 * truncation.
6985 */
6986 if ((flags & IO_NORMAL) != 0) {
6987 /*
6988 * Handle truncation of whole direct and indirect blocks.
6989 */
6990 for (i = iboff + 1; i < UFS_NDADDR; i++)
6991 setup_freedirect(freeblks, ip, i, needj);
6992 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6993 i < UFS_NIADDR;
6994 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6995 /* Release a whole indirect tree. */
6996 if (lbn > lastlbn) {
6997 setup_freeindir(freeblks, ip, i, -lbn -i,
6998 needj);
6999 continue;
7000 }
7001 iboff = i + UFS_NDADDR;
7002 /*
7003 * Traverse partially truncated indirect tree.
7004 */
7005 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
7006 setup_trunc_indir(freeblks, ip, -lbn - i,
7007 lastlbn, DIP(ip, i_ib[i]));
7008 }
7009 /*
7010 * Handle partial truncation to a frag boundary.
7011 */
7012 if (frags) {
7013 ufs2_daddr_t blkno;
7014 long oldfrags;
7015
7016 oldfrags = blksize(fs, ip, lastlbn);
7017 blkno = DIP(ip, i_db[lastlbn]);
7018 if (blkno && oldfrags != frags) {
7019 oldfrags -= frags;
7020 oldfrags = numfrags(fs, oldfrags);
7021 blkno += numfrags(fs, frags);
7022 newfreework(ump, freeblks, NULL, lastlbn,
7023 blkno, oldfrags, 0, needj);
7024 if (needj)
7025 adjust_newfreework(freeblks,
7026 numfrags(fs, frags));
7027 } else if (blkno == 0)
7028 allocblock = 1;
7029 }
7030 /*
7031 * Add a journal record for partial truncate if we are
7032 * handling indirect blocks. Non-indirects need no extra
7033 * journaling.
7034 */
7035 if (length != 0 && lastlbn >= UFS_NDADDR) {
7036 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
7037 newjtrunc(freeblks, length, 0);
7038 }
7039 ip->i_size = length;
7040 DIP_SET(ip, i_size, ip->i_size);
7041 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7042 datablocks = DIP(ip, i_blocks) - extblocks;
7043 if (length != 0)
7044 datablocks = blkcount(fs, datablocks, length);
7045 freeblks->fb_len = length;
7046 }
7047 if ((flags & IO_EXT) != 0) {
7048 for (i = 0; i < UFS_NXADDR; i++)
7049 setup_freeext(freeblks, ip, i, needj);
7050 ip->i_din2->di_extsize = 0;
7051 datablocks += extblocks;
7052 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7053 }
7054 #ifdef QUOTA
7055 /* Reference the quotas in case the block count is wrong in the end. */
7056 quotaref(vp, freeblks->fb_quota);
7057 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
7058 #endif
7059 freeblks->fb_chkcnt = -datablocks;
7060 UFS_LOCK(ump);
7061 fs->fs_pendingblocks += datablocks;
7062 UFS_UNLOCK(ump);
7063 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7064 /*
7065 * Handle truncation of incomplete alloc direct dependencies. We
7066 * hold the inode block locked to prevent incomplete dependencies
7067 * from reaching the disk while we are eliminating those that
7068 * have been truncated. This is a partially inlined ffs_update().
7069 */
7070 ufs_itimes(vp);
7071 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
7072 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
7073 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
7074 NULL, NULL, 0, cred, 0, NULL, &bp);
7075 if (error) {
7076 softdep_error("softdep_journal_freeblocks", error);
7077 return;
7078 }
7079 if (bp->b_bufsize == fs->fs_bsize)
7080 bp->b_flags |= B_CLUSTEROK;
7081 softdep_update_inodeblock(ip, bp, 0);
7082 if (ump->um_fstype == UFS1) {
7083 *((struct ufs1_dinode *)bp->b_data +
7084 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
7085 } else {
7086 ffs_update_dinode_ckhash(fs, ip->i_din2);
7087 *((struct ufs2_dinode *)bp->b_data +
7088 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
7089 }
7090 ACQUIRE_LOCK(ump);
7091 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7092 if ((inodedep->id_state & IOSTARTED) != 0)
7093 panic("softdep_setup_freeblocks: inode busy");
7094 /*
7095 * Add the freeblks structure to the list of operations that
7096 * must await the zero'ed inode being written to disk. If we
7097 * still have a bitmap dependency (needj), then the inode
7098 * has never been written to disk, so we can process the
7099 * freeblks below once we have deleted the dependencies.
7100 */
7101 if (needj)
7102 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7103 else
7104 freeblks->fb_state |= COMPLETE;
7105 if ((flags & IO_NORMAL) != 0) {
7106 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
7107 if (adp->ad_offset > iboff)
7108 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7109 freeblks);
7110 /*
7111 * Truncate the allocdirect. We could eliminate
7112 * or modify journal records as well.
7113 */
7114 else if (adp->ad_offset == iboff && frags)
7115 adp->ad_newsize = frags;
7116 }
7117 }
7118 if ((flags & IO_EXT) != 0)
7119 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7120 cancel_allocdirect(&inodedep->id_extupdt, adp,
7121 freeblks);
7122 /*
7123 * Scan the bufwait list for newblock dependencies that will never
7124 * make it to disk.
7125 */
7126 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
7127 if (wk->wk_type != D_ALLOCDIRECT)
7128 continue;
7129 adp = WK_ALLOCDIRECT(wk);
7130 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
7131 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
7132 cancel_jfreeblk(freeblks, adp->ad_newblkno);
7133 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
7134 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7135 }
7136 }
7137 /*
7138 * Add journal work.
7139 */
7140 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
7141 add_to_journal(&jblkdep->jb_list);
7142 FREE_LOCK(ump);
7143 bdwrite(bp);
7144 /*
7145 * Truncate dependency structures beyond length.
7146 */
7147 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
7148 /*
7149 * This is only set when we need to allocate a fragment because
7150 * none existed at the end of a frag-sized file. It handles only
7151 * allocating a new, zero filled block.
7152 */
7153 if (allocblock) {
7154 ip->i_size = length - lastoff;
7155 DIP_SET(ip, i_size, ip->i_size);
7156 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
7157 if (error != 0) {
7158 softdep_error("softdep_journal_freeblks", error);
7159 return;
7160 }
7161 ip->i_size = length;
7162 DIP_SET(ip, i_size, length);
7163 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
7164 allocbuf(bp, frags);
7165 ffs_update(vp, 0);
7166 bawrite(bp);
7167 } else if (lastoff != 0 && vp->v_type != VDIR) {
7168 int size;
7169
7170 /*
7171 * Zero the end of a truncated frag or block.
7172 */
7173 size = sblksize(fs, length, lastlbn);
7174 error = bread(vp, lastlbn, size, cred, &bp);
7175 if (error == 0) {
7176 bzero((char *)bp->b_data + lastoff, size - lastoff);
7177 bawrite(bp);
7178 } else if (!ffs_fsfail_cleanup(ump, error)) {
7179 softdep_error("softdep_journal_freeblks", error);
7180 return;
7181 }
7182 }
7183 ACQUIRE_LOCK(ump);
7184 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7185 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
7186 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
7187 /*
7188 * We zero earlier truncations so they don't erroneously
7189 * update i_blocks.
7190 */
7191 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
7192 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
7193 fbn->fb_len = 0;
7194 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
7195 LIST_EMPTY(&freeblks->fb_jblkdephd))
7196 freeblks->fb_state |= INPROGRESS;
7197 else
7198 freeblks = NULL;
7199 FREE_LOCK(ump);
7200 if (freeblks)
7201 handle_workitem_freeblocks(freeblks, 0);
7202 trunc_pages(ip, length, extblocks, flags);
7203
7204 }
7205
7206 /*
7207 * Flush a JOP_SYNC to the journal.
7208 */
7209 void
softdep_journal_fsync(ip)7210 softdep_journal_fsync(ip)
7211 struct inode *ip;
7212 {
7213 struct jfsync *jfsync;
7214 struct ufsmount *ump;
7215
7216 ump = ITOUMP(ip);
7217 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7218 ("softdep_journal_fsync called on non-softdep filesystem"));
7219 if ((ip->i_flag & IN_TRUNCATED) == 0)
7220 return;
7221 ip->i_flag &= ~IN_TRUNCATED;
7222 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
7223 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
7224 jfsync->jfs_size = ip->i_size;
7225 jfsync->jfs_ino = ip->i_number;
7226 ACQUIRE_LOCK(ump);
7227 add_to_journal(&jfsync->jfs_list);
7228 jwait(&jfsync->jfs_list, MNT_WAIT);
7229 FREE_LOCK(ump);
7230 }
7231
7232 /*
7233 * Block de-allocation dependencies.
7234 *
7235 * When blocks are de-allocated, the on-disk pointers must be nullified before
7236 * the blocks are made available for use by other files. (The true
7237 * requirement is that old pointers must be nullified before new on-disk
7238 * pointers are set. We chose this slightly more stringent requirement to
7239 * reduce complexity.) Our implementation handles this dependency by updating
7240 * the inode (or indirect block) appropriately but delaying the actual block
7241 * de-allocation (i.e., freemap and free space count manipulation) until
7242 * after the updated versions reach stable storage. After the disk is
7243 * updated, the blocks can be safely de-allocated whenever it is convenient.
7244 * This implementation handles only the common case of reducing a file's
7245 * length to zero. Other cases are handled by the conventional synchronous
7246 * write approach.
7247 *
7248 * The ffs implementation with which we worked double-checks
7249 * the state of the block pointers and file size as it reduces
7250 * a file's length. Some of this code is replicated here in our
7251 * soft updates implementation. The freeblks->fb_chkcnt field is
7252 * used to transfer a part of this information to the procedure
7253 * that eventually de-allocates the blocks.
7254 *
7255 * This routine should be called from the routine that shortens
7256 * a file's length, before the inode's size or block pointers
7257 * are modified. It will save the block pointer information for
7258 * later release and zero the inode so that the calling routine
7259 * can release it.
7260 */
7261 void
softdep_setup_freeblocks(ip,length,flags)7262 softdep_setup_freeblocks(ip, length, flags)
7263 struct inode *ip; /* The inode whose length is to be reduced */
7264 off_t length; /* The new length for the file */
7265 int flags; /* IO_EXT and/or IO_NORMAL */
7266 {
7267 struct ufs1_dinode *dp1;
7268 struct ufs2_dinode *dp2;
7269 struct freeblks *freeblks;
7270 struct inodedep *inodedep;
7271 struct allocdirect *adp;
7272 struct ufsmount *ump;
7273 struct buf *bp;
7274 struct fs *fs;
7275 ufs2_daddr_t extblocks, datablocks;
7276 struct mount *mp;
7277 int i, delay, error;
7278 ufs_lbn_t tmpval;
7279 ufs_lbn_t lbn;
7280
7281 ump = ITOUMP(ip);
7282 mp = UFSTOVFS(ump);
7283 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
7284 ("softdep_setup_freeblocks called on non-softdep filesystem"));
7285 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
7286 ip->i_number, length);
7287 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
7288 fs = ump->um_fs;
7289 if ((error = bread(ump->um_devvp,
7290 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
7291 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
7292 if (!ffs_fsfail_cleanup(ump, error))
7293 softdep_error("softdep_setup_freeblocks", error);
7294 return;
7295 }
7296 freeblks = newfreeblks(mp, ip);
7297 extblocks = 0;
7298 datablocks = 0;
7299 if (fs->fs_magic == FS_UFS2_MAGIC)
7300 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
7301 if ((flags & IO_NORMAL) != 0) {
7302 for (i = 0; i < UFS_NDADDR; i++)
7303 setup_freedirect(freeblks, ip, i, 0);
7304 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
7305 i < UFS_NIADDR;
7306 i++, lbn += tmpval, tmpval *= NINDIR(fs))
7307 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
7308 ip->i_size = 0;
7309 DIP_SET(ip, i_size, 0);
7310 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7311 datablocks = DIP(ip, i_blocks) - extblocks;
7312 }
7313 if ((flags & IO_EXT) != 0) {
7314 for (i = 0; i < UFS_NXADDR; i++)
7315 setup_freeext(freeblks, ip, i, 0);
7316 ip->i_din2->di_extsize = 0;
7317 datablocks += extblocks;
7318 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7319 }
7320 #ifdef QUOTA
7321 /* Reference the quotas in case the block count is wrong in the end. */
7322 quotaref(ITOV(ip), freeblks->fb_quota);
7323 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
7324 #endif
7325 freeblks->fb_chkcnt = -datablocks;
7326 UFS_LOCK(ump);
7327 fs->fs_pendingblocks += datablocks;
7328 UFS_UNLOCK(ump);
7329 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7330 /*
7331 * Push the zero'ed inode to its disk buffer so that we are free
7332 * to delete its dependencies below. Once the dependencies are gone
7333 * the buffer can be safely released.
7334 */
7335 if (ump->um_fstype == UFS1) {
7336 dp1 = ((struct ufs1_dinode *)bp->b_data +
7337 ino_to_fsbo(fs, ip->i_number));
7338 ip->i_din1->di_freelink = dp1->di_freelink;
7339 *dp1 = *ip->i_din1;
7340 } else {
7341 dp2 = ((struct ufs2_dinode *)bp->b_data +
7342 ino_to_fsbo(fs, ip->i_number));
7343 ip->i_din2->di_freelink = dp2->di_freelink;
7344 ffs_update_dinode_ckhash(fs, ip->i_din2);
7345 *dp2 = *ip->i_din2;
7346 }
7347 /*
7348 * Find and eliminate any inode dependencies.
7349 */
7350 ACQUIRE_LOCK(ump);
7351 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7352 if ((inodedep->id_state & IOSTARTED) != 0)
7353 panic("softdep_setup_freeblocks: inode busy");
7354 /*
7355 * Add the freeblks structure to the list of operations that
7356 * must await the zero'ed inode being written to disk. If we
7357 * still have a bitmap dependency (delay == 0), then the inode
7358 * has never been written to disk, so we can process the
7359 * freeblks below once we have deleted the dependencies.
7360 */
7361 delay = (inodedep->id_state & DEPCOMPLETE);
7362 if (delay)
7363 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7364 else
7365 freeblks->fb_state |= COMPLETE;
7366 /*
7367 * Because the file length has been truncated to zero, any
7368 * pending block allocation dependency structures associated
7369 * with this inode are obsolete and can simply be de-allocated.
7370 * We must first merge the two dependency lists to get rid of
7371 * any duplicate freefrag structures, then purge the merged list.
7372 * If we still have a bitmap dependency, then the inode has never
7373 * been written to disk, so we can free any fragments without delay.
7374 */
7375 if (flags & IO_NORMAL) {
7376 merge_inode_lists(&inodedep->id_newinoupdt,
7377 &inodedep->id_inoupdt);
7378 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7379 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7380 freeblks);
7381 }
7382 if (flags & IO_EXT) {
7383 merge_inode_lists(&inodedep->id_newextupdt,
7384 &inodedep->id_extupdt);
7385 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7386 cancel_allocdirect(&inodedep->id_extupdt, adp,
7387 freeblks);
7388 }
7389 FREE_LOCK(ump);
7390 bdwrite(bp);
7391 trunc_dependencies(ip, freeblks, -1, 0, flags);
7392 ACQUIRE_LOCK(ump);
7393 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7394 (void) free_inodedep(inodedep);
7395 freeblks->fb_state |= DEPCOMPLETE;
7396 /*
7397 * If the inode with zeroed block pointers is now on disk
7398 * we can start freeing blocks.
7399 */
7400 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7401 freeblks->fb_state |= INPROGRESS;
7402 else
7403 freeblks = NULL;
7404 FREE_LOCK(ump);
7405 if (freeblks)
7406 handle_workitem_freeblocks(freeblks, 0);
7407 trunc_pages(ip, length, extblocks, flags);
7408 }
7409
7410 /*
7411 * Eliminate pages from the page cache that back parts of this inode and
7412 * adjust the vnode pager's idea of our size. This prevents stale data
7413 * from hanging around in the page cache.
7414 */
7415 static void
trunc_pages(ip,length,extblocks,flags)7416 trunc_pages(ip, length, extblocks, flags)
7417 struct inode *ip;
7418 off_t length;
7419 ufs2_daddr_t extblocks;
7420 int flags;
7421 {
7422 struct vnode *vp;
7423 struct fs *fs;
7424 ufs_lbn_t lbn;
7425 off_t end, extend;
7426
7427 vp = ITOV(ip);
7428 fs = ITOFS(ip);
7429 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7430 if ((flags & IO_EXT) != 0)
7431 vn_pages_remove(vp, extend, 0);
7432 if ((flags & IO_NORMAL) == 0)
7433 return;
7434 BO_LOCK(&vp->v_bufobj);
7435 drain_output(vp);
7436 BO_UNLOCK(&vp->v_bufobj);
7437 /*
7438 * The vnode pager eliminates file pages we eliminate indirects
7439 * below.
7440 */
7441 vnode_pager_setsize(vp, length);
7442 /*
7443 * Calculate the end based on the last indirect we want to keep. If
7444 * the block extends into indirects we can just use the negative of
7445 * its lbn. Doubles and triples exist at lower numbers so we must
7446 * be careful not to remove those, if they exist. double and triple
7447 * indirect lbns do not overlap with others so it is not important
7448 * to verify how many levels are required.
7449 */
7450 lbn = lblkno(fs, length);
7451 if (lbn >= UFS_NDADDR) {
7452 /* Calculate the virtual lbn of the triple indirect. */
7453 lbn = -lbn - (UFS_NIADDR - 1);
7454 end = OFF_TO_IDX(lblktosize(fs, lbn));
7455 } else
7456 end = extend;
7457 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7458 }
7459
7460 /*
7461 * See if the buf bp is in the range eliminated by truncation.
7462 */
7463 static int
trunc_check_buf(bp,blkoffp,lastlbn,lastoff,flags)7464 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7465 struct buf *bp;
7466 int *blkoffp;
7467 ufs_lbn_t lastlbn;
7468 int lastoff;
7469 int flags;
7470 {
7471 ufs_lbn_t lbn;
7472
7473 *blkoffp = 0;
7474 /* Only match ext/normal blocks as appropriate. */
7475 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7476 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7477 return (0);
7478 /* ALTDATA is always a full truncation. */
7479 if ((bp->b_xflags & BX_ALTDATA) != 0)
7480 return (1);
7481 /* -1 is full truncation. */
7482 if (lastlbn == -1)
7483 return (1);
7484 /*
7485 * If this is a partial truncate we only want those
7486 * blocks and indirect blocks that cover the range
7487 * we're after.
7488 */
7489 lbn = bp->b_lblkno;
7490 if (lbn < 0)
7491 lbn = -(lbn + lbn_level(lbn));
7492 if (lbn < lastlbn)
7493 return (0);
7494 /* Here we only truncate lblkno if it's partial. */
7495 if (lbn == lastlbn) {
7496 if (lastoff == 0)
7497 return (0);
7498 *blkoffp = lastoff;
7499 }
7500 return (1);
7501 }
7502
7503 /*
7504 * Eliminate any dependencies that exist in memory beyond lblkno:off
7505 */
7506 static void
trunc_dependencies(ip,freeblks,lastlbn,lastoff,flags)7507 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7508 struct inode *ip;
7509 struct freeblks *freeblks;
7510 ufs_lbn_t lastlbn;
7511 int lastoff;
7512 int flags;
7513 {
7514 struct bufobj *bo;
7515 struct vnode *vp;
7516 struct buf *bp;
7517 int blkoff;
7518
7519 /*
7520 * We must wait for any I/O in progress to finish so that
7521 * all potential buffers on the dirty list will be visible.
7522 * Once they are all there, walk the list and get rid of
7523 * any dependencies.
7524 */
7525 vp = ITOV(ip);
7526 bo = &vp->v_bufobj;
7527 BO_LOCK(bo);
7528 drain_output(vp);
7529 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7530 bp->b_vflags &= ~BV_SCANNED;
7531 restart:
7532 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7533 if (bp->b_vflags & BV_SCANNED)
7534 continue;
7535 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7536 bp->b_vflags |= BV_SCANNED;
7537 continue;
7538 }
7539 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7540 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7541 goto restart;
7542 BO_UNLOCK(bo);
7543 if (deallocate_dependencies(bp, freeblks, blkoff))
7544 bqrelse(bp);
7545 else
7546 brelse(bp);
7547 BO_LOCK(bo);
7548 goto restart;
7549 }
7550 /*
7551 * Now do the work of vtruncbuf while also matching indirect blocks.
7552 */
7553 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7554 bp->b_vflags &= ~BV_SCANNED;
7555 cleanrestart:
7556 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7557 if (bp->b_vflags & BV_SCANNED)
7558 continue;
7559 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7560 bp->b_vflags |= BV_SCANNED;
7561 continue;
7562 }
7563 if (BUF_LOCK(bp,
7564 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7565 BO_LOCKPTR(bo)) == ENOLCK) {
7566 BO_LOCK(bo);
7567 goto cleanrestart;
7568 }
7569 BO_LOCK(bo);
7570 bp->b_vflags |= BV_SCANNED;
7571 BO_UNLOCK(bo);
7572 bremfree(bp);
7573 if (blkoff != 0) {
7574 allocbuf(bp, blkoff);
7575 bqrelse(bp);
7576 } else {
7577 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7578 brelse(bp);
7579 }
7580 BO_LOCK(bo);
7581 goto cleanrestart;
7582 }
7583 drain_output(vp);
7584 BO_UNLOCK(bo);
7585 }
7586
7587 static int
cancel_pagedep(pagedep,freeblks,blkoff)7588 cancel_pagedep(pagedep, freeblks, blkoff)
7589 struct pagedep *pagedep;
7590 struct freeblks *freeblks;
7591 int blkoff;
7592 {
7593 struct jremref *jremref;
7594 struct jmvref *jmvref;
7595 struct dirrem *dirrem, *tmp;
7596 int i;
7597
7598 /*
7599 * Copy any directory remove dependencies to the list
7600 * to be processed after the freeblks proceeds. If
7601 * directory entry never made it to disk they
7602 * can be dumped directly onto the work list.
7603 */
7604 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7605 /* Skip this directory removal if it is intended to remain. */
7606 if (dirrem->dm_offset < blkoff)
7607 continue;
7608 /*
7609 * If there are any dirrems we wait for the journal write
7610 * to complete and then restart the buf scan as the lock
7611 * has been dropped.
7612 */
7613 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7614 jwait(&jremref->jr_list, MNT_WAIT);
7615 return (ERESTART);
7616 }
7617 LIST_REMOVE(dirrem, dm_next);
7618 dirrem->dm_dirinum = pagedep->pd_ino;
7619 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7620 }
7621 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7622 jwait(&jmvref->jm_list, MNT_WAIT);
7623 return (ERESTART);
7624 }
7625 /*
7626 * When we're partially truncating a pagedep we just want to flush
7627 * journal entries and return. There can not be any adds in the
7628 * truncated portion of the directory and newblk must remain if
7629 * part of the block remains.
7630 */
7631 if (blkoff != 0) {
7632 struct diradd *dap;
7633
7634 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7635 if (dap->da_offset > blkoff)
7636 panic("cancel_pagedep: diradd %p off %d > %d",
7637 dap, dap->da_offset, blkoff);
7638 for (i = 0; i < DAHASHSZ; i++)
7639 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7640 if (dap->da_offset > blkoff)
7641 panic("cancel_pagedep: diradd %p off %d > %d",
7642 dap, dap->da_offset, blkoff);
7643 return (0);
7644 }
7645 /*
7646 * There should be no directory add dependencies present
7647 * as the directory could not be truncated until all
7648 * children were removed.
7649 */
7650 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7651 ("deallocate_dependencies: pendinghd != NULL"));
7652 for (i = 0; i < DAHASHSZ; i++)
7653 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7654 ("deallocate_dependencies: diraddhd != NULL"));
7655 if ((pagedep->pd_state & NEWBLOCK) != 0)
7656 free_newdirblk(pagedep->pd_newdirblk);
7657 if (free_pagedep(pagedep) == 0)
7658 panic("Failed to free pagedep %p", pagedep);
7659 return (0);
7660 }
7661
7662 /*
7663 * Reclaim any dependency structures from a buffer that is about to
7664 * be reallocated to a new vnode. The buffer must be locked, thus,
7665 * no I/O completion operations can occur while we are manipulating
7666 * its associated dependencies. The mutex is held so that other I/O's
7667 * associated with related dependencies do not occur.
7668 */
7669 static int
deallocate_dependencies(bp,freeblks,off)7670 deallocate_dependencies(bp, freeblks, off)
7671 struct buf *bp;
7672 struct freeblks *freeblks;
7673 int off;
7674 {
7675 struct indirdep *indirdep;
7676 struct pagedep *pagedep;
7677 struct worklist *wk, *wkn;
7678 struct ufsmount *ump;
7679
7680 ump = softdep_bp_to_mp(bp);
7681 if (ump == NULL)
7682 goto done;
7683 ACQUIRE_LOCK(ump);
7684 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7685 switch (wk->wk_type) {
7686 case D_INDIRDEP:
7687 indirdep = WK_INDIRDEP(wk);
7688 if (bp->b_lblkno >= 0 ||
7689 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7690 panic("deallocate_dependencies: not indir");
7691 cancel_indirdep(indirdep, bp, freeblks);
7692 continue;
7693
7694 case D_PAGEDEP:
7695 pagedep = WK_PAGEDEP(wk);
7696 if (cancel_pagedep(pagedep, freeblks, off)) {
7697 FREE_LOCK(ump);
7698 return (ERESTART);
7699 }
7700 continue;
7701
7702 case D_ALLOCINDIR:
7703 /*
7704 * Simply remove the allocindir, we'll find it via
7705 * the indirdep where we can clear pointers if
7706 * needed.
7707 */
7708 WORKLIST_REMOVE(wk);
7709 continue;
7710
7711 case D_FREEWORK:
7712 /*
7713 * A truncation is waiting for the zero'd pointers
7714 * to be written. It can be freed when the freeblks
7715 * is journaled.
7716 */
7717 WORKLIST_REMOVE(wk);
7718 wk->wk_state |= ONDEPLIST;
7719 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7720 break;
7721
7722 case D_ALLOCDIRECT:
7723 if (off != 0)
7724 continue;
7725 /* FALLTHROUGH */
7726 default:
7727 panic("deallocate_dependencies: Unexpected type %s",
7728 TYPENAME(wk->wk_type));
7729 /* NOTREACHED */
7730 }
7731 }
7732 FREE_LOCK(ump);
7733 done:
7734 /*
7735 * Don't throw away this buf, we were partially truncating and
7736 * some deps may always remain.
7737 */
7738 if (off) {
7739 allocbuf(bp, off);
7740 bp->b_vflags |= BV_SCANNED;
7741 return (EBUSY);
7742 }
7743 bp->b_flags |= B_INVAL | B_NOCACHE;
7744
7745 return (0);
7746 }
7747
7748 /*
7749 * An allocdirect is being canceled due to a truncate. We must make sure
7750 * the journal entry is released in concert with the blkfree that releases
7751 * the storage. Completed journal entries must not be released until the
7752 * space is no longer pointed to by the inode or in the bitmap.
7753 */
7754 static void
cancel_allocdirect(adphead,adp,freeblks)7755 cancel_allocdirect(adphead, adp, freeblks)
7756 struct allocdirectlst *adphead;
7757 struct allocdirect *adp;
7758 struct freeblks *freeblks;
7759 {
7760 struct freework *freework;
7761 struct newblk *newblk;
7762 struct worklist *wk;
7763
7764 TAILQ_REMOVE(adphead, adp, ad_next);
7765 newblk = (struct newblk *)adp;
7766 freework = NULL;
7767 /*
7768 * Find the correct freework structure.
7769 */
7770 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7771 if (wk->wk_type != D_FREEWORK)
7772 continue;
7773 freework = WK_FREEWORK(wk);
7774 if (freework->fw_blkno == newblk->nb_newblkno)
7775 break;
7776 }
7777 if (freework == NULL)
7778 panic("cancel_allocdirect: Freework not found");
7779 /*
7780 * If a newblk exists at all we still have the journal entry that
7781 * initiated the allocation so we do not need to journal the free.
7782 */
7783 cancel_jfreeblk(freeblks, freework->fw_blkno);
7784 /*
7785 * If the journal hasn't been written the jnewblk must be passed
7786 * to the call to ffs_blkfree that reclaims the space. We accomplish
7787 * this by linking the journal dependency into the freework to be
7788 * freed when freework_freeblock() is called. If the journal has
7789 * been written we can simply reclaim the journal space when the
7790 * freeblks work is complete.
7791 */
7792 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7793 &freeblks->fb_jwork);
7794 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7795 }
7796
7797 /*
7798 * Cancel a new block allocation. May be an indirect or direct block. We
7799 * remove it from various lists and return any journal record that needs to
7800 * be resolved by the caller.
7801 *
7802 * A special consideration is made for indirects which were never pointed
7803 * at on disk and will never be found once this block is released.
7804 */
7805 static struct jnewblk *
cancel_newblk(newblk,wk,wkhd)7806 cancel_newblk(newblk, wk, wkhd)
7807 struct newblk *newblk;
7808 struct worklist *wk;
7809 struct workhead *wkhd;
7810 {
7811 struct jnewblk *jnewblk;
7812
7813 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7814
7815 newblk->nb_state |= GOINGAWAY;
7816 /*
7817 * Previously we traversed the completedhd on each indirdep
7818 * attached to this newblk to cancel them and gather journal
7819 * work. Since we need only the oldest journal segment and
7820 * the lowest point on the tree will always have the oldest
7821 * journal segment we are free to release the segments
7822 * of any subordinates and may leave the indirdep list to
7823 * indirdep_complete() when this newblk is freed.
7824 */
7825 if (newblk->nb_state & ONDEPLIST) {
7826 newblk->nb_state &= ~ONDEPLIST;
7827 LIST_REMOVE(newblk, nb_deps);
7828 }
7829 if (newblk->nb_state & ONWORKLIST)
7830 WORKLIST_REMOVE(&newblk->nb_list);
7831 /*
7832 * If the journal entry hasn't been written we save a pointer to
7833 * the dependency that frees it until it is written or the
7834 * superseding operation completes.
7835 */
7836 jnewblk = newblk->nb_jnewblk;
7837 if (jnewblk != NULL && wk != NULL) {
7838 newblk->nb_jnewblk = NULL;
7839 jnewblk->jn_dep = wk;
7840 }
7841 if (!LIST_EMPTY(&newblk->nb_jwork))
7842 jwork_move(wkhd, &newblk->nb_jwork);
7843 /*
7844 * When truncating we must free the newdirblk early to remove
7845 * the pagedep from the hash before returning.
7846 */
7847 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7848 free_newdirblk(WK_NEWDIRBLK(wk));
7849 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7850 panic("cancel_newblk: extra newdirblk");
7851
7852 return (jnewblk);
7853 }
7854
7855 /*
7856 * Schedule the freefrag associated with a newblk to be released once
7857 * the pointers are written and the previous block is no longer needed.
7858 */
7859 static void
newblk_freefrag(newblk)7860 newblk_freefrag(newblk)
7861 struct newblk *newblk;
7862 {
7863 struct freefrag *freefrag;
7864
7865 if (newblk->nb_freefrag == NULL)
7866 return;
7867 freefrag = newblk->nb_freefrag;
7868 newblk->nb_freefrag = NULL;
7869 freefrag->ff_state |= COMPLETE;
7870 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7871 add_to_worklist(&freefrag->ff_list, 0);
7872 }
7873
7874 /*
7875 * Free a newblk. Generate a new freefrag work request if appropriate.
7876 * This must be called after the inode pointer and any direct block pointers
7877 * are valid or fully removed via truncate or frag extension.
7878 */
7879 static void
free_newblk(newblk)7880 free_newblk(newblk)
7881 struct newblk *newblk;
7882 {
7883 struct indirdep *indirdep;
7884 struct worklist *wk;
7885
7886 KASSERT(newblk->nb_jnewblk == NULL,
7887 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7888 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7889 ("free_newblk: unclaimed newblk"));
7890 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7891 newblk_freefrag(newblk);
7892 if (newblk->nb_state & ONDEPLIST)
7893 LIST_REMOVE(newblk, nb_deps);
7894 if (newblk->nb_state & ONWORKLIST)
7895 WORKLIST_REMOVE(&newblk->nb_list);
7896 LIST_REMOVE(newblk, nb_hash);
7897 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7898 free_newdirblk(WK_NEWDIRBLK(wk));
7899 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7900 panic("free_newblk: extra newdirblk");
7901 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7902 indirdep_complete(indirdep);
7903 handle_jwork(&newblk->nb_jwork);
7904 WORKITEM_FREE(newblk, D_NEWBLK);
7905 }
7906
7907 /*
7908 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7909 */
7910 static void
free_newdirblk(newdirblk)7911 free_newdirblk(newdirblk)
7912 struct newdirblk *newdirblk;
7913 {
7914 struct pagedep *pagedep;
7915 struct diradd *dap;
7916 struct worklist *wk;
7917
7918 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7919 WORKLIST_REMOVE(&newdirblk->db_list);
7920 /*
7921 * If the pagedep is still linked onto the directory buffer
7922 * dependency chain, then some of the entries on the
7923 * pd_pendinghd list may not be committed to disk yet. In
7924 * this case, we will simply clear the NEWBLOCK flag and
7925 * let the pd_pendinghd list be processed when the pagedep
7926 * is next written. If the pagedep is no longer on the buffer
7927 * dependency chain, then all the entries on the pd_pending
7928 * list are committed to disk and we can free them here.
7929 */
7930 pagedep = newdirblk->db_pagedep;
7931 pagedep->pd_state &= ~NEWBLOCK;
7932 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7933 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7934 free_diradd(dap, NULL);
7935 /*
7936 * If no dependencies remain, the pagedep will be freed.
7937 */
7938 free_pagedep(pagedep);
7939 }
7940 /* Should only ever be one item in the list. */
7941 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7942 WORKLIST_REMOVE(wk);
7943 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7944 }
7945 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7946 }
7947
7948 /*
7949 * Prepare an inode to be freed. The actual free operation is not
7950 * done until the zero'ed inode has been written to disk.
7951 */
7952 void
softdep_freefile(pvp,ino,mode)7953 softdep_freefile(pvp, ino, mode)
7954 struct vnode *pvp;
7955 ino_t ino;
7956 int mode;
7957 {
7958 struct inode *ip = VTOI(pvp);
7959 struct inodedep *inodedep;
7960 struct freefile *freefile;
7961 struct freeblks *freeblks;
7962 struct ufsmount *ump;
7963
7964 ump = ITOUMP(ip);
7965 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7966 ("softdep_freefile called on non-softdep filesystem"));
7967 /*
7968 * This sets up the inode de-allocation dependency.
7969 */
7970 freefile = malloc(sizeof(struct freefile),
7971 M_FREEFILE, M_SOFTDEP_FLAGS);
7972 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7973 freefile->fx_mode = mode;
7974 freefile->fx_oldinum = ino;
7975 freefile->fx_devvp = ump->um_devvp;
7976 LIST_INIT(&freefile->fx_jwork);
7977 UFS_LOCK(ump);
7978 ump->um_fs->fs_pendinginodes += 1;
7979 UFS_UNLOCK(ump);
7980
7981 /*
7982 * If the inodedep does not exist, then the zero'ed inode has
7983 * been written to disk. If the allocated inode has never been
7984 * written to disk, then the on-disk inode is zero'ed. In either
7985 * case we can free the file immediately. If the journal was
7986 * canceled before being written the inode will never make it to
7987 * disk and we must send the canceled journal entrys to
7988 * ffs_freefile() to be cleared in conjunction with the bitmap.
7989 * Any blocks waiting on the inode to write can be safely freed
7990 * here as it will never been written.
7991 */
7992 ACQUIRE_LOCK(ump);
7993 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7994 if (inodedep) {
7995 /*
7996 * Clear out freeblks that no longer need to reference
7997 * this inode.
7998 */
7999 while ((freeblks =
8000 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
8001 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
8002 fb_next);
8003 freeblks->fb_state &= ~ONDEPLIST;
8004 }
8005 /*
8006 * Remove this inode from the unlinked list.
8007 */
8008 if (inodedep->id_state & UNLINKED) {
8009 /*
8010 * Save the journal work to be freed with the bitmap
8011 * before we clear UNLINKED. Otherwise it can be lost
8012 * if the inode block is written.
8013 */
8014 handle_bufwait(inodedep, &freefile->fx_jwork);
8015 clear_unlinked_inodedep(inodedep);
8016 /*
8017 * Re-acquire inodedep as we've dropped the
8018 * per-filesystem lock in clear_unlinked_inodedep().
8019 */
8020 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
8021 }
8022 }
8023 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
8024 FREE_LOCK(ump);
8025 handle_workitem_freefile(freefile);
8026 return;
8027 }
8028 if ((inodedep->id_state & DEPCOMPLETE) == 0)
8029 inodedep->id_state |= GOINGAWAY;
8030 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
8031 FREE_LOCK(ump);
8032 if (ip->i_number == ino)
8033 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
8034 }
8035
8036 /*
8037 * Check to see if an inode has never been written to disk. If
8038 * so free the inodedep and return success, otherwise return failure.
8039 *
8040 * If we still have a bitmap dependency, then the inode has never
8041 * been written to disk. Drop the dependency as it is no longer
8042 * necessary since the inode is being deallocated. We set the
8043 * ALLCOMPLETE flags since the bitmap now properly shows that the
8044 * inode is not allocated. Even if the inode is actively being
8045 * written, it has been rolled back to its zero'ed state, so we
8046 * are ensured that a zero inode is what is on the disk. For short
8047 * lived files, this change will usually result in removing all the
8048 * dependencies from the inode so that it can be freed immediately.
8049 */
8050 static int
check_inode_unwritten(inodedep)8051 check_inode_unwritten(inodedep)
8052 struct inodedep *inodedep;
8053 {
8054
8055 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8056
8057 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
8058 !LIST_EMPTY(&inodedep->id_dirremhd) ||
8059 !LIST_EMPTY(&inodedep->id_pendinghd) ||
8060 !LIST_EMPTY(&inodedep->id_bufwait) ||
8061 !LIST_EMPTY(&inodedep->id_inowait) ||
8062 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
8063 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
8064 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
8065 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
8066 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
8067 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
8068 inodedep->id_mkdiradd != NULL ||
8069 inodedep->id_nlinkdelta != 0)
8070 return (0);
8071 /*
8072 * Another process might be in initiate_write_inodeblock_ufs[12]
8073 * trying to allocate memory without holding "Softdep Lock".
8074 */
8075 if ((inodedep->id_state & IOSTARTED) != 0 &&
8076 inodedep->id_savedino1 == NULL)
8077 return (0);
8078
8079 if (inodedep->id_state & ONDEPLIST)
8080 LIST_REMOVE(inodedep, id_deps);
8081 inodedep->id_state &= ~ONDEPLIST;
8082 inodedep->id_state |= ALLCOMPLETE;
8083 inodedep->id_bmsafemap = NULL;
8084 if (inodedep->id_state & ONWORKLIST)
8085 WORKLIST_REMOVE(&inodedep->id_list);
8086 if (inodedep->id_savedino1 != NULL) {
8087 free(inodedep->id_savedino1, M_SAVEDINO);
8088 inodedep->id_savedino1 = NULL;
8089 }
8090 if (free_inodedep(inodedep) == 0)
8091 panic("check_inode_unwritten: busy inode");
8092 return (1);
8093 }
8094
8095 static int
check_inodedep_free(inodedep)8096 check_inodedep_free(inodedep)
8097 struct inodedep *inodedep;
8098 {
8099
8100 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8101 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
8102 !LIST_EMPTY(&inodedep->id_dirremhd) ||
8103 !LIST_EMPTY(&inodedep->id_pendinghd) ||
8104 !LIST_EMPTY(&inodedep->id_bufwait) ||
8105 !LIST_EMPTY(&inodedep->id_inowait) ||
8106 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
8107 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
8108 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
8109 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
8110 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
8111 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
8112 inodedep->id_mkdiradd != NULL ||
8113 inodedep->id_nlinkdelta != 0 ||
8114 inodedep->id_savedino1 != NULL)
8115 return (0);
8116 return (1);
8117 }
8118
8119 /*
8120 * Try to free an inodedep structure. Return 1 if it could be freed.
8121 */
8122 static int
free_inodedep(inodedep)8123 free_inodedep(inodedep)
8124 struct inodedep *inodedep;
8125 {
8126
8127 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8128 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
8129 !check_inodedep_free(inodedep))
8130 return (0);
8131 if (inodedep->id_state & ONDEPLIST)
8132 LIST_REMOVE(inodedep, id_deps);
8133 LIST_REMOVE(inodedep, id_hash);
8134 WORKITEM_FREE(inodedep, D_INODEDEP);
8135 return (1);
8136 }
8137
8138 /*
8139 * Free the block referenced by a freework structure. The parent freeblks
8140 * structure is released and completed when the final cg bitmap reaches
8141 * the disk. This routine may be freeing a jnewblk which never made it to
8142 * disk in which case we do not have to wait as the operation is undone
8143 * in memory immediately.
8144 */
8145 static void
freework_freeblock(freework,key)8146 freework_freeblock(freework, key)
8147 struct freework *freework;
8148 u_long key;
8149 {
8150 struct freeblks *freeblks;
8151 struct jnewblk *jnewblk;
8152 struct ufsmount *ump;
8153 struct workhead wkhd;
8154 struct fs *fs;
8155 int bsize;
8156 int needj;
8157
8158 ump = VFSTOUFS(freework->fw_list.wk_mp);
8159 LOCK_OWNED(ump);
8160 /*
8161 * Handle partial truncate separately.
8162 */
8163 if (freework->fw_indir) {
8164 complete_trunc_indir(freework);
8165 return;
8166 }
8167 freeblks = freework->fw_freeblks;
8168 fs = ump->um_fs;
8169 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
8170 bsize = lfragtosize(fs, freework->fw_frags);
8171 LIST_INIT(&wkhd);
8172 /*
8173 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
8174 * on the indirblk hashtable and prevents premature freeing.
8175 */
8176 freework->fw_state |= DEPCOMPLETE;
8177 /*
8178 * SUJ needs to wait for the segment referencing freed indirect
8179 * blocks to expire so that we know the checker will not confuse
8180 * a re-allocated indirect block with its old contents.
8181 */
8182 if (needj && freework->fw_lbn <= -UFS_NDADDR)
8183 indirblk_insert(freework);
8184 /*
8185 * If we are canceling an existing jnewblk pass it to the free
8186 * routine, otherwise pass the freeblk which will ultimately
8187 * release the freeblks. If we're not journaling, we can just
8188 * free the freeblks immediately.
8189 */
8190 jnewblk = freework->fw_jnewblk;
8191 if (jnewblk != NULL) {
8192 cancel_jnewblk(jnewblk, &wkhd);
8193 needj = 0;
8194 } else if (needj) {
8195 freework->fw_state |= DELAYEDFREE;
8196 freeblks->fb_cgwait++;
8197 WORKLIST_INSERT(&wkhd, &freework->fw_list);
8198 }
8199 FREE_LOCK(ump);
8200 freeblks_free(ump, freeblks, btodb(bsize));
8201 CTR4(KTR_SUJ,
8202 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
8203 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
8204 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
8205 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
8206 ACQUIRE_LOCK(ump);
8207 /*
8208 * The jnewblk will be discarded and the bits in the map never
8209 * made it to disk. We can immediately free the freeblk.
8210 */
8211 if (needj == 0)
8212 handle_written_freework(freework);
8213 }
8214
8215 /*
8216 * We enqueue freework items that need processing back on the freeblks and
8217 * add the freeblks to the worklist. This makes it easier to find all work
8218 * required to flush a truncation in process_truncates().
8219 */
8220 static void
freework_enqueue(freework)8221 freework_enqueue(freework)
8222 struct freework *freework;
8223 {
8224 struct freeblks *freeblks;
8225
8226 freeblks = freework->fw_freeblks;
8227 if ((freework->fw_state & INPROGRESS) == 0)
8228 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
8229 if ((freeblks->fb_state &
8230 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
8231 LIST_EMPTY(&freeblks->fb_jblkdephd))
8232 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8233 }
8234
8235 /*
8236 * Start, continue, or finish the process of freeing an indirect block tree.
8237 * The free operation may be paused at any point with fw_off containing the
8238 * offset to restart from. This enables us to implement some flow control
8239 * for large truncates which may fan out and generate a huge number of
8240 * dependencies.
8241 */
8242 static void
handle_workitem_indirblk(freework)8243 handle_workitem_indirblk(freework)
8244 struct freework *freework;
8245 {
8246 struct freeblks *freeblks;
8247 struct ufsmount *ump;
8248 struct fs *fs;
8249
8250 freeblks = freework->fw_freeblks;
8251 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8252 fs = ump->um_fs;
8253 if (freework->fw_state & DEPCOMPLETE) {
8254 handle_written_freework(freework);
8255 return;
8256 }
8257 if (freework->fw_off == NINDIR(fs)) {
8258 freework_freeblock(freework, SINGLETON_KEY);
8259 return;
8260 }
8261 freework->fw_state |= INPROGRESS;
8262 FREE_LOCK(ump);
8263 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
8264 freework->fw_lbn);
8265 ACQUIRE_LOCK(ump);
8266 }
8267
8268 /*
8269 * Called when a freework structure attached to a cg buf is written. The
8270 * ref on either the parent or the freeblks structure is released and
8271 * the freeblks is added back to the worklist if there is more work to do.
8272 */
8273 static void
handle_written_freework(freework)8274 handle_written_freework(freework)
8275 struct freework *freework;
8276 {
8277 struct freeblks *freeblks;
8278 struct freework *parent;
8279
8280 freeblks = freework->fw_freeblks;
8281 parent = freework->fw_parent;
8282 if (freework->fw_state & DELAYEDFREE)
8283 freeblks->fb_cgwait--;
8284 freework->fw_state |= COMPLETE;
8285 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
8286 WORKITEM_FREE(freework, D_FREEWORK);
8287 if (parent) {
8288 if (--parent->fw_ref == 0)
8289 freework_enqueue(parent);
8290 return;
8291 }
8292 if (--freeblks->fb_ref != 0)
8293 return;
8294 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
8295 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
8296 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8297 }
8298
8299 /*
8300 * This workitem routine performs the block de-allocation.
8301 * The workitem is added to the pending list after the updated
8302 * inode block has been written to disk. As mentioned above,
8303 * checks regarding the number of blocks de-allocated (compared
8304 * to the number of blocks allocated for the file) are also
8305 * performed in this function.
8306 */
8307 static int
handle_workitem_freeblocks(freeblks,flags)8308 handle_workitem_freeblocks(freeblks, flags)
8309 struct freeblks *freeblks;
8310 int flags;
8311 {
8312 struct freework *freework;
8313 struct newblk *newblk;
8314 struct allocindir *aip;
8315 struct ufsmount *ump;
8316 struct worklist *wk;
8317 u_long key;
8318
8319 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
8320 ("handle_workitem_freeblocks: Journal entries not written."));
8321 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8322 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8323 ACQUIRE_LOCK(ump);
8324 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
8325 WORKLIST_REMOVE(wk);
8326 switch (wk->wk_type) {
8327 case D_DIRREM:
8328 wk->wk_state |= COMPLETE;
8329 add_to_worklist(wk, 0);
8330 continue;
8331
8332 case D_ALLOCDIRECT:
8333 free_newblk(WK_NEWBLK(wk));
8334 continue;
8335
8336 case D_ALLOCINDIR:
8337 aip = WK_ALLOCINDIR(wk);
8338 freework = NULL;
8339 if (aip->ai_state & DELAYEDFREE) {
8340 FREE_LOCK(ump);
8341 freework = newfreework(ump, freeblks, NULL,
8342 aip->ai_lbn, aip->ai_newblkno,
8343 ump->um_fs->fs_frag, 0, 0);
8344 ACQUIRE_LOCK(ump);
8345 }
8346 newblk = WK_NEWBLK(wk);
8347 if (newblk->nb_jnewblk) {
8348 freework->fw_jnewblk = newblk->nb_jnewblk;
8349 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
8350 newblk->nb_jnewblk = NULL;
8351 }
8352 free_newblk(newblk);
8353 continue;
8354
8355 case D_FREEWORK:
8356 freework = WK_FREEWORK(wk);
8357 if (freework->fw_lbn <= -UFS_NDADDR)
8358 handle_workitem_indirblk(freework);
8359 else
8360 freework_freeblock(freework, key);
8361 continue;
8362 default:
8363 panic("handle_workitem_freeblocks: Unknown type %s",
8364 TYPENAME(wk->wk_type));
8365 }
8366 }
8367 if (freeblks->fb_ref != 0) {
8368 freeblks->fb_state &= ~INPROGRESS;
8369 wake_worklist(&freeblks->fb_list);
8370 freeblks = NULL;
8371 }
8372 FREE_LOCK(ump);
8373 ffs_blkrelease_finish(ump, key);
8374 if (freeblks)
8375 return handle_complete_freeblocks(freeblks, flags);
8376 return (0);
8377 }
8378
8379 /*
8380 * Handle completion of block free via truncate. This allows fs_pending
8381 * to track the actual free block count more closely than if we only updated
8382 * it at the end. We must be careful to handle cases where the block count
8383 * on free was incorrect.
8384 */
8385 static void
freeblks_free(ump,freeblks,blocks)8386 freeblks_free(ump, freeblks, blocks)
8387 struct ufsmount *ump;
8388 struct freeblks *freeblks;
8389 int blocks;
8390 {
8391 struct fs *fs;
8392 ufs2_daddr_t remain;
8393
8394 UFS_LOCK(ump);
8395 remain = -freeblks->fb_chkcnt;
8396 freeblks->fb_chkcnt += blocks;
8397 if (remain > 0) {
8398 if (remain < blocks)
8399 blocks = remain;
8400 fs = ump->um_fs;
8401 fs->fs_pendingblocks -= blocks;
8402 }
8403 UFS_UNLOCK(ump);
8404 }
8405
8406 /*
8407 * Once all of the freework workitems are complete we can retire the
8408 * freeblocks dependency and any journal work awaiting completion. This
8409 * can not be called until all other dependencies are stable on disk.
8410 */
8411 static int
handle_complete_freeblocks(freeblks,flags)8412 handle_complete_freeblocks(freeblks, flags)
8413 struct freeblks *freeblks;
8414 int flags;
8415 {
8416 struct inodedep *inodedep;
8417 struct inode *ip;
8418 struct vnode *vp;
8419 struct fs *fs;
8420 struct ufsmount *ump;
8421 ufs2_daddr_t spare;
8422
8423 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8424 fs = ump->um_fs;
8425 flags = LK_EXCLUSIVE | flags;
8426 spare = freeblks->fb_chkcnt;
8427
8428 /*
8429 * If we did not release the expected number of blocks we may have
8430 * to adjust the inode block count here. Only do so if it wasn't
8431 * a truncation to zero and the modrev still matches.
8432 */
8433 if (spare && freeblks->fb_len != 0) {
8434 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8435 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0)
8436 return (EBUSY);
8437 ip = VTOI(vp);
8438 if (ip->i_mode == 0) {
8439 vgone(vp);
8440 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8441 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8442 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8443 /*
8444 * We must wait so this happens before the
8445 * journal is reclaimed.
8446 */
8447 ffs_update(vp, 1);
8448 }
8449 vput(vp);
8450 }
8451 if (spare < 0) {
8452 UFS_LOCK(ump);
8453 fs->fs_pendingblocks += spare;
8454 UFS_UNLOCK(ump);
8455 }
8456 #ifdef QUOTA
8457 /* Handle spare. */
8458 if (spare)
8459 quotaadj(freeblks->fb_quota, ump, -spare);
8460 quotarele(freeblks->fb_quota);
8461 #endif
8462 ACQUIRE_LOCK(ump);
8463 if (freeblks->fb_state & ONDEPLIST) {
8464 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8465 0, &inodedep);
8466 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8467 freeblks->fb_state &= ~ONDEPLIST;
8468 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8469 free_inodedep(inodedep);
8470 }
8471 /*
8472 * All of the freeblock deps must be complete prior to this call
8473 * so it's now safe to complete earlier outstanding journal entries.
8474 */
8475 handle_jwork(&freeblks->fb_jwork);
8476 WORKITEM_FREE(freeblks, D_FREEBLKS);
8477 FREE_LOCK(ump);
8478 return (0);
8479 }
8480
8481 /*
8482 * Release blocks associated with the freeblks and stored in the indirect
8483 * block dbn. If level is greater than SINGLE, the block is an indirect block
8484 * and recursive calls to indirtrunc must be used to cleanse other indirect
8485 * blocks.
8486 *
8487 * This handles partial and complete truncation of blocks. Partial is noted
8488 * with goingaway == 0. In this case the freework is completed after the
8489 * zero'd indirects are written to disk. For full truncation the freework
8490 * is completed after the block is freed.
8491 */
8492 static void
indir_trunc(freework,dbn,lbn)8493 indir_trunc(freework, dbn, lbn)
8494 struct freework *freework;
8495 ufs2_daddr_t dbn;
8496 ufs_lbn_t lbn;
8497 {
8498 struct freework *nfreework;
8499 struct workhead wkhd;
8500 struct freeblks *freeblks;
8501 struct buf *bp;
8502 struct fs *fs;
8503 struct indirdep *indirdep;
8504 struct mount *mp;
8505 struct ufsmount *ump;
8506 ufs1_daddr_t *bap1;
8507 ufs2_daddr_t nb, nnb, *bap2;
8508 ufs_lbn_t lbnadd, nlbn;
8509 u_long key;
8510 int nblocks, ufs1fmt, freedblocks;
8511 int goingaway, freedeps, needj, level, cnt, i, error;
8512
8513 freeblks = freework->fw_freeblks;
8514 mp = freeblks->fb_list.wk_mp;
8515 ump = VFSTOUFS(mp);
8516 fs = ump->um_fs;
8517 /*
8518 * Get buffer of block pointers to be freed. There are three cases:
8519 *
8520 * 1) Partial truncate caches the indirdep pointer in the freework
8521 * which provides us a back copy to the save bp which holds the
8522 * pointers we want to clear. When this completes the zero
8523 * pointers are written to the real copy.
8524 * 2) The indirect is being completely truncated, cancel_indirdep()
8525 * eliminated the real copy and placed the indirdep on the saved
8526 * copy. The indirdep and buf are discarded when this completes.
8527 * 3) The indirect was not in memory, we read a copy off of the disk
8528 * using the devvp and drop and invalidate the buffer when we're
8529 * done.
8530 */
8531 goingaway = 1;
8532 indirdep = NULL;
8533 if (freework->fw_indir != NULL) {
8534 goingaway = 0;
8535 indirdep = freework->fw_indir;
8536 bp = indirdep->ir_savebp;
8537 if (bp == NULL || bp->b_blkno != dbn)
8538 panic("indir_trunc: Bad saved buf %p blkno %jd",
8539 bp, (intmax_t)dbn);
8540 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8541 /*
8542 * The lock prevents the buf dep list from changing and
8543 * indirects on devvp should only ever have one dependency.
8544 */
8545 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8546 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8547 panic("indir_trunc: Bad indirdep %p from buf %p",
8548 indirdep, bp);
8549 } else {
8550 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn,
8551 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
8552 if (error)
8553 return;
8554 }
8555 ACQUIRE_LOCK(ump);
8556 /* Protects against a race with complete_trunc_indir(). */
8557 freework->fw_state &= ~INPROGRESS;
8558 /*
8559 * If we have an indirdep we need to enforce the truncation order
8560 * and discard it when it is complete.
8561 */
8562 if (indirdep) {
8563 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8564 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8565 /*
8566 * Add the complete truncate to the list on the
8567 * indirdep to enforce in-order processing.
8568 */
8569 if (freework->fw_indir == NULL)
8570 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8571 freework, fw_next);
8572 FREE_LOCK(ump);
8573 return;
8574 }
8575 /*
8576 * If we're goingaway, free the indirdep. Otherwise it will
8577 * linger until the write completes.
8578 */
8579 if (goingaway) {
8580 KASSERT(indirdep->ir_savebp == bp,
8581 ("indir_trunc: losing ir_savebp %p",
8582 indirdep->ir_savebp));
8583 indirdep->ir_savebp = NULL;
8584 free_indirdep(indirdep);
8585 }
8586 }
8587 FREE_LOCK(ump);
8588 /* Initialize pointers depending on block size. */
8589 if (ump->um_fstype == UFS1) {
8590 bap1 = (ufs1_daddr_t *)bp->b_data;
8591 nb = bap1[freework->fw_off];
8592 ufs1fmt = 1;
8593 bap2 = NULL;
8594 } else {
8595 bap2 = (ufs2_daddr_t *)bp->b_data;
8596 nb = bap2[freework->fw_off];
8597 ufs1fmt = 0;
8598 bap1 = NULL;
8599 }
8600 level = lbn_level(lbn);
8601 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8602 lbnadd = lbn_offset(fs, level);
8603 nblocks = btodb(fs->fs_bsize);
8604 nfreework = freework;
8605 freedeps = 0;
8606 cnt = 0;
8607 /*
8608 * Reclaim blocks. Traverses into nested indirect levels and
8609 * arranges for the current level to be freed when subordinates
8610 * are free when journaling.
8611 */
8612 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8613 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8614 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8615 fs->fs_bsize) != 0)
8616 nb = 0;
8617 if (i != NINDIR(fs) - 1) {
8618 if (ufs1fmt)
8619 nnb = bap1[i+1];
8620 else
8621 nnb = bap2[i+1];
8622 } else
8623 nnb = 0;
8624 if (nb == 0)
8625 continue;
8626 cnt++;
8627 if (level != 0) {
8628 nlbn = (lbn + 1) - (i * lbnadd);
8629 if (needj != 0) {
8630 nfreework = newfreework(ump, freeblks, freework,
8631 nlbn, nb, fs->fs_frag, 0, 0);
8632 freedeps++;
8633 }
8634 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8635 } else {
8636 struct freedep *freedep;
8637
8638 /*
8639 * Attempt to aggregate freedep dependencies for
8640 * all blocks being released to the same CG.
8641 */
8642 LIST_INIT(&wkhd);
8643 if (needj != 0 &&
8644 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8645 freedep = newfreedep(freework);
8646 WORKLIST_INSERT_UNLOCKED(&wkhd,
8647 &freedep->fd_list);
8648 freedeps++;
8649 }
8650 CTR3(KTR_SUJ,
8651 "indir_trunc: ino %jd blkno %jd size %d",
8652 freeblks->fb_inum, nb, fs->fs_bsize);
8653 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8654 fs->fs_bsize, freeblks->fb_inum,
8655 freeblks->fb_vtype, &wkhd, key);
8656 }
8657 }
8658 ffs_blkrelease_finish(ump, key);
8659 if (goingaway) {
8660 bp->b_flags |= B_INVAL | B_NOCACHE;
8661 brelse(bp);
8662 }
8663 freedblocks = 0;
8664 if (level == 0)
8665 freedblocks = (nblocks * cnt);
8666 if (needj == 0)
8667 freedblocks += nblocks;
8668 freeblks_free(ump, freeblks, freedblocks);
8669 /*
8670 * If we are journaling set up the ref counts and offset so this
8671 * indirect can be completed when its children are free.
8672 */
8673 if (needj) {
8674 ACQUIRE_LOCK(ump);
8675 freework->fw_off = i;
8676 freework->fw_ref += freedeps;
8677 freework->fw_ref -= NINDIR(fs) + 1;
8678 if (level == 0)
8679 freeblks->fb_cgwait += freedeps;
8680 if (freework->fw_ref == 0)
8681 freework_freeblock(freework, SINGLETON_KEY);
8682 FREE_LOCK(ump);
8683 return;
8684 }
8685 /*
8686 * If we're not journaling we can free the indirect now.
8687 */
8688 dbn = dbtofsb(fs, dbn);
8689 CTR3(KTR_SUJ,
8690 "indir_trunc 2: ino %jd blkno %jd size %d",
8691 freeblks->fb_inum, dbn, fs->fs_bsize);
8692 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8693 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8694 /* Non SUJ softdep does single-threaded truncations. */
8695 if (freework->fw_blkno == dbn) {
8696 freework->fw_state |= ALLCOMPLETE;
8697 ACQUIRE_LOCK(ump);
8698 handle_written_freework(freework);
8699 FREE_LOCK(ump);
8700 }
8701 return;
8702 }
8703
8704 /*
8705 * Cancel an allocindir when it is removed via truncation. When bp is not
8706 * NULL the indirect never appeared on disk and is scheduled to be freed
8707 * independently of the indir so we can more easily track journal work.
8708 */
8709 static void
cancel_allocindir(aip,bp,freeblks,trunc)8710 cancel_allocindir(aip, bp, freeblks, trunc)
8711 struct allocindir *aip;
8712 struct buf *bp;
8713 struct freeblks *freeblks;
8714 int trunc;
8715 {
8716 struct indirdep *indirdep;
8717 struct freefrag *freefrag;
8718 struct newblk *newblk;
8719
8720 newblk = (struct newblk *)aip;
8721 LIST_REMOVE(aip, ai_next);
8722 /*
8723 * We must eliminate the pointer in bp if it must be freed on its
8724 * own due to partial truncate or pending journal work.
8725 */
8726 if (bp && (trunc || newblk->nb_jnewblk)) {
8727 /*
8728 * Clear the pointer and mark the aip to be freed
8729 * directly if it never existed on disk.
8730 */
8731 aip->ai_state |= DELAYEDFREE;
8732 indirdep = aip->ai_indirdep;
8733 if (indirdep->ir_state & UFS1FMT)
8734 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8735 else
8736 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8737 }
8738 /*
8739 * When truncating the previous pointer will be freed via
8740 * savedbp. Eliminate the freefrag which would dup free.
8741 */
8742 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8743 newblk->nb_freefrag = NULL;
8744 if (freefrag->ff_jdep)
8745 cancel_jfreefrag(
8746 WK_JFREEFRAG(freefrag->ff_jdep));
8747 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8748 WORKITEM_FREE(freefrag, D_FREEFRAG);
8749 }
8750 /*
8751 * If the journal hasn't been written the jnewblk must be passed
8752 * to the call to ffs_blkfree that reclaims the space. We accomplish
8753 * this by leaving the journal dependency on the newblk to be freed
8754 * when a freework is created in handle_workitem_freeblocks().
8755 */
8756 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8757 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8758 }
8759
8760 /*
8761 * Create the mkdir dependencies for . and .. in a new directory. Link them
8762 * in to a newdirblk so any subsequent additions are tracked properly. The
8763 * caller is responsible for adding the mkdir1 dependency to the journal
8764 * and updating id_mkdiradd. This function returns with the per-filesystem
8765 * lock held.
8766 */
8767 static struct mkdir *
setup_newdir(dap,newinum,dinum,newdirbp,mkdirp)8768 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8769 struct diradd *dap;
8770 ino_t newinum;
8771 ino_t dinum;
8772 struct buf *newdirbp;
8773 struct mkdir **mkdirp;
8774 {
8775 struct newblk *newblk;
8776 struct pagedep *pagedep;
8777 struct inodedep *inodedep;
8778 struct newdirblk *newdirblk;
8779 struct mkdir *mkdir1, *mkdir2;
8780 struct worklist *wk;
8781 struct jaddref *jaddref;
8782 struct ufsmount *ump;
8783 struct mount *mp;
8784
8785 mp = dap->da_list.wk_mp;
8786 ump = VFSTOUFS(mp);
8787 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8788 M_SOFTDEP_FLAGS);
8789 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8790 LIST_INIT(&newdirblk->db_mkdir);
8791 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8792 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8793 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8794 mkdir1->md_diradd = dap;
8795 mkdir1->md_jaddref = NULL;
8796 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8797 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8798 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8799 mkdir2->md_diradd = dap;
8800 mkdir2->md_jaddref = NULL;
8801 if (MOUNTEDSUJ(mp) == 0) {
8802 mkdir1->md_state |= DEPCOMPLETE;
8803 mkdir2->md_state |= DEPCOMPLETE;
8804 }
8805 /*
8806 * Dependency on "." and ".." being written to disk.
8807 */
8808 mkdir1->md_buf = newdirbp;
8809 ACQUIRE_LOCK(VFSTOUFS(mp));
8810 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8811 /*
8812 * We must link the pagedep, allocdirect, and newdirblk for
8813 * the initial file page so the pointer to the new directory
8814 * is not written until the directory contents are live and
8815 * any subsequent additions are not marked live until the
8816 * block is reachable via the inode.
8817 */
8818 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8819 panic("setup_newdir: lost pagedep");
8820 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8821 if (wk->wk_type == D_ALLOCDIRECT)
8822 break;
8823 if (wk == NULL)
8824 panic("setup_newdir: lost allocdirect");
8825 if (pagedep->pd_state & NEWBLOCK)
8826 panic("setup_newdir: NEWBLOCK already set");
8827 newblk = WK_NEWBLK(wk);
8828 pagedep->pd_state |= NEWBLOCK;
8829 pagedep->pd_newdirblk = newdirblk;
8830 newdirblk->db_pagedep = pagedep;
8831 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8832 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8833 /*
8834 * Look up the inodedep for the parent directory so that we
8835 * can link mkdir2 into the pending dotdot jaddref or
8836 * the inode write if there is none. If the inode is
8837 * ALLCOMPLETE and no jaddref is present all dependencies have
8838 * been satisfied and mkdir2 can be freed.
8839 */
8840 inodedep_lookup(mp, dinum, 0, &inodedep);
8841 if (MOUNTEDSUJ(mp)) {
8842 if (inodedep == NULL)
8843 panic("setup_newdir: Lost parent.");
8844 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8845 inoreflst);
8846 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8847 (jaddref->ja_state & MKDIR_PARENT),
8848 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8849 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8850 mkdir2->md_jaddref = jaddref;
8851 jaddref->ja_mkdir = mkdir2;
8852 } else if (inodedep == NULL ||
8853 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8854 dap->da_state &= ~MKDIR_PARENT;
8855 WORKITEM_FREE(mkdir2, D_MKDIR);
8856 mkdir2 = NULL;
8857 } else {
8858 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8859 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8860 }
8861 *mkdirp = mkdir2;
8862
8863 return (mkdir1);
8864 }
8865
8866 /*
8867 * Directory entry addition dependencies.
8868 *
8869 * When adding a new directory entry, the inode (with its incremented link
8870 * count) must be written to disk before the directory entry's pointer to it.
8871 * Also, if the inode is newly allocated, the corresponding freemap must be
8872 * updated (on disk) before the directory entry's pointer. These requirements
8873 * are met via undo/redo on the directory entry's pointer, which consists
8874 * simply of the inode number.
8875 *
8876 * As directory entries are added and deleted, the free space within a
8877 * directory block can become fragmented. The ufs filesystem will compact
8878 * a fragmented directory block to make space for a new entry. When this
8879 * occurs, the offsets of previously added entries change. Any "diradd"
8880 * dependency structures corresponding to these entries must be updated with
8881 * the new offsets.
8882 */
8883
8884 /*
8885 * This routine is called after the in-memory inode's link
8886 * count has been incremented, but before the directory entry's
8887 * pointer to the inode has been set.
8888 */
8889 int
softdep_setup_directory_add(bp,dp,diroffset,newinum,newdirbp,isnewblk)8890 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8891 struct buf *bp; /* buffer containing directory block */
8892 struct inode *dp; /* inode for directory */
8893 off_t diroffset; /* offset of new entry in directory */
8894 ino_t newinum; /* inode referenced by new directory entry */
8895 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
8896 int isnewblk; /* entry is in a newly allocated block */
8897 {
8898 int offset; /* offset of new entry within directory block */
8899 ufs_lbn_t lbn; /* block in directory containing new entry */
8900 struct fs *fs;
8901 struct diradd *dap;
8902 struct newblk *newblk;
8903 struct pagedep *pagedep;
8904 struct inodedep *inodedep;
8905 struct newdirblk *newdirblk;
8906 struct mkdir *mkdir1, *mkdir2;
8907 struct jaddref *jaddref;
8908 struct ufsmount *ump;
8909 struct mount *mp;
8910 int isindir;
8911
8912 mp = ITOVFS(dp);
8913 ump = VFSTOUFS(mp);
8914 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8915 ("softdep_setup_directory_add called on non-softdep filesystem"));
8916 /*
8917 * Whiteouts have no dependencies.
8918 */
8919 if (newinum == UFS_WINO) {
8920 if (newdirbp != NULL)
8921 bdwrite(newdirbp);
8922 return (0);
8923 }
8924 jaddref = NULL;
8925 mkdir1 = mkdir2 = NULL;
8926 fs = ump->um_fs;
8927 lbn = lblkno(fs, diroffset);
8928 offset = blkoff(fs, diroffset);
8929 dap = malloc(sizeof(struct diradd), M_DIRADD,
8930 M_SOFTDEP_FLAGS|M_ZERO);
8931 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8932 dap->da_offset = offset;
8933 dap->da_newinum = newinum;
8934 dap->da_state = ATTACHED;
8935 LIST_INIT(&dap->da_jwork);
8936 isindir = bp->b_lblkno >= UFS_NDADDR;
8937 newdirblk = NULL;
8938 if (isnewblk &&
8939 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8940 newdirblk = malloc(sizeof(struct newdirblk),
8941 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8942 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8943 LIST_INIT(&newdirblk->db_mkdir);
8944 }
8945 /*
8946 * If we're creating a new directory setup the dependencies and set
8947 * the dap state to wait for them. Otherwise it's COMPLETE and
8948 * we can move on.
8949 */
8950 if (newdirbp == NULL) {
8951 dap->da_state |= DEPCOMPLETE;
8952 ACQUIRE_LOCK(ump);
8953 } else {
8954 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8955 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8956 &mkdir2);
8957 }
8958 /*
8959 * Link into parent directory pagedep to await its being written.
8960 */
8961 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8962 #ifdef INVARIANTS
8963 if (diradd_lookup(pagedep, offset) != NULL)
8964 panic("softdep_setup_directory_add: %p already at off %d\n",
8965 diradd_lookup(pagedep, offset), offset);
8966 #endif
8967 dap->da_pagedep = pagedep;
8968 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8969 da_pdlist);
8970 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8971 /*
8972 * If we're journaling, link the diradd into the jaddref so it
8973 * may be completed after the journal entry is written. Otherwise,
8974 * link the diradd into its inodedep. If the inode is not yet
8975 * written place it on the bufwait list, otherwise do the post-inode
8976 * write processing to put it on the id_pendinghd list.
8977 */
8978 if (MOUNTEDSUJ(mp)) {
8979 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8980 inoreflst);
8981 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8982 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8983 jaddref->ja_diroff = diroffset;
8984 jaddref->ja_diradd = dap;
8985 add_to_journal(&jaddref->ja_list);
8986 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8987 diradd_inode_written(dap, inodedep);
8988 else
8989 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8990 /*
8991 * Add the journal entries for . and .. links now that the primary
8992 * link is written.
8993 */
8994 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8995 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8996 inoreflst, if_deps);
8997 KASSERT(jaddref != NULL &&
8998 jaddref->ja_ino == jaddref->ja_parent &&
8999 (jaddref->ja_state & MKDIR_BODY),
9000 ("softdep_setup_directory_add: bad dot jaddref %p",
9001 jaddref));
9002 mkdir1->md_jaddref = jaddref;
9003 jaddref->ja_mkdir = mkdir1;
9004 /*
9005 * It is important that the dotdot journal entry
9006 * is added prior to the dot entry since dot writes
9007 * both the dot and dotdot links. These both must
9008 * be added after the primary link for the journal
9009 * to remain consistent.
9010 */
9011 add_to_journal(&mkdir2->md_jaddref->ja_list);
9012 add_to_journal(&jaddref->ja_list);
9013 }
9014 /*
9015 * If we are adding a new directory remember this diradd so that if
9016 * we rename it we can keep the dot and dotdot dependencies. If
9017 * we are adding a new name for an inode that has a mkdiradd we
9018 * must be in rename and we have to move the dot and dotdot
9019 * dependencies to this new name. The old name is being orphaned
9020 * soon.
9021 */
9022 if (mkdir1 != NULL) {
9023 if (inodedep->id_mkdiradd != NULL)
9024 panic("softdep_setup_directory_add: Existing mkdir");
9025 inodedep->id_mkdiradd = dap;
9026 } else if (inodedep->id_mkdiradd)
9027 merge_diradd(inodedep, dap);
9028 if (newdirblk != NULL) {
9029 /*
9030 * There is nothing to do if we are already tracking
9031 * this block.
9032 */
9033 if ((pagedep->pd_state & NEWBLOCK) != 0) {
9034 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
9035 FREE_LOCK(ump);
9036 return (0);
9037 }
9038 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
9039 == 0)
9040 panic("softdep_setup_directory_add: lost entry");
9041 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
9042 pagedep->pd_state |= NEWBLOCK;
9043 pagedep->pd_newdirblk = newdirblk;
9044 newdirblk->db_pagedep = pagedep;
9045 FREE_LOCK(ump);
9046 /*
9047 * If we extended into an indirect signal direnter to sync.
9048 */
9049 if (isindir)
9050 return (1);
9051 return (0);
9052 }
9053 FREE_LOCK(ump);
9054 return (0);
9055 }
9056
9057 /*
9058 * This procedure is called to change the offset of a directory
9059 * entry when compacting a directory block which must be owned
9060 * exclusively by the caller. Note that the actual entry movement
9061 * must be done in this procedure to ensure that no I/O completions
9062 * occur while the move is in progress.
9063 */
9064 void
softdep_change_directoryentry_offset(bp,dp,base,oldloc,newloc,entrysize)9065 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
9066 struct buf *bp; /* Buffer holding directory block. */
9067 struct inode *dp; /* inode for directory */
9068 caddr_t base; /* address of dp->i_offset */
9069 caddr_t oldloc; /* address of old directory location */
9070 caddr_t newloc; /* address of new directory location */
9071 int entrysize; /* size of directory entry */
9072 {
9073 int offset, oldoffset, newoffset;
9074 struct pagedep *pagedep;
9075 struct jmvref *jmvref;
9076 struct diradd *dap;
9077 struct direct *de;
9078 struct mount *mp;
9079 struct ufsmount *ump;
9080 ufs_lbn_t lbn;
9081 int flags;
9082
9083 mp = ITOVFS(dp);
9084 ump = VFSTOUFS(mp);
9085 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9086 ("softdep_change_directoryentry_offset called on "
9087 "non-softdep filesystem"));
9088 de = (struct direct *)oldloc;
9089 jmvref = NULL;
9090 flags = 0;
9091 /*
9092 * Moves are always journaled as it would be too complex to
9093 * determine if any affected adds or removes are present in the
9094 * journal.
9095 */
9096 if (MOUNTEDSUJ(mp)) {
9097 flags = DEPALLOC;
9098 jmvref = newjmvref(dp, de->d_ino,
9099 I_OFFSET(dp) + (oldloc - base),
9100 I_OFFSET(dp) + (newloc - base));
9101 }
9102 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9103 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9104 oldoffset = offset + (oldloc - base);
9105 newoffset = offset + (newloc - base);
9106 ACQUIRE_LOCK(ump);
9107 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
9108 goto done;
9109 dap = diradd_lookup(pagedep, oldoffset);
9110 if (dap) {
9111 dap->da_offset = newoffset;
9112 newoffset = DIRADDHASH(newoffset);
9113 oldoffset = DIRADDHASH(oldoffset);
9114 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
9115 newoffset != oldoffset) {
9116 LIST_REMOVE(dap, da_pdlist);
9117 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
9118 dap, da_pdlist);
9119 }
9120 }
9121 done:
9122 if (jmvref) {
9123 jmvref->jm_pagedep = pagedep;
9124 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
9125 add_to_journal(&jmvref->jm_list);
9126 }
9127 bcopy(oldloc, newloc, entrysize);
9128 FREE_LOCK(ump);
9129 }
9130
9131 /*
9132 * Move the mkdir dependencies and journal work from one diradd to another
9133 * when renaming a directory. The new name must depend on the mkdir deps
9134 * completing as the old name did. Directories can only have one valid link
9135 * at a time so one must be canonical.
9136 */
9137 static void
merge_diradd(inodedep,newdap)9138 merge_diradd(inodedep, newdap)
9139 struct inodedep *inodedep;
9140 struct diradd *newdap;
9141 {
9142 struct diradd *olddap;
9143 struct mkdir *mkdir, *nextmd;
9144 struct ufsmount *ump;
9145 short state;
9146
9147 olddap = inodedep->id_mkdiradd;
9148 inodedep->id_mkdiradd = newdap;
9149 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9150 newdap->da_state &= ~DEPCOMPLETE;
9151 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9152 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9153 mkdir = nextmd) {
9154 nextmd = LIST_NEXT(mkdir, md_mkdirs);
9155 if (mkdir->md_diradd != olddap)
9156 continue;
9157 mkdir->md_diradd = newdap;
9158 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
9159 newdap->da_state |= state;
9160 olddap->da_state &= ~state;
9161 if ((olddap->da_state &
9162 (MKDIR_PARENT | MKDIR_BODY)) == 0)
9163 break;
9164 }
9165 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9166 panic("merge_diradd: unfound ref");
9167 }
9168 /*
9169 * Any mkdir related journal items are not safe to be freed until
9170 * the new name is stable.
9171 */
9172 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
9173 olddap->da_state |= DEPCOMPLETE;
9174 complete_diradd(olddap);
9175 }
9176
9177 /*
9178 * Move the diradd to the pending list when all diradd dependencies are
9179 * complete.
9180 */
9181 static void
complete_diradd(dap)9182 complete_diradd(dap)
9183 struct diradd *dap;
9184 {
9185 struct pagedep *pagedep;
9186
9187 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
9188 if (dap->da_state & DIRCHG)
9189 pagedep = dap->da_previous->dm_pagedep;
9190 else
9191 pagedep = dap->da_pagedep;
9192 LIST_REMOVE(dap, da_pdlist);
9193 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9194 }
9195 }
9196
9197 /*
9198 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
9199 * add entries and conditionally journal the remove.
9200 */
9201 static void
cancel_diradd(dap,dirrem,jremref,dotremref,dotdotremref)9202 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
9203 struct diradd *dap;
9204 struct dirrem *dirrem;
9205 struct jremref *jremref;
9206 struct jremref *dotremref;
9207 struct jremref *dotdotremref;
9208 {
9209 struct inodedep *inodedep;
9210 struct jaddref *jaddref;
9211 struct inoref *inoref;
9212 struct ufsmount *ump;
9213 struct mkdir *mkdir;
9214
9215 /*
9216 * If no remove references were allocated we're on a non-journaled
9217 * filesystem and can skip the cancel step.
9218 */
9219 if (jremref == NULL) {
9220 free_diradd(dap, NULL);
9221 return;
9222 }
9223 /*
9224 * Cancel the primary name an free it if it does not require
9225 * journaling.
9226 */
9227 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
9228 0, &inodedep) != 0) {
9229 /* Abort the addref that reference this diradd. */
9230 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
9231 if (inoref->if_list.wk_type != D_JADDREF)
9232 continue;
9233 jaddref = (struct jaddref *)inoref;
9234 if (jaddref->ja_diradd != dap)
9235 continue;
9236 if (cancel_jaddref(jaddref, inodedep,
9237 &dirrem->dm_jwork) == 0) {
9238 free_jremref(jremref);
9239 jremref = NULL;
9240 }
9241 break;
9242 }
9243 }
9244 /*
9245 * Cancel subordinate names and free them if they do not require
9246 * journaling.
9247 */
9248 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9249 ump = VFSTOUFS(dap->da_list.wk_mp);
9250 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
9251 if (mkdir->md_diradd != dap)
9252 continue;
9253 if ((jaddref = mkdir->md_jaddref) == NULL)
9254 continue;
9255 mkdir->md_jaddref = NULL;
9256 if (mkdir->md_state & MKDIR_PARENT) {
9257 if (cancel_jaddref(jaddref, NULL,
9258 &dirrem->dm_jwork) == 0) {
9259 free_jremref(dotdotremref);
9260 dotdotremref = NULL;
9261 }
9262 } else {
9263 if (cancel_jaddref(jaddref, inodedep,
9264 &dirrem->dm_jwork) == 0) {
9265 free_jremref(dotremref);
9266 dotremref = NULL;
9267 }
9268 }
9269 }
9270 }
9271
9272 if (jremref)
9273 journal_jremref(dirrem, jremref, inodedep);
9274 if (dotremref)
9275 journal_jremref(dirrem, dotremref, inodedep);
9276 if (dotdotremref)
9277 journal_jremref(dirrem, dotdotremref, NULL);
9278 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
9279 free_diradd(dap, &dirrem->dm_jwork);
9280 }
9281
9282 /*
9283 * Free a diradd dependency structure.
9284 */
9285 static void
free_diradd(dap,wkhd)9286 free_diradd(dap, wkhd)
9287 struct diradd *dap;
9288 struct workhead *wkhd;
9289 {
9290 struct dirrem *dirrem;
9291 struct pagedep *pagedep;
9292 struct inodedep *inodedep;
9293 struct mkdir *mkdir, *nextmd;
9294 struct ufsmount *ump;
9295
9296 ump = VFSTOUFS(dap->da_list.wk_mp);
9297 LOCK_OWNED(ump);
9298 LIST_REMOVE(dap, da_pdlist);
9299 if (dap->da_state & ONWORKLIST)
9300 WORKLIST_REMOVE(&dap->da_list);
9301 if ((dap->da_state & DIRCHG) == 0) {
9302 pagedep = dap->da_pagedep;
9303 } else {
9304 dirrem = dap->da_previous;
9305 pagedep = dirrem->dm_pagedep;
9306 dirrem->dm_dirinum = pagedep->pd_ino;
9307 dirrem->dm_state |= COMPLETE;
9308 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9309 add_to_worklist(&dirrem->dm_list, 0);
9310 }
9311 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
9312 0, &inodedep) != 0)
9313 if (inodedep->id_mkdiradd == dap)
9314 inodedep->id_mkdiradd = NULL;
9315 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9316 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9317 mkdir = nextmd) {
9318 nextmd = LIST_NEXT(mkdir, md_mkdirs);
9319 if (mkdir->md_diradd != dap)
9320 continue;
9321 dap->da_state &=
9322 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
9323 LIST_REMOVE(mkdir, md_mkdirs);
9324 if (mkdir->md_state & ONWORKLIST)
9325 WORKLIST_REMOVE(&mkdir->md_list);
9326 if (mkdir->md_jaddref != NULL)
9327 panic("free_diradd: Unexpected jaddref");
9328 WORKITEM_FREE(mkdir, D_MKDIR);
9329 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
9330 break;
9331 }
9332 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9333 panic("free_diradd: unfound ref");
9334 }
9335 if (inodedep)
9336 free_inodedep(inodedep);
9337 /*
9338 * Free any journal segments waiting for the directory write.
9339 */
9340 handle_jwork(&dap->da_jwork);
9341 WORKITEM_FREE(dap, D_DIRADD);
9342 }
9343
9344 /*
9345 * Directory entry removal dependencies.
9346 *
9347 * When removing a directory entry, the entry's inode pointer must be
9348 * zero'ed on disk before the corresponding inode's link count is decremented
9349 * (possibly freeing the inode for re-use). This dependency is handled by
9350 * updating the directory entry but delaying the inode count reduction until
9351 * after the directory block has been written to disk. After this point, the
9352 * inode count can be decremented whenever it is convenient.
9353 */
9354
9355 /*
9356 * This routine should be called immediately after removing
9357 * a directory entry. The inode's link count should not be
9358 * decremented by the calling procedure -- the soft updates
9359 * code will do this task when it is safe.
9360 */
9361 void
softdep_setup_remove(bp,dp,ip,isrmdir)9362 softdep_setup_remove(bp, dp, ip, isrmdir)
9363 struct buf *bp; /* buffer containing directory block */
9364 struct inode *dp; /* inode for the directory being modified */
9365 struct inode *ip; /* inode for directory entry being removed */
9366 int isrmdir; /* indicates if doing RMDIR */
9367 {
9368 struct dirrem *dirrem, *prevdirrem;
9369 struct inodedep *inodedep;
9370 struct ufsmount *ump;
9371 int direct;
9372
9373 ump = ITOUMP(ip);
9374 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9375 ("softdep_setup_remove called on non-softdep filesystem"));
9376 /*
9377 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
9378 * newdirrem() to setup the full directory remove which requires
9379 * isrmdir > 1.
9380 */
9381 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9382 /*
9383 * Add the dirrem to the inodedep's pending remove list for quick
9384 * discovery later.
9385 */
9386 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9387 panic("softdep_setup_remove: Lost inodedep.");
9388 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9389 dirrem->dm_state |= ONDEPLIST;
9390 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9391
9392 /*
9393 * If the COMPLETE flag is clear, then there were no active
9394 * entries and we want to roll back to a zeroed entry until
9395 * the new inode is committed to disk. If the COMPLETE flag is
9396 * set then we have deleted an entry that never made it to
9397 * disk. If the entry we deleted resulted from a name change,
9398 * then the old name still resides on disk. We cannot delete
9399 * its inode (returned to us in prevdirrem) until the zeroed
9400 * directory entry gets to disk. The new inode has never been
9401 * referenced on the disk, so can be deleted immediately.
9402 */
9403 if ((dirrem->dm_state & COMPLETE) == 0) {
9404 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9405 dm_next);
9406 FREE_LOCK(ump);
9407 } else {
9408 if (prevdirrem != NULL)
9409 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9410 prevdirrem, dm_next);
9411 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9412 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9413 FREE_LOCK(ump);
9414 if (direct)
9415 handle_workitem_remove(dirrem, 0);
9416 }
9417 }
9418
9419 /*
9420 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9421 * pd_pendinghd list of a pagedep.
9422 */
9423 static struct diradd *
diradd_lookup(pagedep,offset)9424 diradd_lookup(pagedep, offset)
9425 struct pagedep *pagedep;
9426 int offset;
9427 {
9428 struct diradd *dap;
9429
9430 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9431 if (dap->da_offset == offset)
9432 return (dap);
9433 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9434 if (dap->da_offset == offset)
9435 return (dap);
9436 return (NULL);
9437 }
9438
9439 /*
9440 * Search for a .. diradd dependency in a directory that is being removed.
9441 * If the directory was renamed to a new parent we have a diradd rather
9442 * than a mkdir for the .. entry. We need to cancel it now before
9443 * it is found in truncate().
9444 */
9445 static struct jremref *
cancel_diradd_dotdot(ip,dirrem,jremref)9446 cancel_diradd_dotdot(ip, dirrem, jremref)
9447 struct inode *ip;
9448 struct dirrem *dirrem;
9449 struct jremref *jremref;
9450 {
9451 struct pagedep *pagedep;
9452 struct diradd *dap;
9453 struct worklist *wk;
9454
9455 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9456 return (jremref);
9457 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9458 if (dap == NULL)
9459 return (jremref);
9460 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9461 /*
9462 * Mark any journal work as belonging to the parent so it is freed
9463 * with the .. reference.
9464 */
9465 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9466 wk->wk_state |= MKDIR_PARENT;
9467 return (NULL);
9468 }
9469
9470 /*
9471 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9472 * replace it with a dirrem/diradd pair as a result of re-parenting a
9473 * directory. This ensures that we don't simultaneously have a mkdir and
9474 * a diradd for the same .. entry.
9475 */
9476 static struct jremref *
cancel_mkdir_dotdot(ip,dirrem,jremref)9477 cancel_mkdir_dotdot(ip, dirrem, jremref)
9478 struct inode *ip;
9479 struct dirrem *dirrem;
9480 struct jremref *jremref;
9481 {
9482 struct inodedep *inodedep;
9483 struct jaddref *jaddref;
9484 struct ufsmount *ump;
9485 struct mkdir *mkdir;
9486 struct diradd *dap;
9487 struct mount *mp;
9488
9489 mp = ITOVFS(ip);
9490 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9491 return (jremref);
9492 dap = inodedep->id_mkdiradd;
9493 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9494 return (jremref);
9495 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9496 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9497 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9498 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9499 break;
9500 if (mkdir == NULL)
9501 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9502 if ((jaddref = mkdir->md_jaddref) != NULL) {
9503 mkdir->md_jaddref = NULL;
9504 jaddref->ja_state &= ~MKDIR_PARENT;
9505 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9506 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9507 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9508 journal_jremref(dirrem, jremref, inodedep);
9509 jremref = NULL;
9510 }
9511 }
9512 if (mkdir->md_state & ONWORKLIST)
9513 WORKLIST_REMOVE(&mkdir->md_list);
9514 mkdir->md_state |= ALLCOMPLETE;
9515 complete_mkdir(mkdir);
9516 return (jremref);
9517 }
9518
9519 static void
journal_jremref(dirrem,jremref,inodedep)9520 journal_jremref(dirrem, jremref, inodedep)
9521 struct dirrem *dirrem;
9522 struct jremref *jremref;
9523 struct inodedep *inodedep;
9524 {
9525
9526 if (inodedep == NULL)
9527 if (inodedep_lookup(jremref->jr_list.wk_mp,
9528 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9529 panic("journal_jremref: Lost inodedep");
9530 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9531 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9532 add_to_journal(&jremref->jr_list);
9533 }
9534
9535 static void
dirrem_journal(dirrem,jremref,dotremref,dotdotremref)9536 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9537 struct dirrem *dirrem;
9538 struct jremref *jremref;
9539 struct jremref *dotremref;
9540 struct jremref *dotdotremref;
9541 {
9542 struct inodedep *inodedep;
9543
9544 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9545 &inodedep) == 0)
9546 panic("dirrem_journal: Lost inodedep");
9547 journal_jremref(dirrem, jremref, inodedep);
9548 if (dotremref)
9549 journal_jremref(dirrem, dotremref, inodedep);
9550 if (dotdotremref)
9551 journal_jremref(dirrem, dotdotremref, NULL);
9552 }
9553
9554 /*
9555 * Allocate a new dirrem if appropriate and return it along with
9556 * its associated pagedep. Called without a lock, returns with lock.
9557 */
9558 static struct dirrem *
newdirrem(bp,dp,ip,isrmdir,prevdirremp)9559 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9560 struct buf *bp; /* buffer containing directory block */
9561 struct inode *dp; /* inode for the directory being modified */
9562 struct inode *ip; /* inode for directory entry being removed */
9563 int isrmdir; /* indicates if doing RMDIR */
9564 struct dirrem **prevdirremp; /* previously referenced inode, if any */
9565 {
9566 int offset;
9567 ufs_lbn_t lbn;
9568 struct diradd *dap;
9569 struct dirrem *dirrem;
9570 struct pagedep *pagedep;
9571 struct jremref *jremref;
9572 struct jremref *dotremref;
9573 struct jremref *dotdotremref;
9574 struct vnode *dvp;
9575 struct ufsmount *ump;
9576
9577 /*
9578 * Whiteouts have no deletion dependencies.
9579 */
9580 if (ip == NULL)
9581 panic("newdirrem: whiteout");
9582 dvp = ITOV(dp);
9583 ump = ITOUMP(dp);
9584
9585 /*
9586 * If the system is over its limit and our filesystem is
9587 * responsible for more than our share of that usage and
9588 * we are not a snapshot, request some inodedep cleanup.
9589 * Limiting the number of dirrem structures will also limit
9590 * the number of freefile and freeblks structures.
9591 */
9592 ACQUIRE_LOCK(ump);
9593 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9594 schedule_cleanup(UFSTOVFS(ump));
9595 else
9596 FREE_LOCK(ump);
9597 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9598 M_ZERO);
9599 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9600 LIST_INIT(&dirrem->dm_jremrefhd);
9601 LIST_INIT(&dirrem->dm_jwork);
9602 dirrem->dm_state = isrmdir ? RMDIR : 0;
9603 dirrem->dm_oldinum = ip->i_number;
9604 *prevdirremp = NULL;
9605 /*
9606 * Allocate remove reference structures to track journal write
9607 * dependencies. We will always have one for the link and
9608 * when doing directories we will always have one more for dot.
9609 * When renaming a directory we skip the dotdot link change so
9610 * this is not needed.
9611 */
9612 jremref = dotremref = dotdotremref = NULL;
9613 if (DOINGSUJ(dvp)) {
9614 if (isrmdir) {
9615 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9616 ip->i_effnlink + 2);
9617 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9618 ip->i_effnlink + 1);
9619 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9620 dp->i_effnlink + 1);
9621 dotdotremref->jr_state |= MKDIR_PARENT;
9622 } else
9623 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9624 ip->i_effnlink + 1);
9625 }
9626 ACQUIRE_LOCK(ump);
9627 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9628 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9629 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9630 &pagedep);
9631 dirrem->dm_pagedep = pagedep;
9632 dirrem->dm_offset = offset;
9633 /*
9634 * If we're renaming a .. link to a new directory, cancel any
9635 * existing MKDIR_PARENT mkdir. If it has already been canceled
9636 * the jremref is preserved for any potential diradd in this
9637 * location. This can not coincide with a rmdir.
9638 */
9639 if (I_OFFSET(dp) == DOTDOT_OFFSET) {
9640 if (isrmdir)
9641 panic("newdirrem: .. directory change during remove?");
9642 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9643 }
9644 /*
9645 * If we're removing a directory search for the .. dependency now and
9646 * cancel it. Any pending journal work will be added to the dirrem
9647 * to be completed when the workitem remove completes.
9648 */
9649 if (isrmdir)
9650 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9651 /*
9652 * Check for a diradd dependency for the same directory entry.
9653 * If present, then both dependencies become obsolete and can
9654 * be de-allocated.
9655 */
9656 dap = diradd_lookup(pagedep, offset);
9657 if (dap == NULL) {
9658 /*
9659 * Link the jremref structures into the dirrem so they are
9660 * written prior to the pagedep.
9661 */
9662 if (jremref)
9663 dirrem_journal(dirrem, jremref, dotremref,
9664 dotdotremref);
9665 return (dirrem);
9666 }
9667 /*
9668 * Must be ATTACHED at this point.
9669 */
9670 if ((dap->da_state & ATTACHED) == 0)
9671 panic("newdirrem: not ATTACHED");
9672 if (dap->da_newinum != ip->i_number)
9673 panic("newdirrem: inum %ju should be %ju",
9674 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9675 /*
9676 * If we are deleting a changed name that never made it to disk,
9677 * then return the dirrem describing the previous inode (which
9678 * represents the inode currently referenced from this entry on disk).
9679 */
9680 if ((dap->da_state & DIRCHG) != 0) {
9681 *prevdirremp = dap->da_previous;
9682 dap->da_state &= ~DIRCHG;
9683 dap->da_pagedep = pagedep;
9684 }
9685 /*
9686 * We are deleting an entry that never made it to disk.
9687 * Mark it COMPLETE so we can delete its inode immediately.
9688 */
9689 dirrem->dm_state |= COMPLETE;
9690 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9691 #ifdef INVARIANTS
9692 if (isrmdir == 0) {
9693 struct worklist *wk;
9694
9695 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9696 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9697 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9698 }
9699 #endif
9700
9701 return (dirrem);
9702 }
9703
9704 /*
9705 * Directory entry change dependencies.
9706 *
9707 * Changing an existing directory entry requires that an add operation
9708 * be completed first followed by a deletion. The semantics for the addition
9709 * are identical to the description of adding a new entry above except
9710 * that the rollback is to the old inode number rather than zero. Once
9711 * the addition dependency is completed, the removal is done as described
9712 * in the removal routine above.
9713 */
9714
9715 /*
9716 * This routine should be called immediately after changing
9717 * a directory entry. The inode's link count should not be
9718 * decremented by the calling procedure -- the soft updates
9719 * code will perform this task when it is safe.
9720 */
9721 void
softdep_setup_directory_change(bp,dp,ip,newinum,isrmdir)9722 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9723 struct buf *bp; /* buffer containing directory block */
9724 struct inode *dp; /* inode for the directory being modified */
9725 struct inode *ip; /* inode for directory entry being removed */
9726 ino_t newinum; /* new inode number for changed entry */
9727 int isrmdir; /* indicates if doing RMDIR */
9728 {
9729 int offset;
9730 struct diradd *dap = NULL;
9731 struct dirrem *dirrem, *prevdirrem;
9732 struct pagedep *pagedep;
9733 struct inodedep *inodedep;
9734 struct jaddref *jaddref;
9735 struct mount *mp;
9736 struct ufsmount *ump;
9737
9738 mp = ITOVFS(dp);
9739 ump = VFSTOUFS(mp);
9740 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9741 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9742 ("softdep_setup_directory_change called on non-softdep filesystem"));
9743
9744 /*
9745 * Whiteouts do not need diradd dependencies.
9746 */
9747 if (newinum != UFS_WINO) {
9748 dap = malloc(sizeof(struct diradd),
9749 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9750 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9751 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9752 dap->da_offset = offset;
9753 dap->da_newinum = newinum;
9754 LIST_INIT(&dap->da_jwork);
9755 }
9756
9757 /*
9758 * Allocate a new dirrem and ACQUIRE_LOCK.
9759 */
9760 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9761 pagedep = dirrem->dm_pagedep;
9762 /*
9763 * The possible values for isrmdir:
9764 * 0 - non-directory file rename
9765 * 1 - directory rename within same directory
9766 * inum - directory rename to new directory of given inode number
9767 * When renaming to a new directory, we are both deleting and
9768 * creating a new directory entry, so the link count on the new
9769 * directory should not change. Thus we do not need the followup
9770 * dirrem which is usually done in handle_workitem_remove. We set
9771 * the DIRCHG flag to tell handle_workitem_remove to skip the
9772 * followup dirrem.
9773 */
9774 if (isrmdir > 1)
9775 dirrem->dm_state |= DIRCHG;
9776
9777 /*
9778 * Whiteouts have no additional dependencies,
9779 * so just put the dirrem on the correct list.
9780 */
9781 if (newinum == UFS_WINO) {
9782 if ((dirrem->dm_state & COMPLETE) == 0) {
9783 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9784 dm_next);
9785 } else {
9786 dirrem->dm_dirinum = pagedep->pd_ino;
9787 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9788 add_to_worklist(&dirrem->dm_list, 0);
9789 }
9790 FREE_LOCK(ump);
9791 return;
9792 }
9793 /*
9794 * Add the dirrem to the inodedep's pending remove list for quick
9795 * discovery later. A valid nlinkdelta ensures that this lookup
9796 * will not fail.
9797 */
9798 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9799 panic("softdep_setup_directory_change: Lost inodedep.");
9800 dirrem->dm_state |= ONDEPLIST;
9801 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9802
9803 /*
9804 * If the COMPLETE flag is clear, then there were no active
9805 * entries and we want to roll back to the previous inode until
9806 * the new inode is committed to disk. If the COMPLETE flag is
9807 * set, then we have deleted an entry that never made it to disk.
9808 * If the entry we deleted resulted from a name change, then the old
9809 * inode reference still resides on disk. Any rollback that we do
9810 * needs to be to that old inode (returned to us in prevdirrem). If
9811 * the entry we deleted resulted from a create, then there is
9812 * no entry on the disk, so we want to roll back to zero rather
9813 * than the uncommitted inode. In either of the COMPLETE cases we
9814 * want to immediately free the unwritten and unreferenced inode.
9815 */
9816 if ((dirrem->dm_state & COMPLETE) == 0) {
9817 dap->da_previous = dirrem;
9818 } else {
9819 if (prevdirrem != NULL) {
9820 dap->da_previous = prevdirrem;
9821 } else {
9822 dap->da_state &= ~DIRCHG;
9823 dap->da_pagedep = pagedep;
9824 }
9825 dirrem->dm_dirinum = pagedep->pd_ino;
9826 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9827 add_to_worklist(&dirrem->dm_list, 0);
9828 }
9829 /*
9830 * Lookup the jaddref for this journal entry. We must finish
9831 * initializing it and make the diradd write dependent on it.
9832 * If we're not journaling, put it on the id_bufwait list if the
9833 * inode is not yet written. If it is written, do the post-inode
9834 * write processing to put it on the id_pendinghd list.
9835 */
9836 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9837 if (MOUNTEDSUJ(mp)) {
9838 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9839 inoreflst);
9840 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9841 ("softdep_setup_directory_change: bad jaddref %p",
9842 jaddref));
9843 jaddref->ja_diroff = I_OFFSET(dp);
9844 jaddref->ja_diradd = dap;
9845 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9846 dap, da_pdlist);
9847 add_to_journal(&jaddref->ja_list);
9848 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9849 dap->da_state |= COMPLETE;
9850 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9851 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9852 } else {
9853 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9854 dap, da_pdlist);
9855 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9856 }
9857 /*
9858 * If we're making a new name for a directory that has not been
9859 * committed when need to move the dot and dotdot references to
9860 * this new name.
9861 */
9862 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET)
9863 merge_diradd(inodedep, dap);
9864 FREE_LOCK(ump);
9865 }
9866
9867 /*
9868 * Called whenever the link count on an inode is changed.
9869 * It creates an inode dependency so that the new reference(s)
9870 * to the inode cannot be committed to disk until the updated
9871 * inode has been written.
9872 */
9873 void
softdep_change_linkcnt(ip)9874 softdep_change_linkcnt(ip)
9875 struct inode *ip; /* the inode with the increased link count */
9876 {
9877 struct inodedep *inodedep;
9878 struct ufsmount *ump;
9879
9880 ump = ITOUMP(ip);
9881 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9882 ("softdep_change_linkcnt called on non-softdep filesystem"));
9883 ACQUIRE_LOCK(ump);
9884 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9885 if (ip->i_nlink < ip->i_effnlink)
9886 panic("softdep_change_linkcnt: bad delta");
9887 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9888 FREE_LOCK(ump);
9889 }
9890
9891 /*
9892 * Attach a sbdep dependency to the superblock buf so that we can keep
9893 * track of the head of the linked list of referenced but unlinked inodes.
9894 */
9895 void
softdep_setup_sbupdate(ump,fs,bp)9896 softdep_setup_sbupdate(ump, fs, bp)
9897 struct ufsmount *ump;
9898 struct fs *fs;
9899 struct buf *bp;
9900 {
9901 struct sbdep *sbdep;
9902 struct worklist *wk;
9903
9904 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9905 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9906 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9907 if (wk->wk_type == D_SBDEP)
9908 break;
9909 if (wk != NULL)
9910 return;
9911 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9912 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9913 sbdep->sb_fs = fs;
9914 sbdep->sb_ump = ump;
9915 ACQUIRE_LOCK(ump);
9916 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9917 FREE_LOCK(ump);
9918 }
9919
9920 /*
9921 * Return the first unlinked inodedep which is ready to be the head of the
9922 * list. The inodedep and all those after it must have valid next pointers.
9923 */
9924 static struct inodedep *
first_unlinked_inodedep(ump)9925 first_unlinked_inodedep(ump)
9926 struct ufsmount *ump;
9927 {
9928 struct inodedep *inodedep;
9929 struct inodedep *idp;
9930
9931 LOCK_OWNED(ump);
9932 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9933 inodedep; inodedep = idp) {
9934 if ((inodedep->id_state & UNLINKNEXT) == 0)
9935 return (NULL);
9936 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9937 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9938 break;
9939 if ((inodedep->id_state & UNLINKPREV) == 0)
9940 break;
9941 }
9942 return (inodedep);
9943 }
9944
9945 /*
9946 * Set the sujfree unlinked head pointer prior to writing a superblock.
9947 */
9948 static void
initiate_write_sbdep(sbdep)9949 initiate_write_sbdep(sbdep)
9950 struct sbdep *sbdep;
9951 {
9952 struct inodedep *inodedep;
9953 struct fs *bpfs;
9954 struct fs *fs;
9955
9956 bpfs = sbdep->sb_fs;
9957 fs = sbdep->sb_ump->um_fs;
9958 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9959 if (inodedep) {
9960 fs->fs_sujfree = inodedep->id_ino;
9961 inodedep->id_state |= UNLINKPREV;
9962 } else
9963 fs->fs_sujfree = 0;
9964 bpfs->fs_sujfree = fs->fs_sujfree;
9965 /*
9966 * Because we have made changes to the superblock, we need to
9967 * recompute its check-hash.
9968 */
9969 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9970 }
9971
9972 /*
9973 * After a superblock is written determine whether it must be written again
9974 * due to a changing unlinked list head.
9975 */
9976 static int
handle_written_sbdep(sbdep,bp)9977 handle_written_sbdep(sbdep, bp)
9978 struct sbdep *sbdep;
9979 struct buf *bp;
9980 {
9981 struct inodedep *inodedep;
9982 struct fs *fs;
9983
9984 LOCK_OWNED(sbdep->sb_ump);
9985 fs = sbdep->sb_fs;
9986 /*
9987 * If the superblock doesn't match the in-memory list start over.
9988 */
9989 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9990 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9991 (inodedep == NULL && fs->fs_sujfree != 0)) {
9992 bdirty(bp);
9993 return (1);
9994 }
9995 WORKITEM_FREE(sbdep, D_SBDEP);
9996 if (fs->fs_sujfree == 0)
9997 return (0);
9998 /*
9999 * Now that we have a record of this inode in stable store allow it
10000 * to be written to free up pending work. Inodes may see a lot of
10001 * write activity after they are unlinked which we must not hold up.
10002 */
10003 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
10004 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
10005 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
10006 inodedep, inodedep->id_state);
10007 if (inodedep->id_state & UNLINKONLIST)
10008 break;
10009 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
10010 }
10011
10012 return (0);
10013 }
10014
10015 /*
10016 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
10017 */
10018 static void
unlinked_inodedep(mp,inodedep)10019 unlinked_inodedep(mp, inodedep)
10020 struct mount *mp;
10021 struct inodedep *inodedep;
10022 {
10023 struct ufsmount *ump;
10024
10025 ump = VFSTOUFS(mp);
10026 LOCK_OWNED(ump);
10027 if (MOUNTEDSUJ(mp) == 0)
10028 return;
10029 ump->um_fs->fs_fmod = 1;
10030 if (inodedep->id_state & UNLINKED)
10031 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
10032 inodedep->id_state |= UNLINKED;
10033 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
10034 }
10035
10036 /*
10037 * Remove an inodedep from the unlinked inodedep list. This may require
10038 * disk writes if the inode has made it that far.
10039 */
10040 static void
clear_unlinked_inodedep(inodedep)10041 clear_unlinked_inodedep(inodedep)
10042 struct inodedep *inodedep;
10043 {
10044 struct ufs2_dinode *dip;
10045 struct ufsmount *ump;
10046 struct inodedep *idp;
10047 struct inodedep *idn;
10048 struct fs *fs, *bpfs;
10049 struct buf *bp;
10050 daddr_t dbn;
10051 ino_t ino;
10052 ino_t nino;
10053 ino_t pino;
10054 int error;
10055
10056 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10057 fs = ump->um_fs;
10058 ino = inodedep->id_ino;
10059 error = 0;
10060 for (;;) {
10061 LOCK_OWNED(ump);
10062 KASSERT((inodedep->id_state & UNLINKED) != 0,
10063 ("clear_unlinked_inodedep: inodedep %p not unlinked",
10064 inodedep));
10065 /*
10066 * If nothing has yet been written simply remove us from
10067 * the in memory list and return. This is the most common
10068 * case where handle_workitem_remove() loses the final
10069 * reference.
10070 */
10071 if ((inodedep->id_state & UNLINKLINKS) == 0)
10072 break;
10073 /*
10074 * If we have a NEXT pointer and no PREV pointer we can simply
10075 * clear NEXT's PREV and remove ourselves from the list. Be
10076 * careful not to clear PREV if the superblock points at
10077 * next as well.
10078 */
10079 idn = TAILQ_NEXT(inodedep, id_unlinked);
10080 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
10081 if (idn && fs->fs_sujfree != idn->id_ino)
10082 idn->id_state &= ~UNLINKPREV;
10083 break;
10084 }
10085 /*
10086 * Here we have an inodedep which is actually linked into
10087 * the list. We must remove it by forcing a write to the
10088 * link before us, whether it be the superblock or an inode.
10089 * Unfortunately the list may change while we're waiting
10090 * on the buf lock for either resource so we must loop until
10091 * we lock the right one. If both the superblock and an
10092 * inode point to this inode we must clear the inode first
10093 * followed by the superblock.
10094 */
10095 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
10096 pino = 0;
10097 if (idp && (idp->id_state & UNLINKNEXT))
10098 pino = idp->id_ino;
10099 FREE_LOCK(ump);
10100 if (pino == 0) {
10101 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
10102 (int)fs->fs_sbsize, 0, 0, 0);
10103 } else {
10104 dbn = fsbtodb(fs, ino_to_fsba(fs, pino));
10105 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn,
10106 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL,
10107 &bp);
10108 }
10109 ACQUIRE_LOCK(ump);
10110 if (error)
10111 break;
10112 /* If the list has changed restart the loop. */
10113 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
10114 nino = 0;
10115 if (idp && (idp->id_state & UNLINKNEXT))
10116 nino = idp->id_ino;
10117 if (nino != pino ||
10118 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
10119 FREE_LOCK(ump);
10120 brelse(bp);
10121 ACQUIRE_LOCK(ump);
10122 continue;
10123 }
10124 nino = 0;
10125 idn = TAILQ_NEXT(inodedep, id_unlinked);
10126 if (idn)
10127 nino = idn->id_ino;
10128 /*
10129 * Remove us from the in memory list. After this we cannot
10130 * access the inodedep.
10131 */
10132 KASSERT((inodedep->id_state & UNLINKED) != 0,
10133 ("clear_unlinked_inodedep: inodedep %p not unlinked",
10134 inodedep));
10135 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
10136 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
10137 FREE_LOCK(ump);
10138 /*
10139 * The predecessor's next pointer is manually updated here
10140 * so that the NEXT flag is never cleared for an element
10141 * that is in the list.
10142 */
10143 if (pino == 0) {
10144 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
10145 bpfs = (struct fs *)bp->b_data;
10146 ffs_oldfscompat_write(bpfs, ump);
10147 softdep_setup_sbupdate(ump, bpfs, bp);
10148 /*
10149 * Because we may have made changes to the superblock,
10150 * we need to recompute its check-hash.
10151 */
10152 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
10153 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
10154 ((struct ufs1_dinode *)bp->b_data +
10155 ino_to_fsbo(fs, pino))->di_freelink = nino;
10156 } else {
10157 dip = (struct ufs2_dinode *)bp->b_data +
10158 ino_to_fsbo(fs, pino);
10159 dip->di_freelink = nino;
10160 ffs_update_dinode_ckhash(fs, dip);
10161 }
10162 /*
10163 * If the bwrite fails we have no recourse to recover. The
10164 * filesystem is corrupted already.
10165 */
10166 bwrite(bp);
10167 ACQUIRE_LOCK(ump);
10168 /*
10169 * If the superblock pointer still needs to be cleared force
10170 * a write here.
10171 */
10172 if (fs->fs_sujfree == ino) {
10173 FREE_LOCK(ump);
10174 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
10175 (int)fs->fs_sbsize, 0, 0, 0);
10176 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
10177 bpfs = (struct fs *)bp->b_data;
10178 ffs_oldfscompat_write(bpfs, ump);
10179 softdep_setup_sbupdate(ump, bpfs, bp);
10180 /*
10181 * Because we may have made changes to the superblock,
10182 * we need to recompute its check-hash.
10183 */
10184 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
10185 bwrite(bp);
10186 ACQUIRE_LOCK(ump);
10187 }
10188
10189 if (fs->fs_sujfree != ino)
10190 return;
10191 panic("clear_unlinked_inodedep: Failed to clear free head");
10192 }
10193 if (inodedep->id_ino == fs->fs_sujfree)
10194 panic("clear_unlinked_inodedep: Freeing head of free list");
10195 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
10196 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
10197 return;
10198 }
10199
10200 /*
10201 * This workitem decrements the inode's link count.
10202 * If the link count reaches zero, the file is removed.
10203 */
10204 static int
handle_workitem_remove(dirrem,flags)10205 handle_workitem_remove(dirrem, flags)
10206 struct dirrem *dirrem;
10207 int flags;
10208 {
10209 struct inodedep *inodedep;
10210 struct workhead dotdotwk;
10211 struct worklist *wk;
10212 struct ufsmount *ump;
10213 struct mount *mp;
10214 struct vnode *vp;
10215 struct inode *ip;
10216 ino_t oldinum;
10217
10218 if (dirrem->dm_state & ONWORKLIST)
10219 panic("handle_workitem_remove: dirrem %p still on worklist",
10220 dirrem);
10221 oldinum = dirrem->dm_oldinum;
10222 mp = dirrem->dm_list.wk_mp;
10223 ump = VFSTOUFS(mp);
10224 flags |= LK_EXCLUSIVE;
10225 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ |
10226 FFSV_FORCEINODEDEP) != 0)
10227 return (EBUSY);
10228 ip = VTOI(vp);
10229 MPASS(ip->i_mode != 0);
10230 ACQUIRE_LOCK(ump);
10231 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
10232 panic("handle_workitem_remove: lost inodedep");
10233 if (dirrem->dm_state & ONDEPLIST)
10234 LIST_REMOVE(dirrem, dm_inonext);
10235 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
10236 ("handle_workitem_remove: Journal entries not written."));
10237
10238 /*
10239 * Move all dependencies waiting on the remove to complete
10240 * from the dirrem to the inode inowait list to be completed
10241 * after the inode has been updated and written to disk.
10242 *
10243 * Any marked MKDIR_PARENT are saved to be completed when the
10244 * dotdot ref is removed unless DIRCHG is specified. For
10245 * directory change operations there will be no further
10246 * directory writes and the jsegdeps need to be moved along
10247 * with the rest to be completed when the inode is free or
10248 * stable in the inode free list.
10249 */
10250 LIST_INIT(&dotdotwk);
10251 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
10252 WORKLIST_REMOVE(wk);
10253 if ((dirrem->dm_state & DIRCHG) == 0 &&
10254 wk->wk_state & MKDIR_PARENT) {
10255 wk->wk_state &= ~MKDIR_PARENT;
10256 WORKLIST_INSERT(&dotdotwk, wk);
10257 continue;
10258 }
10259 WORKLIST_INSERT(&inodedep->id_inowait, wk);
10260 }
10261 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
10262 /*
10263 * Normal file deletion.
10264 */
10265 if ((dirrem->dm_state & RMDIR) == 0) {
10266 ip->i_nlink--;
10267 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
10268 "%ju negative i_nlink %d", (intmax_t)ip->i_number,
10269 ip->i_nlink));
10270 DIP_SET(ip, i_nlink, ip->i_nlink);
10271 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10272 if (ip->i_nlink < ip->i_effnlink)
10273 panic("handle_workitem_remove: bad file delta");
10274 if (ip->i_nlink == 0)
10275 unlinked_inodedep(mp, inodedep);
10276 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10277 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10278 ("handle_workitem_remove: worklist not empty. %s",
10279 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
10280 WORKITEM_FREE(dirrem, D_DIRREM);
10281 FREE_LOCK(ump);
10282 goto out;
10283 }
10284 /*
10285 * Directory deletion. Decrement reference count for both the
10286 * just deleted parent directory entry and the reference for ".".
10287 * Arrange to have the reference count on the parent decremented
10288 * to account for the loss of "..".
10289 */
10290 ip->i_nlink -= 2;
10291 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
10292 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
10293 DIP_SET(ip, i_nlink, ip->i_nlink);
10294 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10295 if (ip->i_nlink < ip->i_effnlink)
10296 panic("handle_workitem_remove: bad dir delta");
10297 if (ip->i_nlink == 0)
10298 unlinked_inodedep(mp, inodedep);
10299 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10300 /*
10301 * Rename a directory to a new parent. Since, we are both deleting
10302 * and creating a new directory entry, the link count on the new
10303 * directory should not change. Thus we skip the followup dirrem.
10304 */
10305 if (dirrem->dm_state & DIRCHG) {
10306 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10307 ("handle_workitem_remove: DIRCHG and worklist not empty."));
10308 WORKITEM_FREE(dirrem, D_DIRREM);
10309 FREE_LOCK(ump);
10310 goto out;
10311 }
10312 dirrem->dm_state = ONDEPLIST;
10313 dirrem->dm_oldinum = dirrem->dm_dirinum;
10314 /*
10315 * Place the dirrem on the parent's diremhd list.
10316 */
10317 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
10318 panic("handle_workitem_remove: lost dir inodedep");
10319 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
10320 /*
10321 * If the allocated inode has never been written to disk, then
10322 * the on-disk inode is zero'ed and we can remove the file
10323 * immediately. When journaling if the inode has been marked
10324 * unlinked and not DEPCOMPLETE we know it can never be written.
10325 */
10326 inodedep_lookup(mp, oldinum, 0, &inodedep);
10327 if (inodedep == NULL ||
10328 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
10329 check_inode_unwritten(inodedep)) {
10330 FREE_LOCK(ump);
10331 vput(vp);
10332 return handle_workitem_remove(dirrem, flags);
10333 }
10334 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
10335 FREE_LOCK(ump);
10336 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10337 out:
10338 ffs_update(vp, 0);
10339 vput(vp);
10340 return (0);
10341 }
10342
10343 /*
10344 * Inode de-allocation dependencies.
10345 *
10346 * When an inode's link count is reduced to zero, it can be de-allocated. We
10347 * found it convenient to postpone de-allocation until after the inode is
10348 * written to disk with its new link count (zero). At this point, all of the
10349 * on-disk inode's block pointers are nullified and, with careful dependency
10350 * list ordering, all dependencies related to the inode will be satisfied and
10351 * the corresponding dependency structures de-allocated. So, if/when the
10352 * inode is reused, there will be no mixing of old dependencies with new
10353 * ones. This artificial dependency is set up by the block de-allocation
10354 * procedure above (softdep_setup_freeblocks) and completed by the
10355 * following procedure.
10356 */
10357 static void
handle_workitem_freefile(freefile)10358 handle_workitem_freefile(freefile)
10359 struct freefile *freefile;
10360 {
10361 struct workhead wkhd;
10362 struct fs *fs;
10363 struct ufsmount *ump;
10364 int error;
10365 #ifdef INVARIANTS
10366 struct inodedep *idp;
10367 #endif
10368
10369 ump = VFSTOUFS(freefile->fx_list.wk_mp);
10370 fs = ump->um_fs;
10371 #ifdef INVARIANTS
10372 ACQUIRE_LOCK(ump);
10373 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10374 FREE_LOCK(ump);
10375 if (error)
10376 panic("handle_workitem_freefile: inodedep %p survived", idp);
10377 #endif
10378 UFS_LOCK(ump);
10379 fs->fs_pendinginodes -= 1;
10380 UFS_UNLOCK(ump);
10381 LIST_INIT(&wkhd);
10382 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10383 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10384 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10385 softdep_error("handle_workitem_freefile", error);
10386 ACQUIRE_LOCK(ump);
10387 WORKITEM_FREE(freefile, D_FREEFILE);
10388 FREE_LOCK(ump);
10389 }
10390
10391 /*
10392 * Helper function which unlinks marker element from work list and returns
10393 * the next element on the list.
10394 */
10395 static __inline struct worklist *
markernext(struct worklist * marker)10396 markernext(struct worklist *marker)
10397 {
10398 struct worklist *next;
10399
10400 next = LIST_NEXT(marker, wk_list);
10401 LIST_REMOVE(marker, wk_list);
10402 return next;
10403 }
10404
10405 /*
10406 * Disk writes.
10407 *
10408 * The dependency structures constructed above are most actively used when file
10409 * system blocks are written to disk. No constraints are placed on when a
10410 * block can be written, but unsatisfied update dependencies are made safe by
10411 * modifying (or replacing) the source memory for the duration of the disk
10412 * write. When the disk write completes, the memory block is again brought
10413 * up-to-date.
10414 *
10415 * In-core inode structure reclamation.
10416 *
10417 * Because there are a finite number of "in-core" inode structures, they are
10418 * reused regularly. By transferring all inode-related dependencies to the
10419 * in-memory inode block and indexing them separately (via "inodedep"s), we
10420 * can allow "in-core" inode structures to be reused at any time and avoid
10421 * any increase in contention.
10422 *
10423 * Called just before entering the device driver to initiate a new disk I/O.
10424 * The buffer must be locked, thus, no I/O completion operations can occur
10425 * while we are manipulating its associated dependencies.
10426 */
10427 static void
softdep_disk_io_initiation(bp)10428 softdep_disk_io_initiation(bp)
10429 struct buf *bp; /* structure describing disk write to occur */
10430 {
10431 struct worklist *wk;
10432 struct worklist marker;
10433 struct inodedep *inodedep;
10434 struct freeblks *freeblks;
10435 struct jblkdep *jblkdep;
10436 struct newblk *newblk;
10437 struct ufsmount *ump;
10438
10439 /*
10440 * We only care about write operations. There should never
10441 * be dependencies for reads.
10442 */
10443 if (bp->b_iocmd != BIO_WRITE)
10444 panic("softdep_disk_io_initiation: not write");
10445
10446 if (bp->b_vflags & BV_BKGRDINPROG)
10447 panic("softdep_disk_io_initiation: Writing buffer with "
10448 "background write in progress: %p", bp);
10449
10450 ump = softdep_bp_to_mp(bp);
10451 if (ump == NULL)
10452 return;
10453
10454 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10455 PHOLD(curproc); /* Don't swap out kernel stack */
10456 ACQUIRE_LOCK(ump);
10457 /*
10458 * Do any necessary pre-I/O processing.
10459 */
10460 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10461 wk = markernext(&marker)) {
10462 LIST_INSERT_AFTER(wk, &marker, wk_list);
10463 switch (wk->wk_type) {
10464 case D_PAGEDEP:
10465 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10466 continue;
10467
10468 case D_INODEDEP:
10469 inodedep = WK_INODEDEP(wk);
10470 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10471 initiate_write_inodeblock_ufs1(inodedep, bp);
10472 else
10473 initiate_write_inodeblock_ufs2(inodedep, bp);
10474 continue;
10475
10476 case D_INDIRDEP:
10477 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10478 continue;
10479
10480 case D_BMSAFEMAP:
10481 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10482 continue;
10483
10484 case D_JSEG:
10485 WK_JSEG(wk)->js_buf = NULL;
10486 continue;
10487
10488 case D_FREEBLKS:
10489 freeblks = WK_FREEBLKS(wk);
10490 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10491 /*
10492 * We have to wait for the freeblks to be journaled
10493 * before we can write an inodeblock with updated
10494 * pointers. Be careful to arrange the marker so
10495 * we revisit the freeblks if it's not removed by
10496 * the first jwait().
10497 */
10498 if (jblkdep != NULL) {
10499 LIST_REMOVE(&marker, wk_list);
10500 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10501 jwait(&jblkdep->jb_list, MNT_WAIT);
10502 }
10503 continue;
10504 case D_ALLOCDIRECT:
10505 case D_ALLOCINDIR:
10506 /*
10507 * We have to wait for the jnewblk to be journaled
10508 * before we can write to a block if the contents
10509 * may be confused with an earlier file's indirect
10510 * at recovery time. Handle the marker as described
10511 * above.
10512 */
10513 newblk = WK_NEWBLK(wk);
10514 if (newblk->nb_jnewblk != NULL &&
10515 indirblk_lookup(newblk->nb_list.wk_mp,
10516 newblk->nb_newblkno)) {
10517 LIST_REMOVE(&marker, wk_list);
10518 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10519 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10520 }
10521 continue;
10522
10523 case D_SBDEP:
10524 initiate_write_sbdep(WK_SBDEP(wk));
10525 continue;
10526
10527 case D_MKDIR:
10528 case D_FREEWORK:
10529 case D_FREEDEP:
10530 case D_JSEGDEP:
10531 continue;
10532
10533 default:
10534 panic("handle_disk_io_initiation: Unexpected type %s",
10535 TYPENAME(wk->wk_type));
10536 /* NOTREACHED */
10537 }
10538 }
10539 FREE_LOCK(ump);
10540 PRELE(curproc); /* Allow swapout of kernel stack */
10541 }
10542
10543 /*
10544 * Called from within the procedure above to deal with unsatisfied
10545 * allocation dependencies in a directory. The buffer must be locked,
10546 * thus, no I/O completion operations can occur while we are
10547 * manipulating its associated dependencies.
10548 */
10549 static void
initiate_write_filepage(pagedep,bp)10550 initiate_write_filepage(pagedep, bp)
10551 struct pagedep *pagedep;
10552 struct buf *bp;
10553 {
10554 struct jremref *jremref;
10555 struct jmvref *jmvref;
10556 struct dirrem *dirrem;
10557 struct diradd *dap;
10558 struct direct *ep;
10559 int i;
10560
10561 if (pagedep->pd_state & IOSTARTED) {
10562 /*
10563 * This can only happen if there is a driver that does not
10564 * understand chaining. Here biodone will reissue the call
10565 * to strategy for the incomplete buffers.
10566 */
10567 printf("initiate_write_filepage: already started\n");
10568 return;
10569 }
10570 pagedep->pd_state |= IOSTARTED;
10571 /*
10572 * Wait for all journal remove dependencies to hit the disk.
10573 * We can not allow any potentially conflicting directory adds
10574 * to be visible before removes and rollback is too difficult.
10575 * The per-filesystem lock may be dropped and re-acquired, however
10576 * we hold the buf locked so the dependency can not go away.
10577 */
10578 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10579 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10580 jwait(&jremref->jr_list, MNT_WAIT);
10581 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10582 jwait(&jmvref->jm_list, MNT_WAIT);
10583 for (i = 0; i < DAHASHSZ; i++) {
10584 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10585 ep = (struct direct *)
10586 ((char *)bp->b_data + dap->da_offset);
10587 if (ep->d_ino != dap->da_newinum)
10588 panic("%s: dir inum %ju != new %ju",
10589 "initiate_write_filepage",
10590 (uintmax_t)ep->d_ino,
10591 (uintmax_t)dap->da_newinum);
10592 if (dap->da_state & DIRCHG)
10593 ep->d_ino = dap->da_previous->dm_oldinum;
10594 else
10595 ep->d_ino = 0;
10596 dap->da_state &= ~ATTACHED;
10597 dap->da_state |= UNDONE;
10598 }
10599 }
10600 }
10601
10602 /*
10603 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10604 * Note that any bug fixes made to this routine must be done in the
10605 * version found below.
10606 *
10607 * Called from within the procedure above to deal with unsatisfied
10608 * allocation dependencies in an inodeblock. The buffer must be
10609 * locked, thus, no I/O completion operations can occur while we
10610 * are manipulating its associated dependencies.
10611 */
10612 static void
initiate_write_inodeblock_ufs1(inodedep,bp)10613 initiate_write_inodeblock_ufs1(inodedep, bp)
10614 struct inodedep *inodedep;
10615 struct buf *bp; /* The inode block */
10616 {
10617 struct allocdirect *adp, *lastadp;
10618 struct ufs1_dinode *dp;
10619 struct ufs1_dinode *sip;
10620 struct inoref *inoref;
10621 struct ufsmount *ump;
10622 struct fs *fs;
10623 ufs_lbn_t i;
10624 #ifdef INVARIANTS
10625 ufs_lbn_t prevlbn = 0;
10626 #endif
10627 int deplist __diagused;
10628
10629 if (inodedep->id_state & IOSTARTED)
10630 panic("initiate_write_inodeblock_ufs1: already started");
10631 inodedep->id_state |= IOSTARTED;
10632 fs = inodedep->id_fs;
10633 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10634 LOCK_OWNED(ump);
10635 dp = (struct ufs1_dinode *)bp->b_data +
10636 ino_to_fsbo(fs, inodedep->id_ino);
10637
10638 /*
10639 * If we're on the unlinked list but have not yet written our
10640 * next pointer initialize it here.
10641 */
10642 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10643 struct inodedep *inon;
10644
10645 inon = TAILQ_NEXT(inodedep, id_unlinked);
10646 dp->di_freelink = inon ? inon->id_ino : 0;
10647 }
10648 /*
10649 * If the bitmap is not yet written, then the allocated
10650 * inode cannot be written to disk.
10651 */
10652 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10653 if (inodedep->id_savedino1 != NULL)
10654 panic("initiate_write_inodeblock_ufs1: I/O underway");
10655 FREE_LOCK(ump);
10656 sip = malloc(sizeof(struct ufs1_dinode),
10657 M_SAVEDINO, M_SOFTDEP_FLAGS);
10658 ACQUIRE_LOCK(ump);
10659 inodedep->id_savedino1 = sip;
10660 *inodedep->id_savedino1 = *dp;
10661 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10662 dp->di_gen = inodedep->id_savedino1->di_gen;
10663 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10664 return;
10665 }
10666 /*
10667 * If no dependencies, then there is nothing to roll back.
10668 */
10669 inodedep->id_savedsize = dp->di_size;
10670 inodedep->id_savedextsize = 0;
10671 inodedep->id_savednlink = dp->di_nlink;
10672 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10673 TAILQ_EMPTY(&inodedep->id_inoreflst))
10674 return;
10675 /*
10676 * Revert the link count to that of the first unwritten journal entry.
10677 */
10678 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10679 if (inoref)
10680 dp->di_nlink = inoref->if_nlink;
10681 /*
10682 * Set the dependencies to busy.
10683 */
10684 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10685 adp = TAILQ_NEXT(adp, ad_next)) {
10686 #ifdef INVARIANTS
10687 if (deplist != 0 && prevlbn >= adp->ad_offset)
10688 panic("softdep_write_inodeblock: lbn order");
10689 prevlbn = adp->ad_offset;
10690 if (adp->ad_offset < UFS_NDADDR &&
10691 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10692 panic("initiate_write_inodeblock_ufs1: "
10693 "direct pointer #%jd mismatch %d != %jd",
10694 (intmax_t)adp->ad_offset,
10695 dp->di_db[adp->ad_offset],
10696 (intmax_t)adp->ad_newblkno);
10697 if (adp->ad_offset >= UFS_NDADDR &&
10698 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10699 panic("initiate_write_inodeblock_ufs1: "
10700 "indirect pointer #%jd mismatch %d != %jd",
10701 (intmax_t)adp->ad_offset - UFS_NDADDR,
10702 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10703 (intmax_t)adp->ad_newblkno);
10704 deplist |= 1 << adp->ad_offset;
10705 if ((adp->ad_state & ATTACHED) == 0)
10706 panic("initiate_write_inodeblock_ufs1: "
10707 "Unknown state 0x%x", adp->ad_state);
10708 #endif /* INVARIANTS */
10709 adp->ad_state &= ~ATTACHED;
10710 adp->ad_state |= UNDONE;
10711 }
10712 /*
10713 * The on-disk inode cannot claim to be any larger than the last
10714 * fragment that has been written. Otherwise, the on-disk inode
10715 * might have fragments that were not the last block in the file
10716 * which would corrupt the filesystem.
10717 */
10718 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10719 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10720 if (adp->ad_offset >= UFS_NDADDR)
10721 break;
10722 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10723 /* keep going until hitting a rollback to a frag */
10724 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10725 continue;
10726 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10727 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10728 #ifdef INVARIANTS
10729 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10730 panic("initiate_write_inodeblock_ufs1: "
10731 "lost dep1");
10732 #endif /* INVARIANTS */
10733 dp->di_db[i] = 0;
10734 }
10735 for (i = 0; i < UFS_NIADDR; i++) {
10736 #ifdef INVARIANTS
10737 if (dp->di_ib[i] != 0 &&
10738 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10739 panic("initiate_write_inodeblock_ufs1: "
10740 "lost dep2");
10741 #endif /* INVARIANTS */
10742 dp->di_ib[i] = 0;
10743 }
10744 return;
10745 }
10746 /*
10747 * If we have zero'ed out the last allocated block of the file,
10748 * roll back the size to the last currently allocated block.
10749 * We know that this last allocated block is a full-sized as
10750 * we already checked for fragments in the loop above.
10751 */
10752 if (lastadp != NULL &&
10753 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10754 for (i = lastadp->ad_offset; i >= 0; i--)
10755 if (dp->di_db[i] != 0)
10756 break;
10757 dp->di_size = (i + 1) * fs->fs_bsize;
10758 }
10759 /*
10760 * The only dependencies are for indirect blocks.
10761 *
10762 * The file size for indirect block additions is not guaranteed.
10763 * Such a guarantee would be non-trivial to achieve. The conventional
10764 * synchronous write implementation also does not make this guarantee.
10765 * Fsck should catch and fix discrepancies. Arguably, the file size
10766 * can be over-estimated without destroying integrity when the file
10767 * moves into the indirect blocks (i.e., is large). If we want to
10768 * postpone fsck, we are stuck with this argument.
10769 */
10770 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10771 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10772 }
10773
10774 /*
10775 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10776 * Note that any bug fixes made to this routine must be done in the
10777 * version found above.
10778 *
10779 * Called from within the procedure above to deal with unsatisfied
10780 * allocation dependencies in an inodeblock. The buffer must be
10781 * locked, thus, no I/O completion operations can occur while we
10782 * are manipulating its associated dependencies.
10783 */
10784 static void
initiate_write_inodeblock_ufs2(inodedep,bp)10785 initiate_write_inodeblock_ufs2(inodedep, bp)
10786 struct inodedep *inodedep;
10787 struct buf *bp; /* The inode block */
10788 {
10789 struct allocdirect *adp, *lastadp;
10790 struct ufs2_dinode *dp;
10791 struct ufs2_dinode *sip;
10792 struct inoref *inoref;
10793 struct ufsmount *ump;
10794 struct fs *fs;
10795 ufs_lbn_t i;
10796 #ifdef INVARIANTS
10797 ufs_lbn_t prevlbn = 0;
10798 #endif
10799 int deplist __diagused;
10800
10801 if (inodedep->id_state & IOSTARTED)
10802 panic("initiate_write_inodeblock_ufs2: already started");
10803 inodedep->id_state |= IOSTARTED;
10804 fs = inodedep->id_fs;
10805 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10806 LOCK_OWNED(ump);
10807 dp = (struct ufs2_dinode *)bp->b_data +
10808 ino_to_fsbo(fs, inodedep->id_ino);
10809
10810 /*
10811 * If we're on the unlinked list but have not yet written our
10812 * next pointer initialize it here.
10813 */
10814 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10815 struct inodedep *inon;
10816
10817 inon = TAILQ_NEXT(inodedep, id_unlinked);
10818 dp->di_freelink = inon ? inon->id_ino : 0;
10819 ffs_update_dinode_ckhash(fs, dp);
10820 }
10821 /*
10822 * If the bitmap is not yet written, then the allocated
10823 * inode cannot be written to disk.
10824 */
10825 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10826 if (inodedep->id_savedino2 != NULL)
10827 panic("initiate_write_inodeblock_ufs2: I/O underway");
10828 FREE_LOCK(ump);
10829 sip = malloc(sizeof(struct ufs2_dinode),
10830 M_SAVEDINO, M_SOFTDEP_FLAGS);
10831 ACQUIRE_LOCK(ump);
10832 inodedep->id_savedino2 = sip;
10833 *inodedep->id_savedino2 = *dp;
10834 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10835 dp->di_gen = inodedep->id_savedino2->di_gen;
10836 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10837 return;
10838 }
10839 /*
10840 * If no dependencies, then there is nothing to roll back.
10841 */
10842 inodedep->id_savedsize = dp->di_size;
10843 inodedep->id_savedextsize = dp->di_extsize;
10844 inodedep->id_savednlink = dp->di_nlink;
10845 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10846 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10847 TAILQ_EMPTY(&inodedep->id_inoreflst))
10848 return;
10849 /*
10850 * Revert the link count to that of the first unwritten journal entry.
10851 */
10852 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10853 if (inoref)
10854 dp->di_nlink = inoref->if_nlink;
10855
10856 /*
10857 * Set the ext data dependencies to busy.
10858 */
10859 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10860 adp = TAILQ_NEXT(adp, ad_next)) {
10861 #ifdef INVARIANTS
10862 if (deplist != 0 && prevlbn >= adp->ad_offset)
10863 panic("initiate_write_inodeblock_ufs2: lbn order");
10864 prevlbn = adp->ad_offset;
10865 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10866 panic("initiate_write_inodeblock_ufs2: "
10867 "ext pointer #%jd mismatch %jd != %jd",
10868 (intmax_t)adp->ad_offset,
10869 (intmax_t)dp->di_extb[adp->ad_offset],
10870 (intmax_t)adp->ad_newblkno);
10871 deplist |= 1 << adp->ad_offset;
10872 if ((adp->ad_state & ATTACHED) == 0)
10873 panic("initiate_write_inodeblock_ufs2: Unknown "
10874 "state 0x%x", adp->ad_state);
10875 #endif /* INVARIANTS */
10876 adp->ad_state &= ~ATTACHED;
10877 adp->ad_state |= UNDONE;
10878 }
10879 /*
10880 * The on-disk inode cannot claim to be any larger than the last
10881 * fragment that has been written. Otherwise, the on-disk inode
10882 * might have fragments that were not the last block in the ext
10883 * data which would corrupt the filesystem.
10884 */
10885 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10886 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10887 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10888 /* keep going until hitting a rollback to a frag */
10889 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10890 continue;
10891 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10892 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10893 #ifdef INVARIANTS
10894 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10895 panic("initiate_write_inodeblock_ufs2: "
10896 "lost dep1");
10897 #endif /* INVARIANTS */
10898 dp->di_extb[i] = 0;
10899 }
10900 lastadp = NULL;
10901 break;
10902 }
10903 /*
10904 * If we have zero'ed out the last allocated block of the ext
10905 * data, roll back the size to the last currently allocated block.
10906 * We know that this last allocated block is a full-sized as
10907 * we already checked for fragments in the loop above.
10908 */
10909 if (lastadp != NULL &&
10910 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10911 for (i = lastadp->ad_offset; i >= 0; i--)
10912 if (dp->di_extb[i] != 0)
10913 break;
10914 dp->di_extsize = (i + 1) * fs->fs_bsize;
10915 }
10916 /*
10917 * Set the file data dependencies to busy.
10918 */
10919 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10920 adp = TAILQ_NEXT(adp, ad_next)) {
10921 #ifdef INVARIANTS
10922 if (deplist != 0 && prevlbn >= adp->ad_offset)
10923 panic("softdep_write_inodeblock: lbn order");
10924 if ((adp->ad_state & ATTACHED) == 0)
10925 panic("inodedep %p and adp %p not attached", inodedep, adp);
10926 prevlbn = adp->ad_offset;
10927 if (!ffs_fsfail_cleanup(ump, 0) &&
10928 adp->ad_offset < UFS_NDADDR &&
10929 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10930 panic("initiate_write_inodeblock_ufs2: "
10931 "direct pointer #%jd mismatch %jd != %jd",
10932 (intmax_t)adp->ad_offset,
10933 (intmax_t)dp->di_db[adp->ad_offset],
10934 (intmax_t)adp->ad_newblkno);
10935 if (!ffs_fsfail_cleanup(ump, 0) &&
10936 adp->ad_offset >= UFS_NDADDR &&
10937 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10938 panic("initiate_write_inodeblock_ufs2: "
10939 "indirect pointer #%jd mismatch %jd != %jd",
10940 (intmax_t)adp->ad_offset - UFS_NDADDR,
10941 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10942 (intmax_t)adp->ad_newblkno);
10943 deplist |= 1 << adp->ad_offset;
10944 if ((adp->ad_state & ATTACHED) == 0)
10945 panic("initiate_write_inodeblock_ufs2: Unknown "
10946 "state 0x%x", adp->ad_state);
10947 #endif /* INVARIANTS */
10948 adp->ad_state &= ~ATTACHED;
10949 adp->ad_state |= UNDONE;
10950 }
10951 /*
10952 * The on-disk inode cannot claim to be any larger than the last
10953 * fragment that has been written. Otherwise, the on-disk inode
10954 * might have fragments that were not the last block in the file
10955 * which would corrupt the filesystem.
10956 */
10957 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10958 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10959 if (adp->ad_offset >= UFS_NDADDR)
10960 break;
10961 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10962 /* keep going until hitting a rollback to a frag */
10963 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10964 continue;
10965 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10966 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10967 #ifdef INVARIANTS
10968 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10969 panic("initiate_write_inodeblock_ufs2: "
10970 "lost dep2");
10971 #endif /* INVARIANTS */
10972 dp->di_db[i] = 0;
10973 }
10974 for (i = 0; i < UFS_NIADDR; i++) {
10975 #ifdef INVARIANTS
10976 if (dp->di_ib[i] != 0 &&
10977 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10978 panic("initiate_write_inodeblock_ufs2: "
10979 "lost dep3");
10980 #endif /* INVARIANTS */
10981 dp->di_ib[i] = 0;
10982 }
10983 ffs_update_dinode_ckhash(fs, dp);
10984 return;
10985 }
10986 /*
10987 * If we have zero'ed out the last allocated block of the file,
10988 * roll back the size to the last currently allocated block.
10989 * We know that this last allocated block is a full-sized as
10990 * we already checked for fragments in the loop above.
10991 */
10992 if (lastadp != NULL &&
10993 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10994 for (i = lastadp->ad_offset; i >= 0; i--)
10995 if (dp->di_db[i] != 0)
10996 break;
10997 dp->di_size = (i + 1) * fs->fs_bsize;
10998 }
10999 /*
11000 * The only dependencies are for indirect blocks.
11001 *
11002 * The file size for indirect block additions is not guaranteed.
11003 * Such a guarantee would be non-trivial to achieve. The conventional
11004 * synchronous write implementation also does not make this guarantee.
11005 * Fsck should catch and fix discrepancies. Arguably, the file size
11006 * can be over-estimated without destroying integrity when the file
11007 * moves into the indirect blocks (i.e., is large). If we want to
11008 * postpone fsck, we are stuck with this argument.
11009 */
11010 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
11011 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
11012 ffs_update_dinode_ckhash(fs, dp);
11013 }
11014
11015 /*
11016 * Cancel an indirdep as a result of truncation. Release all of the
11017 * children allocindirs and place their journal work on the appropriate
11018 * list.
11019 */
11020 static void
cancel_indirdep(indirdep,bp,freeblks)11021 cancel_indirdep(indirdep, bp, freeblks)
11022 struct indirdep *indirdep;
11023 struct buf *bp;
11024 struct freeblks *freeblks;
11025 {
11026 struct allocindir *aip;
11027
11028 /*
11029 * None of the indirect pointers will ever be visible,
11030 * so they can simply be tossed. GOINGAWAY ensures
11031 * that allocated pointers will be saved in the buffer
11032 * cache until they are freed. Note that they will
11033 * only be able to be found by their physical address
11034 * since the inode mapping the logical address will
11035 * be gone. The save buffer used for the safe copy
11036 * was allocated in setup_allocindir_phase2 using
11037 * the physical address so it could be used for this
11038 * purpose. Hence we swap the safe copy with the real
11039 * copy, allowing the safe copy to be freed and holding
11040 * on to the real copy for later use in indir_trunc.
11041 */
11042 if (indirdep->ir_state & GOINGAWAY)
11043 panic("cancel_indirdep: already gone");
11044 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11045 indirdep->ir_state |= DEPCOMPLETE;
11046 LIST_REMOVE(indirdep, ir_next);
11047 }
11048 indirdep->ir_state |= GOINGAWAY;
11049 /*
11050 * Pass in bp for blocks still have journal writes
11051 * pending so we can cancel them on their own.
11052 */
11053 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
11054 cancel_allocindir(aip, bp, freeblks, 0);
11055 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
11056 cancel_allocindir(aip, NULL, freeblks, 0);
11057 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
11058 cancel_allocindir(aip, NULL, freeblks, 0);
11059 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
11060 cancel_allocindir(aip, NULL, freeblks, 0);
11061 /*
11062 * If there are pending partial truncations we need to keep the
11063 * old block copy around until they complete. This is because
11064 * the current b_data is not a perfect superset of the available
11065 * blocks.
11066 */
11067 if (TAILQ_EMPTY(&indirdep->ir_trunc))
11068 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
11069 else
11070 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
11071 WORKLIST_REMOVE(&indirdep->ir_list);
11072 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
11073 indirdep->ir_bp = NULL;
11074 indirdep->ir_freeblks = freeblks;
11075 }
11076
11077 /*
11078 * Free an indirdep once it no longer has new pointers to track.
11079 */
11080 static void
free_indirdep(indirdep)11081 free_indirdep(indirdep)
11082 struct indirdep *indirdep;
11083 {
11084
11085 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
11086 ("free_indirdep: Indir trunc list not empty."));
11087 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
11088 ("free_indirdep: Complete head not empty."));
11089 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
11090 ("free_indirdep: write head not empty."));
11091 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
11092 ("free_indirdep: done head not empty."));
11093 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
11094 ("free_indirdep: deplist head not empty."));
11095 KASSERT((indirdep->ir_state & DEPCOMPLETE),
11096 ("free_indirdep: %p still on newblk list.", indirdep));
11097 KASSERT(indirdep->ir_saveddata == NULL,
11098 ("free_indirdep: %p still has saved data.", indirdep));
11099 KASSERT(indirdep->ir_savebp == NULL,
11100 ("free_indirdep: %p still has savebp buffer.", indirdep));
11101 if (indirdep->ir_state & ONWORKLIST)
11102 WORKLIST_REMOVE(&indirdep->ir_list);
11103 WORKITEM_FREE(indirdep, D_INDIRDEP);
11104 }
11105
11106 /*
11107 * Called before a write to an indirdep. This routine is responsible for
11108 * rolling back pointers to a safe state which includes only those
11109 * allocindirs which have been completed.
11110 */
11111 static void
initiate_write_indirdep(indirdep,bp)11112 initiate_write_indirdep(indirdep, bp)
11113 struct indirdep *indirdep;
11114 struct buf *bp;
11115 {
11116 struct ufsmount *ump;
11117
11118 indirdep->ir_state |= IOSTARTED;
11119 if (indirdep->ir_state & GOINGAWAY)
11120 panic("disk_io_initiation: indirdep gone");
11121 /*
11122 * If there are no remaining dependencies, this will be writing
11123 * the real pointers.
11124 */
11125 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
11126 TAILQ_EMPTY(&indirdep->ir_trunc))
11127 return;
11128 /*
11129 * Replace up-to-date version with safe version.
11130 */
11131 if (indirdep->ir_saveddata == NULL) {
11132 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
11133 LOCK_OWNED(ump);
11134 FREE_LOCK(ump);
11135 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
11136 M_SOFTDEP_FLAGS);
11137 ACQUIRE_LOCK(ump);
11138 }
11139 indirdep->ir_state &= ~ATTACHED;
11140 indirdep->ir_state |= UNDONE;
11141 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
11142 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
11143 bp->b_bcount);
11144 }
11145
11146 /*
11147 * Called when an inode has been cleared in a cg bitmap. This finally
11148 * eliminates any canceled jaddrefs
11149 */
11150 void
softdep_setup_inofree(mp,bp,ino,wkhd)11151 softdep_setup_inofree(mp, bp, ino, wkhd)
11152 struct mount *mp;
11153 struct buf *bp;
11154 ino_t ino;
11155 struct workhead *wkhd;
11156 {
11157 struct worklist *wk, *wkn;
11158 struct inodedep *inodedep;
11159 struct ufsmount *ump;
11160 uint8_t *inosused;
11161 struct cg *cgp;
11162 struct fs *fs;
11163
11164 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
11165 ("softdep_setup_inofree called on non-softdep filesystem"));
11166 ump = VFSTOUFS(mp);
11167 ACQUIRE_LOCK(ump);
11168 if (!ffs_fsfail_cleanup(ump, 0)) {
11169 fs = ump->um_fs;
11170 cgp = (struct cg *)bp->b_data;
11171 inosused = cg_inosused(cgp);
11172 if (isset(inosused, ino % fs->fs_ipg))
11173 panic("softdep_setup_inofree: inode %ju not freed.",
11174 (uintmax_t)ino);
11175 }
11176 if (inodedep_lookup(mp, ino, 0, &inodedep))
11177 panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
11178 (uintmax_t)ino, inodedep);
11179 if (wkhd) {
11180 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
11181 if (wk->wk_type != D_JADDREF)
11182 continue;
11183 WORKLIST_REMOVE(wk);
11184 /*
11185 * We can free immediately even if the jaddref
11186 * isn't attached in a background write as now
11187 * the bitmaps are reconciled.
11188 */
11189 wk->wk_state |= COMPLETE | ATTACHED;
11190 free_jaddref(WK_JADDREF(wk));
11191 }
11192 jwork_move(&bp->b_dep, wkhd);
11193 }
11194 FREE_LOCK(ump);
11195 }
11196
11197 /*
11198 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
11199 * map. Any dependencies waiting for the write to clear are added to the
11200 * buf's list and any jnewblks that are being canceled are discarded
11201 * immediately.
11202 */
11203 void
softdep_setup_blkfree(mp,bp,blkno,frags,wkhd)11204 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
11205 struct mount *mp;
11206 struct buf *bp;
11207 ufs2_daddr_t blkno;
11208 int frags;
11209 struct workhead *wkhd;
11210 {
11211 struct bmsafemap *bmsafemap;
11212 struct jnewblk *jnewblk;
11213 struct ufsmount *ump;
11214 struct worklist *wk;
11215 struct fs *fs;
11216 #ifdef INVARIANTS
11217 uint8_t *blksfree;
11218 struct cg *cgp;
11219 ufs2_daddr_t jstart;
11220 ufs2_daddr_t jend;
11221 ufs2_daddr_t end;
11222 long bno;
11223 int i;
11224 #endif
11225
11226 CTR3(KTR_SUJ,
11227 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
11228 blkno, frags, wkhd);
11229
11230 ump = VFSTOUFS(mp);
11231 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
11232 ("softdep_setup_blkfree called on non-softdep filesystem"));
11233 ACQUIRE_LOCK(ump);
11234 /* Lookup the bmsafemap so we track when it is dirty. */
11235 fs = ump->um_fs;
11236 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11237 /*
11238 * Detach any jnewblks which have been canceled. They must linger
11239 * until the bitmap is cleared again by ffs_blkfree() to prevent
11240 * an unjournaled allocation from hitting the disk.
11241 */
11242 if (wkhd) {
11243 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11244 CTR2(KTR_SUJ,
11245 "softdep_setup_blkfree: blkno %jd wk type %d",
11246 blkno, wk->wk_type);
11247 WORKLIST_REMOVE(wk);
11248 if (wk->wk_type != D_JNEWBLK) {
11249 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
11250 continue;
11251 }
11252 jnewblk = WK_JNEWBLK(wk);
11253 KASSERT(jnewblk->jn_state & GOINGAWAY,
11254 ("softdep_setup_blkfree: jnewblk not canceled."));
11255 #ifdef INVARIANTS
11256 /*
11257 * Assert that this block is free in the bitmap
11258 * before we discard the jnewblk.
11259 */
11260 cgp = (struct cg *)bp->b_data;
11261 blksfree = cg_blksfree(cgp);
11262 bno = dtogd(fs, jnewblk->jn_blkno);
11263 for (i = jnewblk->jn_oldfrags;
11264 i < jnewblk->jn_frags; i++) {
11265 if (isset(blksfree, bno + i))
11266 continue;
11267 panic("softdep_setup_blkfree: not free");
11268 }
11269 #endif
11270 /*
11271 * Even if it's not attached we can free immediately
11272 * as the new bitmap is correct.
11273 */
11274 wk->wk_state |= COMPLETE | ATTACHED;
11275 free_jnewblk(jnewblk);
11276 }
11277 }
11278
11279 #ifdef INVARIANTS
11280 /*
11281 * Assert that we are not freeing a block which has an outstanding
11282 * allocation dependency.
11283 */
11284 fs = VFSTOUFS(mp)->um_fs;
11285 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11286 end = blkno + frags;
11287 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11288 /*
11289 * Don't match against blocks that will be freed when the
11290 * background write is done.
11291 */
11292 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
11293 (COMPLETE | DEPCOMPLETE))
11294 continue;
11295 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
11296 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
11297 if ((blkno >= jstart && blkno < jend) ||
11298 (end > jstart && end <= jend)) {
11299 printf("state 0x%X %jd - %d %d dep %p\n",
11300 jnewblk->jn_state, jnewblk->jn_blkno,
11301 jnewblk->jn_oldfrags, jnewblk->jn_frags,
11302 jnewblk->jn_dep);
11303 panic("softdep_setup_blkfree: "
11304 "%jd-%jd(%d) overlaps with %jd-%jd",
11305 blkno, end, frags, jstart, jend);
11306 }
11307 }
11308 #endif
11309 FREE_LOCK(ump);
11310 }
11311
11312 /*
11313 * Revert a block allocation when the journal record that describes it
11314 * is not yet written.
11315 */
11316 static int
jnewblk_rollback(jnewblk,fs,cgp,blksfree)11317 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
11318 struct jnewblk *jnewblk;
11319 struct fs *fs;
11320 struct cg *cgp;
11321 uint8_t *blksfree;
11322 {
11323 ufs1_daddr_t fragno;
11324 long cgbno, bbase;
11325 int frags, blk;
11326 int i;
11327
11328 frags = 0;
11329 cgbno = dtogd(fs, jnewblk->jn_blkno);
11330 /*
11331 * We have to test which frags need to be rolled back. We may
11332 * be operating on a stale copy when doing background writes.
11333 */
11334 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
11335 if (isclr(blksfree, cgbno + i))
11336 frags++;
11337 if (frags == 0)
11338 return (0);
11339 /*
11340 * This is mostly ffs_blkfree() sans some validation and
11341 * superblock updates.
11342 */
11343 if (frags == fs->fs_frag) {
11344 fragno = fragstoblks(fs, cgbno);
11345 ffs_setblock(fs, blksfree, fragno);
11346 ffs_clusteracct(fs, cgp, fragno, 1);
11347 cgp->cg_cs.cs_nbfree++;
11348 } else {
11349 cgbno += jnewblk->jn_oldfrags;
11350 bbase = cgbno - fragnum(fs, cgbno);
11351 /* Decrement the old frags. */
11352 blk = blkmap(fs, blksfree, bbase);
11353 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11354 /* Deallocate the fragment */
11355 for (i = 0; i < frags; i++)
11356 setbit(blksfree, cgbno + i);
11357 cgp->cg_cs.cs_nffree += frags;
11358 /* Add back in counts associated with the new frags */
11359 blk = blkmap(fs, blksfree, bbase);
11360 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11361 /* If a complete block has been reassembled, account for it. */
11362 fragno = fragstoblks(fs, bbase);
11363 if (ffs_isblock(fs, blksfree, fragno)) {
11364 cgp->cg_cs.cs_nffree -= fs->fs_frag;
11365 ffs_clusteracct(fs, cgp, fragno, 1);
11366 cgp->cg_cs.cs_nbfree++;
11367 }
11368 }
11369 stat_jnewblk++;
11370 jnewblk->jn_state &= ~ATTACHED;
11371 jnewblk->jn_state |= UNDONE;
11372
11373 return (frags);
11374 }
11375
11376 static void
initiate_write_bmsafemap(bmsafemap,bp)11377 initiate_write_bmsafemap(bmsafemap, bp)
11378 struct bmsafemap *bmsafemap;
11379 struct buf *bp; /* The cg block. */
11380 {
11381 struct jaddref *jaddref;
11382 struct jnewblk *jnewblk;
11383 uint8_t *inosused;
11384 uint8_t *blksfree;
11385 struct cg *cgp;
11386 struct fs *fs;
11387 ino_t ino;
11388
11389 /*
11390 * If this is a background write, we did this at the time that
11391 * the copy was made, so do not need to do it again.
11392 */
11393 if (bmsafemap->sm_state & IOSTARTED)
11394 return;
11395 bmsafemap->sm_state |= IOSTARTED;
11396 /*
11397 * Clear any inode allocations which are pending journal writes.
11398 */
11399 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11400 cgp = (struct cg *)bp->b_data;
11401 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11402 inosused = cg_inosused(cgp);
11403 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11404 ino = jaddref->ja_ino % fs->fs_ipg;
11405 if (isset(inosused, ino)) {
11406 if ((jaddref->ja_mode & IFMT) == IFDIR)
11407 cgp->cg_cs.cs_ndir--;
11408 cgp->cg_cs.cs_nifree++;
11409 clrbit(inosused, ino);
11410 jaddref->ja_state &= ~ATTACHED;
11411 jaddref->ja_state |= UNDONE;
11412 stat_jaddref++;
11413 } else
11414 panic("initiate_write_bmsafemap: inode %ju "
11415 "marked free", (uintmax_t)jaddref->ja_ino);
11416 }
11417 }
11418 /*
11419 * Clear any block allocations which are pending journal writes.
11420 */
11421 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11422 cgp = (struct cg *)bp->b_data;
11423 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11424 blksfree = cg_blksfree(cgp);
11425 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11426 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11427 continue;
11428 panic("initiate_write_bmsafemap: block %jd "
11429 "marked free", jnewblk->jn_blkno);
11430 }
11431 }
11432 /*
11433 * Move allocation lists to the written lists so they can be
11434 * cleared once the block write is complete.
11435 */
11436 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11437 inodedep, id_deps);
11438 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11439 newblk, nb_deps);
11440 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11441 wk_list);
11442 }
11443
11444 void
softdep_handle_error(struct buf * bp)11445 softdep_handle_error(struct buf *bp)
11446 {
11447 struct ufsmount *ump;
11448
11449 ump = softdep_bp_to_mp(bp);
11450 if (ump == NULL)
11451 return;
11452
11453 if (ffs_fsfail_cleanup(ump, bp->b_error)) {
11454 /*
11455 * No future writes will succeed, so the on-disk image is safe.
11456 * Pretend that this write succeeded so that the softdep state
11457 * will be cleaned up naturally.
11458 */
11459 bp->b_ioflags &= ~BIO_ERROR;
11460 bp->b_error = 0;
11461 }
11462 }
11463
11464 /*
11465 * This routine is called during the completion interrupt
11466 * service routine for a disk write (from the procedure called
11467 * by the device driver to inform the filesystem caches of
11468 * a request completion). It should be called early in this
11469 * procedure, before the block is made available to other
11470 * processes or other routines are called.
11471 *
11472 */
11473 static void
softdep_disk_write_complete(bp)11474 softdep_disk_write_complete(bp)
11475 struct buf *bp; /* describes the completed disk write */
11476 {
11477 struct worklist *wk;
11478 struct worklist *owk;
11479 struct ufsmount *ump;
11480 struct workhead reattach;
11481 struct freeblks *freeblks;
11482 struct buf *sbp;
11483
11484 ump = softdep_bp_to_mp(bp);
11485 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11486 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11487 "with outstanding dependencies for buffer %p", bp));
11488 if (ump == NULL)
11489 return;
11490 if ((bp->b_ioflags & BIO_ERROR) != 0)
11491 softdep_handle_error(bp);
11492 /*
11493 * If an error occurred while doing the write, then the data
11494 * has not hit the disk and the dependencies cannot be processed.
11495 * But we do have to go through and roll forward any dependencies
11496 * that were rolled back before the disk write.
11497 */
11498 sbp = NULL;
11499 ACQUIRE_LOCK(ump);
11500 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11501 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11502 switch (wk->wk_type) {
11503 case D_PAGEDEP:
11504 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11505 continue;
11506
11507 case D_INODEDEP:
11508 handle_written_inodeblock(WK_INODEDEP(wk),
11509 bp, 0);
11510 continue;
11511
11512 case D_BMSAFEMAP:
11513 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11514 bp, 0);
11515 continue;
11516
11517 case D_INDIRDEP:
11518 handle_written_indirdep(WK_INDIRDEP(wk),
11519 bp, &sbp, 0);
11520 continue;
11521 default:
11522 /* nothing to roll forward */
11523 continue;
11524 }
11525 }
11526 FREE_LOCK(ump);
11527 if (sbp)
11528 brelse(sbp);
11529 return;
11530 }
11531 LIST_INIT(&reattach);
11532
11533 /*
11534 * Ump SU lock must not be released anywhere in this code segment.
11535 */
11536 owk = NULL;
11537 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11538 WORKLIST_REMOVE(wk);
11539 atomic_add_long(&dep_write[wk->wk_type], 1);
11540 if (wk == owk)
11541 panic("duplicate worklist: %p\n", wk);
11542 owk = wk;
11543 switch (wk->wk_type) {
11544 case D_PAGEDEP:
11545 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11546 WRITESUCCEEDED))
11547 WORKLIST_INSERT(&reattach, wk);
11548 continue;
11549
11550 case D_INODEDEP:
11551 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11552 WRITESUCCEEDED))
11553 WORKLIST_INSERT(&reattach, wk);
11554 continue;
11555
11556 case D_BMSAFEMAP:
11557 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11558 WRITESUCCEEDED))
11559 WORKLIST_INSERT(&reattach, wk);
11560 continue;
11561
11562 case D_MKDIR:
11563 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11564 continue;
11565
11566 case D_ALLOCDIRECT:
11567 wk->wk_state |= COMPLETE;
11568 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11569 continue;
11570
11571 case D_ALLOCINDIR:
11572 wk->wk_state |= COMPLETE;
11573 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11574 continue;
11575
11576 case D_INDIRDEP:
11577 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11578 WRITESUCCEEDED))
11579 WORKLIST_INSERT(&reattach, wk);
11580 continue;
11581
11582 case D_FREEBLKS:
11583 wk->wk_state |= COMPLETE;
11584 freeblks = WK_FREEBLKS(wk);
11585 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11586 LIST_EMPTY(&freeblks->fb_jblkdephd))
11587 add_to_worklist(wk, WK_NODELAY);
11588 continue;
11589
11590 case D_FREEWORK:
11591 handle_written_freework(WK_FREEWORK(wk));
11592 break;
11593
11594 case D_JSEGDEP:
11595 free_jsegdep(WK_JSEGDEP(wk));
11596 continue;
11597
11598 case D_JSEG:
11599 handle_written_jseg(WK_JSEG(wk), bp);
11600 continue;
11601
11602 case D_SBDEP:
11603 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11604 WORKLIST_INSERT(&reattach, wk);
11605 continue;
11606
11607 case D_FREEDEP:
11608 free_freedep(WK_FREEDEP(wk));
11609 continue;
11610
11611 default:
11612 panic("handle_disk_write_complete: Unknown type %s",
11613 TYPENAME(wk->wk_type));
11614 /* NOTREACHED */
11615 }
11616 }
11617 /*
11618 * Reattach any requests that must be redone.
11619 */
11620 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11621 WORKLIST_REMOVE(wk);
11622 WORKLIST_INSERT(&bp->b_dep, wk);
11623 }
11624 FREE_LOCK(ump);
11625 if (sbp)
11626 brelse(sbp);
11627 }
11628
11629 /*
11630 * Called from within softdep_disk_write_complete above.
11631 */
11632 static void
handle_allocdirect_partdone(adp,wkhd)11633 handle_allocdirect_partdone(adp, wkhd)
11634 struct allocdirect *adp; /* the completed allocdirect */
11635 struct workhead *wkhd; /* Work to do when inode is writtne. */
11636 {
11637 struct allocdirectlst *listhead;
11638 struct allocdirect *listadp;
11639 struct inodedep *inodedep;
11640 long bsize;
11641
11642 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11643 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11644 return;
11645 /*
11646 * The on-disk inode cannot claim to be any larger than the last
11647 * fragment that has been written. Otherwise, the on-disk inode
11648 * might have fragments that were not the last block in the file
11649 * which would corrupt the filesystem. Thus, we cannot free any
11650 * allocdirects after one whose ad_oldblkno claims a fragment as
11651 * these blocks must be rolled back to zero before writing the inode.
11652 * We check the currently active set of allocdirects in id_inoupdt
11653 * or id_extupdt as appropriate.
11654 */
11655 inodedep = adp->ad_inodedep;
11656 bsize = inodedep->id_fs->fs_bsize;
11657 if (adp->ad_state & EXTDATA)
11658 listhead = &inodedep->id_extupdt;
11659 else
11660 listhead = &inodedep->id_inoupdt;
11661 TAILQ_FOREACH(listadp, listhead, ad_next) {
11662 /* found our block */
11663 if (listadp == adp)
11664 break;
11665 /* continue if ad_oldlbn is not a fragment */
11666 if (listadp->ad_oldsize == 0 ||
11667 listadp->ad_oldsize == bsize)
11668 continue;
11669 /* hit a fragment */
11670 return;
11671 }
11672 /*
11673 * If we have reached the end of the current list without
11674 * finding the just finished dependency, then it must be
11675 * on the future dependency list. Future dependencies cannot
11676 * be freed until they are moved to the current list.
11677 */
11678 if (listadp == NULL) {
11679 #ifdef INVARIANTS
11680 if (adp->ad_state & EXTDATA)
11681 listhead = &inodedep->id_newextupdt;
11682 else
11683 listhead = &inodedep->id_newinoupdt;
11684 TAILQ_FOREACH(listadp, listhead, ad_next)
11685 /* found our block */
11686 if (listadp == adp)
11687 break;
11688 if (listadp == NULL)
11689 panic("handle_allocdirect_partdone: lost dep");
11690 #endif /* INVARIANTS */
11691 return;
11692 }
11693 /*
11694 * If we have found the just finished dependency, then queue
11695 * it along with anything that follows it that is complete.
11696 * Since the pointer has not yet been written in the inode
11697 * as the dependency prevents it, place the allocdirect on the
11698 * bufwait list where it will be freed once the pointer is
11699 * valid.
11700 */
11701 if (wkhd == NULL)
11702 wkhd = &inodedep->id_bufwait;
11703 for (; adp; adp = listadp) {
11704 listadp = TAILQ_NEXT(adp, ad_next);
11705 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11706 return;
11707 TAILQ_REMOVE(listhead, adp, ad_next);
11708 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11709 }
11710 }
11711
11712 /*
11713 * Called from within softdep_disk_write_complete above. This routine
11714 * completes successfully written allocindirs.
11715 */
11716 static void
handle_allocindir_partdone(aip)11717 handle_allocindir_partdone(aip)
11718 struct allocindir *aip; /* the completed allocindir */
11719 {
11720 struct indirdep *indirdep;
11721
11722 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11723 return;
11724 indirdep = aip->ai_indirdep;
11725 LIST_REMOVE(aip, ai_next);
11726 /*
11727 * Don't set a pointer while the buffer is undergoing IO or while
11728 * we have active truncations.
11729 */
11730 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11731 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11732 return;
11733 }
11734 if (indirdep->ir_state & UFS1FMT)
11735 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11736 aip->ai_newblkno;
11737 else
11738 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11739 aip->ai_newblkno;
11740 /*
11741 * Await the pointer write before freeing the allocindir.
11742 */
11743 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11744 }
11745
11746 /*
11747 * Release segments held on a jwork list.
11748 */
11749 static void
handle_jwork(wkhd)11750 handle_jwork(wkhd)
11751 struct workhead *wkhd;
11752 {
11753 struct worklist *wk;
11754
11755 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11756 WORKLIST_REMOVE(wk);
11757 switch (wk->wk_type) {
11758 case D_JSEGDEP:
11759 free_jsegdep(WK_JSEGDEP(wk));
11760 continue;
11761 case D_FREEDEP:
11762 free_freedep(WK_FREEDEP(wk));
11763 continue;
11764 case D_FREEFRAG:
11765 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11766 WORKITEM_FREE(wk, D_FREEFRAG);
11767 continue;
11768 case D_FREEWORK:
11769 handle_written_freework(WK_FREEWORK(wk));
11770 continue;
11771 default:
11772 panic("handle_jwork: Unknown type %s\n",
11773 TYPENAME(wk->wk_type));
11774 }
11775 }
11776 }
11777
11778 /*
11779 * Handle the bufwait list on an inode when it is safe to release items
11780 * held there. This normally happens after an inode block is written but
11781 * may be delayed and handled later if there are pending journal items that
11782 * are not yet safe to be released.
11783 */
11784 static struct freefile *
handle_bufwait(inodedep,refhd)11785 handle_bufwait(inodedep, refhd)
11786 struct inodedep *inodedep;
11787 struct workhead *refhd;
11788 {
11789 struct jaddref *jaddref;
11790 struct freefile *freefile;
11791 struct worklist *wk;
11792
11793 freefile = NULL;
11794 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11795 WORKLIST_REMOVE(wk);
11796 switch (wk->wk_type) {
11797 case D_FREEFILE:
11798 /*
11799 * We defer adding freefile to the worklist
11800 * until all other additions have been made to
11801 * ensure that it will be done after all the
11802 * old blocks have been freed.
11803 */
11804 if (freefile != NULL)
11805 panic("handle_bufwait: freefile");
11806 freefile = WK_FREEFILE(wk);
11807 continue;
11808
11809 case D_MKDIR:
11810 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11811 continue;
11812
11813 case D_DIRADD:
11814 diradd_inode_written(WK_DIRADD(wk), inodedep);
11815 continue;
11816
11817 case D_FREEFRAG:
11818 wk->wk_state |= COMPLETE;
11819 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11820 add_to_worklist(wk, 0);
11821 continue;
11822
11823 case D_DIRREM:
11824 wk->wk_state |= COMPLETE;
11825 add_to_worklist(wk, 0);
11826 continue;
11827
11828 case D_ALLOCDIRECT:
11829 case D_ALLOCINDIR:
11830 free_newblk(WK_NEWBLK(wk));
11831 continue;
11832
11833 case D_JNEWBLK:
11834 wk->wk_state |= COMPLETE;
11835 free_jnewblk(WK_JNEWBLK(wk));
11836 continue;
11837
11838 /*
11839 * Save freed journal segments and add references on
11840 * the supplied list which will delay their release
11841 * until the cg bitmap is cleared on disk.
11842 */
11843 case D_JSEGDEP:
11844 if (refhd == NULL)
11845 free_jsegdep(WK_JSEGDEP(wk));
11846 else
11847 WORKLIST_INSERT(refhd, wk);
11848 continue;
11849
11850 case D_JADDREF:
11851 jaddref = WK_JADDREF(wk);
11852 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11853 if_deps);
11854 /*
11855 * Transfer any jaddrefs to the list to be freed with
11856 * the bitmap if we're handling a removed file.
11857 */
11858 if (refhd == NULL) {
11859 wk->wk_state |= COMPLETE;
11860 free_jaddref(jaddref);
11861 } else
11862 WORKLIST_INSERT(refhd, wk);
11863 continue;
11864
11865 default:
11866 panic("handle_bufwait: Unknown type %p(%s)",
11867 wk, TYPENAME(wk->wk_type));
11868 /* NOTREACHED */
11869 }
11870 }
11871 return (freefile);
11872 }
11873 /*
11874 * Called from within softdep_disk_write_complete above to restore
11875 * in-memory inode block contents to their most up-to-date state. Note
11876 * that this routine is always called from interrupt level with further
11877 * interrupts from this device blocked.
11878 *
11879 * If the write did not succeed, we will do all the roll-forward
11880 * operations, but we will not take the actions that will allow its
11881 * dependencies to be processed.
11882 */
11883 static int
handle_written_inodeblock(inodedep,bp,flags)11884 handle_written_inodeblock(inodedep, bp, flags)
11885 struct inodedep *inodedep;
11886 struct buf *bp; /* buffer containing the inode block */
11887 int flags;
11888 {
11889 struct freefile *freefile;
11890 struct allocdirect *adp, *nextadp;
11891 struct ufs1_dinode *dp1 = NULL;
11892 struct ufs2_dinode *dp2 = NULL;
11893 struct workhead wkhd;
11894 int hadchanges, fstype;
11895 ino_t freelink;
11896
11897 LIST_INIT(&wkhd);
11898 hadchanges = 0;
11899 freefile = NULL;
11900 if ((inodedep->id_state & IOSTARTED) == 0)
11901 panic("handle_written_inodeblock: not started");
11902 inodedep->id_state &= ~IOSTARTED;
11903 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11904 fstype = UFS1;
11905 dp1 = (struct ufs1_dinode *)bp->b_data +
11906 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11907 freelink = dp1->di_freelink;
11908 } else {
11909 fstype = UFS2;
11910 dp2 = (struct ufs2_dinode *)bp->b_data +
11911 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11912 freelink = dp2->di_freelink;
11913 }
11914 /*
11915 * Leave this inodeblock dirty until it's in the list.
11916 */
11917 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11918 (flags & WRITESUCCEEDED)) {
11919 struct inodedep *inon;
11920
11921 inon = TAILQ_NEXT(inodedep, id_unlinked);
11922 if ((inon == NULL && freelink == 0) ||
11923 (inon && inon->id_ino == freelink)) {
11924 if (inon)
11925 inon->id_state |= UNLINKPREV;
11926 inodedep->id_state |= UNLINKNEXT;
11927 }
11928 hadchanges = 1;
11929 }
11930 /*
11931 * If we had to rollback the inode allocation because of
11932 * bitmaps being incomplete, then simply restore it.
11933 * Keep the block dirty so that it will not be reclaimed until
11934 * all associated dependencies have been cleared and the
11935 * corresponding updates written to disk.
11936 */
11937 if (inodedep->id_savedino1 != NULL) {
11938 hadchanges = 1;
11939 if (fstype == UFS1)
11940 *dp1 = *inodedep->id_savedino1;
11941 else
11942 *dp2 = *inodedep->id_savedino2;
11943 free(inodedep->id_savedino1, M_SAVEDINO);
11944 inodedep->id_savedino1 = NULL;
11945 if ((bp->b_flags & B_DELWRI) == 0)
11946 stat_inode_bitmap++;
11947 bdirty(bp);
11948 /*
11949 * If the inode is clear here and GOINGAWAY it will never
11950 * be written. Process the bufwait and clear any pending
11951 * work which may include the freefile.
11952 */
11953 if (inodedep->id_state & GOINGAWAY)
11954 goto bufwait;
11955 return (1);
11956 }
11957 if (flags & WRITESUCCEEDED)
11958 inodedep->id_state |= COMPLETE;
11959 /*
11960 * Roll forward anything that had to be rolled back before
11961 * the inode could be updated.
11962 */
11963 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11964 nextadp = TAILQ_NEXT(adp, ad_next);
11965 if (adp->ad_state & ATTACHED)
11966 panic("handle_written_inodeblock: new entry");
11967 if (fstype == UFS1) {
11968 if (adp->ad_offset < UFS_NDADDR) {
11969 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11970 panic("%s %s #%jd mismatch %d != %jd",
11971 "handle_written_inodeblock:",
11972 "direct pointer",
11973 (intmax_t)adp->ad_offset,
11974 dp1->di_db[adp->ad_offset],
11975 (intmax_t)adp->ad_oldblkno);
11976 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11977 } else {
11978 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11979 0)
11980 panic("%s: %s #%jd allocated as %d",
11981 "handle_written_inodeblock",
11982 "indirect pointer",
11983 (intmax_t)adp->ad_offset -
11984 UFS_NDADDR,
11985 dp1->di_ib[adp->ad_offset -
11986 UFS_NDADDR]);
11987 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11988 adp->ad_newblkno;
11989 }
11990 } else {
11991 if (adp->ad_offset < UFS_NDADDR) {
11992 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11993 panic("%s: %s #%jd %s %jd != %jd",
11994 "handle_written_inodeblock",
11995 "direct pointer",
11996 (intmax_t)adp->ad_offset, "mismatch",
11997 (intmax_t)dp2->di_db[adp->ad_offset],
11998 (intmax_t)adp->ad_oldblkno);
11999 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
12000 } else {
12001 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
12002 0)
12003 panic("%s: %s #%jd allocated as %jd",
12004 "handle_written_inodeblock",
12005 "indirect pointer",
12006 (intmax_t)adp->ad_offset -
12007 UFS_NDADDR,
12008 (intmax_t)
12009 dp2->di_ib[adp->ad_offset -
12010 UFS_NDADDR]);
12011 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
12012 adp->ad_newblkno;
12013 }
12014 }
12015 adp->ad_state &= ~UNDONE;
12016 adp->ad_state |= ATTACHED;
12017 hadchanges = 1;
12018 }
12019 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
12020 nextadp = TAILQ_NEXT(adp, ad_next);
12021 if (adp->ad_state & ATTACHED)
12022 panic("handle_written_inodeblock: new entry");
12023 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
12024 panic("%s: direct pointers #%jd %s %jd != %jd",
12025 "handle_written_inodeblock",
12026 (intmax_t)adp->ad_offset, "mismatch",
12027 (intmax_t)dp2->di_extb[adp->ad_offset],
12028 (intmax_t)adp->ad_oldblkno);
12029 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
12030 adp->ad_state &= ~UNDONE;
12031 adp->ad_state |= ATTACHED;
12032 hadchanges = 1;
12033 }
12034 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
12035 stat_direct_blk_ptrs++;
12036 /*
12037 * Reset the file size to its most up-to-date value.
12038 */
12039 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
12040 panic("handle_written_inodeblock: bad size");
12041 if (inodedep->id_savednlink > UFS_LINK_MAX)
12042 panic("handle_written_inodeblock: Invalid link count "
12043 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
12044 inodedep);
12045 if (fstype == UFS1) {
12046 if (dp1->di_nlink != inodedep->id_savednlink) {
12047 dp1->di_nlink = inodedep->id_savednlink;
12048 hadchanges = 1;
12049 }
12050 if (dp1->di_size != inodedep->id_savedsize) {
12051 dp1->di_size = inodedep->id_savedsize;
12052 hadchanges = 1;
12053 }
12054 } else {
12055 if (dp2->di_nlink != inodedep->id_savednlink) {
12056 dp2->di_nlink = inodedep->id_savednlink;
12057 hadchanges = 1;
12058 }
12059 if (dp2->di_size != inodedep->id_savedsize) {
12060 dp2->di_size = inodedep->id_savedsize;
12061 hadchanges = 1;
12062 }
12063 if (dp2->di_extsize != inodedep->id_savedextsize) {
12064 dp2->di_extsize = inodedep->id_savedextsize;
12065 hadchanges = 1;
12066 }
12067 }
12068 inodedep->id_savedsize = -1;
12069 inodedep->id_savedextsize = -1;
12070 inodedep->id_savednlink = -1;
12071 /*
12072 * If there were any rollbacks in the inode block, then it must be
12073 * marked dirty so that its will eventually get written back in
12074 * its correct form.
12075 */
12076 if (hadchanges) {
12077 if (fstype == UFS2)
12078 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
12079 bdirty(bp);
12080 }
12081 bufwait:
12082 /*
12083 * If the write did not succeed, we have done all the roll-forward
12084 * operations, but we cannot take the actions that will allow its
12085 * dependencies to be processed.
12086 */
12087 if ((flags & WRITESUCCEEDED) == 0)
12088 return (hadchanges);
12089 /*
12090 * Process any allocdirects that completed during the update.
12091 */
12092 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
12093 handle_allocdirect_partdone(adp, &wkhd);
12094 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
12095 handle_allocdirect_partdone(adp, &wkhd);
12096 /*
12097 * Process deallocations that were held pending until the
12098 * inode had been written to disk. Freeing of the inode
12099 * is delayed until after all blocks have been freed to
12100 * avoid creation of new <vfsid, inum, lbn> triples
12101 * before the old ones have been deleted. Completely
12102 * unlinked inodes are not processed until the unlinked
12103 * inode list is written or the last reference is removed.
12104 */
12105 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
12106 freefile = handle_bufwait(inodedep, NULL);
12107 if (freefile && !LIST_EMPTY(&wkhd)) {
12108 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
12109 freefile = NULL;
12110 }
12111 }
12112 /*
12113 * Move rolled forward dependency completions to the bufwait list
12114 * now that those that were already written have been processed.
12115 */
12116 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
12117 panic("handle_written_inodeblock: bufwait but no changes");
12118 jwork_move(&inodedep->id_bufwait, &wkhd);
12119
12120 if (freefile != NULL) {
12121 /*
12122 * If the inode is goingaway it was never written. Fake up
12123 * the state here so free_inodedep() can succeed.
12124 */
12125 if (inodedep->id_state & GOINGAWAY)
12126 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
12127 if (free_inodedep(inodedep) == 0)
12128 panic("handle_written_inodeblock: live inodedep %p",
12129 inodedep);
12130 add_to_worklist(&freefile->fx_list, 0);
12131 return (0);
12132 }
12133
12134 /*
12135 * If no outstanding dependencies, free it.
12136 */
12137 if (free_inodedep(inodedep) ||
12138 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
12139 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
12140 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
12141 LIST_FIRST(&inodedep->id_bufwait) == 0))
12142 return (0);
12143 return (hadchanges);
12144 }
12145
12146 /*
12147 * Perform needed roll-forwards and kick off any dependencies that
12148 * can now be processed.
12149 *
12150 * If the write did not succeed, we will do all the roll-forward
12151 * operations, but we will not take the actions that will allow its
12152 * dependencies to be processed.
12153 */
12154 static int
handle_written_indirdep(indirdep,bp,bpp,flags)12155 handle_written_indirdep(indirdep, bp, bpp, flags)
12156 struct indirdep *indirdep;
12157 struct buf *bp;
12158 struct buf **bpp;
12159 int flags;
12160 {
12161 struct allocindir *aip;
12162 struct buf *sbp;
12163 int chgs;
12164
12165 if (indirdep->ir_state & GOINGAWAY)
12166 panic("handle_written_indirdep: indirdep gone");
12167 if ((indirdep->ir_state & IOSTARTED) == 0)
12168 panic("handle_written_indirdep: IO not started");
12169 chgs = 0;
12170 /*
12171 * If there were rollbacks revert them here.
12172 */
12173 if (indirdep->ir_saveddata) {
12174 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
12175 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12176 free(indirdep->ir_saveddata, M_INDIRDEP);
12177 indirdep->ir_saveddata = NULL;
12178 }
12179 chgs = 1;
12180 }
12181 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
12182 indirdep->ir_state |= ATTACHED;
12183 /*
12184 * If the write did not succeed, we have done all the roll-forward
12185 * operations, but we cannot take the actions that will allow its
12186 * dependencies to be processed.
12187 */
12188 if ((flags & WRITESUCCEEDED) == 0) {
12189 stat_indir_blk_ptrs++;
12190 bdirty(bp);
12191 return (1);
12192 }
12193 /*
12194 * Move allocindirs with written pointers to the completehd if
12195 * the indirdep's pointer is not yet written. Otherwise
12196 * free them here.
12197 */
12198 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
12199 LIST_REMOVE(aip, ai_next);
12200 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
12201 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
12202 ai_next);
12203 newblk_freefrag(&aip->ai_block);
12204 continue;
12205 }
12206 free_newblk(&aip->ai_block);
12207 }
12208 /*
12209 * Move allocindirs that have finished dependency processing from
12210 * the done list to the write list after updating the pointers.
12211 */
12212 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12213 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
12214 handle_allocindir_partdone(aip);
12215 if (aip == LIST_FIRST(&indirdep->ir_donehd))
12216 panic("disk_write_complete: not gone");
12217 chgs = 1;
12218 }
12219 }
12220 /*
12221 * Preserve the indirdep if there were any changes or if it is not
12222 * yet valid on disk.
12223 */
12224 if (chgs) {
12225 stat_indir_blk_ptrs++;
12226 bdirty(bp);
12227 return (1);
12228 }
12229 /*
12230 * If there were no changes we can discard the savedbp and detach
12231 * ourselves from the buf. We are only carrying completed pointers
12232 * in this case.
12233 */
12234 sbp = indirdep->ir_savebp;
12235 sbp->b_flags |= B_INVAL | B_NOCACHE;
12236 indirdep->ir_savebp = NULL;
12237 indirdep->ir_bp = NULL;
12238 if (*bpp != NULL)
12239 panic("handle_written_indirdep: bp already exists.");
12240 *bpp = sbp;
12241 /*
12242 * The indirdep may not be freed until its parent points at it.
12243 */
12244 if (indirdep->ir_state & DEPCOMPLETE)
12245 free_indirdep(indirdep);
12246
12247 return (0);
12248 }
12249
12250 /*
12251 * Process a diradd entry after its dependent inode has been written.
12252 */
12253 static void
diradd_inode_written(dap,inodedep)12254 diradd_inode_written(dap, inodedep)
12255 struct diradd *dap;
12256 struct inodedep *inodedep;
12257 {
12258
12259 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
12260 dap->da_state |= COMPLETE;
12261 complete_diradd(dap);
12262 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
12263 }
12264
12265 /*
12266 * Returns true if the bmsafemap will have rollbacks when written. Must only
12267 * be called with the per-filesystem lock and the buf lock on the cg held.
12268 */
12269 static int
bmsafemap_backgroundwrite(bmsafemap,bp)12270 bmsafemap_backgroundwrite(bmsafemap, bp)
12271 struct bmsafemap *bmsafemap;
12272 struct buf *bp;
12273 {
12274 int dirty;
12275
12276 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
12277 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
12278 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
12279 /*
12280 * If we're initiating a background write we need to process the
12281 * rollbacks as they exist now, not as they exist when IO starts.
12282 * No other consumers will look at the contents of the shadowed
12283 * buf so this is safe to do here.
12284 */
12285 if (bp->b_xflags & BX_BKGRDMARKER)
12286 initiate_write_bmsafemap(bmsafemap, bp);
12287
12288 return (dirty);
12289 }
12290
12291 /*
12292 * Re-apply an allocation when a cg write is complete.
12293 */
12294 static int
jnewblk_rollforward(jnewblk,fs,cgp,blksfree)12295 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
12296 struct jnewblk *jnewblk;
12297 struct fs *fs;
12298 struct cg *cgp;
12299 uint8_t *blksfree;
12300 {
12301 ufs1_daddr_t fragno;
12302 ufs2_daddr_t blkno;
12303 long cgbno, bbase;
12304 int frags, blk;
12305 int i;
12306
12307 frags = 0;
12308 cgbno = dtogd(fs, jnewblk->jn_blkno);
12309 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
12310 if (isclr(blksfree, cgbno + i))
12311 panic("jnewblk_rollforward: re-allocated fragment");
12312 frags++;
12313 }
12314 if (frags == fs->fs_frag) {
12315 blkno = fragstoblks(fs, cgbno);
12316 ffs_clrblock(fs, blksfree, (long)blkno);
12317 ffs_clusteracct(fs, cgp, blkno, -1);
12318 cgp->cg_cs.cs_nbfree--;
12319 } else {
12320 bbase = cgbno - fragnum(fs, cgbno);
12321 cgbno += jnewblk->jn_oldfrags;
12322 /* If a complete block had been reassembled, account for it. */
12323 fragno = fragstoblks(fs, bbase);
12324 if (ffs_isblock(fs, blksfree, fragno)) {
12325 cgp->cg_cs.cs_nffree += fs->fs_frag;
12326 ffs_clusteracct(fs, cgp, fragno, -1);
12327 cgp->cg_cs.cs_nbfree--;
12328 }
12329 /* Decrement the old frags. */
12330 blk = blkmap(fs, blksfree, bbase);
12331 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12332 /* Allocate the fragment */
12333 for (i = 0; i < frags; i++)
12334 clrbit(blksfree, cgbno + i);
12335 cgp->cg_cs.cs_nffree -= frags;
12336 /* Add back in counts associated with the new frags */
12337 blk = blkmap(fs, blksfree, bbase);
12338 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12339 }
12340 return (frags);
12341 }
12342
12343 /*
12344 * Complete a write to a bmsafemap structure. Roll forward any bitmap
12345 * changes if it's not a background write. Set all written dependencies
12346 * to DEPCOMPLETE and free the structure if possible.
12347 *
12348 * If the write did not succeed, we will do all the roll-forward
12349 * operations, but we will not take the actions that will allow its
12350 * dependencies to be processed.
12351 */
12352 static int
handle_written_bmsafemap(bmsafemap,bp,flags)12353 handle_written_bmsafemap(bmsafemap, bp, flags)
12354 struct bmsafemap *bmsafemap;
12355 struct buf *bp;
12356 int flags;
12357 {
12358 struct newblk *newblk;
12359 struct inodedep *inodedep;
12360 struct jaddref *jaddref, *jatmp;
12361 struct jnewblk *jnewblk, *jntmp;
12362 struct ufsmount *ump;
12363 uint8_t *inosused;
12364 uint8_t *blksfree;
12365 struct cg *cgp;
12366 struct fs *fs;
12367 ino_t ino;
12368 int foreground;
12369 int chgs;
12370
12371 if ((bmsafemap->sm_state & IOSTARTED) == 0)
12372 panic("handle_written_bmsafemap: Not started\n");
12373 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
12374 chgs = 0;
12375 bmsafemap->sm_state &= ~IOSTARTED;
12376 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
12377 /*
12378 * If write was successful, release journal work that was waiting
12379 * on the write. Otherwise move the work back.
12380 */
12381 if (flags & WRITESUCCEEDED)
12382 handle_jwork(&bmsafemap->sm_freewr);
12383 else
12384 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12385 worklist, wk_list);
12386
12387 /*
12388 * Restore unwritten inode allocation pending jaddref writes.
12389 */
12390 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12391 cgp = (struct cg *)bp->b_data;
12392 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12393 inosused = cg_inosused(cgp);
12394 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12395 ja_bmdeps, jatmp) {
12396 if ((jaddref->ja_state & UNDONE) == 0)
12397 continue;
12398 ino = jaddref->ja_ino % fs->fs_ipg;
12399 if (isset(inosused, ino))
12400 panic("handle_written_bmsafemap: "
12401 "re-allocated inode");
12402 /* Do the roll-forward only if it's a real copy. */
12403 if (foreground) {
12404 if ((jaddref->ja_mode & IFMT) == IFDIR)
12405 cgp->cg_cs.cs_ndir++;
12406 cgp->cg_cs.cs_nifree--;
12407 setbit(inosused, ino);
12408 chgs = 1;
12409 }
12410 jaddref->ja_state &= ~UNDONE;
12411 jaddref->ja_state |= ATTACHED;
12412 free_jaddref(jaddref);
12413 }
12414 }
12415 /*
12416 * Restore any block allocations which are pending journal writes.
12417 */
12418 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12419 cgp = (struct cg *)bp->b_data;
12420 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12421 blksfree = cg_blksfree(cgp);
12422 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12423 jntmp) {
12424 if ((jnewblk->jn_state & UNDONE) == 0)
12425 continue;
12426 /* Do the roll-forward only if it's a real copy. */
12427 if (foreground &&
12428 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12429 chgs = 1;
12430 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12431 jnewblk->jn_state |= ATTACHED;
12432 free_jnewblk(jnewblk);
12433 }
12434 }
12435 /*
12436 * If the write did not succeed, we have done all the roll-forward
12437 * operations, but we cannot take the actions that will allow its
12438 * dependencies to be processed.
12439 */
12440 if ((flags & WRITESUCCEEDED) == 0) {
12441 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12442 newblk, nb_deps);
12443 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12444 worklist, wk_list);
12445 if (foreground)
12446 bdirty(bp);
12447 return (1);
12448 }
12449 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12450 newblk->nb_state |= DEPCOMPLETE;
12451 newblk->nb_state &= ~ONDEPLIST;
12452 newblk->nb_bmsafemap = NULL;
12453 LIST_REMOVE(newblk, nb_deps);
12454 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12455 handle_allocdirect_partdone(
12456 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12457 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12458 handle_allocindir_partdone(
12459 WK_ALLOCINDIR(&newblk->nb_list));
12460 else if (newblk->nb_list.wk_type != D_NEWBLK)
12461 panic("handle_written_bmsafemap: Unexpected type: %s",
12462 TYPENAME(newblk->nb_list.wk_type));
12463 }
12464 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12465 inodedep->id_state |= DEPCOMPLETE;
12466 inodedep->id_state &= ~ONDEPLIST;
12467 LIST_REMOVE(inodedep, id_deps);
12468 inodedep->id_bmsafemap = NULL;
12469 }
12470 LIST_REMOVE(bmsafemap, sm_next);
12471 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12472 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12473 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12474 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12475 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12476 LIST_REMOVE(bmsafemap, sm_hash);
12477 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12478 return (0);
12479 }
12480 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12481 if (foreground)
12482 bdirty(bp);
12483 return (1);
12484 }
12485
12486 /*
12487 * Try to free a mkdir dependency.
12488 */
12489 static void
complete_mkdir(mkdir)12490 complete_mkdir(mkdir)
12491 struct mkdir *mkdir;
12492 {
12493 struct diradd *dap;
12494
12495 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12496 return;
12497 LIST_REMOVE(mkdir, md_mkdirs);
12498 dap = mkdir->md_diradd;
12499 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12500 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12501 dap->da_state |= DEPCOMPLETE;
12502 complete_diradd(dap);
12503 }
12504 WORKITEM_FREE(mkdir, D_MKDIR);
12505 }
12506
12507 /*
12508 * Handle the completion of a mkdir dependency.
12509 */
12510 static void
handle_written_mkdir(mkdir,type)12511 handle_written_mkdir(mkdir, type)
12512 struct mkdir *mkdir;
12513 int type;
12514 {
12515
12516 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12517 panic("handle_written_mkdir: bad type");
12518 mkdir->md_state |= COMPLETE;
12519 complete_mkdir(mkdir);
12520 }
12521
12522 static int
free_pagedep(pagedep)12523 free_pagedep(pagedep)
12524 struct pagedep *pagedep;
12525 {
12526 int i;
12527
12528 if (pagedep->pd_state & NEWBLOCK)
12529 return (0);
12530 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12531 return (0);
12532 for (i = 0; i < DAHASHSZ; i++)
12533 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12534 return (0);
12535 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12536 return (0);
12537 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12538 return (0);
12539 if (pagedep->pd_state & ONWORKLIST)
12540 WORKLIST_REMOVE(&pagedep->pd_list);
12541 LIST_REMOVE(pagedep, pd_hash);
12542 WORKITEM_FREE(pagedep, D_PAGEDEP);
12543
12544 return (1);
12545 }
12546
12547 /*
12548 * Called from within softdep_disk_write_complete above.
12549 * A write operation was just completed. Removed inodes can
12550 * now be freed and associated block pointers may be committed.
12551 * Note that this routine is always called from interrupt level
12552 * with further interrupts from this device blocked.
12553 *
12554 * If the write did not succeed, we will do all the roll-forward
12555 * operations, but we will not take the actions that will allow its
12556 * dependencies to be processed.
12557 */
12558 static int
handle_written_filepage(pagedep,bp,flags)12559 handle_written_filepage(pagedep, bp, flags)
12560 struct pagedep *pagedep;
12561 struct buf *bp; /* buffer containing the written page */
12562 int flags;
12563 {
12564 struct dirrem *dirrem;
12565 struct diradd *dap, *nextdap;
12566 struct direct *ep;
12567 int i, chgs;
12568
12569 if ((pagedep->pd_state & IOSTARTED) == 0)
12570 panic("handle_written_filepage: not started");
12571 pagedep->pd_state &= ~IOSTARTED;
12572 if ((flags & WRITESUCCEEDED) == 0)
12573 goto rollforward;
12574 /*
12575 * Process any directory removals that have been committed.
12576 */
12577 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12578 LIST_REMOVE(dirrem, dm_next);
12579 dirrem->dm_state |= COMPLETE;
12580 dirrem->dm_dirinum = pagedep->pd_ino;
12581 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12582 ("handle_written_filepage: Journal entries not written."));
12583 add_to_worklist(&dirrem->dm_list, 0);
12584 }
12585 /*
12586 * Free any directory additions that have been committed.
12587 * If it is a newly allocated block, we have to wait until
12588 * the on-disk directory inode claims the new block.
12589 */
12590 if ((pagedep->pd_state & NEWBLOCK) == 0)
12591 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12592 free_diradd(dap, NULL);
12593 rollforward:
12594 /*
12595 * Uncommitted directory entries must be restored.
12596 */
12597 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12598 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12599 dap = nextdap) {
12600 nextdap = LIST_NEXT(dap, da_pdlist);
12601 if (dap->da_state & ATTACHED)
12602 panic("handle_written_filepage: attached");
12603 ep = (struct direct *)
12604 ((char *)bp->b_data + dap->da_offset);
12605 ep->d_ino = dap->da_newinum;
12606 dap->da_state &= ~UNDONE;
12607 dap->da_state |= ATTACHED;
12608 chgs = 1;
12609 /*
12610 * If the inode referenced by the directory has
12611 * been written out, then the dependency can be
12612 * moved to the pending list.
12613 */
12614 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12615 LIST_REMOVE(dap, da_pdlist);
12616 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12617 da_pdlist);
12618 }
12619 }
12620 }
12621 /*
12622 * If there were any rollbacks in the directory, then it must be
12623 * marked dirty so that its will eventually get written back in
12624 * its correct form.
12625 */
12626 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12627 if ((bp->b_flags & B_DELWRI) == 0)
12628 stat_dir_entry++;
12629 bdirty(bp);
12630 return (1);
12631 }
12632 /*
12633 * If we are not waiting for a new directory block to be
12634 * claimed by its inode, then the pagedep will be freed.
12635 * Otherwise it will remain to track any new entries on
12636 * the page in case they are fsync'ed.
12637 */
12638 free_pagedep(pagedep);
12639 return (0);
12640 }
12641
12642 /*
12643 * Writing back in-core inode structures.
12644 *
12645 * The filesystem only accesses an inode's contents when it occupies an
12646 * "in-core" inode structure. These "in-core" structures are separate from
12647 * the page frames used to cache inode blocks. Only the latter are
12648 * transferred to/from the disk. So, when the updated contents of the
12649 * "in-core" inode structure are copied to the corresponding in-memory inode
12650 * block, the dependencies are also transferred. The following procedure is
12651 * called when copying a dirty "in-core" inode to a cached inode block.
12652 */
12653
12654 /*
12655 * Called when an inode is loaded from disk. If the effective link count
12656 * differed from the actual link count when it was last flushed, then we
12657 * need to ensure that the correct effective link count is put back.
12658 */
12659 void
softdep_load_inodeblock(ip)12660 softdep_load_inodeblock(ip)
12661 struct inode *ip; /* the "in_core" copy of the inode */
12662 {
12663 struct inodedep *inodedep;
12664 struct ufsmount *ump;
12665
12666 ump = ITOUMP(ip);
12667 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12668 ("softdep_load_inodeblock called on non-softdep filesystem"));
12669 /*
12670 * Check for alternate nlink count.
12671 */
12672 ip->i_effnlink = ip->i_nlink;
12673 ACQUIRE_LOCK(ump);
12674 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12675 FREE_LOCK(ump);
12676 return;
12677 }
12678 if (ip->i_nlink != inodedep->id_nlinkwrote &&
12679 inodedep->id_nlinkwrote != -1) {
12680 KASSERT(ip->i_nlink == 0 &&
12681 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0,
12682 ("read bad i_nlink value"));
12683 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote;
12684 }
12685 ip->i_effnlink -= inodedep->id_nlinkdelta;
12686 KASSERT(ip->i_effnlink >= 0,
12687 ("softdep_load_inodeblock: negative i_effnlink"));
12688 FREE_LOCK(ump);
12689 }
12690
12691 /*
12692 * This routine is called just before the "in-core" inode
12693 * information is to be copied to the in-memory inode block.
12694 * Recall that an inode block contains several inodes. If
12695 * the force flag is set, then the dependencies will be
12696 * cleared so that the update can always be made. Note that
12697 * the buffer is locked when this routine is called, so we
12698 * will never be in the middle of writing the inode block
12699 * to disk.
12700 */
12701 void
softdep_update_inodeblock(ip,bp,waitfor)12702 softdep_update_inodeblock(ip, bp, waitfor)
12703 struct inode *ip; /* the "in_core" copy of the inode */
12704 struct buf *bp; /* the buffer containing the inode block */
12705 int waitfor; /* nonzero => update must be allowed */
12706 {
12707 struct inodedep *inodedep;
12708 struct inoref *inoref;
12709 struct ufsmount *ump;
12710 struct worklist *wk;
12711 struct mount *mp;
12712 struct buf *ibp;
12713 struct fs *fs;
12714 int error;
12715
12716 ump = ITOUMP(ip);
12717 mp = UFSTOVFS(ump);
12718 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12719 ("softdep_update_inodeblock called on non-softdep filesystem"));
12720 fs = ump->um_fs;
12721 /*
12722 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12723 * does not have access to the in-core ip so must write directly into
12724 * the inode block buffer when setting freelink.
12725 */
12726 if (fs->fs_magic == FS_UFS1_MAGIC)
12727 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12728 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12729 else
12730 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12731 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12732 /*
12733 * If the effective link count is not equal to the actual link
12734 * count, then we must track the difference in an inodedep while
12735 * the inode is (potentially) tossed out of the cache. Otherwise,
12736 * if there is no existing inodedep, then there are no dependencies
12737 * to track.
12738 */
12739 ACQUIRE_LOCK(ump);
12740 again:
12741 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12742 FREE_LOCK(ump);
12743 if (ip->i_effnlink != ip->i_nlink)
12744 panic("softdep_update_inodeblock: bad link count");
12745 return;
12746 }
12747 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta,
12748 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d "
12749 "inodedep %p id_nlinkdelta %jd",
12750 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta));
12751 inodedep->id_nlinkwrote = ip->i_nlink;
12752 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12753 panic("softdep_update_inodeblock: bad delta");
12754 /*
12755 * If we're flushing all dependencies we must also move any waiting
12756 * for journal writes onto the bufwait list prior to I/O.
12757 */
12758 if (waitfor) {
12759 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12760 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12761 == DEPCOMPLETE) {
12762 jwait(&inoref->if_list, MNT_WAIT);
12763 goto again;
12764 }
12765 }
12766 }
12767 /*
12768 * Changes have been initiated. Anything depending on these
12769 * changes cannot occur until this inode has been written.
12770 */
12771 inodedep->id_state &= ~COMPLETE;
12772 if ((inodedep->id_state & ONWORKLIST) == 0)
12773 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12774 /*
12775 * Any new dependencies associated with the incore inode must
12776 * now be moved to the list associated with the buffer holding
12777 * the in-memory copy of the inode. Once merged process any
12778 * allocdirects that are completed by the merger.
12779 */
12780 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12781 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12782 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12783 NULL);
12784 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12785 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12786 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12787 NULL);
12788 /*
12789 * Now that the inode has been pushed into the buffer, the
12790 * operations dependent on the inode being written to disk
12791 * can be moved to the id_bufwait so that they will be
12792 * processed when the buffer I/O completes.
12793 */
12794 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12795 WORKLIST_REMOVE(wk);
12796 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12797 }
12798 /*
12799 * Newly allocated inodes cannot be written until the bitmap
12800 * that allocates them have been written (indicated by
12801 * DEPCOMPLETE being set in id_state). If we are doing a
12802 * forced sync (e.g., an fsync on a file), we force the bitmap
12803 * to be written so that the update can be done.
12804 */
12805 if (waitfor == 0) {
12806 FREE_LOCK(ump);
12807 return;
12808 }
12809 retry:
12810 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12811 FREE_LOCK(ump);
12812 return;
12813 }
12814 ibp = inodedep->id_bmsafemap->sm_buf;
12815 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12816 if (ibp == NULL) {
12817 /*
12818 * If ibp came back as NULL, the dependency could have been
12819 * freed while we slept. Look it up again, and check to see
12820 * that it has completed.
12821 */
12822 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12823 goto retry;
12824 FREE_LOCK(ump);
12825 return;
12826 }
12827 FREE_LOCK(ump);
12828 if ((error = bwrite(ibp)) != 0)
12829 softdep_error("softdep_update_inodeblock: bwrite", error);
12830 }
12831
12832 /*
12833 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12834 * old inode dependency list (such as id_inoupdt).
12835 */
12836 static void
merge_inode_lists(newlisthead,oldlisthead)12837 merge_inode_lists(newlisthead, oldlisthead)
12838 struct allocdirectlst *newlisthead;
12839 struct allocdirectlst *oldlisthead;
12840 {
12841 struct allocdirect *listadp, *newadp;
12842
12843 newadp = TAILQ_FIRST(newlisthead);
12844 if (newadp != NULL)
12845 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12846 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12847 if (listadp->ad_offset < newadp->ad_offset) {
12848 listadp = TAILQ_NEXT(listadp, ad_next);
12849 continue;
12850 }
12851 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12852 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12853 if (listadp->ad_offset == newadp->ad_offset) {
12854 allocdirect_merge(oldlisthead, newadp,
12855 listadp);
12856 listadp = newadp;
12857 }
12858 newadp = TAILQ_FIRST(newlisthead);
12859 }
12860 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12861 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12862 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12863 }
12864 }
12865
12866 /*
12867 * If we are doing an fsync, then we must ensure that any directory
12868 * entries for the inode have been written after the inode gets to disk.
12869 */
12870 int
softdep_fsync(vp)12871 softdep_fsync(vp)
12872 struct vnode *vp; /* the "in_core" copy of the inode */
12873 {
12874 struct inodedep *inodedep;
12875 struct pagedep *pagedep;
12876 struct inoref *inoref;
12877 struct ufsmount *ump;
12878 struct worklist *wk;
12879 struct diradd *dap;
12880 struct mount *mp;
12881 struct vnode *pvp;
12882 struct inode *ip;
12883 struct buf *bp;
12884 struct fs *fs;
12885 struct thread *td = curthread;
12886 int error, flushparent, pagedep_new_block;
12887 ino_t parentino;
12888 ufs_lbn_t lbn;
12889
12890 ip = VTOI(vp);
12891 mp = vp->v_mount;
12892 ump = VFSTOUFS(mp);
12893 fs = ump->um_fs;
12894 if (MOUNTEDSOFTDEP(mp) == 0)
12895 return (0);
12896 ACQUIRE_LOCK(ump);
12897 restart:
12898 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12899 FREE_LOCK(ump);
12900 return (0);
12901 }
12902 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12903 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12904 == DEPCOMPLETE) {
12905 jwait(&inoref->if_list, MNT_WAIT);
12906 goto restart;
12907 }
12908 }
12909 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12910 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12911 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12912 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12913 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12914 panic("softdep_fsync: pending ops %p", inodedep);
12915 for (error = 0, flushparent = 0; ; ) {
12916 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12917 break;
12918 if (wk->wk_type != D_DIRADD)
12919 panic("softdep_fsync: Unexpected type %s",
12920 TYPENAME(wk->wk_type));
12921 dap = WK_DIRADD(wk);
12922 /*
12923 * Flush our parent if this directory entry has a MKDIR_PARENT
12924 * dependency or is contained in a newly allocated block.
12925 */
12926 if (dap->da_state & DIRCHG)
12927 pagedep = dap->da_previous->dm_pagedep;
12928 else
12929 pagedep = dap->da_pagedep;
12930 parentino = pagedep->pd_ino;
12931 lbn = pagedep->pd_lbn;
12932 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12933 panic("softdep_fsync: dirty");
12934 if ((dap->da_state & MKDIR_PARENT) ||
12935 (pagedep->pd_state & NEWBLOCK))
12936 flushparent = 1;
12937 else
12938 flushparent = 0;
12939 /*
12940 * If we are being fsync'ed as part of vgone'ing this vnode,
12941 * then we will not be able to release and recover the
12942 * vnode below, so we just have to give up on writing its
12943 * directory entry out. It will eventually be written, just
12944 * not now, but then the user was not asking to have it
12945 * written, so we are not breaking any promises.
12946 */
12947 if (VN_IS_DOOMED(vp))
12948 break;
12949 /*
12950 * We prevent deadlock by always fetching inodes from the
12951 * root, moving down the directory tree. Thus, when fetching
12952 * our parent directory, we first try to get the lock. If
12953 * that fails, we must unlock ourselves before requesting
12954 * the lock on our parent. See the comment in ufs_lookup
12955 * for details on possible races.
12956 */
12957 FREE_LOCK(ump);
12958 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL,
12959 &pvp);
12960 if (error == ERELOOKUP)
12961 error = 0;
12962 if (error != 0)
12963 return (error);
12964 /*
12965 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12966 * that are contained in direct blocks will be resolved by
12967 * doing a ffs_update. Pagedeps contained in indirect blocks
12968 * may require a complete sync'ing of the directory. So, we
12969 * try the cheap and fast ffs_update first, and if that fails,
12970 * then we do the slower ffs_syncvnode of the directory.
12971 */
12972 if (flushparent) {
12973 int locked;
12974
12975 if ((error = ffs_update(pvp, 1)) != 0) {
12976 vput(pvp);
12977 return (error);
12978 }
12979 ACQUIRE_LOCK(ump);
12980 locked = 1;
12981 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12982 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12983 if (wk->wk_type != D_DIRADD)
12984 panic("softdep_fsync: Unexpected type %s",
12985 TYPENAME(wk->wk_type));
12986 dap = WK_DIRADD(wk);
12987 if (dap->da_state & DIRCHG)
12988 pagedep = dap->da_previous->dm_pagedep;
12989 else
12990 pagedep = dap->da_pagedep;
12991 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12992 FREE_LOCK(ump);
12993 locked = 0;
12994 if (pagedep_new_block && (error =
12995 ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12996 vput(pvp);
12997 return (error);
12998 }
12999 }
13000 }
13001 if (locked)
13002 FREE_LOCK(ump);
13003 }
13004 /*
13005 * Flush directory page containing the inode's name.
13006 */
13007 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
13008 &bp);
13009 if (error == 0)
13010 error = bwrite(bp);
13011 else
13012 brelse(bp);
13013 vput(pvp);
13014 if (!ffs_fsfail_cleanup(ump, error))
13015 return (error);
13016 ACQUIRE_LOCK(ump);
13017 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
13018 break;
13019 }
13020 FREE_LOCK(ump);
13021 return (0);
13022 }
13023
13024 /*
13025 * Flush all the dirty bitmaps associated with the block device
13026 * before flushing the rest of the dirty blocks so as to reduce
13027 * the number of dependencies that will have to be rolled back.
13028 *
13029 * XXX Unused?
13030 */
13031 void
softdep_fsync_mountdev(vp)13032 softdep_fsync_mountdev(vp)
13033 struct vnode *vp;
13034 {
13035 struct buf *bp, *nbp;
13036 struct worklist *wk;
13037 struct bufobj *bo;
13038
13039 if (!vn_isdisk(vp))
13040 panic("softdep_fsync_mountdev: vnode not a disk");
13041 bo = &vp->v_bufobj;
13042 restart:
13043 BO_LOCK(bo);
13044 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
13045 /*
13046 * If it is already scheduled, skip to the next buffer.
13047 */
13048 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
13049 continue;
13050
13051 if ((bp->b_flags & B_DELWRI) == 0)
13052 panic("softdep_fsync_mountdev: not dirty");
13053 /*
13054 * We are only interested in bitmaps with outstanding
13055 * dependencies.
13056 */
13057 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
13058 wk->wk_type != D_BMSAFEMAP ||
13059 (bp->b_vflags & BV_BKGRDINPROG)) {
13060 BUF_UNLOCK(bp);
13061 continue;
13062 }
13063 BO_UNLOCK(bo);
13064 bremfree(bp);
13065 (void) bawrite(bp);
13066 goto restart;
13067 }
13068 drain_output(vp);
13069 BO_UNLOCK(bo);
13070 }
13071
13072 /*
13073 * Sync all cylinder groups that were dirty at the time this function is
13074 * called. Newly dirtied cgs will be inserted before the sentinel. This
13075 * is used to flush freedep activity that may be holding up writes to a
13076 * indirect block.
13077 */
13078 static int
sync_cgs(mp,waitfor)13079 sync_cgs(mp, waitfor)
13080 struct mount *mp;
13081 int waitfor;
13082 {
13083 struct bmsafemap *bmsafemap;
13084 struct bmsafemap *sentinel;
13085 struct ufsmount *ump;
13086 struct buf *bp;
13087 int error;
13088
13089 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
13090 sentinel->sm_cg = -1;
13091 ump = VFSTOUFS(mp);
13092 error = 0;
13093 ACQUIRE_LOCK(ump);
13094 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
13095 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
13096 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
13097 /* Skip sentinels and cgs with no work to release. */
13098 if (bmsafemap->sm_cg == -1 ||
13099 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
13100 LIST_EMPTY(&bmsafemap->sm_freewr))) {
13101 LIST_REMOVE(sentinel, sm_next);
13102 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
13103 continue;
13104 }
13105 /*
13106 * If we don't get the lock and we're waiting try again, if
13107 * not move on to the next buf and try to sync it.
13108 */
13109 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
13110 if (bp == NULL && waitfor == MNT_WAIT)
13111 continue;
13112 LIST_REMOVE(sentinel, sm_next);
13113 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
13114 if (bp == NULL)
13115 continue;
13116 FREE_LOCK(ump);
13117 if (waitfor == MNT_NOWAIT)
13118 bawrite(bp);
13119 else
13120 error = bwrite(bp);
13121 ACQUIRE_LOCK(ump);
13122 if (error)
13123 break;
13124 }
13125 LIST_REMOVE(sentinel, sm_next);
13126 FREE_LOCK(ump);
13127 free(sentinel, M_BMSAFEMAP);
13128 return (error);
13129 }
13130
13131 /*
13132 * This routine is called when we are trying to synchronously flush a
13133 * file. This routine must eliminate any filesystem metadata dependencies
13134 * so that the syncing routine can succeed.
13135 */
13136 int
softdep_sync_metadata(struct vnode * vp)13137 softdep_sync_metadata(struct vnode *vp)
13138 {
13139 struct inode *ip;
13140 int error;
13141
13142 ip = VTOI(vp);
13143 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13144 ("softdep_sync_metadata called on non-softdep filesystem"));
13145 /*
13146 * Ensure that any direct block dependencies have been cleared,
13147 * truncations are started, and inode references are journaled.
13148 */
13149 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
13150 /*
13151 * Write all journal records to prevent rollbacks on devvp.
13152 */
13153 if (vp->v_type == VCHR)
13154 softdep_flushjournal(vp->v_mount);
13155 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
13156 /*
13157 * Ensure that all truncates are written so we won't find deps on
13158 * indirect blocks.
13159 */
13160 process_truncates(vp);
13161 FREE_LOCK(VFSTOUFS(vp->v_mount));
13162
13163 return (error);
13164 }
13165
13166 /*
13167 * This routine is called when we are attempting to sync a buf with
13168 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
13169 * other IO it can but returns EBUSY if the buffer is not yet able to
13170 * be written. Dependencies which will not cause rollbacks will always
13171 * return 0.
13172 */
13173 int
softdep_sync_buf(struct vnode * vp,struct buf * bp,int waitfor)13174 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
13175 {
13176 struct indirdep *indirdep;
13177 struct pagedep *pagedep;
13178 struct allocindir *aip;
13179 struct newblk *newblk;
13180 struct ufsmount *ump;
13181 struct buf *nbp;
13182 struct worklist *wk;
13183 int i, error;
13184
13185 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13186 ("softdep_sync_buf called on non-softdep filesystem"));
13187 /*
13188 * For VCHR we just don't want to force flush any dependencies that
13189 * will cause rollbacks.
13190 */
13191 if (vp->v_type == VCHR) {
13192 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
13193 return (EBUSY);
13194 return (0);
13195 }
13196 ump = VFSTOUFS(vp->v_mount);
13197 ACQUIRE_LOCK(ump);
13198 /*
13199 * As we hold the buffer locked, none of its dependencies
13200 * will disappear.
13201 */
13202 error = 0;
13203 top:
13204 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
13205 switch (wk->wk_type) {
13206 case D_ALLOCDIRECT:
13207 case D_ALLOCINDIR:
13208 newblk = WK_NEWBLK(wk);
13209 if (newblk->nb_jnewblk != NULL) {
13210 if (waitfor == MNT_NOWAIT) {
13211 error = EBUSY;
13212 goto out_unlock;
13213 }
13214 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
13215 goto top;
13216 }
13217 if (newblk->nb_state & DEPCOMPLETE ||
13218 waitfor == MNT_NOWAIT)
13219 continue;
13220 nbp = newblk->nb_bmsafemap->sm_buf;
13221 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13222 if (nbp == NULL)
13223 goto top;
13224 FREE_LOCK(ump);
13225 if ((error = bwrite(nbp)) != 0)
13226 goto out;
13227 ACQUIRE_LOCK(ump);
13228 continue;
13229
13230 case D_INDIRDEP:
13231 indirdep = WK_INDIRDEP(wk);
13232 if (waitfor == MNT_NOWAIT) {
13233 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
13234 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
13235 error = EBUSY;
13236 goto out_unlock;
13237 }
13238 }
13239 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
13240 panic("softdep_sync_buf: truncation pending.");
13241 restart:
13242 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
13243 newblk = (struct newblk *)aip;
13244 if (newblk->nb_jnewblk != NULL) {
13245 jwait(&newblk->nb_jnewblk->jn_list,
13246 waitfor);
13247 goto restart;
13248 }
13249 if (newblk->nb_state & DEPCOMPLETE)
13250 continue;
13251 nbp = newblk->nb_bmsafemap->sm_buf;
13252 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13253 if (nbp == NULL)
13254 goto restart;
13255 FREE_LOCK(ump);
13256 if ((error = bwrite(nbp)) != 0)
13257 goto out;
13258 ACQUIRE_LOCK(ump);
13259 goto restart;
13260 }
13261 continue;
13262
13263 case D_PAGEDEP:
13264 /*
13265 * Only flush directory entries in synchronous passes.
13266 */
13267 if (waitfor != MNT_WAIT) {
13268 error = EBUSY;
13269 goto out_unlock;
13270 }
13271 /*
13272 * While syncing snapshots, we must allow recursive
13273 * lookups.
13274 */
13275 BUF_AREC(bp);
13276 /*
13277 * We are trying to sync a directory that may
13278 * have dependencies on both its own metadata
13279 * and/or dependencies on the inodes of any
13280 * recently allocated files. We walk its diradd
13281 * lists pushing out the associated inode.
13282 */
13283 pagedep = WK_PAGEDEP(wk);
13284 for (i = 0; i < DAHASHSZ; i++) {
13285 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
13286 continue;
13287 error = flush_pagedep_deps(vp, wk->wk_mp,
13288 &pagedep->pd_diraddhd[i], bp);
13289 if (error != 0) {
13290 if (error != ERELOOKUP)
13291 BUF_NOREC(bp);
13292 goto out_unlock;
13293 }
13294 }
13295 BUF_NOREC(bp);
13296 continue;
13297
13298 case D_FREEWORK:
13299 case D_FREEDEP:
13300 case D_JSEGDEP:
13301 case D_JNEWBLK:
13302 continue;
13303
13304 default:
13305 panic("softdep_sync_buf: Unknown type %s",
13306 TYPENAME(wk->wk_type));
13307 /* NOTREACHED */
13308 }
13309 }
13310 out_unlock:
13311 FREE_LOCK(ump);
13312 out:
13313 return (error);
13314 }
13315
13316 /*
13317 * Flush the dependencies associated with an inodedep.
13318 */
13319 static int
flush_inodedep_deps(vp,mp,ino)13320 flush_inodedep_deps(vp, mp, ino)
13321 struct vnode *vp;
13322 struct mount *mp;
13323 ino_t ino;
13324 {
13325 struct inodedep *inodedep;
13326 struct inoref *inoref;
13327 struct ufsmount *ump;
13328 int error, waitfor;
13329
13330 /*
13331 * This work is done in two passes. The first pass grabs most
13332 * of the buffers and begins asynchronously writing them. The
13333 * only way to wait for these asynchronous writes is to sleep
13334 * on the filesystem vnode which may stay busy for a long time
13335 * if the filesystem is active. So, instead, we make a second
13336 * pass over the dependencies blocking on each write. In the
13337 * usual case we will be blocking against a write that we
13338 * initiated, so when it is done the dependency will have been
13339 * resolved. Thus the second pass is expected to end quickly.
13340 * We give a brief window at the top of the loop to allow
13341 * any pending I/O to complete.
13342 */
13343 ump = VFSTOUFS(mp);
13344 LOCK_OWNED(ump);
13345 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
13346 if (error)
13347 return (error);
13348 FREE_LOCK(ump);
13349 ACQUIRE_LOCK(ump);
13350 restart:
13351 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13352 return (0);
13353 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13354 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13355 == DEPCOMPLETE) {
13356 jwait(&inoref->if_list, MNT_WAIT);
13357 goto restart;
13358 }
13359 }
13360 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
13361 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
13362 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
13363 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
13364 continue;
13365 /*
13366 * If pass2, we are done, otherwise do pass 2.
13367 */
13368 if (waitfor == MNT_WAIT)
13369 break;
13370 waitfor = MNT_WAIT;
13371 }
13372 /*
13373 * Try freeing inodedep in case all dependencies have been removed.
13374 */
13375 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
13376 (void) free_inodedep(inodedep);
13377 return (0);
13378 }
13379
13380 /*
13381 * Flush an inode dependency list.
13382 */
13383 static int
flush_deplist(listhead,waitfor,errorp)13384 flush_deplist(listhead, waitfor, errorp)
13385 struct allocdirectlst *listhead;
13386 int waitfor;
13387 int *errorp;
13388 {
13389 struct allocdirect *adp;
13390 struct newblk *newblk;
13391 struct ufsmount *ump;
13392 struct buf *bp;
13393
13394 if ((adp = TAILQ_FIRST(listhead)) == NULL)
13395 return (0);
13396 ump = VFSTOUFS(adp->ad_list.wk_mp);
13397 LOCK_OWNED(ump);
13398 TAILQ_FOREACH(adp, listhead, ad_next) {
13399 newblk = (struct newblk *)adp;
13400 if (newblk->nb_jnewblk != NULL) {
13401 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13402 return (1);
13403 }
13404 if (newblk->nb_state & DEPCOMPLETE)
13405 continue;
13406 bp = newblk->nb_bmsafemap->sm_buf;
13407 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13408 if (bp == NULL) {
13409 if (waitfor == MNT_NOWAIT)
13410 continue;
13411 return (1);
13412 }
13413 FREE_LOCK(ump);
13414 if (waitfor == MNT_NOWAIT)
13415 bawrite(bp);
13416 else
13417 *errorp = bwrite(bp);
13418 ACQUIRE_LOCK(ump);
13419 return (1);
13420 }
13421 return (0);
13422 }
13423
13424 /*
13425 * Flush dependencies associated with an allocdirect block.
13426 */
13427 static int
flush_newblk_dep(vp,mp,lbn)13428 flush_newblk_dep(vp, mp, lbn)
13429 struct vnode *vp;
13430 struct mount *mp;
13431 ufs_lbn_t lbn;
13432 {
13433 struct newblk *newblk;
13434 struct ufsmount *ump;
13435 struct bufobj *bo;
13436 struct inode *ip;
13437 struct buf *bp;
13438 ufs2_daddr_t blkno;
13439 int error;
13440
13441 error = 0;
13442 bo = &vp->v_bufobj;
13443 ip = VTOI(vp);
13444 blkno = DIP(ip, i_db[lbn]);
13445 if (blkno == 0)
13446 panic("flush_newblk_dep: Missing block");
13447 ump = VFSTOUFS(mp);
13448 ACQUIRE_LOCK(ump);
13449 /*
13450 * Loop until all dependencies related to this block are satisfied.
13451 * We must be careful to restart after each sleep in case a write
13452 * completes some part of this process for us.
13453 */
13454 for (;;) {
13455 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13456 FREE_LOCK(ump);
13457 break;
13458 }
13459 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13460 panic("flush_newblk_dep: Bad newblk %p", newblk);
13461 /*
13462 * Flush the journal.
13463 */
13464 if (newblk->nb_jnewblk != NULL) {
13465 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13466 continue;
13467 }
13468 /*
13469 * Write the bitmap dependency.
13470 */
13471 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13472 bp = newblk->nb_bmsafemap->sm_buf;
13473 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13474 if (bp == NULL)
13475 continue;
13476 FREE_LOCK(ump);
13477 error = bwrite(bp);
13478 if (error)
13479 break;
13480 ACQUIRE_LOCK(ump);
13481 continue;
13482 }
13483 /*
13484 * Write the buffer.
13485 */
13486 FREE_LOCK(ump);
13487 BO_LOCK(bo);
13488 bp = gbincore(bo, lbn);
13489 if (bp != NULL) {
13490 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13491 LK_INTERLOCK, BO_LOCKPTR(bo));
13492 if (error == ENOLCK) {
13493 ACQUIRE_LOCK(ump);
13494 error = 0;
13495 continue; /* Slept, retry */
13496 }
13497 if (error != 0)
13498 break; /* Failed */
13499 if (bp->b_flags & B_DELWRI) {
13500 bremfree(bp);
13501 error = bwrite(bp);
13502 if (error)
13503 break;
13504 } else
13505 BUF_UNLOCK(bp);
13506 } else
13507 BO_UNLOCK(bo);
13508 /*
13509 * We have to wait for the direct pointers to
13510 * point at the newdirblk before the dependency
13511 * will go away.
13512 */
13513 error = ffs_update(vp, 1);
13514 if (error)
13515 break;
13516 ACQUIRE_LOCK(ump);
13517 }
13518 return (error);
13519 }
13520
13521 /*
13522 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13523 */
13524 static int
flush_pagedep_deps(pvp,mp,diraddhdp,locked_bp)13525 flush_pagedep_deps(pvp, mp, diraddhdp, locked_bp)
13526 struct vnode *pvp;
13527 struct mount *mp;
13528 struct diraddhd *diraddhdp;
13529 struct buf *locked_bp;
13530 {
13531 struct inodedep *inodedep;
13532 struct inoref *inoref;
13533 struct ufsmount *ump;
13534 struct diradd *dap;
13535 struct vnode *vp;
13536 int error = 0;
13537 struct buf *bp;
13538 ino_t inum;
13539 struct diraddhd unfinished;
13540
13541 LIST_INIT(&unfinished);
13542 ump = VFSTOUFS(mp);
13543 LOCK_OWNED(ump);
13544 restart:
13545 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13546 /*
13547 * Flush ourselves if this directory entry
13548 * has a MKDIR_PARENT dependency.
13549 */
13550 if (dap->da_state & MKDIR_PARENT) {
13551 FREE_LOCK(ump);
13552 if ((error = ffs_update(pvp, 1)) != 0)
13553 break;
13554 ACQUIRE_LOCK(ump);
13555 /*
13556 * If that cleared dependencies, go on to next.
13557 */
13558 if (dap != LIST_FIRST(diraddhdp))
13559 continue;
13560 /*
13561 * All MKDIR_PARENT dependencies and all the
13562 * NEWBLOCK pagedeps that are contained in direct
13563 * blocks were resolved by doing above ffs_update.
13564 * Pagedeps contained in indirect blocks may
13565 * require a complete sync'ing of the directory.
13566 * We are in the midst of doing a complete sync,
13567 * so if they are not resolved in this pass we
13568 * defer them for now as they will be sync'ed by
13569 * our caller shortly.
13570 */
13571 LIST_REMOVE(dap, da_pdlist);
13572 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13573 continue;
13574 }
13575 /*
13576 * A newly allocated directory must have its "." and
13577 * ".." entries written out before its name can be
13578 * committed in its parent.
13579 */
13580 inum = dap->da_newinum;
13581 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13582 panic("flush_pagedep_deps: lost inode1");
13583 /*
13584 * Wait for any pending journal adds to complete so we don't
13585 * cause rollbacks while syncing.
13586 */
13587 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13588 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13589 == DEPCOMPLETE) {
13590 jwait(&inoref->if_list, MNT_WAIT);
13591 goto restart;
13592 }
13593 }
13594 if (dap->da_state & MKDIR_BODY) {
13595 FREE_LOCK(ump);
13596 error = get_parent_vp(pvp, mp, inum, locked_bp,
13597 diraddhdp, &unfinished, &vp);
13598 if (error != 0)
13599 break;
13600 error = flush_newblk_dep(vp, mp, 0);
13601 /*
13602 * If we still have the dependency we might need to
13603 * update the vnode to sync the new link count to
13604 * disk.
13605 */
13606 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13607 error = ffs_update(vp, 1);
13608 vput(vp);
13609 if (error != 0)
13610 break;
13611 ACQUIRE_LOCK(ump);
13612 /*
13613 * If that cleared dependencies, go on to next.
13614 */
13615 if (dap != LIST_FIRST(diraddhdp))
13616 continue;
13617 if (dap->da_state & MKDIR_BODY) {
13618 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13619 &inodedep);
13620 panic("flush_pagedep_deps: MKDIR_BODY "
13621 "inodedep %p dap %p vp %p",
13622 inodedep, dap, vp);
13623 }
13624 }
13625 /*
13626 * Flush the inode on which the directory entry depends.
13627 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13628 * the only remaining dependency is that the updated inode
13629 * count must get pushed to disk. The inode has already
13630 * been pushed into its inode buffer (via VOP_UPDATE) at
13631 * the time of the reference count change. So we need only
13632 * locate that buffer, ensure that there will be no rollback
13633 * caused by a bitmap dependency, then write the inode buffer.
13634 */
13635 retry:
13636 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13637 panic("flush_pagedep_deps: lost inode");
13638 /*
13639 * If the inode still has bitmap dependencies,
13640 * push them to disk.
13641 */
13642 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13643 bp = inodedep->id_bmsafemap->sm_buf;
13644 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13645 if (bp == NULL)
13646 goto retry;
13647 FREE_LOCK(ump);
13648 if ((error = bwrite(bp)) != 0)
13649 break;
13650 ACQUIRE_LOCK(ump);
13651 if (dap != LIST_FIRST(diraddhdp))
13652 continue;
13653 }
13654 /*
13655 * If the inode is still sitting in a buffer waiting
13656 * to be written or waiting for the link count to be
13657 * adjusted update it here to flush it to disk.
13658 */
13659 if (dap == LIST_FIRST(diraddhdp)) {
13660 FREE_LOCK(ump);
13661 error = get_parent_vp(pvp, mp, inum, locked_bp,
13662 diraddhdp, &unfinished, &vp);
13663 if (error != 0)
13664 break;
13665 error = ffs_update(vp, 1);
13666 vput(vp);
13667 if (error)
13668 break;
13669 ACQUIRE_LOCK(ump);
13670 }
13671 /*
13672 * If we have failed to get rid of all the dependencies
13673 * then something is seriously wrong.
13674 */
13675 if (dap == LIST_FIRST(diraddhdp)) {
13676 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13677 panic("flush_pagedep_deps: failed to flush "
13678 "inodedep %p ino %ju dap %p",
13679 inodedep, (uintmax_t)inum, dap);
13680 }
13681 }
13682 if (error)
13683 ACQUIRE_LOCK(ump);
13684 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13685 LIST_REMOVE(dap, da_pdlist);
13686 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13687 }
13688 return (error);
13689 }
13690
13691 /*
13692 * A large burst of file addition or deletion activity can drive the
13693 * memory load excessively high. First attempt to slow things down
13694 * using the techniques below. If that fails, this routine requests
13695 * the offending operations to fall back to running synchronously
13696 * until the memory load returns to a reasonable level.
13697 */
13698 int
softdep_slowdown(vp)13699 softdep_slowdown(vp)
13700 struct vnode *vp;
13701 {
13702 struct ufsmount *ump;
13703 int jlow;
13704 int max_softdeps_hard;
13705
13706 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13707 ("softdep_slowdown called on non-softdep filesystem"));
13708 ump = VFSTOUFS(vp->v_mount);
13709 ACQUIRE_LOCK(ump);
13710 jlow = 0;
13711 /*
13712 * Check for journal space if needed.
13713 */
13714 if (DOINGSUJ(vp)) {
13715 if (journal_space(ump, 0) == 0)
13716 jlow = 1;
13717 }
13718 /*
13719 * If the system is under its limits and our filesystem is
13720 * not responsible for more than our share of the usage and
13721 * we are not low on journal space, then no need to slow down.
13722 */
13723 max_softdeps_hard = max_softdeps * 11 / 10;
13724 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13725 dep_current[D_INODEDEP] < max_softdeps_hard &&
13726 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13727 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13728 ump->softdep_curdeps[D_DIRREM] <
13729 (max_softdeps_hard / 2) / stat_flush_threads &&
13730 ump->softdep_curdeps[D_INODEDEP] <
13731 max_softdeps_hard / stat_flush_threads &&
13732 ump->softdep_curdeps[D_INDIRDEP] <
13733 (max_softdeps_hard / 1000) / stat_flush_threads &&
13734 ump->softdep_curdeps[D_FREEBLKS] <
13735 max_softdeps_hard / stat_flush_threads) {
13736 FREE_LOCK(ump);
13737 return (0);
13738 }
13739 /*
13740 * If the journal is low or our filesystem is over its limit
13741 * then speedup the cleanup.
13742 */
13743 if (ump->softdep_curdeps[D_INDIRDEP] <
13744 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13745 softdep_speedup(ump);
13746 stat_sync_limit_hit += 1;
13747 FREE_LOCK(ump);
13748 /*
13749 * We only slow down the rate at which new dependencies are
13750 * generated if we are not using journaling. With journaling,
13751 * the cleanup should always be sufficient to keep things
13752 * under control.
13753 */
13754 if (DOINGSUJ(vp))
13755 return (0);
13756 return (1);
13757 }
13758
13759 static int
softdep_request_cleanup_filter(struct vnode * vp,void * arg __unused)13760 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused)
13761 {
13762 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 &&
13763 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0));
13764 }
13765
13766 static void
softdep_request_cleanup_inactivate(struct mount * mp)13767 softdep_request_cleanup_inactivate(struct mount *mp)
13768 {
13769 struct vnode *vp, *mvp;
13770 int error;
13771
13772 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter,
13773 NULL) {
13774 vholdl(vp);
13775 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
13776 VI_LOCK(vp);
13777 if (IS_UFS(vp) && vp->v_usecount == 0) {
13778 while ((vp->v_iflag & VI_OWEINACT) != 0) {
13779 error = vinactive(vp);
13780 if (error != 0 && error != ERELOOKUP)
13781 break;
13782 }
13783 atomic_add_int(&stat_delayed_inact, 1);
13784 }
13785 VOP_UNLOCK(vp);
13786 vdropl(vp);
13787 }
13788 }
13789
13790 /*
13791 * Called by the allocation routines when they are about to fail
13792 * in the hope that we can free up the requested resource (inodes
13793 * or disk space).
13794 *
13795 * First check to see if the work list has anything on it. If it has,
13796 * clean up entries until we successfully free the requested resource.
13797 * Because this process holds inodes locked, we cannot handle any remove
13798 * requests that might block on a locked inode as that could lead to
13799 * deadlock. If the worklist yields none of the requested resource,
13800 * start syncing out vnodes to free up the needed space.
13801 */
13802 int
softdep_request_cleanup(fs,vp,cred,resource)13803 softdep_request_cleanup(fs, vp, cred, resource)
13804 struct fs *fs;
13805 struct vnode *vp;
13806 struct ucred *cred;
13807 int resource;
13808 {
13809 struct ufsmount *ump;
13810 struct mount *mp;
13811 long starttime;
13812 ufs2_daddr_t needed;
13813 int error, failed_vnode;
13814
13815 /*
13816 * If we are being called because of a process doing a
13817 * copy-on-write, then it is not safe to process any
13818 * worklist items as we will recurse into the copyonwrite
13819 * routine. This will result in an incoherent snapshot.
13820 * If the vnode that we hold is a snapshot, we must avoid
13821 * handling other resources that could cause deadlock.
13822 */
13823 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13824 return (0);
13825
13826 if (resource == FLUSH_BLOCKS_WAIT)
13827 stat_cleanup_blkrequests += 1;
13828 else
13829 stat_cleanup_inorequests += 1;
13830
13831 mp = vp->v_mount;
13832 ump = VFSTOUFS(mp);
13833 mtx_assert(UFS_MTX(ump), MA_OWNED);
13834 UFS_UNLOCK(ump);
13835 error = ffs_update(vp, 1);
13836 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13837 UFS_LOCK(ump);
13838 return (0);
13839 }
13840 /*
13841 * If we are in need of resources, start by cleaning up
13842 * any block removals associated with our inode.
13843 */
13844 ACQUIRE_LOCK(ump);
13845 process_removes(vp);
13846 process_truncates(vp);
13847 FREE_LOCK(ump);
13848 /*
13849 * Now clean up at least as many resources as we will need.
13850 *
13851 * When requested to clean up inodes, the number that are needed
13852 * is set by the number of simultaneous writers (mnt_writeopcount)
13853 * plus a bit of slop (2) in case some more writers show up while
13854 * we are cleaning.
13855 *
13856 * When requested to free up space, the amount of space that
13857 * we need is enough blocks to allocate a full-sized segment
13858 * (fs_contigsumsize). The number of such segments that will
13859 * be needed is set by the number of simultaneous writers
13860 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13861 * writers show up while we are cleaning.
13862 *
13863 * Additionally, if we are unpriviledged and allocating space,
13864 * we need to ensure that we clean up enough blocks to get the
13865 * needed number of blocks over the threshold of the minimum
13866 * number of blocks required to be kept free by the filesystem
13867 * (fs_minfree).
13868 */
13869 if (resource == FLUSH_INODES_WAIT) {
13870 needed = vfs_mount_fetch_counter(vp->v_mount,
13871 MNT_COUNT_WRITEOPCOUNT) + 2;
13872 } else if (resource == FLUSH_BLOCKS_WAIT) {
13873 needed = (vfs_mount_fetch_counter(vp->v_mount,
13874 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13875 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13876 needed += fragstoblks(fs,
13877 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13878 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13879 } else {
13880 printf("softdep_request_cleanup: Unknown resource type %d\n",
13881 resource);
13882 UFS_LOCK(ump);
13883 return (0);
13884 }
13885 starttime = time_second;
13886 retry:
13887 if (resource == FLUSH_BLOCKS_WAIT &&
13888 fs->fs_cstotal.cs_nbfree <= needed)
13889 softdep_send_speedup(ump, needed * fs->fs_bsize,
13890 BIO_SPEEDUP_TRIM);
13891 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13892 fs->fs_cstotal.cs_nbfree <= needed) ||
13893 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13894 fs->fs_cstotal.cs_nifree <= needed)) {
13895 ACQUIRE_LOCK(ump);
13896 if (ump->softdep_on_worklist > 0 &&
13897 process_worklist_item(UFSTOVFS(ump),
13898 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13899 stat_worklist_push += 1;
13900 FREE_LOCK(ump);
13901 }
13902
13903 /*
13904 * Check that there are vnodes pending inactivation. As they
13905 * have been unlinked, inactivating them will free up their
13906 * inodes.
13907 */
13908 ACQUIRE_LOCK(ump);
13909 if (resource == FLUSH_INODES_WAIT &&
13910 fs->fs_cstotal.cs_nifree <= needed &&
13911 fs->fs_pendinginodes <= needed) {
13912 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) {
13913 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE;
13914 FREE_LOCK(ump);
13915 softdep_request_cleanup_inactivate(mp);
13916 ACQUIRE_LOCK(ump);
13917 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE;
13918 wakeup(&ump->um_softdep->sd_flags);
13919 } else {
13920 while ((ump->um_softdep->sd_flags &
13921 FLUSH_DI_ACTIVE) != 0) {
13922 msleep(&ump->um_softdep->sd_flags,
13923 LOCK_PTR(ump), PVM, "ffsvina", hz);
13924 }
13925 }
13926 }
13927 FREE_LOCK(ump);
13928
13929 /*
13930 * If we still need resources and there are no more worklist
13931 * entries to process to obtain them, we have to start flushing
13932 * the dirty vnodes to force the release of additional requests
13933 * to the worklist that we can then process to reap addition
13934 * resources. We walk the vnodes associated with the mount point
13935 * until we get the needed worklist requests that we can reap.
13936 *
13937 * If there are several threads all needing to clean the same
13938 * mount point, only one is allowed to walk the mount list.
13939 * When several threads all try to walk the same mount list,
13940 * they end up competing with each other and often end up in
13941 * livelock. This approach ensures that forward progress is
13942 * made at the cost of occational ENOSPC errors being returned
13943 * that might otherwise have been avoided.
13944 */
13945 error = 1;
13946 if ((resource == FLUSH_BLOCKS_WAIT &&
13947 fs->fs_cstotal.cs_nbfree <= needed) ||
13948 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13949 fs->fs_cstotal.cs_nifree <= needed)) {
13950 ACQUIRE_LOCK(ump);
13951 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13952 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13953 FREE_LOCK(ump);
13954 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13955 ACQUIRE_LOCK(ump);
13956 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13957 wakeup(&ump->um_softdep->sd_flags);
13958 FREE_LOCK(ump);
13959 if (ump->softdep_on_worklist > 0) {
13960 stat_cleanup_retries += 1;
13961 if (!failed_vnode)
13962 goto retry;
13963 }
13964 } else {
13965 while ((ump->um_softdep->sd_flags &
13966 FLUSH_RC_ACTIVE) != 0) {
13967 msleep(&ump->um_softdep->sd_flags,
13968 LOCK_PTR(ump), PVM, "ffsrca", hz);
13969 }
13970 FREE_LOCK(ump);
13971 error = 0;
13972 }
13973 stat_cleanup_failures += 1;
13974 }
13975 if (time_second - starttime > stat_cleanup_high_delay)
13976 stat_cleanup_high_delay = time_second - starttime;
13977 UFS_LOCK(ump);
13978 return (error);
13979 }
13980
13981 /*
13982 * Scan the vnodes for the specified mount point flushing out any
13983 * vnodes that can be locked without waiting. Finally, try to flush
13984 * the device associated with the mount point if it can be locked
13985 * without waiting.
13986 *
13987 * We return 0 if we were able to lock every vnode in our scan.
13988 * If we had to skip one or more vnodes, we return 1.
13989 */
13990 static int
softdep_request_cleanup_flush(mp,ump)13991 softdep_request_cleanup_flush(mp, ump)
13992 struct mount *mp;
13993 struct ufsmount *ump;
13994 {
13995 struct thread *td;
13996 struct vnode *lvp, *mvp;
13997 int failed_vnode;
13998
13999 failed_vnode = 0;
14000 td = curthread;
14001 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
14002 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
14003 VI_UNLOCK(lvp);
14004 continue;
14005 }
14006 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) {
14007 failed_vnode = 1;
14008 continue;
14009 }
14010 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
14011 vput(lvp);
14012 continue;
14013 }
14014 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
14015 vput(lvp);
14016 }
14017 lvp = ump->um_devvp;
14018 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
14019 VOP_FSYNC(lvp, MNT_NOWAIT, td);
14020 VOP_UNLOCK(lvp);
14021 }
14022 return (failed_vnode);
14023 }
14024
14025 static bool
softdep_excess_items(struct ufsmount * ump,int item)14026 softdep_excess_items(struct ufsmount *ump, int item)
14027 {
14028
14029 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
14030 return (dep_current[item] > max_softdeps &&
14031 ump->softdep_curdeps[item] > max_softdeps /
14032 stat_flush_threads);
14033 }
14034
14035 static void
schedule_cleanup(struct mount * mp)14036 schedule_cleanup(struct mount *mp)
14037 {
14038 struct ufsmount *ump;
14039 struct thread *td;
14040
14041 ump = VFSTOUFS(mp);
14042 LOCK_OWNED(ump);
14043 FREE_LOCK(ump);
14044 td = curthread;
14045 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
14046 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
14047 /*
14048 * No ast is delivered to kernel threads, so nobody
14049 * would deref the mp. Some kernel threads
14050 * explicitely check for AST, e.g. NFS daemon does
14051 * this in the serving loop.
14052 */
14053 return;
14054 }
14055 if (td->td_su != NULL)
14056 vfs_rel(td->td_su);
14057 vfs_ref(mp);
14058 td->td_su = mp;
14059 thread_lock(td);
14060 td->td_flags |= TDF_ASTPENDING;
14061 thread_unlock(td);
14062 }
14063
14064 static void
softdep_ast_cleanup_proc(struct thread * td)14065 softdep_ast_cleanup_proc(struct thread *td)
14066 {
14067 struct mount *mp;
14068 struct ufsmount *ump;
14069 int error;
14070 bool req;
14071
14072 while ((mp = td->td_su) != NULL) {
14073 td->td_su = NULL;
14074 error = vfs_busy(mp, MBF_NOWAIT);
14075 vfs_rel(mp);
14076 if (error != 0)
14077 return;
14078 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
14079 ump = VFSTOUFS(mp);
14080 for (;;) {
14081 req = false;
14082 ACQUIRE_LOCK(ump);
14083 if (softdep_excess_items(ump, D_INODEDEP)) {
14084 req = true;
14085 request_cleanup(mp, FLUSH_INODES);
14086 }
14087 if (softdep_excess_items(ump, D_DIRREM)) {
14088 req = true;
14089 request_cleanup(mp, FLUSH_BLOCKS);
14090 }
14091 FREE_LOCK(ump);
14092 if (softdep_excess_items(ump, D_NEWBLK) ||
14093 softdep_excess_items(ump, D_ALLOCDIRECT) ||
14094 softdep_excess_items(ump, D_ALLOCINDIR)) {
14095 error = vn_start_write(NULL, &mp,
14096 V_WAIT);
14097 if (error == 0) {
14098 req = true;
14099 VFS_SYNC(mp, MNT_WAIT);
14100 vn_finished_write(mp);
14101 }
14102 }
14103 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
14104 break;
14105 }
14106 }
14107 vfs_unbusy(mp);
14108 }
14109 if ((mp = td->td_su) != NULL) {
14110 td->td_su = NULL;
14111 vfs_rel(mp);
14112 }
14113 }
14114
14115 /*
14116 * If memory utilization has gotten too high, deliberately slow things
14117 * down and speed up the I/O processing.
14118 */
14119 static int
request_cleanup(mp,resource)14120 request_cleanup(mp, resource)
14121 struct mount *mp;
14122 int resource;
14123 {
14124 struct thread *td = curthread;
14125 struct ufsmount *ump;
14126
14127 ump = VFSTOUFS(mp);
14128 LOCK_OWNED(ump);
14129 /*
14130 * We never hold up the filesystem syncer or buf daemon.
14131 */
14132 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
14133 return (0);
14134 /*
14135 * First check to see if the work list has gotten backlogged.
14136 * If it has, co-opt this process to help clean up two entries.
14137 * Because this process may hold inodes locked, we cannot
14138 * handle any remove requests that might block on a locked
14139 * inode as that could lead to deadlock. We set TDP_SOFTDEP
14140 * to avoid recursively processing the worklist.
14141 */
14142 if (ump->softdep_on_worklist > max_softdeps / 10) {
14143 td->td_pflags |= TDP_SOFTDEP;
14144 process_worklist_item(mp, 2, LK_NOWAIT);
14145 td->td_pflags &= ~TDP_SOFTDEP;
14146 stat_worklist_push += 2;
14147 return(1);
14148 }
14149 /*
14150 * Next, we attempt to speed up the syncer process. If that
14151 * is successful, then we allow the process to continue.
14152 */
14153 if (softdep_speedup(ump) &&
14154 resource != FLUSH_BLOCKS_WAIT &&
14155 resource != FLUSH_INODES_WAIT)
14156 return(0);
14157 /*
14158 * If we are resource constrained on inode dependencies, try
14159 * flushing some dirty inodes. Otherwise, we are constrained
14160 * by file deletions, so try accelerating flushes of directories
14161 * with removal dependencies. We would like to do the cleanup
14162 * here, but we probably hold an inode locked at this point and
14163 * that might deadlock against one that we try to clean. So,
14164 * the best that we can do is request the syncer daemon to do
14165 * the cleanup for us.
14166 */
14167 switch (resource) {
14168 case FLUSH_INODES:
14169 case FLUSH_INODES_WAIT:
14170 ACQUIRE_GBLLOCK(&lk);
14171 stat_ino_limit_push += 1;
14172 req_clear_inodedeps += 1;
14173 FREE_GBLLOCK(&lk);
14174 stat_countp = &stat_ino_limit_hit;
14175 break;
14176
14177 case FLUSH_BLOCKS:
14178 case FLUSH_BLOCKS_WAIT:
14179 ACQUIRE_GBLLOCK(&lk);
14180 stat_blk_limit_push += 1;
14181 req_clear_remove += 1;
14182 FREE_GBLLOCK(&lk);
14183 stat_countp = &stat_blk_limit_hit;
14184 break;
14185
14186 default:
14187 panic("request_cleanup: unknown type");
14188 }
14189 /*
14190 * Hopefully the syncer daemon will catch up and awaken us.
14191 * We wait at most tickdelay before proceeding in any case.
14192 */
14193 ACQUIRE_GBLLOCK(&lk);
14194 FREE_LOCK(ump);
14195 proc_waiting += 1;
14196 if (callout_pending(&softdep_callout) == FALSE)
14197 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
14198 pause_timer, 0);
14199
14200 if ((td->td_pflags & TDP_KTHREAD) == 0)
14201 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
14202 proc_waiting -= 1;
14203 FREE_GBLLOCK(&lk);
14204 ACQUIRE_LOCK(ump);
14205 return (1);
14206 }
14207
14208 /*
14209 * Awaken processes pausing in request_cleanup and clear proc_waiting
14210 * to indicate that there is no longer a timer running. Pause_timer
14211 * will be called with the global softdep mutex (&lk) locked.
14212 */
14213 static void
pause_timer(arg)14214 pause_timer(arg)
14215 void *arg;
14216 {
14217
14218 GBLLOCK_OWNED(&lk);
14219 /*
14220 * The callout_ API has acquired mtx and will hold it around this
14221 * function call.
14222 */
14223 *stat_countp += proc_waiting;
14224 wakeup(&proc_waiting);
14225 }
14226
14227 /*
14228 * If requested, try removing inode or removal dependencies.
14229 */
14230 static void
check_clear_deps(mp)14231 check_clear_deps(mp)
14232 struct mount *mp;
14233 {
14234 struct ufsmount *ump;
14235 bool suj_susp;
14236
14237 /*
14238 * Tell the lower layers that any TRIM or WRITE transactions that have
14239 * been delayed for performance reasons should proceed to help alleviate
14240 * the shortage faster. The race between checking req_* and the softdep
14241 * mutex (lk) is fine since this is an advisory operation that at most
14242 * causes deferred work to be done sooner.
14243 */
14244 ump = VFSTOUFS(mp);
14245 suj_susp = ump->um_softdep->sd_jblocks != NULL &&
14246 ump->softdep_jblocks->jb_suspended;
14247 if (req_clear_remove || req_clear_inodedeps || suj_susp) {
14248 FREE_LOCK(ump);
14249 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
14250 ACQUIRE_LOCK(ump);
14251 }
14252
14253 /*
14254 * If we are suspended, it may be because of our using
14255 * too many inodedeps, so help clear them out.
14256 */
14257 if (suj_susp)
14258 clear_inodedeps(mp);
14259
14260 /*
14261 * General requests for cleanup of backed up dependencies
14262 */
14263 ACQUIRE_GBLLOCK(&lk);
14264 if (req_clear_inodedeps) {
14265 req_clear_inodedeps -= 1;
14266 FREE_GBLLOCK(&lk);
14267 clear_inodedeps(mp);
14268 ACQUIRE_GBLLOCK(&lk);
14269 wakeup(&proc_waiting);
14270 }
14271 if (req_clear_remove) {
14272 req_clear_remove -= 1;
14273 FREE_GBLLOCK(&lk);
14274 clear_remove(mp);
14275 ACQUIRE_GBLLOCK(&lk);
14276 wakeup(&proc_waiting);
14277 }
14278 FREE_GBLLOCK(&lk);
14279 }
14280
14281 /*
14282 * Flush out a directory with at least one removal dependency in an effort to
14283 * reduce the number of dirrem, freefile, and freeblks dependency structures.
14284 */
14285 static void
clear_remove(mp)14286 clear_remove(mp)
14287 struct mount *mp;
14288 {
14289 struct pagedep_hashhead *pagedephd;
14290 struct pagedep *pagedep;
14291 struct ufsmount *ump;
14292 struct vnode *vp;
14293 struct bufobj *bo;
14294 int error, cnt;
14295 ino_t ino;
14296
14297 ump = VFSTOUFS(mp);
14298 LOCK_OWNED(ump);
14299
14300 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
14301 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
14302 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
14303 ump->pagedep_nextclean = 0;
14304 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
14305 if (LIST_EMPTY(&pagedep->pd_dirremhd))
14306 continue;
14307 ino = pagedep->pd_ino;
14308 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14309 continue;
14310 FREE_LOCK(ump);
14311
14312 /*
14313 * Let unmount clear deps
14314 */
14315 error = vfs_busy(mp, MBF_NOWAIT);
14316 if (error != 0)
14317 goto finish_write;
14318 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14319 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
14320 vfs_unbusy(mp);
14321 if (error != 0) {
14322 softdep_error("clear_remove: vget", error);
14323 goto finish_write;
14324 }
14325 MPASS(VTOI(vp)->i_mode != 0);
14326 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14327 softdep_error("clear_remove: fsync", error);
14328 bo = &vp->v_bufobj;
14329 BO_LOCK(bo);
14330 drain_output(vp);
14331 BO_UNLOCK(bo);
14332 vput(vp);
14333 finish_write:
14334 vn_finished_write(mp);
14335 ACQUIRE_LOCK(ump);
14336 return;
14337 }
14338 }
14339 }
14340
14341 /*
14342 * Clear out a block of dirty inodes in an effort to reduce
14343 * the number of inodedep dependency structures.
14344 */
14345 static void
clear_inodedeps(mp)14346 clear_inodedeps(mp)
14347 struct mount *mp;
14348 {
14349 struct inodedep_hashhead *inodedephd;
14350 struct inodedep *inodedep;
14351 struct ufsmount *ump;
14352 struct vnode *vp;
14353 struct fs *fs;
14354 int error, cnt;
14355 ino_t firstino, lastino, ino;
14356
14357 ump = VFSTOUFS(mp);
14358 fs = ump->um_fs;
14359 LOCK_OWNED(ump);
14360 /*
14361 * Pick a random inode dependency to be cleared.
14362 * We will then gather up all the inodes in its block
14363 * that have dependencies and flush them out.
14364 */
14365 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
14366 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
14367 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
14368 ump->inodedep_nextclean = 0;
14369 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
14370 break;
14371 }
14372 if (inodedep == NULL)
14373 return;
14374 /*
14375 * Find the last inode in the block with dependencies.
14376 */
14377 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
14378 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
14379 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
14380 break;
14381 /*
14382 * Asynchronously push all but the last inode with dependencies.
14383 * Synchronously push the last inode with dependencies to ensure
14384 * that the inode block gets written to free up the inodedeps.
14385 */
14386 for (ino = firstino; ino <= lastino; ino++) {
14387 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
14388 continue;
14389 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14390 continue;
14391 FREE_LOCK(ump);
14392 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
14393 if (error != 0) {
14394 vn_finished_write(mp);
14395 ACQUIRE_LOCK(ump);
14396 return;
14397 }
14398 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14399 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) {
14400 softdep_error("clear_inodedeps: vget", error);
14401 vfs_unbusy(mp);
14402 vn_finished_write(mp);
14403 ACQUIRE_LOCK(ump);
14404 return;
14405 }
14406 vfs_unbusy(mp);
14407 if (VTOI(vp)->i_mode == 0) {
14408 vgone(vp);
14409 } else if (ino == lastino) {
14410 do {
14411 error = ffs_syncvnode(vp, MNT_WAIT, 0);
14412 } while (error == ERELOOKUP);
14413 if (error != 0)
14414 softdep_error("clear_inodedeps: fsync1", error);
14415 } else {
14416 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14417 softdep_error("clear_inodedeps: fsync2", error);
14418 BO_LOCK(&vp->v_bufobj);
14419 drain_output(vp);
14420 BO_UNLOCK(&vp->v_bufobj);
14421 }
14422 vput(vp);
14423 vn_finished_write(mp);
14424 ACQUIRE_LOCK(ump);
14425 }
14426 }
14427
14428 void
softdep_buf_append(bp,wkhd)14429 softdep_buf_append(bp, wkhd)
14430 struct buf *bp;
14431 struct workhead *wkhd;
14432 {
14433 struct worklist *wk;
14434 struct ufsmount *ump;
14435
14436 if ((wk = LIST_FIRST(wkhd)) == NULL)
14437 return;
14438 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14439 ("softdep_buf_append called on non-softdep filesystem"));
14440 ump = VFSTOUFS(wk->wk_mp);
14441 ACQUIRE_LOCK(ump);
14442 while ((wk = LIST_FIRST(wkhd)) != NULL) {
14443 WORKLIST_REMOVE(wk);
14444 WORKLIST_INSERT(&bp->b_dep, wk);
14445 }
14446 FREE_LOCK(ump);
14447
14448 }
14449
14450 void
softdep_inode_append(ip,cred,wkhd)14451 softdep_inode_append(ip, cred, wkhd)
14452 struct inode *ip;
14453 struct ucred *cred;
14454 struct workhead *wkhd;
14455 {
14456 struct buf *bp;
14457 struct fs *fs;
14458 struct ufsmount *ump;
14459 int error;
14460
14461 ump = ITOUMP(ip);
14462 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14463 ("softdep_inode_append called on non-softdep filesystem"));
14464 fs = ump->um_fs;
14465 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14466 (int)fs->fs_bsize, cred, &bp);
14467 if (error) {
14468 bqrelse(bp);
14469 softdep_freework(wkhd);
14470 return;
14471 }
14472 softdep_buf_append(bp, wkhd);
14473 bqrelse(bp);
14474 }
14475
14476 void
softdep_freework(wkhd)14477 softdep_freework(wkhd)
14478 struct workhead *wkhd;
14479 {
14480 struct worklist *wk;
14481 struct ufsmount *ump;
14482
14483 if ((wk = LIST_FIRST(wkhd)) == NULL)
14484 return;
14485 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14486 ("softdep_freework called on non-softdep filesystem"));
14487 ump = VFSTOUFS(wk->wk_mp);
14488 ACQUIRE_LOCK(ump);
14489 handle_jwork(wkhd);
14490 FREE_LOCK(ump);
14491 }
14492
14493 static struct ufsmount *
softdep_bp_to_mp(bp)14494 softdep_bp_to_mp(bp)
14495 struct buf *bp;
14496 {
14497 struct mount *mp;
14498 struct vnode *vp;
14499
14500 if (LIST_EMPTY(&bp->b_dep))
14501 return (NULL);
14502 vp = bp->b_vp;
14503 KASSERT(vp != NULL,
14504 ("%s, buffer with dependencies lacks vnode", __func__));
14505
14506 /*
14507 * The ump mount point is stable after we get a correct
14508 * pointer, since bp is locked and this prevents unmount from
14509 * proceeding. But to get to it, we cannot dereference bp->b_dep
14510 * head wk_mp, because we do not yet own SU ump lock and
14511 * workitem might be freed while dereferenced.
14512 */
14513 retry:
14514 switch (vp->v_type) {
14515 case VCHR:
14516 VI_LOCK(vp);
14517 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14518 VI_UNLOCK(vp);
14519 if (mp == NULL)
14520 goto retry;
14521 break;
14522 case VREG:
14523 case VDIR:
14524 case VLNK:
14525 case VFIFO:
14526 case VSOCK:
14527 mp = vp->v_mount;
14528 break;
14529 case VBLK:
14530 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14531 /* FALLTHROUGH */
14532 case VNON:
14533 case VBAD:
14534 case VMARKER:
14535 mp = NULL;
14536 break;
14537 default:
14538 vn_printf(vp, "unknown vnode type");
14539 mp = NULL;
14540 break;
14541 }
14542 return (VFSTOUFS(mp));
14543 }
14544
14545 /*
14546 * Function to determine if the buffer has outstanding dependencies
14547 * that will cause a roll-back if the buffer is written. If wantcount
14548 * is set, return number of dependencies, otherwise just yes or no.
14549 */
14550 static int
softdep_count_dependencies(bp,wantcount)14551 softdep_count_dependencies(bp, wantcount)
14552 struct buf *bp;
14553 int wantcount;
14554 {
14555 struct worklist *wk;
14556 struct ufsmount *ump;
14557 struct bmsafemap *bmsafemap;
14558 struct freework *freework;
14559 struct inodedep *inodedep;
14560 struct indirdep *indirdep;
14561 struct freeblks *freeblks;
14562 struct allocindir *aip;
14563 struct pagedep *pagedep;
14564 struct dirrem *dirrem;
14565 struct newblk *newblk;
14566 struct mkdir *mkdir;
14567 struct diradd *dap;
14568 int i, retval;
14569
14570 ump = softdep_bp_to_mp(bp);
14571 if (ump == NULL)
14572 return (0);
14573 retval = 0;
14574 ACQUIRE_LOCK(ump);
14575 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14576 switch (wk->wk_type) {
14577 case D_INODEDEP:
14578 inodedep = WK_INODEDEP(wk);
14579 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14580 /* bitmap allocation dependency */
14581 retval += 1;
14582 if (!wantcount)
14583 goto out;
14584 }
14585 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14586 /* direct block pointer dependency */
14587 retval += 1;
14588 if (!wantcount)
14589 goto out;
14590 }
14591 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14592 /* direct block pointer dependency */
14593 retval += 1;
14594 if (!wantcount)
14595 goto out;
14596 }
14597 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14598 /* Add reference dependency. */
14599 retval += 1;
14600 if (!wantcount)
14601 goto out;
14602 }
14603 continue;
14604
14605 case D_INDIRDEP:
14606 indirdep = WK_INDIRDEP(wk);
14607
14608 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14609 /* indirect truncation dependency */
14610 retval += 1;
14611 if (!wantcount)
14612 goto out;
14613 }
14614
14615 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14616 /* indirect block pointer dependency */
14617 retval += 1;
14618 if (!wantcount)
14619 goto out;
14620 }
14621 continue;
14622
14623 case D_PAGEDEP:
14624 pagedep = WK_PAGEDEP(wk);
14625 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14626 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14627 /* Journal remove ref dependency. */
14628 retval += 1;
14629 if (!wantcount)
14630 goto out;
14631 }
14632 }
14633 for (i = 0; i < DAHASHSZ; i++) {
14634 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14635 /* directory entry dependency */
14636 retval += 1;
14637 if (!wantcount)
14638 goto out;
14639 }
14640 }
14641 continue;
14642
14643 case D_BMSAFEMAP:
14644 bmsafemap = WK_BMSAFEMAP(wk);
14645 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14646 /* Add reference dependency. */
14647 retval += 1;
14648 if (!wantcount)
14649 goto out;
14650 }
14651 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14652 /* Allocate block dependency. */
14653 retval += 1;
14654 if (!wantcount)
14655 goto out;
14656 }
14657 continue;
14658
14659 case D_FREEBLKS:
14660 freeblks = WK_FREEBLKS(wk);
14661 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14662 /* Freeblk journal dependency. */
14663 retval += 1;
14664 if (!wantcount)
14665 goto out;
14666 }
14667 continue;
14668
14669 case D_ALLOCDIRECT:
14670 case D_ALLOCINDIR:
14671 newblk = WK_NEWBLK(wk);
14672 if (newblk->nb_jnewblk) {
14673 /* Journal allocate dependency. */
14674 retval += 1;
14675 if (!wantcount)
14676 goto out;
14677 }
14678 continue;
14679
14680 case D_MKDIR:
14681 mkdir = WK_MKDIR(wk);
14682 if (mkdir->md_jaddref) {
14683 /* Journal reference dependency. */
14684 retval += 1;
14685 if (!wantcount)
14686 goto out;
14687 }
14688 continue;
14689
14690 case D_FREEWORK:
14691 case D_FREEDEP:
14692 case D_JSEGDEP:
14693 case D_JSEG:
14694 case D_SBDEP:
14695 /* never a dependency on these blocks */
14696 continue;
14697
14698 default:
14699 panic("softdep_count_dependencies: Unexpected type %s",
14700 TYPENAME(wk->wk_type));
14701 /* NOTREACHED */
14702 }
14703 }
14704 out:
14705 FREE_LOCK(ump);
14706 return (retval);
14707 }
14708
14709 /*
14710 * Acquire exclusive access to a buffer.
14711 * Must be called with a locked mtx parameter.
14712 * Return acquired buffer or NULL on failure.
14713 */
14714 static struct buf *
getdirtybuf(bp,lock,waitfor)14715 getdirtybuf(bp, lock, waitfor)
14716 struct buf *bp;
14717 struct rwlock *lock;
14718 int waitfor;
14719 {
14720 int error;
14721
14722 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14723 if (waitfor != MNT_WAIT)
14724 return (NULL);
14725 error = BUF_LOCK(bp,
14726 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14727 /*
14728 * Even if we successfully acquire bp here, we have dropped
14729 * lock, which may violates our guarantee.
14730 */
14731 if (error == 0)
14732 BUF_UNLOCK(bp);
14733 else if (error != ENOLCK)
14734 panic("getdirtybuf: inconsistent lock: %d", error);
14735 rw_wlock(lock);
14736 return (NULL);
14737 }
14738 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14739 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14740 rw_wunlock(lock);
14741 BO_LOCK(bp->b_bufobj);
14742 BUF_UNLOCK(bp);
14743 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14744 bp->b_vflags |= BV_BKGRDWAIT;
14745 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14746 PRIBIO | PDROP, "getbuf", 0);
14747 } else
14748 BO_UNLOCK(bp->b_bufobj);
14749 rw_wlock(lock);
14750 return (NULL);
14751 }
14752 BUF_UNLOCK(bp);
14753 if (waitfor != MNT_WAIT)
14754 return (NULL);
14755 #ifdef DEBUG_VFS_LOCKS
14756 if (bp->b_vp->v_type != VCHR)
14757 ASSERT_BO_WLOCKED(bp->b_bufobj);
14758 #endif
14759 bp->b_vflags |= BV_BKGRDWAIT;
14760 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14761 return (NULL);
14762 }
14763 if ((bp->b_flags & B_DELWRI) == 0) {
14764 BUF_UNLOCK(bp);
14765 return (NULL);
14766 }
14767 bremfree(bp);
14768 return (bp);
14769 }
14770
14771 /*
14772 * Check if it is safe to suspend the file system now. On entry,
14773 * the vnode interlock for devvp should be held. Return 0 with
14774 * the mount interlock held if the file system can be suspended now,
14775 * otherwise return EAGAIN with the mount interlock held.
14776 */
14777 int
softdep_check_suspend(struct mount * mp,struct vnode * devvp,int softdep_depcnt,int softdep_accdepcnt,int secondary_writes,int secondary_accwrites)14778 softdep_check_suspend(struct mount *mp,
14779 struct vnode *devvp,
14780 int softdep_depcnt,
14781 int softdep_accdepcnt,
14782 int secondary_writes,
14783 int secondary_accwrites)
14784 {
14785 struct buf *bp;
14786 struct bufobj *bo;
14787 struct ufsmount *ump;
14788 struct inodedep *inodedep;
14789 struct indirdep *indirdep;
14790 struct worklist *wk, *nextwk;
14791 int error, unlinked;
14792
14793 bo = &devvp->v_bufobj;
14794 ASSERT_BO_WLOCKED(bo);
14795
14796 /*
14797 * If we are not running with soft updates, then we need only
14798 * deal with secondary writes as we try to suspend.
14799 */
14800 if (MOUNTEDSOFTDEP(mp) == 0) {
14801 MNT_ILOCK(mp);
14802 while (mp->mnt_secondary_writes != 0) {
14803 BO_UNLOCK(bo);
14804 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14805 (PUSER - 1) | PDROP, "secwr", 0);
14806 BO_LOCK(bo);
14807 MNT_ILOCK(mp);
14808 }
14809
14810 /*
14811 * Reasons for needing more work before suspend:
14812 * - Dirty buffers on devvp.
14813 * - Secondary writes occurred after start of vnode sync loop
14814 */
14815 error = 0;
14816 if (bo->bo_numoutput > 0 ||
14817 bo->bo_dirty.bv_cnt > 0 ||
14818 secondary_writes != 0 ||
14819 mp->mnt_secondary_writes != 0 ||
14820 secondary_accwrites != mp->mnt_secondary_accwrites)
14821 error = EAGAIN;
14822 BO_UNLOCK(bo);
14823 return (error);
14824 }
14825
14826 /*
14827 * If we are running with soft updates, then we need to coordinate
14828 * with them as we try to suspend.
14829 */
14830 ump = VFSTOUFS(mp);
14831 for (;;) {
14832 if (!TRY_ACQUIRE_LOCK(ump)) {
14833 BO_UNLOCK(bo);
14834 ACQUIRE_LOCK(ump);
14835 FREE_LOCK(ump);
14836 BO_LOCK(bo);
14837 continue;
14838 }
14839 MNT_ILOCK(mp);
14840 if (mp->mnt_secondary_writes != 0) {
14841 FREE_LOCK(ump);
14842 BO_UNLOCK(bo);
14843 msleep(&mp->mnt_secondary_writes,
14844 MNT_MTX(mp),
14845 (PUSER - 1) | PDROP, "secwr", 0);
14846 BO_LOCK(bo);
14847 continue;
14848 }
14849 break;
14850 }
14851
14852 unlinked = 0;
14853 if (MOUNTEDSUJ(mp)) {
14854 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14855 inodedep != NULL;
14856 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14857 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14858 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14859 UNLINKONLIST) ||
14860 !check_inodedep_free(inodedep))
14861 continue;
14862 unlinked++;
14863 }
14864 }
14865
14866 /*
14867 * XXX Check for orphaned indirdep dependency structures.
14868 *
14869 * During forcible unmount after a disk failure there is a
14870 * bug that causes one or more indirdep dependency structures
14871 * to fail to be deallocated. We check for them here and clean
14872 * them up so that the unmount can succeed.
14873 */
14874 if ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0 && ump->softdep_deps > 0 &&
14875 ump->softdep_deps == ump->softdep_curdeps[D_INDIRDEP]) {
14876 LIST_FOREACH_SAFE(wk, &ump->softdep_alldeps[D_INDIRDEP],
14877 wk_all, nextwk) {
14878 indirdep = WK_INDIRDEP(wk);
14879 if ((indirdep->ir_state & (GOINGAWAY | DEPCOMPLETE)) !=
14880 (GOINGAWAY | DEPCOMPLETE) ||
14881 !TAILQ_EMPTY(&indirdep->ir_trunc) ||
14882 !LIST_EMPTY(&indirdep->ir_completehd) ||
14883 !LIST_EMPTY(&indirdep->ir_writehd) ||
14884 !LIST_EMPTY(&indirdep->ir_donehd) ||
14885 !LIST_EMPTY(&indirdep->ir_deplisthd) ||
14886 indirdep->ir_saveddata != NULL ||
14887 indirdep->ir_savebp == NULL) {
14888 printf("%s: skipping orphaned indirdep %p\n",
14889 __FUNCTION__, indirdep);
14890 continue;
14891 }
14892 printf("%s: freeing orphaned indirdep %p\n",
14893 __FUNCTION__, indirdep);
14894 bp = indirdep->ir_savebp;
14895 indirdep->ir_savebp = NULL;
14896 free_indirdep(indirdep);
14897 FREE_LOCK(ump);
14898 brelse(bp);
14899 while (!TRY_ACQUIRE_LOCK(ump)) {
14900 BO_UNLOCK(bo);
14901 ACQUIRE_LOCK(ump);
14902 FREE_LOCK(ump);
14903 BO_LOCK(bo);
14904 }
14905 }
14906 }
14907
14908 /*
14909 * Reasons for needing more work before suspend:
14910 * - Dirty buffers on devvp.
14911 * - Dependency structures still exist
14912 * - Softdep activity occurred after start of vnode sync loop
14913 * - Secondary writes occurred after start of vnode sync loop
14914 */
14915 error = 0;
14916 if (bo->bo_numoutput > 0 ||
14917 bo->bo_dirty.bv_cnt > 0 ||
14918 softdep_depcnt != unlinked ||
14919 ump->softdep_deps != unlinked ||
14920 softdep_accdepcnt != ump->softdep_accdeps ||
14921 secondary_writes != 0 ||
14922 mp->mnt_secondary_writes != 0 ||
14923 secondary_accwrites != mp->mnt_secondary_accwrites)
14924 error = EAGAIN;
14925 FREE_LOCK(ump);
14926 BO_UNLOCK(bo);
14927 return (error);
14928 }
14929
14930 /*
14931 * Get the number of dependency structures for the file system, both
14932 * the current number and the total number allocated. These will
14933 * later be used to detect that softdep processing has occurred.
14934 */
14935 void
softdep_get_depcounts(struct mount * mp,int * softdep_depsp,int * softdep_accdepsp)14936 softdep_get_depcounts(struct mount *mp,
14937 int *softdep_depsp,
14938 int *softdep_accdepsp)
14939 {
14940 struct ufsmount *ump;
14941
14942 if (MOUNTEDSOFTDEP(mp) == 0) {
14943 *softdep_depsp = 0;
14944 *softdep_accdepsp = 0;
14945 return;
14946 }
14947 ump = VFSTOUFS(mp);
14948 ACQUIRE_LOCK(ump);
14949 *softdep_depsp = ump->softdep_deps;
14950 *softdep_accdepsp = ump->softdep_accdeps;
14951 FREE_LOCK(ump);
14952 }
14953
14954 /*
14955 * Wait for pending output on a vnode to complete.
14956 */
14957 static void
drain_output(vp)14958 drain_output(vp)
14959 struct vnode *vp;
14960 {
14961
14962 ASSERT_VOP_LOCKED(vp, "drain_output");
14963 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14964 }
14965
14966 /*
14967 * Called whenever a buffer that is being invalidated or reallocated
14968 * contains dependencies. This should only happen if an I/O error has
14969 * occurred. The routine is called with the buffer locked.
14970 */
14971 static void
softdep_deallocate_dependencies(bp)14972 softdep_deallocate_dependencies(bp)
14973 struct buf *bp;
14974 {
14975
14976 if ((bp->b_ioflags & BIO_ERROR) == 0)
14977 panic("softdep_deallocate_dependencies: dangling deps");
14978 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14979 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14980 else
14981 printf("softdep_deallocate_dependencies: "
14982 "got error %d while accessing filesystem\n", bp->b_error);
14983 if (bp->b_error != ENXIO)
14984 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14985 }
14986
14987 /*
14988 * Function to handle asynchronous write errors in the filesystem.
14989 */
14990 static void
softdep_error(func,error)14991 softdep_error(func, error)
14992 char *func;
14993 int error;
14994 {
14995
14996 /* XXX should do something better! */
14997 printf("%s: got error %d while accessing filesystem\n", func, error);
14998 }
14999
15000 #ifdef DDB
15001
15002 /* exported to ffs_vfsops.c */
15003 extern void db_print_ffs(struct ufsmount *ump);
15004 void
db_print_ffs(struct ufsmount * ump)15005 db_print_ffs(struct ufsmount *ump)
15006 {
15007 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
15008 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
15009 db_printf(" fs %p ", ump->um_fs);
15010
15011 if (ump->um_softdep != NULL) {
15012 db_printf("su_wl %d su_deps %d su_req %d\n",
15013 ump->softdep_on_worklist, ump->softdep_deps,
15014 ump->softdep_req);
15015 } else {
15016 db_printf("su disabled\n");
15017 }
15018 }
15019
15020 static void
worklist_print(struct worklist * wk,int verbose)15021 worklist_print(struct worklist *wk, int verbose)
15022 {
15023
15024 if (!verbose) {
15025 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
15026 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
15027 return;
15028 }
15029 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
15030 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
15031 LIST_NEXT(wk, wk_list));
15032 db_print_ffs(VFSTOUFS(wk->wk_mp));
15033 }
15034
15035 static void
inodedep_print(struct inodedep * inodedep,int verbose)15036 inodedep_print(struct inodedep *inodedep, int verbose)
15037 {
15038
15039 worklist_print(&inodedep->id_list, 0);
15040 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
15041 inodedep->id_fs,
15042 (intmax_t)inodedep->id_ino,
15043 (intmax_t)fsbtodb(inodedep->id_fs,
15044 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
15045 (intmax_t)inodedep->id_nlinkdelta,
15046 (intmax_t)inodedep->id_savednlink);
15047
15048 if (verbose == 0)
15049 return;
15050
15051 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
15052 inodedep->id_bmsafemap,
15053 inodedep->id_mkdiradd,
15054 TAILQ_FIRST(&inodedep->id_inoreflst));
15055 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
15056 LIST_FIRST(&inodedep->id_dirremhd),
15057 LIST_FIRST(&inodedep->id_pendinghd),
15058 LIST_FIRST(&inodedep->id_bufwait));
15059 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
15060 LIST_FIRST(&inodedep->id_inowait),
15061 TAILQ_FIRST(&inodedep->id_inoupdt),
15062 TAILQ_FIRST(&inodedep->id_newinoupdt));
15063 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
15064 TAILQ_FIRST(&inodedep->id_extupdt),
15065 TAILQ_FIRST(&inodedep->id_newextupdt),
15066 TAILQ_FIRST(&inodedep->id_freeblklst));
15067 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
15068 inodedep->id_savedino1,
15069 (intmax_t)inodedep->id_savedsize,
15070 (intmax_t)inodedep->id_savedextsize);
15071 }
15072
15073 static void
newblk_print(struct newblk * nbp)15074 newblk_print(struct newblk *nbp)
15075 {
15076
15077 worklist_print(&nbp->nb_list, 0);
15078 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
15079 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
15080 &nbp->nb_jnewblk,
15081 &nbp->nb_bmsafemap,
15082 &nbp->nb_freefrag);
15083 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
15084 LIST_FIRST(&nbp->nb_indirdeps),
15085 LIST_FIRST(&nbp->nb_newdirblk),
15086 LIST_FIRST(&nbp->nb_jwork));
15087 }
15088
15089 static void
allocdirect_print(struct allocdirect * adp)15090 allocdirect_print(struct allocdirect *adp)
15091 {
15092
15093 newblk_print(&adp->ad_block);
15094 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
15095 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
15096 db_printf(" offset %d, inodedep %p\n",
15097 adp->ad_offset, adp->ad_inodedep);
15098 }
15099
15100 static void
allocindir_print(struct allocindir * aip)15101 allocindir_print(struct allocindir *aip)
15102 {
15103
15104 newblk_print(&aip->ai_block);
15105 db_printf(" oldblkno %jd, lbn %jd\n",
15106 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
15107 db_printf(" offset %d, indirdep %p\n",
15108 aip->ai_offset, aip->ai_indirdep);
15109 }
15110
15111 static void
mkdir_print(struct mkdir * mkdir)15112 mkdir_print(struct mkdir *mkdir)
15113 {
15114
15115 worklist_print(&mkdir->md_list, 0);
15116 db_printf(" diradd %p, jaddref %p, buf %p\n",
15117 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
15118 }
15119
DB_SHOW_COMMAND(sd_inodedep,db_show_sd_inodedep)15120 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
15121 {
15122
15123 if (have_addr == 0) {
15124 db_printf("inodedep address required\n");
15125 return;
15126 }
15127 inodedep_print((struct inodedep*)addr, 1);
15128 }
15129
DB_SHOW_COMMAND(sd_allinodedeps,db_show_sd_allinodedeps)15130 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
15131 {
15132 struct inodedep_hashhead *inodedephd;
15133 struct inodedep *inodedep;
15134 struct ufsmount *ump;
15135 int cnt;
15136
15137 if (have_addr == 0) {
15138 db_printf("ufsmount address required\n");
15139 return;
15140 }
15141 ump = (struct ufsmount *)addr;
15142 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
15143 inodedephd = &ump->inodedep_hashtbl[cnt];
15144 LIST_FOREACH(inodedep, inodedephd, id_hash) {
15145 inodedep_print(inodedep, 0);
15146 }
15147 }
15148 }
15149
DB_SHOW_COMMAND(sd_worklist,db_show_sd_worklist)15150 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
15151 {
15152
15153 if (have_addr == 0) {
15154 db_printf("worklist address required\n");
15155 return;
15156 }
15157 worklist_print((struct worklist *)addr, 1);
15158 }
15159
DB_SHOW_COMMAND(sd_workhead,db_show_sd_workhead)15160 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
15161 {
15162 struct worklist *wk;
15163 struct workhead *wkhd;
15164
15165 if (have_addr == 0) {
15166 db_printf("worklist address required "
15167 "(for example value in bp->b_dep)\n");
15168 return;
15169 }
15170 /*
15171 * We often do not have the address of the worklist head but
15172 * instead a pointer to its first entry (e.g., we have the
15173 * contents of bp->b_dep rather than &bp->b_dep). But the back
15174 * pointer of bp->b_dep will point at the head of the list, so
15175 * we cheat and use that instead. If we are in the middle of
15176 * a list we will still get the same result, so nothing
15177 * unexpected will result.
15178 */
15179 wk = (struct worklist *)addr;
15180 if (wk == NULL)
15181 return;
15182 wkhd = (struct workhead *)wk->wk_list.le_prev;
15183 LIST_FOREACH(wk, wkhd, wk_list) {
15184 switch(wk->wk_type) {
15185 case D_INODEDEP:
15186 inodedep_print(WK_INODEDEP(wk), 0);
15187 continue;
15188 case D_ALLOCDIRECT:
15189 allocdirect_print(WK_ALLOCDIRECT(wk));
15190 continue;
15191 case D_ALLOCINDIR:
15192 allocindir_print(WK_ALLOCINDIR(wk));
15193 continue;
15194 case D_MKDIR:
15195 mkdir_print(WK_MKDIR(wk));
15196 continue;
15197 default:
15198 worklist_print(wk, 0);
15199 continue;
15200 }
15201 }
15202 }
15203
DB_SHOW_COMMAND(sd_mkdir,db_show_sd_mkdir)15204 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
15205 {
15206 if (have_addr == 0) {
15207 db_printf("mkdir address required\n");
15208 return;
15209 }
15210 mkdir_print((struct mkdir *)addr);
15211 }
15212
DB_SHOW_COMMAND(sd_mkdir_list,db_show_sd_mkdir_list)15213 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
15214 {
15215 struct mkdirlist *mkdirlisthd;
15216 struct mkdir *mkdir;
15217
15218 if (have_addr == 0) {
15219 db_printf("mkdir listhead address required\n");
15220 return;
15221 }
15222 mkdirlisthd = (struct mkdirlist *)addr;
15223 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
15224 mkdir_print(mkdir);
15225 if (mkdir->md_diradd != NULL) {
15226 db_printf(" ");
15227 worklist_print(&mkdir->md_diradd->da_list, 0);
15228 }
15229 if (mkdir->md_jaddref != NULL) {
15230 db_printf(" ");
15231 worklist_print(&mkdir->md_jaddref->ja_list, 0);
15232 }
15233 }
15234 }
15235
DB_SHOW_COMMAND(sd_allocdirect,db_show_sd_allocdirect)15236 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
15237 {
15238 if (have_addr == 0) {
15239 db_printf("allocdirect address required\n");
15240 return;
15241 }
15242 allocdirect_print((struct allocdirect *)addr);
15243 }
15244
DB_SHOW_COMMAND(sd_allocindir,db_show_sd_allocindir)15245 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
15246 {
15247 if (have_addr == 0) {
15248 db_printf("allocindir address required\n");
15249 return;
15250 }
15251 allocindir_print((struct allocindir *)addr);
15252 }
15253
15254 #endif /* DDB */
15255
15256 #endif /* SOFTUPDATES */
15257