1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 1998, 2000 Marshall Kirk McKusick.
5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
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 mckusick@mckusick.com
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 #include "opt_ffs.h"
46 #include "opt_quota.h"
47 #include "opt_ddb.h"
48
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/kdb.h>
55 #include <sys/kthread.h>
56 #include <sys/ktr.h>
57 #include <sys/limits.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 #include <sys/racct.h>
66 #include <sys/rwlock.h>
67 #include <sys/stat.h>
68 #include <sys/sysctl.h>
69 #include <sys/syslog.h>
70 #include <sys/vnode.h>
71 #include <sys/conf.h>
72
73 #include <ufs/ufs/dir.h>
74 #include <ufs/ufs/extattr.h>
75 #include <ufs/ufs/quota.h>
76 #include <ufs/ufs/inode.h>
77 #include <ufs/ufs/ufsmount.h>
78 #include <ufs/ffs/fs.h>
79 #include <ufs/ffs/softdep.h>
80 #include <ufs/ffs/ffs_extern.h>
81 #include <ufs/ufs/ufs_extern.h>
82
83 #include <vm/vm.h>
84 #include <vm/vm_extern.h>
85 #include <vm/vm_object.h>
86
87 #include <geom/geom.h>
88 #include <geom/geom_vfs.h>
89
90 #include <ddb/ddb.h>
91
92 #define KTR_SUJ 0 /* Define to KTR_SPARE. */
93
94 #ifndef SOFTUPDATES
95
96 int
softdep_flushfiles(struct mount * oldmnt,int flags,struct thread * td)97 softdep_flushfiles(struct mount *oldmnt,
98 int flags,
99 struct thread *td)
100 {
101
102 panic("softdep_flushfiles called");
103 }
104
105 int
softdep_mount(struct vnode * devvp,struct mount * mp,struct fs * fs,struct ucred * cred)106 softdep_mount(struct vnode *devvp,
107 struct mount *mp,
108 struct fs *fs,
109 struct ucred *cred)
110 {
111
112 return (0);
113 }
114
115 void
softdep_initialize(void)116 softdep_initialize(void)
117 {
118
119 return;
120 }
121
122 void
softdep_uninitialize(void)123 softdep_uninitialize(void)
124 {
125
126 return;
127 }
128
129 void
softdep_unmount(struct mount * mp)130 softdep_unmount(struct mount *mp)
131 {
132
133 panic("softdep_unmount called");
134 }
135
136 void
softdep_setup_sbupdate(struct ufsmount * ump,struct fs * fs,struct buf * bp)137 softdep_setup_sbupdate(struct ufsmount *ump,
138 struct fs *fs,
139 struct buf *bp)
140 {
141
142 panic("softdep_setup_sbupdate called");
143 }
144
145 void
softdep_setup_inomapdep(struct buf * bp,struct inode * ip,ino_t newinum,int mode)146 softdep_setup_inomapdep(struct buf *bp,
147 struct inode *ip,
148 ino_t newinum,
149 int mode)
150 {
151
152 panic("softdep_setup_inomapdep called");
153 }
154
155 void
softdep_setup_blkmapdep(struct buf * bp,struct mount * mp,ufs2_daddr_t newblkno,int frags,int oldfrags)156 softdep_setup_blkmapdep(struct buf *bp,
157 struct mount *mp,
158 ufs2_daddr_t newblkno,
159 int frags,
160 int oldfrags)
161 {
162
163 panic("softdep_setup_blkmapdep called");
164 }
165
166 void
softdep_setup_allocdirect(struct inode * ip,ufs_lbn_t lbn,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,long newsize,long oldsize,struct buf * bp)167 softdep_setup_allocdirect(struct inode *ip,
168 ufs_lbn_t lbn,
169 ufs2_daddr_t newblkno,
170 ufs2_daddr_t oldblkno,
171 long newsize,
172 long oldsize,
173 struct buf *bp)
174 {
175
176 panic("softdep_setup_allocdirect called");
177 }
178
179 void
softdep_setup_allocext(struct inode * ip,ufs_lbn_t lbn,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,long newsize,long oldsize,struct buf * bp)180 softdep_setup_allocext(struct inode *ip,
181 ufs_lbn_t lbn,
182 ufs2_daddr_t newblkno,
183 ufs2_daddr_t oldblkno,
184 long newsize,
185 long oldsize,
186 struct buf *bp)
187 {
188
189 panic("softdep_setup_allocext called");
190 }
191
192 void
softdep_setup_allocindir_page(struct inode * ip,ufs_lbn_t lbn,struct buf * bp,int ptrno,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,struct buf * nbp)193 softdep_setup_allocindir_page(struct inode *ip,
194 ufs_lbn_t lbn,
195 struct buf *bp,
196 int ptrno,
197 ufs2_daddr_t newblkno,
198 ufs2_daddr_t oldblkno,
199 struct buf *nbp)
200 {
201
202 panic("softdep_setup_allocindir_page called");
203 }
204
205 void
softdep_setup_allocindir_meta(struct buf * nbp,struct inode * ip,struct buf * bp,int ptrno,ufs2_daddr_t newblkno)206 softdep_setup_allocindir_meta(struct buf *nbp,
207 struct inode *ip,
208 struct buf *bp,
209 int ptrno,
210 ufs2_daddr_t newblkno)
211 {
212
213 panic("softdep_setup_allocindir_meta called");
214 }
215
216 void
softdep_journal_freeblocks(struct inode * ip,struct ucred * cred,off_t length,int flags)217 softdep_journal_freeblocks(struct inode *ip,
218 struct ucred *cred,
219 off_t length,
220 int flags)
221 {
222
223 panic("softdep_journal_freeblocks called");
224 }
225
226 void
softdep_journal_fsync(struct inode * ip)227 softdep_journal_fsync(struct inode *ip)
228 {
229
230 panic("softdep_journal_fsync called");
231 }
232
233 void
softdep_setup_freeblocks(struct inode * ip,off_t length,int flags)234 softdep_setup_freeblocks(struct inode *ip,
235 off_t length,
236 int flags)
237 {
238
239 panic("softdep_setup_freeblocks called");
240 }
241
242 void
softdep_freefile(struct vnode * pvp,ino_t ino,int mode)243 softdep_freefile(struct vnode *pvp,
244 ino_t ino,
245 int mode)
246 {
247
248 panic("softdep_freefile called");
249 }
250
251 int
softdep_setup_directory_add(struct buf * bp,struct inode * dp,off_t diroffset,ino_t newinum,struct buf * newdirbp,int isnewblk)252 softdep_setup_directory_add(struct buf *bp,
253 struct inode *dp,
254 off_t diroffset,
255 ino_t newinum,
256 struct buf *newdirbp,
257 int isnewblk)
258 {
259
260 panic("softdep_setup_directory_add called");
261 }
262
263 void
softdep_change_directoryentry_offset(struct buf * bp,struct inode * dp,caddr_t base,caddr_t oldloc,caddr_t newloc,int entrysize)264 softdep_change_directoryentry_offset(struct buf *bp,
265 struct inode *dp,
266 caddr_t base,
267 caddr_t oldloc,
268 caddr_t newloc,
269 int entrysize)
270 {
271
272 panic("softdep_change_directoryentry_offset called");
273 }
274
275 void
softdep_setup_remove(struct buf * bp,struct inode * dp,struct inode * ip,int isrmdir)276 softdep_setup_remove(struct buf *bp,
277 struct inode *dp,
278 struct inode *ip,
279 int isrmdir)
280 {
281
282 panic("softdep_setup_remove called");
283 }
284
285 void
softdep_setup_directory_change(struct buf * bp,struct inode * dp,struct inode * ip,ino_t newinum,int isrmdir)286 softdep_setup_directory_change(struct buf *bp,
287 struct inode *dp,
288 struct inode *ip,
289 ino_t newinum,
290 int isrmdir)
291 {
292
293 panic("softdep_setup_directory_change called");
294 }
295
296 void
softdep_setup_blkfree(struct mount * mp,struct buf * bp,ufs2_daddr_t blkno,int frags,struct workhead * wkhd,bool doingrecovery)297 softdep_setup_blkfree(struct mount *mp,
298 struct buf *bp,
299 ufs2_daddr_t blkno,
300 int frags,
301 struct workhead *wkhd,
302 bool doingrecovery)
303 {
304
305 panic("%s called", __FUNCTION__);
306 }
307
308 void
softdep_setup_inofree(struct mount * mp,struct buf * bp,ino_t ino,struct workhead * wkhd,bool doingrecovery)309 softdep_setup_inofree(struct mount *mp,
310 struct buf *bp,
311 ino_t ino,
312 struct workhead *wkhd,
313 bool doingrecovery)
314 {
315
316 panic("%s called", __FUNCTION__);
317 }
318
319 void
softdep_setup_unlink(struct inode * dp,struct inode * ip)320 softdep_setup_unlink(struct inode *dp, struct inode *ip)
321 {
322
323 panic("%s called", __FUNCTION__);
324 }
325
326 void
softdep_setup_link(struct inode * dp,struct inode * ip)327 softdep_setup_link(struct inode *dp, struct inode *ip)
328 {
329
330 panic("%s called", __FUNCTION__);
331 }
332
333 void
softdep_revert_link(struct inode * dp,struct inode * ip)334 softdep_revert_link(struct inode *dp, struct inode *ip)
335 {
336
337 panic("%s called", __FUNCTION__);
338 }
339
340 void
softdep_setup_rmdir(struct inode * dp,struct inode * ip)341 softdep_setup_rmdir(struct inode *dp, struct inode *ip)
342 {
343
344 panic("%s called", __FUNCTION__);
345 }
346
347 void
softdep_revert_rmdir(struct inode * dp,struct inode * ip)348 softdep_revert_rmdir(struct inode *dp, struct inode *ip)
349 {
350
351 panic("%s called", __FUNCTION__);
352 }
353
354 void
softdep_setup_create(struct inode * dp,struct inode * ip)355 softdep_setup_create(struct inode *dp, struct inode *ip)
356 {
357
358 panic("%s called", __FUNCTION__);
359 }
360
361 void
softdep_revert_create(struct inode * dp,struct inode * ip)362 softdep_revert_create(struct inode *dp, struct inode *ip)
363 {
364
365 panic("%s called", __FUNCTION__);
366 }
367
368 void
softdep_setup_mkdir(struct inode * dp,struct inode * ip)369 softdep_setup_mkdir(struct inode *dp, struct inode *ip)
370 {
371
372 panic("%s called", __FUNCTION__);
373 }
374
375 void
softdep_revert_mkdir(struct inode * dp,struct inode * ip)376 softdep_revert_mkdir(struct inode *dp, struct inode *ip)
377 {
378
379 panic("%s called", __FUNCTION__);
380 }
381
382 void
softdep_setup_dotdot_link(struct inode * dp,struct inode * ip)383 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip)
384 {
385
386 panic("%s called", __FUNCTION__);
387 }
388
389 int
softdep_prealloc(struct vnode * vp,int waitok)390 softdep_prealloc(struct vnode *vp, int waitok)
391 {
392
393 panic("%s called", __FUNCTION__);
394 }
395
396 int
softdep_journal_lookup(struct mount * mp,struct vnode ** vpp)397 softdep_journal_lookup(struct mount *mp, struct vnode **vpp)
398 {
399
400 return (ENOENT);
401 }
402
403 void
softdep_change_linkcnt(struct inode * ip)404 softdep_change_linkcnt(struct inode *ip)
405 {
406
407 panic("softdep_change_linkcnt called");
408 }
409
410 void
softdep_load_inodeblock(struct inode * ip)411 softdep_load_inodeblock(struct inode *ip)
412 {
413
414 panic("softdep_load_inodeblock called");
415 }
416
417 void
softdep_update_inodeblock(struct inode * ip,struct buf * bp,int waitfor)418 softdep_update_inodeblock(struct inode *ip,
419 struct buf *bp,
420 int waitfor)
421 {
422
423 panic("softdep_update_inodeblock called");
424 }
425
426 int
softdep_fsync(struct vnode * vp)427 softdep_fsync(struct vnode *vp) /* the "in_core" copy of the inode */
428 {
429
430 return (0);
431 }
432
433 void
softdep_fsync_mountdev(struct vnode * vp)434 softdep_fsync_mountdev(struct vnode *vp)
435 {
436
437 return;
438 }
439
440 int
softdep_flushworklist(struct mount * oldmnt,int * countp,struct thread * td)441 softdep_flushworklist(struct mount *oldmnt,
442 int *countp,
443 struct thread *td)
444 {
445
446 *countp = 0;
447 return (0);
448 }
449
450 int
softdep_sync_metadata(struct vnode * vp)451 softdep_sync_metadata(struct vnode *vp)
452 {
453
454 panic("softdep_sync_metadata called");
455 }
456
457 int
softdep_sync_buf(struct vnode * vp,struct buf * bp,int waitfor)458 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
459 {
460
461 panic("softdep_sync_buf called");
462 }
463
464 int
softdep_slowdown(struct vnode * vp)465 softdep_slowdown(struct vnode *vp)
466 {
467
468 panic("softdep_slowdown called");
469 }
470
471 int
softdep_request_cleanup(struct fs * fs,struct vnode * vp,struct ucred * cred,int resource)472 softdep_request_cleanup(struct fs *fs,
473 struct vnode *vp,
474 struct ucred *cred,
475 int resource)
476 {
477
478 return (0);
479 }
480
481 int
softdep_check_suspend(struct mount * mp,struct vnode * devvp,int softdep_depcnt,int softdep_accdepcnt,int secondary_writes,int secondary_accwrites)482 softdep_check_suspend(struct mount *mp,
483 struct vnode *devvp,
484 int softdep_depcnt,
485 int softdep_accdepcnt,
486 int secondary_writes,
487 int secondary_accwrites)
488 {
489 struct bufobj *bo;
490 int error;
491
492 (void) softdep_depcnt,
493 (void) softdep_accdepcnt;
494
495 bo = &devvp->v_bufobj;
496 ASSERT_BO_WLOCKED(bo);
497
498 MNT_ILOCK(mp);
499 while (mp->mnt_secondary_writes != 0) {
500 BO_UNLOCK(bo);
501 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
502 (PUSER - 1) | PDROP, "secwr", 0);
503 BO_LOCK(bo);
504 MNT_ILOCK(mp);
505 }
506
507 /*
508 * Reasons for needing more work before suspend:
509 * - Dirty buffers on devvp.
510 * - Secondary writes occurred after start of vnode sync loop
511 */
512 error = 0;
513 if (bo->bo_numoutput > 0 ||
514 bo->bo_dirty.bv_cnt > 0 ||
515 secondary_writes != 0 ||
516 mp->mnt_secondary_writes != 0 ||
517 secondary_accwrites != mp->mnt_secondary_accwrites)
518 error = EAGAIN;
519 BO_UNLOCK(bo);
520 return (error);
521 }
522
523 void
softdep_get_depcounts(struct mount * mp,int * softdepactivep,int * softdepactiveaccp)524 softdep_get_depcounts(struct mount *mp,
525 int *softdepactivep,
526 int *softdepactiveaccp)
527 {
528 (void) mp;
529 *softdepactivep = 0;
530 *softdepactiveaccp = 0;
531 }
532
533 void
softdep_buf_append(struct buf * bp,struct workhead * wkhd)534 softdep_buf_append(struct buf *bp, struct workhead *wkhd)
535 {
536
537 panic("softdep_buf_appendwork called");
538 }
539
540 void
softdep_inode_append(struct inode * ip,struct ucred * cred,struct workhead * wkhd)541 softdep_inode_append(struct inode *ip,
542 struct ucred *cred,
543 struct workhead *wkhd)
544 {
545
546 panic("softdep_inode_appendwork called");
547 }
548
549 void
softdep_freework(struct workhead * wkhd)550 softdep_freework(struct workhead *wkhd)
551 {
552
553 panic("softdep_freework called");
554 }
555
556 int
softdep_prerename(struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp)557 softdep_prerename(struct vnode *fdvp,
558 struct vnode *fvp,
559 struct vnode *tdvp,
560 struct vnode *tvp)
561 {
562
563 panic("softdep_prerename called");
564 }
565
566 int
softdep_prelink(struct vnode * dvp,struct vnode * vp,struct componentname * cnp)567 softdep_prelink(struct vnode *dvp,
568 struct vnode *vp,
569 struct componentname *cnp)
570 {
571
572 panic("softdep_prelink called");
573 }
574
575 #else
576
577 FEATURE(softupdates, "FFS soft-updates support");
578
579 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
580 "soft updates stats");
581 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total,
582 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
583 "total dependencies allocated");
584 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse,
585 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
586 "high use dependencies allocated");
587 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current,
588 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
589 "current dependencies allocated");
590 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write,
591 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
592 "current dependencies written");
593
594 unsigned long dep_current[D_LAST + 1];
595 unsigned long dep_highuse[D_LAST + 1];
596 unsigned long dep_total[D_LAST + 1];
597 unsigned long dep_write[D_LAST + 1];
598
599 #define SOFTDEP_TYPE(type, str, long) \
600 static MALLOC_DEFINE(M_ ## type, #str, long); \
601 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \
602 &dep_total[D_ ## type], 0, ""); \
603 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \
604 &dep_current[D_ ## type], 0, ""); \
605 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \
606 &dep_highuse[D_ ## type], 0, ""); \
607 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \
608 &dep_write[D_ ## type], 0, "");
609
610 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
611 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
612 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
613 "Block or frag allocated from cyl group map");
614 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
615 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
616 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
617 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
618 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
619 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
620 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
621 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
622 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
623 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
624 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
625 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
626 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
627 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
628 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
629 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
630 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
631 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
632 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
633 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
634 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
635 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
636 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
637 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
638
639 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
640
641 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
642 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
643 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
644
645 #define M_SOFTDEP_FLAGS (M_WAITOK)
646
647 /*
648 * translate from workitem type to memory type
649 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
650 */
651 static struct malloc_type *memtype[] = {
652 NULL,
653 M_PAGEDEP,
654 M_INODEDEP,
655 M_BMSAFEMAP,
656 M_NEWBLK,
657 M_ALLOCDIRECT,
658 M_INDIRDEP,
659 M_ALLOCINDIR,
660 M_FREEFRAG,
661 M_FREEBLKS,
662 M_FREEFILE,
663 M_DIRADD,
664 M_MKDIR,
665 M_DIRREM,
666 M_NEWDIRBLK,
667 M_FREEWORK,
668 M_FREEDEP,
669 M_JADDREF,
670 M_JREMREF,
671 M_JMVREF,
672 M_JNEWBLK,
673 M_JFREEBLK,
674 M_JFREEFRAG,
675 M_JSEG,
676 M_JSEGDEP,
677 M_SBDEP,
678 M_JTRUNC,
679 M_JFSYNC,
680 M_SENTINEL
681 };
682
683 #define DtoM(type) (memtype[type])
684
685 /*
686 * Names of malloc types.
687 */
688 #define TYPENAME(type) \
689 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
690 memtype[type]->ks_shortdesc : "???")
691 /*
692 * End system adaptation definitions.
693 */
694
695 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
696 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
697
698 /*
699 * Internal function prototypes.
700 */
701 static void check_clear_deps(struct mount *);
702 static void softdep_error(char *, int);
703 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *);
704 static int softdep_process_worklist(struct mount *, int);
705 static int softdep_waitidle(struct mount *, int);
706 static void drain_output(struct vnode *);
707 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
708 static int check_inodedep_free(struct inodedep *);
709 static void clear_remove(struct mount *);
710 static void clear_inodedeps(struct mount *);
711 static void unlinked_inodedep(struct mount *, struct inodedep *);
712 static void clear_unlinked_inodedep(struct inodedep *);
713 static struct inodedep *first_unlinked_inodedep(struct ufsmount *);
714 static int flush_pagedep_deps(struct vnode *, struct mount *,
715 struct diraddhd *, struct buf *);
716 static int free_pagedep(struct pagedep *);
717 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
718 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
719 static int flush_deplist(struct allocdirectlst *, int, int *);
720 static int sync_cgs(struct mount *, int);
721 static int handle_written_filepage(struct pagedep *, struct buf *, int);
722 static int handle_written_sbdep(struct sbdep *, struct buf *);
723 static void initiate_write_sbdep(struct sbdep *);
724 static void diradd_inode_written(struct diradd *, struct inodedep *);
725 static int handle_written_indirdep(struct indirdep *, struct buf *,
726 struct buf**, int);
727 static int handle_written_inodeblock(struct inodedep *, struct buf *, int);
728 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
729 uint8_t *);
730 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
731 static void handle_written_jaddref(struct jaddref *);
732 static void handle_written_jremref(struct jremref *);
733 static void handle_written_jseg(struct jseg *, struct buf *);
734 static void handle_written_jnewblk(struct jnewblk *);
735 static void handle_written_jblkdep(struct jblkdep *);
736 static void handle_written_jfreefrag(struct jfreefrag *);
737 static void complete_jseg(struct jseg *);
738 static void complete_jsegs(struct jseg *);
739 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
740 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
741 static void jremref_write(struct jremref *, struct jseg *, uint8_t *);
742 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
743 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
744 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
745 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
746 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
747 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
748 static inline void inoref_write(struct inoref *, struct jseg *,
749 struct jrefrec *);
750 static void handle_allocdirect_partdone(struct allocdirect *,
751 struct workhead *);
752 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
753 struct workhead *);
754 static void indirdep_complete(struct indirdep *);
755 static int indirblk_lookup(struct mount *, ufs2_daddr_t);
756 static void indirblk_insert(struct freework *);
757 static void indirblk_remove(struct freework *);
758 static void handle_allocindir_partdone(struct allocindir *);
759 static void initiate_write_filepage(struct pagedep *, struct buf *);
760 static void initiate_write_indirdep(struct indirdep*, struct buf *);
761 static void handle_written_mkdir(struct mkdir *, int);
762 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
763 uint8_t *);
764 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
765 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
766 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
767 static void handle_workitem_freefile(struct freefile *);
768 static int handle_workitem_remove(struct dirrem *, int);
769 static struct dirrem *newdirrem(struct buf *, struct inode *,
770 struct inode *, int, struct dirrem **);
771 static struct indirdep *indirdep_lookup(struct mount *, struct inode *,
772 struct buf *);
773 static void cancel_indirdep(struct indirdep *, struct buf *,
774 struct freeblks *);
775 static void free_indirdep(struct indirdep *);
776 static void free_diradd(struct diradd *, struct workhead *);
777 static void merge_diradd(struct inodedep *, struct diradd *);
778 static void complete_diradd(struct diradd *);
779 static struct diradd *diradd_lookup(struct pagedep *, int);
780 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
781 struct jremref *);
782 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
783 struct jremref *);
784 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
785 struct jremref *, struct jremref *);
786 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
787 struct jremref *);
788 static void cancel_allocindir(struct allocindir *, struct buf *bp,
789 struct freeblks *, int);
790 static int setup_trunc_indir(struct freeblks *, struct inode *,
791 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
792 static void complete_trunc_indir(struct freework *);
793 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
794 int);
795 static void complete_mkdir(struct mkdir *);
796 static void free_newdirblk(struct newdirblk *);
797 static void free_jremref(struct jremref *);
798 static void free_jaddref(struct jaddref *);
799 static void free_jsegdep(struct jsegdep *);
800 static void free_jsegs(struct jblocks *);
801 static void rele_jseg(struct jseg *);
802 static void free_jseg(struct jseg *, struct jblocks *);
803 static void free_jnewblk(struct jnewblk *);
804 static void free_jblkdep(struct jblkdep *);
805 static void free_jfreefrag(struct jfreefrag *);
806 static void free_freedep(struct freedep *);
807 static void journal_jremref(struct dirrem *, struct jremref *,
808 struct inodedep *);
809 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
810 static int cancel_jaddref(struct jaddref *, struct inodedep *,
811 struct workhead *);
812 static void cancel_jfreefrag(struct jfreefrag *);
813 static inline void setup_freedirect(struct freeblks *, struct inode *,
814 int, int);
815 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
816 static inline void setup_freeindir(struct freeblks *, struct inode *, int,
817 ufs_lbn_t, int);
818 static inline struct freeblks *newfreeblks(struct mount *, struct inode *);
819 static void freeblks_free(struct ufsmount *, struct freeblks *, int);
820 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
821 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
822 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
823 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
824 int, int);
825 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
826 static int cancel_pagedep(struct pagedep *, struct freeblks *, int);
827 static int deallocate_dependencies(struct buf *, struct freeblks *, int);
828 static void newblk_freefrag(struct newblk*);
829 static void free_newblk(struct newblk *);
830 static void cancel_allocdirect(struct allocdirectlst *,
831 struct allocdirect *, struct freeblks *);
832 static int check_inode_unwritten(struct inodedep *);
833 static int free_inodedep(struct inodedep *);
834 static void freework_freeblock(struct freework *, uint64_t);
835 static void freework_enqueue(struct freework *);
836 static int handle_workitem_freeblocks(struct freeblks *, int);
837 static int handle_complete_freeblocks(struct freeblks *, int);
838 static void handle_workitem_indirblk(struct freework *);
839 static void handle_written_freework(struct freework *);
840 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
841 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
842 struct workhead *);
843 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
844 struct inodedep *, struct allocindir *, ufs_lbn_t);
845 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
846 ufs2_daddr_t, ufs_lbn_t);
847 static void handle_workitem_freefrag(struct freefrag *);
848 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
849 ufs_lbn_t, uint64_t);
850 static void allocdirect_merge(struct allocdirectlst *,
851 struct allocdirect *, struct allocdirect *);
852 static struct freefrag *allocindir_merge(struct allocindir *,
853 struct allocindir *);
854 static int bmsafemap_find(struct bmsafemap_hashhead *, int,
855 struct bmsafemap **);
856 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
857 int cg, struct bmsafemap *);
858 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
859 struct newblk **);
860 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
861 static int inodedep_find(struct inodedep_hashhead *, ino_t,
862 struct inodedep **);
863 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
864 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
865 int, struct pagedep **);
866 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
867 struct pagedep **);
868 static void pause_timer(void *);
869 static int request_cleanup(struct mount *, int);
870 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
871 static void schedule_cleanup(struct mount *);
872 static void softdep_ast_cleanup_proc(struct thread *, int);
873 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
874 static int process_worklist_item(struct mount *, int, int);
875 static void process_removes(struct vnode *);
876 static void process_truncates(struct vnode *);
877 static void jwork_move(struct workhead *, struct workhead *);
878 static void jwork_insert(struct workhead *, struct jsegdep *);
879 static void add_to_worklist(struct worklist *, int);
880 static void wake_worklist(struct worklist *);
881 static void wait_worklist(struct worklist *, char *);
882 static void remove_from_worklist(struct worklist *);
883 static void softdep_flush(void *);
884 static void softdep_flushjournal(struct mount *);
885 static int softdep_speedup(struct ufsmount *);
886 static void worklist_speedup(struct mount *);
887 static int journal_mount(struct mount *, struct fs *, struct ucred *);
888 static void journal_unmount(struct ufsmount *);
889 static int journal_space(struct ufsmount *, int);
890 static void journal_suspend(struct ufsmount *);
891 static int journal_unsuspend(struct ufsmount *ump);
892 static void add_to_journal(struct worklist *);
893 static void remove_from_journal(struct worklist *);
894 static bool softdep_excess_items(struct ufsmount *, int);
895 static void softdep_process_journal(struct mount *, struct worklist *, int);
896 static struct jremref *newjremref(struct dirrem *, struct inode *,
897 struct inode *ip, off_t, nlink_t);
898 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
899 uint16_t);
900 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
901 uint16_t);
902 static inline struct jsegdep *inoref_jseg(struct inoref *);
903 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
904 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
905 ufs2_daddr_t, int);
906 static void adjust_newfreework(struct freeblks *, int);
907 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
908 static void move_newblock_dep(struct jaddref *, struct inodedep *);
909 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
910 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
911 ufs2_daddr_t, long, ufs_lbn_t);
912 static struct freework *newfreework(struct ufsmount *, struct freeblks *,
913 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
914 static int jwait(struct worklist *, int);
915 static struct inodedep *inodedep_lookup_ip(struct inode *);
916 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
917 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
918 static void handle_jwork(struct workhead *);
919 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
920 struct mkdir **);
921 static struct jblocks *jblocks_create(void);
922 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
923 static void jblocks_free(struct jblocks *, struct mount *, int);
924 static void jblocks_destroy(struct jblocks *);
925 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
926
927 /*
928 * Exported softdep operations.
929 */
930 static void softdep_disk_io_initiation(struct buf *);
931 static void softdep_disk_write_complete(struct buf *);
932 static void softdep_deallocate_dependencies(struct buf *);
933 static int softdep_count_dependencies(struct buf *bp, int);
934
935 /*
936 * Global lock over all of soft updates.
937 */
938 static struct mtx lk;
939 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF);
940
941 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk)
942 #define FREE_GBLLOCK(lk) mtx_unlock(lk)
943 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED)
944
945 /*
946 * Per-filesystem soft-updates locking.
947 */
948 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock)
949 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock)
950 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock)
951 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock)
952 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \
953 RA_WLOCKED)
954
955 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock)
956 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock)
957
958 /*
959 * Worklist queue management.
960 * These routines require that the lock be held.
961 */
962 #ifndef /* NOT */ INVARIANTS
963 #define WORKLIST_INSERT(head, item) do { \
964 (item)->wk_state |= ONWORKLIST; \
965 LIST_INSERT_HEAD(head, item, wk_list); \
966 } while (0)
967 #define WORKLIST_REMOVE(item) do { \
968 (item)->wk_state &= ~ONWORKLIST; \
969 LIST_REMOVE(item, wk_list); \
970 } while (0)
971 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT
972 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE
973
974 #else /* INVARIANTS */
975 static void worklist_insert(struct workhead *, struct worklist *, int,
976 const char *, int);
977 static void worklist_remove(struct worklist *, int, const char *, int);
978
979 #define WORKLIST_INSERT(head, item) \
980 worklist_insert(head, item, 1, __func__, __LINE__)
981 #define WORKLIST_INSERT_UNLOCKED(head, item)\
982 worklist_insert(head, item, 0, __func__, __LINE__)
983 #define WORKLIST_REMOVE(item)\
984 worklist_remove(item, 1, __func__, __LINE__)
985 #define WORKLIST_REMOVE_UNLOCKED(item)\
986 worklist_remove(item, 0, __func__, __LINE__)
987
988 static void
worklist_insert(struct workhead * head,struct worklist * item,int locked,const char * func,int line)989 worklist_insert(struct workhead *head,
990 struct worklist *item,
991 int locked,
992 const char *func,
993 int line)
994 {
995
996 if (locked)
997 LOCK_OWNED(VFSTOUFS(item->wk_mp));
998 if (item->wk_state & ONWORKLIST)
999 panic("worklist_insert: %p %s(0x%X) already on list, "
1000 "added in function %s at line %d",
1001 item, TYPENAME(item->wk_type), item->wk_state,
1002 item->wk_func, item->wk_line);
1003 item->wk_state |= ONWORKLIST;
1004 item->wk_func = func;
1005 item->wk_line = line;
1006 LIST_INSERT_HEAD(head, item, wk_list);
1007 }
1008
1009 static void
worklist_remove(struct worklist * item,int locked,const char * func,int line)1010 worklist_remove(struct worklist *item,
1011 int locked,
1012 const char *func,
1013 int line)
1014 {
1015
1016 if (locked)
1017 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1018 if ((item->wk_state & ONWORKLIST) == 0)
1019 panic("worklist_remove: %p %s(0x%X) not on list, "
1020 "removed in function %s at line %d",
1021 item, TYPENAME(item->wk_type), item->wk_state,
1022 item->wk_func, item->wk_line);
1023 item->wk_state &= ~ONWORKLIST;
1024 item->wk_func = func;
1025 item->wk_line = line;
1026 LIST_REMOVE(item, wk_list);
1027 }
1028 #endif /* INVARIANTS */
1029
1030 /*
1031 * Merge two jsegdeps keeping only the oldest one as newer references
1032 * can't be discarded until after older references.
1033 */
1034 static inline struct jsegdep *
jsegdep_merge(struct jsegdep * one,struct jsegdep * two)1035 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1036 {
1037 struct jsegdep *swp;
1038
1039 if (two == NULL)
1040 return (one);
1041
1042 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1043 swp = one;
1044 one = two;
1045 two = swp;
1046 }
1047 WORKLIST_REMOVE(&two->jd_list);
1048 free_jsegdep(two);
1049
1050 return (one);
1051 }
1052
1053 /*
1054 * If two freedeps are compatible free one to reduce list size.
1055 */
1056 static inline struct freedep *
freedep_merge(struct freedep * one,struct freedep * two)1057 freedep_merge(struct freedep *one, struct freedep *two)
1058 {
1059 if (two == NULL)
1060 return (one);
1061
1062 if (one->fd_freework == two->fd_freework) {
1063 WORKLIST_REMOVE(&two->fd_list);
1064 free_freedep(two);
1065 }
1066 return (one);
1067 }
1068
1069 /*
1070 * Move journal work from one list to another. Duplicate freedeps and
1071 * jsegdeps are coalesced to keep the lists as small as possible.
1072 */
1073 static void
jwork_move(struct workhead * dst,struct workhead * src)1074 jwork_move(struct workhead *dst, struct workhead *src)
1075 {
1076 struct freedep *freedep;
1077 struct jsegdep *jsegdep;
1078 struct worklist *wkn;
1079 struct worklist *wk;
1080
1081 KASSERT(dst != src,
1082 ("jwork_move: dst == src"));
1083 freedep = NULL;
1084 jsegdep = NULL;
1085 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1086 if (wk->wk_type == D_JSEGDEP)
1087 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1088 else if (wk->wk_type == D_FREEDEP)
1089 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1090 }
1091
1092 while ((wk = LIST_FIRST(src)) != NULL) {
1093 WORKLIST_REMOVE(wk);
1094 WORKLIST_INSERT(dst, wk);
1095 if (wk->wk_type == D_JSEGDEP) {
1096 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1097 continue;
1098 }
1099 if (wk->wk_type == D_FREEDEP)
1100 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1101 }
1102 }
1103
1104 static void
jwork_insert(struct workhead * dst,struct jsegdep * jsegdep)1105 jwork_insert(struct workhead *dst, struct jsegdep *jsegdep)
1106 {
1107 struct jsegdep *jsegdepn;
1108 struct worklist *wk;
1109
1110 LIST_FOREACH(wk, dst, wk_list)
1111 if (wk->wk_type == D_JSEGDEP)
1112 break;
1113 if (wk == NULL) {
1114 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1115 return;
1116 }
1117 jsegdepn = WK_JSEGDEP(wk);
1118 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1119 WORKLIST_REMOVE(wk);
1120 free_jsegdep(jsegdepn);
1121 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1122 } else
1123 free_jsegdep(jsegdep);
1124 }
1125
1126 /*
1127 * Routines for tracking and managing workitems.
1128 */
1129 static void workitem_free(struct worklist *, int);
1130 static void workitem_alloc(struct worklist *, int, struct mount *);
1131 static void workitem_reassign(struct worklist *, int);
1132
1133 #define WORKITEM_FREE(item, type) \
1134 workitem_free((struct worklist *)(item), (type))
1135 #define WORKITEM_REASSIGN(item, type) \
1136 workitem_reassign((struct worklist *)(item), (type))
1137
1138 static void
workitem_free(struct worklist * item,int type)1139 workitem_free(struct worklist *item, int type)
1140 {
1141 struct ufsmount *ump;
1142
1143 #ifdef INVARIANTS
1144 if (item->wk_state & ONWORKLIST)
1145 panic("workitem_free: %s(0x%X) still on list, "
1146 "added in function %s at line %d",
1147 TYPENAME(item->wk_type), item->wk_state,
1148 item->wk_func, item->wk_line);
1149 if (item->wk_type != type && type != D_NEWBLK)
1150 panic("workitem_free: type mismatch %s != %s",
1151 TYPENAME(item->wk_type), TYPENAME(type));
1152 #endif
1153 if (item->wk_state & IOWAITING)
1154 wakeup(item);
1155 ump = VFSTOUFS(item->wk_mp);
1156 LOCK_OWNED(ump);
1157 KASSERT(ump->softdep_deps > 0,
1158 ("workitem_free: %s: softdep_deps going negative",
1159 ump->um_fs->fs_fsmnt));
1160 if (--ump->softdep_deps == 0 && ump->softdep_req)
1161 wakeup(&ump->softdep_deps);
1162 KASSERT(dep_current[item->wk_type] > 0,
1163 ("workitem_free: %s: dep_current[%s] going negative",
1164 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1165 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1166 ("workitem_free: %s: softdep_curdeps[%s] going negative",
1167 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1168 atomic_subtract_long(&dep_current[item->wk_type], 1);
1169 ump->softdep_curdeps[item->wk_type] -= 1;
1170 LIST_REMOVE(item, wk_all);
1171 free(item, DtoM(type));
1172 }
1173
1174 static void
workitem_alloc(struct worklist * item,int type,struct mount * mp)1175 workitem_alloc(struct worklist *item,
1176 int type,
1177 struct mount *mp)
1178 {
1179 struct ufsmount *ump;
1180
1181 item->wk_type = type;
1182 item->wk_mp = mp;
1183 item->wk_state = 0;
1184
1185 ump = VFSTOUFS(mp);
1186 ACQUIRE_GBLLOCK(&lk);
1187 dep_current[type]++;
1188 if (dep_current[type] > dep_highuse[type])
1189 dep_highuse[type] = dep_current[type];
1190 dep_total[type]++;
1191 FREE_GBLLOCK(&lk);
1192 ACQUIRE_LOCK(ump);
1193 ump->softdep_curdeps[type] += 1;
1194 ump->softdep_deps++;
1195 ump->softdep_accdeps++;
1196 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all);
1197 FREE_LOCK(ump);
1198 }
1199
1200 static void
workitem_reassign(struct worklist * item,int newtype)1201 workitem_reassign(struct worklist *item, int newtype)
1202 {
1203 struct ufsmount *ump;
1204
1205 ump = VFSTOUFS(item->wk_mp);
1206 LOCK_OWNED(ump);
1207 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1208 ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1209 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1210 ump->softdep_curdeps[item->wk_type] -= 1;
1211 ump->softdep_curdeps[newtype] += 1;
1212 KASSERT(dep_current[item->wk_type] > 0,
1213 ("workitem_reassign: %s: dep_current[%s] going negative",
1214 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1215 ACQUIRE_GBLLOCK(&lk);
1216 dep_current[newtype]++;
1217 dep_current[item->wk_type]--;
1218 if (dep_current[newtype] > dep_highuse[newtype])
1219 dep_highuse[newtype] = dep_current[newtype];
1220 dep_total[newtype]++;
1221 FREE_GBLLOCK(&lk);
1222 item->wk_type = newtype;
1223 LIST_REMOVE(item, wk_all);
1224 LIST_INSERT_HEAD(&ump->softdep_alldeps[newtype], item, wk_all);
1225 }
1226
1227 /*
1228 * Workitem queue management
1229 */
1230 static int max_softdeps; /* maximum number of structs before slowdown */
1231 static int tickdelay = 2; /* number of ticks to pause during slowdown */
1232 static int proc_waiting; /* tracks whether we have a timeout posted */
1233 static int *stat_countp; /* statistic to count in proc_waiting timeout */
1234 static struct callout softdep_callout;
1235 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
1236 static int req_clear_remove; /* syncer process flush some freeblks */
1237 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1238
1239 /*
1240 * runtime statistics
1241 */
1242 static int stat_flush_threads; /* number of softdep flushing threads */
1243 static int stat_worklist_push; /* number of worklist cleanups */
1244 static int stat_delayed_inact; /* number of delayed inactivation cleanups */
1245 static int stat_blk_limit_push; /* number of times block limit neared */
1246 static int stat_ino_limit_push; /* number of times inode limit neared */
1247 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
1248 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
1249 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
1250 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
1251 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
1252 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1253 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
1254 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */
1255 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */
1256 static int stat_journal_min; /* Times hit journal min threshold */
1257 static int stat_journal_low; /* Times hit journal low threshold */
1258 static int stat_journal_wait; /* Times blocked in jwait(). */
1259 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */
1260 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */
1261 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */
1262 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */
1263 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1264 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1265 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1266 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1267 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1268 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1269
1270 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1271 &max_softdeps, 0, "");
1272 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1273 &tickdelay, 0, "");
1274 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1275 &stat_flush_threads, 0, "");
1276 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push,
1277 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,"");
1278 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD,
1279 &stat_delayed_inact, 0, "");
1280 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push,
1281 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,"");
1282 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push,
1283 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,"");
1284 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit,
1285 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, "");
1286 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit,
1287 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, "");
1288 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit,
1289 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, "");
1290 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs,
1291 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, "");
1292 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap,
1293 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, "");
1294 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs,
1295 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, "");
1296 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry,
1297 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, "");
1298 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback,
1299 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, "");
1300 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback,
1301 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, "");
1302 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low,
1303 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, "");
1304 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min,
1305 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, "");
1306 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait,
1307 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, "");
1308 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage,
1309 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, "");
1310 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks,
1311 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, "");
1312 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode,
1313 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, "");
1314 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk,
1315 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, "");
1316 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests,
1317 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, "");
1318 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests,
1319 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, "");
1320 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay,
1321 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, "");
1322 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries,
1323 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, "");
1324 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures,
1325 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, "");
1326
1327 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1328 &softdep_flushcache, 0, "");
1329 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1330 &stat_emptyjblocks, 0, "");
1331
1332 SYSCTL_DECL(_vfs_ffs);
1333
1334 /* Whether to recompute the summary at mount time */
1335 static int compute_summary_at_mount = 0;
1336 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1337 &compute_summary_at_mount, 0, "Recompute summary at mount");
1338 static int print_threads = 0;
1339 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1340 &print_threads, 0, "Notify flusher thread start/stop");
1341
1342 /* List of all filesystems mounted with soft updates */
1343 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1344
1345 static void
get_parent_vp_unlock_bp(struct mount * mp,struct buf * bp,struct diraddhd * diraddhdp,struct diraddhd * unfinishedp)1346 get_parent_vp_unlock_bp(struct mount *mp,
1347 struct buf *bp,
1348 struct diraddhd *diraddhdp,
1349 struct diraddhd *unfinishedp)
1350 {
1351 struct diradd *dap;
1352
1353 /*
1354 * Requeue unfinished dependencies before
1355 * unlocking buffer, which could make
1356 * diraddhdp invalid.
1357 */
1358 ACQUIRE_LOCK(VFSTOUFS(mp));
1359 while ((dap = LIST_FIRST(unfinishedp)) != NULL) {
1360 LIST_REMOVE(dap, da_pdlist);
1361 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
1362 }
1363 FREE_LOCK(VFSTOUFS(mp));
1364
1365 bp->b_vflags &= ~BV_SCANNED;
1366 BUF_NOREC(bp);
1367 BUF_UNLOCK(bp);
1368 }
1369
1370 /*
1371 * This function fetches inode inum on mount point mp. We already
1372 * hold a locked vnode vp, and might have a locked buffer bp belonging
1373 * to vp.
1374
1375 * We must not block on acquiring the new inode lock as we will get
1376 * into a lock-order reversal with the buffer lock and possibly get a
1377 * deadlock. Thus if we cannot instantiate the requested vnode
1378 * without sleeping on its lock, we must unlock the vnode and the
1379 * buffer before doing a blocking on the vnode lock. We return
1380 * ERELOOKUP if we have had to unlock either the vnode or the buffer so
1381 * that the caller can reassess its state.
1382 *
1383 * Top-level VFS code (for syscalls and other consumers, e.g. callers
1384 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe
1385 * point.
1386 *
1387 * Since callers expect to operate on fully constructed vnode, we also
1388 * recheck v_data after relock, and return ENOENT if NULL.
1389 *
1390 * If unlocking bp, we must unroll dequeueing its unfinished
1391 * dependencies, and clear scan flag, before unlocking. If unlocking
1392 * vp while it is under deactivation, we re-queue deactivation.
1393 */
1394 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)1395 get_parent_vp(struct vnode *vp,
1396 struct mount *mp,
1397 ino_t inum,
1398 struct buf *bp,
1399 struct diraddhd *diraddhdp,
1400 struct diraddhd *unfinishedp,
1401 struct vnode **rvp)
1402 {
1403 struct vnode *pvp;
1404 int error;
1405 bool bplocked;
1406
1407 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked");
1408 for (bplocked = true, pvp = NULL;;) {
1409 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp,
1410 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
1411 if (error == 0) {
1412 /*
1413 * Since we could have unlocked vp, the inode
1414 * number could no longer indicate a
1415 * constructed node. In this case, we must
1416 * restart the syscall.
1417 */
1418 if (VTOI(pvp)->i_mode == 0 || !bplocked) {
1419 if (bp != NULL && bplocked)
1420 get_parent_vp_unlock_bp(mp, bp,
1421 diraddhdp, unfinishedp);
1422 if (VTOI(pvp)->i_mode == 0)
1423 vgone(pvp);
1424 error = ERELOOKUP;
1425 goto out2;
1426 }
1427 goto out1;
1428 }
1429 if (bp != NULL && bplocked) {
1430 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp);
1431 bplocked = false;
1432 }
1433
1434 /*
1435 * Do not drop vnode lock while inactivating during
1436 * vunref. This would result in leaks of the VI flags
1437 * and reclaiming of non-truncated vnode. Instead,
1438 * re-schedule inactivation hoping that we would be
1439 * able to sync inode later.
1440 */
1441 if ((vp->v_iflag & VI_DOINGINACT) != 0 &&
1442 (vp->v_vflag & VV_UNREF) != 0) {
1443 VI_LOCK(vp);
1444 vp->v_iflag |= VI_OWEINACT;
1445 VI_UNLOCK(vp);
1446 return (ERELOOKUP);
1447 }
1448
1449 VOP_UNLOCK(vp);
1450 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp,
1451 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
1452 if (error != 0) {
1453 MPASS(error != ERELOOKUP);
1454 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1455 break;
1456 }
1457 if (VTOI(pvp)->i_mode == 0) {
1458 vgone(pvp);
1459 vput(pvp);
1460 pvp = NULL;
1461 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1462 error = ERELOOKUP;
1463 break;
1464 }
1465 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
1466 if (error == 0)
1467 break;
1468 vput(pvp);
1469 pvp = NULL;
1470 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1471 if (vp->v_data == NULL) {
1472 error = ENOENT;
1473 break;
1474 }
1475 }
1476 if (bp != NULL) {
1477 MPASS(!bplocked);
1478 error = ERELOOKUP;
1479 }
1480 out2:
1481 if (error != 0 && pvp != NULL) {
1482 vput(pvp);
1483 pvp = NULL;
1484 }
1485 out1:
1486 *rvp = pvp;
1487 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return");
1488 return (error);
1489 }
1490
1491 /*
1492 * This function cleans the worklist for a filesystem.
1493 * Each filesystem running with soft dependencies gets its own
1494 * thread to run in this function. The thread is started up in
1495 * softdep_mount and shutdown in softdep_unmount. They show up
1496 * as part of the kernel "bufdaemon" process whose process
1497 * entry is available in bufdaemonproc.
1498 */
1499 static int searchfailed;
1500 extern struct proc *bufdaemonproc;
1501 static void
softdep_flush(void * addr)1502 softdep_flush(void *addr)
1503 {
1504 struct mount *mp;
1505 struct thread *td;
1506 struct ufsmount *ump;
1507 int cleanups;
1508
1509 td = curthread;
1510 td->td_pflags |= TDP_NORUNNINGBUF;
1511 mp = (struct mount *)addr;
1512 ump = VFSTOUFS(mp);
1513 atomic_add_int(&stat_flush_threads, 1);
1514 ACQUIRE_LOCK(ump);
1515 ump->softdep_flags &= ~FLUSH_STARTING;
1516 wakeup(&ump->softdep_flushtd);
1517 FREE_LOCK(ump);
1518 if (print_threads) {
1519 if (stat_flush_threads == 1)
1520 printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1521 bufdaemonproc->p_pid);
1522 printf("Start thread %s\n", td->td_name);
1523 }
1524 for (;;) {
1525 while (softdep_process_worklist(mp, 0) > 0 ||
1526 (MOUNTEDSUJ(mp) &&
1527 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1528 kthread_suspend_check();
1529 ACQUIRE_LOCK(ump);
1530 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1531 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1532 "sdflush", hz / 2);
1533 ump->softdep_flags &= ~FLUSH_CLEANUP;
1534 /*
1535 * Check to see if we are done and need to exit.
1536 */
1537 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1538 FREE_LOCK(ump);
1539 continue;
1540 }
1541 ump->softdep_flags &= ~FLUSH_EXIT;
1542 cleanups = ump->um_softdep->sd_cleanups;
1543 FREE_LOCK(ump);
1544 wakeup(&ump->softdep_flags);
1545 if (print_threads) {
1546 printf("Stop thread %s: searchfailed %d, "
1547 "did cleanups %d\n",
1548 td->td_name, searchfailed, cleanups);
1549 }
1550 atomic_subtract_int(&stat_flush_threads, 1);
1551 kthread_exit();
1552 panic("kthread_exit failed\n");
1553 }
1554 }
1555
1556 static void
worklist_speedup(struct mount * mp)1557 worklist_speedup(struct mount *mp)
1558 {
1559 struct ufsmount *ump;
1560
1561 ump = VFSTOUFS(mp);
1562 LOCK_OWNED(ump);
1563 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1564 ump->softdep_flags |= FLUSH_CLEANUP;
1565 wakeup(&ump->softdep_flushtd);
1566 }
1567
1568 static void
softdep_send_speedup(struct ufsmount * ump,off_t shortage,uint64_t flags)1569 softdep_send_speedup(struct ufsmount *ump,
1570 off_t shortage,
1571 uint64_t flags)
1572 {
1573 struct buf *bp;
1574
1575 if ((ump->um_flags & UM_CANSPEEDUP) == 0)
1576 return;
1577
1578 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO);
1579 bp->b_iocmd = BIO_SPEEDUP;
1580 bp->b_ioflags = flags;
1581 bp->b_bcount = omin(shortage, LONG_MAX);
1582 g_vfs_strategy(ump->um_bo, bp);
1583 bufwait(bp);
1584 free(bp, M_TRIM);
1585 }
1586
1587 static int
softdep_speedup(struct ufsmount * ump)1588 softdep_speedup(struct ufsmount *ump)
1589 {
1590 struct ufsmount *altump;
1591 struct mount_softdeps *sdp;
1592
1593 LOCK_OWNED(ump);
1594 worklist_speedup(ump->um_mountp);
1595 bd_speedup();
1596 /*
1597 * If we have global shortages, then we need other
1598 * filesystems to help with the cleanup. Here we wakeup a
1599 * flusher thread for a filesystem that is over its fair
1600 * share of resources.
1601 */
1602 if (req_clear_inodedeps || req_clear_remove) {
1603 ACQUIRE_GBLLOCK(&lk);
1604 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1605 if ((altump = sdp->sd_ump) == ump)
1606 continue;
1607 if (((req_clear_inodedeps &&
1608 altump->softdep_curdeps[D_INODEDEP] >
1609 max_softdeps / stat_flush_threads) ||
1610 (req_clear_remove &&
1611 altump->softdep_curdeps[D_DIRREM] >
1612 (max_softdeps / 2) / stat_flush_threads)) &&
1613 TRY_ACQUIRE_LOCK(altump))
1614 break;
1615 }
1616 if (sdp == NULL) {
1617 searchfailed++;
1618 FREE_GBLLOCK(&lk);
1619 } else {
1620 /*
1621 * Move to the end of the list so we pick a
1622 * different one on out next try.
1623 */
1624 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1625 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1626 FREE_GBLLOCK(&lk);
1627 if ((altump->softdep_flags &
1628 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1629 altump->softdep_flags |= FLUSH_CLEANUP;
1630 altump->um_softdep->sd_cleanups++;
1631 wakeup(&altump->softdep_flushtd);
1632 FREE_LOCK(altump);
1633 }
1634 }
1635 return (speedup_syncer());
1636 }
1637
1638 /*
1639 * Add an item to the end of the work queue.
1640 * This routine requires that the lock be held.
1641 * This is the only routine that adds items to the list.
1642 * The following routine is the only one that removes items
1643 * and does so in order from first to last.
1644 */
1645
1646 #define WK_HEAD 0x0001 /* Add to HEAD. */
1647 #define WK_NODELAY 0x0002 /* Process immediately. */
1648
1649 static void
add_to_worklist(struct worklist * wk,int flags)1650 add_to_worklist(struct worklist *wk, int flags)
1651 {
1652 struct ufsmount *ump;
1653
1654 ump = VFSTOUFS(wk->wk_mp);
1655 LOCK_OWNED(ump);
1656 if (wk->wk_state & ONWORKLIST)
1657 panic("add_to_worklist: %s(0x%X) already on list",
1658 TYPENAME(wk->wk_type), wk->wk_state);
1659 wk->wk_state |= ONWORKLIST;
1660 if (ump->softdep_on_worklist == 0) {
1661 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1662 ump->softdep_worklist_tail = wk;
1663 } else if (flags & WK_HEAD) {
1664 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1665 } else {
1666 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1667 ump->softdep_worklist_tail = wk;
1668 }
1669 ump->softdep_on_worklist += 1;
1670 if (flags & WK_NODELAY)
1671 worklist_speedup(wk->wk_mp);
1672 }
1673
1674 /*
1675 * Remove the item to be processed. If we are removing the last
1676 * item on the list, we need to recalculate the tail pointer.
1677 */
1678 static void
remove_from_worklist(struct worklist * wk)1679 remove_from_worklist(struct worklist *wk)
1680 {
1681 struct ufsmount *ump;
1682
1683 ump = VFSTOUFS(wk->wk_mp);
1684 if (ump->softdep_worklist_tail == wk)
1685 ump->softdep_worklist_tail =
1686 (struct worklist *)wk->wk_list.le_prev;
1687 WORKLIST_REMOVE(wk);
1688 ump->softdep_on_worklist -= 1;
1689 }
1690
1691 static void
wake_worklist(struct worklist * wk)1692 wake_worklist(struct worklist *wk)
1693 {
1694 if (wk->wk_state & IOWAITING) {
1695 wk->wk_state &= ~IOWAITING;
1696 wakeup(wk);
1697 }
1698 }
1699
1700 static void
wait_worklist(struct worklist * wk,char * wmesg)1701 wait_worklist(struct worklist *wk, char *wmesg)
1702 {
1703 struct ufsmount *ump;
1704
1705 ump = VFSTOUFS(wk->wk_mp);
1706 wk->wk_state |= IOWAITING;
1707 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1708 }
1709
1710 /*
1711 * Process that runs once per second to handle items in the background queue.
1712 *
1713 * Note that we ensure that everything is done in the order in which they
1714 * appear in the queue. The code below depends on this property to ensure
1715 * that blocks of a file are freed before the inode itself is freed. This
1716 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1717 * until all the old ones have been purged from the dependency lists.
1718 */
1719 static int
softdep_process_worklist(struct mount * mp,int full)1720 softdep_process_worklist(struct mount *mp, int full)
1721 {
1722 int cnt, matchcnt;
1723 struct ufsmount *ump;
1724 long starttime;
1725
1726 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1727 ump = VFSTOUFS(mp);
1728 if (ump->um_softdep == NULL)
1729 return (0);
1730 matchcnt = 0;
1731 ACQUIRE_LOCK(ump);
1732 starttime = time_second;
1733 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1734 check_clear_deps(mp);
1735 while (ump->softdep_on_worklist > 0) {
1736 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1737 break;
1738 else
1739 matchcnt += cnt;
1740 check_clear_deps(mp);
1741 /*
1742 * We do not generally want to stop for buffer space, but if
1743 * we are really being a buffer hog, we will stop and wait.
1744 */
1745 if (should_yield()) {
1746 FREE_LOCK(ump);
1747 kern_yield(PRI_USER);
1748 bwillwrite();
1749 ACQUIRE_LOCK(ump);
1750 }
1751 /*
1752 * Never allow processing to run for more than one
1753 * second. This gives the syncer thread the opportunity
1754 * to pause if appropriate.
1755 */
1756 if (!full && starttime != time_second)
1757 break;
1758 }
1759 if (full == 0)
1760 journal_unsuspend(ump);
1761 FREE_LOCK(ump);
1762 return (matchcnt);
1763 }
1764
1765 /*
1766 * Process all removes associated with a vnode if we are running out of
1767 * journal space. Any other process which attempts to flush these will
1768 * be unable as we have the vnodes locked.
1769 */
1770 static void
process_removes(struct vnode * vp)1771 process_removes(struct vnode *vp)
1772 {
1773 struct inodedep *inodedep;
1774 struct dirrem *dirrem;
1775 struct ufsmount *ump;
1776 struct mount *mp;
1777 ino_t inum;
1778
1779 mp = vp->v_mount;
1780 ump = VFSTOUFS(mp);
1781 LOCK_OWNED(ump);
1782 inum = VTOI(vp)->i_number;
1783 for (;;) {
1784 top:
1785 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1786 return;
1787 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1788 /*
1789 * If another thread is trying to lock this vnode
1790 * it will fail but we must wait for it to do so
1791 * before we can proceed.
1792 */
1793 if (dirrem->dm_state & INPROGRESS) {
1794 wait_worklist(&dirrem->dm_list, "pwrwait");
1795 goto top;
1796 }
1797 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1798 (COMPLETE | ONWORKLIST))
1799 break;
1800 }
1801 if (dirrem == NULL)
1802 return;
1803 remove_from_worklist(&dirrem->dm_list);
1804 FREE_LOCK(ump);
1805 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1806 panic("process_removes: suspended filesystem");
1807 handle_workitem_remove(dirrem, 0);
1808 vn_finished_secondary_write(mp);
1809 ACQUIRE_LOCK(ump);
1810 }
1811 }
1812
1813 /*
1814 * Process all truncations associated with a vnode if we are running out
1815 * of journal space. This is called when the vnode lock is already held
1816 * and no other process can clear the truncation. This function returns
1817 * a value greater than zero if it did any work.
1818 */
1819 static void
process_truncates(struct vnode * vp)1820 process_truncates(struct vnode *vp)
1821 {
1822 struct inodedep *inodedep;
1823 struct freeblks *freeblks;
1824 struct ufsmount *ump;
1825 struct mount *mp;
1826 ino_t inum;
1827 int cgwait;
1828
1829 mp = vp->v_mount;
1830 ump = VFSTOUFS(mp);
1831 LOCK_OWNED(ump);
1832 inum = VTOI(vp)->i_number;
1833 for (;;) {
1834 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1835 return;
1836 cgwait = 0;
1837 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1838 /* Journal entries not yet written. */
1839 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1840 jwait(&LIST_FIRST(
1841 &freeblks->fb_jblkdephd)->jb_list,
1842 MNT_WAIT);
1843 break;
1844 }
1845 /* Another thread is executing this item. */
1846 if (freeblks->fb_state & INPROGRESS) {
1847 wait_worklist(&freeblks->fb_list, "ptrwait");
1848 break;
1849 }
1850 /* Freeblks is waiting on a inode write. */
1851 if ((freeblks->fb_state & COMPLETE) == 0) {
1852 FREE_LOCK(ump);
1853 ffs_update(vp, 1);
1854 ACQUIRE_LOCK(ump);
1855 break;
1856 }
1857 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1858 (ALLCOMPLETE | ONWORKLIST)) {
1859 remove_from_worklist(&freeblks->fb_list);
1860 freeblks->fb_state |= INPROGRESS;
1861 FREE_LOCK(ump);
1862 if (vn_start_secondary_write(NULL, &mp,
1863 V_NOWAIT))
1864 panic("process_truncates: "
1865 "suspended filesystem");
1866 handle_workitem_freeblocks(freeblks, 0);
1867 vn_finished_secondary_write(mp);
1868 ACQUIRE_LOCK(ump);
1869 break;
1870 }
1871 if (freeblks->fb_cgwait)
1872 cgwait++;
1873 }
1874 if (cgwait) {
1875 FREE_LOCK(ump);
1876 sync_cgs(mp, MNT_WAIT);
1877 ffs_sync_snap(mp, MNT_WAIT);
1878 ACQUIRE_LOCK(ump);
1879 continue;
1880 }
1881 if (freeblks == NULL)
1882 break;
1883 }
1884 return;
1885 }
1886
1887 /*
1888 * Process one item on the worklist.
1889 */
1890 static int
process_worklist_item(struct mount * mp,int target,int flags)1891 process_worklist_item(struct mount *mp,
1892 int target,
1893 int flags)
1894 {
1895 struct worklist sentinel;
1896 struct worklist *wk;
1897 struct ufsmount *ump;
1898 int matchcnt;
1899 int error;
1900
1901 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1902 /*
1903 * If we are being called because of a process doing a
1904 * copy-on-write, then it is not safe to write as we may
1905 * recurse into the copy-on-write routine.
1906 */
1907 if (curthread->td_pflags & TDP_COWINPROGRESS)
1908 return (-1);
1909 PHOLD(curproc); /* Don't let the stack go away. */
1910 ump = VFSTOUFS(mp);
1911 LOCK_OWNED(ump);
1912 matchcnt = 0;
1913 sentinel.wk_mp = NULL;
1914 sentinel.wk_type = D_SENTINEL;
1915 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1916 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1917 wk = LIST_NEXT(&sentinel, wk_list)) {
1918 if (wk->wk_type == D_SENTINEL) {
1919 LIST_REMOVE(&sentinel, wk_list);
1920 LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1921 continue;
1922 }
1923 if (wk->wk_state & INPROGRESS)
1924 panic("process_worklist_item: %p already in progress.",
1925 wk);
1926 wk->wk_state |= INPROGRESS;
1927 remove_from_worklist(wk);
1928 FREE_LOCK(ump);
1929 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1930 panic("process_worklist_item: suspended filesystem");
1931 switch (wk->wk_type) {
1932 case D_DIRREM:
1933 /* removal of a directory entry */
1934 error = handle_workitem_remove(WK_DIRREM(wk), flags);
1935 break;
1936
1937 case D_FREEBLKS:
1938 /* releasing blocks and/or fragments from a file */
1939 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
1940 flags);
1941 break;
1942
1943 case D_FREEFRAG:
1944 /* releasing a fragment when replaced as a file grows */
1945 handle_workitem_freefrag(WK_FREEFRAG(wk));
1946 error = 0;
1947 break;
1948
1949 case D_FREEFILE:
1950 /* releasing an inode when its link count drops to 0 */
1951 handle_workitem_freefile(WK_FREEFILE(wk));
1952 error = 0;
1953 break;
1954
1955 default:
1956 panic("%s_process_worklist: Unknown type %s",
1957 "softdep", TYPENAME(wk->wk_type));
1958 /* NOTREACHED */
1959 }
1960 vn_finished_secondary_write(mp);
1961 ACQUIRE_LOCK(ump);
1962 if (error == 0) {
1963 if (++matchcnt == target)
1964 break;
1965 continue;
1966 }
1967 /*
1968 * We have to retry the worklist item later. Wake up any
1969 * waiters who may be able to complete it immediately and
1970 * add the item back to the head so we don't try to execute
1971 * it again.
1972 */
1973 wk->wk_state &= ~INPROGRESS;
1974 wake_worklist(wk);
1975 add_to_worklist(wk, WK_HEAD);
1976 }
1977 /* Sentinal could've become the tail from remove_from_worklist. */
1978 if (ump->softdep_worklist_tail == &sentinel)
1979 ump->softdep_worklist_tail =
1980 (struct worklist *)sentinel.wk_list.le_prev;
1981 LIST_REMOVE(&sentinel, wk_list);
1982 PRELE(curproc);
1983 return (matchcnt);
1984 }
1985
1986 /*
1987 * Move dependencies from one buffer to another.
1988 */
1989 int
softdep_move_dependencies(struct buf * oldbp,struct buf * newbp)1990 softdep_move_dependencies(struct buf *oldbp, struct buf *newbp)
1991 {
1992 struct worklist *wk, *wktail;
1993 struct ufsmount *ump;
1994 int dirty;
1995
1996 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
1997 return (0);
1998 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
1999 ("softdep_move_dependencies called on non-softdep filesystem"));
2000 dirty = 0;
2001 wktail = NULL;
2002 ump = VFSTOUFS(wk->wk_mp);
2003 ACQUIRE_LOCK(ump);
2004 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
2005 LIST_REMOVE(wk, wk_list);
2006 if (wk->wk_type == D_BMSAFEMAP &&
2007 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
2008 dirty = 1;
2009 if (wktail == NULL)
2010 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
2011 else
2012 LIST_INSERT_AFTER(wktail, wk, wk_list);
2013 wktail = wk;
2014 }
2015 FREE_LOCK(ump);
2016
2017 return (dirty);
2018 }
2019
2020 /*
2021 * Purge the work list of all items associated with a particular mount point.
2022 */
2023 int
softdep_flushworklist(struct mount * oldmnt,int * countp,struct thread * td)2024 softdep_flushworklist(struct mount *oldmnt,
2025 int *countp,
2026 struct thread *td)
2027 {
2028 struct vnode *devvp;
2029 struct ufsmount *ump;
2030 int count, error;
2031
2032 /*
2033 * Alternately flush the block device associated with the mount
2034 * point and process any dependencies that the flushing
2035 * creates. We continue until no more worklist dependencies
2036 * are found.
2037 */
2038 *countp = 0;
2039 error = 0;
2040 ump = VFSTOUFS(oldmnt);
2041 devvp = ump->um_devvp;
2042 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
2043 *countp += count;
2044 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2045 error = VOP_FSYNC(devvp, MNT_WAIT, td);
2046 VOP_UNLOCK(devvp);
2047 if (error != 0)
2048 break;
2049 }
2050 return (error);
2051 }
2052
2053 #define SU_WAITIDLE_RETRIES 20
2054 static int
softdep_waitidle(struct mount * mp,int flags __unused)2055 softdep_waitidle(struct mount *mp, int flags __unused)
2056 {
2057 struct ufsmount *ump;
2058 struct vnode *devvp;
2059 struct thread *td;
2060 int error, i;
2061
2062 ump = VFSTOUFS(mp);
2063 KASSERT(ump->um_softdep != NULL,
2064 ("softdep_waitidle called on non-softdep filesystem"));
2065 devvp = ump->um_devvp;
2066 td = curthread;
2067 error = 0;
2068 ACQUIRE_LOCK(ump);
2069 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
2070 ump->softdep_req = 1;
2071 KASSERT((flags & FORCECLOSE) == 0 ||
2072 ump->softdep_on_worklist == 0,
2073 ("softdep_waitidle: work added after flush"));
2074 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
2075 "softdeps", 10 * hz);
2076 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2077 error = VOP_FSYNC(devvp, MNT_WAIT, td);
2078 VOP_UNLOCK(devvp);
2079 ACQUIRE_LOCK(ump);
2080 if (error != 0)
2081 break;
2082 }
2083 ump->softdep_req = 0;
2084 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
2085 error = EBUSY;
2086 printf("softdep_waitidle: Failed to flush worklist for %p\n",
2087 mp);
2088 }
2089 FREE_LOCK(ump);
2090 return (error);
2091 }
2092
2093 /*
2094 * Flush all vnodes and worklist items associated with a specified mount point.
2095 */
2096 int
softdep_flushfiles(struct mount * oldmnt,int flags,struct thread * td)2097 softdep_flushfiles(struct mount *oldmnt,
2098 int flags,
2099 struct thread *td)
2100 {
2101 struct ufsmount *ump __unused;
2102 #ifdef QUOTA
2103 int i;
2104 #endif
2105 int error, early, depcount, loopcnt, retry_flush_count, retry;
2106 int morework;
2107
2108 ump = VFSTOUFS(oldmnt);
2109 KASSERT(ump->um_softdep != NULL,
2110 ("softdep_flushfiles called on non-softdep filesystem"));
2111 loopcnt = 10;
2112 retry_flush_count = 3;
2113 retry_flush:
2114 error = 0;
2115
2116 /*
2117 * Alternately flush the vnodes associated with the mount
2118 * point and process any dependencies that the flushing
2119 * creates. In theory, this loop can happen at most twice,
2120 * but we give it a few extra just to be sure.
2121 */
2122 for (; loopcnt > 0; loopcnt--) {
2123 /*
2124 * Do another flush in case any vnodes were brought in
2125 * as part of the cleanup operations.
2126 */
2127 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2128 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2129 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2130 break;
2131 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2132 depcount == 0)
2133 break;
2134 }
2135 /*
2136 * If we are unmounting then it is an error to fail. If we
2137 * are simply trying to downgrade to read-only, then filesystem
2138 * activity can keep us busy forever, so we just fail with EBUSY.
2139 */
2140 if (loopcnt == 0) {
2141 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2142 panic("softdep_flushfiles: looping");
2143 error = EBUSY;
2144 }
2145 if (!error)
2146 error = softdep_waitidle(oldmnt, flags);
2147 if (!error) {
2148 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2149 retry = 0;
2150 MNT_ILOCK(oldmnt);
2151 morework = oldmnt->mnt_nvnodelistsize > 0;
2152 #ifdef QUOTA
2153 UFS_LOCK(ump);
2154 for (i = 0; i < MAXQUOTAS; i++) {
2155 if (ump->um_quotas[i] != NULLVP)
2156 morework = 1;
2157 }
2158 UFS_UNLOCK(ump);
2159 #endif
2160 if (morework) {
2161 if (--retry_flush_count > 0) {
2162 retry = 1;
2163 loopcnt = 3;
2164 } else
2165 error = EBUSY;
2166 }
2167 MNT_IUNLOCK(oldmnt);
2168 if (retry)
2169 goto retry_flush;
2170 }
2171 }
2172 return (error);
2173 }
2174
2175 /*
2176 * Structure hashing.
2177 *
2178 * There are four types of structures that can be looked up:
2179 * 1) pagedep structures identified by mount point, inode number,
2180 * and logical block.
2181 * 2) inodedep structures identified by mount point and inode number.
2182 * 3) newblk structures identified by mount point and
2183 * physical block number.
2184 * 4) bmsafemap structures identified by mount point and
2185 * cylinder group number.
2186 *
2187 * The "pagedep" and "inodedep" dependency structures are hashed
2188 * separately from the file blocks and inodes to which they correspond.
2189 * This separation helps when the in-memory copy of an inode or
2190 * file block must be replaced. It also obviates the need to access
2191 * an inode or file page when simply updating (or de-allocating)
2192 * dependency structures. Lookup of newblk structures is needed to
2193 * find newly allocated blocks when trying to associate them with
2194 * their allocdirect or allocindir structure.
2195 *
2196 * The lookup routines optionally create and hash a new instance when
2197 * an existing entry is not found. The bmsafemap lookup routine always
2198 * allocates a new structure if an existing one is not found.
2199 */
2200 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2201
2202 /*
2203 * Structures and routines associated with pagedep caching.
2204 */
2205 #define PAGEDEP_HASH(ump, inum, lbn) \
2206 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2207
2208 static int
pagedep_find(struct pagedep_hashhead * pagedephd,ino_t ino,ufs_lbn_t lbn,struct pagedep ** pagedeppp)2209 pagedep_find(struct pagedep_hashhead *pagedephd,
2210 ino_t ino,
2211 ufs_lbn_t lbn,
2212 struct pagedep **pagedeppp)
2213 {
2214 struct pagedep *pagedep;
2215
2216 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2217 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2218 *pagedeppp = pagedep;
2219 return (1);
2220 }
2221 }
2222 *pagedeppp = NULL;
2223 return (0);
2224 }
2225 /*
2226 * Look up a pagedep. Return 1 if found, 0 otherwise.
2227 * If not found, allocate if DEPALLOC flag is passed.
2228 * Found or allocated entry is returned in pagedeppp.
2229 */
2230 static int
pagedep_lookup(struct mount * mp,struct buf * bp,ino_t ino,ufs_lbn_t lbn,int flags,struct pagedep ** pagedeppp)2231 pagedep_lookup(struct mount *mp,
2232 struct buf *bp,
2233 ino_t ino,
2234 ufs_lbn_t lbn,
2235 int flags,
2236 struct pagedep **pagedeppp)
2237 {
2238 struct pagedep *pagedep;
2239 struct pagedep_hashhead *pagedephd;
2240 struct worklist *wk;
2241 struct ufsmount *ump;
2242 int ret;
2243 int i;
2244
2245 ump = VFSTOUFS(mp);
2246 LOCK_OWNED(ump);
2247 if (bp) {
2248 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2249 if (wk->wk_type == D_PAGEDEP) {
2250 *pagedeppp = WK_PAGEDEP(wk);
2251 return (1);
2252 }
2253 }
2254 }
2255 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2256 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2257 if (ret) {
2258 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2259 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2260 return (1);
2261 }
2262 if ((flags & DEPALLOC) == 0)
2263 return (0);
2264 FREE_LOCK(ump);
2265 pagedep = malloc(sizeof(struct pagedep),
2266 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2267 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2268 ACQUIRE_LOCK(ump);
2269 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2270 if (*pagedeppp) {
2271 /*
2272 * This should never happen since we only create pagedeps
2273 * with the vnode lock held. Could be an assert.
2274 */
2275 WORKITEM_FREE(pagedep, D_PAGEDEP);
2276 return (ret);
2277 }
2278 pagedep->pd_ino = ino;
2279 pagedep->pd_lbn = lbn;
2280 LIST_INIT(&pagedep->pd_dirremhd);
2281 LIST_INIT(&pagedep->pd_pendinghd);
2282 for (i = 0; i < DAHASHSZ; i++)
2283 LIST_INIT(&pagedep->pd_diraddhd[i]);
2284 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2285 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2286 *pagedeppp = pagedep;
2287 return (0);
2288 }
2289
2290 /*
2291 * Structures and routines associated with inodedep caching.
2292 */
2293 #define INODEDEP_HASH(ump, inum) \
2294 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2295
2296 static int
inodedep_find(struct inodedep_hashhead * inodedephd,ino_t inum,struct inodedep ** inodedeppp)2297 inodedep_find(struct inodedep_hashhead *inodedephd,
2298 ino_t inum,
2299 struct inodedep **inodedeppp)
2300 {
2301 struct inodedep *inodedep;
2302
2303 LIST_FOREACH(inodedep, inodedephd, id_hash)
2304 if (inum == inodedep->id_ino)
2305 break;
2306 if (inodedep) {
2307 *inodedeppp = inodedep;
2308 return (1);
2309 }
2310 *inodedeppp = NULL;
2311
2312 return (0);
2313 }
2314 /*
2315 * Look up an inodedep. Return 1 if found, 0 if not found.
2316 * If not found, allocate if DEPALLOC flag is passed.
2317 * Found or allocated entry is returned in inodedeppp.
2318 */
2319 static int
inodedep_lookup(struct mount * mp,ino_t inum,int flags,struct inodedep ** inodedeppp)2320 inodedep_lookup(struct mount *mp,
2321 ino_t inum,
2322 int flags,
2323 struct inodedep **inodedeppp)
2324 {
2325 struct inodedep *inodedep;
2326 struct inodedep_hashhead *inodedephd;
2327 struct ufsmount *ump;
2328 struct fs *fs;
2329
2330 ump = VFSTOUFS(mp);
2331 LOCK_OWNED(ump);
2332 fs = ump->um_fs;
2333 inodedephd = INODEDEP_HASH(ump, inum);
2334
2335 if (inodedep_find(inodedephd, inum, inodedeppp))
2336 return (1);
2337 if ((flags & DEPALLOC) == 0)
2338 return (0);
2339 /*
2340 * If the system is over its limit and our filesystem is
2341 * responsible for more than our share of that usage and
2342 * we are not in a rush, request some inodedep cleanup.
2343 */
2344 if (softdep_excess_items(ump, D_INODEDEP))
2345 schedule_cleanup(mp);
2346 else
2347 FREE_LOCK(ump);
2348 inodedep = malloc(sizeof(struct inodedep),
2349 M_INODEDEP, M_SOFTDEP_FLAGS);
2350 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2351 ACQUIRE_LOCK(ump);
2352 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2353 WORKITEM_FREE(inodedep, D_INODEDEP);
2354 return (1);
2355 }
2356 inodedep->id_fs = fs;
2357 inodedep->id_ino = inum;
2358 inodedep->id_state = ALLCOMPLETE;
2359 inodedep->id_nlinkdelta = 0;
2360 inodedep->id_nlinkwrote = -1;
2361 inodedep->id_savedino1 = NULL;
2362 inodedep->id_savedsize = -1;
2363 inodedep->id_savedextsize = -1;
2364 inodedep->id_savednlink = -1;
2365 inodedep->id_bmsafemap = NULL;
2366 inodedep->id_mkdiradd = NULL;
2367 LIST_INIT(&inodedep->id_dirremhd);
2368 LIST_INIT(&inodedep->id_pendinghd);
2369 LIST_INIT(&inodedep->id_inowait);
2370 LIST_INIT(&inodedep->id_bufwait);
2371 TAILQ_INIT(&inodedep->id_inoreflst);
2372 TAILQ_INIT(&inodedep->id_inoupdt);
2373 TAILQ_INIT(&inodedep->id_newinoupdt);
2374 TAILQ_INIT(&inodedep->id_extupdt);
2375 TAILQ_INIT(&inodedep->id_newextupdt);
2376 TAILQ_INIT(&inodedep->id_freeblklst);
2377 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2378 *inodedeppp = inodedep;
2379 return (0);
2380 }
2381
2382 /*
2383 * Structures and routines associated with newblk caching.
2384 */
2385 #define NEWBLK_HASH(ump, inum) \
2386 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2387
2388 static int
newblk_find(struct newblk_hashhead * newblkhd,ufs2_daddr_t newblkno,int flags,struct newblk ** newblkpp)2389 newblk_find(struct newblk_hashhead *newblkhd,
2390 ufs2_daddr_t newblkno,
2391 int flags,
2392 struct newblk **newblkpp)
2393 {
2394 struct newblk *newblk;
2395
2396 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2397 if (newblkno != newblk->nb_newblkno)
2398 continue;
2399 /*
2400 * If we're creating a new dependency don't match those that
2401 * have already been converted to allocdirects. This is for
2402 * a frag extend.
2403 */
2404 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2405 continue;
2406 break;
2407 }
2408 if (newblk) {
2409 *newblkpp = newblk;
2410 return (1);
2411 }
2412 *newblkpp = NULL;
2413 return (0);
2414 }
2415
2416 /*
2417 * Look up a newblk. Return 1 if found, 0 if not found.
2418 * If not found, allocate if DEPALLOC flag is passed.
2419 * Found or allocated entry is returned in newblkpp.
2420 */
2421 static int
newblk_lookup(struct mount * mp,ufs2_daddr_t newblkno,int flags,struct newblk ** newblkpp)2422 newblk_lookup(struct mount *mp,
2423 ufs2_daddr_t newblkno,
2424 int flags,
2425 struct newblk **newblkpp)
2426 {
2427 struct newblk *newblk;
2428 struct newblk_hashhead *newblkhd;
2429 struct ufsmount *ump;
2430
2431 ump = VFSTOUFS(mp);
2432 LOCK_OWNED(ump);
2433 newblkhd = NEWBLK_HASH(ump, newblkno);
2434 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2435 return (1);
2436 if ((flags & DEPALLOC) == 0)
2437 return (0);
2438 if (softdep_excess_items(ump, D_NEWBLK) ||
2439 softdep_excess_items(ump, D_ALLOCDIRECT) ||
2440 softdep_excess_items(ump, D_ALLOCINDIR))
2441 schedule_cleanup(mp);
2442 else
2443 FREE_LOCK(ump);
2444 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2445 M_SOFTDEP_FLAGS | M_ZERO);
2446 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2447 ACQUIRE_LOCK(ump);
2448 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2449 WORKITEM_FREE(newblk, D_NEWBLK);
2450 return (1);
2451 }
2452 newblk->nb_freefrag = NULL;
2453 LIST_INIT(&newblk->nb_indirdeps);
2454 LIST_INIT(&newblk->nb_newdirblk);
2455 LIST_INIT(&newblk->nb_jwork);
2456 newblk->nb_state = ATTACHED;
2457 newblk->nb_newblkno = newblkno;
2458 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2459 *newblkpp = newblk;
2460 return (0);
2461 }
2462
2463 /*
2464 * Structures and routines associated with freed indirect block caching.
2465 */
2466 #define INDIR_HASH(ump, blkno) \
2467 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2468
2469 /*
2470 * Lookup an indirect block in the indir hash table. The freework is
2471 * removed and potentially freed. The caller must do a blocking journal
2472 * write before writing to the blkno.
2473 */
2474 static int
indirblk_lookup(struct mount * mp,ufs2_daddr_t blkno)2475 indirblk_lookup(struct mount *mp, ufs2_daddr_t blkno)
2476 {
2477 struct freework *freework;
2478 struct indir_hashhead *wkhd;
2479 struct ufsmount *ump;
2480
2481 ump = VFSTOUFS(mp);
2482 wkhd = INDIR_HASH(ump, blkno);
2483 TAILQ_FOREACH(freework, wkhd, fw_next) {
2484 if (freework->fw_blkno != blkno)
2485 continue;
2486 indirblk_remove(freework);
2487 return (1);
2488 }
2489 return (0);
2490 }
2491
2492 /*
2493 * Insert an indirect block represented by freework into the indirblk
2494 * hash table so that it may prevent the block from being re-used prior
2495 * to the journal being written.
2496 */
2497 static void
indirblk_insert(struct freework * freework)2498 indirblk_insert(struct freework *freework)
2499 {
2500 struct jblocks *jblocks;
2501 struct jseg *jseg;
2502 struct ufsmount *ump;
2503
2504 ump = VFSTOUFS(freework->fw_list.wk_mp);
2505 jblocks = ump->softdep_jblocks;
2506 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2507 if (jseg == NULL)
2508 return;
2509
2510 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2511 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2512 fw_next);
2513 freework->fw_state &= ~DEPCOMPLETE;
2514 }
2515
2516 static void
indirblk_remove(struct freework * freework)2517 indirblk_remove(struct freework *freework)
2518 {
2519 struct ufsmount *ump;
2520
2521 ump = VFSTOUFS(freework->fw_list.wk_mp);
2522 LIST_REMOVE(freework, fw_segs);
2523 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2524 freework->fw_state |= DEPCOMPLETE;
2525 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2526 WORKITEM_FREE(freework, D_FREEWORK);
2527 }
2528
2529 /*
2530 * Executed during filesystem system initialization before
2531 * mounting any filesystems.
2532 */
2533 void
softdep_initialize(void)2534 softdep_initialize(void)
2535 {
2536
2537 TAILQ_INIT(&softdepmounts);
2538 #ifdef __LP64__
2539 max_softdeps = desiredvnodes * 4;
2540 #else
2541 max_softdeps = desiredvnodes * 2;
2542 #endif
2543
2544 /* initialise bioops hack */
2545 bioops.io_start = softdep_disk_io_initiation;
2546 bioops.io_complete = softdep_disk_write_complete;
2547 bioops.io_deallocate = softdep_deallocate_dependencies;
2548 bioops.io_countdeps = softdep_count_dependencies;
2549 ast_register(TDA_UFS, ASTR_KCLEAR | ASTR_ASTF_REQUIRED, 0,
2550 softdep_ast_cleanup_proc);
2551
2552 /* Initialize the callout with an mtx. */
2553 callout_init_mtx(&softdep_callout, &lk, 0);
2554 }
2555
2556 /*
2557 * Executed after all filesystems have been unmounted during
2558 * filesystem module unload.
2559 */
2560 void
softdep_uninitialize(void)2561 softdep_uninitialize(void)
2562 {
2563
2564 /* clear bioops hack */
2565 bioops.io_start = NULL;
2566 bioops.io_complete = NULL;
2567 bioops.io_deallocate = NULL;
2568 bioops.io_countdeps = NULL;
2569 ast_deregister(TDA_UFS);
2570
2571 callout_drain(&softdep_callout);
2572 }
2573
2574 /*
2575 * Called at mount time to notify the dependency code that a
2576 * filesystem wishes to use it.
2577 */
2578 int
softdep_mount(struct vnode * devvp,struct mount * mp,struct fs * fs,struct ucred * cred)2579 softdep_mount(struct vnode *devvp,
2580 struct mount *mp,
2581 struct fs *fs,
2582 struct ucred *cred)
2583 {
2584 struct csum_total cstotal;
2585 struct mount_softdeps *sdp;
2586 struct ufsmount *ump;
2587 struct cg *cgp;
2588 struct buf *bp;
2589 uint64_t cyl, i;
2590 int error;
2591
2592 ump = VFSTOUFS(mp);
2593
2594 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2595 M_WAITOK | M_ZERO);
2596 rw_init(&sdp->sd_fslock, "SUrw");
2597 sdp->sd_ump = ump;
2598 LIST_INIT(&sdp->sd_workitem_pending);
2599 LIST_INIT(&sdp->sd_journal_pending);
2600 TAILQ_INIT(&sdp->sd_unlinked);
2601 LIST_INIT(&sdp->sd_dirtycg);
2602 sdp->sd_worklist_tail = NULL;
2603 sdp->sd_on_worklist = 0;
2604 sdp->sd_deps = 0;
2605 LIST_INIT(&sdp->sd_mkdirlisthd);
2606 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP,
2607 &sdp->sd_pdhashsize);
2608 sdp->sd_pdnextclean = 0;
2609 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP,
2610 &sdp->sd_idhashsize);
2611 sdp->sd_idnextclean = 0;
2612 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK,
2613 &sdp->sd_newblkhashsize);
2614 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize);
2615 i = 1 << (ffs(desiredvnodes / 10) - 1);
2616 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead),
2617 M_FREEWORK, M_WAITOK);
2618 sdp->sd_indirhashsize = i - 1;
2619 for (i = 0; i <= sdp->sd_indirhashsize; i++)
2620 TAILQ_INIT(&sdp->sd_indirhash[i]);
2621 for (i = 0; i <= D_LAST; i++)
2622 LIST_INIT(&sdp->sd_alldeps[i]);
2623 ACQUIRE_GBLLOCK(&lk);
2624 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2625 FREE_GBLLOCK(&lk);
2626
2627 ump->um_softdep = sdp;
2628 MNT_ILOCK(mp);
2629 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2630 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2631 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2632 MNTK_SOFTDEP | MNTK_NOASYNC;
2633 }
2634 MNT_IUNLOCK(mp);
2635
2636 if ((fs->fs_flags & FS_SUJ) &&
2637 (error = journal_mount(mp, fs, cred)) != 0) {
2638 printf("%s: failed to start journal: %d\n",
2639 mp->mnt_stat.f_mntonname, error);
2640 softdep_unmount(mp);
2641 return (error);
2642 }
2643 /*
2644 * Start our flushing thread in the bufdaemon process.
2645 */
2646 ACQUIRE_LOCK(ump);
2647 ump->softdep_flags |= FLUSH_STARTING;
2648 FREE_LOCK(ump);
2649 error = kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2650 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2651 mp->mnt_stat.f_mntonname);
2652 ACQUIRE_LOCK(ump);
2653 if (error != 0) {
2654 printf("%s: failed to start softdepflush thread: %d\n",
2655 mp->mnt_stat.f_mntonname, error);
2656 ump->softdep_flags &= ~FLUSH_STARTING;
2657 FREE_LOCK(ump);
2658 softdep_unmount(mp);
2659 return (error);
2660 }
2661 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2662 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2663 hz / 2);
2664 }
2665 FREE_LOCK(ump);
2666 /*
2667 * When doing soft updates, the counters in the
2668 * superblock may have gotten out of sync. Recomputation
2669 * can take a long time and can be deferred for background
2670 * fsck. However, the old behavior of scanning the cylinder
2671 * groups and recalculating them at mount time is available
2672 * by setting vfs.ffs.compute_summary_at_mount to one.
2673 */
2674 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2675 return (0);
2676 bzero(&cstotal, sizeof cstotal);
2677 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2678 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2679 fs->fs_cgsize, cred, &bp)) != 0) {
2680 brelse(bp);
2681 softdep_unmount(mp);
2682 return (error);
2683 }
2684 cgp = (struct cg *)bp->b_data;
2685 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2686 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2687 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2688 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2689 fs->fs_cs(fs, cyl) = cgp->cg_cs;
2690 brelse(bp);
2691 }
2692 #ifdef INVARIANTS
2693 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2694 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2695 #endif
2696 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2697 return (0);
2698 }
2699
2700 void
softdep_unmount(struct mount * mp)2701 softdep_unmount(struct mount *mp)
2702 {
2703 struct ufsmount *ump;
2704 struct mount_softdeps *ums;
2705
2706 ump = VFSTOUFS(mp);
2707 KASSERT(ump->um_softdep != NULL,
2708 ("softdep_unmount called on non-softdep filesystem"));
2709 MNT_ILOCK(mp);
2710 mp->mnt_flag &= ~MNT_SOFTDEP;
2711 if ((mp->mnt_flag & MNT_SUJ) == 0) {
2712 MNT_IUNLOCK(mp);
2713 } else {
2714 mp->mnt_flag &= ~MNT_SUJ;
2715 MNT_IUNLOCK(mp);
2716 journal_unmount(ump);
2717 }
2718 /*
2719 * Shut down our flushing thread. Check for NULL is if
2720 * softdep_mount errors out before the thread has been created.
2721 */
2722 if (ump->softdep_flushtd != NULL) {
2723 ACQUIRE_LOCK(ump);
2724 ump->softdep_flags |= FLUSH_EXIT;
2725 wakeup(&ump->softdep_flushtd);
2726 while ((ump->softdep_flags & FLUSH_EXIT) != 0) {
2727 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM,
2728 "sdwait", 0);
2729 }
2730 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2731 ("Thread shutdown failed"));
2732 FREE_LOCK(ump);
2733 }
2734
2735 /*
2736 * We are no longer have softdep structure attached to ump.
2737 */
2738 ums = ump->um_softdep;
2739 ACQUIRE_GBLLOCK(&lk);
2740 TAILQ_REMOVE(&softdepmounts, ums, sd_next);
2741 FREE_GBLLOCK(&lk);
2742 ump->um_softdep = NULL;
2743
2744 KASSERT(ums->sd_on_journal == 0,
2745 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal));
2746 KASSERT(ums->sd_on_worklist == 0,
2747 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist));
2748 KASSERT(ums->sd_deps == 0,
2749 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps));
2750
2751 /*
2752 * Free up our resources.
2753 */
2754 rw_destroy(&ums->sd_fslock);
2755 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize);
2756 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize);
2757 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize);
2758 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize);
2759 free(ums->sd_indirhash, M_FREEWORK);
2760 #ifdef INVARIANTS
2761 for (int i = 0; i <= D_LAST; i++) {
2762 KASSERT(ums->sd_curdeps[i] == 0,
2763 ("Unmount %s: Dep type %s != 0 (%jd)", ump->um_fs->fs_fsmnt,
2764 TYPENAME(i), (intmax_t)ums->sd_curdeps[i]));
2765 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]),
2766 ("Unmount %s: Dep type %s not empty (%p)",
2767 ump->um_fs->fs_fsmnt,
2768 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i])));
2769 }
2770 #endif
2771 free(ums, M_MOUNTDATA);
2772 }
2773
2774 static struct jblocks *
jblocks_create(void)2775 jblocks_create(void)
2776 {
2777 struct jblocks *jblocks;
2778
2779 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2780 TAILQ_INIT(&jblocks->jb_segs);
2781 jblocks->jb_avail = 10;
2782 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2783 M_JBLOCKS, M_WAITOK | M_ZERO);
2784
2785 return (jblocks);
2786 }
2787
2788 static ufs2_daddr_t
jblocks_alloc(struct jblocks * jblocks,int bytes,int * actual)2789 jblocks_alloc(struct jblocks *jblocks,
2790 int bytes,
2791 int *actual)
2792 {
2793 ufs2_daddr_t daddr;
2794 struct jextent *jext;
2795 int freecnt;
2796 int blocks;
2797
2798 blocks = bytes / DEV_BSIZE;
2799 jext = &jblocks->jb_extent[jblocks->jb_head];
2800 freecnt = jext->je_blocks - jblocks->jb_off;
2801 if (freecnt == 0) {
2802 jblocks->jb_off = 0;
2803 if (++jblocks->jb_head > jblocks->jb_used)
2804 jblocks->jb_head = 0;
2805 jext = &jblocks->jb_extent[jblocks->jb_head];
2806 freecnt = jext->je_blocks;
2807 }
2808 if (freecnt > blocks)
2809 freecnt = blocks;
2810 *actual = freecnt * DEV_BSIZE;
2811 daddr = jext->je_daddr + jblocks->jb_off;
2812 jblocks->jb_off += freecnt;
2813 jblocks->jb_free -= freecnt;
2814
2815 return (daddr);
2816 }
2817
2818 static void
jblocks_free(struct jblocks * jblocks,struct mount * mp,int bytes)2819 jblocks_free(struct jblocks *jblocks,
2820 struct mount *mp,
2821 int bytes)
2822 {
2823
2824 LOCK_OWNED(VFSTOUFS(mp));
2825 jblocks->jb_free += bytes / DEV_BSIZE;
2826 if (jblocks->jb_suspended)
2827 worklist_speedup(mp);
2828 wakeup(jblocks);
2829 }
2830
2831 static void
jblocks_destroy(struct jblocks * jblocks)2832 jblocks_destroy(struct jblocks *jblocks)
2833 {
2834
2835 if (jblocks->jb_extent)
2836 free(jblocks->jb_extent, M_JBLOCKS);
2837 free(jblocks, M_JBLOCKS);
2838 }
2839
2840 static void
jblocks_add(struct jblocks * jblocks,ufs2_daddr_t daddr,int blocks)2841 jblocks_add(struct jblocks *jblocks,
2842 ufs2_daddr_t daddr,
2843 int blocks)
2844 {
2845 struct jextent *jext;
2846
2847 jblocks->jb_blocks += blocks;
2848 jblocks->jb_free += blocks;
2849 jext = &jblocks->jb_extent[jblocks->jb_used];
2850 /* Adding the first block. */
2851 if (jext->je_daddr == 0) {
2852 jext->je_daddr = daddr;
2853 jext->je_blocks = blocks;
2854 return;
2855 }
2856 /* Extending the last extent. */
2857 if (jext->je_daddr + jext->je_blocks == daddr) {
2858 jext->je_blocks += blocks;
2859 return;
2860 }
2861 /* Adding a new extent. */
2862 if (++jblocks->jb_used == jblocks->jb_avail) {
2863 jblocks->jb_avail *= 2;
2864 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2865 M_JBLOCKS, M_WAITOK | M_ZERO);
2866 memcpy(jext, jblocks->jb_extent,
2867 sizeof(struct jextent) * jblocks->jb_used);
2868 free(jblocks->jb_extent, M_JBLOCKS);
2869 jblocks->jb_extent = jext;
2870 }
2871 jext = &jblocks->jb_extent[jblocks->jb_used];
2872 jext->je_daddr = daddr;
2873 jext->je_blocks = blocks;
2874 return;
2875 }
2876
2877 int
softdep_journal_lookup(struct mount * mp,struct vnode ** vpp)2878 softdep_journal_lookup(struct mount *mp, struct vnode **vpp)
2879 {
2880 struct componentname cnp;
2881 struct vnode *dvp;
2882 ino_t sujournal;
2883 int error;
2884
2885 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2886 if (error)
2887 return (error);
2888 bzero(&cnp, sizeof(cnp));
2889 cnp.cn_nameiop = LOOKUP;
2890 cnp.cn_flags = ISLASTCN;
2891 cnp.cn_cred = curthread->td_ucred;
2892 cnp.cn_pnbuf = SUJ_FILE;
2893 cnp.cn_nameptr = SUJ_FILE;
2894 cnp.cn_namelen = strlen(SUJ_FILE);
2895 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2896 vput(dvp);
2897 if (error != 0)
2898 return (error);
2899 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2900 return (error);
2901 }
2902
2903 /*
2904 * Open and verify the journal file.
2905 */
2906 static int
journal_mount(struct mount * mp,struct fs * fs,struct ucred * cred)2907 journal_mount(struct mount *mp,
2908 struct fs *fs,
2909 struct ucred *cred)
2910 {
2911 struct jblocks *jblocks;
2912 struct ufsmount *ump;
2913 struct vnode *vp;
2914 struct inode *ip;
2915 ufs2_daddr_t blkno;
2916 int bcount;
2917 int error;
2918 int i;
2919
2920 ump = VFSTOUFS(mp);
2921 ump->softdep_journal_tail = NULL;
2922 ump->softdep_on_journal = 0;
2923 ump->softdep_accdeps = 0;
2924 ump->softdep_req = 0;
2925 ump->softdep_jblocks = NULL;
2926 error = softdep_journal_lookup(mp, &vp);
2927 if (error != 0) {
2928 printf("Failed to find journal. Use tunefs to create one\n");
2929 return (error);
2930 }
2931 ip = VTOI(vp);
2932 if (ip->i_size < SUJ_MIN) {
2933 error = ENOSPC;
2934 goto out;
2935 }
2936 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */
2937 jblocks = jblocks_create();
2938 for (i = 0; i < bcount; i++) {
2939 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
2940 if (error)
2941 break;
2942 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
2943 }
2944 if (error) {
2945 jblocks_destroy(jblocks);
2946 goto out;
2947 }
2948 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */
2949 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
2950 ump->softdep_jblocks = jblocks;
2951
2952 MNT_ILOCK(mp);
2953 mp->mnt_flag |= MNT_SUJ;
2954 MNT_IUNLOCK(mp);
2955
2956 /*
2957 * Only validate the journal contents if the
2958 * filesystem is clean, otherwise we write the logs
2959 * but they'll never be used. If the filesystem was
2960 * still dirty when we mounted it the journal is
2961 * invalid and a new journal can only be valid if it
2962 * starts from a clean mount.
2963 */
2964 if (fs->fs_clean) {
2965 DIP_SET(ip, i_modrev, fs->fs_mtime);
2966 ip->i_flags |= IN_MODIFIED;
2967 ffs_update(vp, 1);
2968 }
2969 out:
2970 vput(vp);
2971 return (error);
2972 }
2973
2974 static void
journal_unmount(struct ufsmount * ump)2975 journal_unmount(struct ufsmount *ump)
2976 {
2977
2978 if (ump->softdep_jblocks)
2979 jblocks_destroy(ump->softdep_jblocks);
2980 ump->softdep_jblocks = NULL;
2981 }
2982
2983 /*
2984 * Called when a journal record is ready to be written. Space is allocated
2985 * and the journal entry is created when the journal is flushed to stable
2986 * store.
2987 */
2988 static void
add_to_journal(struct worklist * wk)2989 add_to_journal(struct worklist *wk)
2990 {
2991 struct ufsmount *ump;
2992
2993 ump = VFSTOUFS(wk->wk_mp);
2994 LOCK_OWNED(ump);
2995 if (wk->wk_state & ONWORKLIST)
2996 panic("add_to_journal: %s(0x%X) already on list",
2997 TYPENAME(wk->wk_type), wk->wk_state);
2998 wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
2999 if (LIST_EMPTY(&ump->softdep_journal_pending)) {
3000 ump->softdep_jblocks->jb_age = ticks;
3001 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
3002 } else
3003 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
3004 ump->softdep_journal_tail = wk;
3005 ump->softdep_on_journal += 1;
3006 }
3007
3008 /*
3009 * Remove an arbitrary item for the journal worklist maintain the tail
3010 * pointer. This happens when a new operation obviates the need to
3011 * journal an old operation.
3012 */
3013 static void
remove_from_journal(struct worklist * wk)3014 remove_from_journal(struct worklist *wk)
3015 {
3016 struct ufsmount *ump;
3017
3018 ump = VFSTOUFS(wk->wk_mp);
3019 LOCK_OWNED(ump);
3020 #ifdef INVARIANTS
3021 {
3022 struct worklist *wkn;
3023
3024 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
3025 if (wkn == wk)
3026 break;
3027 if (wkn == NULL)
3028 panic("remove_from_journal: %p is not in journal", wk);
3029 }
3030 #endif
3031 /*
3032 * We emulate a TAILQ to save space in most structures which do not
3033 * require TAILQ semantics. Here we must update the tail position
3034 * when removing the tail which is not the final entry. This works
3035 * only if the worklist linkage are at the beginning of the structure.
3036 */
3037 if (ump->softdep_journal_tail == wk)
3038 ump->softdep_journal_tail =
3039 (struct worklist *)wk->wk_list.le_prev;
3040 WORKLIST_REMOVE(wk);
3041 ump->softdep_on_journal -= 1;
3042 }
3043
3044 /*
3045 * Check for journal space as well as dependency limits so the prelink
3046 * code can throttle both journaled and non-journaled filesystems.
3047 * Threshold is 0 for low and 1 for min.
3048 */
3049 static int
journal_space(struct ufsmount * ump,int thresh)3050 journal_space(struct ufsmount *ump, int thresh)
3051 {
3052 struct jblocks *jblocks;
3053 int limit, avail;
3054
3055 jblocks = ump->softdep_jblocks;
3056 if (jblocks == NULL)
3057 return (1);
3058 /*
3059 * We use a tighter restriction here to prevent request_cleanup()
3060 * running in threads from running into locks we currently hold.
3061 * We have to be over the limit and our filesystem has to be
3062 * responsible for more than our share of that usage.
3063 */
3064 limit = (max_softdeps / 10) * 9;
3065 if (dep_current[D_INODEDEP] > limit &&
3066 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
3067 return (0);
3068 if (thresh)
3069 thresh = jblocks->jb_min;
3070 else
3071 thresh = jblocks->jb_low;
3072 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
3073 avail = jblocks->jb_free - avail;
3074
3075 return (avail > thresh);
3076 }
3077
3078 static void
journal_suspend(struct ufsmount * ump)3079 journal_suspend(struct ufsmount *ump)
3080 {
3081 struct jblocks *jblocks;
3082 struct mount *mp;
3083 bool set;
3084
3085 mp = UFSTOVFS(ump);
3086 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0)
3087 return;
3088
3089 jblocks = ump->softdep_jblocks;
3090 vfs_op_enter(mp);
3091 set = false;
3092 MNT_ILOCK(mp);
3093 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
3094 stat_journal_min++;
3095 mp->mnt_kern_flag |= MNTK_SUSPEND;
3096 mp->mnt_susp_owner = ump->softdep_flushtd;
3097 set = true;
3098 }
3099 jblocks->jb_suspended = 1;
3100 MNT_IUNLOCK(mp);
3101 if (!set)
3102 vfs_op_exit(mp);
3103 }
3104
3105 static int
journal_unsuspend(struct ufsmount * ump)3106 journal_unsuspend(struct ufsmount *ump)
3107 {
3108 struct jblocks *jblocks;
3109 struct mount *mp;
3110
3111 mp = UFSTOVFS(ump);
3112 jblocks = ump->softdep_jblocks;
3113
3114 if (jblocks != NULL && jblocks->jb_suspended &&
3115 journal_space(ump, jblocks->jb_min)) {
3116 jblocks->jb_suspended = 0;
3117 FREE_LOCK(ump);
3118 mp->mnt_susp_owner = curthread;
3119 vfs_write_resume(mp, 0);
3120 ACQUIRE_LOCK(ump);
3121 return (1);
3122 }
3123 return (0);
3124 }
3125
3126 static void
journal_check_space(struct ufsmount * ump)3127 journal_check_space(struct ufsmount *ump)
3128 {
3129 struct mount *mp;
3130
3131 LOCK_OWNED(ump);
3132
3133 if (journal_space(ump, 0) == 0) {
3134 softdep_speedup(ump);
3135 mp = UFSTOVFS(ump);
3136 FREE_LOCK(ump);
3137 VFS_SYNC(mp, MNT_NOWAIT);
3138 ffs_sbupdate(ump, MNT_WAIT, 0);
3139 ACQUIRE_LOCK(ump);
3140 if (journal_space(ump, 1) == 0)
3141 journal_suspend(ump);
3142 }
3143 }
3144
3145 /*
3146 * Called before any allocation function to be certain that there is
3147 * sufficient space in the journal prior to creating any new records.
3148 * Since in the case of block allocation we may have multiple locked
3149 * buffers at the time of the actual allocation we can not block
3150 * when the journal records are created. Doing so would create a deadlock
3151 * if any of these buffers needed to be flushed to reclaim space. Instead
3152 * we require a sufficiently large amount of available space such that
3153 * each thread in the system could have passed this allocation check and
3154 * still have sufficient free space. With 20% of a minimum journal size
3155 * of 1MB we have 6553 records available.
3156 */
3157 int
softdep_prealloc(struct vnode * vp,int waitok)3158 softdep_prealloc(struct vnode *vp, int waitok)
3159 {
3160 struct ufsmount *ump;
3161
3162 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3163 ("softdep_prealloc called on non-softdep filesystem"));
3164 /*
3165 * Nothing to do if we are not running journaled soft updates.
3166 * If we currently hold the snapshot lock, we must avoid
3167 * handling other resources that could cause deadlock. Do not
3168 * touch quotas vnode since it is typically recursed with
3169 * other vnode locks held.
3170 */
3171 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3172 (vp->v_vflag & VV_SYSTEM) != 0)
3173 return (0);
3174 ump = VFSTOUFS(vp->v_mount);
3175 ACQUIRE_LOCK(ump);
3176 if (journal_space(ump, 0)) {
3177 FREE_LOCK(ump);
3178 return (0);
3179 }
3180 stat_journal_low++;
3181 FREE_LOCK(ump);
3182 if (waitok == MNT_NOWAIT)
3183 return (ENOSPC);
3184 /*
3185 * Attempt to sync this vnode once to flush any journal
3186 * work attached to it.
3187 */
3188 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3189 ffs_syncvnode(vp, waitok, 0);
3190 ACQUIRE_LOCK(ump);
3191 process_removes(vp);
3192 process_truncates(vp);
3193 journal_check_space(ump);
3194 FREE_LOCK(ump);
3195
3196 return (0);
3197 }
3198
3199 /*
3200 * Try hard to sync all data and metadata for the vnode, and workitems
3201 * flushing which might conflict with the vnode lock. This is a
3202 * helper for softdep_prerename().
3203 */
3204 static int
softdep_prerename_vnode(struct ufsmount * ump,struct vnode * vp)3205 softdep_prerename_vnode(struct ufsmount *ump, struct vnode *vp)
3206 {
3207 int error;
3208
3209 ASSERT_VOP_ELOCKED(vp, "prehandle");
3210 if (vp->v_data == NULL)
3211 return (0);
3212 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3213 if (error != 0)
3214 return (error);
3215 ACQUIRE_LOCK(ump);
3216 process_removes(vp);
3217 process_truncates(vp);
3218 FREE_LOCK(ump);
3219 return (0);
3220 }
3221
3222 /*
3223 * Must be called from VOP_RENAME() after all vnodes are locked.
3224 * Ensures that there is enough journal space for rename. It is
3225 * sufficiently different from softdep_prelink() by having to handle
3226 * four vnodes.
3227 */
3228 int
softdep_prerename(struct vnode * fdvp,struct vnode * fvp,struct vnode * tdvp,struct vnode * tvp)3229 softdep_prerename(struct vnode *fdvp,
3230 struct vnode *fvp,
3231 struct vnode *tdvp,
3232 struct vnode *tvp)
3233 {
3234 struct ufsmount *ump;
3235 int error;
3236
3237 ump = VFSTOUFS(fdvp->v_mount);
3238
3239 if (journal_space(ump, 0))
3240 return (0);
3241
3242 VOP_UNLOCK(tdvp);
3243 VOP_UNLOCK(fvp);
3244 if (tvp != NULL && tvp != tdvp)
3245 VOP_UNLOCK(tvp);
3246
3247 error = softdep_prerename_vnode(ump, fdvp);
3248 VOP_UNLOCK(fdvp);
3249 if (error != 0)
3250 return (error);
3251
3252 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY);
3253 error = softdep_prerename_vnode(ump, fvp);
3254 VOP_UNLOCK(fvp);
3255 if (error != 0)
3256 return (error);
3257
3258 if (tdvp != fdvp) {
3259 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY);
3260 error = softdep_prerename_vnode(ump, tdvp);
3261 VOP_UNLOCK(tdvp);
3262 if (error != 0)
3263 return (error);
3264 }
3265
3266 if (tvp != fvp && tvp != NULL) {
3267 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY);
3268 error = softdep_prerename_vnode(ump, tvp);
3269 VOP_UNLOCK(tvp);
3270 if (error != 0)
3271 return (error);
3272 }
3273
3274 ACQUIRE_LOCK(ump);
3275 softdep_speedup(ump);
3276 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3277 journal_check_space(ump);
3278 FREE_LOCK(ump);
3279 return (ERELOOKUP);
3280 }
3281
3282 /*
3283 * Before adjusting a link count on a vnode verify that we have sufficient
3284 * journal space. If not, process operations that depend on the currently
3285 * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3286 * and softdep flush threads can not acquire these locks to reclaim space.
3287 *
3288 * Returns 0 if all owned locks are still valid and were not dropped
3289 * in the process, in other case it returns either an error from sync,
3290 * or ERELOOKUP if any of the locks were re-acquired. In the later
3291 * case, the state of the vnodes cannot be relied upon and our VFS
3292 * syscall must be restarted at top level from the lookup.
3293 */
3294 int
softdep_prelink(struct vnode * dvp,struct vnode * vp,struct componentname * cnp)3295 softdep_prelink(struct vnode *dvp,
3296 struct vnode *vp,
3297 struct componentname *cnp)
3298 {
3299 struct ufsmount *ump;
3300 struct nameidata *ndp;
3301
3302 ASSERT_VOP_ELOCKED(dvp, "prelink dvp");
3303 if (vp != NULL)
3304 ASSERT_VOP_ELOCKED(vp, "prelink vp");
3305 ump = VFSTOUFS(dvp->v_mount);
3306
3307 /*
3308 * Nothing to do if we have sufficient journal space. We skip
3309 * flushing when vp is a snapshot to avoid deadlock where
3310 * another thread is trying to update the inodeblock for dvp
3311 * and is waiting on snaplk that vp holds.
3312 */
3313 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp))))
3314 return (0);
3315
3316 /*
3317 * Check if the journal space consumption can in theory be
3318 * accounted on dvp and vp. If the vnodes metadata was not
3319 * changed comparing with the previous round-trip into
3320 * softdep_prelink(), as indicated by the seqc generation
3321 * recorded in the nameidata, then there is no point in
3322 * starting the sync.
3323 */
3324 ndp = __containerof(cnp, struct nameidata, ni_cnd);
3325 if (!seqc_in_modify(ndp->ni_dvp_seqc) &&
3326 vn_seqc_consistent(dvp, ndp->ni_dvp_seqc) &&
3327 (vp == NULL || (!seqc_in_modify(ndp->ni_vp_seqc) &&
3328 vn_seqc_consistent(vp, ndp->ni_vp_seqc))))
3329 return (0);
3330
3331 stat_journal_low++;
3332 if (vp != NULL) {
3333 VOP_UNLOCK(dvp);
3334 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3335 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, true, LK_EXCLUSIVE);
3336 if (dvp->v_data == NULL)
3337 goto out;
3338 }
3339 if (vp != NULL)
3340 VOP_UNLOCK(vp);
3341 ffs_syncvnode(dvp, MNT_WAIT, 0);
3342 /* Process vp before dvp as it may create .. removes. */
3343 if (vp != NULL) {
3344 VOP_UNLOCK(dvp);
3345 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3346 if (vp->v_data == NULL) {
3347 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, true,
3348 LK_EXCLUSIVE);
3349 goto out;
3350 }
3351 ACQUIRE_LOCK(ump);
3352 process_removes(vp);
3353 process_truncates(vp);
3354 FREE_LOCK(ump);
3355 VOP_UNLOCK(vp);
3356 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
3357 if (dvp->v_data == NULL) {
3358 vn_lock_pair(dvp, true, LK_EXCLUSIVE, vp, false,
3359 LK_EXCLUSIVE);
3360 goto out;
3361 }
3362 }
3363
3364 ACQUIRE_LOCK(ump);
3365 process_removes(dvp);
3366 process_truncates(dvp);
3367 VOP_UNLOCK(dvp);
3368 softdep_speedup(ump);
3369
3370 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3371 journal_check_space(ump);
3372 FREE_LOCK(ump);
3373
3374 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, false, LK_EXCLUSIVE);
3375 out:
3376 ndp->ni_dvp_seqc = vn_seqc_read_any(dvp);
3377 if (vp != NULL)
3378 ndp->ni_vp_seqc = vn_seqc_read_any(vp);
3379 return (ERELOOKUP);
3380 }
3381
3382 static void
jseg_write(struct ufsmount * ump,struct jseg * jseg,uint8_t * data)3383 jseg_write(struct ufsmount *ump,
3384 struct jseg *jseg,
3385 uint8_t *data)
3386 {
3387 struct jsegrec *rec;
3388
3389 rec = (struct jsegrec *)data;
3390 rec->jsr_seq = jseg->js_seq;
3391 rec->jsr_oldest = jseg->js_oldseq;
3392 rec->jsr_cnt = jseg->js_cnt;
3393 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3394 rec->jsr_crc = 0;
3395 rec->jsr_time = ump->um_fs->fs_mtime;
3396 }
3397
3398 static inline void
inoref_write(struct inoref * inoref,struct jseg * jseg,struct jrefrec * rec)3399 inoref_write(struct inoref *inoref,
3400 struct jseg *jseg,
3401 struct jrefrec *rec)
3402 {
3403
3404 inoref->if_jsegdep->jd_seg = jseg;
3405 rec->jr_ino = inoref->if_ino;
3406 rec->jr_parent = inoref->if_parent;
3407 rec->jr_nlink = inoref->if_nlink;
3408 rec->jr_mode = inoref->if_mode;
3409 rec->jr_diroff = inoref->if_diroff;
3410 }
3411
3412 static void
jaddref_write(struct jaddref * jaddref,struct jseg * jseg,uint8_t * data)3413 jaddref_write(struct jaddref *jaddref,
3414 struct jseg *jseg,
3415 uint8_t *data)
3416 {
3417 struct jrefrec *rec;
3418
3419 rec = (struct jrefrec *)data;
3420 rec->jr_op = JOP_ADDREF;
3421 inoref_write(&jaddref->ja_ref, jseg, rec);
3422 }
3423
3424 static void
jremref_write(struct jremref * jremref,struct jseg * jseg,uint8_t * data)3425 jremref_write(struct jremref *jremref,
3426 struct jseg *jseg,
3427 uint8_t *data)
3428 {
3429 struct jrefrec *rec;
3430
3431 rec = (struct jrefrec *)data;
3432 rec->jr_op = JOP_REMREF;
3433 inoref_write(&jremref->jr_ref, jseg, rec);
3434 }
3435
3436 static void
jmvref_write(struct jmvref * jmvref,struct jseg * jseg,uint8_t * data)3437 jmvref_write(struct jmvref *jmvref,
3438 struct jseg *jseg,
3439 uint8_t *data)
3440 {
3441 struct jmvrec *rec;
3442
3443 rec = (struct jmvrec *)data;
3444 rec->jm_op = JOP_MVREF;
3445 rec->jm_ino = jmvref->jm_ino;
3446 rec->jm_parent = jmvref->jm_parent;
3447 rec->jm_oldoff = jmvref->jm_oldoff;
3448 rec->jm_newoff = jmvref->jm_newoff;
3449 }
3450
3451 static void
jnewblk_write(struct jnewblk * jnewblk,struct jseg * jseg,uint8_t * data)3452 jnewblk_write(struct jnewblk *jnewblk,
3453 struct jseg *jseg,
3454 uint8_t *data)
3455 {
3456 struct jblkrec *rec;
3457
3458 jnewblk->jn_jsegdep->jd_seg = jseg;
3459 rec = (struct jblkrec *)data;
3460 rec->jb_op = JOP_NEWBLK;
3461 rec->jb_ino = jnewblk->jn_ino;
3462 rec->jb_blkno = jnewblk->jn_blkno;
3463 rec->jb_lbn = jnewblk->jn_lbn;
3464 rec->jb_frags = jnewblk->jn_frags;
3465 rec->jb_oldfrags = jnewblk->jn_oldfrags;
3466 }
3467
3468 static void
jfreeblk_write(struct jfreeblk * jfreeblk,struct jseg * jseg,uint8_t * data)3469 jfreeblk_write(struct jfreeblk *jfreeblk,
3470 struct jseg *jseg,
3471 uint8_t *data)
3472 {
3473 struct jblkrec *rec;
3474
3475 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3476 rec = (struct jblkrec *)data;
3477 rec->jb_op = JOP_FREEBLK;
3478 rec->jb_ino = jfreeblk->jf_ino;
3479 rec->jb_blkno = jfreeblk->jf_blkno;
3480 rec->jb_lbn = jfreeblk->jf_lbn;
3481 rec->jb_frags = jfreeblk->jf_frags;
3482 rec->jb_oldfrags = 0;
3483 }
3484
3485 static void
jfreefrag_write(struct jfreefrag * jfreefrag,struct jseg * jseg,uint8_t * data)3486 jfreefrag_write(struct jfreefrag *jfreefrag,
3487 struct jseg *jseg,
3488 uint8_t *data)
3489 {
3490 struct jblkrec *rec;
3491
3492 jfreefrag->fr_jsegdep->jd_seg = jseg;
3493 rec = (struct jblkrec *)data;
3494 rec->jb_op = JOP_FREEBLK;
3495 rec->jb_ino = jfreefrag->fr_ino;
3496 rec->jb_blkno = jfreefrag->fr_blkno;
3497 rec->jb_lbn = jfreefrag->fr_lbn;
3498 rec->jb_frags = jfreefrag->fr_frags;
3499 rec->jb_oldfrags = 0;
3500 }
3501
3502 static void
jtrunc_write(struct jtrunc * jtrunc,struct jseg * jseg,uint8_t * data)3503 jtrunc_write(struct jtrunc *jtrunc,
3504 struct jseg *jseg,
3505 uint8_t *data)
3506 {
3507 struct jtrncrec *rec;
3508
3509 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3510 rec = (struct jtrncrec *)data;
3511 rec->jt_op = JOP_TRUNC;
3512 rec->jt_ino = jtrunc->jt_ino;
3513 rec->jt_size = jtrunc->jt_size;
3514 rec->jt_extsize = jtrunc->jt_extsize;
3515 }
3516
3517 static void
jfsync_write(struct jfsync * jfsync,struct jseg * jseg,uint8_t * data)3518 jfsync_write(struct jfsync *jfsync,
3519 struct jseg *jseg,
3520 uint8_t *data)
3521 {
3522 struct jtrncrec *rec;
3523
3524 rec = (struct jtrncrec *)data;
3525 rec->jt_op = JOP_SYNC;
3526 rec->jt_ino = jfsync->jfs_ino;
3527 rec->jt_size = jfsync->jfs_size;
3528 rec->jt_extsize = jfsync->jfs_extsize;
3529 }
3530
3531 static void
softdep_flushjournal(struct mount * mp)3532 softdep_flushjournal(struct mount *mp)
3533 {
3534 struct jblocks *jblocks;
3535 struct ufsmount *ump;
3536
3537 if (MOUNTEDSUJ(mp) == 0)
3538 return;
3539 ump = VFSTOUFS(mp);
3540 jblocks = ump->softdep_jblocks;
3541 ACQUIRE_LOCK(ump);
3542 while (ump->softdep_on_journal) {
3543 jblocks->jb_needseg = 1;
3544 softdep_process_journal(mp, NULL, MNT_WAIT);
3545 }
3546 FREE_LOCK(ump);
3547 }
3548
3549 static void softdep_synchronize_completed(struct bio *);
3550 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3551
3552 static void
softdep_synchronize_completed(struct bio * bp)3553 softdep_synchronize_completed(struct bio *bp)
3554 {
3555 struct jseg *oldest;
3556 struct jseg *jseg;
3557 struct ufsmount *ump;
3558
3559 /*
3560 * caller1 marks the last segment written before we issued the
3561 * synchronize cache.
3562 */
3563 jseg = bp->bio_caller1;
3564 if (jseg == NULL) {
3565 g_destroy_bio(bp);
3566 return;
3567 }
3568 ump = VFSTOUFS(jseg->js_list.wk_mp);
3569 ACQUIRE_LOCK(ump);
3570 oldest = NULL;
3571 /*
3572 * Mark all the journal entries waiting on the synchronize cache
3573 * as completed so they may continue on.
3574 */
3575 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3576 jseg->js_state |= COMPLETE;
3577 oldest = jseg;
3578 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3579 }
3580 /*
3581 * Restart deferred journal entry processing from the oldest
3582 * completed jseg.
3583 */
3584 if (oldest)
3585 complete_jsegs(oldest);
3586
3587 FREE_LOCK(ump);
3588 g_destroy_bio(bp);
3589 }
3590
3591 /*
3592 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3593 * barriers. The journal must be written prior to any blocks that depend
3594 * on it and the journal can not be released until the blocks have be
3595 * written. This code handles both barriers simultaneously.
3596 */
3597 static void
softdep_synchronize(struct bio * bp,struct ufsmount * ump,void * caller1)3598 softdep_synchronize(struct bio *bp,
3599 struct ufsmount *ump,
3600 void *caller1)
3601 {
3602
3603 bp->bio_cmd = BIO_FLUSH;
3604 bp->bio_flags |= BIO_ORDERED;
3605 bp->bio_data = NULL;
3606 bp->bio_offset = ump->um_cp->provider->mediasize;
3607 bp->bio_length = 0;
3608 bp->bio_done = softdep_synchronize_completed;
3609 bp->bio_caller1 = caller1;
3610 g_io_request(bp, ump->um_cp);
3611 }
3612
3613 /*
3614 * Flush some journal records to disk.
3615 */
3616 static void
softdep_process_journal(struct mount * mp,struct worklist * needwk,int flags)3617 softdep_process_journal(struct mount *mp,
3618 struct worklist *needwk,
3619 int flags)
3620 {
3621 struct jblocks *jblocks;
3622 struct ufsmount *ump;
3623 struct worklist *wk;
3624 struct jseg *jseg;
3625 struct buf *bp;
3626 struct bio *bio;
3627 uint8_t *data;
3628 struct fs *fs;
3629 int shouldflush;
3630 int segwritten;
3631 int jrecmin; /* Minimum records per block. */
3632 int jrecmax; /* Maximum records per block. */
3633 int size;
3634 int cnt;
3635 int off;
3636 int devbsize;
3637 int savef;
3638
3639 ump = VFSTOUFS(mp);
3640 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL)
3641 return;
3642 shouldflush = softdep_flushcache;
3643 bio = NULL;
3644 jseg = NULL;
3645 LOCK_OWNED(ump);
3646 fs = ump->um_fs;
3647 jblocks = ump->softdep_jblocks;
3648 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3649 savef = curthread_pflags_set(TDP_NORUNNINGBUF);
3650
3651 /*
3652 * We write anywhere between a disk block and fs block. The upper
3653 * bound is picked to prevent buffer cache fragmentation and limit
3654 * processing time per I/O.
3655 */
3656 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3657 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3658 segwritten = 0;
3659 for (;;) {
3660 cnt = ump->softdep_on_journal;
3661 /*
3662 * Criteria for writing a segment:
3663 * 1) We have a full block.
3664 * 2) We're called from jwait() and haven't found the
3665 * journal item yet.
3666 * 3) Always write if needseg is set.
3667 * 4) If we are called from process_worklist and have
3668 * not yet written anything we write a partial block
3669 * to enforce a 1 second maximum latency on journal
3670 * entries.
3671 */
3672 if (cnt < (jrecmax - 1) && needwk == NULL &&
3673 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3674 break;
3675 cnt++;
3676 /*
3677 * Verify some free journal space. softdep_prealloc() should
3678 * guarantee that we don't run out so this is indicative of
3679 * a problem with the flow control. Try to recover
3680 * gracefully in any event.
3681 */
3682 while (jblocks->jb_free == 0) {
3683 if (flags != MNT_WAIT)
3684 break;
3685 printf("softdep: Out of journal space!\n");
3686 softdep_speedup(ump);
3687 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3688 }
3689 FREE_LOCK(ump);
3690 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3691 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3692 LIST_INIT(&jseg->js_entries);
3693 LIST_INIT(&jseg->js_indirs);
3694 jseg->js_state = ATTACHED;
3695 if (shouldflush == 0)
3696 jseg->js_state |= COMPLETE;
3697 else if (bio == NULL)
3698 bio = g_alloc_bio();
3699 jseg->js_jblocks = jblocks;
3700 bp = geteblk(fs->fs_bsize, 0);
3701 ACQUIRE_LOCK(ump);
3702 /*
3703 * If there was a race while we were allocating the block
3704 * and jseg the entry we care about was likely written.
3705 * We bail out in both the WAIT and NOWAIT case and assume
3706 * the caller will loop if the entry it cares about is
3707 * not written.
3708 */
3709 cnt = ump->softdep_on_journal;
3710 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3711 bp->b_flags |= B_INVAL | B_NOCACHE;
3712 WORKITEM_FREE(jseg, D_JSEG);
3713 FREE_LOCK(ump);
3714 brelse(bp);
3715 ACQUIRE_LOCK(ump);
3716 break;
3717 }
3718 /*
3719 * Calculate the disk block size required for the available
3720 * records rounded to the min size.
3721 */
3722 if (cnt == 0)
3723 size = devbsize;
3724 else if (cnt < jrecmax)
3725 size = howmany(cnt, jrecmin) * devbsize;
3726 else
3727 size = fs->fs_bsize;
3728 /*
3729 * Allocate a disk block for this journal data and account
3730 * for truncation of the requested size if enough contiguous
3731 * space was not available.
3732 */
3733 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3734 bp->b_lblkno = bp->b_blkno;
3735 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3736 bp->b_bcount = size;
3737 bp->b_flags &= ~B_INVAL;
3738 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3739 /*
3740 * Initialize our jseg with cnt records. Assign the next
3741 * sequence number to it and link it in-order.
3742 */
3743 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3744 jseg->js_buf = bp;
3745 jseg->js_cnt = cnt;
3746 jseg->js_refs = cnt + 1; /* Self ref. */
3747 jseg->js_size = size;
3748 jseg->js_seq = jblocks->jb_nextseq++;
3749 if (jblocks->jb_oldestseg == NULL)
3750 jblocks->jb_oldestseg = jseg;
3751 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3752 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3753 if (jblocks->jb_writeseg == NULL)
3754 jblocks->jb_writeseg = jseg;
3755 /*
3756 * Start filling in records from the pending list.
3757 */
3758 data = bp->b_data;
3759 off = 0;
3760
3761 /*
3762 * Always put a header on the first block.
3763 * XXX As with below, there might not be a chance to get
3764 * into the loop. Ensure that something valid is written.
3765 */
3766 jseg_write(ump, jseg, data);
3767 off += JREC_SIZE;
3768 data = bp->b_data + off;
3769
3770 /*
3771 * XXX Something is wrong here. There's no work to do,
3772 * but we need to perform and I/O and allow it to complete
3773 * anyways.
3774 */
3775 if (LIST_EMPTY(&ump->softdep_journal_pending))
3776 stat_emptyjblocks++;
3777
3778 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3779 != NULL) {
3780 if (cnt == 0)
3781 break;
3782 /* Place a segment header on every device block. */
3783 if ((off % devbsize) == 0) {
3784 jseg_write(ump, jseg, data);
3785 off += JREC_SIZE;
3786 data = bp->b_data + off;
3787 }
3788 if (wk == needwk)
3789 needwk = NULL;
3790 remove_from_journal(wk);
3791 wk->wk_state |= INPROGRESS;
3792 WORKLIST_INSERT(&jseg->js_entries, wk);
3793 switch (wk->wk_type) {
3794 case D_JADDREF:
3795 jaddref_write(WK_JADDREF(wk), jseg, data);
3796 break;
3797 case D_JREMREF:
3798 jremref_write(WK_JREMREF(wk), jseg, data);
3799 break;
3800 case D_JMVREF:
3801 jmvref_write(WK_JMVREF(wk), jseg, data);
3802 break;
3803 case D_JNEWBLK:
3804 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3805 break;
3806 case D_JFREEBLK:
3807 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3808 break;
3809 case D_JFREEFRAG:
3810 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3811 break;
3812 case D_JTRUNC:
3813 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3814 break;
3815 case D_JFSYNC:
3816 jfsync_write(WK_JFSYNC(wk), jseg, data);
3817 break;
3818 default:
3819 panic("process_journal: Unknown type %s",
3820 TYPENAME(wk->wk_type));
3821 /* NOTREACHED */
3822 }
3823 off += JREC_SIZE;
3824 data = bp->b_data + off;
3825 cnt--;
3826 }
3827
3828 /* Clear any remaining space so we don't leak kernel data */
3829 if (size > off)
3830 bzero(data, size - off);
3831
3832 /*
3833 * Write this one buffer and continue.
3834 */
3835 segwritten = 1;
3836 jblocks->jb_needseg = 0;
3837 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3838 FREE_LOCK(ump);
3839 bp->b_xflags |= BX_CVTENXIO;
3840 pbgetvp(ump->um_devvp, bp);
3841 /*
3842 * We only do the blocking wait once we find the journal
3843 * entry we're looking for.
3844 */
3845 if (needwk == NULL && flags == MNT_WAIT)
3846 bwrite(bp);
3847 else
3848 bawrite(bp);
3849 ACQUIRE_LOCK(ump);
3850 }
3851 /*
3852 * If we wrote a segment issue a synchronize cache so the journal
3853 * is reflected on disk before the data is written. Since reclaiming
3854 * journal space also requires writing a journal record this
3855 * process also enforces a barrier before reclamation.
3856 */
3857 if (segwritten && shouldflush) {
3858 softdep_synchronize(bio, ump,
3859 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3860 } else if (bio)
3861 g_destroy_bio(bio);
3862 /*
3863 * If we've suspended the filesystem because we ran out of journal
3864 * space either try to sync it here to make some progress or
3865 * unsuspend it if we already have.
3866 */
3867 if (flags == 0 && jblocks->jb_suspended) {
3868 if (journal_unsuspend(ump))
3869 goto out;
3870 FREE_LOCK(ump);
3871 VFS_SYNC(mp, MNT_NOWAIT);
3872 ffs_sbupdate(ump, MNT_WAIT, 0);
3873 ACQUIRE_LOCK(ump);
3874 }
3875
3876 out:
3877 curthread_pflags_restore(savef);
3878 }
3879
3880 /*
3881 * Complete a jseg, allowing all dependencies awaiting journal writes
3882 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3883 * structures so that the journal segment can be freed to reclaim space.
3884 */
3885 static void
complete_jseg(struct jseg * jseg)3886 complete_jseg(struct jseg *jseg)
3887 {
3888 struct worklist *wk;
3889 struct jmvref *jmvref;
3890 #ifdef INVARIANTS
3891 int i = 0;
3892 #endif
3893
3894 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3895 WORKLIST_REMOVE(wk);
3896 wk->wk_state &= ~INPROGRESS;
3897 wk->wk_state |= COMPLETE;
3898 KASSERT(i++ < jseg->js_cnt,
3899 ("handle_written_jseg: overflow %d >= %d",
3900 i - 1, jseg->js_cnt));
3901 switch (wk->wk_type) {
3902 case D_JADDREF:
3903 handle_written_jaddref(WK_JADDREF(wk));
3904 break;
3905 case D_JREMREF:
3906 handle_written_jremref(WK_JREMREF(wk));
3907 break;
3908 case D_JMVREF:
3909 rele_jseg(jseg); /* No jsegdep. */
3910 jmvref = WK_JMVREF(wk);
3911 LIST_REMOVE(jmvref, jm_deps);
3912 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3913 free_pagedep(jmvref->jm_pagedep);
3914 WORKITEM_FREE(jmvref, D_JMVREF);
3915 break;
3916 case D_JNEWBLK:
3917 handle_written_jnewblk(WK_JNEWBLK(wk));
3918 break;
3919 case D_JFREEBLK:
3920 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3921 break;
3922 case D_JTRUNC:
3923 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3924 break;
3925 case D_JFSYNC:
3926 rele_jseg(jseg); /* No jsegdep. */
3927 WORKITEM_FREE(wk, D_JFSYNC);
3928 break;
3929 case D_JFREEFRAG:
3930 handle_written_jfreefrag(WK_JFREEFRAG(wk));
3931 break;
3932 default:
3933 panic("handle_written_jseg: Unknown type %s",
3934 TYPENAME(wk->wk_type));
3935 /* NOTREACHED */
3936 }
3937 }
3938 /* Release the self reference so the structure may be freed. */
3939 rele_jseg(jseg);
3940 }
3941
3942 /*
3943 * Determine which jsegs are ready for completion processing. Waits for
3944 * synchronize cache to complete as well as forcing in-order completion
3945 * of journal entries.
3946 */
3947 static void
complete_jsegs(struct jseg * jseg)3948 complete_jsegs(struct jseg *jseg)
3949 {
3950 struct jblocks *jblocks;
3951 struct jseg *jsegn;
3952
3953 jblocks = jseg->js_jblocks;
3954 /*
3955 * Don't allow out of order completions. If this isn't the first
3956 * block wait for it to write before we're done.
3957 */
3958 if (jseg != jblocks->jb_writeseg)
3959 return;
3960 /* Iterate through available jsegs processing their entries. */
3961 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
3962 jblocks->jb_oldestwrseq = jseg->js_oldseq;
3963 jsegn = TAILQ_NEXT(jseg, js_next);
3964 complete_jseg(jseg);
3965 jseg = jsegn;
3966 }
3967 jblocks->jb_writeseg = jseg;
3968 /*
3969 * Attempt to free jsegs now that oldestwrseq may have advanced.
3970 */
3971 free_jsegs(jblocks);
3972 }
3973
3974 /*
3975 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
3976 * the final completions.
3977 */
3978 static void
handle_written_jseg(struct jseg * jseg,struct buf * bp)3979 handle_written_jseg(struct jseg *jseg, struct buf *bp)
3980 {
3981
3982 if (jseg->js_refs == 0)
3983 panic("handle_written_jseg: No self-reference on %p", jseg);
3984 jseg->js_state |= DEPCOMPLETE;
3985 /*
3986 * We'll never need this buffer again, set flags so it will be
3987 * discarded.
3988 */
3989 bp->b_flags |= B_INVAL | B_NOCACHE;
3990 pbrelvp(bp);
3991 complete_jsegs(jseg);
3992 }
3993
3994 static inline struct jsegdep *
inoref_jseg(struct inoref * inoref)3995 inoref_jseg(struct inoref *inoref)
3996 {
3997 struct jsegdep *jsegdep;
3998
3999 jsegdep = inoref->if_jsegdep;
4000 inoref->if_jsegdep = NULL;
4001
4002 return (jsegdep);
4003 }
4004
4005 /*
4006 * Called once a jremref has made it to stable store. The jremref is marked
4007 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
4008 * for the jremref to complete will be awoken by free_jremref.
4009 */
4010 static void
handle_written_jremref(struct jremref * jremref)4011 handle_written_jremref(struct jremref *jremref)
4012 {
4013 struct inodedep *inodedep;
4014 struct jsegdep *jsegdep;
4015 struct dirrem *dirrem;
4016
4017 /* Grab the jsegdep. */
4018 jsegdep = inoref_jseg(&jremref->jr_ref);
4019 /*
4020 * Remove us from the inoref list.
4021 */
4022 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
4023 0, &inodedep) == 0)
4024 panic("handle_written_jremref: Lost inodedep");
4025 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
4026 /*
4027 * Complete the dirrem.
4028 */
4029 dirrem = jremref->jr_dirrem;
4030 jremref->jr_dirrem = NULL;
4031 LIST_REMOVE(jremref, jr_deps);
4032 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
4033 jwork_insert(&dirrem->dm_jwork, jsegdep);
4034 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
4035 (dirrem->dm_state & COMPLETE) != 0)
4036 add_to_worklist(&dirrem->dm_list, 0);
4037 free_jremref(jremref);
4038 }
4039
4040 /*
4041 * Called once a jaddref has made it to stable store. The dependency is
4042 * marked complete and any dependent structures are added to the inode
4043 * bufwait list to be completed as soon as it is written. If a bitmap write
4044 * depends on this entry we move the inode into the inodedephd of the
4045 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
4046 */
4047 static void
handle_written_jaddref(struct jaddref * jaddref)4048 handle_written_jaddref(struct jaddref *jaddref)
4049 {
4050 struct jsegdep *jsegdep;
4051 struct inodedep *inodedep;
4052 struct diradd *diradd;
4053 struct mkdir *mkdir;
4054
4055 /* Grab the jsegdep. */
4056 jsegdep = inoref_jseg(&jaddref->ja_ref);
4057 mkdir = NULL;
4058 diradd = NULL;
4059 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4060 0, &inodedep) == 0)
4061 panic("handle_written_jaddref: Lost inodedep.");
4062 if (jaddref->ja_diradd == NULL)
4063 panic("handle_written_jaddref: No dependency");
4064 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
4065 diradd = jaddref->ja_diradd;
4066 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
4067 } else if (jaddref->ja_state & MKDIR_PARENT) {
4068 mkdir = jaddref->ja_mkdir;
4069 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
4070 } else if (jaddref->ja_state & MKDIR_BODY)
4071 mkdir = jaddref->ja_mkdir;
4072 else
4073 panic("handle_written_jaddref: Unknown dependency %p",
4074 jaddref->ja_diradd);
4075 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
4076 /*
4077 * Remove us from the inode list.
4078 */
4079 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
4080 /*
4081 * The mkdir may be waiting on the jaddref to clear before freeing.
4082 */
4083 if (mkdir) {
4084 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
4085 ("handle_written_jaddref: Incorrect type for mkdir %s",
4086 TYPENAME(mkdir->md_list.wk_type)));
4087 mkdir->md_jaddref = NULL;
4088 diradd = mkdir->md_diradd;
4089 mkdir->md_state |= DEPCOMPLETE;
4090 complete_mkdir(mkdir);
4091 }
4092 jwork_insert(&diradd->da_jwork, jsegdep);
4093 if (jaddref->ja_state & NEWBLOCK) {
4094 inodedep->id_state |= ONDEPLIST;
4095 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
4096 inodedep, id_deps);
4097 }
4098 free_jaddref(jaddref);
4099 }
4100
4101 /*
4102 * Called once a jnewblk journal is written. The allocdirect or allocindir
4103 * is placed in the bmsafemap to await notification of a written bitmap. If
4104 * the operation was canceled we add the segdep to the appropriate
4105 * dependency to free the journal space once the canceling operation
4106 * completes.
4107 */
4108 static void
handle_written_jnewblk(struct jnewblk * jnewblk)4109 handle_written_jnewblk(struct jnewblk *jnewblk)
4110 {
4111 struct bmsafemap *bmsafemap;
4112 struct freefrag *freefrag;
4113 struct freework *freework;
4114 struct jsegdep *jsegdep;
4115 struct newblk *newblk;
4116
4117 /* Grab the jsegdep. */
4118 jsegdep = jnewblk->jn_jsegdep;
4119 jnewblk->jn_jsegdep = NULL;
4120 if (jnewblk->jn_dep == NULL)
4121 panic("handle_written_jnewblk: No dependency for the segdep.");
4122 switch (jnewblk->jn_dep->wk_type) {
4123 case D_NEWBLK:
4124 case D_ALLOCDIRECT:
4125 case D_ALLOCINDIR:
4126 /*
4127 * Add the written block to the bmsafemap so it can
4128 * be notified when the bitmap is on disk.
4129 */
4130 newblk = WK_NEWBLK(jnewblk->jn_dep);
4131 newblk->nb_jnewblk = NULL;
4132 if ((newblk->nb_state & GOINGAWAY) == 0) {
4133 bmsafemap = newblk->nb_bmsafemap;
4134 newblk->nb_state |= ONDEPLIST;
4135 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
4136 nb_deps);
4137 }
4138 jwork_insert(&newblk->nb_jwork, jsegdep);
4139 break;
4140 case D_FREEFRAG:
4141 /*
4142 * A newblock being removed by a freefrag when replaced by
4143 * frag extension.
4144 */
4145 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
4146 freefrag->ff_jdep = NULL;
4147 jwork_insert(&freefrag->ff_jwork, jsegdep);
4148 break;
4149 case D_FREEWORK:
4150 /*
4151 * A direct block was removed by truncate.
4152 */
4153 freework = WK_FREEWORK(jnewblk->jn_dep);
4154 freework->fw_jnewblk = NULL;
4155 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
4156 break;
4157 default:
4158 panic("handle_written_jnewblk: Unknown type %d.",
4159 jnewblk->jn_dep->wk_type);
4160 }
4161 jnewblk->jn_dep = NULL;
4162 free_jnewblk(jnewblk);
4163 }
4164
4165 /*
4166 * Cancel a jfreefrag that won't be needed, probably due to colliding with
4167 * an in-flight allocation that has not yet been committed. Divorce us
4168 * from the freefrag and mark it DEPCOMPLETE so that it may be added
4169 * to the worklist.
4170 */
4171 static void
cancel_jfreefrag(struct jfreefrag * jfreefrag)4172 cancel_jfreefrag(struct jfreefrag *jfreefrag)
4173 {
4174 struct freefrag *freefrag;
4175
4176 if (jfreefrag->fr_jsegdep) {
4177 free_jsegdep(jfreefrag->fr_jsegdep);
4178 jfreefrag->fr_jsegdep = NULL;
4179 }
4180 freefrag = jfreefrag->fr_freefrag;
4181 jfreefrag->fr_freefrag = NULL;
4182 free_jfreefrag(jfreefrag);
4183 freefrag->ff_state |= DEPCOMPLETE;
4184 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
4185 }
4186
4187 /*
4188 * Free a jfreefrag when the parent freefrag is rendered obsolete.
4189 */
4190 static void
free_jfreefrag(struct jfreefrag * jfreefrag)4191 free_jfreefrag(struct jfreefrag *jfreefrag)
4192 {
4193
4194 if (jfreefrag->fr_state & INPROGRESS)
4195 WORKLIST_REMOVE(&jfreefrag->fr_list);
4196 else if (jfreefrag->fr_state & ONWORKLIST)
4197 remove_from_journal(&jfreefrag->fr_list);
4198 if (jfreefrag->fr_freefrag != NULL)
4199 panic("free_jfreefrag: Still attached to a freefrag.");
4200 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
4201 }
4202
4203 /*
4204 * Called when the journal write for a jfreefrag completes. The parent
4205 * freefrag is added to the worklist if this completes its dependencies.
4206 */
4207 static void
handle_written_jfreefrag(struct jfreefrag * jfreefrag)4208 handle_written_jfreefrag(struct jfreefrag *jfreefrag)
4209 {
4210 struct jsegdep *jsegdep;
4211 struct freefrag *freefrag;
4212
4213 /* Grab the jsegdep. */
4214 jsegdep = jfreefrag->fr_jsegdep;
4215 jfreefrag->fr_jsegdep = NULL;
4216 freefrag = jfreefrag->fr_freefrag;
4217 if (freefrag == NULL)
4218 panic("handle_written_jfreefrag: No freefrag.");
4219 freefrag->ff_state |= DEPCOMPLETE;
4220 freefrag->ff_jdep = NULL;
4221 jwork_insert(&freefrag->ff_jwork, jsegdep);
4222 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
4223 add_to_worklist(&freefrag->ff_list, 0);
4224 jfreefrag->fr_freefrag = NULL;
4225 free_jfreefrag(jfreefrag);
4226 }
4227
4228 /*
4229 * Called when the journal write for a jfreeblk completes. The jfreeblk
4230 * is removed from the freeblks list of pending journal writes and the
4231 * jsegdep is moved to the freeblks jwork to be completed when all blocks
4232 * have been reclaimed.
4233 */
4234 static void
handle_written_jblkdep(struct jblkdep * jblkdep)4235 handle_written_jblkdep(struct jblkdep *jblkdep)
4236 {
4237 struct freeblks *freeblks;
4238 struct jsegdep *jsegdep;
4239
4240 /* Grab the jsegdep. */
4241 jsegdep = jblkdep->jb_jsegdep;
4242 jblkdep->jb_jsegdep = NULL;
4243 freeblks = jblkdep->jb_freeblks;
4244 LIST_REMOVE(jblkdep, jb_deps);
4245 jwork_insert(&freeblks->fb_jwork, jsegdep);
4246 /*
4247 * If the freeblks is all journaled, we can add it to the worklist.
4248 */
4249 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4250 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4251 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4252
4253 free_jblkdep(jblkdep);
4254 }
4255
4256 static struct jsegdep *
newjsegdep(struct worklist * wk)4257 newjsegdep(struct worklist *wk)
4258 {
4259 struct jsegdep *jsegdep;
4260
4261 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4262 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4263 jsegdep->jd_seg = NULL;
4264
4265 return (jsegdep);
4266 }
4267
4268 static struct jmvref *
newjmvref(struct inode * dp,ino_t ino,off_t oldoff,off_t newoff)4269 newjmvref(struct inode *dp,
4270 ino_t ino,
4271 off_t oldoff,
4272 off_t newoff)
4273 {
4274 struct jmvref *jmvref;
4275
4276 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4277 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4278 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4279 jmvref->jm_parent = dp->i_number;
4280 jmvref->jm_ino = ino;
4281 jmvref->jm_oldoff = oldoff;
4282 jmvref->jm_newoff = newoff;
4283
4284 return (jmvref);
4285 }
4286
4287 /*
4288 * Allocate a new jremref that tracks the removal of ip from dp with the
4289 * directory entry offset of diroff. Mark the entry as ATTACHED and
4290 * DEPCOMPLETE as we have all the information required for the journal write
4291 * and the directory has already been removed from the buffer. The caller
4292 * is responsible for linking the jremref into the pagedep and adding it
4293 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4294 * a DOTDOT addition so handle_workitem_remove() can properly assign
4295 * the jsegdep when we're done.
4296 */
4297 static struct jremref *
newjremref(struct dirrem * dirrem,struct inode * dp,struct inode * ip,off_t diroff,nlink_t nlink)4298 newjremref(struct dirrem *dirrem,
4299 struct inode *dp,
4300 struct inode *ip,
4301 off_t diroff,
4302 nlink_t nlink)
4303 {
4304 struct jremref *jremref;
4305
4306 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4307 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4308 jremref->jr_state = ATTACHED;
4309 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4310 nlink, ip->i_mode);
4311 jremref->jr_dirrem = dirrem;
4312
4313 return (jremref);
4314 }
4315
4316 static inline void
newinoref(struct inoref * inoref,ino_t ino,ino_t parent,off_t diroff,nlink_t nlink,uint16_t mode)4317 newinoref(struct inoref *inoref,
4318 ino_t ino,
4319 ino_t parent,
4320 off_t diroff,
4321 nlink_t nlink,
4322 uint16_t mode)
4323 {
4324
4325 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4326 inoref->if_diroff = diroff;
4327 inoref->if_ino = ino;
4328 inoref->if_parent = parent;
4329 inoref->if_nlink = nlink;
4330 inoref->if_mode = mode;
4331 }
4332
4333 /*
4334 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4335 * directory offset may not be known until later. The caller is responsible
4336 * adding the entry to the journal when this information is available. nlink
4337 * should be the link count prior to the addition and mode is only required
4338 * to have the correct FMT.
4339 */
4340 static struct jaddref *
newjaddref(struct inode * dp,ino_t ino,off_t diroff,int16_t nlink,uint16_t mode)4341 newjaddref(struct inode *dp,
4342 ino_t ino,
4343 off_t diroff,
4344 int16_t nlink,
4345 uint16_t mode)
4346 {
4347 struct jaddref *jaddref;
4348
4349 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4350 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4351 jaddref->ja_state = ATTACHED;
4352 jaddref->ja_mkdir = NULL;
4353 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4354
4355 return (jaddref);
4356 }
4357
4358 /*
4359 * Create a new free dependency for a freework. The caller is responsible
4360 * for adjusting the reference count when it has the lock held. The freedep
4361 * will track an outstanding bitmap write that will ultimately clear the
4362 * freework to continue.
4363 */
4364 static struct freedep *
newfreedep(struct freework * freework)4365 newfreedep(struct freework *freework)
4366 {
4367 struct freedep *freedep;
4368
4369 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4370 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4371 freedep->fd_freework = freework;
4372
4373 return (freedep);
4374 }
4375
4376 /*
4377 * Free a freedep structure once the buffer it is linked to is written. If
4378 * this is the last reference to the freework schedule it for completion.
4379 */
4380 static void
free_freedep(struct freedep * freedep)4381 free_freedep(struct freedep *freedep)
4382 {
4383 struct freework *freework;
4384
4385 freework = freedep->fd_freework;
4386 freework->fw_freeblks->fb_cgwait--;
4387 if (--freework->fw_ref == 0)
4388 freework_enqueue(freework);
4389 WORKITEM_FREE(freedep, D_FREEDEP);
4390 }
4391
4392 /*
4393 * Allocate a new freework structure that may be a level in an indirect
4394 * when parent is not NULL or a top level block when it is. The top level
4395 * freework structures are allocated without the per-filesystem lock held
4396 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4397 */
4398 static struct freework *
newfreework(struct ufsmount * ump,struct freeblks * freeblks,struct freework * parent,ufs_lbn_t lbn,ufs2_daddr_t nb,int frags,int off,int journal)4399 newfreework(struct ufsmount *ump,
4400 struct freeblks *freeblks,
4401 struct freework *parent,
4402 ufs_lbn_t lbn,
4403 ufs2_daddr_t nb,
4404 int frags,
4405 int off,
4406 int journal)
4407 {
4408 struct freework *freework;
4409
4410 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4411 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4412 freework->fw_state = ATTACHED;
4413 freework->fw_jnewblk = NULL;
4414 freework->fw_freeblks = freeblks;
4415 freework->fw_parent = parent;
4416 freework->fw_lbn = lbn;
4417 freework->fw_blkno = nb;
4418 freework->fw_frags = frags;
4419 freework->fw_indir = NULL;
4420 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4421 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4422 freework->fw_start = freework->fw_off = off;
4423 if (journal)
4424 newjfreeblk(freeblks, lbn, nb, frags);
4425 if (parent == NULL) {
4426 ACQUIRE_LOCK(ump);
4427 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4428 freeblks->fb_ref++;
4429 FREE_LOCK(ump);
4430 }
4431
4432 return (freework);
4433 }
4434
4435 /*
4436 * Eliminate a jfreeblk for a block that does not need journaling.
4437 */
4438 static void
cancel_jfreeblk(struct freeblks * freeblks,ufs2_daddr_t blkno)4439 cancel_jfreeblk(struct freeblks *freeblks, ufs2_daddr_t blkno)
4440 {
4441 struct jfreeblk *jfreeblk;
4442 struct jblkdep *jblkdep;
4443
4444 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4445 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4446 continue;
4447 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4448 if (jfreeblk->jf_blkno == blkno)
4449 break;
4450 }
4451 if (jblkdep == NULL)
4452 return;
4453 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4454 free_jsegdep(jblkdep->jb_jsegdep);
4455 LIST_REMOVE(jblkdep, jb_deps);
4456 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4457 }
4458
4459 /*
4460 * Allocate a new jfreeblk to journal top level block pointer when truncating
4461 * a file. The caller must add this to the worklist when the per-filesystem
4462 * lock is held.
4463 */
4464 static struct jfreeblk *
newjfreeblk(struct freeblks * freeblks,ufs_lbn_t lbn,ufs2_daddr_t blkno,int frags)4465 newjfreeblk(struct freeblks *freeblks,
4466 ufs_lbn_t lbn,
4467 ufs2_daddr_t blkno,
4468 int frags)
4469 {
4470 struct jfreeblk *jfreeblk;
4471
4472 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4473 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4474 freeblks->fb_list.wk_mp);
4475 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4476 jfreeblk->jf_dep.jb_freeblks = freeblks;
4477 jfreeblk->jf_ino = freeblks->fb_inum;
4478 jfreeblk->jf_lbn = lbn;
4479 jfreeblk->jf_blkno = blkno;
4480 jfreeblk->jf_frags = frags;
4481 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4482
4483 return (jfreeblk);
4484 }
4485
4486 /*
4487 * The journal is only prepared to handle full-size block numbers, so we
4488 * have to adjust the record to reflect the change to a full-size block.
4489 * For example, suppose we have a block made up of fragments 8-15 and
4490 * want to free its last two fragments. We are given a request that says:
4491 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4492 * where frags are the number of fragments to free and oldfrags are the
4493 * number of fragments to keep. To block align it, we have to change it to
4494 * have a valid full-size blkno, so it becomes:
4495 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4496 */
4497 static void
adjust_newfreework(struct freeblks * freeblks,int frag_offset)4498 adjust_newfreework(struct freeblks *freeblks, int frag_offset)
4499 {
4500 struct jfreeblk *jfreeblk;
4501
4502 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4503 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4504 ("adjust_newfreework: Missing freeblks dependency"));
4505
4506 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4507 jfreeblk->jf_blkno -= frag_offset;
4508 jfreeblk->jf_frags += frag_offset;
4509 }
4510
4511 /*
4512 * Allocate a new jtrunc to track a partial truncation.
4513 */
4514 static struct jtrunc *
newjtrunc(struct freeblks * freeblks,off_t size,int extsize)4515 newjtrunc(struct freeblks *freeblks,
4516 off_t size,
4517 int extsize)
4518 {
4519 struct jtrunc *jtrunc;
4520
4521 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4522 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4523 freeblks->fb_list.wk_mp);
4524 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4525 jtrunc->jt_dep.jb_freeblks = freeblks;
4526 jtrunc->jt_ino = freeblks->fb_inum;
4527 jtrunc->jt_size = size;
4528 jtrunc->jt_extsize = extsize;
4529 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4530
4531 return (jtrunc);
4532 }
4533
4534 /*
4535 * If we're canceling a new bitmap we have to search for another ref
4536 * to move into the bmsafemap dep. This might be better expressed
4537 * with another structure.
4538 */
4539 static void
move_newblock_dep(struct jaddref * jaddref,struct inodedep * inodedep)4540 move_newblock_dep(struct jaddref *jaddref, struct inodedep *inodedep)
4541 {
4542 struct inoref *inoref;
4543 struct jaddref *jaddrefn;
4544
4545 jaddrefn = NULL;
4546 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4547 inoref = TAILQ_NEXT(inoref, if_deps)) {
4548 if ((jaddref->ja_state & NEWBLOCK) &&
4549 inoref->if_list.wk_type == D_JADDREF) {
4550 jaddrefn = (struct jaddref *)inoref;
4551 break;
4552 }
4553 }
4554 if (jaddrefn == NULL)
4555 return;
4556 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4557 jaddrefn->ja_state |= jaddref->ja_state &
4558 (ATTACHED | UNDONE | NEWBLOCK);
4559 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4560 jaddref->ja_state |= ATTACHED;
4561 LIST_REMOVE(jaddref, ja_bmdeps);
4562 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4563 ja_bmdeps);
4564 }
4565
4566 /*
4567 * Cancel a jaddref either before it has been written or while it is being
4568 * written. This happens when a link is removed before the add reaches
4569 * the disk. The jaddref dependency is kept linked into the bmsafemap
4570 * and inode to prevent the link count or bitmap from reaching the disk
4571 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4572 * required.
4573 *
4574 * Returns 1 if the canceled addref requires journaling of the remove and
4575 * 0 otherwise.
4576 */
4577 static int
cancel_jaddref(struct jaddref * jaddref,struct inodedep * inodedep,struct workhead * wkhd)4578 cancel_jaddref(struct jaddref *jaddref,
4579 struct inodedep *inodedep,
4580 struct workhead *wkhd)
4581 {
4582 struct inoref *inoref;
4583 struct jsegdep *jsegdep;
4584 int needsj;
4585
4586 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4587 ("cancel_jaddref: Canceling complete jaddref"));
4588 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4589 needsj = 1;
4590 else
4591 needsj = 0;
4592 if (inodedep == NULL)
4593 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4594 0, &inodedep) == 0)
4595 panic("cancel_jaddref: Lost inodedep");
4596 /*
4597 * We must adjust the nlink of any reference operation that follows
4598 * us so that it is consistent with the in-memory reference. This
4599 * ensures that inode nlink rollbacks always have the correct link.
4600 */
4601 if (needsj == 0) {
4602 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4603 inoref = TAILQ_NEXT(inoref, if_deps)) {
4604 if (inoref->if_state & GOINGAWAY)
4605 break;
4606 inoref->if_nlink--;
4607 }
4608 }
4609 jsegdep = inoref_jseg(&jaddref->ja_ref);
4610 if (jaddref->ja_state & NEWBLOCK)
4611 move_newblock_dep(jaddref, inodedep);
4612 wake_worklist(&jaddref->ja_list);
4613 jaddref->ja_mkdir = NULL;
4614 if (jaddref->ja_state & INPROGRESS) {
4615 jaddref->ja_state &= ~INPROGRESS;
4616 WORKLIST_REMOVE(&jaddref->ja_list);
4617 jwork_insert(wkhd, jsegdep);
4618 } else {
4619 free_jsegdep(jsegdep);
4620 if (jaddref->ja_state & DEPCOMPLETE)
4621 remove_from_journal(&jaddref->ja_list);
4622 }
4623 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4624 /*
4625 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4626 * can arrange for them to be freed with the bitmap. Otherwise we
4627 * no longer need this addref attached to the inoreflst and it
4628 * will incorrectly adjust nlink if we leave it.
4629 */
4630 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4631 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4632 if_deps);
4633 jaddref->ja_state |= COMPLETE;
4634 free_jaddref(jaddref);
4635 return (needsj);
4636 }
4637 /*
4638 * Leave the head of the list for jsegdeps for fast merging.
4639 */
4640 if (LIST_FIRST(wkhd) != NULL) {
4641 jaddref->ja_state |= ONWORKLIST;
4642 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4643 } else
4644 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4645
4646 return (needsj);
4647 }
4648
4649 /*
4650 * Attempt to free a jaddref structure when some work completes. This
4651 * should only succeed once the entry is written and all dependencies have
4652 * been notified.
4653 */
4654 static void
free_jaddref(struct jaddref * jaddref)4655 free_jaddref(struct jaddref *jaddref)
4656 {
4657
4658 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4659 return;
4660 if (jaddref->ja_ref.if_jsegdep)
4661 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4662 jaddref, jaddref->ja_state);
4663 if (jaddref->ja_state & NEWBLOCK)
4664 LIST_REMOVE(jaddref, ja_bmdeps);
4665 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4666 panic("free_jaddref: Bad state %p(0x%X)",
4667 jaddref, jaddref->ja_state);
4668 if (jaddref->ja_mkdir != NULL)
4669 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4670 WORKITEM_FREE(jaddref, D_JADDREF);
4671 }
4672
4673 /*
4674 * Free a jremref structure once it has been written or discarded.
4675 */
4676 static void
free_jremref(struct jremref * jremref)4677 free_jremref(struct jremref *jremref)
4678 {
4679
4680 if (jremref->jr_ref.if_jsegdep)
4681 free_jsegdep(jremref->jr_ref.if_jsegdep);
4682 if (jremref->jr_state & INPROGRESS)
4683 panic("free_jremref: IO still pending");
4684 WORKITEM_FREE(jremref, D_JREMREF);
4685 }
4686
4687 /*
4688 * Free a jnewblk structure.
4689 */
4690 static void
free_jnewblk(struct jnewblk * jnewblk)4691 free_jnewblk(struct jnewblk *jnewblk)
4692 {
4693
4694 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4695 return;
4696 LIST_REMOVE(jnewblk, jn_deps);
4697 if (jnewblk->jn_dep != NULL)
4698 panic("free_jnewblk: Dependency still attached.");
4699 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4700 }
4701
4702 /*
4703 * Cancel a jnewblk which has been been made redundant by frag extension.
4704 */
4705 static void
cancel_jnewblk(struct jnewblk * jnewblk,struct workhead * wkhd)4706 cancel_jnewblk(struct jnewblk *jnewblk, struct workhead *wkhd)
4707 {
4708 struct jsegdep *jsegdep;
4709
4710 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4711 jsegdep = jnewblk->jn_jsegdep;
4712 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4713 panic("cancel_jnewblk: Invalid state");
4714 jnewblk->jn_jsegdep = NULL;
4715 jnewblk->jn_dep = NULL;
4716 jnewblk->jn_state |= GOINGAWAY;
4717 if (jnewblk->jn_state & INPROGRESS) {
4718 jnewblk->jn_state &= ~INPROGRESS;
4719 WORKLIST_REMOVE(&jnewblk->jn_list);
4720 jwork_insert(wkhd, jsegdep);
4721 } else {
4722 free_jsegdep(jsegdep);
4723 remove_from_journal(&jnewblk->jn_list);
4724 }
4725 wake_worklist(&jnewblk->jn_list);
4726 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4727 }
4728
4729 static void
free_jblkdep(struct jblkdep * jblkdep)4730 free_jblkdep(struct jblkdep *jblkdep)
4731 {
4732
4733 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4734 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4735 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4736 WORKITEM_FREE(jblkdep, D_JTRUNC);
4737 else
4738 panic("free_jblkdep: Unexpected type %s",
4739 TYPENAME(jblkdep->jb_list.wk_type));
4740 }
4741
4742 /*
4743 * Free a single jseg once it is no longer referenced in memory or on
4744 * disk. Reclaim journal blocks and dependencies waiting for the segment
4745 * to disappear.
4746 */
4747 static void
free_jseg(struct jseg * jseg,struct jblocks * jblocks)4748 free_jseg(struct jseg *jseg, struct jblocks *jblocks)
4749 {
4750 struct freework *freework;
4751
4752 /*
4753 * Free freework structures that were lingering to indicate freed
4754 * indirect blocks that forced journal write ordering on reallocate.
4755 */
4756 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4757 indirblk_remove(freework);
4758 if (jblocks->jb_oldestseg == jseg)
4759 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4760 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4761 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4762 KASSERT(LIST_EMPTY(&jseg->js_entries),
4763 ("free_jseg: Freed jseg has valid entries."));
4764 WORKITEM_FREE(jseg, D_JSEG);
4765 }
4766
4767 /*
4768 * Free all jsegs that meet the criteria for being reclaimed and update
4769 * oldestseg.
4770 */
4771 static void
free_jsegs(struct jblocks * jblocks)4772 free_jsegs(struct jblocks *jblocks)
4773 {
4774 struct jseg *jseg;
4775
4776 /*
4777 * Free only those jsegs which have none allocated before them to
4778 * preserve the journal space ordering.
4779 */
4780 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4781 /*
4782 * Only reclaim space when nothing depends on this journal
4783 * set and another set has written that it is no longer
4784 * valid.
4785 */
4786 if (jseg->js_refs != 0) {
4787 jblocks->jb_oldestseg = jseg;
4788 return;
4789 }
4790 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4791 break;
4792 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4793 break;
4794 /*
4795 * We can free jsegs that didn't write entries when
4796 * oldestwrseq == js_seq.
4797 */
4798 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4799 jseg->js_cnt != 0)
4800 break;
4801 free_jseg(jseg, jblocks);
4802 }
4803 /*
4804 * If we exited the loop above we still must discover the
4805 * oldest valid segment.
4806 */
4807 if (jseg)
4808 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4809 jseg = TAILQ_NEXT(jseg, js_next))
4810 if (jseg->js_refs != 0)
4811 break;
4812 jblocks->jb_oldestseg = jseg;
4813 /*
4814 * The journal has no valid records but some jsegs may still be
4815 * waiting on oldestwrseq to advance. We force a small record
4816 * out to permit these lingering records to be reclaimed.
4817 */
4818 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4819 jblocks->jb_needseg = 1;
4820 }
4821
4822 /*
4823 * Release one reference to a jseg and free it if the count reaches 0. This
4824 * should eventually reclaim journal space as well.
4825 */
4826 static void
rele_jseg(struct jseg * jseg)4827 rele_jseg(struct jseg *jseg)
4828 {
4829
4830 KASSERT(jseg->js_refs > 0,
4831 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4832 if (--jseg->js_refs != 0)
4833 return;
4834 free_jsegs(jseg->js_jblocks);
4835 }
4836
4837 /*
4838 * Release a jsegdep and decrement the jseg count.
4839 */
4840 static void
free_jsegdep(struct jsegdep * jsegdep)4841 free_jsegdep(struct jsegdep *jsegdep)
4842 {
4843
4844 if (jsegdep->jd_seg)
4845 rele_jseg(jsegdep->jd_seg);
4846 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4847 }
4848
4849 /*
4850 * Wait for a journal item to make it to disk. Initiate journal processing
4851 * if required.
4852 */
4853 static int
jwait(struct worklist * wk,int waitfor)4854 jwait(struct worklist *wk, int waitfor)
4855 {
4856
4857 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4858 /*
4859 * Blocking journal waits cause slow synchronous behavior. Record
4860 * stats on the frequency of these blocking operations.
4861 */
4862 if (waitfor == MNT_WAIT) {
4863 stat_journal_wait++;
4864 switch (wk->wk_type) {
4865 case D_JREMREF:
4866 case D_JMVREF:
4867 stat_jwait_filepage++;
4868 break;
4869 case D_JTRUNC:
4870 case D_JFREEBLK:
4871 stat_jwait_freeblks++;
4872 break;
4873 case D_JNEWBLK:
4874 stat_jwait_newblk++;
4875 break;
4876 case D_JADDREF:
4877 stat_jwait_inode++;
4878 break;
4879 default:
4880 break;
4881 }
4882 }
4883 /*
4884 * If IO has not started we process the journal. We can't mark the
4885 * worklist item as IOWAITING because we drop the lock while
4886 * processing the journal and the worklist entry may be freed after
4887 * this point. The caller may call back in and re-issue the request.
4888 */
4889 if ((wk->wk_state & INPROGRESS) == 0) {
4890 softdep_process_journal(wk->wk_mp, wk, waitfor);
4891 if (waitfor != MNT_WAIT)
4892 return (EBUSY);
4893 return (0);
4894 }
4895 if (waitfor != MNT_WAIT)
4896 return (EBUSY);
4897 wait_worklist(wk, "jwait");
4898 return (0);
4899 }
4900
4901 /*
4902 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4903 * appropriate. This is a convenience function to reduce duplicate code
4904 * for the setup and revert functions below.
4905 */
4906 static struct inodedep *
inodedep_lookup_ip(struct inode * ip)4907 inodedep_lookup_ip(struct inode *ip)
4908 {
4909 struct inodedep *inodedep;
4910
4911 KASSERT(ip->i_nlink >= ip->i_effnlink,
4912 ("inodedep_lookup_ip: bad delta"));
4913 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
4914 &inodedep);
4915 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4916 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
4917
4918 return (inodedep);
4919 }
4920
4921 /*
4922 * Called prior to creating a new inode and linking it to a directory. The
4923 * jaddref structure must already be allocated by softdep_setup_inomapdep
4924 * and it is discovered here so we can initialize the mode and update
4925 * nlinkdelta.
4926 */
4927 void
softdep_setup_create(struct inode * dp,struct inode * ip)4928 softdep_setup_create(struct inode *dp, struct inode *ip)
4929 {
4930 struct inodedep *inodedep;
4931 struct jaddref *jaddref __diagused;
4932 struct vnode *dvp;
4933
4934 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4935 ("softdep_setup_create called on non-softdep filesystem"));
4936 KASSERT(ip->i_nlink == 1,
4937 ("softdep_setup_create: Invalid link count."));
4938 dvp = ITOV(dp);
4939 ACQUIRE_LOCK(ITOUMP(dp));
4940 inodedep = inodedep_lookup_ip(ip);
4941 if (DOINGSUJ(dvp)) {
4942 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4943 inoreflst);
4944 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4945 ("softdep_setup_create: No addref structure present."));
4946 }
4947 FREE_LOCK(ITOUMP(dp));
4948 }
4949
4950 /*
4951 * Create a jaddref structure to track the addition of a DOTDOT link when
4952 * we are reparenting an inode as part of a rename. This jaddref will be
4953 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
4954 * non-journaling softdep.
4955 */
4956 void
softdep_setup_dotdot_link(struct inode * dp,struct inode * ip)4957 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip)
4958 {
4959 struct inodedep *inodedep;
4960 struct jaddref *jaddref;
4961 struct vnode *dvp;
4962
4963 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4964 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
4965 dvp = ITOV(dp);
4966 jaddref = NULL;
4967 /*
4968 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
4969 * is used as a normal link would be.
4970 */
4971 if (DOINGSUJ(dvp))
4972 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4973 dp->i_effnlink - 1, dp->i_mode);
4974 ACQUIRE_LOCK(ITOUMP(dp));
4975 inodedep = inodedep_lookup_ip(dp);
4976 if (jaddref)
4977 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4978 if_deps);
4979 FREE_LOCK(ITOUMP(dp));
4980 }
4981
4982 /*
4983 * Create a jaddref structure to track a new link to an inode. The directory
4984 * offset is not known until softdep_setup_directory_add or
4985 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
4986 * softdep.
4987 */
4988 void
softdep_setup_link(struct inode * dp,struct inode * ip)4989 softdep_setup_link(struct inode *dp, struct inode *ip)
4990 {
4991 struct inodedep *inodedep;
4992 struct jaddref *jaddref;
4993 struct vnode *dvp;
4994
4995 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4996 ("softdep_setup_link called on non-softdep filesystem"));
4997 dvp = ITOV(dp);
4998 jaddref = NULL;
4999 if (DOINGSUJ(dvp))
5000 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
5001 ip->i_mode);
5002 ACQUIRE_LOCK(ITOUMP(dp));
5003 inodedep = inodedep_lookup_ip(ip);
5004 if (jaddref)
5005 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5006 if_deps);
5007 FREE_LOCK(ITOUMP(dp));
5008 }
5009
5010 /*
5011 * Called to create the jaddref structures to track . and .. references as
5012 * well as lookup and further initialize the incomplete jaddref created
5013 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
5014 * nlinkdelta for non-journaling softdep.
5015 */
5016 void
softdep_setup_mkdir(struct inode * dp,struct inode * ip)5017 softdep_setup_mkdir(struct inode *dp, struct inode *ip)
5018 {
5019 struct inodedep *inodedep;
5020 struct jaddref *dotdotaddref;
5021 struct jaddref *dotaddref;
5022 struct jaddref *jaddref;
5023 struct vnode *dvp;
5024
5025 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5026 ("softdep_setup_mkdir called on non-softdep filesystem"));
5027 dvp = ITOV(dp);
5028 dotaddref = dotdotaddref = NULL;
5029 if (DOINGSUJ(dvp)) {
5030 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
5031 ip->i_mode);
5032 dotaddref->ja_state |= MKDIR_BODY;
5033 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5034 dp->i_effnlink - 1, dp->i_mode);
5035 dotdotaddref->ja_state |= MKDIR_PARENT;
5036 }
5037 ACQUIRE_LOCK(ITOUMP(dp));
5038 inodedep = inodedep_lookup_ip(ip);
5039 if (DOINGSUJ(dvp)) {
5040 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5041 inoreflst);
5042 KASSERT(jaddref != NULL,
5043 ("softdep_setup_mkdir: No addref structure present."));
5044 KASSERT(jaddref->ja_parent == dp->i_number,
5045 ("softdep_setup_mkdir: bad parent %ju",
5046 (uintmax_t)jaddref->ja_parent));
5047 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
5048 if_deps);
5049 }
5050 inodedep = inodedep_lookup_ip(dp);
5051 if (DOINGSUJ(dvp))
5052 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
5053 &dotdotaddref->ja_ref, if_deps);
5054 FREE_LOCK(ITOUMP(dp));
5055 }
5056
5057 /*
5058 * Called to track nlinkdelta of the inode and parent directories prior to
5059 * unlinking a directory.
5060 */
5061 void
softdep_setup_rmdir(struct inode * dp,struct inode * ip)5062 softdep_setup_rmdir(struct inode *dp, struct inode *ip)
5063 {
5064
5065 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5066 ("softdep_setup_rmdir called on non-softdep filesystem"));
5067 ACQUIRE_LOCK(ITOUMP(dp));
5068 (void) inodedep_lookup_ip(ip);
5069 (void) inodedep_lookup_ip(dp);
5070 FREE_LOCK(ITOUMP(dp));
5071 }
5072
5073 /*
5074 * Called to track nlinkdelta of the inode and parent directories prior to
5075 * unlink.
5076 */
5077 void
softdep_setup_unlink(struct inode * dp,struct inode * ip)5078 softdep_setup_unlink(struct inode *dp, struct inode *ip)
5079 {
5080
5081 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5082 ("softdep_setup_unlink called on non-softdep filesystem"));
5083 ACQUIRE_LOCK(ITOUMP(dp));
5084 (void) inodedep_lookup_ip(ip);
5085 (void) inodedep_lookup_ip(dp);
5086 FREE_LOCK(ITOUMP(dp));
5087 }
5088
5089 /*
5090 * Called to release the journal structures created by a failed non-directory
5091 * creation. Adjusts nlinkdelta for non-journaling softdep.
5092 */
5093 void
softdep_revert_create(struct inode * dp,struct inode * ip)5094 softdep_revert_create(struct inode *dp, struct inode *ip)
5095 {
5096 struct inodedep *inodedep;
5097 struct jaddref *jaddref;
5098 struct vnode *dvp;
5099
5100 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
5101 ("softdep_revert_create called on non-softdep filesystem"));
5102 dvp = ITOV(dp);
5103 ACQUIRE_LOCK(ITOUMP(dp));
5104 inodedep = inodedep_lookup_ip(ip);
5105 if (DOINGSUJ(dvp)) {
5106 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5107 inoreflst);
5108 KASSERT(jaddref->ja_parent == dp->i_number,
5109 ("softdep_revert_create: addref parent mismatch"));
5110 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5111 }
5112 FREE_LOCK(ITOUMP(dp));
5113 }
5114
5115 /*
5116 * Called to release the journal structures created by a failed link
5117 * addition. Adjusts nlinkdelta for non-journaling softdep.
5118 */
5119 void
softdep_revert_link(struct inode * dp,struct inode * ip)5120 softdep_revert_link(struct inode *dp, struct inode *ip)
5121 {
5122 struct inodedep *inodedep;
5123 struct jaddref *jaddref;
5124 struct vnode *dvp;
5125
5126 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5127 ("softdep_revert_link called on non-softdep filesystem"));
5128 dvp = ITOV(dp);
5129 ACQUIRE_LOCK(ITOUMP(dp));
5130 inodedep = inodedep_lookup_ip(ip);
5131 if (DOINGSUJ(dvp)) {
5132 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5133 inoreflst);
5134 KASSERT(jaddref->ja_parent == dp->i_number,
5135 ("softdep_revert_link: addref parent mismatch"));
5136 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5137 }
5138 FREE_LOCK(ITOUMP(dp));
5139 }
5140
5141 /*
5142 * Called to release the journal structures created by a failed mkdir
5143 * attempt. Adjusts nlinkdelta for non-journaling softdep.
5144 */
5145 void
softdep_revert_mkdir(struct inode * dp,struct inode * ip)5146 softdep_revert_mkdir(struct inode *dp, struct inode *ip)
5147 {
5148 struct inodedep *inodedep;
5149 struct jaddref *jaddref;
5150 struct jaddref *dotaddref;
5151 struct vnode *dvp;
5152
5153 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5154 ("softdep_revert_mkdir called on non-softdep filesystem"));
5155 dvp = ITOV(dp);
5156
5157 ACQUIRE_LOCK(ITOUMP(dp));
5158 inodedep = inodedep_lookup_ip(dp);
5159 if (DOINGSUJ(dvp)) {
5160 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5161 inoreflst);
5162 KASSERT(jaddref->ja_parent == ip->i_number,
5163 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
5164 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5165 }
5166 inodedep = inodedep_lookup_ip(ip);
5167 if (DOINGSUJ(dvp)) {
5168 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5169 inoreflst);
5170 KASSERT(jaddref->ja_parent == dp->i_number,
5171 ("softdep_revert_mkdir: addref parent mismatch"));
5172 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
5173 inoreflst, if_deps);
5174 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5175 KASSERT(dotaddref->ja_parent == ip->i_number,
5176 ("softdep_revert_mkdir: dot addref parent mismatch"));
5177 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
5178 }
5179 FREE_LOCK(ITOUMP(dp));
5180 }
5181
5182 /*
5183 * Called to correct nlinkdelta after a failed rmdir.
5184 */
5185 void
softdep_revert_rmdir(struct inode * dp,struct inode * ip)5186 softdep_revert_rmdir(struct inode *dp, struct inode *ip)
5187 {
5188
5189 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5190 ("softdep_revert_rmdir called on non-softdep filesystem"));
5191 ACQUIRE_LOCK(ITOUMP(dp));
5192 (void) inodedep_lookup_ip(ip);
5193 (void) inodedep_lookup_ip(dp);
5194 FREE_LOCK(ITOUMP(dp));
5195 }
5196
5197 /*
5198 * Protecting the freemaps (or bitmaps).
5199 *
5200 * To eliminate the need to execute fsck before mounting a filesystem
5201 * after a power failure, one must (conservatively) guarantee that the
5202 * on-disk copy of the bitmaps never indicate that a live inode or block is
5203 * free. So, when a block or inode is allocated, the bitmap should be
5204 * updated (on disk) before any new pointers. When a block or inode is
5205 * freed, the bitmap should not be updated until all pointers have been
5206 * reset. The latter dependency is handled by the delayed de-allocation
5207 * approach described below for block and inode de-allocation. The former
5208 * dependency is handled by calling the following procedure when a block or
5209 * inode is allocated. When an inode is allocated an "inodedep" is created
5210 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5211 * Each "inodedep" is also inserted into the hash indexing structure so
5212 * that any additional link additions can be made dependent on the inode
5213 * allocation.
5214 *
5215 * The ufs filesystem maintains a number of free block counts (e.g., per
5216 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5217 * in addition to the bitmaps. These counts are used to improve efficiency
5218 * during allocation and therefore must be consistent with the bitmaps.
5219 * There is no convenient way to guarantee post-crash consistency of these
5220 * counts with simple update ordering, for two main reasons: (1) The counts
5221 * and bitmaps for a single cylinder group block are not in the same disk
5222 * sector. If a disk write is interrupted (e.g., by power failure), one may
5223 * be written and the other not. (2) Some of the counts are located in the
5224 * superblock rather than the cylinder group block. So, we focus our soft
5225 * updates implementation on protecting the bitmaps. When mounting a
5226 * filesystem, we recompute the auxiliary counts from the bitmaps.
5227 */
5228
5229 /*
5230 * Called just after updating the cylinder group block to allocate an inode.
5231 */
5232 void
softdep_setup_inomapdep(struct buf * bp,struct inode * ip,ino_t newinum,int mode)5233 softdep_setup_inomapdep(
5234 struct buf *bp, /* buffer for cylgroup block with inode map */
5235 struct inode *ip, /* inode related to allocation */
5236 ino_t newinum, /* new inode number being allocated */
5237 int mode)
5238 {
5239 struct inodedep *inodedep;
5240 struct bmsafemap *bmsafemap;
5241 struct jaddref *jaddref;
5242 struct mount *mp;
5243 struct fs *fs;
5244
5245 mp = ITOVFS(ip);
5246 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5247 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5248 fs = VFSTOUFS(mp)->um_fs;
5249 jaddref = NULL;
5250
5251 /*
5252 * Allocate the journal reference add structure so that the bitmap
5253 * can be dependent on it.
5254 */
5255 if (MOUNTEDSUJ(mp)) {
5256 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5257 jaddref->ja_state |= NEWBLOCK;
5258 }
5259
5260 /*
5261 * Create a dependency for the newly allocated inode.
5262 * Panic if it already exists as something is seriously wrong.
5263 * Otherwise add it to the dependency list for the buffer holding
5264 * the cylinder group map from which it was allocated.
5265 *
5266 * We have to preallocate a bmsafemap entry in case it is needed
5267 * in bmsafemap_lookup since once we allocate the inodedep, we
5268 * have to finish initializing it before we can FREE_LOCK().
5269 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5270 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5271 * creating the inodedep as it can be freed during the time
5272 * that we FREE_LOCK() while allocating the inodedep. We must
5273 * call workitem_alloc() before entering the locked section as
5274 * it also acquires the lock and we must avoid trying doing so
5275 * recursively.
5276 */
5277 bmsafemap = malloc(sizeof(struct bmsafemap),
5278 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5279 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5280 ACQUIRE_LOCK(ITOUMP(ip));
5281 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5282 panic("softdep_setup_inomapdep: dependency %p for new"
5283 "inode already exists", inodedep);
5284 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5285 if (jaddref) {
5286 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5287 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5288 if_deps);
5289 } else {
5290 inodedep->id_state |= ONDEPLIST;
5291 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5292 }
5293 inodedep->id_bmsafemap = bmsafemap;
5294 inodedep->id_state &= ~DEPCOMPLETE;
5295 FREE_LOCK(ITOUMP(ip));
5296 }
5297
5298 /*
5299 * Called just after updating the cylinder group block to
5300 * allocate block or fragment.
5301 */
5302 void
softdep_setup_blkmapdep(struct buf * bp,struct mount * mp,ufs2_daddr_t newblkno,int frags,int oldfrags)5303 softdep_setup_blkmapdep(
5304 struct buf *bp, /* buffer for cylgroup block with block map */
5305 struct mount *mp, /* filesystem doing allocation */
5306 ufs2_daddr_t newblkno, /* number of newly allocated block */
5307 int frags, /* Number of fragments. */
5308 int oldfrags) /* Previous number of fragments for extend. */
5309 {
5310 struct newblk *newblk;
5311 struct bmsafemap *bmsafemap;
5312 struct jnewblk *jnewblk;
5313 struct ufsmount *ump;
5314 struct fs *fs;
5315
5316 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5317 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5318 ump = VFSTOUFS(mp);
5319 fs = ump->um_fs;
5320 jnewblk = NULL;
5321 /*
5322 * Create a dependency for the newly allocated block.
5323 * Add it to the dependency list for the buffer holding
5324 * the cylinder group map from which it was allocated.
5325 */
5326 if (MOUNTEDSUJ(mp)) {
5327 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5328 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5329 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5330 jnewblk->jn_state = ATTACHED;
5331 jnewblk->jn_blkno = newblkno;
5332 jnewblk->jn_frags = frags;
5333 jnewblk->jn_oldfrags = oldfrags;
5334 #ifdef INVARIANTS
5335 {
5336 struct cg *cgp;
5337 uint8_t *blksfree;
5338 long bno;
5339 int i;
5340
5341 cgp = (struct cg *)bp->b_data;
5342 blksfree = cg_blksfree(cgp);
5343 bno = dtogd(fs, jnewblk->jn_blkno);
5344 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5345 i++) {
5346 if (isset(blksfree, bno + i))
5347 panic("softdep_setup_blkmapdep: "
5348 "free fragment %d from %d-%d "
5349 "state 0x%X dep %p", i,
5350 jnewblk->jn_oldfrags,
5351 jnewblk->jn_frags,
5352 jnewblk->jn_state,
5353 jnewblk->jn_dep);
5354 }
5355 }
5356 #endif
5357 }
5358
5359 CTR3(KTR_SUJ,
5360 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5361 newblkno, frags, oldfrags);
5362 ACQUIRE_LOCK(ump);
5363 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5364 panic("softdep_setup_blkmapdep: found block");
5365 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5366 dtog(fs, newblkno), NULL);
5367 if (jnewblk) {
5368 jnewblk->jn_dep = (struct worklist *)newblk;
5369 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5370 } else {
5371 newblk->nb_state |= ONDEPLIST;
5372 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5373 }
5374 newblk->nb_bmsafemap = bmsafemap;
5375 newblk->nb_jnewblk = jnewblk;
5376 FREE_LOCK(ump);
5377 }
5378
5379 #define BMSAFEMAP_HASH(ump, cg) \
5380 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5381
5382 static int
bmsafemap_find(struct bmsafemap_hashhead * bmsafemaphd,int cg,struct bmsafemap ** bmsafemapp)5383 bmsafemap_find(
5384 struct bmsafemap_hashhead *bmsafemaphd,
5385 int cg,
5386 struct bmsafemap **bmsafemapp)
5387 {
5388 struct bmsafemap *bmsafemap;
5389
5390 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5391 if (bmsafemap->sm_cg == cg)
5392 break;
5393 if (bmsafemap) {
5394 *bmsafemapp = bmsafemap;
5395 return (1);
5396 }
5397 *bmsafemapp = NULL;
5398
5399 return (0);
5400 }
5401
5402 /*
5403 * Find the bmsafemap associated with a cylinder group buffer.
5404 * If none exists, create one. The buffer must be locked when
5405 * this routine is called and this routine must be called with
5406 * the softdep lock held. To avoid giving up the lock while
5407 * allocating a new bmsafemap, a preallocated bmsafemap may be
5408 * provided. If it is provided but not needed, it is freed.
5409 */
5410 static struct bmsafemap *
bmsafemap_lookup(struct mount * mp,struct buf * bp,int cg,struct bmsafemap * newbmsafemap)5411 bmsafemap_lookup(struct mount *mp,
5412 struct buf *bp,
5413 int cg,
5414 struct bmsafemap *newbmsafemap)
5415 {
5416 struct bmsafemap_hashhead *bmsafemaphd;
5417 struct bmsafemap *bmsafemap, *collision;
5418 struct worklist *wk;
5419 struct ufsmount *ump;
5420
5421 ump = VFSTOUFS(mp);
5422 LOCK_OWNED(ump);
5423 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5424 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5425 if (wk->wk_type == D_BMSAFEMAP) {
5426 if (newbmsafemap)
5427 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5428 return (WK_BMSAFEMAP(wk));
5429 }
5430 }
5431 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5432 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5433 if (newbmsafemap)
5434 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5435 return (bmsafemap);
5436 }
5437 if (newbmsafemap) {
5438 bmsafemap = newbmsafemap;
5439 } else {
5440 FREE_LOCK(ump);
5441 bmsafemap = malloc(sizeof(struct bmsafemap),
5442 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5443 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5444 ACQUIRE_LOCK(ump);
5445 }
5446 bmsafemap->sm_buf = bp;
5447 LIST_INIT(&bmsafemap->sm_inodedephd);
5448 LIST_INIT(&bmsafemap->sm_inodedepwr);
5449 LIST_INIT(&bmsafemap->sm_newblkhd);
5450 LIST_INIT(&bmsafemap->sm_newblkwr);
5451 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5452 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5453 LIST_INIT(&bmsafemap->sm_freehd);
5454 LIST_INIT(&bmsafemap->sm_freewr);
5455 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5456 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5457 return (collision);
5458 }
5459 bmsafemap->sm_cg = cg;
5460 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5461 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5462 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5463 return (bmsafemap);
5464 }
5465
5466 /*
5467 * Direct block allocation dependencies.
5468 *
5469 * When a new block is allocated, the corresponding disk locations must be
5470 * initialized (with zeros or new data) before the on-disk inode points to
5471 * them. Also, the freemap from which the block was allocated must be
5472 * updated (on disk) before the inode's pointer. These two dependencies are
5473 * independent of each other and are needed for all file blocks and indirect
5474 * blocks that are pointed to directly by the inode. Just before the
5475 * "in-core" version of the inode is updated with a newly allocated block
5476 * number, a procedure (below) is called to setup allocation dependency
5477 * structures. These structures are removed when the corresponding
5478 * dependencies are satisfied or when the block allocation becomes obsolete
5479 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5480 * fragment that gets upgraded). All of these cases are handled in
5481 * procedures described later.
5482 *
5483 * When a file extension causes a fragment to be upgraded, either to a larger
5484 * fragment or to a full block, the on-disk location may change (if the
5485 * previous fragment could not simply be extended). In this case, the old
5486 * fragment must be de-allocated, but not until after the inode's pointer has
5487 * been updated. In most cases, this is handled by later procedures, which
5488 * will construct a "freefrag" structure to be added to the workitem queue
5489 * when the inode update is complete (or obsolete). The main exception to
5490 * this is when an allocation occurs while a pending allocation dependency
5491 * (for the same block pointer) remains. This case is handled in the main
5492 * allocation dependency setup procedure by immediately freeing the
5493 * unreferenced fragments.
5494 */
5495 void
softdep_setup_allocdirect(struct inode * ip,ufs_lbn_t off,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,long newsize,long oldsize,struct buf * bp)5496 softdep_setup_allocdirect(
5497 struct inode *ip, /* inode to which block is being added */
5498 ufs_lbn_t off, /* block pointer within inode */
5499 ufs2_daddr_t newblkno, /* disk block number being added */
5500 ufs2_daddr_t oldblkno, /* previous block number, 0 unless frag */
5501 long newsize, /* size of new block */
5502 long oldsize, /* size of new block */
5503 struct buf *bp) /* bp for allocated block */
5504 {
5505 struct allocdirect *adp, *oldadp;
5506 struct allocdirectlst *adphead;
5507 struct freefrag *freefrag;
5508 struct inodedep *inodedep;
5509 struct pagedep *pagedep;
5510 struct jnewblk *jnewblk;
5511 struct newblk *newblk;
5512 struct mount *mp;
5513 ufs_lbn_t lbn;
5514
5515 lbn = bp->b_lblkno;
5516 mp = ITOVFS(ip);
5517 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5518 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5519 if (oldblkno && oldblkno != newblkno)
5520 /*
5521 * The usual case is that a smaller fragment that
5522 * was just allocated has been replaced with a bigger
5523 * fragment or a full-size block. If it is marked as
5524 * B_DELWRI, the current contents have not been written
5525 * to disk. It is possible that the block was written
5526 * earlier, but very uncommon. If the block has never
5527 * been written, there is no need to send a BIO_DELETE
5528 * for it when it is freed. The gain from avoiding the
5529 * TRIMs for the common case of unwritten blocks far
5530 * exceeds the cost of the write amplification for the
5531 * uncommon case of failing to send a TRIM for a block
5532 * that had been written.
5533 */
5534 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5535 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5536 else
5537 freefrag = NULL;
5538
5539 CTR6(KTR_SUJ,
5540 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5541 "off %jd newsize %ld oldsize %d",
5542 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5543 ACQUIRE_LOCK(ITOUMP(ip));
5544 if (off >= UFS_NDADDR) {
5545 if (lbn > 0)
5546 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5547 lbn, off);
5548 /* allocating an indirect block */
5549 if (oldblkno != 0)
5550 panic("softdep_setup_allocdirect: non-zero indir");
5551 } else {
5552 if (off != lbn)
5553 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5554 lbn, off);
5555 /*
5556 * Allocating a direct block.
5557 *
5558 * If we are allocating a directory block, then we must
5559 * allocate an associated pagedep to track additions and
5560 * deletions.
5561 */
5562 if ((ip->i_mode & IFMT) == IFDIR)
5563 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5564 &pagedep);
5565 }
5566 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5567 panic("softdep_setup_allocdirect: lost block");
5568 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5569 ("softdep_setup_allocdirect: newblk already initialized"));
5570 /*
5571 * Convert the newblk to an allocdirect.
5572 */
5573 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5574 adp = (struct allocdirect *)newblk;
5575 newblk->nb_freefrag = freefrag;
5576 adp->ad_offset = off;
5577 adp->ad_oldblkno = oldblkno;
5578 adp->ad_newsize = newsize;
5579 adp->ad_oldsize = oldsize;
5580
5581 /*
5582 * Finish initializing the journal.
5583 */
5584 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5585 jnewblk->jn_ino = ip->i_number;
5586 jnewblk->jn_lbn = lbn;
5587 add_to_journal(&jnewblk->jn_list);
5588 }
5589 if (freefrag && freefrag->ff_jdep != NULL &&
5590 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5591 add_to_journal(freefrag->ff_jdep);
5592 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5593 adp->ad_inodedep = inodedep;
5594
5595 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5596 /*
5597 * The list of allocdirects must be kept in sorted and ascending
5598 * order so that the rollback routines can quickly determine the
5599 * first uncommitted block (the size of the file stored on disk
5600 * ends at the end of the lowest committed fragment, or if there
5601 * are no fragments, at the end of the highest committed block).
5602 * Since files generally grow, the typical case is that the new
5603 * block is to be added at the end of the list. We speed this
5604 * special case by checking against the last allocdirect in the
5605 * list before laboriously traversing the list looking for the
5606 * insertion point.
5607 */
5608 adphead = &inodedep->id_newinoupdt;
5609 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5610 if (oldadp == NULL || oldadp->ad_offset <= off) {
5611 /* insert at end of list */
5612 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5613 if (oldadp != NULL && oldadp->ad_offset == off)
5614 allocdirect_merge(adphead, adp, oldadp);
5615 FREE_LOCK(ITOUMP(ip));
5616 return;
5617 }
5618 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5619 if (oldadp->ad_offset >= off)
5620 break;
5621 }
5622 if (oldadp == NULL)
5623 panic("softdep_setup_allocdirect: lost entry");
5624 /* insert in middle of list */
5625 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5626 if (oldadp->ad_offset == off)
5627 allocdirect_merge(adphead, adp, oldadp);
5628
5629 FREE_LOCK(ITOUMP(ip));
5630 }
5631
5632 /*
5633 * Merge a newer and older journal record to be stored either in a
5634 * newblock or freefrag. This handles aggregating journal records for
5635 * fragment allocation into a second record as well as replacing a
5636 * journal free with an aborted journal allocation. A segment for the
5637 * oldest record will be placed on wkhd if it has been written. If not
5638 * the segment for the newer record will suffice.
5639 */
5640 static struct worklist *
jnewblk_merge(struct worklist * new,struct worklist * old,struct workhead * wkhd)5641 jnewblk_merge(struct worklist *new,
5642 struct worklist *old,
5643 struct workhead *wkhd)
5644 {
5645 struct jnewblk *njnewblk;
5646 struct jnewblk *jnewblk;
5647
5648 /* Handle NULLs to simplify callers. */
5649 if (new == NULL)
5650 return (old);
5651 if (old == NULL)
5652 return (new);
5653 /* Replace a jfreefrag with a jnewblk. */
5654 if (new->wk_type == D_JFREEFRAG) {
5655 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5656 panic("jnewblk_merge: blkno mismatch: %p, %p",
5657 old, new);
5658 cancel_jfreefrag(WK_JFREEFRAG(new));
5659 return (old);
5660 }
5661 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5662 panic("jnewblk_merge: Bad type: old %d new %d\n",
5663 old->wk_type, new->wk_type);
5664 /*
5665 * Handle merging of two jnewblk records that describe
5666 * different sets of fragments in the same block.
5667 */
5668 jnewblk = WK_JNEWBLK(old);
5669 njnewblk = WK_JNEWBLK(new);
5670 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5671 panic("jnewblk_merge: Merging disparate blocks.");
5672 /*
5673 * The record may be rolled back in the cg.
5674 */
5675 if (jnewblk->jn_state & UNDONE) {
5676 jnewblk->jn_state &= ~UNDONE;
5677 njnewblk->jn_state |= UNDONE;
5678 njnewblk->jn_state &= ~ATTACHED;
5679 }
5680 /*
5681 * We modify the newer addref and free the older so that if neither
5682 * has been written the most up-to-date copy will be on disk. If
5683 * both have been written but rolled back we only temporarily need
5684 * one of them to fix the bits when the cg write completes.
5685 */
5686 jnewblk->jn_state |= ATTACHED | COMPLETE;
5687 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5688 cancel_jnewblk(jnewblk, wkhd);
5689 WORKLIST_REMOVE(&jnewblk->jn_list);
5690 free_jnewblk(jnewblk);
5691 return (new);
5692 }
5693
5694 /*
5695 * Replace an old allocdirect dependency with a newer one.
5696 */
5697 static void
allocdirect_merge(struct allocdirectlst * adphead,struct allocdirect * newadp,struct allocdirect * oldadp)5698 allocdirect_merge(
5699 struct allocdirectlst *adphead, /* head of list holding allocdirects */
5700 struct allocdirect *newadp, /* allocdirect being added */
5701 struct allocdirect *oldadp) /* existing allocdirect being checked */
5702 {
5703 struct worklist *wk;
5704 struct freefrag *freefrag;
5705
5706 freefrag = NULL;
5707 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5708 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5709 newadp->ad_oldsize != oldadp->ad_newsize ||
5710 newadp->ad_offset >= UFS_NDADDR)
5711 panic("%s %jd != new %jd || old size %ld != new %ld",
5712 "allocdirect_merge: old blkno",
5713 (intmax_t)newadp->ad_oldblkno,
5714 (intmax_t)oldadp->ad_newblkno,
5715 newadp->ad_oldsize, oldadp->ad_newsize);
5716 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5717 newadp->ad_oldsize = oldadp->ad_oldsize;
5718 /*
5719 * If the old dependency had a fragment to free or had never
5720 * previously had a block allocated, then the new dependency
5721 * can immediately post its freefrag and adopt the old freefrag.
5722 * This action is done by swapping the freefrag dependencies.
5723 * The new dependency gains the old one's freefrag, and the
5724 * old one gets the new one and then immediately puts it on
5725 * the worklist when it is freed by free_newblk. It is
5726 * not possible to do this swap when the old dependency had a
5727 * non-zero size but no previous fragment to free. This condition
5728 * arises when the new block is an extension of the old block.
5729 * Here, the first part of the fragment allocated to the new
5730 * dependency is part of the block currently claimed on disk by
5731 * the old dependency, so cannot legitimately be freed until the
5732 * conditions for the new dependency are fulfilled.
5733 */
5734 freefrag = newadp->ad_freefrag;
5735 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5736 newadp->ad_freefrag = oldadp->ad_freefrag;
5737 oldadp->ad_freefrag = freefrag;
5738 }
5739 /*
5740 * If we are tracking a new directory-block allocation,
5741 * move it from the old allocdirect to the new allocdirect.
5742 */
5743 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5744 WORKLIST_REMOVE(wk);
5745 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5746 panic("allocdirect_merge: extra newdirblk");
5747 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5748 }
5749 TAILQ_REMOVE(adphead, oldadp, ad_next);
5750 /*
5751 * We need to move any journal dependencies over to the freefrag
5752 * that releases this block if it exists. Otherwise we are
5753 * extending an existing block and we'll wait until that is
5754 * complete to release the journal space and extend the
5755 * new journal to cover this old space as well.
5756 */
5757 if (freefrag == NULL) {
5758 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5759 panic("allocdirect_merge: %jd != %jd",
5760 oldadp->ad_newblkno, newadp->ad_newblkno);
5761 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5762 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5763 &oldadp->ad_block.nb_jnewblk->jn_list,
5764 &newadp->ad_block.nb_jwork);
5765 oldadp->ad_block.nb_jnewblk = NULL;
5766 cancel_newblk(&oldadp->ad_block, NULL,
5767 &newadp->ad_block.nb_jwork);
5768 } else {
5769 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5770 &freefrag->ff_list, &freefrag->ff_jwork);
5771 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5772 &freefrag->ff_jwork);
5773 }
5774 free_newblk(&oldadp->ad_block);
5775 }
5776
5777 /*
5778 * Allocate a jfreefrag structure to journal a single block free.
5779 */
5780 static struct jfreefrag *
newjfreefrag(struct freefrag * freefrag,struct inode * ip,ufs2_daddr_t blkno,long size,ufs_lbn_t lbn)5781 newjfreefrag(struct freefrag *freefrag,
5782 struct inode *ip,
5783 ufs2_daddr_t blkno,
5784 long size,
5785 ufs_lbn_t lbn)
5786 {
5787 struct jfreefrag *jfreefrag;
5788 struct fs *fs;
5789
5790 fs = ITOFS(ip);
5791 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5792 M_SOFTDEP_FLAGS);
5793 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5794 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5795 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5796 jfreefrag->fr_ino = ip->i_number;
5797 jfreefrag->fr_lbn = lbn;
5798 jfreefrag->fr_blkno = blkno;
5799 jfreefrag->fr_frags = numfrags(fs, size);
5800 jfreefrag->fr_freefrag = freefrag;
5801
5802 return (jfreefrag);
5803 }
5804
5805 /*
5806 * Allocate a new freefrag structure.
5807 */
5808 static struct freefrag *
newfreefrag(struct inode * ip,ufs2_daddr_t blkno,long size,ufs_lbn_t lbn,uint64_t key)5809 newfreefrag(struct inode *ip,
5810 ufs2_daddr_t blkno,
5811 long size,
5812 ufs_lbn_t lbn,
5813 uint64_t key)
5814 {
5815 struct freefrag *freefrag;
5816 struct ufsmount *ump;
5817 struct fs *fs;
5818
5819 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5820 ip->i_number, blkno, size, lbn);
5821 ump = ITOUMP(ip);
5822 fs = ump->um_fs;
5823 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5824 panic("newfreefrag: frag size");
5825 freefrag = malloc(sizeof(struct freefrag),
5826 M_FREEFRAG, M_SOFTDEP_FLAGS);
5827 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5828 freefrag->ff_state = ATTACHED;
5829 LIST_INIT(&freefrag->ff_jwork);
5830 freefrag->ff_inum = ip->i_number;
5831 freefrag->ff_vtype = ITOV(ip)->v_type;
5832 freefrag->ff_blkno = blkno;
5833 freefrag->ff_fragsize = size;
5834 freefrag->ff_key = key;
5835
5836 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5837 freefrag->ff_jdep = (struct worklist *)
5838 newjfreefrag(freefrag, ip, blkno, size, lbn);
5839 } else {
5840 freefrag->ff_state |= DEPCOMPLETE;
5841 freefrag->ff_jdep = NULL;
5842 }
5843
5844 return (freefrag);
5845 }
5846
5847 /*
5848 * This workitem de-allocates fragments that were replaced during
5849 * file block allocation.
5850 */
5851 static void
handle_workitem_freefrag(struct freefrag * freefrag)5852 handle_workitem_freefrag(struct freefrag *freefrag)
5853 {
5854 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5855 struct workhead wkhd;
5856
5857 CTR3(KTR_SUJ,
5858 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5859 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5860 /*
5861 * It would be illegal to add new completion items to the
5862 * freefrag after it was schedule to be done so it must be
5863 * safe to modify the list head here.
5864 */
5865 LIST_INIT(&wkhd);
5866 ACQUIRE_LOCK(ump);
5867 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5868 /*
5869 * If the journal has not been written we must cancel it here.
5870 */
5871 if (freefrag->ff_jdep) {
5872 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5873 panic("handle_workitem_freefrag: Unexpected type %d\n",
5874 freefrag->ff_jdep->wk_type);
5875 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5876 }
5877 FREE_LOCK(ump);
5878 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5879 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
5880 &wkhd, freefrag->ff_key);
5881 ACQUIRE_LOCK(ump);
5882 WORKITEM_FREE(freefrag, D_FREEFRAG);
5883 FREE_LOCK(ump);
5884 }
5885
5886 /*
5887 * Set up a dependency structure for an external attributes data block.
5888 * This routine follows much of the structure of softdep_setup_allocdirect.
5889 * See the description of softdep_setup_allocdirect above for details.
5890 */
5891 void
softdep_setup_allocext(struct inode * ip,ufs_lbn_t off,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,long newsize,long oldsize,struct buf * bp)5892 softdep_setup_allocext(
5893 struct inode *ip,
5894 ufs_lbn_t off,
5895 ufs2_daddr_t newblkno,
5896 ufs2_daddr_t oldblkno,
5897 long newsize,
5898 long oldsize,
5899 struct buf *bp)
5900 {
5901 struct allocdirect *adp, *oldadp;
5902 struct allocdirectlst *adphead;
5903 struct freefrag *freefrag;
5904 struct inodedep *inodedep;
5905 struct jnewblk *jnewblk;
5906 struct newblk *newblk;
5907 struct mount *mp;
5908 struct ufsmount *ump;
5909 ufs_lbn_t lbn;
5910
5911 mp = ITOVFS(ip);
5912 ump = VFSTOUFS(mp);
5913 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5914 ("softdep_setup_allocext called on non-softdep filesystem"));
5915 KASSERT(off < UFS_NXADDR,
5916 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
5917
5918 lbn = bp->b_lblkno;
5919 if (oldblkno && oldblkno != newblkno)
5920 /*
5921 * The usual case is that a smaller fragment that
5922 * was just allocated has been replaced with a bigger
5923 * fragment or a full-size block. If it is marked as
5924 * B_DELWRI, the current contents have not been written
5925 * to disk. It is possible that the block was written
5926 * earlier, but very uncommon. If the block has never
5927 * been written, there is no need to send a BIO_DELETE
5928 * for it when it is freed. The gain from avoiding the
5929 * TRIMs for the common case of unwritten blocks far
5930 * exceeds the cost of the write amplification for the
5931 * uncommon case of failing to send a TRIM for a block
5932 * that had been written.
5933 */
5934 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5935 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5936 else
5937 freefrag = NULL;
5938
5939 ACQUIRE_LOCK(ump);
5940 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5941 panic("softdep_setup_allocext: lost block");
5942 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5943 ("softdep_setup_allocext: newblk already initialized"));
5944 /*
5945 * Convert the newblk to an allocdirect.
5946 */
5947 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5948 adp = (struct allocdirect *)newblk;
5949 newblk->nb_freefrag = freefrag;
5950 adp->ad_offset = off;
5951 adp->ad_oldblkno = oldblkno;
5952 adp->ad_newsize = newsize;
5953 adp->ad_oldsize = oldsize;
5954 adp->ad_state |= EXTDATA;
5955
5956 /*
5957 * Finish initializing the journal.
5958 */
5959 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5960 jnewblk->jn_ino = ip->i_number;
5961 jnewblk->jn_lbn = lbn;
5962 add_to_journal(&jnewblk->jn_list);
5963 }
5964 if (freefrag && freefrag->ff_jdep != NULL &&
5965 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5966 add_to_journal(freefrag->ff_jdep);
5967 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5968 adp->ad_inodedep = inodedep;
5969
5970 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5971 /*
5972 * The list of allocdirects must be kept in sorted and ascending
5973 * order so that the rollback routines can quickly determine the
5974 * first uncommitted block (the size of the file stored on disk
5975 * ends at the end of the lowest committed fragment, or if there
5976 * are no fragments, at the end of the highest committed block).
5977 * Since files generally grow, the typical case is that the new
5978 * block is to be added at the end of the list. We speed this
5979 * special case by checking against the last allocdirect in the
5980 * list before laboriously traversing the list looking for the
5981 * insertion point.
5982 */
5983 adphead = &inodedep->id_newextupdt;
5984 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5985 if (oldadp == NULL || oldadp->ad_offset <= off) {
5986 /* insert at end of list */
5987 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5988 if (oldadp != NULL && oldadp->ad_offset == off)
5989 allocdirect_merge(adphead, adp, oldadp);
5990 FREE_LOCK(ump);
5991 return;
5992 }
5993 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5994 if (oldadp->ad_offset >= off)
5995 break;
5996 }
5997 if (oldadp == NULL)
5998 panic("softdep_setup_allocext: lost entry");
5999 /* insert in middle of list */
6000 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
6001 if (oldadp->ad_offset == off)
6002 allocdirect_merge(adphead, adp, oldadp);
6003 FREE_LOCK(ump);
6004 }
6005
6006 /*
6007 * Indirect block allocation dependencies.
6008 *
6009 * The same dependencies that exist for a direct block also exist when
6010 * a new block is allocated and pointed to by an entry in a block of
6011 * indirect pointers. The undo/redo states described above are also
6012 * used here. Because an indirect block contains many pointers that
6013 * may have dependencies, a second copy of the entire in-memory indirect
6014 * block is kept. The buffer cache copy is always completely up-to-date.
6015 * The second copy, which is used only as a source for disk writes,
6016 * contains only the safe pointers (i.e., those that have no remaining
6017 * update dependencies). The second copy is freed when all pointers
6018 * are safe. The cache is not allowed to replace indirect blocks with
6019 * pending update dependencies. If a buffer containing an indirect
6020 * block with dependencies is written, these routines will mark it
6021 * dirty again. It can only be successfully written once all the
6022 * dependencies are removed. The ffs_fsync routine in conjunction with
6023 * softdep_sync_metadata work together to get all the dependencies
6024 * removed so that a file can be successfully written to disk. Three
6025 * procedures are used when setting up indirect block pointer
6026 * dependencies. The division is necessary because of the organization
6027 * of the "balloc" routine and because of the distinction between file
6028 * pages and file metadata blocks.
6029 */
6030
6031 /*
6032 * Allocate a new allocindir structure.
6033 */
6034 static struct allocindir *
newallocindir(struct inode * ip,int ptrno,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,ufs_lbn_t lbn)6035 newallocindir(
6036 struct inode *ip, /* inode for file being extended */
6037 int ptrno, /* offset of pointer in indirect block */
6038 ufs2_daddr_t newblkno, /* disk block number being added */
6039 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */
6040 ufs_lbn_t lbn)
6041 {
6042 struct newblk *newblk;
6043 struct allocindir *aip;
6044 struct freefrag *freefrag;
6045 struct jnewblk *jnewblk;
6046
6047 if (oldblkno)
6048 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
6049 SINGLETON_KEY);
6050 else
6051 freefrag = NULL;
6052 ACQUIRE_LOCK(ITOUMP(ip));
6053 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
6054 panic("new_allocindir: lost block");
6055 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6056 ("newallocindir: newblk already initialized"));
6057 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
6058 newblk->nb_freefrag = freefrag;
6059 aip = (struct allocindir *)newblk;
6060 aip->ai_offset = ptrno;
6061 aip->ai_oldblkno = oldblkno;
6062 aip->ai_lbn = lbn;
6063 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6064 jnewblk->jn_ino = ip->i_number;
6065 jnewblk->jn_lbn = lbn;
6066 add_to_journal(&jnewblk->jn_list);
6067 }
6068 if (freefrag && freefrag->ff_jdep != NULL &&
6069 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6070 add_to_journal(freefrag->ff_jdep);
6071 return (aip);
6072 }
6073
6074 /*
6075 * Called just before setting an indirect block pointer
6076 * to a newly allocated file page.
6077 */
6078 void
softdep_setup_allocindir_page(struct inode * ip,ufs_lbn_t lbn,struct buf * bp,int ptrno,ufs2_daddr_t newblkno,ufs2_daddr_t oldblkno,struct buf * nbp)6079 softdep_setup_allocindir_page(
6080 struct inode *ip, /* inode for file being extended */
6081 ufs_lbn_t lbn, /* allocated block number within file */
6082 struct buf *bp, /* buffer with indirect blk referencing page */
6083 int ptrno, /* offset of pointer in indirect block */
6084 ufs2_daddr_t newblkno, /* disk block number being added */
6085 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */
6086 struct buf *nbp) /* buffer holding allocated page */
6087 {
6088 struct inodedep *inodedep;
6089 struct freefrag *freefrag;
6090 struct allocindir *aip;
6091 struct pagedep *pagedep;
6092 struct mount *mp;
6093 struct ufsmount *ump;
6094
6095 mp = ITOVFS(ip);
6096 ump = VFSTOUFS(mp);
6097 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6098 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
6099 KASSERT(lbn == nbp->b_lblkno,
6100 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
6101 lbn, bp->b_lblkno));
6102 CTR4(KTR_SUJ,
6103 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
6104 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
6105 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
6106 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
6107 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6108 /*
6109 * If we are allocating a directory page, then we must
6110 * allocate an associated pagedep to track additions and
6111 * deletions.
6112 */
6113 if ((ip->i_mode & IFMT) == IFDIR)
6114 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
6115 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6116 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
6117 FREE_LOCK(ump);
6118 if (freefrag)
6119 handle_workitem_freefrag(freefrag);
6120 }
6121
6122 /*
6123 * Called just before setting an indirect block pointer to a
6124 * newly allocated indirect block.
6125 */
6126 void
softdep_setup_allocindir_meta(struct buf * nbp,struct inode * ip,struct buf * bp,int ptrno,ufs2_daddr_t newblkno)6127 softdep_setup_allocindir_meta(
6128 struct buf *nbp, /* newly allocated indirect block */
6129 struct inode *ip, /* inode for file being extended */
6130 struct buf *bp, /* indirect block referencing allocated block */
6131 int ptrno, /* offset of pointer in indirect block */
6132 ufs2_daddr_t newblkno) /* disk block number being added */
6133 {
6134 struct inodedep *inodedep;
6135 struct allocindir *aip;
6136 struct ufsmount *ump;
6137 ufs_lbn_t lbn;
6138
6139 ump = ITOUMP(ip);
6140 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6141 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
6142 CTR3(KTR_SUJ,
6143 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
6144 ip->i_number, newblkno, ptrno);
6145 lbn = nbp->b_lblkno;
6146 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
6147 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
6148 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
6149 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6150 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
6151 panic("softdep_setup_allocindir_meta: Block already existed");
6152 FREE_LOCK(ump);
6153 }
6154
6155 static void
indirdep_complete(struct indirdep * indirdep)6156 indirdep_complete(struct indirdep *indirdep)
6157 {
6158 struct allocindir *aip;
6159
6160 LIST_REMOVE(indirdep, ir_next);
6161 indirdep->ir_state |= DEPCOMPLETE;
6162
6163 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
6164 LIST_REMOVE(aip, ai_next);
6165 free_newblk(&aip->ai_block);
6166 }
6167 /*
6168 * If this indirdep is not attached to a buf it was simply waiting
6169 * on completion to clear completehd. free_indirdep() asserts
6170 * that nothing is dangling.
6171 */
6172 if ((indirdep->ir_state & ONWORKLIST) == 0)
6173 free_indirdep(indirdep);
6174 }
6175
6176 static struct indirdep *
indirdep_lookup(struct mount * mp,struct inode * ip,struct buf * bp)6177 indirdep_lookup(struct mount *mp,
6178 struct inode *ip,
6179 struct buf *bp)
6180 {
6181 struct indirdep *indirdep, *newindirdep;
6182 struct newblk *newblk;
6183 struct ufsmount *ump;
6184 struct worklist *wk;
6185 struct fs *fs;
6186 ufs2_daddr_t blkno;
6187
6188 ump = VFSTOUFS(mp);
6189 LOCK_OWNED(ump);
6190 indirdep = NULL;
6191 newindirdep = NULL;
6192 fs = ump->um_fs;
6193 for (;;) {
6194 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6195 if (wk->wk_type != D_INDIRDEP)
6196 continue;
6197 indirdep = WK_INDIRDEP(wk);
6198 break;
6199 }
6200 /* Found on the buffer worklist, no new structure to free. */
6201 if (indirdep != NULL && newindirdep == NULL)
6202 return (indirdep);
6203 if (indirdep != NULL && newindirdep != NULL)
6204 panic("indirdep_lookup: simultaneous create");
6205 /* None found on the buffer and a new structure is ready. */
6206 if (indirdep == NULL && newindirdep != NULL)
6207 break;
6208 /* None found and no new structure available. */
6209 FREE_LOCK(ump);
6210 newindirdep = malloc(sizeof(struct indirdep),
6211 M_INDIRDEP, M_SOFTDEP_FLAGS);
6212 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6213 newindirdep->ir_state = ATTACHED;
6214 if (I_IS_UFS1(ip))
6215 newindirdep->ir_state |= UFS1FMT;
6216 TAILQ_INIT(&newindirdep->ir_trunc);
6217 newindirdep->ir_saveddata = NULL;
6218 LIST_INIT(&newindirdep->ir_deplisthd);
6219 LIST_INIT(&newindirdep->ir_donehd);
6220 LIST_INIT(&newindirdep->ir_writehd);
6221 LIST_INIT(&newindirdep->ir_completehd);
6222 if (bp->b_blkno == bp->b_lblkno) {
6223 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6224 NULL, NULL);
6225 bp->b_blkno = blkno;
6226 }
6227 newindirdep->ir_freeblks = NULL;
6228 newindirdep->ir_savebp =
6229 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6230 newindirdep->ir_bp = bp;
6231 BUF_KERNPROC(newindirdep->ir_savebp);
6232 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6233 ACQUIRE_LOCK(ump);
6234 }
6235 indirdep = newindirdep;
6236 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6237 /*
6238 * If the block is not yet allocated we don't set DEPCOMPLETE so
6239 * that we don't free dependencies until the pointers are valid.
6240 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6241 * than using the hash.
6242 */
6243 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6244 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6245 else
6246 indirdep->ir_state |= DEPCOMPLETE;
6247 return (indirdep);
6248 }
6249
6250 /*
6251 * Called to finish the allocation of the "aip" allocated
6252 * by one of the two routines above.
6253 */
6254 static struct freefrag *
setup_allocindir_phase2(struct buf * bp,struct inode * ip,struct inodedep * inodedep,struct allocindir * aip,ufs_lbn_t lbn)6255 setup_allocindir_phase2(
6256 struct buf *bp, /* in-memory copy of the indirect block */
6257 struct inode *ip, /* inode for file being extended */
6258 struct inodedep *inodedep, /* Inodedep for ip */
6259 struct allocindir *aip, /* allocindir allocated by the above routines */
6260 ufs_lbn_t lbn) /* Logical block number for this block. */
6261 {
6262 struct fs *fs __diagused;
6263 struct indirdep *indirdep;
6264 struct allocindir *oldaip;
6265 struct freefrag *freefrag;
6266 struct mount *mp;
6267 struct ufsmount *ump;
6268
6269 mp = ITOVFS(ip);
6270 ump = VFSTOUFS(mp);
6271 LOCK_OWNED(ump);
6272 fs = ump->um_fs;
6273 if (bp->b_lblkno >= 0)
6274 panic("setup_allocindir_phase2: not indir blk");
6275 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6276 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6277 indirdep = indirdep_lookup(mp, ip, bp);
6278 KASSERT(indirdep->ir_savebp != NULL,
6279 ("setup_allocindir_phase2 NULL ir_savebp"));
6280 aip->ai_indirdep = indirdep;
6281 /*
6282 * Check for an unwritten dependency for this indirect offset. If
6283 * there is, merge the old dependency into the new one. This happens
6284 * as a result of reallocblk only.
6285 */
6286 freefrag = NULL;
6287 if (aip->ai_oldblkno != 0) {
6288 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6289 if (oldaip->ai_offset == aip->ai_offset) {
6290 freefrag = allocindir_merge(aip, oldaip);
6291 goto done;
6292 }
6293 }
6294 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6295 if (oldaip->ai_offset == aip->ai_offset) {
6296 freefrag = allocindir_merge(aip, oldaip);
6297 goto done;
6298 }
6299 }
6300 }
6301 done:
6302 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6303 return (freefrag);
6304 }
6305
6306 /*
6307 * Merge two allocindirs which refer to the same block. Move newblock
6308 * dependencies and setup the freefrags appropriately.
6309 */
6310 static struct freefrag *
allocindir_merge(struct allocindir * aip,struct allocindir * oldaip)6311 allocindir_merge(
6312 struct allocindir *aip,
6313 struct allocindir *oldaip)
6314 {
6315 struct freefrag *freefrag;
6316 struct worklist *wk;
6317
6318 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6319 panic("allocindir_merge: blkno");
6320 aip->ai_oldblkno = oldaip->ai_oldblkno;
6321 freefrag = aip->ai_freefrag;
6322 aip->ai_freefrag = oldaip->ai_freefrag;
6323 oldaip->ai_freefrag = NULL;
6324 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6325 /*
6326 * If we are tracking a new directory-block allocation,
6327 * move it from the old allocindir to the new allocindir.
6328 */
6329 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6330 WORKLIST_REMOVE(wk);
6331 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6332 panic("allocindir_merge: extra newdirblk");
6333 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6334 }
6335 /*
6336 * We can skip journaling for this freefrag and just complete
6337 * any pending journal work for the allocindir that is being
6338 * removed after the freefrag completes.
6339 */
6340 if (freefrag->ff_jdep)
6341 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6342 LIST_REMOVE(oldaip, ai_next);
6343 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6344 &freefrag->ff_list, &freefrag->ff_jwork);
6345 free_newblk(&oldaip->ai_block);
6346
6347 return (freefrag);
6348 }
6349
6350 static inline void
setup_freedirect(struct freeblks * freeblks,struct inode * ip,int i,int needj)6351 setup_freedirect(
6352 struct freeblks *freeblks,
6353 struct inode *ip,
6354 int i,
6355 int needj)
6356 {
6357 struct ufsmount *ump;
6358 ufs2_daddr_t blkno;
6359 int frags;
6360
6361 blkno = DIP(ip, i_db[i]);
6362 if (blkno == 0)
6363 return;
6364 DIP_SET(ip, i_db[i], 0);
6365 ump = ITOUMP(ip);
6366 frags = sblksize(ump->um_fs, ip->i_size, i);
6367 frags = numfrags(ump->um_fs, frags);
6368 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6369 }
6370
6371 static inline void
setup_freeext(struct freeblks * freeblks,struct inode * ip,int i,int needj)6372 setup_freeext(
6373 struct freeblks *freeblks,
6374 struct inode *ip,
6375 int i,
6376 int needj)
6377 {
6378 struct ufsmount *ump;
6379 ufs2_daddr_t blkno;
6380 int frags;
6381
6382 blkno = ip->i_din2->di_extb[i];
6383 if (blkno == 0)
6384 return;
6385 ip->i_din2->di_extb[i] = 0;
6386 ump = ITOUMP(ip);
6387 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6388 frags = numfrags(ump->um_fs, frags);
6389 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6390 }
6391
6392 static inline void
setup_freeindir(struct freeblks * freeblks,struct inode * ip,int i,ufs_lbn_t lbn,int needj)6393 setup_freeindir(
6394 struct freeblks *freeblks,
6395 struct inode *ip,
6396 int i,
6397 ufs_lbn_t lbn,
6398 int needj)
6399 {
6400 struct ufsmount *ump;
6401 ufs2_daddr_t blkno;
6402
6403 blkno = DIP(ip, i_ib[i]);
6404 if (blkno == 0)
6405 return;
6406 DIP_SET(ip, i_ib[i], 0);
6407 ump = ITOUMP(ip);
6408 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6409 0, needj);
6410 }
6411
6412 static inline struct freeblks *
newfreeblks(struct mount * mp,struct inode * ip)6413 newfreeblks(struct mount *mp, struct inode *ip)
6414 {
6415 struct freeblks *freeblks;
6416
6417 freeblks = malloc(sizeof(struct freeblks),
6418 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6419 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6420 LIST_INIT(&freeblks->fb_jblkdephd);
6421 LIST_INIT(&freeblks->fb_jwork);
6422 freeblks->fb_ref = 0;
6423 freeblks->fb_cgwait = 0;
6424 freeblks->fb_state = ATTACHED;
6425 freeblks->fb_uid = ip->i_uid;
6426 freeblks->fb_inum = ip->i_number;
6427 freeblks->fb_vtype = ITOV(ip)->v_type;
6428 freeblks->fb_modrev = DIP(ip, i_modrev);
6429 freeblks->fb_devvp = ITODEVVP(ip);
6430 freeblks->fb_chkcnt = 0;
6431 freeblks->fb_len = 0;
6432
6433 return (freeblks);
6434 }
6435
6436 static void
trunc_indirdep(struct indirdep * indirdep,struct freeblks * freeblks,struct buf * bp,int off)6437 trunc_indirdep(
6438 struct indirdep *indirdep,
6439 struct freeblks *freeblks,
6440 struct buf *bp,
6441 int off)
6442 {
6443 struct allocindir *aip, *aipn;
6444
6445 /*
6446 * The first set of allocindirs won't be in savedbp.
6447 */
6448 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6449 if (aip->ai_offset > off)
6450 cancel_allocindir(aip, bp, freeblks, 1);
6451 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6452 if (aip->ai_offset > off)
6453 cancel_allocindir(aip, bp, freeblks, 1);
6454 /*
6455 * These will exist in savedbp.
6456 */
6457 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6458 if (aip->ai_offset > off)
6459 cancel_allocindir(aip, NULL, freeblks, 0);
6460 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6461 if (aip->ai_offset > off)
6462 cancel_allocindir(aip, NULL, freeblks, 0);
6463 }
6464
6465 /*
6466 * Follow the chain of indirects down to lastlbn creating a freework
6467 * structure for each. This will be used to start indir_trunc() at
6468 * the right offset and create the journal records for the parrtial
6469 * truncation. A second step will handle the truncated dependencies.
6470 */
6471 static int
setup_trunc_indir(struct freeblks * freeblks,struct inode * ip,ufs_lbn_t lbn,ufs_lbn_t lastlbn,ufs2_daddr_t blkno)6472 setup_trunc_indir(
6473 struct freeblks *freeblks,
6474 struct inode *ip,
6475 ufs_lbn_t lbn,
6476 ufs_lbn_t lastlbn,
6477 ufs2_daddr_t blkno)
6478 {
6479 struct indirdep *indirdep;
6480 struct indirdep *indirn;
6481 struct freework *freework;
6482 struct newblk *newblk;
6483 struct mount *mp;
6484 struct ufsmount *ump;
6485 struct buf *bp;
6486 uint8_t *start;
6487 uint8_t *end;
6488 ufs_lbn_t lbnadd;
6489 int level;
6490 int error;
6491 int off;
6492
6493 freework = NULL;
6494 if (blkno == 0)
6495 return (0);
6496 mp = freeblks->fb_list.wk_mp;
6497 ump = VFSTOUFS(mp);
6498 /*
6499 * Here, calls to VOP_BMAP() will fail. However, we already have
6500 * the on-disk address, so we just pass it to bread() instead of
6501 * having bread() attempt to calculate it using VOP_BMAP().
6502 */
6503 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno),
6504 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6505 if (error)
6506 return (error);
6507 level = lbn_level(lbn);
6508 lbnadd = lbn_offset(ump->um_fs, level);
6509 /*
6510 * Compute the offset of the last block we want to keep. Store
6511 * in the freework the first block we want to completely free.
6512 */
6513 off = (lastlbn - -(lbn + level)) / lbnadd;
6514 if (off + 1 == NINDIR(ump->um_fs))
6515 goto nowork;
6516 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6517 /*
6518 * Link the freework into the indirdep. This will prevent any new
6519 * allocations from proceeding until we are finished with the
6520 * truncate and the block is written.
6521 */
6522 ACQUIRE_LOCK(ump);
6523 indirdep = indirdep_lookup(mp, ip, bp);
6524 if (indirdep->ir_freeblks)
6525 panic("setup_trunc_indir: indirdep already truncated.");
6526 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6527 freework->fw_indir = indirdep;
6528 /*
6529 * Cancel any allocindirs that will not make it to disk.
6530 * We have to do this for all copies of the indirdep that
6531 * live on this newblk.
6532 */
6533 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6534 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6535 &newblk) == 0)
6536 panic("setup_trunc_indir: lost block");
6537 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6538 trunc_indirdep(indirn, freeblks, bp, off);
6539 } else
6540 trunc_indirdep(indirdep, freeblks, bp, off);
6541 FREE_LOCK(ump);
6542 /*
6543 * Creation is protected by the buf lock. The saveddata is only
6544 * needed if a full truncation follows a partial truncation but it
6545 * is difficult to allocate in that case so we fetch it anyway.
6546 */
6547 if (indirdep->ir_saveddata == NULL)
6548 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6549 M_SOFTDEP_FLAGS);
6550 nowork:
6551 /* Fetch the blkno of the child and the zero start offset. */
6552 if (I_IS_UFS1(ip)) {
6553 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6554 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6555 } else {
6556 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6557 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6558 }
6559 if (freework) {
6560 /* Zero the truncated pointers. */
6561 end = bp->b_data + bp->b_bcount;
6562 bzero(start, end - start);
6563 bdwrite(bp);
6564 } else
6565 bqrelse(bp);
6566 if (level == 0)
6567 return (0);
6568 lbn++; /* adjust level */
6569 lbn -= (off * lbnadd);
6570 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6571 }
6572
6573 /*
6574 * Complete the partial truncation of an indirect block setup by
6575 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6576 * copy and writes them to disk before the freeblks is allowed to complete.
6577 */
6578 static void
complete_trunc_indir(struct freework * freework)6579 complete_trunc_indir(struct freework *freework)
6580 {
6581 struct freework *fwn;
6582 struct indirdep *indirdep;
6583 struct ufsmount *ump;
6584 struct buf *bp;
6585 uintptr_t start;
6586 int count;
6587
6588 ump = VFSTOUFS(freework->fw_list.wk_mp);
6589 LOCK_OWNED(ump);
6590 indirdep = freework->fw_indir;
6591 for (;;) {
6592 bp = indirdep->ir_bp;
6593 /* See if the block was discarded. */
6594 if (bp == NULL)
6595 break;
6596 /* Inline part of getdirtybuf(). We dont want bremfree. */
6597 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6598 break;
6599 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6600 LOCK_PTR(ump)) == 0)
6601 BUF_UNLOCK(bp);
6602 ACQUIRE_LOCK(ump);
6603 }
6604 freework->fw_state |= DEPCOMPLETE;
6605 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6606 /*
6607 * Zero the pointers in the saved copy.
6608 */
6609 if (indirdep->ir_state & UFS1FMT)
6610 start = sizeof(ufs1_daddr_t);
6611 else
6612 start = sizeof(ufs2_daddr_t);
6613 start *= freework->fw_start;
6614 count = indirdep->ir_savebp->b_bcount - start;
6615 start += (uintptr_t)indirdep->ir_savebp->b_data;
6616 bzero((char *)start, count);
6617 /*
6618 * We need to start the next truncation in the list if it has not
6619 * been started yet.
6620 */
6621 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6622 if (fwn != NULL) {
6623 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6624 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6625 if ((fwn->fw_state & ONWORKLIST) == 0)
6626 freework_enqueue(fwn);
6627 }
6628 /*
6629 * If bp is NULL the block was fully truncated, restore
6630 * the saved block list otherwise free it if it is no
6631 * longer needed.
6632 */
6633 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6634 if (bp == NULL)
6635 bcopy(indirdep->ir_saveddata,
6636 indirdep->ir_savebp->b_data,
6637 indirdep->ir_savebp->b_bcount);
6638 free(indirdep->ir_saveddata, M_INDIRDEP);
6639 indirdep->ir_saveddata = NULL;
6640 }
6641 /*
6642 * When bp is NULL there is a full truncation pending. We
6643 * must wait for this full truncation to be journaled before
6644 * we can release this freework because the disk pointers will
6645 * never be written as zero.
6646 */
6647 if (bp == NULL) {
6648 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6649 handle_written_freework(freework);
6650 else
6651 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6652 &freework->fw_list);
6653 if (fwn == NULL) {
6654 freework->fw_indir = (void *)0x0000deadbeef0000;
6655 bp = indirdep->ir_savebp;
6656 indirdep->ir_savebp = NULL;
6657 free_indirdep(indirdep);
6658 FREE_LOCK(ump);
6659 brelse(bp);
6660 ACQUIRE_LOCK(ump);
6661 }
6662 } else {
6663 /* Complete when the real copy is written. */
6664 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6665 BUF_UNLOCK(bp);
6666 }
6667 }
6668
6669 /*
6670 * Calculate the number of blocks we are going to release where datablocks
6671 * is the current total and length is the new file size.
6672 */
6673 static ufs2_daddr_t
blkcount(struct fs * fs,ufs2_daddr_t datablocks,off_t length)6674 blkcount(struct fs *fs,
6675 ufs2_daddr_t datablocks,
6676 off_t length)
6677 {
6678 off_t totblks, numblks;
6679
6680 totblks = 0;
6681 numblks = howmany(length, fs->fs_bsize);
6682 if (numblks <= UFS_NDADDR) {
6683 totblks = howmany(length, fs->fs_fsize);
6684 goto out;
6685 }
6686 totblks = blkstofrags(fs, numblks);
6687 numblks -= UFS_NDADDR;
6688 /*
6689 * Count all single, then double, then triple indirects required.
6690 * Subtracting one indirects worth of blocks for each pass
6691 * acknowledges one of each pointed to by the inode.
6692 */
6693 for (;;) {
6694 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6695 numblks -= NINDIR(fs);
6696 if (numblks <= 0)
6697 break;
6698 numblks = howmany(numblks, NINDIR(fs));
6699 }
6700 out:
6701 totblks = fsbtodb(fs, totblks);
6702 /*
6703 * Handle sparse files. We can't reclaim more blocks than the inode
6704 * references. We will correct it later in handle_complete_freeblks()
6705 * when we know the real count.
6706 */
6707 if (totblks > datablocks)
6708 return (0);
6709 return (datablocks - totblks);
6710 }
6711
6712 /*
6713 * Handle freeblocks for journaled softupdate filesystems.
6714 *
6715 * Contrary to normal softupdates, we must preserve the block pointers in
6716 * indirects until their subordinates are free. This is to avoid journaling
6717 * every block that is freed which may consume more space than the journal
6718 * itself. The recovery program will see the free block journals at the
6719 * base of the truncated area and traverse them to reclaim space. The
6720 * pointers in the inode may be cleared immediately after the journal
6721 * records are written because each direct and indirect pointer in the
6722 * inode is recorded in a journal. This permits full truncation to proceed
6723 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6724 *
6725 * The algorithm is as follows:
6726 * 1) Traverse the in-memory state and create journal entries to release
6727 * the relevant blocks and full indirect trees.
6728 * 2) Traverse the indirect block chain adding partial truncation freework
6729 * records to indirects in the path to lastlbn. The freework will
6730 * prevent new allocation dependencies from being satisfied in this
6731 * indirect until the truncation completes.
6732 * 3) Read and lock the inode block, performing an update with the new size
6733 * and pointers. This prevents truncated data from becoming valid on
6734 * disk through step 4.
6735 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6736 * eliminate journal work for those records that do not require it.
6737 * 5) Schedule the journal records to be written followed by the inode block.
6738 * 6) Allocate any necessary frags for the end of file.
6739 * 7) Zero any partially truncated blocks.
6740 *
6741 * From this truncation proceeds asynchronously using the freework and
6742 * indir_trunc machinery. The file will not be extended again into a
6743 * partially truncated indirect block until all work is completed but
6744 * the normal dependency mechanism ensures that it is rolled back/forward
6745 * as appropriate. Further truncation may occur without delay and is
6746 * serialized in indir_trunc().
6747 */
6748 void
softdep_journal_freeblocks(struct inode * ip,struct ucred * cred,off_t length,int flags)6749 softdep_journal_freeblocks(
6750 struct inode *ip, /* The inode whose length is to be reduced */
6751 struct ucred *cred,
6752 off_t length, /* The new length for the file */
6753 int flags) /* IO_EXT and/or IO_NORMAL */
6754 {
6755 struct freeblks *freeblks, *fbn;
6756 struct worklist *wk, *wkn;
6757 struct inodedep *inodedep;
6758 struct jblkdep *jblkdep;
6759 struct allocdirect *adp, *adpn;
6760 struct ufsmount *ump;
6761 struct fs *fs;
6762 struct buf *bp;
6763 struct vnode *vp;
6764 struct mount *mp;
6765 daddr_t dbn;
6766 ufs2_daddr_t extblocks, datablocks;
6767 ufs_lbn_t tmpval, lbn, lastlbn;
6768 int frags, lastoff, iboff, allocblock, needj, error, i;
6769
6770 ump = ITOUMP(ip);
6771 mp = UFSTOVFS(ump);
6772 fs = ump->um_fs;
6773 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6774 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6775 vp = ITOV(ip);
6776 needj = 1;
6777 iboff = -1;
6778 allocblock = 0;
6779 extblocks = 0;
6780 datablocks = 0;
6781 frags = 0;
6782 freeblks = newfreeblks(mp, ip);
6783 ACQUIRE_LOCK(ump);
6784 /*
6785 * If we're truncating a removed file that will never be written
6786 * we don't need to journal the block frees. The canceled journals
6787 * for the allocations will suffice.
6788 */
6789 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6790 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6791 length == 0)
6792 needj = 0;
6793 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6794 ip->i_number, length, needj);
6795 FREE_LOCK(ump);
6796 /*
6797 * Calculate the lbn that we are truncating to. This results in -1
6798 * if we're truncating the 0 bytes. So it is the last lbn we want
6799 * to keep, not the first lbn we want to truncate.
6800 */
6801 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6802 lastoff = blkoff(fs, length);
6803 /*
6804 * Compute frags we are keeping in lastlbn. 0 means all.
6805 */
6806 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6807 frags = fragroundup(fs, lastoff);
6808 /* adp offset of last valid allocdirect. */
6809 iboff = lastlbn;
6810 } else if (lastlbn > 0)
6811 iboff = UFS_NDADDR;
6812 if (fs->fs_magic == FS_UFS2_MAGIC)
6813 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6814 /*
6815 * Handle normal data blocks and indirects. This section saves
6816 * values used after the inode update to complete frag and indirect
6817 * truncation.
6818 */
6819 if ((flags & IO_NORMAL) != 0) {
6820 /*
6821 * Handle truncation of whole direct and indirect blocks.
6822 */
6823 for (i = iboff + 1; i < UFS_NDADDR; i++)
6824 setup_freedirect(freeblks, ip, i, needj);
6825 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6826 i < UFS_NIADDR;
6827 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6828 /* Release a whole indirect tree. */
6829 if (lbn > lastlbn) {
6830 setup_freeindir(freeblks, ip, i, -lbn -i,
6831 needj);
6832 continue;
6833 }
6834 iboff = i + UFS_NDADDR;
6835 /*
6836 * Traverse partially truncated indirect tree.
6837 */
6838 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6839 setup_trunc_indir(freeblks, ip, -lbn - i,
6840 lastlbn, DIP(ip, i_ib[i]));
6841 }
6842 /*
6843 * Handle partial truncation to a frag boundary.
6844 */
6845 if (frags) {
6846 ufs2_daddr_t blkno;
6847 long oldfrags;
6848
6849 oldfrags = blksize(fs, ip, lastlbn);
6850 blkno = DIP(ip, i_db[lastlbn]);
6851 if (blkno && oldfrags != frags) {
6852 oldfrags -= frags;
6853 oldfrags = numfrags(fs, oldfrags);
6854 blkno += numfrags(fs, frags);
6855 newfreework(ump, freeblks, NULL, lastlbn,
6856 blkno, oldfrags, 0, needj);
6857 if (needj)
6858 adjust_newfreework(freeblks,
6859 numfrags(fs, frags));
6860 } else if (blkno == 0)
6861 allocblock = 1;
6862 }
6863 /*
6864 * Add a journal record for partial truncate if we are
6865 * handling indirect blocks. Non-indirects need no extra
6866 * journaling.
6867 */
6868 if (length != 0 && lastlbn >= UFS_NDADDR) {
6869 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
6870 newjtrunc(freeblks, length, 0);
6871 }
6872 ip->i_size = length;
6873 DIP_SET(ip, i_size, ip->i_size);
6874 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6875 datablocks = DIP(ip, i_blocks) - extblocks;
6876 if (length != 0)
6877 datablocks = blkcount(fs, datablocks, length);
6878 freeblks->fb_len = length;
6879 }
6880 if ((flags & IO_EXT) != 0) {
6881 for (i = 0; i < UFS_NXADDR; i++)
6882 setup_freeext(freeblks, ip, i, needj);
6883 ip->i_din2->di_extsize = 0;
6884 datablocks += extblocks;
6885 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6886 }
6887 #ifdef QUOTA
6888 /* Reference the quotas in case the block count is wrong in the end. */
6889 quotaref(vp, freeblks->fb_quota);
6890 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
6891 #endif
6892 freeblks->fb_chkcnt = -datablocks;
6893 UFS_LOCK(ump);
6894 fs->fs_pendingblocks += datablocks;
6895 UFS_UNLOCK(ump);
6896 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6897 /*
6898 * Handle truncation of incomplete alloc direct dependencies. We
6899 * hold the inode block locked to prevent incomplete dependencies
6900 * from reaching the disk while we are eliminating those that
6901 * have been truncated. This is a partially inlined ffs_update().
6902 */
6903 ufs_itimes(vp);
6904 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
6905 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
6906 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
6907 NULL, NULL, 0, cred, 0, NULL, &bp);
6908 if (error) {
6909 softdep_error("softdep_journal_freeblocks", error);
6910 return;
6911 }
6912 if (bp->b_bufsize == fs->fs_bsize)
6913 bp->b_flags |= B_CLUSTEROK;
6914 softdep_update_inodeblock(ip, bp, 0);
6915 if (ump->um_fstype == UFS1) {
6916 *((struct ufs1_dinode *)bp->b_data +
6917 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
6918 } else {
6919 ffs_update_dinode_ckhash(fs, ip->i_din2);
6920 *((struct ufs2_dinode *)bp->b_data +
6921 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
6922 }
6923 ACQUIRE_LOCK(ump);
6924 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6925 if ((inodedep->id_state & IOSTARTED) != 0)
6926 panic("softdep_setup_freeblocks: inode busy");
6927 /*
6928 * Add the freeblks structure to the list of operations that
6929 * must await the zero'ed inode being written to disk. If we
6930 * still have a bitmap dependency (needj), then the inode
6931 * has never been written to disk, so we can process the
6932 * freeblks below once we have deleted the dependencies.
6933 */
6934 if (needj)
6935 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6936 else
6937 freeblks->fb_state |= COMPLETE;
6938 if ((flags & IO_NORMAL) != 0) {
6939 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
6940 if (adp->ad_offset > iboff)
6941 cancel_allocdirect(&inodedep->id_inoupdt, adp,
6942 freeblks);
6943 /*
6944 * Truncate the allocdirect. We could eliminate
6945 * or modify journal records as well.
6946 */
6947 else if (adp->ad_offset == iboff && frags)
6948 adp->ad_newsize = frags;
6949 }
6950 }
6951 if ((flags & IO_EXT) != 0)
6952 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
6953 cancel_allocdirect(&inodedep->id_extupdt, adp,
6954 freeblks);
6955 /*
6956 * Scan the bufwait list for newblock dependencies that will never
6957 * make it to disk.
6958 */
6959 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6960 if (wk->wk_type != D_ALLOCDIRECT)
6961 continue;
6962 adp = WK_ALLOCDIRECT(wk);
6963 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
6964 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
6965 cancel_jfreeblk(freeblks, adp->ad_newblkno);
6966 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
6967 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
6968 }
6969 }
6970 /*
6971 * Add journal work.
6972 */
6973 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6974 add_to_journal(&jblkdep->jb_list);
6975 FREE_LOCK(ump);
6976 bdwrite(bp);
6977 /*
6978 * Truncate dependency structures beyond length.
6979 */
6980 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
6981 /*
6982 * This is only set when we need to allocate a fragment because
6983 * none existed at the end of a frag-sized file. It handles only
6984 * allocating a new, zero filled block.
6985 */
6986 if (allocblock) {
6987 ip->i_size = length - lastoff;
6988 DIP_SET(ip, i_size, ip->i_size);
6989 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
6990 if (error != 0) {
6991 softdep_error("softdep_journal_freeblks", error);
6992 return;
6993 }
6994 ip->i_size = length;
6995 DIP_SET(ip, i_size, length);
6996 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
6997 allocbuf(bp, frags);
6998 ffs_update(vp, 0);
6999 bawrite(bp);
7000 } else if (lastoff != 0 && vp->v_type != VDIR) {
7001 int size;
7002
7003 /*
7004 * Zero the end of a truncated frag or block.
7005 */
7006 size = sblksize(fs, length, lastlbn);
7007 error = bread(vp, lastlbn, size, cred, &bp);
7008 if (error == 0) {
7009 bzero((char *)bp->b_data + lastoff, size - lastoff);
7010 bawrite(bp);
7011 } else if (!ffs_fsfail_cleanup(ump, error)) {
7012 softdep_error("softdep_journal_freeblks", error);
7013 return;
7014 }
7015 }
7016 ACQUIRE_LOCK(ump);
7017 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7018 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
7019 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
7020 /*
7021 * We zero earlier truncations so they don't erroneously
7022 * update i_blocks.
7023 */
7024 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
7025 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
7026 fbn->fb_len = 0;
7027 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
7028 LIST_EMPTY(&freeblks->fb_jblkdephd))
7029 freeblks->fb_state |= INPROGRESS;
7030 else
7031 freeblks = NULL;
7032 FREE_LOCK(ump);
7033 if (freeblks)
7034 handle_workitem_freeblocks(freeblks, 0);
7035 trunc_pages(ip, length, extblocks, flags);
7036
7037 }
7038
7039 /*
7040 * Flush a JOP_SYNC to the journal.
7041 */
7042 void
softdep_journal_fsync(struct inode * ip)7043 softdep_journal_fsync(struct inode *ip)
7044 {
7045 struct jfsync *jfsync;
7046 struct ufsmount *ump;
7047
7048 ump = ITOUMP(ip);
7049 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7050 ("softdep_journal_fsync called on non-softdep filesystem"));
7051 if ((ip->i_flag & IN_TRUNCATED) == 0)
7052 return;
7053 ip->i_flag &= ~IN_TRUNCATED;
7054 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
7055 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
7056 jfsync->jfs_size = ip->i_size;
7057 jfsync->jfs_ino = ip->i_number;
7058 ACQUIRE_LOCK(ump);
7059 add_to_journal(&jfsync->jfs_list);
7060 jwait(&jfsync->jfs_list, MNT_WAIT);
7061 FREE_LOCK(ump);
7062 }
7063
7064 /*
7065 * Block de-allocation dependencies.
7066 *
7067 * When blocks are de-allocated, the on-disk pointers must be nullified before
7068 * the blocks are made available for use by other files. (The true
7069 * requirement is that old pointers must be nullified before new on-disk
7070 * pointers are set. We chose this slightly more stringent requirement to
7071 * reduce complexity.) Our implementation handles this dependency by updating
7072 * the inode (or indirect block) appropriately but delaying the actual block
7073 * de-allocation (i.e., freemap and free space count manipulation) until
7074 * after the updated versions reach stable storage. After the disk is
7075 * updated, the blocks can be safely de-allocated whenever it is convenient.
7076 * This implementation handles only the common case of reducing a file's
7077 * length to zero. Other cases are handled by the conventional synchronous
7078 * write approach.
7079 *
7080 * The ffs implementation with which we worked double-checks
7081 * the state of the block pointers and file size as it reduces
7082 * a file's length. Some of this code is replicated here in our
7083 * soft updates implementation. The freeblks->fb_chkcnt field is
7084 * used to transfer a part of this information to the procedure
7085 * that eventually de-allocates the blocks.
7086 *
7087 * This routine should be called from the routine that shortens
7088 * a file's length, before the inode's size or block pointers
7089 * are modified. It will save the block pointer information for
7090 * later release and zero the inode so that the calling routine
7091 * can release it.
7092 */
7093 void
softdep_setup_freeblocks(struct inode * ip,off_t length,int flags)7094 softdep_setup_freeblocks(
7095 struct inode *ip, /* The inode whose length is to be reduced */
7096 off_t length, /* The new length for the file */
7097 int flags) /* IO_EXT and/or IO_NORMAL */
7098 {
7099 struct ufs1_dinode *dp1;
7100 struct ufs2_dinode *dp2;
7101 struct freeblks *freeblks;
7102 struct inodedep *inodedep;
7103 struct allocdirect *adp;
7104 struct ufsmount *ump;
7105 struct buf *bp;
7106 struct fs *fs;
7107 ufs2_daddr_t extblocks, datablocks;
7108 struct mount *mp;
7109 int i, delay, error;
7110 ufs_lbn_t tmpval;
7111 ufs_lbn_t lbn;
7112
7113 ump = ITOUMP(ip);
7114 mp = UFSTOVFS(ump);
7115 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
7116 ("softdep_setup_freeblocks called on non-softdep filesystem"));
7117 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
7118 ip->i_number, length);
7119 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
7120 fs = ump->um_fs;
7121 if ((error = bread(ump->um_devvp,
7122 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
7123 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
7124 if (!ffs_fsfail_cleanup(ump, error))
7125 softdep_error("softdep_setup_freeblocks", error);
7126 return;
7127 }
7128 freeblks = newfreeblks(mp, ip);
7129 extblocks = 0;
7130 datablocks = 0;
7131 if (fs->fs_magic == FS_UFS2_MAGIC)
7132 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
7133 if ((flags & IO_NORMAL) != 0) {
7134 for (i = 0; i < UFS_NDADDR; i++)
7135 setup_freedirect(freeblks, ip, i, 0);
7136 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
7137 i < UFS_NIADDR;
7138 i++, lbn += tmpval, tmpval *= NINDIR(fs))
7139 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
7140 ip->i_size = 0;
7141 DIP_SET(ip, i_size, 0);
7142 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7143 datablocks = DIP(ip, i_blocks) - extblocks;
7144 }
7145 if ((flags & IO_EXT) != 0) {
7146 for (i = 0; i < UFS_NXADDR; i++)
7147 setup_freeext(freeblks, ip, i, 0);
7148 ip->i_din2->di_extsize = 0;
7149 datablocks += extblocks;
7150 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7151 }
7152 #ifdef QUOTA
7153 /* Reference the quotas in case the block count is wrong in the end. */
7154 quotaref(ITOV(ip), freeblks->fb_quota);
7155 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
7156 #endif
7157 freeblks->fb_chkcnt = -datablocks;
7158 UFS_LOCK(ump);
7159 fs->fs_pendingblocks += datablocks;
7160 UFS_UNLOCK(ump);
7161 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7162 /*
7163 * Push the zero'ed inode to its disk buffer so that we are free
7164 * to delete its dependencies below. Once the dependencies are gone
7165 * the buffer can be safely released.
7166 */
7167 if (ump->um_fstype == UFS1) {
7168 dp1 = ((struct ufs1_dinode *)bp->b_data +
7169 ino_to_fsbo(fs, ip->i_number));
7170 ip->i_din1->di_freelink = dp1->di_freelink;
7171 *dp1 = *ip->i_din1;
7172 } else {
7173 dp2 = ((struct ufs2_dinode *)bp->b_data +
7174 ino_to_fsbo(fs, ip->i_number));
7175 ip->i_din2->di_freelink = dp2->di_freelink;
7176 ffs_update_dinode_ckhash(fs, ip->i_din2);
7177 *dp2 = *ip->i_din2;
7178 }
7179 /*
7180 * Find and eliminate any inode dependencies.
7181 */
7182 ACQUIRE_LOCK(ump);
7183 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7184 if ((inodedep->id_state & IOSTARTED) != 0)
7185 panic("softdep_setup_freeblocks: inode busy");
7186 /*
7187 * Add the freeblks structure to the list of operations that
7188 * must await the zero'ed inode being written to disk. If we
7189 * still have a bitmap dependency (delay == 0), then the inode
7190 * has never been written to disk, so we can process the
7191 * freeblks below once we have deleted the dependencies.
7192 */
7193 delay = (inodedep->id_state & DEPCOMPLETE);
7194 if (delay)
7195 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7196 else
7197 freeblks->fb_state |= COMPLETE;
7198 /*
7199 * Because the file length has been truncated to zero, any
7200 * pending block allocation dependency structures associated
7201 * with this inode are obsolete and can simply be de-allocated.
7202 * We must first merge the two dependency lists to get rid of
7203 * any duplicate freefrag structures, then purge the merged list.
7204 * If we still have a bitmap dependency, then the inode has never
7205 * been written to disk, so we can free any fragments without delay.
7206 */
7207 if (flags & IO_NORMAL) {
7208 merge_inode_lists(&inodedep->id_newinoupdt,
7209 &inodedep->id_inoupdt);
7210 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7211 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7212 freeblks);
7213 }
7214 if (flags & IO_EXT) {
7215 merge_inode_lists(&inodedep->id_newextupdt,
7216 &inodedep->id_extupdt);
7217 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7218 cancel_allocdirect(&inodedep->id_extupdt, adp,
7219 freeblks);
7220 }
7221 FREE_LOCK(ump);
7222 bdwrite(bp);
7223 trunc_dependencies(ip, freeblks, -1, 0, flags);
7224 ACQUIRE_LOCK(ump);
7225 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7226 (void) free_inodedep(inodedep);
7227 freeblks->fb_state |= DEPCOMPLETE;
7228 /*
7229 * If the inode with zeroed block pointers is now on disk
7230 * we can start freeing blocks.
7231 */
7232 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7233 freeblks->fb_state |= INPROGRESS;
7234 else
7235 freeblks = NULL;
7236 FREE_LOCK(ump);
7237 if (freeblks)
7238 handle_workitem_freeblocks(freeblks, 0);
7239 trunc_pages(ip, length, extblocks, flags);
7240 }
7241
7242 /*
7243 * Eliminate pages from the page cache that back parts of this inode and
7244 * adjust the vnode pager's idea of our size. This prevents stale data
7245 * from hanging around in the page cache.
7246 */
7247 static void
trunc_pages(struct inode * ip,off_t length,ufs2_daddr_t extblocks,int flags)7248 trunc_pages(
7249 struct inode *ip,
7250 off_t length,
7251 ufs2_daddr_t extblocks,
7252 int flags)
7253 {
7254 struct vnode *vp;
7255 struct fs *fs;
7256 ufs_lbn_t lbn;
7257 off_t end, extend;
7258
7259 vp = ITOV(ip);
7260 fs = ITOFS(ip);
7261 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7262 if ((flags & IO_EXT) != 0)
7263 vn_pages_remove(vp, extend, 0);
7264 if ((flags & IO_NORMAL) == 0)
7265 return;
7266 BO_LOCK(&vp->v_bufobj);
7267 drain_output(vp);
7268 BO_UNLOCK(&vp->v_bufobj);
7269 /*
7270 * The vnode pager eliminates file pages we eliminate indirects
7271 * below.
7272 */
7273 vnode_pager_setsize(vp, length);
7274 /*
7275 * Calculate the end based on the last indirect we want to keep. If
7276 * the block extends into indirects we can just use the negative of
7277 * its lbn. Doubles and triples exist at lower numbers so we must
7278 * be careful not to remove those, if they exist. double and triple
7279 * indirect lbns do not overlap with others so it is not important
7280 * to verify how many levels are required.
7281 */
7282 lbn = lblkno(fs, length);
7283 if (lbn >= UFS_NDADDR) {
7284 /* Calculate the virtual lbn of the triple indirect. */
7285 lbn = -lbn - (UFS_NIADDR - 1);
7286 end = OFF_TO_IDX(lblktosize(fs, lbn));
7287 } else
7288 end = extend;
7289 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7290 }
7291
7292 /*
7293 * See if the buf bp is in the range eliminated by truncation.
7294 */
7295 static int
trunc_check_buf(struct buf * bp,int * blkoffp,ufs_lbn_t lastlbn,int lastoff,int flags)7296 trunc_check_buf(
7297 struct buf *bp,
7298 int *blkoffp,
7299 ufs_lbn_t lastlbn,
7300 int lastoff,
7301 int flags)
7302 {
7303 ufs_lbn_t lbn;
7304
7305 *blkoffp = 0;
7306 /* Only match ext/normal blocks as appropriate. */
7307 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7308 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7309 return (0);
7310 /* ALTDATA is always a full truncation. */
7311 if ((bp->b_xflags & BX_ALTDATA) != 0)
7312 return (1);
7313 /* -1 is full truncation. */
7314 if (lastlbn == -1)
7315 return (1);
7316 /*
7317 * If this is a partial truncate we only want those
7318 * blocks and indirect blocks that cover the range
7319 * we're after.
7320 */
7321 lbn = bp->b_lblkno;
7322 if (lbn < 0)
7323 lbn = -(lbn + lbn_level(lbn));
7324 if (lbn < lastlbn)
7325 return (0);
7326 /* Here we only truncate lblkno if it's partial. */
7327 if (lbn == lastlbn) {
7328 if (lastoff == 0)
7329 return (0);
7330 *blkoffp = lastoff;
7331 }
7332 return (1);
7333 }
7334
7335 /*
7336 * Eliminate any dependencies that exist in memory beyond lblkno:off
7337 */
7338 static void
trunc_dependencies(struct inode * ip,struct freeblks * freeblks,ufs_lbn_t lastlbn,int lastoff,int flags)7339 trunc_dependencies(
7340 struct inode *ip,
7341 struct freeblks *freeblks,
7342 ufs_lbn_t lastlbn,
7343 int lastoff,
7344 int flags)
7345 {
7346 struct bufobj *bo;
7347 struct vnode *vp;
7348 struct buf *bp;
7349 int blkoff;
7350
7351 /*
7352 * We must wait for any I/O in progress to finish so that
7353 * all potential buffers on the dirty list will be visible.
7354 * Once they are all there, walk the list and get rid of
7355 * any dependencies.
7356 */
7357 vp = ITOV(ip);
7358 bo = &vp->v_bufobj;
7359 BO_LOCK(bo);
7360 drain_output(vp);
7361 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7362 bp->b_vflags &= ~BV_SCANNED;
7363 restart:
7364 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7365 if (bp->b_vflags & BV_SCANNED)
7366 continue;
7367 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7368 bp->b_vflags |= BV_SCANNED;
7369 continue;
7370 }
7371 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7372 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7373 goto restart;
7374 BO_UNLOCK(bo);
7375 if (deallocate_dependencies(bp, freeblks, blkoff))
7376 bqrelse(bp);
7377 else
7378 brelse(bp);
7379 BO_LOCK(bo);
7380 goto restart;
7381 }
7382 /*
7383 * Now do the work of vtruncbuf while also matching indirect blocks.
7384 */
7385 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7386 bp->b_vflags &= ~BV_SCANNED;
7387 cleanrestart:
7388 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7389 if (bp->b_vflags & BV_SCANNED)
7390 continue;
7391 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7392 bp->b_vflags |= BV_SCANNED;
7393 continue;
7394 }
7395 if (BUF_LOCK(bp,
7396 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7397 BO_LOCKPTR(bo)) == ENOLCK) {
7398 BO_LOCK(bo);
7399 goto cleanrestart;
7400 }
7401 BO_LOCK(bo);
7402 bp->b_vflags |= BV_SCANNED;
7403 BO_UNLOCK(bo);
7404 bremfree(bp);
7405 if (blkoff != 0) {
7406 allocbuf(bp, blkoff);
7407 bqrelse(bp);
7408 } else {
7409 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7410 brelse(bp);
7411 }
7412 BO_LOCK(bo);
7413 goto cleanrestart;
7414 }
7415 drain_output(vp);
7416 BO_UNLOCK(bo);
7417 }
7418
7419 static int
cancel_pagedep(struct pagedep * pagedep,struct freeblks * freeblks,int blkoff)7420 cancel_pagedep(
7421 struct pagedep *pagedep,
7422 struct freeblks *freeblks,
7423 int blkoff)
7424 {
7425 struct jremref *jremref;
7426 struct jmvref *jmvref;
7427 struct dirrem *dirrem, *tmp;
7428 int i;
7429
7430 /*
7431 * Copy any directory remove dependencies to the list
7432 * to be processed after the freeblks proceeds. If
7433 * directory entry never made it to disk they
7434 * can be dumped directly onto the work list.
7435 */
7436 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7437 /* Skip this directory removal if it is intended to remain. */
7438 if (dirrem->dm_offset < blkoff)
7439 continue;
7440 /*
7441 * If there are any dirrems we wait for the journal write
7442 * to complete and then restart the buf scan as the lock
7443 * has been dropped.
7444 */
7445 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7446 jwait(&jremref->jr_list, MNT_WAIT);
7447 return (ERESTART);
7448 }
7449 LIST_REMOVE(dirrem, dm_next);
7450 dirrem->dm_dirinum = pagedep->pd_ino;
7451 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7452 }
7453 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7454 jwait(&jmvref->jm_list, MNT_WAIT);
7455 return (ERESTART);
7456 }
7457 /*
7458 * When we're partially truncating a pagedep we just want to flush
7459 * journal entries and return. There can not be any adds in the
7460 * truncated portion of the directory and newblk must remain if
7461 * part of the block remains.
7462 */
7463 if (blkoff != 0) {
7464 struct diradd *dap;
7465
7466 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7467 if (dap->da_offset > blkoff)
7468 panic("cancel_pagedep: diradd %p off %d > %d",
7469 dap, dap->da_offset, blkoff);
7470 for (i = 0; i < DAHASHSZ; i++)
7471 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7472 if (dap->da_offset > blkoff)
7473 panic("cancel_pagedep: diradd %p off %d > %d",
7474 dap, dap->da_offset, blkoff);
7475 return (0);
7476 }
7477 /*
7478 * There should be no directory add dependencies present
7479 * as the directory could not be truncated until all
7480 * children were removed.
7481 */
7482 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7483 ("deallocate_dependencies: pendinghd != NULL"));
7484 for (i = 0; i < DAHASHSZ; i++)
7485 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7486 ("deallocate_dependencies: diraddhd != NULL"));
7487 if ((pagedep->pd_state & NEWBLOCK) != 0)
7488 free_newdirblk(pagedep->pd_newdirblk);
7489 if (free_pagedep(pagedep) == 0)
7490 panic("Failed to free pagedep %p", pagedep);
7491 return (0);
7492 }
7493
7494 /*
7495 * Reclaim any dependency structures from a buffer that is about to
7496 * be reallocated to a new vnode. The buffer must be locked, thus,
7497 * no I/O completion operations can occur while we are manipulating
7498 * its associated dependencies. The mutex is held so that other I/O's
7499 * associated with related dependencies do not occur.
7500 */
7501 static int
deallocate_dependencies(struct buf * bp,struct freeblks * freeblks,int off)7502 deallocate_dependencies(
7503 struct buf *bp,
7504 struct freeblks *freeblks,
7505 int off)
7506 {
7507 struct indirdep *indirdep;
7508 struct pagedep *pagedep;
7509 struct worklist *wk, *wkn;
7510 struct ufsmount *ump;
7511
7512 ump = softdep_bp_to_mp(bp);
7513 if (ump == NULL)
7514 goto done;
7515 ACQUIRE_LOCK(ump);
7516 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7517 switch (wk->wk_type) {
7518 case D_INDIRDEP:
7519 indirdep = WK_INDIRDEP(wk);
7520 if (bp->b_lblkno >= 0 ||
7521 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7522 panic("deallocate_dependencies: not indir");
7523 cancel_indirdep(indirdep, bp, freeblks);
7524 continue;
7525
7526 case D_PAGEDEP:
7527 pagedep = WK_PAGEDEP(wk);
7528 if (cancel_pagedep(pagedep, freeblks, off)) {
7529 FREE_LOCK(ump);
7530 return (ERESTART);
7531 }
7532 continue;
7533
7534 case D_ALLOCINDIR:
7535 /*
7536 * Simply remove the allocindir, we'll find it via
7537 * the indirdep where we can clear pointers if
7538 * needed.
7539 */
7540 WORKLIST_REMOVE(wk);
7541 continue;
7542
7543 case D_FREEWORK:
7544 /*
7545 * A truncation is waiting for the zero'd pointers
7546 * to be written. It can be freed when the freeblks
7547 * is journaled.
7548 */
7549 WORKLIST_REMOVE(wk);
7550 wk->wk_state |= ONDEPLIST;
7551 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7552 break;
7553
7554 case D_ALLOCDIRECT:
7555 if (off != 0)
7556 continue;
7557 /* FALLTHROUGH */
7558 default:
7559 panic("deallocate_dependencies: Unexpected type %s",
7560 TYPENAME(wk->wk_type));
7561 /* NOTREACHED */
7562 }
7563 }
7564 FREE_LOCK(ump);
7565 done:
7566 /*
7567 * Don't throw away this buf, we were partially truncating and
7568 * some deps may always remain.
7569 */
7570 if (off) {
7571 allocbuf(bp, off);
7572 bp->b_vflags |= BV_SCANNED;
7573 return (EBUSY);
7574 }
7575 bp->b_flags |= B_INVAL | B_NOCACHE;
7576
7577 return (0);
7578 }
7579
7580 /*
7581 * An allocdirect is being canceled due to a truncate. We must make sure
7582 * the journal entry is released in concert with the blkfree that releases
7583 * the storage. Completed journal entries must not be released until the
7584 * space is no longer pointed to by the inode or in the bitmap.
7585 */
7586 static void
cancel_allocdirect(struct allocdirectlst * adphead,struct allocdirect * adp,struct freeblks * freeblks)7587 cancel_allocdirect(
7588 struct allocdirectlst *adphead,
7589 struct allocdirect *adp,
7590 struct freeblks *freeblks)
7591 {
7592 struct freework *freework;
7593 struct newblk *newblk;
7594 struct worklist *wk;
7595
7596 TAILQ_REMOVE(adphead, adp, ad_next);
7597 newblk = (struct newblk *)adp;
7598 freework = NULL;
7599 /*
7600 * Find the correct freework structure.
7601 */
7602 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7603 if (wk->wk_type != D_FREEWORK)
7604 continue;
7605 freework = WK_FREEWORK(wk);
7606 if (freework->fw_blkno == newblk->nb_newblkno)
7607 break;
7608 }
7609 if (freework == NULL)
7610 panic("cancel_allocdirect: Freework not found");
7611 /*
7612 * If a newblk exists at all we still have the journal entry that
7613 * initiated the allocation so we do not need to journal the free.
7614 */
7615 cancel_jfreeblk(freeblks, freework->fw_blkno);
7616 /*
7617 * If the journal hasn't been written the jnewblk must be passed
7618 * to the call to ffs_blkfree that reclaims the space. We accomplish
7619 * this by linking the journal dependency into the freework to be
7620 * freed when freework_freeblock() is called. If the journal has
7621 * been written we can simply reclaim the journal space when the
7622 * freeblks work is complete.
7623 */
7624 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7625 &freeblks->fb_jwork);
7626 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7627 }
7628
7629 /*
7630 * Cancel a new block allocation. May be an indirect or direct block. We
7631 * remove it from various lists and return any journal record that needs to
7632 * be resolved by the caller.
7633 *
7634 * A special consideration is made for indirects which were never pointed
7635 * at on disk and will never be found once this block is released.
7636 */
7637 static struct jnewblk *
cancel_newblk(struct newblk * newblk,struct worklist * wk,struct workhead * wkhd)7638 cancel_newblk(
7639 struct newblk *newblk,
7640 struct worklist *wk,
7641 struct workhead *wkhd)
7642 {
7643 struct jnewblk *jnewblk;
7644
7645 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7646
7647 newblk->nb_state |= GOINGAWAY;
7648 /*
7649 * Previously we traversed the completedhd on each indirdep
7650 * attached to this newblk to cancel them and gather journal
7651 * work. Since we need only the oldest journal segment and
7652 * the lowest point on the tree will always have the oldest
7653 * journal segment we are free to release the segments
7654 * of any subordinates and may leave the indirdep list to
7655 * indirdep_complete() when this newblk is freed.
7656 */
7657 if (newblk->nb_state & ONDEPLIST) {
7658 newblk->nb_state &= ~ONDEPLIST;
7659 LIST_REMOVE(newblk, nb_deps);
7660 }
7661 if (newblk->nb_state & ONWORKLIST)
7662 WORKLIST_REMOVE(&newblk->nb_list);
7663 /*
7664 * If the journal entry hasn't been written we save a pointer to
7665 * the dependency that frees it until it is written or the
7666 * superseding operation completes.
7667 */
7668 jnewblk = newblk->nb_jnewblk;
7669 if (jnewblk != NULL && wk != NULL) {
7670 newblk->nb_jnewblk = NULL;
7671 jnewblk->jn_dep = wk;
7672 }
7673 if (!LIST_EMPTY(&newblk->nb_jwork))
7674 jwork_move(wkhd, &newblk->nb_jwork);
7675 /*
7676 * When truncating we must free the newdirblk early to remove
7677 * the pagedep from the hash before returning.
7678 */
7679 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7680 free_newdirblk(WK_NEWDIRBLK(wk));
7681 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7682 panic("cancel_newblk: extra newdirblk");
7683
7684 return (jnewblk);
7685 }
7686
7687 /*
7688 * Schedule the freefrag associated with a newblk to be released once
7689 * the pointers are written and the previous block is no longer needed.
7690 */
7691 static void
newblk_freefrag(struct newblk * newblk)7692 newblk_freefrag(struct newblk *newblk)
7693 {
7694 struct freefrag *freefrag;
7695
7696 if (newblk->nb_freefrag == NULL)
7697 return;
7698 freefrag = newblk->nb_freefrag;
7699 newblk->nb_freefrag = NULL;
7700 freefrag->ff_state |= COMPLETE;
7701 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7702 add_to_worklist(&freefrag->ff_list, 0);
7703 }
7704
7705 /*
7706 * Free a newblk. Generate a new freefrag work request if appropriate.
7707 * This must be called after the inode pointer and any direct block pointers
7708 * are valid or fully removed via truncate or frag extension.
7709 */
7710 static void
free_newblk(struct newblk * newblk)7711 free_newblk(struct newblk *newblk)
7712 {
7713 struct indirdep *indirdep;
7714 struct worklist *wk;
7715
7716 KASSERT(newblk->nb_jnewblk == NULL,
7717 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7718 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7719 ("free_newblk: unclaimed newblk"));
7720 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7721 newblk_freefrag(newblk);
7722 if (newblk->nb_state & ONDEPLIST)
7723 LIST_REMOVE(newblk, nb_deps);
7724 if (newblk->nb_state & ONWORKLIST)
7725 WORKLIST_REMOVE(&newblk->nb_list);
7726 LIST_REMOVE(newblk, nb_hash);
7727 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7728 free_newdirblk(WK_NEWDIRBLK(wk));
7729 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7730 panic("free_newblk: extra newdirblk");
7731 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7732 indirdep_complete(indirdep);
7733 handle_jwork(&newblk->nb_jwork);
7734 WORKITEM_FREE(newblk, D_NEWBLK);
7735 }
7736
7737 /*
7738 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7739 */
7740 static void
free_newdirblk(struct newdirblk * newdirblk)7741 free_newdirblk(struct newdirblk *newdirblk)
7742 {
7743 struct pagedep *pagedep;
7744 struct diradd *dap;
7745 struct worklist *wk;
7746
7747 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7748 WORKLIST_REMOVE(&newdirblk->db_list);
7749 /*
7750 * If the pagedep is still linked onto the directory buffer
7751 * dependency chain, then some of the entries on the
7752 * pd_pendinghd list may not be committed to disk yet. In
7753 * this case, we will simply clear the NEWBLOCK flag and
7754 * let the pd_pendinghd list be processed when the pagedep
7755 * is next written. If the pagedep is no longer on the buffer
7756 * dependency chain, then all the entries on the pd_pending
7757 * list are committed to disk and we can free them here.
7758 */
7759 pagedep = newdirblk->db_pagedep;
7760 pagedep->pd_state &= ~NEWBLOCK;
7761 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7762 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7763 free_diradd(dap, NULL);
7764 /*
7765 * If no dependencies remain, the pagedep will be freed.
7766 */
7767 free_pagedep(pagedep);
7768 }
7769 /* Should only ever be one item in the list. */
7770 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7771 WORKLIST_REMOVE(wk);
7772 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7773 }
7774 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7775 }
7776
7777 /*
7778 * Prepare an inode to be freed. The actual free operation is not
7779 * done until the zero'ed inode has been written to disk.
7780 */
7781 void
softdep_freefile(struct vnode * pvp,ino_t ino,int mode)7782 softdep_freefile(
7783 struct vnode *pvp,
7784 ino_t ino,
7785 int mode)
7786 {
7787 struct inode *ip = VTOI(pvp);
7788 struct inodedep *inodedep;
7789 struct freefile *freefile;
7790 struct freeblks *freeblks;
7791 struct ufsmount *ump;
7792
7793 ump = ITOUMP(ip);
7794 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7795 ("softdep_freefile called on non-softdep filesystem"));
7796 /*
7797 * This sets up the inode de-allocation dependency.
7798 */
7799 freefile = malloc(sizeof(struct freefile),
7800 M_FREEFILE, M_SOFTDEP_FLAGS);
7801 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7802 freefile->fx_mode = mode;
7803 freefile->fx_oldinum = ino;
7804 freefile->fx_devvp = ump->um_devvp;
7805 LIST_INIT(&freefile->fx_jwork);
7806 UFS_LOCK(ump);
7807 ump->um_fs->fs_pendinginodes += 1;
7808 UFS_UNLOCK(ump);
7809
7810 /*
7811 * If the inodedep does not exist, then the zero'ed inode has
7812 * been written to disk. If the allocated inode has never been
7813 * written to disk, then the on-disk inode is zero'ed. In either
7814 * case we can free the file immediately. If the journal was
7815 * canceled before being written the inode will never make it to
7816 * disk and we must send the canceled journal entrys to
7817 * ffs_freefile() to be cleared in conjunction with the bitmap.
7818 * Any blocks waiting on the inode to write can be safely freed
7819 * here as it will never been written.
7820 */
7821 ACQUIRE_LOCK(ump);
7822 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7823 if (inodedep) {
7824 /*
7825 * Clear out freeblks that no longer need to reference
7826 * this inode.
7827 */
7828 while ((freeblks =
7829 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7830 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7831 fb_next);
7832 freeblks->fb_state &= ~ONDEPLIST;
7833 }
7834 /*
7835 * Remove this inode from the unlinked list.
7836 */
7837 if (inodedep->id_state & UNLINKED) {
7838 /*
7839 * Save the journal work to be freed with the bitmap
7840 * before we clear UNLINKED. Otherwise it can be lost
7841 * if the inode block is written.
7842 */
7843 handle_bufwait(inodedep, &freefile->fx_jwork);
7844 clear_unlinked_inodedep(inodedep);
7845 /*
7846 * Re-acquire inodedep as we've dropped the
7847 * per-filesystem lock in clear_unlinked_inodedep().
7848 */
7849 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7850 }
7851 }
7852 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7853 FREE_LOCK(ump);
7854 handle_workitem_freefile(freefile);
7855 return;
7856 }
7857 if ((inodedep->id_state & DEPCOMPLETE) == 0)
7858 inodedep->id_state |= GOINGAWAY;
7859 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7860 FREE_LOCK(ump);
7861 if (ip->i_number == ino)
7862 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
7863 }
7864
7865 /*
7866 * Check to see if an inode has never been written to disk. If
7867 * so free the inodedep and return success, otherwise return failure.
7868 *
7869 * If we still have a bitmap dependency, then the inode has never
7870 * been written to disk. Drop the dependency as it is no longer
7871 * necessary since the inode is being deallocated. We set the
7872 * ALLCOMPLETE flags since the bitmap now properly shows that the
7873 * inode is not allocated. Even if the inode is actively being
7874 * written, it has been rolled back to its zero'ed state, so we
7875 * are ensured that a zero inode is what is on the disk. For short
7876 * lived files, this change will usually result in removing all the
7877 * dependencies from the inode so that it can be freed immediately.
7878 */
7879 static int
check_inode_unwritten(struct inodedep * inodedep)7880 check_inode_unwritten(struct inodedep *inodedep)
7881 {
7882
7883 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7884
7885 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
7886 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7887 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7888 !LIST_EMPTY(&inodedep->id_bufwait) ||
7889 !LIST_EMPTY(&inodedep->id_inowait) ||
7890 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7891 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7892 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7893 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7894 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7895 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7896 inodedep->id_mkdiradd != NULL ||
7897 inodedep->id_nlinkdelta != 0)
7898 return (0);
7899 /*
7900 * Another process might be in initiate_write_inodeblock_ufs[12]
7901 * trying to allocate memory without holding "Softdep Lock".
7902 */
7903 if ((inodedep->id_state & IOSTARTED) != 0 &&
7904 inodedep->id_savedino1 == NULL)
7905 return (0);
7906
7907 if (inodedep->id_state & ONDEPLIST)
7908 LIST_REMOVE(inodedep, id_deps);
7909 inodedep->id_state &= ~ONDEPLIST;
7910 inodedep->id_state |= ALLCOMPLETE;
7911 inodedep->id_bmsafemap = NULL;
7912 if (inodedep->id_state & ONWORKLIST)
7913 WORKLIST_REMOVE(&inodedep->id_list);
7914 if (inodedep->id_savedino1 != NULL) {
7915 free(inodedep->id_savedino1, M_SAVEDINO);
7916 inodedep->id_savedino1 = NULL;
7917 }
7918 if (free_inodedep(inodedep) == 0)
7919 panic("check_inode_unwritten: busy inode");
7920 return (1);
7921 }
7922
7923 static int
check_inodedep_free(struct inodedep * inodedep)7924 check_inodedep_free(struct inodedep *inodedep)
7925 {
7926
7927 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7928 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
7929 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7930 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7931 !LIST_EMPTY(&inodedep->id_bufwait) ||
7932 !LIST_EMPTY(&inodedep->id_inowait) ||
7933 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7934 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7935 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7936 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7937 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7938 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7939 inodedep->id_mkdiradd != NULL ||
7940 inodedep->id_nlinkdelta != 0 ||
7941 inodedep->id_savedino1 != NULL)
7942 return (0);
7943 return (1);
7944 }
7945
7946 /*
7947 * Try to free an inodedep structure. Return 1 if it could be freed.
7948 */
7949 static int
free_inodedep(struct inodedep * inodedep)7950 free_inodedep(struct inodedep *inodedep)
7951 {
7952
7953 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7954 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7955 !check_inodedep_free(inodedep))
7956 return (0);
7957 if (inodedep->id_state & ONDEPLIST)
7958 LIST_REMOVE(inodedep, id_deps);
7959 LIST_REMOVE(inodedep, id_hash);
7960 WORKITEM_FREE(inodedep, D_INODEDEP);
7961 return (1);
7962 }
7963
7964 /*
7965 * Free the block referenced by a freework structure. The parent freeblks
7966 * structure is released and completed when the final cg bitmap reaches
7967 * the disk. This routine may be freeing a jnewblk which never made it to
7968 * disk in which case we do not have to wait as the operation is undone
7969 * in memory immediately.
7970 */
7971 static void
freework_freeblock(struct freework * freework,uint64_t key)7972 freework_freeblock(struct freework *freework, uint64_t key)
7973 {
7974 struct freeblks *freeblks;
7975 struct jnewblk *jnewblk;
7976 struct ufsmount *ump;
7977 struct workhead wkhd;
7978 struct fs *fs;
7979 int bsize;
7980 int needj;
7981
7982 ump = VFSTOUFS(freework->fw_list.wk_mp);
7983 LOCK_OWNED(ump);
7984 /*
7985 * Handle partial truncate separately.
7986 */
7987 if (freework->fw_indir) {
7988 complete_trunc_indir(freework);
7989 return;
7990 }
7991 freeblks = freework->fw_freeblks;
7992 fs = ump->um_fs;
7993 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7994 bsize = lfragtosize(fs, freework->fw_frags);
7995 LIST_INIT(&wkhd);
7996 /*
7997 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7998 * on the indirblk hashtable and prevents premature freeing.
7999 */
8000 freework->fw_state |= DEPCOMPLETE;
8001 /*
8002 * SUJ needs to wait for the segment referencing freed indirect
8003 * blocks to expire so that we know the checker will not confuse
8004 * a re-allocated indirect block with its old contents.
8005 */
8006 if (needj && freework->fw_lbn <= -UFS_NDADDR)
8007 indirblk_insert(freework);
8008 /*
8009 * If we are canceling an existing jnewblk pass it to the free
8010 * routine, otherwise pass the freeblk which will ultimately
8011 * release the freeblks. If we're not journaling, we can just
8012 * free the freeblks immediately.
8013 */
8014 jnewblk = freework->fw_jnewblk;
8015 if (jnewblk != NULL) {
8016 cancel_jnewblk(jnewblk, &wkhd);
8017 needj = 0;
8018 } else if (needj) {
8019 freework->fw_state |= DELAYEDFREE;
8020 freeblks->fb_cgwait++;
8021 WORKLIST_INSERT(&wkhd, &freework->fw_list);
8022 }
8023 FREE_LOCK(ump);
8024 freeblks_free(ump, freeblks, btodb(bsize));
8025 CTR4(KTR_SUJ,
8026 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
8027 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
8028 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
8029 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
8030 ACQUIRE_LOCK(ump);
8031 /*
8032 * The jnewblk will be discarded and the bits in the map never
8033 * made it to disk. We can immediately free the freeblk.
8034 */
8035 if (needj == 0)
8036 handle_written_freework(freework);
8037 }
8038
8039 /*
8040 * We enqueue freework items that need processing back on the freeblks and
8041 * add the freeblks to the worklist. This makes it easier to find all work
8042 * required to flush a truncation in process_truncates().
8043 */
8044 static void
freework_enqueue(struct freework * freework)8045 freework_enqueue(struct freework *freework)
8046 {
8047 struct freeblks *freeblks;
8048
8049 freeblks = freework->fw_freeblks;
8050 if ((freework->fw_state & INPROGRESS) == 0)
8051 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
8052 if ((freeblks->fb_state &
8053 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
8054 LIST_EMPTY(&freeblks->fb_jblkdephd))
8055 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8056 }
8057
8058 /*
8059 * Start, continue, or finish the process of freeing an indirect block tree.
8060 * The free operation may be paused at any point with fw_off containing the
8061 * offset to restart from. This enables us to implement some flow control
8062 * for large truncates which may fan out and generate a huge number of
8063 * dependencies.
8064 */
8065 static void
handle_workitem_indirblk(struct freework * freework)8066 handle_workitem_indirblk(struct freework *freework)
8067 {
8068 struct freeblks *freeblks;
8069 struct ufsmount *ump;
8070 struct fs *fs;
8071
8072 freeblks = freework->fw_freeblks;
8073 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8074 fs = ump->um_fs;
8075 if (freework->fw_state & DEPCOMPLETE) {
8076 handle_written_freework(freework);
8077 return;
8078 }
8079 if (freework->fw_off == NINDIR(fs)) {
8080 freework_freeblock(freework, SINGLETON_KEY);
8081 return;
8082 }
8083 freework->fw_state |= INPROGRESS;
8084 FREE_LOCK(ump);
8085 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
8086 freework->fw_lbn);
8087 ACQUIRE_LOCK(ump);
8088 }
8089
8090 /*
8091 * Called when a freework structure attached to a cg buf is written. The
8092 * ref on either the parent or the freeblks structure is released and
8093 * the freeblks is added back to the worklist if there is more work to do.
8094 */
8095 static void
handle_written_freework(struct freework * freework)8096 handle_written_freework(struct freework *freework)
8097 {
8098 struct freeblks *freeblks;
8099 struct freework *parent;
8100
8101 freeblks = freework->fw_freeblks;
8102 parent = freework->fw_parent;
8103 if (freework->fw_state & DELAYEDFREE)
8104 freeblks->fb_cgwait--;
8105 freework->fw_state |= COMPLETE;
8106 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
8107 WORKITEM_FREE(freework, D_FREEWORK);
8108 if (parent) {
8109 if (--parent->fw_ref == 0)
8110 freework_enqueue(parent);
8111 return;
8112 }
8113 if (--freeblks->fb_ref != 0)
8114 return;
8115 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
8116 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
8117 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8118 }
8119
8120 /*
8121 * This workitem routine performs the block de-allocation.
8122 * The workitem is added to the pending list after the updated
8123 * inode block has been written to disk. As mentioned above,
8124 * checks regarding the number of blocks de-allocated (compared
8125 * to the number of blocks allocated for the file) are also
8126 * performed in this function.
8127 */
8128 static int
handle_workitem_freeblocks(struct freeblks * freeblks,int flags)8129 handle_workitem_freeblocks(struct freeblks *freeblks, int flags)
8130 {
8131 struct freework *freework;
8132 struct newblk *newblk;
8133 struct allocindir *aip;
8134 struct ufsmount *ump;
8135 struct worklist *wk;
8136 uint64_t key;
8137
8138 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
8139 ("handle_workitem_freeblocks: Journal entries not written."));
8140 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8141 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8142 ACQUIRE_LOCK(ump);
8143 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
8144 WORKLIST_REMOVE(wk);
8145 switch (wk->wk_type) {
8146 case D_DIRREM:
8147 wk->wk_state |= COMPLETE;
8148 add_to_worklist(wk, 0);
8149 continue;
8150
8151 case D_ALLOCDIRECT:
8152 free_newblk(WK_NEWBLK(wk));
8153 continue;
8154
8155 case D_ALLOCINDIR:
8156 aip = WK_ALLOCINDIR(wk);
8157 freework = NULL;
8158 if (aip->ai_state & DELAYEDFREE) {
8159 FREE_LOCK(ump);
8160 freework = newfreework(ump, freeblks, NULL,
8161 aip->ai_lbn, aip->ai_newblkno,
8162 ump->um_fs->fs_frag, 0, 0);
8163 ACQUIRE_LOCK(ump);
8164 }
8165 newblk = WK_NEWBLK(wk);
8166 if (newblk->nb_jnewblk) {
8167 freework->fw_jnewblk = newblk->nb_jnewblk;
8168 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
8169 newblk->nb_jnewblk = NULL;
8170 }
8171 free_newblk(newblk);
8172 continue;
8173
8174 case D_FREEWORK:
8175 freework = WK_FREEWORK(wk);
8176 if (freework->fw_lbn <= -UFS_NDADDR)
8177 handle_workitem_indirblk(freework);
8178 else
8179 freework_freeblock(freework, key);
8180 continue;
8181 default:
8182 panic("handle_workitem_freeblocks: Unknown type %s",
8183 TYPENAME(wk->wk_type));
8184 }
8185 }
8186 if (freeblks->fb_ref != 0) {
8187 freeblks->fb_state &= ~INPROGRESS;
8188 wake_worklist(&freeblks->fb_list);
8189 freeblks = NULL;
8190 }
8191 FREE_LOCK(ump);
8192 ffs_blkrelease_finish(ump, key);
8193 if (freeblks)
8194 return handle_complete_freeblocks(freeblks, flags);
8195 return (0);
8196 }
8197
8198 /*
8199 * Handle completion of block free via truncate. This allows fs_pending
8200 * to track the actual free block count more closely than if we only updated
8201 * it at the end. We must be careful to handle cases where the block count
8202 * on free was incorrect.
8203 */
8204 static void
freeblks_free(struct ufsmount * ump,struct freeblks * freeblks,int blocks)8205 freeblks_free(struct ufsmount *ump,
8206 struct freeblks *freeblks,
8207 int blocks)
8208 {
8209 struct fs *fs;
8210 ufs2_daddr_t remain;
8211
8212 UFS_LOCK(ump);
8213 remain = -freeblks->fb_chkcnt;
8214 freeblks->fb_chkcnt += blocks;
8215 if (remain > 0) {
8216 if (remain < blocks)
8217 blocks = remain;
8218 fs = ump->um_fs;
8219 fs->fs_pendingblocks -= blocks;
8220 }
8221 UFS_UNLOCK(ump);
8222 }
8223
8224 /*
8225 * Once all of the freework workitems are complete we can retire the
8226 * freeblocks dependency and any journal work awaiting completion. This
8227 * can not be called until all other dependencies are stable on disk.
8228 */
8229 static int
handle_complete_freeblocks(struct freeblks * freeblks,int flags)8230 handle_complete_freeblocks(struct freeblks *freeblks, int flags)
8231 {
8232 struct inodedep *inodedep;
8233 struct inode *ip;
8234 struct vnode *vp;
8235 struct fs *fs;
8236 struct ufsmount *ump;
8237 ufs2_daddr_t spare;
8238
8239 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8240 fs = ump->um_fs;
8241 flags = LK_EXCLUSIVE | flags;
8242 spare = freeblks->fb_chkcnt;
8243
8244 /*
8245 * If we did not release the expected number of blocks we may have
8246 * to adjust the inode block count here. Only do so if it wasn't
8247 * a truncation to zero and the modrev still matches.
8248 */
8249 if (spare && freeblks->fb_len != 0) {
8250 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8251 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0)
8252 return (EBUSY);
8253 ip = VTOI(vp);
8254 if (ip->i_mode == 0) {
8255 vgone(vp);
8256 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8257 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8258 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8259 /*
8260 * We must wait so this happens before the
8261 * journal is reclaimed.
8262 */
8263 ffs_update(vp, 1);
8264 }
8265 vput(vp);
8266 }
8267 if (spare < 0) {
8268 UFS_LOCK(ump);
8269 fs->fs_pendingblocks += spare;
8270 UFS_UNLOCK(ump);
8271 }
8272 #ifdef QUOTA
8273 /* Handle spare. */
8274 if (spare)
8275 quotaadj(freeblks->fb_quota, ump, -spare);
8276 quotarele(freeblks->fb_quota);
8277 #endif
8278 ACQUIRE_LOCK(ump);
8279 if (freeblks->fb_state & ONDEPLIST) {
8280 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8281 0, &inodedep);
8282 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8283 freeblks->fb_state &= ~ONDEPLIST;
8284 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8285 free_inodedep(inodedep);
8286 }
8287 /*
8288 * All of the freeblock deps must be complete prior to this call
8289 * so it's now safe to complete earlier outstanding journal entries.
8290 */
8291 handle_jwork(&freeblks->fb_jwork);
8292 WORKITEM_FREE(freeblks, D_FREEBLKS);
8293 FREE_LOCK(ump);
8294 return (0);
8295 }
8296
8297 /*
8298 * Release blocks associated with the freeblks and stored in the indirect
8299 * block dbn. If level is greater than SINGLE, the block is an indirect block
8300 * and recursive calls to indirtrunc must be used to cleanse other indirect
8301 * blocks.
8302 *
8303 * This handles partial and complete truncation of blocks. Partial is noted
8304 * with goingaway == 0. In this case the freework is completed after the
8305 * zero'd indirects are written to disk. For full truncation the freework
8306 * is completed after the block is freed.
8307 */
8308 static void
indir_trunc(struct freework * freework,ufs2_daddr_t dbn,ufs_lbn_t lbn)8309 indir_trunc(struct freework *freework,
8310 ufs2_daddr_t dbn,
8311 ufs_lbn_t lbn)
8312 {
8313 struct freework *nfreework;
8314 struct workhead wkhd;
8315 struct freeblks *freeblks;
8316 struct buf *bp;
8317 struct fs *fs;
8318 struct indirdep *indirdep;
8319 struct mount *mp;
8320 struct ufsmount *ump;
8321 ufs1_daddr_t *bap1;
8322 ufs2_daddr_t nb, nnb, *bap2;
8323 ufs_lbn_t lbnadd, nlbn;
8324 uint64_t key;
8325 int nblocks, ufs1fmt, freedblocks;
8326 int goingaway, freedeps, needj, level, cnt, i, error;
8327
8328 freeblks = freework->fw_freeblks;
8329 mp = freeblks->fb_list.wk_mp;
8330 ump = VFSTOUFS(mp);
8331 fs = ump->um_fs;
8332 /*
8333 * Get buffer of block pointers to be freed. There are three cases:
8334 *
8335 * 1) Partial truncate caches the indirdep pointer in the freework
8336 * which provides us a back copy to the save bp which holds the
8337 * pointers we want to clear. When this completes the zero
8338 * pointers are written to the real copy.
8339 * 2) The indirect is being completely truncated, cancel_indirdep()
8340 * eliminated the real copy and placed the indirdep on the saved
8341 * copy. The indirdep and buf are discarded when this completes.
8342 * 3) The indirect was not in memory, we read a copy off of the disk
8343 * using the devvp and drop and invalidate the buffer when we're
8344 * done.
8345 */
8346 goingaway = 1;
8347 indirdep = NULL;
8348 if (freework->fw_indir != NULL) {
8349 goingaway = 0;
8350 indirdep = freework->fw_indir;
8351 bp = indirdep->ir_savebp;
8352 if (bp == NULL || bp->b_blkno != dbn)
8353 panic("indir_trunc: Bad saved buf %p blkno %jd",
8354 bp, (intmax_t)dbn);
8355 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8356 /*
8357 * The lock prevents the buf dep list from changing and
8358 * indirects on devvp should only ever have one dependency.
8359 */
8360 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8361 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8362 panic("indir_trunc: Bad indirdep %p from buf %p",
8363 indirdep, bp);
8364 } else {
8365 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn,
8366 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
8367 if (error)
8368 return;
8369 }
8370 ACQUIRE_LOCK(ump);
8371 /* Protects against a race with complete_trunc_indir(). */
8372 freework->fw_state &= ~INPROGRESS;
8373 /*
8374 * If we have an indirdep we need to enforce the truncation order
8375 * and discard it when it is complete.
8376 */
8377 if (indirdep) {
8378 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8379 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8380 /*
8381 * Add the complete truncate to the list on the
8382 * indirdep to enforce in-order processing.
8383 */
8384 if (freework->fw_indir == NULL)
8385 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8386 freework, fw_next);
8387 FREE_LOCK(ump);
8388 return;
8389 }
8390 /*
8391 * If we're goingaway, free the indirdep. Otherwise it will
8392 * linger until the write completes.
8393 */
8394 if (goingaway) {
8395 KASSERT(indirdep->ir_savebp == bp,
8396 ("indir_trunc: losing ir_savebp %p",
8397 indirdep->ir_savebp));
8398 indirdep->ir_savebp = NULL;
8399 free_indirdep(indirdep);
8400 }
8401 }
8402 FREE_LOCK(ump);
8403 /* Initialize pointers depending on block size. */
8404 if (ump->um_fstype == UFS1) {
8405 bap1 = (ufs1_daddr_t *)bp->b_data;
8406 nb = bap1[freework->fw_off];
8407 ufs1fmt = 1;
8408 bap2 = NULL;
8409 } else {
8410 bap2 = (ufs2_daddr_t *)bp->b_data;
8411 nb = bap2[freework->fw_off];
8412 ufs1fmt = 0;
8413 bap1 = NULL;
8414 }
8415 level = lbn_level(lbn);
8416 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8417 lbnadd = lbn_offset(fs, level);
8418 nblocks = btodb(fs->fs_bsize);
8419 nfreework = freework;
8420 freedeps = 0;
8421 cnt = 0;
8422 /*
8423 * Reclaim blocks. Traverses into nested indirect levels and
8424 * arranges for the current level to be freed when subordinates
8425 * are free when journaling.
8426 */
8427 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8428 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8429 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8430 fs->fs_bsize) != 0)
8431 nb = 0;
8432 if (i != NINDIR(fs) - 1) {
8433 if (ufs1fmt)
8434 nnb = bap1[i+1];
8435 else
8436 nnb = bap2[i+1];
8437 } else
8438 nnb = 0;
8439 if (nb == 0)
8440 continue;
8441 cnt++;
8442 if (level != 0) {
8443 nlbn = (lbn + 1) - (i * lbnadd);
8444 if (needj != 0) {
8445 nfreework = newfreework(ump, freeblks, freework,
8446 nlbn, nb, fs->fs_frag, 0, 0);
8447 freedeps++;
8448 }
8449 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8450 } else {
8451 struct freedep *freedep;
8452
8453 /*
8454 * Attempt to aggregate freedep dependencies for
8455 * all blocks being released to the same CG.
8456 */
8457 LIST_INIT(&wkhd);
8458 if (needj != 0 &&
8459 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8460 freedep = newfreedep(freework);
8461 WORKLIST_INSERT_UNLOCKED(&wkhd,
8462 &freedep->fd_list);
8463 freedeps++;
8464 }
8465 CTR3(KTR_SUJ,
8466 "indir_trunc: ino %jd blkno %jd size %d",
8467 freeblks->fb_inum, nb, fs->fs_bsize);
8468 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8469 fs->fs_bsize, freeblks->fb_inum,
8470 freeblks->fb_vtype, &wkhd, key);
8471 }
8472 }
8473 ffs_blkrelease_finish(ump, key);
8474 if (goingaway) {
8475 bp->b_flags |= B_INVAL | B_NOCACHE;
8476 brelse(bp);
8477 }
8478 freedblocks = 0;
8479 if (level == 0)
8480 freedblocks = (nblocks * cnt);
8481 if (needj == 0)
8482 freedblocks += nblocks;
8483 freeblks_free(ump, freeblks, freedblocks);
8484 /*
8485 * If we are journaling set up the ref counts and offset so this
8486 * indirect can be completed when its children are free.
8487 */
8488 if (needj) {
8489 ACQUIRE_LOCK(ump);
8490 freework->fw_off = i;
8491 freework->fw_ref += freedeps;
8492 freework->fw_ref -= NINDIR(fs) + 1;
8493 if (level == 0)
8494 freeblks->fb_cgwait += freedeps;
8495 if (freework->fw_ref == 0)
8496 freework_freeblock(freework, SINGLETON_KEY);
8497 FREE_LOCK(ump);
8498 return;
8499 }
8500 /*
8501 * If we're not journaling we can free the indirect now.
8502 */
8503 dbn = dbtofsb(fs, dbn);
8504 CTR3(KTR_SUJ,
8505 "indir_trunc 2: ino %jd blkno %jd size %d",
8506 freeblks->fb_inum, dbn, fs->fs_bsize);
8507 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8508 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8509 /* Non SUJ softdep does single-threaded truncations. */
8510 if (freework->fw_blkno == dbn) {
8511 freework->fw_state |= ALLCOMPLETE;
8512 ACQUIRE_LOCK(ump);
8513 handle_written_freework(freework);
8514 FREE_LOCK(ump);
8515 }
8516 return;
8517 }
8518
8519 /*
8520 * Cancel an allocindir when it is removed via truncation. When bp is not
8521 * NULL the indirect never appeared on disk and is scheduled to be freed
8522 * independently of the indir so we can more easily track journal work.
8523 */
8524 static void
cancel_allocindir(struct allocindir * aip,struct buf * bp,struct freeblks * freeblks,int trunc)8525 cancel_allocindir(
8526 struct allocindir *aip,
8527 struct buf *bp,
8528 struct freeblks *freeblks,
8529 int trunc)
8530 {
8531 struct indirdep *indirdep;
8532 struct freefrag *freefrag;
8533 struct newblk *newblk;
8534
8535 newblk = (struct newblk *)aip;
8536 LIST_REMOVE(aip, ai_next);
8537 /*
8538 * We must eliminate the pointer in bp if it must be freed on its
8539 * own due to partial truncate or pending journal work.
8540 */
8541 if (bp && (trunc || newblk->nb_jnewblk)) {
8542 /*
8543 * Clear the pointer and mark the aip to be freed
8544 * directly if it never existed on disk.
8545 */
8546 aip->ai_state |= DELAYEDFREE;
8547 indirdep = aip->ai_indirdep;
8548 if (indirdep->ir_state & UFS1FMT)
8549 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8550 else
8551 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8552 }
8553 /*
8554 * When truncating the previous pointer will be freed via
8555 * savedbp. Eliminate the freefrag which would dup free.
8556 */
8557 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8558 newblk->nb_freefrag = NULL;
8559 if (freefrag->ff_jdep)
8560 cancel_jfreefrag(
8561 WK_JFREEFRAG(freefrag->ff_jdep));
8562 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8563 WORKITEM_FREE(freefrag, D_FREEFRAG);
8564 }
8565 /*
8566 * If the journal hasn't been written the jnewblk must be passed
8567 * to the call to ffs_blkfree that reclaims the space. We accomplish
8568 * this by leaving the journal dependency on the newblk to be freed
8569 * when a freework is created in handle_workitem_freeblocks().
8570 */
8571 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8572 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8573 }
8574
8575 /*
8576 * Create the mkdir dependencies for . and .. in a new directory. Link them
8577 * in to a newdirblk so any subsequent additions are tracked properly. The
8578 * caller is responsible for adding the mkdir1 dependency to the journal
8579 * and updating id_mkdiradd. This function returns with the per-filesystem
8580 * lock held.
8581 */
8582 static struct mkdir *
setup_newdir(struct diradd * dap,ino_t newinum,ino_t dinum,struct buf * newdirbp,struct mkdir ** mkdirp)8583 setup_newdir(
8584 struct diradd *dap,
8585 ino_t newinum,
8586 ino_t dinum,
8587 struct buf *newdirbp,
8588 struct mkdir **mkdirp)
8589 {
8590 struct newblk *newblk;
8591 struct pagedep *pagedep;
8592 struct inodedep *inodedep;
8593 struct newdirblk *newdirblk;
8594 struct mkdir *mkdir1, *mkdir2;
8595 struct worklist *wk;
8596 struct jaddref *jaddref;
8597 struct ufsmount *ump;
8598 struct mount *mp;
8599
8600 mp = dap->da_list.wk_mp;
8601 ump = VFSTOUFS(mp);
8602 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8603 M_SOFTDEP_FLAGS);
8604 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8605 LIST_INIT(&newdirblk->db_mkdir);
8606 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8607 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8608 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8609 mkdir1->md_diradd = dap;
8610 mkdir1->md_jaddref = NULL;
8611 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8612 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8613 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8614 mkdir2->md_diradd = dap;
8615 mkdir2->md_jaddref = NULL;
8616 if (MOUNTEDSUJ(mp) == 0) {
8617 mkdir1->md_state |= DEPCOMPLETE;
8618 mkdir2->md_state |= DEPCOMPLETE;
8619 }
8620 /*
8621 * Dependency on "." and ".." being written to disk.
8622 */
8623 mkdir1->md_buf = newdirbp;
8624 ACQUIRE_LOCK(VFSTOUFS(mp));
8625 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8626 /*
8627 * We must link the pagedep, allocdirect, and newdirblk for
8628 * the initial file page so the pointer to the new directory
8629 * is not written until the directory contents are live and
8630 * any subsequent additions are not marked live until the
8631 * block is reachable via the inode.
8632 */
8633 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8634 panic("setup_newdir: lost pagedep");
8635 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8636 if (wk->wk_type == D_ALLOCDIRECT)
8637 break;
8638 if (wk == NULL)
8639 panic("setup_newdir: lost allocdirect");
8640 if (pagedep->pd_state & NEWBLOCK)
8641 panic("setup_newdir: NEWBLOCK already set");
8642 newblk = WK_NEWBLK(wk);
8643 pagedep->pd_state |= NEWBLOCK;
8644 pagedep->pd_newdirblk = newdirblk;
8645 newdirblk->db_pagedep = pagedep;
8646 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8647 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8648 /*
8649 * Look up the inodedep for the parent directory so that we
8650 * can link mkdir2 into the pending dotdot jaddref or
8651 * the inode write if there is none. If the inode is
8652 * ALLCOMPLETE and no jaddref is present all dependencies have
8653 * been satisfied and mkdir2 can be freed.
8654 */
8655 inodedep_lookup(mp, dinum, 0, &inodedep);
8656 if (MOUNTEDSUJ(mp)) {
8657 if (inodedep == NULL)
8658 panic("setup_newdir: Lost parent.");
8659 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8660 inoreflst);
8661 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8662 (jaddref->ja_state & MKDIR_PARENT),
8663 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8664 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8665 mkdir2->md_jaddref = jaddref;
8666 jaddref->ja_mkdir = mkdir2;
8667 } else if (inodedep == NULL ||
8668 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8669 dap->da_state &= ~MKDIR_PARENT;
8670 WORKITEM_FREE(mkdir2, D_MKDIR);
8671 mkdir2 = NULL;
8672 } else {
8673 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8674 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8675 }
8676 *mkdirp = mkdir2;
8677
8678 return (mkdir1);
8679 }
8680
8681 /*
8682 * Directory entry addition dependencies.
8683 *
8684 * When adding a new directory entry, the inode (with its incremented link
8685 * count) must be written to disk before the directory entry's pointer to it.
8686 * Also, if the inode is newly allocated, the corresponding freemap must be
8687 * updated (on disk) before the directory entry's pointer. These requirements
8688 * are met via undo/redo on the directory entry's pointer, which consists
8689 * simply of the inode number.
8690 *
8691 * As directory entries are added and deleted, the free space within a
8692 * directory block can become fragmented. The ufs filesystem will compact
8693 * a fragmented directory block to make space for a new entry. When this
8694 * occurs, the offsets of previously added entries change. Any "diradd"
8695 * dependency structures corresponding to these entries must be updated with
8696 * the new offsets.
8697 */
8698
8699 /*
8700 * This routine is called after the in-memory inode's link
8701 * count has been incremented, but before the directory entry's
8702 * pointer to the inode has been set.
8703 */
8704 int
softdep_setup_directory_add(struct buf * bp,struct inode * dp,off_t diroffset,ino_t newinum,struct buf * newdirbp,int isnewblk)8705 softdep_setup_directory_add(
8706 struct buf *bp, /* buffer containing directory block */
8707 struct inode *dp, /* inode for directory */
8708 off_t diroffset, /* offset of new entry in directory */
8709 ino_t newinum, /* inode referenced by new directory entry */
8710 struct buf *newdirbp, /* non-NULL => contents of new mkdir */
8711 int isnewblk) /* entry is in a newly allocated block */
8712 {
8713 int offset; /* offset of new entry within directory block */
8714 ufs_lbn_t lbn; /* block in directory containing new entry */
8715 struct fs *fs;
8716 struct diradd *dap;
8717 struct newblk *newblk;
8718 struct pagedep *pagedep;
8719 struct inodedep *inodedep;
8720 struct newdirblk *newdirblk;
8721 struct mkdir *mkdir1, *mkdir2;
8722 struct jaddref *jaddref;
8723 struct ufsmount *ump;
8724 struct mount *mp;
8725 int isindir;
8726
8727 mp = ITOVFS(dp);
8728 ump = VFSTOUFS(mp);
8729 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8730 ("softdep_setup_directory_add called on non-softdep filesystem"));
8731 /*
8732 * Whiteouts have no dependencies.
8733 */
8734 if (newinum == UFS_WINO) {
8735 if (newdirbp != NULL)
8736 bdwrite(newdirbp);
8737 return (0);
8738 }
8739 jaddref = NULL;
8740 mkdir1 = mkdir2 = NULL;
8741 fs = ump->um_fs;
8742 lbn = lblkno(fs, diroffset);
8743 offset = blkoff(fs, diroffset);
8744 dap = malloc(sizeof(struct diradd), M_DIRADD,
8745 M_SOFTDEP_FLAGS|M_ZERO);
8746 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8747 dap->da_offset = offset;
8748 dap->da_newinum = newinum;
8749 dap->da_state = ATTACHED;
8750 LIST_INIT(&dap->da_jwork);
8751 isindir = bp->b_lblkno >= UFS_NDADDR;
8752 newdirblk = NULL;
8753 if (isnewblk &&
8754 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8755 newdirblk = malloc(sizeof(struct newdirblk),
8756 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8757 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8758 LIST_INIT(&newdirblk->db_mkdir);
8759 }
8760 /*
8761 * If we're creating a new directory setup the dependencies and set
8762 * the dap state to wait for them. Otherwise it's COMPLETE and
8763 * we can move on.
8764 */
8765 if (newdirbp == NULL) {
8766 dap->da_state |= DEPCOMPLETE;
8767 ACQUIRE_LOCK(ump);
8768 } else {
8769 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8770 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8771 &mkdir2);
8772 }
8773 /*
8774 * Link into parent directory pagedep to await its being written.
8775 */
8776 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8777 #ifdef INVARIANTS
8778 if (diradd_lookup(pagedep, offset) != NULL)
8779 panic("softdep_setup_directory_add: %p already at off %d\n",
8780 diradd_lookup(pagedep, offset), offset);
8781 #endif
8782 dap->da_pagedep = pagedep;
8783 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8784 da_pdlist);
8785 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8786 /*
8787 * If we're journaling, link the diradd into the jaddref so it
8788 * may be completed after the journal entry is written. Otherwise,
8789 * link the diradd into its inodedep. If the inode is not yet
8790 * written place it on the bufwait list, otherwise do the post-inode
8791 * write processing to put it on the id_pendinghd list.
8792 */
8793 if (MOUNTEDSUJ(mp)) {
8794 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8795 inoreflst);
8796 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8797 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8798 jaddref->ja_diroff = diroffset;
8799 jaddref->ja_diradd = dap;
8800 add_to_journal(&jaddref->ja_list);
8801 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8802 diradd_inode_written(dap, inodedep);
8803 else
8804 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8805 /*
8806 * Add the journal entries for . and .. links now that the primary
8807 * link is written.
8808 */
8809 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8810 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8811 inoreflst, if_deps);
8812 KASSERT(jaddref != NULL &&
8813 jaddref->ja_ino == jaddref->ja_parent &&
8814 (jaddref->ja_state & MKDIR_BODY),
8815 ("softdep_setup_directory_add: bad dot jaddref %p",
8816 jaddref));
8817 mkdir1->md_jaddref = jaddref;
8818 jaddref->ja_mkdir = mkdir1;
8819 /*
8820 * It is important that the dotdot journal entry
8821 * is added prior to the dot entry since dot writes
8822 * both the dot and dotdot links. These both must
8823 * be added after the primary link for the journal
8824 * to remain consistent.
8825 */
8826 add_to_journal(&mkdir2->md_jaddref->ja_list);
8827 add_to_journal(&jaddref->ja_list);
8828 }
8829 /*
8830 * If we are adding a new directory remember this diradd so that if
8831 * we rename it we can keep the dot and dotdot dependencies. If
8832 * we are adding a new name for an inode that has a mkdiradd we
8833 * must be in rename and we have to move the dot and dotdot
8834 * dependencies to this new name. The old name is being orphaned
8835 * soon.
8836 */
8837 if (mkdir1 != NULL) {
8838 if (inodedep->id_mkdiradd != NULL)
8839 panic("softdep_setup_directory_add: Existing mkdir");
8840 inodedep->id_mkdiradd = dap;
8841 } else if (inodedep->id_mkdiradd)
8842 merge_diradd(inodedep, dap);
8843 if (newdirblk != NULL) {
8844 /*
8845 * There is nothing to do if we are already tracking
8846 * this block.
8847 */
8848 if ((pagedep->pd_state & NEWBLOCK) != 0) {
8849 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8850 FREE_LOCK(ump);
8851 return (0);
8852 }
8853 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8854 == 0)
8855 panic("softdep_setup_directory_add: lost entry");
8856 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8857 pagedep->pd_state |= NEWBLOCK;
8858 pagedep->pd_newdirblk = newdirblk;
8859 newdirblk->db_pagedep = pagedep;
8860 FREE_LOCK(ump);
8861 /*
8862 * If we extended into an indirect signal direnter to sync.
8863 */
8864 if (isindir)
8865 return (1);
8866 return (0);
8867 }
8868 FREE_LOCK(ump);
8869 return (0);
8870 }
8871
8872 /*
8873 * This procedure is called to change the offset of a directory
8874 * entry when compacting a directory block which must be owned
8875 * exclusively by the caller. Note that the actual entry movement
8876 * must be done in this procedure to ensure that no I/O completions
8877 * occur while the move is in progress.
8878 */
8879 void
softdep_change_directoryentry_offset(struct buf * bp,struct inode * dp,caddr_t base,caddr_t oldloc,caddr_t newloc,int entrysize)8880 softdep_change_directoryentry_offset(
8881 struct buf *bp, /* Buffer holding directory block. */
8882 struct inode *dp, /* inode for directory */
8883 caddr_t base, /* address of dp->i_offset */
8884 caddr_t oldloc, /* address of old directory location */
8885 caddr_t newloc, /* address of new directory location */
8886 int entrysize) /* size of directory entry */
8887 {
8888 int offset, oldoffset, newoffset;
8889 struct pagedep *pagedep;
8890 struct jmvref *jmvref;
8891 struct diradd *dap;
8892 struct direct *de;
8893 struct mount *mp;
8894 struct ufsmount *ump;
8895 ufs_lbn_t lbn;
8896 int flags;
8897
8898 mp = ITOVFS(dp);
8899 ump = VFSTOUFS(mp);
8900 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8901 ("softdep_change_directoryentry_offset called on "
8902 "non-softdep filesystem"));
8903 de = (struct direct *)oldloc;
8904 jmvref = NULL;
8905 flags = 0;
8906 /*
8907 * Moves are always journaled as it would be too complex to
8908 * determine if any affected adds or removes are present in the
8909 * journal.
8910 */
8911 if (MOUNTEDSUJ(mp)) {
8912 flags = DEPALLOC;
8913 jmvref = newjmvref(dp, de->d_ino,
8914 I_OFFSET(dp) + (oldloc - base),
8915 I_OFFSET(dp) + (newloc - base));
8916 }
8917 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
8918 offset = blkoff(ump->um_fs, I_OFFSET(dp));
8919 oldoffset = offset + (oldloc - base);
8920 newoffset = offset + (newloc - base);
8921 ACQUIRE_LOCK(ump);
8922 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8923 goto done;
8924 dap = diradd_lookup(pagedep, oldoffset);
8925 if (dap) {
8926 dap->da_offset = newoffset;
8927 newoffset = DIRADDHASH(newoffset);
8928 oldoffset = DIRADDHASH(oldoffset);
8929 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8930 newoffset != oldoffset) {
8931 LIST_REMOVE(dap, da_pdlist);
8932 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8933 dap, da_pdlist);
8934 }
8935 }
8936 done:
8937 if (jmvref) {
8938 jmvref->jm_pagedep = pagedep;
8939 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8940 add_to_journal(&jmvref->jm_list);
8941 }
8942 bcopy(oldloc, newloc, entrysize);
8943 FREE_LOCK(ump);
8944 }
8945
8946 /*
8947 * Move the mkdir dependencies and journal work from one diradd to another
8948 * when renaming a directory. The new name must depend on the mkdir deps
8949 * completing as the old name did. Directories can only have one valid link
8950 * at a time so one must be canonical.
8951 */
8952 static void
merge_diradd(struct inodedep * inodedep,struct diradd * newdap)8953 merge_diradd(struct inodedep *inodedep, struct diradd *newdap)
8954 {
8955 struct diradd *olddap;
8956 struct mkdir *mkdir, *nextmd;
8957 struct ufsmount *ump;
8958 short state;
8959
8960 olddap = inodedep->id_mkdiradd;
8961 inodedep->id_mkdiradd = newdap;
8962 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8963 newdap->da_state &= ~DEPCOMPLETE;
8964 ump = VFSTOUFS(inodedep->id_list.wk_mp);
8965 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8966 mkdir = nextmd) {
8967 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8968 if (mkdir->md_diradd != olddap)
8969 continue;
8970 mkdir->md_diradd = newdap;
8971 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8972 newdap->da_state |= state;
8973 olddap->da_state &= ~state;
8974 if ((olddap->da_state &
8975 (MKDIR_PARENT | MKDIR_BODY)) == 0)
8976 break;
8977 }
8978 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8979 panic("merge_diradd: unfound ref");
8980 }
8981 /*
8982 * Any mkdir related journal items are not safe to be freed until
8983 * the new name is stable.
8984 */
8985 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8986 olddap->da_state |= DEPCOMPLETE;
8987 complete_diradd(olddap);
8988 }
8989
8990 /*
8991 * Move the diradd to the pending list when all diradd dependencies are
8992 * complete.
8993 */
8994 static void
complete_diradd(struct diradd * dap)8995 complete_diradd(struct diradd *dap)
8996 {
8997 struct pagedep *pagedep;
8998
8999 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
9000 if (dap->da_state & DIRCHG)
9001 pagedep = dap->da_previous->dm_pagedep;
9002 else
9003 pagedep = dap->da_pagedep;
9004 LIST_REMOVE(dap, da_pdlist);
9005 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9006 }
9007 }
9008
9009 /*
9010 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
9011 * add entries and conditionally journal the remove.
9012 */
9013 static void
cancel_diradd(struct diradd * dap,struct dirrem * dirrem,struct jremref * jremref,struct jremref * dotremref,struct jremref * dotdotremref)9014 cancel_diradd(
9015 struct diradd *dap,
9016 struct dirrem *dirrem,
9017 struct jremref *jremref,
9018 struct jremref *dotremref,
9019 struct jremref *dotdotremref)
9020 {
9021 struct inodedep *inodedep;
9022 struct jaddref *jaddref;
9023 struct inoref *inoref;
9024 struct ufsmount *ump;
9025 struct mkdir *mkdir;
9026
9027 /*
9028 * If no remove references were allocated we're on a non-journaled
9029 * filesystem and can skip the cancel step.
9030 */
9031 if (jremref == NULL) {
9032 free_diradd(dap, NULL);
9033 return;
9034 }
9035 /*
9036 * Cancel the primary name an free it if it does not require
9037 * journaling.
9038 */
9039 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
9040 0, &inodedep) != 0) {
9041 /* Abort the addref that reference this diradd. */
9042 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
9043 if (inoref->if_list.wk_type != D_JADDREF)
9044 continue;
9045 jaddref = (struct jaddref *)inoref;
9046 if (jaddref->ja_diradd != dap)
9047 continue;
9048 if (cancel_jaddref(jaddref, inodedep,
9049 &dirrem->dm_jwork) == 0) {
9050 free_jremref(jremref);
9051 jremref = NULL;
9052 }
9053 break;
9054 }
9055 }
9056 /*
9057 * Cancel subordinate names and free them if they do not require
9058 * journaling.
9059 */
9060 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9061 ump = VFSTOUFS(dap->da_list.wk_mp);
9062 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
9063 if (mkdir->md_diradd != dap)
9064 continue;
9065 if ((jaddref = mkdir->md_jaddref) == NULL)
9066 continue;
9067 mkdir->md_jaddref = NULL;
9068 if (mkdir->md_state & MKDIR_PARENT) {
9069 if (cancel_jaddref(jaddref, NULL,
9070 &dirrem->dm_jwork) == 0) {
9071 free_jremref(dotdotremref);
9072 dotdotremref = NULL;
9073 }
9074 } else {
9075 if (cancel_jaddref(jaddref, inodedep,
9076 &dirrem->dm_jwork) == 0) {
9077 free_jremref(dotremref);
9078 dotremref = NULL;
9079 }
9080 }
9081 }
9082 }
9083
9084 if (jremref)
9085 journal_jremref(dirrem, jremref, inodedep);
9086 if (dotremref)
9087 journal_jremref(dirrem, dotremref, inodedep);
9088 if (dotdotremref)
9089 journal_jremref(dirrem, dotdotremref, NULL);
9090 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
9091 free_diradd(dap, &dirrem->dm_jwork);
9092 }
9093
9094 /*
9095 * Free a diradd dependency structure.
9096 */
9097 static void
free_diradd(struct diradd * dap,struct workhead * wkhd)9098 free_diradd(struct diradd *dap, struct workhead *wkhd)
9099 {
9100 struct dirrem *dirrem;
9101 struct pagedep *pagedep;
9102 struct inodedep *inodedep;
9103 struct mkdir *mkdir, *nextmd;
9104 struct ufsmount *ump;
9105
9106 ump = VFSTOUFS(dap->da_list.wk_mp);
9107 LOCK_OWNED(ump);
9108 LIST_REMOVE(dap, da_pdlist);
9109 if (dap->da_state & ONWORKLIST)
9110 WORKLIST_REMOVE(&dap->da_list);
9111 if ((dap->da_state & DIRCHG) == 0) {
9112 pagedep = dap->da_pagedep;
9113 } else {
9114 dirrem = dap->da_previous;
9115 pagedep = dirrem->dm_pagedep;
9116 dirrem->dm_dirinum = pagedep->pd_ino;
9117 dirrem->dm_state |= COMPLETE;
9118 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9119 add_to_worklist(&dirrem->dm_list, 0);
9120 }
9121 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
9122 0, &inodedep) != 0)
9123 if (inodedep->id_mkdiradd == dap)
9124 inodedep->id_mkdiradd = NULL;
9125 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9126 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9127 mkdir = nextmd) {
9128 nextmd = LIST_NEXT(mkdir, md_mkdirs);
9129 if (mkdir->md_diradd != dap)
9130 continue;
9131 dap->da_state &=
9132 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
9133 LIST_REMOVE(mkdir, md_mkdirs);
9134 if (mkdir->md_state & ONWORKLIST)
9135 WORKLIST_REMOVE(&mkdir->md_list);
9136 if (mkdir->md_jaddref != NULL)
9137 panic("free_diradd: Unexpected jaddref");
9138 WORKITEM_FREE(mkdir, D_MKDIR);
9139 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
9140 break;
9141 }
9142 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9143 panic("free_diradd: unfound ref");
9144 }
9145 if (inodedep)
9146 free_inodedep(inodedep);
9147 /*
9148 * Free any journal segments waiting for the directory write.
9149 */
9150 handle_jwork(&dap->da_jwork);
9151 WORKITEM_FREE(dap, D_DIRADD);
9152 }
9153
9154 /*
9155 * Directory entry removal dependencies.
9156 *
9157 * When removing a directory entry, the entry's inode pointer must be
9158 * zero'ed on disk before the corresponding inode's link count is decremented
9159 * (possibly freeing the inode for re-use). This dependency is handled by
9160 * updating the directory entry but delaying the inode count reduction until
9161 * after the directory block has been written to disk. After this point, the
9162 * inode count can be decremented whenever it is convenient.
9163 */
9164
9165 /*
9166 * This routine should be called immediately after removing
9167 * a directory entry. The inode's link count should not be
9168 * decremented by the calling procedure -- the soft updates
9169 * code will do this task when it is safe.
9170 */
9171 void
softdep_setup_remove(struct buf * bp,struct inode * dp,struct inode * ip,int isrmdir)9172 softdep_setup_remove(
9173 struct buf *bp, /* buffer containing directory block */
9174 struct inode *dp, /* inode for the directory being modified */
9175 struct inode *ip, /* inode for directory entry being removed */
9176 int isrmdir) /* indicates if doing RMDIR */
9177 {
9178 struct dirrem *dirrem, *prevdirrem;
9179 struct inodedep *inodedep;
9180 struct ufsmount *ump;
9181 int direct;
9182
9183 ump = ITOUMP(ip);
9184 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9185 ("softdep_setup_remove called on non-softdep filesystem"));
9186 /*
9187 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
9188 * newdirrem() to setup the full directory remove which requires
9189 * isrmdir > 1.
9190 */
9191 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9192 /*
9193 * Add the dirrem to the inodedep's pending remove list for quick
9194 * discovery later.
9195 */
9196 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9197 panic("softdep_setup_remove: Lost inodedep.");
9198 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9199 dirrem->dm_state |= ONDEPLIST;
9200 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9201
9202 /*
9203 * If the COMPLETE flag is clear, then there were no active
9204 * entries and we want to roll back to a zeroed entry until
9205 * the new inode is committed to disk. If the COMPLETE flag is
9206 * set then we have deleted an entry that never made it to
9207 * disk. If the entry we deleted resulted from a name change,
9208 * then the old name still resides on disk. We cannot delete
9209 * its inode (returned to us in prevdirrem) until the zeroed
9210 * directory entry gets to disk. The new inode has never been
9211 * referenced on the disk, so can be deleted immediately.
9212 */
9213 if ((dirrem->dm_state & COMPLETE) == 0) {
9214 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9215 dm_next);
9216 FREE_LOCK(ump);
9217 } else {
9218 if (prevdirrem != NULL)
9219 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9220 prevdirrem, dm_next);
9221 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9222 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9223 FREE_LOCK(ump);
9224 if (direct)
9225 handle_workitem_remove(dirrem, 0);
9226 }
9227 }
9228
9229 /*
9230 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9231 * pd_pendinghd list of a pagedep.
9232 */
9233 static struct diradd *
diradd_lookup(struct pagedep * pagedep,int offset)9234 diradd_lookup(struct pagedep *pagedep, int offset)
9235 {
9236 struct diradd *dap;
9237
9238 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9239 if (dap->da_offset == offset)
9240 return (dap);
9241 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9242 if (dap->da_offset == offset)
9243 return (dap);
9244 return (NULL);
9245 }
9246
9247 /*
9248 * Search for a .. diradd dependency in a directory that is being removed.
9249 * If the directory was renamed to a new parent we have a diradd rather
9250 * than a mkdir for the .. entry. We need to cancel it now before
9251 * it is found in truncate().
9252 */
9253 static struct jremref *
cancel_diradd_dotdot(struct inode * ip,struct dirrem * dirrem,struct jremref * jremref)9254 cancel_diradd_dotdot(struct inode *ip,
9255 struct dirrem *dirrem,
9256 struct jremref *jremref)
9257 {
9258 struct pagedep *pagedep;
9259 struct diradd *dap;
9260 struct worklist *wk;
9261
9262 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9263 return (jremref);
9264 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9265 if (dap == NULL)
9266 return (jremref);
9267 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9268 /*
9269 * Mark any journal work as belonging to the parent so it is freed
9270 * with the .. reference.
9271 */
9272 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9273 wk->wk_state |= MKDIR_PARENT;
9274 return (NULL);
9275 }
9276
9277 /*
9278 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9279 * replace it with a dirrem/diradd pair as a result of re-parenting a
9280 * directory. This ensures that we don't simultaneously have a mkdir and
9281 * a diradd for the same .. entry.
9282 */
9283 static struct jremref *
cancel_mkdir_dotdot(struct inode * ip,struct dirrem * dirrem,struct jremref * jremref)9284 cancel_mkdir_dotdot(struct inode *ip,
9285 struct dirrem *dirrem,
9286 struct jremref *jremref)
9287 {
9288 struct inodedep *inodedep;
9289 struct jaddref *jaddref;
9290 struct ufsmount *ump;
9291 struct mkdir *mkdir;
9292 struct diradd *dap;
9293 struct mount *mp;
9294
9295 mp = ITOVFS(ip);
9296 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9297 return (jremref);
9298 dap = inodedep->id_mkdiradd;
9299 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9300 return (jremref);
9301 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9302 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9303 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9304 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9305 break;
9306 if (mkdir == NULL)
9307 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9308 if ((jaddref = mkdir->md_jaddref) != NULL) {
9309 mkdir->md_jaddref = NULL;
9310 jaddref->ja_state &= ~MKDIR_PARENT;
9311 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9312 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9313 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9314 journal_jremref(dirrem, jremref, inodedep);
9315 jremref = NULL;
9316 }
9317 }
9318 if (mkdir->md_state & ONWORKLIST)
9319 WORKLIST_REMOVE(&mkdir->md_list);
9320 mkdir->md_state |= ALLCOMPLETE;
9321 complete_mkdir(mkdir);
9322 return (jremref);
9323 }
9324
9325 static void
journal_jremref(struct dirrem * dirrem,struct jremref * jremref,struct inodedep * inodedep)9326 journal_jremref(struct dirrem *dirrem,
9327 struct jremref *jremref,
9328 struct inodedep *inodedep)
9329 {
9330
9331 if (inodedep == NULL)
9332 if (inodedep_lookup(jremref->jr_list.wk_mp,
9333 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9334 panic("journal_jremref: Lost inodedep");
9335 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9336 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9337 add_to_journal(&jremref->jr_list);
9338 }
9339
9340 static void
dirrem_journal(struct dirrem * dirrem,struct jremref * jremref,struct jremref * dotremref,struct jremref * dotdotremref)9341 dirrem_journal(
9342 struct dirrem *dirrem,
9343 struct jremref *jremref,
9344 struct jremref *dotremref,
9345 struct jremref *dotdotremref)
9346 {
9347 struct inodedep *inodedep;
9348
9349 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9350 &inodedep) == 0)
9351 panic("dirrem_journal: Lost inodedep");
9352 journal_jremref(dirrem, jremref, inodedep);
9353 if (dotremref)
9354 journal_jremref(dirrem, dotremref, inodedep);
9355 if (dotdotremref)
9356 journal_jremref(dirrem, dotdotremref, NULL);
9357 }
9358
9359 /*
9360 * Allocate a new dirrem if appropriate and return it along with
9361 * its associated pagedep. Called without a lock, returns with lock.
9362 */
9363 static struct dirrem *
newdirrem(struct buf * bp,struct inode * dp,struct inode * ip,int isrmdir,struct dirrem ** prevdirremp)9364 newdirrem(
9365 struct buf *bp, /* buffer containing directory block */
9366 struct inode *dp, /* inode for the directory being modified */
9367 struct inode *ip, /* inode for directory entry being removed */
9368 int isrmdir, /* indicates if doing RMDIR */
9369 struct dirrem **prevdirremp) /* previously referenced inode, if any */
9370 {
9371 int offset;
9372 ufs_lbn_t lbn;
9373 struct diradd *dap;
9374 struct dirrem *dirrem;
9375 struct pagedep *pagedep;
9376 struct jremref *jremref;
9377 struct jremref *dotremref;
9378 struct jremref *dotdotremref;
9379 struct vnode *dvp;
9380 struct ufsmount *ump;
9381
9382 /*
9383 * Whiteouts have no deletion dependencies.
9384 */
9385 if (ip == NULL)
9386 panic("newdirrem: whiteout");
9387 dvp = ITOV(dp);
9388 ump = ITOUMP(dp);
9389
9390 /*
9391 * If the system is over its limit and our filesystem is
9392 * responsible for more than our share of that usage and
9393 * we are not a snapshot, request some inodedep cleanup.
9394 * Limiting the number of dirrem structures will also limit
9395 * the number of freefile and freeblks structures.
9396 */
9397 ACQUIRE_LOCK(ump);
9398 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9399 schedule_cleanup(UFSTOVFS(ump));
9400 else
9401 FREE_LOCK(ump);
9402 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9403 M_ZERO);
9404 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9405 LIST_INIT(&dirrem->dm_jremrefhd);
9406 LIST_INIT(&dirrem->dm_jwork);
9407 dirrem->dm_state = isrmdir ? RMDIR : 0;
9408 dirrem->dm_oldinum = ip->i_number;
9409 *prevdirremp = NULL;
9410 /*
9411 * Allocate remove reference structures to track journal write
9412 * dependencies. We will always have one for the link and
9413 * when doing directories we will always have one more for dot.
9414 * When renaming a directory we skip the dotdot link change so
9415 * this is not needed.
9416 */
9417 jremref = dotremref = dotdotremref = NULL;
9418 if (DOINGSUJ(dvp)) {
9419 if (isrmdir) {
9420 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9421 ip->i_effnlink + 2);
9422 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9423 ip->i_effnlink + 1);
9424 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9425 dp->i_effnlink + 1);
9426 dotdotremref->jr_state |= MKDIR_PARENT;
9427 } else
9428 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9429 ip->i_effnlink + 1);
9430 }
9431 ACQUIRE_LOCK(ump);
9432 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9433 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9434 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9435 &pagedep);
9436 dirrem->dm_pagedep = pagedep;
9437 dirrem->dm_offset = offset;
9438 /*
9439 * If we're renaming a .. link to a new directory, cancel any
9440 * existing MKDIR_PARENT mkdir. If it has already been canceled
9441 * the jremref is preserved for any potential diradd in this
9442 * location. This can not coincide with a rmdir.
9443 */
9444 if (I_OFFSET(dp) == DOTDOT_OFFSET) {
9445 if (isrmdir)
9446 panic("newdirrem: .. directory change during remove?");
9447 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9448 }
9449 /*
9450 * If we're removing a directory search for the .. dependency now and
9451 * cancel it. Any pending journal work will be added to the dirrem
9452 * to be completed when the workitem remove completes.
9453 */
9454 if (isrmdir)
9455 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9456 /*
9457 * Check for a diradd dependency for the same directory entry.
9458 * If present, then both dependencies become obsolete and can
9459 * be de-allocated.
9460 */
9461 dap = diradd_lookup(pagedep, offset);
9462 if (dap == NULL) {
9463 /*
9464 * Link the jremref structures into the dirrem so they are
9465 * written prior to the pagedep.
9466 */
9467 if (jremref)
9468 dirrem_journal(dirrem, jremref, dotremref,
9469 dotdotremref);
9470 return (dirrem);
9471 }
9472 /*
9473 * Must be ATTACHED at this point.
9474 */
9475 if ((dap->da_state & ATTACHED) == 0)
9476 panic("newdirrem: not ATTACHED");
9477 if (dap->da_newinum != ip->i_number)
9478 panic("newdirrem: inum %ju should be %ju",
9479 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9480 /*
9481 * If we are deleting a changed name that never made it to disk,
9482 * then return the dirrem describing the previous inode (which
9483 * represents the inode currently referenced from this entry on disk).
9484 */
9485 if ((dap->da_state & DIRCHG) != 0) {
9486 *prevdirremp = dap->da_previous;
9487 dap->da_state &= ~DIRCHG;
9488 dap->da_pagedep = pagedep;
9489 }
9490 /*
9491 * We are deleting an entry that never made it to disk.
9492 * Mark it COMPLETE so we can delete its inode immediately.
9493 */
9494 dirrem->dm_state |= COMPLETE;
9495 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9496 #ifdef INVARIANTS
9497 if (isrmdir == 0) {
9498 struct worklist *wk;
9499
9500 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9501 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9502 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9503 }
9504 #endif
9505
9506 return (dirrem);
9507 }
9508
9509 /*
9510 * Directory entry change dependencies.
9511 *
9512 * Changing an existing directory entry requires that an add operation
9513 * be completed first followed by a deletion. The semantics for the addition
9514 * are identical to the description of adding a new entry above except
9515 * that the rollback is to the old inode number rather than zero. Once
9516 * the addition dependency is completed, the removal is done as described
9517 * in the removal routine above.
9518 */
9519
9520 /*
9521 * This routine should be called immediately after changing
9522 * a directory entry. The inode's link count should not be
9523 * decremented by the calling procedure -- the soft updates
9524 * code will perform this task when it is safe.
9525 */
9526 void
softdep_setup_directory_change(struct buf * bp,struct inode * dp,struct inode * ip,ino_t newinum,int isrmdir)9527 softdep_setup_directory_change(
9528 struct buf *bp, /* buffer containing directory block */
9529 struct inode *dp, /* inode for the directory being modified */
9530 struct inode *ip, /* inode for directory entry being removed */
9531 ino_t newinum, /* new inode number for changed entry */
9532 int isrmdir) /* indicates if doing RMDIR */
9533 {
9534 int offset;
9535 struct diradd *dap = NULL;
9536 struct dirrem *dirrem, *prevdirrem;
9537 struct pagedep *pagedep;
9538 struct inodedep *inodedep;
9539 struct jaddref *jaddref;
9540 struct mount *mp;
9541 struct ufsmount *ump;
9542
9543 mp = ITOVFS(dp);
9544 ump = VFSTOUFS(mp);
9545 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9546 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9547 ("softdep_setup_directory_change called on non-softdep filesystem"));
9548
9549 /*
9550 * Whiteouts do not need diradd dependencies.
9551 */
9552 if (newinum != UFS_WINO) {
9553 dap = malloc(sizeof(struct diradd),
9554 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9555 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9556 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9557 dap->da_offset = offset;
9558 dap->da_newinum = newinum;
9559 LIST_INIT(&dap->da_jwork);
9560 }
9561
9562 /*
9563 * Allocate a new dirrem and ACQUIRE_LOCK.
9564 */
9565 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9566 pagedep = dirrem->dm_pagedep;
9567 /*
9568 * The possible values for isrmdir:
9569 * 0 - non-directory file rename
9570 * 1 - directory rename within same directory
9571 * inum - directory rename to new directory of given inode number
9572 * When renaming to a new directory, we are both deleting and
9573 * creating a new directory entry, so the link count on the new
9574 * directory should not change. Thus we do not need the followup
9575 * dirrem which is usually done in handle_workitem_remove. We set
9576 * the DIRCHG flag to tell handle_workitem_remove to skip the
9577 * followup dirrem.
9578 */
9579 if (isrmdir > 1)
9580 dirrem->dm_state |= DIRCHG;
9581
9582 /*
9583 * Whiteouts have no additional dependencies,
9584 * so just put the dirrem on the correct list.
9585 */
9586 if (newinum == UFS_WINO) {
9587 if ((dirrem->dm_state & COMPLETE) == 0) {
9588 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9589 dm_next);
9590 } else {
9591 dirrem->dm_dirinum = pagedep->pd_ino;
9592 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9593 add_to_worklist(&dirrem->dm_list, 0);
9594 }
9595 FREE_LOCK(ump);
9596 return;
9597 }
9598 /*
9599 * Add the dirrem to the inodedep's pending remove list for quick
9600 * discovery later. A valid nlinkdelta ensures that this lookup
9601 * will not fail.
9602 */
9603 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9604 panic("softdep_setup_directory_change: Lost inodedep.");
9605 dirrem->dm_state |= ONDEPLIST;
9606 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9607
9608 /*
9609 * If the COMPLETE flag is clear, then there were no active
9610 * entries and we want to roll back to the previous inode until
9611 * the new inode is committed to disk. If the COMPLETE flag is
9612 * set, then we have deleted an entry that never made it to disk.
9613 * If the entry we deleted resulted from a name change, then the old
9614 * inode reference still resides on disk. Any rollback that we do
9615 * needs to be to that old inode (returned to us in prevdirrem). If
9616 * the entry we deleted resulted from a create, then there is
9617 * no entry on the disk, so we want to roll back to zero rather
9618 * than the uncommitted inode. In either of the COMPLETE cases we
9619 * want to immediately free the unwritten and unreferenced inode.
9620 */
9621 if ((dirrem->dm_state & COMPLETE) == 0) {
9622 dap->da_previous = dirrem;
9623 } else {
9624 if (prevdirrem != NULL) {
9625 dap->da_previous = prevdirrem;
9626 } else {
9627 dap->da_state &= ~DIRCHG;
9628 dap->da_pagedep = pagedep;
9629 }
9630 dirrem->dm_dirinum = pagedep->pd_ino;
9631 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9632 add_to_worklist(&dirrem->dm_list, 0);
9633 }
9634 /*
9635 * Lookup the jaddref for this journal entry. We must finish
9636 * initializing it and make the diradd write dependent on it.
9637 * If we're not journaling, put it on the id_bufwait list if the
9638 * inode is not yet written. If it is written, do the post-inode
9639 * write processing to put it on the id_pendinghd list.
9640 */
9641 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9642 if (MOUNTEDSUJ(mp)) {
9643 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9644 inoreflst);
9645 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9646 ("softdep_setup_directory_change: bad jaddref %p",
9647 jaddref));
9648 jaddref->ja_diroff = I_OFFSET(dp);
9649 jaddref->ja_diradd = dap;
9650 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9651 dap, da_pdlist);
9652 add_to_journal(&jaddref->ja_list);
9653 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9654 dap->da_state |= COMPLETE;
9655 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9656 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9657 } else {
9658 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9659 dap, da_pdlist);
9660 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9661 }
9662 /*
9663 * If we're making a new name for a directory that has not been
9664 * committed when need to move the dot and dotdot references to
9665 * this new name.
9666 */
9667 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET)
9668 merge_diradd(inodedep, dap);
9669 FREE_LOCK(ump);
9670 }
9671
9672 /*
9673 * Called whenever the link count on an inode is changed.
9674 * It creates an inode dependency so that the new reference(s)
9675 * to the inode cannot be committed to disk until the updated
9676 * inode has been written.
9677 */
9678 void
softdep_change_linkcnt(struct inode * ip)9679 softdep_change_linkcnt(
9680 struct inode *ip) /* the inode with the increased link count */
9681 {
9682 struct inodedep *inodedep;
9683 struct ufsmount *ump;
9684
9685 ump = ITOUMP(ip);
9686 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9687 ("softdep_change_linkcnt called on non-softdep filesystem"));
9688 ACQUIRE_LOCK(ump);
9689 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9690 if (ip->i_nlink < ip->i_effnlink)
9691 panic("softdep_change_linkcnt: bad delta");
9692 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9693 FREE_LOCK(ump);
9694 }
9695
9696 /*
9697 * Attach a sbdep dependency to the superblock buf so that we can keep
9698 * track of the head of the linked list of referenced but unlinked inodes.
9699 */
9700 void
softdep_setup_sbupdate(struct ufsmount * ump,struct fs * fs,struct buf * bp)9701 softdep_setup_sbupdate(
9702 struct ufsmount *ump,
9703 struct fs *fs,
9704 struct buf *bp)
9705 {
9706 struct sbdep *sbdep;
9707 struct worklist *wk;
9708
9709 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9710 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9711 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9712 if (wk->wk_type == D_SBDEP)
9713 break;
9714 if (wk != NULL)
9715 return;
9716 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9717 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9718 sbdep->sb_fs = fs;
9719 sbdep->sb_ump = ump;
9720 ACQUIRE_LOCK(ump);
9721 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9722 FREE_LOCK(ump);
9723 }
9724
9725 /*
9726 * Return the first unlinked inodedep which is ready to be the head of the
9727 * list. The inodedep and all those after it must have valid next pointers.
9728 */
9729 static struct inodedep *
first_unlinked_inodedep(struct ufsmount * ump)9730 first_unlinked_inodedep(struct ufsmount *ump)
9731 {
9732 struct inodedep *inodedep;
9733 struct inodedep *idp;
9734
9735 LOCK_OWNED(ump);
9736 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9737 inodedep; inodedep = idp) {
9738 if ((inodedep->id_state & UNLINKNEXT) == 0)
9739 return (NULL);
9740 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9741 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9742 break;
9743 if ((inodedep->id_state & UNLINKPREV) == 0)
9744 break;
9745 }
9746 return (inodedep);
9747 }
9748
9749 /*
9750 * Set the sujfree unlinked head pointer prior to writing a superblock.
9751 */
9752 static void
initiate_write_sbdep(struct sbdep * sbdep)9753 initiate_write_sbdep(struct sbdep *sbdep)
9754 {
9755 struct inodedep *inodedep;
9756 struct fs *bpfs;
9757 struct fs *fs;
9758
9759 bpfs = sbdep->sb_fs;
9760 fs = sbdep->sb_ump->um_fs;
9761 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9762 if (inodedep) {
9763 fs->fs_sujfree = inodedep->id_ino;
9764 inodedep->id_state |= UNLINKPREV;
9765 } else
9766 fs->fs_sujfree = 0;
9767 bpfs->fs_sujfree = fs->fs_sujfree;
9768 /*
9769 * Because we have made changes to the superblock, we need to
9770 * recompute its check-hash.
9771 */
9772 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9773 }
9774
9775 /*
9776 * After a superblock is written determine whether it must be written again
9777 * due to a changing unlinked list head.
9778 */
9779 static int
handle_written_sbdep(struct sbdep * sbdep,struct buf * bp)9780 handle_written_sbdep(struct sbdep *sbdep, struct buf *bp)
9781 {
9782 struct inodedep *inodedep;
9783 struct fs *fs;
9784
9785 LOCK_OWNED(sbdep->sb_ump);
9786 fs = sbdep->sb_fs;
9787 /*
9788 * If the superblock doesn't match the in-memory list start over.
9789 */
9790 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9791 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9792 (inodedep == NULL && fs->fs_sujfree != 0)) {
9793 bdirty(bp);
9794 return (1);
9795 }
9796 WORKITEM_FREE(sbdep, D_SBDEP);
9797 if (fs->fs_sujfree == 0)
9798 return (0);
9799 /*
9800 * Now that we have a record of this inode in stable store allow it
9801 * to be written to free up pending work. Inodes may see a lot of
9802 * write activity after they are unlinked which we must not hold up.
9803 */
9804 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9805 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9806 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9807 inodedep, inodedep->id_state);
9808 if (inodedep->id_state & UNLINKONLIST)
9809 break;
9810 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9811 }
9812
9813 return (0);
9814 }
9815
9816 /*
9817 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9818 */
9819 static void
unlinked_inodedep(struct mount * mp,struct inodedep * inodedep)9820 unlinked_inodedep( struct mount *mp, struct inodedep *inodedep)
9821 {
9822 struct ufsmount *ump;
9823
9824 ump = VFSTOUFS(mp);
9825 LOCK_OWNED(ump);
9826 if (MOUNTEDSUJ(mp) == 0)
9827 return;
9828 ump->um_fs->fs_fmod = 1;
9829 if (inodedep->id_state & UNLINKED)
9830 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9831 inodedep->id_state |= UNLINKED;
9832 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9833 }
9834
9835 /*
9836 * Remove an inodedep from the unlinked inodedep list. This may require
9837 * disk writes if the inode has made it that far.
9838 */
9839 static void
clear_unlinked_inodedep(struct inodedep * inodedep)9840 clear_unlinked_inodedep( struct inodedep *inodedep)
9841 {
9842 struct ufs2_dinode *dip;
9843 struct ufsmount *ump;
9844 struct inodedep *idp;
9845 struct inodedep *idn;
9846 struct fs *fs, *bpfs;
9847 struct buf *bp;
9848 daddr_t dbn;
9849 ino_t ino;
9850 ino_t nino;
9851 ino_t pino;
9852 int error;
9853
9854 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9855 fs = ump->um_fs;
9856 ino = inodedep->id_ino;
9857 error = 0;
9858 for (;;) {
9859 LOCK_OWNED(ump);
9860 KASSERT((inodedep->id_state & UNLINKED) != 0,
9861 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9862 inodedep));
9863 /*
9864 * If nothing has yet been written simply remove us from
9865 * the in memory list and return. This is the most common
9866 * case where handle_workitem_remove() loses the final
9867 * reference.
9868 */
9869 if ((inodedep->id_state & UNLINKLINKS) == 0)
9870 break;
9871 /*
9872 * If we have a NEXT pointer and no PREV pointer we can simply
9873 * clear NEXT's PREV and remove ourselves from the list. Be
9874 * careful not to clear PREV if the superblock points at
9875 * next as well.
9876 */
9877 idn = TAILQ_NEXT(inodedep, id_unlinked);
9878 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9879 if (idn && fs->fs_sujfree != idn->id_ino)
9880 idn->id_state &= ~UNLINKPREV;
9881 break;
9882 }
9883 /*
9884 * Here we have an inodedep which is actually linked into
9885 * the list. We must remove it by forcing a write to the
9886 * link before us, whether it be the superblock or an inode.
9887 * Unfortunately the list may change while we're waiting
9888 * on the buf lock for either resource so we must loop until
9889 * we lock the right one. If both the superblock and an
9890 * inode point to this inode we must clear the inode first
9891 * followed by the superblock.
9892 */
9893 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9894 pino = 0;
9895 if (idp && (idp->id_state & UNLINKNEXT))
9896 pino = idp->id_ino;
9897 FREE_LOCK(ump);
9898 if (pino == 0) {
9899 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9900 (int)fs->fs_sbsize, 0, 0, 0);
9901 } else {
9902 dbn = fsbtodb(fs, ino_to_fsba(fs, pino));
9903 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn,
9904 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL,
9905 &bp);
9906 }
9907 ACQUIRE_LOCK(ump);
9908 if (error)
9909 break;
9910 /* If the list has changed restart the loop. */
9911 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9912 nino = 0;
9913 if (idp && (idp->id_state & UNLINKNEXT))
9914 nino = idp->id_ino;
9915 if (nino != pino ||
9916 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9917 FREE_LOCK(ump);
9918 brelse(bp);
9919 ACQUIRE_LOCK(ump);
9920 continue;
9921 }
9922 nino = 0;
9923 idn = TAILQ_NEXT(inodedep, id_unlinked);
9924 if (idn)
9925 nino = idn->id_ino;
9926 /*
9927 * Remove us from the in memory list. After this we cannot
9928 * access the inodedep.
9929 */
9930 KASSERT((inodedep->id_state & UNLINKED) != 0,
9931 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9932 inodedep));
9933 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9934 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9935 FREE_LOCK(ump);
9936 /*
9937 * The predecessor's next pointer is manually updated here
9938 * so that the NEXT flag is never cleared for an element
9939 * that is in the list.
9940 */
9941 if (pino == 0) {
9942 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize);
9943 bpfs = (struct fs *)bp->b_data;
9944 ffs_oldfscompat_write(bpfs);
9945 softdep_setup_sbupdate(ump, bpfs, bp);
9946 /*
9947 * Because we may have made changes to the superblock,
9948 * we need to recompute its check-hash.
9949 */
9950 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9951 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
9952 ((struct ufs1_dinode *)bp->b_data +
9953 ino_to_fsbo(fs, pino))->di_freelink = nino;
9954 } else {
9955 dip = (struct ufs2_dinode *)bp->b_data +
9956 ino_to_fsbo(fs, pino);
9957 dip->di_freelink = nino;
9958 ffs_update_dinode_ckhash(fs, dip);
9959 }
9960 /*
9961 * If the bwrite fails we have no recourse to recover. The
9962 * filesystem is corrupted already.
9963 */
9964 bwrite(bp);
9965 ACQUIRE_LOCK(ump);
9966 /*
9967 * If the superblock pointer still needs to be cleared force
9968 * a write here.
9969 */
9970 if (fs->fs_sujfree == ino) {
9971 FREE_LOCK(ump);
9972 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9973 (int)fs->fs_sbsize, 0, 0, 0);
9974 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize);
9975 bpfs = (struct fs *)bp->b_data;
9976 ffs_oldfscompat_write(bpfs);
9977 softdep_setup_sbupdate(ump, bpfs, bp);
9978 /*
9979 * Because we may have made changes to the superblock,
9980 * we need to recompute its check-hash.
9981 */
9982 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9983 bwrite(bp);
9984 ACQUIRE_LOCK(ump);
9985 }
9986
9987 if (fs->fs_sujfree != ino)
9988 return;
9989 panic("clear_unlinked_inodedep: Failed to clear free head");
9990 }
9991 if (inodedep->id_ino == fs->fs_sujfree)
9992 panic("clear_unlinked_inodedep: Freeing head of free list");
9993 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9994 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9995 return;
9996 }
9997
9998 /*
9999 * This workitem decrements the inode's link count.
10000 * If the link count reaches zero, the file is removed.
10001 */
10002 static int
handle_workitem_remove(struct dirrem * dirrem,int flags)10003 handle_workitem_remove(struct dirrem *dirrem, int flags)
10004 {
10005 struct inodedep *inodedep;
10006 struct workhead dotdotwk;
10007 struct worklist *wk;
10008 struct ufsmount *ump;
10009 struct mount *mp;
10010 struct vnode *vp;
10011 struct inode *ip;
10012 ino_t oldinum;
10013
10014 if (dirrem->dm_state & ONWORKLIST)
10015 panic("handle_workitem_remove: dirrem %p still on worklist",
10016 dirrem);
10017 oldinum = dirrem->dm_oldinum;
10018 mp = dirrem->dm_list.wk_mp;
10019 ump = VFSTOUFS(mp);
10020 flags |= LK_EXCLUSIVE;
10021 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ |
10022 FFSV_FORCEINODEDEP) != 0)
10023 return (EBUSY);
10024 ip = VTOI(vp);
10025 MPASS(ip->i_mode != 0);
10026 ACQUIRE_LOCK(ump);
10027 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
10028 panic("handle_workitem_remove: lost inodedep");
10029 if (dirrem->dm_state & ONDEPLIST)
10030 LIST_REMOVE(dirrem, dm_inonext);
10031 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
10032 ("handle_workitem_remove: Journal entries not written."));
10033
10034 /*
10035 * Move all dependencies waiting on the remove to complete
10036 * from the dirrem to the inode inowait list to be completed
10037 * after the inode has been updated and written to disk.
10038 *
10039 * Any marked MKDIR_PARENT are saved to be completed when the
10040 * dotdot ref is removed unless DIRCHG is specified. For
10041 * directory change operations there will be no further
10042 * directory writes and the jsegdeps need to be moved along
10043 * with the rest to be completed when the inode is free or
10044 * stable in the inode free list.
10045 */
10046 LIST_INIT(&dotdotwk);
10047 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
10048 WORKLIST_REMOVE(wk);
10049 if ((dirrem->dm_state & DIRCHG) == 0 &&
10050 wk->wk_state & MKDIR_PARENT) {
10051 wk->wk_state &= ~MKDIR_PARENT;
10052 WORKLIST_INSERT(&dotdotwk, wk);
10053 continue;
10054 }
10055 WORKLIST_INSERT(&inodedep->id_inowait, wk);
10056 }
10057 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
10058 /*
10059 * Normal file deletion.
10060 */
10061 if ((dirrem->dm_state & RMDIR) == 0) {
10062 ip->i_nlink--;
10063 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
10064 "%ju negative i_nlink %d", (intmax_t)ip->i_number,
10065 ip->i_nlink));
10066 DIP_SET_NLINK(ip, ip->i_nlink);
10067 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10068 if (ip->i_nlink < ip->i_effnlink)
10069 panic("handle_workitem_remove: bad file delta");
10070 if (ip->i_nlink == 0)
10071 unlinked_inodedep(mp, inodedep);
10072 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10073 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10074 ("handle_workitem_remove: worklist not empty. %s",
10075 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
10076 WORKITEM_FREE(dirrem, D_DIRREM);
10077 FREE_LOCK(ump);
10078 goto out;
10079 }
10080 /*
10081 * Directory deletion. Decrement reference count for both the
10082 * just deleted parent directory entry and the reference for ".".
10083 * Arrange to have the reference count on the parent decremented
10084 * to account for the loss of "..".
10085 */
10086 ip->i_nlink -= 2;
10087 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
10088 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
10089 DIP_SET_NLINK(ip, ip->i_nlink);
10090 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10091 if (ip->i_nlink < ip->i_effnlink)
10092 panic("handle_workitem_remove: bad dir delta");
10093 if (ip->i_nlink == 0)
10094 unlinked_inodedep(mp, inodedep);
10095 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10096 /*
10097 * Rename a directory to a new parent. Since, we are both deleting
10098 * and creating a new directory entry, the link count on the new
10099 * directory should not change. Thus we skip the followup dirrem.
10100 */
10101 if (dirrem->dm_state & DIRCHG) {
10102 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10103 ("handle_workitem_remove: DIRCHG and worklist not empty."));
10104 WORKITEM_FREE(dirrem, D_DIRREM);
10105 FREE_LOCK(ump);
10106 goto out;
10107 }
10108 dirrem->dm_state = ONDEPLIST;
10109 dirrem->dm_oldinum = dirrem->dm_dirinum;
10110 /*
10111 * Place the dirrem on the parent's diremhd list.
10112 */
10113 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
10114 panic("handle_workitem_remove: lost dir inodedep");
10115 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
10116 /*
10117 * If the allocated inode has never been written to disk, then
10118 * the on-disk inode is zero'ed and we can remove the file
10119 * immediately. When journaling if the inode has been marked
10120 * unlinked and not DEPCOMPLETE we know it can never be written.
10121 */
10122 inodedep_lookup(mp, oldinum, 0, &inodedep);
10123 if (inodedep == NULL ||
10124 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
10125 check_inode_unwritten(inodedep)) {
10126 FREE_LOCK(ump);
10127 vput(vp);
10128 return handle_workitem_remove(dirrem, flags);
10129 }
10130 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
10131 FREE_LOCK(ump);
10132 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10133 out:
10134 ffs_update(vp, 0);
10135 vput(vp);
10136 return (0);
10137 }
10138
10139 /*
10140 * Inode de-allocation dependencies.
10141 *
10142 * When an inode's link count is reduced to zero, it can be de-allocated. We
10143 * found it convenient to postpone de-allocation until after the inode is
10144 * written to disk with its new link count (zero). At this point, all of the
10145 * on-disk inode's block pointers are nullified and, with careful dependency
10146 * list ordering, all dependencies related to the inode will be satisfied and
10147 * the corresponding dependency structures de-allocated. So, if/when the
10148 * inode is reused, there will be no mixing of old dependencies with new
10149 * ones. This artificial dependency is set up by the block de-allocation
10150 * procedure above (softdep_setup_freeblocks) and completed by the
10151 * following procedure.
10152 */
10153 static void
handle_workitem_freefile(struct freefile * freefile)10154 handle_workitem_freefile(struct freefile *freefile)
10155 {
10156 struct workhead wkhd;
10157 struct fs *fs;
10158 struct ufsmount *ump;
10159 int error;
10160 #ifdef INVARIANTS
10161 struct inodedep *idp;
10162 #endif
10163
10164 ump = VFSTOUFS(freefile->fx_list.wk_mp);
10165 fs = ump->um_fs;
10166 #ifdef INVARIANTS
10167 ACQUIRE_LOCK(ump);
10168 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10169 FREE_LOCK(ump);
10170 if (error)
10171 panic("handle_workitem_freefile: inodedep %p survived", idp);
10172 #endif
10173 UFS_LOCK(ump);
10174 fs->fs_pendinginodes -= 1;
10175 UFS_UNLOCK(ump);
10176 LIST_INIT(&wkhd);
10177 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10178 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10179 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10180 softdep_error("handle_workitem_freefile", error);
10181 ACQUIRE_LOCK(ump);
10182 WORKITEM_FREE(freefile, D_FREEFILE);
10183 FREE_LOCK(ump);
10184 }
10185
10186 /*
10187 * Helper function which unlinks marker element from work list and returns
10188 * the next element on the list.
10189 */
10190 static __inline struct worklist *
markernext(struct worklist * marker)10191 markernext(struct worklist *marker)
10192 {
10193 struct worklist *next;
10194
10195 next = LIST_NEXT(marker, wk_list);
10196 LIST_REMOVE(marker, wk_list);
10197 return next;
10198 }
10199
10200 /*
10201 * Disk writes.
10202 *
10203 * The dependency structures constructed above are most actively used when file
10204 * system blocks are written to disk. No constraints are placed on when a
10205 * block can be written, but unsatisfied update dependencies are made safe by
10206 * modifying (or replacing) the source memory for the duration of the disk
10207 * write. When the disk write completes, the memory block is again brought
10208 * up-to-date.
10209 *
10210 * In-core inode structure reclamation.
10211 *
10212 * Because there are a finite number of "in-core" inode structures, they are
10213 * reused regularly. By transferring all inode-related dependencies to the
10214 * in-memory inode block and indexing them separately (via "inodedep"s), we
10215 * can allow "in-core" inode structures to be reused at any time and avoid
10216 * any increase in contention.
10217 *
10218 * Called just before entering the device driver to initiate a new disk I/O.
10219 * The buffer must be locked, thus, no I/O completion operations can occur
10220 * while we are manipulating its associated dependencies.
10221 */
10222 static void
softdep_disk_io_initiation(struct buf * bp)10223 softdep_disk_io_initiation(
10224 struct buf *bp) /* structure describing disk write to occur */
10225 {
10226 struct worklist *wk;
10227 struct worklist marker;
10228 struct inodedep *inodedep;
10229 struct freeblks *freeblks;
10230 struct jblkdep *jblkdep;
10231 struct newblk *newblk;
10232 struct ufsmount *ump;
10233
10234 /*
10235 * We only care about write operations. There should never
10236 * be dependencies for reads.
10237 */
10238 if (bp->b_iocmd != BIO_WRITE)
10239 panic("softdep_disk_io_initiation: not write");
10240
10241 if (bp->b_vflags & BV_BKGRDINPROG)
10242 panic("softdep_disk_io_initiation: Writing buffer with "
10243 "background write in progress: %p", bp);
10244
10245 ump = softdep_bp_to_mp(bp);
10246 if (ump == NULL)
10247 return;
10248
10249 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10250 PHOLD(curproc); /* Don't swap out kernel stack */
10251 ACQUIRE_LOCK(ump);
10252 /*
10253 * Do any necessary pre-I/O processing.
10254 */
10255 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10256 wk = markernext(&marker)) {
10257 LIST_INSERT_AFTER(wk, &marker, wk_list);
10258 switch (wk->wk_type) {
10259 case D_PAGEDEP:
10260 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10261 continue;
10262
10263 case D_INODEDEP:
10264 inodedep = WK_INODEDEP(wk);
10265 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10266 initiate_write_inodeblock_ufs1(inodedep, bp);
10267 else
10268 initiate_write_inodeblock_ufs2(inodedep, bp);
10269 continue;
10270
10271 case D_INDIRDEP:
10272 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10273 continue;
10274
10275 case D_BMSAFEMAP:
10276 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10277 continue;
10278
10279 case D_JSEG:
10280 WK_JSEG(wk)->js_buf = NULL;
10281 continue;
10282
10283 case D_FREEBLKS:
10284 freeblks = WK_FREEBLKS(wk);
10285 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10286 /*
10287 * We have to wait for the freeblks to be journaled
10288 * before we can write an inodeblock with updated
10289 * pointers. Be careful to arrange the marker so
10290 * we revisit the freeblks if it's not removed by
10291 * the first jwait().
10292 */
10293 if (jblkdep != NULL) {
10294 LIST_REMOVE(&marker, wk_list);
10295 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10296 jwait(&jblkdep->jb_list, MNT_WAIT);
10297 }
10298 continue;
10299 case D_ALLOCDIRECT:
10300 case D_ALLOCINDIR:
10301 /*
10302 * We have to wait for the jnewblk to be journaled
10303 * before we can write to a block if the contents
10304 * may be confused with an earlier file's indirect
10305 * at recovery time. Handle the marker as described
10306 * above.
10307 */
10308 newblk = WK_NEWBLK(wk);
10309 if (newblk->nb_jnewblk != NULL &&
10310 indirblk_lookup(newblk->nb_list.wk_mp,
10311 newblk->nb_newblkno)) {
10312 LIST_REMOVE(&marker, wk_list);
10313 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10314 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10315 }
10316 continue;
10317
10318 case D_SBDEP:
10319 initiate_write_sbdep(WK_SBDEP(wk));
10320 continue;
10321
10322 case D_MKDIR:
10323 case D_FREEWORK:
10324 case D_FREEDEP:
10325 case D_JSEGDEP:
10326 continue;
10327
10328 default:
10329 panic("handle_disk_io_initiation: Unexpected type %s",
10330 TYPENAME(wk->wk_type));
10331 /* NOTREACHED */
10332 }
10333 }
10334 FREE_LOCK(ump);
10335 PRELE(curproc); /* Allow swapout of kernel stack */
10336 }
10337
10338 /*
10339 * Called from within the procedure above to deal with unsatisfied
10340 * allocation dependencies in a directory. The buffer must be locked,
10341 * thus, no I/O completion operations can occur while we are
10342 * manipulating its associated dependencies.
10343 */
10344 static void
initiate_write_filepage(struct pagedep * pagedep,struct buf * bp)10345 initiate_write_filepage(struct pagedep *pagedep, struct buf *bp)
10346 {
10347 struct jremref *jremref;
10348 struct jmvref *jmvref;
10349 struct dirrem *dirrem;
10350 struct diradd *dap;
10351 struct direct *ep;
10352 int i;
10353
10354 if (pagedep->pd_state & IOSTARTED) {
10355 /*
10356 * This can only happen if there is a driver that does not
10357 * understand chaining. Here biodone will reissue the call
10358 * to strategy for the incomplete buffers.
10359 */
10360 printf("initiate_write_filepage: already started\n");
10361 return;
10362 }
10363 pagedep->pd_state |= IOSTARTED;
10364 /*
10365 * Wait for all journal remove dependencies to hit the disk.
10366 * We can not allow any potentially conflicting directory adds
10367 * to be visible before removes and rollback is too difficult.
10368 * The per-filesystem lock may be dropped and re-acquired, however
10369 * we hold the buf locked so the dependency can not go away.
10370 */
10371 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10372 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10373 jwait(&jremref->jr_list, MNT_WAIT);
10374 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10375 jwait(&jmvref->jm_list, MNT_WAIT);
10376 for (i = 0; i < DAHASHSZ; i++) {
10377 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10378 ep = (struct direct *)
10379 ((char *)bp->b_data + dap->da_offset);
10380 if (ep->d_ino != dap->da_newinum)
10381 panic("%s: dir inum %ju != new %ju",
10382 "initiate_write_filepage",
10383 (uintmax_t)ep->d_ino,
10384 (uintmax_t)dap->da_newinum);
10385 if (dap->da_state & DIRCHG)
10386 ep->d_ino = dap->da_previous->dm_oldinum;
10387 else
10388 ep->d_ino = 0;
10389 dap->da_state &= ~ATTACHED;
10390 dap->da_state |= UNDONE;
10391 }
10392 }
10393 }
10394
10395 /*
10396 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10397 * Note that any bug fixes made to this routine must be done in the
10398 * version found below.
10399 *
10400 * Called from within the procedure above to deal with unsatisfied
10401 * allocation dependencies in an inodeblock. The buffer must be
10402 * locked, thus, no I/O completion operations can occur while we
10403 * are manipulating its associated dependencies.
10404 */
10405 static void
initiate_write_inodeblock_ufs1(struct inodedep * inodedep,struct buf * bp)10406 initiate_write_inodeblock_ufs1(
10407 struct inodedep *inodedep,
10408 struct buf *bp) /* The inode block */
10409 {
10410 struct allocdirect *adp, *lastadp;
10411 struct ufs1_dinode *dp;
10412 struct ufs1_dinode *sip;
10413 struct inoref *inoref;
10414 struct ufsmount *ump;
10415 struct fs *fs;
10416 ufs_lbn_t i;
10417 #ifdef INVARIANTS
10418 ufs_lbn_t prevlbn = 0;
10419 #endif
10420 int deplist __diagused;
10421
10422 if (inodedep->id_state & IOSTARTED)
10423 panic("initiate_write_inodeblock_ufs1: already started");
10424 inodedep->id_state |= IOSTARTED;
10425 fs = inodedep->id_fs;
10426 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10427 LOCK_OWNED(ump);
10428 dp = (struct ufs1_dinode *)bp->b_data +
10429 ino_to_fsbo(fs, inodedep->id_ino);
10430
10431 /*
10432 * If we're on the unlinked list but have not yet written our
10433 * next pointer initialize it here.
10434 */
10435 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10436 struct inodedep *inon;
10437
10438 inon = TAILQ_NEXT(inodedep, id_unlinked);
10439 dp->di_freelink = inon ? inon->id_ino : 0;
10440 }
10441 /*
10442 * If the bitmap is not yet written, then the allocated
10443 * inode cannot be written to disk.
10444 */
10445 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10446 if (inodedep->id_savedino1 != NULL)
10447 panic("initiate_write_inodeblock_ufs1: I/O underway");
10448 FREE_LOCK(ump);
10449 sip = malloc(sizeof(struct ufs1_dinode),
10450 M_SAVEDINO, M_SOFTDEP_FLAGS);
10451 ACQUIRE_LOCK(ump);
10452 inodedep->id_savedino1 = sip;
10453 *inodedep->id_savedino1 = *dp;
10454 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10455 dp->di_gen = inodedep->id_savedino1->di_gen;
10456 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10457 return;
10458 }
10459 /*
10460 * If no dependencies, then there is nothing to roll back.
10461 */
10462 inodedep->id_savedsize = dp->di_size;
10463 inodedep->id_savedextsize = 0;
10464 inodedep->id_savednlink = dp->di_nlink;
10465 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10466 TAILQ_EMPTY(&inodedep->id_inoreflst))
10467 return;
10468 /*
10469 * Revert the link count to that of the first unwritten journal entry.
10470 */
10471 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10472 if (inoref)
10473 dp->di_nlink = inoref->if_nlink;
10474 /*
10475 * Set the dependencies to busy.
10476 */
10477 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10478 adp = TAILQ_NEXT(adp, ad_next)) {
10479 #ifdef INVARIANTS
10480 if (deplist != 0 && prevlbn >= adp->ad_offset)
10481 panic("softdep_write_inodeblock: lbn order");
10482 prevlbn = adp->ad_offset;
10483 if (adp->ad_offset < UFS_NDADDR &&
10484 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10485 panic("initiate_write_inodeblock_ufs1: "
10486 "direct pointer #%jd mismatch %d != %jd",
10487 (intmax_t)adp->ad_offset,
10488 dp->di_db[adp->ad_offset],
10489 (intmax_t)adp->ad_newblkno);
10490 if (adp->ad_offset >= UFS_NDADDR &&
10491 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10492 panic("initiate_write_inodeblock_ufs1: "
10493 "indirect pointer #%jd mismatch %d != %jd",
10494 (intmax_t)adp->ad_offset - UFS_NDADDR,
10495 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10496 (intmax_t)adp->ad_newblkno);
10497 deplist |= 1 << adp->ad_offset;
10498 if ((adp->ad_state & ATTACHED) == 0)
10499 panic("initiate_write_inodeblock_ufs1: "
10500 "Unknown state 0x%x", adp->ad_state);
10501 #endif /* INVARIANTS */
10502 adp->ad_state &= ~ATTACHED;
10503 adp->ad_state |= UNDONE;
10504 }
10505 /*
10506 * The on-disk inode cannot claim to be any larger than the last
10507 * fragment that has been written. Otherwise, the on-disk inode
10508 * might have fragments that were not the last block in the file
10509 * which would corrupt the filesystem.
10510 */
10511 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10512 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10513 if (adp->ad_offset >= UFS_NDADDR)
10514 break;
10515 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10516 /* keep going until hitting a rollback to a frag */
10517 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10518 continue;
10519 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10520 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10521 #ifdef INVARIANTS
10522 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10523 panic("initiate_write_inodeblock_ufs1: "
10524 "lost dep1");
10525 #endif /* INVARIANTS */
10526 dp->di_db[i] = 0;
10527 }
10528 for (i = 0; i < UFS_NIADDR; i++) {
10529 #ifdef INVARIANTS
10530 if (dp->di_ib[i] != 0 &&
10531 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10532 panic("initiate_write_inodeblock_ufs1: "
10533 "lost dep2");
10534 #endif /* INVARIANTS */
10535 dp->di_ib[i] = 0;
10536 }
10537 return;
10538 }
10539 /*
10540 * If we have zero'ed out the last allocated block of the file,
10541 * roll back the size to the last currently allocated block.
10542 * We know that this last allocated block is a full-sized as
10543 * we already checked for fragments in the loop above.
10544 */
10545 if (lastadp != NULL &&
10546 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10547 for (i = lastadp->ad_offset; i >= 0; i--)
10548 if (dp->di_db[i] != 0)
10549 break;
10550 dp->di_size = (i + 1) * fs->fs_bsize;
10551 }
10552 /*
10553 * The only dependencies are for indirect blocks.
10554 *
10555 * The file size for indirect block additions is not guaranteed.
10556 * Such a guarantee would be non-trivial to achieve. The conventional
10557 * synchronous write implementation also does not make this guarantee.
10558 * Fsck should catch and fix discrepancies. Arguably, the file size
10559 * can be over-estimated without destroying integrity when the file
10560 * moves into the indirect blocks (i.e., is large). If we want to
10561 * postpone fsck, we are stuck with this argument.
10562 */
10563 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10564 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10565 }
10566
10567 /*
10568 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10569 * Note that any bug fixes made to this routine must be done in the
10570 * version found above.
10571 *
10572 * Called from within the procedure above to deal with unsatisfied
10573 * allocation dependencies in an inodeblock. The buffer must be
10574 * locked, thus, no I/O completion operations can occur while we
10575 * are manipulating its associated dependencies.
10576 */
10577 static void
initiate_write_inodeblock_ufs2(struct inodedep * inodedep,struct buf * bp)10578 initiate_write_inodeblock_ufs2(
10579 struct inodedep *inodedep,
10580 struct buf *bp) /* The inode block */
10581 {
10582 struct allocdirect *adp, *lastadp;
10583 struct ufs2_dinode *dp;
10584 struct ufs2_dinode *sip;
10585 struct inoref *inoref;
10586 struct ufsmount *ump;
10587 struct fs *fs;
10588 ufs_lbn_t i;
10589 #ifdef INVARIANTS
10590 ufs_lbn_t prevlbn = 0;
10591 #endif
10592 int deplist __diagused;
10593
10594 if (inodedep->id_state & IOSTARTED)
10595 panic("initiate_write_inodeblock_ufs2: already started");
10596 inodedep->id_state |= IOSTARTED;
10597 fs = inodedep->id_fs;
10598 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10599 LOCK_OWNED(ump);
10600 dp = (struct ufs2_dinode *)bp->b_data +
10601 ino_to_fsbo(fs, inodedep->id_ino);
10602
10603 /*
10604 * If we're on the unlinked list but have not yet written our
10605 * next pointer initialize it here.
10606 */
10607 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10608 struct inodedep *inon;
10609
10610 inon = TAILQ_NEXT(inodedep, id_unlinked);
10611 dp->di_freelink = inon ? inon->id_ino : 0;
10612 ffs_update_dinode_ckhash(fs, dp);
10613 }
10614 /*
10615 * If the bitmap is not yet written, then the allocated
10616 * inode cannot be written to disk.
10617 */
10618 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10619 if (inodedep->id_savedino2 != NULL)
10620 panic("initiate_write_inodeblock_ufs2: I/O underway");
10621 FREE_LOCK(ump);
10622 sip = malloc(sizeof(struct ufs2_dinode),
10623 M_SAVEDINO, M_SOFTDEP_FLAGS);
10624 ACQUIRE_LOCK(ump);
10625 inodedep->id_savedino2 = sip;
10626 *inodedep->id_savedino2 = *dp;
10627 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10628 dp->di_gen = inodedep->id_savedino2->di_gen;
10629 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10630 return;
10631 }
10632 /*
10633 * If no dependencies, then there is nothing to roll back.
10634 */
10635 inodedep->id_savedsize = dp->di_size;
10636 inodedep->id_savedextsize = dp->di_extsize;
10637 inodedep->id_savednlink = dp->di_nlink;
10638 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10639 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10640 TAILQ_EMPTY(&inodedep->id_inoreflst))
10641 return;
10642 /*
10643 * Revert the link count to that of the first unwritten journal entry.
10644 */
10645 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10646 if (inoref)
10647 dp->di_nlink = inoref->if_nlink;
10648
10649 /*
10650 * Set the ext data dependencies to busy.
10651 */
10652 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10653 adp = TAILQ_NEXT(adp, ad_next)) {
10654 #ifdef INVARIANTS
10655 if (deplist != 0 && prevlbn >= adp->ad_offset)
10656 panic("initiate_write_inodeblock_ufs2: lbn order");
10657 prevlbn = adp->ad_offset;
10658 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10659 panic("initiate_write_inodeblock_ufs2: "
10660 "ext pointer #%jd mismatch %jd != %jd",
10661 (intmax_t)adp->ad_offset,
10662 (intmax_t)dp->di_extb[adp->ad_offset],
10663 (intmax_t)adp->ad_newblkno);
10664 deplist |= 1 << adp->ad_offset;
10665 if ((adp->ad_state & ATTACHED) == 0)
10666 panic("initiate_write_inodeblock_ufs2: Unknown "
10667 "state 0x%x", adp->ad_state);
10668 #endif /* INVARIANTS */
10669 adp->ad_state &= ~ATTACHED;
10670 adp->ad_state |= UNDONE;
10671 }
10672 /*
10673 * The on-disk inode cannot claim to be any larger than the last
10674 * fragment that has been written. Otherwise, the on-disk inode
10675 * might have fragments that were not the last block in the ext
10676 * data which would corrupt the filesystem.
10677 */
10678 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10679 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10680 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10681 /* keep going until hitting a rollback to a frag */
10682 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10683 continue;
10684 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10685 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10686 #ifdef INVARIANTS
10687 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10688 panic("initiate_write_inodeblock_ufs2: "
10689 "lost dep1");
10690 #endif /* INVARIANTS */
10691 dp->di_extb[i] = 0;
10692 }
10693 lastadp = NULL;
10694 break;
10695 }
10696 /*
10697 * If we have zero'ed out the last allocated block of the ext
10698 * data, roll back the size to the last currently allocated block.
10699 * We know that this last allocated block is a full-sized as
10700 * we already checked for fragments in the loop above.
10701 */
10702 if (lastadp != NULL &&
10703 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10704 for (i = lastadp->ad_offset; i >= 0; i--)
10705 if (dp->di_extb[i] != 0)
10706 break;
10707 dp->di_extsize = (i + 1) * fs->fs_bsize;
10708 }
10709 /*
10710 * Set the file data dependencies to busy.
10711 */
10712 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10713 adp = TAILQ_NEXT(adp, ad_next)) {
10714 #ifdef INVARIANTS
10715 if (deplist != 0 && prevlbn >= adp->ad_offset)
10716 panic("softdep_write_inodeblock: lbn order");
10717 if ((adp->ad_state & ATTACHED) == 0)
10718 panic("inodedep %p and adp %p not attached", inodedep, adp);
10719 prevlbn = adp->ad_offset;
10720 if (!ffs_fsfail_cleanup(ump, 0) &&
10721 adp->ad_offset < UFS_NDADDR &&
10722 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10723 panic("initiate_write_inodeblock_ufs2: "
10724 "direct pointer #%jd mismatch %jd != %jd",
10725 (intmax_t)adp->ad_offset,
10726 (intmax_t)dp->di_db[adp->ad_offset],
10727 (intmax_t)adp->ad_newblkno);
10728 if (!ffs_fsfail_cleanup(ump, 0) &&
10729 adp->ad_offset >= UFS_NDADDR &&
10730 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10731 panic("initiate_write_inodeblock_ufs2: "
10732 "indirect pointer #%jd mismatch %jd != %jd",
10733 (intmax_t)adp->ad_offset - UFS_NDADDR,
10734 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10735 (intmax_t)adp->ad_newblkno);
10736 deplist |= 1 << adp->ad_offset;
10737 if ((adp->ad_state & ATTACHED) == 0)
10738 panic("initiate_write_inodeblock_ufs2: Unknown "
10739 "state 0x%x", adp->ad_state);
10740 #endif /* INVARIANTS */
10741 adp->ad_state &= ~ATTACHED;
10742 adp->ad_state |= UNDONE;
10743 }
10744 /*
10745 * The on-disk inode cannot claim to be any larger than the last
10746 * fragment that has been written. Otherwise, the on-disk inode
10747 * might have fragments that were not the last block in the file
10748 * which would corrupt the filesystem.
10749 */
10750 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10751 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10752 if (adp->ad_offset >= UFS_NDADDR)
10753 break;
10754 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10755 /* keep going until hitting a rollback to a frag */
10756 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10757 continue;
10758 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10759 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10760 #ifdef INVARIANTS
10761 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10762 panic("initiate_write_inodeblock_ufs2: "
10763 "lost dep2");
10764 #endif /* INVARIANTS */
10765 dp->di_db[i] = 0;
10766 }
10767 for (i = 0; i < UFS_NIADDR; i++) {
10768 #ifdef INVARIANTS
10769 if (dp->di_ib[i] != 0 &&
10770 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10771 panic("initiate_write_inodeblock_ufs2: "
10772 "lost dep3");
10773 #endif /* INVARIANTS */
10774 dp->di_ib[i] = 0;
10775 }
10776 ffs_update_dinode_ckhash(fs, dp);
10777 return;
10778 }
10779 /*
10780 * If we have zero'ed out the last allocated block of the file,
10781 * roll back the size to the last currently allocated block.
10782 * We know that this last allocated block is a full-sized as
10783 * we already checked for fragments in the loop above.
10784 */
10785 if (lastadp != NULL &&
10786 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10787 for (i = lastadp->ad_offset; i >= 0; i--)
10788 if (dp->di_db[i] != 0)
10789 break;
10790 dp->di_size = (i + 1) * fs->fs_bsize;
10791 }
10792 /*
10793 * The only dependencies are for indirect blocks.
10794 *
10795 * The file size for indirect block additions is not guaranteed.
10796 * Such a guarantee would be non-trivial to achieve. The conventional
10797 * synchronous write implementation also does not make this guarantee.
10798 * Fsck should catch and fix discrepancies. Arguably, the file size
10799 * can be over-estimated without destroying integrity when the file
10800 * moves into the indirect blocks (i.e., is large). If we want to
10801 * postpone fsck, we are stuck with this argument.
10802 */
10803 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10804 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10805 ffs_update_dinode_ckhash(fs, dp);
10806 }
10807
10808 /*
10809 * Cancel an indirdep as a result of truncation. Release all of the
10810 * children allocindirs and place their journal work on the appropriate
10811 * list.
10812 */
10813 static void
cancel_indirdep(struct indirdep * indirdep,struct buf * bp,struct freeblks * freeblks)10814 cancel_indirdep(
10815 struct indirdep *indirdep,
10816 struct buf *bp,
10817 struct freeblks *freeblks)
10818 {
10819 struct allocindir *aip;
10820
10821 /*
10822 * None of the indirect pointers will ever be visible,
10823 * so they can simply be tossed. GOINGAWAY ensures
10824 * that allocated pointers will be saved in the buffer
10825 * cache until they are freed. Note that they will
10826 * only be able to be found by their physical address
10827 * since the inode mapping the logical address will
10828 * be gone. The save buffer used for the safe copy
10829 * was allocated in setup_allocindir_phase2 using
10830 * the physical address so it could be used for this
10831 * purpose. Hence we swap the safe copy with the real
10832 * copy, allowing the safe copy to be freed and holding
10833 * on to the real copy for later use in indir_trunc.
10834 */
10835 if (indirdep->ir_state & GOINGAWAY)
10836 panic("cancel_indirdep: already gone");
10837 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10838 indirdep->ir_state |= DEPCOMPLETE;
10839 LIST_REMOVE(indirdep, ir_next);
10840 }
10841 indirdep->ir_state |= GOINGAWAY;
10842 /*
10843 * Pass in bp for blocks still have journal writes
10844 * pending so we can cancel them on their own.
10845 */
10846 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
10847 cancel_allocindir(aip, bp, freeblks, 0);
10848 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
10849 cancel_allocindir(aip, NULL, freeblks, 0);
10850 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
10851 cancel_allocindir(aip, NULL, freeblks, 0);
10852 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
10853 cancel_allocindir(aip, NULL, freeblks, 0);
10854 /*
10855 * If there are pending partial truncations we need to keep the
10856 * old block copy around until they complete. This is because
10857 * the current b_data is not a perfect superset of the available
10858 * blocks.
10859 */
10860 if (TAILQ_EMPTY(&indirdep->ir_trunc))
10861 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10862 else
10863 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10864 WORKLIST_REMOVE(&indirdep->ir_list);
10865 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10866 indirdep->ir_bp = NULL;
10867 indirdep->ir_freeblks = freeblks;
10868 }
10869
10870 /*
10871 * Free an indirdep once it no longer has new pointers to track.
10872 */
10873 static void
free_indirdep(struct indirdep * indirdep)10874 free_indirdep(struct indirdep *indirdep)
10875 {
10876
10877 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10878 ("free_indirdep: Indir trunc list not empty."));
10879 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10880 ("free_indirdep: Complete head not empty."));
10881 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10882 ("free_indirdep: write head not empty."));
10883 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10884 ("free_indirdep: done head not empty."));
10885 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10886 ("free_indirdep: deplist head not empty."));
10887 KASSERT((indirdep->ir_state & DEPCOMPLETE),
10888 ("free_indirdep: %p still on newblk list.", indirdep));
10889 KASSERT(indirdep->ir_saveddata == NULL,
10890 ("free_indirdep: %p still has saved data.", indirdep));
10891 KASSERT(indirdep->ir_savebp == NULL,
10892 ("free_indirdep: %p still has savebp buffer.", indirdep));
10893 if (indirdep->ir_state & ONWORKLIST)
10894 WORKLIST_REMOVE(&indirdep->ir_list);
10895 WORKITEM_FREE(indirdep, D_INDIRDEP);
10896 }
10897
10898 /*
10899 * Called before a write to an indirdep. This routine is responsible for
10900 * rolling back pointers to a safe state which includes only those
10901 * allocindirs which have been completed.
10902 */
10903 static void
initiate_write_indirdep(struct indirdep * indirdep,struct buf * bp)10904 initiate_write_indirdep(struct indirdep *indirdep, struct buf *bp)
10905 {
10906 struct ufsmount *ump;
10907
10908 indirdep->ir_state |= IOSTARTED;
10909 if (indirdep->ir_state & GOINGAWAY)
10910 panic("disk_io_initiation: indirdep gone");
10911 /*
10912 * If there are no remaining dependencies, this will be writing
10913 * the real pointers.
10914 */
10915 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10916 TAILQ_EMPTY(&indirdep->ir_trunc))
10917 return;
10918 /*
10919 * Replace up-to-date version with safe version.
10920 */
10921 if (indirdep->ir_saveddata == NULL) {
10922 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10923 LOCK_OWNED(ump);
10924 FREE_LOCK(ump);
10925 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10926 M_SOFTDEP_FLAGS);
10927 ACQUIRE_LOCK(ump);
10928 }
10929 indirdep->ir_state &= ~ATTACHED;
10930 indirdep->ir_state |= UNDONE;
10931 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10932 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10933 bp->b_bcount);
10934 }
10935
10936 /*
10937 * Called when an inode has been cleared in a cg bitmap. This finally
10938 * eliminates any canceled jaddrefs
10939 */
10940 void
softdep_setup_inofree(struct mount * mp,struct buf * bp,ino_t ino,struct workhead * wkhd,bool doingrecovery)10941 softdep_setup_inofree(struct mount *mp,
10942 struct buf *bp,
10943 ino_t ino,
10944 struct workhead *wkhd,
10945 bool doingrecovery)
10946 {
10947 struct worklist *wk, *wkn;
10948 struct ufsmount *ump;
10949 #ifdef INVARIANTS
10950 struct inodedep *inodedep;
10951 #endif
10952
10953 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10954 ("softdep_setup_inofree called on non-softdep filesystem"));
10955 ump = VFSTOUFS(mp);
10956 ACQUIRE_LOCK(ump);
10957 KASSERT(doingrecovery || ffs_fsfail_cleanup(ump, 0) ||
10958 isclr(cg_inosused((struct cg *)bp->b_data),
10959 ino % ump->um_fs->fs_ipg),
10960 ("softdep_setup_inofree: inode %ju not freed.", (uintmax_t)ino));
10961 KASSERT(inodedep_lookup(mp, ino, 0, &inodedep) == 0,
10962 ("softdep_setup_inofree: ino %ju has existing inodedep %p",
10963 (uintmax_t)ino, inodedep));
10964 if (wkhd) {
10965 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10966 if (wk->wk_type != D_JADDREF)
10967 continue;
10968 WORKLIST_REMOVE(wk);
10969 /*
10970 * We can free immediately even if the jaddref
10971 * isn't attached in a background write as now
10972 * the bitmaps are reconciled.
10973 */
10974 wk->wk_state |= COMPLETE | ATTACHED;
10975 free_jaddref(WK_JADDREF(wk));
10976 }
10977 jwork_move(&bp->b_dep, wkhd);
10978 }
10979 FREE_LOCK(ump);
10980 }
10981
10982 /*
10983 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10984 * map. Any dependencies waiting for the write to clear are added to the
10985 * buf's list and any jnewblks that are being canceled are discarded
10986 * immediately.
10987 */
10988 void
softdep_setup_blkfree(struct mount * mp,struct buf * bp,ufs2_daddr_t blkno,int frags,struct workhead * wkhd,bool doingrecovery)10989 softdep_setup_blkfree(
10990 struct mount *mp,
10991 struct buf *bp,
10992 ufs2_daddr_t blkno,
10993 int frags,
10994 struct workhead *wkhd,
10995 bool doingrecovery)
10996 {
10997 struct bmsafemap *bmsafemap;
10998 struct jnewblk *jnewblk;
10999 struct ufsmount *ump;
11000 struct worklist *wk;
11001 struct fs *fs;
11002 #ifdef INVARIANTS
11003 uint8_t *blksfree;
11004 struct cg *cgp;
11005 ufs2_daddr_t jstart;
11006 ufs2_daddr_t jend;
11007 ufs2_daddr_t end;
11008 long bno;
11009 int i;
11010 #endif
11011
11012 CTR3(KTR_SUJ,
11013 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
11014 blkno, frags, wkhd);
11015
11016 ump = VFSTOUFS(mp);
11017 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
11018 ("softdep_setup_blkfree called on non-softdep filesystem"));
11019 ACQUIRE_LOCK(ump);
11020 /* Lookup the bmsafemap so we track when it is dirty. */
11021 fs = ump->um_fs;
11022 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11023 /*
11024 * Detach any jnewblks which have been canceled. They must linger
11025 * until the bitmap is cleared again by ffs_blkfree() to prevent
11026 * an unjournaled allocation from hitting the disk.
11027 */
11028 if (wkhd) {
11029 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11030 CTR2(KTR_SUJ,
11031 "softdep_setup_blkfree: blkno %jd wk type %d",
11032 blkno, wk->wk_type);
11033 WORKLIST_REMOVE(wk);
11034 if (wk->wk_type != D_JNEWBLK) {
11035 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
11036 continue;
11037 }
11038 jnewblk = WK_JNEWBLK(wk);
11039 KASSERT(jnewblk->jn_state & GOINGAWAY,
11040 ("softdep_setup_blkfree: jnewblk not canceled."));
11041 #ifdef INVARIANTS
11042 if (!doingrecovery && !ffs_fsfail_cleanup(ump, 0)) {
11043 /*
11044 * Assert that this block is free in the
11045 * bitmap before we discard the jnewblk.
11046 */
11047 cgp = (struct cg *)bp->b_data;
11048 blksfree = cg_blksfree(cgp);
11049 bno = dtogd(fs, jnewblk->jn_blkno);
11050 for (i = jnewblk->jn_oldfrags;
11051 i < jnewblk->jn_frags; i++) {
11052 if (isset(blksfree, bno + i))
11053 continue;
11054 panic("softdep_setup_blkfree: block "
11055 "%ju not freed.",
11056 (uintmax_t)jnewblk->jn_blkno);
11057 }
11058 }
11059 #endif
11060 /*
11061 * Even if it's not attached we can free immediately
11062 * as the new bitmap is correct.
11063 */
11064 wk->wk_state |= COMPLETE | ATTACHED;
11065 free_jnewblk(jnewblk);
11066 }
11067 }
11068
11069 #ifdef INVARIANTS
11070 /*
11071 * Assert that we are not freeing a block which has an outstanding
11072 * allocation dependency.
11073 */
11074 fs = VFSTOUFS(mp)->um_fs;
11075 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11076 end = blkno + frags;
11077 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11078 /*
11079 * Don't match against blocks that will be freed when the
11080 * background write is done.
11081 */
11082 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
11083 (COMPLETE | DEPCOMPLETE))
11084 continue;
11085 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
11086 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
11087 if ((blkno >= jstart && blkno < jend) ||
11088 (end > jstart && end <= jend)) {
11089 printf("state 0x%X %jd - %d %d dep %p\n",
11090 jnewblk->jn_state, jnewblk->jn_blkno,
11091 jnewblk->jn_oldfrags, jnewblk->jn_frags,
11092 jnewblk->jn_dep);
11093 panic("softdep_setup_blkfree: "
11094 "%jd-%jd(%d) overlaps with %jd-%jd",
11095 blkno, end, frags, jstart, jend);
11096 }
11097 }
11098 #endif
11099 FREE_LOCK(ump);
11100 }
11101
11102 /*
11103 * Revert a block allocation when the journal record that describes it
11104 * is not yet written.
11105 */
11106 static int
jnewblk_rollback(struct jnewblk * jnewblk,struct fs * fs,struct cg * cgp,uint8_t * blksfree)11107 jnewblk_rollback(
11108 struct jnewblk *jnewblk,
11109 struct fs *fs,
11110 struct cg *cgp,
11111 uint8_t *blksfree)
11112 {
11113 ufs1_daddr_t fragno;
11114 long cgbno, bbase;
11115 int frags, blk;
11116 int i;
11117
11118 frags = 0;
11119 cgbno = dtogd(fs, jnewblk->jn_blkno);
11120 /*
11121 * We have to test which frags need to be rolled back. We may
11122 * be operating on a stale copy when doing background writes.
11123 */
11124 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
11125 if (isclr(blksfree, cgbno + i))
11126 frags++;
11127 if (frags == 0)
11128 return (0);
11129 /*
11130 * This is mostly ffs_blkfree() sans some validation and
11131 * superblock updates.
11132 */
11133 if (frags == fs->fs_frag) {
11134 fragno = fragstoblks(fs, cgbno);
11135 ffs_setblock(fs, blksfree, fragno);
11136 ffs_clusteracct(fs, cgp, fragno, 1);
11137 cgp->cg_cs.cs_nbfree++;
11138 } else {
11139 cgbno += jnewblk->jn_oldfrags;
11140 bbase = cgbno - fragnum(fs, cgbno);
11141 /* Decrement the old frags. */
11142 blk = blkmap(fs, blksfree, bbase);
11143 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11144 /* Deallocate the fragment */
11145 for (i = 0; i < frags; i++)
11146 setbit(blksfree, cgbno + i);
11147 cgp->cg_cs.cs_nffree += frags;
11148 /* Add back in counts associated with the new frags */
11149 blk = blkmap(fs, blksfree, bbase);
11150 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11151 /* If a complete block has been reassembled, account for it. */
11152 fragno = fragstoblks(fs, bbase);
11153 if (ffs_isblock(fs, blksfree, fragno)) {
11154 cgp->cg_cs.cs_nffree -= fs->fs_frag;
11155 ffs_clusteracct(fs, cgp, fragno, 1);
11156 cgp->cg_cs.cs_nbfree++;
11157 }
11158 }
11159 stat_jnewblk++;
11160 jnewblk->jn_state &= ~ATTACHED;
11161 jnewblk->jn_state |= UNDONE;
11162
11163 return (frags);
11164 }
11165
11166 static void
initiate_write_bmsafemap(struct bmsafemap * bmsafemap,struct buf * bp)11167 initiate_write_bmsafemap(
11168 struct bmsafemap *bmsafemap,
11169 struct buf *bp) /* The cg block. */
11170 {
11171 struct jaddref *jaddref;
11172 struct jnewblk *jnewblk;
11173 uint8_t *inosused;
11174 uint8_t *blksfree;
11175 struct cg *cgp;
11176 struct fs *fs;
11177 ino_t ino;
11178
11179 /*
11180 * If this is a background write, we did this at the time that
11181 * the copy was made, so do not need to do it again.
11182 */
11183 if (bmsafemap->sm_state & IOSTARTED)
11184 return;
11185 bmsafemap->sm_state |= IOSTARTED;
11186 /*
11187 * Clear any inode allocations which are pending journal writes.
11188 */
11189 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11190 cgp = (struct cg *)bp->b_data;
11191 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11192 inosused = cg_inosused(cgp);
11193 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11194 ino = jaddref->ja_ino % fs->fs_ipg;
11195 if (isset(inosused, ino)) {
11196 if ((jaddref->ja_mode & IFMT) == IFDIR)
11197 cgp->cg_cs.cs_ndir--;
11198 cgp->cg_cs.cs_nifree++;
11199 clrbit(inosused, ino);
11200 jaddref->ja_state &= ~ATTACHED;
11201 jaddref->ja_state |= UNDONE;
11202 stat_jaddref++;
11203 } else
11204 panic("initiate_write_bmsafemap: inode %ju "
11205 "marked free", (uintmax_t)jaddref->ja_ino);
11206 }
11207 }
11208 /*
11209 * Clear any block allocations which are pending journal writes.
11210 */
11211 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11212 cgp = (struct cg *)bp->b_data;
11213 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11214 blksfree = cg_blksfree(cgp);
11215 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11216 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11217 continue;
11218 panic("initiate_write_bmsafemap: block %jd "
11219 "marked free", jnewblk->jn_blkno);
11220 }
11221 }
11222 /*
11223 * Move allocation lists to the written lists so they can be
11224 * cleared once the block write is complete.
11225 */
11226 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11227 inodedep, id_deps);
11228 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11229 newblk, nb_deps);
11230 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11231 wk_list);
11232 }
11233
11234 void
softdep_handle_error(struct buf * bp)11235 softdep_handle_error(struct buf *bp)
11236 {
11237 struct ufsmount *ump;
11238
11239 ump = softdep_bp_to_mp(bp);
11240 if (ump == NULL)
11241 return;
11242
11243 if (ffs_fsfail_cleanup(ump, bp->b_error)) {
11244 /*
11245 * No future writes will succeed, so the on-disk image is safe.
11246 * Pretend that this write succeeded so that the softdep state
11247 * will be cleaned up naturally.
11248 */
11249 bp->b_ioflags &= ~BIO_ERROR;
11250 bp->b_error = 0;
11251 }
11252 }
11253
11254 /*
11255 * This routine is called during the completion interrupt
11256 * service routine for a disk write (from the procedure called
11257 * by the device driver to inform the filesystem caches of
11258 * a request completion). It should be called early in this
11259 * procedure, before the block is made available to other
11260 * processes or other routines are called.
11261 *
11262 */
11263 static void
softdep_disk_write_complete(struct buf * bp)11264 softdep_disk_write_complete(
11265 struct buf *bp) /* describes the completed disk write */
11266 {
11267 struct worklist *wk;
11268 struct worklist *owk;
11269 struct ufsmount *ump;
11270 struct workhead reattach;
11271 struct freeblks *freeblks;
11272 struct buf *sbp;
11273
11274 ump = softdep_bp_to_mp(bp);
11275 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11276 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11277 "with outstanding dependencies for buffer %p", bp));
11278 if (ump == NULL)
11279 return;
11280 if ((bp->b_ioflags & BIO_ERROR) != 0)
11281 softdep_handle_error(bp);
11282 /*
11283 * If an error occurred while doing the write, then the data
11284 * has not hit the disk and the dependencies cannot be processed.
11285 * But we do have to go through and roll forward any dependencies
11286 * that were rolled back before the disk write.
11287 */
11288 sbp = NULL;
11289 ACQUIRE_LOCK(ump);
11290 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11291 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11292 switch (wk->wk_type) {
11293 case D_PAGEDEP:
11294 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11295 continue;
11296
11297 case D_INODEDEP:
11298 handle_written_inodeblock(WK_INODEDEP(wk),
11299 bp, 0);
11300 continue;
11301
11302 case D_BMSAFEMAP:
11303 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11304 bp, 0);
11305 continue;
11306
11307 case D_INDIRDEP:
11308 handle_written_indirdep(WK_INDIRDEP(wk),
11309 bp, &sbp, 0);
11310 continue;
11311 default:
11312 /* nothing to roll forward */
11313 continue;
11314 }
11315 }
11316 FREE_LOCK(ump);
11317 if (sbp)
11318 brelse(sbp);
11319 return;
11320 }
11321 LIST_INIT(&reattach);
11322
11323 /*
11324 * Ump SU lock must not be released anywhere in this code segment.
11325 */
11326 owk = NULL;
11327 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11328 WORKLIST_REMOVE(wk);
11329 atomic_add_long(&dep_write[wk->wk_type], 1);
11330 if (wk == owk)
11331 panic("duplicate worklist: %p\n", wk);
11332 owk = wk;
11333 switch (wk->wk_type) {
11334 case D_PAGEDEP:
11335 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11336 WRITESUCCEEDED))
11337 WORKLIST_INSERT(&reattach, wk);
11338 continue;
11339
11340 case D_INODEDEP:
11341 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11342 WRITESUCCEEDED))
11343 WORKLIST_INSERT(&reattach, wk);
11344 continue;
11345
11346 case D_BMSAFEMAP:
11347 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11348 WRITESUCCEEDED))
11349 WORKLIST_INSERT(&reattach, wk);
11350 continue;
11351
11352 case D_MKDIR:
11353 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11354 continue;
11355
11356 case D_ALLOCDIRECT:
11357 wk->wk_state |= COMPLETE;
11358 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11359 continue;
11360
11361 case D_ALLOCINDIR:
11362 wk->wk_state |= COMPLETE;
11363 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11364 continue;
11365
11366 case D_INDIRDEP:
11367 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11368 WRITESUCCEEDED))
11369 WORKLIST_INSERT(&reattach, wk);
11370 continue;
11371
11372 case D_FREEBLKS:
11373 wk->wk_state |= COMPLETE;
11374 freeblks = WK_FREEBLKS(wk);
11375 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11376 LIST_EMPTY(&freeblks->fb_jblkdephd))
11377 add_to_worklist(wk, WK_NODELAY);
11378 continue;
11379
11380 case D_FREEWORK:
11381 handle_written_freework(WK_FREEWORK(wk));
11382 break;
11383
11384 case D_JSEGDEP:
11385 free_jsegdep(WK_JSEGDEP(wk));
11386 continue;
11387
11388 case D_JSEG:
11389 handle_written_jseg(WK_JSEG(wk), bp);
11390 continue;
11391
11392 case D_SBDEP:
11393 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11394 WORKLIST_INSERT(&reattach, wk);
11395 continue;
11396
11397 case D_FREEDEP:
11398 free_freedep(WK_FREEDEP(wk));
11399 continue;
11400
11401 default:
11402 panic("handle_disk_write_complete: Unknown type %s",
11403 TYPENAME(wk->wk_type));
11404 /* NOTREACHED */
11405 }
11406 }
11407 /*
11408 * Reattach any requests that must be redone.
11409 */
11410 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11411 WORKLIST_REMOVE(wk);
11412 WORKLIST_INSERT(&bp->b_dep, wk);
11413 }
11414 FREE_LOCK(ump);
11415 if (sbp)
11416 brelse(sbp);
11417 }
11418
11419 /*
11420 * Called from within softdep_disk_write_complete above.
11421 */
11422 static void
handle_allocdirect_partdone(struct allocdirect * adp,struct workhead * wkhd)11423 handle_allocdirect_partdone(
11424 struct allocdirect *adp, /* the completed allocdirect */
11425 struct workhead *wkhd) /* Work to do when inode is writtne. */
11426 {
11427 struct allocdirectlst *listhead;
11428 struct allocdirect *listadp;
11429 struct inodedep *inodedep;
11430 long bsize;
11431
11432 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11433 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11434 return;
11435 /*
11436 * The on-disk inode cannot claim to be any larger than the last
11437 * fragment that has been written. Otherwise, the on-disk inode
11438 * might have fragments that were not the last block in the file
11439 * which would corrupt the filesystem. Thus, we cannot free any
11440 * allocdirects after one whose ad_oldblkno claims a fragment as
11441 * these blocks must be rolled back to zero before writing the inode.
11442 * We check the currently active set of allocdirects in id_inoupdt
11443 * or id_extupdt as appropriate.
11444 */
11445 inodedep = adp->ad_inodedep;
11446 bsize = inodedep->id_fs->fs_bsize;
11447 if (adp->ad_state & EXTDATA)
11448 listhead = &inodedep->id_extupdt;
11449 else
11450 listhead = &inodedep->id_inoupdt;
11451 TAILQ_FOREACH(listadp, listhead, ad_next) {
11452 /* found our block */
11453 if (listadp == adp)
11454 break;
11455 /* continue if ad_oldlbn is not a fragment */
11456 if (listadp->ad_oldsize == 0 ||
11457 listadp->ad_oldsize == bsize)
11458 continue;
11459 /* hit a fragment */
11460 return;
11461 }
11462 /*
11463 * If we have reached the end of the current list without
11464 * finding the just finished dependency, then it must be
11465 * on the future dependency list. Future dependencies cannot
11466 * be freed until they are moved to the current list.
11467 */
11468 if (listadp == NULL) {
11469 #ifdef INVARIANTS
11470 if (adp->ad_state & EXTDATA)
11471 listhead = &inodedep->id_newextupdt;
11472 else
11473 listhead = &inodedep->id_newinoupdt;
11474 TAILQ_FOREACH(listadp, listhead, ad_next)
11475 /* found our block */
11476 if (listadp == adp)
11477 break;
11478 if (listadp == NULL)
11479 panic("handle_allocdirect_partdone: lost dep");
11480 #endif /* INVARIANTS */
11481 return;
11482 }
11483 /*
11484 * If we have found the just finished dependency, then queue
11485 * it along with anything that follows it that is complete.
11486 * Since the pointer has not yet been written in the inode
11487 * as the dependency prevents it, place the allocdirect on the
11488 * bufwait list where it will be freed once the pointer is
11489 * valid.
11490 */
11491 if (wkhd == NULL)
11492 wkhd = &inodedep->id_bufwait;
11493 for (; adp; adp = listadp) {
11494 listadp = TAILQ_NEXT(adp, ad_next);
11495 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11496 return;
11497 TAILQ_REMOVE(listhead, adp, ad_next);
11498 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11499 }
11500 }
11501
11502 /*
11503 * Called from within softdep_disk_write_complete above. This routine
11504 * completes successfully written allocindirs.
11505 */
11506 static void
handle_allocindir_partdone(struct allocindir * aip)11507 handle_allocindir_partdone(
11508 struct allocindir *aip) /* the completed allocindir */
11509 {
11510 struct indirdep *indirdep;
11511
11512 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11513 return;
11514 indirdep = aip->ai_indirdep;
11515 LIST_REMOVE(aip, ai_next);
11516 /*
11517 * Don't set a pointer while the buffer is undergoing IO or while
11518 * we have active truncations.
11519 */
11520 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11521 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11522 return;
11523 }
11524 if (indirdep->ir_state & UFS1FMT)
11525 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11526 aip->ai_newblkno;
11527 else
11528 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11529 aip->ai_newblkno;
11530 /*
11531 * Await the pointer write before freeing the allocindir.
11532 */
11533 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11534 }
11535
11536 /*
11537 * Release segments held on a jwork list.
11538 */
11539 static void
handle_jwork(struct workhead * wkhd)11540 handle_jwork(struct workhead *wkhd)
11541 {
11542 struct worklist *wk;
11543
11544 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11545 WORKLIST_REMOVE(wk);
11546 switch (wk->wk_type) {
11547 case D_JSEGDEP:
11548 free_jsegdep(WK_JSEGDEP(wk));
11549 continue;
11550 case D_FREEDEP:
11551 free_freedep(WK_FREEDEP(wk));
11552 continue;
11553 case D_FREEFRAG:
11554 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11555 WORKITEM_FREE(wk, D_FREEFRAG);
11556 continue;
11557 case D_FREEWORK:
11558 handle_written_freework(WK_FREEWORK(wk));
11559 continue;
11560 default:
11561 panic("handle_jwork: Unknown type %s\n",
11562 TYPENAME(wk->wk_type));
11563 }
11564 }
11565 }
11566
11567 /*
11568 * Handle the bufwait list on an inode when it is safe to release items
11569 * held there. This normally happens after an inode block is written but
11570 * may be delayed and handled later if there are pending journal items that
11571 * are not yet safe to be released.
11572 */
11573 static struct freefile *
handle_bufwait(struct inodedep * inodedep,struct workhead * refhd)11574 handle_bufwait(
11575 struct inodedep *inodedep,
11576 struct workhead *refhd)
11577 {
11578 struct jaddref *jaddref;
11579 struct freefile *freefile;
11580 struct worklist *wk;
11581
11582 freefile = NULL;
11583 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11584 WORKLIST_REMOVE(wk);
11585 switch (wk->wk_type) {
11586 case D_FREEFILE:
11587 /*
11588 * We defer adding freefile to the worklist
11589 * until all other additions have been made to
11590 * ensure that it will be done after all the
11591 * old blocks have been freed.
11592 */
11593 if (freefile != NULL)
11594 panic("handle_bufwait: freefile");
11595 freefile = WK_FREEFILE(wk);
11596 continue;
11597
11598 case D_MKDIR:
11599 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11600 continue;
11601
11602 case D_DIRADD:
11603 diradd_inode_written(WK_DIRADD(wk), inodedep);
11604 continue;
11605
11606 case D_FREEFRAG:
11607 wk->wk_state |= COMPLETE;
11608 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11609 add_to_worklist(wk, 0);
11610 continue;
11611
11612 case D_DIRREM:
11613 wk->wk_state |= COMPLETE;
11614 add_to_worklist(wk, 0);
11615 continue;
11616
11617 case D_ALLOCDIRECT:
11618 case D_ALLOCINDIR:
11619 free_newblk(WK_NEWBLK(wk));
11620 continue;
11621
11622 case D_JNEWBLK:
11623 wk->wk_state |= COMPLETE;
11624 free_jnewblk(WK_JNEWBLK(wk));
11625 continue;
11626
11627 /*
11628 * Save freed journal segments and add references on
11629 * the supplied list which will delay their release
11630 * until the cg bitmap is cleared on disk.
11631 */
11632 case D_JSEGDEP:
11633 if (refhd == NULL)
11634 free_jsegdep(WK_JSEGDEP(wk));
11635 else
11636 WORKLIST_INSERT(refhd, wk);
11637 continue;
11638
11639 case D_JADDREF:
11640 jaddref = WK_JADDREF(wk);
11641 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11642 if_deps);
11643 /*
11644 * Transfer any jaddrefs to the list to be freed with
11645 * the bitmap if we're handling a removed file.
11646 */
11647 if (refhd == NULL) {
11648 wk->wk_state |= COMPLETE;
11649 free_jaddref(jaddref);
11650 } else
11651 WORKLIST_INSERT(refhd, wk);
11652 continue;
11653
11654 default:
11655 panic("handle_bufwait: Unknown type %p(%s)",
11656 wk, TYPENAME(wk->wk_type));
11657 /* NOTREACHED */
11658 }
11659 }
11660 return (freefile);
11661 }
11662 /*
11663 * Called from within softdep_disk_write_complete above to restore
11664 * in-memory inode block contents to their most up-to-date state. Note
11665 * that this routine is always called from interrupt level with further
11666 * interrupts from this device blocked.
11667 *
11668 * If the write did not succeed, we will do all the roll-forward
11669 * operations, but we will not take the actions that will allow its
11670 * dependencies to be processed.
11671 */
11672 static int
handle_written_inodeblock(struct inodedep * inodedep,struct buf * bp,int flags)11673 handle_written_inodeblock(
11674 struct inodedep *inodedep,
11675 struct buf *bp, /* buffer containing the inode block */
11676 int flags)
11677 {
11678 struct freefile *freefile;
11679 struct allocdirect *adp, *nextadp;
11680 struct ufs1_dinode *dp1 = NULL;
11681 struct ufs2_dinode *dp2 = NULL;
11682 struct workhead wkhd;
11683 int hadchanges, fstype;
11684 ino_t freelink;
11685
11686 LIST_INIT(&wkhd);
11687 hadchanges = 0;
11688 freefile = NULL;
11689 if ((inodedep->id_state & IOSTARTED) == 0)
11690 panic("handle_written_inodeblock: not started");
11691 inodedep->id_state &= ~IOSTARTED;
11692 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11693 fstype = UFS1;
11694 dp1 = (struct ufs1_dinode *)bp->b_data +
11695 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11696 freelink = dp1->di_freelink;
11697 } else {
11698 fstype = UFS2;
11699 dp2 = (struct ufs2_dinode *)bp->b_data +
11700 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11701 freelink = dp2->di_freelink;
11702 }
11703 /*
11704 * Leave this inodeblock dirty until it's in the list.
11705 */
11706 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11707 (flags & WRITESUCCEEDED)) {
11708 struct inodedep *inon;
11709
11710 inon = TAILQ_NEXT(inodedep, id_unlinked);
11711 if ((inon == NULL && freelink == 0) ||
11712 (inon && inon->id_ino == freelink)) {
11713 if (inon)
11714 inon->id_state |= UNLINKPREV;
11715 inodedep->id_state |= UNLINKNEXT;
11716 }
11717 hadchanges = 1;
11718 }
11719 /*
11720 * If we had to rollback the inode allocation because of
11721 * bitmaps being incomplete, then simply restore it.
11722 * Keep the block dirty so that it will not be reclaimed until
11723 * all associated dependencies have been cleared and the
11724 * corresponding updates written to disk.
11725 */
11726 if (inodedep->id_savedino1 != NULL) {
11727 hadchanges = 1;
11728 if (fstype == UFS1)
11729 *dp1 = *inodedep->id_savedino1;
11730 else
11731 *dp2 = *inodedep->id_savedino2;
11732 free(inodedep->id_savedino1, M_SAVEDINO);
11733 inodedep->id_savedino1 = NULL;
11734 if ((bp->b_flags & B_DELWRI) == 0)
11735 stat_inode_bitmap++;
11736 bdirty(bp);
11737 /*
11738 * If the inode is clear here and GOINGAWAY it will never
11739 * be written. Process the bufwait and clear any pending
11740 * work which may include the freefile.
11741 */
11742 if (inodedep->id_state & GOINGAWAY)
11743 goto bufwait;
11744 return (1);
11745 }
11746 if (flags & WRITESUCCEEDED)
11747 inodedep->id_state |= COMPLETE;
11748 /*
11749 * Roll forward anything that had to be rolled back before
11750 * the inode could be updated.
11751 */
11752 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11753 nextadp = TAILQ_NEXT(adp, ad_next);
11754 if (adp->ad_state & ATTACHED)
11755 panic("handle_written_inodeblock: new entry");
11756 if (fstype == UFS1) {
11757 if (adp->ad_offset < UFS_NDADDR) {
11758 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11759 panic("%s %s #%jd mismatch %d != %jd",
11760 "handle_written_inodeblock:",
11761 "direct pointer",
11762 (intmax_t)adp->ad_offset,
11763 dp1->di_db[adp->ad_offset],
11764 (intmax_t)adp->ad_oldblkno);
11765 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11766 } else {
11767 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11768 0)
11769 panic("%s: %s #%jd allocated as %d",
11770 "handle_written_inodeblock",
11771 "indirect pointer",
11772 (intmax_t)adp->ad_offset -
11773 UFS_NDADDR,
11774 dp1->di_ib[adp->ad_offset -
11775 UFS_NDADDR]);
11776 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11777 adp->ad_newblkno;
11778 }
11779 } else {
11780 if (adp->ad_offset < UFS_NDADDR) {
11781 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11782 panic("%s: %s #%jd %s %jd != %jd",
11783 "handle_written_inodeblock",
11784 "direct pointer",
11785 (intmax_t)adp->ad_offset, "mismatch",
11786 (intmax_t)dp2->di_db[adp->ad_offset],
11787 (intmax_t)adp->ad_oldblkno);
11788 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11789 } else {
11790 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11791 0)
11792 panic("%s: %s #%jd allocated as %jd",
11793 "handle_written_inodeblock",
11794 "indirect pointer",
11795 (intmax_t)adp->ad_offset -
11796 UFS_NDADDR,
11797 (intmax_t)
11798 dp2->di_ib[adp->ad_offset -
11799 UFS_NDADDR]);
11800 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11801 adp->ad_newblkno;
11802 }
11803 }
11804 adp->ad_state &= ~UNDONE;
11805 adp->ad_state |= ATTACHED;
11806 hadchanges = 1;
11807 }
11808 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11809 nextadp = TAILQ_NEXT(adp, ad_next);
11810 if (adp->ad_state & ATTACHED)
11811 panic("handle_written_inodeblock: new entry");
11812 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11813 panic("%s: direct pointers #%jd %s %jd != %jd",
11814 "handle_written_inodeblock",
11815 (intmax_t)adp->ad_offset, "mismatch",
11816 (intmax_t)dp2->di_extb[adp->ad_offset],
11817 (intmax_t)adp->ad_oldblkno);
11818 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11819 adp->ad_state &= ~UNDONE;
11820 adp->ad_state |= ATTACHED;
11821 hadchanges = 1;
11822 }
11823 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11824 stat_direct_blk_ptrs++;
11825 /*
11826 * Reset the file size to its most up-to-date value.
11827 */
11828 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11829 panic("handle_written_inodeblock: bad size");
11830 if (inodedep->id_savednlink > UFS_LINK_MAX)
11831 panic("handle_written_inodeblock: Invalid link count "
11832 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11833 inodedep);
11834 if (fstype == UFS1) {
11835 if (dp1->di_nlink != inodedep->id_savednlink) {
11836 dp1->di_nlink = inodedep->id_savednlink;
11837 hadchanges = 1;
11838 }
11839 if (dp1->di_size != inodedep->id_savedsize) {
11840 dp1->di_size = inodedep->id_savedsize;
11841 hadchanges = 1;
11842 }
11843 } else {
11844 if (dp2->di_nlink != inodedep->id_savednlink) {
11845 dp2->di_nlink = inodedep->id_savednlink;
11846 hadchanges = 1;
11847 }
11848 if (dp2->di_size != inodedep->id_savedsize) {
11849 dp2->di_size = inodedep->id_savedsize;
11850 hadchanges = 1;
11851 }
11852 if (dp2->di_extsize != inodedep->id_savedextsize) {
11853 dp2->di_extsize = inodedep->id_savedextsize;
11854 hadchanges = 1;
11855 }
11856 }
11857 inodedep->id_savedsize = -1;
11858 inodedep->id_savedextsize = -1;
11859 inodedep->id_savednlink = -1;
11860 /*
11861 * If there were any rollbacks in the inode block, then it must be
11862 * marked dirty so that its will eventually get written back in
11863 * its correct form.
11864 */
11865 if (hadchanges) {
11866 if (fstype == UFS2)
11867 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
11868 bdirty(bp);
11869 }
11870 bufwait:
11871 /*
11872 * If the write did not succeed, we have done all the roll-forward
11873 * operations, but we cannot take the actions that will allow its
11874 * dependencies to be processed.
11875 */
11876 if ((flags & WRITESUCCEEDED) == 0)
11877 return (hadchanges);
11878 /*
11879 * Process any allocdirects that completed during the update.
11880 */
11881 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11882 handle_allocdirect_partdone(adp, &wkhd);
11883 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11884 handle_allocdirect_partdone(adp, &wkhd);
11885 /*
11886 * Process deallocations that were held pending until the
11887 * inode had been written to disk. Freeing of the inode
11888 * is delayed until after all blocks have been freed to
11889 * avoid creation of new <vfsid, inum, lbn> triples
11890 * before the old ones have been deleted. Completely
11891 * unlinked inodes are not processed until the unlinked
11892 * inode list is written or the last reference is removed.
11893 */
11894 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11895 freefile = handle_bufwait(inodedep, NULL);
11896 if (freefile && !LIST_EMPTY(&wkhd)) {
11897 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11898 freefile = NULL;
11899 }
11900 }
11901 /*
11902 * Move rolled forward dependency completions to the bufwait list
11903 * now that those that were already written have been processed.
11904 */
11905 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11906 panic("handle_written_inodeblock: bufwait but no changes");
11907 jwork_move(&inodedep->id_bufwait, &wkhd);
11908
11909 if (freefile != NULL) {
11910 /*
11911 * If the inode is goingaway it was never written. Fake up
11912 * the state here so free_inodedep() can succeed.
11913 */
11914 if (inodedep->id_state & GOINGAWAY)
11915 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11916 if (free_inodedep(inodedep) == 0)
11917 panic("handle_written_inodeblock: live inodedep %p",
11918 inodedep);
11919 add_to_worklist(&freefile->fx_list, 0);
11920 return (0);
11921 }
11922
11923 /*
11924 * If no outstanding dependencies, free it.
11925 */
11926 if (free_inodedep(inodedep) ||
11927 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11928 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11929 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11930 LIST_FIRST(&inodedep->id_bufwait) == 0))
11931 return (0);
11932 return (hadchanges);
11933 }
11934
11935 /*
11936 * Perform needed roll-forwards and kick off any dependencies that
11937 * can now be processed.
11938 *
11939 * If the write did not succeed, we will do all the roll-forward
11940 * operations, but we will not take the actions that will allow its
11941 * dependencies to be processed.
11942 */
11943 static int
handle_written_indirdep(struct indirdep * indirdep,struct buf * bp,struct buf ** bpp,int flags)11944 handle_written_indirdep(
11945 struct indirdep *indirdep,
11946 struct buf *bp,
11947 struct buf **bpp,
11948 int flags)
11949 {
11950 struct allocindir *aip;
11951 struct buf *sbp;
11952 int chgs;
11953
11954 if (indirdep->ir_state & GOINGAWAY)
11955 panic("handle_written_indirdep: indirdep gone");
11956 if ((indirdep->ir_state & IOSTARTED) == 0)
11957 panic("handle_written_indirdep: IO not started");
11958 chgs = 0;
11959 /*
11960 * If there were rollbacks revert them here.
11961 */
11962 if (indirdep->ir_saveddata) {
11963 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11964 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11965 free(indirdep->ir_saveddata, M_INDIRDEP);
11966 indirdep->ir_saveddata = NULL;
11967 }
11968 chgs = 1;
11969 }
11970 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11971 indirdep->ir_state |= ATTACHED;
11972 /*
11973 * If the write did not succeed, we have done all the roll-forward
11974 * operations, but we cannot take the actions that will allow its
11975 * dependencies to be processed.
11976 */
11977 if ((flags & WRITESUCCEEDED) == 0) {
11978 stat_indir_blk_ptrs++;
11979 bdirty(bp);
11980 return (1);
11981 }
11982 /*
11983 * Move allocindirs with written pointers to the completehd if
11984 * the indirdep's pointer is not yet written. Otherwise
11985 * free them here.
11986 */
11987 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
11988 LIST_REMOVE(aip, ai_next);
11989 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11990 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11991 ai_next);
11992 newblk_freefrag(&aip->ai_block);
11993 continue;
11994 }
11995 free_newblk(&aip->ai_block);
11996 }
11997 /*
11998 * Move allocindirs that have finished dependency processing from
11999 * the done list to the write list after updating the pointers.
12000 */
12001 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12002 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
12003 handle_allocindir_partdone(aip);
12004 if (aip == LIST_FIRST(&indirdep->ir_donehd))
12005 panic("disk_write_complete: not gone");
12006 chgs = 1;
12007 }
12008 }
12009 /*
12010 * Preserve the indirdep if there were any changes or if it is not
12011 * yet valid on disk.
12012 */
12013 if (chgs) {
12014 stat_indir_blk_ptrs++;
12015 bdirty(bp);
12016 return (1);
12017 }
12018 /*
12019 * If there were no changes we can discard the savedbp and detach
12020 * ourselves from the buf. We are only carrying completed pointers
12021 * in this case.
12022 */
12023 sbp = indirdep->ir_savebp;
12024 sbp->b_flags |= B_INVAL | B_NOCACHE;
12025 indirdep->ir_savebp = NULL;
12026 indirdep->ir_bp = NULL;
12027 if (*bpp != NULL)
12028 panic("handle_written_indirdep: bp already exists.");
12029 *bpp = sbp;
12030 /*
12031 * The indirdep may not be freed until its parent points at it.
12032 */
12033 if (indirdep->ir_state & DEPCOMPLETE)
12034 free_indirdep(indirdep);
12035
12036 return (0);
12037 }
12038
12039 /*
12040 * Process a diradd entry after its dependent inode has been written.
12041 */
12042 static void
diradd_inode_written(struct diradd * dap,struct inodedep * inodedep)12043 diradd_inode_written(
12044 struct diradd *dap,
12045 struct inodedep *inodedep)
12046 {
12047
12048 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
12049 dap->da_state |= COMPLETE;
12050 complete_diradd(dap);
12051 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
12052 }
12053
12054 /*
12055 * Returns true if the bmsafemap will have rollbacks when written. Must only
12056 * be called with the per-filesystem lock and the buf lock on the cg held.
12057 */
12058 static int
bmsafemap_backgroundwrite(struct bmsafemap * bmsafemap,struct buf * bp)12059 bmsafemap_backgroundwrite(
12060 struct bmsafemap *bmsafemap,
12061 struct buf *bp)
12062 {
12063 int dirty;
12064
12065 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
12066 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
12067 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
12068 /*
12069 * If we're initiating a background write we need to process the
12070 * rollbacks as they exist now, not as they exist when IO starts.
12071 * No other consumers will look at the contents of the shadowed
12072 * buf so this is safe to do here.
12073 */
12074 if (bp->b_xflags & BX_BKGRDMARKER)
12075 initiate_write_bmsafemap(bmsafemap, bp);
12076
12077 return (dirty);
12078 }
12079
12080 /*
12081 * Re-apply an allocation when a cg write is complete.
12082 */
12083 static int
jnewblk_rollforward(struct jnewblk * jnewblk,struct fs * fs,struct cg * cgp,uint8_t * blksfree)12084 jnewblk_rollforward(
12085 struct jnewblk *jnewblk,
12086 struct fs *fs,
12087 struct cg *cgp,
12088 uint8_t *blksfree)
12089 {
12090 ufs1_daddr_t fragno;
12091 ufs2_daddr_t blkno;
12092 long cgbno, bbase;
12093 int frags, blk;
12094 int i;
12095
12096 frags = 0;
12097 cgbno = dtogd(fs, jnewblk->jn_blkno);
12098 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
12099 if (isclr(blksfree, cgbno + i))
12100 panic("jnewblk_rollforward: re-allocated fragment");
12101 frags++;
12102 }
12103 if (frags == fs->fs_frag) {
12104 blkno = fragstoblks(fs, cgbno);
12105 ffs_clrblock(fs, blksfree, (long)blkno);
12106 ffs_clusteracct(fs, cgp, blkno, -1);
12107 cgp->cg_cs.cs_nbfree--;
12108 } else {
12109 bbase = cgbno - fragnum(fs, cgbno);
12110 cgbno += jnewblk->jn_oldfrags;
12111 /* If a complete block had been reassembled, account for it. */
12112 fragno = fragstoblks(fs, bbase);
12113 if (ffs_isblock(fs, blksfree, fragno)) {
12114 cgp->cg_cs.cs_nffree += fs->fs_frag;
12115 ffs_clusteracct(fs, cgp, fragno, -1);
12116 cgp->cg_cs.cs_nbfree--;
12117 }
12118 /* Decrement the old frags. */
12119 blk = blkmap(fs, blksfree, bbase);
12120 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12121 /* Allocate the fragment */
12122 for (i = 0; i < frags; i++)
12123 clrbit(blksfree, cgbno + i);
12124 cgp->cg_cs.cs_nffree -= frags;
12125 /* Add back in counts associated with the new frags */
12126 blk = blkmap(fs, blksfree, bbase);
12127 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12128 }
12129 return (frags);
12130 }
12131
12132 /*
12133 * Complete a write to a bmsafemap structure. Roll forward any bitmap
12134 * changes if it's not a background write. Set all written dependencies
12135 * to DEPCOMPLETE and free the structure if possible.
12136 *
12137 * If the write did not succeed, we will do all the roll-forward
12138 * operations, but we will not take the actions that will allow its
12139 * dependencies to be processed.
12140 */
12141 static int
handle_written_bmsafemap(struct bmsafemap * bmsafemap,struct buf * bp,int flags)12142 handle_written_bmsafemap(
12143 struct bmsafemap *bmsafemap,
12144 struct buf *bp,
12145 int flags)
12146 {
12147 struct newblk *newblk;
12148 struct inodedep *inodedep;
12149 struct jaddref *jaddref, *jatmp;
12150 struct jnewblk *jnewblk, *jntmp;
12151 struct ufsmount *ump;
12152 uint8_t *inosused;
12153 uint8_t *blksfree;
12154 struct cg *cgp;
12155 struct fs *fs;
12156 ino_t ino;
12157 int foreground;
12158 int chgs;
12159
12160 if ((bmsafemap->sm_state & IOSTARTED) == 0)
12161 panic("handle_written_bmsafemap: Not started\n");
12162 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
12163 chgs = 0;
12164 bmsafemap->sm_state &= ~IOSTARTED;
12165 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
12166 /*
12167 * If write was successful, release journal work that was waiting
12168 * on the write. Otherwise move the work back.
12169 */
12170 if (flags & WRITESUCCEEDED)
12171 handle_jwork(&bmsafemap->sm_freewr);
12172 else
12173 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12174 worklist, wk_list);
12175
12176 /*
12177 * Restore unwritten inode allocation pending jaddref writes.
12178 */
12179 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12180 cgp = (struct cg *)bp->b_data;
12181 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12182 inosused = cg_inosused(cgp);
12183 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12184 ja_bmdeps, jatmp) {
12185 if ((jaddref->ja_state & UNDONE) == 0)
12186 continue;
12187 ino = jaddref->ja_ino % fs->fs_ipg;
12188 if (isset(inosused, ino))
12189 panic("handle_written_bmsafemap: "
12190 "re-allocated inode");
12191 /* Do the roll-forward only if it's a real copy. */
12192 if (foreground) {
12193 if ((jaddref->ja_mode & IFMT) == IFDIR)
12194 cgp->cg_cs.cs_ndir++;
12195 cgp->cg_cs.cs_nifree--;
12196 setbit(inosused, ino);
12197 chgs = 1;
12198 }
12199 jaddref->ja_state &= ~UNDONE;
12200 jaddref->ja_state |= ATTACHED;
12201 free_jaddref(jaddref);
12202 }
12203 }
12204 /*
12205 * Restore any block allocations which are pending journal writes.
12206 */
12207 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12208 cgp = (struct cg *)bp->b_data;
12209 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12210 blksfree = cg_blksfree(cgp);
12211 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12212 jntmp) {
12213 if ((jnewblk->jn_state & UNDONE) == 0)
12214 continue;
12215 /* Do the roll-forward only if it's a real copy. */
12216 if (foreground &&
12217 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12218 chgs = 1;
12219 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12220 jnewblk->jn_state |= ATTACHED;
12221 free_jnewblk(jnewblk);
12222 }
12223 }
12224 /*
12225 * If the write did not succeed, we have done all the roll-forward
12226 * operations, but we cannot take the actions that will allow its
12227 * dependencies to be processed.
12228 */
12229 if ((flags & WRITESUCCEEDED) == 0) {
12230 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12231 newblk, nb_deps);
12232 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12233 worklist, wk_list);
12234 if (foreground)
12235 bdirty(bp);
12236 return (1);
12237 }
12238 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12239 newblk->nb_state |= DEPCOMPLETE;
12240 newblk->nb_state &= ~ONDEPLIST;
12241 newblk->nb_bmsafemap = NULL;
12242 LIST_REMOVE(newblk, nb_deps);
12243 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12244 handle_allocdirect_partdone(
12245 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12246 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12247 handle_allocindir_partdone(
12248 WK_ALLOCINDIR(&newblk->nb_list));
12249 else if (newblk->nb_list.wk_type != D_NEWBLK)
12250 panic("handle_written_bmsafemap: Unexpected type: %s",
12251 TYPENAME(newblk->nb_list.wk_type));
12252 }
12253 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12254 inodedep->id_state |= DEPCOMPLETE;
12255 inodedep->id_state &= ~ONDEPLIST;
12256 LIST_REMOVE(inodedep, id_deps);
12257 inodedep->id_bmsafemap = NULL;
12258 }
12259 LIST_REMOVE(bmsafemap, sm_next);
12260 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12261 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12262 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12263 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12264 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12265 LIST_REMOVE(bmsafemap, sm_hash);
12266 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12267 return (0);
12268 }
12269 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12270 if (foreground)
12271 bdirty(bp);
12272 return (1);
12273 }
12274
12275 /*
12276 * Try to free a mkdir dependency.
12277 */
12278 static void
complete_mkdir(struct mkdir * mkdir)12279 complete_mkdir(struct mkdir *mkdir)
12280 {
12281 struct diradd *dap;
12282
12283 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12284 return;
12285 LIST_REMOVE(mkdir, md_mkdirs);
12286 dap = mkdir->md_diradd;
12287 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12288 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12289 dap->da_state |= DEPCOMPLETE;
12290 complete_diradd(dap);
12291 }
12292 WORKITEM_FREE(mkdir, D_MKDIR);
12293 }
12294
12295 /*
12296 * Handle the completion of a mkdir dependency.
12297 */
12298 static void
handle_written_mkdir(struct mkdir * mkdir,int type)12299 handle_written_mkdir(struct mkdir *mkdir, int type)
12300 {
12301
12302 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12303 panic("handle_written_mkdir: bad type");
12304 mkdir->md_state |= COMPLETE;
12305 complete_mkdir(mkdir);
12306 }
12307
12308 static int
free_pagedep(struct pagedep * pagedep)12309 free_pagedep(struct pagedep *pagedep)
12310 {
12311 int i;
12312
12313 if (pagedep->pd_state & NEWBLOCK)
12314 return (0);
12315 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12316 return (0);
12317 for (i = 0; i < DAHASHSZ; i++)
12318 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12319 return (0);
12320 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12321 return (0);
12322 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12323 return (0);
12324 if (pagedep->pd_state & ONWORKLIST)
12325 WORKLIST_REMOVE(&pagedep->pd_list);
12326 LIST_REMOVE(pagedep, pd_hash);
12327 WORKITEM_FREE(pagedep, D_PAGEDEP);
12328
12329 return (1);
12330 }
12331
12332 /*
12333 * Called from within softdep_disk_write_complete above.
12334 * A write operation was just completed. Removed inodes can
12335 * now be freed and associated block pointers may be committed.
12336 * Note that this routine is always called from interrupt level
12337 * with further interrupts from this device blocked.
12338 *
12339 * If the write did not succeed, we will do all the roll-forward
12340 * operations, but we will not take the actions that will allow its
12341 * dependencies to be processed.
12342 */
12343 static int
handle_written_filepage(struct pagedep * pagedep,struct buf * bp,int flags)12344 handle_written_filepage(
12345 struct pagedep *pagedep,
12346 struct buf *bp, /* buffer containing the written page */
12347 int flags)
12348 {
12349 struct dirrem *dirrem;
12350 struct diradd *dap, *nextdap;
12351 struct direct *ep;
12352 int i, chgs;
12353
12354 if ((pagedep->pd_state & IOSTARTED) == 0)
12355 panic("handle_written_filepage: not started");
12356 pagedep->pd_state &= ~IOSTARTED;
12357 if ((flags & WRITESUCCEEDED) == 0)
12358 goto rollforward;
12359 /*
12360 * Process any directory removals that have been committed.
12361 */
12362 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12363 LIST_REMOVE(dirrem, dm_next);
12364 dirrem->dm_state |= COMPLETE;
12365 dirrem->dm_dirinum = pagedep->pd_ino;
12366 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12367 ("handle_written_filepage: Journal entries not written."));
12368 add_to_worklist(&dirrem->dm_list, 0);
12369 }
12370 /*
12371 * Free any directory additions that have been committed.
12372 * If it is a newly allocated block, we have to wait until
12373 * the on-disk directory inode claims the new block.
12374 */
12375 if ((pagedep->pd_state & NEWBLOCK) == 0)
12376 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12377 free_diradd(dap, NULL);
12378 rollforward:
12379 /*
12380 * Uncommitted directory entries must be restored.
12381 */
12382 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12383 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12384 dap = nextdap) {
12385 nextdap = LIST_NEXT(dap, da_pdlist);
12386 if (dap->da_state & ATTACHED)
12387 panic("handle_written_filepage: attached");
12388 ep = (struct direct *)
12389 ((char *)bp->b_data + dap->da_offset);
12390 ep->d_ino = dap->da_newinum;
12391 dap->da_state &= ~UNDONE;
12392 dap->da_state |= ATTACHED;
12393 chgs = 1;
12394 /*
12395 * If the inode referenced by the directory has
12396 * been written out, then the dependency can be
12397 * moved to the pending list.
12398 */
12399 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12400 LIST_REMOVE(dap, da_pdlist);
12401 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12402 da_pdlist);
12403 }
12404 }
12405 }
12406 /*
12407 * If there were any rollbacks in the directory, then it must be
12408 * marked dirty so that its will eventually get written back in
12409 * its correct form.
12410 */
12411 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12412 if ((bp->b_flags & B_DELWRI) == 0)
12413 stat_dir_entry++;
12414 bdirty(bp);
12415 return (1);
12416 }
12417 /*
12418 * If we are not waiting for a new directory block to be
12419 * claimed by its inode, then the pagedep will be freed.
12420 * Otherwise it will remain to track any new entries on
12421 * the page in case they are fsync'ed.
12422 */
12423 free_pagedep(pagedep);
12424 return (0);
12425 }
12426
12427 /*
12428 * Writing back in-core inode structures.
12429 *
12430 * The filesystem only accesses an inode's contents when it occupies an
12431 * "in-core" inode structure. These "in-core" structures are separate from
12432 * the page frames used to cache inode blocks. Only the latter are
12433 * transferred to/from the disk. So, when the updated contents of the
12434 * "in-core" inode structure are copied to the corresponding in-memory inode
12435 * block, the dependencies are also transferred. The following procedure is
12436 * called when copying a dirty "in-core" inode to a cached inode block.
12437 */
12438
12439 /*
12440 * Called when an inode is loaded from disk. If the effective link count
12441 * differed from the actual link count when it was last flushed, then we
12442 * need to ensure that the correct effective link count is put back.
12443 */
12444 void
softdep_load_inodeblock(struct inode * ip)12445 softdep_load_inodeblock(
12446 struct inode *ip) /* the "in_core" copy of the inode */
12447 {
12448 struct inodedep *inodedep;
12449 struct ufsmount *ump;
12450
12451 ump = ITOUMP(ip);
12452 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12453 ("softdep_load_inodeblock called on non-softdep filesystem"));
12454 /*
12455 * Check for alternate nlink count.
12456 */
12457 ip->i_effnlink = ip->i_nlink;
12458 ACQUIRE_LOCK(ump);
12459 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12460 FREE_LOCK(ump);
12461 return;
12462 }
12463 if (ip->i_nlink != inodedep->id_nlinkwrote &&
12464 inodedep->id_nlinkwrote != -1) {
12465 KASSERT(ip->i_nlink == 0 &&
12466 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0,
12467 ("read bad i_nlink value"));
12468 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote;
12469 }
12470 ip->i_effnlink -= inodedep->id_nlinkdelta;
12471 KASSERT(ip->i_effnlink >= 0,
12472 ("softdep_load_inodeblock: negative i_effnlink"));
12473 FREE_LOCK(ump);
12474 }
12475
12476 /*
12477 * This routine is called just before the "in-core" inode
12478 * information is to be copied to the in-memory inode block.
12479 * Recall that an inode block contains several inodes. If
12480 * the force flag is set, then the dependencies will be
12481 * cleared so that the update can always be made. Note that
12482 * the buffer is locked when this routine is called, so we
12483 * will never be in the middle of writing the inode block
12484 * to disk.
12485 */
12486 void
softdep_update_inodeblock(struct inode * ip,struct buf * bp,int waitfor)12487 softdep_update_inodeblock(
12488 struct inode *ip, /* the "in_core" copy of the inode */
12489 struct buf *bp, /* the buffer containing the inode block */
12490 int waitfor) /* nonzero => update must be allowed */
12491 {
12492 struct inodedep *inodedep;
12493 struct inoref *inoref;
12494 struct ufsmount *ump;
12495 struct worklist *wk;
12496 struct mount *mp;
12497 struct buf *ibp;
12498 struct fs *fs;
12499 int error;
12500
12501 ump = ITOUMP(ip);
12502 mp = UFSTOVFS(ump);
12503 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12504 ("softdep_update_inodeblock called on non-softdep filesystem"));
12505 fs = ump->um_fs;
12506 /*
12507 * If the effective link count is not equal to the actual link
12508 * count, then we must track the difference in an inodedep while
12509 * the inode is (potentially) tossed out of the cache. Otherwise,
12510 * if there is no existing inodedep, then there are no dependencies
12511 * to track.
12512 */
12513 ACQUIRE_LOCK(ump);
12514 again:
12515 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12516 FREE_LOCK(ump);
12517 if (ip->i_effnlink != ip->i_nlink)
12518 panic("softdep_update_inodeblock: bad link count");
12519 return;
12520 }
12521 /*
12522 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12523 * does not have access to the in-core ip so must write directly into
12524 * the inode block buffer when setting freelink.
12525 */
12526 if ((inodedep->id_state & UNLINKED) != 0) {
12527 if (fs->fs_magic == FS_UFS1_MAGIC)
12528 DIP_SET(ip, i_freelink,
12529 ((struct ufs1_dinode *)bp->b_data +
12530 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12531 else
12532 DIP_SET(ip, i_freelink,
12533 ((struct ufs2_dinode *)bp->b_data +
12534 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12535 }
12536 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta,
12537 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d "
12538 "inodedep %p id_nlinkdelta %jd",
12539 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta));
12540 inodedep->id_nlinkwrote = ip->i_nlink;
12541 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12542 panic("softdep_update_inodeblock: bad delta");
12543 /*
12544 * If we're flushing all dependencies we must also move any waiting
12545 * for journal writes onto the bufwait list prior to I/O.
12546 */
12547 if (waitfor) {
12548 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12549 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12550 == DEPCOMPLETE) {
12551 jwait(&inoref->if_list, MNT_WAIT);
12552 goto again;
12553 }
12554 }
12555 }
12556 /*
12557 * Changes have been initiated. Anything depending on these
12558 * changes cannot occur until this inode has been written.
12559 */
12560 inodedep->id_state &= ~COMPLETE;
12561 if ((inodedep->id_state & ONWORKLIST) == 0)
12562 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12563 /*
12564 * Any new dependencies associated with the incore inode must
12565 * now be moved to the list associated with the buffer holding
12566 * the in-memory copy of the inode. Once merged process any
12567 * allocdirects that are completed by the merger.
12568 */
12569 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12570 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12571 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12572 NULL);
12573 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12574 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12575 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12576 NULL);
12577 /*
12578 * Now that the inode has been pushed into the buffer, the
12579 * operations dependent on the inode being written to disk
12580 * can be moved to the id_bufwait so that they will be
12581 * processed when the buffer I/O completes.
12582 */
12583 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12584 WORKLIST_REMOVE(wk);
12585 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12586 }
12587 /*
12588 * Newly allocated inodes cannot be written until the bitmap
12589 * that allocates them have been written (indicated by
12590 * DEPCOMPLETE being set in id_state). If we are doing a
12591 * forced sync (e.g., an fsync on a file), we force the bitmap
12592 * to be written so that the update can be done.
12593 */
12594 if (waitfor == 0) {
12595 FREE_LOCK(ump);
12596 return;
12597 }
12598 retry:
12599 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12600 FREE_LOCK(ump);
12601 return;
12602 }
12603 ibp = inodedep->id_bmsafemap->sm_buf;
12604 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12605 if (ibp == NULL) {
12606 /*
12607 * If ibp came back as NULL, the dependency could have been
12608 * freed while we slept. Look it up again, and check to see
12609 * that it has completed.
12610 */
12611 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12612 goto retry;
12613 FREE_LOCK(ump);
12614 return;
12615 }
12616 FREE_LOCK(ump);
12617 if ((error = bwrite(ibp)) != 0)
12618 softdep_error("softdep_update_inodeblock: bwrite", error);
12619 }
12620
12621 /*
12622 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12623 * old inode dependency list (such as id_inoupdt).
12624 */
12625 static void
merge_inode_lists(struct allocdirectlst * newlisthead,struct allocdirectlst * oldlisthead)12626 merge_inode_lists(
12627 struct allocdirectlst *newlisthead,
12628 struct allocdirectlst *oldlisthead)
12629 {
12630 struct allocdirect *listadp, *newadp;
12631
12632 newadp = TAILQ_FIRST(newlisthead);
12633 if (newadp != NULL)
12634 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12635 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12636 if (listadp->ad_offset < newadp->ad_offset) {
12637 listadp = TAILQ_NEXT(listadp, ad_next);
12638 continue;
12639 }
12640 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12641 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12642 if (listadp->ad_offset == newadp->ad_offset) {
12643 allocdirect_merge(oldlisthead, newadp,
12644 listadp);
12645 listadp = newadp;
12646 }
12647 newadp = TAILQ_FIRST(newlisthead);
12648 }
12649 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12650 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12651 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12652 }
12653 }
12654
12655 /*
12656 * If we are doing an fsync, then we must ensure that any directory
12657 * entries for the inode have been written after the inode gets to disk.
12658 */
12659 int
softdep_fsync(struct vnode * vp)12660 softdep_fsync(
12661 struct vnode *vp) /* the "in_core" copy of the inode */
12662 {
12663 struct inodedep *inodedep;
12664 struct pagedep *pagedep;
12665 struct inoref *inoref;
12666 struct ufsmount *ump;
12667 struct worklist *wk;
12668 struct diradd *dap;
12669 struct mount *mp;
12670 struct vnode *pvp;
12671 struct inode *ip;
12672 struct buf *bp;
12673 struct fs *fs;
12674 struct thread *td = curthread;
12675 int error, flushparent, pagedep_new_block;
12676 ino_t parentino;
12677 ufs_lbn_t lbn;
12678
12679 ip = VTOI(vp);
12680 mp = vp->v_mount;
12681 ump = VFSTOUFS(mp);
12682 fs = ump->um_fs;
12683 if (MOUNTEDSOFTDEP(mp) == 0)
12684 return (0);
12685 ACQUIRE_LOCK(ump);
12686 restart:
12687 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12688 FREE_LOCK(ump);
12689 return (0);
12690 }
12691 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12692 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12693 == DEPCOMPLETE) {
12694 jwait(&inoref->if_list, MNT_WAIT);
12695 goto restart;
12696 }
12697 }
12698 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12699 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12700 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12701 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12702 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12703 panic("softdep_fsync: pending ops %p", inodedep);
12704 for (error = 0, flushparent = 0; ; ) {
12705 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12706 break;
12707 if (wk->wk_type != D_DIRADD)
12708 panic("softdep_fsync: Unexpected type %s",
12709 TYPENAME(wk->wk_type));
12710 dap = WK_DIRADD(wk);
12711 /*
12712 * Flush our parent if this directory entry has a MKDIR_PARENT
12713 * dependency or is contained in a newly allocated block.
12714 */
12715 if (dap->da_state & DIRCHG)
12716 pagedep = dap->da_previous->dm_pagedep;
12717 else
12718 pagedep = dap->da_pagedep;
12719 parentino = pagedep->pd_ino;
12720 lbn = pagedep->pd_lbn;
12721 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12722 panic("softdep_fsync: dirty");
12723 if ((dap->da_state & MKDIR_PARENT) ||
12724 (pagedep->pd_state & NEWBLOCK))
12725 flushparent = 1;
12726 else
12727 flushparent = 0;
12728 /*
12729 * If we are being fsync'ed as part of vgone'ing this vnode,
12730 * then we will not be able to release and recover the
12731 * vnode below, so we just have to give up on writing its
12732 * directory entry out. It will eventually be written, just
12733 * not now, but then the user was not asking to have it
12734 * written, so we are not breaking any promises.
12735 */
12736 if (VN_IS_DOOMED(vp))
12737 break;
12738 /*
12739 * We prevent deadlock by always fetching inodes from the
12740 * root, moving down the directory tree. Thus, when fetching
12741 * our parent directory, we first try to get the lock. If
12742 * that fails, we must unlock ourselves before requesting
12743 * the lock on our parent. See the comment in ufs_lookup
12744 * for details on possible races.
12745 */
12746 FREE_LOCK(ump);
12747 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL,
12748 &pvp);
12749 if (error == ERELOOKUP)
12750 error = 0;
12751 if (error != 0)
12752 return (error);
12753 /*
12754 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12755 * that are contained in direct blocks will be resolved by
12756 * doing a ffs_update. Pagedeps contained in indirect blocks
12757 * may require a complete sync'ing of the directory. So, we
12758 * try the cheap and fast ffs_update first, and if that fails,
12759 * then we do the slower ffs_syncvnode of the directory.
12760 */
12761 if (flushparent) {
12762 int locked;
12763
12764 if ((error = ffs_update(pvp, 1)) != 0) {
12765 vput(pvp);
12766 return (error);
12767 }
12768 ACQUIRE_LOCK(ump);
12769 locked = 1;
12770 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12771 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12772 if (wk->wk_type != D_DIRADD)
12773 panic("softdep_fsync: Unexpected type %s",
12774 TYPENAME(wk->wk_type));
12775 dap = WK_DIRADD(wk);
12776 if (dap->da_state & DIRCHG)
12777 pagedep = dap->da_previous->dm_pagedep;
12778 else
12779 pagedep = dap->da_pagedep;
12780 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12781 FREE_LOCK(ump);
12782 locked = 0;
12783 if (pagedep_new_block) {
12784 VOP_UNLOCK(vp);
12785 error = ffs_syncvnode(pvp,
12786 MNT_WAIT, 0);
12787 if (error == 0)
12788 error = ERELOOKUP;
12789 vput(pvp);
12790 vn_lock(vp, LK_EXCLUSIVE |
12791 LK_RETRY);
12792 return (error);
12793 }
12794 }
12795 }
12796 if (locked)
12797 FREE_LOCK(ump);
12798 }
12799 /*
12800 * Flush directory page containing the inode's name.
12801 */
12802 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12803 &bp);
12804 if (error == 0)
12805 error = bwrite(bp);
12806 else
12807 brelse(bp);
12808 vput(pvp);
12809 if (!ffs_fsfail_cleanup(ump, error))
12810 return (error);
12811 ACQUIRE_LOCK(ump);
12812 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12813 break;
12814 }
12815 FREE_LOCK(ump);
12816 return (0);
12817 }
12818
12819 /*
12820 * Flush all the dirty bitmaps associated with the block device
12821 * before flushing the rest of the dirty blocks so as to reduce
12822 * the number of dependencies that will have to be rolled back.
12823 *
12824 * XXX Unused?
12825 */
12826 void
softdep_fsync_mountdev(struct vnode * vp)12827 softdep_fsync_mountdev(struct vnode *vp)
12828 {
12829 struct buf *bp, *nbp;
12830 struct worklist *wk;
12831 struct bufobj *bo;
12832
12833 if (!vn_isdisk(vp))
12834 panic("softdep_fsync_mountdev: vnode not a disk");
12835 bo = &vp->v_bufobj;
12836 restart:
12837 BO_LOCK(bo);
12838 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12839 /*
12840 * If it is already scheduled, skip to the next buffer.
12841 */
12842 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12843 continue;
12844
12845 if ((bp->b_flags & B_DELWRI) == 0)
12846 panic("softdep_fsync_mountdev: not dirty");
12847 /*
12848 * We are only interested in bitmaps with outstanding
12849 * dependencies.
12850 */
12851 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12852 wk->wk_type != D_BMSAFEMAP ||
12853 (bp->b_vflags & BV_BKGRDINPROG)) {
12854 BUF_UNLOCK(bp);
12855 continue;
12856 }
12857 BO_UNLOCK(bo);
12858 bremfree(bp);
12859 (void) bawrite(bp);
12860 goto restart;
12861 }
12862 drain_output(vp);
12863 BO_UNLOCK(bo);
12864 }
12865
12866 /*
12867 * Sync all cylinder groups that were dirty at the time this function is
12868 * called. Newly dirtied cgs will be inserted before the sentinel. This
12869 * is used to flush freedep activity that may be holding up writes to a
12870 * indirect block.
12871 */
12872 static int
sync_cgs(struct mount * mp,int waitfor)12873 sync_cgs(struct mount *mp, int waitfor)
12874 {
12875 struct bmsafemap *bmsafemap;
12876 struct bmsafemap *sentinel;
12877 struct ufsmount *ump;
12878 struct buf *bp;
12879 int error;
12880
12881 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12882 sentinel->sm_cg = -1;
12883 ump = VFSTOUFS(mp);
12884 error = 0;
12885 ACQUIRE_LOCK(ump);
12886 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12887 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12888 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12889 /* Skip sentinels and cgs with no work to release. */
12890 if (bmsafemap->sm_cg == -1 ||
12891 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12892 LIST_EMPTY(&bmsafemap->sm_freewr))) {
12893 LIST_REMOVE(sentinel, sm_next);
12894 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12895 continue;
12896 }
12897 /*
12898 * If we don't get the lock and we're waiting try again, if
12899 * not move on to the next buf and try to sync it.
12900 */
12901 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12902 if (bp == NULL && waitfor == MNT_WAIT)
12903 continue;
12904 LIST_REMOVE(sentinel, sm_next);
12905 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12906 if (bp == NULL)
12907 continue;
12908 FREE_LOCK(ump);
12909 if (waitfor == MNT_NOWAIT)
12910 bawrite(bp);
12911 else
12912 error = bwrite(bp);
12913 ACQUIRE_LOCK(ump);
12914 if (error)
12915 break;
12916 }
12917 LIST_REMOVE(sentinel, sm_next);
12918 FREE_LOCK(ump);
12919 free(sentinel, M_BMSAFEMAP);
12920 return (error);
12921 }
12922
12923 /*
12924 * This routine is called when we are trying to synchronously flush a
12925 * file. This routine must eliminate any filesystem metadata dependencies
12926 * so that the syncing routine can succeed.
12927 */
12928 int
softdep_sync_metadata(struct vnode * vp)12929 softdep_sync_metadata(struct vnode *vp)
12930 {
12931 struct inode *ip;
12932 int error;
12933
12934 ip = VTOI(vp);
12935 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12936 ("softdep_sync_metadata called on non-softdep filesystem"));
12937 /*
12938 * Ensure that any direct block dependencies have been cleared,
12939 * truncations are started, and inode references are journaled.
12940 */
12941 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
12942 /*
12943 * Write all journal records to prevent rollbacks on devvp.
12944 */
12945 if (vp->v_type == VCHR)
12946 softdep_flushjournal(vp->v_mount);
12947 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12948 /*
12949 * Ensure that all truncates are written so we won't find deps on
12950 * indirect blocks.
12951 */
12952 process_truncates(vp);
12953 FREE_LOCK(VFSTOUFS(vp->v_mount));
12954
12955 return (error);
12956 }
12957
12958 /*
12959 * This routine is called when we are attempting to sync a buf with
12960 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
12961 * other IO it can but returns EBUSY if the buffer is not yet able to
12962 * be written. Dependencies which will not cause rollbacks will always
12963 * return 0.
12964 */
12965 int
softdep_sync_buf(struct vnode * vp,struct buf * bp,int waitfor)12966 softdep_sync_buf(struct vnode *vp,
12967 struct buf *bp,
12968 int waitfor)
12969 {
12970 struct indirdep *indirdep;
12971 struct pagedep *pagedep;
12972 struct allocindir *aip;
12973 struct newblk *newblk;
12974 struct ufsmount *ump;
12975 struct buf *nbp;
12976 struct worklist *wk;
12977 int i, error;
12978
12979 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12980 ("softdep_sync_buf called on non-softdep filesystem"));
12981 /*
12982 * For VCHR we just don't want to force flush any dependencies that
12983 * will cause rollbacks.
12984 */
12985 if (vp->v_type == VCHR) {
12986 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12987 return (EBUSY);
12988 return (0);
12989 }
12990 ump = VFSTOUFS(vp->v_mount);
12991 ACQUIRE_LOCK(ump);
12992 /*
12993 * As we hold the buffer locked, none of its dependencies
12994 * will disappear.
12995 */
12996 error = 0;
12997 top:
12998 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12999 switch (wk->wk_type) {
13000 case D_ALLOCDIRECT:
13001 case D_ALLOCINDIR:
13002 newblk = WK_NEWBLK(wk);
13003 if (newblk->nb_jnewblk != NULL) {
13004 if (waitfor == MNT_NOWAIT) {
13005 error = EBUSY;
13006 goto out_unlock;
13007 }
13008 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
13009 goto top;
13010 }
13011 if (newblk->nb_state & DEPCOMPLETE ||
13012 waitfor == MNT_NOWAIT)
13013 continue;
13014 nbp = newblk->nb_bmsafemap->sm_buf;
13015 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13016 if (nbp == NULL)
13017 goto top;
13018 FREE_LOCK(ump);
13019 if ((error = bwrite(nbp)) != 0)
13020 goto out;
13021 ACQUIRE_LOCK(ump);
13022 continue;
13023
13024 case D_INDIRDEP:
13025 indirdep = WK_INDIRDEP(wk);
13026 if (waitfor == MNT_NOWAIT) {
13027 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
13028 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
13029 error = EBUSY;
13030 goto out_unlock;
13031 }
13032 }
13033 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
13034 panic("softdep_sync_buf: truncation pending.");
13035 restart:
13036 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
13037 newblk = (struct newblk *)aip;
13038 if (newblk->nb_jnewblk != NULL) {
13039 jwait(&newblk->nb_jnewblk->jn_list,
13040 waitfor);
13041 goto restart;
13042 }
13043 if (newblk->nb_state & DEPCOMPLETE)
13044 continue;
13045 nbp = newblk->nb_bmsafemap->sm_buf;
13046 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13047 if (nbp == NULL)
13048 goto restart;
13049 FREE_LOCK(ump);
13050 if ((error = bwrite(nbp)) != 0)
13051 goto out;
13052 ACQUIRE_LOCK(ump);
13053 goto restart;
13054 }
13055 continue;
13056
13057 case D_PAGEDEP:
13058 /*
13059 * Only flush directory entries in synchronous passes.
13060 */
13061 if (waitfor != MNT_WAIT) {
13062 error = EBUSY;
13063 goto out_unlock;
13064 }
13065 /*
13066 * While syncing snapshots, we must allow recursive
13067 * lookups.
13068 */
13069 BUF_AREC(bp);
13070 /*
13071 * We are trying to sync a directory that may
13072 * have dependencies on both its own metadata
13073 * and/or dependencies on the inodes of any
13074 * recently allocated files. We walk its diradd
13075 * lists pushing out the associated inode.
13076 */
13077 pagedep = WK_PAGEDEP(wk);
13078 for (i = 0; i < DAHASHSZ; i++) {
13079 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
13080 continue;
13081 error = flush_pagedep_deps(vp, wk->wk_mp,
13082 &pagedep->pd_diraddhd[i], bp);
13083 if (error != 0) {
13084 if (error != ERELOOKUP)
13085 BUF_NOREC(bp);
13086 goto out_unlock;
13087 }
13088 }
13089 BUF_NOREC(bp);
13090 continue;
13091
13092 case D_FREEWORK:
13093 case D_FREEDEP:
13094 case D_JSEGDEP:
13095 case D_JNEWBLK:
13096 continue;
13097
13098 default:
13099 panic("softdep_sync_buf: Unknown type %s",
13100 TYPENAME(wk->wk_type));
13101 /* NOTREACHED */
13102 }
13103 }
13104 out_unlock:
13105 FREE_LOCK(ump);
13106 out:
13107 return (error);
13108 }
13109
13110 /*
13111 * Flush the dependencies associated with an inodedep.
13112 */
13113 static int
flush_inodedep_deps(struct vnode * vp,struct mount * mp,ino_t ino)13114 flush_inodedep_deps(
13115 struct vnode *vp,
13116 struct mount *mp,
13117 ino_t ino)
13118 {
13119 struct inodedep *inodedep;
13120 struct inoref *inoref;
13121 struct ufsmount *ump;
13122 int error, waitfor;
13123
13124 /*
13125 * This work is done in two passes. The first pass grabs most
13126 * of the buffers and begins asynchronously writing them. The
13127 * only way to wait for these asynchronous writes is to sleep
13128 * on the filesystem vnode which may stay busy for a long time
13129 * if the filesystem is active. So, instead, we make a second
13130 * pass over the dependencies blocking on each write. In the
13131 * usual case we will be blocking against a write that we
13132 * initiated, so when it is done the dependency will have been
13133 * resolved. Thus the second pass is expected to end quickly.
13134 * We give a brief window at the top of the loop to allow
13135 * any pending I/O to complete.
13136 */
13137 ump = VFSTOUFS(mp);
13138 LOCK_OWNED(ump);
13139 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
13140 if (error)
13141 return (error);
13142 FREE_LOCK(ump);
13143 ACQUIRE_LOCK(ump);
13144 restart:
13145 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13146 return (0);
13147 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13148 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13149 == DEPCOMPLETE) {
13150 jwait(&inoref->if_list, MNT_WAIT);
13151 goto restart;
13152 }
13153 }
13154 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
13155 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
13156 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
13157 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
13158 continue;
13159 /*
13160 * If pass2, we are done, otherwise do pass 2.
13161 */
13162 if (waitfor == MNT_WAIT)
13163 break;
13164 waitfor = MNT_WAIT;
13165 }
13166 /*
13167 * Try freeing inodedep in case all dependencies have been removed.
13168 */
13169 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
13170 (void) free_inodedep(inodedep);
13171 return (0);
13172 }
13173
13174 /*
13175 * Flush an inode dependency list.
13176 */
13177 static int
flush_deplist(struct allocdirectlst * listhead,int waitfor,int * errorp)13178 flush_deplist(
13179 struct allocdirectlst *listhead,
13180 int waitfor,
13181 int *errorp)
13182 {
13183 struct allocdirect *adp;
13184 struct newblk *newblk;
13185 struct ufsmount *ump;
13186 struct buf *bp;
13187
13188 if ((adp = TAILQ_FIRST(listhead)) == NULL)
13189 return (0);
13190 ump = VFSTOUFS(adp->ad_list.wk_mp);
13191 LOCK_OWNED(ump);
13192 TAILQ_FOREACH(adp, listhead, ad_next) {
13193 newblk = (struct newblk *)adp;
13194 if (newblk->nb_jnewblk != NULL) {
13195 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13196 return (1);
13197 }
13198 if (newblk->nb_state & DEPCOMPLETE)
13199 continue;
13200 bp = newblk->nb_bmsafemap->sm_buf;
13201 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13202 if (bp == NULL) {
13203 if (waitfor == MNT_NOWAIT)
13204 continue;
13205 return (1);
13206 }
13207 FREE_LOCK(ump);
13208 if (waitfor == MNT_NOWAIT)
13209 bawrite(bp);
13210 else
13211 *errorp = bwrite(bp);
13212 ACQUIRE_LOCK(ump);
13213 return (1);
13214 }
13215 return (0);
13216 }
13217
13218 /*
13219 * Flush dependencies associated with an allocdirect block.
13220 */
13221 static int
flush_newblk_dep(struct vnode * vp,struct mount * mp,ufs_lbn_t lbn)13222 flush_newblk_dep(
13223 struct vnode *vp,
13224 struct mount *mp,
13225 ufs_lbn_t lbn)
13226 {
13227 struct newblk *newblk;
13228 struct ufsmount *ump;
13229 struct bufobj *bo;
13230 struct inode *ip;
13231 struct buf *bp;
13232 ufs2_daddr_t blkno;
13233 int error;
13234
13235 error = 0;
13236 bo = &vp->v_bufobj;
13237 ip = VTOI(vp);
13238 blkno = DIP(ip, i_db[lbn]);
13239 if (blkno == 0)
13240 panic("flush_newblk_dep: Missing block");
13241 ump = VFSTOUFS(mp);
13242 ACQUIRE_LOCK(ump);
13243 /*
13244 * Loop until all dependencies related to this block are satisfied.
13245 * We must be careful to restart after each sleep in case a write
13246 * completes some part of this process for us.
13247 */
13248 for (;;) {
13249 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13250 FREE_LOCK(ump);
13251 break;
13252 }
13253 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13254 panic("flush_newblk_dep: Bad newblk %p", newblk);
13255 /*
13256 * Flush the journal.
13257 */
13258 if (newblk->nb_jnewblk != NULL) {
13259 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13260 continue;
13261 }
13262 /*
13263 * Write the bitmap dependency.
13264 */
13265 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13266 bp = newblk->nb_bmsafemap->sm_buf;
13267 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13268 if (bp == NULL)
13269 continue;
13270 FREE_LOCK(ump);
13271 error = bwrite(bp);
13272 if (error)
13273 break;
13274 ACQUIRE_LOCK(ump);
13275 continue;
13276 }
13277 /*
13278 * Write the buffer.
13279 */
13280 FREE_LOCK(ump);
13281 BO_LOCK(bo);
13282 bp = gbincore(bo, lbn);
13283 if (bp != NULL) {
13284 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13285 LK_INTERLOCK, BO_LOCKPTR(bo));
13286 if (error == ENOLCK) {
13287 ACQUIRE_LOCK(ump);
13288 error = 0;
13289 continue; /* Slept, retry */
13290 }
13291 if (error != 0)
13292 break; /* Failed */
13293 if (bp->b_flags & B_DELWRI) {
13294 bremfree(bp);
13295 error = bwrite(bp);
13296 if (error)
13297 break;
13298 } else
13299 BUF_UNLOCK(bp);
13300 } else
13301 BO_UNLOCK(bo);
13302 /*
13303 * We have to wait for the direct pointers to
13304 * point at the newdirblk before the dependency
13305 * will go away.
13306 */
13307 error = ffs_update(vp, 1);
13308 if (error)
13309 break;
13310 ACQUIRE_LOCK(ump);
13311 }
13312 return (error);
13313 }
13314
13315 /*
13316 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13317 */
13318 static int
flush_pagedep_deps(struct vnode * pvp,struct mount * mp,struct diraddhd * diraddhdp,struct buf * locked_bp)13319 flush_pagedep_deps(
13320 struct vnode *pvp,
13321 struct mount *mp,
13322 struct diraddhd *diraddhdp,
13323 struct buf *locked_bp)
13324 {
13325 struct inodedep *inodedep;
13326 struct inoref *inoref;
13327 struct ufsmount *ump;
13328 struct diradd *dap;
13329 struct vnode *vp;
13330 int error = 0;
13331 struct buf *bp;
13332 ino_t inum;
13333 struct diraddhd unfinished;
13334
13335 LIST_INIT(&unfinished);
13336 ump = VFSTOUFS(mp);
13337 LOCK_OWNED(ump);
13338 restart:
13339 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13340 /*
13341 * Flush ourselves if this directory entry
13342 * has a MKDIR_PARENT dependency.
13343 */
13344 if (dap->da_state & MKDIR_PARENT) {
13345 FREE_LOCK(ump);
13346 if ((error = ffs_update(pvp, 1)) != 0)
13347 break;
13348 ACQUIRE_LOCK(ump);
13349 /*
13350 * If that cleared dependencies, go on to next.
13351 */
13352 if (dap != LIST_FIRST(diraddhdp))
13353 continue;
13354 /*
13355 * All MKDIR_PARENT dependencies and all the
13356 * NEWBLOCK pagedeps that are contained in direct
13357 * blocks were resolved by doing above ffs_update.
13358 * Pagedeps contained in indirect blocks may
13359 * require a complete sync'ing of the directory.
13360 * We are in the midst of doing a complete sync,
13361 * so if they are not resolved in this pass we
13362 * defer them for now as they will be sync'ed by
13363 * our caller shortly.
13364 */
13365 LIST_REMOVE(dap, da_pdlist);
13366 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13367 continue;
13368 }
13369 /*
13370 * A newly allocated directory must have its "." and
13371 * ".." entries written out before its name can be
13372 * committed in its parent.
13373 */
13374 inum = dap->da_newinum;
13375 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13376 panic("flush_pagedep_deps: lost inode1");
13377 /*
13378 * Wait for any pending journal adds to complete so we don't
13379 * cause rollbacks while syncing.
13380 */
13381 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13382 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13383 == DEPCOMPLETE) {
13384 jwait(&inoref->if_list, MNT_WAIT);
13385 goto restart;
13386 }
13387 }
13388 if (dap->da_state & MKDIR_BODY) {
13389 FREE_LOCK(ump);
13390 error = get_parent_vp(pvp, mp, inum, locked_bp,
13391 diraddhdp, &unfinished, &vp);
13392 if (error != 0)
13393 break;
13394 error = flush_newblk_dep(vp, mp, 0);
13395 /*
13396 * If we still have the dependency we might need to
13397 * update the vnode to sync the new link count to
13398 * disk.
13399 */
13400 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13401 error = ffs_update(vp, 1);
13402 vput(vp);
13403 if (error != 0)
13404 break;
13405 ACQUIRE_LOCK(ump);
13406 /*
13407 * If that cleared dependencies, go on to next.
13408 */
13409 if (dap != LIST_FIRST(diraddhdp))
13410 continue;
13411 if (dap->da_state & MKDIR_BODY) {
13412 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13413 &inodedep);
13414 panic("flush_pagedep_deps: MKDIR_BODY "
13415 "inodedep %p dap %p vp %p",
13416 inodedep, dap, vp);
13417 }
13418 }
13419 /*
13420 * Flush the inode on which the directory entry depends.
13421 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13422 * the only remaining dependency is that the updated inode
13423 * count must get pushed to disk. The inode has already
13424 * been pushed into its inode buffer (via VOP_UPDATE) at
13425 * the time of the reference count change. So we need only
13426 * locate that buffer, ensure that there will be no rollback
13427 * caused by a bitmap dependency, then write the inode buffer.
13428 */
13429 retry:
13430 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13431 panic("flush_pagedep_deps: lost inode");
13432 /*
13433 * If the inode still has bitmap dependencies,
13434 * push them to disk.
13435 */
13436 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13437 bp = inodedep->id_bmsafemap->sm_buf;
13438 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13439 if (bp == NULL)
13440 goto retry;
13441 FREE_LOCK(ump);
13442 if ((error = bwrite(bp)) != 0)
13443 break;
13444 ACQUIRE_LOCK(ump);
13445 if (dap != LIST_FIRST(diraddhdp))
13446 continue;
13447 }
13448 /*
13449 * If the inode is still sitting in a buffer waiting
13450 * to be written or waiting for the link count to be
13451 * adjusted update it here to flush it to disk.
13452 */
13453 if (dap == LIST_FIRST(diraddhdp)) {
13454 FREE_LOCK(ump);
13455 error = get_parent_vp(pvp, mp, inum, locked_bp,
13456 diraddhdp, &unfinished, &vp);
13457 if (error != 0)
13458 break;
13459 error = ffs_update(vp, 1);
13460 vput(vp);
13461 if (error)
13462 break;
13463 ACQUIRE_LOCK(ump);
13464 }
13465 /*
13466 * If we have failed to get rid of all the dependencies
13467 * then something is seriously wrong.
13468 */
13469 if (dap == LIST_FIRST(diraddhdp)) {
13470 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13471 panic("flush_pagedep_deps: failed to flush "
13472 "inodedep %p ino %ju dap %p",
13473 inodedep, (uintmax_t)inum, dap);
13474 }
13475 }
13476 if (error)
13477 ACQUIRE_LOCK(ump);
13478 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13479 LIST_REMOVE(dap, da_pdlist);
13480 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13481 }
13482 return (error);
13483 }
13484
13485 /*
13486 * A large burst of file addition or deletion activity can drive the
13487 * memory load excessively high. First attempt to slow things down
13488 * using the techniques below. If that fails, this routine requests
13489 * the offending operations to fall back to running synchronously
13490 * until the memory load returns to a reasonable level.
13491 */
13492 int
softdep_slowdown(struct vnode * vp)13493 softdep_slowdown(struct vnode *vp)
13494 {
13495 struct ufsmount *ump;
13496 int jlow;
13497 int max_softdeps_hard;
13498
13499 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13500 ("softdep_slowdown called on non-softdep filesystem"));
13501 ump = VFSTOUFS(vp->v_mount);
13502 ACQUIRE_LOCK(ump);
13503 jlow = 0;
13504 /*
13505 * Check for journal space if needed.
13506 */
13507 if (DOINGSUJ(vp)) {
13508 if (journal_space(ump, 0) == 0)
13509 jlow = 1;
13510 }
13511 /*
13512 * If the system is under its limits and our filesystem is
13513 * not responsible for more than our share of the usage and
13514 * we are not low on journal space, then no need to slow down.
13515 */
13516 max_softdeps_hard = max_softdeps * 11 / 10;
13517 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13518 dep_current[D_INODEDEP] < max_softdeps_hard &&
13519 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13520 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13521 ump->softdep_curdeps[D_DIRREM] <
13522 (max_softdeps_hard / 2) / stat_flush_threads &&
13523 ump->softdep_curdeps[D_INODEDEP] <
13524 max_softdeps_hard / stat_flush_threads &&
13525 ump->softdep_curdeps[D_INDIRDEP] <
13526 (max_softdeps_hard / 1000) / stat_flush_threads &&
13527 ump->softdep_curdeps[D_FREEBLKS] <
13528 max_softdeps_hard / stat_flush_threads) {
13529 FREE_LOCK(ump);
13530 return (0);
13531 }
13532 /*
13533 * If the journal is low or our filesystem is over its limit
13534 * then speedup the cleanup.
13535 */
13536 if (ump->softdep_curdeps[D_INDIRDEP] <
13537 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13538 softdep_speedup(ump);
13539 stat_sync_limit_hit += 1;
13540 FREE_LOCK(ump);
13541 /*
13542 * We only slow down the rate at which new dependencies are
13543 * generated if we are not using journaling. With journaling,
13544 * the cleanup should always be sufficient to keep things
13545 * under control.
13546 */
13547 if (DOINGSUJ(vp))
13548 return (0);
13549 return (1);
13550 }
13551
13552 static int
softdep_request_cleanup_filter(struct vnode * vp,void * arg __unused)13553 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused)
13554 {
13555 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 &&
13556 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0));
13557 }
13558
13559 static void
softdep_request_cleanup_inactivate(struct mount * mp)13560 softdep_request_cleanup_inactivate(struct mount *mp)
13561 {
13562 struct vnode *vp, *mvp;
13563 int error;
13564
13565 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter,
13566 NULL) {
13567 vholdl(vp);
13568 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
13569 VI_LOCK(vp);
13570 if (IS_UFS(vp) && vp->v_usecount == 0) {
13571 while ((vp->v_iflag & VI_OWEINACT) != 0) {
13572 error = vinactive(vp);
13573 if (error != 0 && error != ERELOOKUP)
13574 break;
13575 }
13576 atomic_add_int(&stat_delayed_inact, 1);
13577 }
13578 VOP_UNLOCK(vp);
13579 vdropl(vp);
13580 }
13581 }
13582
13583 /*
13584 * Called by the allocation routines when they are about to fail
13585 * in the hope that we can free up the requested resource (inodes
13586 * or disk space).
13587 *
13588 * First check to see if the work list has anything on it. If it has,
13589 * clean up entries until we successfully free the requested resource.
13590 * Because this process holds inodes locked, we cannot handle any remove
13591 * requests that might block on a locked inode as that could lead to
13592 * deadlock. If the worklist yields none of the requested resource,
13593 * start syncing out vnodes to free up the needed space.
13594 */
13595 int
softdep_request_cleanup(struct fs * fs,struct vnode * vp,struct ucred * cred,int resource)13596 softdep_request_cleanup(
13597 struct fs *fs,
13598 struct vnode *vp,
13599 struct ucred *cred,
13600 int resource)
13601 {
13602 struct ufsmount *ump;
13603 struct mount *mp;
13604 long starttime;
13605 ufs2_daddr_t needed;
13606 int error, failed_vnode;
13607
13608 /*
13609 * If we are being called because of a process doing a
13610 * copy-on-write, then it is not safe to process any
13611 * worklist items as we will recurse into the copyonwrite
13612 * routine. This will result in an incoherent snapshot.
13613 * If the vnode that we hold is a snapshot, we must avoid
13614 * handling other resources that could cause deadlock.
13615 */
13616 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13617 return (0);
13618
13619 if (resource == FLUSH_BLOCKS_WAIT)
13620 stat_cleanup_blkrequests += 1;
13621 else
13622 stat_cleanup_inorequests += 1;
13623
13624 mp = vp->v_mount;
13625 ump = VFSTOUFS(mp);
13626 mtx_assert(UFS_MTX(ump), MA_OWNED);
13627 UFS_UNLOCK(ump);
13628 error = ffs_update(vp, 1);
13629 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13630 UFS_LOCK(ump);
13631 return (0);
13632 }
13633 /*
13634 * If we are in need of resources, start by cleaning up
13635 * any block removals associated with our inode.
13636 */
13637 ACQUIRE_LOCK(ump);
13638 process_removes(vp);
13639 process_truncates(vp);
13640 FREE_LOCK(ump);
13641 /*
13642 * Now clean up at least as many resources as we will need.
13643 *
13644 * When requested to clean up inodes, the number that are needed
13645 * is set by the number of simultaneous writers (mnt_writeopcount)
13646 * plus a bit of slop (2) in case some more writers show up while
13647 * we are cleaning.
13648 *
13649 * When requested to free up space, the amount of space that
13650 * we need is enough blocks to allocate a full-sized segment
13651 * (fs_contigsumsize). The number of such segments that will
13652 * be needed is set by the number of simultaneous writers
13653 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13654 * writers show up while we are cleaning.
13655 *
13656 * Additionally, if we are unpriviledged and allocating space,
13657 * we need to ensure that we clean up enough blocks to get the
13658 * needed number of blocks over the threshold of the minimum
13659 * number of blocks required to be kept free by the filesystem
13660 * (fs_minfree).
13661 */
13662 if (resource == FLUSH_INODES_WAIT) {
13663 needed = vfs_mount_fetch_counter(vp->v_mount,
13664 MNT_COUNT_WRITEOPCOUNT) + 2;
13665 } else if (resource == FLUSH_BLOCKS_WAIT) {
13666 needed = (vfs_mount_fetch_counter(vp->v_mount,
13667 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13668 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13669 needed += fragstoblks(fs,
13670 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13671 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13672 } else {
13673 printf("softdep_request_cleanup: Unknown resource type %d\n",
13674 resource);
13675 UFS_LOCK(ump);
13676 return (0);
13677 }
13678 starttime = time_second;
13679 retry:
13680 if (resource == FLUSH_BLOCKS_WAIT &&
13681 fs->fs_cstotal.cs_nbfree <= needed)
13682 softdep_send_speedup(ump, needed * fs->fs_bsize,
13683 BIO_SPEEDUP_TRIM);
13684 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13685 fs->fs_cstotal.cs_nbfree <= needed) ||
13686 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13687 fs->fs_cstotal.cs_nifree <= needed)) {
13688 ACQUIRE_LOCK(ump);
13689 if (ump->softdep_on_worklist > 0 &&
13690 process_worklist_item(UFSTOVFS(ump),
13691 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13692 stat_worklist_push += 1;
13693 FREE_LOCK(ump);
13694 }
13695
13696 /*
13697 * Check that there are vnodes pending inactivation. As they
13698 * have been unlinked, inactivating them will free up their
13699 * inodes.
13700 */
13701 ACQUIRE_LOCK(ump);
13702 if (resource == FLUSH_INODES_WAIT &&
13703 fs->fs_cstotal.cs_nifree <= needed &&
13704 fs->fs_pendinginodes <= needed) {
13705 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) {
13706 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE;
13707 FREE_LOCK(ump);
13708 softdep_request_cleanup_inactivate(mp);
13709 ACQUIRE_LOCK(ump);
13710 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE;
13711 wakeup(&ump->um_softdep->sd_flags);
13712 } else {
13713 while ((ump->um_softdep->sd_flags &
13714 FLUSH_DI_ACTIVE) != 0) {
13715 msleep(&ump->um_softdep->sd_flags,
13716 LOCK_PTR(ump), PVM, "ffsvina", hz);
13717 }
13718 }
13719 }
13720 FREE_LOCK(ump);
13721
13722 /*
13723 * If we still need resources and there are no more worklist
13724 * entries to process to obtain them, we have to start flushing
13725 * the dirty vnodes to force the release of additional requests
13726 * to the worklist that we can then process to reap addition
13727 * resources. We walk the vnodes associated with the mount point
13728 * until we get the needed worklist requests that we can reap.
13729 *
13730 * If there are several threads all needing to clean the same
13731 * mount point, only one is allowed to walk the mount list.
13732 * When several threads all try to walk the same mount list,
13733 * they end up competing with each other and often end up in
13734 * livelock. This approach ensures that forward progress is
13735 * made at the cost of occational ENOSPC errors being returned
13736 * that might otherwise have been avoided.
13737 */
13738 error = 1;
13739 if ((resource == FLUSH_BLOCKS_WAIT &&
13740 fs->fs_cstotal.cs_nbfree <= needed) ||
13741 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13742 fs->fs_cstotal.cs_nifree <= needed)) {
13743 ACQUIRE_LOCK(ump);
13744 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13745 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13746 FREE_LOCK(ump);
13747 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13748 ACQUIRE_LOCK(ump);
13749 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13750 wakeup(&ump->um_softdep->sd_flags);
13751 FREE_LOCK(ump);
13752 if (ump->softdep_on_worklist > 0) {
13753 stat_cleanup_retries += 1;
13754 if (!failed_vnode)
13755 goto retry;
13756 }
13757 } else {
13758 while ((ump->um_softdep->sd_flags &
13759 FLUSH_RC_ACTIVE) != 0) {
13760 msleep(&ump->um_softdep->sd_flags,
13761 LOCK_PTR(ump), PVM, "ffsrca", hz);
13762 }
13763 FREE_LOCK(ump);
13764 error = 0;
13765 }
13766 stat_cleanup_failures += 1;
13767 }
13768 if (time_second - starttime > stat_cleanup_high_delay)
13769 stat_cleanup_high_delay = time_second - starttime;
13770 UFS_LOCK(ump);
13771 return (error);
13772 }
13773
13774 /*
13775 * Scan the vnodes for the specified mount point flushing out any
13776 * vnodes that can be locked without waiting. Finally, try to flush
13777 * the device associated with the mount point if it can be locked
13778 * without waiting.
13779 *
13780 * We return 0 if we were able to lock every vnode in our scan.
13781 * If we had to skip one or more vnodes, we return 1.
13782 */
13783 static int
softdep_request_cleanup_flush(struct mount * mp,struct ufsmount * ump)13784 softdep_request_cleanup_flush(struct mount *mp, struct ufsmount *ump)
13785 {
13786 struct thread *td;
13787 struct vnode *lvp, *mvp;
13788 int failed_vnode;
13789
13790 failed_vnode = 0;
13791 td = curthread;
13792 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13793 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13794 VI_UNLOCK(lvp);
13795 continue;
13796 }
13797 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) {
13798 failed_vnode = 1;
13799 continue;
13800 }
13801 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
13802 vput(lvp);
13803 continue;
13804 }
13805 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13806 vput(lvp);
13807 }
13808 lvp = ump->um_devvp;
13809 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13810 VOP_FSYNC(lvp, MNT_NOWAIT, td);
13811 VOP_UNLOCK(lvp);
13812 }
13813 return (failed_vnode);
13814 }
13815
13816 static bool
softdep_excess_items(struct ufsmount * ump,int item)13817 softdep_excess_items(struct ufsmount *ump, int item)
13818 {
13819
13820 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13821 return (dep_current[item] > max_softdeps &&
13822 ump->softdep_curdeps[item] > max_softdeps /
13823 stat_flush_threads);
13824 }
13825
13826 static void
schedule_cleanup(struct mount * mp)13827 schedule_cleanup(struct mount *mp)
13828 {
13829 struct ufsmount *ump;
13830 struct thread *td;
13831
13832 ump = VFSTOUFS(mp);
13833 LOCK_OWNED(ump);
13834 FREE_LOCK(ump);
13835 td = curthread;
13836 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13837 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13838 /*
13839 * No ast is delivered to kernel threads, so nobody
13840 * would deref the mp. Some kernel threads
13841 * explicitly check for AST, e.g. NFS daemon does
13842 * this in the serving loop.
13843 */
13844 return;
13845 }
13846 if (td->td_su != NULL)
13847 vfs_rel(td->td_su);
13848 vfs_ref(mp);
13849 td->td_su = mp;
13850 ast_sched(td, TDA_UFS);
13851 }
13852
13853 static void
softdep_ast_cleanup_proc(struct thread * td,int ast __unused)13854 softdep_ast_cleanup_proc(struct thread *td, int ast __unused)
13855 {
13856 struct mount *mp;
13857 struct ufsmount *ump;
13858 int error;
13859 bool req;
13860
13861 while ((mp = td->td_su) != NULL) {
13862 td->td_su = NULL;
13863 error = vfs_busy(mp, MBF_NOWAIT);
13864 vfs_rel(mp);
13865 if (error != 0)
13866 return;
13867 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13868 ump = VFSTOUFS(mp);
13869 for (;;) {
13870 req = false;
13871 ACQUIRE_LOCK(ump);
13872 if (softdep_excess_items(ump, D_INODEDEP)) {
13873 req = true;
13874 request_cleanup(mp, FLUSH_INODES);
13875 }
13876 if (softdep_excess_items(ump, D_DIRREM)) {
13877 req = true;
13878 request_cleanup(mp, FLUSH_BLOCKS);
13879 }
13880 FREE_LOCK(ump);
13881 if (softdep_excess_items(ump, D_NEWBLK) ||
13882 softdep_excess_items(ump, D_ALLOCDIRECT) ||
13883 softdep_excess_items(ump, D_ALLOCINDIR)) {
13884 error = vn_start_write(NULL, &mp,
13885 V_WAIT);
13886 if (error == 0) {
13887 req = true;
13888 VFS_SYNC(mp, MNT_WAIT);
13889 vn_finished_write(mp);
13890 }
13891 }
13892 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13893 break;
13894 }
13895 }
13896 vfs_unbusy(mp);
13897 }
13898 if ((mp = td->td_su) != NULL) {
13899 td->td_su = NULL;
13900 vfs_rel(mp);
13901 }
13902 }
13903
13904 /*
13905 * If memory utilization has gotten too high, deliberately slow things
13906 * down and speed up the I/O processing.
13907 */
13908 static int
request_cleanup(struct mount * mp,int resource)13909 request_cleanup(struct mount *mp, int resource)
13910 {
13911 struct thread *td = curthread;
13912 struct ufsmount *ump;
13913
13914 ump = VFSTOUFS(mp);
13915 LOCK_OWNED(ump);
13916 /*
13917 * We never hold up the filesystem syncer or buf daemon.
13918 */
13919 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13920 return (0);
13921 /*
13922 * First check to see if the work list has gotten backlogged.
13923 * If it has, co-opt this process to help clean up two entries.
13924 * Because this process may hold inodes locked, we cannot
13925 * handle any remove requests that might block on a locked
13926 * inode as that could lead to deadlock. We set TDP_SOFTDEP
13927 * to avoid recursively processing the worklist.
13928 */
13929 if (ump->softdep_on_worklist > max_softdeps / 10) {
13930 td->td_pflags |= TDP_SOFTDEP;
13931 process_worklist_item(mp, 2, LK_NOWAIT);
13932 td->td_pflags &= ~TDP_SOFTDEP;
13933 stat_worklist_push += 2;
13934 return(1);
13935 }
13936 /*
13937 * Next, we attempt to speed up the syncer process. If that
13938 * is successful, then we allow the process to continue.
13939 */
13940 if (softdep_speedup(ump) &&
13941 resource != FLUSH_BLOCKS_WAIT &&
13942 resource != FLUSH_INODES_WAIT)
13943 return(0);
13944 /*
13945 * If we are resource constrained on inode dependencies, try
13946 * flushing some dirty inodes. Otherwise, we are constrained
13947 * by file deletions, so try accelerating flushes of directories
13948 * with removal dependencies. We would like to do the cleanup
13949 * here, but we probably hold an inode locked at this point and
13950 * that might deadlock against one that we try to clean. So,
13951 * the best that we can do is request the syncer daemon to do
13952 * the cleanup for us.
13953 */
13954 switch (resource) {
13955 case FLUSH_INODES:
13956 case FLUSH_INODES_WAIT:
13957 ACQUIRE_GBLLOCK(&lk);
13958 stat_ino_limit_push += 1;
13959 req_clear_inodedeps += 1;
13960 FREE_GBLLOCK(&lk);
13961 stat_countp = &stat_ino_limit_hit;
13962 break;
13963
13964 case FLUSH_BLOCKS:
13965 case FLUSH_BLOCKS_WAIT:
13966 ACQUIRE_GBLLOCK(&lk);
13967 stat_blk_limit_push += 1;
13968 req_clear_remove += 1;
13969 FREE_GBLLOCK(&lk);
13970 stat_countp = &stat_blk_limit_hit;
13971 break;
13972
13973 default:
13974 panic("request_cleanup: unknown type");
13975 }
13976 /*
13977 * Hopefully the syncer daemon will catch up and awaken us.
13978 * We wait at most tickdelay before proceeding in any case.
13979 */
13980 ACQUIRE_GBLLOCK(&lk);
13981 FREE_LOCK(ump);
13982 proc_waiting += 1;
13983 if (callout_pending(&softdep_callout) == FALSE)
13984 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13985 pause_timer, 0);
13986
13987 if ((td->td_pflags & TDP_KTHREAD) == 0)
13988 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13989 proc_waiting -= 1;
13990 FREE_GBLLOCK(&lk);
13991 ACQUIRE_LOCK(ump);
13992 return (1);
13993 }
13994
13995 /*
13996 * Awaken processes pausing in request_cleanup and clear proc_waiting
13997 * to indicate that there is no longer a timer running. Pause_timer
13998 * will be called with the global softdep mutex (&lk) locked.
13999 */
14000 static void
pause_timer(void * arg)14001 pause_timer(void *arg)
14002 {
14003
14004 GBLLOCK_OWNED(&lk);
14005 /*
14006 * The callout_ API has acquired mtx and will hold it around this
14007 * function call.
14008 */
14009 *stat_countp += proc_waiting;
14010 wakeup(&proc_waiting);
14011 }
14012
14013 /*
14014 * If requested, try removing inode or removal dependencies.
14015 */
14016 static void
check_clear_deps(struct mount * mp)14017 check_clear_deps(struct mount *mp)
14018 {
14019 struct ufsmount *ump;
14020 bool suj_susp;
14021
14022 /*
14023 * Tell the lower layers that any TRIM or WRITE transactions that have
14024 * been delayed for performance reasons should proceed to help alleviate
14025 * the shortage faster. The race between checking req_* and the softdep
14026 * mutex (lk) is fine since this is an advisory operation that at most
14027 * causes deferred work to be done sooner.
14028 */
14029 ump = VFSTOUFS(mp);
14030 suj_susp = ump->um_softdep->sd_jblocks != NULL &&
14031 ump->softdep_jblocks->jb_suspended;
14032 if (req_clear_remove || req_clear_inodedeps || suj_susp) {
14033 FREE_LOCK(ump);
14034 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
14035 ACQUIRE_LOCK(ump);
14036 }
14037
14038 /*
14039 * If we are suspended, it may be because of our using
14040 * too many inodedeps, so help clear them out.
14041 */
14042 if (suj_susp)
14043 clear_inodedeps(mp);
14044
14045 /*
14046 * General requests for cleanup of backed up dependencies
14047 */
14048 ACQUIRE_GBLLOCK(&lk);
14049 if (req_clear_inodedeps) {
14050 req_clear_inodedeps -= 1;
14051 FREE_GBLLOCK(&lk);
14052 clear_inodedeps(mp);
14053 ACQUIRE_GBLLOCK(&lk);
14054 wakeup(&proc_waiting);
14055 }
14056 if (req_clear_remove) {
14057 req_clear_remove -= 1;
14058 FREE_GBLLOCK(&lk);
14059 clear_remove(mp);
14060 ACQUIRE_GBLLOCK(&lk);
14061 wakeup(&proc_waiting);
14062 }
14063 FREE_GBLLOCK(&lk);
14064 }
14065
14066 /*
14067 * Flush out a directory with at least one removal dependency in an effort to
14068 * reduce the number of dirrem, freefile, and freeblks dependency structures.
14069 */
14070 static void
clear_remove(struct mount * mp)14071 clear_remove(struct mount *mp)
14072 {
14073 struct pagedep_hashhead *pagedephd;
14074 struct pagedep *pagedep;
14075 struct ufsmount *ump;
14076 struct vnode *vp;
14077 struct bufobj *bo;
14078 int error, cnt;
14079 ino_t ino;
14080
14081 ump = VFSTOUFS(mp);
14082 LOCK_OWNED(ump);
14083
14084 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
14085 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
14086 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
14087 ump->pagedep_nextclean = 0;
14088 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
14089 if (LIST_EMPTY(&pagedep->pd_dirremhd))
14090 continue;
14091 ino = pagedep->pd_ino;
14092 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14093 continue;
14094 FREE_LOCK(ump);
14095
14096 /*
14097 * Let unmount clear deps
14098 */
14099 error = vfs_busy(mp, MBF_NOWAIT);
14100 if (error != 0)
14101 goto finish_write;
14102 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14103 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
14104 vfs_unbusy(mp);
14105 if (error != 0) {
14106 softdep_error("clear_remove: vget", error);
14107 goto finish_write;
14108 }
14109 MPASS(VTOI(vp)->i_mode != 0);
14110 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14111 softdep_error("clear_remove: fsync", error);
14112 bo = &vp->v_bufobj;
14113 BO_LOCK(bo);
14114 drain_output(vp);
14115 BO_UNLOCK(bo);
14116 vput(vp);
14117 finish_write:
14118 vn_finished_write(mp);
14119 ACQUIRE_LOCK(ump);
14120 return;
14121 }
14122 }
14123 }
14124
14125 /*
14126 * Clear out a block of dirty inodes in an effort to reduce
14127 * the number of inodedep dependency structures.
14128 */
14129 static void
clear_inodedeps(struct mount * mp)14130 clear_inodedeps(struct mount *mp)
14131 {
14132 struct inodedep_hashhead *inodedephd;
14133 struct inodedep *inodedep;
14134 struct ufsmount *ump;
14135 struct vnode *vp;
14136 struct fs *fs;
14137 int error, cnt;
14138 ino_t firstino, lastino, ino;
14139
14140 ump = VFSTOUFS(mp);
14141 fs = ump->um_fs;
14142 LOCK_OWNED(ump);
14143 /*
14144 * Pick a random inode dependency to be cleared.
14145 * We will then gather up all the inodes in its block
14146 * that have dependencies and flush them out.
14147 */
14148 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
14149 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
14150 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
14151 ump->inodedep_nextclean = 0;
14152 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
14153 break;
14154 }
14155 if (inodedep == NULL)
14156 return;
14157 /*
14158 * Find the last inode in the block with dependencies.
14159 */
14160 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
14161 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
14162 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
14163 break;
14164 /*
14165 * Asynchronously push all but the last inode with dependencies.
14166 * Synchronously push the last inode with dependencies to ensure
14167 * that the inode block gets written to free up the inodedeps.
14168 */
14169 for (ino = firstino; ino <= lastino; ino++) {
14170 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
14171 continue;
14172 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14173 continue;
14174 FREE_LOCK(ump);
14175 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
14176 if (error != 0) {
14177 vn_finished_write(mp);
14178 ACQUIRE_LOCK(ump);
14179 return;
14180 }
14181 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14182 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) {
14183 softdep_error("clear_inodedeps: vget", error);
14184 vfs_unbusy(mp);
14185 vn_finished_write(mp);
14186 ACQUIRE_LOCK(ump);
14187 return;
14188 }
14189 vfs_unbusy(mp);
14190 if (VTOI(vp)->i_mode == 0) {
14191 vgone(vp);
14192 } else if (ino == lastino) {
14193 do {
14194 error = ffs_syncvnode(vp, MNT_WAIT, 0);
14195 } while (error == ERELOOKUP);
14196 if (error != 0)
14197 softdep_error("clear_inodedeps: fsync1", error);
14198 } else {
14199 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14200 softdep_error("clear_inodedeps: fsync2", error);
14201 BO_LOCK(&vp->v_bufobj);
14202 drain_output(vp);
14203 BO_UNLOCK(&vp->v_bufobj);
14204 }
14205 vput(vp);
14206 vn_finished_write(mp);
14207 ACQUIRE_LOCK(ump);
14208 }
14209 }
14210
14211 void
softdep_buf_append(struct buf * bp,struct workhead * wkhd)14212 softdep_buf_append(struct buf *bp, struct workhead *wkhd)
14213 {
14214 struct worklist *wk;
14215 struct ufsmount *ump;
14216
14217 if ((wk = LIST_FIRST(wkhd)) == NULL)
14218 return;
14219 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14220 ("softdep_buf_append called on non-softdep filesystem"));
14221 ump = VFSTOUFS(wk->wk_mp);
14222 ACQUIRE_LOCK(ump);
14223 while ((wk = LIST_FIRST(wkhd)) != NULL) {
14224 WORKLIST_REMOVE(wk);
14225 WORKLIST_INSERT(&bp->b_dep, wk);
14226 }
14227 FREE_LOCK(ump);
14228
14229 }
14230
14231 void
softdep_inode_append(struct inode * ip,struct ucred * cred,struct workhead * wkhd)14232 softdep_inode_append(
14233 struct inode *ip,
14234 struct ucred *cred,
14235 struct workhead *wkhd)
14236 {
14237 struct buf *bp;
14238 struct fs *fs;
14239 struct ufsmount *ump;
14240 int error;
14241
14242 ump = ITOUMP(ip);
14243 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14244 ("softdep_inode_append called on non-softdep filesystem"));
14245 fs = ump->um_fs;
14246 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14247 (int)fs->fs_bsize, cred, &bp);
14248 if (error) {
14249 bqrelse(bp);
14250 softdep_freework(wkhd);
14251 return;
14252 }
14253 softdep_buf_append(bp, wkhd);
14254 bqrelse(bp);
14255 }
14256
14257 void
softdep_freework(struct workhead * wkhd)14258 softdep_freework(struct workhead *wkhd)
14259 {
14260 struct worklist *wk;
14261 struct ufsmount *ump;
14262
14263 if ((wk = LIST_FIRST(wkhd)) == NULL)
14264 return;
14265 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14266 ("softdep_freework called on non-softdep filesystem"));
14267 ump = VFSTOUFS(wk->wk_mp);
14268 ACQUIRE_LOCK(ump);
14269 handle_jwork(wkhd);
14270 FREE_LOCK(ump);
14271 }
14272
14273 static struct ufsmount *
softdep_bp_to_mp(struct buf * bp)14274 softdep_bp_to_mp(struct buf *bp)
14275 {
14276 struct mount *mp;
14277 struct vnode *vp;
14278
14279 if (LIST_EMPTY(&bp->b_dep))
14280 return (NULL);
14281 vp = bp->b_vp;
14282 KASSERT(vp != NULL,
14283 ("%s, buffer with dependencies lacks vnode", __func__));
14284
14285 /*
14286 * The ump mount point is stable after we get a correct
14287 * pointer, since bp is locked and this prevents unmount from
14288 * proceeding. But to get to it, we cannot dereference bp->b_dep
14289 * head wk_mp, because we do not yet own SU ump lock and
14290 * workitem might be freed while dereferenced.
14291 */
14292 retry:
14293 switch (vp->v_type) {
14294 case VCHR:
14295 VI_LOCK(vp);
14296 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14297 VI_UNLOCK(vp);
14298 if (mp == NULL)
14299 goto retry;
14300 break;
14301 case VREG:
14302 case VDIR:
14303 case VLNK:
14304 case VFIFO:
14305 case VSOCK:
14306 mp = vp->v_mount;
14307 break;
14308 case VBLK:
14309 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14310 /* FALLTHROUGH */
14311 case VNON:
14312 case VBAD:
14313 case VMARKER:
14314 mp = NULL;
14315 break;
14316 default:
14317 vn_printf(vp, "unknown vnode type");
14318 mp = NULL;
14319 break;
14320 }
14321 return (VFSTOUFS(mp));
14322 }
14323
14324 /*
14325 * Function to determine if the buffer has outstanding dependencies
14326 * that will cause a roll-back if the buffer is written. If wantcount
14327 * is set, return number of dependencies, otherwise just yes or no.
14328 */
14329 static int
softdep_count_dependencies(struct buf * bp,int wantcount)14330 softdep_count_dependencies(struct buf *bp, int wantcount)
14331 {
14332 struct worklist *wk;
14333 struct ufsmount *ump;
14334 struct bmsafemap *bmsafemap;
14335 struct freework *freework;
14336 struct inodedep *inodedep;
14337 struct indirdep *indirdep;
14338 struct freeblks *freeblks;
14339 struct allocindir *aip;
14340 struct pagedep *pagedep;
14341 struct dirrem *dirrem;
14342 struct newblk *newblk;
14343 struct mkdir *mkdir;
14344 struct diradd *dap;
14345 int i, retval;
14346
14347 ump = softdep_bp_to_mp(bp);
14348 if (ump == NULL)
14349 return (0);
14350 retval = 0;
14351 ACQUIRE_LOCK(ump);
14352 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14353 switch (wk->wk_type) {
14354 case D_INODEDEP:
14355 inodedep = WK_INODEDEP(wk);
14356 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14357 /* bitmap allocation dependency */
14358 retval += 1;
14359 if (!wantcount)
14360 goto out;
14361 }
14362 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14363 /* direct block pointer dependency */
14364 retval += 1;
14365 if (!wantcount)
14366 goto out;
14367 }
14368 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14369 /* direct block pointer dependency */
14370 retval += 1;
14371 if (!wantcount)
14372 goto out;
14373 }
14374 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14375 /* Add reference dependency. */
14376 retval += 1;
14377 if (!wantcount)
14378 goto out;
14379 }
14380 continue;
14381
14382 case D_INDIRDEP:
14383 indirdep = WK_INDIRDEP(wk);
14384
14385 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14386 /* indirect truncation dependency */
14387 retval += 1;
14388 if (!wantcount)
14389 goto out;
14390 }
14391
14392 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14393 /* indirect block pointer dependency */
14394 retval += 1;
14395 if (!wantcount)
14396 goto out;
14397 }
14398 continue;
14399
14400 case D_PAGEDEP:
14401 pagedep = WK_PAGEDEP(wk);
14402 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14403 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14404 /* Journal remove ref dependency. */
14405 retval += 1;
14406 if (!wantcount)
14407 goto out;
14408 }
14409 }
14410 for (i = 0; i < DAHASHSZ; i++) {
14411 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14412 /* directory entry dependency */
14413 retval += 1;
14414 if (!wantcount)
14415 goto out;
14416 }
14417 }
14418 continue;
14419
14420 case D_BMSAFEMAP:
14421 bmsafemap = WK_BMSAFEMAP(wk);
14422 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14423 /* Add reference dependency. */
14424 retval += 1;
14425 if (!wantcount)
14426 goto out;
14427 }
14428 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14429 /* Allocate block dependency. */
14430 retval += 1;
14431 if (!wantcount)
14432 goto out;
14433 }
14434 continue;
14435
14436 case D_FREEBLKS:
14437 freeblks = WK_FREEBLKS(wk);
14438 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14439 /* Freeblk journal dependency. */
14440 retval += 1;
14441 if (!wantcount)
14442 goto out;
14443 }
14444 continue;
14445
14446 case D_ALLOCDIRECT:
14447 case D_ALLOCINDIR:
14448 newblk = WK_NEWBLK(wk);
14449 if (newblk->nb_jnewblk) {
14450 /* Journal allocate dependency. */
14451 retval += 1;
14452 if (!wantcount)
14453 goto out;
14454 }
14455 continue;
14456
14457 case D_MKDIR:
14458 mkdir = WK_MKDIR(wk);
14459 if (mkdir->md_jaddref) {
14460 /* Journal reference dependency. */
14461 retval += 1;
14462 if (!wantcount)
14463 goto out;
14464 }
14465 continue;
14466
14467 case D_FREEWORK:
14468 case D_FREEDEP:
14469 case D_JSEGDEP:
14470 case D_JSEG:
14471 case D_SBDEP:
14472 /* never a dependency on these blocks */
14473 continue;
14474
14475 default:
14476 panic("softdep_count_dependencies: Unexpected type %s",
14477 TYPENAME(wk->wk_type));
14478 /* NOTREACHED */
14479 }
14480 }
14481 out:
14482 FREE_LOCK(ump);
14483 return (retval);
14484 }
14485
14486 /*
14487 * Acquire exclusive access to a buffer.
14488 * Must be called with a locked mtx parameter.
14489 * Return acquired buffer or NULL on failure.
14490 */
14491 static struct buf *
getdirtybuf(struct buf * bp,struct rwlock * lock,int waitfor)14492 getdirtybuf(struct buf *bp,
14493 struct rwlock *lock,
14494 int waitfor)
14495 {
14496 int error;
14497
14498 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14499 if (waitfor != MNT_WAIT)
14500 return (NULL);
14501 error = BUF_LOCK(bp,
14502 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14503 /*
14504 * Even if we successfully acquire bp here, we have dropped
14505 * lock, which may violates our guarantee.
14506 */
14507 if (error == 0)
14508 BUF_UNLOCK(bp);
14509 else if (error != ENOLCK)
14510 panic("getdirtybuf: inconsistent lock: %d", error);
14511 rw_wlock(lock);
14512 return (NULL);
14513 }
14514 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14515 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14516 rw_wunlock(lock);
14517 BO_LOCK(bp->b_bufobj);
14518 BUF_UNLOCK(bp);
14519 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14520 bp->b_vflags |= BV_BKGRDWAIT;
14521 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14522 PRIBIO | PDROP, "getbuf", 0);
14523 } else
14524 BO_UNLOCK(bp->b_bufobj);
14525 rw_wlock(lock);
14526 return (NULL);
14527 }
14528 BUF_UNLOCK(bp);
14529 if (waitfor != MNT_WAIT)
14530 return (NULL);
14531 #ifdef DEBUG_VFS_LOCKS
14532 if (bp->b_vp->v_type != VCHR)
14533 ASSERT_BO_WLOCKED(bp->b_bufobj);
14534 #endif
14535 bp->b_vflags |= BV_BKGRDWAIT;
14536 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14537 return (NULL);
14538 }
14539 if ((bp->b_flags & B_DELWRI) == 0) {
14540 BUF_UNLOCK(bp);
14541 return (NULL);
14542 }
14543 bremfree(bp);
14544 return (bp);
14545 }
14546
14547 /*
14548 * Check if it is safe to suspend the file system now. On entry,
14549 * the vnode interlock for devvp should be held. Return 0 with
14550 * the mount interlock held if the file system can be suspended now,
14551 * otherwise return EAGAIN with the mount interlock held.
14552 */
14553 int
softdep_check_suspend(struct mount * mp,struct vnode * devvp,int softdep_depcnt,int softdep_accdepcnt,int secondary_writes,int secondary_accwrites)14554 softdep_check_suspend(struct mount *mp,
14555 struct vnode *devvp,
14556 int softdep_depcnt,
14557 int softdep_accdepcnt,
14558 int secondary_writes,
14559 int secondary_accwrites)
14560 {
14561 struct buf *bp;
14562 struct bufobj *bo;
14563 struct ufsmount *ump;
14564 struct inodedep *inodedep;
14565 struct indirdep *indirdep;
14566 struct worklist *wk, *nextwk;
14567 int error, unlinked;
14568
14569 bo = &devvp->v_bufobj;
14570 ASSERT_BO_WLOCKED(bo);
14571
14572 /*
14573 * If we are not running with soft updates, then we need only
14574 * deal with secondary writes as we try to suspend.
14575 */
14576 if (MOUNTEDSOFTDEP(mp) == 0) {
14577 MNT_ILOCK(mp);
14578 while (mp->mnt_secondary_writes != 0) {
14579 BO_UNLOCK(bo);
14580 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14581 (PUSER - 1) | PDROP, "secwr", 0);
14582 BO_LOCK(bo);
14583 MNT_ILOCK(mp);
14584 }
14585
14586 /*
14587 * Reasons for needing more work before suspend:
14588 * - Dirty buffers on devvp.
14589 * - Secondary writes occurred after start of vnode sync loop
14590 */
14591 error = 0;
14592 if (bo->bo_numoutput > 0 ||
14593 bo->bo_dirty.bv_cnt > 0 ||
14594 secondary_writes != 0 ||
14595 mp->mnt_secondary_writes != 0 ||
14596 secondary_accwrites != mp->mnt_secondary_accwrites)
14597 error = EAGAIN;
14598 BO_UNLOCK(bo);
14599 return (error);
14600 }
14601
14602 /*
14603 * If we are running with soft updates, then we need to coordinate
14604 * with them as we try to suspend.
14605 */
14606 ump = VFSTOUFS(mp);
14607 for (;;) {
14608 if (!TRY_ACQUIRE_LOCK(ump)) {
14609 BO_UNLOCK(bo);
14610 ACQUIRE_LOCK(ump);
14611 FREE_LOCK(ump);
14612 BO_LOCK(bo);
14613 continue;
14614 }
14615 MNT_ILOCK(mp);
14616 if (mp->mnt_secondary_writes != 0) {
14617 FREE_LOCK(ump);
14618 BO_UNLOCK(bo);
14619 msleep(&mp->mnt_secondary_writes,
14620 MNT_MTX(mp),
14621 (PUSER - 1) | PDROP, "secwr", 0);
14622 BO_LOCK(bo);
14623 continue;
14624 }
14625 break;
14626 }
14627
14628 unlinked = 0;
14629 if (MOUNTEDSUJ(mp)) {
14630 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14631 inodedep != NULL;
14632 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14633 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14634 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14635 UNLINKONLIST) ||
14636 !check_inodedep_free(inodedep))
14637 continue;
14638 unlinked++;
14639 }
14640 }
14641
14642 /*
14643 * XXX Check for orphaned indirdep dependency structures.
14644 *
14645 * During forcible unmount after a disk failure there is a
14646 * bug that causes one or more indirdep dependency structures
14647 * to fail to be deallocated. We check for them here and clean
14648 * them up so that the unmount can succeed.
14649 */
14650 if ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0 && ump->softdep_deps > 0 &&
14651 ump->softdep_deps == ump->softdep_curdeps[D_INDIRDEP]) {
14652 LIST_FOREACH_SAFE(wk, &ump->softdep_alldeps[D_INDIRDEP],
14653 wk_all, nextwk) {
14654 indirdep = WK_INDIRDEP(wk);
14655 if ((indirdep->ir_state & (GOINGAWAY | DEPCOMPLETE)) !=
14656 (GOINGAWAY | DEPCOMPLETE) ||
14657 !TAILQ_EMPTY(&indirdep->ir_trunc) ||
14658 !LIST_EMPTY(&indirdep->ir_completehd) ||
14659 !LIST_EMPTY(&indirdep->ir_writehd) ||
14660 !LIST_EMPTY(&indirdep->ir_donehd) ||
14661 !LIST_EMPTY(&indirdep->ir_deplisthd) ||
14662 indirdep->ir_saveddata != NULL ||
14663 indirdep->ir_savebp == NULL) {
14664 printf("%s: skipping orphaned indirdep %p\n",
14665 __FUNCTION__, indirdep);
14666 continue;
14667 }
14668 printf("%s: freeing orphaned indirdep %p\n",
14669 __FUNCTION__, indirdep);
14670 bp = indirdep->ir_savebp;
14671 indirdep->ir_savebp = NULL;
14672 free_indirdep(indirdep);
14673 FREE_LOCK(ump);
14674 brelse(bp);
14675 while (!TRY_ACQUIRE_LOCK(ump)) {
14676 BO_UNLOCK(bo);
14677 ACQUIRE_LOCK(ump);
14678 FREE_LOCK(ump);
14679 BO_LOCK(bo);
14680 }
14681 }
14682 }
14683
14684 /*
14685 * Reasons for needing more work before suspend:
14686 * - Dirty buffers on devvp.
14687 * - Dependency structures still exist
14688 * - Softdep activity occurred after start of vnode sync loop
14689 * - Secondary writes occurred after start of vnode sync loop
14690 */
14691 error = 0;
14692 if (bo->bo_numoutput > 0 ||
14693 bo->bo_dirty.bv_cnt > 0 ||
14694 softdep_depcnt != unlinked ||
14695 ump->softdep_deps != unlinked ||
14696 softdep_accdepcnt != ump->softdep_accdeps ||
14697 secondary_writes != 0 ||
14698 mp->mnt_secondary_writes != 0 ||
14699 secondary_accwrites != mp->mnt_secondary_accwrites)
14700 error = EAGAIN;
14701 FREE_LOCK(ump);
14702 BO_UNLOCK(bo);
14703 return (error);
14704 }
14705
14706 /*
14707 * Get the number of dependency structures for the file system, both
14708 * the current number and the total number allocated. These will
14709 * later be used to detect that softdep processing has occurred.
14710 */
14711 void
softdep_get_depcounts(struct mount * mp,int * softdep_depsp,int * softdep_accdepsp)14712 softdep_get_depcounts(struct mount *mp,
14713 int *softdep_depsp,
14714 int *softdep_accdepsp)
14715 {
14716 struct ufsmount *ump;
14717
14718 if (MOUNTEDSOFTDEP(mp) == 0) {
14719 *softdep_depsp = 0;
14720 *softdep_accdepsp = 0;
14721 return;
14722 }
14723 ump = VFSTOUFS(mp);
14724 ACQUIRE_LOCK(ump);
14725 *softdep_depsp = ump->softdep_deps;
14726 *softdep_accdepsp = ump->softdep_accdeps;
14727 FREE_LOCK(ump);
14728 }
14729
14730 /*
14731 * Wait for pending output on a vnode to complete.
14732 */
14733 static void
drain_output(struct vnode * vp)14734 drain_output(struct vnode *vp)
14735 {
14736
14737 ASSERT_VOP_LOCKED(vp, "drain_output");
14738 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14739 }
14740
14741 /*
14742 * Called whenever a buffer that is being invalidated or reallocated
14743 * contains dependencies. This should only happen if an I/O error has
14744 * occurred. The routine is called with the buffer locked.
14745 */
14746 static void
softdep_deallocate_dependencies(struct buf * bp)14747 softdep_deallocate_dependencies(struct buf *bp)
14748 {
14749
14750 if ((bp->b_ioflags & BIO_ERROR) == 0)
14751 panic("softdep_deallocate_dependencies: dangling deps");
14752 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14753 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14754 else
14755 printf("softdep_deallocate_dependencies: "
14756 "got error %d while accessing filesystem\n", bp->b_error);
14757 if (bp->b_error != ENXIO)
14758 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14759 }
14760
14761 /*
14762 * Function to handle asynchronous write errors in the filesystem.
14763 */
14764 static void
softdep_error(char * func,int error)14765 softdep_error(char *func, int error)
14766 {
14767
14768 /* XXX should do something better! */
14769 printf("%s: got error %d while accessing filesystem\n", func, error);
14770 }
14771
14772 #ifdef DDB
14773
14774 /* exported to ffs_vfsops.c */
14775 extern void db_print_ffs(struct ufsmount *ump);
14776 void
db_print_ffs(struct ufsmount * ump)14777 db_print_ffs(struct ufsmount *ump)
14778 {
14779 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14780 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14781 db_printf(" fs %p ", ump->um_fs);
14782
14783 if (ump->um_softdep != NULL) {
14784 db_printf("su_wl %d su_deps %d su_req %d\n",
14785 ump->softdep_on_worklist, ump->softdep_deps,
14786 ump->softdep_req);
14787 } else {
14788 db_printf("su disabled\n");
14789 }
14790 }
14791
14792 static void
worklist_print(struct worklist * wk,int verbose)14793 worklist_print(struct worklist *wk, int verbose)
14794 {
14795
14796 if (!verbose) {
14797 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14798 wk->wk_state, PRINT_SOFTDEP_FLAGS);
14799 return;
14800 }
14801 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
14802 TYPENAME(wk->wk_type), wk->wk_state, PRINT_SOFTDEP_FLAGS,
14803 LIST_NEXT(wk, wk_list));
14804 db_print_ffs(VFSTOUFS(wk->wk_mp));
14805 }
14806
14807 static void
inodedep_print(struct inodedep * inodedep,int verbose)14808 inodedep_print(struct inodedep *inodedep, int verbose)
14809 {
14810
14811 worklist_print(&inodedep->id_list, 0);
14812 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14813 inodedep->id_fs,
14814 (intmax_t)inodedep->id_ino,
14815 (intmax_t)fsbtodb(inodedep->id_fs,
14816 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14817 (intmax_t)inodedep->id_nlinkdelta,
14818 (intmax_t)inodedep->id_savednlink);
14819
14820 if (verbose == 0)
14821 return;
14822
14823 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14824 inodedep->id_bmsafemap,
14825 inodedep->id_mkdiradd,
14826 TAILQ_FIRST(&inodedep->id_inoreflst));
14827 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
14828 LIST_FIRST(&inodedep->id_dirremhd),
14829 LIST_FIRST(&inodedep->id_pendinghd),
14830 LIST_FIRST(&inodedep->id_bufwait));
14831 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
14832 LIST_FIRST(&inodedep->id_inowait),
14833 TAILQ_FIRST(&inodedep->id_inoupdt),
14834 TAILQ_FIRST(&inodedep->id_newinoupdt));
14835 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
14836 TAILQ_FIRST(&inodedep->id_extupdt),
14837 TAILQ_FIRST(&inodedep->id_newextupdt),
14838 TAILQ_FIRST(&inodedep->id_freeblklst));
14839 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
14840 inodedep->id_savedino1,
14841 (intmax_t)inodedep->id_savedsize,
14842 (intmax_t)inodedep->id_savedextsize);
14843 }
14844
14845 static void
newblk_print(struct newblk * nbp)14846 newblk_print(struct newblk *nbp)
14847 {
14848
14849 worklist_print(&nbp->nb_list, 0);
14850 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14851 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
14852 &nbp->nb_jnewblk,
14853 &nbp->nb_bmsafemap,
14854 &nbp->nb_freefrag);
14855 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
14856 LIST_FIRST(&nbp->nb_indirdeps),
14857 LIST_FIRST(&nbp->nb_newdirblk),
14858 LIST_FIRST(&nbp->nb_jwork));
14859 }
14860
14861 static void
allocdirect_print(struct allocdirect * adp)14862 allocdirect_print(struct allocdirect *adp)
14863 {
14864
14865 newblk_print(&adp->ad_block);
14866 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
14867 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14868 db_printf(" offset %d, inodedep %p\n",
14869 adp->ad_offset, adp->ad_inodedep);
14870 }
14871
14872 static void
allocindir_print(struct allocindir * aip)14873 allocindir_print(struct allocindir *aip)
14874 {
14875
14876 newblk_print(&aip->ai_block);
14877 db_printf(" oldblkno %jd, lbn %jd\n",
14878 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
14879 db_printf(" offset %d, indirdep %p\n",
14880 aip->ai_offset, aip->ai_indirdep);
14881 }
14882
14883 static void
mkdir_print(struct mkdir * mkdir)14884 mkdir_print(struct mkdir *mkdir)
14885 {
14886
14887 worklist_print(&mkdir->md_list, 0);
14888 db_printf(" diradd %p, jaddref %p, buf %p\n",
14889 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
14890 }
14891
DB_SHOW_COMMAND(sd_inodedep,db_show_sd_inodedep)14892 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
14893 {
14894
14895 if (have_addr == 0) {
14896 db_printf("inodedep address required\n");
14897 return;
14898 }
14899 inodedep_print((struct inodedep*)addr, 1);
14900 }
14901
DB_SHOW_COMMAND(sd_allinodedeps,db_show_sd_allinodedeps)14902 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
14903 {
14904 struct inodedep_hashhead *inodedephd;
14905 struct inodedep *inodedep;
14906 struct ufsmount *ump;
14907 int cnt;
14908
14909 if (have_addr == 0) {
14910 db_printf("ufsmount address required\n");
14911 return;
14912 }
14913 ump = (struct ufsmount *)addr;
14914 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14915 inodedephd = &ump->inodedep_hashtbl[cnt];
14916 LIST_FOREACH(inodedep, inodedephd, id_hash) {
14917 inodedep_print(inodedep, 0);
14918 }
14919 }
14920 }
14921
DB_SHOW_COMMAND(sd_worklist,db_show_sd_worklist)14922 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
14923 {
14924
14925 if (have_addr == 0) {
14926 db_printf("worklist address required\n");
14927 return;
14928 }
14929 worklist_print((struct worklist *)addr, 1);
14930 }
14931
DB_SHOW_COMMAND(sd_workhead,db_show_sd_workhead)14932 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
14933 {
14934 struct worklist *wk;
14935 struct workhead *wkhd;
14936
14937 if (have_addr == 0) {
14938 db_printf("worklist address required "
14939 "(for example value in bp->b_dep)\n");
14940 return;
14941 }
14942 /*
14943 * We often do not have the address of the worklist head but
14944 * instead a pointer to its first entry (e.g., we have the
14945 * contents of bp->b_dep rather than &bp->b_dep). But the back
14946 * pointer of bp->b_dep will point at the head of the list, so
14947 * we cheat and use that instead. If we are in the middle of
14948 * a list we will still get the same result, so nothing
14949 * unexpected will result.
14950 */
14951 wk = (struct worklist *)addr;
14952 if (wk == NULL)
14953 return;
14954 wkhd = (struct workhead *)wk->wk_list.le_prev;
14955 LIST_FOREACH(wk, wkhd, wk_list) {
14956 switch(wk->wk_type) {
14957 case D_INODEDEP:
14958 inodedep_print(WK_INODEDEP(wk), 0);
14959 continue;
14960 case D_ALLOCDIRECT:
14961 allocdirect_print(WK_ALLOCDIRECT(wk));
14962 continue;
14963 case D_ALLOCINDIR:
14964 allocindir_print(WK_ALLOCINDIR(wk));
14965 continue;
14966 case D_MKDIR:
14967 mkdir_print(WK_MKDIR(wk));
14968 continue;
14969 default:
14970 worklist_print(wk, 0);
14971 continue;
14972 }
14973 }
14974 }
14975
DB_SHOW_COMMAND(sd_mkdir,db_show_sd_mkdir)14976 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
14977 {
14978 if (have_addr == 0) {
14979 db_printf("mkdir address required\n");
14980 return;
14981 }
14982 mkdir_print((struct mkdir *)addr);
14983 }
14984
DB_SHOW_COMMAND(sd_mkdir_list,db_show_sd_mkdir_list)14985 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
14986 {
14987 struct mkdirlist *mkdirlisthd;
14988 struct mkdir *mkdir;
14989
14990 if (have_addr == 0) {
14991 db_printf("mkdir listhead address required\n");
14992 return;
14993 }
14994 mkdirlisthd = (struct mkdirlist *)addr;
14995 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14996 mkdir_print(mkdir);
14997 if (mkdir->md_diradd != NULL) {
14998 db_printf(" ");
14999 worklist_print(&mkdir->md_diradd->da_list, 0);
15000 }
15001 if (mkdir->md_jaddref != NULL) {
15002 db_printf(" ");
15003 worklist_print(&mkdir->md_jaddref->ja_list, 0);
15004 }
15005 }
15006 }
15007
DB_SHOW_COMMAND(sd_allocdirect,db_show_sd_allocdirect)15008 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
15009 {
15010 if (have_addr == 0) {
15011 db_printf("allocdirect address required\n");
15012 return;
15013 }
15014 allocdirect_print((struct allocdirect *)addr);
15015 }
15016
DB_SHOW_COMMAND(sd_allocindir,db_show_sd_allocindir)15017 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
15018 {
15019 if (have_addr == 0) {
15020 db_printf("allocindir address required\n");
15021 return;
15022 }
15023 allocindir_print((struct allocindir *)addr);
15024 }
15025
15026 #endif /* DDB */
15027
15028 #endif /* SOFTUPDATES */
15029